Home | History | Annotate | Download | only in dynamic_sensor
      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
     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
    125     // android specified custom sensor definition.
    126     bool detectAndroidCustomSensor(const std::string &description);
    127 
    128     // process HID sensor spec defined three axis sensors usages: accel, gyro, mag.
    129     bool processTriAxisUsage(const std::vector<HidParser::ReportPacket> &packets,
    130             uint32_t usageX, uint32_t usageY, uint32_t usageZ, double defaultScaling = 1);
    131 
    132     // process HID snesor spec defined orientation(quaternion) sensor usages.
    133     bool processQuaternionUsage(const std::vector<HidParser::ReportPacket> &packets);
    134 
    135     // dump data for test/debug purpose
    136     std::string dump() const;
    137 
    138     // Features for control sensor
    139     int mReportingStateId;
    140     unsigned int mReportingStateOffset;
    141 
    142     int mPowerStateId;
    143     unsigned int mPowerStateOffset;
    144 
    145     int mReportIntervalId;
    146     unsigned int mReportIntervalOffset;
    147     unsigned int mReportIntervalSize;
    148 
    149     // Input report translate table
    150     std::vector<ReportTranslateRecord> mTranslateTable;
    151     unsigned mInputReportId;
    152 
    153     FeatureValue mFeatureInfo;
    154     sensor_t mSensor;
    155 
    156     // runtime states variable
    157     bool mEnabled;
    158     int64_t mSamplingPeriod;    // ns
    159     int64_t mBatchingPeriod;    // ns
    160 
    161     WP(HidDevice) mDevice;
    162     bool mValid;
    163 };
    164 
    165 } // namespace SensorHalExt
    166 } // namespace android
    167 #endif // ANDROID_SENSORHAL_EXT_HIDRAW_SENSOR_H
    168 
    169