MH2 | Add rough proxy callback postEvents method
Add post events method to HalProxyCallback without fully implementing
the wakeup events handling and pending blocking writes. Also change
mSensorList of the halProxy to mSensors which is a map from sensorHandle
to sensorInfo so that getNumWakeupEvents method of callback can find
sensorinfo info from sensorhandle of event type. Instantiate
halproxycallback vector in HalProxy for each subhal in the ctor as well.
Add very simple test for the postEvents method of callback.
Bug: 136511617
Test: New unit test passes.
Change-Id: I39c861cff286f24992bfcfcaa6bb468b4544b0e0
diff --git a/sensors/2.0/multihal/include/HalProxy.h b/sensors/2.0/multihal/include/HalProxy.h
index 4ecb58b..6c50eef 100644
--- a/sensors/2.0/multihal/include/HalProxy.h
+++ b/sensors/2.0/multihal/include/HalProxy.h
@@ -24,6 +24,8 @@
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>
+#include <map>
+
namespace android {
namespace hardware {
namespace sensors {
@@ -45,7 +47,9 @@
using OperationMode = ::android::hardware::sensors::V1_0::OperationMode;
using RateLevel = ::android::hardware::sensors::V1_0::RateLevel;
using Result = ::android::hardware::sensors::V1_0::Result;
+ using SensorInfo = ::android::hardware::sensors::V1_0::SensorInfo;
using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo;
+ using ISensorsSubHal = ::android::hardware::sensors::V2_0::implementation::ISensorsSubHal;
explicit HalProxy();
// Test only constructor.
@@ -91,6 +95,27 @@
Return<void> onDynamicSensorsDisconnected(const hidl_vec<int32_t>& dynamicSensorHandlesRemoved,
int32_t subHalIndex);
+ // Below methods are for HalProxyCallback
+
+ /**
+ * Post events to the event message queue if there is room to write them. Otherwise post the
+ * remaining events to a background thread for a blocking write with a 5 second timeout.
+ *
+ * @param events The list of events to post to the message queue.
+ */
+ void postEventsToMessageQueue(const std::vector<Event>& events);
+
+ /**
+ * Get the SensorInfo object associated with the sensorHandle.
+ *
+ * @param sensorHandle The sensorHandle for the sensor.
+ *
+ * @return The sensor info for the sensor.
+ */
+ const SensorInfo& getSensorInfo(uint32_t sensorHandle) const {
+ return mSensors.at(sensorHandle);
+ }
+
private:
using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>;
using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>;
@@ -120,14 +145,16 @@
*/
std::vector<ISensorsSubHal*> mSubHalList;
+ std::vector<const sp<IHalProxyCallback>> mSubHalCallbacks;
+
/**
- * List of SensorInfo objects that contains the sensor info from subhals as
+ * Map of sensor handles to SensorInfo objects that contains the sensor info from subhals as
* well as the modified sensor handle for the framework.
*
- * The subhal index is encoded in the first byte of the sensor handle and
- * the remaining bytes are generated by the subhal to identify the sensor.
+ * The subhal index is encoded in the first byte of the sensor handle and the remaining
+ * bytes are generated by the subhal to identify the sensor.
*/
- std::vector<SensorInfo> mSensorList;
+ std::map<uint32_t, SensorInfo> mSensors;
//! The current operation mode for all subhals.
OperationMode mCurrentOperationMode = OperationMode::NORMAL;
@@ -135,6 +162,9 @@
//! The single subHal that supports directChannel reporting.
ISensorsSubHal* mDirectChannelSubHal = nullptr;
+ //! The mutex for the event queue.
+ std::mutex mEventQueueMutex;
+
//! The bit mask used to get the subhal index from a sensor handle.
static constexpr uint32_t kSensorHandleSubHalIndexMask = 0xFF000000;
@@ -145,12 +175,22 @@
void initializeSubHalListFromConfigFile(const char* configFileName);
/**
+ * Initialize the HalProxyCallback vector using the list of subhals.
+ */
+ void initializeSubHalCallbacks();
+
+ /**
* Initialize the list of SensorInfo objects in mSensorList by getting sensors from each
* subhal.
*/
void initializeSensorList();
/**
+ * Calls the above two helper methods which are shared in both ctors.
+ */
+ void initializeSubHalCallbacksAndSensorList();
+
+ /**
* Clear direct channel flags if the HalProxy has already chosen a subhal as its direct channel
* subhal. Set the directChannelSubHal pointer to the subHal passed in if this is the first
* direct channel enabled sensor seen.
@@ -179,6 +219,45 @@
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
+ */
+class HalProxyCallback : public IHalProxyCallback {
+ using SensorInfo = ::android::hardware::sensors::V1_0::SensorInfo;
+
+ public:
+ HalProxyCallback(HalProxy* halProxy, int32_t subHalIndex)
+ : mHalProxy(halProxy), mSubHalIndex(subHalIndex) {}
+
+ Return<void> onDynamicSensorsConnected(
+ const hidl_vec<SensorInfo>& dynamicSensorsAdded) override {
+ return mHalProxy->onDynamicSensorsConnected(dynamicSensorsAdded, mSubHalIndex);
+ }
+
+ Return<void> onDynamicSensorsDisconnected(
+ const hidl_vec<int32_t>& dynamicSensorHandlesRemoved) override {
+ return mHalProxy->onDynamicSensorsDisconnected(dynamicSensorHandlesRemoved, mSubHalIndex);
+ }
+
+ void postEvents(const std::vector<Event>& events, ScopedWakelock wakelock);
+
+ ScopedWakelock createScopedWakelock(bool lock);
+
+ private:
+ HalProxy* mHalProxy;
+ int32_t mSubHalIndex;
+
+ std::vector<Event> processEvents(const std::vector<Event>& events,
+ size_t* numWakeupEvents) const;
+
+ uint32_t setSubHalIndex(uint32_t sensorHandle) const;
+};
+
} // namespace implementation
} // namespace V2_0
} // namespace sensors
diff --git a/sensors/2.0/multihal/include/SubHal.h b/sensors/2.0/multihal/include/SubHal.h
index e84cba5..6181e0b 100644
--- a/sensors/2.0/multihal/include/SubHal.h
+++ b/sensors/2.0/multihal/include/SubHal.h
@@ -21,10 +21,6 @@
#include <vector>
-using ::android::hardware::sensors::V1_0::Event;
-using ::android::hardware::sensors::V1_0::Result;
-using ::android::hardware::sensors::V1_0::SensorInfo;
-
// Indicates the current version of the multiHAL interface formatted as (HAL major version) << 24 |
// (HAL minor version) << 16 | (multiHAL version)
#define SUB_HAL_2_0_VERSION 0x02000000
@@ -35,6 +31,10 @@
namespace V2_0 {
namespace implementation {
+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.
@@ -68,7 +68,7 @@
bool mLocked;
private:
- // TODO: Mark HalProxy's subclass of ScopedWakelock as a friend so that it can be initialized.
+ friend class HalProxyCallback;
ScopedWakelock();
ScopedWakelock(const ScopedWakelock&) = delete;
ScopedWakelock& operator=(const ScopedWakelock&) = delete;
@@ -169,7 +169,9 @@
/**
* First method invoked on the sub-HAL after it's allocated through sensorsHalGetSubHal() by the
* HalProxy. Sub-HALs should use this to initialize any state and retain the callback given in
- * order to communicate with the HalProxy.
+ * order to communicate with the HalProxy. Method will be called anytime the sensors framework
+ * restarts. Therefore, this method will be responsible for reseting the state of the subhal and
+ * cleaning up and reallocating any previously allocated data.
*
* @param halProxyCallback callback used to inform the HalProxy when a dynamic sensor's state
* changes, new sensor events should be sent to the framework, and when a new ScopedWakelock