mirror of
https://github.com/Evolution-X/hardware_interfaces
synced 2026-02-02 20:24:19 +00:00
MH2 | Implement ScopeWakelock ctor and dtor
Put the ScopedWakelock class into its own header and its implementation into its own cpp. Implement the refcounting of the wakelocks. However, do not incorporate the wakelock handling into post events in halproxy yet. Bug: 136511617 Test: No new tests yet. Will add more in a later patchset once more fleshed out and integrated with postEvents. Change-Id: I0815c6a6659ec5933b799294956595b11e7bf1b3
This commit is contained in:
@@ -42,6 +42,7 @@ cc_binary {
|
||||
srcs: [
|
||||
"service.cpp",
|
||||
"HalProxy.cpp",
|
||||
"ScopedWakelock.cpp",
|
||||
],
|
||||
init_rc: ["android.hardware.sensors@2.0-service-multihal.rc"],
|
||||
vintf_fragments: ["android.hardware.sensors@2.0-multihal.xml"],
|
||||
@@ -61,6 +62,7 @@ cc_test_library {
|
||||
vendor_available: true,
|
||||
srcs: [
|
||||
"HalProxy.cpp",
|
||||
"ScopedWakelock.cpp",
|
||||
],
|
||||
export_header_lib_headers: [
|
||||
"android.hardware.sensors@2.0-multihal.header",
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
|
||||
#include <android/hardware/sensors/2.0/types.h>
|
||||
|
||||
#include "hardware_legacy/power.h"
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include <fstream>
|
||||
@@ -36,10 +38,6 @@ using ::android::hardware::sensors::V2_0::EventQueueFlagBits;
|
||||
|
||||
typedef ISensorsSubHal*(SensorsHalGetSubHalFunc)(uint32_t*);
|
||||
|
||||
ScopedWakelock::ScopedWakelock() {
|
||||
// TODO: Implement
|
||||
}
|
||||
|
||||
HalProxy::HalProxy() {
|
||||
const char* kMultiHalConfigFile = "/vendor/etc/sensors/hals.conf";
|
||||
initializeSubHalListFromConfigFile(kMultiHalConfigFile);
|
||||
@@ -277,6 +275,25 @@ void HalProxy::postEventsToMessageQueue(const std::vector<Event>& events) {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Implement the wakelock timeout in these next two methods. Also pass in the subhal
|
||||
// index for better tracking.
|
||||
|
||||
void HalProxy::incrementRefCountAndMaybeAcquireWakelock() {
|
||||
std::lock_guard<std::mutex> lockGuard(mWakelockRefCountMutex);
|
||||
if (mWakelockRefCount == 0) {
|
||||
acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLockName);
|
||||
}
|
||||
mWakelockRefCount++;
|
||||
}
|
||||
|
||||
void HalProxy::decrementRefCountAndMaybeReleaseWakelock() {
|
||||
std::lock_guard<std::mutex> lockGuard(mWakelockRefCountMutex);
|
||||
mWakelockRefCount--;
|
||||
if (mWakelockRefCount == 0) {
|
||||
release_wake_lock(kWakeLockName);
|
||||
}
|
||||
}
|
||||
|
||||
void HalProxy::setDirectChannelFlags(SensorInfo* sensorInfo, ISensorsSubHal* subHal) {
|
||||
bool sensorSupportsDirectChannel =
|
||||
(sensorInfo->flags & (V1_0::SensorFlagBits::MASK_DIRECT_REPORT |
|
||||
@@ -319,8 +336,7 @@ void HalProxyCallback::postEvents(const std::vector<Event>& events, ScopedWakelo
|
||||
}
|
||||
|
||||
ScopedWakelock HalProxyCallback::createScopedWakelock(bool lock) {
|
||||
ScopedWakelock wakelock;
|
||||
wakelock.mLocked = lock;
|
||||
ScopedWakelock wakelock(mHalProxy, lock);
|
||||
return wakelock;
|
||||
}
|
||||
|
||||
|
||||
44
sensors/2.0/multihal/ScopedWakelock.cpp
Normal file
44
sensors/2.0/multihal/ScopedWakelock.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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 "ScopedWakelock.h"
|
||||
|
||||
namespace android {
|
||||
namespace hardware {
|
||||
namespace sensors {
|
||||
namespace V2_0 {
|
||||
namespace implementation {
|
||||
|
||||
ScopedWakelock::ScopedWakelock(IScopedWakelockRefCounter* refCounter, bool locked)
|
||||
: mRefCounter(refCounter), mLocked(locked) {
|
||||
// TODO: Move this implementation into HalProxy object instead
|
||||
if (mLocked) {
|
||||
mRefCounter->incrementRefCountAndMaybeAcquireWakelock();
|
||||
}
|
||||
}
|
||||
|
||||
ScopedWakelock::~ScopedWakelock() {
|
||||
// TODO: Move this implementation into HalProxy object instead
|
||||
if (mLocked) {
|
||||
mRefCounter->decrementRefCountAndMaybeReleaseWakelock();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace implementation
|
||||
} // namespace V2_0
|
||||
} // namespace sensors
|
||||
} // namespace hardware
|
||||
} // namespace android
|
||||
@@ -41,7 +41,7 @@ using ::android::hardware::MQDescriptor;
|
||||
using ::android::hardware::Return;
|
||||
using ::android::hardware::Void;
|
||||
|
||||
class HalProxy : public ISensors {
|
||||
class HalProxy : public ISensors, public IScopedWakelockRefCounter {
|
||||
public:
|
||||
using Event = ::android::hardware::sensors::V1_0::Event;
|
||||
using OperationMode = ::android::hardware::sensors::V1_0::OperationMode;
|
||||
@@ -95,6 +95,18 @@ class HalProxy : public ISensors {
|
||||
Return<void> onDynamicSensorsDisconnected(const hidl_vec<int32_t>& dynamicSensorHandlesRemoved,
|
||||
int32_t subHalIndex);
|
||||
|
||||
// Below methods follow IScopedWakelockRefCounter
|
||||
|
||||
/**
|
||||
* Increment ref count and maybe acquire wakelock.
|
||||
*/
|
||||
void incrementRefCountAndMaybeAcquireWakelock() override;
|
||||
|
||||
/**
|
||||
* Decrement ref count and maybe release wakelock.
|
||||
*/
|
||||
void decrementRefCountAndMaybeReleaseWakelock() override;
|
||||
|
||||
// Below methods are for HalProxyCallback
|
||||
|
||||
/**
|
||||
@@ -120,6 +132,8 @@ class HalProxy : public ISensors {
|
||||
using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>;
|
||||
using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>;
|
||||
|
||||
const char* kWakeLockName = "SensorsHAL_WAKEUP";
|
||||
|
||||
/**
|
||||
* The Event FMQ where sensor events are written
|
||||
*/
|
||||
@@ -165,6 +179,12 @@ class HalProxy : public ISensors {
|
||||
//! The mutex for the event queue.
|
||||
std::mutex mEventQueueMutex;
|
||||
|
||||
//! The scoped wakelock ref count.
|
||||
size_t mWakelockRefCount = 0;
|
||||
|
||||
//! The mutex guarding the mWakelockRefCount variable
|
||||
std::mutex mWakelockRefCountMutex;
|
||||
|
||||
//! The bit mask used to get the subhal index from a sensor handle.
|
||||
static constexpr uint32_t kSensorHandleSubHalIndexMask = 0xFF000000;
|
||||
|
||||
@@ -219,11 +239,6 @@ class HalProxy : public ISensors {
|
||||
static uint32_t clearSubHalIndex(uint32_t sensorHandle);
|
||||
};
|
||||
|
||||
// TODO: Use this wake lock name as the prefix to all sensors HAL wake locks acquired.
|
||||
// constexpr const char* kWakeLockName = "SensorsHAL_WAKEUP";
|
||||
|
||||
// TODO: Use the following class as a starting point for implementing the full HalProxyCallback
|
||||
// along with being inspiration for how to implement the ScopedWakelock class.
|
||||
/**
|
||||
* Callback class used to provide the HalProxy with the index of which subHal is invoking
|
||||
*/
|
||||
|
||||
77
sensors/2.0/multihal/include/ScopedWakelock.h
Normal file
77
sensors/2.0/multihal/include/ScopedWakelock.h
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
|
||||
namespace android {
|
||||
namespace hardware {
|
||||
namespace sensors {
|
||||
namespace V2_0 {
|
||||
namespace implementation {
|
||||
|
||||
class IScopedWakelockRefCounter {
|
||||
public:
|
||||
virtual void incrementRefCountAndMaybeAcquireWakelock() = 0;
|
||||
virtual void decrementRefCountAndMaybeReleaseWakelock() = 0;
|
||||
// Virtual dtor needed for compilation success
|
||||
virtual ~IScopedWakelockRefCounter(){};
|
||||
};
|
||||
|
||||
/**
|
||||
* Wrapper around wake lock acquisition functions (acquire/release_wake_lock) that provides a
|
||||
* RAII-style mechanism for keeping a wake lock held for the duration of a scoped block.
|
||||
* When a ScopedWakelock is created, it increments the reference count stored in the HalProxy
|
||||
* for the sub-HALs specific wake lock, acquiring the wake lock if necessary. When the object goes
|
||||
* out of scope, the ref count is decremented, potentially releasing the wake lock if no other
|
||||
* references to the wake lock exist.
|
||||
*
|
||||
* This class is allocated through the createScopedWakelock callback inside the IHalProxyCallback
|
||||
* provided to sub-HALs during initialization and should be used for all wake lock acquisition
|
||||
* inside of the sub-HAL to ensure wake locks are not held indefinitely.
|
||||
*
|
||||
* The most prevalent use case for this class will be for posting events to the framework through
|
||||
* the postEvents HalProxy callback. The expectation is that sub-HALs will create this
|
||||
* ScopedWakelock through the createScopedWakelock upon receiving a sensor events. The lock boolean
|
||||
* provided to createScopedWakelock will be set the according to whether the sensor events are
|
||||
* from wakeup sensors. Then, the sub-HAL will perform any processing necessary before invoking the
|
||||
* postEvents callback passing in the previously created ScopedWakelock. At this point, ownership
|
||||
* of the object will be passed to the HalProxy that will then be responsible for ensuring any
|
||||
* wake locks continue to be held, if necessary.
|
||||
*/
|
||||
class ScopedWakelock {
|
||||
public:
|
||||
ScopedWakelock(ScopedWakelock&&) = default;
|
||||
ScopedWakelock& operator=(ScopedWakelock&&) = default;
|
||||
virtual ~ScopedWakelock();
|
||||
|
||||
bool isLocked() const { return mLocked; }
|
||||
|
||||
private:
|
||||
friend class HalProxyCallback;
|
||||
IScopedWakelockRefCounter* mRefCounter;
|
||||
bool mLocked;
|
||||
ScopedWakelock(IScopedWakelockRefCounter* refCounter, bool locked);
|
||||
ScopedWakelock(const ScopedWakelock&) = delete;
|
||||
ScopedWakelock& operator=(const ScopedWakelock&) = delete;
|
||||
};
|
||||
|
||||
} // namespace implementation
|
||||
} // namespace V2_0
|
||||
} // namespace sensors
|
||||
} // namespace hardware
|
||||
} // namespace android
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ScopedWakelock.h"
|
||||
|
||||
#include <android/hardware/sensors/1.0/types.h>
|
||||
#include <android/hardware/sensors/2.0/ISensors.h>
|
||||
|
||||
@@ -35,45 +37,6 @@ using ::android::hardware::sensors::V1_0::Event;
|
||||
using ::android::hardware::sensors::V1_0::Result;
|
||||
using ::android::hardware::sensors::V1_0::SensorInfo;
|
||||
|
||||
/**
|
||||
* Wrapper around wake lock acquisition functions (acquire/release_wake_lock) that provides a
|
||||
* RAII-style mechanism for keeping a wake lock held for the duration of a scoped block.
|
||||
* When a ScopedWakelock is created, it increments the reference count stored in the HalProxy
|
||||
* for the sub-HALs specific wake lock, acquiring the wake lock if necessary. When the object goes
|
||||
* out of scope, the ref count is decremented, potentially releasing the wake lock if no other
|
||||
* references to the wake lock exist.
|
||||
*
|
||||
* This class is allocated through the createScopedWakelock callback inside the IHalProxyCallback
|
||||
* provided to sub-HALs during initialization and should be used for all wake lock acquisition
|
||||
* inside of the sub-HAL to ensure wake locks are not held indefinitely.
|
||||
*
|
||||
* The most prevalent use case for this class will be for posting events to the framework through
|
||||
* the postEvents HalProxy callback. The expectation is that sub-HALs will create this
|
||||
* ScopedWakelock through the createScopedWakelock upon receiving a sensor events. The lock boolean
|
||||
* provided to createScopedWakelock will be set the according to whether the sensor events are
|
||||
* from wakeup sensors. Then, the sub-HAL will perform any processing necessary before invoking the
|
||||
* postEvents callback passing in the previously created ScopedWakelock. At this point, ownership
|
||||
* of the object will be passed to the HalProxy that will then be responsible for ensuring any
|
||||
* wake locks continue to be held, if necessary.
|
||||
*/
|
||||
class ScopedWakelock {
|
||||
public:
|
||||
ScopedWakelock(ScopedWakelock&&) = default;
|
||||
ScopedWakelock& operator=(ScopedWakelock&&) = default;
|
||||
virtual ~ScopedWakelock() { mLocked = false; };
|
||||
|
||||
bool isLocked() const { return mLocked; }
|
||||
|
||||
protected:
|
||||
bool mLocked;
|
||||
|
||||
private:
|
||||
friend class HalProxyCallback;
|
||||
ScopedWakelock();
|
||||
ScopedWakelock(const ScopedWakelock&) = delete;
|
||||
ScopedWakelock& operator=(const ScopedWakelock&) = delete;
|
||||
};
|
||||
|
||||
/**
|
||||
* Interface that contains several callbacks into the HalProxy class to communicate dynamic sensor
|
||||
* changes and sensor events to the framework and acquire wake locks. The HalProxy will ensure
|
||||
|
||||
@@ -33,6 +33,9 @@ cc_defaults {
|
||||
"libpower",
|
||||
"libutils",
|
||||
],
|
||||
static_libs: [
|
||||
"android.hardware.sensors@2.0-HalProxy",
|
||||
],
|
||||
}
|
||||
|
||||
cc_library {
|
||||
|
||||
@@ -19,8 +19,8 @@
|
||||
#include <fmq/MessageQueue.h>
|
||||
|
||||
#include "HalProxy.h"
|
||||
#include "ScopedWakelock.h"
|
||||
#include "SensorsSubHal.h"
|
||||
#include "SubHal.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user