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