forked from noble/node-bluetooth-hci-socket
-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathusb.js
306 lines (242 loc) · 8.52 KB
/
usb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
var events = require('events');
var util = require('util');
var debug = require('debug')('hci-usb');
var usb = require('usb');
var HCI_COMMAND_PKT = 0x01;
var HCI_ACLDATA_PKT = 0x02;
var HCI_EVENT_PKT = 0x04;
var OGF_HOST_CTL = 0x03;
var OCF_RESET = 0x0003;
var VENDOR_DEVICE_LIST = [
{vid: 0x0CF3, pid: 0xE300 }, // Qualcomm Atheros QCA61x4
{vid: 0x0a5c, pid: 0x21e8 }, // Broadcom BCM20702A0
{vid: 0x0a5c, pid: 0x21f1 }, // Broadcom BCM20702A0
{vid: 0x19ff, pid: 0x0239 }, // Broadcom BCM20702A0
{vid: 0x413c, pid: 0x8143 }, // Broadcom BCM20702A0
{vid: 0x0a12, pid: 0x0001 }, // CSR
{vid: 0x0b05, pid: 0x17cb }, // ASUS BT400
{vid: 0x8087, pid: 0x07da }, // Intel 6235
{vid: 0x8087, pid: 0x07dc }, // Intel 7260
{vid: 0x8087, pid: 0x0a2a }, // Intel 7265
{vid: 0x8087, pid: 0x0a2b }, // Intel 8265
{vid: 0x0489, pid: 0xe07a }, // Broadcom BCM20702A1
{vid: 0x0a5c, pid: 0x6412 }, // Broadcom BCM2045A0
{vid: 0x050D, pid: 0x065A }, // Belkin BCM20702A0
{vid: 0x1286, pid: 0x204C }, // Marvell AVASTAR
{vid: 0x8087, pid: 0x0025 }, // Dell Precision 5530
];
function BluetoothHciSocket() {
this._isUp = false;
this._hciEventEndpointBuffer = Buffer.alloc(0);
this._aclDataInEndpointBuffer = Buffer.alloc(0);
}
util.inherits(BluetoothHciSocket, events.EventEmitter);
BluetoothHciSocket.prototype.setFilter = function(filter) {
// no-op
};
BluetoothHciSocket.prototype.bindRaw = function(devId, params) {
this.bindUser(devId, params);
this._mode = 'raw';
this.reset();
};
BluetoothHciSocket.prototype.bindUser = function(devId, params) {
this._mode = 'user';
var usbParams = this._getUsbParams(params);
if (Number.isInteger(usbParams.usb.vid) && Number.isInteger(usbParams.usb.pid)) {
debug('using USB VID = ' + usbParams.usb.vid + ', PID = ' + usbParams.usb.pid);
if (Number.isInteger(usbParams.usb.bus) && Number.isInteger(usbParams.usb.address)) {
debug('using USB BUS = ' + usbParams.usb.bus + ', Address = ' + usbParams.usb.address);
this._usbDevice = this._findUsbDevice(0, usbParams);
} else {
this._usbDevice = this._findUsbDevice(devId, usbParams);
}
} else {
this._usbDevice = VENDOR_DEVICE_LIST
.map(d => usb.findByIds(d.vid, d.pid))
.find(d => d != null);
}
if (!this._usbDevice) {
throw new Error('No compatible USB Bluetooth 4.0 device found!');
}
this._usbDevice.open();
this._usbDeviceInterface = this._usbDevice.interfaces[0];
this._aclDataOutEndpoint = this._usbDeviceInterface.endpoint(0x02);
if (this._aclDataOutEndpoint === undefined) this._aclDataOutEndpoint = this._usbDeviceInterface.endpoint(0x01);
this._hciEventEndpoint = this._usbDeviceInterface.endpoint(0x81);
this._aclDataInEndpoint = this._usbDeviceInterface.endpoint(0x82);
this._usbDeviceInterface.claim();
};
BluetoothHciSocket.prototype._getUsbParams = function(params) {
var usbParams = {
usb: {
vid: undefined, pid: undefined,
bus: undefined, address: undefined,
},
};
if (process.env.BLUETOOTH_HCI_SOCKET_USB_VID) {
usbParams.usb.vid = parseInt(process.env.BLUETOOTH_HCI_SOCKET_USB_VID, 10);
}
if (process.env.BLUETOOTH_HCI_SOCKET_USB_PID) {
usbParams.usb.pid = parseInt(process.env.BLUETOOTH_HCI_SOCKET_USB_PID, 10);
}
if (process.env.BLUETOOTH_HCI_SOCKET_USB_BUS) {
usbParams.usb.bus = parseInt(process.env.BLUETOOTH_HCI_SOCKET_USB_BUS, 10);
}
if (process.env.BLUETOOTH_HCI_SOCKET_USB_ADDRESS) {
usbParams.usb.address = parseInt(process.env.BLUETOOTH_HCI_SOCKET_USB_ADDRESS, 10);
}
if (params && params.usb) {
if (Number.isInteger(params.usb.vid)) {
usbParams.usb.vid = params.usb.vid;
}
if (Number.isInteger(params.usb.pid)) {
usbParams.usb.pid = params.usb.pid;
}
if (Number.isInteger(params.usb.bus)) {
usbParams.usb.bus = params.usb.bus;
}
if (Number.isInteger(params.usb.address)) {
usbParams.usb.address = params.usb.address;
}
}
return usbParams;
};
BluetoothHciSocket.prototype._findUsbDevice = function(devId, usbParams) {
var usbDevices = usb.getDeviceList();
for (var i = 0; i < usbDevices.length; i++) {
var usbDevice = usbDevices[i];
var usbDeviceDesc = usbDevice.deviceDescriptor;
if (Number.isInteger(usbParams.usb.vid) && usbDeviceDesc.idVendor !== usbParams.usb.vid) {
continue;
}
if (Number.isInteger(usbParams.usb.pid) && usbDeviceDesc.idProduct !== usbParams.usb.pid) {
continue;
}
if (Number.isInteger(usbParams.usb.bus) && usbDevice.bus !== usbParams.usb.bus) {
continue;
}
if (Number.isInteger(usbParams.usb.address) && usbDevice.address !== usbParams.usb.address) {
continue;
}
if (--devId > 0) {
continue;
}
return usbDevices[i];
}
};
BluetoothHciSocket.prototype.getDeviceList = function() {
return usb.getDeviceList()
.filter(dev => {
return VENDOR_DEVICE_LIST.findIndex(d => {
return dev.deviceDescriptor.idVendor == d.vid && dev.deviceDescriptor.idProduct == d.pid;
}) !== -1;
})
.map(dev => ({
"devId": null,
"devUp": null,
"idVendor": dev.deviceDescriptor.idVendor,
"idProduct": dev.deviceDescriptor.idProduct,
"busNumber": dev.busNumber,
"deviceAddress": dev.deviceAddress,
}));
};
BluetoothHciSocket.prototype.bindControl = function() {
this._mode = 'control';
};
BluetoothHciSocket.prototype.isDevUp = function() {
return this._isUp;
};
BluetoothHciSocket.prototype.start = function() {
if (this._mode === 'raw' || this._mode === 'user') {
this._hciEventEndpoint.on('data', this.onHciEventEndpointData.bind(this));
this._hciEventEndpoint.startPoll();
this._aclDataInEndpoint.on('data', this.onAclDataInEndpointData.bind(this));
this._aclDataInEndpoint.startPoll();
}
};
BluetoothHciSocket.prototype.stop = function() {
if (this._mode === 'raw' || this._mode === 'user') {
this._hciEventEndpoint.stopPoll();
this._hciEventEndpoint.removeAllListeners();
this._aclDataInEndpoint.stopPoll();
this._aclDataInEndpoint.removeAllListeners();
}
};
BluetoothHciSocket.prototype.write = function(data) {
debug('write: ' + data.toString('hex'));
if (this._mode === 'raw' || this._mode === 'user') {
var 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() {});
} else if(HCI_ACLDATA_PKT === type) {
this._aclDataOutEndpoint.transfer(data.slice(1));
}
}
};
BluetoothHciSocket.prototype.onHciEventEndpointData = function(data) {
debug('HCI event: ' + data.toString('hex'));
if (data.length === 0) {
return;
}
// add to buffer
this._hciEventEndpointBuffer = Buffer.concat([
this._hciEventEndpointBuffer,
data
]);
if (this._hciEventEndpointBuffer.length < 2) {
return;
}
// check if desired length
var pktLen = this._hciEventEndpointBuffer.readUInt8(1);
if (pktLen <= (this._hciEventEndpointBuffer.length - 2)) {
var buf = this._hciEventEndpointBuffer.slice(0, pktLen + 2);
if (this._mode === 'raw' && buf.length === 6 && ('0e0401030c00' === buf.toString('hex') || '0e0402030c00' === buf.toString('hex'))) {
debug('reset complete');
this._isUp = true;
}
// fire event
this.emit('data', Buffer.concat([
Buffer.from([HCI_EVENT_PKT]),
buf
]));
// reset buffer
this._hciEventEndpointBuffer = this._hciEventEndpointBuffer.slice(pktLen + 2);
}
};
BluetoothHciSocket.prototype.onAclDataInEndpointData = function(data) {
debug('ACL Data In: ' + data.toString('hex'));
if (data.length === 0) {
return;
}
// add to buffer
this._aclDataInEndpointBuffer = Buffer.concat([
this._aclDataInEndpointBuffer,
data
]);
if (this._aclDataInEndpointBuffer.length < 4) {
return;
}
// check if desired length
var pktLen = this._aclDataInEndpointBuffer.readUInt16LE(2);
if (pktLen <= (this._aclDataInEndpointBuffer.length - 4)) {
var buf = this._aclDataInEndpointBuffer.slice(0, pktLen + 4);
// fire event
this.emit('data', Buffer.concat([
Buffer.from([HCI_ACLDATA_PKT]),
buf
]));
// reset buffer
this._aclDataInEndpointBuffer = this._aclDataInEndpointBuffer.slice(pktLen + 4);
}
};
BluetoothHciSocket.prototype.reset = function() {
var cmd = Buffer.alloc(4);
// header
cmd.writeUInt8(HCI_COMMAND_PKT, 0);
cmd.writeUInt16LE(OCF_RESET | OGF_HOST_CTL << 10, 1);
// length
cmd.writeUInt8(0x00, 3);
debug('reset');
this.write(cmd);
};
module.exports = BluetoothHciSocket;