Port TuyaOS to RTOS Platforms

Last Updated on : 2024-06-25 03:41:34download

The APIs of various real-time operating systems (RTOS) are not standardized. TuyaOS offers a set of APIs to help you adapt your software to the target platform for a stable and consistent runtime. This topic describes how to adapt the API to fit the requirements of your OS and hardware.

Adapt compilation

The TuyaOS kernel on top of the RTOS does not come with the default compilation adaptation. tuyaos/build.sh is the entry point to firmware compilation, which can be modified to your needs.

You only need to compile the chip vendor-related files and link the TuyaOS library, without compiling TuyaOS files.

In order to guarantee compatibility and stability with Tuya IoT cloud services, we recommend using the mbedtls library that comes with TuyaOS, even if it already exists in your system. This avoids potential conflict issues and reduces the possibility of unpredictable errors.

Link options and path references:

# The root directory, which is the `software/TuyaOS` directory.
ROOT_DIR ?= $(abspath ../../../)

# Variables used for compilation. Save the compiled firmware to this directory.
OUTPUT_DIR = $(ROOT_DIR)/$(APP_PATH)/output/$(APP_NAME)_$(APP_VER)

# Path of TuyaOS libraries
TUYAOS_LIB_DIR = $(ROOT_DIR)/libs

# The recommended link options. A `.a` file might be empty. If your toolchain does not support an empty `.a` file, you can remove it.
LINKFLAGS = -L$(TUYAOS_LIB_DIR) -Xlinker "-(" -l$(APP_NAME) -ltuyaapp_components -ltuyaapp_drivers -ltuyaos -ltuyaos_adapter -Xlinker "-)"

If you need to reference complex headers from the chip vendor in the files of the tuyaos_adapter/src directory during adaptation and compilation, simply remove these files from the TuyaOS compilation system and add them to the chip vendor-specific compilation file list. Modify the tuyaos_adapter/local.mk file as shown below.

###################################################################################
# Note
# You can compile tuyaos_adapter in the chip vendor-specific environment, and if you do, there is no need to modify this file.
# If you don't, copy the source code in tuyaos_adapter/src/ to
# LOCAL_SRC_FILES, and place the necessary header files for compilation to LOCAL_TUYA_SDK_CFLAGS.
###################################################################################
# The `include` directory contains the source files of Tuya-specific components. Do not modify
LOCAL_SRC_FILES := $(shell find $(LOCAL_PATH)/include -name "*.c" -o -name "*.cpp" -o -name "*.cc")
# To compile the adaptation layer together with TuyaOS, open this.
#LOCAL_SRC_FILES += $(shell find $(LOCAL_PATH)/src -name "*.c" -o -name "*.cpp" -o -name "*.cc")
# To compile the adaptation layer together with TuyaOS, add the path of the chip vendor's header files here.
#LOCAL_TUYA_SDK_INC +=
###################################################################################

Adapt the program entry point

tuya_app_main is the TuyaOS entry point. You need to embed this function into the appropriate location in the main function where your chip platform program starts.

Adapt the system APIs

The system APIs are used for various operating system functionalities, including basic operations, threads, semaphores, mutexes, I/O, memory management, sleep mode, and firmware updates. TuyaOS can function properly only after the system APIs are adapted.

Memory management

APIs
Description
VOID_T *tkl_system_malloc ( SIZE_T size ) Request memory allocation.
VOID_T tkl_system_free ( VOID_T* ptr ) Free the memory.
VOID_T *tkl_system_calloc ( size_t nitems, size_t size ) Allocate the memory contiguously.
VOID_T *tkl_system_realloc ( VOID_T *ptr, size_t size ) Reallocate the memory.
INT_T tkl_system_get_free_heap_size ( VOID_T ) Get the free space on the heap.

Thread management

APIs
Description
OPERATE_RET tkl_thread_create (
TKL_THREAD_HANDLE* thread,
CONST CHAR_T* name,
UINT_T stack_size,
UINT_T priority,
CONST THREAD_FUNC_T func,
VOID_T* CONST arg )
Create and start a thread.
OPERATE_RET tkl_thread_release ( TKL_THREAD_HANDLE thread ) Destroy the thread and free the memory.
OPERATE_RET tkl_thread_get_watermark (
TKL_THREAD_HANDLE thread,
UINT_T* watermark )
Get the water mark of the thread’s stack.
OPERATE_RET tkl_thread_diagnose ( TKL_THREAD_HANDLE thread ) Get the thread diagnostic information, such as about the stack and register.

Mutexes

APIs
Description
OPERATE_RET tkl_mutex_create_init ( TKL_MUTEX_HANDLE *pMutexHandle ) Create and initialize a mutex. Recursive calls must be supported.
OPERATE_RET tkl_mutex_release ( CONST TKL_MUTEX_HANDLE mutexHandle ) Destroy the mutex and free the memory.
OPERATE_RET tkl_mutex_lock ( CONST TKL_MUTEX_HANDLE mutexHandle ) Lock the mutex. If it has been locked, block the thread.
OPERATE_RET tkl_mutex_trylock ( CONST TKL_MUTEX_HANDLE mutexHandle ) Try to lock the mutex. If it has been locked, an error is returned.
OPERATE_RET tkl_mutex_unlock ( CONST TKL_MUTEX_HANDLE mutexHandle ) Unlock the mutex.

Semaphores

APIs
Description
OPERATE_RET tkl_semaphore_create_init (
TKL_SEM_HANDLE *handle,
UINT_T sem_cnt,
UINT_T sem_max )
Create and initialize a semaphore.
OPERATE_RET tkl_semaphore_wait ( CONST TKL_SEM_HANDLE handle, UINT_T timeout ) Wait on the semaphore. A timeout of TKL_SEM_WAIT_FOREVER means to wait indefinitely.
OPERATE_RET tkl_semaphore_post ( CONST TKL_SEM_HANDLE handle ) Signal a semaphore.
OPERATE_RET tkl_semaphore_release ( CONST TKL_SEM_HANDLE handle ) Destroy the semaphore and free the memory.

Output

APIs
Description
VOID_T tkl_log_output ( CONST CHAR_T *format, … ) Log output. The parameter is variadic.

OTA

APIs
Description
OPERATE_RET tkl_ota_get_ability (
UINT_T *image_size,
TUYA_OTA_TYPE_E *type )
The OTA capability supported by the Tuya Developer Platform. For platform porting, select TUYA_OTA_FULL.
OPERATE_RET tkl_ota_start_notify (
UINT_T image_size,
TUYA_OTA_TYPE_E type,
TUYA_OTA_PATH_E path )
Notify the chip platform of preparing the resource to write data.
OPERATE_RET tkl_ota_data_process (
TUYA_OTA_DATA_T *pack,
UINT_T *remain_len )
Process firmware data write. TuyaOS pulls data from the cloud and writes it to the flash memory of the chip platform.
OPERATE_RET tkl_ota_end_notify ( BOOL_T reset ) Notify the chip platform of verifying firmware and updating status when data write is completed.

Low power

APIs
Description
OPERATE_RET tkl_cpu_sleep_mode_set (
BOOL_T enable,
TUYA_CPU_SLEEP_MODE_E mode )
Make the CPU enter low power mode.

Reference

APIs
Description
VOID_T tkl_system_reset ( VOID_T ) Restart the device.
SYS_TICK_T tkl_system_get_tick_count ( VOID_T ) Get the device uptime in system ticks.
SYS_TIME_T tkl_system_get_millisecond ( VOID_T ) Get the device uptime in milliseconds.
INT_T tkl_system_get_random ( UINT_T range ) Get a random number from 0 to range. Be sure to correctly implement this important function.
TUYA_RESET_REASON_E tkl_system_get_reset_reason ( CHAR_T** describe ) Get the reason for device restart. You can just return the value and ignore the parameter.
VOID_T tkl_system_sleep ( UINT_T num_ms) Put the current task to sleep for a specified time, in milliseconds.
OPERATE_RET tkl_system_get_cpu_info (
TUYA_CPU_INFO_T **cpu_ary,
INT_T *cpu_cnt )
Get the CPU usage.

Adapt the flash

On top of the RTOS system, TuyaOS provides a unified flash-based solution for data storage and file operation.

