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

Vehicle HAL(7)--how client connect to Vehicle HAL?

目录

1.ard13 vhal test IVehicle 中的连接方法

2. ard13 carservice 使用aidl IVehicle

3. ard13 carwatchdog 使用 IVhalClient

4. ard13 接入vhal订阅单一属性的样例

5. ard15 的 AIDL_VHAL_SERVICE–"android.hardware.automotive.vehicle.IVehicle/default"

5.1 AIDL_VHAL_SERVICE 服务的发布

5.2 client 使用 aidl IVehicle connect to vhal

6. android15 client 使用 IVhalClient 连接 vhal


1.ard13 vhal test IVehicle 中的连接方法

android13/hardware/interfaces/automotive/vehicle/aidl/impl/vhal/test/DefaultVehicleHalTest.cpp

std::shared_ptr<IVehicle> getClient() { return mVhal; }

1104  TEST_F(DefaultVehicleHalTest, testSubscribeUnsubscribe) {
1105      std::vector<SubscribeOptions> options = {
1106              {
1107                      .propId = GLOBAL_ON_CHANGE_PROP,
1108              },
1109      };
1110 
1111      auto status = getClient()->subscribe(getCallbackClient(), options, 0);
1112 
1113      ASSERT_TRUE(status.isOk()) << "subscribe failed: " << status.getMessage();
1114 
1115      status = getClient()->unsubscribe(getCallbackClient(),
1116                                        std::vector<int32_t>({GLOBAL_ON_CHANGE_PROP}));
1117 
1118      ASSERT_TRUE(status.isOk()) << "unsubscribe failed: " << status.getMessage();
1119  }

2. ard13 carservice 使用 aidl IVehicle

android13/packages/services/Car/cpp/vhal/client/src/AidlVhalClient.cpp

124  AidlVhalClient::AidlVhalClient(std::shared_ptr<IVehicle> hal, int64_t timeoutInMs,
125                                 std::unique_ptr<ILinkUnlinkToDeath> linkUnlinkImpl) :
126        mHal(hal) {
127      mGetSetValueClient = SharedRefBase::make<GetSetValueClient>(
128              /*timeoutInNs=*/timeoutInMs * 1'000'000, hal);
129      mDeathRecipient = ScopedAIBinder_DeathRecipient(
130              AIBinder_DeathRecipient_new(&AidlVhalClient::onBinderDied));
131      mLinkUnlinkImpl = std::move(linkUnlinkImpl);
132      binder_status_t status =
133              mLinkUnlinkImpl->linkToDeath(hal->asBinder().get(), mDeathRecipient.get(),
134                                           static_cast<void*>(this));
135      if (status != STATUS_OK) {
136          ALOGE("failed to link to VHAL death, status: %d", static_cast<int32_t>(status));
137      }
138  }
552  VhalResult<void> AidlSubscriptionClient::subscribe(const std::vector<SubscribeOptions>& options) {
553      std::vector<int32_t> propIds;
554      for (const SubscribeOptions& option : options) {
555          propIds.push_back(option.propId);
556      }
557 
558      // TODO(b/205189110): Fill in maxSharedMemoryFileCount after we support memory pool.
559      if (auto status = mHal->subscribe(mSubscriptionCallback, options,
560                                        /*maxSharedMemoryFileCount=*/0);
561          !status.isOk()) {
562          return AidlVhalClient::statusToError<
563                  void>(status,
564                        StringPrintf("failed to subscribe to prop IDs: %s",
565                                     toString(propIds).c_str()));
566      }
567      return {};
568  }

3. ard13 carwatchdog 使用 IVhalClient

std::shared_ptr<android::frameworks::automotive::vhal::IVhalClient> mVhalService

