1 /* 2 * Copyright (c) 2022, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef CPP_VHAL_CLIENT_INCLUDE_HIDLVHALCLIENT_H_ 18 #define CPP_VHAL_CLIENT_INCLUDE_HIDLVHALCLIENT_H_ 19 20 #include "IVhalClient.h" 21 22 #include <aidl/android/hardware/automotive/vehicle/SubscribeOptions.h> 23 #include <android-base/thread_annotations.h> 24 #include <android/hardware/automotive/vehicle/2.0/IVehicle.h> 25 #include <utils/StrongPointer.h> 26 27 #include <mutex> // NOLINT 28 #include <unordered_set> 29 30 namespace android { 31 namespace frameworks { 32 namespace automotive { 33 namespace vhal { 34 35 namespace hidl_test { 36 37 class HidlVhalClientTest; 38 39 } // namespace hidl_test 40 41 class HidlVhalClient final : public IVhalClient { 42 public: 43 static std::shared_ptr<IVhalClient> create(); 44 static std::shared_ptr<IVhalClient> tryCreate(); 45 static std::shared_ptr<IVhalClient> tryCreate(const char* descriptor); 46 47 explicit HidlVhalClient( 48 android::sp<android::hardware::automotive::vehicle::V2_0::IVehicle> hal); 49 50 ~HidlVhalClient(); 51 52 bool isAidlVhal() override; 53 54 std::unique_ptr<IHalPropValue> createHalPropValue(int32_t propId) override; 55 56 std::unique_ptr<IHalPropValue> createHalPropValue(int32_t propId, int32_t areaId) override; 57 58 void getValue(const IHalPropValue& requestValue, 59 std::shared_ptr<GetValueCallbackFunc> callback) override; 60 61 void setValue(const IHalPropValue& value, 62 std::shared_ptr<HidlVhalClient::SetValueCallbackFunc> callback) override; 63 64 // Add the callback that would be called when VHAL binder died. 65 VhalClientResult<void> addOnBinderDiedCallback( 66 std::shared_ptr<OnBinderDiedCallbackFunc> callback) override; 67 68 // Remove a previously added OnBinderDied callback. 69 VhalClientResult<void> removeOnBinderDiedCallback( 70 std::shared_ptr<OnBinderDiedCallbackFunc> callback) override; 71 72 VhalClientResult<std::vector<std::unique_ptr<IHalPropConfig>>> getAllPropConfigs() override; 73 74 VhalClientResult<std::vector<std::unique_ptr<IHalPropConfig>>> getPropConfigs( 75 std::vector<int32_t> propIds) override; 76 77 std::unique_ptr<ISubscriptionClient> getSubscriptionClient( 78 std::shared_ptr<ISubscriptionCallback> callback) override; 79 80 private: 81 friend class hidl_test::HidlVhalClientTest; 82 83 class DeathRecipient : public android::hardware::hidl_death_recipient { 84 public: 85 explicit DeathRecipient(HidlVhalClient* client); 86 87 void serviceDied(uint64_t cookie, 88 const android::wp<android::hidl::base::V1_0::IBase>& who) override; 89 90 private: 91 HidlVhalClient* mClient; 92 }; 93 94 android::sp<::android::hardware::automotive::vehicle::V2_0::IVehicle> mHal; 95 android::sp<DeathRecipient> mDeathRecipient; 96 97 std::mutex mLock; 98 std::unordered_set<std::shared_ptr<OnBinderDiedCallbackFunc>> mOnBinderDiedCallbacks 99 GUARDED_BY(mLock); 100 101 void onBinderDied(); 102 }; 103 104 class SubscriptionCallback; 105 106 class HidlSubscriptionClient final : public ISubscriptionClient { 107 public: 108 ~HidlSubscriptionClient() = default; 109 110 HidlSubscriptionClient(android::sp<android::hardware::automotive::vehicle::V2_0::IVehicle> hal, 111 std::shared_ptr<ISubscriptionCallback> callback); 112 113 VhalClientResult<void> subscribe( 114 const std::vector<aidl::android::hardware::automotive::vehicle::SubscribeOptions>& 115 options) override; 116 VhalClientResult<void> unsubscribe(const std::vector<int32_t>& propIds) override; 117 118 private: 119 std::shared_ptr<ISubscriptionCallback> mCallback; 120 android::sp<android::hardware::automotive::vehicle::V2_0::IVehicle> mHal; 121 android::sp<SubscriptionCallback> mVhalCallback; 122 }; 123 124 class SubscriptionCallback : public android::hardware::automotive::vehicle::V2_0::IVehicleCallback { 125 public: 126 explicit SubscriptionCallback(std::shared_ptr<ISubscriptionCallback> callback); 127 128 android::hardware::Return<void> onPropertyEvent( 129 const android::hardware::hidl_vec< 130 hardware::automotive::vehicle::V2_0::VehiclePropValue>& propValues) override; 131 android::hardware::Return<void> onPropertySet( 132 const android::hardware::automotive::vehicle::V2_0::VehiclePropValue& propValue) 133 override; 134 android::hardware::Return<void> onPropertySetError( 135 android::hardware::automotive::vehicle::V2_0::StatusCode status, int32_t propId, 136 int32_t areaId) override; 137 138 private: 139 std::shared_ptr<ISubscriptionCallback> mCallback; 140 }; 141 142 } // namespace vhal 143 } // namespace automotive 144 } // namespace frameworks 145 } // namespace android 146 147 #endif // CPP_VHAL_CLIENT_INCLUDE_HIDLVHALCLIENT_H_ 148