2 * Copyright (c) 1986, 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
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
33 * @(#)multitech.c 8.1 (Berkeley) 6/6/93
37 * Routines for calling up on a Courier modem.
38 * Derived from Hayes driver.
42 #include "acucommon.h"
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;
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);
77 static int timeout = 0;
78 static int connected = 0;
79 static jmp_buf timeoutbuf, intbuf;
81 int multitech_dialer (register char *num, char *acu)
87 static int multitech_connect(), multitech_swallow();
92 if ((i = speed(number(value(BAUDRATE)))) == 0)
97 if (boolean(value(VERBOSE)))
98 printf("Using \"%s\"\n", acu);
105 if (!multitechsync()) {
107 printf("can't synchronize with multitech\n");
109 logent(value(HOST), num, "multitech", "can't synch up");
113 acu_nap (intercommand_delay);
115 multitech_write_str (FD, echo_off_command); /* turn off echoing */
120 if (boolean(value(VERBOSE)))
121 multitech_verbose_read();
126 acu_nap (intercommand_delay);
127 multitech_write_str (FD, init_string);
129 if (!multitech_swallow ("\r\nOK\r\n"))
134 acu_nap (intercommand_delay);
135 multitech_write_str (FD, dial_command);
137 for (cp = num; *cp; cp++)
141 multitech_write_str (FD, num);
143 multitech_write_str (FD, "\r");
145 connected = multitech_connect();
149 sprintf(line, "%d second dial timeout",
150 number(value(DIALTIMEOUT)));
151 logent(value(HOST), num, "multitech", line);
155 multitech_disconnect ();
159 void multitech_disconnect ()
162 for (retries = okay = 0; retries < 3 && !okay; retries++)
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);
170 * If not strapped for DTR control, try to get command mode.
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");
181 logent(value(HOST), "", "multitech", "can't hang up modem");
183 if (boolean(value(VERBOSE)))
184 printf("hang up failed\n");
189 void multitech_abort ()
191 multitech_write_str (FD, "\r"); /* send anything to abort the call */
192 multitech_disconnect ();
195 static void sigALRM ()
197 (void) printf("\07timeout waiting for reply\n");
199 longjmp(timeoutbuf, 1);
202 static int multitech_swallow (register char *match)
207 f = signal(SIGALRM, sigALRM);
214 if (setjmp(timeoutbuf)) {
218 alarm(number(value(DIALTIMEOUT)));
223 if (boolean(value(VERBOSE)))
226 } while (c == *match++);
228 if (boolean(value(VERBOSE)))
231 signal(SIGALRM, SIG_DFL);
235 static struct baud_msg {
247 static int multitech_connect ()
255 if (multitech_swallow("\r\n") == 0)
257 f = signal(SIGALRM, sigALRM);
259 nc = 0; nl = sizeof(dialer_buf)-1;
260 bzero(dialer_buf, sizeof(dialer_buf));
262 for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) {
263 if (setjmp(timeoutbuf))
265 alarm(number(value(DIALTIMEOUT)));
272 if (multitech_swallow("\n") == 0)
276 if (strcmp(dialer_buf, "RINGING") == 0 &&
277 boolean(value(VERBOSE))) {
279 printf("%s\r\n", dialer_buf);
283 if (strncmp(dialer_buf, "CONNECT",
284 sizeof("CONNECT")-1) != 0)
289 if (boolean(value(VERBOSE)))
290 printf("%s\r\n", dialer_buf);
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))
300 if (boolean(value(VERBOSE)))
301 printf("%s\r\n", dialer_buf);
310 printf("%s\r\n", dialer_buf);
317 * This convoluted piece of code attempts to get
318 * the multitech in sync.
320 static int multitechsync ()
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);
334 len = read(FD, buf, sizeof(buf));
337 printf("multitechsync: (\"%s\")\n\r", buf);
339 if (index(buf, '0') ||
340 (index(buf, 'O') && index(buf, 'K')))
344 * If not strapped for DTR control,
345 * try to get command mode.
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);
352 * Toggle DTR to force anyone off that might have left
353 * the modem connected.
355 acu_nap (escape_guard_time);
356 ioctl (FD, TIOCCDTR, 0);
357 acu_nap (escape_guard_time);
358 ioctl (FD, TIOCSDTR, 0);
360 acu_nap (intercommand_delay);
361 multitech_write_str (FD, reset_command);
365 void multitech_write_str (int fd, const char *cp)
368 printf ("multitech: sending %s\n", cp);
370 multitech_write (fd, cp, strlen (cp));
373 void multitech_write (int fd, const char *cp, int n)
376 acu_nap (intercharacter_delay);
377 for ( ; n-- ; cp++) {
380 acu_nap (intercharacter_delay);
385 multitech_verbose_read()
390 if (ioctl(FD, FIONREAD, &n) < 0)
394 if (read(FD, buf, n) != n)
400 /* end of multitech.c */