1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #include "RecentEventLogger.h"
18*38e8c45fSAndroid Build Coastguard Worker #include "SensorServiceUtils.h"
19*38e8c45fSAndroid Build Coastguard Worker
20*38e8c45fSAndroid Build Coastguard Worker #include <android/util/ProtoOutputStream.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <frameworks/base/core/proto/android/service/sensor_service.proto.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <utils/Timers.h>
23*38e8c45fSAndroid Build Coastguard Worker
24*38e8c45fSAndroid Build Coastguard Worker #include <inttypes.h>
25*38e8c45fSAndroid Build Coastguard Worker
26*38e8c45fSAndroid Build Coastguard Worker namespace android {
27*38e8c45fSAndroid Build Coastguard Worker namespace SensorServiceUtil {
28*38e8c45fSAndroid Build Coastguard Worker
29*38e8c45fSAndroid Build Coastguard Worker namespace {
30*38e8c45fSAndroid Build Coastguard Worker constexpr size_t LOG_SIZE = 10;
31*38e8c45fSAndroid Build Coastguard Worker constexpr size_t LOG_SIZE_MED = 30; // debugging for slower sensors
32*38e8c45fSAndroid Build Coastguard Worker constexpr size_t LOG_SIZE_LARGE = 50; // larger samples for debugging
33*38e8c45fSAndroid Build Coastguard Worker }// unnamed namespace
34*38e8c45fSAndroid Build Coastguard Worker
RecentEventLogger(int sensorType)35*38e8c45fSAndroid Build Coastguard Worker RecentEventLogger::RecentEventLogger(int sensorType) :
36*38e8c45fSAndroid Build Coastguard Worker mSensorType(sensorType), mEventSize(eventSizeBySensorType(mSensorType)),
37*38e8c45fSAndroid Build Coastguard Worker mRecentEvents(logSizeBySensorType(sensorType)), mMaskData(false),
38*38e8c45fSAndroid Build Coastguard Worker mIsLastEventCurrent(false) {
39*38e8c45fSAndroid Build Coastguard Worker // blank
40*38e8c45fSAndroid Build Coastguard Worker }
41*38e8c45fSAndroid Build Coastguard Worker
addEvent(const sensors_event_t & event)42*38e8c45fSAndroid Build Coastguard Worker void RecentEventLogger::addEvent(const sensors_event_t& event) {
43*38e8c45fSAndroid Build Coastguard Worker std::lock_guard<std::mutex> lk(mLock);
44*38e8c45fSAndroid Build Coastguard Worker mRecentEvents.emplace(event);
45*38e8c45fSAndroid Build Coastguard Worker mIsLastEventCurrent = true;
46*38e8c45fSAndroid Build Coastguard Worker }
47*38e8c45fSAndroid Build Coastguard Worker
isEmpty() const48*38e8c45fSAndroid Build Coastguard Worker bool RecentEventLogger::isEmpty() const {
49*38e8c45fSAndroid Build Coastguard Worker return mRecentEvents.size() == 0;
50*38e8c45fSAndroid Build Coastguard Worker }
51*38e8c45fSAndroid Build Coastguard Worker
setLastEventStale()52*38e8c45fSAndroid Build Coastguard Worker void RecentEventLogger::setLastEventStale() {
53*38e8c45fSAndroid Build Coastguard Worker std::lock_guard<std::mutex> lk(mLock);
54*38e8c45fSAndroid Build Coastguard Worker mIsLastEventCurrent = false;
55*38e8c45fSAndroid Build Coastguard Worker }
56*38e8c45fSAndroid Build Coastguard Worker
dump() const57*38e8c45fSAndroid Build Coastguard Worker std::string RecentEventLogger::dump() const {
58*38e8c45fSAndroid Build Coastguard Worker std::lock_guard<std::mutex> lk(mLock);
59*38e8c45fSAndroid Build Coastguard Worker
60*38e8c45fSAndroid Build Coastguard Worker //TODO: replace String8 with std::string completely in this function
61*38e8c45fSAndroid Build Coastguard Worker String8 buffer;
62*38e8c45fSAndroid Build Coastguard Worker
63*38e8c45fSAndroid Build Coastguard Worker buffer.appendFormat("last %zu events\n", mRecentEvents.size());
64*38e8c45fSAndroid Build Coastguard Worker int j = 0;
65*38e8c45fSAndroid Build Coastguard Worker for (int i = mRecentEvents.size() - 1; i >= 0; --i) {
66*38e8c45fSAndroid Build Coastguard Worker const auto& ev = mRecentEvents[i];
67*38e8c45fSAndroid Build Coastguard Worker struct tm * timeinfo = localtime(&(ev.mWallTime.tv_sec));
68*38e8c45fSAndroid Build Coastguard Worker buffer.appendFormat("\t%2d (ts=%.9f, wall=%02d:%02d:%02d.%03d) ",
69*38e8c45fSAndroid Build Coastguard Worker ++j, ev.mEvent.timestamp/1e9, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec,
70*38e8c45fSAndroid Build Coastguard Worker (int) ns2ms(ev.mWallTime.tv_nsec));
71*38e8c45fSAndroid Build Coastguard Worker
72*38e8c45fSAndroid Build Coastguard Worker // data
73*38e8c45fSAndroid Build Coastguard Worker if (!mMaskData) {
74*38e8c45fSAndroid Build Coastguard Worker if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
75*38e8c45fSAndroid Build Coastguard Worker buffer.appendFormat("%" PRIu64 ", ", ev.mEvent.u64.step_counter);
76*38e8c45fSAndroid Build Coastguard Worker } else {
77*38e8c45fSAndroid Build Coastguard Worker for (size_t k = 0; k < mEventSize; ++k) {
78*38e8c45fSAndroid Build Coastguard Worker buffer.appendFormat("%.2f, ", ev.mEvent.data[k]);
79*38e8c45fSAndroid Build Coastguard Worker }
80*38e8c45fSAndroid Build Coastguard Worker }
81*38e8c45fSAndroid Build Coastguard Worker } else {
82*38e8c45fSAndroid Build Coastguard Worker buffer.append("[value masked]");
83*38e8c45fSAndroid Build Coastguard Worker }
84*38e8c45fSAndroid Build Coastguard Worker buffer.append("\n");
85*38e8c45fSAndroid Build Coastguard Worker }
86*38e8c45fSAndroid Build Coastguard Worker return std::string(buffer.c_str());
87*38e8c45fSAndroid Build Coastguard Worker }
88*38e8c45fSAndroid Build Coastguard Worker
89*38e8c45fSAndroid Build Coastguard Worker /**
90*38e8c45fSAndroid Build Coastguard Worker * Dump debugging information as android.service.SensorEventsProto protobuf message using
91*38e8c45fSAndroid Build Coastguard Worker * ProtoOutputStream.
92*38e8c45fSAndroid Build Coastguard Worker *
93*38e8c45fSAndroid Build Coastguard Worker * See proto definition and some notes about ProtoOutputStream in
94*38e8c45fSAndroid Build Coastguard Worker * frameworks/base/core/proto/android/service/sensor_service.proto
95*38e8c45fSAndroid Build Coastguard Worker */
dump(util::ProtoOutputStream * proto) const96*38e8c45fSAndroid Build Coastguard Worker void RecentEventLogger::dump(util::ProtoOutputStream* proto) const {
97*38e8c45fSAndroid Build Coastguard Worker using namespace service::SensorEventsProto;
98*38e8c45fSAndroid Build Coastguard Worker std::lock_guard<std::mutex> lk(mLock);
99*38e8c45fSAndroid Build Coastguard Worker
100*38e8c45fSAndroid Build Coastguard Worker proto->write(RecentEventsLog::RECENT_EVENTS_COUNT, int(mRecentEvents.size()));
101*38e8c45fSAndroid Build Coastguard Worker for (int i = mRecentEvents.size() - 1; i >= 0; --i) {
102*38e8c45fSAndroid Build Coastguard Worker const auto& ev = mRecentEvents[i];
103*38e8c45fSAndroid Build Coastguard Worker const uint64_t token = proto->start(RecentEventsLog::EVENTS);
104*38e8c45fSAndroid Build Coastguard Worker proto->write(Event::TIMESTAMP_SEC, float(ev.mEvent.timestamp) / 1e9f);
105*38e8c45fSAndroid Build Coastguard Worker proto->write(Event::WALL_TIMESTAMP_MS, ev.mWallTime.tv_sec * 1000LL
106*38e8c45fSAndroid Build Coastguard Worker + ns2ms(ev.mWallTime.tv_nsec));
107*38e8c45fSAndroid Build Coastguard Worker
108*38e8c45fSAndroid Build Coastguard Worker if (mMaskData) {
109*38e8c45fSAndroid Build Coastguard Worker proto->write(Event::MASKED, true);
110*38e8c45fSAndroid Build Coastguard Worker } else {
111*38e8c45fSAndroid Build Coastguard Worker if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
112*38e8c45fSAndroid Build Coastguard Worker proto->write(Event::INT64_DATA, int64_t(ev.mEvent.u64.step_counter));
113*38e8c45fSAndroid Build Coastguard Worker } else {
114*38e8c45fSAndroid Build Coastguard Worker for (size_t k = 0; k < mEventSize; ++k) {
115*38e8c45fSAndroid Build Coastguard Worker proto->write(Event::FLOAT_ARRAY, ev.mEvent.data[k]);
116*38e8c45fSAndroid Build Coastguard Worker }
117*38e8c45fSAndroid Build Coastguard Worker }
118*38e8c45fSAndroid Build Coastguard Worker }
119*38e8c45fSAndroid Build Coastguard Worker proto->end(token);
120*38e8c45fSAndroid Build Coastguard Worker }
121*38e8c45fSAndroid Build Coastguard Worker }
122*38e8c45fSAndroid Build Coastguard Worker
setFormat(std::string format)123*38e8c45fSAndroid Build Coastguard Worker void RecentEventLogger::setFormat(std::string format) {
124*38e8c45fSAndroid Build Coastguard Worker if (format == "mask_data" ) {
125*38e8c45fSAndroid Build Coastguard Worker mMaskData = true;
126*38e8c45fSAndroid Build Coastguard Worker } else {
127*38e8c45fSAndroid Build Coastguard Worker mMaskData = false;
128*38e8c45fSAndroid Build Coastguard Worker }
129*38e8c45fSAndroid Build Coastguard Worker }
130*38e8c45fSAndroid Build Coastguard Worker
populateLastEventIfCurrent(sensors_event_t * event) const131*38e8c45fSAndroid Build Coastguard Worker bool RecentEventLogger::populateLastEventIfCurrent(sensors_event_t *event) const {
132*38e8c45fSAndroid Build Coastguard Worker std::lock_guard<std::mutex> lk(mLock);
133*38e8c45fSAndroid Build Coastguard Worker
134*38e8c45fSAndroid Build Coastguard Worker if (mIsLastEventCurrent && mRecentEvents.size()) {
135*38e8c45fSAndroid Build Coastguard Worker // Index 0 contains the latest event emplace()'ed
136*38e8c45fSAndroid Build Coastguard Worker *event = mRecentEvents[0].mEvent;
137*38e8c45fSAndroid Build Coastguard Worker return true;
138*38e8c45fSAndroid Build Coastguard Worker } else {
139*38e8c45fSAndroid Build Coastguard Worker return false;
140*38e8c45fSAndroid Build Coastguard Worker }
141*38e8c45fSAndroid Build Coastguard Worker }
142*38e8c45fSAndroid Build Coastguard Worker
143*38e8c45fSAndroid Build Coastguard Worker
logSizeBySensorType(int sensorType)144*38e8c45fSAndroid Build Coastguard Worker size_t RecentEventLogger::logSizeBySensorType(int sensorType) {
145*38e8c45fSAndroid Build Coastguard Worker if (sensorType == SENSOR_TYPE_STEP_COUNTER ||
146*38e8c45fSAndroid Build Coastguard Worker sensorType == SENSOR_TYPE_SIGNIFICANT_MOTION ||
147*38e8c45fSAndroid Build Coastguard Worker sensorType == SENSOR_TYPE_ACCELEROMETER ||
148*38e8c45fSAndroid Build Coastguard Worker sensorType == SENSOR_TYPE_LIGHT) {
149*38e8c45fSAndroid Build Coastguard Worker return LOG_SIZE_LARGE;
150*38e8c45fSAndroid Build Coastguard Worker }
151*38e8c45fSAndroid Build Coastguard Worker if (sensorType == SENSOR_TYPE_PROXIMITY) {
152*38e8c45fSAndroid Build Coastguard Worker return LOG_SIZE_MED;
153*38e8c45fSAndroid Build Coastguard Worker }
154*38e8c45fSAndroid Build Coastguard Worker return LOG_SIZE;
155*38e8c45fSAndroid Build Coastguard Worker }
156*38e8c45fSAndroid Build Coastguard Worker
SensorEventLog(const sensors_event_t & e)157*38e8c45fSAndroid Build Coastguard Worker RecentEventLogger::SensorEventLog::SensorEventLog(const sensors_event_t& e) : mEvent(e) {
158*38e8c45fSAndroid Build Coastguard Worker clock_gettime(CLOCK_REALTIME, &mWallTime);
159*38e8c45fSAndroid Build Coastguard Worker }
160*38e8c45fSAndroid Build Coastguard Worker
161*38e8c45fSAndroid Build Coastguard Worker } // namespace SensorServiceUtil
162*38e8c45fSAndroid Build Coastguard Worker } // namespace android
163