Skip to content

Commit

Permalink
Handle devices in dfu mode
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jun 20, 2024
1 parent c268826 commit 94885a3
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/cmd/device-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {

const localBootloaderPath = await this._downloadBootloader();
await this._flashBootloader(localBootloaderPath);
// FIXME: Device could still be flashing the bootloader at this point.
addToOutput.push(`${deviceStr} is now an open device.${os.EOL}`);

const success = await this._markAsDevelopmentDevice(true);
Expand Down Expand Up @@ -194,6 +195,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
protectedBinary = await this._getProtectedBinary({ file: localBootloaderPath, verbose: false });
}
await this._flashBootloader(protectedBinary);
// FIXME: Device could still be flashing the bootloader at this point.
addToOutput.push(`${deviceStr} is now a protected device.${os.EOL}`);
const success = await this._markAsDevelopmentDevice(false);
addToOutput.push(success ?
Expand Down Expand Up @@ -332,16 +334,20 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
* @returns {Promise<*>} The result of the function execution.
*/
async _withDevice(fn) {
let putDeviceInDfuMode = false;
try {
await this._getUsbDevice(this.device);

if (this.device.isInDfuMode) {
putDeviceInDfuMode = true;
await this._resetDevice(this.device);
await this._getUsbDevice(this.device);
this.device = await usbUtils.reopenInNormalMode( { id: this.deviceId });
}

return await fn();
} finally {
if (putDeviceInDfuMode) {
await this.device.enterDfuMode();
}
if (this.device && this.device.isOpen) {
await this.device.close();
}
Expand Down Expand Up @@ -373,6 +379,10 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
}
}

async delay(ms){
return new Promise((resolve) => setTimeout(resolve, ms));
}

/**
* Resets the device and waits for it to restart.
*
Expand All @@ -381,11 +391,10 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
* @returns {Promise<void>}
*/
async _resetDevice(device) {
await device.reset();
if (device.isOpen) {
if (device.isInDfuMode) {
await device.enterSafeMode();
await device.close();
}
await new Promise(resolve => setTimeout(resolve, 3000));
}

/**
Expand Down

0 comments on commit 94885a3

Please sign in to comment.