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