1 /* $OpenBSD: hayes.c,v 1.13 2006/03/17 19:17:13 moritz 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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 static char sccsid[] = "@(#)hayes.c 8.1 (Berkeley) 6/6/93";
39 static const char rcsid[] = "$OpenBSD: hayes.c,v 1.13 2006/03/17 19:17:13 moritz Exp $";
44 * Routines for calling up on a Hayes Modem
45 * (based on the old VenTel driver).
46 * The modem is expected to be strapped for "echo".
47 * Also, the switches enabling the DTR and CD lines
48 * must be set correctly.
50 * The easy way to hang up a modem is always simply to
51 * clear the DTR signal. However, if the +++ sequence
52 * (which switches the modem back to local mode) is sent
53 * before modem is hung up, removal of the DTR signal
54 * has no effect (except that it prevents the modem from
55 * recognizing commands).
56 * (by Helge Skrivervik, Calma Company, Sunnyvale, CA. 1984)
60 * It is probably not a good idea to switch the modem
61 * state between 'verbose' and terse (status messages).
62 * This should be kicked out and we should use verbose
63 * mode only. This would make it consistent with normal
64 * interactive use thru the command 'tip dialer'.
69 #include <sys/ioctl.h>
71 #define min(a,b) ((a < b) ? a : b)
73 static int dialtimeout = 0;
74 static jmp_buf timeoutbuf;
77 static char dumbuf[DUMBUFLEN];
83 static int state = IDLE;
85 static void sigALRM(int);
86 static char gobble(char *);
87 static void error_rep(char);
88 static void goodbye(void);
89 static int hay_sync(void);
92 hay_dialer(char *num, char *acu)
101 if (hay_sync() == 0) /* make sure we can talk to the modem */
103 if (boolean(value(VERBOSE)))
104 printf("\ndialing...");
106 tcgetattr(FD, &cntrl);
107 cntrl.c_cflag |= HUPCL;
108 tcsetattr(FD, TCSANOW, &cntrl);
109 tcflush(FD, TCIOFLUSH);
110 write(FD, "ATv0\r", 5); /* tell modem to use short status codes */
113 write(FD, "ATTD", 4); /* send dial command */
114 for (cp = num; *cp; cp++)
117 write(FD, num, strlen(num));
122 if ((dummy = gobble("01234")) != '1')
131 return (connected); /* lets get out of here.. */
133 tcflush(FD, TCIOFLUSH);
136 (void)snprintf(line, sizeof line, "%ld second dial timeout",
137 number(value(DIALTIMEOUT)));
138 logent(value(HOST), num, "hayes", line);
142 hay_disconnect(); /* insurance */
149 /* first hang up the modem*/
151 printf("\rdisconnecting modem....\n\r");
153 ioctl(FD, TIOCCDTR, 0);
155 ioctl(FD, TIOCSDTR, 0);
162 write(FD, "\r", 1); /* send anything to abort the call */
170 printf("\07timeout waiting for reply\n\r");
172 longjmp(timeoutbuf, 1);
182 f = signal(SIGALRM, sigALRM);
185 printf("\ngobble: waiting for %s\n", match);
188 if (setjmp(timeoutbuf)) {
192 alarm(number(value(DIALTIMEOUT)));
197 printf("%c 0x%x ", c, c);
199 for (i = 0; i < strlen(match); i++)
202 } while (status == 0);
203 signal(SIGALRM, SIG_DFL);
229 printf("NO CARRIER");
233 printf("ERROR in input");
237 printf("CONNECT 1200");
241 printf("Unknown Modem error: %c (0x%x)", c, c);
248 * set modem back to normal verbose status codes.
256 tcflush(FD, TCIOFLUSH);
260 tcflush(FD, TCIOFLUSH);
262 write(FD, "ATH0\r", 5); /* insurance */
265 if (c != '0' && c != '3') {
266 printf("cannot hang up modem\n\r");
267 printf("please use 'tip dialer' to make sure the line is hung up\n\r");
271 ioctl(FD, FIONREAD, &len);
273 printf("goodbye1: len=%d -- ", len);
274 rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
276 printf("read (%d): %s\r\n", rlen, dumbuf);
278 write(FD, "ATv1\r", 5);
281 ioctl(FD, FIONREAD, &len);
282 printf("goodbye2: len=%d -- ", len);
283 rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
285 printf("read (%d): %s\r\n", rlen, dumbuf);
288 tcflush(FD, TCIOFLUSH);
289 ioctl(FD, TIOCCDTR, 0); /* clear DTR (insurance) */
300 while (retry++ <= MAXRETRY) {
301 write(FD, "AT\r", 3);
303 ioctl(FD, FIONREAD, &len);
305 len = read(FD, dumbuf, min(len, DUMBUFLEN));
306 if (strchr(dumbuf, '0') ||
307 (strchr(dumbuf, 'O') && strchr(dumbuf, 'K')))
311 printf("hay_sync: (\"%s\") %d\n\r", dumbuf, retry);
314 ioctl(FD, TIOCCDTR, 0);
315 ioctl(FD, TIOCSDTR, 0);
317 printf("Cannot synchronize with hayes...\n\r");