Pet Device Initialization

Last Updated on : 2026-04-20 03:14:43Copy for LLMView as MarkdownDownload PDF

This topic describes the initialization process and interfaces of the TuyaOS SDK for pet devices in terms of initialization concepts and procedures, and related APIs.

Background information

Initialization of the TuyaOS SDK is made up of a set of interfaces needed to build a minimal software model for a pet device. Initialization consists of the following phases:

  • TuyaOS initialization: Initialize basic IoT services to support capabilities such as pairing and activation, cloud access, OTA updates.
  • Streaming media SDK initialization: Initialize the basic services for audio and video capabilities. Streaming media SDK initialization is optional. 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 audio and video control operations.
  • Pet event and video message initialization: Enable pet event reporting, allowing the device to upload pet-related events and video messages.
  • Business initialization: Based on your own requirements, you can implement business logic such as data point (DP) control, event reporting initialization, and pet service initialization.

To build a pet device, TuyaOS initialization is required. Streaming media and pet-related features can be initialized as needed. After that, you can connect your hardware to the Tuya Developer Platform.

Initialization process

TuyaOS initialization

ty_sys_start
ty_iot_sdk_init
ty_rvc_iot_init
tuya_iot_init_params
tuya_iot_set_log_attr
tuya_iot_set_wf_gw_prod_info
tuya_iot_wf_dev_init
tuya_iot_reg_get_wf_nw_stat_cb
ty_robot_media_sdk_init
ty_rvc_media_adapter_init
ty_rvc_media_ring_buffer_init
ty_rvc_media_av_event_init
ty_cmd_dp_init
ty_log_upload_init
Completed

Device initialization

Main Entry
ty_sys_start Initialize device SDK
ty_media_cloud_storage Initialize cloud storage
tuya_av_stream_init Initialize audio and video
tuya_ipc_event_add_alarm_types Add pet events
tuya_ipc_event_module_init Initialize event module
tuya_ipc_video_msg_init Initialize video messaging
ty_rvc_server_init Initialize media services
tuya_pet_detect_init Initialize pet SDK
Initialization completed

Example

#include "tuya_iot_config.h"
#include "tuya_ipc_notify.h"
#include "tuya_ipc_event.h"
#include "tuya_app_av_demo.h"
#include "tuya_app_pet_event_demo.h"
#include "tuya_pet_detect_api.h"
#include "ty_rvc_sdk_init.h"

extern void set_trans_event_cb(/* Adjust based on actual implementation */);

int main(int argc, char **argv)
{
    ty_sys_start(s_token);
    ty_media_cloud_storage(TRUE, 10);
    tuya_pet_event_deal();
    tuya_av_stream_init();

    TUYA_ALARM_BITMAP_T alarm_of_events[1] = { 0 };
    tuya_ipc_event_add_alarm_types(&alarm_of_events[0], E_ALARM_CAT);
    tuya_ipc_event_module_init(1, alarm_of_events, 1);
    tuya_ipc_video_msg_init(1, 10);

    ty_rvc_server_init(set_trans_event_cb);

    //Fill in path_opt, pet_config, pet_cb, mqtt_param as needed and then call:
    tuya_pet_detect_init(&path_opt, &pet_config, pet_cb, &mqtt_param, NULL);

    while (1)
        sleep(1);
    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 attached 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 Wi-Fi 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 Wi-Fi 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 module 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 functions
 */
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 pet device event

This interface initializes device IoT events. This API must be called before other initialization procedures.

/***********************************************************
 *@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 service

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

/**
 * @brief TuyaOS system service init
 *
 * @param[in] fs_storge_path: Storage 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 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. TuyaOS requires authorization data (Product ID, UUID, and Auth Key) for cloud authentication during activation. Skip this interface if using Tuya production test tools. Otherwise, you need to manually write the information 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 device

Initialize pet device. Configure the pairing mode GW_WF_START_MODE and the Wi-Fi working mode GW_WF_CFG_MTHD_SEL, register callback functions 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: Wi-Fi device work mode.
 * @param[in] start_mode: Wi-Fi netcfg mode.
 * @param[in] cbs: Tuya Wi-Fi SDK user callbacks.
 * @param[in] firmware_key: Firmware key.
 * @param[in] product_key: Product key/Product ID, get from tuya open platform.
 * @param[in] wf_sw_ver: Wi-Fi 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 service

Only after calling this interface can the preview video be started and stopped normally.

/**
 * @brief real-time map and path trans 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);

Initialize pet-related business

Initialize pet profile management and audio distribution services. This module can be configured as needed.

    const char * file_path = "/oem/tuya";

   // Algorithm configuration structure. If a proprietary algorithm is used, you can pass a user-defined configuration.
    TUYA_PET_DETECT_CONFIG_T pet_config = {0};
    pet_config.detect_body_opt = true; // Enable pet body detection
    pet_config.detect_face_opt = true; // Enable pet face detection
    pet_config.track_opt = true; // Enable tracking
    pet_config.feature_opt = true; // Enable feature extraction

    // Callbacks for algorithm results and pet profile operation results
    TUYA_PET_DETECT_CB_T pet_cb = {0};
    pet_cb.pet_detect_result_get_cb = tuya_pet_detect_result_cb;
    pet_cb.oper_result_cb = tuya_pet_detect_operate_result_cb;

    // Callbacks for audio file operation results and file synchronization
    mqtt_param.status_cb = audio_status_cb;
    mqtt_param.file_sync_cb = pet_file_sync_cb;

    // Storage directories for model files, pet images, and audio files. Configure as required.
    static char s_model_dir[TUYA_PET_DETECT_PATH_LEN];
    static char s_image_dir[TUYA_PET_DETECT_PATH_LEN];
    static char s_audio_dir[TUYA_PET_DETECT_PATH_LEN];
    snprintf(s_model_dir, sizeof(s_model_dir), "%s/model", file_path);
    snprintf(s_image_dir, sizeof(s_image_dir), "%s/face_image", file_path);
    snprintf(s_audio_dir, sizeof(s_audio_dir), "%s/audio", file_path);
    strncpy(s_pet_audio_dir, s_audio_dir, sizeof(s_pet_audio_dir) - 1);  /* User-defined storage path for audio files, used by file_sync_cb and playback */
    // Path configuration structure
    TUYA_PET_DETECT_PATH_T path_opt = {
        .model_dir = s_model_dir,
        .image_dir = s_image_dir,
        .audio_dir = s_audio_dir,
    };
    // Feature options. Enable features as required.
    TUYA_PET_DETECT_INIT_OPT_T init_opt = {
    need_pet_profile    = 1;
    need_audio_download  = 1;
    store_pet_face_image = 1;
    };
    tuya_pet_detect_init(&path_opt, &pet_config, pet_cb, &mqtt_param, &init_opt );