ry_sy.c 6.0 KB

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