1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 2000 by Sun Microsystems, Inc.
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /* from UCB 4.4 6/25/83 */
7*0Sstevel@tonic-gate /*
8*0Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
9*0Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
10*0Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
11*0Sstevel@tonic-gate  */
12*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate /*
15*0Sstevel@tonic-gate  * Routines for calling up on a Vadic 3451 Modem
16*0Sstevel@tonic-gate  */
17*0Sstevel@tonic-gate #include "tip.h"
18*0Sstevel@tonic-gate 
19*0Sstevel@tonic-gate static	sigjmp_buf Sjbuf;
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate v3451_dialer(num, acu)
22*0Sstevel@tonic-gate 	register char *num;
23*0Sstevel@tonic-gate 	char *acu;
24*0Sstevel@tonic-gate {
25*0Sstevel@tonic-gate 	int ok;
26*0Sstevel@tonic-gate 	void (*func)();
27*0Sstevel@tonic-gate 	struct termios buf;
28*0Sstevel@tonic-gate 	int slow = number(value(BAUDRATE)) < 1200;
29*0Sstevel@tonic-gate 	char phone[50];
30*0Sstevel@tonic-gate #ifdef ACULOG
31*0Sstevel@tonic-gate 	char line[80];
32*0Sstevel@tonic-gate #endif
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate 	/*
35*0Sstevel@tonic-gate 	 * Get in synch
36*0Sstevel@tonic-gate 	 */
37*0Sstevel@tonic-gate 	vawrite("I\r", 1 + slow);
38*0Sstevel@tonic-gate 	vawrite("I\r", 1 + slow);
39*0Sstevel@tonic-gate 	vawrite("I\r", 1 + slow);
40*0Sstevel@tonic-gate 	vawrite("\005\r", 2 + slow);
41*0Sstevel@tonic-gate 	if (!expect("READY")) {
42*0Sstevel@tonic-gate 		printf("can't synchronize with vadic 3451\n");
43*0Sstevel@tonic-gate #ifdef ACULOG
44*0Sstevel@tonic-gate 		logent(value(HOST), num, "vadic", "can't synch up");
45*0Sstevel@tonic-gate #endif
46*0Sstevel@tonic-gate 		return (0);
47*0Sstevel@tonic-gate 	}
48*0Sstevel@tonic-gate 	ioctl(FD, TCGETS, &buf);
49*0Sstevel@tonic-gate 	buf.c_cflag |= HUPCL;
50*0Sstevel@tonic-gate 	ioctl(FD, TCSETSF, &buf);
51*0Sstevel@tonic-gate 	sleep(1);
52*0Sstevel@tonic-gate 	vawrite("D\r", 2 + slow);
53*0Sstevel@tonic-gate 	if (!expect("NUMBER?")) {
54*0Sstevel@tonic-gate 		printf("Vadic will not accept dial command\n");
55*0Sstevel@tonic-gate #ifdef ACULOG
56*0Sstevel@tonic-gate 		logent(value(HOST), num, "vadic", "will not accept dial");
57*0Sstevel@tonic-gate #endif
58*0Sstevel@tonic-gate 		return (0);
59*0Sstevel@tonic-gate 	}
60*0Sstevel@tonic-gate 	strlcpy(phone, num, sizeof (phone));
61*0Sstevel@tonic-gate 	strlcat(phone, "\r", sizeof (phone));
62*0Sstevel@tonic-gate 	vawrite(phone, 1 + slow);
63*0Sstevel@tonic-gate 	if (!expect(phone)) {
64*0Sstevel@tonic-gate 		printf("Vadic will not accept phone number\n");
65*0Sstevel@tonic-gate #ifdef ACULOG
66*0Sstevel@tonic-gate 		logent(value(HOST), num, "vadic", "will not accept number");
67*0Sstevel@tonic-gate #endif
68*0Sstevel@tonic-gate 		return (0);
69*0Sstevel@tonic-gate 	}
70*0Sstevel@tonic-gate 	func = signal(SIGINT, SIG_IGN);
71*0Sstevel@tonic-gate 	/*
72*0Sstevel@tonic-gate 	 * You cannot interrupt the Vadic when its dialing;
73*0Sstevel@tonic-gate 	 * even dropping DTR does not work (definitely a
74*0Sstevel@tonic-gate 	 * brain damaged design).
75*0Sstevel@tonic-gate 	 */
76*0Sstevel@tonic-gate 	vawrite("\r", 1 + slow);
77*0Sstevel@tonic-gate 	vawrite("\r", 1 + slow);
78*0Sstevel@tonic-gate 	if (!expect("DIALING:")) {
79*0Sstevel@tonic-gate 		printf("Vadic failed to dial\n");
80*0Sstevel@tonic-gate #ifdef ACULOG
81*0Sstevel@tonic-gate 		logent(value(HOST), num, "vadic", "failed to dial");
82*0Sstevel@tonic-gate #endif
83*0Sstevel@tonic-gate 		return (0);
84*0Sstevel@tonic-gate 	}
85*0Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
86*0Sstevel@tonic-gate 		printf("\ndialing...");
87*0Sstevel@tonic-gate 	ok = expect("ON LINE");
88*0Sstevel@tonic-gate 	signal(SIGINT, func);
89*0Sstevel@tonic-gate 	if (!ok) {
90*0Sstevel@tonic-gate 		printf("call failed\n");
91*0Sstevel@tonic-gate #ifdef ACULOG
92*0Sstevel@tonic-gate 		logent(value(HOST), num, "vadic", "call failed");
93*0Sstevel@tonic-gate #endif
94*0Sstevel@tonic-gate 		return (0);
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 	ioctl(FD, TCFLSH, TCOFLUSH);
97*0Sstevel@tonic-gate 	return (1);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate v3451_disconnect()
101*0Sstevel@tonic-gate {
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	close(FD);
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate v3451_abort()
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	close(FD);
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate static
113*0Sstevel@tonic-gate vawrite(cp, delay)
114*0Sstevel@tonic-gate 	register char *cp;
115*0Sstevel@tonic-gate 	int delay;
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	for (; *cp; sleep(delay), cp++)
119*0Sstevel@tonic-gate 		write(FD, cp, 1);
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate static
123*0Sstevel@tonic-gate expect(cp)
124*0Sstevel@tonic-gate 	register char *cp;
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate 	char buf[300];
127*0Sstevel@tonic-gate 	register char *rp = buf;
128*0Sstevel@tonic-gate 	static void alarmtr();
129*0Sstevel@tonic-gate 	int timeout = 30, online = 0;
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	if (strcmp(cp, "\"\"") == 0)
132*0Sstevel@tonic-gate 		return (1);
133*0Sstevel@tonic-gate 	*rp = 0;
134*0Sstevel@tonic-gate 	/*
135*0Sstevel@tonic-gate 	 * If we are waiting for the Vadic to complete
136*0Sstevel@tonic-gate 	 * dialing and get a connection, allow more time
137*0Sstevel@tonic-gate 	 * Unfortunately, the Vadic times out 24 seconds after
138*0Sstevel@tonic-gate 	 * the last digit is dialed
139*0Sstevel@tonic-gate 	 */
140*0Sstevel@tonic-gate 	online = strcmp(cp, "ON LINE") == 0;
141*0Sstevel@tonic-gate 	if (online)
142*0Sstevel@tonic-gate 		timeout = number(value(DIALTIMEOUT));
143*0Sstevel@tonic-gate 	signal(SIGALRM, alarmtr);
144*0Sstevel@tonic-gate 	if (sigsetjmp(Sjbuf, 1))
145*0Sstevel@tonic-gate 		return (0);
146*0Sstevel@tonic-gate 	alarm(timeout);
147*0Sstevel@tonic-gate 	while (notin(cp, buf) && rp < buf + sizeof (buf) - 1) {
148*0Sstevel@tonic-gate 		if (online && notin("FAILED CALL", buf) == 0)
149*0Sstevel@tonic-gate 			return (0);
150*0Sstevel@tonic-gate 		if (read(FD, rp, 1) < 0) {
151*0Sstevel@tonic-gate 			alarm(0);
152*0Sstevel@tonic-gate 			return (0);
153*0Sstevel@tonic-gate 		}
154*0Sstevel@tonic-gate 		if (*rp &= 0177)
155*0Sstevel@tonic-gate 			rp++;
156*0Sstevel@tonic-gate 		*rp = '\0';
157*0Sstevel@tonic-gate 	}
158*0Sstevel@tonic-gate 	alarm(0);
159*0Sstevel@tonic-gate 	return (1);
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate static void
163*0Sstevel@tonic-gate alarmtr()
164*0Sstevel@tonic-gate {
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	siglongjmp(Sjbuf, 1);
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate static
170*0Sstevel@tonic-gate notin(sh, lg)
171*0Sstevel@tonic-gate 	char *sh, *lg;
172*0Sstevel@tonic-gate {
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	for (; *lg; lg++)
175*0Sstevel@tonic-gate 		if (prefix(sh, lg))
176*0Sstevel@tonic-gate 			return (0);
177*0Sstevel@tonic-gate 	return (1);
178*0Sstevel@tonic-gate }
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate static
181*0Sstevel@tonic-gate prefix(s1, s2)
182*0Sstevel@tonic-gate 	register char *s1, *s2;
183*0Sstevel@tonic-gate {
184*0Sstevel@tonic-gate 	register char c;
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	while ((c = *s1++) == *s2++)
187*0Sstevel@tonic-gate 		if (c == '\0')
188*0Sstevel@tonic-gate 			return (1);
189*0Sstevel@tonic-gate 	return (c == '\0');
190*0Sstevel@tonic-gate }
191