Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve usb device selector output #680

Merged
merged 8 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cmd/flash.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ module.exports = class FlashCommand extends CLICommandBase {
throw new Error("Use 'particle flash --local' to flash a zipped bundle.");
}

device = await usbUtils.getOneUsbDevice({ ui: this.ui });
const { api, auth } = this._particleApi();
device = await usbUtils.getOneUsbDevice({ api, auth, ui: this.ui });
const platformName = platformForId(device.platformId).name;
validateDFUSupport({ device, ui: this.ui });

Expand Down
47 changes: 44 additions & 3 deletions src/cmd/usb-util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { getDevice, isDeviceId } = require('./device-util');
const { systemSupportsUdev, promptAndInstallUdevRules } = require('./udev');
const { delay } = require('../lib/utilities');
const { platformForId } = require('../lib/platform');
const {
getDevices,
openDeviceById,
Expand All @@ -15,6 +16,44 @@ const REOPEN_TIMEOUT = 60000;
// When reopening a device that was about to reset, give it some time to boot into the firmware
const REOPEN_DELAY = 500;

async function _getDeviceInfo(device) {
let id = null;
let mode = null;
try {
await device.open();
id = device._id;
if (device.isInDfuMode) {
mode = 'DFU';
return { id, mode };
}
mode = await device.getDeviceMode({ timeout: 10 * 1000 });
// not required to show NORMAL mode to the user
if (mode === 'NORMAL') {
mode = '';
}
return { id, mode };
} catch (err) {
if (err instanceof TimeoutError) {
return { id, mode: 'UNKNOWN' };
} else {
throw new Error(`Unable to get device mode: ${err.message}`);
}
} finally {
if (device.isOpen) {
await device.close();
}
}
}

async function _getDeviceName({ id, api, auth, ui }) {
try {
const device = await getDevice({ id, api, auth, ui });
return device && device.name ? device.name : '<no name>';
} catch (err) {
return '<unknown>';
}
}

/**
* USB permissions error.
*/
Expand Down Expand Up @@ -153,12 +192,14 @@ async function getOneUsbDevice({ idOrName, api, auth, ui }) {
name: 'device',
message: 'Which device would you like to select?',
choices() {
return usbDevices.map((d) => {
return Promise.all(usbDevices.map(async (d) => {
const { id, mode } = await _getDeviceInfo(d);
const name = await _getDeviceName({ id, api, auth, ui });
return {
name: d.type,
name: `${name} [${id}] (${platformForId(d._info.id).displayName}${mode ? ', ' + mode : '' })`,
value: d
};
});
}));
}
};
const nonInteractiveError = 'Multiple devices found. Connect only one device when running in non-interactive mode.';
Expand Down