Flash partitioning

Proper flash partitioning is important for an RTOS system to ensure smooth execution of TuyaOS programs.

  • Partition type

    The comments in the code below describe the purposes of different partitions. The one specified as required must be allocated with the required size. Optimal flash partitioning requires the consideration of both current and future system needs.

    /**
    * @brief flash type
    *
    */
    typedef enum {
        TUYA_FLASH_TYPE_BTL0 = 0,                // (Partitioning required) The first-stage bootloader. The size depends on your needs.
        TUYA_FLASH_TYPE_BTL1,            // The second-stage bootloader. The size depends on your needs. Ignore this partition if not needed.
        TUYA_FLASH_TYPE_STACK,            // Protocol storage. The size depends on your needs. Ignore this partition if not needed.
        TUYA_FLASH_TYPE_APP,            // (Partitioning required) The application segment. The size depends on your needs.
        TUYA_FLASH_TYPE_OTA,            // (Partitioning required) OTA file storage. The size depends on your needs.
        TUYA_FLASH_TYPE_USER0,            // User data partition 1, used by the application layer. The size depends on your needs. Ignore this partition if not needed.
        TUYA_FLASH_TYPE_USER1,            // User data partition 2, used by the application layer. The size depends on your needs. Ignore this partition if not needed.
        TUYA_FLASH_TYPE_KV_DATA,                    // (Partitioning required) KV database, at least 32 KB.
        TUYA_FLASH_TYPE_KV_SWAP,                // (Deprecated) KV read-write swap partition. Ignore it.
        TUYA_FLASH_TYPE_KV_KEY,            // (Partitioning required) KV key storage, 4 KB.
        TUYA_FLASH_TYPE_UF,                // (Partitioning required) File storage, at least 32 KB.
        TUYA_FLASH_TYPE_INFO,            // Ignore
        TUYA_FLASH_TYPE_KV_UF,            // Ignore
        TUYA_FLASH_TYPE_KV_PROTECT,                    // (Partitioning required) KV critical data backup, at least 4 KB.
        TUYA_FLASH_TYPE_RCD,            // RCD, gateway-dedicated data storage mechanism. Not needed for non-gateway devices.
        TUYA_FLASH_TYPE_RSV0,            // The following is reserved for future use.
        TUYA_FLASH_TYPE_RSV1,
        TUYA_FLASH_TYPE_RSV2,
        TUYA_FLASH_TYPE_RSV3,
        TUYA_FLASH_TYPE_RSV4,
        TUYA_FLASH_TYPE_RSV5,
        TUYA_FLASH_TYPE_RSV6,
        TUYA_FLASH_TYPE_RSV7,
        TUYA_FLASH_TYPE_ALL,
        TUYA_FLASH_TYPE_MAX,
    } TUYA_FLASH_TYPE_E;
    
  • Partition description

    A functional partition can be composed of multiple small and scattered partitions. TUYA_FLASH_TYPE_MAX_PARTITION_NUM defaults to 10.

    typedef struct {
        UINT_T partition_num;                                    // The number of partitions.
        TUYA_FLASH_PARTITION_T partition[TUYA_FLASH_TYPE_MAX_PARTITION_NUM];    // partition
    } TUYA_FLASH_BASE_INFO_T;
    

Flash operations

APIs
Description
OPERATE_RET tkl_flash_read ( UINT32_T addr,
UCHAR_T *dst,
UINT32_T size )
Read the flash.
OPERATE_RET tkl_flash_write ( UINT32_T addr,
CONST UCHAR_T *src,
UINT32_T size )
Write the flash.
OPERATE_RET tkl_flash_erase ( UINT32_T addr, UINT32_T size ) Erase the flash.
OPERATE_RET tkl_flash_lock ( UINT32_T addr, UINT32_T size ) Lock the flash.
OPERATE_RET tkl_flash_unlock ( UINT32_T addr, UINT32_T size ) Unlock the flash.
OPERATE_RET tkl_flash_get_one_type_info (
TUYA_FLASH_TYPE_E type,
TUYA_FLASH_BASE_INFO_T *info )
Get the information about a specific partition type.

Adapt the RTC

