BLE SDK 说明

更新时间:2022-11-24 09:20:04下载pdf

本文档于 2021 年 8 月 24 日停止更新。 如果您想查看更新的内容,请参考 《TuyaOS》蓝牙设备接入

BLE SDK 主要封装了和涂鸦智能手机 app 之间的通信协议以及实现了简易事件调度机制,使用 tuya ble sdk 的设备无须关心具体的通信协议实现细节,通过调用 tuya ble sdk 提供的 API 和 Call Back 即可与涂鸦智能手机 app 互联互通。

本文主要描述了 tuya ble sdk 的架构、移植说明、配置说明、API 说明以及使用说明等内容。

BLE SDK 架构

系统架构

下图所示为基于 tuya ble sdk 的应用框架:

  • platform:所使用的芯片平台,芯片 + 协议栈由芯片公司维护。

  • Port:tuya ble sdk 所需要的抽象接口,需要用户根据具体的芯片平台移植实现。

  • Tuya ble sdk : 封装了涂鸦 BLE 通信协议,提供构建涂鸦 BLE 应用所需的服务接口。

  • Application:基于tuya ble sdk 构建的应用。

  • Tuya sdk API:API用于设备实现BLE相关的管理、通信等,API的调用将采用基于消息的异步机制,API的执行结果将会以message或者call back的方式通知给设备的Application。

  • sdk config:tuya ble sdk 可裁剪可配置,通过配置文件中的宏定义可将tuya ble sdk设置成不同模式,例如配置成 适用于多协议设备的通用配网模式,蓝牙单点设备、基于 ECDH 密钥协商加密模式、是否使用 OS 等。

  • Main process function:为tuya ble sdk的主引擎,Application 需要一直调用,如果platform架构是带OS的,tuya ble sdk会基于port层提供的OS相关接口自动创建一个任务用于执行Main process function,如果是非OS平台,需要设备Application循环调用。

  • Message or Call Back:sdk 通过 Message 或者设备 Application注册的Call Back函数向设备 Application 发送数据(状态、数据等)。

BLE SDK 说明

OS支持

tuya ble sdk 可运行在基于OS的芯片平台下(linux暂不支持)。如果使用OS,api 的调用将采用基于消息的异步机制,初始化tuya ble sdk 时,sdk 将会根据 tuya_ble_config.h文件的相关配置自动创建一个任务用于处理sdk 的消息事件,同时自动创建一个消息队列用于接收应用 api 的执行请求,api 的执行结果也将会以message的方式通知给设备的Application,所以用户Application需要创建一个消息队列并在调用tuya_ble_sdk_init()或者tuya_ble_sdk_init_async()后调用tuya_ble_callback_queue_register()将消息队列注册至sdk中。

在OS的芯片平台架构中,也可以通过配置实现tuya ble sdk不自建task而是使用Application提供的task处理消息,在这种配置下Application需要在port层实现消息发送接口。

事件队列

先进先出 (FIFO),用于缓存 Application 和 Platform 层发送的消息事件(API 调用、BLE 底层数据接收等动作),Main process function 模块会循环查询消息队列并将其取出处理。

BLE SDK 目录说明

Directory Description
app 存放tuya ble sdk 需要管理的一些应用,例如涂鸦产测应用、通用对接应用等
doc 说明文档
extern_components 一些外部组件,例如安全相关算法实现
port 需要 Application 移植实现的抽象接口
sdk tuya ble sdk 核心代码
tuya_ble_config.h 配置文件,Application 需要新建一个自定义配置文件来实现所需的功能
tuya_ble_sdk_version.h 版本头文件
README.md 说明文件
tuya_ble_sdk_version.txt 版本迭代中文说明文件
CHANGELOG.md 版本迭代英文说明文件

BLE service 说明

概述

tuya ble sdk 不提供初始化 service 相关接口,Application 需要在初始化 sdk 前实现 下表所定义的 service characteristics,当然,Application除了定义tuya ble sdk 所需的 service 外,也可以定义其他 service。广播数据的初始格式按照下表要求实现,否则tuya ble sdk 将不能正常工作。

Service Characteristics

Service UUID Characteristic UUID Properties Security Permissions
1910 2b10 Notify None.
2b11 Write,write without response. None.

MTU

基于更好的兼容性考虑,目前 tuya ble sdk 使用的ATT MTU 为23,GATT MTU (ATT DATA MAX)为 20。

广播数据格式

BLE 广播包数据结构

BLE SDK 说明

广播包数据内容

广播数据段描述 类型 说明
设备 LE 物理连接标识 0x01 长度:0x02 类型:0x01 数据:0x06
Service UUID 0x02 长度:0x03 类型:0x02 数据:0xA201
Service Data 0x16 长度:0x0C 或者 0x14
类型:0x16
数据:0x01, 0xA2, type (0-pid,1-product_key)PID或者product_key (8 或者 16 字节)

内容示例(8字节PID): 02 01 05 03 02 01 A2 0C 16 01 A2 00 00 00 00 00 00 00 00 00

扫描响应包数据内容

扫描响应包数据段描述 类型 说明
Complete Local Name 0x09 长度:0x03 类型:0x09 数据:0x54,0x59
厂商自定义数据 0xff 长度:0x19
类型:0xff
数据 :COMPANY ID:0x07D0
FLAG:0x00
协议版本:0x03
加密方式:0x00
通信能力:0x0000
保留字段:0x00
ID 字段: 16 个字节或 6 字节

内容示例(未绑定):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

移植和配置说明

移植说明

如下图所示,tuya_ble_port.htuya_ble_port_peripheral.h里定义的所有接口都需要应用根据具体的芯片平台移植实现,如果应用所用芯片平台是非OS架构的,OS相关接口不需要实现。tuya_ble_port.ctuya_ble_port_peripheral.c 这两个文件里弱实现了 tuya_ble_port.htuya_ble_port_peripheral.h 所定义的接口,用户不能在该文件里实现具体的平台接口,应该新建一个c文件,例如新建一个tuya_ble_port_nrf52832.c文件。
注:SDK内以平台名字命名的文件里是涂鸦已经适配移植好的平台实现文件,用户可以参考使用。

BLE SDK 说明

port 接口介绍

TUYA_BLE_LOG

函数名 TUYA_BLE_LOG
函数原型 void TUYA_BLE_LOG(const char *format,…)
功能概述 格式化输出
参数 format[in] : 格式控制符;
…[in] :可变参数 ;
返回值
备注

TUYA_BLE_HEXDUMP

函数名 TUYA_BLE_HEXDUMP
函数原型 void TUYA_BLE_HEXDUMP(uint8_t *p_data , uint16_t len)
功能概述 16进制打印
参数 p_data[in]: 要打印的数据指针;
len[in] :数据长度 ;
返回值
备注

tuya_ble_gap_advertising_adv_data_update

函数名 tuya_ble_gap_advertising_adv_data_update
函数原型 tuya_ble_status_t tuya_ble_gap_advertising_adv_data_update(uint8_t const * p_ad_data, uint8_t ad_len)
功能概述 BLE 广播包数据更新
参数 p_ad_data[in]:新的广播数据;
ad_len[in]:数据长度 ;
返回值 TUYA_BLE_SUCCESS:成功;
其他:失败。
备注

tuya_ble_gap_advertising_scan_rsp_data_update

函数名 tuya_ble_gap_advertising_scan_rsp_data_update
函数原型 tuya_ble_status_t tuya_ble_gap_advertising_scan_rsp_data_update(uint8_t const *p_sr_data, uint8_t sr_len)
功能概述 BLE 扫描响应包数据更新
参数 p_sr_data[in]:新的扫描响应包数据;
sr_len[in] :数据长度 ;
返回值 TUYA_BLE_SUCCESS:成功;
其他:失败。
备注

tuya_ble_gap_disconnect

函数名 tuya_ble_gap_disconnect
函数原型 tuya_ble_status_t tuya_ble_gap_disconnect(void)
功能概述 断开 BLE 连接
参数
返回值 TUYA_BLE_SUCCESS: 成功;
其他:失败。
备注

tuya_ble_gatt_send_data

函数名 tuya_ble_gatt_send_data
函数原型 tuya_ble_status_t tuya_ble_gatt_send_data(const uint8_t *p_data,uint16_t len)
功能概述 ble gatt 发送数据(notify)
参数 p_data[in] : 要发送的数据指针;
len[in] : 发送的数据长度(目前不能超过20字节)。
返回值 TUYA_BLE_SUCCESS: 成功;其他:失败。
备注 必须是 notify 方式发送。

tuya_ble_timer_create

函数名 tuya_ble_timer_create
函数原型 tuya_ble_status_t tuya_ble_timer_create(void** p_timer_id,uint32_t timeout_value_ms,
tuya_ble_timer_mode mode,tuya_ble_timer_handler_t timeout_handler)
功能概述 创建一个定时器
参数 p_timer_id[out] :创建的定时器指针;
timeout_value_ms[in]:定时时间,单位 ms;
mode[in] :
TUYA_BLE_TIMER_SINGLE_SHOT - 单一模式,
TUYA_BLE_TIMER_REPEATED - 重复模式。
timeout_handler [in]: 定时器回调函数。
返回值 TUYA_BLE_SUCCESS: 成功;其他:失败。
备注

tuya_ble_timer_delete

函数名 tuya_ble_timer_delete
函数原型 tuya_ble_status_t tuya_ble_timer_delete(void* timer_id)
功能概述 删除一个定时器
参数 timer_id [in] : 定时器id;
返回值 TUYA_BLE_SUCCESS: 成功;
TUYA_BLE_ERR_INVALID_PARAM: 无效的参数;
其他:失败。
备注

tuya_ble_timer_start

函数名 tuya_ble_timer_start
函数原型 tuya_ble_status_t tuya_ble_timer_start(void* timer_id)
功能概述 启动一个定时器
参数 timer_id [in] :定时器id;
返回值 TUYA_BLE_SUCCESS: 成功;
TUYA_BLE_ERR_INVALID_PARAM: 无效的参数;
其他:失败。
备注 如果定时器已经启动,执行该函数后会重新启动。

tuya_ble_timer_restart

函数名 tuya_ble_timer_restart
函数原型 tuya_ble_status_t tuya_ble_timer_restart(void* timer_id,uint32_t timeout_value_ms)
功能概述 以新的定时时间重新启动一个定时器
参数 timer_id [in] :定时器id;
timeout_value_ms[in]:定时时间,单位ms。
返回值 TUYA_BLE_SUCCESS: 成功;
TUYA_BLE_ERR_INVALID_PARAM: 无效的参数;
其他:失败。
备注

tuya_ble_timer_stop

函数名 tuya_ble_timer_stop
函数原型 tuya_ble_status_t tuya_ble_timer_stop(void* timer_id)
功能概述 停止一个定时器
参数 timer_id [in] :定时器id;
返回值 TUYA_BLE_SUCCESS: 成功;
TUYA_BLE_ERR_INVALID_PARAM: 无效的参数;
其他:失败。
备注

tuya_ble_device_delay_ms

函数名 tuya_ble_device_delay_ms
函数原型 void tuya_ble_device_delay_ms(uint32_t ms)
功能概述 ms 级延时函数
参数 ms [in] : 延时毫秒数;
返回值
备注 如果是在 os 平台下,必须为无阻塞延时,仅仅应用在特殊场景下,
例如初始化sdk、重启之前等。

tuya_ble_device_delay_us

函数名 tuya_ble_device_delay_us
函数原型 void tuya_ble_device_delay_us(uint32_t us)
功能概述 us 级延时函数
参数 us [in] : 延时微妙数;
返回值
备注 如果是在 os 平台下,必须为无阻塞延时,仅仅应用在特殊场景下,
例如初始化sdk、重启之前等。

tuya_ble_device_reset

函数名 tuya_ble_device_reset
函数原型 tuya_ble_status_t tuya_ble_device_reset(void)
功能概述 设备重启
参数
返回值 TUYA_BLE_SUCCESS: 成功;
其他:失败。
备注

tuya_ble_gap_addr_get

函数名 tuya_ble_gap_addr_get
函数原型 tuya_ble_status_t tuya_ble_gap_addr_get(tuya_ble_gap_addr_t *p_addr);
功能概述 获取设备 mac 地址
参数 p_addr [out] : mac 地址指针。
返回值 TUYA_BLE_SUCCESS: 成功;
其他:失败。
备注
typedef enum

{

TUYA_BLE_ADDRESS_TYPE_PUBLIC, // public address

TUYA_BLE_ADDRESS_TYPE_RANDOM, // random address

} tuya_ble_addr_type_t;

typedef struct

{

tuya_ble_addr_type_t addr_type;

uint8_t addr[6];

}tuya_ble_gap_addr_t;

tuya_ble_gap_addr_set

函数名 tuya_ble_gap_addr_set
函数原型 tuya_ble_status_t tuya_ble_gap_addr_set(tuya_ble_gap_addr_t *p_addr);
功能概述 设置更新设备 mac 地址
参数 p_addr [in] : mac 地址指针。
返回值 TUYA_BLE_SUCCESS: 成功;
其他:失败。
备注

tuya_ble_device_enter_critical

函数名 tuya_ble_device_enter_critical
函数原型 voidtuya_ble_device_enter_critical(void)
功能概述 进入临界区
参数
返回值
备注

tuya_ble_device_exit_critical

函数名 tuya_ble_device_exit_critical
函数原型 void tuya_ble_device_exit_critical(void)
功能概述 退出临界区
参数
返回值
备注

tuya_ble_rand_generator

函数名 tuya_ble_rand_generator
函数原型 tuya_ble_status_t tuya_ble_rand_generator(uint8_t* p_buf, uint8_t len)
功能概述 随机数生成
参数 p_buf [out]: 生成的随机数的数组指针;
len[in] : 随机数字节数。
返回值 TUYA_BLE_SUCCESS: 成功;
其他:失败。
备注

tuya_ble_rtc_get_timestamp

函数名 tuya_ble_rtc_get_timestamp
函数原型 tuya_ble_status_t tuya_ble_rtc_get_timestamp(uint32_t *timestamp,int32_t *timezone);
功能概述 获取 unix 时间戳
参数 timestamp [out]: 时间戳;
timezone [out] : 时区(有符号型整数,真实时区的100倍)。
返回值 TUYA_BLE_SUCCESS: 成功;
其他:失败。
备注 数据来自应用本身维护的 RTC 实时时钟,如果应用没有RTC,无需实现该接口。

