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 namespace android {
20 namespace hardware {
21 namespace automotive {
22 namespace vehicle {
23
24 namespace {
25
26 using ::aidl::android::hardware::automotive::vehicle::GetValueResults;
27 using ::aidl::android::hardware::automotive::vehicle::SetValueResults;
28 using ::aidl::android::hardware::automotive::vehicle::VehiclePropErrors;
29 using ::aidl::android::hardware::automotive::vehicle::VehiclePropValues;
30 using ::ndk::ScopedAStatus;
31 using ::ndk::ScopedFileDescriptor;
32
33 template <class T>
storeResults(const T & results,std::list<T> * storedResults)34 static ScopedAStatus storeResults(const T& results, std::list<T>* storedResults) {
35 T resultsCopy{
36 .payloads = results.payloads,
37 };
38 int fd = results.sharedMemoryFd.get();
39 if (fd != -1) {
40 resultsCopy.sharedMemoryFd = ScopedFileDescriptor(dup(fd));
41 }
42 storedResults->push_back(std::move(resultsCopy));
43 return ScopedAStatus::ok();
44 }
45
46 } // namespace
47
onGetValues(const GetValueResults & results)48 ScopedAStatus MockVehicleCallback::onGetValues(const GetValueResults& results) {
49 std::scoped_lock<std::mutex> lockGuard(mLock);
50 return storeResults(results, &mGetValueResults);
51 }
52
onSetValues(const SetValueResults & results)53 ScopedAStatus MockVehicleCallback::onSetValues(const SetValueResults& results) {
54 std::scoped_lock<std::mutex> lockGuard(mLock);
55 return storeResults(results, &mSetValueResults);
56 }
57
onPropertyEvent(const VehiclePropValues & results,int32_t sharedMemoryFileCount)58 ScopedAStatus MockVehicleCallback::onPropertyEvent(const VehiclePropValues& results,
59 int32_t sharedMemoryFileCount) {
60 std::scoped_lock<std::mutex> lockGuard(mLock);
61
62 mSharedMemoryFileCount = sharedMemoryFileCount;
63 return storeResults(results, &mOnPropertyEventResults);
64 }
65
onPropertySetError(const VehiclePropErrors &)66 ScopedAStatus MockVehicleCallback::onPropertySetError(const VehiclePropErrors&) {
67 return ScopedAStatus::ok();
68 }
69
nextGetValueResults()70 std::optional<GetValueResults> MockVehicleCallback::nextGetValueResults() {
71 std::scoped_lock<std::mutex> lockGuard(mLock);
72 return pop(mGetValueResults);
73 }
74
nextSetValueResults()75 std::optional<SetValueResults> MockVehicleCallback::nextSetValueResults() {
76 std::scoped_lock<std::mutex> lockGuard(mLock);
77 return pop(mSetValueResults);
78 }
79
nextOnPropertyEventResults()80 std::optional<VehiclePropValues> MockVehicleCallback::nextOnPropertyEventResults() {
81 std::scoped_lock<std::mutex> lockGuard(mLock);
82 return pop(mOnPropertyEventResults);
83 }
84
countOnPropertyEventResults()85 size_t MockVehicleCallback::countOnPropertyEventResults() {
86 std::scoped_lock<std::mutex> lockGuard(mLock);
87 return mOnPropertyEventResults.size();
88 }
89
90 } // namespace vehicle
91 } // namespace automotive
92 } // namespace hardware
93 } // namespace android
94