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