# Tuya Wi-Fi SDK Reference

### Change log <a href="#title-0-change-20log" id="title-0-change-20log"></a>

| Version | Description          | Date     |
| ------- | -------------------- | -------- |
| 1.0.0   | The initial version. | 20200301 |

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

The Tuya device OS is a proprietary IoT operating system, which provides abundant components and you can make SDK customization according to your business requirements. It has the characteristic of lightweight, interconnection, secure transmission, fully functional components and rapid development, effectively lowering the development threshold and shortening the development cycle. It can be widely used in wearable devices, video monitors, compute gateways, smart speakers, smart locks and other smart home appliances.

The Tuya Wi-Fi SDK is developed based on the Tuya device OS technology and tailored according to the Wi-Fi application in the Internet of Things scenarios. It is applicable to any Wi-Fi chip that is configured according to the Tuya Smart platform interconnection rules.

### Project address <a href="#title-2-project-20address" id="title-2-project-20address"></a>

[GitHub repo of the Tuya Wi-Fi SDK for RTL8710BN](https://github.com/TuyaInc/tuya_DeviceOS_SDK/blob/master/IOT%20Device%20OS/ty_iot_wf_rtos_sdk/1.0.3/1.0.3_rtl8710bn.tar.gz)

### Directories <a href="#title-3-directories" id="title-3-directories"></a>

The following table describes the directories of the Tuya Wi-Fi SDK.

| Directory      | Subdirectory            | Description                                                                                                                                                                                       | Reference                                               |
| -------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- |
| `apps`         | /                       | Stores your application and samples of the Tuya Wi-Fi SDK. It is better to create a folder for every application or sample.                                                                       | See the sample of Tuya Wi-Fi SDK for usage instruction. |
|                | `oem_rtlbn_1_plug_demo` | Contains one or more samples.                                                                                                                                                                     | N/A.                                                    |
| `sdk`          | `sdk`                   | Stores the header files and library files of Tuya Wi-Fi SDK.                                                                                                                                      | See the API reference section for details.              |
|                | `include`               | Stores all of the APIs provided by Tuya Wi-Fi SDK.                                                                                                                                                | N/A.                                                    |
|                | `lib`                   | The `libtuya_iot.a` file that is compiled by Tuya Wi-Fi SDK.                                                                                                                                      | N/A.                                                    |
| `platforms`    | /                       | Stores the development environment and toolchains of Tuya Wi-Fi SDK. The `platform` is an empty folder at first. Related toolchains are downloaded automatically to this folder during compiling. | N/A.                                                    |
| `build_app.sh` | /                       | The compiling entry point for applications and samples.                                                                                                                                           | Environment preparation and compilation.                |
| `CHANGELOG.md` | /                       | Records all the notable changes for each version.                                                                                                                                                 | N/A.                                                    |
| `README.md`    | /                       | User guide written in Markdown.                                                                                                                                                                   | N/A.                                                    |

### Initialization <a href="#title-4-initialization" id="title-4-initialization"></a>

#### Process <a href="#title-5-process" id="title-5-process"></a>

The initialization is the process of loading and running firmware automatically after the device is powered on for initializing.

Because the Tuya Wi-Fi SDK is platform independent, differences exist in its initialization process for different products. Taking the lighting products that have SoC designs and are connected via Wi-Fi as an example, the initialization process is shown in the following picture.

![Tuya Wi-Fi SDK Reference](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200612/dfa903ec569f47c788185197f0e73f57.png)

The initialization of Tuya Wi-Fi SDK is almost completed in the `user_main()` API. It mainly includes the following stages:

* `pre_init_process`: makes preparation before initializing the Tuya IoT SDK. It is optional, therefore you can implement it depending on your needs. The `pre_init_process` is vastly used for lighting products, it is recommended to turn on them as soon as possible to create a better user experience. During the `pre_init_process` stage, the following actions will be performed.
  * `pre_device_init`: prints the status information and sets the importance level of logs.
  * `tuya_cnt_rst_judge`: counts the hardware devices.
  * `tuya_iot_kv_flash_init`: initializes the storage space to store the generated critical data during the `pre_init_process` operation.
  * Your customized actions, such as subscribing to events.
* `tuya_iot_init`: this stage is to initialize necessary functions for Tuya Wi-Fi SDK. Allocate necessary compute and storage resources.
* `app_init`: initializes the programming code of application, and set the working mode.
* `device_init`: performs API initialization according to the working mode. The initialization API varies for different product categories.
  * SoC series: `tuya_iot_wf_soc_dev_init`
  * MCU series: `tuya_iot_wf_mcu_dev_init`
  * Gateway series: `tuya_iot_wf_gw_dev_init`
  * To initialize a product, you must set the value of `product_key`, `firmware_key` and `start_mode` (network configuration mode).

#### Working mode <a href="#title-6-working-20mode" id="title-6-working-20mode"></a>

During the `app_init` stage, you must select an appropriate working mode. The mode determines the initialization, network configuration, reset and other properties of your products.

**GWCM\_OLD**

The `GWCM_OLD` mode is mainly applicable for the firmware using serial port interaction type.

Production testing: only after the MCU initiates a production testing request, which can be initiated any time, the device can scan the router that are used for production testing.

> **Note**: if a SoC firmware uses the `OLD` mode, it cannot enter into the production testing mode.

**GWCM\_LOW\_POWER**

The `GWCM_LOW_POWER` mode is mainly applicable for the sockets with SoC designs.

* Before configuring the network:
  * After powering on, the `GWCM_LOW_POWER` mode is indicated with a constant light status. Manual switching is required to enter the network configuration status.
  * Within 10 seconds, if the device keeps disconnected, you can restart it to re-enter the network configuration status.
  * After 10 seconds, if the device keeps disconnected, you can restart it to re-enter the `GWCM_LOW_POWER` status.
  * If the device keeps disconnected within 3 minutes, it enters into the `GWCM_LOW_POWER` status automatically.
* Network configured and product added
  * Removal from App: the device restarts and enters into the network configuration mode automatically. By default, the network configuration is the EZ mode (smart configuration). The existing value of the `SSID` and `PassWD` will be erased.
  * Manual removal: the device restarts and enters into the network configuration mode automatically. The existing value of the `SSID` and `PassWD` will be erased.
  * Network not configured:
    * Within 10 seconds, if the device keeps disconnected, you can restart it to re-enter the network configuration status.
    * After 10 seconds, if the device keeps disconnected, you can restart it to re-enter the `GWCM_LOW_POWER` status.
    * If the device keeps disconnected within 3 minutes, it enters into the `GWCM_LOW_POWER` status automatically.
  * Network configured: the device is activated and connected again.
* Production testing: when a device is in the `GWCM_LOW_POWER` mode, you must restart it to scan the production testing router to enter the production testing mode.

**GWCM\_SPCL\_MODE**

The `SPCL_MODE` mode is mainly applicable for the lighting products with SoC designs.

* Before configuring the network:
  * After powering on, the `SPCL_MODE` mode is indicated with a constant light status. Manual switching is required to enter the network configuration status.
  * Within 10 seconds, if the device keeps disconnected, you can restart it to re-enter the network configuration status.
  * After 10 seconds, if the device keeps disconnected, you can restart it to re-enter the `GWCM_SPCL_MODE` status.
  * If the device keeps disconnected within 3 minutes, it enters into the `LOW_POWER` status automatically.
* Network configured and product added
  * Removal from App: the device restarts and enters into the network configuration mode automatically. By default, the network configuration is the EZ mode (smart configuration). The existing value of the `SSID` and `PassWD` will be erased.
  * Manual removal: the device restarts and enters into the network configuration mode automatically. The existing value of the `SSID` and `PassWD` will be erased.
  * Network not configured:
    * Within 10 seconds, if the device keeps disconnected, you can restart it to re-enter the network configuration status.
    * After 10 seconds, if the device keeps disconnected, you can restart it to re-enter the `GWCM_SPCL_MODE` status.
    * If the device keeps disconnected within 3 minutes, it enters into the `GWCM_SPCL_MODE` status automatically.
  * Network configured: the device is activated and connected again.
* Production testing: when a device is in the `GWCM_SPCL_MODE` mode, you must restart it to scan the production testing router to enter the production testing mode.

**GWCM\_OLD\_CPT**

The `GWCM_OLD_CPT` mode is mainly used for electrical and lighting products with SoC designs. After powering on, products enter the network configuration state and wait for configuration.

When the device waits for network configuration, you can restart the device to make it scanned by routers and enter into the production testing mode.

**GWCM\_LOW\_POWER\_AUTO\_CFG**

The `GWCM_LOW_POWER_AUTO_CFG` mode is mainly used for electrical products with SoC designs. Usually, those products flash when they are powered on, and enter into the `LOW_POWER` mode 3 minutes later.

* Before configuring the network: after powering on, the products enter into the EZ (network smart configuration) mode, and sway between the EZ and AP mode.
  * Network not configured: if the product is restarted within 3 minutes, it keeps the last network configuration state. If the network of the product is not configured within 3 minutes, it enters into the `LOW_POWER` mode automatically.
* Network configured and device added
  * Removal from App: the device restarts and enters into the network configuration mode automatically. By default, the network configuration is the EZ mode (smart configuration). The existing value of the `SSID` and `PassWD` will be erased.
  * Manual removal: the device restarts and enters into the network configuration mode automatically. The existing value of the `SSID` and `PassWD` will be erased.
  * If the device keeps disconnected within 3 minutes, it enters into the `GWCM_SPCL_MODE` status automatically.
  * Network configured: the device is activated and connected again.
* Production testing: when a device is in the `GWCM_LOW_POWER_AUTO_CFG` mode, you must restart it to scan the production testing router to enter the production testing mode.

**GWCM\_SPCL\_AUTOCFG**

The `GWCM_SPCL_AUTOCFG` mode is mainly used for lighting products with SoC designs.

* Before configuring the network:
  * After powering on, the `GWCM_SPCL_AUTOCFG` mode is indicated with a constant light status. Manual switching is required to enter the network configuration status.
  * Within 10 seconds, if the device keeps disconnected, you can restart it to re-enter the network configuration status.
  * After 10 seconds, if the device keeps disconnected, you can restart it to re-enter the `GWCM_LOW_POWER` status.
  * If the device keeps disconnected within 3 minutes, it enters into the `GWCM_LOW_POWER` status automatically.
* Network configured and product added
  * Removal from App: the device restarts and enters into the network configuration mode automatically. By default, the network configuration is the EZ mode (smart configuration). The existing value of the `SSID` and `PassWD` will be erased.
  * Network not configured:
    * Within 10 seconds, if the device keeps disconnected, you can restart it to re-enter the network configuration status.
    * After 10 seconds, if the device keeps disconnected, you can restart it to re-enter the `GWCM_SPCL_AUTOCFG` status.
    * If the device keeps disconnected within 3 minutes, it enters into the `GWCM_LOW_POWER` status automatically.
  * Manual removal: the device restarts and enters into the network configuration mode automatically. The existing value of the `SSID` and `PassWD` will be erased.
  * Network configured: the device is activated and connected again.
* Production testing: when a device is in the `GWCM_SPCL_AUTOCFG` mode, you must restart it to scan the production testing router to enter the production testing mode.

#### API reference <a href="#title-7-api-20reference" id="title-7-api-20reference"></a>

**TY\_IOT\_CBS\_S**

In the `app_init` phase of the initial process, users need to provide some callbacks according to their needs. The SDK will notify the application of the corresponding state change through the callback at the corresponding stage.

```
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;
    ACTIVE_SHORTURL_CB 		active_shorturl;
}TY_IOT_CBS_S;
```

**Description**

The SDK callback interface.

**Member description**

| Member             | Prototype                                                           | Description                                        |
| ------------------ | ------------------------------------------------------------------- | -------------------------------------------------- |
| gw\_status\_cb     | VOID (\*GW\_STATUS\_CHANGED\_CB)(IN CONST GW\_STATUS\_E status);    | It is called when the SDK internal status changes. |
| gw\_ug\_cb         | int (\*GW\_UG\_INFORM\_CB)(IN CONST FW\_UG\_S \*fw);                | It is called when the SDK detects it.              |
| gw\_reset\_cb      | VOID (\*GW\_RESET\_IFM\_CB)(GW\_RESET\_TYPE\_E type);               | It is called when SDK resets the device.           |
| dev\_obj\_dp\_cb   | VOID (\*DEV\_OBJ\_DP\_CMD\_CB)(IN CONST TY\_RECV\_OBJ\_DP\_S \*dp); | It is called when SDK receives DP.                 |
| dev\_raw\_dp\_cb   | VOID (\*DEV\_RAW\_DP\_CMD\_CB)(IN CONST TY\_RECV\_RAW\_DP\_S \*dp); | It is called when SDK receives RAW DP.             |
| dev\_dp\_query\_cb | VOID (\*DEV\_DP\_QUERY\_CB)(IN CONST TY\_DP\_QUERY\_S \*dp\_qry);   | It is called when SDK receives DP query command.   |

**tuya\_iot\_init**

**Prototype**

```
OPERATE_RET tuya_iot_init(IN CONST CHAR_T *fs_storge_path)
```

**Description**

Initializes the Tuya IoT system. And it must be called at first.

**Parameter**

| **Parameter**    | **Description**                                              | **Data type** | **Required?** | **Notice**                                        |
| ---------------- | ------------------------------------------------------------ | ------------- | ------------- | ------------------------------------------------- |
| fs\_storge\_path | Allocates a readable and writable file partition for the SDK | CHAR\_T       | Yes           | The path length cannot be greater than 110 bytes. |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Success                                |
| Error Code       | The error code is returned on failure. |

**tuya\_iot\_get\_sdk\_info**

**Prototype**

```
CHAR_T *tuya_iot_get_sdk_info(VOID);
```

**Description**

Obtains the Tuya IoT SDK version information.

**Parameter**

| **Parameter** | **Description** | **Data type** | **Required?** | **Notice** |
| ------------- | --------------- | ------------- | ------------- | ---------- |
| VOID          | N/A             | N/A           | N/A           | N/A        |

**Return value**

| **Return value** | **Description**                                                                                                        |
| ---------------- | ---------------------------------------------------------------------------------------------------------------------- |
| SDK information  | A string. SDK information includes SDK compilation time, platform, and version number, and enabled function attributes |

**tuya\_iot\_sys\_mag\_hb\_init**

**Prototype**

```
OPERATE_RET tuya_iot_sys_mag_hb_init(IN CONST DEV_HEARTBEAT_SEND_CB hb_send_cb);
```

**Description**

Enables heartbeat management capabilities of child devices.

**Parameter**

| **Parameter** | **Description**                                                                                                                                                                                                                                                                   | **Data type**                  | **Required?** | **Notice** |
| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------ | ------------- | ---------- |
| hb\_send\_cb  | The gateway checks all child devices every 3 seconds. If the child device does not send a heartbeat to the gateway within the timeout period of the heartbeat packet, the gateway will set the child device offline and notify the user at least three times through `hb_send_cb` | `hb_send_cb` callback function | Yes           | N/A        |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_iot\_set\_wf\_gw\_prod\_info**

**Prototype**

```
OPERATE_RET tuya_iot_set_wf_gw_prod_info(IN CONST WF_GW_PROD_INFO_S *wf_prod_info);
```

**Description**

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

| **Parameter**  | **Description**                                                                                    | **Data type**            | **Required?** | **Notice**                                    |
| -------------- | -------------------------------------------------------------------------------------------------- | ------------------------ | ------------- | --------------------------------------------- |
| wf\_prod\_info | <p>This structure contains device information:<br>uuid and authkey<br>Wi-Fi ssid and password.</p> | WF\_GW\_PROD\_INFO\_S \* | Yes           | You need to pass this information to the SDK. |

**Return value**

| **Return value** | **Description**                |
| ---------------- | ------------------------------ |
| OPRT\_OK         | Operation succeeded.           |
| Error Code       | Error code returned on failure |

**tuya\_iot\_wf\_mcu\_dev\_init**

**Prototype**

```
OPERATE_RET tuya_iot_wf_mcu_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 CHAR_T *p_firmware_key, 
                                     IN CONST CHAR_T *product_key, IN CONST CHAR_T *wf_sw_ver, IN CONST CHAR_T *mcu_sw_ver);
```

**Description**

The metworking module and MCU device initialization interface.

**Parameter**

| **Parameter**    | **Description**                                                                                                                                                                                                                                                                             | **Data type**          | **Required?** | **Notice** |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | ------------- | ---------- |
| cfg              | <p>How Wi-Fi devices work. Working mode:<br>1. Non-low power consumption mode.<br>2. Low power consumption mode.<br>3. Special low power consumption mode.</p>                                                                                                                              | GW\_WF\_CFG\_MTHD\_SEL | Yes           | N/A        |
| start\_mode      | <p>Wi-Fi distribution mode:<br>1. Only work in ap configuration mode.<br>2. Only smart config mode.<br>3. ap and smart config mode, the default ap mode.<br>4. ap and smart config mode.<br>The default value sets to the smart config mode. Select the mode according to actual needs.</p> | GW\_WF\_START\_MODE    | Yes           | N/A        |
| cbs              | Wi-Fi SDK user callback function                                                                                                                                                                                                                                                            | TY\_IOT\_CBS\_S \*     | Yes           | N/A        |
| p\_firmware\_key | Firmware key. When the firmware is uploaded to the Tuya platform, there will be a firmware key.                                                                                                                                                                                             | String                 | Yes           | N/A        |
| product\_key     | Get when Tuya creates a product                                                                                                                                                                                                                                                             | String                 | Yes           | N/A        |
| wf\_sw\_ver      | Wi-Fi software version number                                                                                                                                                                                                                                                               | String                 | Yes           | N/A        |
| mcu\_sw\_ver     | MCU firmware version number                                                                                                                                                                                                                                                                 | String                 | Yes           | N/A        |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_iot\_wf\_soc\_dev\_init**

**Prototype**

```
OPERATE_RET tuya_iot_wf_soc_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 CHAR_T *product_key, IN CONST CHAR_T *wf_sw_ver);
```

**Description**

The Wi-Fi SoC device initialization interface.

**Parameter**

| **Parameter** | **Description**                                                                                                                                                                                                                                           | **Data type**          | **Required?** | **Notice** |
| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | ------------- | ---------- |
| cfg           | How Wi-Fi devices work. Working mode: 1) Non-low power consumption mode. 2) Low power consumption mode. 3) Special low power consumption mode.                                                                                                            | GW\_WF\_CFG\_MTHD\_SEL | Yes           | N/A        |
| start\_mode   | Wi-Fi distribution mode: 1) Only works in AP configuration mode. 2) Only smart config mode. 3) AP and smart config mode, the default AP mode. 4) AP and smart config mode. The default smart config mode. Select the mode according to your actual needs. | GW\_WF\_START\_MODE    |               | N/A        |
| cbs           | Wi-Fi SDK user callback function                                                                                                                                                                                                                          | TY\_IOT\_CBS\_S \*     |               | N/A        |
| product\_key  | Get when Tuya creates a product                                                                                                                                                                                                                           | String                 |               | N/A        |
| wf\_sw\_ver   | Software version number                                                                                                                                                                                                                                   | String                 |               | xx.xx.xx   |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_iot\_wf\_gw\_init**

