OTA Service

[中文]

OTA service can upgrade firmware over the air (OTA). The firmware can be fetched from local files or the network.

Application Example

Implementation of this API is demonstrated in the following example:

Header File

Functions

periph_service_handle_t ota_service_create(ota_service_config_t *config)

Create the OTA service instance.

Return

  • NULL: Failed

  • Others: Success

Parameters
  • config: configuration of the OTA service

esp_err_t ota_service_set_upgrade_param(periph_service_handle_t handle, ota_upgrade_ops_t *list, int list_len)

Configure the upgrade parameter This function is not thread safe.

This function will set the parameter table to ota service, and the ota service will upgrade the partitions defined in the table one by one,

Return

  • ESP_OK: Success

  • Others: Failed

Parameters
  • [in] handle: pointer to ‘periph_service_handle_t’ structure

  • [in] list: pointer to ‘ota_upgrade_ops_t’ structure

  • [in] list_len: length of the ‘list’

Structures

struct ota_service_config_t

The OTA service configuration.

Public Members

int task_stack

>0 Service task stack; =0 with out task created

int task_prio

Service task priority (based on freeRTOS priority)

int task_core

Service task running in core (0 or 1)

periph_service_cb evt_cb

Service callback function

void *cb_ctx

Callback context

struct ota_node_attr_t

The OTA node attributions.

Public Members

esp_partition_type_t type

Partition type

char *label

Partition label

char *uri

The upgrade URL

char *cert_pem

SSL server certification, PEM format as string, if the client requires to verify server

struct ota_upgrade_ops_t

The upgrade operation.

Public Members

ota_node_attr_t node

The OTA node

ota_service_err_reason_t (*prepare)(void **handle, ota_node_attr_t *node)

Functions ready for upgrade

ota_service_err_reason_t (*need_upgrade)(void *handle, ota_node_attr_t *node)

Detect whether an upgrade is required

ota_service_err_reason_t (*execute_upgrade)(void *handle, ota_node_attr_t *node)

For execute upgrade

ota_service_err_reason_t (*finished_check)(void *handle, ota_node_attr_t *node, ota_service_err_reason_t result)

Check result of upgrade

bool reboot_flag

Reboot or not after upgrade

bool break_after_fail

Abort upgrade when got failed

struct ota_result_t

The result of the OTA upgrade.

Public Members

uint8_t id

The result ID

ota_service_err_reason_t result

The error reason

Macros

OTA_SERVICE_ERR_REASON_BASE
OTA_SERVICE_DEFAULT_CONFIG()

Enumerations

enum ota_service_event_type_t

The OTA service event type.

Values:

OTA_SERV_EVENT_TYPE_RESULT
OTA_SERV_EVENT_TYPE_FINISH
enum ota_service_err_reason_t

The OTA service error reasons.

Values:

OTA_SERV_ERR_REASON_UNKNOWN = ESP_FAIL
OTA_SERV_ERR_REASON_SUCCESS = ESP_OK
OTA_SERV_ERR_REASON_NULL_POINTER = OTA_SERVICE_ERR_REASON_BASE + 1
OTA_SERV_ERR_REASON_URL_PARSE_FAIL = OTA_SERVICE_ERR_REASON_BASE + 2
OTA_SERV_ERR_REASON_ERROR_VERSION = OTA_SERVICE_ERR_REASON_BASE + 3
OTA_SERV_ERR_REASON_NO_HIGHER_VERSION = OTA_SERVICE_ERR_REASON_BASE + 4
OTA_SERV_ERR_REASON_ERROR_MAGIC_WORD = OTA_SERVICE_ERR_REASON_BASE + 5
OTA_SERV_ERR_REASON_ERROR_PROJECT_NAME = OTA_SERVICE_ERR_REASON_BASE + 6
OTA_SERV_ERR_REASON_FILE_NOT_FOUND = OTA_SERVICE_ERR_REASON_BASE + 7
OTA_SERV_ERR_REASON_PARTITION_NOT_FOUND = OTA_SERVICE_ERR_REASON_BASE + 8
OTA_SERV_ERR_REASON_PARTITION_WT_FAIL = OTA_SERVICE_ERR_REASON_BASE + 9
OTA_SERV_ERR_REASON_PARTITION_RD_FAIL = OTA_SERVICE_ERR_REASON_BASE + 10
OTA_SERV_ERR_REASON_STREAM_INIT_FAIL = OTA_SERVICE_ERR_REASON_BASE + 11
OTA_SERV_ERR_REASON_STREAM_RD_FAIL = OTA_SERVICE_ERR_REASON_BASE + 12
OTA_SERV_ERR_REASON_GET_NEW_APP_DESC_FAIL = OTA_SERVICE_ERR_REASON_BASE + 13

