Vehicle HAL(2)--Vehicle HAL 的启动

article/2025/8/4 18:41:41

目录

1. VehicleService-main 函数分析

2. 构建EmulatedVehicleHal

2.1 EmulatedVehicleHal::EmulatedVehicleHal(xxx)

2.2 EmulatedVehicleHal::initStaticConfig()

2.3 EmulatedVehicleHal::onPropertyValue()

3. 构建VehicleEmulator

4. 构建VehicleHalManager

(1)初始化成员变量SubscriptionManager mSubscriptionManager

(2)调用VehicleHalManager::init()

a. 调用VehicleHal::init()

b. mHal->listProperties() 获取支持的属性

5. 构建WatchdogClient对象,并初始化

6. vhal主线程开启while(true)循环

1. VehicleService-main 函数分析

vhal在VehicleService-main中启动。具体代码如下:

hardware/interfaces/automotive/vehicle/2.0/default/VehicleService.cpp

17  #define LOG_TAG "automotive.vehicle@2.0-service"
18  #include <android/log.h>
19  #include <hidl/HidlTransportSupport.h>
20  
21  #include <iostream>
22  
23  #include <android/binder_process.h>
24  #include <utils/Looper.h>
25  #include <vhal_v2_0/EmulatedVehicleConnector.h>
26  #include <vhal_v2_0/EmulatedVehicleHal.h>
27  #include <vhal_v2_0/VehicleHalManager.h>
28  #include <vhal_v2_0/WatchdogClient.h>
29  
30  using namespace android;
31  using namespace android::hardware;
32  using namespace android::hardware::automotive::vehicle::V2_0;
33  
34  int main(int /* argc */, char* /* argv */ []) {
35      auto store = std::make_unique<VehiclePropertyStore>();//新建对象,采用默认构造函数VehiclePropertyStore
36      auto connector = std::make_unique<impl::EmulatedVehicleConnector>();//同上,构建EmulatedVehicleConnector
37      auto hal = std::make_unique<impl::EmulatedVehicleHal>(store.get(), connector.get());//第2章,获取对象的指针,来新建EmulatedVehicleHal
38      auto emulator = std::make_unique<impl::VehicleEmulator>(hal.get());//第3章,新建VehicleEmulator
39      auto service = std::make_unique<VehicleHalManager>(hal.get());//第4章,构建VehicleHalManager,传入hal指针,VehicleHalManager是实现hidl服务的主体
40      connector->setValuePool(hal->getValuePool());//属性池的指针设置给connector
41  
42      configureRpcThreadpool(4, false /* callerWillJoin */);
43  
44      ALOGI("Registering as service...");
45      status_t status = service->registerAsService();//hidl服务注册
46  
47      if (status != OK) {
48          ALOGE("Unable to register vehicle service (%d)", status);
49          return 1;
50      }
51  
52      // Setup a binder thread pool to be a car watchdog client.
53      ABinderProcess_setThreadPoolMaxThreadCount(1);
54      ABinderProcess_startThreadPool();
55      sp<Looper> looper(Looper::prepare(0 /* opts */));
56      std::shared_ptr<WatchdogClient> watchdogClient =
57              ndk::SharedRefBase::make<WatchdogClient>(looper, service.get());//第5章,构建WatchdogClient对象,并初始化
58      // The current health check is done in the main thread, so it falls short of capturing the real
59      // situation. Checking through HAL binder thread should be considered.
60      if (!watchdogClient->initialize()) {//初始化
61          ALOGE("Failed to initialize car watchdog client");
62          return 1;
63      }
64      ALOGI("Ready");
65      while (true) {
66          looper->pollAll(-1 /* timeoutMillis */);//第6章,开启while(true)循环
67      }
68  
69      return 1;
70  }
71  

2. 构建EmulatedVehicleHal

EmulatedVehicleHal实现了vhal中的VehicleHal抽象类。

从下图可以看出继承关系。

android11/hardware/interfaces/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.h

android11/hardware/interfaces/automotive/vehicle/2.0/default/common/include/vhal_v2_0/VehicleHal.h

