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 // get head tracker sensor event data 50 bool getHeadTrackerEventData(const std::vector<uint8_t> &message, 51 sensors_event_t *event); 52 53 // get generic sensor event data 54 bool getSensorEventData(const std::vector<uint8_t> &message, 55 sensors_event_t *event); 56 57 // indicate if the HidRawSensor is a valid one isValid()58 bool isValid() const { return mValid; }; 59 60 private: 61 62 // structure used for holding descriptor parse result for each report field 63 enum { 64 TYPE_FLOAT, 65 TYPE_INT64, 66 TYPE_ACCURACY 67 }; 68 struct ReportTranslateRecord { 69 int type; 70 int index; 71 int64_t maxValue; 72 int64_t minValue; 73 size_t byteOffset; 74 size_t byteSize; 75 double a; 76 int64_t b; 77 }; 78 79 // sensor related information parsed from HID descriptor 80 struct FeatureValue { 81 // information needed to furnish sensor_t structure (see hardware/sensors.h) 82 std::string name; 83 std::string vendor; 84 std::string permission; 85 std::string typeString; 86 int32_t type; 87 int version; 88 float maxRange; 89 float resolution; 90 float power; 91 int32_t minDelay; 92 int64_t maxDelay; 93 size_t fifoSize; 94 size_t fifoMaxSize; 95 uint32_t reportModeFlag; 96 bool isWakeUp; 97 bool useUniqueIdForUuid; 98 99 // dynamic sensor specific 100 std::string uniqueId; 101 uint8_t uuid[16]; 102 103 // if the device is custom sensor HID device that furnished android specific descriptors 104 bool isAndroidCustom; 105 }; 106 107 // helper function to find the first report item with specified usage, type and id. 108 // if parameter id is omitted, this function looks for usage with all ids. 109 // return nullptr if nothing is found. 110 static const HidParser::ReportItem* find 111 (const std::vector<HidParser::ReportPacket> &packets, 112 unsigned int usage, int type, int id = -1); 113 114 // helper function to decode std::string from HID feature report buffer. 115 static bool decodeString( 116 const HidParser::ReportItem &report, 117 const std::vector<uint8_t> &buffer, std::string *d); 118 119 // initialize default feature values default based on hid device info 120 static void initFeatureValueFromHidDeviceInfo( 121 FeatureValue *featureValue, const HidDevice::HidDeviceInfo &info); 122 123 // populates feature values from descripitors and hid feature reports 124 bool populateFeatureValueFromFeatureReport( 125 FeatureValue *featureValue, const std::vector<HidParser::ReportPacket> &packets); 126 127 // validate feature values and construct sensor_t structure if values are ok. 128 bool validateFeatureValueAndBuildSensor(); 129 130 // helper function to find sensor control feature usage from packets 131 bool findSensorControlUsage(const std::vector<HidParser::ReportPacket> &packets); 132 133 // try to parse sensor description feature value to see if it matches any 134 // known sensors 135 void detectSensorFromDescription(const std::string &description); 136 137 // try to parse sensor description feature value to see if it matches the 138 // Android header tracker sensor 139 bool detectAndroidHeadTrackerSensor(const std::string &description); 140 141 // try to parse sensor description feature value to see if it matches 142 // android specified custom sensor definition. 143 bool detectAndroidCustomSensor(const std::string &description); 144 145 // process HID sensor spec defined three axis sensors usages: accel, gyro, mag. 146 bool processTriAxisUsage(const std::vector<HidParser::ReportPacket> &packets, 147 uint32_t usageX, uint32_t usageY, uint32_t usageZ, double defaultScaling = 1); 148 149 // process HID snesor spec defined orientation(quaternion) sensor usages. 150 bool processQuaternionUsage(const std::vector<HidParser::ReportPacket> &packets); 151 152 bool setLeAudioTransport(const SP(HidDevice) &device, bool enable); 153 bool setPower(const SP(HidDevice) &device, bool enable); 154 bool setReportingState(const SP(HidDevice) &device, bool enable); 155 156 // get the value of a report field 157 template<typename ValueType> getReportFieldValue(const std::vector<uint8_t> & message,ReportTranslateRecord * rec,ValueType * value)158 bool getReportFieldValue(const std::vector<uint8_t> &message, 159 ReportTranslateRecord* rec, ValueType* value) { 160 bool valid = true; 161 int64_t v = 0; 162 if (rec->minValue < 0) { 163 v = (message[rec->byteOffset + rec->byteSize - 1] & 0x80) ? -1 : 0; 164 } 165 166 for (int i = static_cast<int>(rec->byteSize) - 1; i >= 0; --i) { 167 v = (v << 8) | message[rec->byteOffset + i]; // HID is little endian 168 } 169 if (v > rec->maxValue || v < rec->minValue) { 170 valid = false; 171 } 172 173 switch (rec->type) { 174 case TYPE_FLOAT: 175 *value = rec->a * (v + rec->b); 176 break; 177 case TYPE_INT64: 178 *value = v + rec->b; 179 break; 180 } 181 182 return valid; 183 } 184 185 // dump data for test/debug purpose 186 std::string dump() const; 187 188 // Features for control sensor 189 int mReportingStateId; 190 unsigned int mReportingStateBitOffset; 191 unsigned int mReportingStateBitSize; 192 int mReportingStateDisableIndex; 193 int mReportingStateEnableIndex; 194 195 int mPowerStateId; 196 unsigned int mPowerStateBitOffset; 197 unsigned int mPowerStateBitSize; 198 int mPowerStateOffIndex; 199 int mPowerStateOnIndex; 200 201 int mReportIntervalId; 202 unsigned int mReportIntervalBitOffset; 203 unsigned int mReportIntervalBitSize; 204 double mReportIntervalScale; 205 int64_t mReportIntervalOffset; 206 207 int mLeTransportId; 208 unsigned int mLeTransportBitOffset; 209 unsigned int mLeTransportBitSize; 210 bool mRequiresLeTransport; 211 int mLeTransportAclIndex; 212 int mLeTransportIsoIndex; 213 214 // Input report translate table 215 std::vector<ReportTranslateRecord> mTranslateTable; 216 unsigned mInputReportId; 217 218 FeatureValue mFeatureInfo; 219 sensor_t mSensor; 220 221 // runtime states variable 222 bool mEnabled; 223 int64_t mSamplingPeriod; // ns 224 int64_t mBatchingPeriod; // ns 225 226 WP(HidDevice) mDevice; 227 bool mValid; 228 229 /** 230 * The first major version which LE audio capabilities are encoded. 231 * For this version, we expect the HID descriptor to be the following format: 232 * #AndroidHeadTracker#<major version>.<minor version>#<capability> 233 * where capability is an integer that defines where LE audio supported 234 * transports are indicated: 235 * - 1: ACL 236 * - 2: ISO 237 * - 3: ACL + ISO 238 */ 239 const uint8_t kLeAudioCapabilitiesMajorVersion = 2; 240 const uint8_t kAclBitMask = 0x1; 241 const uint8_t kIsoBitMask = 0x2; 242 }; 243 244 } // namespace SensorHalExt 245 } // namespace android 246 #endif // ANDROID_SENSORHAL_EXT_HIDRAW_SENSOR_H 247 248