tuya_ble_rtc_set_timestamp

函数名 tuya_ble_rtc_set_timestamp
函数原型 tuya_ble_status_t tuya_ble_rtc_set_timestamp(uint32_t timestamp,int32_t timezone)
功能概述 更新 unix 时间戳
参数 timestamp [in]:unix 时间戳;
timezone [in] : 时区(有符号型整数,真实时区的100倍)。
返回值 TUYA_BLE_SUCCESS: 成功;
其他:失败。
备注 SDK 通过调用此函数更新应用程序维护的 RTC 实时时钟,如果应用没有RTC,无需实现该接口。

tuya_ble_nv_init

函数名 tuya_ble_nv_init
函数原型 tuya_ble_status_t tuya_ble_nv_init(void)
功能概述 NV 初始化。
参数
返回值 TUYA_BLE_SUCCESS: 成功;
其他:失败。
备注 配合 配置文件定义的 NV 空间地址使用,sdk调用 NV相关函数来存储和管理授权信息和其他信息。

tuya_ble_nv_erase

函数名 tuya_ble_nv_erase
函数原型 tuya_ble_status_t tuya_ble_nv_erase(uint32_t addr,uint32_t size)
功能概述 NV 擦除函数。
参数 addr[in] : 要擦除 NV 区域的起始地址;
size[in] : 要擦除的大小(单位:字节)。
返回值 TUYA_BLE_SUCCESS: 成功;
其他:失败。
备注 根据 配置文件 里定义的 NV 空间地址来使用,sdk 调用 NV相关函数来存储、管理授权信息和其他信息。

tuya_ble_nv_write

函数名 tuya_ble_nv_write
函数原型 tuya_ble_status_t tuya_ble_nv_write(uint32_t addr,const uint8_t * p_data, uint32_t size)
功能概述 NV写数据函数。
参数 addr[in] : 指向被写入数据的起始地址;
p_data[in] : 写入的起始地址。
size[in] : 要写入数据的大小(单位:字节)。
返回值 TUYA_BLE_SUCCESS: 成功;
其他:失败。
备注 根据 配置文件 里定义的 NV 空间地址来使用,sdk 调用 NV相关函数来存储、管理授权信息和其他信息。

tuya_ble_nv_read

函数名 tuya_ble_nv_read
函数原型 tuya_ble_status_t tuya_ble_nv_read(uint32_t addr,uint8_t * p_data, uint32_t size)
功能概述 NV读数据函数。
参数 addr[in] : 要读取数据的NV起始地址;
p_data[out] : 读取数据的地址;
size[in] : 要读取数据的大小(单位:字节)。
返回值 TUYA_BLE_SUCCESS: 成功;
其他:失败。
备注 配合 配置文件定义的 NV 空间地址使用,sdk 调用 NV相关函数来存储和管理授权信息和其他信息。

tuya_ble_nv_erase_async

函数名 tuya_ble_nv_erase_async
函数原型 void tuya_ble_nv_erase_async(uint32_t addr,uint32_t size,void *p_context,tuya_ble_nv_async_callback_t callback)
功能概述 NV 异步擦除函数。
参数 addr[in] : 要擦除NV区域的起始地址;
size[in] : 要擦除的大小(字节);
p_context[in] : 用户自定义数据;
callback[in] : 回调函数,执行结果通过该回调函数返回。
返回值 无。
备注 配合配置文件定义的NV空间地址使用,sdk调用NV相关函数来存储
和管理授权信息和其他信息,该接口在基于异步flash操作的平台下使用。

tuya_ble_nv_write_async

函数名 tuya_ble_nv_write_async
函数原型 void tuya_ble_nv_write_async(uint32_t addr,const uint8_t * p_src, uint32_t size,void *p_context,tuya_ble_nv_async_callback_t callback)
功能概述 NV异步写数据函数。
参数 addr[in] : 指向被写入数据的起始地址;
p_src[in] : 写入数据源的起始地址。
size[in] : 要写入数据的字节大小;
p_context[in] : 用户自定义数据;
callback[in] : 回调函数,执行结果通过该回调函数返回。
返回值 无。
备注 配合配置文件定义的NV空间地址使用,sdk 调用NV相关函数来存储
和管理授权信息和其他信息,该接口在基于异步flash操作的平台下使用。

tuya_ble_nv_read_async

函数名 tuya_ble_nv_read_async
函数原型 void tuya_ble_nv_read_async(uint32_t addr, uint8_t * p_dest, uint32_t size,void *p_context,tuya_ble_nv_async_callback_t callback)
功能概述 NV异步读数据函数。
参数 addr[in] : 要读取数据的NV起始地址;
p_dest[out] : 缓冲读取数据的地址;
size[in] : 要读取数据的大小(单位:字节);
p_context[in] : 用户自定义数据;
callback[in] : 回调函数,执行结果通过该回调函数返回。
返回值 无。
备注 配合配置文件定义的NV空间地址使用,sdk 调用NV相关函数来存储
和管理授权信息和其他信息,该接口在基于异步flash操作的平台下使用。

tuya_ble_common_uart_init

函数名 tuya_ble_common_uart_init
函数原型 tuya_ble_status_t tuya_ble_common_uart_init(void)
功能概述 uart初始化函数。
参数
返回值 TUYA_BLE_SUCCESS: 成功;
其他:失败。
备注 如果应用程序不使用tuya ble sdk提供的产测授权功能,则不需要实现该接口,
如果需要使用sdk的产测授权功能并且应用代码在初始化sdk之前已经初始化过uart,
则不用实现该接口。

tuya_ble_common_uart_send_data

函数名 tuya_ble_common_uart_send_data
函数原型 tuya_ble_status_t tuya_ble_common_uart_send_data(const uint8_t *p_data,uint16_t len)
功能概述 uart 发送数据函数。
参数 p_data[in] : 要发送的数据指针;
len[in] : 要发送的数据长度。
返回值 TUYA_BLE_SUCCESS: 成功;
其他:失败。
备注 如果应用程序不使用tuya ble sdk提供的产测授权功能,则不需要实现该接口。

tuya_ble_os_task_create

函数名 tuya_ble_os_task_create
函数原型 bool tuya_ble_os_task_create(void **pp_handle, const char *p_name, void(*p_routine)(void *),void *p_param, uint16_t stack_size, uint16_t priority)
功能概述 Task create。
参数 pp_handle [out] :Used to pass back a handle by which the created task can be referenced;
p_name[in] : Adescriptive name for the task;
p_routine [in] : Pointerto task routine function that must be implemented to never return;
p_param[in] :Pointer parameter passed to the task routine function;
stack_size[in] :The size of the task stack that is specified as the number of bytes;
priority[in] : Thepriority at which the task should run. Higher priority task has higherpriority value
返回值 True : Task wascreated successfully and added to task ready list.
False:Task was failed to create.
备注 该接口函数只需在基于 os 的平台下实现。

tuya_ble_os_task_delete

函数名 tuya_ble_os_task_delete
函数原型 bool tuya_ble_os_task_delete(void *p_handle)
功能概述 Remove a task fromRTOS’s task management. The task being deleted will be removed from RUNNING,READY or WAITING state
参数 pp_handle [in] : Thehandle of the task to be deleted.
返回值 True : Task wasdeleted successfully
False:Task was failed to delete.
备注 该接口函数只需在基于 os 的平台下实现。

tuya_ble_os_task_suspend

函数名 tuya_ble_os_task_suspend
函数原型 bool tuya_ble_os_task_suspend(void *p_handle)
功能概述 Suspend the task.The suspended task will not be scheduled and never get any microcontrollerprocessing time.
参数 pp_handle [in] : Thehandle of the task to be suspend.
返回值 True : Task wassuspend successfully.
False:Task was failed to suspend.
备注 该接口函数只需在基于os的平台下实现。

tuya_ble_os_task_resume

函数名 tuya_ble_os_task_resume
函数原型 bool tuya_ble_os_task_resume(void *p_handle)
功能概述 Resume thesuspended task.
参数 pp_handle [in] : Thehandle of the task to be resumed.
返回值 True : Task wasresumed successfully.
False:Task was failed to resume.
备注 该接口函数只需在基于 os 的平台下实现。

tuya_ble_os_msg_queue_create

函数名 tuya_ble_os_msg_queue_create
函数原型 bool tuya_ble_os_msg_queue_create(void **pp_handle, uint32_t msg_num, uint32_tmsg_size)
功能概述 Creates a messagequeue instance. This allocates the storage required by the new queue andpasses back a handle for the queue.
参数 pp_handle [out] :Used to pass back a handle by which the message queue can be referenced.
msg_num [in] : Themaximum number of items that the queue can contain.
msg_size [in] : Thenumber of bytes each item in the queue will require.
返回值 True : Messagequeue was created successfully.
False:Message queue was failed to create.
备注 该接口函数只需在基于 os 的平台下实现。

tuya_ble_os_msg_queue_delete

函数名 tuya_ble_os_msg_queue_delete
函数原型 bool tuya_ble_os_msg_queue_delete(void *p_handle)
功能概述 Creates a messagequeue instance. This allocates the storage required by the new queue andpasses back a handle for the queue.
参数 pp_handle [in] : Thehandle to the message queue being deleted.
返回值 True : Messagequeue was deleted successfully.
False:Message queue was failed to delete.
备注 该接口函数只需在基于 os 的平台下实现。

tuya_ble_os_msg_queue_peek

函数名 tuya_ble_os_msg_queue_peek
函数原型 bool tuya_ble_os_msg_queue_peek(void *p_handle, uint32_t *p_msg_num)
功能概述 Peek the number ofitems sent and resided on the message queue.
参数 pp_handle [in] : Thehandle to the message queue being peeked.
p_msg_num[out] : Usedto pass back the number of items residing on the message queue.
返回值 True : Messagequeue was peeked successfully.
False:Message queue was failed to peek.
备注 该接口函数只需在基于 os 的平台下实现。

tuya_ble_os_msg_queue_send

函数名 tuya_ble_os_msg_queue_send
函数原型 bool tuya_ble_os_msg_queue_send(void *p_handle, void *p_msg, uint32_t wait_ms)
功能概述 Send an item to theback of the specified message queue.
参数 pp_handle [in] : Thehandle to the message queue on which the item is to be sent.
p_msg[in] : Pointerto the item that is to be sent on the queue.
wait_ms[in] : Themaximum amount of time in milliseconds that the task should block waiting forthe item to sent on the queue.
* 0 No blocking and returnimmediately.
* 0xFFFFFFFF Block infinitely until the item sent.
* others The timeout value in milliseconds.
返回值 True : Message itemwas sent successfully.
False:Message item was failed to send.
备注 该接口函数只需在基于 os 的平台下实现。

tuya_ble_os_msg_queue_recv

函数名 tuya_ble_os_msg_queue_recv
函数原型 bool tuya_ble_os_msg_queue_recv(void *p_handle, void *p_msg, uint32_t wait_ms)
功能概述 Receive an itemfrom the specified message queue.
参数 pp_handle [in] : Thehandle to the message queue from which the item is to be received.
p_msg[out] : Pointerto the buffer into which the received item will be copied.
wait_ms[in] : Themaximum amount of time in milliseconds that the task should block waiting forthe item to received on the queue.
* 0 No blocking and return immediately.
* 0xFFFFFFFF Block infinitely until the item received.
* others The timeout value in milliseconds.
返回值 True : Message itemwas received successfully.
False:Message item was failed to receive.
备注 该接口函数只需在基于 os 的平台下实现。

tuya_ble_event_queue_send_port

函数名 tuya_ble_event_queue_send_port
函数原型 bool tuya_ble_event_queue_send_port(tuya_ble_evt_param_t *evt, uint32_t wait_ms)
功能概述 If undefine TUYA_BLE_SELF_BUILT_TASK ,
Application should provide the task to sdk to process the event.
sdk will use this port to send event to the task ofprovided by Application.
参数 evt [in] : themessage data point to be send.
wait_ms[in] : Themaximum amount of time in milliseconds that the task should block waiting forthe item to sent on the queue.
* 0 No blocking and returnimmediately.
* 0xFFFFFFFF Block infinitely until the item sent.
* others The timeout value in milliseconds.
返回值 True : Message itemwas sent successfully.
False:Message item was failed to send.
备注 该接口函数只需在基于os的平台下实现。

tuya_ble_aes128_ecb_encrypt

函数名 tuya_ble_aes128_ecb_encrypt
函数原型 bool tuya_ble_aes128_ecb_encrypt(uint8_t *key,uint8_t *input,uint16_tinput_len,uint8_t *output)
功能概述 128 bit AES ECBencryption on speicified plaintext and keys
参数 key [in] : keys toencrypt the plaintext
In_put[in] : specifedplain text to be encypted.
in_put_len[in] : bytelength of the data to be encrypted, must be multiples of 16.
Out_put[out] : outputbuffer to store encrypted data.
返回值 True : successful.
False:fail.
备注

tuya_ble_aes128_ecb_decrypt

函数名 tuya_ble_aes128_ecb_decrypt
函数原型 bool tuya_ble_aes128_ecb_decrypt(uint8_t *key,uint8_t *input,uint16_tinput_len,uint8_t *output)
功能概述 128 bit AES ECBdecryption on speicified encrypted data and keys
参数 key [in] : keys to decryptthe plaintext
In_put[in] : specifedencypted data to be decypted
in_put_len[in] : bytelength of the data to be descrypted, must be multiples of 16.
Out_put[out] : outputbuffer to store decrypted data.
返回值 True : successful.
False:fail.
备注

tuya_ble_aes128_cbc_encrypt

函数名 tuya_ble_aes128_cbc_encrypt
函数原型 bool tuya_ble_aes128_cbc_encrypt(uint8_t *key,uint8_t *iv,uint8_t input,uint16_t input_len,uint8_toutput)
功能概述 128 bit AES CBCencryption on speicified plaintext and keys.
参数 key [in] : keys toencrypt the plaintext.
iv[in] : initializationvector (IV) for CBC mode.
In_put[in] : specifedplain text to be encypted.
in_put_len[in] : bytelength of the data to be encrypted, must be multiples of 16.
Out_put[out] : outputbuffer to store encrypted data.
返回值 True : successful.
False:fail.
备注

