Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Sep 18, 2023
1 parent 45738f6 commit c552ea8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 218 deletions.
166 changes: 0 additions & 166 deletions src/dfu-mem-layout.js

This file was deleted.

105 changes: 53 additions & 52 deletions src/dfu.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,13 @@ class Dfu {
return this._dev.transferOut(setup, data);
}

async _sendUploadReqest(data, value) { // ???
async _sendUploadReqest(length, value) {
const setup = {
bmRequestType: DfuBmRequestType.DEVICE_TO_HOST,
bRequest: DfuRequestType.DFU_UPLOAD,
wIndex: this._interface,
wValue: value,
wLength: data
wLength: length
};
return this._dev.transferIn(setup);
}
Expand Down Expand Up @@ -763,83 +763,84 @@ class Dfu {
}

async doUpload({ startAddr, maxSize, filename, progress }) {
if (isNaN(startAddr)) {
startAddr = this._memoryInfo.segments[0].start;
console.log("Using inferred start address 0x" + startAddr.toString(16));
} else if (this._getSegment(startAddr) === null) {
console.log(`Start address 0x${startAddr.toString(16)} outside of memory map bounds`);
}

console.log(`Reading up to 0x${maxSize.toString(16)} bytes starting at 0x${startAddr.toString(16)}`);
if (isNaN(startAddr)) {
startAddr = this._memoryInfo.segments[0].start;
console.log('Using inferred start address 0x' + startAddr.toString(16));
} else if (this._getSegment(startAddr) === null) {
console.log(`Start address 0x${startAddr.toString(16)} outside of memory map bounds`);
}

console.log(`Reading up to 0x${maxSize.toString(16)} bytes starting at 0x${startAddr.toString(16)}`);
const state = await this._getStatus();
if (state.state !== DfuDeviceState.dfuIDLE) {
if (state.state !== DfuDeviceState.dfuIDLE) {
await this._clearStatus(); // suggested by dfu-util
await this.abortToIdle();
}
await this.abortToIdle();
}
await this._dfuseCommand(DfuseCommand.DFUSE_COMMAND_SET_ADDRESS_POINTER, startAddr);
await this.abortToIdle();
await this.abortToIdle();

// DfuSe encodes the read address based on the transfer size,
// the block number - 2, and the SET_ADDRESS pointer.
const data = await this._doUploadImpl(maxSize, 2, progress);
// DfuSe encodes the read address based on the transfer size,
// the block number - 2, and the SET_ADDRESS pointer.
const data = await this._doUploadImpl(maxSize, 2, progress);
// current particle implementations only require binary encoding
await fs.writeFile(filename, data, 'binary');
}
}

async _doUploadImpl(maxSize=Infinity, firstBlock=0, progress) {
let transaction = firstBlock;
let blocks = [];
let bytesRead = 0;
async _doUploadImpl(maxSize = Infinity, firstBlock = 0, progress) {
let transaction = firstBlock;
const blocks = [];
let bytesRead = 0;

console.log("Copying data from DFU device to browser");
console.log('Copying data from DFU device to browser');
if (progress) {
progress({ event: 'start-upload', bytes: maxSize });
}

let result;
let bytesToRead;
do {
bytesToRead = Math.min(this._transferSize, maxSize - bytesRead);
result = await this._sendUploadReqest(bytesToRead, transaction++);
console.log("Read " + result.byteLength + " bytes");
if (result.byteLength > 0) {
blocks.push(Buffer.from(result));
bytesRead += result.byteLength;
}
if (Number.isFinite(maxSize)) {
console.log(bytesRead, maxSize, "upload");
let result;
let bytesToRead;
do {
bytesToRead = Math.min(this._transferSize, maxSize - bytesRead);
result = await this._sendUploadReqest(bytesToRead, transaction++);
console.log('Read ' + result.byteLength + ' bytes');
if (result.byteLength > 0) {
blocks.push(Buffer.from(result));
bytesRead += result.byteLength;
}
if (Number.isFinite(maxSize)) {
console.log(bytesRead, maxSize, 'upload');
if (progress) {
progress({ event: 'uploaded', bytes: bytesRead });
}
} else {
console.log(bytesRead, undefined, "upload");
} else {
console.log(bytesRead, undefined, 'upload');
if (progress) {
progress({ event: 'uploaded', bytes: bytesRead });
}
}
} while ((bytesRead < maxSize) && (result.byteLength == bytesToRead));
}
} while ((bytesRead < maxSize) && (result.byteLength === bytesToRead));

if (bytesRead == maxSize) {
await this.abortToIdle();
}
if (bytesRead === maxSize) {
await this.abortToIdle();
}

console.log(`Read ${bytesRead} bytes`);
console.log(`Read ${bytesRead} bytes`);
if (progress) {
progress({ event: 'complete-upload', bytes: bytesRead });
}

return Buffer.concat(blocks);
};
}

async abortToIdle() {
await this._sendAbortRequest();
let state = await this._getStatus();
if (state.state === DfuDeviceState.dfuERROR) {
await this._clearStatus();
state = await this._getStatus();
}
if (state.state !== DfuDeviceState.dfuIDLE) {
throw new Error("Failed to return to idle state after abort: state " + state.state);
}
let state = await this._getStatus();
if (state.state === DfuDeviceState.dfuERROR) {
await this._clearStatus();
state = await this._getStatus();
}
if (state.state !== DfuDeviceState.dfuIDLE) {
throw new Error('Failed to return to idle state after abort: state ' + state.state);
}
}
}

Expand Down

0 comments on commit c552ea8

Please sign in to comment.