t3000.c revision 1.8 1 1.8 mrg /* $NetBSD: t3000.c,v 1.8 1998/07/12 09:14:20 mrg Exp $ */
2 1.2 jtc
3 1.1 jtc /*
4 1.1 jtc * Copyright (c) 1992, 1993
5 1.1 jtc * The Regents of the University of California. All rights reserved.
6 1.1 jtc *
7 1.1 jtc * Redistribution and use in source and binary forms, with or without
8 1.1 jtc * modification, are permitted provided that the following conditions
9 1.1 jtc * are met:
10 1.1 jtc * 1. Redistributions of source code must retain the above copyright
11 1.1 jtc * notice, this list of conditions and the following disclaimer.
12 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jtc * notice, this list of conditions and the following disclaimer in the
14 1.1 jtc * documentation and/or other materials provided with the distribution.
15 1.1 jtc * 3. All advertising materials mentioning features or use of this software
16 1.1 jtc * must display the following acknowledgement:
17 1.1 jtc * This product includes software developed by the University of
18 1.1 jtc * California, Berkeley and its contributors.
19 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
20 1.1 jtc * may be used to endorse or promote products derived from this software
21 1.1 jtc * without specific prior written permission.
22 1.1 jtc *
23 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 jtc * SUCH DAMAGE.
34 1.1 jtc */
35 1.1 jtc
36 1.6 lukem #include <sys/cdefs.h>
37 1.1 jtc #ifndef lint
38 1.2 jtc #if 0
39 1.1 jtc static char sccsid[] = "@(#)t3000.c 8.1 (Berkeley) 6/6/93";
40 1.2 jtc #endif
41 1.8 mrg __RCSID("$NetBSD: t3000.c,v 1.8 1998/07/12 09:14:20 mrg Exp $");
42 1.1 jtc #endif /* not lint */
43 1.1 jtc
44 1.1 jtc /*
45 1.1 jtc * Routines for calling up on a Telebit T3000 modem.
46 1.1 jtc * Derived from Courier driver.
47 1.1 jtc */
48 1.1 jtc #include "tip.h"
49 1.3 pk
50 1.1 jtc #define MAXRETRY 5
51 1.1 jtc
52 1.1 jtc static int timeout = 0;
53 1.1 jtc static int connected = 0;
54 1.6 lukem static jmp_buf timeoutbuf;
55 1.6 lukem
56 1.6 lukem static void sigALRM __P((int));
57 1.6 lukem static int t3000_connect __P((void));
58 1.6 lukem static void t3000_nap __P((void));
59 1.6 lukem static void t3000_napx __P((int));
60 1.6 lukem static int t3000_swallow __P((char *));
61 1.6 lukem static int t3000_sync __P((void));
62 1.6 lukem static void t3000_write __P((int, char *, int));
63 1.1 jtc
64 1.6 lukem int
65 1.1 jtc t3000_dialer(num, acu)
66 1.6 lukem char *num;
67 1.1 jtc char *acu;
68 1.1 jtc {
69 1.6 lukem char *cp;
70 1.3 pk struct termios cntrl;
71 1.1 jtc #ifdef ACULOG
72 1.1 jtc char line[80];
73 1.1 jtc #endif
74 1.1 jtc
75 1.1 jtc if (boolean(value(VERBOSE)))
76 1.1 jtc printf("Using \"%s\"\n", acu);
77 1.1 jtc
78 1.3 pk tcgetattr(FD, &cntrl);
79 1.3 pk cntrl.c_cflag |= HUPCL;
80 1.3 pk tcsetattr(FD, TCSANOW, &cntrl);
81 1.1 jtc /*
82 1.1 jtc * Get in synch.
83 1.1 jtc */
84 1.1 jtc if (!t3000_sync()) {
85 1.1 jtc badsynch:
86 1.1 jtc printf("can't synchronize with t3000\n");
87 1.1 jtc #ifdef ACULOG
88 1.1 jtc logent(value(HOST), num, "t3000", "can't synch up");
89 1.1 jtc #endif
90 1.1 jtc return (0);
91 1.1 jtc }
92 1.1 jtc t3000_write(FD, "AT E0\r", 6); /* turn off echoing */
93 1.1 jtc sleep(1);
94 1.1 jtc #ifdef DEBUG
95 1.1 jtc if (boolean(value(VERBOSE)))
96 1.1 jtc t3000_verbose_read();
97 1.1 jtc #endif
98 1.3 pk tcflush(FD, TCIOFLUSH);
99 1.1 jtc t3000_write(FD, "AT E0 H0 Q0 X4 V1\r", 18);
100 1.1 jtc if (!t3000_swallow("\r\nOK\r\n"))
101 1.1 jtc goto badsynch;
102 1.1 jtc fflush(stdout);
103 1.1 jtc t3000_write(FD, "AT D", 4);
104 1.1 jtc for (cp = num; *cp; cp++)
105 1.1 jtc if (*cp == '=')
106 1.1 jtc *cp = ',';
107 1.1 jtc t3000_write(FD, num, strlen(num));
108 1.1 jtc t3000_write(FD, "\r", 1);
109 1.1 jtc connected = t3000_connect();
110 1.1 jtc #ifdef ACULOG
111 1.1 jtc if (timeout) {
112 1.5 mrg (void)snprintf(line, sizeof line, "%d second dial timeout",
113 1.6 lukem (int)number(value(DIALTIMEOUT)));
114 1.1 jtc logent(value(HOST), num, "t3000", line);
115 1.1 jtc }
116 1.1 jtc #endif
117 1.1 jtc if (timeout)
118 1.1 jtc t3000_disconnect();
119 1.1 jtc return (connected);
120 1.1 jtc }
121 1.1 jtc
122 1.6 lukem void
123 1.1 jtc t3000_disconnect()
124 1.1 jtc {
125 1.1 jtc /* first hang up the modem*/
126 1.1 jtc ioctl(FD, TIOCCDTR, 0);
127 1.1 jtc sleep(1);
128 1.1 jtc ioctl(FD, TIOCSDTR, 0);
129 1.1 jtc t3000_sync(); /* reset */
130 1.1 jtc close(FD);
131 1.1 jtc }
132 1.1 jtc
133 1.6 lukem void
134 1.1 jtc t3000_abort()
135 1.1 jtc {
136 1.1 jtc t3000_write(FD, "\r", 1); /* send anything to abort the call */
137 1.1 jtc t3000_disconnect();
138 1.1 jtc }
139 1.1 jtc
140 1.1 jtc static void
141 1.6 lukem sigALRM(dummy)
142 1.6 lukem int dummy;
143 1.1 jtc {
144 1.1 jtc printf("\07timeout waiting for reply\n");
145 1.1 jtc timeout = 1;
146 1.1 jtc longjmp(timeoutbuf, 1);
147 1.1 jtc }
148 1.1 jtc
149 1.1 jtc static int
150 1.1 jtc t3000_swallow(match)
151 1.6 lukem char *match;
152 1.6 lukem {
153 1.1 jtc sig_t f;
154 1.1 jtc char c;
155 1.1 jtc
156 1.6 lukem #if __GNUC__ /* XXX pacify gcc */
157 1.6 lukem (void)&match;
158 1.6 lukem #endif
159 1.6 lukem
160 1.1 jtc f = signal(SIGALRM, sigALRM);
161 1.1 jtc timeout = 0;
162 1.1 jtc do {
163 1.1 jtc if (*match =='\0') {
164 1.1 jtc signal(SIGALRM, f);
165 1.1 jtc return (1);
166 1.1 jtc }
167 1.1 jtc if (setjmp(timeoutbuf)) {
168 1.1 jtc signal(SIGALRM, f);
169 1.1 jtc return (0);
170 1.1 jtc }
171 1.1 jtc alarm(number(value(DIALTIMEOUT)));
172 1.1 jtc read(FD, &c, 1);
173 1.1 jtc alarm(0);
174 1.1 jtc c &= 0177;
175 1.1 jtc #ifdef DEBUG
176 1.1 jtc if (boolean(value(VERBOSE)))
177 1.1 jtc putchar(c);
178 1.1 jtc #endif
179 1.1 jtc } while (c == *match++);
180 1.1 jtc #ifdef DEBUG
181 1.1 jtc if (boolean(value(VERBOSE)))
182 1.1 jtc fflush(stdout);
183 1.1 jtc #endif
184 1.1 jtc signal(SIGALRM, SIG_DFL);
185 1.1 jtc return (0);
186 1.1 jtc }
187 1.1 jtc
188 1.1 jtc #ifndef B19200 /* XXX */
189 1.1 jtc #define B19200 EXTA
190 1.1 jtc #define B38400 EXTB
191 1.1 jtc #endif
192 1.1 jtc
193 1.1 jtc struct tbaud_msg {
194 1.1 jtc char *msg;
195 1.1 jtc int baud;
196 1.1 jtc int baud2;
197 1.1 jtc } tbaud_msg[] = {
198 1.6 lukem { "", B300, 0 },
199 1.6 lukem { " 1200", B1200, 0 },
200 1.6 lukem { " 2400", B2400, 0 },
201 1.6 lukem { " 4800", B4800, 0 },
202 1.6 lukem { " 9600", B9600, 0 },
203 1.6 lukem { " 14400", B19200, B9600 },
204 1.6 lukem { " 19200", B19200, B9600 },
205 1.6 lukem { " 38400", B38400, B9600 },
206 1.6 lukem { " 57600", B38400, B9600 },
207 1.6 lukem { " 7512", B9600, 0 },
208 1.6 lukem { " 1275", B2400, 0 },
209 1.6 lukem { " 7200", B9600, 0 },
210 1.6 lukem { " 12000", B19200, B9600 },
211 1.6 lukem { 0, 0, 0 },
212 1.1 jtc };
213 1.1 jtc
214 1.1 jtc static int
215 1.1 jtc t3000_connect()
216 1.1 jtc {
217 1.1 jtc char c;
218 1.1 jtc int nc, nl, n;
219 1.1 jtc char dialer_buf[64];
220 1.1 jtc struct tbaud_msg *bm;
221 1.1 jtc sig_t f;
222 1.1 jtc
223 1.6 lukem #if __GNUC__ /* XXX pacify gcc */
224 1.6 lukem (void)&nc;
225 1.6 lukem (void)&nl;
226 1.6 lukem #endif
227 1.6 lukem
228 1.1 jtc if (t3000_swallow("\r\n") == 0)
229 1.1 jtc return (0);
230 1.1 jtc f = signal(SIGALRM, sigALRM);
231 1.1 jtc again:
232 1.6 lukem memset(dialer_buf, 0, sizeof(dialer_buf));
233 1.1 jtc timeout = 0;
234 1.1 jtc for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) {
235 1.1 jtc if (setjmp(timeoutbuf))
236 1.1 jtc break;
237 1.1 jtc alarm(number(value(DIALTIMEOUT)));
238 1.1 jtc n = read(FD, &c, 1);
239 1.1 jtc alarm(0);
240 1.1 jtc if (n <= 0)
241 1.1 jtc break;
242 1.1 jtc c &= 0x7f;
243 1.1 jtc if (c == '\r') {
244 1.1 jtc if (t3000_swallow("\n") == 0)
245 1.1 jtc break;
246 1.1 jtc if (!dialer_buf[0])
247 1.1 jtc goto again;
248 1.1 jtc if (strcmp(dialer_buf, "RINGING") == 0 &&
249 1.1 jtc boolean(value(VERBOSE))) {
250 1.1 jtc #ifdef DEBUG
251 1.1 jtc printf("%s\r\n", dialer_buf);
252 1.1 jtc #endif
253 1.1 jtc goto again;
254 1.1 jtc }
255 1.1 jtc if (strncmp(dialer_buf, "CONNECT",
256 1.1 jtc sizeof("CONNECT")-1) != 0)
257 1.1 jtc break;
258 1.1 jtc for (bm = tbaud_msg ; bm->msg ; bm++)
259 1.1 jtc if (strcmp(bm->msg,
260 1.1 jtc dialer_buf+sizeof("CONNECT")-1) == 0) {
261 1.3 pk struct termios cntrl;
262 1.3 pk
263 1.3 pk tcgetattr(FD, &cntrl);
264 1.3 pk cfsetospeed(&cntrl, bm->baud);
265 1.3 pk cfsetispeed(&cntrl, bm->baud);
266 1.3 pk tcsetattr(FD, TCSAFLUSH, &cntrl);
267 1.1 jtc signal(SIGALRM, f);
268 1.1 jtc #ifdef DEBUG
269 1.1 jtc if (boolean(value(VERBOSE)))
270 1.1 jtc printf("%s\r\n", dialer_buf);
271 1.1 jtc #endif
272 1.1 jtc return (1);
273 1.1 jtc }
274 1.1 jtc break;
275 1.1 jtc }
276 1.1 jtc dialer_buf[nc] = c;
277 1.1 jtc #ifdef notdef
278 1.1 jtc if (boolean(value(VERBOSE)))
279 1.1 jtc putchar(c);
280 1.1 jtc #endif
281 1.1 jtc }
282 1.1 jtc printf("%s\r\n", dialer_buf);
283 1.1 jtc signal(SIGALRM, f);
284 1.1 jtc return (0);
285 1.1 jtc }
286 1.1 jtc
287 1.1 jtc /*
288 1.1 jtc * This convoluted piece of code attempts to get
289 1.1 jtc * the t3000 in sync.
290 1.1 jtc */
291 1.1 jtc static int
292 1.1 jtc t3000_sync()
293 1.1 jtc {
294 1.1 jtc int already = 0;
295 1.1 jtc int len;
296 1.1 jtc char buf[40];
297 1.1 jtc
298 1.1 jtc while (already++ < MAXRETRY) {
299 1.3 pk tcflush(FD, TCIOFLUSH);
300 1.1 jtc t3000_write(FD, "\rAT Z\r", 6); /* reset modem */
301 1.6 lukem memset(buf, 0, sizeof(buf));
302 1.1 jtc sleep(2);
303 1.1 jtc ioctl(FD, FIONREAD, &len);
304 1.1 jtc #if 1
305 1.1 jtc if (len == 0) len = 1;
306 1.1 jtc #endif
307 1.1 jtc if (len) {
308 1.1 jtc len = read(FD, buf, sizeof(buf));
309 1.1 jtc #ifdef DEBUG
310 1.1 jtc buf[len] = '\0';
311 1.1 jtc printf("t3000_sync: (\"%s\")\n\r", buf);
312 1.1 jtc #endif
313 1.6 lukem if (strchr(buf, '0') ||
314 1.6 lukem (strchr(buf, 'O') && strchr(buf, 'K')))
315 1.1 jtc return(1);
316 1.1 jtc }
317 1.1 jtc /*
318 1.1 jtc * If not strapped for DTR control,
319 1.1 jtc * try to get command mode.
320 1.1 jtc */
321 1.1 jtc sleep(1);
322 1.1 jtc t3000_write(FD, "+++", 3);
323 1.1 jtc sleep(1);
324 1.1 jtc /*
325 1.1 jtc * Toggle DTR to force anyone off that might have left
326 1.1 jtc * the modem connected.
327 1.1 jtc */
328 1.1 jtc ioctl(FD, TIOCCDTR, 0);
329 1.1 jtc sleep(1);
330 1.1 jtc ioctl(FD, TIOCSDTR, 0);
331 1.1 jtc }
332 1.1 jtc t3000_write(FD, "\rAT Z\r", 6);
333 1.1 jtc return (0);
334 1.1 jtc }
335 1.1 jtc
336 1.6 lukem static void
337 1.1 jtc t3000_write(fd, cp, n)
338 1.6 lukem int fd;
339 1.6 lukem char *cp;
340 1.6 lukem int n;
341 1.1 jtc {
342 1.8 mrg
343 1.1 jtc #ifdef notdef
344 1.1 jtc if (boolean(value(VERBOSE)))
345 1.1 jtc write(1, cp, n);
346 1.1 jtc #endif
347 1.3 pk tcdrain(fd);
348 1.1 jtc t3000_nap();
349 1.1 jtc for ( ; n-- ; cp++) {
350 1.1 jtc write(fd, cp, 1);
351 1.3 pk tcdrain(fd);
352 1.1 jtc t3000_nap();
353 1.1 jtc }
354 1.1 jtc }
355 1.1 jtc
356 1.1 jtc #ifdef DEBUG
357 1.1 jtc t3000_verbose_read()
358 1.1 jtc {
359 1.1 jtc int n = 0;
360 1.1 jtc char buf[BUFSIZ];
361 1.1 jtc
362 1.1 jtc if (ioctl(FD, FIONREAD, &n) < 0)
363 1.1 jtc return;
364 1.1 jtc if (n <= 0)
365 1.1 jtc return;
366 1.1 jtc if (read(FD, buf, n) != n)
367 1.1 jtc return;
368 1.1 jtc write(1, buf, n);
369 1.1 jtc }
370 1.1 jtc #endif
371 1.1 jtc
372 1.1 jtc /*
373 1.1 jtc * Code stolen from /usr/src/lib/libc/gen/sleep.c
374 1.1 jtc */
375 1.1 jtc #define mask(s) (1<<((s)-1))
376 1.1 jtc #define setvec(vec, a) \
377 1.1 jtc vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0
378 1.1 jtc
379 1.7 mrg static int napms = 50; /* Give the t3000 50 milliseconds between characters */
380 1.1 jtc
381 1.1 jtc static int ringring;
382 1.1 jtc
383 1.6 lukem static void
384 1.1 jtc t3000_nap()
385 1.1 jtc {
386 1.1 jtc
387 1.1 jtc int omask;
388 1.1 jtc struct itimerval itv, oitv;
389 1.6 lukem struct itimerval *itp = &itv;
390 1.1 jtc struct sigvec vec, ovec;
391 1.1 jtc
392 1.1 jtc timerclear(&itp->it_interval);
393 1.1 jtc timerclear(&itp->it_value);
394 1.1 jtc if (setitimer(ITIMER_REAL, itp, &oitv) < 0)
395 1.1 jtc return;
396 1.1 jtc setvec(ovec, SIG_DFL);
397 1.1 jtc omask = sigblock(mask(SIGALRM));
398 1.1 jtc itp->it_value.tv_sec = napms/1000;
399 1.1 jtc itp->it_value.tv_usec = ((napms%1000)*1000);
400 1.1 jtc setvec(vec, t3000_napx);
401 1.1 jtc ringring = 0;
402 1.1 jtc (void) sigvec(SIGALRM, &vec, &ovec);
403 1.1 jtc (void) setitimer(ITIMER_REAL, itp, (struct itimerval *)0);
404 1.1 jtc while (!ringring)
405 1.1 jtc sigpause(omask &~ mask(SIGALRM));
406 1.1 jtc (void) sigvec(SIGALRM, &ovec, (struct sigvec *)0);
407 1.1 jtc (void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0);
408 1.1 jtc (void) sigsetmask(omask);
409 1.1 jtc }
410 1.1 jtc
411 1.1 jtc static void
412 1.6 lukem t3000_napx(dummy)
413 1.6 lukem int dummy;
414 1.1 jtc {
415 1.8 mrg
416 1.1 jtc ringring = 1;
417 1.1 jtc }
418