xref: /freebsd/usr.bin/tip/libacu/biz22.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
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  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Copyright (c) 1983, 1993
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #endif /* not lint */
38 
39 #include "tip.h"
40 
41 #define DISCONNECT_CMD	"\20\04"	/* disconnection string */
42 
43 static	int dialtimeout = 0;
44 static	jmp_buf timeoutbuf;
45 
46 static int	biz_dialer(char *, char *);
47 static void	sigALRM(int);
48 static int	cmd(char *);
49 static int	detect(char *);
50 
51 /*
52  * Dial up on a BIZCOMP Model 1022 with either
53  * 	tone dialing (mod = "V")
54  *	pulse dialing (mod = "W")
55  */
56 static int
57 biz_dialer(char *num, char *mod)
58 {
59 	int connected = 0;
60 	char cbuf[40];
61 
62 	if (boolean(value(VERBOSE)))
63 		printf("\nstarting call...");
64 	/*
65 	 * Disable auto-answer and configure for tone/pulse
66 	 *  dialing
67 	 */
68 	if (cmd("\02K\r")) {
69 		printf("can't initialize bizcomp...");
70 		return (0);
71 	}
72 	(void)strlcpy(cbuf, "\02.\r", sizeof cbuf);
73 	cbuf[1] = *mod;
74 	if (cmd(cbuf)) {
75 		printf("can't set dialing mode...");
76 		return (0);
77 	}
78 	(void)snprintf(cbuf, sizeof(cbuf), "\02D%s\r", num);
79 	write(FD, cbuf, strlen(cbuf));
80 	if (!detect("7\r")) {
81 		printf("can't get dial tone...");
82 		return (0);
83 	}
84 	if (boolean(value(VERBOSE)))
85 		printf("ringing...");
86 	/*
87 	 * The reply from the BIZCOMP should be:
88 	 *	2 \r or 7 \r	failure
89 	 *	1 \r		success
90 	 */
91 	connected = detect("1\r");
92 #ifdef ACULOG
93 	if (dialtimeout) {
94 		char line[80];
95 
96 		(void)snprintf(line, sizeof line, "%ld second dial timeout",
97 			number(value(DIALTIMEOUT)));
98 		logent(value(HOST), num, "biz1022", line);
99 	}
100 #endif
101 	if (dialtimeout)
102 		biz22_disconnect();	/* insurance */
103 	return (connected);
104 }
105 
106 int
107 biz22w_dialer(char *num, char *acu)
108 {
109 	return (biz_dialer(num, "W"));
110 }
111 
112 int
113 biz22f_dialer(char *num, char *acu)
114 {
115 	return (biz_dialer(num, "V"));
116 }
117 
118 void
119 biz22_disconnect(void)
120 {
121 	write(FD, DISCONNECT_CMD, sizeof(DISCONNECT_CMD)-1);
122 	sleep(2);
123 	tcflush(FD, TCIOFLUSH);
124 }
125 
126 void
127 biz22_abort(void)
128 {
129 	write(FD, "\02", 1);
130 }
131 
132 /*ARGSUSED*/
133 static void
134 sigALRM(int signo)
135 {
136 	dialtimeout = 1;
137 	longjmp(timeoutbuf, 1);
138 }
139 
140 static int
141 cmd(char *s)
142 {
143 	sig_t f;
144 	char c;
145 
146 	write(FD, s, strlen(s));
147 	f = signal(SIGALRM, sigALRM);
148 	if (setjmp(timeoutbuf)) {
149 		biz22_abort();
150 		signal(SIGALRM, f);
151 		return (1);
152 	}
153 	alarm(number(value(DIALTIMEOUT)));
154 	read(FD, &c, 1);
155 	alarm(0);
156 	signal(SIGALRM, f);
157 	c &= 0177;
158 	return (c != '\r');
159 }
160 
161 static int
162 detect(char *s)
163 {
164 	sig_t f;
165 	char c;
166 
167 	f = signal(SIGALRM, sigALRM);
168 	dialtimeout = 0;
169 	while (*s) {
170 		if (setjmp(timeoutbuf)) {
171 			biz22_abort();
172 			break;
173 		}
174 		alarm(number(value(DIALTIMEOUT)));
175 		read(FD, &c, 1);
176 		alarm(0);
177 		c &= 0177;
178 		if (c != *s++)
179 			return (0);
180 	}
181 	signal(SIGALRM, f);
182 	return (dialtimeout == 0);
183 }
184