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