Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: make usb-interface command line argument optional #2484

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading