Acquire and Release Wake Lock
Acquire a wake lock in the Sensors 2.0 Default implementation whenever
there are outstanding WAKE_UP events. Release the wake lock whenever
the number of oustanding WAKE_UP events is zero or at least
SensorTimeout::WAKE_LOCK_SECONDS seconds have elapsed since the
previous WAKE_UP event was written to the Event FMQ.
Bug: 111070257
Test: Builds, wake lock is acquired and released as expected.
Change-Id: I7c57724430144fd4022646d1fef1b1fa8bc4235d
diff --git a/sensors/2.0/default/Sensors.h b/sensors/2.0/default/Sensors.h
index f543935..eba3f97 100644
--- a/sensors/2.0/default/Sensors.h
+++ b/sensors/2.0/default/Sensors.h
@@ -21,10 +21,13 @@
#include <android/hardware/sensors/2.0/ISensors.h>
#include <fmq/MessageQueue.h>
+#include <hardware_legacy/power.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>
+#include <atomic>
#include <memory>
+#include <thread>
namespace android {
namespace hardware {
@@ -80,7 +83,7 @@
Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
configDirectReport_cb _hidl_cb) override;
- void postEvents(const std::vector<Event>& events) override;
+ void postEvents(const std::vector<Event>& events, bool wakeup) override;
private:
/**
@@ -88,6 +91,18 @@
*/
void deleteEventFlag();
+ /**
+ * Function to read the Wake Lock FMQ and release the wake lock when appropriate
+ */
+ void readWakeLockFMQ();
+
+ static void startReadWakeLockThread(Sensors* sensors);
+
+ /**
+ * Responsible for acquiring and releasing a wake lock when there are unhandled WAKE_UP events
+ */
+ void updateWakeLock(int32_t eventsWritten, int32_t eventsHandled);
+
using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>;
using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>;
@@ -117,9 +132,39 @@
std::map<int32_t, std::shared_ptr<Sensor>> mSensors;
/**
- * Lock to protect writes and reads to the FMQs
+ * Lock to protect writes to the FMQs
*/
- std::mutex mLock;
+ std::mutex mWriteLock;
+
+ /**
+ * Lock to protect acquiring and releasing the wake lock
+ */
+ std::mutex mWakeLockLock;
+
+ /**
+ * Track the number of WAKE_UP events that have not been handled by the framework
+ */
+ uint32_t mOutstandingWakeUpEvents;
+
+ /**
+ * A thread to read the Wake Lock FMQ
+ */
+ std::thread mWakeLockThread;
+
+ /**
+ * Flag to indicate that the Wake Lock Thread should continue to run
+ */
+ std::atomic_bool mReadWakeLockQueueRun;
+
+ /**
+ * Track the time when the wake lock should automatically be released
+ */
+ int64_t mAutoReleaseWakeLockTime;
+
+ /**
+ * Flag to indicate if a wake lock has been acquired
+ */
+ bool mHasWakeLock;
};
} // namespace implementation