Skip to content

Commit 7f1ab24

Browse files
committed
chore: usb scripts can be executed with vid,pid
1 parent 0f7ad47 commit 7f1ab24

8 files changed

Lines changed: 93 additions & 9 deletions

File tree

packages/uhk-agent/src/util/command-line.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import commandLineArgs from 'command-line-args';
22
import commandLineUsage from 'command-line-usage';
33
import { CommandLineArgs } from 'uhk-common';
4+
import { assertCommandLineOptions } from 'uhk-usb';
45

56
const optionDefinitions: commandLineArgs.OptionDefinition[] = [
67
{ name: 'devtools', type: Boolean },
@@ -9,14 +10,18 @@ const optionDefinitions: commandLineArgs.OptionDefinition[] = [
910
{ name: 'log', type: String },
1011
{ name: 'modules', type: Boolean },
1112
{ name: 'help', type: Boolean },
13+
{ name: 'pid', type: Number },
1214
{ name: 'preserve-udev-rules', type: Boolean },
1315
{ name: 'print-usb-devices', type: Boolean },
1416
{ name: 'reenumerate-and-exit', type: String },
1517
{ name: 'spe', type: Boolean }, // simulate privilege escalation error
16-
{ name: 'usb-non-blocking', type: Boolean }
18+
{ name: 'usb-interface', type: Number },
19+
{ name: 'usb-non-blocking', type: Boolean },
20+
{ name: 'vid', type: Number },
1721
];
1822

1923
export const options: CommandLineArgs = commandLineArgs(optionDefinitions, { partial: true }) as CommandLineArgs;
24+
assertCommandLineOptions(options);
2025

2126
const sections: commandLineUsage.Section[] = [
2227
{
@@ -51,6 +56,11 @@ const sections: commandLineUsage.Section[] = [
5156
description: 'Make the modules menu visible',
5257
type: Boolean
5358
},
59+
{
60+
name: 'pid',
61+
description: 'Use the specified USB product id. If you set it you have to set the vid and usb-interface too.',
62+
type: Number
63+
},
5464
{
5565
name: 'preserve-udev-rules',
5666
description: 'Don\'t force udev rule update',
@@ -73,10 +83,20 @@ const sections: commandLineUsage.Section[] = [
7383
description: 'Simulate privilege escalation error',
7484
type: Boolean
7585
},
86+
{
87+
name: 'usb-interface',
88+
description: 'Use the specified USB interface id. If you set it you have to set the vid and pid too.',
89+
type: Number
90+
},
7691
{
7792
name: 'usb-non-blocking',
7893
description: 'Use USB non-blocking communication',
7994
type: Boolean
95+
},
96+
{
97+
name: 'vid',
98+
description: 'Use the specified USB vendor id. If you set it you have to set the pid and usb-interface too.',
99+
type: Number
80100
}
81101
]
82102
}

packages/uhk-common/src/models/command-line-args.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export interface CommandLineArgs {
2626
* show help.
2727
*/
2828
help?: boolean;
29+
/**
30+
* USB product id
31+
*/
32+
pid?: number;
2933
/**
3034
* Agent not force the udev rule update
3135
*/
@@ -43,8 +47,16 @@ export interface CommandLineArgs {
4347
* simulate privilege escalation error
4448
*/
4549
spe?: boolean;
50+
/**
51+
* USB interface id
52+
*/
53+
'usb-interface'?: number;
4654
/**
4755
* Use USB non-blocking communication
4856
*/
4957
'usb-non-blocking'?: boolean;
58+
/**
59+
* USB vendor id
60+
*/
61+
vid?: number;
5062
}

packages/uhk-usb/src/uhk-hid-device.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
import { DeviceState, GetDeviceOptions, ReenumerateOption } from './models/index.js';
3737
import {
3838
calculateHalvesState,
39+
findDeviceByOptions,
3940
getNumberOfConnectedDevices,
4041
getUhkDevices,
4142
usbDeviceJsonFormatter
@@ -92,10 +93,12 @@ export class UhkHidDevice {
9293
}
9394

9495
this.logService.misc('[UhkHidDevice] Devices before checking permission:');
95-
const devs = getUhkDevices();
96+
const devs = getUhkDevices(this.options.vid);
9697
this.listAvailableDevices(devs);
9798

98-
const dev = devs.find((x: Device) => isUhkZeroInterface(x) || isBootloader(x));
99+
const dev = this.options.vid
100+
? devs.find(findDeviceByOptions(this.options))
101+
: devs.find((x: Device) => isUhkZeroInterface(x) || isBootloader(x));
99102

100103
if (!dev) {
101104
return true;
@@ -119,7 +122,7 @@ export class UhkHidDevice {
119122
* @returns {DeviceConnectionState}
120123
*/
121124
public async getDeviceConnectionStateAsync(): Promise<DeviceConnectionState> {
122-
const devs = getUhkDevices();
125+
const devs = getUhkDevices(this.options.vid);
123126
const result: DeviceConnectionState = {
124127
bootloaderActive: false,
125128
zeroInterfaceAvailable: false,
@@ -232,7 +235,7 @@ export class UhkHidDevice {
232235
let jumped = false;
233236

234237
while (new Date().getTime() - startTime.getTime() < waitTimeout) {
235-
const devs = getUhkDevices();
238+
const devs = getUhkDevices(vendorId);
236239

237240
const inBootloaderMode = devs.some((x: Device) =>
238241
x.vendorId === vendorId &&
@@ -414,10 +417,12 @@ export class UhkHidDevice {
414417
*/
415418
private connectToDevice({ errorLogLevel = 'error' }: GetDeviceOptions = {}): HID {
416419
try {
417-
const devs = getUhkDevices();
420+
const devs = getUhkDevices(this.options.vid);
418421
this.listAvailableDevices(devs);
419422

420-
const dev = devs.find(isUhkZeroInterface);
423+
const dev = this.options.vid
424+
? devs.find(findDeviceByOptions(this.options))
425+
: devs.find(isUhkZeroInterface);
421426

422427
if (!dev) {
423428
this.logService.misc('[UhkHidDevice] UHK Device not found:');
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { CommandLineArgs } from 'uhk-common';
2+
3+
const USB_PROPERTIES = ['vid', 'pid', 'usb-interface'];
4+
5+
export function assertCommandLineOptions (options: CommandLineArgs) {
6+
let anyUsbOption = false;
7+
let allUsbOptions = true;
8+
9+
for (const usbProperty of USB_PROPERTIES) {
10+
if (options.hasOwnProperty(usbProperty)) {
11+
anyUsbOption = true;
12+
} else {
13+
allUsbOptions = false;
14+
}
15+
}
16+
17+
if (anyUsbOption && !allUsbOptions) {
18+
throw new Error('You have to set all of the following options: vid, pid, usb-interface');
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Device } from 'node-hid';
2+
import { CommandLineArgs } from 'uhk-common';
3+
4+
export function findDeviceByOptions(options: CommandLineArgs): (device: Device) => boolean {
5+
return (device: Device) => {
6+
return device.vendorId === options.vid
7+
&& device.productId === options.pid
8+
&& device.interface === options['usb-interface'];
9+
};
10+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Device, devices } from 'node-hid';
22
import { UHK_VENDOR_ID } from 'uhk-common';
33

4-
export function getUhkDevices(): Array<Device> {
5-
return devices().filter(x => x.vendorId === UHK_VENDOR_ID);
4+
export function getUhkDevices(vendorId: number = UHK_VENDOR_ID): Array<Device> {
5+
return devices().filter(x => x.vendorId === vendorId);
66
}

packages/uhk-usb/src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
export * from './assert-command-line-options.js';
12
export * from './check-firmware-and-device-compatibility.js';
23
export * from './calculate-halves-state.js';
34
export * from './convert-ms-to-duration.js';
45
export * from './convert-slave-i2c-error-buffer.js';
6+
export * from './find-device-by-options.js';
57
export * from './get-current-uhk-device-product.js';
68
export * from './get-current-uhk-device-product-by-bootloader-id.js';
79
export * from './get-device-enumerate-product-id.js';

packages/usb/src/command-line.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { assertCommandLineOptions } from 'uhk-usb';
12
import Yargs from 'yargs';
23
import { hideBin } from 'yargs/helpers';
34

@@ -12,11 +13,25 @@ export const yargs = Yargs(hideBin(process.argv))
1213
default: 'none',
1314
choices: ['all', 'config', 'misc', 'none', 'usb']
1415
})
16+
.option('pid', {
17+
description: 'Set USB product id. If you set it you have to set the vid and usb-interface too.',
18+
type: 'number'
19+
})
1520
.option('usb-non-blocking', {
1621
description: 'Use USB non blocking communication',
1722
type: 'boolean',
1823
default: false
1924
})
25+
.option('vid', {
26+
description: 'Set USB vendor id. If you set it you have to set the pid and usb-interface too.',
27+
type: 'number'
28+
})
29+
.option('usb-interface', {
30+
description: 'Set USB interface id. If you set it you have to set the vid and pid too.',
31+
type: 'number'
32+
})
2033
.help('help')
2134
.version(false)
2235
;
36+
37+
assertCommandLineOptions(yargs.argv);

0 commit comments

Comments
 (0)