**Prototype**

```
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);
```

**Description**

The Wi-Fi gateway initialization interface.

**Parameter**

| **Parameter** | **Description**                                                                                                                                                                                                          | **Data type**          | **Required?** | **Notice** |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------- | ------------- | ---------- |
| cfg           | How Wi-Fi devices work. Working mode: 1) Non-low power consumption mode 2) Low power consumption mode 3) Special low power consumption mode                                                                              | GW\_WF\_CFG\_MTHD\_SEL | Yes           | N/A        |
| start\_mode   | <p>Wi-Fi distribution mode:<br>1. Only works in AP configuration mode<br>2. Only smart config mode<br>3. AP and smart config mode, the default AP mode<br>4. AP and smart config mode, the default smart config mode</p> | GW\_WF\_START\_MODE    | Yes           | N/A        |
| cbs           | Tuya Wi-Fi SDK user callback                                                                                                                                                                                             | TY\_IOT\_CBS\_S \*     | Yes           | N/A        |
| gw\_cbs       | Tuya Gateway user callback                                                                                                                                                                                               | TY\_IOT\_GW\_CBS\_S \* | Yes           | N/A        |
| product\_key  | Tuya platform creation time to obtain                                                                                                                                                                                    | String                 | Yes           | N/A        |
| wf\_sw\_ver   | Software version number                                                                                                                                                                                                  | String                 | Yes           | N/A        |
| attr          | Array of Gateway Attributes                                                                                                                                                                                              | GW\_ATTACH\_ATTR\_T \* | Yes           | N/A        |
| attr\_num     | Gateway attribute length                                                                                                                                                                                                 | UINT\_T                | Yes           | N/A        |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_iot\_wf\_gw\_dev\_init**

