Skip to content

Commit

Permalink
chore: usb scripts can be executed with vid,pid
Browse files Browse the repository at this point in the history
  • Loading branch information
ert78gb committed Dec 21, 2023
1 parent 0f7ad47 commit 7f1ab24
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 9 deletions.
22 changes: 21 additions & 1 deletion packages/uhk-agent/src/util/command-line.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import commandLineArgs from 'command-line-args';
import commandLineUsage from 'command-line-usage';
import { CommandLineArgs } from 'uhk-common';
import { assertCommandLineOptions } from 'uhk-usb';

const optionDefinitions: commandLineArgs.OptionDefinition[] = [
{ name: 'devtools', type: Boolean },
Expand All @@ -9,14 +10,18 @@ const optionDefinitions: commandLineArgs.OptionDefinition[] = [
{ name: 'log', type: String },
{ name: 'modules', type: Boolean },
{ name: 'help', type: Boolean },
{ name: 'pid', type: Number },
{ name: 'preserve-udev-rules', type: Boolean },
{ name: 'print-usb-devices', type: Boolean },
{ name: 'reenumerate-and-exit', type: String },
{ name: 'spe', type: Boolean }, // simulate privilege escalation error
{ name: 'usb-non-blocking', type: Boolean }
{ name: 'usb-interface', type: Number },
{ name: 'usb-non-blocking', type: Boolean },
{ name: 'vid', type: Number },
];

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

const sections: commandLineUsage.Section[] = [
{
Expand Down Expand Up @@ -51,6 +56,11 @@ const sections: commandLineUsage.Section[] = [
description: 'Make the modules menu visible',
type: Boolean
},
{
name: 'pid',
description: 'Use the specified USB product id. If you set it you have to set the vid and usb-interface too.',
type: Number
},
{
name: 'preserve-udev-rules',
description: 'Don\'t force udev rule update',
Expand All @@ -73,10 +83,20 @@ const sections: commandLineUsage.Section[] = [
description: 'Simulate privilege escalation error',
type: Boolean
},
{
name: 'usb-interface',
description: 'Use the specified USB interface id. If you set it you have to set the vid and pid too.',
type: Number
},
{
name: 'usb-non-blocking',
description: 'Use USB non-blocking communication',
type: Boolean
},
{
name: 'vid',
description: 'Use the specified USB vendor id. If you set it you have to set the pid and usb-interface too.',
type: Number
}
]
}
Expand Down
12 changes: 12 additions & 0 deletions packages/uhk-common/src/models/command-line-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export interface CommandLineArgs {
* show help.
*/
help?: boolean;
/**
* USB product id
*/
pid?: number;
/**
* Agent not force the udev rule update
*/
Expand All @@ -43,8 +47,16 @@ export interface CommandLineArgs {
* simulate privilege escalation error
*/
spe?: boolean;
/**
* USB interface id
*/
'usb-interface'?: number;
/**
* Use USB non-blocking communication
*/
'usb-non-blocking'?: boolean;
/**
* USB vendor id
*/
vid?: number;
}
17 changes: 11 additions & 6 deletions packages/uhk-usb/src/uhk-hid-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
import { DeviceState, GetDeviceOptions, ReenumerateOption } from './models/index.js';
import {
calculateHalvesState,
findDeviceByOptions,
getNumberOfConnectedDevices,
getUhkDevices,
usbDeviceJsonFormatter
Expand Down Expand Up @@ -92,10 +93,12 @@ export class UhkHidDevice {
}

this.logService.misc('[UhkHidDevice] Devices before checking permission:');
const devs = getUhkDevices();
const devs = getUhkDevices(this.options.vid);
this.listAvailableDevices(devs);

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

if (!dev) {
return true;
Expand All @@ -119,7 +122,7 @@ export class UhkHidDevice {
* @returns {DeviceConnectionState}
*/
public async getDeviceConnectionStateAsync(): Promise<DeviceConnectionState> {
const devs = getUhkDevices();
const devs = getUhkDevices(this.options.vid);
const result: DeviceConnectionState = {
bootloaderActive: false,
zeroInterfaceAvailable: false,
Expand Down Expand Up @@ -232,7 +235,7 @@ export class UhkHidDevice {
let jumped = false;

while (new Date().getTime() - startTime.getTime() < waitTimeout) {
const devs = getUhkDevices();
const devs = getUhkDevices(vendorId);

const inBootloaderMode = devs.some((x: Device) =>
x.vendorId === vendorId &&
Expand Down Expand Up @@ -414,10 +417,12 @@ export class UhkHidDevice {
*/
private connectToDevice({ errorLogLevel = 'error' }: GetDeviceOptions = {}): HID {
try {
const devs = getUhkDevices();
const devs = getUhkDevices(this.options.vid);
this.listAvailableDevices(devs);

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

if (!dev) {
this.logService.misc('[UhkHidDevice] UHK Device not found:');
Expand Down
20 changes: 20 additions & 0 deletions packages/uhk-usb/src/utils/assert-command-line-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { CommandLineArgs } from 'uhk-common';

const USB_PROPERTIES = ['vid', 'pid', 'usb-interface'];

export function assertCommandLineOptions (options: CommandLineArgs) {
let anyUsbOption = false;
let allUsbOptions = true;

for (const usbProperty of USB_PROPERTIES) {
if (options.hasOwnProperty(usbProperty)) {
anyUsbOption = true;
} else {
allUsbOptions = false;
}
}

if (anyUsbOption && !allUsbOptions) {
throw new Error('You have to set all of the following options: vid, pid, usb-interface');
}
}
10 changes: 10 additions & 0 deletions packages/uhk-usb/src/utils/find-device-by-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Device } from 'node-hid';
import { CommandLineArgs } from 'uhk-common';

export function findDeviceByOptions(options: CommandLineArgs): (device: Device) => boolean {
return (device: Device) => {
return device.vendorId === options.vid
&& device.productId === options.pid
&& device.interface === options['usb-interface'];
};
}
4 changes: 2 additions & 2 deletions packages/uhk-usb/src/utils/get-uhk-devices.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Device, devices } from 'node-hid';
import { UHK_VENDOR_ID } from 'uhk-common';

export function getUhkDevices(): Array<Device> {
return devices().filter(x => x.vendorId === UHK_VENDOR_ID);
export function getUhkDevices(vendorId: number = UHK_VENDOR_ID): Array<Device> {
return devices().filter(x => x.vendorId === vendorId);
}
2 changes: 2 additions & 0 deletions packages/uhk-usb/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export * from './assert-command-line-options.js';
export * from './check-firmware-and-device-compatibility.js';
export * from './calculate-halves-state.js';
export * from './convert-ms-to-duration.js';
export * from './convert-slave-i2c-error-buffer.js';
export * from './find-device-by-options.js';
export * from './get-current-uhk-device-product.js';
export * from './get-current-uhk-device-product-by-bootloader-id.js';
export * from './get-device-enumerate-product-id.js';
Expand Down
15 changes: 15 additions & 0 deletions packages/usb/src/command-line.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assertCommandLineOptions } from 'uhk-usb';
import Yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

Expand All @@ -12,11 +13,25 @@ export const yargs = Yargs(hideBin(process.argv))
default: 'none',
choices: ['all', 'config', 'misc', 'none', 'usb']
})
.option('pid', {
description: 'Set USB product id. If you set it you have to set the vid and usb-interface too.',
type: 'number'
})
.option('usb-non-blocking', {
description: 'Use USB non blocking communication',
type: 'boolean',
default: false
})
.option('vid', {
description: 'Set USB vendor id. If you set it you have to set the pid and usb-interface too.',
type: 'number'
})
.option('usb-interface', {
description: 'Set USB interface id. If you set it you have to set the vid and pid too.',
type: 'number'
})
.help('help')
.version(false)
;

assertCommandLineOptions(yargs.argv);

0 comments on commit 7f1ab24

Please sign in to comment.