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.
5 #include "device/hid/hid_report_descriptor_item.h"
9 #include "base/logging.h"
10 #include "device/hid/hid_usage_and_page.h"
24 HidReportDescriptorItem::HidReportDescriptorItem(
26 HidReportDescriptorItem
* previous
)
27 : previous_(previous
), next_(NULL
), parent_(NULL
), shortData_(0) {
28 Header
* header
= (Header
*)&bytes
[0];
29 tag_
= (Tag
)(header
->tag
<< 2 | header
->type
);
32 // In a long item, payload size is the second byte.
33 payload_size_
= bytes
[1];
35 payload_size_
= header
->size
;
36 DCHECK(payload_size_
<= sizeof(shortData_
));
37 memcpy(&shortData_
, &bytes
[GetHeaderSize()], payload_size());
41 DCHECK(!previous
->next_
);
42 previous
->next_
= this;
43 switch (previous
->tag()) {
52 case kTagEndCollection
:
53 if (previous
->parent()) {
54 parent_
= previous
->parent()->parent();
58 parent_
= previous
->parent();
65 size_t HidReportDescriptorItem::GetDepth() const {
66 HidReportDescriptorItem
* parent_item
= parent();
68 return parent_item
->GetDepth() + 1;
72 bool HidReportDescriptorItem::IsLong() const { return tag() == kTagLong
; }
74 size_t HidReportDescriptorItem::GetHeaderSize() const {
75 return IsLong() ? 3 : 1;
78 size_t HidReportDescriptorItem::GetSize() const {
79 return GetHeaderSize() + payload_size();
82 uint32_t HidReportDescriptorItem::GetShortData() const {
87 HidReportDescriptorItem::CollectionType
88 HidReportDescriptorItem::GetCollectionTypeFromValue(uint32_t value
) {
91 return kCollectionTypePhysical
;
93 return kCollectionTypeApplication
;
95 return kCollectionTypeLogical
;
97 return kCollectionTypeReport
;
99 return kCollectionTypeNamedArray
;
101 return kCollectionTypeUsageSwitch
;
103 return kCollectionTypeUsageModifier
;
107 if (0x80 < value
&& value
< 0xFF)
108 return kCollectionTypeVendor
;
109 return kCollectionTypeReserved
;
112 } // namespace device