home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / bss / pup.arc / MDMFUNC.C < prev    next >
C/C++ Source or Header  |  1987-10-31  |  11KB  |  374 lines

  1. #include <ascii.h>
  2. #include <puppy.h>
  3. #include <pupmem.h>
  4.  
  5. /*
  6.  
  7. Low level modem support. These depend on and set the following:
  8.  
  9. FLAG cd_flag;        1 == ignore cd(). Used while initializing,
  10.             dialing, disconnecting, etc.
  11.  
  12. unsigned cd_bit;    modem CD mask bit. 
  13.  
  14. unsigned datarate;    data baud rate, at which file transfer times
  15.             and such are computed. It may be the same as
  16.             the link rate.
  17.  
  18. unsigned rate;        modem baud rate; the speed at which Fido talks
  19.             to the modem. For 300/1200/2400 type modems, it
  20.             is the same as datarate.
  21.  
  22. See statics below for mdmfunc. internal variables.
  23.  
  24.  
  25. setbaud(r)
  26.     Set the baud rate to r. For variable speed modems, this sets
  27.     the data rate and the link rate the same. For fixed link rate
  28.     modems it sets only the data rate; the link rate never changes.
  29.  
  30. answer()
  31.     This is called while waiting for modem activity or an event.
  32.     Answer the phone if it is ringing. This looks for a RING result
  33.     code, and issues an ATA command and waits for connect if one
  34.     is found. 
  35.  
  36.     Returns:    -1    modem commands in progress (dont dial!)
  37.             0    modem idle
  38.             1    modem connected & online
  39.  
  40.     If answer() says connected, then cd_flag is false and true
  41.     carrier can be watched. 
  42.  
  43.     If mdmstate == 1 and carrier is not present, the modem is
  44.     assumed to be idle.
  45.  
  46. discon()
  47.     Disconnects the modem and readies it for the next call. This does
  48.     various obscure things to make lousy hobbiest modems perform more or
  49.     less reliably. 
  50.  
  51. chk_modem()    
  52.     Poll the modem, and return any result code found or -1 if
  53.     none received. This returns the numeric value of the result 
  54.     code string. The buffer must be initialized to a null string
  55.     the first pass through, and contents preserved between calls.
  56.  
  57. connect(result)
  58. int result;
  59.     Interprets the result code from chk_modem() and displays a message
  60.     and returns a status word as follows: 0: no connection made; 
  61.     1: connected, variable 'rate' set and baud rate set, -1 error 
  62.     in dialing.
  63.  
  64. char *lastconnect()
  65.     Returns the string defining the last modem result.
  66.  
  67. */
  68.  
  69. static int mdmstate;        /* what the modem is up to:
  70.                     0    idling
  71.                     1    online
  72.                     -1    awaiting result from the modem
  73.                 */
  74.  
  75. static char mdmbuff[SS] = "";    /* we store results here, etc */
  76. static char mdmresult[SS] = "";    /* last modem result */
  77.  
  78. /* Send any modem initialization file to the modem. */
  79.  
  80. init_modem(initstr)
  81. char *initstr;
  82. {
  83. char *si;
  84. int i,n;
  85.  
  86.     printf("Pup says: \"Initializing the modem\"\r\n");
  87.     cd_flag= 1;            /* ignore carrier */
  88.     setbaud(pup.maxbaud);        /* set the data rate */
  89.     flush(0);
  90.     discon();            /* disconnect, */
  91.  
  92.     cd_flag= 1;            /* ignore carrier */
  93.     modout(CR);            /* clear the modem command buffer */
  94.     flush(50);
  95.  
  96.     atp(initstr);            /* send it, */
  97.     sendwt("\r");            /* CR & wait for result, */
  98.     modem_chk();            /* wake it up, */
  99.     mdmstate= 0;            /* modem is now idle */
  100.     cd_flag= 0;
  101. }
  102.  
  103. /* Output a modem command string, wait for the result code, or a timeout
  104. after 2 seconds. Returns the result code or -1 if timeout. */
  105.  
  106. sendwt(s)
  107. char *s;
  108. {
  109. int n;
  110.     atp(s);                /* send the string, */
  111.     for (millisec= 0L; millisec < 2000L; ) {
  112.         n= chk_modem();        /* check for result, */
  113.         if (n >= 0) {        /* stop if we get one */
  114.             delay(50);
  115.             break;
  116.         }
  117.     }
  118.     return(n);
  119. }
  120.  
  121. /* Send a command sequence to the modem, */
  122.  
  123. atp(s)
  124. char *s;
  125. {
  126.     while (*s) {
  127.         modout(*s++);
  128.         delay(2);
  129.     }
  130. }
  131.  
  132. /* Set the desired baud rate. Bound it to the maximum the modem can handle. */
  133.  
  134. setbaud(n)
  135. unsigned n;
  136. {
  137.     if (n > pup.maxbaud) n= pup.maxbaud;    /* bound it */
  138.     datarate= n;                /* set data rate, */
  139.     baud(datarate);                /* set it */
  140. }
  141.  
  142. /* Disconnect. First, it delays to let (output buffered) characters out,
  143. then drops DTR and waits for carrier loss. (Timeout if it does not
  144. go away.) A delay is done to ensure that stupid modems see DTR and dont 
  145. get upset. Then DTR is raised. */
  146.  
  147. discon() {
  148.  
  149. int i;
  150.  
  151.     limit= 0;                /* no time limit, we check explicitly */
  152.  
  153. /* Wait until the output buffer is flushed. If carrier is lost, flush
  154. anything that remains and stop waiting. Output a CR to flush the modems
  155. command buffer in case anything went out with CD low. */
  156.  
  157.     cd_flag= 0;                /* need to look at CD */
  158.     while (cd() && _mbusy());        /* while carrier & outputting */
  159.     flush(0);                /* flush anything remaining */
  160.     cd_flag= 1;                /* ignore CD, */
  161.     flush(20);                /* flush buffers */
  162.     modout(CR);                /* abort dialing, etc USR modem */
  163.  
  164. /* With DTR low, wait for CD to go away. If it doesnt, complain like hell,
  165. and wait for a Control-C, then quit everything. This aborts dialing also
  166. for some modems. */
  167.  
  168.     cd_flag= 0;                /* watch CD again */
  169.     lower_dtr();                /* drop DTR, */
  170.     for (i= 10; i--;) {            /* wait for no CD */
  171.         delay(10);            /* 100 mS minimum, */
  172.         if (! cd()) break;
  173.     }
  174.     raise_dtr();                /* enable the modem again */
  175.     delay(10);                /* short delay for DTR */
  176.  
  177. /* If that didnt work, try +++; if there is still carrier (USR disconnects
  178. on +++) issue ATH0 (which will disconnect the Hayes etc) */
  179.  
  180.     if (cd()) {                /* if STILL carrier,*/
  181.         for (i= 1; i++;) {        /* try +++ */
  182.             cd_flag= 1;
  183.             flush(0);
  184.             sendwt("+++");        /* try to disconnect */
  185.             cd_flag= 0;
  186.             if (! cd()) break;    /* stop if disconnected, */
  187.  
  188.             cd_flag= 1;
  189.             sendwt("ATH0\r");    /* try H0 next ... */
  190.  
  191.             cd_flag= 0;
  192.             if (! cd()) break;    /* did that work??? */
  193.  
  194.             if (i > 5) {
  195.                 printf("      Puppy cannot make the modem disconnect!\r\n");
  196.                 printf("      Type Control-C to abort to DOS: ");
  197.                 doscode= 1;
  198.                 if (bdos(6,0xff) == ETX) return;
  199.             }
  200.         }
  201.     }
  202.  
  203. /* Now see if the modem is still alive; this flushes garbage and makes sure
  204. the modem is ready before continuing. */
  205.  
  206.     cd_flag= 1;                /* ignore DTR */
  207.     modem_chk();                /* check if modem is dead */
  208.     mdmstate= 0;                /* modem is now idle */
  209.     cd_flag= 0;
  210. }
  211. /* Issue AT commands a few times and try to get the modems attention. */
  212.  
  213. modem_chk() {
  214.  
  215. int i;
  216.  
  217.     cd_flag= 1;
  218.     for (i= 3; i--; ) {
  219.         if (sendwt("AT\r") >= 0) return;
  220.     }
  221.     printf("Pup says: \"Modem not responding!\"\r\n");
  222. }
  223.  
  224. /* Poll the modem looking for activity; returns a code indicating whats
  225. happening. If 0, the modem is idle, awaiting calls etc. -1 means a 
  226. RING was received, and ATA issued, and we're waiting for connect/fail.
  227. 1 means we are connected and online. */
  228.  
  229. answer() {
  230. int n;
  231.  
  232.     cd_flag= 0;                /* watch true carrier */
  233.     if (!cd() && (mdmstate == 1))         /* if we're supposedly connected, */
  234.         mdmstate= 0;            /* idle if no carrier */
  235.  
  236.     n= chk_modem();                /* check for a result, */
  237.     if (n < 0) return(mdmstate);        /* none yet, same state */
  238.     switch (connect(n)) {            /* see what happened, */
  239.         case 1:                /* dial sucessful */
  240.             cd_flag= 0;        /* wait for CD */
  241.             mdmstate= 0;        /* (assume not connected) */
  242.             for (millisec= 0L; millisec < 20000L;) {
  243.                 if (cd()) {    /* if we find it, */
  244.                     delay(200); /* delay for modem/telco */
  245.                     mdmstate= 1; /* flag the connection */
  246.                     break;
  247.                 }
  248.             }
  249.             break;            /* go return modem state */
  250.  
  251.         case -1:            /* dialing/command error */
  252.             mdmstate= 0;        /*   reset state, */
  253.             break;
  254.  
  255.         case 0:                /* modem idle, no connection */
  256.             if (n == 2) {        /*   but we're answering! */
  257.                 printf("Pup says: \"Attemping a connection\"\r\n");
  258.                 flush(20);    /*   wait til its ready (& flush RINGs) */
  259.                 atp("ATA\r");    /*   force answer if RING */
  260.                 mdmstate= -1;    /*   somethings happening */
  261.  
  262.             } else {        /* not RING, */
  263.                 mdmstate= 0;    /*   so now idle */
  264.             }
  265.             break;
  266.     }
  267.     return(mdmstate);            /* say what it was */
  268. }
  269.  
  270. /* Given a result code from the modem, return 1 if connected OK and
  271. baud rate set, 0 if no connection, or -1 if an error. Though in theory the
  272. modem COULD return any result code at any time, in practice it doesn't; 
  273. the modem will not say RINGING while waiting for an incoming call ... hence
  274. some of these messages dont apply to all cases. */
  275.  
  276. connect(result)
  277. int result;
  278. {
  279. char *s;
  280. int r,n;
  281. long x;
  282.  
  283.     n= -1;
  284.     s= "";
  285.  
  286.     switch (result) {
  287. /* These are connect for either incoming calls or dialing */
  288.         case 1: n= 1; r= 300; break;
  289.         case 5: n= 1; r= 1200; break;
  290.         case 9: n= 1; r= 600; break;
  291.         case 10: n= 1; r= 2400; break;
  292.         case 13: n= 1; r= 9600; break;
  293.  
  294. /* These are from the Hayes Courier HST */
  295.         case 11: strcpy(mdmresult,"Ringing!"); n= 0; break;
  296.         case 15: n= 1; r= 1200; s= "/ARQ"; break;
  297.         case 16: n= 1; r= 2400; s= "/ARQ"; break;
  298.         case 17: n= 1; r= 9600; s= "/ARQ"; break;
  299.  
  300. /* Stupid MNP things from telebit */
  301.         case 20: n= 1; r= 300; s= "/REL"; break;
  302.         case 22: n= 1; r= 1200; s= "/REL"; break;
  303.         case 23: n= 1; r= 2400; s= "/REL"; break;
  304.  
  305. /* These are Telebit TrailBlazer */
  306.         case 50: n= 1; r= (80L * linkrate) / 100L; s= "/FAST"; break;
  307.         case 61: n= 1; r= (80L * linkrate) / 100L; s= "/FAST/KERMIT"; break;
  308.         case 62: n= 1; r= (80L * linkrate) / 100L; s= "/FAST/XMODEM"; break;
  309.         case 63: n= 1; r= (80L * linkrate) / 100L; s= "/FAST/UUCP"; break;
  310.         case 52: strcpy(mdmresult,"R-R-Ring!"); n= 0; break;
  311.  
  312. /* These cause idle for incoming calls, but terminate dialing. RINGING is
  313. special cased in dial(), and incoming RING is special cased in answer(). */
  314.         case 2: strcpy(mdmresult,"Ring!"); n= 0; break;
  315.         case 8: strcpy(mdmresult,"No Answer"); n= 0; break;
  316.         case 3: strcpy(mdmresult,"No Carrier"); n= 0; break;
  317.         case 6: strcpy(mdmresult,"No Dial Tone!"); n= 0; break;
  318.         case 7: strcpy(mdmresult,"Busy"); n= 0; break;
  319.  
  320. /* These cause idle for incoming calls, and error-terminate dialing */
  321.         case 4: strcpy(mdmresult,"Command error!"); break;
  322.         case 12: strcpy(mdmresult,"Voice!"); break;
  323.  
  324. /* This is just a plain old fucking error */
  325.         case 0:
  326.         default: 
  327.              *mdmresult= NUL; n= 0; break;    /* usually nothing */
  328.     }
  329.  
  330. /* Do some setup if we are connected. */
  331.  
  332.     if (n == 1) {
  333.         setbaud(r);            /* set baud rate, */
  334.         flush(20);            /* flush trash */
  335.         sprintf(mdmresult,"Connected at %,d%s",datarate,s);
  336.     }
  337.     if (*mdmresult) {
  338.         puts("Modem says: \"");
  339.         puts(mdmresult);
  340.         puts("\"\r\n");
  341.     }
  342.     return(n);
  343. }
  344.  
  345. /* Return the last connect string. */
  346.  
  347. lastconnect() {
  348.  
  349.     return(mdmresult);
  350. }
  351.  
  352. /* Check the modem for a result code, return its numerical value or -1
  353. if none yet. This uses the modem buffer to store characters between
  354. iterations. */
  355.  
  356. chk_modem() {
  357. #define i (mdmbuff[0])        /* the index into the buffer */
  358. #define buff (&mdmbuff[1])    /* what we use as the buffer */
  359.  
  360.     cd_flag= 1;                /* we are NOT online! */
  361.     if (_mconstat()) {            /* if a character there, */
  362.         buff[i]= _mconin() & 0x7f;    /* get it, */
  363.         if (buff[i] == CR) {        /* if a complete line, */
  364.             i= 0;            /* empty the line, */
  365.             return(atoi(buff));    /* return the result */
  366.  
  367.         } else if (isdigit(buff[i])) {    /* if a new digit, */
  368.             ++i;            /* install it, */
  369.         }
  370.         buff[i]= NUL;            /* terminate string & erase non-digit */
  371.     }
  372.     return(-1);                /* nothing happened */
  373. }
  374.