xref: /freebsd/usr.bin/tip/libacu/biz22.c (revision d391708c)
1 /*	$OpenBSD: biz22.c,v 1.13 2006/03/17 19:17:13 moritz Exp $	*/
2 /*	$NetBSD: biz22.c,v 1.6 1997/02/11 09:24:11 mrg Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 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 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)biz22.c	8.1 (Berkeley) 6/6/93";
36 #endif
37 static const char rcsid[] = "$OpenBSD: biz22.c,v 1.13 2006/03/17 19:17:13 moritz Exp $";
38 #endif /* not lint */
39 
40 #include "tip.h"
41 
42 #define DISCONNECT_CMD	"\20\04"	/* disconnection string */
43 
44 static	int dialtimeout = 0;
45 static	jmp_buf timeoutbuf;
46 
47 static int	biz_dialer(char *, char *);
48 static void	sigALRM(int);
49 static int	cmd(char *);
50 static int	detect(char *);
51 
52 /*
53  * Dial up on a BIZCOMP Model 1022 with either
54  * 	tone dialing (mod = "V")
55  *	pulse dialing (mod = "W")
56  */
57 static int
58 biz_dialer(char *num, char *mod)
59 {
60 	int connected = 0;
61 	char cbuf[40];
62 
63 	if (boolean(value(VERBOSE)))
64 		printf("\nstarting call...");
65 	/*
66 	 * Disable auto-answer and configure for tone/pulse
67 	 *  dialing
68 	 */
69 	if (cmd("\02K\r")) {
70 		printf("can't initialize bizcomp...");
71 		return (0);
72 	}
73 	(void)strlcpy(cbuf, "\02.\r", sizeof cbuf);
74 	cbuf[1] = *mod;
75 	if (cmd(cbuf)) {
76 		printf("can't set dialing mode...");
77 		return (0);
78 	}
79 	(void)snprintf(cbuf, sizeof(cbuf), "\02D%s\r", num);
80 	write(FD, cbuf, strlen(cbuf));
81 	if (!detect("7\r")) {
82 		printf("can't get dial tone...");
83 		return (0);
84 	}
85 	if (boolean(value(VERBOSE)))
86 		printf("ringing...");
87 	/*
88 	 * The reply from the BIZCOMP should be:
89 	 *	2 \r or 7 \r	failure
90 	 *	1 \r		success
91 	 */
92 	connected = detect("1\r");
93 #ifdef ACULOG
94 	if (dialtimeout) {
95 		char line[80];
96 
97 		(void)snprintf(line, sizeof line, "%ld second dial timeout",
98 			number(value(DIALTIMEOUT)));
99 		logent(value(HOST), num, "biz1022", line);
100 	}
101 #endif
102 	if (dialtimeout)
103 		biz22_disconnect();	/* insurance */
104 	return (connected);
105 }
106 
107 int
108 biz22w_dialer(char *num, char *acu)
109 {
110 	return (biz_dialer(num, "W"));
111 }
112 
113 int
114 biz22f_dialer(char *num, char *acu)
115 {
116 	return (biz_dialer(num, "V"));
117 }
118 
119 void
120 biz22_disconnect(void)
121 {
122 	write(FD, DISCONNECT_CMD, sizeof(DISCONNECT_CMD)-1);
123 	sleep(2);
124 	tcflush(FD, TCIOFLUSH);
125 }
126 
127 void
128 biz22_abort(void)
129 {
130 	write(FD, "\02", 1);
131 }
132 
133 /*ARGSUSED*/
134 static void
135 sigALRM(int signo)
136 {
137 	dialtimeout = 1;
138 	longjmp(timeoutbuf, 1);
139 }
140 
141 static int
142 cmd(char *s)
143 {
144 	sig_t f;
145 	char c;
146 
147 	write(FD, s, strlen(s));
148 	f = signal(SIGALRM, sigALRM);
149 	if (setjmp(timeoutbuf)) {
150 		biz22_abort();
151 		signal(SIGALRM, f);
152 		return (1);
153 	}
154 	alarm(number(value(DIALTIMEOUT)));
155 	read(FD, &c, 1);
156 	alarm(0);
157 	signal(SIGALRM, f);
158 	c &= 0177;
159 	return (c != '\r');
160 }
161 
162 static int
163 detect(char *s)
164 {
165 	sig_t f;
166 	char c;
167 
168 	f = signal(SIGALRM, sigALRM);
169 	dialtimeout = 0;
170 	while (*s) {
171 		if (setjmp(timeoutbuf)) {
172 			biz22_abort();
173 			break;
174 		}
175 		alarm(number(value(DIALTIMEOUT)));
176 		read(FD, &c, 1);
177 		alarm(0);
178 		c &= 0177;
179 		if (c != *s++)
180 			return (0);
181 	}
182 	signal(SIGALRM, f);
183 	return (dialtimeout == 0);
184 }
185