2.enable()流程
//bluetooth.cc btif\src
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;
}
和init函數(shù)相似,通過stack_manager_get_interface()返回的interface接口變量缀棍,找到并調(diào)用start_up_stack_async()函數(shù)
關(guān)于start_up_stack_async()函數(shù)
static void start_up_stack_async(void) {
thread_post(management_thread, event_start_up_stack, NULL);
}
向線程棧發(fā)送消息坞生,創(chuàng)建event_start_up_stackd線程岔留,并綁定到線程工作隊(duì)列management_thread后吨拗,等待執(zhí)行
關(guān)注event_start_up_stack函數(shù)
//stack_manager.cc btif\src
// Synchronous function to start up the stack
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));
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);
}
該函數(shù)做如下工作
1.ensure_stack_is_initialized()
確保該隊(duì)列已經(jīng)被初始化(如果沒進(jìn)行過初始化就進(jìn)行初始化操作)
2.module_start_up
啟動(dòng)BTIF_CONFIG_MODULE模塊
3.bte_main_enable()
//bte_main.cc
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();
}
1.啟動(dòng)BTSNOOP_MODULE庵佣,HCI_MODULE模塊
2.BTU_StartUp();
void BTU_StartUp(void) {
btu_trace_level = HCI_INITIAL_TRACE_LEVEL;
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();
}
bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME);
新建一個(gè)新的工作線程隊(duì)列bt_workqueue_threadthread_set_rt_priority(bt_workqueue_thread, BTU_TASK_RT_PRIORITY);
設(shè)置bt_workqueue_thread
3.thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
向bt_workqueue_thread中添加btu_task_start_up線程歉胶,等待執(zhí)行
關(guān)注btu_task_start_up執(zhí)行函數(shù)
//btu_task.cc stack\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));
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);
thread_post(message_loop_thread_, btu_message_loop_run, nullptr);
}
1.btu_init_core()
void btu_init_core(void) {
/* Initialize the mandatory core stack components */
btm_init();
l2c_init();
sdp_init();
gatt_init();
SMP_Init();
btm_ble_init();
}
2.BTE_InitStack();
void BTE_InitStack(void) {
/* Initialize the optional stack components */
RFCOMM_Init();
/**************************
* BNEP and its profiles **
**************************/
#if (BNEP_INCLUDED == TRUE)
BNEP_Init();
#if (PAN_INCLUDED == TRUE)
PAN_Init();
#endif /* PAN */
#endif /* BNEP Included */
/**************************
* AVDT and its profiles **
**************************/
A2DP_Init();
AVRC_Init();
///[android-aries-dev](http://172.16.26.250:18080/source/xref/android-aries-dev/)/[system](http://172.16.26.250:18080/source/xref/android-aries-dev/system/)/[bt](http://172.16.26.250:18080/source/xref/android-aries-dev/system/bt/)/[stack](http://172.16.26.250:18080/source/xref/android-aries-dev/system/bt/stack/)/[avrc](http://172.16.26.250:18080/source/xref/android-aries-dev/system/bt/stack/avrc/)/[avrc_sdp.cc](http://172.16.26.250:18080/source/xref/android-aries-dev/system/bt/stack/avrc/avrc_sdp.cc)
/***********
* Others **
***********/
GAP_Init();
#if (HID_HOST_INCLUDED == TRUE)
HID_HostInit();
#endif
#if (MCA_INCLUDED == TRUE)
MCA_Init();
#endif
}
進(jìn)入A2DP_Init()看一下
void A2DP_Init(void) {
memset(&a2dp_cb, 0, sizeof(tA2DP_CB));
a2dp_cb.avdt_sdp_ver = AVDT_VERSION;
#if defined(A2DP_INITIAL_TRACE_LEVEL)
a2dp_cb.trace_level = A2DP_INITIAL_TRACE_LEVEL;
#else
a2dp_cb.trace_level = BT_TRACE_LEVEL_NONE;
#endif
}
可以看到(a2dp_api.cc stack\a2dp)文件內(nèi)除了init接口外還有
A2DP_FindService等用于操作A2DP協(xié)議的接口函數(shù)
3.bta_sys_init();
void bta_sys_init(void) {
memset(&bta_sys_cb, 0, sizeof(tBTA_SYS_CB));
appl_trace_level = APPL_INITIAL_TRACE_LEVEL;
/* register BTA SYS message handler */
bta_sys_register(BTA_ID_SYS, &bta_sys_hw_reg);
/* register for BTM notifications */
BTM_RegisterForDeviceStatusNotif(&bta_sys_hw_btm_cback);
#if (defined BTA_AR_INCLUDED) && (BTA_AR_INCLUDED == TRUE)
bta_ar_init();
#endif
}
4.module_init(get_module(BTE_LOGMSG_MODULE));
BTE_LOGMSG_MODULE模塊初始化
5.message_loop_thread_ = thread_new("btu message loop");
創(chuàng)建message_loop_thread_線程隊(duì)列
6.thread_set_rt_priority(message_loop_thread_, THREAD_RT_PRIORITY);
設(shè)置message_loop_thread_線程隊(duì)列
7.thread_post(message_loop_thread_, btu_message_loop_run, nullptr);
將btu_message_loop_run線程加入message_loop_thread_,等待執(zhí)行
關(guān)注btu_message_loop_run函數(shù)
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;
}