Skip to main content

tkl_ota | OTA Firmware Update

The tkl_ota API provides the kernel-layer (TKL) hooks for an over-the-air (OTA) firmware update. The update runs in stages: query the device OTA capability, signal the start, process each data packet, and signal the end. A separate call retrieves the previous firmware's information for resumable transfers. Every function returns OPRT_OK on success or an error code defined in tuya_error_code.h.

The tkl_ota.c implementation is generated by the porting tool. You add platform-specific code in the user-defined areas so it survives regeneration.

tkl_ota_get_abilityโ€‹

OPERATE_RET tkl_ota_get_ability(uint32_t *image_size, TUYA_OTA_TYPE_E *type);

Gets the device's OTA capability: the maximum firmware image size it can receive and the OTA type it supports.

ParameterDirectionDescription
image_sizeoutMaximum firmware image size the device accepts.
typeoutSupported OTA type. See TUYA_OTA_TYPE_E.

Returns OPRT_OK on success. Other values indicate an error; see tuya_error_code.h.

tkl_ota_start_notifyโ€‹

OPERATE_RET tkl_ota_start_notify(uint32_t image_size, TUYA_OTA_TYPE_E type, TUYA_OTA_PATH_E path);

Notifies the platform that an OTA update is starting. The implementation initializes the variables and state needed for the transfer.

ParameterDirectionDescription
image_sizeinSize of the firmware image to be written.
typeinOTA type. See TUYA_OTA_TYPE_E.
pathinTransport channel the data arrives on. See TUYA_OTA_PATH_E.

Returns OPRT_OK on success. Other values indicate an error; see tuya_error_code.h.

tkl_ota_data_processโ€‹

OPERATE_RET tkl_ota_data_process(TUYA_OTA_DATA_T *pack, uint32_t *remain_len);

Processes one received OTA data packet, writing its payload to Flash.

ParameterDirectionDescription
packinPointer to the OTA data packet. See TUYA_OTA_DATA_T.
remain_lenoutNumber of bytes in the packet that were not yet processed.

Returns OPRT_OK on success. Other values indicate an error; see tuya_error_code.h.

tkl_ota_end_notifyโ€‹

OPERATE_RET tkl_ota_end_notify(BOOL_T reset);

Notifies the platform that the OTA transfer has ended. The implementation verifies the received image and performs any post-processing, optionally resetting the device.

ParameterDirectionDescription
resetinWhether to reset the device after the update completes.

Returns OPRT_OK on success. If verification fails or another error occurs, an error code is returned; see tuya_error_code.h.

tkl_ota_get_old_firmware_infoโ€‹

OPERATE_RET tkl_ota_get_old_firmware_info(TUYA_OTA_FIRMWARE_INFO_T **info);

Gets information about the current firmware. This is used to resume an interrupted transfer at a break-point.

note

This API is used only for BLE sub-devices.

ParameterDirectionDescription
infooutReceives a pointer to the firmware information structure. See TUYA_OTA_FIRMWARE_INFO_T.

Returns OPRT_OK on success. Other values indicate an error; see tuya_error_code.h.

Typesโ€‹

TUYA_OTA_TYPE_Eโ€‹

OTA package type.

typedef enum {
TUYA_OTA_FULL = 1, ///< AB area switch, full package upgrade
TUYA_OTA_DIFF = 2, ///< fixed area, difference package upgrade
} TUYA_OTA_TYPE_E;

TUYA_OTA_PATH_Eโ€‹

Transport channel the OTA data arrives on.

typedef enum {
TUYA_OTA_PATH_AIR = 0, ///< OTA from Wired/Wi-Fi/Cellular/NBIoT
TUYA_OTA_PATH_UART = 1, ///< OTA from uart for MF
TUYA_OTA_PATH_BLE = 2, ///< OTA from BLE protocol for subdev
TUYA_OTA_PATH_ZIGBEE = 3, ///< OTA from Zigbee protocol for subdev
TUYA_OTA_PATH_SEC_A = 4, ///< OTA from multi-section A
TUYA_OTA_PATH_SEC_B = 5, ///< OTA from multi-section B
TUYA_OTA_PATH_INVALID = 255 ///< OTA from multi-section invalid
} TUYA_OTA_PATH_E;

TUYA_OTA_DATA_Tโ€‹

One OTA packet. The implementation writes the payload to Flash at start_addr + offset.

typedef struct {
uint32_t total_len; ///< ota image total len
uint32_t start_addr; ///< ota flash start addr
uint32_t offset; ///< ota image offset
uint8_t *data; ///< ota data
uint32_t len; ///< ota data len
void *pri_data; ///< private pointer
} TUYA_OTA_DATA_T;

TUYA_OTA_FIRMWARE_INFO_Tโ€‹

Describes a firmware image.

typedef struct {
uint32_t len;
uint32_t crc32;
uint8_t md5[TUYA_OTA_FILE_MD5_LEN];
} TUYA_OTA_FIRMWARE_INFO_T;

TUYA_OTA_FILE_MD5_LEN is 16.