MFC: Fix an issue w/libthread_xu's priority ranges.
[dragonfly.git] / usr.bin / tip / libacu / hayes.c
blobf5afc5865bfbf578d38215809bb3a237d0e1b3de
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)hayes.c 8.1 (Berkeley) 6/6/93
37 * Routines for calling up on a Hayes Modem
38 * (based on the old VenTel driver).
39 * The modem is expected to be strapped for "echo".
40 * Also, the switches enabling the DTR and CD lines
41 * must be set correctly.
42 * NOTICE:
43 * The easy way to hang up a modem is always simply to
44 * clear the DTR signal. However, if the +++ sequence
45 * (which switches the modem back to local mode) is sent
46 * before modem is hung up, removal of the DTR signal
47 * has no effect (except that it prevents the modem from
48 * recognizing commands).
49 * (by Helge Skrivervik, Calma Company, Sunnyvale, CA. 1984)
52 * TODO:
53 * It is probably not a good idea to switch the modem
54 * state between 'verbose' and terse (status messages).
55 * This should be kicked out and we should use verbose
56 * mode only. This would make it consistent with normal
57 * interactive use thru the command 'tip dialer'.
59 #include "tipconf.h"
60 #include "tip.h"
62 #define min(a,b) ((a < b) ? a : b)
64 static void sigALRM();
65 static int timeout = 0;
66 static jmp_buf timeoutbuf;
67 static char gobble();
68 #define DUMBUFLEN 40
69 static char dumbuf[DUMBUFLEN];
71 #define DIALING 1
72 #define IDLE 2
73 #define CONNECTED 3
74 #define FAILED 4
75 static int state = IDLE;
77 hay_dialer(num, acu)
78 register char *num;
79 char *acu;
81 register char *cp;
82 register int connected = 0;
83 char dummy;
84 #if ACULOG
85 char line[80];
86 #endif
87 if (hay_sync() == 0) /* make sure we can talk to the modem */
88 return(0);
89 if (boolean(value(VERBOSE)))
90 printf("\ndialing...");
91 fflush(stdout);
92 acu_hupcl ();
93 acu_flush ();
94 write(FD, "ATv0\r", 5); /* tell modem to use short status codes */
95 gobble("\r");
96 gobble("\r");
97 write(FD, "ATTD", 4); /* send dial command */
98 write(FD, num, strlen(num));
99 state = DIALING;
100 write(FD, "\r", 1);
101 connected = 0;
102 if (gobble("\r")) {
103 if ((dummy = gobble("01234")) != '1')
104 error_rep(dummy);
105 else
106 connected = 1;
108 if (connected)
109 state = CONNECTED;
110 else {
111 state = FAILED;
112 return (connected); /* lets get out of here.. */
114 ioctl(FD, TIOCFLUSH, 0);
115 #if ACULOG
116 if (timeout) {
117 sprintf(line, "%d second dial timeout",
118 number(value(DIALTIMEOUT)));
119 logent(value(HOST), num, "hayes", line);
121 #endif
122 if (timeout)
123 hay_disconnect(); /* insurance */
124 return (connected);
128 hay_disconnect()
130 char c;
131 int len, rlen;
133 /* first hang up the modem*/
134 #ifdef DEBUG
135 printf("\rdisconnecting modem....\n\r");
136 #endif
137 ioctl(FD, TIOCCDTR, 0);
138 sleep(1);
139 ioctl(FD, TIOCSDTR, 0);
140 goodbye();
143 hay_abort()
146 char c;
148 write(FD, "\r", 1); /* send anything to abort the call */
149 hay_disconnect();
152 static void
153 sigALRM()
156 printf("\07timeout waiting for reply\n\r");
157 timeout = 1;
158 longjmp(timeoutbuf, 1);
161 static char
162 gobble(match)
163 register char *match;
165 char c;
166 sig_t f;
167 int i, status = 0;
169 f = signal(SIGALRM, sigALRM);
170 timeout = 0;
171 #ifdef DEBUG
172 printf("\ngobble: waiting for %s\n", match);
173 #endif
174 do {
175 if (setjmp(timeoutbuf)) {
176 signal(SIGALRM, f);
177 return (0);
179 alarm(number(value(DIALTIMEOUT)));
180 read(FD, &c, 1);
181 alarm(0);
182 c &= 0177;
183 #ifdef DEBUG
184 printf("%c 0x%x ", c, c);
185 #endif
186 for (i = 0; i < strlen(match); i++)
187 if (c == match[i])
188 status = c;
189 } while (status == 0);
190 signal(SIGALRM, SIG_DFL);
191 #ifdef DEBUG
192 printf("\n");
193 #endif
194 return (status);
197 error_rep(c)
198 register char c;
200 printf("\n\r");
201 switch (c) {
203 case '0':
204 printf("OK");
205 break;
207 case '1':
208 printf("CONNECT");
209 break;
211 case '2':
212 printf("RING");
213 break;
215 case '3':
216 printf("NO CARRIER");
217 break;
219 case '4':
220 printf("ERROR in input");
221 break;
223 case '5':
224 printf("CONNECT 1200");
225 break;
227 default:
228 printf("Unknown Modem error: %c (0x%x)", c, c);
230 printf("\n\r");
231 return;
235 * set modem back to normal verbose status codes.
237 goodbye()
239 int len, rlen;
240 char c;
242 ioctl(FD, TIOCFLUSH, &len); /* get rid of trash */
243 if (hay_sync()) {
244 sleep(1);
245 #ifndef DEBUG
246 ioctl(FD, TIOCFLUSH, 0);
247 #endif
248 write(FD, "ATH0\r", 5); /* insurance */
249 #ifndef DEBUG
250 c = gobble("03");
251 if (c != '0' && c != '3') {
252 printf("cannot hang up modem\n\r");
253 printf("please use 'tip dialer' to make sure the line is hung up\n\r");
255 #endif
256 sleep(1);
257 ioctl(FD, FIONREAD, &len);
258 #ifdef DEBUG
259 printf("goodbye1: len=%d -- ", len);
260 rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
261 dumbuf[rlen] = '\0';
262 printf("read (%d): %s\r\n", rlen, dumbuf);
263 #endif
264 write(FD, "ATv1\r", 5);
265 sleep(1);
266 #ifdef DEBUG
267 ioctl(FD, FIONREAD, &len);
268 printf("goodbye2: len=%d -- ", len);
269 rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
270 dumbuf[rlen] = '\0';
271 printf("read (%d): %s\r\n", rlen, dumbuf);
272 #endif
274 ioctl(FD, TIOCFLUSH, 0); /* clear the input buffer */
275 ioctl(FD, TIOCCDTR, 0); /* clear DTR (insurance) */
276 close(FD);
279 #define MAXRETRY 5
281 hay_sync()
283 int len, retry = 0;
285 while (retry++ <= MAXRETRY) {
286 write(FD, "AT\r", 3);
287 sleep(1);
288 ioctl(FD, FIONREAD, &len);
289 if (len) {
290 len = read(FD, dumbuf, min(len, DUMBUFLEN));
291 if (index(dumbuf, '0') ||
292 (index(dumbuf, 'O') && index(dumbuf, 'K')))
293 return(1);
294 #ifdef DEBUG
295 dumbuf[len] = '\0';
296 printf("hay_sync: (\"%s\") %d\n\r", dumbuf, retry);
297 #endif
299 ioctl(FD, TIOCCDTR, 0);
300 ioctl(FD, TIOCSDTR, 0);
302 printf("Cannot synchronize with hayes...\n\r");
303 return(0);