Home | History | Annotate | Line # | Download | only in aculib
v3451.c revision 1.11
      1 /*	$NetBSD: v3451.c,v 1.11 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[] = "@(#)v3451.c	8.1 (Berkeley) 6/6/93";
     36 #endif
     37 __RCSID("$NetBSD: v3451.c,v 1.11 2006/04/03 00:51:14 perry Exp $");
     38 #endif /* not lint */
     39 
     40 /*
     41  * Routines for calling up on a Vadic 3451 Modem
     42  */
     43 #include "tip.h"
     44 
     45 static	jmp_buf Sjbuf;
     46 
     47 static	void	alarmtr(int);
     48 static	int	expect(const char *);
     49 static	int	notin(const char *, char *);
     50 static	int	prefix(const char *, char *);
     51 static	void	vawrite(const char *, int);
     52 
     53 int
     54 v3451_dialer(char *num, char *acu)
     55 {
     56 	sig_t func;
     57 	int ok;
     58 	int slow = number(value(BAUDRATE)) < 1200;
     59 	char phone[50];
     60 	struct termios cntrl;
     61 
     62 	/*
     63 	 * Get in synch
     64 	 */
     65 	vawrite("I\r", 1 + slow);
     66 	vawrite("I\r", 1 + slow);
     67 	vawrite("I\r", 1 + slow);
     68 	vawrite("\005\r", 2 + slow);
     69 	if (!expect("READY")) {
     70 		printf("can't synchronize with vadic 3451\n");
     71 #ifdef ACULOG
     72 		logent(value(HOST), num, "vadic", "can't synch up");
     73 #endif
     74 		return (0);
     75 	}
     76 	tcgetattr(FD, &cntrl);
     77 	term.c_cflag |= HUPCL;
     78 	tcsetattr(FD, TCSANOW, &cntrl);
     79 	sleep(1);
     80 	vawrite("D\r", 2 + slow);
     81 	if (!expect("NUMBER?")) {
     82 		printf("Vadic will not accept dial command\n");
     83 #ifdef ACULOG
     84 		logent(value(HOST), num, "vadic", "will not accept dial");
     85 #endif
     86 		return (0);
     87 	}
     88 	snprintf(phone, sizeof phone, "%s\r", num);
     89 	vawrite(phone, 1 + slow);
     90 	if (!expect(phone)) {
     91 		printf("Vadic will not accept phone number\n");
     92 #ifdef ACULOG
     93 		logent(value(HOST), num, "vadic", "will not accept number");
     94 #endif
     95 		return (0);
     96 	}
     97 	func = signal(SIGINT,SIG_IGN);
     98 	/*
     99 	 * You cannot interrupt the Vadic when its dialing;
    100 	 * even dropping DTR does not work (definitely a
    101 	 * brain damaged design).
    102 	 */
    103 	vawrite("\r", 1 + slow);
    104 	vawrite("\r", 1 + slow);
    105 	if (!expect("DIALING:")) {
    106 		printf("Vadic failed to dial\n");
    107 #ifdef ACULOG
    108 		logent(value(HOST), num, "vadic", "failed to dial");
    109 #endif
    110 		return (0);
    111 	}
    112 	if (boolean(value(VERBOSE)))
    113 		printf("\ndialing...");
    114 	ok = expect("ON LINE");
    115 	signal(SIGINT, func);
    116 	if (!ok) {
    117 		printf("call failed\n");
    118 #ifdef ACULOG
    119 		logent(value(HOST), num, "vadic", "call failed");
    120 #endif
    121 		return (0);
    122 	}
    123 	tcflush(FD, TCIOFLUSH);
    124 	return (1);
    125 }
    126 
    127 void
    128 v3451_disconnect(void)
    129 {
    130 
    131 	close(FD);
    132 }
    133 
    134 void
    135 v3451_abort(void)
    136 {
    137 
    138 	close(FD);
    139 }
    140 
    141 static void
    142 vawrite(const char *cp, int delay)
    143 {
    144 
    145 	for (; *cp; sleep(delay), cp++)
    146 		write(FD, cp, 1);
    147 }
    148 
    149 static int
    150 expect(const char *cp)
    151 {
    152 	char buf[300];
    153 	char *rp = buf;
    154 	int timeout = 30, online = 0;
    155 
    156 #if __GNUC__	/* XXX pacify gcc */
    157 	(void)&online;
    158 	(void)&rp;
    159 	(void)&timeout;
    160 #endif
    161 
    162 	if (strcmp(cp, "\"\"") == 0)
    163 		return (1);
    164 	*rp = 0;
    165 	/*
    166 	 * If we are waiting for the Vadic to complete
    167 	 * dialing and get a connection, allow more time
    168 	 * Unfortunately, the Vadic times out 24 seconds after
    169 	 * the last digit is dialed
    170 	 */
    171 	online = strcmp(cp, "ON LINE") == 0;
    172 	if (online)
    173 		timeout = number(value(DIALTIMEOUT));
    174 	signal(SIGALRM, alarmtr);
    175 	if (setjmp(Sjbuf))
    176 		return (0);
    177 	alarm(timeout);
    178 	while (notin(cp, buf) && rp < buf + sizeof (buf) - 1) {
    179 		if (online && notin("FAILED CALL", buf) == 0)
    180 			return (0);
    181 		if (read(FD, rp, 1) < 0) {
    182 			alarm(0);
    183 			return (0);
    184 		}
    185 		if (*rp &= 0177)
    186 			rp++;
    187 		*rp = '\0';
    188 	}
    189 	alarm(0);
    190 	return (1);
    191 }
    192 
    193 static void
    194 alarmtr(int dummy)
    195 {
    196 
    197 	longjmp(Sjbuf, 1);
    198 }
    199 
    200 static int
    201 notin(const char *sh, char *lg)
    202 {
    203 
    204 	for (; *lg; lg++)
    205 		if (prefix(sh, lg))
    206 			return (0);
    207 	return (1);
    208 }
    209 
    210 static int
    211 prefix(const char *s1, char *s2)
    212 {
    213 	char c;
    214 
    215 	while ((c = *s1++) == *s2++)
    216 		if (c == '\0')
    217 			return (1);
    218 	return (c == '\0');
    219 }
    220