Home | History | Annotate | Line # | Download | only in aculib
hayes.c revision 1.3
      1  1.3  jtc /*	$NetBSD: hayes.c,v 1.3 1994/12/08 09:31:42 jtc Exp $	*/
      2  1.3  jtc 
      3  1.1  cgd /*
      4  1.3  jtc  * Copyright (c) 1983, 1993
      5  1.3  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.1  cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1  cgd  *    must display the following acknowledgement:
     17  1.1  cgd  *	This product includes software developed by the University of
     18  1.1  cgd  *	California, Berkeley and its contributors.
     19  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1  cgd  *    may be used to endorse or promote products derived from this software
     21  1.1  cgd  *    without specific prior written permission.
     22  1.1  cgd  *
     23  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  cgd  * SUCH DAMAGE.
     34  1.1  cgd  */
     35  1.1  cgd 
     36  1.1  cgd #ifndef lint
     37  1.3  jtc #if 0
     38  1.3  jtc static char sccsid[] = "@(#)hayes.c	8.1 (Berkeley) 6/6/93";
     39  1.3  jtc #endif
     40  1.3  jtc static char rcsid[] = "$NetBSD: hayes.c,v 1.3 1994/12/08 09:31:42 jtc Exp $";
     41  1.1  cgd #endif /* not lint */
     42  1.1  cgd 
     43  1.1  cgd /*
     44  1.1  cgd  * Routines for calling up on a Hayes Modem
     45  1.1  cgd  * (based on the old VenTel driver).
     46  1.1  cgd  * The modem is expected to be strapped for "echo".
     47  1.1  cgd  * Also, the switches enabling the DTR and CD lines
     48  1.1  cgd  * must be set correctly.
     49  1.1  cgd  * NOTICE:
     50  1.1  cgd  * The easy way to hang up a modem is always simply to
     51  1.1  cgd  * clear the DTR signal. However, if the +++ sequence
     52  1.1  cgd  * (which switches the modem back to local mode) is sent
     53  1.1  cgd  * before modem is hung up, removal of the DTR signal
     54  1.1  cgd  * has no effect (except that it prevents the modem from
     55  1.1  cgd  * recognizing commands).
     56  1.1  cgd  * (by Helge Skrivervik, Calma Company, Sunnyvale, CA. 1984)
     57  1.1  cgd  */
     58  1.1  cgd /*
     59  1.1  cgd  * TODO:
     60  1.1  cgd  * It is probably not a good idea to switch the modem
     61  1.1  cgd  * state between 'verbose' and terse (status messages).
     62  1.1  cgd  * This should be kicked out and we should use verbose
     63  1.1  cgd  * mode only. This would make it consistent with normal
     64  1.1  cgd  * interactive use thru the command 'tip dialer'.
     65  1.1  cgd  */
     66  1.1  cgd #include "tip.h"
     67  1.1  cgd 
     68  1.1  cgd #define	min(a,b)	((a < b) ? a : b)
     69  1.1  cgd 
     70  1.1  cgd static	void sigALRM();
     71  1.1  cgd static	int timeout = 0;
     72  1.1  cgd static	jmp_buf timeoutbuf;
     73  1.1  cgd static 	char gobble();
     74  1.1  cgd #define DUMBUFLEN	40
     75  1.1  cgd static char dumbuf[DUMBUFLEN];
     76  1.1  cgd 
     77  1.1  cgd #define	DIALING		1
     78  1.1  cgd #define IDLE		2
     79  1.1  cgd #define CONNECTED	3
     80  1.1  cgd #define	FAILED		4
     81  1.1  cgd static	int state = IDLE;
     82  1.1  cgd 
     83  1.1  cgd hay_dialer(num, acu)
     84  1.1  cgd 	register char *num;
     85  1.1  cgd 	char *acu;
     86  1.1  cgd {
     87  1.1  cgd 	register char *cp;
     88  1.1  cgd 	register int connected = 0;
     89  1.1  cgd 	char dummy;
     90  1.1  cgd #ifdef ACULOG
     91  1.1  cgd 	char line[80];
     92  1.1  cgd #endif
     93  1.1  cgd 	if (hay_sync() == 0)		/* make sure we can talk to the modem */
     94  1.1  cgd 		return(0);
     95  1.1  cgd 	if (boolean(value(VERBOSE)))
     96  1.1  cgd 		printf("\ndialing...");
     97  1.1  cgd 	fflush(stdout);
     98  1.1  cgd 	ioctl(FD, TIOCHPCL, 0);
     99  1.1  cgd 	ioctl(FD, TIOCFLUSH, 0);	/* get rid of garbage */
    100  1.1  cgd 	write(FD, "ATv0\r", 5);	/* tell modem to use short status codes */
    101  1.1  cgd 	gobble("\r");
    102  1.1  cgd 	gobble("\r");
    103  1.1  cgd 	write(FD, "ATTD", 4);	/* send dial command */
    104  1.1  cgd 	write(FD, num, strlen(num));
    105  1.1  cgd 	state = DIALING;
    106  1.1  cgd 	write(FD, "\r", 1);
    107  1.1  cgd 	connected = 0;
    108  1.1  cgd 	if (gobble("\r")) {
    109  1.1  cgd 		if ((dummy = gobble("01234")) != '1')
    110  1.1  cgd 			error_rep(dummy);
    111  1.1  cgd 		else
    112  1.1  cgd 			connected = 1;
    113  1.1  cgd 	}
    114  1.1  cgd 	if (connected)
    115  1.1  cgd 		state = CONNECTED;
    116  1.1  cgd 	else {
    117  1.1  cgd 		state = FAILED;
    118  1.1  cgd 		return (connected);	/* lets get out of here.. */
    119  1.1  cgd 	}
    120  1.1  cgd 	ioctl(FD, TIOCFLUSH, 0);
    121  1.1  cgd #ifdef ACULOG
    122  1.1  cgd 	if (timeout) {
    123  1.1  cgd 		sprintf(line, "%d second dial timeout",
    124  1.1  cgd 			number(value(DIALTIMEOUT)));
    125  1.1  cgd 		logent(value(HOST), num, "hayes", line);
    126  1.1  cgd 	}
    127  1.1  cgd #endif
    128  1.1  cgd 	if (timeout)
    129  1.1  cgd 		hay_disconnect();	/* insurance */
    130  1.1  cgd 	return (connected);
    131  1.1  cgd }
    132  1.1  cgd 
    133  1.1  cgd 
    134  1.1  cgd hay_disconnect()
    135  1.1  cgd {
    136  1.1  cgd 	char c;
    137  1.1  cgd 	int len, rlen;
    138  1.1  cgd 
    139  1.1  cgd 	/* first hang up the modem*/
    140  1.1  cgd #ifdef DEBUG
    141  1.1  cgd 	printf("\rdisconnecting modem....\n\r");
    142  1.1  cgd #endif
    143  1.1  cgd 	ioctl(FD, TIOCCDTR, 0);
    144  1.1  cgd 	sleep(1);
    145  1.1  cgd 	ioctl(FD, TIOCSDTR, 0);
    146  1.1  cgd 	goodbye();
    147  1.1  cgd }
    148  1.1  cgd 
    149  1.1  cgd hay_abort()
    150  1.1  cgd {
    151  1.1  cgd 
    152  1.1  cgd 	char c;
    153  1.1  cgd 
    154  1.1  cgd 	write(FD, "\r", 1);	/* send anything to abort the call */
    155  1.1  cgd 	hay_disconnect();
    156  1.1  cgd }
    157  1.1  cgd 
    158  1.1  cgd static void
    159  1.1  cgd sigALRM()
    160  1.1  cgd {
    161  1.1  cgd 
    162  1.1  cgd 	printf("\07timeout waiting for reply\n\r");
    163  1.1  cgd 	timeout = 1;
    164  1.1  cgd 	longjmp(timeoutbuf, 1);
    165  1.1  cgd }
    166  1.1  cgd 
    167  1.1  cgd static char
    168  1.1  cgd gobble(match)
    169  1.1  cgd 	register char *match;
    170  1.1  cgd {
    171  1.1  cgd 	char c;
    172  1.1  cgd 	sig_t f;
    173  1.1  cgd 	int i, status = 0;
    174  1.1  cgd 
    175  1.1  cgd 	f = signal(SIGALRM, sigALRM);
    176  1.1  cgd 	timeout = 0;
    177  1.1  cgd #ifdef DEBUG
    178  1.1  cgd 	printf("\ngobble: waiting for %s\n", match);
    179  1.1  cgd #endif
    180  1.1  cgd 	do {
    181  1.1  cgd 		if (setjmp(timeoutbuf)) {
    182  1.1  cgd 			signal(SIGALRM, f);
    183  1.1  cgd 			return (0);
    184  1.1  cgd 		}
    185  1.1  cgd 		alarm(number(value(DIALTIMEOUT)));
    186  1.1  cgd 		read(FD, &c, 1);
    187  1.1  cgd 		alarm(0);
    188  1.1  cgd 		c &= 0177;
    189  1.1  cgd #ifdef DEBUG
    190  1.1  cgd 		printf("%c 0x%x ", c, c);
    191  1.1  cgd #endif
    192  1.1  cgd 		for (i = 0; i < strlen(match); i++)
    193  1.1  cgd 			if (c == match[i])
    194  1.1  cgd 				status = c;
    195  1.1  cgd 	} while (status == 0);
    196  1.1  cgd 	signal(SIGALRM, SIG_DFL);
    197  1.1  cgd #ifdef DEBUG
    198  1.1  cgd 	printf("\n");
    199  1.1  cgd #endif
    200  1.1  cgd 	return (status);
    201  1.1  cgd }
    202  1.1  cgd 
    203  1.1  cgd error_rep(c)
    204  1.1  cgd 	register char c;
    205  1.1  cgd {
    206  1.1  cgd 	printf("\n\r");
    207  1.1  cgd 	switch (c) {
    208  1.1  cgd 
    209  1.1  cgd 	case '0':
    210  1.1  cgd 		printf("OK");
    211  1.1  cgd 		break;
    212  1.1  cgd 
    213  1.1  cgd 	case '1':
    214  1.1  cgd 		printf("CONNECT");
    215  1.1  cgd 		break;
    216  1.1  cgd 
    217  1.1  cgd 	case '2':
    218  1.1  cgd 		printf("RING");
    219  1.1  cgd 		break;
    220  1.1  cgd 
    221  1.1  cgd 	case '3':
    222  1.1  cgd 		printf("NO CARRIER");
    223  1.1  cgd 		break;
    224  1.1  cgd 
    225  1.1  cgd 	case '4':
    226  1.1  cgd 		printf("ERROR in input");
    227  1.1  cgd 		break;
    228  1.1  cgd 
    229  1.1  cgd 	case '5':
    230  1.1  cgd 		printf("CONNECT 1200");
    231  1.1  cgd 		break;
    232  1.1  cgd 
    233  1.1  cgd 	default:
    234  1.1  cgd 		printf("Unknown Modem error: %c (0x%x)", c, c);
    235  1.1  cgd 	}
    236  1.1  cgd 	printf("\n\r");
    237  1.1  cgd 	return;
    238  1.1  cgd }
    239  1.1  cgd 
    240  1.1  cgd /*
    241  1.1  cgd  * set modem back to normal verbose status codes.
    242  1.1  cgd  */
    243  1.1  cgd goodbye()
    244  1.1  cgd {
    245  1.1  cgd 	int len, rlen;
    246  1.1  cgd 	char c;
    247  1.1  cgd 
    248  1.1  cgd 	ioctl(FD, TIOCFLUSH, &len);	/* get rid of trash */
    249  1.1  cgd 	if (hay_sync()) {
    250  1.1  cgd 		sleep(1);
    251  1.1  cgd #ifndef DEBUG
    252  1.1  cgd 		ioctl(FD, TIOCFLUSH, 0);
    253  1.1  cgd #endif
    254  1.1  cgd 		write(FD, "ATH0\r", 5);		/* insurance */
    255  1.1  cgd #ifndef DEBUG
    256  1.1  cgd 		c = gobble("03");
    257  1.1  cgd 		if (c != '0' && c != '3') {
    258  1.1  cgd 			printf("cannot hang up modem\n\r");
    259  1.1  cgd 			printf("please use 'tip dialer' to make sure the line is hung up\n\r");
    260  1.1  cgd 		}
    261  1.1  cgd #endif
    262  1.1  cgd 		sleep(1);
    263  1.1  cgd 		ioctl(FD, FIONREAD, &len);
    264  1.1  cgd #ifdef DEBUG
    265  1.1  cgd 		printf("goodbye1: len=%d -- ", len);
    266  1.1  cgd 		rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
    267  1.1  cgd 		dumbuf[rlen] = '\0';
    268  1.1  cgd 		printf("read (%d): %s\r\n", rlen, dumbuf);
    269  1.1  cgd #endif
    270  1.1  cgd 		write(FD, "ATv1\r", 5);
    271  1.1  cgd 		sleep(1);
    272  1.1  cgd #ifdef DEBUG
    273  1.1  cgd 		ioctl(FD, FIONREAD, &len);
    274  1.1  cgd 		printf("goodbye2: len=%d -- ", len);
    275  1.1  cgd 		rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
    276  1.1  cgd 		dumbuf[rlen] = '\0';
    277  1.1  cgd 		printf("read (%d): %s\r\n", rlen, dumbuf);
    278  1.1  cgd #endif
    279  1.1  cgd 	}
    280  1.1  cgd 	ioctl(FD, TIOCFLUSH, 0);	/* clear the input buffer */
    281  1.1  cgd 	ioctl(FD, TIOCCDTR, 0);		/* clear DTR (insurance) */
    282  1.1  cgd 	close(FD);
    283  1.1  cgd }
    284  1.1  cgd 
    285  1.1  cgd #define MAXRETRY	5
    286  1.1  cgd 
    287  1.1  cgd hay_sync()
    288  1.1  cgd {
    289  1.1  cgd 	int len, retry = 0;
    290  1.1  cgd 
    291  1.1  cgd 	while (retry++ <= MAXRETRY) {
    292  1.1  cgd 		write(FD, "AT\r", 3);
    293  1.1  cgd 		sleep(1);
    294  1.1  cgd 		ioctl(FD, FIONREAD, &len);
    295  1.1  cgd 		if (len) {
    296  1.1  cgd 			len = read(FD, dumbuf, min(len, DUMBUFLEN));
    297  1.1  cgd 			if (index(dumbuf, '0') ||
    298  1.1  cgd 		   	(index(dumbuf, 'O') && index(dumbuf, 'K')))
    299  1.1  cgd 				return(1);
    300  1.1  cgd #ifdef DEBUG
    301  1.1  cgd 			dumbuf[len] = '\0';
    302  1.1  cgd 			printf("hay_sync: (\"%s\") %d\n\r", dumbuf, retry);
    303  1.1  cgd #endif
    304  1.1  cgd 		}
    305  1.1  cgd 		ioctl(FD, TIOCCDTR, 0);
    306  1.1  cgd 		ioctl(FD, TIOCSDTR, 0);
    307  1.1  cgd 	}
    308  1.1  cgd 	printf("Cannot synchronize with hayes...\n\r");
    309  1.1  cgd 	return(0);
    310  1.1  cgd }
    311