1 /* $NetBSD: t3000.c,v 1.13 2006/04/03 02:25:27 perry Exp $ */
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
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
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)t3000.c 8.1 (Berkeley) 6/6/93";
37 __RCSID("$NetBSD: t3000.c,v 1.13 2006/04/03 02:25:27 perry Exp $");
41 * Routines for calling up on a Telebit T3000 modem.
42 * Derived from Courier driver.
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);
61 t3000_dialer(char *num
, char *acu
)
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
);
77 (void)printf("can't synchronize with t3000\n");
80 t3000_write(FD
, "AT E0\r", 6); /* turn off echoing */
83 if (boolean(value(VERBOSE
)))
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"))
91 t3000_write(FD
, "AT D", 4);
92 for (cp
= num
; *cp
; cp
++)
95 t3000_write(FD
, num
, (int)strlen(num
));
96 t3000_write(FD
, "\r", 1);
97 connected
= t3000_connect();
104 t3000_disconnect(void)
106 /* first hang up the modem*/
107 (void)ioctl(FD
, TIOCCDTR
, 0);
109 (void)ioctl(FD
, TIOCSDTR
, 0);
110 (void)t3000_sync(); /* reset */
117 t3000_write(FD
, "\r", 1); /* send anything to abort the call */
123 sigALRM(int dummy __unused
)
125 (void)printf("\07timeout waiting for reply\n");
127 longjmp(timeoutbuf
, 1);
131 t3000_swallow(const char * volatile match
)
136 f
= signal(SIGALRM
, sigALRM
);
140 (void)signal(SIGALRM
, f
);
143 if (setjmp(timeoutbuf
)) {
144 (void)signal(SIGALRM
, f
);
147 (void)alarm((unsigned)number(value(DIALTIMEOUT
)));
148 (void)read(FD
, &c
, 1);
152 if (boolean(value(VERBOSE
)))
155 } while (c
== *match
++);
157 if (boolean(value(VERBOSE
)))
158 (void)fflush(stdout
);
160 (void)signal(SIGALRM
, SIG_DFL
);
164 #ifndef B19200 /* XXX */
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
},
198 struct tbaud_msg
*bm
;
201 if (t3000_swallow("\r\n") == 0)
203 f
= signal(SIGALRM
, sigALRM
);
205 (void)memset(dialer_buf
, 0, sizeof(dialer_buf
));
207 for (nc
= 0, nl
= sizeof(dialer_buf
)-1 ; nl
> 0 ; nc
++, nl
--) {
208 if (setjmp(timeoutbuf
))
210 (void)alarm((unsigned)number(value(DIALTIMEOUT
)));
217 if (t3000_swallow("\n") == 0)
221 if (strcmp(dialer_buf
, "RINGING") == 0 &&
222 boolean(value(VERBOSE
))) {
224 (void)printf("%s\r\n", dialer_buf
);
228 if (strncmp(dialer_buf
, "CONNECT",
229 sizeof("CONNECT")-1) != 0)
231 for (bm
= tbaud_msg
; bm
->msg
; bm
++)
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
);
242 if (boolean(value(VERBOSE
)))
243 (void)printf("%s\r\n", dialer_buf
);
251 if (boolean(value(VERBOSE
)))
255 (void)printf("%s\r\n", dialer_buf
);
256 (void)signal(SIGALRM
, f
);
261 * This convoluted piece of code attempts to get
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
));
276 (void)ioctl(FD
, FIONREAD
, &len
);
278 if (len
== 0) len
= 1;
281 len
= read(FD
, buf
, sizeof(buf
));
284 (void)printf("t3000_sync: (\"%s\")\n\r", buf
);
286 if (strchr(buf
, '0') ||
287 (strchr(buf
, 'O') && strchr(buf
, 'K')))
291 * If not strapped for DTR control,
292 * try to get command mode.
295 t3000_write(FD
, "+++", 3);
298 * Toggle DTR to force anyone off that might have left
299 * the modem connected.
301 (void)ioctl(FD
, TIOCCDTR
, 0);
303 (void)ioctl(FD
, TIOCSDTR
, 0);
305 t3000_write(FD
, "\rAT Z\r", 6);
310 t3000_write(int fd
, const char *cp
, int n
)
314 if (boolean(value(VERBOSE
)))
315 (void)write(1, cp
, n
);
319 for ( ; n
-- ; cp
++) {
320 (void)write(fd
, cp
, 1);
327 t3000_verbose_read(void)
332 if (ioctl(FD
, FIONREAD
, &n
) < 0)
336 if (read(FD
, buf
, n
) != n
)
338 (void)write(1, buf
, n
);
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 */
353 struct itimerval itv
, oitv
;
354 struct itimerval
*itp
= &itv
;
355 struct sigaction sa
, osa
;
358 timerclear(&itp
->it_interval
);
359 timerclear(&itp
->it_value
);
360 if (setitimer(ITIMER_REAL
, itp
, &oitv
) < 0)
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
);
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
);
388 t3000_napx(int dummy __unused
)