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