1 /* $OpenBSD: t3000.c,v 1.9 2001/10/24 18:38:58 millert Exp $ */ 2 /* $NetBSD: t3000.c,v 1.5 1997/02/11 09:24:18 mrg Exp $ */ 3 4 /* 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)t3000.c 8.1 (Berkeley) 6/6/93"; 43 static char rcsid[] = "$OpenBSD: t3000.c,v 1.9 2001/10/24 18:38:58 millert Exp $"; 44 #endif 45 #endif /* not lint */ 46 47 /* 48 * Routines for calling up on a Telebit T3000 modem. 49 * Derived from Courier driver. 50 */ 51 #include "tip.h" 52 53 #include <sys/ioctl.h> 54 #include <stdio.h> 55 56 #define MAXRETRY 5 57 58 static void sigALRM(); 59 static int timeout = 0; 60 static int connected = 0; 61 static jmp_buf timeoutbuf, intbuf; 62 static int t3000_sync(), t3000_connect(), t3000_swallow(); 63 static void t3000_nap(); 64 65 int 66 t3000_dialer(num, acu) 67 char *num; 68 char *acu; 69 { 70 char *cp; 71 struct termios cntrl; 72 #ifdef ACULOG 73 char line[80]; 74 #endif 75 76 if (boolean(value(VERBOSE))) 77 printf("Using \"%s\"\n", acu); 78 79 tcgetattr(FD, &cntrl); 80 cntrl.c_cflag |= HUPCL; 81 tcsetattr(FD, TCSANOW, &cntrl); 82 /* 83 * Get in synch. 84 */ 85 if (!t3000_sync()) { 86 badsynch: 87 printf("can't synchronize with t3000\n"); 88 #ifdef ACULOG 89 logent(value(HOST), num, "t3000", "can't synch up"); 90 #endif 91 return (0); 92 } 93 t3000_write(FD, "AT E0\r", 6); /* turn off echoing */ 94 sleep(1); 95 #ifdef DEBUG 96 if (boolean(value(VERBOSE))) 97 t3000_verbose_read(); 98 #endif 99 tcflush(FD, TCIOFLUSH); 100 t3000_write(FD, "AT E0 H0 Q0 X4 V1\r", 18); 101 if (!t3000_swallow("\r\nOK\r\n")) 102 goto badsynch; 103 fflush(stdout); 104 t3000_write(FD, "AT D", 4); 105 for (cp = num; *cp; cp++) 106 if (*cp == '=') 107 *cp = ','; 108 t3000_write(FD, num, strlen(num)); 109 t3000_write(FD, "\r", 1); 110 connected = t3000_connect(); 111 #ifdef ACULOG 112 if (timeout) { 113 (void)sprintf(line, "%ld second dial timeout", 114 number(value(DIALTIMEOUT))); 115 logent(value(HOST), num, "t3000", line); 116 } 117 #endif 118 if (timeout) 119 t3000_disconnect(); 120 return (connected); 121 } 122 123 void 124 t3000_disconnect() 125 { 126 /* first hang up the modem*/ 127 ioctl(FD, TIOCCDTR, 0); 128 sleep(1); 129 ioctl(FD, TIOCSDTR, 0); 130 t3000_sync(); /* reset */ 131 close(FD); 132 } 133 134 void 135 t3000_abort() 136 { 137 t3000_write(FD, "\r", 1); /* send anything to abort the call */ 138 t3000_disconnect(); 139 } 140 141 static void 142 sigALRM() 143 { 144 printf("\07timeout waiting for reply\n"); 145 timeout = 1; 146 longjmp(timeoutbuf, 1); 147 } 148 149 static int 150 t3000_swallow(match) 151 char *match; 152 { 153 sig_t f; 154 char c; 155 156 f = signal(SIGALRM, sigALRM); 157 timeout = 0; 158 do { 159 if (*match =='\0') { 160 signal(SIGALRM, f); 161 return (1); 162 } 163 if (setjmp(timeoutbuf)) { 164 signal(SIGALRM, f); 165 return (0); 166 } 167 alarm(number(value(DIALTIMEOUT))); 168 read(FD, &c, 1); 169 alarm(0); 170 c &= 0177; 171 #ifdef DEBUG 172 if (boolean(value(VERBOSE))) 173 putchar(c); 174 #endif 175 } while (c == *match++); 176 #ifdef DEBUG 177 if (boolean(value(VERBOSE))) 178 fflush(stdout); 179 #endif 180 signal(SIGALRM, SIG_DFL); 181 return (0); 182 } 183 184 #ifndef B19200 /* XXX */ 185 #define B19200 EXTA 186 #define B38400 EXTB 187 #endif 188 189 struct tbaud_msg { 190 char *msg; 191 int baud; 192 int baud2; 193 } tbaud_msg[] = { 194 "", B300, 0, 195 " 1200", B1200, 0, 196 " 2400", B2400, 0, 197 " 4800", B4800, 0, 198 " 9600", B9600, 0, 199 " 14400", B19200, B9600, 200 " 19200", B19200, B9600, 201 " 38400", B38400, B9600, 202 " 57600", B38400, B9600, 203 " 7512", B9600, 0, 204 " 1275", B2400, 0, 205 " 7200", B9600, 0, 206 " 12000", B19200, B9600, 207 0, 0, 0, 208 }; 209 210 static int 211 t3000_connect() 212 { 213 char c; 214 int nc, nl, n; 215 char dialer_buf[64]; 216 struct tbaud_msg *bm; 217 sig_t f; 218 219 if (t3000_swallow("\r\n") == 0) 220 return (0); 221 f = signal(SIGALRM, sigALRM); 222 again: 223 nc = 0; nl = sizeof(dialer_buf)-1; 224 bzero(dialer_buf, sizeof(dialer_buf)); 225 timeout = 0; 226 for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) { 227 if (setjmp(timeoutbuf)) 228 break; 229 alarm(number(value(DIALTIMEOUT))); 230 n = read(FD, &c, 1); 231 alarm(0); 232 if (n <= 0) 233 break; 234 c &= 0x7f; 235 if (c == '\r') { 236 if (t3000_swallow("\n") == 0) 237 break; 238 if (!dialer_buf[0]) 239 goto again; 240 if (strcmp(dialer_buf, "RINGING") == 0 && 241 boolean(value(VERBOSE))) { 242 #ifdef DEBUG 243 printf("%s\r\n", dialer_buf); 244 #endif 245 goto again; 246 } 247 if (strncmp(dialer_buf, "CONNECT", 248 sizeof("CONNECT")-1) != 0) 249 break; 250 for (bm = tbaud_msg ; bm->msg ; bm++) 251 if (strcmp(bm->msg, 252 dialer_buf+sizeof("CONNECT")-1) == 0) { 253 struct termios cntrl; 254 255 tcgetattr(FD, &cntrl); 256 cfsetospeed(&cntrl, bm->baud); 257 cfsetispeed(&cntrl, bm->baud); 258 tcsetattr(FD, TCSAFLUSH, &cntrl); 259 signal(SIGALRM, f); 260 #ifdef DEBUG 261 if (boolean(value(VERBOSE))) 262 printf("%s\r\n", dialer_buf); 263 #endif 264 return (1); 265 } 266 break; 267 } 268 dialer_buf[nc] = c; 269 #ifdef notdef 270 if (boolean(value(VERBOSE))) 271 putchar(c); 272 #endif 273 } 274 printf("%s\r\n", dialer_buf); 275 signal(SIGALRM, f); 276 return (0); 277 } 278 279 /* 280 * This convoluted piece of code attempts to get 281 * the t3000 in sync. 282 */ 283 static int 284 t3000_sync() 285 { 286 int already = 0; 287 int len; 288 char buf[40]; 289 290 while (already++ < MAXRETRY) { 291 tcflush(FD, TCIOFLUSH); 292 t3000_write(FD, "\rAT Z\r", 6); /* reset modem */ 293 bzero(buf, sizeof(buf)); 294 sleep(2); 295 ioctl(FD, FIONREAD, &len); 296 #if 1 297 if (len == 0) len = 1; 298 #endif 299 if (len) { 300 len = read(FD, buf, sizeof(buf)); 301 #ifdef DEBUG 302 buf[len] = '\0'; 303 printf("t3000_sync: (\"%s\")\n\r", buf); 304 #endif 305 if (strchr(buf, '0') || 306 (strchr(buf, 'O') && strchr(buf, 'K'))) 307 return(1); 308 } 309 /* 310 * If not strapped for DTR control, 311 * try to get command mode. 312 */ 313 sleep(1); 314 t3000_write(FD, "+++", 3); 315 sleep(1); 316 /* 317 * Toggle DTR to force anyone off that might have left 318 * the modem connected. 319 */ 320 ioctl(FD, TIOCCDTR, 0); 321 sleep(1); 322 ioctl(FD, TIOCSDTR, 0); 323 } 324 t3000_write(FD, "\rAT Z\r", 6); 325 return (0); 326 } 327 328 static int 329 t3000_write(fd, cp, n) 330 int fd; 331 char *cp; 332 int n; 333 { 334 #ifdef notdef 335 if (boolean(value(VERBOSE))) 336 write(1, cp, n); 337 #endif 338 tcdrain(fd); 339 t3000_nap(); 340 for ( ; n-- ; cp++) { 341 write(fd, cp, 1); 342 tcdrain(fd); 343 t3000_nap(); 344 } 345 } 346 347 #ifdef DEBUG 348 t3000_verbose_read() 349 { 350 int n = 0; 351 char buf[BUFSIZ]; 352 353 if (ioctl(FD, FIONREAD, &n) < 0) 354 return; 355 if (n <= 0) 356 return; 357 if (read(FD, buf, n) != n) 358 return; 359 write(1, buf, n); 360 } 361 #endif 362 363 /* Give the t3000 50 milliseconds between characters */ 364 void 365 t3000_nap() 366 { 367 struct timespec ts; 368 369 ts.tv_sec = 0; 370 ts.tv_nsec = 50 * 1000000; 371 372 nanosleep(&ts, NULL); 373 } 374