Skip to content

Commit

Permalink
Merge branch 'master' into feature/cli-installer-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomontero committed May 8, 2024
2 parents abbe4b0 + 58e52f2 commit 0b69d42
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
21 changes: 13 additions & 8 deletions src/lib/flash-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,26 @@ const FLASH_TIMEOUT = 4 * 60000;
async function flashFiles({ device, flashSteps, resetAfterFlash = true, ui }) {
const progress = _createFlashProgress({ flashSteps, ui });
let success = false;
let lastStepDfu = false;
try {
for (const step of flashSteps) {
device = await prepareDeviceForFlash({ device, mode: step.flashMode, progress });
if (step.flashMode === 'normal') {
device = await _flashDeviceInNormalMode(device, step.data, { name: step.name, progress: progress, checkSkip: step.checkSkip });
lastStepDfu = false;
} else {
// CLI always flashes to internal flash which is the DFU alt setting 0
const altSetting = 0;
device = await _flashDeviceInDfuMode(device, step.data, { name: step.name, altSetting: altSetting, startAddr: step.address, progress: progress });
lastStepDfu = true;
}
}
success = true;
} finally {
progress({ event: 'finish', success });
if (device.isOpen) {
if (resetAfterFlash) {
// only reset the device if the last step was in DFU mode
if (resetAfterFlash && lastStepDfu) {
try {
await device.reset();
} catch (error) {
Expand Down Expand Up @@ -62,6 +66,12 @@ async function _flashDeviceInNormalMode(device, data, { name, progress, checkSki
}
return device;
}
try {
await device.enterListeningMode();
await delay(1000); // Just in case
} catch (error) {
// ignore
}
await device.updateFirmware(data, { progress, timeout: FLASH_TIMEOUT });
return device;
} catch (error) {
Expand All @@ -86,12 +96,6 @@ async function prepareDeviceForFlash({ device, mode, progress }) {
}
device = await usbUtils.reopenInNormalMode(device, { reset: true });
}
try {
await device.enterListeningMode();
await delay(1000); // Just in case
} catch (error) {
// ignore
}
break;
case 'dfu':
if (!device.isInDfuMode) {
Expand Down Expand Up @@ -328,8 +332,9 @@ async function createFlashSteps({ modules, isInDfuMode, factory, platformId }) {
}

function _skipAsset(module, existingAssets) {
const name = path.basename(module.filename);
const hashAssetToBeFlashed = _get256Hash(module);
return existingAssets.some((asset) => hashAssetToBeFlashed === asset.hash);
return existingAssets.some((asset) => hashAssetToBeFlashed === asset.hash && name === asset.name);
}

function _get256Hash(module) {
Expand Down
16 changes: 4 additions & 12 deletions src/lib/flash-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,60 +416,52 @@ describe('flash-helper', () => {
const device = {
isOpen: true,
isInDfuMode: true,
close: sinon.stub(),
enterListeningMode: sinon.stub()
close: sinon.stub()
};
reopenInNormalStub.resolves(device);
reopenStub.resolves(device);
await prepareDeviceForFlash({ device, mode: 'normal' });
expect(reopenStub).to.have.been.calledOnce;
expect(reopenInNormalStub).to.have.been.calledOnce;
expect(reopenInDfuModeStub).to.not.have.been.called;
expect(device.enterListeningMode).to.have.been.calledOnce;
});
it('prepares the device when is required for normal mode and currently is in normal mode', async () => {
const device = {
isOpen: true,
isInDfuMode: false,
close: sinon.stub(),
enterListeningMode: sinon.stub()
close: sinon.stub()
};

reopenStub.resolves(device);
await prepareDeviceForFlash({ device, mode: 'normal' });
expect(reopenStub).to.have.been.calledOnce;
expect(reopenInNormalStub).to.not.have.been.called;
expect(reopenInDfuModeStub).to.not.have.been.called;
expect(device.enterListeningMode).to.have.been.calledOnce;
});
it('prepares the device when is required for dfu mode and currently is in normal mode', async () => {
const device = {
isOpen: true,
isInDfuMode: false,
close: sinon.stub(),
enterListeningMode: sinon.stub()
close: sinon.stub()
};

reopenStub.resolves(device);
await prepareDeviceForFlash({ device, mode: 'dfu' });
expect(reopenStub).to.have.been.calledOnce;
expect(reopenInDfuModeStub).to.have.been.calledOnce;
expect(reopenInNormalStub).to.not.have.been.called;
expect(device.enterListeningMode).to.not.have.been.called;
});
it('prepares the device when is required for dfu mode and currently is in dfu mode', async () => {
const device = {
isOpen: true,
isInDfuMode: true,
close: sinon.stub(),
enterListeningMode: sinon.stub()
close: sinon.stub()
};
reopenStub.resolves(device);
await prepareDeviceForFlash({ device, mode: 'dfu' });
expect(reopenStub).to.have.been.calledOnce;
expect(reopenInDfuModeStub).to.not.have.been.called;
expect(reopenInNormalStub).to.not.have.been.called;
expect(device.enterListeningMode).to.not.have.been.called;
});
});

Expand Down

0 comments on commit 0b69d42

Please sign in to comment.