Device Initialization

Last Updated on : 2025-02-05 02:47:34download

This topic describes the initialization process of the TuyaOS Sweeper SDK in terms of initialization concepts and phases, and related APIs.

Background information

Initialization of the SDK is made up of a set of interfaces needed to build a minimal software model for a laser robot vacuum. Initialization can be broken down into the following parts by phase:

  • TuyaOS initialization: Initialize IoT basic services to support capabilities such as network pairing and activation, cloud connectivity, OTA updates, and control over MQTT, as well as reporting real-time maps, routes, floor maps, and cleaning history.
  • Streaming media SDK initialization: Initialize the basic services for audio and video capabilities. Streaming media SDK initialization is an optional feature. If you want to support audio and video capabilities, you need to initialize the SDK. For more information, see Audio and Video Capabilities.
  • Streaming service initialization: Start the streaming service to provide support for real-time map and route transmission.
  • Services initialization: Based on your own requirements, you can implement business logic such as data point (DP) control, collection and reporting of maps, routes, floor maps, and cleaning history.

To build a laser robot vacuum, TuyaOS initialization and streaming service initialization are required. After that, you can connect your hardware to the Tuya Developer Platform.

Initialization process

Initialize streaming media SDKInitialize the media abstraction layerty_rvc_media_adapter_initRegister network status callbacks tuya_iot_get_wf_nw_stat_cb()Initialize the ring bufferty_rvc_media_ring_buffer_initRegister audio and video event callbacksty_rvc_media_av_event_initInitialize robot vacuum eventsty_rvc_iot_initInitialize system servicetuya_iot_init_params()Set log leveltuya_iot_set_log_attr()Set authorization information tuya_iot_set_wf_gw_prod_infoInitialize the device tuya_iot_wf_dev_init()Initialize the device media servicety_rvc_server_initInitialize services

In the initialization process, the streaming media SDK initialization interface is optional. However, for other processes, you must make API requests in sequence. You cannot modify or skip APIs.

Example

#include "tuya_iot_com_api.h"
#include "tuya_iot_config.h"
#include "tuya_iot_wifi_api.h"
#include "tuya_wifi_netcfg.h"
#include "utilities/uni_log.h"
#define UUID                        "f2*************b0"
#define AUTHKEY                     "6P**************************MX"
#define PID                         "U0**************Zy"  
#define USER_SW_VER                 "1.0.0"
#define TY_MCU_VERSION              "1.0.1"
#define CFG_ONLINE_LOG_PATH         "./tmp/"
#define CFG_STORAGE_PATH            "./tuya_db_files/"

