fix: permission detection on linux (#651)

This commit is contained in:
Róbert Kiss
2018-05-21 11:46:19 +02:00
committed by László Monda
parent daa0e723b1
commit ab8ae31324
2 changed files with 20 additions and 18 deletions

View File

@@ -13,7 +13,7 @@ import {
ModuleSlotToId, ModuleSlotToId,
UsbCommand UsbCommand
} from './constants'; } from './constants';
import { bufferToString, getTransferData, retry, snooze } from './util'; import { bufferToString, getTransferData, isUhkDevice, retry, snooze } from './util';
export const BOOTLOADER_TIMEOUT_MS = 5000; export const BOOTLOADER_TIMEOUT_MS = 5000;
@@ -50,14 +50,12 @@ export class UhkHidDevice {
return true; return true;
} }
if (process.platform === 'linux') { const dev = devices().find((x: Device) => isUhkDevice(x) || x.productId === Constants.BOOTLOADER_ID);
const devs = devices();
this._hasPermission = devs.some((x: Device) => x.vendorId === Constants.VENDOR_ID && const device = new HID(dev.path);
(x.productId === Constants.PRODUCT_ID || x.productId === Constants.BOOTLOADER_ID)); device.close();
} else {
this._hasPermission = true; this._hasPermission = true;
}
return this._hasPermission; return this._hasPermission;
} catch (err) { } catch (err) {
@@ -80,8 +78,7 @@ export class UhkHidDevice {
}; };
for (const dev of devs) { for (const dev of devs) {
if (dev.vendorId === Constants.VENDOR_ID && if (isUhkDevice(dev)) {
dev.productId === Constants.PRODUCT_ID) {
result.connected = true; result.connected = true;
} else if (dev.vendorId === Constants.VENDOR_ID && } else if (dev.vendorId === Constants.VENDOR_ID &&
dev.productId === Constants.BOOTLOADER_ID) { dev.productId === Constants.BOOTLOADER_ID) {
@@ -254,13 +251,7 @@ export class UhkHidDevice {
this.logService.debug('[UhkHidDevice] Available devices unchanged'); this.logService.debug('[UhkHidDevice] Available devices unchanged');
} }
const dev = devs.find((x: Device) => const dev = devs.find(isUhkDevice);
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));
if (!dev) { if (!dev) {
this.logService.debug('[UhkHidDevice] UHK Device not found:'); this.logService.debug('[UhkHidDevice] UHK Device not found:');

View File

@@ -1,6 +1,8 @@
import { Constants, UsbCommand } from './constants'; import { Device } from 'node-hid';
import { DeviceConnectionState, LogService } from 'uhk-common'; import { DeviceConnectionState, LogService } from 'uhk-common';
import { Constants, UsbCommand } from './constants';
export const snooze = ms => new Promise(resolve => setTimeout(resolve, ms)); 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.connected === b.connected
&& a.bootloaderActive === b.bootloaderActive; && 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);
};