esp_https_ota.h 9.2 KB

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