blob: 973a247a63599df2c2fd1f8c0e84bb9f2fe543e0 [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#ifndef ANDROID_SENSOR_SERVICE_UTIL_RECENT_EVENT_LOGGER_H
18#define ANDROID_SENSOR_SERVICE_UTIL_RECENT_EVENT_LOGGER_H
19
20#include "RingBuffer.h"
21#include "SensorServiceUtils.h"
22
23#include <hardware/sensors.h>
24#include <utils/String8.h>
25
26#include <mutex>
27
28namespace android {
29namespace SensorServiceUtil {
30
31// A circular buffer that record the last N events of a sensor type for debugging. The size of this
32// buffer depends on sensor type and is controlled by logSizeBySensorType(). The last N events
33// generated from the sensor are stored in this buffer. The buffer is NOT cleared when the sensor
34// unregisters and as a result very old data in the dumpsys output can be seen, which is an intended
35// behavior.
36class RecentEventLogger : public Dumpable {
37public:
Chih-Hung Hsieh38fc1012016-09-01 11:32:35 -070038 explicit RecentEventLogger(int sensorType);
Peng Xu6a2d3a02015-12-21 12:00:23 -080039 void addEvent(const sensors_event_t& event);
40 bool populateLastEvent(sensors_event_t *event) const;
41 bool isEmpty() const;
42 virtual ~RecentEventLogger() {}
43
44 // Dumpable interface
45 virtual std::string dump() const override;
46
47protected:
48 struct SensorEventLog {
Chih-Hung Hsieh38fc1012016-09-01 11:32:35 -070049 explicit SensorEventLog(const sensors_event_t& e);
Peng Xu6a2d3a02015-12-21 12:00:23 -080050 timespec mWallTime;
51 sensors_event_t mEvent;
52 };
53
54 const int mSensorType;
55 const size_t mEventSize;
56
57 mutable std::mutex mLock;
58 RingBuffer<SensorEventLog> mRecentEvents;
59
60private:
61 static size_t logSizeBySensorType(int sensorType);
62};
63
64} // namespace SensorServiceUtil
65} // namespace android;
66
67#endif // ANDROID_SENSOR_SERVICE_UTIL_RECENT_EVENT_LOGGER_H
68