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