Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const CHECK_LIBUSB_REQUEST_SET_CONFIGURATION: number = usb.LIBUSB_REQUEST_SET_CONFIGURATION;
const CHECK_LIBUSB_REQUEST_GET_INTERFACE: number = usb.LIBUSB_REQUEST_GET_INTERFACE;
const CHECK_LIBUSB_REQUEST_SET_INTERFACE: number = usb.LIBUSB_REQUEST_SET_INTERFACE;
const CHECK_LIBUSB_REQUEST_SYNCH_FRAME: number = usb.LIBUSB_REQUEST_SYNCH_FRAME;
// libusb_descriptor_type
const CHECK_LIBUSB_DT_DEVICE: number = usb.LIBUSB_DT_DEVICE;
const CHECK_LIBUSB_DT_CONFIG: number = usb.LIBUSB_DT_CONFIG;
const CHECK_LIBUSB_DT_STRING: number = usb.LIBUSB_DT_STRING;
const CHECK_LIBUSB_DT_INTERFACE: number = usb.LIBUSB_DT_INTERFACE;
const CHECK_LIBUSB_DT_ENDPOINT: number = usb.LIBUSB_DT_ENDPOINT;
const CHECK_LIBUSB_DT_HID: number = usb.LIBUSB_DT_HID;
const CHECK_LIBUSB_DT_REPORT: number = usb.LIBUSB_DT_REPORT;
const CHECK_LIBUSB_DT_PHYSICAL: number = usb.LIBUSB_DT_PHYSICAL;
const CHECK_LIBUSB_DT_HUB: number = usb.LIBUSB_DT_HUB;
// libusb_endpoint_direction
const CHECK_LIBUSB_ENDPOINT_IN: number = usb.LIBUSB_ENDPOINT_IN;
const CHECK_LIBUSB_ENDPOINT_OUT: number = usb.LIBUSB_ENDPOINT_OUT;
// libusb_transfer_type
const CHECK_LIBUSB_TRANSFER_TYPE_CONTROL: number = usb.LIBUSB_TRANSFER_TYPE_CONTROL;
const CHECK_LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: number = usb.LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
const CHECK_LIBUSB_TRANSFER_TYPE_BULK: number = usb.LIBUSB_TRANSFER_TYPE_BULK;
const CHECK_LIBUSB_TRANSFER_TYPE_INTERRUPT: number = usb.LIBUSB_TRANSFER_TYPE_INTERRUPT;
// libusb_iso_sync_type
const CHECK_LIBUSB_ISO_SYNC_TYPE_NONE: number = usb.LIBUSB_ISO_SYNC_TYPE_NONE;
const CHECK_LIBUSB_ISO_SYNC_TYPE_ASYNC: number = usb.LIBUSB_ISO_SYNC_TYPE_ASYNC;
const CHECK_LIBUSB_ISO_SYNC_TYPE_ADAPTIVE: number = usb.LIBUSB_ISO_SYNC_TYPE_ADAPTIVE;
const CHECK_LIBUSB_ISO_SYNC_TYPE_SYNC: number = usb.LIBUSB_ISO_SYNC_TYPE_SYNC;
// libusb_iso_usage_type
const CHECK_LIBUSB_ISO_USAGE_TYPE_DATA: number = usb.LIBUSB_ISO_USAGE_TYPE_DATA;
const CHECK_LIBUSB_ISO_USAGE_TYPE_FEEDBACK: number = usb.LIBUSB_ISO_USAGE_TYPE_FEEDBACK;
const CHECK_LIBUSB_ISO_USAGE_TYPE_IMPLICIT: number = usb.LIBUSB_ISO_USAGE_TYPE_IMPLICIT;
// libusb_transfer_status
static async getSerialNumber (device) {
let langId = 0x0409;
let length = 255;
let deviceDescriptor = device.deviceDescriptor;
let controlTransferAsync = Promise.promisify(device.controlTransfer, device);
let serialNumber = await controlTransferAsync(usb.LIBUSB_ENDPOINT_IN
, usb.LIBUSB_REQUEST_GET_DESCRIPTOR
, (usb.LIBUSB_DT_STRING << 8) | deviceDescriptor.iSerialNumber
, langId
, length);
console.log("serialNumber: ", serialNumber.toString());
return serialNumber.toString('utf16le', 2);
}
return new Promise((resolve, reject) => {
let requestType = 0;
let lengthOrData = ti.length ? ti.length : Buffer.from(ti.data);
if (ti.requestType === 'vendor') {
requestType |= usb.LIBUSB_REQUEST_TYPE_VENDOR;
}
if (ti.recipient === 'device') {
requestType |= usb.LIBUSB_RECIPIENT_DEVICE;
}
if (ti.direction === 'out') {
requestType |= usb.LIBUSB_ENDPOINT_OUT;
} else if (ti.direction === 'in') {
requestType |= usb.LIBUSB_ENDPOINT_IN;
}
this._device.controlTransfer(requestType, ti.request, ti.value, ti.index, lengthOrData, (err, data) => {
if (err) {
return reject(err);
}
if (data) {
return resolve(Uint8Array.from(data).buffer);
}
resolve();
});
});
};