1 /* $NetBSD: t3000.c,v 1.13 2006/04/03 02:25:27 perry Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 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[] = "@(#)t3000.c 8.1 (Berkeley) 6/6/93"; 36 #endif 37 __RCSID("$NetBSD: t3000.c,v 1.13 2006/04/03 02:25:27 perry Exp $"); 38 #endif /* not lint */ 39 40 /* 41 * Routines for calling up on a Telebit T3000 modem. 42 * Derived from Courier driver. 43 */ 44 #include "tip.h" 45 46 #define MAXRETRY 5 47 48 static int timeout = 0; 49 static int connected = 0; 50 static jmp_buf timeoutbuf; 51 52 static void sigALRM(int); 53 static int t3000_connect(void); 54 static void t3000_nap(void); 55 static void t3000_napx(int); 56 static int t3000_swallow(const char *); 57 static int t3000_sync(void); 58 static void t3000_write(int, const char *, int); 59 60 int 61 t3000_dialer(char *num, char *acu) 62 { 63 char *cp; 64 struct termios cntrl; 65 66 if (boolean(value(VERBOSE))) 67 printf("Using \"%s\"\n", acu); 68 69 tcgetattr(FD, &cntrl); 70 cntrl.c_cflag |= HUPCL; 71 tcsetattr(FD, TCSANOW, &cntrl); 72 /* 73 * Get in synch. 74 */ 75 if (!t3000_sync()) { 76 badsynch: 77 printf("can't synchronize with t3000\n"); 78 return (0); 79 } 80 t3000_write(FD, "AT E0\r", 6); /* turn off echoing */ 81 sleep(1); 82 #ifdef DEBUG 83 if (boolean(value(VERBOSE))) 84 t3000_verbose_read(); 85 #endif 86 tcflush(FD, TCIOFLUSH); 87 t3000_write(FD, "AT E0 H0 Q0 X4 V1\r", 18); 88 if (!t3000_swallow("\r\nOK\r\n")) 89 goto badsynch; 90 fflush(stdout); 91 t3000_write(FD, "AT D", 4); 92 for (cp = num; *cp; cp++) 93 if (*cp == '=') 94 *cp = ','; 95 t3000_write(FD, num, strlen(num)); 96 t3000_write(FD, "\r", 1); 97 connected = t3000_connect(); 98 if (timeout) 99 t3000_disconnect(); 100 return (connected); 101 } 102 103 void 104 t3000_disconnect(void) 105 { 106 /* first hang up the modem*/ 107 ioctl(FD, TIOCCDTR, 0); 108 sleep(1); 109 ioctl(FD, TIOCSDTR, 0); 110 t3000_sync(); /* reset */ 111 close(FD); 112 } 113 114 void 115 t3000_abort(void) 116 { 117 t3000_write(FD, "\r", 1); /* send anything to abort the call */ 118 t3000_disconnect(); 119 } 120 121 static void 122 sigALRM(int dummy) 123 { 124 printf("\07timeout waiting for reply\n"); 125 timeout = 1; 126 longjmp(timeoutbuf, 1); 127 } 128 129 static int 130 t3000_swallow(const char *match) 131 { 132 sig_t f; 133 char c; 134 135 #if __GNUC__ /* XXX pacify gcc */ 136 (void)&match; 137 #endif 138 139 f = signal(SIGALRM, sigALRM); 140 timeout = 0; 141 do { 142 if (*match =='\0') { 143 signal(SIGALRM, f); 144 return (1); 145 } 146 if (setjmp(timeoutbuf)) { 147 signal(SIGALRM, f); 148 return (0); 149 } 150 alarm(number(value(DIALTIMEOUT))); 151 read(FD, &c, 1); 152 alarm(0); 153 c &= 0177; 154 #ifdef DEBUG 155 if (boolean(value(VERBOSE))) 156 putchar(c); 157 #endif 158 } while (c == *match++); 159 #ifdef DEBUG 160 if (boolean(value(VERBOSE))) 161 fflush(stdout); 162 #endif 163 signal(SIGALRM, SIG_DFL); 164 return (0); 165 } 166 167 #ifndef B19200 /* XXX */ 168 #define B19200 EXTA 169 #define B38400 EXTB 170 #endif 171 172 struct tbaud_msg { 173 const char *msg; 174 int baud; 175 int baud2; 176 } tbaud_msg[] = { 177 { "", B300, 0 }, 178 { " 1200", B1200, 0 }, 179 { " 2400", B2400, 0 }, 180 { " 4800", B4800, 0 }, 181 { " 9600", B9600, 0 }, 182 { " 14400", B19200, B9600 }, 183 { " 19200", B19200, B9600 }, 184 { " 38400", B38400, B9600 }, 185 { " 57600", B38400, B9600 }, 186 { " 7512", B9600, 0 }, 187 { " 1275", B2400, 0 }, 188 { " 7200", B9600, 0 }, 189 { " 12000", B19200, B9600 }, 190 { 0, 0, 0 }, 191 }; 192 193 static int 194 t3000_connect(void) 195 { 196 char c; 197 int nc, nl, n; 198 char dialer_buf[64]; 199 struct tbaud_msg *bm; 200 sig_t f; 201 202 #if __GNUC__ /* XXX pacify gcc */ 203 (void)&nc; 204 (void)&nl; 205 #endif 206 207 if (t3000_swallow("\r\n") == 0) 208 return (0); 209 f = signal(SIGALRM, sigALRM); 210 again: 211 memset(dialer_buf, 0, sizeof(dialer_buf)); 212 timeout = 0; 213 for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) { 214 if (setjmp(timeoutbuf)) 215 break; 216 alarm(number(value(DIALTIMEOUT))); 217 n = read(FD, &c, 1); 218 alarm(0); 219 if (n <= 0) 220 break; 221 c &= 0x7f; 222 if (c == '\r') { 223 if (t3000_swallow("\n") == 0) 224 break; 225 if (!dialer_buf[0]) 226 goto again; 227 if (strcmp(dialer_buf, "RINGING") == 0 && 228 boolean(value(VERBOSE))) { 229 #ifdef DEBUG 230 printf("%s\r\n", dialer_buf); 231 #endif 232 goto again; 233 } 234 if (strncmp(dialer_buf, "CONNECT", 235 sizeof("CONNECT")-1) != 0) 236 break; 237 for (bm = tbaud_msg ; bm->msg ; bm++) 238 if (strcmp(bm->msg, 239 dialer_buf+sizeof("CONNECT")-1) == 0) { 240 struct termios cntrl; 241 242 tcgetattr(FD, &cntrl); 243 cfsetospeed(&cntrl, bm->baud); 244 cfsetispeed(&cntrl, bm->baud); 245 tcsetattr(FD, TCSAFLUSH, &cntrl); 246 signal(SIGALRM, f); 247 #ifdef DEBUG 248 if (boolean(value(VERBOSE))) 249 printf("%s\r\n", dialer_buf); 250 #endif 251 return (1); 252 } 253 break; 254 } 255 dialer_buf[nc] = c; 256 #ifdef notdef 257 if (boolean(value(VERBOSE))) 258 putchar(c); 259 #endif 260 } 261 printf("%s\r\n", dialer_buf); 262 signal(SIGALRM, f); 263 return (0); 264 } 265 266 /* 267 * This convoluted piece of code attempts to get 268 * the t3000 in sync. 269 */ 270 static int 271 t3000_sync(void) 272 { 273 int already = 0; 274 int len; 275 char buf[40]; 276 277 while (already++ < MAXRETRY) { 278 tcflush(FD, TCIOFLUSH); 279 t3000_write(FD, "\rAT Z\r", 6); /* reset modem */ 280 memset(buf, 0, sizeof(buf)); 281 sleep(2); 282 ioctl(FD, FIONREAD, &len); 283 #if 1 284 if (len == 0) len = 1; 285 #endif 286 if (len) { 287 len = read(FD, buf, sizeof(buf)); 288 #ifdef DEBUG 289 buf[len] = '\0'; 290 printf("t3000_sync: (\"%s\")\n\r", buf); 291 #endif 292 if (strchr(buf, '0') || 293 (strchr(buf, 'O') && strchr(buf, 'K'))) 294 return(1); 295 } 296 /* 297 * If not strapped for DTR control, 298 * try to get command mode. 299 */ 300 sleep(1); 301 t3000_write(FD, "+++", 3); 302 sleep(1); 303 /* 304 * Toggle DTR to force anyone off that might have left 305 * the modem connected. 306 */ 307 ioctl(FD, TIOCCDTR, 0); 308 sleep(1); 309 ioctl(FD, TIOCSDTR, 0); 310 } 311 t3000_write(FD, "\rAT Z\r", 6); 312 return (0); 313 } 314 315 static void 316 t3000_write(int fd, const char *cp, int n) 317 { 318 319 #ifdef notdef 320 if (boolean(value(VERBOSE))) 321 write(1, cp, n); 322 #endif 323 tcdrain(fd); 324 t3000_nap(); 325 for ( ; n-- ; cp++) { 326 write(fd, cp, 1); 327 tcdrain(fd); 328 t3000_nap(); 329 } 330 } 331 332 #ifdef DEBUG 333 t3000_verbose_read(void) 334 { 335 int n = 0; 336 char buf[BUFSIZ]; 337 338 if (ioctl(FD, FIONREAD, &n) < 0) 339 return; 340 if (n <= 0) 341 return; 342 if (read(FD, buf, n) != n) 343 return; 344 write(1, buf, n); 345 } 346 #endif 347 348 #define setsa(sa, a) \ 349 sa.sa_handler = a; sigemptyset(&sa.sa_mask); sa.sa_flags = 0 350 351 static int napms = 50; /* Give the t3000 50 milliseconds between characters */ 352 353 static int ringring; 354 355 void 356 t3000_nap(void) 357 { 358 359 struct itimerval itv, oitv; 360 struct itimerval *itp = &itv; 361 struct sigaction sa, osa; 362 sigset_t sm, osm; 363 364 timerclear(&itp->it_interval); 365 timerclear(&itp->it_value); 366 if (setitimer(ITIMER_REAL, itp, &oitv) < 0) 367 return; 368 369 sigemptyset(&sm); 370 sigaddset(&sm, SIGALRM); 371 (void)sigprocmask(SIG_BLOCK, &sm, &osm); 372 373 itp->it_value.tv_sec = napms/1000; 374 itp->it_value.tv_usec = ((napms%1000)*1000); 375 376 setsa(sa, t3000_napx); 377 (void)sigaction(SIGALRM, &sa, &osa); 378 379 (void)setitimer(ITIMER_REAL, itp, NULL); 380 381 sm = osm; 382 sigdelset(&sm, SIGALRM); 383 384 for (ringring = 0; !ringring; ) 385 sigsuspend(&sm); 386 387 (void)sigaction(SIGALRM, &osa, NULL); 388 (void)setitimer(ITIMER_REAL, &oitv, NULL); 389 (void)sigprocmask(SIG_SETMASK, &osm, NULL); 390 } 391 392 static void 393 t3000_napx(int dummy) 394 { 395 396 ringring = 1; 397 } 398