1 /* $OpenBSD: ventel.c,v 1.12 2006/03/17 19:17:13 moritz Exp $ */
2 /* $NetBSD: ventel.c,v 1.6 1997/02/11 09:24:21 mrg Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (c) 1983, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 static char sccsid[] = "@(#)ventel.c 8.1 (Berkeley) 6/6/93";
41 static const char rcsid[] = "$OpenBSD: ventel.c,v 1.12 2006/03/17 19:17:13 moritz Exp $";
46 * Routines for calling up on a Ventel Modem
47 * The Ventel is expected to be strapped for local echo (just like uucp)
51 #include <sys/ioctl.h>
55 static int dialtimeout = 0;
56 static jmp_buf timeoutbuf;
58 static void echo(char *);
59 static void sigALRM(int);
60 static int gobble(char, char *);
61 static int vensync(int);
64 * some sleep calls have been replaced by this macro
65 * because some ventel modems require two <cr>s in less than
66 * a second in order to 'wake up'... yes, it is dirty...
68 #define delay(num,denom) busyloop(CPUSPEED*num/denom)
69 #define CPUSPEED 1000000 /* VAX 780 is 1MIPS */
70 #define DELAY(n) do { long N = (n); while (--N > 0); } while (0)
71 #define busyloop(n) do { DELAY(n); } while (0)
74 ven_dialer(char *num, char *acu)
82 * Get in synch with a couple of carriage returns
85 printf("can't synchronize with ventel\n");
87 logent(value(HOST), num, "ventel", "can't synch up");
91 if (boolean(value(VERBOSE)))
92 printf("\ndialing...");
94 tcgetattr(FD, &cntrl);
95 cntrl.c_cflag |= HUPCL;
96 tcsetattr(FD, TCSANOW, &cntrl);
97 echo("#k$\r$\n$D$I$A$L$:$ ");
98 for (cp = num; *cp; cp++) {
105 if (gobble('\n', line))
106 connected = gobble('!', line);
107 tcflush(FD, TCIOFLUSH);
110 (void)snprintf(line, sizeof line, "%ld second dial timeout",
111 number(value(DIALTIMEOUT)));
112 logent(value(HOST), num, "ventel", line);
116 ven_disconnect(); /* insurance */
117 if (connected || dialtimeout || !boolean(value(VERBOSE)))
119 /* call failed, parse response for user */
120 cp = strchr(line, '\r');
123 for (cp = line; (cp = strchr(cp, ' ')) != NULL; cp++)
135 printf("%s...", msg);
158 while ((c = *s++) != '\0')
180 printf("\07timeout waiting for reply\n");
182 longjmp(timeoutbuf, 1);
186 gobble(char match, char response[])
192 f = signal(SIGALRM, sigALRM);
195 if (setjmp(timeoutbuf)) {
200 alarm(number(value(DIALTIMEOUT)));
205 if (boolean(value(VERBOSE)))
208 } while (c != '\n' && c != match);
209 signal(SIGALRM, SIG_DFL);
214 #define min(a,b) ((a)>(b)?(b):(a))
216 * This convoluted piece of code attempts to get
217 * the ventel in sync. If you don't have FIONREAD
218 * there are gory ways to simulate this.
223 int already = 0, nread;
227 * Toggle DTR to force anyone off that might have left
228 * the modem connected, and insure a consistent state
231 * If you don't have the ioctl calls to diddle directly
232 * with DTR, you can always try setting the baud rate to 0.
234 ioctl(FD, TIOCCDTR, 0);
236 ioctl(FD, TIOCSDTR, 0);
237 while (already < MAXRETRY) {
239 * After reseting the modem, send it two \r's to
240 * autobaud on. Make sure to delay between them
241 * so the modem can frame the incoming characters.
247 if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
248 perror("tip: ioctl");
252 read(fd, buf, min(nread, 60));
253 if ((buf[nread - 1] & 0177) == '$')
255 nread -= min(nread, 60);