This topic describes the initialization process and interfaces of the TuyaOS SDK for boundaryless lawn mower in terms of initialization concepts and procedures, and related APIs.
Initialization of the TuyaOS SDK is made up of a set of interfaces needed to build a minimal software model for a boundaryless lawn mower. Initialization consists of the following phases:
To build a lawn mower, TuyaOS initialization is required. After that, you can connect your hardware to the Tuya Developer Platform.
Follow the API call sequence in the initialization process. Do not modify or skip calls at will.
#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 device firmware upgrade callback
STATIC VOID __soc_dev_status_changed_cb(IN CONST GW_STATUS_E status);// SOC device cloud status change callback
STATIC VOID __soc_dev_dp_query_cb(IN CONST TY_DP_QUERY_S *dp_qry);// SOC device DP query callback
STATIC VOID __soc_dev_obj_dp_cmd_cb(IN CONST TY_RECV_OBJ_DP_S *dp);// SOC device object DP command handling callback
STATIC VOID __soc_dev_raw_dp_cmd_cb(IN CONST TY_RECV_RAW_DP_S *dp);// SOC device raw DP command handling callback
STATIC VOID __soc_dev_restart_req_cb(GW_RESET_TYPE_E type);// SOC device reset and unpair callback
STATIC VOID __soc_dev_net_status_cb(IN CONST GW_BASE_NW_STAT_T stat);// SOC device Wi-Fi status change callback
int sample_main(int argc, char *argv[])
{
int rt = OPRT_OK;
rt = ty_rlm_iot_init(); // Initialize mowing robot events
if (rt != OPRT_OK) {
PR_ERR("ty_rlm_iot_init err");
return rt;
}
// This section is for the initialization of TuyaOS basic functions. It allocates storage resources for TuyaOS and enables essential 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 describes how to set the authorization information for TuyaOS. For mass-produced devices, set this information 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 level
// This section is for device initialization. It implements basic callback functions to meet the basic capability requirements of a device, and registers a callback function for obtaining the 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));
// This section is for initializing device application functions after device initialization.
while (1)
{
sleep(10);
// Monitor device status and print status prompts
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 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.
/**
* 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 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.
/* 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.
/**
* @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;
/**
* @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;
This interface initializes device IoT events. This is a required function for boundaryless lawn mowers, and you must call this interface before other initializations.
/***********************************************************
*@function: ty_rlm_iot_init
*@brief initialize tuya rlm event
*@param[*] VOID
*@return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
***********************************************************/
OPERATE_RET ty_rlm_iot_init(VOID);
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 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 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 boundaryless lawn mower initialization. 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: 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);
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback