作为主设备去连接其他蓝牙设备
This commit is contained in:
528
apps/earphone/xtell_remote_control/ble_handler/ble_handler.c
Normal file
528
apps/earphone/xtell_remote_control/ble_handler/ble_handler.c
Normal file
@ -0,0 +1,528 @@
|
||||
#include "system/includes.h"
|
||||
#include "media/includes.h"
|
||||
#include "tone_player.h"
|
||||
#include "earphone.h"
|
||||
|
||||
#include "app_config.h"
|
||||
#include "app_action.h"
|
||||
#include "app_task.h"
|
||||
|
||||
#include "btstack/avctp_user.h"
|
||||
#include "btstack/btstack_task.h"
|
||||
#include "btctrler/btctrler_task.h"
|
||||
#include "btstack/frame_queque.h"
|
||||
#include "user_cfg.h"
|
||||
// #include "aec_user.h"
|
||||
#include "classic/hci_lmp.h"
|
||||
#include "bt_common.h"
|
||||
#include "bt_ble.h"
|
||||
#include "bt_tws.h"
|
||||
#include "pbg_user.h"
|
||||
#include "btstack/bluetooth.h"
|
||||
#include "colorful_lights/colorful_lights.h"
|
||||
|
||||
#include "app_chargestore.h"
|
||||
#include "jl_kws/jl_kws_api.h"
|
||||
|
||||
#include "asm/charge.h"
|
||||
#include "app_charge.h"
|
||||
#include "ui_manage.h"
|
||||
|
||||
#include "app_chargestore.h"
|
||||
#include "app_umidigi_chargestore.h"
|
||||
#include "app_testbox.h"
|
||||
#include "app_online_cfg.h"
|
||||
#include "app_main.h"
|
||||
#include "app_power_manage.h"
|
||||
#include "gSensor/gSensor_manage.h"
|
||||
#include "key_event_deal.h"
|
||||
#include "classic/tws_api.h"
|
||||
#include "asm/pwm_led.h"
|
||||
#include "ir_sensor/ir_manage.h"
|
||||
#include "in_ear_detect/in_ear_manage.h"
|
||||
#include "vol_sync.h"
|
||||
#include "bt_background.h"
|
||||
#include "default_event_handler.h"
|
||||
#include "debug.h"
|
||||
#include "system/event.h"
|
||||
#include "../nvs/nvs.h"
|
||||
#if (JL_EARPHONE_APP_EN)
|
||||
#include "rcsp_adv_bluetooth.h"
|
||||
#endif
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//宏定义
|
||||
#define LOG_TAG_CONST EARPHONE
|
||||
#define LOG_TAG "[EARPHONE]"
|
||||
#define LOG_ERROR_ENABLE
|
||||
#define LOG_DEBUG_ENABLE
|
||||
#define xlog_ENABLE
|
||||
|
||||
|
||||
#if(USE_DMA_UART_TEST) //使用dm串口测试时不能同时打开
|
||||
#define MY_SNIFF_EN 0
|
||||
#else
|
||||
#define MY_SNIFF_EN 1 //默认打开
|
||||
#endif
|
||||
|
||||
#define ENABLE_XLOG 1
|
||||
#ifdef xlog
|
||||
#undef xlog
|
||||
#endif
|
||||
#if ENABLE_XLOG
|
||||
#define xlog(format, ...) printf("[XT:%s] " format, __func__, ##__VA_ARGS__)
|
||||
#else
|
||||
#define xlog(format, ...) ((void)0)
|
||||
#endif
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//变量
|
||||
extern u8 init_ok;
|
||||
extern u8 sniff_out;
|
||||
unsigned char xtell_bl_state=0; //存放经典蓝牙的连接状态,0断开,1是连接
|
||||
u8 bt_newname =0;
|
||||
unsigned char xt_ble_new_name[9] = "CM-55555";
|
||||
static u16 play_poweron_ok_timer_id = 0;
|
||||
|
||||
// -- 初始化标志位 --
|
||||
u8 SC7U22_init = 0x10; //六轴是否初始化
|
||||
u8 MMC5603nj_init = 0x20; //地磁是否初始化
|
||||
u8 BMP280_init = 0x30; //气压计初始化
|
||||
// -- 线程id --
|
||||
u16 SC7U22_calibration_id;
|
||||
u16 start_collect_fuc_id;
|
||||
u16 BLE_send_fuc_id;
|
||||
u16 rfid_fuc_id;
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
extern int bt_hci_event_handler(struct bt_event *bt);
|
||||
extern void SC7U22_static_calibration(void);
|
||||
extern void create_process(u16* pid, const char* name, void *priv, void (*func)(void *priv), u32 msec);
|
||||
extern void close_process(u16* pid,char* name);
|
||||
extern void start_collect_fuc(void);
|
||||
extern void BLE_send_fuc(void);
|
||||
extern void xtell_ble_central_test_start(void);
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/*
|
||||
* 模式状态机, 通过start_app()控制状态切换
|
||||
*/
|
||||
/* extern int audio_mic_init(); */
|
||||
|
||||
static int state_machine(struct application *app, enum app_state state, struct intent *it){
|
||||
int error = 0;
|
||||
static u8 tone_player_err = 0;
|
||||
xlog("bt_state_machine=%d\n", state);
|
||||
switch (state) {
|
||||
case APP_STA_CREATE:
|
||||
xlog("APP_STA_CREATE\n");
|
||||
/* set_adjust_conn_dac_check(0); */
|
||||
|
||||
break;
|
||||
case APP_STA_START:
|
||||
xlog("APP_STA_START\n");
|
||||
if (!it) {
|
||||
xlog("APP_STA_START:it none\n");
|
||||
break;
|
||||
}
|
||||
switch (it->action) {
|
||||
case ACTION_EARPHONE_MAIN:
|
||||
xlog("ble init\n");
|
||||
/*
|
||||
* handler 初始化
|
||||
*/
|
||||
clk_set("sys", BT_NORMAL_HZ);
|
||||
u32 sys_clk = clk_get("sys");
|
||||
bt_pll_para(TCFG_CLOCK_OSC_HZ, sys_clk, 0, 0);
|
||||
/* bredr_set_dut_enble(1, 1); */
|
||||
bt_function_select_init();
|
||||
bredr_handle_register();
|
||||
EARPHONE_STATE_INIT();
|
||||
btstack_init();
|
||||
sys_auto_shut_down_enable();
|
||||
bt_sniff_feature_init();
|
||||
sys_auto_sniff_controle(MY_SNIFF_EN, NULL);
|
||||
app_var.dev_volume = -1;
|
||||
break;
|
||||
case ACTION_A2DP_START: //蓝牙音频传输协议
|
||||
xlog("ACTION_A2DP_START\n");
|
||||
break;
|
||||
case ACTION_BY_KEY_MODE:
|
||||
xlog("ACTION_BY_KEY_MODE\n");
|
||||
break;
|
||||
case ACTION_TONE_PLAY:
|
||||
xlog("ACTION_TONE_PLAY\n");
|
||||
// STATUS *p_tone = get_tone_config();
|
||||
// tone_play_index(p_tone->bt_init_ok, 1);
|
||||
break;
|
||||
case ACTION_DO_NOTHING:
|
||||
xlog("ACTION_DO_NOTHING\n");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case APP_STA_PAUSE:
|
||||
xlog("APP_STA_PAUSE\n");
|
||||
break;
|
||||
case APP_STA_RESUME:
|
||||
xlog("APP_STA_RESUME\n");
|
||||
//恢复前台运行
|
||||
sys_auto_shut_down_disable();
|
||||
sys_key_event_enable();
|
||||
break;
|
||||
case APP_STA_STOP:
|
||||
xlog("APP_STA_STOP\n");
|
||||
break;
|
||||
case APP_STA_DESTROY:
|
||||
xlog("APP_STA_DESTROY\n");
|
||||
r_printf("APP_STA_DESTROY\n");
|
||||
if (!app_var.goto_poweroff_flag) {
|
||||
bt_app_exit(NULL);
|
||||
}
|
||||
break;
|
||||
}
|
||||
xlog("state machine error\n");
|
||||
return error;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//handle
|
||||
|
||||
|
||||
void le_user_app_send_event(size_t command, unsigned char* data, size_t size)
|
||||
{
|
||||
// 中断->事件
|
||||
static unsigned char buffer[512];
|
||||
if(data && size && size <= sizeof(buffer)) {
|
||||
// 拷贝到缓存,避免转发事件的时候,地址发送改变。
|
||||
memcpy(buffer, data, size);
|
||||
struct sys_event event;
|
||||
event.type = SYS_APP_USER_EVENT;
|
||||
event.u.app.command = command;
|
||||
event.u.app.buffer = buffer;
|
||||
event.u.app.size = size;
|
||||
sys_event_notify(&event);
|
||||
}
|
||||
}
|
||||
|
||||
void le_user_app_event_handler(struct sys_event* event){
|
||||
|
||||
switch (event->type) {
|
||||
// 打印接收到的数据
|
||||
printf("BLE data\n");
|
||||
put_buf(event->u.app.buffer, event->u.app.size);
|
||||
case SYS_APP_USER_EVENT:
|
||||
if (event->u.app.buffer[0] == 0xBE && event->u.app.buffer[1] == 0xBB) {
|
||||
if(event->u.app.buffer[2] == 0x01){ //后面的数据长度 1
|
||||
switch (event->u.app.buffer[3]){
|
||||
case 0x01:
|
||||
nvs_test_factory_info();
|
||||
break;
|
||||
case 0x02:
|
||||
// factory_info_t read_info;;
|
||||
// nvs_read_factory_info(&read_info);
|
||||
extern void rfid_task_fuc(void);
|
||||
create_process(&rfid_fuc_id,"rfid",NULL,rfid_task_fuc,2000);
|
||||
break;
|
||||
case 0xff: //测试
|
||||
int ret = hw_iic_init(0);
|
||||
// int ret = soft_iic_init(0);
|
||||
xlog("init iic result:%d\n", ret); //返回0成功
|
||||
extern void i2c_scanner_probe(void);
|
||||
i2c_scanner_probe();
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}else if(event->u.app.buffer[2] == 0x02){ //后面数据长度为2
|
||||
switch (event->u.app.buffer[3]){ //数据包类型
|
||||
case 0x00:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
xlog("%d\n",event->type);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
static void play_poweron_ok_timer(void *priv)
|
||||
{
|
||||
app_var.wait_timer_do = 0;
|
||||
|
||||
log_d("\n-------play_poweron_ok_timer-------\n", priv);
|
||||
if (is_dac_power_off()) {
|
||||
#if TCFG_USER_TWS_ENABLE
|
||||
bt_tws_poweron();
|
||||
#else
|
||||
bt_wait_connect_and_phone_connect_switch(0);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
app_var.wait_timer_do = sys_timeout_add(priv, play_poweron_ok_timer, 100);
|
||||
}
|
||||
|
||||
static void play_bt_connect_dly(void *priv)
|
||||
{
|
||||
app_var.wait_timer_do = 0;
|
||||
|
||||
log_d("\n-------play_bt_connect_dly-------\n", priv);
|
||||
|
||||
if (!app_var.goto_poweroff_flag) {
|
||||
STATUS *p_tone = get_tone_config();
|
||||
tone_play_index(p_tone->bt_connect_ok, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int bt_connction_status_event_handler(struct bt_event *bt)
|
||||
{
|
||||
STATUS *p_tone = get_tone_config();
|
||||
u8 *phone_number = NULL;
|
||||
|
||||
switch (bt->event) {
|
||||
case BT_STATUS_INIT_OK:
|
||||
/*
|
||||
* 蓝牙初始化完成
|
||||
*/
|
||||
xlog("BT_STATUS_INIT_OK\n");
|
||||
init_ok = 1;
|
||||
__set_sbc_cap_bitpool(38);
|
||||
|
||||
#if (TCFG_USER_BLE_ENABLE)
|
||||
if (BT_MODE_IS(BT_BQB)) {
|
||||
ble_bqb_test_thread_init();
|
||||
} else {
|
||||
#if !TCFG_WIRELESS_MIC_ENABLE
|
||||
bt_ble_init(); // lmx,初始化完,初始化ble,决定ble是作为主设备还是从设备
|
||||
// xtell_ble_central_test_start(); //xtell
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
bt_init_ok_search_index();
|
||||
#if TCFG_TEST_BOX_ENABLE
|
||||
testbox_set_bt_init_ok(1);
|
||||
#endif
|
||||
|
||||
#if ((CONFIG_BT_MODE == BT_BQB)||(CONFIG_BT_MODE == BT_PER))
|
||||
bt_wait_phone_connect_control(1);
|
||||
#else
|
||||
if (is_dac_power_off()) {
|
||||
bt_wait_connect_and_phone_connect_switch(0);
|
||||
} else {
|
||||
app_var.wait_timer_do = sys_timeout_add(NULL, play_poweron_ok_timer, 100);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*if (app_var.play_poweron_tone) {
|
||||
tone_play_index(p_tone->power_on, 1);
|
||||
}*/
|
||||
break;
|
||||
|
||||
case BT_STATUS_SECOND_CONNECTED:
|
||||
clear_current_poweron_memory_search_index(0);
|
||||
case BT_STATUS_FIRST_CONNECTED:
|
||||
xlog("BT_STATUS_CONNECTED\n");
|
||||
xtell_bl_state = 1; //蓝牙连接成功 置1
|
||||
if(strcmp(xt_ble_new_name,"CM-11111") != 0){
|
||||
//蓝牙连接成功
|
||||
bt_newname =1;
|
||||
u8 temp[5]={0xBB,0xBE,0x02,0x04,0x00};
|
||||
temp[4] = xtell_bl_state; //经典蓝牙连接状态
|
||||
// send_data_to_ble_client(&temp,5);
|
||||
}
|
||||
earphone_change_pwr_mode(PWR_DCDC15, 3000);
|
||||
sys_auto_shut_down_disable();
|
||||
|
||||
ui_update_status(STATUS_BT_CONN); //单台在此处设置连接状态,对耳的连接状态需要同步,在bt_tws.c中去设置
|
||||
|
||||
/* tone_play(TONE_CONN); */
|
||||
/*os_time_dly(40); // for test*/
|
||||
xlog("tone status:%d\n", tone_get_status());
|
||||
if (get_call_status() == BT_CALL_HANGUP) {
|
||||
if (app_var.phone_dly_discon_time) {
|
||||
sys_timeout_del(app_var.phone_dly_discon_time);
|
||||
app_var.phone_dly_discon_time = 0;
|
||||
} else {
|
||||
app_var.wait_timer_do = sys_timeout_add(NULL, play_bt_connect_dly, 1600);
|
||||
/* tone_play_index(p_tone->bt_connect_ok, 1); */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*int timeout = 5000 + rand32() % 10000;
|
||||
sys_timeout_add(NULL, connect_phone_test, timeout);*/
|
||||
break;
|
||||
case BT_STATUS_FIRST_DISCONNECT:
|
||||
case BT_STATUS_SECOND_DISCONNECT:
|
||||
xlog("BT_STATUS_DISCONNECT\n");
|
||||
xtell_bl_state = 0; //断开蓝牙 清0
|
||||
//蓝牙断开连接
|
||||
if(bt_newname){ //已经改成新蓝牙名字,断开才播报
|
||||
bt_newname=0;
|
||||
u8 temp[5]={0xBB,0xBE,0x02,0x04,0x00};
|
||||
temp[4] = xtell_bl_state; //经典蓝牙连接状态
|
||||
// send_data_to_ble_client(&temp,5);
|
||||
}
|
||||
if (app_var.goto_poweroff_flag) {
|
||||
/*关机不播断开提示音*/
|
||||
/*关机时不改UI*/
|
||||
break;
|
||||
}
|
||||
// bt_discon_dly_handle(NULL);
|
||||
break;
|
||||
|
||||
//phone status deal
|
||||
case BT_STATUS_PHONE_INCOME:
|
||||
break;
|
||||
case BT_STATUS_PHONE_OUT:
|
||||
|
||||
break;
|
||||
case BT_STATUS_PHONE_ACTIVE:
|
||||
break;
|
||||
case BT_STATUS_PHONE_HANGUP:
|
||||
break;
|
||||
case BT_STATUS_PHONE_NUMBER:
|
||||
break;
|
||||
case BT_STATUS_INBAND_RINGTONE: //铃声
|
||||
break;
|
||||
case BT_STATUS_CALL_VOL_CHANGE:
|
||||
|
||||
break;
|
||||
case BT_STATUS_SNIFF_STATE_UPDATE:
|
||||
xlog(" BT_STATUS_SNIFF_STATE_UPDATE %d\n", bt->value); //0退出SNIFF
|
||||
if (bt->value == 0) {
|
||||
sniff_out = 1;
|
||||
sys_auto_sniff_controle(MY_SNIFF_EN, bt->args);
|
||||
} else {
|
||||
sys_auto_sniff_controle(0, bt->args);
|
||||
}
|
||||
break;
|
||||
|
||||
case BT_STATUS_LAST_CALL_TYPE_CHANGE:
|
||||
break;
|
||||
|
||||
case BT_STATUS_CONN_A2DP_CH:
|
||||
case BT_STATUS_CONN_HFP_CH:
|
||||
|
||||
if ((!is_1t2_connection()) && (get_current_poweron_memory_search_index(NULL))) { //回连下一个device
|
||||
if (get_esco_coder_busy_flag()) {
|
||||
clear_current_poweron_memory_search_index(0);
|
||||
} else {
|
||||
user_send_cmd_prepare(USER_CTRL_START_CONNECTION, 0, NULL);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case BT_STATUS_PHONE_MANUFACTURER:
|
||||
break;
|
||||
case BT_STATUS_VOICE_RECOGNITION:
|
||||
|
||||
break;
|
||||
case BT_STATUS_AVRCP_INCOME_OPID:
|
||||
xlog("BT_STATUS_AVRCP_INCOME_OPID:%d\n", bt->value);
|
||||
break;
|
||||
default:
|
||||
xlog(" BT STATUS DEFAULT\n");
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int event_handler(struct application *app, struct sys_event *event)
|
||||
{
|
||||
|
||||
le_user_app_event_handler(event);
|
||||
|
||||
if (SYS_EVENT_REMAP(event)) {
|
||||
g_printf("****SYS_EVENT_REMAP**** \n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (event->type) {
|
||||
case SYS_KEY_EVENT:
|
||||
break;
|
||||
case SYS_BT_EVENT:
|
||||
/*
|
||||
* 蓝牙事件处理
|
||||
*/
|
||||
if ((u32)event->arg == SYS_BT_EVENT_TYPE_CON_STATUS) {
|
||||
printf("in event_handler:bt_connction_status_event_handler");
|
||||
bt_connction_status_event_handler(&event->u.bt);
|
||||
} else if ((u32)event->arg == SYS_BT_EVENT_TYPE_HCI_STATUS) {
|
||||
bt_hci_event_handler(&event->u.bt);
|
||||
}
|
||||
break;
|
||||
case SYS_DEVICE_EVENT:
|
||||
/*
|
||||
* 系统设备事件处理
|
||||
*/
|
||||
if ((u32)event->arg == DEVICE_EVENT_FROM_CHARGE) {
|
||||
|
||||
} else if ((u32)event->arg == DEVICE_EVENT_FROM_POWER) {
|
||||
return app_power_event_handler(&event->u.dev);
|
||||
}
|
||||
#if (JL_EARPHONE_APP_EN)
|
||||
else if ((u32)event->arg == DEVICE_EVENT_FROM_RCSP) {
|
||||
xlog("DEVICE_EVENT_FROM_RCSP: %d", event->u.rcsp.event);
|
||||
switch (event->u.rcsp.event) {
|
||||
case MSG_JL_UPDATE_START:
|
||||
xlog(">>> Xtell APP: MSG_JL_UPDATE_START\n");
|
||||
// You can add UI notifications here, like LED blinking or a tone.
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if TCFG_UMIDIGI_BOX_ENABLE
|
||||
else if ((u32)event->arg == DEVICE_EVENT_UMIDIGI_CHARGE_STORE) {
|
||||
app_umidigi_chargestore_event_handler(&event->u.umidigi_chargestore);
|
||||
}
|
||||
#endif
|
||||
#if TCFG_TEST_BOX_ENABLE
|
||||
else if ((u32)event->arg == DEVICE_EVENT_TEST_BOX) {
|
||||
app_testbox_event_handler(&event->u.testbox);
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
SYS_EVENT_HANDLER_SPECIFIC(event);
|
||||
#ifdef CONFIG_BT_BACKGROUND_ENABLE
|
||||
if (app) {
|
||||
default_event_handler(event);
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static const struct application_operation app_handler_ops = {
|
||||
.state_machine = state_machine,
|
||||
.event_handler = event_handler,
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* 注册earphone模式
|
||||
*/
|
||||
REGISTER_APPLICATION(app_handler) = {
|
||||
.name = "handler",
|
||||
.action = ACTION_EARPHONE_MAIN,
|
||||
.ops = &app_handler_ops,
|
||||
.state = APP_STA_DESTROY,
|
||||
};
|
||||
268
apps/earphone/xtell_remote_control/ble_handler/ble_test.c
Normal file
268
apps/earphone/xtell_remote_control/ble_handler/ble_test.c
Normal file
@ -0,0 +1,268 @@
|
||||
|
||||
#include "system/app_core.h"
|
||||
#include "system/includes.h"
|
||||
#include "btstack/btstack_task.h"
|
||||
#include "btstack/bluetooth.h"
|
||||
#include "le_common.h"
|
||||
#include "ble_user.h"
|
||||
|
||||
#if 0 //ENABLE_THIS_TEST
|
||||
|
||||
#define LOG_TAG_CONST EARPHONE
|
||||
#define LOG_TAG "[BLE_TEST]"
|
||||
#define LOG_ERROR_ENABLE
|
||||
#define LOG_DEBUG_ENABLE
|
||||
#define LOG_INFO_ENABLE
|
||||
#include "debug.h"
|
||||
|
||||
// =================== 配置区 START ===================
|
||||
|
||||
// 1. 设置要连接的目标从机设备蓝牙名称
|
||||
#define TARGET_BLE_NAME "CM-22222"
|
||||
|
||||
// 2. 设置要搜索的 Service UUID 和 Characteristic UUID
|
||||
// Service UUID: 0x180D
|
||||
#define TARGET_SERVICE_UUID16 0x180D
|
||||
// Characteristic UUID: 0x2A37 (Notify)
|
||||
#define TARGET_CHARACTERISTIC_UUID16 0x2A37
|
||||
#define TARGET_CHARACTERISTIC_OPT_TYPE ATT_PROPERTY_NOTIFY
|
||||
|
||||
// =================== 配置区 END =====================
|
||||
|
||||
// ATT RAM buffer
|
||||
#define ATT_LOCAL_MTU_SIZE (517)
|
||||
#define ATT_SEND_CBUF_SIZE (256)
|
||||
#define ATT_RAM_BUFSIZE (ATT_CTRL_BLOCK_SIZE + ATT_LOCAL_MTU_SIZE + ATT_SEND_CBUF_SIZE)
|
||||
static u8 att_ram_buffer[ATT_RAM_BUFSIZE] __attribute__((aligned(4)));
|
||||
|
||||
// Profile 搜索 buffer
|
||||
#define SEARCH_PROFILE_BUFSIZE (512)
|
||||
static u8 search_ram_buffer[SEARCH_PROFILE_BUFSIZE] __attribute__((aligned(4)));
|
||||
|
||||
// BLE 工作状态
|
||||
static u8 ble_work_state = 0;
|
||||
static hci_con_handle_t con_handle;
|
||||
|
||||
// 搜索到的目标特征值句柄
|
||||
static u16 target_write_handle = 0;
|
||||
static u16 target_notify_handle = 0;
|
||||
|
||||
// 函数前向声明
|
||||
static int ble_central_test_scan_enable(u32 en);
|
||||
static void cbk_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
|
||||
|
||||
|
||||
// 设置 BLE 工作状态
|
||||
static void set_ble_work_state(u8 state) {
|
||||
if (state != ble_work_state) {
|
||||
log_info("ble_work_state: %d -> %d\n", ble_work_state, state);
|
||||
ble_work_state = state;
|
||||
}
|
||||
}
|
||||
|
||||
// 解析广播数据,寻找目标设备
|
||||
static bool resolve_adv_report(u8 *adv_address, u8 data_length, u8 *data) {
|
||||
u8 i, length, ad_type;
|
||||
u8 *adv_data_ptr = data;
|
||||
|
||||
for (i = 0; i < data_length;) {
|
||||
if (*adv_data_ptr == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
length = *adv_data_ptr++;
|
||||
ad_type = *adv_data_ptr++;
|
||||
i += (length + 1);
|
||||
|
||||
if (ad_type == HCI_EIR_DATATYPE_COMPLETE_LOCAL_NAME || ad_type == HCI_EIR_DATATYPE_SHORTENED_LOCAL_NAME) {
|
||||
if (length > 1 && (0 == memcmp(adv_data_ptr, TARGET_BLE_NAME, strlen(TARGET_BLE_NAME)))) {
|
||||
log_info("Found target device: %s", TARGET_BLE_NAME);
|
||||
log_info_hexdump(adv_address, 6);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
adv_data_ptr += (length - 1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 创建连接
|
||||
static void central_test_create_connection(u8 *addr, u8 addr_type) {
|
||||
if (ble_work_state >= BLE_ST_CREATE_CONN) {
|
||||
return;
|
||||
}
|
||||
log_info("Stopping scan...");
|
||||
ble_central_test_scan_enable(0);
|
||||
|
||||
struct create_conn_param_t conn_param = {0};
|
||||
conn_param.conn_interval = 24;
|
||||
conn_param.conn_latency = 0;
|
||||
conn_param.supervision_timeout = 600;
|
||||
conn_param.peer_address_type = addr_type;
|
||||
memcpy(conn_param.peer_address, addr, 6);
|
||||
|
||||
set_ble_work_state(BLE_ST_CREATE_CONN);
|
||||
ble_op_create_connection(&conn_param);
|
||||
}
|
||||
|
||||
// 开始搜索 Profile
|
||||
static void central_test_search_profile() {
|
||||
target_write_handle = 0;
|
||||
target_notify_handle = 0;
|
||||
user_client_init(con_handle, search_ram_buffer, SEARCH_PROFILE_BUFSIZE);
|
||||
ble_op_search_profile_all();
|
||||
}
|
||||
|
||||
// 搜索 Profile 结果回调
|
||||
void user_client_report_search_result(search_result_t *result_info) {
|
||||
if (result_info == (void *)-1) {
|
||||
log_info("Search profile complete.");
|
||||
set_ble_work_state(BLE_ST_SEARCH_COMPLETE);
|
||||
// 如果找到了 NOTIFY 特征,使能它
|
||||
if (target_notify_handle) {
|
||||
log_info("Enabling notification for handle 0x%04x", target_notify_handle);
|
||||
u16 val = 0x0001;
|
||||
ble_op_att_send_data(target_notify_handle + 1, &val, 2, ATT_OP_WRITE);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (result_info->services.uuid16 == TARGET_SERVICE_UUID16 &&
|
||||
result_info->characteristic.uuid16 == TARGET_CHARACTERISTIC_UUID16) {
|
||||
log_info("Found target characteristic UUID 0x%04x", TARGET_CHARACTERISTIC_UUID16);
|
||||
if ((result_info->characteristic.properties & ATT_PROPERTY_WRITE_WITHOUT_RESPONSE) ||
|
||||
(result_info->characteristic.properties & ATT_PROPERTY_WRITE)) {
|
||||
target_write_handle = result_info->characteristic.value_handle;
|
||||
log_info("Found write handle: 0x%04x", target_write_handle);
|
||||
}
|
||||
if (result_info->characteristic.properties & ATT_PROPERTY_NOTIFY) {
|
||||
target_notify_handle = result_info->characteristic.value_handle;
|
||||
log_info("Found notify handle: 0x%04x", target_notify_handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 接收到数据回调
|
||||
void user_client_report_data_callback(att_data_report_t *report_data) {
|
||||
log_info("RX data, handle=0x%04x, len=%d:", report_data->value_handle, report_data->blob_length);
|
||||
log_info_hexdump(report_data->blob, report_data->blob_length);
|
||||
}
|
||||
|
||||
// BLE 事件回调处理
|
||||
static void cbk_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
|
||||
switch (packet_type) {
|
||||
case HCI_EVENT_PACKET:
|
||||
switch (hci_event_packet_get_type(packet)) {
|
||||
case HCI_EVENT_LE_META:
|
||||
switch (hci_event_le_meta_get_subevent_code(packet)) {
|
||||
case HCI_SUBEVENT_LE_ENHANCED_CONNECTION_COMPLETE:
|
||||
case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
|
||||
if (hci_subevent_le_connection_complete_get_status(packet)) {
|
||||
log_error("Connection failed, status: 0x%x", hci_subevent_le_connection_complete_get_status(packet));
|
||||
set_ble_work_state(BLE_ST_CONNECT_FAIL);
|
||||
ble_central_test_scan_enable(1); // 重新开始扫描
|
||||
} else {
|
||||
con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
|
||||
log_info("Connection established, handle: 0x%04x", con_handle);
|
||||
ble_op_att_send_init(con_handle, att_ram_buffer, ATT_RAM_BUFSIZE, ATT_LOCAL_MTU_SIZE);
|
||||
set_ble_work_state(BLE_ST_CONNECT);
|
||||
central_test_search_profile();
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case HCI_EVENT_DISCONNECTION_COMPLETE:
|
||||
log_info("Disconnected, reason: 0x%x", hci_event_disconnection_complete_get_reason(packet));
|
||||
con_handle = 0;
|
||||
ble_op_att_send_init(0, NULL, 0, 0);
|
||||
set_ble_work_state(BLE_ST_DISCONN);
|
||||
ble_central_test_scan_enable(1); // 断开后重新扫描
|
||||
break;
|
||||
|
||||
case GAP_EVENT_ADVERTISING_REPORT:
|
||||
if (ble_work_state != BLE_ST_SCAN) {
|
||||
break;
|
||||
}
|
||||
adv_report_t *report = (void *)&packet[2];
|
||||
if (resolve_adv_report(report->address, report->length, report->data)) {
|
||||
central_test_create_connection(report->address, report->address_type);
|
||||
}
|
||||
break;
|
||||
|
||||
case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
|
||||
log_info("MTU exchange complete, MTU = %d", att_event_mtu_exchange_complete_get_MTU(packet));
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 启动或停止扫描
|
||||
static int ble_central_test_scan_enable(u32 en) {
|
||||
if (en) {
|
||||
if (ble_work_state >= BLE_ST_SCAN) {
|
||||
return 0; // 已经在扫描或连接中
|
||||
}
|
||||
log_info("Starting scan...");
|
||||
set_ble_work_state(BLE_ST_SCAN);
|
||||
ble_op_set_scan_param(SCAN_ACTIVE, 48, 48);
|
||||
ble_op_scan_enable2(1, 0);
|
||||
} else {
|
||||
if (ble_work_state < BLE_ST_SCAN) {
|
||||
return 0; // 已经停止
|
||||
}
|
||||
log_info("Stopping scan...");
|
||||
set_ble_work_state(BLE_ST_IDLE);
|
||||
ble_op_scan_enable2(0, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 发送数据接口
|
||||
int xtell_ble_central_test_send_data(u8 *data, u16 len) {
|
||||
if (!con_handle || ble_work_state != BLE_ST_SEARCH_COMPLETE) {
|
||||
log_error("Not connected or profile search not complete.");
|
||||
return -1;
|
||||
}
|
||||
if (!target_write_handle) {
|
||||
log_error("No writable characteristic found.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = ble_op_att_send_data(target_write_handle, data, len, ATT_OP_WRITE_WITHOUT_RESPOND);
|
||||
if (ret == 0) {
|
||||
log_info("TX data, len=%d:", len);
|
||||
log_info_hexdump(data, len);
|
||||
} else {
|
||||
log_error("Failed to send data, ret=%d", ret);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 注册 BLE central 模式需要的回调
|
||||
static void ble_central_test_register_handlers(void) {
|
||||
log_info("Registering BLE central test handlers...");
|
||||
// 注意:le_device_db_init() 和 ble_stack_gatt_role() 可能已被RCSP的profile_init调用
|
||||
// 这里我们只覆盖 packet handlers
|
||||
gatt_client_init(); // 确保 gatt client 被初始化
|
||||
gatt_client_register_packet_handler(cbk_packet_handler);
|
||||
hci_event_callback_set(cbk_packet_handler);
|
||||
le_l2cap_register_packet_handler(cbk_packet_handler);
|
||||
ble_vendor_set_default_att_mtu(ATT_LOCAL_MTU_SIZE);
|
||||
}
|
||||
|
||||
// 测试总开关和初始化
|
||||
void xtell_ble_central_test_start(void) {
|
||||
if (ble_work_state != 0) {
|
||||
log_info("Test is already running.");
|
||||
return;
|
||||
}
|
||||
log_info("======== Xtell BLE Central Test Start ========");
|
||||
set_ble_work_state(BLE_ST_INIT_OK);
|
||||
ble_module_enable(1); // 使能 BLE 模块
|
||||
ble_central_test_register_handlers(); // 注册我们的回调
|
||||
ble_central_test_scan_enable(1); // 开始扫描
|
||||
}
|
||||
|
||||
#endif
|
||||
1622
apps/earphone/xtell_remote_control/ble_handler/client_handler.c
Normal file
1622
apps/earphone/xtell_remote_control/ble_handler/client_handler.c
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,37 @@
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//START -- 宏定义
|
||||
#define ENABLE_XLOG 1
|
||||
#ifdef xlog
|
||||
#undef xlog
|
||||
#endif
|
||||
#if ENABLE_XLOG
|
||||
#define xlog(format, ...) printf("[%s] " format, __func__, ##__VA_ARGS__)
|
||||
#else
|
||||
#define xlog(format, ...) ((void)0)
|
||||
#endif
|
||||
|
||||
//END -- 宏定义
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//START -- 变量定义
|
||||
|
||||
|
||||
|
||||
|
||||
//END -- 变量定义
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//START -- 函数定义
|
||||
|
||||
|
||||
//END -- 函数定义
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//实现
|
||||
Reference in New Issue
Block a user