Home | History | Annotate | Line # | Download | only in aculib
ventel.c revision 1.13
      1 /*	$NetBSD: ventel.c,v 1.13 2006/04/03 00:51:14 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[] = "@(#)ventel.c	8.1 (Berkeley) 6/6/93";
     36 #endif
     37 __RCSID("$NetBSD: ventel.c,v 1.13 2006/04/03 00:51:14 perry Exp $");
     38 #endif /* not lint */
     39 
     40 /*
     41  * Routines for calling up on a Ventel Modem
     42  * The Ventel is expected to be strapped for local echo (just like uucp)
     43  */
     44 #include "tip.h"
     45 
     46 #define	MAXRETRY	5
     47 
     48 static	int timeout = 0;
     49 static	jmp_buf timeoutbuf;
     50 
     51 static	void	echo(const char *);
     52 static	int	gobble(char, char *);
     53 static	void	sigALRM(int);
     54 static	int	vensync(int);
     55 
     56 /*
     57  * some sleep calls have been replaced by usleep(DELAYUS)
     58  * because some ventel modems require two <cr>s in less than
     59  * a second in order to 'wake up'
     60  */
     61 #define	DELAYUS		100000		/* delay in microseconds */
     62 
     63 int
     64 ven_dialer(char *num, char *acu)
     65 {
     66 	char *cp;
     67 	int connected = 0;
     68 	char *msg, line[80];
     69 	struct termios	cntrl;
     70 
     71 	/*
     72 	 * Get in synch with a couple of carriage returns
     73 	 */
     74 	if (!vensync(FD)) {
     75 		printf("can't synchronize with ventel\n");
     76 #ifdef ACULOG
     77 		logent(value(HOST), num, "ventel", "can't synch up");
     78 #endif
     79 		return (0);
     80 	}
     81 	if (boolean(value(VERBOSE)))
     82 		printf("\ndialing...");
     83 	fflush(stdout);
     84 	tcgetattr(FD, &cntrl);
     85 	cntrl.c_cflag |= HUPCL;
     86 	tcsetattr(FD, TCSANOW, &cntrl);
     87 	echo("#k$\r$\n$D$I$A$L$:$ ");
     88 	for (cp = num; *cp; cp++) {
     89 		usleep(DELAYUS);
     90 		write(FD, cp, 1);
     91 	}
     92 	usleep(DELAYUS);
     93 	write(FD, "\r", 1);
     94 	gobble('\n', line);
     95 	if (gobble('\n', line))
     96 		connected = gobble('!', line);
     97 	tcflush(FD, TCIOFLUSH);
     98 #ifdef ACULOG
     99 	if (timeout) {
    100 		(void)snprintf(line, sizeof line, "%d second dial timeout",
    101 			(int)number(value(DIALTIMEOUT)));
    102 		logent(value(HOST), num, "ventel", line);
    103 	}
    104 #endif
    105 	if (timeout)
    106 		ven_disconnect();	/* insurance */
    107 	if (connected || timeout || !boolean(value(VERBOSE)))
    108 		return (connected);
    109 	/* call failed, parse response for user */
    110 	cp = strchr(line, '\r');
    111 	if (cp)
    112 		*cp = '\0';
    113 	for (cp = line; (cp = strchr(cp, ' ')) != NULL; cp++)
    114 		if (cp[1] == ' ')
    115 			break;
    116 	if (cp) {
    117 		while (*cp == ' ')
    118 			cp++;
    119 		msg = cp;
    120 		while (*cp) {
    121 			if (isupper((unsigned char)*cp))
    122 				*cp = tolower((unsigned char)*cp);
    123 			cp++;
    124 		}
    125 		printf("%s...", msg);
    126 	}
    127 	return (connected);
    128 }
    129 
    130 void
    131 ven_disconnect(void)
    132 {
    133 
    134 	close(FD);
    135 }
    136 
    137 void
    138 ven_abort(void)
    139 {
    140 
    141 	write(FD, "\03", 1);
    142 	close(FD);
    143 }
    144 
    145 static void
    146 echo(const char *s)
    147 {
    148 	char c;
    149 
    150 	while ((c = *s++) != 0) switch (c) {
    151 
    152 	case '$':
    153 		read(FD, &c, 1);
    154 		s++;
    155 		break;
    156 
    157 	case '#':
    158 		c = *s++;
    159 		write(FD, &c, 1);
    160 		break;
    161 
    162 	default:
    163 		write(FD, &c, 1);
    164 		read(FD, &c, 1);
    165 	}
    166 }
    167 
    168 static void
    169 sigALRM(int dummy)
    170 {
    171 
    172 	printf("\07timeout waiting for reply\n");
    173 	timeout = 1;
    174 	longjmp(timeoutbuf, 1);
    175 }
    176 
    177 static int
    178 gobble(char match, char response[])
    179 {
    180 	char *cp = response;
    181 	sig_t f;
    182 	char c;
    183 
    184 #if __GNUC__	/* XXX pacify gcc */
    185 	(void)&cp;
    186 #endif
    187 
    188 	f = signal(SIGALRM, sigALRM);
    189 	timeout = 0;
    190 	do {
    191 		if (setjmp(timeoutbuf)) {
    192 			signal(SIGALRM, f);
    193 			*cp = '\0';
    194 			return (0);
    195 		}
    196 		alarm(number(value(DIALTIMEOUT)));
    197 		read(FD, cp, 1);
    198 		alarm(0);
    199 		c = (*cp++ &= 0177);
    200 #ifdef notdef
    201 		if (boolean(value(VERBOSE)))
    202 			putchar(c);
    203 #endif
    204 	} while (c != '\n' && c != match);
    205 	signal(SIGALRM, SIG_DFL);
    206 	*cp = '\0';
    207 	return (c == match);
    208 }
    209 
    210 #define min(a,b)	((a)>(b)?(b):(a))
    211 /*
    212  * This convoluted piece of code attempts to get
    213  * the ventel in sync.  If you don't have FIONREAD
    214  * there are gory ways to simulate this.
    215  */
    216 static int
    217 vensync(int fd)
    218 {
    219 	int already = 0, nread;
    220 	char buf[60];
    221 
    222 	/*
    223 	 * Toggle DTR to force anyone off that might have left
    224 	 * the modem connected, and insure a consistent state
    225 	 * to start from.
    226 	 *
    227 	 * If you don't have the ioctl calls to diddle directly
    228 	 * with DTR, you can always try setting the baud rate to 0.
    229 	 */
    230 	ioctl(FD, TIOCCDTR, 0);
    231 	sleep(1);
    232 	ioctl(FD, TIOCSDTR, 0);
    233 	while (already < MAXRETRY) {
    234 		/*
    235 		 * After reseting the modem, send it two \r's to
    236 		 * autobaud on. Make sure to delay between them
    237 		 * so the modem can frame the incoming characters.
    238 		 */
    239 		write(fd, "\r", 1);
    240 		usleep(DELAYUS);
    241 		write(fd, "\r", 1);
    242 		sleep(2);
    243 		if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
    244 			perror("tip: ioctl");
    245 			continue;
    246 		}
    247 		while (nread > 0) {
    248 			read(fd, buf, min(nread, sizeof buf));
    249 			if ((buf[min(nread, sizeof buf) - 1] & 0177) == '$')
    250 				return (1);
    251 			nread -= min(nread, 60);
    252 		}
    253 		sleep(1);
    254 		already++;
    255 	}
    256 	return (0);
    257 }
    258