Loading sensors/2.0/multihal/Android.bp +7 −1 Original line number Diff line number Diff line Loading @@ -25,6 +25,9 @@ cc_binary { ], init_rc: ["android.hardware.sensors@2.0-service-multihal.rc"], vintf_fragments: ["android.hardware.sensors@2.0-multihal.xml"], header_libs: [ "android.hardware.sensors@2.X-shared-utils", ], shared_libs: [ "android.hardware.sensors@2.0", "android.hardware.sensors@2.0-ScopedWakelock", Loading @@ -37,5 +40,8 @@ cc_binary { "libpower", "libutils", ], static_libs: ["android.hardware.sensors@2.X-multihal"], static_libs: [ "android.hardware.sensors@1.0-convert", "android.hardware.sensors@2.X-multihal", ], } sensors/2.0/multihal/service.cpp +2 −2 Original line number Diff line number Diff line Loading @@ -23,12 +23,12 @@ using android::hardware::configureRpcThreadpool; using android::hardware::joinRpcThreadpool; using android::hardware::sensors::V2_0::ISensors; using android::hardware::sensors::V2_0::implementation::HalProxy; using android::hardware::sensors::V2_1::implementation::HalProxyV2_0; int main(int /* argc */, char** /* argv */) { configureRpcThreadpool(1, true); android::sp<ISensors> halProxy = new HalProxy(); android::sp<ISensors> halProxy = new HalProxyV2_0(); if (halProxy->registerAsService() != ::android::OK) { ALOGE("Failed to register Sensors HAL instance"); return -1; Loading sensors/common/default/2.X/multihal/Android.bp +5 −0 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ cc_defaults { name: "android.hardware.sensors@2.X-multihal-defaults", header_libs: [ "android.hardware.sensors@2.X-multihal.header", "android.hardware.sensors@2.X-shared-utils", ], shared_libs: [ "android.hardware.sensors@1.0", Loading @@ -30,6 +31,9 @@ cc_defaults { "libpower", "libutils", ], static_libs: [ "android.hardware.sensors@1.0-convert", ], cflags: ["-DLOG_TAG=\"SensorsMultiHal\""], } Loading Loading @@ -62,6 +66,7 @@ cc_library_static { ], srcs: [ "HalProxy.cpp", "HalProxyCallback.cpp", ], vendor_available: true, export_header_lib_headers: [ Loading sensors/common/default/2.X/multihal/HalProxy.cpp +120 −85 Original line number Diff line number Diff line Loading @@ -32,15 +32,17 @@ namespace android { namespace hardware { namespace sensors { namespace V2_0 { namespace V2_1 { namespace implementation { using ::android::hardware::sensors::V1_0::Result; using ::android::hardware::sensors::V2_0::EventQueueFlagBits; using ::android::hardware::sensors::V2_0::WakeLockQueueFlagBits; using ::android::hardware::sensors::V2_0::implementation::getTimeNow; using ::android::hardware::sensors::V2_0::implementation::kWakelockTimeoutNs; typedef ISensorsSubHal*(SensorsHalGetSubHalFunc)(uint32_t*); typedef V2_0::implementation::ISensorsSubHal*(SensorsHalGetSubHalFunc)(uint32_t*); typedef V2_1::implementation::ISensorsSubHal*(SensorsHalGetSubHalV2_1Func)(uint32_t*); static constexpr int32_t kBitsAfterSubHalIndex = 24; Loading Loading @@ -85,7 +87,24 @@ HalProxy::HalProxy() { init(); } HalProxy::HalProxy(std::vector<ISensorsSubHal*>& subHalList) : mSubHalList(subHalList) { HalProxy::HalProxy(std::vector<ISensorsSubHalV2_0*>& subHalList) { for (ISensorsSubHalV2_0* subHal : subHalList) { mSubHalList.push_back(std::make_unique<SubHalWrapperV2_0>(subHal)); } init(); } HalProxy::HalProxy(std::vector<ISensorsSubHalV2_0*>& subHalList, std::vector<ISensorsSubHalV2_1*>& subHalListV2_1) { for (ISensorsSubHalV2_0* subHal : subHalList) { mSubHalList.push_back(std::make_unique<SubHalWrapperV2_0>(subHal)); } for (ISensorsSubHalV2_1* subHal : subHalListV2_1) { mSubHalList.push_back(std::make_unique<SubHalWrapperV2_1>(subHal)); } init(); } Loading @@ -93,8 +112,8 @@ HalProxy::~HalProxy() { stopThreads(); } Return<void> HalProxy::getSensorsList(getSensorsList_cb _hidl_cb) { std::vector<SensorInfo> sensors; Return<void> HalProxy::getSensorsList_2_1(ISensorsV2_1::getSensorsList_2_1_cb _hidl_cb) { std::vector<V2_1::SensorInfo> sensors; for (const auto& iter : mSensors) { sensors.push_back(iter.second); } Loading @@ -102,22 +121,31 @@ Return<void> HalProxy::getSensorsList(getSensorsList_cb _hidl_cb) { return Void(); } Return<void> HalProxy::getSensorsList(ISensorsV2_0::getSensorsList_cb _hidl_cb) { std::vector<V1_0::SensorInfo> sensors; for (const auto& iter : mSensors) { sensors.push_back(convertToOldSensorInfo(iter.second)); } _hidl_cb(sensors); return Void(); } Return<Result> HalProxy::setOperationMode(OperationMode mode) { Result result = Result::OK; size_t subHalIndex; for (subHalIndex = 0; subHalIndex < mSubHalList.size(); subHalIndex++) { ISensorsSubHal* subHal = mSubHalList[subHalIndex]; result = subHal->setOperationMode(mode); result = mSubHalList[subHalIndex]->setOperationMode(mode); if (result != Result::OK) { ALOGE("setOperationMode failed for SubHal: %s", subHal->getName().c_str()); ALOGE("setOperationMode failed for SubHal: %s", mSubHalList[subHalIndex]->getName().c_str()); break; } } if (result != Result::OK) { // Reset the subhal operation modes that have been flipped for (size_t i = 0; i < subHalIndex; i++) { ISensorsSubHal* subHal = mSubHalList[i]; subHal->setOperationMode(mCurrentOperationMode); mSubHalList[i]->setOperationMode(mCurrentOperationMode); } } else { mCurrentOperationMode = mode; Loading @@ -133,10 +161,42 @@ Return<Result> HalProxy::activate(int32_t sensorHandle, bool enabled) { ->activate(clearSubHalIndex(sensorHandle), enabled); } Return<Result> HalProxy::initialize_2_1( const ::android::hardware::MQDescriptorSync<V2_1::Event>& eventQueueDescriptor, const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor, const sp<V2_1::ISensorsCallback>& sensorsCallback) { sp<ISensorsCallbackWrapperBase> dynamicCallback = new ISensorsCallbackWrapperV2_1(sensorsCallback); // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions. auto eventQueue = std::make_unique<EventMessageQueueV2_1>(eventQueueDescriptor, true /* resetPointers */); std::unique_ptr<EventMessageQueueWrapperBase> queue = std::make_unique<EventMessageQueueWrapperV2_1>(eventQueue); return initializeCommon(queue, wakeLockDescriptor, dynamicCallback); } Return<Result> HalProxy::initialize( const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor, const ::android::hardware::MQDescriptorSync<V1_0::Event>& eventQueueDescriptor, const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor, const sp<V2_0::ISensorsCallback>& sensorsCallback) { sp<ISensorsCallbackWrapperBase> dynamicCallback = new ISensorsCallbackWrapperV2_0(sensorsCallback); // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions. auto eventQueue = std::make_unique<EventMessageQueueV2_0>(eventQueueDescriptor, true /* resetPointers */); std::unique_ptr<EventMessageQueueWrapperBase> queue = std::make_unique<EventMessageQueueWrapperV1_0>(eventQueue); return initializeCommon(queue, wakeLockDescriptor, dynamicCallback); } Return<Result> HalProxy::initializeCommon( std::unique_ptr<EventMessageQueueWrapperBase>& eventQueue, const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor, const sp<ISensorsCallback>& sensorsCallback) { const sp<ISensorsCallbackWrapperBase>& sensorsCallback) { Result result = Result::OK; stopThreads(); Loading @@ -147,7 +207,7 @@ Return<Result> HalProxy::initialize( disableAllSensors(); // Clears the queue if any events were pending write before. mPendingWriteEventsQueue = std::queue<std::pair<std::vector<Event>, size_t>>(); mPendingWriteEventsQueue = std::queue<std::pair<std::vector<V2_1::Event>, size_t>>(); mSizePendingWriteEventsQueue = 0; // Clears previously connected dynamic sensors Loading @@ -156,8 +216,7 @@ Return<Result> HalProxy::initialize( mDynamicSensorsCallback = sensorsCallback; // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions. mEventQueue = std::make_unique<EventMessageQueue>(eventQueueDescriptor, true /* resetPointers */); mEventQueue = std::move(eventQueue); // Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP // events have been successfully read and handled by the framework. Loading Loading @@ -186,12 +245,10 @@ Return<Result> HalProxy::initialize( mWakelockThread = std::thread(startWakelockThread, this); for (size_t i = 0; i < mSubHalList.size(); i++) { auto subHal = mSubHalList[i]; const auto& subHalCallback = mSubHalCallbacks[i]; Result currRes = subHal->initialize(subHalCallback); Result currRes = mSubHalList[i]->initialize(this, this, i); if (currRes != Result::OK) { result = currRes; ALOGE("Subhal '%s' failed to initialize.", subHal->getName().c_str()); ALOGE("Subhal '%s' failed to initialize.", mSubHalList[i]->getName().c_str()); break; } } Loading @@ -217,7 +274,11 @@ Return<Result> HalProxy::flush(int32_t sensorHandle) { return getSubHalForSensorHandle(sensorHandle)->flush(clearSubHalIndex(sensorHandle)); } Return<Result> HalProxy::injectSensorData(const Event& event) { Return<Result> HalProxy::injectSensorData_2_1(const V2_1::Event& event) { return injectSensorData(convertToOldEvent(event)); } Return<Result> HalProxy::injectSensorData(const V1_0::Event& event) { Result result = Result::OK; if (mCurrentOperationMode == OperationMode::NORMAL && event.sensorType != V1_0::SensorType::ADDITIONAL_INFO) { Loading @@ -226,18 +287,19 @@ Return<Result> HalProxy::injectSensorData(const Event& event) { result = Result::BAD_VALUE; } if (result == Result::OK) { Event subHalEvent = event; V1_0::Event subHalEvent = event; if (!isSubHalIndexValid(event.sensorHandle)) { return Result::BAD_VALUE; } subHalEvent.sensorHandle = clearSubHalIndex(event.sensorHandle); result = getSubHalForSensorHandle(event.sensorHandle)->injectSensorData(subHalEvent); result = getSubHalForSensorHandle(event.sensorHandle) ->injectSensorData(convertToNewEvent(subHalEvent)); } return result; } Return<void> HalProxy::registerDirectChannel(const SharedMemInfo& mem, registerDirectChannel_cb _hidl_cb) { ISensorsV2_0::registerDirectChannel_cb _hidl_cb) { if (mDirectChannelSubHal == nullptr) { _hidl_cb(Result::INVALID_OPERATION, -1 /* channelHandle */); } else { Loading @@ -257,7 +319,8 @@ Return<Result> HalProxy::unregisterDirectChannel(int32_t channelHandle) { } Return<void> HalProxy::configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate, configDirectReport_cb _hidl_cb) { RateLevel rate, ISensorsV2_0::configDirectReport_cb _hidl_cb) { if (mDirectChannelSubHal == nullptr) { _hidl_cb(Result::INVALID_OPERATION, -1 /* reportToken */); } else if (sensorHandle == -1 && rate != RateLevel::STOP) { Loading Loading @@ -302,7 +365,7 @@ Return<void> HalProxy::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& stream << " # of non-dynamic sensors across all subhals: " << mSensors.size() << std::endl; stream << " # of dynamic sensors across all subhals: " << mDynamicSensors.size() << std::endl; stream << "SubHals (" << mSubHalList.size() << "):" << std::endl; for (ISensorsSubHal* subHal : mSubHalList) { for (auto& subHal : mSubHalList) { stream << " Name: " << subHal->getName() << std::endl; stream << " Debug dump: " << std::endl; android::base::WriteStringToFd(stream.str(), writeFd); Loading Loading @@ -369,57 +432,66 @@ void HalProxy::initializeSubHalListFromConfigFile(const char* configFileName) { } else { SensorsHalGetSubHalFunc* sensorsHalGetSubHalPtr = (SensorsHalGetSubHalFunc*)dlsym(handle, "sensorsHalGetSubHal"); if (sensorsHalGetSubHalPtr == nullptr) { ALOGE("Failed to locate sensorsHalGetSubHal function for library: %s", subHalLibraryFile.c_str()); } else { if (sensorsHalGetSubHalPtr != nullptr) { std::function<SensorsHalGetSubHalFunc> sensorsHalGetSubHal = *sensorsHalGetSubHalPtr; uint32_t version; ISensorsSubHal* subHal = sensorsHalGetSubHal(&version); ISensorsSubHalV2_0* subHal = sensorsHalGetSubHal(&version); if (version != SUB_HAL_2_0_VERSION) { ALOGE("SubHal version was not 2.0 for library: %s", subHalLibraryFile.c_str()); } else { ALOGV("Loaded SubHal from library: %s", subHalLibraryFile.c_str()); mSubHalList.push_back(subHal); mSubHalList.push_back(std::make_unique<SubHalWrapperV2_0>(subHal)); } } else { SensorsHalGetSubHalV2_1Func* getSubHalV2_1Ptr = (SensorsHalGetSubHalV2_1Func*)dlsym(handle, "sensorsHalGetSubHal_2_1"); if (getSubHalV2_1Ptr == nullptr) { ALOGE("Failed to locate sensorsHalGetSubHal function for library: %s", subHalLibraryFile.c_str()); } else { std::function<SensorsHalGetSubHalV2_1Func> sensorsHalGetSubHal_2_1 = *getSubHalV2_1Ptr; uint32_t version; ISensorsSubHalV2_1* subHal = sensorsHalGetSubHal_2_1(&version); if (version != SUB_HAL_2_1_VERSION) { ALOGE("SubHal version was not 2.1 for library: %s", subHalLibraryFile.c_str()); } else { ALOGV("Loaded SubHal from library: %s", subHalLibraryFile.c_str()); mSubHalList.push_back(std::make_unique<SubHalWrapperV2_1>(subHal)); } } } } } void HalProxy::initializeSubHalCallbacks() { for (size_t subHalIndex = 0; subHalIndex < mSubHalList.size(); subHalIndex++) { sp<IHalProxyCallback> callback = new HalProxyCallback(this, subHalIndex); mSubHalCallbacks.push_back(callback); } } void HalProxy::initializeSensorList() { for (size_t subHalIndex = 0; subHalIndex < mSubHalList.size(); subHalIndex++) { ISensorsSubHal* subHal = mSubHalList[subHalIndex]; auto result = subHal->getSensorsList([&](const auto& list) { auto result = mSubHalList[subHalIndex]->getSensorsList([&](const auto& list) { for (SensorInfo sensor : list) { if (!subHalIndexIsClear(sensor.sensorHandle)) { ALOGE("SubHal sensorHandle's first byte was not 0"); } else { ALOGV("Loaded sensor: %s", sensor.name.c_str()); sensor.sensorHandle = setSubHalIndex(sensor.sensorHandle, subHalIndex); setDirectChannelFlags(&sensor, subHal); setDirectChannelFlags(&sensor, mSubHalList[subHalIndex]); mSensors[sensor.sensorHandle] = sensor; } } }); if (!result.isOk()) { ALOGE("getSensorsList call failed for SubHal: %s", subHal->getName().c_str()); ALOGE("getSensorsList call failed for SubHal: %s", mSubHalList[subHalIndex]->getName().c_str()); } } } void HalProxy::init() { initializeSubHalCallbacks(); initializeSensorList(); } Loading Loading @@ -552,7 +624,7 @@ void HalProxy::resetSharedWakelock() { } void HalProxy::postEventsToMessageQueue(const std::vector<Event>& events, size_t numWakeupEvents, ScopedWakelock wakelock) { V2_0::implementation::ScopedWakelock wakelock) { size_t numToWrite = 0; std::lock_guard<std::mutex> lock(mEventQueueWriteMutex); if (wakelock.isLocked()) { Loading Loading @@ -610,7 +682,8 @@ void HalProxy::decrementRefCountAndMaybeReleaseWakelock(size_t delta, } } void HalProxy::setDirectChannelFlags(SensorInfo* sensorInfo, ISensorsSubHal* subHal) { void HalProxy::setDirectChannelFlags(SensorInfo* sensorInfo, std::shared_ptr<ISubHalWrapperBase> subHal) { bool sensorSupportsDirectChannel = (sensorInfo->flags & (V1_0::SensorFlagBits::MASK_DIRECT_REPORT | V1_0::SensorFlagBits::MASK_DIRECT_CHANNEL)) != 0; Loading @@ -624,7 +697,7 @@ void HalProxy::setDirectChannelFlags(SensorInfo* sensorInfo, ISensorsSubHal* sub } } ISensorsSubHal* HalProxy::getSubHalForSensorHandle(int32_t sensorHandle) { std::shared_ptr<ISubHalWrapperBase> HalProxy::getSubHalForSensorHandle(int32_t sensorHandle) { return mSubHalList[extractSubHalIndex(sensorHandle)]; } Loading @@ -651,46 +724,8 @@ bool HalProxy::subHalIndexIsClear(int32_t sensorHandle) { return (sensorHandle & kSensorHandleSubHalIndexMask) == 0; } void HalProxyCallback::postEvents(const std::vector<Event>& events, ScopedWakelock wakelock) { if (events.empty() || !mHalProxy->areThreadsRunning()) return; size_t numWakeupEvents; std::vector<Event> processedEvents = processEvents(events, &numWakeupEvents); if (numWakeupEvents > 0) { ALOG_ASSERT(wakelock.isLocked(), "Wakeup events posted while wakelock unlocked for subhal" " w/ index %" PRId32 ".", mSubHalIndex); } else { ALOG_ASSERT(!wakelock.isLocked(), "No Wakeup events posted but wakelock locked for subhal" " w/ index %" PRId32 ".", mSubHalIndex); } mHalProxy->postEventsToMessageQueue(processedEvents, numWakeupEvents, std::move(wakelock)); } ScopedWakelock HalProxyCallback::createScopedWakelock(bool lock) { ScopedWakelock wakelock(mHalProxy, lock); return wakelock; } std::vector<Event> HalProxyCallback::processEvents(const std::vector<Event>& events, size_t* numWakeupEvents) const { *numWakeupEvents = 0; std::vector<Event> eventsOut; for (Event event : events) { event.sensorHandle = setSubHalIndex(event.sensorHandle, mSubHalIndex); eventsOut.push_back(event); const SensorInfo& sensor = mHalProxy->getSensorInfo(event.sensorHandle); if ((sensor.flags & V1_0::SensorFlagBits::WAKE_UP) != 0) { (*numWakeupEvents)++; } } return eventsOut; } } // namespace implementation } // namespace V2_0 } // namespace V2_1 } // namespace sensors } // namespace hardware } // namespace android sensors/common/default/2.X/multihal/HalProxyCallback.cpp 0 → 100644 +84 −0 Original line number Diff line number Diff line /* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "HalProxyCallback.h" #include <cinttypes> namespace android { namespace hardware { namespace sensors { namespace V2_0 { namespace implementation { static constexpr int32_t kBitsAfterSubHalIndex = 24; /** * Set the subhal index as first byte of sensor handle and return this modified version. * * @param sensorHandle The sensor handle to modify. * @param subHalIndex The index in the hal proxy of the sub hal this sensor belongs to. * * @return The modified sensor handle. */ int32_t setSubHalIndex(int32_t sensorHandle, size_t subHalIndex) { return sensorHandle | (static_cast<int32_t>(subHalIndex) << kBitsAfterSubHalIndex); } void HalProxyCallbackBase::postEvents(const std::vector<V2_1::Event>& events, ScopedWakelock wakelock) { if (events.empty() || !mCallback->areThreadsRunning()) return; size_t numWakeupEvents; std::vector<V2_1::Event> processedEvents = processEvents(events, &numWakeupEvents); if (numWakeupEvents > 0) { ALOG_ASSERT(wakelock.isLocked(), "Wakeup events posted while wakelock unlocked for subhal" " w/ index %" PRId32 ".", mSubHalIndex); } else { ALOG_ASSERT(!wakelock.isLocked(), "No Wakeup events posted but wakelock locked for subhal" " w/ index %" PRId32 ".", mSubHalIndex); } mCallback->postEventsToMessageQueue(processedEvents, numWakeupEvents, std::move(wakelock)); } ScopedWakelock HalProxyCallbackBase::createScopedWakelock(bool lock) { ScopedWakelock wakelock(mRefCounter, lock); return wakelock; } std::vector<V2_1::Event> HalProxyCallbackBase::processEvents(const std::vector<V2_1::Event>& events, size_t* numWakeupEvents) const { *numWakeupEvents = 0; std::vector<V2_1::Event> eventsOut; for (V2_1::Event event : events) { event.sensorHandle = setSubHalIndex(event.sensorHandle, mSubHalIndex); eventsOut.push_back(event); const V2_1::SensorInfo& sensor = mCallback->getSensorInfo(event.sensorHandle); if ((sensor.flags & V1_0::SensorFlagBits::WAKE_UP) != 0) { (*numWakeupEvents)++; } } return eventsOut; } } // namespace implementation } // namespace V2_0 } // namespace sensors } // namespace hardware } // namespace android Loading
sensors/2.0/multihal/Android.bp +7 −1 Original line number Diff line number Diff line Loading @@ -25,6 +25,9 @@ cc_binary { ], init_rc: ["android.hardware.sensors@2.0-service-multihal.rc"], vintf_fragments: ["android.hardware.sensors@2.0-multihal.xml"], header_libs: [ "android.hardware.sensors@2.X-shared-utils", ], shared_libs: [ "android.hardware.sensors@2.0", "android.hardware.sensors@2.0-ScopedWakelock", Loading @@ -37,5 +40,8 @@ cc_binary { "libpower", "libutils", ], static_libs: ["android.hardware.sensors@2.X-multihal"], static_libs: [ "android.hardware.sensors@1.0-convert", "android.hardware.sensors@2.X-multihal", ], }
sensors/2.0/multihal/service.cpp +2 −2 Original line number Diff line number Diff line Loading @@ -23,12 +23,12 @@ using android::hardware::configureRpcThreadpool; using android::hardware::joinRpcThreadpool; using android::hardware::sensors::V2_0::ISensors; using android::hardware::sensors::V2_0::implementation::HalProxy; using android::hardware::sensors::V2_1::implementation::HalProxyV2_0; int main(int /* argc */, char** /* argv */) { configureRpcThreadpool(1, true); android::sp<ISensors> halProxy = new HalProxy(); android::sp<ISensors> halProxy = new HalProxyV2_0(); if (halProxy->registerAsService() != ::android::OK) { ALOGE("Failed to register Sensors HAL instance"); return -1; Loading
sensors/common/default/2.X/multihal/Android.bp +5 −0 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ cc_defaults { name: "android.hardware.sensors@2.X-multihal-defaults", header_libs: [ "android.hardware.sensors@2.X-multihal.header", "android.hardware.sensors@2.X-shared-utils", ], shared_libs: [ "android.hardware.sensors@1.0", Loading @@ -30,6 +31,9 @@ cc_defaults { "libpower", "libutils", ], static_libs: [ "android.hardware.sensors@1.0-convert", ], cflags: ["-DLOG_TAG=\"SensorsMultiHal\""], } Loading Loading @@ -62,6 +66,7 @@ cc_library_static { ], srcs: [ "HalProxy.cpp", "HalProxyCallback.cpp", ], vendor_available: true, export_header_lib_headers: [ Loading
sensors/common/default/2.X/multihal/HalProxy.cpp +120 −85 Original line number Diff line number Diff line Loading @@ -32,15 +32,17 @@ namespace android { namespace hardware { namespace sensors { namespace V2_0 { namespace V2_1 { namespace implementation { using ::android::hardware::sensors::V1_0::Result; using ::android::hardware::sensors::V2_0::EventQueueFlagBits; using ::android::hardware::sensors::V2_0::WakeLockQueueFlagBits; using ::android::hardware::sensors::V2_0::implementation::getTimeNow; using ::android::hardware::sensors::V2_0::implementation::kWakelockTimeoutNs; typedef ISensorsSubHal*(SensorsHalGetSubHalFunc)(uint32_t*); typedef V2_0::implementation::ISensorsSubHal*(SensorsHalGetSubHalFunc)(uint32_t*); typedef V2_1::implementation::ISensorsSubHal*(SensorsHalGetSubHalV2_1Func)(uint32_t*); static constexpr int32_t kBitsAfterSubHalIndex = 24; Loading Loading @@ -85,7 +87,24 @@ HalProxy::HalProxy() { init(); } HalProxy::HalProxy(std::vector<ISensorsSubHal*>& subHalList) : mSubHalList(subHalList) { HalProxy::HalProxy(std::vector<ISensorsSubHalV2_0*>& subHalList) { for (ISensorsSubHalV2_0* subHal : subHalList) { mSubHalList.push_back(std::make_unique<SubHalWrapperV2_0>(subHal)); } init(); } HalProxy::HalProxy(std::vector<ISensorsSubHalV2_0*>& subHalList, std::vector<ISensorsSubHalV2_1*>& subHalListV2_1) { for (ISensorsSubHalV2_0* subHal : subHalList) { mSubHalList.push_back(std::make_unique<SubHalWrapperV2_0>(subHal)); } for (ISensorsSubHalV2_1* subHal : subHalListV2_1) { mSubHalList.push_back(std::make_unique<SubHalWrapperV2_1>(subHal)); } init(); } Loading @@ -93,8 +112,8 @@ HalProxy::~HalProxy() { stopThreads(); } Return<void> HalProxy::getSensorsList(getSensorsList_cb _hidl_cb) { std::vector<SensorInfo> sensors; Return<void> HalProxy::getSensorsList_2_1(ISensorsV2_1::getSensorsList_2_1_cb _hidl_cb) { std::vector<V2_1::SensorInfo> sensors; for (const auto& iter : mSensors) { sensors.push_back(iter.second); } Loading @@ -102,22 +121,31 @@ Return<void> HalProxy::getSensorsList(getSensorsList_cb _hidl_cb) { return Void(); } Return<void> HalProxy::getSensorsList(ISensorsV2_0::getSensorsList_cb _hidl_cb) { std::vector<V1_0::SensorInfo> sensors; for (const auto& iter : mSensors) { sensors.push_back(convertToOldSensorInfo(iter.second)); } _hidl_cb(sensors); return Void(); } Return<Result> HalProxy::setOperationMode(OperationMode mode) { Result result = Result::OK; size_t subHalIndex; for (subHalIndex = 0; subHalIndex < mSubHalList.size(); subHalIndex++) { ISensorsSubHal* subHal = mSubHalList[subHalIndex]; result = subHal->setOperationMode(mode); result = mSubHalList[subHalIndex]->setOperationMode(mode); if (result != Result::OK) { ALOGE("setOperationMode failed for SubHal: %s", subHal->getName().c_str()); ALOGE("setOperationMode failed for SubHal: %s", mSubHalList[subHalIndex]->getName().c_str()); break; } } if (result != Result::OK) { // Reset the subhal operation modes that have been flipped for (size_t i = 0; i < subHalIndex; i++) { ISensorsSubHal* subHal = mSubHalList[i]; subHal->setOperationMode(mCurrentOperationMode); mSubHalList[i]->setOperationMode(mCurrentOperationMode); } } else { mCurrentOperationMode = mode; Loading @@ -133,10 +161,42 @@ Return<Result> HalProxy::activate(int32_t sensorHandle, bool enabled) { ->activate(clearSubHalIndex(sensorHandle), enabled); } Return<Result> HalProxy::initialize_2_1( const ::android::hardware::MQDescriptorSync<V2_1::Event>& eventQueueDescriptor, const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor, const sp<V2_1::ISensorsCallback>& sensorsCallback) { sp<ISensorsCallbackWrapperBase> dynamicCallback = new ISensorsCallbackWrapperV2_1(sensorsCallback); // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions. auto eventQueue = std::make_unique<EventMessageQueueV2_1>(eventQueueDescriptor, true /* resetPointers */); std::unique_ptr<EventMessageQueueWrapperBase> queue = std::make_unique<EventMessageQueueWrapperV2_1>(eventQueue); return initializeCommon(queue, wakeLockDescriptor, dynamicCallback); } Return<Result> HalProxy::initialize( const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor, const ::android::hardware::MQDescriptorSync<V1_0::Event>& eventQueueDescriptor, const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor, const sp<V2_0::ISensorsCallback>& sensorsCallback) { sp<ISensorsCallbackWrapperBase> dynamicCallback = new ISensorsCallbackWrapperV2_0(sensorsCallback); // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions. auto eventQueue = std::make_unique<EventMessageQueueV2_0>(eventQueueDescriptor, true /* resetPointers */); std::unique_ptr<EventMessageQueueWrapperBase> queue = std::make_unique<EventMessageQueueWrapperV1_0>(eventQueue); return initializeCommon(queue, wakeLockDescriptor, dynamicCallback); } Return<Result> HalProxy::initializeCommon( std::unique_ptr<EventMessageQueueWrapperBase>& eventQueue, const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor, const sp<ISensorsCallback>& sensorsCallback) { const sp<ISensorsCallbackWrapperBase>& sensorsCallback) { Result result = Result::OK; stopThreads(); Loading @@ -147,7 +207,7 @@ Return<Result> HalProxy::initialize( disableAllSensors(); // Clears the queue if any events were pending write before. mPendingWriteEventsQueue = std::queue<std::pair<std::vector<Event>, size_t>>(); mPendingWriteEventsQueue = std::queue<std::pair<std::vector<V2_1::Event>, size_t>>(); mSizePendingWriteEventsQueue = 0; // Clears previously connected dynamic sensors Loading @@ -156,8 +216,7 @@ Return<Result> HalProxy::initialize( mDynamicSensorsCallback = sensorsCallback; // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions. mEventQueue = std::make_unique<EventMessageQueue>(eventQueueDescriptor, true /* resetPointers */); mEventQueue = std::move(eventQueue); // Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP // events have been successfully read and handled by the framework. Loading Loading @@ -186,12 +245,10 @@ Return<Result> HalProxy::initialize( mWakelockThread = std::thread(startWakelockThread, this); for (size_t i = 0; i < mSubHalList.size(); i++) { auto subHal = mSubHalList[i]; const auto& subHalCallback = mSubHalCallbacks[i]; Result currRes = subHal->initialize(subHalCallback); Result currRes = mSubHalList[i]->initialize(this, this, i); if (currRes != Result::OK) { result = currRes; ALOGE("Subhal '%s' failed to initialize.", subHal->getName().c_str()); ALOGE("Subhal '%s' failed to initialize.", mSubHalList[i]->getName().c_str()); break; } } Loading @@ -217,7 +274,11 @@ Return<Result> HalProxy::flush(int32_t sensorHandle) { return getSubHalForSensorHandle(sensorHandle)->flush(clearSubHalIndex(sensorHandle)); } Return<Result> HalProxy::injectSensorData(const Event& event) { Return<Result> HalProxy::injectSensorData_2_1(const V2_1::Event& event) { return injectSensorData(convertToOldEvent(event)); } Return<Result> HalProxy::injectSensorData(const V1_0::Event& event) { Result result = Result::OK; if (mCurrentOperationMode == OperationMode::NORMAL && event.sensorType != V1_0::SensorType::ADDITIONAL_INFO) { Loading @@ -226,18 +287,19 @@ Return<Result> HalProxy::injectSensorData(const Event& event) { result = Result::BAD_VALUE; } if (result == Result::OK) { Event subHalEvent = event; V1_0::Event subHalEvent = event; if (!isSubHalIndexValid(event.sensorHandle)) { return Result::BAD_VALUE; } subHalEvent.sensorHandle = clearSubHalIndex(event.sensorHandle); result = getSubHalForSensorHandle(event.sensorHandle)->injectSensorData(subHalEvent); result = getSubHalForSensorHandle(event.sensorHandle) ->injectSensorData(convertToNewEvent(subHalEvent)); } return result; } Return<void> HalProxy::registerDirectChannel(const SharedMemInfo& mem, registerDirectChannel_cb _hidl_cb) { ISensorsV2_0::registerDirectChannel_cb _hidl_cb) { if (mDirectChannelSubHal == nullptr) { _hidl_cb(Result::INVALID_OPERATION, -1 /* channelHandle */); } else { Loading @@ -257,7 +319,8 @@ Return<Result> HalProxy::unregisterDirectChannel(int32_t channelHandle) { } Return<void> HalProxy::configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate, configDirectReport_cb _hidl_cb) { RateLevel rate, ISensorsV2_0::configDirectReport_cb _hidl_cb) { if (mDirectChannelSubHal == nullptr) { _hidl_cb(Result::INVALID_OPERATION, -1 /* reportToken */); } else if (sensorHandle == -1 && rate != RateLevel::STOP) { Loading Loading @@ -302,7 +365,7 @@ Return<void> HalProxy::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& stream << " # of non-dynamic sensors across all subhals: " << mSensors.size() << std::endl; stream << " # of dynamic sensors across all subhals: " << mDynamicSensors.size() << std::endl; stream << "SubHals (" << mSubHalList.size() << "):" << std::endl; for (ISensorsSubHal* subHal : mSubHalList) { for (auto& subHal : mSubHalList) { stream << " Name: " << subHal->getName() << std::endl; stream << " Debug dump: " << std::endl; android::base::WriteStringToFd(stream.str(), writeFd); Loading Loading @@ -369,57 +432,66 @@ void HalProxy::initializeSubHalListFromConfigFile(const char* configFileName) { } else { SensorsHalGetSubHalFunc* sensorsHalGetSubHalPtr = (SensorsHalGetSubHalFunc*)dlsym(handle, "sensorsHalGetSubHal"); if (sensorsHalGetSubHalPtr == nullptr) { ALOGE("Failed to locate sensorsHalGetSubHal function for library: %s", subHalLibraryFile.c_str()); } else { if (sensorsHalGetSubHalPtr != nullptr) { std::function<SensorsHalGetSubHalFunc> sensorsHalGetSubHal = *sensorsHalGetSubHalPtr; uint32_t version; ISensorsSubHal* subHal = sensorsHalGetSubHal(&version); ISensorsSubHalV2_0* subHal = sensorsHalGetSubHal(&version); if (version != SUB_HAL_2_0_VERSION) { ALOGE("SubHal version was not 2.0 for library: %s", subHalLibraryFile.c_str()); } else { ALOGV("Loaded SubHal from library: %s", subHalLibraryFile.c_str()); mSubHalList.push_back(subHal); mSubHalList.push_back(std::make_unique<SubHalWrapperV2_0>(subHal)); } } else { SensorsHalGetSubHalV2_1Func* getSubHalV2_1Ptr = (SensorsHalGetSubHalV2_1Func*)dlsym(handle, "sensorsHalGetSubHal_2_1"); if (getSubHalV2_1Ptr == nullptr) { ALOGE("Failed to locate sensorsHalGetSubHal function for library: %s", subHalLibraryFile.c_str()); } else { std::function<SensorsHalGetSubHalV2_1Func> sensorsHalGetSubHal_2_1 = *getSubHalV2_1Ptr; uint32_t version; ISensorsSubHalV2_1* subHal = sensorsHalGetSubHal_2_1(&version); if (version != SUB_HAL_2_1_VERSION) { ALOGE("SubHal version was not 2.1 for library: %s", subHalLibraryFile.c_str()); } else { ALOGV("Loaded SubHal from library: %s", subHalLibraryFile.c_str()); mSubHalList.push_back(std::make_unique<SubHalWrapperV2_1>(subHal)); } } } } } void HalProxy::initializeSubHalCallbacks() { for (size_t subHalIndex = 0; subHalIndex < mSubHalList.size(); subHalIndex++) { sp<IHalProxyCallback> callback = new HalProxyCallback(this, subHalIndex); mSubHalCallbacks.push_back(callback); } } void HalProxy::initializeSensorList() { for (size_t subHalIndex = 0; subHalIndex < mSubHalList.size(); subHalIndex++) { ISensorsSubHal* subHal = mSubHalList[subHalIndex]; auto result = subHal->getSensorsList([&](const auto& list) { auto result = mSubHalList[subHalIndex]->getSensorsList([&](const auto& list) { for (SensorInfo sensor : list) { if (!subHalIndexIsClear(sensor.sensorHandle)) { ALOGE("SubHal sensorHandle's first byte was not 0"); } else { ALOGV("Loaded sensor: %s", sensor.name.c_str()); sensor.sensorHandle = setSubHalIndex(sensor.sensorHandle, subHalIndex); setDirectChannelFlags(&sensor, subHal); setDirectChannelFlags(&sensor, mSubHalList[subHalIndex]); mSensors[sensor.sensorHandle] = sensor; } } }); if (!result.isOk()) { ALOGE("getSensorsList call failed for SubHal: %s", subHal->getName().c_str()); ALOGE("getSensorsList call failed for SubHal: %s", mSubHalList[subHalIndex]->getName().c_str()); } } } void HalProxy::init() { initializeSubHalCallbacks(); initializeSensorList(); } Loading Loading @@ -552,7 +624,7 @@ void HalProxy::resetSharedWakelock() { } void HalProxy::postEventsToMessageQueue(const std::vector<Event>& events, size_t numWakeupEvents, ScopedWakelock wakelock) { V2_0::implementation::ScopedWakelock wakelock) { size_t numToWrite = 0; std::lock_guard<std::mutex> lock(mEventQueueWriteMutex); if (wakelock.isLocked()) { Loading Loading @@ -610,7 +682,8 @@ void HalProxy::decrementRefCountAndMaybeReleaseWakelock(size_t delta, } } void HalProxy::setDirectChannelFlags(SensorInfo* sensorInfo, ISensorsSubHal* subHal) { void HalProxy::setDirectChannelFlags(SensorInfo* sensorInfo, std::shared_ptr<ISubHalWrapperBase> subHal) { bool sensorSupportsDirectChannel = (sensorInfo->flags & (V1_0::SensorFlagBits::MASK_DIRECT_REPORT | V1_0::SensorFlagBits::MASK_DIRECT_CHANNEL)) != 0; Loading @@ -624,7 +697,7 @@ void HalProxy::setDirectChannelFlags(SensorInfo* sensorInfo, ISensorsSubHal* sub } } ISensorsSubHal* HalProxy::getSubHalForSensorHandle(int32_t sensorHandle) { std::shared_ptr<ISubHalWrapperBase> HalProxy::getSubHalForSensorHandle(int32_t sensorHandle) { return mSubHalList[extractSubHalIndex(sensorHandle)]; } Loading @@ -651,46 +724,8 @@ bool HalProxy::subHalIndexIsClear(int32_t sensorHandle) { return (sensorHandle & kSensorHandleSubHalIndexMask) == 0; } void HalProxyCallback::postEvents(const std::vector<Event>& events, ScopedWakelock wakelock) { if (events.empty() || !mHalProxy->areThreadsRunning()) return; size_t numWakeupEvents; std::vector<Event> processedEvents = processEvents(events, &numWakeupEvents); if (numWakeupEvents > 0) { ALOG_ASSERT(wakelock.isLocked(), "Wakeup events posted while wakelock unlocked for subhal" " w/ index %" PRId32 ".", mSubHalIndex); } else { ALOG_ASSERT(!wakelock.isLocked(), "No Wakeup events posted but wakelock locked for subhal" " w/ index %" PRId32 ".", mSubHalIndex); } mHalProxy->postEventsToMessageQueue(processedEvents, numWakeupEvents, std::move(wakelock)); } ScopedWakelock HalProxyCallback::createScopedWakelock(bool lock) { ScopedWakelock wakelock(mHalProxy, lock); return wakelock; } std::vector<Event> HalProxyCallback::processEvents(const std::vector<Event>& events, size_t* numWakeupEvents) const { *numWakeupEvents = 0; std::vector<Event> eventsOut; for (Event event : events) { event.sensorHandle = setSubHalIndex(event.sensorHandle, mSubHalIndex); eventsOut.push_back(event); const SensorInfo& sensor = mHalProxy->getSensorInfo(event.sensorHandle); if ((sensor.flags & V1_0::SensorFlagBits::WAKE_UP) != 0) { (*numWakeupEvents)++; } } return eventsOut; } } // namespace implementation } // namespace V2_0 } // namespace V2_1 } // namespace sensors } // namespace hardware } // namespace android
sensors/common/default/2.X/multihal/HalProxyCallback.cpp 0 → 100644 +84 −0 Original line number Diff line number Diff line /* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "HalProxyCallback.h" #include <cinttypes> namespace android { namespace hardware { namespace sensors { namespace V2_0 { namespace implementation { static constexpr int32_t kBitsAfterSubHalIndex = 24; /** * Set the subhal index as first byte of sensor handle and return this modified version. * * @param sensorHandle The sensor handle to modify. * @param subHalIndex The index in the hal proxy of the sub hal this sensor belongs to. * * @return The modified sensor handle. */ int32_t setSubHalIndex(int32_t sensorHandle, size_t subHalIndex) { return sensorHandle | (static_cast<int32_t>(subHalIndex) << kBitsAfterSubHalIndex); } void HalProxyCallbackBase::postEvents(const std::vector<V2_1::Event>& events, ScopedWakelock wakelock) { if (events.empty() || !mCallback->areThreadsRunning()) return; size_t numWakeupEvents; std::vector<V2_1::Event> processedEvents = processEvents(events, &numWakeupEvents); if (numWakeupEvents > 0) { ALOG_ASSERT(wakelock.isLocked(), "Wakeup events posted while wakelock unlocked for subhal" " w/ index %" PRId32 ".", mSubHalIndex); } else { ALOG_ASSERT(!wakelock.isLocked(), "No Wakeup events posted but wakelock locked for subhal" " w/ index %" PRId32 ".", mSubHalIndex); } mCallback->postEventsToMessageQueue(processedEvents, numWakeupEvents, std::move(wakelock)); } ScopedWakelock HalProxyCallbackBase::createScopedWakelock(bool lock) { ScopedWakelock wakelock(mRefCounter, lock); return wakelock; } std::vector<V2_1::Event> HalProxyCallbackBase::processEvents(const std::vector<V2_1::Event>& events, size_t* numWakeupEvents) const { *numWakeupEvents = 0; std::vector<V2_1::Event> eventsOut; for (V2_1::Event event : events) { event.sensorHandle = setSubHalIndex(event.sensorHandle, mSubHalIndex); eventsOut.push_back(event); const V2_1::SensorInfo& sensor = mCallback->getSensorInfo(event.sensorHandle); if ((sensor.flags & V1_0::SensorFlagBits::WAKE_UP) != 0) { (*numWakeupEvents)++; } } return eventsOut; } } // namespace implementation } // namespace V2_0 } // namespace sensors } // namespace hardware } // namespace android