| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2010 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include "MostRecentEventLogger.h" | 
|  | 18 |  | 
|  | 19 | namespace android { | 
|  | 20 |  | 
|  | 21 | SensorService::MostRecentEventLogger::MostRecentEventLogger(int sensorType) : | 
|  | 22 | mSensorType(sensorType), mNextInd(0) { | 
|  | 23 |  | 
|  | 24 | mBufSize = (sensorType == SENSOR_TYPE_STEP_COUNTER || | 
|  | 25 | sensorType == SENSOR_TYPE_SIGNIFICANT_MOTION || | 
|  | 26 | sensorType == SENSOR_TYPE_ACCELEROMETER) ? LOG_SIZE : LOG_SIZE_LARGE; | 
|  | 27 |  | 
|  | 28 | mTrimmedSensorEventArr = new TrimmedSensorEvent *[mBufSize]; | 
|  | 29 | mSensorType = sensorType; | 
|  | 30 | for (int i = 0; i < mBufSize; ++i) { | 
|  | 31 | mTrimmedSensorEventArr[i] = new TrimmedSensorEvent(mSensorType); | 
|  | 32 | } | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | void SensorService::MostRecentEventLogger::addEvent(const sensors_event_t& event) { | 
|  | 36 | TrimmedSensorEvent *curr_event = mTrimmedSensorEventArr[mNextInd]; | 
|  | 37 | curr_event->mTimestamp = event.timestamp; | 
|  | 38 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { | 
|  | 39 | curr_event->mStepCounter = event.u64.step_counter; | 
|  | 40 | } else { | 
|  | 41 | memcpy(curr_event->mData, event.data, | 
|  | 42 | sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType)); | 
|  | 43 | } | 
|  | 44 | time_t rawtime = time(NULL); | 
|  | 45 | struct tm * timeinfo = localtime(&rawtime); | 
|  | 46 | curr_event->mHour = timeinfo->tm_hour; | 
|  | 47 | curr_event->mMin = timeinfo->tm_min; | 
|  | 48 | curr_event->mSec = timeinfo->tm_sec; | 
|  | 49 | mNextInd = (mNextInd + 1) % mBufSize; | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | void SensorService::MostRecentEventLogger::printBuffer(String8& result) const { | 
|  | 53 | const int numData = SensorService::getNumEventsForSensorType(mSensorType); | 
|  | 54 | int i = mNextInd, eventNum = 1; | 
|  | 55 | result.appendFormat("last %d events = < ", mBufSize); | 
|  | 56 | do { | 
|  | 57 | if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[i])) { | 
|  | 58 | // Sentinel, ignore. | 
|  | 59 | i = (i + 1) % mBufSize; | 
|  | 60 | continue; | 
|  | 61 | } | 
|  | 62 | result.appendFormat("%d) ", eventNum++); | 
|  | 63 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { | 
|  | 64 | result.appendFormat("%llu,", mTrimmedSensorEventArr[i]->mStepCounter); | 
|  | 65 | } else { | 
|  | 66 | for (int j = 0; j < numData; ++j) { | 
|  | 67 | result.appendFormat("%5.1f,", mTrimmedSensorEventArr[i]->mData[j]); | 
|  | 68 | } | 
|  | 69 | } | 
|  | 70 | result.appendFormat("%lld %02d:%02d:%02d ", mTrimmedSensorEventArr[i]->mTimestamp, | 
|  | 71 | mTrimmedSensorEventArr[i]->mHour, mTrimmedSensorEventArr[i]->mMin, | 
|  | 72 | mTrimmedSensorEventArr[i]->mSec); | 
|  | 73 | i = (i + 1) % mBufSize; | 
|  | 74 | } while (i != mNextInd); | 
|  | 75 | result.appendFormat(">\n"); | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | bool SensorService::MostRecentEventLogger::populateLastEvent(sensors_event_t *event) { | 
|  | 79 | int lastEventInd = (mNextInd - 1 + mBufSize) % mBufSize; | 
|  | 80 | // Check if the buffer is empty. | 
|  | 81 | if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[lastEventInd])) { | 
|  | 82 | return false; | 
|  | 83 | } | 
|  | 84 | event->version = sizeof(sensors_event_t); | 
|  | 85 | event->type = mSensorType; | 
|  | 86 | event->timestamp = mTrimmedSensorEventArr[lastEventInd]->mTimestamp; | 
|  | 87 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { | 
|  | 88 | event->u64.step_counter = mTrimmedSensorEventArr[lastEventInd]->mStepCounter; | 
|  | 89 | } else { | 
|  | 90 | memcpy(event->data, mTrimmedSensorEventArr[lastEventInd]->mData, | 
|  | 91 | sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType)); | 
|  | 92 | } | 
|  | 93 | return true; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | SensorService::MostRecentEventLogger::~MostRecentEventLogger() { | 
|  | 97 | for (int i = 0; i < mBufSize; ++i) { | 
|  | 98 | delete mTrimmedSensorEventArr[i]; | 
|  | 99 | } | 
|  | 100 | delete [] mTrimmedSensorEventArr; | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | // ----------------------------------------------------------------------------- | 
|  | 104 | SensorService::MostRecentEventLogger::TrimmedSensorEvent::TrimmedSensorEvent(int sensorType) { | 
|  | 105 | mTimestamp = -1; | 
|  | 106 | const int numData = SensorService::getNumEventsForSensorType(sensorType); | 
|  | 107 | if (sensorType == SENSOR_TYPE_STEP_COUNTER) { | 
|  | 108 | mStepCounter = 0; | 
|  | 109 | } else { | 
|  | 110 | mData = new float[numData]; | 
|  | 111 | for (int i = 0; i < numData; ++i) { | 
|  | 112 | mData[i] = -1.0; | 
|  | 113 | } | 
|  | 114 | } | 
|  | 115 | mHour = mMin = mSec = INT32_MIN; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | bool SensorService::MostRecentEventLogger::TrimmedSensorEvent:: | 
|  | 119 | isSentinel(const TrimmedSensorEvent& event) { | 
|  | 120 | return (event.mHour == INT32_MIN && event.mMin == INT32_MIN && event.mSec == INT32_MIN); | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | } // namespace android |