xref: /netbsd-src/usr.bin/tip/aculib/hayes.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: hayes.c,v 1.8 1998/07/12 09:14:20 mrg 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)hayes.c	8.1 (Berkeley) 6/6/93";
40 #endif
41 __RCSID("$NetBSD: hayes.c,v 1.8 1998/07/12 09:14:20 mrg Exp $");
42 #endif /* not lint */
43 
44 /*
45  * Routines for calling up on a Hayes Modem
46  * (based on the old VenTel driver).
47  * The modem is expected to be strapped for "echo".
48  * Also, the switches enabling the DTR and CD lines
49  * must be set correctly.
50  * NOTICE:
51  * The easy way to hang up a modem is always simply to
52  * clear the DTR signal. However, if the +++ sequence
53  * (which switches the modem back to local mode) is sent
54  * before modem is hung up, removal of the DTR signal
55  * has no effect (except that it prevents the modem from
56  * recognizing commands).
57  * (by Helge Skrivervik, Calma Company, Sunnyvale, CA. 1984)
58  */
59 /*
60  * TODO:
61  * It is probably not a good idea to switch the modem
62  * state between 'verbose' and terse (status messages).
63  * This should be kicked out and we should use verbose
64  * mode only. This would make it consistent with normal
65  * interactive use thru the command 'tip dialer'.
66  */
67 #include "tip.h"
68 
69 #define	min(a,b)	((a < b) ? a : b)
70 
71 static	int timeout = 0;
72 static	jmp_buf timeoutbuf;
73 #define DUMBUFLEN	40
74 static char dumbuf[DUMBUFLEN];
75 
76 #define	DIALING		1
77 #define IDLE		2
78 #define CONNECTED	3
79 #define	FAILED		4
80 static	int state = IDLE;
81 
82 static	void	error_rep __P((char));
83 static	char	gobble __P((char *));
84 static	void	goodbye __P((void));
85 static	int	hay_sync __P((void));
86 static	void	sigALRM __P((int));
87 
88 int
89 hay_dialer(num, acu)
90 	char *num;
91 	char *acu;
92 {
93 	char *cp;
94 	int connected = 0;
95 	char dummy;
96 	struct termios cntrl;
97 #ifdef ACULOG
98 	char line[80];
99 #endif
100 	if (hay_sync() == 0)		/* make sure we can talk to the modem */
101 		return(0);
102 	if (boolean(value(VERBOSE)))
103 		printf("\ndialing...");
104 	fflush(stdout);
105 	tcgetattr(FD, &cntrl);
106 	cntrl.c_cflag |= HUPCL;
107 	tcsetattr(FD, TCSANOW, &cntrl);
108 	tcflush(FD, TCIOFLUSH);
109 	write(FD, "ATv0\r", 5);	/* tell modem to use short status codes */
110 	gobble("\r");
111 	gobble("\r");
112 	write(FD, "ATTD", 4);	/* send dial command */
113 	for (cp = num; *cp; cp++)
114 		if (*cp == '=')
115 			*cp = ',';
116 	write(FD, num, strlen(num));
117 	state = DIALING;
118 	write(FD, "\r", 1);
119 	connected = 0;
120 	if (gobble("\r")) {
121 		if ((dummy = gobble("01234")) != '1')
122 			error_rep(dummy);
123 		else
124 			connected = 1;
125 	}
126 	if (connected)
127 		state = CONNECTED;
128 	else {
129 		state = FAILED;
130 		return (connected);	/* lets get out of here.. */
131 	}
132 	tcflush(FD, TCIOFLUSH);
133 #ifdef ACULOG
134 	if (timeout) {
135 		(void)snprintf(line, sizeof line, "%d second dial timeout",
136 			(int)number(value(DIALTIMEOUT)));
137 		logent(value(HOST), num, "hayes", line);
138 	}
139 #endif
140 	if (timeout)
141 		hay_disconnect();	/* insurance */
142 	return (connected);
143 }
144 
145 
146 void
147 hay_disconnect()
148 {
149 
150 	/* first hang up the modem*/
151 #ifdef DEBUG
152 	printf("\rdisconnecting modem....\n\r");
153 #endif
154 	ioctl(FD, TIOCCDTR, 0);
155 	sleep(1);
156 	ioctl(FD, TIOCSDTR, 0);
157 	goodbye();
158 }
159 
160 void
161 hay_abort()
162 {
163 
164 	write(FD, "\r", 1);	/* send anything to abort the call */
165 	hay_disconnect();
166 }
167 
168 static void
169 sigALRM(dummy)
170 	int dummy;
171 {
172 
173 	printf("\07timeout waiting for reply\n\r");
174 	timeout = 1;
175 	longjmp(timeoutbuf, 1);
176 }
177 
178 static char
179 gobble(match)
180 	char *match;
181 {
182 	char c;
183 	sig_t f;
184 	int i, status = 0;
185 
186 #if __GNUC__	/* XXX pacify gcc */
187 	(void)&status;
188 #endif
189 
190 	f = signal(SIGALRM, sigALRM);
191 	timeout = 0;
192 #ifdef DEBUG
193 	printf("\ngobble: waiting for %s\n", match);
194 #endif
195 	do {
196 		if (setjmp(timeoutbuf)) {
197 			signal(SIGALRM, f);
198 			return (0);
199 		}
200 		alarm(number(value(DIALTIMEOUT)));
201 		read(FD, &c, 1);
202 		alarm(0);
203 		c &= 0177;
204 #ifdef DEBUG
205 		printf("%c 0x%x ", c, c);
206 #endif
207 		for (i = 0; i < strlen(match); i++)
208 			if (c == match[i])
209 				status = c;
210 	} while (status == 0);
211 	signal(SIGALRM, SIG_DFL);
212 #ifdef DEBUG
213 	printf("\n");
214 #endif
215 	return (status);
216 }
217 
218 static void
219 error_rep(c)
220 	char c;
221 {
222 
223 	printf("\n\r");
224 	switch (c) {
225 
226 	case '0':
227 		printf("OK");
228 		break;
229 
230 	case '1':
231 		printf("CONNECT");
232 		break;
233 
234 	case '2':
235 		printf("RING");
236 		break;
237 
238 	case '3':
239 		printf("NO CARRIER");
240 		break;
241 
242 	case '4':
243 		printf("ERROR in input");
244 		break;
245 
246 	case '5':
247 		printf("CONNECT 1200");
248 		break;
249 
250 	default:
251 		printf("Unknown Modem error: %c (0x%x)", c, c);
252 	}
253 	printf("\n\r");
254 	return;
255 }
256 
257 /*
258  * set modem back to normal verbose status codes.
259  */
260 void
261 goodbye()
262 {
263 	int len;
264 	char c;
265 
266 	tcflush(FD, TCIOFLUSH);
267 	if (hay_sync()) {
268 		sleep(1);
269 #ifndef DEBUG
270 		tcflush(FD, TCIOFLUSH);
271 #endif
272 		write(FD, "ATH0\r", 5);		/* insurance */
273 #ifndef DEBUG
274 		c = gobble("03");
275 		if (c != '0' && c != '3') {
276 			printf("cannot hang up modem\n\r");
277 			printf("please use 'tip dialer' to make sure the line is hung up\n\r");
278 		}
279 #endif
280 		sleep(1);
281 		ioctl(FD, FIONREAD, &len);
282 #ifdef DEBUG
283 		printf("goodbye1: len=%d -- ", len);
284 		rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
285 		dumbuf[rlen] = '\0';
286 		printf("read (%d): %s\r\n", rlen, dumbuf);
287 #endif
288 		write(FD, "ATv1\r", 5);
289 		sleep(1);
290 #ifdef DEBUG
291 		ioctl(FD, FIONREAD, &len);
292 		printf("goodbye2: len=%d -- ", len);
293 		rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
294 		dumbuf[rlen] = '\0';
295 		printf("read (%d): %s\r\n", rlen, dumbuf);
296 #endif
297 	}
298 	tcflush(FD, TCIOFLUSH);
299 	ioctl(FD, TIOCCDTR, 0);		/* clear DTR (insurance) */
300 	close(FD);
301 }
302 
303 #define MAXRETRY	5
304 
305 int
306 hay_sync()
307 {
308 	int len, retry = 0;
309 
310 	while (retry++ <= MAXRETRY) {
311 		write(FD, "AT\r", 3);
312 		sleep(1);
313 		ioctl(FD, FIONREAD, &len);
314 		if (len) {
315 			len = read(FD, dumbuf, min(len, DUMBUFLEN));
316 			if (strchr(dumbuf, '0') ||
317 		   	(strchr(dumbuf, 'O') && strchr(dumbuf, 'K')))
318 				return(1);
319 #ifdef DEBUG
320 			dumbuf[len] = '\0';
321 			printf("hay_sync: (\"%s\") %d\n\r", dumbuf, retry);
322 #endif
323 		}
324 		ioctl(FD, TIOCCDTR, 0);
325 		ioctl(FD, TIOCSDTR, 0);
326 	}
327 	printf("Cannot synchronize with hayes...\n\r");
328 	return(0);
329 }
330