TuyaOSPort TuyaOSPort TuyaOS to Linux Platforms

Port TuyaOS to Linux Platforms

Last Updated on : 2023-08-11 08:20:56download

The APIs for the Linux operating system, network, and file system are unified. TuyaOS frameworks provide a set of Linux APIs that can be used directly, unless specific requirements exist.

The hardware-related capabilities may vary by hardware and chips. TuyaOS offers null functions for these capabilities, allowing you to adapt them based on your hardware and chip.

Adapt compilation

The TuyaOS kernel on top of Linux comes with compilation scripts, which are stored in the tuyaos/ directory of the template folder. The firmware compilation entry tuyaos/build.sh directly calls tuyaos/Makefile to compile files into an executable. You can adapt it to your needs.

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