From 6c7232a5ba50a9a75419b788dc42d060d311dba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Kiss?= Date: Tue, 13 Feb 2018 02:11:27 +0100 Subject: [PATCH] feat(device): Make Agent able to unbrick bricked modules (#577) --- packages/uhk-usb/src/constants.ts | 3 ++- packages/uhk-usb/src/uhk-operations.ts | 36 ++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/packages/uhk-usb/src/constants.ts b/packages/uhk-usb/src/constants.ts index f0056b08..4d154b7a 100644 --- a/packages/uhk-usb/src/constants.ts +++ b/packages/uhk-usb/src/constants.ts @@ -39,7 +39,8 @@ export enum ConfigBufferId { export enum DevicePropertyIds { DeviceProtocolVersion = 0, ProtocolVersions = 1, - ConfigSizes = 2 + ConfigSizes = 2, + CurrentKbootCommand = 3 } export enum EnumerationModes { diff --git a/packages/uhk-usb/src/uhk-operations.ts b/packages/uhk-usb/src/uhk-operations.ts index 85258494..c4b9534e 100644 --- a/packages/uhk-usb/src/uhk-operations.ts +++ b/packages/uhk-usb/src/uhk-operations.ts @@ -5,8 +5,13 @@ import * as fs from 'fs'; import { UhkBlhost } from './uhk-blhost'; import { UhkHidDevice } from './uhk-hid-device'; import { snooze } from './util'; -import { convertBufferToIntArray, getTransferBuffers, DevicePropertyIds, UsbCommand, ConfigBufferId - } from '../index'; +import { + convertBufferToIntArray, + getTransferBuffers, + DevicePropertyIds, + UsbCommand, + ConfigBufferId +} from '../index'; import { LoadConfigurationsResult } from './models/load-configurations-result'; export class UhkOperations { @@ -42,6 +47,13 @@ export class UhkOperations { await snooze(1000); await this.device.jumpToBootloaderModule(ModuleSlotToId.leftHalf); this.device.close(); + + const leftModuleBricked = await this.waitForKbootIdle(); + if (!leftModuleBricked) { + this.logService.error('[UhkOperations] Couldn\'t connect to the left keyboard half.'); + return; + } + await this.device.reenumerate(EnumerationModes.Buspal); this.device.close(); await this.blhost.runBlhostCommandRetry([...buspalPrefix, 'get-property', '1']); @@ -161,6 +173,26 @@ export class UhkOperations { } } + public async waitForKbootIdle(): Promise { + const timeoutTime = new Date(new Date().getTime() + 30000); + + while (new Date() < timeoutTime) { + const buffer = await this.device.write(new Buffer([UsbCommand.GetProperty, DevicePropertyIds.CurrentKbootCommand])); + this.device.close(); + + if (buffer[1] === 0) { + return true; + } + + // tslint:disable-next-line: max-line-length + this.logService.info('[DeviceOperation] Cannot ping the bootloader. Please reconnect the left keyboard half. It probably needs several tries, so keep reconnecting until you see this message.'); + + await snooze(1000); + } + + return false; + } + /** * IpcMain handler. Send the UserConfiguration to the UHK Device and send a response with the result. * @param {string} json - UserConfiguration in JSON format