tuya_ble_aes128_cbc_decrypt

函数名 tuya_ble_aes128_cbc_decrypt
函数原型 bool tuya_ble_aes128_cbc_decrypt(uint8_t*key,uint8_t *iv,uint8_t *input,uint16_t input_len,uint8_t *output)
功能概述 128 bit AES CBC descryption on speicified plaintext and keys.
参数 key [in] : keys to decryptthe plaintext.
Iv[in] : initializationvector (IV) for CBC mode
In_put[in] : specifedencypted data to be decrypted.
in_put_len[in] : bytelength of the data to be descrypted, must be multiples of 16.
Out_put[out] : outputbuffer to store decrypted data.
返回值 True : successful.
False:fail.
备注

tuya_ble_md5_crypt

函数名 tuya_ble_md5_crypt
函数原型 booltuya_ble_md5_crypt(uint8_t *input,uint16_t input_len,uint8_t *output)
功能概述 MD5 checksum.
参数 In_put[in] : specifedplain text to be encypted.
in_put_len[in] : ytelength of the data to be encypted.
Out_put[out] : outputbuffer to store md5 result data,output data len is always 16.
返回值 True : successful.
False:fail.
备注

tuya_ble_hmac_sha1_crypt

函数名 tuya_ble_hmac_sha1_crypt
函数原型 bool tuya_ble_hmac_sha1_crypt(constuint8_t *key, uint32_t key_len,
const uint8_t *input, uint32_t input_len,uint8_t *output)
功能概述 calculates the fullgeneric HMAC on the input buffer with the provided key.
参数 key [in] : The HMACsecret key.
key_len[in] : Thelength of the HMAC secret key in Bytes.
In_put[in] : specifedplain text to be encypted.
in_put_len[in] : bytelength of the data to be encypted.
Out_put[out] : outputbuffer to store the result data.
返回值 True : successful.
False:fail.
备注 Not used currently, no need to implement.

tuya_ble_hmac_sha256_crypt

函数名 tuya_ble_hmac_sha1_crypt
函数原型 bool tuya_ble_hmac_sha256_crypt(const uint8_t *key, uint32_t key_len,
const uint8_t *input, uint32_t input_len, uint8_t *output)
功能概述 calculates the fullgeneric HMAC on the input buffer with the provided key.
参数 key [in] : The HMACsecret key.
key_len[in] : Thelength of the HMAC secret key in Bytes.
In_put[in] : specifedplain text to be encypted.
in_put_len[in] : bytelength of the data to be encypted.
out_put[out] : outputbuffer to store the result data.
返回值 True : successful.
False:fail.
备注 Not used currently, no need to implement.

tuya_ble_port_malloc

函数名 tuya_ble_port_malloc
函数原型 void *tuya_ble_port_malloc( uint32_t size )
功能概述 Allocate a memoryblock with required size.
参数 Size[in] : Required memory size.
返回值 The address of theallocated memory block. If the address is NULL, the memory allocation failed.
备注 该接口函数只需在 TUYA_BLE_USE_PLATFORM_MEMORY_HEAP==1 时移植实现。

tuya_ble_port_free

函数名 tuya_ble_port_free
函数原型 void tuya_ble_port_free(void *pv )
功能概述 Free a memory blockthat had been allocated.
参数 pv[in]:The address of memoryblock being freed.
返回值 None.
备注 该接口函数只需在TUYA_BLE_USE_PLATFORM_MEMORY_HEAP==1时移植实现

配置说明

tuya_ble_config.h 里的相关配置项,可将 tuya ble sdk 配置成不同的应用场景,例如多协议设备的通用配网模式、平台是否使用 OS,设备通信能力配置,sdk 是否管理授权信息等。应用程序必须新建一个自定义名称的应用配置文件,来配置相关配置项。各配置项说明如下所示:

CUSTOMIZED_TUYA_BLE_CONFIG_FILE

宏定义 CUSTOMIZED_TUYA_BLE_CONFIG_FILE
依赖项
描述 客户自定义配置文件名称,
该定义文件名称会覆盖tuya_ble_config.h文件中的默认配置,
应用程序必须新建一个自定义名称的应用配置文件,
然后将该文件名字赋值给 CUSTOMIZED_TUYA_BLE_CONFIG_FILE
例如:
CUSTOMIZED_TUYA_BLE_CONFIG_FILE = <custom_tuya_ble_config.h>
备注

CUSTOMIZED_TUYA_BLE_APP_PRODUCT_TEST_HEADER_FILE

宏定义 CUSTOMIZED_TUYA_BLE_APP_PRODUCT_TEST_HEADER_FILE
依赖项
描述 sdk提供基础的产测授权功能,
如果应用程序想使用涂鸦的测试协议实现更多的测试项,
应用程序需要新建一个产测文件实现自定义的测试项,
同时将该文件对应的头文件名字赋值给 CUSTOMIZED_TUYA_BLE_APP_PRODUCT_TEST_HEADER_FILE
备注 必须在自定义配置文件中配置。

CUSTOMIZED_TUYA_BLE_APP_UART_COMMON_HEADER_FILE

宏定义 CUSTOMIZED_TUYA_BLE_APP_UART_COMMON_HEADER_FILE
依赖项
描述 目前只应用于涂鸦通用模组协议。
备注 必须在自定义配置文件中配置。

TUYA_BLE_USE_OS

宏定义 TUYA_BLE_USE_OS
依赖项
描述 如果芯片平台架构是基于 OS 的(例如 FreeRTOS):
#define TUYA_BLE_USE_OS 1
否则:
#define TUYA_BLE_USE_OS 0
备注

TUYA_BLE_SELF_BUILT_TASK

宏定义 TUYA_BLE_SELF_BUILT_TASK
依赖项 #define TUYA_BLE_USE_OS 1
描述 在os平台架构下,tuya ble sdk 是否使用自建的task来处理消息,如果是:
#define TUYA_BLE_SELF_BUILT_TASK 1
否则:
#define TUYA_BLE_SELF_BUILT_TASK 0
备注

TUYA_BLE_TASK_PRIORITY

宏定义 TUYA_BLE_TASK_PRIORITY
依赖项 #define TUYA_BLE_USE_OS 1
#define TUYA_BLE_SELF_BUILT_TASK 1
描述 tuya ble sdk 自建 task 的优先级,例如:
#define TUYA_BLE_TASK_PRIORITY 1
备注 任务优先级,具体的优先级取值需要根据芯片平台所使用的的 os 来设置。

TUYA_BLE_TASK_STACK_SIZE

宏定义 TUYA_BLE_TASK_STACK_SIZE
依赖项 #define TUYA_BLE_USE_OS 1
#define TUYA_BLE_SELF_BUILT_TASK 1
描述 tuya ble sdk 自建task的stack大小,例如:
#define TUYA_BLE_TASK_STACK_SIZE 256*10
备注

TUYA_BLE_DEVICE_COMMUNICATION_ABILITY

宏定义 TUYA_BLE_DEVICE_COMMUNICATION_ABILITY
依赖项
描述 设备能力定义,
参数说明:
TUYA_BLE_DEVICE_COMMUNICATION_ABILITY_BLE
— 是否支持 BLE;
TUYA_BLE_DEVICE_COMMUNICATION_ABILITY_REGISTER_FROM_BLE
— 是否通过 BLE 注册设备;
TUYA_BLE_DEVICE_COMMUNICATION_ABILITY_MESH
— 是否支持MESH;
TUYA_BLE_DEVICE_COMMUNICATION_ABILITY_WIFI_24G
— 是否支持2.4G Wi-Fi;
TUYA_BLE_DEVICE_COMMUNICATION_ABILITY_WIFI_5G
— 是否支持5G Wi-Fi;
TUYA_BLE_DEVICE_COMMUNICATION_ABILITY_ZIGBEE
— 是否支持zigbee;
TUYA_BLE_DEVICE_COMMUNICATION_ABILITY_NB
— 是否支持NB-IoT。
备注

TUYA_BLE_DEVICE_SHARED

宏定义 TUYA_BLE_DEVICE_SHARED
依赖项
描述 是否是共享类设备
备注 如果不明白共享类设备定义,请 #define TUYA_BLE_DEVICE_SHARED 0

TUYA_BLE_DEVICE_UNBIND_MODE

宏定义 TUYA_BLE_DEVICE_UNBIND_MODE
依赖项 无。
描述 主要应用于共享类设备是否需要执行实际的解绑操作?
备注 如果不明白共享类设备定义,请 #define TUYA_BLE_DEVICE_UNBIND_MODE 1

TUYA_BLE_WIFI_DEVICE_REGISTER_MODE

宏定义 TUYA_BLE_WIFI_DEVICE_REGISTER_MODE
依赖项 设备能力必须支持wifi
描述 通过BLE对WIFI进行配网时,是否首先发送查询配网状态指令,如果是:
#define TUYA_BLE_WIFI_DEVICE_REGISTER_MODE 1
否则:
#define TUYA_BLE_WIFI_DEVICE_REGISTER_MODE 0
备注 目前暂不使用

TUYA_BLE_DEVICE_AUTH_SELF_MANAGEMENT

宏定义 TUYA_BLE_DEVICE_AUTH_SELF_MANAGEMENT
依赖项
描述 tuya ble sdk 是否需要自己管理授权信息,如果是:
#define TUYA_BLE_DEVICE_AUTH_SELF_MANAGEMENT 1
否则:
#define TUYA_BLE_DEVICE_AUTH_SELF_MANAGEMENT 0
备注 对于不具备 Wi-Fi 能力的 BLE 设备,该值建议定义为 1

TUYA_BLE_SECURE_CONNECTION_TYPE

宏定义 TUYA_BLE_SECURE_CONNECTION_TYPE
依赖项
描述 tuya ble sdk通信安全加密方式,定义如下:
TUYA_BLE_SECURE_CONNECTION_WITH_AUTH_KEY
— encrypt with auth key;
TUYA_BLE_SECURE_CONNECTION_WITH_ECC
— encrypt with ECDH;
TUYA_BLE_SECURE_CONNECTION_WTIH_PASSTHROUGH
— no encrypt;
例如:
#define TUYA_BLE_DEVICE_AUTH_SELF_MANAGEMENT TUYA_BLE_SECURE_CONNECTION_WITH_AUTH_KEY
备注 目前仅支持TUYA_BLE_SECURE_CONNECTION_WITH_AUTH_KEY

TUYA_BLE_DEVICE_MAC_UPDATE

宏定义 TUYA_BLE_DEVICE_MAC_UPDATE
依赖项
描述 是否需要使用涂鸦授权信息中的mac地址作为设备mac地址?
备注

TUYA_BLE_DEVICE_MAC_UPDATE_RESET

宏定义 TUYA_BLE_DEVICE_MAC_UPDATE_RESET
依赖项 #define TUYA_BLE_DEVICE_MAC_UPDATE 1
描述 设备更新mac地址后是否需要重启生效,如果是:
#define TUYA_BLE_DEVICE_MAC_UPDATE_RESET 1
否则:
#define TUYA_BLE_DEVICE_MAC_UPDATE_RESET 0
备注

TUYA_BLE_USE_PLATFORM_MEMORY_HEAP

宏定义 TUYA_BLE_USE_PLATFORM_MEMORY_HEAP
依赖项
描述 tuya ble sdk 是否使用自己的内存堆,如果是:
#define TUYA_BLE_USE_PLATFORM_MEMORY_HEAP 0
否则:
#define TUYA_BLE_USE_PLATFORM_MEMORY_HEAP 1
备注 如果定义为 1,需要 Application 在port层移植实现内存管理函数供 tuya ble sdk 使用。

TUYA_BLE_GATT_SEND_DATA_QUEUE_SIZE

宏定义 TUYA_BLE_GATT_SEND_DATA_QUEUE_SIZE
依赖项
描述 tuya ble sdk 内部使用的gatt发送队列的大小,默认20:
#define TUYA_BLE_GATT_SEND_DATA_QUEUE_SIZE 20
备注 如果对该宏定义不了解,建议保持为 20,因为我们在多个平台测试过。

TUYA_BLE_DATA_MTU_MAX

宏定义 TUYA_BLE_DATA_MTU_MAX
依赖项
描述 GATT MTU大小:
#define TUYA_BLE_DATA_MTU_MAX 20
备注 目前仅支持 20 字节的 GATT MTU,后续会扩展升级。

TUYA_BLE_LOG_ENABLE

宏定义 TUYA_BLE_LOG_ENABLE
依赖项
描述 是否开启tuya ble sdk内部 log 打印,如果开启:
#define TUYA_BLE_LOG_ENABLE 1
否则:
#define TUYA_BLE_LOG_ENABLE 0
备注 开启 sdk 内部 log ,会增加一定的代码空间,建议debug 版本开启,release 版关掉。

TUYA_BLE_LOG_COLORS_ENABLE

宏定义 TUYA_BLE_LOG_COLORS_ENABLE
依赖项 #define TUYA_BLE_LOG_ENABLE 1
描述 是否开启tuya ble sdk 内部 log 多颜色显示,如果开启:
#define TUYA_BLE_LOG_COLORS_ENABLE 1
否则:
#define TUYA_BLE_LOG_COLORS_ENABLE 0
备注 并不是所有的 log 显示工具都支持,例如使用 J-Link 的 RTT 时不支持。

TUYA_BLE_LOG_LEVEL

宏定义 TUYA_BLE_LOG_LEVEL
依赖项 #define TUYA_BLE_LOG_ENABLE 1
描述 定义tuya ble sdk内部log的显示等级,分为如下几个等级:
#defineTUYA_BLE_LOG_LEVEL_ERROR 1U
#defineTUYA_BLE_LOG_LEVEL_WARNING 2U
#defineTUYA_BLE_LOG_LEVEL_INFO 3U
#defineTUYA_BLE_LOG_LEVEL_DEBUG 4U
如果只需要打印错误信息,则定义为:
#define TUYA_BLE_LOG_LEVEL TUYA_BLE_LOG_LEVEL_ERROR
备注

