compat: Add libsensor -> libsensorndkbridge wrapper

Change-Id: I62c27463af385dc791dde64731a39e9ebadfd587
This commit is contained in:
Sebastiano Barezzi 2023-07-18 12:05:39 +02:00 committed by Jan Altensen (Stricted)
parent ac25b3eb52
commit aea5a5fba5
7 changed files with 375 additions and 0 deletions

View file

@ -368,6 +368,20 @@ cc_library {
vendor_available: true,
}
cc_library_shared {
name: "libsensor_vendor",
srcs: [
"libsensor/Sensor.cpp",
"libsensor/SensorEventQueue.cpp",
"libsensor/SensorManager.cpp",
],
shared_libs: [
"libsensorndkbridge",
"libutils",
],
vendor: true,
}
cc_library_shared {
name: "libtinyxml2_shim",
srcs: ["libtinyxml2/XMLDocument.cpp"],

63
libsensor/Sensor.cpp Normal file
View file

@ -0,0 +1,63 @@
/*
* Copyright (C) 2023 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "Sensor.h"
namespace android {
Sensor::Sensor(ASensorRef sensorRef) : mASensorRef(sensorRef) {
mName = ASensor_getName(mASensorRef);
mVendor = ASensor_getVendor(mASensorRef);
mHandle = ASensor_getHandle(mASensorRef);
mType = ASensor_getType(mASensorRef);
mResolution = ASensor_getResolution(mASensorRef);
mMinDelay = ASensor_getMinDelay(mASensorRef);
mFifoReservedEventCount = ASensor_getFifoReservedEventCount(mASensorRef);
mFifoMaxEventCount = ASensor_getFifoMaxEventCount(mASensorRef);
mStringType = ASensor_getStringType(mASensorRef);
}
const String8& Sensor::getName() const {
return mName;
}
const String8& Sensor::getVendor() const {
return mVendor;
}
int32_t Sensor::getHandle() const {
return mHandle;
}
int32_t Sensor::getType() const {
return mType;
}
float Sensor::getResolution() const {
return mResolution;
}
int32_t Sensor::getMinDelay() const {
return mMinDelay;
}
uint32_t Sensor::getFifoReservedEventCount() const {
return mFifoReservedEventCount;
}
uint32_t Sensor::getFifoMaxEventCount() const {
return mFifoMaxEventCount;
}
const String8& Sensor::getStringType() const {
return mStringType;
}
ASensorRef Sensor::getASensorRef() const {
return mASensorRef;
}
} // namespace android

73
libsensor/Sensor.h Normal file
View file

@ -0,0 +1,73 @@
/*
* Copyright (C) 2023 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <android/sensor.h>
#include <stdint.h>
#include <utils/Errors.h>
#include <utils/String8.h>
namespace android {
class Sensor {
public:
Sensor(ASensorRef sensorRef);
~Sensor() = default;
const String8& getName() const;
const String8& getVendor() const;
int32_t getHandle() const;
int32_t getType() const;
/*
float getMinValue() const;
float getMaxValue() const;
*/
float getResolution() const;
/*
float getPowerUsage() const;
*/
int32_t getMinDelay() const;
/*
nsecs_t getMinDelayNs() const;
int32_t getVersion() const;
*/
uint32_t getFifoReservedEventCount() const;
uint32_t getFifoMaxEventCount() const;
const String8& getStringType() const;
/*
const String8& getRequiredPermission() const;
bool isRequiredPermissionRuntime() const;
int32_t getRequiredAppOp() const;
int32_t getMaxDelay() const;
uint32_t getFlags() const;
bool isWakeUpSensor() const;
bool isDynamicSensor() const;
bool isDataInjectionSupported() const;
bool hasAdditionalInfo() const;
int32_t getHighestDirectReportRateLevel() const;
bool isDirectChannelTypeSupported(int32_t sharedMemType) const;
int32_t getReportingMode() const;
*/
// Custom methods
ASensorRef getASensorRef() const;
private:
ASensorRef mASensorRef;
String8 mName;
String8 mVendor;
int32_t mHandle;
int32_t mType;
float mResolution;
int32_t mMinDelay;
uint32_t mFifoReservedEventCount;
uint32_t mFifoMaxEventCount;
String8 mStringType;
};
} // namespace android

View file

@ -0,0 +1,34 @@
/*
* Copyright (C) 2023 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "SensorEventQueue.h"
namespace android {
SensorEventQueue::SensorEventQueue(ASensorEventQueue* aSensorEventQueue)
: mASensorEventQueue(aSensorEventQueue) {}
ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) {
return ASensorEventQueue_getEvents(mASensorEventQueue, events, numEvents);
}
status_t SensorEventQueue::waitForEvent() const {
return OK;
}
status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
return ASensorEventQueue_enableSensor(mASensorEventQueue, sensor->getASensorRef());
}
status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
return ASensorEventQueue_disableSensor(mASensorEventQueue, sensor->getASensorRef());
}
status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
return ASensorEventQueue_setEventRate(mASensorEventQueue, sensor->getASensorRef(), ns);
}
} // namespace android

View file

@ -0,0 +1,37 @@
/*
* Copyright (C) 2023 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <android/sensor.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
#include <utils/Timers.h>
#include "Sensor.h"
namespace android {
class SensorEventQueue : public RefBase {
public:
SensorEventQueue(ASensorEventQueue* aSensorEventQueue);
~SensorEventQueue() = default;
ssize_t read(ASensorEvent* events, size_t numEvents);
status_t waitForEvent() const;
status_t enableSensor(Sensor const* sensor) const;
/*
status_t enableSensor(Sensor const* sensor, int32_t samplingPeriodUs) const;
*/
status_t disableSensor(Sensor const* sensor) const;
status_t setEventRate(Sensor const* sensor, nsecs_t ns) const;
private:
ASensorEventQueue* mASensorEventQueue;
};
} // namespace android

