1 /* $OpenBSD: hayes.c,v 1.8 2001/10/24 18:38:58 millert Exp $ */
2 /* $NetBSD: hayes.c,v 1.6 1997/02/11 09:24:17 mrg Exp $ */
5 * Copyright (c) 1983, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
42 static char sccsid[] = "@(#)hayes.c 8.1 (Berkeley) 6/6/93";
43 static char rcsid[] = "$OpenBSD: hayes.c,v 1.8 2001/10/24 18:38:58 millert Exp $";
48 * Routines for calling up on a Hayes Modem
49 * (based on the old VenTel driver).
50 * The modem is expected to be strapped for "echo".
51 * Also, the switches enabling the DTR and CD lines
52 * must be set correctly.
54 * The easy way to hang up a modem is always simply to
55 * clear the DTR signal. However, if the +++ sequence
56 * (which switches the modem back to local mode) is sent
57 * before modem is hung up, removal of the DTR signal
58 * has no effect (except that it prevents the modem from
59 * recognizing commands).
60 * (by Helge Skrivervik, Calma Company, Sunnyvale, CA. 1984)
64 * It is probably not a good idea to switch the modem
65 * state between 'verbose' and terse (status messages).
66 * This should be kicked out and we should use verbose
67 * mode only. This would make it consistent with normal
68 * interactive use thru the command 'tip dialer'.
73 #include <sys/ioctl.h>
75 #define min(a,b) ((a < b) ? a : b)
77 static void error_rep(char c);
78 static void goodbye(void);
79 static void sigALRM();
80 static int timeout = 0;
81 static jmp_buf timeoutbuf;
84 static char dumbuf[DUMBUFLEN];
90 static int state = IDLE;
100 struct termios cntrl;
104 if (hay_sync() == 0) /* make sure we can talk to the modem */
106 if (boolean(value(VERBOSE)))
107 printf("\ndialing...");
109 tcgetattr(FD, &cntrl);
110 cntrl.c_cflag |= HUPCL;
111 tcsetattr(FD, TCSANOW, &cntrl);
112 tcflush(FD, TCIOFLUSH);
113 write(FD, "ATv0\r", 5); /* tell modem to use short status codes */
116 write(FD, "ATTD", 4); /* send dial command */
117 for (cp = num; *cp; cp++)
120 write(FD, num, strlen(num));
125 if ((dummy = gobble("01234")) != '1')
134 return (connected); /* lets get out of here.. */
136 tcflush(FD, TCIOFLUSH);
139 (void)sprintf(line, "%ld second dial timeout",
140 number(value(DIALTIMEOUT)));
141 logent(value(HOST), num, "hayes", line);
145 hay_disconnect(); /* insurance */
153 /* first hang up the modem*/
155 printf("\rdisconnecting modem....\n\r");
157 ioctl(FD, TIOCCDTR, 0);
159 ioctl(FD, TIOCSDTR, 0);
167 write(FD, "\r", 1); /* send anything to abort the call */
175 printf("\07timeout waiting for reply\n\r");
177 longjmp(timeoutbuf, 1);
188 f = signal(SIGALRM, sigALRM);
191 printf("\ngobble: waiting for %s\n", match);
194 if (setjmp(timeoutbuf)) {
198 alarm(number(value(DIALTIMEOUT)));
203 printf("%c 0x%x ", c, c);
205 for (i = 0; i < strlen(match); i++)
208 } while (status == 0);
209 signal(SIGALRM, SIG_DFL);
236 printf("NO CARRIER");
240 printf("ERROR in input");
244 printf("CONNECT 1200");
248 printf("Unknown Modem error: %c (0x%x)", c, c);
255 * set modem back to normal verbose status codes.
263 tcflush(FD, TCIOFLUSH);
267 tcflush(FD, TCIOFLUSH);
269 write(FD, "ATH0\r", 5); /* insurance */
272 if (c != '0' && c != '3') {
273 printf("cannot hang up modem\n\r");
274 printf("please use 'tip dialer' to make sure the line is hung up\n\r");
278 ioctl(FD, FIONREAD, &len);
280 printf("goodbye1: len=%d -- ", len);
281 rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
283 printf("read (%d): %s\r\n", rlen, dumbuf);
285 write(FD, "ATv1\r", 5);
288 ioctl(FD, FIONREAD, &len);
289 printf("goodbye2: len=%d -- ", len);
290 rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
292 printf("read (%d): %s\r\n", rlen, dumbuf);
295 tcflush(FD, TCIOFLUSH);
296 ioctl(FD, TIOCCDTR, 0); /* clear DTR (insurance) */
307 while (retry++ <= MAXRETRY) {
308 write(FD, "AT\r", 3);
310 ioctl(FD, FIONREAD, &len);
312 len = read(FD, dumbuf, min(len, DUMBUFLEN));
313 if (strchr(dumbuf, '0') ||
314 (strchr(dumbuf, 'O') && strchr(dumbuf, 'K')))
318 printf("hay_sync: (\"%s\") %d\n\r", dumbuf, retry);
321 ioctl(FD, TIOCCDTR, 0);
322 ioctl(FD, TIOCSDTR, 0);
324 printf("Cannot synchronize with hayes...\n\r");