Platforms that come with a real-time clock (RTC) can implement the RTC APIs to ensure accurate timekeeping.

After the device is paired and activated, TuyaOS requests the current time from the cloud and sets the RTC time accordingly. TuyaOS depends on the RTC time. When the difference between the local time and server time exceeds five seconds, TuyaOS will sync time with the cloud to prevent further deviation. However, when the device is offline, it entirely depends on the RTC to accurately keep track of time.

APIs
Description
OPERATE_RET tkl_rtc_init ( VOID_T ) Initialize the RTC to make it function properly. The RTC timekeeping cycle should be in seconds.
OPERATE_RET tkl_rtc_deinit ( VOID_T ) Deinitialize the RTC and free the allocated memory.
OPERATE_RET tkl_rtc_time_set ( TIME_T time_sec ) Set the RTC timestamp, in seconds. From this timestamp forth, the RTC starts counting time.
OPERATE_RET tkl_rtc_time_get ( TIME_T *time_sec ) Get the current system timestamp.

Adapt the watchdog

Platforms that come with a watchdog can implement the watchdog APIs to allow a device to recover from anomalies by a restart, thus reducing the risk of failure and minimizing downtime. In TuyaOS, the feeding operation is performed in a separate and high-priority thread, ensuring that it remains unaffected by business operations.

APIs
Description
UINT32_T tkl_watchdog_init ( TUYA_WDOG_BASE_CFG_T *cfg ) Initialize the watchdog. The return value is the actual timeout period, which should be at least five seconds.
OPERATE_RET tkl_watchdog_deinit ( VOID_T ) Deinitialize the watchdog and free the allocated memory.
OPERATE_RET tkl_watchdog_refresh ( VOID_T ) Refresh the watchdog and reset the watchdog status. The refresh cycle is one-third of the actual one.

Adapt the network interface

Use TuyaOS’s built-in lwIP

TuyaOS’s built-in lightweight IP (lwIP) library allows you to easily implement the adaptation with just a few APIs. You only need to adapt the APIs for the network interface card (NIC) to enable communication between the NIC driver and the protocol stack.

APIs
Description
OPERATE_RET tkl_ethernetif_init ( TKL_NETIF_HANDLE netif ) Initialize NIC
OPERATE_RET tkl_ethernetif_output ( TKL_NETIF_HANDLE netif, TKL_PBUF_HANDLE p) NIC sends data.
OPERATE_RET tkl_ethernetif_recv ( TKL_NETIF_HANDLE netif, TKL_PBUF_HANDLE p ) NIC receives data.

Use chip platform’s built-in lwIP

To use the protocol stack provided by the chip platform, you need to adapt the following APIs.

