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