获取对象的指针,来新建EmulatedVehicleHal

    EmulatedVehicleHal(VehiclePropertyStore* propStore, VehicleHalClient* client,EmulatedUserHal* emulatedUserHal = nullptr);

EmulatedVehicleHal本身存放了很多静态配置的属性值,构建的时候会传入mPropStore。

2.1 EmulatedVehicleHal::EmulatedVehicleHal(xxx)

EmulatedVehicleHal::EmulatedVehicleHal(VehiclePropertyStore* propStore, VehicleHalClient* client,EmulatedUserHal* emulatedUserHal): mPropStore(propStore),//缓存,属性storemHvacPowerProps(std::begin(kHvacPowerProperties), std::end(kHvacPowerProperties)),mRecurrentTimer(std::bind(&EmulatedVehicleHal::onContinuousPropertyTimer, this,std::placeholders::_1)),mVehicleClient(client),//VehicleHalClient* mVehicleClient; 缓存,多重继承的EmulatedVehicleConnectormEmulatedUserHal(emulatedUserHal) {//初始化为nullinitStaticConfig();//初始化静态配置,里面会调用mPropStore->registerProperty(),有mPropStore的进一步操作for (size_t i = 0; i < arraysize(kVehicleProperties); i++) {mPropStore->registerProperty(kVehicleProperties[i].config);//继续初始化mPropStore,注册一些属性}mVehicleClient->registerPropertyValueCallback(std::bind(&EmulatedVehicleHal::onPropertyValue,//会从mVehicleClient触发回调回来this, std::placeholders::_1,std::placeholders::_2));//关键调用,属性变化之后的回调
}

2.2 EmulatedVehicleHal::initStaticConfig()

EmulatedVehicleHal() > EmulatedVehicleHal::initStaticConfig() > mPropStore->registerProperty()

kVehicleProperties是静态配置好了的。

195  const ConfigDeclaration kVehicleProperties[]

android11/hardware/interfaces/automotive/vehicle/2.0/default/impl/vhal_v2_0/DefaultConfig.h

360  void EmulatedVehicleHal::initStaticConfig() {
361      for (auto&& it = std::begin(kVehicleProperties); it != std::end(kVehicleProperties); ++it) {
362          const auto& cfg = it->config;
363          VehiclePropertyStore::TokenFunction tokenFunction = nullptr;
364  
365          switch (cfg.prop) {
366              case OBD2_FREEZE_FRAME: {
367                  tokenFunction = [](const VehiclePropValue& propValue) {
368                      return propValue.timestamp;
369                  };
370                  break;
371              }
372              default:
373                  break;
374          }
375  
376          mPropStore->registerProperty(cfg, tokenFunction);//关键代码
377      }
378  }
38  void VehiclePropertyStore::registerProperty(const VehiclePropConfig& config,
39                                              VehiclePropertyStore::TokenFunction tokenFunc) {
40      MuxGuard g(mLock);
41      mConfigs.insert({ config.prop, RecordConfig { config, tokenFunc } });//向std::unordered_map中插入
42  }
hardware/interfaces/automotive/vehicle/2.0/default/common/include/vhal_v2_0/VehiclePropertyStore.h
93      std::unordered_map<int32_t /* VehicleProperty */, RecordConfig> mConfigs;

2.3 EmulatedVehicleHal::onPropertyValue()

此为传递属性改变的关键函数


351  void EmulatedVehicleHal::onPropertyValue(const VehiclePropValue& value, bool updateStatus) {
352      VehiclePropValuePtr updatedPropValue = getValuePool()->obtain(value);
353  
354      if (mPropStore->writeValue(*updatedPropValue, updateStatus)) {//更新属性
355          getEmulatorOrDie()->doSetValueFromClient(*updatedPropValue);//关键调用,Emulator,client,获取时通过socket走到can的另外一端
356          doHalEvent(std::move(updatedPropValue));//关键调用,返回变化到carservice
357      }
358  }

3. 构建VehicleEmulator

模拟车,做一些通信接口相关的初始化。把消息发给可能存在的ecu。在原生上是通过can给ecu发消息。见上一节EmulatedVehicleHal的onPropertyValue消息处理中有getEmulatorOrDie()->doSetValueFromClient(xxx)。

