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

Commit d333511e authored by Steven Moreland's avatar Steven Moreland Committed by Ashutosh Joshi
Browse files

sensorservice: Remove ENABLE_TREBLE flag.

Only hidl hals should be built/referenced by the framework,
and ENABLE_TREBLE will be deprecated soon.

Bug: 32978887
Test: compiles/wip
Change-Id: Ibb32129e1f30ae5baa15e116d3b13cc72bf27395
parent 2c275922
Loading
Loading
Loading
Loading
+1 −13
Original line number Original line Diff line number Diff line
@@ -10,6 +10,7 @@ LOCAL_SRC_FILES:= \
    OrientationSensor.cpp \
    OrientationSensor.cpp \
    RecentEventLogger.cpp \
    RecentEventLogger.cpp \
    RotationVectorSensor.cpp \
    RotationVectorSensor.cpp \
    SensorDevice.cpp \
    SensorDirectConnection.cpp \
    SensorDirectConnection.cpp \
    SensorEventConnection.cpp \
    SensorEventConnection.cpp \
    SensorFusion.cpp \
    SensorFusion.cpp \
@@ -25,13 +26,6 @@ LOCAL_CFLAGS += -Wall -Werror -Wextra


LOCAL_CFLAGS += -fvisibility=hidden
LOCAL_CFLAGS += -fvisibility=hidden


ifeq ($(ENABLE_TREBLE), true)
LOCAL_SRC_FILES += SensorDeviceTreble.cpp
LOCAL_CFLAGS += -DENABLE_TREBLE=1
else
LOCAL_SRC_FILES += SensorDevice.cpp
endif

LOCAL_SHARED_LIBRARIES := \
LOCAL_SHARED_LIBRARIES := \
    libcutils \
    libcutils \
    libhardware \
    libhardware \
@@ -42,10 +36,6 @@ LOCAL_SHARED_LIBRARIES := \
    libui \
    libui \
    libgui \
    libgui \
    libcrypto \
    libcrypto \

ifeq ($(ENABLE_TREBLE), true)

LOCAL_SHARED_LIBRARIES += \
    libbase \
    libbase \
    libhidlbase \
    libhidlbase \
    libhidltransport \
    libhidltransport \
@@ -55,8 +45,6 @@ LOCAL_SHARED_LIBRARIES += \
LOCAL_STATIC_LIBRARIES := \
LOCAL_STATIC_LIBRARIES := \
    android.hardware.sensors@1.0-convert
    android.hardware.sensors@1.0-convert


endif  # ENABLE_TREBLE

LOCAL_MODULE:= libsensorservice
LOCAL_MODULE:= libsensorservice


include $(BUILD_SHARED_LIBRARY)
include $(BUILD_SHARED_LIBRARY)
+306 −201
Original line number Original line Diff line number Diff line
@@ -14,138 +14,167 @@
 * limitations under the License.
 * limitations under the License.
 */
 */


#include <inttypes.h>
#include <math.h>
#include <stdint.h>
#include <sys/types.h>

#include <android-base/logging.h>
#include <utils/Atomic.h>
#include <utils/Errors.h>
#include <utils/Singleton.h>


#include "SensorDevice.h"
#include "SensorDevice.h"
#include "SensorService.h"
#include "SensorService.h"


#include <sensors/convert.h>


#include <binder/BinderService.h>
using android::hardware::hidl_vec;
#include <binder/Parcel.h>
#include <binder/IServiceManager.h>
#include <cutils/ashmem.h>
#include <hardware/sensors.h>
#include <utils/Atomic.h>
#include <utils/Errors.h>
#include <utils/Singleton.h>


#include <inttypes.h>
using namespace android::hardware::sensors::V1_0;
#include <math.h>
using namespace android::hardware::sensors::V1_0::implementation;
#include <sys/mman.h>
#include <stdint.h>
#include <sys/types.h>
#include <sstream>
#include <unistd.h>


namespace android {
namespace android {
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------


ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)
ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)


