OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Use the <code>chrome.usb</code> API to interact with connected USB | 5 // Use the <code>chrome.usb</code> API to interact with connected USB |
6 // devices. This API provides access to USB operations from within the context | 6 // devices. This API provides access to USB operations from within the context |
7 // of an app. Using this API, apps can function as drivers for hardware devices. | 7 // of an app. Using this API, apps can function as drivers for hardware devices. |
8 namespace usb { | 8 namespace usb { |
9 | 9 |
10 // Direction, Recipient, RequestType, and TransferType all map to their | 10 // Direction, Recipient, RequestType, and TransferType all map to their |
11 // namesakes within the USB specification. | 11 // namesakes within the USB specification. |
12 enum Direction {in, out}; | 12 enum Direction {in, out}; |
13 enum Recipient {device, _interface, endpoint, other}; | 13 enum Recipient {device, _interface, endpoint, other}; |
14 enum RequestType {standard, class, vendor, reserved}; | 14 enum RequestType {standard, class, vendor, reserved}; |
15 enum TransferType {control, interrupt, isochronous, bulk}; | 15 enum TransferType {control, interrupt, isochronous, bulk}; |
16 | 16 |
17 // For isochronous mode, SynchronizationType and UsageType map to their | 17 // For isochronous mode, SynchronizationType and UsageType map to their |
18 // namesakes within the USB specification. | 18 // namesakes within the USB specification. |
19 enum SynchronizationType {asynchronous, adaptive, synchronous}; | 19 enum SynchronizationType {asynchronous, adaptive, synchronous}; |
20 enum UsageType {data, feedback, explicitFeedback}; | 20 enum UsageType {data, feedback, explicitFeedback}; |
21 | 21 |
22 // A Device encapsulates everything that is needed to communicate with a USB | 22 // Returned by |getDevices| to identify a connected USB device. |
23 // device. They are returned by findDevice calls and have all of their | |
24 // fields populated before being returned. | |
25 dictionary Device { | 23 dictionary Device { |
24 // The id of the USB device. It remains unchanged until the device is | |
25 // unplugged. | |
26 long device; | |
27 long vendorId; | |
28 long productId; | |
29 }; | |
30 | |
31 // Returned by |openDevice| to be used for USB communication. Every time a | |
32 // device is opened, a new device handle is created. All device handles can | |
33 // work together if the device allows it. The device handle will be closed | |
34 // when the app is reloaded or suspended. | |
35 // | |
36 // When a device handle is closed by calling |closeDevice| or unplugging the | |
37 // device physically, all the interfaces it claimed will be released and all | |
38 // the transfers in progress will be canceled immediately. | |
39 dictionary DeviceHandle { | |
40 // The id of the USB device handle for. It represents different kinds of | |
scheib
2013/08/29 20:47:58
This rewording sounds wrong. Delete 'for'?
Bei Zhang
2013/08/29 21:02:05
I gave up describing the difference between the tw
| |
41 // identifiers from |Device.device| field. | |
26 long handle; | 42 long handle; |
27 long vendorId; | 43 long vendorId; |
28 long productId; | 44 long productId; |
29 }; | 45 }; |
30 | 46 |
31 dictionary EndpointDescriptor { | 47 dictionary EndpointDescriptor { |
32 long address; | 48 long address; |
33 TransferType type; | 49 TransferType type; |
34 Direction direction; | 50 Direction direction; |
35 long maximumPacketSize; | 51 long maximumPacketSize; |
(...skipping 30 matching lines...) Expand all Loading... | |
66 | 82 |
67 long request; | 83 long request; |
68 long value; | 84 long value; |
69 long index; | 85 long index; |
70 | 86 |
71 // If this transfer is an input transfer, then this field must be set to | 87 // If this transfer is an input transfer, then this field must be set to |
72 // indicate the expected data length. If this is an output transfer, then | 88 // indicate the expected data length. If this is an output transfer, then |
73 // this field is ignored. | 89 // this field is ignored. |
74 long? length; | 90 long? length; |
75 | 91 |
76 // The data payload carried by this transfer. If this is an output tranfer | 92 // The data payload carried by this transfer. If this is an output transfer |
77 // then this field must be set. | 93 // then this field must be set. |
78 ArrayBuffer? data; | 94 ArrayBuffer? data; |
79 }; | 95 }; |
80 | 96 |
81 // GenericTransferInfo is used by both bulk and interrupt transfers to | 97 // GenericTransferInfo is used by both bulk and interrupt transfers to |
82 // specify the parameters of the transfer. | 98 // specify the parameters of the transfer. |
83 dictionary GenericTransferInfo { | 99 dictionary GenericTransferInfo { |
84 // The direction of this transfer. | 100 // The direction of this transfer. |
85 Direction direction; | 101 Direction direction; |
86 | 102 |
(...skipping 27 matching lines...) Expand all Loading... | |
114 dictionary TransferResultInfo { | 130 dictionary TransferResultInfo { |
115 // A value of 0 indicates that the transfer was a success. Other values | 131 // A value of 0 indicates that the transfer was a success. Other values |
116 // indicate failure. | 132 // indicate failure. |
117 long? resultCode; | 133 long? resultCode; |
118 | 134 |
119 // If the transfer was an input transfer then this field will contain all | 135 // If the transfer was an input transfer then this field will contain all |
120 // of the input data requested. | 136 // of the input data requested. |
121 ArrayBuffer? data; | 137 ArrayBuffer? data; |
122 }; | 138 }; |
123 | 139 |
124 // FindDevicesOptions describes the properties of devices which are found and | 140 // Describes the properties of devices which are found via |getDevices| and |
125 // opened via findDevices. | 141 // |findDevices|. |
126 dictionary FindDevicesOptions { | 142 dictionary EnumerateDevicesOptions { |
127 long vendorId; | 143 long vendorId; |
128 long productId; | 144 long productId; |
129 long? interfaceId; | 145 long? interfaceId; |
130 }; | 146 }; |
131 | 147 |
132 callback VoidCallback = void (); | 148 callback VoidCallback = void (); |
133 callback FindDevicesCallback = void (Device[] device); | 149 callback GetDevicesCallback = void (Device[] devices); |
150 callback OpenDeviceCallback = void (DeviceHandle handle); | |
151 callback FindDevicesCallback = void (DeviceHandle[] handles); | |
134 callback ListInterfacesCallback = void (InterfaceDescriptor[] descriptors); | 152 callback ListInterfacesCallback = void (InterfaceDescriptor[] descriptors); |
135 callback CloseDeviceCallback = void (); | 153 callback CloseDeviceCallback = void (); |
136 callback TransferCallback = void (TransferResultInfo info); | 154 callback TransferCallback = void (TransferResultInfo info); |
137 callback ResetDeviceCallback = void(boolean result); | 155 callback ResetDeviceCallback = void(boolean result); |
138 | 156 |
139 interface Functions { | 157 interface Functions { |
140 // Finds the first instance of the USB device specified by the vendorId/ | 158 // Lists USB devices specified by vendorId/productId/interfaceId tuple. |
141 // productId pair and, if permissions allow, opens it for use. | |
142 // Upon successfully opening a device the callback is invoked with a | |
143 // populated Device object. On failure, the callback is invoked with null. | |
144 // |options|: The properties to search for on target devices. | 159 // |options|: The properties to search for on target devices. |
145 // |callback|: Invoked with the opened Device on success. | 160 // |callback|: Invoked with a list of |Device|s on complete. |
146 static void findDevices(FindDevicesOptions options, | 161 static void getDevices(EnumerateDevicesOptions options, |
147 FindDevicesCallback callback); | 162 GetDevicesCallback callback); |
148 | 163 |
149 // Closes an open device instance. Invoking operations on a device after it | 164 // Opens a USB device returned by |getDevices|. |
165 // |device|: The device to open. | |
166 // |callback|: Invoked with the created DeviceHandle on complete. | |
167 static void openDevice(Device device, OpenDeviceCallback callback); | |
168 | |
169 // Finds USB devices specified by the vendorId/productId/interfaceId tuple | |
170 // and, if permissions allow, opens them for use. | |
171 // | |
172 // If a device is failed to be opened, its handles will not be returned. | |
173 // | |
174 // Calling this method is equivalent to a |getDevices| followed by | |
175 // a series of |openDevice| calls, and returning all the successfully opened | |
176 // device handles. | |
177 // |options|: The properties to search for on target devices. | |
178 // |callback|: Invoked with the opened DeviceHandle on complete. | |
179 static void findDevices(EnumerateDevicesOptions options, | |
180 FindDevicesCallback callback); | |
181 | |
182 // Closes a device handle. Invoking operations on a device after it | |
150 // has been closed is a safe operation, but causes no action to be taken. | 183 // has been closed is a safe operation, but causes no action to be taken. |
151 // |device|: The device to close. | 184 // |handle|: The device handle to close. |
152 // |callback|: The callback to invoke once the device is closed. | 185 // |callback|: The callback to invoke once the device is closed. |
153 static void closeDevice(Device device, | 186 static void closeDevice(DeviceHandle handle, |
154 optional CloseDeviceCallback callback); | 187 optional CloseDeviceCallback callback); |
155 | 188 |
156 // Lists all the interfaces on the USB device. | 189 // Lists all the interfaces on the USB device. |
157 // |device|: The device from which the interfaces should be listed. | 190 // |handle|: The device from which the interfaces should be listed. |
158 // |callback|: The callback to invoke when the interfaces are enumerated. | 191 // |callback|: The callback to invoke when the interfaces are enumerated. |
159 static void listInterfaces(Device device, | 192 static void listInterfaces(DeviceHandle handle, |
160 ListInterfacesCallback callback); | 193 ListInterfacesCallback callback); |
161 | 194 |
162 // Claims an interface on the specified USB device. | 195 // Claims an interface on the specified USB device. |
163 // |device|: The device on which the interface is to be claimed. | 196 // Before you can transfer data with endpoints, you must claim their parent |
197 // interfaces. Only one device handle on the same host can claim each | |
198 // interface. If the interface is already claimed, this call will fail. | |
199 // | |
200 // You shall call releaseInterface when the interface is not needed anymore. | |
201 // | |
202 // |handle|: The device on which the interface is to be claimed. | |
164 // |interface|: The interface number to be claimed. | 203 // |interface|: The interface number to be claimed. |
165 // |callback|: The callback to invoke once the interface is claimed. | 204 // |callback|: The callback to invoke once the interface is claimed. |
166 static void claimInterface(Device device, long interfaceNumber, | 205 static void claimInterface(DeviceHandle handle, long interfaceNumber, |
167 VoidCallback callback); | 206 VoidCallback callback); |
168 | 207 |
169 // Releases a claim to an interface on the provided device. | 208 // Releases a claim to an interface on the provided device. |
170 // |device|: The device on which the interface is to be released. | 209 // |handle|: The device on which the interface is to be released. |
171 // |interface|: The interface number to be released. | 210 // |interface|: The interface number to be released. |
172 // |callback|: The callback to invoke once the interface is released. | 211 // |callback|: The callback to invoke once the interface is released. |
173 static void releaseInterface(Device device, long interfaceNumber, | 212 static void releaseInterface(DeviceHandle handle, long interfaceNumber, |
174 VoidCallback callback); | 213 VoidCallback callback); |
175 | 214 |
176 // Selects an alternate setting on a previously claimed interface on a | 215 // Selects an alternate setting on a previously claimed interface on a |
177 // device. | 216 // device. |
178 // |device|: The device on which the interface settings are to be set. | 217 // |handle|: The device on which the interface settings are to be set. |
179 // |interface|: The interface number to be set. | 218 // |interface|: The interface number to be set. |
180 // |alternateSetting|: The alternate setting to set. | 219 // |alternateSetting|: The alternate setting to set. |
181 // |callback|: The callback to invoke once the interface setting is set. | 220 // |callback|: The callback to invoke once the interface setting is set. |
182 static void setInterfaceAlternateSetting(Device device, | 221 static void setInterfaceAlternateSetting(DeviceHandle handle, |
183 long interfaceNumber, long alternateSetting, VoidCallback callback); | 222 long interfaceNumber, |
223 long alternateSetting, | |
224 VoidCallback callback); | |
184 | 225 |
185 // Performs a control transfer on the specified device. See the | 226 // Performs a control transfer on the specified device. See the |
186 // ControlTransferInfo structure for the parameters required to make a | 227 // ControlTransferInfo structure for the parameters required to make a |
187 // transfer. | 228 // transfer. |
188 // |device|: An open device to make the transfer on. | 229 // |
230 // Conceptually control transfer talks to the device itself. You do not need | |
231 // to claim interface 0 to perform a control transfer. | |
232 // | |
233 // |handle|: A device handle to make the transfer on. | |
189 // |transferInfo|: The parameters to the transfer. See ControlTransferInfo. | 234 // |transferInfo|: The parameters to the transfer. See ControlTransferInfo. |
190 // |callback|: Invoked once the transfer has completed. | 235 // |callback|: Invoked once the transfer has completed. |
191 static void controlTransfer(Device device, | 236 static void controlTransfer(DeviceHandle handle, |
192 ControlTransferInfo transferInfo, TransferCallback callback); | 237 ControlTransferInfo transferInfo, |
238 TransferCallback callback); | |
193 | 239 |
194 // Performs a bulk transfer on the specified device. | 240 // Performs a bulk transfer on the specified device. |
195 // |device|: An open device to make the transfer on. | 241 // |handle|: A device handle to make the transfer on. |
196 // |transferInfo|: The paramters to the transfer. See GenericTransferInfo. | 242 // |transferInfo|: The parameters to the transfer. See GenericTransferInfo. |
197 // |callback|: Invoked once the transfer has completed. | 243 // |callback|: Invoked once the transfer has completed. |
198 static void bulkTransfer(Device device, GenericTransferInfo transferInfo, | 244 static void bulkTransfer(DeviceHandle handle, |
199 TransferCallback callback); | 245 GenericTransferInfo transferInfo, |
246 TransferCallback callback); | |
200 | 247 |
201 // Performs an interrupt transfer on the specified device. | 248 // Performs an interrupt transfer on the specified device. |
202 // |device|: An open device to make the transfer on. | 249 // |handle|: A device handle to make the transfer on. |
203 // |transferInfo|: The paramters to the transfer. See GenericTransferInfo. | 250 // |transferInfo|: The parameters to the transfer. See GenericTransferInfo. |
204 // |callback|: Invoked once the transfer has completed. | 251 // |callback|: Invoked once the transfer has completed. |
205 static void interruptTransfer(Device device, | 252 static void interruptTransfer(DeviceHandle handle, |
206 GenericTransferInfo transferInfo, TransferCallback callback); | 253 GenericTransferInfo transferInfo, |
254 TransferCallback callback); | |
207 | 255 |
208 // Performs an isochronous transfer on the specific device. | 256 // Performs an isochronous transfer on the specific device. |
209 // |device|: An open device to make the transfer on. | 257 // |handle|: A device handle to make the transfer on. |
210 // |transferInfo|: The parameters to the transfer. See | 258 // |transferInfo|: The parameters to the transfer. See |
211 // IsochronousTransferInfo. | 259 // IsochronousTransferInfo. |
212 // |callback|: Invoked once the transfer has been completed. | 260 // |callback|: Invoked once the transfer has been completed. |
213 static void isochronousTransfer(Device device, | 261 static void isochronousTransfer(DeviceHandle handle, |
214 IsochronousTransferInfo transferInfo, | 262 IsochronousTransferInfo transferInfo, |
215 TransferCallback callback); | 263 TransferCallback callback); |
216 | 264 |
217 // Try to reset the USB device and restore the previous status. | 265 // Tries to reset the USB device and restores it to the previous status. |
266 // If the reset fails, the given device handle will be closed and the USB | |
267 // device will appear to be disconnected then reconnected. In that case you | |
268 // must call |getDevices| or |findDevices| again to acquire the device. | |
218 // | 269 // |
219 // If the reset fails, the given device will be closed and the USB device | 270 // |handle|: A device handle to reset. |
220 // will appear to be disconected and reconnected. | |
221 // You must call <code>findDevice</code> again to acquire the device. | |
222 // | |
223 // |device|: An opened device to reset. | |
224 // |callback|: Invoked once the device is reset with a boolean indicating | 271 // |callback|: Invoked once the device is reset with a boolean indicating |
225 // whether the reset is completed successfully. | 272 // whether the reset is completed successfully. |
226 static void resetDevice(Device device, | 273 static void resetDevice(DeviceHandle handle, |
227 ResetDeviceCallback callback); | 274 ResetDeviceCallback callback); |
228 }; | 275 }; |
229 }; | 276 }; |
OLD | NEW |