class VehicleEmulator : public MessageProcessor

用刚才构建的EmulatedVehicleHal来构建VehicleEmulator。VehicleEmulator模拟的vehicle,或者说模拟的car。

/**
 * Emulates vehicle by providing controlling interface from host side either through ADB or Pipe.
 */

VehicleEmulator(EmulatedVehicleHalIface* hal);

VehicleEmulator::VehicleEmulator(EmulatedVehicleHalIface* hal) : mHal{hal} {mHal->registerEmulator(this);//把自己注册到EmulatedVehicleHal中,VehicleEmulator和mHal产生关系。mHal可以调用到VehicleEmulator。ALOGI("Starting SocketComm");mSocketComm = std::make_unique<SocketComm>(this);//新建socket通信相关对象mSocketComm->start();if (android::base::GetBoolProperty("ro.kernel.qemu", false)) {ALOGI("Starting PipeComm");mPipeComm = std::make_unique<PipeComm>(this);//pipe相关的mPipeComm->start();}if (android::base::GetBoolProperty("persist.vendor.cansocket", false)) {//enable siengine CanSocketCommALOGI("Starting CanSocketComm");mCanSocketComm = std::make_unique<CanSocketComm>(this);//CanSocketCommmCanSocketComm->start();}
}

4. 构建VehicleHalManager

构建VehicleHalManager的关键过程为有3步。VehicleHalManager实现了IVehiclel接口,hal服务的主体。

    VehicleHalManager(VehicleHal* vehicleHal): mHal(vehicleHal),//初始化mHalmSubscriptionManager(std::bind(&VehicleHalManager::onAllClientsUnsubscribed,this, std::placeholders::_1)) {//初始化mSubscriptionManagerinit();//调用自己的init()函数}

(1)初始化成员变量SubscriptionManager mSubscriptionManager

SubscriptionManager 的构造函数

    /*** Constructs SubscriptionManager** @param onPropertyUnsubscribed - called when no more clients are subscribed to the property.*/SubscriptionManager(const OnPropertyUnsubscribed& onPropertyUnsubscribed): mOnPropertyUnsubscribed(onPropertyUnsubscribed),//初始化给mOnPropertyUnsubscribedmCallbackDeathRecipient(new DeathRecipient(std::bind(&SubscriptionManager::onCallbackDead, this, std::placeholders::_1))){}

(2)调用VehicleHalManager::init()

hardware/interfaces/automotive/vehicle/2.0/default/common/src/VehicleHalManager.cpp

431  void VehicleHalManager::init() {
432      ALOGI("VehicleHalManager::init");
433  
434      mHidlVecOfVehiclePropValuePool.resize(kMaxHidlVecOfVehiclPropValuePoolSize);
435  
436  
437      mBatchingConsumer.run(&mEventQueue,
438                            kHalEventBatchingTimeWindow,
439                            std::bind(&VehicleHalManager::onBatchHalEvent,
440                                      this, _1));//启动mBatchingConsumer
441  
442      mHal->init(&mValueObjectPool,
443                 std::bind(&VehicleHalManager::onHalEvent, this, _1),
444                 std::bind(&VehicleHalManager::onHalPropertySetError, this,
445                           _1, _2, _3));//a.调用hal的init函数
446  
447      // Initialize index with vehicle configurations received from VehicleHal.
448      auto supportedPropConfigs = mHal->listProperties();//b.获取支持的属性
449      mConfigIndex.reset(new VehiclePropConfigIndex(supportedPropConfigs));//放入mConfigIndex
450  
451      std::vector<int32_t> supportedProperties(
452          supportedPropConfigs.size());
453      for (const auto& config : supportedPropConfigs) {
454          supportedProperties.push_back(config.prop);
455      }
456  }
a. 调用VehicleHal::init()

调用hal的init函数:VehicleHal的init函数

93      void init(
94          VehiclePropValuePool* valueObjectPool,
95          const HalEventFunction& onHalEvent,
96          const HalErrorFunction& onHalError) {
97          mValuePool = valueObjectPool;
98          mOnHalEvent = onHalEvent;
99          mOnHalPropertySetError = onHalError;
100  
101          onCreate();
102      }
b. mHal->listProperties() 获取支持的属性

