Sync usage with man page.
[netbsd-mini2440.git] / usr.bin / tip / aculib / t3000.c
blob9d3db090b8a4f3d4dfc80c63e8abafa4d8eb12d8
1 /* $NetBSD: t3000.c,v 1.13 2006/04/03 02:25:27 perry Exp $ */
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)t3000.c 8.1 (Berkeley) 6/6/93";
36 #endif
37 __RCSID("$NetBSD: t3000.c,v 1.13 2006/04/03 02:25:27 perry Exp $");
38 #endif /* not lint */
41 * Routines for calling up on a Telebit T3000 modem.
42 * Derived from Courier driver.
44 #include "tip.h"
46 #define MAXRETRY 5
48 static int timeout = 0;
49 static int connected = 0;
50 static jmp_buf timeoutbuf;
52 static void sigALRM(int);
53 static int t3000_connect(void);
54 static void t3000_nap(void);
55 static void t3000_napx(int);
56 static int t3000_swallow(const char *);
57 static int t3000_sync(void);
58 static void t3000_write(int, const char *, int);
60 int
61 t3000_dialer(char *num, char *acu)
63 char *cp;
64 struct termios cntrl;
66 if (boolean(value(VERBOSE)))
67 (void)printf("Using \"%s\"\n", acu);
69 (void)tcgetattr(FD, &cntrl);
70 cntrl.c_cflag |= HUPCL;
71 (void)tcsetattr(FD, TCSANOW, &cntrl);
73 * Get in synch.
75 if (!t3000_sync()) {
76 badsynch:
77 (void)printf("can't synchronize with t3000\n");
78 return (0);
80 t3000_write(FD, "AT E0\r", 6); /* turn off echoing */
81 (void)sleep(1);
82 #ifdef DEBUG
83 if (boolean(value(VERBOSE)))
84 t3000_verbose_read();
85 #endif
86 (void)tcflush(FD, TCIOFLUSH);
87 t3000_write(FD, "AT E0 H0 Q0 X4 V1\r", 18);
88 if (!t3000_swallow("\r\nOK\r\n"))
89 goto badsynch;
90 (void)fflush(stdout);
91 t3000_write(FD, "AT D", 4);
92 for (cp = num; *cp; cp++)
93 if (*cp == '=')
94 *cp = ',';
95 t3000_write(FD, num, (int)strlen(num));
96 t3000_write(FD, "\r", 1);
97 connected = t3000_connect();
98 if (timeout)
99 t3000_disconnect();
100 return (connected);
103 void
104 t3000_disconnect(void)
106 /* first hang up the modem*/
107 (void)ioctl(FD, TIOCCDTR, 0);
108 (void)sleep(1);
109 (void)ioctl(FD, TIOCSDTR, 0);
110 (void)t3000_sync(); /* reset */
111 (void)close(FD);
114 void
115 t3000_abort(void)
117 t3000_write(FD, "\r", 1); /* send anything to abort the call */
118 t3000_disconnect();
121 static void
122 /*ARGSUSED*/
123 sigALRM(int dummy __unused)
125 (void)printf("\07timeout waiting for reply\n");
126 timeout = 1;
127 longjmp(timeoutbuf, 1);
130 static int
131 t3000_swallow(const char * volatile match)
133 sig_t f;
134 char c;
136 f = signal(SIGALRM, sigALRM);
137 timeout = 0;
138 do {
139 if (*match =='\0') {
140 (void)signal(SIGALRM, f);
141 return (1);
143 if (setjmp(timeoutbuf)) {
144 (void)signal(SIGALRM, f);
145 return (0);
147 (void)alarm((unsigned)number(value(DIALTIMEOUT)));
148 (void)read(FD, &c, 1);
149 (void)alarm(0);
150 c &= 0177;
151 #ifdef DEBUG
152 if (boolean(value(VERBOSE)))
153 (void)putchar(c);
154 #endif
155 } while (c == *match++);
156 #ifdef DEBUG
157 if (boolean(value(VERBOSE)))
158 (void)fflush(stdout);
159 #endif
160 (void)signal(SIGALRM, SIG_DFL);
161 return (0);
164 #ifndef B19200 /* XXX */
165 #define B19200 EXTA
166 #define B38400 EXTB
167 #endif
169 struct tbaud_msg {
170 const char *msg;
171 unsigned int baud;
172 unsigned int baud2;
173 } tbaud_msg[] = {
174 { "", B300, 0 },
175 { " 1200", B1200, 0 },
176 { " 2400", B2400, 0 },
177 { " 4800", B4800, 0 },
178 { " 9600", B9600, 0 },
179 { " 14400", B19200, B9600 },
180 { " 19200", B19200, B9600 },
181 { " 38400", B38400, B9600 },
182 { " 57600", B38400, B9600 },
183 { " 7512", B9600, 0 },
184 { " 1275", B2400, 0 },
185 { " 7200", B9600, 0 },
186 { " 12000", B19200, B9600 },
187 { 0, 0, 0 },
190 static int
191 t3000_connect(void)
193 char c;
194 int volatile nc;
195 int volatile nl;
196 int n;
197 char dialer_buf[64];
198 struct tbaud_msg *bm;
199 sig_t f;
201 if (t3000_swallow("\r\n") == 0)
202 return (0);
203 f = signal(SIGALRM, sigALRM);
204 again:
205 (void)memset(dialer_buf, 0, sizeof(dialer_buf));
206 timeout = 0;
207 for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) {
208 if (setjmp(timeoutbuf))
209 break;
210 (void)alarm((unsigned)number(value(DIALTIMEOUT)));
211 n = read(FD, &c, 1);
212 (void)alarm(0);
213 if (n <= 0)
214 break;
215 c &= 0x7f;
216 if (c == '\r') {
217 if (t3000_swallow("\n") == 0)
218 break;
219 if (!dialer_buf[0])
220 goto again;
221 if (strcmp(dialer_buf, "RINGING") == 0 &&
222 boolean(value(VERBOSE))) {
223 #ifdef DEBUG
224 (void)printf("%s\r\n", dialer_buf);
225 #endif
226 goto again;
228 if (strncmp(dialer_buf, "CONNECT",
229 sizeof("CONNECT")-1) != 0)
230 break;
231 for (bm = tbaud_msg ; bm->msg ; bm++)
232 if (strcmp(bm->msg,
233 dialer_buf+sizeof("CONNECT")-1) == 0) {
234 struct termios cntrl;
236 (void)tcgetattr(FD, &cntrl);
237 (void)cfsetospeed(&cntrl, bm->baud);
238 (void)cfsetispeed(&cntrl, bm->baud);
239 (void)tcsetattr(FD, TCSAFLUSH, &cntrl);
240 (void)signal(SIGALRM, f);
241 #ifdef DEBUG
242 if (boolean(value(VERBOSE)))
243 (void)printf("%s\r\n", dialer_buf);
244 #endif
245 return (1);
247 break;
249 dialer_buf[nc] = c;
250 #ifdef notdef
251 if (boolean(value(VERBOSE)))
252 (void)putchar(c);
253 #endif
255 (void)printf("%s\r\n", dialer_buf);
256 (void)signal(SIGALRM, f);
257 return (0);
261 * This convoluted piece of code attempts to get
262 * the t3000 in sync.
264 static int
265 t3000_sync(void)
267 int already = 0;
268 int len;
269 char buf[40];
271 while (already++ < MAXRETRY) {
272 (void)tcflush(FD, TCIOFLUSH);
273 t3000_write(FD, "\rAT Z\r", 6); /* reset modem */
274 (void)memset(buf, 0, sizeof(buf));
275 (void)sleep(2);
276 (void)ioctl(FD, FIONREAD, &len);
277 #if 1
278 if (len == 0) len = 1;
279 #endif
280 if (len) {
281 len = read(FD, buf, sizeof(buf));
282 #ifdef DEBUG
283 buf[len] = '\0';
284 (void)printf("t3000_sync: (\"%s\")\n\r", buf);
285 #endif
286 if (strchr(buf, '0') ||
287 (strchr(buf, 'O') && strchr(buf, 'K')))
288 return(1);
291 * If not strapped for DTR control,
292 * try to get command mode.
294 (void)sleep(1);
295 t3000_write(FD, "+++", 3);
296 (void)sleep(1);
298 * Toggle DTR to force anyone off that might have left
299 * the modem connected.
301 (void)ioctl(FD, TIOCCDTR, 0);
302 (void)sleep(1);
303 (void)ioctl(FD, TIOCSDTR, 0);
305 t3000_write(FD, "\rAT Z\r", 6);
306 return (0);
309 static void
310 t3000_write(int fd, const char *cp, int n)
313 #ifdef notdef
314 if (boolean(value(VERBOSE)))
315 (void)write(1, cp, n);
316 #endif
317 (void)tcdrain(fd);
318 t3000_nap();
319 for ( ; n-- ; cp++) {
320 (void)write(fd, cp, 1);
321 (void)tcdrain(fd);
322 t3000_nap();
326 #ifdef DEBUG
327 t3000_verbose_read(void)
329 int n = 0;
330 char buf[BUFSIZ];
332 if (ioctl(FD, FIONREAD, &n) < 0)
333 return;
334 if (n <= 0)
335 return;
336 if (read(FD, buf, n) != n)
337 return;
338 (void)write(1, buf, n);
340 #endif
342 #define setsa(sa, a) \
343 sa.sa_handler = a; (void)sigemptyset(&sa.sa_mask); sa.sa_flags = 0
345 static int napms = 50; /* Give the t3000 50 milliseconds between characters */
347 static int ringring;
349 void
350 t3000_nap(void)
353 struct itimerval itv, oitv;
354 struct itimerval *itp = &itv;
355 struct sigaction sa, osa;
356 sigset_t sm, osm;
358 timerclear(&itp->it_interval);
359 timerclear(&itp->it_value);
360 if (setitimer(ITIMER_REAL, itp, &oitv) < 0)
361 return;
363 (void)sigemptyset(&sm);
364 (void)sigaddset(&sm, SIGALRM);
365 (void)sigprocmask(SIG_BLOCK, &sm, &osm);
367 itp->it_value.tv_sec = napms/1000;
368 itp->it_value.tv_usec = ((napms%1000)*1000);
370 setsa(sa, t3000_napx);
371 (void)sigaction(SIGALRM, &sa, &osa);
373 (void)setitimer(ITIMER_REAL, itp, NULL);
375 sm = osm;
376 (void)sigdelset(&sm, SIGALRM);
378 for (ringring = 0; !ringring; )
379 (void)sigsuspend(&sm);
381 (void)sigaction(SIGALRM, &osa, NULL);
382 (void)setitimer(ITIMER_REAL, &oitv, NULL);
383 (void)sigprocmask(SIG_SETMASK, &osm, NULL);
386 static void
387 /*ARGSUSED*/
388 t3000_napx(int dummy __unused)
391 ringring = 1;