fota.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) 2019-2020 Alibaba Group Holding Limited
  3. */
  4. #ifndef __FOTA_H__
  5. #define __FOTA_H__
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include <stdint.h>
  10. #include <time.h>
  11. #include <aos/list.h>
  12. #include <aos/kernel.h>
  13. #include <yoc/netio.h>
  14. #define FW_URL_KEY "FW_URL_KEY"
  15. #define KV_FOTA_OFFSET "fota_offset"
  16. #define KV_FOTA_CLOUD_PLATFORM "fota_cldp"
  17. #define KV_FOTA_FROM_URL "fota_fromurl"
  18. #define KV_FOTA_TO_URL "fota_tourl"
  19. #define KV_FOTA_READ_TIMEOUTMS "fota_rtmout"
  20. #define KV_FOTA_WRITE_TIMEOUTMS "fota_wtmout"
  21. #define KV_FOTA_RETRY_COUNT "fota_retry"
  22. #define KV_FOTA_SLEEP_TIMEMS "fota_slptm"
  23. #define KV_FOTA_AUTO_CHECK "fota_autock"
  24. #define KV_FOTA_FINISH "fota_finish"
  25. #ifndef CONFIG_FOTA_TASK_STACK_SIZE
  26. #define CONFIG_FOTA_TASK_STACK_SIZE (4 * 1024)
  27. #endif
  28. // use httpclient
  29. #ifndef CONFIG_FOTA_USE_HTTPC
  30. #define CONFIG_FOTA_USE_HTTPC 0
  31. #endif
  32. #ifndef CONFIG_FOTA_DATA_IN_RAM
  33. #define CONFIG_FOTA_DATA_IN_RAM 0
  34. #endif
  35. typedef struct fota fota_t;
  36. typedef enum {
  37. FOTA_EVENT_START = 0, /*!< Start the fota version check and download steps */
  38. FOTA_EVENT_VERSION, /*!< Check version from server ok */
  39. FOTA_EVENT_PROGRESS, /*!< Downloading the fota data */
  40. FOTA_EVENT_FAIL, /*!< This event occurs when there are any errors during execution */
  41. FOTA_EVENT_VERIFY, /*!< verify fota data */
  42. FOTA_EVENT_FINISH, /*!< fota download flow finish */
  43. FOTA_EVENT_QUIT, /*!< Fota task quit */
  44. FOTA_EVENT_RESTART /*!< real want to restart */
  45. } fota_event_e;
  46. typedef enum {
  47. FOTA_ERROR_NULL = 0,
  48. FOTA_ERROR_VERSION_CHECK,
  49. FOTA_ERROR_PREPARE,
  50. FOTA_ERROR_NET_SEEK,
  51. FOTA_ERROR_NET_READ,
  52. FOTA_ERROR_WRITE,
  53. FOTA_ERROR_MALLOC,
  54. FOTA_ERROR_VERIFY
  55. } fota_error_code_e;
  56. typedef enum fota_status {
  57. FOTA_INIT = 1, /*!< create fota task, wait for version check */
  58. FOTA_DOWNLOAD = 2, /*!< start to download fota data */
  59. FOTA_ABORT = 3, /*!< read or write exception */
  60. FOTA_FINISH = 4, /*!< download finish */
  61. } fota_status_e;
  62. typedef struct {
  63. char *cur_version; /*!< the local image version, read from kv*/
  64. char *local_changelog; /*!< the local image changelog, read from kv*/
  65. char *new_version; /*!< the incoming image version, read from cloud server*/
  66. char *changelog; /*!< the incoming image changelog, read from cloud server*/
  67. char *fota_url; /*!< the incoming image url, read from cloud server*/
  68. int timestamp; /*!< the incoming image timestamp, read from cloud server*/
  69. } fota_info_t;
  70. typedef struct fota_cls {
  71. const char *name;
  72. int (*init)(fota_info_t *info);
  73. int (*version_check)(fota_info_t *info);
  74. int (*finish)(fota_info_t *info);
  75. int (*fail)(fota_info_t *info);
  76. int (*restart)(void);
  77. int64_t (*get_size)(const char *name);
  78. } fota_cls_t;
  79. typedef struct {
  80. int read_timeoutms; /*!< read timeout, millisecond */
  81. int write_timeoutms; /*!< write timeout, millisecond */
  82. int retry_count; /*!< when download abort, it will retry to download again in retry_count times */
  83. int sleep_time; /*!< the sleep time for auto-check task */
  84. int auto_check_en; /*!< whether check version automatic */
  85. } fota_config_t;
  86. typedef int (*fota_event_cb_t)(void *fota, fota_event_e event); ///< fota Event call back.
  87. struct fota {
  88. const fota_cls_t *cls; /*!< the fota server ops */
  89. netio_t *from; /*!< the read netio handle */
  90. netio_t *to; /*!< the write netio handle */
  91. fota_status_e status; /*!< the fota status, see enum `fota_status_e` */
  92. char *from_path; /*!< where the fota data read from, url format */
  93. char *to_path; /*!< where the fota data write to, url format*/
  94. uint8_t *buffer; /*!< buffer for reading data from net */
  95. int offset; /*!< downloaded data bytes */
  96. int total_size; /*!< total length of fota data */
  97. int quit; /*!< fota task quit flag */
  98. aos_task_t task; /*!< fota task handle */
  99. aos_sem_t sem; /*!< semaphore for waiting fota task quit */
  100. aos_sem_t sem_download; /*!< semaphore for starting download */
  101. aos_sem_t do_check_event; /*!< the semaphore for checking version loop or force */
  102. fota_event_cb_t event_cb; /*!< the event callback */
  103. fota_error_code_e error_code; /*!< fota error code, get it when event occurs */
  104. fota_config_t config; /*!< fota config */
  105. fota_info_t info; /*!< fota information */
  106. aos_timer_t restart_timer; /*!< the timer to norify to restart */
  107. void *private; /*!< user data context */
  108. };
  109. /**
  110. * @brief 创建FOTA服务
  111. * @param [in] fota: fota 句柄
  112. * @return 0 on success, -1 on failed
  113. */
  114. int fota_start(fota_t *fota);
  115. /**
  116. * @brief 停止FOTA功能,退出FOTA服务
  117. * @param [in] fota: fota 句柄
  118. * @return 0 on success, -1 on failed
  119. */
  120. int fota_stop(fota_t *fota);
  121. /**
  122. * @brief 开始下载
  123. * @param [in] fota: fota 句柄
  124. * @return 0 on success, -1 on failed
  125. */
  126. int fota_download(fota_t *fota);
  127. /**
  128. * @brief 触发重启并开始升级
  129. * @param [in] fota: fota 句柄
  130. * @param [in] delay_ms: 延时多少毫秒才重启
  131. * @return 0 on success, -1 on failed
  132. */
  133. int fota_restart(fota_t *fota, int delay_ms);
  134. /**
  135. * @brief 配置FOTA参数
  136. * @param [in] fota: fota 句柄
  137. * @param [in] config: 配置数据指针,具体见 `fota_config_t`
  138. */
  139. void fota_config(fota_t *fota, fota_config_t *config);
  140. /**
  141. * @brief 获取FOTA参数
  142. * @param [in] fota: fota 句柄
  143. * @return 配置数据指针,具体见 `fota_config_t`
  144. */
  145. fota_config_t *fota_get_config(fota_t *fota);
  146. /**
  147. * @brief 设置是否自动不断检测服务器固件版本并升级的功能
  148. * @param [in] fota: fota 句柄
  149. * @param [in] enable: 0表示不自动检测,1表示自动检测
  150. */
  151. void fota_set_auto_check(fota_t *fota, int enable);
  152. /**
  153. * @brief 获取是否自动检测判断
  154. * @param [in] fota: fota 句柄
  155. * @return 0表示不自动检测,1表示自动检测
  156. */
  157. int fota_get_auto_check(fota_t *fota);
  158. /**
  159. * @brief 获取升级状态
  160. * @param [in] fota: fota 句柄
  161. * @return fota_status_e
  162. */
  163. fota_status_e fota_get_status(fota_t *fota);
  164. /**
  165. * @brief 获取剩余可用空间
  166. * @param [in] fota: fota 句柄
  167. * @param [in] name:分区名字
  168. * @return 可用空间,>= 0 on success, -1 on failed
  169. */
  170. int64_t fota_get_size(fota_t *fota, const char *name);
  171. /**
  172. * @brief fota初始化
  173. * @param [in] fota_name: FOTA平台名字,比如"cop",
  174. * @param [in] dst: 差分包存储url
  175. * @param [in] event_cb: 用户事件回调
  176. * @return fota句柄或者NULL
  177. */
  178. fota_t *fota_open(const char *fota_name, const char *dst, fota_event_cb_t event_cb);
  179. /**
  180. * @brief 关闭FOTA功能,释放所有资源
  181. * @param [in] fota: fota 句柄
  182. * @return 0 on success, -1 on failed
  183. */
  184. int fota_close(fota_t *fota);
  185. /**
  186. * @brief 强制检测版本
  187. * @param [in] fota: fota 句柄
  188. */
  189. void fota_do_check(fota_t *fota);
  190. /**
  191. * @brief 注册为cop平台,即从cop平台下载固件
  192. * @return 0 on success, -1 on failed
  193. */
  194. int fota_register_cop(void);
  195. /**
  196. * @brief 注册平台接口
  197. * @param [in] cls: 不同平台实现的接口集合,具体实现接口见`fota_cls_t`
  198. * @return 0 on success, -1 on failed
  199. */
  200. int fota_register(const fota_cls_t *cls);
  201. /**
  202. * @brief 对已经下载好的FOTA数据进行校验,(用户可自定义)
  203. * @return 0 on success, -1 on failed
  204. */
  205. int fota_data_verify(void);
  206. #if CONFIG_FOTA_DATA_IN_RAM > 0
  207. /**
  208. * @brief 获取存储fota数据的ram地址,(用户可自定义)
  209. * @return address
  210. */
  211. unsigned long fota_data_address_get(void);
  212. #endif
  213. #ifdef __cplusplus
  214. }
  215. #endif
  216. #endif