SensorDevice::SensorDevice()
static status_t StatusFromResult(Result result) {
    :  mSensorDevice(0),
    switch (result) {
       mSensorModule(0) {
        case Result::OK:
    status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID,
            return OK;
            (hw_module_t const**)&mSensorModule);
        case Result::BAD_VALUE:
            return BAD_VALUE;
        case Result::PERMISSION_DENIED:
            return PERMISSION_DENIED;
        case Result::INVALID_OPERATION:
            return INVALID_OPERATION;
        case Result::NO_MEMORY:
            return NO_MEMORY;
    }
}


    ALOGE_IF(err, "couldn't load %s module (%s)",
SensorDevice::SensorDevice() {
            SENSORS_HARDWARE_MODULE_ID, strerror(-err));
    mSensors = ISensors::getService();


    if (mSensorModule) {
    if (mSensors == NULL) {
        err = sensors_open_1(&mSensorModule->common, &mSensorDevice);
        return;
    }


        ALOGE_IF(err, "couldn't open device for module %s (%s)",
    mSensors->getSensorsList(
                SENSORS_HARDWARE_MODULE_ID, strerror(-err));
            [&](const auto &list) {
                const size_t count = list.size();


        if (mSensorDevice) {
                mActivationCount.setCapacity(count);
                Info model;
                for (size_t i=0 ; i < count; i++) {
                    sensor_t sensor;
                    convertToSensor(list[i], &sensor);
                    mSensorList.push_back(sensor);


            sensor_t const* list;
                    mActivationCount.add(list[i].sensorHandle, model);
            ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);


            if (mSensorDevice->common.version < SENSORS_DEVICE_API_VERSION_1_3) {
                    mSensors->activate(list[i].sensorHandle, 0 /* enabled */);
                ALOGE(">>>> WARNING <<< Upgrade sensor HAL to version 1_3, ignoring sensors reported by this device");
                count = 0;
                }
                }
            });


            mActivationCount.setCapacity(count);
    mIsDirectReportSupported =
            Info model;
           (mSensors->unregisterDirectChannel(-1) != Result::INVALID_OPERATION);
            for (size_t i=0 ; i<size_t(count) ; i++) {
                mActivationCount.add(list[i].handle, model);
                mSensorDevice->activate(
                        reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
                        list[i].handle, 0);
            }
        }
    }
}
}


void SensorDevice::handleDynamicSensorConnection(int handle, bool connected) {
void SensorDevice::handleDynamicSensorConnection(int handle, bool connected) {
    if (connected) {
    if (connected) {
        Info model;
        Info model;
        mActivationCount.add(handle, model);
        mActivationCount.add(handle, model);
        mSensorDevice->activate(
        mSensors->activate(handle, 0 /* enabled */);
                reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), handle, 0);
    } else {
    } else {
        mActivationCount.removeItem(handle);
        mActivationCount.removeItem(handle);
    }
    }
}
}


