ry_sy.c 5.5 KB

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