Bluedroid 蓝牙初始化init

蓝牙初始化,创建了stack_manager线程和bt_jni_workqueue线程,并初始化各个模块。
在这里插入图片描述

1、Bluetooth.cc init

static int init(bt_callbacks_t* callbacks) {
LOG_INFO(LOG_TAG, “%s”, func);

if (interface_ready()) return BT_STATUS_DONE;

#ifdef BLUEDROID_DEBUG
allocation_tracker_init();
#endif

bt_hal_cbacks = callbacks;
stack_manager_get_interface()->init_stack();
btif_debug_init();
return BT_STATUS_SUCCESS;
}

2、stack_manager_get_interface()->init_stack()

2.1stack_manager_get_interface–创建stack_manager线程
const stack_manager_t* stack_manager_get_interface() {
ensure_manager_initialized();
return &interface;
}
static void ensure_manager_initialized(void) {
if (management_thread) return;

management_thread = thread_new(“stack_manager”);
if (!management_thread) {
LOG_ERROR(LOG_TAG, “%s unable to create stack management thread”, func);
return;
}
}

2.2 init_stack初始化协议栈,将event_init_stack事件放到stack_manager线程的工作队列
static void init_stack(void) {
// This is a synchronous process. Post it to the thread though, so
// state modification only happens there. Using the thread to perform
// all stack operations ensures that the operations are done serially
// and do not overlap.
semaphore_t* semaphore = semaphore_new(0);
//创建线程
thread_post(management_thread, event_init_stack, semaphore);
semaphore_wait(semaphore);
semaphore_free(semaphore);
}

3、event_init_stack里面初始化加载模块,初始化btif

static void event_init_stack(void* context) {
semaphore_t* semaphore = (semaphore_t*)context;

LOG_INFO(LOG_TAG, “%s is initializing the stack”, func);

if (stack_is_initialized) {
LOG_INFO(LOG_TAG, “%s found the stack already in initialized state”,
func);
} else {
module_management_start();

module_init(get_module(OSI_MODULE));
module_init(get_module(BT_UTILS_MODULE));
module_init(get_module(BTIF_CONFIG_MODULE));
**btif_init_bluetooth();**

// stack init is synchronous, so no waiting necessary here
stack_is_initialized = true;

}

LOG_INFO(LOG_TAG, “%s finished”, func);

if (semaphore) semaphore_post(semaphore);
}

4、btif_init_bluetooth 创建btif任务并启动调度程序

bt_status_t btif_init_bluetooth() {
LOG_INFO(LOG_TAG, “%s entered”, func);

bte_main_boot_entry();

bt_jni_workqueue_thread = thread_new(BT_JNI_WORKQUEUE_NAME);
if (bt_jni_workqueue_thread == NULL) {
LOG_ERROR(LOG_TAG, “%s Unable to create thread %s”, func,
BT_JNI_WORKQUEUE_NAME);
goto error_exit;
}
//创建jni workqueue线程
thread_post(bt_jni_workqueue_thread, run_message_loop, nullptr);

LOG_INFO(LOG_TAG, “%s finished”, func);
return BT_STATUS_SUCCESS;

error_exit:;
thread_free(bt_jni_workqueue_thread);

bt_jni_workqueue_thread = NULL;

return BT_STATUS_FAIL;
}
4.1 bte_main_boot_entry
加载hci设置回调处理函数
void bte_main_boot_entry(void) {
module_init(get_module(INTEROP_MODULE));

hci = hci_layer_get_interface();
if (!hci) {
LOG_ERROR(LOG_TAG, “%s could not get hci layer interface.”, func);
return;
}

hci->set_data_cb(base::Bind(&post_to_hci_message_loop));

module_init(get_module(STACK_CONFIG_MODULE));
}
4.2 run_message_loop
将回调事件消息在messge loop消息队列循环中post到jni回调
void run_message_loop(UNUSED_ATTR void* context) {
LOG_INFO(LOG_TAG, “%s entered”, func);
btif_thread_id_ = PlatformThread::CurrentId();

// TODO(jpawlowski): exit_manager should be defined in main(), but there is no
// main method.
// It is therefore defined in bt_jni_workqueue_thread, and will be deleted
// when we free it.
base::AtExitManager exit_manager;

message_loop_ = new base::MessageLoop(base::MessageLoop::Type::TYPE_DEFAULT);

// Associate this workqueue thread with JNI.
message_loop_->task_runner()->PostTask(FROM_HERE,
base::Bind(&btif_jni_associate));

jni_run_loop = new base::RunLoop();
jni_run_loop->Run();

delete message_loop_;
message_loop_ = NULL;

delete jni_run_loop;
jni_run_loop = NULL;

btif_thread_id_ = -1;
LOG_INFO(LOG_TAG, “%s finished”, func);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值