Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "RecentEventLogger.h" |
| 18 | #include "SensorServiceUtils.h" |
| 19 | |
| 20 | #include <utils/Timers.h> |
| 21 | |
| 22 | #include <inttypes.h> |
| 23 | |
| 24 | namespace android { |
| 25 | namespace SensorServiceUtil { |
| 26 | |
| 27 | namespace { |
| 28 | constexpr size_t LOG_SIZE = 10; |
| 29 | constexpr size_t LOG_SIZE_LARGE = 50; // larger samples for debugging |
| 30 | }// unnamed namespace |
| 31 | |
| 32 | RecentEventLogger::RecentEventLogger(int sensorType) : |
| 33 | mSensorType(sensorType), mEventSize(eventSizeBySensorType(mSensorType)), |
Peng Xu | 2c238fc | 2016-09-08 12:36:59 -0700 | [diff] [blame] | 34 | mRecentEvents(logSizeBySensorType(sensorType)), mMaskData(false) { |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 35 | // blank |
| 36 | } |
| 37 | |
| 38 | void RecentEventLogger::addEvent(const sensors_event_t& event) { |
| 39 | std::lock_guard<std::mutex> lk(mLock); |
| 40 | mRecentEvents.emplace(event); |
| 41 | } |
| 42 | |
| 43 | bool RecentEventLogger::isEmpty() const { |
| 44 | return mRecentEvents.size() == 0; |
| 45 | } |
| 46 | |
| 47 | std::string RecentEventLogger::dump() const { |
| 48 | std::lock_guard<std::mutex> lk(mLock); |
| 49 | |
| 50 | //TODO: replace String8 with std::string completely in this function |
| 51 | String8 buffer; |
| 52 | |
| 53 | buffer.appendFormat("last %zu events\n", mRecentEvents.size()); |
| 54 | int j = 0; |
| 55 | for (int i = mRecentEvents.size() - 1; i >= 0; --i) { |
| 56 | const auto& ev = mRecentEvents[i]; |
| 57 | struct tm * timeinfo = localtime(&(ev.mWallTime.tv_sec)); |
| 58 | buffer.appendFormat("\t%2d (ts=%.9f, wall=%02d:%02d:%02d.%03d) ", |
| 59 | ++j, ev.mEvent.timestamp/1e9, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, |
| 60 | (int) ns2ms(ev.mWallTime.tv_nsec)); |
| 61 | |
| 62 | // data |
Peng Xu | 2c238fc | 2016-09-08 12:36:59 -0700 | [diff] [blame] | 63 | if (!mMaskData) { |
| 64 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { |
| 65 | buffer.appendFormat("%" PRIu64 ", ", ev.mEvent.u64.step_counter); |
| 66 | } else { |
| 67 | for (size_t k = 0; k < mEventSize; ++k) { |
| 68 | buffer.appendFormat("%.2f, ", ev.mEvent.data[k]); |
| 69 | } |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 70 | } |
Peng Xu | 2c238fc | 2016-09-08 12:36:59 -0700 | [diff] [blame] | 71 | } else { |
| 72 | buffer.append("[value masked]"); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 73 | } |
| 74 | buffer.append("\n"); |
| 75 | } |
| 76 | return std::string(buffer.string()); |
| 77 | } |
| 78 | |
Peng Xu | 2c238fc | 2016-09-08 12:36:59 -0700 | [diff] [blame] | 79 | void RecentEventLogger::setFormat(std::string format) { |
| 80 | if (format == "mask_data" ) { |
| 81 | mMaskData = true; |
| 82 | } else { |
| 83 | mMaskData = false; |
| 84 | } |
| 85 | } |
| 86 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 87 | bool RecentEventLogger::populateLastEvent(sensors_event_t *event) const { |
| 88 | std::lock_guard<std::mutex> lk(mLock); |
| 89 | |
| 90 | if (mRecentEvents.size()) { |
Peng Xu | 45ca9f5 | 2016-05-02 14:15:50 -0700 | [diff] [blame] | 91 | // Index 0 contains the latest event emplace()'ed |
| 92 | *event = mRecentEvents[0].mEvent; |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 93 | return true; |
| 94 | } else { |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | size_t RecentEventLogger::logSizeBySensorType(int sensorType) { |
| 101 | return (sensorType == SENSOR_TYPE_STEP_COUNTER || |
| 102 | sensorType == SENSOR_TYPE_SIGNIFICANT_MOTION || |
| 103 | sensorType == SENSOR_TYPE_ACCELEROMETER) ? LOG_SIZE_LARGE : LOG_SIZE; |
| 104 | } |
| 105 | |
| 106 | RecentEventLogger::SensorEventLog::SensorEventLog(const sensors_event_t& e) : mEvent(e) { |
| 107 | clock_gettime(CLOCK_REALTIME, &mWallTime); |
| 108 | } |
| 109 | |
| 110 | } // namespace SensorServiceUtil |
| 111 | } // namespace android |