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