1 /* $OpenBSD: ventel.c,v 1.7 2001/11/19 19:02:16 mpech Exp $ */
2 /* $NetBSD: ventel.c,v 1.6 1997/02/11 09:24:21 mrg Exp $ */
5 * Copyright (c) 1983, 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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
42 static char sccsid[] = "@(#)ventel.c 8.1 (Berkeley) 6/6/93";
43 static char rcsid[] = "$OpenBSD: ventel.c,v 1.7 2001/11/19 19:02:16 mpech Exp $";
48 * Routines for calling up on a Ventel Modem
49 * The Ventel is expected to be strapped for local echo (just like uucp)
53 #include <sys/ioctl.h>
57 static void sigALRM();
58 static int timeout = 0;
59 static jmp_buf timeoutbuf;
61 static int gobble(), vensync();
65 * some sleep calls have been replaced by this macro
66 * because some ventel modems require two <cr>s in less than
67 * a second in order to 'wake up'... yes, it is dirty...
69 #define delay(num,denom) busyloop(CPUSPEED*num/denom)
70 #define CPUSPEED 1000000 /* VAX 780 is 1MIPS */
71 #define DELAY(n) do { long N = (n); while (--N > 0); } while (0)
72 #define busyloop(n) do { DELAY(n); } while (0)
85 * Get in synch with a couple of carriage returns
88 printf("can't synchronize with ventel\n");
90 logent(value(HOST), num, "ventel", "can't synch up");
94 if (boolean(value(VERBOSE)))
95 printf("\ndialing...");
97 tcgetattr(FD, &cntrl);
98 cntrl.c_cflag |= HUPCL;
99 tcsetattr(FD, TCSANOW, &cntrl);
100 echo("#k$\r$\n$D$I$A$L$:$ ");
101 for (cp = num; *cp; cp++) {
108 if (gobble('\n', line))
109 connected = gobble('!', line);
110 tcflush(FD, TCIOFLUSH);
113 (void)sprintf(line, "%ld second dial timeout",
114 number(value(DIALTIMEOUT)));
115 logent(value(HOST), num, "ventel", line);
119 ven_disconnect(); /* insurance */
120 if (connected || timeout || !boolean(value(VERBOSE)))
122 /* call failed, parse response for user */
123 cp = strchr(line, '\r');
126 for (cp = line; cp = strchr(cp, ' '); cp++)
138 printf("%s...", msg);
164 while (c = *s++) switch (c) {
185 printf("\07timeout waiting for reply\n");
187 longjmp(timeoutbuf, 1);
191 gobble(match, response)
199 f = signal(SIGALRM, sigALRM);
202 if (setjmp(timeoutbuf)) {
207 alarm(number(value(DIALTIMEOUT)));
212 if (boolean(value(VERBOSE)))
215 } while (c != '\n' && c != match);
216 signal(SIGALRM, SIG_DFL);
221 #define min(a,b) ((a)>(b)?(b):(a))
223 * This convoluted piece of code attempts to get
224 * the ventel in sync. If you don't have FIONREAD
225 * there are gory ways to simulate this.
230 int already = 0, nread;
234 * Toggle DTR to force anyone off that might have left
235 * the modem connected, and insure a consistent state
238 * If you don't have the ioctl calls to diddle directly
239 * with DTR, you can always try setting the baud rate to 0.
241 ioctl(FD, TIOCCDTR, 0);
243 ioctl(FD, TIOCSDTR, 0);
244 while (already < MAXRETRY) {
246 * After reseting the modem, send it two \r's to
247 * autobaud on. Make sure to delay between them
248 * so the modem can frame the incoming characters.
254 if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
255 perror("tip: ioctl");
259 read(fd, buf, min(nread, 60));
260 if ((buf[nread - 1] & 0177) == '$')
262 nread -= min(nread, 60);