Skip to content

Commit

Permalink
fix: use only Device.usage and Device.usagePage to identify UHK commu…
Browse files Browse the repository at this point in the history
…nication interface
  • Loading branch information
ert78gb committed Jan 6, 2024
1 parent 7f1ab24 commit 5817d34
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 41 deletions.
2 changes: 1 addition & 1 deletion packages/uhk-agent/src/services/device.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ export class DeviceService {
if (!isEqual(state, savedState)) {
const newState = cloneDeep(state);

if (state.hasPermission && state.zeroInterfaceAvailable) {
if (state.hasPermission && state.communicationInterfaceAvailable) {
state.hardwareModules = await this.getHardwareModules(false);
deviceProtocolVersion = state.hardwareModules.rightModuleInfo.deviceProtocolVersion;
this._checkStatusBuffer = true;
Expand Down
2 changes: 1 addition & 1 deletion packages/uhk-common/src/models/device-connection-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface DeviceConnectionState {
* True if more then 1 UHK device connected.
*/
multiDevice: boolean;
zeroInterfaceAvailable: boolean;
communicationInterfaceAvailable: boolean;
halvesInfo: HalvesInfo;
hardwareModules?: HardwareModules;
}
14 changes: 7 additions & 7 deletions packages/uhk-usb/src/uhk-hid-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
getTransferData,
isBootloader,
getUhkDevice,
isUhkZeroInterface,
isUhkCommunicationInterface,
retry,
snooze
} from './util.js';
Expand Down Expand Up @@ -98,7 +98,7 @@ export class UhkHidDevice {

const dev = this.options.vid
? devs.find(findDeviceByOptions(this.options))
: devs.find((x: Device) => isUhkZeroInterface(x) || isBootloader(x));
: devs.find((x: Device) => isUhkCommunicationInterface(x) || isBootloader(x));

if (!dev) {
return true;
Expand All @@ -125,7 +125,7 @@ export class UhkHidDevice {
const devs = getUhkDevices(this.options.vid);
const result: DeviceConnectionState = {
bootloaderActive: false,
zeroInterfaceAvailable: false,
communicationInterfaceAvailable: false,
hasPermission: this.hasPermission(),
halvesInfo: {
areHalvesMerged: true,
Expand All @@ -147,14 +147,14 @@ export class UhkHidDevice {
result.connectedDevice = getUhkDevice(dev);
}

if (isUhkZeroInterface(dev)) {
result.zeroInterfaceAvailable = true;
if (isUhkCommunicationInterface(dev)) {
result.communicationInterfaceAvailable = true;
} else if (isBootloader(dev)) {
result.bootloaderActive = true;
}
}

if (result.connectedDevice && result.hasPermission && result.zeroInterfaceAvailable) {
if (result.connectedDevice && result.hasPermission && result.communicationInterfaceAvailable) {
const deviceState = await this.getDeviceState();
result.halvesInfo = calculateHalvesState(deviceState);
result.isMacroStatusDirty = deviceState.isMacroStatusDirty;
Expand Down Expand Up @@ -422,7 +422,7 @@ export class UhkHidDevice {

const dev = this.options.vid
? devs.find(findDeviceByOptions(this.options))
: devs.find(isUhkZeroInterface);
: devs.find(isUhkCommunicationInterface);

if (!dev) {
this.logService.misc('[UhkHidDevice] UHK Device not found:');
Expand Down
6 changes: 2 additions & 4 deletions packages/uhk-usb/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import MemoryMap from 'nrf-intel-hex';
import { Buffer, LogService, UHK_DEVICES, UhkDeviceProduct } from 'uhk-common';

import { Constants, UsbCommand } from './constants.js';
import isOsProvideUsbInterface from './utils/is-os-provide-usb-interface.js';

export const snooze = ms => new Promise(resolve => setTimeout(resolve, ms));

Expand Down Expand Up @@ -102,12 +101,11 @@ export async function retry(command: Function, maxTry = 3, logService?: LogServi
}
}

export const isUhkZeroInterface = (dev: Device): boolean => {
export const isUhkCommunicationInterface = (dev: Device): boolean => {
return UHK_DEVICES.some(device => dev.vendorId === device.vendorId &&
dev.productId === device.keyboardPid &&
((dev.usagePage === 128 && dev.usage === 129) || // Old firmware
(dev.usagePage === (0xFF00 | 0x00) && dev.usage === 0x01) || // New firmware
(dev.interface === 0 && isOsProvideUsbInterface())
(dev.usagePage === 65280 && dev.usage === 1) // New firmware
)
);
};
Expand Down
4 changes: 2 additions & 2 deletions packages/uhk-usb/src/utils/get-number-of-connected-devices.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { devices } from 'node-hid';

import { isBootloader, isUhkZeroInterface } from '../util.js';
import { isBootloader, isUhkCommunicationInterface } from '../util.js';

export function getNumberOfConnectedDevices(): number {
return devices()
.filter(dev => isUhkZeroInterface(dev) || isBootloader(dev))
.filter(dev => isUhkCommunicationInterface(dev) || isBootloader(dev))
.length;
}
18 changes: 0 additions & 18 deletions packages/uhk-usb/src/utils/is-os-provide-usb-interface.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/uhk-web/src/app/store/effects/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class DeviceEffects {
return this.router.navigate(['/update-firmware']);
}

if (state.connectedDevice && state.zeroInterfaceAvailable) {
if (state.connectedDevice && state.communicationInterfaceAvailable) {
const allowDefaultNavigation = [
'/detection',
'/privilege',
Expand All @@ -161,14 +161,14 @@ export class DeviceEffects {

return prevConnected === currConnected &&
prevAction.payload.hasPermission === currAction.payload.hasPermission &&
prevAction.payload.zeroInterfaceAvailable === currAction.payload.zeroInterfaceAvailable;
prevAction.payload.communicationInterfaceAvailable === currAction.payload.communicationInterfaceAvailable;
}),
mergeMap(([action, route, connected]) => {
const payload = action.payload;

if (connected
&& payload.hasPermission
&& payload.zeroInterfaceAvailable) {
&& payload.communicationInterfaceAvailable) {

const result: Array<Action> = [
new ReadConfigSizesAction(),
Expand Down
2 changes: 1 addition & 1 deletion packages/uhk-web/src/app/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export const deviceConnected = createSelector(
}

if (app.platform === 'linux') {
return device.connectedDevice && (device.zeroInterfaceAvailable || upgradingFirmware);
return device.connectedDevice && (device.communicationInterfaceAvailable || upgradingFirmware);
}

return !!device.connectedDevice;
Expand Down
8 changes: 4 additions & 4 deletions packages/uhk-web/src/app/store/reducers/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface State {
deviceConnectionStateLoaded: boolean;
keyboardHalvesAlwaysJoined: boolean;
multiDevice: boolean;
zeroInterfaceAvailable: boolean;
communicationInterfaceAvailable: boolean;
saveToKeyboard: ProgressButtonState;
modifiedConfigWhileSaved: boolean;
savingToKeyboard: boolean;
Expand All @@ -51,7 +51,7 @@ export const initialState: State = {
deviceConnectionStateLoaded: false,
keyboardHalvesAlwaysJoined: false,
multiDevice: false,
zeroInterfaceAvailable: true,
communicationInterfaceAvailable: true,
saveToKeyboard: initProgressButtonState,
modifiedConfigWhileSaved: false,
savingToKeyboard: false,
Expand Down Expand Up @@ -115,7 +115,7 @@ export function reducer(state = initialState, action: Action): State {
connectedDevice: data.connectedDevice,
deviceConnectionStateLoaded: true,
hasPermission: data.hasPermission,
zeroInterfaceAvailable: data.zeroInterfaceAvailable,
communicationInterfaceAvailable: data.communicationInterfaceAvailable,
bootloaderActive: data.bootloaderActive,
halvesInfo: data.halvesInfo,
modules: data.hardwareModules,
Expand Down Expand Up @@ -275,7 +275,7 @@ export const getMissingDeviceState = (state: State): MissingDeviceState => {
};
}

if (state.connectedDevice && !state.zeroInterfaceAvailable) {
if (state.connectedDevice && !state.communicationInterfaceAvailable) {
return {
header: 'Cannot find your UHK',
subtitle: 'Please reconnect it!'
Expand Down

0 comments on commit 5817d34

Please sign in to comment.