• 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 #include "MockVehicleCallback.h"
18 
19 #include <android-base/thread_annotations.h>
20 #include <chrono>
21 
22 namespace android {
23 namespace hardware {
24 namespace automotive {
25 namespace vehicle {
26 
27 namespace {
28 
29 using ::aidl::android::hardware::automotive::vehicle::GetValueResults;
30 using ::aidl::android::hardware::automotive::vehicle::PropIdAreaId;
31 using ::aidl::android::hardware::automotive::vehicle::SetValueResults;
32 using ::aidl::android::hardware::automotive::vehicle::VehiclePropErrors;
33 using ::aidl::android::hardware::automotive::vehicle::VehiclePropValues;
34 using ::android::base::ScopedLockAssertion;
35 using ::ndk::ScopedAStatus;
36 using ::ndk::ScopedFileDescriptor;
37 
38 template <class T>
storeResults(const T & results,std::list<T> * storedResults)39 static ScopedAStatus storeResults(const T& results, std::list<T>* storedResults) {
40     T resultsCopy{
41             .payloads = results.payloads,
42     };
43     int fd = results.sharedMemoryFd.get();
44     if (fd != -1) {
45         resultsCopy.sharedMemoryFd = ScopedFileDescriptor(dup(fd));
46     }
47     storedResults->push_back(std::move(resultsCopy));
48     return ScopedAStatus::ok();
49 }
50 
51 }  // namespace
52 
onGetValues(const GetValueResults & results)53 ScopedAStatus MockVehicleCallback::onGetValues(const GetValueResults& results) {
54     ScopedAStatus result;
55     {
56         std::scoped_lock<std::mutex> lockGuard(mLock);
57         result = storeResults(results, &mGetValueResults);
58     }
59     mCond.notify_all();
60     return result;
61 }
62 
onSetValues(const SetValueResults & results)63 ScopedAStatus MockVehicleCallback::onSetValues(const SetValueResults& results) {
64     ScopedAStatus result;
65     {
66         std::scoped_lock<std::mutex> lockGuard(mLock);
67         result = storeResults(results, &mSetValueResults);
68     }
69     mCond.notify_all();
70     return result;
71 }
72 
onPropertyEvent(const VehiclePropValues & results,int32_t sharedMemoryFileCount)73 ScopedAStatus MockVehicleCallback::onPropertyEvent(const VehiclePropValues& results,
74                                                    int32_t sharedMemoryFileCount) {
75     ScopedAStatus result;
76     {
77         std::scoped_lock<std::mutex> lockGuard(mLock);
78         mSharedMemoryFileCount = sharedMemoryFileCount;
79         result = storeResults(results, &mOnPropertyEventResults);
80     }
81     mCond.notify_all();
82     return result;
83 }
84 
onPropertySetError(const VehiclePropErrors & results)85 ScopedAStatus MockVehicleCallback::onPropertySetError(const VehiclePropErrors& results) {
86     ScopedAStatus result;
87     {
88         std::scoped_lock<std::mutex> lockGuard(mLock);
89         result = storeResults(results, &mOnPropertySetErrorResults);
90     }
91     mCond.notify_all();
92     return result;
93 }
94 
onSupportedValueChange(const std::vector<PropIdAreaId> & propIdAreaIds)95 ScopedAStatus MockVehicleCallback::onSupportedValueChange(
96         const std::vector<PropIdAreaId>& propIdAreaIds) {
97     {
98         std::scoped_lock<std::mutex> lockGuard(mLock);
99         mOnSupportedValueChangePropIdAreaIds = propIdAreaIds;
100     }
101     mCond.notify_all();
102     return ScopedAStatus::ok();
103 }
104 
nextGetValueResults()105 std::optional<GetValueResults> MockVehicleCallback::nextGetValueResults() {
106     std::scoped_lock<std::mutex> lockGuard(mLock);
107     return pop(mGetValueResults);
108 }
109 
nextSetValueResults()110 std::optional<SetValueResults> MockVehicleCallback::nextSetValueResults() {
111     std::scoped_lock<std::mutex> lockGuard(mLock);
112     return pop(mSetValueResults);
113 }
114 
nextOnPropertyEventResults()115 std::optional<VehiclePropValues> MockVehicleCallback::nextOnPropertyEventResults() {
116     std::scoped_lock<std::mutex> lockGuard(mLock);
117     return pop(mOnPropertyEventResults);
118 }
119 
countOnPropertyEventResults()120 size_t MockVehicleCallback::countOnPropertyEventResults() {
121     std::scoped_lock<std::mutex> lockGuard(mLock);
122     return mOnPropertyEventResults.size();
123 }
124 
nextOnPropertySetErrorResults()125 std::optional<VehiclePropErrors> MockVehicleCallback::nextOnPropertySetErrorResults() {
126     std::scoped_lock<std::mutex> lockGuard(mLock);
127     return pop(mOnPropertySetErrorResults);
128 }
129 
countOnPropertySetErrorResults()130 size_t MockVehicleCallback::countOnPropertySetErrorResults() {
131     std::scoped_lock<std::mutex> lockGuard(mLock);
132     return mOnPropertySetErrorResults.size();
133 }
134 
waitForSetValueResults(size_t size,size_t timeoutInNano)135 bool MockVehicleCallback::waitForSetValueResults(size_t size, size_t timeoutInNano) {
136     std::unique_lock lk(mLock);
137     return mCond.wait_for(lk, std::chrono::nanoseconds(timeoutInNano), [this, size] {
138         ScopedLockAssertion lockAssertion(mLock);
139         return mSetValueResults.size() >= size;
140     });
141 }
142 
waitForGetValueResults(size_t size,size_t timeoutInNano)143 bool MockVehicleCallback::waitForGetValueResults(size_t size, size_t timeoutInNano) {
144     std::unique_lock lk(mLock);
145     return mCond.wait_for(lk, std::chrono::nanoseconds(timeoutInNano), [this, size] {
146         ScopedLockAssertion lockAssertion(mLock);
147         return mGetValueResults.size() >= size;
148     });
149 }
150 
waitForOnPropertyEventResults(size_t size,size_t timeoutInNano)151 bool MockVehicleCallback::waitForOnPropertyEventResults(size_t size, size_t timeoutInNano) {
152     std::unique_lock lk(mLock);
153     return mCond.wait_for(lk, std::chrono::nanoseconds(timeoutInNano), [this, size] {
154         ScopedLockAssertion lockAssertion(mLock);
155         return mOnPropertyEventResults.size() >= size;
156     });
157 }
158 
waitForOnSupportedValueChange(size_t size,size_t timeoutInNano)159 bool MockVehicleCallback::waitForOnSupportedValueChange(size_t size, size_t timeoutInNano) {
160     std::unique_lock lk(mLock);
161     return mCond.wait_for(lk, std::chrono::nanoseconds(timeoutInNano), [this, size] {
162         ScopedLockAssertion lockAssertion(mLock);
163         return mOnSupportedValueChangePropIdAreaIds.size() >= size;
164     });
165 }
166 
getOnSupportedValueChangePropIdAreaIds()167 std::vector<PropIdAreaId> MockVehicleCallback::getOnSupportedValueChangePropIdAreaIds() {
168     std::scoped_lock<std::mutex> lockGuard(mLock);
169     return mOnSupportedValueChangePropIdAreaIds;
170 }
171 
172 }  // namespace vehicle
173 }  // namespace automotive
174 }  // namespace hardware
175 }  // namespace android
176