xref: /aosp_15_r20/frameworks/native/services/sensorservice/RecentEventLogger.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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 #ifndef ANDROID_SENSOR_SERVICE_UTIL_RECENT_EVENT_LOGGER_H
18*38e8c45fSAndroid Build Coastguard Worker #define ANDROID_SENSOR_SERVICE_UTIL_RECENT_EVENT_LOGGER_H
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #include "RingBuffer.h"
21*38e8c45fSAndroid Build Coastguard Worker #include "SensorServiceUtils.h"
22*38e8c45fSAndroid Build Coastguard Worker 
23*38e8c45fSAndroid Build Coastguard Worker #include <hardware/sensors.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <utils/String8.h>
25*38e8c45fSAndroid Build Coastguard Worker 
26*38e8c45fSAndroid Build Coastguard Worker #include <mutex>
27*38e8c45fSAndroid Build Coastguard Worker 
28*38e8c45fSAndroid Build Coastguard Worker namespace android {
29*38e8c45fSAndroid Build Coastguard Worker namespace SensorServiceUtil {
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker // A circular buffer that record the last N events of a sensor type for debugging. The size of this
32*38e8c45fSAndroid Build Coastguard Worker // buffer depends on sensor type and is controlled by logSizeBySensorType(). The last N events
33*38e8c45fSAndroid Build Coastguard Worker // generated from the sensor are stored in this buffer.  The buffer is NOT cleared when the sensor
34*38e8c45fSAndroid Build Coastguard Worker // unregisters and as a result very old data in the dumpsys output can be seen, which is an intended
35*38e8c45fSAndroid Build Coastguard Worker // behavior.
36*38e8c45fSAndroid Build Coastguard Worker class RecentEventLogger : public Dumpable {
37*38e8c45fSAndroid Build Coastguard Worker public:
38*38e8c45fSAndroid Build Coastguard Worker     explicit RecentEventLogger(int sensorType);
39*38e8c45fSAndroid Build Coastguard Worker     void addEvent(const sensors_event_t& event);
40*38e8c45fSAndroid Build Coastguard Worker 
41*38e8c45fSAndroid Build Coastguard Worker     // Populate event with the last recorded sensor event if it is not stale. An event is
42*38e8c45fSAndroid Build Coastguard Worker     // considered stale if the sensor has become deactivated since the event was recorded.
43*38e8c45fSAndroid Build Coastguard Worker     // returns true on success, false if no recent event is available or the last event is stale
44*38e8c45fSAndroid Build Coastguard Worker     bool populateLastEventIfCurrent(sensors_event_t *event) const;
45*38e8c45fSAndroid Build Coastguard Worker     bool isEmpty() const;
46*38e8c45fSAndroid Build Coastguard Worker     void setLastEventStale();
~RecentEventLogger()47*38e8c45fSAndroid Build Coastguard Worker     virtual ~RecentEventLogger() {}
48*38e8c45fSAndroid Build Coastguard Worker 
49*38e8c45fSAndroid Build Coastguard Worker     // Dumpable interface
50*38e8c45fSAndroid Build Coastguard Worker     virtual std::string dump() const override;
51*38e8c45fSAndroid Build Coastguard Worker     virtual void dump(util::ProtoOutputStream* proto) const override;
52*38e8c45fSAndroid Build Coastguard Worker     virtual void setFormat(std::string format) override;
53*38e8c45fSAndroid Build Coastguard Worker 
54*38e8c45fSAndroid Build Coastguard Worker protected:
55*38e8c45fSAndroid Build Coastguard Worker     struct SensorEventLog {
56*38e8c45fSAndroid Build Coastguard Worker         explicit SensorEventLog(const sensors_event_t& e);
57*38e8c45fSAndroid Build Coastguard Worker         timespec mWallTime;
58*38e8c45fSAndroid Build Coastguard Worker         sensors_event_t mEvent;
59*38e8c45fSAndroid Build Coastguard Worker     };
60*38e8c45fSAndroid Build Coastguard Worker 
61*38e8c45fSAndroid Build Coastguard Worker     const int mSensorType;
62*38e8c45fSAndroid Build Coastguard Worker     const size_t mEventSize;
63*38e8c45fSAndroid Build Coastguard Worker 
64*38e8c45fSAndroid Build Coastguard Worker     mutable std::mutex mLock;
65*38e8c45fSAndroid Build Coastguard Worker     RingBuffer<SensorEventLog> mRecentEvents;
66*38e8c45fSAndroid Build Coastguard Worker 
67*38e8c45fSAndroid Build Coastguard Worker     bool mMaskData;
68*38e8c45fSAndroid Build Coastguard Worker     bool mIsLastEventCurrent;
69*38e8c45fSAndroid Build Coastguard Worker 
70*38e8c45fSAndroid Build Coastguard Worker private:
71*38e8c45fSAndroid Build Coastguard Worker     static size_t logSizeBySensorType(int sensorType);
72*38e8c45fSAndroid Build Coastguard Worker };
73*38e8c45fSAndroid Build Coastguard Worker 
74*38e8c45fSAndroid Build Coastguard Worker } // namespace SensorServiceUtil
75*38e8c45fSAndroid Build Coastguard Worker } // namespace android;
76*38e8c45fSAndroid Build Coastguard Worker 
77*38e8c45fSAndroid Build Coastguard Worker #endif // ANDROID_SENSOR_SERVICE_UTIL_RECENT_EVENT_LOGGER_H
78*38e8c45fSAndroid Build Coastguard Worker 
79