APIs
Description
TUYA_ERRNO tkl_net_get_errno ( VOID ) Get the errno
OPERATE_RET tkl_net_fd_set ( CONST INT_T fd, TUYA_FD_SET_T* fds ) Add a file descriptor to fdset.
OPERATE_RET tkl_net_fd_clear ( CONST INT_T fd, TUYA_FD_SET_T* fds ) Remove a file descriptor from fdset.
OPERATE_RET tkl_net_fd_isset ( CONST INT_T fd, TUYA_FD_SET_T* fds ) Check whether a file descriptor exists in fdset.
OPERATE_RET tkl_net_fd_zero ( TUYA_FD_SET_T* fds ) Empty fdset.
INT_T tkl_net_socket_create ( CONST TUYA_PROTOCOL_TYPE_E type ) Create a socket.
TUYA_ERRNO tkl_net_close ( CONST INT_T fd ) Close a socket.
INT_T tkl_net_select (
CONST INT_T maxfd,
TUYA_FD_SET_T *readfds,
TUYA_FD_SET_T *writefds,
TUYA_FD_SET_T *errorfds,
CONST UINT_T ms_timeout)
Select a socket.
TUYA_ERRNO tkl_net_connect (
CONST INT_T fd,
CONST TUYA_IP_ADDR_T addr,
CONST UINT16_T port)
Connect to a socket.
TUYA_ERRNO tkl_net_connect_raw (
CONST INT_T fd,
VOID *p_socket_addr,
CONST INT_T len )
Connect to a raw socket.
TUYA_ERRNO tkl_net_bind (
CONST INT_T fd,
CONST TUYA_IP_ADDR_T addr,
CONST UINT16_T port )
Bind a socket with an IP address and port number.
TUYA_ERRNO tkl_net_listen ( CONST INT_T fd, CONST INT_T backlog ) Put a socket into a listening state.
TUYA_ERRNO tkl_net_accept ( CONST INT_T fd, TUYA_IP_ADDR_T *addr, UINT16_T *port ) Accept a connection on a socket.
TUYA_ERRNO tkl_net_send ( CONST INT_T fd, CONST VOID *buf, CONST UINT_T nbytes ) Send data to a connected socket.
TUYA_ERRNO tkl_net_send_to (
CONST INT_T fd,
CONST VOID *buf,
CONST UINT_T nbytes,
CONST TUYA_IP_ADDR_T addr,
CONST UINT16_T port )
Send data to a socket, whether it is connected or not.
TUYA_ERRNO tkl_net_recv (
CONST INT_T fd,
VOID *buf,
CONST UINT_T nbytes )
Receive data from a connected socket.
INT_T tkl_net_recv_nd_size (
CONST INT_T fd,
VOID *buf,
CONST UINT_T buf_size,
CONST UINT_T nd_size )
Set the maximum socket receive buffer.
TUYA_ERRNO tkl_net_recvfrom (
CONST INT_T fd,
VOID *buf,
CONST UINT_T nbytes,
TUYA_IP_ADDR_T *addr,
UINT16_T *port )
Receive data and store the source address.
OPERATE_RET tkl_net_gethostbyname ( CONST CHAR_T *domain, TUYA_IP_ADDR_T *addr ) Get the IP address for a specified host name via DNS.
INT_T tkl_net_get_nonblock ( CONST INT_T fd ) Get whether a socket is blocked.
OPERATE_RET tkl_net_set_block ( CONST INT_T fd, CONST BOOL_T block ) Set the blocking mode on a socket.
TUYA_ERRNO tkl_net_shutdown ( CONST INT_T fd, CONST INT_T how ) Shut down a socket.
OPERATE_RET tkl_net_socket_bind ( CONST INT_T fd, CONST CHAR_T *ip ) Set socket binding.
OPERATE_RET tkl_net_set_cloexec ( CONST INT_T fd ) Set the close-on-exec flag.
OPERATE_RET tkl_net_get_socket_ip ( CONST INT_T fd, TUYA_IP_ADDR_T *addr ) Get the IP address of a socket.
OPERATE_RET tkl_net_set_timeout (
CONST INT_T fd,
CONST INT_T ms_timeout,
CONST TUYA_TRANS_TYPE_E type )
Set the timeout value of a socket.
OPERATE_RET tkl_net_set_bufsize (
CONST INT_T fd,
CONST INT_T buf_size,
CONST TUYA_TRANS_TYPE_E type)
Set the buffer size of a socket.
OPERATE_RET tkl_net_set_reuse ( CONST INT_T fd ) Set socket reuse.
OPERATE_RET tkl_net_disable_nagle ( CONST INT_T fd ) Disable Nagle’s algorithm.
OPERATE_RET tkl_net_set_broadcast ( CONST INT_T fd ) Set socket broadcast.
OPERATE_RET tkl_net_set_keepalive (
INT_T fd,
CONST BOOL_T alive,
CONST UINT_T idle,
CONST UINT_T intr,
CONST UINT_T cnt )
Set socket keep-alive.
OPERATE_RET tkl_net_getsockname ( INT_T fd, TUYA_IP_ADDR_T *addr, UINT16_T *port ) Get the IP address and port number of a socket.
OPERATE_RET tkl_net_getpeername ( INT_T fd, TUYA_IP_ADDR_T *addr, UINT16_T *port ) Get the IP address and port number of the peer connected to a socket.
OPERATE_RET tkl_net_setsockopt (
CONST INT_T fd,
CONST TUYA_OPT_LEVEL level,
CONST TUYA_OPT_NAME optname,
CONST VOID_T *optval,
CONST INT_T optlen )
Set the options associated with a socket.
OPERATE_RET tkl_net_getsockopt (
CONST INT_T fd,
CONST TUYA_OPT_LEVEL level,
CONST TUYA_OPT_NAME optname,
VOID_T *optval,
INT_T *optlen )
Get the options associated with a socket.

