home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_09 / 8n09094a < prev    next >
Text File  |  1990-06-24  |  8KB  |  258 lines

  1. /*------------------------------------------------
  2. XMODEM.H
  3.  
  4. Author        Date        Description
  5. -------------------------------------------
  6. Jon Ward    22 Apr 90    Initial Revision.
  7. ------------------------------------------------*/
  8.  
  9. #ifndef XMODEM_H
  10. #define XMODEM_H
  11.  
  12. /*------------------------------------------------
  13.           Xmodem Transfer Errors
  14. ------------------------------------------------*/
  15. enum xmodem_errors
  16.   {
  17.   XERR_OK,        /* No errors */
  18.  
  19.   XERR_XMIT_FUNC,    /* NULL Transmit function pointer */
  20.   XERR_RCVR_FUNC,    /* NULL Receive function pointer */
  21.  
  22.   XERR_RCVR_CANCEL,    /* Receiver cancelled the transfer */
  23.   XERR_SEND_CANCEL,    /* Sender cancelled the transfer */
  24.   XERR_USER_CANCEL,    /* User cancelled the transfer */
  25.  
  26.   XERR_FILE_READ,    /* Error reading the file */
  27.   XERR_FILE_WRITE,    /* Error writing the file */
  28.  
  29.   XERR_ACK_TIMEOUT,    /* Timed out waiting for data pack ACK */
  30.   XERR_NAK_TIMEOUT,    /* Timed out waiting for initial NAK */
  31.   XERR_SOH_TIMEOUT,    /* Timed out waiting for SOH */
  32.   XERR_DATA_TIMEOUT,    /* Timed out waiting for data */
  33.   XERR_LAST_ACK_TIMEOUT,/* Timed out waiting for final ACK */
  34.  
  35.   XERR_INVALID_SOH,    /* Invalid char waiting for SOH */
  36.   XERR_INVALID_BLOCK_NUM,/* Block mismatch in packet header */
  37.   XERR_INVALID_CRC,    /* CRC is incorrect */
  38.   XERR_INVALID_CHECKSUM,/* Checksum is incorrect */
  39.   XERR_BLOCK_SEQUENCE,    /* Block out of sequence */
  40.  
  41.   XERR_CHAR_ERROR,    /* Received character error */
  42.   XERR_OFFLINE,        /* Modem not online */
  43.  
  44.   XERR_BLOCK_NAK,    /* NAK received */
  45.   };
  46.  
  47.  
  48. #ifdef XMODEM_LIB
  49. /*------------------------------------------------
  50. Number of attempts and timeout for each attempt
  51. to transferr using CRC.
  52. ------------------------------------------------*/
  53. #define CRC_RETRY_COUNT        4
  54. #define CRC_TIMEOUT        (3L * MS_PER_SEC)
  55.  
  56. /*------------------------------------------------
  57. Number of attempts and timeout for each attempt
  58. to Negative Acknowledge a bad packet.  This also
  59. includes the initial NAK to get the thing started.
  60. ------------------------------------------------*/
  61. #define NAK_RETRY_COUNT        10
  62. #define NAK_TIMEOUT        (10L * MS_PER_SEC)
  63.  
  64. /*------------------------------------------------
  65. Number of attempts and timeout for each attempt
  66. to Acknowledge a good packet.
  67. ------------------------------------------------*/
  68. #define ACK_RETRY_COUNT        10
  69. #define ACK_TIMEOUT        (10L * MS_PER_SEC)
  70.  
  71. /*------------------------------------------------
  72. Number of consecutive CANs and the timeout between
  73. them.
  74. ------------------------------------------------*/
  75. #define CAN_COUNT_ABORT        2
  76. #define CAN_TIMEOUT        (2L * MS_PER_SEC)
  77.  
  78. /*------------------------------------------------
  79. Timeout between packet data bytes.
  80. ------------------------------------------------*/
  81. #define DATA_TIMEOUT        (1L * MS_PER_SEC)
  82.  
  83. /*------------------------------------------------
  84. Number of false start characters that the sender
  85. can receive before aborting the transmission.
  86. ------------------------------------------------*/
  87. #define START_XMIT_RETRY_COUNT    10
  88. #define START_XMIT_TIMEOUT    (60L * MS_PER_SEC)
  89.  
  90. /*------------------------------------------------
  91. Maximum number of attempts to send a single block.
  92. ------------------------------------------------*/
  93. #define BLOCK_RETRY_COUNT        10
  94.  
  95. /*------------------------------------------------
  96. Timeout for block responses.
  97. ------------------------------------------------*/
  98. #define BLOCK_RESP_TIMEOUT    (10L * MS_PER_SEC)
  99.  
  100. /*------------------------------------------------
  101. Macro to purge all characters from the receive
  102. buffer.  This waits until there are no characters
  103. received for a 1 second period.
  104. ------------------------------------------------*/
  105. #define PURGE_RECEIVER(c) \
  106. while ((*(c)) (MS_PER_SEC, NULL) != RECV_TIMEOUT)
  107.  
  108. #endif    /* XMODEM_LIB */
  109.  
  110. /*------------------------------------------------
  111. Number of milliseconds in a second.  I know this
  112. is silly, but it makes the meaning a little more
  113. obvious.
  114. ------------------------------------------------*/
  115. #define MS_PER_SEC        1000L
  116.  
  117. /*------------------------------------------------
  118. Block sizes for normal and for 1K XMODEM
  119. transfers.
  120. ------------------------------------------------*/
  121. #define XMODEM_BLOCK_SIZE    128
  122. #define XMODEM_1K_BLOCK_SIZE    1024
  123.  
  124. /*------------------------------------------------
  125. Values returned by the receive character serial
  126. interface function.
  127. ------------------------------------------------*/
  128. #define RECV_TIMEOUT    -1 /* receiver timed-out */
  129.  
  130. /*------------------------------------------------
  131. Error bits stored in the receiv character error
  132. flag.
  133. ------------------------------------------------*/
  134. #define RE_OVERRUN_    0x01    /* receiver overrun error */
  135. #define RE_PARITY_    0x02    /* receiver parity error */
  136. #define RE_FRAMING_    0x04    /* receiver framing error */
  137.  
  138. /*------------------------------------------------
  139. Values returned by the transmit character serial
  140. interface function.
  141. ------------------------------------------------*/
  142. #define XMIT_OK        0    /* character enqued for transmission */
  143. #define XMIT_OFFLINE    -1    /* modem offline */
  144.  
  145. /*------------------------------------------------
  146.        ASCII values used by XMODEM
  147. ------------------------------------------------*/
  148. #define SOH            1
  149. #define STX            2
  150. #define EOT            4
  151. #define ACK            6
  152. #define BACKSPACE        8
  153. #define NAK            21
  154. #define CAN            24
  155.  
  156. #define CPMEOF            26
  157.  
  158. /*------------------------------------------------
  159. XMODEM block description structure.  This is only
  160. used internally by the send and receive routines
  161. and should not be visible outside of these.
  162. ------------------------------------------------*/
  163. #ifdef XMODEM_LIB
  164. struct xmodem_block_st
  165.   {
  166.   long total_block_count;    /* total blocks transferred */
  167.   long total_byte_count;    /* total bytes transferred */
  168.  
  169.   unsigned char start_char;    /* block starting character */
  170.   unsigned char block_num;    /* transmission block number */
  171.   unsigned char not_block_num;    /* one's complement block number */
  172.   char buffer [XMODEM_1K_BLOCK_SIZE + 1];    /* data buffer */
  173.   int buflen;            /* buffer length (128 or 1024) */
  174.   unsigned char checksum;    /* data checksum */
  175.   unsigned int crc;        /* data CRC-16 */
  176.  
  177.   unsigned int crc_used: 1;    /* 0=Checksum 1=CRC-16 */
  178.   };
  179.  
  180. typedef struct xmodem_block_st xblock;
  181. #endif    /* XMODEM_LIB */
  182.  
  183. /*------------------------------------------------
  184. XMODEM function pointer structure.
  185. ------------------------------------------------*/
  186. #ifdef XMODEM_LIB
  187. struct xmodem_func_st
  188.   {
  189.   int (*transmit) (        /* xmit function */
  190.     char);
  191.  
  192.   int (*receive) (        /* recv function */
  193.     long,
  194.     unsigned int *);
  195.  
  196.   void (*dispstat) (        /* display function */
  197.     long,
  198.     long,
  199.     const char *);
  200.  
  201.   int (*check_abort) (void);    /* manual abort function */
  202.   };
  203.  
  204. typedef struct xmodem_func_st xfunc;
  205. #endif    /* XMODEM_LIB */
  206.  
  207. /*------------------------------------------------
  208.             XMODEMR.C
  209. ------------------------------------------------*/
  210. int xmodem_recv (
  211.   FILE *f,                    /* file to write to */
  212.   int (*transmit) (char),            /* xmit function */
  213.   int (*receive) (long, unsigned int *),    /* recv function */
  214.   void (*dispstat) (long, long, const char *),    /* display function */
  215.   int (*check_abort) (void));            /* manual abort function */
  216.  
  217.  
  218. #ifdef XMODEM_LIB
  219. int xm_perror (
  220.   int error,                /* error number */
  221.   xfunc *xmf);                /* xmodem external functions */
  222.  
  223. void xm_no_disp_func (
  224.   long a,
  225.   long b,
  226.   const char *buf);
  227.  
  228. int xm_no_abort_func (void);
  229. #endif    /* XMODEM_LIB */
  230.  
  231.  
  232.  
  233. /*------------------------------------------------
  234.             XMODEMS.C
  235. ------------------------------------------------*/
  236. int xmodem_send (
  237.   int block_size,                /* maximum block size */
  238.   FILE *f,                    /* file to write to */
  239.   int (*transmit) (char),            /* xmit function */
  240.   int (*receive) (long, unsigned int *),    /* recv function */
  241.   void (*dispstat) (long, long, const char *),    /* display function */
  242.   int (*check_abort) (void));            /* manual abort function */
  243.  
  244. #ifdef XMODEM_LIB
  245. unsigned int xm_update_CRC (
  246.   unsigned int crc,        /* current CRC */
  247.   unsigned int c);        /* character to add to CRC */
  248.  
  249. void xm_send_cancel (
  250.   int (*transmit) (char));    /* transmit function */
  251. #endif    /* XMODEM_LIB */
  252.  
  253.  
  254. #endif        /* XMODEM_H */
  255.  
  256.  
  257.  
  258.