Link SDK Access

Version History

Version

Compilation / Revision Instructions

Revision Person

Revision Date

Remarks

1.0.0

Create document

Liu Bo

20200327

Overview

Tuya Gateway Networking SDK is a software middleware. It can directly establish communication links with Tuya Cloud and Tuya APP by means of the networking capabilities of the device, and carry out Tuya standard data interaction. Users use this SDK to develop gateway products connected to Tuya Cloud.

  • Gateway products

The gateway networking SDK and different protocol controllers are used to make the product into a physical gateway product, and the gateway is connected to various products with different communication protocols, such as: Zigbee, BLE, ZWAVE, etc. For example: common control centers and smart hosts in smart homes. The gateway provides gateway firmware, gateway controller, and firmware upgrade capabilities for sub-terminal devices.

  • Gateway and equipment products

In addition to the control of gateway devices for sub-devices, it also serves as a device product to define the function points of the device and realize the control of the gateway device.

Prerequisites

Before development, users need to register Tuya IoT platform account to obtain the necessary information in the device development stage, including product ID, authorization code, etc.specific operation instructions, please refer toDocument Center.If you have any questions, please contact Tuya Business.

Hardware Requirements

  • Flash: SDK code takes up about 2MB. And there is a readable and writable area.

  • RAM: After the SDK runs, the memory occupies 5MB. The user should increase the memory according to the functional complexity of the developed product.

Software Requirements

  • Support LINUX or ANDROID system.

  • Support TCP / IP protocol stack.

SDK Acquisition

Tuya Gateway Distribution Network SDK is provided to users in the form of C language dynamic link library (.so) or static library (.a). Therefore, users need to provide their system's cross-compilation tool chain to package SDK.

At the same time,Tuya Gateway Distribution Network SDK will also be regularly uploaded to GitHub.If the compiled product already contains the user's cross-compilation tool chain, you can download it directly.

Directory Structure

.
├── build_app.sh
├── CHANGELOG.md
├── demos
├── platforms
└── sdk
    ├── include
    └── lib
        ├── libtuya_iot.a

For the directory structure and usage, please refer to the README.md file in the product package.

SDK Initialization

Equipment Certification

  • The device's identity authentication adopts the one-machine-one-password method to program the device's unique uuid & authkey on the device. This method requires certain modifications to the device's production line tools. uuid & authkey.

  • uuid & authkey appear in pairs, each device must have its own uuid & authkey, and unique, please apply to the Tuya project manager for several groups for debugging.

  • Device authentication message is set via interface tuya_iot_set_gw_prod_info .

Note: The PID in the demo, uuid & authkey are only used for testing, not for actual products, otherwise the subsequent products will be unavailable. PID requires users to apply from the Tuya Developer Platform. Each device must have its own uuid & authkey and be unique.

Initialize The Interface Explanation

tuya_iot_init

OPERATE_RET tuya_iot_init(IN CONST CHAR_T *fs_storge_path)

Features Explanation( Summary )

For the initialization of the tuya iot system, it must be called first.

Parameter Explanation( Parameters )

Parameter Name

Explanation

fs_storge_path

Assign a readable and writable path to the SDK. The path length cannot be greater than 110 bytes.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Success

Error Code

Fail return Error Code

tuya_iot_get_sdk_info

CHAR_T *tuya_iot_get_sdk_info(VOID);

FeaturesExplanation( Summary )

Obtain the TUYA networke SDK version information.

Parameter Explanation( Parameters )

Parameter Name

Explanation

VOID

\

Return Value(Return Values)

Return Value

Explanation

SDK Message String

Including SDK compile time, platform, version number, enabled features, etc.

TY_IoT_CBS_S

typedef struct {
    GW_STATUS_CHANGED_CB gw_status_cb;
    GW_UG_INFORM_CB gw_ug_cb;
    GW_RESET_IFM_CB gw_reset_cb;
    DEV_OBJ_DP_CMD_CB dev_obj_dp_cb;
    DEV_RAW_DP_CMD_CB dev_raw_dp_cb;
    DEV_DP_QUERY_CB dev_dp_query_cb;
    DEV_UG_INFORM_CB dev_ug_cb;
    DEV_RESET_IFM_CB dev_reset_cb;
#if defined(TUYA_GW_OPERATOR) && (TUYA_GW_OPERATOR==1)
    OPE_HTTPC_GET_CHCODE_CB ope_get_chcode_cb;
#endif
#if defined(ENABLE_ALARM) && (ENABLE_ALARM==1)
    GW_OFFLINE_DP_SAVE gw_offline_dp_save_cb;
#endif
} TY_IoT_CBS_S;

Features Explanation(Summary)

SDK user callback interface

Member Explanation

Member name

Explanation

gw_status_cb

Gateway status callback.

gw_ug_cb

Reference gw_ug_cb

gw_reset_cb

Reference gw_reset_cb

dev_obj_dp_cb

Reference dev_obj_dp_cb

dev_raw_dp_cb

Reference dev_raw_dp_cb

dev_dp_query_cb

dp point query callback

dev_ug_cb

Reference dev_ug_cb

dev_reset_cb

Reference dev_reset_cb

Wi-Fi Configuration device interface,Reference "tuya_iot_Wi-Fi_api.h"

WF_GW_PROD_INFO_S

typedef struct {
    CHAR_T *uuid; 
    CHAR_T *auth_key;
    CHAR_T *ap_ssid;
    CHAR_T *ap_passwd;
} WF_GW_PROD_INFO_S;

Features Explanation( Summary )

Wi-Fi product information.

Member Explanation

Member name

Explanation

uuid

uuid string. Must not be NULL. The length is less than or equal to 16 bytes.

auth_key

auth_key string. Must not be NULL. The length is less than or equal to 32 bytes.

ap_ssid

SSID prefix. The length is less than or equal to 16 bytes. If set to NULL, the ssid prefix is "Smartlife-" by default.

ap_passwd

The length is less than or equal to 16 bytes. The default is NULL.

tuya_iot_set_wf_gw_prod_info

OPERATE_RET tuya_iot_set_wf_gw_prod_info(IN CONST WF_GW_PROD_INFO_S *wf_prod_info);

Features Explanation( Summary )

Set the authorization information of the configuration Wi-Fi device. The authorization information needs to be obtained through TUYA, otherwise the device cannot be used normally.

parameter Explanation( Parameters )

Parameter Name

Explanation

wf_prod_info

Gateway product information. Reference WF_GW_PROD_INFO_S Explanation。

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_iot_wf_gw_init

OPERATE_RET tuya_iot_wf_gw_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 CONST TY_IoT_GW_CBS_S *gw_cbs, 
                                IN CONST CHAR_T *product_key, 
                                IN CONST CHAR_T *wf_sw_ver, 
                                IN CONST GW_ATTACH_ATTR_T *attr, 
                                IN CONST UINT_T attr_num);

FeaturesExplanation( Summary )

Wi-Fi gateway initialization interface.

parameter Explanation( Parameters )

Parameter Name

Explanation

cfg

Wi-Fi configuration method

start_mode

Wi-Fi distribution mode

cbs

Callback registered by the user.Reference TY_IoT_CBS_S

gw_cbs

Gateway callback registered by the user.Reference TY_IoT_GW_CBS_S

product_key

Obtained when the platform creates a product

wf_sw_ver

Firmware version number

attr

Gateway adapter configuration list

attr_num

Number of gateway adapter configuration lists

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Success

Error Code

Fail return Error Code

tuya_iot_wf_gw_dev_init

OPERATE_RET tuya_iot_wf_gw_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 CONST TY_IoT_GW_CBS_S *gw_cbs, 
                                    IN CONST CHAR_T *product_key, 
                                    IN CONST CHAR_T *wf_sw_ver, 
                                    IN CONST GW_ATTACH_ATTR_T *attr, 
                                    IN CONST UINT_T attr_num);

FeaturesExplanation( Summary )

Wi-Fi gateway plus device initialization interface, compared with A tuya_iot_wf_gw_init ,This function makes the gateway have the attributes of the device, and can set the features point, etc.

parameter Explanation( Parameters )

Refer to tuya_iot_wf_gw_init interface explanation.

Return Value(Return Values)

Refer to tuya_iot_wf_gw_init interface explanation.

tuya_iot_reg_get_wf_nw_stat_cb

OPERATE_RET tuya_iot_reg_get_wf_nw_stat_cb(IN CONST GET_WF_NW_STAT_CB nw_stat_cb);

FeaturesExplanation( Summary )

Get Wi-Fi status interface.

parameter Explanation( Parameters )

Parameter Name

Explanation

nw_stat_cb

Register network status callback function

The callback function nw_stat_cb parameter is of type GW_WI-FI_NW_STAT_E and is defined as follows:

#define STAT_LOW_POWER          0   // idle status,use to external config network
#define STAT_UNPROVISION        1   // smart config status
#define STAT_AP_STA_UNCFG       2   // ap WI-FI config status
#define STAT_AP_STA_DISC        3   // ap WI-FI already config,station disconnect
#define STAT_AP_STA_CONN        4   // ap station mode,station connect
#define STAT_STA_DISC           5   // only station mode,disconnect
#define STAT_STA_CONN           6   // station mode connect
#define STAT_CLOUD_CONN         7   // cloud connect
#define STAT_AP_CLOUD_CONN      8   // cloud connect and ap start 

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

Non-Wi-Fi configuration device interface,Reference "tuya_iot_base_api.h"

GW_PROD_INFO_S

typedef struct {
    CHAR_T *uuid; 
    CHAR_T *auth_key;
} GW_PROD_INFO_S;

FeaturesExplanation( Summary )

Gateway product information.

Member Explanation

Member name

Explanation

uuid

uuid string. Must not be NULL.

auth_key

auth_key string. Must not be NULL.

tuya_iot_set_gw_prod_info

OPERATE_RET tuya_iot_set_gw_prod_info(IN CONST GW_PROD_INFO_S *prod_info);

Features Explanation( Summary )

Set the authorization information of the non-Wi-Fi configuration networking module. The authorization information needs to be obtained through TUYA, otherwise the device cannot be used normally.

parameter Explanation( Parameters )

Parameter Name

Explanation

prod_info

product information. Reference GW_PROD_INFO_S

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_iot_gw_init

OPERATE_RET tuya_iot_gw_init(IN CONST TY_IoT_CBS_S *cbs, 
                             IN CONST TY_IoT_GW_CBS_S *gw_cbs, 
                             IN CONST CHAR_T *product_key,
                             IN CONST CHAR_T *sw_ver, 
                             IN CONST GW_ATTACH_ATTR_T *attr, 
                             IN CONST UINT_T attr_num);

FeaturesExplanation( Summary )

Networking module gateway initialization interface.

parameter Explanation( Parameters )

Parameter Name

Explanation

cbs

The user needs to register the callback function array.Reference TY_IoT_CBS_S

gw_cbs

The user needs to register the callback function array of gateway.Reference TY_IoT_GW_CBS_S

product_key

The product ID of the product when it was created on the platform.

sw_ver

The firmware version number.

attr

Gateway adapter configuration list.

attr_num

The number of gateway adapter configuration lists.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_iot_gw_dev_init

OPERATE_RET tuya_iot_gw_dev_init(IN CONST TY_IoT_CBS_S *cbs, 
                                 IN CONST TY_IoT_GW_CBS_S *gw_cbs, 
                                 IN CONST CHAR_T *product_key, 
                                 IN CONST CHAR_T *sw_ver, 
                                 IN CONST GW_ATTACH_ATTR_T *attr, 
                                 IN CONST UINT_T attr_num);

FeaturesExplanation

The networking module gateway plus the device initialization interface. Compared with A, this function makes the gateway have the attributes of the device, and you can set the features point.

parameter Explanation( Parameters )

Refer to tuya_iot_gw_init interface explanation

Return Value(Return Values)

Refer to tuya_iot_gw_init interface explanation

tuya_iot_reg_get_nw_stat_cb

OPERATE_RET tuya_iot_reg_get_nw_stat_cb(IN CONST GET_NW_STAT_CB nw_stat_cb);

FeaturesExplanation( Summary )

Register the network network status acquisition function.In the registered callback function,In the registered callback function, the user can obtain the current network status information.

parameter Explanation( Parameters )

Parameter Name

Explanation

nw_stat_cb

Register the network change callback function

In the registered callback function, the user knows the current network status according to the callback function parameter. The network status GW_BASE_NW_STAT_T is defined as follows:

/* offline in LAN.  user wired callback <hwl_bnw_station_conn> return <false> */
#define GB_STAT_LAN_UNCONN 0

/* online in LAN, offline in WAN.
   user wired callback <hwl_bnw_station_conn> return <true> but mqtt is offline
*/
#define GB_STAT_LAN_CONN 1

/* online in WAN.
   user wired callback <hwl_bnw_station_conn> return <true> and mqtt is online
*/
#define GB_STAT_CLOUD_CONN 2

Reference tuya_cloud_base_defs.h

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

Wired and Wi-Fi configuration interface,reference "tuya_iot_wired_Wi-Fi_api.h"

tuya_iot_set_wired_Wi-Fi_gw_prod_info

tuya_iot_set_wired_Wi-Fi_gw_prod_info(IN CONST WF_GW_PROD_INFO_S *wf_prod_info);

Parameter explanation reference tuya_iot_set_wf_gw_prod_info function section.

tuya_iot_wired_Wi-Fi_gw_init

OPERATE_RET tuya_iot_wired_Wi-Fi_gw_init(IN CONST IoT_GW_NET_TYPE_T net_mode, 
                                        IN CONST GW_WF_CFG_MTHD_SEL cfg, 
                                        IN CONST GW_WF_START_MODE start_mode,
                                        IN CONST TY_IoT_CBS_S *cbs, 
                                        IN CONST TY_IoT_GW_CBS_S *gw_cbs,
                                        IN CONST CHAR_T *product_key, 
                                        IN CONST CHAR_T *wf_sw_ver,
                                        IN CONST GW_ATTACH_ATTR_T *attr, 
                                        IN CONST UINT_T attr_num);

FeaturesExplanation( Summary )

Wired + Wi-Fi gateway initialization interface.

parameter Explanation( Parameters )

Parameter Name

Explanation

net_mode

Network Type

cfg

Wi-Fi configuration method

start_mode

Wi-Fi distribution mode

cbs

The user needs to register the callback function array.Reference TY_IoT_CBS_S

gw_cbs

Array of gateway callback functions that users need to register.Reference TY_IoT_GW_CBS_S

product_key

Obtained when the platform is created

wf_sw_ver

Firmware version number

attr

Gateway adapter configuration list

attr_num

Number of gateway adapter configuration lists

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_iot_wired_Wi-Fi_gw_dev_init

OPERATE_RET tuya_iot_wired_Wi-Fi_gw_dev_init(IN CONST IoT_GW_NET_TYPE_T net_mode, 
                                            IN CONST GW_WF_CFG_MTHD_SEL cfg, 
                                            IN CONST GW_WF_START_MODE start_mode, 
                                            IN CONST TY_IoT_CBS_S *cbs, 
                                            IN CONST TY_IoT_GW_CBS_S *gw_cbs, 
                                            IN CONST CHAR_T *product_key, 
                                            IN CONST CHAR_T *wf_sw_ver, 
                                            IN CONST GW_ATTACH_ATTR_T *attr, 
                                            IN CONST UINT_T attr_num);

Features Explanation( Summary )

Wired + Wi-Fi gateway (the gateway also acts as a device) initializes the interface.

parameter Explanation( Parameters )

Parameter Name

Explanation

net_mode

Network Type

cfg

Wi-Fi configuration method

sw_ver

Device firmware version string, format :xx.xx.xx

cbs

The user needs to register the callback function array.Reference TY_IoT_CBS_S

gw_cbs

Gateway callback

product_key

Array of gateway callback functions that users need to register.Reference TY_IoT_GW_CBS_S

wf_sw_ver

Firmware version number

attr

Gateway adapter configuration list

attr_num

Number of gateway adapter configuration lists

IoT_GW_NET_TYPE_T definition :

#define IoT_GW_NET_WIRED       0   //only support wried
#define IoT_GW_NET_WI-FI        1   //only support Wi-Fi
#define IoT_GW_NET_WIRED_WI-FI  2   //support wired and Wi-Fi

GW_WF_CFG_MTHD_SEL definition:

#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

In Linux / Android, select GWCM_OLD_PROD.

GW_WF_START_MODE definition:

#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 is ap-cfg mode
#define WF_START_SMART_FIRST    3   // have both ap-cfg and smart-cfg. default is smart-cfg mode
#define WF_START_SMART_AP_CONCURRENT    4   //  ap-cfg and smart-cfg is concurrent