815  Result<void> WatchdogProcessService::connectToVhalLocked() {
816      if (mVhalService != nullptr) {
817          return {};
818      }
819      mVhalService = IVhalClient::tryCreate();//这里
820      if (mVhalService == nullptr) {
821          return Error() << "Failed to connect to VHAL.";
822      }
823      mVhalService->addOnBinderDiedCallback(mOnBinderDiedCallback);
824      queryVhalPropertiesLocked();
825      subscribeToVhalHeartBeatLocked();
826      ALOGI("Successfully connected to VHAL.");
827      return {};
828  }

android13/packages/services/Car/cpp/watchdog/server/src/WatchdogProcessService.cpp

847  void WatchdogProcessService::subscribeToVhalHeartBeatLocked() {
848      if (mNotSupportedVhalProperties.count(VehicleProperty::VHAL_HEARTBEAT) > 0) {
849          ALOGW("VHAL doesn't support VHAL_HEARTBEAT. Checking VHAL health is disabled.");
850          return;
851      }
852 
853      mVhalHeartBeat = {
854              .eventTime = 0,
855              .value = 0,
856      };
857 
858      std::vector<SubscribeOptions> options = {
859              {.propId = static_cast<int32_t>(VehicleProperty::VHAL_HEARTBEAT), .areaIds = {}},
860      };
861      if (auto result =
862                  mVhalService->getSubscriptionClient(mPropertyChangeListener)->subscribe(options);
863          !result.ok()) {
864          ALOGW("Failed to subscribe to VHAL_HEARTBEAT. Checking VHAL health is disabled. '%s'",
865                result.error().message().c_str());
866          return;
867      }
868      std::chrono::nanoseconds intervalNs = mVhalHealthCheckWindowMs + kHealthCheckDelayMs;
869      mHandlerLooper->sendMessageDelayed(intervalNs.count(), mMessageHandler,
870                                         Message(MSG_VHAL_HEALTH_CHECK));
871  }

4. ard13 接入vhal订阅单一属性的样例

连上订阅管理就可以了。类似上层使用了HalClient.java。

android13/hardware/interfaces/automotive/vehicle/aidl/impl/vhal/test/SubscriptionManagerTest.cpp

android13/hardware/interfaces/automotive/vehicle/aidl/impl/vhal/test/MockVehicleHardware.cpp

5. ard15 的 AIDL_VHAL_SERVICE–"android.hardware.automotive.vehicle.IVehicle/default"

5.1 AIDL_VHAL_SERVICE 服务的发布

android15/hardware/interfaces/automotive/vehicle/aidl/impl/vhal/src/VehicleService.cpp

29  int main(int /* argc */, char* /* argv */[]) {
30      ALOGI("Starting thread pool...");
31      if (!ABinderProcess_setThreadPoolMaxThreadCount(4)) {
32          ALOGE("%s", "failed to set thread pool max thread count");
33          return 1;
34      }
35      ABinderProcess_startThreadPool();
36 
37      std::unique_ptr<FakeVehicleHardware> hardware = std::make_unique<FakeVehicleHardware>();
38      std::shared_ptr<DefaultVehicleHal> vhal =
39              ::ndk::SharedRefBase::make<DefaultVehicleHal>(std::move(hardware));
40 
41      ALOGI("Registering as service...");
42      binder_exception_t err = AServiceManager_addService(
43              vhal->asBinder().get(), "android.hardware.automotive.vehicle.IVehicle/default");
44      if (err != EX_NONE) {
45          ALOGE("failed to register android.hardware.automotive.vehicle service, exception: %d", err);
46          return 1;
47      }
48 
49      ALOGI("Vehicle Service Ready");
50 
51      ABinderProcess_joinThreadPool();
52 
53      ALOGI("Vehicle Service Exiting");
54 
55      return 0;
56  }

android15/hardware/interfaces/automotive/vehicle/aidl/impl/vhal/include/DefaultVehicleHal.h

