xref: /freebsd/usr.bin/tip/libacu/courier.c (revision 6adf353a56a161443406b44a45d00c688ca7b857)
1 /*
2  * Copyright (c) 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char rcsid[] =
36   "$FreeBSD$";
37 #endif
38 
39 /*
40  * Routines for calling up on a Courier modem.
41  * Derived from Hayes driver.
42  */
43 #include "tipconf.h"
44 #include "tip.h"
45 #include "acucommon.h"
46 #include <stdio.h>
47 #include <unistd.h>
48 
49 #define	MAXRETRY	5
50 
51 static	void sigALRM();
52 static	int timeout = 0;
53 static	int connected = 0;
54 static	jmp_buf timeoutbuf, intbuf;
55 static	int coursync();
56 
57 cour_dialer(num, acu)
58 	register char *num;
59 	char *acu;
60 {
61 	register char *cp;
62 #if ACULOG
63 	char line[80];
64 #endif
65 	static int cour_connect(), cour_swallow();
66 
67 	if (boolean(value(VERBOSE)))
68 		printf("Using \"%s\"\n", acu);
69 
70 	acu_hupcl ();
71 
72 	/*
73 	 * Get in synch.
74 	 */
75 	if (!coursync()) {
76 badsynch:
77 		printf("can't synchronize with courier\n");
78 #if ACULOG
79 		logent(value(HOST), num, "courier", "can't synch up");
80 #endif
81 		return (0);
82 	}
83 	cour_write(FD, "AT E0\r", 6);	/* turn off echoing */
84 	sleep(1);
85 #ifdef DEBUG
86 	if (boolean(value(VERBOSE)))
87 		cour_verbose_read();
88 #endif
89 	ioctl(FD, TIOCFLUSH, 0);	/* flush any clutter */
90 	cour_write(FD, "AT C1 E0 H0 Q0 X6 V1\r", 21);
91 	if (!cour_swallow("\r\nOK\r\n"))
92 		goto badsynch;
93 	fflush(stdout);
94 	cour_write(FD, "AT D", 4);
95 	for (cp = num; *cp; cp++)
96 		if (*cp == '=')
97 			*cp = ',';
98 	cour_write(FD, num, strlen(num));
99 	cour_write(FD, "\r", 1);
100 	connected = cour_connect();
101 #if ACULOG
102 	if (timeout) {
103 		sprintf(line, "%d second dial timeout",
104 			number(value(DIALTIMEOUT)));
105 		logent(value(HOST), num, "cour", line);
106 	}
107 #endif
108 	if (timeout)
109 		cour_disconnect();
110 	return (connected);
111 }
112 
113 cour_disconnect()
114 {
115 	 /* first hang up the modem*/
116 	ioctl(FD, TIOCCDTR, 0);
117 	sleep(1);
118 	ioctl(FD, TIOCSDTR, 0);
119 	coursync();				/* reset */
120 	close(FD);
121 }
122 
123 cour_abort()
124 {
125 	cour_write(FD, "\r", 1);	/* send anything to abort the call */
126 	cour_disconnect();
127 }
128 
129 static void
130 sigALRM()
131 {
132 	printf("\07timeout waiting for reply\n");
133 	timeout = 1;
134 	longjmp(timeoutbuf, 1);
135 }
136 
137 static int
138 cour_swallow(match)
139   register char *match;
140   {
141 	sig_t f;
142 	char c;
143 
144 	f = signal(SIGALRM, sigALRM);
145 	timeout = 0;
146 	do {
147 		if (*match =='\0') {
148 			signal(SIGALRM, f);
149 			return (1);
150 		}
151 		if (setjmp(timeoutbuf)) {
152 			signal(SIGALRM, f);
153 			return (0);
154 		}
155 		alarm(number(value(DIALTIMEOUT)));
156 		read(FD, &c, 1);
157 		alarm(0);
158 		c &= 0177;
159 #ifdef DEBUG
160 		if (boolean(value(VERBOSE)))
161 			putchar(c);
162 #endif
163 	} while (c == *match++);
164 #ifdef DEBUG
165 	if (boolean(value(VERBOSE)))
166 		fflush(stdout);
167 #endif
168 	signal(SIGALRM, SIG_DFL);
169 	return (0);
170 }
171 
172 struct baud_msg {
173 	char *msg;
174 	int baud;
175 } baud_msg[] = {
176 	"",		B300,
177 	" 1200",	B1200,
178 	" 2400",	B2400,
179 	" 9600",	B9600,
180 	" 9600/ARQ",	B9600,
181 	0,		0,
182 };
183 
184 static int
185 cour_connect()
186 {
187 	char c;
188 	int nc, nl, n;
189 	char dialer_buf[64];
190 	struct baud_msg *bm;
191 	sig_t f;
192 
193 	if (cour_swallow("\r\n") == 0)
194 		return (0);
195 	f = signal(SIGALRM, sigALRM);
196 again:
197 	nc = 0; nl = sizeof(dialer_buf)-1;
198 	bzero(dialer_buf, sizeof(dialer_buf));
199 	timeout = 0;
200 	for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) {
201 		if (setjmp(timeoutbuf))
202 			break;
203 		alarm(number(value(DIALTIMEOUT)));
204 		n = read(FD, &c, 1);
205 		alarm(0);
206 		if (n <= 0)
207 			break;
208 		c &= 0x7f;
209 		if (c == '\r') {
210 			if (cour_swallow("\n") == 0)
211 				break;
212 			if (!dialer_buf[0])
213 				goto again;
214 			if (strcmp(dialer_buf, "RINGING") == 0 &&
215 			    boolean(value(VERBOSE))) {
216 #ifdef DEBUG
217 				printf("%s\r\n", dialer_buf);
218 #endif
219 				goto again;
220 			}
221 			if (strncmp(dialer_buf, "CONNECT",
222 				    sizeof("CONNECT")-1) != 0)
223 				break;
224 			for (bm = baud_msg ; bm->msg ; bm++)
225 				if (strcmp(bm->msg,
226 				    dialer_buf+sizeof("CONNECT")-1) == 0) {
227 					if (!acu_setspeed(bm->baud))
228 						goto error;
229 					signal(SIGALRM, f);
230 #ifdef DEBUG
231 					if (boolean(value(VERBOSE)))
232 						printf("%s\r\n", dialer_buf);
233 #endif
234 					return (1);
235 				}
236 			break;
237 		}
238 		dialer_buf[nc] = c;
239 #ifdef notdef
240 		if (boolean(value(VERBOSE)))
241 			putchar(c);
242 #endif
243 	}
244 error1:
245 	printf("%s\r\n", dialer_buf);
246 error:
247 	signal(SIGALRM, f);
248 	return (0);
249 }
250 
251 /*
252  * This convoluted piece of code attempts to get
253  * the courier in sync.
254  */
255 static int
256 coursync()
257 {
258 	int already = 0;
259 	int len;
260 	char buf[40];
261 
262 	while (already++ < MAXRETRY) {
263 		ioctl(FD, TIOCFLUSH, 0);	/* flush any clutter */
264 		cour_write(FD, "\rAT Z\r", 6);	/* reset modem */
265 		bzero(buf, sizeof(buf));
266 		sleep(1);
267 		ioctl(FD, FIONREAD, &len);
268 		if (len) {
269 			len = read(FD, buf, sizeof(buf));
270 #ifdef DEBUG
271 			buf[len] = '\0';
272 			printf("coursync: (\"%s\")\n\r", buf);
273 #endif
274 			if (index(buf, '0') ||
275 		   	   (index(buf, 'O') && index(buf, 'K')))
276 				return(1);
277 		}
278 		/*
279 		 * If not strapped for DTR control,
280 		 * try to get command mode.
281 		 */
282 		sleep(1);
283 		cour_write(FD, "+++", 3);
284 		sleep(1);
285 		/*
286 		 * Toggle DTR to force anyone off that might have left
287 		 * the modem connected.
288 		 */
289 		ioctl(FD, TIOCCDTR, 0);
290 		sleep(1);
291 		ioctl(FD, TIOCSDTR, 0);
292 	}
293 	cour_write(FD, "\rAT Z\r", 6);
294 	return (0);
295 }
296 
297 cour_write(fd, cp, n)
298 int fd;
299 char *cp;
300 int n;
301 {
302 #ifdef notdef
303 	if (boolean(value(VERBOSE)))
304 		write(STDOUT_FILENO, cp, n);
305 #endif
306 	acu_flush ();
307 	cour_nap();
308 	for ( ; n-- ; cp++) {
309 		write(fd, cp, 1);
310 		acu_flush ();
311 		cour_nap();
312 	}
313 }
314 
315 #ifdef DEBUG
316 cour_verbose_read()
317 {
318 	int n = 0;
319 	char buf[BUFSIZ];
320 
321 	if (ioctl(FD, FIONREAD, &n) < 0)
322 		return;
323 	if (n <= 0)
324 		return;
325 	if (read(FD, buf, n) != n)
326 		return;
327 	write(STDOUT_FILENO, buf, n);
328 }
329 #endif
330 
331 cour_nap()
332 {
333 	acu_nap (50);
334 }
335 
336 /* end of courier.c */
337