blob: 2488db88c81c8623f25b747e7862f81d253a2f4b [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 Xu19a6b342017-02-16 20:30:26 -080046 void testGyroscopeSharedMemoryDirectReport(JNIEnv *env);
47private:
48 std::unique_ptr<TestSensorManager> mManager;
49};
50
51// NDK ASensorManager wrapper
52class TestSensorManager {
53public:
54 static TestSensorManager * getInstanceForPackage(const char *package);
55 virtual ~TestSensorManager();
56
57 TestSensor getDefaultSensor(int type);
58 int createDirectChannel(const TestSharedMemory &mem);
59 void destroyDirectChannel(int channel);
60 int configureDirectReport(TestSensor sensor, int channel, int rateLevel);
61 bool isValid() const { return mManager != nullptr; }
62private:
63 TestSensorManager(const char *package);
64 int createSharedMemoryDirectChannel(int fd, size_t size);
65 int createHardwareBufferDirectChannel(AHardwareBuffer const *buffer, size_t size);
66
67 ASensorManager *mManager; // singleton, does not need delete
68
69 // book keeping
70 std::unordered_set<int> mSensorDirectChannel;
71};
72
73// NDK ASensor warpper
74class TestSensor {
75public:
76 TestSensor(ASensor const *s) : mSensor(s) { }
77
78 int getType() const {
79 if (!isValid()) {
80 return -1;
81 }
82 return ASensor_getType(mSensor);
83 }
84
85 bool isDirectChannelTypeSupported(int channelType) const {
86 if (!isValid()) {
87 return false;
88 }
89 return ASensor_isDirectChannelTypeSupported(mSensor, channelType);
90 }
91
92 int getHighestDirectReportRateLevel() const {
93 if (!isValid()) {
94 return ASENSOR_DIRECT_RATE_STOP;
95 }
96 return ASensor_getHighestDirectReportRateLevel(mSensor);
97 }
98
99 operator ASensor const * () { return mSensor; }
100
101 bool isValid() const { return mSensor != nullptr; }
102private:
103 ASensor const * mSensor;
104};
105
106// Shared memory wrapper class
107class TestSharedMemory {
108public:
109 static TestSharedMemory* create(int type, size_t size);
110 char * getBuffer() const;
111 std::vector<ASensorEvent> parseEvents(int64_t lastCounter = -1, size_t offset = 0) const;
112 virtual ~TestSharedMemory();
113
114 int getSharedMemoryFd() const {
115 return mSharedMemoryFd;
116 }
117
118 AHardwareBuffer const * getHardwareBuffer() const {
119 return mHardwareBuffer;
120 }
121
122 int getType() const {
123 return mType;
124 }
125
126 size_t getSize() const {
127 return mSize;
128 }
129private:
130 TestSharedMemory(int type, size_t size);
131 void release();
132
133 const int mType;
134 size_t mSize;
135 char* mBuffer;
136 int mSharedMemoryFd;
137 AHardwareBuffer *mHardwareBuffer;
138};
139} // namespace SensorTest
140} // namespace android
141
142#endif // SENSOR_TEST_H