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