xref: /csrg-svn/usr.bin/tip/aculib/ventel.c (revision 13281)
1*13281Ssam #ifndef lint
2*13281Ssam static char sccsid[] = "@(#)ventel.c	1.4 (Berkeley) 06/25/83";
3*13281Ssam #endif
46562Sshannon 
56562Sshannon #if VENTEL
66562Sshannon /*
76562Sshannon  * Routines for calling up on a Ventel Modem
8*13281Ssam  * The Ventel is expected to be strapped for "no echo".
96562Sshannon  */
106562Sshannon #include "tip.h"
116562Sshannon 
126562Sshannon #define	MAXRETRY	5
136562Sshannon 
14*13281Ssam static	int sigALRM();
15*13281Ssam static	int timeout = 0;
16*13281Ssam static	jmp_buf timeoutbuf;
176562Sshannon 
186562Sshannon ven_dialer(num, acu)
196562Sshannon 	register char *num;
206562Sshannon 	char *acu;
216562Sshannon {
226562Sshannon 	register char *cp;
236562Sshannon 	register int connected = 0;
246562Sshannon #ifdef ACULOG
256562Sshannon 	char line[80];
266562Sshannon #endif
276562Sshannon 	/*
286562Sshannon 	 * Get in synch with a couple of carriage returns
296562Sshannon 	 */
306562Sshannon 	if (!vensync(FD)) {
316562Sshannon 		printf("can't synchronize with ventel\n");
326562Sshannon #ifdef ACULOG
336562Sshannon 		logent(value(HOST), num, "ventel", "can't synch up");
346562Sshannon #endif
356562Sshannon 		return (0);
366562Sshannon 	}
377592Sshannon 	if (boolean(value(VERBOSE)))
387592Sshannon 		printf("\ndialing...");
397592Sshannon 	fflush(stdout);
406562Sshannon 	ioctl(FD, TIOCHPCL, 0);
41*13281Ssam 	echo("#k$\r$\n$D$I$A$L$:$ ");
426562Sshannon 	for (cp = num; *cp; cp++) {
436562Sshannon 		sleep(1);
446562Sshannon 		write(FD, cp, 1);
456562Sshannon 	}
46*13281Ssam 	echo("\r$\n");
476562Sshannon 	if (gobble('\n'))
486562Sshannon 		connected = gobble('!');
496562Sshannon 	ioctl(FD, TIOCFLUSH);
506562Sshannon #ifdef ACULOG
516562Sshannon 	if (timeout) {
526562Sshannon 		sprintf(line, "%d second dial timeout",
536562Sshannon 			number(value(DIALTIMEOUT)));
546562Sshannon 		logent(value(HOST), num, "ventel", line);
556562Sshannon 	}
566562Sshannon #endif
576562Sshannon 	if (timeout)
586562Sshannon 		ven_disconnect();	/* insurance */
596562Sshannon 	return (connected);
606562Sshannon }
616562Sshannon 
626562Sshannon ven_disconnect()
636562Sshannon {
64*13281Ssam 
656562Sshannon 	close(FD);
666562Sshannon }
676562Sshannon 
686562Sshannon ven_abort()
696562Sshannon {
70*13281Ssam 
716562Sshannon 	write(FD, "\03", 1);
726562Sshannon 	close(FD);
736562Sshannon }
746562Sshannon 
756562Sshannon static int
766562Sshannon echo(s)
776562Sshannon 	register char *s;
786562Sshannon {
796562Sshannon 	char c;
806562Sshannon 
816562Sshannon 	while (c = *s++) switch (c) {
826562Sshannon 
836562Sshannon 	case '$':
846562Sshannon 		read(FD, &c, 1);
856562Sshannon 		s++;
866562Sshannon 		break;
876562Sshannon 
886562Sshannon 	case '#':
896562Sshannon 		c = *s++;
906562Sshannon 		write(FD, &c, 1);
916562Sshannon 		break;
926562Sshannon 
936562Sshannon 	default:
946562Sshannon 		write(FD, &c, 1);
956562Sshannon 		read(FD, &c, 1);
966562Sshannon 	}
976562Sshannon }
986562Sshannon 
996562Sshannon static int
1006562Sshannon sigALRM()
1016562Sshannon {
102*13281Ssam 
1036562Sshannon 	printf("\07timeout waiting for reply\n");
1046562Sshannon 	timeout = 1;
105*13281Ssam 	longjmp(timeoutbuf, 1);
1066562Sshannon }
1076562Sshannon 
1086562Sshannon static int
109*13281Ssam gobble(match)
110*13281Ssam 	register char match;
1116562Sshannon {
1126562Sshannon 	char c;
113*13281Ssam 	int (*f)();
1146562Sshannon 
1156562Sshannon 	signal(SIGALRM, sigALRM);
1166562Sshannon 	timeout = 0;
1176562Sshannon 	do {
118*13281Ssam 		if (setjmp(timeoutbuf)) {
119*13281Ssam 			signal(SIGALRM, f);
120*13281Ssam 			return (0);
121*13281Ssam 		}
1226562Sshannon 		alarm(number(value(DIALTIMEOUT)));
1236562Sshannon 		read(FD, &c, 1);
124*13281Ssam 		alarm(0);
1256562Sshannon 		c &= 0177;
1266562Sshannon #ifdef notdef
1276562Sshannon 		if (boolean(value(VERBOSE)))
1287592Sshannon 			putchar(c);
1296562Sshannon #endif
130*13281Ssam 	} while (c != '\n' && c != match);
1316562Sshannon 	signal(SIGALRM, SIG_DFL);
132*13281Ssam 	return (c == match);
1336562Sshannon }
1346562Sshannon 
1356562Sshannon #define min(a,b)	((a)>(b)?(b):(a))
1366562Sshannon /*
1376562Sshannon  * This convoluted piece of code attempts to get
138*13281Ssam  * the ventel in sync.  If you don't have FIONREAD
139*13281Ssam  * there are gory ways to simulate this.
1406562Sshannon  */
1416562Sshannon static int
1426562Sshannon vensync(fd)
1436562Sshannon {
144*13281Ssam 	int already = 0, nread;
1456562Sshannon 	char buf[60];
1466562Sshannon 
1476562Sshannon 	/*
1486562Sshannon 	 * Toggle DTR to force anyone off that might have left
1496562Sshannon 	 * the modem connected, and insure a consistent state
1506562Sshannon 	 * to start from.
1516562Sshannon 	 *
1526562Sshannon 	 * If you don't have the ioctl calls to diddle directly
1536562Sshannon 	 * with DTR, you can always try setting the baud rate to 0.
1546562Sshannon 	 */
1556562Sshannon 	ioctl(FD, TIOCCDTR, 0);
1566562Sshannon 	sleep(2);
1576562Sshannon 	ioctl(FD, TIOCSDTR, 0);
1586562Sshannon 	while (already < MAXRETRY) {
1596562Sshannon 		/*
1606562Sshannon 		 * After reseting the modem, send it two \r's to
1616562Sshannon 		 * autobaud on. Make sure to delay between them
1626562Sshannon 		 * so the modem can frame the incoming characters.
1636562Sshannon 		 */
1646562Sshannon 		write(fd, "\r", 1);
1656562Sshannon 		sleep(1);
1666562Sshannon 		write(fd, "\r", 1);
1676562Sshannon 		sleep(3);
168*13281Ssam 		if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
169*13281Ssam 			perror("tip: ioctl");
170*13281Ssam 			continue;
1716562Sshannon 		}
172*13281Ssam 		while (nread > 0) {
173*13281Ssam 			read(fd, buf, min(nread, 60));
174*13281Ssam 			if ((buf[nread - 1] & 0177) == '$')
175*13281Ssam 				return (1);
176*13281Ssam 			nread -= min(nread, 60);
177*13281Ssam 		}
178*13281Ssam 		sleep(1);
179*13281Ssam 		already++;
1806562Sshannon 	}
1816562Sshannon 	return (0);
1826562Sshannon }
1836562Sshannon #endif
184