esp_https_ota.h 9.7 KB

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