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
34 * $DragonFly: src/usr.bin/tip/libacu/multitech.c,v 1.3 2005/05/07 23:20:43 corecode Exp $
38 * Routines for calling up on a Courier modem.
39 * Derived from Hayes driver.
43 #include "acucommon.h"
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;
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);
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)
93 if ((i = speed(number(value(BAUDRATE)))) == 0)
98 if (boolean(value(VERBOSE)))
99 printf("Using \"%s\"\n", acu);
106 if (!multitechsync()) {
108 printf("can't synchronize with multitech\n");
110 logent(value(HOST), num, "multitech", "can't synch up");
114 acu_nap (intercommand_delay);
116 multitech_write_str (FD, echo_off_command); /* turn off echoing */
121 if (boolean(value(VERBOSE)))
122 multitech_verbose_read();
127 acu_nap (intercommand_delay);
128 multitech_write_str (FD, init_string);
130 if (!multitech_swallow ("\r\nOK\r\n"))
135 acu_nap (intercommand_delay);
136 multitech_write_str (FD, dial_command);
138 for (cp = num; *cp; cp++)
142 multitech_write_str (FD, num);
144 multitech_write_str (FD, "\r");
146 connected = multitech_connect();
150 sprintf(line, "%d second dial timeout",
151 number(value(DIALTIMEOUT)));
152 logent(value(HOST), num, "multitech", line);
156 multitech_disconnect ();
160 void multitech_disconnect ()
163 for (retries = okay = 0; retries < 3 && !okay; retries++)
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);
171 * If not strapped for DTR control, try to get command mode.
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");
182 logent(value(HOST), "", "multitech", "can't hang up modem");
184 if (boolean(value(VERBOSE)))
185 printf("hang up failed\n");
190 void multitech_abort ()
192 multitech_write_str (FD, "\r"); /* send anything to abort the call */
193 multitech_disconnect ();
196 static void sigALRM ()
198 (void) printf("\07timeout waiting for reply\n");
200 longjmp(timeoutbuf, 1);
203 static int multitech_swallow (register char *match)
208 f = signal(SIGALRM, sigALRM);
215 if (setjmp(timeoutbuf)) {
219 alarm(number(value(DIALTIMEOUT)));
224 if (boolean(value(VERBOSE)))
227 } while (c == *match++);
229 if (boolean(value(VERBOSE)))
232 signal(SIGALRM, SIG_DFL);
236 static struct baud_msg {
248 static int multitech_connect ()
256 if (multitech_swallow("\r\n") == 0)
258 f = signal(SIGALRM, sigALRM);
260 nc = 0; nl = sizeof(dialer_buf)-1;
261 bzero(dialer_buf, sizeof(dialer_buf));
263 for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) {
264 if (setjmp(timeoutbuf))
266 alarm(number(value(DIALTIMEOUT)));
273 if (multitech_swallow("\n") == 0)
277 if (strcmp(dialer_buf, "RINGING") == 0 &&
278 boolean(value(VERBOSE))) {
280 printf("%s\r\n", dialer_buf);
284 if (strncmp(dialer_buf, "CONNECT",
285 sizeof("CONNECT")-1) != 0)
290 if (boolean(value(VERBOSE)))
291 printf("%s\r\n", dialer_buf);
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))
301 if (boolean(value(VERBOSE)))
302 printf("%s\r\n", dialer_buf);
311 printf("%s\r\n", dialer_buf);
318 * This convoluted piece of code attempts to get
319 * the multitech in sync.
321 static int multitechsync ()
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);
335 len = read(FD, buf, sizeof(buf));
338 printf("multitechsync: (\"%s\")\n\r", buf);
340 if (index(buf, '0') ||
341 (index(buf, 'O') && index(buf, 'K')))
345 * If not strapped for DTR control,
346 * try to get command mode.
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);
353 * Toggle DTR to force anyone off that might have left
354 * the modem connected.
356 acu_nap (escape_guard_time);
357 ioctl (FD, TIOCCDTR, 0);
358 acu_nap (escape_guard_time);
359 ioctl (FD, TIOCSDTR, 0);
361 acu_nap (intercommand_delay);
362 multitech_write_str (FD, reset_command);
366 void multitech_write_str (int fd, const char *cp)
369 printf ("multitech: sending %s\n", cp);
371 multitech_write (fd, cp, strlen (cp));
374 void multitech_write (int fd, const char *cp, int n)
377 acu_nap (intercharacter_delay);
378 for ( ; n-- ; cp++) {
381 acu_nap (intercharacter_delay);
386 multitech_verbose_read()
391 if (ioctl(FD, FIONREAD, &n) < 0)
395 if (read(FD, buf, n) != n)
401 /* end of multitech.c */