Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 #include "device/hid/hid_report_descriptor.h" | |
| 6 | |
| 7 #include <stdlib.h> | |
| 8 | |
| 9 #include <algorithm> | |
| 10 | |
| 11 using namespace std; | |
| 12 | |
| 13 namespace device { | |
| 14 | |
| 15 HidReportDescriptorItem::HidReportDescriptorItem( | |
| 16 const uint8_t* bytes, | |
| 17 HidReportDescriptorItem* previous) | |
| 18 : bytes_(bytes) | |
|
Ken Rockot(use gerrit already)
2014/04/15 17:53:38
Please, commas at the end of lines not at the begi
| |
| 19 , previous_(previous) | |
| 20 , parent_(NULL) { | |
| 21 | |
|
Ken Rockot(use gerrit already)
2014/04/15 17:53:38
Please remove some of this extra vertical whitespa
| |
| 22 if (previous) { | |
| 23 | |
|
Ken Rockot(use gerrit already)
2014/04/15 17:53:38
Whitespace
| |
| 24 previous->next_ = this; | |
| 25 | |
|
Ken Rockot(use gerrit already)
2014/04/15 17:53:38
Whitespace
| |
| 26 switch (previous->tag()) { | |
| 27 case kTagCollection: | |
| 28 parent_ = previous; | |
| 29 break; | |
| 30 default: | |
| 31 break; | |
| 32 } | |
| 33 | |
|
Ken Rockot(use gerrit already)
2014/04/15 17:53:38
etc...
| |
| 34 if (!parent_) { | |
| 35 switch (tag()) { | |
| 36 case kTagEndCollection: | |
| 37 if (previous->parent()) { | |
| 38 parent_ = previous->parent()->parent(); | |
| 39 } | |
| 40 break; | |
| 41 default: | |
| 42 parent_ = previous->parent(); | |
| 43 break; | |
| 44 } | |
| 45 } | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 HidReportDescriptorItem::~HidReportDescriptorItem() {} | |
| 50 | |
| 51 size_t HidReportDescriptorItem::depth() const { | |
| 52 HidReportDescriptorItem* parnt = parent(); | |
| 53 if (parnt) | |
|
Ken Rockot(use gerrit already)
2014/04/15 17:53:38
Please do not use abbreviated names like this. par
| |
| 54 return parnt->depth() + 1; | |
| 55 return 0; | |
| 56 } | |
| 57 | |
| 58 bool HidReportDescriptorItem::isLong() const { | |
| 59 return tag() == kTagLong; | |
| 60 } | |
| 61 | |
| 62 size_t HidReportDescriptorItem::headerSize() const { | |
| 63 return isLong() ? 3 : 1; | |
| 64 } | |
| 65 | |
| 66 size_t HidReportDescriptorItem::payloadSize() const { | |
| 67 return isLong() ? longHeader()->data_size : header()->size; | |
| 68 } | |
| 69 | |
| 70 size_t HidReportDescriptorItem::size() const { | |
| 71 return headerSize() + payloadSize(); | |
| 72 } | |
| 73 | |
| 74 HidReportDescriptorItem::Header* HidReportDescriptorItem::header() const { | |
| 75 return (HidReportDescriptorItem::Header*)(&bytes_[0]); | |
| 76 } | |
| 77 | |
| 78 HidReportDescriptorItem::LongHeader* | |
| 79 HidReportDescriptorItem::longHeader() const { | |
| 80 DCHECK(isLong()); | |
| 81 return (HidReportDescriptorItem::LongHeader*)(&bytes_[0]); | |
| 82 } | |
| 83 | |
| 84 HidReportDescriptorItem::Tag HidReportDescriptorItem::tag() const { | |
| 85 return (HidReportDescriptorItem::Tag)(header()->tag << 2 | header()->type); | |
| 86 } | |
| 87 | |
| 88 HidReportDescriptorItem::Data* HidReportDescriptorItem::data() const { | |
| 89 void *data = malloc(sizeof(HidReportDescriptorItem::Data)); | |
| 90 memset(data, 0, sizeof(data)); | |
| 91 memcpy(data, &bytes_[headerSize()], payloadSize()); | |
| 92 return (HidReportDescriptorItem::Data*)data; | |
| 93 } | |
| 94 | |
| 95 HidReportDescriptor::HidReportDescriptor(const uint8_t *bytes, size_t size) | |
| 96 : bytes_(bytes) | |
| 97 , size_(size) { | |
| 98 size_t header_index = 0; | |
| 99 HidReportDescriptorItem *item = NULL; | |
| 100 while (header_index < size_) { | |
| 101 item = new HidReportDescriptorItem(&bytes_[header_index], item); | |
| 102 items_.push_back(item); | |
| 103 header_index += item->size(); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 const char HidReportDescriptor::kIndentChar = ' '; | |
| 108 | |
| 109 HidReportDescriptor::~HidReportDescriptor() { | |
| 110 for (vector<HidReportDescriptorItem*>::const_iterator items_iter = | |
| 111 items_.begin(); | |
| 112 items_iter != items_.end(); | |
| 113 ++items_iter) { | |
| 114 delete *items_iter; | |
| 115 } | |
| 116 items_.clear(); | |
| 117 } | |
| 118 | |
| 119 const std::vector<HidReportDescriptorItem*>& | |
| 120 HidReportDescriptor::items() const { | |
| 121 return items_; | |
| 122 } | |
| 123 | |
| 124 std::vector<HidUsageAndPage> HidReportDescriptor::topLevelCollections() const { | |
| 125 | |
| 126 std::vector<HidUsageAndPage> tlcs; | |
| 127 | |
| 128 for (std::vector<HidReportDescriptorItem*>::const_iterator items_iter = | |
| 129 items_.begin(); | |
| 130 items_iter != items_.end(); | |
| 131 ++items_iter) { | |
| 132 HidReportDescriptorItem* item = *items_iter; | |
| 133 | |
| 134 bool isTopLevelCollection = | |
| 135 item->tag() == HidReportDescriptorItem::kTagCollection | |
| 136 && item->parent() == NULL; | |
| 137 | |
| 138 if (isTopLevelCollection) { | |
| 139 HidReportDescriptorItem* usage = item->previous(); | |
| 140 HidReportDescriptorItem* usage_page = usage->previous(); | |
| 141 | |
| 142 HidUsageAndPage usage_and_page; | |
| 143 if (usage && usage->tag() | |
| 144 == HidReportDescriptorItem::kTagUsage) { | |
| 145 usage_and_page.usage | |
| 146 = ((HidReportDescriptorItem::Usage*)usage->data())->value; | |
| 147 } | |
| 148 if (usage_page && usage_page->tag() | |
| 149 == HidReportDescriptorItem::kTagUsagePage) { | |
| 150 usage_and_page.usage_page | |
| 151 = ((HidReportDescriptorItem::UsagePage*)usage_page->data())->value; | |
| 152 } | |
| 153 tlcs.push_back(usage_and_page); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 return tlcs; | |
| 158 } | |
| 159 | |
| 160 } // namespace device | |
| OLD | NEW |