From 2702a740359c717d922752128d357e2ce7c45b06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Wed, 13 Dec 2017 01:20:23 +0100 Subject: [PATCH] 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 --- packages/uhk-agent/src/package.json | 4 +- packages/uhk-usb/src/constants.ts | 39 +++++++----- packages/uhk-usb/src/uhk-hid-device.ts | 13 ++-- packages/uhk-usb/src/uhk-operations.ts | 21 ++++--- packages/usb/eeprom.js | 8 +-- ...-keyboard-state.js => get-device-state.js} | 2 +- packages/usb/read-config.js | 6 +- packages/usb/uhk.js | 62 +++++++++++++------ packages/usb/write-config.js | 4 +- 9 files changed, 100 insertions(+), 59 deletions(-) rename packages/usb/{get-keyboard-state.js => get-device-state.js} (91%) diff --git a/packages/uhk-agent/src/package.json b/packages/uhk-agent/src/package.json index 949bda06..b120eb4c 100644 --- a/packages/uhk-agent/src/package.json +++ b/packages/uhk-agent/src/package.json @@ -16,8 +16,8 @@ "dependencies": { "node-hid": "0.5.7" }, - "firmwareVersion": "5.0.1", + "firmwareVersion": "6.0.0", "dataModelVersion": "4.0.0", - "usbProtocolVersion": "2.0.0", + "usbProtocolVersion": "3.0.0", "slaveProtocolVersion": "3.0.0" } diff --git a/packages/uhk-usb/src/constants.ts b/packages/uhk-usb/src/constants.ts index 5350414b..9b9889c9 100644 --- a/packages/uhk-usb/src/constants.ts +++ b/packages/uhk-usb/src/constants.ts @@ -8,24 +8,31 @@ export namespace Constants { * UHK USB Communications command. All communication package should have start with a command code. */ export enum UsbCommand { - GetProperty = 0, - Reenumerate = 1, - UploadUserConfig = 8, - ApplyConfig = 9, - LaunchEepromTransfer = 12, - ReadHardwareConfig = 13, - WriteHardwareConfig = 14, - ReadUserConfig = 15, - GetKeyboardState = 16, - JumpToModuleBootloader = 18, - SendKbootCommandToModule = 19 + GetProperty = 0x00, + Reenumerate = 0x01, + JumpToModuleBootloader = 0x02, + SendKbootCommandToModule = 0x03, + ReadConfig = 0x04, + WriteHardwareConfig = 0x05, + WriteStagingUserConfig = 0x06, + ApplyConfig = 0x07, + LaunchEepromTransfer = 0x08, + GetDeviceState = 0x09, + SetTestLed = 0x0a, + GetDebugBuffer = 0x0b, + GetAdcValue = 0x0c, + SetLedPwmBrightness = 0x0d } -export enum EepromTransfer { - ReadHardwareConfig = 0, - WriteHardwareConfig = 1, - ReadUserConfig = 2, - WriteUserConfig = 3 +export enum EepromOperation { + read = 0, + write = 1 +} + +export enum ConfigBufferId { + hardwareConfig = 0, + stagingUserConfig = 1, + validatedUserConfig = 2 } export enum SystemPropertyIds { diff --git a/packages/uhk-usb/src/uhk-hid-device.ts b/packages/uhk-usb/src/uhk-hid-device.ts index 6fdef263..be22c7a5 100644 --- a/packages/uhk-usb/src/uhk-hid-device.ts +++ b/packages/uhk-usb/src/uhk-hid-device.ts @@ -2,8 +2,9 @@ import { Device, devices, HID } from 'node-hid'; import { LogService } from 'uhk-common'; import { + ConfigBufferId, Constants, - EepromTransfer, + EepromOperation, enumerationModeIdToProductId, EnumerationModes, KbootCommands, @@ -81,8 +82,12 @@ export class UhkHidDevice { }); } - public async writeConfigToEeprom(transferType: EepromTransfer): Promise { - await this.write(new Buffer([UsbCommand.LaunchEepromTransfer, transferType])); + public async writeUserConfigToEeprom(): Promise { + await this.write(new Buffer([ + UsbCommand.LaunchEepromTransfer, + EepromOperation.write, + ConfigBufferId.validatedUserConfig + ])); await this.waitUntilKeyboardBusy(); } @@ -101,7 +106,7 @@ export class UhkHidDevice { public async waitUntilKeyboardBusy(): Promise { while (true) { - const buffer = await this.write(new Buffer([UsbCommand.GetKeyboardState])); + const buffer = await this.write(new Buffer([UsbCommand.GetDeviceState])); if (buffer[1] === 0) { break; } diff --git a/packages/uhk-usb/src/uhk-operations.ts b/packages/uhk-usb/src/uhk-operations.ts index e2b23525..15dbdc7b 100644 --- a/packages/uhk-usb/src/uhk-operations.ts +++ b/packages/uhk-usb/src/uhk-operations.ts @@ -5,7 +5,8 @@ import * as fs from 'fs'; import { UhkBlhost } from './uhk-blhost'; import { UhkHidDevice } from './uhk-hid-device'; 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'; export class UhkOperations { @@ -69,12 +70,12 @@ export class UhkOperations { await this.device.waitUntilKeyboardBusy(); const userConfiguration = await this.loadConfiguration( SystemPropertyIds.MaxUserConfigSize, - UsbCommand.ReadUserConfig, + ConfigBufferId.validatedUserConfig, 'user configuration'); const hardwareConfiguration = await this.loadConfiguration( SystemPropertyIds.HardwareConfigSize, - UsbCommand.ReadHardwareConfig, + ConfigBufferId.hardwareConfig, 'hardware configuration'); return { @@ -90,7 +91,10 @@ export class UhkOperations { * Return with the actual user / hardware fonfiguration from UHK Device * @returns {Promise} */ - public async loadConfiguration(property: SystemPropertyIds, config: UsbCommand, configName: string): Promise { + public async loadConfiguration( + property: SystemPropertyIds, + configBufferId: ConfigBufferId , + configName: string): Promise { let response = []; try { @@ -106,12 +110,13 @@ export class UhkOperations { this.logService.debug(`[DeviceOperation] USB[T]: Read ${configName} from keyboard`); while (offset < configSize) { 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); configBuffer = Buffer.concat([configBuffer, new Buffer(readBuffer.slice(1, chunkSizeToRead + 1))]); offset += chunkSizeToRead; - if (firstRead && config === UsbCommand.ReadUserConfig) { + if (firstRead && configBufferId !== ConfigBufferId.hardwareConfig) { firstRead = false; configSize = readBuffer[7] + (readBuffer[8] << 8); this.logService.debug(`[DeviceOperation] userConfigSize: ${configSize}`); @@ -148,7 +153,7 @@ export class UhkOperations { this.logService.debug('[DeviceOperation] USB[T]: Write user configuration to keyboard'); await this.sendUserConfigToKeyboard(json); this.logService.debug('[DeviceOperation] USB[T]: Write user configuration to EEPROM'); - await this.device.writeConfigToEeprom(EepromTransfer.WriteUserConfig); + await this.device.writeUserConfigToEeprom(); } catch (error) { this.logService.error('[DeviceOperation] Transferring error', error); @@ -166,7 +171,7 @@ export class UhkOperations { */ private async sendUserConfigToKeyboard(json: string): Promise { 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) { await this.device.write(fragment); } diff --git a/packages/usb/eeprom.js b/packages/usb/eeprom.js index 76b5c0a3..3b903231 100755 --- a/packages/usb/eeprom.js +++ b/packages/usb/eeprom.js @@ -2,15 +2,15 @@ const uhk = require('./uhk'); 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(', ')}`); process.exit(1); } 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 responseCode = buffer[0]; if (responseCode !== 0) { @@ -20,7 +20,7 @@ if (responseCode !== 0) { 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()); if (keyboardStateBuffer[1] === 1) { diff --git a/packages/usb/get-keyboard-state.js b/packages/usb/get-device-state.js similarity index 91% rename from packages/usb/get-keyboard-state.js rename to packages/usb/get-device-state.js index 8372f0e7..2115c62e 100755 --- a/packages/usb/get-keyboard-state.js +++ b/packages/usb/get-device-state.js @@ -3,7 +3,7 @@ const uhk = require('./uhk'); const device = uhk.getUhkDevice(); function readKeyboardState() { - const payload = new Buffer([uhk.usbCommands.getKeyboardState]); + const payload = new Buffer([uhk.usbCommands.getDeviceState]); console.log('Sending ', uhk.bufferToString(payload)); device.write(uhk.getTransferData(payload)); const receivedBuffer = device.readSync(); diff --git a/packages/usb/read-config.js b/packages/usb/read-config.js index fbe2c518..d3969882 100755 --- a/packages/usb/read-config.js +++ b/packages/usb/read-config.js @@ -23,10 +23,10 @@ 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; +while (offset < configSize) { + const configBufferId = isHardwareConfig ? uhk.configBufferIds.hardwareConfig : uhk.configBufferIds.validatedUserConfig; 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)); device.write(uhk.getTransferData(buffer)); buffer = Buffer.from(device.readSync()); diff --git a/packages/usb/uhk.js b/packages/usb/uhk.js index c80bf0e8..c5c85329 100644 --- a/packages/usb/uhk.js +++ b/packages/usb/uhk.js @@ -44,6 +44,17 @@ function getBootloaderDevice() { return foundDevice; } +let configBufferIds = { + hardwareConfig: 0, + stagingUserConfig: 1, + validatedUserConfig: 2, +}; + +let eepromOperations = { + read: 0, + write: 1, +}; + exports = module.exports = moduleExports = { bufferToString, getUhkDevice, @@ -51,21 +62,20 @@ exports = module.exports = moduleExports = { getTransferData, checkModuleSlot, usbCommands: { - getProperty: 0, - reenumerate: 1, - setTestLed: 2, - writeUserConfig: 8, - applyConfig: 9, - setLedPwmBrightness: 10, - getAdcValue: 11, - launchEepromTransferLegacy: 12, - readHardwareConfig: 13, - writeHardwareConfig: 14, - readUserConfig: 15, - getKeyboardState: 16, - getDebugInfo: 17, - jumpToModuleBootloader: 18, - sendKbootCommandToModule: 19, + getProperty : 0x00, + reenumerate : 0x01, + jumpToModuleBootloader : 0x02, + sendKbootCommandToModule: 0x03, + readConfig : 0x04, + writeHardwareConfig : 0x05, + writeStagingUserConfig : 0x06, + applyConfig : 0x07, + launchEepromTransfer : 0x08, + getDeviceState : 0x09, + setTestLed : 0x0a, + getDebugBuffer : 0x0b, + getAdcValue : 0x0c, + setLedPwmBrightness : 0x0d, }, enumerationModes: { bootloader: 0, @@ -94,11 +104,25 @@ exports = module.exports = moduleExports = { hardwareConfigSize: 4, userConfigSize: 5, }, + configBufferIds, + eepromOperations, eepromTransfer: { - readHardwareConfig: 0, - writeHardwareConfig: 1, - readUserConfig: 2, - writeUserConfig: 3, + readHardwareConfig: { + operation: eepromOperations.read, + configBuffer: configBufferIds.hardwareConfig, + }, + writeHardwareConfig: { + operation: eepromOperations.write, + configBuffer:configBufferIds.hardwareConfig, + }, + readUserConfig: { + operation: eepromOperations.read, + configBuffer: configBufferIds.validatedUserConfig, + }, + writeUserConfig: { + operation: eepromOperations.write, + configBuffer: configBufferIds.validatedUserConfig, + }, }, kbootCommands: { idle: 0, diff --git a/packages/usb/write-config.js b/packages/usb/write-config.js index 9a671fb0..2ae75bcd 100755 --- a/packages/usb/write-config.js +++ b/packages/usb/write-config.js @@ -24,8 +24,8 @@ 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.writeHardwareConfig : uhk.usbCommands.writeUserConfig; +while (offset < configSize) { + const usbCommand = isHardwareConfig ? uhk.usbCommands.writeHardwareConfig : uhk.usbCommands.writeStagingUserConfig; chunkSizeToRead = Math.min(chunkSize, configSize - offset); buffer = Buffer.concat([ new Buffer([usbCommand, chunkSizeToRead, offset & 0xff, offset >> 8]),