TUYA_APP_LOG_ENABLE

宏定义 TUYA_APP_LOG_ENABLE
依赖项
描述 是否开启Application log,如果开启:
#define TUYA_APP_LOG_ENABLE 1
否则:
#define TUYA_APP_LOG_ENABLE 0
备注 开启log会增加一定的代码空间,建议debug版本开启,release版关掉。

TUYA_APP_LOG_COLORS_ENABLE

宏定义 TUYA_APP_LOG_COLORS_ENABLE
依赖项 #define TUYA_APP_LOG_ENABLE 1
描述 是否开启Application log多颜色显示,如果开启:
#define TUYA_APP_LOG_COLORS_ENABLE 1
否则:
#define TUYA_APP_LOG_COLORS_ENABLE 0
备注 并不是所有的 log 显示工具都支持,例如使用 J-Link 的 RTT 时不支持。

TUYA_APP_LOG_LEVEL

宏定义 TUYA_APP_LOG_LEVEL
依赖项 #define TUYA_APP_LOG_ENABLE 1
描述 定义Application log的显示等级,分为如下几个等级:
#defineTUYA_APP_LOG_LEVEL_ERROR 1U
#defineTUYA_APP_LOG_LEVEL_WARNING 2U
#defineTUYA_APP_LOG_LEVEL_INFO 3U
#defineTUYA_APP_LOG_LEVEL_DEBUG 4U
如果只需要打印错误信息,则定义为:
#define TUYA_APP_LOG_LEVEL TUYA_APP_LOG_LEVEL_ERROR
备注

TUYA_BLE_ADVANCED_ENCRYPTION_DEVICE

宏定义 TUYA_BLE_ADVANCED_ENCRYPTION_DEVICE
依赖项
描述 是否使用高级加密方式,如果是:
#define TUYA_BLE_ADVANCED_ENCRYPTION_DEVICE 1
否则:
#define TUYA_BLE_ADVANCED_ENCRYPTION_DEVICE 0
备注 暂不支持高级加密。

TUYA_NV_ERASE_MIN_SIZE

宏定义 TUYA_NV_ERASE_MIN_SIZE
依赖项
描述 给tuya ble sdk 分配的NV(flash)空间最小擦除单位,例如:
#define TUYA_NV_ERASE_MIN_SIZE 4096
备注 根据port层对NV接口的实现方式来定义。

TUYA_NV_WRITE_GRAN

宏定义 TUYA_NV_WRITE_GRAN
依赖项
描述 NV(flash)写入粒度,例如:
#define TUYA_NV_WRITE_GRAN 4
表示只能以4字节单位写入。
备注 根据port层对NV接口的实现方式来定义。

TUYA_NV_START_ADDR

宏定义 TUYA_NV_START_ADDR
依赖项
描述 给 tuya ble sdk 分配的 NV(flash)起始地址,例如:
#define TUYA_NV_START_ADDR 0x1000
备注

TUYA_NV_AREA_SIZE

宏定义 TUYA_NV_AREA_SIZE
依赖项
描述 给tuya ble sdk 分配的NV(flash)大小,例如:
#define TUYA_NV_AREA_SIZE (4*TUYA_NV_ERASE_MIN_SIZE)
大小必须是TUYA_NV_ERASE_MIN_SIZE的整数倍。
备注

TUYA_BLE_APP_VERSION_STRING

宏定义 TUYA_BLE_APP_VERSION_STRING
依赖项
描述 Application版本号字符串,例如:
#define TUYA_BLE_APP_VERSION_STRING "1.0"
备注 当前只支持两位版本号。

TUYA_BLE_APP_BUILD_FIRMNAME_STRING

宏定义 TUYA_BLE_APP_BUILD_FIRMNAME_STRING
依赖项
描述 Application固件名称,例如:
#define TUYA_BLE_APP_BUILD_FIRMNAME_STRING "tuya_ble_sdk_app_demo_xxx"
备注 只有使用涂鸦产测授权协议时才需要。

api 介绍

tuya ble sdk 提供封装好的 api 用于设备Application实现BLE相关的管理、通信等,api 函数定义在 tuya_ble_api.ctuya_ble_api.h文件中,无需更改,有兴趣可以阅读源码理解实现原理,下面对各个 api 做个介绍。

tuya_ble_main_tasks_exec

函数名 tuya_ble_main_tasks_exec
函数原型 void tuya_ble_main_tasks_exec(void)
功能概述 在不使用 OS 的芯片平台架构下 tuya ble sdk 的事件主调度器,应用程序必须在主循环中调用。
参数
返回值
备注 This function mustbe called from within the main loop.
It will execute all events scheduled since the last time it was called.

示例:例如在 nrf52832 平台下的调用位置如下所示:

/**@brief Function for handling the idle state (main loop).
 *
 * @details If there is no pending log operation, then sleep until next the next event occurs.
 */
static void idle_state_handle(void)
{
    tuya_ble_main_tasks_exec();

    if (NRF_LOG_PROCESS() == false)
    {
        nrf_pwr_mgmt_run();
    }

}

tuya_ble_gatt_receive_data

函数名 tuya_ble_gatt_receive_data
函数原型 tuya_ble_status_t tuya_ble_gatt_receive_data(uint8_t*p_data,uint16_t len);
功能概述 通过调用该函数将蓝牙收到的 gatt 数据发送至 tuya ble sdk 。
参数 p_data[in] : 指向要发送的数据;
len[in] : 要发送的数据长度,不能超过 TUYA_BLE_DATA_MTU_MAX
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INTERNAL: 发送失败。
备注 This function must be called from where the BLE data is received.

示例(nrf52832示例demo):

/**@brief Function for handling the data from the Nordic UART Service.
 *
 * @details This function will process the data received from the Nordic UART BLE Service and send
 *          it to the UART module.
 *
 * @param[in] p_evt       Nordic UART Service event.
 */
/**@snippet [Handling the data received over BLE] */
static void nus_commdata_handler(ble_nus_evt_t * p_evt)
{

    if (p_evt->type == BLE_NUS_EVT_RX_DATA)
    {
        tuya_ble_gatt_receive_data((uint8_t*)(p_evt->params.rx_data.p_data),p_evt->params.rx_data.length);
        TUYA_BLE_HEXDUMP("nus_commdata_handler :",20,(uint8_t*)(p_evt->params.rx_data.p_data),p_evt->params.rx_data.length);
    }

}

tuya_ble_common_uart_receive_data

函数名 tuya_ble_common_uart_receive_data
函数原型 tuya_ble_status_t tuya_ble_common_uart_receive_data (uint8_t *p_data,uint16_t len);
功能概述 如果Application使用tuya ble sdk提供的产测授权模块,
那么需要通过调用该函数将 uart 数据发送至 tuya ble sdk;
如果不使用tuya ble sdk提供的产测授权功能模块,那么就不需要调用该函数。
参数 p_data[in] : 指向要发送的数据;
len[in] : 要发送的数据长度。
返回值 TUYA_BLE_SUCCESS :发送成功;
其他: 发送失败。
备注 该函数内部有调用 malloc申请内存,
如果应用配置使用的是平台提供的 malloc 接口,
要确认平台 malloc 是否支持中断调用,
如果不支持,一定不能在 UART 中断里调用tuya_ble_common_uart_receive_data()

tuya_ble_common_uart_send_full_instruction_received

函数名 tuya_ble_common_uart_send_full_instruction_received
函数原型 tuya_ble_status_t
tuya_ble_common_uart_send_full_instruction_received (uint8_t *p_data,uint16_t len);
功能概述 通过调用该函数将通过 uart 接收并并解析好的完整tuya产测协议指令
(包含 cmd/data/checksum)发送至 tuya ble sdk。
如果Application不使用tuya ble sdk提供的协议解析函数
tuya_ble_common_uart_receive_data()而是使用自定义的uart接收解析函数来
解析产测协议数据,那么解析完成后需要调用该函数将解析好的完成产测指令发送给tuya ble sdk。
参数 p_data[in] : 指向要发送的完整指令数据;
len[in] : 要发送的指令数据长度。
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_PARAM:参数有误;
TUYA_BLE_ERR_NO_MEM:内存申请失败;
TUYA_BLE_ERR_BUSY :BLE SDK内部忙。
备注 该函数内部有调用 malloc,如果应用配置使用的是平台提供的 malloc 接口,
要确认平台 malloc 是否支持中断调用,
如果不支持,一定不能在 UART 中断里调用tuya_ble_common_uart_send_full_instruction_received()

说明:

  • tuya ble sdk 集成产测授权功能模块,产测授权模块通过uart和PC端的产测授权工具进行通信,uart通信有一套完整的指令格式,具体参照《蓝牙通用产测授权协议》。

  • tuya ble sdk 包含 uart 通信指令解析功能,应用只需要在收到uart的数据的地方调用tuya_ble_common_uart_receive_data() 函数即可,当然应用也可以自己解析出完整的uart通信指令,然后通过调用 tuya_ble_common_uart_send_full_instruction_received() 函数发送完整指令给 tuya ble sdk。

tuya_ble_device_update_product_id

函数名 tuya_ble_device_update_product_id
函数原型 tuya_ble_status_t tuya_ble_device_update_product_id
(tuya_ble_product_id_type_t type, uint8_t len, uint8_t* p_buf);
功能概述 更新 product id 函数。
参数 type [in] : id 类型;
len[in] : id 长度;
p_buf[in] : id 数据。
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_PARAM:参数有误;
TUYA_BLE_ERR_INTERNAL:内部错误。
备注 在基于tuya ble sdk 的 SOC 开发方案中一般不需要调用此函数,因为 product id 一般不会变。

tuya_ble_device_update_login_key

函数名 tuya_ble_device_update_login_key
函数原型 tuya_ble_status_t tuya_ble_device_update_login_key(uint8_t* p_buf, uint8_t len);
功能概述 更新 login key 函数。
参数 len[in] : 长度。
p_buf[in] : loginkey。
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_PARAM:参数有误;
TUYA_BLE_ERR_INTERNAL:内部错误。
备注 该api主要用于Wi-Fi/BLE双协议设备中
(通过BLE发送配网信息给Wi-Fi,设备通过Wi-Fi向云端注册设备并将
注册成功后的login key调用此函数发送给tuya ble sdk,同时也需要更新绑定状态)。

tuya_ble_device_update_bound_state

函数名 tuya_ble_device_update_bound_state
函数原型 tuya_ble_status_t tuya_ble_device_update_bound_state(uint8_t state);
功能概述 更新注册绑定状态。
参数 state[in] : 1-设备已注册绑定,0-设备未注册绑定。
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_PARAM:参数有误;
TUYA_BLE_ERR_INTERNAL:内部错误。
备注 该api主要用于 Wi-Fi/BLE 双协议设备中
(设备通过 Wi-Fi 链路注册绑定成功后调用此函数更新绑定状态给 tuya ble sdk,
同时也需要更新 login key)。

tuya_ble_device_update_mcu_version

函数名 tuya_ble_device_update_mcu_version
函数原型 tuya_ble_status_t tuya_ble_device_update_mcu_version(uint32_t mcu_firmware_version, uint32_t mcu_hardware_version);
功能概述 更新外部mcu版本号。
参数 mcu_firmware_version [in] : MCU 固件版本号,例如0x010101表示v1.1.1;
mcu_hardware_version [in] : MCU 硬件版本号(PCBA版本号);
返回值 TUYA_BLE_SUCCESS :发送成功;
其他 :失败。
备注 该 api 主要用于开发蓝牙 BLE 模组,SOC 开发方案不需要使用。

tuya_ble_sdk_init

函数名 tuya_ble_sdk_init
函数原型 tuya_ble_status_t tuya_ble_sdk_init(tuya_ble_device_param_t * param_data);
功能概述 tuya ble sdk 初始化函数。
参数 param_data [in] : 初始化参数。
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_PARAM:参数无效。
备注 tuya ble sdk 初始化函数,应用程序必须调用此函数初始化sdk ,否则sdk不能工作。
初始化函数也有可能是tuya_ble_sdk_init_async()

参数说明:

tuya_ble_device_param_t 结构体如下所示:

typedef struct{
    uint8_t device_id_len;   //if ==20,Compressed into 16
	uint8_t device_id[DEVICE_ID_LEN_MAX];
    tuya_ble_product_id_type_t p_type;
    uint8_t product_id_len;
	uint8_t product_id[TUYA_BLE_PRODUCT_ID_MAX_LEN];
    uint8_t device_vid[DEVICE_VIRTUAL_ID_LEN];
	uint8_t auth_key[AUTH_KEY_LEN];
	uint8_t login_key[LOGIN_KEY_LEN];
    uint8_t bound_flag;
    uint32_t firmware_version; //0x00010102 : v1.1.2
    uint32_t hardware_version;
    uint8_t reserve_1;
    uint8_t reserve_2;
}tuya_ble_device_param_t;	

各成员变量含义:

  • device id 和 auth key:device_uuid 和auth key 是涂鸦云平台分配给设备的唯一 id,在产测授权时通过产测工具写入,uuid 和 auth key 一一对应,单BLE设备经过产测授权后,tuya ble sdk会自动管理授权信息(device uuid和auth key),所以一般应用初始化 sdk 时将 device id 和auth key 赋值 0 即可;如果不为0,tuya ble sdk将会使用在初始化 sdk 时传入的 device id 和 auth key。当然,对于 Wi-Fi / BLE 双协议设备来讲,device id 和 auth key 是由 Wi-Fi 端来管理的,所以在初始化 tuya ble sdk 时也需要传入。

  • product id :产品 id 简称 pid,是在 iot 工作台新建产品时自动生成的,生成后不会改变,需要应用代码以常量的形式保存,需要在初始化 tuya ble sdk 时传入。

  • device_vid :设备虚拟 id,设备注册绑定后由涂鸦云平台生成,主要作用是设备绑定解绑再绑定时通过该 id 来查找云端对该设备的历史数据记录,对于需要 tuya ble sdk 管理授权信息的 BLE 设备来说,赋值为 0 即可,对于 Wi-Fi / BLE 多协议设备或者 Application 管理授权信息的设备需要传入此参数。

  • login key 和bound flag :对于需要tuya ble sdk管理授权信息的 BLE 设备赋值为 0 即可,对于 Wi-Fi / BLE 多协议设备或者Application管理授权信息的设备需要传入。

  • firmware_version 和 hardware_version :设备固件版本号和硬件(PCBA)版本号,需要在初始化 tuya ble sdk时传入,目前蓝牙 BLE 设备只支持两位版本号例如(0x0100 表示 V1.0)。

