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

Commit cf43c265 authored by Peng Xu's avatar Peng Xu Committed by Android (Google) Code Review
Browse files

Merge "Fix sensor uuid, retrofit recent event logger" into nyc-dev

parents 6cfb89c0 6a2d3a06
Loading
Loading
Loading
Loading
+10 −3
Original line number Diff line number Diff line
@@ -52,12 +52,18 @@ public:
        TYPE_PROXIMITY      = ASENSOR_TYPE_PROXIMITY
    };

    typedef struct {
    struct uuid_t{
        union {
            uint8_t b[16];
    } uuid_t;
            int64_t i64[2];
        };
        uuid_t(const uint8_t (&uuid)[16]) { memcpy(b, uuid, sizeof(b));}
        uuid_t() : b{0} {}
    };

    Sensor(const char * name = "");
    Sensor(struct sensor_t const* hwSensor, int halVersion = 0);
    Sensor(struct sensor_t const& hwSensor, const uuid_t& uuid, int halVersion = 0);
    ~Sensor();

    const String8& getName() const;
@@ -81,6 +87,7 @@ public:
    uint32_t getFlags() const;
    bool isWakeUpSensor() const;
    bool isDynamicSensor() const;
    bool hasAdditionalInfo() const;
    int32_t getReportingMode() const;
    const uuid_t& getUuid() const;

+67 −67
Original line number Diff line number Diff line
@@ -14,69 +14,70 @@
 * limitations under the License.
 */

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

#include <utils/Errors.h>
#include <utils/String8.h>
#include <utils/Flattenable.h>

#include <hardware/sensors.h>

#include <binder/AppOpsManager.h>
#include <binder/IServiceManager.h>

#include <gui/Sensor.h>
#include <hardware/sensors.h>
#include <log/log.h>
#include <utils/Errors.h>
#include <utils/String8.h>
#include <utils/Flattenable.h>

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

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

Sensor::Sensor(const char * name)
    : mName(name), mHandle(0), mType(0),
