• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 _UI_INPUTREADER_SENSOR_INPUT_MAPPER_H
18 #define _UI_INPUTREADER_SENSOR_INPUT_MAPPER_H
19 
20 #include "InputMapper.h"
21 
22 namespace android {
23 // sensor data vector length
24 static constexpr ssize_t SENSOR_VEC_LEN = 3;
25 
26 class SensorInputMapper : public InputMapper {
27 public:
28     explicit SensorInputMapper(InputDeviceContext& deviceContext);
29     ~SensorInputMapper() override;
30 
31     uint32_t getSources() override;
32     void populateDeviceInfo(InputDeviceInfo* deviceInfo) override;
33     void dump(std::string& dump) override;
34     void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) override;
35     void reset(nsecs_t when) override;
36     void process(const RawEvent* rawEvent) override;
37     bool enableSensor(InputDeviceSensorType sensorType, std::chrono::microseconds samplingPeriod,
38                       std::chrono::microseconds maxBatchReportLatency) override;
39     void disableSensor(InputDeviceSensorType sensorType) override;
40     void flushSensor(InputDeviceSensorType sensorType) override;
41 
42 private:
43     struct Axis {
AxisAxis44         explicit Axis(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo, float scale,
45                       float offset, float min, float max, float flat, float fuzz, float resolution,
46                       float filter)
47               : rawAxisInfo(rawAxisInfo),
48                 axisInfo(axisInfo),
49                 scale(scale),
50                 offset(offset),
51                 min(min),
52                 max(max),
53                 flat(flat),
54                 fuzz(fuzz),
55                 resolution(resolution),
56                 filter(filter) {
57             resetValue();
58         }
59 
60         RawAbsoluteAxisInfo rawAxisInfo;
61         AxisInfo axisInfo;
62 
63         float scale;  // scale factor from raw to normalized values
64         float offset; // offset to add after scaling for normalization
65 
66         float min;        // normalized inclusive minimum
67         float max;        // normalized inclusive maximum
68         float flat;       // normalized flat region size
69         float fuzz;       // normalized error tolerance
70         float resolution; // normalized resolution in units
71 
72         float filter;       // filter out small variations of this size
73         float currentValue; // current value
74         float newValue;     // most recent value
75 
resetValueAxis76         void resetValue() {
77             this->currentValue = 0;
78             this->newValue = 0;
79         }
80     };
81 
82     struct Sensor {
SensorSensor83         explicit Sensor(const InputDeviceSensorInfo& sensorInfo) : sensorInfo(sensorInfo) {
84             resetValue();
85         }
86         bool enabled;
87         InputDeviceSensorAccuracy accuracy;
88         std::chrono::nanoseconds samplingPeriod;
89         std::chrono::nanoseconds maxBatchReportLatency;
90         // last sample time in nano seconds
91         std::optional<nsecs_t> lastSampleTimeNs;
92         InputDeviceSensorInfo sensorInfo;
93         // Sensor X, Y, Z data mapping to abs
94         std::array<int32_t, SENSOR_VEC_LEN> dataVec;
resetValueSensor95         void resetValue() {
96             this->enabled = false;
97             this->accuracy = InputDeviceSensorAccuracy::ACCURACY_NONE;
98             this->samplingPeriod = std::chrono::nanoseconds(0);
99             this->maxBatchReportLatency = std::chrono::nanoseconds(0);
100             this->lastSampleTimeNs = std::nullopt;
101         }
102     };
103 
104     static Axis createAxis(const AxisInfo& AxisInfo, const RawAbsoluteAxisInfo& rawAxisInfo);
105 
106     // Axes indexed by raw ABS_* axis index.
107     std::unordered_map<int32_t, Axis> mAxes;
108 
109     // hardware timestamp from MSC_TIMESTAMP
110     nsecs_t mHardwareTimestamp;
111     uint32_t mPrevMscTime;
112 
113     bool mDeviceEnabled;
114     // Does device support MSC_TIMESTAMP
115     bool mHasHardwareTimestamp;
116 
117     // Sensor list
118     std::unordered_map<InputDeviceSensorType, Sensor> mSensors;
119 
120     void sync(nsecs_t when, bool force);
121 
122     template <typename T>
123     bool tryGetProperty(std::string keyName, T& outValue);
124 
125     void parseSensorConfiguration(InputDeviceSensorType sensorType, int32_t absCode,
126                                   int32_t sensorDataIndex, const Axis& axis);
127 
128     void processHardWareTimestamp(nsecs_t evTime, int32_t evValue);
129 
130     Sensor createSensor(InputDeviceSensorType sensorType, const Axis& axis);
131 
132     bool setSensorEnabled(InputDeviceSensorType sensorType, bool enabled);
133 };
134 
135 } // namespace android
136 
137 #endif // _UI_INPUTREADER_SENSOR_INPUT_MAPPER_H