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