OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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 "media/midi/usb_midi_descriptor_parser.h" | |
6 | |
7 #include "base/logging.h" | |
8 | |
9 namespace media { | |
10 | |
11 namespace { | |
12 | |
13 // The constants below are specified in USB spec, USB audio spec | |
14 // and USB midi spec. | |
15 | |
16 enum DescriptorType { | |
17 TYPE_DEVICE = 1, | |
18 TYPE_CONFIGURATION = 2, | |
19 TYPE_STRING = 3, | |
20 TYPE_INTERFACE = 4, | |
21 TYPE_ENDPOINT = 5, | |
22 TYPE_DEVICE_QUALIFIER = 6, | |
23 TYPE_OTHER_SPEED_CONFIGURATION = 7, | |
24 TYPE_INTERFACE_POWER = 8, | |
25 | |
26 TYPE_CS_INTERFACE = 36, | |
27 TYPE_CS_ENDPOINT = 37, | |
28 }; | |
29 | |
30 enum DescriptorSubType { | |
31 SUBTYPE_MS_DESCRIPTOR_UNDEFINED = 0, | |
32 SUBTYPE_MS_HEADER = 1, | |
33 SUBTYPE_MIDI_IN_JACK = 2, | |
34 SUBTYPE_MIDI_OUT_JACK = 3, | |
35 SUBTYPE_ELEMENT = 4, | |
36 }; | |
37 | |
38 enum JackType { | |
39 JACK_TYPE_UNDEFINED = 0, | |
40 JACK_TYPE_EMBEDDED = 1, | |
41 JACK_TYPE_EXTERNAL = 2, | |
42 }; | |
43 | |
44 const uint8 kAudioInterfaceClass = 1; | |
45 const uint8 kAudioMidiInterfaceSubclass = 3; | |
46 | |
47 class JackMatcher { | |
48 public: | |
49 explicit JackMatcher(uint8 id) : id_(id) {} | |
50 | |
51 bool operator() (const UsbMidiJack& jack) const { | |
52 return jack.jack_id == id_; | |
53 } | |
54 | |
55 private: | |
56 uint8 id_; | |
57 }; | |
58 | |
59 } // namespace | |
60 | |
61 UsbMidiDescriptorParser::UsbMidiDescriptorParser() | |
62 : is_parsing_usb_midi_interface_(false), | |
63 current_endpoint_address_(0), | |
64 current_cable_number_(0) {} | |
65 | |
66 UsbMidiDescriptorParser::~UsbMidiDescriptorParser() {} | |
67 | |
68 bool UsbMidiDescriptorParser::Parse(UsbMidiDevice* device, | |
69 const uint8* data, | |
70 size_t size, | |
71 std::vector<UsbMidiJack>* jacks) { | |
72 jacks->clear(); | |
73 bool result = ParseInternal(device, data, size, jacks); | |
74 if (!result) | |
75 jacks->clear(); | |
76 Clear(); | |
77 return result; | |
78 } | |
79 | |
80 bool UsbMidiDescriptorParser::ParseInternal(UsbMidiDevice* device, | |
81 const uint8* data, | |
82 size_t size, | |
83 std::vector<UsbMidiJack>* jacks) { | |
84 const uint8* current = data; | |
85 while (current < data + size) { | |
86 uint8 length = current[0]; | |
87 if (length <= 1) { | |
88 DVLOG(1) << "Descriptor Type is not accessible."; | |
89 return false; | |
90 } | |
91 if (current + length > data + size) { | |
DaleCurtis
2014/01/10 01:55:57
current + length >= data + size
yhirano
2014/01/10 02:30:32
current_length == data + size means that it is the
| |
92 DVLOG(1) << "The header size is incorrect."; | |
93 return false; | |
94 } | |
95 DescriptorType descriptor_type = static_cast<DescriptorType>(current[1]); | |
96 switch (descriptor_type) { | |
DaleCurtis
2014/01/10 01:55:57
Instead of checking !is_parsing_usb_midi_interface
yhirano
2014/01/10 02:30:32
Thanks, done.
| |
97 case TYPE_INTERFACE: | |
98 if (!ParseInterface(current, length)) | |
99 return false; | |
100 break; | |
101 case TYPE_CS_INTERFACE: | |
102 // We are assuming that the corresponding INTERFACE precedes | |
103 // the CS_INTERFACE descriptor, as specified. | |
104 if (!ParseCSInterface(device, current, length)) | |
105 return false; | |
106 break; | |
107 case TYPE_ENDPOINT: | |
108 // We are assuming that endpoints are contained in an interface. | |
109 if (!ParseEndpoint(current, length)) | |
110 return false; | |
111 break; | |
112 case TYPE_CS_ENDPOINT: | |
113 // We are assuming that the corresponding ENDPOINT precedes | |
114 // the CS_ENDPOINT descriptor, as specified. | |
115 if (!ParseCSEndpoint(current, length, jacks)) | |
116 return false; | |
117 break; | |
118 default: | |
119 // Ignore uninteresting types. | |
120 break; | |
121 } | |
122 current += length; | |
123 } | |
124 return true; | |
125 } | |
126 | |
127 bool UsbMidiDescriptorParser::ParseInterface(const uint8* data, size_t size) { | |
128 if (size != 9) { | |
129 DVLOG(1) << "INTERFACE header size is incorrect."; | |
130 return false; | |
131 } | |
132 incomplete_jacks_.clear(); | |
133 | |
134 uint8 interface_class = data[5]; | |
135 uint8 interface_subclass = data[6]; | |
136 | |
137 // All descriptors of endpoints contained in this interface | |
138 // precede the next INTERFACE descriptor. | |
139 is_parsing_usb_midi_interface_ = | |
140 interface_class == kAudioInterfaceClass && | |
141 interface_subclass == kAudioMidiInterfaceSubclass; | |
142 return true; | |
143 } | |
144 | |
145 bool UsbMidiDescriptorParser::ParseCSInterface(UsbMidiDevice* device, | |
146 const uint8* data, | |
147 size_t size) { | |
148 if (!is_parsing_usb_midi_interface_) | |
149 return true; | |
150 | |
151 // Descriptor Type and Descriptor Subtype should be accessible. | |
152 if (size < 3) { | |
153 DVLOG(1) << "CS_INTERFACE header size is incorrect."; | |
154 return false; | |
155 } | |
156 | |
157 DescriptorSubType subtype = static_cast<DescriptorSubType>(data[2]); | |
158 | |
159 switch (subtype) { | |
DaleCurtis
2014/01/10 01:55:57
Replace the switch with an early return if !IN_JAC
yhirano
2014/01/10 02:30:32
Done.
| |
160 case SUBTYPE_MIDI_IN_JACK: | |
161 case SUBTYPE_MIDI_OUT_JACK: { | |
162 if (size < 6) { | |
163 DVLOG(1) << "CS_INTERFACE (MIDI JACK) header size is incorrect."; | |
164 return false; | |
165 } | |
166 uint8 jack_type = data[3]; | |
167 uint8 id = data[4]; | |
168 if (jack_type == JACK_TYPE_EMBEDDED) { | |
169 // We can't determine the associated endpoint now. | |
170 incomplete_jacks_.push_back(UsbMidiJack(device, id, 0, 0)); | |
171 } | |
172 break; | |
173 } | |
174 default: | |
175 break; | |
176 } | |
177 return true; | |
178 } | |
179 | |
180 bool UsbMidiDescriptorParser::ParseEndpoint(const uint8* data, size_t size) { | |
181 if (!is_parsing_usb_midi_interface_) | |
182 return true; | |
183 | |
184 if (size < 4) { | |
185 DVLOG(1) << "ENDPOINT header size is incorrect."; | |
186 return false; | |
187 } | |
188 current_endpoint_address_ = data[2]; | |
189 current_cable_number_ = 0; | |
190 return true; | |
191 } | |
192 | |
193 bool UsbMidiDescriptorParser::ParseCSEndpoint(const uint8* data, | |
194 size_t size, | |
195 std::vector<UsbMidiJack>* jacks) { | |
196 const size_t kSizeForEmptyJacks = 4; | |
197 if (!is_parsing_usb_midi_interface_) | |
198 return true; | |
199 // CS_ENDPOINT must be of size 4 + n where n is the number of associated | |
200 // jacks. | |
201 if (size < kSizeForEmptyJacks) { | |
202 DVLOG(1) << "CS_ENDPOINT header size is incorrect."; | |
203 return false; | |
204 } | |
205 uint8 num_jacks = data[3]; | |
206 if (size != kSizeForEmptyJacks + num_jacks) { | |
207 DVLOG(1) << "CS_ENDPOINT header size is incorrect."; | |
208 return false; | |
209 } | |
210 | |
211 for (size_t i = 0; i < num_jacks; ++i) { | |
212 uint8 jack = data[kSizeForEmptyJacks + i]; | |
213 std::vector<UsbMidiJack>::iterator it = | |
214 std::find_if(incomplete_jacks_.begin(), | |
215 incomplete_jacks_.end(), | |
216 JackMatcher(jack)); | |
217 if (it == incomplete_jacks_.end()) { | |
218 DVLOG(1) << "A non-existing MIDI jack is associated."; | |
219 return false; | |
220 } | |
221 if (current_cable_number_ > 0xf) { | |
222 DVLOG(1) << "Cable number should range from 0x0 to 0xf."; | |
223 return false; | |
224 } | |
225 // CS_ENDPOINT follows ENDPOINT and hence we can use the following | |
226 // member variables. | |
227 it->cable_number = current_cable_number_++; | |
228 it->endpoint_address = current_endpoint_address_; | |
229 jacks->push_back(*it); | |
230 incomplete_jacks_.erase(it); | |
231 } | |
232 return true; | |
233 } | |
234 | |
235 void UsbMidiDescriptorParser::Clear() { | |
236 is_parsing_usb_midi_interface_ = false; | |
237 current_endpoint_address_ = 0; | |
238 current_cable_number_ = 0; | |
239 incomplete_jacks_.clear(); | |
240 } | |
241 | |
242 } // namespace media | |
OLD | NEW |