**Prototype**

```
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);
```

**Description**

The Wi-Fi gateway and device initialization interface. Compared with `tuya_iot_wf_gw_init`, this function makes the gateway have the attributes of the device and can set function points.

**Parameter**

Refers to `tuya_iot_wf_gw_init` interface description.

**Return value**

Refers to `tuya_iot_wf_gw_init` interface description.

**tuya\_iot\_reg\_get\_wf\_nw\_stat\_cb**

**Prototype**

```
OPERATE_RET tuya_iot_reg_get_wf_nw_stat_cb(IN CONST GET_NW_STAT_CB nw_stat_cb);
```

**Description**

Gets Wi-Fi status interface.

**Parameter**

| **Parameter** | **Description**                                                                                    | **Data type** | **Required?** | **Notice**                   |
| ------------- | -------------------------------------------------------------------------------------------------- | ------------- | ------------- | ---------------------------- |
| nw\_stat\_cb  | Tuya SDK network detection callback, the parameter of the callback function is the network status. | nw\_stat\_cb  | Yes           | See SDK samples for details. |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

### Network configuration <a href="#title-8-network-20configuration" id="title-8-network-20configuration"></a>

#### The process <a href="#title-9-the-20process" id="title-9-the-20process"></a>

After the device is initialized, the most important follow-up is to configure the network so that the device can connect to the cloud through the router. If the device has been connected, you do not need to configure the network after device restarting or initializing.

You need to determine the working mode supported by the Wi-Fi network card, and inform the Tuya Wi-Fi SDK the network configuration mode of the device when the `tuya_iot_wf_soc_dev_init` or `tuya_iot_wf_gw_dev_init` is initialized. Refer to the description of `start_mode` parameters of the interface for more information. During the network configuration, you must use a mobile App to interact with the device, and send the `ssid`, `pswd`, `token`, and other information through coding, broadcast packets and multicast packets until the network is configured.

**EZ mode**

EZ indicates fast configuration. When the Wi-Fi module works in the `monitor` mode, the device can easily to receive and resolve the Wi-Fi beacon packets that are sent by the mobile App, and get configuration information from them. Later, the Wi-Fi module turns to the `station` mode and wait for network configuration.

![Tuya Wi-Fi SDK Reference](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200608/107233ede85d4d52940569dd709caf4f.png)

**AP network configuration mode**

The process of AP network configuration is relatively complicated. In this mode, devices must enable AP hotspot, and wait for network connection from mobile phone. Next, the configuration information is sent via LAN network to the device, and the Wi-Fi module turns to the `station` mode and wait for network configuration.

**Other network configuration mode**

The Tuya Wi-Fi SDK supports other network configuration mode, such as QR code scanning or Bluetooth communication. In those configuration mode, your application obtains the `ssid`, `passwd`, and `token` via the following methods and directly applies for device joining by calling APIs.

* QR code: the devices scan the QR code that are generated by App to obtain the `ssid`, `passwd`, and `token` information.
* Easy routing: the devices obtain the `ssid`, `passwd`, and `token` information via routers.
* FFS: the devices obtain the `ssid`, `passwd`, and `token` information via third party devices, such as Amazon Echo.
* Sound wave: the devices obtain the encoded `ssid`, `passwd`, and `token` information via serial ports or sound waves.
* Bluetooth: the devices obtain the `ssid`, `passwd`, and `token` information via Bluetooth.

**The network configuration mode**

You can choose the network configuration mode by calling the `device_init()` API, and the following values are supported.

* AP only (`WF_START_AP_ONLY`): only works in the AP configuration mode.
* Smart only (`WF_START_SMART_ONLY`): works in the `smart config` mode.
* AP first (`WF_START_AP_FIRST`): works both in the AP configuration mode and the `smart config` mode, but the AP configuration mode is the default setting.
* Smart first (`WF_START_SMART_FIRST`): works both in the AP configuration mode and the `smart config` mode, but the `smart config` mode is the default setting.
* Both AP and Smart (`WF_START_SMART_AP_CONCURRENT`): works both in the AP configuration mode and the `smart config` mode.

#### API reference <a href="#title-10-api-20reference" id="title-10-api-20reference"></a>

**tuya\_hal\_wifi\_all\_ap\_scan**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_all_ap_scan(OUT AP_IF_S **ap_ary, OUT UINT_T *num);
```

**Description**

Scans all AP.

**Parameter**

| **Parameter** | **Description**                                            | **Data type**  | **Required?** | **Notice**          |
| ------------- | ---------------------------------------------------------- | -------------- | ------------- | ------------------- |
| ap\_ary       | List of AP information in the current environment.         | AP\_IF\_S \*\* | Yes           | As output parameter |
| num           | The length of the current environment AP information list. | UINT\_T        | Yes           | As output parameter |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_assign\_ap\_scan**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_assign_ap_scan(IN CONST CHAR_T *ssid, OUT AP_IF_S **ap);
```

**Description**

Scans the specified AP.

**Parameter**

| **Parameter** | **Description**                    | **Data type**  | **Required?** | **Notice**      |
| ------------- | ---------------------------------- | -------------- | ------------- | --------------- |
| ssid          | The ssid of the specified AP.      | String         | Yes           | Input Parameter |
| ap            | Gets the specified AP information. | AP\_IF\_S \*\* | Yes           | Output          |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_release\_ap**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_release_ap(IN AP_IF_S *ap);
```

**Description**

Release processing of resources.

**Parameter**

| **Parameter** | **Description**                                                                                                                     | **Data type** | **Required?** | **Notice** |
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ------------- | ------------- | ---------- |
| ap            | When the ap information is useless, release the memory requested in `tuya_hal_wifi_all_ap_scan` and `tuya_hal_wifi_assign_ap_scan`. | AP\_IF\_S \*  | Yes           | N/A        |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_set\_cur\_channel**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_set_cur_channel(IN CONST BYTE_T chan);
```

**Description**

Sets the Wi-Fi working channel.

**Parameter**

| **Parameter** | **Description**                 | **Data type** | **Required?** | **Notice** |
| ------------- | ------------------------------- | ------------- | ------------- | ---------- |
| chan          | Wi-Fi working channel to be set | BYTE\_T       | Yes           | N/A        |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_get\_cur\_channel**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_get_cur_channel(OUT BYTE_T *chan);
```

**Description**

Gets the current working channel.

**Parameter**

| **Parameter** | **Description**                                                                                 | **Data type** | **Required?** | **Notice**                   |
| ------------- | ----------------------------------------------------------------------------------------------- | ------------- | ------------- | ---------------------------- |
| nw\_stat\_cb  | Network change callback function, the parameter of the callback function is the network status. | nw\_stat\_cb  | Yes           | See SDK samples for details. |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_sniffer\_set**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_sniffer_set(IN CONST BOOL_T en, IN CONST SNIFFER_CALLBACK cb);
```

**Description**

Wi-Fi device promiscuous mode setting.

**Parameter**

| **Parameter** | **Description**                                                                           | **Data type**     | **Required?** | **Notice** |
| ------------- | ----------------------------------------------------------------------------------------- | ----------------- | ------------- | ---------- |
| en            | Enables or disables Wi-Fi sniffer mode                                                    | nw\_stat\_cb      | Yes           | N/A        |
| cb            | Notification callback function. The captured 802.11 air packet contains the frame header. | SNIFFER\_CALLBACK | Yes           | N/A        |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_get\_ip**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_get_ip(IN CONST WF_IF_E wf, OUT NW_IP_S *ip);
```

**Description**

Gets Wi-Fi device ip information.

**Parameter**

| **Parameter** | **Description**                                                                             | **Data type** | **Required?** | **Notice**        |
| ------------- | ------------------------------------------------------------------------------------------- | ------------- | ------------- | ----------------- |
| wf            | Wi-Fi working mode , AP and station.                                                        | WF\_IF\_E     | Yes           | N/A               |
| ip            | Saves the obtained IP information structure, the information contains IP, submask, gateway. | NW\_IP\_S \*  | Yes           | Output parameters |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_get\_mac**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_get_mac(IN CONST WF_IF_E wf, OUT NW_MAC_S *mac);
```

**Description**

Gets the MAC address of the Wi-Fi device.

**Parameter**

| **Parameter** | **Description**                    | **Data type** | **Required?** | **Notice**          |
| ------------- | ---------------------------------- | ------------- | ------------- | ------------------- |
| wf            | Wi-Fi working mode. AP and station | nw\_stat\_cb  | Yes           | N/A                 |
| mac           | Saves the obtained MAC address     | NW\_MAC\_S \* | Yes           | As output parameter |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_set\_mac**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_set_mac(IN CONST WF_IF_E wf,IN CONST NW_MAC_S *mac);
```

**Description**

Sets the MAC address of the Wi-Fi device is optional.

**Parameter sescription**

| **Parameter** | **Description**                    | **Data type** | **Required?** | **Notice**       |
| ------------- | ---------------------------------- | ------------- | ------------- | ---------------- |
| wf            | Wi-Fi working mode, AP and station | WF\_IF\_E     | Yes           | Input parameters |
| mac           | The set MAC address                | NW\_MAC\_S \* | Yes           | Input parameters |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_wk\_mode\_set**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_wk_mode_set(IN CONST WF_WK_MD_E mode);
```

