xref: /netbsd-src/usr.bin/tip/aculib/ventel.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: ventel.c,v 1.12 2004/11/04 07:29:09 dsl Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)ventel.c	8.1 (Berkeley) 6/6/93";
36 #endif
37 __RCSID("$NetBSD: ventel.c,v 1.12 2004/11/04 07:29:09 dsl Exp $");
38 #endif /* not lint */
39 
40 /*
41  * Routines for calling up on a Ventel Modem
42  * The Ventel is expected to be strapped for local echo (just like uucp)
43  */
44 #include "tip.h"
45 
46 #define	MAXRETRY	5
47 
48 static	int timeout = 0;
49 static	jmp_buf timeoutbuf;
50 
51 static	void	echo __P((const char *));
52 static	int	gobble __P((char, char *));
53 static	void	sigALRM __P((int));
54 static	int	vensync __P((int));
55 
56 /*
57  * some sleep calls have been replaced by usleep(DELAYUS)
58  * because some ventel modems require two <cr>s in less than
59  * a second in order to 'wake up'
60  */
61 #define	DELAYUS		100000		/* delay in microseconds */
62 
63 int
64 ven_dialer(num, acu)
65 	char *num;
66 	char *acu;
67 {
68 	char *cp;
69 	int connected = 0;
70 	char *msg, line[80];
71 	struct termios	cntrl;
72 
73 	/*
74 	 * Get in synch with a couple of carriage returns
75 	 */
76 	if (!vensync(FD)) {
77 		printf("can't synchronize with ventel\n");
78 #ifdef ACULOG
79 		logent(value(HOST), num, "ventel", "can't synch up");
80 #endif
81 		return (0);
82 	}
83 	if (boolean(value(VERBOSE)))
84 		printf("\ndialing...");
85 	fflush(stdout);
86 	tcgetattr(FD, &cntrl);
87 	cntrl.c_cflag |= HUPCL;
88 	tcsetattr(FD, TCSANOW, &cntrl);
89 	echo("#k$\r$\n$D$I$A$L$:$ ");
90 	for (cp = num; *cp; cp++) {
91 		usleep(DELAYUS);
92 		write(FD, cp, 1);
93 	}
94 	usleep(DELAYUS);
95 	write(FD, "\r", 1);
96 	gobble('\n', line);
97 	if (gobble('\n', line))
98 		connected = gobble('!', line);
99 	tcflush(FD, TCIOFLUSH);
100 #ifdef ACULOG
101 	if (timeout) {
102 		(void)snprintf(line, sizeof line, "%d second dial timeout",
103 			(int)number(value(DIALTIMEOUT)));
104 		logent(value(HOST), num, "ventel", line);
105 	}
106 #endif
107 	if (timeout)
108 		ven_disconnect();	/* insurance */
109 	if (connected || timeout || !boolean(value(VERBOSE)))
110 		return (connected);
111 	/* call failed, parse response for user */
112 	cp = strchr(line, '\r');
113 	if (cp)
114 		*cp = '\0';
115 	for (cp = line; (cp = strchr(cp, ' ')) != NULL; cp++)
116 		if (cp[1] == ' ')
117 			break;
118 	if (cp) {
119 		while (*cp == ' ')
120 			cp++;
121 		msg = cp;
122 		while (*cp) {
123 			if (isupper((unsigned char)*cp))
124 				*cp = tolower((unsigned char)*cp);
125 			cp++;
126 		}
127 		printf("%s...", msg);
128 	}
129 	return (connected);
130 }
131 
132 void
133 ven_disconnect()
134 {
135 
136 	close(FD);
137 }
138 
139 void
140 ven_abort()
141 {
142 
143 	write(FD, "\03", 1);
144 	close(FD);
145 }
146 
147 static void
148 echo(s)
149 	const char *s;
150 {
151 	char c;
152 
153 	while ((c = *s++) != 0) switch (c) {
154 
155 	case '$':
156 		read(FD, &c, 1);
157 		s++;
158 		break;
159 
160 	case '#':
161 		c = *s++;
162 		write(FD, &c, 1);
163 		break;
164 
165 	default:
166 		write(FD, &c, 1);
167 		read(FD, &c, 1);
168 	}
169 }
170 
171 static void
172 sigALRM(dummy)
173 	int dummy;
174 {
175 
176 	printf("\07timeout waiting for reply\n");
177 	timeout = 1;
178 	longjmp(timeoutbuf, 1);
179 }
180 
181 static int
182 gobble(match, response)
183 	char match;
184 	char response[];
185 {
186 	char *cp = response;
187 	sig_t f;
188 	char c;
189 
190 #if __GNUC__	/* XXX pacify gcc */
191 	(void)&cp;
192 #endif
193 
194 	f = signal(SIGALRM, sigALRM);
195 	timeout = 0;
196 	do {
197 		if (setjmp(timeoutbuf)) {
198 			signal(SIGALRM, f);
199 			*cp = '\0';
200 			return (0);
201 		}
202 		alarm(number(value(DIALTIMEOUT)));
203 		read(FD, cp, 1);
204 		alarm(0);
205 		c = (*cp++ &= 0177);
206 #ifdef notdef
207 		if (boolean(value(VERBOSE)))
208 			putchar(c);
209 #endif
210 	} while (c != '\n' && c != match);
211 	signal(SIGALRM, SIG_DFL);
212 	*cp = '\0';
213 	return (c == match);
214 }
215 
216 #define min(a,b)	((a)>(b)?(b):(a))
217 /*
218  * This convoluted piece of code attempts to get
219  * the ventel in sync.  If you don't have FIONREAD
220  * there are gory ways to simulate this.
221  */
222 static int
223 vensync(fd)
224 	int fd;
225 {
226 	int already = 0, nread;
227 	char buf[60];
228 
229 	/*
230 	 * Toggle DTR to force anyone off that might have left
231 	 * the modem connected, and insure a consistent state
232 	 * to start from.
233 	 *
234 	 * If you don't have the ioctl calls to diddle directly
235 	 * with DTR, you can always try setting the baud rate to 0.
236 	 */
237 	ioctl(FD, TIOCCDTR, 0);
238 	sleep(1);
239 	ioctl(FD, TIOCSDTR, 0);
240 	while (already < MAXRETRY) {
241 		/*
242 		 * After reseting the modem, send it two \r's to
243 		 * autobaud on. Make sure to delay between them
244 		 * so the modem can frame the incoming characters.
245 		 */
246 		write(fd, "\r", 1);
247 		usleep(DELAYUS);
248 		write(fd, "\r", 1);
249 		sleep(2);
250 		if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
251 			perror("tip: ioctl");
252 			continue;
253 		}
254 		while (nread > 0) {
255 			read(fd, buf, min(nread, sizeof buf));
256 			if ((buf[min(nread, sizeof buf) - 1] & 0177) == '$')
257 				return (1);
258 			nread -= min(nread, 60);
259 		}
260 		sleep(1);
261 		already++;
262 	}
263 	return (0);
264 }
265