Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 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 | GTEST_DISALLOW_COPY_AND_ASSIGN_(SensorsAidlEnvironment); |
| 73 | |
| 74 | /** |
| 75 | * Pointer to the Sensors HAL Interface that allows the test to call HAL functions. |
| 76 | */ |
| 77 | std::shared_ptr<ISensors> mSensors; |
| 78 | std::shared_ptr<ISensorsCallback> mCallback; |
| 79 | |
| 80 | ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; |
| 81 | |
| 82 | /** |
| 83 | * Type used to simplify the creation of the Wake Lock FMQ |
| 84 | */ |
| 85 | typedef android::AidlMessageQueue<int32_t, SynchronizedReadWrite> WakeLockQueue; |
| 86 | typedef android::AidlMessageQueue<Event, SynchronizedReadWrite> EventQueue; |
| 87 | |
| 88 | /** |
| 89 | * The Wake Lock FMQ is used by the test to notify the Sensors HAL whenever it has processed |
| 90 | * WAKE_UP sensor events. |
| 91 | */ |
| 92 | std::unique_ptr<WakeLockQueue> mWakeLockQueue; |
| 93 | std::unique_ptr<EventQueue> mEventQueue; |
| 94 | |
| 95 | /** |
| 96 | * The Event Queue Flag notifies the test framework when sensor events have been written to the |
| 97 | * Event FMQ by the Sensors HAL. |
| 98 | */ |
| 99 | ::android::hardware::EventFlag* mEventQueueFlag; |
| 100 | |
| 101 | std::atomic_bool mStopThread; |
| 102 | std::thread mPollThread; |
| 103 | |
| 104 | /** |
| 105 | * An array that is used to store sensor events read from the Event FMQ |
| 106 | */ |
| 107 | std::array<Event, MAX_RECEIVE_BUFFER_EVENT_COUNT> mEventBuffer; |
| 108 | }; |
| 109 | |
| 110 | #endif // ANDROID_SENSORS_AIDL_ENVIRONMENT_H |