Add ICU message format support
[chromium-blink-merge.git] / device / hid / hid_report_descriptor_item.cc
blob60ba764bc93b682046749f756f93faa22cabe65b
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"
7 #include <stdlib.h>
9 #include "base/logging.h"
10 #include "device/hid/hid_usage_and_page.h"
12 namespace device {
14 namespace {
16 struct Header {
17 uint8_t size : 2;
18 uint8_t type : 2;
19 uint8_t tag : 4;
22 } // namespace
24 HidReportDescriptorItem::HidReportDescriptorItem(
25 const uint8_t* bytes,
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);
31 if (IsLong()) {
32 // In a long item, payload size is the second byte.
33 payload_size_ = bytes[1];
34 } else {
35 payload_size_ = header->size;
36 DCHECK(payload_size_ <= sizeof(shortData_));
37 memcpy(&shortData_, &bytes[GetHeaderSize()], payload_size());
40 if (previous) {
41 DCHECK(!previous->next_);
42 previous->next_ = this;
43 switch (previous->tag()) {
44 case kTagCollection:
45 parent_ = previous;
46 break;
47 default:
48 break;
50 if (!parent_) {
51 switch (tag()) {
52 case kTagEndCollection:
53 if (previous->parent()) {
54 parent_ = previous->parent()->parent();
56 break;
57 default:
58 parent_ = previous->parent();
59 break;
65 size_t HidReportDescriptorItem::GetDepth() const {
66 HidReportDescriptorItem* parent_item = parent();
67 if (parent_item)
68 return parent_item->GetDepth() + 1;
69 return 0;
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 {
83 DCHECK(!IsLong());
84 return shortData_;
87 HidReportDescriptorItem::CollectionType
88 HidReportDescriptorItem::GetCollectionTypeFromValue(uint32_t value) {
89 switch (value) {
90 case 0x00:
91 return kCollectionTypePhysical;
92 case 0x01:
93 return kCollectionTypeApplication;
94 case 0x02:
95 return kCollectionTypeLogical;
96 case 0x03:
97 return kCollectionTypeReport;
98 case 0x04:
99 return kCollectionTypeNamedArray;
100 case 0x05:
101 return kCollectionTypeUsageSwitch;
102 case 0x06:
103 return kCollectionTypeUsageModifier;
104 default:
105 break;
107 if (0x80 < value && value < 0xFF)
108 return kCollectionTypeVendor;
109 return kCollectionTypeReserved;
112 } // namespace device