parameter attr the user sets the adapter that he supports.For example, the gateway supports zigbee, it can be set to:

GW_ATTACH_ATTR_T attr[] = {
    {GP_DEV_ZB,"1.0.0"},
};

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_iot_reg_get_wired_Wi-Fi_nw_stat_cb

OPERATE_RET tuya_iot_reg_get_wired_Wi-Fi_nw_stat_cb(nw_stat_cb, wf_nw_stat_cb);

Features Explanation( Summary )

Register callback function for network status management.

parameter Explanation( Parameters )

Parameter Name

Explanation

nw_stat_cb

Register callback function for network status change

wf_nw_stat_cb

Register callback function for Wi-Fi network status change

Referance tuya_iot_reg_get_nw_stat_cb and tuya_iot_reg_get_wf_nw_stat_cb introduction of callback function.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_iot_gw_wired_Wi-Fi_set_net_name

VOID tuya_iot_gw_wired_Wi-Fi_set_net_name(IN CONST CHAR_T *wired_net_name, IN CONST CHAR_T *Wi-Fi_net_name);

Features Explanation

Set up wired and Wi-Fi network interfaces.

parameter Explanation( Parameters )

Parameter Name

Explanation

wired_net_name

Wired network interface name

Wi-Fi_net_name

Wi-Fi interface network name

Return Value(Return Values)

Return Value

Explanation

VOID

\

Equipment Distribution Network

Explanation:tuya_SDK will enable multi-threading, and the entire process must be restarted to enter the distribution mode to take effect.

Because there are many platforms that TUYA SDK takes into account, it is impossible to take into account the hardware details of all networking modules.Therefore, for the inside of the SDK, a set of hardware interfaces related to networked devices needs to be abstracted.Divided into two categories according to product form: Wi-Fi configuration network interface, non-Wi-Fi configuration network interface, specific ExplanationReference demo code.

Wired Equipment Distribution Network

The device is connected to the Internet through a wire, without entering the hotspot name and password of the router, the device is connected to the external network; just obtain the activation token from the APP side, you can apply to activate the device in TUYA cloud.

Reset The Device (enter the network to be configured)

tuya_iot_gw_unactive

OPERATE_RET tuya_iot_gw_unactive(VOID);

FeaturesExplanation( Summary )

Reset the gateway and release the binding relationship between the gateway and APP, so that the gateway is in an inactive state (network to be configured).

parameter Explanation( Parameters )

Parameter Name

Explanation

VOID

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_iot_wired_Wi-Fi_gw_unactive

OPERATE_RET tuya_iot_wired_Wi-Fi_gw_unactive(VOID)

Explanation refer tuya_iot_gw_unactive

Interactive Diagram of Distribution Process

  • As shown above, after resetting on the device, you need to restart the SDK process before you can enter the network configuration mode.

  • dev_reset_cb refer to common problem Explanation

User-implemented Interface

tuya_hal_wired_get_ip

OPERATE_RET tuya_hal_wired_get_ip(OUT NW_IP_S *ip)

FeaturesExplanation( Summary )

Obtain the IP address of the wired network card

parameter Explanation( Parameters )

Parameter Name

Explanation

ip

IP information structure.Network card IP address, subnet mask, gateway.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_wired_station_conn

BOOL_T tuya_hal_wired_station_conn(VOID);

FeaturesExplanation( Summary )

Get the status of the network connection to the external network.

parameter Explanation( Parameters )

Parameter Name

Explanation

VOID

Return Value(Return Values)

Return Value

Explanation

TRUE

The device is online

FALSE

The device is disconnected from the network.

tuya_hal_wired_get_mac

OPERATE_RET tuya_hal_wired_get_mac(OUT NW_MAC_S *mac)

FeaturesExplanation( Summary )

Get the MAC address of the wired network interface. Optional implementation.

parameter Explanation( Parameters )

Parameter Name

Explanation

mac

The MAC information of the obtained network interface.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Get success

Error Code

Get failed

hwl_bnw_set_mac

OPERATE_RET tuya_hal_wired_set_mac(IN CONST NW_MAC_S *mac);

FeaturesExplanation( Summary )

Set the MAC address of the wired network card. There is currently no need for implementation.

parameter Explanation( Parameters )

Parameter Name

Explanation

mac

Set the MAC address of the wired network card.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_wired_Wi-Fi_set_station_connect

OPERATE_RET tuya_hal_wired_Wi-Fi_set_station_connect(IN CONST CHAR_T *ssid, 
                                                    IN CONST CHAR_T *passwd)

FeaturesExplanation( Summary )

When the product form is wired + wireless mode, users need to realize the function of Wi-Fi connection.

parameter Explanation( Parameters )

Parameter Name

Explanation

ssid

The ssid of the Wi-Fi router.

password

The password of the Wi-Fi router.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_wired_Wi-Fi_need_cfg

BOOL_T tuya_hal_wired_Wi-Fi_need_cfg(VOID);

FeaturesExplanation( Summary )

If the hardware form is Wi-Fi + wired mode, do users need to connect to external Wi-Fi configuration?

parameter Explanation( Parameters )

Parameter Name

Explanation

VOID

Return Value(Return Values)

Return Value

Explanation

TRUE

If the hardware has a Wi-Fi interface, the user wants to connect to Wi-Fi.

FALSE

If the hardware does not have a Wi-Fi interface, you need to return FALSE if you do not want to connect to Wi-Fi.

tuya_hal_wired_Wi-Fi_station_get_conn_ap_rssi

OPERATE_RET tuya_hal_wired_Wi-Fi_station_get_conn_ap_rssi(OUT SCHAR_T *rssi)

FeaturesExplanation( Summary )

When the product form is wired + wireless, this interface is used to obtain the rssi value of Wi-Fi.

parameter Explanation( Parameters )

Parameter Name

Explanation

rssi

rssi saves the obtained rssi information.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Get Success

Other

Get failed

tuya_hal_wired_if_connect_internet

int tuya_hal_wired_if_connect_internet(bool *status);

FeaturesExplanation( Summary )

Get the network status.

parameter Explanation( Parameters )

Parameter Name

Explanation

status

network status. Networking is TRUE, otherwise it is FALSE.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Get success

Other

Get failed

Wi-Fi Equipment Distribution Network

  • The user needs to determine the working mode supported by the Wi-Fi network card first, and then inform the SDK of the supported network configuration mode when the tuya_iot_wf_xx_init interface is initialized.Specific reference tuya_iot_wf_xx_dev_init interface like cfg parameter Explanation

  • The SDK supports EZ distribution network and AP distribution mode.EZ is also called fast network configuration. It means that the Wi-Fi of the device is in monitor mode to obtain the wireless air interface packets sent by the APP.The SDK will parse these messages, obtain configuration information from it, and switch to station mode for networking.The AP network configuration process is relatively complicated, which means that the device turns on the AP hotspot, the mobile phone is connected, and the configuration information is sent through the local area network. After the device obtains the configuration information, it switches to the station mode for networking.

Reset The Device (enter network configuration mode)

  • As shown above, after resetting the device locally, you need to restart the SDK process before you can enter the network distribution mode.

  • dev_reset_cb refer tocommon problem Explanation;

  • When the configuration supports both AP and EZ network configuration modes during initialization, calling this interface will switch the network distribution mode cyclically;

tuya_iot_wf_gw_unactive

OPERATE_RET tuya_iot_wf_gw_unactive(VOID);

FeaturesExplanation( Summary )

Reset the gateway to release the binding relationship between the gateway sub-devices, so that the gateway is in the inactive (network to be configured) state.

parameter Explanation( Parameters )

Parameter Name

Explanation

VOID

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_iot_wired_Wi-Fi_gw_unactive

OPERATE_RET tuya_iot_wired_Wi-Fi_gw_unactive(VOID);

FeaturesExplanation( Summary )

Wired + Wi-Fi reset. same as tuya_iot_wf_gw_unactive

tuya_iot_wf_fast_get_nc_type

OPERATE_RET tuya_iot_wf_fast_get_nc_type(GW_WF_NWC_FAST_STAT_T *nc_type)

FeaturesExplanation( Summary )

Get the current Wi-Fi configuration method.

parameter Explanation( Parameters )

Parameter Name

Explanation

nc_type

GW_WF_NWC_FAST_STAT_T the definition is as follows:

