summaryrefslogtreecommitdiffstats
path: root/device/hid/hid_report_descriptor_item.cc
blob: 60ba764bc93b682046749f756f93faa22cabe65b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "device/hid/hid_report_descriptor_item.h"

#include <stdlib.h>

#include "base/logging.h"
#include "device/hid/hid_usage_and_page.h"

namespace device {

namespace {

struct Header {
  uint8_t size : 2;
  uint8_t type : 2;
  uint8_t tag : 4;
};

}  // namespace

HidReportDescriptorItem::HidReportDescriptorItem(
    const uint8_t* bytes,
    HidReportDescriptorItem* previous)
    : previous_(previous), next_(NULL), parent_(NULL), shortData_(0) {
  Header* header = (Header*)&bytes[0];
  tag_ = (Tag)(header->tag << 2 | header->type);

  if (IsLong()) {
    // In a long item, payload size is the second byte.
    payload_size_ = bytes[1];
  } else {
    payload_size_ = header->size;
    DCHECK(payload_size_ <= sizeof(shortData_));
    memcpy(&shortData_, &bytes[GetHeaderSize()], payload_size());
  }

  if (previous) {
    DCHECK(!previous->next_);
    previous->next_ = this;
    switch (previous->tag()) {
      case kTagCollection:
        parent_ = previous;
        break;
      default:
        break;
    }
    if (!parent_) {
      switch (tag()) {
        case kTagEndCollection:
          if (previous->parent()) {
            parent_ = previous->parent()->parent();
          }
          break;
        default:
          parent_ = previous->parent();
          break;
      }
    }
  }
}

size_t HidReportDescriptorItem::GetDepth() const {
  HidReportDescriptorItem* parent_item = parent();
  if (parent_item)
    return parent_item->GetDepth() + 1;
  return 0;
}

bool HidReportDescriptorItem::IsLong() const { return tag() == kTagLong; }

size_t HidReportDescriptorItem::GetHeaderSize() const {
  return IsLong() ? 3 : 1;
}

size_t HidReportDescriptorItem::GetSize() const {
  return GetHeaderSize() + payload_size();
}

uint32_t HidReportDescriptorItem::GetShortData() const {
  DCHECK(!IsLong());
  return shortData_;
}

HidReportDescriptorItem::CollectionType
HidReportDescriptorItem::GetCollectionTypeFromValue(uint32_t value) {
  switch (value) {
    case 0x00:
      return kCollectionTypePhysical;
    case 0x01:
      return kCollectionTypeApplication;
    case 0x02:
      return kCollectionTypeLogical;
    case 0x03:
      return kCollectionTypeReport;
    case 0x04:
      return kCollectionTypeNamedArray;
    case 0x05:
      return kCollectionTypeUsageSwitch;
    case 0x06:
      return kCollectionTypeUsageModifier;
    default:
      break;
  }
  if (0x80 < value && value < 0xFF)
    return kCollectionTypeVendor;
  return kCollectionTypeReserved;
}

}  // namespace device