hardware/interfaces/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.cpp

290  std::vector<VehiclePropConfig> EmulatedVehicleHal::listProperties()  {
291      return mPropStore->getAllConfigs();
292  }

mHal中mPropStore的来源

hardware/interfaces/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.cpp

90  EmulatedVehicleHal::EmulatedVehicleHal(VehiclePropertyStore* propStore, VehicleHalClient* client,
91                                         EmulatedUserHal* emulatedUserHal)
92      : mPropStore(propStore),//mPropStore的来源
93        mHvacPowerProps(std::begin(kHvacPowerProperties), std::end(kHvacPowerProperties)),
94        mRecurrentTimer(std::bind(&EmulatedVehicleHal::onContinuousPropertyTimer, this,
95                                  std::placeholders::_1)),
96        mVehicleClient(client),
97        mEmulatedUserHal(emulatedUserHal) {
98      initStaticConfig();
99      for (size_t i = 0; i < arraysize(kVehicleProperties); i++) {
100          mPropStore->registerProperty(kVehicleProperties[i].config);
101      }
102      mVehicleClient->registerPropertyValueCallback(std::bind(&EmulatedVehicleHal::onPropertyValue,
103                                                              this, std::placeholders::_1,
104                                                              std::placeholders::_2));
105  }
106  
mPropStore->getAllConfigs()的实现:

hardware/interfaces/automotive/vehicle/2.0/default/common/src/VehiclePropertyStore.cpp

124  std::vector<VehiclePropConfig> VehiclePropertyStore::getAllConfigs() const {
125      MuxGuard g(mLock);
126      std::vector<VehiclePropConfig> configs;
127      configs.reserve(mConfigs.size());//从mConfigs来
128      for (auto&& recordConfigIt: mConfigs) {//遍历mConfigs
129          configs.push_back(recordConfigIt.second.propConfig);
130      }
131      return configs;
132  }

5. 构建WatchdogClient对象,并初始化

    explicit WatchdogClient(const ::android::sp<::android::Looper>& handlerLooper,
                            VehicleHalManager* vhalManager);

将自己加入car watchdog的监控。

WatchdogClient::WatchdogClient(const sp<Looper>& handlerLooper, VehicleHalManager* vhalManager): mHandlerLooper(handlerLooper), mVhalManager(vhalManager), mCurrentSessionId(-1) {mMessageHandler = new MessageHandlerImpl(this);//新建内部的handler
}
bool WatchdogClient::initialize() {ndk::SpAIBinder binder(AServiceManager_getService("android.automotive.watchdog.ICarWatchdog/default"));//获取服务if (binder.get() == nullptr) {ALOGE("Failed to get carwatchdog daemon");return false;}std::shared_ptr<ICarWatchdog> server = ICarWatchdog::fromBinder(binder);if (server == nullptr) {ALOGE("Failed to connect to carwatchdog daemon");return false;}mWatchdogServer = server;//保存serverbinder = this->asBinder();if (binder.get() == nullptr) {ALOGE("Failed to get car watchdog client binder object");return false;}std::shared_ptr<ICarWatchdogClient> client = ICarWatchdogClient::fromBinder(binder);if (client == nullptr) {ALOGE("Failed to get ICarWatchdogClient from binder");return false;}mTestClient = client;mWatchdogServer->registerClient(client, TimeoutLength::TIMEOUT_NORMAL);//把自己注册到sever端,类型为TIMEOUT_NORMAL型的checkALOGI("Successfully registered the client to car watchdog server");return true;
}

接收来自sever(carwatchdogd)的回调

ndk::ScopedAStatus WatchdogClient::prepareProcessTermination() {return ndk::ScopedAStatus::ok();
}

6. vhal主线程开启while(true)循环

64      ALOGI("Ready");
65      while (true) {
66          looper->pollAll(-1 /* timeoutMillis */);//开启while(true)循环,主线程一直睡在这里
67      }


http://www.hkcw.cn/article/VMxDqcwwvS.shtml

