ventel.c revision 1.15 1 /* $NetBSD: ventel.c,v 1.15 2006/12/14 14:18:04 christos 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)ventel.c 8.1 (Berkeley) 6/6/93";
36 #endif
37 __RCSID("$NetBSD: ventel.c,v 1.15 2006/12/14 14:18:04 christos Exp $");
38 #endif /* not lint */
39
40 /*
41 * Routines for calling up on a Ventel Modem
42 * The Ventel is expected to be strapped for local echo (just like uucp)
43 */
44 #include "tip.h"
45
46 #define MAXRETRY 5
47
48 static int timeout = 0;
49 static jmp_buf timeoutbuf;
50
51 static void echo(const char *);
52 static int gobble(char, char *);
53 static void sigALRM(int);
54 static int vensync(int);
55
56 /*
57 * some sleep calls have been replaced by usleep(DELAYUS)
58 * because some ventel modems require two <cr>s in less than
59 * a second in order to 'wake up'
60 */
61 #define DELAYUS 100000 /* delay in microseconds */
62
63 int
64 ven_dialer(char *num, char *acu)
65 {
66 char *cp;
67 int connected = 0;
68 char *msg, line[80];
69 struct termios cntrl;
70
71 /*
72 * Get in synch with a couple of carriage returns
73 */
74 if (!vensync(FD)) {
75 printf("can't synchronize with ventel\n");
76 return (0);
77 }
78 if (boolean(value(VERBOSE)))
79 printf("\ndialing...");
80 fflush(stdout);
81 tcgetattr(FD, &cntrl);
82 cntrl.c_cflag |= HUPCL;
83 tcsetattr(FD, TCSANOW, &cntrl);
84 echo("#k$\r$\n$D$I$A$L$:$ ");
85 for (cp = num; *cp; cp++) {
86 usleep(DELAYUS);
87 write(FD, cp, 1);
88 }
89 usleep(DELAYUS);
90 write(FD, "\r", 1);
91 gobble('\n', line);
92 if (gobble('\n', line))
93 connected = gobble('!', line);
94 tcflush(FD, TCIOFLUSH);
95 if (timeout)
96 ven_disconnect(); /* insurance */
97 if (connected || timeout || !boolean(value(VERBOSE)))
98 return (connected);
99 /* call failed, parse response for user */
100 cp = strchr(line, '\r');
101 if (cp)
102 *cp = '\0';
103 for (cp = line; (cp = strchr(cp, ' ')) != NULL; cp++)
104 if (cp[1] == ' ')
105 break;
106 if (cp) {
107 while (*cp == ' ')
108 cp++;
109 msg = cp;
110 while (*cp) {
111 if (isupper((unsigned char)*cp))
112 *cp = tolower((unsigned char)*cp);
113 cp++;
114 }
115 printf("%s...", msg);
116 }
117 return (connected);
118 }
119
120 void
121 ven_disconnect(void)
122 {
123
124 close(FD);
125 }
126
127 void
128 ven_abort(void)
129 {
130
131 write(FD, "\03", 1);
132 close(FD);
133 }
134
135 static void
136 echo(const char *s)
137 {
138 char c;
139
140 while ((c = *s++) != 0) switch (c) {
141
142 case '$':
143 read(FD, &c, 1);
144 s++;
145 break;
146
147 case '#':
148 c = *s++;
149 write(FD, &c, 1);
150 break;
151
152 default:
153 write(FD, &c, 1);
154 read(FD, &c, 1);
155 }
156 }
157
158 static void
159 sigALRM(int dummy)
160 {
161
162 printf("\07timeout waiting for reply\n");
163 timeout = 1;
164 longjmp(timeoutbuf, 1);
165 }
166
167 static int
168 gobble(char match, char response[])
169 {
170 char * volatile cp;
171 sig_t f;
172 char c;
173
174 cp = response;
175 f = signal(SIGALRM, sigALRM);
176 timeout = 0;
177 do {
178 if (setjmp(timeoutbuf)) {
179 signal(SIGALRM, f);
180 *cp = '\0';
181 return (0);
182 }
183 alarm(number(value(DIALTIMEOUT)));
184 read(FD, cp, 1);
185 alarm(0);
186 c = (*cp++ &= 0177);
187 #ifdef notdef
188 if (boolean(value(VERBOSE)))
189 putchar(c);
190 #endif
191 } while (c != '\n' && c != match);
192 signal(SIGALRM, SIG_DFL);
193 *cp = '\0';
194 return (c == match);
195 }
196
197 #define min(a,b) ((a)>(b)?(b):(a))
198 /*
199 * This convoluted piece of code attempts to get
200 * the ventel in sync. If you don't have FIONREAD
201 * there are gory ways to simulate this.
202 */
203 static int
204 vensync(int fd)
205 {
206 int already = 0, nread;
207 char buf[60];
208
209 /*
210 * Toggle DTR to force anyone off that might have left
211 * the modem connected, and insure a consistent state
212 * to start from.
213 *
214 * If you don't have the ioctl calls to diddle directly
215 * with DTR, you can always try setting the baud rate to 0.
216 */
217 ioctl(FD, TIOCCDTR, 0);
218 sleep(1);
219 ioctl(FD, TIOCSDTR, 0);
220 while (already < MAXRETRY) {
221 /*
222 * After reseting the modem, send it two \r's to
223 * autobaud on. Make sure to delay between them
224 * so the modem can frame the incoming characters.
225 */
226 write(fd, "\r", 1);
227 usleep(DELAYUS);
228 write(fd, "\r", 1);
229 sleep(2);
230 if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
231 perror("tip: ioctl");
232 continue;
233 }
234 while (nread > 0) {
235 read(fd, buf, min(nread, sizeof buf));
236 if ((buf[min(nread, sizeof buf) - 1] & 0177) == '$')
237 return (1);
238 nread -= min(nread, 60);
239 }
240 sleep(1);
241 already++;
242 }
243 return (0);
244 }
245