Adapt the connection APIs

The connection APIs serve as drivers for various types of connectivity, including wired, Wi-Fi, and Bluetooth Low Energy (LE). You can adapt them to the needs of your connectivity solution.

Wired

APIs
Description
OPERATE_RET tkl_wired_get_status ( TKL_WIRED_STAT_E *status ) Get the status of the wired connection.
OPERATE_RET tkl_wired_set_status_cb ( TKL_WIRED_STATUS_CHANGE_CB cb ) Set the notification callback to invoke when the status of the wired connection changes.
OPERATE_RET tkl_wired_get_ip ( NW_IP_S *ip ) Get the IP address of the network interface.
OPERATE_RET tkl_wired_get_mac ( NW_MAC_S *mac ) Get the MAC address of the network interface.
OPERATE_RET tkl_wired_set_mac ( CONST NW_MAC_S *mac ) Set the MAC address of the network interface.

Wi-Fi

APIs
Description
OPERATE_RET tkl_wifi_init ( WIFI_EVENT_CB cb ) Initialize Wi-Fi.
OPERATE_RET tkl_wifi_scan_ap (
CONST SCHAR_T *ssid,
AP_IF_S **ap_ary,
UINT_T *num )
Scan for available access points (APs). You can specify a target SSID.
OPERATE_RET tkl_wifi_release_ap ( AP_IF_S *ap ) Free the memory used by AP scan to prevent memory leaks, which corresponds to the AP scan function.
OPERATE_RET tkl_wifi_start_ap ( CONST WF_AP_CFG_IF_S *cfg ) Start the AP. The SSID and password must be specified.
OPERATE_RET tkl_wifi_stop_ap ( VOID_T ) Stop the AP and free the resource, which corresponds to the AP start function.
OPERATE_RET tkl_wifi_set_cur_channel ( CONST UCHAR_T chan ) Set the Wi-Fi operating channel.
OPERATE_RET tkl_wifi_get_cur_channel ( UCHAR_T *chan ) Get the Wi-Fi operating channel.
OPERATE_RET tkl_wifi_set_sniffer (
CONST BOOL_T en,
CONST SNIFFER_CALLBACK cb )
Set the sniffer callback. The original packet captured by the sniffer should be reported.
OPERATE_RET tkl_wifi_get_ip ( CONST WF_IF_E wf, NW_IP_S *ip ) Get the IP address of the Wi-Fi network interface.
OPERATE_RET tkl_wifi_set_ip ( CONST WF_IF_E wf, NW_IP_S *ip ) Set the IP address of the Wi-Fi network interface.
OPERATE_RET tkl_wifi_set_mac ( CONST WF_IF_E wf, CONST NW_MAC_S *mac ) Set the MAC address of the Wi-Fi network interface.
OPERATE_RET tkl_wifi_get_mac ( CONST WF_IF_E wf, NW_MAC_S *mac ) Get the MAC address of the Wi-Fi network interface.
OPERATE_RET tkl_wifi_set_work_mode ( CONST WF_WK_MD_E mode ) Set the operating mode of the Wi-Fi network.
OPERATE_RET tkl_wifi_get_work_mode ( WF_WK_MD_E *mode ) Get the operating mode of the Wi-Fi network.
OPERATE_RET tkl_wifi_get_connected_ap_info ( FAST_WF_CONNECTED_AP_INFO_T **fast_ap_info ) Get the information about the connected AP.
OPERATE_RET tkl_wifi_get_bssid ( UCHAR_T *mac ) Get the basic service set identifier (BSSID) of the Wi-Fi network interface.
OPERATE_RET tkl_wifi_set_country_code ( CONST COUNTRY_CODE_E ccode ) Set the country code of the Wi-Fi network interface.
OPERATE_RET tkl_wifi_set_rf_calibrated ( VOID_T ) Set the Wi-Fi RF calibration flag for production testing, which is necessary if you use Tuya’s production testing.
OPERATE_RET tkl_wifi_set_lp_mode ( CONST BOOL_T enable, CONST UCHAR_T dtim ) Set the low power mode of the Wi-Fi.
OPERATE_RET tkl_wifi_station_fast_connect ( CONST FAST_WF_CONNECTED_AP_INFO_T *fast_ap_info ) Fast connect to a saved AP.
OPERATE_RET tkl_wifi_station_connect ( CONST SCHAR_T *ssid, CONST SCHAR_T *passwd ) Connect to an AP.
OPERATE_RET tkl_wifi_station_disconnect ( VOID_T ) Disconnect from an AP.
OPERATE_RET tkl_wifi_station_get_conn_ap_rssi ( SCHAR_T *rssi ) Get the signal strength.
OPERATE_RET tkl_wifi_station_get_status ( WF_STATION_STAT_E *stat ) Get the connection status.
OPERATE_RET tkl_wifi_send_mgnt ( CONST UCHAR_T *buf, CONST UINT_T len ) Send a management frame.
OPERATE_RET tkl_wifi_register_recv_mgnt_callback (
CONST BOOL_T enable,
CONST WIFI_REV_MGNT_CB recv_cb )
Receive a management frame.
OPERATE_RET tkl_wifi_ioctl ( WF_IOCTL_CMD_E cmd, VOID *args ) Reserved for future use.

