Skip to content

Commit

Permalink
Merge pull request #547 from particle-iot/setup_done/ch46416
Browse files Browse the repository at this point in the history
Add a command to set the setup done flag
  • Loading branch information
busticated authored Feb 6, 2020
2 parents 49f3b0f + bce31d8 commit a1205a7
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
19 changes: 19 additions & 0 deletions src/cli/usb.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ module.exports = ({ commandProcessor, root }) => {
}
});

commandProcessor.createCommand(usb, 'setup-done', 'Set the setup done flag', {
params: '[devices...]',
options: {
'reset': {
description: 'Clear the setup done flag',
boolean: true
},
...commonOptions
},
examples: {
'$0 $command my_device': 'Set the setup done flag on the device "my_device"',
'$0 $command --reset my_device': 'Clear the setup done flag on the device "my_device"',
'$0 $command --all': 'Set the setup done flag on all devices connected to the host computer',
},
handler: (args) => {
return usbCommand().setSetupDone(args);
}
});

commandProcessor.createCommand(usb, 'configure', 'Update the system USB configuration', {
handler: (args) => {
return usbCommand().configure(args);
Expand Down
43 changes: 40 additions & 3 deletions src/cmd/usb.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,30 @@ module.exports = class UsbCommand {
}
})
.then(device => {
const type = platformsById[usbDevice.platformId];
let info = [device, usbDevice.isInDfuMode];

if (!usbDevice.isInDfuMode){
info.push(usbDevice.getDeviceMode());
}

return Promise.all(info);
})
.then(([device, isInDfuMode, mode]) => {
const platform = platformsById[usbDevice.platformId];
const type = [platform];

if (isInDfuMode){
type.push('DFU');
}

if (mode && mode !== 'UNKNOWN'){
type.push(mode);
}

return {
id: usbDevice.id,
type: usbDevice.isInDfuMode ? `${type}, DFU` : type,
name: (device && device.name) ? device.name : ''
name: (device && device.name) ? device.name : '',
type: `${type.join(', ')}`
};
})
.finally(() => usbDevice.close());
Expand Down Expand Up @@ -109,6 +128,24 @@ module.exports = class UsbCommand {
});
}

setSetupDone(args) {
const done = !args.reset;
return this._forEachUsbDevice(args, usbDevice => {
if (usbDevice.isMeshDevice) {
return usbDevice.setSetupDone(done)
.then(() => {
if (done) {
return usbDevice.leaveListeningMode();
}
return usbDevice.enterListeningMode();
});
}
})
.then(() => {
console.log('Done.');
});
}

configure() {
if (!systemSupportsUdev()) {
console.log('The system does not require configuration.');
Expand Down
10 changes: 5 additions & 5 deletions test/e2e/help.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ describe('Help & Unknown Command / Argument Handling', () => {
'serial mac', 'serial inspect', 'serial flash', 'serial claim',
'serial', 'setup', 'subscribe', 'token list', 'token revoke',
'token create', 'token', 'udp send', 'udp listen', 'udp', 'update',
'update-cli', 'usb list', 'usb start-listening', 'usb listen', 'usb stop-listening',
'usb safe-mode', 'usb dfu', 'usb reset', 'usb configure', 'usb',
'variable list', 'variable get', 'variable monitor', 'variable',
'webhook create', 'webhook list', 'webhook delete', 'webhook POST',
'webhook GET', 'webhook', 'whoami'];
'update-cli', 'usb list', 'usb start-listening', 'usb listen',
'usb stop-listening', 'usb safe-mode', 'usb dfu', 'usb reset',
'usb setup-done', 'usb configure', 'usb', 'variable list', 'variable get',
'variable monitor', 'variable', 'webhook create', 'webhook list',
'webhook delete', 'webhook POST', 'webhook GET', 'webhook', 'whoami'];

const mainCmds = dedupe(allCmds.map(c => c.split(' ')[0]));

Expand Down
23 changes: 22 additions & 1 deletion test/e2e/usb.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('USB Commands [@device]', () => {
' safe-mode Put a device into the safe mode',
' dfu Put a device into the DFU mode',
' reset Reset a device',
' setup-done Set the setup done flag',
' configure Update the system USB configuration',
'',
'Global Options:',
Expand All @@ -34,6 +35,8 @@ describe('USB Commands [@device]', () => {
});

after(async () => {
await cli.run(['usb', 'setup-done']);
await delay(2000);
await cli.logout();
await cli.setDefaultProfile();
});
Expand Down Expand Up @@ -90,5 +93,23 @@ describe('USB Commands [@device]', () => {
expect(subproc.stderr).to.equal('');
expect(subproc.exitCode).to.equal(1);
});
});

it('Sets and clears the setup done flag', async function test() {
await cli.run(['usb', 'setup-done', '--reset']);
await delay(2000);

const platform = capitalize(DEVICE_PLATFORM_NAME);
const { stdout, stderr, exitCode } = await cli.run(['usb', 'list']);
expect(stdout).to.include(`${DEVICE_NAME} [${DEVICE_ID}] (${platform}, LISTENING)`);
expect(stderr).to.equal('');
expect(exitCode).to.equal(0);

await cli.run(['usb', 'setup-done']);
await delay(2000);

const subproc = await cli.run(['usb', 'list']);
expect(subproc.stdout).to.include(`${DEVICE_NAME} [${DEVICE_ID}] (${platform})`);
expect(subproc.stderr).to.equal('');
expect(subproc.exitCode).to.equal(0);
});
});

0 comments on commit a1205a7

Please sign in to comment.