From f5dab89965595311d5467dd7a9937be6f83b64be Mon Sep 17 00:00:00 2001 From: Mark Haslinghuis Date: Sat, 15 Jan 2022 05:51:23 +0100 Subject: [PATCH 1/3] fix_reboot Fix clicks reinitializeConnection should be callback MSP fix --- src/js/backup_restore.js | 2 +- src/js/msp.js | 3 +- src/js/serial.js | 2 +- src/js/serial_backend.js | 56 ++++++++++++++++------------------ src/js/tabs/cli.js | 2 +- src/js/tabs/configuration.js | 3 +- src/js/tabs/failsafe.js | 3 +- src/js/tabs/motors.js | 2 +- src/js/tabs/onboard_logging.js | 3 +- src/js/tabs/ports.js | 3 +- src/js/tabs/receiver.js | 3 +- src/js/tabs/transponder.js | 3 +- 12 files changed, 38 insertions(+), 47 deletions(-) diff --git a/src/js/backup_restore.js b/src/js/backup_restore.js index 94fb3f5b15..91be2d8166 100644 --- a/src/js/backup_restore.js +++ b/src/js/backup_restore.js @@ -911,7 +911,7 @@ function configuration_restore(callback) { GUI.log(i18n.getMessage('eeprom_saved_ok')); GUI.tab_switch_cleanup(function() { - MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, reinitialiseConnection('setup', _callback)); + MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, reinitializeConnection('setup', _callback)); }); } } diff --git a/src/js/msp.js b/src/js/msp.js index 5e44749c75..f2219ba629 100644 --- a/src/js/msp.js +++ b/src/js/msp.js @@ -321,9 +321,10 @@ const MSP = { return; } - if (code === undefined) { + if (code === undefined || !serial.connectionId) { return; } + let bufferOut; if (code <= 254) { bufferOut = this.encode_message_v1(code, data); diff --git a/src/js/serial.js b/src/js/serial.js index 6acc7cc144..f8edf393ac 100644 --- a/src/js/serial.js +++ b/src/js/serial.js @@ -457,7 +457,7 @@ const serial = { if (GUI.connected_to || GUI.connecting_to) { $('a.connect').trigger('click'); } else { - self.disconnect(); + serial.disconnect(); } }, }; diff --git a/src/js/serial_backend.js b/src/js/serial_backend.js index 2f8168c2a4..da8e48ede8 100644 --- a/src/js/serial_backend.js +++ b/src/js/serial_backend.js @@ -2,6 +2,7 @@ let mspHelper; let connectionTimestamp; +let clicks = false; function initializeSerialBackend() { GUI.updateManualPortVisibility = function(){ @@ -43,11 +44,8 @@ function initializeSerialBackend() { $('div.connect_controls a.connect').click(function () { if (GUI.connect_lock != true) { // GUI control overrides the user control - const thisElement = $(this); - const clicks = thisElement.data('clicks'); - const toggleStatus = function() { - thisElement.data("clicks", !clicks); + clicks = !clicks; }; GUI.configuration_loaded = false; @@ -336,7 +334,7 @@ function abortConnect() { $('div#port-picker #port, div#port-picker #baud, div#port-picker #delay').prop('disabled', false); // reset data - $('div#connectbutton a.connect').data("clicks", false); + clicks = false; } function processBoardInfo() { @@ -829,34 +827,32 @@ function update_dataflash_global() { } } -function reinitialiseConnection(originatorTab, callback) { +function reinitializeConnection(originatorTab, callback) { + + // Close connection gracefully if it still exists. + if (serial.connectionId) { + if (GUI.connected_to || GUI.connecting_to) { + $('a.connect').trigger('click'); + } else { + serial.disconnect(); + } + } + GUI.log(i18n.getMessage('deviceRebooting')); + let connectionTimeout = 200; - ConfigStorage.get('connectionTimeout', function (result) { - if (result.connectionTimeout) { - connectionTimeout = result.connectionTimeout; - } + const result = ConfigStorage.get('connectionTimeout'); - if (FC.boardHasVcp()) { // VCP-based flight controls may crash old drivers, we catch and reconnect - GUI.timeout_add('waiting_for_disconnect', function waiting_for_bootup() { - if (callback) { - callback(); - } - }, connectionTimeout); - //TODO: Need to work out how to do a proper reconnect here. - // caveat: Timeouts set with `GUI.timeout_add()` are removed on disconnect. - } else { - GUI.timeout_add('waiting_for_bootup', function waiting_for_bootup() { - MSP.send_message(MSPCodes.MSP_STATUS, false, false, function() { - GUI.log(i18n.getMessage('deviceReady')); - originatorTab.initialize(false, $('#content').scrollTop()); - }); + if (result.connectionTimeout) { + connectionTimeout = result.connectionTimeout; + } - if (callback) { - callback(); - } + setTimeout(() => { + MSP.send_message(MSPCodes.MSP_STATUS, false, false, () => { + GUI.log(i18n.getMessage('deviceReady')); + originatorTab.initialize(false, $('#content').scrollTop()); + }); - }, connectionTimeout); // 1500 ms seems to be just the right amount of delay to prevent data request timeouts - } - }); + callback?.(); + }, connectionTimeout); } diff --git a/src/js/tabs/cli.js b/src/js/tabs/cli.js index 95695f2a72..150a949d17 100644 --- a/src/js/tabs/cli.js +++ b/src/js/tabs/cli.js @@ -462,7 +462,7 @@ TABS.cli.read = function (readInfo) { CONFIGURATOR.cliActive = false; CONFIGURATOR.cliValid = false; GUI.log(i18n.getMessage('cliReboot')); - reinitialiseConnection(self); + reinitializeConnection(self); } } diff --git a/src/js/tabs/configuration.js b/src/js/tabs/configuration.js index c83a0983ea..85925f2441 100644 --- a/src/js/tabs/configuration.js +++ b/src/js/tabs/configuration.js @@ -626,8 +626,7 @@ TABS.configuration.initialize = function (callback) { GUI.log(i18n.getMessage('configurationEepromSaved')); GUI.tab_switch_cleanup(function() { - MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false); - reinitialiseConnection(self); + MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, reinitializeConnection(self)); }); } diff --git a/src/js/tabs/failsafe.js b/src/js/tabs/failsafe.js index ecb163411d..068f4a671b 100644 --- a/src/js/tabs/failsafe.js +++ b/src/js/tabs/failsafe.js @@ -413,8 +413,7 @@ TABS.failsafe.initialize = function (callback, scrollPosition) { GUI.log(i18n.getMessage('configurationEepromSaved')); GUI.tab_switch_cleanup(function() { - MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false); - reinitialiseConnection(self); + MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, reinitializeConnection(self)); }); } diff --git a/src/js/tabs/motors.js b/src/js/tabs/motors.js index f4ea4d4586..d2f2df1248 100644 --- a/src/js/tabs/motors.js +++ b/src/js/tabs/motors.js @@ -1117,7 +1117,7 @@ TABS.motors.initialize = function (callback) { function reboot() { GUI.log(i18n.getMessage('configurationEepromSaved')); - MSP.promise(MSPCodes.MSP_SET_REBOOT, false, false).then(() => reinitialiseConnection()); + MSP.promise(MSPCodes.MSP_SET_REBOOT, false, false).then(() => reinitializeConnection(self)); } function showDialogMixerReset(message) { diff --git a/src/js/tabs/onboard_logging.js b/src/js/tabs/onboard_logging.js index 9e9feeec0b..9cfb670f58 100644 --- a/src/js/tabs/onboard_logging.js +++ b/src/js/tabs/onboard_logging.js @@ -48,8 +48,7 @@ TABS.onboard_logging.initialize = function (callback) { GUI.log(i18n.getMessage('configurationEepromSaved')); GUI.tab_switch_cleanup(function() { - MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false); - reinitialiseConnection(self); + MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, reinitializeConnection(self)); }); } diff --git a/src/js/tabs/ports.js b/src/js/tabs/ports.js index 4470550840..79b0ade1c7 100644 --- a/src/js/tabs/ports.js +++ b/src/js/tabs/ports.js @@ -412,8 +412,7 @@ TABS.ports.initialize = function (callback, scrollPosition) { GUI.log(i18n.getMessage('configurationEepromSaved')); GUI.tab_switch_cleanup(function() { - MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false); - reinitialiseConnection(self); + MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, reinitializeConnection(self)); }); } } diff --git a/src/js/tabs/receiver.js b/src/js/tabs/receiver.js index 3291b74154..ac80f5d208 100644 --- a/src/js/tabs/receiver.js +++ b/src/js/tabs/receiver.js @@ -486,8 +486,7 @@ TABS.receiver.initialize = function (callback) { GUI.log(i18n.getMessage('configurationEepromSaved')); if (boot) { GUI.tab_switch_cleanup(function() { - MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false); - reinitialiseConnection(tab); + MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, reinitializeConnection(tab)); }); } } diff --git a/src/js/tabs/transponder.js b/src/js/tabs/transponder.js index e830bcc822..179e50cedd 100644 --- a/src/js/tabs/transponder.js +++ b/src/js/tabs/transponder.js @@ -303,8 +303,7 @@ TABS.transponder.initialize = function(callback, scrollPosition) { GUI.log(i18n.getMessage('transponderEepromSaved')); if ( $(_this).hasClass('reboot') ) { GUI.tab_switch_cleanup(function() { - MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false); - reinitialiseConnection(self); + MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, reinitializeConnection(self)); }); } }); From 8e17e5a59be6ffec1bbe49df6806ac866a554b5d Mon Sep 17 00:00:00 2001 From: Mark Haslinghuis Date: Wed, 23 Feb 2022 00:48:50 +0100 Subject: [PATCH 2/3] Change timeout for MacOS --- src/js/protocols/stm32.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/js/protocols/stm32.js b/src/js/protocols/stm32.js index 28655eb2d8..7f2c31bbee 100644 --- a/src/js/protocols/stm32.js +++ b/src/js/protocols/stm32.js @@ -116,7 +116,8 @@ STM32_protocol.prototype.connect = function (port, baud, hex, options, callback) if (disconnectionResult) { // delay to allow board to boot in bootloader mode // required to detect if a DFU device appears - setTimeout(startFlashing, 1000); + // MacOs seems to need about 5 seconds delay + setTimeout(startFlashing, GUI.operating_system === 'MacOS' ? 5000 : 1000); } else { GUI.connect_lock = false; } From e7ca4dc07dc754c358cca821f9492dc75daf5aef Mon Sep 17 00:00:00 2001 From: Mark Haslinghuis Date: Thu, 24 Feb 2022 01:04:30 +0100 Subject: [PATCH 3/3] Fix logging --- src/js/protocols/stm32usbdfu.js | 167 ++++++++++++++++---------------- 1 file changed, 85 insertions(+), 82 deletions(-) diff --git a/src/js/protocols/stm32usbdfu.js b/src/js/protocols/stm32usbdfu.js index cfa2d5e20c..3448503d73 100644 --- a/src/js/protocols/stm32usbdfu.js +++ b/src/js/protocols/stm32usbdfu.js @@ -15,7 +15,7 @@ // Task for the brave ones. There are quite a few shadow variables which clash when // const or let are used. So need to run thorough tests when chaning `var` /* eslint-disable no-var */ -var STM32DFU_protocol = function () { +const STM32DFU_protocol = function () { this.callback = null; this.hex = null; this.verify_hex = []; @@ -112,7 +112,7 @@ STM32DFU_protocol.prototype.openDevice = function (device) { if (checkChromeRuntimeError()) { console.log('Failed to open USB device!'); GUI.log(i18n.getMessage('usbDeviceOpenFail')); - if(GUI.operating_system === 'Linux') { + if (GUI.operating_system === 'Linux') { GUI.log(i18n.getMessage('usbDeviceUdevNotice')); } return; @@ -129,15 +129,15 @@ STM32DFU_protocol.prototype.openDevice = function (device) { STM32DFU_protocol.prototype.closeDevice = function () { const self = this; - chrome.usb.closeDevice(this.handle, function closed() { + chrome.usb.closeDevice(self.handle, function closed() { if (checkChromeRuntimeError()) { console.log('Failed to close USB device!'); GUI.log(i18n.getMessage('usbDeviceCloseFail')); + } else { + GUI.log(i18n.getMessage('usbDeviceClosed')); + console.log(`Device closed with Handle ID: ${self.handle.handle}`); } - GUI.log(i18n.getMessage('usbDeviceClosed')); - console.log(`Device closed with Handle ID: ${self.handle.handle}`); - self.handle = null; }); }; @@ -145,21 +145,18 @@ STM32DFU_protocol.prototype.closeDevice = function () { STM32DFU_protocol.prototype.claimInterface = function (interfaceNumber) { const self = this; - chrome.usb.claimInterface(this.handle, interfaceNumber, function claimed() { - // Don't perform the error check on MacOS at this time as there seems to be a bug - // where it always reports the Chrome error "Error claiming interface." even though - // the interface is in fact successfully claimed. - if (checkChromeRuntimeError() && (GUI.operating_system !== "MacOS")) { + chrome.usb.claimInterface(self.handle, interfaceNumber, function claimed() { + if (checkChromeRuntimeError()) { console.log('Failed to claim USB device!'); self.cleanup(); - } - - console.log(`Claimed interface: ${interfaceNumber}`); - - if (self.options.exitDfu) { - self.leave(); } else { - self.upload_procedure(0); + console.log(`Claimed interface: ${interfaceNumber}`); + + if (self.options.exitDfu) { + self.leave(); + } else { + self.upload_procedure(0); + } } }); }; @@ -167,8 +164,12 @@ STM32DFU_protocol.prototype.claimInterface = function (interfaceNumber) { STM32DFU_protocol.prototype.releaseInterface = function (interfaceNumber) { const self = this; - chrome.usb.releaseInterface(this.handle, interfaceNumber, function released() { - console.log(`Released interface: ${interfaceNumber}`); + chrome.usb.releaseInterface(self.handle, interfaceNumber, function released() { + if (checkChromeRuntimeError()) { + console.log(`Could not release interface: ${interfaceNumber}`); + } else { + console.log(`Released interface: ${interfaceNumber}`); + } self.closeDevice(); }); @@ -176,9 +177,13 @@ STM32DFU_protocol.prototype.releaseInterface = function (interfaceNumber) { STM32DFU_protocol.prototype.resetDevice = function (callback) { chrome.usb.resetDevice(this.handle, function (result) { - console.log(`Reset Device: ${result}`); + if (checkChromeRuntimeError()) { + console.log(`Could not reset device: ${result}`); + } else { + console.log(`Reset Device: ${result}`); + } - if (callback) callback(); + callback?.(); }); }; @@ -202,7 +207,7 @@ STM32DFU_protocol.prototype.getString = function (index, callback) { var view = new DataView(result.data); var length = view.getUint8(0); var descriptor = ""; - for (var i = 2; i < length; i += 2) { + for (let i = 2; i < length; i += 2) { var charCode = view.getUint16(i, true); descriptor += String.fromCharCode(charCode); } @@ -213,47 +218,46 @@ STM32DFU_protocol.prototype.getString = function (index, callback) { STM32DFU_protocol.prototype.getInterfaceDescriptors = function (interfaceNum, callback) { const self = this; - chrome.usb.getConfiguration( this.handle, function (config) { + chrome.usb.getConfiguration(self.handle, function (config) { if (checkChromeRuntimeError()) { console.log('USB getConfiguration failed!'); callback([], -200); return; } - var interfaceID = 0; - var descriptorStringArray = []; - var getDescriptorString = function () { - if(interfaceID < config.interfaces.length) { - self.getInterfaceDescriptor(interfaceID, function (descriptor, resultCode) { - if (resultCode) { - callback([], resultCode); - return; - } - interfaceID++; - self.getString(descriptor.iInterface, function (descriptorString, resultCode) { + let interfaceID = 0; + const descriptorStringArray = []; + const getDescriptorString = function () { + if (interfaceID < config.interfaces.length) { + self.getInterfaceDescriptor(interfaceID, function (descriptor, resultCode) { if (resultCode) { callback([], resultCode); return; } - if (descriptor.bInterfaceNumber == interfaceNum) { - descriptorStringArray.push(descriptorString); - } - getDescriptorString(); + interfaceID++; + self.getString(descriptor.iInterface, function (descriptorString, resultCode) { + if (resultCode) { + callback([], resultCode); + return; + } + if (descriptor.bInterfaceNumber === interfaceNum) { + descriptorStringArray.push(descriptorString); + } + getDescriptorString(); + }); }); - }); - } else { - //console.log(descriptorStringArray); - callback(descriptorStringArray, 0); - return; - } - }; - getDescriptorString(); + } else { + //console.log(descriptorStringArray); + callback(descriptorStringArray, 0); + return; + } + }; + getDescriptorString(); }); }; STM32DFU_protocol.prototype.getInterfaceDescriptor = function (_interface, callback) { - const self = this; chrome.usb.controlTransfer(this.handle, { 'direction': 'in', 'recipient': 'device', @@ -269,8 +273,8 @@ STM32DFU_protocol.prototype.getInterfaceDescriptor = function (_interface, callb return; } - var buf = new Uint8Array(result.data, 9 + _interface * 9); - var descriptor = { + const buf = new Uint8Array(result.data, 9 + _interface * 9); + const descriptor = { 'bLength': buf[0], 'bDescriptorType': buf[1], 'bInterfaceNumber': buf[2], @@ -287,7 +291,6 @@ STM32DFU_protocol.prototype.getInterfaceDescriptor = function (_interface, callb }; STM32DFU_protocol.prototype.getFunctionalDescriptor = function (_interface, callback) { - const self = this; chrome.usb.controlTransfer(this.handle, { 'direction': 'in', 'recipient': 'interface', @@ -303,9 +306,9 @@ STM32DFU_protocol.prototype.getFunctionalDescriptor = function (_interface, call return; } - var buf = new Uint8Array(result.data); + const buf = new Uint8Array(result.data); - var descriptor = { + const descriptor = { 'bLength': buf[0], 'bDescriptorType': buf[1], 'bmAttributes': buf[2], @@ -340,7 +343,7 @@ STM32DFU_protocol.prototype.getChipInfo = function (_interface, callback) { // H750 SPRacing H7 EXST: "@External Flash /0x90000000/1001*128Kg,3*128Kg,20*128Ka" - Early BL firmware with incorrect string, treat as above. // H750 Partitions: Flash, Config, Firmware, 1x BB Management block + x BB Replacement blocks) - if (str == "@External Flash /0x90000000/1001*128Kg,3*128Kg,20*128Ka") { + if (str === "@External Flash /0x90000000/1001*128Kg,3*128Kg,20*128Ka") { str = "@External Flash /0x90000000/998*128Kg,1*128Kg,4*128Kg,21*128Ka"; } @@ -379,7 +382,7 @@ STM32DFU_protocol.prototype.getChipInfo = function (_interface, callback) { for (var i = 0; i < tmp2.length; i++) { // split into [num_pages, page_size] var tmp3 = tmp2[i].split('*'); - if (tmp3.length != 2) { + if (tmp3.length !== 2) { return null; } var num_pages = parseInt(tmp3[0]); @@ -433,9 +436,9 @@ STM32DFU_protocol.prototype.controlTransfer = function (direction, request, valu timeout = _timeout; } - if (direction == 'in') { + if (direction === 'in') { // data is ignored - chrome.usb.controlTransfer(this.handle, { + chrome.usb.controlTransfer(self.handle, { 'direction': 'in', 'recipient': 'interface', 'requestType': 'class', @@ -463,7 +466,7 @@ STM32DFU_protocol.prototype.controlTransfer = function (direction, request, valu var arrayBuf = new ArrayBuffer(0); } - chrome.usb.controlTransfer(this.handle, { + chrome.usb.controlTransfer(self.handle, { 'direction': 'out', 'recipient': 'interface', 'requestType': 'class', @@ -489,7 +492,7 @@ STM32DFU_protocol.prototype.clearStatus = function (callback) { function check_status() { self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) { - if (data[4] == self.state.dfuIDLE) { + if (data[4] === self.state.dfuIDLE) { callback(data); } else { var delay = data[1] | (data[2] << 8) | (data[3] << 16); @@ -511,16 +514,16 @@ STM32DFU_protocol.prototype.loadAddress = function (address, callback, abort) { self.controlTransfer('out', self.request.DNLOAD, 0, 0, 0, [0x21, address & 0xff, (address >> 8) & 0xff, (address >> 16) & 0xff, (address >> 24) & 0xff], function () { self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) { - if (data[4] == self.state.dfuDNBUSY) { + if (data[4] === self.state.dfuDNBUSY) { var delay = data[1] | (data[2] << 8) | (data[3] << 16); setTimeout(function () { self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) { - if (data[4] == self.state.dfuDNLOAD_IDLE) { + if (data[4] === self.state.dfuDNLOAD_IDLE) { callback(data); } else { console.log('Failed to execute address load'); - if(typeof abort === "undefined" || abort) { + if (typeof abort === "undefined" || abort) { self.cleanup(); } else { callback(data); @@ -541,7 +544,7 @@ STM32DFU_protocol.prototype.loadAddress = function (address, callback, abort) { // result = true/false STM32DFU_protocol.prototype.verify_flash = function (first_array, second_array) { for (var i = 0; i < first_array.length; i++) { - if (first_array[i] != second_array[i]) { + if (first_array[i] !== second_array[i]) { console.log(`Verification failed on byte: ${i} expected: 0x${first_array[i].toString(16)} received: 0x${second_array[i].toString(16)}`); return false; } @@ -599,7 +602,7 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) { switch (step) { case 0: self.getChipInfo(0, function (chipInfo, resultCode) { - if (resultCode != 0 || typeof chipInfo === "undefined") { + if (resultCode !== 0 || typeof chipInfo === "undefined") { console.log(`Failed to detect chip info, resultCode: ${resultCode}`); self.cleanup(); } else { @@ -667,7 +670,7 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) { self.controlTransfer('out', self.request.DNLOAD, 0, 0, 0, [0x92], function () { // 0x92 initiates read unprotect self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) { - if (data[4] == self.state.dfuDNBUSY) { // completely normal + if (data[4] === self.state.dfuDNBUSY) { // completely normal var delay = data[1] | (data[2] << 8) | (data[3] << 16); var total_delay = delay + 20000; // wait at least 20 seconds to make sure the user does not disconnect the board while erasing the memory var timeSpentWaiting = 0; @@ -676,13 +679,13 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) { TABS.firmware_flasher.flashProgress(Math.min(timeSpentWaiting / total_delay, 1) * 100); - if(timeSpentWaiting < total_delay) { + if (timeSpentWaiting < total_delay) { timeSpentWaiting += incr; return; } clearInterval(waitForErase); self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data, error) { // should stall/disconnect - if(error) { // we encounter an error, but this is expected. should be a stall. + if (error) { // we encounter an error, but this is expected. should be a stall. console.log('Unprotect memory command ran successfully. Unplug flight controller. Connect again in DFU mode and try flashing again.'); GUI.log(i18n.getMessage('stm32UnprotectSuccessful')); @@ -716,13 +719,13 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) { var tryReadOB = function() { // the following should fail if read protection is active self.controlTransfer('in', self.request.UPLOAD, 2, 0, self.chipInfo.option_bytes.total_size, 0, function (ob_data, errcode) { - if(errcode) { + if (errcode) { console.log(`USB transfer error while reading option bytes: ${errcode1}`); self.cleanup(); return; } self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) { - if (data[4] == self.state.dfuUPLOAD_IDLE && ob_data.length == self.chipInfo.option_bytes.total_size) { + if (data[4] === self.state.dfuUPLOAD_IDLE && ob_data.length === self.chipInfo.option_bytes.total_size) { console.log('Option bytes read successfully'); console.log('Chip does not appear read protected'); GUI.log(i18n.getMessage('stm32NotReadProtected')); @@ -772,12 +775,12 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) { var initReadOB = function (loadAddressResponse) { // contrary to what is in the docs. Address load should in theory work even if read protection is active // if address load fails with this specific error though, it is very likely bc of read protection - if(loadAddressResponse[4] == self.state.dfuERROR && loadAddressResponse[0] == self.status.errVENDOR) { + if (loadAddressResponse[4] === self.state.dfuERROR && loadAddressResponse[0] === self.status.errVENDOR) { // read protected GUI.log(i18n.getMessage('stm32AddressLoadFailed')); self.clearStatus(unprotect); return; - } else if(loadAddressResponse[4] == self.state.dfuDNLOAD_IDLE) { + } else if (loadAddressResponse[4] === self.state.dfuDNLOAD_IDLE) { console.log('Address load for option bytes sector succeeded.'); self.clearStatus(tryReadOB); } else { @@ -811,9 +814,9 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) { var spans_page = self.hex.data[k].address < page_start && end_address > page_end; if (starts_in_page || ends_in_page || spans_page) { var idx = erase_pages.findIndex(function (element, index, array) { - return element.sector == i && element.page == j; + return element.sector === i && element.page === j; }); - if (idx == -1) + if (idx === -1) erase_pages.push({'sector': i, 'page': j}); } } @@ -839,7 +842,7 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) { TABS.firmware_flasher.flashProgress((page + 1) / erase_pages.length * 100); page++; - if(page == erase_pages.length) { + if (page === erase_pages.length) { console.log("Erase: complete"); GUI.log(i18n.getMessage('dfu_erased_kilobytes', (total_erased / 1024).toString())); self.upload_procedure(4); @@ -858,13 +861,13 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) { self.controlTransfer('out', self.request.DNLOAD, 0, 0, 0, cmd, function () { self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) { - if (data[4] == self.state.dfuDNBUSY) { // completely normal + if (data[4] === self.state.dfuDNBUSY) { // completely normal var delay = data[1] | (data[2] << 8) | (data[3] << 16); setTimeout(function () { self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) { - if (data[4] == self.state.dfuDNBUSY) { + if (data[4] === self.state.dfuDNBUSY) { // // H743 Rev.V (probably other H7 Rev.Vs also) remains in dfuDNBUSY state after the specified delay time. @@ -879,7 +882,7 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) { self.clearStatus(function() { self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) { - if (data[4] == self.state.dfuIDLE) { + if (data[4] === self.state.dfuIDLE) { erase_page_next(); } else { console.log(`Failed to erase page 0x${page_addr.toString(16)} (did not reach dfuIDLE after clearing`); @@ -887,7 +890,7 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) { } }); }); - } else if (data[4] == self.state.dfuDNLOAD_IDLE) { + } else if (data[4] === self.state.dfuDNLOAD_IDLE) { erase_page_next(); } else { console.log(`Failed to erase page 0x${page_addr.toString(16)}`); @@ -933,12 +936,12 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) { self.controlTransfer('out', self.request.DNLOAD, wBlockNum++, 0, 0, data_to_flash, function () { self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) { - if (data[4] == self.state.dfuDNBUSY) { + if (data[4] === self.state.dfuDNBUSY) { var delay = data[1] | (data[2] << 8) | (data[3] << 16); setTimeout(function () { self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) { - if (data[4] == self.state.dfuDNLOAD_IDLE) { + if (data[4] === self.state.dfuDNLOAD_IDLE) { // update progress bar TABS.firmware_flasher.flashProgress(bytes_flashed_total / (self.hex.bytes_total * 2) * 100); @@ -1102,7 +1105,7 @@ STM32DFU_protocol.prototype.cleanup = function () { GUI.connect_lock = false; - var timeSpent = new Date().getTime() - self.upload_time_start; + const timeSpent = new Date().getTime() - self.upload_time_start; console.log(`Script finished after: ${timeSpent / 1000} seconds`); @@ -1112,4 +1115,4 @@ STM32DFU_protocol.prototype.cleanup = function () { }; // initialize object -var STM32DFU = new STM32DFU_protocol(); +const STM32DFU = new STM32DFU_protocol();