【android bluetooth 框架分析 02】【Module详解 7】【VendorSpecificEventManager 模块介绍】

发布于:2025-05-14 ⋅ 阅读:(15) ⋅ 点赞:(0)

1. 背景

我们在 gd_shim_module 介绍章节中,看到 我们将 VendorSpecificEventManager 模块加入到了 modules 中。

// system/main/shim/stack.cc
modules.add<hci::VendorSpecificEventManager>();

在 ModuleRegistry::Start 函数中我们对 加入的所有 module 挨个初始化。
而在该函数中启动一个 module 都要执行那下面几步:

  1. 创建module 实体

    • Module* instance = module->ctor_();
  2. 将 当前 module 实体和 gd_stack_thread 线程绑定

    • set_registry_and_handler(instance, thread);
  3. 启动当前模块所依赖的所有子模块。

    • instance->ListDependencies(&instance->dependencies_);
    • Start(&instance->dependencies_, thread);
  4. 最后调用自己的 Start() 函数

    • instance->Start();
  5. 将module 实体加入到 started_modules_

    • started_modules_[module] = instance;

本篇文章就拿 hal::VendorSpecificEventManager 模块来具体分析一下 他的启动。

2. modules.add

我们先来看一下 在调用 modules.add 时, 到底做了那些事情。

modules.add<hal::VendorSpecificEventManager>();
class ModuleList {
 friend Module;
 friend ModuleRegistry;

public:
 template <class T>
 void add() {
   list_.push_back(&T::Factory); // add 时 添加的是 hal::VendorSpecificEventManager::Factory
 }

 private:
  std::vector<const ModuleFactory*> list_;
};
  • 从代码中不难发现, 我们是将 hal::HciLayer::Factory 加入到 list_ 中的。
// system/gd/hci/vendor_specific_event_manager.cc
const ModuleFactory VendorSpecificEventManager::Factory =
    ModuleFactory([]() { return new VendorSpecificEventManager(); });// 这里在创建 ModuleFactory 对象时, 传入了一个 函数, 这个函数 去 new VendorSpecificEventManager 对象
  • 这里在创建 ModuleFactory 对象时, 传入了一个 函数,但是并没有去调用这个函数。
    • 这个函数的目的是 去 new VendorSpecificEventManager 对象
class ModuleFactory {
 friend ModuleRegistry;
 friend FuzzTestModuleRegistry;

public:
 ModuleFactory(std::function<Module*()> ctor);

private:
 std::function<Module*()> ctor_;
};

//  system/gd/module.cc
ModuleFactory::ModuleFactory(std::function<Module*()> ctor) : ctor_(ctor) {
}
  • 在创建 ModuleFactory 对象时, 也仅仅是将 如下的函数赋值给了 ModuleFactory::ctor_ 函数指针。
[]() { 
	return new VendorSpecificEventManager(); 

}

3. 模块具体启动流程

1. 创建module 实体

  1. 创建module 实体
    • Module* instance = module->ctor_();
[]() { 
	return new VendorSpecificEventManager(); 

}


  • 这里就会去实际触发 该函数,去创建 VendorSpecificEventManager 对象。
  • 也就是说, modules.addhal::VendorSpecificEventManager() 模块对应的 实体其实是 VendorSpecificEventManager 对象。
class VendorSpecificEventManager : public ::bluetooth::Module {

}
  • VendorSpecificEventManager 继承 Module
VendorSpecificEventManager::VendorSpecificEventManager() {
  pimpl_ = std::make_unique<impl>(this);
}
  • 在 VendorSpecificEventManager 构造函数里面, 创建了 VendorSpecificEventManager::impl 对象。

2. 将 当前 module 实体和 gd_stack_thread 线程绑定

  1. 将 当前 module 实体和 gd_stack_thread 线程绑定
    • set_registry_and_handler(instance, thread);
void ModuleRegistry::set_registry_and_handler(Module* instance, Thread* thread) const {
  instance->registry_ = this;
  instance->handler_ = new Handler(thread);
}
  • 将我们的 gd_stack_thread 对应的 handle 直接保存在 Module->handler_ 中。
Handler* Module::GetHandler() const {
  ASSERT_LOG(handler_ != nullptr, "Can't get handler when it's not started");
  return handler_;
}
  • 通过 Module::GetHandler() 来获取当前 handler_

3.启动当前模块所依赖的所有子模块

  1. 启动当前模块所依赖的所有子模块。
    • instance->ListDependencies(&instance->dependencies_);
    • Start(&instance->dependencies_, thread);
// system/gd/hci/vendor_specific_event_manager.cc
void VendorSpecificEventManager::ListDependencies(ModuleList* list) const {
  list->add<hci::HciLayer>();
  list->add<hci::Controller>();
}
  • 从上面的代码中可以看到 VendorSpecificEventManager 模块依赖
    • HciLayer 模块, 在之前已经启动了。
    • Controller 模块,之前没有启动。 此时就会触发 Controller 模块的 启动流程。 这个我们在下节讨论。

4. 最后调用自己的 Start() 函数

  1. 最后调用自己的 Start() 函数
    • instance->Start();

1. VendorSpecificEventManager::Start



void VendorSpecificEventManager::Start() {
  pimpl_->start(GetHandler(), GetDependency<hci::HciLayer>(), GetDependency<hci::Controller>());
}
  • 直接调用了 VendorSpecificEventManager::impl::start 函数。

2. VendorSpecificEventManager::impl::start

// 如下是 VendorSpecificEventManager::impl::start 的实现

  void start(os::Handler* handler, hci::HciLayer* hci_layer, hci::Controller* controller) {
    module_handler_ = handler;
    hci_layer_ = hci_layer;
    controller_ = controller;

    hci_layer_->RegisterEventHandler(
        EventCode::VENDOR_SPECIFIC, handler->BindOn(this, &VendorSpecificEventManager::impl::on_vendor_specific_event)); // 向 hciLayer 层,注册 VENDOR_SPECIFIC 事件的回调
    vendor_capabilities_ = controller->GetVendorCapabilities(); // 获取 contoller 芯片支持的能力

  }

1. 注册 VENDOR_SPECIFIC 事件的回调

我们在介绍 hciLayer 模块时, 就介绍过 RegisterEventHandler 的使用。 这里不再赘述。
当我们收到 VENDOR_SPECIFIC 相关的事件后,就会回调 VendorSpecificEventManager::impl::on_vendor_specific_event 方法

2. on_vendor_specific_event

  void on_vendor_specific_event(EventView event_view) {
    auto vendor_specific_event_view = VendorSpecificEventView::Create(event_view);
    ASSERT(vendor_specific_event_view.IsValid());
    VseSubeventCode vse_subevent_code = vendor_specific_event_view.GetSubeventCode();


    if (subevent_handlers_.find(vse_subevent_code) == subevent_handlers_.end()) {
      LOG_WARN("Unhandled vendor specific event of type 0x%02hhx", vse_subevent_code);
      return;
    }
    // 会从 subevent_handlers_ 中继续找到 子事件的回调。
    subevent_handlers_[vse_subevent_code].Invoke(vendor_specific_event_view);
  }

3. subevent_handlers_

std::map<VseSubeventCode, common::ContextualCallback<void(VendorSpecificEventView)>> subevent_handlers_;

  • subevent_handlers_ 是一个 map.

那么 subevent_handlers_ 中的子事件 回调是怎么注册进来的?


// VendorSpecificEventManager::impl::register_event

  void register_event(VseSubeventCode event, common::ContextualCallback<void(VendorSpecificEventView)> handler) {
    ASSERT_LOG(
        subevent_handlers_.count(event) == 0,
        "Can not register a second handler for %02hhx (%s)",
        event,
        VseSubeventCodeText(event).c_str());
    subevent_handlers_[event] = handler;
  }

void VendorSpecificEventManager::RegisterEventHandler(
    VseSubeventCode event, common::ContextualCallback<void(VendorSpecificEventView)> handler) {
  CallOn(pimpl_.get(), &impl::register_event, event, handler);
}

  • 整个 VendorSpecificEventManager 很简单,主要就是 对外提供 RegisterEventHandler 方法。管理厂商相关事件的回调。

VendorSpecificEventManager::RegisterEventHandler 函数主要用在 LeScanningManager 和 LeScanningManager 模块。 等介绍 这俩模块时,我们在展开介绍。

5.将module 实体加入到 started_modules_

  1. 将module 实体加入到 started_modules_
    • started_modules_[module] = instance;