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