1*ffe34450Schristos /* $NetBSD: biz22.c,v 1.13 2006/12/14 17:09:43 christos Exp $ */
239801cccSjtc
361f28255Scgd /*
439801cccSjtc * Copyright (c) 1983, 1993
539801cccSjtc * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
1589aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32e37283e1Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3439801cccSjtc #if 0
3539801cccSjtc static char sccsid[] = "@(#)biz22.c 8.1 (Berkeley) 6/6/93";
3639801cccSjtc #endif
37*ffe34450Schristos __RCSID("$NetBSD: biz22.c,v 1.13 2006/12/14 17:09:43 christos Exp $");
3861f28255Scgd #endif /* not lint */
3961f28255Scgd
4061f28255Scgd #include "tip.h"
4161f28255Scgd
4261f28255Scgd #define DISCONNECT_CMD "\20\04" /* disconnection string */
4361f28255Scgd
44e37283e1Slukem static int btimeout = 0;
4561f28255Scgd static jmp_buf timeoutbuf;
4661f28255Scgd
4758c2151fSperry static int biz_dialer(char *, const char *);
4858c2151fSperry static int cmd(const char *);
4958c2151fSperry static int detect(const char *);
5058c2151fSperry static void sigALRM(int);
5189dcb48dScgd
5261f28255Scgd /*
5361f28255Scgd * Dial up on a BIZCOMP Model 1022 with either
5461f28255Scgd * tone dialing (mod = "V")
5561f28255Scgd * pulse dialing (mod = "W")
5661f28255Scgd */
5761f28255Scgd static int
biz_dialer(char * num,const char * mod)5858c2151fSperry biz_dialer(char *num, const char *mod)
5961f28255Scgd {
60e37283e1Slukem int connected = 0;
6161f28255Scgd char cbuf[40];
6261f28255Scgd
6361f28255Scgd if (boolean(value(VERBOSE)))
64*ffe34450Schristos (void)printf("\nstarting call...");
6561f28255Scgd /*
6661f28255Scgd * Disable auto-answer and configure for tone/pulse
6761f28255Scgd * dialing
6861f28255Scgd */
6961f28255Scgd if (cmd("\02K\r")) {
70*ffe34450Schristos (void)printf("can't initialize bizcomp...");
7161f28255Scgd return (0);
7261f28255Scgd }
7353fddda6Smrg (void)strncpy(cbuf, "\02.\r", sizeof(cbuf) - 1);
7461f28255Scgd cbuf[1] = *mod;
7561f28255Scgd if (cmd(cbuf)) {
76*ffe34450Schristos (void)printf("can't set dialing mode...");
7761f28255Scgd return (0);
7861f28255Scgd }
7953fddda6Smrg (void)snprintf(cbuf, sizeof cbuf, "\02D%s\r", num);
80*ffe34450Schristos (void)write(FD, cbuf, strlen(cbuf));
8161f28255Scgd if (!detect("7\r")) {
82*ffe34450Schristos (void)printf("can't get dial tone...");
8361f28255Scgd return (0);
8461f28255Scgd }
8561f28255Scgd if (boolean(value(VERBOSE)))
86*ffe34450Schristos (void)printf("ringing...");
8761f28255Scgd /*
8861f28255Scgd * The reply from the BIZCOMP should be:
8961f28255Scgd * 2 \r or 7 \r failure
9061f28255Scgd * 1 \r success
9161f28255Scgd */
9261f28255Scgd connected = detect("1\r");
93e37283e1Slukem if (btimeout)
9461f28255Scgd biz22_disconnect(); /* insurance */
9561f28255Scgd return (connected);
9661f28255Scgd }
9761f28255Scgd
98e37283e1Slukem int
99*ffe34450Schristos /*ARGSUSED*/
biz22w_dialer(char * num,char * acu __unused)100*ffe34450Schristos biz22w_dialer(char *num, char *acu __unused)
10161f28255Scgd {
10261f28255Scgd
10361f28255Scgd return (biz_dialer(num, "W"));
10461f28255Scgd }
10561f28255Scgd
106e37283e1Slukem int
107*ffe34450Schristos /*ARGSUSED*/
biz22f_dialer(char * num,char * acu __unused)108*ffe34450Schristos biz22f_dialer(char *num, char *acu __unused)
10961f28255Scgd {
11061f28255Scgd
11161f28255Scgd return (biz_dialer(num, "V"));
11261f28255Scgd }
11361f28255Scgd
114e37283e1Slukem void
biz22_disconnect(void)11558c2151fSperry biz22_disconnect(void)
11661f28255Scgd {
11761f28255Scgd
118*ffe34450Schristos (void)write(FD, DISCONNECT_CMD, 4);
119*ffe34450Schristos (void)sleep(2);
120*ffe34450Schristos (void)tcflush(FD, TCIOFLUSH);
12161f28255Scgd }
12261f28255Scgd
123e37283e1Slukem void
biz22_abort(void)12458c2151fSperry biz22_abort(void)
12561f28255Scgd {
12661f28255Scgd
127*ffe34450Schristos (void)write(FD, "\02", 1);
12861f28255Scgd }
12961f28255Scgd
13061f28255Scgd static void
131*ffe34450Schristos /*ARGSUSED*/
sigALRM(int dummy __unused)132*ffe34450Schristos sigALRM(int dummy __unused)
13361f28255Scgd {
13461f28255Scgd
135e37283e1Slukem btimeout = 1;
13661f28255Scgd longjmp(timeoutbuf, 1);
13761f28255Scgd }
13861f28255Scgd
13961f28255Scgd static int
cmd(const char * s)14058c2151fSperry cmd(const char *s)
14161f28255Scgd {
14261f28255Scgd sig_t f;
14361f28255Scgd char c;
14461f28255Scgd
145*ffe34450Schristos (void)write(FD, s, strlen(s));
14661f28255Scgd f = signal(SIGALRM, sigALRM);
14761f28255Scgd if (setjmp(timeoutbuf)) {
14861f28255Scgd biz22_abort();
149*ffe34450Schristos (void)signal(SIGALRM, f);
15061f28255Scgd return (1);
15161f28255Scgd }
152*ffe34450Schristos (void)alarm((unsigned)number(value(DIALTIMEOUT)));
153*ffe34450Schristos (void)read(FD, &c, 1);
154*ffe34450Schristos (void)alarm(0);
155*ffe34450Schristos (void)signal(SIGALRM, f);
15661f28255Scgd c &= 0177;
15761f28255Scgd return (c != '\r');
15861f28255Scgd }
15961f28255Scgd
16061f28255Scgd static int
detect(const char * volatile s)161239d1ed1Schristos detect(const char * volatile s)
16261f28255Scgd {
16361f28255Scgd sig_t f;
16461f28255Scgd char c;
16561f28255Scgd
16661f28255Scgd f = signal(SIGALRM, sigALRM);
167e37283e1Slukem btimeout = 0;
16861f28255Scgd while (*s) {
16961f28255Scgd if (setjmp(timeoutbuf)) {
17061f28255Scgd biz22_abort();
17161f28255Scgd break;
17261f28255Scgd }
173*ffe34450Schristos (void)alarm((unsigned)number(value(DIALTIMEOUT)));
174*ffe34450Schristos (void)read(FD, &c, 1);
175*ffe34450Schristos (void)alarm(0);
17661f28255Scgd c &= 0177;
17761f28255Scgd if (c != *s++)
17861f28255Scgd return (0);
17961f28255Scgd }
180*ffe34450Schristos (void)signal(SIGALRM, f);
181e37283e1Slukem return (btimeout == 0);
18261f28255Scgd }
183