esp_https_ota.h 8.9 KB

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