xref: /netbsd-src/usr.bin/tip/aculib/biz22.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: biz22.c,v 1.11 2006/04/03 02:25:27 perry Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)biz22.c	8.1 (Berkeley) 6/6/93";
36 #endif
37 __RCSID("$NetBSD: biz22.c,v 1.11 2006/04/03 02:25:27 perry Exp $");
38 #endif /* not lint */
39 
40 #include "tip.h"
41 
42 #define DISCONNECT_CMD	"\20\04"	/* disconnection string */
43 
44 static	int btimeout = 0;
45 static	jmp_buf timeoutbuf;
46 
47 static	int	biz_dialer(char *, const char *);
48 static	int	cmd(const char *);
49 static	int	detect(const char *);
50 static	void	sigALRM(int);
51 
52 /*
53  * Dial up on a BIZCOMP Model 1022 with either
54  * 	tone dialing (mod = "V")
55  *	pulse dialing (mod = "W")
56  */
57 static int
58 biz_dialer(char *num, const char *mod)
59 {
60 	int connected = 0;
61 	char cbuf[40];
62 
63 	if (boolean(value(VERBOSE)))
64 		printf("\nstarting call...");
65 	/*
66 	 * Disable auto-answer and configure for tone/pulse
67 	 *  dialing
68 	 */
69 	if (cmd("\02K\r")) {
70 		printf("can't initialize bizcomp...");
71 		return (0);
72 	}
73 	(void)strncpy(cbuf, "\02.\r", sizeof(cbuf) - 1);
74 	cbuf[1] = *mod;
75 	if (cmd(cbuf)) {
76 		printf("can't set dialing mode...");
77 		return (0);
78 	}
79 	(void)snprintf(cbuf, sizeof cbuf, "\02D%s\r", num);
80 	write(FD, cbuf, strlen(cbuf));
81 	if (!detect("7\r")) {
82 		printf("can't get dial tone...");
83 		return (0);
84 	}
85 	if (boolean(value(VERBOSE)))
86 		printf("ringing...");
87 	/*
88 	 * The reply from the BIZCOMP should be:
89 	 *	2 \r or 7 \r	failure
90 	 *	1 \r		success
91 	 */
92 	connected = detect("1\r");
93 	if (btimeout)
94 		biz22_disconnect();	/* insurance */
95 	return (connected);
96 }
97 
98 int
99 biz22w_dialer(char *num, char *acu)
100 {
101 
102 	return (biz_dialer(num, "W"));
103 }
104 
105 int
106 biz22f_dialer(char *num, char *acu)
107 {
108 
109 	return (biz_dialer(num, "V"));
110 }
111 
112 void
113 biz22_disconnect(void)
114 {
115 
116 	write(FD, DISCONNECT_CMD, 4);
117 	sleep(2);
118 	tcflush(FD, TCIOFLUSH);
119 }
120 
121 void
122 biz22_abort(void)
123 {
124 
125 	write(FD, "\02", 1);
126 }
127 
128 static void
129 sigALRM(int dummy)
130 {
131 
132 	btimeout = 1;
133 	longjmp(timeoutbuf, 1);
134 }
135 
136 static int
137 cmd(const char *s)
138 {
139 	sig_t f;
140 	char c;
141 
142 	write(FD, s, strlen(s));
143 	f = signal(SIGALRM, sigALRM);
144 	if (setjmp(timeoutbuf)) {
145 		biz22_abort();
146 		signal(SIGALRM, f);
147 		return (1);
148 	}
149 	alarm(number(value(DIALTIMEOUT)));
150 	read(FD, &c, 1);
151 	alarm(0);
152 	signal(SIGALRM, f);
153 	c &= 0177;
154 	return (c != '\r');
155 }
156 
157 static int
158 detect(const char *s)
159 {
160 	sig_t f;
161 	char c;
162 
163 #if __GNUC__	/* XXX pacify gcc */
164 	(void)&s;
165 #endif
166 
167 	f = signal(SIGALRM, sigALRM);
168 	btimeout = 0;
169 	while (*s) {
170 		if (setjmp(timeoutbuf)) {
171 			biz22_abort();
172 			break;
173 		}
174 		alarm(number(value(DIALTIMEOUT)));
175 		read(FD, &c, 1);
176 		alarm(0);
177 		c &= 0177;
178 		if (c != *s++)
179 			return (0);
180 	}
181 	signal(SIGALRM, f);
182 	return (btimeout == 0);
183 }
184