| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 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 | #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 |  | 
|  | 28 | namespace android { | 
|  | 29 | namespace 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. | 
|  | 36 | class RecentEventLogger : public Dumpable { | 
|  | 37 | public: | 
| Chih-Hung Hsieh | 38fc101 | 2016-09-01 11:32:35 -0700 | [diff] [blame] | 38 | explicit RecentEventLogger(int sensorType); | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 39 | void addEvent(const sensors_event_t& event); | 
| Brian Stack | 0b858c1 | 2018-10-19 10:53:25 -0700 | [diff] [blame] | 40 |  | 
|  | 41 | // Populate event with the last recorded sensor event if it is not stale. An event is | 
|  | 42 | // considered stale if the sensor has become deactivated since the event was recorded. | 
|  | 43 | // returns true on success, false if no recent event is available or the last event is stale | 
|  | 44 | bool populateLastEventIfCurrent(sensors_event_t *event) const; | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 45 | bool isEmpty() const; | 
| Brian Stack | 0b858c1 | 2018-10-19 10:53:25 -0700 | [diff] [blame] | 46 | void setLastEventStale(); | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 47 | virtual ~RecentEventLogger() {} | 
|  | 48 |  | 
|  | 49 | // Dumpable interface | 
|  | 50 | virtual std::string dump() const override; | 
| Peng Xu | 2c238fc | 2016-09-08 12:36:59 -0700 | [diff] [blame] | 51 | virtual void setFormat(std::string format) override; | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 52 |  | 
|  | 53 | protected: | 
|  | 54 | struct SensorEventLog { | 
| Chih-Hung Hsieh | 38fc101 | 2016-09-01 11:32:35 -0700 | [diff] [blame] | 55 | explicit SensorEventLog(const sensors_event_t& e); | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 56 | timespec mWallTime; | 
|  | 57 | sensors_event_t mEvent; | 
|  | 58 | }; | 
|  | 59 |  | 
|  | 60 | const int mSensorType; | 
|  | 61 | const size_t mEventSize; | 
|  | 62 |  | 
|  | 63 | mutable std::mutex mLock; | 
|  | 64 | RingBuffer<SensorEventLog> mRecentEvents; | 
|  | 65 |  | 
| Peng Xu | 2c238fc | 2016-09-08 12:36:59 -0700 | [diff] [blame] | 66 | bool mMaskData; | 
| Brian Stack | 0b858c1 | 2018-10-19 10:53:25 -0700 | [diff] [blame] | 67 | bool mIsLastEventCurrent; | 
| Peng Xu | 2c238fc | 2016-09-08 12:36:59 -0700 | [diff] [blame] | 68 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 69 | private: | 
|  | 70 | static size_t logSizeBySensorType(int sensorType); | 
|  | 71 | }; | 
|  | 72 |  | 
|  | 73 | } // namespace SensorServiceUtil | 
|  | 74 | } // namespace android; | 
|  | 75 |  | 
|  | 76 | #endif // ANDROID_SENSOR_SERVICE_UTIL_RECENT_EVENT_LOGGER_H | 
|  | 77 |  |