From ab8ae3132468daadcda45f59742d65c591dc817e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Kiss?= Date: Mon, 21 May 2018 11:46:19 +0200 Subject: [PATCH] fix: permission detection on linux (#651) --- packages/uhk-usb/src/uhk-hid-device.ts | 25 ++++++++----------------- packages/uhk-usb/src/util.ts | 13 ++++++++++++- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/packages/uhk-usb/src/uhk-hid-device.ts b/packages/uhk-usb/src/uhk-hid-device.ts index 4f30223d..f10b2375 100644 --- a/packages/uhk-usb/src/uhk-hid-device.ts +++ b/packages/uhk-usb/src/uhk-hid-device.ts @@ -13,7 +13,7 @@ import { ModuleSlotToId, UsbCommand } from './constants'; -import { bufferToString, getTransferData, retry, snooze } from './util'; +import { bufferToString, getTransferData, isUhkDevice, retry, snooze } from './util'; export const BOOTLOADER_TIMEOUT_MS = 5000; @@ -50,14 +50,12 @@ export class UhkHidDevice { return true; } - if (process.platform === 'linux') { - const devs = devices(); + const dev = devices().find((x: Device) => isUhkDevice(x) || x.productId === Constants.BOOTLOADER_ID); - this._hasPermission = devs.some((x: Device) => x.vendorId === Constants.VENDOR_ID && - (x.productId === Constants.PRODUCT_ID || x.productId === Constants.BOOTLOADER_ID)); - } else { - this._hasPermission = true; - } + const device = new HID(dev.path); + device.close(); + + this._hasPermission = true; return this._hasPermission; } catch (err) { @@ -80,8 +78,7 @@ export class UhkHidDevice { }; for (const dev of devs) { - if (dev.vendorId === Constants.VENDOR_ID && - dev.productId === Constants.PRODUCT_ID) { + if (isUhkDevice(dev)) { result.connected = true; } else if (dev.vendorId === Constants.VENDOR_ID && dev.productId === Constants.BOOTLOADER_ID) { @@ -254,13 +251,7 @@ export class UhkHidDevice { this.logService.debug('[UhkHidDevice] Available devices unchanged'); } - const dev = devs.find((x: Device) => - x.vendorId === Constants.VENDOR_ID && - x.productId === Constants.PRODUCT_ID && - // hidapi can not read the interface number on Mac, so check the usage page and usage - ((x.usagePage === 128 && x.usage === 129) || // Old firmware - (x.usagePage === (0xFF00 | 0x00) && x.usage === 0x01) || // New firmware - x.interface === 0)); + const dev = devs.find(isUhkDevice); if (!dev) { this.logService.debug('[UhkHidDevice] UHK Device not found:'); diff --git a/packages/uhk-usb/src/util.ts b/packages/uhk-usb/src/util.ts index c4976cd5..2a9ba651 100644 --- a/packages/uhk-usb/src/util.ts +++ b/packages/uhk-usb/src/util.ts @@ -1,6 +1,8 @@ -import { Constants, UsbCommand } from './constants'; +import { Device } from 'node-hid'; import { DeviceConnectionState, LogService } from 'uhk-common'; +import { Constants, UsbCommand } from './constants'; + export const snooze = ms => new Promise(resolve => setTimeout(resolve, ms)); /** @@ -101,3 +103,12 @@ export const deviceConnectionStateComparer = (a: DeviceConnectionState, b: Devic && a.connected === b.connected && a.bootloaderActive === b.bootloaderActive; }; + +export const isUhkDevice = (dev: Device): boolean => { + return dev.vendorId === Constants.VENDOR_ID && + dev.productId === Constants.PRODUCT_ID && + // hidapi can not read the interface number on Mac, so check the usage page and usage + ((dev.usagePage === 128 && dev.usage === 129) || // Old firmware + (dev.usagePage === (0xFF00 | 0x00) && dev.usage === 0x01) || // New firmware + dev.interface === 0); +};