nrf52832平台的tuya ble sdk初始化示例:

static const char auth_key_test[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
static const char device_id_test[] = "yyyyyyyyyyyyyyyy";

#define APP_PRODUCT_ID  "vvvvvvvv"

void tuya_ble_app_init(void)
{
	tuya_ble_device_param_t device_param = {0};
    device_param.device_id_len = 16;
    memcpy(device_param.auth_key,(void *)auth_key_test,AUTH_KEY_LEN);
    memcpy(device_param.device_id,(void *)device_id_test,DEVICE_ID_LEN);
    device_param.p_type = TUYA_BLE_PRODUCT_ID_TYPE_PID;
    device_param.product_id_len = 8;
    memcpy(device_param.product_id,APP_PRODUCT_ID,8);
    device_param.firmware_version = TY_APP_VER_NUM;
    device_param.hardware_version = TY_HARD_VER_NUM;

    tuya_ble_sdk_init(&device_param);
    tuya_ble_callback_queue_register(tuya_cb_handler);

    tuya_ota_init();
}

/*nrf52832示例*/
int main(void)
{
    bool erase_bonds;

    // Initialize.
    uart_init();
    app_log_init();
    timers_init();
    buttons_leds_init(&erase_bonds);
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();   
    tuya_ble_app_init();
    advertising_start();
    
    NRF_LOG_INFO("app version : %s\r\n",TY_APP_VER_STR);
       
    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
    }
}

tuya_ble_sdk_init_async

函数名 tuya_ble_sdk_init_async
函数原型 void tuya_ble_sdk_init_async(tuya_ble_device_param_t * param_data,tuya_ble_nv_async_callback_t callback)
功能概述 tuya ble sdk 初始化函数,主要在异步flash操作架构的tuya ble sdk中使用。
参数 param_data [in] : 初始化参数。
callback[in] :回调函数,tuya ble sdk初始化完成后会自动执行该回调。
返回值 无。
备注 tuya ble sdk 异步初始化函数,应用程序必须调用此函数初始化sdk ,否则sdk不能工作。

tuya ble sdk异步初始化示例:

static const char auth_key_test[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
static const char device_id_test[] = "yyyyyyyyyyyyyyyy";

#define APP_PRODUCT_ID  "vvvvvvvv"

static void tuya_ble_sdk_init_async_completed(void *p_param,tuya_ble_status_t result)
{
    if(result==TUYA_BLE_SUCCESS)
    {
        tuya_ble_callback_queue_register(tuya_cb_handler);

        tuya_ota_init();

        TUYA_APP_LOG_INFO("tuya sdk init succeed.");
        TUYA_APP_LOG_INFO("app version : "TY_APP_VER_STR);
        TUYA_APP_LOG_DEBUG("current free heap size = %d",xTuyaPortGetFreeHeapSize());
        //tuya_ble_device_factory_reset();
    }
    else
    {
        TUYA_APP_LOG_INFO("tuya sdk init failed.");
    }
}

void tuya_ble_app_init(void)
{
    tuya_ble_nv_init_custom();
    memset(&device_param,0,sizeof(tuya_ble_device_param_t));
    device_param.device_id_len = 16;
    memcpy(device_param.auth_key,(void *)auth_key_test,AUTH_KEY_LEN);
    memcpy(device_param.device_id,(void *)device_id_test,DEVICE_ID_LEN);
    
    device_param.p_type = TUYA_BLE_PRODUCT_ID_TYPE_PID;
    device_param.product_id_len = 8;
    memcpy(device_param.product_id,APP_PRODUCT_ID,8);
    device_param.firmware_version = TY_APP_VER_NUM;
    device_param.hardware_version = TY_HARD_VER_NUM;

    tuya_ble_sdk_init_async(&device_param,tuya_ble_sdk_init_async_completed);
    
        
}

/*nrf52832示例*/
int main(void)
{
    bool erase_bonds;

    // Initialize.
    uart_init();
    app_log_init();
    timers_init();
    buttons_leds_init(&erase_bonds);
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();   
    tuya_ble_app_init();
    advertising_start();
    
    NRF_LOG_INFO("app version : %s\r\n",TY_APP_VER_STR);
       
    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
    }
}

tuya_ble_dp_data_report

函数名 tuya_ble_dp_data_report
函数原型 tuya_ble_status_t tuya_ble_dp_data_report(uint8_t *p_data,uint32_t len);
功能概述 上报dp点数据。
参数 p_data [in] : dp点数据。
len[in] : 数据长度,最大不能超过TUYA_BLE_REPORT_MAX_DP_DATA_LEN
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_PARAM:参数无效;
TUYA_BLE_ERR_INVALID_STATE:当前状态不支持发送,例如蓝牙断开;
TUYA_BLE_ERR_NO_MEM:内存申请失败;
TUYA_BLE_ERR_INVALID_LENGTH:数据长度错误 ;
TUYA_BLE_ERR_NO_EVENT:其他错误。
备注 Application通过调用该函数上报 DP 点数据到 手机app。

参数说明:

  • 涂鸦云平台是以 dp 点的方式管理数据,任何设备产生的数据都需要抽象为 dp 点的形式,一个完整的dp点数据由四部分组成(具体参考 IoT 工作台上的相关介绍):

Dp_id: 1个字节,在开发平台注册的 dp_id 序号。

Dp_type:1 个字节,dp点类型。

#define DT_RAW 0 raw类型;

#define DT_BOOL 1 布尔类型;

#define DT_VALUE 2 数值类型,其范围在iot平台注册时指定;

#define DT_STRING 3 字符串类型;

#define DT_ENUM 4 枚举类型;

#define DT_BITMAP 5 位映射类型;

Dp_len:1 个字节或者两个字节,目前蓝牙仅支持一个字节,即单个 dp 点数据最长 255 个字节。

Dp_data:数据,dp_len 个字节。

  • 该 dp 点上报函数的参数 p_data 指向的数据必须以下表格式组装上报:
Dp点1的数据 ~ Dp点n的数据
1 2 3 4~ ~ n n+1 n+2 n+3~
Dp_id Dp_type Dp_len Dp_data ~ Dp_id Dp_type Dp_len Dp_data
  • 调用该函数时,参数 len 的最大长度为 TUYA_BLE_REPORT_MAX_DP_DATA_LEN(当前为255+3)。

tuya_ble_dp_data_with_time_report

函数名 tuya_ble_dp_data_with_time_report
函数原型 tuya_ble_status_t tuya_ble_dp_data_with_time_report(uint32_t timestamp,uint8_t *p_data,uint32_t len);
功能概述 上报带时间戳的 dp 点数据。
参数 timestamp[in] : 4字节 unix 时间戳;
p_data [in] : dp点数据;
len[in] : 数据长度,最大不能超过 TUYA_BLE_REPORT_MAX_DP_DATA_LEN
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_PARAM:参数无效;
TUYA_BLE_ERR_INVALID_STATE:当前状态不支持发送,例如蓝牙断开;
TUYA_BLE_ERR_NO_MEM:内存申请失败;
TUYA_BLE_ERR_INVALID_LENGTH:数据长度错误。
TUYA_BLE_ERR_NO_EVENT:其他错误。
备注 应用代码通过调用该函数带时间戳上报 dp 点数据到 App;
一般是离线缓存的数据才需要带时间戳上报。

tuya_ble_dp_data_with_time_ms_string_report

函数名 tuya_ble_dp_data_with_time_ms_string_report
函数原型 tuya_ble_status_t tuya_ble_dp_data_with_time_ms_string_report(uint8_t *time_string,uint8_t *p_data,uint32_t len)
功能概述 上报带字符串格式时间的 dp 点数据。
参数 time_string[in] : 13 字节 ms 级字符串格式时间;
p_data [in] : dp 点数据;
Len[in] : 数据长度,最大不能超过 TUYA_BLE_REPORT_MAX_DP_DATA_LEN
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_PARAM:参数无效;
TUYA_BLE_ERR_INVALID_STATE:当前状态不支持发送,例如蓝牙断开;
TUYA_BLE_ERR_NO_MEM:内存申请失败;
TUYA_BLE_ERR_INVALID_LENGTH:数据长度错误;
TUYA_BLE_ERR_NO_EVENT:其他错误。
备注 应用代码通过调用该函数带字符串格式时间上报 dp 点数据到 App;
一般是离线缓存的数据才需要带时间上报;
13 字节 ms 级时间字符串,例如 “0000000123456”,不够 13 字节前面补字符 0。

tuya_ble_dp_data_with_flag_report

函数名 tuya_ble_dp_data_with_flag_report
函数原型 tuya_ble_status_t tuya_ble_dp_data_with_flag_report(uint16_t sn,tuya_ble_report_mode_t mode,uint8_t *p_data,uint32_t len)
功能概述 带标志上报dp点数据。
参数 sn[in] : Application定义的序号;
mode[in] : 上报模式;
p_data [in] : dp点数据。
len[in] : dp点数据长度,最大不能超过TUYA_BLE_REPORT_MAX_DP_DATA_LEN
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_PARAM:参数无效;
TUYA_BLE_ERR_INVALID_STATE:当前状态不支持发送,例如蓝牙断开;
TUYA_BLE_ERR_NO_MEM:内存申请失败;
TUYA_BLE_ERR_INVALID_LENGTH:数据长度错误 ;
TUYA_BLE_ERR_NO_EVENT:其他错误。
备注 Application通过调用该函数带标志上报 DP 点数据到 手机app。

参数说明:

mode :

REPORT_FOR_CLOUD_PANEL : 同时上报面板和云端;

REPORT_FOR_CLOUD : 只上报给云端;

REPORT_FOR_PANEL : 只上报给面板;

REPORT_FOR_NONE : 既不上报给面板也不上报给云端;

tuya_ble_dp_data_with_flag_and_time_report

函数名 tuya_ble_dp_data_with_flag_and_time_report
函数原型 tuya_ble_status_t tuya_ble_dp_data_with_flag_and_time_report
(uint16_t sn,tuya_ble_report_mode_t mode,uint32_t timestamp,uint8_t *p_data,uint32_t len)
功能概述 带标志上报带时间戳的 dp 点数据。
参数 sn[in] : Application定义的序号;
mode[in] : 上报模式;
timestamp[in] : 4字节 unix 时间戳;
p_data [in] : dp点数据;
len[in] : 数据长度,最大不能超过 TUYA_BLE_REPORT_MAX_DP_DATA_LEN
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_PARAM:参数无效;
TUYA_BLE_ERR_INVALID_STATE:当前状态不支持发送,例如蓝牙断开;
TUYA_BLE_ERR_NO_MEM:内存申请失败;
TUYA_BLE_ERR_INVALID_LENGTH:数据长度错误。
TUYA_BLE_ERR_NO_EVENT:其他错误。
备注 应用代码通过调用该函数带时间戳上报 dp 点数据到 App;
一般是离线缓存的数据才需要带时间戳上报。

tuya_ble_dp_data_with_flag_and_time_ms_string_report

函数名 tuya_ble_dp_data_with_flag_and_time_ms_string_report
函数原型 tuya_ble_status_t tuya_ble_dp_data_with_flag_and_time_ms_string_report
(uint16_t sn,tuya_ble_report_mode_t mode,uint8_t *time_string,uint8_t *p_data,uint32_t len)
功能概述 带标志上报带字符串格式时间的 dp 点数据。
参数 sn[in] : Application定义的序号;
mode[in] : 上报模式;
time_string[in] : 13 字节 ms 级字符串格式时间;
p_data [in] : dp 点数据;
Len[in] : 数据长度,最大不能超过 TUYA_BLE_REPORT_MAX_DP_DATA_LEN
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_PARAM:参数无效;
TUYA_BLE_ERR_INVALID_STATE:当前状态不支持发送,例如蓝牙断开;
TUYA_BLE_ERR_NO_MEM:内存申请失败;
TUYA_BLE_ERR_INVALID_LENGTH:数据长度错误;
TUYA_BLE_ERR_NO_EVENT:其他错误。
备注 应用代码通过调用该函数带字符串格式时间上报 dp 点数据到 App;
一般是离线缓存的数据才需要带时间上报;
13 字节 ms 级时间字符串,例如 “0000000123456”,不够 13 字节前面补字符 0。

tuya_ble_connected_handler

函数名 tuya_ble_connected_handler
函数原型 void tuya_ble_connected_handler(void)
功能概述 蓝牙连接回调函数。
参数 无。
返回值 无。
备注 应用代码需要在芯片平台 sdk的蓝牙连接回调处调用此函数,
tuya ble sdk是基于该函数来管理内部蓝牙连接状态的。

tuya_ble_disconnected_handler

函数名 tuya_ble_disconnected_handler
函数原型 void tuya_ble_disconnected_handler(void)
功能概述 蓝牙断开连接回调函数。
参数 无。
返回值 无。
备注 应用代码需要在芯片平台 sdk的蓝牙断开连接回调处调用此函数,
tuya ble sdk 是根据此函数的执行来管理内部蓝牙连接状态的。

nrf52832平台调用tuya_ble_connected_handler和tuya_ble_disconnected_handler的示例:

/**@brief Function for handling BLE events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 * @param[in]   p_context   Unused.
 */
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    uint32_t err_code;

    switch (p_ble_evt->header.evt_id)
    {
    case BLE_GAP_EVT_CONNECTED:

        NRF_LOG_INFO("Connected");

        tuya_ble_connected_handler();

        err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
        APP_ERROR_CHECK(err_code);
        m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
        err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
        APP_ERROR_CHECK(err_code);

        break;

    case BLE_GAP_EVT_DISCONNECTED:
        
        NRF_LOG_INFO("Disconnected");
    
        tuya_ble_disconnected_handler();
    
        tuya_ota_init_disconnect();
        // LED indication will be changed when advertising starts.
        m_conn_handle = BLE_CONN_HANDLE_INVALID;
        break;

    case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
    {
        NRF_LOG_DEBUG("PHY update request.");
        ble_gap_phys_t const phys =
        {
            .rx_phys = BLE_GAP_PHY_AUTO,
            .tx_phys = BLE_GAP_PHY_AUTO,
        };
        err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
        APP_ERROR_CHECK(err_code);
    }
    break;

    case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
        // Pairing not supported
        err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
        APP_ERROR_CHECK(err_code);
        break;

    case BLE_GATTS_EVT_SYS_ATTR_MISSING:
        // No system attributes have been stored.
        err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
        APP_ERROR_CHECK(err_code);
        break;

    case BLE_GATTC_EVT_TIMEOUT:
        // Disconnect on GATT Client timeout event.
        err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
                                         BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
        APP_ERROR_CHECK(err_code);
        break;

    case BLE_GATTS_EVT_TIMEOUT:
        // Disconnect on GATT Server timeout event.
        err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
                                         BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
        APP_ERROR_CHECK(err_code);
        break;

    default:
        // No implementation needed.
        break;
    }
}

tuya_ble_adv_data_connecting_request_set

函数名 tuya_ble_adv_data_connecting_request_set
函数原型 tuya_ble_status_t tuya_ble_adv_data_connecting_request_set(uint8_t on_off)
功能概述 触发广播包改变请求连接标志。
参数 on_off[in] : 0 - 清除标志,1 - 置位标志。
返回值 TUYA_BLE_SUCCESS :成功;
TUYA_BLE_ERR_INVALID_STATE:当前状态不支持,例如当前是已连接状态;
TUYA_BLE_ERR_INVALID_PARAM:参数错误;
TUYA_BLE_ERR_NO_EVENT:其他错误。
备注

tuya_ble_data_passthrough

函数名 tuya_ble_data_passthrough
函数原型 tuya_ble_status_t tuya_ble_data_passthrough(uint8_t *p_data,uint32_t len)
功能概述 透传Appliaction自定义数据。
参数 p_data [in] : 需要透传的数据指针
len[in] : 数据长度,最大不能超过 TUYA_BLE_TRANSMISSION_MAX_DATA_LEN
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_STATE:当前状态不支持发送,例如蓝牙断开;TUYA_BLE_ERR_INVALID_LENGTH:数据长度错误;
TUYA_BLE_ERR_NO_EVENT:其他错误。
备注 Application通过调用该函数透传数据到App;
透传的数据格式是由设备Application和手机App端协商的,tuya ble sdk不做解析。

tuya_ble_production_test_asynchronous_response

函数名 tuya_ble_production_test_asynchronous_response
函数原型 tuya_ble_status_t tuya_ble_production_test_asynchronous_response
(uint8_t channel,uint8_t *p_data,uint32_t len)
功能概述 产测指令异步响应函数。
参数 Channel[in] : 传输通道,0-uart;1-ble;
p_data [in] : 需要响应的完整指令数据;
len[in] : 数据长度。
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_STATE:当前状态不支持发送,例如蓝牙断开;
TUYA_BLE_ERR_INVALID_LENGTH:数据长度错误;
TUYA_BLE_ERR_NO_MEM:申请内存失败;
TUYA_BLE_ERR_NO_EVENT:其他错误。
备注 在进行产测时(产测授权是通过 uart ,整机测试通过 BLE)
有些测试项是不能立即响应结果给上位机产测工具的,
或者有些测试项需要应用程序来处理,这个时候就需要
调用该函数将测试结果发送给上位机产测工具。

tuya_ble_net_config_response

函数名 tuya_ble_net_config_response
函数原型 tuya_ble_status_t tuya_ble_net_config_response(int16_t result_code)
功能概述 Wi-Fi配网响应函数。
参数 Result_code[in] : 配网状态码;
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_STATE:当前状态不支持发送,例如蓝牙断开;
TUYA_BLE_ERR_NO_EVENT:其他错误。
备注 适用于 Wi-Fi / BLE combo设备。

tuya_ble_ubound_response

函数名 tuya_ble_ubound_response
函数原型 tuya_ble_status_t tuya_ble_ubound_response(uint8_t result_code)
功能概述 多协议combo设备解绑响应函数。
参数 Result_code[in] : 状态码, 0 - 成功,1 - 失败;
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_STATE:当前状态不支持发送,例如蓝牙断开;TUYA_BLE_ERR_NO_EVENT:其他错误。
备注 适用于 Wi-Fi / BLE 多协议combo设备。

tuya_ble_anomaly_ubound_response

函数名 tuya_ble_anomaly_ubound_response
函数原型 tuya_ble_status_t tuya_ble_anomaly_ubound_response (uint8_t result_code)
功能概述 多协议combo设备异常解绑响应函数。
参数 Result_code[in] : 状态,0 - 成功,1 - 失败;
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_STATE:当前状态不支持发送,例如蓝牙断开;
TUYA_BLE_ERR_NO_EVENT:其他错误。
备注 适用于 Wi-Fi / BLE 多协议combo设备。

tuya_ble_device_reset_response

函数名 tuya_ble_device_reset_response
函数原型 tuya_ble_status_t tuya_ble_device_reset_response (uint8_t result_code)
功能概述 多协议combo设备恢复出厂设置响应函数。
参数 Result_code[in] : 状态,0 - 成功,1 - 失败;
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_STATE:当前状态不支持发送,例如蓝牙断开;TUYA_BLE_ERR_NO_EVENT:其他错误。
备注 适用于 Wi-Fi / BLE 多协议combo设备。

tuya_ble_connect_status_get

函数名 tuya_ble_connect_status_get
函数原型 tuya_ble_connect_status_t tuya_ble_connect_status_get(void)
功能概述 获取 当前 BLE 连接状态。
参数 无。
返回值 tuya_ble_connect_status_t详见下面返回值说明。
备注

返回值说明:

typedef enum{
    UNBONDING_UNCONN = 0,   
    UNBONDING_CONN,
    BONDING_UNCONN,
    BONDING_CONN,
    BONDING_UNAUTH_CONN,  
    UNBONDING_UNAUTH_CONN,
    UNKNOW_STATUS
}tuya_ble_connect_status_t;

tuya_ble_device_factory_reset

函数名 tuya_ble_device_factory_reset
函数原型 tuya_ble_status_t tuya_ble_device_factory_reset(void)
功能概述 设备重置。
参数 无。
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INTERNAL:其他错误。
备注 对于定义外部按键重置功能的设备,按键触发重置后应用程序需要调用该函数通知tuya ble sdk 重置相关信息,例如清除绑定信息。

tuya_ble_time_req

函数名 tuya_ble_time_req
函数原型 tuya_ble_status_t tuya_ble_time_req(uint8_t time_type)
功能概述 请求云端时间。
参数 time_type[in] :
0 - 请求13 字节 ms 级字符串格式的时间;
1 - 请求年月日时分秒星期格式的时间。
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_PARAM: 参数错误;
TUYA_BLE_ERR_INTERNAL:其他错误。
备注 13字节字符串时间格式:“0000000123456” 表示123456ms时间戳;
常规时间格式:0x13,0x04,0x1C,0x0C,0x17,0x19,0x02 表示:
2019 年 4 月 28 日 12:23:25 星期二;
tuya ble sdk收到请求后会发送相应指令给 App,
sdk收到 App 返回的时间后将会以消息的方式发送给设备应用程序。

tuya_ble_ota_response

函数名 tuya_ble_ota_response
函数原型 tuya_ble_status_t tuya_ble_ota_response(tuya_ble_ota_response_t *p_data)
功能概述 OTA 响应指令。
参数 p_data[in] : OTA 响应数据。
返回值 TUYA_BLE_SUCCESS :发送成功;
TUYA_BLE_ERR_INVALID_STATE:状态错误;
TUYA_BLE_ERR_INVALID_LENGTH: 数据长度错误;
TUYA_BLE_ERR_NO_MEM: 内存申请失败;
TUYA_BLE_ERR_INTERNAL:其他错误。
备注 具体格式见 OTA 介绍章节。

tuya_ble_custom_event_send

函数名 tuya_ble_custom_event_send
函数原型 uint8_t tuya_ble_custom_event_send(tuya_ble_custom_evt_t evt)
功能概述 发送应用自定义消息及回调给 tuya ble sdk
参数 evt[in] : 自定义事件。
返回值 0 :发送成功;
1 :失败。
备注 typedef struct{
void *data;
void (*custom_event_handler)(void*data);
} tuya_ble_custom_evt_t;

说明:该函数主要用于应用程序想使用tuya ble sdk 内部的消息调度器来处理应用程序的消息事件的场景。

tuya_ble_custom_event_send 应用示例:

#define APP_CUSTOM_EVENT_1  1
#define APP_CUSTOM_EVENT_2  2
#define APP_CUSTOM_EVENT_3  3
#define APP_CUSTOM_EVENT_4  4
#define APP_CUSTOM_EVENT_5  5

typedef struct {
    uint8_t data[50];
} custom_data_type_t;

void custom_data_process(int32_t evt_id,void *data)
{
    custom_data_type_t *event_1_data;
    TUYA_BLE_LOG_DEBUG("custom event id = %d",evt_id);
    switch (evt_id)
    {
        case APP_CUSTOM_EVENT_1:
            event_1_data = (custom_data_type_t *)data;
            TUYA_BLE_LOG_HEXDUMP_DEBUG("received APP_CUSTOM_EVENT_1 data:",event_1_data->data,50);
            break;
        case APP_CUSTOM_EVENT_2:
            break;
        case APP_CUSTOM_EVENT_3:
            break;
        case APP_CUSTOM_EVENT_4:
            break;
        case APP_CUSTOM_EVENT_5:
            break;
        default:
            break;
  
    }
}

custom_data_type_t custom_data;

void custom_evt_1_send_test(uint8_t data)
{    
    tuya_ble_custom_evt_t event;
    
    for(uint8_t i=0; i<50; i++)
    {
        custom_data.data[i] = data;
    }
    event.evt_id = APP_CUSTOM_EVENT_1;
    event.custom_event_handler = (void *)custom_data_process;
    event.data = &custom_data;
    tuya_ble_custom_event_send(event);
}

tuya_ble_callback_queue_register

函数名 tuya_ble_callback_queue_register
函数原型 原型1:tuya_ble_status_t tuya_ble_callback_queue_register(void *cb_queue);
原型2:tuya_ble_status_t tuya_ble_callback_queue_register(tuya_ble_callback_t cb);
功能概述 在OS 平台架构下注册用于接收 tuya ble sdk 消息的消息队列 使用原型1;
无OS平台架构下注册用于接收tuya ble sdk 消息的回调函数 使用原型2。
参数 cb_queue [in] : 消息队列;
cb[in] :callback函数地址。
返回值 TUYA_BLE_ERR_RESOURCES: 注册失败;
TUYA_BLE_SUCCESS :注册成功。
备注

OS 平台下应用示例:

void *tuya_custom_queue_handle;

os_msg_queue_create(&tuya_custom_queue_handle, MAX_NUMBER_OF_TUYA_CUSTOM_MESSAGE, sizeof(tuya_ble_cb_evt_param_t));

tuya_ble_callback_queue_register(tuya_custom_queue_handle);

无 OS 平台下应用示例:

void tuya_cb_handler(tuya_ble_cb_evt_param_t* event);

tuya_ble_callback_queue_register(tuya_cb_handler);

tuya_ble_event_response

函数名 tuya_ble_event_response
函数原型 tuya_ble_status_t tuya_ble_event_response(tuya_ble_cb_evt_param_t *param)
功能概述 在OS 平台架构下用于响应 tuya ble sdk 的消息。
参数 param [in] : 消息指针。
返回值 TUYA_BLE_SUCCESS :成功;
其他 :失败。
备注 在OS平台架构下,应用代码处理完tuya ble sdk发送来的消息后,
必须调用此函数给tuya ble sdk反馈。

应用示例:

 /*处理ble sdk消息的应用task*/
void app_custom_task(void *p_param)
{
    tuya_ble_cb_evt_param_t event;

    while (true)
    {
        if (os_msg_recv(tuya_custom_queue_handle, &event, 0xFFFFFFFF) == true)
        {
            switch (event.evt)
            {
            case TUYA_BLE_CB_EVT_CONNECTE_STATUS:
                break;
            case TUYA_BLE_CB_EVT_DP_WRITE:            
                break;
            case TUYA_BLE_CB_EVT_DP_DATA_REPORT_RESPONSE:
                break;
            case TUYA_BLE_CB_EVT_DP_DATA_WTTH_TIME_REPORT_RESPONSE:
                break;
            case TUYA_BLE_CB_EVT_UNBOUND:
                break;
            case TUYA_BLE_CB_EVT_ANOMALY_UNBOUND:
                break;
            case TUYA_BLE_CB_EVT_DEVICE_RESET:
                break;
            case TUYA_BLE_CB_EVT_DP_QUERY:
                break;
            case TUYA_BLE_CB_EVT_OTA_DATA:
                break;
            case TUYA_BLE_CB_EVT_NETWORK_INFO:
                break;
            case TUYA_BLE_CB_EVT_WIFI_SSID:
                break;
            case TUYA_BLE_CB_EVT_TIME_STAMP:
                break;
            case TUYA_BLE_CB_EVT_TIME_NORMAL:
                break;
            case TUYA_BLE_CB_EVT_DATA_PASSTHROUGH:
                break;
            default:
                break;
            }
            
            tuya_ble_event_response(&event);
        }
    }

}

tuya_ble_scheduler_queue_size_get

函数名 tuya_ble_scheduler_queue_size_get
函数原型 uint16_t tuya_ble_scheduler_queue_size_get(void)
功能概述 在无OS平台下获取 tuya ble sdk 调度器队列总大小。
参数 无。
返回值 调度器队列总大小。
备注

tuya_ble_scheduler_queue_space_get

函数名 tuya_ble_scheduler_queue_space_get
函数原型 uint16_t tuya_ble_scheduler_queue_space_get(void)
功能概述 在无OS平台下获取 tuya ble sdk 调度器队列空闲数。
参数 无。
返回值 调度器队列空闲数。
备注

tuya_ble_scheduler_queue_events_get

