uiot_export_ota.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /**
  41. * @brief 初始化OTA模块和返回句柄
  42. * MQTT客户端必须在调用此接口之前进行初始化
  43. *
  44. * @param product_sn: 指定产品序列号
  45. * @param device_sn: 指定设备序列号
  46. * @param ch_signal: 指定的信号通道.
  47. *
  48. * @retval : 成功则返回句柄,失败返回NULL
  49. */
  50. void *IOT_OTA_Init(const char *product_sn, const char *device_sn, void *ch_signal);
  51. /**
  52. * @brief 释放OTA相关的资源
  53. * 如果在下载之后没有调用重新启动,则须调用该接口以释放资源
  54. *
  55. * @param handle: 指定OTA模块
  56. *
  57. * @retval 0 : 成功
  58. * @retval < 0 : 失败,返回具体错误码
  59. */
  60. int IOT_OTA_Destroy(void *handle);
  61. /**
  62. * @brief 向OTA服务器报告固件版本信息。
  63. * NOTE: 进行OTA前请保证先上报一次本地固件的版本信息,以便服务器获取到设备目前的固件信息
  64. *
  65. * @param handle: 指定OTA模块
  66. * @param version: 以字符串格式指定固件版本
  67. *
  68. * @retval > 0 : 对应publish的packet id
  69. * @retval < 0 : 失败,返回具体错误码
  70. */
  71. int IOT_OTA_ReportVersion(void *handle, const char *version);
  72. /**
  73. * @brief 向OTA服务器报告详细进度
  74. *
  75. * @param handle: 指定OTA模块
  76. * @param progress: 下载或升级进度
  77. * @param reportType: 指定当前的OTA状态
  78. *
  79. * @retval 0 : 成功
  80. * @retval < 0 : 失败,返回具体错误码
  81. */
  82. int IOT_OTA_ReportProgress(void *handle, int progress, IOT_OTA_ProgressState state);
  83. /**
  84. * @brief 向OTA服务器上报升级成功
  85. *
  86. * @param handle: 指定OTA模块
  87. * @param version: 即将升级的固件信息
  88. *
  89. * @retval > 0 : 对应publish的packet id
  90. * @retval < 0 : 失败,返回具体错误码
  91. */
  92. int IOT_OTA_ReportSuccess(void *handle, const char *version);
  93. /**
  94. * @brief 向OTA服务器上报失败信息
  95. *
  96. * @param handle: 指定OTA模块
  97. * @param err_code: 错误码
  98. *
  99. * @retval > 0 : 对应publish的packet id
  100. * @retval < 0 : 失败,返回具体错误码
  101. */
  102. int IOT_OTA_ReportFail(void *handle, IOT_OTA_ReportErrCode err_code);
  103. /**
  104. * @brief 检查固件是否已经下载完成
  105. *
  106. * @param handle: 指定OTA模块
  107. *
  108. * @retval 1 : Yes.
  109. * @retval 0 : No.
  110. */
  111. int IOT_OTA_IsFetchFinish(void *handle);
  112. /**
  113. * @brief 从具有特定超时值的远程服务器获取固件
  114. * 注意:如果你想要下载的更快,那么应该给出更大的“buf”
  115. *
  116. * @param handle: 指定OTA模块
  117. * @param buf: 指定存储固件数据的空间
  118. * @param buf_len: 用字节指定“buf”的长度
  119. * @param range_len: 用字节指定分片的长度
  120. * @param timeout_s: 超时时间
  121. *
  122. * @retval < 0 : 对应的错误码
  123. * @retval 0 : 在“timeout_s”超时期间没有任何数据被下载
  124. * @retval (0, len] : 在“timeout_s”超时时间内以字节的方式下载数据的长度
  125. */
  126. int IOT_OTA_FetchYield(void *handle, char *buf, size_t buf_len, size_t range_len, uint32_t timeout_s);
  127. /**
  128. * @brief 获取指定的OTA信息
  129. * 通过这个接口,可以获得诸如状态、文件大小、文件的md5等信息
  130. *
  131. * @param handle: 指定OTA模块
  132. * @param type: 指定您想要的信息,请参见详细信息“IOT_OTA_CmdType”
  133. * @param buf: 为数据交换指定缓冲区
  134. * @param buf_len: 在字节中指定“buf”的长度
  135. * @return
  136. NOTE:
  137. 1) 如果 type==OTA_IOCTL_FETCHED_SIZE, 'buf' 需要传入 uint32_t 类型指针, 'buf_len' 需指定为 4
  138. 2) 如果 type==OTA_IOCTL_FILE_SIZE, 'buf' 需要传入 uint32_t 类型指针, 'buf_len' 需指定为 4
  139. 3) 如果 type==OTA_IOCTL_MD5SUM, 'buf' 需要传入 buffer, 'buf_len' 需指定为 33
  140. 4) 如果 type==OTA_IOCTL_VERSION, 'buf' 需要传入 buffer, 'buf_len' 需指定为 OTA_VERSION_LEN_MAX
  141. 5) 如果 type==OTA_IOCTL_CHECK_FIRMWARE, 'buf' 需要传入 uint32_t 类型指针, 'buf_len'需指定为 4
  142. 0, 固件MD5校验不通过, 固件是无效的; 1, 固件是有效的.
  143. *
  144. * @retval 0 : 执行成功
  145. * @retval < 0 : 执行失败,返回对应的错误码
  146. */
  147. int IOT_OTA_Ioctl(void *handle, IOT_OTA_CmdType type, void *buf, size_t buf_len);
  148. /**
  149. * @brief 得到最后一个错误代码
  150. *
  151. * @param handle: 指定OTA模块
  152. *
  153. * @return 对应错误的错误码.
  154. */
  155. int IOT_OTA_GetLastError(void *handle);
  156. /**
  157. * @brief 请求固件更新消息。设备离线时,不能接收服务端推送的升级消息。通过MQTT协议接入物联网平台的设备再次上线后,主动请求固件更新消息
  158. *
  159. * @param handle: 指定OTA模块
  160. * @param version: 当前固件版本号
  161. *
  162. * @retval > 0 : 对应publish的packet id
  163. * @retval < 0 : 失败,返回具体错误码
  164. */
  165. int IOT_OTA_RequestFirmware(void *handle, const char *version);
  166. /**
  167. * @brief 下载固件,下载结束后重启设备
  168. *
  169. * @param handle: 指定OTA模块
  170. *
  171. * @return 对应错误的错误码.
  172. */
  173. int IOT_OTA_fw_download(void *handle);
  174. #ifdef __cplusplus
  175. }
  176. #endif
  177. #endif //C_SDK_UIOT_EXPORT_OTA_H_