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

Commit eb4d628b authored by Peng Xu's avatar Peng Xu
Browse files

Divide huge SensorService files into managable pieces

Splitted SensorService.cpp and SensorService.h into smaller more
manageable pieces with the embedded classes/structs now in individual
files.

Change-Id: I5e75c41b9e0e5c9aff102fe4b0f142c61d1203e2
parent 1ba6b9b9
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -12,7 +12,11 @@ LOCAL_SRC_FILES:= \
    SensorDevice.cpp \
    SensorFusion.cpp \
    SensorInterface.cpp \
    SensorService.cpp
    SensorService.cpp \
    SensorEventConnection.cpp \
    MostRecentEventLogger.cpp \
    SensorRecord.cpp \


LOCAL_CFLAGS:= -DLOG_TAG=\"SensorService\"

+123 −0
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"

namespace android {

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

    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("%llu,", mTrimmedSensorEventArr[i]->mStepCounter);
        } else {
            for (int j = 0; j < numData; ++j) {
                result.appendFormat("%5.1f,", mTrimmedSensorEventArr[i]->mData[j]);
            }
        }
        result.appendFormat("%lld %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
+71 −0
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.
 */

#ifndef ANDROID_MOST_RECENT_EVENT_LOGGER_H
#define ANDROID_MOST_RECENT_EVENT_LOGGER_H

#include "SensorService.h"

namespace android {

class SensorService;

// A circular buffer of TrimmedSensorEvents. The size of this buffer is typically 10. The last N
// events generated from the sensor are stored in this buffer. The buffer is NOT cleared when the
// sensor unregisters and as a result very old data in the dumpsys output can be seen, which is an
// intended behavior.
class SensorService::MostRecentEventLogger {
public:
    MostRecentEventLogger(int sensorType);
    void addEvent(const sensors_event_t& event);
    void printBuffer(String8& buffer) const;
    bool populateLastEvent(sensors_event_t *event);
    ~MostRecentEventLogger();

private:
    // sensor_event_t with only the data and the timestamp.
    static const size_t LOG_SIZE = 10;
    static const size_t LOG_SIZE_LARGE = 50;

    struct TrimmedSensorEvent {
        union {
            float *mData;
            uint64_t mStepCounter;
        };
        // Timestamp from the sensors_event_t.
        int64_t mTimestamp;
        // HH:MM:SS local time at which this sensor event is read at SensorService. Useful
        // for debugging.
        int32_t mHour, mMin, mSec;

        TrimmedSensorEvent(int sensorType);
        static bool isSentinel(const TrimmedSensorEvent& event);

        ~TrimmedSensorEvent() {
            delete [] mData;
        }
    };

    int mNextInd;
    int mSensorType;
    int mBufSize;
    TrimmedSensorEvent ** mTrimmedSensorEventArr;
};

} // namespace android;

#endif // ANDROID_MOST_RECENT_EVENT_LOGGER_H
+37 −0
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.
 */

#ifndef ANDROID_SENSOR_EVENT_ACK_RECEIVER_H
#define ANDROID_SENSOR_EVENT_ACK_RECEIVER_H

#include "SensorService.h"

namespace android {

class SensorService;

class SensorService::SensorEventAckReceiver : public Thread {
    sp<SensorService> const mService;
public:
    virtual bool threadLoop();
    SensorEventAckReceiver(const sp<SensorService>& service)
            : mService(service) {
    }
};

}

#endif // ANDROID_SENSOR_EVNET_ACK_RECEIVER_H
+636 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading