blob: 7abd1e91191eadf42f39528c5e59c1a7da8ba5ae [file] [log] [blame]
Peng Xu19a6b342017-02-16 20:30:26 -08001/*
2 * Copyright (C) 2017 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 SENSOR_TEST_H
18#define SENSOR_TEST_H
19
20#include "nativeTestHelper.h"
21#include <android/sensor.h>
22#include <android/hardware_buffer.h>
23#include <android/sharedmem.h>
24
25#include <unordered_set>
26#include <vector>
27#include <sys/mman.h>
28#include <unistd.h>
29
30namespace android {
31namespace SensorTest {
32
33class TestSensor;
34class TestSensorManager;
35class TestSharedMemory;
36
37class SensorTest {
38public:
39 virtual bool SetUp();
40 virtual void TearDown();
41 virtual ~SensorTest() = default;
42
43 // tests
44 void testInitialized(JNIEnv *env);
Peng Xuf47b7ed2017-02-28 20:36:00 -080045 void testInvalidParameter(JNIEnv *env);
Peng Xua6088df2017-03-05 01:46:54 -080046 void testDirectReport(JNIEnv *env, int32_t sensorType, int32_t channelType, int32_t rateLevel);
47
Peng Xu19a6b342017-02-16 20:30:26 -080048private:
49 std::unique_ptr<TestSensorManager> mManager;
50};
51
52// NDK ASensorManager wrapper
53class TestSensorManager {
54public:
55 static TestSensorManager * getInstanceForPackage(const char *package);
56 virtual ~TestSensorManager();
57
58 TestSensor getDefaultSensor(int type);
59 int createDirectChannel(const TestSharedMemory &mem);
60 void destroyDirectChannel(int channel);
61 int configureDirectReport(TestSensor sensor, int channel, int rateLevel);
62 bool isValid() const { return mManager != nullptr; }
63private:
64 TestSensorManager(const char *package);
65 int createSharedMemoryDirectChannel(int fd, size_t size);
66 int createHardwareBufferDirectChannel(AHardwareBuffer const *buffer, size_t size);
67
68 ASensorManager *mManager; // singleton, does not need delete
69
70 // book keeping
71 std::unordered_set<int> mSensorDirectChannel;
72};
73
74// NDK ASensor warpper
75class TestSensor {
76public:
77 TestSensor(ASensor const *s) : mSensor(s) { }
78
79 int getType() const {
80 if (!isValid()) {
81 return -1;
82 }
83 return ASensor_getType(mSensor);
84 }
85
86 bool isDirectChannelTypeSupported(int channelType) const {
87 if (!isValid()) {
88 return false;
89 }
90 return ASensor_isDirectChannelTypeSupported(mSensor, channelType);
91 }
92
93 int getHighestDirectReportRateLevel() const {
94 if (!isValid()) {
95 return ASENSOR_DIRECT_RATE_STOP;
96 }
97 return ASensor_getHighestDirectReportRateLevel(mSensor);
98 }
99
100 operator ASensor const * () { return mSensor; }
101
102 bool isValid() const { return mSensor != nullptr; }
103private:
104 ASensor const * mSensor;
105};
106
107// Shared memory wrapper class
108class TestSharedMemory {
109public:
110 static TestSharedMemory* create(int type, size_t size);
111 char * getBuffer() const;
112 std::vector<ASensorEvent> parseEvents(int64_t lastCounter = -1, size_t offset = 0) const;
113 virtual ~TestSharedMemory();
114
115 int getSharedMemoryFd() const {
116 return mSharedMemoryFd;
117 }
118
119 AHardwareBuffer const * getHardwareBuffer() const {
120 return mHardwareBuffer;
121 }
122
123 int getType() const {
124 return mType;
125 }
126
127 size_t getSize() const {
128 return mSize;
129 }
130private:
131 TestSharedMemory(int type, size_t size);
132 void release();
133
134 const int mType;
135 size_t mSize;
136 char* mBuffer;
137 int mSharedMemoryFd;
138 AHardwareBuffer *mHardwareBuffer;
139};
140} // namespace SensorTest
141} // namespace android
142
143#endif // SENSOR_TEST_H