diff --git a/packages/uhk-common/src/models/device-connection-state.ts b/packages/uhk-common/src/models/device-connection-state.ts index a366ef56..7ecf2965 100644 --- a/packages/uhk-common/src/models/device-connection-state.ts +++ b/packages/uhk-common/src/models/device-connection-state.ts @@ -23,5 +23,10 @@ export interface DeviceConnectionState { communicationInterfaceAvailable: boolean; halvesInfo: HalvesInfo; hardwareModules?: HardwareModules; + /** + * Each element describe the hostConnection is the user-config are paired to the keyboard or not. + * If the value is 1 then paired. + */ + pairedDevices: number[]; udevRulesInfo: UdevRulesInfo; } diff --git a/packages/uhk-common/src/util/index.ts b/packages/uhk-common/src/util/index.ts index 53724a50..efea673b 100644 --- a/packages/uhk-common/src/util/index.ts +++ b/packages/uhk-common/src/util/index.ts @@ -11,6 +11,7 @@ export * from './get-formatted-timestamp.js'; export * from './get-md5-hash-from-file-name.js'; export * from './get-slot-id-name.js'; export * from './helpers.js'; +export * from './is-bit-set.js'; export * from './is-device-protocol-support-firmware-checksum.js'; export * from './is-device-protocol-support-git-info.js'; export * from './is-device-protocol-support-status-error.js'; diff --git a/packages/uhk-common/src/util/is-bit-set.ts b/packages/uhk-common/src/util/is-bit-set.ts new file mode 100644 index 00000000..5a14bdda --- /dev/null +++ b/packages/uhk-common/src/util/is-bit-set.ts @@ -0,0 +1,3 @@ +export function isBitSet(value: number, bitPosition: number): boolean { + return (value & (1 << bitPosition)) !== 0; +} diff --git a/packages/uhk-usb/src/models/device-state.ts b/packages/uhk-usb/src/models/device-state.ts index 3967bcc5..dc80a62a 100644 --- a/packages/uhk-usb/src/models/device-state.ts +++ b/packages/uhk-usb/src/models/device-state.ts @@ -8,5 +8,6 @@ export interface DeviceState { activeLayerToggled: boolean; leftKeyboardHalfSlot: string; leftModuleSlot: string; + newPairedDevice: boolean; rightModuleSlot: string; } diff --git a/packages/uhk-usb/src/uhk-hid-device.ts b/packages/uhk-usb/src/uhk-hid-device.ts index 1813a8ce..1ddc0c2e 100644 --- a/packages/uhk-usb/src/uhk-hid-device.ts +++ b/packages/uhk-usb/src/uhk-hid-device.ts @@ -12,6 +12,8 @@ import { DeviceConnectionState, FIRMWARE_UPGRADE_METHODS, HalvesInfo, + HOST_CONNECTION_COUNT_MAX, + isBitSet, isEqualArray, LeftSlotModules, LogService, @@ -337,6 +339,7 @@ export class UhkHidDevice { isMacroStatusDirty: false, leftHalfDetected: false, multiDevice: await getNumberOfConnectedDevices(this.options) > 1, + pairedDevices: [], udevRulesInfo: await this.getUdevInfoAsync(), }; @@ -414,6 +417,10 @@ export class UhkHidDevice { const deviceState = await this.getDeviceState(); result.halvesInfo = calculateHalvesState(deviceState); result.isMacroStatusDirty = deviceState.isMacroStatusDirty; + + if (deviceState.newPairedDevice) { + result.pairedDevices = await this.getPairedDevices(); + } } else if (!result.connectedDevice) { this._device = undefined; } @@ -608,19 +615,21 @@ export class UhkHidDevice { } async getDeviceState(): Promise { + this.logService.usb('[UhkHidDevice] USB[T]: Get device state'); const buffer = await this.write(Buffer.from([UsbCommand.GetDeviceState])); const activeLayerNumber = buffer[6] & 0x7f; return { isEepromBusy: buffer[1] !== 0, isMacroStatusDirty: buffer[7] !== 0, - areHalvesMerged: (buffer[2] & 0x1) !== 0, + areHalvesMerged: isBitSet(buffer[2], 0), isLeftHalfConnected: buffer[3] !== 0, activeLayerNumber, activeLayerName: LAYER_NUMBER_TO_STRING[activeLayerNumber], activeLayerToggled: (buffer[6] & 0x80) === 1, leftKeyboardHalfSlot: MODULE_ID_TO_STRING[buffer[3]], leftModuleSlot: MODULE_ID_TO_STRING[buffer[4]], + newPairedDevice: isBitSet(buffer[2], 2), rightModuleSlot: MODULE_ID_TO_STRING[buffer[5]] }; } @@ -673,6 +682,15 @@ export class UhkHidDevice { } } + async getPairedDevices(): Promise { + this.logService.usb('[UhkHidDevice] USB[T]: Read paired devices'); + const command = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.NewPairings]); + const buffer = await this.write(command); + const pairedDevices = convertBufferToIntArray(buffer); + + return pairedDevices.slice(0, HOST_CONNECTION_COUNT_MAX + 1); + } + async getProtocolVersions(): Promise { if (this._protocolVersions) { return this._protocolVersions; diff --git a/packages/usb/get-device-state.ts b/packages/usb/get-device-state.ts index 18921181..9c4b76d7 100755 --- a/packages/usb/get-device-state.ts +++ b/packages/usb/get-device-state.ts @@ -15,6 +15,7 @@ setInterval(async function () { `isEepromBusy: ${state.isEepromBusy ? 'yes' : 'no'} | \ isMacroStatusDirty: ${state.isMacroStatusDirty ? 'yes' : 'no'} | \ areHalvesMerged: ${state.areHalvesMerged ? 'yes' : 'no'} | \ +newPairedDevice: ${state.newPairedDevice ? 'yes' : 'no'} | \ leftKeyboardHalfSlot:${state.leftKeyboardHalfSlot} | \ leftModuleSlot:${state.leftModuleSlot} | \ rightModuleSlot:${state.rightModuleSlot} | \