home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BTMTSRC3.ZIP / SENDSYNC.C < prev    next >
C/C++ Source or Header  |  1991-08-14  |  15KB  |  369 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software, Co.                       */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          Freely Available<tm> Software.                 */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  (C) Copyright 1987-90, Bit Bucket Software Co., a Delaware Corporation. */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*               This module was written by Vince Perriello                 */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*          FidoNet(R) Mail Session Calling System Synchronization          */
  17. /*                                                                          */
  18. /*                                                                          */
  19. /*    For complete  details  of the licensing restrictions, please refer    */
  20. /*    to the License  agreement,  which  is published in its entirety in    */
  21. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.240.    */
  22. /*                                                                          */
  23. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  24. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  25. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  26. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  27. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  28. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  29. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  30. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  31. /*                                                                          */
  32. /*                                                                          */
  33. /* You can contact Bit Bucket Software Co. at any one of the following      */
  34. /* addresses:                                                               */
  35. /*                                                                          */
  36. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:132/491, 1:141/491  */
  37. /* P.O. Box 460398                AlterNet 7:491/0                          */
  38. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  39. /*                                Internet f491.n132.z1.fidonet.org         */
  40. /*                                                                          */
  41. /* Please feel free to contact us at any time to share your comments about  */
  42. /* our software and/or licensing policies.                                  */
  43. /*                                                                          */
  44. /*                                                                          */
  45. /*--------------------------------------------------------------------------*/
  46.  
  47.  
  48.  
  49. extern void pascal com_direct(unsigned char c);        /* CML */
  50.  
  51.  
  52.  /*
  53.     Sender Synchronization state table.
  54.    
  55.     This logic is used by the calling system. It will determine the
  56.     type of mail transfer which can be used in communicating with
  57.     the called system.
  58.    
  59.     This stuff was copied almost verbatim from a file sent to us by TJ.
  60.     (Of course, then we hacked the heck out of it!!!)
  61.   
  62.     Thanks, Tom.
  63.   
  64.  
  65.  .-----+----------+-------------------------+-------------------------+-----.
  66.  | SS0 | SyncInit |                         | Prepare 3 sec Sync timer|     |
  67.  |     |          |                         | Prepare .5 sec NAK tmr  |     |
  68.  |     |          |                         | Init NAK Count          |     |
  69.  |     |          |                         | Start 60 sec master tmr | SS1 |
  70.  |-----+----------+-------------------------+-------------------------+-----|
  71.  | SS1 | SendSync | 1. Over 60 seconds      |                         |     |
  72.  |     |          |    or carrier lost      | no response             | exit|
  73.  |     |          +-------------------------+-------------------------+-----|
  74.  |     |          | 2. 3 sec elapsed        | Clear Inbound buffer    |     |
  75.  |     |          |    or timer not started | Send YOOHOO, then TSYNC |     |
  76.  |     |          |                         | Start 3 sec Sync timer  | SS2 |
  77.  |     |          +-------------------------+-------------------------+-----|
  78.  |     |          | 3. not elapsed          |                         | SS2 |
  79.  |-----+----------+-------------------------+-------------------------+-----|
  80.  | SS2 | WaitResp | 1. Nothing received     | require a response      | SS1 |
  81.  |     |          +-------------------------+-------------------------+-----|
  82.  |     |          | 2. ENQ received         | Wazoo Protocol selected | exit|
  83.  |     |          +-------------------------+-------------------------+-----|
  84.  |     |          | 3. 'C' received         | probable FSC001         | SS3 |
  85.  |     |          +-------------------------+-------------------------+-----|
  86.  |     |          | 4. NAK received         | probable FSC001         | SS3 |
  87.  |     |          +-------------------------+-------------------------+-----|
  88.  |     |          | 5. Debris (might include| Reset NAK timer         |     |
  89.  |     |          |    (YOOHOO|TSYNC) & 127)| if started              | SS1 |
  90.  |-----+----------+-------------------------+-------------------------+-----|
  91.  | SS3 | NAKTmr   | 1. Timer not expired    | Zero NAK count          |     |
  92.  |     |          |    or timer not started | Start .5 sec NAK timer  | SS1 |
  93.  |     |          +-------------------------+-------------------------+-----|
  94.  |     |          | 2. Timer expired        | Bump NAK count          | SS4 |
  95.  |-----+----------+-------------------------+-------------------------+-----|
  96.  | SS4 | NAKCount | 1. Count >= 2?          | assume FSC001           | exit|
  97.  |     |          +-------------------------+-------------------------+-----|
  98.  |     |          | 2. Count < 2            | Keep looking            | SS1 |
  99.  `-----+----------+-------------------------+-------------------------+-----'
  100.  
  101.  
  102.  */
  103.  
  104. /* System include files */
  105.  
  106. #include <stdio.h>
  107.  
  108. /* Local include files */
  109.  
  110. #include "defines.h"
  111. #include "com.h"
  112. #include "xfer.h"
  113. #include "zmodem.h"
  114. #include "keybd.h"
  115. #include "sbuf.h"
  116. #include "sched.h"
  117. #include "externs.h"
  118. #include "prototyp.h"
  119.  
  120.  
  121. /*
  122.  * Data structure used by all SendSync state machine functions.
  123.  * Contains all data which needs to be passed between various states.
  124.  *
  125.  */
  126.  
  127. typedef struct {
  128.    long NAK_Timer;                      /* 1/2 second NAK interval timer    */
  129.    long SendSyncTimer;                  /* 3 second SendSync Timer          */
  130.    long Master_Timer;                   /* 60 second master timeout         */
  131.    int  NAK_Count;                      /* Count of NAK's received          */
  132.    int  result;                         /* Result we want to send out       */
  133. } SSARGS, *SSARGSP;
  134.  
  135. static int SSSyncInit (SSARGSP, int);   /* Called by state machine at start */
  136. static int SSExit     (SSARGSP, int);   /* Called by state machine at end   */
  137. static int SSSendSync (SSARGSP);        /* SS1 state processing function    */
  138. static int SSWaitResp (SSARGSP);        /* SS2 state processing function    */
  139. static int SSNAKTmr   (SSARGSP);        /* SS3 state processing function    */
  140. static int SSNAKCount (SSARGSP);        /* SS4 state processing function    */
  141.  
  142.  
  143. #define SS0    0                        /* Reserved value of 0 for init     */
  144. #define SSexit 0                        /* Slot 1 is exit, but called by 0  */
  145. #define SS1    2                        /* First "user" slot is 2.          */
  146. #define SS2    3                        /* After that, it all maps n : n+1  */
  147. #define SS3    4
  148. #define SS4    5
  149. #define SS5    6
  150.  
  151.  
  152. static STATES Send_Sync [] = {          /* Table used by state machine      */
  153.    { "SSSyncInit", SSSyncInit },        /* And referred to by 'SSn' defines */
  154.    { "SSExit",     SSExit },            /* listed above ...                 */
  155.    { "SSSendSync", SSSendSync },
  156.    { "SSWaitResp", SSWaitResp },
  157.    { "SSNAKTmr",   SSNAKTmr },
  158.    { "SSNAKCount", SSNAKCount },
  159. };
  160.  
  161.  
  162. /*
  163.  * CallerSendSync
  164.  * Determine whether we are talking to an FTS-0001 mailer or an FTS-0006
  165.  * mailer. Use the general state machine driver.
  166.  *
  167.  * This is the only external entry point into this module.
  168.  *
  169.  */
  170.  
  171. int CallerSendSync (void)
  172. {
  173.    SSARGS args;
  174.    int res;
  175.  
  176.    args.result = 0;
  177.  
  178.    res = state_machine (Send_Sync, &args, 2);
  179.    return (res);
  180. }
  181.  
  182.  
  183.  
  184. /*
  185.  * This routine is called by the state machine when the 'SSexit'
  186.  * state is seen. Its return value is what the state machine
  187.  * will return to its caller as the result of the function.
  188.  *
  189.  */
  190.  
  191. static int SSExit (args, cur_state)
  192. SSARGSP args;
  193. int cur_state;
  194. {
  195.    cur_state = args->result;    /* Not meaningful. Makes compiler happy */
  196.    return (args->result);
  197. }
  198.  
  199.  
  200.  
  201.  /*
  202.  
  203.  .-----+----------+-------------------------+-------------------------+-----.
  204.  | SS0 | SyncInit |                         | Prepare 3 sec Sync timer|     |
  205.  |     |          |                         | Prepare .5 sec NAK tmr  |     |
  206.  |     |          |                         | Init NAK Count          |     |
  207.  |     |          |                         | Start 60 sec master tmr | SS1 |
  208.  `-----+----------+-------------------------+-------------------------+-----'
  209.  
  210.   */
  211.  
  212.  
  213. static int SSSyncInit (args, start_state)
  214. SSARGSP args;
  215. int start_state;
  216. {
  217.    args->NAK_Timer = args->SendSyncTimer = 0L;
  218.    args->NAK_Count = 0;
  219.    args->Master_Timer = timerset (6000);
  220.    start_state = SS1;           /* Not meaningful. Makes compiler happy */
  221.    return (SS1);
  222. }
  223.  
  224.  /*
  225.  
  226.  .-----+----------+-------------------------+-------------------------+-----.
  227.  | SS1 | SendSync | 1. Over 60 seconds      |                         |     |
  228.  |     |          |    or carrier lost      | no response             | exit|
  229.  |     |          +-------------------------+-------------------------+-----|
  230.  |     |          | 2. 3 sec elapsed        | Clear Inbound buffer    |     |
  231.  |     |          |    or timer not started | Send YOOHOO, then TSYNC |     |
  232.  |     |          |                         | Start 3 sec Sync timer  | SS2 |
  233.  |     |          +-------------------------+-------------------------+-----|
  234.  |     |          | 3. not elapsed          |                         | SS2 |
  235.  `-----+----------+-------------------------+-------------------------+-----'
  236.  
  237.   */
  238.  
  239.  
  240. static int SSSendSync (args)
  241. SSARGSP args;
  242. {
  243.    if (!(CARRIER) || (timeup (args->Master_Timer))) {
  244.       args->result = 0;         /* Failure */
  245.       return (SSexit);
  246.    }
  247.    
  248.    if (!(args->SendSyncTimer) || timeup (args->SendSyncTimer)) {
  249.       CLEAR_INBOUND ();
  250.       if (!no_WaZOO && !no_WaZOO_Session)        /* If we're WaZOO,   */
  251.          com_direct (YOOHOO);            /* CML */
  252.       com_direct (TSYNC);        /* CML */
  253. //      args->SendSyncTimer = timerset (300);
  254.       args->SendSyncTimer = timerset (600);        /* CML - Bend rules a little under OS/2 */
  255.    }
  256.    return (SS2);
  257. }
  258.  
  259.  
  260.  /*
  261.  
  262.  .-----+----------+-------------------------+-------------------------+-----.
  263.  | SS2 | WaitResp | 1. Nothing received     | require a response      | SS1 |
  264.  |     |          +-------------------------+-------------------------+-----|
  265.  |     |          | 2. ENQ received         | Wazoo Protocol selected | exit|
  266.  |     |          +-------------------------+-------------------------+-----|
  267.  |     |          | 3. 'C' received         | probable FSC001         | SS3 |
  268.  |     |          +-------------------------+-------------------------+-----|
  269.  |     |          | 4. NAK received         | probable FSC001         | SS3 |
  270.  |     |          +-------------------------+-------------------------+-----|
  271.  |     |          | 5. Debris (might include| Reset NAK timer         |     |
  272.  |     |          |    (YOOHOO|TSYNC) & 127)| if started              | SS1 |
  273.  `-----+----------+-------------------------+-------------------------+-----'
  274.  
  275.   */
  276.  
  277.  
  278. static int SSWaitResp (args)
  279. SSARGSP args;
  280. {
  281.    int i;
  282.    int exit_code;
  283.    
  284.    if (!CHAR_AVAIL ()) {
  285.       return (SS1);
  286.    }
  287.    
  288.    i = PEEKBYTE ();
  289.    
  290.    switch (i) {
  291.    
  292.       case 'C':
  293.       case NAK:
  294.          exit_code = SS3;
  295.          break;
  296.  
  297.       case ENQ:
  298.          if (!no_WaZOO && !no_WaZOO_Session) {      /* If we're WaZOO,   */
  299.             args->result = 2;      /* WaZOO */
  300.             exit_code = SSexit;
  301.             break;
  302.          }         
  303.  
  304.       /* Deliberately fall through from ENQ if we're not doing WaZOO */
  305.  
  306.       default:
  307.          (void) TIMED_READ (0);
  308.          if (args->NAK_Timer) {
  309. //            args->NAK_Timer = timerset (50);
  310.             args->NAK_Timer = timerset (100);        /* CML - bend rules a little under OS/2 */
  311.          }
  312.          exit_code = SS1;
  313.          break;
  314.     }
  315.  
  316.    return (exit_code);
  317. }
  318.  
  319.  /*
  320.  
  321.  .-----+----------+-------------------------+-------------------------+-----.
  322.  | SS3 | NAKTmr   | 1. Timer not expired    | Zero NAK count          |     |
  323.  |     |          |    or timer not started | Start .5 sec NAK timer  | SS1 |
  324.  |     |          +-------------------------+-------------------------+-----|
  325.  |     |          | 2. Timer expired        | Bump NAK count          | SS4 |
  326.  `-----+----------+-------------------------+-------------------------+-----'
  327.  
  328.   */
  329.  
  330.  
  331. static int SSNAKTmr (args)
  332. SSARGSP args;
  333. {
  334.    if (!(args->NAK_Timer) || !timeup (args->NAK_Timer)) {
  335.       args->NAK_Count = 0;
  336. //      args->NAK_Timer = timerset (50);
  337.       args->NAK_Timer = timerset (100);        /* CML - bend rules a little under OS/2 */
  338.       (void) TIMED_READ (0);
  339.       return (SS1);
  340.    } else {
  341.       (args->NAK_Count)++;
  342.       return (SS4);
  343.    }
  344. }
  345.  
  346.  /*
  347.  
  348.  .-----+----------+-------------------------+-------------------------+-----.
  349.  | SS4 | NAKCount | 1. Count >= 2?          | assume FSC001           | exit|
  350.  |     |          +-------------------------+-------------------------+-----|
  351.  |     |          | 2. Count < 2            | Keep looking            | SS1 |
  352.  `-----+----------+-------------------------+-------------------------+-----'
  353.  
  354.   */
  355.  
  356.  
  357. static int SSNAKCount (args)
  358. SSARGSP args;
  359. {
  360.    if (args->NAK_Count < 2) {
  361.       (void) TIMED_READ (0);
  362.       return (SS1);
  363.    } else {
  364.       args->result = 1;         /* FSC001 */
  365.       return (SSexit);
  366.    }
  367. }
  368.  
  369.