diff --git a/lib/usb.js b/lib/usb.js index f05e5d8..80ed93c 100644 --- a/lib/usb.js +++ b/lib/usb.js @@ -2,7 +2,7 @@ const events = require('events'); const util = require('util'); const debug = require('debug')('hci-usb'); -const usb = require('usb'); +const { usb, findByIds, getDeviceList } = require('usb'); const HCI_COMMAND_PKT = 0x01; const HCI_ACLDATA_PKT = 0x02; @@ -67,9 +67,7 @@ BluetoothHciSocket.prototype.bindUser = function (devId, params) { this._usbDevice = this._findUsbDevice(devId, usbParams); } } else { - this._usbDevice = VENDOR_DEVICE_LIST - .map(d => usb.findByIds(d.vid, d.pid)) - .find(d => d != null); + this._usbDevice = VENDOR_DEVICE_LIST.map((d) => findByIds(d.vid, d.pid)).find((d) => d != null); } if (!this._usbDevice) { @@ -131,7 +129,7 @@ BluetoothHciSocket.prototype._getUsbParams = function (params) { }; BluetoothHciSocket.prototype._findUsbDevice = function (devId, usbParams) { - const usbDevices = usb.getDeviceList(); + const usbDevices = getDeviceList(); for (let i = 0; i < usbDevices.length; i++) { const usbDevice = usbDevices[i]; @@ -158,7 +156,7 @@ BluetoothHciSocket.prototype._findUsbDevice = function (devId, usbParams) { }; BluetoothHciSocket.prototype.getDeviceList = function () { - return usb.getDeviceList() + return getDeviceList() .filter(dev => { return VENDOR_DEVICE_LIST.findIndex(d => { return dev.deviceDescriptor.idVendor === d.vid && dev.deviceDescriptor.idProduct === d.pid; @@ -185,11 +183,21 @@ BluetoothHciSocket.prototype.isDevUp = function () { BluetoothHciSocket.prototype.start = function () { if (this._mode === 'raw' || this._mode === 'user') { process.on('exit', this._exitHandler); - + + this._hciEventEndpoint.removeAllListeners(); this._hciEventEndpoint.on('data', this.onHciEventEndpointData.bind(this)); + this._hciEventEndpoint.on('error', (error) => { + debug('HCI event endpoint error: ' + error); + this.emit('error', error); + }); this._hciEventEndpoint.startPoll(); + this._aclDataInEndpoint.removeAllListeners(); this._aclDataInEndpoint.on('data', this.onAclDataInEndpointData.bind(this)); + this._aclDataInEndpoint.on('error', (error) => { + debug('ACL data in endpoint error: ' + error); + this.emit('error', error); + }); this._aclDataInEndpoint.startPoll(); } }; @@ -212,9 +220,26 @@ BluetoothHciSocket.prototype.write = function (data) { const type = data.readUInt8(0); if (HCI_COMMAND_PKT === type) { - this._usbDevice.controlTransfer(usb.LIBUSB_REQUEST_TYPE_CLASS | usb.LIBUSB_RECIPIENT_INTERFACE, 0, 0, 0, data.slice(1), function () {}); + this._usbDevice.controlTransfer( + usb.LIBUSB_REQUEST_TYPE_CLASS | usb.LIBUSB_RECIPIENT_INTERFACE, + 0, + 0, + 0, + data.slice(1), + (error) => { + if (error) { + debug('Control transfer failed: ' + error); + this.emit('error', error); + } + } + ); } else if (HCI_ACLDATA_PKT === type) { - this._aclDataOutEndpoint.transfer(data.slice(1)); + this._aclDataOutEndpoint.transfer(data.slice(1), (error) => { + if (error) { + debug('ACL data transfer failed: ' + error); + this.emit('error', error); + } + }); } } };