2019-03-11

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函数相似,通过stack_manager_get_interface()返回的interface接口变量,找到并调用start_up_stack_async()函数

关于start_up_stack_async()函数

static void start_up_stack_async(void) {
  thread_post(management_thread, event_start_up_stack, NULL);
}

向线程栈发送消息,创建event_start_up_stackd线程,并绑定到线程工作队列management_thread后,等待执行

关注event_start_up_stack函数

//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);
}

该函数做如下工作
1.ensure_stack_is_initialized()
确保该队列已经被初始化(如果没进行过初始化就进行初始化操作)

2.module_start_up
启动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.启动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();
}
  1. bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME);
    新建一个新的工作线程队列bt_workqueue_thread

  2. thread_set_rt_priority(bt_workqueue_thread, BTU_TASK_RT_PRIORITY);
    设置bt_workqueue_thread

3.thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
向bt_workqueue_thread中添加btu_task_start_up线程,等待执行

关注btu_task_start_up执行函数

//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
}

进入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)文件内除了init接口外还有
A2DP_FindService等用于操作A2DP协议的接口函数

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");
创建message_loop_thread_线程队列

6.thread_set_rt_priority(message_loop_thread_, THREAD_RT_PRIORITY);
设置message_loop_thread_线程队列

7.thread_post(message_loop_thread_, btu_message_loop_run, nullptr);
将btu_message_loop_run线程加入message_loop_thread_,等待执行

关注btu_message_loop_run函数

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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容