From fa444de2caa694a3e01233a2b746e94f7f07d6e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kiss=20R=C3=B3bert?= Date: Wed, 8 Jan 2025 19:29:00 +0100 Subject: [PATCH] chore: make usb-interface command line argument optional --- packages/uhk-agent/src/util/command-line.ts | 4 +-- .../src/utils/assert-command-line-options.ts | 27 +++++++++++-------- .../utils/device-vid-pid-interface-filter.ts | 8 +++++- packages/usb/src/command-line.ts | 4 +-- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/packages/uhk-agent/src/util/command-line.ts b/packages/uhk-agent/src/util/command-line.ts index bf6b55d4055..57aa5bf0a6a 100644 --- a/packages/uhk-agent/src/util/command-line.ts +++ b/packages/uhk-agent/src/util/command-line.ts @@ -66,7 +66,7 @@ const sections: commandLineUsage.Section[] = [ }, { name: 'pid', - description: 'Use the specified USB product id. If you set it you have to set the vid and usb-interface too.', + description: 'Use the specified USB product id. If you set it you have to set the vid too.', type: Number }, { @@ -112,7 +112,7 @@ const sections: commandLineUsage.Section[] = [ }, { name: 'vid', - description: 'Use the specified USB vendor id. If you set it you have to set the pid and usb-interface too.', + description: 'Use the specified USB vendor id. If you set it you have to set the pid too.', type: Number } ] diff --git a/packages/uhk-usb/src/utils/assert-command-line-options.ts b/packages/uhk-usb/src/utils/assert-command-line-options.ts index 2713cfe5abe..98df36cafca 100644 --- a/packages/uhk-usb/src/utils/assert-command-line-options.ts +++ b/packages/uhk-usb/src/utils/assert-command-line-options.ts @@ -1,21 +1,26 @@ import { CommandLineArgs } from 'uhk-common'; -const USB_PROPERTIES = ['vid', 'pid', 'usb-interface']; - export function assertCommandLineOptions (options: CommandLineArgs) { - let anyUsbOption = false; - let allUsbOptions = true; + if (options['usb-interface'] !== null && options['usb-interface'] !== undefined) { + if (!options.vid && !options.pid) { + throw new Error('You have to set the vid, pid commandline options too'); + } + + if (!options.vid) { + throw new Error('You have to set the vid commandline options too'); + } - for (const usbProperty of USB_PROPERTIES) { - if (options.hasOwnProperty(usbProperty)) { - anyUsbOption = true; - } else { - allUsbOptions = false; + if (!options.pid) { + throw new Error('You have to set the pid commandline options too'); } } - if (anyUsbOption && !allUsbOptions) { - throw new Error('You have to set all of the following options: vid, pid, usb-interface'); + if (options.vid && !options.pid) { + throw new Error('You have to set the pid commandline options too'); + } + + if (!options.vid && options.pid) { + throw new Error('You have to set the vid commandline options too'); } if (options['report-id'] !== null && options['report-id'] !== undefined && options['no-report-id'] === true) { diff --git a/packages/uhk-usb/src/utils/device-vid-pid-interface-filter.ts b/packages/uhk-usb/src/utils/device-vid-pid-interface-filter.ts index 9514798fe52..d82fe84b6e7 100644 --- a/packages/uhk-usb/src/utils/device-vid-pid-interface-filter.ts +++ b/packages/uhk-usb/src/utils/device-vid-pid-interface-filter.ts @@ -1,10 +1,16 @@ import { Device } from 'node-hid'; import { DeviceIdentifier } from 'uhk-common'; +import { isUhkCommunicationUsage } from '../util.js'; + export function deviceVidPidInterfaceFilter(deviceIdentifier: DeviceIdentifier): (device: Device) => boolean { + const isInterfaceProvided = deviceIdentifier['usb-interface'] !== null && deviceIdentifier['usb-interface'] !== undefined; + return (device: Device): boolean => { return device.vendorId === deviceIdentifier.vid && device.productId === deviceIdentifier.pid - && device.interface === deviceIdentifier['usb-interface']; + && ((isInterfaceProvided && device.interface === deviceIdentifier['usb-interface']) + || (!isInterfaceProvided && isUhkCommunicationUsage(device)) + ); }; } diff --git a/packages/usb/src/command-line.ts b/packages/usb/src/command-line.ts index 2fbc8e75fab..0b8af3fd7cd 100644 --- a/packages/usb/src/command-line.ts +++ b/packages/usb/src/command-line.ts @@ -27,7 +27,7 @@ export const yargs = Yargs(hideBin(process.argv)) default: false, }) .option('pid', { - description: 'Set USB product id. If you set it you have to set the vid and usb-interface too.', + description: 'Set USB product id. If you set it you have to set the vid too.', type: 'number' }) .option('report-id', { @@ -44,7 +44,7 @@ export const yargs = Yargs(hideBin(process.argv)) default: false }) .option('vid', { - description: 'Set USB vendor id. If you set it you have to set the pid and usb-interface too.', + description: 'Set USB vendor id. If you set it you have to set the pid too.', type: 'number' }) .option('usb-interface', {