1
0

ry_sy.c 5.4 KB

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