uiot_export_ota.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #ifndef C_SDK_UIOT_EXPORT_OTA_H_
  16. #define C_SDK_UIOT_EXPORT_OTA_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #include "uiot_import.h"
  21. typedef enum {
  22. OTA_IOCTL_FETCHED_SIZE, /* 固件已经下载的大小 */
  23. OTA_IOCTL_FILE_SIZE, /* 固件总大小 */
  24. OTA_IOCTL_MD5SUM, /* md5(字符串类型) */
  25. OTA_IOCTL_VERSION, /* 版本号(字符串类型)t */
  26. OTA_IOCTL_CHECK_FIRMWARE /* 对固件进行校验 */
  27. } IOT_OTA_CmdType;
  28. typedef enum {
  29. OTA_PROGRESS_DOWNLOADING = 1, /* 版本号(字符串类型)t */
  30. OTA_PROGRESS_BURNING = 2 /* 对固件进行校验 */
  31. } IOT_OTA_ProgressState;
  32. typedef enum {
  33. OTA_ERRCODE_FIRMWARE_NOT_EXIST = -1, /* 固件文件不存在(URL无法访问) */
  34. OTA_ERRCODE_SIGNATURE_EXPIRED = -2, /* URL签名过期 */
  35. OTA_ERRCODE_DOWNLOAD_TIMEOUT = -3, /* 下载超时 */
  36. OTA_ERRCODE_MD5_MISMATCH = -4, /* MD5不匹配 */
  37. OTA_ERRCODE_FIRMWARE_BURN_FAILED = -5, /* 固件烧录失败 */
  38. OTA_ERRCODE_UNDEFINED_ERROR = -6 /* 未定义错误 */
  39. } IOT_OTA_ReportErrCode;
  40. typedef enum {
  41. OTA_REPORT_UNDEFINED_ERROR = -6,
  42. OTA_REPORT_FIRMWARE_BURN_FAILED = -5,
  43. OTA_REPORT_MD5_MISMATCH = -4,
  44. OTA_REPORT_DOWNLOAD_TIMEOUT = -3,
  45. OTA_REPORT_SIGNATURE_EXPIRED = -2,
  46. OTA_REPORT_FIRMWARE_NOT_EXIST = -1,
  47. OTA_REPORT_NONE = 0,
  48. OTA_REPORT_DOWNLOADING = 1,
  49. OTA_REPORT_BURNING = 2,
  50. OTA_REPORT_SUCCESS = 3,
  51. OTA_REQUEST_FIRMWARE = 4,
  52. OTA_REPORT_VERSION = 5,
  53. } IOT_OTA_UpstreamMsgType;
  54. typedef int (*IOT_OTA_FetchCallback)(void *handle, IOT_OTA_UpstreamMsgType state);
  55. /* OTA状态 */
  56. typedef enum {
  57. OTA_STATE_UNINITED = 0, /* 未初始化 */
  58. OTA_STATE_INITED, /* 初始化完成 */
  59. OTA_STATE_FETCHING, /* 正在下载固件 */
  60. OTA_STATE_FETCHED, /* 固件下载完成 */
  61. OTA_STATE_DISCONNECTED /* 连接已经断开 */
  62. } IOT_OTA_State;
  63. typedef struct {
  64. uint32_t id; /* message id */
  65. IOT_OTA_State state; /* OTA state */
  66. uint32_t size_last_fetched; /* size of last downloaded */
  67. uint32_t size_fetched; /* size of already downloaded */
  68. uint32_t size_file; /* size of file */
  69. char *url; /* point to URL */
  70. char *download_name; /* download partition name */
  71. char *module; /* download module name */
  72. char *version; /* point to version */
  73. char *md5sum; /* MD5 string */
  74. void *md5; /* MD5 handle */
  75. void *ch_signal; /* channel handle of signal exchanged with OTA server */
  76. void *ch_fetch; /* channel handle of download */
  77. int err; /* last error code */
  78. Timer report_timer;
  79. IOT_OTA_FetchCallback fetch_callback_func;
  80. } OTA_Struct_t;
  81. /**
  82. * @brief 初始化OTA模块和返回句柄
  83. * MQTT客户端必须在调用此接口之前进行初始化
  84. *
  85. * @param product_sn: 指定产品序列号
  86. * @param device_sn: 指定设备序列号
  87. * @param ch_signal: 指定的信号通道.
  88. *
  89. * @retval : 成功则返回句柄,失败返回NULL
  90. */
  91. void *IOT_OTA_Init(const char *product_sn, const char *device_sn, void *ch_signal);
  92. /**
  93. * @brief 释放OTA相关的资源
  94. * 如果在下载之后没有调用重新启动,则须调用该接口以释放资源
  95. *
  96. * @param handle: 指定OTA模块
  97. *
  98. * @retval 0 : 成功
  99. * @retval < 0 : 失败,返回具体错误码
  100. */
  101. int IOT_OTA_Destroy(void *handle);
  102. /**
  103. * @brief 向OTA服务器报告固件版本信息。
  104. * NOTE: 进行OTA前请保证先上报一次本地固件的版本信息,以便服务器获取到设备目前的固件信息
  105. *
  106. * @param handle: 指定OTA模块
  107. * @param module: 版本所属类型
  108. * @param version: 以字符串格式指定固件版本
  109. *
  110. * @retval > 0 : 对应publish的packet id
  111. * @retval < 0 : 失败,返回具体错误码
  112. */
  113. int IOT_OTA_ReportVersion(void *handle, const char *module, const char *version);
  114. /**
  115. * @brief 向OTA服务器报告详细进度
  116. *
  117. * @param handle: 指定OTA模块
  118. * @param progress: 下载或升级进度
  119. * @param reportType: 指定当前的OTA状态
  120. *
  121. * @retval 0 : 成功
  122. * @retval < 0 : 失败,返回具体错误码
  123. */
  124. int IOT_OTA_ReportProgress(void *handle, int progress, IOT_OTA_ProgressState state);
  125. /**
  126. * @brief 向OTA服务器上报升级成功
  127. *
  128. * @param handle: 指定OTA模块
  129. * @param version: 即将升级的固件信息
  130. *
  131. * @retval > 0 : 对应publish的packet id
  132. * @retval < 0 : 失败,返回具体错误码
  133. */
  134. int IOT_OTA_ReportSuccess(void *handle, const char *version);
  135. /**
  136. * @brief 向OTA服务器上报失败信息
  137. *
  138. * @param handle: 指定OTA模块
  139. * @param err_code: 错误码
  140. *
  141. * @retval > 0 : 对应publish的packet id
  142. * @retval < 0 : 失败,返回具体错误码
  143. */
  144. int IOT_OTA_ReportFail(void *handle, IOT_OTA_ReportErrCode err_code);
  145. /**
  146. * @brief 检查固件是否已经下载完成
  147. *
  148. * @param handle: 指定OTA模块
  149. *
  150. * @retval 1 : Yes.
  151. * @retval 0 : No.
  152. */
  153. int IOT_OTA_IsFetchFinish(void *handle);
  154. /**
  155. * @brief 从具有特定超时值的远程服务器获取固件
  156. * 注意:如果你想要下载的更快,那么应该给出更大的“buf”
  157. *
  158. * @param handle: 指定OTA模块
  159. * @param buf: 指定存储固件数据的空间
  160. * @param buf_len: 用字节指定“buf”的长度
  161. * @param range_len: 用字节指定分片的长度
  162. * @param timeout_s: 超时时间
  163. *
  164. * @retval < 0 : 对应的错误码
  165. * @retval 0 : 在“timeout_s”超时期间没有任何数据被下载
  166. * @retval (0, len] : 在“timeout_s”超时时间内以字节的方式下载数据的长度
  167. */
  168. int IOT_OTA_FetchYield(void *handle, char *buf, size_t buf_len, size_t range_len, uint32_t timeout_s);
  169. /**
  170. * @brief 获取指定的OTA信息
  171. * 通过这个接口,可以获得诸如状态、文件大小、文件的md5等信息
  172. *
  173. * @param handle: 指定OTA模块
  174. * @param type: 指定您想要的信息,请参见详细信息“IOT_OTA_CmdType”
  175. * @param buf: 为数据交换指定缓冲区
  176. * @param buf_len: 在字节中指定“buf”的长度
  177. * @return
  178. NOTE:
  179. 1) 如果 type==OTA_IOCTL_FETCHED_SIZE, 'buf' 需要传入 uint32_t 类型指针, 'buf_len' 需指定为 4
  180. 2) 如果 type==OTA_IOCTL_FILE_SIZE, 'buf' 需要传入 uint32_t 类型指针, 'buf_len' 需指定为 4
  181. 3) 如果 type==OTA_IOCTL_MD5SUM, 'buf' 需要传入 buffer, 'buf_len' 需指定为 33
  182. 4) 如果 type==OTA_IOCTL_VERSION, 'buf' 需要传入 buffer, 'buf_len' 需指定为 OTA_VERSION_LEN_MAX
  183. 5) 如果 type==OTA_IOCTL_CHECK_FIRMWARE, 'buf' 需要传入 uint32_t 类型指针, 'buf_len'需指定为 4
  184. 0, 固件MD5校验不通过, 固件是无效的; 1, 固件是有效的.
  185. *
  186. * @retval 0 : 执行成功
  187. * @retval < 0 : 执行失败,返回对应的错误码
  188. */
  189. int IOT_OTA_Ioctl(void *handle, IOT_OTA_CmdType type, void *buf, size_t buf_len);
  190. /**
  191. * @brief 得到最后一个错误代码
  192. *
  193. * @param handle: 指定OTA模块
  194. *
  195. * @return 对应错误的错误码.
  196. */
  197. int IOT_OTA_GetLastError(void *handle);
  198. /**
  199. * @brief 请求固件更新消息。设备离线时,不能接收服务端推送的升级消息。通过MQTT协议接入物联网平台的设备再次上线后,主动请求固件更新消息
  200. *
  201. * @param handle: 指定OTA模块
  202. * @param module: 版本所属类型
  203. * @param version: 当前固件版本号
  204. *
  205. * @retval > 0 : 对应publish的packet id
  206. * @retval < 0 : 失败,返回具体错误码
  207. */
  208. int IOT_OTA_RequestFirmware(void *handle, const char *module, const char *version);
  209. /**
  210. * @brief 下载固件,下载结束后重启设备
  211. *
  212. * @param handle: 指定OTA模块
  213. *
  214. * @return 对应错误的错误码.
  215. */
  216. int IOT_OTA_fw_download(void *handle);
  217. #ifdef __cplusplus
  218. }
  219. #endif
  220. #endif //C_SDK_UIOT_EXPORT_OTA_H_