View file

@ -0,0 +1,89 @@
/*
* Copyright (C) 2023 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "SensorManager.h"
namespace android {
SensorManager* SensorManager::sInstance = nullptr;
Mutex SensorManager::sInstanceLock;
SensorManager* SensorManager::getInstance() {
Mutex::Autolock autoLock(sInstanceLock);
if (sInstance == nullptr) {
sInstance = new SensorManager;
if (sInstance->initCheck() != OK) {
delete sInstance;
sInstance = nullptr;
}
}
return sInstance;
}
SensorManager& SensorManager::getInstanceForPackage(const String16& /*packageName*/) {
return *getInstance();
}
void SensorManager::removeInstanceForPackage(const String16& /*packageName*/) {
// no-op
}
SensorManager::SensorManager() : mInitCheck(NO_INIT) {
mASensorManager = ASensorManager_getInstanceForPackage("");
if (mASensorManager != NULL) {
mInitCheck = OK;
}
}
ssize_t SensorManager::getSensorList(Sensor const* const** list) {
Mutex::Autolock autoLock(mLock);
ASensorList* aSensorList = nullptr;
int count;
ASensorRef aSensor;
count = ASensorManager_getSensorList(mASensorManager, aSensorList);
mSensors = static_cast<Sensor const**>(malloc(count * sizeof(Sensor*)));
assert(mSensors != nullptr);
for (int i = 0; i < count; i++) {
aSensor = *aSensorList[i];
mSensors[i] = new Sensor(aSensor);
}
*list = mSensors;
return count;
}
Sensor const* SensorManager::getDefaultSensor(int type) {
const ASensor* sensor = ASensorManager_getDefaultSensor(mASensorManager, type);
return sensor != nullptr ? new Sensor(ASensorManager_getDefaultSensor(mASensorManager, type))
: nullptr;
}
sp<SensorEventQueue> SensorManager::createEventQueue(String8 /*packageName*/, int /*mode*/,
String16 /*attributionTag*/) {
return sp(new SensorEventQueue(ASensorManager_createEventQueue(
mASensorManager, ALooper_forThread(), 0, nullptr, nullptr)));
}
status_t SensorManager::initCheck() const {
return mInitCheck;
}
sp<SensorEventQueue> SensorManager::createEventQueue(String8 packageName) {
return createEventQueue(packageName, 0);
}
sp<SensorEventQueue> SensorManager::createEventQueue(String8 packageName, int mode) {
return createEventQueue(packageName, mode, String16(""));
}
} // namespace android

65
libsensor/SensorManager.h Normal file
View file

@ -0,0 +1,65 @@
/*
* Copyright (C) 2023 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <android/sensor.h>
#include <stdint.h>
#include <utils/Errors.h>
#include <utils/Mutex.h>
#include <utils/String16.h>
#include <utils/StrongPointer.h>
#include <utils/Vector.h>
#include "Sensor.h"
#include "SensorEventQueue.h"
namespace android {
class SensorManager {
public:
static SensorManager& getInstanceForPackage(const String16& packageName);
static void removeInstanceForPackage(const String16& packageName);
~SensorManager() = default;
ssize_t getSensorList(Sensor const* const** list);
/*
ssize_t getDynamicSensorList(Vector<Sensor>& list);
ssize_t getDynamicSensorList(Sensor const* const** list);
*/
Sensor const* getDefaultSensor(int type);
sp<SensorEventQueue> createEventQueue(String8 packageName /*= String8("")*/, int mode /*= 0*/,
String16 attributionTag /*= String16("")*/);
/*
bool isDataInjectionEnabled();
int createDirectChannel(size_t size, int channelType, const native_handle_t* channelData);
void destroyDirectChannel(int channelNativeHandle);
int configureDirectChannel(int channelNativeHandle, int sensorHandle, int rateLevel);
int setOperationParameter(int handle, int type, const Vector<float>& floats,
const Vector<int32_t>& ints);
*/
// Shims
sp<SensorEventQueue> createEventQueue(String8 packageName);
sp<SensorEventQueue> createEventQueue(String8 packageName, int mode);
// Custom methods
status_t initCheck() const;
private:
static SensorManager* sInstance;
static Mutex sInstanceLock;
static SensorManager* getInstance();
SensorManager();
status_t mInitCheck;
ASensorManager* mASensorManager;
Mutex mLock;
Sensor const** mSensors;
};
} // namespace android