#define GWNS_FAST_LOWPOWER           0   // current in low power mode
#define GWNS_FAST_UNCFG_SMC          1   // current in smart-cfg mode
#define GWNS_FAST_UNCFG_AP           2   // current in ap-cfg mode
#define GWNS_FAST_UNCFG_NORMAL       3   // current in normal mode

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_iot_wired_Wi-Fi_fast_get_nc_type

OPERATE_RET tuya_iot_wired_Wi-Fi_fast_get_nc_type(GW_WF_NWC_FAST_STAT_T *nc_type);

FeaturesExplanation( Summary )

Get the current Wi-Fi configuration mode in wired + Wi-Fi mode. Parameter explanation same as tuya_iot_wf_fast_get_nc_type

tuya_iot_set_wf_cfg_err_code_cb

OPERATE_RET tuya_iot_set_wf_cfg_err_code_cb(IN CONST WF_NW_CFG_ERR_CODE_CB wf_nw_cfg_err_code_cb);

FeaturesExplanation( Summary )

Registration callback function --- used to get the error code generated by Wi-Fi distribution.

parameter Explanation( Parameters )

Parameter Name

Explanation

wf_nw_cfg_err_code_cb

Callback function for obtaining error code generated by Wi-Fi distribution

The parameter of the callback function is the error code generated by the Wi-Fi distribution network. The definition is as follows:

/*Initial state*/
#define NW_CFG_INIT      			0
/*Activation fails*/
#define NW_CFG_ACTIVE_FAILED 		1
/*ap not found*/
#define NW_CFG_AP_NOT_FOUND 		2
/*wrong password*/
#define NW_CFG_ERR_PASSWD 			3
/*ap can't connect*/
#define NW_CFG_CANT_CONN_AP 		4
/*dhcp error*/
#define NW_CFG_DHCP_FAILED 			5
/*Router connected successfully*/
#define NW_CFG_SUCC  				100

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

AP distribution network

  1. Implement the interface that the user needs

  2. After the device is reset, make the SDK enter the network to be configured state

tuya_iot_set_user_def_ap_if

OPERATE_RET tuya_iot_set_user_def_ap_if(IN CONST CHAR_T *ssid,IN CONST CHAR_T *passwd)

FeaturesExplanation( Summary )

User-defined SSID prefix and password. If not set, the SSID prefix defaults to "SmartLife-", the password is blank, and the encryption method is open.

parameter Explanation( Parameters )

Parameter Name

Explanation

ssid

Custom SSID prefix

passwd

AP password

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

WF_AP_CFG_IF_S

typedef struct {
    uint8_t ssid[WI-FI_SSID_LEN+1];       ///< ssid
    uint8_t s_len;                       ///< len of ssid
    uint8_t passwd[WI-FI_PASSWD_LEN+1];   ///< passwd
    uint8_t p_len;                       ///< len of passwd
    uint8_t chan;                        ///< channel. default:0
    WF_AP_AUTH_MODE_E md;                ///< encryption type
    uint8_t ssid_hidden;                 ///< ssid hidden  default:0
    uint8_t max_conn;                    ///< max sta connect nums default:0
    uint16_t ms_interval;                ///< broadcast interval default:0
} WF_AP_CFG_IF_S;

FeaturesExplanation(Summary)

AP configuration structure.

Member Explanation

Member name

Explanation

ssid

ssid

s_len

ssid length

passwd

password

p_len

password length

chan

Wi-Fi working channel

md

Wi-Fi encryption type

ssid_hidden

hidden SSID

max_conn

Maximum number of station equipment connections

ms_interval

broadcast interval

User-implemented Interface

tuya_hal_Wi-Fi_get_ip

OPERATE_RET tuya_hal_Wi-Fi_get_ip(IN CONST WF_IF_E wf,OUT NW_IP_S *ip)

FeaturesExplanation( Summary )

Obtain the IP address of the Wi-Fi network card.

parameter Explanation( Parameters )

Parameter Name

Explanation

wf

The working type of the Wi-Fi network card.

ip

Save the obtained IP information structure, the information contains IP, submask, gateway.

Type definition of WF_IF_E:

typedef enum
{
    WF_STATION = 0,     // station type; station type
    WF_AP,              // ap type; AP type
} WF_IF_E;

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_get_mac

OPERATE_RET tuya_hal_Wi-Fi_get_mac(IN CONST WF_IF_E wf,INOUT NW_MAC_S *mac)

FeaturesExplanation( Summary )

Get the MAC address of the Wi-Fi network card.

parameter Explanation( Parameters )

Parameter Name

Explanation

wf

Working type of Wi-Fi network card.

mac

Save the obtained MAC address.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_set_mac

OPERATE_RET tuya_hal_Wi-Fi_set_mac(IN CONST WF_IF_E wf,IN CONST NW_MAC_S *mac)

FeaturesExplanation( Summary )

Set the MAC address of the Wi-Fi device. There is currently no need for implementation.

parameter Explanation( Parameters )

Parameter Name

Explanation

wf

The working type of the Wi-Fi network card.

mac

The set MAC address.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_set_work_mode

OPERATE_RET tuya_hal_Wi-Fi_set_work_mode(IN CONST WF_WK_MD_E mode)

FeaturesExplanation( Summary )

Set Wi-Fi working mode.

parameter Explanation( Parameters )

Parameter Name

Explanation

wf

Set the Wi-Fi device working mode.

The working mode of Wi-Fi is defined as follows:

typedef enum
{
    WWM_LOWPOWER = 0,   ///< Wi-Fi work in lowpower mode. The LINUX system does not care about low power consumption and does not need to be dealt with.
    WWM_SNIFFER,        ///< Wi-Fi work in sniffer mode
    WWM_STATION,        ///< Wi-Fi work in station mode
    WWM_SOFTAP,         ///< Wi-Fi work in ap mode
    WWM_STATIONAP,      ///< Wi-Fi work in station+ap mode
}WF_WK_MD_E;

Need to deal with WWM_SNIFFER and WWM_STATION (or WWM_STATIONAP) in EZ distribution network。

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_get_work_mode

OPERATE_RET tuya_hal_Wi-Fi_get_work_mode(OUT WF_WK_MD_E *mode)

FeaturesExplanation( Summary )

Get Wi-Fi working mode.

parameter Explanation( Parameters )

Parameter Name

Explanation

mode

It has the same meaning as the parameter mode in hwl_wf_wk_mode_set.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_station_connect

OPERATE_RET tuya_hal_Wi-Fi_station_connect(IN CONST CHAR_T *ssid,IN CONST CHAR_T *passwd)

FeaturesExplanation( Summary )

Wi-Fi devices connect to the router based on ssid & password.

parameter Explanation( Parameters )

Parameter Name

Explanation

ssid

The ssid connected to the router.

password

Connect the password of the router.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_station_disconnect

Function prototype

OPERATE_RET tuya_hal_Wi-Fi_station_disconnect(VOID)

FeaturesExplanation

Disconnect the Wi-Fi device and router.

parameter Explanation

Return Value

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_station_get_conn_ap_rssi

OPERATE_RET tuya_hal_Wi-Fi_station_get_conn_ap_rssi(OUT SCHAR_T *rssi)

FeaturesExplanation( Summary )

Get the signal strength of the Wi-Fi device.

parameter Explanation( Parameters )

Parameter Name

Explanation

rssi

Obtained rssi.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_station_get_status

OPERATE_RET tuya_hal_Wi-Fi_station_get_status(OUT WF_STATION_STAT_E *stat)

FeaturesExplanation( Summary )

Get the network status of the Wi-Fi device.

parameter Explanation( Parameters )

Parameter Name

Explanation

stat

User needs to fill in statistics.Reference WF_STATION_STAT_E Explanation。

WF_STATION_STAT_E Explanation:

typedef enum {
    WSS_IDLE = 0,                       ///< not connected;  
    WSS_CONNECTING,                     ///< connecting Wi-Fi; 
    WSS_PASSWD_WRONG,                   ///< passwd not match; 
    WSS_NO_AP_FOUND,                    ///< ap is not found;
    WSS_CONN_FAIL,                      ///< connect fail; 
    WSS_CONN_SUCCESS,                   ///< connect Wi-Fi success; 
    WSS_GOT_IP,                         ///< get ip success;
} WF_STATION_STAT_E;

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_set_country_code

OPERATE_RET tuya_hal_Wi-Fi_set_country_code(IN CONST CHAR_T *p_country_code)

FeaturesExplanation( Summary )

Set the country code where the Wi-Fi device works.

parameter Explanation( Parameters )

Parameter Name

Explanation

p_country_code