std::string SensorDevice::dump() const {
std::string SensorDevice::dump() const {
    if (!mSensorModule) return "HAL not initialized\n";
    if (mSensors == NULL) return "HAL not initialized\n";


    String8 result;
    String8 result;
    sensor_t const* list;
    mSensors->getSensorsList([&](const auto &list) {
    int count = mSensorModule->get_sensors_list(mSensorModule, &list);
            const size_t count = list.size();


    result.appendFormat("HAL: %s (%s), version %#010x\n",
            result.appendFormat(
                        mSensorModule->common.name,
                "Total %zu h/w sensors, %zu running:\n",
                        mSensorModule->common.author,
                count,
                        getHalDeviceVersion());
                mActivationCount.size());
    result.appendFormat("Total %d h/w sensors, %zu running:\n", count, mActivationCount.size());


            Mutex::Autolock _l(mLock);
            Mutex::Autolock _l(mLock);
    for (int i = 0 ; i < count ; i++) {
            for (size_t i = 0 ; i < count ; i++) {
        const Info& info = mActivationCount.valueFor(list[i].handle);
                const Info& info = mActivationCount.valueFor(
                    list[i].sensorHandle);

                if (info.batchParams.isEmpty()) continue;
                if (info.batchParams.isEmpty()) continue;
        result.appendFormat("0x%08x) active-count = %zu; ", list[i].handle,
                result.appendFormat(
                    "0x%08x) active-count = %zu; ",
                    list[i].sensorHandle,
                    info.batchParams.size());
                    info.batchParams.size());


                result.append("sampling_period(ms) = {");
                result.append("sampling_period(ms) = {");
                for (size_t j = 0; j < info.batchParams.size(); j++) {
                for (size_t j = 0; j < info.batchParams.size(); j++) {
                    const BatchParams& params = info.batchParams.valueAt(j);
                    const BatchParams& params = info.batchParams.valueAt(j);
            result.appendFormat("%.1f%s", params.batchDelay / 1e6f,
                    result.appendFormat(
                        "%.1f%s",
                        params.batchDelay / 1e6f,
                        j < info.batchParams.size() - 1 ? ", " : "");
                        j < info.batchParams.size() - 1 ? ", " : "");
                }
                }
        result.appendFormat("}, selected = %.1f ms; ", info.bestBatchParams.batchDelay / 1e6f);
                result.appendFormat(
                        "}, selected = %.1f ms; ",
                        info.bestBatchParams.batchDelay / 1e6f);


                result.append("batching_period(ms) = {");
                result.append("batching_period(ms) = {");
                for (size_t j = 0; j < info.batchParams.size(); j++) {
                for (size_t j = 0; j < info.batchParams.size(); j++) {
                    BatchParams params = info.batchParams.valueAt(j);
                    BatchParams params = info.batchParams.valueAt(j);
            result.appendFormat("%.1f%s", params.batchTimeout / 1e6f,

                    result.appendFormat(
                            "%.1f%s",
                            params.batchTimeout / 1e6f,
                            j < info.batchParams.size() - 1 ? ", " : "");
                            j < info.batchParams.size() - 1 ? ", " : "");
                }
                }
        result.appendFormat("}, selected = %.1f ms\n", info.bestBatchParams.batchTimeout / 1e6f);

                result.appendFormat(
                        "}, selected = %.1f ms\n",
                        info.bestBatchParams.batchTimeout / 1e6f);
            }
            }
        });

    return result.string();
    return result.string();
}
}


ssize_t SensorDevice::getSensorList(sensor_t const** list) {
ssize_t SensorDevice::getSensorList(sensor_t const** list) {
    if (!mSensorModule) return NO_INIT;
    *list = &mSensorList[0];
    ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list);

    return count;
    return mSensorList.size();
}
}


status_t SensorDevice::initCheck() const {
status_t SensorDevice::initCheck() const {
    return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT;
    return mSensors != NULL ? NO_ERROR : NO_INIT;
}
}


ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
    if (!mSensorDevice) return NO_INIT;
    if (mSensors == NULL) return NO_INIT;
    ssize_t c;

    do {
    ssize_t err;
        c = mSensorDevice->poll(reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice),

                                buffer, count);
    mSensors->poll(
    } while (c == -EINTR);
            count,
    return c;
            [&](auto result,
                const auto &events,
                const auto &dynamicSensorsAdded) {
                if (result == Result::OK) {
                    convertToSensorEvents(events, dynamicSensorsAdded, buffer);
                    err = (ssize_t)events.size();
                } else {
                    err = StatusFromResult(result);
                }
            });

    return err;
}
}


