Home | History | Annotate | Line # | Download | only in aculib
hayes.c revision 1.7
      1 /*	$NetBSD: hayes.c,v 1.7 1997/11/22 07:28:56 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 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[] = "@(#)hayes.c	8.1 (Berkeley) 6/6/93";
     40 #endif
     41 __RCSID("$NetBSD: hayes.c,v 1.7 1997/11/22 07:28:56 lukem Exp $");
     42 #endif /* not lint */
     43 
     44 /*
     45  * Routines for calling up on a Hayes Modem
     46  * (based on the old VenTel driver).
     47  * The modem is expected to be strapped for "echo".
     48  * Also, the switches enabling the DTR and CD lines
     49  * must be set correctly.
     50  * NOTICE:
     51  * The easy way to hang up a modem is always simply to
     52  * clear the DTR signal. However, if the +++ sequence
     53  * (which switches the modem back to local mode) is sent
     54  * before modem is hung up, removal of the DTR signal
     55  * has no effect (except that it prevents the modem from
     56  * recognizing commands).
     57  * (by Helge Skrivervik, Calma Company, Sunnyvale, CA. 1984)
     58  */
     59 /*
     60  * TODO:
     61  * It is probably not a good idea to switch the modem
     62  * state between 'verbose' and terse (status messages).
     63  * This should be kicked out and we should use verbose
     64  * mode only. This would make it consistent with normal
     65  * interactive use thru the command 'tip dialer'.
     66  */
     67 #include "tip.h"
     68 
     69 #define	min(a,b)	((a < b) ? a : b)
     70 
     71 static	int timeout = 0;
     72 static	jmp_buf timeoutbuf;
     73 #define DUMBUFLEN	40
     74 static char dumbuf[DUMBUFLEN];
     75 
     76 #define	DIALING		1
     77 #define IDLE		2
     78 #define CONNECTED	3
     79 #define	FAILED		4
     80 static	int state = IDLE;
     81 
     82 static	void	error_rep __P((char));
     83 static	char	gobble __P((char *));
     84 static	void	goodbye __P((void));
     85 static	int	hay_sync __P((void));
     86 static	void	sigALRM __P((int));
     87 
     88 int
     89 hay_dialer(num, acu)
     90 	char *num;
     91 	char *acu;
     92 {
     93 	char *cp;
     94 	int connected = 0;
     95 	char dummy;
     96 	struct termios cntrl;
     97 #ifdef ACULOG
     98 	char line[80];
     99 #endif
    100 	if (hay_sync() == 0)		/* make sure we can talk to the modem */
    101 		return(0);
    102 	if (boolean(value(VERBOSE)))
    103 		printf("\ndialing...");
    104 	fflush(stdout);
    105 	tcgetattr(FD, &cntrl);
    106 	cntrl.c_cflag |= HUPCL;
    107 	tcsetattr(FD, TCSANOW, &cntrl);
    108 	tcflush(FD, TCIOFLUSH);
    109 	write(FD, "ATv0\r", 5);	/* tell modem to use short status codes */
    110 	gobble("\r");
    111 	gobble("\r");
    112 	write(FD, "ATTD", 4);	/* send dial command */
    113 	for (cp = num; *cp; cp++)
    114 		if (*cp == '=')
    115 			*cp = ',';
    116 	write(FD, num, strlen(num));
    117 	state = DIALING;
    118 	write(FD, "\r", 1);
    119 	connected = 0;
    120 	if (gobble("\r")) {
    121 		if ((dummy = gobble("01234")) != '1')
    122 			error_rep(dummy);
    123 		else
    124 			connected = 1;
    125 	}
    126 	if (connected)
    127 		state = CONNECTED;
    128 	else {
    129 		state = FAILED;
    130 		return (connected);	/* lets get out of here.. */
    131 	}
    132 	tcflush(FD, TCIOFLUSH);
    133 #ifdef ACULOG
    134 	if (timeout) {
    135 		(void)snprintf(line, sizeof line, "%d second dial timeout",
    136 			(int)number(value(DIALTIMEOUT)));
    137 		logent(value(HOST), num, "hayes", line);
    138 	}
    139 #endif
    140 	if (timeout)
    141 		hay_disconnect();	/* insurance */
    142 	return (connected);
    143 }
    144 
    145 
    146 void
    147 hay_disconnect()
    148 {
    149 
    150 	/* first hang up the modem*/
    151 #ifdef DEBUG
    152 	printf("\rdisconnecting modem....\n\r");
    153 #endif
    154 	ioctl(FD, TIOCCDTR, 0);
    155 	sleep(1);
    156 	ioctl(FD, TIOCSDTR, 0);
    157 	goodbye();
    158 }
    159 
    160 void
    161 hay_abort()
    162 {
    163 
    164 	write(FD, "\r", 1);	/* send anything to abort the call */
    165 	hay_disconnect();
    166 }
    167 
    168 static void
    169 sigALRM(dummy)
    170 	int dummy;
    171 {
    172 
    173 	printf("\07timeout waiting for reply\n\r");
    174 	timeout = 1;
    175 	longjmp(timeoutbuf, 1);
    176 }
    177 
    178 static char
    179 gobble(match)
    180 	char *match;
    181 {
    182 	char c;
    183 	sig_t f;
    184 	int i, status = 0;
    185 
    186 #if __GNUC__	/* XXX pacify gcc */
    187 	(void)&status;
    188 #endif
    189 
    190 	f = signal(SIGALRM, sigALRM);
    191 	timeout = 0;
    192 #ifdef DEBUG
    193 	printf("\ngobble: waiting for %s\n", match);
    194 #endif
    195 	do {
    196 		if (setjmp(timeoutbuf)) {
    197 			signal(SIGALRM, f);
    198 			return (0);
    199 		}
    200 		alarm(number(value(DIALTIMEOUT)));
    201 		read(FD, &c, 1);
    202 		alarm(0);
    203 		c &= 0177;
    204 #ifdef DEBUG
    205 		printf("%c 0x%x ", c, c);
    206 #endif
    207 		for (i = 0; i < strlen(match); i++)
    208 			if (c == match[i])
    209 				status = c;
    210 	} while (status == 0);
    211 	signal(SIGALRM, SIG_DFL);
    212 #ifdef DEBUG
    213 	printf("\n");
    214 #endif
    215 	return (status);
    216 }
    217 
    218 static void
    219 error_rep(c)
    220 	char c;
    221 {
    222 	printf("\n\r");
    223 	switch (c) {
    224 
    225 	case '0':
    226 		printf("OK");
    227 		break;
    228 
    229 	case '1':
    230 		printf("CONNECT");
    231 		break;
    232 
    233 	case '2':
    234 		printf("RING");
    235 		break;
    236 
    237 	case '3':
    238 		printf("NO CARRIER");
    239 		break;
    240 
    241 	case '4':
    242 		printf("ERROR in input");
    243 		break;
    244 
    245 	case '5':
    246 		printf("CONNECT 1200");
    247 		break;
    248 
    249 	default:
    250 		printf("Unknown Modem error: %c (0x%x)", c, c);
    251 	}
    252 	printf("\n\r");
    253 	return;
    254 }
    255 
    256 /*
    257  * set modem back to normal verbose status codes.
    258  */
    259 void
    260 goodbye()
    261 {
    262 	int len;
    263 	char c;
    264 
    265 	tcflush(FD, TCIOFLUSH);
    266 	if (hay_sync()) {
    267 		sleep(1);
    268 #ifndef DEBUG
    269 		tcflush(FD, TCIOFLUSH);
    270 #endif
    271 		write(FD, "ATH0\r", 5);		/* insurance */
    272 #ifndef DEBUG
    273 		c = gobble("03");
    274 		if (c != '0' && c != '3') {
    275 			printf("cannot hang up modem\n\r");
    276 			printf("please use 'tip dialer' to make sure the line is hung up\n\r");
    277 		}
    278 #endif
    279 		sleep(1);
    280 		ioctl(FD, FIONREAD, &len);
    281 #ifdef DEBUG
    282 		printf("goodbye1: len=%d -- ", len);
    283 		rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
    284 		dumbuf[rlen] = '\0';
    285 		printf("read (%d): %s\r\n", rlen, dumbuf);
    286 #endif
    287 		write(FD, "ATv1\r", 5);
    288 		sleep(1);
    289 #ifdef DEBUG
    290 		ioctl(FD, FIONREAD, &len);
    291 		printf("goodbye2: len=%d -- ", len);
    292 		rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
    293 		dumbuf[rlen] = '\0';
    294 		printf("read (%d): %s\r\n", rlen, dumbuf);
    295 #endif
    296 	}
    297 	tcflush(FD, TCIOFLUSH);
    298 	ioctl(FD, TIOCCDTR, 0);		/* clear DTR (insurance) */
    299 	close(FD);
    300 }
    301 
    302 #define MAXRETRY	5
    303 
    304 int
    305 hay_sync()
    306 {
    307 	int len, retry = 0;
    308 
    309 	while (retry++ <= MAXRETRY) {
    310 		write(FD, "AT\r", 3);
    311 		sleep(1);
    312 		ioctl(FD, FIONREAD, &len);
    313 		if (len) {
    314 			len = read(FD, dumbuf, min(len, DUMBUFLEN));
    315 			if (strchr(dumbuf, '0') ||
    316 		   	(strchr(dumbuf, 'O') && strchr(dumbuf, 'K')))
    317 				return(1);
    318 #ifdef DEBUG
    319 			dumbuf[len] = '\0';
    320 			printf("hay_sync: (\"%s\") %d\n\r", dumbuf, retry);
    321 #endif
    322 		}
    323 		ioctl(FD, TIOCCDTR, 0);
    324 		ioctl(FD, TIOCSDTR, 0);
    325 	}
    326 	printf("Cannot synchronize with hayes...\n\r");
    327 	return(0);
    328 }
    329