biz22.c revision 1.10 1 /* $NetBSD: biz22.c,v 1.10 2006/04/03 00:51:14 perry Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)biz22.c 8.1 (Berkeley) 6/6/93";
36 #endif
37 __RCSID("$NetBSD: biz22.c,v 1.10 2006/04/03 00:51:14 perry Exp $");
38 #endif /* not lint */
39
40 #include "tip.h"
41
42 #define DISCONNECT_CMD "\20\04" /* disconnection string */
43
44 static int btimeout = 0;
45 static jmp_buf timeoutbuf;
46
47 static int biz_dialer(char *, const char *);
48 static int cmd(const char *);
49 static int detect(const char *);
50 static void sigALRM(int);
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, const 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)strncpy(cbuf, "\02.\r", sizeof(cbuf) - 1);
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 (btimeout) {
95 char line[80];
96
97 (void)snprintf(line, sizeof line, "%d second dial timeout",
98 (int)number(value(DIALTIMEOUT)));
99 logent(value(HOST), num, "biz1022", line);
100 }
101 #endif
102 if (btimeout)
103 biz22_disconnect(); /* insurance */
104 return (connected);
105 }
106
107 int
108 biz22w_dialer(char *num, char *acu)
109 {
110
111 return (biz_dialer(num, "W"));
112 }
113
114 int
115 biz22f_dialer(char *num, char *acu)
116 {
117
118 return (biz_dialer(num, "V"));
119 }
120
121 void
122 biz22_disconnect(void)
123 {
124
125 write(FD, DISCONNECT_CMD, 4);
126 sleep(2);
127 tcflush(FD, TCIOFLUSH);
128 }
129
130 void
131 biz22_abort(void)
132 {
133
134 write(FD, "\02", 1);
135 }
136
137 static void
138 sigALRM(int dummy)
139 {
140
141 btimeout = 1;
142 longjmp(timeoutbuf, 1);
143 }
144
145 static int
146 cmd(const char *s)
147 {
148 sig_t f;
149 char c;
150
151 write(FD, s, strlen(s));
152 f = signal(SIGALRM, sigALRM);
153 if (setjmp(timeoutbuf)) {
154 biz22_abort();
155 signal(SIGALRM, f);
156 return (1);
157 }
158 alarm(number(value(DIALTIMEOUT)));
159 read(FD, &c, 1);
160 alarm(0);
161 signal(SIGALRM, f);
162 c &= 0177;
163 return (c != '\r');
164 }
165
166 static int
167 detect(const char *s)
168 {
169 sig_t f;
170 char c;
171
172 #if __GNUC__ /* XXX pacify gcc */
173 (void)&s;
174 #endif
175
176 f = signal(SIGALRM, sigALRM);
177 btimeout = 0;
178 while (*s) {
179 if (setjmp(timeoutbuf)) {
180 biz22_abort();
181 break;
182 }
183 alarm(number(value(DIALTIMEOUT)));
184 read(FD, &c, 1);
185 alarm(0);
186 c &= 0177;
187 if (c != *s++)
188 return (0);
189 }
190 signal(SIGALRM, f);
191 return (btimeout == 0);
192 }
193