-
Notifications
You must be signed in to change notification settings - Fork 1
/
usbDevices.js
334 lines (286 loc) · 9.43 KB
/
usbDevices.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
var exports = module.exports = {};
var usb = require('usb');
var atob = require('atob');
var btoa = require('btoa');
var when = require('when');
var debug = require('debug')('eve-connector:usb');
// polling USB interfaces
var _pollingUsb = [];
var checkDeviceType = function(device)
{
if (!device)
throw ('device is undefined')
if ( device.type != 'usb' )
throw (['Device type should be "usb"', device])
if ( ! device.params )
throw (['Device params is empty', device])
if ( !device.params.vid || !device.params.pid )
throw (['You must provide VID and PID parameters for USB devices', device.params]);
}
var listDevices = function(type) {
return usb.getDeviceList();
}
var isDeviceAvailable = function(device)
{
return when.promise(function(resolve, reject){
debug('isDeviceAvailable()');
checkDeviceType(device);
var found = usb.findByIds(parseInt(device.params.vid), parseInt(device.params.pid));
var res = {
available: ( found !== undefined ),
device: device
}
resolve(res);
});
}
var areDevicesAvailable = function(type, devicesList)
{
debug('areDevicesAvailable()');
var available = { type: type, params: []};
var checks = [];
devicesList.forEach(function(d){
var device = {type: type, params:{vid: d.vid, pid: d.pid}};
var check = when.promise(function(resolve, reject){
isDeviceAvailable(device).then(
function(res){
if ( res.available )
available.params.push({vid: d.vid, pid: d.pid});
resolve(available);
},
function(err){
reject(err);
}
);
});
checks.push(check);
});
return when.all(checks);
}
var resetData = function(device, socket)
{
//usb.setDebugLevel(4);
return when.promise(function(resolve, reject){
checkDeviceType(device);
if ( isPolling(device) )
stopPoll(device);
var vid = parseInt(device.params.vid);
var pid = parseInt(device.params.pid);
var usbdev = usb.findByIds(vid, pid);
if ( usbdev === undefined) {
throw new Error('Device not available');
}
usbdev.open();
debug('Resetting device...');
usbdev.reset(function(error){
error && reject(error);
debug('Device reset');
});
});
};
var sendData = function(device, data, socket)
{
//usb.setDebugLevel(4);
return when.promise(function(resolve, reject){
checkDeviceType(device);
var wasPolling = isPolling(device);
stopPoll(device);
var vid = parseInt(device.params.vid);
var pid = parseInt(device.params.pid);
var usbdev = usb.findByIds(vid, pid);
if ( usbdev === undefined) {
throw new Error('Device not available');
}
usbdev.open();
try {
var interface = claimUsbInterface(vid, pid);
var outEp = getEndpoint(interface, 'out');
outEp.on('error', function(epError){
debug('outEp error', epError)
wasPolling && startPoll(device);
reject(epError);
});
// decode base64 data
var bin = new Buffer(atob(data.toString()), 'binary');
debug('sending data... ');
outEp.transfer(bin, function(epError, tf_data){
debug('...data sent to USB');
wasPolling && startPoll(device, socket);
epError && reject(epError);
//usbdev.close(); // keep this even if polling ???
resolve(tf_data);
});
}
catch(e){
wasPolling && startPoll(device);
reject(e);
}
});
};
var sendData_BAK = function(device, data, socket)
{
usb.setDebugLevel(4);
return when.promise(function(resolve, reject){
checkDeviceType(device);
var wasPolling = isPolling(device);
stopPoll(device);
var vid = parseInt(device.params.vid);
var pid = parseInt(device.params.pid);
var usbdev = usb.findByIds(vid, pid);
if ( usbdev === undefined) {
throw new Error('Device not available');
}
usbdev.open();
debug('Resetting device...');
usbdev.reset(function(error){
error && reject(error);
debug('Device reset');
try {
var interface = claimUsbInterface(vid, pid);
var outEp = getEndpoint(interface, 'out');
outEp.on('error', function(epError){
debug('outEp error', epError)
wasPolling && startPoll(device);
reject(epError);
});
// decode base64 data
var bin = new Buffer(atob(data.toString()), 'binary');
debug('sending data... ');
outEp.transfer(bin, function(epError, tf_data){
debug('...data sent to USB');
wasPolling && startPoll(device, socket);
epError && reject(epError);
resolve(tf_data);
});
}
catch(e){
wasPolling && startPoll(device);
reject(e);
}
});
});
};
var readData = function(device, length)
{
debug('readData()');
return when.promise(function(resolve, reject){
checkDeviceType(device);
var interface = claimUsbInterface(device.params.vid, device.params.pid);
var inEp = getEndpoint(interface, 'in');
inEp.on('error', function(error) {
debug('inEp error', error);
reject(error);
});
length = length || inEp.descriptor.wMaxPacketSize;
debug('Start reading in endpoint on device', device);
inEp.transfer(length, function(error, data){
error && reject(error);
debug('readData:', data);
// we send back base64 encoded data
resolve(data != undefined ? btoa(data) : '');
});
});
}
var isPolling = function(device) {
var polling = _pollingUsb.find(function(item){
return ( item.vid == device.params.vid && item.pid == device.params.pid );
});
return polling !== undefined;
}
var getPollingEndpoint = function(device) {
var polling = _pollingUsb.find(function(item){
return ( item.vid == device.params.vid && item.pid == device.params.pid );
});
return ( polling !== undefined ) ? polling.endpoint : null;
}
var startPoll = function(device, socket)
{
return when.promise(function(resolve, reject){
checkDeviceType(device);
if ( isPolling(device) ) {
debug('Already polling device...', device);
return resolve(true);
}
var interface = claimUsbInterface(device.params.vid, device.params.pid);
var inEp = getEndpoint(interface, 'in');
inEp.on('error', function(error) {
debug('inEp polling error', error);
});
inEp.on('end', function(error) {
debug('inEp polling ended');
var polling = _pollingUsb.find(function(item){
return ( item.endpoint == inEp );
});
if ( polling !== undefined )
_pollingUsb.splice(_pollingUsb.indexOf(polling), 1);
inEp.removeListener('data');
});
inEp.on('data', function(data) {
if ( data && data.length ) {
debug('inEp data received:', data, data.length, device);
socket.emit('usbPoll', btoa(data));
}
});
debug('Start polling device...', device);
inEp.startPoll();
_pollingUsb.push({
vid: device.params.vid,
pid: device.params.pid,
endpoint: inEp
});
resolve(true);
});
}
var stopPoll = function(device)
{
checkDeviceType(device);
var inEndpoint = getPollingEndpoint(device);
if ( !inEndpoint ) {
debug('Was not polling device...', device);
return;
}
inEndpoint.stopPoll();
}
var claimUsbInterface = function(vid, pid)
{
vid = parseInt(vid);
pid = parseInt(pid);
var usbdev = usb.findByIds(vid, pid);
if ( usbdev === undefined) {
throw new Error('Device not available');
}
usbdev.open();
// we use the first interface
// TODO: implement interface number
var interface = usbdev.interface(0);
if ( process.platform == 'linux' && interface.isKernelDriverActive() )
interface.detachKernelDriver();
interface.claim();
return interface;
}
var getEndpoint = function(interface, direction)
{
var endpoint = interface.endpoints.find(function(ep){
return ep.direction === direction;
});
if (endpoint == undefined)
throw new Error(direction + ' enpoint not found on interface');
return endpoint;
}
var test = function(device)
{
debug('test');
startPoll(device);
setTimeout(function () {
//throw(['this is the error']);
//stopPoll(device);
}, 1000);
}
exports.isDeviceAvailable = isDeviceAvailable;
exports.areDevicesAvailable = areDevicesAvailable;
exports.listDevices = listDevices;
exports.sendData = sendData;
exports.startPoll = startPoll;
exports.stopPoll = stopPoll;
exports.readData = readData;
exports.resetData = resetData;
exports.test = test;