Initial commit: TXW82x FPV v2.7.0.7-42229 SDK + project sources

This commit is contained in:
2026-07-06 11:30:13 +08:00
commit e76462eeb7
3451 changed files with 1415300 additions and 0 deletions

View File

@@ -0,0 +1,156 @@
#include "lib/bluetooth/hci/hci_host.h"
#include "lib/bluetooth/uble/ble_adv.h"
struct ble_rx_ctrl *ble_ctrl;
const uint8 adv_identify_info[ADV_IDENTIFY_SET_LEN] = {0x9f, 0x1a, 0x41, 0x10, 0x7d, 0xfd, 0xf0, 0x73}; // 识别头信息
const uint8 adv_addr[6] = {0x70, 0xaf, 0x1e, 0x7f, 0x0e, 0x7e}; // 广播地址
extern void ble_adv_parse_param(uint8 *data, uint32 len);
static int32 ble_adv_parse_data(uint8 *data, int32 len)
{
if(len <= ADV_DATA_MAX_LEN) {
memcpy(&ble_ctrl->adv_info, data, len);
}else {
return 0;
}
if(ble_ctrl->adv_info.header_info.pdu_type != ADV_DISCOVER_TYPE) {
return 0;
}
if(ble_ctrl->adv_info.payload_info.manufacturer_id != ADV_MANUFACTURER_ID) {
return 0;
}
#if (ADV_IDENTIFY_SET_LEN > 0 && ADV_IDENTIFY_SET_LEN < ADV_IDENTIFY_MAX_LEN)
if(memcmp(ble_ctrl->adv_info.payload_info.identify_info, adv_identify_info, ADV_IDENTIFY_SET_LEN) != 0) {
return 0;
}
#endif
//dump_hex("ble_adv_parse_data adv_data:\r\n", data, len, 1);
//接收超时
if(os_jiffies_to_msecs(os_jiffies())-ble_ctrl->adv_pro.cur_tick >= ble_ctrl->adv_pro.rec_overtime) {
if(ble_ctrl->adv_info.payload_info.section_idx == 0) {
ble_ctrl->adv_pro.cur_tick = os_jiffies_to_msecs(os_jiffies());
ble_ctrl->adv_pro.section_num = ble_ctrl->adv_info.payload_info.section_num;
ble_ctrl->adv_pro.rec_overtime = (ble_ctrl->adv_pro.section_num+1)*1000; //ms
ble_ctrl->adv_pro.section_idx = 0;
ble_ctrl->adv_pro.byte_offset_len = 0;
ble_ctrl->adv_pro.len = 0;
ble_ctrl->adv_pro.start_flag = 1;
}else {
ble_ctrl->adv_pro.start_flag = 0;
os_printf(KERN_ERR"%s: adv recv timeout!\r\n", __func__);
}
}
if((ble_ctrl->adv_pro.start_flag == 1) && (ble_ctrl->adv_info.payload_info.section_num == ble_ctrl->adv_pro.section_num)) {
if(ble_ctrl->adv_info.payload_info.section_idx == ble_ctrl->adv_pro.section_idx) {
memcpy(ble_ctrl->adv_pro.data + ble_ctrl->adv_pro.byte_offset_len, ble_ctrl->adv_info.payload_info.data, ble_ctrl->adv_info.payload_info.byte_len);
ble_ctrl->adv_pro.byte_offset_len += ble_ctrl->adv_info.payload_info.byte_len;
++ble_ctrl->adv_pro.section_idx;
}
if(ble_ctrl->adv_info.payload_info.section_num == ble_ctrl->adv_pro.section_idx) {
ble_ctrl->adv_pro.len = ble_ctrl->adv_pro.byte_offset_len;
ble_ctrl->adv_pro.start_flag = 0;
return 1;
}
}
return 0;
}
static int32 ble_adv_get_data(uint8 **data)
{
if(ble_ctrl->adv_pro.data == NULL) {
return 0;
}else {
*data = ble_ctrl->adv_pro.data;
return ble_ctrl->adv_pro.len;
}
}
int32 ble_adv_rx_data(uint8 *data, uint32 len)
{
uint8 *ncdata = NULL;
uint32 data_len = 0;
//dump_hex("ble adv rx adv_data:\r\n", data, len, 1);
if (ble_adv_parse_data(data, len)) {
data_len = ble_adv_get_data(&ncdata);
if (data_len && ncdata) {
ble_adv_parse_param(ncdata, data_len);
}
}
return RET_OK;
}
int32 ble_adv_tx_data(uint8 *data, int32 len)
{
uint8 *buff = NULL;
uint8 section_num = 0;
uint8 section_idx = 0;
uint16 last_section_len = 0;
uint16 start_pos = 0;
uint16 cur_section_len = 0;
struct ble_adv_info adv_info;
if((data == NULL) || (len <= 0) || (len > 250 * ADV_MAX_SECTION_LEN)) { //section_num:1byte(255)
return RET_ERR;
}
section_num = (len + ADV_MAX_SECTION_LEN - 1) / ADV_MAX_SECTION_LEN;
last_section_len = len - (section_num - 1) * ADV_MAX_SECTION_LEN;
adv_info.header_info.pdu_type = ADV_DISCOVER_TYPE;
adv_info.header_info.tx_add = 1;
adv_info.header_info.rx_add = 0;
memcpy(adv_info.payload_info.addr, adv_addr, 6);
adv_info.payload_info.ad_type = 0xFF;
adv_info.payload_info.manufacturer_id = ADV_MANUFACTURER_ID;
memcpy(adv_info.payload_info.identify_info, adv_identify_info, ADV_IDENTIFY_SET_LEN);
while(1) {
start_pos = section_idx * ADV_MAX_SECTION_LEN;
if(section_idx < section_num - 1) {
cur_section_len = ADV_MAX_SECTION_LEN;
}else {
cur_section_len = last_section_len;
}
adv_info.header_info.length = 6 + 4 + ADV_IDENTIFY_SET_LEN + 3 + cur_section_len; //addr+(1E FF 04 41)+ADV_IDENTIFY_SET_LEN+section_num+section_idx+byte_len+data
adv_info.payload_info.ad_len = 3 + ADV_IDENTIFY_SET_LEN + 3 + cur_section_len; //(FF 04 41)+ADV_IDENTIFY_SET_LEN+section_num+section_idx+byte_len+data
adv_info.payload_info.section_num = section_num;
adv_info.payload_info.section_idx = section_idx;
adv_info.payload_info.byte_len = cur_section_len;
memcpy(adv_info.payload_info.data, data + start_pos, cur_section_len);
buff = os_malloc(adv_info.header_info.length + 2 + 1);
if (buff) {
return RET_ERR;
}
os_memcpy(buff+1, &adv_info, adv_info.header_info.length + 2);
if (bt_hci_tx_adv_data(buff, adv_info.header_info.length + 2)) {
os_free(buff);
return RET_ERR;
}
os_free(buff);
section_idx = section_idx + 1;
if(section_idx >= section_num) {
break;
}
}
return RET_OK;
}
int32 ble_adv_ctrl_create(void)
{
if (ble_ctrl == NULL) {
ble_ctrl = (struct ble_rx_ctrl *)os_zalloc(sizeof(struct ble_rx_ctrl));
}
return RET_OK;
}
int32 ble_adv_ctrl_destory(void)
{
if (ble_ctrl) {
os_free(ble_ctrl);
}
return RET_OK;
}

