esp_https_ota.h 8.5 KB

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