相关文章

【科研绘图系列】R语言绘制论文组合图形(multiple plots)

禁止商业或二改转载,仅供自学使用,侵权必究,如需截取部分内容请后台联系作者! 文章目录 介绍加载R包数据下载导入数据数据预处理画图1画图2画图3画图4画图5系统信息介绍 这篇文章详细介绍了如何使用R语言进行科研绘图,特别是绘制论文组合图形(multiple plots)。文章从数…

设计模式——命令设计模式(行为型)

摘要 本文介绍了命令设计模式&#xff0c;这是一种行为型设计模式&#xff0c;用于将请求封装为对象&#xff0c;实现请求的解耦和灵活控制。它包含命令接口、具体命令、接收者、调用者和客户端等角色&#xff0c;优点是解耦请求发送者与接收者&#xff0c;支持命令的排队、记…

用提示词写程序(3),VSCODE+Claude3.5+deepseek开发edge扩展插件V2

edge扩展插件;筛选书签,跳转搜索,设置背景 链接: https://pan.baidu.com/s/1nfnwQXCkePRnRh5ltFyfag?pwd86se 提取码: 86se 导入解压的扩展文件夹: 导入扩展成功: edge扩展插件;筛选书签,跳转搜索,设置背景

一周学会Pandas2之Python数据处理与分析-Pandas2数据分组与聚合

锋哥原创的Pandas2 Python数据处理与分析 视频教程&#xff1a; 2025版 Pandas2 Python数据处理与分析 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili Pandas 的分组与聚合功能是数据分析的核心工具&#xff0c;允许你根据特定条件分割数据&#xff0c;然后对每个子集应…

零基础上手 Cherry Studio:打造专属 AI 助手的第一步

名人说&#xff1a;博观而约取&#xff0c;厚积而薄发。——苏轼《稼说送张琥》 创作者&#xff1a;Code_流苏(CSDN)&#xff08;一个喜欢古诗词和编程的Coder&#x1f60a;&#xff09; 目录 一、Cherry Studio是什么&#xff1f;为什么选择它&#xff1f;1.Cherry Studio简介…

API平台(API网关)之API编排

支持WEB可视化的API编排能力&#xff0c;内置基本组件、流程控制、报文转换以及其他类型的组件&#xff0c;通过可视化拖、拉、拽对API进行编排和调度&#xff0c;支持Restful API、WebService、JAVA等多协议转换混合编排和API服务聚合能力&#xff0c;满足复杂业务场景的API支…

初识PS(Photoshop)

初识PS&#xff08;Photoshop&#xff09; 1、Photoshop界面 2、常用快捷键

鸿蒙ArkTS | Badge 信息标记组件自学指南

在做 UI 设计时&#xff0c;我们经常会碰到这样一个需求&#xff1a;要在某个图标、按钮或列表项的角落加一个小小的提示&#xff0c;比如红点、新消息数量、状态标签之类的。这时候就轮到 ​​Badge​​ 组件登场了。 如果你想要一个简单优雅、可配置灵活的信息标记功能&…

【C++】cin和cout的性能问题讨论和优化方法

目录 1、背景知识2、ios::sync_with_stdio(false)2.1 **作用**2.2 原理2.3 注意事项 3、cin.tie(0)3.1 作用3.2 原理3.3 注意事项 4、代码演示5、实例操作5.1 创建数据文件5.2 进行代码演示 个人主页<—请点击 C专栏<—请点击 1、背景知识 在 C 中&#xff0c;标准输⼊输…

【数据结构】图的存储(十字链表)

弧节点 tailvex数据域&#xff1a;存储弧尾一端顶点在顺序表中的位置下标&#xff1b;headvex 数据域&#xff1a;存储弧头一端顶点在顺序表中的位置下标&#xff1b;hlink 指针域&#xff1a;指向下一个以当前顶点作为弧头的弧&#xff1b;tlink 指针域&#xff1a;指向下一个…

(二)微服务(grpc/grpc消费者)

