mirror of
https://github.com/Evolution-X/hardware_interfaces
synced 2026-02-01 22:04:26 +00:00
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
This commit is contained in:
@@ -16,12 +16,15 @@
|
||||
|
||||
#include "HalProxy.h"
|
||||
|
||||
#include "SubHal.h"
|
||||
|
||||
#include <android/hardware/sensors/2.0/types.h>
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <thread>
|
||||
|
||||
namespace android {
|
||||
namespace hardware {
|
||||
@@ -29,53 +32,22 @@ namespace sensors {
|
||||
namespace V2_0 {
|
||||
namespace implementation {
|
||||
|
||||
using ::android::hardware::sensors::V2_0::EventQueueFlagBits;
|
||||
|
||||
typedef ISensorsSubHal*(SensorsHalGetSubHalFunc)(uint32_t*);
|
||||
|
||||
// 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 SensorsCallbackProxy : public ISensorsCallback {
|
||||
public:
|
||||
SensorsCallbackProxy(wp<HalProxy>& halProxy, int32_t subHalIndex)
|
||||
: mHalProxy(halProxy), mSubHalIndex(subHalIndex) {}
|
||||
|
||||
Return<void> onDynamicSensorsConnected(
|
||||
const hidl_vec<SensorInfo>& dynamicSensorsAdded) override {
|
||||
sp<HalProxy> halProxy(mHalProxy.promote());
|
||||
if (halProxy != nullptr) {
|
||||
return halProxy->onDynamicSensorsConnected(dynamicSensorsAdded, mSubHalIndex);
|
||||
}
|
||||
return Return<void>();
|
||||
}
|
||||
|
||||
Return<void> onDynamicSensorsDisconnected(
|
||||
const hidl_vec<int32_t>& dynamicSensorHandlesRemoved) override {
|
||||
sp<HalProxy> halProxy(mHalProxy.promote());
|
||||
if (halProxy != nullptr) {
|
||||
return halProxy->onDynamicSensorsDisconnected(dynamicSensorHandlesRemoved,
|
||||
mSubHalIndex);
|
||||
}
|
||||
return Return<void>();
|
||||
}
|
||||
|
||||
private:
|
||||
wp<HalProxy>& mHalProxy;
|
||||
int32_t mSubHalIndex;
|
||||
};
|
||||
ScopedWakelock::ScopedWakelock() {
|
||||
// TODO: Implement
|
||||
}
|
||||
|
||||
HalProxy::HalProxy() {
|
||||
const char* kMultiHalConfigFile = "/vendor/etc/sensors/hals.conf";
|
||||
initializeSubHalListFromConfigFile(kMultiHalConfigFile);
|
||||
initializeSensorList();
|
||||
initializeSubHalCallbacksAndSensorList();
|
||||
}
|
||||
|
||||
HalProxy::HalProxy(std::vector<ISensorsSubHal*>& subHalList) : mSubHalList(subHalList) {
|
||||
initializeSensorList();
|
||||
initializeSubHalCallbacksAndSensorList();
|
||||
}
|
||||
|
||||
HalProxy::~HalProxy() {
|
||||
@@ -84,7 +56,11 @@ HalProxy::~HalProxy() {
|
||||
}
|
||||
|
||||
Return<void> HalProxy::getSensorsList(getSensorsList_cb _hidl_cb) {
|
||||
_hidl_cb(mSensorList);
|
||||
std::vector<SensorInfo> sensors;
|
||||
for (const auto& iter : mSensors) {
|
||||
sensors.push_back(iter.second);
|
||||
}
|
||||
_hidl_cb(sensors);
|
||||
return Void();
|
||||
}
|
||||
|
||||
@@ -148,6 +124,17 @@ Return<Result> HalProxy::initialize(
|
||||
|
||||
// TODO: start threads to read wake locks and process events from sub HALs.
|
||||
|
||||
for (size_t i = 0; i < mSubHalList.size(); i++) {
|
||||
auto subHal = mSubHalList[i];
|
||||
const auto& subHalCallback = mSubHalCallbacks[i];
|
||||
Result currRes = subHal->initialize(subHalCallback);
|
||||
if (currRes != Result::OK) {
|
||||
result = currRes;
|
||||
ALOGE("Subhal '%s' failed to initialize.", subHal->getName().c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -239,6 +226,13 @@ void HalProxy::initializeSubHalListFromConfigFile(const char* configFileName) {
|
||||
}
|
||||
}
|
||||
|
||||
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];
|
||||
@@ -250,7 +244,7 @@ void HalProxy::initializeSensorList() {
|
||||
ALOGV("Loaded sensor: %s", sensor.name.c_str());
|
||||
sensor.sensorHandle |= (subHalIndex << 24);
|
||||
setDirectChannelFlags(&sensor, subHal);
|
||||
mSensorList.push_back(sensor);
|
||||
mSensors[sensor.sensorHandle] = sensor;
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -260,6 +254,29 @@ void HalProxy::initializeSensorList() {
|
||||
}
|
||||
}
|
||||
|
||||
void HalProxy::initializeSubHalCallbacksAndSensorList() {
|
||||
initializeSubHalCallbacks();
|
||||
initializeSensorList();
|
||||
}
|
||||
|
||||
void HalProxy::postEventsToMessageQueue(const std::vector<Event>& events) {
|
||||
std::lock_guard<std::mutex> lock(mEventQueueMutex);
|
||||
size_t numToWrite = std::min(events.size(), mEventQueue->availableToWrite());
|
||||
if (numToWrite > 0) {
|
||||
if (mEventQueue->write(events.data(), numToWrite)) {
|
||||
// TODO: While loop if mEventQueue->avaiableToWrite > 0 to possibly fit in more writes
|
||||
// immediately
|
||||
mEventQueueFlag->wake(static_cast<uint32_t>(EventQueueFlagBits::READ_AND_PROCESS));
|
||||
} else {
|
||||
numToWrite = 0;
|
||||
}
|
||||
}
|
||||
if (numToWrite < events.size()) {
|
||||
// TODO: Post from events[numToWrite -> end] to background events queue
|
||||
// Signal background thread
|
||||
}
|
||||
}
|
||||
|
||||
void HalProxy::setDirectChannelFlags(SensorInfo* sensorInfo, ISensorsSubHal* subHal) {
|
||||
bool sensorSupportsDirectChannel =
|
||||
(sensorInfo->flags & (V1_0::SensorFlagBits::MASK_DIRECT_REPORT |
|
||||
@@ -282,6 +299,50 @@ uint32_t HalProxy::clearSubHalIndex(uint32_t sensorHandle) {
|
||||
return sensorHandle & (~kSensorHandleSubHalIndexMask);
|
||||
}
|
||||
|
||||
void HalProxyCallback::postEvents(const std::vector<Event>& events, ScopedWakelock wakelock) {
|
||||
(void)wakelock;
|
||||
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 %zu.",
|
||||
mSubHalIndex);
|
||||
} else {
|
||||
ALOG_ASSERT(!wakelock.isLocked(),
|
||||
"No Wakeup events posted but wakelock locked for subhal"
|
||||
" w/ index %zu.",
|
||||
mSubHalIndex);
|
||||
}
|
||||
|
||||
mHalProxy->postEventsToMessageQueue(processedEvents);
|
||||
}
|
||||
|
||||
ScopedWakelock HalProxyCallback::createScopedWakelock(bool lock) {
|
||||
ScopedWakelock wakelock;
|
||||
wakelock.mLocked = lock;
|
||||
return wakelock;
|
||||
}
|
||||
|
||||
std::vector<Event> HalProxyCallback::processEvents(const std::vector<Event>& events,
|
||||
size_t* numWakeupEvents) const {
|
||||
std::vector<Event> eventsOut;
|
||||
*numWakeupEvents = 0;
|
||||
for (Event event : events) {
|
||||
event.sensorHandle = setSubHalIndex(event.sensorHandle);
|
||||
eventsOut.push_back(event);
|
||||
if ((mHalProxy->getSensorInfo(event.sensorHandle).flags & V1_0::SensorFlagBits::WAKE_UP) !=
|
||||
0) {
|
||||
(*numWakeupEvents)++;
|
||||
}
|
||||
}
|
||||
return eventsOut;
|
||||
}
|
||||
|
||||
uint32_t HalProxyCallback::setSubHalIndex(uint32_t sensorHandle) const {
|
||||
return sensorHandle | mSubHalIndex << 24;
|
||||
}
|
||||
|
||||
} // namespace implementation
|
||||
} // namespace V2_0
|
||||
} // namespace sensors
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
#include <hidl/MQDescriptor.h>
|
||||
#include <hidl/Status.h>
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace android {
|
||||
namespace hardware {
|
||||
namespace sensors {
|
||||
@@ -45,7 +47,9 @@ class HalProxy : public ISensors {
|
||||
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 @@ class HalProxy : public ISensors {
|
||||
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 @@ class HalProxy : public ISensors {
|
||||
*/
|
||||
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 @@ class HalProxy : public ISensors {
|
||||
//! 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;
|
||||
|
||||
@@ -144,12 +174,22 @@ class HalProxy : public ISensors {
|
||||
*/
|
||||
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
|
||||
@@ -179,6 +219,45 @@ 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
|
||||
*/
|
||||
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
|
||||
|
||||
@@ -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 sensors {
|
||||
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 @@ class ScopedWakelock {
|
||||
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 @@ class ISensorsSubHal : public ISensors {
|
||||
/**
|
||||
* 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
|
||||
|
||||
@@ -16,18 +16,29 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <android/hardware/sensors/2.0/types.h>
|
||||
#include <fmq/MessageQueue.h>
|
||||
|
||||
#include "HalProxy.h"
|
||||
#include "SensorsSubHal.h"
|
||||
#include "SubHal.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#undef LOG_TAG
|
||||
#define LOG_TAG "HalProxy_test"
|
||||
|
||||
namespace {
|
||||
|
||||
using ::android::hardware::hidl_vec;
|
||||
using ::android::hardware::MessageQueue;
|
||||
using ::android::hardware::Return;
|
||||
using ::android::hardware::sensors::V1_0::EventPayload;
|
||||
using ::android::hardware::sensors::V1_0::SensorFlagBits;
|
||||
using ::android::hardware::sensors::V1_0::SensorInfo;
|
||||
using ::android::hardware::sensors::V1_0::SensorType;
|
||||
using ::android::hardware::sensors::V2_0::ISensorsCallback;
|
||||
using ::android::hardware::sensors::V2_0::implementation::HalProxy;
|
||||
using ::android::hardware::sensors::V2_0::implementation::HalProxyCallback;
|
||||
using ::android::hardware::sensors::V2_0::subhal::implementation::AllSensorsSubHal;
|
||||
using ::android::hardware::sensors::V2_0::subhal::implementation::
|
||||
AllSupportDirectChannelSensorsSubHal;
|
||||
@@ -36,10 +47,28 @@ using ::android::hardware::sensors::V2_0::subhal::implementation::
|
||||
DoesNotSupportDirectChannelSensorsSubHal;
|
||||
using ::android::hardware::sensors::V2_0::subhal::implementation::OnChangeSensorsSubHal;
|
||||
using ::android::hardware::sensors::V2_0::subhal::implementation::SensorsSubHal;
|
||||
|
||||
using ::android::hardware::sensors::V2_0::subhal::implementation::
|
||||
SetOperationModeFailingSensorsSubHal;
|
||||
|
||||
using EventMessageQueue = MessageQueue<Event, ::android::hardware::kSynchronizedReadWrite>;
|
||||
using WakeupMessageQueue = MessageQueue<uint32_t, ::android::hardware::kSynchronizedReadWrite>;
|
||||
|
||||
// The barebones sensors callback class passed into halproxy initialize calls
|
||||
class SensorsCallback : public ISensorsCallback {
|
||||
public:
|
||||
Return<void> onDynamicSensorsConnected(
|
||||
const hidl_vec<SensorInfo>& /*dynamicSensorsAdded*/) override {
|
||||
// Nothing yet
|
||||
return Return<void>();
|
||||
}
|
||||
|
||||
Return<void> onDynamicSensorsDisconnected(
|
||||
const hidl_vec<int32_t>& /*dynamicSensorHandlesRemoved*/) override {
|
||||
// Nothing yet
|
||||
return Return<void>();
|
||||
}
|
||||
};
|
||||
|
||||
// Helper declarations follow
|
||||
|
||||
/**
|
||||
@@ -65,6 +94,22 @@ void testSensorsListFromProxyAndSubHal(const std::vector<SensorInfo>& proxySenso
|
||||
void testSensorsListForOneDirectChannelEnabledSubHal(const std::vector<SensorInfo>& sensorsList,
|
||||
size_t enabledSubHalIndex);
|
||||
|
||||
/**
|
||||
* Construct and return a HIDL Event type thats sensorHandle refers to a proximity sensor
|
||||
* which is a wakeup type sensor.
|
||||
*
|
||||
* @ return A proximity event.
|
||||
*/
|
||||
Event makeProximityEvent();
|
||||
|
||||
/**
|
||||
* Construct and return a HIDL Event type thats sensorHandle refers to a proximity sensor
|
||||
* which is a wakeup type sensor.
|
||||
*
|
||||
* @ return A proximity event.
|
||||
*/
|
||||
Event makeAccelerometerEvent();
|
||||
|
||||
// Tests follow
|
||||
TEST(HalProxyTest, GetSensorsListOneSubHalTest) {
|
||||
AllSensorsSubHal subHal;
|
||||
@@ -156,6 +201,86 @@ TEST(HalProxyTest, InitDirectChannelThreeSubHalsUnitTest) {
|
||||
});
|
||||
}
|
||||
|
||||
TEST(HalProxyTest, PostSingleNonWakeupEvent) {
|
||||
constexpr size_t kQueueSize = 5;
|
||||
AllSensorsSubHal subHal;
|
||||
std::vector<ISensorsSubHal*> subHals{&subHal};
|
||||
HalProxy proxy(subHals);
|
||||
std::unique_ptr<EventMessageQueue> eventQueue =
|
||||
std::make_unique<EventMessageQueue>(kQueueSize, true);
|
||||
std::unique_ptr<WakeupMessageQueue> wakeLockQueue =
|
||||
std::make_unique<WakeupMessageQueue>(kQueueSize, true);
|
||||
::android::sp<ISensorsCallback> callback = new SensorsCallback();
|
||||
proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
|
||||
|
||||
std::vector<Event> events{makeAccelerometerEvent()};
|
||||
subHal.postEvents(events, false /* wakeup */);
|
||||
|
||||
EXPECT_EQ(eventQueue->availableToRead(), 1);
|
||||
}
|
||||
|
||||
TEST(HalProxyTest, PostMultipleNonWakeupEvent) {
|
||||
constexpr size_t kQueueSize = 5;
|
||||
constexpr size_t kNumEvents = 3;
|
||||
AllSensorsSubHal subHal;
|
||||
std::vector<ISensorsSubHal*> subHals{&subHal};
|
||||
HalProxy proxy(subHals);
|
||||
std::unique_ptr<EventMessageQueue> eventQueue =
|
||||
std::make_unique<EventMessageQueue>(kQueueSize, true);
|
||||
std::unique_ptr<WakeupMessageQueue> wakeLockQueue =
|
||||
std::make_unique<WakeupMessageQueue>(kQueueSize, true);
|
||||
::android::sp<ISensorsCallback> callback = new SensorsCallback();
|
||||
proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
|
||||
|
||||
std::vector<Event> events;
|
||||
for (size_t i = 0; i < kNumEvents; i++) {
|
||||
events.push_back(makeAccelerometerEvent());
|
||||
}
|
||||
subHal.postEvents(events, false /* wakeup */);
|
||||
|
||||
EXPECT_EQ(eventQueue->availableToRead(), kNumEvents);
|
||||
}
|
||||
|
||||
TEST(HalProxyTest, PostSingleWakeupEvent) {
|
||||
constexpr size_t kQueueSize = 5;
|
||||
AllSensorsSubHal subHal;
|
||||
std::vector<ISensorsSubHal*> subHals{&subHal};
|
||||
HalProxy proxy(subHals);
|
||||
std::unique_ptr<EventMessageQueue> eventQueue =
|
||||
std::make_unique<EventMessageQueue>(kQueueSize, true);
|
||||
std::unique_ptr<WakeupMessageQueue> wakeLockQueue =
|
||||
std::make_unique<WakeupMessageQueue>(kQueueSize, true);
|
||||
::android::sp<ISensorsCallback> callback = new SensorsCallback();
|
||||
proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
|
||||
|
||||
std::vector<Event> events{makeProximityEvent()};
|
||||
subHal.postEvents(events, true /* wakeup */);
|
||||
|
||||
EXPECT_EQ(eventQueue->availableToRead(), 1);
|
||||
}
|
||||
|
||||
TEST(HalProxyTest, PostMultipleWakeupEvents) {
|
||||
constexpr size_t kQueueSize = 5;
|
||||
constexpr size_t kNumEvents = 3;
|
||||
AllSensorsSubHal subHal;
|
||||
std::vector<ISensorsSubHal*> subHals{&subHal};
|
||||
HalProxy proxy(subHals);
|
||||
std::unique_ptr<EventMessageQueue> eventQueue =
|
||||
std::make_unique<EventMessageQueue>(kQueueSize, true);
|
||||
std::unique_ptr<WakeupMessageQueue> wakeLockQueue =
|
||||
std::make_unique<WakeupMessageQueue>(kQueueSize, true);
|
||||
::android::sp<ISensorsCallback> callback = new SensorsCallback();
|
||||
proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
|
||||
|
||||
std::vector<Event> events;
|
||||
for (size_t i = 0; i < kNumEvents; i++) {
|
||||
events.push_back(makeProximityEvent());
|
||||
}
|
||||
subHal.postEvents(events, true /* wakeup */);
|
||||
|
||||
EXPECT_EQ(eventQueue->availableToRead(), kNumEvents);
|
||||
}
|
||||
|
||||
// Helper implementations follow
|
||||
void testSensorsListFromProxyAndSubHal(const std::vector<SensorInfo>& proxySensorsList,
|
||||
const std::vector<SensorInfo>& subHalSensorsList) {
|
||||
@@ -187,4 +312,24 @@ void testSensorsListForOneDirectChannelEnabledSubHal(const std::vector<SensorInf
|
||||
}
|
||||
}
|
||||
|
||||
Event makeProximityEvent() {
|
||||
Event event;
|
||||
event.timestamp = 0xFF00FF00;
|
||||
// This is the sensorhandle of proximity, which is wakeup type
|
||||
event.sensorHandle = 0x00000008;
|
||||
event.sensorType = SensorType::PROXIMITY;
|
||||
event.u = EventPayload();
|
||||
return event;
|
||||
}
|
||||
|
||||
Event makeAccelerometerEvent() {
|
||||
Event event;
|
||||
event.timestamp = 0xFF00FF00;
|
||||
// This is the sensorhandle of proximity, which is wakeup type
|
||||
event.sensorHandle = 0x00000001;
|
||||
event.sensorType = SensorType::ACCELEROMETER;
|
||||
event.u = EventPayload();
|
||||
return event;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user