**Description**

Sets the Wi-Fi working mode.

**Parameter**

| **Parameter** | **Description**                                                                                                    | **Data type** | **Required?** | **Notice** |
| ------------- | ------------------------------------------------------------------------------------------------------------------ | ------------- | ------------- | ---------- |
| mode          | Sets the Wi-Fi working mode: 1. Low power mode 2. Promiscuous mode 3. station mode 4. ap mode 5. ap + station mode | WF\_WK\_MD\_E | Yes           | N/A        |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_wk\_mode\_get**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_wk_mode_get(OUT WF_WK_MD_E *mode);
```

**Description**

Gets Wi-Fi working mode.

**Parameter**

| **Parameter** | **Description**                                                      | **Data type** | **Required?** | **Notice**        |
| ------------- | -------------------------------------------------------------------- | ------------- | ------------- | ----------------- |
| mode          | Same meaning as the parameter mode in tuya\_hal\_wifi\_wk\_mode\_set | WF\_WK\_MD\_E | Yes           | Output parameters |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_station\_connect**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_station_connect(IN CONST CHAR_T *ssid, IN CONST CHAR_T *passwd);
```

**Description**

Sets up the Wi-Fi device to establish a connection with the AP/hotspot/Wi-Fi router.

**Parameter**

| **Parameter** | **Description**                                        | **Data type** | **Required?** | **Notice** |
| ------------- | ------------------------------------------------------ | ------------- | ------------- | ---------- |
| ssid          | The ssid connected to the ap/hotspot/Wi-Fi router      | String        | Yes           | N/A        |
| password      | The password to connect to the ap/hotspot/Wi-Fi router |               | Yes           | N/A        |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_station\_disconnect**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_station_disconnect(VOID);
```

**Description**

Disconnect the Wi-Fi device from the AP/hotspot/Wi-Fi router.

**Parameter**

VOID.

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_station\_get\_conn\_ap\_rssi**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_station_get_conn_ap_rssi(OUT SCHAR_T *rssi);
```

**Description**

Gets the signal strength of the Wi-Fi device in station mode connected to the AP.

**Parameter**

| **Parameter** | **Description** | **Data type** | **Required?** | **Notice**       |
| ------------- | --------------- | ------------- | ------------- | ---------------- |
| rssi          | obtained rssi   | SCHAR\_T\*    | Yes           | output parameter |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_station\_stat\_get**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_station_stat_get(OUT WF_STATION_STAT_E *stat);
```

**Description**

Gets the connection status of the Wi-Fi device in station mode.

**Parameter**

| **Parameter** | **Description**                                                                                                                                                                                 | **Data type**           | **Required?** | **Notice**        |
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------- | ------------- | ----------------- |
| stat          | Wi-Fi and ap connection status in `Station` mode, which includes Not connected, Connecting, Wrong password, ap not found, Connection failed, Successful connection, and Successful acquisition. | WF\_STATION\_STAT\_E \* | Yes           | Output parameters |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_set\_country\_code**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_set_country_code(IN CONST CHAR_T *p_country_code);
```

**Function sescription**

Sets the country code.

**Parameter sescription**

| **Parameter**    | **Description**                                                                         | **Data type** | **Required?** | **Notice** |
| ---------------- | --------------------------------------------------------------------------------------- | ------------- | ------------- | ---------- |
| p\_country\_code | The frequency and channel signal strength of Wi-Fi devices vary in different countries. | CHAR\_T \*    | Yes           | N/A        |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_ap\_start**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_ap_start(IN CONST WF_AP_CFG_IF_S *cfg);
```

**Description**

Starts the Wi-Fi AP hotspot.

**Parameter**

| **Parameter** | **Description**                                                                                                                                                       | **Data type**         | **Required?** | **Notice** |
| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------- | ------------- | ---------- |
| cfg           | Start as a configuration parameter in AP mode. The ssid, password, channel, encryption method, whether to hide ssid, and maximum number of connections are supported. | WF\_AP\_CFG\_IF\_S \* | Yes           | N/A        |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_hal\_wifi\_ap\_stop**

**Prototype**

```
OPERATE_RET tuya_hal_wifi_ap_stop(VOID);
```

**Description**

Stop Wi-Fi AP hotspot.

**Parameter**

None.

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**tuya\_iot\_gw\_wf\_user\_cfg**

**Prototype**

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

**Description**

When the network is not configured in the AP or EZ mode, call this interface for processing.

**Parameter**

| **Parameter** | **Description**                                         | **Data type** | **Required?** | **Notice** |
| ------------- | ------------------------------------------------------- | ------------- | ------------- | ---------- |
| ssid          | The ssid used by the network in AP mode.                | String        | Yes           | N/A        |
| passwd        | The password used for network configuration in AP mode. | String        | Yes           | N/A        |
| token         | The token used by the distribution network in AP mode.  | String        | Yes           | N/A        |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

### Device reset <a href="#title-11-device-20reset" id="title-11-device-20reset"></a>

#### APP reset <a href="#title-12-app-20reset" id="title-12-app-20reset"></a>

You can reset a device via App. The APP reset APIs send a reset message through MQTT to reset the device.

#### Local reset <a href="#title-13-local-20reset" id="title-13-local-20reset"></a>

Alternatively, the SDK retains the ability to reset the gateway locally, for example, you can reset the device locally by pressing buttons.

**tuya\_iot\_gw\_unactive**

```
OPERATE_RET tuya_iot_gw_unactive(VOID);
```

**Description**

Resets the device to unassociate the gateway with the App, so that the gateway is in an inactive state.

> **Note**: This function is asynchronous, and it will trigger the [gw\_reset\_cb](https://docs.tuya.com/en/iot/device-development/access-mode-link/wifi-chip-sdk/tuya-common-wifi-sdk?id=K9glcmvw4u9ml#gw_reset_cb) callback after execution, you can restart the thread in the callback.

**Parameter**

None.

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

### Firmware upgrade <a href="#title-14-firmware-20upgrade" id="title-14-firmware-20upgrade"></a>

#### The process <a href="#title-15-the-20process" id="title-15-the-20process"></a>

**Configuration of firmware package**

1. After the gateway or device is successfully configured, App obtains the virtual ID of the device, and add it to the white list for firmware upgrade.
2. Compile the firmware package that is to be upgraded, and the firmware version is newer than the firm that is running in the device.
3. Log on to the [Tuya IoT console](https://iot.tuya.com/index), find the relevant product, and upload the compiled firmware package.
4. Perform the follow-ups. For more information, see [Upgrade Firmwares](https://docs.tuya.com/zh/iot/configure-in-platform/advanced-features/firmware-upgrade-operation-guide?id=K93ixsft1w3to).

**How the upgrade starts**

After the firmware is uploaded to cloud, your devices will not receive the upgrade notification immediately. At present, the following upgrade notification is available.

* App notification: when your users switch to the App panel of the devices, they receive an upgrade reminder box and can choose to upgrade or not.
* Silent upgrade: after the devices are restarted, they will ask the cloud if there is a silent upgrade task, and complete the upgrade automatically. If your users happen to switch to the App panel of the devices during the upgrade, an progress tracker will be displayed, and the device cannot be operated for a while.
* Forcible upgrade: when your users switch to the App panel of the devices for the first time, they receive the upgrade reminder pop-up and only confirmation is provided. The equipment cannot be operated for a while.
* Upgrade detection: your users switch to the App panels of the devices and click the upper right corner to enter the information center to check the firmware version of the device, and proactively update the firmware.

**The firmware upgrade process**

![Tuya Wi-Fi SDK Reference](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200608/e4e1d42025594256b230d980ffea5e50.png)

#### API reference <a href="#title-16-api-20reference" id="title-16-api-20reference"></a>

**tuya\_iot\_upgrade\_gw\_notify**

**Prototype**

```
OPERATE_RET tuya_iot_upgrade_gw(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);
```

**Description**

The networking module firmware upgrade processing interface.

**Parameter**

| **Parameter**       | **Description**                                           | **Data type**               | **Required?** | **Notice**                                                              |
| ------------------- | --------------------------------------------------------- | --------------------------- | ------------- | ----------------------------------------------------------------------- |
| fw                  | The firmware information                                  | FW\_UG\_S structure pointer | Yes           | N/A.                                                                    |
| get\_file\_cb       | Downloads the file to store.                              | Callback function           | Yes           | N/A.                                                                    |
| upgrd\_nofity\_cb   | Notifies the upgrade status of the application            | Callback function           | Yes           | N/A.                                                                    |
| pri\_data           | Parameters passed to `get_file_cb` and `upgrd_nofity_cb`. | Pointer                     | No            | If no parameters are passed, set to NULL.                               |
| notify              | Chooses whether to report upgrade progress via SDK.       | Boolean                     | Yes           | TRUE is reported by the SDK, FALSE is reported by the application layer |
| download\_buf\_size | Download maximum cache, unit: byte.                       | UINT\_T                     | No            | Pass in 0, SDK default cache size                                       |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Success                                |
| Error Code       | The error code is returned on failure. |

**tuya\_iot\_dev\_upgd\_progress\_rept**

**Prototype**

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

**Description**

Reports the upgrade progress.

**Parameter**

| **Parameter** | **Description**                                                                                   | **Data type** | **Required?** | **Notice**                |
| ------------- | ------------------------------------------------------------------------------------------------- | ------------- | ------------- | ------------------------- |
| percent       | Upgrade progress value                                                                            | UINT\_T       | Yes           | 0\~99                     |
| devid         | When the sub-device is passed, the device ID of the sub-device; When the gateway is passed, NULL. | CHAR\_T \*    | Yes           | Refer to the SDK samples. |
| tp            | Device type                                                                                       | DEV\_TYPE\_T  | Yes           | Refer to the SDK samples. |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Success                                |
| Error Code       | The error code is returned on failure. |

### Data point of devices <a href="#title-17-data-20point-20of-20devices" id="title-17-data-20point-20of-20devices"></a>

#### The process <a href="#title-18-the-20process" id="title-18-the-20process"></a>

Tuya Smart provides a network application protocol based on MQTT to achieve device control and status reporting. MQTT is a lightweight message transmission protocol designed specifically for the Internet of things applications in low bandwidth and unstable network environment.

The Tuya Wi-Fi SDK encapsulates the APIs for MQTT protocol layer, which is presented in the form of data points (hereinafter referred to as DP points). It supports data type of integer, Boolean, enum, string, bitmap, and raw, which are the same as defining variables in C programming language.

You must create corresponding data points on the Tuya IoT console according to the device functions, and create a new DP Point description. For more information, see [Overview of function definition](https://docs.tuya.com/en/iot/configure-in-platform/function-definition/define-product-features?id=K97vug7wgxpoq).

#### Features <a href="#title-19-features" id="title-19-features"></a>

* At present, up to 35 DPs for each product are supported. To develop complex functions, the raw data is recommended.
* The objects such as Boolean, integer, string, enum, and bitmap, the Tuya Wi-Fi SDK filters the reported values, and the repeated data will be dropped.

#### API reference <a href="#title-20-api-20reference" id="title-20-api-20reference"></a>

**dev\_obj\_dp\_cb**

**Prototype**

```
VOID dev_obj_dp_cb(IN CONST TY_RECV_OBJ_DP_S *dp);
```

**Description**

The OBJ function point information command callback.

**Parameter**

| **Parameter** | **Description**                                                                                                                                                                                                                                                                          | **Data type**          | **Required?** | **Notice**                                                                                              |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | ------------- | ------------------------------------------------------------------------------------------------------- |
| dp            | TY\_RECV\_OBJ\_DP\_S contains the dp command type, device ID controlled by dp point, the mb\_id group ID when the dtt\_tp is multi-broadcasted, the length of function point structure array, and the function point structure array. For more information, see `tuya_cloud_com_defs.h`. | TY\_RECV\_OBJ\_DP\_S\* | Yes           | You can focus on the function point structure arrays. Function points are defined on the Tuya platform. |

**Return value**

| **Return value** | **Description**                |
| ---------------- | ------------------------------ |
| None             | No return value                |
| Error Code       | Error Code Returned on failure |

**dev\_raw\_dp\_cb**

**Prototype**

```
VOID dev_raw_dp_cb(IN CONST TY_RECV_RAW_DP_S *dp);
```

**Description**

The command callback for function points passthrough.

**Parameter**

| **Parameter** | **Description**                                                                                                                                                                                                                                                                                           | **Data type**          | **Required?** | **Notice** |
| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | ------------- | ---------- |
| dp            | The structure contains the dp command type, the device ID controlled by DP, the mb\_id group ID when the dtt\_tp is multi-broadcasted, the Number of data bytes passed through, the passed through data types, and the function point structure array. For more information, see `tuya_cloud_com_defs.h`. | TY\_RECV\_RAW\_DP\_S\* | Yes           | N/A        |

**Return value**

| **Return value** | **Description** |
| ---------------- | --------------- |
| VOID             | No return value |

**dev\_dp\_query\_cb**

**Prototype**

```
VOID dev_dp_query_cb(IN CONST TY_DP_QUERY_S *dp_qry);
```

**Description**

The device specific data query entry. It is an optional implementation.

**Parameter**

| **Parameter name** | **Description** | **Parameter type** | **Whether it is mandatory** | **Notice** |
| ------------------ | --------------- | ------------------ | --------------------------- | ---------- |
| dp\_qry            |                 | TY\_DP\_QUERY\_S\* | Yes                         | N/A        |

**Return value**

| **Return value** | **Description** |
| ---------------- | --------------- |
| VOID             | No return value |

**dev\_report\_dp\_json\_async**

**Prototype**

```
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);
```

**Description**

The report function point information in an asynchronous way.

**Parameter**

| **Parameter** | **Description**                     | **Data type**                          | **Required?** | **Notice**                                                                                                                |
| ------------- | ----------------------------------- | -------------------------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------- |
| dev\_id       | The device ID.                      | String                                 | Yes           | If it is a child device, this parameter is the ID of the child device. If it is a gateway or MCU, this parameter is NULL. |
| dp\_data      | The function point data structure.  | TY\_OBJ\_DP\_S structure array pointer | Yes           | N/A                                                                                                                       |
| cnt           | Number of dp\_data structure arrays | UINT\_T                                | Yes           | N/A                                                                                                                       |

**Return value**

| **Return value** | **Description**                |
| ---------------- | ------------------------------ |
| OPRT\_OK         | Operation succeeded.           |
| Error Code       | Error Code Returned on failure |

**dev\_report\_dp\_raw\_sync**

**Prototype**

```
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);
```

**Description**

The device data reporting interface in the passthrough method, and the caller guarantees the reliability of data reporting.

**Parameter**

| **Parameter** | **Description**          | **Data type** | **Required?** | **Notice**                                                                                                                |
| ------------- | ------------------------ | ------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------- |
| dev\_id       | The device ID.           | String        | Yes           | If it is a child device, this parameter is the ID of the child device. If it is a gateway or MCU, this parameter is NULL. |
| dpid          | function point id        | UINT\_T       | Yes           | N/A                                                                                                                       |
| data          | data                     | BYTE\_T \*    | Yes           | N/A                                                                                                                       |
| len           | Data length              | UINT\_T       | Yes           | N/A                                                                                                                       |
| timeout       | Function plunger timeout | UINT\_T       | Yes           | Unit: seconds.                                                                                                            |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**dev\_report\_dp\_stat\_sync**

**Prototype**

```
OPERATE_RET dev_report_dp_stat_sync(IN CONST CHAR_T *dev_id, IN CONST TY_OBJ_DP_S *dp_data, IN CONST UINT_T cnt, 
                                    IN CONST UINT_T timeout);
