usb 2.5.2-alpha.1 → 2.6.0

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.
Files changed (43) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/index.d.ts +10 -6
  3. package/dist/index.js +27 -11
  4. package/dist/index.js.map +1 -1
  5. package/dist/usb/endpoint.js +3 -1
  6. package/dist/usb/endpoint.js.map +1 -1
  7. package/dist/webusb/index.js +40 -24
  8. package/dist/webusb/index.js.map +1 -1
  9. package/dist/webusb/webusb-device.d.ts +1 -1
  10. package/dist/webusb/webusb-device.js +24 -30
  11. package/dist/webusb/webusb-device.js.map +1 -1
  12. package/package.json +12 -12
  13. package/prebuilds/android-arm/node.napi.armv7.node +0 -0
  14. package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
  15. package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
  16. package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
  17. package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
  18. package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
  19. package/prebuilds/linux-ia32/node.napi.node +0 -0
  20. package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  21. package/prebuilds/linux-x64/node.napi.musl.node +0 -0
  22. package/prebuilds/win32-ia32/node.napi.node +0 -0
  23. package/prebuilds/win32-x64/node.napi.node +0 -0
  24. package/.eslintignore +0 -4
  25. package/.eslintrc.json +0 -87
  26. package/.gitattributes +0 -1
  27. package/.gitmodules +0 -3
  28. package/.vscode/launch.json +0 -15
  29. package/.vscode/tasks.json +0 -23
  30. package/libusb/.gitattributes +0 -7
  31. package/libusb/msvc/.gitattributes +0 -3
  32. package/tsc/index.ts +0 -75
  33. package/tsc/usb/bindings.ts +0 -310
  34. package/tsc/usb/capability.ts +0 -22
  35. package/tsc/usb/descriptors.ts +0 -180
  36. package/tsc/usb/device.ts +0 -331
  37. package/tsc/usb/endpoint.ts +0 -229
  38. package/tsc/usb/index.ts +0 -149
  39. package/tsc/usb/interface.ts +0 -167
  40. package/tsc/webusb/index.ts +0 -374
  41. package/tsc/webusb/webusb-device.ts +0 -490
  42. package/tsconfig.json +0 -17
  43. package/typedoc.json +0 -9
