ry_sy.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. * 2026-02-01 wdfk-prog update ymodem transfer behaviors
  11. */
  12. #include <rtthread.h>
  13. #include <ymodem.h>
  14. #include <dfs_file.h>
  15. #include <unistd.h>
  16. #include <sys/stat.h>
  17. #include <sys/statfs.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #ifndef DFS_USING_POSIX
  21. #error "Please enable DFS_USING_POSIX"
  22. #endif
  23. struct custom_ctx
  24. {
  25. struct rym_ctx parent;
  26. int fd;
  27. int flen;
  28. char fpath[DFS_PATH_MAX];
  29. };
  30. static const char *_get_path_lastname(const char *path)
  31. {
  32. char *ptr;
  33. if ((ptr = (char *)strrchr(path, '/')) == NULL)
  34. return path;
  35. /* skip the '/' then return */
  36. return ++ptr;
  37. }
  38. static enum rym_code _rym_recv_begin(
  39. struct rym_ctx *ctx,
  40. rt_uint8_t *buf,
  41. rt_size_t len)
  42. {
  43. struct custom_ctx *cctx = (struct custom_ctx *)ctx;
  44. rt_err_t err;
  45. /* support recv multiple files in one session */
  46. char user_path[DFS_PATH_MAX]={0};
  47. rt_snprintf(user_path, DFS_PATH_MAX, "%s%s", cctx->fpath, buf);
  48. cctx->fd = open(user_path, O_CREAT | O_WRONLY | O_TRUNC, 0);
  49. if (cctx->fd < 0)
  50. {
  51. err = rt_get_errno();
  52. rt_kprintf("error creating file: %ld\n", err);
  53. return RYM_CODE_CAN;
  54. }
  55. cctx->flen = atoi(1 + (const char *)buf + rt_strnlen((const char *)buf, len - 1));
  56. if (cctx->flen == 0)
  57. cctx->flen = -1;
  58. return RYM_CODE_ACK;
  59. }
  60. static enum rym_code _rym_recv_data(
  61. struct rym_ctx *ctx,
  62. rt_uint8_t *buf,
  63. rt_size_t len)
  64. {
  65. struct custom_ctx *cctx = (struct custom_ctx *)ctx;
  66. RT_ASSERT(cctx->fd >= 0);
  67. if (cctx->flen == -1)
  68. {
  69. write(cctx->fd, buf, len);
  70. }
  71. else
  72. {
  73. int wlen = len > cctx->flen ? cctx->flen : len;
  74. write(cctx->fd, buf, wlen);
  75. cctx->flen -= wlen;
  76. }
  77. return RYM_CODE_ACK;
  78. }
  79. static enum rym_code _rym_recv_end(
  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. RT_ASSERT(cctx->fd >= 0);
  86. close(cctx->fd);
  87. cctx->fd = -1;
  88. return RYM_CODE_ACK;
  89. }
  90. static enum rym_code _rym_send_begin(
  91. struct rym_ctx *ctx,
  92. rt_uint8_t *buf,
  93. rt_size_t len)
  94. {
  95. struct custom_ctx *cctx = (struct custom_ctx *)ctx;
  96. struct stat file_buf;
  97. char insert_0 = '\0';
  98. rt_err_t err;
  99. cctx->fd = open(cctx->fpath, O_RDONLY);
  100. if (cctx->fd < 0)
  101. {
  102. err = rt_get_errno();
  103. rt_kprintf("error open file: %ld\n", err);
  104. return RYM_ERR_FILE;
  105. }
  106. rt_memset(buf, 0, len);
  107. err = stat(cctx->fpath, &file_buf);
  108. if (err != RT_EOK)
  109. {
  110. rt_kprintf("error open file.\n");
  111. return RYM_ERR_FILE;
  112. }
  113. const char *fdst = _get_path_lastname(cctx->fpath);
  114. if(fdst != cctx->fpath)
  115. {
  116. fdst = dfs_normalize_path(RT_NULL, fdst);
  117. if (fdst == RT_NULL)
  118. {
  119. return RYM_ERR_FILE;
  120. }
  121. }
  122. rt_sprintf((char *)buf, "%s%c%ld", fdst, insert_0, file_buf.st_size);
  123. return RYM_CODE_SOH;
  124. }
  125. static enum rym_code _rym_send_data(
  126. struct rym_ctx *ctx,
  127. rt_uint8_t *buf,
  128. rt_size_t len)
  129. {
  130. struct custom_ctx *cctx = (struct custom_ctx *)ctx;
  131. rt_size_t read_size;
  132. int rlen;
  133. read_size = 0;
  134. /* Loop until we fill one YMODEM data block, hit EOF, or get a read error. */
  135. while (read_size < len)
  136. {
  137. rlen = read(cctx->fd, buf + read_size, len - read_size);
  138. if (rlen > 0)
  139. {
  140. read_size += rlen;
  141. if (read_size == len)
  142. break;
  143. }
  144. else if (rlen == 0)
  145. {
  146. /* EOF: mark finishing so sender switches to EOT after padding. */
  147. ctx->stage = RYM_STAGE_FINISHING;
  148. break;
  149. }
  150. else
  151. {
  152. /* Read error: abort transfer and report file error to the protocol. */
  153. return RYM_ERR_FILE;
  154. }
  155. }
  156. if (read_size < len)
  157. {
  158. rt_memset(buf + read_size, 0x1A, len - read_size);
  159. }
  160. if (read_size > 128)
  161. {
  162. return RYM_CODE_STX;
  163. }
  164. return RYM_CODE_SOH;
  165. }
  166. static enum rym_code _rym_send_end(
  167. struct rym_ctx *ctx,
  168. rt_uint8_t *buf,
  169. rt_size_t len)
  170. {
  171. struct custom_ctx *cctx = (struct custom_ctx *)ctx;
  172. rt_memset(buf, 0, len);
  173. close(cctx->fd);
  174. cctx->fd = -1;
  175. return RYM_CODE_SOH;
  176. }
  177. static rt_err_t rym_download_file(rt_device_t idev,const char *file_path)
  178. {
  179. rt_err_t res;
  180. struct custom_ctx *ctx = rt_calloc(1, sizeof(*ctx));
  181. if (!ctx)
  182. {
  183. rt_kprintf("rt_malloc failed\n");
  184. return -RT_ENOMEM;
  185. }
  186. ctx->fd = -1;
  187. rt_strncpy(ctx->fpath, file_path, DFS_PATH_MAX);
  188. RT_ASSERT(idev);
  189. res = rym_recv_on_device(&ctx->parent, idev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  190. _rym_recv_begin, _rym_recv_data, _rym_recv_end, 1000);
  191. rt_free(ctx);
  192. return res;
  193. }
  194. static rt_err_t rym_upload_file(rt_device_t idev, const char *file_path)
  195. {
  196. rt_err_t res = 0;
  197. struct custom_ctx *ctx = rt_calloc(1, sizeof(*ctx));
  198. if (!ctx)
  199. {
  200. rt_kprintf("rt_malloc failed\n");
  201. return -RT_ENOMEM;
  202. }
  203. ctx->fd = -1;
  204. rt_strncpy(ctx->fpath, file_path, DFS_PATH_MAX);
  205. RT_ASSERT(idev);
  206. res = rym_send_on_device(&ctx->parent, idev,
  207. RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  208. _rym_send_begin, _rym_send_data, _rym_send_end, 1000);
  209. rt_free(ctx);
  210. return res;
  211. }
  212. #ifdef RT_USING_FINSH
  213. #include <finsh.h>
  214. static rt_err_t ry(uint8_t argc, char **argv)
  215. {
  216. rt_err_t res;
  217. rt_device_t dev;
  218. const char *file_path;
  219. if (argc < 2)
  220. {
  221. rt_kprintf("invalid file path.\n");
  222. return -RT_ERROR;
  223. }
  224. if (argc > 2)
  225. dev = rt_device_find(argv[2]);
  226. else
  227. dev = rt_console_get_device();
  228. if (!dev)
  229. {
  230. rt_kprintf("could not find device.\n");
  231. return -RT_ERROR;
  232. }
  233. file_path = argv[1];
  234. res = rym_download_file(dev,file_path);
  235. return res;
  236. }
  237. MSH_CMD_EXPORT(ry, YMODEM Receive e.g: ry file_path [uart0] default by console.);
  238. static rt_err_t sy(uint8_t argc, char **argv)
  239. {
  240. rt_err_t res;
  241. /* temporarily support 1 file*/
  242. const char *file_path;
  243. rt_device_t dev;
  244. if (argc < 2)
  245. {
  246. rt_kprintf("invalid file path.\n");
  247. return -RT_ERROR;
  248. }
  249. if (argc > 2)
  250. dev = rt_device_find(argv[2]);
  251. else
  252. dev = rt_console_get_device();
  253. if (!dev)
  254. {
  255. rt_kprintf("could not find device.\n");
  256. return -RT_ERROR;
  257. }
  258. file_path = argv[1];
  259. res = rym_upload_file(dev, file_path);
  260. return res;
  261. }
  262. MSH_CMD_EXPORT(sy, YMODEM Send e.g: sy file_path [uart0] default by console.);
  263. #endif /* RT_USING_FINSH */