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