STATIC VOID __soc_dev_rev_upgrade_info_cb(IN CONST FW_UG_S *fw);// The entry to update an SoC device
STATIC VOID __soc_dev_status_changed_cb(IN CONST GW_STATUS_E status);// The callback invoked when the cloud status of an SoC device changes
STATIC VOID __soc_dev_dp_query_cb(IN CONST TY_DP_QUERY_S *dp_qry);// The entry to query specific data of an SoC device
STATIC VOID __soc_dev_obj_dp_cmd_cb(IN CONST TY_RECV_OBJ_DP_S *dp);// The entry to send object type command data to an SoC device
STATIC VOID __soc_dev_raw_dp_cmd_cb(IN CONST TY_RECV_RAW_DP_S *dp);// The entry to send raw command data to an SoC device
STATIC VOID __soc_dev_restart_req_cb(GW_RESET_TYPE_E type);// The entry to remove the pairing of an SoC device
STATIC VOID __soc_dev_net_status_cb(IN CONST GW_BASE_NW_STAT_T stat);// The callback invoked when the Wi-Fi network status changes
/**
 * @brief  The callback for real-time map and route transmission status
 * @param [in] onoff There are two events, 0: stop transmission, 1: start transmission
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
STATIC OPERATE_RET set_trans_event_cb(int onoff)
{
    OPERATE_RET ret = OPRT_OK;
    PR_DEBUG("Callback function called with: %d", onoff);
    return OPRT_OK;
}

int sample_main(int argc, char *argv[])
{
    int rt = OPRT_OK;
    rt = ty_rvc_iot_init(); // Initialize robot vacuum events
    if (rt != OPRT_OK) {
        PR_ERR("ty_rvc_iot_init err");
        return rt;
    }
    // This section is for the initialization of TuyaOS basic features. It allocates storage resources for TuyaOS and activates some of the most fundamental capabilities.
    TY_INIT_PARAMS_S init_param = { 0 };
    init_param.init_db = TRUE;
    strcpy(init_param.sys_env, TARGET_PLATFORM);
    strcpy(init_param.log_seq_path, CFG_ONLINE_LOG_PATH); // The path where SDK online logs are saved
    TUYA_CALL_ERR_LOG(tuya_iot_init_params(CFG_STORAGE_PATH    , &init_param)); // The path where SDK DB files are saved

    // This section is for writing the authorization information for TuyaOS. In official products, the authorization information is written during production testing.
    WF_GW_PROD_INFO_S prod_info = {UUID, AUTHKEY, NULL,NULL};
    TUYA_CALL_ERR_RETURN(tuya_iot_set_wf_gw_prod_info(&prod_info));
    // Set the log level
    tuya_iot_set_log_attr(TY_LOG_LEVEL_DEBUG); // Set the log printing level    
    // This section is for device initialization. Implement basic callback features to meet basic capability requirements of a device and register a callback function to get connection status.
    TY_IOT_CBS_S iot_cbs = {0};
    iot_cbs.gw_status_cb = __soc_dev_status_changed_cb;
    iot_cbs.gw_ug_cb = __soc_dev_rev_upgrade_info_cb;
    iot_cbs.gw_reset_cb = __soc_dev_restart_req_cb;
    iot_cbs.dev_obj_dp_cb = __soc_dev_obj_dp_cmd_cb;
    iot_cbs.dev_raw_dp_cb = __soc_dev_raw_dp_cmd_cb;
    iot_cbs.dev_dp_query_cb = __soc_dev_dp_query_cb;   

    /* The following code applies to dual firmware (main firmware and MCU firmware). */
    GW_ATTACH_ATTR_T arrt;
    arrt.tp = GP_DEV_MCU; // The MCU channel is fixed to 9.
    strcpy(arrt.ver, TY_MCU_VERSION);
    TUYA_CALL_ERR_RETURN(tuya_iot_wf_dev_init(GWCM_OLD_PROD, WF_START_AP_ONLY, &iot_cbs, NULL, PID,
    USER_SW_VER , DEV_NM_ATH_SNGL, &arrt, 1));
    TUYA_CALL_ERR_RETURN(tuya_iot_reg_get_wf_nw_stat_cb(__soc_dev_net_status_cb));  
    ret = ty_rvc_server_init(set_trans_event_cb); // Initialize the device media service
    if (ret != OPRT_OK) {
        PR_ERR("tuya media server init failed");
        return ret;
    }
    // This section is for initializing device application features after device initialization.
    while (1)
    {
        sleep(10);        
        // Monitor device status and print status prompts
        ty_devos_monitor();
    }

    return 0;
}

Data type

TY_INIT_PARAMS_S

#define SYS_ENV_LEN             20      // Max string length of ENV.
#define LOG_SEQ_PATH_LEN        128     //Max string length of LOG SEQ PATH.
/**
 * @brief Definition of TUYA DevOS init param
 */
typedef struct {
    /** kv init or not */
    BOOL_T init_db;
    /** sys env settings */
    CHAR_T sys_env[SYS_ENV_LEN];
    /** log seq path */
    CHAR_T log_seq_path[LOG_SEQ_PATH_LEN];
} TY_INIT_PARAMS_S;

Log level

#define TY_LOG_LEVEL_ERR                         0
#define TY_LOG_LEVEL_WARN                        1
#define TY_LOG_LEVEL_NOTICE                      2
#define TY_LOG_LEVEL_INFO                        3
#define TY_LOG_LEVEL_DEBUG                       4
#define TY_LOG_LEVEL_TRACE                       5

GW_PERMIT_DEV_TP_T

/**
 * @brief Definition of all attach types
 */