Wi-Fi devices work in different countries, with different frequencies and channel signal strengths.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_ap_start

OPERATE_RET tuya_hal_Wi-Fi_ap_start(IN CONST WF_AP_CFG_IF_S *cfg)

FeaturesExplanation( Summary )

Start the Wi-Fi AP hotspot.

parameter Explanation( Parameters )

Parameter Name

Explanation

cfg

Set the AP according to the passed function "parameter cfg".Reference WF_AP_CFG_IF_S

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_ap_stop

OPERATE_RET tuya_hal_Wi-Fi_ap_stop(VOID)

FeaturesExplanation( Summary )

Stop hotspots in AP configuration mode.

parameter Explanation( Parameters )

Parameter Name

Explanation

VOID

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

EZ Distribution Network

The Interface that the User Needs to Implement (EZ distribution network)

tuya_hal_Wi-Fi_all_ap_scan

OPERATE_RET tuya_hal_Wi-Fi_all_ap_scan(OUT AP_IF_S **ap_ary,OUT UINT_T *num)

FeaturesExplanation( Summary )

Get AP list information in the current environment.

parameter Explanation( Parameters )

Parameter Name

Explanation

ap_ary

List of AP information in the current environment.

num

The length of the AP information list in the current environment.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_assign_ap_scan

OPERATE_RET tuya_hal_Wi-Fi_assign_ap_scan(IN CONST CHAR_T *ssid,OUT AP_IF_S **ap)

FeaturesExplanation( Summary )

Get the AP information of the specified SSID.

parameter Explanation( Parameters )

Parameter Name

Explanation

ssid

The ssid of the specified AP.

ap

Get the specified AP information.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_release_ap

OPERATE_RET tuya_hal_Wi-Fi_release_ap(IN AP_IF_S *ap)

FeaturesExplanation( Summary )

Release processing of allocated resources.

parameter Explanation( Parameters )

Parameter Name

Explanation

ap

Release the content applied here: tuya_hal_Wi-Fi_all_ap_scan and tuya_hal_Wi-Fi_assign_ap_scan

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_set_cur_channel

OPERATE_RET tuya_hal_Wi-Fi_set_cur_channel(IN CONST BYTE_T chan)

FeaturesExplanation( Summary )

Set the Wi-Fi working channel.

parameter Explanation( Parameters )

Parameter Name

Explanation

chan

Channels that require Wi-Fi equipment to work.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_get_cur_channel

OPERATE_RET tuya_hal_Wi-Fi_get_cur_channel(OUT BYTE_T *chan)

FeaturesExplanation( Summary )

Get the current working channel.

parameter Explanation( Parameters )

Parameter Name

Explanation

chan

Fill the working channel of the current Wi-Fi device.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_hal_Wi-Fi_sniffer_set

OPERATE_RET tuya_hal_Wi-Fi_sniffer_set(IN CONST bool en,IN CONST SNIFFER_CALLBACK cb)

FeaturesExplanation( Summary )

Set the packet capture status of the Wi-Fi device in sniffer mode.

parameter Explanation( Parameters )

Parameter Name

Explanation

en

Enable / disable Wi-Fi sniffer mode.

cb

Notify callback function. The captured air 802.11 data contains the frame header.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

Other distribution methods

  • The user layer of such distribution equipment obtains the ssid, passwd and distribution token of the router through other methods, and directly calls the interface to apply for device activation.

  • Two-dimensional code distribution network, the device scans the two-dimensional code generated on the APP through the camera to obtain ssid / passwd / token information.

  • Bluetooth distribution network, the device obtains the ssid / passwd / token information through the QR code on the APP via Bluetooth.

  • Serial port, sonic distribution network, etc.

tuya_iot_gw_wf_user_cfg

OPERATE_RET tuya_iot_gw_wf_user_cfg(IN CONST CHAR_T *ssid, IN CONST CHAR_T *passwd,IN CONST CHAR_T *token);

FeaturesExplanation( Summary )

When the network is configured in other modes (not AP / EZ), such as the camera's QR code distribution network, acoustic wave distribution network, etc., call this interface for processing.

parameter Explanation( Parameters )

Parameter Name

Explanation

ssid

The ssid used by the network in AP mode.

passwd

The password used for network configuration in AP mode.

token

Token used in distribution network in AP mode.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

Gateway Reset

APP Reset Geteway

The user can reset the gateway through the APP.

gw_reset_cb

VOID gw_reset_cb(GW_RESET_TYPE_E type);

FeaturesExplanation( Summary )

