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 <android/util/ProtoOutputStream.h>
21 #include <frameworks/base/core/proto/android/service/sensor_service.proto.h>
22 #include <utils/Timers.h>
23 
24 #include <inttypes.h>
25 
26 namespace android {
27 namespace SensorServiceUtil {
28 
29 namespace {
30     constexpr size_t LOG_SIZE = 10;
31     constexpr size_t LOG_SIZE_MED = 30;  // debugging for slower sensors
32     constexpr size_t LOG_SIZE_LARGE = 50;  // larger samples for debugging
33 }// unnamed namespace
34 
RecentEventLogger(int sensorType)35 RecentEventLogger::RecentEventLogger(int sensorType) :
36         mSensorType(sensorType), mEventSize(eventSizeBySensorType(mSensorType)),
37         mRecentEvents(logSizeBySensorType(sensorType)), mMaskData(false),
38         mIsLastEventCurrent(false) {
39     // blank
40 }
41 
addEvent(const sensors_event_t & event)42 void RecentEventLogger::addEvent(const sensors_event_t& event) {
43     std::lock_guard<std::mutex> lk(mLock);
44     mRecentEvents.emplace(event);
45     mIsLastEventCurrent = true;
46 }
47 
isEmpty() const48 bool RecentEventLogger::isEmpty() const {
49     return mRecentEvents.size() == 0;
50 }
51 
setLastEventStale()52 void RecentEventLogger::setLastEventStale() {
53     std::lock_guard<std::mutex> lk(mLock);
54     mIsLastEventCurrent = false;
55 }
56 
dump() const57 std::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
73         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                 }
80             }
81         } else {
82             buffer.append("[value masked]");
83         }
84         buffer.append("\n");
85     }
86     return std::string(buffer.string());
87 }
88 
89 /**
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  */
dump(util::ProtoOutputStream * proto) const96 void 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 
setFormat(std::string format)123 void RecentEventLogger::setFormat(std::string format) {
124     if (format == "mask_data" ) {
125         mMaskData = true;
126     } else {
127         mMaskData = false;
128     }
129 }
130 
populateLastEventIfCurrent(sensors_event_t * event) const131 bool RecentEventLogger::populateLastEventIfCurrent(sensors_event_t *event) const {
132     std::lock_guard<std::mutex> lk(mLock);
133 
134     if (mIsLastEventCurrent && mRecentEvents.size()) {
135         // Index 0 contains the latest event emplace()'ed
136         *event = mRecentEvents[0].mEvent;
137         return true;
138     } else {
139         return false;
140     }
141 }
142 
143 
logSizeBySensorType(int sensorType)144 size_t RecentEventLogger::logSizeBySensorType(int sensorType) {
145     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;
155 }
156 
SensorEventLog(const sensors_event_t & e)157 RecentEventLogger::SensorEventLog::SensorEventLog(const sensors_event_t& e) : mEvent(e) {
158     clock_gettime(CLOCK_REALTIME, &mWallTime);
159 }
160 
161 } // namespace SensorServiceUtil
162 } // namespace android
163