文章目录 项目地址一、grpc介绍1.1 项目初始化1. 创建grpc项目2. 项目结构二、Discount grpc创建2.1 实体层1. Coupon实体2.2 Protos1. 创建discount.proto2. 配置proto3. 创建DiscountService4. Program里注册服务2.3 Seed 数据1. 创建表和Seed数据2. 自动migration2.4 更新Do…

机电的焊接技术

焊接技术:高温或高压条件下,使用焊接材料(焊条或焊丝)将两块或两块以上的母材(待焊接的工件)连接 成一个整体的操作方法&#xff61; 2.3.1 焊接设备和焊接材料的分类及选用 1.焊接设备&#xff08;对应焊接方法&#xff09; 2.焊接材料&#xff08;焊条、焊丝、焊剂、焊接气…

Ⅰ.计算机二级选择题(C语言概述)

【注&#xff1a;重点题以及添加目录格式导航&#xff01;&#xff01;&#xff01;】 【重点】&#xff08;第2题&#xff09; 【重点】&#xff08;第8题&#xff09; 【重点】&#xff08;第17题&#xff09; 【重点】&#xff08;第19题&#xff09; 【重点】&#xff08;第…

ck-editor5的研究 (4):初步使用 CKEditor5 的插件功能

前言 在上一篇文章中—— ck-editor5 的研究&#xff08;3&#xff09;&#xff1a;初步使用 CKEditor5 的事件系统和API &#xff0c;我们已经初步了解了 CKEditor5 的工作方式 那么这篇文章&#xff0c;我们将初步使用 CKEditor5 的插件功能&#xff0c;我将会写一个自己的…

分类预测 | Matlab实现CNN-LSTM-Attention高光谱数据分类

分类预测 | Matlab实现CNN-LSTM-Attention高光谱数据分类 目录 分类预测 | Matlab实现CNN-LSTM-Attention高光谱数据分类分类效果功能概述程序设计参考资料 分类效果 功能概述 代码功能 该MATLAB代码实现了一个结合CNN、LSTM和注意力机制的高光谱数据分类模型&#xff0c;核心…

嵌入式项目之mini2440系统制作烧写

系统移植的必要性与组成 在嵌入式开发中&#xff0c;**系统移植&#xff08;Linux 系统定制&#xff09;** 是常见的需求&#xff0c;主要原因在于&#xff1a; 1. **官方镜像体积过大**&#xff1a;标准 Linux 发行版&#xff08;如 Ubuntu&#xff09;可能占用数 GB 存储…

OpenShift AI - 启用过时版本的 Notebook 镜像

《OpenShift / RHEL / DevSecOps 汇总目录》 说明&#xff1a;本文已经在 OpenShift 4.18 OpenShift AI 2.19 的环境中验证 文章目录 查看可用 Notebook 镜像控制台查看命令行查看 Notebook 镜像和 Image Stream 对应关系启用老版本的 Notebook 镜像参考 查看可用 Notebook 镜…

“人单酬“理念:财税行业的自我驱动革命

引言&#xff1a;当薪酬不再是"固定数字"&#xff0c;而是"成长标尺" "为什么有人拼命工作却收入停滞&#xff1f;为什么企业总在人才流失中挣扎&#xff1f;"这些问题背后&#xff0c;往往隐藏着传统薪酬体系的僵化。而"人单酬"&…

yolo目标检测助手:具有模型预测、图像标注功能

在人工智能浪潮席卷各行各业的今天&#xff0c;计算机视觉模型&#xff08;如 YOLO&#xff09;已成为目标检测领域的标杆。然而&#xff0c;模型的强大能力需要直观的界面和便捷的工具才能充分发挥其演示、验证与迭代优化的价值。为此&#xff0c;我开发了一款基于 WPF 的桌面…

Spring Ai 从Demo到搭建套壳项目(一)初识与实现与deepseek对话模式

前言 为什么说Java长青&#xff0c;主要是因为其生态圈完善&#xff0c;Spring又做了一款脚手架&#xff0c;把对接各个LLM厂商的sdk做了一遍&#xff0c;形成一系列的spring-ai-starter-** 的依赖。 目前为止版本去到1.0.0.M6&#xff0c;golang跟不上了吧&#xff0c; Make …