本文主要介绍宠物类设备 TuyaOS SDK 的初始化流程及接口说明,包括各阶段初始化的概念、初始化的流程,以及初始化相关的 API 介绍。
TuyaOS SDK 的初始化不是一个独立的接口,而是由一组接口组成,它们共同构成了宠物类设备的最小软件模型。按初始化阶段划分,可以把初始化分为以下几类:
开发宠物类设备必须使用 TuyaOS 初始化,流媒体功能与宠物相关功能可以按需初始化,初始化完成后就可以把硬件设备加入到 涂鸦开发者平台。
#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(/* 根据实际声明 */);
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);
//此处按需填充 path_opt、pet_config、pet_cb、mqtt_param 后调用:
tuya_pet_detect_init(&path_opt, &pet_config, pet_cb, &mqtt_param, NULL);
while (1)
sleep(1);
return 0;
}
#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;
#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
/**
* @brief Definition of all attache 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.
/**
* 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;
/* 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.
/* 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.
/**
* @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;
/**
* @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;
设备 IoT 事件的相关初始化,必须要在其他初始化前调用该接口。
/***********************************************************
*@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);
接口内部会对 TuyaOS 的内部基础功能分配存储资源、初始化。
/**
* @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)
日志打印等级设置。调试时设置为 TY_LOG_LEVEL_DEBUG 等级,正常版本可以设置 TY_LOG_LEVEL_NOTICE 等级。
/**
* @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);
设置涂鸦授权信息。TuyaOS 在激活时需要使用授权信息到涂鸦云进行设备认证,因此需要设置授权信息。授权信息里包含了产品信息、唯一标识和认证密钥。使用涂鸦产测软件进行产测授权可以不需要调用此接口,否则需要自行获取授权信息,并在初始化的时候写入设备。
/**
* @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);
宠物类设备初始化。配置初始化的配网模式 GW_WF_START_MODE 和 Wi-Fi 工作模式 GW_WF_CFG_MTHD_SEL,注册功能回调 TY_IOT_CBS_S ,添加设备 PID、固件版本和 MCU 的升级通道,实现配网激活,完成设备与涂鸦云服务的对接。
/**
* @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);
只有调用了该接口,才能正常的进行预览视频的开启和停止。
/**
* @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);
宠物档案管理、宠物录音下发相关初始化,可根据需要进行初始化。
const char * file_path = "/oem/tuya";
//算法相关配置结构体,如果客户使用自研算法,可传递自定义结构
TUYA_PET_DETECT_CONFIG_T pet_config = {0};
pet_config.detect_body_opt = true; // 检测宠物身体开关
pet_config.detect_face_opt = true; // 检测宠物脸开关
pet_config.track_opt = true; // 追踪开关
pet_config.feature_opt = true; // 特征提取开关
//算法结果通知回调和宠物档案操作结果通知回调
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;
//音频文件操作结果回调与文件同步回调
mqtt_param.status_cb = audio_status_cb;
mqtt_param.file_sync_cb = pet_file_sync_cb;
//模型文件、宠物图片、音频文件保存目录,客户按需配置
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); /* 客户自存路径,供 file_sync_cb/播放等使用 */
//路径初始化结构体
TUYA_PET_DETECT_PATH_T path_opt = {
.model_dir = s_model_dir,
.image_dir = s_image_dir,
.audio_dir = s_audio_dir,
};
//功能选择结构体,按需选择使能的功能
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 );
该内容对您有帮助吗?
是意见反馈该内容对您有帮助吗?
是意见反馈