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