xref: /csrg-svn/usr.bin/tip/aculib/v3451.c (revision 13131)
1*13131Sralph /*	v3451.c	4.1	83/06/15	*/
2*13131Sralph 
3*13131Sralph #if VADIC
4*13131Sralph /*
5*13131Sralph  * Routines for calling up on a Vadic 3451 Modem
6*13131Sralph  */
7*13131Sralph #include "tip.h"
8*13131Sralph #include <setjmp.h>
9*13131Sralph #include <errno.h>
10*13131Sralph #include <signal.h>
11*13131Sralph 
12*13131Sralph static char *sccsid = "@(#)v3451.c	4.1 06/15/83";
13*13131Sralph 
14*13131Sralph int	va_delay;
15*13131Sralph static	int	fudge=0;	/* for sleep in vawrite */
16*13131Sralph jmp_buf Sjbuf;
17*13131Sralph 
18*13131Sralph vadic_dialer(num, acu)
19*13131Sralph 	register char *num;
20*13131Sralph 	char *acu;
21*13131Sralph {
22*13131Sralph 	int lt;
23*13131Sralph 	int ok;
24*13131Sralph 	char phone[50];
25*13131Sralph #ifdef ACULOG
26*13131Sralph 	char line[80];
27*13131Sralph #endif
28*13131Sralph 	int (*func) ();
29*13131Sralph 
30*13131Sralph 	if(number(value(BAUDRATE)) < 1200)
31*13131Sralph 		fudge = 1;
32*13131Sralph 	/*
33*13131Sralph 	 * Get in synch
34*13131Sralph 	 */
35*13131Sralph 	lt = strlen(num);
36*13131Sralph 	va_delay = 15 + 3*lt;
37*13131Sralph 	vawrite("I\r",1);
38*13131Sralph 	vawrite("I\r",1);
39*13131Sralph 	vawrite("I\r",1);
40*13131Sralph 	vawrite("\005\r",2);
41*13131Sralph 	ok = expect("READY");
42*13131Sralph 
43*13131Sralph 	if ( ok ) {
44*13131Sralph 		printf("can't synchronize with vadic 3451\n");
45*13131Sralph #ifdef ACULOG
46*13131Sralph 		logent(value(HOST), num, "vadic", "can't synch up");
47*13131Sralph #endif
48*13131Sralph 		return (0);
49*13131Sralph 	}
50*13131Sralph 	ioctl(FD, TIOCHPCL, 0);
51*13131Sralph 	sleep(1);
52*13131Sralph 	vawrite("D\r",2);
53*13131Sralph 	ok = expect("NUMBER?");
54*13131Sralph 	if ( ok ) {
55*13131Sralph 		printf("Vadic will not accept dial command\n");
56*13131Sralph #ifdef ACULOG
57*13131Sralph 		logent(value(HOST), num, "vadic", "will not accept dial");
58*13131Sralph #endif
59*13131Sralph 		return (0);
60*13131Sralph 	}
61*13131Sralph 	strcpy(phone,num);
62*13131Sralph 	strcat(phone,"\r");
63*13131Sralph 	vawrite(phone,1);
64*13131Sralph 	ok = expect(phone);
65*13131Sralph 	if ( ok ) {
66*13131Sralph 		printf("Vadic will not accept phone number\n");
67*13131Sralph #ifdef ACULOG
68*13131Sralph 		logent(value(HOST), num, "vadic", "will not accept number");
69*13131Sralph #endif
70*13131Sralph 		return (0);
71*13131Sralph 	}
72*13131Sralph 	func = signal(SIGINT,SIG_IGN);
73*13131Sralph 	/* You cannot interrupt the Vadic when its dialing */
74*13131Sralph 	/* Even dropping DTR does not work /*
75*13131Sralph 	/* Definitely a Brain Damaged Design */
76*13131Sralph 	vawrite("\r",1);
77*13131Sralph 	vawrite("\r",1);
78*13131Sralph 	ok = expect("DIALING:");
79*13131Sralph 	if ( ok ) {
80*13131Sralph 		printf("Vadic failed to dial\n");
81*13131Sralph #ifdef ACULOG
82*13131Sralph 		logent(value(HOST), num, "vadic", "failed to dial");
83*13131Sralph #endif
84*13131Sralph 		return (0);
85*13131Sralph 	} else
86*13131Sralph 		printf("dialing...\n");
87*13131Sralph 	ok = expect("ON LINE");
88*13131Sralph 	signal(SIGINT,func);
89*13131Sralph 	if ( ok ) {
90*13131Sralph 		printf("call failed\n");
91*13131Sralph #ifdef ACULOG
92*13131Sralph 		logent(value(HOST), num, "vadic", "call failed");
93*13131Sralph #endif
94*13131Sralph 		return (0);
95*13131Sralph 	}
96*13131Sralph 	ioctl(FD, TIOCFLUSH);
97*13131Sralph 	return (1);
98*13131Sralph }
99*13131Sralph 
100*13131Sralph vadic_disconnect()
101*13131Sralph {
102*13131Sralph 	char string[100];
103*13131Sralph 	close(FD);
104*13131Sralph 	sleep(5); /* insure that the phone line is dropped */
105*13131Sralph 	sprintf(string,"/usr/lib/uucp/enable %s\n",rindex(DV,'/')+1);
106*13131Sralph 	system(string);
107*13131Sralph }
108*13131Sralph 
109*13131Sralph vadic_abort()
110*13131Sralph {
111*13131Sralph 	vadic_disconnect();
112*13131Sralph }
113*13131Sralph 
114*13131Sralph vawrite(str,delay)
115*13131Sralph char *str;
116*13131Sralph int delay;
117*13131Sralph {
118*13131Sralph 	while(*str)
119*13131Sralph 	{
120*13131Sralph 		write(FD,str,1);
121*13131Sralph 		sleep(delay+fudge);
122*13131Sralph 		str++;
123*13131Sralph 	}
124*13131Sralph 	return;
125*13131Sralph }
126*13131Sralph 
127*13131Sralph 
128*13131Sralph #define MR 300
129*13131Sralph 
130*13131Sralph int Error = 0;
131*13131Sralph 
132*13131Sralph /***
133*13131Sralph  *	expect(str)	look for expected string
134*13131Sralph  *	char *str;
135*13131Sralph  *
136*13131Sralph  *	return codes:
137*13131Sralph  *		0  -  found
138*13131Sralph  *		FAIL  -  lost line or too many characters read
139*13131Sralph  *		some character  -  timed out
140*13131Sralph  */
141*13131Sralph 
142*13131Sralph expect(str)
143*13131Sralph char *str;
144*13131Sralph {
145*13131Sralph 	static char rdvec[MR];
146*13131Sralph 	extern alarmtr();
147*13131Sralph 	char *rp = rdvec;
148*13131Sralph 	int nextch = 0, kr;
149*13131Sralph 	int alarm_tm;
150*13131Sralph 	int expect_online = 0;
151*13131Sralph 
152*13131Sralph 	if (strcmp(str, "\"\"") == 0)
153*13131Sralph 		return(0);
154*13131Sralph 	*rp = 0;
155*13131Sralph 	/*
156*13131Sralph 	 * If we are waiting for the Vadic to complete
157*13131Sralph 	 * dialing and get a connection, allow more time
158*13131Sralph 	 * Unfortunately, the Vadic times out 24 seconds after
159*13131Sralph 	 * the last digit is dialed
160*13131Sralph 	 */
161*13131Sralph 	if(strcmp(str, "ON LINE") == 0){
162*13131Sralph 		alarm_tm = number(value(DIALTIMEOUT));
163*13131Sralph 		expect_online++;
164*13131Sralph 	}
165*13131Sralph 	else
166*13131Sralph 		alarm_tm = 30;
167*13131Sralph 	if (setjmp(Sjbuf)) {
168*13131Sralph 		return(1);
169*13131Sralph 	}
170*13131Sralph 	signal(SIGALRM, alarmtr);
171*13131Sralph 	alarm(alarm_tm);
172*13131Sralph 	while (notin(str, rdvec)) {
173*13131Sralph 		if(expect_online)
174*13131Sralph 			if(notin("FAILED CALL", rdvec) == 0)
175*13131Sralph 				return(1);
176*13131Sralph 		kr = read(FD, &nextch, 1);
177*13131Sralph 		if (kr <= 0) {
178*13131Sralph 			alarm(0);
179*13131Sralph 			return(1);
180*13131Sralph 		}
181*13131Sralph 		{
182*13131Sralph 		int c;
183*13131Sralph 		c = nextch & 0177;
184*13131Sralph 		}
185*13131Sralph 		if ((*rp = nextch & 0177) != '\0')
186*13131Sralph 			rp++;
187*13131Sralph 		*rp = '\0';
188*13131Sralph 		if (rp >= rdvec + MR)
189*13131Sralph 			return(1);
190*13131Sralph 	}
191*13131Sralph 	alarm(0);
192*13131Sralph 	return(0);
193*13131Sralph }
194*13131Sralph 
195*13131Sralph /***
196*13131Sralph  *	alarmtr()  -  catch alarm routine for "expect".
197*13131Sralph  */
198*13131Sralph 
199*13131Sralph alarmtr()
200*13131Sralph {
201*13131Sralph 	longjmp(Sjbuf, 1);
202*13131Sralph }
203*13131Sralph 
204*13131Sralph /***
205*13131Sralph  *	notin(sh, lg)	check for occurrence of substring "sh"
206*13131Sralph  *	char *sh, *lg;
207*13131Sralph  *
208*13131Sralph  *	return codes:
209*13131Sralph  *		0  -  found the string
210*13131Sralph  *		1  -  not in the string
211*13131Sralph  */
212*13131Sralph 
213*13131Sralph notin(sh, lg)
214*13131Sralph char *sh, *lg;
215*13131Sralph {
216*13131Sralph 	while (*lg != '\0') {
217*13131Sralph 		if (prefix(sh, lg))
218*13131Sralph 			return(0);
219*13131Sralph 		else
220*13131Sralph 			lg++;
221*13131Sralph 	}
222*13131Sralph 	return(1);
223*13131Sralph }
224*13131Sralph 
225*13131Sralph /*******
226*13131Sralph  *	prefix(s1, s2)	check s2 for prefix s1
227*13131Sralph  *	char *s1, *s2;
228*13131Sralph  *
229*13131Sralph  *	return 0 - !=
230*13131Sralph  *	return 1 - ==
231*13131Sralph  */
232*13131Sralph 
233*13131Sralph prefix(s1, s2)
234*13131Sralph char *s1, *s2;
235*13131Sralph {
236*13131Sralph 	char c;
237*13131Sralph 
238*13131Sralph 	while ((c = *s1++) == *s2++)
239*13131Sralph 		if (c == '\0')
240*13131Sralph 			return(1);
241*13131Sralph 	return(c == '\0');
242*13131Sralph }
243