2019-03-11

init流程.png

1.蓝牙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;
}

通过 stack_manager_get_interface()->init_stack();

const stack_manager_t* stack_manager_get_interface() {
  ensure_manager_initialized();
  return &interface;
}

可以看出stack_manager_get_interface()
1.执行一个ensure_manager_initialized()函数
2.之后返回一个const stack_manager_t类型的interface变量,

ensure_manager_initialized()初始化了了一个新的线程"stack_manager"

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

返回的interface变量,包含如下接口

static const stack_manager_t interface = {
init_stack, 
start_up_stack_async,                            
shut_down_stack_async,
 clean_up_stack,
get_stack_is_running
};

调用其中init_stack这一函数

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

这个函数中除了一些基本的信号量操作外,主要关注
thread_post(management_thread, event_init_stack, semaphore);
可以看出,这一函数应该创建了一个event_init_stack作为执行函数的线程,并将其推入了蓝牙的线程栈中

///system/bt/btif/src/stack_manager.cc

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

该函数做了如下事情
1.基本的信号量操作(关于蓝牙线程栈)
2.module相关函数(对其它模块的一些初始化检查操作)
3.btif_init_bluetooth();

主要关注btif_init_bluetooth()

//src/btif_core.cc
/*******************************************************************************
 *
 * Function         btif_init_bluetooth
 *
 * Description      Creates BTIF task and prepares BT scheduler for startup
 *
 * Returns          bt_status_t
 *
 ******************************************************************************/
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;
  }

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

该函数主要做如下工作:

  1. bte_main_boot_entry()

  2. bt_jni_workqueue_thread = thread_new(BT_JNI_WORKQUEUE_NAME);
    新建一个工作队列线

  3. thread_post(bt_jni_workqueue_thread, run_message_loop, nullptr);
    将run_message_loop创建线程(执行函数),并加入bt_jni_workqueue_thread这个工作队列中

bte_main_boot_entry()
主机和蓝牙芯片相关初始化入口

//main/bte_main.cc
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));
(了解base::Bind)
  module_init(get_module(STACK_CONFIG_MODULE));
}

1.检查并初始化INTEROP_MODULE模块
2.初始化,获取HCI接口 hci_layer_get_interface(),并返回hci变量

  1. hci->set_data_cb 监听 post_to_hci_message_loop 消息

hci_layer_get_interface()

//bt/hci/src/hci_layer.cc
const hci_t* hci_layer_get_interface() {
  buffer_allocator = buffer_allocator_get_interface();
//const allocator_t* buffer_allocator_get_interface() { return &interface; }

  btsnoop = btsnoop_get_interface();
//const btsnoop_t* btsnoop_get_interface() {return &interface;}


  packet_fragmenter = packet_fragmenter_get_interface();
/*
const packet_fragmenter_t* packet_fragmenter_get_interface() {
  controller = controller_get_interface();
  buffer_allocator = buffer_allocator_get_interface();
  return &interface;
}
*/


  init_layer_interface();

  return &interface;
}

//bt/hci/include/hci_layer.h
typedef struct hci_t {
  // Set the callback that the HCI layer uses to send data upwards
  void (*set_data_cb)(
      base::Callback<void(const tracked_objects::Location&, BT_HDR*)>
          send_data_cb);

  // Send a command through the HCI layer
  void (*transmit_command)(BT_HDR* command,
                           command_complete_cb complete_callback,
                           command_status_cb status_cb, void* context);

  future_t* (*transmit_command_futured)(BT_HDR* command);

  // Send some data downward through the HCI layer
  void (*transmit_downward)(uint16_t type, void* data);
} hci_t;

可以看出这个返回的这个interface可以完成Host和Controler之间的指令传输工作

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

推荐阅读更多精彩内容