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