usb 1.9.1 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +4 -0
- package/.eslintrc.json +87 -0
- package/.gitattributes +1 -0
- package/.vscode/launch.json +15 -0
- package/.vscode/tasks.json +23 -0
- package/CHANGELOG.md +13 -0
- package/README.md +400 -175
- package/dist/index.d.ts +17 -0
- package/dist/index.js +133 -0
- package/dist/index.js.map +1 -0
- package/dist/usb/bindings.d.ts +253 -0
- package/dist/usb/bindings.js +10 -0
- package/dist/usb/bindings.js.map +1 -0
- package/dist/usb/capability.d.ts +14 -0
- package/dist/usb/capability.js +18 -0
- package/dist/usb/capability.js.map +1 -0
- package/dist/usb/descriptors.d.ts +129 -0
- package/dist/usb/descriptors.js +3 -0
- package/dist/usb/descriptors.js.map +1 -0
- package/dist/usb/device.d.ts +94 -0
- package/dist/usb/device.js +300 -0
- package/dist/usb/device.js.map +1 -0
- package/dist/usb/endpoint.d.ts +91 -0
- package/dist/usb/endpoint.js +236 -0
- package/dist/usb/endpoint.js.map +1 -0
- package/dist/usb/index.d.ts +25 -0
- package/dist/usb/index.js +116 -0
- package/dist/usb/index.js.map +1 -0
- package/dist/usb/interface.d.ts +78 -0
- package/dist/usb/interface.js +137 -0
- package/dist/usb/interface.js.map +1 -0
- package/dist/webusb/index.d.ts +56 -0
- package/dist/webusb/index.js +412 -0
- package/dist/webusb/index.js.map +1 -0
- package/dist/webusb/mutex.d.ts +22 -0
- package/dist/webusb/mutex.js +89 -0
- package/dist/webusb/mutex.js.map +1 -0
- package/dist/webusb/webusb-device.d.ts +49 -0
- package/dist/webusb/webusb-device.js +888 -0
- package/dist/webusb/webusb-device.js.map +1 -0
- package/package.json +28 -15
- package/prebuilds/android-arm/node.napi.armv7.node +0 -0
- package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
- package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
- package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
- package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
- package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
- package/prebuilds/linux-ia32/node.napi.node +0 -0
- package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
- package/prebuilds/linux-x64/node.napi.musl.node +0 -0
- package/prebuilds/win32-ia32/node.napi.node +0 -0
- package/prebuilds/win32-x64/node.napi.node +0 -0
- package/src/device.cc +6 -6
- package/test/usb.coffee +13 -7
- package/test/webusb.coffee +189 -0
- package/tsc/index.ts +67 -0
- package/tsc/usb/bindings.ts +304 -0
- package/tsc/usb/capability.ts +22 -0
- package/tsc/usb/descriptors.ts +180 -0
- package/tsc/usb/device.ts +325 -0
- package/tsc/usb/endpoint.ts +228 -0
- package/tsc/usb/index.ts +111 -0
- package/tsc/usb/interface.ts +172 -0
- package/tsc/webusb/index.ts +363 -0
- package/tsc/webusb/mutex.ts +38 -0
- package/tsc/webusb/webusb-device.ts +534 -0
- package/tsconfig.json +17 -0
- package/typedoc.json +9 -0
- package/usb.js +0 -573
package/README.md
CHANGED
|
@@ -1,112 +1,340 @@
|
|
|
1
|
-
USB Library for Node.JS
|
|
2
|
-
===============================
|
|
1
|
+
# USB Library for Node.JS
|
|
3
2
|
|
|
4
3
|
[](https://github.com/node-usb/node-usb/actions)
|
|
4
|
+
[](https://www.npmjs.com/package/usb)
|
|
5
|
+
[](http://opensource.org/licenses/MIT)
|
|
5
6
|
|
|
6
|
-
Node.JS library for communicating with USB devices
|
|
7
|
+
Node.JS library for communicating with USB devices.
|
|
7
8
|
|
|
8
|
-
This is a refactoring / rewrite of Christopher Klein's [node-usb](https://github.com/schakko/node-usb).
|
|
9
|
+
This is a refactoring / rewrite of Christopher Klein's [node-usb](https://github.com/schakko/node-usb).
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
# Prerequisites
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
============
|
|
13
|
+
[Node.js >= v10.16.0](https://nodejs.org), which includes `npm`.
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
On Windows, use [Zadig](http://zadig.akeo.ie/) to install the WinUSB driver for your USB device. Otherwise you will get `LIBUSB_ERROR_NOT_SUPPORTED` when attempting to open devices.
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
# Installation
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
Native modules are bundled using `prebuildify`, so installation should be as simple as installing the package.
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
With `npm`:
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
```bash
|
|
24
|
+
npm install usb
|
|
25
|
+
```
|
|
25
26
|
|
|
27
|
+
With `yarn`:
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
```bash
|
|
30
|
+
yarn add usb
|
|
31
|
+
```
|
|
29
32
|
|
|
30
|
-
|
|
33
|
+
__Note:__ the library is now written in `TypeScript`, so a separate types file is not longer required to be installed.
|
|
31
34
|
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
# License
|
|
36
|
+
[MIT](LICENSE.md)
|
|
34
37
|
|
|
35
|
-
|
|
38
|
+
Note that the compiled Node extension includes [libusb](https://github.com/libusb/libusb), and is thus subject to the [LGPL](https://github.com/libusb/libusb/blob/master/COPYING).
|
|
36
39
|
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
# Limitations
|
|
41
|
+
Does not support:
|
|
42
|
+
|
|
43
|
+
- Configurations other than the default one
|
|
44
|
+
- Isochronous transfers
|
|
45
|
+
|
|
46
|
+
# Getting Started
|
|
47
|
+
Use the following examples to kickstart your development. Once you have a desired device, use the APIs below to interact with it.
|
|
48
|
+
|
|
49
|
+
## Migrating to `v2.0.0`
|
|
50
|
+
The legacy API exists on an object called `usb` on the main import and the convenience functions exist as top level objects.
|
|
51
|
+
|
|
52
|
+
To use `v2.0.0` simply update your import statements and the function calls;
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
// import * as usb from 'usb';
|
|
56
|
+
// const devices: usb.Device[] = usb.getDeviceList();
|
|
57
|
+
|
|
58
|
+
import { usb, getDeviceList } from 'usb';
|
|
59
|
+
const devices: usb.Device[] = getDeviceList();
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## List all legacy devices
|
|
63
|
+
```typescript
|
|
64
|
+
import { getDeviceList } from 'usb';
|
|
65
|
+
|
|
66
|
+
const devices = getDeviceList();
|
|
67
|
+
|
|
68
|
+
for (const device of devices) {
|
|
69
|
+
console.log(device); // Legacy device
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Find legacy device by vid/pid
|
|
74
|
+
```typescript
|
|
75
|
+
import { findByIds } from 'usb';
|
|
76
|
+
|
|
77
|
+
const device = findByIds(0x59e3, 0x0a23);
|
|
78
|
+
|
|
79
|
+
if (device) {
|
|
80
|
+
console.log(device); // Legacy device
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Find legacy device by SerialNumber
|
|
85
|
+
```typescript
|
|
86
|
+
import { findBySerialNumber } from 'usb';
|
|
87
|
+
|
|
88
|
+
(async () => {
|
|
89
|
+
// Uses a blocking call, so is async
|
|
90
|
+
const device = await findBySerialNumber('TEST_DEVICE');
|
|
91
|
+
|
|
92
|
+
if (device) {
|
|
93
|
+
console.log(device); // Legacy device
|
|
94
|
+
}
|
|
95
|
+
})();
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Turn legacy Device into WebUSB compatible device
|
|
99
|
+
```typescript
|
|
100
|
+
import { findBySerialNumber, WebUSBDevice } from 'usb';
|
|
101
|
+
|
|
102
|
+
(async () => {
|
|
103
|
+
// Uses a blocking call, so is async
|
|
104
|
+
const device = await findBySerialNumber('TEST_DEVICE');
|
|
105
|
+
|
|
106
|
+
// Uses blocking calls, so is async
|
|
107
|
+
const webDevice = await WebUSBDevice.createInstance(device);
|
|
108
|
+
|
|
109
|
+
if (webDevice) {
|
|
110
|
+
console.log(webDevice); // WebUSB device
|
|
111
|
+
}
|
|
112
|
+
})();
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Use WebUSB approach to find a device
|
|
116
|
+
```typescript
|
|
117
|
+
import { webusb } from 'usb';
|
|
118
|
+
|
|
119
|
+
(async () => {
|
|
120
|
+
// Returns first matching device
|
|
121
|
+
const device = await webusb.requestDevice({
|
|
122
|
+
filters: [{}]
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
if (device) {
|
|
126
|
+
console.log(device); // WebUSB device
|
|
127
|
+
}
|
|
128
|
+
})();
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Use WebUSB approach to find a device with custom selection method
|
|
132
|
+
```typescript
|
|
133
|
+
import { WebUSB } from 'usb';
|
|
134
|
+
|
|
135
|
+
(async () => {
|
|
136
|
+
const customWebUSB = new WebUSB({
|
|
137
|
+
// This function can return a promise which allows a UI to be displayed if required
|
|
138
|
+
devicesFound: devices => devices.find(device => device.serialNumber === 'TEST_DEVICE')
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// Returns device based on injected 'devicesFound' function
|
|
142
|
+
const device = await customWebUSB.requestDevice({
|
|
143
|
+
filters: [{}]
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
if (device) {
|
|
147
|
+
console.log(device); // WebUSB device
|
|
148
|
+
}
|
|
149
|
+
})();
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Use WebUSB approach to list authorised devices
|
|
153
|
+
```typescript
|
|
154
|
+
import { webusb } from 'usb';
|
|
155
|
+
|
|
156
|
+
(async () => {
|
|
157
|
+
// The default webusb instance follows the WebUSB spec and only returns authorised devices
|
|
158
|
+
const devices = await webusb.getDevices();
|
|
159
|
+
|
|
160
|
+
for (const device of devices) {
|
|
161
|
+
console.log(device); // WebUSB device
|
|
162
|
+
}
|
|
163
|
+
})();
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Use WebUSB approach to list all devices
|
|
167
|
+
```typescript
|
|
168
|
+
import { WebUSB } from 'usb';
|
|
169
|
+
|
|
170
|
+
(async () => {
|
|
171
|
+
const customWebUSB = new WebUSB({
|
|
172
|
+
// Bypass cheking for authorised devices
|
|
173
|
+
allowAllDevices: true
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// Uses blocking calls, so is async
|
|
177
|
+
const devices = await customWebUSB.getDevices();
|
|
178
|
+
|
|
179
|
+
for (const device of devices) {
|
|
180
|
+
console.log(device); // WebUSB device
|
|
181
|
+
}
|
|
182
|
+
})();
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Electron
|
|
186
|
+
Please refer to the maintained example for using `node-usb` in electron:
|
|
187
|
+
|
|
188
|
+
https://github.com/node-usb/node-usb-example-electron
|
|
189
|
+
|
|
190
|
+
# APIs
|
|
191
|
+
Since `v2.0.0`, the `node-usb` library supports two APIs:
|
|
192
|
+
|
|
193
|
+
- `WebUSB` which follows the [WebUSB Specification](https://wicg.github.io/webusb/) (recommended)
|
|
194
|
+
- `Legacy API` which retains the previous 'non-blocking' API
|
|
195
|
+
|
|
196
|
+
Convenience methods also exist to easily list or find devices as well as convert between a legacy usb.Device device and WebUSB device.
|
|
197
|
+
|
|
198
|
+
Full auto-generated API documentation can be seen here:
|
|
199
|
+
|
|
200
|
+
https://node-usb.github.io/node-usb/
|
|
201
|
+
|
|
202
|
+
## Convenience Functions
|
|
203
|
+
|
|
204
|
+
### getDeviceList()
|
|
205
|
+
Return a list of legacy `Device` objects for the USB devices attached to the system.
|
|
206
|
+
|
|
207
|
+
### findByIds(vid, pid)
|
|
208
|
+
Convenience method to get the first legacy device with the specified VID and PID, or `undefined` if no such device is present.
|
|
209
|
+
|
|
210
|
+
### findBySerialNumber(serialNumber)
|
|
211
|
+
Convenience method to get a promise of the legacy device with the specified serial number, or `undefined` if no such device is present.
|
|
212
|
+
|
|
213
|
+
### WebUSBDevice
|
|
214
|
+
WebUSB Device class for wrapping a legacy Device into a WebUSB device
|
|
215
|
+
|
|
216
|
+
#### WebUSBDevice.createInstance(device)
|
|
217
|
+
Convenience method to return a promise of a WebUSB device based on a legacy device
|
|
218
|
+
|
|
219
|
+
## WebUSB
|
|
220
|
+
|
|
221
|
+
Please refer to the WebUSB specification which be found here:
|
|
222
|
+
|
|
223
|
+
https://wicg.github.io/webusb/
|
|
224
|
+
|
|
225
|
+
### Implementation Status
|
|
226
|
+
|
|
227
|
+
#### USB
|
|
39
228
|
|
|
40
|
-
|
|
41
|
-
|
|
229
|
+
- [x] getDevices()
|
|
230
|
+
- [x] requestDevice()
|
|
42
231
|
|
|
43
|
-
|
|
232
|
+
#### USBDevice
|
|
233
|
+
|
|
234
|
+
- [x] usbVersionMajor
|
|
235
|
+
- [x] usbVersionMinor
|
|
236
|
+
- [x] usbVersionSubminor
|
|
237
|
+
- [x] deviceClass
|
|
238
|
+
- [x] deviceSubclass
|
|
239
|
+
- [x] deviceProtocol
|
|
240
|
+
- [x] vendorId
|
|
241
|
+
- [x] productId
|
|
242
|
+
- [x] deviceVersionMajor
|
|
243
|
+
- [x] deviceVersionMinor
|
|
244
|
+
- [x] deviceVersionSubminor
|
|
245
|
+
- [x] manufacturerName
|
|
246
|
+
- [x] productName
|
|
247
|
+
- [x] serialNumber
|
|
248
|
+
- [x] configuration
|
|
249
|
+
- [x] configurations
|
|
250
|
+
- [x] opened
|
|
251
|
+
- [x] open()
|
|
252
|
+
- [x] close()
|
|
253
|
+
- [x] selectConfiguration()
|
|
254
|
+
- [x] claimInterface()
|
|
255
|
+
- [x] releaseInterface()
|
|
256
|
+
- [x] selectAlternateInterface()
|
|
257
|
+
- [x] controlTransferIn()
|
|
258
|
+
- [x] controlTransferOut() - `bytesWritten` always equals the initial buffer length
|
|
259
|
+
- [x] transferIn()
|
|
260
|
+
- [x] transferOut() - `bytesWritten` always equals the initial buffer length
|
|
261
|
+
- [x] clearHalt()
|
|
262
|
+
- [x] reset()
|
|
263
|
+
- [ ] isochronousTransferIn()
|
|
264
|
+
- [ ] isochronousTransferOut()
|
|
265
|
+
|
|
266
|
+
#### Events
|
|
267
|
+
|
|
268
|
+
- [x] connect
|
|
269
|
+
- [x] disconnect
|
|
270
|
+
|
|
271
|
+
## Legacy API
|
|
272
|
+
|
|
273
|
+
### usb
|
|
274
|
+
Legacy usb object.
|
|
275
|
+
|
|
276
|
+
#### usb.LIBUSB_*
|
|
44
277
|
Constant properties from libusb
|
|
45
278
|
|
|
46
|
-
|
|
279
|
+
#### usb.setDebugLevel(level : int)
|
|
47
280
|
Set the libusb debug level (between 0 and 4)
|
|
48
281
|
|
|
49
|
-
Device
|
|
50
|
-
------
|
|
51
|
-
|
|
282
|
+
### Device
|
|
52
283
|
Represents a USB device.
|
|
53
284
|
|
|
54
|
-
|
|
285
|
+
#### .busNumber
|
|
55
286
|
Integer USB device number
|
|
56
287
|
|
|
57
|
-
|
|
288
|
+
#### .deviceAddress
|
|
58
289
|
Integer USB device address
|
|
59
290
|
|
|
60
|
-
|
|
291
|
+
#### .portNumbers
|
|
61
292
|
Array containing the USB device port numbers, or `undefined` if not supported on this platform.
|
|
62
293
|
|
|
63
|
-
|
|
294
|
+
#### .deviceDescriptor
|
|
64
295
|
Object with properties for the fields of the device descriptor:
|
|
65
296
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
297
|
+
- bLength
|
|
298
|
+
- bDescriptorType
|
|
299
|
+
- bcdUSB
|
|
300
|
+
- bDeviceClass
|
|
301
|
+
- bDeviceSubClass
|
|
302
|
+
- bDeviceProtocol
|
|
303
|
+
- bMaxPacketSize0
|
|
304
|
+
- idVendor
|
|
305
|
+
- idProduct
|
|
306
|
+
- bcdDevice
|
|
307
|
+
- iManufacturer
|
|
308
|
+
- iProduct
|
|
309
|
+
- iSerialNumber
|
|
310
|
+
- bNumConfigurations
|
|
311
|
+
|
|
312
|
+
#### .configDescriptor
|
|
82
313
|
Object with properties for the fields of the configuration descriptor:
|
|
83
314
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
### .allConfigDescriptors
|
|
95
|
-
Contains all config descriptors of the device (same structure as .configDescriptor above)
|
|
315
|
+
- bLength
|
|
316
|
+
- bDescriptorType
|
|
317
|
+
- wTotalLength
|
|
318
|
+
- bNumInterfaces
|
|
319
|
+
- bConfigurationValue
|
|
320
|
+
- iConfiguration
|
|
321
|
+
- bmAttributes
|
|
322
|
+
- bMaxPower
|
|
323
|
+
- extra (Buffer containing any extra data or additional descriptors)
|
|
96
324
|
|
|
97
|
-
|
|
98
|
-
|
|
325
|
+
#### .allConfigDescriptors
|
|
326
|
+
Contains all config descriptors of the device (same structure as .configDescriptor above)
|
|
99
327
|
|
|
100
|
-
|
|
328
|
+
#### .parent
|
|
329
|
+
Contains the parent of the device, such as a hub. If there is no parent this property is set to `null`.
|
|
101
330
|
|
|
331
|
+
#### .open()
|
|
102
332
|
Open the device. All methods below require the device to be open before use.
|
|
103
333
|
|
|
104
|
-
|
|
105
|
-
|
|
334
|
+
#### .close()
|
|
106
335
|
Close the device.
|
|
107
336
|
|
|
108
|
-
|
|
109
|
-
|
|
337
|
+
#### .controlTransfer(bmRequestType, bRequest, wValue, wIndex, data_or_length, callback(error, data))
|
|
110
338
|
Perform a control transfer with `libusb_control_transfer`.
|
|
111
339
|
|
|
112
340
|
Parameter `data_or_length` can be a integer length for an IN transfer, or a Buffer for an out transfer. The type must match the direction specified in the MSB of bmRequestType.
|
|
@@ -115,145 +343,137 @@ The `data` parameter of the callback is always undefined for OUT transfers, or w
|
|
|
115
343
|
|
|
116
344
|
A [package is available to calculate bmRequestType](https://www.npmjs.com/package/bmrequesttype) if needed.
|
|
117
345
|
|
|
118
|
-
|
|
346
|
+
#### .setConfiguration(id, callback(error))
|
|
119
347
|
Set the device configuration to something other than the default (0). To use this, first call `.open(false)` (which tells it not to auto configure), then before claiming an interface, call this method.
|
|
120
348
|
|
|
121
|
-
|
|
349
|
+
#### .getStringDescriptor(index, callback(error, data))
|
|
122
350
|
Perform a control transfer to retrieve a string descriptor
|
|
123
351
|
|
|
124
|
-
|
|
352
|
+
#### .getBosDescriptor(callback(error, bosDescriptor))
|
|
125
353
|
Perform a control transfer to retrieve an object with properties for the fields of the Binary Object Store descriptor:
|
|
126
354
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
355
|
+
- bLength
|
|
356
|
+
- bDescriptorType
|
|
357
|
+
- wTotalLength
|
|
358
|
+
- bNumDeviceCaps
|
|
131
359
|
|
|
132
|
-
|
|
360
|
+
#### .getCapabilities(callback(error, capabilities))
|
|
133
361
|
Retrieve a list of Capability objects for the Binary Object Store capabilities of the device.
|
|
134
362
|
|
|
135
|
-
|
|
363
|
+
#### .interface(interface)
|
|
136
364
|
Return the interface with the specified interface number.
|
|
137
365
|
|
|
138
|
-
|
|
366
|
+
#### .interfaces
|
|
139
367
|
List of Interface objects for the interfaces of the default configuration of the device.
|
|
140
368
|
|
|
141
|
-
|
|
369
|
+
#### .timeout
|
|
142
370
|
Timeout in milliseconds to use for control transfers.
|
|
143
371
|
|
|
144
|
-
|
|
372
|
+
#### .reset(callback(error))
|
|
145
373
|
Performs a reset of the device. Callback is called when complete.
|
|
146
374
|
|
|
375
|
+
### Interface
|
|
147
376
|
|
|
148
|
-
|
|
149
|
-
---------
|
|
150
|
-
|
|
151
|
-
### .endpoint(address)
|
|
377
|
+
#### .endpoint(address)
|
|
152
378
|
Return the InEndpoint or OutEndpoint with the specified address.
|
|
153
379
|
|
|
154
|
-
|
|
380
|
+
#### .endpoints
|
|
155
381
|
List of endpoints on this interface: InEndpoint and OutEndpoint objects.
|
|
156
382
|
|
|
157
|
-
|
|
383
|
+
#### .interface
|
|
158
384
|
Integer interface number.
|
|
159
385
|
|
|
160
|
-
|
|
386
|
+
#### .altSetting
|
|
161
387
|
Integer alternate setting number.
|
|
162
388
|
|
|
163
|
-
|
|
389
|
+
#### .setAltSetting(altSetting, callback(error))
|
|
164
390
|
Sets the alternate setting. It updates the `interface.endpoints` array to reflect the endpoints found in the alternate setting.
|
|
165
391
|
|
|
166
|
-
|
|
392
|
+
#### .claim()
|
|
167
393
|
Claims the interface. This method must be called before using any endpoints of this interface.
|
|
168
394
|
|
|
169
|
-
|
|
395
|
+
#### .release([closeEndpoints], callback(error))
|
|
170
396
|
Releases the interface and resets the alternate setting. Calls callback when complete.
|
|
171
397
|
|
|
172
398
|
It is an error to release an interface with pending transfers. If the optional closeEndpoints parameter is true, any active endpoint streams are stopped (see `Endpoint.stopStream`), and the interface is released after the stream transfers are cancelled. Transfers submitted individually with `Endpoint.transfer` are not affected by this parameter.
|
|
173
399
|
|
|
174
|
-
|
|
400
|
+
#### .isKernelDriverActive()
|
|
175
401
|
Returns `false` if a kernel driver is not active; `true` if active.
|
|
176
402
|
|
|
177
|
-
|
|
403
|
+
#### .detachKernelDriver()
|
|
178
404
|
Detaches the kernel driver from the interface.
|
|
179
405
|
|
|
180
|
-
|
|
406
|
+
#### .attachKernelDriver()
|
|
181
407
|
Re-attaches the kernel driver for the interface.
|
|
182
408
|
|
|
183
|
-
|
|
409
|
+
#### .descriptor
|
|
184
410
|
Object with fields from the interface descriptor -- see libusb documentation or USB spec.
|
|
185
411
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
412
|
+
- bLength
|
|
413
|
+
- bDescriptorType
|
|
414
|
+
- bInterfaceNumber
|
|
415
|
+
- bAlternateSetting
|
|
416
|
+
- bNumEndpoints
|
|
417
|
+
- bInterfaceClass
|
|
418
|
+
- bInterfaceSubClass
|
|
419
|
+
- bInterfaceProtocol
|
|
420
|
+
- iInterface
|
|
421
|
+
- extra (Buffer containing any extra data or additional descriptors)
|
|
197
422
|
|
|
198
|
-
Capability
|
|
199
|
-
---------
|
|
423
|
+
### Capability
|
|
200
424
|
|
|
201
|
-
|
|
425
|
+
#### .type
|
|
202
426
|
Integer capability type.
|
|
203
427
|
|
|
204
|
-
|
|
428
|
+
#### .data
|
|
205
429
|
Buffer capability data.
|
|
206
430
|
|
|
207
|
-
|
|
431
|
+
#### .descriptor
|
|
208
432
|
Object with fields from the capability descriptor -- see libusb documentation or USB spec.
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
Endpoint
|
|
215
|
-
--------
|
|
433
|
+
|
|
434
|
+
- bLength
|
|
435
|
+
- bDescriptorType
|
|
436
|
+
- bDevCapabilityType
|
|
216
437
|
|
|
438
|
+
### Endpoint
|
|
217
439
|
Common base for InEndpoint and OutEndpoint, see below.
|
|
218
440
|
|
|
219
|
-
|
|
441
|
+
#### .direction
|
|
220
442
|
Endpoint direction: `"in"` or `"out"`.
|
|
221
443
|
|
|
222
|
-
|
|
444
|
+
#### .transferType
|
|
223
445
|
Endpoint type: `usb.LIBUSB_TRANSFER_TYPE_BULK`, `usb.LIBUSB_TRANSFER_TYPE_INTERRUPT`, or `usb.LIBUSB_TRANSFER_TYPE_ISOCHRONOUS`.
|
|
224
446
|
|
|
225
|
-
|
|
447
|
+
#### .descriptor
|
|
226
448
|
Object with fields from the endpoint descriptor -- see libusb documentation or USB spec.
|
|
227
449
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
450
|
+
- bLength
|
|
451
|
+
- bDescriptorType
|
|
452
|
+
- bEndpointAddress
|
|
453
|
+
- bmAttributes
|
|
454
|
+
- wMaxPacketSize
|
|
455
|
+
- bInterval
|
|
456
|
+
- bRefresh
|
|
457
|
+
- bSynchAddress
|
|
458
|
+
- extra (Buffer containing any extra data or additional descriptors)
|
|
459
|
+
|
|
460
|
+
#### .timeout
|
|
239
461
|
Sets the timeout in milliseconds for transfers on this endpoint. The default, `0`, is infinite timeout.
|
|
240
462
|
|
|
241
|
-
|
|
463
|
+
#### .clearHalt(callback(error))
|
|
242
464
|
Clear the halt/stall condition for this endpoint.
|
|
243
465
|
|
|
244
|
-
InEndpoint
|
|
245
|
-
----------
|
|
246
|
-
|
|
466
|
+
### InEndpoint
|
|
247
467
|
Endpoints in the IN direction (device->PC) have this type.
|
|
248
468
|
|
|
249
|
-
|
|
469
|
+
#### .transfer(length, callback(error, data))
|
|
250
470
|
Perform a transfer to read data from the endpoint.
|
|
251
471
|
|
|
252
472
|
If length is greater than maxPacketSize, libusb will automatically split the transfer in multiple packets, and you will receive one callback with all data once all packets are complete.
|
|
253
473
|
|
|
254
474
|
`this` in the callback is the InEndpoint object.
|
|
255
475
|
|
|
256
|
-
|
|
476
|
+
#### .startPoll(nTransfers=3, transferSize=maxPacketSize)
|
|
257
477
|
Start polling the endpoint.
|
|
258
478
|
|
|
259
479
|
The library will keep `nTransfers` transfers of size `transferSize` pending in
|
|
@@ -261,85 +481,90 @@ the kernel at all times to ensure continuous data flow. This is handled by the
|
|
|
261
481
|
libusb event thread, so it continues even if the Node v8 thread is busy. The
|
|
262
482
|
`data` and `error` events are emitted as transfers complete.
|
|
263
483
|
|
|
264
|
-
|
|
484
|
+
#### .stopPoll(cb)
|
|
265
485
|
Stop polling.
|
|
266
486
|
|
|
267
487
|
Further data may still be received. The `end` event is emitted and the callback
|
|
268
488
|
is called once all transfers have completed or canceled.
|
|
269
489
|
|
|
270
|
-
|
|
490
|
+
#### Event: data(data : Buffer)
|
|
271
491
|
Emitted with data received by the polling transfers
|
|
272
492
|
|
|
273
|
-
|
|
493
|
+
#### Event: error(error)
|
|
274
494
|
Emitted when polling encounters an error. All in flight transfers will be automatically canceled and no further polling will be done. You have to wait for the `end` event before you can start polling again.
|
|
275
495
|
|
|
276
|
-
|
|
496
|
+
#### Event: end
|
|
277
497
|
Emitted when polling has been canceled
|
|
278
498
|
|
|
279
|
-
OutEndpoint
|
|
280
|
-
-----------
|
|
281
|
-
|
|
499
|
+
### OutEndpoint
|
|
282
500
|
Endpoints in the OUT direction (PC->device) have this type.
|
|
283
501
|
|
|
284
|
-
|
|
502
|
+
#### .transfer(data, callback(error))
|
|
285
503
|
Perform a transfer to write `data` to the endpoint.
|
|
286
504
|
|
|
287
505
|
If length is greater than maxPacketSize, libusb will automatically split the transfer in multiple packets, and you will receive one callback once all packets are complete.
|
|
288
506
|
|
|
289
507
|
`this` in the callback is the OutEndpoint object.
|
|
290
508
|
|
|
291
|
-
|
|
509
|
+
#### Event: error(error)
|
|
292
510
|
Emitted when the stream encounters an error.
|
|
293
511
|
|
|
294
|
-
|
|
512
|
+
#### Event: end
|
|
295
513
|
Emitted when the stream has been stopped and all pending requests have been completed.
|
|
296
514
|
|
|
515
|
+
### UsbDetection
|
|
297
516
|
|
|
298
|
-
|
|
299
|
-
------------
|
|
300
|
-
|
|
301
|
-
### usb.on('attach', function(device) { ... });
|
|
517
|
+
#### usb.on('attach', function(device) { ... });
|
|
302
518
|
Attaches a callback to plugging in a `device`.
|
|
303
519
|
|
|
304
|
-
|
|
520
|
+
#### usb.on('detach', function(device) { ... });
|
|
305
521
|
Attaches a callback to unplugging a `device`.
|
|
306
522
|
|
|
307
|
-
|
|
523
|
+
#### usb.refHotplugEvents();
|
|
308
524
|
Restore (re-reference) the hotplug events unreferenced by `unrefHotplugEvents()`
|
|
309
525
|
|
|
310
|
-
|
|
526
|
+
#### usb.unrefHotplugEvents();
|
|
311
527
|
Listening to events will prevent the process to exit. By calling this function, hotplug events will be unreferenced by the event loop, allowing the process to exit even when listening for the `attach` and `detach` events.
|
|
312
528
|
|
|
529
|
+
# Development
|
|
530
|
+
The library is based on native bindings wrapping the [libusb](https://github.com/libusb/libusb) library.
|
|
313
531
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
To build from git:
|
|
532
|
+
## Setup
|
|
533
|
+
Libusb is included as a submodule, clone this repository and then the submodule as follows:
|
|
318
534
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
535
|
+
```bash
|
|
536
|
+
git clone https://github.com/node-usb/node-usb
|
|
537
|
+
cd node-usb
|
|
538
|
+
git submodule update --init
|
|
539
|
+
```
|
|
322
540
|
|
|
323
|
-
|
|
541
|
+
## Building
|
|
542
|
+
The package uses `yarn` for the typescript code and `prebuildify` to generate the native binaries. These can be executed as follows:
|
|
324
543
|
|
|
325
|
-
|
|
544
|
+
```bash
|
|
545
|
+
yarn
|
|
546
|
+
yarn prebuild
|
|
547
|
+
```
|
|
326
548
|
|
|
327
|
-
|
|
549
|
+
__Note:__ On Linux, you'll need libudev to build libusb. On Ubuntu/Debian:
|
|
328
550
|
|
|
329
|
-
|
|
330
|
-
|
|
551
|
+
```bash
|
|
552
|
+
sudo apt-get install build-essential libudev-dev
|
|
553
|
+
```
|
|
331
554
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
Does not support:
|
|
555
|
+
## Testing
|
|
556
|
+
To execute the unit tests, Run:
|
|
336
557
|
|
|
337
|
-
|
|
338
|
-
|
|
558
|
+
```bash
|
|
559
|
+
yarn test
|
|
560
|
+
```
|
|
339
561
|
|
|
340
|
-
|
|
341
|
-
=======
|
|
562
|
+
Some tests require an [attached STM32F103 Microprocessor USB device with specific firmware](https://github.com/node-usb/node-usb-test-firmware).
|
|
342
563
|
|
|
343
|
-
|
|
564
|
+
```bash
|
|
565
|
+
yarn full-test
|
|
566
|
+
yarn valgrind
|
|
567
|
+
```
|
|
344
568
|
|
|
345
|
-
|
|
569
|
+
## Releasing
|
|
570
|
+
Please refer to the [Wiki](https://github.com/node-usb/node-usb/wiki/Release-Process) for release instructions.
|