home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / newc_dev / uucicosd.lha / modem.c < prev    next >
C/C++ Source or Header  |  1993-03-02  |  5KB  |  262 lines

  1.  
  2. /*
  3.  *  MODEM.C
  4.  *
  5.  *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
  6.  *
  7.  *  $Header: Beta:src/uucp/src/uucico/RCS/modem.c,v 1.1 90/02/02 11:56:00 dillon Exp Locker: dillon $
  8.  */
  9.  
  10. #include "includes.h"
  11. #include "uucp.h"
  12. #include "log.h"
  13.  
  14. Prototype void openline(void);
  15. Prototype int  get_baud(void);
  16. Prototype void modem_init(void);
  17. Prototype char *dial_nbr(const char *);
  18. Prototype void reset_modem(void);
  19.  
  20. /*
  21.  *  NOTE:   modem stuff pretty much ignored if we are run from
  22.  *        a getty.
  23.  */
  24.  
  25. #define MULTIMODEM    /*  I have a multi-modem    */
  26.  
  27. extern int Getty;
  28. extern int IgnoreCD;
  29.  
  30.  
  31.  
  32. void
  33. openline()
  34. {
  35.     int to = 0;
  36.  
  37.     {
  38.     char *ptr;
  39.     if (ptr = FindConfig(CTIMEOUT))
  40.         to = atoi(ptr);
  41.     }
  42.  
  43.  
  44.     signal(SIGINT,sigint);
  45.     IgnoreCD |= 4;
  46. start_again:
  47.     chkabort();
  48.     if (instr("CONNECT", 7, to))
  49.     goto start_again;
  50.  
  51.  
  52. #ifdef MULTIMODEM
  53.  
  54. #else
  55.     set_baud(get_baud());
  56. #endif
  57.     Delay(120); /* sleep 2 seconds */
  58.  
  59.     IgnoreCD &= ~4;
  60. }
  61.  
  62. #ifndef MULTIMODEM
  63.  
  64. int
  65. get_baud()
  66. {
  67. /* We've seen the CONNECT message, now we must see what baud rate
  68.    we've connected with */
  69. /* gather input until \r then see if it's 300, 1200, or 2400 */
  70. /* this is for hayes compatibles */
  71.  
  72.     int data;
  73.     char rate[10];
  74.     int rate_inx = 0;
  75.  
  76.     DEBUG(2,"looking for baud rate\n",0);
  77.  
  78.     while ( ((data = xgetc(BYTE_TO,0)) != EOF) && ((char)data != '\r')) {
  79.         if ((char)data == ' ') continue;
  80.         rate[rate_inx++] = (char)data;
  81.     }
  82.     DEBUG(2, "found baud rate of %s\n", rate);
  83.     if (strncmp(rate,"1200",4) == 0) return 1200;
  84.     if (strncmp(rate,"2400",4) == 0) return 2400;
  85.     if (rate_inx == 0) return 300;
  86.     return 1200;  /* default */
  87. }
  88.  
  89. #endif
  90.  
  91. void
  92. modem_init()
  93. {
  94.     reset_modem();
  95. }
  96.  
  97. /*
  98.  *  Simple dialer routine.  Needs replacement with a full blown
  99.  *  script driven dialer.  Next week maybe :-).  FIXME.
  100.  *
  101.  *  return 0 on success
  102.  */
  103.  
  104. char *
  105. dial_nbr(nbr)
  106. const char *nbr;
  107. {
  108.     char  *dial;
  109.     char  *errmsg = NULL;
  110.     int   i;
  111.     int   to = 0;
  112.     static char buf[64];
  113.  
  114.     if (dial = FindConfig(CTIMEOUT))
  115.     to = atoi(dial);
  116.  
  117.     IgnoreCD |= 4;
  118.  
  119.     /*
  120.      *    Clear any queued data
  121.      */
  122.  
  123.     while (xgetc(0,0) != EOF)
  124.     ;
  125.  
  126.     /*
  127.      *    Start dial sequence
  128.      */
  129.  
  130.     twrite("\rAT\r", 4);
  131.     Delay(50);
  132.  
  133.     if (dial = FindConfig(MODEMINIT))
  134.     xlat_str(dial);
  135.     dial = malloc(strlen(nbr) + 16);
  136.     dial[0] = 0;
  137.     if (strnicmp(nbr, "AT", 2) != 0)
  138.     strcpy(dial, "ATDT");
  139.     strcat(dial, nbr);
  140.     strcat(dial, "\r");
  141.  
  142.     DEBUG(2,"dialing %s\n", dial);
  143.     twrite(dial, strlen(dial));
  144.  
  145.     for (;;) {
  146.     if (inline(to, buf, sizeof(buf)) < 0) {
  147.         errmsg = "Timeout";
  148.         break;
  149.     }
  150.     if (strnicmp(buf, "CONNECT", 7) == 0)
  151.         break;
  152.     if (strnicmp(buf, "NO CARRIER", 10) == 0 ||
  153.         strnicmp(buf, "ERROR", 5) == 0 ||
  154.         strnicmp(buf, "NO DIAL", 7) == 0 ||
  155.         strnicmp(buf, "BUSY", 4) == 0 ||
  156.         strnicmp(buf, "NO ANSWER", 8) == 0) {
  157.         errmsg = buf;
  158.         break;
  159.     }
  160.     }
  161.  
  162.     IgnoreCD &= ~4;
  163.  
  164.     free(dial);
  165.  
  166.     return(errmsg);
  167. }
  168.  
  169. /*
  170.  *  RESET_MODEM()
  171.  *
  172.  *  If run from a Getty we do NOT reset the modem, which would
  173.  *  disconnect an already connected connection.
  174.  *
  175.  *  Note that the delay between CloseSerial() and OpenSerial() only
  176.  *  serves to give the Getty, if running, time to lock the port and
  177.  *  begin a disconnect sequence.
  178.  */
  179.  
  180. void
  181. reset_modem()
  182. {
  183.     if (Getty)          /*  called from a getty             */
  184.     return;
  185.     DEBUG(4, "Beg-Reset\n", 0);
  186.  
  187.     while (xgetc(QUICK_TO,0) != EOF); /*  Eat extraneous chars. */
  188.  
  189.     if (CheckCarrier()) {
  190.     if (IgnoreDTR) {
  191.         Delay(50*2);
  192.         xwrite("+", 1);
  193.         Delay(10);
  194.         xwrite("+", 1);
  195.         Delay(10);
  196.         xwrite("+", 1);
  197.         Delay(50*3);
  198.         xwrite("ATH0\r", 5);
  199.         Delay(50*2);
  200.     } else {
  201.         CloseSerial(0);     /*  drop dtr            */
  202.         Delay(50*3);        /*  delay 3 seconds     */
  203.         DEBUG(4, "End-Reset-1\n", 0);
  204.         OpenSerial(0);      /*  re-open serial      */
  205.     }
  206.     } else {
  207.     xwrite("\r", 1);        /*  hangup the dial?    */
  208.     }
  209.     DEBUG(4, "End-Reset-2\n", 0);
  210. }
  211.  
  212. #ifdef NOTDEF
  213.  
  214.     if (GettyCmd(DeviceName, DeviceUnit, '0', NULL) == 0)
  215.     return;
  216.  
  217.     /*
  218.      *    Getty doesn't exist, we have to reset the modem ourselves!
  219.      */
  220.  
  221.     IgnoreCD |= 4;
  222.  
  223. #ifdef MULTIMODEM
  224.     set_baud(19200);
  225.  
  226.     if (CheckCarrier()) {
  227.     Delay(60);
  228.     twrite("+++", 3);
  229.     Delay(120);
  230.     }
  231.     twrite("ATZ\r", 4);
  232.     instr("OK\r\n", 4, 3);
  233.     twrite("ATH0\r", 5);
  234.     instr("OK\r\n", 4, 3);
  235.     twrite("ATZ\r", 4);
  236.     instr("OK\r\n", 4, 3);
  237.  
  238.     /*amiga_closeopen();*/
  239.     sprintf(init, "%s\r", "ATM0S0=2X4$BA0");
  240.     if (debug > 0)
  241.     init[3] = '1';
  242.     twrite(init, strlen(init));
  243.     instr("OK\r\n", 4, 3);
  244. #else
  245.     twrite("+++", 3);
  246.     Delay(60);
  247.     twrite("ATH0\r", 5);
  248.     Delay(120);
  249.     twrite("ATZ\r", 4);
  250.     Delay(60);
  251.     twrite("ATZ\r", 4);
  252.     Delay(60);
  253.     sprintf(init, "%s\r", "ATS2");
  254.     twrite(init, strlen(init));
  255.     Delay(60);
  256. #endif
  257.     IgnoreCD &= ~4;
  258. }
  259.  
  260. #endif
  261.  
  262.