xref: /netbsd-src/usr.bin/tip/aculib/hayes.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: hayes.c,v 1.13 2006/04/03 04:53:59 christos 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[] = "@(#)hayes.c	8.1 (Berkeley) 6/6/93";
36 #endif
37 __RCSID("$NetBSD: hayes.c,v 1.13 2006/04/03 04:53:59 christos Exp $");
38 #endif /* not lint */
39 
40 /*
41  * Routines for calling up on a Hayes Modem
42  * (based on the old VenTel driver).
43  * The modem is expected to be strapped for "echo".
44  * Also, the switches enabling the DTR and CD lines
45  * must be set correctly.
46  * NOTICE:
47  * The easy way to hang up a modem is always simply to
48  * clear the DTR signal. However, if the +++ sequence
49  * (which switches the modem back to local mode) is sent
50  * before modem is hung up, removal of the DTR signal
51  * has no effect (except that it prevents the modem from
52  * recognizing commands).
53  * (by Helge Skrivervik, Calma Company, Sunnyvale, CA. 1984)
54  */
55 /*
56  * TODO:
57  * It is probably not a good idea to switch the modem
58  * state between 'verbose' and terse (status messages).
59  * This should be kicked out and we should use verbose
60  * mode only. This would make it consistent with normal
61  * interactive use thru the command 'tip dialer'.
62  */
63 #include "tip.h"
64 
65 #define	min(a,b)	((a < b) ? a : b)
66 
67 static	int timeout = 0;
68 static	jmp_buf timeoutbuf;
69 #define DUMBUFLEN	40
70 static char dumbuf[DUMBUFLEN];
71 
72 static	void	error_rep(char);
73 static	char	gobble(const char *);
74 static	void	goodbye(void);
75 static	int	hay_sync(void);
76 static	void	sigALRM(int);
77 
78 int
79 /*ARGSUSED*/
80 hay_dialer(char *num, char *acu)
81 {
82 	char *cp;
83 	int connected = 0;
84 	char dummy;
85 	struct termios cntrl;
86 
87 	if (hay_sync() == 0)		/* make sure we can talk to the modem */
88 		return(0);
89 	if (boolean(value(VERBOSE)))
90 		printf("\ndialing...");
91 	fflush(stdout);
92 	tcgetattr(FD, &cntrl);
93 	cntrl.c_cflag |= HUPCL;
94 	tcsetattr(FD, TCSANOW, &cntrl);
95 	tcflush(FD, TCIOFLUSH);
96 	write(FD, "ATv0\r", 5);	/* tell modem to use short status codes */
97 	gobble("\r");
98 	gobble("\r");
99 	write(FD, "ATTD", 4);	/* send dial command */
100 	for (cp = num; *cp; cp++)
101 		if (*cp == '=')
102 			*cp = ',';
103 	write(FD, num, strlen(num));
104 	write(FD, "\r", 1);
105 	connected = 0;
106 	if (gobble("\r")) {
107 		if ((dummy = gobble("01234")) != '1')
108 			error_rep(dummy);
109 		else
110 			connected = 1;
111 	}
112 	if (!connected)
113 		return (connected);	/* lets get out of here.. */
114 	tcflush(FD, TCIOFLUSH);
115 	if (timeout)
116 		hay_disconnect();	/* insurance */
117 	return (connected);
118 }
119 
120 
121 void
122 hay_disconnect(void)
123 {
124 
125 	/* first hang up the modem*/
126 #ifdef DEBUG
127 	printf("\rdisconnecting modem....\n\r");
128 #endif
129 	ioctl(FD, TIOCCDTR, 0);
130 	sleep(1);
131 	ioctl(FD, TIOCSDTR, 0);
132 	goodbye();
133 }
134 
135 void
136 hay_abort(void)
137 {
138 
139 	write(FD, "\r", 1);	/* send anything to abort the call */
140 	hay_disconnect();
141 }
142 
143 static void
144 /*ARGSUSED*/
145 sigALRM(int dummy)
146 {
147 
148 	printf("\07timeout waiting for reply\n\r");
149 	timeout = 1;
150 	longjmp(timeoutbuf, 1);
151 }
152 
153 static char
154 gobble(const char *match)
155 {
156 	char c;
157 	sig_t f;
158 	int i, status = 0;
159 
160 #if __GNUC__	/* XXX pacify gcc */
161 	(void)&status;
162 #endif
163 
164 	f = signal(SIGALRM, sigALRM);
165 	timeout = 0;
166 #ifdef DEBUG
167 	printf("\ngobble: waiting for %s\n", match);
168 #endif
169 	do {
170 		if (setjmp(timeoutbuf)) {
171 			signal(SIGALRM, f);
172 			return (0);
173 		}
174 		alarm((unsigned int)number(value(DIALTIMEOUT)));
175 		read(FD, &c, 1);
176 		alarm(0);
177 		c &= 0177;
178 #ifdef DEBUG
179 		printf("%c 0x%x ", c, c);
180 #endif
181 		for (i = 0; i < strlen(match); i++)
182 			if (c == match[i])
183 				status = c;
184 	} while (status == 0);
185 	signal(SIGALRM, SIG_DFL);
186 #ifdef DEBUG
187 	printf("\n");
188 #endif
189 	return (status);
190 }
191 
192 static void
193 error_rep(char c)
194 {
195 
196 	printf("\n\r");
197 	switch (c) {
198 
199 	case '0':
200 		printf("OK");
201 		break;
202 
203 	case '1':
204 		printf("CONNECT");
205 		break;
206 
207 	case '2':
208 		printf("RING");
209 		break;
210 
211 	case '3':
212 		printf("NO CARRIER");
213 		break;
214 
215 	case '4':
216 		printf("ERROR in input");
217 		break;
218 
219 	case '5':
220 		printf("CONNECT 1200");
221 		break;
222 
223 	default:
224 		printf("Unknown Modem error: %c (0x%x)", c, c);
225 	}
226 	printf("\n\r");
227 	return;
228 }
229 
230 /*
231  * set modem back to normal verbose status codes.
232  */
233 void
234 goodbye(void)
235 {
236 	int len;
237 	char c;
238 
239 	tcflush(FD, TCIOFLUSH);
240 	if (hay_sync()) {
241 		sleep(1);
242 #ifndef DEBUG
243 		tcflush(FD, TCIOFLUSH);
244 #endif
245 		write(FD, "ATH0\r", 5);		/* insurance */
246 #ifndef DEBUG
247 		c = gobble("03");
248 		if (c != '0' && c != '3') {
249 			printf("cannot hang up modem\n\r");
250 			printf("please use 'tip dialer' to make sure the line is hung up\n\r");
251 		}
252 #endif
253 		sleep(1);
254 		ioctl(FD, FIONREAD, &len);
255 #ifdef DEBUG
256 		printf("goodbye1: len=%d -- ", len);
257 		rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
258 		dumbuf[rlen] = '\0';
259 		printf("read (%d): %s\r\n", rlen, dumbuf);
260 #endif
261 		write(FD, "ATv1\r", 5);
262 		sleep(1);
263 #ifdef DEBUG
264 		ioctl(FD, FIONREAD, &len);
265 		printf("goodbye2: len=%d -- ", len);
266 		rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
267 		dumbuf[rlen] = '\0';
268 		printf("read (%d): %s\r\n", rlen, dumbuf);
269 #endif
270 	}
271 	tcflush(FD, TCIOFLUSH);
272 	ioctl(FD, TIOCCDTR, 0);		/* clear DTR (insurance) */
273 	close(FD);
274 }
275 
276 #define MAXRETRY	5
277 
278 int
279 hay_sync(void)
280 {
281 	int len, retry = 0;
282 
283 	while (retry++ <= MAXRETRY) {
284 		write(FD, "AT\r", 3);
285 		sleep(1);
286 		ioctl(FD, FIONREAD, &len);
287 		if (len) {
288 			len = read(FD, dumbuf, (size_t)min(len, DUMBUFLEN));
289 			if (strchr(dumbuf, '0') ||
290 		   	(strchr(dumbuf, 'O') && strchr(dumbuf, 'K')))
291 				return(1);
292 #ifdef DEBUG
293 			dumbuf[len] = '\0';
294 			printf("hay_sync: (\"%s\") %d\n\r", dumbuf, retry);
295 #endif
296 		}
297 		ioctl(FD, TIOCCDTR, 0);
298 		ioctl(FD, TIOCSDTR, 0);
299 	}
300 	printf("Cannot synchronize with hayes...\n\r");
301 	return(0);
302 }
303