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
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.OutEndpoint = exports.InEndpoint = exports.Endpoint = void 0;
|
|
19
|
+
var events_1 = require("events");
|
|
20
|
+
var bindings_1 = require("./bindings");
|
|
21
|
+
var isBuffer = function (obj) { return obj && obj instanceof Uint8Array; };
|
|
22
|
+
/** Common base for InEndpoint and OutEndpoint. */
|
|
23
|
+
var Endpoint = /** @class */ (function (_super) {
|
|
24
|
+
__extends(Endpoint, _super);
|
|
25
|
+
function Endpoint(device, descriptor) {
|
|
26
|
+
var _this = _super.call(this) || this;
|
|
27
|
+
_this.device = device;
|
|
28
|
+
/** Sets the timeout in milliseconds for transfers on this endpoint. The default, `0`, is infinite timeout. */
|
|
29
|
+
_this.timeout = 0;
|
|
30
|
+
_this.descriptor = descriptor;
|
|
31
|
+
_this.address = descriptor.bEndpointAddress;
|
|
32
|
+
_this.transferType = descriptor.bmAttributes & 0x03;
|
|
33
|
+
return _this;
|
|
34
|
+
}
|
|
35
|
+
/** Clear the halt/stall condition for this endpoint. */
|
|
36
|
+
Endpoint.prototype.clearHalt = function (callback) {
|
|
37
|
+
return this.device.__clearHalt(this.address, callback);
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Create a new `Transfer` object for this endpoint.
|
|
41
|
+
*
|
|
42
|
+
* 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
|
|
43
|
+
* OUT transfers) or read (for IN transfers).
|
|
44
|
+
*
|
|
45
|
+
* @param timeout Timeout for the transfer (0 means unlimited).
|
|
46
|
+
* @param callback Transfer completion callback.
|
|
47
|
+
*/
|
|
48
|
+
Endpoint.prototype.makeTransfer = function (timeout, callback) {
|
|
49
|
+
return new bindings_1.Transfer(this.device, this.address, this.transferType, timeout, callback);
|
|
50
|
+
};
|
|
51
|
+
return Endpoint;
|
|
52
|
+
}(events_1.EventEmitter));
|
|
53
|
+
exports.Endpoint = Endpoint;
|
|
54
|
+
/** Endpoints in the IN direction (device->PC) have this type. */
|
|
55
|
+
var InEndpoint = /** @class */ (function (_super) {
|
|
56
|
+
__extends(InEndpoint, _super);
|
|
57
|
+
function InEndpoint(device, descriptor) {
|
|
58
|
+
var _this = _super.call(this, device, descriptor) || this;
|
|
59
|
+
/** Endpoint direction. */
|
|
60
|
+
_this.direction = 'in';
|
|
61
|
+
_this.pollTransferSize = 0;
|
|
62
|
+
_this.pollPending = 0;
|
|
63
|
+
_this.pollActive = false;
|
|
64
|
+
return _this;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Perform a transfer to read data from the endpoint.
|
|
68
|
+
*
|
|
69
|
+
* 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.
|
|
70
|
+
*
|
|
71
|
+
* `this` in the callback is the InEndpoint object.
|
|
72
|
+
*
|
|
73
|
+
* The device must be open to use this method.
|
|
74
|
+
* @param length
|
|
75
|
+
* @param callback
|
|
76
|
+
*/
|
|
77
|
+
InEndpoint.prototype.transfer = function (length, callback) {
|
|
78
|
+
var _this = this;
|
|
79
|
+
var buffer = Buffer.alloc(length);
|
|
80
|
+
var cb = function (error, _buffer, actualLength) {
|
|
81
|
+
callback.call(_this, error, buffer.slice(0, actualLength));
|
|
82
|
+
};
|
|
83
|
+
try {
|
|
84
|
+
this.makeTransfer(this.timeout, cb).submit(buffer);
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
process.nextTick(function () { return callback.call(_this, e); });
|
|
88
|
+
}
|
|
89
|
+
return this;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Start polling the endpoint.
|
|
93
|
+
*
|
|
94
|
+
* The library will keep `nTransfers` transfers of size `transferSize` pending in the kernel at all times to ensure continuous data flow.
|
|
95
|
+
* 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.
|
|
96
|
+
*
|
|
97
|
+
* The device must be open to use this method.
|
|
98
|
+
* @param nTransfers
|
|
99
|
+
* @param transferSize
|
|
100
|
+
*/
|
|
101
|
+
InEndpoint.prototype.startPoll = function (nTransfers, transferSize, _callback) {
|
|
102
|
+
var _this = this;
|
|
103
|
+
var transferDone = function (error, transfer, buffer, actualLength) {
|
|
104
|
+
if (!error) {
|
|
105
|
+
_this.emit('data', buffer.slice(0, actualLength));
|
|
106
|
+
}
|
|
107
|
+
else if (error.errno != bindings_1.LIBUSB_TRANSFER_CANCELLED) {
|
|
108
|
+
_this.emit('error', error);
|
|
109
|
+
_this.stopPoll();
|
|
110
|
+
}
|
|
111
|
+
if (_this.pollActive) {
|
|
112
|
+
startTransfer(transfer);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
_this.pollPending--;
|
|
116
|
+
if (_this.pollPending === 0) {
|
|
117
|
+
_this.pollTransfers = [];
|
|
118
|
+
_this.emit('end');
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
var startTransfer = function (transfer) {
|
|
123
|
+
try {
|
|
124
|
+
transfer.submit(Buffer.alloc(_this.pollTransferSize), function (error, buffer, actualLength) {
|
|
125
|
+
transferDone(error, transfer, buffer, actualLength);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
catch (e) {
|
|
129
|
+
_this.emit('error', e);
|
|
130
|
+
_this.stopPoll();
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
this.pollTransfers = this.startPollTransfers(nTransfers, transferSize, function (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
|
+
InEndpoint.prototype.startPollTransfers = function (nTransfers, transferSize, callback) {
|
|
141
|
+
if (nTransfers === void 0) { nTransfers = 3; }
|
|
142
|
+
if (transferSize === void 0) { transferSize = this.descriptor.wMaxPacketSize; }
|
|
143
|
+
if (this.pollTransfers) {
|
|
144
|
+
throw new Error('Polling already active');
|
|
145
|
+
}
|
|
146
|
+
this.pollTransferSize = transferSize;
|
|
147
|
+
this.pollActive = true;
|
|
148
|
+
this.pollPending = 0;
|
|
149
|
+
var transfers = [];
|
|
150
|
+
for (var i = 0; i < nTransfers; i++) {
|
|
151
|
+
var transfer = this.makeTransfer(0, callback);
|
|
152
|
+
transfers[i] = transfer;
|
|
153
|
+
}
|
|
154
|
+
return transfers;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Stop polling.
|
|
158
|
+
*
|
|
159
|
+
* Further data may still be received. The `end` event is emitted and the callback is called once all transfers have completed or canceled.
|
|
160
|
+
*
|
|
161
|
+
* The device must be open to use this method.
|
|
162
|
+
* @param callback
|
|
163
|
+
*/
|
|
164
|
+
InEndpoint.prototype.stopPoll = function (callback) {
|
|
165
|
+
if (!this.pollTransfers) {
|
|
166
|
+
throw new Error('Polling is not active.');
|
|
167
|
+
}
|
|
168
|
+
for (var i = 0; i < this.pollTransfers.length; i++) {
|
|
169
|
+
try {
|
|
170
|
+
this.pollTransfers[i].cancel();
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
this.emit('error', error);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
this.pollActive = false;
|
|
177
|
+
if (callback)
|
|
178
|
+
this.once('end', callback);
|
|
179
|
+
};
|
|
180
|
+
return InEndpoint;
|
|
181
|
+
}(Endpoint));
|
|
182
|
+
exports.InEndpoint = InEndpoint;
|
|
183
|
+
/** Endpoints in the OUT direction (PC->device) have this type. */
|
|
184
|
+
var OutEndpoint = /** @class */ (function (_super) {
|
|
185
|
+
__extends(OutEndpoint, _super);
|
|
186
|
+
function OutEndpoint() {
|
|
187
|
+
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
188
|
+
/** Endpoint direction. */
|
|
189
|
+
_this.direction = 'out';
|
|
190
|
+
return _this;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Perform a transfer to write `data` to the endpoint.
|
|
194
|
+
*
|
|
195
|
+
* 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.
|
|
196
|
+
*
|
|
197
|
+
* `this` in the callback is the OutEndpoint object.
|
|
198
|
+
*
|
|
199
|
+
* The device must be open to use this method.
|
|
200
|
+
* @param buffer
|
|
201
|
+
* @param callback
|
|
202
|
+
*/
|
|
203
|
+
OutEndpoint.prototype.transfer = function (buffer, callback) {
|
|
204
|
+
var _this = this;
|
|
205
|
+
if (!buffer) {
|
|
206
|
+
buffer = Buffer.alloc(0);
|
|
207
|
+
}
|
|
208
|
+
else if (!isBuffer(buffer)) {
|
|
209
|
+
buffer = Buffer.from(buffer);
|
|
210
|
+
}
|
|
211
|
+
var cb = function (error, _buffer, actual) {
|
|
212
|
+
if (callback) {
|
|
213
|
+
callback.call(_this, error, actual || 0);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
try {
|
|
217
|
+
this.makeTransfer(this.timeout, cb).submit(buffer);
|
|
218
|
+
}
|
|
219
|
+
catch (e) {
|
|
220
|
+
process.nextTick(function () { return cb(e); });
|
|
221
|
+
}
|
|
222
|
+
return this;
|
|
223
|
+
};
|
|
224
|
+
OutEndpoint.prototype.transferWithZLP = function (buffer, callback) {
|
|
225
|
+
if (buffer.length % this.descriptor.wMaxPacketSize === 0) {
|
|
226
|
+
this.transfer(buffer);
|
|
227
|
+
this.transfer(Buffer.alloc(0), callback);
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
this.transfer(buffer, callback);
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
return OutEndpoint;
|
|
234
|
+
}(Endpoint));
|
|
235
|
+
exports.OutEndpoint = OutEndpoint;
|
|
236
|
+
//# sourceMappingURL=endpoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"endpoint.js","sourceRoot":"","sources":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edDevice } from './device';
|
|
2
|
+
import * as usb from './bindings';
|
|
3
|
+
interface EventListeners<T> {
|
|
4
|
+
newListener: keyof T;
|
|
5
|
+
removeListener: keyof T;
|
|
6
|
+
}
|
|
7
|
+
declare module './bindings' {
|
|
8
|
+
interface Device extends ExtendedDevice {
|
|
9
|
+
}
|
|
10
|
+
interface DeviceEvents extends EventListeners<DeviceEvents> {
|
|
11
|
+
attach: Device;
|
|
12
|
+
detach: Device;
|
|
13
|
+
}
|
|
14
|
+
function addListener<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
|
|
15
|
+
function removeListener<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
|
|
16
|
+
function on<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
|
|
17
|
+
function off<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
|
|
18
|
+
function once<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
|
|
19
|
+
function listeners<K extends keyof DeviceEvents>(event: K): ((arg: DeviceEvents[K]) => void)[];
|
|
20
|
+
function rawListeners<K extends keyof DeviceEvents>(event: K): ((arg: DeviceEvents[K]) => void)[];
|
|
21
|
+
function removeAllListeners<K extends keyof DeviceEvents>(event?: K): void;
|
|
22
|
+
function emit<K extends keyof DeviceEvents>(event: K, arg: DeviceEvents[K]): boolean;
|
|
23
|
+
function listenerCount<K extends keyof DeviceEvents>(event: K): number;
|
|
24
|
+
}
|
|
25
|
+
export = usb;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __values = (this && this.__values) || function(o) {
|
|
3
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
4
|
+
if (m) return m.call(o);
|
|
5
|
+
if (o && typeof o.length === "number") return {
|
|
6
|
+
next: function () {
|
|
7
|
+
if (o && i >= o.length) o = void 0;
|
|
8
|
+
return { value: o && o[i++], done: !o };
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
12
|
+
};
|
|
13
|
+
var events_1 = require("events");
|
|
14
|
+
var device_1 = require("./device");
|
|
15
|
+
var usb = require("./bindings");
|
|
16
|
+
if (usb.INIT_ERROR) {
|
|
17
|
+
/* eslint-disable no-console */
|
|
18
|
+
console.warn('Failed to initialize libusb.');
|
|
19
|
+
}
|
|
20
|
+
Object.setPrototypeOf(usb, events_1.EventEmitter.prototype);
|
|
21
|
+
Object.getOwnPropertyNames(device_1.ExtendedDevice.prototype).forEach(function (name) {
|
|
22
|
+
Object.defineProperty(usb.Device.prototype, name, Object.getOwnPropertyDescriptor(device_1.ExtendedDevice.prototype, name) || Object.create(null));
|
|
23
|
+
});
|
|
24
|
+
// Polling mechanism for discovering device changes until this is fixed:
|
|
25
|
+
// https://github.com/libusb/libusb/issues/86
|
|
26
|
+
var pollTimeout = 500;
|
|
27
|
+
var hotplugSupported = usb._getLibusbCapability(usb.LIBUSB_CAP_HAS_HOTPLUG) > 0;
|
|
28
|
+
var pollingHotplug = false;
|
|
29
|
+
var pollDevices = [];
|
|
30
|
+
var pollHotplug = function (start) {
|
|
31
|
+
var e_1, _a, e_2, _b;
|
|
32
|
+
if (start === void 0) { start = false; }
|
|
33
|
+
if (start) {
|
|
34
|
+
pollingHotplug = true;
|
|
35
|
+
}
|
|
36
|
+
else if (!pollingHotplug) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
var devices = usb.getDeviceList();
|
|
40
|
+
if (!start) {
|
|
41
|
+
var _loop_1 = function (device) {
|
|
42
|
+
var found = pollDevices.find(function (item) { return item.deviceAddress === device.deviceAddress; });
|
|
43
|
+
if (!found) {
|
|
44
|
+
usb.emit('attach', device);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
try {
|
|
48
|
+
// Find attached devices
|
|
49
|
+
for (var devices_1 = __values(devices), devices_1_1 = devices_1.next(); !devices_1_1.done; devices_1_1 = devices_1.next()) {
|
|
50
|
+
var device = devices_1_1.value;
|
|
51
|
+
_loop_1(device);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
55
|
+
finally {
|
|
56
|
+
try {
|
|
57
|
+
if (devices_1_1 && !devices_1_1.done && (_a = devices_1.return)) _a.call(devices_1);
|
|
58
|
+
}
|
|
59
|
+
finally { if (e_1) throw e_1.error; }
|
|
60
|
+
}
|
|
61
|
+
var _loop_2 = function (device) {
|
|
62
|
+
var found = devices.find(function (item) { return item.deviceAddress === device.deviceAddress; });
|
|
63
|
+
if (!found) {
|
|
64
|
+
usb.emit('detach', device);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
try {
|
|
68
|
+
// Find detached devices
|
|
69
|
+
for (var pollDevices_1 = __values(pollDevices), pollDevices_1_1 = pollDevices_1.next(); !pollDevices_1_1.done; pollDevices_1_1 = pollDevices_1.next()) {
|
|
70
|
+
var device = pollDevices_1_1.value;
|
|
71
|
+
_loop_2(device);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
75
|
+
finally {
|
|
76
|
+
try {
|
|
77
|
+
if (pollDevices_1_1 && !pollDevices_1_1.done && (_b = pollDevices_1.return)) _b.call(pollDevices_1);
|
|
78
|
+
}
|
|
79
|
+
finally { if (e_2) throw e_2.error; }
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
pollDevices = devices;
|
|
83
|
+
setTimeout(function () {
|
|
84
|
+
pollHotplug();
|
|
85
|
+
}, pollTimeout);
|
|
86
|
+
};
|
|
87
|
+
usb.on('newListener', function (event) {
|
|
88
|
+
if (event !== 'attach' && event !== 'detach') {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
var listenerCount = usb.listenerCount('attach') + usb.listenerCount('detach');
|
|
92
|
+
if (listenerCount === 0) {
|
|
93
|
+
if (hotplugSupported) {
|
|
94
|
+
usb._enableHotplugEvents();
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
pollHotplug(true);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
usb.on('removeListener', function (event) {
|
|
102
|
+
if (event !== 'attach' && event !== 'detach') {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
var listenerCount = usb.listenerCount('attach') + usb.listenerCount('detach');
|
|
106
|
+
if (listenerCount === 0) {
|
|
107
|
+
if (hotplugSupported) {
|
|
108
|
+
usb._disableHotplugEvents();
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
pollingHotplug = false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
module.exports = usb;
|
|
116
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../tsc/usb/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iCAAsC;AACtC,mCAA0C;AAC1C,gCAAkC;AAElC,IAAI,GAAG,CAAC,UAAU,EAAE;IAChB,+BAA+B;IAC/B,OAAO,CAAC,IAAI,CAAC,8BAA8B,CAAC,CAAC;CAChD;AAED,MAAM,CAAC,cAAc,CAAC,GAAG,EAAE,qBAAY,CAAC,SAAS,CAAC,CAAC;AAEnD,MAAM,CAAC,mBAAmB,CAAC,uBAAc,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,UAAA,IAAI;IAC7D,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,MAAM,CAAC,wBAAwB,CAAC,uBAAc,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AAC9I,CAAC,CAAC,CAAC;AA6BH,wEAAwE;AACxE,6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ED,WAAW,GAAG,OAAO,CAAC;IACtB,UAAU,CAAC;QACP,WAAW,EAAE,CAAC;IAClB,CAAC,EAAE,WAAW,CAAC,CAAC;AACpB,CAAC,CAAC;AAEF,GAAG,CAAC,EAAE,CAAC,aAAa,EAAE,UAAA,KAAK;IACvB,IAAI,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,QAAQ,EAAE;QAC1C,OAAO;KACV;IACD,IAAM,aAAa,GAAG,GAAG,CAAC,aAAa,CAAC,QAAQ,CAAC,GAAG,GAAG,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IAChF,IAAI,aAAa,KAAK,CAAC,EAAE;QACrB,IAAI,gBAAgB,EAAE;YAClB,GAAG,CAAC,oBAAoB,EAAE,CAAC;SAC9B;aAAM;YACH,WAAW,CAAC,IAAI,CAAC,CAAC;SACrB;KACJ;AACL,CAAC,CAAC,CAAC;AAEH,GAAG,CAAC,EAAE,CAAC,gBAAgB,EAAE,UAAA,KAAK;IAC1B,IAAI,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,QAAQ,EAAE;QAC1C,OAAO;KACV;IACD,IAAM,aAAa,GAAG,GAAG,CAAC,aAAa,CAAC,QAAQ,CAAC,GAAG,GAAG,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IAChF,IAAI,aAAa,KAAK,CAAC,EAAE;QACrB,IAAI,gBAAgB,EAAE;YAClB,GAAG,CAAC,qBAAqB,EAAE,CAAC;SAC/B;aAAM;YACH,cAAc,GAAG,KAAK,CAAC;SAC1B;KACJ;AACL,CAAC,CAAC,CAAC;AAEH,iBAAS,GAAG,CAAC"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { LibUSBException, Device } from './bindings';
|
|
2
|
+
import { InterfaceDescriptor } from './descriptors';
|
|
3
|
+
import { Endpoint } from './endpoint';
|
|
4
|
+
export declare class Interface {
|
|
5
|
+
protected device: Device;
|
|
6
|
+
protected id: number;
|
|
7
|
+
/** Integer interface number. */
|
|
8
|
+
interfaceNumber: number;
|
|
9
|
+
/** Integer alternate setting number. */
|
|
10
|
+
altSetting: number;
|
|
11
|
+
/** Object with fields from the interface descriptor -- see libusb documentation or USB spec. */
|
|
12
|
+
descriptor: InterfaceDescriptor;
|
|
13
|
+
/** List of endpoints on this interface: InEndpoint and OutEndpoint objects. */
|
|
14
|
+
endpoints: Endpoint[];
|
|
15
|
+
constructor(device: Device, id: number);
|
|
16
|
+
protected refresh(): void;
|
|
17
|
+
/**
|
|
18
|
+
* Claims the interface. This method must be called before using any endpoints of this interface.
|
|
19
|
+
*
|
|
20
|
+
* The device must be open to use this method.
|
|
21
|
+
*/
|
|
22
|
+
claim(): void;
|
|
23
|
+
/**
|
|
24
|
+
* Releases the interface and resets the alternate setting. Calls callback when complete.
|
|
25
|
+
*
|
|
26
|
+
* It is an error to release an interface with pending transfers.
|
|
27
|
+
*
|
|
28
|
+
* The device must be open to use this method.
|
|
29
|
+
* @param callback
|
|
30
|
+
*/
|
|
31
|
+
release(callback?: (error?: LibUSBException) => void): void;
|
|
32
|
+
/**
|
|
33
|
+
* Releases the interface and resets the alternate setting. Calls callback when complete.
|
|
34
|
+
*
|
|
35
|
+
* It is an error to release an interface with pending transfers. If the optional closeEndpoints
|
|
36
|
+
* parameter is true, any active endpoint streams are stopped (see `Endpoint.stopStream`),
|
|
37
|
+
* and the interface is released after the stream transfers are cancelled. Transfers submitted
|
|
38
|
+
* individually with `Endpoint.transfer` are not affected by this parameter.
|
|
39
|
+
*
|
|
40
|
+
* The device must be open to use this method.
|
|
41
|
+
* @param closeEndpoints
|
|
42
|
+
* @param callback
|
|
43
|
+
*/
|
|
44
|
+
release(closeEndpoints?: boolean, callback?: (error?: LibUSBException) => void): void;
|
|
45
|
+
/**
|
|
46
|
+
* Returns `false` if a kernel driver is not active; `true` if active.
|
|
47
|
+
*
|
|
48
|
+
* The device must be open to use this method.
|
|
49
|
+
*/
|
|
50
|
+
isKernelDriverActive(): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Detaches the kernel driver from the interface.
|
|
53
|
+
*
|
|
54
|
+
* The device must be open to use this method.
|
|
55
|
+
*/
|
|
56
|
+
detachKernelDriver(): void;
|
|
57
|
+
/**
|
|
58
|
+
* Re-attaches the kernel driver for the interface.
|
|
59
|
+
*
|
|
60
|
+
* The device must be open to use this method.
|
|
61
|
+
*/
|
|
62
|
+
attachKernelDriver(): void;
|
|
63
|
+
/**
|
|
64
|
+
* Sets the alternate setting. It updates the `interface.endpoints` array to reflect the endpoints found in the alternate setting.
|
|
65
|
+
*
|
|
66
|
+
* The device must be open to use this method.
|
|
67
|
+
* @param altSetting
|
|
68
|
+
* @param callback
|
|
69
|
+
*/
|
|
70
|
+
setAltSetting(altSetting: number, callback?: (error: LibUSBException | undefined) => void): void;
|
|
71
|
+
/**
|
|
72
|
+
* Return the InEndpoint or OutEndpoint with the specified address.
|
|
73
|
+
*
|
|
74
|
+
* The device must be open to use this method.
|
|
75
|
+
* @param addr
|
|
76
|
+
*/
|
|
77
|
+
endpoint(addr: number): Endpoint;
|
|
78
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Interface = void 0;
|
|
4
|
+
var bindings_1 = require("./bindings");
|
|
5
|
+
var endpoint_1 = require("./endpoint");
|
|
6
|
+
var Interface = /** @class */ (function () {
|
|
7
|
+
function Interface(device, id) {
|
|
8
|
+
this.device = device;
|
|
9
|
+
this.id = id;
|
|
10
|
+
/** Integer alternate setting number. */
|
|
11
|
+
this.altSetting = 0;
|
|
12
|
+
this.refresh();
|
|
13
|
+
}
|
|
14
|
+
Interface.prototype.refresh = function () {
|
|
15
|
+
if (!this.device.configDescriptor) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
this.descriptor = this.device.configDescriptor.interfaces[this.id][this.altSetting];
|
|
19
|
+
this.interfaceNumber = this.descriptor.bInterfaceNumber;
|
|
20
|
+
this.endpoints = [];
|
|
21
|
+
var len = this.descriptor.endpoints.length;
|
|
22
|
+
for (var i = 0; i < len; i++) {
|
|
23
|
+
var desc = this.descriptor.endpoints[i];
|
|
24
|
+
var c = (desc.bEndpointAddress & bindings_1.LIBUSB_ENDPOINT_IN) ? endpoint_1.InEndpoint : endpoint_1.OutEndpoint;
|
|
25
|
+
this.endpoints[i] = new c(this.device, desc);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Claims the interface. This method must be called before using any endpoints of this interface.
|
|
30
|
+
*
|
|
31
|
+
* The device must be open to use this method.
|
|
32
|
+
*/
|
|
33
|
+
Interface.prototype.claim = function () {
|
|
34
|
+
this.device.__claimInterface(this.id);
|
|
35
|
+
};
|
|
36
|
+
Interface.prototype.release = function (closeEndpointsOrCallback, callback) {
|
|
37
|
+
var _this = this;
|
|
38
|
+
var closeEndpoints = false;
|
|
39
|
+
if (typeof closeEndpointsOrCallback === 'boolean') {
|
|
40
|
+
closeEndpoints = closeEndpointsOrCallback;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
callback = closeEndpointsOrCallback;
|
|
44
|
+
}
|
|
45
|
+
var next = function () {
|
|
46
|
+
_this.device.__releaseInterface(_this.id, function (error) {
|
|
47
|
+
if (!error) {
|
|
48
|
+
_this.altSetting = 0;
|
|
49
|
+
_this.refresh();
|
|
50
|
+
}
|
|
51
|
+
if (callback) {
|
|
52
|
+
callback.call(_this, error);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
if (!closeEndpoints || this.endpoints.length === 0) {
|
|
57
|
+
next();
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
var n_1 = this.endpoints.length;
|
|
61
|
+
this.endpoints.forEach(function (ep) {
|
|
62
|
+
if (ep.direction === 'in' && ep.pollActive) {
|
|
63
|
+
ep.once('end', function () {
|
|
64
|
+
if (--n_1 === 0) {
|
|
65
|
+
next();
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
ep.stopPoll();
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
if (--n_1 === 0) {
|
|
72
|
+
next();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Returns `false` if a kernel driver is not active; `true` if active.
|
|
80
|
+
*
|
|
81
|
+
* The device must be open to use this method.
|
|
82
|
+
*/
|
|
83
|
+
Interface.prototype.isKernelDriverActive = function () {
|
|
84
|
+
return this.device.__isKernelDriverActive(this.id);
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Detaches the kernel driver from the interface.
|
|
88
|
+
*
|
|
89
|
+
* The device must be open to use this method.
|
|
90
|
+
*/
|
|
91
|
+
Interface.prototype.detachKernelDriver = function () {
|
|
92
|
+
return this.device.__detachKernelDriver(this.id);
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Re-attaches the kernel driver for the interface.
|
|
96
|
+
*
|
|
97
|
+
* The device must be open to use this method.
|
|
98
|
+
*/
|
|
99
|
+
Interface.prototype.attachKernelDriver = function () {
|
|
100
|
+
return this.device.__attachKernelDriver(this.id);
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Sets the alternate setting. It updates the `interface.endpoints` array to reflect the endpoints found in the alternate setting.
|
|
104
|
+
*
|
|
105
|
+
* The device must be open to use this method.
|
|
106
|
+
* @param altSetting
|
|
107
|
+
* @param callback
|
|
108
|
+
*/
|
|
109
|
+
Interface.prototype.setAltSetting = function (altSetting, callback) {
|
|
110
|
+
var _this = this;
|
|
111
|
+
this.device.__setInterface(this.id, altSetting, function (error) {
|
|
112
|
+
if (!error) {
|
|
113
|
+
_this.altSetting = altSetting;
|
|
114
|
+
_this.refresh();
|
|
115
|
+
}
|
|
116
|
+
if (callback) {
|
|
117
|
+
callback.call(_this, error);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Return the InEndpoint or OutEndpoint with the specified address.
|
|
123
|
+
*
|
|
124
|
+
* The device must be open to use this method.
|
|
125
|
+
* @param addr
|
|
126
|
+
*/
|
|
127
|
+
Interface.prototype.endpoint = function (addr) {
|
|
128
|
+
var endpoint = this.endpoints.find(function (item) { return item.address === addr; });
|
|
129
|
+
if (!endpoint) {
|
|
130
|
+
throw new Error("Endpoint not found for address: " + addr);
|
|
131
|
+
}
|
|
132
|
+
return endpoint;
|
|
133
|
+
};
|
|
134
|
+
return Interface;
|
|
135
|
+
}());
|
|
136
|
+
exports.Interface = Interface;
|
|
137
|
+
//# sourceMappingURL=interface.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interface.js","sourceRoot":"","sources":["../../tsc/usb/interface.ts"],"names":[],"mappings":";;;AAAA,uCAAyE;AAEzE,uCAA+D;AAE/D;IAaI,mBAAsB,MAAc,EAAY,EAAU;QAApC,WAAM,GAAN,MAAM,CAAQ;QAAY,OAAE,GAAF,EAAE,CAAQ;QAT1D,wCAAwC;QACjC,eAAU,GAAG,CAAC,CAAC;QASlB,IAAI,CAAC,OAAO,EAAE,CAAC;IACnB,CAAC;IAES,2BAAO,GAAjB;QACI,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,gBAAgB,EAAE;YAC/B,OAAO;SACV;QAED,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,gBAAgB,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACpF,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,UAAU,CAAC,gBAAgB,CAAC;QACxD,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;QACpB,IAAM,GAAG,GAAG,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,MAAM,CAAC;QAC7C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG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AC;IAED;;;;;OAKG;IACI,4BAAQ,GAAf,UAAgB,IAAY;QACxB,IAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,UAAA,IAAI,IAAI,OAAA,IAAI,CAAC,OAAO,KAAK,IAAI,EAArB,CAAqB,CAAC,CAAC;QACpE,IAAI,CAAC,QAAQ,EAAE;YACX,MAAM,IAAI,KAAK,CAAC,qCAAmC,IAAM,CAAC,CAAC;SAC9D;QAED,OAAO,QAAQ,CAAC;IACpB,CAAC;IACL,gBAAC;AAAD,CAAC,AAvKD,IAuKC;AAvKY,8BAAS"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/// <reference types="w3c-web-usb" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { EventEmitter } from 'events';
|
|
4
|
+
/**
|
|
5
|
+
* USB Options
|
|
6
|
+
*/
|
|
7
|
+
export interface USBOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Optional `device found` callback function to allow the user to select a device
|
|
10
|
+
*/
|
|
11
|
+
devicesFound?: (devices: USBDevice[]) => Promise<USBDevice | void>;
|
|
12
|
+
/**
|
|
13
|
+
* Optional array of preconfigured allowed devices
|
|
14
|
+
*/
|
|
15
|
+
allowedDevices?: USBDeviceFilter[];
|
|
16
|
+
/**
|
|
17
|
+
* Optional flag to automatically allow all devices
|
|
18
|
+
*/
|
|
19
|
+
allowAllDevices?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Optional timeout (in milliseconds) to use for the device control transfers
|
|
22
|
+
*/
|
|
23
|
+
deviceTimeout?: number;
|
|
24
|
+
}
|
|
25
|
+
export declare class WebUSB implements USB {
|
|
26
|
+
private options;
|
|
27
|
+
protected emitter: EventEmitter;
|
|
28
|
+
protected knownDevices: Map<string, USBDevice>;
|
|
29
|
+
protected allowedDevices: USBDeviceFilter[];
|
|
30
|
+
constructor(options?: USBOptions);
|
|
31
|
+
private _onconnect;
|
|
32
|
+
set onconnect(fn: (ev: USBConnectionEvent) => void);
|
|
33
|
+
private _ondisconnect;
|
|
34
|
+
set ondisconnect(fn: (ev: USBConnectionEvent) => void);
|
|
35
|
+
addEventListener(type: 'connect' | 'disconnect', listener: (this: this, ev: USBConnectionEvent) => void): void;
|
|
36
|
+
addEventListener(type: 'connect' | 'disconnect', listener: EventListener): void;
|
|
37
|
+
removeEventListener(type: 'connect' | 'disconnect', callback: (this: this, ev: USBConnectionEvent) => void): void;
|
|
38
|
+
removeEventListener(type: 'connect' | 'disconnect', callback: EventListener): void;
|
|
39
|
+
dispatchEvent(_event: Event): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Requests a single Web USB device
|
|
42
|
+
* @param options The options to use when scanning
|
|
43
|
+
* @returns Promise containing the selected device
|
|
44
|
+
*/
|
|
45
|
+
requestDevice(options?: USBDeviceRequestOptions): Promise<USBDevice>;
|
|
46
|
+
/**
|
|
47
|
+
* Gets all allowed Web USB devices which are connected
|
|
48
|
+
* @returns Promise containing an array of devices
|
|
49
|
+
*/
|
|
50
|
+
getDevices(): Promise<USBDevice[]>;
|
|
51
|
+
private loadDevices;
|
|
52
|
+
private preFilterDevices;
|
|
53
|
+
private filterDevice;
|
|
54
|
+
private getDeviceId;
|
|
55
|
+
private isAllowedDevice;
|
|
56
|
+
}
|