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 aidl::android::hardware::automotive::vehicle::StatusCode setValues( 51 std::shared_ptr<const SetValuesCallback> callback, 52 const std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest>& 53 requests) override; 54 aidl::android::hardware::automotive::vehicle::StatusCode getValues( 55 std::shared_ptr<const GetValuesCallback> callback, 56 const std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>& 57 requests) const override; 58 DumpResult dump(const std::vector<std::string>&) override; 59 aidl::android::hardware::automotive::vehicle::StatusCode checkHealth() override; 60 void registerOnPropertyChangeEvent( 61 std::unique_ptr<const PropertyChangeCallback> callback) override; 62 void registerOnPropertySetErrorEvent(std::unique_ptr<const PropertySetErrorCallback>) override; 63 aidl::android::hardware::automotive::vehicle::StatusCode subscribe( 64 aidl::android::hardware::automotive::vehicle::SubscribeOptions options) override; 65 aidl::android::hardware::automotive::vehicle::StatusCode unsubscribe(int32_t propId, 66 int32_t areaId) override; 67 std::chrono::nanoseconds getPropertyOnChangeEventBatchingWindow() override; 68 69 // Test functions. 70 void setPropertyConfigs( 71 const std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig>& 72 configs); 73 void addGetValueResponses( 74 const std::vector<aidl::android::hardware::automotive::vehicle::GetValueResult>& 75 responses); 76 void addSetValueResponses( 77 const std::vector<aidl::android::hardware::automotive::vehicle::SetValueResult>& 78 responses); 79 void setGetValueResponder( 80 std::function<aidl::android::hardware::automotive::vehicle::StatusCode( 81 std::shared_ptr<const GetValuesCallback>, 82 const std::vector< 83 aidl::android::hardware::automotive::vehicle::GetValueRequest>&)>&& 84 responder); 85 std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest> 86 nextGetValueRequests(); 87 std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest> 88 nextSetValueRequests(); 89 void setStatus(const char* functionName, 90 aidl::android::hardware::automotive::vehicle::StatusCode status); 91 void setSleepTime(int64_t timeInNano); 92 void setDumpResult(DumpResult result); 93 void sendOnPropertySetErrorEvent(const std::vector<SetValueErrorEvent>& errorEvents); 94 void setPropertyOnChangeEventBatchingWindow(std::chrono::nanoseconds window); 95 96 std::set<std::pair<int32_t, int32_t>> getSubscribedOnChangePropIdAreaIds(); 97 std::set<std::pair<int32_t, int32_t>> getSubscribedContinuousPropIdAreaIds(); 98 std::vector<aidl::android::hardware::automotive::vehicle::SubscribeOptions> 99 getSubscribeOptions(); 100 void clearSubscribeOptions(); 101 102 private: 103 mutable std::mutex mLock; 104 mutable std::condition_variable mCv; 105 mutable std::atomic<int> mThreadCount; 106 std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig> mPropertyConfigs 107 GUARDED_BY(mLock); 108 mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>> 109 mGetValueRequests GUARDED_BY(mLock); 110 mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::GetValueResult>> 111 mGetValueResponses GUARDED_BY(mLock); 112 mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest>> 113 mSetValueRequests GUARDED_BY(mLock); 114 mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::SetValueResult>> 115 mSetValueResponses GUARDED_BY(mLock); 116 std::unordered_map<const char*, aidl::android::hardware::automotive::vehicle::StatusCode> 117 mStatusByFunctions GUARDED_BY(mLock); 118 int64_t mSleepTime GUARDED_BY(mLock) = 0; 119 std::unique_ptr<const PropertyChangeCallback> mPropertyChangeCallback GUARDED_BY(mLock); 120 std::unique_ptr<const PropertySetErrorCallback> mPropertySetErrorCallback GUARDED_BY(mLock); 121 std::function<aidl::android::hardware::automotive::vehicle::StatusCode( 122 std::shared_ptr<const GetValuesCallback>, 123 const std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>&)> 124 mGetValueResponder GUARDED_BY(mLock); 125 std::chrono::nanoseconds mEventBatchingWindow GUARDED_BY(mLock) = std::chrono::nanoseconds(0); 126 std::set<std::pair<int32_t, int32_t>> mSubOnChangePropIdAreaIds GUARDED_BY(mLock); 127 std::vector<aidl::android::hardware::automotive::vehicle::SubscribeOptions> mSubscribeOptions 128 GUARDED_BY(mLock); 129 130 template <class ResultType> 131 aidl::android::hardware::automotive::vehicle::StatusCode returnResponse( 132 std::shared_ptr<const std::function<void(std::vector<ResultType>)>> callback, 133 std::list<std::vector<ResultType>>* storedResponses) const; 134 template <class RequestType, class ResultType> 135 aidl::android::hardware::automotive::vehicle::StatusCode handleRequestsLocked( 136 const char* functionName, 137 std::shared_ptr<const std::function<void(std::vector<ResultType>)>> callback, 138 const std::vector<RequestType>& requests, 139 std::list<std::vector<RequestType>>* storedRequests, 140 std::list<std::vector<ResultType>>* storedResponses) const REQUIRES(mLock); 141 aidl::android::hardware::automotive::vehicle::StatusCode subscribePropIdAreaId( 142 int32_t propId, int32_t areaId, float sampleRateHz); 143 144 DumpResult mDumpResult; 145 146 // RecurrentTimer is thread-safe. 147 std::shared_ptr<RecurrentTimer> mRecurrentTimer; 148 std::unordered_map<int32_t, std::unordered_map<int32_t, std::shared_ptr<std::function<void()>>>> 149 mRecurrentActions GUARDED_BY(mLock); 150 }; 151 152 } // namespace vehicle 153 } // namespace automotive 154 } // namespace hardware 155 } // namespace android 156 157 #endif // android_hardware_automotive_vehicle_aidl_impl_vhal_test_MockVehicleHardware_H_ 158