ventel.c revision 1.6 1 /* $NetBSD: ventel.c,v 1.6 1997/02/11 09:24:21 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)ventel.c 8.1 (Berkeley) 6/6/93";
39 #endif
40 static char rcsid[] = "$NetBSD: ventel.c,v 1.6 1997/02/11 09:24:21 mrg Exp $";
41 #endif /* not lint */
42
43 /*
44 * Routines for calling up on a Ventel Modem
45 * The Ventel is expected to be strapped for local echo (just like uucp)
46 */
47 #include "tip.h"
48 #include <termios.h>
49 #include <sys/ioctl.h>
50
51 #define MAXRETRY 5
52
53 static void sigALRM();
54 static int timeout = 0;
55 static jmp_buf timeoutbuf;
56
57 static int gobble(), vensync();
58 static void echo();
59
60 /*
61 * some sleep calls have been replaced by this macro
62 * because some ventel modems require two <cr>s in less than
63 * a second in order to 'wake up'... yes, it is dirty...
64 */
65 #define delay(num,denom) busyloop(CPUSPEED*num/denom)
66 #define CPUSPEED 1000000 /* VAX 780 is 1MIPS */
67 #define DELAY(n) { register long N = (n); while (--N > 0); }
68 busyloop(n) { DELAY(n); }
69
70 ven_dialer(num, acu)
71 register char *num;
72 char *acu;
73 {
74 register char *cp;
75 register int connected = 0;
76 char *msg, *index(), line[80];
77 struct termios cntrl;
78
79 /*
80 * Get in synch with a couple of carriage returns
81 */
82 if (!vensync(FD)) {
83 printf("can't synchronize with ventel\n");
84 #ifdef ACULOG
85 logent(value(HOST), num, "ventel", "can't synch up");
86 #endif
87 return (0);
88 }
89 if (boolean(value(VERBOSE)))
90 printf("\ndialing...");
91 fflush(stdout);
92 tcgetattr(FD, &cntrl);
93 cntrl.c_cflag |= HUPCL;
94 tcsetattr(FD, TCSANOW, &cntrl);
95 echo("#k$\r$\n$D$I$A$L$:$ ");
96 for (cp = num; *cp; cp++) {
97 delay(1, 10);
98 write(FD, cp, 1);
99 }
100 delay(1, 10);
101 write(FD, "\r", 1);
102 gobble('\n', line);
103 if (gobble('\n', line))
104 connected = gobble('!', line);
105 tcflush(FD, TCIOFLUSH);
106 #ifdef ACULOG
107 if (timeout) {
108 (void)snprintf(line, sizeof line, "%d second dial timeout",
109 number(value(DIALTIMEOUT)));
110 logent(value(HOST), num, "ventel", line);
111 }
112 #endif
113 if (timeout)
114 ven_disconnect(); /* insurance */
115 if (connected || timeout || !boolean(value(VERBOSE)))
116 return (connected);
117 /* call failed, parse response for user */
118 cp = index(line, '\r');
119 if (cp)
120 *cp = '\0';
121 for (cp = line; cp = index(cp, ' '); cp++)
122 if (cp[1] == ' ')
123 break;
124 if (cp) {
125 while (*cp == ' ')
126 cp++;
127 msg = cp;
128 while (*cp) {
129 if (isupper(*cp))
130 *cp = tolower(*cp);
131 cp++;
132 }
133 printf("%s...", msg);
134 }
135 return (connected);
136 }
137
138 ven_disconnect()
139 {
140
141 close(FD);
142 }
143
144 ven_abort()
145 {
146
147 write(FD, "\03", 1);
148 close(FD);
149 }
150
151 static void
152 echo(s)
153 register char *s;
154 {
155 char c;
156
157 while (c = *s++) switch (c) {
158
159 case '$':
160 read(FD, &c, 1);
161 s++;
162 break;
163
164 case '#':
165 c = *s++;
166 write(FD, &c, 1);
167 break;
168
169 default:
170 write(FD, &c, 1);
171 read(FD, &c, 1);
172 }
173 }
174
175 static void
176 sigALRM()
177 {
178 printf("\07timeout waiting for reply\n");
179 timeout = 1;
180 longjmp(timeoutbuf, 1);
181 }
182
183 static int
184 gobble(match, response)
185 register char match;
186 char response[];
187 {
188 register char *cp = response;
189 sig_t f;
190 char c;
191
192 f = signal(SIGALRM, sigALRM);
193 timeout = 0;
194 do {
195 if (setjmp(timeoutbuf)) {
196 signal(SIGALRM, f);
197 *cp = '\0';
198 return (0);
199 }
200 alarm(number(value(DIALTIMEOUT)));
201 read(FD, cp, 1);
202 alarm(0);
203 c = (*cp++ &= 0177);
204 #ifdef notdef
205 if (boolean(value(VERBOSE)))
206 putchar(c);
207 #endif
208 } while (c != '\n' && c != match);
209 signal(SIGALRM, SIG_DFL);
210 *cp = '\0';
211 return (c == match);
212 }
213
214 #define min(a,b) ((a)>(b)?(b):(a))
215 /*
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.
219 */
220 static int
221 vensync(fd)
222 {
223 int already = 0, nread;
224 char buf[60];
225
226 /*
227 * Toggle DTR to force anyone off that might have left
228 * the modem connected, and insure a consistent state
229 * to start from.
230 *
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.
233 */
234 ioctl(FD, TIOCCDTR, 0);
235 sleep(1);
236 ioctl(FD, TIOCSDTR, 0);
237 while (already < MAXRETRY) {
238 /*
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.
242 */
243 write(fd, "\r", 1);
244 delay(1,10);
245 write(fd, "\r", 1);
246 sleep(2);
247 if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
248 perror("tip: ioctl");
249 continue;
250 }
251 while (nread > 0) {
252 read(fd, buf, min(nread, 60));
253 if ((buf[nread - 1] & 0177) == '$')
254 return (1);
255 nread -= min(nread, 60);
256 }
257 sleep(1);
258 already++;
259 }
260 return (0);
261 }
262
263