Skip to content

Commit

Permalink
[tests] wait until idle before running dnload. And other lint/minor
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jul 26, 2023
1 parent c4b2022 commit 1744e36
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 80 deletions.
54 changes: 18 additions & 36 deletions src/dfu.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class Dfu {

function asyncSleep(durationMs) {
return new Promise((resolve) => {
console.log('Sleeping for ' + durationMs + 'ms');
// this._log.trace('Sleeping for ' + durationMs + 'ms');
setTimeout(resolve, durationMs);
});
}
Expand Down Expand Up @@ -374,7 +374,7 @@ class Dfu {
if (triesLeft === 0 || error !== 'stall') {
throw new Error('Error during special DfuSe command ' + commandNames[command] + ':' + error);
}
console.log('dfuse error, retrying', error);
this._log.trace('dfuse error, retrying', error);

await new Promise(resolve => setTimeout(resolve, 1000));

Expand Down Expand Up @@ -476,7 +476,7 @@ class Dfu {
let bytesErased = 0;
const bytesToErase = endAddr - addr;
if (bytesToErase > 0) {
console.log(bytesErased, bytesToErase, 'erase');
this._log.info(bytesErased, bytesToErase, 'erase');
}

while (addr < endAddr) {
Expand All @@ -487,16 +487,16 @@ class Dfu {
// Skip over the non-erasable section
bytesErased = Math.min(bytesErased + segment.end - addr, bytesToErase);
addr = segment.end;
console.log(bytesErased, bytesToErase, 'erase');
this._log.trace(bytesErased, bytesToErase, 'erase');
continue;
}
const sectorIndex = Math.floor((addr - segment.start) / segment.sectorSize);
const sectorAddr = segment.start + sectorIndex * segment.sectorSize;
console.log(`Erasing ${segment.sectorSize}B at 0x${sectorAddr.toString(16)}`);
this._log.trace(`Erasing ${segment.sectorSize}B at 0x${sectorAddr.toString(16)}`);
await this.dfuseCommand(DfuseCommand.DFUSE_COMMAND_ERASE, sectorAddr, 4);
addr = sectorAddr + segment.sectorSize;
bytesErased += segment.sectorSize;
console.log(bytesErased, bytesToErase, 'erase');
this._log.info(bytesErased, bytesToErase, 'erase');
}
}

Expand Down Expand Up @@ -530,18 +530,18 @@ class Dfu {
let startAddress = startAddr;
if (isNaN(startAddress)) {
startAddress = this.memoryInfo.segments[0].start;
console.log('Using inferred start address 0x' + startAddress.toString(16));
this._log.warn('Using inferred start address 0x' + startAddress.toString(16));
} else if (this.getSegment(startAddress) === null) {
console.log(`Start address 0x${startAddress.toString(16)} outside of memory map bounds`);
this._log.error(`Start address 0x${startAddress.toString(16)} outside of memory map bounds`);
}
const expectedSize = data.byteLength;

if (!options.noErase) {
console.log('Erasing DFU device memory');
this._log.info('Erasing DFU device memory');
await this.erase(startAddress, expectedSize);
}

console.log('Copying binary data to DFU device startAddress=' + startAddress + ' total_expected_size=' + expectedSize);
this._log.info('Copying binary data to DFU device startAddress=' + startAddress + ' total_expected_size=' + expectedSize);

let bytesSent = 0;
let address = startAddress;
Expand All @@ -553,10 +553,10 @@ class Dfu {
let dfuStatus;
try {
await this.dfuseCommand(DfuseCommand.DFUSE_COMMAND_SET_ADDRESS_POINTER, address, 4);
console.log(`Set address to 0x${address.toString(16)}`);
this._log.trace(`Set address to 0x${address.toString(16)}`);
bytesWritten = await this._sendDnloadRequest(data.slice(bytesSent, bytesSent + chunkSize), 2);
dfuStatus = await this.poll_until_idle(DfuDeviceState.dfuDNLOAD_SYNC);
console.log('Sent ' + bytesWritten + ' bytes');
this._log.trace('Sent ' + bytesWritten + ' bytes');
dfuStatus = await this._goIntoDfuIdleOrDfuDnloadIdle();
address += chunkSize;
} catch (error) {
Expand All @@ -567,15 +567,15 @@ class Dfu {
throw new Error(`DFU DOWNLOAD failed state=${dfuStatus.state}, status=${dfuStatus.status}`);
}

console.log('Wrote ' + bytesWritten + ' bytes');
this._log.trace('Wrote ' + bytesWritten + ' bytes');
bytesSent += bytesWritten;

console.log(bytesSent, expectedSize, 'program');
this._log.info(bytesSent, expectedSize, 'program');
}
console.log(`Wrote ${bytesSent} bytes`);
this._log.info(`Wrote ${bytesSent} bytes`);

if (options.doManifestation) {
console.log('Manifesting new firmware');
this._log.info('Manifesting new firmware');
await this._goIntoDfuIdleOrDfuDnloadIdle();
try {
await this.dfuseCommand(DfuseCommand.DFUSE_COMMAND_SET_ADDRESS_POINTER, startAddress, 4);
Expand All @@ -586,26 +586,8 @@ class Dfu {
}
}

// async do_upload(memoryInfo, startAddr, max_size) {
// let startAddress = startAddr;
// if (isNaN(startAddress)) {
// startAddress = memoryInfo.segments[0].start;
// console.log("Using inferred start address 0x" + startAddress.toString(16));
// } else if (this.getSegment(memoryInfo, startAddress) === null) {
// console.log(`Start address 0x${startAddress.toString(16)} outside of memory map bounds`);
// }
//
// console.log(`Reading up to 0x${max_size.toString(16)} bytes starting at 0x${startAddress.toString(16)}`);
// let state = await this.getState();
// if (state != DfuDeviceState.dfuIDLE) {
// await this.abortToIdle();
// }
// await this.dfuseCommand(DfuseCommand.DFUSE_COMMAND_SET_ADDRESS_POINTER, startAddress, 4);
// await this.abortToIdle();
//
// // DfuSe encodes the read address based on the transfer size,
// // the block number - 2, and the SET_ADDRESS pointer.
// return await dfu.Device.prototype.do_upload.call(this, this.transferSize, max_size, 2);
// async do_upload(startAddr, max_size) {
// // TODO
// }
}

Expand Down
30 changes: 2 additions & 28 deletions test/dfu-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ describe('dfu device', () => { // actually tests src/dfu.js which is the dfu dri
expect(argonDev.isOpen).to.be.true;
let error;
try {
await argonDev._dfu._goIntoDfuIdleOrDfuDnloadIdle();
await argonDev._dfu.dfuseCommand(0x21, 0x08060000, 5);
} catch (_error) {
error = _error;
console.log('Error is', error);
}

expect(error).to.not.be.an.instanceof(Error);
Expand Down Expand Up @@ -267,7 +267,7 @@ describe('dfu device', () => { // actually tests src/dfu.js which is the dfu dri
await argonDev._dfu.erase(startAddr, length);

expect(dfuseCommandStub.calledOnce).to.be.true;
expect(dfuseCommandStub.calledWithExactly(DfuSeCommand.DFUSE_COMMAND_ERASE ,sectorAddr, 4)).to.be.true;
expect(dfuseCommandStub.calledWithExactly(DfuseCommand.DFUSE_COMMAND_ERASE ,sectorAddr, 4)).to.be.true;
});

it('erases the memory on a p2', async () => {
Expand All @@ -287,31 +287,5 @@ describe('dfu device', () => { // actually tests src/dfu.js which is the dfu dri
expect(dfuseCommandStub.callCount).to.equal(247);
});
});

describe('do_download', () => {
it('downloads the memory on a p2', async () => {
fakeUsb.addP2({ dfu: true });
const devs = await getDevices();
expect(devs).to.not.be.empty;
const p2Dev = devs[0];
await p2Dev.open();
expect(p2Dev.isOpen).to.be.true;
p2Dev._dfu.memoryInfo = InternalFlashParsedP2;
const startAddr = 134610944;
const length = 1009100;
const data = Buffer.alloc(length);
const dfuseCommandStub = sinon.stub(p2Dev._dfu, 'dfuseCommand');
sinon.stub(p2Dev._dfu, 'poll_until_idle');
const dfuStatus = {
state: DfuDeviceState.dfuDNLOAD_SYNC,
pollTimeout: 100,
status: DfuDeviceStatus.OK };
sinon.stub(p2Dev._dfu, '_goIntoDfuIdleOrDfuDnloadIdle').resolves(dfuStatus);

await p2Dev._dfu.do_download(startAddr, data, {});

expect(dfuseCommandStub.callCount).to.equal(248);
});
});
});
});
12 changes: 4 additions & 8 deletions test/support/fake-usb.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ class DfuClass {

function asyncSleep(durationMs) {
return new Promise((resolve) => {
console.log('Sleeping for ' + durationMs + 'ms');
// this._log.trace('Sleeping for ' + durationMs + 'ms');
setTimeout(resolve, durationMs);
});
}
Expand Down Expand Up @@ -494,21 +494,17 @@ class DfuClass {
}

_dnload(setup, data) {

Check failure on line 496 in test/support/fake-usb.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v12)

'setup' is defined but never used

Check failure on line 496 in test/support/fake-usb.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v12)

'data' is defined but never used

Check failure on line 496 in test/support/fake-usb.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v14)

'setup' is defined but never used

Check failure on line 496 in test/support/fake-usb.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v14)

