blob: 47fa8b39ac81deac3a6d6fcd7bf63f846365e4a6 [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
Mike Ma24743862020-01-29 00:36:55 -080020#include <android/util/ProtoOutputStream.h>
21#include <frameworks/base/core/proto/android/service/sensor_service.proto.h>
Peng Xu6a2d3a02015-12-21 12:00:23 -080022#include <utils/Timers.h>
23
24#include <inttypes.h>
25
26namespace android {
27namespace SensorServiceUtil {
28
29namespace {
30 constexpr size_t LOG_SIZE = 10;
Andrew Lehmeraeef9d52018-09-24 13:23:16 -070031 constexpr size_t LOG_SIZE_MED = 30; // debugging for slower sensors
Peng Xu6a2d3a02015-12-21 12:00:23 -080032 constexpr size_t LOG_SIZE_LARGE = 50; // larger samples for debugging
33}// unnamed namespace
34
35RecentEventLogger::RecentEventLogger(int sensorType) :
36 mSensorType(sensorType), mEventSize(eventSizeBySensorType(mSensorType)),
Brian Stack0b858c12018-10-19 10:53:25 -070037 mRecentEvents(logSizeBySensorType(sensorType)), mMaskData(false),
38 mIsLastEventCurrent(false) {
Peng Xu6a2d3a02015-12-21 12:00:23 -080039 // blank
40}
41
42void RecentEventLogger::addEvent(const sensors_event_t& event) {
43 std::lock_guard<std::mutex> lk(mLock);
44 mRecentEvents.emplace(event);
Brian Stack0b858c12018-10-19 10:53:25 -070045 mIsLastEventCurrent = true;
Peng Xu6a2d3a02015-12-21 12:00:23 -080046}
47
48bool RecentEventLogger::isEmpty() const {
49 return mRecentEvents.size() == 0;
50}
51
Brian Stack0b858c12018-10-19 10:53:25 -070052void RecentEventLogger::setLastEventStale() {
53 std::lock_guard<std::mutex> lk(mLock);
54 mIsLastEventCurrent = false;
55}
56
Peng Xu6a2d3a02015-12-21 12:00:23 -080057std::string RecentEventLogger::dump() const {
58 std::lock_guard<std::mutex> lk(mLock);
59
60 //TODO: replace String8 with std::string completely in this function
61 String8 buffer;
62
63 buffer.appendFormat("last %zu events\n", mRecentEvents.size());
64 int j = 0;
65 for (int i = mRecentEvents.size() - 1; i >= 0; --i) {
66 const auto& ev = mRecentEvents[i];
67 struct tm * timeinfo = localtime(&(ev.mWallTime.tv_sec));
68 buffer.appendFormat("\t%2d (ts=%.9f, wall=%02d:%02d:%02d.%03d) ",
69 ++j, ev.mEvent.timestamp/1e9, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec,
70 (int) ns2ms(ev.mWallTime.tv_nsec));
71
72 // data
Peng Xu2c238fc2016-09-08 12:36:59 -070073 if (!mMaskData) {
74 if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
75 buffer.appendFormat("%" PRIu64 ", ", ev.mEvent.u64.step_counter);
76 } else {
77 for (size_t k = 0; k < mEventSize; ++k) {
78 buffer.appendFormat("%.2f, ", ev.mEvent.data[k]);
79 }
Peng Xu6a2d3a02015-12-21 12:00:23 -080080 }
Peng Xu2c238fc2016-09-08 12:36:59 -070081 } else {
82 buffer.append("[value masked]");
Peng Xu6a2d3a02015-12-21 12:00:23 -080083 }
84 buffer.append("\n");
85 }
Tomasz Wasilczyk83aa0ab2023-08-11 00:06:51 +000086 return std::string(buffer.c_str());
Peng Xu6a2d3a02015-12-21 12:00:23 -080087}
88
Mike Ma24743862020-01-29 00:36:55 -080089/**
90 * Dump debugging information as android.service.SensorEventsProto protobuf message using
91 * ProtoOutputStream.
92 *
93 * See proto definition and some notes about ProtoOutputStream in
94 * frameworks/base/core/proto/android/service/sensor_service.proto
95 */
96void RecentEventLogger::dump(util::ProtoOutputStream* proto) const {
97 using namespace service::SensorEventsProto;
98 std::lock_guard<std::mutex> lk(mLock);
99
100 proto->write(RecentEventsLog::RECENT_EVENTS_COUNT, int(mRecentEvents.size()));
101 for (int i = mRecentEvents.size() - 1; i >= 0; --i) {
102 const auto& ev = mRecentEvents[i];
103 const uint64_t token = proto->start(RecentEventsLog::EVENTS);
104 proto->write(Event::TIMESTAMP_SEC, float(ev.mEvent.timestamp) / 1e9f);
105 proto->write(Event::WALL_TIMESTAMP_MS, ev.mWallTime.tv_sec * 1000LL
106 + ns2ms(ev.mWallTime.tv_nsec));
107
108 if (mMaskData) {
109 proto->write(Event::MASKED, true);
110 } else {
111 if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
112 proto->write(Event::INT64_DATA, int64_t(ev.mEvent.u64.step_counter));
113 } else {
114 for (size_t k = 0; k < mEventSize; ++k) {
115 proto->write(Event::FLOAT_ARRAY, ev.mEvent.data[k]);
116 }
117 }
118 }
119 proto->end(token);
120 }
121}
122
Peng Xu2c238fc2016-09-08 12:36:59 -0700123void RecentEventLogger::setFormat(std::string format) {
124 if (format == "mask_data" ) {
125 mMaskData = true;
126 } else {
127 mMaskData = false;
128 }
129}
130
Brian Stack0b858c12018-10-19 10:53:25 -0700131bool RecentEventLogger::populateLastEventIfCurrent(sensors_event_t *event) const {
Peng Xu6a2d3a02015-12-21 12:00:23 -0800132 std::lock_guard<std::mutex> lk(mLock);
133
Brian Stack0b858c12018-10-19 10:53:25 -0700134 if (mIsLastEventCurrent && mRecentEvents.size()) {
Peng Xu45ca9f52016-05-02 14:15:50 -0700135 // Index 0 contains the latest event emplace()'ed
136 *event = mRecentEvents[0].mEvent;
Peng Xu6a2d3a02015-12-21 12:00:23 -0800137 return true;
138 } else {
139 return false;
140 }
141}
142
143
144size_t RecentEventLogger::logSizeBySensorType(int sensorType) {
Andrew Lehmeraeef9d52018-09-24 13:23:16 -0700145 if (sensorType == SENSOR_TYPE_STEP_COUNTER ||
146 sensorType == SENSOR_TYPE_SIGNIFICANT_MOTION ||
147 sensorType == SENSOR_TYPE_ACCELEROMETER ||
148 sensorType == SENSOR_TYPE_LIGHT) {
149 return LOG_SIZE_LARGE;
150 }
151 if (sensorType == SENSOR_TYPE_PROXIMITY) {
152 return LOG_SIZE_MED;
153 }
154 return LOG_SIZE;
Peng Xu6a2d3a02015-12-21 12:00:23 -0800155}
156
157RecentEventLogger::SensorEventLog::SensorEventLog(const sensors_event_t& e) : mEvent(e) {
158 clock_gettime(CLOCK_REALTIME, &mWallTime);
159}
160
161} // namespace SensorServiceUtil
162} // namespace android