1 /*        $NetBSD: biz22.c,v 1.13 2006/12/14 17:09:43 christos 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.13 2006/12/14 17:09:43 christos 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
biz_dialer(char * num,const char * mod)58 biz_dialer(char *num, const char *mod)
59 {
60           int connected = 0;
61           char cbuf[40];
62 
63           if (boolean(value(VERBOSE)))
64                     (void)printf("\nstarting call...");
65           /*
66            * Disable auto-answer and configure for tone/pulse
67            *  dialing
68            */
69           if (cmd("\02K\r")) {
70                     (void)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                     (void)printf("can't set dialing mode...");
77                     return (0);
78           }
79           (void)snprintf(cbuf, sizeof cbuf, "\02D%s\r", num);
80           (void)write(FD, cbuf, strlen(cbuf));
81           if (!detect("7\r")) {
82                     (void)printf("can't get dial tone...");
83                     return (0);
84           }
85           if (boolean(value(VERBOSE)))
86                     (void)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           if (btimeout)
94                     biz22_disconnect(); /* insurance */
95           return (connected);
96 }
97 
98 int
99 /*ARGSUSED*/
biz22w_dialer(char * num,char * acu __unused)100 biz22w_dialer(char *num, char *acu __unused)
101 {
102 
103           return (biz_dialer(num, "W"));
104 }
105 
106 int
107 /*ARGSUSED*/
biz22f_dialer(char * num,char * acu __unused)108 biz22f_dialer(char *num, char *acu __unused)
109 {
110 
111           return (biz_dialer(num, "V"));
112 }
113 
114 void
biz22_disconnect(void)115 biz22_disconnect(void)
116 {
117 
118           (void)write(FD, DISCONNECT_CMD, 4);
119           (void)sleep(2);
120           (void)tcflush(FD, TCIOFLUSH);
121 }
122 
123 void
biz22_abort(void)124 biz22_abort(void)
125 {
126 
127           (void)write(FD, "\02", 1);
128 }
129 
130 static void
131 /*ARGSUSED*/
sigALRM(int dummy __unused)132 sigALRM(int dummy __unused)
133 {
134 
135           btimeout = 1;
136           longjmp(timeoutbuf, 1);
137 }
138 
139 static int
cmd(const char * s)140 cmd(const char *s)
141 {
142           sig_t f;
143           char c;
144 
145           (void)write(FD, s, strlen(s));
146           f = signal(SIGALRM, sigALRM);
147           if (setjmp(timeoutbuf)) {
148                     biz22_abort();
149                     (void)signal(SIGALRM, f);
150                     return (1);
151           }
152           (void)alarm((unsigned)number(value(DIALTIMEOUT)));
153           (void)read(FD, &c, 1);
154           (void)alarm(0);
155           (void)signal(SIGALRM, f);
156           c &= 0177;
157           return (c != '\r');
158 }
159 
160 static int
detect(const char * volatile s)161 detect(const char * volatile s)
162 {
163           sig_t f;
164           char c;
165 
166           f = signal(SIGALRM, sigALRM);
167           btimeout = 0;
168           while (*s) {
169                     if (setjmp(timeoutbuf)) {
170                               biz22_abort();
171                               break;
172                     }
173                     (void)alarm((unsigned)number(value(DIALTIMEOUT)));
174                     (void)read(FD, &c, 1);
175                     (void)alarm(0);
176                     c &= 0177;
177                     if (c != *s++)
178                               return (0);
179           }
180           (void)signal(SIGALRM, f);
181           return (btimeout == 0);
182 }
183