blob: cec2ae5443d1288c951a587e49026cd7a819cc4e [file] [log] [blame]
Peng Xu6a2d3a02015-12-21 12:00:23 -08001/*
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
24namespace android {
25namespace SensorServiceUtil {
26
27namespace {
28 constexpr size_t LOG_SIZE = 10;
Andrew Lehmeraeef9d52018-09-24 13:23:16 -070029 constexpr size_t LOG_SIZE_MED = 30; // debugging for slower sensors
Peng Xu6a2d3a02015-12-21 12:00:23 -080030 constexpr size_t LOG_SIZE_LARGE = 50; // larger samples for debugging
31}// unnamed namespace
32
33RecentEventLogger::RecentEventLogger(int sensorType) :
34 mSensorType(sensorType), mEventSize(eventSizeBySensorType(mSensorType)),
Peng Xu2c238fc2016-09-08 12:36:59 -070035 mRecentEvents(logSizeBySensorType(sensorType)), mMaskData(false) {
Peng Xu6a2d3a02015-12-21 12:00:23 -080036 // blank
37}
38
39void RecentEventLogger::addEvent(const sensors_event_t& event) {
40 std::lock_guard<std::mutex> lk(mLock);
41 mRecentEvents.emplace(event);
42}
43
44bool RecentEventLogger::isEmpty() const {
45 return mRecentEvents.size() == 0;
46}
47
48std::string RecentEventLogger::dump() const {
49 std::lock_guard<std::mutex> lk(mLock);
50
51 //TODO: replace String8 with std::string completely in this function
52 String8 buffer;
53
54 buffer.appendFormat("last %zu events\n", mRecentEvents.size());
55 int j = 0;
56 for (int i = mRecentEvents.size() - 1; i >= 0; --i) {
57 const auto& ev = mRecentEvents[i];
58 struct tm * timeinfo = localtime(&(ev.mWallTime.tv_sec));
59 buffer.appendFormat("\t%2d (ts=%.9f, wall=%02d:%02d:%02d.%03d) ",
60 ++j, ev.mEvent.timestamp/1e9, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec,
61 (int) ns2ms(ev.mWallTime.tv_nsec));
62
63 // data
Peng Xu2c238fc2016-09-08 12:36:59 -070064 if (!mMaskData) {
65 if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
66 buffer.appendFormat("%" PRIu64 ", ", ev.mEvent.u64.step_counter);
67 } else {
68 for (size_t k = 0; k < mEventSize; ++k) {
69 buffer.appendFormat("%.2f, ", ev.mEvent.data[k]);
70 }
Peng Xu6a2d3a02015-12-21 12:00:23 -080071 }
Peng Xu2c238fc2016-09-08 12:36:59 -070072 } else {
73 buffer.append("[value masked]");
Peng Xu6a2d3a02015-12-21 12:00:23 -080074 }
75 buffer.append("\n");
76 }
77 return std::string(buffer.string());
78}
79
Peng Xu2c238fc2016-09-08 12:36:59 -070080void RecentEventLogger::setFormat(std::string format) {
81 if (format == "mask_data" ) {
82 mMaskData = true;
83 } else {
84 mMaskData = false;
85 }
86}
87
Peng Xu6a2d3a02015-12-21 12:00:23 -080088bool RecentEventLogger::populateLastEvent(sensors_event_t *event) const {
89 std::lock_guard<std::mutex> lk(mLock);
90
91 if (mRecentEvents.size()) {
Peng Xu45ca9f52016-05-02 14:15:50 -070092 // Index 0 contains the latest event emplace()'ed
93 *event = mRecentEvents[0].mEvent;
Peng Xu6a2d3a02015-12-21 12:00:23 -080094 return true;
95 } else {
96 return false;
97 }
98}
99
100
101size_t RecentEventLogger::logSizeBySensorType(int sensorType) {
Andrew Lehmeraeef9d52018-09-24 13:23:16 -0700102 if (sensorType == SENSOR_TYPE_STEP_COUNTER ||
103 sensorType == SENSOR_TYPE_SIGNIFICANT_MOTION ||
104 sensorType == SENSOR_TYPE_ACCELEROMETER ||
105 sensorType == SENSOR_TYPE_LIGHT) {
106 return LOG_SIZE_LARGE;
107 }
108 if (sensorType == SENSOR_TYPE_PROXIMITY) {
109 return LOG_SIZE_MED;
110 }
111 return LOG_SIZE;
Peng Xu6a2d3a02015-12-21 12:00:23 -0800112}
113
114RecentEventLogger::SensorEventLog::SensorEventLog(const sensors_event_t& e) : mEvent(e) {
115 clock_gettime(CLOCK_REALTIME, &mWallTime);
116}
117
118} // namespace SensorServiceUtil
119} // namespace android