1 /*
2  * Copyright (C) 2018 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_HIDL_ENVIRONMENT_V2_X_H
18 #define ANDROID_SENSORS_HIDL_ENVIRONMENT_V2_X_H
19 
20 #include "ISensorsWrapper.h"
21 #include "sensors-vts-utils/SensorsHidlEnvironmentBase.h"
22 
23 #include <android/hardware/sensors/2.1/ISensors.h>
24 #include <android/hardware/sensors/2.1/types.h>
25 
26 #include <fmq/MessageQueue.h>
27 #include <utils/StrongPointer.h>
28 
29 #include <array>
30 #include <atomic>
31 #include <memory>
32 
33 using ::android::sp;
34 using ::android::hardware::MessageQueue;
35 using ::android::hardware::sensors::V2_1::implementation::ISensorsWrapperBase;
36 using ::android::hardware::sensors::V2_1::implementation::MAX_RECEIVE_BUFFER_EVENT_COUNT;
37 using ::android::hardware::sensors::V2_1::implementation::NoOpSensorsCallback;
38 using ::android::hardware::sensors::V2_1::implementation::wrapISensors;
39 
40 class SensorsHidlTest;
41 
42 class SensorsHalDeathRecipient : public ::android::hardware::hidl_death_recipient {
43     virtual void serviceDied(
44             uint64_t cookie,
45             const ::android::wp<::android::hidl::base::V1_0::IBase>& service) override;
46 };
47 
48 class SensorsHidlEnvironmentV2_X
49     : public SensorsHidlEnvironmentBase<::android::hardware::sensors::V2_1::Event> {
50   public:
51     virtual void HidlTearDown() override;
52 
53   protected:
54     friend SensorsHidlTest;
SensorsHidlEnvironmentV2_X(const std::string & service_name)55     SensorsHidlEnvironmentV2_X(const std::string& service_name)
56         : SensorsHidlEnvironmentBase(service_name), mEventQueueFlag(nullptr) {}
57 
58     /**
59      * Resets the HAL with new FMQs and a new Event Flag
60      *
61      * @return bool true if successful, false otherwise
62      */
63     bool resetHal() override;
64 
65     /**
66      * Starts the polling thread that reads sensor events from the Event FMQ
67      */
68     void startPollingThread() override;
69 
70     /**
71      * Thread responsible for calling functions to read Event FMQ
72      *
73      * @param env SensorEnvironment to being polling for events on
74      */
75     static void pollingThread(SensorsHidlEnvironmentV2_X* env);
76 
77     /**
78      * Reads and saves sensor events from the Event FMQ
79      */
80     void readEvents();
81 
82     GTEST_DISALLOW_COPY_AND_ASSIGN_(SensorsHidlEnvironmentV2_X);
83 
84     /**
85      * Pointer to the Sensors HAL Interface that allows the test to call HAL functions.
86      */
87     sp<ISensorsWrapperBase> mSensors;
88 
89     /**
90      * Monitors the HAL for crashes, triggering test failure if seen
91      */
92     sp<SensorsHalDeathRecipient> mDeathRecipient = new SensorsHalDeathRecipient();
93 
94     /**
95      * Type used to simplify the creation of the Wake Lock FMQ
96      */
97     typedef MessageQueue<uint32_t, ::android::hardware::kSynchronizedReadWrite> WakeLockQueue;
98 
99     /**
100      * The Wake Lock FMQ is used by the test to notify the Sensors HAL whenever it has processed
101      * WAKE_UP sensor events.
102      */
103     std::unique_ptr<WakeLockQueue> mWakeLockQueue;
104 
105     /**
106      * The Event Queue Flag notifies the test framework when sensor events have been written to the
107      * Event FMQ by the Sensors HAL.
108      */
109     ::android::hardware::EventFlag* mEventQueueFlag;
110 
111     /**
112      * An array that is used to store sensor events read from the Event FMQ
113      */
114     std::array<::android::hardware::sensors::V2_1::Event, MAX_RECEIVE_BUFFER_EVENT_COUNT>
115             mEventBuffer;
116 };
117 
118 #endif  // ANDROID_SENSORS_HIDL_ENVIRONMENT_V2_X_H
119