Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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 } // namespace | |
| 48 | |
| 49 UsbMidiDescriptorParser::UsbMidiDescriptorParser() | |
| 50 : is_parsing_usb_midi_interface_(false), | |
| 51 current_endpoint_address_(0), | |
| 52 current_cable_number_(0) {} | |
| 53 | |
| 54 UsbMidiDescriptorParser::~UsbMidiDescriptorParser() {} | |
| 55 | |
| 56 bool UsbMidiDescriptorParser::Parse(UsbMidiDevice* device, | |
| 57 const uint8* data, | |
| 58 size_t size) { | |
| 59 Clear(); | |
|
DaleCurtis
2014/01/03 00:56:49
You seem to be always clearing the internal state
yhirano
2014/01/09 08:33:13
Removing jacks_ is done.
This is not a generic par
| |
| 60 bool result = ParseInternal(device, data, size); | |
| 61 if (result) | |
| 62 ClearExceptJacks(); | |
| 63 else | |
| 64 Clear(); | |
| 65 return result; | |
| 66 } | |
| 67 | |
| 68 bool UsbMidiDescriptorParser::ParseInternal(UsbMidiDevice* device, | |
| 69 const uint8* data, | |
| 70 size_t size) { | |
| 71 DCHECK(jacks_.empty()); | |
| 72 const uint8* current = data; | |
| 73 while (current < &data[size]) { | |
|
DaleCurtis
2014/01/03 00:56:49
The notation here is slightly confusing. It'd be
yhirano
2014/01/09 08:33:13
Done.
| |
| 74 uint8 length = current[0]; | |
| 75 if (length <= 1) { | |
| 76 DVLOG(1) << "Descriptor Type is not accessible."; | |
| 77 return false; | |
| 78 } | |
| 79 if (¤t[length] > &data[size]) { | |
|
DaleCurtis
2014/01/03 00:56:49
Ditto.
yhirano
2014/01/09 08:33:13
Done.
| |
| 80 DVLOG(1) << "The header size is incorrect."; | |
| 81 return false; | |
| 82 } | |
| 83 DescriptorType descriptor_type = static_cast<DescriptorType>(current[1]); | |
| 84 switch (descriptor_type) { | |
| 85 case TYPE_INTERFACE: | |
| 86 if (!ParseInterface(device, current, length)) | |
| 87 return false; | |
| 88 break; | |
| 89 case TYPE_CS_INTERFACE: | |
| 90 if (!ParseCSInterface(device, current, length)) | |
| 91 return false; | |
| 92 break; | |
| 93 case TYPE_ENDPOINT: | |
| 94 if (!ParseEndpoint(device, current, length)) | |
| 95 return false; | |
| 96 break; | |
| 97 case TYPE_CS_ENDPOINT: | |
| 98 if (!ParseCSEndpoint(device, current, length)) | |
| 99 return false; | |
| 100 break; | |
| 101 default: | |
| 102 // Ignore unintersting types. | |
|
DaleCurtis
2014/01/03 00:56:49
uninteresting
yhirano
2014/01/09 08:33:13
Done.
| |
| 103 break; | |
| 104 } | |
| 105 current += length; | |
| 106 } | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 bool UsbMidiDescriptorParser::ParseInterface(UsbMidiDevice* device, | |
| 111 const uint8* data, | |
| 112 size_t size) { | |
| 113 if (size != 9) { | |
| 114 DVLOG(1) << "INTERFACE header size is incorrect."; | |
| 115 return false; | |
| 116 } | |
| 117 incomplete_jacks_.clear(); | |
| 118 | |
| 119 uint8 interface_class = data[5]; | |
| 120 uint8 interface_subclass = data[6]; | |
| 121 | |
| 122 // All descriptors of endpoints contained in this interface | |
| 123 // precede the next INTERFACE descriptor. | |
| 124 is_parsing_usb_midi_interface_ = | |
| 125 (interface_class == kAudioInterfaceClass && | |
|
DaleCurtis
2014/01/03 00:56:49
No need for parens.
yhirano
2014/01/09 08:33:13
Done.
| |
| 126 interface_subclass == kAudioMidiInterfaceSubclass); | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 bool UsbMidiDescriptorParser::ParseCSInterface(UsbMidiDevice* device, | |
| 131 const uint8* data, | |
| 132 size_t size) { | |
| 133 if (!is_parsing_usb_midi_interface_) | |
| 134 return true; | |
|
DaleCurtis
2014/01/03 00:56:49
Should this be false? Here and all other methods.
yhirano
2014/01/09 08:33:13
This should be true, we want to ignore uninteresti
| |
| 135 | |
| 136 // Descriptor Type and Descriptor Subtype should be accessible. | |
| 137 if (size <= 2) { | |
|
DaleCurtis
2014/01/03 00:56:49
< 3 for consistency.
yhirano
2014/01/09 08:33:13
Done.
| |
| 138 DVLOG(1) << "CS_INTERFACE header size is incorrect."; | |
| 139 return false; | |
| 140 } | |
| 141 | |
| 142 DescriptorSubType subtype = static_cast<DescriptorSubType>(data[2]); | |
| 143 | |
| 144 switch (subtype) { | |
| 145 case SUBTYPE_MIDI_IN_JACK: | |
| 146 // FALL_THROUGH | |
|
DaleCurtis
2014/01/03 00:56:49
Label is unnecessary since you're not doing anythi
yhirano
2014/01/09 08:33:13
Done.
| |
| 147 case SUBTYPE_MIDI_OUT_JACK: { | |
| 148 if (size < 6) { | |
| 149 DVLOG(1) << "CS_INTERFACE (MIDI JACK) header size is incorrect."; | |
| 150 return false; | |
| 151 } | |
| 152 uint8 jack_type = data[3]; | |
| 153 uint8 id = data[4]; | |
| 154 if (jack_type == JACK_TYPE_EMBEDDED) { | |
| 155 // We can't determine the associated endpoint now. | |
| 156 incomplete_jacks_.insert( | |
|
DaleCurtis
2014/01/03 00:56:49
incomplete_jacks_[id] = ... ? no need for make_pai
yhirano
2014/01/09 08:33:13
Done.
| |
| 157 std::make_pair(id, UsbMidiJack(device, id, 0, 0))); | |
| 158 } | |
| 159 break; | |
| 160 } | |
| 161 default: | |
| 162 break; | |
| 163 } | |
| 164 return true; | |
| 165 } | |
| 166 | |
| 167 bool UsbMidiDescriptorParser::ParseEndpoint(UsbMidiDevice* device, | |
| 168 const uint8* data, | |
| 169 size_t size) { | |
| 170 if (!is_parsing_usb_midi_interface_) | |
| 171 return true; | |
| 172 | |
| 173 if (size < 4) { | |
|
DaleCurtis
2014/01/03 00:56:49
Ditto.
yhirano
2014/01/09 08:33:13
Sorry, what ditto?
| |
| 174 DVLOG(1) << "ENDPOINT header size is incorrect."; | |
| 175 return false; | |
| 176 } | |
| 177 current_endpoint_address_ = data[2]; | |
| 178 current_cable_number_ = 0; | |
| 179 return true; | |
| 180 } | |
| 181 | |
| 182 bool UsbMidiDescriptorParser::ParseCSEndpoint(UsbMidiDevice* device, | |
| 183 const uint8* data, | |
| 184 size_t size) { | |
| 185 if (!is_parsing_usb_midi_interface_) | |
| 186 return true; | |
| 187 // CS_ENDPOINT must be of size 4 + n where n is the number of associated | |
| 188 // jacks. | |
| 189 if (size < 4) { | |
| 190 DVLOG(1) << "CS_ENDPOINT header size is incorrect."; | |
| 191 return false; | |
| 192 } | |
| 193 uint8 num_jacks = data[3]; | |
| 194 if (size != static_cast<size_t>(4 + num_jacks)) { | |
|
DaleCurtis
2014/01/03 00:56:49
4 is used multiple times, so it should be extracte
yhirano
2014/01/09 08:33:13
Done.
| |
| 195 DVLOG(1) << "CS_ENDPOINT header size is incorrect."; | |
| 196 return false; | |
| 197 } | |
| 198 | |
| 199 for (size_t i = 0; i < num_jacks; ++i) { | |
| 200 uint8 jack = data[4 + i]; | |
| 201 base::hash_map<uint8, UsbMidiJack>::iterator it = | |
| 202 incomplete_jacks_.find(jack); | |
| 203 if (it == incomplete_jacks_.end()) { | |
| 204 DVLOG(1) << "A non-existing MIDI jack is associated."; | |
| 205 return false; | |
| 206 } | |
| 207 if (current_cable_number_ >= 0x10) { | |
|
DaleCurtis
2014/01/03 00:56:49
Using > 0xF is clearer and more consistent with th
yhirano
2014/01/09 08:33:13
Done.
| |
| 208 DVLOG(1) << "Cable number should range from 0x0 to 0xf."; | |
| 209 return false; | |
| 210 } | |
| 211 // CS_ENDPOINT follows ENDPOINT and hence we can use the following | |
| 212 // member variables. | |
| 213 it->second.cable_number = current_cable_number_++; | |
| 214 it->second.endpoint_address = current_endpoint_address_; | |
| 215 jacks_.push_back(it->second); | |
| 216 incomplete_jacks_.erase(it); | |
| 217 } | |
| 218 return true; | |
| 219 } | |
| 220 | |
| 221 void UsbMidiDescriptorParser::Clear() { | |
| 222 ClearExceptJacks(); | |
| 223 jacks_.clear(); | |
| 224 } | |
| 225 | |
| 226 void UsbMidiDescriptorParser::ClearExceptJacks() { | |
| 227 is_parsing_usb_midi_interface_ = false; | |
| 228 current_endpoint_address_ = 0; | |
| 229 current_cable_number_ = 0; | |
| 230 incomplete_jacks_.clear(); | |
| 231 } | |
| 232 | |
| 233 } // namespace media | |
| OLD | NEW |