void SensorDevice::autoDisable(void *ident, int handle) {
void SensorDevice::autoDisable(void *ident, int handle) {
@@ -155,7 +184,8 @@ void SensorDevice::autoDisable(void *ident, int handle) {
}
}


status_t SensorDevice::activate(void* ident, int handle, int enabled) {
status_t SensorDevice::activate(void* ident, int handle, int enabled) {
    if (!mSensorDevice) return NO_INIT;
    if (mSensors == NULL) return NO_INIT;

    status_t err(NO_ERROR);
    status_t err(NO_ERROR);
    bool actuateHardware = false;
    bool actuateHardware = false;


@@ -187,13 +217,19 @@ status_t SensorDevice::activate(void* ident, int handle, int enabled) {
    } else {
    } else {
        ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident));
        ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident));


        // If a connected dynamic sensor is deactivated, remove it from the
        // dictionary.
        auto it = mConnectedDynamicSensors.find(handle);
        if (it != mConnectedDynamicSensors.end()) {
            delete it->second;
            mConnectedDynamicSensors.erase(it);
        }

        if (info.removeBatchParamsForIdent(ident) >= 0) {
        if (info.removeBatchParamsForIdent(ident) >= 0) {
            if (info.numActiveClients() == 0) {
            if (info.numActiveClients() == 0) {
                // This is the last connection, we need to de-activate the underlying h/w sensor.
                // This is the last connection, we need to de-activate the underlying h/w sensor.
                actuateHardware = true;
                actuateHardware = true;
            } else {
            } else {
                const int halVersion = getHalDeviceVersion();
                if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) {
                // Call batch for this sensor with the previously calculated best effort
                // Call batch for this sensor with the previously calculated best effort
                // batch_rate and timeout. One of the apps has unregistered for sensor
                // batch_rate and timeout. One of the apps has unregistered for sensor
                // events, and the best effort batch parameters might have changed.
                // events, and the best effort batch parameters might have changed.
@@ -201,11 +237,11 @@ status_t SensorDevice::activate(void* ident, int handle, int enabled) {
                         "\t>>> actuating h/w batch %d %d %" PRId64 " %" PRId64, handle,
                         "\t>>> actuating h/w batch %d %d %" PRId64 " %" PRId64, handle,
                         info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
                         info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
                         info.bestBatchParams.batchTimeout);
                         info.bestBatchParams.batchTimeout);
                    mSensorDevice->batch(mSensorDevice, handle,info.bestBatchParams.flags,
                mSensors->batch(
                        handle,
                        info.bestBatchParams.batchDelay,
                        info.bestBatchParams.batchDelay,
                        info.bestBatchParams.batchTimeout);
                        info.bestBatchParams.batchTimeout);
            }
            }
            }
        } else {
        } else {
            // sensor wasn't enabled for this ident
            // sensor wasn't enabled for this ident
        }
        }
@@ -218,8 +254,7 @@ status_t SensorDevice::activate(void* ident, int handle, int enabled) {
    if (actuateHardware) {
    if (actuateHardware) {
        ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle,
        ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle,
                 enabled);
                 enabled);
        err = mSensorDevice->activate(
        err = StatusFromResult(mSensors->activate(handle, enabled));
                reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), handle, enabled);
        ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle,
        ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle,
                 strerror(-err));
                 strerror(-err));


@@ -229,31 +264,21 @@ status_t SensorDevice::activate(void* ident, int handle, int enabled) {
        }
        }
    }
    }


    // On older devices which do not support batch, call setDelay().
    if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1 && info.numActiveClients() > 0) {
        ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w setDelay %d %" PRId64, handle,
                 info.bestBatchParams.batchDelay);
        mSensorDevice->setDelay(
                reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
                handle, info.bestBatchParams.batchDelay);
    }
    return err;
    return err;
}
}


status_t SensorDevice::batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
status_t SensorDevice::batch(
        void* ident,
        int handle,
        int flags,
        int64_t samplingPeriodNs,
        int64_t maxBatchReportLatencyNs) {
        int64_t maxBatchReportLatencyNs) {
    if (!mSensorDevice) return NO_INIT;
    if (mSensors == NULL) return NO_INIT;


    if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
    if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
        samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
        samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
    }
    }


    const int halVersion = getHalDeviceVersion();
    if (halVersion < SENSORS_DEVICE_API_VERSION_1_1 && maxBatchReportLatencyNs != 0) {
        // Batch is not supported on older devices return invalid operation.
        return INVALID_OPERATION;
    }

    ALOGD_IF(DEBUG_CONNECTIONS,
    ALOGD_IF(DEBUG_CONNECTIONS,
             "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64,
             "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64,
             ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs);
             ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs);
