home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 December / simtel1292_SIMTEL_1292_Walnut_Creek.iso / msdos / ddjmag / ddj8910.arc / SMITH2.ARC / XMUTIL.C < prev   
Text File  |  1989-04-26  |  4KB  |  135 lines

  1. /* XMUTIL.C Xmodem utility procedures.  Includes CRC generation and other
  2.  *    utility routines not in xmsend.c (send state machine) and
  3.  *    xmrecv.c (receive state machine)
  4.  *  GOAL:  Provide high level interfaces to interrupt handler routines.
  5.  *         Includes init() which sits between interrupt handler and cterm.
  6.  *  NOTE:  Do no printf's in this file.
  7.  */
  8.  
  9. #include "commint.h"   /* defines and structs for Turbo C int handler */
  10. #include "cterm.h"     /* project header goes here */
  11. #include "commn.h"     /* definition of S_INIT structure */
  12.  
  13. #define MAXJUNK  100   /* used by eat_noise */
  14.  
  15. /* Messages posted by A_Send_End and A_Recv_End */
  16. char user_msg[] = "Terminated by user";
  17. char nonak[]    = "No initial NAK/CRC received";
  18. char cancel[]   = "Cancel Received";
  19. char badcomm[]  = "Problem Reading/Writing to comm port";
  20. char badread[]  = "Problem reading disk file";
  21. char badwrite[] = "Problem writing disk file";
  22. char eof_msg[]  = "File successfully transferred";
  23. char giveup[]   = "Retries exhausted by NAK's and/or timeouts";
  24.  
  25. /* ------- External references */
  26. extern int crc;                     /* external flag for CRC or Checksum */
  27. extern void interrupt rxrdy_IH();   /* in module commint.c */
  28. extern unsigned char getcomch();
  29. extern S_INIT cur_config;
  30.  
  31. /* Calls the following from commint.c module:
  32. extern int Remove_IH();
  33. extern int Inst_IH();         */
  34. /* Calls the following from ctermx.c module:
  35. extern int Config_Comm();     */
  36.  
  37. /* Export to xmsend and xmrecv routines */
  38. unsigned crcaccum;
  39. unsigned char checksum;
  40. int crc = 1;                  /* flag for CRC (!0) or checksum  (0) */
  41.  
  42. void updcrc( unsigned char c)
  43. {
  44.   unsigned shifter, i, flag;
  45.  
  46.   if (crc) {
  47.     for (shifter = 0x80; shifter; shifter >>= 1) {
  48.       flag = (crcaccum & 0x8000);
  49.       crcaccum <<= 1;
  50.       crcaccum |= (shifter & c) ? 1 : 0;
  51.       if (flag)
  52.         crcaccum ^= 0x1021;
  53.     }
  54.   }
  55.   else
  56.     checksum += c;
  57. }
  58.  
  59. /* ---------- read_comm ------------------------
  60.  * Does:  Reads data from the comm port already opened.
  61.  * Pass:  by ref - number of bytes to read (returns actually read).
  62.  *        pointer to buffer for data
  63.  *        time to wait (in msecs) for completion. 0 = 1 test
  64.  * Uses:  TurboC 1.5+ specific delay() function.
  65.  *        Test show that delay() works reliably at 10 msecs (not 1)
  66.  * Priority:  If all char's read, returns before time expires.
  67.  *    Any time a receive error is detected, return immediately.
  68.  * Returns: 0 if all read, TIMEOUT (define) if timeout.
  69.  */
  70. int read_comm( int *num, unsigned char *buf, int wait )
  71. {
  72.   int toread = *num;
  73.   int redok  = 0;
  74.   unsigned char *cptr = buf;
  75.   int wait10ms  = wait / 10;
  76.  
  77.   if (wait10ms == 0)
  78.     wait10ms = 1;         /* add 1 in case under 10 */
  79.  
  80.   do {
  81.     if (incomm() != 0)
  82.     {
  83.       /* Data is waiting.  Move it into buffer and get count read */
  84.       *cptr = getcomch();                /* Send to users buffer */
  85.       cptr++;
  86.       redok++;
  87.     }
  88.     else
  89.       if ( wait10ms ) {
  90.         delay(10);             /* Borland says 1 millisecond per... */
  91.         wait10ms--;
  92.       }
  93.   }
  94.   while (!( (redok == toread) || (wait10ms == 0)  ));
  95.  
  96.   *num = redok;    /* return the actual number read via *num ref param */
  97.   return ( (wait10ms > 0) ? 0 : TIMEOUT );
  98. }
  99.  
  100.  
  101. /* ---------- close_comm ------------------------
  102.  * Does:  Closes the comm port previously opened by removing our interrupt
  103.  *        handler and returning the UART to the previous (saved) state.
  104.  */
  105. int close_comm()
  106. {
  107.   Remove_IH();
  108. }
  109.  
  110. /* init ----- initialize params and installs interrupt handler.
  111.  * Pass:  Comm port number (1 or 2) to install and configure, bbsmode flag.
  112.  * Does:  Installs interrupt handler and sets comm port.
  113.  *        Sets comm parameters per struct passed.
  114.  *        Config_Comm publishes global baud rate for receiver timing.
  115.  */
  116. int init_comm( int comnum, int bbsconf )
  117. {
  118.   int retval;
  119.  
  120.   retval = Inst_IH(rxrdy_IH, comnum);
  121.   if (retval != 0) {
  122.     Remove_IH();           /* this could probably waste somebody else */
  123.     return(1);             /* was exit(1) */
  124.   }
  125.  
  126.   if (bbsconf == 0) {
  127.     cur_config.ubits.lctrl = sev1even;
  128.   }
  129.   else
  130.     cur_config.ubits.lctrl = ate1none;
  131.  
  132.   Config_Comm(comnum, cur_config);
  133.   return(0);
  134. }
  135.