Files
agent/packages/usb/read-config.js
Róbert Kiss 30c74c06d3 refactor(usb): Use hid-api instead of lib-usb (#404)
* refactor(usb): Rewrite jump-to-bootloder to node-hid

* refactor(usb): Rewrite the whole usb packages to HID-API

* refactor(usb): Deleted not valid usb command files

* refactor(usb): Deleted not supported usb commands

* Remove obsolete script.

* Remove obsolete script.

* Fix script.

* Fix script.

* No need to assign the silent property because it has been removed.

* This workaround may work on other platforms, but it certainly doesn't work on Linux. It makes some scripts not work, so I'm commenting it out.

* Fix bootloader VID and PID.
2017-09-06 14:32:58 +02:00

38 lines
1.4 KiB
JavaScript
Executable File

#!/usr/bin/env node
const fs = require('fs');
const uhk = require('./uhk');
const device = uhk.getUhkDevice();
const chunkSize = 63;
let isHardwareConfig = process.argv[2] === 'h';
let configTypeString = isHardwareConfig ? 'hardware' : 'user';
let configSize;
let offset = 0;
let configBuffer = new Buffer(0);
let chunkSizeToRead;
const payload = new Buffer([
uhk.usbCommands.getProperty,
isHardwareConfig
? uhk.systemPropertyIds.hardwareConfigSize
: uhk.systemPropertyIds.userConfigSize
]);
device.write(uhk.getTransferData(payload));
let buffer = Buffer.from(device.readSync());
configSize = buffer[1] + (buffer[2]<<8);
console.log(`${configTypeString}configSize:`, configSize);
while(offset < configSize) {
const usbCommand = isHardwareConfig ? uhk.usbCommands.readHardwareConfig : uhk.usbCommands.readUserConfig;
chunkSizeToRead = Math.min(chunkSize, configSize - offset);
buffer = Buffer.from([usbCommand, chunkSizeToRead, offset & 0xff, offset >> 8]);
device.write(uhk.getTransferData(buffer));
buffer = Buffer.from(device.readSync());
console.log('read-config-chunk', uhk.bufferToString(buffer));
configBuffer = Buffer.concat([configBuffer, new Buffer(buffer.slice(1, chunkSizeToRead + 1))]);
offset += chunkSizeToRead
}
console.log('read ', uhk.bufferToString(configBuffer));
fs.writeFileSync(`${configTypeString}-config.read`, configBuffer);