41  namespace android {
42  namespace hardware {
43  namespace automotive {
44  namespace vehicle {
45 
46  namespace aidlvhal = ::aidl::android::hardware::automotive::vehicle;
47 
48  class DefaultVehicleHal final : public aidlvhal::BnVehicle {

5.2 client 使用 aidl IVehicle connect to vhal

android15/packages/services/Car/service/src/com/android/car/AidlVehicleStub.java


91      private static final String AIDL_VHAL_SERVICE =
92              "android.hardware.automotive.vehicle.IVehicle/default";

android15/packages/services/Car/cpp/vhal/client/src/AidlVhalClient.cpp

81  std::shared_ptr<IVhalClient> AidlVhalClient::create() {
82      if (!AServiceManager_isDeclared(AIDL_VHAL_SERVICE)) {
83          ALOGD("AIDL VHAL service is not declared, maybe HIDL VHAL is used instead?");
84          return nullptr;
85      }
86      std::shared_ptr<IVehicle> aidlVhal =
87              IVehicle::fromBinder(SpAIBinder(AServiceManager_waitForService(AIDL_VHAL_SERVICE)));
88      if (aidlVhal == nullptr) {
89          ALOGW("AIDL VHAL service is not available");
90          return nullptr;
91      }
92      ABinderProcess_startThreadPool();
93      return std::make_shared<AidlVhalClient>(aidlVhal);
94  }

6. android15 client 使用 IVhalClient 连接 vhal

using ::android::frameworks::automotive::vhal::IVhalClient;
android15/packages/services/Car/cpp/vhal/client/include/IVhalClient.h

208  // IVhalClient is a thread-safe client for AIDL or HIDL VHAL backend.
209  class IVhalClient {
210  public:
211      // Wait for VHAL service and create a client. Return nullptr if failed to connect to VHAL.
212      static std::shared_ptr<IVhalClient> create();

carwatchdog的例子

android15/packages/services/Car/cpp/watchdog/server/src/WatchdogProcessService.cpp

carpowerpolicyd的例子

android15/packages/services/Car/cpp/powerpolicy/server/src/CarPowerPolicyServer.cpp

注意:需要client在自己的Android.bp中加入如下链接static lib和defaults:

+ static_libs: [
+ "libvhalclient",
+ ],

+ defaults: [
+ "vhalclient_defaults",
+ ],

http://www.xdnf.cn/news/12232.html

相关文章:

  • Spring事务失效-----十大常见场景及解决方案全解析
  • Git Svn
  • 图像测试点列表
  • 如何实现本地mqtt服务器和云端服务器同步?
  • 基于责任链模式进行订单参数的校验
  • Flink 高可用集群部署指南
  • NuxtJS入门指南:环境安装及报错解决
  • 【Redis】类型补充
  • Oracle-高频业务表的性能检查
  • Tailwind CSS 实战:基于 Kooboo 构建 AI 对话框页面(七):消息框交互功能添加
  • 复变函数中的对数函数及其MATLAB演示
  • 人脸识别技术成为时代需求,视频智能分析网关视频监控系统中AI算法的应用
  • 残月个人拟态主页
  • float和float32有什么区别
  • 《前端面试题:CSS的display属性》
  • IDEA 打开文件乱码
  • 每日算法-250605
  • React 第五十三节 Router中 useRouteError 的使用详解和案例分析
  • 使用深蓝词库软件导入自定义的词库到微软拼音输入法
  • 第四十五天打卡
  • OpenCV种的cv::Mat与Qt种的QImage类型相互转换
  • ES 学习总结一 基础内容
  • mac 电脑Pycharm ImportError: No module named pip
  • io多路复用的三种方式
  • Haproxy的基础配置
  • vue+element-ui一个页面有多个子组件组成。子组件里面有各种表单,实现点击enter实现跳转到下一个表单元素的功能。
  • 在 Oracle 中,创建不同类型索引的 SQL 语法
  • 一次Oracle的非正常关闭
  • java学习笔记——数组和二维数组
  • 驶向智能未来:车载 MCP 服务与边缘计算驱动的驾驶数据交互新体验