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 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (c) 1983, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 static char sccsid[] = "@(#)hayes.c 8.1 (Berkeley) 6/6/93";
41 static const char rcsid[] = "$OpenBSD: hayes.c,v 1.13 2006/03/17 19:17:13 moritz Exp $";
46 * Routines for calling up on a Hayes Modem
47 * (based on the old VenTel driver).
48 * The modem is expected to be strapped for "echo".
49 * Also, the switches enabling the DTR and CD lines
50 * must be set correctly.
52 * The easy way to hang up a modem is always simply to
53 * clear the DTR signal. However, if the +++ sequence
54 * (which switches the modem back to local mode) is sent
55 * before modem is hung up, removal of the DTR signal
56 * has no effect (except that it prevents the modem from
57 * recognizing commands).
58 * (by Helge Skrivervik, Calma Company, Sunnyvale, CA. 1984)
62 * It is probably not a good idea to switch the modem
63 * state between 'verbose' and terse (status messages).
64 * This should be kicked out and we should use verbose
65 * mode only. This would make it consistent with normal
66 * interactive use thru the command 'tip dialer'.
71 #include <sys/ioctl.h>
73 #define min(a,b) ((a < b) ? a : b)
75 static int dialtimeout = 0;
76 static jmp_buf timeoutbuf;
79 static char dumbuf[DUMBUFLEN];
85 static int state = IDLE;
87 static void sigALRM(int);
88 static char gobble(char *);
89 static void error_rep(char);
90 static void goodbye(void);
91 static int hay_sync(void);
94 hay_dialer(char *num, char *acu)
103 if (hay_sync() == 0) /* make sure we can talk to the modem */
105 if (boolean(value(VERBOSE)))
106 printf("\ndialing...");
108 tcgetattr(FD, &cntrl);
109 cntrl.c_cflag |= HUPCL;
110 tcsetattr(FD, TCSANOW, &cntrl);
111 tcflush(FD, TCIOFLUSH);
112 write(FD, "ATv0\r", 5); /* tell modem to use short status codes */
115 write(FD, "ATTD", 4); /* send dial command */
116 for (cp = num; *cp; cp++)
119 write(FD, num, strlen(num));
124 if ((dummy = gobble("01234")) != '1')
133 return (connected); /* lets get out of here.. */
135 tcflush(FD, TCIOFLUSH);
138 (void)snprintf(line, sizeof line, "%ld second dial timeout",
139 number(value(DIALTIMEOUT)));
140 logent(value(HOST), num, "hayes", line);
144 hay_disconnect(); /* insurance */
151 /* first hang up the modem*/
153 printf("\rdisconnecting modem....\n\r");
155 ioctl(FD, TIOCCDTR, 0);
157 ioctl(FD, TIOCSDTR, 0);
164 write(FD, "\r", 1); /* send anything to abort the call */
172 printf("\07timeout waiting for reply\n\r");
174 longjmp(timeoutbuf, 1);
185 f = signal(SIGALRM, sigALRM);
188 printf("\ngobble: waiting for %s\n", match);
191 if (setjmp(timeoutbuf)) {
195 alarm(number(value(DIALTIMEOUT)));
200 printf("%c 0x%x ", c, c);
202 for (i = 0; i < strlen(match); i++)
205 } while (status == 0);
206 signal(SIGALRM, SIG_DFL);
232 printf("NO CARRIER");
236 printf("ERROR in input");
240 printf("CONNECT 1200");
244 printf("Unknown Modem error: %c (0x%x)", c, c);
251 * set modem back to normal verbose status codes.
259 tcflush(FD, TCIOFLUSH);
263 tcflush(FD, TCIOFLUSH);
265 write(FD, "ATH0\r", 5); /* insurance */
268 if (c != '0' && c != '3') {
269 printf("cannot hang up modem\n\r");
270 printf("please use 'tip dialer' to make sure the line is hung up\n\r");
274 ioctl(FD, FIONREAD, &len);
276 printf("goodbye1: len=%d -- ", len);
277 rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
279 printf("read (%d): %s\r\n", rlen, dumbuf);
281 write(FD, "ATv1\r", 5);
284 ioctl(FD, FIONREAD, &len);
285 printf("goodbye2: len=%d -- ", len);
286 rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
288 printf("read (%d): %s\r\n", rlen, dumbuf);
291 tcflush(FD, TCIOFLUSH);
292 ioctl(FD, TIOCCDTR, 0); /* clear DTR (insurance) */
303 while (retry++ <= MAXRETRY) {
304 write(FD, "AT\r", 3);
306 ioctl(FD, FIONREAD, &len);
308 len = read(FD, dumbuf, min(len, DUMBUFLEN));
309 if (strchr(dumbuf, '0') ||
310 (strchr(dumbuf, 'O') && strchr(dumbuf, 'K')))
314 printf("hay_sync: (\"%s\") %d\n\r", dumbuf, retry);
317 ioctl(FD, TIOCCDTR, 0);
318 ioctl(FD, TIOCSDTR, 0);
320 printf("Cannot synchronize with hayes...\n\r");