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