Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(671)

Side by Side Diff: device/usb/usb_descriptors.cc

Issue 2821813002: Use Mojo enum types in the C++ USB interface (Closed)
Patch Set: Fix up //device/usb dependencies in //extensions/browser/api Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/usb/usb_descriptors.h ('k') | device/usb/usb_descriptors_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "device/usb/usb_descriptors.h" 5 #include "device/usb/usb_descriptors.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <memory> 10 #include <memory>
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 << desc->configurations.size() << "."; 90 << desc->configurations.size() << ".";
91 callback.Run(nullptr); 91 callback.Run(nullptr);
92 } 92 }
93 } 93 }
94 94
95 void OnReadConfigDescriptor(UsbDeviceDescriptor* desc, 95 void OnReadConfigDescriptor(UsbDeviceDescriptor* desc,
96 const base::Closure& closure, 96 const base::Closure& closure,
97 UsbTransferStatus status, 97 UsbTransferStatus status,
98 scoped_refptr<IOBuffer> buffer, 98 scoped_refptr<IOBuffer> buffer,
99 size_t length) { 99 size_t length) {
100 if (status == USB_TRANSFER_COMPLETED) { 100 if (status == UsbTransferStatus::COMPLETED) {
101 if (!desc->Parse( 101 if (!desc->Parse(
102 std::vector<uint8_t>(buffer->data(), buffer->data() + length))) { 102 std::vector<uint8_t>(buffer->data(), buffer->data() + length))) {
103 LOG(ERROR) << "Failed to parse configuration descriptor."; 103 LOG(ERROR) << "Failed to parse configuration descriptor.";
104 } 104 }
105 } else { 105 } else {
106 LOG(ERROR) << "Failed to read configuration descriptor."; 106 LOG(ERROR) << "Failed to read configuration descriptor.";
107 } 107 }
108 closure.Run(); 108 closure.Run();
109 } 109 }
110 110
111 void OnReadConfigDescriptorHeader(scoped_refptr<UsbDeviceHandle> device_handle, 111 void OnReadConfigDescriptorHeader(scoped_refptr<UsbDeviceHandle> device_handle,
112 UsbDeviceDescriptor* desc, 112 UsbDeviceDescriptor* desc,
113 uint8_t index, 113 uint8_t index,
114 const base::Closure& closure, 114 const base::Closure& closure,
115 UsbTransferStatus status, 115 UsbTransferStatus status,
116 scoped_refptr<IOBuffer> header, 116 scoped_refptr<IOBuffer> header,
117 size_t length) { 117 size_t length) {
118 if (status == USB_TRANSFER_COMPLETED && length == 4) { 118 if (status == UsbTransferStatus::COMPLETED && length == 4) {
119 const uint8_t* data = reinterpret_cast<const uint8_t*>(header->data()); 119 const uint8_t* data = reinterpret_cast<const uint8_t*>(header->data());
120 uint16_t total_length = data[2] | data[3] << 8; 120 uint16_t total_length = data[2] | data[3] << 8;
121 scoped_refptr<IOBuffer> buffer = new IOBuffer(total_length); 121 scoped_refptr<IOBuffer> buffer = new IOBuffer(total_length);
122 device_handle->ControlTransfer( 122 device_handle->ControlTransfer(
123 USB_DIRECTION_INBOUND, UsbDeviceHandle::STANDARD, 123 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD,
124 UsbDeviceHandle::DEVICE, kGetDescriptorRequest, 124 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest,
125 kConfigurationDescriptorType << 8 | index, 0, buffer, total_length, 125 kConfigurationDescriptorType << 8 | index, 0, buffer, total_length,
126 kControlTransferTimeout, 126 kControlTransferTimeout,
127 base::Bind(&OnReadConfigDescriptor, desc, closure)); 127 base::Bind(&OnReadConfigDescriptor, desc, closure));
128 } else { 128 } else {
129 LOG(ERROR) << "Failed to read length for configuration " 129 LOG(ERROR) << "Failed to read length for configuration "
130 << static_cast<int>(index) << "."; 130 << static_cast<int>(index) << ".";
131 closure.Run(); 131 closure.Run();
132 } 132 }
133 } 133 }
134 134
135 void OnReadDeviceDescriptor( 135 void OnReadDeviceDescriptor(
136 scoped_refptr<UsbDeviceHandle> device_handle, 136 scoped_refptr<UsbDeviceHandle> device_handle,
137 const base::Callback<void(std::unique_ptr<UsbDeviceDescriptor>)>& callback, 137 const base::Callback<void(std::unique_ptr<UsbDeviceDescriptor>)>& callback,
138 UsbTransferStatus status, 138 UsbTransferStatus status,
139 scoped_refptr<IOBuffer> buffer, 139 scoped_refptr<IOBuffer> buffer,
140 size_t length) { 140 size_t length) {
141 if (status != USB_TRANSFER_COMPLETED) { 141 if (status != UsbTransferStatus::COMPLETED) {
142 LOG(ERROR) << "Failed to read device descriptor."; 142 LOG(ERROR) << "Failed to read device descriptor.";
143 callback.Run(nullptr); 143 callback.Run(nullptr);
144 return; 144 return;
145 } 145 }
146 146
147 std::unique_ptr<UsbDeviceDescriptor> desc(new UsbDeviceDescriptor()); 147 std::unique_ptr<UsbDeviceDescriptor> desc(new UsbDeviceDescriptor());
148 if (!desc->Parse( 148 if (!desc->Parse(
149 std::vector<uint8_t>(buffer->data(), buffer->data() + length))) { 149 std::vector<uint8_t>(buffer->data(), buffer->data() + length))) {
150 LOG(ERROR) << "Device descriptor parsing error."; 150 LOG(ERROR) << "Device descriptor parsing error.";
151 callback.Run(nullptr); 151 callback.Run(nullptr);
152 return; 152 return;
153 } 153 }
154 154
155 if (desc->num_configurations == 0) { 155 if (desc->num_configurations == 0) {
156 callback.Run(std::move(desc)); 156 callback.Run(std::move(desc));
157 return; 157 return;
158 } 158 }
159 159
160 uint8_t num_configurations = desc->num_configurations; 160 uint8_t num_configurations = desc->num_configurations;
161 UsbDeviceDescriptor* desc_ptr = desc.get(); 161 UsbDeviceDescriptor* desc_ptr = desc.get();
162 base::Closure closure = base::BarrierClosure( 162 base::Closure closure = base::BarrierClosure(
163 num_configurations, 163 num_configurations,
164 base::Bind(OnDoneReadingConfigDescriptors, device_handle, 164 base::Bind(OnDoneReadingConfigDescriptors, device_handle,
165 base::Passed(&desc), callback)); 165 base::Passed(&desc), callback));
166 for (uint8_t i = 0; i < num_configurations; ++i) { 166 for (uint8_t i = 0; i < num_configurations; ++i) {
167 scoped_refptr<IOBufferWithSize> header = new IOBufferWithSize(4); 167 scoped_refptr<IOBufferWithSize> header = new IOBufferWithSize(4);
168 device_handle->ControlTransfer( 168 device_handle->ControlTransfer(
169 USB_DIRECTION_INBOUND, UsbDeviceHandle::STANDARD, 169 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD,
170 UsbDeviceHandle::DEVICE, kGetDescriptorRequest, 170 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest,
171 kConfigurationDescriptorType << 8 | i, 0, header, header->size(), 171 kConfigurationDescriptorType << 8 | i, 0, header, header->size(),
172 kControlTransferTimeout, 172 kControlTransferTimeout,
173 base::Bind(&OnReadConfigDescriptorHeader, device_handle, desc_ptr, i, 173 base::Bind(&OnReadConfigDescriptorHeader, device_handle, desc_ptr, i,
174 closure)); 174 closure));
175 } 175 }
176 } 176 }
177 177
178 void StoreStringDescriptor(IndexMap::iterator it, 178 void StoreStringDescriptor(IndexMap::iterator it,
179 const base::Closure& callback, 179 const base::Closure& callback,
180 const base::string16& string) { 180 const base::string16& string) {
181 it->second = string; 181 it->second = string;
182 callback.Run(); 182 callback.Run();
183 } 183 }
184 184
185 void OnReadStringDescriptor( 185 void OnReadStringDescriptor(
186 const base::Callback<void(const base::string16&)>& callback, 186 const base::Callback<void(const base::string16&)>& callback,
187 UsbTransferStatus status, 187 UsbTransferStatus status,
188 scoped_refptr<IOBuffer> buffer, 188 scoped_refptr<IOBuffer> buffer,
189 size_t length) { 189 size_t length) {
190 base::string16 string; 190 base::string16 string;
191 if (status == USB_TRANSFER_COMPLETED && 191 if (status == UsbTransferStatus::COMPLETED &&
192 ParseUsbStringDescriptor( 192 ParseUsbStringDescriptor(
193 std::vector<uint8_t>(buffer->data(), buffer->data() + length), 193 std::vector<uint8_t>(buffer->data(), buffer->data() + length),
194 &string)) { 194 &string)) {
195 callback.Run(string); 195 callback.Run(string);
196 } else { 196 } else {
197 callback.Run(base::string16()); 197 callback.Run(base::string16());
198 } 198 }
199 } 199 }
200 200
201 void ReadStringDescriptor( 201 void ReadStringDescriptor(
202 scoped_refptr<UsbDeviceHandle> device_handle, 202 scoped_refptr<UsbDeviceHandle> device_handle,
203 uint8_t index, 203 uint8_t index,
204 uint16_t language_id, 204 uint16_t language_id,
205 const base::Callback<void(const base::string16&)>& callback) { 205 const base::Callback<void(const base::string16&)>& callback) {
206 scoped_refptr<IOBufferWithSize> buffer = new IOBufferWithSize(255); 206 scoped_refptr<IOBufferWithSize> buffer = new IOBufferWithSize(255);
207 device_handle->ControlTransfer( 207 device_handle->ControlTransfer(
208 USB_DIRECTION_INBOUND, UsbDeviceHandle::STANDARD, UsbDeviceHandle::DEVICE, 208 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD,
209 kGetDescriptorRequest, kStringDescriptorType << 8 | index, language_id, 209 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest,
210 buffer, buffer->size(), kControlTransferTimeout, 210 kStringDescriptorType << 8 | index, language_id, buffer, buffer->size(),
211 base::Bind(&OnReadStringDescriptor, callback)); 211 kControlTransferTimeout, base::Bind(&OnReadStringDescriptor, callback));
212 } 212 }
213 213
214 void OnReadLanguageIds(scoped_refptr<UsbDeviceHandle> device_handle, 214 void OnReadLanguageIds(scoped_refptr<UsbDeviceHandle> device_handle,
215 IndexMapPtr index_map, 215 IndexMapPtr index_map,
216 const base::Callback<void(IndexMapPtr)>& callback, 216 const base::Callback<void(IndexMapPtr)>& callback,
217 const base::string16& languages) { 217 const base::string16& languages) {
218 // Default to English unless the device provides a language and then just pick 218 // Default to English unless the device provides a language and then just pick
219 // the first one. 219 // the first one.
220 uint16_t language_id = languages.empty() ? 0x0409 : languages[0]; 220 uint16_t language_id = languages.empty() ? 0x0409 : languages[0];
221 221
(...skipping 25 matching lines...) Expand all
247 UsbEndpointDescriptor::UsbEndpointDescriptor(uint8_t address, 247 UsbEndpointDescriptor::UsbEndpointDescriptor(uint8_t address,
248 uint8_t attributes, 248 uint8_t attributes,
249 uint16_t maximum_packet_size, 249 uint16_t maximum_packet_size,
250 uint8_t polling_interval) 250 uint8_t polling_interval)
251 : address(address), 251 : address(address),
252 maximum_packet_size(maximum_packet_size), 252 maximum_packet_size(maximum_packet_size),
253 polling_interval(polling_interval) { 253 polling_interval(polling_interval) {
254 // These fields are defined in Table 9-24 of the USB 3.1 Specification. 254 // These fields are defined in Table 9-24 of the USB 3.1 Specification.
255 switch (address & 0x80) { 255 switch (address & 0x80) {
256 case 0x00: 256 case 0x00:
257 direction = USB_DIRECTION_OUTBOUND; 257 direction = UsbTransferDirection::OUTBOUND;
258 break; 258 break;
259 case 0x80: 259 case 0x80:
260 direction = USB_DIRECTION_INBOUND; 260 direction = UsbTransferDirection::INBOUND;
261 break; 261 break;
262 } 262 }
263 switch (attributes & 0x03) { 263 switch (attributes & 0x03) {
264 case 0x00: 264 case 0x00:
265 transfer_type = USB_TRANSFER_CONTROL; 265 transfer_type = UsbTransferType::CONTROL;
266 break; 266 break;
267 case 0x01: 267 case 0x01:
268 transfer_type = USB_TRANSFER_ISOCHRONOUS; 268 transfer_type = UsbTransferType::ISOCHRONOUS;
269 break; 269 break;
270 case 0x02: 270 case 0x02:
271 transfer_type = USB_TRANSFER_BULK; 271 transfer_type = UsbTransferType::BULK;
272 break; 272 break;
273 case 0x03: 273 case 0x03:
274 transfer_type = USB_TRANSFER_INTERRUPT; 274 transfer_type = UsbTransferType::INTERRUPT;
275 break; 275 break;
276 } 276 }
277 switch (attributes & 0x0F) { 277 switch (attributes & 0x0F) {
278 // Isochronous endpoints only. 278 // Isochronous endpoints only.
279 case 0x05: 279 case 0x05:
280 synchronization_type = USB_SYNCHRONIZATION_ASYNCHRONOUS; 280 synchronization_type = USB_SYNCHRONIZATION_ASYNCHRONOUS;
281 break; 281 break;
282 case 0x09: 282 case 0x09:
283 synchronization_type = USB_SYNCHRONIZATION_ADAPTIVE; 283 synchronization_type = USB_SYNCHRONIZATION_ADAPTIVE;
284 break; 284 break;
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 493
494 return true; 494 return true;
495 } 495 }
496 496
497 void ReadUsbDescriptors(scoped_refptr<UsbDeviceHandle> device_handle, 497 void ReadUsbDescriptors(scoped_refptr<UsbDeviceHandle> device_handle,
498 const base::Callback<void( 498 const base::Callback<void(
499 std::unique_ptr<UsbDeviceDescriptor>)>& callback) { 499 std::unique_ptr<UsbDeviceDescriptor>)>& callback) {
500 scoped_refptr<IOBufferWithSize> buffer = 500 scoped_refptr<IOBufferWithSize> buffer =
501 new IOBufferWithSize(kDeviceDescriptorLength); 501 new IOBufferWithSize(kDeviceDescriptorLength);
502 device_handle->ControlTransfer( 502 device_handle->ControlTransfer(
503 USB_DIRECTION_INBOUND, UsbDeviceHandle::STANDARD, UsbDeviceHandle::DEVICE, 503 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD,
504 kGetDescriptorRequest, kDeviceDescriptorType << 8, 0, buffer, 504 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest,
505 buffer->size(), kControlTransferTimeout, 505 kDeviceDescriptorType << 8, 0, buffer, buffer->size(),
506 kControlTransferTimeout,
506 base::Bind(&OnReadDeviceDescriptor, device_handle, callback)); 507 base::Bind(&OnReadDeviceDescriptor, device_handle, callback));
507 } 508 }
508 509
509 bool ParseUsbStringDescriptor(const std::vector<uint8_t>& descriptor, 510 bool ParseUsbStringDescriptor(const std::vector<uint8_t>& descriptor,
510 base::string16* output) { 511 base::string16* output) {
511 if (descriptor.size() < 2 || descriptor[1] != kStringDescriptorType) 512 if (descriptor.size() < 2 || descriptor[1] != kStringDescriptorType)
512 return false; 513 return false;
513 514
514 // Let the device return a buffer larger than the actual string but prefer the 515 // Let the device return a buffer larger than the actual string but prefer the
515 // length reported inside the descriptor. 516 // length reported inside the descriptor.
(...skipping 19 matching lines...) Expand all
535 callback.Run(std::move(index_map)); 536 callback.Run(std::move(index_map));
536 return; 537 return;
537 } 538 }
538 539
539 ReadStringDescriptor(device_handle, 0, 0, 540 ReadStringDescriptor(device_handle, 0, 0,
540 base::Bind(&OnReadLanguageIds, device_handle, 541 base::Bind(&OnReadLanguageIds, device_handle,
541 base::Passed(&index_map), callback)); 542 base::Passed(&index_map), callback));
542 } 543 }
543 544
544 } // namespace device 545 } // namespace device
OLDNEW
« no previous file with comments | « device/usb/usb_descriptors.h ('k') | device/usb/usb_descriptors_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698