Home | History | Annotate | Line # | Download | only in aculib
ventel.c revision 1.14
      1 /*	$NetBSD: ventel.c,v 1.14 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[] = "@(#)ventel.c	8.1 (Berkeley) 6/6/93";
     36 #endif
     37 __RCSID("$NetBSD: ventel.c,v 1.14 2006/04/03 02:25:27 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 		return (0);
     77 	}
     78 	if (boolean(value(VERBOSE)))
     79 		printf("\ndialing...");
     80 	fflush(stdout);
     81 	tcgetattr(FD, &cntrl);
     82 	cntrl.c_cflag |= HUPCL;
     83 	tcsetattr(FD, TCSANOW, &cntrl);
     84 	echo("#k$\r$\n$D$I$A$L$:$ ");
     85 	for (cp = num; *cp; cp++) {
     86 		usleep(DELAYUS);
     87 		write(FD, cp, 1);
     88 	}
     89 	usleep(DELAYUS);
     90 	write(FD, "\r", 1);
     91 	gobble('\n', line);
     92 	if (gobble('\n', line))
     93 		connected = gobble('!', line);
     94 	tcflush(FD, TCIOFLUSH);
     95 	if (timeout)
     96 		ven_disconnect();	/* insurance */
     97 	if (connected || timeout || !boolean(value(VERBOSE)))
     98 		return (connected);
     99 	/* call failed, parse response for user */
    100 	cp = strchr(line, '\r');
    101 	if (cp)
    102 		*cp = '\0';
    103 	for (cp = line; (cp = strchr(cp, ' ')) != NULL; cp++)
    104 		if (cp[1] == ' ')
    105 			break;
    106 	if (cp) {
    107 		while (*cp == ' ')
    108 			cp++;
    109 		msg = cp;
    110 		while (*cp) {
    111 			if (isupper((unsigned char)*cp))
    112 				*cp = tolower((unsigned char)*cp);
    113 			cp++;
    114 		}
    115 		printf("%s...", msg);
    116 	}
    117 	return (connected);
    118 }
    119 
    120 void
    121 ven_disconnect(void)
    122 {
    123 
    124 	close(FD);
    125 }
    126 
    127 void
    128 ven_abort(void)
    129 {
    130 
    131 	write(FD, "\03", 1);
    132 	close(FD);
    133 }
    134 
    135 static void
    136 echo(const char *s)
    137 {
    138 	char c;
    139 
    140 	while ((c = *s++) != 0) switch (c) {
    141 
    142 	case '$':
    143 		read(FD, &c, 1);
    144 		s++;
    145 		break;
    146 
    147 	case '#':
    148 		c = *s++;
    149 		write(FD, &c, 1);
    150 		break;
    151 
    152 	default:
    153 		write(FD, &c, 1);
    154 		read(FD, &c, 1);
    155 	}
    156 }
    157 
    158 static void
    159 sigALRM(int dummy)
    160 {
    161 
    162 	printf("\07timeout waiting for reply\n");
    163 	timeout = 1;
    164 	longjmp(timeoutbuf, 1);
    165 }
    166 
    167 static int
    168 gobble(char match, char response[])
    169 {
    170 	char *cp = response;
    171 	sig_t f;
    172 	char c;
    173 
    174 #if __GNUC__	/* XXX pacify gcc */
    175 	(void)&cp;
    176 #endif
    177 
    178 	f = signal(SIGALRM, sigALRM);
    179 	timeout = 0;
    180 	do {
    181 		if (setjmp(timeoutbuf)) {
    182 			signal(SIGALRM, f);
    183 			*cp = '\0';
    184 			return (0);
    185 		}
    186 		alarm(number(value(DIALTIMEOUT)));
    187 		read(FD, cp, 1);
    188 		alarm(0);
    189 		c = (*cp++ &= 0177);
    190 #ifdef notdef
    191 		if (boolean(value(VERBOSE)))
    192 			putchar(c);
    193 #endif
    194 	} while (c != '\n' && c != match);
    195 	signal(SIGALRM, SIG_DFL);
    196 	*cp = '\0';
    197 	return (c == match);
    198 }
    199 
    200 #define min(a,b)	((a)>(b)?(b):(a))
    201 /*
    202  * This convoluted piece of code attempts to get
    203  * the ventel in sync.  If you don't have FIONREAD
    204  * there are gory ways to simulate this.
    205  */
    206 static int
    207 vensync(int fd)
    208 {
    209 	int already = 0, nread;
    210 	char buf[60];
    211 
    212 	/*
    213 	 * Toggle DTR to force anyone off that might have left
    214 	 * the modem connected, and insure a consistent state
    215 	 * to start from.
    216 	 *
    217 	 * If you don't have the ioctl calls to diddle directly
    218 	 * with DTR, you can always try setting the baud rate to 0.
    219 	 */
    220 	ioctl(FD, TIOCCDTR, 0);
    221 	sleep(1);
    222 	ioctl(FD, TIOCSDTR, 0);
    223 	while (already < MAXRETRY) {
    224 		/*
    225 		 * After reseting the modem, send it two \r's to
    226 		 * autobaud on. Make sure to delay between them
    227 		 * so the modem can frame the incoming characters.
    228 		 */
    229 		write(fd, "\r", 1);
    230 		usleep(DELAYUS);
    231 		write(fd, "\r", 1);
    232 		sleep(2);
    233 		if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
    234 			perror("tip: ioctl");
    235 			continue;
    236 		}
    237 		while (nread > 0) {
    238 			read(fd, buf, min(nread, sizeof buf));
    239 			if ((buf[min(nread, sizeof buf) - 1] & 0177) == '$')
    240 				return (1);
    241 			nread -= min(nread, 60);
    242 		}
    243 		sleep(1);
    244 		already++;
    245 	}
    246 	return (0);
    247 }
    248