ventel.c revision 1.3 1 /* $NetBSD: ventel.c,v 1.3 1994/12/08 09:31:52 jtc 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.3 1994/12/08 09:31:52 jtc 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
49 #define MAXRETRY 5
50
51 static void sigALRM();
52 static int timeout = 0;
53 static jmp_buf timeoutbuf;
54
55 /*
56 * some sleep calls have been replaced by this macro
57 * because some ventel modems require two <cr>s in less than
58 * a second in order to 'wake up'... yes, it is dirty...
59 */
60 #define delay(num,denom) busyloop(CPUSPEED*num/denom)
61 #define CPUSPEED 1000000 /* VAX 780 is 1MIPS */
62 #define DELAY(n) { register long N = (n); while (--N > 0); }
63 busyloop(n) { DELAY(n); }
64
65 ven_dialer(num, acu)
66 register char *num;
67 char *acu;
68 {
69 register char *cp;
70 register int connected = 0;
71 char *msg, *index(), line[80];
72 static int gobble(), vensync();
73 static void echo();
74
75 /*
76 * Get in synch with a couple of carriage returns
77 */
78 if (!vensync(FD)) {
79 printf("can't synchronize with ventel\n");
80 #ifdef ACULOG
81 logent(value(HOST), num, "ventel", "can't synch up");
82 #endif
83 return (0);
84 }
85 if (boolean(value(VERBOSE)))
86 printf("\ndialing...");
87 fflush(stdout);
88 ioctl(FD, TIOCHPCL, 0);
89 echo("#k$\r$\n$D$I$A$L$:$ ");
90 for (cp = num; *cp; cp++) {
91 delay(1, 10);
92 write(FD, cp, 1);
93 }
94 delay(1, 10);
95 write(FD, "\r", 1);
96 gobble('\n', line);
97 if (gobble('\n', line))
98 connected = gobble('!', line);
99 ioctl(FD, TIOCFLUSH);
100 #ifdef ACULOG
101 if (timeout) {
102 sprintf(line, "%d second dial timeout",
103 number(value(DIALTIMEOUT)));
104 logent(value(HOST), num, "ventel", line);
105 }
106 #endif
107 if (timeout)
108 ven_disconnect(); /* insurance */
109 if (connected || timeout || !boolean(value(VERBOSE)))
110 return (connected);
111 /* call failed, parse response for user */
112 cp = index(line, '\r');
113 if (cp)
114 *cp = '\0';
115 for (cp = line; cp = index(cp, ' '); cp++)
116 if (cp[1] == ' ')
117 break;
118 if (cp) {
119 while (*cp == ' ')
120 cp++;
121 msg = cp;
122 while (*cp) {
123 if (isupper(*cp))
124 *cp = tolower(*cp);
125 cp++;
126 }
127 printf("%s...", msg);
128 }
129 return (connected);
130 }
131
132 ven_disconnect()
133 {
134
135 close(FD);
136 }
137
138 ven_abort()
139 {
140
141 write(FD, "\03", 1);
142 close(FD);
143 }
144
145 static void
146 echo(s)
147 register char *s;
148 {
149 char c;
150
151 while (c = *s++) switch (c) {
152
153 case '$':
154 read(FD, &c, 1);
155 s++;
156 break;
157
158 case '#':
159 c = *s++;
160 write(FD, &c, 1);
161 break;
162
163 default:
164 write(FD, &c, 1);
165 read(FD, &c, 1);
166 }
167 }
168
169 static void
170 sigALRM()
171 {
172 printf("\07timeout waiting for reply\n");
173 timeout = 1;
174 longjmp(timeoutbuf, 1);
175 }
176
177 static int
178 gobble(match, response)
179 register char match;
180 char response[];
181 {
182 register char *cp = response;
183 sig_t f;
184 char c;
185
186 f = signal(SIGALRM, sigALRM);
187 timeout = 0;
188 do {
189 if (setjmp(timeoutbuf)) {
190 signal(SIGALRM, f);
191 *cp = '\0';
192 return (0);
193 }
194 alarm(number(value(DIALTIMEOUT)));
195 read(FD, cp, 1);
196 alarm(0);
197 c = (*cp++ &= 0177);
198 #ifdef notdef
199 if (boolean(value(VERBOSE)))
200 putchar(c);
201 #endif
202 } while (c != '\n' && c != match);
203 signal(SIGALRM, SIG_DFL);
204 *cp = '\0';
205 return (c == match);
206 }
207
208 #define min(a,b) ((a)>(b)?(b):(a))
209 /*
210 * This convoluted piece of code attempts to get
211 * the ventel in sync. If you don't have FIONREAD
212 * there are gory ways to simulate this.
213 */
214 static int
215 vensync(fd)
216 {
217 int already = 0, nread;
218 char buf[60];
219
220 /*
221 * Toggle DTR to force anyone off that might have left
222 * the modem connected, and insure a consistent state
223 * to start from.
224 *
225 * If you don't have the ioctl calls to diddle directly
226 * with DTR, you can always try setting the baud rate to 0.
227 */
228 ioctl(FD, TIOCCDTR, 0);
229 sleep(1);
230 ioctl(FD, TIOCSDTR, 0);
231 while (already < MAXRETRY) {
232 /*
233 * After reseting the modem, send it two \r's to
234 * autobaud on. Make sure to delay between them
235 * so the modem can frame the incoming characters.
236 */
237 write(fd, "\r", 1);
238 delay(1,10);
239 write(fd, "\r", 1);
240 sleep(2);
241 if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
242 perror("tip: ioctl");
243 continue;
244 }
245 while (nread > 0) {
246 read(fd, buf, min(nread, 60));
247 if ((buf[nread - 1] & 0177) == '$')
248 return (1);
249 nread -= min(nread, 60);
250 }
251 sleep(1);
252 already++;
253 }
254 return (0);
255 }
256
257