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 <condition_variable> 28 #include <list> 29 #include <memory> 30 #include <mutex> 31 #include <thread> 32 #include <unordered_map> 33 #include <vector> 34 35 namespace android { 36 namespace hardware { 37 namespace automotive { 38 namespace vehicle { 39 40 class MockVehicleHardware final : public IVehicleHardware { 41 public: 42 MockVehicleHardware(); 43 44 ~MockVehicleHardware(); 45 46 std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig> 47 getAllPropertyConfigs() const override; 48 aidl::android::hardware::automotive::vehicle::StatusCode setValues( 49 std::shared_ptr<const SetValuesCallback> callback, 50 const std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest>& 51 requests) override; 52 aidl::android::hardware::automotive::vehicle::StatusCode getValues( 53 std::shared_ptr<const GetValuesCallback> callback, 54 const std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>& 55 requests) const override; 56 DumpResult dump(const std::vector<std::string>&) override; 57 aidl::android::hardware::automotive::vehicle::StatusCode checkHealth() override; 58 void registerOnPropertyChangeEvent( 59 std::unique_ptr<const PropertyChangeCallback> callback) override; 60 void registerOnPropertySetErrorEvent(std::unique_ptr<const PropertySetErrorCallback>) override; 61 aidl::android::hardware::automotive::vehicle::StatusCode updateSampleRate( 62 int32_t propId, int32_t areaId, float sampleRate) override; 63 64 // Test functions. 65 void setPropertyConfigs( 66 const std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig>& 67 configs); 68 void addGetValueResponses( 69 const std::vector<aidl::android::hardware::automotive::vehicle::GetValueResult>& 70 responses); 71 void addSetValueResponses( 72 const std::vector<aidl::android::hardware::automotive::vehicle::SetValueResult>& 73 responses); 74 void setGetValueResponder( 75 std::function<aidl::android::hardware::automotive::vehicle::StatusCode( 76 std::shared_ptr<const GetValuesCallback>, 77 const std::vector< 78 aidl::android::hardware::automotive::vehicle::GetValueRequest>&)>&& 79 responder); 80 std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest> 81 nextGetValueRequests(); 82 std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest> 83 nextSetValueRequests(); 84 void setStatus(const char* functionName, 85 aidl::android::hardware::automotive::vehicle::StatusCode status); 86 void setSleepTime(int64_t timeInNano); 87 void setDumpResult(DumpResult result); 88 void sendOnPropertySetErrorEvent(const std::vector<SetValueErrorEvent>& errorEvents); 89 90 private: 91 mutable std::mutex mLock; 92 mutable std::condition_variable mCv; 93 mutable std::atomic<int> mThreadCount; 94 std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig> mPropertyConfigs 95 GUARDED_BY(mLock); 96 mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>> 97 mGetValueRequests GUARDED_BY(mLock); 98 mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::GetValueResult>> 99 mGetValueResponses GUARDED_BY(mLock); 100 mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest>> 101 mSetValueRequests GUARDED_BY(mLock); 102 mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::SetValueResult>> 103 mSetValueResponses GUARDED_BY(mLock); 104 std::unordered_map<const char*, aidl::android::hardware::automotive::vehicle::StatusCode> 105 mStatusByFunctions GUARDED_BY(mLock); 106 int64_t mSleepTime GUARDED_BY(mLock) = 0; 107 std::unique_ptr<const PropertyChangeCallback> mPropertyChangeCallback GUARDED_BY(mLock); 108 std::unique_ptr<const PropertySetErrorCallback> mPropertySetErrorCallback GUARDED_BY(mLock); 109 std::function<aidl::android::hardware::automotive::vehicle::StatusCode( 110 std::shared_ptr<const GetValuesCallback>, 111 const std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>&)> 112 mGetValueResponder GUARDED_BY(mLock); 113 114 template <class ResultType> 115 aidl::android::hardware::automotive::vehicle::StatusCode returnResponse( 116 std::shared_ptr<const std::function<void(std::vector<ResultType>)>> callback, 117 std::list<std::vector<ResultType>>* storedResponses) const; 118 template <class RequestType, class ResultType> 119 aidl::android::hardware::automotive::vehicle::StatusCode handleRequestsLocked( 120 const char* functionName, 121 std::shared_ptr<const std::function<void(std::vector<ResultType>)>> callback, 122 const std::vector<RequestType>& requests, 123 std::list<std::vector<RequestType>>* storedRequests, 124 std::list<std::vector<ResultType>>* storedResponses) const REQUIRES(mLock); 125 126 DumpResult mDumpResult; 127 128 // RecurrentTimer is thread-safe. 129 std::shared_ptr<RecurrentTimer> mRecurrentTimer; 130 std::unordered_map<int32_t, std::unordered_map<int32_t, std::shared_ptr<std::function<void()>>>> 131 mRecurrentActions GUARDED_BY(mLock); 132 }; 133 134 } // namespace vehicle 135 } // namespace automotive 136 } // namespace hardware 137 } // namespace android 138 139 #endif // android_hardware_automotive_vehicle_aidl_impl_vhal_test_MockVehicleHardware_H_ 140