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