Porting Guide

Last Updated on : 2023-11-23 02:22:00download

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 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 describes how to adapt the TKL API to run the TuyaOS development framework properly on specific hardware platforms.

TKL API overview

The TKL API abstracts away the complexities and differences of hardware and systems. The development environment includes definitions and example code for the TKL API, which are regularly released and updated to align with its own updates.

  • TKL API definition: software/TuyaOS/vendor/<development environment>/tuyaos/tuyaos_adapter/include.
  • TKL API examples: software/TuyaOS/vendor/<development environment>/tuyaos/tuyaos_adapter/src.

When using the TuyaOS SDK mode, you need to adapt the TKL API. However, this adaptation is not required for TuyaOS OS mode as the API has already been adapted.

TKL API description

System API:

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.

Service API:

Module Description
tkl_wired Auto discovery pairing as well as QR code pairing for 4G devices. You can get the IP address and network connection status.
tkl_wifi Wi-Fi AP pairing. You can start the access point (AP), connect to the router, and get the IP address and network connection status.
tkl_bluetooth Bluetooth pairing. You can initialize the Bluetooth stack, register GAP and GATT callbacks, and set the advertising data and scan response data.
tkl_output Log output.

TKL API adaptation

tkl_wired

This API is used for auto discovery pairing as well as QR code pairing for 4G devices. It applies to the pairing scenarios where an IP address can be assigned without depending on TuyaOS, regardless of pairing over Ethernet, 4G, or screen.

You need to specify the communication between the network interface, Tuya-enabled mobile app, and Tuya IoT cloud, and detect the NIC connection. The method for detecting the NIC connection varies depending on the NIC used on the product. The example code is for reference only. You need to adapt the function to your specific hardware.

For more information, see Pair Over Wired Connection.

tkl_wifi

This API is used for Wi-Fi AP pairing. The device communicates with the Tuya-enabled mobile app through TuyaOS (over Wi-Fi) to get the router’s Wi-Fi credentials, and then connect to the router to access the internet.

For more information, see Pair Over Wireless Connection.

tkl_bluetooth

This API is used for Bluetooth pairing. The device communicates with the Tuya-enabled mobile app through TuyaOS (over Bluetooth) to get the router’s Wi-Fi credentials, and then connect to the router to access the internet.

For more information, see Pair Over Bluetooth.

tkl_output

This API is used for log output. TuyaOS allows you to set the logging level and logging destination, such as serial port, file, terminal, and network. TuyaOS takes care of formatting the log. You only need to output the log to the appropriate media.

Interface Description
VOID_T tkl_log_output(CONST CHAR_T *format, …) Log output.

tkl_fs

This API is used for the file system. The API for Linux complies with the POSIX standard, allowing you to use the example code directly without adaptation.

Interface Description
INT_T tkl_fs_mkdir(CONST CHAR_T *path) Create a directory.
INT_T tkl_fs_remove(CONST CHAR_T *path) Delete a directory.
INT_T tkl_fs_mode(CONST CHAR_T *path, UINT_T *mode) Change directory permissions.
INT_T tkl_fs_is_exist(CONST CHAR_T *path, BOOL_T *is_exist) Check whether the file or directory exists.
INT_T tkl_fs_rename(CONST CHAR_T *path_old, CONST CHAR_T *path_new) Rename the file or directory.
INT_T tkl_dir_open(CONST CHAR_T *path, TUYA_DIR *dir) Open a directory.
INT_T tkl_dir_close(TUYA_DIR dir) Close a directory.
INT_T tkl_dir_read(TUYA_DIR dir, TUYA_FILEINFO *info) Read a directory.
INT_T tkl_dir_name(TUYA_FILEINFO info, CONST CHAR_T **name) Read the name of a directory.
INT_T tkl_dir_is_directory(TUYA_FILEINFO info, BOOL_T *is_dir) Check whether this is a directory.
INT_T tkl_dir_is_regular(TUYA_FILEINFO info, BOOL_T *is_regular) Check whether this is a file.
TUYA_FILE tkl_fopen(CONST CHAR_T *path, CONST CHAR_T *mode) Open a file.
INT_T tkl_fclose(TUYA_FILE file) Close a file.
INT_T tkl_fread(VOID_T *buf, INT_T bytes, TUYA_FILE file) Read a file.
INT_T tkl_fwrite(VOID_T *buf, INT_T bytes, TUYA_FILE file) Write a file.
INT_T tkl_fsync(INT_T fd) Sync files to disk.
CHAR_T *tkl_fgets(CHAR_T *buf, INT_T len, TUYA_FILE file) Read a file.
INT_T tkl_feof(TUYA_FILE file) Check whether the end of a file has been reached.
INT_T tkl_fseek(TUYA_FILE file, INT64_T offs, INT_T whence) Set the file pointer to the specified offset.
INT64_T tkl_ftell(TUYA_FILE file) Get the current position of the file pointer.
INT_T tkl_fgetsize(CONST CHAR_T *filepath) Get the size of the file.
INT_T tkl_fgetc(TUYA_FILE file) Read the next character.
INT_T tkl_fflush(TUYA_FILE file) Refresh the I/O buffer.
INT_T tkl_fileno(TUYA_FILE file) Read the file descriptor.
INT_T tkl_ftruncate(INT_T fd, UINT64_T length) Truncate a file.

