hayes.c revision 1.11 1 /* $NetBSD: hayes.c,v 1.11 2006/04/03 00:51:14 perry 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.11 2006/04/03 00:51:14 perry 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 #define DIALING 1
73 #define IDLE 2
74 #define CONNECTED 3
75 #define FAILED 4
76 static int state = IDLE;
77
78 static void error_rep(char);
79 static char gobble(const char *);
80 static void goodbye(void);
81 static int hay_sync(void);
82 static void sigALRM(int);
83
84 int
85 hay_dialer(char *num, char *acu)
86 {
87 char *cp;
88 int connected = 0;
89 char dummy;
90 struct termios cntrl;
91 #ifdef ACULOG
92 char line[80];
93 #endif
94 if (hay_sync() == 0) /* make sure we can talk to the modem */
95 return(0);
96 if (boolean(value(VERBOSE)))
97 printf("\ndialing...");
98 fflush(stdout);
99 tcgetattr(FD, &cntrl);
100 cntrl.c_cflag |= HUPCL;
101 tcsetattr(FD, TCSANOW, &cntrl);
102 tcflush(FD, TCIOFLUSH);
103 write(FD, "ATv0\r", 5); /* tell modem to use short status codes */
104 gobble("\r");
105 gobble("\r");
106 write(FD, "ATTD", 4); /* send dial command */
107 for (cp = num; *cp; cp++)
108 if (*cp == '=')
109 *cp = ',';
110 write(FD, num, strlen(num));
111 state = DIALING;
112 write(FD, "\r", 1);
113 connected = 0;
114 if (gobble("\r")) {
115 if ((dummy = gobble("01234")) != '1')
116 error_rep(dummy);
117 else
118 connected = 1;
119 }
120 if (connected)
121 state = CONNECTED;
122 else {
123 state = FAILED;
124 return (connected); /* lets get out of here.. */
125 }
126 tcflush(FD, TCIOFLUSH);
127 #ifdef ACULOG
128 if (timeout) {
129 (void)snprintf(line, sizeof line, "%d second dial timeout",
130 (int)number(value(DIALTIMEOUT)));
131 logent(value(HOST), num, "hayes", line);
132 }
133 #endif
134 if (timeout)
135 hay_disconnect(); /* insurance */
136 return (connected);
137 }
138
139
140 void
141 hay_disconnect(void)
142 {
143
144 /* first hang up the modem*/
145 #ifdef DEBUG
146 printf("\rdisconnecting modem....\n\r");
147 #endif
148 ioctl(FD, TIOCCDTR, 0);
149 sleep(1);
150 ioctl(FD, TIOCSDTR, 0);
151 goodbye();
152 }
153
154 void
155 hay_abort(void)
156 {
157
158 write(FD, "\r", 1); /* send anything to abort the call */
159 hay_disconnect();
160 }
161
162 static void
163 sigALRM(int dummy)
164 {
165
166 printf("\07timeout waiting for reply\n\r");
167 timeout = 1;
168 longjmp(timeoutbuf, 1);
169 }
170
171 static char
172 gobble(const char *match)
173 {
174 char c;
175 sig_t f;
176 int i, status = 0;
177
178 #if __GNUC__ /* XXX pacify gcc */
179 (void)&status;
180 #endif
181
182 f = signal(SIGALRM, sigALRM);
183 timeout = 0;
184 #ifdef DEBUG
185 printf("\ngobble: waiting for %s\n", match);
186 #endif
187 do {
188 if (setjmp(timeoutbuf)) {
189 signal(SIGALRM, f);
190 return (0);
191 }
192 alarm(number(value(DIALTIMEOUT)));
193 read(FD, &c, 1);
194 alarm(0);
195 c &= 0177;
196 #ifdef DEBUG
197 printf("%c 0x%x ", c, c);
198 #endif
199 for (i = 0; i < strlen(match); i++)
200 if (c == match[i])
201 status = c;
202 } while (status == 0);
203 signal(SIGALRM, SIG_DFL);
204 #ifdef DEBUG
205 printf("\n");
206 #endif
207 return (status);
208 }
209
210 static void
211 error_rep(char c)
212 {
213
214 printf("\n\r");
215 switch (c) {
216
217 case '0':
218 printf("OK");
219 break;
220
221 case '1':
222 printf("CONNECT");
223 break;
224
225 case '2':
226 printf("RING");
227 break;
228
229 case '3':
230 printf("NO CARRIER");
231 break;
232
233 case '4':
234 printf("ERROR in input");
235 break;
236
237 case '5':
238 printf("CONNECT 1200");
239 break;
240
241 default:
242 printf("Unknown Modem error: %c (0x%x)", c, c);
243 }
244 printf("\n\r");
245 return;
246 }
247
248 /*
249 * set modem back to normal verbose status codes.
250 */
251 void
252 goodbye(void)
253 {
254 int len;
255 char c;
256
257 tcflush(FD, TCIOFLUSH);
258 if (hay_sync()) {
259 sleep(1);
260 #ifndef DEBUG
261 tcflush(FD, TCIOFLUSH);
262 #endif
263 write(FD, "ATH0\r", 5); /* insurance */
264 #ifndef DEBUG
265 c = gobble("03");
266 if (c != '0' && c != '3') {
267 printf("cannot hang up modem\n\r");
268 printf("please use 'tip dialer' to make sure the line is hung up\n\r");
269 }
270 #endif
271 sleep(1);
272 ioctl(FD, FIONREAD, &len);
273 #ifdef DEBUG
274 printf("goodbye1: len=%d -- ", len);
275 rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
276 dumbuf[rlen] = '\0';
277 printf("read (%d): %s\r\n", rlen, dumbuf);
278 #endif
279 write(FD, "ATv1\r", 5);
280 sleep(1);
281 #ifdef DEBUG
282 ioctl(FD, FIONREAD, &len);
283 printf("goodbye2: len=%d -- ", len);
284 rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
285 dumbuf[rlen] = '\0';
286 printf("read (%d): %s\r\n", rlen, dumbuf);
287 #endif
288 }
289 tcflush(FD, TCIOFLUSH);
290 ioctl(FD, TIOCCDTR, 0); /* clear DTR (insurance) */
291 close(FD);
292 }
293
294 #define MAXRETRY 5
295
296 int
297 hay_sync(void)
298 {
299 int len, retry = 0;
300
301 while (retry++ <= MAXRETRY) {
302 write(FD, "AT\r", 3);
303 sleep(1);
304 ioctl(FD, FIONREAD, &len);
305 if (len) {
306 len = read(FD, dumbuf, min(len, DUMBUFLEN));
307 if (strchr(dumbuf, '0') ||
308 (strchr(dumbuf, 'O') && strchr(dumbuf, 'K')))
309 return(1);
310 #ifdef DEBUG
311 dumbuf[len] = '\0';
312 printf("hay_sync: (\"%s\") %d\n\r", dumbuf, retry);
313 #endif
314 }
315 ioctl(FD, TIOCCDTR, 0);
316 ioctl(FD, TIOCSDTR, 0);
317 }
318 printf("Cannot synchronize with hayes...\n\r");
319 return(0);
320 }
321