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