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