1 /* $OpenBSD: t3000.c,v 1.14 2006/03/17 19:17:13 moritz Exp $ */
2 /* $NetBSD: t3000.c,v 1.5 1997/02/11 09:24:18 mrg Exp $ */
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 static char sccsid[] = "@(#)t3000.c 8.1 (Berkeley) 6/6/93";
39 static const char rcsid[] = "$OpenBSD: t3000.c,v 1.14 2006/03/17 19:17:13 moritz Exp $";
44 * Routines for calling up on a Telebit T3000 modem.
45 * Derived from Courier driver.
49 #include <sys/ioctl.h>
54 static int dialtimeout = 0;
55 static int connected = 0;
56 static jmp_buf timeoutbuf;
58 static void sigALRM(int);
59 static int t3000_swallow(char *);
60 static int t3000_connect(void);
61 static int t3000_sync(void);
62 static void t3000_write(int, char *, int);
63 static void t3000_nap(void);
65 static void t3000_verbose_read(void);
69 t3000_dialer(char *num, char *acu)
77 if (boolean(value(VERBOSE)))
78 printf("Using \"%s\"\n", acu);
80 tcgetattr(FD, &cntrl);
81 cntrl.c_cflag |= HUPCL;
82 tcsetattr(FD, TCSANOW, &cntrl);
88 printf("can't synchronize with t3000\n");
90 logent(value(HOST), num, "t3000", "can't synch up");
94 t3000_write(FD, "AT E0\r", 6); /* turn off echoing */
97 if (boolean(value(VERBOSE)))
100 tcflush(FD, TCIOFLUSH);
101 t3000_write(FD, "AT E0 H0 Q0 X4 V1\r", 18);
102 if (!t3000_swallow("\r\nOK\r\n"))
105 t3000_write(FD, "AT D", 4);
106 for (cp = num; *cp; cp++)
109 t3000_write(FD, num, strlen(num));
110 t3000_write(FD, "\r", 1);
111 connected = t3000_connect();
114 (void)snprintf(line, sizeof line, "%ld second dial timeout",
115 number(value(DIALTIMEOUT)));
116 logent(value(HOST), num, "t3000", line);
125 t3000_disconnect(void)
127 /* first hang up the modem*/
128 ioctl(FD, TIOCCDTR, 0);
130 ioctl(FD, TIOCSDTR, 0);
131 t3000_sync(); /* reset */
138 t3000_write(FD, "\r", 1); /* send anything to abort the call */
146 printf("\07timeout waiting for reply\n");
148 longjmp(timeoutbuf, 1);
152 t3000_swallow(char *match)
157 f = signal(SIGALRM, sigALRM);
164 if (setjmp(timeoutbuf)) {
168 alarm(number(value(DIALTIMEOUT)));
173 if (boolean(value(VERBOSE)))
176 } while (c == *match++);
178 if (boolean(value(VERBOSE)))
181 signal(SIGALRM, SIG_DFL);
185 #ifndef B19200 /* XXX */
196 { " 1200", B1200, 0 },
197 { " 2400", B2400, 0 },
198 { " 4800", B4800, 0 },
199 { " 9600", B9600, 0 },
200 { " 14400", B19200, B9600 },
201 { " 19200", B19200, B9600 },
202 { " 38400", B38400, B9600 },
203 { " 57600", B38400, B9600 },
204 { " 7512", B9600, 0 },
205 { " 1275", B2400, 0 },
206 { " 7200", B9600, 0 },
207 { " 12000", B19200, B9600 },
217 struct tbaud_msg *bm;
220 if (t3000_swallow("\r\n") == 0)
222 f = signal(SIGALRM, sigALRM);
224 nc = 0; nl = sizeof(dialer_buf)-1;
225 bzero(dialer_buf, sizeof(dialer_buf));
227 for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) {
228 if (setjmp(timeoutbuf))
230 alarm(number(value(DIALTIMEOUT)));
237 if (t3000_swallow("\n") == 0)
241 if (strcmp(dialer_buf, "RINGING") == 0 &&
242 boolean(value(VERBOSE))) {
244 printf("%s\r\n", dialer_buf);
248 if (strncmp(dialer_buf, "CONNECT",
249 sizeof("CONNECT")-1) != 0)
251 for (bm = tbaud_msg ; bm->msg ; bm++)
253 dialer_buf+sizeof("CONNECT")-1) == 0) {
254 struct termios cntrl;
256 tcgetattr(FD, &cntrl);
257 cfsetospeed(&cntrl, bm->baud);
258 cfsetispeed(&cntrl, bm->baud);
259 tcsetattr(FD, TCSAFLUSH, &cntrl);
262 if (boolean(value(VERBOSE)))
263 printf("%s\r\n", dialer_buf);
271 if (boolean(value(VERBOSE)))
275 printf("%s\r\n", dialer_buf);
281 * This convoluted piece of code attempts to get
291 while (already++ < MAXRETRY) {
292 tcflush(FD, TCIOFLUSH);
293 t3000_write(FD, "\rAT Z\r", 6); /* reset modem */
294 bzero(buf, sizeof(buf));
296 ioctl(FD, FIONREAD, &len);
298 if (len == 0) len = 1;
301 len = read(FD, buf, sizeof(buf));
304 printf("t3000_sync: (\"%s\")\n\r", buf);
306 if (strchr(buf, '0') ||
307 (strchr(buf, 'O') && strchr(buf, 'K')))
311 * If not strapped for DTR control,
312 * try to get command mode.
315 t3000_write(FD, "+++", 3);
318 * Toggle DTR to force anyone off that might have left
319 * the modem connected.
321 ioctl(FD, TIOCCDTR, 0);
323 ioctl(FD, TIOCSDTR, 0);
325 t3000_write(FD, "\rAT Z\r", 6);
330 t3000_write(int fd, char *cp, int n)
333 if (boolean(value(VERBOSE)))
338 for ( ; n-- ; cp++) {
347 t3000_verbose_read(void)
352 if (ioctl(FD, FIONREAD, &n) < 0)
356 if (read(FD, buf, n) != n)
362 /* Give the t3000 50 milliseconds between characters */
369 ts.tv_nsec = 50 * 1000000;
371 nanosleep(&ts, NULL);