| 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 // A <code>Device</code> uniquely identifies a connected USB device. |
| 23 // device. They are returned by findDevice calls and have all of their | 23 // They are returned by <code>getDevices</code> calls and the |
| 24 // fields populated before being returned. | 24 // <code>device</code> field remains consistent for the same device. |
| 25 dictionary Device { | 25 dictionary Device { |
| 26 long device; |
| 27 long vendorId; |
| 28 long productId; |
| 29 }; |
| 30 |
| 31 // A <code>DeviceHandle</code> encapsulates everything needed to communicate |
| 32 // with a USB device. |
| 33 // They are returned by <code>openDevice</code> calls. |
| 34 dictionary DeviceHandle { |
| 26 long handle; | 35 long handle; |
| 27 long vendorId; | 36 long vendorId; |
| 28 long productId; | 37 long productId; |
| 29 }; | 38 }; |
| 30 | 39 |
| 31 dictionary EndpointDescriptor { | 40 dictionary EndpointDescriptor { |
| 32 long address; | 41 long address; |
| 33 TransferType type; | 42 TransferType type; |
| 34 Direction direction; | 43 Direction direction; |
| 35 long maximumPacketSize; | 44 long maximumPacketSize; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 dictionary TransferResultInfo { | 123 dictionary TransferResultInfo { |
| 115 // A value of 0 indicates that the transfer was a success. Other values | 124 // A value of 0 indicates that the transfer was a success. Other values |
| 116 // indicate failure. | 125 // indicate failure. |
| 117 long? resultCode; | 126 long? resultCode; |
| 118 | 127 |
| 119 // If the transfer was an input transfer then this field will contain all | 128 // If the transfer was an input transfer then this field will contain all |
| 120 // of the input data requested. | 129 // of the input data requested. |
| 121 ArrayBuffer? data; | 130 ArrayBuffer? data; |
| 122 }; | 131 }; |
| 123 | 132 |
| 124 // FindDevicesOptions describes the properties of devices which are found and | 133 // GetDevicesOptions describes the properties of devices which are found and |
| 125 // opened via findDevices. | 134 // opened via getDevices. |
| 126 dictionary FindDevicesOptions { | 135 dictionary GetDevicesOptions { |
| 127 long vendorId; | 136 long vendorId; |
| 128 long productId; | 137 long productId; |
| 129 long? interfaceId; | 138 long? interfaceId; |
| 130 }; | 139 }; |
| 131 | 140 |
| 132 callback VoidCallback = void (); | 141 callback VoidCallback = void (); |
| 133 callback FindDevicesCallback = void (Device[] device); | 142 callback GetDevicesCallback = void (Device[] devices); |
| 143 callback FindDevicesCallback = void (DeviceHandle[] handles); |
| 144 callback OpenDeviceCallback = void (DeviceHandle handle); |
| 134 callback ListInterfacesCallback = void (InterfaceDescriptor[] descriptors); | 145 callback ListInterfacesCallback = void (InterfaceDescriptor[] descriptors); |
| 135 callback CloseDeviceCallback = void (); | 146 callback CloseDeviceCallback = void (); |
| 136 callback TransferCallback = void (TransferResultInfo info); | 147 callback TransferCallback = void (TransferResultInfo info); |
| 137 callback ResetDeviceCallback = void(boolean result); | 148 callback ResetDeviceCallback = void(boolean result); |
| 138 | 149 |
| 139 interface Functions { | 150 interface Functions { |
| 140 // Finds the first instance of the USB device specified by the vendorId/ | 151 // Get all instances of the USB devices specified by the vendorId/ |
| 141 // productId pair and, if permissions allow, opens it for use. | 152 // productId pair. |
| 142 // Upon successfully opening a device the callback is invoked with a | 153 // |
| 143 // populated Device object. On failure, the callback is invoked with null. | 154 // Upon successfully enumeration, the callback will be called with an array |
| 155 // of <code>Device</code> objects. <code>Device</code> objects can further |
| 156 // be opened by <code>openDevice</code> before any communication takes |
| 157 // place. |
| 158 // |
| 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 after the enumeration completes. |
| 146 static void findDevices(FindDevicesOptions options, | 161 static void getDevices(GetDevicesOptions options, |
| 147 FindDevicesCallback callback); | 162 GetDevicesCallback callback); |
| 148 | 163 |
| 149 // Closes an open device instance. Invoking operations on a device after it | 164 // Open all instances of the USB devices specified by the vendorId/ |
| 150 // has been closed is a safe operation, but causes no action to be taken. | 165 // productId pair. |
| 151 // |device|: The device to close. | 166 // |
| 167 // Upon successfully enumeration, the callback will be called with an array |
| 168 // of <code>DeviceHandle</code> objects. Developer is responsible to call |
| 169 // <code>closeDevice</code> on every <code>DeviceHandle</code>, otherwise |
| 170 // the USB device will not be closed even if the app not long possess any |
| 171 // reference to the <code>DeviceHanlde</code> object. |
| 172 // |
| 173 // |options|: The properties to search for on target devices. |
| 174 // |callback|: Invoked after the enumeration completes. |
| 175 static void findDevices(GetDevicesOptions options, |
| 176 FindDevicesCallback callback); |
| 177 |
| 178 // Create a <code>DeviceHandle</code> for further operations. |
| 179 // |
| 180 // Multiple <code>DeviceHandle</code>s can be created for a same USB Device |
| 181 // and it is possible to use those handles independently with the device |
| 182 // support. |
| 183 // |
| 184 // Developers are responsible to call <code>closeDevice</code> to close |
| 185 // the handle, otherwise the device handle will not be closed even if there |
| 186 // is no reference to the <code>DeviceHandle</code> object. |
| 187 // |device|: The device to be opened. |
| 188 // |callback|: Invoked with the created <code>DeviceHandle</code>. |
| 189 static void openDevice(Device device, |
| 190 OpenDeviceCallback callback); |
| 191 |
| 192 // Closes an device handle. This operation does not effect other handles |
| 193 // of the same device. |
| 194 // |handle|: The device handle to close. |
| 152 // |callback|: The callback to invoke once the device is closed. | 195 // |callback|: The callback to invoke once the device is closed. |
| 153 static void closeDevice(Device device, | 196 static void closeDevice(DeviceHandle handle, |
| 154 optional CloseDeviceCallback callback); | 197 optional CloseDeviceCallback callback); |
| 155 | 198 |
| 156 // Lists all the interfaces on the USB device. | 199 // Lists all the interfaces on the USB device. |
| 157 // |device|: The device from which the interfaces should be listed. | 200 // |handle|: The handle of the device from which the interfaces should be |
| 201 // listed. |
| 158 // |callback|: The callback to invoke when the interfaces are enumerated. | 202 // |callback|: The callback to invoke when the interfaces are enumerated. |
| 159 static void listInterfaces(Device device, | 203 static void listInterfaces(DeviceHandle handle, |
| 160 ListInterfacesCallback callback); | 204 ListInterfacesCallback callback); |
| 161 | 205 |
| 162 // Claims an interface on the specified USB device. | 206 // Claims an interface on the specified USB device. |
| 163 // |device|: The device on which the interface is to be claimed. | 207 // |device|: The device on which the interface is to be claimed. |
| 164 // |interface|: The interface number to be claimed. | 208 // |interface|: The interface number to be claimed. |
| 165 // |callback|: The callback to invoke once the interface is claimed. | 209 // |callback|: The callback to invoke once the interface is claimed. |
| 166 static void claimInterface(Device device, long interfaceNumber, | 210 static void claimInterface(DeviceHandle handle, long interfaceNumber, |
| 167 VoidCallback callback); | 211 VoidCallback callback); |
| 168 | 212 |
| 169 // Releases a claim to an interface on the provided device. | 213 // Releases a claim to an interface on the provided device. |
| 170 // |device|: The device on which the interface is to be released. | 214 // |handle|: The handle of the device on which the interface is to be |
| 215 // released. |
| 171 // |interface|: The interface number to be released. | 216 // |interface|: The interface number to be released. |
| 172 // |callback|: The callback to invoke once the interface is released. | 217 // |callback|: The callback to invoke once the interface is released. |
| 173 static void releaseInterface(Device device, long interfaceNumber, | 218 static void releaseInterface(DeviceHandle handle, long interfaceNumber, |
| 174 VoidCallback callback); | 219 VoidCallback callback); |
| 175 | 220 |
| 176 // Selects an alternate setting on a previously claimed interface on a | 221 // Selects an alternate setting on a previously claimed interface on a |
| 177 // device. | 222 // device. |
| 178 // |device|: The device on which the interface settings are to be set. | 223 // |handle|: The handle of the device on which the interface settings are |
| 224 // to be set. |
| 179 // |interface|: The interface number to be set. | 225 // |interface|: The interface number to be set. |
| 180 // |alternateSetting|: The alternate setting to set. | 226 // |alternateSetting|: The alternate setting to set. |
| 181 // |callback|: The callback to invoke once the interface setting is set. | 227 // |callback|: The callback to invoke once the interface setting is set. |
| 182 static void setInterfaceAlternateSetting(Device device, | 228 static void setInterfaceAlternateSetting(DeviceHandle handle, |
| 183 long interfaceNumber, long alternateSetting, VoidCallback callback); | 229 long interfaceNumber, long alternateSetting, VoidCallback callback); |
| 184 | 230 |
| 185 // Performs a control transfer on the specified device. See the | 231 // Performs a control transfer on the specified device. See the |
| 186 // ControlTransferInfo structure for the parameters required to make a | 232 // ControlTransferInfo structure for the parameters required to make a |
| 187 // transfer. | 233 // transfer. |
| 188 // |device|: An open device to make the transfer on. | 234 // |handle|: An device handle to make the transfer on. |
| 189 // |transferInfo|: The parameters to the transfer. See ControlTransferInfo. | 235 // |transferInfo|: The parameters to the transfer. See ControlTransferInfo. |
| 190 // |callback|: Invoked once the transfer has completed. | 236 // |callback|: Invoked once the transfer has completed. |
| 191 static void controlTransfer(Device device, | 237 static void controlTransfer(DeviceHandle handle, |
| 192 ControlTransferInfo transferInfo, TransferCallback callback); | 238 ControlTransferInfo transferInfo, 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|: An device handle to make the transfer on. |
| 196 // |transferInfo|: The paramters to the transfer. See GenericTransferInfo. | 242 // |transferInfo|: The paramters 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, GenericTransferInfo transferIn
fo, |
| 199 TransferCallback callback); | 245 TransferCallback callback); |
| 200 | 246 |
| 201 // Performs an interrupt transfer on the specified device. | 247 // Performs an interrupt transfer on the specified device. |
| 202 // |device|: An open device to make the transfer on. | 248 // |handle|: An device handle to make the transfer on. |
| 203 // |transferInfo|: The paramters to the transfer. See GenericTransferInfo. | 249 // |transferInfo|: The paramters to the transfer. See GenericTransferInfo. |
| 204 // |callback|: Invoked once the transfer has completed. | 250 // |callback|: Invoked once the transfer has completed. |
| 205 static void interruptTransfer(Device device, | 251 static void interruptTransfer(DeviceHandle handle, |
| 206 GenericTransferInfo transferInfo, TransferCallback callback); | 252 GenericTransferInfo transferInfo, TransferCallback callback); |
| 207 | 253 |
| 208 // Performs an isochronous transfer on the specific device. | 254 // Performs an isochronous transfer on the specific device. |
| 209 // |device|: An open device to make the transfer on. | 255 // |handle|: An device handle to make the transfer on. |
| 210 // |transferInfo|: The parameters to the transfer. See | 256 // |transferInfo|: The parameters to the transfer. See |
| 211 // IsochronousTransferInfo. | 257 // IsochronousTransferInfo. |
| 212 // |callback|: Invoked once the transfer has been completed. | 258 // |callback|: Invoked once the transfer has been completed. |
| 213 static void isochronousTransfer(Device device, | 259 static void isochronousTransfer(DeviceHandle handle, |
| 214 IsochronousTransferInfo transferInfo, | 260 IsochronousTransferInfo transferInfo, |
| 215 TransferCallback callback); | 261 TransferCallback callback); |
| 216 | 262 |
| 217 // Try to reset the USB device and restore the previous status. | 263 // Try to reset the USB device and restore the previous status. |
| 218 // | 264 // |
| 219 // If the reset fails, the given device will be closed and the USB device | 265 // If the reset fails, the given device will be closed and the USB device |
| 220 // will appear to be disconected and reconnected. | 266 // will appear to be disconected and reconnected. |
| 221 // You must call <code>findDevice</code> again to acquire the device. | 267 // You must call <code>findDevice</code> again to acquire the device. |
| 222 // | 268 // |
| 223 // |device|: An opened device to reset. | 269 // |handle|: An device handle to reset. |
| 224 // |callback|: Invoked once the device is reset with a boolean indicating | 270 // |callback|: Invoked once the device is reset with a boolean indicating |
| 225 // whether the reset is completed successfully. | 271 // whether the reset is completed successfully. |
| 226 static void resetDevice(Device device, | 272 static void resetDevice(DeviceHandle handle, |
| 227 ResetDeviceCallback callback); | 273 ResetDeviceCallback callback); |
| 228 }; | 274 }; |
| 229 }; | 275 }; |
| OLD | NEW |