【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.mzph.cn/web/79695.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

小刚说C语言刷题—1080质因子

1.题目描述 任意输入一正整数 N &#xff0c;求出它的所有质因子。如&#xff1a;10&#xff1d;25&#xff0c;20&#xff1d;225。 输入 输入只有一行&#xff0c;包括 11个整数 n (1≤n≤32768) 输出 输出若干行&#xff0c;按从小到大的顺序给出这个数的所有质因子&am…

C语言中的宏

1.防止头文件重复包含 1.#pragma once #pragma once 是一个编译器指令&#xff0c;用于防止头文件被重复包含。它的核心作用是通过简单语法替代传统的头文件保护宏&#xff08;#ifndef/#define/#endif&#xff09;&#xff0c;提升代码简洁性和可维护性。 作用详解 防止重复…

MapReduce 模型

‌引言‌ MapReduce 是分布式计算领域的里程碑式模型&#xff0c;由 Google 在 2004 年论文中首次提出&#xff0c;旨在简化海量数据处理的复杂性。其核心思想是通过函数式编程的 ‌Map‌ &#xff08;映射&#xff09;和 ‌Reduce‌ &#xff08;归约&#xff09;阶段&#x…

Linux文件编程——标准库函数fopen、fread、fwrite等函数

1. fopen — 打开文件 函数原型&#xff1a; FILE *fopen(const char *filename, const char *mode);参数&#xff1a; filename&#xff1a;要打开的文件名&#xff0c;可以是相对路径或绝对路径。 mode&#xff1a;文件打开模式&#xff0c;表示文件的操作方式&#xff08…

从 Git 到 GitHub - 使用 Git 进行版本控制 - Git 常用命令

希望本贴能从零开始带您一起学习如何使用 Git 进行版本控制&#xff0c;并结合远程仓库 GitHub。这会是一个循序渐进的指南&#xff0c;我们开始吧&#xff01; 学习 Git 和 GitHub 的路线图&#xff1a; 理解核心概念&#xff1a;什么是版本控制&#xff1f;Git 是什么&…

2025.05.11拼多多机考真题算法岗-第四题

📌 点击直达笔试专栏 👉《大厂笔试突围》 💻 春秋招笔试突围在线OJ 👉 笔试突围OJ 04. 记忆碎片重组 问题描述 卢小姐正在开发一款名为"记忆碎片"的游戏,玩家需要分析混乱的记忆数据,推测出形成这些记忆的原始序列。游戏中,记忆数据存储在一个特殊的数…

Android Exoplayer多路不同时长音视频混合播放

在上一篇Android Exoplayer 实现多个音视频文件混合播放以及音轨切换中我们提到一个问题&#xff0c;如果视频和音频时长不一致&#xff0c;特别是想混合多个音频和多个视频时就会出问题&#xff0c;无法播放。报错如下&#xff1a; E/ExoPlayerImplInternal(11191): Playback…

Datawhale PyPOTS时间序列5月第1次笔记

课程原地址&#xff1a; https://github.com/WenjieDu/PyPOTS&#xff08;Package地址&#xff09; https://github.com/WenjieDu/BrewPOTS/tree/datawhale/202505_datawhale&#xff08;Tutorial地址&#xff09; 2.1 PyPOTS简介 PyPOTS 是一个专为处理部分观测时间序列&a…

网安学途—流量分析 attack.pcap

attack.pacp 使用Wireshark查看并分析虚拟机windows 7桌面下的attack.pcapng数据包文件&#xff0c;通过分析数据包attack.pcapng找出黑客的IP地址&#xff0c;并将黑客的IP地址作为FLAG &#xff08;形式&#xff1a;[IP地址]&#xff09;提交&#xff1a; 过滤器筛选&#x…

【大模型】DeepResearcher:通用智能体通过强化学习探索优化

DeepResearcher&#xff1a;通过强化学习在真实环境中扩展深度研究 一、引言二、技术原理&#xff08;一&#xff09;强化学习与深度研究代理&#xff08;二&#xff09;认知行为的出现&#xff08;三&#xff09;模型架构 三、实战运行方式&#xff08;一&#xff09;环境搭建…

