Skip to content
This repository has been archived by the owner on Dec 15, 2024. It is now read-only.

Commit

Permalink
feat: ignore-firmware-checksums command line argument (#2365)
Browse files Browse the repository at this point in the history
Use checksum to identify installed firmware on UHK devices
  • Loading branch information
ert78gb authored Dec 6, 2024
1 parent 1726718 commit 90c46df
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
28 changes: 22 additions & 6 deletions packages/uhk-agent/src/services/device.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
convertBufferToIntArray,
DevicePropertyIds,
EnumerationModes,
findDeviceConfigInFirmwareJson,
getCurrentUhkDeviceProduct,
getCurrentUhkDongleHID,
getCurrenUhk80LeftHID,
Expand Down Expand Up @@ -377,6 +378,11 @@ export class DeviceService {
response.userConfigSaved = false;
response.firmwareDowngraded = false;
const data: UpdateFirmwareData = JSON.parse(args[0]);

if (this.options['ignore-firmware-checksums']) {
data.forceUpgrade = true;
}

let firmwarePathData: TmpFirmware;

try {
Expand Down Expand Up @@ -415,10 +421,15 @@ export class DeviceService {
dongleUhkDevice = new UhkHidDevice(this.logService, this.options, this.rootDir, dongleHid);
let dongleOperations = new UhkOperations(this.logService, dongleUhkDevice);
let versionInfo = await dongleOperations.getDeviceVersionInfo();
this.logService.misc('[DeviceService] Dongle firmware version:',
versionInfo.firmwareVersion);
this.logService.misc('[DeviceService] Current Dongle firmware checksum:',
versionInfo.firmwareChecksum);

const deviceConfig = findDeviceConfigInFirmwareJson(UHK_DONGLE, packageJson);

if (data.forceUpgrade || versionInfo.firmwareVersion !== packageJson.firmwareVersion) {
this.logService.misc('[DeviceService] New Dongle firmware checksum:',
deviceConfig.md5);

if (data.forceUpgrade || versionInfo.firmwareChecksum !== deviceConfig.md5) {
event.sender.send(IpcEvents.device.moduleFirmwareUpgrading, UHK_DONGLE.name);
await dongleOperations.updateDeviceFirmware(dongleFirmwarePath, UHK_DONGLE);
this.logService.misc('[DeviceService] Waiting for keyboard');
Expand Down Expand Up @@ -451,9 +462,14 @@ export class DeviceService {
JSON.stringify(uhkDeviceProduct, usbDeviceJsonFormatter));
const deviceFirmwarePath = getDeviceFirmwarePath(uhkDeviceProduct, packageJson);

this.logService.misc('[DeviceService] Device right firmware version:',
hardwareModules.rightModuleInfo.firmwareVersion);
if (data.forceUpgrade || hardwareModules.rightModuleInfo.firmwareVersion !== packageJson.firmwareVersion) {
this.logService.misc('[DeviceService] Current Device right firmware checksum:',
hardwareModules.rightModuleInfo.firmwareChecksum);

const deviceConfig = findDeviceConfigInFirmwareJson(uhkDeviceProduct, packageJson);
this.logService.misc('[DeviceService] New Device right firmware checksum:',
deviceConfig.md5);

if (data.forceUpgrade || hardwareModules.rightModuleInfo.firmwareChecksum !== deviceConfig.md5) {
event.sender.send(IpcEvents.device.moduleFirmwareUpgrading, RIGHT_HALF_FIRMWARE_UPGRADE_MODULE_NAME);
await this.operations.updateDeviceFirmware(deviceFirmwarePath, uhkDeviceProduct);
this.logService.misc('[DeviceService] Waiting for keyboard');
Expand Down
6 changes: 6 additions & 0 deletions packages/uhk-agent/src/util/command-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const optionDefinitions: commandLineArgs.OptionDefinition[] = [
{ name: 'devtools', type: Boolean },
{ name: 'disable-agent-update-protection', type: Boolean },
{ name: 'error-simulation', type: String },
{ name: 'ignore-firmware-checksums', type: Boolean },
{ name: 'log', type: String },
{ name: 'help', type: Boolean },
{ name: 'pid', type: Number },
Expand Down Expand Up @@ -48,6 +49,11 @@ const sections: commandLineUsage.Section[] = [
description: 'Simulate an error',
typeLabel: 'invalid-user-config'
},
{
name: 'ignore-firmware-checksums',
description: 'Always upgrade firmware of devices and modules even the checksums are same',
type: Boolean
},
{
name: 'log',
description: 'Set logging categories. --log=misc,usb. Default is "misc"',
Expand Down
5 changes: 5 additions & 0 deletions packages/uhk-common/src/models/command-line-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export interface CommandLineArgs extends DeviceIdentifier {
*/
'error-simulation'?: string;

/**
* Always upgrade firmware of devices and modules even the checksums are same
*/
'ignore-firmware-checksums'?: boolean;

/**
* logging categories
*/
Expand Down
11 changes: 11 additions & 0 deletions packages/uhk-usb/src/utils/find-device-config-in-firmware-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { FirmwareJson, FirmwareJsonDevice, UhkDeviceProduct} from 'uhk-common';

export function findDeviceConfigInFirmwareJson(device: UhkDeviceProduct, firmwareJson: FirmwareJson): FirmwareJsonDevice {
const deviceConfig = firmwareJson.devices.find(firmwareDevice => firmwareDevice.deviceId === device.id);

if (!deviceConfig) {
throw new Error(`The user config does not support: ${device.name}`);
}

return deviceConfig;
}
8 changes: 3 additions & 5 deletions packages/uhk-usb/src/utils/get-device-user-config-path.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { dirname, join } from 'path';
import { FirmwareJson, UhkDeviceProduct } from 'uhk-common';

export function getDeviceUserConfigPath(device: UhkDeviceProduct, firmwareJson: FirmwareJson): string {
const deviceConfig = firmwareJson.devices.find(firmwareDevice => firmwareDevice.deviceId === device.id);
import { findDeviceConfigInFirmwareJson } from './find-device-config-in-firmware-json.js';

if (!deviceConfig) {
throw new Error(`The user config does not support: ${device.name}`);
}
export function getDeviceUserConfigPath(device: UhkDeviceProduct, firmwareJson: FirmwareJson): string {
const deviceConfig = findDeviceConfigInFirmwareJson(device, firmwareJson);

return join(dirname(firmwareJson.path), 'devices', deviceConfig.name, 'config.bin');
}
1 change: 1 addition & 0 deletions packages/uhk-usb/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './convert-ms-to-duration.js';
export * from './convert-slave-i2c-error-buffer.js';
export * from './device-vid-pid-interface-filter.js';
export * from './find-device-by-device-identifier.js';
export * from './find-device-config-in-firmware-json.js';
export * from './get-current-uhk-device-product.js';
export * from './get-current-uhk-dongle-HID.js';
export * from './get-current-uhk-80-left-HID.js';
Expand Down

0 comments on commit 90c46df

Please sign in to comment.