Home | History | Annotate | Line # | Download | only in aculib
v3451.c revision 1.13
      1 /*	$NetBSD: v3451.c,v 1.13 2006/12/14 14:18:04 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[] = "@(#)v3451.c	8.1 (Berkeley) 6/6/93";
     36 #endif
     37 __RCSID("$NetBSD: v3451.c,v 1.13 2006/12/14 14:18:04 christos 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 		return (0);
     72 	}
     73 	tcgetattr(FD, &cntrl);
     74 	term.c_cflag |= HUPCL;
     75 	tcsetattr(FD, TCSANOW, &cntrl);
     76 	sleep(1);
     77 	vawrite("D\r", 2 + slow);
     78 	if (!expect("NUMBER?")) {
     79 		printf("Vadic will not accept dial command\n");
     80 		return (0);
     81 	}
     82 	snprintf(phone, sizeof phone, "%s\r", num);
     83 	vawrite(phone, 1 + slow);
     84 	if (!expect(phone)) {
     85 		printf("Vadic will not accept phone number\n");
     86 		return (0);
     87 	}
     88 	func = signal(SIGINT,SIG_IGN);
     89 	/*
     90 	 * You cannot interrupt the Vadic when its dialing;
     91 	 * even dropping DTR does not work (definitely a
     92 	 * brain damaged design).
     93 	 */
     94 	vawrite("\r", 1 + slow);
     95 	vawrite("\r", 1 + slow);
     96 	if (!expect("DIALING:")) {
     97 		printf("Vadic failed to dial\n");
     98 		return (0);
     99 	}
    100 	if (boolean(value(VERBOSE)))
    101 		printf("\ndialing...");
    102 	ok = expect("ON LINE");
    103 	signal(SIGINT, func);
    104 	if (!ok) {
    105 		printf("call failed\n");
    106 		return (0);
    107 	}
    108 	tcflush(FD, TCIOFLUSH);
    109 	return (1);
    110 }
    111 
    112 void
    113 v3451_disconnect(void)
    114 {
    115 
    116 	close(FD);
    117 }
    118 
    119 void
    120 v3451_abort(void)
    121 {
    122 
    123 	close(FD);
    124 }
    125 
    126 static void
    127 vawrite(const char *cp, int delay)
    128 {
    129 
    130 	for (; *cp; sleep(delay), cp++)
    131 		write(FD, cp, 1);
    132 }
    133 
    134 static int
    135 expect(const char *cp)
    136 {
    137 	char buf[300];
    138 	char * volatile rp;
    139 	int volatile timeout;
    140 	int volatile online;
    141 
    142 	rp = buf;
    143 	timeout = 30;
    144 	online = 0;
    145 
    146 	if (strcmp(cp, "\"\"") == 0)
    147 		return (1);
    148 	*rp = 0;
    149 	/*
    150 	 * If we are waiting for the Vadic to complete
    151 	 * dialing and get a connection, allow more time
    152 	 * Unfortunately, the Vadic times out 24 seconds after
    153 	 * the last digit is dialed
    154 	 */
    155 	online = strcmp(cp, "ON LINE") == 0;
    156 	if (online)
    157 		timeout = number(value(DIALTIMEOUT));
    158 	signal(SIGALRM, alarmtr);
    159 	if (setjmp(Sjbuf))
    160 		return (0);
    161 	alarm(timeout);
    162 	while (notin(cp, buf) && rp < buf + sizeof (buf) - 1) {
    163 		if (online && notin("FAILED CALL", buf) == 0)
    164 			return (0);
    165 		if (read(FD, rp, 1) < 0) {
    166 			alarm(0);
    167 			return (0);
    168 		}
    169 		if (*rp &= 0177)
    170 			rp++;
    171 		*rp = '\0';
    172 	}
    173 	alarm(0);
    174 	return (1);
    175 }
    176 
    177 static void
    178 alarmtr(int dummy)
    179 {
    180 
    181 	longjmp(Sjbuf, 1);
    182 }
    183 
    184 static int
    185 notin(const char *sh, char *lg)
    186 {
    187 
    188 	for (; *lg; lg++)
    189 		if (prefix(sh, lg))
    190 			return (0);
    191 	return (1);
    192 }
    193 
    194 static int
    195 prefix(const char *s1, char *s2)
    196 {
    197 	char c;
    198 
    199 	while ((c = *s1++) == *s2++)
    200 		if (c == '\0')
    201 			return (1);
    202 	return (c == '\0');
    203 }
    204