tofile.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <rtthread.h>
  10. #include <ymodem.h>
  11. #include <dfs_posix.h>
  12. #include <stdlib.h>
  13. #include <board.h>
  14. struct custom_ctx
  15. {
  16. struct rym_ctx parent;
  17. int fd;
  18. int flen;
  19. char fpath[256];
  20. };
  21. static enum rym_code _rym_bg(
  22. struct rym_ctx *ctx,
  23. rt_uint8_t *buf,
  24. rt_size_t len)
  25. {
  26. struct custom_ctx *cctx = (struct custom_ctx*)ctx;
  27. cctx->fpath[0] = '/';
  28. /* the buf should be the file name */
  29. strcpy(&(cctx->fpath[1]), (const char*)buf);
  30. cctx->fd = open(cctx->fpath, O_CREAT | O_WRONLY | O_TRUNC, 0);
  31. if (cctx->fd < 0)
  32. {
  33. rt_err_t err = rt_get_errno();
  34. rt_kprintf("error creating file: %d\n", err);
  35. rt_kprintf("abort transmission\n");
  36. return RYM_CODE_CAN;
  37. }
  38. cctx->flen = atoi((const char*)buf+strlen((const char*)buf)+1);
  39. if (cctx->flen == 0)
  40. cctx->flen = -1;
  41. return RYM_CODE_ACK;
  42. }
  43. static enum rym_code _rym_tof(
  44. struct rym_ctx *ctx,
  45. rt_uint8_t *buf,
  46. rt_size_t len)
  47. {
  48. struct custom_ctx *cctx = (struct custom_ctx*)ctx;
  49. RT_ASSERT(cctx->fd >= 0);
  50. if (cctx->flen == -1)
  51. {
  52. write(cctx->fd, buf, len);
  53. }
  54. else
  55. {
  56. int wlen = len > cctx->flen ? cctx->flen : len;
  57. write(cctx->fd, buf, wlen);
  58. cctx->flen -= wlen;
  59. }
  60. return RYM_CODE_ACK;
  61. }
  62. static enum rym_code _rym_end(
  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. close(cctx->fd);
  70. cctx->fd = -1;
  71. return RYM_CODE_ACK;
  72. }
  73. rt_err_t rym_write_to_file(rt_device_t idev)
  74. {
  75. rt_err_t res;
  76. struct custom_ctx *ctx = rt_malloc(sizeof(*ctx));
  77. RT_ASSERT(idev);
  78. rt_kprintf("entering RYM mode\n");
  79. res = rym_recv_on_device(&ctx->parent, idev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  80. _rym_bg, _rym_tof, _rym_end, 1000);
  81. /* there is no Ymodem traffic on the line so print out info. */
  82. rt_kprintf("leaving RYM mode with code %d\n", res);
  83. rt_kprintf("file %s has been created.\n", ctx->fpath);
  84. rt_free(ctx);
  85. return res;
  86. }
  87. #ifdef RT_USING_FINSH
  88. #include <finsh.h>
  89. rt_err_t ry(char *dname)
  90. {
  91. rt_err_t res;
  92. rt_device_t dev = rt_device_find(dname);
  93. if (!dev)
  94. {
  95. rt_kprintf("could not find device:%s\n", dname);
  96. return -RT_ERROR;
  97. }
  98. res = rym_write_to_file(dev);
  99. return res;
  100. }
  101. FINSH_FUNCTION_EXPORT(ry, receive files by ymodem protocol);
  102. #endif