ventel.c revision 1.7 1 /* $NetBSD: ventel.c,v 1.7 1997/11/22 07:28:59 lukem 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)ventel.c 8.1 (Berkeley) 6/6/93";
40 #endif
41 __RCSID("$NetBSD: ventel.c,v 1.7 1997/11/22 07:28:59 lukem Exp $");
42 #endif /* not lint */
43
44 /*
45 * Routines for calling up on a Ventel Modem
46 * The Ventel is expected to be strapped for local echo (just like uucp)
47 */
48 #include "tip.h"
49
50 #define MAXRETRY 5
51
52 static int timeout = 0;
53 static jmp_buf timeoutbuf;
54
55 static void echo __P((char *));
56 static int gobble __P((char, char *));
57 static void sigALRM __P((int));
58 static int vensync __P((int));
59
60 /*
61 * some sleep calls have been replaced by usleep(DELAYUS)
62 * because some ventel modems require two <cr>s in less than
63 * a second in order to 'wake up'
64 */
65 #define DELAYUS 100000 /* delay in microseconds */
66
67 int
68 ven_dialer(num, acu)
69 char *num;
70 char *acu;
71 {
72 char *cp;
73 int connected = 0;
74 char *msg, line[80];
75 struct termios cntrl;
76
77 /*
78 * Get in synch with a couple of carriage returns
79 */
80 if (!vensync(FD)) {
81 printf("can't synchronize with ventel\n");
82 #ifdef ACULOG
83 logent(value(HOST), num, "ventel", "can't synch up");
84 #endif
85 return (0);
86 }
87 if (boolean(value(VERBOSE)))
88 printf("\ndialing...");
89 fflush(stdout);
90 tcgetattr(FD, &cntrl);
91 cntrl.c_cflag |= HUPCL;
92 tcsetattr(FD, TCSANOW, &cntrl);
93 echo("#k$\r$\n$D$I$A$L$:$ ");
94 for (cp = num; *cp; cp++) {
95 usleep(DELAYUS);
96 write(FD, cp, 1);
97 }
98 usleep(DELAYUS);
99 write(FD, "\r", 1);
100 gobble('\n', line);
101 if (gobble('\n', line))
102 connected = gobble('!', line);
103 tcflush(FD, TCIOFLUSH);
104 #ifdef ACULOG
105 if (timeout) {
106 (void)snprintf(line, sizeof line, "%d second dial timeout",
107 (int)number(value(DIALTIMEOUT)));
108 logent(value(HOST), num, "ventel", line);
109 }
110 #endif
111 if (timeout)
112 ven_disconnect(); /* insurance */
113 if (connected || timeout || !boolean(value(VERBOSE)))
114 return (connected);
115 /* call failed, parse response for user */
116 cp = strchr(line, '\r');
117 if (cp)
118 *cp = '\0';
119 for (cp = line; (cp = strchr(cp, ' ')) != NULL; cp++)
120 if (cp[1] == ' ')
121 break;
122 if (cp) {
123 while (*cp == ' ')
124 cp++;
125 msg = cp;
126 while (*cp) {
127 if (isupper(*cp))
128 *cp = tolower(*cp);
129 cp++;
130 }
131 printf("%s...", msg);
132 }
133 return (connected);
134 }
135
136 void
137 ven_disconnect()
138 {
139
140 close(FD);
141 }
142
143 void
144 ven_abort()
145 {
146
147 write(FD, "\03", 1);
148 close(FD);
149 }
150
151 static void
152 echo(s)
153 char *s;
154 {
155 char c;
156
157 while ((c = *s++) != 0) 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(dummy)
177 int dummy;
178 {
179 printf("\07timeout waiting for reply\n");
180 timeout = 1;
181 longjmp(timeoutbuf, 1);
182 }
183
184 static int
185 gobble(match, response)
186 char match;
187 char response[];
188 {
189 char *cp = response;
190 sig_t f;
191 char c;
192
193 #if __GNUC__ /* XXX pacify gcc */
194 (void)&cp;
195 #endif
196
197 f = signal(SIGALRM, sigALRM);
198 timeout = 0;
199 do {
200 if (setjmp(timeoutbuf)) {
201 signal(SIGALRM, f);
202 *cp = '\0';
203 return (0);
204 }
205 alarm(number(value(DIALTIMEOUT)));
206 read(FD, cp, 1);
207 alarm(0);
208 c = (*cp++ &= 0177);
209 #ifdef notdef
210 if (boolean(value(VERBOSE)))
211 putchar(c);
212 #endif
213 } while (c != '\n' && c != match);
214 signal(SIGALRM, SIG_DFL);
215 *cp = '\0';
216 return (c == match);
217 }
218
219 #define min(a,b) ((a)>(b)?(b):(a))
220 /*
221 * This convoluted piece of code attempts to get
222 * the ventel in sync. If you don't have FIONREAD
223 * there are gory ways to simulate this.
224 */
225 static int
226 vensync(fd)
227 int fd;
228 {
229 int already = 0, nread;
230 char buf[60];
231
232 /*
233 * Toggle DTR to force anyone off that might have left
234 * the modem connected, and insure a consistent state
235 * to start from.
236 *
237 * If you don't have the ioctl calls to diddle directly
238 * with DTR, you can always try setting the baud rate to 0.
239 */
240 ioctl(FD, TIOCCDTR, 0);
241 sleep(1);
242 ioctl(FD, TIOCSDTR, 0);
243 while (already < MAXRETRY) {
244 /*
245 * After reseting the modem, send it two \r's to
246 * autobaud on. Make sure to delay between them
247 * so the modem can frame the incoming characters.
248 */
249 write(fd, "\r", 1);
250 usleep(DELAYUS);
251 write(fd, "\r", 1);
252 sleep(2);
253 if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
254 perror("tip: ioctl");
255 continue;
256 }
257 while (nread > 0) {
258 read(fd, buf, min(nread, 60));
259 if ((buf[nread - 1] & 0177) == '$')
260 return (1);
261 nread -= min(nread, 60);
262 }
263 sleep(1);
264 already++;
265 }
266 return (0);
267 }
268
269