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