usb 2.9.0 → 2.10.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.
- package/CHANGELOG.md +5 -0
- package/README.md +6 -0
- package/binding.gyp +1 -1
- package/dist/index.js +35 -113
- package/dist/index.js.map +1 -1
- package/dist/usb/bindings.js +2 -2
- package/dist/usb/bindings.js.map +1 -1
- package/dist/usb/capability.js +3 -4
- package/dist/usb/capability.js.map +1 -1
- package/dist/usb/device.js +101 -123
- package/dist/usb/device.js.map +1 -1
- package/dist/usb/endpoint.d.ts +2 -1
- package/dist/usb/endpoint.js +74 -98
- package/dist/usb/endpoint.js.map +1 -1
- package/dist/usb/index.js +26 -59
- package/dist/usb/index.js.map +1 -1
- package/dist/usb/interface.js +41 -44
- package/dist/usb/interface.js.map +1 -1
- package/dist/webusb/index.js +169 -355
- package/dist/webusb/index.js.map +1 -1
- package/dist/webusb/webusb-device.js +367 -751
- package/dist/webusb/webusb-device.js.map +1 -1
- package/package.json +5 -5
- 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/test/usb.coffee +25 -4
- package/test/worker.cjs +5 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -20,6 +20,8 @@ You can install one using [Zadig](http://zadig.akeo.ie/) or another approach is
|
|
|
20
20
|
|
|
21
21
|
Note that you cannot use multiple drivers on Windows as they get exclusive access to the device. So if you want to switch between drivers (e.g. using a printer with this software or the system), you will need to uninstall/install drivers as required.
|
|
22
22
|
|
|
23
|
+
For further info, check [How to use libusb on Windows](https://github.com/libusb/libusb/wiki/Windows#user-content-How_to_use_libusb_on_Windows) in the libusb's wiki.
|
|
24
|
+
|
|
23
25
|
## Linux
|
|
24
26
|
|
|
25
27
|
On Linux, you'll need libudev to build libusb if a prebuild is not available. On Ubuntu/Debian:
|
|
@@ -28,6 +30,10 @@ On Linux, you'll need libudev to build libusb if a prebuild is not available. On
|
|
|
28
30
|
sudo apt-get install build-essential libudev-dev
|
|
29
31
|
```
|
|
30
32
|
|
|
33
|
+
# Troubleshooting
|
|
34
|
+
|
|
35
|
+
For libusb issues, please refer to the FAQ at https://github.com/libusb/libusb/wiki/FAQ
|
|
36
|
+
|
|
31
37
|
# Installation
|
|
32
38
|
|
|
33
39
|
Native modules are bundled using `prebuildify`, so installation should be as simple as installing the package.
|
package/binding.gyp
CHANGED
package/dist/index.js
CHANGED
|
@@ -9,136 +9,58 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
9
9
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
10
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
23
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
-
function step(op) {
|
|
26
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
-
while (_) try {
|
|
28
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
-
switch (op[0]) {
|
|
31
|
-
case 0: case 1: t = op; break;
|
|
32
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
-
default:
|
|
36
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
-
if (t[2]) _.ops.pop();
|
|
41
|
-
_.trys.pop(); continue;
|
|
42
|
-
}
|
|
43
|
-
op = body.call(thisArg, _);
|
|
44
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
var __values = (this && this.__values) || function(o) {
|
|
49
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
50
|
-
if (m) return m.call(o);
|
|
51
|
-
if (o && typeof o.length === "number") return {
|
|
52
|
-
next: function () {
|
|
53
|
-
if (o && i >= o.length) o = void 0;
|
|
54
|
-
return { value: o && o[i++], done: !o };
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
58
|
-
};
|
|
59
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
60
13
|
exports.LibUSBException = exports.useUsbDkBackend = exports.getDeviceList = exports.Transfer = exports.Device = exports.webusb = exports.findBySerialNumber = exports.findByIds = exports.usb = void 0;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
14
|
+
const util_1 = require("util");
|
|
15
|
+
const webusb_1 = require("./webusb");
|
|
16
|
+
const usb = require("./usb");
|
|
64
17
|
exports.usb = usb;
|
|
65
18
|
/**
|
|
66
19
|
* Convenience method to get the first device with the specified VID and PID, or `undefined` if no such device is present.
|
|
67
20
|
* @param vid
|
|
68
21
|
* @param pid
|
|
69
22
|
*/
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return devices.find(
|
|
23
|
+
const findByIds = (vid, pid) => {
|
|
24
|
+
const devices = usb.getDeviceList();
|
|
25
|
+
return devices.find(item => item.deviceDescriptor.idVendor === vid && item.deviceDescriptor.idProduct === pid);
|
|
73
26
|
};
|
|
74
27
|
exports.findByIds = findByIds;
|
|
75
28
|
/**
|
|
76
29
|
* Convenience method to get the device with the specified serial number, or `undefined` if no such device is present.
|
|
77
30
|
* @param serialNumber
|
|
78
31
|
*/
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (
|
|
99
|
-
device.
|
|
100
|
-
}
|
|
101
|
-
getStringDescriptor = util_1.promisify(device.getStringDescriptor).bind(device);
|
|
102
|
-
return [4 /*yield*/, getStringDescriptor(device.deviceDescriptor.iSerialNumber)];
|
|
103
|
-
case 4:
|
|
104
|
-
buffer = _c.sent();
|
|
105
|
-
if (buffer && buffer.toString() === serialNumber) {
|
|
106
|
-
return [2 /*return*/, device];
|
|
107
|
-
}
|
|
108
|
-
return [3 /*break*/, 7];
|
|
109
|
-
case 5:
|
|
110
|
-
_a = _c.sent();
|
|
111
|
-
return [3 /*break*/, 7];
|
|
112
|
-
case 6:
|
|
113
|
-
try {
|
|
114
|
-
if (opened(device)) {
|
|
115
|
-
device.close();
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
catch (_d) {
|
|
119
|
-
// Ignore any errors, device may be a system device or inaccessible
|
|
120
|
-
}
|
|
121
|
-
return [7 /*endfinally*/];
|
|
122
|
-
case 7:
|
|
123
|
-
devices_1_1 = devices_1.next();
|
|
124
|
-
return [3 /*break*/, 2];
|
|
125
|
-
case 8: return [3 /*break*/, 11];
|
|
126
|
-
case 9:
|
|
127
|
-
e_1_1 = _c.sent();
|
|
128
|
-
e_1 = { error: e_1_1 };
|
|
129
|
-
return [3 /*break*/, 11];
|
|
130
|
-
case 10:
|
|
131
|
-
try {
|
|
132
|
-
if (devices_1_1 && !devices_1_1.done && (_b = devices_1.return)) _b.call(devices_1);
|
|
32
|
+
const findBySerialNumber = async (serialNumber) => {
|
|
33
|
+
const devices = usb.getDeviceList();
|
|
34
|
+
const opened = (device) => !!device.interfaces;
|
|
35
|
+
for (const device of devices) {
|
|
36
|
+
try {
|
|
37
|
+
if (!opened(device)) {
|
|
38
|
+
device.open();
|
|
39
|
+
}
|
|
40
|
+
const getStringDescriptor = util_1.promisify(device.getStringDescriptor).bind(device);
|
|
41
|
+
const buffer = await getStringDescriptor(device.deviceDescriptor.iSerialNumber);
|
|
42
|
+
if (buffer && buffer.toString() === serialNumber) {
|
|
43
|
+
return device;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// Ignore any errors, device may be a system device or inaccessible
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
try {
|
|
51
|
+
if (opened(device)) {
|
|
52
|
+
device.close();
|
|
133
53
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// Ignore any errors, device may be a system device or inaccessible
|
|
57
|
+
}
|
|
137
58
|
}
|
|
138
|
-
}
|
|
139
|
-
|
|
59
|
+
}
|
|
60
|
+
return undefined;
|
|
61
|
+
};
|
|
140
62
|
exports.findBySerialNumber = findBySerialNumber;
|
|
141
|
-
|
|
63
|
+
const webusb = new webusb_1.WebUSB();
|
|
142
64
|
exports.webusb = webusb;
|
|
143
65
|
// Usb types
|
|
144
66
|
var usb_1 = require("./usb");
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../tsc/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../tsc/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,+BAAiC;AACjC,qCAAkC;AAClC,6BAA6B;AAoDzB,kBAAG;AAlDP;;;;GAIG;AACH,MAAM,SAAS,GAAG,CAAC,GAAW,EAAE,GAAW,EAA0B,EAAE;IACnE,MAAM,OAAO,GAAG,GAAG,CAAC,aAAa,EAAE,CAAC;IACpC,OAAO,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,gBAAgB,CAAC,QAAQ,KAAK,GAAG,IAAI,IAAI,CAAC,gBAAgB,CAAC,SAAS,KAAK,GAAG,CAAC,CAAC;AACnH,CAAC,CAAC;AA6CE,8BAAS;AA3Cb;;;GAGG;AACH,MAAM,kBAAkB,GAAG,KAAK,EAAE,YAAoB,EAAmC,EAAE;IACvF,MAAM,OAAO,GAAG,GAAG,CAAC,aAAa,EAAE,CAAC;IACpC,MAAM,MAAM,GAAG,CAAC,MAAkB,EAAW,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC;IAEpE,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;QAC1B,IAAI;YACA,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE;gBACjB,MAAM,CAAC,IAAI,EAAE,CAAC;aACjB;YAED,MAAM,mBAAmB,GAAG,gBAAS,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAC/E,MAAM,MAAM,GAAG,MAAM,mBAAmB,CAAC,MAAM,CAAC,gBAAgB,CAAC,aAAa,CAAC,CAAC;YAEhF,IAAI,MAAM,IAAI,MAAM,CAAC,QAAQ,EAAE,KAAK,YAAY,EAAE;gBAC9C,OAAO,MAAM,CAAC;aACjB;SACJ;QAAC,MAAM;YACJ,mEAAmE;SACtE;gBAAS;YACN,IAAI;gBACA,IAAI,MAAM,CAAC,MAAM,CAAC,EAAE;oBAChB,MAAM,CAAC,KAAK,EAAE,CAAC;iBAClB;aACJ;YAAC,MAAM;gBACJ,mEAAmE;aACtE;SACJ;KACJ;IAED,OAAO,SAAS,CAAC;AACrB,CAAC,CAAC;AAUE,gDAAkB;AARtB,MAAM,MAAM,GAAG,IAAI,eAAM,EAAE,CAAC;AAWxB,wBAAM;AAGV,YAAY;AACZ,6BAAwG;AAA/F,6FAAA,MAAM,OAAA;AAAE,+FAAA,QAAQ,OAAA;AAAgB,oGAAA,aAAa,OAAA;AAAE,sGAAA,eAAe,OAAA;AAAE,sGAAA,eAAe,OAAA;AACxF,mDAAiC;AACjC,oDAAkC;AAClC,iDAA+B;AAC/B,kDAAgC;AAEhC,eAAe;AACf,2CAAyB;AACzB,yDAAuC"}
|
package/dist/usb/bindings.js
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
// Eric Brody <https://github.com/underscorebrody>
|
|
4
4
|
// Rob Moran <https://github.com/thegecko>
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
6
|
+
const path_1 = require("path");
|
|
7
7
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
8
|
-
|
|
8
|
+
const usb = require('node-gyp-build')(path_1.join(__dirname, '..', '..'));
|
|
9
9
|
module.exports = usb;
|
|
10
10
|
//# sourceMappingURL=bindings.js.map
|
package/dist/usb/bindings.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bindings.js","sourceRoot":"","sources":["../../tsc/usb/bindings.ts"],"names":[],"mappings":";AAAA,+CAA+C;AAC/C,mDAAmD;AACnD,2CAA2C;;AAE3C
|
|
1
|
+
{"version":3,"file":"bindings.js","sourceRoot":"","sources":["../../tsc/usb/bindings.ts"],"names":[],"mappings":";AAAA,+CAA+C;AAC/C,mDAAmD;AACnD,2CAA2C;;AAE3C,+BAA4B;AAG5B,uDAAuD;AACvD,MAAM,GAAG,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC,WAAI,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AACnE,MAAM,CAAC,OAAO,GAAG,GAAG,CAAC"}
|
package/dist/usb/capability.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Capability = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
class Capability {
|
|
5
|
+
constructor(device, id) {
|
|
6
6
|
this.device = device;
|
|
7
7
|
this.id = id;
|
|
8
8
|
if (!device._bosDescriptor) {
|
|
@@ -12,7 +12,6 @@ var Capability = /** @class */ (function () {
|
|
|
12
12
|
this.type = this.descriptor.bDevCapabilityType;
|
|
13
13
|
this.data = this.descriptor.dev_capability_data;
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
}());
|
|
15
|
+
}
|
|
17
16
|
exports.Capability = Capability;
|
|
18
17
|
//# sourceMappingURL=capability.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"capability.js","sourceRoot":"","sources":["../../tsc/usb/capability.ts"],"names":[],"mappings":";;;AAGA;
|
|
1
|
+
{"version":3,"file":"capability.js","sourceRoot":"","sources":["../../tsc/usb/capability.ts"],"names":[],"mappings":";;;AAGA,MAAa,UAAU;IAUnB,YAAsB,MAAc,EAAY,EAAU;QAApC,WAAM,GAAN,MAAM,CAAQ;QAAY,OAAE,GAAF,EAAE,CAAQ;QACtD,IAAI,CAAC,MAAM,CAAC,cAAc,EAAE;YACxB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;SAC9C;QACD,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,cAAc,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC9D,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,kBAAkB,CAAC;QAC/C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,mBAAmB,CAAC;IACpD,CAAC;CACJ;AAlBD,gCAkBC"}
|
package/dist/usb/device.js
CHANGED
|
@@ -1,101 +1,84 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ExtendedDevice = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
const usb = require("./bindings");
|
|
5
|
+
const interface_1 = require("./interface");
|
|
6
|
+
const capability_1 = require("./capability");
|
|
7
|
+
const isBuffer = (obj) => !!obj && obj instanceof Uint8Array;
|
|
8
|
+
const DEFAULT_TIMEOUT = 1000;
|
|
9
|
+
class ExtendedDevice {
|
|
10
|
+
constructor() {
|
|
11
11
|
this._timeout = DEFAULT_TIMEOUT;
|
|
12
12
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return
|
|
33
|
-
}
|
|
34
|
-
catch (e) {
|
|
35
|
-
// Check descriptor exists
|
|
36
|
-
if (e.errno === usb.LIBUSB_ERROR_NOT_FOUND) {
|
|
37
|
-
return undefined;
|
|
38
|
-
}
|
|
39
|
-
throw e;
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
enumerable: false,
|
|
43
|
-
configurable: true
|
|
44
|
-
});
|
|
45
|
-
Object.defineProperty(ExtendedDevice.prototype, "allConfigDescriptors", {
|
|
46
|
-
/**
|
|
47
|
-
* Contains all config descriptors of the device (same structure as .configDescriptor above)
|
|
48
|
-
*/
|
|
49
|
-
get: function () {
|
|
50
|
-
try {
|
|
51
|
-
return this.__getAllConfigDescriptors();
|
|
13
|
+
/**
|
|
14
|
+
* Timeout in milliseconds to use for control transfers.
|
|
15
|
+
*/
|
|
16
|
+
get timeout() {
|
|
17
|
+
return this._timeout || DEFAULT_TIMEOUT;
|
|
18
|
+
}
|
|
19
|
+
set timeout(value) {
|
|
20
|
+
this._timeout = value;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Object with properties for the fields of the active configuration descriptor.
|
|
24
|
+
*/
|
|
25
|
+
get configDescriptor() {
|
|
26
|
+
try {
|
|
27
|
+
return this.__getConfigDescriptor();
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
// Check descriptor exists
|
|
31
|
+
if (e.errno === usb.LIBUSB_ERROR_NOT_FOUND) {
|
|
32
|
+
return undefined;
|
|
52
33
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
34
|
+
throw e;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Contains all config descriptors of the device (same structure as .configDescriptor above)
|
|
39
|
+
*/
|
|
40
|
+
get allConfigDescriptors() {
|
|
41
|
+
try {
|
|
42
|
+
return this.__getAllConfigDescriptors();
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
// Check descriptors exist
|
|
46
|
+
if (e.errno === usb.LIBUSB_ERROR_NOT_FOUND) {
|
|
47
|
+
return [];
|
|
59
48
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return this.__getParent();
|
|
70
|
-
},
|
|
71
|
-
enumerable: false,
|
|
72
|
-
configurable: true
|
|
73
|
-
});
|
|
49
|
+
throw e;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Contains the parent of the device, such as a hub. If there is no parent this property is set to `null`.
|
|
54
|
+
*/
|
|
55
|
+
get parent() {
|
|
56
|
+
return this.__getParent();
|
|
57
|
+
}
|
|
74
58
|
/**
|
|
75
59
|
* Open the device.
|
|
76
60
|
* @param defaultConfig
|
|
77
61
|
*/
|
|
78
|
-
|
|
79
|
-
if (defaultConfig === void 0) { defaultConfig = true; }
|
|
62
|
+
open(defaultConfig = true) {
|
|
80
63
|
this.__open();
|
|
81
64
|
if (defaultConfig === false) {
|
|
82
65
|
return;
|
|
83
66
|
}
|
|
84
67
|
this.interfaces = [];
|
|
85
|
-
|
|
86
|
-
for (
|
|
68
|
+
const len = this.configDescriptor ? this.configDescriptor.interfaces.length : 0;
|
|
69
|
+
for (let i = 0; i < len; i++) {
|
|
87
70
|
this.interfaces[i] = new interface_1.Interface(this, i);
|
|
88
71
|
}
|
|
89
|
-
}
|
|
72
|
+
}
|
|
90
73
|
/**
|
|
91
74
|
* Close the device.
|
|
92
75
|
*
|
|
93
76
|
* The device must be open to use this method.
|
|
94
77
|
*/
|
|
95
|
-
|
|
78
|
+
close() {
|
|
96
79
|
this.__close();
|
|
97
80
|
this.interfaces = undefined;
|
|
98
|
-
}
|
|
81
|
+
}
|
|
99
82
|
/**
|
|
100
83
|
* 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),
|
|
101
84
|
* then before claiming an interface, call this method.
|
|
@@ -104,21 +87,20 @@ var ExtendedDevice = /** @class */ (function () {
|
|
|
104
87
|
* @param desired
|
|
105
88
|
* @param callback
|
|
106
89
|
*/
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
this.__setConfiguration(desired, function (error) {
|
|
90
|
+
setConfiguration(desired, callback) {
|
|
91
|
+
this.__setConfiguration(desired, error => {
|
|
110
92
|
if (!error) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
for (
|
|
114
|
-
|
|
93
|
+
this.interfaces = [];
|
|
94
|
+
const len = this.configDescriptor ? this.configDescriptor.interfaces.length : 0;
|
|
95
|
+
for (let i = 0; i < len; i++) {
|
|
96
|
+
this.interfaces[i] = new interface_1.Interface(this, i);
|
|
115
97
|
}
|
|
116
98
|
}
|
|
117
99
|
if (callback) {
|
|
118
|
-
callback.call(
|
|
100
|
+
callback.call(this, error);
|
|
119
101
|
}
|
|
120
102
|
});
|
|
121
|
-
}
|
|
103
|
+
}
|
|
122
104
|
/**
|
|
123
105
|
* Perform a control transfer with `libusb_control_transfer`.
|
|
124
106
|
*
|
|
@@ -134,12 +116,11 @@ var ExtendedDevice = /** @class */ (function () {
|
|
|
134
116
|
* @param data_or_length
|
|
135
117
|
* @param callback
|
|
136
118
|
*/
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
var wLength = isIn ? data_or_length : data_or_length.length;
|
|
119
|
+
controlTransfer(bmRequestType, bRequest, wValue, wIndex, data_or_length, callback) {
|
|
120
|
+
const isIn = !!(bmRequestType & usb.LIBUSB_ENDPOINT_IN);
|
|
121
|
+
const wLength = isIn ? data_or_length : data_or_length.length;
|
|
141
122
|
if (isIn) {
|
|
142
|
-
if (
|
|
123
|
+
if (wLength < 0) {
|
|
143
124
|
throw new TypeError('Expected size number for IN transfer (based on bmRequestType)');
|
|
144
125
|
}
|
|
145
126
|
}
|
|
@@ -150,7 +131,7 @@ var ExtendedDevice = /** @class */ (function () {
|
|
|
150
131
|
}
|
|
151
132
|
// Buffer for the setup packet
|
|
152
133
|
// http://libusbx.sourceforge.net/api-1.0/structlibusb__control__setup.html
|
|
153
|
-
|
|
134
|
+
const buf = Buffer.alloc(wLength + usb.LIBUSB_CONTROL_SETUP_SIZE);
|
|
154
135
|
buf.writeUInt8(bmRequestType, 0);
|
|
155
136
|
buf.writeUInt8(bRequest, 1);
|
|
156
137
|
buf.writeUInt16LE(wValue, 2);
|
|
@@ -159,13 +140,13 @@ var ExtendedDevice = /** @class */ (function () {
|
|
|
159
140
|
if (!isIn) {
|
|
160
141
|
buf.set(data_or_length, usb.LIBUSB_CONTROL_SETUP_SIZE);
|
|
161
142
|
}
|
|
162
|
-
|
|
143
|
+
const transfer = new usb.Transfer(this, 0, usb.LIBUSB_TRANSFER_TYPE_CONTROL, this.timeout, (error, buf, actual) => {
|
|
163
144
|
if (callback) {
|
|
164
145
|
if (isIn) {
|
|
165
|
-
callback.call(
|
|
146
|
+
callback.call(this, error, buf.slice(usb.LIBUSB_CONTROL_SETUP_SIZE, usb.LIBUSB_CONTROL_SETUP_SIZE + actual));
|
|
166
147
|
}
|
|
167
148
|
else {
|
|
168
|
-
callback.call(
|
|
149
|
+
callback.call(this, error, actual);
|
|
169
150
|
}
|
|
170
151
|
}
|
|
171
152
|
});
|
|
@@ -174,29 +155,29 @@ var ExtendedDevice = /** @class */ (function () {
|
|
|
174
155
|
}
|
|
175
156
|
catch (e) {
|
|
176
157
|
if (callback) {
|
|
177
|
-
process.nextTick(
|
|
158
|
+
process.nextTick(() => callback.call(this, e, undefined));
|
|
178
159
|
}
|
|
179
160
|
}
|
|
180
161
|
return this;
|
|
181
|
-
}
|
|
162
|
+
}
|
|
182
163
|
/**
|
|
183
164
|
* Return the interface with the specified interface number.
|
|
184
165
|
*
|
|
185
166
|
* The device must be open to use this method.
|
|
186
167
|
* @param addr
|
|
187
168
|
*/
|
|
188
|
-
|
|
169
|
+
interface(addr) {
|
|
189
170
|
if (!this.interfaces) {
|
|
190
171
|
throw new Error('Device must be open before searching for interfaces');
|
|
191
172
|
}
|
|
192
173
|
addr = addr || 0;
|
|
193
|
-
for (
|
|
174
|
+
for (let i = 0; i < this.interfaces.length; i++) {
|
|
194
175
|
if (this.interfaces[i].interfaceNumber === addr) {
|
|
195
176
|
return this.interfaces[i];
|
|
196
177
|
}
|
|
197
178
|
}
|
|
198
|
-
throw new Error(
|
|
199
|
-
}
|
|
179
|
+
throw new Error(`Interface not found for address: ${addr}`);
|
|
180
|
+
}
|
|
200
181
|
/**
|
|
201
182
|
* Perform a control transfer to retrieve a string descriptor
|
|
202
183
|
*
|
|
@@ -204,29 +185,28 @@ var ExtendedDevice = /** @class */ (function () {
|
|
|
204
185
|
* @param desc_index
|
|
205
186
|
* @param callback
|
|
206
187
|
*/
|
|
207
|
-
|
|
188
|
+
getStringDescriptor(desc_index, callback) {
|
|
208
189
|
// Index 0 indicates null
|
|
209
190
|
if (desc_index === 0) {
|
|
210
191
|
callback();
|
|
211
192
|
return;
|
|
212
193
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
this.controlTransfer(usb.LIBUSB_ENDPOINT_IN, usb.LIBUSB_REQUEST_GET_DESCRIPTOR, ((usb.LIBUSB_DT_STRING << 8) | desc_index), langid, length,
|
|
194
|
+
const langid = 0x0409;
|
|
195
|
+
const length = 255;
|
|
196
|
+
this.controlTransfer(usb.LIBUSB_ENDPOINT_IN, usb.LIBUSB_REQUEST_GET_DESCRIPTOR, ((usb.LIBUSB_DT_STRING << 8) | desc_index), langid, length, (error, buffer) => {
|
|
216
197
|
if (error) {
|
|
217
198
|
return callback(error);
|
|
218
199
|
}
|
|
219
200
|
callback(undefined, isBuffer(buffer) ? buffer.toString('utf16le', 2) : undefined);
|
|
220
201
|
});
|
|
221
|
-
}
|
|
202
|
+
}
|
|
222
203
|
/**
|
|
223
204
|
* Perform a control transfer to retrieve an object with properties for the fields of the Binary Object Store descriptor.
|
|
224
205
|
*
|
|
225
206
|
* The device must be open to use this method.
|
|
226
207
|
* @param callback
|
|
227
208
|
*/
|
|
228
|
-
|
|
229
|
-
var _this = this;
|
|
209
|
+
getBosDescriptor(callback) {
|
|
230
210
|
if (this._bosDescriptor) {
|
|
231
211
|
// Cached descriptor
|
|
232
212
|
return callback(undefined, this._bosDescriptor);
|
|
@@ -235,7 +215,7 @@ var ExtendedDevice = /** @class */ (function () {
|
|
|
235
215
|
// BOS is only supported from USB 2.0.1
|
|
236
216
|
return callback(undefined, undefined);
|
|
237
217
|
}
|
|
238
|
-
this.controlTransfer(usb.LIBUSB_ENDPOINT_IN, usb.LIBUSB_REQUEST_GET_DESCRIPTOR, (usb.LIBUSB_DT_BOS << 8), 0, usb.LIBUSB_DT_BOS_SIZE,
|
|
218
|
+
this.controlTransfer(usb.LIBUSB_ENDPOINT_IN, usb.LIBUSB_REQUEST_GET_DESCRIPTOR, (usb.LIBUSB_DT_BOS << 8), 0, usb.LIBUSB_DT_BOS_SIZE, (error, buffer) => {
|
|
239
219
|
if (error) {
|
|
240
220
|
// Check BOS descriptor exists
|
|
241
221
|
if (error.errno === usb.LIBUSB_TRANSFER_STALL)
|
|
@@ -245,8 +225,8 @@ var ExtendedDevice = /** @class */ (function () {
|
|
|
245
225
|
if (!isBuffer(buffer)) {
|
|
246
226
|
return callback(undefined, undefined);
|
|
247
227
|
}
|
|
248
|
-
|
|
249
|
-
|
|
228
|
+
const totalLength = buffer.readUInt16LE(2);
|
|
229
|
+
this.controlTransfer(usb.LIBUSB_ENDPOINT_IN, usb.LIBUSB_REQUEST_GET_DESCRIPTOR, (usb.LIBUSB_DT_BOS << 8), 0, totalLength, (error, buffer) => {
|
|
250
230
|
if (error) {
|
|
251
231
|
// Check BOS descriptor exists
|
|
252
232
|
if (error.errno === usb.LIBUSB_TRANSFER_STALL)
|
|
@@ -256,16 +236,16 @@ var ExtendedDevice = /** @class */ (function () {
|
|
|
256
236
|
if (!isBuffer(buffer)) {
|
|
257
237
|
return callback(undefined, undefined);
|
|
258
238
|
}
|
|
259
|
-
|
|
239
|
+
const descriptor = {
|
|
260
240
|
bLength: buffer.readUInt8(0),
|
|
261
241
|
bDescriptorType: buffer.readUInt8(1),
|
|
262
242
|
wTotalLength: buffer.readUInt16LE(2),
|
|
263
243
|
bNumDeviceCaps: buffer.readUInt8(4),
|
|
264
244
|
capabilities: []
|
|
265
245
|
};
|
|
266
|
-
|
|
246
|
+
let i = usb.LIBUSB_DT_BOS_SIZE;
|
|
267
247
|
while (i < descriptor.wTotalLength) {
|
|
268
|
-
|
|
248
|
+
const capability = {
|
|
269
249
|
bLength: buffer.readUInt8(i + 0),
|
|
270
250
|
bDescriptorType: buffer.readUInt8(i + 1),
|
|
271
251
|
bDevCapabilityType: buffer.readUInt8(i + 2),
|
|
@@ -275,31 +255,29 @@ var ExtendedDevice = /** @class */ (function () {
|
|
|
275
255
|
i += capability.bLength;
|
|
276
256
|
}
|
|
277
257
|
// Cache descriptor
|
|
278
|
-
|
|
279
|
-
callback(undefined,
|
|
258
|
+
this._bosDescriptor = descriptor;
|
|
259
|
+
callback(undefined, this._bosDescriptor);
|
|
280
260
|
});
|
|
281
261
|
});
|
|
282
|
-
}
|
|
262
|
+
}
|
|
283
263
|
/**
|
|
284
264
|
* Retrieve a list of Capability objects for the Binary Object Store capabilities of the device.
|
|
285
265
|
*
|
|
286
266
|
* The device must be open to use this method.
|
|
287
267
|
* @param callback
|
|
288
268
|
*/
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
this.getBosDescriptor(function (error, descriptor) {
|
|
269
|
+
getCapabilities(callback) {
|
|
270
|
+
const capabilities = [];
|
|
271
|
+
this.getBosDescriptor((error, descriptor) => {
|
|
293
272
|
if (error)
|
|
294
273
|
return callback(error, undefined);
|
|
295
|
-
|
|
296
|
-
for (
|
|
297
|
-
capabilities.push(new capability_1.Capability(
|
|
274
|
+
const len = descriptor ? descriptor.capabilities.length : 0;
|
|
275
|
+
for (let i = 0; i < len; i++) {
|
|
276
|
+
capabilities.push(new capability_1.Capability(this, i));
|
|
298
277
|
}
|
|
299
278
|
callback(undefined, capabilities);
|
|
300
279
|
});
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
}());
|
|
280
|
+
}
|
|
281
|
+
}
|
|
304
282
|
exports.ExtendedDevice = ExtendedDevice;
|
|
305
283
|
//# sourceMappingURL=device.js.map
|