OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/midi/usb_midi_descriptor_parser.h" | 5 #include "media/midi/usb_midi_descriptor_parser.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 | 10 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 size_t size, | 72 size_t size, |
73 std::vector<UsbMidiJack>* jacks) { | 73 std::vector<UsbMidiJack>* jacks) { |
74 jacks->clear(); | 74 jacks->clear(); |
75 bool result = ParseInternal(device, data, size, jacks); | 75 bool result = ParseInternal(device, data, size, jacks); |
76 if (!result) | 76 if (!result) |
77 jacks->clear(); | 77 jacks->clear(); |
78 Clear(); | 78 Clear(); |
79 return result; | 79 return result; |
80 } | 80 } |
81 | 81 |
82 bool UsbMidiDescriptorParser::ParseDeviceInfo( | |
83 const uint8* data, size_t size, DeviceInfo* info) { | |
84 for (const uint8* current = data; | |
85 current < data + size; | |
86 current += current[0]) { | |
87 uint8 length = current[0]; | |
88 if (length < 2) { | |
89 DVLOG(1) << "Descriptor Type is not accessible."; | |
90 return false; | |
91 } | |
92 if (current + length > data + size) { | |
93 DVLOG(1) << "The header size is incorrect."; | |
94 return false; | |
95 } | |
96 DescriptorType descriptor_type = static_cast<DescriptorType>(current[1]); | |
97 if (descriptor_type != TYPE_DEVICE) | |
98 continue; | |
99 bool result = ParseDevice(current, length, info); | |
100 if (!result) | |
101 *info = DeviceInfo(); | |
Takashi Toyoshima
2015/04/22 05:25:13
Shall we initialize *info always at the beginning
yhirano
2015/04/22 06:11:09
You're right, thanks. Done.
| |
102 return result; | |
103 } | |
104 // No DEVICE descriptor is found. | |
105 *info = DeviceInfo(); | |
106 return false; | |
107 } | |
108 | |
82 bool UsbMidiDescriptorParser::ParseInternal(UsbMidiDevice* device, | 109 bool UsbMidiDescriptorParser::ParseInternal(UsbMidiDevice* device, |
83 const uint8* data, | 110 const uint8* data, |
84 size_t size, | 111 size_t size, |
85 std::vector<UsbMidiJack>* jacks) { | 112 std::vector<UsbMidiJack>* jacks) { |
86 for (const uint8* current = data; | 113 for (const uint8* current = data; |
87 current < data + size; | 114 current < data + size; |
88 current += current[0]) { | 115 current += current[0]) { |
89 uint8 length = current[0]; | 116 uint8 length = current[0]; |
90 if (length < 2) { | 117 if (length < 2) { |
91 DVLOG(1) << "Descriptor Type is not accessible."; | 118 DVLOG(1) << "Descriptor Type is not accessible."; |
(...skipping 30 matching lines...) Expand all Loading... | |
122 return false; | 149 return false; |
123 break; | 150 break; |
124 default: | 151 default: |
125 // Ignore uninteresting types. | 152 // Ignore uninteresting types. |
126 break; | 153 break; |
127 } | 154 } |
128 } | 155 } |
129 return true; | 156 return true; |
130 } | 157 } |
131 | 158 |
159 bool UsbMidiDescriptorParser::ParseDevice( | |
160 const uint8* data, size_t size, DeviceInfo* info) { | |
161 if (size < 0x12) { | |
162 DVLOG(1) << "DEVICE header size is incorrect."; | |
163 return false; | |
164 } | |
165 | |
166 info->vendor_id = data[8] | (data[9] << 8); | |
167 info->product_id = data[0xa] | (data[0xb] << 8); | |
168 info->bcd_device_version = data[0xc] | (data[0xd] << 8); | |
169 info->manufacturer_index = data[0xe]; | |
170 info->product_index = data[0xf]; | |
171 return true; | |
172 } | |
173 | |
132 bool UsbMidiDescriptorParser::ParseInterface(const uint8* data, size_t size) { | 174 bool UsbMidiDescriptorParser::ParseInterface(const uint8* data, size_t size) { |
133 if (size != 9) { | 175 if (size != 9) { |
134 DVLOG(1) << "INTERFACE header size is incorrect."; | 176 DVLOG(1) << "INTERFACE header size is incorrect."; |
135 return false; | 177 return false; |
136 } | 178 } |
137 incomplete_jacks_.clear(); | 179 incomplete_jacks_.clear(); |
138 | 180 |
139 uint8 interface_class = data[5]; | 181 uint8 interface_class = data[5]; |
140 uint8 interface_subclass = data[6]; | 182 uint8 interface_subclass = data[6]; |
141 | 183 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 } | 268 } |
227 | 269 |
228 void UsbMidiDescriptorParser::Clear() { | 270 void UsbMidiDescriptorParser::Clear() { |
229 is_parsing_usb_midi_interface_ = false; | 271 is_parsing_usb_midi_interface_ = false; |
230 current_endpoint_address_ = 0; | 272 current_endpoint_address_ = 0; |
231 current_cable_number_ = 0; | 273 current_cable_number_ = 0; |
232 incomplete_jacks_.clear(); | 274 incomplete_jacks_.clear(); |
233 } | 275 } |
234 | 276 |
235 } // namespace media | 277 } // namespace media |
OLD | NEW |