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