• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
89   private:
90     mutable std::mutex mLock;
91     mutable std::condition_variable mCv;
92     mutable std::atomic<int> mThreadCount;
93     std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig> mPropertyConfigs
94             GUARDED_BY(mLock);
95     mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>>
96             mGetValueRequests GUARDED_BY(mLock);
97     mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::GetValueResult>>
98             mGetValueResponses GUARDED_BY(mLock);
99     mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest>>
100             mSetValueRequests GUARDED_BY(mLock);
101     mutable std::list<std::vector<aidl::android::hardware::automotive::vehicle::SetValueResult>>
102             mSetValueResponses GUARDED_BY(mLock);
103     std::unordered_map<const char*, aidl::android::hardware::automotive::vehicle::StatusCode>
104             mStatusByFunctions GUARDED_BY(mLock);
105     int64_t mSleepTime GUARDED_BY(mLock) = 0;
106     std::unique_ptr<const PropertyChangeCallback> mPropertyChangeCallback GUARDED_BY(mLock);
107     std::function<aidl::android::hardware::automotive::vehicle::StatusCode(
108             std::shared_ptr<const GetValuesCallback>,
109             const std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>&)>
110             mGetValueResponder GUARDED_BY(mLock);
111 
112     template <class ResultType>
113     aidl::android::hardware::automotive::vehicle::StatusCode returnResponse(
114             std::shared_ptr<const std::function<void(std::vector<ResultType>)>> callback,
115             std::list<std::vector<ResultType>>* storedResponses) const;
116     template <class RequestType, class ResultType>
117     aidl::android::hardware::automotive::vehicle::StatusCode handleRequestsLocked(
118             const char* functionName,
119             std::shared_ptr<const std::function<void(std::vector<ResultType>)>> callback,
120             const std::vector<RequestType>& requests,
121             std::list<std::vector<RequestType>>* storedRequests,
122             std::list<std::vector<ResultType>>* storedResponses) const REQUIRES(mLock);
123 
124     DumpResult mDumpResult;
125 
126     // RecurrentTimer is thread-safe.
127     std::shared_ptr<RecurrentTimer> mRecurrentTimer;
128     std::unordered_map<int32_t, std::unordered_map<int32_t, std::shared_ptr<std::function<void()>>>>
129             mRecurrentActions GUARDED_BY(mLock);
130 };
131 
132 }  // namespace vehicle
133 }  // namespace automotive
134 }  // namespace hardware
135 }  // namespace android
136 
137 #endif  // android_hardware_automotive_vehicle_aidl_impl_vhal_test_MockVehicleHardware_H_
138