home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / duucp-1.17 / AU-117b4-src.lha / src / uucico / modem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-24  |  4.0 KB  |  230 lines

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