diff --git a/src/cmd/usb-util.js b/src/cmd/usb-util.js index b211c3ffb..77d4eba2f 100644 --- a/src/cmd/usb-util.js +++ b/src/cmd/usb-util.js @@ -7,7 +7,8 @@ const { openDeviceById, NotFoundError, NotAllowedError, - TimeoutError + TimeoutError, + DeviceProtectionError } = require('../lib/require-optional')('particle-usb'); // Timeout when reopening a USB device after an update via control requests. This timeout should be @@ -35,6 +36,8 @@ async function _getDeviceInfo(device) { } catch (err) { if (err instanceof TimeoutError) { return { id, mode: 'UNKNOWN' }; + } else if (err instanceof DeviceProtectionError) { + return { id, mode: 'PROTECTED' }; } else { throw new Error(`Unable to get device mode: ${err.message}`); } @@ -239,6 +242,11 @@ async function getOneUsbDevice({ idOrName, api, auth, ui, flashMode, platformId usbDevice = devices[0].value; } + const devInfo = await _getDeviceInfo(usbDevice); + if (devInfo.mode === 'PROTECTED') { + ui.write(`Attempted to flash device ${devInfo.id}`); + throw new Error('Operation could not be completed due to device protection.'); + } try { await usbDevice.open(); @@ -362,5 +370,6 @@ module.exports = { reopenInNormalMode, reopenDevice, UsbPermissionsError, - TimeoutError + TimeoutError, + DeviceProtectionError }; diff --git a/src/cmd/usb.js b/src/cmd/usb.js index 39bef2033..e1fbd843f 100644 --- a/src/cmd/usb.js +++ b/src/cmd/usb.js @@ -1,7 +1,7 @@ const { spin } = require('../app/ui'); const { delay, asyncMapSeries, buildDeviceFilter } = require('../lib/utilities'); const { getDevice, formatDeviceInfo } = require('./device-util'); -const { getUsbDevices, openUsbDevice, openUsbDeviceByIdOrName, TimeoutError } = require('./usb-util'); +const { getUsbDevices, openUsbDevice, openUsbDeviceByIdOrName, TimeoutError, DeviceProtectionError } = require('./usb-util'); const { systemSupportsUdev, udevRulesInstalled, installUdevRules } = require('./udev'); const { platformForId, isKnownPlatformId } = require('../lib/platform'); const ParticleApi = require('./api'); @@ -46,8 +46,10 @@ module.exports = class UsbCommand { info.push( usbDevice.getDeviceMode({ timeout: 10 * 1000 }) .catch(error => { - if (error instanceof TimeoutError){ + if (error instanceof TimeoutError) { return 'UNKNOWN'; + } else if (error instanceof DeviceProtectionError) { + return 'PROTECTED'; } throw error; })