函数名 tuya_ble_scheduler_queue_events_get
函数原型 uint16_t tuya_ble_scheduler_queue_events_get(void)
功能概述 在无OS平台下获取 tuya ble sdk 调度器队列未处理事件个数。
参数 无。
返回值 调度器队列未处理事件个数。
备注

tuya ble sdk callback event 介绍

tuya ble sdk 通过message(OS平台架构下)或者设备Application注册的callback函数(无OS平台架构下)向设备Application发送消息(状态、数据等),上面tuya_ble_event_response api示例代码段所示是OS平台架构下设备Application代码处理tuya ble sdk消息的典型架构,同理无OS平台架构下的芯片平台也可以使用类似的代码架构来处理消息,只是基于callback函数处理消息后不需要调用tuya_ble_event_response()响应tuya ble sdk的消息,如下所示,本章节主要介绍各种EVENT的含义。

无OS平台架构下注册 callback 函数示例:

    /*处理ble sdk消息的call back函数*/
	static void tuya_cb_handler(tuya_ble_cb_evt_param_t* event)
	{
            switch (event->evt)
            {
            case TUYA_BLE_CB_EVT_CONNECTE_STATUS:
                break;
            case TUYA_BLE_CB_EVT_DP_WRITE:            
                break;
            case TUYA_BLE_CB_EVT_DP_DATA_REPORT_RESPONSE:
                break;
            case TUYA_BLE_CB_EVT_DP_DATA_WTTH_TIME_REPORT_RESPONSE:
                break;
            case TUYA_BLE_CB_EVT_UNBOUND:
                break;
            case TUYA_BLE_CB_EVT_ANOMALY_UNBOUND:
                break;
            case TUYA_BLE_CB_EVT_DEVICE_RESET:
                break;
            case TUYA_BLE_CB_EVT_DP_QUERY:
                break;
            case TUYA_BLE_CB_EVT_OTA_DATA:
                break;
            case TUYA_BLE_CB_EVT_NETWORK_INFO:
                break;
            case TUYA_BLE_CB_EVT_WIFI_SSID:
                break;
            case TUYA_BLE_CB_EVT_TIME_STAMP:
                break;
            case TUYA_BLE_CB_EVT_TIME_NORMAL:
                break;
            case TUYA_BLE_CB_EVT_DATA_PASSTHROUGH:
                break;
            default:
                break;
            }
            
    }
    
    void tuya_ble_app_init(void)
	{
		device_param.device_id_len = 16;
		memcpy(device_param.auth_key,(void *)auth_key_test,AUTH_KEY_LEN);
		memcpy(device_param.device_id,(void *)device_id_test,DEVICE_ID_LEN);
		device_param.p_type = TUYA_BLE_PRODUCT_ID_TYPE_PID;
		device_param.product_id_len = 8;
		memcpy(device_param.product_id,APP_PRODUCT_ID,8);
		device_param.firmware_version = TY_APP_VER_NUM;
		device_param.hardware_version = TY_HARD_VER_NUM;

		tuya_ble_sdk_init(&device_param);
		tuya_ble_callback_queue_register(tuya_cb_handler);

		tuya_ota_init();
	}

TUYA_BLE_CB_EVT_CONNECTE_STATUS

Event TUYA_BLE_CB_EVT_CONNECTE_STATUS
描述 tuya ble sdk 每次状态的改变都会发送该 event 给设备Application。
备注

对应数据结构:

typedef enum{
    UNBONDING_UNCONN = 0,   
    UNBONDING_CONN,
    BONDING_UNCONN,
    BONDING_CONN,
    BONDING_UNAUTH_CONN,  
    UNBONDING_UNAUTH_CONN,
    UNKNOW_STATUS
}tuya_ble_connect_status_t;

TUYA_BLE_CB_EVT_DP_WRITE

Event TUYA_BLE_CB_EVT_DP_WRITE
描述 tuya ble sdk 接收到的手机 App 发送的 dp 点数据。
备注 格式见 tuya_ble_dp_data_report api 介绍。

对应数据结构:

/*
 * dp data  buffer:  (Dp_id,Dp_type,Dp_len,Dp_data),(Dp_id,Dp_type,Dp_len,Dp_data),....
 * */
typedef struct{
	uint8_t *p_data;
	uint16_t data_len;
}tuya_ble_dp_write_data_t;

TUYA_BLE_CB_EVT_DP_QUERY

Event TUYA_BLE_CB_EVT_DP_QUERY
描述 tuya ble sdk 接收到的手机 App 发送的要查询的 dp 点。
备注 data_len=0 表示查询所有的 dp 点,否则 p_data 指向的每一个字节代表要查询的一个 dp 点,例如 data_len=3,p_data{0x01,0x02,0x03},表示要查询 dp_id=1,dp_id=2,dp_id=3 的 3 个 dp 点数据。

对应数据结构:

/*
 * query dp point data,if data_len is 0,means query all dp point data,otherwise query the dp point in p_data buffer.
 * */
typedef struct{
	uint8_t *p_data;
	uint16_t data_len;
}tuya_ble_dp_query_data_t;

TUYA_BLE_CB_EVT_OTA_DATA

Event TUYA_BLE_CB_EVT_OTA_DATA
描述 tuya ble sdk 接收到的手机 App 发送的 OTA 数据。
备注 具体内容见 OTA 章节介绍。

对应数据结构:

typedef struct{
	tuya_ble_ota_data_type_t type;	
	uint16_t data_len;
	uint8_t *p_data;
}tuya_ble_ota_data_t;

TUYA_BLE_CB_EVT_NETWORK_INFO

Event TUYA_BLE_CB_EVT_NETWORK_INFO
描述 tuya ble sdk 接收到的手机 App 发送的 Wi-Fi 配网信息,例如:
“{“wifi_ssid”:“tuya”,“password”:“12345678”,“token”:“xxxxxxxxxx”}”
备注 只适用于 Wi-Fi / BLE 双协议combo设备。

对应数据结构:

/*
 * network data,unformatted json data,for example " {"wifi_ssid":"tuya","password":"12345678","token":"xxxxxxxxxx"} "
 * */
typedef struct{
	uint16_t data_len;//include '\0'
	uint8_t *p_data;
}tuya_ble_network_data_t;

TUYA_BLE_CB_EVT_WIFI_SSID

Event TUYA_BLE_CB_EVT_WIFI_SSID
描述 tuya ble sdk 接收到的手机 App 发送的 Wi-Fi 配网信息,例如:
“{“wifi_ssid”:“tuya”,“password”:“12345678”}”
备注 只适用于 Wi-Fi / BLE 双协议combo设备,和 TUYA_BLE_CB_EVT_NETWORK_INFO 相比缺少 token 字段,主要用于已配网设备的 Wi-Fi SSID 更新。

对应数据结构:

/*
 * wifi ssid data,unformatted json data,for example " {"wifi_ssid":"tuya","password":"12345678"} "
 * */
typedef struct{
	uint16_t data_len;//include '\0'
	uint8_t *p_data;
}tuya_ble_wifi_ssid_data_t;

TUYA_BLE_CB_EVT_TIME_STAMP

Event TUYA_BLE_CB_EVT_TIME_STAMP
描述 tuya ble sdk 收到的手机 App 发送的字符串格式的时间戳,
例如 “0000000123456” 表示 123456ms,ms 级 unix 时间戳。
备注 time_zone 时区为实际时区的 100 倍,例如 -8 区为 -800。

对应数据结构:

/*
 * uninx timestamp 
 * */
typedef struct{
	uint8_t timestamp_string[14];
	int16_t  time_zone;   //actual time zone Multiply by 100.
}tuya_ble_timestamp_data_t;

TUYA_BLE_CB_EVT_TIME_NORMAL

Event TUYA_BLE_CB_EVT_TIME_NORMAL
描述 tuya ble sdk 收到的手机 App 发送的常规格式的时间,例如 :
正 8 区,2019 年 4 月 28 日 12:23:25 星期二,
对应数据:0x13 ,0x04 ,0x1C ,0x0C ,0x17 ,0x19 ,0x02(星期),0x0320(时区time_zone)。
备注

对应数据结构:

/*
 * normal time formatted
 * */
typedef struct{
	uint16_t nYear;
    uint8_t nMonth;
    uint8_t nDay;
    uint8_t nHour;
    uint8_t nMin;
    uint8_t nSec;
    uint8_t DayIndex; /* 0 = Sunday */
	int16_t time_zone;   //actual time zone Multiply by 100.
}tuya_ble_time_noraml_data_t;

TUYA_BLE_CB_EVT_DATA_PASSTHROUGH

Event TUYA_BLE_CB_EVT_DATA_PASSTHROUGH
描述 tuya ble sdk 收到的手机 App 发送的透传数据。
备注 tuya ble sdk 不解析透传的数据,数据格式由设备Application和手机 App 协商定义。

对应数据结构:

typedef struct{
	uint16_t data_len;
	uint8_t *p_data;
}tuya_ble_passthrough_data_t;

TUYA_BLE_CB_EVT_DP_DATA_REPORT_RESPONSE

Event TUYA_BLE_CB_EVT_DP_DATA_REPORT_RESPONSE
描述 设备Application调用 tuya_ble_dp_data_report() 函数发送完 dp 点数据后,如果需要确认是否发送成功,就需要等待该 Event,status=0 表示成功,其他失败,设备Application如果等待发送结果需要加入超时机制,不能无限制等待。
备注

对应数据结构:

typedef struct{
	uint8_t status;
}tuya_ble_dp_data_report_response_t;

TUYA_BLE_CB_EVT_DP_DATA_WTTH_TIME_REPORT_RESPONSE

Event TUYA_BLE_CB_EVT_DP_DATA_WTTH_TIME_REPORT_RESPONSE
描述 设备Application调用tuya_ble_dp_data_with_time_report ()tuya_ble_dp_data_with_time_ms_string_report() 函数发送完带时间戳 dp 点数据后,如果需要确认是否发送成功,就需要等待该 Event,status=0 表示成功,其他失败,设备Application如果等待发送结果必须加入超时机制,不能无限制等待。
备注

对应数据结构:

typedef struct{
	uint8_t status;
}tuya_ble_dp_data_with_time_report_response_t;

TUYA_BLE_CB_EVT_DP_DATA_WITH_FLAG_REPORT_RESPONSE

Event TUYA_BLE_CB_EVT_DP_DATA_WITH_FLAG_REPORT_RESPONSE
描述 设备Application调用 tuya_ble_dp_data_with_flag_report() 函数发送完 dp 点数据后,如果需要确认是否发送成功,就需要等待该 Event;
sn 和 mode 是设备Application发送dp点数据是传入的序号和模式;
status=0 表示成功,其他失败。
备注 设备Application 可以根据序号 sn 判断是哪次发送的dp点数据的响应结果。

对应数据结构:

typedef struct{
    uint16_t sn;
    tuya_ble_report_mode_t mode;
	uint8_t status;
}tuya_ble_dp_data_with_flag_report_response_t;

TUYA_BLE_CB_EVT_DP_DATA_WITH_FLAG_AND_TIME_REPORT_RESPONSE

Event TUYA_BLE_CB_EVT_DP_DATA_WITH_FLAG_AND_TIME_REPORT_RESPONSE
描述 设备Application调用tuya_ble_dp_data_with_flag_and_time_report()tuya_ble_dp_data_with_flag_and_time_ms_string_report() 函数发送完带标志和时间戳 dp 点数据后,如果需要确认是否发送成功,就需要等待该 Event;
sn 和 mode 是设备Application发送dp点数据是传入的序号和模式;
status=0 表示成功,其他失败。
备注 设备Application 可以根据序号 sn 判断是哪次发送的dp点数据的响应结果。

对应数据结构:

typedef struct{
    uint16_t sn;
    tuya_ble_report_mode_t mode;
	uint8_t status;
}tuya_ble_dp_data_with_flag_and_time_report_response_t;

TUYA_BLE_CB_EVT_UNBOUND

Event TUYA_BLE_CB_EVT_UNBOUND
描述 收到该 Event 表示手机 App 发送了解绑指令,其中 data 字段是保留字段,暂不使用。
备注

对应数据结构:

typedef struct{
	uint8_t data;
}tuya_ble_unbound_data_t;

TUYA_BLE_CB_EVT_ANOMALY_UNBOUND

Event TUYA_BLE_CB_EVT_ANOMALY_UNBOUND
描述 收到该 Event 表示手机 App 发送了异常解绑指令,其中 data 字段是保留字段,暂不使用。
备注

对应数据结构:

typedef struct{
	uint8_t data;
}tuya_ble_anomaly_unbound_data_t;

TUYA_BLE_CB_EVT_DEVICE_RESET

Event TUYA_BLE_CB_EVT_DEVICE_RESET
描述 收到该 Event 表示手机 App 发送了重置指令,其中 data 字段是保留字段,暂不使用。
备注 设备Application 收到重置 Event后,需要执行重置功能定义的一些操作。

对应数据结构:

typedef struct{
	uint8_t data;
}tuya_ble_device_reset_data_t;

TUYA_BLE_CB_EVT_UPDATE_LOGIN_KEY_VID

Event TUYA_BLE_CB_EVT_UPDATE_LOGIN_KEY_VID
描述 设备注册绑定成功后,手机 App 会发送设备的 login key 和虚拟 ID 给设备。
备注 单 BLE 设备无需处理该信息。

对应数据结构:

typedef struct{
    uint8_t login_key_len;
    uint8_t vid_len;
	uint8_t login_key[LOGIN_KEY_LEN];
	uint8_t vid[DEVICE_VIRTUAL_ID_LEN];
}tuya_ble_login_key_vid_data_t;

移植示例

本章节以 nrf52832 为例,介绍无 OS 平台架构下的移植步骤,其他平台类似, nrf52832完整示例 demo 及其他平台的示例 demo 请联系 Tuya 项目对接人获取。

nrf52832移植示例

1、 下载 nrf52832 芯片原厂 SDK(以 nRF5_SDK_15.2.0_9412b96 为例说明),并准备一个 nrf52832 开发板。

2、 解压下载好的原厂 SDK 至某个自定义目录,如下图 所示。

BLE SDK 说明

