Porting Guide

Last Updated on : 2024-08-01 03:30:47download

Designed with a cross-platform approach, TuyaOS abstracts away the differences between hardware and systems by providing the TuyaOS Kernel Layer (TKL). The TKL API includes system and service features, such as memory allocation, mutex, and semaphores.

For the Linux operating system, the system API complies with the POSIX standard. You can use the example code provided in the TuyaOS SDK development environment directly, unless you have specific requirements. For the service API, you may need to adapt them to your particular hardware and chip.

This topic compares the legacy and new versions of the Wi-Fi and Bluetooth adaptation layer APIs and describes how to adapt the TKL APIs to run the TuyaOS SDK development framework properly on specific hardware platforms.

  • Legacy versions: IoT SDK v2.3.2 and earlier
  • New versions: TuyaOS SDK v3.6.1 and later

Adaptation comparison

Wi-Fi pairing adaptation

Legacy versions New versions Changes
None tkl_wifi_init A new function was added. See AP Pairing for the usage.
tuya_adapter_wifi_ap_start tkl_wifi_start_ap The function was renamed.
tuya_adapter_wifi_ap_stop tkl_wifi_stop_ap The function was renamed.
tuya_adapter_wifi_set_work_mode tkl_wifi_set_work_mode The function was renamed.
tuya_adapter_wifi_get_work_mode tkl_wifi_get_work_mode The function was renamed.
tuya_adapter_wifi_station_connect tkl_wifi_station_connect The function was renamed.
tuya_adapter_wifi_station_disconnect tkl_wifi_station_disconnect The function was renamed.
tuya_adapter_wifi_station_get_status tkl_wifi_station_get_status Enum values for DHCP failures were added.
tuya_adapter_wifi_get_ip tkl_wifi_get_ip The function was renamed.
tuya_adapter_wifi_sniffer_set None The EZ pairing mode was removed due to security risks.

Bluetooth pairing adaptation

Legacy versions New versions Changes
tuya_os_adapt_bt_port_init tkl_ble_stack_init The server and client roles were added as input parameters.
tuya_os_adapt_bt_send tkl_ble_gatts_value_notify New handles to connection and property were added.
tuya_os_adapt_bt_reset_adv tkl_ble_gap_adv_rsp_data_set The function was renamed.
tuya_os_adapt_bt_start_adv tkl_ble_gap_adv_start New input parameters were added. See the TKL_BLE_GAP_ADV_PARAMS_T struct.
tuya_os_adapt_bt_stop_adv tkl_ble_gap_adv_stop The function was renamed.
None tkl_ble_gap_callback_register A new function was added. See Bluetooth Pairing for the usage.
None tkl_ble_gatt_callback_register A new function was added. See Bluetooth Pairing for the usage.
None tkl_ble_gatts_service_add A new function was added. See Bluetooth Pairing for the usage.
None tkl_ble_gattc_exchange_mtu_request A new function was added. See Bluetooth Pairing for the usage.

APIs

System APIs

The system APIs in the TuyaOS SDK follow a standard Linux implementation and require no modifications. If your platform has specific requirements, adjust them accordingly.

Module Description
tkl_fs File system operations. You can create, open, read, and write files.
tkl_system OS functions. You can get the system time, set a delay, and restart the system.
tkl_memory Memory management. You can allocate and deallocate memory and perform a copy.
tkl_thread Thread management. You can create and destroy threads.
tkl_mutex Mutex management. You can create, lock, unlock, and destroy mutex.
tkl_semaphore Semaphore management. You can create, wait, send, and destroy semaphores.
tkl_uart Serial communication. You can initialize the UART and send and receive data.

Log output API

The TuyaOS SDK outputs logs to the terminal by default. You can implement the tkl_log_output in tkl_output to save the log to the specified location.

// Print the logs to the terminal.
VOID_T tkl_log_output(IN CONST CHAR_T *str, ...)
{
    printf("%s", str);
    fflush(stdout);
    return;
}

// Save the log to a file.
VOID_T tkl_log_output(IN CONST CHAR_T *str, ...)
{
    uFILE * fp = NULL;

    // Open file
    fp = ufopen("sweeper_log.txt", "w+");
    if(NULL == fp) {
    return ;
    }
    // Write to file
    uiWriteCnt = ufwrite(fp, str, strlen(str));
    if(uiWriteCnt != strlen(str)) {
    TAL_PR_ERR("sweeper_log uf file write data error!");
    return ;
    }
    // Close file
    ufclose(fp);
    return;
}

Adapt other TKL APIs as needed.

Reference

For more information about other TKL APIs, see Port TuyaOS to Linux Platforms.