-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow DFU flashing devices by Device ID #566
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
const fs = require('fs'); | ||
const VError = require('verror'); | ||
const dfu = require('../lib/dfu'); | ||
const settings = require('../../settings'); | ||
const ParticleApi = require('./api'); | ||
const { getDevice, isDeviceId } = require('./device-util'); | ||
const ModuleParser = require('binary-version-reader').HalModuleParser; | ||
const ModuleInfo = require('binary-version-reader').ModuleInfo; | ||
const deviceSpecs = require('../lib/deviceSpecs'); | ||
|
@@ -15,7 +18,7 @@ const systemModuleIndexToString = { | |
|
||
|
||
module.exports = class FlashCommand { | ||
flash(device, binary, files, { usb, serial, factory, force, target, port, yes }){ | ||
async flash(device, binary, files, { usb, serial, factory, force, target, port, yes }){ | ||
if (!device && !binary){ | ||
// if no device nor files are passed, show help | ||
// TODO: Replace by UsageError | ||
|
@@ -24,16 +27,42 @@ module.exports = class FlashCommand { | |
|
||
let result; | ||
if (usb){ | ||
result = this.flashDfu({ binary, factory, force }); | ||
if (files.length > 0) { | ||
// If both a device and a binary were provided, the binary will come in as the | ||
// first element in the files array | ||
binary = files[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is ambiguous at best and likely inaccurate. i've been adding tests like this to help clarify similar behavior. |
||
} else { | ||
// Otherwise, no device has been provided so unset the inaccurate value | ||
device = undefined; | ||
} | ||
|
||
// Lookup the Device ID based on the provided Device Name | ||
if (device && !isDeviceId(device)) { | ||
const api = new ParticleApi(settings.apiUrl, { accessToken: settings.access_token }).api; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. at some point i think we're going to have to insist on device IDs across the board as this type of thing leads to confusion - e.g. "why am i getting 4xx errors when flashing locally?!" etc |
||
|
||
let deviceInfo; | ||
try { | ||
deviceInfo = await getDevice({ id: device, api, auth: settings.access_token }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} catch (err) { | ||
throw new VError(ensureError(err), 'Device ID lookup failed'); | ||
} | ||
|
||
if (deviceInfo && deviceInfo.id) { | ||
device = deviceInfo.id; | ||
} else { | ||
throw new VError('Device ID lookup failed'); | ||
} | ||
} | ||
|
||
result = this.flashDfu({ device, binary, factory, force }); | ||
} else if (serial){ | ||
result = this.flashYModem({ binary, port, yes }); | ||
} else { | ||
result = this.flashCloud({ device, files, target }); | ||
} | ||
|
||
return result.then(() => { | ||
console.log ('\nFlash success!'); | ||
}); | ||
await result; | ||
console.log ('\nFlash success!'); | ||
} | ||
|
||
flashCloud({ device, files, target }){ | ||
|
@@ -47,12 +76,16 @@ module.exports = class FlashCommand { | |
return new SerialCommands().flashDevice(binary, { port, yes }); | ||
} | ||
|
||
flashDfu({ binary, factory, force, requestLeave }){ | ||
flashDfu({ device, binary, factory, force, requestLeave }){ | ||
let specs, destSegment, destAddress; | ||
let flashingKnownApp = false; | ||
let targetDevice = {}; | ||
return Promise.resolve() | ||
.then(() => dfu.isDfuUtilInstalled()) | ||
.then(() => dfu.findCompatibleDFU()) | ||
.then(() => dfu.findCompatibleDFU({ deviceId: device })) | ||
.then((compatibleDevice) => { | ||
targetDevice = compatibleDevice; | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: i've been holding off on |
||
.then(() => { | ||
//only match against knownApp if file is not found | ||
let stats; | ||
|
@@ -148,7 +181,7 @@ module.exports = class FlashCommand { | |
const alt = 0; | ||
// todo - leave on factory firmware write too? | ||
const leave = requestLeave !== undefined ? requestLeave : (destSegment === 'userFirmware'); | ||
return dfu.writeDfu(alt, finalBinary, destAddress, leave); | ||
return dfu.writeDfu(alt, finalBinary, destAddress, leave, targetDevice); | ||
}) | ||
.catch((err) => { | ||
throw new VError(ensureError(err), 'Error writing firmware'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have a number of commands in this same namespace that accept
deviceID
as a parameter - e.g.particle-cli/src/cli/keys.js
Lines 56 to 63 in 5659edf
i think we should either support that call signature (total pain, requires manual arg checking / juggling, etc) - OR - we rename the option (flag) to
--deviceID
. in both cases we want to be more explicit about the expected input (sincedevice
meansid
orname
across the rest of the CLI commands)