Sensor::Sensor(const char * name) :
        mName(name), mHandle(0), mType(0),
        mMinValue(0), mMaxValue(0), mResolution(0),
        mPower(0), mMinDelay(0), mVersion(0), mFifoReservedEventCount(0),
        mFifoMaxEventCount(0), mRequiredAppOp(0),
      mMaxDelay(0), mFlags(0)
{
        mMaxDelay(0), mFlags(0) {
}

Sensor::Sensor(struct sensor_t const* hwSensor, int halVersion)
{
    mName = hwSensor->name;
    mVendor = hwSensor->vendor;
    mVersion = hwSensor->version;
    mHandle = hwSensor->handle;
    mType = hwSensor->type;
Sensor::Sensor(struct sensor_t const* hwSensor, int halVersion) :
        Sensor(*hwSensor, uuid_t(), halVersion) {
}

Sensor::Sensor(struct sensor_t const& hwSensor, const uuid_t& uuid, int halVersion) {
    mName = hwSensor.name;
    mVendor = hwSensor.vendor;
    mVersion = hwSensor.version;
    mHandle = hwSensor.handle;
    mType = hwSensor.type;
    mMinValue = 0;                      // FIXME: minValue
    mMaxValue = hwSensor->maxRange;     // FIXME: maxValue
    mResolution = hwSensor->resolution;
    mPower = hwSensor->power;
    mMinDelay = hwSensor->minDelay;
    mMaxValue = hwSensor.maxRange;     // FIXME: maxValue
    mResolution = hwSensor.resolution;
    mPower = hwSensor.power;
    mMinDelay = hwSensor.minDelay;
    mFlags = 0;
    mUuid = uuid;

    // Set fifo event count zero for older devices which do not support batching. Fused
    // sensors also have their fifo counts set to zero.
    if (halVersion > SENSORS_DEVICE_API_VERSION_1_0) {
        mFifoReservedEventCount = hwSensor->fifoReservedEventCount;
        mFifoMaxEventCount = hwSensor->fifoMaxEventCount;
        mFifoReservedEventCount = hwSensor.fifoReservedEventCount;
        mFifoMaxEventCount = hwSensor.fifoMaxEventCount;
    } else {
        mFifoReservedEventCount = 0;
        mFifoMaxEventCount = 0;
    }

    if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
        if (hwSensor->maxDelay > INT_MAX) {
        if (hwSensor.maxDelay > INT_MAX) {
            // Max delay is declared as a 64 bit integer for 64 bit architectures. But it should
            // always fit in a 32 bit integer, log error and cap it to INT_MAX.
            ALOGE("Sensor maxDelay overflow error %s %" PRId64, mName.string(),
                  static_cast<int64_t>(hwSensor->maxDelay));
                  static_cast<int64_t>(hwSensor.maxDelay));
            mMaxDelay = INT_MAX;
        } else {
            mMaxDelay = static_cast<int32_t>(hwSensor->maxDelay);
            mMaxDelay = static_cast<int32_t>(hwSensor.maxDelay);
        }
    } else {
        // For older hals set maxDelay to 0.
@@ -245,11 +246,11 @@ Sensor::Sensor(struct sensor_t const* hwSensor, int halVersion)
        break;
    default:
        // Only pipe the stringType, requiredPermission and flags for custom sensors.
        if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor->stringType) {
            mStringType = hwSensor->stringType;
        if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor.stringType) {
            mStringType = hwSensor.stringType;
        }
        if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor->requiredPermission) {
            mRequiredPermission = hwSensor->requiredPermission;
        if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor.requiredPermission) {
            mRequiredPermission = hwSensor.requiredPermission;
            if (!strcmp(mRequiredPermission, SENSOR_PERMISSION_BODY_SENSORS)) {
                AppOpsManager appOps;
                mRequiredAppOp = appOps.permissionToOpCode(String16(SENSOR_PERMISSION_BODY_SENSORS));
@@ -257,7 +258,7 @@ Sensor::Sensor(struct sensor_t const* hwSensor, int halVersion)
        }

        if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
            mFlags = static_cast<uint32_t>(hwSensor->flags);
            mFlags = static_cast<uint32_t>(hwSensor.flags);
        } else {
            // This is an OEM defined sensor on an older HAL. Use minDelay to determine the
            // reporting mode of the sensor.
@@ -272,31 +273,28 @@ Sensor::Sensor(struct sensor_t const* hwSensor, int halVersion)
        break;
    }

    // Set DYNAMIC_SENSOR_MASK and ADDITIONAL_INFO_MASK flag here. Compatible with HAL 1_3.
    if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
        mFlags |= (hwSensor->flags & (DYNAMIC_SENSOR_MASK | ADDITIONAL_INFO_MASK));
    }

    // Set DATA_INJECTION flag here. Defined in HAL 1_4.
    if (halVersion >= SENSORS_DEVICE_API_VERSION_1_4) {
        mFlags |= (hwSensor->flags & DATA_INJECTION_MASK);
    }
        // Wake-up flag of HAL 1.3 and above is set here
        mFlags |= (hwSensor.flags & SENSOR_FLAG_WAKE_UP);

    // For the newer HALs log errors if reporting mask flags are set incorrectly.
    if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
        // Wake-up flag is set here.
        mFlags |= (hwSensor->flags & SENSOR_FLAG_WAKE_UP);
        if (mFlags != hwSensor->flags) {
            int actualReportingMode =
                 (hwSensor->flags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
        // Log error if the reporting mode is not as expected, but respect HAL setting.
        int actualReportingMode = (hwSensor.flags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
        int expectedReportingMode = (mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
        if (actualReportingMode != expectedReportingMode) {
                ALOGE("Reporting Mode incorrect: sensor %s handle=%d type=%d "
            ALOGE("Reporting Mode incorrect: sensor %s handle=%#010" PRIx32 " type=%" PRId32 " "
                   "actual=%d expected=%d",
                   mName.string(), mHandle, mType, actualReportingMode, expectedReportingMode);
        }
    }

    // Feature flags
    // Set DYNAMIC_SENSOR_MASK and ADDITIONAL_INFO_MASK flag here. Compatible with HAL 1_3.
    if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
        mFlags |= (hwSensor.flags & (DYNAMIC_SENSOR_MASK | ADDITIONAL_INFO_MASK));
    }
    // Set DATA_INJECTION flag here. Defined in HAL 1_4.
    if (halVersion >= SENSORS_DEVICE_API_VERSION_1_4) {
        mFlags |= (hwSensor.flags & DATA_INJECTION_MASK);
    }

    if (mRequiredPermission.length() > 0) {
@@ -311,8 +309,7 @@ Sensor::Sensor(struct sensor_t const* hwSensor, int halVersion)
    }
}

Sensor::~Sensor()
{
Sensor::~Sensor() {
}

const String8& Sensor::getName() const {
@@ -392,11 +389,15 @@ uint32_t Sensor::getFlags() const {
}

bool Sensor::isWakeUpSensor() const {
    return mFlags & SENSOR_FLAG_WAKE_UP;
    return (mFlags & SENSOR_FLAG_WAKE_UP) != 0;
}

bool Sensor::isDynamicSensor() const {
    return mFlags & SENSOR_FLAG_DYNAMIC_SENSOR;
    return (mFlags & SENSOR_FLAG_DYNAMIC_SENSOR) != 0;
}

bool Sensor::hasAdditionalInfo() const {
    return (mFlags & SENSOR_FLAG_ADDITIONAL_INFO) != 0;
}

int32_t Sensor::getReportingMode() const {
@@ -407,8 +408,7 @@ const Sensor::uuid_t& Sensor::getUuid() const {
    return mUuid;
}

size_t Sensor::getFlattenedSize() const
{
size_t Sensor::getFlattenedSize() const {
    size_t fixedSize =
            sizeof(mVersion) + sizeof(mHandle) + sizeof(mType) +
            sizeof(mMinValue) + sizeof(mMaxValue) + sizeof(mResolution) +
+19 −18
Original line number Diff line number Diff line
@@ -8,15 +8,16 @@ LOCAL_SRC_FILES:= \
    GravitySensor.cpp \
    LinearAccelerationSensor.cpp \
    OrientationSensor.cpp \
    RecentEventLogger.cpp \
    RotationVectorSensor.cpp \
    SensorDevice.cpp \
    SensorEventConnection.cpp \
    SensorFusion.cpp \
    SensorInterface.cpp \
    SensorService.cpp \
    SensorEventConnection.cpp \
    MostRecentEventLogger.cpp \
    SensorRecord.cpp \
    SensorList.cpp \
    SensorRecord.cpp \
    SensorService.cpp \
    SensorServiceUtils.cpp \


LOCAL_CFLAGS:= -DLOG_TAG=\"SensorService\"
+0 −125
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 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.
 */

#include "MostRecentEventLogger.h"

#include <inttypes.h>

namespace android {

SensorService::MostRecentEventLogger::MostRecentEventLogger(int sensorType) :
        mNextInd(0), mSensorType(sensorType) {

    mBufSize = (sensorType == SENSOR_TYPE_STEP_COUNTER ||
                sensorType == SENSOR_TYPE_SIGNIFICANT_MOTION ||
                sensorType == SENSOR_TYPE_ACCELEROMETER) ? LOG_SIZE : LOG_SIZE_LARGE;

    mTrimmedSensorEventArr = new TrimmedSensorEvent *[mBufSize];
    mSensorType = sensorType;
    for (int i = 0; i < mBufSize; ++i) {
        mTrimmedSensorEventArr[i] = new TrimmedSensorEvent(mSensorType);
    }
}

void SensorService::MostRecentEventLogger::addEvent(const sensors_event_t& event) {
    TrimmedSensorEvent *curr_event = mTrimmedSensorEventArr[mNextInd];
    curr_event->mTimestamp = event.timestamp;
    if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
        curr_event->mStepCounter = event.u64.step_counter;
    } else {
        memcpy(curr_event->mData, event.data,
                 sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType));
    }
    time_t rawtime = time(NULL);
    struct tm * timeinfo = localtime(&rawtime);
    curr_event->mHour = timeinfo->tm_hour;
    curr_event->mMin = timeinfo->tm_min;
    curr_event->mSec = timeinfo->tm_sec;
    mNextInd = (mNextInd + 1) % mBufSize;
}

void SensorService::MostRecentEventLogger::printBuffer(String8& result) const {
    const int numData = SensorService::getNumEventsForSensorType(mSensorType);
    int i = mNextInd, eventNum = 1;
    result.appendFormat("last %d events = < ", mBufSize);
    do {
        if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[i])) {
            // Sentinel, ignore.
            i = (i + 1) % mBufSize;
            continue;
        }
        result.appendFormat("%d) ", eventNum++);
        if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
            result.appendFormat("%" PRIu64 ",", mTrimmedSensorEventArr[i]->mStepCounter);
        } else {
            for (int j = 0; j < numData; ++j) {
                result.appendFormat("%5.1f,", mTrimmedSensorEventArr[i]->mData[j]);
            }
        }
        result.appendFormat("%" PRId64 " %02d:%02d:%02d ", mTrimmedSensorEventArr[i]->mTimestamp,
                mTrimmedSensorEventArr[i]->mHour, mTrimmedSensorEventArr[i]->mMin,
                mTrimmedSensorEventArr[i]->mSec);
        i = (i + 1) % mBufSize;
    } while (i != mNextInd);
    result.appendFormat(">\n");
}

bool SensorService::MostRecentEventLogger::populateLastEvent(sensors_event_t *event) {
    int lastEventInd = (mNextInd - 1 + mBufSize) % mBufSize;
    // Check if the buffer is empty.
    if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[lastEventInd])) {
        return false;
    }
    event->version = sizeof(sensors_event_t);
    event->type = mSensorType;
    event->timestamp = mTrimmedSensorEventArr[lastEventInd]->mTimestamp;
    if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
          event->u64.step_counter = mTrimmedSensorEventArr[lastEventInd]->mStepCounter;
    } else {
        memcpy(event->data, mTrimmedSensorEventArr[lastEventInd]->mData,
                 sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType));
    }
    return true;
}

