xref: /netbsd-src/usr.bin/tip/aculib/courier.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: courier.c,v 1.4 1994/12/08 09:31:35 jtc Exp $	*/
2 
3 /*
4  * Copyright (c) 1986, 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)courier.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 static char rcsid[] = "$NetBSD: courier.c,v 1.4 1994/12/08 09:31:35 jtc Exp $";
41 #endif /* not lint */
42 
43 /*
44  * Routines for calling up on a Courier modem.
45  * Derived from Hayes driver.
46  */
47 #include "tip.h"
48 #include <stdio.h>
49 
50 #define	MAXRETRY	5
51 
52 static	void sigALRM();
53 static	int timeout = 0;
54 static	int connected = 0;
55 static	jmp_buf timeoutbuf, intbuf;
56 static	int coursync();
57 
58 cour_dialer(num, acu)
59 	register char *num;
60 	char *acu;
61 {
62 	register char *cp;
63 #ifdef ACULOG
64 	char line[80];
65 #endif
66 	static int cour_connect(), cour_swallow();
67 
68 	if (boolean(value(VERBOSE)))
69 		printf("Using \"%s\"\n", acu);
70 
71 	ioctl(FD, TIOCHPCL, 0);
72 	/*
73 	 * Get in synch.
74 	 */
75 	if (!coursync()) {
76 badsynch:
77 		printf("can't synchronize with courier\n");
78 #ifdef 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 #ifdef 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 	struct sgttyb sb;
190 	char dialer_buf[64];
191 	struct baud_msg *bm;
192 	sig_t f;
193 
194 	if (cour_swallow("\r\n") == 0)
195 		return (0);
196 	f = signal(SIGALRM, sigALRM);
197 again:
198 	nc = 0; nl = sizeof(dialer_buf)-1;
199 	bzero(dialer_buf, sizeof(dialer_buf));
200 	timeout = 0;
201 	for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) {
202 		if (setjmp(timeoutbuf))
203 			break;
204 		alarm(number(value(DIALTIMEOUT)));
205 		n = read(FD, &c, 1);
206 		alarm(0);
207 		if (n <= 0)
208 			break;
209 		c &= 0x7f;
210 		if (c == '\r') {
211 			if (cour_swallow("\n") == 0)
212 				break;
213 			if (!dialer_buf[0])
214 				goto again;
215 			if (strcmp(dialer_buf, "RINGING") == 0 &&
216 			    boolean(value(VERBOSE))) {
217 #ifdef DEBUG
218 				printf("%s\r\n", dialer_buf);
219 #endif
220 				goto again;
221 			}
222 			if (strncmp(dialer_buf, "CONNECT",
223 				    sizeof("CONNECT")-1) != 0)
224 				break;
225 			for (bm = baud_msg ; bm->msg ; bm++)
226 				if (strcmp(bm->msg,
227 				    dialer_buf+sizeof("CONNECT")-1) == 0) {
228 					if (ioctl(FD, TIOCGETP, &sb) < 0) {
229 						perror("TIOCGETP");
230 						goto error;
231 					}
232 					sb.sg_ispeed = sb.sg_ospeed = bm->baud;
233 					if (ioctl(FD, TIOCSETP, &sb) < 0) {
234 						perror("TIOCSETP");
235 						goto error;
236 					}
237 					signal(SIGALRM, f);
238 #ifdef DEBUG
239 					if (boolean(value(VERBOSE)))
240 						printf("%s\r\n", dialer_buf);
241 #endif
242 					return (1);
243 				}
244 			break;
245 		}
246 		dialer_buf[nc] = c;
247 #ifdef notdef
248 		if (boolean(value(VERBOSE)))
249 			putchar(c);
250 #endif
251 	}
252 error1:
253 	printf("%s\r\n", dialer_buf);
254 error:
255 	signal(SIGALRM, f);
256 	return (0);
257 }
258 
259 /*
260  * This convoluted piece of code attempts to get
261  * the courier in sync.
262  */
263 static int
264 coursync()
265 {
266 	int already = 0;
267 	int len;
268 	char buf[40];
269 
270 	while (already++ < MAXRETRY) {
271 		ioctl(FD, TIOCFLUSH, 0);	/* flush any clutter */
272 		cour_write(FD, "\rAT Z\r", 6);	/* reset modem */
273 		bzero(buf, sizeof(buf));
274 		sleep(1);
275 		ioctl(FD, FIONREAD, &len);
276 		if (len) {
277 			len = read(FD, buf, sizeof(buf));
278 #ifdef DEBUG
279 			buf[len] = '\0';
280 			printf("coursync: (\"%s\")\n\r", buf);
281 #endif
282 			if (index(buf, '0') ||
283 		   	   (index(buf, 'O') && index(buf, 'K')))
284 				return(1);
285 		}
286 		/*
287 		 * If not strapped for DTR control,
288 		 * try to get command mode.
289 		 */
290 		sleep(1);
291 		cour_write(FD, "+++", 3);
292 		sleep(1);
293 		/*
294 		 * Toggle DTR to force anyone off that might have left
295 		 * the modem connected.
296 		 */
297 		ioctl(FD, TIOCCDTR, 0);
298 		sleep(1);
299 		ioctl(FD, TIOCSDTR, 0);
300 	}
301 	cour_write(FD, "\rAT Z\r", 6);
302 	return (0);
303 }
304 
305 cour_write(fd, cp, n)
306 int fd;
307 char *cp;
308 int n;
309 {
310 	struct sgttyb sb;
311 #ifdef notdef
312 	if (boolean(value(VERBOSE)))
313 		write(1, cp, n);
314 #endif
315 	ioctl(fd, TIOCGETP, &sb);
316 	ioctl(fd, TIOCSETP, &sb);
317 	cour_nap();
318 	for ( ; n-- ; cp++) {
319 		write(fd, cp, 1);
320 		ioctl(fd, TIOCGETP, &sb);
321 		ioctl(fd, TIOCSETP, &sb);
322 		cour_nap();
323 	}
324 }
325 
326 #ifdef DEBUG
327 cour_verbose_read()
328 {
329 	int n = 0;
330 	char buf[BUFSIZ];
331 
332 	if (ioctl(FD, FIONREAD, &n) < 0)
333 		return;
334 	if (n <= 0)
335 		return;
336 	if (read(FD, buf, n) != n)
337 		return;
338 	write(1, buf, n);
339 }
340 #endif
341 
342 /*
343  * Code stolen from /usr/src/lib/libc/gen/sleep.c
344  */
345 #define mask(s) (1<<((s)-1))
346 #define setvec(vec, a) \
347         vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0
348 
349 static napms = 50; /* Give the courier 50 milliseconds between characters */
350 
351 static int ringring;
352 
353 cour_nap()
354 {
355 
356         static void cour_napx();
357 	int omask;
358         struct itimerval itv, oitv;
359         register struct itimerval *itp = &itv;
360         struct sigvec vec, ovec;
361 
362         timerclear(&itp->it_interval);
363         timerclear(&itp->it_value);
364         if (setitimer(ITIMER_REAL, itp, &oitv) < 0)
365                 return;
366         setvec(ovec, SIG_DFL);
367         omask = sigblock(mask(SIGALRM));
368         itp->it_value.tv_sec = napms/1000;
369 	itp->it_value.tv_usec = ((napms%1000)*1000);
370         setvec(vec, cour_napx);
371         ringring = 0;
372         (void) sigvec(SIGALRM, &vec, &ovec);
373         (void) setitimer(ITIMER_REAL, itp, (struct itimerval *)0);
374         while (!ringring)
375                 sigpause(omask &~ mask(SIGALRM));
376         (void) sigvec(SIGALRM, &ovec, (struct sigvec *)0);
377         (void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0);
378 	(void) sigsetmask(omask);
379 }
380 
381 static void
382 cour_napx()
383 {
384         ringring = 1;
385 }
386