typedef BYTE_T GW_PERMIT_DEV_TP_T;
#define GP_DEV_DEF 0xFF                 // Default device type.
#define GP_DEV_ZB DEV_ZB_SNGL           // Zigbee
#define GP_DEV_BLE DEV_BLE_SNGL         // Bluetooth Low Energy
#define GP_DEV_MCU  DEV_NM_NOT_ATH_SNGL // MCU
#define GP_DEV_ATH_1 DEV_ATTACH_MOD_1   // attach 1
#define GP_DEV_ATH_2 DEV_ATTACH_MOD_2   // attach 2
#define GP_DEV_ATH_3 DEV_ATTACH_MOD_3   // attach 3
#define GP_DEV_ATH_4 DEV_ATTACH_MOD_4   // attach 4
#define GP_DEV_ATH_5 DEV_ATTACH_MOD_5   // attach 5
#define GP_DEV_ATH_6 DEV_ATTACH_MOD_6   // attach 6
#define GP_DEV_ATH_7 DEV_ATTACH_MOD_7   // attach 7
#define GP_DEV_ATH_8 DEV_ATTACH_MOD_8   // attach 8
#define GP_DEV_ATH_9 DEV_ATTACH_MOD_9   // attach 9
#define GP_DEV_ATH_10 DEV_ATTACH_MOD_10 // attach 10
#define GP_DEV_SUPPORT_MAX GP_DEV_ATH_10 // Max attach ID.

WF_GW_PROD_INFO_S

/**
 * Definition of Wi-Fi product info
 */
typedef struct {
    CHAR_T *uuid;       // strlen(uuid) <= 16, must not be null.
    CHAR_T *auth_key;   // strlen(auth_key) <= 32, must not be null.
    CHAR_T *ap_ssid;    // strlen(ap_ssid) <= 16, if ap_ssid is null, then the default ssid is Smartlife_xxxx.
    CHAR_T *ap_passwd;  // strlen(ap_passwd) <= 16, default value is null.
} WF_GW_PROD_INFO_S;

GW_WF_CFG_MTHD_SEL

/* tuya sdk definition of wifi work mode */
typedef BYTE_T GW_WF_CFG_MTHD_SEL;  // Wi-Fi configuration method selection.
#define GWCM_OLD                0   // Do not have low power mode.
#define GWCM_LOW_POWER          1   // With low power mode.
#define GWCM_SPCL_MODE          2   // Special with low power mode.
#define GWCM_OLD_PROD           3   // GWCM_OLD mode with product.
#define GWCM_LOW_POWER_AUTOCFG  4   // With low power mode && auto cfg.
#define GWCM_SPCL_AUTOCFG       5   // Special with low power mode && auto cfg.

GW_WF_START_MODE

/* tuya sdk definition of wifi start mode */
typedef BYTE_T GW_WF_START_MODE;
#define WF_START_AP_ONLY        0   // Only have ap-cfg mode.
#define WF_START_SMART_ONLY     1   // Only have smart-cfg mode.
#define WF_START_AP_FIRST       2   // Have both ap-cfg and smart-cfg. Default mode is ap-cfg .
#define WF_START_SMART_FIRST    3   // Have both ap-cfg and smart-cfg. Default mode is smart-cfg.
#define WF_START_SMART_AP_CONCURRENT    4   //  ap-cfg and smart-cfg is concurrent.

GW_ATTACH_ATTR_T

/**
 * @brief Definition of attach moudule attribute
 */
typedef struct {
    /** attach ota channel */
    GW_PERMIT_DEV_TP_T tp;
    /** attach version, format xx.xx.xx */
    CHAR_T ver[SW_VER_LEN + 1];
    CHAR_T md5[SW_MD5_LEN + 1];
} GW_ATTACH_ATTR_T;

TY_IOT_CBS_S

/**
 * @brief Definition of gateway callback funtions
 */
typedef struct {
    /** status update */
    GW_STATUS_CHANGED_CB gw_status_cb;
    /** gateway upgrade */
    GW_UG_INFORM_CB gw_ug_cb;
    /** gateway reset */
    GW_RESET_IFM_CB gw_reset_cb;
    /** structured DP info */
    DEV_OBJ_DP_CMD_CB dev_obj_dp_cb;
    /** raw DP info */
    DEV_RAW_DP_CMD_CB dev_raw_dp_cb;
    /** DP query */
    DEV_DP_QUERY_CB dev_dp_query_cb;
    /** sub-device upgrade */
    DEV_UG_INFORM_CB dev_ug_cb;
    /** sub-device reset */
    DEV_RESET_IFM_CB dev_reset_cb;
    /** active short url */
    ACTIVE_SHORTURL_CB active_shorturl;
    /** gateway upgrade pre-condition */
    GW_UG_INFORM_CB pre_gw_ug_cb;
    /** sub-device upgrade pre-condition */
    DEV_UG_INFORM_CB pre_dev_ug_cb;
} TY_IOT_CBS_S;

