Skip to content

Commit 5ff74cb

Browse files
committed
Device will be put back into dfu mode if the device was in dfu mode at the start of the operation
1 parent efd1f06 commit 5ff74cb

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

src/cmd/device-protection.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
4646
let addToOutput = [];
4747
let s;
4848
try {
49-
await this.ui.showBusySpinnerUntilResolved('Getting device status', this._withDevice(async () => {
49+
await this.ui.showBusySpinnerUntilResolved('Getting device status', this._withDevice(true, async () => {
5050
s = await this._getDeviceProtection();
5151
let res;
5252
let helper;
@@ -94,7 +94,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
9494
async disableProtection({ open } = {}) {
9595
let addToOutput = [];
9696

97-
await this.ui.showBusySpinnerUntilResolved('Disabling device protection', this._withDevice(async () => {
97+
await this.ui.showBusySpinnerUntilResolved('Disabling device protection', this._withDevice(true, async () => {
9898
try {
9999
const deviceStr = await this._getDeviceString();
100100
let s = await this._getDeviceProtection();
@@ -131,7 +131,6 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
131131

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

137136
const success = await this._markAsDevelopmentDevice(true);
@@ -168,7 +167,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
168167
let addToOutput = [];
169168
let protectedBinary = file;
170169
try {
171-
await this.ui.showBusySpinnerUntilResolved('Enabling device protection', this._withDevice(async () => {
170+
await this.ui.showBusySpinnerUntilResolved('Enabling device protection', this._withDevice(false, async () => {
172171
const deviceStr = await this._getDeviceString();
173172
const s = await this._getDeviceProtection();
174173

@@ -194,9 +193,10 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
194193
const localBootloaderPath = await this._downloadBootloader();
195194
protectedBinary = await this._getProtectedBinary({ file: localBootloaderPath, verbose: false });
196195
}
196+
197197
await this._flashBootloader(protectedBinary);
198-
// FIXME: Device could still be flashing the bootloader at this point.
199198
addToOutput.push(`${deviceStr} is now a protected device.${os.EOL}`);
199+
200200
const success = await this._markAsDevelopmentDevice(false);
201201
addToOutput.push(success ?
202202
// TODO: Improve these lines
@@ -333,18 +333,16 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
333333
* @param {Function} fn - The function to execute with the device.
334334
* @returns {Promise<*>} The result of the function execution.
335335
*/
336-
async _withDevice(fn) {
337-
let putDeviceInDfuMode = false;
336+
async _withDevice(putDeviceInDfuMode, fn) {
338337
try {
339338
await this._getUsbDevice(this.device);
340-
if (this.device.isInDfuMode) {
341-
putDeviceInDfuMode = true;
342-
await this._resetDevice(this.device);
343-
}
339+
putDeviceInDfuMode = putDeviceInDfuMode && !this.device.isInDfuMode;
344340

345341
return await fn();
346342
} finally {
343+
console.log('Closing device.');
347344
if (putDeviceInDfuMode) {
345+
await this._waitForDeviceToReboot();
348346
await this.device.enterDfuMode();
349347
}
350348
if (this.device && this.device.isOpen) {
@@ -378,6 +376,20 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
378376
}
379377
}
380378

379+
async _waitForDeviceToReboot() {
380+
const start = Date.now();
381+
while (Date.now() - start < 60000) {
382+
try {
383+
await this.delay(1000);
384+
this.device = await usbUtils.reopenDevice({ id: this.deviceId });
385+
const s = await this.device.getProtectionState();
386+
break;
387+
} catch (error) {
388+
// ignore error
389+
}
390+
}
391+
}
392+
381393
async delay(ms){
382394
return new Promise((resolve) => setTimeout(resolve, ms));
383395
}
@@ -389,10 +401,10 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
389401
* @param {Object} device - The device to reset.
390402
* @returns {Promise<void>}
391403
*/
392-
async _resetDevice(device) {
393-
if (device.isInDfuMode) {
394-
await device.enterSafeMode();
395-
await device.close();
404+
async _putDeviceInSafeMode() {
405+
if (this.device.isInDfuMode) {
406+
await this.device.enterSafeMode();
407+
await this.device.close();
396408
}
397409
this.device = await usbUtils.reopenInNormalMode( { id: this.deviceId });
398410
}

0 commit comments

Comments
 (0)