• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class SensorsHidlEnvironmentV2_0 : public SensorsHidlEnvironmentBase {
36    public:
37     using Event = ::android::hardware::sensors::V1_0::Event;
38     // get the test environment singleton
Instance()39     static SensorsHidlEnvironmentV2_0* Instance() {
40         static SensorsHidlEnvironmentV2_0* instance = new SensorsHidlEnvironmentV2_0();
41         return instance;
42     }
43 
registerTestServices()44     virtual void registerTestServices() override {
45         registerTestService<android::hardware::sensors::V2_0::ISensors>();
46     }
47 
48     virtual void HidlTearDown() override;
49 
50    protected:
51     friend SensorsHidlTest;
52 
SensorsHidlEnvironmentV2_0()53     SensorsHidlEnvironmentV2_0() : mEventQueueFlag(nullptr) {}
54 
55     /**
56      * Resets the HAL with new FMQs and a new Event Flag
57      *
58      * @return bool true if successful, false otherwise
59      */
60     bool resetHal() override;
61 
62     /**
63      * Starts the polling thread that reads sensor events from the Event FMQ
64      */
65     void startPollingThread() override;
66 
67     /**
68      * Thread responsible for calling functions to read Event FMQ
69      *
70      * @param env SensorEnvironment to being polling for events on
71      */
72     static void pollingThread(SensorsHidlEnvironmentV2_0* env);
73 
74     /**
75      * Reads and saves sensor events from the Event FMQ
76      */
77     void readEvents();
78 
79     GTEST_DISALLOW_COPY_AND_ASSIGN_(SensorsHidlEnvironmentV2_0);
80 
81     /**
82      * Pointer to the Sensors HAL Interface that allows the test to call HAL functions.
83      */
84     sp<android::hardware::sensors::V2_0::ISensors> mSensors;
85 
86     /**
87      * Type used to simplify the creation of the Event FMQ
88      */
89     typedef MessageQueue<Event, ::android::hardware::kSynchronizedReadWrite> EventMessageQueue;
90 
91     /**
92      * Type used to simplify the creation of the Wake Lock FMQ
93      */
94     typedef MessageQueue<uint32_t, ::android::hardware::kSynchronizedReadWrite> WakeLockQueue;
95 
96     /**
97      * The Event FMQ where the test framework is able to read sensor events that the Sensors HAL
98      * has written.
99      */
100     std::unique_ptr<EventMessageQueue> mEventQueue;
101 
102     /**
103      * The Wake Lock FMQ is used by the test to notify the Sensors HAL whenever it has processed
104      * WAKE_UP sensor events.
105      */
106     std::unique_ptr<WakeLockQueue> mWakeLockQueue;
107 
108     /**
109      * The Event Queue Flag notifies the test framework when sensor events have been written to the
110      * Event FMQ by the Sensors HAL.
111      */
112     ::android::hardware::EventFlag* mEventQueueFlag;
113 
114     /**
115      * The maximum number of sensor events that can be read from the Event FMQ at one time.
116      */
117     static constexpr size_t MAX_RECEIVE_BUFFER_EVENT_COUNT = 128;
118 
119     /**
120      * An array that is used to store sensor events read from the Event FMQ
121      */
122     std::array<Event, MAX_RECEIVE_BUFFER_EVENT_COUNT> mEventBuffer;
123 };
124 
125 #endif  // ANDROID_SENSORS_HIDL_ENVIRONMENT_V2_0_H
126