xref: /original-bsd/usr.bin/tip/aculib/courier.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)courier.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 /*
13  * Routines for calling up on a Courier modem.
14  * Derived from Hayes driver.
15  */
16 #include "tip.h"
17 #include <stdio.h>
18 
19 #define	MAXRETRY	5
20 
21 static	void sigALRM();
22 static	int timeout = 0;
23 static	int connected = 0;
24 static	jmp_buf timeoutbuf, intbuf;
25 static	int coursync();
26 
27 cour_dialer(num, acu)
28 	register char *num;
29 	char *acu;
30 {
31 	register char *cp;
32 #ifdef ACULOG
33 	char line[80];
34 #endif
35 	static int cour_connect(), cour_swallow();
36 
37 	if (boolean(value(VERBOSE)))
38 		printf("Using \"%s\"\n", acu);
39 
40 	ioctl(FD, TIOCHPCL, 0);
41 	/*
42 	 * Get in synch.
43 	 */
44 	if (!coursync()) {
45 badsynch:
46 		printf("can't synchronize with courier\n");
47 #ifdef ACULOG
48 		logent(value(HOST), num, "courier", "can't synch up");
49 #endif
50 		return (0);
51 	}
52 	cour_write(FD, "AT E0\r", 6);	/* turn off echoing */
53 	sleep(1);
54 #ifdef DEBUG
55 	if (boolean(value(VERBOSE)))
56 		cour_verbose_read();
57 #endif
58 	ioctl(FD, TIOCFLUSH, 0);	/* flush any clutter */
59 	cour_write(FD, "AT C1 E0 H0 Q0 X6 V1\r", 21);
60 	if (!cour_swallow("\r\nOK\r\n"))
61 		goto badsynch;
62 	fflush(stdout);
63 	cour_write(FD, "AT D", 4);
64 	for (cp = num; *cp; cp++)
65 		if (*cp == '=')
66 			*cp = ',';
67 	cour_write(FD, num, strlen(num));
68 	cour_write(FD, "\r", 1);
69 	connected = cour_connect();
70 #ifdef ACULOG
71 	if (timeout) {
72 		sprintf(line, "%d second dial timeout",
73 			number(value(DIALTIMEOUT)));
74 		logent(value(HOST), num, "cour", line);
75 	}
76 #endif
77 	if (timeout)
78 		cour_disconnect();
79 	return (connected);
80 }
81 
82 cour_disconnect()
83 {
84 	 /* first hang up the modem*/
85 	ioctl(FD, TIOCCDTR, 0);
86 	sleep(1);
87 	ioctl(FD, TIOCSDTR, 0);
88 	coursync();				/* reset */
89 	close(FD);
90 }
91 
92 cour_abort()
93 {
94 	cour_write(FD, "\r", 1);	/* send anything to abort the call */
95 	cour_disconnect();
96 }
97 
98 static void
99 sigALRM()
100 {
101 	printf("\07timeout waiting for reply\n");
102 	timeout = 1;
103 	longjmp(timeoutbuf, 1);
104 }
105 
106 static int
107 cour_swallow(match)
108   register char *match;
109   {
110 	sig_t f;
111 	char c;
112 
113 	f = signal(SIGALRM, sigALRM);
114 	timeout = 0;
115 	do {
116 		if (*match =='\0') {
117 			signal(SIGALRM, f);
118 			return (1);
119 		}
120 		if (setjmp(timeoutbuf)) {
121 			signal(SIGALRM, f);
122 			return (0);
123 		}
124 		alarm(number(value(DIALTIMEOUT)));
125 		read(FD, &c, 1);
126 		alarm(0);
127 		c &= 0177;
128 #ifdef DEBUG
129 		if (boolean(value(VERBOSE)))
130 			putchar(c);
131 #endif
132 	} while (c == *match++);
133 #ifdef DEBUG
134 	if (boolean(value(VERBOSE)))
135 		fflush(stdout);
136 #endif
137 	signal(SIGALRM, SIG_DFL);
138 	return (0);
139 }
140 
141 struct baud_msg {
142 	char *msg;
143 	int baud;
144 } baud_msg[] = {
145 	"",		B300,
146 	" 1200",	B1200,
147 	" 2400",	B2400,
148 	" 9600",	B9600,
149 	" 9600/ARQ",	B9600,
150 	0,		0,
151 };
152 
153 static int
154 cour_connect()
155 {
156 	char c;
157 	int nc, nl, n;
158 	struct sgttyb sb;
159 	char dialer_buf[64];
160 	struct baud_msg *bm;
161 	sig_t f;
162 
163 	if (cour_swallow("\r\n") == 0)
164 		return (0);
165 	f = signal(SIGALRM, sigALRM);
166 again:
167 	nc = 0; nl = sizeof(dialer_buf)-1;
168 	bzero(dialer_buf, sizeof(dialer_buf));
169 	timeout = 0;
170 	for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) {
171 		if (setjmp(timeoutbuf))
172 			break;
173 		alarm(number(value(DIALTIMEOUT)));
174 		n = read(FD, &c, 1);
175 		alarm(0);
176 		if (n <= 0)
177 			break;
178 		c &= 0x7f;
179 		if (c == '\r') {
180 			if (cour_swallow("\n") == 0)
181 				break;
182 			if (!dialer_buf[0])
183 				goto again;
184 			if (strcmp(dialer_buf, "RINGING") == 0 &&
185 			    boolean(value(VERBOSE))) {
186 #ifdef DEBUG
187 				printf("%s\r\n", dialer_buf);
188 #endif
189 				goto again;
190 			}
191 			if (strncmp(dialer_buf, "CONNECT",
192 				    sizeof("CONNECT")-1) != 0)
193 				break;
194 			for (bm = baud_msg ; bm->msg ; bm++)
195 				if (strcmp(bm->msg,
196 				    dialer_buf+sizeof("CONNECT")-1) == 0) {
197 					if (ioctl(FD, TIOCGETP, &sb) < 0) {
198 						perror("TIOCGETP");
199 						goto error;
200 					}
201 					sb.sg_ispeed = sb.sg_ospeed = bm->baud;
202 					if (ioctl(FD, TIOCSETP, &sb) < 0) {
203 						perror("TIOCSETP");
204 						goto error;
205 					}
206 					signal(SIGALRM, f);
207 #ifdef DEBUG
208 					if (boolean(value(VERBOSE)))
209 						printf("%s\r\n", dialer_buf);
210 #endif
211 					return (1);
212 				}
213 			break;
214 		}
215 		dialer_buf[nc] = c;
216 #ifdef notdef
217 		if (boolean(value(VERBOSE)))
218 			putchar(c);
219 #endif
220 	}
221 error1:
222 	printf("%s\r\n", dialer_buf);
223 error:
224 	signal(SIGALRM, f);
225 	return (0);
226 }
227 
228 /*
229  * This convoluted piece of code attempts to get
230  * the courier in sync.
231  */
232 static int
233 coursync()
234 {
235 	int already = 0;
236 	int len;
237 	char buf[40];
238 
239 	while (already++ < MAXRETRY) {
240 		ioctl(FD, TIOCFLUSH, 0);	/* flush any clutter */
241 		cour_write(FD, "\rAT Z\r", 6);	/* reset modem */
242 		bzero(buf, sizeof(buf));
243 		sleep(1);
244 		ioctl(FD, FIONREAD, &len);
245 		if (len) {
246 			len = read(FD, buf, sizeof(buf));
247 #ifdef DEBUG
248 			buf[len] = '\0';
249 			printf("coursync: (\"%s\")\n\r", buf);
250 #endif
251 			if (index(buf, '0') ||
252 		   	   (index(buf, 'O') && index(buf, 'K')))
253 				return(1);
254 		}
255 		/*
256 		 * If not strapped for DTR control,
257 		 * try to get command mode.
258 		 */
259 		sleep(1);
260 		cour_write(FD, "+++", 3);
261 		sleep(1);
262 		/*
263 		 * Toggle DTR to force anyone off that might have left
264 		 * the modem connected.
265 		 */
266 		ioctl(FD, TIOCCDTR, 0);
267 		sleep(1);
268 		ioctl(FD, TIOCSDTR, 0);
269 	}
270 	cour_write(FD, "\rAT Z\r", 6);
271 	return (0);
272 }
273 
274 cour_write(fd, cp, n)
275 int fd;
276 char *cp;
277 int n;
278 {
279 	struct sgttyb sb;
280 #ifdef notdef
281 	if (boolean(value(VERBOSE)))
282 		write(1, cp, n);
283 #endif
284 	ioctl(fd, TIOCGETP, &sb);
285 	ioctl(fd, TIOCSETP, &sb);
286 	cour_nap();
287 	for ( ; n-- ; cp++) {
288 		write(fd, cp, 1);
289 		ioctl(fd, TIOCGETP, &sb);
290 		ioctl(fd, TIOCSETP, &sb);
291 		cour_nap();
292 	}
293 }
294 
295 #ifdef DEBUG
296 cour_verbose_read()
297 {
298 	int n = 0;
299 	char buf[BUFSIZ];
300 
301 	if (ioctl(FD, FIONREAD, &n) < 0)
302 		return;
303 	if (n <= 0)
304 		return;
305 	if (read(FD, buf, n) != n)
306 		return;
307 	write(1, buf, n);
308 }
309 #endif
310 
311 /*
312  * Code stolen from /usr/src/lib/libc/gen/sleep.c
313  */
314 #define mask(s) (1<<((s)-1))
315 #define setvec(vec, a) \
316         vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0
317 
318 static napms = 50; /* Give the courier 50 milliseconds between characters */
319 
320 static int ringring;
321 
322 cour_nap()
323 {
324 
325         static void cour_napx();
326 	int omask;
327         struct itimerval itv, oitv;
328         register struct itimerval *itp = &itv;
329         struct sigvec vec, ovec;
330 
331         timerclear(&itp->it_interval);
332         timerclear(&itp->it_value);
333         if (setitimer(ITIMER_REAL, itp, &oitv) < 0)
334                 return;
335         setvec(ovec, SIG_DFL);
336         omask = sigblock(mask(SIGALRM));
337         itp->it_value.tv_sec = napms/1000;
338 	itp->it_value.tv_usec = ((napms%1000)*1000);
339         setvec(vec, cour_napx);
340         ringring = 0;
341         (void) sigvec(SIGALRM, &vec, &ovec);
342         (void) setitimer(ITIMER_REAL, itp, (struct itimerval *)0);
343         while (!ringring)
344                 sigpause(omask &~ mask(SIGALRM));
345         (void) sigvec(SIGALRM, &ovec, (struct sigvec *)0);
346         (void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0);
347 	(void) sigsetmask(omask);
348 }
349 
350 static void
351 cour_napx()
352 {
353         ringring = 1;
354 }
355