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",
+ ],