1 /* 2 * Copyright (C) 2021 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 android_hardware_automotive_vehicle_aidl_impl_vhal_test_MockVehicleHardware_H_ 18 #define android_hardware_automotive_vehicle_aidl_impl_vhal_test_MockVehicleHardware_H_ 19 20 #include <IVehicleHardware.h> 21 #include <RecurrentTimer.h> 22 #include <VehicleHalTypes.h> 23 24 #include <android-base/thread_annotations.h> 25 26 #include <atomic> 27 #include <chrono> 28 #include <condition_variable> 29 #include <list> 30 #include <memory> 31 #include <mutex> 32 #include <set> 33 #include <thread> 34 #include <unordered_map> 35 #include <vector> 36 37 namespace android { 38 namespace hardware { 39 namespace automotive { 40 namespace vehicle { 41 42 class MockVehicleHardware final : public IVehicleHardware { 43 public: 44 MockVehicleHardware(); 45 46 ~MockVehicleHardware(); 47 48 std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig> 49 getAllPropertyConfigs() const override; 50 std::optional<aidl::android::hardware::automotive::vehicle::VehiclePropConfig> 51 getPropertyConfig(int32_t propId) const override; 52 aidl::android::hardware::automotive::vehicle::StatusCode setValues( 53 std::shared_ptr<const SetValuesCallback> callback, 54 const std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest>& 55 requests) override; 56 aidl::android::hardware::automotive::vehicle::StatusCode getValues( 57 std::shared_ptr<const GetValuesCallback> callback, 58 const std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>& 59 requests) const override; 60 DumpResult dump(const std::vector<std::string>&) override; 61 aidl::android::hardware::automotive::vehicle::StatusCode checkHealth() override; 62 void registerOnPropertyChangeEvent( 63 std::unique_ptr<const PropertyChangeCallback> callback) override; 64 void registerOnPropertySetErrorEvent(std::unique_ptr<const PropertySetErrorCallback>) override; 65 void registerSupportedValueChangeCallback( 66 std::unique_ptr<const SupportedValueChangeCallback>) override; 67 aidl::android::hardware::automotive::vehicle::StatusCode subscribe( 68 aidl::android::hardware::automotive::vehicle::SubscribeOptions options) override; 69 aidl::android::hardware::automotive::vehicle::StatusCode unsubscribe(int32_t propId, 70 int32_t areaId) override; 71 std::chrono::nanoseconds getPropertyOnChangeEventBatchingWindow() override; 72 std::vector<aidl::android::hardware::automotive::vehicle::SupportedValuesListResult> 73 getSupportedValuesLists(const std::vector<PropIdAreaId>& propIdAreaIds) override; 74 std::vector<aidl::android::hardware::automotive::vehicle::MinMaxSupportedValueResult> 75 getMinMaxSupportedValues(const std::vector<PropIdAreaId>& propIdAreaIds) override; 76 aidl::android::hardware::automotive::vehicle::StatusCode subscribeSupportedValueChange( 77 const std::vector<PropIdAreaId>& propIdAreaIds) override; 78 aidl::android::hardware::automotive::vehicle::StatusCode unsubscribeSupportedValueChange( 79 const std::vector<PropIdAreaId>& propIdAreaIds) override; 80 81 // Test functions. 82 void setPropertyConfigs( 83 const std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig>& 84 configs); 85 void addGetValueResponses( 86 const std::vector<aidl::android::hardware::automotive::vehicle::GetValueResult>& 87 responses); 88 void addSetValueResponses( 89 const std::vector<aidl::android::hardware::automotive::vehicle::SetValueResult>& 90 responses); 91 void setSupportedValuesListResponse( 92 const std::vector< 93 aidl::android::hardware::automotive::vehicle::SupportedValuesListResult>& 94 response); 95 void setMinMaxSupportedValueResponse( 96 const std::vector< 97 aidl::android::hardware::automotive::vehicle::MinMaxSupportedValueResult>& 98 response); 99 void setGetValueResponder( 100 std::function<aidl::android::hardware::automotive::vehicle::StatusCode( 101 std::shared_ptr<const GetValuesCallback>, 102 const std::vector< 103 aidl::android::hardware::automotive::vehicle::GetValueRequest>&)>&& 104 responder); 105 std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest> 106 nextGetValueRequests(); 107 std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest> 108 nextSetValueRequests(); 109 std::vector<PropIdAreaId> getSupportedValuesListRequest(); 110 std::vector<PropIdAreaId> getMinMaxSupportedValueRequest(); 111 void setStatus(const char* functionName, 112 aidl::android::hardware::automotive::vehicle::StatusCode status); 113 void setSleepTime(int64_t timeInNano); 114 void setDumpResult(DumpResult result); 115 void sendOnPropertySetErrorEvent(const std::vector<SetValueErrorEvent>& errorEvents); 116 void setPropertyOnChangeEventBatchingWindow(std::chrono::nanoseconds window); 117 void sendSupportedValueChangeEvent(const std::vector<PropIdAreaId>& propIdAreaIds); 118 119 std::set<std::pair<int32_t, int32_t>> getSubscribedOnChangePropIdAreaIds(); 120 std::set<std::pair<int32_t, int32_t>> getSubscribedContinuousPropIdAreaIds(); 121 std::vector<aidl::android::hardware::automotive::vehicle::SubscribeOptions> 122 getSubscribeOptions(); 123 void clearSubscribeOptions(); 124 // Whether getAllPropertyConfigs() has been called, which blocks on all property configs 125 // being ready. 126 bool getAllPropertyConfigsCalled(); 127 128 std::unordered_set<PropIdAreaId, PropIdAreaIdHash> 129 getSubscribedSupportedValueChangePropIdAreaIds(); 130 131 private: 132 mutable std::mutex mLock; 133 mutable std::condition_variable mCv; 134 mutable std::atomic<int> mThreadCount; 135 std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig> mPropertyConfigs 136 GUARDED_BY(mLock); 137 mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>> 138 mGetValueRequests GUARDED_BY(mLock); 139 mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::GetValueResult>> 140 mGetValueResponses GUARDED_BY(mLock); 141 mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest>> 142 mSetValueRequests GUARDED_BY(mLock); 143 mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::SetValueResult>> 144 mSetValueResponses GUARDED_BY(mLock); 145 mutable std::vector<PropIdAreaId> mSupportedValuesListRequest GUARDED_BY(mLock); 146 mutable std::vector<PropIdAreaId> mMinMaxSupportedValueRequest GUARDED_BY(mLock); 147 mutable std::vector<aidl::android::hardware::automotive::vehicle::SupportedValuesListResult> 148 mSupportedValuesListResponse GUARDED_BY(mLock); 149 mutable std::vector<aidl::android::hardware::automotive::vehicle::MinMaxSupportedValueResult> 150 mMinMaxSupportedValueResponse GUARDED_BY(mLock); 151 std::unordered_map<const char*, aidl::android::hardware::automotive::vehicle::StatusCode> 152 mStatusByFunctions GUARDED_BY(mLock); 153 int64_t mSleepTime GUARDED_BY(mLock) = 0; 154 std::unique_ptr<const PropertyChangeCallback> mPropertyChangeCallback GUARDED_BY(mLock); 155 std::unique_ptr<const PropertySetErrorCallback> mPropertySetErrorCallback GUARDED_BY(mLock); 156 std::unique_ptr<const SupportedValueChangeCallback> mSupportedValueChangeCallback 157 GUARDED_BY(mLock); 158 std::function<aidl::android::hardware::automotive::vehicle::StatusCode( 159 std::shared_ptr<const GetValuesCallback>, 160 const std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>&)> 161 mGetValueResponder GUARDED_BY(mLock); 162 std::chrono::nanoseconds mEventBatchingWindow GUARDED_BY(mLock) = std::chrono::nanoseconds(0); 163 std::set<std::pair<int32_t, int32_t>> mSubOnChangePropIdAreaIds GUARDED_BY(mLock); 164 std::vector<aidl::android::hardware::automotive::vehicle::SubscribeOptions> mSubscribeOptions 165 GUARDED_BY(mLock); 166 167 template <class ResultType> 168 aidl::android::hardware::automotive::vehicle::StatusCode returnResponse( 169 std::shared_ptr<const std::function<void(std::vector<ResultType>)>> callback, 170 std::list<std::vector<ResultType>>* storedResponses) const; 171 template <class RequestType, class ResultType> 172 aidl::android::hardware::automotive::vehicle::StatusCode handleRequestsLocked( 173 const char* functionName, 174 std::shared_ptr<const std::function<void(std::vector<ResultType>)>> callback, 175 const std::vector<RequestType>& requests, 176 std::list<std::vector<RequestType>>* storedRequests, 177 std::list<std::vector<ResultType>>* storedResponses) const REQUIRES(mLock); 178 aidl::android::hardware::automotive::vehicle::StatusCode subscribePropIdAreaId( 179 int32_t propId, int32_t areaId, float sampleRateHz); 180 181 DumpResult mDumpResult; 182 183 mutable bool mGetAllPropertyConfigsCalled GUARDED_BY(mLock) = false; 184 185 // RecurrentTimer is thread-safe. 186 std::shared_ptr<RecurrentTimer> mRecurrentTimer; 187 std::unordered_map<int32_t, std::unordered_map<int32_t, std::shared_ptr<std::function<void()>>>> 188 mRecurrentActions GUARDED_BY(mLock); 189 190 std::unordered_set<PropIdAreaId, PropIdAreaIdHash> mSubscribedSupportedValueChangePropIdAreaIds 191 GUARDED_BY(mLock); 192 }; 193 194 } // namespace vehicle 195 } // namespace automotive 196 } // namespace hardware 197 } // namespace android 198 199 #endif // android_hardware_automotive_vehicle_aidl_impl_vhal_test_MockVehicleHardware_H_ 200