1 /* $NetBSD: hayes.c,v 1.15 2006/12/14 17:09:43 christos Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)hayes.c 8.1 (Berkeley) 6/6/93";
37 __RCSID("$NetBSD: hayes.c,v 1.15 2006/12/14 17:09:43 christos Exp $");
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.
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)
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'.
65 #define min(a,b) ((a < b) ? a : b)
67 static int timeout
= 0;
68 static jmp_buf timeoutbuf
;
70 static char dumbuf
[DUMBUFLEN
];
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);
80 hay_dialer(char *num
, char *acu __unused
)
87 if (hay_sync() == 0) /* make sure we can talk to the modem */
89 if (boolean(value(VERBOSE
)))
90 (void)printf("\ndialing...");
92 (void)tcgetattr(FD
, &cntrl
);
93 cntrl
.c_cflag
|= HUPCL
;
94 (void)tcsetattr(FD
, TCSANOW
, &cntrl
);
95 (void)tcflush(FD
, TCIOFLUSH
);
96 (void)write(FD
, "ATv0\r", 5); /* tell modem to use short status codes */
99 (void)write(FD
, "ATTD", 4); /* send dial command */
100 for (cp
= num
; *cp
; cp
++)
103 (void)write(FD
, num
, strlen(num
));
104 (void)write(FD
, "\r", 1);
107 if ((dummy
= gobble("01234")) != '1')
113 return (connected
); /* lets get out of here.. */
114 (void)tcflush(FD
, TCIOFLUSH
);
116 hay_disconnect(); /* insurance */
125 /* first hang up the modem*/
127 (void)printf("\rdisconnecting modem....\n\r");
129 (void)ioctl(FD
, TIOCCDTR
, 0);
131 (void)ioctl(FD
, TIOCSDTR
, 0);
139 (void)write(FD
, "\r", 1); /* send anything to abort the call */
145 sigALRM(int dummy __unused
)
148 (void)printf("\07timeout waiting for reply\n\r");
150 longjmp(timeoutbuf
, 1);
154 gobble(const char *match
)
159 volatile int status
= 0;
161 f
= signal(SIGALRM
, sigALRM
);
164 (void)printf("\ngobble: waiting for %s\n", match
);
167 if (setjmp(timeoutbuf
)) {
168 (void)signal(SIGALRM
, f
);
171 (void)alarm((unsigned int)number(value(DIALTIMEOUT
)));
172 (void)read(FD
, &c
, 1);
176 (void)printf("%c 0x%x ", c
, c
);
178 for (i
= 0; i
< strlen(match
); i
++)
181 } while (status
== 0);
182 (void)signal(SIGALRM
, SIG_DFL
);
193 (void)printf("\n\r");
201 (void)printf("CONNECT");
205 (void)printf("RING");
209 (void)printf("NO CARRIER");
213 (void)printf("ERROR in input");
217 (void)printf("CONNECT 1200");
221 (void)printf("Unknown Modem error: %c (0x%x)", c
, c
);
223 (void)printf("\n\r");
228 * set modem back to normal verbose status codes.
236 (void)tcflush(FD
, TCIOFLUSH
);
240 (void)tcflush(FD
, TCIOFLUSH
);
242 (void)write(FD
, "ATH0\r", 5); /* insurance */
245 if (c
!= '0' && c
!= '3') {
246 (void)printf("cannot hang up modem\n\r");
247 (void)printf("please use 'tip dialer' to make sure the line is hung up\n\r");
251 (void)ioctl(FD
, FIONREAD
, &len
);
253 (void)printf("goodbye1: len=%d -- ", len
);
254 rlen
= read(FD
, dumbuf
, min(len
, DUMBUFLEN
));
256 (void)printf("read (%d): %s\r\n", rlen
, dumbuf
);
258 (void)write(FD
, "ATv1\r", 5);
261 (void)ioctl(FD
, FIONREAD
, &len
);
262 (void)printf("goodbye2: len=%d -- ", len
);
263 rlen
= read(FD
, dumbuf
, min(len
, DUMBUFLEN
));
265 (void)printf("read (%d): %s\r\n", rlen
, dumbuf
);
268 (void)tcflush(FD
, TCIOFLUSH
);
269 (void)ioctl(FD
, TIOCCDTR
, 0); /* clear DTR (insurance) */
280 while (retry
++ <= MAXRETRY
) {
281 (void)write(FD
, "AT\r", 3);
283 (void)ioctl(FD
, FIONREAD
, &len
);
285 len
= read(FD
, dumbuf
, (size_t)min(len
, DUMBUFLEN
));
286 if (strchr(dumbuf
, '0') ||
287 (strchr(dumbuf
, 'O') && strchr(dumbuf
, 'K')))
291 (void)printf("hay_sync: (\"%s\") %d\n\r", dumbuf
, retry
);
294 (void)ioctl(FD
, TIOCCDTR
, 0);
295 (void)ioctl(FD
, TIOCSDTR
, 0);
297 (void)printf("Cannot synchronize with hayes...\n\r");