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