esp_https_ota.h 7.5 KB

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