@@ -1,180 +0,0 @@
1
- /** A structure representing the standard USB device descriptor */
2
- export interface DeviceDescriptor {
3
- /** Size of this descriptor (in bytes) */
4
- bLength: number;
5
-
6
- /** Descriptor type. */
7
- bDescriptorType: number;
8
-
9
- /** USB specification release number in binary-coded decimal. */
10
- bcdUSB: number;
11
-
12
- /** USB-IF class code for the device. */
13
- bDeviceClass: number;
14
-
15
- /** USB-IF subclass code for the device, qualified by the bDeviceClass value. */
16
- bDeviceSubClass: number;
17
-
18
- /** USB-IF protocol code for the device, qualified by the bDeviceClass and bDeviceSubClass values. */
19
- bDeviceProtocol: number;
20
-
21
- /** Maximum packet size for endpoint 0. */
22
- bMaxPacketSize0: number;
23
-
24
- /** USB-IF vendor ID. */
25
- idVendor: number;
26
-
27
- /** USB-IF product ID. */
28
- idProduct: number;
29
-
30
- /** Device release number in binary-coded decimal. */
31
- bcdDevice: number;
32
-
33
- /** Index of string descriptor describing manufacturer. */
34
- iManufacturer: number;
35
-
36
- /** Index of string descriptor describing product. */
37
- iProduct: number;
38
-
39
- /** Index of string descriptor containing device serial number. */
40
- iSerialNumber: number;
41
-
42
- /** Number of possible configurations. */
43
- bNumConfigurations: number;
44
- }
45
-
46
- /** A structure representing the standard USB configuration descriptor */
47
- export interface ConfigDescriptor {
48
- /** Size of this descriptor (in bytes) */
49
- bLength: number;
50
-
51
- /** Descriptor type. */
52
- bDescriptorType: number;
53
-
54
- /** Total length of data returned for this configuration. */
55
- wTotalLength: number;
56
-
57
- /** Number of interfaces supported by this configuration. */
58
- bNumInterfaces: number;
59
-
60
- /** Identifier value for this configuration. */
61
- bConfigurationValue: number;
62
-
63
- /** Index of string descriptor describing this configuration. */
64
- iConfiguration: number;
65
-
66
- /** Configuration characteristics. */
67
- bmAttributes: number;
68
-
69
- /** Maximum power consumption of the USB device from this bus in this configuration when the device is fully operation. */
70
- bMaxPower: number;
71
-
72
- /** Extra descriptors. */
73
- extra: Buffer;
74
-
75
- /** Array of interfaces supported by this configuration. */
76
- interfaces: InterfaceDescriptor[][];
77
- }
78
-
79
- /** A structure representing the standard USB interface descriptor */
80
- export interface InterfaceDescriptor {
81
- /** Size of this descriptor (in bytes) */
82
- bLength: number;
83
-
84
- /** Descriptor type. */
85
- bDescriptorType: number;
86
-
87
- /** Number of this interface. */
88
- bInterfaceNumber: number;
89
-
90
- /** Value used to select this alternate setting for this interface. */
91
- bAlternateSetting: number;
92
-
93
- /** Number of endpoints used by this interface (excluding the control endpoint). */
94
- bNumEndpoints: number;
95
-
96
- /** USB-IF class code for this interface. */
97
- bInterfaceClass: number;
98
-
99
- /** USB-IF subclass code for this interface, qualified by the bInterfaceClass value. */
100
- bInterfaceSubClass: number;
101
-
102
- /** USB-IF protocol code for this interface, qualified by the bInterfaceClass and bInterfaceSubClass values. */
103
- bInterfaceProtocol: number;
104
-
105
- /** Index of string descriptor describing this interface. */
106
- iInterface: number;
107
-
108
- /** Extra descriptors. */
109
- extra: Buffer;
110
-
111
- /** Array of endpoint descriptors. */
112
- endpoints: EndpointDescriptor[];
113
- }
114
-
115
- /** A structure representing the standard USB endpoint descriptor */
116
- export interface EndpointDescriptor {
117
- /** Size of this descriptor (in bytes) */
118
- bLength: number;
119
-
120
- /** Descriptor type. */
121
- bDescriptorType: number;
122
-
123
- /** The address of the endpoint described by this descriptor. */
124
- bEndpointAddress: number;
125
-
126
- /** Attributes which apply to the endpoint when it is configured using the bConfigurationValue. */
127
- bmAttributes: number;
128
-
129
- /** Maximum packet size this endpoint is capable of sending/receiving. */
130
- wMaxPacketSize: number;
131
-
132
- /** Interval for polling endpoint for data transfers. */
133
- bInterval: number;
134
-
135
- /** For audio devices only: the rate at which synchronization feedback is provided. */
136
- bRefresh: number;
137
-
138
- /** For audio devices only: the address if the synch endpoint. */
139
- bSynchAddress: number;
140
-
141
- /**
142
- * Extra descriptors.
143
- *
144
- * If libusb encounters unknown endpoint descriptors, it will store them here, should you wish to parse them.
145
- */
146
- extra: Buffer;
147
- }
148
-
149
- /** A generic representation of a BOS Device Capability descriptor */
150
- export interface CapabilityDescriptor {
151
- /** Size of this descriptor (in bytes) */
152
- bLength: number;
153
-
154
- /** Descriptor type. */
155
- bDescriptorType: number;
156
-
157
- /** Device Capability type. */
158
- bDevCapabilityType: number;
159
-
160
- /** Device Capability data (bLength - 3 bytes) */
161
- dev_capability_data: Buffer;
162
- }
163
-
164
- /** A structure representing the Binary Device Object Store (BOS) descriptor */
165
- export interface BosDescriptor {
166
- /** Size of this descriptor (in bytes) */
167
- bLength: number;
168
-
169
- /** Descriptor type. */
170
- bDescriptorType: number;
171
-
172
- /** Length of this descriptor and all of its sub descriptors. */
173
- wTotalLength: number;
174
-
175
- /** The number of separate device capability descriptors in the BOS. */
176
- bNumDeviceCaps: number;
177
-
178
- /** Device Capability Descriptors */
179
- capabilities: CapabilityDescriptor[];
180
- }
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
- }