Home | History | Annotate | Download | only in hid
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef DEVICE_HID_HID_REPORT_DESCRIPTOR_ITEM_H_
      6 #define DEVICE_HID_HID_REPORT_DESCRIPTOR_ITEM_H_
      7 
      8 #include "base/basictypes.h"
      9 
     10 namespace device {
     11 
     12 // An element of a HID report descriptor.
     13 class HidReportDescriptorItem {
     14  private:
     15   friend class HidReportDescriptor;
     16 
     17   enum Type {
     18     kTypeMain = 0,
     19     kTypeGlobal = 1,
     20     kTypeLocal = 2,
     21     kTypeReserved = 3
     22   };
     23 
     24   enum MainTag {
     25     kMainTagDefault = 0x00,       // 0000
     26     kMainTagInput = 0x08,         // 1000
     27     kMainTagOutput = 0x09,        // 1001
     28     kMainTagFeature = 0x0B,       // 1011
     29     kMainTagCollection = 0x0A,    // 1010
     30     kMainTagEndCollection = 0x0C  // 1100
     31   };
     32 
     33   enum GlobalTag {
     34     kGlobalTagUsagePage = 0x00,        // 0000
     35     kGlobalTagLogicalMinimum = 0x01,   // 0001
     36     kGlobalTagLogicalMaximum = 0x02,   // 0010
     37     kGlobalTagPhysicalMinimum = 0x03,  // 0011
     38     kGlobalTagPhysicalMaximum = 0x04,  // 0100
     39     kGlobalTagUnitExponent = 0x05,     // 0101
     40     kGlobalTagUnit = 0x06,             // 0110
     41     kGlobalTagReportSize = 0x07,       // 0111
     42     kGlobalTagReportId = 0x08,         // 1000
     43     kGlobalTagReportCount = 0x09,      // 1001
     44     kGlobalTagPush = 0x0A,             // 1010
     45     kGlobalTagPop = 0x0B               // 1011
     46   };
     47 
     48   enum LocalTag {
     49     kLocalTagUsage = 0x00,              // 0000
     50     kLocalTagUsageMinimum = 0x01,       // 0001
     51     kLocalTagUsageMaximum = 0x02,       // 0010
     52     kLocalTagDesignatorIndex = 0x03,    // 0011
     53     kLocalTagDesignatorMinimum = 0x04,  // 0100
     54     kLocalTagDesignatorMaximum = 0x05,  // 0101
     55     kLocalTagStringIndex = 0x07,        // 0111
     56     kLocalTagStringMinimum = 0x08,      // 1000
     57     kLocalTagStringMaximum = 0x09,      // 1001
     58     kLocalTagDelimiter = 0x0A           // 1010
     59   };
     60 
     61   enum ReservedTag {
     62     kReservedTagLong = 0xF  // 1111
     63   };
     64 
     65  public:
     66   enum Tag {
     67     kTagDefault = kMainTagDefault << 2 | kTypeMain,
     68     kTagInput = kMainTagInput << 2 | kTypeMain,
     69     kTagOutput = kMainTagOutput << 2 | kTypeMain,
     70     kTagFeature = kMainTagFeature << 2 | kTypeMain,
     71     kTagCollection = kMainTagCollection << 2 | kTypeMain,
     72     kTagEndCollection = kMainTagEndCollection << 2 | kTypeMain,
     73     kTagUsagePage = kGlobalTagUsagePage << 2 | kTypeGlobal,
     74     kTagLogicalMinimum = kGlobalTagLogicalMinimum << 2 | kTypeGlobal,
     75     kTagLogicalMaximum = kGlobalTagLogicalMaximum << 2 | kTypeGlobal,
     76     kTagPhysicalMinimum = kGlobalTagPhysicalMinimum << 2 | kTypeGlobal,
     77     kTagPhysicalMaximum = kGlobalTagPhysicalMaximum << 2 | kTypeGlobal,
     78     kTagUnitExponent = kGlobalTagUnitExponent << 2 | kTypeGlobal,
     79     kTagUnit = kGlobalTagUnit << 2 | kTypeGlobal,
     80     kTagReportSize = kGlobalTagReportSize << 2 | kTypeGlobal,
     81     kTagReportId = kGlobalTagReportId << 2 | kTypeGlobal,
     82     kTagReportCount = kGlobalTagReportCount << 2 | kTypeGlobal,
     83     kTagPush = kGlobalTagPush << 2 | kTypeGlobal,
     84     kTagPop = kGlobalTagPop << 2 | kTypeGlobal,
     85     kTagUsage = kLocalTagUsage << 2 | kTypeLocal,
     86     kTagUsageMinimum = kLocalTagUsageMinimum << 2 | kTypeLocal,
     87     kTagUsageMaximum = kLocalTagUsageMaximum << 2 | kTypeLocal,
     88     kTagDesignatorIndex = kLocalTagDesignatorIndex << 2 | kTypeLocal,
     89     kTagDesignatorMinimum = kLocalTagDesignatorMinimum << 2 | kTypeLocal,
     90     kTagDesignatorMaximum = kLocalTagDesignatorMaximum << 2 | kTypeLocal,
     91     kTagStringIndex = kLocalTagStringIndex << 2 | kTypeLocal,
     92     kTagStringMinimum = kLocalTagStringMinimum << 2 | kTypeLocal,
     93     kTagStringMaximum = kLocalTagStringMaximum << 2 | kTypeLocal,
     94     kTagDelimiter = kLocalTagDelimiter << 2 | kTypeLocal,
     95     kTagLong = kReservedTagLong << 2 | kTypeReserved
     96   };
     97 
     98   // HID Input/Output/Feature report information.
     99   // Can be retrieved from GetShortData()
    100   // when item.tag() == HidReportDescriptorItem::kTagInput
    101   // or HidReportDescriptorItem::kTagOutput
    102   // or HidReportDescriptorItem::kTagFeature
    103   struct ReportInfo {
    104     uint8_t data_or_constant : 1;
    105     uint8_t array_or_variable : 1;
    106     uint8_t absolute_or_relative : 1;
    107     uint8_t wrap : 1;
    108     uint8_t linear : 1;
    109     uint8_t preferred : 1;
    110     uint8_t null : 1;
    111     uint8_t reserved_1 : 1;
    112     uint8_t bit_field_or_buffer : 1;
    113     uint8_t reserved_2 : 1;
    114   };
    115 
    116   // HID collection type.
    117   // Can be retrieved from GetShortData()
    118   // when item.tag() == HidReportDescriptorItem::kTagCollection
    119   enum CollectionType {
    120     kCollectionTypePhysical,
    121     kCollectionTypeApplication,
    122     kCollectionTypeLogical,
    123     kCollectionTypeReport,
    124     kCollectionTypeNamedArray,
    125     kCollectionTypeUsageSwitch,
    126     kCollectionTypeUsageModifier,
    127     kCollectionTypeReserved,
    128     kCollectionTypeVendor
    129   };
    130 
    131  private:
    132   HidReportDescriptorItem(const uint8_t* bytes,
    133                           HidReportDescriptorItem* previous);
    134 
    135  public:
    136   ~HidReportDescriptorItem() {}
    137 
    138   // Previous element in report descriptor.
    139   // Owned by descriptor instance.
    140   HidReportDescriptorItem* previous() const {
    141     return previous_;
    142   };
    143   // Next element in report descriptor.
    144   // Owned by descriptor instance.
    145   HidReportDescriptorItem* next() const {
    146     return next_;
    147   };
    148   // Parent element in report descriptor.
    149   // Owned by descriptor instance.
    150   // Can be NULL.
    151   HidReportDescriptorItem* parent() const {
    152     return parent_;
    153   };
    154   // Level in Parent-Children relationship tree.
    155   // 0 for top-level items (parent()==NULL).
    156   // 1 if parent() is top-level.
    157   // 2 if parent() has a top-level parent. Etc.
    158   size_t GetDepth() const;
    159   Tag tag() const { return tag_; }
    160   // Returns true for a long item, false otherwise.
    161   bool IsLong() const;
    162   // Raw data of a short item.
    163   // Not valid for a long item.
    164   uint32_t GetShortData() const;
    165 
    166   static CollectionType GetCollectionTypeFromValue(uint32_t value);
    167 
    168  private:
    169   size_t GetHeaderSize() const;
    170   size_t payload_size() const { return payload_size_; }
    171   size_t GetSize() const;
    172 
    173   HidReportDescriptorItem* previous_;
    174   HidReportDescriptorItem* next_;
    175   HidReportDescriptorItem* parent_;
    176   Tag tag_;
    177   uint32_t shortData_;
    178   size_t payload_size_;
    179 };
    180 
    181 }  // namespace device
    182 
    183 #endif  // DEVICE_HID_HID_REPORT_DESCRIPTOR_ITEM_H_
    184