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

Commit c7196387 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Update sensors framework for HAL 2.1"

parents daaac49d e38a1414
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -33,6 +33,10 @@ cc_library_shared {
        "-fvisibility=hidden"
    ],

    header_libs: [
        "android.hardware.sensors@2.X-shared-utils",
    ],

    shared_libs: [
        "libcutils",
        "libhardware",
@@ -49,9 +53,12 @@ cc_library_shared {
        "libfmq",
        "android.hardware.sensors@1.0",
        "android.hardware.sensors@2.0",
        "android.hardware.sensors@2.1",
    ],

    static_libs: ["android.hardware.sensors@1.0-convert"],
    static_libs: [
        "android.hardware.sensors@1.0-convert",
    ],

    generated_headers: ["framework-cppstream-protos"],

+78 −43
Original line number Diff line number Diff line
@@ -16,8 +16,10 @@

#include "SensorDevice.h"

#include "android/hardware/sensors/2.0/ISensorsCallback.h"
#include "android/hardware/sensors/2.0/types.h"
#include "android/hardware/sensors/2.1/ISensorsCallback.h"
#include "android/hardware/sensors/2.1/types.h"
#include "convertV2_1.h"
#include "SensorService.h"

#include <android-base/logging.h>
@@ -35,9 +37,15 @@
using namespace android::hardware::sensors;
using namespace android::hardware::sensors::V1_0;
using namespace android::hardware::sensors::V1_0::implementation;
using android::hardware::sensors::V2_0::ISensorsCallback;
using android::hardware::sensors::V2_0::EventQueueFlagBits;
using android::hardware::sensors::V2_0::WakeLockQueueFlagBits;
using android::hardware::sensors::V2_1::ISensorsCallback;
using android::hardware::sensors::V2_1::implementation::convertToOldSensorInfo;
using android::hardware::sensors::V2_1::implementation::convertToNewSensorInfos;
using android::hardware::sensors::V2_1::implementation::convertToNewEvents;
using android::hardware::sensors::V2_1::implementation::ISensorsWrapperV1_0;
using android::hardware::sensors::V2_1::implementation::ISensorsWrapperV2_0;
using android::hardware::sensors::V2_1::implementation::ISensorsWrapperV2_1;
using android::hardware::hidl_vec;
using android::hardware::Return;
using android::SensorDeviceUtils::HidlServiceRegistrationWaiter;
@@ -87,11 +95,19 @@ void SensorsHalDeathReceivier::serviceDied(

struct SensorsCallback : public ISensorsCallback {
    using Result = ::android::hardware::sensors::V1_0::Result;
    Return<void> onDynamicSensorsConnected(
    using SensorInfo = ::android::hardware::sensors::V2_1::SensorInfo;

    Return<void> onDynamicSensorsConnected_2_1(
            const hidl_vec<SensorInfo> &dynamicSensorsAdded) override {
        return SensorDevice::getInstance().onDynamicSensorsConnected(dynamicSensorsAdded);
    }

    Return<void> onDynamicSensorsConnected(
            const hidl_vec<V1_0::SensorInfo> &dynamicSensorsAdded) override {
        return SensorDevice::getInstance().onDynamicSensorsConnected(
                convertToNewSensorInfos(dynamicSensorsAdded));
    }

    Return<void> onDynamicSensorsDisconnected(
            const hidl_vec<int32_t> &dynamicSensorHandlesRemoved) override {
        return SensorDevice::getInstance().onDynamicSensorsDisconnected(
@@ -126,7 +142,7 @@ void SensorDevice::initializeSensorList() {
                Info model;
                for (size_t i=0 ; i < count; i++) {
                    sensor_t sensor;
                    convertToSensor(list[i], &sensor);
                    convertToSensor(convertToOldSensorInfo(list[i]), &sensor);
                    // Sanity check and clamp power if it is 0 (or close)
                    if (sensor.power < minPowerMa) {
                        ALOGI("Reported power %f not deemed sane, clamping to %f",
@@ -160,7 +176,11 @@ SensorDevice::~SensorDevice() {
}

bool SensorDevice::connectHidlService() {
    HalConnectionStatus status = connectHidlServiceV2_0();
    HalConnectionStatus status = connectHidlServiceV2_1();
    if (status == HalConnectionStatus::DOES_NOT_EXIST) {
        status = connectHidlServiceV2_0();
    }

    if (status == HalConnectionStatus::DOES_NOT_EXIST) {
        status = connectHidlServiceV1_0();
    }
@@ -180,7 +200,7 @@ SensorDevice::HalConnectionStatus SensorDevice::connectHidlServiceV1_0() {
            break;
        }

        mSensors = new SensorServiceUtil::SensorsWrapperV1_0(sensors);
        mSensors = new ISensorsWrapperV1_0(sensors);
        mRestartWaiter->reset();
        // Poke ISensor service. If it has lingering connection from previous generation of
        // system server, it will kill itself. There is no intention to handle the poll result,
@@ -208,29 +228,45 @@ SensorDevice::HalConnectionStatus SensorDevice::connectHidlServiceV2_0() {
    if (sensors == nullptr) {
        connectionStatus = HalConnectionStatus::DOES_NOT_EXIST;
    } else {
        mSensors = new SensorServiceUtil::SensorsWrapperV2_0(sensors);
        mSensors = new ISensorsWrapperV2_0(sensors);
        connectionStatus = initializeHidlServiceV2_X();
    }

        mEventQueue = std::make_unique<EventMessageQueue>(
                SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT,
                true /* configureEventFlagWord */);
    return connectionStatus;
}

SensorDevice::HalConnectionStatus SensorDevice::connectHidlServiceV2_1() {
    HalConnectionStatus connectionStatus = HalConnectionStatus::UNKNOWN;
    sp<V2_1::ISensors> sensors = V2_1::ISensors::getService();

    if (sensors == nullptr) {
        connectionStatus = HalConnectionStatus::DOES_NOT_EXIST;
    } else {
        mSensors = new ISensorsWrapperV2_1(sensors);
        connectionStatus = initializeHidlServiceV2_X();
    }

    return connectionStatus;
}

SensorDevice::HalConnectionStatus SensorDevice::initializeHidlServiceV2_X() {
    HalConnectionStatus connectionStatus = HalConnectionStatus::UNKNOWN;

    mWakeLockQueue = std::make_unique<WakeLockQueue>(
            SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT,
            true /* configureEventFlagWord */);

    hardware::EventFlag::deleteEventFlag(&mEventQueueFlag);
        hardware::EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag);
    hardware::EventFlag::createEventFlag(mSensors->getEventQueue()->getEventFlagWord(), &mEventQueueFlag);

    hardware::EventFlag::deleteEventFlag(&mWakeLockQueueFlag);
    hardware::EventFlag::createEventFlag(mWakeLockQueue->getEventFlagWord(),
                                            &mWakeLockQueueFlag);

        CHECK(mSensors != nullptr && mEventQueue != nullptr &&
                mWakeLockQueue != nullptr && mEventQueueFlag != nullptr &&
                mWakeLockQueueFlag != nullptr);
    CHECK(mSensors != nullptr && mWakeLockQueue != nullptr &&
            mEventQueueFlag != nullptr && mWakeLockQueueFlag != nullptr);

    status_t status = checkReturnAndGetStatus(mSensors->initialize(
                *mEventQueue->getDesc(),
            *mWakeLockQueue->getDesc(),
            new SensorsCallback()));

@@ -240,8 +276,7 @@ SensorDevice::HalConnectionStatus SensorDevice::connectHidlServiceV2_0() {
    } else {
        connectionStatus = HalConnectionStatus::CONNECTED;
        mSensorsHalDeathReceiver = new SensorsHalDeathReceivier();
            sensors->linkToDeath(mSensorsHalDeathReceiver, 0 /* cookie */);
        }
        mSensors->linkToDeath(mSensorsHalDeathReceiver, 0 /* cookie */);
    }

    return connectionStatus;
@@ -473,7 +508,8 @@ ssize_t SensorDevice::pollHal(sensors_event_t* buffer, size_t count) {
                    const auto &events,
                    const auto &dynamicSensorsAdded) {
                    if (result == Result::OK) {
                        convertToSensorEvents(events, dynamicSensorsAdded, buffer);
                        convertToSensorEvents(convertToNewEvents(events),
                                convertToNewSensorInfos(dynamicSensorsAdded), buffer);
                        err = (ssize_t)events.size();
                    } else {
                        err = statusFromResult(result);
@@ -507,7 +543,7 @@ ssize_t SensorDevice::pollHal(sensors_event_t* buffer, size_t count) {

ssize_t SensorDevice::pollFmq(sensors_event_t* buffer, size_t maxNumEventsToRead) {
    ssize_t eventsRead = 0;
    size_t availableEvents = mEventQueue->availableToRead();
    size_t availableEvents = mSensors->getEventQueue()->availableToRead();

    if (availableEvents == 0) {
        uint32_t eventFlagState = 0;
@@ -518,7 +554,7 @@ ssize_t SensorDevice::pollFmq(sensors_event_t* buffer, size_t maxNumEventsToRead
        // additional latency in delivering events to applications.
        mEventQueueFlag->wait(asBaseType(EventQueueFlagBits::READ_AND_PROCESS) |
                              asBaseType(INTERNAL_WAKE), &eventFlagState);
        availableEvents = mEventQueue->availableToRead();
        availableEvents = mSensors->getEventQueue()->availableToRead();

        if ((eventFlagState & asBaseType(INTERNAL_WAKE)) && mReconnecting) {
            ALOGD("Event FMQ internal wake, returning from poll with no events");
@@ -528,7 +564,7 @@ ssize_t SensorDevice::pollFmq(sensors_event_t* buffer, size_t maxNumEventsToRead

    size_t eventsToRead = std::min({availableEvents, maxNumEventsToRead, mEventBuffer.size()});
    if (eventsToRead > 0) {
        if (mEventQueue->read(mEventBuffer.data(), eventsToRead)) {
        if (mSensors->getEventQueue()->read(mEventBuffer.data(), eventsToRead)) {
            // Notify the Sensors HAL that sensor events have been read. This is required to support
            // the use of writeBlocking by the Sensors HAL.
            mEventQueueFlag->wake(asBaseType(EventQueueFlagBits::EVENTS_READ));
@@ -557,7 +593,7 @@ Return<void> SensorDevice::onDynamicSensorsConnected(
        CHECK(it == mConnectedDynamicSensors.end());

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

        mConnectedDynamicSensors.insert(
                std::make_pair(sensor->handle, sensor));
@@ -858,7 +894,7 @@ status_t SensorDevice::injectSensorData(
            injected_sensor_event->data[5]);

    Event ev;
    convertFromSensorEvent(*injected_sensor_event, &ev);
    V2_1::implementation::convertFromSensorEvent(*injected_sensor_event, &ev);

    return checkReturnAndGetStatus(mSensors->injectSensorData(ev));
}
@@ -1021,10 +1057,9 @@ bool SensorDevice::isDirectReportSupported() const {

void SensorDevice::convertToSensorEvent(
        const Event &src, sensors_event_t *dst) {
    ::android::hardware::sensors::V1_0::implementation::convertToSensorEvent(
            src, dst);
    V2_1::implementation::convertToSensorEvent(src, dst);

    if (src.sensorType == SensorType::DYNAMIC_SENSOR_META) {
    if (src.sensorType == V2_1::SensorType::DYNAMIC_SENSOR_META) {
        const DynamicSensorInfo &dyn = src.u.dynamic;

        dst->dynamic_sensor_meta.connected = dyn.connected;
@@ -1052,7 +1087,7 @@ void SensorDevice::convertToSensorEvents(
    }

    for (size_t i = 0; i < src.size(); ++i) {
        convertToSensorEvent(src[i], &dst[i]);
        V2_1::implementation::convertToSensorEvent(src[i], &dst[i]);
    }
}

+7 −7
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@

#include "SensorDeviceUtils.h"
#include "SensorServiceUtils.h"
#include "SensorsWrapper.h"
#include "ISensorsWrapper.h"

#include <fmq/MessageQueue.h>
#include <sensor/SensorEventQueue.h>
@@ -112,7 +112,7 @@ public:

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

@@ -128,7 +128,7 @@ public:
private:
    friend class Singleton<SensorDevice>;

    sp<SensorServiceUtil::ISensorsWrapper> mSensors;
    sp<::android::hardware::sensors::V2_1::implementation::ISensorsWrapperBase> mSensors;
    Vector<sensor_t> mSensorList;
    std::unordered_map<int32_t, sensor_t*> mConnectedDynamicSensors;

@@ -205,6 +205,8 @@ private:
    };
    HalConnectionStatus connectHidlServiceV1_0();
    HalConnectionStatus connectHidlServiceV2_0();
    HalConnectionStatus connectHidlServiceV2_1();
    HalConnectionStatus initializeHidlServiceV2_X();

    ssize_t pollHal(sensors_event_t* buffer, size_t count);
    ssize_t pollFmq(sensors_event_t* buffer, size_t count);
@@ -226,8 +228,8 @@ private:
    bool isClientDisabled(void* ident);
    bool isClientDisabledLocked(void* ident);

    using Event = hardware::sensors::V1_0::Event;
    using SensorInfo = hardware::sensors::V1_0::SensorInfo;
    using Event = hardware::sensors::V2_1::Event;
    using SensorInfo = hardware::sensors::V2_1::SensorInfo;

    void convertToSensorEvent(const Event &src, sensors_event_t *dst);

@@ -238,9 +240,7 @@ private:

    bool mIsDirectReportSupported;

    typedef hardware::MessageQueue<Event, hardware::kSynchronizedReadWrite> EventMessageQueue;
    typedef hardware::MessageQueue<uint32_t, hardware::kSynchronizedReadWrite> WakeLockQueue;
    std::unique_ptr<EventMessageQueue> mEventQueue;
    std::unique_ptr<WakeLockQueue> mWakeLockQueue;

    hardware::EventFlag* mEventQueueFlag;
+0 −194
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.
 */

#ifndef ANDROID_SENSORS_WRAPPER_H
#define ANDROID_SENSORS_WRAPPER_H

#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>

namespace android {
namespace SensorServiceUtil {

using ::android::hardware::MQDescriptorSync;
using ::android::hardware::Return;
using ::android::hardware::sensors::V1_0::Event;
using ::android::hardware::sensors::V1_0::ISensors;
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
 * allows for the SensorDevice to use the ISensorsWrapper interface to interact with the Sensors
 * HAL regardless of the current version of the Sensors HAL that is loaded. Each concrete
 * instantiation of ISensorsWrapper must correspond to a specific Sensors HAL version. This design
 * is beneficial because only the functions that change between Sensors HAL versions must be newly
 * newly implemented, any previously implemented function that does not change may remain the same.
 *
 * Functions that exist across all versions of the Sensors HAL should be implemented as pure
 * virtual functions which forces the concrete instantiations to implement the functions.
 *
 * Functions that do not exist across all versions of the Sensors HAL should include a default
 * implementation that generates an error if called. The default implementation should never
 * be called and must be overridden by Sensors HAL versions that support the function.
 */
class ISensorsWrapper : public VirtualLightRefBase {
public:
    virtual bool supportsPolling() const = 0;

    virtual bool supportsMessageQueues() const = 0;

    virtual Return<void> getSensorsList(ISensors::getSensorsList_cb _hidl_cb) = 0;

    virtual Return<Result> setOperationMode(OperationMode mode) = 0;

    virtual Return<Result> activate(int32_t sensorHandle, bool enabled) = 0;

    virtual Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
                                 int64_t maxReportLatencyNs) = 0;

    virtual Return<Result> flush(int32_t sensorHandle) = 0;

    virtual Return<Result> injectSensorData(const Event& event) = 0;

    virtual Return<void> registerDirectChannel(const SharedMemInfo& mem,
                                               ISensors::registerDirectChannel_cb _hidl_cb) = 0;

    virtual Return<Result> unregisterDirectChannel(int32_t channelHandle) = 0;

    virtual Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle,
                                            RateLevel rate,
                                            ISensors::configDirectReport_cb _hidl_cb) = 0;

    virtual Return<void> poll(int32_t maxCount, ISensors::poll_cb _hidl_cb) {
        (void)maxCount;
        (void)_hidl_cb;
        // TODO (b/111070257): Generate an assert-level error since this should never be called
        // directly
        return Return<void>();
    }

    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;
    }
};

template<typename T>
class SensorsWrapperBase : public ISensorsWrapper {
public:
    SensorsWrapperBase(sp<T> sensors) :
        mSensors(sensors) { };

    Return<void> getSensorsList(ISensors::getSensorsList_cb _hidl_cb) override {
        return mSensors->getSensorsList(_hidl_cb);
    }

    Return<Result> setOperationMode(OperationMode mode) override {
        return mSensors->setOperationMode(mode);
    }

    Return<Result> activate(int32_t sensorHandle, bool enabled) override {
        return mSensors->activate(sensorHandle, enabled);
    }

    Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
                         int64_t maxReportLatencyNs) override {
        return mSensors->batch(sensorHandle, samplingPeriodNs, maxReportLatencyNs);
    }

    Return<Result> flush(int32_t sensorHandle) override {
        return mSensors->flush(sensorHandle);
    }

    Return<Result> injectSensorData(const Event& event) override {
        return mSensors->injectSensorData(event);
    }

    Return<void> registerDirectChannel(const SharedMemInfo& mem,
                                       ISensors::registerDirectChannel_cb _hidl_cb) override {
        return mSensors->registerDirectChannel(mem, _hidl_cb);
    }

    Return<Result> unregisterDirectChannel(int32_t channelHandle) override {
        return mSensors->unregisterDirectChannel(channelHandle);
    }

    Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle,
                                    RateLevel rate,
                                    ISensors::configDirectReport_cb _hidl_cb) override {
        return mSensors->configDirectReport(sensorHandle, channelHandle, rate, _hidl_cb);
    }

protected:
    sp<T> mSensors;
};

class SensorsWrapperV1_0 : public SensorsWrapperBase<hardware::sensors::V1_0::ISensors> {
public:
    SensorsWrapperV1_0(sp<hardware::sensors::V1_0::ISensors> sensors) :
        SensorsWrapperBase(sensors) { };

    bool supportsPolling() const override {
        return true;
    }

    bool supportsMessageQueues() const override {
        return false;
    }

    Return<void> poll(int32_t maxCount,
                      hardware::sensors::V1_0::ISensors::poll_cb _hidl_cb) override {
        return mSensors->poll(maxCount, _hidl_cb);
    }
};

class SensorsWrapperV2_0 : public SensorsWrapperBase<hardware::sensors::V2_0::ISensors> {
public:
    SensorsWrapperV2_0(sp<hardware::sensors::V2_0::ISensors> sensors)
        : SensorsWrapperBase(sensors) { };

    bool supportsPolling() const override {
        return false;
    }

    bool supportsMessageQueues() const override {
        return true;
    }

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

}; // namespace SensorServiceUtil
}; // namespace android

#endif // ANDROID_SENSORS_WRAPPER_H