@@ -282,21 +307,17 @@ status_t SensorDevice::batch(void* ident, int handle, int flags, int64_t samplin
    status_t err(NO_ERROR);
    status_t err(NO_ERROR);
    // If the min period or min timeout has changed since the last batch call, call batch.
    // If the min period or min timeout has changed since the last batch call, call batch.
    if (prevBestBatchParams != info.bestBatchParams) {
    if (prevBestBatchParams != info.bestBatchParams) {
        if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) {
        ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH %d %d %" PRId64 " %" PRId64, handle,
        ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH %d %d %" PRId64 " %" PRId64, handle,
                 info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
                 info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
                 info.bestBatchParams.batchTimeout);
                 info.bestBatchParams.batchTimeout);
            err = mSensorDevice->batch(mSensorDevice, handle, info.bestBatchParams.flags,
        err = StatusFromResult(
                mSensors->batch(
                    handle,
                    info.bestBatchParams.batchDelay,
                    info.bestBatchParams.batchDelay,
                                       info.bestBatchParams.batchTimeout);
                    info.bestBatchParams.batchTimeout));
        } else {
            // For older devices which do not support batch, call setDelay() after activate() is
            // called. Some older devices may not support calling setDelay before activate(), so
            // call setDelay in SensorDevice::activate() method.
        }
        if (err != NO_ERROR) {
        if (err != NO_ERROR) {
            ALOGE("sensor batch failed %p %d %d %" PRId64 " %" PRId64 " err=%s",
            ALOGE("sensor batch failed %p %d %d %" PRId64 " %" PRId64 " err=%s",
                  mSensorDevice, handle,
                  mSensors.get(), handle,
                  info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
                  info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
                  info.bestBatchParams.batchTimeout, strerror(-err));
                  info.bestBatchParams.batchTimeout, strerror(-err));
            info.removeBatchParamsForIdent(ident);
            info.removeBatchParamsForIdent(ident);
@@ -306,7 +327,7 @@ status_t SensorDevice::batch(void* ident, int handle, int flags, int64_t samplin
}
}


status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) {
status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) {
    if (!mSensorDevice) return NO_INIT;
    if (mSensors == NULL) return NO_INIT;
    if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
    if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
        samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
        samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
    }
    }
@@ -325,22 +346,20 @@ status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodN
    BatchParams& params = info.batchParams.editValueAt(index);
    BatchParams& params = info.batchParams.editValueAt(index);
    params.batchDelay = samplingPeriodNs;
    params.batchDelay = samplingPeriodNs;
    info.selectBatchParams();
    info.selectBatchParams();
    return mSensorDevice->setDelay(reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),

                                   handle, info.bestBatchParams.batchDelay);
    return StatusFromResult(
            mSensors->batch(handle, info.bestBatchParams.batchDelay, 0));
}
}


int SensorDevice::getHalDeviceVersion() const {
int SensorDevice::getHalDeviceVersion() const {
    if (!mSensorDevice) return -1;
    if (mSensors == NULL) return -1;
    return mSensorDevice->common.version;
    return SENSORS_DEVICE_API_VERSION_1_4;
}
}


status_t SensorDevice::flush(void* ident, int handle) {
status_t SensorDevice::flush(void* ident, int handle) {
    if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1) {
        return INVALID_OPERATION;
    }
    if (isClientDisabled(ident)) return INVALID_OPERATION;
    if (isClientDisabled(ident)) return INVALID_OPERATION;
    ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle);
    ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle);
    return mSensorDevice->flush(mSensorDevice, handle);
    return StatusFromResult(mSensors->flush(handle));
}
}


bool SensorDevice::isClientDisabled(void* ident) {
bool SensorDevice::isClientDisabled(void* ident) {
@@ -356,7 +375,6 @@ void SensorDevice::enableAllSensors() {
    Mutex::Autolock _l(mLock);
    Mutex::Autolock _l(mLock);
    mDisabledClients.clear();
    mDisabledClients.clear();
    ALOGI("cleared mDisabledClients");
    ALOGI("cleared mDisabledClients");
    const int halVersion = getHalDeviceVersion();
    for (size_t i = 0; i< mActivationCount.size(); ++i) {
    for (size_t i = 0; i< mActivationCount.size(); ++i) {
        Info& info = mActivationCount.editValueAt(i);
        Info& info = mActivationCount.editValueAt(i);
        if (info.batchParams.isEmpty()) continue;
        if (info.batchParams.isEmpty()) continue;
@@ -364,27 +382,18 @@ void SensorDevice::enableAllSensors() {
        const int sensor_handle = mActivationCount.keyAt(i);
        const int sensor_handle = mActivationCount.keyAt(i);
        ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ",
        ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ",
                   sensor_handle);
                   sensor_handle);
        status_t err(NO_ERROR);
        status_t err = StatusFromResult(
        if (halVersion > SENSORS_DEVICE_API_VERSION_1_0) {
                mSensors->batch(
            err = mSensorDevice->batch(mSensorDevice, sensor_handle,
                    sensor_handle,
                 info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
                    info.bestBatchParams.batchDelay,
                 info.bestBatchParams.batchTimeout);
                    info.bestBatchParams.batchTimeout));
        ALOGE_IF(err, "Error calling batch on sensor %d (%s)", sensor_handle, strerror(-err));
        ALOGE_IF(err, "Error calling batch on sensor %d (%s)", sensor_handle, strerror(-err));
        }


        if (err == NO_ERROR) {
        if (err == NO_ERROR) {
            err = mSensorDevice->activate(
            err = StatusFromResult(
                    reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
                    mSensors->activate(sensor_handle, 1 /* enabled */));
                    sensor_handle, 1);
            ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err));
            ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err));
        }
        }

        if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0) {
             err = mSensorDevice->setDelay(
                    reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
                    sensor_handle, info.bestBatchParams.batchDelay);
             ALOGE_IF(err, "Error calling setDelay sensor %d (%s)", sensor_handle, strerror(-err));
        }
    }
    }
}
}


