No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / tip / aculib / hayes.c
blobda0851ffb44224b841ae58ba4172fe1efd8b8ab9
1 /* $NetBSD: hayes.c,v 1.15 2006/12/14 17:09:43 christos Exp $ */
3 /*
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
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.
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.
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.15 2006/12/14 17:09:43 christos Exp $");
38 #endif /* not lint */
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)
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'.
63 #include "tip.h"
65 #define min(a,b) ((a < b) ? a : b)
67 static int timeout = 0;
68 static jmp_buf timeoutbuf;
69 #define DUMBUFLEN 40
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);
78 int
79 /*ARGSUSED*/
80 hay_dialer(char *num, char *acu __unused)
82 char *cp;
83 int connected = 0;
84 char dummy;
85 struct termios cntrl;
87 if (hay_sync() == 0) /* make sure we can talk to the modem */
88 return(0);
89 if (boolean(value(VERBOSE)))
90 (void)printf("\ndialing...");
91 (void)fflush(stdout);
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 */
97 (void)gobble("\r");
98 (void)gobble("\r");
99 (void)write(FD, "ATTD", 4); /* send dial command */
100 for (cp = num; *cp; cp++)
101 if (*cp == '=')
102 *cp = ',';
103 (void)write(FD, num, strlen(num));
104 (void)write(FD, "\r", 1);
105 connected = 0;
106 if (gobble("\r")) {
107 if ((dummy = gobble("01234")) != '1')
108 error_rep(dummy);
109 else
110 connected = 1;
112 if (!connected)
113 return (connected); /* lets get out of here.. */
114 (void)tcflush(FD, TCIOFLUSH);
115 if (timeout)
116 hay_disconnect(); /* insurance */
117 return (connected);
121 void
122 hay_disconnect(void)
125 /* first hang up the modem*/
126 #ifdef DEBUG
127 (void)printf("\rdisconnecting modem....\n\r");
128 #endif
129 (void)ioctl(FD, TIOCCDTR, 0);
130 (void)sleep(1);
131 (void)ioctl(FD, TIOCSDTR, 0);
132 goodbye();
135 void
136 hay_abort(void)
139 (void)write(FD, "\r", 1); /* send anything to abort the call */
140 hay_disconnect();
143 static void
144 /*ARGSUSED*/
145 sigALRM(int dummy __unused)
148 (void)printf("\07timeout waiting for reply\n\r");
149 timeout = 1;
150 longjmp(timeoutbuf, 1);
153 static char
154 gobble(const char *match)
156 char c;
157 sig_t f;
158 size_t i;
159 volatile int status = 0;
161 f = signal(SIGALRM, sigALRM);
162 timeout = 0;
163 #ifdef DEBUG
164 (void)printf("\ngobble: waiting for %s\n", match);
165 #endif
166 do {
167 if (setjmp(timeoutbuf)) {
168 (void)signal(SIGALRM, f);
169 return (0);
171 (void)alarm((unsigned int)number(value(DIALTIMEOUT)));
172 (void)read(FD, &c, 1);
173 (void)alarm(0);
174 c &= 0177;
175 #ifdef DEBUG
176 (void)printf("%c 0x%x ", c, c);
177 #endif
178 for (i = 0; i < strlen(match); i++)
179 if (c == match[i])
180 status = c;
181 } while (status == 0);
182 (void)signal(SIGALRM, SIG_DFL);
183 #ifdef DEBUG
184 (void)printf("\n");
185 #endif
186 return (status);
189 static void
190 error_rep(char c)
193 (void)printf("\n\r");
194 switch (c) {
196 case '0':
197 (void)printf("OK");
198 break;
200 case '1':
201 (void)printf("CONNECT");
202 break;
204 case '2':
205 (void)printf("RING");
206 break;
208 case '3':
209 (void)printf("NO CARRIER");
210 break;
212 case '4':
213 (void)printf("ERROR in input");
214 break;
216 case '5':
217 (void)printf("CONNECT 1200");
218 break;
220 default:
221 (void)printf("Unknown Modem error: %c (0x%x)", c, c);
223 (void)printf("\n\r");
224 return;
228 * set modem back to normal verbose status codes.
230 void
231 goodbye(void)
233 int len;
234 char c;
236 (void)tcflush(FD, TCIOFLUSH);
237 if (hay_sync()) {
238 (void)sleep(1);
239 #ifndef DEBUG
240 (void)tcflush(FD, TCIOFLUSH);
241 #endif
242 (void)write(FD, "ATH0\r", 5); /* insurance */
243 #ifndef DEBUG
244 c = gobble("03");
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");
249 #endif
250 (void)sleep(1);
251 (void)ioctl(FD, FIONREAD, &len);
252 #ifdef DEBUG
253 (void)printf("goodbye1: len=%d -- ", len);
254 rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
255 dumbuf[rlen] = '\0';
256 (void)printf("read (%d): %s\r\n", rlen, dumbuf);
257 #endif
258 (void)write(FD, "ATv1\r", 5);
259 (void)sleep(1);
260 #ifdef DEBUG
261 (void)ioctl(FD, FIONREAD, &len);
262 (void)printf("goodbye2: len=%d -- ", len);
263 rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
264 dumbuf[rlen] = '\0';
265 (void)printf("read (%d): %s\r\n", rlen, dumbuf);
266 #endif
268 (void)tcflush(FD, TCIOFLUSH);
269 (void)ioctl(FD, TIOCCDTR, 0); /* clear DTR (insurance) */
270 (void)close(FD);
273 #define MAXRETRY 5
276 hay_sync(void)
278 int len, retry = 0;
280 while (retry++ <= MAXRETRY) {
281 (void)write(FD, "AT\r", 3);
282 (void)sleep(1);
283 (void)ioctl(FD, FIONREAD, &len);
284 if (len) {
285 len = read(FD, dumbuf, (size_t)min(len, DUMBUFLEN));
286 if (strchr(dumbuf, '0') ||
287 (strchr(dumbuf, 'O') && strchr(dumbuf, 'K')))
288 return(1);
289 #ifdef DEBUG
290 dumbuf[len] = '\0';
291 (void)printf("hay_sync: (\"%s\") %d\n\r", dumbuf, retry);
292 #endif
294 (void)ioctl(FD, TIOCCDTR, 0);
295 (void)ioctl(FD, TIOCSDTR, 0);
297 (void)printf("Cannot synchronize with hayes...\n\r");
298 return(0);