APIs and Examples

Last Updated on : 2022-11-24 09:20:29download

This topic describes the OTA update API and provides examples of how to update the MCU firmware using the API.

API

/**

 * Initialize the MCU update

 * @param  handle_cb Upgrade data callback function registration parameters

 * @param  pack_len  The length of data transferred to the user per pull packet swap

 * @return Function Operation Result  OPRT_OK is ok other is failed 

 */

OPERATE_RET tuya_svc_mcu_ota_serve_init(IN TY_OTA_CB_HANDLE_S handle_cb,IN UINT_T pack_len);



/**

 * Inform the MCU to upgrade and continue to pull packets, while choose OTA_PACK_WAIT

 * @return Function Operation Result  OPRT_OK is ok other is failed 

 */

OPERATE_RET tuya_svc_mcu_ota_wait_type_post(IN TY_OTA_POST_RESULT_E result);



/**

 * Start the MCU upgrade service, input parameters need to be obtained in the IoT specified callback

 * @param  p_ug_info Information about the upgraded file

 * @return Function Operation Result  OPRT_OK is ok other is failed 

 */

OPERATE_RET tuya_svc_mcu_ota_serve_start(IN CONST FW_UG_S *p_ug_info);



/**

 * Upgrade the packet processing callback function

 * Rereport the changed MCU version number after the system upgrade

 * HTTP report will start after 1S after the interface is used. If it fails, 5S retry

 * @param  p_mcu_ver Upgraded file information pointer

 * @return Function Operation Result  OPRT_OK is ok other is failed 

 */

OPERATE_RET tuya_svc_mcu_ota_ver_report_syn(IN CONST CHAR_T *p_mcu_ver);



/**

 * MCU upgrades subcontract length Settings

 * @param  pack_len  The length of data transferred to the user per pull packet swap,256,512,1024

 * @return Function Operation Result  OPRT_OK is ok other is failed 

 */

OPERATE_RET tuya_svc_mcu_ota_pack_len_set(IN UINT_T pack_len);

Example

OTA update process

  1. The mobile app notifies users of an available update. After the user confirms the update, the mobile app will send a message to the device for starting an update.

  2. After the device wakes up from sleep and is connected to the cloud, it will receive the update information through the callback dev_ug_inform_cb.

  3. The device starts to download the firmware update.

  4. After the update is installed, an update callback will be invoked.

  5. The MCU must instruct the module to restart after the update is completed and then reports its current firmware version number.

STATIC OPERATE_RET ty_uart_mcu_ota_start_cb(IN CONST FW_UG_S *p_ug_info,OUT TY_OTA_PACK_E *action_type);
STATIC OPERATE_RET ty_uart_mcu_ota_data_cb(IN CONST UCHAR_T *p_data, IN CONST UINT_T len,IN UINT_T offset, OUT TY_OTA_PACK_E *action_type);
STATIC OPERATE_RET ty_uart_mcu_ota_end_status_cb(IN TY_OTA_END_STATUS_E status,IN UINT_T offset);

STATIC TY_OTA_CB_HANDLE_S g_ota_msg = {\
   .iot_dev_ota_start_cb = ty_uart_mcu_ota_start_cb,\
   .iot_dev_ota_data_cb =  ty_uart_mcu_ota_data_cb,\
   .iot_dev_ota_end_sta_cb = ty_uart_mcu_ota_end_status_cb,\
};
STATIC OPERATE_RET ty_uart_mcu_ota_start_cb(IN CONST FW_UG_S *p_ug_info,OUT TY_OTA_PACK_E *action_type)
{
    OPERATE_RET op_ret = OPRT_OK;
    // A callabck invoked before the update.
    // todo
    *action_type = OTA_PACK_GO_ON;  // In case of `OTA_PACK_WAIT`, the component waits for `tuya_svc_mcu_ota_wait_type_post`.
    
    return op_ret;
}

STATIC OPERATE_RET ty_uart_mcu_ota_data_cb(IN CONST UCHAR_T *p_data, IN CONST UINT_T len,IN UINT_T offset, OUT TY_OTA_PACK_E *action_type)
{

    OPERATE_RET op_ret = OPRT_OK;
    //  Update downloading callback.
   // todo
    *action_type = OTA_PACK_GO_ON;  // In case of `OTA_PACK_WAIT`, the component waits for `tuya_svc_mcu_ota_wait_type_post` to control the download progress.

    return op_ret;
}

STATIC OPERATE_RET ty_uart_mcu_ota_end_status_cb(IN TY_OTA_END_STATUS_E status,IN UINT_T offset)
{
    OPERATE_RET op_ret = OPRT_OK;

    // A callback invoked after the update is installed.
   // todo

    return op_ret;
}
OPERATE_RET tuya_ota_service_init(VOID)
{
    OPERATE_RET op_ret = OPRT_OK;
    op_ret = tuya_svc_mcu_ota_serve_init(g_ota_msg,256);
    if(OPRT_OK != op_ret) {
        PR_ERR("svc_mcu_ota_serve_init err:%d",op_ret);
        return op_ret;
    }
}

// System upadate callback.
OPERATE_RET dev_ug_inform_cb(IN CONST FW_UG_S *fw)
{
    OPERATE_RET op_ret = OPRT_OK;
    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);
// Update the MCU firmware.
    op_ret = tuya_svc_mcu_ota_serve_start(fw);
    if(OPRT_OK != op_ret) {
        PR_ERR("tuya_svc_mcu_ota_serve_start err:%d",op_ret);
    }
    return op_ret;
}

OPERATE_RET tuya_uart_cloud_init(void)
{
    OPERATE_RET op_ret = OPRT_COM_ERROR;
    TY_IOT_CBS_S wf_cbs = {
        .gw_status_cb = status_changed_cb,\
        .gw_ug_cb = gw_ug_inform_cb,\
        .gw_reset_cb = dev_reset_cb,\
        .dev_obj_dp_cb = dev_obj_dp_cb,\
        .dev_raw_dp_cb = dev_raw_dp_cb,\
        .dev_dp_query_cb = dev_dp_query_cb,\
        .dev_ug_cb = dev_ug_inform_cb,  \
        .pre_gw_ug_cb = pre_gw_ug_inform_cb
    };

    op_ret = tuya_iot_wf_mcu_dev_init(GWCM_LOW_POWER,WF_START_SMART_AP_CONCURRENT,\
                                        &wf_cbs,firmware_key,product_id,firmware_version,mcu_sw_ver);

    if(OPRT_OK != op_ret) {
        PR_ERR("tuya iot wifi init faild ! err:%d",op_ret);
        return op_ret;
    }

  
    return OPRT_OK;
}
OPERATE_RET device_init(VOID)
{
    tuya_uart_cloud_init();
}