xref: /netbsd-src/usr.bin/tip/aculib/hayes.c (revision f20d1d233de6f2f789af449c78fbea2bfb1a5a92)
1*f20d1d23Sjoerg /*	$NetBSD: hayes.c,v 1.17 2011/09/06 18:33:01 joerg Exp $	*/
239801cccSjtc 
361f28255Scgd /*
439801cccSjtc  * Copyright (c) 1983, 1993
539801cccSjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32e37283e1Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3439801cccSjtc #if 0
3539801cccSjtc static char sccsid[] = "@(#)hayes.c	8.1 (Berkeley) 6/6/93";
3639801cccSjtc #endif
37*f20d1d23Sjoerg __RCSID("$NetBSD: hayes.c,v 1.17 2011/09/06 18:33:01 joerg Exp $");
3861f28255Scgd #endif /* not lint */
3961f28255Scgd 
4061f28255Scgd /*
4161f28255Scgd  * Routines for calling up on a Hayes Modem
4261f28255Scgd  * (based on the old VenTel driver).
4361f28255Scgd  * The modem is expected to be strapped for "echo".
4461f28255Scgd  * Also, the switches enabling the DTR and CD lines
4561f28255Scgd  * must be set correctly.
4661f28255Scgd  * NOTICE:
4761f28255Scgd  * The easy way to hang up a modem is always simply to
4861f28255Scgd  * clear the DTR signal. However, if the +++ sequence
4961f28255Scgd  * (which switches the modem back to local mode) is sent
5061f28255Scgd  * before modem is hung up, removal of the DTR signal
5161f28255Scgd  * has no effect (except that it prevents the modem from
5261f28255Scgd  * recognizing commands).
5361f28255Scgd  * (by Helge Skrivervik, Calma Company, Sunnyvale, CA. 1984)
5461f28255Scgd  */
5561f28255Scgd /*
5661f28255Scgd  * TODO:
5761f28255Scgd  * It is probably not a good idea to switch the modem
5861f28255Scgd  * state between 'verbose' and terse (status messages).
5961f28255Scgd  * This should be kicked out and we should use verbose
6061f28255Scgd  * mode only. This would make it consistent with normal
6161f28255Scgd  * interactive use thru the command 'tip dialer'.
6261f28255Scgd  */
6361f28255Scgd #include "tip.h"
6461f28255Scgd 
6561f28255Scgd #define	min(a,b)	((a < b) ? a : b)
6661f28255Scgd 
6761f28255Scgd static	int timeout = 0;
6861f28255Scgd static	jmp_buf timeoutbuf;
6961f28255Scgd #define DUMBUFLEN	40
7061f28255Scgd static char dumbuf[DUMBUFLEN];
7161f28255Scgd 
7258c2151fSperry static	void	error_rep(char);
7358c2151fSperry static	char	gobble(const char *);
7458c2151fSperry static	void	goodbye(void);
7558c2151fSperry static	int	hay_sync(void);
76*f20d1d23Sjoerg __dead static	void	sigALRM(int);
77e37283e1Slukem 
78e37283e1Slukem int
79c4341bc3Schristos /*ARGSUSED*/
hay_dialer(char * num,char * acu __unused)80ffe34450Schristos hay_dialer(char *num, char *acu __unused)
8161f28255Scgd {
82e37283e1Slukem 	char *cp;
83e37283e1Slukem 	int connected = 0;
8461f28255Scgd 	char dummy;
85258108ceSpk 	struct termios cntrl;
86d71f683eSperry 
8761f28255Scgd 	if (hay_sync() == 0)		/* make sure we can talk to the modem */
8861f28255Scgd 		return(0);
8961f28255Scgd 	if (boolean(value(VERBOSE)))
90ffe34450Schristos 		(void)printf("\ndialing...");
91ffe34450Schristos 	(void)fflush(stdout);
92ffe34450Schristos 	(void)tcgetattr(FD, &cntrl);
93258108ceSpk 	cntrl.c_cflag |= HUPCL;
94ffe34450Schristos 	(void)tcsetattr(FD, TCSANOW, &cntrl);
95ffe34450Schristos 	(void)tcflush(FD, TCIOFLUSH);
96ffe34450Schristos 	(void)write(FD, "ATv0\r", 5);	/* tell modem to use short status codes */
97ffe34450Schristos 	(void)gobble("\r");
98ffe34450Schristos 	(void)gobble("\r");
99ffe34450Schristos 	(void)write(FD, "ATTD", 4);	/* send dial command */
100b7e69ffbScgd 	for (cp = num; *cp; cp++)
101b7e69ffbScgd 		if (*cp == '=')
102b7e69ffbScgd 			*cp = ',';
103ffe34450Schristos 	(void)write(FD, num, strlen(num));
104ffe34450Schristos 	(void)write(FD, "\r", 1);
10561f28255Scgd 	connected = 0;
10661f28255Scgd 	if (gobble("\r")) {
10761f28255Scgd 		if ((dummy = gobble("01234")) != '1')
10861f28255Scgd 			error_rep(dummy);
10961f28255Scgd 		else
11061f28255Scgd 			connected = 1;
11161f28255Scgd 	}
112c4341bc3Schristos 	if (!connected)
11361f28255Scgd 		return (connected);	/* lets get out of here.. */
114ffe34450Schristos 	(void)tcflush(FD, TCIOFLUSH);
11561f28255Scgd 	if (timeout)
11661f28255Scgd 		hay_disconnect();	/* insurance */
11761f28255Scgd 	return (connected);
11861f28255Scgd }
11961f28255Scgd 
12061f28255Scgd 
121e37283e1Slukem void
hay_disconnect(void)12258c2151fSperry hay_disconnect(void)
12361f28255Scgd {
12461f28255Scgd 
12561f28255Scgd 	/* first hang up the modem*/
12661f28255Scgd #ifdef DEBUG
127ffe34450Schristos 	(void)printf("\rdisconnecting modem....\n\r");
12861f28255Scgd #endif
129ffe34450Schristos 	(void)ioctl(FD, TIOCCDTR, 0);
130ffe34450Schristos 	(void)sleep(1);
131ffe34450Schristos 	(void)ioctl(FD, TIOCSDTR, 0);
13261f28255Scgd 	goodbye();
13361f28255Scgd }
13461f28255Scgd 
135e37283e1Slukem void
hay_abort(void)13658c2151fSperry hay_abort(void)
13761f28255Scgd {
13861f28255Scgd 
139ffe34450Schristos 	(void)write(FD, "\r", 1);	/* send anything to abort the call */
14061f28255Scgd 	hay_disconnect();
14161f28255Scgd }
14261f28255Scgd 
14361f28255Scgd static void
144c4341bc3Schristos /*ARGSUSED*/
sigALRM(int dummy __unused)145ffe34450Schristos sigALRM(int dummy __unused)
14661f28255Scgd {
14761f28255Scgd 
148ffe34450Schristos 	(void)printf("\07timeout waiting for reply\n\r");
14961f28255Scgd 	timeout = 1;
15061f28255Scgd 	longjmp(timeoutbuf, 1);
15161f28255Scgd }
15261f28255Scgd 
15361f28255Scgd static char
gobble(const char * match)15458c2151fSperry gobble(const char *match)
15561f28255Scgd {
15661f28255Scgd 	char c;
15761f28255Scgd 	sig_t f;
158c493aef5Slukem 	size_t i;
15997e81e47Schristos 	volatile int status = 0;
160e37283e1Slukem 
16161f28255Scgd 	f = signal(SIGALRM, sigALRM);
16261f28255Scgd 	timeout = 0;
16361f28255Scgd #ifdef DEBUG
164ffe34450Schristos 	(void)printf("\ngobble: waiting for %s\n", match);
16561f28255Scgd #endif
16661f28255Scgd 	do {
16761f28255Scgd 		if (setjmp(timeoutbuf)) {
168ffe34450Schristos 			(void)signal(SIGALRM, f);
16961f28255Scgd 			return (0);
17061f28255Scgd 		}
171ffe34450Schristos 		(void)alarm((unsigned int)number(value(DIALTIMEOUT)));
172ffe34450Schristos 		(void)read(FD, &c, 1);
173ffe34450Schristos 		(void)alarm(0);
17461f28255Scgd 		c &= 0177;
17561f28255Scgd #ifdef DEBUG
176ffe34450Schristos 		(void)printf("%c 0x%x ", c, c);
17761f28255Scgd #endif
17861f28255Scgd 		for (i = 0; i < strlen(match); i++)
17961f28255Scgd 			if (c == match[i])
18061f28255Scgd 				status = c;
18161f28255Scgd 	} while (status == 0);
182ffe34450Schristos 	(void)signal(SIGALRM, SIG_DFL);
18361f28255Scgd #ifdef DEBUG
184ffe34450Schristos 	(void)printf("\n");
18561f28255Scgd #endif
18661f28255Scgd 	return (status);
18761f28255Scgd }
18861f28255Scgd 
189e37283e1Slukem static void
error_rep(char c)19058c2151fSperry error_rep(char c)
19161f28255Scgd {
19297eafd50Smrg 
193ffe34450Schristos 	(void)printf("\n\r");
19461f28255Scgd 	switch (c) {
19561f28255Scgd 
19661f28255Scgd 	case '0':
197ffe34450Schristos 		(void)printf("OK");
19861f28255Scgd 		break;
19961f28255Scgd 
20061f28255Scgd 	case '1':
201ffe34450Schristos 		(void)printf("CONNECT");
20261f28255Scgd 		break;
20361f28255Scgd 
20461f28255Scgd 	case '2':
205ffe34450Schristos 		(void)printf("RING");
20661f28255Scgd 		break;
20761f28255Scgd 
20861f28255Scgd 	case '3':
209ffe34450Schristos 		(void)printf("NO CARRIER");
21061f28255Scgd 		break;
21161f28255Scgd 
21261f28255Scgd 	case '4':
213ffe34450Schristos 		(void)printf("ERROR in input");
21461f28255Scgd 		break;
21561f28255Scgd 
21661f28255Scgd 	case '5':
217ffe34450Schristos 		(void)printf("CONNECT 1200");
21861f28255Scgd 		break;
21961f28255Scgd 
22061f28255Scgd 	default:
221ffe34450Schristos 		(void)printf("Unknown Modem error: %c (0x%x)", c, c);
22261f28255Scgd 	}
223ffe34450Schristos 	(void)printf("\n\r");
22461f28255Scgd 	return;
22561f28255Scgd }
22661f28255Scgd 
22761f28255Scgd /*
22861f28255Scgd  * set modem back to normal verbose status codes.
22961f28255Scgd  */
230e37283e1Slukem void
goodbye(void)23158c2151fSperry goodbye(void)
23261f28255Scgd {
233e37283e1Slukem 	int len;
23461f28255Scgd 	char c;
23561f28255Scgd 
236ffe34450Schristos 	(void)tcflush(FD, TCIOFLUSH);
23761f28255Scgd 	if (hay_sync()) {
238ffe34450Schristos 		(void)sleep(1);
23961f28255Scgd #ifndef DEBUG
240ffe34450Schristos 		(void)tcflush(FD, TCIOFLUSH);
24161f28255Scgd #endif
242ffe34450Schristos 		(void)write(FD, "ATH0\r", 5);		/* insurance */
24361f28255Scgd #ifndef DEBUG
24461f28255Scgd 		c = gobble("03");
24561f28255Scgd 		if (c != '0' && c != '3') {
246ffe34450Schristos 			(void)printf("cannot hang up modem\n\r");
247ffe34450Schristos 			(void)printf("please use 'tip dialer' to make sure the line is hung up\n\r");
24861f28255Scgd 		}
24961f28255Scgd #endif
250ffe34450Schristos 		(void)sleep(1);
251ffe34450Schristos 		(void)ioctl(FD, FIONREAD, &len);
25261f28255Scgd #ifdef DEBUG
253ffe34450Schristos 		(void)printf("goodbye1: len=%d -- ", len);
25461f28255Scgd 		rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
25561f28255Scgd 		dumbuf[rlen] = '\0';
256ffe34450Schristos 		(void)printf("read (%d): %s\r\n", rlen, dumbuf);
25761f28255Scgd #endif
258ffe34450Schristos 		(void)write(FD, "ATv1\r", 5);
259ffe34450Schristos 		(void)sleep(1);
26061f28255Scgd #ifdef DEBUG
261ffe34450Schristos 		(void)ioctl(FD, FIONREAD, &len);
262ffe34450Schristos 		(void)printf("goodbye2: len=%d -- ", len);
26361f28255Scgd 		rlen = read(FD, dumbuf, min(len, DUMBUFLEN));
26461f28255Scgd 		dumbuf[rlen] = '\0';
265ffe34450Schristos 		(void)printf("read (%d): %s\r\n", rlen, dumbuf);
26661f28255Scgd #endif
26761f28255Scgd 	}
268ffe34450Schristos 	(void)tcflush(FD, TCIOFLUSH);
269ffe34450Schristos 	(void)ioctl(FD, TIOCCDTR, 0);		/* clear DTR (insurance) */
270ffe34450Schristos 	(void)close(FD);
27161f28255Scgd }
27261f28255Scgd 
27361f28255Scgd #define MAXRETRY	5
27461f28255Scgd 
275e37283e1Slukem int
hay_sync(void)27658c2151fSperry hay_sync(void)
27761f28255Scgd {
27861f28255Scgd 	int len, retry = 0;
27961f28255Scgd 
28061f28255Scgd 	while (retry++ <= MAXRETRY) {
281ffe34450Schristos 		(void)write(FD, "AT\r", 3);
282ffe34450Schristos 		(void)sleep(1);
283ffe34450Schristos 		(void)ioctl(FD, FIONREAD, &len);
28461f28255Scgd 		if (len) {
285c4341bc3Schristos 			len = read(FD, dumbuf, (size_t)min(len, DUMBUFLEN));
286e37283e1Slukem 			if (strchr(dumbuf, '0') ||
287e37283e1Slukem 		   	(strchr(dumbuf, 'O') && strchr(dumbuf, 'K')))
28861f28255Scgd 				return(1);
28961f28255Scgd #ifdef DEBUG
29061f28255Scgd 			dumbuf[len] = '\0';
291ffe34450Schristos 			(void)printf("hay_sync: (\"%s\") %d\n\r", dumbuf, retry);
29261f28255Scgd #endif
29361f28255Scgd 		}
294ffe34450Schristos 		(void)ioctl(FD, TIOCCDTR, 0);
295ffe34450Schristos 		(void)ioctl(FD, TIOCSDTR, 0);
29661f28255Scgd 	}
297ffe34450Schristos 	(void)printf("Cannot synchronize with hayes...\n\r");
29861f28255Scgd 	return(0);
29961f28255Scgd }
300