@@ -397,9 +406,8 @@ void SensorDevice::disableAllSensors() {
           const int sensor_handle = mActivationCount.keyAt(i);
           const int sensor_handle = mActivationCount.keyAt(i);
           ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ",
           ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ",
                   sensor_handle);
                   sensor_handle);
           mSensorDevice->activate(
           mSensors->activate(sensor_handle, 0 /* enabled */);
                   reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice),

                   sensor_handle, 0);
           // Add all the connections that were registered for this sensor to the disabled
           // Add all the connections that were registered for this sensor to the disabled
           // clients list.
           // clients list.
           for (size_t j = 0; j < info.batchParams.size(); ++j) {
           for (size_t j = 0; j < info.batchParams.size(); ++j) {
@@ -410,7 +418,8 @@ void SensorDevice::disableAllSensors() {
    }
    }
}
}


status_t SensorDevice::injectSensorData(const sensors_event_t *injected_sensor_event) {
status_t SensorDevice::injectSensorData(
        const sensors_event_t *injected_sensor_event) {
    ALOGD_IF(DEBUG_CONNECTIONS,
    ALOGD_IF(DEBUG_CONNECTIONS,
            "sensor_event handle=%d ts=%" PRId64 " data=%.2f, %.2f, %.2f %.2f %.2f %.2f",
            "sensor_event handle=%d ts=%" PRId64 " data=%.2f, %.2f, %.2f %.2f %.2f %.2f",
            injected_sensor_event->sensor,
            injected_sensor_event->sensor,
@@ -418,17 +427,18 @@ status_t SensorDevice::injectSensorData(const sensors_event_t *injected_sensor_e
            injected_sensor_event->data[1], injected_sensor_event->data[2],
            injected_sensor_event->data[1], injected_sensor_event->data[2],
            injected_sensor_event->data[3], injected_sensor_event->data[4],
            injected_sensor_event->data[3], injected_sensor_event->data[4],
            injected_sensor_event->data[5]);
            injected_sensor_event->data[5]);
      if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) {

          return INVALID_OPERATION;
    Event ev;
      }
    convertFromSensorEvent(*injected_sensor_event, &ev);
      return mSensorDevice->inject_sensor_data(mSensorDevice, injected_sensor_event);

    return StatusFromResult(mSensors->injectSensorData(ev));
}
}


status_t SensorDevice::setMode(uint32_t mode) {
status_t SensorDevice::setMode(uint32_t mode) {
     if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) {

          return INVALID_OPERATION;
     return StatusFromResult(
     }
             mSensors->setOperationMode(
     return mSensorModule->set_operation_mode(mode);
                 static_cast<hardware::sensors::V1_0::OperationMode>(mode)));
}
}


// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
@@ -491,44 +501,139 @@ void SensorDevice::notifyConnectionDestroyed(void* ident) {
}
}


