1 /* $OpenBSD: ventel.c,v 1.12 2006/03/17 19:17:13 moritz Exp $ */ 2 /* $NetBSD: ventel.c,v 1.6 1997/02/11 09:24:21 mrg Exp $ */ 3 4 /*- 5 * SPDX-License-Identifier: BSD-3-Clause 6 * 7 * Copyright (c) 1983, 1993 8 * The Regents of the University of California. All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #ifndef lint 37 #endif /* not lint */ 38 39 /* 40 * Routines for calling up on a Ventel Modem 41 * The Ventel is expected to be strapped for local echo (just like uucp) 42 */ 43 #include "tip.h" 44 #include <termios.h> 45 #include <sys/ioctl.h> 46 47 #define MAXRETRY 5 48 49 static int dialtimeout = 0; 50 static jmp_buf timeoutbuf; 51 52 static void echo(char *); 53 static void sigALRM(int); 54 static int gobble(char, char *); 55 static int vensync(int); 56 57 /* 58 * some sleep calls have been replaced by this macro 59 * because some ventel modems require two <cr>s in less than 60 * a second in order to 'wake up'... yes, it is dirty... 61 */ 62 #define delay(num,denom) busyloop(CPUSPEED*num/denom) 63 #define CPUSPEED 1000000 /* VAX 780 is 1MIPS */ 64 #define DELAY(n) do { long N = (n); while (--N > 0); } while (0) 65 #define busyloop(n) do { DELAY(n); } while (0) 66 67 int 68 ven_dialer(char *num, char *acu) 69 { 70 char *cp; 71 int connected = 0; 72 char *msg, line[80]; 73 struct termios cntrl; 74 75 /* 76 * Get in synch with a couple of carriage returns 77 */ 78 if (!vensync(FD)) { 79 printf("can't synchronize with ventel\n"); 80 #ifdef ACULOG 81 logent(value(HOST), num, "ventel", "can't synch up"); 82 #endif 83 return (0); 84 } 85 if (boolean(value(VERBOSE))) 86 printf("\ndialing..."); 87 fflush(stdout); 88 tcgetattr(FD, &cntrl); 89 cntrl.c_cflag |= HUPCL; 90 tcsetattr(FD, TCSANOW, &cntrl); 91 echo("#k$\r$\n$D$I$A$L$:$ "); 92 for (cp = num; *cp; cp++) { 93 delay(1, 10); 94 write(FD, cp, 1); 95 } 96 delay(1, 10); 97 write(FD, "\r", 1); 98 gobble('\n', line); 99 if (gobble('\n', line)) 100 connected = gobble('!', line); 101 tcflush(FD, TCIOFLUSH); 102 #ifdef ACULOG 103 if (dialtimeout) { 104 (void)snprintf(line, sizeof line, "%ld second dial timeout", 105 number(value(DIALTIMEOUT))); 106 logent(value(HOST), num, "ventel", line); 107 } 108 #endif 109 if (dialtimeout) 110 ven_disconnect(); /* insurance */ 111 if (connected || dialtimeout || !boolean(value(VERBOSE))) 112 return (connected); 113 /* call failed, parse response for user */ 114 cp = strchr(line, '\r'); 115 if (cp) 116 *cp = '\0'; 117 for (cp = line; (cp = strchr(cp, ' ')) != NULL; cp++) 118 if (cp[1] == ' ') 119 break; 120 if (cp) { 121 while (*cp == ' ') 122 cp++; 123 msg = cp; 124 while (*cp) { 125 if (isupper(*cp)) 126 *cp = tolower(*cp); 127 cp++; 128 } 129 printf("%s...", msg); 130 } 131 return (connected); 132 } 133 134 void 135 ven_disconnect(void) 136 { 137 close(FD); 138 } 139 140 void 141 ven_abort(void) 142 { 143 write(FD, "\03", 1); 144 close(FD); 145 } 146 147 static void 148 echo(char *s) 149 { 150 char c; 151 152 while ((c = *s++) != '\0') 153 switch (c) { 154 case '$': 155 read(FD, &c, 1); 156 s++; 157 break; 158 159 case '#': 160 c = *s++; 161 write(FD, &c, 1); 162 break; 163 164 default: 165 write(FD, &c, 1); 166 read(FD, &c, 1); 167 } 168 } 169 170 /*ARGSUSED*/ 171 static void 172 sigALRM(int signo) 173 { 174 printf("\07timeout waiting for reply\n"); 175 dialtimeout = 1; 176 longjmp(timeoutbuf, 1); 177 } 178 179 static int 180 gobble(char match, char response[]) 181 { 182 char *cp = response; 183 sig_t f; 184 char c; 185 186 f = signal(SIGALRM, sigALRM); 187 dialtimeout = 0; 188 do { 189 if (setjmp(timeoutbuf)) { 190 signal(SIGALRM, f); 191 *cp = '\0'; 192 return (0); 193 } 194 alarm(number(value(DIALTIMEOUT))); 195 read(FD, cp, 1); 196 alarm(0); 197 c = (*cp++ &= 0177); 198 #ifdef notdef 199 if (boolean(value(VERBOSE))) 200 putchar(c); 201 #endif 202 } while (c != '\n' && c != match); 203 signal(SIGALRM, SIG_DFL); 204 *cp = '\0'; 205 return (c == match); 206 } 207 208 #define min(a,b) ((a)>(b)?(b):(a)) 209 /* 210 * This convoluted piece of code attempts to get 211 * the ventel in sync. If you don't have FIONREAD 212 * there are gory ways to simulate this. 213 */ 214 static int 215 vensync(int fd) 216 { 217 int already = 0, nread; 218 char buf[60]; 219 220 /* 221 * Toggle DTR to force anyone off that might have left 222 * the modem connected, and insure a consistent state 223 * to start from. 224 * 225 * If you don't have the ioctl calls to diddle directly 226 * with DTR, you can always try setting the baud rate to 0. 227 */ 228 ioctl(FD, TIOCCDTR, 0); 229 sleep(1); 230 ioctl(FD, TIOCSDTR, 0); 231 while (already < MAXRETRY) { 232 /* 233 * After reseting the modem, send it two \r's to 234 * autobaud on. Make sure to delay between them 235 * so the modem can frame the incoming characters. 236 */ 237 write(fd, "\r", 1); 238 delay(1,10); 239 write(fd, "\r", 1); 240 sleep(2); 241 if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) { 242 perror("tip: ioctl"); 243 continue; 244 } 245 while (nread > 0) { 246 read(fd, buf, min(nread, 60)); 247 if ((buf[nread - 1] & 0177) == '$') 248 return (1); 249 nread -= min(nread, 60); 250 } 251 sleep(1); 252 already++; 253 } 254 return (0); 255 } 256