ry_sy.c 7.0 KB

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