TUYA BLE SDK User Guide
The tuya ble sdk encapsulates the communication protocol with Tuya Smart mobile App and implements event scheduling abilities. The device using tuya ble sdk does not need to care about the specific communication protocol implementation details. It can be interconnected with Tuya Smart App by calling the API and callback provided by the tuya ble sdk.
This topic gives details of the components, porting instruction, SDK configuration, API description, and usage of tuya ble sdk.
Project address
https://github.com/TuyaInc/tuya_ble_sdk.git
Overview of tuya ble sdk
Framework
The following figure shows the Application framework based on tuya ble sdk:
platform: the chip platform. The chip and protocol stack are maintained by the chip company.
Port: the abstract interfaces needed by the tuya ble sdk. You must implement them according to the chip-specific platform.
tuya ble sdk: encapsulates the communication protocol of Tuya BLE and provides the service interface to develop Tuya BLE devices.
Application: your application, built by using tuya ble sdk.
Tuya sdk API: implements BLE related management, communication, and so forth. The calls of API are based on asynchronous messages, and the result of API will be notified to the Application of device by message or call back.
sdk config: by setting the macro in the configuration file, you can configure tuya ble sdk to different modes, for example, the general network configuration mode applicable to multi-protocol devices, Bluetooth singlepoint devices mode, ECDH key based encryption method, whether to use OS, and so forth.
Main process function: the engine of tuya ble sdk, to which the Application will call all the time. If the platform architecture has an OS, The tuya ble sdk will automatically create a task to run the main process based on the OS related interface provided by the port layer. If the platform does not have an OS, the device Application needs to be called circularly.
Message or Call back: SDK sends the data of status, data, and others to device Application through call back function registered by device Application or messages.
OS compatibility
The tuya ble sdk can run on OS based chip platform besides Linux. If an OS is used, the API requests are based on asynchronous messages. When tuya ble sdk is initialized, the SDK automatically creates a task based on 'tuya_ble_config.h
file to process the message events of the SDK, and creates a message queue to receive the responses of the Application API. The results of the API are notified to the Application of the device in the form of message, so your Application needs to create a message queue and call tuya_ble_callback_queue_register()
after calling tuya_ble_sdk_init()
or tuya_ble_sdk_init_async()
to register the message queue to the SDK.
In the chip platform that has an OS, you can also configure the tuya ble sdk to process messages using the task provided by Application instead of tasks within the tuya ble sdk. By doing so, the Application must implement the outbound message interface at the port layer.
Event queue
The earlier event takes precedence to leave (FIFO). Event queue caches the messages sent by the Application and platform layer, the event can be API calls, data response from BLE devices, and so forth. The main process function module circularly queries the message queue and takes it out for processing.
Directories
The concepts of tuya ble service
The tuya ble sdk does not provide the interfaces for initializing service. Your Application needs to implement the service characteristics defined in the following table before you initializing the SDK. Other than the services required by the tuya ble sdk, you can also define other services if needed. The initial format of broadcast data must be implemented according to the following table, otherwise the tuya ble sdk cannot work.
The MTU
For a better compatibility, the ATT MTU used by tuya ble sdk is 23, and the GATT MTU (ATT DATA MAX) is 20.
Broadcast data format
The following picture illustrates the broadcast packet format of BLE.
The following table describes what are contained in the broadcast packet.
Example of 8 byte PID: 02 01 05 03 02 01 A2 0C 16 01 A2 00 00 00 00 00 00 00 00 00
The following table describes what are contained in the scan response data.
Example of an unassociated devices: 03 09 54 59 19 FF D0 07 00 0300 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
How to port and configure tuya ble sdk
As the following picture shows, the interfaces defined in the file tuya_ble_port.h
and tuya_ble_port_peripheral.h
must be ported and implemented according to the chip-specific platform. Note that if the platform used in the Application does not have an OS, the OS related interfaces do not need to be implemented. The tuya_ble_port.c
and tuya_ble_port_peripheral.c
are the weak implementation of the interfaces defined for the tuya_ble_port.h
and tuya_ble_port_peripheral.h
.
You cannot implement platform-specific interfaces in the preceding .c
files, please create a new one, for example tuya_ble_port_nrf52832.c
. If the file name contains the keyword tuya
, it is the platform implementation file that Tuya Smart has adapted and transplanted, you can refer to it if needed.
Reference of porting APIs
TUYA_BLE_LOG
TUYA_BLE_HEXDUMP
tuya_ble_gap_advertising_adv_data_update
tuya_ble_gap_advertising_scan_rsp_data_update
tuya_ble_gap_disconnect
tuya_ble_gatt_send_data
tuya_ble_timer_create
tuya_ble_timer_delete
tuya_ble_timer_start
tuya_ble_timer_restart
tuya_ble_timer_stop
tuya_ble_device_delay_ms
tuya_ble_device_delay_us
tuya_ble_device_reset
tuya_ble_gap_addr_get
The following sample illustrates how to use the tuya_ble_gap_addr_get
method.
tuya_ble_gap_addr_set
tuya_ble_device_enter_critical
tuya_ble_device_exit_critical
tuya_ble_rand_generator
tuya_ble_rtc_get_timestamp
tuya_ble_rtc_set_timestamp
tuya_ble_nv_init
tuya_ble_nv_erase
tuya_ble_nv_write
tuya_ble_nv_read
tuya_ble_nv_erase_async
tuya_ble_nv_write_async
tuya_ble_nv_read_async
tuya_ble_common_uart_init
tuya_ble_common_uart_send_data
tuya_ble_os_task_create
tuya_ble_os_task_delete
tuya_ble_os_task_suspend
tuya_ble_os_task_resume
tuya_ble_os_msg_queue_create
tuya_ble_os_msg_queue_delete
tuya_ble_os_msg_queue_peek
tuya_ble_os_msg_queue_send
tuya_ble_os_msg_queue_recv
tuya_ble_event_queue_send_port
tuya_ble_aes128_ecb_encrypt
tuya_ble_aes128_ecb_decrypt
tuya_ble_aes128_cbc_encrypt
tuya_ble_aes128_cbc_decrypt
tuya_ble_md5_crypt
tuya_ble_hmac_sha1_crypt
tuya_ble_hmac_sha256_crypt
tuya_ble_port_malloc
tuya_ble_port_free
Reference of configuration APIs
The items in the tuya_ble_config.h
file are used to configure the tuya ble sdk for different use cases, for example, network configuration for multi-protocol devices, whether running an OS on the platform or not, device communication, whether self-manage the authorization, and so forth.
CUSTOMIZED_TUYA_BLE_CONFIG_FILE
CUSTOMIZED_TUYA_BLE_APP_PRODUCT_TEST_HEADER_FILE
CUSTOMIZED_TUYA_BLE_APP_UART_COMMON_HEADER_FILE
TUYA_BLE_USE_OS
TUYA_BLE_SELF_BUILT_TASK
TUYA_BLE_TASK_PRIORITY
TUYA_BLE_TASK_STACK_SIZE
TUYA_BLE_DEVICE_COMMUNICATION_ABILITY
TUYA_BLE_DEVICE_SHARED
TUYA_BLE_DEVICE_UNBIND_MODE
TUYA_BLE_WIFI_DEVICE_REGISTER_MODE
TUYA_BLE_DEVICE_AUTH_SELF_MANAGEMENT
TUYA_BLE_SECURE_CONNECTION_TYPE
TUYA_BLE_DEVICE_MAC_UPDATE
TUYA_BLE_DEVICE_MAC_UPDATE_RESET
TUYA_BLE_USE_PLATFORM_MEMORY_HEAP
TUYA_BLE_GATT_SEND_DATA_QUEUE_SIZE
TUYA_BLE_DATA_MTU_MAX
TUYA_BLE_LOG_ENABLE
TUYA_BLE_LOG_COLORS_ENABLE
TUYA_BLE_LOG_LEVEL
TUYA_APP_LOG_ENABLE
TUYA_APP_LOG_COLORS_ENABLE
TUYA_APP_LOG_LEVEL
TUYA_BLE_ADVANCED_ENCRYPTION_DEVICE
TUYA_NV_ERASE_MIN_SIZE
TUYA_NV_WRITE_GRAN
TUYA_NV_START_ADDR
TUYA_NV_AREA_SIZE
TUYA_BLE_APP_VERSION_STRING
TUYA_BLE_APP_BUILD_FIRMNAME_STRING
API reference
The tuya ble sdk provides packaged API for Application to achieve BLE related management, communication, and so forth. The APIs are defined in the tuya_ble_api.c
and tuya_ble_api.h
files, you can read the source code to understand how the APIs are implemented. The following tables describes the details of each API.
tuya_ble_main_tasks_exec
Example: the calling location under nrf52832 platform is as follows:
tuya_ble_gatt_receive_data
Example (nrf52832 example demo):
tuya_ble_common_uart_receive_data
tuya_ble_common_uart_send_full_instruction_received
Explanation:
The tuya ble sdk integrates the production testing authorization function module. The production testing authorization module communicates through the UART and PC production testing authorization tools. UART communication has a complete set of command formats. For details, refer to the Bluetooth General Production Test Authorization Agreement.
The tuya ble sdk includes the UART communication instruction parsing function. The Application only needs to call the
tuya_ble_common_uart_receive_data()
function where UART data is received. The Application can also parse out the complete UART communication instruction, and then calltuya_ble_common_uart_send_full_instruction_received()
function to send a complete command to tuya ble sdk.
tuya_ble_device_update_product_id
tuya_ble_device_update_login_key
tuya_ble_device_update_bound_state
tuya_ble_device_update_mcu_version
tuya_ble_sdk_init
Parameter explanation:
The structure of tuya_ble_device_param_t
is as follows:
The following items describe the variables in the preceding snippet.
device_id
andauth_key
: thedevice_uuid
and theauth_key
are unique identifier pairs assigned to the BLE devices by the Tuya IoT platform. They are required when you perform the production testing authorization via Tuya testing tool. After a BLE device is authorized by production testing, the tuya ble sdk takes control of and manages them automatically. Therefore, you can set thedevice_uuid
ad theauth_key
to0
when the tuya ble sdk is initialized. If they are not0
, the tuya ble sdk uses the pre-defineddevice_id
andauth_key
. For a Wi-Fi/BLE dual-mode device, itsdevice_id
andauth_key
are managed by the Wi-Fi layer, therefore, the actualdevice_id
andauth_key
are needed when the tuya ble sdk is initialized.product_id
: pid for short. It is automatically generated when a new product is created on the IoT platform. In your Application, the PID needs to be saved in the form of a constant. It is required when initializing the tuya ble sdk.device_vid
: the virtual ID of a BLE device, which is generated by Tuya IoT platform after the binding of the notice volume. It can be used to find the data record of the cloud to the BLE device when the BLE device is added or removed. For BLE devices that need the authorization information of the tuya ble sdk management, the value is0
. For Wi-Fi/BLE dual mode devices, or BLE devices of which the authorization is managed by your Application, the actualdevice_vid
is needed.login_key
andbound_flag
: if the authorization of BLE devices are managed by the tuya ble sdk, set the values to0
. For Wi-Fi/BLE dual mode devices, or BLE devices of which the authorization is managed by your Application, the actual values are needed.firmware_version
andhardware_version
: indicates the firmware version number and the hardware (PCBA) version number of BLE devices. They are required when initializing the tuya ble sdk. Currently, BLE devices only support two digit version numbers, for example 0x0100 represents v1.0.
The following snippet is the initialization example of tuya ble sdk for nrf52832 platform.
tuya_ble_sdk_init_async
Example of tuya ble sdk asynchronous initialization:
tuya_ble_dp_data_report
parameter description:
Tuya IoT platform manages data in the form of dp points. The data generated by any device needs to be abstracted in the form of dp points. A complete dp point data consists of four parts (refer to the relevant introduction on the IoT platform for details):
Dp_id: 1 byte, dp_id serial number registered in the development platform.
Dp_type: 1 byte, dp point type.
#define DT_RAW 0 //raw type
#define DT_BOOL 1 //Boolean type
#define DT_VALUE 2 //Value type
#define DT_STRING 3 //String type
#define DT_ENUM 4 //Enumeration type
#define DT_BITMAP 5 // Bitmap type
Dp_len: Bluetooth currently supports only one byte, that is, the longest data of a single dp point is 255 bytes.
Dp_data: Data, dp_len bytes.
The dp point reporting function, the data pointed to by parameter p_data must be assembled and reported in the form of a table:
When calling this function, the maximum length of parameter len is
TUYA_BLE_REPORT_MAX_DP_DATA_LEN
(currently 255+3).
tuya_ble_dp_data_with_time_report
tuya_ble_dp_data_with_time_ms_string_report
tuya_ble_dp_data_with_flag_report
parameter description:
mode:
REPORT_FOR_CLOUD_PANEL
: report to the panel and the cloud at the same time.
REPORT_FOR_CLOUD
: only report to the cloud.
REPORT_FOR_PANEL
: only report to the panel.
REPORT_FOR_NONE
: neither reported to the panel nor to the cloud.
tuya_ble_dp_data_with_flag_and_time_report
tuya_ble_dp_data_with_flag_and_time_ms_string_report
tuya_ble_connected_handler
tuya_ble_disconnected_handler
Example of calling tuya_ble_connected_handler and tuya_ble_disconnected_handler on nrf52832 platform:
tuya_ble_adv_data_connecting_request_set
tuya_ble_data_passthrough
tuya_ble_production_test_asynchronous_response
tuya_ble_net_config_response
tuya_ble_ubound_response
tuya_ble_anomaly_ubound_response
tuya_ble_device_reset_response
tuya_ble_connect_status_get
Responses description:
tuya_ble_device_factory_reset
tuya_ble_time_req
tuya_ble_ota_response
tuya_ble_custom_event_send
Description: This function is mainly used when the Application wants to use the internal message scheduler of tuya ble sdk to process the Application's message events.
tuya_ble_custom_event_send
Application examples:
tuya_ble_callback_queue_register
Application examples for OS platform:
Application example without OS platform:
tuya_ble_event_response
Application examples:
tuya_ble_scheduler_queue_size_get
tuya_ble_scheduler_queue_space_get
tuya_ble_scheduler_queue_events_get
The callback event of tuya ble sdk
The tuya ble sdk sends message of status, data, and other information to Application through message (for OS architecture platform) or callback function registered by Application (for non-OS architecture platform). According to the previous section, the example of tuya_ble_event_response
describes how the Application in the OS architecture platform handle the messages sent by tuya ble sdk. The chip platform without an OS can use the similar code to process messages, however it is not necessary to call the tuya_ble_event_response()
to respond to tuya ble sdk after the messages is handled by the callback function. As the following snippet shows an example for callback registration in the platform without an OS.
TUYA_BLE_CB_EVT_CONNECTE_STATUS
Corresponding data structure:
TUYA_BLE_CB_EVT_DP_WRITE
Corresponding data structure:
TUYA_BLE_CB_EVT_DP_QUERY
Corresponding data structure:
TUYA_BLE_CB_EVT_OTA_DATA
Corresponding data structure:
TUYA_BLE_CB_EVT_NETWORK_INFO
Corresponding data structure:
TUYA_BLE_CB_EVT_WIFI_SSID
Corresponding data structure:
TUYA_BLE_CB_EVT_TIME_STAMP
Corresponding data structure:
TUYA_BLE_CB_EVT_TIME_NORMAL
Corresponding data structure:
TUYA_BLE_CB_EVT_DATA_PASSTHROUGH
Corresponding data structure:
TUYA_BLE_CB_EVT_DP_DATA_REPORT_RESPONSE
Corresponding data structure:
TUYA_BLE_CB_EVT_DP_DATA_WTTH_TIME_REPORT_RESPONSE
Corresponding data structure:
TUYA_BLE_CB_EVT_DP_DATA_WITH_FLAG_REPORT_RESPONSE
Corresponding data structure:
TUYA_BLE_CB_EVT_DP_DATA_WITH_FLAG_AND_TIME_REPORT_RESPONSE
Corresponding data structure:
TUYA_BLE_CB_EVT_UNBOUND
Corresponding data structure:
TUYA_BLE_CB_EVT_ANOMALY_UNBOUND
Corresponding data structure:
TUYA_BLE_CB_EVT_DEVICE_RESET
Corresponding data structure:
TUYA_BLE_CB_EVT_UPDATE_LOGIN_KEY_VID
Corresponding data structure: