当前位置: 首页 > ai >正文

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

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_eventvoid 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;
http://www.xdnf.cn/news/5780.html

相关文章:

  • 前端开发避坑指南:React 代理配置常见问题与解决方案
  • BFS算法篇——打开智慧之门,BFS算法在拓扑排序中的诗意探索(上)
  • 机器学习——聚类算法练习题
  • [Java实战]Spring Boot 3构建 RESTful 风格服务(二十)
  • java使用 FreeMarker 模板生成包含图片的 `.doc` 文件
  • RustDesk:开源电脑远程控制软件
  • 端侧智能重构智能监控新路径 | 2025 高通边缘智能创新应用大赛第三场公开课来袭!
  • 霍夫圆变换全面解析(OpenCV)
  • 6. 多列布局/用户界面 - 杂志风格文章布局
  • 手机换IP真的有用吗?可以干什么?
  • spark-local模式
  • WM_TIMER定时器消息优先级低,可能会被系统丢弃,导致定时任务无法正常执行
  • T-BOX硬件方案深度解析:STM32与SD NAND Flash存储的完美搭配
  • Linux中find命令用法核心要点提炼
  • spark-standalone
  • http断点续传
  • Games101作业四
  • 在Ubuntu服务器上部署Label Studio
  • 从SAM看交互式分割与可提示分割的区别与联系:Interactive Segmentation Promptable Segmentation
  • Java基础(IO)
  • Android Native 之 自定义进程
  • 【氮化镓】电子辐照下温度对GaN位移阈能的影响
  • 开源网络地图可视化第五章学习指南
  • 【认知思维】光环效应:第一印象的持久力量
  • MySQL 8.0 OCP 1Z0-908 题目解析(2)
  • SpringBoot整合MQTT实战:基于EMQX实现双向设备通信(附源码)
  • QEMU模拟32位ARM实现自定义系统调用
  • 基于STM32、HAL库的PCM3060PWR 音频接口芯片驱动程序设计
  • 深度学习计算
  • GOOSE 协议中MAC配置