API description

Initialize robot vacuum events

Initialize the device IoT events. This is a required feature for a robot vacuum, and you must call this interface before other initializations.

/***********************************************************
 *@function: ty_rvc_iot_init  
 *@brief initialize tuya rvc event
 *@param[*] VOID
 *@return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
***********************************************************/
OPERATE_RET ty_rvc_iot_init(VOID);

Initialize system services

This interface allocates resources for TuyaOS basic services and performs initialization.

/**
 * @brief TuyaOS system service init
 *
 * @param[in] fs_storge_path: Storge path of KV database(Linux only)
 * @param[in] p_param: Extra init params, @see TY_INIT_PARAMS_S
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tuya_iot_init_params(IN CONST CHAR_T *fs_storge_path, IN CONST TY_INIT_PARAMS_S *p_param);
#define tuya_iot_init(fs_storge_path) \
    tuya_iot_init_params(fs_storge_path, NULL)

Set the log level

Set up the log printing level. Set the log level to TY_LOG_LEVEL_DEBUG in debug mode and TY_LOG_LEVEL_NOTICE in normal mode.

/**
 * @brief tuya_iot_set_log_attr
 *
 * @param[in] log_level
 *
 * @return OPERATE_RET
 */
OPERATE_RET tuya_iot_set_log_attr(IN CONST INT_T log_level);

Set authorization information

Set the Tuya authorization information. During the activation process, authorization information is required to authenticate the device with the Tuya IoT cloud. Therefore, you need to set the authorization information. The authorization information includes product information, a unique identifier, and an authentication key. You do not need to call this interface when using Tuya production test software for authorization. Otherwise, you need to get authorization information and write it into the device during initialization.

/**
* @brief tuya_iot_set_wf_gw_prod_info
*
* @param[in] wf_prod_info: device product info
*
* @return OPERATE_RET
*/
OPERATE_RET tuya_iot_set_wf_gw_prod_info(IN CONST WF_GW_PROD_INFO_S *wf_prod_info);

Initialize the device

Initialize the robot vacuum device. Configure the pairing mode GW_WF_START_MODE and the Wi-Fi working mode GW_WF_CFG_MTHD_SEL, register the functionality callback TY_IOT_CBS_S and add the device PID, firmware version, and MCU update channel. This enables device pairing, activation, and connection to the Tuya IoT cloud services.

/**
 * @brief tuya_iot_wf_dev_init
 *
 * @param[in] cfg: wifi device work mode
 * @param[in] start_mode: wifi netcfg mode
 * @param[in] cbs: tuya wifi sdk user callbacks
 * @param[in] firmware_key: the firmware key
 * @param[in] product_key: product key/proudct id,get from tuya open platform
 * @param[in] wf_sw_ver: wifi module software version format:xx.xx.xx (0<=x<=9)
 * @param[in] attr: the attach firmware of this device
 * @param[in] attr_num: the attach firmware number
 *
 * @return OPERATE_RET
 */
OPERATE_RET tuya_iot_wf_dev_init(IN CONST GW_WF_CFG_MTHD_SEL cfg, IN CONST GW_WF_START_MODE start_mode,
                                 IN CONST TY_IOT_CBS_S *cbs, IN CHAR_T *firmware_key,
                                 IN CHAR_T *product_key, IN CHAR_T *wf_sw_ver, IN CONST DEV_TYPE_T tp,
                                 IN GW_ATTACH_ATTR_T *attr, IN CONST UINT_T attr_num);

Initialize streaming media services

Real-time maps and routes can be transmitted as expected only if you call this interface.


/**
 * @brief real time map and path treans cb
 */
typedef INT_T (*RVC_TRANS_EVENT_CB)(IN CONST int onoff);

/***********************************************************
 *@Function: ty_rvc_server_init
 *@brief  initialize media server init
 *@param[in] handler The status callback for real-time data transmission, 
             with statuses including: start and end.
 *@return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 ***********************************************************/

OPERATE_RET ty_rvc_server_init(RVC_TRANS_EVENT_CB handler);

Parameter description

handler: The transmission status of real-time maps and routes. You can determine whether data reporting is started based on the status value.