Home | History | Annotate | Line # | Download | only in aculib
courier.c revision 1.8
      1 /*	$NetBSD: courier.c,v 1.8 1997/11/22 07:28:53 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1986, 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)courier.c	8.1 (Berkeley) 6/6/93";
     40 #endif
     41 __RCSID("$NetBSD: courier.c,v 1.8 1997/11/22 07:28:53 lukem Exp $");
     42 #endif /* not lint */
     43 
     44 /*
     45  * Routines for calling up on a Courier modem.
     46  * Derived from Hayes driver.
     47  */
     48 #include "tip.h"
     49 
     50 #define	MAXRETRY	5
     51 
     52 static	int timeout = 0;
     53 static	int connected = 0;
     54 static	jmp_buf timeoutbuf;
     55 
     56 static	int	cour_connect __P((void));
     57 static	void	cour_nap __P((void));
     58 static	void	cour_napx __P((int));
     59 static	int	cour_swallow __P((char *));
     60 static	int	coursync __P((void));
     61 #ifdef DEBUG
     62 static	void	cour_verbose_read __P((void));
     63 #endif
     64 static	void	cour_write __P((int, char *, int));
     65 static	void	sigALRM __P((int));
     66 
     67 int
     68 cour_dialer(num, acu)
     69 	char *num;
     70 	char *acu;
     71 {
     72 	char *cp;
     73 #ifdef ACULOG
     74 	char line[80];
     75 #endif
     76 	struct termios cntrl;
     77 
     78 	if (boolean(value(VERBOSE)))
     79 		printf("Using \"%s\"\n", acu);
     80 
     81 	tcgetattr(FD, &cntrl);
     82 	cntrl.c_cflag |= HUPCL;
     83 	tcsetattr(FD, TCSAFLUSH, &cntrl);
     84 	/*
     85 	 * Get in synch.
     86 	 */
     87 	if (!coursync()) {
     88 badsynch:
     89 		printf("can't synchronize with courier\n");
     90 #ifdef ACULOG
     91 		logent(value(HOST), num, "courier", "can't synch up");
     92 #endif
     93 		return (0);
     94 	}
     95 	cour_write(FD, "AT E0\r", 6);	/* turn off echoing */
     96 	sleep(1);
     97 #ifdef DEBUG
     98 	if (boolean(value(VERBOSE)))
     99 		cour_verbose_read();
    100 #endif
    101 	tcflush(FD, TCIOFLUSH);
    102 	cour_write(FD, "AT C1 E0 H0 Q0 X6 V1\r", 21);
    103 	if (!cour_swallow("\r\nOK\r\n"))
    104 		goto badsynch;
    105 	fflush(stdout);
    106 	cour_write(FD, "AT D", 4);
    107 	for (cp = num; *cp; cp++)
    108 		if (*cp == '=')
    109 			*cp = ',';
    110 	cour_write(FD, num, strlen(num));
    111 	cour_write(FD, "\r", 1);
    112 	connected = cour_connect();
    113 #ifdef ACULOG
    114 	if (timeout) {
    115 		(void)snprintf(line, sizeof line, "%d second dial timeout",
    116 			(int)number(value(DIALTIMEOUT)));
    117 		logent(value(HOST), num, "cour", line);
    118 	}
    119 #endif
    120 	if (timeout)
    121 		cour_disconnect();
    122 	return (connected);
    123 }
    124 
    125 void
    126 cour_disconnect()
    127 {
    128 	 /* first hang up the modem*/
    129 	ioctl(FD, TIOCCDTR, 0);
    130 	sleep(1);
    131 	ioctl(FD, TIOCSDTR, 0);
    132 	coursync();				/* reset */
    133 	close(FD);
    134 }
    135 
    136 void
    137 cour_abort()
    138 {
    139 	cour_write(FD, "\r", 1);	/* send anything to abort the call */
    140 	cour_disconnect();
    141 }
    142 
    143 static void
    144 sigALRM(dummy)
    145 	int dummy;
    146 {
    147 	printf("\07timeout waiting for reply\n");
    148 	timeout = 1;
    149 	longjmp(timeoutbuf, 1);
    150 }
    151 
    152 static int
    153 cour_swallow(match)
    154 	char *match;
    155 {
    156 	sig_t f;
    157 	char c;
    158 
    159 #if __GNUC__	/* XXX pacify gcc */
    160 	(void)&match;
    161 #endif
    162 
    163 	f = signal(SIGALRM, sigALRM);
    164 	timeout = 0;
    165 	do {
    166 		if (*match =='\0') {
    167 			signal(SIGALRM, f);
    168 			return (1);
    169 		}
    170 		if (setjmp(timeoutbuf)) {
    171 			signal(SIGALRM, f);
    172 			return (0);
    173 		}
    174 		alarm(number(value(DIALTIMEOUT)));
    175 		read(FD, &c, 1);
    176 		alarm(0);
    177 		c &= 0177;
    178 #ifdef DEBUG
    179 		if (boolean(value(VERBOSE)))
    180 			putchar(c);
    181 #endif
    182 	} while (c == *match++);
    183 #ifdef DEBUG
    184 	if (boolean(value(VERBOSE)))
    185 		fflush(stdout);
    186 #endif
    187 	signal(SIGALRM, SIG_DFL);
    188 	return (0);
    189 }
    190 
    191 struct baud_msg {
    192 	char *msg;
    193 	int baud;
    194 } baud_msg[] = {
    195 	{ "",		B300 },
    196 	{ " 1200",	B1200 },
    197 	{ " 2400",	B2400 },
    198 	{ " 9600",	B9600 },
    199 	{ " 9600/ARQ",	B9600 },
    200 	{ 0,		0 }
    201 };
    202 
    203 static int
    204 cour_connect()
    205 {
    206 	char c;
    207 	int nc, nl, n;
    208 	char dialer_buf[64];
    209 	struct baud_msg *bm;
    210 	sig_t f;
    211 
    212 #if __GNUC__	/* XXX pacify gcc */
    213 	(void)&nc;
    214 	(void)&nl;
    215 #endif
    216 
    217 	if (cour_swallow("\r\n") == 0)
    218 		return (0);
    219 	f = signal(SIGALRM, sigALRM);
    220 again:
    221 	nc = 0; nl = sizeof(dialer_buf)-1;
    222 	memset(dialer_buf, 0, sizeof(dialer_buf));
    223 	timeout = 0;
    224 	for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) {
    225 		if (setjmp(timeoutbuf))
    226 			break;
    227 		alarm(number(value(DIALTIMEOUT)));
    228 		n = read(FD, &c, 1);
    229 		alarm(0);
    230 		if (n <= 0)
    231 			break;
    232 		c &= 0x7f;
    233 		if (c == '\r') {
    234 			if (cour_swallow("\n") == 0)
    235 				break;
    236 			if (!dialer_buf[0])
    237 				goto again;
    238 			if (strcmp(dialer_buf, "RINGING") == 0 &&
    239 			    boolean(value(VERBOSE))) {
    240 #ifdef DEBUG
    241 				printf("%s\r\n", dialer_buf);
    242 #endif
    243 				goto again;
    244 			}
    245 			if (strncmp(dialer_buf, "CONNECT",
    246 				    sizeof("CONNECT")-1) != 0)
    247 				break;
    248 			for (bm = baud_msg ; bm->msg ; bm++)
    249 				if (strcmp(bm->msg,
    250 				    dialer_buf+sizeof("CONNECT")-1) == 0) {
    251 					struct termios	cntrl;
    252 
    253 					tcgetattr(FD, &cntrl);
    254 					cfsetospeed(&cntrl, bm->baud);
    255 					cfsetispeed(&cntrl, bm->baud);
    256 					tcsetattr(FD, TCSAFLUSH, &cntrl);
    257 					signal(SIGALRM, f);
    258 #ifdef DEBUG
    259 					if (boolean(value(VERBOSE)))
    260 						printf("%s\r\n", dialer_buf);
    261 #endif
    262 					return (1);
    263 				}
    264 			break;
    265 		}
    266 		dialer_buf[nc] = c;
    267 #ifdef notdef
    268 		if (boolean(value(VERBOSE)))
    269 			putchar(c);
    270 #endif
    271 	}
    272 	printf("%s\r\n", dialer_buf);
    273 	signal(SIGALRM, f);
    274 	return (0);
    275 }
    276 
    277 /*
    278  * This convoluted piece of code attempts to get
    279  * the courier in sync.
    280  */
    281 static int
    282 coursync()
    283 {
    284 	int already = 0;
    285 	int len;
    286 	char buf[40];
    287 
    288 	while (already++ < MAXRETRY) {
    289 		tcflush(FD, TCIOFLUSH);
    290 		cour_write(FD, "\rAT Z\r", 6);	/* reset modem */
    291 		memset(buf, 0, sizeof(buf));
    292 		sleep(1);
    293 		ioctl(FD, FIONREAD, &len);
    294 		if (len) {
    295 			len = read(FD, buf, sizeof(buf));
    296 #ifdef DEBUG
    297 			buf[len] = '\0';
    298 			printf("coursync: (\"%s\")\n\r", buf);
    299 #endif
    300 			if (strchr(buf, '0') ||
    301 		   	   (strchr(buf, 'O') && strchr(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 		cour_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 	cour_write(FD, "\rAT Z\r", 6);
    320 	return (0);
    321 }
    322 
    323 static void
    324 cour_write(fd, cp, n)
    325 	int fd;
    326 	char *cp;
    327 	int n;
    328 {
    329 #ifdef notdef
    330 	if (boolean(value(VERBOSE)))
    331 		write(1, cp, n);
    332 #endif
    333 	tcdrain(fd);
    334 	cour_nap();
    335 	for ( ; n-- ; cp++) {
    336 		write(fd, cp, 1);
    337 		tcdrain(fd);
    338 		cour_nap();
    339 	}
    340 }
    341 
    342 #ifdef DEBUG
    343 static void
    344 cour_verbose_read()
    345 {
    346 	int n = 0;
    347 	char buf[BUFSIZ];
    348 
    349 	if (ioctl(FD, FIONREAD, &n) < 0)
    350 		return;
    351 	if (n <= 0)
    352 		return;
    353 	if (read(FD, buf, n) != n)
    354 		return;
    355 	write(1, buf, n);
    356 }
    357 #endif
    358 
    359 /*
    360  * Code stolen from /usr/src/lib/libc/gen/sleep.c
    361  */
    362 #define mask(s) (1<<((s)-1))
    363 #define setvec(vec, a) \
    364         vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0
    365 
    366 static napms = 50; /* Give the courier 50 milliseconds between characters */
    367 
    368 static int ringring;
    369 
    370 void
    371 cour_nap()
    372 {
    373 
    374 	int omask;
    375         struct itimerval itv, oitv;
    376         struct itimerval *itp = &itv;
    377         struct sigvec vec, ovec;
    378 
    379         timerclear(&itp->it_interval);
    380         timerclear(&itp->it_value);
    381         if (setitimer(ITIMER_REAL, itp, &oitv) < 0)
    382                 return;
    383         setvec(ovec, SIG_DFL);
    384         omask = sigblock(mask(SIGALRM));
    385         itp->it_value.tv_sec = napms/1000;
    386 	itp->it_value.tv_usec = ((napms%1000)*1000);
    387         setvec(vec, cour_napx);
    388         ringring = 0;
    389         (void) sigvec(SIGALRM, &vec, &ovec);
    390         (void) setitimer(ITIMER_REAL, itp, (struct itimerval *)0);
    391         while (!ringring)
    392                 sigpause(omask &~ mask(SIGALRM));
    393         (void) sigvec(SIGALRM, &ovec, (struct sigvec *)0);
    394         (void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0);
    395 	(void) sigsetmask(omask);
    396 }
    397 
    398 static void
    399 cour_napx(dummy)
    400 	int dummy;
    401 {
    402         ringring = 1;
    403 }
    404