3、 进入 examples 文件夹下的 ble_peripheral 目录,如下图 所示。

BLE SDK 说明

4、 ble_peripheral 文件夹提供多个蓝牙 BLE 从机的应用实例,我们以 ble_app_uart 为模板新建一个项目,拷贝 ble_app_uart 目录并命名为 tuya_ble_standard_nordic(也可以命名为其他名字),如图所示。

BLE SDK 说明

5、 打开工程(工程位置:pca10040/s132)并编译,先确认能编译通过并能在开发板上正确运行。

6、 以 nRF_BLE_Services 文件夹下的 ble_nus.c 文件为模板新建tuya_ble_service.c文件,并修改此文件下的代码,实现涂鸦要求的 tuya ble service,修改 main.c 文件中广播相关的配置参数,使设备按照之前介绍的广播内容广播。

7、 编译并下载到开发板运行,用第三方手机蓝牙扫描 App(例如 iOS下的 lightBlue)扫描设备,扫描到后检查广播内容和 service 是否满足要求。

8、 下载 tuya ble sdk 并放到新建项目的目录下,如下图所示,并将相关源文件添加到工程中编译一次。

BLE SDK 说明

9、 添加好的工程目录如图 所示,注意选择正确的库文件。

BLE SDK 说明

10、新建一个 custom_tuya_ble_config.h 文件,并放到工程目录中,本示例放在了 tuya_ble_app 目录下。custom_tuya_ble_config.h 文件里的配置项根据实际需求和环境配置,tuya ble sdk 提供了一些芯片平台的参考,参考配置文件放在 tuya ble sdk 文件夹里 port 目录下的各平台目录下。

11、将 custom_tuya_ble_config.h 文件名字赋值给 CUSTOMIZED_TUYA_BLE_CONFIG_FILE,并添加到工程的宏定义中,如图所示。

BLE SDK 说明

12、新建所用芯片平台对应的 port 文件,例如命名为 tuya_ble_port_nrf52832.h tuya_ble_port_nrf52832.c,在新建的 port 文件中按需配置,实现 tuya_ble_port.h 中所列的接口(并不需要实现全部的接口,例如本示例 demo 没有使用 OS,所以不需要实现 os 相关接口,本示例配置为使用 tuya ble sdk 的内部内存管理模块,所以也不需要实现内存分配和释放接口),tuya ble sdk 里 port 文件下的各平台名字命名的文件夹里有对应平台的参考移植实现文件。

13、编写完 port 文件后,在 custom_tuya_ble_config.h 配置文件中添加相应的 port 文件定义,如图 7-7 所示。

BLE SDK 说明

14、编译一次,如果编译不过,先检查代码优化错误。

15、接下来是 tuya ble sdk 的初始化,本示例专门新建了一个文件用于处理 tuya ble sdk 的初始化、注册回调函数以及处理 sdk的回调消息等,如下图所示。

BLE SDK 说明

16、在 tuya IoT 工作台注册产品并将生成的 product id 拷贝至工程代码中,如下图所示,其中 APP_PRODUCT_IDAPP_BUILD_FIRMNAMETY_APP_VER_NUMTY_APP_VER_STRTY_HARD_VER_NUMTY_HARD_VER_STR 宏定义名字不可改变,“xxxxxxxx” 替换为 IoT 工作台注册的产品 id,tuya_ble_sdk_app_demo_nrf52832 替换为在 Tuya BackendNG 管理后台上创建的项目名称(如果需要使用 Tuya 的产测工具授权,必须联系 Tuya 负责对接的同事在 Tuya BackendNG 管理后台创建项目)。

BLE SDK 说明

17、在工程的相应的位置上分别调用 tuya_ble_app_init()tuya_ble_main_tasks_exec()tuya_ble_gatt_receive_data()tuya_ble_common_uart_receive_data()tuya_ble_disconnected_handler()tuya_ble_connected_handler(),如下所示。

BLE SDK 说明

18、开发调试阶段, auth_key_testdevice_id_test 联系 tuya 负责对接的产品经理获取。

19、最后编译代码,下载进开发板执行,下载涂鸦智能 App,扫描添加设备即可联调。

其他平台移植示例

请参考各平台完整示例 demo.

OTA协议及接口介绍

固件升级和芯片平台架构关联性比较大,所以 tuya ble sdk 只提供固件升级接口,Application 只需通过 tuya ble sdk提供的 OTA 通信接口按照如下所述的 OTA 协议实现即可。

Application通过注册的回调函数(无 OS 环境下)或者注册的接收队列(OS 环境下)接收OTA数据,EVENT ID为TUYA_BLE_CB_EVT_OTA_DATA,数据格式见升级协议章节,OTA 响应数据通过tuya_ble_ota_response(tuya_ble_ota_response_t *p_data) 函数发送。

OTA升级流程

BLE SDK 说明

OTA升级协议

OTA相关数据结构

typedef enum 

{

​    TUYA_BLE_OTA_REQ,      //OTA升级请求指令

​    TUYA_BLE_OTA_FILE_INFO,    //OTA升级文件信息指令

​    TUYA_BLE_OTA_FILE_OFFSET_REQ, //OTA升级文件偏移量指令

​    TUYA_BLE_OTA_DATA,      //OTA升级数据指令

​    TUYA_BLE_OTA_END,      //OTA升级结束指令

​    TUYA_BLE_OTA_UNKONWN,

}tuya_ble_ota_data_type_t;

typedef struct{tuya_ble_ota_data_type_t type;  

​    uint16_t data_len;

​    uint8_t *p_data;

}tuya_ble_ota_data_t;   //手机App发送OTA升级EVENT(TUYA_BLE_CB_EVT_OTA_DATA)对应的数据结构。

typedef struct{tuya_ble_ota_data_type_t type;  

​    uint16_t data_len;

​    uint8_t *p_data;

}tuya_ble_ota_response_t;  // OTA响应数据发送函数tuya_ble_ota_response(tuya_ble_ota_response_t *p_data) 对应的数据结构

OTA升级请求(TUYA_BLE_OTA_REQ

App>设备:

data_len=1
长度: 1 字节
Data: 固定 0

设备->App:

data_len=9
长度: 1 字节 1 字节 1 字节 4 字节 2 字节
data: Flag OTA_Version 0 Version四字节 最大包长度

参数说明:

Flag:0x00 - 允许升级,0x01 - 拒绝升级。

OTA_Version:OTA 协议大版本,例如 0x03 代表 3.X 的协议版本。

Version:当前固件版本号,大端格式,例如 0x00 01 00 02 代表版本为 V1.0.2。

最大包长度:设备允许的单包最大长度,单位字节,当前版本不要超过 512字节。

OTA升级文件信息(TUYA_BLE_OTA_FILE_INFO

App->设备:

data_len=37
长度: 1 字节 8 字节 4 字节 16 字节 4 字节 4 字节
Data: 0 产品 PID 文件版本 文件 MD5 文件长度 CRC32

文件版本:例如,0x00010002代表版本为V1.0.2。

设备->App:

data_len=26
长度: 1 字节 1 字节 4 字节 4 字节 16 字节
data: 0 STATE 已储存文件长度 已储存文件CRC32 已储存文件 MD5(目前不使用)

参数说明:

STATE:

0x00:正常升级

0x01:产品 PID 不一致

0x02: 文件版本低于或者等于当前版本

0x03:文件大小超过范围。

其他:保留。

已储存文件信息:

为了支持断点续传,这里会返回设备端已经储存的文件信息,App 在收到后,首先根据设备返回的已储存文件长度计算新文件对应长度的 CRC32,然后和设备返回的 CRC32 对比,如果两者都吻合,那么在下面的文件起始传输请求中将起始传输偏移量改为该长度值,否则文件起始传输偏移量改为 0,表示从头开始传输。

OTA升级文件偏移(TUYA_BLE_OTA_FILE_OFFSET_REQ

App->设备:

data_len=5
长度: 1 字节 4 字节
Data: 0 Offset

offset:升级文件偏移量。

设备->App:

data_len=5
长度: 1 字节 4 字节
Data: 0 Offset

offset:设备要求的起始传输文件偏移量。

实际文件传输的偏移地址应该以设备端要求的为准,且设备端要求的地址会小于等于 App端给出的偏移。

OTA升级数据(TUYA_BLE_OTA_DATA

App->设备:

data_len=7+n
长度: 1 字节 2 字节 2 字节 2 字节 n 字节
Data: 0 包号 当前包数据长度 n 当前包数据 CRC16 当前包数据

包号从 0 开始,高字节在前。

设备->App:

data_len=2
长度: 1 字节 1 字节
Data: 0 STATE

STATE:

0x00:成功

0x01:包号异常

0x02:长度不一致。

0x03:crc检验失败

0x04:其它

OTA升级结束(TUYA_BLE_OTA_END

App->设备:

data_len=1
长度: 1 字节
Data: 0

设备->App:

data_len=2
长度: 1 字节 1 字节
Data: 0 STATE

STATE:

0x00:成功

0x01:数据总长度错误

0x02: 数据总 CRC 检验失败

0x03: 其它

设备 OTA 文件验证成功后如果需要重启,通过调用api tuya_ble_ota_response(tuya_ble_ota_response_t *p_data) 响应给 App 结果至少无阻塞延时 2 秒后再重启。

OTA升级接口

Application通过注册的回调函数(无 RTOS 环境下)或者注册的接收队列(RTOS 环境下)接收OTA 数据,EVENT ID 为 TUYA_BLE_CB_EVT_OTA_DATA,OTA 响应数据通过tuya_ble_ota_response(tuya_ble_ota_response_t *p_data) 函数发送。

如下所示,Application 在此处调用自定义的 OTA 处理函数。

BLE SDK 说明

OTA 处理函数参考:

void tuya_ota_proc(uint16_t cmd,uint8_t*recv_data,uint32_t recv_len)
{
    TUYA_BLE_LOG_DEBUG("ota cmd : 0x%04x , recv_len : %d",cmd,recv_len);
    switch(cmd)
    {
    case TUYA_BLE_OTA_REQ:
        tuya_ota_start_req(recv_data,recv_len);
        break;
    case TUYA_BLE_OTA_FILE_INFO:
        tuya_ota_file_info_req(recv_data,recv_len);
        break;
    case TUYA_BLE_OTA_FILE_OFFSET_REQ:
        tuya_ota_offset_req(recv_data,recv_len);
        break;
    case TUYA_BLE_OTA_DATA:
        tuya_ota_data_req(recv_data,recv_len);
        break;
    case TUYA_BLE_OTA_END:
        tuya_ota_end_req(recv_data,recv_len);
        break;
    default:
        break;
    }

}

产测接口介绍

蓝牙BLE 设备接入涂鸦 IoT 平台需要预先烧录授权信息(license)(一个设备一组license),一般在工厂生产时烧录,客户可以使用涂鸦的产测工具进行烧录授权以及测试,当然也可以批量购买 license 使用自定义的协议和接口管理,如果使用自定义的接口管理授权信息,需要在初始化 tuya ble sdk是传入授权信息(auth key和device id),如下所示:


tuya_ble_device_param_t device_param ;

void tuya_ble_app_init(void)
{
    memset(&device_param,0,sizeof(tuya_ble_device_param_t));
    device_param.device_id_len = 16;
    memcpy(device_param.auth_key,(void *)auth_key_test,AUTH_KEY_LEN);
    memcpy(device_param.device_id,(void *)device_id_test,DEVICE_ID_LEN);
    device_param.p_type = TUYA_BLE_PRODUCT_ID_TYPE_PID;
    device_param.product_id_len = 8;
    memcpy(device_param.product_id,APP_PRODUCT_ID,8);
    device_param.firmware_version = TY_APP_VER_NUM;
    device_param.hardware_version = TY_HARD_VER_NUM;

    tuya_ble_sdk_init(&device_param);
    tuya_ble_callback_queue_register(tuya_cb_handler);

    tuya_ota_init();

    TUYA_APP_LOG_INFO("app version : "TY_APP_VER_STR);
     
}

如果使用涂鸦产测授权工具烧录授权信息,并且需要tuya ble sdk自管理授权信息,那么在初始化tuya ble sdk时不需要传入授权信息,如下所示:

tuya_ble_device_param_t device_param ;

void tuya_ble_app_init(void)
{
    memset(&device_param,0,sizeof(tuya_ble_device_param_t));
    device_param.device_id_len = 0;
    device_param.p_type = TUYA_BLE_PRODUCT_ID_TYPE_PID;
    device_param.product_id_len = 8;
    memcpy(device_param.product_id,APP_PRODUCT_ID,8);
    device_param.firmware_version = TY_APP_VER_NUM;
    device_param.hardware_version = TY_HARD_VER_NUM;

    tuya_ble_sdk_init(&device_param);
    tuya_ble_callback_queue_register(tuya_cb_handler);

    tuya_ota_init();

    TUYA_APP_LOG_INFO("app version : "TY_APP_VER_STR);
     
}

如果使用涂鸦产测授权工具烧录授权信息,要确保TUYA_BLE_DEVICE_AUTH_SELF_MANAGEMENT 已开启。

#define TUYA_BLE_DEVICE_AUTH_SELF_MANAGEMENT 1

产测分为 “通用产测授权“ 和 “通用整机产测”,“通用整机产测协议” 是 “通用产测授权协议” 的子集,“通用产测授权“ 主要包括烧录授权信息、GPIO测试以及RSSI测试;通用整机产测包含产品定制附加的一些测试,具体协议格式请参考《蓝牙通用产测授权协议》和《蓝牙通用整机产测协议》。

tuya ble sdk 已实现了“通用产测授权”的协议,但是像 RSSI 测试(被测设备扫描特定的 beacon 信标)和 GPIO 测试以及基于“通用整机产测协议”的产品附加项目测试,需要根据此产品测试需求,按需实现对应接口。tuya ble sdk 中 tuya_ble_app_production_test.c 源文件中已对这几项测试预留了对应函数接口,都是以__TUYA_BLE_WEAK定义的弱实现,客户应用程序只需要在其他的源文件中(例如新建custom_app_product_test.c)重新定义这几个函数即可,并在自定义配置文件中引用。

如下图所示:

BLE SDK 说明

BLE SDK 说明

BLE SDK 说明