home *** CD-ROM | disk | FTP | other *** search
/ nisttime.carsoncity.k12.mi.us / nisttime.carsoncity.k12.mi.us.tar / nisttime.carsoncity.k12.mi.us / pub / lockclock / dial_l.c < prev    next >
C/C++ Source or Header  |  1996-11-18  |  8KB  |  239 lines

  1. int dial(int alt_num)
  2. {
  3. /*
  4.         this subroutine dials a hayes modem connected
  5.         to the com port using calls to wrtbuf and rdbuf.
  6.  
  7.         if the first character in the dialing sequence (after
  8.         the ATD) is M (or m), then "manual" dialing is assumed.
  9.         in that case, just return and wait for received message.
  10.  
  11.     the manual dialing capability is retained for compatibility
  12.     with the ACTS version and will not normally be used.
  13.  
  14.         if echo is set to zero by the configuration file, then
  15.         echoes are not expected from the modem.  if echo is not zero
  16.         then echoes are expected and a fatal error exists if
  17.         echoes are not received within the time out period.
  18.  
  19.         this version of the program (7 jan. 92) uses time-out values
  20.         that are specified in units of clock ticks.  this method
  21.         eliminates the need for time-out values that are scaled by
  22.         the CPU speed and is intended to be better suited to the
  23.         faster 386 and 486 configurations where cpuspd sometimes does
  24.         not give the correct relative speed value so that the
  25.         time-outs are wrong (usually too short).
  26.  
  27.     this version is slightly different from the version that is
  28.     used with ACTS.  The ACTS version prints a message on the
  29.     terminal in case of a problem; this version does not
  30.     do any output but simply returns a status value.  the
  31.     status values are +1 for connection, 0 for no answer and
  32.     negative values for various modem problems and time-outs.
  33.  
  34.     (version of 11 March 1996).  If the primary ACTS telephone
  35.     number does not work and if the problem appears to be in
  36.     the ACTS service (rather than the modem itself), then the
  37.     program will try again with the alternate telephone number.
  38.     if input parameter alt_num is not zero then the alternate
  39.     ACTS number is tried immediately.
  40. */
  41. #include "getdif.h"
  42. #include <stdio.h>
  43. #ifdef IBMPC
  44. #include <dos.h>
  45. #endif
  46. void wrtbuf(),wait1s();
  47. void hangup();
  48. int rdbuf();
  49. void putmsg(char *);        /*writes message to error file */
  50. char msgbuf[80];        /*holds message for putmsg */
  51. char *ptr;
  52. char ans[400],cc;
  53. char ie1 = 'K';
  54. char ie2 = '\n';
  55. char ie3 = '\r';
  56. char ie4 = 'Y';        /* response is BUSY */
  57. char ie5 = 'T';        /* response is CONNECT */
  58. char ie6 = 'R';        /* response is NO ANSWER or NO CARRIER */
  59. char ie7 =  -120;
  60. int j,k,kk,stat;
  61. #ifdef IBMPC
  62. int tmo  =  -200;     /* time-out limit, roughly 10 seconds */
  63. int ttmo =  -900;    /* wait up to 50 sec for number to be dialed */
  64. #endif
  65. #ifdef SUN
  66. int tmo = -10;
  67. int ttmo= -200;     /* this is a LONG time-out. */
  68. #endif
  69. again:        /*try to dial the number from the start */
  70. /*
  71.     send string ATZ, expect response of OK unless echo == 0
  72. */
  73.         ptr="ATZ\r";
  74.         wrtbuf(ptr);
  75.         if( (rdbuf(ans,ie1,ie7,ie7,tmo) == 0) && (echo != 0) ) 
  76.        {
  77.        if(retry-- == 0) return(-1);
  78.        hangup();
  79.        goto tryit;        /*wait a bit and try again*/
  80.     }
  81. /*
  82.     look for <cr><lf> following the OK
  83. */
  84.         if( (rdbuf(ans,ie2,ie7,ie7,tmo) == 0) && (echo != 0) ) 
  85.        {
  86.        if(retry-- == 0) return(-2);
  87.        hangup();
  88.        goto tryit;
  89.     }
  90.         wait1s();
  91. /*
  92.         set: echo on, full duplex, speaker off, basic word responses on
  93.     expect echo of OK <cr>
  94. */
  95.         ptr="AT E1 M1 Q0 V1\r";
  96.         wrtbuf(ptr);
  97.         if( (rdbuf(ans,ie1,ie7,ie7,tmo) == 0) && (echo !=0) ) return(-3);
  98.         wait1s();
  99. /*
  100.     now send the dialing string.  switch to the alternate number
  101.     when there are only three re-tries left.  Since the default
  102.     value for retry is 6, this results in 3 tries with the primary
  103.     number followed by three with the alternate one.  Try the
  104.     alternate number immediately if input parameter alt_num
  105.     is non zero.
  106. */
  107.         if( (retry > 3) && (alt_num == 0) ) wrtbuf(number);
  108.     else          wrtbuf(alt_number);
  109.         if( (rdbuf(ans,ie3,ie7,ie7,tmo) == 0) && (echo !=0) ) return(-4);
  110. /*
  111.     now wait for response from modem.  there are several
  112.     possibilities as follows:
  113.     1.  modem does not response at all.  this is almost
  114.         certainly an error -- flag immediately if echo
  115.         mode enabled. otherwise parse response.
  116.  
  117.     2.  read terminates on R from RINGING -- only some modems
  118.         can do this -- go back and read some more. do not
  119.         reset buffer pointer here since response is only
  120.         partial.
  121.  
  122.     3.  read terminates on R from NO ANSWER or NO CARRIER --
  123.         that's all folks.  Same thing if read terminates on Y
  124.         from BUSY.
  125.  
  126.     4.  read terminates on T from CONNECT -- the kind of
  127.         stuff we like to see.
  128.  
  129.     5.  read terminates on T from AT or ATDT -- throw
  130.         it away and read some more.  this should only
  131.         happen on SUN machines where the reads are buffered
  132.         on IBMPC where the input port is not buffered the
  133.         ATDT string should have been overwritten by the
  134.         digits of the number.
  135.     the variable stat is 0 until we are finished and is
  136.     1 when the final modem status has been received
  137.     kk points to the origin of the buffer for the current
  138.     read.  it is zero unless a partial answer has been
  139.     received (NO CAR for example).
  140. */
  141.     stat=0;
  142.     kk=0;
  143.     do
  144.     {
  145. /*
  146.        read some stuff, abort if timeout and no response
  147.        (modem should have given status of call).
  148.  
  149.        when rdbuf returns, k points to terminator relative to
  150.        origin which is kk.
  151. */
  152.            if( (k=rdbuf(&ans[kk],ie4,ie5,ie6,ttmo)) == 0 ) return(-5);
  153. /*
  154.        if we read more than one character, and
  155.        if end of message is ER, we have final modem status.
  156.        answer is either NO ANSWER or NO CARRIER. bad news
  157.        either way but we are finished.
  158.        likewise if the message ends in SY, then the call
  159.        status is BUSY.  Also bad news, but we are finished
  160. */
  161.            if( (k  > 1) && (ans[kk+k-1] == 'R') && (ans[kk+k-2] == 'E'))
  162.           stat=1;
  163.        if( (k  > 1) && (ans[kk+k-1] == 'Y') && (ans[kk+k-2] == 'S'))
  164.           stat=1;
  165. /*
  166.        if we read more than one character and
  167.        if end of message is CT we have final modem status.
  168.        answer is CONNECT (possibly CONNECT 1200).
  169. */
  170.        if( (k > 1) && (ans[kk+k-1] == 'T') && (ans[kk+k-2] == 'C') )
  171.           stat=1;
  172. /*
  173.        if we terminated on an R and it is the first character, then
  174.        it is the R of RINGING. if we read more than one character
  175.        and we ended with an R and the previous character was not E,
  176.        then we are in the midst of NO CARRIER  -- keep on reading.
  177. */
  178.        if( (kk+k > 1) && (ans[kk+k-1] == 'R') && (ans[kk+k-2] != 'E') )
  179.           kk += k;
  180.        if( (kk+k == 1) && (ans[0] == 'R') )
  181.           kk += k;
  182. /*
  183.        if we have read 200 characters and still not found a
  184.        terminator of some sort then something is wrong.  there
  185.        is probably lots of line noise.  in any case, we will
  186.        abandon this call and try again.
  187. */
  188.        if( (stat == 0) && (kk > 200) )
  189.           {
  190.           sprintf(ans,"NO CARRIER");
  191.           stat = 1;            /*get out of the loop*/
  192.        }
  193.     } while (stat == 0);
  194. /*
  195.     since modem response was set to words, skip leading
  196.     extraneous stuff including remnants of number and possible
  197.     leading newline. first loop ends when a letter is found,
  198.     and j points to it.
  199. */
  200.     for(j=0; ( (j < k+kk) && (ans[j] < 'A')) ; j++)  ;
  201.         for(k=j; (cc=ans[k]) != 0; k++) 
  202.        {
  203.        if(cc == 'T') 
  204.          {
  205.          if(retry > 3) return(1); /*connect on default number!*/
  206.          return(11);          /*connect on alternate number*/
  207.        }
  208.     }
  209. /*
  210.     if this attempt did not work and retries are either 
  211.     exhausted or disabled then too bad.  Return to caller 
  212.     with a failure status
  213.  
  214.     else if manual retries have been requested, then prompt
  215.     operator and either retry or exit depending on response.
  216.  
  217.     else if automatic retries were selected try again. Decrement
  218.     retry to keep track of how many times we have done this.
  219.     Eventually there will be no more left and then
  220.     we will return to caller with a failure status 
  221. */
  222.         if(retry == 0) return(0);
  223. /*
  224.     if we are here then retry > 0 and automatic retries are
  225.     selected and available.  So decrement the count and then
  226.     go around again.  This loop will either eventually connect
  227.     or retry will go to 0 and we will return a failed status
  228.     above.
  229. */
  230.     retry--;
  231. tryit:        /*   wait a few seconds and then try it again */
  232.     sprintf(msgbuf,"\nfailed in dial, retry=%d\n",retry);
  233.     putmsg(msgbuf);
  234.     putmsg(ans);
  235.     wait1s();
  236.     wait1s();
  237.     goto again;
  238. }
  239.