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,
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:');

View File

@@ -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);
};