ymodem.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * COPYRIGHT (C) 2011-2022, Real-Thread Information Technology Ltd
  3. * All rights reserved
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2013-04-14 Grissiom initial implementation
  10. * 2019-12-09 Steven Liu add YMODEM send protocol
  11. * 2022-08-04 Meco Man move error codes to rym_code to silence warnings
  12. */
  13. #ifndef __YMODEM_H__
  14. #define __YMODEM_H__
  15. #include "rtthread.h"
  16. #include <string.h>
  17. /* The word "RYM" is stand for "Real-YModem". */
  18. enum rym_code
  19. {
  20. RYM_CODE_NONE = 0x00,
  21. RYM_CODE_SOH = 0x01,
  22. RYM_CODE_STX = 0x02,
  23. RYM_CODE_EOT = 0x04,
  24. RYM_CODE_ACK = 0x06,
  25. RYM_CODE_NAK = 0x15,
  26. RYM_CODE_CAN = 0x18,
  27. RYM_CODE_C = 0x43,
  28. /* RYM error code */
  29. RYM_ERR_TMO = 0x70, /* timeout on handshake */
  30. RYM_ERR_CODE = 0x71, /* wrong code, wrong SOH, STX etc */
  31. RYM_ERR_SEQ = 0x72, /* wrong sequence number */
  32. RYM_ERR_CRC = 0x73, /* wrong CRC checksum */
  33. RYM_ERR_DSZ = 0x74, /* not enough data received */
  34. RYM_ERR_CAN = 0x75, /* the transmission is aborted by user */
  35. RYM_ERR_ACK = 0x76, /* wrong answer, wrong ACK or C */
  36. RYM_ERR_FILE = 0x77, /* transmit file invalid */
  37. };
  38. /* how many ticks wait for chars between packet. */
  39. #ifndef RYM_WAIT_CHR_TICK
  40. #define RYM_WAIT_CHR_TICK (RT_TICK_PER_SECOND * 3)
  41. #endif
  42. /* how many ticks wait for between packet. */
  43. #ifndef RYM_WAIT_PKG_TICK
  44. #define RYM_WAIT_PKG_TICK (RT_TICK_PER_SECOND * 3)
  45. #endif
  46. /* how many ticks between two handshake code. */
  47. #ifndef RYM_CHD_INTV_TICK
  48. #define RYM_CHD_INTV_TICK (RT_TICK_PER_SECOND * 3)
  49. #endif
  50. /* how many CAN be sent when user active end the session. */
  51. #ifndef RYM_END_SESSION_SEND_CAN_NUM
  52. #define RYM_END_SESSION_SEND_CAN_NUM 0x07
  53. #endif
  54. /* how many retries were made when the error occurred */
  55. #ifndef RYM_MAX_ERRORS
  56. #define RYM_MAX_ERRORS ((rt_size_t)5)
  57. #endif
  58. enum rym_stage
  59. {
  60. RYM_STAGE_NONE = 0,
  61. /* set when C is send */
  62. RYM_STAGE_ESTABLISHING,
  63. /* set when we've got the packet 0 and sent ACK and second C */
  64. RYM_STAGE_ESTABLISHED,
  65. /* set when the sender respond to our second C and recviever got a real
  66. * data packet. */
  67. RYM_STAGE_TRANSMITTING,
  68. /* set when the sender send a EOT */
  69. RYM_STAGE_FINISHING,
  70. /* set when transmission is really finished, i.e., after the NAK, C, final
  71. * NULL packet stuff. */
  72. RYM_STAGE_FINISHED,
  73. };
  74. struct rym_ctx;
  75. /* When receiving files, the buf will be the data received from ymodem protocol
  76. * and the len is the data size.
  77. *
  78. * When sending files, the len is the buf size in RYM. The callback need to
  79. * fill the buf with data to send. Returning RYM_CODE_EOT will terminate the
  80. * transfer and the buf will be discarded. Any other return values will cause
  81. * the transfer continue.
  82. */
  83. typedef enum rym_code(*rym_callback)(struct rym_ctx *ctx, rt_uint8_t *buf, rt_size_t len);
  84. /* Currently RYM only support one transfer session(ctx) for simplicity.
  85. *
  86. * In case we could support multiple sessions in The future, the first argument
  87. * of APIs are (struct rym_ctx*).
  88. */
  89. struct rym_ctx
  90. {
  91. rym_callback on_data;
  92. rym_callback on_begin;
  93. rym_callback on_end;
  94. /* When error happened, user need to check this to get when the error has
  95. * happened. */
  96. enum rym_stage stage;
  97. /* user could get the error content through this */
  98. rt_uint8_t *buf;
  99. struct rt_semaphore sem;
  100. rt_device_t dev;
  101. };
  102. /* recv a file on device dev with ymodem session ctx.
  103. *
  104. * If an error happens, you can get where it is failed from ctx->stage.
  105. *
  106. * @param on_begin The callback will be invoked when the first packet arrived.
  107. * This packet often contain file names and the size of the file, if the sender
  108. * support it. So if you want to save the data to a file, you may need to
  109. * create the file on need. It is the on_begin's responsibility to parse the
  110. * data content. The on_begin can be NULL, in which case the transmission will
  111. * continue without any side-effects.
  112. *
  113. * @param on_data The callback will be invoked on the packets received. The
  114. * callback should save the data to the destination. The return value will be
  115. * sent to the sender and in turn, only RYM_{ACK,CAN} is valid. When on_data is
  116. * NULL, RYM will barely send ACK on every packet and have no side-effects.
  117. *
  118. * @param on_end The callback will be invoked when one transmission is
  119. * finished. The data should be 128 bytes of NULL. You can do some cleaning job
  120. * in this callback such as closing the file. The return value of this callback
  121. * is ignored. As above, this parameter can be NULL if you don't need such
  122. * function.
  123. *
  124. * @param handshake_timeout the timeout when hand shaking. The unit is in
  125. * second.
  126. */
  127. rt_err_t rym_recv_on_device(struct rym_ctx *ctx, rt_device_t dev, rt_uint16_t oflag,
  128. rym_callback on_begin, rym_callback on_data, rym_callback on_end,
  129. int handshake_timeout);
  130. /* send a file on device dev with ymodem session ctx.
  131. *
  132. * If an error happens, you can get where it is failed from ctx->stage.
  133. *
  134. * @param on_begin The callback will be invoked when the first packet is sent.
  135. * This packet often contain file names and the size of the file. It is the
  136. * on_begin's responsibility to parse the basic information of the file. The
  137. * on_begin can not be NULL.
  138. *
  139. * @param on_data The callback will be invoked when the data packets is sent.
  140. * The callback should read file system and prepare the data packets. The
  141. * on_data can not be NULL.
  142. *
  143. * @param on_end The callback will be invoked when one transmission is
  144. * finished. The data should be 128 bytes of NULL. The on_end can not be NULL.
  145. *
  146. * @param handshake_timeout the timeout when hand shaking. The unit is in
  147. * second.
  148. */
  149. rt_err_t rym_send_on_device(struct rym_ctx *ctx, rt_device_t dev, rt_uint16_t oflag,
  150. rym_callback on_begin, rym_callback on_data, rym_callback on_end,
  151. int handshake_timeout);
  152. #endif