From aea5a5fba568c359f74a3818fe700df945c8ee12 Mon Sep 17 00:00:00 2001 From: Sebastiano Barezzi Date: Tue, 18 Jul 2023 12:05:39 +0200 Subject: [PATCH] compat: Add libsensor -> libsensorndkbridge wrapper Change-Id: I62c27463af385dc791dde64731a39e9ebadfd587 --- Android.bp | 14 ++++++ libsensor/Sensor.cpp | 63 ++++++++++++++++++++++++ libsensor/Sensor.h | 73 ++++++++++++++++++++++++++++ libsensor/SensorEventQueue.cpp | 34 +++++++++++++ libsensor/SensorEventQueue.h | 37 ++++++++++++++ libsensor/SensorManager.cpp | 89 ++++++++++++++++++++++++++++++++++ libsensor/SensorManager.h | 65 +++++++++++++++++++++++++ 7 files changed, 375 insertions(+) create mode 100644 libsensor/Sensor.cpp create mode 100644 libsensor/Sensor.h create mode 100644 libsensor/SensorEventQueue.cpp create mode 100644 libsensor/SensorEventQueue.h create mode 100644 libsensor/SensorManager.cpp create mode 100644 libsensor/SensorManager.h diff --git a/Android.bp b/Android.bp index 0c8123e..000e754 100644 --- a/Android.bp +++ b/Android.bp @@ -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"], diff --git a/libsensor/Sensor.cpp b/libsensor/Sensor.cpp new file mode 100644 index 0000000..539ee25 --- /dev/null +++ b/libsensor/Sensor.cpp @@ -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 diff --git a/libsensor/Sensor.h b/libsensor/Sensor.h new file mode 100644 index 0000000..1e78fe3 --- /dev/null +++ b/libsensor/Sensor.h @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2023 The LineageOS Project + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include +#include + +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 diff --git a/libsensor/SensorEventQueue.cpp b/libsensor/SensorEventQueue.cpp new file mode 100644 index 0000000..47ae1e9 --- /dev/null +++ b/libsensor/SensorEventQueue.cpp @@ -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 diff --git a/libsensor/SensorEventQueue.h b/libsensor/SensorEventQueue.h new file mode 100644 index 0000000..e8d6e82 --- /dev/null +++ b/libsensor/SensorEventQueue.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2023 The LineageOS Project + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include +#include +#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 diff --git a/libsensor/SensorManager.cpp b/libsensor/SensorManager.cpp new file mode 100644 index 0000000..cfe46fc --- /dev/null +++ b/libsensor/SensorManager.cpp @@ -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(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 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 SensorManager::createEventQueue(String8 packageName) { + return createEventQueue(packageName, 0); +} + +sp SensorManager::createEventQueue(String8 packageName, int mode) { + return createEventQueue(packageName, mode, String16("")); +} + +} // namespace android diff --git a/libsensor/SensorManager.h b/libsensor/SensorManager.h new file mode 100644 index 0000000..d694d19 --- /dev/null +++ b/libsensor/SensorManager.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2023 The LineageOS Project + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#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& list); + ssize_t getDynamicSensorList(Sensor const* const** list); + */ + Sensor const* getDefaultSensor(int type); + sp 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& floats, + const Vector& ints); + */ + + // Shims + sp createEventQueue(String8 packageName); + sp 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