usb 2.5.1 → 2.5.2
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/CHANGELOG.md +10 -0
- package/dist/index.d.ts +10 -6
- package/dist/index.js +27 -11
- package/dist/index.js.map +1 -1
- package/dist/webusb/webusb-device.d.ts +1 -1
- package/dist/webusb/webusb-device.js +4 -9
- package/dist/webusb/webusb-device.js.map +1 -1
- package/package.json +1 -1
- package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
- package/prebuilds/win32-ia32/node.napi.node +0 -0
- package/prebuilds/win32-x64/node.napi.node +0 -0
- package/src/hotplug/windows.cc +11 -6
- package/.eslintignore +0 -4
- package/.eslintrc.json +0 -87
- package/.gitattributes +0 -1
- package/.gitmodules +0 -3
- package/.vscode/launch.json +0 -15
- package/.vscode/tasks.json +0 -23
- package/libusb/.gitattributes +0 -7
- package/libusb/msvc/.gitattributes +0 -3
- package/tsc/index.ts +0 -75
- package/tsc/usb/bindings.ts +0 -310
- package/tsc/usb/capability.ts +0 -22
- package/tsc/usb/descriptors.ts +0 -180
- package/tsc/usb/device.ts +0 -331
- package/tsc/usb/endpoint.ts +0 -229
- package/tsc/usb/index.ts +0 -149
- package/tsc/usb/interface.ts +0 -167
- package/tsc/webusb/index.ts +0 -374
- package/tsc/webusb/webusb-device.ts +0 -490
- package/tsconfig.json +0 -17
- package/typedoc.json +0 -9
package/tsc/usb/device.ts
DELETED
|
@@ -1,331 +0,0 @@
|
|
|
1
|
-
import * as usb from './bindings';
|
|
2
|
-
import { Interface } from './interface';
|
|
3
|
-
import { Capability } from './capability';
|
|
4
|
-
import { BosDescriptor, ConfigDescriptor } from './descriptors';
|
|
5
|
-
|
|
6
|
-
const isBuffer = (obj: number | Uint8Array | undefined): obj is Uint8Array => !!obj && obj instanceof Uint8Array;
|
|
7
|
-
const DEFAULT_TIMEOUT = 1000;
|
|
8
|
-
|
|
9
|
-
export class ExtendedDevice {
|
|
10
|
-
/**
|
|
11
|
-
* List of Interface objects for the interfaces of the default configuration of the device.
|
|
12
|
-
*/
|
|
13
|
-
public interfaces: Interface[] | undefined;
|
|
14
|
-
|
|
15
|
-
private _timeout = DEFAULT_TIMEOUT;
|
|
16
|
-
/**
|
|
17
|
-
* Timeout in milliseconds to use for control transfers.
|
|
18
|
-
*/
|
|
19
|
-
public get timeout(): number {
|
|
20
|
-
return this._timeout || DEFAULT_TIMEOUT;
|
|
21
|
-
}
|
|
22
|
-
public set timeout(value: number) {
|
|
23
|
-
this._timeout = value;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Object with properties for the fields of the active configuration descriptor.
|
|
28
|
-
*/
|
|
29
|
-
public get configDescriptor(): ConfigDescriptor | undefined {
|
|
30
|
-
try {
|
|
31
|
-
return (this as unknown as usb.Device).__getConfigDescriptor();
|
|
32
|
-
} catch (e) {
|
|
33
|
-
// Check descriptor exists
|
|
34
|
-
if (e.errno === usb.LIBUSB_ERROR_NOT_FOUND) {
|
|
35
|
-
return undefined;
|
|
36
|
-
}
|
|
37
|
-
throw e;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Contains all config descriptors of the device (same structure as .configDescriptor above)
|
|
43
|
-
*/
|
|
44
|
-
public get allConfigDescriptors(): ConfigDescriptor[] {
|
|
45
|
-
try {
|
|
46
|
-
return (this as unknown as usb.Device).__getAllConfigDescriptors();
|
|
47
|
-
} catch (e) {
|
|
48
|
-
// Check descriptors exist
|
|
49
|
-
if (e.errno === usb.LIBUSB_ERROR_NOT_FOUND) {
|
|
50
|
-
return [];
|
|
51
|
-
}
|
|
52
|
-
throw e;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Contains the parent of the device, such as a hub. If there is no parent this property is set to `null`.
|
|
58
|
-
*/
|
|
59
|
-
public get parent(): usb.Device {
|
|
60
|
-
return (this as unknown as usb.Device).__getParent();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Open the device.
|
|
65
|
-
* @param defaultConfig
|
|
66
|
-
*/
|
|
67
|
-
public open(this: usb.Device, defaultConfig = true): void {
|
|
68
|
-
this.__open();
|
|
69
|
-
if (defaultConfig === false) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
this.interfaces = [];
|
|
73
|
-
const len = this.configDescriptor ? this.configDescriptor.interfaces.length : 0;
|
|
74
|
-
for (let i = 0; i < len; i++) {
|
|
75
|
-
this.interfaces[i] = new Interface(this, i);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Close the device.
|
|
81
|
-
*
|
|
82
|
-
* The device must be open to use this method.
|
|
83
|
-
*/
|
|
84
|
-
public close(this: usb.Device): void {
|
|
85
|
-
this.__close();
|
|
86
|
-
this.interfaces = undefined;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* 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),
|
|
91
|
-
* then before claiming an interface, call this method.
|
|
92
|
-
*
|
|
93
|
-
* The device must be open to use this method.
|
|
94
|
-
* @param desired
|
|
95
|
-
* @param callback
|
|
96
|
-
*/
|
|
97
|
-
public setConfiguration(this: usb.Device, desired: number, callback?: (error: usb.LibUSBException | undefined) => void): void {
|
|
98
|
-
this.__setConfiguration(desired, error => {
|
|
99
|
-
if (!error) {
|
|
100
|
-
this.interfaces = [];
|
|
101
|
-
const len = this.configDescriptor ? this.configDescriptor.interfaces.length : 0;
|
|
102
|
-
for (let i = 0; i < len; i++) {
|
|
103
|
-
this.interfaces[i] = new Interface(this, i);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
if (callback) {
|
|
107
|
-
callback.call(this, error);
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Perform a control transfer with `libusb_control_transfer`.
|
|
114
|
-
*
|
|
115
|
-
* Parameter `data_or_length` can be an 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.
|
|
116
|
-
*
|
|
117
|
-
* The `data` parameter of the callback is actual transferred for OUT transfers, or will be passed a Buffer for IN transfers.
|
|
118
|
-
*
|
|
119
|
-
* The device must be open to use this method.
|
|
120
|
-
* @param bmRequestType
|
|
121
|
-
* @param bRequest
|
|
122
|
-
* @param wValue
|
|
123
|
-
* @param wIndex
|
|
124
|
-
* @param data_or_length
|
|
125
|
-
* @param callback
|
|
126
|
-
*/
|
|
127
|
-
public controlTransfer(this: usb.Device, bmRequestType: number, bRequest: number, wValue: number, wIndex: number, data_or_length: number | Buffer,
|
|
128
|
-
callback?: (error: usb.LibUSBException | undefined, buffer: Buffer | number | undefined) => void): usb.Device {
|
|
129
|
-
const isIn = !!(bmRequestType & usb.LIBUSB_ENDPOINT_IN);
|
|
130
|
-
const wLength = isIn ? data_or_length as number : (data_or_length as Buffer).length;
|
|
131
|
-
|
|
132
|
-
if (isIn) {
|
|
133
|
-
if (!(data_or_length >= 0)) {
|
|
134
|
-
throw new TypeError('Expected size number for IN transfer (based on bmRequestType)');
|
|
135
|
-
}
|
|
136
|
-
} else {
|
|
137
|
-
if (!isBuffer(data_or_length)) {
|
|
138
|
-
throw new TypeError('Expected buffer for OUT transfer (based on bmRequestType)');
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// Buffer for the setup packet
|
|
143
|
-
// http://libusbx.sourceforge.net/api-1.0/structlibusb__control__setup.html
|
|
144
|
-
const buf = Buffer.alloc(wLength + usb.LIBUSB_CONTROL_SETUP_SIZE);
|
|
145
|
-
buf.writeUInt8(bmRequestType, 0);
|
|
146
|
-
buf.writeUInt8(bRequest, 1);
|
|
147
|
-
buf.writeUInt16LE(wValue, 2);
|
|
148
|
-
buf.writeUInt16LE(wIndex, 4);
|
|
149
|
-
buf.writeUInt16LE(wLength, 6);
|
|
150
|
-
|
|
151
|
-
if (!isIn) {
|
|
152
|
-
buf.set(data_or_length as Buffer, usb.LIBUSB_CONTROL_SETUP_SIZE);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const transfer = new usb.Transfer(this, 0, usb.LIBUSB_TRANSFER_TYPE_CONTROL, this.timeout,
|
|
156
|
-
(error, buf, actual) => {
|
|
157
|
-
if (callback) {
|
|
158
|
-
if (isIn) {
|
|
159
|
-
callback.call(this, error, buf.slice(usb.LIBUSB_CONTROL_SETUP_SIZE, usb.LIBUSB_CONTROL_SETUP_SIZE + actual));
|
|
160
|
-
} else {
|
|
161
|
-
callback.call(this, error, actual);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
try {
|
|
168
|
-
transfer.submit(buf);
|
|
169
|
-
} catch (e) {
|
|
170
|
-
if (callback) {
|
|
171
|
-
process.nextTick(() => callback.call(this, e, undefined));
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
return this;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Return the interface with the specified interface number.
|
|
179
|
-
*
|
|
180
|
-
* The device must be open to use this method.
|
|
181
|
-
* @param addr
|
|
182
|
-
*/
|
|
183
|
-
public interface(this: usb.Device, addr: number): Interface {
|
|
184
|
-
if (!this.interfaces) {
|
|
185
|
-
throw new Error('Device must be open before searching for interfaces');
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
addr = addr || 0;
|
|
189
|
-
for (let i = 0; i < this.interfaces.length; i++) {
|
|
190
|
-
if (this.interfaces[i].interfaceNumber === addr) {
|
|
191
|
-
return this.interfaces[i];
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
throw new Error(`Interface not found for address: ${addr}`);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Perform a control transfer to retrieve a string descriptor
|
|
200
|
-
*
|
|
201
|
-
* The device must be open to use this method.
|
|
202
|
-
* @param desc_index
|
|
203
|
-
* @param callback
|
|
204
|
-
*/
|
|
205
|
-
public getStringDescriptor(this: usb.Device, desc_index: number, callback: (error?: usb.LibUSBException, value?: string) => void): void {
|
|
206
|
-
// Index 0 indicates null
|
|
207
|
-
if (desc_index === 0) {
|
|
208
|
-
callback();
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
const langid = 0x0409;
|
|
213
|
-
const length = 255;
|
|
214
|
-
this.controlTransfer(
|
|
215
|
-
usb.LIBUSB_ENDPOINT_IN,
|
|
216
|
-
usb.LIBUSB_REQUEST_GET_DESCRIPTOR,
|
|
217
|
-
((usb.LIBUSB_DT_STRING << 8) | desc_index),
|
|
218
|
-
langid,
|
|
219
|
-
length,
|
|
220
|
-
(error, buffer) => {
|
|
221
|
-
if (error) {
|
|
222
|
-
return callback(error);
|
|
223
|
-
}
|
|
224
|
-
callback(undefined, isBuffer(buffer) ? buffer.toString('utf16le', 2) : undefined);
|
|
225
|
-
}
|
|
226
|
-
);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Perform a control transfer to retrieve an object with properties for the fields of the Binary Object Store descriptor.
|
|
231
|
-
*
|
|
232
|
-
* The device must be open to use this method.
|
|
233
|
-
* @param callback
|
|
234
|
-
*/
|
|
235
|
-
public getBosDescriptor(this: usb.Device, callback: (error?: usb.LibUSBException, descriptor?: BosDescriptor) => void): void {
|
|
236
|
-
if (this._bosDescriptor) {
|
|
237
|
-
// Cached descriptor
|
|
238
|
-
return callback(undefined, this._bosDescriptor);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
if (this.deviceDescriptor.bcdUSB < 0x201) {
|
|
242
|
-
// BOS is only supported from USB 2.0.1
|
|
243
|
-
return callback(undefined, undefined);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
this.controlTransfer(
|
|
247
|
-
usb.LIBUSB_ENDPOINT_IN,
|
|
248
|
-
usb.LIBUSB_REQUEST_GET_DESCRIPTOR,
|
|
249
|
-
(usb.LIBUSB_DT_BOS << 8),
|
|
250
|
-
0,
|
|
251
|
-
usb.LIBUSB_DT_BOS_SIZE,
|
|
252
|
-
(error, buffer) => {
|
|
253
|
-
if (error) {
|
|
254
|
-
// Check BOS descriptor exists
|
|
255
|
-
if (error.errno === usb.LIBUSB_TRANSFER_STALL) return callback(undefined, undefined);
|
|
256
|
-
return callback(error, undefined);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
if (!isBuffer(buffer)) {
|
|
260
|
-
return callback(undefined, undefined);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
const totalLength = buffer.readUInt16LE(2);
|
|
264
|
-
this.controlTransfer(
|
|
265
|
-
usb.LIBUSB_ENDPOINT_IN,
|
|
266
|
-
usb.LIBUSB_REQUEST_GET_DESCRIPTOR,
|
|
267
|
-
(usb.LIBUSB_DT_BOS << 8),
|
|
268
|
-
0,
|
|
269
|
-
totalLength,
|
|
270
|
-
(error, buffer) => {
|
|
271
|
-
if (error) {
|
|
272
|
-
// Check BOS descriptor exists
|
|
273
|
-
if (error.errno === usb.LIBUSB_TRANSFER_STALL) return callback(undefined, undefined);
|
|
274
|
-
return callback(error, undefined);
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
if (!isBuffer(buffer)) {
|
|
278
|
-
return callback(undefined, undefined);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const descriptor: BosDescriptor = {
|
|
282
|
-
bLength: buffer.readUInt8(0),
|
|
283
|
-
bDescriptorType: buffer.readUInt8(1),
|
|
284
|
-
wTotalLength: buffer.readUInt16LE(2),
|
|
285
|
-
bNumDeviceCaps: buffer.readUInt8(4),
|
|
286
|
-
capabilities: []
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
let i = usb.LIBUSB_DT_BOS_SIZE;
|
|
290
|
-
while (i < descriptor.wTotalLength) {
|
|
291
|
-
const capability = {
|
|
292
|
-
bLength: buffer.readUInt8(i + 0),
|
|
293
|
-
bDescriptorType: buffer.readUInt8(i + 1),
|
|
294
|
-
bDevCapabilityType: buffer.readUInt8(i + 2),
|
|
295
|
-
dev_capability_data: buffer.slice(i + 3, i + buffer.readUInt8(i + 0))
|
|
296
|
-
};
|
|
297
|
-
|
|
298
|
-
descriptor.capabilities.push(capability);
|
|
299
|
-
i += capability.bLength;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
// Cache descriptor
|
|
303
|
-
this._bosDescriptor = descriptor;
|
|
304
|
-
callback(undefined, this._bosDescriptor);
|
|
305
|
-
}
|
|
306
|
-
);
|
|
307
|
-
}
|
|
308
|
-
);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* Retrieve a list of Capability objects for the Binary Object Store capabilities of the device.
|
|
313
|
-
*
|
|
314
|
-
* The device must be open to use this method.
|
|
315
|
-
* @param callback
|
|
316
|
-
*/
|
|
317
|
-
public getCapabilities(this: usb.Device, callback: (error: usb.LibUSBException | undefined, capabilities?: Capability[]) => void): void {
|
|
318
|
-
const capabilities: Capability[] = [];
|
|
319
|
-
|
|
320
|
-
this.getBosDescriptor((error, descriptor) => {
|
|
321
|
-
if (error) return callback(error, undefined);
|
|
322
|
-
|
|
323
|
-
const len = descriptor ? descriptor.capabilities.length : 0;
|
|
324
|
-
for (let i = 0; i < len; i++) {
|
|
325
|
-
capabilities.push(new Capability(this, i));
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
callback(undefined, capabilities);
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
}
|
package/tsc/usb/endpoint.ts
DELETED
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'events';
|
|
2
|
-
import { LibUSBException, LIBUSB_TRANSFER_CANCELLED, Transfer, Device } from './bindings';
|
|
3
|
-
import { EndpointDescriptor } from './descriptors';
|
|
4
|
-
|
|
5
|
-
const isBuffer = (obj: ArrayBuffer | Buffer): obj is Uint8Array => obj && obj instanceof Uint8Array;
|
|
6
|
-
|
|
7
|
-
/** Common base for InEndpoint and OutEndpoint. */
|
|
8
|
-
export abstract class Endpoint extends EventEmitter {
|
|
9
|
-
public address: number;
|
|
10
|
-
|
|
11
|
-
/** Endpoint direction: `"in"` or `"out"`. */
|
|
12
|
-
public abstract direction: 'in' | 'out';
|
|
13
|
-
|
|
14
|
-
/** Endpoint type: `usb.LIBUSB_TRANSFER_TYPE_BULK`, `usb.LIBUSB_TRANSFER_TYPE_INTERRUPT`, or `usb.LIBUSB_TRANSFER_TYPE_ISOCHRONOUS`. */
|
|
15
|
-
public transferType: number;
|
|
16
|
-
|
|
17
|
-
/** Sets the timeout in milliseconds for transfers on this endpoint. The default, `0`, is infinite timeout. */
|
|
18
|
-
public timeout = 0;
|
|
19
|
-
|
|
20
|
-
/** Object with fields from the endpoint descriptor -- see libusb documentation or USB spec. */
|
|
21
|
-
public descriptor: EndpointDescriptor;
|
|
22
|
-
|
|
23
|
-
constructor(protected device: Device, descriptor: EndpointDescriptor) {
|
|
24
|
-
super();
|
|
25
|
-
this.descriptor = descriptor;
|
|
26
|
-
this.address = descriptor.bEndpointAddress;
|
|
27
|
-
this.transferType = descriptor.bmAttributes & 0x03;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** Clear the halt/stall condition for this endpoint. */
|
|
31
|
-
public clearHalt(callback: (error: LibUSBException | undefined) => void): void {
|
|
32
|
-
return this.device.__clearHalt(this.address, callback);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Create a new `Transfer` object for this endpoint.
|
|
37
|
-
*
|
|
38
|
-
* The passed callback will be called when the transfer is submitted and finishes. Its arguments are the error (if any), the submitted buffer, and the amount of data actually written (for
|
|
39
|
-
* OUT transfers) or read (for IN transfers).
|
|
40
|
-
*
|
|
41
|
-
* @param timeout Timeout for the transfer (0 means unlimited).
|
|
42
|
-
* @param callback Transfer completion callback.
|
|
43
|
-
*/
|
|
44
|
-
public makeTransfer(timeout: number, callback: (error: LibUSBException | undefined, buffer: Buffer, actualLength: number) => void): Transfer {
|
|
45
|
-
return new Transfer(this.device, this.address, this.transferType, timeout, callback);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Endpoints in the IN direction (device->PC) have this type. */
|
|
50
|
-
export class InEndpoint extends Endpoint {
|
|
51
|
-
|
|
52
|
-
/** Endpoint direction. */
|
|
53
|
-
public direction: 'in' | 'out' = 'in';
|
|
54
|
-
|
|
55
|
-
protected pollTransfers: Transfer[] = [];
|
|
56
|
-
protected pollTransferSize = 0;
|
|
57
|
-
protected pollPending = 0;
|
|
58
|
-
public pollActive = false;
|
|
59
|
-
|
|
60
|
-
constructor(device: Device, descriptor: EndpointDescriptor) {
|
|
61
|
-
super(device, descriptor);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Perform a transfer to read data from the endpoint.
|
|
66
|
-
*
|
|
67
|
-
* 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.
|
|
68
|
-
*
|
|
69
|
-
* `this` in the callback is the InEndpoint object.
|
|
70
|
-
*
|
|
71
|
-
* The device must be open to use this method.
|
|
72
|
-
* @param length
|
|
73
|
-
* @param callback
|
|
74
|
-
*/
|
|
75
|
-
public transfer(length: number, callback: (error: LibUSBException | undefined, data?: Buffer) => void): InEndpoint {
|
|
76
|
-
const buffer = Buffer.alloc(length);
|
|
77
|
-
|
|
78
|
-
const cb = (error: LibUSBException | undefined, _buffer?: Buffer, actualLength?: number) => {
|
|
79
|
-
callback.call(this, error, buffer.slice(0, actualLength));
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
try {
|
|
83
|
-
this.makeTransfer(this.timeout, cb).submit(buffer);
|
|
84
|
-
} catch (e) {
|
|
85
|
-
process.nextTick(() => callback.call(this, e));
|
|
86
|
-
}
|
|
87
|
-
return this;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Start polling the endpoint.
|
|
92
|
-
*
|
|
93
|
-
* The library will keep `nTransfers` transfers of size `transferSize` pending in the kernel at all times to ensure continuous data flow.
|
|
94
|
-
* This is handled by the libusb event thread, so it continues even if the Node v8 thread is busy. The `data` and `error` events are emitted as transfers complete.
|
|
95
|
-
*
|
|
96
|
-
* The device must be open to use this method.
|
|
97
|
-
* @param nTransfers
|
|
98
|
-
* @param transferSize
|
|
99
|
-
*/
|
|
100
|
-
public startPoll(nTransfers?: number, transferSize?: number, _callback?: (error: LibUSBException | undefined, buffer: Buffer, actualLength: number) => void): Transfer[] {
|
|
101
|
-
const transferDone = (error: LibUSBException | undefined, transfer: Transfer, buffer: Buffer, actualLength: number) => {
|
|
102
|
-
if (!error) {
|
|
103
|
-
this.emit('data', buffer.slice(0, actualLength));
|
|
104
|
-
} else if (error.errno != LIBUSB_TRANSFER_CANCELLED) {
|
|
105
|
-
this.emit('error', error);
|
|
106
|
-
this.stopPoll();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (this.pollActive) {
|
|
110
|
-
startTransfer(transfer);
|
|
111
|
-
} else {
|
|
112
|
-
this.pollPending--;
|
|
113
|
-
|
|
114
|
-
if (this.pollPending === 0) {
|
|
115
|
-
this.pollTransfers = [];
|
|
116
|
-
this.pollActive = false;
|
|
117
|
-
this.emit('end');
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
const startTransfer = (transfer: Transfer) => {
|
|
123
|
-
try {
|
|
124
|
-
transfer.submit(Buffer.alloc(this.pollTransferSize), (error, buffer, actualLength) => {
|
|
125
|
-
transferDone(error, transfer, buffer, actualLength);
|
|
126
|
-
});
|
|
127
|
-
} catch (e) {
|
|
128
|
-
this.emit('error', e);
|
|
129
|
-
this.stopPoll();
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
this.pollTransfers = this.startPollTransfers(nTransfers, transferSize, function (this: Transfer, error, buffer, actualLength) {
|
|
134
|
-
transferDone(error, this, buffer, actualLength);
|
|
135
|
-
});
|
|
136
|
-
this.pollTransfers.forEach(startTransfer);
|
|
137
|
-
this.pollPending = this.pollTransfers.length;
|
|
138
|
-
return this.pollTransfers;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
protected startPollTransfers(nTransfers = 3, transferSize = this.descriptor.wMaxPacketSize, callback: (error: LibUSBException | undefined, buffer: Buffer, actualLength: number) => void): Transfer[] {
|
|
142
|
-
if (this.pollActive) {
|
|
143
|
-
throw new Error('Polling already active');
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
this.pollTransferSize = transferSize;
|
|
147
|
-
this.pollActive = true;
|
|
148
|
-
this.pollPending = 0;
|
|
149
|
-
|
|
150
|
-
const transfers: Transfer[] = [];
|
|
151
|
-
for (let i = 0; i < nTransfers; i++) {
|
|
152
|
-
const transfer = this.makeTransfer(0, callback);
|
|
153
|
-
transfers[i] = transfer;
|
|
154
|
-
}
|
|
155
|
-
return transfers;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Stop polling.
|
|
160
|
-
*
|
|
161
|
-
* Further data may still be received. The `end` event is emitted and the callback is called once all transfers have completed or canceled.
|
|
162
|
-
*
|
|
163
|
-
* The device must be open to use this method.
|
|
164
|
-
* @param callback
|
|
165
|
-
*/
|
|
166
|
-
public stopPoll(callback?: () => void): void {
|
|
167
|
-
if (!this.pollActive) {
|
|
168
|
-
throw new Error('Polling is not active.');
|
|
169
|
-
}
|
|
170
|
-
for (let i = 0; i < this.pollTransfers.length; i++) {
|
|
171
|
-
try {
|
|
172
|
-
this.pollTransfers[i].cancel();
|
|
173
|
-
} catch (error) {
|
|
174
|
-
this.emit('error', error);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
this.pollActive = false;
|
|
178
|
-
if (callback) this.once('end', callback);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/** Endpoints in the OUT direction (PC->device) have this type. */
|
|
183
|
-
export class OutEndpoint extends Endpoint {
|
|
184
|
-
|
|
185
|
-
/** Endpoint direction. */
|
|
186
|
-
public direction: 'in' | 'out' = 'out';
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Perform a transfer to write `data` to the endpoint.
|
|
190
|
-
*
|
|
191
|
-
* 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.
|
|
192
|
-
*
|
|
193
|
-
* `this` in the callback is the OutEndpoint object.
|
|
194
|
-
*
|
|
195
|
-
* The device must be open to use this method.
|
|
196
|
-
* @param buffer
|
|
197
|
-
* @param callback
|
|
198
|
-
*/
|
|
199
|
-
public transfer(buffer: Buffer, callback?: (error: LibUSBException | undefined, actual: number) => void): OutEndpoint {
|
|
200
|
-
if (!buffer) {
|
|
201
|
-
buffer = Buffer.alloc(0);
|
|
202
|
-
} else if (!isBuffer(buffer)) {
|
|
203
|
-
buffer = Buffer.from(buffer);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
const cb = (error: LibUSBException | undefined, _buffer?: Buffer, actual?: number) => {
|
|
207
|
-
if (callback) {
|
|
208
|
-
callback.call(this, error, actual || 0);
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
try {
|
|
213
|
-
this.makeTransfer(this.timeout, cb).submit(buffer);
|
|
214
|
-
} catch (e) {
|
|
215
|
-
process.nextTick(() => cb(e));
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
return this;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
public transferWithZLP(buffer: Buffer, callback: (error: LibUSBException | undefined) => void): void {
|
|
222
|
-
if (buffer.length % this.descriptor.wMaxPacketSize === 0) {
|
|
223
|
-
this.transfer(buffer);
|
|
224
|
-
this.transfer(Buffer.alloc(0), callback);
|
|
225
|
-
} else {
|
|
226
|
-
this.transfer(buffer, callback);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
package/tsc/usb/index.ts
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'events';
|
|
2
|
-
import { ExtendedDevice } from './device';
|
|
3
|
-
import * as usb from './bindings';
|
|
4
|
-
|
|
5
|
-
if (usb.INIT_ERROR) {
|
|
6
|
-
/* eslint-disable no-console */
|
|
7
|
-
console.warn('Failed to initialize libusb.');
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
Object.setPrototypeOf(usb, EventEmitter.prototype);
|
|
11
|
-
|
|
12
|
-
Object.getOwnPropertyNames(ExtendedDevice.prototype).forEach(name => {
|
|
13
|
-
Object.defineProperty(usb.Device.prototype, name, Object.getOwnPropertyDescriptor(ExtendedDevice.prototype, name) || Object.create(null));
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
interface EventListeners<T> {
|
|
17
|
-
newListener: keyof T;
|
|
18
|
-
removeListener: keyof T;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface DeviceIds {
|
|
22
|
-
idVendor: number;
|
|
23
|
-
idProduct: number;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
declare module './bindings' {
|
|
27
|
-
|
|
28
|
-
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
29
|
-
interface Device extends ExtendedDevice { }
|
|
30
|
-
|
|
31
|
-
interface DeviceEvents extends EventListeners<DeviceEvents> {
|
|
32
|
-
attach: Device;
|
|
33
|
-
detach: Device;
|
|
34
|
-
attachIds: DeviceIds;
|
|
35
|
-
detachIds: DeviceIds;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function addListener<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
|
|
39
|
-
function removeListener<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
|
|
40
|
-
function on<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
|
|
41
|
-
function off<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
|
|
42
|
-
function once<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
|
|
43
|
-
function listeners<K extends keyof DeviceEvents>(event: K): ((arg: DeviceEvents[K]) => void)[];
|
|
44
|
-
function rawListeners<K extends keyof DeviceEvents>(event: K): ((arg: DeviceEvents[K]) => void)[];
|
|
45
|
-
function removeAllListeners<K extends keyof DeviceEvents>(event?: K): void;
|
|
46
|
-
function emit<K extends keyof DeviceEvents>(event: K, arg: DeviceEvents[K]): boolean;
|
|
47
|
-
function listenerCount<K extends keyof DeviceEvents>(event: K): number;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Hotplug support
|
|
51
|
-
const hotplugSupported = usb._supportedHotplugEvents();
|
|
52
|
-
|
|
53
|
-
// Devices delta support for non-libusb hotplug events
|
|
54
|
-
// This methd needs to be used for attach/detach IDs (hotplugSupportType === 2) rather than a lookup because vid/pid are not unique
|
|
55
|
-
let hotPlugDevices = new Set<usb.Device>();
|
|
56
|
-
const emitHotplugEvents = () => {
|
|
57
|
-
// Collect current devices
|
|
58
|
-
const devices = new Set(usb.getDeviceList());
|
|
59
|
-
|
|
60
|
-
// Find attached devices
|
|
61
|
-
for (const device of devices) {
|
|
62
|
-
if (!hotPlugDevices.has(device)) {
|
|
63
|
-
usb.emit('attach', device);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Find detached devices
|
|
68
|
-
for (const device of hotPlugDevices) {
|
|
69
|
-
if (!devices.has(device)) {
|
|
70
|
-
usb.emit('detach', device);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
hotPlugDevices = devices;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
// Polling mechanism for checking device changes where hotplug detection is not available
|
|
78
|
-
let pollingHotplug = false;
|
|
79
|
-
const pollHotplug = (start = false) => {
|
|
80
|
-
if (start) {
|
|
81
|
-
pollingHotplug = true;
|
|
82
|
-
} else if (!pollingHotplug) {
|
|
83
|
-
return;
|
|
84
|
-
} else {
|
|
85
|
-
emitHotplugEvents();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
setTimeout(() => pollHotplug(), 500);
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
// Hotplug control
|
|
92
|
-
const startHotplug = () => {
|
|
93
|
-
if (hotplugSupported !== 1) {
|
|
94
|
-
// Collect initial devices when not using libusb
|
|
95
|
-
hotPlugDevices = new Set(usb.getDeviceList());
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (hotplugSupported) {
|
|
99
|
-
// Use hotplug event emitters
|
|
100
|
-
usb._enableHotplugEvents();
|
|
101
|
-
|
|
102
|
-
if (hotplugSupported === 2) {
|
|
103
|
-
// Use hotplug ID events to trigger a change check
|
|
104
|
-
usb.on('attachIds', emitHotplugEvents);
|
|
105
|
-
usb.on('detachIds', emitHotplugEvents);
|
|
106
|
-
}
|
|
107
|
-
} else {
|
|
108
|
-
// Fallback to using polling to check for changes
|
|
109
|
-
pollHotplug(true);
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
const stopHotplug = () => {
|
|
114
|
-
if (hotplugSupported) {
|
|
115
|
-
// Disable hotplug events
|
|
116
|
-
usb._disableHotplugEvents();
|
|
117
|
-
|
|
118
|
-
if (hotplugSupported === 2) {
|
|
119
|
-
// Remove hotplug ID event listeners
|
|
120
|
-
usb.off('attachIds', emitHotplugEvents);
|
|
121
|
-
usb.off('detachIds', emitHotplugEvents);
|
|
122
|
-
}
|
|
123
|
-
} else {
|
|
124
|
-
// Stop polling
|
|
125
|
-
pollingHotplug = false;
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
usb.on('newListener', event => {
|
|
130
|
-
if (event !== 'attach' && event !== 'detach') {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
const listenerCount = usb.listenerCount('attach') + usb.listenerCount('detach');
|
|
134
|
-
if (listenerCount === 0) {
|
|
135
|
-
startHotplug();
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
usb.on('removeListener', event => {
|
|
140
|
-
if (event !== 'attach' && event !== 'detach') {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
const listenerCount = usb.listenerCount('attach') + usb.listenerCount('detach');
|
|
144
|
-
if (listenerCount === 0) {
|
|
145
|
-
stopHotplug();
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
export = usb;
|