ESP HTTPS OTA

Overview

esp_https_ota provides simplified APIs to perform firmware upgrades over HTTPS. It’s an abstraction layer over existing OTA APIs.

Application Example

esp_err_t do_firmware_upgrade()
{
    esp_http_client_config_t config = {
        .url = CONFIG_FIRMWARE_UPGRADE_URL,
        .cert_pem = (char *)server_cert_pem_start,
    };
    esp_err_t ret = esp_https_ota(&config);
    if (ret == ESP_OK) {
        esp_restart();
    } else {
        return ESP_FAIL;
    }
    return ESP_OK;
}

Signature Verification

For additional security, signature of OTA firmware images can be verified. For that, refer Secure OTA Updates Without Secure boot

API Reference

Functions

esp_err_t esp_https_ota(const esp_http_client_config_t *config)

HTTPS OTA Firmware upgrade.

This function allocates HTTPS OTA Firmware upgrade context, establishes HTTPS connection, reads image data from HTTP stream and writes it to OTA partition and finishes HTTPS OTA Firmware upgrade operation. This API supports URL redirection, but if CA cert of URLs differ then it should be appended to cert_pem member of config.

Note
This API handles the entire OTA operation, so if this API is being used then no other APIs from esp_https_ota component should be called. If more information and control is needed during the HTTPS OTA process, then one can use esp_https_ota_begin and subsequent APIs. If this API returns successfully, esp_restart() must be called to boot from the new firmware image.
Return
  • ESP_OK: OTA data updated, next reboot will use specified partition.
  • ESP_FAIL: For generic failure.
  • ESP_ERR_INVALID_ARG: Invalid argument
  • ESP_ERR_OTA_VALIDATE_FAILED: Invalid app image
  • ESP_ERR_NO_MEM: Cannot allocate memory for OTA operation.
  • ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
  • For other return codes, refer OTA documentation in esp-idf’s app_update component.
Parameters

esp_err_t esp_https_ota_begin(esp_https_ota_config_t *ota_config, esp_https_ota_handle_t *handle)

Start HTTPS OTA Firmware upgrade.

This function initializes ESP HTTPS OTA context and establishes HTTPS connection. This function must be invoked first. If this function returns successfully, then esp_https_ota_perform should be called to continue with the OTA process and there should be a call to esp_https_ota_finish on completion of OTA operation or on failure in subsequent operations. This API supports URL redirection, but if CA cert of URLs differ then it should be appended to cert_pem member of http_config, which is a part of ota_config.

Note
This API is blocking, so setting is_async member of http_config structure will result in an error.
Return
  • ESP_OK: HTTPS OTA Firmware upgrade context initialised and HTTPS connection established
  • ESP_FAIL: For generic failure.
  • ESP_ERR_INVALID_ARG: Invalid argument (missing/incorrect config, certificate, etc.)
  • For other return codes, refer documentation in app_update component and esp_http_client component in esp-idf.
Parameters
  • ota_config: pointer to esp_https_ota_config_t structure
  • handle: pointer to an allocated data of type esp_https_ota_handle_t which will be initialised in this function

esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle)

Read image data from HTTP stream and write it to OTA partition.

This function reads image data from HTTP stream and writes it to OTA partition. This function must be called only if esp_https_ota_begin() returns successfully. This function must be called in a loop since it returns after every HTTP read operation thus giving you the flexibility to stop OTA operation midway.

Return
  • ESP_ERR_HTTPS_OTA_IN_PROGRESS: OTA update is in progress, call this API again to continue.
  • ESP_OK: OTA update was successful
  • ESP_FAIL: OTA update failed
  • ESP_ERR_INVALID_ARG: Invalid argument
  • ESP_ERR_OTA_VALIDATE_FAILED: Invalid app image
  • ESP_ERR_NO_MEM: Cannot allocate memory for OTA operation.
  • ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
  • For other return codes, refer OTA documentation in esp-idf’s app_update component.
Parameters
  • https_ota_handle: pointer to esp_https_ota_handle_t structure

esp_err_t esp_https_ota_finish(esp_https_ota_handle_t https_ota_handle)

Clean-up HTTPS OTA Firmware upgrade and close HTTPS connection.

This function closes the HTTP connection and frees the ESP HTTPS OTA context. This function switches the boot partition to the OTA partition containing the new firmware image.

Note
If this API returns successfully, esp_restart() must be called to boot from the new firmware image
Return
  • ESP_OK: Clean-up successful
  • ESP_ERR_INVALID_STATE
  • ESP_ERR_INVALID_ARG: Invalid argument
  • ESP_ERR_OTA_VALIDATE_FAILED: Invalid app image
Parameters
  • https_ota_handle: pointer to esp_https_ota_handle_t structure

esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, esp_app_desc_t *new_app_info)

Reads app description from image header. The app description provides information like the “Firmware version” of the image.

Note
This API can be called only after esp_https_ota_begin() and before esp_https_ota_perform(). Calling this API is not mandatory.
Return
  • ESP_ERR_INVALID_ARG: Invalid arguments
  • ESP_FAIL: Failed to read image descriptor
  • ESP_OK: Successfully read image descriptor
Parameters
  • https_ota_handle: pointer to esp_https_ota_handle_t structure
  • new_app_info: pointer to an allocated esp_app_desc_t structure

int esp_https_ota_get_image_len_read(esp_https_ota_handle_t https_ota_handle)

Structures

struct esp_https_ota_config_t

ESP HTTPS OTA configuration.

Public Members

const esp_http_client_config_t *http_config

ESP HTTP client configuration

Macros

ESP_ERR_HTTPS_OTA_BASE
ESP_ERR_HTTPS_OTA_IN_PROGRESS

Type Definitions

typedef void *esp_https_ota_handle_t