tkl_system

This is the OS API, allowing you to use the example code directly without adaptation.

Interface Description
SYS_TICK_T tkl_system_get_tick_count(VOID_T) Get the system uptime.
SYS_TIME_T tkl_system_get_millisecond(VOID_T) Get the system uptime in milliseconds.
VOID_T tkl_system_sleep(CONST UINT_T num_ms) Sleep
VOID_T tkl_system_reset(VOID_T) Restart
INT_T tkl_system_get_free_heap_size(VOID_T) Get the free memory.
TUYA_RESET_REASON_E tkl_system_get_reset_reason(CHAR_T **describe) Get the reason for a restart.
INT_T tkl_system_get_random(CONST UINT_T range) Generate a random number.
UINT_T tkl_system_enter_critical(VOID_T) Enter the critical section.
VOID_T tkl_system_exit_critical(UINT_T irq_mask) Exit the critical section.
VOID_T tkl_system_delay(UINT_T num_ms) Delay
OPERATE_RET tkl_system_get_cpu_info(TUYA_CPU_INFO_T **cpu_ary, INT_T *cpu_cnt) Get the CPU information.

tkl_memory

This is the memory management API, allowing you to use the example code directly without adaptation.

Interface Description
VOID_T *tkl_system_malloc(CONST SIZE_T size) Allocate the memory.
VOID_T tkl_system_free(VOID_T *ptr) Free the memory.
VOID_T *tkl_system_memset(VOID_T *src, INT_T ch, CONST SIZE_T n) Clear data.
VOID_T *tkl_system_memcpy(VOID_T *src, CONST VOID_T *dst, CONST SIZE_T n) Copy a block of memory from one location to another.
VOID_T *tkl_system_calloc(size_t nitems, size_t size) Allocate a block of memory and initialize all its bits to zero.
VOID_T *tkl_system_realloc(VOID_T *ptr, size_t size) Reallocate the memory.

tkl_thread

This is the thread management API, allowing you to use the example code directly without adaptation.

Interface 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(CONST TKL_THREAD_HANDLE thread) Destroy and terminate a thread.
OPERATE_RET tkl_thread_get_id(TKL_THREAD_HANDLE *thread) Get the thread handle.
OPERATE_RET tkl_thread_set_self_name(CONST CHAR_T* name) Get the thread name.

tkl_mutex

This is the mutex management API, allowing you to use the example code directly without adaptation.

Interface Description
OPERATE_RET tkl_mutex_create_init(TKL_MUTEX_HANDLE *handle) Create a mutex.
OPERATE_RET tkl_mutex_lock(CONST TKL_MUTEX_HANDLE handle) Lock the mutex.
OPERATE_RET tkl_mutex_trylock(CONST TKL_MUTEX_HANDLE handle) Attempt to lock the mutex.
OPERATE_RET tkl_mutex_unlock(CONST TKL_MUTEX_HANDLE handle) Unlock the mutex.
OPERATE_RET tkl_mutex_release(CONST TKL_MUTEX_HANDLE handle) Release the mutex.

tkl_semaphore

This is the semaphore management API, allowing you to use the example code directly without adaptation.

Interface Description
OPERATE_RET tkl_semaphore_create_init(TKL_SEM_HANDLE *handle, CONST UINT_T sem_cnt, CONST UINT_T sem_max) Create a semaphore.
OPERATE_RET tkl_semaphore_wait(CONST TKL_SEM_HANDLE handle, CONST UINT_T timeout) Wait for a semaphore.
OPERATE_RET tkl_semaphore_post(CONST TKL_SEM_HANDLE handle) Send a semaphore.
OPERATE_RET tkl_semaphore_release(CONST TKL_SEM_HANDLE handle) Release a semaphore.

tkl_uart

This is the serial communication API, allowing you to use the example code directly unless you have specific requirements.

The example code uses the names /dev/ttyS* and /dev/ttyUSB* for the serial devices. If your serial devices are not of this type, change the name accordingly.

Interface Description
OPERATE_RET tkl_uart_init(UINT_T port_id, TUYA_UART_BASE_CFG_T *cfg) Initialize the serial port.
OPERATE_RET tkl_uart_deinit(UINT_T port_id) Deinitialize the serial port.
INT_T tkl_uart_write(UINT_T port_id, VOID_T *buff, UINT16_T len) Write data.
INT_T tkl_uart_read(UINT_T port_id, VOID_T *buff, UINT16_T len) Read data.