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

View File

@@ -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"
}

View File

@@ -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 {

View File

@@ -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<void> {
await this.write(new Buffer([UsbCommand.LaunchEepromTransfer, transferType]));
public async writeUserConfigToEeprom(): Promise<void> {
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<void> {
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;
}

View File

@@ -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<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 = [];
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<void> {
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);
}

View File

@@ -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) {

View File

@@ -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();

View File

@@ -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());

View File

@@ -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,

View File

@@ -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]),