View File

@@ -0,0 +1,268 @@
/**
******************************************************************************
* @file sdk\lib\bluetooth\uble
* @author TAIXIN-SEMI Application Team
* @version V2.0.0
* @date 03-07-2025
* @brief This file contains three BLE configuration network features.
******************************************************************************
* @attention
*
* COPYRIGHT 2023 TAIXIN-SEMI
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "lib/bluetooth/uble/ble_demo.h"
#include "lib/umac/ieee80211.h"
#include "syscfg.h"
#if BLE_SUPPORT
/**
* @brief This function is a response callback function that handles ATT requests.
*
* @param entry
* @param read ATT read request
* @param buff ATT write request
* @param size buffer size
* @param offset read/write offset.
* @return int32
*/
static int32 uble_test_hdlval(const struct uble_value_entry *entry, uint8 read, uint8 *buff, int32 size, uint32 offset)
{
char *ptr1, *ptr2;
uint8 temp = 1;
if (read) { /*get value*/
//buff: used to store response data.
//size: buff's size.
os_printf("BLE READ: offset = %d\r\n", offset);
os_memset(buff, 'U', size);
//return response len.
return offset == 0 ? size : 4;
} else { /*set value*/
dump_hex("BLE WRITE: ", buff, size, 0);
//os_printf("BLE WRITE: buf=%s, size = %d\r\n", buff, size);
if (buff[0] == ':') {
ptr1 = os_strchr(buff, ',');
if (ptr1 == NULL) { return UBLE_ATT_ERR_REQ_NOT_SUPPORTED; }
*ptr1++ = 0;
os_strcpy(sys_cfgs.ssid, buff + 1);
os_printf("SET ssid:%s\r\n", buff + 1);
ptr2 = os_strchr(ptr1, ',');
if (ptr2 == NULL) { return UBLE_ATT_ERR_REQ_NOT_SUPPORTED; }
*ptr2++ = 0;
os_strcpy(sys_cfgs.passwd, ptr1);
os_printf("SET passwd:%s\r\n", ptr1);
if (ptr2[0] == '0') {
sys_cfgs.key_mgmt = WPA_KEY_MGMT_NONE;
} else if (ptr2[0] == '1') {
sys_cfgs.key_mgmt = WPA_KEY_MGMT_PSK;
} else if (ptr2[0] == '2') {
sys_cfgs.key_mgmt = WPA_KEY_MGMT_SAE;
} else if (ptr2[0] == '3') {
sys_cfgs.key_mgmt = WPA_KEY_MGMT_OWE;
} else {
temp = 0;
}
if (temp) {
os_printf("SET keymgmt:%c\r\n", ptr2[0]);
} else {
os_printf("Unsupported keymgmt:%c\r\n", ptr2[0]);
}
syscfg_dump();
SYSEVT_NEW_BLE_EVT(SYSEVT_BLE_NETWORK_CONFIGURED, 0);
}
return 0;
}
}
/**
* @brief define BLE value table
*/
static const struct uble_value_entry uble_demo_values[] = {
{.type = UBLE_VALUE_TYPE_HDL, .value = uble_test_hdlval, .size = 0, .bitoff = 0, .maskbit = 0,},
{.type = UBLE_VALUE_TYPE_STRING, .value = sys_cfgs.ssid, .size = 21, .bitoff = 0, .maskbit = 0,},
{.type = UBLE_VALUE_TYPE_STRING, .value = sys_cfgs.passwd, .size = 21, .bitoff = 0, .maskbit = 0,},
};
#if BLE_UUID_128
/*360 service*/
#define BLE_360_SERVICE {0x06,0x05,0x04,0x03,0x02,0x01,0x03,0x00,0x02,0x00,0x01,0x00,0x01,0x00,0x00,0x36}
#define BLE_360_WR_UUID {0x06,0x05,0x04,0x03,0x02,0x01,0x03,0x00,0x02,0x00,0x01,0x00,0x02,0x00,0x00,0x36}
#define BLE_360_RD_UUID {0x06,0x05,0x04,0x03,0x02,0x01,0x03,0x00,0x02,0x00,0x01,0x00,0x03,0x00,0x00,0x36}
#define BLE_360_CCCD_UUID {0xfb,0x34,0x9b,0x5f,0x80,0x00,0x00,0x80,0x00,0x10,0x00,0x00,0x02,0x29,0x00,0x00}
#define BLE_360_properties1 UBLE_GATT_CHARAC_WRITE|UBLE_GATT_CHARAC_WRITE_WITHOUT_RESPONSE
#define BLE_360_properties2 UBLE_GATT_CHARAC_READ|UBLE_GATT_CHARAC_NOTIFY
#endif
#define BLE_TUYA_properties1 UBLE_GATT_CHARAC_WRITE_WITHOUT_RESPONSE|UBLE_GATT_CHARAC_READ
/**
* @brief ATT service
*/
static const struct uble_gatt_data uble_demo_att_table[] = {
/* Generic Access service ***************************************************/
{ .att_type=0x2800, .properties=0, .att_value=0x1800},
/* Characteristic: *Device Name *********************************************/
{ .att_type=0x2803, .properties=UBLE_GATT_CHARAC_READ, .att_value=0x2A00},
{ .att_type=0x2A00, .properties=0, .att_value=0},
/* Characteristic: Appearance ***********************************************/
{ .att_type=0x2803, .properties=UBLE_GATT_CHARAC_READ, .att_value=0x2A01},
{ .att_type=0x2A01, .properties=0, .att_value=0},
#if BLE_UUID_128
/* 360 service: 360 service ******************************************************************/
{ .att_type=0x2800, .properties=0, .att_value_128=BLE_360_SERVICE},
/* Characteristic: app write ******************************************************************/
{ .att_type=0x2803, .properties=BLE_360_properties1, .att_value_128=BLE_360_WR_UUID},
{ .att_type_128=BLE_360_WR_UUID, .properties=0, .att_value=(uint32)&uble_demo_values[0]},
/* Characteristic: read, notify ****************************************************************/
{ .att_type=0x2803, .properties=BLE_360_properties2, .att_value_128= BLE_360_RD_UUID},
{ .att_type_128=BLE_360_RD_UUID, .properties=0, .att_value=0},
{ .att_type=0x2902, .properties=0, .att_value=0}, /*Characteristic CCCD*/
#else
/* Tuya service ************************************************************************/
{ .att_type=0x2800, .properties=0, .att_value=0x1910},
/* Characteristic: app write ***********************************************************/
{ .att_type=0x2803, .properties=BLE_TUYA_properties1, .att_value=0x2b11},
{ .att_type=0x2b11, .properties=0, .att_value=(uint32)&uble_demo_values[0]},
/* Characteristic: notify **************************************************************/
{ .att_type=0x2803, .properties=UBLE_GATT_CHARAC_NOTIFY, .att_value=0x2b10},
{ .att_type=0x2b10, .properties=0, .att_value=0},
{ .att_type=0x2902, .properties=0, .att_value=0}, /*Characteristic CCCD*/
#endif
/* Characteristic: ssid (read) **************************************************************/
{ .att_type=0x2803, .properties=UBLE_GATT_CHARAC_READ, .att_value=0x2b12},
{ .att_type=0x2b12, .properties=0, .att_value=(uint32)&uble_demo_values[1]},
/* Characteristic: password (read) **********************************************************/
{ .att_type=0x2803, .properties=UBLE_GATT_CHARAC_READ, .att_value=0x2b13},
{ .att_type=0x2b13, .properties=0, .att_value=(uint32)&uble_demo_values[2]},
};
/**
* @brief This function is used to process data obtained from mode 1(Broadcast Configuration Network).
*
* @param data buffer
* @param len buffer size
*/
void ble_adv_parse_param(uint8 *data, uint32 len)
{
uint8 *ptr = data;
uint8 temp = 1;
extern struct sys_config sys_cfgs;
#if 1 //sample code
uint8 buff[33];
while (ptr < data + len) {
switch (ptr[0]) {
case 1: //SSID
os_memset(buff, 0, sizeof(buff));
os_memcpy(buff, ptr + 2, ptr[1]);
os_printf("SET ssid:%s\r\n", buff);
os_memcpy(sys_cfgs.ssid, buff, sizeof(buff));
break;
case 2: //PassWord
os_memset(buff, 0, sizeof(buff));
os_memcpy(buff, ptr + 2, ptr[1]);
os_printf("SET passwd:%s\r\n", buff);
os_memcpy(sys_cfgs.passwd, buff, sizeof(buff));
break;
case 3: //Keymgmt
os_memset(buff, 0, sizeof(buff));
os_memcpy(buff, ptr + 2, ptr[1]);
if (os_strncasecmp(buff, "NONE", 4) == 0) {
sys_cfgs.key_mgmt = WPA_KEY_MGMT_NONE;
} else if (os_strncasecmp(buff, "WPA-PSK", 8) == 0) {
sys_cfgs.key_mgmt = WPA_KEY_MGMT_PSK;
} else if (os_strncasecmp(buff, "SAE", 3) == 0) {
sys_cfgs.key_mgmt = WPA_KEY_MGMT_SAE;
} else if (os_strncasecmp(buff, "OWE", 3) == 0) {
sys_cfgs.key_mgmt = WPA_KEY_MGMT_OWE;
} else {
temp = 0;
}
if (temp) {
os_printf("SET keymgmt:%s\r\n", buff);
} else {
os_printf("Unsupported keymgmt:%s\r\n", buff);
}
break;
case 4: //auth
os_printf("AUTH %d\r\n", ptr[2]);
break;
default:
os_printf("Unsupport ID:%d\r\n", ptr[0]);
break;
}
ptr += (ptr[1] + 2);
}
syscfg_dump();
SYSEVT_NEW_BLE_EVT(SYSEVT_BLE_NETWORK_CONFIGURED, 0);
#endif
}
int32 ble_demo_init(void)
{
uint8 scan_resp[] = {0x04, 0x09, 0x53, 0x53, 0x53, 0x19, 0xFF, 0xD0, 0x07, 0x01, 0x03, 0x00, 0x00, 0x0C, 0x00, 0x88, 0xD1, 0xC4, 0x89, 0x2B, 0x56, 0x7D, 0xE5, 0x65, 0xAC, 0xA1, 0x3F, 0x09, 0x1C, 0x43, 0x92};
uint8 adv_data[] = {0x02, 0x01, 0x06, 0x03, 0x02, 0x01, 0xA2, 0x14, 0x16, 0x01, 0xA2, 0x01, 0x6B, 0x65, 0x79, 0x79, 0x66, 0x67, 0x35, 0x79, 0x33, 0x34, 0x79, 0x71, 0x78, 0x71, 0x67, 0x64};
uble_init(uble_demo_att_table, ARRAY_SIZE(uble_demo_att_table), 512, (void *)ble_adv_rx_data);
bt_hci_set_advdata(adv_data, sizeof(adv_data));
bt_hci_set_scan_rsp(scan_resp, sizeof(scan_resp));
bt_hci_set_adv_interval(50000);
bt_hci_set_adv_en(1);
bt_hci_set_ll_length(251);
return RET_OK;
}
/**
* @brief Startup or shutdown mode.
* @param mode
* 0 close
* 1 mode1(广播配网)
* 2 mode2(可扫描配网)
* 3 mode3(BLE协议配网)
* @param chan 37/38/39
*/
int32 ble_set_mode(uint8 mode, uint8 chan)
{
switch (mode) {
case 0:
ble_adv_ctrl_destory();
break;
case 1:
case 2:
ble_adv_ctrl_create();
break;
case 3:
break;
default:
os_printf(KERN_ERR"Unsupported mode = %d\r\n", mode);
return -ENOTSUPP;
}
return bt_hci_set_mode(mode, chan);
}
int32 ble_set_coexist_en(uint8 coexist, uint8 dec_duty)
{
return bt_hci_set_coexist_en(coexist, dec_duty);
}
#endif
/*************************** (C) COPYRIGHT 2023 TAIXIN-SEMI ***** END OF FILE *****/