xref: /freebsd/usr.bin/tip/libacu/v3451.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
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 #ifndef lint
37 #endif /* not lint */
38 
39 /*
40  * Routines for calling up on a Vadic 3451 Modem
41  */
42 #include "tip.h"
43 
44 static	jmp_buf Sjbuf;
45 
46 static void	vawrite(char *, int);
47 static int	expect(char *);
48 static void	alarmtr(int);
49 static int	notin(char *, char *);
50 static int	prefix(char *, char *);
51 
52 int
53 v3451_dialer(char *num, char *acu)
54 {
55 	sig_t func;
56 	int ok;
57 	int slow = number(value(BAUDRATE)) < 1200;
58 	char phone[50];
59 	struct termios cntrl;
60 
61 	/*
62 	 * Get in synch
63 	 */
64 	vawrite("I\r", 1 + slow);
65 	vawrite("I\r", 1 + slow);
66 	vawrite("I\r", 1 + slow);
67 	vawrite("\005\r", 2 + slow);
68 	if (!expect("READY")) {
69 		printf("can't synchronize with vadic 3451\n");
70 #ifdef ACULOG
71 		logent(value(HOST), num, "vadic", "can't synch up");
72 #endif
73 		return (0);
74 	}
75 	tcgetattr(FD, &cntrl);
76 	term.c_cflag |= HUPCL;
77 	tcsetattr(FD, TCSANOW, &cntrl);
78 	sleep(1);
79 	vawrite("D\r", 2 + slow);
80 	if (!expect("NUMBER?")) {
81 		printf("Vadic will not accept dial command\n");
82 #ifdef ACULOG
83 		logent(value(HOST), num, "vadic", "will not accept dial");
84 #endif
85 		return (0);
86 	}
87 	(void)snprintf(phone, sizeof phone, "%s\r", num);
88 	vawrite(phone, 1 + slow);
89 	if (!expect(phone)) {
90 		printf("Vadic will not accept phone number\n");
91 #ifdef ACULOG
92 		logent(value(HOST), num, "vadic", "will not accept number");
93 #endif
94 		return (0);
95 	}
96 	func = signal(SIGINT,SIG_IGN);
97 	/*
98 	 * You cannot interrupt the Vadic when its dialing;
99 	 * even dropping DTR does not work (definitely a
100 	 * brain damaged design).
101 	 */
102 	vawrite("\r", 1 + slow);
103 	vawrite("\r", 1 + slow);
104 	if (!expect("DIALING:")) {
105 		printf("Vadic failed to dial\n");
106 #ifdef ACULOG
107 		logent(value(HOST), num, "vadic", "failed to dial");
108 #endif
109 		return (0);
110 	}
111 	if (boolean(value(VERBOSE)))
112 		printf("\ndialing...");
113 	ok = expect("ON LINE");
114 	signal(SIGINT, func);
115 	if (!ok) {
116 		printf("call failed\n");
117 #ifdef ACULOG
118 		logent(value(HOST), num, "vadic", "call failed");
119 #endif
120 		return (0);
121 	}
122 	tcflush(FD, TCIOFLUSH);
123 	return (1);
124 }
125 
126 void
127 v3451_disconnect(void)
128 {
129 	close(FD);
130 }
131 
132 void
133 v3451_abort(void)
134 {
135 	close(FD);
136 }
137 
138 static void
139 vawrite(char *cp, int delay)
140 {
141 	for (; *cp; sleep(delay), cp++)
142 		write(FD, cp, 1);
143 }
144 
145 static int
146 expect(char *cp)
147 {
148 	char buf[300];
149 	char *rp = buf;
150 	int timeout = 30, online = 0;
151 
152 	if (strcmp(cp, "\"\"") == 0)
153 		return (1);
154 	*rp = 0;
155 	/*
156 	 * If we are waiting for the Vadic to complete
157 	 * dialing and get a connection, allow more time
158 	 * Unfortunately, the Vadic times out 24 seconds after
159 	 * the last digit is dialed
160 	 */
161 	online = strcmp(cp, "ON LINE") == 0;
162 	if (online)
163 		timeout = number(value(DIALTIMEOUT));
164 	signal(SIGALRM, alarmtr);
165 	if (setjmp(Sjbuf))
166 		return (0);
167 	alarm(timeout);
168 	while (notin(cp, buf) && rp < buf + sizeof (buf) - 1) {
169 		if (online && notin("FAILED CALL", buf) == 0)
170 			return (0);
171 		if (read(FD, rp, 1) < 0) {
172 			alarm(0);
173 			return (0);
174 		}
175 		if (*rp &= 0177)
176 			rp++;
177 		*rp = '\0';
178 	}
179 	alarm(0);
180 	return (1);
181 }
182 
183 /*ARGSUSED*/
184 static void
185 alarmtr(int signo)
186 {
187 	longjmp(Sjbuf, 1);
188 }
189 
190 static int
191 notin(char *sh, char *lg)
192 {
193 	for (; *lg; lg++)
194 		if (prefix(sh, lg))
195 			return (0);
196 	return (1);
197 }
198 
199 static int
200 prefix(char *s1, char *s2)
201 {
202 	char c;
203 
204 	while ((c = *s1++) == *s2++)
205 		if (c == '\0')
206 			return (1);
207 	return (c == '\0');
208 }
209