-
Notifications
You must be signed in to change notification settings - Fork 2
/
audiomoth-hid.js
executable file
·375 lines (212 loc) · 9.84 KB
/
audiomoth-hid.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/****************************************************************************
* audiomoth.js
* openacousticdevices.info
* June 2017
*****************************************************************************/
'use strict';
/*jslint bitwise: true, nomen: true*/
const fs = require('fs');
const path = require('path');
const child_process = require('child_process');
const USB_MSG_TYPE_GET_TIME = 0x01;
const USB_MSG_TYPE_SET_TIME = 0x02;
const USB_MSG_TYPE_GET_UID = 0x03;
const USB_MSG_TYPE_GET_BATTERY = 0x04;
const USB_MSG_TYPE_GET_APP_PACKET = 0x05;
const USB_MSG_TYPE_SET_APP_PACKET = 0x06;
const USB_MSG_TYPE_GET_FIRMWARE_VERSION = 0x07;
const USB_MSG_TYPE_GET_FIRMWARE_DESCRIPTION = 0x08;
const USB_MSG_TYPE_QUERY_SERIAL_BOOTLOADER = 0x09;
const USB_MSG_TYPE_ENTER_SERIAL_BOOTLOADER = 0x0A;
const USB_MSG_TYPE_QUERY_USBHID_BOOTLOADER = 0x0B
const USB_MSG_TYPE_ENTER_USBHID_BOOTLOADER = 0x0C
const VENDORID = 0x10c4;
const PRODUCTID = 0x0002;
const AUDIOMOTH_PACKETSIZE = 62;
const FULL_USB_HID_PACKETSIZE = 64;
const FIRMWARE_DESCRIPTION_LENGTH = 32;
const pckg = require('./package.json');
exports.version = pckg.version;
/* Generate command line instruction */
var executable = 'usbhidtool-macOS';
if (process.platform === 'win32') {
if(process.arch === 'ia32') {
executable = 'usbhidtool-windows32'
} else {
executable = 'usbhidtool-windows';
}
}
if (process.platform === 'linux') {
executable = 'usbhidtool-linux';
}
var directory = path.join(__dirname, 'bin');
var unpackedDirectory = directory.replace("app.asar", "app.asar.unpacked");
if (fs.existsSync(directory)) {
directory = unpackedDirectory;
}
const command = '"' + path.join(directory, executable) + '" ' + VENDORID + ' ' + PRODUCTID + ' ';
/* Exported conversion function */
function convertFourBytesFromBufferToDate(buffer, offset) {
const unixTimestamp = (buffer[offset] & 0xFF) + ((buffer[offset + 1] & 0xFF) << 8) + ((buffer[offset + 2] & 0xFF) << 16) + ((buffer[offset + 3] & 0xFF) << 24);
return new Date(unixTimestamp * 1000);
}
function convertDateToFourBytesInBuffer(buffer, offset, date) {
const unixTimeStamp = Math.round(date.valueOf() / 1000);
buffer[offset + 3] = (unixTimeStamp >> 24) & 0xFF;
buffer[offset + 2] = (unixTimeStamp >> 16) & 0xFF;
buffer[offset + 1] = (unixTimeStamp >> 8) & 0xFF;
buffer[offset] = (unixTimeStamp & 0xFF);
}
function convertEightBytesFromBufferToID(buffer, offset) {
return Array.from(buffer.slice(offset, offset + 8).reverse(), function (byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('').toUpperCase();
}
function convertOneByteFromBufferToBatteryState(buffer, offset) {
const batteryState = buffer[offset];
if (batteryState === 0) {
return "< 3.6V";
}
if (batteryState === 15) {
return "> 4.9V";
}
return (3.5 + batteryState / 10).toFixed(1) + "V";
}
function convertThreeBytesFromBufferToFirmwareVersion(buffer, offset) {
return [buffer[offset], buffer[offset + 1], buffer[offset + 2]];
}
function convertBytesFromBufferToFirmwareDescription(buffer, offset) {
let descriptionStr = "";
for (let i = 0; i < FIRMWARE_DESCRIPTION_LENGTH; i++) {
const descriptionChar = String.fromCharCode(buffer[offset + i]);
if (descriptionChar === "\u0000") {
break;
}
descriptionStr += descriptionChar;
}
return descriptionStr;
}
function convertOneByteFromBufferToBoolean(buffer, offset) {
return (buffer[offset] === 0x01);
}
exports.convertFourBytesFromBufferToDate = convertFourBytesFromBufferToDate;
exports.convertDateToFourBytesInBuffer = convertDateToFourBytesInBuffer;
exports.convertEightBytesFromBufferToID = convertEightBytesFromBufferToID;
exports.convertOneByteFromBufferToBatteryState = convertOneByteFromBufferToBatteryState;
exports.convertThreeBytesFromBufferToFirmwareVersion = convertThreeBytesFromBufferToFirmwareVersion;
exports.convertBytesFromBufferToFirmwareDescription = convertBytesFromBufferToFirmwareDescription;
exports.convertOneByteFromBufferToBoolean = convertOneByteFromBufferToBoolean;
/* Main device functions */
function writeToDevice(buffer, callback) {
const cmd = command + buffer.join(' ');
child_process.exec(cmd, function (error, stdout) {
if (error) {
callback('Error calling usbhidtool');
} else {
if (stdout.slice(0, 4) === 'NULL') {
callback(null, null);
} else if (stdout.slice(0, 5) === 'ERROR') {
callback('Error reported by usbhidtool - ' + stdout.slice(7, stdout.length - 1));
} else {
let parseError = false;
const data = Array.from(stdout.split(" "), function (byte) {
return parseInt(byte, 16);
});
for (let i = 0; i < data.length; i += 1) {
parseError |= isNaN(data[i]);
}
if (parseError || data.length !== 64) {
callback('Error parsing response from usbhidtool');
} else {
callback(null, data);
}
}
}
});
}
/* Exported module functions */
function makeResponseHandler(messageType, convert, callback) {
const handler = function (err, data) {
if (err) {
callback(err);
} else if (data === null) {
callback(null, null);
} else if (data[0] !== messageType) {
callback('Incorrect message type from AudioMoth device');
} else {
callback(null, convert(data, 1));
}
};
return handler;
}
exports.getTime = function (callback) {
const buffer = [0x00, USB_MSG_TYPE_GET_TIME];
writeToDevice(buffer, makeResponseHandler(USB_MSG_TYPE_GET_TIME, convertFourBytesFromBufferToDate, callback));
};
exports.setTime = function (date, callback) {
const buffer = [0x00, USB_MSG_TYPE_SET_TIME, 0x00, 0x00, 0x00, 0x00];
convertDateToFourBytesInBuffer(buffer, 2, date);
writeToDevice(buffer, makeResponseHandler(USB_MSG_TYPE_SET_TIME, convertFourBytesFromBufferToDate, callback));
};
exports.getID = function (callback) {
const buffer = [0x00, USB_MSG_TYPE_GET_UID];
writeToDevice(buffer, makeResponseHandler(USB_MSG_TYPE_GET_UID, convertEightBytesFromBufferToID, callback));
};
exports.getBatteryState = function (callback) {
const buffer = [0x00, USB_MSG_TYPE_GET_BATTERY];
writeToDevice(buffer, makeResponseHandler(USB_MSG_TYPE_GET_BATTERY, convertOneByteFromBufferToBatteryState, callback));
};
exports.getFirmwareVersion = function (callback) {
const buffer = [0x00, USB_MSG_TYPE_GET_FIRMWARE_VERSION];
writeToDevice(buffer, makeResponseHandler(USB_MSG_TYPE_GET_FIRMWARE_VERSION, convertThreeBytesFromBufferToFirmwareVersion, callback));
};
exports.getFirmwareDescription = function (callback) {
const buffer = [0x00, USB_MSG_TYPE_GET_FIRMWARE_DESCRIPTION];
writeToDevice(buffer, makeResponseHandler(USB_MSG_TYPE_GET_FIRMWARE_DESCRIPTION, convertBytesFromBufferToFirmwareDescription, callback));
};
exports.getPacket = function (callback) {
const buffer = [0x00, USB_MSG_TYPE_GET_APP_PACKET];
writeToDevice(buffer, makeResponseHandler(USB_MSG_TYPE_GET_APP_PACKET, (buffer) => buffer, callback));
};
exports.setPacket = function (packet, callback) {
const packet_length = Math.min(packet.length, 62);
const buffer = [0x00, USB_MSG_TYPE_SET_APP_PACKET];
for (let i = 0; i < packet_length; i += 1) {
buffer.push(packet[i]);
}
writeToDevice(buffer, makeResponseHandler(USB_MSG_TYPE_SET_APP_PACKET, (buffer) => buffer, callback));
};
exports.queryBootloader = function (callback) {
const buffer = [0x00, USB_MSG_TYPE_QUERY_SERIAL_BOOTLOADER];
writeToDevice(buffer, makeResponseHandler(USB_MSG_TYPE_QUERY_SERIAL_BOOTLOADER, convertOneByteFromBufferToBoolean, callback));
};
exports.switchToBootloader = function (callback) {
const buffer = [0x00, USB_MSG_TYPE_ENTER_SERIAL_BOOTLOADER];
writeToDevice(buffer, makeResponseHandler(USB_MSG_TYPE_ENTER_SERIAL_BOOTLOADER, convertOneByteFromBufferToBoolean, callback));
};
exports.queryUSBHIDBootloader = function (callback) {
const buffer = [0x00, USB_MSG_TYPE_QUERY_USBHID_BOOTLOADER];
writeToDevice(buffer, makeResponseHandler(USB_MSG_TYPE_QUERY_USBHID_BOOTLOADER, convertOneByteFromBufferToBoolean, callback));
};
function makeBufferForUSBHIDBootloader(packet) {
const packet_length = Math.min(packet.length, AUDIOMOTH_PACKETSIZE);
const buffer = [0x00, USB_MSG_TYPE_ENTER_USBHID_BOOTLOADER];
for (let i = 0; i < packet_length; i += 1) buffer.push(packet[i] & 0xFF);
while (buffer.length < FULL_USB_HID_PACKETSIZE) buffer.push(0x00);
return buffer;
}
exports.sendPacketToUSBHIDBootloader = function (packet, callback) {
const buffer = makeBufferForUSBHIDBootloader(packet);
writeToDevice(buffer, makeResponseHandler(USB_MSG_TYPE_ENTER_USBHID_BOOTLOADER, (buffer) => buffer, callback));
};
exports.sendMultiplePacketsToUSBHIDBootloader = function (packets, callback) {
const buffers = [];
for (let i = 0; i < packets.length; i += 1) {
buffers.push(makeBufferForUSBHIDBootloader(packets[i]));
}
let buffer = [];
for (let i = 0; i < buffers.length; i += 1) {
buffer = buffer.concat(buffers[i]);
}
writeToDevice(buffer, makeResponseHandler(USB_MSG_TYPE_ENTER_USBHID_BOOTLOADER, (buffer) => buffer, callback));
};