netio.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2019-2020 Alibaba Group Holding Limited
  3. */
  4. #ifndef YOC_NETIO_H
  5. #define YOC_NETIO_H
  6. #include <stdint.h>
  7. #include <stddef.h>
  8. #include <aos/list.h>
  9. #ifndef CONFIG_FOTA_BUFFER_SIZE
  10. #define CONFIG_FOTA_BUFFER_SIZE 512
  11. #define BUFFER_SIZE 2048
  12. #else
  13. #define BUFFER_SIZE (CONFIG_FOTA_BUFFER_SIZE * 2)
  14. #endif
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. typedef struct netio_cls netio_cls_t;
  19. typedef struct {
  20. const netio_cls_t *cls; /*!< netio ops */
  21. size_t offset; /*!< offset for seek */
  22. size_t size; /*!< file size or partition size */
  23. size_t block_size; /*!< the size for transmission(sector size) */
  24. void *private; /*!< user data */
  25. } netio_t;
  26. struct netio_cls {
  27. const char *name;
  28. int (*open)(netio_t *io, const char *path);
  29. int (*close)(netio_t *io);
  30. int (*read)(netio_t *io, uint8_t *buffer, int length, int timeoutms);
  31. int (*write)(netio_t *io, uint8_t *buffer, int length, int timeoutms);
  32. int (*remove)(netio_t *io);
  33. int (*seek)(netio_t *io, size_t offset, int whence);
  34. void *private;
  35. };
  36. /**
  37. * @brief netio 注册
  38. * @param [in] cls: netio 操作接口
  39. * @return 0 on success, -1 on failed
  40. */
  41. int netio_register(const netio_cls_t *cls);
  42. /**
  43. * @brief 将http操作功能注册到netio
  44. * @return 0 on success, -1 on failed
  45. */
  46. int netio_register_http(void);
  47. /**
  48. * @brief 将http client操作功能注册到netio
  49. * @param [in] cert: https证书,可以为NULL
  50. * @return 0 on success, -1 on failed
  51. */
  52. int netio_register_httpc(const char *cert);
  53. /**
  54. * @brief 将flash操作功能注册到netio
  55. * @return 0 on success, -1 on failed
  56. */
  57. int netio_register_flash(void);
  58. /**
  59. * @brief 将coap操作功能注册到netio
  60. * @return 0 on success, -1 on failed
  61. */
  62. int netio_register_coap(void);
  63. /**
  64. * @brief netio 打开
  65. * @param [in] path: 下载或者储存 url
  66. * @return netio_t句柄或者NULL
  67. */
  68. netio_t *netio_open(const char *path);
  69. /**
  70. * @brief netio 关闭
  71. * @param [in] io: netio句柄
  72. * @return 0 on success, -1 on failed
  73. */
  74. int netio_close(netio_t *io);
  75. /**
  76. * @brief netio 读取
  77. * @param [in] io: netio句柄
  78. * @param [in] buffer: 用于存放读取数据的buffer
  79. * @param [in] lenght: buffer大小
  80. * @param [in] timeoutms: 超时时长
  81. * @return 0表示文件读完,-1超时失败,否则为读取的长度
  82. */
  83. int netio_read(netio_t *io, uint8_t *buffer, size_t lenght, int timeoutms);
  84. /**
  85. * @brief netio 写
  86. * @param [in] io: netio句柄
  87. * @param [in] buffer: 用于存放读取数据的buffer
  88. * @param [in] lenght: buffer大小
  89. * @param [in] timeoutms: 超时时长
  90. * @return -1超时失败,否则为写入的长度
  91. */
  92. int netio_write(netio_t *io, uint8_t *buffer, size_t lenght, int timeoutms);
  93. /**
  94. * @brief netio 寻址
  95. * @param [in] io: netio句柄
  96. * @param [in] offset:偏移
  97. * @param [in] whence:偏移方向
  98. * @return 0 on success, -1 on failed
  99. */
  100. int netio_seek(netio_t *io, size_t offset, int whence);
  101. #ifdef __cplusplus
  102. }
  103. #endif
  104. #endif