esp_https_ota.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <esp_http_client.h>
  8. #include <bootloader_common.h>
  9. #include "esp_app_desc.h"
  10. #include <sdkconfig.h>
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. typedef void *esp_https_ota_handle_t;
  15. typedef esp_err_t(*http_client_init_cb_t)(esp_http_client_handle_t);
  16. #if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  17. typedef struct {
  18. const char *data_in; /*!< Pointer to data to be decrypted */
  19. size_t data_in_len; /*!< Input data length */
  20. char *data_out; /*!< Pointer to data decrypted using callback, this will be freed after data is written to flash */
  21. size_t data_out_len; /*!< Output data length */
  22. } decrypt_cb_arg_t;
  23. typedef esp_err_t(*decrypt_cb_t)(decrypt_cb_arg_t *args, void *user_ctx);
  24. #endif // CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  25. /**
  26. * @brief ESP HTTPS OTA configuration
  27. */
  28. typedef struct {
  29. const esp_http_client_config_t *http_config; /*!< ESP HTTP client configuration */
  30. http_client_init_cb_t http_client_init_cb; /*!< Callback after ESP HTTP client is initialised */
  31. bool bulk_flash_erase; /*!< Erase entire flash partition during initialization. By default flash partition is erased during write operation and in chunk of 4K sector size */
  32. bool partial_http_download; /*!< Enable Firmware image to be downloaded over multiple HTTP requests */
  33. int max_http_request_size; /*!< Maximum request size for partial HTTP download */
  34. #if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  35. decrypt_cb_t decrypt_cb; /*!< Callback for external decryption layer */
  36. void *decrypt_user_ctx; /*!< User context for external decryption layer */
  37. #endif
  38. } esp_https_ota_config_t;
  39. #define ESP_ERR_HTTPS_OTA_BASE (0x9000)
  40. #define ESP_ERR_HTTPS_OTA_IN_PROGRESS (ESP_ERR_HTTPS_OTA_BASE + 1) /* OTA operation in progress */
  41. /**
  42. * @brief HTTPS OTA Firmware upgrade.
  43. *
  44. * This function allocates HTTPS OTA Firmware upgrade context, establishes HTTPS connection,
  45. * reads image data from HTTP stream and writes it to OTA partition and
  46. * finishes HTTPS OTA Firmware upgrade operation.
  47. * This API supports URL redirection, but if CA cert of URLs differ then it
  48. * should be appended to `cert_pem` member of `ota_config->http_config`.
  49. *
  50. * @param[in] ota_config pointer to esp_https_ota_config_t structure.
  51. *
  52. * @note This API handles the entire OTA operation, so if this API is being used
  53. * then no other APIs from `esp_https_ota` component should be called.
  54. * If more information and control is needed during the HTTPS OTA process,
  55. * then one can use `esp_https_ota_begin` and subsequent APIs. If this API returns
  56. * successfully, esp_restart() must be called to boot from the new firmware image.
  57. *
  58. * @return
  59. * - ESP_OK: OTA data updated, next reboot will use specified partition.
  60. * - ESP_FAIL: For generic failure.
  61. * - ESP_ERR_INVALID_ARG: Invalid argument
  62. * - ESP_ERR_OTA_VALIDATE_FAILED: Invalid app image
  63. * - ESP_ERR_NO_MEM: Cannot allocate memory for OTA operation.
  64. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
  65. * - For other return codes, refer OTA documentation in esp-idf's app_update component.
  66. */
  67. esp_err_t esp_https_ota(const esp_https_ota_config_t *ota_config);
  68. /**
  69. * @brief Start HTTPS OTA Firmware upgrade
  70. *
  71. * This function initializes ESP HTTPS OTA context and establishes HTTPS connection.
  72. * This function must be invoked first. If this function returns successfully, then `esp_https_ota_perform` should be
  73. * called to continue with the OTA process and there should be a call to `esp_https_ota_finish` on
  74. * completion of OTA operation or on failure in subsequent operations.
  75. * This API supports URL redirection, but if CA cert of URLs differ then it
  76. * should be appended to `cert_pem` member of `http_config`, which is a part of `ota_config`.
  77. * In case of error, this API explicitly sets `handle` to NULL.
  78. *
  79. * @param[in] ota_config pointer to esp_https_ota_config_t structure
  80. * @param[out] handle pointer to an allocated data of type `esp_https_ota_handle_t`
  81. * which will be initialised in this function
  82. *
  83. * @note This API is blocking, so setting `is_async` member of `http_config` structure will
  84. * result in an error.
  85. *
  86. * @return
  87. * - ESP_OK: HTTPS OTA Firmware upgrade context initialised and HTTPS connection established
  88. * - ESP_FAIL: For generic failure.
  89. * - ESP_ERR_INVALID_ARG: Invalid argument (missing/incorrect config, certificate, etc.)
  90. * - For other return codes, refer documentation in app_update component and esp_http_client
  91. * component in esp-idf.
  92. */
  93. esp_err_t esp_https_ota_begin(const esp_https_ota_config_t *ota_config, esp_https_ota_handle_t *handle);
  94. /**
  95. * @brief Read image data from HTTP stream and write it to OTA partition
  96. *
  97. * This function reads image data from HTTP stream and writes it to OTA partition. This function
  98. * must be called only if esp_https_ota_begin() returns successfully.
  99. * This function must be called in a loop since it returns after every HTTP read operation thus
  100. * giving you the flexibility to stop OTA operation midway.
  101. *
  102. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  103. *
  104. * @return
  105. * - ESP_ERR_HTTPS_OTA_IN_PROGRESS: OTA update is in progress, call this API again to continue.
  106. * - ESP_OK: OTA update was successful
  107. * - ESP_FAIL: OTA update failed
  108. * - ESP_ERR_INVALID_ARG: Invalid argument
  109. * - ESP_ERR_INVALID_VERSION: Invalid chip revision in image header
  110. * - ESP_ERR_OTA_VALIDATE_FAILED: Invalid app image
  111. * - ESP_ERR_NO_MEM: Cannot allocate memory for OTA operation.
  112. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
  113. * - For other return codes, refer OTA documentation in esp-idf's app_update component.
  114. */
  115. esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle);
  116. /**
  117. * @brief Checks if complete data was received or not
  118. *
  119. * @note This API can be called just before esp_https_ota_finish() to validate if the complete image was indeed received.
  120. *
  121. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  122. *
  123. * @return
  124. * - false
  125. * - true
  126. */
  127. bool esp_https_ota_is_complete_data_received(esp_https_ota_handle_t https_ota_handle);
  128. /**
  129. * @brief Clean-up HTTPS OTA Firmware upgrade and close HTTPS connection
  130. *
  131. * This function closes the HTTP connection and frees the ESP HTTPS OTA context.
  132. * This function switches the boot partition to the OTA partition containing the
  133. * new firmware image.
  134. *
  135. * @note If this API returns successfully, esp_restart() must be called to
  136. * boot from the new firmware image
  137. * esp_https_ota_finish should not be called after calling esp_https_ota_abort
  138. *
  139. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  140. *
  141. * @return
  142. * - ESP_OK: Clean-up successful
  143. * - ESP_ERR_INVALID_STATE
  144. * - ESP_ERR_INVALID_ARG: Invalid argument
  145. * - ESP_ERR_OTA_VALIDATE_FAILED: Invalid app image
  146. */
  147. esp_err_t esp_https_ota_finish(esp_https_ota_handle_t https_ota_handle);
  148. /**
  149. * @brief Clean-up HTTPS OTA Firmware upgrade and close HTTPS connection
  150. *
  151. * This function closes the HTTP connection and frees the ESP HTTPS OTA context.
  152. *
  153. * @note esp_https_ota_abort should not be called after calling esp_https_ota_finish
  154. *
  155. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  156. *
  157. * @return
  158. * - ESP_OK: Clean-up successful
  159. * - ESP_ERR_INVALID_STATE: Invalid ESP HTTPS OTA state
  160. * - ESP_FAIL: OTA not started
  161. * - ESP_ERR_NOT_FOUND: OTA handle not found
  162. * - ESP_ERR_INVALID_ARG: Invalid argument
  163. */
  164. esp_err_t esp_https_ota_abort(esp_https_ota_handle_t https_ota_handle);
  165. /**
  166. * @brief Reads app description from image header. The app description provides information
  167. * like the "Firmware version" of the image.
  168. *
  169. * @note This API can be called only after esp_https_ota_begin() and before esp_https_ota_perform().
  170. * Calling this API is not mandatory.
  171. *
  172. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  173. * @param[out] new_app_info pointer to an allocated esp_app_desc_t structure
  174. *
  175. * @return
  176. * - ESP_ERR_INVALID_ARG: Invalid arguments
  177. * - ESP_ERR_INVALID_STATE: Invalid state to call this API. esp_https_ota_begin() not called yet.
  178. * - ESP_FAIL: Failed to read image descriptor
  179. * - ESP_OK: Successfully read image descriptor
  180. */
  181. esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, esp_app_desc_t *new_app_info);
  182. /**
  183. * @brief This function returns OTA image data read so far.
  184. *
  185. * @note This API should be called only if `esp_https_ota_perform()` has been called atleast once or
  186. * if `esp_https_ota_get_img_desc` has been called before.
  187. *
  188. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  189. *
  190. * @return
  191. * - -1 On failure
  192. * - total bytes read so far
  193. */
  194. int esp_https_ota_get_image_len_read(esp_https_ota_handle_t https_ota_handle);
  195. /**
  196. * @brief This function returns OTA image total size.
  197. *
  198. * @note This API should be called after esp_https_ota_begin() has been already called.
  199. * This can be used to create some sort of progress indication
  200. * (in combination with esp_https_ota_get_image_len_read())
  201. *
  202. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  203. *
  204. * @return
  205. * - -1 On failure or chunked encoding
  206. * - total bytes of image
  207. */
  208. int esp_https_ota_get_image_size(esp_https_ota_handle_t https_ota_handle);
  209. #ifdef __cplusplus
  210. }
  211. #endif