This commit is contained in:
huxi
2025-12-03 11:12:34 +08:00
parent c23ae4f24c
commit bc195654bf
8163 changed files with 3799544 additions and 92 deletions
+269
View File
@@ -0,0 +1,269 @@
#ifdef SUPPORT_MS_EXTENSIONS
#pragma bss_seg(".pbg_demo.data.bss")
#pragma data_seg(".pbg_demo.data")
#pragma const_seg(".pbg_demo.text.const")
#pragma code_seg(".pbg_demo.text")
#endif
#include "system/includes.h"
#include "string.h"
#include "circular_buf.h"
#include "btstack/avctp_user.h"
#include "app_config.h"
#include "bt_tws.h"
#include "bt_common.h"
#include "syscfg_id.h"
#include "pbg_user.h"
#include "audio_cvp.h"
#define PBG_DEMO_EN 0//enalbe pbg_demo
#if (PBG_DEMO_EN&&TCFG_USER_TWS_ENABLE)
#define log_info(x, ...) printf("\n[###pbg_demo@@@]" x " ", ## __VA_ARGS__)
typedef struct {
// linked list - assert: first field
void *offset_item;
// data is contained in same memory
u32 service_record_handle;
u8 *service_record;
} service_record_item_t;
#define SDP_RECORD_HANDLER_REGISTER(handler) \
const service_record_item_t handler \
sec(.sdp_record_item)
//---------
enum {
PBG_EVENT_CONNECT = 1,
PBG_EVENT_DISCONNECT,
PBG_EVENT_MONITOR_START,//监听开始
PBG_EVENT_PACKET_HANDLER = 7,
};
enum {
PBG_USER_ST_NULL = 0x0,
PBG_USER_ST_CONNECT,
PBG_USER_ST_DISCONN,
};
enum {
PBG_USER_ERR_NONE = 0x0,
PBG_USER_ERR_SEND_BUFF_BUSY,
PBG_USER_ERR_SEND_OVER_LIMIT,
PBG_USER_ERR_SEND_FAIL,
};
//--------------------------------------------------------------------------------------------------
//---------
u8 pbg_profile_support = 1; //enable libs profile success
//---------
#define USER_SEND_POOL_SIZE (256L)
static u8 user_send_pool[USER_SEND_POOL_SIZE];
static u8 user_send_busy;
static u8 user_state;
u32 pbg_user_send(void *priv, u8 *buf, u32 len);
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//示例添加SDP服务表,注意不要跟bt_profile_config.c的定义有重复和冲突!!!!!!
//标准sdp结构表,以PNP服务示例定义,表末端增加 0x00,0x00 为结束符
/* static const u8 sdp_pnp_service_data[] = { */
/* 0x36, 0x00, 0x34, 0x09, 0x00, 0x00, 0x0A, 0x50, 0x01, 0x10, 0x00, 0x09, 0x00, 0x01, 0x36, 0x00, */
/* 0x03, 0x19, 0x12, 0x00, 0x09, 0x02, 0x00, 0x09, 0x01, 0x03, 0x09, 0x02, 0x01, 0x09, 0x05, 0xD6, */
/* 0x09, 0x02, 0x02, 0x09, 0x00, 0x0A, 0x09, 0x02, 0x03, 0x09, 0x02, 0x40, 0x09, 0x02, 0x04, 0x28, */
/* 0x01, 0x09, 0x02, 0x05, 0x09, 0x00, 0x01, 0x00, 0x00 */
/* }; */
/* SDP_RECORD_HANDLER_REGISTER(pnp_sdp_record_item) = { */
/* .service_record = (u8 *)sdp_pnp_service_data, */
/* .service_record_handle = 0x50011000, */
/* }; */
//--------------------------------------------------------------------------------------------------
//发送数据接口,注意返回值;确定OK
//数据真正发送成功,以接口user_pbg_send_ok_callback 回调为准
u32 pbg_user_send(void *priv, u8 *buf, u32 len)
{
if (user_state != PBG_USER_ST_CONNECT) {
return PBG_USER_ERR_SEND_FAIL;
}
log_info("send_data(%d)\n", len);
put_buf(buf, len);
if (user_send_busy == 1) {
log_info("ERR_SEND_BUFF_BUSY\n");
return PBG_USER_ERR_SEND_BUFF_BUSY;
}
if (user_send_pool == NULL) {
return PBG_USER_ERR_SEND_FAIL;
}
if (len) {
if (len > USER_SEND_POOL_SIZE) {
log_info("ERR_SEND_OVER_LIMIT\n");
return PBG_USER_ERR_SEND_OVER_LIMIT;
}
user_send_busy = 1;
memcpy(user_send_pool, buf, len);
u32 ret = bt_cmd_prepare(USER_CTRL_PBG_SEND_DATA, len, user_send_pool);
if (ret) {
user_send_busy = 0;
return PBG_USER_ERR_SEND_FAIL;
}
}
return PBG_USER_ERR_NONE;
}
void user_pbg_send_ok_callback(int err_code)
{
user_send_busy = 0;
log_info("send_ok\n");
}
static void user_pbg_packet_handler(u8 packet_type, u16 ch, u8 *packet, u16 size)
{
u32 tmp32;
switch (packet_type) {
case PBG_EVENT_PACKET_HANDLER:
log_info("pbg packet_data(%d):");
put_buf(packet, size);
/* test_pbg_cmd_send(packet);//for test */
break;
case PBG_EVENT_CONNECT:
user_state = PBG_USER_ST_CONNECT;
log_info("pbg connect #############\n");
break;
case PBG_EVENT_DISCONNECT:
user_state = PBG_USER_ST_DISCONN;
log_info("pbg disconnect #############\n");
break;
case PBG_EVENT_MONITOR_START:
log_info("pbg monitor start #############\n");
break;
default:
break;
}
}
#define PSM_BrowseGroupDescriptor (0x1001)
void pbg_demo_init(void)
{
log_info("pbg_demo_init\n");
pbg_profile_init(PSM_BrowseGroupDescriptor);//input PSM ID
pbg_event_handler_register(user_pbg_packet_handler);
user_state = PBG_USER_ST_DISCONN;
user_send_busy = 0;
}
static int pbg_btstack_event_handler(int *msg)
{
struct bt_event *bt = (struct bt_event *)msg;
switch (bt->event) {
case BT_STATUS_INIT_OK:
pbg_demo_init();
break;
}
return 0;
}
APP_MSG_HANDLER(pbg_stack_msg_entry) = {
.owner = 0xff,
.from = MSG_FROM_BT_STACK,
.handler = pbg_btstack_event_handler,
};
static int pbg_tws_event_handler(int *msg)
{
struct tws_event *evt = (struct tws_event *)msg;
switch (evt->event) {
case TWS_EVENT_CONNECTED:
pbg_user_set_tws_state(1);
break;
case TWS_EVENT_SEARCH_TIMEOUT:
case TWS_EVENT_CONNECTION_TIMEOUT:
case TWS_EVENT_CONNECTION_DETACH:
case TWS_EVENT_REMOVE_PAIRS:
pbg_user_set_tws_state(0);
break;
}
}
APP_MSG_HANDLER(pbg_tws_msg_entry) = {
.owner = 0xff,
.from = MSG_FROM_TWS,
.handler = pbg_tws_event_handler,
};
#else
void pbg_demo_init(void)
{
/* printf("pbg not enable...\n"); */
}
void pbg_user_battery_level_sync(u8 *dev_bat)
{
//add code
}
void pbg_user_ear_pos_sync(u8 left, u8 right)
{
//add code
}
/*bool pbg_user_key_vaild(u8 *key_msg, struct sys_event *event)
{
//add code
return false;
}*/
void pbg_user_event_deal(struct pbg_event *evt)
{
//add code
}
void pbg_user_set_tws_state(u8 conn_flag)
{
//add code
}
void pbg_user_mic_fixed_deal(u8 mode)
{
//add code
}
void pbg_user_recieve_sync_info(u8 *sync_info)
{
//add code
}
int pbg_user_is_connected(void)
{
//add code
//return PBG服务是否已连上,区分安卓和苹果,是否持续广播
return 1;//不支持PBG服务,return 1
}
//----------------------------------------
#endif
+245
View File
@@ -0,0 +1,245 @@
#ifdef SUPPORT_MS_EXTENSIONS
#pragma bss_seg(".trans_data_demo.data.bss")
#pragma data_seg(".trans_data_demo.data")
#pragma const_seg(".trans_data_demo.text.const")
#pragma code_seg(".trans_data_demo.text")
#endif
#include "app_config.h"
#include "bt.h"
#include "app_main.h"
#include "update_tws.h"
#include "3th_profile_api.h"
#include "btstack/avctp_user.h"
#include "btstack/btstack_task.h"
#include "bt_tws.h"
#include "spp_trans_data.h"
#include "spp_user.h"
#if (BT_AI_SEL_PROTOCOL & TRANS_DATA_EN)
#define LOG_TAG "[TRANS_DATA]"
#define LOG_ERROR_ENABLE
#define LOG_DEBUG_ENABLE
#define LOG_INFO_ENABLE
/* #define LOG_DUMP_ENABLE */
#define LOG_CLI_ENABLE
#include "debug.h"
u8 ble_adv_miss_flag = 0;
u8 ble_adv_poweron_flag = 0;
int trans_data_earphone_state_tws_connected(int first_pair, u8 *comm_addr)
{
#if 0
if (first_pair) {
/* bt_ble_adv_enable(0); */
u8 tmp_ble_addr[6] = {0};
bt_make_ble_address(tmp_ble_addr, comm_addr);
le_controller_set_mac(tmp_ble_addr);//将ble广播地址改成公共地址
bt_update_mac_addr(comm_addr);
/* bt_ble_adv_enable(1); */
/*新的连接,公共地址改变了,要重新将新的地址广播出去*/
if (tws_api_get_role() == TWS_ROLE_MASTER) {
printf("\nNew Connect Master!!!\n\n");
ble_app_disconnect();
bt_ble_adv_enable(0);
bt_ble_adv_enable(1);
} else {
printf("\nConnect Slave!!!\n\n");
/*从机ble关掉*/
ble_app_disconnect();
bt_ble_adv_enable(0);
}
}
#endif
return 0;
}
int trans_data_earphone_state_enter_soft_poweroff()
{
bt_ble_exit();
return 0;
}
void trans_data_bt_tws_event_handler(struct bt_event *bt)
{
int role = bt->args[0];
int phone_link_connection = bt->args[1];
int reason = bt->args[2];
switch (bt->event) {
case TWS_EVENT_CONNECTED:
//bt_ble_adv_enable(1);
if (tws_api_get_role() == TWS_ROLE_SLAVE) {
//master enable
printf("\nConnect Slave!!!\n\n");
/*从机ble关掉*/
ble_app_disconnect();
bt_ble_adv_enable(0);
}
break;
case TWS_EVENT_PHONE_LINK_DETACH:
/*
* 跟手机的链路LMP层已完全断开, 只有tws在连接状态才会收到此事件
*/
break;
case TWS_EVENT_CONNECTION_DETACH:
/*
* TWS连接断开
*/
if (app_var.goto_poweroff_flag) {
break;
}
if (get_app_connect_type() == 0) {
printf("\ntws detach to open ble~~~\n\n");
bt_ble_adv_enable(1);
}
set_ble_connect_type(TYPE_NULL);
break;
case TWS_EVENT_SYNC_FUN_CMD:
break;
case TWS_EVENT_ROLE_SWITCH:
break;
}
#if OTA_TWS_SAME_TIME_ENABLE
tws_ota_app_event_deal(bt->event);
#endif
}
int trans_data_sys_event_handler_specific(struct sys_event *event)
{
switch (event->type) {
case SYS_BT_EVENT:
#if 0
if ((u32)event->arg == SYS_BT_EVENT_TYPE_CON_STATUS) {
} else if ((u32)event->arg == SYS_BT_EVENT_TYPE_HCI_STATUS) {
}
#if TCFG_USER_TWS_ENABLE
else if (((u32)event->arg == SYS_BT_EVENT_FROM_TWS)) {
trans_data_bt_tws_event_handler(&event->u.bt);
}
#endif
#if OTA_TWS_SAME_TIME_ENABLE
else if (((u32)event->arg == SYS_BT_OTA_EVENT_TYPE_STATUS)) {
bt_ota_event_handler(&event->u.bt);
}
#endif
#endif
break;
case SYS_DEVICE_EVENT:
break;
}
return 0;
}
int user_spp_state_specific(u8 packet_type)
{
#if 0
switch (packet_type) {
case 1:
bt_ble_adv_enable(0);
set_app_connect_type(TYPE_SPP);
break;
case 2:
set_app_connect_type(TYPE_NULL);
#if TCFG_USER_TWS_ENABLE
if (!(tws_api_get_tws_state() & TWS_STA_SIBLING_CONNECTED)) {
ble_module_enable(1);
} else {
if (tws_api_get_role() == TWS_ROLE_MASTER) {
ble_module_enable(1);
}
}
#else
ble_module_enable(1);
#endif
break;
}
#endif
return 0;
}
static int trans_data_earphone_state_init()
{
transport_spp_init();
bt_ble_init();
return 0;
}
static int trans_data_btstack_event_handler(int *_event)
{
struct bt_event *event = (struct bt_event *)_event;
switch (event->event) {
case BT_STATUS_INIT_OK:
#if TCFG_NORMAL_SET_DUT_MODE
break;
#endif
printf("trans_data_earphone_state_init\n");
trans_data_earphone_state_init();
return 0;
case BT_STATUS_FIRST_CONNECTED:
break;
case BT_STATUS_SECOND_CONNECTED:
break;
}
return 0;
}
APP_MSG_HANDLER(trans_data_stack_msg_entry) = {
.owner = 0xff,
.from = MSG_FROM_BT_STACK,
.handler = trans_data_btstack_event_handler,
};
static int trans_data_tws_event_handler(int *_event)
{
struct tws_event *event = (struct tws_event *)_event;
int role = event->args[0];
int reason = event->args[2];
switch (event->event) {
case TWS_EVENT_CONNECTED:
if (role == TWS_ROLE_MASTER) {
} else {
}
break;
case TWS_EVENT_CONNECTION_DETACH:
if (app_var.goto_poweroff_flag) {
break;
}
break;
case TWS_EVENT_ROLE_SWITCH:
break;
}
return 0;
}
APP_MSG_HANDLER(trans_data_tws_msg_entry) = {
.owner = 0xff,
.from = MSG_FROM_TWS,
.handler = trans_data_tws_event_handler,
};
#endif