Bluetooth

A Bluetooth device can be either a central or peripheral:

  • A peripheral device accepts an incoming connection request after advertising. It can be controlled by a mobile app.
  • A central device scans for peripheral devices and initiates an outgoing connection request. It can be controlled by a remote.

You can adapt the APIs as needed.

  • Bluetooth stack

    APIs
    Description
    OPERATE_RET tkl_ble_stack_init ( UCHAR_T role ) Initialize the Bluetooth stack.
    OPERATE_RET tkl_ble_stack_deinit ( UCHAR_T role ) Deinitialize the Bluetooth stack, corresponding to Bluetooth stack initialization.
    OPERATE_RET tkl_ble_stack_gatt_link ( USHORT_T *p_link ) Get the GATT connection information.
    OPERATE_RET tkl_ble_gap_callback_register ( CONST TKL_BLE_GAP_EVT_FUNC_CB gap_evt ) Register GAP event callbacks.
    OPERATE_RET tkl_ble_gatt_callback_register ( CONST TKL_BLE_GATT_EVT_FUNC_CB gatt_evt ) Register GATT event callbacks.
    OPERATE_RET tkl_ble_gap_addr_set ( TKL_BLE_GAP_ADDR_T CONST *p_peer_addr ) Set the GAP address.
    OPERATE_RET tkl_ble_gap_address_get ( TKL_BLE_GAP_ADDR_T *p_peer_addr ) Get the GAP address.
    OPERATE_RET tkl_ble_gap_conn_param_update (
    USHORT_T conn_handle,
    TKL_BLE_GAP_CONN_PARAMS_T CONST *p_conn_params )
    Update the connection parameters.
    OPERATE_RET tkl_ble_gap_tx_power_set ( UCHAR_T role, INT_T tx_power ) Set the transmitter power.
    OPERATE_RET tkl_ble_gap_rssi_get ( USHORT_T conn_handle ) Get the Bluetooth signal strength.
  • Peripheral device

    APIs
    Description
    OPERATE_RET tkl_ble_gap_adv_start ( TKL_BLE_GAP_ADV_PARAMS_T CONST *p_adv_params ) Start GAP advertising.
    OPERATE_RET tkl_ble_gap_adv_stop ( VOID ) Stop GAP advertising.
    OPERATE_RET tkl_ble_gap_adv_rsp_data_set (
    TKL_BLE_DATA_T CONST *p_adv,
    TKL_BLE_DATA_T CONST *p_scan_rsp )
    Set the advertising response.
    OPERATE_RET tkl_ble_gap_adv_rsp_data_update (
    TKL_BLE_DATA_T CONST *p_adv,
    TKL_BLE_DATA_T CONST *p_scan_rsp )
    Update the advertising response.
    OPERATE_RET tkl_ble_gap_name_set ( CHAR_T *p_name ) Set the name.
    OPERATE_RET tkl_ble_gap_disconnect (
    USHORT_T conn_handle,
    UCHAR_T hci_reason )
    Disconnect from a central device.
    OPERATE_RET tkl_ble_gatts_service_add ( TKL_BLE_GATTS_PARAMS_T *p_service ) Add a service.
    OPERATE_RET tkl_ble_gatts_value_set (
    USHORT_T conn_handle,
    USHORT_T char_handle,
    UCHAR_T *p_data,
    USHORT_T length )
    Set the value of the specified attribute.
    OPERATE_RET tkl_ble_gatts_value_get (
    USHORT_T conn_handle,
    USHORT_T char_handle,
    UCHAR_T *p_data,
    USHORT_T length )
    Get the value of the specified attribute.
    OPERATE_RET tkl_ble_gatts_value_notify (
    USHORT_T conn_handle,
    USHORT_T char_handle,
    UCHAR_T *p_data,
    USHORT_T length )
    Notify the central device when a characteristic’s value changes, without acknowledgment.
    OPERATE_RET tkl_ble_gatts_value_indicate (
    USHORT_T conn_handle,
    USHORT_T char_handle,
    UCHAR_T *p_data,
    USHORT_T length )
    Notify the central device when a characteristic’s value changes, with acknowledgment.
    OPERATE_RET tkl_ble_gatts_exchange_mtu_reply (
    USHORT_T conn_handle,
    USHORT_T server_rx_mtu )
    Respond to MTU exchange.
  • Central device

    APIs
    Description
    OPERATE_RET tkl_ble_gap_scan_start ( TKL_BLE_GAP_SCAN_PARAMS_T CONST *p_scan_params ) Start scanning for GAP advertising packets.
    OPERATE_RET tkl_ble_gap_scan_stop ( VOID ) Stop scanning for GAP advertising packets, corresponding to scan start.
    OPERATE_RET tkl_ble_gap_connect (
    TKL_BLE_GAP_ADDR_T CONST *p_peer_addr,
    TKL_BLE_GAP_SCAN_PARAMS_T CONST *p_scan_params,
    TKL_BLE_GAP_CONN_PARAMS_T CONST *p_conn_params )
    Connect to a peripheral device.
    OPERATE_RET tkl_ble_gap_disconnect ( USHORT_T conn_handle, UCHAR_T hci_reason ) Disconnect from a peripheral device.
    OPERATE_RET tkl_ble_gattc_all_service_discovery ( USHORT_T conn_handle ) Discover all services.
    OPERATE_RET tkl_ble_gattc_all_char_discovery (
    USHORT_T conn_handle,
    USHORT_T start_handle,
    USHORT_T end_handle )
    Discover all characteristics.
    OPERATE_RET tkl_ble_gattc_char_desc_discovery (
    USHORT_T conn_handle,
    USHORT_T start_handle,
    USHORT_T end_handle )
    Discover all descriptors.
    OPERATE_RET tkl_ble_gattc_write_without_rsp (
    USHORT_T conn_handle,
    USHORT_T char_handle,
    UCHAR_T *p_data,
    USHORT_T length )
    Write to a GATT server without response.
    OPERATE_RET tkl_ble_gattc_write (
    USHORT_T conn_handle,
    USHORT_T char_handle,
    UCHAR_T *p_data,
    USHORT_T length )
    Write to a GATT server with response.
    OPERATE_RET tkl_ble_gattc_read ( USHORT_T conn_handle, USHORT_T char_handle ) Read from a GATT server.
    OPERATE_RET tkl_ble_gattc_exchange_mtu_request (
    USHORT_T conn_handle,
    USHORT_T client_rx_mtu )
    Request MTU exchange.

Adapt other peripherals

After you adapt the required APIs above, a minimum viable TuyaOS product is made. To connect to other peripherals, you can use drivers provided by the chip vendor or TuyaOS.

Acceptance testing

  • software/TuyaOS/apps/tuyaos_demo_examples includes a collection of test cases to help you verify the API adaptation. You can compile this directory, flash it to your development board, and execute the test cases through the command line.

  • To achieve thorough coverage, you can test the overall functionality and performance of your product simultaneously.