go语言实现IP归属地查询

效果: 实现代码main.go package mainimport ("encoding/json""fmt""io/ioutil""net/http""os" )type AreaData struct {Continent string json:"continent"Country string json:"country"ZipCode …

基于STM32、HAL库的SGTL5000XNLA3R2音频接口芯片驱动程序设计

一、简介: SGTL5000XNLA3R2 是 Cirrus Logic 推出的高性能、低功耗音频编解码器,专为便携式和电池供电设备设计。它集成了立体声 ADC、DAC、麦克风前置放大器、耳机放大器和数字信号处理功能,支持 I2S/PCM 音频接口和 I2C 控制接口,非常适合与 STM32 微控制器配合使用。 二…

window 显示驱动开发-报告图形内存(一)

计算图形内存 在 VidMm 能够向客户端报告准确的帐户之前&#xff0c;它必须首先计算图形内存的总量。 VidMm 使用以下内存类型和公式来计算图形内存&#xff1a; 系统总内存 此值是操作系统可访问的系统内存总量。 BIOS 分配的内存不会出现在此数字中。 例如&#xff0c;一台…

[FA1C4] 博客链接

Blog Link 博客已经从 CSDN 转移 高情商&#xff1a;博客是给人看的 低情商&#xff1a;CSDN 已经烂了根本不能看 链接: https://fa1c4.github.io/

python通过curl访问deepseek的API调用案例

废话少说&#xff0c;开干&#xff01; API申请和充值 下面是deepeek的API网站 https://platform.deepseek.com/ 进去先注册&#xff0c;是不是手机账号密码都不重要&#xff0c;都一样&#xff0c;完事充值打米&#xff0c;主要是打米后左侧API Keys里面创建一个API Keys&am…

【计算机视觉】OpenCV项目实战:基于face_recognition库的实时人脸识别系统深度解析

基于face_recognition库的实时人脸识别系统深度解析 1. 项目概述2. 技术原理与算法设计2.1 人脸检测模块2.2 特征编码2.3 相似度计算 3. 实战部署指南3.1 环境配置3.2 数据准备3.3 实时识别流程 4. 常见问题与解决方案4.1 dlib安装失败4.2 人脸检测性能差4.3 误识别率高 5. 关键…

第6章: SEO与交互指标

第6章: SEO与交互指标 在当今的SEO环境中&#xff0c;Google越来越重视用户交互指标&#xff0c;如页面停留时长、交互性能等。本章将深入探讨如何优化网页速度和用户交互体验&#xff0c;以提升SEO效果和用户满意度。 1. Google的新时代SEO指标 随着互联网技术的发展&#xff…

Starrocks的主键表涉及到的MOR Delete+Insert更新策略

背景 写这个文章的作用主要是做一些总结和梳理&#xff0c;特别是正对大数据场景下的实时写入更新策略 COW 和 MOR 以及 DeleteInsert 的技术策略的演进&#xff0c; 这也适用于其他大数据的计算存储系统。该文章主要参考了Primary Key table. 分析总结 Starrocks 的主键表主…

C 语言_常见排序算法全解析

排序算法是计算机科学中的基础内容,本文将介绍 C 语言中几种常见的排序算法,包括实现代码、时间复杂度分析、适用场景和详细解析。 一、冒泡排序(Bubble Sort) 基本思想:重复遍历数组,比较相邻元素,将较大元素交换到右侧。 代码实现: void bubbleSort(int arr[], i…

JIT+Opcache如何配置才能达到性能最优

首先打开php.ini文件&#xff0c;进行配置 1、OPcache配置 ; 启用OPcache opcache.enable1; CLI环境下启用OPcache&#xff08;按需配置&#xff09; opcache.enable_cli0; 预加载脚本&#xff08;PHP 7.4&#xff0c;加速常用类&#xff09; ; opcache.preload/path/to/prel…