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