Skip to content

Commit

Permalink
chore: make usb-interface command line argument optional (#2484)
Browse files Browse the repository at this point in the history
  • Loading branch information
ert78gb authored Jan 13, 2025
1 parent 72e28d4 commit dedc3d7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/uhk-agent/src/util/command-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
{
Expand Down Expand Up @@ -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
}
]
Expand Down
27 changes: 16 additions & 11 deletions packages/uhk-usb/src/utils/assert-command-line-options.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
);
};
}
4 changes: 2 additions & 2 deletions packages/usb/src/command-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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', {
Expand Down

0 comments on commit dedc3d7

Please sign in to comment.