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
35 static char sccsid[] = "@(#)multitech.c 8.1 (Berkeley) 6/6/93";
39 * Routines for calling up on a Courier modem.
40 * Derived from Hayes driver.
44 #include "acucommon.h"
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;
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);
79 static int timeout = 0;
80 static int connected = 0;
81 static jmp_buf timeoutbuf, intbuf;
83 int multitech_dialer (register char *num, char *acu)
89 static int multitech_connect(), multitech_swallow();
94 if ((i = speed(number(value(BAUDRATE)))) == 0)
99 if (boolean(value(VERBOSE)))
100 printf("Using \"%s\"\n", acu);
107 if (!multitechsync()) {
109 printf("can't synchronize with multitech\n");
111 logent(value(HOST), num, "multitech", "can't synch up");
115 acu_nap (intercommand_delay);
117 multitech_write_str (FD, echo_off_command); /* turn off echoing */
122 if (boolean(value(VERBOSE)))
123 multitech_verbose_read();
128 acu_nap (intercommand_delay);
129 multitech_write_str (FD, init_string);
131 if (!multitech_swallow ("\r\nOK\r\n"))
136 acu_nap (intercommand_delay);
137 multitech_write_str (FD, dial_command);
139 for (cp = num; *cp; cp++)
143 multitech_write_str (FD, num);
145 multitech_write_str (FD, "\r");
147 connected = multitech_connect();
151 sprintf(line, "%d second dial timeout",
152 number(value(DIALTIMEOUT)));
153 logent(value(HOST), num, "multitech", line);
157 multitech_disconnect ();
161 void multitech_disconnect ()
164 for (retries = okay = 0; retries < 3 && !okay; retries++)
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);
172 * If not strapped for DTR control, try to get command mode.
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");
183 logent(value(HOST), "", "multitech", "can't hang up modem");
185 if (boolean(value(VERBOSE)))
186 printf("hang up failed\n");
191 void multitech_abort ()
193 multitech_write_str (FD, "\r"); /* send anything to abort the call */
194 multitech_disconnect ();
197 static void sigALRM ()
199 (void) printf("\07timeout waiting for reply\n");
201 longjmp(timeoutbuf, 1);
204 static int multitech_swallow (register char *match)
209 f = signal(SIGALRM, sigALRM);
216 if (setjmp(timeoutbuf)) {
220 alarm(number(value(DIALTIMEOUT)));
225 if (boolean(value(VERBOSE)))
228 } while (c == *match++);
230 if (boolean(value(VERBOSE)))
233 signal(SIGALRM, SIG_DFL);
237 static struct baud_msg {
249 static int multitech_connect ()
257 if (multitech_swallow("\r\n") == 0)
259 f = signal(SIGALRM, sigALRM);
261 nc = 0; nl = sizeof(dialer_buf)-1;
262 bzero(dialer_buf, sizeof(dialer_buf));
264 for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) {
265 if (setjmp(timeoutbuf))
267 alarm(number(value(DIALTIMEOUT)));
274 if (multitech_swallow("\n") == 0)
278 if (strcmp(dialer_buf, "RINGING") == 0 &&
279 boolean(value(VERBOSE))) {
281 printf("%s\r\n", dialer_buf);
285 if (strncmp(dialer_buf, "CONNECT",
286 sizeof("CONNECT")-1) != 0)
291 if (boolean(value(VERBOSE)))
292 printf("%s\r\n", dialer_buf);
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))
302 if (boolean(value(VERBOSE)))
303 printf("%s\r\n", dialer_buf);
312 printf("%s\r\n", dialer_buf);
319 * This convoluted piece of code attempts to get
320 * the multitech in sync.
322 static int multitechsync ()
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);
336 len = read(FD, buf, sizeof(buf));
339 printf("multitechsync: (\"%s\")\n\r", buf);
341 if (index(buf, '0') ||
342 (index(buf, 'O') && index(buf, 'K')))
346 * If not strapped for DTR control,
347 * try to get command mode.
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);
354 * Toggle DTR to force anyone off that might have left
355 * the modem connected.
357 acu_nap (escape_guard_time);
358 ioctl (FD, TIOCCDTR, 0);
359 acu_nap (escape_guard_time);
360 ioctl (FD, TIOCSDTR, 0);
362 acu_nap (intercommand_delay);
363 multitech_write_str (FD, reset_command);
367 void multitech_write_str (int fd, const char *cp)
370 printf ("multitech: sending %s\n", cp);
372 multitech_write (fd, cp, strlen (cp));
375 void multitech_write (int fd, const char *cp, int n)
378 acu_nap (intercharacter_delay);
379 for ( ; n-- ; cp++) {
382 acu_nap (intercharacter_delay);
387 multitech_verbose_read()
392 if (ioctl(FD, FIONREAD, &n) < 0)
396 if (read(FD, buf, n) != n)
402 /* end of multitech.c */