esp_https_ota.h 7.0 KB

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