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