'data' is defined but never used

Check failure on line 496 in test/support/fake-usb.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v16)

'setup' is defined but never used

Check failure on line 496 in test/support/fake-usb.js

View workflow job for this annotation

GitHub Actions / Test (Node.js v16)

'data' is defined but never used
// Handle only leave request
if (data && data.length > 0) {
// throw new UsbError('Unsupport DFU_DNLOAD request');
}
// if (data && data.length > 0) {
// throw new UsbError('Unsupport DFU_DNLOAD request');
// }

switch (this._state.state) {
case dfu.DfuDeviceState.dfuDNBUSY:
this._setState(dfu.DfuDeviceState.dfuDNLOAD_IDLE);
case dfu.DfuDeviceState.dfuIDLE:
case dfu.DfuDeviceState.dfuDNLOAD_IDLE: {
// Go into dfuMANIFEST_SYNC state
this._setState(dfu.DfuDeviceState.dfuMANIFEST_SYNC);
break;
}

default: {
this._setError(dfu.DfuDeviceStatus.errUNKNOWN);
throw new UsbError('Invalid state (endpoint stalled)');
Expand Down
16 changes: 8 additions & 8 deletions test/support/usb-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ const InternalFlashParsedElectron = {
};

const InternalFlashParsedP2 = {
"name": "Internal Flash",
"segments": [
'name': 'Internal Flash',
'segments': [
{
"start": 134217728,
"sectorSize": 4096,
"end": 142606336,
"readable": true,
"erasable": true,
"writable": true
'start': 134217728,
'sectorSize': 4096,
'end': 142606336,
'readable': true,
'erasable': true,
'writable': true
}
]
};
Expand Down

0 comments on commit 1744e36

Please sign in to comment.