OTA APIs and Examples

Last Updated on : 2023-01-29 07:31:53

If you use the custom solution for development, you can update the chips that are unable to access the internet via OTA. You can take such chips as the subordinate devices to the network modules. When a network module collaborates with an MCU, the MCU is subordinate to the module. This topic describes how to implement an OTA update on the subordinate device through the network module.

In the Tuya IoT SDK, a callback struct processes OTA updates, network status changes, and reporting and receiving data point (DP) data.

tuya_iot_wf_soc_dev_init_param() is used to register the callback struct. Implementing OTA updates on subordinate devices depend on this struct.

The following code snippet shows the function prototype for the callback struct:

typedef struct {
    GW_STATUS_CHANGED_CB gw_status_cb;
    GW_UG_INFORM_CB gw_ug_cb;
    GW_RESET_IFM_CB gw_reset_cb;
    DEV_OBJ_DP_CMD_CB dev_obj_dp_cb;
    DEV_RAW_DP_CMD_CB dev_raw_dp_cb;
    DEV_DP_QUERY_CB dev_dp_query_cb;
    DEV_UG_INFORM_CB dev_ug_cb;
    DEV_RESET_IFM_CB dev_reset_cb;
#if defined(TUYA_GW_OPERATOR) && (TUYA_GW_OPERATOR==1)
    OPE_HTTPC_GET_CHCODE_CB ope_get_chcode_cb;
#endif
#if defined(ENABLE_ALARM) && (ENABLE_ALARM==1)
    GW_OFFLINE_DP_SAVE gw_offline_dp_save_cb;
#endif
#if defined(QRCODE_ACTIVE_MODE) && (QRCODE_ACTIVE_MODE==1)
    ACTIVE_SHORTURL_CB active_shorturl;
#endif
    GW_UG_INFORM_CB pre_gw_ug_cb;
    DEV_UG_INFORM_CB pre_dev_ug_cb;
}TY_IOT_CBS_S;

GW_UG_INFORM_CB gw_ug_cb; processes an OTA update on a subordinate device. The sample code shows that tuya_iot_upgrade_gw() is invoked to register get_file_data_cb() and upgrade_notify_cb().

/**
* @brief ota inform callback
*
* @param[in] fw: firmware info
* @return
*/
INT_T gw_ug_inform_cb(IN CONST FW_UG_S *fw)
{
    PR_DEBUG("Rev GW Upgrade Info");
    PR_DEBUG("fw->fw_url:%s", fw->fw_url);
    PR_DEBUG("fw->sw_ver:%s", fw->sw_ver);
    PR_DEBUG("fw->file_size:%d", fw->file_size);

    return tuya_iot_upgrade_gw(fw, get_file_data_cb, upgrade_notify_cb, NULL);
}

Download firmware update file

The callback is used to download the firmware update file for the subordinate device.

OPERATE_RET get_file_data_cb(IN CONST FW_UG_S *fw, IN CONST UINT_T total_len, IN CONST UINT_T offset,
                                IN CONST BYTE_T *data, IN CONST UINT_T len, OUT UINT_T *remain_len, IN PVOID_T pri_data);

Parameters:

  • fw: Firmware information.
  • total_len: The total length of the firmware.
  • offset: The offset of the current packet.
  • data: The data of the current packet.
  • len: The length of the current packet.
  • remain_len: The remaining length.
  • pri_data: The private data.

Return:

  • OPRT_OK: Success
  • Others: Failure

The following code snippet shows the firmware information struct.

typedef struct {
    DEV_TYPE_T tp;      // firmware type
    UPGRADE_TYPE_T type;
    CHAR_T fw_url[FW_URL_LEN+1];  // firmware download url
    CHAR_T sw_ver[SW_VER_LEN+1];  // firmware version
    UINT_T file_size;             // firmware size in BYTE
    CHAR_T fw_hmac[FW_HMAC_LEN+1];  // firmware hmac
#if defined(ENABLE_IPC) && (ENABLE_IPC != 0)
    CHAR_T fw_md5[FW_MD5_LEN+1];  // firmware md5
#endif
    BOOL_T diff_ota;
}FW_UG_S;

tp represents the firmware channel. It varies depending on the firmware type. Channels 0 to 9 are reserved for Tuya use. Channels 10 to 19 can be defined by you.

Download completes

The callback is invoked when the firmware download completes.

VOID upgrade_notify_cb(IN CONST FW_UG_S *fw, IN CONST INT_T download_result, IN PVOID_T pri_data);

Parameters:

  • fw: Firmware information.
  • download_result: 0 indicates success. Other values indicate failure.
  • pri_data: The private data.

Return:

  • None

Deploy OTA update on platform

To deploy an OTA update, you need to upload the firmware to the Tuya IoT Development Platform.

See how to upload firmware to the Tuya IoT Development Platform.

See how to deploy an OTA update on the Tuya IoT Development Platform.

Sample application

The firmware data received from the Tuya IoT Development Platform is printed to the log. Channel 9 represents the firmware channel specific to the MCU. Chips of different types are assigned a unique channel to distinguish different subordinate devices because multiple devices can be subordinate to one network module.

For example, a Wi-Fi module integrated into a smart multimode gateway has two subordinates, a Bluetooth chip and a Zigbee chip.

The example implements OTA updates for the MCU that is subordinate to the Wi-Fi module. Tuya IoT Development Platform only passes the firmware file to the device. The features such as encryption and verification are implemented by you.

Check out the complete code on GitHub.

OTA APIs and Examples