diff --git a/src/cli/usb.js b/src/cli/usb.js index 94d42e222..f515344ae 100644 --- a/src/cli/usb.js +++ b/src/cli/usb.js @@ -12,6 +12,7 @@ module.exports = ({ commandProcessor, root }) => { const usb = commandProcessor.createCategory(root, 'usb', 'Control USB devices'); commandProcessor.createCommand(usb, 'list', 'List the devices connected to the host computer', { + params: '[filter]', options: { 'exclude-dfu': { description: 'Do not list devices which are in DFU mode', diff --git a/src/cmd/usb.js b/src/cmd/usb.js index aea09e625..2ffcfd48e 100644 --- a/src/cmd/usb.js +++ b/src/cmd/usb.js @@ -1,5 +1,5 @@ const { spin } = require('../app/ui'); -const { asyncMapSeries } = require('../lib/utilities'); +const { asyncMapSeries, knownPlatforms } = require('../lib/utilities'); const { getDevice, formatDeviceInfo } = require('./device-util'); const { getUsbDevices, openUsbDevice, openUsbDeviceById } = require('./usb-util'); const { systemSupportsUdev, udevRulesInstalled, installUdevRules } = require('./udev'); @@ -16,6 +16,30 @@ module.exports = class UsbCommand { list(args) { const idsOnly = args['ids-only']; const excludeDfu = args['exclude-dfu']; + const filter = args.params.filter; + + let filterFunc = null; + if (filter){ + const platforms = knownPlatforms(); + if (filter === 'online') { + filterFunc = (d) => { + return d.connected; + }; + } else if (filter === 'offline') { + filterFunc = (d) => { + return !d.connected; + }; + } else if (Object.keys(platforms).indexOf(filter) >= 0) { + filterFunc = (d) => { + return d.platform_id === platforms[filter]; + }; + } else { + filterFunc = (d) => { + return d.id === filter || d.name === filter; + }; + } + } + // Enumerate USB devices return getUsbDevices({ dfuMode: !excludeDfu }) .then(usbDevices => { @@ -59,7 +83,9 @@ module.exports = class UsbCommand { return { id: usbDevice.id, name: (device && device.name) ? device.name : '', - type: `${type.join(', ')}` + type: `${type.join(', ')}`, + platform_id: device.platform_id, + connected: device.connected }; }) .finally(() => usbDevice.close()); @@ -73,6 +99,10 @@ module.exports = class UsbCommand { console.log('No devices found.'); } else { devices = devices.sort((a, b) => a.name.localeCompare(b.name)); // Sort devices by name + + if (filter) { + devices = devices.filter(filterFunc); + } devices.forEach(device => { console.log(formatDeviceInfo(device)); });