Skip to content

Commit

Permalink
Merge pull request #2544 from balena-io/usbboot-limit-transfer-size
Browse files Browse the repository at this point in the history
Usbboot limit transfer size
  • Loading branch information
zvin authored Nov 13, 2018
2 parents 333298e + 7fb382b commit b298e53
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ lint-spell:
codespell \
--dictionary - \
--dictionary dictionary.txt \
--skip *.gz,*.bz2,*.xz,*.zip,*.img,*.dmg,*.iso,*.rpi-sdcard,.DS_Store,*.dtb,*.dtbo,*.dat,*.elf,*.bin,*.foo,xz-without-extension \
--skip *.svg *.gz,*.bz2,*.xz,*.zip,*.img,*.dmg,*.iso,*.rpi-sdcard,.DS_Store,*.dtb,*.dtbo,*.dat,*.elf,*.bin,*.foo,xz-without-extension \
lib tests docs scripts Makefile *.md LICENSE

lint: lint-js lint-sass lint-cpp lint-html lint-spell
Expand Down
16 changes: 13 additions & 3 deletions lib/sdk/adapters/usbboot/protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const usb = require('./usb')
const NULL_BUFFER_SIZE = 0
const NULL_BUFFER = Buffer.alloc(NULL_BUFFER_SIZE)

const ONE_MEGABYTE = 1048576

/**
* @summary The size of the boot message bootcode length section
* @type {Number}
Expand Down Expand Up @@ -206,6 +208,12 @@ exports.sendBufferSize = (device, size) => {
})
}

const chunks = function *(buffer, size) {
for (let start = 0; start < buffer.length; start += size) {
yield buffer.slice(start, start + size)
}
}

/**
* @summary Write a buffer to an OUT endpoint
* @function
Expand All @@ -231,9 +239,11 @@ exports.write = (device, endpoint, buffer) => {
.delay(USB_REQUEST_DELAY_MS)

.then(() => {
return Bluebird.fromCallback((callback) => {
endpoint.timeout = USB_BULK_TRANSFER_TIMEOUT_MS
endpoint.transfer(buffer, callback)
endpoint.timeout = USB_BULK_TRANSFER_TIMEOUT_MS
return Bluebird.each(chunks(buffer, ONE_MEGABYTE), (chunk) => {
return Bluebird.fromCallback((callback) => {
endpoint.transfer(chunk, callback)
})
})
})
}
Expand Down

0 comments on commit b298e53

Please sign in to comment.