int32_t SensorDevice::registerDirectChannel(const sensors_direct_mem_t* memory) {
int32_t SensorDevice::registerDirectChannel(const sensors_direct_mem_t* memory) {
    Mutex::Autolock _l(mLock);


    if (!isDirectReportSupported()) {
    SharedMemType type;
        return INVALID_OPERATION;
    switch (memory->type) {
        case SENSOR_DIRECT_MEM_TYPE_ASHMEM:
            type = SharedMemType::ASHMEM;
            break;
        case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
            type = SharedMemType::GRALLOC;
            break;
        default:
            return BAD_VALUE;
    }

    SharedMemFormat format;
    if (memory->format != SENSOR_DIRECT_FMT_SENSORS_EVENT) {
        return BAD_VALUE;
    }
    format = SharedMemFormat::SENSORS_EVENT;

    SharedMemInfo mem = {
        .type = type,
        .format = format,
        .size = static_cast<uint32_t>(memory->size),
        .memoryHandle = memory->handle,
    };

    int32_t ret;
    mSensors->registerDirectChannel(mem,
            [&ret](auto result, auto channelHandle) {
                if (result == Result::OK) {
                    ret = channelHandle;
                } else {
                    ret = StatusFromResult(result);
                }
            });
    return ret;
}
}


void SensorDevice::unregisterDirectChannel(int32_t channelHandle) {
    Mutex::Autolock _l(mLock);
    Mutex::Autolock _l(mLock);

    mSensors->unregisterDirectChannel(channelHandle);
    int32_t channelHandle = mSensorDevice->register_direct_channel(
            mSensorDevice, memory, -1 /*channel_handle*/);
    return channelHandle;
}
}


void SensorDevice::unregisterDirectChannel(int32_t channelHandle) {
int32_t SensorDevice::configureDirectChannel(int32_t sensorHandle,
        int32_t channelHandle, const struct sensors_direct_cfg_t *config) {
    Mutex::Autolock _l(mLock);
    Mutex::Autolock _l(mLock);


    mSensorDevice->register_direct_channel(mSensorDevice, nullptr, channelHandle);
    RateLevel rate;
    switch(config->rate_level) {
        case SENSOR_DIRECT_RATE_STOP:
            rate = RateLevel::STOP;
            break;
        case SENSOR_DIRECT_RATE_NORMAL:
            rate = RateLevel::NORMAL;
            break;
        case SENSOR_DIRECT_RATE_FAST:
            rate = RateLevel::FAST;
            break;
        case SENSOR_DIRECT_RATE_VERY_FAST:
            rate = RateLevel::VERY_FAST;
            break;
        default:
            return BAD_VALUE;
    }

    int32_t ret;
    mSensors->configDirectReport(sensorHandle, channelHandle, rate,
            [&ret, rate] (auto result, auto token) {
                if (rate == RateLevel::STOP) {
                    ret = StatusFromResult(result);
                } else {
                    if (result == Result::OK) {
                        ret = token;
                    } else {
                        ret = StatusFromResult(result);
                    }
                }
                }
            });


int32_t SensorDevice::configureDirectChannel(int32_t sensorHandle, int32_t channelHandle,
    return ret;
        const struct sensors_direct_cfg_t *config) {
}


    if (!isDirectReportSupported()) {
bool SensorDevice::isDirectReportSupported() const {
        return INVALID_OPERATION;
    return mIsDirectReportSupported;
}
}


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


    int32_t ret = mSensorDevice->config_direct_report(
    if (src.sensorType == SensorType::DYNAMIC_SENSOR_META) {
            mSensorDevice, sensorHandle, channelHandle, config);
        const DynamicSensorInfo &dyn = src.u.dynamic;
    ALOGE_IF(ret < 0, "SensorDevice::configureDirectChannel ret %d", ret);

    return ret;
        dst->dynamic_sensor_meta.connected = dyn.connected;
        dst->dynamic_sensor_meta.handle = dyn.sensorHandle;
        if (dyn.connected) {
            auto it = mConnectedDynamicSensors.find(dyn.sensorHandle);
            CHECK(it != mConnectedDynamicSensors.end());

            dst->dynamic_sensor_meta.sensor = it->second;

            memcpy(dst->dynamic_sensor_meta.uuid,
                   dyn.uuid.data(),
                   sizeof(dst->dynamic_sensor_meta.uuid));
        }
    }
}
}


bool SensorDevice::isDirectReportSupported() const {
void SensorDevice::convertToSensorEvents(
    bool ret = mSensorDevice->register_direct_channel != nullptr
        const hidl_vec<Event> &src,
            && mSensorDevice->config_direct_report != nullptr;
        const hidl_vec<SensorInfo> &dynamicSensorsAdded,
    return ret;
        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));
    }

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

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
}; // namespace android
}; // namespace android