1 /* 2 * Copyright (C) 2017 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 #ifndef ANDROID_SENSORHAL_EXT_HIDRAW_SENSOR_H 17 #define ANDROID_SENSORHAL_EXT_HIDRAW_SENSOR_H 18 19 #include "BaseSensorObject.h" 20 #include "HidDevice.h" 21 #include "Utils.h" 22 23 #include <HidParser.h> 24 #include <hardware/sensors.h> 25 26 namespace android { 27 namespace SensorHalExt { 28 29 using HidUtil::HidParser; 30 using ReportPacket = HidParser::ReportPacket; 31 using ReportItem = HidParser::ReportItem; 32 33 class HidRawSensor : public BaseSensorObject { 34 friend class HidRawSensorTest; 35 friend class HidRawDeviceTest; 36 public: 37 HidRawSensor(SP(HidDevice) device, uint32_t usage, 38 const std::vector<HidParser::ReportPacket> &report); 39 40 // implements BaseSensorObject 41 virtual const sensor_t* getSensor() const; 42 virtual void getUuid(uint8_t* uuid) const; 43 virtual int enable(bool enable); 44 virtual int batch(int64_t samplePeriod, int64_t batchPeriod); // unit nano-seconds 45 46 // handle input report received 47 void handleInput(uint8_t id, const std::vector<uint8_t> &message); 48 49 // indicate if the HidRawSensor is a valid one isValid()50 bool isValid() const { return mValid; }; 51 52 private: 53 54 // structure used for holding descriptor parse result for each report field 55 enum { 56 TYPE_FLOAT, 57 TYPE_INT64, 58 TYPE_ACCURACY 59 }; 60 struct ReportTranslateRecord { 61 int type; 62 int index; 63 int64_t maxValue; 64 int64_t minValue; 65 size_t byteOffset; 66 size_t byteSize; 67 double a; 68 int64_t b; 69 }; 70 71 // sensor related information parsed from HID descriptor 72 struct FeatureValue { 73 // information needed to furnish sensor_t structure (see hardware/sensors.h) 74 std::string name; 75 std::string vendor; 76 std::string permission; 77 std::string typeString; 78 int32_t type; 79 int version; 80 float maxRange; 81 float resolution; 82 float power; 83 int32_t minDelay; 84 int64_t maxDelay; 85 size_t fifoSize; 86 size_t fifoMaxSize; 87 uint32_t reportModeFlag; 88 bool isWakeUp; 89 90 // dynamic sensor specific 91 std::string uniqueId; 92 uint8_t uuid[16]; 93 94 // if the device is custom sensor HID device that furnished android specific descriptors 95 bool isAndroidCustom; 96 }; 97 98 // helper function to find the first report item with specified usage, type and id. 99 // if parameter id is omitted, this function looks for usage with all ids. 100 // return nullptr if nothing is found. 101 static const HidParser::ReportItem* find 102 (const std::vector<HidParser::ReportPacket> &packets, 103 unsigned int usage, int type, int id = -1); 104 105 // helper function to decode std::string from HID feature report buffer. 106 static bool decodeString( 107 const HidParser::ReportItem &report, 108 const std::vector<uint8_t> &buffer, std::string *d); 109 110 // initialize default feature values default based on hid device info 111 static void initFeatureValueFromHidDeviceInfo( 112 FeatureValue *featureValue, const HidDevice::HidDeviceInfo &info); 113 114 // populates feature values from descripitors and hid feature reports 115 bool populateFeatureValueFromFeatureReport( 116 FeatureValue *featureValue, const std::vector<HidParser::ReportPacket> &packets); 117 118 // validate feature values and construct sensor_t structure if values are ok. 119 bool validateFeatureValueAndBuildSensor(); 120 121 // helper function to find sensor control feature usage from packets 122 bool findSensorControlUsage(const std::vector<HidParser::ReportPacket> &packets); 123 124 // try to parse sensor description feature value to see if it matches any 125 // known sensors 126 void detectSensorFromDescription(const std::string &description); 127 128 // try to parse sensor description feature value to see if it matches the 129 // Android header tracker sensor 130 bool detectAndroidHeadTrackerSensor(const std::string &description); 131 132 // try to parse sensor description feature value to see if it matches 133 // android specified custom sensor definition. 134 bool detectAndroidCustomSensor(const std::string &description); 135 136 // process HID sensor spec defined three axis sensors usages: accel, gyro, mag. 137 bool processTriAxisUsage(const std::vector<HidParser::ReportPacket> &packets, 138 uint32_t usageX, uint32_t usageY, uint32_t usageZ, double defaultScaling = 1); 139 140 // process HID snesor spec defined orientation(quaternion) sensor usages. 141 bool processQuaternionUsage(const std::vector<HidParser::ReportPacket> &packets); 142 143 // dump data for test/debug purpose 144 std::string dump() const; 145 146 // Features for control sensor 147 int mReportingStateId; 148 unsigned int mReportingStateBitOffset; 149 unsigned int mReportingStateBitSize; 150 int mReportingStateDisableIndex; 151 int mReportingStateEnableIndex; 152 153 int mPowerStateId; 154 unsigned int mPowerStateBitOffset; 155 unsigned int mPowerStateBitSize; 156 int mPowerStateOffIndex; 157 int mPowerStateOnIndex; 158 159 int mReportIntervalId; 160 unsigned int mReportIntervalBitOffset; 161 unsigned int mReportIntervalBitSize; 162 163 // Input report translate table 164 std::vector<ReportTranslateRecord> mTranslateTable; 165 unsigned mInputReportId; 166 167 FeatureValue mFeatureInfo; 168 sensor_t mSensor; 169 170 // runtime states variable 171 bool mEnabled; 172 int64_t mSamplingPeriod; // ns 173 int64_t mBatchingPeriod; // ns 174 175 WP(HidDevice) mDevice; 176 bool mValid; 177 }; 178 179 } // namespace SensorHalExt 180 } // namespace android 181 #endif // ANDROID_SENSORHAL_EXT_HIDRAW_SENSOR_H 182 183