Home | History | Annotate | Line # | Download | only in aculib
t3000.c revision 1.14
      1 /*	$NetBSD: t3000.c,v 1.14 2006/12/14 14:18:04 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      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.
     18  *
     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.
     30  */
     31 
     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.14 2006/12/14 14:18:04 christos Exp $");
     38 #endif /* not lint */
     39 
     40 /*
     41  * Routines for calling up on a Telebit T3000 modem.
     42  * Derived from Courier driver.
     43  */
     44 #include "tip.h"
     45 
     46 #define	MAXRETRY	5
     47 
     48 static	int timeout = 0;
     49 static	int connected = 0;
     50 static	jmp_buf timeoutbuf;
     51 
     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);
     59 
     60 int
     61 t3000_dialer(char *num, char *acu)
     62 {
     63 	char *cp;
     64 	struct termios cntrl;
     65 
     66 	if (boolean(value(VERBOSE)))
     67 		printf("Using \"%s\"\n", acu);
     68 
     69 	tcgetattr(FD, &cntrl);
     70 	cntrl.c_cflag |= HUPCL;
     71 	tcsetattr(FD, TCSANOW, &cntrl);
     72 	/*
     73 	 * Get in synch.
     74 	 */
     75 	if (!t3000_sync()) {
     76 badsynch:
     77 		printf("can't synchronize with t3000\n");
     78 		return (0);
     79 	}
     80 	t3000_write(FD, "AT E0\r", 6);	/* turn off echoing */
     81 	sleep(1);
     82 #ifdef DEBUG
     83 	if (boolean(value(VERBOSE)))
     84 		t3000_verbose_read();
     85 #endif
     86 	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 	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, strlen(num));
     96 	t3000_write(FD, "\r", 1);
     97 	connected = t3000_connect();
     98 	if (timeout)
     99 		t3000_disconnect();
    100 	return (connected);
    101 }
    102 
    103 void
    104 t3000_disconnect(void)
    105 {
    106 	 /* first hang up the modem*/
    107 	ioctl(FD, TIOCCDTR, 0);
    108 	sleep(1);
    109 	ioctl(FD, TIOCSDTR, 0);
    110 	t3000_sync();				/* reset */
    111 	close(FD);
    112 }
    113 
    114 void
    115 t3000_abort(void)
    116 {
    117 	t3000_write(FD, "\r", 1);	/* send anything to abort the call */
    118 	t3000_disconnect();
    119 }
    120 
    121 static void
    122 sigALRM(int dummy)
    123 {
    124 	printf("\07timeout waiting for reply\n");
    125 	timeout = 1;
    126 	longjmp(timeoutbuf, 1);
    127 }
    128 
    129 static int
    130 t3000_swallow(const char * volatile match)
    131 {
    132 	sig_t f;
    133 	char c;
    134 
    135 	f = signal(SIGALRM, sigALRM);
    136 	timeout = 0;
    137 	do {
    138 		if (*match =='\0') {
    139 			signal(SIGALRM, f);
    140 			return (1);
    141 		}
    142 		if (setjmp(timeoutbuf)) {
    143 			signal(SIGALRM, f);
    144 			return (0);
    145 		}
    146 		alarm(number(value(DIALTIMEOUT)));
    147 		read(FD, &c, 1);
    148 		alarm(0);
    149 		c &= 0177;
    150 #ifdef DEBUG
    151 		if (boolean(value(VERBOSE)))
    152 			putchar(c);
    153 #endif
    154 	} while (c == *match++);
    155 #ifdef DEBUG
    156 	if (boolean(value(VERBOSE)))
    157 		fflush(stdout);
    158 #endif
    159 	signal(SIGALRM, SIG_DFL);
    160 	return (0);
    161 }
    162 
    163 #ifndef B19200		/* XXX */
    164 #define	B19200	EXTA
    165 #define	B38400	EXTB
    166 #endif
    167 
    168 struct tbaud_msg {
    169 	const char *msg;
    170 	int baud;
    171 	int baud2;
    172 } tbaud_msg[] = {
    173 	{ "",		B300,	0 },
    174 	{ " 1200",	B1200,	0 },
    175 	{ " 2400",	B2400,	0 },
    176 	{ " 4800",	B4800,	0 },
    177 	{ " 9600",	B9600,	0 },
    178 	{ " 14400",	B19200,	B9600 },
    179 	{ " 19200",	B19200,	B9600 },
    180 	{ " 38400",	B38400,	B9600 },
    181 	{ " 57600",	B38400,	B9600 },
    182 	{ " 7512",	B9600,	0 },
    183 	{ " 1275",	B2400,	0 },
    184 	{ " 7200",	B9600,	0 },
    185 	{ " 12000",	B19200,	B9600 },
    186 	{ 0,		0,	0 },
    187 };
    188 
    189 static int
    190 t3000_connect(void)
    191 {
    192 	char c;
    193 	int volatile nc;
    194 	int volatile nl;
    195 	int n;
    196 	char dialer_buf[64];
    197 	struct tbaud_msg *bm;
    198 	sig_t f;
    199 
    200 	if (t3000_swallow("\r\n") == 0)
    201 		return (0);
    202 	f = signal(SIGALRM, sigALRM);
    203 again:
    204 	memset(dialer_buf, 0, sizeof(dialer_buf));
    205 	timeout = 0;
    206 	for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) {
    207 		if (setjmp(timeoutbuf))
    208 			break;
    209 		alarm(number(value(DIALTIMEOUT)));
    210 		n = read(FD, &c, 1);
    211 		alarm(0);
    212 		if (n <= 0)
    213 			break;
    214 		c &= 0x7f;
    215 		if (c == '\r') {
    216 			if (t3000_swallow("\n") == 0)
    217 				break;
    218 			if (!dialer_buf[0])
    219 				goto again;
    220 			if (strcmp(dialer_buf, "RINGING") == 0 &&
    221 			    boolean(value(VERBOSE))) {
    222 #ifdef DEBUG
    223 				printf("%s\r\n", dialer_buf);
    224 #endif
    225 				goto again;
    226 			}
    227 			if (strncmp(dialer_buf, "CONNECT",
    228 				    sizeof("CONNECT")-1) != 0)
    229 				break;
    230 			for (bm = tbaud_msg ; bm->msg ; bm++)
    231 				if (strcmp(bm->msg,
    232 				    dialer_buf+sizeof("CONNECT")-1) == 0) {
    233 					struct termios	cntrl;
    234 
    235 					tcgetattr(FD, &cntrl);
    236 					cfsetospeed(&cntrl, bm->baud);
    237 					cfsetispeed(&cntrl, bm->baud);
    238 					tcsetattr(FD, TCSAFLUSH, &cntrl);
    239 					signal(SIGALRM, f);
    240 #ifdef DEBUG
    241 					if (boolean(value(VERBOSE)))
    242 						printf("%s\r\n", dialer_buf);
    243 #endif
    244 					return (1);
    245 				}
    246 			break;
    247 		}
    248 		dialer_buf[nc] = c;
    249 #ifdef notdef
    250 		if (boolean(value(VERBOSE)))
    251 			putchar(c);
    252 #endif
    253 	}
    254 	printf("%s\r\n", dialer_buf);
    255 	signal(SIGALRM, f);
    256 	return (0);
    257 }
    258 
    259 /*
    260  * This convoluted piece of code attempts to get
    261  * the t3000 in sync.
    262  */
    263 static int
    264 t3000_sync(void)
    265 {
    266 	int already = 0;
    267 	int len;
    268 	char buf[40];
    269 
    270 	while (already++ < MAXRETRY) {
    271 		tcflush(FD, TCIOFLUSH);
    272 		t3000_write(FD, "\rAT Z\r", 6);	/* reset modem */
    273 		memset(buf, 0, sizeof(buf));
    274 		sleep(2);
    275 		ioctl(FD, FIONREAD, &len);
    276 #if 1
    277 if (len == 0) len = 1;
    278 #endif
    279 		if (len) {
    280 			len = read(FD, buf, sizeof(buf));
    281 #ifdef DEBUG
    282 			buf[len] = '\0';
    283 			printf("t3000_sync: (\"%s\")\n\r", buf);
    284 #endif
    285 			if (strchr(buf, '0') ||
    286 		   	   (strchr(buf, 'O') && strchr(buf, 'K')))
    287 				return(1);
    288 		}
    289 		/*
    290 		 * If not strapped for DTR control,
    291 		 * try to get command mode.
    292 		 */
    293 		sleep(1);
    294 		t3000_write(FD, "+++", 3);
    295 		sleep(1);
    296 		/*
    297 		 * Toggle DTR to force anyone off that might have left
    298 		 * the modem connected.
    299 		 */
    300 		ioctl(FD, TIOCCDTR, 0);
    301 		sleep(1);
    302 		ioctl(FD, TIOCSDTR, 0);
    303 	}
    304 	t3000_write(FD, "\rAT Z\r", 6);
    305 	return (0);
    306 }
    307 
    308 static void
    309 t3000_write(int fd, const char *cp, int n)
    310 {
    311 
    312 #ifdef notdef
    313 	if (boolean(value(VERBOSE)))
    314 		write(1, cp, n);
    315 #endif
    316 	tcdrain(fd);
    317 	t3000_nap();
    318 	for ( ; n-- ; cp++) {
    319 		write(fd, cp, 1);
    320 		tcdrain(fd);
    321 		t3000_nap();
    322 	}
    323 }
    324 
    325 #ifdef DEBUG
    326 t3000_verbose_read(void)
    327 {
    328 	int n = 0;
    329 	char buf[BUFSIZ];
    330 
    331 	if (ioctl(FD, FIONREAD, &n) < 0)
    332 		return;
    333 	if (n <= 0)
    334 		return;
    335 	if (read(FD, buf, n) != n)
    336 		return;
    337 	write(1, buf, n);
    338 }
    339 #endif
    340 
    341 #define setsa(sa, a) \
    342 	sa.sa_handler = a; sigemptyset(&sa.sa_mask); sa.sa_flags = 0
    343 
    344 static int napms = 50; /* Give the t3000 50 milliseconds between characters */
    345 
    346 static int ringring;
    347 
    348 void
    349 t3000_nap(void)
    350 {
    351 
    352 	struct itimerval itv, oitv;
    353 	struct itimerval *itp = &itv;
    354 	struct sigaction sa, osa;
    355 	sigset_t sm, osm;
    356 
    357 	timerclear(&itp->it_interval);
    358 	timerclear(&itp->it_value);
    359 	if (setitimer(ITIMER_REAL, itp, &oitv) < 0)
    360 		return;
    361 
    362 	sigemptyset(&sm);
    363 	sigaddset(&sm, SIGALRM);
    364 	(void)sigprocmask(SIG_BLOCK, &sm, &osm);
    365 
    366 	itp->it_value.tv_sec = napms/1000;
    367 	itp->it_value.tv_usec = ((napms%1000)*1000);
    368 
    369 	setsa(sa, t3000_napx);
    370 	(void)sigaction(SIGALRM, &sa, &osa);
    371 
    372 	(void)setitimer(ITIMER_REAL, itp, NULL);
    373 
    374 	sm = osm;
    375 	sigdelset(&sm, SIGALRM);
    376 
    377 	for (ringring = 0; !ringring; )
    378 		sigsuspend(&sm);
    379 
    380 	(void)sigaction(SIGALRM, &osa, NULL);
    381 	(void)setitimer(ITIMER_REAL, &oitv, NULL);
    382 	(void)sigprocmask(SIG_SETMASK, &osm, NULL);
    383 }
    384 
    385 static void
    386 t3000_napx(int dummy)
    387 {
    388 
    389         ringring = 1;
    390 }
    391