Home | History | Annotate | Line # | Download | only in aculib
biz22.c revision 1.6
      1 /*	$NetBSD: biz22.c,v 1.6 1997/02/11 09:24:11 mrg 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)biz22.c	8.1 (Berkeley) 6/6/93";
     39 #endif
     40 static char rcsid[] = "$NetBSD: biz22.c,v 1.6 1997/02/11 09:24:11 mrg Exp $";
     41 #endif /* not lint */
     42 
     43 #include "tip.h"
     44 
     45 #define DISCONNECT_CMD	"\20\04"	/* disconnection string */
     46 
     47 static	void sigALRM();
     48 static	int timeout = 0;
     49 static	jmp_buf timeoutbuf;
     50 
     51 static int cmd(), detect();
     52 
     53 /*
     54  * Dial up on a BIZCOMP Model 1022 with either
     55  * 	tone dialing (mod = "V")
     56  *	pulse dialing (mod = "W")
     57  */
     58 static int
     59 biz_dialer(num, mod)
     60 	char *num, *mod;
     61 {
     62 	register int connected = 0;
     63 	char cbuf[40];
     64 
     65 	if (boolean(value(VERBOSE)))
     66 		printf("\nstarting call...");
     67 	/*
     68 	 * Disable auto-answer and configure for tone/pulse
     69 	 *  dialing
     70 	 */
     71 	if (cmd("\02K\r")) {
     72 		printf("can't initialize bizcomp...");
     73 		return (0);
     74 	}
     75 	(void)strncpy(cbuf, "\02.\r", sizeof(cbuf) - 1);
     76 	cbuf[1] = *mod;
     77 	if (cmd(cbuf)) {
     78 		printf("can't set dialing mode...");
     79 		return (0);
     80 	}
     81 	(void)snprintf(cbuf, sizeof cbuf, "\02D%s\r", num);
     82 	write(FD, cbuf, strlen(cbuf));
     83 	if (!detect("7\r")) {
     84 		printf("can't get dial tone...");
     85 		return (0);
     86 	}
     87 	if (boolean(value(VERBOSE)))
     88 		printf("ringing...");
     89 	/*
     90 	 * The reply from the BIZCOMP should be:
     91 	 *	2 \r or 7 \r	failure
     92 	 *	1 \r		success
     93 	 */
     94 	connected = detect("1\r");
     95 #ifdef ACULOG
     96 	if (timeout) {
     97 		char line[80];
     98 
     99 		(void)snprintf(line, sizeof line, "%d second dial timeout",
    100 			number(value(DIALTIMEOUT)));
    101 		logent(value(HOST), num, "biz1022", line);
    102 	}
    103 #endif
    104 	if (timeout)
    105 		biz22_disconnect();	/* insurance */
    106 	return (connected);
    107 }
    108 
    109 biz22w_dialer(num, acu)
    110 	char *num, *acu;
    111 {
    112 
    113 	return (biz_dialer(num, "W"));
    114 }
    115 
    116 biz22f_dialer(num, acu)
    117 	char *num, *acu;
    118 {
    119 
    120 	return (biz_dialer(num, "V"));
    121 }
    122 
    123 biz22_disconnect()
    124 {
    125 	int rw = 2;
    126 
    127 	write(FD, DISCONNECT_CMD, 4);
    128 	sleep(2);
    129 	tcflush(FD, TCIOFLUSH);
    130 }
    131 
    132 biz22_abort()
    133 {
    134 
    135 	write(FD, "\02", 1);
    136 }
    137 
    138 static void
    139 sigALRM()
    140 {
    141 
    142 	timeout = 1;
    143 	longjmp(timeoutbuf, 1);
    144 }
    145 
    146 static int
    147 cmd(s)
    148 	register char *s;
    149 {
    150 	sig_t f;
    151 	char c;
    152 
    153 	write(FD, s, strlen(s));
    154 	f = signal(SIGALRM, sigALRM);
    155 	if (setjmp(timeoutbuf)) {
    156 		biz22_abort();
    157 		signal(SIGALRM, f);
    158 		return (1);
    159 	}
    160 	alarm(number(value(DIALTIMEOUT)));
    161 	read(FD, &c, 1);
    162 	alarm(0);
    163 	signal(SIGALRM, f);
    164 	c &= 0177;
    165 	return (c != '\r');
    166 }
    167 
    168 static int
    169 detect(s)
    170 	register char *s;
    171 {
    172 	sig_t f;
    173 	char c;
    174 
    175 	f = signal(SIGALRM, sigALRM);
    176 	timeout = 0;
    177 	while (*s) {
    178 		if (setjmp(timeoutbuf)) {
    179 			biz22_abort();
    180 			break;
    181 		}
    182 		alarm(number(value(DIALTIMEOUT)));
    183 		read(FD, &c, 1);
    184 		alarm(0);
    185 		c &= 0177;
    186 		if (c != *s++)
    187 			return (0);
    188 	}
    189 	signal(SIGALRM, f);
    190 	return (timeout == 0);
    191 }
    192