# Development Environment Setup When Using Tuya Wi-Fi SDK

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

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

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

For different developing requirement, Tuya provides different strategies and services. If you have independent development ability and can develop the underlying hardware and drivers, you can use the provided SDK to adapt to the abstract interface of the development environment, so as to achieve higher flexibility for device functions.

This topic describes how to set up the development environment for Tuya Wi-Fi SDK. The development environment includes the tool chain, script, library file and other information. However, it is not necessary if you use the module provided by Tuya, or Tuya has designed the SDK to work well with the specific chip platforms. The SDK products released by Tuya have completed the compilation adaptation, interface adaptation and other configuration, you can see the `README.md` file for more information.

### Set up the development environment <a href="#title-2-set-20up-20the-20development-20environment" id="title-2-set-20up-20the-20development-20environment"></a>

The development environment includes the tool chain, script, library file and other information provided by the chip manufacturer, which are necessary to compile the Tuya Wi-Fi SDK. Therefore, you must set up the development environment according to the following rules to compile the SDK and firmware.

* Collect the tools, libraries, and scripts of the genuine chip, send them together with compilation option and functional requirement to your Tuya account manager.
* Upon receiving the material, Tuya engineers make adaptation to the toolchain of genuine chip according your requirement. Taking the `RTL8720CF` as an example, see the following description for details.
* Create a `toolchain` folder under the root directory of the tool chain provided by the chip manufacturer.

  ![Development Enviroment Setup When Using Tuya Wi-Fi SDK](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200612/966f7827774048e3a0735466540f440c.png)