The user registers the callback function gw_reset_cb in [TY_IoT_CBS_S] (# TY_IoT_CBS_S) .

parameter Explanation( Parameters )

Parameter Name

Explanation

type

GW_RESET_TYPE_E definition:

typedef enum {
    GW_LOCAL_RESET_FACTORY = 0,
    GW_REMOTE_UNACTIVE,
    GW_LOCAL_UNACTIVE,
    GW_REMOTE_RESET_FACTORY,
    GW_RESET_DATA_FACTORY, //need clear local data when active
}GW_RESET_TYPE_E;

Currently, the GW_REMOTE_RESET_FACTORY and GW_RESET_DATA_FACTORY reset types need to be handled in the gateway callback. The gateway application needs to be restarted after reset.

Return Value(Return Values)

Return Value

Explanation

VOID

Local reset gateway

The SDK retains the ability to reset the gateway locally, and users can reset the gateway locally by pressing buttons.Reference tuya_iot_gw_unactive and tuya_iot_wf_gw_unactive .

Device firmware upgrade (OTA)

  • Firmware upgrade is mainly used to repair the device BUG and add new functions of the device. There are two main types of firmware upgrade, the first is device upgrade, and the second is MCU upgrade. The firmware upgrade of Wi-Fi devices here refers to the device upgrade.

  • The device upgrade process involves mobile APP, device, and TUYA cloud terminal.

    • Mobile APP: The display of the upgrade progress result or the initiator of the upgrade message.

    • TUYA cloud: The manager in the upgrade process is responsible for the storage of the upgraded firmware, the update of the device upgrade status, and the push of the upgrade copy.

    • Device: Responsible for receiving firmware. Performer of firmware upgrade.

Firmware package configuration instructions

  • After the network configuration of the gateway or sub-device is successful, obtain the virtual ID in the device information from the APP as a whitelist for firmware upgrade.

  • Compile the firmware to be upgraded, the firmware version is higher than the firmware version running in the device.

  • Log in to the developer platform https://iot.tuya.com/index , and upload the configuration firmware package to the corresponding created product.

  • Instructions:https://docs.tuya.com/cn/product/ota.html

How the upgrade begins

After the device firmware is uploaded to the cloud, the device will not immediately receive the upgrade message. Currently TUYA supports the following methods:

  • APP reminder upgrade: When users open the device panel for the first time, they will receive an upgrade reminder pop-up box, they can choose to upgrade or not to upgrade.

  • APP silent upgrade: That is, the device is silently upgraded. After the device is restarted, it will request the cloud to check whether there is a silent upgrade task, and if so, directly upgrade; if the user opens the device panel, a progress box will be displayed at this time. The device is inoperable at this time.

  • APP forced upgrade: When the user opens the device panel for the first time, he will receive an upgrade reminder pop-up box. Only the confirmation is optional, and the responsible device cannot be operated.

  • APP detection upgrade: APP users actively click on the panel of the corresponding device, then click on the upper right corner to enter the device information interface, detect the device firmware version, and actively update.

Data interaction diagram

  • Explanation:The upgrade progress reported in the following figure is reported by tuya_SDK, and can also be configured to be reported by the application layer through tuya_iot_upgrade_gw_notify.

Relevant interface and callback description

FW_UG_S

typedef struct {
    DEV_TYPE_T tp;
    UPGRADE_TYPE_T type;
    CHAR_T fw_url[FW_URL_LEN+1];
    CHAR_T sw_ver[SW_VER_LEN+1];
    UINT_T file_size;
    CHAR_T fw_hmac[FW_HMAC_LEN+1];
} FW_UG_S;

FeaturesExplanation(Summary)

Upgraded firmware information.

Member Explanation

Member name

Explanation

tp

Firmware type

type

UPGRADE_TYPE_NORMAL : Ordinary upgrade UPGRADE_TYPE_SILENT : Forced upgrade

fw_url

URL of firmware download

sw_ver

Firmware version number

file_size

Firmware size

fw_hmac

Firmware HMAC check code

gw_ug_cb

VOID gw_ug_cb(IN CONST FW_UG_S *fw)

FeaturesExplanation( Summary )

The user registers the callback function gw_ug_cb in [TY_IoT_CBS_S] (# TY_IoT_CBS_S). The gateway has a new firmware notification callback.

parameter Explanation( Parameters )

Parameter Name

Explanation

fw

Firmware information.Reference FW_UG_S

Return Value(Return Values)

Return Value

Explanation

VOID

tuya_iot_upgrade_gw

OPERATE_RET tuya_iot_upgrade_gw_notify(IN CONST FW_UG_S *fw, 
                                       IN CONST GET_FILE_DATA_CB get_file_cb, 
                                       IN CONST UPGRADE_NOTIFY_CB upgrd_nofity_cb, 
                                       IN CONST PVOID_T pri_data, 
                                       BOOL_T notify, 
                                       UINT_T download_buf_size);

FeaturesExplanation( Summary )

Networking module firmware upgrade processing interface.

parameter Explanation( Parameters )

Parameter Name

Explanation

fw

Firmware information

get_file_cb

Callback function for download content storage

upgrd_nofity_cb

Callback function to notify the upgrade status of the application

pri_data

Parameters passed to get_file_cb and upgrd_nofity_cb

notify

Choose whether to report upgrade progress by SDK

download_buf_size

Download the maximum cache, in bytes

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Success

Error Code

Fail return Error Code

The application layer reports the progress of the upgrade

When the progress of the application layer control upgrade is reported, please use tuya_iot_upgrade_gw_notify to close the tuya_SDK and report the upgrade progress.

tuya_iot_dev_upgd_progress_rept

OPERATE_RET tuya_iot_dev_upgd_progress_rept(IN CONST UINT_T percent, 
                                            IN CONST CHAR_T *devid, 
                                            IN CONST DEV_TYPE_T tp);

FeaturesExplanation( Summary )

Report the upgrade progress.

parameter Explanation( Parameters )

Parameter Name

Explanation

percent

Upgrade progress value. 0 ~ 99

devid

When it is a child device, the ID of the child device is passed in; When it is a gateway, NULL

tp

Equipment type

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Success

Error Code

Fail return Error Code

tuya_iot_dev_upgd_result_report

OPERATE_RET tuya_iot_dev_upgd_result_report(IN CONST CHAR_T *dev_id, 
                                            IN CONST DEV_TYPE_T type, 
                                            IN CONST INT_T result);

FeaturesExplanation( Summary )

Report device upgrade results.

parameter Explanation( Parameters )

Parameter Name

Explanation

devid

When it is a child device, the ID of the child device is passed in; When it is a gateway, NULL

tp

Equipment type

result

Upgrade result

TI_UPGRD_STAT_S definition:

#define TUS_RD 1         //Upgrade is ready
#define TUS_UPGRDING 2   // upgrading
#define TUS_UPGRD_FINI 3  // update completed
#define TUS_UPGRD_EXEC 4  // Leave after upgrade

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Success

Error Code

Fail return Error Code

tuya_iot_refuse_upgrade

OPERATE_RET tuya_iot_refuse_upgrade(IN CONST FW_UG_S *fw, IN CONST CHAR_T *dev_id);

FeaturesExplanation( Summary )

Stop the upgrade during the upgrade process.

parameter Explanation( Parameters )

Parameter Name

Explanation

fw

Firmware information.Reference FW_UG_S

devid

When it is a child device, the ID of the child device is passed in; When it is a gateway, NULL

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Success

Error Code

Fail return Error Code

tuya_iot_gw_version_update

OPERATE_RET tuya_iot_gw_version_update(IN GW_PERMIT_DEV_TP_T type, IN CONST CHAR_T *ver);

FeaturesExplanation( Summary )

After the upgrade is complete, update the gateway or gateway adapter firmware version number.

parameter Explanation( Parameters )

Parameter Name

Explanation

type

Gateway type

ver

Firmware version number

GW_PERMIT_DEV_TP_T definition:

#define GP_DEV_ZB DEV_ZB_SNGL    // zigbee 
#define GP_DEV_OTHER DEV_OTHER_SNGL // other
#define GP_DEV_BLE DEV_BLE_SNGL     // Bluetooth
#define GP_DEV_INFRARED DEV_INFRARED_SNGL //Infrared
#define GP_DEV_MCU  DEV_NM_NOT_ATH_SNGL // MCU

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Success

Error Code

Fail return 的Error Code

Device function points (dp point)

Tuya provides an MQTT-based network application protocol to implement device control and status reporting. MQTT Yes is a lightweight publish-subscribe mode message transmission protocol, specifically designed for IoT applications in low-bandwidth and unstable network environments.

tuya_SDK encapsulates the implementation of the MQTT protocol layer and is presented in the form of function points (hereinafter referred to as dp points). It supports numeric, Boolean, enumerated, String, fault, and RAW data, as simple as defining C variables.

Developers need to create corresponding function points on Tuya Developer Platform according to the device functions, and create a new dp point Description: https://docs.tuya.com/cn/product/function.html

Features

1.Currently supports the creation of a maximum of 35 dp per product. Please use RAW data to implement complex functions; 2.Obj type: bool type, value type, string type, enum type, fault type (bitmap), tuya_SDK will filter the continuously reported values, the same will not be allowed Upload.

DP Structure

TY_RECV_OBJ_DP_S

typedef struct {
    DP_CMD_TYPE_E cmd_tp;
    DP_TRANS_TYPE_T dtt_tp;
    CHAR_T *cid;
    CHAR_T *mb_id;
    UINT_T dps_cnt;
    TY_OBJ_DP_S dps[0];
} TY_RECV_OBJ_DP_S;

FeaturesExplanation(Summary)

OBJ function point information command callback.

Member Explanation

Member name

Explanation

cmd_tp

Command type, how the command is generated

dtt_tp

Type of transmission

cid

CID is NULL, expressed as a gateway device is not empty, expressed as a sub-device.

mb_id

When dtt_tp is multicast, mb_id is the group ID

dps_cnt

Number of function point structure arrays

dps_cnt

Function point structure array

TY_RECV_RAW_DP_S

typedef struct {
    DP_CMD_TYPE_E cmd_tp;
    DP_TRANS_TYPE_T dtt_tp;
    CHAR_T *cid;
    BYTE_T dpid;
    CHAR_T *mb_id;
    UINT_T len;
    BYTE_T data[0];
} TY_RECV_RAW_DP_S;

FeaturesExplanation(Summary)

DP point data structure of RAW type.

Member Explanation

Parameter Name

Explanation

cmd_tp

Command type, how the command is generated

dtt_tp

Type of transmission

cid

CID is NULL, expressed as a gateway device is not empty, expressed as a sub-device.

dpid

DP point ID 。

mb_id

When dtt_tp is multicast, mb_id is the group ID

len

Transparent transmission data length

data

Transparent data cache

TY_DP_QUERY_S

typedef struct {
    CHAR_T *cid;
    UINT_T cnt;
    BYTE_T dpid[0];
} TY_DP_QUERY_S;

FeaturesExplanation(Summary)

Query DP point information.

Member Explanation

Parameter Name

Explanation

cid

CID is NULL, expressed as a gateway device is not empty, expressed as a sub-device.

cnt

The number of dp points. If it is equal to 0, it means all DP points.

dpid

The ID of the DP point to be queried.

Device command to issue callback

dev_obj_dp_cb

VOID dev_obj_dp_cb(IN CONST TY_RECV_OBJ_DP_S *dp);

FeaturesExplanation( Summary )

The user registers the callback function dev_obj_dp_cb in [TY_IoT_CBS_S] (# TY_IoT_CBS_S) . OBJ function point callback.

parameter Explanation( Parameters )

Parameter Name

Explanation

dp

The SDK calls back the user's OBJ type function point data. Reference TY_RECV_OBJ_DP_S

Return Value(Return Values)

Return Value

Explanation

dev_raw_dp_cb

VOID dev_raw_dp_cb(IN CONST TY_RECV_RAW_DP_S *dp);

FeaturesExplanation( Summary )

The user registers the callback function dev_raw_dp_cb in TY_IoT_CBS_S .Callback for transparent function points.

parameter Explanation( Parameters )

Parameter Name

Explanation

dp

The SDK calls back the user's RAW function point data.Reference TY_RECV_RAW_DP_S

Return Value(Return Values)

Return Value

Explanation

dev_dp_query_cb

VOID dev_dp_query_cb(IN CONST TY_DP_QUERY_S *dp_qry);

FeaturesExplanation( Summary )

The user registers the callback function dev_dp_query_cb in TY_IoT_CBS_S .Device specific data query entry callback.

parameter Explanation( Parameters )

Parameter Name

Explanation

dp_qry

Reference TY_DP_QUERY_S

Return Value(Return Values)

Return Value

Explanation

Device status report

dev_report_dp_json_async

OPERATE_RET dev_report_dp_json_async(IN CONST CHAR_T *dev_id, 
                                     IN CONST TY_OBJ_DP_S *dp_data,
                                     IN CONST UINT_T cnt);

FeaturesExplanation( Summary )

Asynchronous way, report function point information.

parameter Explanation( Parameters )

Parameter Name

Explanation

dev_id

Device ID

dp_data

Function point data structure

cnt

Number of dp_data structure arrays

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

dev_report_dp_raw_sync

OPERATE_RET dev_report_dp_raw_sync (IN CONST CHAR_T *dev_id, 
                                    IN CONST BYTE_T dpid, 
                                    IN CONST BYTE_T *data,
                                    IN CONST UINT_T len, 
                                    IN CONST UINT_T timeout);

FeaturesExplanation( Summary )

The device transparently transmits data to the reporting interface synchronously. The caller guarantees the reliability of data reporting.

parameter Explanation( Parameters )

Parameter Name

Explanation

dev_id

Device ID

dpid

Function point ID

data

Transparent data

len

Transparent data length

timeout

Function blocking timeout

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

dev_report_dp_stat_sync

#define dev_report_dp_stat_sync(dev_id, dp_data, cnt, timeout) \
    dev_report_dp_stat_sync_extend(dev_id, dp_data, cnt, timeout, TRUE)
OPERATE_RET dev_report_dp_stat_sync_extend(IN CONST CHAR_T *dev_id,
                                           IN CONST TY_OBJ_DP_S *dp_data,
                                           IN CONST UINT_T cnt,
                                           IN CONST UINT_T timeout, 
                                           IN CONST BOOL_T enable_auto_retrans);

FeaturesExplanation( Summary )

Device structured data synchronous reporting interface. The caller guarantees the reliability of data reporting. It is usually used for reporting statistical data.

parameter Explanation( Parameters )

Parameter Name

Explanation

dev_id

Device ID

dp_data

Function point information structure array.TY_OBJ_DP_S

cnt

The length of the DP status array

timeout

Function blocking timeout

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

Sub-device management

Sub-device distribution network

Data interaction diagram

  • This picture is to add a sub-device from the APP side of the mobile phone.

  • The gateway can also call tuya_iot_gw_bind_dev to bind directly.

  • The communication protocol between the sub-device and the gateway is implemented by the user.

Interface & callback description

TY_IoT_GW_CBS_S

typedef struct {
    GW_PERMIT_ADD_DEV_CB gw_add_dev_cb;
    GW_DEV_DEL_CB gw_del_cb;
    GW_DEV_GRP_INFM_CB gw_dev_grp_cb;
    GW_DEV_SCENE_INFM_CB gw_dev_scene_cb;
    GW_BIND_DEV_INFORM_CB gw_ifm_cb;
} TY_IoT_GW_CBS_S;

FeaturesExplanation(Summary)

The gateway enables the addition of sub-devices. This callback function is used to notify the scene, group and sub-device binding of deleting the sub-device.

Member Explanation

Member name

Explanation

gw_add_dev_cb

reference gw_add_dev_cb

gw_del_cb

reference gw_del_cb

gw_dev_grp_cb

reference gw_dev_grp_cb

gw_dev_scene_cb

reference gw_dev_scene_cb

gw_ifm_cb

reference gw_ifm_cb

gw_add_dev_cb

BOOL_T gw_add_dev_cb(IN CONST GW_PERMIT_DEV_TP_T tp, 
                     IN CONST BOOL_T permit, 
                     IN CONST UINT_T timeout);

FeaturesExplanation( Summary )

The user registers the callback function gw_add_dev_cb in TY_IoT_GW_CBS_S . When adding a sub-device, the function of enabling device access to the network is implemented in this callback.

parameter Explanation( Parameters )

Parameter Name

Explanation

tp

Sub-device type

permit

Whether to add sub-devices.TRUE true is allowed;FALSE is forbidden.

timeout

The timeout period of the distribution network. ** Unit in seconds **

Return Value(Return Values)

Return Value

Explanation

TRUE

Successful operation

FALSE

failure

gw_del_cb

VOID gw_del_cb(IN CONST CHAR_T *dev_id);

FeaturesExplanation( Summary )

The user registers the callback function gw_del_cb in TY_IoT_GW_CBS_S .When the sub-device is deleted, the user is notified via this callback.

parameter Explanation( Parameters )

Parameter Name

Explanation

dev_id

The ID of the sub-device that was deleted.

Return Value(Return Values)

Return Value

Explanation

tuya_iot_gw_bind_dev

OPERATE_RET tuya_iot_gw_bind_dev(IN CONST GW_PERMIT_DEV_TP_T tp, 
                                 IN CONST USER_DEV_DTL_DEF_T uddd, 
                                 IN CONST CHAR_T *id,
                                 IN CONST CHAR_T *pk, 
                                 IN CONST CHAR_T *ver);

FeaturesExplanation( Summary )

Sub-device interface bound to the gateway. When TUYA APP enables the gateway to add sub-devices, call this interface to bind the discovered sub-devices.

parameter Explanation( Parameters )

Parameter Name

Explanation

tp

Sub-device type.

uddd

The device type is defined by the user, which distinguishes different types of devices.

id

Sub-device id. The length cannot exceed 25 bytes.

pk

Sub-device product key。The length cannot exceed 16 bytes.

ver

Sub-device firmware version。The length cannot exceed 10 bytes.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

gw_bind_ifm_cb

VOID gw_bind_ifm_cb(IN CONST CHAR_T *dev_id, IN CONST OPERATE_RET op_ret);

FeaturesExplanation( Summary )

The user registers the callback function gw_bind_ifm_cb in TY_IoT_GW_CBS_S. When the sub-device is bound, the user is notified of the binding result through this callback.

parameter Explanation( Parameters )

Parameter Name

Explanation

dev_id

The ID of the bound child device.

op_ret

Binding results. OPRT_OK means to bind Success. Other means the binding failed.

Return Value(Return Values)

Return Value

Explanation

Unbind the subdevice

Interface & callback description

tuya_iot_gw_unbind_dev

OPERATE_RET tuya_iot_gw_unbind_dev(IN CONST CHAR_T *id);

FeaturesExplanation(Summary)

Unbind the subdevice

parameter Explanation( Parameters )

Parameter Name

Explanation

dev_id

The ID of the sub-device that needs to be unbound from the gateway.

Return Value(Return Values)

Return Value

Explanation

Sub-device firmware upgrade (OTA)

Data interaction diagram

  • The communication between the sub-device and the gateway device layer is implemented by the customer. Here only briefly.

  • The upgrade progress report can be controlled by the user. Only when the gateway sends the firmware package to the sub-device, it will update the report progress.

  • TUYA Smart APP has a 60s timeout mechanism for firmware upgrade:It will update the message and retime. When receiving the progress of a package of the device each time; the application layer can report the upgrade progress by controlling the upgrade progress to prolong the APP timeout.

Interface & callback description

dev_ug_inform_cb

VOID dev_ug_cb(IN CONST CHAR_T *dev_id, IN CONST FW_UG_S *fw)

FeaturesExplanation( Summary )

The user registers the callback function dev_ug_cb in TY_IoT_CBS_S . Notification of OTA firmware upgrade of the child device. It indicates that there is an updateable sub-device firmware.

parameter Explanation( Parameters )

Parameter Name

Explanation

dev_id

Sub-device ID with new firmware that can be updated.

fw

Return Value(Return Values)

Return Value

Explanation

tuya_iot_upgrade_dev

OPERATE_RET tuya_iot_upgrade_dev_notify(IN CONST CHAR_T *devid, 
                                        IN CONST FW_UG_S *fw,
                                        IN CONST GET_FILE_DATA_CB get_file_cb,
                                        IN CONST UPGRADE_NOTIFY_CB upgrd_nofity_cb,
                                        IN CONST PVOID_T pri_data)

FeaturesExplanation( Summary )

Device firmware upgrade interface. It contains the upgrade of the main control device on the gateway and the sub-devices of each terminal.

parameter Explanation( Parameters )

Parameter Name

Explanation

devid

Device id string

fw

Upgraded firmware information.Reference FW_UG_S

get_file_cb

Implemented in this callback, download content storage

upgrd_nofity_cb

Notification function that is called back when the download is complete. In this callback, the upgrade function of the device needs to be called.

pri_data

Parameters passed to get_file_cb and upgrd_nofity_cb. For example, download the upgrade file handle.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Success

Error Code

Fail return Error Code

tuya_iot_gw_subdevice_update

OPERATE_RET tuya_iot_gw_subdevice_update (IN CONST CHAR_T *id, IN CONST CHAR_T *ver);

FeaturesExplanation( Summary )

Update the firmware version number of the sub-device.

parameter Explanation( Parameters )

Parameter Name

Explanation

dev_id

Sub-device ID. ** Cannot exceed 25 bytes. **

ver

Subdevice firmware version number. ** Refer to the TUYA definition of version number format in the article. **

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

Sub-device online management

  • Enable tuya_SDK's heartbeat detection mechanismZ.

As shown below, this is a diagram of the interface call when the SDK's heartbeat keep-alive mechanism is turned on.

tuya_iot_dev_online_stat_update

OPERATE_RET tuya_iot_dev_online_stat_update(IN CONST CHAR_T *dev_id, IN CONST BOOL_T online);

FeaturesExplanation( Summary )

The gateway updates the online / offline status of the child device.

parameter Explanation( Parameters )

Parameter Name

Explanation

dev_id

Sub-device ID.

online

online status. TRUE is online, FALSE is offline.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_iot_sys_mag_hb_init

OPERATE_RET tuya_iot_sys_mag_hb_init(IN CONST DEV_HEARTBEAT_SEND_CB hb_send_cb);

FeaturesExplanation( Summary )

Enable heartbeat management capabilities of sub-devices.

parameter Explanation( Parameters )

Parameter Name

Explanation

hb_send_cb

The gateway checks all sub-devices every 3 seconds. If the sub-device is within the heartbeat packet timeout and the sub-device does not send a heartbeat to the gateway, the gateway will set the sub-device offline. Notify users at least three times via hb_send_cb.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_iot_set_dev_hb_timeout

OPERATE_RET tuya_iot_set_dev_hb_timeout (IN CONST CHAR_T *dev_id, IN CONST TIME_S hb_timeout_time);

FeaturesExplanation( Summary )

Set the heartbeat timeout time of the sub-device. If the gateway does not receive the heartbeat of the sub-device within the set time, the gateway will set the sub-device offline.

parameter Explanation( Parameters )

Parameter Name

Explanation

dev_id

Sub-device ID.

hb_timeout_time

Heartbeat timeout time, in seconds. If set to 0xffffffff, the sub-device will be skipped for heartbeat check and will always be online.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

tuya_iot_fresh_dev_hb

OPERATE_RET tuya_iot_fresh_dev_hb(IN CONST CHAR_T *dev_id)

FeaturesExplanation( Summary )

When the gateway receives the heartbeat information of the sub-device, it calls this function to refresh the heartbeat state of the sub-device.

parameter Explanation( Parameters )

Parameter Name

Explanation

dev_id

sub-device ID

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

Get sub-device information

tuya_iot_dev_traversal

DEV_DESC_IF_S *tuya_iot_dev_traversal(INOUT VOID **iterator);

FeaturesExplanation( Summary )

Sub-device traversal. Through this interface, all sub-devices under the gateway can be traversed.

parameter Explanation( Parameters )

Parameter Name

Explanation

iterator

Temporarily save the linked list. The user only needs to define a null pointer.

Return Value(Return Values)

Return Value

Explanation

DEV_DESC_IF_S

Pointer to the structure of device information. The user can read device information from it.

NULL

The device information has been read.

Sub-device group control

gw_dev_grp_cb

OPERATE_RET gw_dev_grp_cb(IN CONST GRP_ACTION_E action, IN CONST CHAR_T *dev_id, IN CONST CHAR_T *grp_id);

FeaturesExplanation( Summary )

The user registers the callback function gw_dev_grp_cb in TY_IoT_GW_CBS_S . In this callback, the group operation processing command is realized.

parameter Explanation( Parameters )

Parameter Name

Explanation

action

The type of operation for the group. Add and delete group operations.

dev_id

The ID of the sub-device that joined the group grp_id.

grp_id

Group ID .

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

Sub-device scene features

gw_dev_scene_cb

OPERATE_RET gw_dev_scene_cb(IN CONST SCE_ACTION_E action, 
                            IN CONST CHAR_T *dev_id, 
                            IN CONST CHAR_T *grp_id, 
                            IN CONST CHAR_T *sce_id);

FeaturesExplanation( Summary )

The user registers the callback function gw_dev_scene_cb in TY_IoT_GW_CBS_S .The scene processing command feature is implemented in this callback.

parameter Explanation( Parameters )

Parameter Name

Explanation

action

The operation type of the scene. Add and delete, and perform scene operations.

dev_id

Sub-device ID.

grp_id

Group ID .

sce_id

Scene ID.

Return Value(Return Values)

Return Value

Explanation

OPRT_OK

Successful operation

Error Code

Fail return Error Code

Sub-device reset callback

dev_reset_cb

VOID dev_reset_cb(IN CONST CHAR_T *dev_id, IN DEV_RESET_TYPE_E type)

FeaturesExplanation( Summary )

The user registers the callback function dev_reset_cb in TY_IoT_CBS_S .In this callback, reset processing of the sub-device needs to be handled.

parameter Explanation( Parameters )

Parameter Name

Explanation

dev_id

The ID of the sub-device that needs to be reset.

type

Reset type.

typedef enum {
    DEV_REMOTE_RESET_FACTORY, //Remove device
    DEV_RESET_DATA_FACTORY, //Clear local data and remove the device.
} DEV_RESET_TYPE_E;

Return Value(Return Values)

Return Value

Explanation

VOID

Log management

tuya_iot_app_cbs_init

VOID tuya_iot_app_cbs_init(IN CONST TY_IoT_APP_CBS_S *app_cbs);

FeaturesExplanation( Summary )

Initialize the application callback. Currently only supports users to register local log to get callback.

parameter Explanation( Parameters )

Parameter Name

Explanation

app_cbs

Get local log callback name.

Return Value(Return Values)

Return Value

Explanation

VOID

Notes (Notes)

Log management is essential for equipment maintenance and troubleshooting.It is recommended to leave about 5M ~ 10M space on the device side to save the device operation log.When the device fails, you can pull the device log on the developer platform through the gateway device ID information.** For the convenience of equipment problem analysis, this function must be implemented. **

Fake code

STATIC VOID app_log_path_cb(OUT CHAR_T *path, IN CONST INT_T len);
int main(void)
{
    tuya_iot_init(CFG_STORAGE_PATH);
    ......
    TY_IoT_APP_CBS_S iot_app_cbs =  {
        app_log_path_cb,
    };
    tuya_iot_app_cbs_init(&iot_app_cbs);
    ......
        
        
    while(1){
        sleep(10);
    }
}
/*****************************************************************
 * @Function: app_log_path_cb
 * @Description: Local log get callback
 * @Param[out]: path, File path string of the local log archive
 * @Param[in]: len,The maximum length of the path string of the local log compressed package file
 * @Return: void
 *****************************************************************/
STATIC VOID app_log_path_cb(OUT CHAR_T *path, IN CONST INT_T len)
{
    PR_DEBUG("app_log_path_cb,log_file_path maxlen:%d",len);
	//This is for reference only, the saving and compression of log files need to be implemented by users themselves
    CHAR_T log_file_path[] = {"/tmp/tuya_log.tar.gz"};

    if( (strlen(log_file_path) < len) && (path != NULL) ){
        snprintf(path, len, "%s", log_file_path);
    }
}

Error Code Description

  • It is convenient for developers to locate problems during debugging and will be continuously updated.

Error Code

Reason

Solution

-944

When reporting function points, the corresponding DPID is not under the product

Login to the Tuya IoT platform to confirm

-916

The device is disconnected from Tuya Cloud MQTT server long connection

Check whether the device is connected to the external network

-926

When the device reports MQTT data, it waits for the server puback to time out

Check the device network condition or reduce the reporting frequency

-945

DP point attribute values do not match

Check whether the reported DP point attribute value is consistent with the platform definition

Error code definition view: tuya_cloud_error_code.h

Last updated