Header File

Functions

void ota_app_get_default_proc(ota_upgrade_ops_t *ops)

get the default process of app partition upgrade

Return

  • void

Parameters

void ota_data_get_default_proc(ota_upgrade_ops_t *ops)

get the default process of data partition upgrade

Return

  • void

Parameters

ota_service_err_reason_t ota_data_image_stream_read(void *handle, char *buf, int wanted_size)

read from the stream of upgrading

Return

  • OTA_SERV_ERR_REASON_SUCCESS: Success

  • Others: Failed

Parameters
  • [in] handle: pointer to upgrade handle

  • [in] buf: pointer to receive buffer

  • [in] wanted_size: bytes to read

ota_service_err_reason_t ota_data_partition_write(void *handle, char *buf, int size)

write to the data partition under upgrading

Return

  • OTA_SERV_ERR_REASON_SUCCESS: Success

  • Others: Failed

Parameters
  • [in] handle: pointer to upgrade handle

  • [in] buf: pointer to data buffer

  • [in] size: bytes to write

void ota_data_partition_erase_mark(void *handle)

Indicates that the ota partition has been erased By default, this part of flash will be erased during ota. If the behavior of erasing is called in applition, this API needs to be called.

Return

  • void

Parameters
  • [in] handle: pointer to upgrade handle

int ota_get_version_number(char *version)

Convert string of version to integer The version should be (V0.0.0 - V255.255.255)

Return

  • -1: Failed

  • Others: version number

Parameters
  • [in] version: pointer to the string of version

Header File

Functions

esp_err_t esp_fs_ota(esp_fs_ota_config_t *ota_config)

Upgrade the firmware from filesystem.

Note

This API handles the entire OTA operation, so if this API is being used then no other APIs from esp_fs_ota component should be called. If more information and control is needed during the FS OTA process, then one can use esp_fs_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_fs_ota_begin(esp_fs_ota_config_t *ota_config, esp_fs_ota_handle_t *handle)

Start FS OTA Firmware upgrade.

This function initializes ESP FS OTA context and open the firmware file. This function must be invoked first. If this function returns successfully, then esp_fs_ota_perform should be called to continue with the OTA process and there should be a call to esp_fs_ota_finish on completion of OTA operation or on failure in subsequent operations.

Return

  • ESP_OK: FS OTA Firmware upgrade context initialised and file opened successful

  • ESP_FAIL: For generic failure.

  • ESP_ERR_INVALID_ARG: Invalid argument (missing/incorrect config, etc.)

  • For other return codes, refer documentation in app_update component and esp_http_client component in esp-idf.

Parameters
  • [in] ota_config: pointer to esp_fs_ota_config_t structure

  • [out] handle: pointer to an allocated data of type esp_fs_ota_handle_t which will be initialised in this function

esp_err_t esp_fs_ota_perform(esp_fs_ota_handle_t fs_ota_handle)

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

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

Return

  • ESP_ERR_FS_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
  • [in] fs_ota_handle: pointer to esp_fs_ota_handle_t structure

esp_err_t esp_fs_ota_finish(esp_fs_ota_handle_t fs_ota_handle)

Clean-up FS OTA Firmware upgrade and close the file stream.

This function closes the file stream and frees the ESP FS 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
  • [in] fs_ota_handle: pointer to esp_fs_ota_handle_t structure

esp_err_t esp_fs_ota_get_img_desc(esp_fs_ota_handle_t fs_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_fs_ota_begin() and before esp_fs_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
  • [in] fs_ota_handle: pointer to esp_fs_ota_handle_t structure

  • [out] new_app_info: pointer to an allocated esp_app_desc_t structure

int esp_fs_ota_get_image_len_read(esp_fs_ota_handle_t fs_ota_handle)

Structures

struct esp_fs_ota_config_t

ESP FS OTA configuration.

Public Members

const char *path

file path

const int buffer_size

Size of buffer

Macros

ESP_ERR_FS_OTA_BASE
ESP_ERR_FS_OTA_IN_PROGRESS

Type Definitions

typedef void *esp_fs_ota_handle_t