xref: /freebsd/usr.bin/tip/libacu/v3451.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
1 /*	$OpenBSD: v3451.c,v 1.6 2001/10/24 18:38:58 millert Exp $	*/
2 /*	$NetBSD: v3451.c,v 1.6 1997/02/11 09:24:20 mrg Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)v3451.c	8.1 (Berkeley) 6/6/93";
43 static char rcsid[] = "$OpenBSD: v3451.c,v 1.6 2001/10/24 18:38:58 millert Exp $";
44 #endif
45 #endif /* not lint */
46 
47 /*
48  * Routines for calling up on a Vadic 3451 Modem
49  */
50 #include "tip.h"
51 
52 static	jmp_buf Sjbuf;
53 
54 static	int expect(), notin(), prefix();
55 static	void vawrite(), alarmtr();
56 
57 int
58 v3451_dialer(num, acu)
59 	char *num;
60 	char *acu;
61 {
62 	sig_t func;
63 	int ok;
64 	int slow = number(value(BAUDRATE)) < 1200;
65 	char phone[50];
66 	struct termios cntrl;
67 
68 	/*
69 	 * Get in synch
70 	 */
71 	vawrite("I\r", 1 + slow);
72 	vawrite("I\r", 1 + slow);
73 	vawrite("I\r", 1 + slow);
74 	vawrite("\005\r", 2 + slow);
75 	if (!expect("READY")) {
76 		printf("can't synchronize with vadic 3451\n");
77 #ifdef ACULOG
78 		logent(value(HOST), num, "vadic", "can't synch up");
79 #endif
80 		return (0);
81 	}
82 	tcgetattr(FD, &cntrl);
83 	term.c_cflag |= HUPCL;
84 	tcsetattr(FD, TCSANOW, &cntrl);
85 	sleep(1);
86 	vawrite("D\r", 2 + slow);
87 	if (!expect("NUMBER?")) {
88 		printf("Vadic will not accept dial command\n");
89 #ifdef ACULOG
90 		logent(value(HOST), num, "vadic", "will not accept dial");
91 #endif
92 		return (0);
93 	}
94 	(void)snprintf(phone, sizeof phone, "%s\r", num);
95 	vawrite(phone, 1 + slow);
96 	if (!expect(phone)) {
97 		printf("Vadic will not accept phone number\n");
98 #ifdef ACULOG
99 		logent(value(HOST), num, "vadic", "will not accept number");
100 #endif
101 		return (0);
102 	}
103 	func = signal(SIGINT,SIG_IGN);
104 	/*
105 	 * You cannot interrupt the Vadic when its dialing;
106 	 * even dropping DTR does not work (definitely a
107 	 * brain damaged design).
108 	 */
109 	vawrite("\r", 1 + slow);
110 	vawrite("\r", 1 + slow);
111 	if (!expect("DIALING:")) {
112 		printf("Vadic failed to dial\n");
113 #ifdef ACULOG
114 		logent(value(HOST), num, "vadic", "failed to dial");
115 #endif
116 		return (0);
117 	}
118 	if (boolean(value(VERBOSE)))
119 		printf("\ndialing...");
120 	ok = expect("ON LINE");
121 	signal(SIGINT, func);
122 	if (!ok) {
123 		printf("call failed\n");
124 #ifdef ACULOG
125 		logent(value(HOST), num, "vadic", "call failed");
126 #endif
127 		return (0);
128 	}
129 	tcflush(FD, TCIOFLUSH);
130 	return (1);
131 }
132 
133 void
134 v3451_disconnect()
135 {
136 
137 	close(FD);
138 }
139 
140 void
141 v3451_abort()
142 {
143 
144 	close(FD);
145 }
146 
147 static void
148 vawrite(cp, delay)
149 	char *cp;
150 	int delay;
151 {
152 
153 	for (; *cp; sleep(delay), cp++)
154 		write(FD, cp, 1);
155 }
156 
157 static int
158 expect(cp)
159 	char *cp;
160 {
161 	char buf[300];
162 	char *rp = buf;
163 	int timeout = 30, online = 0;
164 
165 	if (strcmp(cp, "\"\"") == 0)
166 		return (1);
167 	*rp = 0;
168 	/*
169 	 * If we are waiting for the Vadic to complete
170 	 * dialing and get a connection, allow more time
171 	 * Unfortunately, the Vadic times out 24 seconds after
172 	 * the last digit is dialed
173 	 */
174 	online = strcmp(cp, "ON LINE") == 0;
175 	if (online)
176 		timeout = number(value(DIALTIMEOUT));
177 	signal(SIGALRM, alarmtr);
178 	if (setjmp(Sjbuf))
179 		return (0);
180 	alarm(timeout);
181 	while (notin(cp, buf) && rp < buf + sizeof (buf) - 1) {
182 		if (online && notin("FAILED CALL", buf) == 0)
183 			return (0);
184 		if (read(FD, rp, 1) < 0) {
185 			alarm(0);
186 			return (0);
187 		}
188 		if (*rp &= 0177)
189 			rp++;
190 		*rp = '\0';
191 	}
192 	alarm(0);
193 	return (1);
194 }
195 
196 static void
197 alarmtr()
198 {
199 	longjmp(Sjbuf, 1);
200 }
201 
202 static int
203 notin(sh, lg)
204 	char *sh, *lg;
205 {
206 
207 	for (; *lg; lg++)
208 		if (prefix(sh, lg))
209 			return (0);
210 	return (1);
211 }
212 
213 static int
214 prefix(s1, s2)
215 	char *s1, *s2;
216 {
217 	char c;
218 
219 	while ((c = *s1++) == *s2++)
220 		if (c == '\0')
221 			return (1);
222 	return (c == '\0');
223 }
224