1*ffe34450Schristos /* $NetBSD: courier.c,v 1.17 2006/12/14 17:09:43 christos Exp $ */
239801cccSjtc
361f28255Scgd /*
439801cccSjtc * Copyright (c) 1986, 1993
539801cccSjtc * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
1589aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32e37283e1Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3439801cccSjtc #if 0
3539801cccSjtc static char sccsid[] = "@(#)courier.c 8.1 (Berkeley) 6/6/93";
3639801cccSjtc #endif
37*ffe34450Schristos __RCSID("$NetBSD: courier.c,v 1.17 2006/12/14 17:09:43 christos Exp $");
3861f28255Scgd #endif /* not lint */
3961f28255Scgd
4061f28255Scgd /*
4161f28255Scgd * Routines for calling up on a Courier modem.
4261f28255Scgd * Derived from Hayes driver.
4361f28255Scgd */
4461f28255Scgd #include "tip.h"
4561f28255Scgd
4661f28255Scgd #define MAXRETRY 5
4761f28255Scgd
4861f28255Scgd static int timeout = 0;
4961f28255Scgd static int connected = 0;
50e37283e1Slukem static jmp_buf timeoutbuf;
5161f28255Scgd
5258c2151fSperry static int cour_connect(void);
5358c2151fSperry static void cour_nap(void);
5458c2151fSperry static void cour_napx(int);
5558c2151fSperry static int cour_swallow(const char *);
5658c2151fSperry static int coursync(void);
57e37283e1Slukem #ifdef DEBUG
5858c2151fSperry static void cour_verbose_read(void);
59e37283e1Slukem #endif
6058c2151fSperry static void cour_write(int, const char *, int);
6158c2151fSperry static void sigALRM(int);
62e37283e1Slukem
63e37283e1Slukem int
cour_dialer(char * num,char * acu)6458c2151fSperry cour_dialer(char *num, char *acu)
6561f28255Scgd {
66e37283e1Slukem char *cp;
67258108ceSpk struct termios cntrl;
6861f28255Scgd
6961f28255Scgd if (boolean(value(VERBOSE)))
70*ffe34450Schristos (void)printf("Using \"%s\"\n", acu);
7161f28255Scgd
72*ffe34450Schristos (void)tcgetattr(FD, &cntrl);
73258108ceSpk cntrl.c_cflag |= HUPCL;
74*ffe34450Schristos (void)tcsetattr(FD, TCSAFLUSH, &cntrl);
7561f28255Scgd /*
7661f28255Scgd * Get in synch.
7761f28255Scgd */
7861f28255Scgd if (!coursync()) {
7961f28255Scgd badsynch:
80*ffe34450Schristos (void)printf("can't synchronize with courier\n");
8161f28255Scgd return (0);
8261f28255Scgd }
8361f28255Scgd cour_write(FD, "AT E0\r", 6); /* turn off echoing */
84*ffe34450Schristos (void)sleep(1);
8561f28255Scgd #ifdef DEBUG
8661f28255Scgd if (boolean(value(VERBOSE)))
8739801cccSjtc cour_verbose_read();
8861f28255Scgd #endif
89*ffe34450Schristos (void)tcflush(FD, TCIOFLUSH);
9061f28255Scgd cour_write(FD, "AT C1 E0 H0 Q0 X6 V1\r", 21);
9161f28255Scgd if (!cour_swallow("\r\nOK\r\n"))
9261f28255Scgd goto badsynch;
93*ffe34450Schristos (void)fflush(stdout);
9461f28255Scgd cour_write(FD, "AT D", 4);
9561f28255Scgd for (cp = num; *cp; cp++)
9661f28255Scgd if (*cp == '=')
9761f28255Scgd *cp = ',';
98*ffe34450Schristos cour_write(FD, num, (int)strlen(num));
9961f28255Scgd cour_write(FD, "\r", 1);
10061f28255Scgd connected = cour_connect();
10161f28255Scgd if (timeout)
10261f28255Scgd cour_disconnect();
10361f28255Scgd return (connected);
10461f28255Scgd }
10561f28255Scgd
106e37283e1Slukem void
cour_disconnect(void)10758c2151fSperry cour_disconnect(void)
10861f28255Scgd {
10997eafd50Smrg
11061f28255Scgd /* first hang up the modem*/
111*ffe34450Schristos (void)ioctl(FD, TIOCCDTR, 0);
112*ffe34450Schristos (void)sleep(1);
113*ffe34450Schristos (void)ioctl(FD, TIOCSDTR, 0);
114*ffe34450Schristos (void)coursync(); /* reset */
115*ffe34450Schristos (void)close(FD);
11661f28255Scgd }
11761f28255Scgd
118e37283e1Slukem void
cour_abort(void)11958c2151fSperry cour_abort(void)
12061f28255Scgd {
12197eafd50Smrg
12261f28255Scgd cour_write(FD, "\r", 1); /* send anything to abort the call */
12361f28255Scgd cour_disconnect();
12461f28255Scgd }
12561f28255Scgd
12661f28255Scgd static void
127*ffe34450Schristos /*ARGSUSED*/
sigALRM(int dummy __unused)128*ffe34450Schristos sigALRM(int dummy __unused)
12961f28255Scgd {
13097eafd50Smrg
131*ffe34450Schristos (void)printf("\07timeout waiting for reply\n");
13261f28255Scgd timeout = 1;
13361f28255Scgd longjmp(timeoutbuf, 1);
13461f28255Scgd }
13561f28255Scgd
13661f28255Scgd static int
cour_swallow(const char * volatile match)137239d1ed1Schristos cour_swallow(const char * volatile match)
13861f28255Scgd {
13961f28255Scgd sig_t f;
14061f28255Scgd char c;
14161f28255Scgd
14261f28255Scgd f = signal(SIGALRM, sigALRM);
14361f28255Scgd timeout = 0;
14461f28255Scgd do {
14561f28255Scgd if (*match =='\0') {
146*ffe34450Schristos (void)signal(SIGALRM, f);
14761f28255Scgd return (1);
14861f28255Scgd }
14961f28255Scgd if (setjmp(timeoutbuf)) {
150*ffe34450Schristos (void)signal(SIGALRM, f);
15161f28255Scgd return (0);
15261f28255Scgd }
153*ffe34450Schristos (void)alarm((unsigned)number(value(DIALTIMEOUT)));
154*ffe34450Schristos (void)read(FD, &c, 1);
155*ffe34450Schristos (void)alarm(0);
15661f28255Scgd c &= 0177;
15761f28255Scgd #ifdef DEBUG
15861f28255Scgd if (boolean(value(VERBOSE)))
159*ffe34450Schristos (void)putchar(c);
16061f28255Scgd #endif
16161f28255Scgd } while (c == *match++);
16261f28255Scgd #ifdef DEBUG
16361f28255Scgd if (boolean(value(VERBOSE)))
164*ffe34450Schristos (void)fflush(stdout);
16561f28255Scgd #endif
166*ffe34450Schristos (void)signal(SIGALRM, SIG_DFL);
16761f28255Scgd return (0);
16861f28255Scgd }
16961f28255Scgd
17061f28255Scgd struct baud_msg {
1714f6045fcSchristos const char *msg;
172*ffe34450Schristos unsigned int baud;
17361f28255Scgd } baud_msg[] = {
174e37283e1Slukem { "", B300 },
175e37283e1Slukem { " 1200", B1200 },
176e37283e1Slukem { " 2400", B2400 },
177e37283e1Slukem { " 9600", B9600 },
178e37283e1Slukem { " 9600/ARQ", B9600 },
179e37283e1Slukem { 0, 0 }
18061f28255Scgd };
18161f28255Scgd
18261f28255Scgd static int
cour_connect(void)18358c2151fSperry cour_connect(void)
18461f28255Scgd {
18561f28255Scgd char c;
186239d1ed1Schristos int volatile nc;
187239d1ed1Schristos int volatile nl;
188239d1ed1Schristos int n;
18961f28255Scgd char dialer_buf[64];
19061f28255Scgd struct baud_msg *bm;
19161f28255Scgd sig_t f;
19261f28255Scgd
19361f28255Scgd if (cour_swallow("\r\n") == 0)
19461f28255Scgd return (0);
19561f28255Scgd f = signal(SIGALRM, sigALRM);
19661f28255Scgd again:
197*ffe34450Schristos (void)memset(dialer_buf, 0, sizeof(dialer_buf));
19861f28255Scgd timeout = 0;
19961f28255Scgd for (nc = 0, nl = sizeof(dialer_buf) - 1 ; nl > 0 ; nc++, nl--) {
20061f28255Scgd if (setjmp(timeoutbuf))
20161f28255Scgd break;
202*ffe34450Schristos (void)alarm((unsigned)number(value(DIALTIMEOUT)));
20361f28255Scgd n = read(FD, &c, 1);
204*ffe34450Schristos (void)alarm(0);
20561f28255Scgd if (n <= 0)
20661f28255Scgd break;
20761f28255Scgd c &= 0x7f;
20861f28255Scgd if (c == '\r') {
20961f28255Scgd if (cour_swallow("\n") == 0)
21061f28255Scgd break;
21161f28255Scgd if (!dialer_buf[0])
21261f28255Scgd goto again;
21361f28255Scgd if (strcmp(dialer_buf, "RINGING") == 0 &&
21461f28255Scgd boolean(value(VERBOSE))) {
21561f28255Scgd #ifdef DEBUG
216*ffe34450Schristos (void)printf("%s\r\n", dialer_buf);
21761f28255Scgd #endif
21861f28255Scgd goto again;
21961f28255Scgd }
22061f28255Scgd if (strncmp(dialer_buf, "CONNECT",
22161f28255Scgd sizeof("CONNECT")-1) != 0)
22261f28255Scgd break;
223e267ad3fScgd for (bm = baud_msg ; bm->msg ; bm++)
22461f28255Scgd if (strcmp(bm->msg,
22561f28255Scgd dialer_buf+sizeof("CONNECT")-1) == 0) {
226258108ceSpk struct termios cntrl;
227258108ceSpk
228*ffe34450Schristos (void)tcgetattr(FD, &cntrl);
229*ffe34450Schristos (void)cfsetospeed(&cntrl, bm->baud);
230*ffe34450Schristos (void)cfsetispeed(&cntrl, bm->baud);
231*ffe34450Schristos (void)tcsetattr(FD, TCSAFLUSH, &cntrl);
232*ffe34450Schristos (void)signal(SIGALRM, f);
23361f28255Scgd #ifdef DEBUG
23461f28255Scgd if (boolean(value(VERBOSE)))
235*ffe34450Schristos (void)printf("%s\r\n", dialer_buf);
23661f28255Scgd #endif
23761f28255Scgd return (1);
23861f28255Scgd }
23961f28255Scgd break;
24061f28255Scgd }
24161f28255Scgd dialer_buf[nc] = c;
24261f28255Scgd #ifdef notdef
24361f28255Scgd if (boolean(value(VERBOSE)))
244*ffe34450Schristos (void)putchar(c);
24561f28255Scgd #endif
24661f28255Scgd }
247*ffe34450Schristos (void)printf("%s\r\n", dialer_buf);
248*ffe34450Schristos (void)signal(SIGALRM, f);
24961f28255Scgd return (0);
25061f28255Scgd }
25161f28255Scgd
25261f28255Scgd /*
25361f28255Scgd * This convoluted piece of code attempts to get
25461f28255Scgd * the courier in sync.
25561f28255Scgd */
25661f28255Scgd static int
coursync(void)25758c2151fSperry coursync(void)
25861f28255Scgd {
25961f28255Scgd int already = 0;
26061f28255Scgd int len;
26161f28255Scgd char buf[40];
26261f28255Scgd
26361f28255Scgd while (already++ < MAXRETRY) {
264*ffe34450Schristos (void)tcflush(FD, TCIOFLUSH);
26561f28255Scgd cour_write(FD, "\rAT Z\r", 6); /* reset modem */
266*ffe34450Schristos (void)memset(buf, 0, sizeof(buf));
267*ffe34450Schristos (void)sleep(1);
268*ffe34450Schristos (void)ioctl(FD, FIONREAD, &len);
26961f28255Scgd if (len) {
27061f28255Scgd len = read(FD, buf, sizeof(buf));
27161f28255Scgd #ifdef DEBUG
27261f28255Scgd buf[len] = '\0';
273*ffe34450Schristos (void)printf("coursync: (\"%s\")\n\r", buf);
27461f28255Scgd #endif
275e37283e1Slukem if (strchr(buf, '0') ||
276e37283e1Slukem (strchr(buf, 'O') && strchr(buf, 'K')))
27761f28255Scgd return(1);
27861f28255Scgd }
27961f28255Scgd /*
28061f28255Scgd * If not strapped for DTR control,
28161f28255Scgd * try to get command mode.
28261f28255Scgd */
283*ffe34450Schristos (void)sleep(1);
28461f28255Scgd cour_write(FD, "+++", 3);
285*ffe34450Schristos (void)sleep(1);
28661f28255Scgd /*
28761f28255Scgd * Toggle DTR to force anyone off that might have left
28861f28255Scgd * the modem connected.
28961f28255Scgd */
290*ffe34450Schristos (void)ioctl(FD, TIOCCDTR, 0);
291*ffe34450Schristos (void)sleep(1);
292*ffe34450Schristos (void)ioctl(FD, TIOCSDTR, 0);
29361f28255Scgd }
29461f28255Scgd cour_write(FD, "\rAT Z\r", 6);
29561f28255Scgd return (0);
29661f28255Scgd }
29761f28255Scgd
298e37283e1Slukem static void
cour_write(int fd,const char * cp,int n)29958c2151fSperry cour_write(int fd, const char *cp, int n)
30061f28255Scgd {
30197eafd50Smrg
30261f28255Scgd #ifdef notdef
30361f28255Scgd if (boolean(value(VERBOSE)))
304*ffe34450Schristos (void)write(1, cp, n);
30561f28255Scgd #endif
306*ffe34450Schristos (void)tcdrain(fd);
30761f28255Scgd cour_nap();
30861f28255Scgd for ( ; n-- ; cp++) {
309*ffe34450Schristos (void)write(fd, cp, 1);
310*ffe34450Schristos (void)tcdrain(fd);
31161f28255Scgd cour_nap();
31261f28255Scgd }
31361f28255Scgd }
31461f28255Scgd
31561f28255Scgd #ifdef DEBUG
316e37283e1Slukem static void
cour_verbose_read(void)31758c2151fSperry cour_verbose_read(void)
31861f28255Scgd {
31961f28255Scgd int n = 0;
32061f28255Scgd char buf[BUFSIZ];
32161f28255Scgd
32261f28255Scgd if (ioctl(FD, FIONREAD, &n) < 0)
32361f28255Scgd return;
32461f28255Scgd if (n <= 0)
32561f28255Scgd return;
32661f28255Scgd if (read(FD, buf, n) != n)
32761f28255Scgd return;
328*ffe34450Schristos (void)write(1, buf, n);
32961f28255Scgd }
33061f28255Scgd #endif
33161f28255Scgd
3324dadb162Schristos #define setsa(sa, a) \
333*ffe34450Schristos sa.sa_handler = a; (void)sigemptyset(&sa.sa_mask); sa.sa_flags = 0
33461f28255Scgd
3359de74469Smrg static int napms = 50; /* Give the courier 50 milliseconds between characters */
33661f28255Scgd
33761f28255Scgd static int ringring;
33861f28255Scgd
339e37283e1Slukem void
cour_nap(void)34058c2151fSperry cour_nap(void)
34161f28255Scgd {
34261f28255Scgd
34361f28255Scgd struct itimerval itv, oitv;
344e37283e1Slukem struct itimerval *itp = &itv;
3454dadb162Schristos struct sigaction sa, osa;
3464dadb162Schristos sigset_t sm, osm;
34761f28255Scgd
34861f28255Scgd timerclear(&itp->it_interval);
34961f28255Scgd timerclear(&itp->it_value);
35061f28255Scgd if (setitimer(ITIMER_REAL, itp, &oitv) < 0)
35161f28255Scgd return;
3524dadb162Schristos
353*ffe34450Schristos (void)sigemptyset(&sm);
354*ffe34450Schristos (void)sigaddset(&sm, SIGALRM);
3554dadb162Schristos (void)sigprocmask(SIG_BLOCK, &sm, &osm);
3564dadb162Schristos
35761f28255Scgd itp->it_value.tv_sec = napms/1000;
35861f28255Scgd itp->it_value.tv_usec = ((napms%1000)*1000);
3594dadb162Schristos
3604dadb162Schristos setsa(sa, cour_napx);
3614dadb162Schristos (void)sigaction(SIGALRM, &sa, &osa);
3624dadb162Schristos
3634dadb162Schristos (void)setitimer(ITIMER_REAL, itp, NULL);
3644dadb162Schristos
3654dadb162Schristos sm = osm;
366*ffe34450Schristos (void)sigdelset(&sm, SIGALRM);
3674dadb162Schristos
3684dadb162Schristos for (ringring = 0; !ringring; )
369*ffe34450Schristos (void)sigsuspend(&sm);
3704dadb162Schristos
3714dadb162Schristos (void)sigaction(SIGALRM, &osa, NULL);
3724dadb162Schristos (void)setitimer(ITIMER_REAL, &oitv, NULL);
3734dadb162Schristos (void)sigprocmask(SIG_SETMASK, &osm, NULL);
37461f28255Scgd }
37561f28255Scgd
37661f28255Scgd static void
377*ffe34450Schristos /*ARGSUSED*/
cour_napx(int dummy __unused)378*ffe34450Schristos cour_napx(int dummy __unused)
37961f28255Scgd {
38097eafd50Smrg
38161f28255Scgd ringring = 1;
38261f28255Scgd }
383