1 /* 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 30 namespace android { 31 namespace SensorTest { 32 33 class TestSensor; 34 class TestSensorManager; 35 class TestSharedMemory; 36 37 class SensorTest { 38 public: 39 virtual bool SetUp(); 40 virtual void TearDown(); 41 virtual ~SensorTest() = default; 42 43 // tests 44 void testInitialized(JNIEnv *env); 45 void testInvalidParameter(JNIEnv *env); 46 void testDirectReport(JNIEnv *env, int32_t sensorType, int32_t channelType, int32_t rateLevel); 47 48 private: 49 std::unique_ptr<TestSensorManager> mManager; 50 }; 51 52 // NDK ASensorManager wrapper 53 class TestSensorManager { 54 public: 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); isValid()62 bool isValid() const { return mManager != nullptr; } 63 private: 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 75 class TestSensor { 76 public: TestSensor(ASensor const * s)77 TestSensor(ASensor const *s) : mSensor(s) { } 78 getType()79 int getType() const { 80 if (!isValid()) { 81 return -1; 82 } 83 return ASensor_getType(mSensor); 84 } 85 isDirectChannelTypeSupported(int channelType)86 bool isDirectChannelTypeSupported(int channelType) const { 87 if (!isValid()) { 88 return false; 89 } 90 return ASensor_isDirectChannelTypeSupported(mSensor, channelType); 91 } 92 getHighestDirectReportRateLevel()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 isValid()102 bool isValid() const { return mSensor != nullptr; } 103 private: 104 ASensor const * mSensor; 105 }; 106 107 // Shared memory wrapper class 108 class TestSharedMemory { 109 public: 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 getSharedMemoryFd()115 int getSharedMemoryFd() const { 116 return mSharedMemoryFd; 117 } 118 getHardwareBuffer()119 AHardwareBuffer const * getHardwareBuffer() const { 120 return mHardwareBuffer; 121 } 122 getType()123 int getType() const { 124 return mType; 125 } 126 getSize()127 size_t getSize() const { 128 return mSize; 129 } 130 private: 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 144