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