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 | |
Aurimas Liutikas | 6f7854b | 2016-02-19 14:08:07 -0800 | [diff] [blame] | 19 | #include <inttypes.h> |
| 20 | |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 21 | namespace android { |
| 22 | |
| 23 | SensorService::MostRecentEventLogger::MostRecentEventLogger(int sensorType) : |
Aurimas Liutikas | 6f7854b | 2016-02-19 14:08:07 -0800 | [diff] [blame] | 24 | mNextInd(0), mSensorType(sensorType) { |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 25 | |
| 26 | mBufSize = (sensorType == SENSOR_TYPE_STEP_COUNTER || |
| 27 | sensorType == SENSOR_TYPE_SIGNIFICANT_MOTION || |
| 28 | sensorType == SENSOR_TYPE_ACCELEROMETER) ? LOG_SIZE : LOG_SIZE_LARGE; |
| 29 | |
| 30 | mTrimmedSensorEventArr = new TrimmedSensorEvent *[mBufSize]; |
| 31 | mSensorType = sensorType; |
| 32 | for (int i = 0; i < mBufSize; ++i) { |
| 33 | mTrimmedSensorEventArr[i] = new TrimmedSensorEvent(mSensorType); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void SensorService::MostRecentEventLogger::addEvent(const sensors_event_t& event) { |
| 38 | TrimmedSensorEvent *curr_event = mTrimmedSensorEventArr[mNextInd]; |
| 39 | curr_event->mTimestamp = event.timestamp; |
| 40 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { |
| 41 | curr_event->mStepCounter = event.u64.step_counter; |
| 42 | } else { |
| 43 | memcpy(curr_event->mData, event.data, |
| 44 | sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType)); |
| 45 | } |
| 46 | time_t rawtime = time(NULL); |
| 47 | struct tm * timeinfo = localtime(&rawtime); |
| 48 | curr_event->mHour = timeinfo->tm_hour; |
| 49 | curr_event->mMin = timeinfo->tm_min; |
| 50 | curr_event->mSec = timeinfo->tm_sec; |
| 51 | mNextInd = (mNextInd + 1) % mBufSize; |
| 52 | } |
| 53 | |
| 54 | void SensorService::MostRecentEventLogger::printBuffer(String8& result) const { |
| 55 | const int numData = SensorService::getNumEventsForSensorType(mSensorType); |
| 56 | int i = mNextInd, eventNum = 1; |
| 57 | result.appendFormat("last %d events = < ", mBufSize); |
| 58 | do { |
| 59 | if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[i])) { |
| 60 | // Sentinel, ignore. |
| 61 | i = (i + 1) % mBufSize; |
| 62 | continue; |
| 63 | } |
| 64 | result.appendFormat("%d) ", eventNum++); |
| 65 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { |
Aurimas Liutikas | 6f7854b | 2016-02-19 14:08:07 -0800 | [diff] [blame] | 66 | result.appendFormat("%" PRIu64 ",", mTrimmedSensorEventArr[i]->mStepCounter); |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 67 | } else { |
| 68 | for (int j = 0; j < numData; ++j) { |
| 69 | result.appendFormat("%5.1f,", mTrimmedSensorEventArr[i]->mData[j]); |
| 70 | } |
| 71 | } |
Aurimas Liutikas | 6f7854b | 2016-02-19 14:08:07 -0800 | [diff] [blame] | 72 | result.appendFormat("%" PRId64 " %02d:%02d:%02d ", mTrimmedSensorEventArr[i]->mTimestamp, |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 73 | mTrimmedSensorEventArr[i]->mHour, mTrimmedSensorEventArr[i]->mMin, |
| 74 | mTrimmedSensorEventArr[i]->mSec); |
| 75 | i = (i + 1) % mBufSize; |
| 76 | } while (i != mNextInd); |
| 77 | result.appendFormat(">\n"); |
| 78 | } |
| 79 | |
| 80 | bool SensorService::MostRecentEventLogger::populateLastEvent(sensors_event_t *event) { |
| 81 | int lastEventInd = (mNextInd - 1 + mBufSize) % mBufSize; |
| 82 | // Check if the buffer is empty. |
| 83 | if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[lastEventInd])) { |
| 84 | return false; |
| 85 | } |
| 86 | event->version = sizeof(sensors_event_t); |
| 87 | event->type = mSensorType; |
| 88 | event->timestamp = mTrimmedSensorEventArr[lastEventInd]->mTimestamp; |
| 89 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { |
| 90 | event->u64.step_counter = mTrimmedSensorEventArr[lastEventInd]->mStepCounter; |
| 91 | } else { |
| 92 | memcpy(event->data, mTrimmedSensorEventArr[lastEventInd]->mData, |
| 93 | sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType)); |
| 94 | } |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | SensorService::MostRecentEventLogger::~MostRecentEventLogger() { |
| 99 | for (int i = 0; i < mBufSize; ++i) { |
| 100 | delete mTrimmedSensorEventArr[i]; |
| 101 | } |
| 102 | delete [] mTrimmedSensorEventArr; |
| 103 | } |
| 104 | |
| 105 | // ----------------------------------------------------------------------------- |
| 106 | SensorService::MostRecentEventLogger::TrimmedSensorEvent::TrimmedSensorEvent(int sensorType) { |
| 107 | mTimestamp = -1; |
| 108 | const int numData = SensorService::getNumEventsForSensorType(sensorType); |
| 109 | if (sensorType == SENSOR_TYPE_STEP_COUNTER) { |
| 110 | mStepCounter = 0; |
| 111 | } else { |
| 112 | mData = new float[numData]; |
| 113 | for (int i = 0; i < numData; ++i) { |
| 114 | mData[i] = -1.0; |
| 115 | } |
| 116 | } |
| 117 | mHour = mMin = mSec = INT32_MIN; |
| 118 | } |
| 119 | |
| 120 | bool SensorService::MostRecentEventLogger::TrimmedSensorEvent:: |
| 121 | isSentinel(const TrimmedSensorEvent& event) { |
| 122 | return (event.mHour == INT32_MIN && event.mMin == INT32_MIN && event.mSec == INT32_MIN); |
| 123 | } |
| 124 | |
| 125 | } // namespace android |