```

**Description**

The structured device data reporting interface in the passthrough method, and the caller guarantees the reliability of data reporting. It is usually used for statistical data reporting.

**Parameter**

| **Parameter** | **Description**                            | **Data type**     | **Required?** | **Notice**                                                                                                                |
| ------------- | ------------------------------------------ | ----------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------- |
| dev\_id       | The device ID.                             | String            | Yes           | If it is a child device, this parameter is the ID of the child device. If it is a gateway or MCU, this parameter is NULL. |
| dp\_data      | Function point information structure array | TY\_OBJ\_DP\_S \* | Yes           | N/A                                                                                                                       |
| cnt           | Dp state array length                      | UINT\_T           | Yes           | N/A                                                                                                                       |
| timeout       | Function plunger timeout                   | UINT\_T           | Yes           | Units: seconds.                                                                                                           |

**Return value**

| **Return value** | **Description**                |
| ---------------- | ------------------------------ |
| OPRT\_OK         | Operation succeeded.           |
| Error Code       | Error Code Returned on failure |

### Log management <a href="#title-21-log-20management" id="title-21-log-20management"></a>

The Tuya Wi-Fi SDK can collect and redirect logs. By default, the log is printed to a standard terminals and can be redirected if needed, such as printing to a file or the cloud.

**AddOutputTerm**

**Prototype**

```
OPERATE_RET AddOutputTerm(IN CONST CHAR_T *name, IN CONST LOG_OUTPUT term);
```

**Description**

Adds an output callback for the tuya\_SDK log to write the log to a file.

**Parameter**

| **Parameter** | **Description**              | **Data type** | **Required** | **Notice**                                         |
| ------------- | ---------------------------- | ------------- | ------------ | -------------------------------------------------- |
| name          | The name of the log callback | String        | Yes          | Used for DelOutputTerm to unregister this callback |
| term          | LOG\_OUTPUT                  | callback      | Yes          | N/A                                                |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

**DelOutputTerm**

**Prototype**

```
VOID DelOutputTerm(IN CONST CHAR_T *name);
```

**Description**

Unregisters the log callback registered by the AddOutputTerm interface.

**Parameter**

| **Parameter** | **Description**               | **Data type** | **Required?** | **Notice**                                                      |
| ------------- | ----------------------------- | ------------- | ------------- | --------------------------------------------------------------- |
| name          | The name of the log callback. | String        | Yes           | Unregisters log callback registered by AddOutputTerm interface. |

**Return value** VOID

**SetLogManageAttr**

**Prototype**

```
OPERATE_RET SetLogManageAttr(IN CONST LOG_LEVEL curLogLevel);
```

**Description**

Sets the log level.

**Parameter**

| **Parameter** | **Description** | **Data type** | **Required?** | **Notice**                  |
| ------------- | --------------- | ------------- | ------------- | --------------------------- |
| curLogLevel   | Log level       | UINT          | Yes           | See uni\_log.h instructions |

**Return value**

| **Return value** | **Description**                        |
| ---------------- | -------------------------------------- |
| OPRT\_OK         | Operation succeeded.                   |
| Error Code       | The error code is returned on failure. |

### Timer <a href="#title-22-timer" id="title-22-timer"></a>

**Add a system timer**

**Prototype**

```
OPERATE_RET sys_add_timer(IN CONST P_TIMER_FUNC pTimerFunc,
IN CONST PVOID pTimerArg, OUT TIMER_ID *p_timerID);
```

**Description**

Adds a system timer.

**Parameter**

| **Parameter** | **Description**             | **Data type**      | **Required?** | **Notice**                                                                                    |
| ------------- | --------------------------- | ------------------ | ------------- | --------------------------------------------------------------------------------------------- |
| pTimerFunc    | Timer handler function      | **P\_TIMER\_FUNC** | Yes           | Pointer to callback function for timer aging call                                             |
| pTimerArg     | Timer processing parameters | **PVOID**          | Yes           | Timer related parameters, the specific type is determined internally by the callback function |
| p\_timerID    | Timer ID number             | **TIMER\_ID \***   | Yes           | Output parameter, add timer ID successfully returned                                          |

**Return value**

| **Return value** | **Description**            |
| ---------------- | -------------------------- |
| **OPERATE\_RET** | **Error code information** |

**sys\_stop\_timer**

**Prototype**

```
OPERATE_RET sys_stop_timer(IN CONST TIMER_ID timerID));
```

**Description**

Stops a timer.

**Parameter**

| **Paramete** | **Description** | **Data type**    | **Required?** | **Notice**                             |
| ------------ | --------------- | ---------------- | ------------- | -------------------------------------- |
| timerID      | timer ID number | **TIMER\_ID \*** | Yes           | The timer ID that needs to be stopped. |

**Return value**

| **Return value** | **Description**            |
| ---------------- | -------------------------- |
| **OPERATE\_RET** | **Error code information** |

**IsThisSysTimerRun**

**Prototype**

```
BOOL IsThisSysTimerRun(IN CONST TIMER_ID timerID);
```

**Description**

Determines whether a timer is running.

**Parameter**

| **Parameter** | **Description** | **Data type**    | **Required?** | **Notice**                                              |
| ------------- | --------------- | ---------------- | ------------- | ------------------------------------------------------- |
| timerID       | timer ID number | **TIMER\_ID \*** | Yes           | Your need to determine whether the timer ID is running. |

**Return value**

| **Return value** | **Description**                   |
| ---------------- | --------------------------------- |
| **BOOL**         | **Whether the timing is running** |

**sys\_start\_timer**

**Prototype**

```
OPERATE_RET sys_start_timer(IN CONST TIMER_ID timerID, IN CONST TIME_MS timeCycle, IN CONST TIMER_TYPE timer_type);
```

**Description**

Starts a timer.

**Parameter**

| **Parameter** | **Description** | **Data type**    | **Required?** | **Notice**                                                         |
| ------------- | --------------- | ---------------- | ------------- | ------------------------------------------------------------------ |
| timerID       | timer ID number | **TIMER\_ID \*** | Yes           | need to determine whether the timer id running timer               |
| timeCycle     | Timed period    | **TIME\_MS**     | Yes           | Timed period, (in milliseconds)                                    |
| timer\_type   | timer type      | **TIMER\_TYPE**  | Yes           | <p>TIMER\_ONCE single execution<br>TIMER\_CYCLE loop execution</p> |

**Return value**

| **Return value** | **Description**                   |
| ---------------- | --------------------------------- |
| **BOOL**         | **Whether the timing is running** |

### message queue <a href="#title-23-message-20queue" id="title-23-message-20queue"></a>

**CreateMsgQueAndInit**

**Prototype**

```
OPERATE_RET CreateMsgQueAndInit(OUT MSG_QUE_HANDLE *pMsgQueHandle);
```

**Description**

Createa message queues.

**Parameter**

| **Parameter**     | **Description**                     | **Data type**           | **Required?** | **Notice**                                                  |
| ----------------- | ----------------------------------- | ----------------------- | ------------- | ----------------------------------------------------------- |
| **pMsgQueHandle** | Message management structure handle | **MSG\_QUE\_HANDLE \*** | Yes           | Output parameter, handle returned after successful creation |

**Return value**

| **Return value** | **Description**            |
| ---------------- | -------------------------- |
| **OPERATE\_RET** | **Error code information** |

**AddMsgNodeToQueue**

**Prototype**

```
OPERATE_RET AddMsgNodeToQueue(IN CONST MSG_QUE_HANDLE msgQueHandle, IN CONST MSG_ID msgID, IN CONST P_MSG_DATA pMsgData,IN CONST MSG_DATA_LEN msgDataLen, IN CONST MSG_TYPE msgType);
```

**Description**

Creates message queues.

**Parameter**

| **Parameter** | **Description**                     | **Data type**        | **Required?** | **Notice**                          |
| ------------- | ----------------------------------- | -------------------- | ------------- | ----------------------------------- |
| pMsgQueHandle | Message management structure handle | **MSG\_QUE\_HANDLE** | Yes           | Create handle returned successfully |
| msgID         | Message ID                          | **MSG\_ID**          | Yes           | N/A                                 |
| pMsgData      | Message data                        | **P\_MSG\_DATA**     | Yes           | N/A                                 |
| msgDataLen    | Message data length                 | **MSG\_DATA\_LEN**   | Yes           | N/A                                 |
| msgType       | Message Type                        | **MSG\_TYPE**        | Yes           | N/A                                 |

**Return value**

| **Return value** | **Description**            |
| ---------------- | -------------------------- |
| **OPERATE\_RET** | **Error code information** |

**GetMsgNodeFromQueue**

**Prototype**

```
OPERATE_RET GetMsgNodeFromQueue(IN CONST MSG_QUE_HANDLE msgQueHandle, IN CONST MSG_ID msgID, OUT P_MSG_LIST *ppMsgListNode);
```

**Description**

Gets the message node of the specified ID.

**Parameter**

| **Parameter** | **Description**                     | **Data type**        | **Required?** | **Notice**                                |
| ------------- | ----------------------------------- | -------------------- | ------------- | ----------------------------------------- |
| pMsgQueHandle | Message management structure handle | **MSG\_QUE\_HANDLE** | Yes           | Handle returned after successful creation |
| msgID         | Message ID                          | **MSG\_ID**          | Yes           |                                           |
| pMsgListNode  | Message node                        | **P\_MSG\_LIST \***  | Yes           | Output parameter, obtained message node   |

**Return value**

| **Return value** | **Description**            |
| ---------------- | -------------------------- |
| **OPERATE\_RET** | **Error code information** |

**GetFirstMsgFromQueue**

**Prototype**

```
OPERATE_RET GetFirstMsgFromQueue(IN CONST MSG_QUE_HANDLE msgQueHandle, OUT P_MSG_LIST *ppMsgListNode);
```

**Description**

Gets the message node that first enters the chain.

**Parameter**

| **Parameter** | **Description**                     | **Data type**        | **Required?** | **Notice**                                |
| ------------- | ----------------------------------- | -------------------- | ------------- | ----------------------------------------- |
| pMsgQueHandle | Message management structure handle | **MSG\_QUE\_HANDLE** | Yes           | Handle returned after successful creation |
| pMsgListNode  | Message node                        | **P\_MSG\_LIST \***  | Yes           | Output parameter, obtained message node   |

**Return value**

| **Return value** | **Description**            |
| ---------------- | -------------------------- |
| **OPERATE\_RET** | **Error code information** |

**GetMsgNodeNum**

**Prototype**

```
OPERATE_RET GetMsgNodeNum(IN CONST MSG_QUE_HANDLE msgQueHandle,OUT PINT pMsgNodeNum);
```

**Description**

Gets the total number of message nodes in the linked list.

**Parameter**

| **Parameter** | **Description**                     | **Data type**        | **Required?** | **Notice**                                                |
| ------------- | ----------------------------------- | -------------------- | ------------- | --------------------------------------------------------- |
| pMsgQueHandle | Message management structure handle | **MSG\_QUE\_HANDLE** | Yes           | Handle returned after successful creation                 |
| pMsgNodeNum   | Total number of message nodes       | **PINT**             | Yes           | Output parameters, total number of message nodes obtained |

**Return value**

| **Return value** | **Description**            |
| ---------------- | -------------------------- |
| **OPERATE\_RET** | **Error code information** |

**DelAndFreeMsgNodeFromQueue**

**Prototype**

```
OPERATE_RET DelAndFreeMsgNodeFromQueue(IN CONST MSG_QUE_HANDLE msgQueHandle, IN CONST P_MSG_LIST pMsgListNode);
```

**Description**

Removes the message node from the chain and free the message node memory.

**Parameter**

| **Parameter** | **Description**                     | **Data type**        | **Required?** | **Notice**                                 |
| ------------- | ----------------------------------- | -------------------- | ------------- | ------------------------------------------ |
| pMsgQueHandle | Message management structure handle | **MSG\_QUE\_HANDLE** | Yes           | Handle returned after successful creation. |
| pMsgListNode  | Deleted message node                | **P\_MSG\_LIST**     | Yes           | Deleted message node                       |

**Return value**

| **Return value** | **Description**            |
| ---------------- | -------------------------- |
| **OPERATE\_RET** | **Error code information** |

**ReleaseMsgQue**

**Prototype**

```
OPERATE_RET ReleaseMsgQue(IN CONST MSG_QUE_HANDLE msgQueHandle);
```

**Description**

Releases the memory occupied by the message queue.

**Parameter**

| **Parameter** | **Description**                     | **Data type**        | **Required?** | **Notice**                                |
| ------------- | ----------------------------------- | -------------------- | ------------- | ----------------------------------------- |
| pMsgQueHandle | Message management structure handle | **MSG\_QUE\_HANDLE** | Yes           | Handle returned after successful creation |

**Return value**

| **Return value** | **Description**            |
| ---------------- | -------------------------- |
| **OPERATE\_RET** | **Error code information** |

**PostMessage**

**Prototype**

```
OPERATE_RET PostMessage(IN CONST MSG_QUE_HANDLE msgQueHandle, IN CONST MSG_ID msgID, IN CONST P_MSG_DATA pMsgData, IN CONST MSG_DATA_LEN msgDataLen);
```

**Description**

Delivers a message to the module (the message is executed first).

**Parameter dDescription**

| **Parameter** | **Description**           | **Data type**        | **Required?** | **Notice**                                |
| ------------- | ------------------------- | -------------------- | ------------- | ----------------------------------------- |
| msgQueHandle  | Message processing handle | **MSG\_QUE\_HANDLE** | Yes           | Handle returned after successful creation |
| msgID         | Message ID                | **MSG\_ID**          | Yes           | N/A                                       |
| pMsgData      | Message data              | **P\_MSG\_DATA**     | Yes           | N/A                                       |
| msgDataLen    | Message data length       | **MSG\_DATA\_LEN**   | Yes           | N/A                                       |

**Return value**

| **Return value** | **Description**            |
| ---------------- | -------------------------- |
| **OPERATE\_RET** | **Error code information** |

**PostInstancyMsg**

**Prototype**

```
OPERATE_RET PostInstancyMsg(IN CONST MSG_QUE_HANDLE msgQueHandle, IN CONST MSG_ID msgID, IN CONST P_MSG_DATA pMsgData, IN CONST MSG_DATA_LEN msgDataLen);
```

**Description**

Delivers urgent messages (messages are executed immediately).

**Parameter**

| **Parameter** | **Description**           | **Data type**        | **Required?** | **Notice**                                |
| ------------- | ------------------------- | -------------------- | ------------- | ----------------------------------------- |
| msgQueHandle  | Message processing handle | **MSG\_QUE\_HANDLE** | Yes           | Handle returned after successful creation |
| msgID         | Message ID                | **MSG\_ID**          | Yes           | N/A                                       |
| pMsgData      | Message data              | **P\_MSG\_DATA**     | Yes           | N/A                                       |
| msgDataLen    | Message data length       | **MSG\_DATA\_LEN**   | Yes           | N/A                                       |

**Return value**

| **Return value** | **Description**            |
| ---------------- | -------------------------- |
| **OPERATE\_RET** | **Error code information** |

**WaitMessage**

**Prototype**

```
OPERATE_RET WaitMessage(IN CONST MSG_QUE_HANDLE msgQueHandle, OUT P_MSG_LIST *ppMsgListNode);
```

**Description**

Delivers urgent messages (messages are executed immediately). `WaitMessage` needs to be called successfully, and DelAndFreeMsgNodeFromQueue needs to be called after the message is processed to release the message

**Parameter**

| **Parameter** | **Description**           | **Data type**        | **Required?** | **Notice**                                |
| ------------- | ------------------------- | -------------------- | ------------- | ----------------------------------------- |
| msgQueHandle  | Message processing handle | **MSG\_QUE\_HANDLE** | Yes           | Handle returned after successful creation |
| ppMsgListNode | Message node              | **P\_MSG\_LIST \***  | Yes           | Output parameters, message node received  |

**Return value**

| **Return value** | **Description**            |
| ---------------- | -------------------------- |
| **OPERATE\_RET** | **Error code information** |

### Task queue <a href="#title-24-task-20queue" id="title-24-task-20queue"></a>

**CreateAndStart**

**Prototype**

```
OPERATE_RET CreateAndStart(OUT THRD_HANDLE *pThrdHandle, IN CONST P_THRD_FUNC pThrdFunc,
IN CONST PVOID pThrdFuncArg, IN CONST STACK_SIZE stack_size,IN CONST TRD_PRI pri,
IN CONST CHAR *thrd_name);;

```

**Description**

Creates a task.

**Parameter**

| **Parameter** | **Description**             | **Data type**        | **Required?** | **Notice**                                |
| ------------- | --------------------------- | -------------------- | ------------- | ----------------------------------------- |
| pThrdHandle   | pThrdHandle task handle     | **MSG\_QUE\_HANDLE** | Yes           | Handle returned after successful creation |
| pThrdFunc     | Task processing function    | **P\_THRD\_FUNC**    | Yes           | N/A                                       |
| pThrdFuncArg  | Task parameters             | **PVOID**            | Yes           | N/A                                       |
| stack\_size   | Specify the task stack size | **STACK\_SIZE**      | Yes           | N/A                                       |
| pri           | Task priority               | **TRD\_PRI**         | Yes           | N/A                                       |
| thrd\_name    | Task name                   | **CHAR \***          | Yes           | N/A                                       |

**return value**

| **Return value** | **Description**            |
| ---------------- | -------------------------- |
| **OPERATE\_RET** | **Error code information** |

**ThrdJoin**

**Prototype**

```
OPERATE_RET ThrdJoin(IN CONST THRD_HANDLE thrdHandle, OUT VOID **ppThrdRet);;

```

**Description**

Creates a task.

**Parameter**

| **Parameter** | **Description**         | **Data type**        | **Required?** | **Notice**                                |
| ------------- | ----------------------- | -------------------- | ------------- | ----------------------------------------- |
| thrdHandle    | pThrdHandle task handle | **MSG\_QUE\_HANDLE** | Yes           | Handle returned after successful creation |
| ppThrdRet     | Task exit code          | **VOID \***\*        | Yes           |                                           |

**Return value**

| **Return value** | **Description**            |
| ---------------- | -------------------------- |
| **OPERATE\_RET** | **Error code information** |

### HAL interface <a href="#title-25-hal-20interface" id="title-25-hal-20interface"></a>

The Tuya Device OS is a platform-independent device-to-cloud operating system. To achieve platform independence, you need to implement the underlying abstract interface according to your systems. For specific implementation, refer to [Tuya\_GM\_WIFI\_SDK development environment adaptation Description](https://docs.tuya.com/en/iot/device-development/access-mode-link/wifi-chip-sdk/tuya-common-sdk-hal-adapter?id=K9mtiqg2uk5qz).

### SDK samples <a href="#title-26-sdk-20samples" id="title-26-sdk-20samples"></a>

The Tuya Wi-Fi SDK provides a dedicated Demo to guide you to use. For specific steps, please refer to [Tuya\_Wi-Fi\_SDK Demo Description](https://docs.tuya.com/en/iot/device-development/access-mode-link/wifi-chip-sdk/tuya-common-wifi-sdk?id=K9glcmvw4u9ml).

### Error codes <a href="#title-27-error-20codes" id="title-27-error-20codes"></a>

| **Error Code**                                       | **Error Code Value** |
| ---------------------------------------------------- | -------------------- |
| OPRT\_OK                                             | (0)                  |
| OPRT\_COM\_ERROR                                     | (-1)                 |
| OPRT\_INVALID\_PARM                                  | (-2)                 |
| OPRT\_MALLOC\_FAILED                                 | (-3)                 |
| OPRT\_NOT\_SUPPORTED                                 | (-4)                 |
| OPRT\_NETWORK\_ERROR                                 | (-5)                 |
| OPRT\_TIMEOUT                                        | (-6)                 |
| OPRT\_FILE\_NOT\_FIND                                | (-7)                 |
| OPRT\_INIT\_MORE\_THAN\_ONCE                         | (-8)                 |
| OPRT\_INDEX\_OUT\_OF\_BOUND                          | (-9)                 |
| OPRT\_RESOURCE\_NOT\_READY                           | (-10)                |
| OPRT\_INIT\_MUTEX\_ATTR\_FAILED                      | (-101)               |
| OPRT\_SET\_MUTEX\_ATTR\_FAILED                       | (-102)               |
| OPRT\_DESTROY\_MUTEX\_ATTR\_FAILED                   | (-103)               |
| OPRT\_INIT\_MUTEX\_FAILED                            | (-104)               |
| OPRT\_MUTEX\_LOCK\_FAILED                            | (-105)               |
| OPRT\_MUTEX\_TRYLOCK\_FAILED                         | (-106)               |
| OPRT\_MUTEX\_LOCK\_BUSY                              | (-107)               |
| OPRT\_MUTEX\_UNLOCK\_FAILED                          | (-108)               |
| OPRT\_MUTEX\_RELEASE\_FAILED                         | (-109)               |
| OPRT\_CR\_MUTEX\_ERR                                 | (-110)               |
| OPRT\_MEM\_PARTITION\_EMPTY                          | (-111)               |
| OPRT\_MEM\_PARTITION\_FULL                           | (-112)               |
| OPRT\_MEM\_PARTITION\_NOT\_FOUND                     | (-113)               |
| OPRT\_DONOT\_FOUND\_MODULE                           | (-114)               |
| OPRT\_INIT\_SEM\_FAILED                              | (-201)               |
| OPRT\_WAIT\_SEM\_FAILED                              | (-202)               |
| OPRT\_POST\_SEM\_FAILED                              | (-203)               |
| OPRT\_THRD\_STA\_UNVALID                             | (-301)               |
| OPRT\_THRD\_CR\_FAILED                               | (-302)               |
| OPRT\_THRD\_JOIN\_FAILED                             | (-303)               |
| OPRT\_THRD\_SELF\_CAN\_NOT\_JOIN                     | (-304)               |
| OPRT\_TIMERID\_EXIST                                 | (-401)               |
| OPRT\_TIMERID\_NOT\_FOUND                            | (-402)               |
| OPRT\_TIMERID\_UNVALID                               | (-403)               |
| OPRT\_GET\_IDLE\_TIMERID\_ERROR                      | (-404)               |
| OPRT\_MSG\_NOT\_FOUND                                | (-501)               |
| OPRT\_MSG\_LIST\_EMPTY                               | (-502)               |
| OPRT\_WIFI\_SCAN\_FAIL                               | (-601)               |
| OPRT\_WF\_MAC\_SET\_FAIL                             | (-602)               |
| OPRT\_WF\_CONN\_FAIL                                 | (-603)               |
| OPRT\_WF\_NW\_CFG\_FAIL                              | (-604)               |
| OPRT\_WF\_AP\_SACN\_FAIL                             | (-605)               |
| OPRT\_WF\_NOT\_FIND\_ASS\_AP                         | (-606)               |
| OPRT\_DISCONNECTED\_WITH\_ROUTER                     | (-607)               |
| OPRT\_SOCK\_ERR                                      | (-701)               |
| OPRT\_SET\_SOCK\_ERR                                 | (-702)               |
| OPRT\_SOCK\_CONN\_ERR                                | (-703)               |
| OPRT\_BUF\_NOT\_ENOUGH                               | (-704)               |
| OPRT\_URL\_PARAM\_OUT\_LIMIT                         | (-705)               |
| OPRT\_HTTP\_OS\_ERROR                                | (-706)               |
| OPRT\_HTTP\_PR\_REQ\_ERROR                           | (-707)               |
| OPRT\_HTTP\_SD\_REQ\_ERROR                           | (-708)               |
| OPRT\_HTTP\_RD\_ERROR                                | (-709)               |
| OPRT\_HTTP\_AD\_HD\_ERROR                            | (-710)               |
| OPRT\_HTTP\_GET\_RESP\_ERROR                         | (-711)               |
| OPRT\_HTTP\_AES\_INIT\_ERR                           | (-712)               |
| OPRT\_HTTP\_AES\_OPEN\_ERR                           | (-713)               |
| OPRT\_HTTP\_AES\_SET\_KEY\_ERR                       | (-714)               |
| OPRT\_HTTP\_AES\_ENCRYPT\_ERR                        | (-715)               |
| OPRT\_CR\_HTTP\_URL\_H\_ERR                          | (-716)               |
| OPRT\_HTTPS\_HANDLE\_FAIL                            | (-717)               |
| OPRT\_HTTPS\_RESP\_UNVALID                           | (-718)               |
| OPRT\_HTTPS\_NO\_SUPPORT\_RANGE                      | (-719)               |
| OPRT\_HTTPS\_NOT\_ENCRYPT\_RET                       | (-720)               |
| OPRT\_CR\_CJSON\_ERR                                 | (-801)               |
| OPRT\_CJSON\_PARSE\_ERR                              | (-802)               |
| OPRT\_CJSON\_GET\_ERR                                | (-803)               |
| OPRT\_NOT\_FOUND                                     | (-901)               |
| OPRT\_DP\_ATTR\_ILLEGAL                              | (-902)               |
| OPRT\_DP\_TYPE\_PROP\_ILLEGAL                        | (-903)               |
| OPRT\_DP\_REPORT\_CLOUD\_ERR                         | (-904)               |
| OPRT\_NO\_NEED\_SET\_PRODINFO                        | (-905)               |
| OPRT\_NW\_INVALID                                    | (-906)               |
| OPRT\_SELECT\_ERR                                    | (-907)               |
| OPRT\_SELECT\_TM                                     | (-908)               |
| OPRT\_SEND\_ERR                                      | (-909)               |
| OPRT\_DEV\_NOT\_BIND                                 | (-910)               |
| OPRT\_FW\_UG\_FAILED                                 | (-911)               |
| OPRT\_VER\_FMT\_ERR                                  | (-912)               |
| OPRT\_FW\_NOT\_EXIST                                 | (-913)               |
| OPRT\_SEM\_CR\_ERR                                   | (-914)               |
| OPRT\_SELECT\_TIMEOUT                                | (-915)               |
| OPRT\_GW\_MQ\_OFFLILNE                               | (-916)               |
| OPRT\_NOT\_SPT\_CLX\_DP                              | (-917)               |
| OPRT\_RECV\_ERR                                      | (-918)               |
| OPRT\_UG\_PKG\_NOT\_ENOUGH                           | (-919)               |
| OPRT\_SCMA\_INVALID                                  | (-920)               |
| OPRT\_PRODECT\_KEY\_NULL                             | (-921)               |
| OPRT\_DEVICE\_VER\_NULL                              | (-922)               |
| OPRT\_MSG\_OUT\_OF\_LMT                              | (-923)               |
| OPRT\_NOT\_FOUND\_AUTH\_SSID                         | (-924)               |
| OPRT\_SOCKET\_FAULT                                  | (-925)               |
| OPRT\_MQ\_PUBLISH\_TIMEOUT                           | (-926)               |
| OPRT\_GW\_NOT\_EXIST                                 | (-927)               |
| OPRT\_GW\_SCHEMA\_SIZE\_LMT\_OUT                     | (-928)               |
| OPRT\_DEV\_DP\_CNT\_INVALID                          | (-929)               |
| OPRT\_TOKEN\_OVERTIME                                | (-930)               |
| OPRT\_WF\_NW\_CFG\_RECV\_CONTINUE                    | (-931)               |
| OPRT\_RECV\_DA\_NOT\_ENOUGH                          | (-932)               |
| OPRT\_SERV\_VRFY\_FAIL                               | (-933)               |
| OPRT\_KVS\_WR\_FAIL                                  | (-934)               |
| OPRT\_KVS\_RD\_FAIL                                  | (-935)               |
| OPRT\_NO\_AUTHENTICATION                             | (-936)               |
| OPRT\_CMD\_OUT\_OF\_TIME\_NOT\_EXEC                  | (-937)               |
| OPRT\_CMD\_NOT\_EXEC                                 | (-938)               |
| OPRT\_CRC32\_FAILED                                  | (-939)               |
| OPRT\_LAN\_NO\_CLIENT                                | (-940)               |
| OPRT\_WF\_INTF\_LACK\_REG\_CB                        | (-941)               |
| OPRT\_WF\_INTF\_NO\_REG\_CB                          | (-942)               |
| OPRT\_STORAGE\_UPLOAD                                | (-943)               |
| OPRT\_DP\_ID\_NOT\_FOUND                             | (-944)               |
| OPRT\_DP\_TP\_NOT\_MATCH                             | (-945)               |
| OPRT\_DEV\_NEED\_REGISTER                            | (-946)               |
| OPRT\_DEV\_RESET\_FACTORY                            | (-947)               |
| OPRT\_API\_VERSION\_WRONG                            | (-948)               |
| OPRT\_DEV\_ALREADY\_BIND                             | (-949)               |
| OPRT\_API\_DECODE\_FAILED                            | (-950)               |
| OPRT\_API\_TOKEN\_EXPIRE                             | (-951)               |
| OPRT\_NOT\_EXISTS                                    | (-952)               |
| OPRT\_DP\_ALREADY\_PROCESS                           | (-953)               |
| OPRT\_API\_VERIFY\_FAILED                            | (-954)               |
| OPRT\_FILE\_IS\_FULL                                 | (-999)               |
| OPRT\_WRITE\_FILE\_FAILED                            | (-1000)              |
| OPRT\_OPEN\_FILE\_FAILED                             | (-1001)              |
| OPRT\_PRINT\_LOG\_LEVEL\_HIGHER                      | (-1002)              |
| OPRT\_FORMAT\_STRING\_FAILED                         | (-1003)              |
| OPRT\_STRING\_TOO\_LONG                              | (-1004)              |
| OPRT\_WR\_FLASH\_ERROR                               | (-1005)              |
| OPRT\_ROUTER\_NOT\_FIND                              | (-1006)              |
| OPRT\_EXCEED\_UPPER\_LIMIT                           | (-1007)              |
| OPRT\_SECURITY\_VERIFY\_NOT\_OK                      | (-1008)              |
| OPRT\_FLASH\_NOT\_ENOUGH\_PAGE                       | (-1009)              |
| OPRT\_ERASE\_FLASH\_ERROR                            | (-1088)              |
| OPRT\_OSS\_OPERATION\_INIT\_FAILED                   | (-1100)              |
| OPRT\_OSS\_OPERATION\_UPDATE\_FAILED                 | (-1101)              |
| OPRT\_OSS\_OPERATION\_UPLOAD\_FAILED                 | (-1102)              |
| OPRT\_MOTION\_DETECTION\_ALARM\_PACK\_REPORT\_FAILED | (-1103)              |
| OPRT\_TRANSFER\_INIT\_FAILED                         | (-1104)              |
| OPRT\_LOG\_SEQ\_LOCKED                               | (-1200)              |
| OPRT\_LOG\_SEQ\_DONT\_LOCKED                         | (-1201)              |
| OPRT\_LOCAL\_LINK\_IGNORE                            | (-1300)              |
| OPRT\_LOCAL\_LINK\_CONTINUE                          | (-1301)              |
| OPRT\_IMG\_PROC\_IMG\_TYPE                           | (-1201)              |
| OPRT\_IMG\_PROC\_IMG\_SIZE                           | (-1202)              |
| OPRT\_IMG\_PROC\_PTR\_NULL                           | (-1203)              |
| OPRT\_IMG\_PROC\_ARRAY\_SIZE                         | (-1204)              |
| OPRT\_IMG\_PROC\_SCALE\_SIZE                         | (-1205)              |
| OPRT\_IMG\_PROC\_PARA\_ERROR                         | (-1206)              |
| OPRT\_EVENT\_STATUS\_INVALID                         | (-2000)              |
| OPRT\_ENCRYPT\_KEY\_UPDATED                          | (-2001)              |
| OPRT\_ORDER\_EXPIRE                                  | (-2002)              |
| OPRT\_NO\_MORE\_DATA                                 | (-2003)              |
| OPRT\_CLOUD\_STORAGE\_OFF                            | (-2004)              |
| OPRT\_CLOUD\_STORAGE\_STATUS\_INVALID                | (-2005)              |
| OPRT\_REMOTE\_API\_RUN\_UNKNOW\_FAILED               | (-2006)              |
| OPRT\_DEVICE\_REMOVED                                | (-2007)              |
| OPRT\_SIGNATURE\_ERROR                               | (-2008)              |
| OPRT\_DEVICE\_NOT\_WRONG                             | (-2009)              |
| OPRT\_WORK\_NUM\_EXIST                               | (-2010)              |
| OPRT\_SERVER\_NOT\_CONNECTED                         | (-2011)              |
| OPRT\_AUTH\_FAIL                                     | (-2012)              |
| OPRT\_LOCAL\_AI\_FACE\_NOT\_INIT                     | (-2013)              |
| OPRT\_LOCAL\_AI\_FACE\_DB\_ERROR                     | (-2014)              |
| OPRT\_WARN\_LIVING\_CHROMECAST                       | (148)                |
| OPRT\_WARN\_LIVING\_ECHOSHOW                         | (149)                |
| OPRT\_INFO\_QUIT\_CHROMECAST                         | (150)                |
| OPRT\_INFO\_QUIT\_ECHOSHOW                           | (151)                |
| MQTT\_DEF\_ERR                                       | (-3000)              |
| MQTT\_INVALID\_PARM                                  | (-3001)              |
| MQTT\_MALLOC\_FAILED                                 | (-3002)              |
| MQTT\_DNS\_PARSED\_FAILED                            | (-3010)              |
| MQTT\_SOCK\_CREAT\_FAILED                            | (-3011)              |
| MQTT\_SOCK\_CET\_FAILED                              | (-3012)              |
| MQTT\_TCP\_CONNECD\_FAILED                           | (-3013)              |
| MQTT\_TCP\_TLS\_CONNECD\_FAILED                      | (-3014)              |
| MQTT\_PACK\_SEND\_FAILED                             | (-3015)              |
| MQTT\_RECV\_DATA\_FORMAT\_WRONG                      | (-3016)              |
| MQTT\_RECV\_DATA\_MSGID\_NOT\_MATCH                  | (-3017)              |
| MQTT\_START\_TM\_MSG\_ERR                            | (-3018)              |
| MQTT\_OVER\_MAX\_MESSAGE\_LEN                        | (-3019)              |
| MQTT\_PING\_SEND\_ERR                                | (-3020)              |
