1、bluetooth.cc enable执行打开蓝牙

static int enable(bool start_restricted) {
LOG_INFO(LOG_TAG, "%s: start restricted = %d", __func__, start_restricted);
restricted_mode = start_restricted;
if (!interface_ready()) return BT_STATUS_NOT_READY;
//异步启动协议栈
stack_manager_get_interface()->start_up_stack_async();
return BT_STATUS_SUCCESS;
}
stack_manager线程中加入启动协议栈的事件event_start_up_stack
static void start_up_stack_async(void) {
thread_post(management_thread, event_start_up_stack, NULL);
}
2、启动协议栈
static void event_start_up_stack(UNUSED_ATTR void* context) {
if (stack_is_running) {
LOG_INFO(LOG_TAG, "%s stack already brought up", __func__);
return;
}
ensure_stack_is_initialized();
LOG_INFO(LOG_TAG, "%s is bringing up the stack", __func__);
future_t* local_hack_future = future_new();
hack_future = local_hack_future;
// Include this for now to put btif config into a shutdown-able state
module_start_up(get_module(BTIF_CONFIG_MODULE));
//enable协议栈
bte_main_enable();
if (future_await(local_hack_future) != FUTURE_SUCCESS) {
LOG_ERROR(LOG_TAG, "%s failed to start up the stack", __func__);
stack_is_running = true; // So stack shutdown actually happens
event_shut_down_stack(NULL);
return;
}
stack_is_running = true;
LOG_INFO(LOG_TAG, "%s finished", __func__);
//通知协议栈启动消息
btif_thread_post(event_signal_stack_up, NULL);
}
2.1 bte_main_enable
创建bte task
void bte_main_enable() {
APPL_TRACE_DEBUG("%s", __func__);
module_start_up(get_module(BTSNOOP_MODULE));
module_start_up(get_module(HCI_MODULE));
BTU_StartUp();
}
2.2 BTU_StartUp-初始化BTU 控制
void BTU_StartUp(void) {
btu_trace_level = HCI_INITIAL_TRACE_LEVEL;
//创建bt_workqueue线程
bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME);
if (bt_workqueue_thread == NULL) goto error_exit;
thread_set_rt_priority(bt_workqueue_thread, BTU_TASK_RT_PRIORITY);
// Continue startup on bt workqueue thread.
thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
return;
error_exit:;
LOG_ERROR(LOG_TAG, "%s Unable to allocate resources for bt_workqueue",
__func__);
BTU_ShutDown();
}
2.3 btu_task_start_up启动btu
void btu_task_start_up(UNUSED_ATTR void* context) {
LOG(INFO) << "Bluetooth chip preload is complete";
/* Initialize the mandatory core stack control blocks
(BTU, BTM, L2CAP, and SDP)
*/
btu_init_core();
/* Initialize any optional stack components */
BTE_InitStack();
bta_sys_init();
/* Initialise platform trace levels at this point as BTE_InitStack() and
* bta_sys_init()
* reset the control blocks and preset the trace level with
* XXX_INITIAL_TRACE_LEVEL
*/
module_init(get_module(BTE_LOGMSG_MODULE));
//创建btu_message_loop线程,用于处理btu消息
message_loop_thread_ = thread_new("btu message loop");
if (!message_loop_thread_) {
LOG(FATAL) << __func__ << " unable to create btu message loop thread.";
}
thread_set_rt_priority(message_loop_thread_, THREAD_RT_PRIORITY);
//btu_message_loop_run 添加到btu message线程的工作队列
thread_post(message_loop_thread_, btu_message_loop_run, nullptr);
}
2.4 btu_message_loop_run
创建消息队列发送消息btif_init_ok
void btu_message_loop_run(UNUSED_ATTR void* context) {
message_loop_ = new base::MessageLoop();
run_loop_ = new base::RunLoop();
// Inform the bt jni thread initialization is ok.
message_loop_->task_runner()->PostTask(
FROM_HERE, base::Bind(base::IgnoreResult(&btif_transfer_context),
btif_init_ok, 0, nullptr, 0, nullptr));
run_loop_->Run();
delete message_loop_;
message_loop_ = NULL;
delete run_loop_;
run_loop_ = NULL;
}
void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char* p_param) {
BTIF_TRACE_DEBUG("btif_task: received trigger stack init event");
btif_dm_load_ble_local_keys();
BTA_EnableBluetooth(bte_dm_evt);
}
tBTA_STATUS BTA_EnableBluetooth(tBTA_DM_SEC_CBACK* p_cback) {
/* Bluetooth disabling is in progress */
if (bta_dm_cb.disabling) return BTA_FAILURE;
bta_sys_register(BTA_ID_DM_SEARCH, &bta_dm_search_reg);
/* if UUID list is not provided as static data */
bta_sys_eir_register(bta_dm_eir_update_uuid);
bta_sys_cust_eir_register(bta_dm_eir_update_cust_uuid);
do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_enable, p_cback));
return BTA_SUCCESS;
}
2.5 bta_dm_enable-初始化蓝牙设备管理
void bta_dm_enable(tBTA_DM_SEC_CBACK* p_sec_cback) {
/* if already in use, return an error */
if (bta_dm_cb.is_bta_dm_active) {
tBTA_DM_SEC enable_event;
APPL_TRACE_WARNING("%s Device already started by another application",
__func__);
memset(&enable_event, 0, sizeof(tBTA_DM_SEC));
enable_event.enable.status = BTA_FAILURE;
if (p_sec_cback != NULL) p_sec_cback(BTA_DM_ENABLE_EVT, &enable_event);
return;
}
/* first, register our callback to SYS HW manager */
bta_sys_hw_register(BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback);
/* make sure security callback is saved - if no callback, do not erase the
previous one,
it could be an error recovery mechanism */
if (p_sec_cback != NULL) bta_dm_cb.p_sec_cback = p_sec_cback;
/* notify BTA DM is now active */
bta_dm_cb.is_bta_dm_active = true;
/* send a message to BTA SYS */
tBTA_SYS_HW_MSG* sys_enable_event =
(tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
sys_enable_event->hdr.event = BTA_SYS_API_ENABLE_EVT;
sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH;
bta_sys_sendmsg(sys_enable_event);
}

1555

被折叠的 条评论
为什么被折叠?