* Create a `build_path` folder to export the compilation location for the toolchain.
* `TUYA_SDK_TOOLCHAIN_ZIP`: identifies the location of toolchain compression package. During the compilation, if this value is detected, the compression package will be decompressed.
* `TUYA_SDK_BUILD_PATH`: identifies the location of compiler. The Tuya automated compilation system uses this path as a prefix for the compilation tool.![Development Enviroment Setup When Using Tuya Wi-Fi SDK](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200608/3a26f8a0d045453ba1f1f9a98372044a.png)
* Create a `platform.mk` folder to interact with the Tuya continuous integration system, and perform automated SDK compilation according to your requirement.
* `TUYA_PLATFORM_DIR`: identifies the location of current development environment.
* `TUYA_PLATFORM_CFLAGS`: adds CFLAGS and the requested compilation options.![Development Enviroment Setup When Using Tuya Wi-Fi SDK](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200608/5b0ded36fb52416b88835552f8de4618.png)
* Create a `build.conf` folder to customize your functional features requirement. For more information, see [Appendix](https://docs.tuya.com/en/iot/device-development/access-mode-link/wifi-chip-sdk/tuya-common-sdk-hal-adapter?id=K9mtiqg2uk5qz).
* After you receive the compiled Tuya Wi-Fi SDK, you can adjust the firmware compilation process according to the following section to compile the firmware, and then burn it to the chip for implementation.

#### Firmware compilation and setup <a href="#title-3-firmware-20compilation-20and-20setup" id="title-3-firmware-20compilation-20and-20setup"></a>

After decompressing the compiled Tuya Wi-Fi SDK, the directory is shown in the following table.

| 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. |
|                | `one_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. Sets up development and compilation environment.                                                                                          | N/A.                                                    |
| `CHANGELOG.md` | /               | Records all the notable changes for each version.                                                                                                                                                 | N/A.                                                    |
| `README.md`    | /               | User guide written in Markdown.                                                                                                                                                                   | N/A.                                                    |

Based on this environment, Tuya provides a set of compilation features. You can also customize the compilation process. The following instructions taking the `one_plug_demo` to describe the firmware compilation process.

![Development Enviroment Setup When Using Tuya Wi-Fi SDK](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200612/b41913597ad24a979be52b34af1250e3.png)

`one_plug_demo`: downloads the development environment packages to perform the compilation script. Alternatively, the shell script and makefile are supported for compilation.

![Development Enviroment Setup When Using Tuya Wi-Fi SDK](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200608/2ada8a85eaf344f9a8df8a28d7c5a657.png)

`./apps/one_plug_demo/build.sh`: enters into the development environment, run the compile script, and make packages.

![Development Enviroment Setup When Using Tuya Wi-Fi SDK](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200608/eb0d9ce1ebb84478b01db5811fd9aed9.png)

The compiled product will be saved to the `apps/one_plug_demo/output/` directory.

### The porting of tuya\_common <a href="#title-4-the-20porting-20of-20tuya_common" id="title-4-the-20porting-20of-20tuya_common"></a>

The `tuya_common` is stored in the Tuya Wi-Fi SDK package.

It is included in the development environment that are configured by running `build_app.sh`. You can directly use the related functions in the `tuya_common` folder.

![Development Enviroment Setup When Using Tuya Wi-Fi SDK](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200612/44a21dfd56374c1292a072b6ee27be06.png)

### The tuya\_os\_adapter configuration <a href="#title-5-the-20tuya_os_adapter-20configuration" id="title-5-the-20tuya_os_adapter-20configuration"></a>

The `tuya_os_adapter` uses a widely applicable registration method, which provides interface description and definition, and realizes the relevant interfaces and registration, so as to achieve the platform independence of Tuya Wi-Fi SDK. You need to implement `tuya_os_adapter` manually and place it in the root directory of development environment.

![Development Enviroment Setup When Using Tuya Wi-Fi SDK](https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/goat/20200612/c7a88bd64184408b934bb90035023d71.png)

#### System <a href="#title-6-system" id="title-6-system"></a>

The Tuya HAL System provides several interfaces of the operating system. For Linux platforms, the Tuya Wi-Fi SDK provides a set of widely-used system related interfaces. These interfaces can be implemented for your customization requirements.

**os\_adapter\_get\_systemtickcount**

```
SYS_TICK_T os_adapter_get_systemtickcount(void)
```

* **Description**

  Gets the current ticket value of the system.
* **Parameter**

  None.
* **Return value**

  The current tick value of the system.

**os\_adapter\_get\_tickratems**

```
unsigned int os_adapter_get_tickratems(void)
```

* **Description**

  How many milliseconds to get a ticket for the system.
* **Parameter**

  None.
* **Return value**

  How many milliseconds is 1 ticket in the system.

**os\_adapter\_system\_sleep**

```
void os_adapter_system_sleep(const unsigned long msTime)
```

* **Description**

  Thread sleep.
* **Parameter**

  | Input or output | Parameter name | Description                  |
  | --------------- | -------------- | ---------------------------- |
  | \[in]           | msTime         | System sleep time, unit: ms. |
* **Return value**

  None.

**os\_adapter\_system\_isrstatus**

```
bool os_adapter_system_isrstatus(void)
```

* **Description**

  Gets whether the system is in the interrupt routine.
* **Parameter**

  None.
* **Return value**

  TRUE: in interrupt routine.

  FALSE: not in interrupt routine.

**os\_adapter\_system\_reset**

```
void os_adapter_system_reset(void)
```

* **Description**

  Restarts the system.
* **Parameter**

  None.
* **Return value**

  None.

**os\_adapter\_system\_getheapsize**

```
unsigned int os_adapter_system_getheapsize(void)
```

* **Description**

  Gets the remaining memory size of the system.
* **Parameter**

  None.
* **Return value**

  Remaining memory.

**os\_adapter\_system\_get\_rst\_info**

```
TY_RST_REASON_E os_adapter_system_get_rst_info(void)
```

* **Description**

  Gets the reason for system restart.

  | TY\_RST\_REASON\_E          | ——                        |
  | --------------------------- | ------------------------- |
  | TY\_RST\_POWER\_OFF         | Power restart             |
  | TY\_RST\_HARDWARE\_WATCHDOG | Hardware watchdog reset   |
  | TY\_RST\_FATAL\_EXCEPTION   | Exception                 |
  | TY\_RST\_SOFTWARE\_WATCHDOG | Software watchdog restart |
  | TY\_RST\_SOFTWARE           | Software reset            |
  | TY\_RST\_DEEPSLEEP          | Deep sleep                |
  | TY\_RST\_HARDWARE           | Hardware reset            |
  | TY\_RST\_OTHER              | Other causes              |
  | TY\_RST\_UNSUPPORT          | Not supported             |
* **Parameter**

  None.
* **Return value**

  Reason for restart.

**os\_adapter\_get\_random\_data**

```
int os_adapter_get_random_data(const unsigned int range)
```

* **Description**

  Generates a random number.
* **Parameter**

  | Input or output | Parameter name | Description         |
  | --------------- | -------------- | ------------------- |
  | \[in]           | range          | Random number range |
* **Return value**

  A random value.
* **Notice**

  True random number.

**os\_adapter\_set\_cpu\_lp\_mode**

```
int os_adapter_set_cpu_lp_mode(const bool en,const TY_CPU_SLEEP_MODE_E mode)

```

* **Description**

  Sets CPU low power consumption mode.

  | mode                 | ——                                                        |
  | -------------------- | --------------------------------------------------------- |
  | TY\_CPU\_SLEEP       | Low power consumption mode, os auto wake up, within 15mA. |
  | TY\_CPU\_DEEP\_SLEEP | Deep sleep.                                               |
* **Parameter**

  | Input or output | Parameter name | Description                           |
  | --------------- | -------------- | ------------------------------------- |
  | \[in]           | en             | Whether to enable CPU low power mode. |
  | \[in]           | mode           | CPU low power mode.                   |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_CPU\_LPMODE\_SET\_FAILED: fail

#### Memory <a href="#title-7-memory" id="title-7-memory"></a>

Tuya HAL Memory provides several interfaces for memory application and release. For Linux platforms, the Tuya Wi-Fi SDK provides a set of widely-used system related interfaces. These interfaces can be implemented for your customization requirements.

**os\_adapter\_system\_malloc**

```
void *os_adapter_system_malloc(const size_t size)

```

* **Description**

  Memory allocation.
* **Parameter**

  | Input or output | Parameter name | Description            |
  | --------------- | -------------- | ---------------------- |
  | \[in]           | size           | Allocated memory size. |
* **Return value**

  NULL: Memory allocation failed.

  Other: Assigned successfully.
* **Notice**

  Other file APIs of the adaptation layer need to apply for memory, and all calls to os\_adapter\_system\_malloc.

**os\_adapter\_system\_free**

```
void os_adapter_system_free(void* ptr)

```

* **Description**

  Frees up memory.
* **Parameter**

  | Input or output | Parameter name | Description          |
  | --------------- | -------------- | -------------------- |
  | \[in]           | ptr            | Free memory address. |
* **Return value**

  None.
* **Notice**

  The other file APIs of the adaptation layer need to release memory, and all calls to `os_adapter_system_free`.

#### Semaphore <a href="#title-8-semaphore" id="title-8-semaphore"></a>

Tuya HAL Semaphore provides several interfaces for semaphore operations. For Linux platforms, the Tuya Wi-Fi SDK provides a set of widely-used system related interfaces. These interfaces can be implemented for your customization requirements.

**os\_adapter\_semaphore\_create\_init**

```
int os_adapter_semaphore_create_init(SEM_HANDLE *pHandle, const unsigned int semCnt, const unsigned int sem_max)

```

* **Description**

  Creates and initializes semaphore.
* **Parameter**

  | Input or output | Parameter name | Description                          |
  | --------------- | -------------- | ------------------------------------ |
  | \[out]          | pHandle        | Pointer to semaphore handle.         |
  | \[in]           | semCnt         | Number of initialization semaphores. |
  | \[in]           | sem\_max       | Maximum semaphore number.            |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MALLOC\_FAILED: failed to apply for memory

  OPRT\_OS\_ADAPTER\_THRD\_CREAT\_FAILED: fail

**os\_adapter\_semaphore\_wait**

```
int os_adapter_semaphore_wait(const SEM_HANDLE semHandle)

```

* **Description**

  Waits for semaphore.
* **Parameter**

  | Input or output | Parameter name | Description      |
  | --------------- | -------------- | ---------------- |
  | \[in]           | semHandle      | Semaphore handle |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_SEM\_WAIT\_FAILED: fail

**os\_adapter\_semaphore\_post**

```
int os_adapter_semaphore_post(const SEM_HANDLE semHandle)

```

* **Description**

  Sends semaphore.
* **Parameter**

  | Input or output | Parameter name | Description       |
  | --------------- | -------------- | ----------------- |
  | \[in]           | mutexHandle    | Semaphore handle. |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_SEM\_POST\_FAILED: fail
* **Notice**

  You need to distinguish between thread context and interrupt context.

**os\_adapter\_semaphore\_release**

```
int os_adapter_semaphore_release(const SEM_HANDLE semHandle)

```

* **Description**

  Frees up semaphore resources.
* **Parameter**

  | Input or output | Parameter name | Description      |
  | --------------- | -------------- | ---------------- |
  | \[in]           | semHandle      | Semaphore handle |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_SEM\_RELEASE\_FAILED: fail

#### Mutex <a href="#title-9-mutex" id="title-9-mutex"></a>

Tuya HAL Mutex provides a mutex operation interface, these interfaces can be implemented for your customization requirements. If it is a Linux platform, it may not be provided. Tuya Wi-Fi SDK provides a set of common interfaces for Linux platforms.

**os\_adapter\_mutex\_create\_init**

```
int os_adapter_mutex_create_init(MUTEX_HANDLE *pMutexHandle)

```

* **Description**

  Creates and initializes a mutex.
* **Parameter**

  | Input or output | Parameter name | Description                 |
  | --------------- | -------------- | --------------------------- |
  | \[out]          | pMutexHandle   | Pointer to the mutex handle |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MALLOC\_FAILED: failed to apply for memory

  OPRT\_OS\_ADAPTER\_MUTEX\_CREAT\_FAILED: fail

**os\_adapter\_mutex\_release**

```
int os_adapter_mutex_release(const MUTEX_HANDLE mutexHandle)

```

* **Description**

  Releases mutex resources.
* **Parameter**

  | Input or output | Parameter name | Description      |
  | --------------- | -------------- | ---------------- |
  | \[in]           | mutexHandle    | Semaphore handle |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MUTEX\_RELEASE\_FAILED: fail

**os\_adapter\_mutex\_lock**

```
int os_adapter_mutex_lock(const MUTEX_HANDLE mutexHandle)

```

* **Description**

  Locks mutex.
* **Parameter**

  | Input or output | Parameter name | Description      |
  | --------------- | -------------- | ---------------- |
  | \[in]           | mutexHandle    | Semaphore handle |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MUTEX\_LOCK\_FAILED: fail

**os\_adapter\_mutex\_unlock**

```
int os_adapter_mutex_unlock(const MUTEX_HANDLE mutexHandle)

```

* **Description**

  Unlocks mutex.
* **Parameter**

  | Input or output | Parameter name | Description      |
  | --------------- | -------------- | ---------------- |
  | \[in]           | mutexHandle    | Semaphore handle |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MUTEX\_UNLOCK\_FAILED: fail
* **Notice**

  You need to distinguish between thread context and interrupt context.

#### Thread <a href="#title-10-thread" id="title-10-thread"></a>

Tuya HAL Thread provides interfaces for thread creation and release, these interfaces can be implemented for your customization requirements. If it is a Linux platform, it may not be provided. Tuya Wi-Fi SDK provides a set of common interfaces for Linux platforms.

**os\_adapter\_thread\_create**

```
int os_adapter_thread_create(THREAD_HANDLE* thread,
                           const char* name,
                           const unsigned int stack_size,
                           const unsigned int priority,
                           const THREAD_FUNC_T func,
                           const void* arg)

```

* **Description**

  Creates and starts a thread.
* **Parameter**

  | Input or output | Parameter name | Description                                  |
  | --------------- | -------------- | -------------------------------------------- |
  | \[out]          | thread         | Pointer to thread handle                     |
  | \[in]           | name           | Thread name                                  |
  | \[in]           | stack\_size    | Thread stack size, unit is byte, not 4 bytes |
  | \[in]           | priority       | Thread priority                              |
  | \[in]           | func           | Thread function                              |
  | \[in]           | arg            | User parameters                              |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: create success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MALLOC\_FAILED: failed to apply for memory

  OPRT\_OS\_ADAPTER\_THRD\_CREAT\_FAILED: fail
* **Notice**
  1. The stack\_size parameter is 1 byte as a unit.
  2. Tuya business will use six thread priorities of 0, 1, 2, 3, 4, and 5, a lower number indicates a higher priority.

**os\_adapter\_thread\_release**

```
int os_adapter_thread_release(THREAD_HANDLE thread)

```

* **Description**

  Releases thread resources.
* **Parameter**

  | Input or output | Parameter name | Description      |
  | --------------- | -------------- | ---------------- |
  | \[in]           | thread         | Semaphore handle |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_THRD\_RELEASE\_FAILED: fail

**os\_adapter\_thread\_is\_self**

```
int os_adapter_thread_is_self(THREAD_HANDLE thread, bool* is_self)

```

* **Description**

  Determines whether the running context is in the specified thread.
* **Parameter**

  | Input or output | Parameter name | Description             |
  | --------------- | -------------- | ----------------------- |
  | \[in]           | thread         | Semaphore handle        |
  | \[out]          | is\_self       | Is the specified thread |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_THRD\_JUDGE\_SELF\_FAILED: fail

#### Watch Dog <a href="#title-11-watch-20dog" id="title-11-watch-20dog"></a>

Tuya HAL Watch Dog provides an interface for Watch Dog creation and refresh, which can be implemented for your customization requirements.

**os\_adapter\_watchdog\_init\_start**

```
unsigned int os_adapter_watchdog_init_start(const unsigned int timeval);

```

* **Description**

  Initializes and runs watchdog.
* **Parameter**

  | Input or output | Parameter name | Description   |
  | --------------- | -------------- | ------------- |
  | \[in]           | timeval        | Watchdog time |
* **Return value**

  int Watchdog time actually set by the chip.
* **Notice**

  The upper layer of the graffiti will set the watchdog time to 60s. If the chip does not support the 60s watchdog, the actual set time will be returned.

**os\_adapter\_watchdog\_refresh**

```
void os_adapter_watchdog_refresh(void);

```

* **Description**

  Refreshes watch dog.
* **Return value**

  void

**os\_adapter\_watchdog\_stop**

```
void os_adapter_watchdog_stop(void);

```

* **Description**

  Stops watch dog.
* **Return value**

  void

#### RTC <a href="#title-12-rtc" id="title-12-rtc"></a>

Tuya HAL RTC provides an interface for RTC operation and requires different platforms to implement it, which can be implemented for your customization requirements.

**os\_adapter\_rtc\_init**

```
void os_adapter_rtc_init(void)

```

* **Description**

  Initializes the internal RTC of the chip.
* **Return value**

  void no return value.

**os\_adapter\_rtc\_set\_time**

```
void os_adapter_rtc_set_time(const time_t write_time)

```

* **Description**

  Sets time to RTC inside the chip.
* **Parameter**

  | Input or output | Parameter name | Description          |
  | --------------- | -------------- | -------------------- |
  | \[in]           | write\_time    | Timestamp in seconds |
* **Return value**

  Void.

**os\_adapter\_rtc\_get\_time**

```
time_t os_adapter_rtc_get_time(void)

```

* **Description**

  Gets the time from the internal RTC clock of chip.
* **Parameter**

  | Input or output | Parameter name | Description |
  | --------------- | -------------- | ----------- |
  | \[in]           | void           | None.       |
* **Return value**

  `time_t RTC`: time inside the chip, in seconds.

#### OTA <a href="#title-13-ota" id="title-13-ota"></a>

Tuya HAL OTA provides an interface for OTA operation, which requires different platforms to implement it.

**os\_adapter\_ota\_start\_inform**

```
int os_adapter_ota_start_inform(unsigned int file_size);

```

* **Description**

  Sends the notification of upgrade start.
* **Parameter**

  | Input or output | Parameter name | Description                          |
  | --------------- | -------------- | ------------------------------------ |
  | \[in]           | file\_size     | Total firmware upgrade package size. |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MALLOC\_FAILED: failed to apply for memory

  OPRT\_OS\_ADAPTER\_OTA\_START\_INFORM\_FAILED: fail
* **Notice**

  Inform the HAL layer that the OTA upgrade is about to start. The HAL layer needs to apply in advance to upgrade OTA management resources and record the size of the OTA firmware package.

**os\_adapter\_ota\_data\_process**

```
int os_adapter_ota_data_process(const unsigned int total_len, const unsigned int offset,
                              const unsigned char* data, const unsigned int len, unsigned int* remain_len, void* pri_data);

```

* **Description**

  Processes The OTA packet.
* **Parameter**

  | Input or output | Parameter name | Description                                                       |
  | --------------- | -------------- | ----------------------------------------------------------------- |
  | \[in]           | total\_len     | The OTA upgrade firmware total size.                              |
  | \[in]           | offset         | The offset of data pointer in OTA upgrade firmware.               |
  | \[in]           | data           | The buffer pointer of this OTA packet.                            |
  | \[in]           | len            | The length of this OTA packet.                                    |
  | \[out]          | remain\_len    | Length of data packets that have been delivered but not processed |
  | \[in]           | pri\_data      | Keep parameters, not used for now                                 |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MALLOC\_FAILED: failed to apply for memory

  OPRT\_OS\_ADAPTER\_OTA\_PROCESS\_FAILED: fail
* **Notice**

  This function is used to support OTA upgrade, which needs to be implemented in conjunction with the Tuya upgrade package pull service. There are currently two upgrade methods in Tuya:

  * HTTP firmware upgrade.
  * Private serial port protocol firmware upgrade; but for the HAL function, there is no need to care about the source of the data packet.

  During the OTA upgrade process, Tuya Wi-Fi SDK will receive packets in a loop and notify this function to the OTA data packet. This function completes the work of writing the OTA data packet into the flash.

**os\_adapter\_ota\_end\_inform**

```
int os_adapter_ota_end_inform(bool reset);

```

* **Description**

  Informs the end of OTA data download.
* **Parameter**

  | Input or output | Parameter name | Description                                                      |
  | --------------- | -------------- | ---------------------------------------------------------------- |
  | \[in]           | reset          | Inform the hal layer whether the download needs to be restarted. |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_OTA\_VERIFY\_FAILED: verification failed

  OPRT\_OS\_ADAPTER\_OTA\_END\_INFORM\_FAILED: fail
* **Notice**

  This notification function will be called after the transmission of the OTA data packet is completed, which needs to be implemented in the secondary function: 1. Release the resources requested by the HAL layer at the beginning of OTA. 2. Do firmware integrity check 3. Modify the OTA parameter area and run the new firmware after restarting 4. Decide whether to restart according to the input parameter reset (restart logic after successful Tuya serial port upgrade is not implemented in this API)

#### Storage <a href="#title-14-storage" id="title-14-storage"></a>

Tuya HAL Storage provides an interface for flash operations, which can be implemented for your customization requirements.

**os\_adapter\_flash\_read**

```
int os_adapter_flash_read(const unsigned int addr, unsigned char *dst, const unsigned int size);

```

* **Description**

  Reads data from flash.
* **Parameter**

  | Input or output | Parameter name | Description                                     |
  | --------------- | -------------- | ----------------------------------------------- |
  | \[in]           | addr           | The first flash address read.                   |
  | \[out]          | dst            | The buffer pointer that receives the read data. |
  | \[in]           | size           | The size of the buffer read.                    |
* **Return value**

  0: success；

  Other: failure error code
* **Notice**
  1. Since it is possible for multiple threads on the Tuya upper layer to call the OS\_adapter\_FLASH layer API interface simultaneously, the os\_adapter\_flash\_read, OS\_adapter\_flash\_write, and OS\_adapter\_flash\_erase three operation functions need to share the mutex. If the called chip Flash API itself is mutually exclusive, no locking is required.If the calling chip Flash API itself is not mutex exclusive, a mutex is added to the OS\_adapter\_FLASH layer.
  2. If supported to run on the chip, operating flash may cause the system to tick, after operating Flash need to make up system tick (important).
  3. Read a sector within 3ms.

**os\_adapter\_flash\_write**

```
int os_adapter_flash_write(const unsigned int addr, const unsigned char *src, const unsigned int size);

```

* **Description**

  Writes data to flash.
* **Parameter**

  | Input or output | Parameter name | Description                                |
  | --------------- | -------------- | ------------------------------------------ |
  | \[in]           | addr           | Writes the first flash address.            |
  | \[in]           | dst            | A buffer pointer to which data is written. |
  | \[in]           | size           | The size of the buffer to be written.      |
* **Return value**

  0: success

  Other: failure error code
* **Notice**

  Write a sector within 20ms.

**os\_adapter\_flash\_erase**

```
int os_adapter_flash_erase(const unsigned int addr, const unsigned int size);

```

* **Description**

  Erases flash block.
* **Parameter**

  | Input or output | Parameter name | Description                    |
  | --------------- | -------------- | ------------------------------ |
  | \[in]           | addr           | Erase the flash first address. |
  | \[in]           | size           | Flash size erased.             |
* **Return value**

  0: success；

  Other: failure error code
* **Notice**

  Wipe a sector within 65ms.

**os\_adapter\_storage\_get\_desc**

```
UNI_STORAGE_DESC_S* os_adapter_storage_get_desc(void);

```

* **Description**

  Gets the KV(key-value) -Flash partition information of the chip SDK for data partitioning of graffiti application.
* **Parameter**

  None.
* **Return value**

  UNI\_STORAGE\_DESC\_S\*: KV- Flash partition information structure pointer.
* **Notice**
  1. You can define a static structure variable in `os_adapter_storge.c` that describes the KV-Flash partition information of the chip for Tuya application data partitioning. For example:

     ```
       static UNI_STORAGE_DESC_S storage = {
           SIMPLE_FLASH_START,
           SIMPLE_FLASH_SIZE,
           PARTITION_SIZE,
           SIMPLE_FLASH_SWAP_START,
           SIMPLE_FLASH_SWAP_SIZE,
           SIMPLE_FLASH_KEY_ADDR
       };

       UNI_STORAGE_DESC_S* os_adapter_storage_get_desc(void)
       {
           return &storage;
       }

     ```
  2. Recommended area:

     | The partition name       | Description                    | Length (recommended value） | instructions   |
     | ------------------------ | ------------------------------ | -------------------------- | -------------- |
     | SIMPLE\_FLASH            | KV data                        | 0x10000(64K)               | At least 32 k  |
     | SIMPLE\_FLASH\_SWAP      | Backup area for KV data        | 0x4000(16K)                | At least 12 k  |
     | SIMPLE\_FLASH\_KEY\_ADDR | KV encryption key storage area | 0x1000(4K)                 | Size immutable |
  3. After KV Flash has determined the Flash partition, it cannot be changed. If there is any change, forward compatibility is not supported.
  4. Tuya authorization information is stored in KV zone.

**os\_adapter\_uf\_get\_desc**

```
UF_PARTITION_TABLE_S* os_adapter_uf_get_desc(void);

```

* **Description**

  Gets the UF\_file-Flash partition information of the chip SDK for the data of the graffiti application.
* **Parameter**

  None.
* **Return value**

  UF\_PARTITION\_TABLE\_S\*: Uf\_file-flash partition information structure pointer.
* **Notice**
  1. Tuya Uf-file is a Flash storage component of Linux file IO operation API (Open, Write,read,close, etc.). It supports the application layer to operate Flash through uf\_file.h interface.
  2. You can define a static structure variable in OS\_adapter\_storge. C that describes the uf\_file-Flash partition information that the chip USES to partition data for Tuya.For example,

     ```
       static UF_PARTITION_TABLE_S uf_file = {
           .sector_size = PARTITION_SIZE,
           .uf_partition_num = 2,
           .uf_partition = {
               {xx, 0x8000},
               {xx, 0x18000}
           }
       };

     ```
  3. Recommended partitions (the combined size of several partitions is not less than 128K) :

     | partition name | Description           | Description length (recommended) | instructions                           |
     | -------------- | --------------------- | -------------------------------- | -------------------------------------- |
     | UF\_FILE\_1    | UF\_FILE data 1 block | 0x8000(32K)                      | 32K, starting position can be adjusted |
     | UF\_FILE\_2    | UF\_FILE data area 2  | 0x18000(96K)                     | 96K, starting position can be adjusted |

#### Output <a href="#title-15-output" id="title-15-output"></a>

The Tuya HAL Output interface provides interfaces for redirecting logs, such as printing to a file or the cloud. These interfaces can be implemented for your customization requirements.

**os\_adapter\_output\_log**

```
void os_adapter_output_log(const char *str)

```

* **Description**

  Prints a string.
* **Parameter**

  | Input or output | Parameter name | Description    |
  | --------------- | -------------- | -------------- |
  | \[in]           | str            | String pointer |
* **Return value**

  Void.

**os\_adapter\_log\_close**

```
int os_adapter_log_close(void)

```

* **Description**

  Closes the log I/O pin. The business needs to use log UART IO as normal IO to turn off the UART function.
* **Parameter**

  | Input or output | Parameter name | Description    |
  | --------------- | -------------- | -------------- |
  | \[in]           | str            | String pointer |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_LOG\_CLOSE\_FAILED: fail

**os\_adapter\_log\_open**

```
int os_adapter_log_open(void)

```

* **Description**

  Restores the log I/O pin.
* **Parameter**

  None.
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_LOG\_OPEN\_FAILED: fail

#### Wi-Fi <a href="#title-16-wi-fi" id="title-16-wi-fi"></a>

Tuya HAL Wi-Fi provides interfaces for Wi-Fi operations that requires different platforms to implement.

**os\_adapter\_wifi\_all\_ap\_scan**

```
int os_adapter_wifi_all_ap_scan(AP_IF_S **ap_ary, unsigned int *num)

```

* **Description**

  Scans all AP information for the current environment.
* **Parameter**

  | Input or output | Parameter name | Description          |
  | --------------- | -------------- | -------------------- |
  | \[out]          | ap\_ary        | Scan results         |
  | \[out]          | num            | Number of SCANNED AP |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_AP\_SCAN\_FAILED: fail
* **Notice**
  1. blocking function.
  2. can only scan AP in the current country code channel list.
  3. The `ap_ary` needs to allocate memory within the API (scan multiple APS, need to allocate contiguous memory space).
  4. The memory is released by the Tuya SDK which actively calls `os_adapter_wifi_release_ap`.

**os\_adapter\_wifi\_assign\_ap\_scan**

```
int os_adapter_wifi_assign_ap_scan(const char *ssid, AP_IF_S **ap)

```

* **Description**

  Scans AP information for the SSID specified in the current environment.
* **Parameter**

  | Input or output | Parameter name | Description      |
  | --------------- | -------------- | ---------------- |
  | \[in]           | ssid           | Specify the ssid |
  | \[out]          | ap             | Scan results     |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_AP\_SCAN\_FAILED: Scan failed
* **Notice**
  1. Blocking function.
  2. Only scan AP in the current country code channel list.
  3. When an AP specifying an SSID exists in the environment, the success rate of scanning the SSID is more than 98%.
  4. AP needs to allocate memory within the API (scanning multiple APS requires allocating continuous memory space).
  5. The memory is released by the Tuya SDK which actively calls `os_adapter_wifi_release_ap`.

**os\_adapter\_wifi\_release\_ap**

```
int os_adapter_wifi_release_ap(AP_IF_S *ap)

```

* **Description**

  Frees up the AP information resources.
* **Parameter**

  | Input or output | Parameter name | Description                              |
  | --------------- | -------------- | ---------------------------------------- |
  | \[in]           | ap             | Ap information that needs to be released |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_AP\_RELEASE\_FAILED: fail

**os\_adapter\_wifi\_set\_cur\_channel**

```
int os_adapter_wifi_set_cur_channel(const unsigned char chan)

```

* **Description**

  Sets the signal channel.
* **Parameter**

  | Input or output | Parameter name | Description |
  | --------------- | -------------- | ----------- |
  | \[in]           | chan           | channel     |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_CHAN\_SET\_FAILED: fail
* **Notice**
  1. Failed to set a channel signal outside the current national code channel range.
  2. support to set channels in sniffer callback.

**os\_adapter\_wifi\_get\_cur\_channel**

```
int os_adapter_wifi_get_cur_channel(unsigned char *chan)

```

* **Description**

  Gets the current channel.
* **Parameter**

  | Input or output | Parameter name | Description |
  | --------------- | -------------- | ----------- |
  | \[out]          | chan           | channel     |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_CHAN\_GET\_FAILED: fail

**os\_adapter\_wifi\_get\_ip**

```
int os_adapter_wifi_get_ip(const WF_IF_E wf, NW_IP_S *ip)

```

* **Description**

  Gets WIFI IP information (IP address, gateway address, address mask).
* **Parameter**

  | Input or output | Parameter name | Description    |
  | --------------- | -------------- | -------------- |
  | \[in]           | wf             | Wi-Fi model    |
  | \[out]          | ip             | IP information |
  | --------        | --------       |                |
  | WF\_STATION     | station type   |                |
  | WF\_AP          | ap type        |                |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_IP\_GET\_FAILED: fail
* **Notice**

  IP information under different modes (based on transmission reference WF) needs to be distinguished.

**os\_adapter\_wifi\_set\_mac**

```
int os_adapter_wifi_set_mac(const WF_IF_E wf, const NW_MAC_S *mac)

```

* **Description**

  Sets the MAC address.
* **Parameter**

  | Input or output | Parameter name | Description     |
  | --------------- | -------------- | --------------- |
  | \[in]           | wf             | Wi-Fi model     |
  | \[in]           | mac            | The MAC address |
  | --------        | --------       |                 |
  | WF\_STATION     | station type   |                 |
  | WF\_AP          | ap type        |                 |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_MAC\_SET\_FAILED: fail
* **Notice**

  Permanent effect, no loss of power cut.

**os\_adapter\_wifi\_get\_mac**

```
int os_adapter_wifi_get_mac(const WF_IF_E wf, NW_MAC_S *mac)

```

* **Description**

  Gets the MAC address.
* **Parameter**

  | Input or output | Parameter name | Description     |
  | --------------- | -------------- | --------------- |
  | \[in]           | wf             | Wi-Fi model     |
  | \[out]          | mac            | The MAC address |
  | --------        | --------       |                 |
  | WF\_STATION     | station type   |                 |
  | WF\_AP          | ap type        |                 |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_MAC\_GET\_FAILED: fail
* **Notice**

  MAC addresses in STATIONAP mode need to distinguish between operating modes (according to transmission parameter WF).

**os\_adapter\_wifi\_set\_work\_mode**

```
int os_adapter_wifi_set_work_mode(const WF_WK_MD_E mode)

```

* **Description**

  Sets the Wi-Fi working mode.
* **Parameter**

  | Input or output | Parameter name                                         | Description        |
  | --------------- | ------------------------------------------------------ | ------------------ |
  | \[in]           | mode                                                   | Wi-Fi working mode |
  | --------        | --------                                               |                    |
  | WWM\_LOWPOWER   | Low power consumption (refer to turn off Wi-Fi module) | N/A                |
  | WWM\_SNIFFER    | monitor                                                | N/A                |
  | WWM\_STATION    | station                                                | N/A                |
  | WWM\_SOFTAP     | softap                                                 | N/A                |
  | WWM\_STATIONAP  | stationap                                              | N/A                |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_WORKMODE\_SET\_FAILED: fail
* **Notice**
  1. From the power on the device to the call of Tuya entrance, it is required to be within 200ms.
  2. If the device initialization time exceeds 200ms, the bottom layer can make some necessary initialization and then call Tuya entrance.Place the time consuming Wi-Fi initialization on another thread for execution.
  3. Os\_adapter\_wifi\_set\_work\_mode is the first API that Tuya service calls with Wi-Fi. In this API, it is necessary to determine whether Wi-Fi initialization has ended.If it's not finished, wait here until it's finished before proceeding to the next step (only need to judge once).
  4. If the device is less than 200ms from electric to Tuya entrance, there is no need to consider this point.

**os\_adapter\_wifi\_get\_work\_mode**

```
int os_adapter_wifi_get_work_mode(WF_WK_MD_E *mode)

```

* **Description**

  Gets Wi-Fi working mode.
* **Parameter**

  | Input or output | Parameter name                                         | Description        |
  | --------------- | ------------------------------------------------------ | ------------------ |
  | \[out]          | mode                                                   | Wi-Fi working mode |
  | --------        | --------                                               | N/A                |
  | WWM\_LOWPOWER   | Low power consumption (refer to turn off Wi-Fi module) | N/A                |
  | WWM\_SNIFFER    | monitor                                                | N/A                |
  | WWM\_STATION    | station                                                | N/A                |
  | WWM\_SOFTAP     | softap                                                 | N/A                |
  | WWM\_STATIONAP  | stationap                                              | N/A                |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_WORKMODE\_GET\_FAILED: fail

**os\_adapter\_wifi\_sniffer\_set**

```
int os_adapter_wifi_sniffer_set(const bool en, const SNIFFER_CALLBACK cb)

```

* **Description**

  Sets the sniffer function switch.
* **Parameter**

  | Input or output | Parameter name | Description               |
  | --------------- | -------------- | ------------------------- |
  | \[in]           | en             | Turn sniffer on or off    |
  | \[in]           | cb             | Grab packet data callback |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_SNIFFER\_SET\_FAILED: fail
* **Notice**

  The data returned to the application includes the management data.

**os\_adapter\_wifi\_ap\_start**

```
int os_adapter_wifi_ap_start( const WF_AP_CFG_IF_S *cfg)

```

* **Description**

  Starts AP mode.
* **Parameter**

  | Input or output | Parameter name | Description                 |
  | --------------- | -------------- | --------------------------- |
  | \[in]           | cfg            | AP configuration parameters |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_AP\_START\_FAILED: fail
* **Notice**
  1. According to MAX\_CONN, the maximum number of STA can be connected in AP mode.
  2. IP information in AP mode needs to be set according to IP information.
  3. StationAP mode, station Port to connect router, connection process and connection if AP port has connection. The AP port should not be disconnected and broadcast packets can be sent or sent after a failure.
  4. Ez + AP coexistence:
     1. Softap or StationAP mode enables sniffer function and channel switching.
     2. After enabling sniffer, the data thrown back to the callback shall include the device connection request packet of the AP port.

**os\_adapter\_wifi\_ap\_stop**

```
int os_adapter_wifi_ap_stop(void)

```

* **Description**

  Stops AP mode.
* **Parameter**

  None.
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_AP\_STOP\_FAILED: fail
* **Notice**
  1. Different closing logic needs to be done for the current mode of softap or StationAP.
  2. In the mode of StationAP, if the station port terminal is connected, close the AP Port process and the station port cannot be disconnected.

**os\_adapter\_wifi\_get\_connected\_ap\_info\_v2**

```
int os_adapter_wifi_get_connected_ap_info_v2(FAST_WF_CONNECTED_AP_INFO_V2_S **fast_ap_info)

```

* **Description**

  Gets AP information for fast connection.
* **Parameter**

  | Input or output | Parameter name | Description               |
  | --------------- | -------------- | ------------------------- |
  | \[out]          | fast\_ap\_info | Associated AP information |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_APINFO\_GET\_FAILED: fail
* **Notice**
  1. Fast\_ap\_info needs to dynamically request memory within the API.
  2. Each time the router is reconnected successfully, the router information will be obtained once, and if there is an update, it will be synchronized to Flash.

**os\_adapter\_fast\_station\_connect\_v2**

```
int os_adapter_fast_station_connect_v2(const FAST_WF_CONNECTED_AP_INFO_V2_S *fast_ap_info)

```

* **Description**

  Connects with router quickly.
* **Parameter**

  | Input or output | Parameter name | Description                                 |
  | --------------- | -------------- | ------------------------------------------- |
  | \[in]           | fast\_ap\_info | Ap information required for fast connection |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_FAST\_CONN\_FAILED: fail
* **Notice**

  This function is not called until the first connection has been connected and restarted.

**os\_adapter\_wifi\_station\_connect**

```
int os_adapter_wifi_station_connect(const char *ssid, const char *passwd)

```

* **Description**

  Connects with router.
* **Parameter**

  | Input or output | Parameter name | Description |
  | --------------- | -------------- | ----------- |
  | \[in]           | ssid           | ssid        |
  | \[in]           | passwd         | passwd      |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_CONN\_FAILED: fail
* **Notice**
  1. Non-blocking. After starting the connection process successfully, the upper layer will call OS\_adapter\_wifi\_STATION\_get\_status to query the Wi-Fi connection status every 1s.
  2. The function of automatic re-connection should be enabled. The re-connection time should be less than 1min.

**os\_adapter\_wifi\_station\_disconnect**

```
int os_adapter_wifi_station_disconnect(void)

```

* **Description**

  Disconnects with router.
* **Parameter**

  None.
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_DISCONN\_FAILED: fail

**os\_adapter\_wifi\_station\_get\_conn\_ap\_rssi**

```
int os_adapter_wifi_station_get_conn_ap_rssi(char *rssi)

```

* **Description**

  Gets the signal strength of the associated AP.
* **Parameter**

  | Input or output | Parameter name | Description     |
  | --------------- | -------------- | --------------- |
  | \[out]          | rssi           | Signal strength |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_RSSI\_GET\_FAILED: fail
* **Notice**
  1. If the location of the device and router is not moved, multiple RSSI acquisition can only produce a small range of fluctuation.
  2. The location of mobile devices and routers; rsSI acquisition will have corresponding changes.

**os\_adapter\_wifi\_get\_bssid**

```
int os_adapter_wifi_get_bssid(unsigned char *mac)

```

* **Description**

  Gets the MAC address of the ASSOCIATED AP.
* **Parameter**

  | Input or output | Parameter name | Description     |
  | --------------- | -------------- | --------------- |
  | \[out]          | mac            | The MAC address |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_BSSID\_GET\_FAILED: fail

**os\_adapter\_wifi\_station\_get\_status**

```
int os_adapter_wifi_station_get_status(WF_STATION_STAT_E *stat)

```

* **Description**

  Gets the current connection status of Wi-Fi.
* **Parameter**

  | Input or output    | Parameter name               | Description       |
  | ------------------ | ---------------------------- | ----------------- |
  | \[out]             | stat                         | Connection status |
  | WSS\_IDLE          |                              | N/A               |
  | WSS\_CONNECTING    | In the connection            | N/A               |
  | WSS\_PASSWD\_WRONG | Password mistake             | N/A               |
  | WSS\_CONN\_FAIL    | The connection fails         | N/A               |
  | WSS\_CONN\_SUCCESS | The connection is successful | N/A               |
  | WSS\_GOT\_IP       | DHCP success                 | N/A               |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_STAT\_GET\_FAILED: fail

**os\_adapter\_wifi\_set\_country\_code**

```
int os_adapter_wifi_set_country_code(const COUNTRY_CODE_E ccode)

```

* **Description**

  Sets the country code.
* **Parameter**

  | Input or output   | Parameter name                                          | Description  |
  | ----------------- | ------------------------------------------------------- | ------------ |
  | \[in]             | ccode                                                   | Country code |
  | --------          | --------                                                |              |
  | COUNTRY\_CODE\_CN | China 1-13                                              |              |
  | COUNTRY\_CODE\_US | The United States district 1-11                         |              |
  | COUNTRY\_CODE\_JP | Japan 1-14                                              |              |
  | COUNTRY\_CODE\_EU | European1-13 European adaptation needs to be considered |              |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_CCODE\_SET\_FAILED: fail
* **Notice**
  1. The country code only needs to support China (CN 1-13), US (US 1-11), Japan (JP 1-14) and Europe (EU 1-13).
  2. For different country codes, the requirements are as follows:
     1. When scanning routers, only AP corresponding to channel list of country code can be scanned.
     2. The European region needs to realize the European adaptive function.
     3. When setting the channel, the channel set is not within the range of the channel list of the current country code, and set the channel return error.

**os\_adapter\_wifi\_send\_mgnt**

```
int os_adapter_wifi_send_mgnt(const unsigned char *buf, const unsigned int len)

```

* **Description**

  Sends management packet data.
* **Parameter**

  | Input or output | Parameter name | Description               |
  | --------------- | -------------- | ------------------------- |
  | \[in]           | buf            | Manage package data BUF   |
  | \[in]           | len            | Manage packet data length |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_MGNT\_SEND\_FAILED: fail

**os\_adapter\_wifi\_register\_recv\_mgnt\_callback**

```
int os_adapter_wifi_register_recv_mgnt_callback(const bool enable, const WIFI_REV_MGNT_CB recv_cb)

```

* **Description**

  Sets whether the application layer receives management package data.
* **Parameter**

  | Input or output | Parameter name | Description                                     |
  | --------------- | -------------- | ----------------------------------------------- |
  | \[in]           | enable         | Whether to open receive management package data |
  | \[in]           | recv\_cb       | Receives the management package data callback   |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_MGNT\_REG\_FAILED: fail

**os\_adapter\_set\_wifi\_lp\_mode**

```
int os_adapter_set_wifi_lp_mode(IN const bool en, IN const unsigned int dtim)

```

* **Description**

  Sets to the low power mode.
* **Parameter**

  | Input or output | Parameter name | Description                        |
  | --------------- | -------------- | ---------------------------------- |
  | \[in]           | en             | Whether Wi-Fi low power mode is on |
  | \[in]           | dtim           | The dtim parameters                |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_WF\_LPMODE\_SET\_FAILED: fail

#### BLE <a href="#title-17-ble" id="title-17-ble"></a>

Tuya HAL BLE provides an interface to BLE operations that requires different platforms to implement.

**ty\_bt\_param\_t**

```
typedef VOID (*TY_BT_MSG_CB)(int id, ty_bt_cb_event_t e, unsigned char *buf, unsigned int len);

typedef struct {
    char name[DEVICE_NAME_LEN]; //Bluetooth name
    ty_bt_mode_t mode; 
    unsigned char link_num; // Maximum number of supported client connections. Default is 1
    TY_BT_MSG_CB cb; // Event and data callback functions
    tuya_ble_data_buf_t adv; // Broadcast content
    tuya_ble_data_buf_t scan_rsp; // The client (APP) scans the response data
}ty_bt_param_t;


```

**ty\_bt\_scan\_info\_t**

```
typedef struct {
    ty_bt_scan_type_t scan_type; /* Scan type */
    char name[DEVICE_NAME_LEN]; // Bluetooth name, which takes effect when set to TY_BT_SCAN_BY_NAME
    char bd_addr[6]; //Bluetooth MAC, when set to TY_BT_SCAN_BY_MAC
    char rssi; // After scanning, return the signal strength
    unsigned char channel;  // After scanning, return to the channel
    unsigned char timeout_s; /* Scan timeout, not scanned in the specified time, return failure, the unit is second. */
}ty_bt_scan_info_t;

```

**os\_adapter\_bt\_port\_init**

```
int os_adapter_bt_port_init(ty_bt_param_t *p)

```

* **Description**

  Initializes Bluetooth. In cases where RF is shared between Wi-Fi and Bluetooth, this function needs to support multiple calls, that is, it can be called again after `tuya_bt_port_deinit` turns off Bluetooth.
* **Parameter**

  | Input or output | Parameter name | Description                           |
  | --------------- | -------------- | ------------------------------------- |
  | \[in]           | p              | Bluetooth parameter structure pointer |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MALLOC\_FAILED: Memory application failed

  OPRT\_OS\_ADAPTER\_BT\_INIT\_FAILED: fail
* **Notice**
  1. You cannot add sleep to this function to wait for initialization to complete.
  2. After Bluetooth initialization is complete, the notification is returned to the application layer via the TY\_BT\_EVENT\_ADV\_READY event.

**os\_adapter\_bt\_port\_deinit**

```
int os_adapter_bt_port_deinit(void)

```

* **Description**

  Stops the Bluetooth initialization.
* **Parameter**

  None.
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_BT\_DEINIT\_FAILED: fail
* **Notice**
  1. In the case of RF sharing between Wi-Fi and Bluetooth, it is necessary to call this function to turn off Bluetooth before turning off Wi-Fi.
  2. In the case of low power consumption, this function is called to turn off Bluetooth to achieve low power consumption.

**os\_adapter\_bt\_gap\_disconnect**

```
int os_adapter_bt_gap_disconnect(void)

```

* **Description**

  Disconnects via the Bluetooth as a child node.
* **Parameter**

  None.
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_BT\_DISCONN\_FAILED: fail

**os\_adapter\_bt\_send**

```
int os_adapter_bt_send(unsigned char *data, unsigned char len)

```

* **Description**

  Sends data via the Bluetooth as a child node.
* **Parameter**

  | Input or output | Parameter name | Description            |
  | --------------- | -------------- | ---------------------- |
  | \[in]           | data           | Pointer to the data    |
  | \[in]           | len            | The length of the data |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_BT\_SEND\_FAILED: fail

**os\_adapter\_bt\_reset\_adv**

```
int os_adapter_bt_reset_adv(tuya_ble_data_buf_t *adv, tuya_ble_data_buf_t *scan_resp)

```

* **Description**

  Resets Bluetooth broadcast content and scan response content.
* **Parameter**

  | Input or output | Parameter name | Description                       |
  | --------------- | -------------- | --------------------------------- |
  | \[in]           | adv            | Bluetooth broadcast data          |
  | \[in]           | lscan\_respen  | Bluetooth scans the response data |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_BT\_ADV\_RESET\_FAILED: fail

**os\_adapter\_bt\_get\_rssi**

```
int os_adapter_bt_get_rssi(signed char *rssi)

```

* **Description**

  Gets the signal strength of Bluetooth.
* **Parameter**

  | Input or output | Parameter name | Description             |
  | --------------- | -------------- | ----------------------- |
  | \[out]          | rssi           | Signal strength pointer |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_BT\_RSSI\_GET\_FAILED: fail

**os\_adapter\_bt\_start\_adv**

```
int os_adapter_bt_start_adv()

```

* **Description**

  Starts Bluetooth broadcasting, using the original parameters.
* **Parameter**

  None.
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MALLOC\_FAILED: Memory application failed

  OPRT\_OS\_ADAPTER\_BT\_ADV\_START\_FAILED: fail

**os\_adapter\_bt\_stop\_adv**

```
int os_adapter_bt_stop_adv()

```

* **Description**

  Stops Bluetooth broadcasting.
* **Parameter**

  None.
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_BT\_ADV\_STOP\_FAILED: fail

**9-os\_adapter\_bt\_assign\_scan**

```
int os_adapter_bt_assign_scan(ty_bt_scan_info_t *info)

```

* **Description**

  Scans the specified Bluetooth name to obtain Bluetooth information for production testing.
* **Parameter**

  | Input or output | Parameter name | Description           |
  | --------------- | -------------- | --------------------- |
  | \[inout]        | info           | Bluetooth information |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MALLOC\_FAILED: Memory application failed

  OPRT\_OS\_ADAPTER\_BT\_SCAN\_FAILED: fail
* **Notice**
  1. Scan\_type is TY\_BT\_SCAN\_BY\_NAME, then the beacon for the specific Bluetooth name is scanned.
  2. Scan\_type is TY\_BT\_SCAN\_BY\_MAC, then the beacon for a particular MAC address is scanned.
  3. The signal strength must be carried in the returned information.

#### UART <a href="#title-18-uart" id="title-18-uart"></a>

Tuya HAL UART provides the interface for UART operation and requires different platforms to implement it.

**TY\_UART\_PORT\_E**

```
typedef enum {
    TY_UART0 = 0x00,
    TY_UART1,
    TY_UART2,
    TY_UART3,
    TY_UART_NUM,
} TY_UART_PORT_E;

```

The `TY_UART0` is the default log port, `TY_UART1` is the account, `TY_UART2`, `TY_UART3` is the extension serial port.

The serial port authorized by graffiti upper layer and used by users for docking is TY\_UART1. If the serial port set by graffiti is corresponding to the serial port and the core.

The platform is in conflict, please make adjustments in the adaptation layer.

**os\_adapter\_uart\_init**

```
int *os_adapter_uart_init(const TY_UART_DEV_S *uart)

```

* **Description**

  Initializes the serial port.
* **Parameter**

  | Input or output | Parameter name | Description                                                           |
  | --------------- | -------------- | --------------------------------------------------------------------- |
  | \[in]           | uart           | Serial port handle, which needs to initialize the relevant parameters |
  | \[in]           | port           | A serial port ID                                                      |
  | \[in]           | arg            | Configuration parameters                                              |
  | \[in]           | cb             | UART RX interrupts the callback                                       |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MALLOC\_FAILED: Memory application failed

  OPRT\_OS\_ADAPTER\_UART\_INIT\_FAILED: fail

**os\_adapter\_uart\_deinit**

```
int os_adapter_uart_deinit(const TY_UART_PORT_E port)

```

* **Description**

  The serial port de-initialization function.
* **Parameter**

  | Input or output | Parameter name | Description   |
  | --------------- | -------------- | ------------- |
  | \[in]           | port           | A serial port |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_UART\_DEINIT\_FAILED: fail

**os\_adapter\_uart\_send\_char**

```
int os_adapter_uart_send_char(const TY_UART_PORT_E port, const unsigned char ch)

```

* **Description**

  Sends data via serial port.
* **Parameter**

  | Input or output | Parameter name | Description         |
  | --------------- | -------------- | ------------------- |
  | \[in]           | port           | A serial port       |
  | \[in]           | ch             | The data to be sent |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_UART\_SEND\_FAILED: fail

**os\_adapter\_uart\_pool\_read\_char**

```
int os_adapter_uart_pool_read_char(const TY_UART_PORT_E port, unsigned char *ch)

```

* **Description**

  Polls data via serial port.
* **Parameter**

  | Input or output | Parameter name | Description                    |
  | --------------- | -------------- | ------------------------------ |
  | \[in]           | port           | A serial port                  |
  | \[out]          | ch             | A pointer to the received data |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_UART\_READ\_FAILED: fail

#### GPIO <a href="#title-19-gpio" id="title-19-gpio"></a>

Tuya HAL GPIO provides interfaces to the GPIO operation and requires different platforms to implement it.

**os\_adapter\_gpio\_inout\_set**

```
int os_adapter_gpio_inout_set(const TY_GPIO_PORT_E port, const bool in)

```

* **Description**

  Sets the GPIO I/O state.
* **Parameter**

  | Input or output | Parameter name | Description                    |
  | --------------- | -------------- | ------------------------------ |
  | \[in]           | port           | GPIO PORT                      |
  | \[in]           | in             | <p>0: output.<br>1: input.</p> |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MALLOC\_FAILED: Memory application failed

  OPRT\_OS\_ADAPTER\_GPIO\_INOUT\_SET\_FAILED: fail

**os\_adapter\_gpio\_mode\_set**

```
int os_adapter_gpio_mode_set(const TY_GPIO_PORT_E port, const TY_GPIO_MODE_E mode)

```

* **Description**

  Sets the GPIO mode function.

  | Mode                       | Description            |
  | -------------------------- | ---------------------- |
  | TY\_GPIO\_PULLUP           | Pull mode              |
  | TY\_GPIO\_PULLDOWN         | Dropdown mode          |
  | TY\_GPIO\_PULLUP\_PULLDOWN | Push-pull output mode  |
  | TY\_GPIO\_OPENDRAIN        | Open leaky output mode |
  | TY\_GPIO\_FLOATING         | Input mode             |
* **Parameter**

  | Input or output | Parameter name | Description       |
  | --------------- | -------------- | ----------------- |
  | \[in]           | port           | GPIO PORT         |
  | \[in]           | mode           | GPIO working mode |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MALLOC\_FAILED: Memory application failed

  OPRT\_OS\_ADAPTER\_GPIO\_MODE\_SET\_FAILED: fail

**os\_adapter\_gpio\_read**

```
int os_adapter_gpio_read(const TY_GPIO_PORT_E port)

```

* **Description**

  Reads the GPIO port level state function.
* **Parameter**

  | Input or output | Parameter name | Description |
  | --------------- | -------------- | ----------- |
  | \[in]           | port           | GPIO PORT   |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_GPIO\_READ\_FAILED: fail

**os\_adapter\_gpio\_write**

```
int os_adapter_gpio_write(const TY_GPIO_PORT_E port, const bool high)

```

* **Description**

  Sets the GPIO port level state function.
* **Parameter**

  | Input or output | Parameter name | Description                                             |
  | --------------- | -------------- | ------------------------------------------------------- |
  | \[in]           | port           | GPIO port                                               |
  | \[in]           | high           | <p>Level status.<br>0: low level.<br>1: high level.</p> |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_GPIO\_WRITE\_FAILED: fail

**os\_adapter\_gpio\_irq\_init**

```
int os_adapter_gpio_irq_init(const TY_GPIO_PORT_E port, const TY_GPIO_CALLBACK cb, const TY_GPIO_IRQ_TRIG_TYPE trig_type)

```

* **Description**

  Initializes the GPIO port interrupt function.

  | trig\_type    | Description           |
  | ------------- | --------------------- |
  | TY\_IRQ\_NONE | N/A                   |
  | TY\_IRQ\_RISE | Rising edge triggers  |
  | TY\_IRQ\_FALL | Falling edge triggers |
  | TY\_TRQ\_BOTH | Triggers bilaterally  |
  | TY\_IRQ\_HIGH | High level triggers   |
  | TY\_IRQ\_LOW  | lLow level triggers   |
* **Parameter**

  | Input or output | Parameter name | Description                                  |
  | --------------- | -------------- | -------------------------------------------- |
  | \[in]           | port           | GPIO PORT                                    |
  | \[in]           | cb             | Interrupt callback function assigned to GPIO |
  | \[in]           | trig\_type     | Interrupt trigger source type                |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_GPIO\_IRQ\_INIT\_FAILED: fail

#### I2C <a href="#title-20-i2c" id="title-20-i2c"></a>

The Tuya HAL I2C provides interfaces for I2C operation and requires different platforms to implement it.

**os\_adapter\_i2c\_open**

```
int os_adapter_i2c_open(int port)

```

* **Description**

  Turn on the I2C device.
* **Parameter**

  | Input or output | Parameter name | Description       |
  | --------------- | -------------- | ----------------- |
  | \[in]           | port           | I2c device number |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MALLOC\_FAILED: Memory application failed

  OPRT\_OS\_ADAPTER\_I2C\_OPEN\_FAILED: fail

**os\_adapter\_i2c\_close**

```
int os_adapter_i2c_close(int port)

```

* **Description**

  Turn off the I2C device.
* **Parameter**

  | Input or output | Parameter name | Description       |
  | --------------- | -------------- | ----------------- |
  | \[in]           | port           | I2c device number |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_I2C\_CLOSE\_FAILED: fail

**os\_adapter\_i2c\_read**

```
int os_adapter_i2c_read(int port, int addr, uint8_t* buf, int len)

```

* **Description**

  Reads the data.
* **Parameter**

  | Input or output | Parameter name | Description                   |
  | --------------- | -------------- | ----------------------------- |
  | \[in]           | port           | i2c device number             |
  | \[in]           | addr           | slave address                 |
  | \[out]          | buf            | reads the data buffer address |
  | \[in]           | len            | buffer length                 |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MALLOC\_FAILED: Memory application failed

  OPRT\_OS\_ADAPTER\_I2C\_READ\_FAILED: fail

**os\_adapter\_i2c\_write**

```
int os_adapter_i2c_write(int port, int addr, uint8_t const* buf, int len)

```

* **Description**

  Writes data.
* **Parameter**

  | Input or output | Parameter name | Description                   |
  | --------------- | -------------- | ----------------------------- |
  | \[in]           | port           | i2c device number             |
  | \[in]           | addr           | slave address                 |
  | \[in]           | buf            | reads the data buffer address |
  | \[in]           | len            | buffer length                 |
* **Return value**

  OPRT\_OS\_ADAPTER\_OK: success

  OPRT\_OS\_ADAPTER\_INVALID\_PARM: pass parameter error

  OPRT\_OS\_ADAPTER\_MALLOC\_FAILED: Memory application failed

  OPRT\_OS\_ADAPTER\_I2C\_WRITE\_FAILED: fail

### Tuya HAL interface registration <a href="#title-21-tuya-20hal-20interface-20registration" id="title-21-tuya-20hal-20interface-20registration"></a>

Once you have implemented the required Tuya HAL interface, you need to register these implementations with the Tuya Device OS SDK. This allows the SDK to use these implementations.

### HAL error codes <a href="#title-22-hal-20error-20codes" id="title-22-hal-20error-20codes"></a>

| No. | Name                                            | Value  | Message                                         |
| --- | ----------------------------------------------- | ------ | ----------------------------------------------- |
| 1   | OPRT\_OS\_ADAPTER\_INIT\_MUTEX\_ATTR\_FAILED    | -10100 | Failed to initialize synchronization properties |
| 2   | OPRT\_OS\_ADAPTER\_SET\_MUTEX\_ATTR\_FAILED     | -10101 | Failed to set synchronization properties        |
| 3   | OPRT\_OS\_ADAPTER\_DESTROY\_MUTEX\_ATTR\_FAILED | -10102 | Failed to destroy the synchronization attribute |
| 4   | OPRT\_OS\_ADAPTER\_INIT\_MUTEX\_FAILED          | -10103 | Failed to initialize mutex                      |
| 5   | OPRT\_OS\_ADAPTER\_MUTEX\_LOCK\_FAILED          | -10104 | Mutex lock failure                              |
| 6   | OPRT\_OS\_ADAPTER\_MUTEX\_TRYLOCK\_FAILED       | -10105 | The mutex failed to try to lock                 |
| 7   | OPRT\_OS\_ADAPTER\_MUTEX\_LOCK\_BUSY            | -10106 | Mutex busy                                      |
| 8   | OPRT\_OS\_ADAPTER\_MUTEX\_UNLOCK\_FAILED        | -10107 | Mutex unlock failed                             |
| 9   | OPRT\_OS\_ADAPTER\_MUTEX\_RELEASE\_FAILED       | -10108 | Mutex release failed                            |
| 10  | OPRT\_OS\_ADAPTER\_CR\_MUTEX\_ERR               | -10109 | Mutex creation failed                           |
| 11  | OPRT\_OS\_ADAPTER\_MEM\_PARTITION\_EMPTY        | -10110 | Memory partition is empty                       |
| 12  | OPRT\_OS\_ADAPTER\_MEM\_PARTITION\_FULL         | -10111 | Memory partition is full                        |
| 13  | OPRT\_OS\_ADAPTER\_MEM\_PARTITION\_NOT\_FOUND   | -10112 | Memory partition does not exist                 |
| 14  | OPRT\_OS\_ADAPTER\_INIT\_SEM\_FAILED            | -10113 | Failed to initialize semaphore                  |
| 15  | OPRT\_OS\_ADAPTER\_WAIT\_SEM\_FAILED            | -10114 | Failed to wait for the semaphore                |
| 16  | OPRT\_OS\_ADAPTER\_POST\_SEM\_FAILED            | -10115 | Failed to release semaphore                     |
| 17  | OPRT\_OS\_ADAPTER\_THRD\_STA\_UNVALID           | -10116 | Thread status is illegal                        |
| 18  | OPRT\_OS\_ADAPTER\_THRD\_CR\_FAILED             | -10117 | Thread creation failed                          |
| 19  | OPRT\_OS\_ADAPTER\_THRD\_JOIN\_FAILED           | -10118 | Thread JOIN function call failed                |
| 20  | OPRT\_OS\_ADAPTER\_THRD\_SELF\_CAN\_NOT\_JOIN   | -10119 | The own thread cannot call the JOIN function    |
| 21  | OPRT\_OS\_ADAPTER\_COND\_CREATE\_FAILED         | -10120 | Failed to create condition variable             |
| 22  | OPRT\_OS\_ADAPTER\_COND\_RELEASE\_FAILED        | -10121 | Failed to release condition variable            |
| 23  | OPRT\_OS\_ADAPTER\_COND\_WAIT\_FAILED           | -10122 | Failed to wait for condition variable           |
| 24  | OPRT\_OS\_ADAPTER\_COND\_WAIT\_TIMEOUT          | -10123 | Wait for condition variable timeout             |
| 25  | OPRT\_OS\_ADAPTER\_COND\_SIGNAL\_FAILED         | -10124 | Failure to notify condition variables           |
| 26  | OPRT\_OS\_ADAPTER\_COND\_BROADCAST\_FAILED      | -10125 | Failed to broadcast condition variable          |

#### Tuya Build.conf <a href="#title-23-tuya-20build.conf" id="title-23-tuya-20build.conf"></a>

| No. | Name                        | Default             | Message                                                                                                                                                         |
| --- | --------------------------- | ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 1   | TUYA\_IOT\_DEBUG            | 1                   | Set DEBUG mode                                                                                                                                                  |
| 2   | KV\_KEY\_SEED               | None.               | Generate KEY-VALUE to store the seed of encrypted KEY1, in order to be compatible with old products (RTOS, LINUX use), new products do not need to provide      |
| 3   | KV\_KEY\_PROJECT\_NAME      | None.               | Generate KEY-VALUE to store the seed of encrypted KEY, in order to be compatible with the old product (used by LINUX), the new product does not need to provide |
| 4   | WIFI\_GW                    | 1                   | Enable WIFI                                                                                                                                                     |
| 5   | GW\_SUPPORT\_WIRED\_WIFI    | 0                   | To enable wired + WIFI, you must ensure that WIFI\_GW is also enabled                                                                                           |
| 6   | TY\_BT\_MOD                 | 0                   | Bluetooth enabled                                                                                                                                               |
| 7   | ENABLE\_STATION\_AP\_MODE   | 0                   | Enable Station AP mode                                                                                                                                          |
| 8   | KV\_FILE                    | 0                   | Enable KV\_FILE, if it is 0, then use KV\_FLASH, generally linux is set to 1, ROTS is set to 0                                                                  |
| 9   | SHUTDOWN\_MODE              | 1                   | Enable Socket shutdown mode                                                                                                                                     |
| 10  | LITTLE\_END                 | 1                   | Set the system endian mode                                                                                                                                      |
| 11  | TLS\_SESSION                | 0                   | Enable TLS session retention, each session retention will consume 240 bytes of memory                                                                           |
| 12  | TLS\_MODE                   | TLS\_TUYA\_ECC\_PSK | TLS mode                                                                                                                                                        |
| 13  | ENABLE\_CLOUD\_OPERATION    | 0                   | Enable streaming service                                                                                                                                        |
| 14  | ENABLE\_SYS\_RPC            | 0                   | Enable RPC                                                                                                                                                      |
| 15  | TY\_SECURITY\_CHIP          | 0                   | Enable security chip capability, turn on when there is a security chip                                                                                          |
| 16  | TY\_RTC                     | 1                   | Enable RTC                                                                                                                                                      |
| 17  | TY\_WATCHDOG                | 1                   | Enable watchdog                                                                                                                                                 |
| 18  | TUYA\_OPERATOR\_TYPE        | 0                   | Third-party operator ID reporting, enabling different operators by bit                                                                                          |
| 19  | ENABLE\_HTTP\_TRUNK         | 0                   | Enable HTTP TRUNK mode                                                                                                                                          |
| 20  | QRCODE\_ACTIVE\_MODE        | 0                   | Enable scan code distribution network                                                                                                                           |
| 21  | ENABLE\_AP\_FAST\_CONNECT   | 0                   | Enable AP fast connection mode                                                                                                                                  |
| 22  | TY\_WIFI\_FFC               | 0                   | Enable FFC                                                                                                                                                      |
| 23  | ENABLE\_GOOGLE\_LOCAL\_HOME | 0                   | Enable google local home control                                                                                                                                |
| 24  | ENABLE\_LAN\_LINKAGE        | 1                   | Enable LAN linkage                                                                                                                                              |
