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_TEST_BASE_H
18 #define ANDROID_SENSORS_HIDL_TEST_BASE_H
19 
20 #include "sensors-vts-utils/SensorEventsChecker.h"
21 #include "sensors-vts-utils/SensorsHidlEnvironmentBase.h"
22 
23 #include <android/hardware/sensors/1.0/ISensors.h>
24 #include <android/hardware/sensors/1.0/types.h>
25 #include <gtest/gtest.h>
26 
27 #include <unordered_set>
28 #include <vector>
29 
30 using ::android::sp;
31 using ::android::hardware::hidl_string;
32 using ::android::hardware::Return;
33 using ::android::hardware::Void;
34 
35 using ::android::sp;
36 using ::android::hardware::hidl_string;
37 using ::android::hardware::sensors::V1_0::Event;
38 using ::android::hardware::sensors::V1_0::ISensors;
39 using ::android::hardware::sensors::V1_0::RateLevel;
40 using ::android::hardware::sensors::V1_0::Result;
41 using ::android::hardware::sensors::V1_0::SensorFlagBits;
42 using ::android::hardware::sensors::V1_0::SensorInfo;
43 using ::android::hardware::sensors::V1_0::SensorType;
44 using ::android::hardware::sensors::V1_0::SharedMemInfo;
45 using ::android::hardware::sensors::V1_0::SharedMemType;
46 
47 class SensorsHidlTestBase : public testing::TestWithParam<std::string> {
48   public:
49     virtual SensorsHidlEnvironmentBase* getEnvironment() = 0;
SetUp()50     virtual void SetUp() override {}
51 
TearDown()52     virtual void TearDown() override {
53         // stop all sensors
54         for (auto s : mSensorHandles) {
55             activate(s, false);
56         }
57         mSensorHandles.clear();
58 
59         // stop all direct report and channels
60         for (auto c : mDirectChannelHandles) {
61             // disable all reports
62             configDirectReport(-1, c, RateLevel::STOP, [](auto, auto) {});
63             unregisterDirectChannel(c);
64         }
65         mDirectChannelHandles.clear();
66     }
67 
68     // implementation wrapper
69     virtual SensorInfo defaultSensorByType(SensorType type) = 0;
70     virtual Return<void> getSensorsList(ISensors::getSensorsList_cb _hidl_cb) = 0;
71 
72     virtual Return<Result> activate(int32_t sensorHandle, bool enabled) = 0;
73 
74     virtual Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
75                                  int64_t maxReportLatencyNs) = 0;
76 
77     virtual Return<Result> flush(int32_t sensorHandle) = 0;
78     virtual Return<Result> injectSensorData(const Event& event) = 0;
79     virtual Return<void> registerDirectChannel(const SharedMemInfo& mem,
80                                                ISensors::registerDirectChannel_cb _hidl_cb) = 0;
81     virtual Return<Result> unregisterDirectChannel(int32_t channelHandle) = 0;
82     virtual Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle,
83                                             RateLevel rate,
84                                             ISensors::configDirectReport_cb _hidl_cb) = 0;
85 
86     std::vector<Event> collectEvents(useconds_t timeLimitUs, size_t nEventLimit,
87                                      bool clearBeforeStart = true, bool changeCollection = true);
88     static std::vector<Event> collectEvents(useconds_t timeLimitUs, size_t nEventLimit,
89                                             SensorsHidlEnvironmentBase* environment,
90                                             bool clearBeforeStart = true,
91                                             bool changeCollection = true);
92 
extractReportMode(uint64_t flag)93     inline static SensorFlagBits extractReportMode(uint64_t flag) {
94         return (SensorFlagBits)(flag & ((uint64_t)SensorFlagBits::CONTINUOUS_MODE |
95                                         (uint64_t)SensorFlagBits::ON_CHANGE_MODE |
96                                         (uint64_t)SensorFlagBits::ONE_SHOT_MODE |
97                                         (uint64_t)SensorFlagBits::SPECIAL_REPORTING_MODE));
98     }
99 
isMetaSensorType(SensorType type)100     inline static bool isMetaSensorType(SensorType type) {
101         return (type == SensorType::META_DATA || type == SensorType::DYNAMIC_SENSOR_META ||
102                 type == SensorType::ADDITIONAL_INFO);
103     }
104 
isValidType(SensorType type)105     inline static bool isValidType(SensorType type) { return (int32_t)type > 0; }
106 
107     void testStreamingOperation(SensorType type, std::chrono::nanoseconds samplingPeriod,
108                                 std::chrono::seconds duration, const SensorEventsChecker& checker);
109     void testSamplingRateHotSwitchOperation(SensorType type, bool fastToSlow = true);
110     void testBatchingOperation(SensorType type);
111     void testDirectReportOperation(SensorType type, SharedMemType memType, RateLevel rate,
112                                    const SensorEventsChecker& checker);
113 
114     static void assertTypeMatchStringType(SensorType type, const hidl_string& stringType);
115     static void assertTypeMatchReportMode(SensorType type, SensorFlagBits reportMode);
116     static void assertDelayMatchReportMode(int32_t minDelay, int32_t maxDelay,
117                                            SensorFlagBits reportMode);
118     static SensorFlagBits expectedReportModeForType(SensorType type);
119     static bool isDirectReportRateSupported(SensorInfo sensor, RateLevel rate);
120     static bool isDirectChannelTypeSupported(SensorInfo sensor, SharedMemType type);
121 
122    protected:
123     // checkers
124     static const Vec3NormChecker sAccelNormChecker;
125     static const Vec3NormChecker sGyroNormChecker;
126 
127     // all sensors and direct channnels used
128     std::unordered_set<int32_t> mSensorHandles;
129     std::unordered_set<int32_t> mDirectChannelHandles;
130 };
131 
132 #endif  // ANDROID_SENSORS_HIDL_TEST_BASE_H
133