1 /* $OpenBSD: courier.c,v 1.15 2006/03/17 19:17:13 moritz Exp $ */
2 /* $NetBSD: courier.c,v 1.7 1997/02/11 09:24:16 mrg Exp $ */
5 * Copyright (c) 1986, 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[] = "@(#)courier.c 8.1 (Berkeley) 6/6/93";
39 static const char rcsid[] = "$OpenBSD: courier.c,v 1.15 2006/03/17 19:17:13 moritz Exp $";
44 * Routines for calling up on a Courier modem.
45 * Derived from Hayes driver.
48 #include <sys/ioctl.h>
53 static int dialtimeout = 0;
54 static int connected = 0;
55 static jmp_buf timeoutbuf;
57 static void sigALRM(int);
58 static int cour_swallow(char *);
59 static int cour_connect(void);
60 static int coursync(void);
61 static void cour_write(int, char *, int);
62 static void cour_nap(void);
64 static void cour_verbose_read(void);
68 cour_dialer(char *num, char *acu)
76 if (boolean(value(VERBOSE)))
77 printf("Using \"%s\"\n", acu);
79 tcgetattr(FD, &cntrl);
80 cntrl.c_cflag |= HUPCL;
81 tcsetattr(FD, TCSAFLUSH, &cntrl);
87 printf("can't synchronize with courier\n");
89 logent(value(HOST), num, "courier", "can't synch up");
93 cour_write(FD, "AT E0\r", 6); /* turn off echoing */
96 if (boolean(value(VERBOSE)))
99 tcflush(FD, TCIOFLUSH);
100 cour_write(FD, "AT C1 E0 H0 Q0 X6 V1\r", 21);
101 if (!cour_swallow("\r\nOK\r\n"))
104 cour_write(FD, "AT D", 4);
105 for (cp = num; *cp; cp++)
108 cour_write(FD, num, strlen(num));
109 cour_write(FD, "\r", 1);
110 connected = cour_connect();
113 (void)snprintf(line, sizeof line, "%ld second dial timeout",
114 number(value(DIALTIMEOUT)));
115 logent(value(HOST), num, "cour", line);
124 cour_disconnect(void)
126 /* first hang up the modem*/
127 ioctl(FD, TIOCCDTR, 0);
129 ioctl(FD, TIOCSDTR, 0);
130 coursync(); /* reset */
137 cour_write(FD, "\r", 1); /* send anything to abort the call */
145 printf("\07timeout waiting for reply\n");
147 longjmp(timeoutbuf, 1);
151 cour_swallow(char *match)
156 f = signal(SIGALRM, sigALRM);
163 if (setjmp(timeoutbuf)) {
167 alarm(number(value(DIALTIMEOUT)));
172 if (boolean(value(VERBOSE)))
175 } while (c == *match++);
177 if (boolean(value(VERBOSE)))
180 signal(SIGALRM, SIG_DFL);
192 { " 9600/ARQ", B9600 },
205 if (cour_swallow("\r\n") == 0)
207 f = signal(SIGALRM, sigALRM);
209 nc = 0; nl = sizeof(dialer_buf)-1;
210 bzero(dialer_buf, sizeof(dialer_buf));
212 for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) {
213 if (setjmp(timeoutbuf))
215 alarm(number(value(DIALTIMEOUT)));
222 if (cour_swallow("\n") == 0)
226 if (strcmp(dialer_buf, "RINGING") == 0 &&
227 boolean(value(VERBOSE))) {
229 printf("%s\r\n", dialer_buf);
233 if (strncmp(dialer_buf, "CONNECT",
234 sizeof("CONNECT")-1) != 0)
236 for (bm = baud_msg ; bm->msg ; bm++)
238 dialer_buf+sizeof("CONNECT")-1) == 0) {
239 struct termios cntrl;
241 tcgetattr(FD, &cntrl);
242 cfsetospeed(&cntrl, bm->baud);
243 cfsetispeed(&cntrl, bm->baud);
244 tcsetattr(FD, TCSAFLUSH, &cntrl);
247 if (boolean(value(VERBOSE)))
248 printf("%s\r\n", dialer_buf);
256 if (boolean(value(VERBOSE)))
260 printf("%s\r\n", dialer_buf);
266 * This convoluted piece of code attempts to get
267 * the courier in sync.
276 while (already++ < MAXRETRY) {
277 tcflush(FD, TCIOFLUSH);
278 cour_write(FD, "\rAT Z\r", 6); /* reset modem */
279 bzero(buf, sizeof(buf));
281 ioctl(FD, FIONREAD, &len);
283 len = read(FD, buf, sizeof(buf));
286 printf("coursync: (\"%s\")\n\r", buf);
288 if (strchr(buf, '0') ||
289 (strchr(buf, 'O') && strchr(buf, 'K')))
293 * If not strapped for DTR control,
294 * try to get command mode.
297 cour_write(FD, "+++", 3);
300 * Toggle DTR to force anyone off that might have left
301 * the modem connected.
303 ioctl(FD, TIOCCDTR, 0);
305 ioctl(FD, TIOCSDTR, 0);
307 cour_write(FD, "\rAT Z\r", 6);
312 cour_write(int fd, char *cp, int n)
315 if (boolean(value(VERBOSE)))
320 for ( ; n-- ; cp++) {
329 cour_verbose_read(void)
334 if (ioctl(FD, FIONREAD, &n) < 0)
338 if (read(FD, buf, n) != n)
344 /* Give the courier 50 milliseconds between characters */
351 ts.tv_nsec = 50 * 1000000;
353 nanosleep(&ts, NULL);