xmodem.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* XMODEM receiver: 128-byte packets, 1-byte checksum. */
  2. #include "serial.h"
  3. #include "timer.h"
  4. #include "xmodem.h"
  5. #define SOH 0x01
  6. #define EOT 0x04
  7. #define ACK 0x06
  8. #define NAK 0x15
  9. #define CAN 0x18
  10. #define PACKET_HEADER 2 /* block number, 255-block (SOH already consumed) */
  11. #define PACKET_PAYLOAD 128
  12. #define PACKET_CSUM 1
  13. #define NAK_INTERVAL_MS 1000
  14. #define RECV_TIMEOUT_MS 3000
  15. static int get_byte(unsigned int timeout_ms)
  16. {
  17. return serial_getchar(timeout_ms);
  18. }
  19. static void send_byte(unsigned char b)
  20. {
  21. serial_putchar((char)b);
  22. }
  23. int xmodem_receive(void *buf, unsigned int max_len)
  24. {
  25. unsigned char *dst = (unsigned char *)buf;
  26. unsigned int total = 0;
  27. unsigned char block_num = 1;
  28. unsigned int last_nak = 0;
  29. /* Send NAK repeatedly until we get SOH (start of first packet) or EOT (empty file). */
  30. for (;;) {
  31. unsigned int now = get_ticks_ms();
  32. if (now - last_nak >= NAK_INTERVAL_MS) {
  33. send_byte(NAK);
  34. last_nak = now;
  35. }
  36. {
  37. int c = get_byte(100);
  38. if (c < 0)
  39. continue;
  40. if (c == EOT) {
  41. send_byte(ACK);
  42. return 0;
  43. }
  44. if (c == SOH)
  45. break;
  46. if (c == CAN)
  47. return -1;
  48. }
  49. }
  50. for (;;) {
  51. unsigned char header[PACKET_HEADER];
  52. unsigned char payload[PACKET_PAYLOAD];
  53. unsigned char csum;
  54. unsigned int i;
  55. unsigned char sum = 0;
  56. for (i = 0; i < PACKET_HEADER; i++) {
  57. int c = get_byte(RECV_TIMEOUT_MS);
  58. if (c < 0)
  59. return -1;
  60. header[i] = (unsigned char)c;
  61. }
  62. for (i = 0; i < PACKET_PAYLOAD; i++) {
  63. int c = get_byte(RECV_TIMEOUT_MS);
  64. if (c < 0)
  65. return -1;
  66. payload[i] = (unsigned char)c;
  67. sum += payload[i];
  68. }
  69. {
  70. int c = get_byte(RECV_TIMEOUT_MS);
  71. if (c < 0)
  72. return -1;
  73. csum = (unsigned char)c;
  74. }
  75. if (header[0] != block_num || header[1] != (unsigned char)(255 - block_num)) {
  76. send_byte(NAK);
  77. continue;
  78. }
  79. if (csum != sum) {
  80. send_byte(NAK);
  81. continue;
  82. }
  83. /* Copy payload; respect max_len. */
  84. for (i = 0; i < PACKET_PAYLOAD && total < max_len; i++, total++)
  85. dst[total] = payload[i];
  86. send_byte(ACK);
  87. block_num++;
  88. /* Next: expect another SOH or EOT. */
  89. {
  90. int c = get_byte(RECV_TIMEOUT_MS);
  91. if (c < 0)
  92. return -1;
  93. if (c == EOT) {
  94. send_byte(ACK);
  95. return (int)total;
  96. }
  97. if (c == CAN)
  98. return -1;
  99. if (c != SOH) {
  100. /* Unexpected; treat as abort. */
  101. return -1;
  102. }
  103. }
  104. }
  105. }