home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 292.lha / TinyTerm / termxfr.c < prev   
C/C++ Source or Header  |  1992-09-02  |  7KB  |  316 lines

  1. #include "term.h"
  2.  
  3. UBYTE  SOH = 1;
  4. UBYTE  EOT = 4;
  5. UBYTE  ACK = 6;
  6. UBYTE  DLE = 16;
  7. UBYTE  NAK = 21;
  8. UBYTE  CAN = 24; 
  9. UBYTE  ProtoInit = 'W';
  10. UBYTE  DataBuffer[2048];
  11. USHORT iDataBuffer;
  12. USHORT Crc16, Timeout, TimeoutRetry, Abort;
  13.  
  14. USHORT CrcTable[256] =
  15. {
  16. 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
  17. 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
  18. 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,
  19. 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
  20. 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,
  21. 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
  22. 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,
  23. 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
  24. 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,
  25. 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
  26. 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
  27. 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
  28. 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,
  29. 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
  30. 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,
  31. 0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
  32. 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,
  33. 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
  34. 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,
  35. 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
  36. 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,
  37. 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
  38. 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,
  39. 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
  40. 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,
  41. 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
  42. 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,
  43. 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
  44. 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,
  45. 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
  46. 0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,
  47. 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0,
  48. };
  49.  
  50. #define DoCrc(x) (Crc16 = CrcTable[x ^ (Crc16 >> 8)] ^ (Crc16 << 8))
  51.  
  52. UBYTE ReadProtocol(void)
  53. {
  54.     ULONG signal;
  55.     UBYTE data;
  56.     
  57.     data = NULL;
  58.         TimerIO.tr_node.io_Command = TR_ADDREQUEST;
  59.         TimerIO.tr_node.io_Flags   = 0; /*IOF_QUICK;*/
  60.         TimerIO.tr_time.tv_secs    = 10;
  61.         TimerIO.tr_time.tv_micro   = 0;
  62.         SendIO((struct IORequest *)&TimerIO);
  63.     Timeout = FALSE;
  64.     signal = Wait(TimeSignal|ScreenSignal|ReadSignal);
  65.     if (signal & TimeSignal)
  66.     {
  67.         Prints("Timeout.....\n");
  68.         TimeoutRetry--;
  69.         Timeout = TRUE;
  70.     }
  71.     if (signal & ReadSignal)
  72.     {
  73.         AbortIO((struct IORequest *)&TimerIO);
  74.         Wait(TimeSignal);
  75.         WaitIO((struct IORequest *)ReadReq);
  76.         data = ReadData[0];
  77.         SendIO((struct IORequest *)ReadReq);
  78.     }
  79.     if (signal & ScreenSignal)
  80.     {
  81.         Prints("User Abort!!!!\n");
  82.         AbortIO((struct IORequest *)&TimerIO);
  83.         Wait(TimeSignal);
  84.         Abort = TRUE;
  85.     }
  86.     if ((ProtoInit == 'W') && (data == DLE))
  87.         data = ReadProtocol() ^ 0x40;
  88.     return (data);
  89. }
  90.  
  91. void DownLoad(void)
  92. {
  93.     FILE *File;
  94.     char *FileName;
  95.     USHORT Realblk;
  96.     UBYTE c, blknum, blkack;
  97.     UBYTE DispBuffer[80];
  98.     USHORT State, EOTcnt, EOTrequired;
  99.     #define BLKNUM 0
  100.     #define INVBLK 1
  101.     #define TEXT   2
  102.     #define CRC1   3
  103.     #define CRC2   4
  104.     #define SYNCUP 5
  105.     #define MAXFILES 10
  106.  
  107.     if (!GetFile(w, &FileName))
  108.     {
  109.         Prints("Download Cancelled\n");
  110.         return;
  111.     }
  112.     File = fopen(FileName, "wb");
  113.     if (File == NULL)
  114.     {
  115.         Prints("Cannot open ");
  116.         Prints(FileName);
  117.         Prints("\n");
  118.         return;
  119.     }
  120.     Prints("Output file = ");
  121.     Prints(FileName);
  122.     Prints("\n");
  123.     
  124.     iDataBuffer = 0;
  125.     Prints("Starting Download\n");
  126.     TimeoutRetry = 2;
  127.     while (TimeoutRetry || (ProtoInit != NAK))
  128.     {
  129.         WriteSerial(&ProtoInit, 1);
  130.         c = ReadProtocol();
  131.         if (c == SOH)
  132.             break;
  133.         if (TimeoutRetry == 0)
  134.         {
  135.             if (ProtoInit == 'W')
  136.             {
  137.                 Prints("Switching to CRC\n");
  138.                 ProtoInit = 'C';
  139.                 TimeoutRetry = 2;
  140.             }
  141.             else if (ProtoInit == 'C')
  142.             {
  143.                 Prints("Switching to Checksum\n");
  144.                 ProtoInit = NAK;
  145.                 TimeoutRetry = 2;
  146.             }
  147.         }
  148.     }
  149.     blknum = 
  150.     Realblk = 1;
  151.     TimeoutRetry = 4;
  152.     Abort = FALSE;
  153.     State = BLKNUM;
  154.     if (ProtoInit == 'W')
  155.         EOTrequired = 2;
  156.     else
  157.         EOTrequired = 1;
  158.     EOTcnt = 0;
  159.     if (c != SOH)
  160.         State = SYNCUP;
  161.     
  162.     for (;;)
  163.     {
  164.         c = ReadProtocol();
  165.         if (TimeoutRetry == 0)
  166.             goto finish;
  167.         if (Timeout)
  168.         {
  169.             WriteSerial(&NAK, 1);
  170.             blkack = blknum & 3;
  171.             if (ProtoInit == 'W')
  172.                 WriteSerial(&blkack, 1);
  173.             State = SYNCUP;
  174.             continue;
  175.         }
  176.         else    
  177.             TimeoutRetry = 4;
  178.             
  179.         switch (State)
  180.         {
  181.             case BLKNUM:
  182.             if (c == blknum)
  183.                 State = INVBLK;
  184.             else
  185.             {
  186.                 Prints("Block Num Error\n");
  187.                 State = SYNCUP;
  188.             }
  189.             break;
  190.             
  191.             case INVBLK:
  192.             Crc16 = 0;
  193.             if (c == ~blknum)
  194.                 State = TEXT;
  195.             else
  196.             {
  197.                 Prints("Inverse Block Num Error\n");
  198.                 State = SYNCUP;
  199.             }
  200.             break;
  201.             
  202.             case TEXT:
  203.             DataBuffer[iDataBuffer++] = c;
  204.             if (ProtoInit == NAK)
  205.                 Crc16 = (Crc16 + c) & 0xff;
  206.             else
  207.                 DoCrc(c);
  208.             if ((iDataBuffer % 128) == 0)
  209.             {
  210.                 if (ProtoInit == NAK)
  211.                     State = CRC2;
  212.                 else
  213.                     State = CRC1;
  214.             }
  215.             break;
  216.             
  217.             case CRC1:
  218.             if (ProtoInit == NAK)
  219.                 Crc16 = (Crc16 + c) & 0xff;
  220.             else
  221.                 DoCrc(c);
  222.             State = CRC2;
  223.             break;
  224.             
  225.             case CRC2:
  226.             if (ProtoInit == NAK)
  227.                 Crc16 -= c;
  228.             else
  229.                 DoCrc(c);
  230.             if (Abort)
  231.             {
  232.                 c = NULL;
  233.                 goto finish;
  234.             }
  235.             if (Crc16 == 0)
  236.             {
  237.                 WriteSerial(&ACK, 1);
  238.                 if (ProtoInit == 'W')
  239.                 {
  240.                     blkack = blknum & 3;
  241.                     WriteSerial(&blkack, 1);
  242.                 }
  243.                 sprintf( DispBuffer
  244.                        , "\rBlock %d"
  245.                        , Realblk);
  246.                 Prints(DispBuffer);
  247.                 blknum++;
  248.                 Realblk++;
  249.             }
  250.             else
  251.             {
  252.                 WriteSerial(&NAK, 1);
  253.                 if (ProtoInit == 'W')
  254.                 {
  255.                     blkack = blknum & 3;
  256.                     WriteSerial(&blkack, 1);
  257.                 }
  258.                 sprintf( DispBuffer
  259.                        , "\nError Block %d"
  260.                        , Realblk);
  261.                 Prints(DispBuffer);
  262.             }
  263.             State = SYNCUP;
  264.             break;
  265.             
  266.             case SYNCUP:
  267.             if (Abort)
  268.                 goto finish;
  269.             if (c == EOT)
  270.             {
  271.                 WriteSerial(&ACK, 1);
  272.                 if (++EOTcnt == EOTrequired)
  273.                     goto finish;
  274.                 break;
  275.             }
  276.             EOTcnt = 0;
  277.             if (c == SOH)
  278.             {
  279.                 State = BLKNUM;
  280.                 if (iDataBuffer >= sizeof(DataBuffer))
  281.                 {
  282.                     fwrite( DataBuffer
  283.                           , 1
  284.                           , iDataBuffer
  285.                           , File);
  286.                     iDataBuffer = 0;
  287.                 }
  288.             }
  289.             if (c == CAN)
  290.                 goto finish;
  291.             break;
  292.         }
  293.     }
  294.     
  295. finish:
  296.     if (c == EOT)
  297.         Prints("\nDownload Complete\n");
  298.     else
  299.     {
  300.         Prints("\nAborting\n");
  301.         WriteSerial(&CAN, 1);
  302.         WriteSerial(&CAN, 1);
  303.     }
  304.     if (iDataBuffer)
  305.         fwrite(DataBuffer, 1, iDataBuffer, File);
  306.     fclose(File);
  307. }
  308.  
  309. void UpLoad(void)
  310. {
  311.     ;
  312. }
  313.  
  314.  
  315.  
  316.