Skip to content

Commit

Permalink
Expect device to be ready to run a control request after CLI op finishes
Browse files Browse the repository at this point in the history
Check for Device Protection for only Gen3+
  • Loading branch information
keeramis committed Jul 31, 2024
1 parent 4a3e1e1 commit 1ebcd6c
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions src/cmd/usb-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,29 @@ class UsbPermissionsError extends Error {
async function executeWithUsbDevice({ args, func, dfuMode = false } = {}) {
let device = await getOneUsbDevice(args, { dfuMode });
let deviceIsProtected = false;
try {
const s = await deviceProtectionHelper.getProtectionStatus(device);
deviceIsProtected = s.protected && !s.overrridden;
} catch (err) {
if (err.message === 'Not supported') {
// Device Protection is not supported on certain platforms and versions.
// It means that the device is not protected.
}
throw err;
}
if (deviceIsProtected) {
const deviceWasInDfuMode = device.isInDfuMode;
if (deviceWasInDfuMode) {
device = await _putDeviceInSafeMode(device);

const platform = platformForId(device.platformId);
if (platform.generation > 2) { // Skipping device protection check for Gen2 platforms
try {
const s = await deviceProtectionHelper.getProtectionStatus(device);
deviceIsProtected = s.protected && !s.overrridden;
} catch (err) {
if (err.message === 'Not supported') {
// Device Protection is not supported on certain platforms and versions.
// It means that the device is not protected.
} else {
throw err;
}
}
await deviceProtectionHelper.disableDeviceProtection(device);
if (deviceWasInDfuMode) {
device = await reopenInDfuMode(device);
if (deviceIsProtected) {
const deviceWasInDfuMode = device.isInDfuMode;
if (deviceWasInDfuMode) {
device = await _putDeviceInSafeMode(device);
}
await deviceProtectionHelper.disableDeviceProtection(device);
if (deviceWasInDfuMode) {
device = await reopenInDfuMode(device);
}
}
}

Expand All @@ -123,15 +128,11 @@ async function executeWithUsbDevice({ args, func, dfuMode = false } = {}) {
res = await func(device);
} finally {
if (deviceIsProtected) {
device = await reopenDevice(device);
if (!device.isInDfuMode) {
device = await waitForDeviceToReboot(device);
}
try {
// Only works for 6.1.2 and later
// 'device' has finished the CLI command and is now ready to run this command
await deviceProtectionHelper.turnOffServiceMode(device);
} catch (error) {
// FIXME: ignore errors
// Ignore error. At most, device is left in Service Mode
}
}
if (device && device.isOpen) {
Expand All @@ -147,9 +148,9 @@ async function executeWithUsbDevice({ args, func, dfuMode = false } = {}) {
* @param {*} deviceId
* @returns
*/
async function waitForDeviceToReboot(device) {
async function waitForDeviceToRespond(device) {
const deviceId = device.id;
const REBOOT_TIME_MSEC = 60000;
const REBOOT_TIME_MSEC = 20000;
const REBOOT_INTERVAL_MSEC = 1000;
const start = Date.now();
if (device.isOpen) {
Expand Down Expand Up @@ -550,5 +551,5 @@ module.exports = {
forEachUsbDevice,
openUsbDevices,
executeWithUsbDevice,
waitForDeviceToReboot
waitForDeviceToRespond
};

0 comments on commit 1ebcd6c

Please sign in to comment.