esp_https_ota.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. *
  65. * @param[in] ota_config pointer to esp_https_ota_config_t structure
  66. * @param[out] handle pointer to an allocated data of type `esp_https_ota_handle_t`
  67. * which will be initialised in this function
  68. *
  69. * @note This API is blocking, so setting `is_async` member of `http_config` structure will
  70. * result in an error.
  71. *
  72. * @return
  73. * - ESP_OK: HTTPS OTA Firmware upgrade context initialised and HTTPS connection established
  74. * - ESP_FAIL: For generic failure.
  75. * - ESP_ERR_INVALID_ARG: Invalid argument (missing/incorrect config, certificate, etc.)
  76. * - For other return codes, refer documentation in app_update component and esp_http_client
  77. * component in esp-idf.
  78. */
  79. esp_err_t esp_https_ota_begin(esp_https_ota_config_t *ota_config, esp_https_ota_handle_t *handle);
  80. /**
  81. * @brief Read image data from HTTP stream and write it to OTA partition
  82. *
  83. * This function reads image data from HTTP stream and writes it to OTA partition. This function
  84. * must be called only if esp_https_ota_begin() returns successfully.
  85. * This function must be called in a loop since it returns after every HTTP read operation thus
  86. * giving you the flexibility to stop OTA operation midway.
  87. *
  88. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  89. *
  90. * @return
  91. * - ESP_ERR_HTTPS_OTA_IN_PROGRESS: OTA update is in progress, call this API again to continue.
  92. * - ESP_OK: OTA update was successful
  93. * - ESP_FAIL: OTA update failed
  94. * - ESP_ERR_INVALID_ARG: Invalid argument
  95. * - ESP_ERR_OTA_VALIDATE_FAILED: Invalid app image
  96. * - ESP_ERR_NO_MEM: Cannot allocate memory for OTA operation.
  97. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
  98. * - For other return codes, refer OTA documentation in esp-idf's app_update component.
  99. */
  100. esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle);
  101. /**
  102. * @brief Clean-up HTTPS OTA Firmware upgrade and close HTTPS connection
  103. *
  104. * This function closes the HTTP connection and frees the ESP HTTPS OTA context.
  105. * This function switches the boot partition to the OTA partition containing the
  106. * new firmware image.
  107. *
  108. * @note If this API returns successfully, esp_restart() must be called to
  109. * boot from the new firmware image
  110. *
  111. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  112. *
  113. * @return
  114. * - ESP_OK: Clean-up successful
  115. * - ESP_ERR_INVALID_STATE
  116. * - ESP_ERR_INVALID_ARG: Invalid argument
  117. * - ESP_ERR_OTA_VALIDATE_FAILED: Invalid app image
  118. */
  119. esp_err_t esp_https_ota_finish(esp_https_ota_handle_t https_ota_handle);
  120. /**
  121. * @brief Reads app description from image header. The app description provides information
  122. * like the "Firmware version" of the image.
  123. *
  124. * @note This API can be called only after esp_https_ota_begin() and before esp_https_ota_perform().
  125. * Calling this API is not mandatory.
  126. *
  127. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  128. * @param[out] new_app_info pointer to an allocated esp_app_desc_t structure
  129. *
  130. * @return
  131. * - ESP_ERR_INVALID_ARG: Invalid arguments
  132. * - ESP_FAIL: Failed to read image descriptor
  133. * - ESP_OK: Successfully read image descriptor
  134. */
  135. esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, esp_app_desc_t *new_app_info);
  136. /*
  137. * @brief This function returns OTA image data read so far.
  138. *
  139. * @note This API should be called only if `esp_https_ota_perform()` has been called atleast once or
  140. * if `esp_https_ota_get_img_desc` has been called before.
  141. *
  142. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  143. *
  144. * @return
  145. * - -1 On failure
  146. * - total bytes read so far
  147. */
  148. int esp_https_ota_get_image_len_read(esp_https_ota_handle_t https_ota_handle);
  149. #ifdef __cplusplus
  150. }
  151. #endif