更新时间:2025-01-24 05:49:53下载pdf
本文主要介绍扫地机 TuyaOS SDK 的初始化流程及接口说明,包括各阶段初始化的概念、初始化的流程,以及初始化相关的 API 介绍。
TuyaOS SDK 的初始化不是一个独立的接口,而是由一组接口组成,它们共同构成了激光扫地机的最小软件模型。按初始化阶段划分,可以把初始化分为以下几类:
开发激光扫地机必须使用 TuyaOS 初始化 和 流媒体服务初始化,初始化完成后就可以把硬件设备加入到 涂鸦开发者平台。
初始化流程中除了 流媒体 SDK 初始化 接口可根据开发者选配外,其他的流程必须遵循初始化流程中的接口调用顺序,不可随意修改或者不去调用。
#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);// SOC 设备升级入口
STATIC VOID __soc_dev_status_changed_cb(IN CONST GW_STATUS_E status);// SOC 设备云端状态变更回调
STATIC VOID __soc_dev_dp_query_cb(IN CONST TY_DP_QUERY_S *dp_qry);// SOC 设备特定数据查询入口
STATIC VOID __soc_dev_obj_dp_cmd_cb(IN CONST TY_RECV_OBJ_DP_S *dp);// SOC 设备obj DP指令数据下发入口
STATIC VOID __soc_dev_raw_dp_cmd_cb(IN CONST TY_RECV_RAW_DP_S *dp);// SOC 设备raw DP指令数据下发入口
STATIC VOID __soc_dev_restart_req_cb(GW_RESET_TYPE_E type);// SOC 设备移除配网入口
STATIC VOID __soc_dev_net_status_cb(IN CONST GW_BASE_NW_STAT_T stat);// SOC Wi-Fi状态变动回调
/**
* @brief 实时地图及路径传输状态回调
* @param [in] onoff 一共两种事件,0 表示停止传输,1 表示开始传输
* @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(); //机器人相关事件初始化
if (rt != OPRT_OK) {
PR_ERR("ty_rvc_iot_init err");
return rt;
}
// 此处为涂鸦 TuyaOS 基础功能初始化,为 TuyaOS 分配存储资源,并启动一些最基础的能力
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); // SDK 在线日志保存的路径
TUYA_CALL_ERR_LOG(tuya_iot_init_params(CFG_STORAGE_PATH , &init_param)); // SDK DB 文件存储路径
// 此处为涂鸦 TuyaOS 授权信息写入,正式产品是在生产测试的时候写入授权信息
WF_GW_PROD_INFO_S prod_info = {UUID, AUTHKEY, NULL,NULL};
TUYA_CALL_ERR_RETURN(tuya_iot_set_wf_gw_prod_info(&prod_info));
//设置日志等级
tuya_iot_set_log_attr(TY_LOG_LEVEL_DEBUG); // 设置打印级别
// 此处为设备初始化,实现了一些基础的回调功能,满足一个设备的基本能力需求,并注册获取连接状态回调函数
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;
/*以下代码是双固件(主固件 + MCU 固件)的情况下使用*/
GW_ATTACH_ATTR_T arrt;
arrt.tp = GP_DEV_MCU; // MCU 通道固定为 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); // 设备媒体服务初始化
if (ret != OPRT_OK) {
PR_ERR("tuya media server init failed");
return ret;
}
// 此处为设备初始化后的一些设备应用功能初始化
while (1)
{
sleep(10);
// 监控设备状态,打印状态提示
ty_devos_monitor();
}
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);
参数说明
handler
:实时地图及路径传输状态,开发者可根据状态值来判断是否开始上报数据。
该内容对您有帮助吗?
是意见反馈该内容对您有帮助吗?
是意见反馈