Skip to content

Commit

Permalink
Merge pull request #759 from particle-iot/feature/sc-129067/automatic…
Browse files Browse the repository at this point in the history
…-service-mode-usbutil

Simplify the generic helper that takes care of Protected Devices
  • Loading branch information
keeramis authored Jul 31, 2024
2 parents 4a3e1e1 + e5da90e commit c7f7833
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 c7f7833

Please sign in to comment.