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

news2025/6/4 7:34:49

目录

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),//缓存,属性store
      mHvacPowerProps(std::begin(kHvacPowerProperties), std::end(kHvacPowerProperties)),
      mRecurrentTimer(std::bind(&EmulatedVehicleHal::onContinuousPropertyTimer, this,
                                std::placeholders::_1)),
      mVehicleClient(client),//VehicleHalClient* mVehicleClient; 缓存,多重继承的EmulatedVehicleConnector
      mEmulatedUserHal(emulatedUserHal) {//初始化为null
    initStaticConfig();//初始化静态配置,里面会调用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 CanSocketComm
        ALOGI("Starting CanSocketComm");
        mCanSocketComm = std::make_unique<CanSocketComm>(this);//CanSocketComm
        mCanSocketComm->start();

    }
}

4. 构建VehicleHalManager

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

    VehicleHalManager(VehicleHal* vehicleHal)
        : mHal(vehicleHal),//初始化mHal
          mSubscriptionManager(std::bind(&VehicleHalManager::onAllClientsUnsubscribed,
                                         this, std::placeholders::_1)) {//初始化mSubscriptionManager
        init();//调用自己的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),//初始化给mOnPropertyUnsubscribed
                mCallbackDeathRecipient(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;//保存server


    binder = 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型的check
    ALOGI("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.coloradmin.cn/o/2395995.html

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

相关文章

【C语言】详解 指针

前言&#xff1a; 在学习指针前&#xff0c;通过比喻的方法&#xff0c;让大家知道指针的作用。 想象一下&#xff0c;你在一栋巨大的图书馆里找一本书。如果没有书架编号和目录&#xff0c;这几乎是不可能完成的任务。 在 C 语言中&#xff0c;指针就像是图书馆的索引系统&…

RabbitMQ仲裁队列高可用架构解析

#作者&#xff1a;闫乾苓 文章目录 概述工作原理1.节点之间的交互2.消息复制3.共识机制4.选举领导者5.消息持久化6.自动故障转移 集群环境节点管理仲裁队列增加集群节点重新平衡仲裁队列leader所在节点仲裁队列减少集群节点 副本管理add_member 在给定节点上添加仲裁队列成员&…

Apache Kafka 实现原理深度解析:生产、存储与消费全流程

Apache Kafka 实现原理深度解析&#xff1a;生产、存储与消费全流程 引言 Apache Kafka 作为分布式流处理平台的核心&#xff0c;其高吞吐、低延迟、持久化存储的设计使其成为现代数据管道的事实标准。本文将从消息生产、持久化存储、消息消费三个阶段拆解 Kafka 的核心实现原…

Python 训练营打卡 Day 41

简单CNN 一、数据预处理 在图像数据预处理环节&#xff0c;为提升数据多样性&#xff0c;可采用数据增强&#xff08;数据增广&#xff09;策略。该策略通常不改变单次训练的样本总数&#xff0c;而是通过对现有图像进行多样化变换&#xff0c;使每次训练输入的样本呈现更丰富…

leetcode付费题 353. 贪吃蛇游戏解题思路

贪吃蛇游戏试玩:https://patorjk.com/games/snake/ 问题描述 设计一个贪吃蛇游戏,要求实现以下功能: 初始化游戏:给定网格宽度、高度和食物位置序列移动操作:根据指令(上、下、左、右)移动蛇头规则: 蛇头碰到边界或自身身体时游戏结束(返回-1)吃到食物时蛇身长度增加…

CCPC dongbei 2025 I

题目链接&#xff1a;https://codeforces.com/gym/105924 题目背景&#xff1a; 给定一个二分图&#xff0c;左图编号 1 ~ n&#xff0c;右图 n 1 ~ 2n&#xff0c;左图的每个城市都会与右图的某个城市犯冲&#xff08;每个城市都只与一个城市犯冲&#xff09;&#xff0c;除…

系统性学习C语言-第十三讲-深入理解指针(3)

系统性学习C语言-第十三讲-深入理解指针&#xff08;3&#xff09; 1. 数组名的理解2. 使用指针访问数组3. ⼀维数组传参的本质4. 冒泡排序5. ⼆级指针 6. 指针数组7. 指针数组模拟二维数组 1. 数组名的理解 在上⼀个章节我们在使用指针访问数组的内容时&#xff0c;有这样的代…

贪心算法实战篇2

文章目录 前言序列问题摆动序列单调递增的数字 贪心解决股票问题买卖股票的最佳时机II 两个维度权衡问题分发糖果根据身高重建队列 前言 今天继续带大家进行贪心算法的实战篇2&#xff0c;本章注意来解答一些运用贪心算法的中等的问题&#xff0c;大家好好体会&#xff0c;怎么…

Java 大视界 -- Java 大数据机器学习模型在元宇宙虚拟场景智能交互中的关键技术(239)

&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎来到 青云交的博客&#xff01;能与诸位在此相逢&#xff0c;我倍感荣幸。在这飞速更迭的时代&#xff0c;我们都渴望一方心灵净土&#xff0c;而 我的博客 正是这样温暖的所在。这里为你呈上趣味与实用兼具的知识&#xff0c;也…

高速串行接口

1.网口设计方案 上图中给出了两种网口设计方案&#xff0c;最上面是传统设计方式&#xff0c;下面是利用GT作为PHY层的设计&#xff0c;然后FPGA中设计协议层和MAC层。 2.SRIO SRIO的本地操作和远程操作 3.其他高速接口 srio rapid io aurora8b10b aurora64b66b pcie s…

学习STC51单片机23(芯片为STC89C52RCRC)

每日一言 成功的路上从不拥挤&#xff0c;因为坚持的人不多&#xff0c;你要做那个例外。 通过单片机发指令给ESP8266进行通信 通信原理(也是接线原理) 代码如下 代码解释一下&#xff0c;因为我们的指令是字符数组&#xff08;c语言没有字符串的概念&#xff09;&#xff0c;…

一个完整的日志收集方案:Elasticsearch + Logstash + Kibana+Filebeat (一)

整体链路 [应用服务器] --> [Filebeat] --> [Logstash] --> [Elasticsearch] --> [Kibana] 组件职责 Kibana&#xff1a; 可视化和分析日志数据Elasticsearch&#xff1a; 存储和索引日志数据Logstash&#xff1a; 解析、转换和丰富日志数据Filebeat&#xff1a…

网络系统中安全漏洞扫描为何重要?扫描啥?咋扫描?

在网络系统中&#xff0c;安全漏洞扫描占据着极其重要的位置&#xff0c;这一环节有助于我们发现并消除潜在的安全隐患&#xff0c;进而提高网络安全防护的等级。下面&#xff0c;我将对此进行详尽的说明。 基本概念 漏洞扫描技术可以揭示并评估网站存在的安全风险&#xff0…

Socket 编程 TCP

目录 1. TCP socket API 详解 1.1 socket 1.2 bind 1.3 listen 1.4 accept 1.5 read&&write 1.6 connect 1.7 recv 1.8 send 1.9 popen 1.10 fgets 2. EchoServer 3. 多线程远程命令执行 4. 引入线程池版本翻译 5. 验证TCP - windows作为client访问Linu…

基于TMC5160堵转检测技术的夹紧力控制系统设计与实现

点击下面图片带您领略全新的嵌入式学习路线 &#x1f525;爆款热榜 90万阅读 1.6万收藏 一、技术背景与系统原理 在工业自动化领域&#xff0c;夹紧力控制是精密装配、机床夹具等场景的核心需求。传统方案多采用压力传感器伺服电机的闭环控制方式&#xff0c;但存在系统复杂…

XCTF-web-fileclude

解析如下 <?php include("flag.php"); // 包含敏感文件&#xff08;通常包含CTF挑战的flag&#xff09; highlight_file(__FILE__); // 高亮显示当前PHP文件源代码&#xff08;方便查看代码逻辑&#xff09;if(isset($_GET["file1"]…

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

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

Redis 缓存穿透、缓存击穿、缓存雪崩详解与解决方案

在分布式系统中&#xff0c;Redis 凭借高性能和高并发处理能力&#xff0c;成为常用的缓存组件。然而&#xff0c;在实际应用中&#xff0c;缓存穿透、缓存击穿、缓存雪崩这三大问题会严重影响系统的性能与稳定性。本文将详细解析这三个问题的成因&#xff0c;并提供对应的解决…

DQN和DDQN(进阶版)

来源&#xff1a; *《第五章 深度强化学习 Q网络》.ppt --周炜星、谢文杰 一、前言 Q表格、Q网络与策略函数 Q表格是有限的离散的&#xff0c;而神经网络可以是无限的。 对于动作有限的智能体来说&#xff0c;使用Q网络获得当下状态的对于每个动作的 状态-动作值 。那么 a…

【组件】翻牌器效果

目录 效果组件代码背景素材 效果 组件代码 <template><divclass"card-flop":style"{height: typeof height number ? ${height}px : height,--box-width: typeof boxWidth number ? ${boxWidth}px : boxWidth,--box-height: typeof boxHeight nu…