ry_sy.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-12-09 Steven Liu the first version
  9. */
  10. #include <rtthread.h>
  11. #include <ymodem.h>
  12. #include <dfs_posix.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. struct custom_ctx
  17. {
  18. struct rym_ctx parent;
  19. int fd;
  20. int flen;
  21. char fpath[DFS_PATH_MAX];
  22. };
  23. static enum rym_code _rym_recv_begin(
  24. struct rym_ctx *ctx,
  25. rt_uint8_t *buf,
  26. rt_size_t len)
  27. {
  28. struct custom_ctx *cctx = (struct custom_ctx *)ctx;
  29. cctx->fpath[0] = '/';
  30. rt_strncpy(&(cctx->fpath[1]), (const char *)buf, len - 1);
  31. cctx->fd = open(cctx->fpath, O_CREAT | O_WRONLY | O_TRUNC, 0);
  32. if (cctx->fd < 0)
  33. {
  34. rt_err_t err = rt_get_errno();
  35. rt_kprintf("error creating file: %d\n", err);
  36. return RYM_CODE_CAN;
  37. }
  38. cctx->flen = atoi(1 + (const char *)buf + rt_strnlen((const char *)buf, len - 1));
  39. if (cctx->flen == 0)
  40. cctx->flen = -1;
  41. return RYM_CODE_ACK;
  42. }
  43. static enum rym_code _rym_recv_data(
  44. struct rym_ctx *ctx,
  45. rt_uint8_t *buf,
  46. rt_size_t len)
  47. {
  48. struct custom_ctx *cctx = (struct custom_ctx *)ctx;
  49. RT_ASSERT(cctx->fd >= 0);
  50. if (cctx->flen == -1)
  51. {
  52. write(cctx->fd, buf, len);
  53. }
  54. else
  55. {
  56. int wlen = len > cctx->flen ? cctx->flen : len;
  57. write(cctx->fd, buf, wlen);
  58. cctx->flen -= wlen;
  59. }
  60. return RYM_CODE_ACK;
  61. }
  62. static enum rym_code _rym_recv_end(
  63. struct rym_ctx *ctx,
  64. rt_uint8_t *buf,
  65. rt_size_t len)
  66. {
  67. struct custom_ctx *cctx = (struct custom_ctx *)ctx;
  68. RT_ASSERT(cctx->fd >= 0);
  69. close(cctx->fd);
  70. cctx->fd = -1;
  71. return RYM_CODE_ACK;
  72. }
  73. static enum rym_code _rym_send_begin(
  74. struct rym_ctx *ctx,
  75. rt_uint8_t *buf,
  76. rt_size_t len)
  77. {
  78. struct custom_ctx *cctx = (struct custom_ctx *)ctx;
  79. struct stat file_buf;
  80. char insert_0 = '\0';
  81. rt_err_t err;
  82. cctx->fd = open(cctx->fpath, O_RDONLY);
  83. if (cctx->fd < 0)
  84. {
  85. err = rt_get_errno();
  86. rt_kprintf("error open file: %d\n", err);
  87. return RYM_ERR_FILE;
  88. }
  89. rt_memset(buf, 0, len);
  90. err = stat(cctx->fpath, &file_buf);
  91. if (err != RT_EOK)
  92. {
  93. rt_kprintf("error open file.\n");
  94. return RYM_ERR_FILE;
  95. }
  96. sprintf((char *)buf, "%s%c%ld", (char *) & (cctx->fpath[1]), insert_0, file_buf.st_size);
  97. return RYM_CODE_SOH;
  98. }
  99. static enum rym_code _rym_send_data(
  100. struct rym_ctx *ctx,
  101. rt_uint8_t *buf,
  102. rt_size_t len)
  103. {
  104. struct custom_ctx *cctx = (struct custom_ctx *)ctx;
  105. rt_size_t read_size;
  106. int retry_read;
  107. read_size = 0;
  108. for (retry_read = 0; retry_read < 10; retry_read++)
  109. {
  110. read_size += read(cctx->fd, buf + read_size, len - read_size);
  111. if (read_size == len)
  112. break;
  113. }
  114. if (read_size < len)
  115. {
  116. rt_memset(buf + read_size, 0x1A, len - read_size);
  117. /* stage = RYM_STAGE_FINISHING */
  118. ctx->stage = 4;
  119. }
  120. return RYM_CODE_SOH;
  121. }
  122. static enum rym_code _rym_send_end(
  123. struct rym_ctx *ctx,
  124. rt_uint8_t *buf,
  125. rt_size_t len)
  126. {
  127. rt_memset(buf, 0, len);
  128. return RYM_CODE_SOH;
  129. }
  130. static rt_err_t rym_download_file(rt_device_t idev)
  131. {
  132. rt_err_t res;
  133. struct custom_ctx *ctx = rt_calloc(1, sizeof(*ctx));
  134. if (!ctx)
  135. {
  136. rt_kprintf("rt_malloc failed\n");
  137. return RT_ENOMEM;
  138. }
  139. ctx->fd = -1;
  140. RT_ASSERT(idev);
  141. res = rym_recv_on_device(&ctx->parent, idev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  142. _rym_recv_begin, _rym_recv_data, _rym_recv_end, 1000);
  143. rt_free(ctx);
  144. return res;
  145. }
  146. static rt_err_t rym_upload_file(rt_device_t idev, const char *file_path)
  147. {
  148. rt_err_t res = 0;
  149. struct custom_ctx *ctx = rt_calloc(1, sizeof(*ctx));
  150. if (!ctx)
  151. {
  152. rt_kprintf("rt_malloc failed\n");
  153. return RT_ENOMEM;
  154. }
  155. ctx->fd = -1;
  156. rt_strncpy(ctx->fpath, file_path, DFS_PATH_MAX);
  157. RT_ASSERT(idev);
  158. res = rym_send_on_device(&ctx->parent, idev,
  159. RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  160. _rym_send_begin, _rym_send_data, _rym_send_end, 1000);
  161. rt_free(ctx);
  162. return res;
  163. }
  164. #ifdef RT_USING_FINSH
  165. #include <finsh.h>
  166. static rt_err_t ry(uint8_t argc, char **argv)
  167. {
  168. rt_err_t res;
  169. rt_device_t dev;
  170. if (argc > 1)
  171. dev = rt_device_find(argv[1]);
  172. else
  173. dev = rt_console_get_device();
  174. if (!dev)
  175. {
  176. rt_kprintf("could not find device.\n");
  177. return -RT_ERROR;
  178. }
  179. res = rym_download_file(dev);
  180. return res;
  181. }
  182. MSH_CMD_EXPORT(ry, YMODEM Receive e.g: ry [uart0] default by console.);
  183. static rt_err_t sy(uint8_t argc, char **argv)
  184. {
  185. rt_err_t res;
  186. /* temporarily support 1 file*/
  187. const char *file_path;
  188. rt_device_t dev;
  189. if (argc > 2)
  190. dev = rt_device_find(argv[2]);
  191. else
  192. dev = rt_console_get_device();
  193. if (!dev)
  194. {
  195. rt_kprintf("could not find device.\n");
  196. return -RT_ERROR;
  197. }
  198. file_path = argv[1];
  199. res = rym_upload_file(dev, file_path);
  200. return res;
  201. }
  202. MSH_CMD_EXPORT(sy, YMODEM Send e.g: sy file_path [uart0] default by console.);
  203. #endif /* RT_USING_FINSH */