diff --git a/src/api/flash/dfu-util.js b/src/api/flash/dfu-util.js index f60174079..7812a386a 100644 --- a/src/api/flash/dfu-util.js +++ b/src/api/flash/dfu-util.js @@ -70,7 +70,7 @@ var device = null; const intf = device.settings["interface"].interfaceNumber; const alt = device.settings.alternate.alternateSetting; const serial = device.device_.serialNumber; - let info = `${mode}: [${vid}:${pid}] cfg=${cfg}, intf=${intf}, alt=${alt}, name="${name}" serial="${serial}"`; + const info = `${mode}: [${vid}:${pid}] cfg=${cfg}, intf=${intf}, alt=${alt}, name="${name}" serial="${serial}"`; return info; } @@ -94,17 +94,17 @@ var device = null; // Check if any interface names were not read correctly if (interfaces.some((intf) => intf.name == null)) { // Manually retrieve the interface name string descriptors - let tempDevice = new dfu.Device(device_, interfaces[0]); + const tempDevice = new dfu.Device(device_, interfaces[0]); await tempDevice.device_.open(); await tempDevice.device_.selectConfiguration(1); - let mapping = await tempDevice.readInterfaceNames(); + const mapping = await tempDevice.readInterfaceNames(); await tempDevice.close(); - for (let intf of interfaces) { + for (const intf of interfaces) { if (intf.name === null) { - let configIndex = intf.configuration.configurationValue; - let intfNumber = intf["interface"].interfaceNumber; - let alt = intf.alternate.alternateSetting; + const configIndex = intf.configuration.configurationValue; + const intfNumber = intf["interface"].interfaceNumber; + const alt = intf.alternate.alternateSetting; intf.name = mapping[configIndex][intfNumber][alt]; } } @@ -112,27 +112,27 @@ var device = null; } function populateInterfaceList(form, device_, interfaces) { - let old_choices = Array.from(form.getElementsByTagName("div")); - for (let radio_div of old_choices) { + const old_choices = Array.from(form.getElementsByTagName("div")); + for (const radio_div of old_choices) { form.removeChild(radio_div); } - let button = form.getElementsByTagName("button")[0]; + const button = form.getElementsByTagName("button")[0]; for (let i = 0; i < interfaces.length; i++) { - let radio = document.createElement("input"); + const radio = document.createElement("input"); radio.type = "radio"; radio.name = "interfaceIndex"; radio.value = i; radio.id = "interface" + i; radio.required = true; - let label = document.createElement("label"); + const label = document.createElement("label"); label.textContent = formatDFUInterfaceAlternate(interfaces[i]); label.className = "radio"; label.setAttribute("for", "interface" + i); - let div = document.createElement("div"); + const div = document.createElement("div"); div.appendChild(radio); div.appendChild(label); form.insertBefore(div, button); @@ -144,11 +144,11 @@ var device = null; // TODO: read the selected configuration's descriptor return device.readConfigurationDescriptor(0).then( (data) => { - let configDesc = dfu.parseConfigurationDescriptor(data); + const configDesc = dfu.parseConfigurationDescriptor(data); let funcDesc = null; - let configValue = device.settings.configuration.configurationValue; + const configValue = device.settings.configuration.configurationValue; if (configDesc.bConfigurationValue == configValue) { - for (let desc of configDesc.descriptors) { + for (const desc of configDesc.descriptors) { if ( desc.bDescriptorType == 0x21 && desc.hasOwnProperty("bcdDFUVersion") @@ -201,7 +201,7 @@ var device = null; console.log(msg); if (logContext) { - let info = document.createElement("p"); + const info = document.createElement("p"); info.className = "info"; info.textContent = msg; logContext.appendChild(info); @@ -212,7 +212,7 @@ var device = null; console.log(msg); if (logContext) { - let warning = document.createElement("p"); + const warning = document.createElement("p"); warning.className = "warning"; warning.textContent = msg; logContext.appendChild(warning); @@ -223,7 +223,7 @@ var device = null; console.log(msg); if (logContext) { - let error = document.createElement("p"); + const error = document.createElement("p"); error.className = "error"; error.textContent = msg; logContext.appendChild(error); @@ -248,16 +248,16 @@ var device = null; } document.addEventListener("DOMContentLoaded", (event) => { - let connectButton = document.querySelector("#connect"); - let downloadButton = document.querySelector("#download"); - let statusDisplay = document.querySelector("#status"); - let infoDisplay = document.querySelector("#usbInfo"); - let dfuDisplay = document.querySelector("#dfuInfo"); - let vidField = document.querySelector("#vid"); - let interfaceDialog = document.querySelector("#interfaceDialog"); - let interfaceForm = document.querySelector("#interfaceForm"); - - let searchParams = new URLSearchParams(window.location.search); + const connectButton = document.querySelector("#connect"); + const downloadButton = document.querySelector("#download"); + const statusDisplay = document.querySelector("#status"); + const infoDisplay = document.querySelector("#usbInfo"); + const dfuDisplay = document.querySelector("#dfuInfo"); + const vidField = document.querySelector("#vid"); + const interfaceDialog = document.querySelector("#interfaceDialog"); + const interfaceForm = document.querySelector("#interfaceForm"); + + const searchParams = new URLSearchParams(window.location.search); let fromLandingPage = false; let vid; // = parseInt("0x3496"); // Default to keyboardio devices // Set the vendor ID from the landing page URL @@ -287,18 +287,18 @@ var device = null; fromLandingPage = true; } - let configForm = document.querySelector("#configForm"); + const configForm = document.querySelector("#configForm"); let transferSize = 1024; let firmwareFile = null; - let downloadLog = document.querySelector("#downloadLog"); - let firmwareVersionSelect = document.getElementById("firmwareVersion"); + const downloadLog = document.querySelector("#downloadLog"); + const firmwareVersionSelect = document.getElementById("firmwareVersion"); let manifestationTolerant = true; async function fetchSelectedFirmware() { - let firmwareVersion = + const firmwareVersion = firmwareVersionSelect.options[firmwareVersionSelect.selectedIndex] .value; @@ -379,10 +379,10 @@ var device = null; throw error; } - let memorySummary = ""; + const memorySummary = ""; if (desc && Object.keys(desc).length > 0) { device.properties = desc; - let info = `WillDetach=${desc.WillDetach}, ManifestationTolerant=${ + const info = `WillDetach=${desc.WillDetach}, ManifestationTolerant=${ desc.ManifestationTolerant }, CanUpload=${desc.CanUpload}, CanDnload=${ desc.CanDnload @@ -394,12 +394,6 @@ var device = null; if (desc.CanDnload) { manifestationTolerant = desc.ManifestationTolerant; } - - if (device.settings.alternate.interfaceProtocol == 0x02) { - if (!desc.CanDnload) { - dnloadButton.disabled = true; - } - } } // Bind logging methods @@ -442,8 +436,8 @@ var device = null; function autoConnect(vid, serial) { dfu.findAllDfuInterfaces().then(async (dfu_devices) => { - let matching_devices = []; - for (let dfu_device of dfu_devices) { + const matching_devices = []; + for (const dfu_device of dfu_devices) { if (serial) { if (dfu_device.device_.serialNumber == serial) { matching_devices.push(dfu_device); @@ -475,12 +469,14 @@ var device = null; vid = parseInt(vidField.value, 16); }); - connectButton.addEventListener("click", function () { + connectButton.addEventListener("click", clickConnect); + + const clickConnect = () => { if (device) { device.close().then(onDisconnect); device = null; } else { - let filters = []; + const filters = []; if (serial) { filters.push({ serialNumber: serial }); } else if (vid) { @@ -489,7 +485,7 @@ var device = null; navigator.usb .requestDevice({ filters: filters }) .then(async (selectedDevice) => { - let interfaces = dfu.findDeviceDfuInterfaces(selectedDevice); + const interfaces = dfu.findDeviceDfuInterfaces(selectedDevice); console.log(selectedDevice.productId); if (selectedDevice.productId == 0x0006) { @@ -536,9 +532,10 @@ var device = null; statusDisplay.textContent = error; }); } - }); + }; - downloadButton.addEventListener("click", async function (event) { + downloadButton.addEventListener("click", clickDownload); + const clickDownload = async (event) => { event.preventDefault(); event.stopPropagation(); if (!configForm.checkValidity()) { @@ -551,7 +548,7 @@ var device = null; setLogContext(downloadLog); clearLog(downloadLog); try { - let status = await device.getStatus(); + const status = await device.getStatus(); if (status.state == dfu.dfuERROR) { await device.clearStatus(); } @@ -562,7 +559,6 @@ var device = null; .do_download(transferSize, firmwareFile, manifestationTolerant) .then( () => { - logInfo("Done!"); setLogContext(null); if (!manifestationTolerant) { device.waitDisconnected(5000).then( @@ -589,7 +585,7 @@ var device = null; } //return false; - }); + }; // Check if WebUSB is available if (typeof navigator.usb !== "undefined") { diff --git a/src/api/flash/dfu.js b/src/api/flash/dfu.js index 521f113d1..9b8526584 100644 --- a/src/api/flash/dfu.js +++ b/src/api/flash/dfu.js @@ -52,16 +52,16 @@ var dfu = {}; }; dfu.findDeviceDfuInterfaces = function (device) { - let interfaces = []; - for (let conf of device.configurations) { - for (let intf of conf.interfaces) { - for (let alt of intf.alternates) { + const interfaces = []; + for (const conf of device.configurations) { + for (const intf of conf.interfaces) { + for (const alt of intf.alternates) { if ( alt.interfaceClass == 0xfe && alt.interfaceSubclass == 0x01 && (alt.interfaceProtocol == 0x01 || alt.interfaceProtocol == 0x02) ) { - let settings = { + const settings = { configuration: conf, interface: intf, alternate: alt, @@ -78,10 +78,10 @@ var dfu = {}; dfu.findAllDfuInterfaces = function () { return navigator.usb.getDevices().then((devices) => { - let matches = []; - for (let device of devices) { - let interfaces = dfu.findDeviceDfuInterfaces(device); - for (let interface_ of interfaces) { + const matches = []; + for (const device of devices) { + const interfaces = dfu.findDeviceDfuInterfaces(device); + for (const interface_ of interfaces) { matches.push(new dfu.Device(device, interface_)); } } @@ -211,7 +211,7 @@ var dfu = {}; result = await this.device_.controlTransferIn(request_setup, bLength); if (result.status == "ok") { const len = (bLength - 2) / 2; - let u16_words = []; + const u16_words = []; for (let i = 0; i < len; i++) { u16_words.push(result.data.getUint16(2 + i * 2, true)); } @@ -231,20 +231,20 @@ var dfu = {}; dfu.Device.prototype.readInterfaceNames = async function () { const DT_INTERFACE = 4; - let configs = {}; - let allStringIndices = new Set(); + const configs = {}; + const allStringIndices = new Set(); for ( let configIndex = 0; configIndex < this.device_.configurations.length; configIndex++ ) { const rawConfig = await this.readConfigurationDescriptor(configIndex); - let configDesc = dfu.parseConfigurationDescriptor(rawConfig); - let configValue = configDesc.bConfigurationValue; + const configDesc = dfu.parseConfigurationDescriptor(rawConfig); + const configValue = configDesc.bConfigurationValue; configs[configValue] = {}; // Retrieve string indices for interface names - for (let desc of configDesc.descriptors) { + for (const desc of configDesc.descriptors) { if (desc.bDescriptorType == DT_INTERFACE) { if (!(desc.bInterfaceNumber in configs[configValue])) { configs[configValue][desc.bInterfaceNumber] = {}; @@ -258,9 +258,9 @@ var dfu = {}; } } - let strings = {}; + const strings = {}; // Retrieve interface name strings - for (let index of allStringIndices) { + for (const index of allStringIndices) { try { strings[index] = await this.readStringDescriptor(index, 0x0409); } catch (error) { @@ -269,9 +269,9 @@ var dfu = {}; } } - for (let configValue in configs) { - for (let intfNumber in configs[configValue]) { - for (let alt in configs[configValue][intfNumber]) { + for (const configValue in configs) { + for (const intfNumber in configs[configValue]) { + for (const alt in configs[configValue][intfNumber]) { const iIndex = configs[configValue][intfNumber][alt]; configs[configValue][intfNumber][alt] = strings[iIndex]; } @@ -301,8 +301,8 @@ var dfu = {}; }; dfu.parseConfigurationDescriptor = function (data) { - let descriptorData = new DataView(data.buffer.slice(9)); - let descriptors = dfu.parseSubDescriptors(descriptorData); + const descriptorData = new DataView(data.buffer.slice(9)); + const descriptors = dfu.parseSubDescriptors(descriptorData); return { bLength: data.getUint8(0), bDescriptorType: data.getUint8(1), @@ -349,13 +349,13 @@ var dfu = {}; const USB_CLASS_APP_SPECIFIC = 0xfe; const USB_SUBCLASS_DFU = 0x01; let remainingData = descriptorData; - let descriptors = []; + const descriptors = []; let currIntf; let inDfuIntf = false; while (remainingData.byteLength > 2) { - let bLength = remainingData.getUint8(0); - let bDescriptorType = remainingData.getUint8(1); - let descData = new DataView(remainingData.buffer.slice(0, bLength)); + const bLength = remainingData.getUint8(0); + const bDescriptorType = remainingData.getUint8(1); + const descData = new DataView(remainingData.buffer.slice(0, bLength)); if (bDescriptorType == DT_INTERFACE) { currIntf = dfu.parseInterfaceDescriptor(descData); if ( @@ -368,11 +368,11 @@ var dfu = {}; } descriptors.push(currIntf); } else if (inDfuIntf && bDescriptorType == DT_DFU_FUNCTIONAL) { - let funcDesc = dfu.parseFunctionalDescriptor(descData); + const funcDesc = dfu.parseFunctionalDescriptor(descData); descriptors.push(funcDesc); currIntf.descriptors.push(funcDesc); } else { - let desc = { + const desc = { bLength: bLength, bDescriptorType: bDescriptorType, data: descData, @@ -488,8 +488,8 @@ var dfu = {}; }; dfu.Device.prototype.waitDisconnected = async function (timeout) { - let device = this; - let usbDevice = this.device_; + const device = this; + const usbDevice = this.device_; return new Promise(function (resolve, reject) { let timeoutID; if (timeout > 0) { @@ -578,7 +578,7 @@ var dfu = {}; first_block = 0 ) { let transaction = first_block; - let blocks = []; + const blocks = []; let bytes_read = 0; this.logInfo("Copying data from DFU device to browser"); @@ -614,7 +614,7 @@ var dfu = {}; dfu.Device.prototype.poll_until = async function (state_predicate) { let dfu_status = await this.getStatus(); - let device = this; + const device = this; function async_sleep(duration_ms) { return new Promise(function (resolve, reject) { device.logDebug("Sleeping for " + duration_ms + "ms"); @@ -643,7 +643,7 @@ var dfu = {}; manifestationTolerant ) { let bytes_sent = 0; - let expected_size = data.byteLength; + const expected_size = data.byteLength; let transaction = 0; this.logInfo("Copying data from browser to DFU device"); @@ -724,7 +724,7 @@ var dfu = {}; this.logInfo("manifestation not tolerant"); // Try polling once to initiate manifestation try { - let final_status = await this.getStatus(); + const final_status = await this.getStatus(); this.logDebug( `Final DFU status: state=${final_status.state}, status=${final_status.status}` );