@@ -46,7 +46,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
46
46
let addToOutput = [ ] ;
47
47
let s ;
48
48
try {
49
- await this . ui . showBusySpinnerUntilResolved ( 'Getting device status' , this . _withDevice ( async ( ) => {
49
+ await this . ui . showBusySpinnerUntilResolved ( 'Getting device status' , this . _withDevice ( true , async ( ) => {
50
50
s = await this . _getDeviceProtection ( ) ;
51
51
let res ;
52
52
let helper ;
@@ -94,7 +94,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
94
94
async disableProtection ( { open } = { } ) {
95
95
let addToOutput = [ ] ;
96
96
97
- await this . ui . showBusySpinnerUntilResolved ( 'Disabling device protection' , this . _withDevice ( async ( ) => {
97
+ await this . ui . showBusySpinnerUntilResolved ( 'Disabling device protection' , this . _withDevice ( true , async ( ) => {
98
98
try {
99
99
const deviceStr = await this . _getDeviceString ( ) ;
100
100
let s = await this . _getDeviceProtection ( ) ;
@@ -131,7 +131,6 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
131
131
132
132
const localBootloaderPath = await this . _downloadBootloader ( ) ;
133
133
await this . _flashBootloader ( localBootloaderPath ) ;
134
- // FIXME: Device could still be flashing the bootloader at this point.
135
134
addToOutput . push ( `${ deviceStr } is now an open device.${ os . EOL } ` ) ;
136
135
137
136
const success = await this . _markAsDevelopmentDevice ( true ) ;
@@ -168,7 +167,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
168
167
let addToOutput = [ ] ;
169
168
let protectedBinary = file ;
170
169
try {
171
- await this . ui . showBusySpinnerUntilResolved ( 'Enabling device protection' , this . _withDevice ( async ( ) => {
170
+ await this . ui . showBusySpinnerUntilResolved ( 'Enabling device protection' , this . _withDevice ( false , async ( ) => {
172
171
const deviceStr = await this . _getDeviceString ( ) ;
173
172
const s = await this . _getDeviceProtection ( ) ;
174
173
@@ -194,9 +193,10 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
194
193
const localBootloaderPath = await this . _downloadBootloader ( ) ;
195
194
protectedBinary = await this . _getProtectedBinary ( { file : localBootloaderPath , verbose : false } ) ;
196
195
}
196
+
197
197
await this . _flashBootloader ( protectedBinary ) ;
198
- // FIXME: Device could still be flashing the bootloader at this point.
199
198
addToOutput . push ( `${ deviceStr } is now a protected device.${ os . EOL } ` ) ;
199
+
200
200
const success = await this . _markAsDevelopmentDevice ( false ) ;
201
201
addToOutput . push ( success ?
202
202
// TODO: Improve these lines
@@ -333,18 +333,16 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
333
333
* @param {Function } fn - The function to execute with the device.
334
334
* @returns {Promise<*> } The result of the function execution.
335
335
*/
336
- async _withDevice ( fn ) {
337
- let putDeviceInDfuMode = false ;
336
+ async _withDevice ( putDeviceInDfuMode , fn ) {
338
337
try {
339
338
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 ;
344
340
345
341
return await fn ( ) ;
346
342
} finally {
343
+ console . log ( 'Closing device.' ) ;
347
344
if ( putDeviceInDfuMode ) {
345
+ await this . _waitForDeviceToReboot ( ) ;
348
346
await this . device . enterDfuMode ( ) ;
349
347
}
350
348
if ( this . device && this . device . isOpen ) {
@@ -378,6 +376,20 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
378
376
}
379
377
}
380
378
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
+
381
393
async delay ( ms ) {
382
394
return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
383
395
}
@@ -389,10 +401,10 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
389
401
* @param {Object } device - The device to reset.
390
402
* @returns {Promise<void> }
391
403
*/
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 ( ) ;
396
408
}
397
409
this . device = await usbUtils . reopenInNormalMode ( { id : this . deviceId } ) ;
398
410
}
0 commit comments