SensorService::MostRecentEventLogger::~MostRecentEventLogger() {
    for (int i = 0; i < mBufSize; ++i) {
        delete mTrimmedSensorEventArr[i];
    }
    delete [] mTrimmedSensorEventArr;
}

// -----------------------------------------------------------------------------
SensorService::MostRecentEventLogger::TrimmedSensorEvent::TrimmedSensorEvent(int sensorType) {
    mTimestamp = -1;
    const int numData = SensorService::getNumEventsForSensorType(sensorType);
    if (sensorType == SENSOR_TYPE_STEP_COUNTER) {
        mStepCounter = 0;
    } else {
        mData = new float[numData];
        for (int i = 0; i < numData; ++i) {
            mData[i] = -1.0;
        }
    }
    mHour = mMin = mSec = INT32_MIN;
}

bool SensorService::MostRecentEventLogger::TrimmedSensorEvent::
    isSentinel(const TrimmedSensorEvent& event) {
    return (event.mHour == INT32_MIN && event.mMin == INT32_MIN && event.mSec == INT32_MIN);
}

} // namespace android
+98 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 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.
 */

#include "RecentEventLogger.h"
#include "SensorServiceUtils.h"

#include <utils/Timers.h>

#include <inttypes.h>

namespace android {
namespace SensorServiceUtil {

namespace {
    constexpr size_t LOG_SIZE = 10;
    constexpr size_t LOG_SIZE_LARGE = 50;  // larger samples for debugging
}// unnamed namespace

RecentEventLogger::RecentEventLogger(int sensorType) :
        mSensorType(sensorType), mEventSize(eventSizeBySensorType(mSensorType)),
        mRecentEvents(logSizeBySensorType(sensorType)) {
    // blank
}

void RecentEventLogger::addEvent(const sensors_event_t& event) {
    std::lock_guard<std::mutex> lk(mLock);
    mRecentEvents.emplace(event);
}

bool RecentEventLogger::isEmpty() const {
    return mRecentEvents.size() == 0;
}

std::string RecentEventLogger::dump() const {
    std::lock_guard<std::mutex> lk(mLock);

    //TODO: replace String8 with std::string completely in this function
    String8 buffer;

    buffer.appendFormat("last %zu events\n", mRecentEvents.size());
    int j = 0;
    for (int i = mRecentEvents.size() - 1; i >= 0; --i) {
        const auto& ev = mRecentEvents[i];
        struct tm * timeinfo = localtime(&(ev.mWallTime.tv_sec));
        buffer.appendFormat("\t%2d (ts=%.9f, wall=%02d:%02d:%02d.%03d) ",
                ++j, ev.mEvent.timestamp/1e9, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec,
                (int) ns2ms(ev.mWallTime.tv_nsec));

        // data
        if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
            buffer.appendFormat("%" PRIu64 ", ", ev.mEvent.u64.step_counter);
        } else {
            for (size_t k = 0; k < mEventSize; ++k) {
                buffer.appendFormat("%.2f, ", ev.mEvent.data[k]);
            }
        }
        buffer.append("\n");
    }
    return std::string(buffer.string());
}

bool RecentEventLogger::populateLastEvent(sensors_event_t *event) const {
    std::lock_guard<std::mutex> lk(mLock);

    if (mRecentEvents.size()) {
        *event = mRecentEvents[mRecentEvents.size()-1].mEvent;
        return true;
    } else {
        return false;
    }
}


size_t RecentEventLogger::logSizeBySensorType(int sensorType) {
    return (sensorType == SENSOR_TYPE_STEP_COUNTER ||
            sensorType == SENSOR_TYPE_SIGNIFICANT_MOTION ||
            sensorType == SENSOR_TYPE_ACCELEROMETER) ? LOG_SIZE_LARGE : LOG_SIZE;
}

RecentEventLogger::SensorEventLog::SensorEventLog(const sensors_event_t& e) : mEvent(e) {
    clock_gettime(CLOCK_REALTIME, &mWallTime);
}

} // namespace SensorServiceUtil
} // namespace android
Loading