ry_sy.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. struct stat file_buf;
  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. rt_err_t 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 retry_read;
  143. read_size = 0;
  144. for (retry_read = 0; retry_read < 10; retry_read++)
  145. {
  146. read_size += read(cctx->fd, buf + read_size, len - read_size);
  147. if (read_size == len)
  148. break;
  149. }
  150. if (read_size < len)
  151. {
  152. rt_memset(buf + read_size, 0x1A, len - read_size);
  153. ctx->stage = RYM_STAGE_FINISHING;
  154. }
  155. if (read_size > 128)
  156. {
  157. return RYM_CODE_STX;
  158. }
  159. return RYM_CODE_SOH;
  160. }
  161. static enum rym_code _rym_send_end(
  162. struct rym_ctx *ctx,
  163. rt_uint8_t *buf,
  164. rt_size_t len)
  165. {
  166. rt_memset(buf, 0, len);
  167. return RYM_CODE_SOH;
  168. }
  169. static rt_err_t rym_download_file(rt_device_t idev,const char *file_path)
  170. {
  171. rt_err_t res;
  172. struct custom_ctx *ctx = rt_calloc(1, sizeof(*ctx));
  173. if (!ctx)
  174. {
  175. rt_kprintf("rt_malloc failed\n");
  176. return -RT_ENOMEM;
  177. }
  178. ctx->fd = -1;
  179. rt_strncpy(ctx->fpath, file_path, DFS_PATH_MAX);
  180. RT_ASSERT(idev);
  181. res = rym_recv_on_device(&ctx->parent, idev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  182. _rym_recv_begin, _rym_recv_data, _rym_recv_end, 1000);
  183. rt_free(ctx);
  184. return res;
  185. }
  186. static rt_err_t rym_upload_file(rt_device_t idev, const char *file_path)
  187. {
  188. rt_err_t res = 0;
  189. struct custom_ctx *ctx = rt_calloc(1, sizeof(*ctx));
  190. if (!ctx)
  191. {
  192. rt_kprintf("rt_malloc failed\n");
  193. return -RT_ENOMEM;
  194. }
  195. ctx->fd = -1;
  196. rt_strncpy(ctx->fpath, file_path, DFS_PATH_MAX);
  197. RT_ASSERT(idev);
  198. res = rym_send_on_device(&ctx->parent, idev,
  199. RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  200. _rym_send_begin, _rym_send_data, _rym_send_end, 1000);
  201. rt_free(ctx);
  202. return res;
  203. }
  204. #ifdef RT_USING_FINSH
  205. #include <finsh.h>
  206. static rt_err_t ry(uint8_t argc, char **argv)
  207. {
  208. rt_err_t res;
  209. rt_device_t dev;
  210. /* temporarily support 1 file*/
  211. const char *file_path;
  212. if (argc < 2)
  213. {
  214. rt_kprintf("invalid file path.\n");
  215. return -RT_ERROR;
  216. }
  217. if (argc > 2)
  218. dev = rt_device_find(argv[2]);
  219. else
  220. dev = rt_console_get_device();
  221. if (!dev)
  222. {
  223. rt_kprintf("could not find device.\n");
  224. return -RT_ERROR;
  225. }
  226. file_path = argv[1];
  227. res = rym_download_file(dev,file_path);
  228. return res;
  229. }
  230. MSH_CMD_EXPORT(ry, YMODEM Receive e.g: ry file_path [uart0] default by console.);
  231. static rt_err_t sy(uint8_t argc, char **argv)
  232. {
  233. rt_err_t res;
  234. /* temporarily support 1 file*/
  235. const char *file_path;
  236. rt_device_t dev;
  237. if (argc < 2)
  238. {
  239. rt_kprintf("invalid file path.\n");
  240. return -RT_ERROR;
  241. }
  242. if (argc > 2)
  243. dev = rt_device_find(argv[2]);
  244. else
  245. dev = rt_console_get_device();
  246. if (!dev)
  247. {
  248. rt_kprintf("could not find device.\n");
  249. return -RT_ERROR;
  250. }
  251. file_path = argv[1];
  252. res = rym_upload_file(dev, file_path);
  253. return res;
  254. }
  255. MSH_CMD_EXPORT(sy, YMODEM Send e.g: sy file_path [uart0] default by console.);
  256. #endif /* RT_USING_FINSH */