Skip to content

Commit 9dfe271

Browse files
committed
Do not return device handle to the generic wrapper
1 parent 46431b7 commit 9dfe271

File tree

6 files changed

+17
-41
lines changed

6 files changed

+17
-41
lines changed

src/cmd/device-protection.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
6565
} catch (error) {
6666
// TODO: Log detailed and user-friendly error messages from the device or API instead of displaying the raw error message
6767
if (error.message === 'Not supported') {
68-
throw new Error(`Device Protection feature is not supported on this device. Visit ${chalk.yellow('https://docs.particle.io')} for more information${os.EOL}`);
68+
throw new Error(`Device Protection feature is not supported on this device. Visit ${chalk.yellow('https://docs.particle.io')} for more information.`);
6969
}
7070
throw new Error(`Unable to get device status: ${error.message}${os.EOL}`);
7171
}
@@ -108,7 +108,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
108108
});
109109
} catch (error) {
110110
if (error.message === 'Not supported') {
111-
throw new Error(`Device Protection feature is not supported on this device. Visit ${chalk.yellow('https://docs.particle.io')} for more information${os.EOL}`);
111+
throw new Error(`Device Protection feature is not supported on this device. Visit ${chalk.yellow('https://docs.particle.io')} for more information.`);
112112
}
113113
throw new Error(`Failed to disable Device Protection: ${error.message}${os.EOL}`);
114114
}
@@ -306,6 +306,13 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
306306
*/
307307
async _withDevice({ spinner }, fn) {
308308
await this._getUsbDevice(this.device);
309+
310+
const platform = platformForId(this.device.platformId);
311+
// Gen2 platforms can take a long time to respond to control requests. Quit early.
312+
if (platform.generation <= 2) {
313+
throw new Error(`Device Protection feature is not supported on this device. Visit ${chalk.yellow('https://docs.particle.io')} for more information.`);
314+
}
315+
309316
await this.ui.showBusySpinnerUntilResolved(spinner, (async () => {
310317
this.status = await DeviceProtectionHelper.getProtectionStatus(this.device);
311318
return await fn();

src/cmd/device-protection.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ describe('DeviceProtectionCommands', () => {
1111
deviceProtectionCommands.device = {
1212
isInDfuMode: false,
1313
unprotectDevice: sinon.stub(),
14-
enterDfuMode: sinon.stub()
14+
enterDfuMode: sinon.stub(),
15+
platformId: 13 /*boron*/
1516
};
1617
});
1718

src/cmd/flash.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ module.exports = class FlashCommand extends CLICommandBase {
7474
}
7575

7676
async _flashOverUsb(device, binary, factory) {
77-
const deviceId = device.id;
7877
const platformName = platformForId(device.platformId).name;
7978
validateDFUSupport({ device, ui: this.ui });
8079

@@ -104,14 +103,6 @@ module.exports = class FlashCommand extends CLICommandBase {
104103
this.ui.write(`Flashing ${platformName} device ${device.id}`);
105104
const resetAfterFlash = !factory && modulesToFlash[0].prefixInfo.moduleFunction === ModuleInfo.FunctionType.USER_PART;
106105
await flashFiles({ device, flashSteps, resetAfterFlash, ui: this.ui });
107-
108-
// The device obtained here could be closed, so reopen the device and ensure it is ready to talk to
109-
// FIXME: Gen2 devices may not be able to respond to control requests immediately after flashing
110-
const platform = platformForId(device.platformId);
111-
if (platform.generation > 2) {
112-
device = await usbUtils.waitForDeviceToRespond(deviceId);
113-
return device;
114-
}
115106
}
116107

117108
flashCloud({ device, files, target }) {
@@ -138,7 +129,6 @@ module.exports = class FlashCommand extends CLICommandBase {
138129
}
139130

140131
async _flashLocal(device, parsedFiles, deviceIdOrName, knownApp, applicationOnly, target, verbose=true) {
141-
const deviceId = device.id;
142132
const platformId = device.platformId;
143133
const platformName = platformForId(platformId).name;
144134
const currentDeviceOsVersion = device.firmwareVersion;
@@ -184,14 +174,6 @@ module.exports = class FlashCommand extends CLICommandBase {
184174
});
185175

186176
await flashFiles({ device, flashSteps, ui: this.ui, verbose });
187-
188-
// The device obtained here may have been closed, so reopen it and ensure it is ready to send control requests to.
189-
// FIXME: Gen2 devices may not be able to respond to control requests immediately after flashing
190-
const platform = platformForId(device.platformId);
191-
if (platform.generation > 2) {
192-
device = await usbUtils.waitForDeviceToRespond(deviceId);
193-
return device;
194-
}
195177
}
196178

197179
async _analyzeFiles(files) {

src/cmd/update.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ module.exports = class UpdateCommand extends CLICommandBase {
2929
}
3030

3131
async _updateDevice(device, deviceIdOrName, target) {
32-
const deviceId = device.id;
3332
const { api } = this._particleApi();
3433
const version = target || 'latest';
3534
validateDFUSupport({ device, ui: this.ui });
@@ -53,14 +52,6 @@ module.exports = class UpdateCommand extends CLICommandBase {
5352
await flashFiles({ device, flashSteps, ui: this.ui });
5453

5554
this.ui.write('Update success!');
56-
57-
// The device obtained here may have been closed, so reopen it and ensure it is ready to send control requests to.
58-
// FIXME: Gen2 devices may not be able to respond to control requests immediately after flashing
59-
const platform = platformForId(device.platformId);
60-
if (platform.generation > 2) {
61-
device = await usbUtils.waitForDeviceToRespond(deviceId);
62-
return device;
63-
}
6455
}
6556

6657
_particleApi() {

src/cmd/usb-util.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class UsbPermissionsError extends Error {
9797
*/
9898
async function executeWithUsbDevice({ args, func, enterDfuMode = false, allowProtectedDevices = true } = {}) {
9999
let device = await getOneUsbDevice(args);
100+
const deviceId = device.id;
100101
let deviceIsProtected = false; // Protected and Protected Devices in Service Mode
101102
let disableProtection = false; // Only Protected Devices (not in Service Mode)
102103

@@ -129,22 +130,16 @@ async function executeWithUsbDevice({ args, func, enterDfuMode = false, allowPro
129130
}
130131
}
131132

132-
let newDeviceHandle = null;
133133
try {
134134
if (enterDfuMode) {
135135
validateDFUSupport({ device, ui: args.ui });
136136
device = await reopenInDfuMode(device);
137137
}
138-
newDeviceHandle = await func(device);
139-
// Overwrite device handle if it is provided by the executed function
140-
// FIXME: Perhaps a poor way to uniquely identify a device is of UsbDevice type
141-
if (newDeviceHandle?._dev) {
142-
device = newDeviceHandle;
143-
}
138+
await func(device);
144139
} finally {
145140
if (deviceIsProtected) {
146141
try {
147-
// 'device' has finished the CLI command and is now ready to run this command
142+
device = await waitForDeviceToRespond(deviceId);
148143
await deviceProtectionHelper.turnOffServiceMode(device);
149144
} catch (error) {
150145
// Ignore error. At most, device is left in Service Mode

src/lib/flash-helper.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,11 @@ async function maintainDeviceProtection({ modules, device }) {
398398
// Firmware module info is only available when the device is not in DFU mode
399399
// If the device was in DFU mode, allow the flashing to continue and fail midway if the device is not compatible
400400
if (!device.isInDfuMode) {
401-
let currentModules, incompatSystem, incompatBootloader;
401+
let currentModules, incompatSystem, incompatBootloader, currentSystem, currentBootloader;
402402
try {
403403
currentModules = await device.getFirmwareModuleInfo({ timeout: 5000 });
404-
const currentSystem = currentModules.find(m => m.type === 'SYSTEM_PART');
405-
const currentBootloader = currentModules.find(m => m.type === 'BOOTLOADER' && m.index === 0);
404+
currentSystem = currentModules.find(m => m.type === 'SYSTEM_PART');
405+
currentBootloader = currentModules.find(m => m.type === 'BOOTLOADER' && m.index === 0);
406406
incompatSystem = currentSystem && INCOMPATIBLE_PROTECTED_SYSTEM_VERSIONS[currentSystem.version] &&
407407
INCOMPATIBLE_PROTECTED_SYSTEM_VERSIONS[currentSystem.version].includes(moduleVersion);
408408
incompatBootloader = currentBootloader && INCOMPATIBLE_PROTECTED_BOOTLOADER_VERSIONS[currentBootloader.version] &&

0 commit comments

Comments
 (0)