1 /* 2 * Copyright (c) 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)unidialer.c 8.1 (Berkeley) 6/6/93 34 * $FreeBSD: src/usr.bin/tip/libacu/unidialer.c,v 1.7 1999/08/28 01:06:30 peter Exp $ 35 */ 36 37 /* 38 * Generalized routines for calling up on a Hayes AT command set based modem. 39 * Control variables are pulled out of a modem caps-style database to 40 * configure the driver for a particular modem. 41 */ 42 #include "tip.h" 43 #include "pathnames.h" 44 45 #include <sys/times.h> 46 #include <assert.h> 47 #include <err.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 51 #include "acucommon.h" 52 #include "tod.h" 53 54 /* #define DEBUG */ 55 #define MAXRETRY 5 56 57 typedef enum 58 { 59 mpt_notype, mpt_string, mpt_number, mpt_boolean 60 } modem_parm_type_t; 61 62 typedef struct { 63 modem_parm_type_t modem_parm_type; 64 const char *name; 65 union { 66 char **string; 67 unsigned int *number; 68 } value; 69 union { 70 char *string; 71 unsigned int number; 72 } default_value; 73 } modem_parm_t; 74 75 /* 76 Configuration 77 */ 78 static char modem_name [80]; 79 static char *dial_command; 80 static char *hangup_command; 81 static char *echo_off_command; 82 static char *reset_command; 83 static char *init_string; 84 static char *escape_sequence; 85 static int hw_flow_control; 86 static int lock_baud; 87 static unsigned int intercharacter_delay; 88 static unsigned int intercommand_delay; 89 static unsigned int escape_guard_time; 90 static unsigned int reset_delay; 91 92 static int unidialer_dialer(char *num, char *acu); 93 static void unidialer_disconnect(void); 94 static void unidialer_abort(void); 95 static int unidialer_connect(void); 96 static int unidialer_swallow(char *); 97 #ifdef DEBUG 98 static void unidialer_verbose_read (void); 99 #endif 100 static void unidialer_modem_cmd(int fd, const char *cmd); 101 static void unidialer_write(int fd, const char *cp, int n); 102 static void unidialer_write_str(int fd, const char *cp); 103 static void sigALRM(int); 104 static int unidialersync(void); 105 106 static acu_t unidialer = 107 { 108 modem_name, 109 unidialer_dialer, 110 unidialer_disconnect, 111 unidialer_abort 112 }; 113 114 /* 115 Table of parameters kept in modem database 116 */ 117 modem_parm_t modem_parms [] = { 118 { mpt_string, "dial_command", { &dial_command }, { "ATDT%s\r" } }, 119 { mpt_string, "hangup_command", { &hangup_command }, { "ATH\r", } }, 120 { mpt_string, "echo_off_command", { &echo_off_command }, { "ATE0\r" } }, 121 { mpt_string, "reset_command", { &reset_command }, { "ATZ\r" } }, 122 { mpt_string, "init_string", { &init_string }, { "AT{ &F\r", } }, 123 { mpt_string, "escape_sequence", { &escape_sequence }, { "+++" } }, 124 { mpt_boolean, "hw_flow_control", { (char **)&hw_flow_control }, { NULL } }, 125 { mpt_boolean, "lock_baud", { (char **)&lock_baud }, { NULL } }, 126 { mpt_number, "intercharacter_delay", { (char **)&intercharacter_delay }, { (char *)50 } }, 127 { mpt_number, "intercommand_delay", { (char **)&intercommand_delay }, { (char *)300 } }, 128 { mpt_number, "escape_guard_time", { (char **)&escape_guard_time }, { (char *)300 } }, 129 { mpt_number, "reset_delay", { (char **)&reset_delay }, { (char *)3000 } }, 130 { mpt_notype, NULL, { NULL }, { NULL } } 131 }; 132 133 /* 134 Global vars 135 */ 136 static int timeout = 0; 137 static int connected = 0; 138 static jmp_buf timeoutbuf; 139 140 #define cgetflag(f) (cgetcap(bp, f, ':') != NULL) 141 142 #ifdef DEBUG 143 144 #define print_str(x) printf (#x " = %s\n", x) 145 #define print_num(x) printf (#x " = %d\n", x) 146 147 void dumpmodemparms (char *modem) 148 { 149 printf ("modem parms for %s\n", modem); 150 print_str (dial_command); 151 print_str (hangup_command); 152 print_str (echo_off_command); 153 print_str (reset_command); 154 print_str (init_string); 155 print_str (escape_sequence); 156 print_num (lock_baud); 157 print_num (intercharacter_delay); 158 print_num (intercommand_delay); 159 print_num (escape_guard_time); 160 print_num (reset_delay); 161 printf ("\n"); 162 } 163 #endif 164 165 static int getmodemparms (const char *modem) 166 { 167 char *bp, *db_array [3], *modempath; 168 int ndx, stat; 169 modem_parm_t *mpp; 170 171 modempath = getenv ("MODEMS"); 172 173 ndx = 0; 174 175 if (modempath != NULL) 176 db_array [ndx++] = modempath; 177 178 db_array [ndx++] = _PATH_MODEMS; 179 db_array [ndx] = NULL; 180 181 if ((stat = cgetent (&bp, db_array, (char *)modem)) < 0) { 182 switch (stat) { 183 case -1: 184 warnx ("unknown modem %s", modem); 185 break; 186 case -2: 187 warnx ("can't open modem description file"); 188 break; 189 case -3: 190 warnx ("possible reference loop in modem description file"); 191 break; 192 } 193 return 0; 194 } 195 for (mpp = modem_parms; mpp->name; mpp++) 196 { 197 switch (mpp->modem_parm_type) 198 { 199 case mpt_string: 200 if (cgetstr (bp, (char *)mpp->name, mpp->value.string) == -1) 201 *mpp->value.string = mpp->default_value.string; 202 break; 203 case mpt_number: 204 { 205 long l; 206 if (cgetnum (bp, (char *)mpp->name, &l) == -1) 207 *mpp->value.number = mpp->default_value.number; 208 else 209 *mpp->value.number = (unsigned int)l; 210 } 211 break; 212 case mpt_boolean: 213 *mpp->value.number = cgetflag ((char *)mpp->name); 214 break; 215 default: 216 break; 217 } 218 } 219 strncpy (modem_name, modem, sizeof (modem_name) - 1); 220 modem_name [sizeof (modem_name) - 1] = '\0'; 221 return 1; 222 } 223 224 /* 225 */ 226 acu_t * 227 unidialer_getmodem(const char *modem_name) 228 { 229 acu_t* rc = NULL; 230 if (getmodemparms (modem_name)) 231 rc = &unidialer; 232 return rc; 233 } 234 235 static int 236 unidialer_modem_ready(void) 237 { 238 int state; 239 ioctl (FD, TIOCMGET, &state); 240 return (state & TIOCM_DSR) ? 1 : 0; 241 } 242 243 static int unidialer_waitfor_modem_ready (int ms) 244 { 245 int count; 246 for (count = 0; count < ms; count += 100) 247 { 248 if (unidialer_modem_ready ()) 249 { 250 #ifdef DEBUG 251 printf ("unidialer_waitfor_modem_ready: modem ready.\n"); 252 #endif 253 break; 254 } 255 acu_nap (100); 256 } 257 return (count < ms); 258 } 259 260 static void 261 unidialer_tty_clocal(int flag) 262 { 263 struct termios t; 264 tcgetattr (FD, &t); 265 if (flag) 266 t.c_cflag |= CLOCAL; 267 else 268 t.c_cflag &= ~CLOCAL; 269 tcsetattr (FD, TCSANOW, &t); 270 } 271 272 static int 273 unidialer_get_modem_response(char *buf, int bufsz, int response_timeout) 274 { 275 sig_t f; 276 char c, *p = buf, *lid = buf + bufsz - 1; 277 int state; 278 279 assert (bufsz > 0); 280 281 f = signal (SIGALRM, sigALRM); 282 283 timeout = 0; 284 285 if (setjmp (timeoutbuf)) { 286 signal (SIGALRM, f); 287 *p = '\0'; 288 #ifdef DEBUG 289 printf ("get_response: timeout buf=%s\n", buf); 290 #endif 291 return (0); 292 } 293 294 ualarm (response_timeout * 1000, 0); 295 296 state = 0; 297 298 while (1) 299 { 300 switch (state) 301 { 302 case 0: 303 if (read (FD, &c, 1) == 1) 304 { 305 if (c == '\r') 306 { 307 ++state; 308 } 309 else 310 { 311 #ifdef DEBUG 312 printf ("get_response: unexpected char %s.\n", ctrl (c)); 313 #endif 314 } 315 } 316 break; 317 318 case 1: 319 if (read (FD, &c, 1) == 1) 320 { 321 if (c == '\n') 322 { 323 #ifdef DEBUG 324 printf ("get_response: <CRLF> encountered.\n"); 325 #endif 326 ++state; 327 } 328 else 329 { 330 state = 0; 331 #ifdef DEBUG 332 printf ("get_response: unexpected char %s.\n", ctrl (c)); 333 #endif 334 } 335 } 336 break; 337 338 case 2: 339 if (read (FD, &c, 1) == 1) 340 { 341 if (c == '\r') 342 ++state; 343 else if (c >= ' ' && p < lid) 344 *p++ = c; 345 } 346 break; 347 348 case 3: 349 if (read (FD, &c, 1) == 1) 350 { 351 if (c == '\n') 352 { 353 signal (SIGALRM, f); 354 /* ualarm (0, 0); */ 355 alarm (0); 356 *p = '\0'; 357 #ifdef DEBUG 358 printf ("get_response: %s\n", buf); 359 #endif 360 return (1); 361 } 362 else 363 { 364 state = 0; 365 p = buf; 366 } 367 } 368 break; 369 } 370 } 371 } 372 373 static int 374 unidialer_get_okay(int ms) 375 { 376 int okay; 377 char buf [BUFSIZ]; 378 okay = unidialer_get_modem_response (buf, sizeof (buf), ms) && 379 strcmp (buf, "OK") == 0; 380 return okay; 381 } 382 383 static int 384 unidialer_dialer(char *num, char *acu) 385 { 386 char *cp; 387 char dial_string [80]; 388 char line [80]; 389 390 #ifdef DEBUG 391 dumpmodemparms (modem_name); 392 #endif 393 394 if (lock_baud) { 395 int i; 396 if ((i = speed(number(value(BAUDRATE)))) == 0) 397 return 0; 398 ttysetup (i); 399 } 400 401 if (boolean(value(VERBOSE))) 402 printf("Using \"%s\"\n", acu); 403 404 acu_hupcl (); 405 406 /* 407 * Get in synch. 408 */ 409 if (!unidialersync()) { 410 badsynch: 411 printf("tip: can't synchronize with %s\n", modem_name); 412 logent(value(HOST), num, modem_name, "can't synch up"); 413 return (0); 414 } 415 416 unidialer_modem_cmd (FD, echo_off_command); /* turn off echoing */ 417 418 sleep(1); 419 420 #ifdef DEBUG 421 if (boolean(value(VERBOSE))) 422 unidialer_verbose_read(); 423 #endif 424 425 acu_flush (); /* flush any clutter */ 426 427 unidialer_modem_cmd (FD, init_string); 428 429 if (!unidialer_get_okay (reset_delay)) 430 goto badsynch; 431 432 fflush (stdout); 433 434 for (cp = num; *cp; cp++) 435 if (*cp == '=') 436 *cp = ','; 437 438 (void) sprintf (dial_string, dial_command, num); 439 440 unidialer_modem_cmd (FD, dial_string); 441 442 connected = unidialer_connect (); 443 444 if (connected && hw_flow_control) { 445 acu_hw_flow_control (hw_flow_control); 446 } 447 448 if (timeout) { 449 sprintf(line, "%d second dial timeout", 450 number(value(DIALTIMEOUT))); 451 logent(value(HOST), num, modem_name, line); 452 } 453 454 if (timeout) 455 unidialer_disconnect (); 456 457 return (connected); 458 } 459 460 static void 461 unidialer_disconnect(void) 462 { 463 int okay, retries; 464 465 acu_flush (); /* flush any clutter */ 466 467 unidialer_tty_clocal (TRUE); 468 469 /* first hang up the modem*/ 470 ioctl (FD, TIOCCDTR, 0); 471 acu_nap (250); 472 ioctl (FD, TIOCSDTR, 0); 473 474 /* 475 * If AT&D2, then dropping DTR *should* just hangup the modem. But 476 * some modems reset anyway; also, the modem may be programmed to reset 477 * anyway with AT&D3. Play it safe and wait for the full reset time before 478 * proceeding. 479 */ 480 acu_nap (reset_delay); 481 482 if (!unidialer_waitfor_modem_ready (reset_delay)) 483 { 484 #ifdef DEBUG 485 printf ("unidialer_disconnect: warning CTS low.\r\n"); 486 #endif 487 } 488 489 /* 490 * If not strapped for DTR control, try to get command mode. 491 */ 492 for (retries = okay = 0; retries < MAXRETRY && !okay; retries++) 493 { 494 int timeout_value; 495 /* flush any clutter */ 496 if (!acu_flush ()) 497 { 498 #ifdef DEBUG 499 printf ("unidialer_disconnect: warning flush failed.\r\n"); 500 #endif 501 } 502 timeout_value = escape_guard_time; 503 timeout_value += (timeout_value * retries / MAXRETRY); 504 acu_nap (timeout_value); 505 acu_flush (); /* flush any clutter */ 506 unidialer_modem_cmd (FD, escape_sequence); 507 acu_nap (timeout_value); 508 unidialer_modem_cmd (FD, hangup_command); 509 okay = unidialer_get_okay (reset_delay); 510 } 511 if (!okay) 512 { 513 logent(value(HOST), "", modem_name, "can't hang up modem"); 514 if (boolean(value(VERBOSE))) 515 printf("hang up failed\n"); 516 } 517 (void) acu_flush (); 518 close (FD); 519 } 520 521 static void 522 unidialer_abort(void) 523 { 524 unidialer_write_str (FD, "\r"); /* send anything to abort the call */ 525 unidialer_disconnect (); 526 } 527 528 static void 529 sigALRM(int signo) 530 { 531 (void) printf("\07timeout waiting for reply\n"); 532 timeout = 1; 533 longjmp(timeoutbuf, 1); 534 } 535 536 static int unidialer_swallow (char *match) 537 { 538 sig_t f; 539 char c; 540 541 f = signal(SIGALRM, sigALRM); 542 543 timeout = 0; 544 545 if (setjmp(timeoutbuf)) { 546 signal(SIGALRM, f); 547 return (0); 548 } 549 550 alarm(number(value(DIALTIMEOUT))); 551 552 do { 553 if (*match =='\0') { 554 signal(SIGALRM, f); 555 alarm (0); 556 return (1); 557 } 558 do { 559 read (FD, &c, 1); 560 } while (c == '\0'); 561 c &= 0177; 562 #ifdef DEBUG 563 if (boolean(value(VERBOSE))) 564 { 565 /* putchar(c); */ 566 printf (ctrl (c)); 567 } 568 #endif 569 } while (c == *match++); 570 signal(SIGALRM, SIG_DFL); 571 alarm(0); 572 #ifdef DEBUG 573 if (boolean(value(VERBOSE))) 574 fflush (stdout); 575 #endif 576 return (0); 577 } 578 579 static struct baud_msg { 580 char *msg; 581 int baud; 582 } baud_msg[] = { 583 { "", B300 }, 584 { " 1200", B1200 }, 585 { " 2400", B2400 }, 586 { " 9600", B9600 }, 587 { " 9600/ARQ", B9600 }, 588 { 0, 0 }, 589 }; 590 591 static int 592 unidialer_connect(void) 593 { 594 char c; 595 int nc, nl, n; 596 char dialer_buf[64]; 597 struct baud_msg *bm; 598 sig_t f; 599 600 if (unidialer_swallow("\r\n") == 0) 601 return (0); 602 f = signal(SIGALRM, sigALRM); 603 again: 604 nc = 0; nl = sizeof(dialer_buf)-1; 605 bzero(dialer_buf, sizeof(dialer_buf)); 606 timeout = 0; 607 for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) { 608 if (setjmp(timeoutbuf)) 609 break; 610 alarm(number(value(DIALTIMEOUT))); 611 n = read(FD, &c, 1); 612 alarm(0); 613 if (n <= 0) 614 break; 615 c &= 0x7f; 616 if (c == '\r') { 617 if (unidialer_swallow("\n") == 0) 618 break; 619 if (!dialer_buf[0]) 620 goto again; 621 if (strcmp(dialer_buf, "RINGING") == 0 && 622 boolean(value(VERBOSE))) { 623 #ifdef DEBUG 624 printf("%s\r\n", dialer_buf); 625 #endif 626 goto again; 627 } 628 if (strncmp(dialer_buf, "CONNECT", 629 sizeof("CONNECT")-1) != 0) 630 break; 631 if (lock_baud) { 632 signal(SIGALRM, f); 633 #ifdef DEBUG 634 if (boolean(value(VERBOSE))) 635 printf("%s\r\n", dialer_buf); 636 #endif 637 return (1); 638 } 639 for (bm = baud_msg ; bm->msg ; bm++) 640 if (strcmp(bm->msg, dialer_buf+sizeof("CONNECT")-1) == 0) { 641 if (!acu_setspeed (bm->baud)) 642 goto error; 643 signal(SIGALRM, f); 644 #ifdef DEBUG 645 if (boolean(value(VERBOSE))) 646 printf("%s\r\n", dialer_buf); 647 #endif 648 return (1); 649 } 650 break; 651 } 652 dialer_buf[nc] = c; 653 } 654 printf("%s\r\n", dialer_buf); 655 error: 656 signal(SIGALRM, f); 657 return (0); 658 } 659 660 /* 661 * This convoluted piece of code attempts to get 662 * the unidialer in sync. 663 */ 664 static int 665 unidialersync(void) 666 { 667 int already = 0; 668 int len; 669 char buf[40]; 670 671 while (already++ < MAXRETRY) { 672 acu_nap (intercommand_delay); 673 acu_flush (); /* flush any clutter */ 674 unidialer_write_str (FD, reset_command); /* reset modem */ 675 bzero(buf, sizeof(buf)); 676 acu_nap (reset_delay); 677 ioctl (FD, FIONREAD, &len); 678 if (len) { 679 len = read(FD, buf, sizeof(buf)); 680 #ifdef DEBUG 681 buf [len] = '\0'; 682 printf("unidialersync (%s): (\"%s\")\n\r", modem_name, buf); 683 #endif 684 if (index(buf, '0') || 685 (index(buf, 'O') && index(buf, 'K'))) 686 return(1); 687 } 688 /* 689 * If not strapped for DTR control, 690 * try to get command mode. 691 */ 692 acu_nap (escape_guard_time); 693 unidialer_write_str (FD, escape_sequence); 694 acu_nap (escape_guard_time); 695 unidialer_write_str (FD, hangup_command); 696 /* 697 * Toggle DTR to force anyone off that might have left 698 * the modem connected. 699 */ 700 acu_nap (escape_guard_time); 701 ioctl (FD, TIOCCDTR, 0); 702 acu_nap (1000); 703 ioctl (FD, TIOCSDTR, 0); 704 } 705 acu_nap (intercommand_delay); 706 unidialer_write_str (FD, reset_command); 707 return (0); 708 } 709 710 /* 711 Send commands to modem; impose delay between commands. 712 */ 713 static void unidialer_modem_cmd (int fd, const char *cmd) 714 { 715 static struct timeval oldt = { 0, 0 }; 716 struct timeval newt; 717 tod_gettime (&newt); 718 if (tod_lt (&newt, &oldt)) 719 { 720 unsigned int naptime; 721 tod_subfrom (&oldt, newt); 722 naptime = oldt.tv_sec * 1000 + oldt.tv_usec / 1000; 723 if (naptime > intercommand_delay) 724 { 725 #ifdef DEBUG 726 printf ("unidialer_modem_cmd: suspicious naptime (%u ms)\r\n", naptime); 727 #endif 728 naptime = intercommand_delay; 729 } 730 #ifdef DEBUG 731 printf ("unidialer_modem_cmd: delaying %u ms\r\n", naptime); 732 #endif 733 acu_nap (naptime); 734 } 735 unidialer_write_str (fd, cmd); 736 tod_gettime (&oldt); 737 newt.tv_sec = 0; 738 newt.tv_usec = intercommand_delay; 739 tod_addto (&oldt, &newt); 740 } 741 742 static void unidialer_write_str (int fd, const char *cp) 743 { 744 #ifdef DEBUG 745 printf ("unidialer (%s): sending %s\n", modem_name, cp); 746 #endif 747 unidialer_write (fd, cp, strlen (cp)); 748 } 749 750 static void unidialer_write (int fd, const char *cp, int n) 751 { 752 acu_nap (intercharacter_delay); 753 for ( ; n-- ; cp++) { 754 write (fd, cp, 1); 755 acu_nap (intercharacter_delay); 756 } 757 } 758 759 #ifdef DEBUG 760 static void unidialer_verbose_read(void) 761 { 762 int n = 0; 763 char buf[BUFSIZ]; 764 765 if (ioctl(FD, FIONREAD, &n) < 0) 766 return; 767 if (n <= 0) 768 return; 769 if (read(FD, buf, n) != n) 770 return; 771 write(1, buf, n); 772 } 773 #endif 774 775 /* end of unidialer.c */ 776