Finalize usb protocol (#515)

* Change UsbCommandId_SetTestLed from 0x02 to 0x14

* Change UsbCommandId_JumpToModuleBootloader from 0x12 to 0x02.

* Change UsbCommandId_SendKbootCommandToModule from 0x13 to 0x03.

* Replace UsbCommandId_ReadHardwareConfig and UsbCommandId_ReadUserConfig with UsbCommandId_ReadConfig.

* Change UsbCommandId_WriteHardwareConfig and UsbCommandId_WriteUserConfig to 0x05 and 0x06.

* Change UsbCommandId_ApplyConfig to 0x07.

* Change the arguments of UsbCommandId_LaunchEepromTransfer and its id to 0x08.

* Change the value of UsbCommandId_{GetDeviceState,SetTestLed,GetDebugBuffer,GetAdcValue,SetLedPwmBrightness}.

* Use firmware 6.0.0
This commit is contained in:
László Monda
2017-12-13 01:20:23 +01:00
committed by GitHub
parent 8e121d88ab
commit 2702a74035
9 changed files with 100 additions and 59 deletions
+2 -2
View File
@@ -16,8 +16,8 @@
"dependencies": { "dependencies": {
"node-hid": "0.5.7" "node-hid": "0.5.7"
}, },
"firmwareVersion": "5.0.1", "firmwareVersion": "6.0.0",
"dataModelVersion": "4.0.0", "dataModelVersion": "4.0.0",
"usbProtocolVersion": "2.0.0", "usbProtocolVersion": "3.0.0",
"slaveProtocolVersion": "3.0.0" "slaveProtocolVersion": "3.0.0"
} }
+23 -16
View File
@@ -8,24 +8,31 @@ export namespace Constants {
* UHK USB Communications command. All communication package should have start with a command code. * UHK USB Communications command. All communication package should have start with a command code.
*/ */
export enum UsbCommand { export enum UsbCommand {
GetProperty = 0, GetProperty = 0x00,
Reenumerate = 1, Reenumerate = 0x01,
UploadUserConfig = 8, JumpToModuleBootloader = 0x02,
ApplyConfig = 9, SendKbootCommandToModule = 0x03,
LaunchEepromTransfer = 12, ReadConfig = 0x04,
ReadHardwareConfig = 13, WriteHardwareConfig = 0x05,
WriteHardwareConfig = 14, WriteStagingUserConfig = 0x06,
ReadUserConfig = 15, ApplyConfig = 0x07,
GetKeyboardState = 16, LaunchEepromTransfer = 0x08,
JumpToModuleBootloader = 18, GetDeviceState = 0x09,
SendKbootCommandToModule = 19 SetTestLed = 0x0a,
GetDebugBuffer = 0x0b,
GetAdcValue = 0x0c,
SetLedPwmBrightness = 0x0d
} }
export enum EepromTransfer { export enum EepromOperation {
ReadHardwareConfig = 0, read = 0,
WriteHardwareConfig = 1, write = 1
ReadUserConfig = 2, }
WriteUserConfig = 3
export enum ConfigBufferId {
hardwareConfig = 0,
stagingUserConfig = 1,
validatedUserConfig = 2
} }
export enum SystemPropertyIds { export enum SystemPropertyIds {
+9 -4
View File
@@ -2,8 +2,9 @@ import { Device, devices, HID } from 'node-hid';
import { LogService } from 'uhk-common'; import { LogService } from 'uhk-common';
import { import {
ConfigBufferId,
Constants, Constants,
EepromTransfer, EepromOperation,
enumerationModeIdToProductId, enumerationModeIdToProductId,
EnumerationModes, EnumerationModes,
KbootCommands, KbootCommands,
@@ -81,8 +82,12 @@ export class UhkHidDevice {
}); });
} }
public async writeConfigToEeprom(transferType: EepromTransfer): Promise<void> { public async writeUserConfigToEeprom(): Promise<void> {
await this.write(new Buffer([UsbCommand.LaunchEepromTransfer, transferType])); await this.write(new Buffer([
UsbCommand.LaunchEepromTransfer,
EepromOperation.write,
ConfigBufferId.validatedUserConfig
]));
await this.waitUntilKeyboardBusy(); await this.waitUntilKeyboardBusy();
} }
@@ -101,7 +106,7 @@ export class UhkHidDevice {
public async waitUntilKeyboardBusy(): Promise<void> { public async waitUntilKeyboardBusy(): Promise<void> {
while (true) { while (true) {
const buffer = await this.write(new Buffer([UsbCommand.GetKeyboardState])); const buffer = await this.write(new Buffer([UsbCommand.GetDeviceState]));
if (buffer[1] === 0) { if (buffer[1] === 0) {
break; break;
} }
+13 -8
View File
@@ -5,7 +5,8 @@ import * as fs from 'fs';
import { UhkBlhost } from './uhk-blhost'; import { UhkBlhost } from './uhk-blhost';
import { UhkHidDevice } from './uhk-hid-device'; import { UhkHidDevice } from './uhk-hid-device';
import { snooze } from './util'; import { snooze } from './util';
import { convertBufferToIntArray, EepromTransfer, getTransferBuffers, SystemPropertyIds, UsbCommand } from '../index'; import { convertBufferToIntArray, getTransferBuffers, SystemPropertyIds, UsbCommand, ConfigBufferId
} from '../index';
import { LoadConfigurationsResult } from './models/load-configurations-result'; import { LoadConfigurationsResult } from './models/load-configurations-result';
export class UhkOperations { export class UhkOperations {
@@ -69,12 +70,12 @@ export class UhkOperations {
await this.device.waitUntilKeyboardBusy(); await this.device.waitUntilKeyboardBusy();
const userConfiguration = await this.loadConfiguration( const userConfiguration = await this.loadConfiguration(
SystemPropertyIds.MaxUserConfigSize, SystemPropertyIds.MaxUserConfigSize,
UsbCommand.ReadUserConfig, ConfigBufferId.validatedUserConfig,
'user configuration'); 'user configuration');
const hardwareConfiguration = await this.loadConfiguration( const hardwareConfiguration = await this.loadConfiguration(
SystemPropertyIds.HardwareConfigSize, SystemPropertyIds.HardwareConfigSize,
UsbCommand.ReadHardwareConfig, ConfigBufferId.hardwareConfig,
'hardware configuration'); 'hardware configuration');
return { return {
@@ -90,7 +91,10 @@ export class UhkOperations {
* Return with the actual user / hardware fonfiguration from UHK Device * Return with the actual user / hardware fonfiguration from UHK Device
* @returns {Promise<Buffer>} * @returns {Promise<Buffer>}
*/ */
public async loadConfiguration(property: SystemPropertyIds, config: UsbCommand, configName: string): Promise<string> { public async loadConfiguration(
property: SystemPropertyIds,
configBufferId: ConfigBufferId ,
configName: string): Promise<string> {
let response = []; let response = [];
try { try {
@@ -106,12 +110,13 @@ export class UhkOperations {
this.logService.debug(`[DeviceOperation] USB[T]: Read ${configName} from keyboard`); this.logService.debug(`[DeviceOperation] USB[T]: Read ${configName} from keyboard`);
while (offset < configSize) { while (offset < configSize) {
const chunkSizeToRead = Math.min(chunkSize, configSize - offset); const chunkSizeToRead = Math.min(chunkSize, configSize - offset);
const writeBuffer = Buffer.from([config, chunkSizeToRead, offset & 0xff, offset >> 8]); const writeBuffer = Buffer.from(
[UsbCommand.ReadConfig, configBufferId, chunkSizeToRead, offset & 0xff, offset >> 8]);
const readBuffer = await this.device.write(writeBuffer); const readBuffer = await this.device.write(writeBuffer);
configBuffer = Buffer.concat([configBuffer, new Buffer(readBuffer.slice(1, chunkSizeToRead + 1))]); configBuffer = Buffer.concat([configBuffer, new Buffer(readBuffer.slice(1, chunkSizeToRead + 1))]);
offset += chunkSizeToRead; offset += chunkSizeToRead;
if (firstRead && config === UsbCommand.ReadUserConfig) { if (firstRead && configBufferId !== ConfigBufferId.hardwareConfig) {
firstRead = false; firstRead = false;
configSize = readBuffer[7] + (readBuffer[8] << 8); configSize = readBuffer[7] + (readBuffer[8] << 8);
this.logService.debug(`[DeviceOperation] userConfigSize: ${configSize}`); this.logService.debug(`[DeviceOperation] userConfigSize: ${configSize}`);
@@ -148,7 +153,7 @@ export class UhkOperations {
this.logService.debug('[DeviceOperation] USB[T]: Write user configuration to keyboard'); this.logService.debug('[DeviceOperation] USB[T]: Write user configuration to keyboard');
await this.sendUserConfigToKeyboard(json); await this.sendUserConfigToKeyboard(json);
this.logService.debug('[DeviceOperation] USB[T]: Write user configuration to EEPROM'); this.logService.debug('[DeviceOperation] USB[T]: Write user configuration to EEPROM');
await this.device.writeConfigToEeprom(EepromTransfer.WriteUserConfig); await this.device.writeUserConfigToEeprom();
} }
catch (error) { catch (error) {
this.logService.error('[DeviceOperation] Transferring error', error); this.logService.error('[DeviceOperation] Transferring error', error);
@@ -166,7 +171,7 @@ export class UhkOperations {
*/ */
private async sendUserConfigToKeyboard(json: string): Promise<void> { private async sendUserConfigToKeyboard(json: string): Promise<void> {
const buffer: Buffer = new Buffer(JSON.parse(json).data); const buffer: Buffer = new Buffer(JSON.parse(json).data);
const fragments = getTransferBuffers(UsbCommand.UploadUserConfig, buffer); const fragments = getTransferBuffers(UsbCommand.WriteStagingUserConfig, buffer);
for (const fragment of fragments) { for (const fragment of fragments) {
await this.device.write(fragment); await this.device.write(fragment);
} }
+4 -4
View File
@@ -2,15 +2,15 @@
const uhk = require('./uhk'); const uhk = require('./uhk');
const eepromTransferType = process.argv[2]; const eepromTransferType = process.argv[2];
const eepromTransferId = uhk.eepromTransfer[eepromTransferType]; const eepromTransfer = uhk.eepromTransfer[eepromTransferType];
if (eepromTransferId === undefined) { if (eepromTransfer === undefined) {
console.error(`Gotta provide one of ${Object.keys(uhk.eepromTransfer).join(', ')}`); console.error(`Gotta provide one of ${Object.keys(uhk.eepromTransfer).join(', ')}`);
process.exit(1); process.exit(1);
} }
const device = uhk.getUhkDevice(); const device = uhk.getUhkDevice();
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.launchEepromTransferLegacy, eepromTransferId]))); device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.launchEepromTransfer, eepromTransfer.operation, eepromTransfer.configBuffer])));
const buffer = Buffer.from(device.readSync()); const buffer = Buffer.from(device.readSync());
const responseCode = buffer[0]; const responseCode = buffer[0];
if (responseCode !== 0) { if (responseCode !== 0) {
@@ -20,7 +20,7 @@ if (responseCode !== 0) {
function waitUntilKeyboardBusy() { function waitUntilKeyboardBusy() {
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.getKeyboardState]))); device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.getDeviceState])));
const keyboardStateBuffer = Buffer.from(device.readSync()); const keyboardStateBuffer = Buffer.from(device.readSync());
if (keyboardStateBuffer[1] === 1) { if (keyboardStateBuffer[1] === 1) {
@@ -3,7 +3,7 @@ const uhk = require('./uhk');
const device = uhk.getUhkDevice(); const device = uhk.getUhkDevice();
function readKeyboardState() { function readKeyboardState() {
const payload = new Buffer([uhk.usbCommands.getKeyboardState]); const payload = new Buffer([uhk.usbCommands.getDeviceState]);
console.log('Sending ', uhk.bufferToString(payload)); console.log('Sending ', uhk.bufferToString(payload));
device.write(uhk.getTransferData(payload)); device.write(uhk.getTransferData(payload));
const receivedBuffer = device.readSync(); const receivedBuffer = device.readSync();
+3 -3
View File
@@ -23,10 +23,10 @@ device.write(uhk.getTransferData(payload));
let buffer = Buffer.from(device.readSync()); let buffer = Buffer.from(device.readSync());
configSize = buffer[1] + (buffer[2]<<8); configSize = buffer[1] + (buffer[2]<<8);
console.log(`${configTypeString}configSize:`, configSize); console.log(`${configTypeString}configSize:`, configSize);
while(offset < configSize) { while (offset < configSize) {
const usbCommand = isHardwareConfig ? uhk.usbCommands.readHardwareConfig : uhk.usbCommands.readUserConfig; const configBufferId = isHardwareConfig ? uhk.configBufferIds.hardwareConfig : uhk.configBufferIds.validatedUserConfig;
chunkSizeToRead = Math.min(chunkSize, configSize - offset); chunkSizeToRead = Math.min(chunkSize, configSize - offset);
buffer = Buffer.from([usbCommand, chunkSizeToRead, offset & 0xff, offset >> 8]); buffer = Buffer.from([uhk.usbCommands.readConfig, configBufferId, chunkSizeToRead, offset & 0xff, offset >> 8]);
console.log('write to keyboard', uhk.bufferToString(buffer)); console.log('write to keyboard', uhk.bufferToString(buffer));
device.write(uhk.getTransferData(buffer)); device.write(uhk.getTransferData(buffer));
buffer = Buffer.from(device.readSync()); buffer = Buffer.from(device.readSync());
+43 -19
View File
@@ -44,6 +44,17 @@ function getBootloaderDevice() {
return foundDevice; return foundDevice;
} }
let configBufferIds = {
hardwareConfig: 0,
stagingUserConfig: 1,
validatedUserConfig: 2,
};
let eepromOperations = {
read: 0,
write: 1,
};
exports = module.exports = moduleExports = { exports = module.exports = moduleExports = {
bufferToString, bufferToString,
getUhkDevice, getUhkDevice,
@@ -51,21 +62,20 @@ exports = module.exports = moduleExports = {
getTransferData, getTransferData,
checkModuleSlot, checkModuleSlot,
usbCommands: { usbCommands: {
getProperty: 0, getProperty : 0x00,
reenumerate: 1, reenumerate : 0x01,
setTestLed: 2, jumpToModuleBootloader : 0x02,
writeUserConfig: 8, sendKbootCommandToModule: 0x03,
applyConfig: 9, readConfig : 0x04,
setLedPwmBrightness: 10, writeHardwareConfig : 0x05,
getAdcValue: 11, writeStagingUserConfig : 0x06,
launchEepromTransferLegacy: 12, applyConfig : 0x07,
readHardwareConfig: 13, launchEepromTransfer : 0x08,
writeHardwareConfig: 14, getDeviceState : 0x09,
readUserConfig: 15, setTestLed : 0x0a,
getKeyboardState: 16, getDebugBuffer : 0x0b,
getDebugInfo: 17, getAdcValue : 0x0c,
jumpToModuleBootloader: 18, setLedPwmBrightness : 0x0d,
sendKbootCommandToModule: 19,
}, },
enumerationModes: { enumerationModes: {
bootloader: 0, bootloader: 0,
@@ -94,11 +104,25 @@ exports = module.exports = moduleExports = {
hardwareConfigSize: 4, hardwareConfigSize: 4,
userConfigSize: 5, userConfigSize: 5,
}, },
configBufferIds,
eepromOperations,
eepromTransfer: { eepromTransfer: {
readHardwareConfig: 0, readHardwareConfig: {
writeHardwareConfig: 1, operation: eepromOperations.read,
readUserConfig: 2, configBuffer: configBufferIds.hardwareConfig,
writeUserConfig: 3, },
writeHardwareConfig: {
operation: eepromOperations.write,
configBuffer:configBufferIds.hardwareConfig,
},
readUserConfig: {
operation: eepromOperations.read,
configBuffer: configBufferIds.validatedUserConfig,
},
writeUserConfig: {
operation: eepromOperations.write,
configBuffer: configBufferIds.validatedUserConfig,
},
}, },
kbootCommands: { kbootCommands: {
idle: 0, idle: 0,
+2 -2
View File
@@ -24,8 +24,8 @@ let buffer = Buffer.from(device.readSync());
configSize = buffer[1] + (buffer[2]<<8); configSize = buffer[1] + (buffer[2]<<8);
console.log(`${configTypeString}configSize:`, configSize); console.log(`${configTypeString}configSize:`, configSize);
while (offset < configSize){ while (offset < configSize) {
const usbCommand = isHardwareConfig ? uhk.usbCommands.writeHardwareConfig : uhk.usbCommands.writeUserConfig; const usbCommand = isHardwareConfig ? uhk.usbCommands.writeHardwareConfig : uhk.usbCommands.writeStagingUserConfig;
chunkSizeToRead = Math.min(chunkSize, configSize - offset); chunkSizeToRead = Math.min(chunkSize, configSize - offset);
buffer = Buffer.concat([ buffer = Buffer.concat([
new Buffer([usbCommand, chunkSizeToRead, offset & 0xff, offset >> 8]), new Buffer([usbCommand, chunkSizeToRead, offset & 0xff, offset >> 8]),