Fluoride Bluetooth stack

25 年 11 月 3 日 星期一 (已编辑)
624 字
4 分钟

stack_manager

stack_manager 核心就围绕着 stack_manager_t.h 中定义的 stack_manager_t 相关管理方法:

  • init_stack 初始化协议栈
  • start_up_stack_async 启动协议栈
  • shut_down_stack_async 关闭协议栈
  • clean_up_stack 清空协议栈(关闭并清理资源)
  • start_up_rust_module_async 启动rust模块
  • shut_down_rust_module_async 关闭rust模块
system/btif/include/stack_manager_t.hc
typedef struct {
  void (*init_stack)(bluetooth::core::CoreInterface*);
  void (*start_up_stack_async)(bluetooth::core::CoreInterface*, ProfileStartCallback,
                               ProfileStopCallback);
  void (*shut_down_stack_async)(ProfileStopCallback);
  void (*clean_up_stack)(ProfileStopCallback);
  void (*start_up_rust_module_async)(std::promise<void> promise);
  void (*shut_down_rust_module_async)();

  bool (*get_stack_is_running)(void);
} stack_manager_t;

const stack_manager_t* stack_manager_get_interface();

stack_manager.cc 提供了相关方法的实现:

system/btif/src/stack_manager.ccc
static const stack_manager_t interface = {
        init_stack,          start_up_stack_async,       shut_down_stack_async,
        clean_up_stack,      start_up_rust_module_async, shut_down_rust_module_async,
        get_stack_is_running};

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

纵观 stack_manger.cc 的实现, 每个 stack_manager_t 中定义的方法都通过对应的事件方法在 management_thread 中运行:

system/btif/src/stack_manager.ccc
static void event_init_stack(std::promise<void> promise, bluetooth::core::CoreInterface* interface);
static void event_start_up_stack(bluetooth::core::CoreInterface* interface,
                                 ProfileStartCallback startProfiles,
                                 ProfileStopCallback stopProfiles);
static void event_shut_down_stack(ProfileStopCallback stopProfiles);
static void event_clean_up_stack(std::promise<void> promise, ProfileStopCallback stopProfiles);
static void event_start_up_rust_module(std::promise<void> promise);
static void event_shut_down_rust_module();

static void event_signal_stack_up(void* context);
static void event_signal_stack_down(void* context);

event_init_stack 初始化协议栈

初始化协议栈主要是完成以下内容:

  • 启动各种模块
  • 启动主线程 main_thread_start_up
  • 启动JNI线程 btif_init_bluetooth
system/btif/src/stack_manager.ccc
static void init_stack_internal(bluetooth::core::CoreInterface* interface) {
  // all callbacks out of libbluetooth-core happen via this interface
  interfaceToProfiles = interface;

  module_management_start();

  main_thread_start_up();

  module_init(get_local_module(DEVICE_IOT_CONFIG_MODULE));
  module_init(get_local_module(OSI_MODULE));
  module_start_up(get_local_module(GD_SHIM_MODULE));
  module_init(get_local_module(BTIF_CONFIG_MODULE));
  btif_init_bluetooth();

  module_init(get_local_module(INTEROP_MODULE));
  module_init(get_local_module(STACK_CONFIG_MODULE));

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

通过源码可以看到定义了这些module(具体module的解析需要查看module.h module.cc):

system/btif/src/stack_manager.ccc
extern const module_t bt_utils_module;
extern const module_t btif_config_module;
extern const module_t gd_shim_module;
extern const module_t interop_module;
extern const module_t osi_module;
extern const module_t rust_module;
extern const module_t stack_config_module;
extern const module_t device_iot_config_module;

struct module_lookup {
  const char* name;
  const module_t* module;
};

const struct module_lookup module_table[] = {
        {BTIF_CONFIG_MODULE, &btif_config_module},
        {GD_SHIM_MODULE, &gd_shim_module},
        {INTEROP_MODULE, &interop_module},
        {OSI_MODULE, &osi_module},
        {RUST_MODULE, &rust_module},
        {STACK_CONFIG_MODULE, &stack_config_module},
        {DEVICE_IOT_CONFIG_MODULE, &device_iot_config_module},
        {NULL, NULL},
};

inline const module_t* get_local_module(const char* name) {
  size_t len = strlen(name);

  for (const struct module_lookup* l = module_table; l->module; l++) {
    if (strncmp(l->name, name, len) == 0) {
      return l->module;
    }
  }

  fatal("Cannot find module {}, aborting", name);
  return nullptr;
}

event_start_up_stack 启动协议栈

这里主要就是各种profile的初始化了(TODO):

system/btif/src/stack_manager.ccc
static void event_start_up_stack(bluetooth::core::CoreInterface* interface,
                                 ProfileStartCallback startProfiles,
                                 ProfileStopCallback stopProfiles) {
  if (stack_is_running) {
    info("stack already brought up");
    return;
  }

  ensure_stack_is_initialized(interface);

  info("is bringing up the stack");
  future_t* local_hack_future = future_new();
  hack_future = local_hack_future;

  info("Gd shim module enabled");
  get_btm_client_interface().lifecycle.btm_init();
  module_start_up(get_local_module(BTIF_CONFIG_MODULE));

  l2c_init();
  sdp_init();
  gatt_init();
  SMP_Init(get_btm_client_interface().security.BTM_GetSecurityMode());
  get_btm_client_interface().lifecycle.btm_ble_init();

  RFCOMM_Init();
  GAP_Init();
  AIS_Init();

  startProfiles();

  bta_sys_init();

  btif_init_ok();
  BTA_dm_init();
  bta_dm_enable(btif_dm_sec_evt, btif_dm_acl_evt);

  btm_acl_device_down();
  get_btm_client_interface().lifecycle.BTM_reset_complete();

  BTA_dm_on_hw_on();

  if (future_await(local_hack_future) != FUTURE_SUCCESS) {
    error("failed to start up the stack");
    stack_is_running = true;  // So stack shutdown actually happens
    event_shut_down_stack(stopProfiles);
    return;
  }

  if (!com::android::bluetooth::flags::only_start_scan_during_ble_on()) {
    info("Starting rust module");
    module_start_up(get_local_module(RUST_MODULE));
    if (com::android::bluetooth::flags::channel_sounding_in_stack()) {
      bluetooth::ras::GetRasServer()->Initialize();
      bluetooth::ras::GetRasClient()->Initialize();
    }
  }

  stack_is_running = true;
  info("finished");
  do_in_jni_thread(base::BindOnce(event_signal_stack_up, nullptr));
}

event_shut_down_stack 关闭协议栈

关闭各种profile, 关闭协议栈不会重置协议栈的初始化状态

event_clean_up_stack 清理协议栈

关闭各种profile并清除module, 清理协议栈会重置协议栈为未初始化状态

start_up_rust_module_async/shut_down_rust_module_async Rust模块初始化及关闭

Google 在 Android 11 中引入了新的 Gabeldorsche (GD) stack. 在新的GD协议栈中, 引入了Rust实现的模块.

参考

android bluetooth 协议分析

文章标题:Fluoride Bluetooth stack

文章作者:zabbits

文章链接:https://zabbits.com/posts/bluetooth/fluoride-bluetooth-stack[复制]

最后修改时间:


商业转载请联系站长获得授权,非商业转载请注明本文出处及文章链接,您可以自由地在任何媒体以任何形式复制和分发作品,也可以修改和创作,但是分发衍生作品时必须采用相同的许可协议。
本文采用CC BY-NC-SA 4.0进行许可。