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_SENSORS_AIDL_ENVIRONMENT_H 18 #define ANDROID_SENSORS_AIDL_ENVIRONMENT_H 19 20 #include "sensors-vts-utils/SensorsVtsEnvironmentBase.h" 21 22 #include <atomic> 23 #include <memory> 24 #include <mutex> 25 #include <thread> 26 #include <vector> 27 28 #include <aidl/android/hardware/sensors/ISensors.h> 29 #include <fmq/AidlMessageQueue.h> 30 31 using aidl::android::hardware::common::fmq::SynchronizedReadWrite; 32 using aidl::android::hardware::sensors::Event; 33 using aidl::android::hardware::sensors::ISensors; 34 using aidl::android::hardware::sensors::ISensorsCallback; 35 36 static constexpr size_t MAX_RECEIVE_BUFFER_EVENT_COUNT = 256; 37 38 class SensorsAidlTest; 39 40 class SensorsAidlEnvironment : public SensorsVtsEnvironmentBase<Event> { 41 public: 42 virtual void TearDown() override; 43 44 protected: 45 friend SensorsAidlTest; 46 SensorsAidlEnvironment(const std::string& service_name); 47 48 /** 49 * Resets the HAL with new FMQs and a new Event Flag 50 * 51 * @return bool true if successful, false otherwise 52 */ 53 bool resetHal() override; 54 55 /** 56 * Starts the polling thread that reads sensor events from the Event FMQ 57 */ 58 void startPollingThread() override; 59 60 /** 61 * Thread responsible for calling functions to read Event FMQ 62 * 63 * @param env SensorEnvironment to being polling for events on 64 */ 65 static void pollingThread(SensorsAidlEnvironment* env); 66 67 /** 68 * Reads and saves sensor events from the Event FMQ 69 */ 70 void readEvents(); 71 72 /** 73 * Pointer to the Sensors HAL Interface that allows the test to call HAL functions. 74 */ 75 std::shared_ptr<ISensors> mSensors; 76 std::shared_ptr<ISensorsCallback> mCallback; 77 78 ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; 79 80 /** 81 * Type used to simplify the creation of the Wake Lock FMQ 82 */ 83 typedef android::AidlMessageQueue<int32_t, SynchronizedReadWrite> WakeLockQueue; 84 typedef android::AidlMessageQueue<Event, SynchronizedReadWrite> EventQueue; 85 86 /** 87 * The Wake Lock FMQ is used by the test to notify the Sensors HAL whenever it has processed 88 * WAKE_UP sensor events. 89 */ 90 std::unique_ptr<WakeLockQueue> mWakeLockQueue; 91 std::unique_ptr<EventQueue> mEventQueue; 92 93 /** 94 * The Event Queue Flag notifies the test framework when sensor events have been written to the 95 * Event FMQ by the Sensors HAL. 96 */ 97 ::android::hardware::EventFlag* mEventQueueFlag; 98 99 std::atomic_bool mStopThread; 100 std::thread mPollThread; 101 102 /** 103 * An array that is used to store sensor events read from the Event FMQ 104 */ 105 std::array<Event, MAX_RECEIVE_BUFFER_EVENT_COUNT> mEventBuffer; 106 }; 107 108 #endif // ANDROID_SENSORS_AIDL_ENVIRONMENT_H 109