xref: /csrg-svn/usr.bin/tip/aculib/ventel.c (revision 15195)
113281Ssam #ifndef lint
2*15195Ssam static char sccsid[] = "@(#)ventel.c	1.6 (Berkeley) 10/09/83";
313281Ssam #endif
46562Sshannon 
56562Sshannon /*
66562Sshannon  * Routines for calling up on a Ventel Modem
713281Ssam  * The Ventel is expected to be strapped for "no echo".
86562Sshannon  */
96562Sshannon #include "tip.h"
106562Sshannon 
116562Sshannon #define	MAXRETRY	5
126562Sshannon 
1313281Ssam static	int sigALRM();
1413281Ssam static	int timeout = 0;
1513281Ssam static	jmp_buf timeoutbuf;
166562Sshannon 
176562Sshannon ven_dialer(num, acu)
186562Sshannon 	register char *num;
196562Sshannon 	char *acu;
206562Sshannon {
216562Sshannon 	register char *cp;
226562Sshannon 	register int connected = 0;
23*15195Ssam 	char *msg, *index(), line[80];
24*15195Ssam 
256562Sshannon 	/*
266562Sshannon 	 * Get in synch with a couple of carriage returns
276562Sshannon 	 */
286562Sshannon 	if (!vensync(FD)) {
296562Sshannon 		printf("can't synchronize with ventel\n");
306562Sshannon #ifdef ACULOG
316562Sshannon 		logent(value(HOST), num, "ventel", "can't synch up");
326562Sshannon #endif
336562Sshannon 		return (0);
346562Sshannon 	}
357592Sshannon 	if (boolean(value(VERBOSE)))
367592Sshannon 		printf("\ndialing...");
377592Sshannon 	fflush(stdout);
386562Sshannon 	ioctl(FD, TIOCHPCL, 0);
3913281Ssam 	echo("#k$\r$\n$D$I$A$L$:$ ");
406562Sshannon 	for (cp = num; *cp; cp++) {
416562Sshannon 		sleep(1);
426562Sshannon 		write(FD, cp, 1);
436562Sshannon 	}
4413281Ssam 	echo("\r$\n");
45*15195Ssam 	if (gobble('\n', line))
46*15195Ssam 		connected = gobble('!', line);
476562Sshannon 	ioctl(FD, TIOCFLUSH);
486562Sshannon #ifdef ACULOG
496562Sshannon 	if (timeout) {
506562Sshannon 		sprintf(line, "%d second dial timeout",
516562Sshannon 			number(value(DIALTIMEOUT)));
526562Sshannon 		logent(value(HOST), num, "ventel", line);
536562Sshannon 	}
546562Sshannon #endif
556562Sshannon 	if (timeout)
566562Sshannon 		ven_disconnect();	/* insurance */
57*15195Ssam 	if (connected || timeout || !boolean(value(VERBOSE)))
58*15195Ssam 		return (connected);
59*15195Ssam 	/* call failed, parse response for user */
60*15195Ssam 	cp = index(line, '\r');
61*15195Ssam 	if (cp)
62*15195Ssam 		*cp = '\0';
63*15195Ssam 	for (cp = line; cp = index(cp, ' '); cp++)
64*15195Ssam 		if (cp[1] == ' ')
65*15195Ssam 			break;
66*15195Ssam 	if (cp) {
67*15195Ssam 		while (*cp == ' ')
68*15195Ssam 			cp++;
69*15195Ssam 		msg = cp;
70*15195Ssam 		while (*cp) {
71*15195Ssam 			if (isupper(*cp))
72*15195Ssam 				*cp = tolower(*cp);
73*15195Ssam 			cp++;
74*15195Ssam 		}
75*15195Ssam 		printf("%s...", msg);
76*15195Ssam 	}
776562Sshannon 	return (connected);
786562Sshannon }
796562Sshannon 
806562Sshannon ven_disconnect()
816562Sshannon {
8213281Ssam 
836562Sshannon 	close(FD);
846562Sshannon }
856562Sshannon 
866562Sshannon ven_abort()
876562Sshannon {
8813281Ssam 
896562Sshannon 	write(FD, "\03", 1);
906562Sshannon 	close(FD);
916562Sshannon }
926562Sshannon 
936562Sshannon static int
946562Sshannon echo(s)
956562Sshannon 	register char *s;
966562Sshannon {
976562Sshannon 	char c;
986562Sshannon 
996562Sshannon 	while (c = *s++) switch (c) {
1006562Sshannon 
1016562Sshannon 	case '$':
1026562Sshannon 		read(FD, &c, 1);
1036562Sshannon 		s++;
1046562Sshannon 		break;
1056562Sshannon 
1066562Sshannon 	case '#':
1076562Sshannon 		c = *s++;
1086562Sshannon 		write(FD, &c, 1);
1096562Sshannon 		break;
1106562Sshannon 
1116562Sshannon 	default:
1126562Sshannon 		write(FD, &c, 1);
1136562Sshannon 		read(FD, &c, 1);
1146562Sshannon 	}
1156562Sshannon }
1166562Sshannon 
1176562Sshannon static int
1186562Sshannon sigALRM()
1196562Sshannon {
12013281Ssam 
1216562Sshannon 	printf("\07timeout waiting for reply\n");
1226562Sshannon 	timeout = 1;
12313281Ssam 	longjmp(timeoutbuf, 1);
1246562Sshannon }
1256562Sshannon 
1266562Sshannon static int
127*15195Ssam gobble(match, response)
12813281Ssam 	register char match;
129*15195Ssam 	char response[];
1306562Sshannon {
131*15195Ssam 	register char *cp = response;
1326562Sshannon 	char c;
13313281Ssam 	int (*f)();
1346562Sshannon 
1356562Sshannon 	signal(SIGALRM, sigALRM);
1366562Sshannon 	timeout = 0;
1376562Sshannon 	do {
13813281Ssam 		if (setjmp(timeoutbuf)) {
13913281Ssam 			signal(SIGALRM, f);
140*15195Ssam 			*cp = '\0';
14113281Ssam 			return (0);
14213281Ssam 		}
1436562Sshannon 		alarm(number(value(DIALTIMEOUT)));
144*15195Ssam 		read(FD, cp, 1);
14513281Ssam 		alarm(0);
146*15195Ssam 		c = (*cp++ &= 0177);
1476562Sshannon #ifdef notdef
1486562Sshannon 		if (boolean(value(VERBOSE)))
1497592Sshannon 			putchar(c);
1506562Sshannon #endif
15113281Ssam 	} while (c != '\n' && c != match);
1526562Sshannon 	signal(SIGALRM, SIG_DFL);
153*15195Ssam 	*cp = '\0';
15413281Ssam 	return (c == match);
1556562Sshannon }
1566562Sshannon 
1576562Sshannon #define min(a,b)	((a)>(b)?(b):(a))
1586562Sshannon /*
1596562Sshannon  * This convoluted piece of code attempts to get
16013281Ssam  * the ventel in sync.  If you don't have FIONREAD
16113281Ssam  * there are gory ways to simulate this.
1626562Sshannon  */
1636562Sshannon static int
1646562Sshannon vensync(fd)
1656562Sshannon {
16613281Ssam 	int already = 0, nread;
1676562Sshannon 	char buf[60];
1686562Sshannon 
1696562Sshannon 	/*
1706562Sshannon 	 * Toggle DTR to force anyone off that might have left
1716562Sshannon 	 * the modem connected, and insure a consistent state
1726562Sshannon 	 * to start from.
1736562Sshannon 	 *
1746562Sshannon 	 * If you don't have the ioctl calls to diddle directly
1756562Sshannon 	 * with DTR, you can always try setting the baud rate to 0.
1766562Sshannon 	 */
1776562Sshannon 	ioctl(FD, TIOCCDTR, 0);
1786562Sshannon 	sleep(2);
1796562Sshannon 	ioctl(FD, TIOCSDTR, 0);
1806562Sshannon 	while (already < MAXRETRY) {
1816562Sshannon 		/*
1826562Sshannon 		 * After reseting the modem, send it two \r's to
1836562Sshannon 		 * autobaud on. Make sure to delay between them
1846562Sshannon 		 * so the modem can frame the incoming characters.
1856562Sshannon 		 */
1866562Sshannon 		write(fd, "\r", 1);
1876562Sshannon 		sleep(1);
1886562Sshannon 		write(fd, "\r", 1);
1896562Sshannon 		sleep(3);
19013281Ssam 		if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
19113281Ssam 			perror("tip: ioctl");
19213281Ssam 			continue;
1936562Sshannon 		}
19413281Ssam 		while (nread > 0) {
19513281Ssam 			read(fd, buf, min(nread, 60));
19613281Ssam 			if ((buf[nread - 1] & 0177) == '$')
19713281Ssam 				return (1);
19813281Ssam 			nread -= min(nread, 60);
19913281Ssam 		}
20013281Ssam 		sleep(1);
20113281Ssam 		already++;
2026562Sshannon 	}
2036562Sshannon 	return (0);
2046562Sshannon }
205