Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / tip / libacu / multitech.c
1 /*
2  * Copyright (c) 1986, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
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.
20  *
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.
32  *
33  * @(#)multitech.c      8.1 (Berkeley) 6/6/93
34  */
35
36 /*
37  * Routines for calling up on a Courier modem.
38  * Derived from Hayes driver.
39  */
40 #include "tipconf.h"
41 #include "tip.h"
42 #include "acucommon.h"
43
44 #include <stdio.h>
45
46 /* #define DEBUG */
47 #define MAXRETRY        5
48 /*
49         Configuration
50 */
51 static CONST char *dial_command = "ATDT";
52 static CONST char *hangup_command = "ATH\r";
53 static CONST char *echo_off_command = "ATE0\r";
54 static CONST char *reset_command = "\rATZ\r";
55 static CONST char *init_string = "AT$BA0$SB38400&E1&E4&E13&E15Q0V1X4E0S0=0\r";
56 static CONST char *escape_sequence = "+++"; /* return to command escape sequence */
57 static CONST int lock_baud = 1;
58 static CONST unsigned int intercharacter_delay = 20;
59 static CONST unsigned int intercommand_delay = 250;
60 static CONST unsigned int escape_guard_time = 250;
61 static CONST unsigned int reset_delay = 2000;
62
63 /*
64         Forward declarations
65 */
66 void multitech_write (int fd, CONST char *cp, int n);
67 void multitech_write_str (int fd, CONST char *cp);
68 void multitech_disconnect ();
69 void acu_nap (unsigned int how_long);
70 static void sigALRM ();
71 static int multitechsync ();
72 static int multitech_swallow (register char *match);
73
74 /*
75         Global vars
76 */
77 static int timeout = 0;
78 static int connected = 0;
79 static jmp_buf timeoutbuf, intbuf;
80
81 int multitech_dialer (register char *num, char *acu)
82 {
83         register char *cp;
84 #if ACULOG
85         char line [80];
86 #endif
87         static int multitech_connect(), multitech_swallow();
88
89         if (lock_baud)
90         {
91                 int i;
92                 if ((i = speed(number(value(BAUDRATE)))) == 0)
93                         return 0;
94                 ttysetup (i);
95         }
96
97         if (boolean(value(VERBOSE)))
98                 printf("Using \"%s\"\n", acu);
99
100         acu_hupcl ();
101
102         /*
103          * Get in synch.
104          */
105         if (!multitechsync()) {
106 badsynch:
107                 printf("can't synchronize with multitech\n");
108 #if ACULOG
109                 logent(value(HOST), num, "multitech", "can't synch up");
110 #endif
111                 return (0);
112         }
113         acu_nap (intercommand_delay);
114
115         multitech_write_str (FD, echo_off_command);     /* turn off echoing */
116
117         sleep(1);
118
119 #ifdef DEBUG
120         if (boolean(value(VERBOSE)))
121                 multitech_verbose_read();
122 #endif
123
124         acu_flush ();
125
126         acu_nap (intercommand_delay);
127         multitech_write_str (FD, init_string);
128
129         if (!multitech_swallow ("\r\nOK\r\n"))
130                 goto badsynch;
131
132         fflush (stdout);
133
134         acu_nap (intercommand_delay);
135         multitech_write_str (FD, dial_command);
136
137         for (cp = num; *cp; cp++)
138                 if (*cp == '=')
139                         *cp = ',';
140
141         multitech_write_str (FD, num);
142
143         multitech_write_str (FD, "\r");
144
145         connected = multitech_connect();
146
147 #if ACULOG
148         if (timeout) {
149                 sprintf(line, "%d second dial timeout",
150                         number(value(DIALTIMEOUT)));
151                 logent(value(HOST), num, "multitech", line);
152         }
153 #endif
154         if (timeout)
155                 multitech_disconnect ();
156         return (connected);
157 }
158
159 void multitech_disconnect ()
160 {
161         int okay, retries;
162         for (retries = okay = 0; retries < 3 && !okay; retries++)
163         {
164                  /* first hang up the modem*/
165                 ioctl (FD, TIOCCDTR, 0);
166                 acu_nap (escape_guard_time);
167                 ioctl (FD, TIOCSDTR, 0);
168                 acu_nap (escape_guard_time);
169                 /*
170                  * If not strapped for DTR control, try to get command mode.
171                  */
172                 acu_nap (escape_guard_time);
173                 multitech_write_str (FD, escape_sequence);
174                 acu_nap (escape_guard_time);
175                 multitech_write_str (FD, hangup_command);
176                 okay = multitech_swallow ("\r\nOK\r\n");
177         }
178         if (!okay)
179         {
180                 #if ACULOG
181                 logent(value(HOST), "", "multitech", "can't hang up modem");
182                 #endif
183                 if (boolean(value(VERBOSE)))
184                         printf("hang up failed\n");
185         }
186         close (FD);
187 }
188
189 void multitech_abort ()
190 {
191         multitech_write_str (FD, "\r"); /* send anything to abort the call */
192         multitech_disconnect ();
193 }
194
195 static void sigALRM ()
196 {
197         (void) printf("\07timeout waiting for reply\n");
198         timeout = 1;
199         longjmp(timeoutbuf, 1);
200 }
201
202 static int multitech_swallow (register char *match)
203   {
204         sig_t f;
205         char c;
206
207         f = signal(SIGALRM, sigALRM);
208         timeout = 0;
209         do {
210                 if (*match =='\0') {
211                         signal(SIGALRM, f);
212                         return (1);
213                 }
214                 if (setjmp(timeoutbuf)) {
215                         signal(SIGALRM, f);
216                         return (0);
217                 }
218                 alarm(number(value(DIALTIMEOUT)));
219                 read(FD, &c, 1);
220                 alarm(0);
221                 c &= 0177;
222 #ifdef DEBUG
223                 if (boolean(value(VERBOSE)))
224                         putchar(c);
225 #endif
226         } while (c == *match++);
227 #ifdef DEBUG
228         if (boolean(value(VERBOSE)))
229                 fflush (stdout);
230 #endif
231         signal(SIGALRM, SIG_DFL);
232         return (0);
233 }
234
235 static struct baud_msg {
236         char *msg;
237         int baud;
238 } baud_msg[] = {
239         "",             B300,
240         " 1200",        B1200,
241         " 2400",        B2400,
242         " 9600",        B9600,
243         " 9600/ARQ",    B9600,
244         0,              0,
245 };
246
247 static int multitech_connect ()
248 {
249         char c;
250         int nc, nl, n;
251         char dialer_buf[64];
252         struct baud_msg *bm;
253         sig_t f;
254
255         if (multitech_swallow("\r\n") == 0)
256                 return (0);
257         f = signal(SIGALRM, sigALRM);
258 again:
259         nc = 0; nl = sizeof(dialer_buf)-1;
260         bzero(dialer_buf, sizeof(dialer_buf));
261         timeout = 0;
262         for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) {
263                 if (setjmp(timeoutbuf))
264                         break;
265                 alarm(number(value(DIALTIMEOUT)));
266                 n = read(FD, &c, 1);
267                 alarm(0);
268                 if (n <= 0)
269                         break;
270                 c &= 0x7f;
271                 if (c == '\r') {
272                         if (multitech_swallow("\n") == 0)
273                                 break;
274                         if (!dialer_buf[0])
275                                 goto again;
276                         if (strcmp(dialer_buf, "RINGING") == 0 &&
277                             boolean(value(VERBOSE))) {
278 #ifdef DEBUG
279                                 printf("%s\r\n", dialer_buf);
280 #endif
281                                 goto again;
282                         }
283                         if (strncmp(dialer_buf, "CONNECT",
284                                     sizeof("CONNECT")-1) != 0)
285                                 break;
286                         if (lock_baud) {
287                                 signal(SIGALRM, f);
288 #ifdef DEBUG
289                                 if (boolean(value(VERBOSE)))
290                                         printf("%s\r\n", dialer_buf);
291 #endif
292                                 return (1);
293                         }
294                         for (bm = baud_msg ; bm->msg ; bm++)
295                                 if (strcmp(bm->msg, dialer_buf+sizeof("CONNECT")-1) == 0) {
296                                         if (!acu_setspeed (bm->baud))
297                                                 goto error;
298                                         signal(SIGALRM, f);
299 #ifdef DEBUG
300                                         if (boolean(value(VERBOSE)))
301                                                 printf("%s\r\n", dialer_buf);
302 #endif
303                                         return (1);
304                                 }
305                         break;
306                 }
307                 dialer_buf[nc] = c;
308         }
309 error1:
310         printf("%s\r\n", dialer_buf);
311 error:
312         signal(SIGALRM, f);
313         return (0);
314 }
315
316 /*
317  * This convoluted piece of code attempts to get
318  * the multitech in sync.
319  */
320 static int multitechsync ()
321 {
322         int already = 0;
323         int len;
324         char buf[40];
325
326         while (already++ < MAXRETRY) {
327                 acu_nap (intercommand_delay);
328                 ioctl (FD, TIOCFLUSH, 0);       /* flush any clutter */
329                 multitech_write_str (FD, reset_command); /* reset modem */
330                 bzero(buf, sizeof(buf));
331                 acu_nap (reset_delay);
332                 ioctl (FD, FIONREAD, &len);
333                 if (len) {
334                         len = read(FD, buf, sizeof(buf));
335 #ifdef DEBUG
336                         buf [len] = '\0';
337                         printf("multitechsync: (\"%s\")\n\r", buf);
338 #endif
339                         if (index(buf, '0') ||
340                            (index(buf, 'O') && index(buf, 'K')))
341                                 return(1);
342                 }
343                 /*
344                  * If not strapped for DTR control,
345                  * try to get command mode.
346                  */
347                 acu_nap (escape_guard_time);
348                 multitech_write_str (FD, escape_sequence);
349                 acu_nap (escape_guard_time);
350                 multitech_write_str (FD, hangup_command);
351                 /*
352                  * Toggle DTR to force anyone off that might have left
353                  * the modem connected.
354                  */
355                 acu_nap (escape_guard_time);
356                 ioctl (FD, TIOCCDTR, 0);
357                 acu_nap (escape_guard_time);
358                 ioctl (FD, TIOCSDTR, 0);
359         }
360         acu_nap (intercommand_delay);
361         multitech_write_str (FD, reset_command);
362         return (0);
363 }
364
365 void multitech_write_str (int fd, const char *cp)
366 {
367 #ifdef DEBUG
368         printf ("multitech: sending %s\n", cp);
369 #endif
370         multitech_write (fd, cp, strlen (cp));
371 }
372
373 void multitech_write (int fd, const char *cp, int n)
374 {
375         acu_flush ();
376         acu_nap (intercharacter_delay);
377         for ( ; n-- ; cp++) {
378                 write (fd, cp, 1);
379                 acu_flush ();
380                 acu_nap (intercharacter_delay);
381         }
382 }
383
384 #ifdef DEBUG
385 multitech_verbose_read()
386 {
387         int n = 0;
388         char buf[BUFSIZ];
389
390         if (ioctl(FD, FIONREAD, &n) < 0)
391                 return;
392         if (n <= 0)
393                 return;
394         if (read(FD, buf, n) != n)
395                 return;
396         write(1, buf, n);
397 }
398 #endif
399
400 /* end of multitech.c */