1 /* $NetBSD: ventel.c,v 1.9 1998/12/19 23:02:21 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. 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 #include <sys/cdefs.h> 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)ventel.c 8.1 (Berkeley) 6/6/93"; 40 #endif 41 __RCSID("$NetBSD: ventel.c,v 1.9 1998/12/19 23:02:21 christos Exp $"); 42 #endif /* not lint */ 43 44 /* 45 * Routines for calling up on a Ventel Modem 46 * The Ventel is expected to be strapped for local echo (just like uucp) 47 */ 48 #include "tip.h" 49 50 #define MAXRETRY 5 51 52 static int timeout = 0; 53 static jmp_buf timeoutbuf; 54 55 static void echo __P((char *)); 56 static int gobble __P((char, char *)); 57 static void sigALRM __P((int)); 58 static int vensync __P((int)); 59 60 /* 61 * some sleep calls have been replaced by usleep(DELAYUS) 62 * because some ventel modems require two <cr>s in less than 63 * a second in order to 'wake up' 64 */ 65 #define DELAYUS 100000 /* delay in microseconds */ 66 67 int 68 ven_dialer(num, acu) 69 char *num; 70 char *acu; 71 { 72 char *cp; 73 int connected = 0; 74 char *msg, line[80]; 75 struct termios cntrl; 76 77 /* 78 * Get in synch with a couple of carriage returns 79 */ 80 if (!vensync(FD)) { 81 printf("can't synchronize with ventel\n"); 82 #ifdef ACULOG 83 logent(value(HOST), num, "ventel", "can't synch up"); 84 #endif 85 return (0); 86 } 87 if (boolean(value(VERBOSE))) 88 printf("\ndialing..."); 89 fflush(stdout); 90 tcgetattr(FD, &cntrl); 91 cntrl.c_cflag |= HUPCL; 92 tcsetattr(FD, TCSANOW, &cntrl); 93 echo("#k$\r$\n$D$I$A$L$:$ "); 94 for (cp = num; *cp; cp++) { 95 usleep(DELAYUS); 96 write(FD, cp, 1); 97 } 98 usleep(DELAYUS); 99 write(FD, "\r", 1); 100 gobble('\n', line); 101 if (gobble('\n', line)) 102 connected = gobble('!', line); 103 tcflush(FD, TCIOFLUSH); 104 #ifdef ACULOG 105 if (timeout) { 106 (void)snprintf(line, sizeof line, "%d second dial timeout", 107 (int)number(value(DIALTIMEOUT))); 108 logent(value(HOST), num, "ventel", line); 109 } 110 #endif 111 if (timeout) 112 ven_disconnect(); /* insurance */ 113 if (connected || timeout || !boolean(value(VERBOSE))) 114 return (connected); 115 /* call failed, parse response for user */ 116 cp = strchr(line, '\r'); 117 if (cp) 118 *cp = '\0'; 119 for (cp = line; (cp = strchr(cp, ' ')) != NULL; cp++) 120 if (cp[1] == ' ') 121 break; 122 if (cp) { 123 while (*cp == ' ') 124 cp++; 125 msg = cp; 126 while (*cp) { 127 if (isupper((unsigned char)*cp)) 128 *cp = tolower(*cp); 129 cp++; 130 } 131 printf("%s...", msg); 132 } 133 return (connected); 134 } 135 136 void 137 ven_disconnect() 138 { 139 140 close(FD); 141 } 142 143 void 144 ven_abort() 145 { 146 147 write(FD, "\03", 1); 148 close(FD); 149 } 150 151 static void 152 echo(s) 153 char *s; 154 { 155 char c; 156 157 while ((c = *s++) != 0) switch (c) { 158 159 case '$': 160 read(FD, &c, 1); 161 s++; 162 break; 163 164 case '#': 165 c = *s++; 166 write(FD, &c, 1); 167 break; 168 169 default: 170 write(FD, &c, 1); 171 read(FD, &c, 1); 172 } 173 } 174 175 static void 176 sigALRM(dummy) 177 int dummy; 178 { 179 180 printf("\07timeout waiting for reply\n"); 181 timeout = 1; 182 longjmp(timeoutbuf, 1); 183 } 184 185 static int 186 gobble(match, response) 187 char match; 188 char response[]; 189 { 190 char *cp = response; 191 sig_t f; 192 char c; 193 194 #if __GNUC__ /* XXX pacify gcc */ 195 (void)&cp; 196 #endif 197 198 f = signal(SIGALRM, sigALRM); 199 timeout = 0; 200 do { 201 if (setjmp(timeoutbuf)) { 202 signal(SIGALRM, f); 203 *cp = '\0'; 204 return (0); 205 } 206 alarm(number(value(DIALTIMEOUT))); 207 read(FD, cp, 1); 208 alarm(0); 209 c = (*cp++ &= 0177); 210 #ifdef notdef 211 if (boolean(value(VERBOSE))) 212 putchar(c); 213 #endif 214 } while (c != '\n' && c != match); 215 signal(SIGALRM, SIG_DFL); 216 *cp = '\0'; 217 return (c == match); 218 } 219 220 #define min(a,b) ((a)>(b)?(b):(a)) 221 /* 222 * This convoluted piece of code attempts to get 223 * the ventel in sync. If you don't have FIONREAD 224 * there are gory ways to simulate this. 225 */ 226 static int 227 vensync(fd) 228 int fd; 229 { 230 int already = 0, nread; 231 char buf[60]; 232 233 /* 234 * Toggle DTR to force anyone off that might have left 235 * the modem connected, and insure a consistent state 236 * to start from. 237 * 238 * If you don't have the ioctl calls to diddle directly 239 * with DTR, you can always try setting the baud rate to 0. 240 */ 241 ioctl(FD, TIOCCDTR, 0); 242 sleep(1); 243 ioctl(FD, TIOCSDTR, 0); 244 while (already < MAXRETRY) { 245 /* 246 * After reseting the modem, send it two \r's to 247 * autobaud on. Make sure to delay between them 248 * so the modem can frame the incoming characters. 249 */ 250 write(fd, "\r", 1); 251 usleep(DELAYUS); 252 write(fd, "\r", 1); 253 sleep(2); 254 if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) { 255 perror("tip: ioctl"); 256 continue; 257 } 258 while (nread > 0) { 259 read(fd, buf, min(nread, sizeof buf)); 260 if ((buf[min(nread, sizeof buf) - 1] & 0177) == '$') 261 return (1); 262 nread -= min(nread, 60); 263 } 264 sleep(1); 265 already++; 266 } 267 return (0); 268 } 269