Skip to content

Commit

Permalink
Merge pull request #2444 from UltimateHackingKeyboard/chore-usb-ops
Browse files Browse the repository at this point in the history
chore: usbOps logging option
  • Loading branch information
mondalaci authored Dec 15, 2024
2 parents 31ef659 + bdd1c77 commit e8e615a
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 48 deletions.
10 changes: 8 additions & 2 deletions packages/uhk-agent/src/services/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ export class ElectronLogService extends LogService {
} else {
this.log('%c' + args.join(' '), 'color:red');
}
} else if (LogRegExps.transferRegExp.test(args[0])) {
this.log('%c' + args.join(' '), 'color:orange');
} else {
this.log(...args);
}
}

usbOps(...args) {
if (!this._options.usbOps) {
return;
}

this.log('%c' + args.join(' '), 'color:orange');
}

protected log(...args: any[]): void {
log.log(...args);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/uhk-agent/src/util/command-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const sections: commandLineUsage.Section[] = [
{
name: 'log',
description: 'Set logging categories. --log=misc,usb. Default is "misc"',
typeLabel: 'config | misc | usb | all'
typeLabel: 'config | misc | usb | usbOps | all'
},
{
name: 'no-report-id',
Expand Down
6 changes: 6 additions & 0 deletions packages/uhk-common/src/log/get-log-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function getLogOptions(options: CommandLineArgs): LogOptions {
logOptions.config = true;
logOptions.misc = true;
logOptions.usb = true;
logOptions.usbOps = true;
break;

case 'config':
Expand All @@ -27,6 +28,11 @@ export function getLogOptions(options: CommandLineArgs): LogOptions {

case 'usb':
logOptions.usb = true;
logOptions.usbOps = true;
break;

case 'usbOps':
logOptions.usbOps = true;
break;

default:
Expand Down
1 change: 0 additions & 1 deletion packages/uhk-common/src/log/log-reg-exps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export namespace LogRegExps {
export const transferRegExp = /USB\[T]:/;
export const writeRegExp = /USB\[W]:/;
export const readRegExp = /USB\[R]:/;
}
8 changes: 8 additions & 0 deletions packages/uhk-common/src/log/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ export class LogService {
this.log(...args);
}

usbOps(...args: any[]): void {
if (!this._options.usbOps) {
return;
}

this.log(...args);
}

protected log(...args: any[]): void {
console.log(...args);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/uhk-common/src/models/log-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export interface LogOptions {
/**
* Log every USB operations USB[T], USB[W], USB[R]
*/
usb?: boolean;
/**
* Log only USB[T] log entries
*/
usbOps?: boolean;
config?: boolean;
misc?: boolean;
}
30 changes: 15 additions & 15 deletions packages/uhk-usb/src/uhk-hid-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,23 @@ export class UhkHidDevice {
public async deleteBond(address: number[]): Promise<void> {
await this.assertDeviceSupportWirelessUSBCommands();

this.logService.usb('[UhkHidDevice] USB[T]: Delete all bonds');
this.logService.usbOps('[UhkHidDevice] USB[T]: Delete all bonds');
const buffer = Buffer.from([UsbCommand.UnpairAll, ...address]);
await this.write(buffer);
}

public async deleteAllBonds(): Promise<void> {
await this.assertDeviceSupportWirelessUSBCommands();

this.logService.usb('[UhkHidDevice] USB[T]: Delete all bonds');
this.logService.usbOps('[UhkHidDevice] USB[T]: Delete all bonds');
const buffer = Buffer.from([UsbCommand.UnpairAll, 0, 0, 0, 0, 0, 0]);
await this.write(buffer);
}

public async getBleAddress(): Promise<number[]> {
await this.assertDeviceSupportWirelessUSBCommands();

this.logService.usb('[UhkHidDevice] USB[T]: get BLE address');
this.logService.usbOps('[UhkHidDevice] USB[T]: get BLE address');
const buffer = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.BleAddress]);
const responseBuffer = await this.write(buffer);

Expand All @@ -180,7 +180,7 @@ export class UhkHidDevice {
public async getPairedRightPairBleAddress(): Promise<number[]> {
await this.assertDeviceSupportWirelessUSBCommands();

this.logService.usb('[UhkHidDevice] USB[T]: get paired right pair BLE address');
this.logService.usbOps('[UhkHidDevice] USB[T]: get paired right pair BLE address');
const buffer = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.PairedRightPeerBleAddress]);
const responseBuffer = await this.write(buffer);

Expand All @@ -196,7 +196,7 @@ export class UhkHidDevice {
public async getPairingInfo(): Promise<PairingInfo> {
await this.assertDeviceSupportWirelessUSBCommands();

this.logService.usb('[UhkHidDevice] USB[T]: read pairing info');
this.logService.usbOps('[UhkHidDevice] USB[T]: read pairing info');
const buffer = Buffer.from([UsbCommand.GetPairingData]);
const responseBuffer = await this.write(buffer);
const numbers = convertBufferToIntArray(responseBuffer);
Expand All @@ -216,7 +216,7 @@ export class UhkHidDevice {
public async getPairingStatus(): Promise<PairingStatuses> {
await this.assertDeviceSupportWirelessUSBCommands();

this.logService.usb('[UhkHidDevice] USB[T]: read pairing info');
this.logService.usbOps('[UhkHidDevice] USB[T]: read pairing info');
const buffer = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.PairingStatus]);
const responseBuffer = await this.write(buffer);

Expand All @@ -237,7 +237,7 @@ export class UhkHidDevice {
public async isPairedWith(address: number[]): Promise<boolean> {
await this.assertDeviceSupportWirelessUSBCommands();

this.logService.usb('[UhkHidDevice] USB[T]: is paired with');
this.logService.usbOps('[UhkHidDevice] USB[T]: is paired with');
const buffer = Buffer.from([
UsbCommand.IsPaired,
...address,
Expand All @@ -252,23 +252,23 @@ export class UhkHidDevice {
public async pairCentral(): Promise<void> {
await this.assertDeviceSupportWirelessUSBCommands();

this.logService.usb('[UhkHidDevice] USB[T]: pair central');
this.logService.usbOps('[UhkHidDevice] USB[T]: pair central');
const buffer = Buffer.from([UsbCommand.PairCentral]);
await this.write(buffer);
}

public async pairPeripheral(): Promise<void> {
await this.assertDeviceSupportWirelessUSBCommands();

this.logService.usb('[UhkHidDevice] USB[T]: pair peripheral');
this.logService.usbOps('[UhkHidDevice] USB[T]: pair peripheral');
const buffer = Buffer.from([UsbCommand.PairPeripheral]);
await this.write(buffer);
}

public async setPairingInfo(pairId: PairIds, info: PairingInfo): Promise<void> {
await this.assertDeviceSupportWirelessUSBCommands();

this.logService.usb('[UhkHidDevice] USB[T]: set pairing info');
this.logService.usbOps('[UhkHidDevice] USB[T]: set pairing info');
const buffer = Buffer.from([
UsbCommand.SetPairingData,
pairId,
Expand All @@ -283,7 +283,7 @@ export class UhkHidDevice {
public async switchToPairingMode(): Promise<void> {
await this.assertDeviceSupportWirelessUSBCommands();

this.logService.usb('[UhkHidDevice] USB[T]: switch to pairing mode');
this.logService.usbOps('[UhkHidDevice] USB[T]: switch to pairing mode');
const buffer = Buffer.from([UsbCommand.EnterPairingMode]);
await this.write(buffer);
}
Expand Down Expand Up @@ -558,7 +558,7 @@ export class UhkHidDevice {
(timeout & 0xff << 24) >> 24
]);
const data = this.getTransferData(message, reportId);
this.logService.usb(`[UhkHidDevice] USB[T]: Enumerated device, mode: ${reenumMode}`);
this.logService.usbOps(`[UhkHidDevice] USB[T]: Enumerated device, mode: ${reenumMode}`);
this.logService.usb('[UhkHidDevice] USB[W]:', bufferToString(data).substr(3));
try {
keyboardDevice.write(data);
Expand All @@ -576,7 +576,7 @@ export class UhkHidDevice {
}
jumped = true;
} else {
this.logService.usb('[UhkHidDevice] USB[T]: Enumerated device is not ready yet');
this.logService.usbOps('[UhkHidDevice] USB[T]: Enumerated device is not ready yet');
}
}
else {
Expand All @@ -592,7 +592,7 @@ export class UhkHidDevice {

async sendKbootCommandToModule(module: ModuleSlotToI2cAddress, command: KbootCommands, maxTry = 1): Promise<any> {
let transfer;
this.logService.usb(`[UhkHidDevice] USB[T]: Send KbootCommand ${mapI2cAddressToModuleName(module)} ${KbootCommands[command].toString()}`);
this.logService.usbOps(`[UhkHidDevice] USB[T]: Send KbootCommand ${mapI2cAddressToModuleName(module)} ${KbootCommands[command].toString()}`);
if (command === KbootCommands.idle) {
transfer = Buffer.from([UsbCommand.SendKbootCommandToModule, command]);
} else {
Expand Down Expand Up @@ -678,7 +678,7 @@ export class UhkHidDevice {
return this._protocolVersions;
}

this.logService.usb('[UhkHidDevice] USB[T]: Read device protocol version information');
this.logService.usbOps('[UhkHidDevice] USB[T]: Read device protocol version information');
const command = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.ProtocolVersions]);
const buffer = await this.write(command);
const uhkBuffer = UhkBuffer.fromArray(convertBufferToIntArray(buffer));
Expand Down
Loading

0 comments on commit e8e615a

Please sign in to comment.