xref: /freebsd/usr.bin/tip/libacu/ventel.c (revision e4e9813eb92cd7c4d4b819a8fbed5cbd3d92f5d8)
1 /*	$OpenBSD: ventel.c,v 1.7 2001/11/19 19:02:16 mpech Exp $	*/
2 /*	$NetBSD: ventel.c,v 1.6 1997/02/11 09:24:21 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[] = "@(#)ventel.c	8.1 (Berkeley) 6/6/93";
43 static char rcsid[] = "$OpenBSD: ventel.c,v 1.7 2001/11/19 19:02:16 mpech Exp $";
44 #endif
45 #endif /* not lint */
46 
47 /*
48  * Routines for calling up on a Ventel Modem
49  * The Ventel is expected to be strapped for local echo (just like uucp)
50  */
51 #include "tip.h"
52 #include <termios.h>
53 #include <sys/ioctl.h>
54 
55 #define	MAXRETRY	5
56 
57 static	void sigALRM();
58 static	int timeout = 0;
59 static	jmp_buf timeoutbuf;
60 
61 static	int gobble(), vensync();
62 static	void echo();
63 
64 /*
65  * some sleep calls have been replaced by this macro
66  * because some ventel modems require two <cr>s in less than
67  * a second in order to 'wake up'... yes, it is dirty...
68  */
69 #define delay(num,denom) busyloop(CPUSPEED*num/denom)
70 #define CPUSPEED 1000000	/* VAX 780 is 1MIPS */
71 #define DELAY(n) do { long N = (n); while (--N > 0); } while (0)
72 #define busyloop(n) do { DELAY(n); } while (0)
73 
74 int
75 ven_dialer(num, acu)
76 	char *num;
77 	char *acu;
78 {
79 	char *cp;
80 	int connected = 0;
81 	char *msg, line[80];
82 	struct termios	cntrl;
83 
84 	/*
85 	 * Get in synch with a couple of carriage returns
86 	 */
87 	if (!vensync(FD)) {
88 		printf("can't synchronize with ventel\n");
89 #ifdef ACULOG
90 		logent(value(HOST), num, "ventel", "can't synch up");
91 #endif
92 		return (0);
93 	}
94 	if (boolean(value(VERBOSE)))
95 		printf("\ndialing...");
96 	fflush(stdout);
97 	tcgetattr(FD, &cntrl);
98 	cntrl.c_cflag |= HUPCL;
99 	tcsetattr(FD, TCSANOW, &cntrl);
100 	echo("#k$\r$\n$D$I$A$L$:$ ");
101 	for (cp = num; *cp; cp++) {
102 		delay(1, 10);
103 		write(FD, cp, 1);
104 	}
105 	delay(1, 10);
106 	write(FD, "\r", 1);
107 	gobble('\n', line);
108 	if (gobble('\n', line))
109 		connected = gobble('!', line);
110 	tcflush(FD, TCIOFLUSH);
111 #ifdef ACULOG
112 	if (timeout) {
113 		(void)sprintf(line, "%ld second dial timeout",
114 			number(value(DIALTIMEOUT)));
115 		logent(value(HOST), num, "ventel", line);
116 	}
117 #endif
118 	if (timeout)
119 		ven_disconnect();	/* insurance */
120 	if (connected || timeout || !boolean(value(VERBOSE)))
121 		return (connected);
122 	/* call failed, parse response for user */
123 	cp = strchr(line, '\r');
124 	if (cp)
125 		*cp = '\0';
126 	for (cp = line; cp = strchr(cp, ' '); cp++)
127 		if (cp[1] == ' ')
128 			break;
129 	if (cp) {
130 		while (*cp == ' ')
131 			cp++;
132 		msg = cp;
133 		while (*cp) {
134 			if (isupper(*cp))
135 				*cp = tolower(*cp);
136 			cp++;
137 		}
138 		printf("%s...", msg);
139 	}
140 	return (connected);
141 }
142 
143 void
144 ven_disconnect()
145 {
146 
147 	close(FD);
148 }
149 
150 void
151 ven_abort()
152 {
153 
154 	write(FD, "\03", 1);
155 	close(FD);
156 }
157 
158 static void
159 echo(s)
160 	char *s;
161 {
162 	char c;
163 
164 	while (c = *s++) switch (c) {
165 
166 	case '$':
167 		read(FD, &c, 1);
168 		s++;
169 		break;
170 
171 	case '#':
172 		c = *s++;
173 		write(FD, &c, 1);
174 		break;
175 
176 	default:
177 		write(FD, &c, 1);
178 		read(FD, &c, 1);
179 	}
180 }
181 
182 static void
183 sigALRM()
184 {
185 	printf("\07timeout waiting for reply\n");
186 	timeout = 1;
187 	longjmp(timeoutbuf, 1);
188 }
189 
190 static int
191 gobble(match, response)
192 	char match;
193 	char response[];
194 {
195 	char *cp = response;
196 	sig_t f;
197 	char c;
198 
199 	f = signal(SIGALRM, sigALRM);
200 	timeout = 0;
201 	do {
202 		if (setjmp(timeoutbuf)) {
203 			signal(SIGALRM, f);
204 			*cp = '\0';
205 			return (0);
206 		}
207 		alarm(number(value(DIALTIMEOUT)));
208 		read(FD, cp, 1);
209 		alarm(0);
210 		c = (*cp++ &= 0177);
211 #ifdef notdef
212 		if (boolean(value(VERBOSE)))
213 			putchar(c);
214 #endif
215 	} while (c != '\n' && c != match);
216 	signal(SIGALRM, SIG_DFL);
217 	*cp = '\0';
218 	return (c == match);
219 }
220 
221 #define min(a,b)	((a)>(b)?(b):(a))
222 /*
223  * This convoluted piece of code attempts to get
224  * the ventel in sync.  If you don't have FIONREAD
225  * there are gory ways to simulate this.
226  */
227 static int
228 vensync(fd)
229 {
230 	int already = 0, nread;
231 	char buf[60];
232 
233 	/*
234 	 * Toggle DTR to force anyone off that might have left
235 	 * the modem connected, and insure a consistent state
236 	 * to start from.
237 	 *
238 	 * If you don't have the ioctl calls to diddle directly
239 	 * with DTR, you can always try setting the baud rate to 0.
240 	 */
241 	ioctl(FD, TIOCCDTR, 0);
242 	sleep(1);
243 	ioctl(FD, TIOCSDTR, 0);
244 	while (already < MAXRETRY) {
245 		/*
246 		 * After reseting the modem, send it two \r's to
247 		 * autobaud on. Make sure to delay between them
248 		 * so the modem can frame the incoming characters.
249 		 */
250 		write(fd, "\r", 1);
251 		delay(1,10);
252 		write(fd, "\r", 1);
253 		sleep(2);
254 		if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
255 			perror("tip: ioctl");
256 			continue;
257 		}
258 		while (nread > 0) {
259 			read(fd, buf, min(nread, 60));
260 			if ((buf[nread - 1] & 0177) == '$')
261 				return (1);
262 			nread -= min(nread, 60);
263 		}
264 		sleep(1);
265 		already++;
266 	}
267 	return (0);
268 }
269 
270