ry_sy.c 5.5 KB

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