Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6c49e6f7 authored by Brian Stack's avatar Brian Stack
Browse files

Implement ISensorsCallback

Implement the ISensorsCallback interface in the SensorDevice. This
interface is responsible for handling callbacks from the Sensors HAL,
such as when a dynamic sensor is connected or disconnected.

Bug: 111070257
Test: Compile, configured system to use sensors@2.0 and ensured that
      a_sns_test received events

Change-Id: I180e05db29e0e1d008fb661e6cf6e3b552a1b0b6
parent a28e921d
Loading
Loading
Loading
Loading
+34 −15
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ using namespace android::hardware::sensors::V1_0;
using namespace android::hardware::sensors::V1_0::implementation;
using android::hardware::sensors::V2_0::EventQueueFlagBits;
using android::hardware::hidl_vec;
using android::hardware::Return;
using android::SensorDeviceUtils::HidlServiceRegistrationWaiter;

namespace android {
@@ -162,13 +163,14 @@ SensorDevice::HalConnectionStatus SensorDevice::connectHidlServiceV2_0() {
        CHECK(mSensors != nullptr && mEventQueue != nullptr &&
                mWakeLockQueue != nullptr && mEventQueueFlag != nullptr);

        status_t status = StatusFromResult(checkReturn(mSensors->initializeMessageQueues(
        status_t status = StatusFromResult(checkReturn(mSensors->initialize(
                *mEventQueue->getDesc(),
                *mWakeLockQueue->getDesc())));
                *mWakeLockQueue->getDesc(),
                this)));

        if (status != NO_ERROR) {
            connectionStatus = HalConnectionStatus::FAILED_TO_CONNECT;
            ALOGE("Failed to initialize message queues (%s)", strerror(-status));
            ALOGE("Failed to initialize Sensors HAL (%s)", strerror(-status));
        } else {
            connectionStatus = HalConnectionStatus::CONNECTED;
        }
@@ -332,6 +334,33 @@ ssize_t SensorDevice::pollFmq(sensors_event_t* buffer, size_t maxNumEventsToRead
    return eventsRead;
}

Return<void> SensorDevice::onDynamicSensorsConnected(
        const hidl_vec<SensorInfo> &dynamicSensorsAdded) {
    // Allocate a sensor_t structure for each dynamic sensor added and insert
    // it into the dictionary of connected dynamic sensors keyed by handle.
    for (size_t i = 0; i < dynamicSensorsAdded.size(); ++i) {
        const SensorInfo &info = dynamicSensorsAdded[i];

        auto it = mConnectedDynamicSensors.find(info.sensorHandle);
        CHECK(it == mConnectedDynamicSensors.end());

        sensor_t *sensor = new sensor_t();
        convertToSensor(info, sensor);

        mConnectedDynamicSensors.insert(
                std::make_pair(sensor->handle, sensor));
    }

    return Return<void>();
}

Return<void> SensorDevice::onDynamicSensorsDisconnected(
        const hidl_vec<int32_t> &dynamicSensorHandlesRemoved) {
    (void) dynamicSensorHandlesRemoved;
    // TODO: Currently dynamic sensors do not seem to be removed
    return Return<void>();
}

void SensorDevice::autoDisable(void *ident, int handle) {
    Mutex::Autolock _l(mLock);
    ssize_t activationIndex = mActivationCount.indexOfKey(handle);
@@ -767,19 +796,9 @@ void SensorDevice::convertToSensorEvents(
        const hidl_vec<Event> &src,
        const hidl_vec<SensorInfo> &dynamicSensorsAdded,
        sensors_event_t *dst) {
    // Allocate a sensor_t structure for each dynamic sensor added and insert
    // it into the dictionary of connected dynamic sensors keyed by handle.
    for (size_t i = 0; i < dynamicSensorsAdded.size(); ++i) {
        const SensorInfo &info = dynamicSensorsAdded[i];

        auto it = mConnectedDynamicSensors.find(info.sensorHandle);
        CHECK(it == mConnectedDynamicSensors.end());

        sensor_t *sensor = new sensor_t;
        convertToSensor(info, sensor);

        mConnectedDynamicSensors.insert(
                std::make_pair(sensor->handle, sensor));
    if (dynamicSensorsAdded.size() > 0) {
        onDynamicSensorsConnected(dynamicSensorsAdded);
    }

    for (size_t i = 0; i < src.size(); ++i) {
+9 −1
Original line number Diff line number Diff line
@@ -43,7 +43,9 @@ namespace android {

// ---------------------------------------------------------------------------

class SensorDevice : public Singleton<SensorDevice>, public SensorServiceUtil::Dumpable {
class SensorDevice : public Singleton<SensorDevice>,
                     public android::hardware::sensors::V2_0::ISensorsCallback,
                     public SensorServiceUtil::Dumpable {
public:
    class HidlTransportErrorLog {
     public:
@@ -102,6 +104,12 @@ public:
    status_t injectSensorData(const sensors_event_t *event);
    void notifyConnectionDestroyed(void *ident);

    using Result = ::android::hardware::sensors::V1_0::Result;
    hardware::Return<void> onDynamicSensorsConnected(
            const hardware::hidl_vec<hardware::sensors::V1_0::SensorInfo> &dynamicSensorsAdded) override;
    hardware::Return<void> onDynamicSensorsDisconnected(
            const hardware::hidl_vec<int32_t> &dynamicSensorHandlesRemoved) override;

    // Dumpable
    virtual std::string dump() const;
private:
+10 −6
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

#include "android/hardware/sensors/1.0/ISensors.h"
#include "android/hardware/sensors/2.0/ISensors.h"
#include "android/hardware/sensors/2.0/ISensorsCallback.h"

#include <utils/LightRefBase.h>

@@ -33,6 +34,7 @@ using ::android::hardware::sensors::V1_0::OperationMode;
using ::android::hardware::sensors::V1_0::RateLevel;
using ::android::hardware::sensors::V1_0::Result;
using ::android::hardware::sensors::V1_0::SharedMemInfo;
using ::android::hardware::sensors::V2_0::ISensorsCallback;

/*
 * The ISensorsWrapper interface includes all function from supported Sensors HAL versions. This
@@ -85,10 +87,12 @@ public:
        return Return<void>();
    }

    virtual Return<Result> initializeMessageQueues(const MQDescriptorSync<Event>& eventQueueDesc,
                                                   const MQDescriptorSync<uint32_t>& wakeLockDesc) {
    virtual Return<Result> initialize(const MQDescriptorSync<Event>& eventQueueDesc,
                                      const MQDescriptorSync<uint32_t>& wakeLockDesc,
                                      const ::android::sp<ISensorsCallback>& callback) {
        (void)eventQueueDesc;
        (void)wakeLockDesc;
        (void)callback;
        // TODO (b/111070257): Generate an assert-level error since this should never be called
        // directly
        return Result::INVALID_OPERATION;
@@ -177,10 +181,10 @@ public:
        return true;
    }

    Return<Result> initializeMessageQueues(
            const MQDescriptorSync<Event>& eventQueueDesc,
            const MQDescriptorSync<uint32_t>& wakeLockDesc) override {
        return mSensors->initializeMessageQueues(eventQueueDesc, wakeLockDesc);
    Return<Result> initialize(const MQDescriptorSync<Event>& eventQueueDesc,
                              const MQDescriptorSync<uint32_t>& wakeLockDesc,
                              const ::android::sp<ISensorsCallback>& callback) override {
        return mSensors->initialize(eventQueueDesc, wakeLockDesc, callback);
    }
};