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