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_MockVehicleCallback_H_ 18 #define android_hardware_automotive_vehicle_aidl_impl_vhal_test_MockVehicleCallback_H_ 19 20 #include <VehicleHalTypes.h> 21 22 #include <aidl/android/hardware/automotive/vehicle/BnVehicleCallback.h> 23 #include <android-base/thread_annotations.h> 24 25 #include <condition_variable> 26 #include <list> 27 #include <mutex> 28 #include <optional> 29 30 namespace android { 31 namespace hardware { 32 namespace automotive { 33 namespace vehicle { 34 35 template <class T> pop(std::list<T> & items)36std::optional<T> pop(std::list<T>& items) { 37 if (items.size() > 0) { 38 auto item = std::move(items.front()); 39 items.pop_front(); 40 return item; 41 } 42 return std::nullopt; 43 } 44 45 // MockVehicleCallback is a mock VehicleCallback implementation that simply stores the results. 46 class MockVehicleCallback final 47 : public aidl::android::hardware::automotive::vehicle::BnVehicleCallback { 48 public: 49 ndk::ScopedAStatus onGetValues( 50 const aidl::android::hardware::automotive::vehicle::GetValueResults& results) override; 51 ndk::ScopedAStatus onSetValues( 52 const aidl::android::hardware::automotive::vehicle::SetValueResults& results) override; 53 ndk::ScopedAStatus onPropertyEvent( 54 const aidl::android::hardware::automotive::vehicle::VehiclePropValues&, 55 int32_t) override; 56 ndk::ScopedAStatus onPropertySetError( 57 const aidl::android::hardware::automotive::vehicle::VehiclePropErrors&) override; 58 59 // Test functions 60 std::optional<aidl::android::hardware::automotive::vehicle::GetValueResults> 61 nextGetValueResults(); 62 std::optional<aidl::android::hardware::automotive::vehicle::SetValueResults> 63 nextSetValueResults(); 64 std::optional<aidl::android::hardware::automotive::vehicle::VehiclePropValues> 65 nextOnPropertyEventResults(); 66 size_t countOnPropertySetErrorResults(); 67 std::optional<aidl::android::hardware::automotive::vehicle::VehiclePropErrors> 68 nextOnPropertySetErrorResults(); 69 size_t countOnPropertyEventResults(); 70 bool waitForSetValueResults(size_t size, size_t timeoutInNano); 71 bool waitForGetValueResults(size_t size, size_t timeoutInNano); 72 bool waitForOnPropertyEventResults(size_t size, size_t timeoutInNano); 73 74 private: 75 std::mutex mLock; 76 std::condition_variable mCond; 77 std::list<aidl::android::hardware::automotive::vehicle::GetValueResults> mGetValueResults 78 GUARDED_BY(mLock); 79 std::list<aidl::android::hardware::automotive::vehicle::SetValueResults> mSetValueResults 80 GUARDED_BY(mLock); 81 std::list<aidl::android::hardware::automotive::vehicle::VehiclePropValues> 82 mOnPropertyEventResults GUARDED_BY(mLock); 83 int32_t mSharedMemoryFileCount GUARDED_BY(mLock); 84 std::list<aidl::android::hardware::automotive::vehicle::VehiclePropErrors> 85 mOnPropertySetErrorResults GUARDED_BY(mLock); 86 }; 87 88 } // namespace vehicle 89 } // namespace automotive 90 } // namespace hardware 91 } // namespace android 92 93 #endif // android_hardware_automotive_vehicle_aidl_impl_vhal_test_MockVehicleCallback_H_ 94