# Link SDK Access

## Tuya Gateway Link SDK Development Manual

### Version History <a href="#title-0-version-20history" id="title-0-version-20history"></a>

| Version | Compilation / Revision Instructions | Revision Person | Revision Date | Remarks |
| ------- | ----------------------------------- | --------------- | ------------- | ------- |
| 1.0.0   | Create document                     | Liu Bo          | 20200327      |         |

### Overview <a href="#title-1-overview" id="title-1-overview"></a>

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 <a href="#title-2-prerequisites" id="title-2-prerequisites"></a>

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 to[Document Center](https://docs.tuya.com/zh/iot).If you have any questions, please contact Tuya Business.

#### Hardware Requirements <a href="#title-3-hardware-20requirements" id="title-3-hardware-20requirements"></a>

* 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 <a href="#title-4-software-20requirements" id="title-4-software-20requirements"></a>

* Support LINUX or ANDROID system.
* Support TCP / IP protocol stack.

#### SDK Acquisition <a href="#title-5-sdk-20acquisition" id="title-5-sdk-20acquisition"></a>

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](https://github.com/TuyaInc/tuya_gw_link_sdk).If the compiled product already contains the user's cross-compilation tool chain, you can download it directly.

#### Directory Structure <a href="#title-6-directory-20structure" id="title-6-directory-20structure"></a>

```
.
├── 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 <a href="#title-7-sdk-20initialization" id="title-7-sdk-20initialization"></a>

#### Equipment Certification <a href="#title-8-equipment-20certification" id="title-8-equipment-20certification"></a>

* 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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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.

![Tuya Gateway Link SDK Development Manual](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200601/ad41a48300b24372aaac559e6040b911.png)

#### Initialize The Interface Explanation <a href="#title-9-initialize-20the-20interface-20explanation" id="title-9-initialize-20the-20interface-20explanation"></a>

**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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#gw_ug_cb)            |
| gw\_reset\_cb      | Reference [gw\_reset\_cb](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#gw_reset_cb)      |
| dev\_obj\_dp\_cb   | Reference [dev\_obj\_dp\_cb](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#dev_obj_dp_cb) |
| dev\_raw\_dp\_cb   | Reference [dev\_raw\_dp\_cb](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#dev_raw_dp_cb) |
| dev\_dp\_query\_cb | dp point query callback                                                                                                                                                                                            |
| dev\_ug\_cb        | Reference [dev\_ug\_cb](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#dev_ug_cb)          |
| dev\_reset\_cb     | Reference [dev\_reset\_cb](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#TY_IoT_CBS_S)                |
| gw\_cbs        | Gateway callback registered by the user.Reference [TY\_IoT\_GW\_CBS\_S](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#tuya_iot_wf_gw_init) interface explanation.

**Return Value(Return Values)**

Refer to [tuya\_iot\_wf\_gw\_init](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#TY_IoT_CBS_S)                   |
| gw\_cbs        | The user needs to register the callback function array of gateway.Reference [TY\_IoT\_GW\_CBS\_S](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#tuya_iot_gw_init) interface explanation

**Return Value(Return Values)**

Refer to [tuya\_iot\_gw\_init](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#TY_IoT_CBS_S)                 |
| gw\_cbs        | Array of gateway callback functions that users need to register.Reference [TY\_IoT\_GW\_CBS\_S](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#tuya_iot_reg_get_nw_stat_cb) and [tuya\_iot\_reg\_get\_wf\_nw\_stat\_cb](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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 <a href="#title-10-equipment-20distribution-20network" id="title-10-equipment-20distribution-20network"></a>

> 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 <a href="#title-11-wired-20equipment-20distribution-20network" id="title-11-wired-20equipment-20distribution-20network"></a>

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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#tuya_iot_gw_unactive) 。

**Interactive Diagram of Distribution Process**

![Tuya Gateway Link SDK Development Manual](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200601/2a5f2411bec045dbb502e130dd78a07b.png)

* 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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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         | <p>network status.<br>Networking is TRUE, otherwise it is FALSE.</p> |

**Return Value(Return Values)**

| **Return Value** | **Explanation** |
| ---------------- | --------------- |
| OPRT\_OK         | Get success     |
| Other            | Get failed      |

#### Wi-Fi Equipment Distribution Network <a href="#title-12-wi-fi-20equipment-20distribution-20network" id="title-12-wi-fi-20equipment-20distribution-20network"></a>

* 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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#tuya%5C_iot%5C_wf%5C_soc%5C_dev%5C_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)**

![Tuya Gateway Link SDK Development Manual](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200601/7e8096b282fc4b5f99a7622066f523bc.png)

* 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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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**

![Tuya Gateway Link SDK Development Manual](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200601/be26cb2858a6414c862069efb5172a76.png)

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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#tuya_hal_Wi-Fi_all_ap_scan) and [tuya\_hal\_Wi-Fi\_assign\_ap\_scan](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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 <a href="#title-13-gateway-20reset" id="title-13-gateway-20reset"></a>

#### APP Reset Geteway <a href="#title-14-app-20reset-20geteway" id="title-14-app-20reset-20geteway"></a>

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 <a href="#title-15-local-20reset-20gateway" id="title-15-local-20reset-20gateway"></a>

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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#tuya_iot_gw_unactive) and [tuya\_iot\_wf\_gw\_unactive](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#tuya_iot_wf_gw_unactive) .

### Device firmware upgrade (OTA) <a href="#title-16-device-20firmware-20upgrade-20-ota" id="title-16-device-20firmware-20upgrade-20-ota"></a>

* 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 <a href="#title-17-firmware-20package-20configuration-20instructions" id="title-17-firmware-20package-20configuration-20instructions"></a>

* 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 <a href="#title-18-how-20the-20upgrade-20begins" id="title-18-how-20the-20upgrade-20begins"></a>

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 <a href="#title-19-data-20interaction-20diagram" id="title-19-data-20interaction-20diagram"></a>

* 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.

![Tuya Gateway Link SDK Development Manual](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200601/ca56b72aa296419a87657821273557e7.png)

#### Relevant interface and callback description <a href="#title-20-relevant-20interface-20and-20callback-20description" id="title-20-relevant-20interface-20and-20callback-20description"></a>

**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        | <p>UPGRADE\_TYPE\_NORMAL : Ordinary upgrade<br>UPGRADE\_TYPE\_SILENT : Forced upgrade</p> |
| 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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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 <a href="#title-21-the-20application-20layer-20reports-20the-20progress-20of-20the-20upgrade" id="title-21-the-20application-20layer-20reports-20the-20progress-20of-20the-20upgrade"></a>

When the progress of the application layer control upgrade is reported, please use [tuya\_iot\_upgrade\_gw\_notify](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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          | <p>When it is a child device, the ID of the child device is passed in;<br>When it is a gateway, NULL</p> |
| 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          | <p>When it is a child device, the ID of the child device is passed in;<br>When it is a gateway, NULL</p> |
| 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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#FW_UG_S) |
| devid          | <p>When it is a child device, the ID of the child device is passed in;<br>When it is a gateway, NULL</p>                                                                                                                   |

**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) <a href="#title-22-device-20function-20points-20-dp-20point" id="title-22-device-20function-20points-20-dp-20point"></a>

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 <a href="#title-23-dp-20structure" id="title-23-dp-20structure"></a>

**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         | <p>CID is NULL, expressed as a gateway device<br>is not empty, expressed as a sub-device.</p> |
| 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            | <p>CID is NULL, expressed as a gateway device<br>is not empty, expressed as a sub-device.</p> |
| 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            | <p>CID is NULL, expressed as a gateway device<br>is not empty, expressed as a sub-device.</p> |
| 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 <a href="#title-24-device-20command-20to-20issue-20callback" id="title-24-device-20command-20to-20issue-20callback"></a>

**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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#TY_IoT_CBS_S) .Device specific data query entry callback.

**parameter Explanation( Parameters )**

| Parameter Name | Explanation                                                                                                                                                                                                        |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| dp\_qry        | Reference [TY\_DP\_QUERY\_S](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#TY_DP_QUERY_S) |

**Return Value(Return Values)**

| **Return Value** | **Explanation** |
| ---------------- | --------------- |
|                  |                 |

#### Device status report <a href="#title-25-device-20status-20report" id="title-25-device-20status-20report"></a>

**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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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 <a href="#title-26-sub-device-20management" id="title-26-sub-device-20management"></a>

#### Sub-device distribution network <a href="#title-27-sub-device-20distribution-20network" id="title-27-sub-device-20distribution-20network"></a>

**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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#tuya_iot_gw_bind_dev) to bind directly.
* The communication protocol between the sub-device and the gateway is implemented by the user.

![Tuya Gateway Link SDK Development Manual](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200601/679d8516003a413fb151bef1259cb6b9.png)

**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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#gw_add_dev_cb)     |
| gw\_del\_cb        | reference [gw\_del\_cb](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#gw_del_cb)              |
| gw\_dev\_grp\_cb   | reference [gw\_dev\_grp\_cb](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#gw_dev_grp_cb)     |
| gw\_dev\_scene\_cb | reference [gw\_dev\_scene\_cb](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#gw_dev_scene_cb) |
| gw\_ifm\_cb        | reference [gw\_ifm\_cb](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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        | <p>Binding results.<br>OPRT\_OK means to bind Success.<br>Other means the binding failed.</p> |

**Return Value(Return Values)**

| **Return Value** | **Explanation** |
| ---------------- | --------------- |
|                  |                 |

#### Unbind the subdevice <a href="#title-28-unbind-20the-20subdevice" id="title-28-unbind-20the-20subdevice"></a>

**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) <a href="#title-29-sub-device-20firmware-20upgrade-20-ota" id="title-29-sub-device-20firmware-20upgrade-20-ota"></a>

**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.

![Tuya Gateway Link SDK Development Manual](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200601/86fec99c7fa848d0b19f589bc94f5f9e.png)

**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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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             | [FW\_UG\_S](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#FW_UG_S) |

**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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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 <a href="#title-30-sub-device-20online-20management" id="title-30-sub-device-20online-20management"></a>

* 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 Gateway Link SDK Development Manual](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200601/8c217353a1ef43d195281e719f455126.png)

**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         | <p>online status.<br>TRUE is online,<br>FALSE is offline.</p> |

**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 <a href="#title-31-get-20sub-device-20information" id="title-31-get-20sub-device-20information"></a>

**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 <a href="#title-32-sub-device-20group-20control" id="title-32-sub-device-20group-20control"></a>

**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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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 <a href="#title-33-sub-device-20scene-20features" id="title-33-sub-device-20scene-20features"></a>

**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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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 <a href="#title-34-sub-device-20reset-20callback" id="title-34-sub-device-20reset-20callback"></a>

**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](https://docs.tuya.com/en/iot/smart-product-solution/product-solutiongateway/gateway-link-sdk-access-solution/tuya-gateway-link-sdk-development-manual?id=K9ducoah42rl2#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 <a href="#title-35-log-20management" id="title-35-log-20management"></a>

**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 <a href="#title-36-error-20code-20description" id="title-36-error-20code-20description"></a>

* 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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ifreeq.com/developer/smart-product-solution/gateway/link-sdk-access.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
