ry_sy.c 6.5 KB

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