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