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 "components/usb_service/usb_interface.h" | 5 #include "components/usb_service/usb_interface_impl.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "third_party/libusb/src/libusb/libusb.h" | 8 #include "third_party/libusb/src/libusb/libusb.h" |
9 | 9 |
10 namespace usb_service { | 10 namespace usb_service { |
11 | 11 |
12 UsbEndpointDescriptor::UsbEndpointDescriptor( | 12 UsbEndpointDescriptorImpl::UsbEndpointDescriptorImpl( |
13 scoped_refptr<const UsbConfigDescriptor> config, | 13 scoped_refptr<const UsbConfigDescriptor> config, |
14 PlatformUsbEndpointDescriptor descriptor) | 14 PlatformUsbEndpointDescriptor descriptor) |
15 : config_(config), descriptor_(descriptor) { | 15 : config_(config), descriptor_(descriptor) { |
16 } | 16 } |
17 | 17 |
18 UsbEndpointDescriptor::~UsbEndpointDescriptor() { | 18 UsbEndpointDescriptorImpl::~UsbEndpointDescriptorImpl() { |
19 } | 19 } |
20 | 20 |
21 int UsbEndpointDescriptor::GetAddress() const { | 21 int UsbEndpointDescriptorImpl::GetAddress() const { |
22 return descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_ADDRESS_MASK; | 22 return descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_ADDRESS_MASK; |
23 } | 23 } |
24 | 24 |
25 UsbEndpointDirection UsbEndpointDescriptor::GetDirection() const { | 25 UsbEndpointDirection UsbEndpointDescriptorImpl::GetDirection() const { |
26 switch (descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) { | 26 switch (descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) { |
27 case LIBUSB_ENDPOINT_IN: | 27 case LIBUSB_ENDPOINT_IN: |
28 return USB_DIRECTION_INBOUND; | 28 return USB_DIRECTION_INBOUND; |
29 case LIBUSB_ENDPOINT_OUT: | 29 case LIBUSB_ENDPOINT_OUT: |
30 return USB_DIRECTION_OUTBOUND; | 30 return USB_DIRECTION_OUTBOUND; |
31 default: | 31 default: |
32 NOTREACHED(); | 32 NOTREACHED(); |
33 return USB_DIRECTION_INBOUND; | 33 return USB_DIRECTION_INBOUND; |
34 } | 34 } |
35 } | 35 } |
36 | 36 |
37 int UsbEndpointDescriptor::GetMaximumPacketSize() const { | 37 int UsbEndpointDescriptorImpl::GetMaximumPacketSize() const { |
38 return descriptor_->wMaxPacketSize; | 38 return descriptor_->wMaxPacketSize; |
39 } | 39 } |
40 | 40 |
41 UsbSynchronizationType UsbEndpointDescriptor::GetSynchronizationType() const { | 41 UsbSynchronizationType |
| 42 UsbEndpointDescriptorImpl::GetSynchronizationType() const { |
42 switch (descriptor_->bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) { | 43 switch (descriptor_->bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) { |
43 case LIBUSB_ISO_SYNC_TYPE_NONE: | 44 case LIBUSB_ISO_SYNC_TYPE_NONE: |
44 return USB_SYNCHRONIZATION_NONE; | 45 return USB_SYNCHRONIZATION_NONE; |
45 case LIBUSB_ISO_SYNC_TYPE_ASYNC: | 46 case LIBUSB_ISO_SYNC_TYPE_ASYNC: |
46 return USB_SYNCHRONIZATION_ASYNCHRONOUS; | 47 return USB_SYNCHRONIZATION_ASYNCHRONOUS; |
47 case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE: | 48 case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE: |
48 return USB_SYNCHRONIZATION_ADAPTIVE; | 49 return USB_SYNCHRONIZATION_ADAPTIVE; |
49 case LIBUSB_ISO_SYNC_TYPE_SYNC: | 50 case LIBUSB_ISO_SYNC_TYPE_SYNC: |
50 return USB_SYNCHRONIZATION_SYNCHRONOUS; | 51 return USB_SYNCHRONIZATION_SYNCHRONOUS; |
51 default: | 52 default: |
52 NOTREACHED(); | 53 NOTREACHED(); |
53 return USB_SYNCHRONIZATION_NONE; | 54 return USB_SYNCHRONIZATION_NONE; |
54 } | 55 } |
55 } | 56 } |
56 | 57 |
57 UsbTransferType UsbEndpointDescriptor::GetTransferType() const { | 58 UsbTransferType UsbEndpointDescriptorImpl::GetTransferType() const { |
58 switch (descriptor_->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) { | 59 switch (descriptor_->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) { |
59 case LIBUSB_TRANSFER_TYPE_CONTROL: | 60 case LIBUSB_TRANSFER_TYPE_CONTROL: |
60 return USB_TRANSFER_CONTROL; | 61 return USB_TRANSFER_CONTROL; |
61 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: | 62 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: |
62 return USB_TRANSFER_ISOCHRONOUS; | 63 return USB_TRANSFER_ISOCHRONOUS; |
63 case LIBUSB_TRANSFER_TYPE_BULK: | 64 case LIBUSB_TRANSFER_TYPE_BULK: |
64 return USB_TRANSFER_BULK; | 65 return USB_TRANSFER_BULK; |
65 case LIBUSB_TRANSFER_TYPE_INTERRUPT: | 66 case LIBUSB_TRANSFER_TYPE_INTERRUPT: |
66 return USB_TRANSFER_INTERRUPT; | 67 return USB_TRANSFER_INTERRUPT; |
67 default: | 68 default: |
68 NOTREACHED(); | 69 NOTREACHED(); |
69 return USB_TRANSFER_CONTROL; | 70 return USB_TRANSFER_CONTROL; |
70 } | 71 } |
71 } | 72 } |
72 | 73 |
73 UsbUsageType UsbEndpointDescriptor::GetUsageType() const { | 74 UsbUsageType UsbEndpointDescriptorImpl::GetUsageType() const { |
74 switch (descriptor_->bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) { | 75 switch (descriptor_->bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) { |
75 case LIBUSB_ISO_USAGE_TYPE_DATA: | 76 case LIBUSB_ISO_USAGE_TYPE_DATA: |
76 return USB_USAGE_DATA; | 77 return USB_USAGE_DATA; |
77 case LIBUSB_ISO_USAGE_TYPE_FEEDBACK: | 78 case LIBUSB_ISO_USAGE_TYPE_FEEDBACK: |
78 return USB_USAGE_FEEDBACK; | 79 return USB_USAGE_FEEDBACK; |
79 case LIBUSB_ISO_USAGE_TYPE_IMPLICIT: | 80 case LIBUSB_ISO_USAGE_TYPE_IMPLICIT: |
80 return USB_USAGE_EXPLICIT_FEEDBACK; | 81 return USB_USAGE_EXPLICIT_FEEDBACK; |
81 default: | 82 default: |
82 NOTREACHED(); | 83 NOTREACHED(); |
83 return USB_USAGE_DATA; | 84 return USB_USAGE_DATA; |
84 } | 85 } |
85 } | 86 } |
86 | 87 |
87 int UsbEndpointDescriptor::GetPollingInterval() const { | 88 int UsbEndpointDescriptorImpl::GetPollingInterval() const { |
88 return descriptor_->bInterval; | 89 return descriptor_->bInterval; |
89 } | 90 } |
90 | 91 |
91 UsbInterfaceAltSettingDescriptor::UsbInterfaceAltSettingDescriptor( | 92 UsbInterfaceAltSettingDescriptorImpl::UsbInterfaceAltSettingDescriptorImpl( |
92 scoped_refptr<const UsbConfigDescriptor> config, | 93 scoped_refptr<const UsbConfigDescriptor> config, |
93 PlatformUsbInterfaceDescriptor descriptor) | 94 PlatformUsbInterfaceDescriptor descriptor) |
94 : config_(config), descriptor_(descriptor) { | 95 : config_(config), descriptor_(descriptor) { |
95 } | 96 } |
96 | 97 |
97 UsbInterfaceAltSettingDescriptor::~UsbInterfaceAltSettingDescriptor() { | 98 UsbInterfaceAltSettingDescriptorImpl::~UsbInterfaceAltSettingDescriptorImpl() { |
98 } | 99 } |
99 | 100 |
100 size_t UsbInterfaceAltSettingDescriptor::GetNumEndpoints() const { | 101 size_t UsbInterfaceAltSettingDescriptorImpl::GetNumEndpoints() const { |
101 return descriptor_->bNumEndpoints; | 102 return descriptor_->bNumEndpoints; |
102 } | 103 } |
103 | 104 |
104 scoped_refptr<const UsbEndpointDescriptor> | 105 scoped_refptr<const UsbEndpointDescriptor> |
105 UsbInterfaceAltSettingDescriptor::GetEndpoint(size_t index) const { | 106 UsbInterfaceAltSettingDescriptorImpl::GetEndpoint(size_t index) const { |
106 return new UsbEndpointDescriptor(config_, &descriptor_->endpoint[index]); | 107 return new UsbEndpointDescriptorImpl(config_, &descriptor_->endpoint[index]); |
107 } | 108 } |
108 | 109 |
109 int UsbInterfaceAltSettingDescriptor::GetInterfaceNumber() const { | 110 int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceNumber() const { |
110 return descriptor_->bInterfaceNumber; | 111 return descriptor_->bInterfaceNumber; |
111 } | 112 } |
112 | 113 |
113 int UsbInterfaceAltSettingDescriptor::GetAlternateSetting() const { | 114 int UsbInterfaceAltSettingDescriptorImpl::GetAlternateSetting() const { |
114 return descriptor_->bAlternateSetting; | 115 return descriptor_->bAlternateSetting; |
115 } | 116 } |
116 | 117 |
117 int UsbInterfaceAltSettingDescriptor::GetInterfaceClass() const { | 118 int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceClass() const { |
118 return descriptor_->bInterfaceClass; | 119 return descriptor_->bInterfaceClass; |
119 } | 120 } |
120 | 121 |
121 int UsbInterfaceAltSettingDescriptor::GetInterfaceSubclass() const { | 122 int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceSubclass() const { |
122 return descriptor_->bInterfaceSubClass; | 123 return descriptor_->bInterfaceSubClass; |
123 } | 124 } |
124 | 125 |
125 int UsbInterfaceAltSettingDescriptor::GetInterfaceProtocol() const { | 126 int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceProtocol() const { |
126 return descriptor_->bInterfaceProtocol; | 127 return descriptor_->bInterfaceProtocol; |
127 } | 128 } |
128 | 129 |
129 UsbInterfaceDescriptor::UsbInterfaceDescriptor( | 130 UsbInterfaceDescriptorImpl::UsbInterfaceDescriptorImpl( |
130 scoped_refptr<const UsbConfigDescriptor> config, | 131 scoped_refptr<const UsbConfigDescriptor> config, |
131 PlatformUsbInterface usbInterface) | 132 PlatformUsbInterface usbInterface) |
132 : config_(config), interface_(usbInterface) { | 133 : config_(config), interface_(usbInterface) { |
133 } | 134 } |
134 | 135 |
135 UsbInterfaceDescriptor::~UsbInterfaceDescriptor() { | 136 UsbInterfaceDescriptorImpl::~UsbInterfaceDescriptorImpl() { |
136 } | 137 } |
137 | 138 |
138 size_t UsbInterfaceDescriptor::GetNumAltSettings() const { | 139 size_t UsbInterfaceDescriptorImpl::GetNumAltSettings() const { |
139 return interface_->num_altsetting; | 140 return interface_->num_altsetting; |
140 } | 141 } |
141 | 142 |
142 scoped_refptr<const UsbInterfaceAltSettingDescriptor> | 143 scoped_refptr<const UsbInterfaceAltSettingDescriptor> |
143 UsbInterfaceDescriptor::GetAltSetting(size_t index) const { | 144 UsbInterfaceDescriptorImpl::GetAltSetting(size_t index) const { |
144 return new UsbInterfaceAltSettingDescriptor(config_, | 145 return new UsbInterfaceAltSettingDescriptorImpl( |
145 &interface_->altsetting[index]); | 146 config_, &interface_->altsetting[index]); |
146 } | 147 } |
147 | 148 |
148 UsbConfigDescriptor::UsbConfigDescriptor(PlatformUsbConfigDescriptor config) | 149 UsbConfigDescriptorImpl::UsbConfigDescriptorImpl( |
| 150 PlatformUsbConfigDescriptor config) |
149 : config_(config) { | 151 : config_(config) { |
| 152 DCHECK(config); |
150 } | 153 } |
151 | 154 |
152 // TODO(zvorygin): Used for tests only. Should be removed when | 155 UsbConfigDescriptorImpl::~UsbConfigDescriptorImpl() { |
153 // all interfaces are extracted properly. | 156 libusb_free_config_descriptor(config_); |
154 UsbConfigDescriptor::UsbConfigDescriptor() { | |
155 config_ = NULL; | |
156 } | 157 } |
157 | 158 |
158 UsbConfigDescriptor::~UsbConfigDescriptor() { | 159 size_t UsbConfigDescriptorImpl::GetNumInterfaces() const { |
159 if (config_ != NULL) { | |
160 libusb_free_config_descriptor(config_); | |
161 config_ = NULL; | |
162 } | |
163 } | |
164 | |
165 size_t UsbConfigDescriptor::GetNumInterfaces() const { | |
166 return config_->bNumInterfaces; | 160 return config_->bNumInterfaces; |
167 } | 161 } |
168 | 162 |
169 scoped_refptr<const UsbInterfaceDescriptor> UsbConfigDescriptor::GetInterface( | 163 scoped_refptr<const UsbInterfaceDescriptor> |
170 size_t index) const { | 164 UsbConfigDescriptorImpl::GetInterface(size_t index) const { |
171 return new UsbInterfaceDescriptor(this, &config_->interface[index]); | 165 return new UsbInterfaceDescriptorImpl(this, &config_->interface[index]); |
172 } | 166 } |
173 | 167 |
174 } // namespace usb_service | 168 } // namespace usb_service |
OLD | NEW |