esp_https_ota.h 9.5 KB

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