diff --git a/src/js/msp.js b/src/js/msp.js index 976b5d00a2e..ef6ef0ba748 100644 --- a/src/js/msp.js +++ b/src/js/msp.js @@ -53,7 +53,7 @@ const MSP = { packet_error: 0, unsupported: 0, - MIN_TIMEOUT: 200, + MIN_TIMEOUT: 50, MAX_TIMEOUT: 2000, timeout: 200, @@ -305,6 +305,10 @@ const MSP = { bufView[bufferSize - 1] = this.crc8_dvb_s2_data(bufView, 3, bufferSize - 1); return bufferOut; }, + // function to get name of code + getMSPCodeName: function(code) { + return Object.keys(MSPCodes).find(key => MSPCodes[key] === code); + }, send_message: function (code, data, callback_sent, callback_msp, doCallbackOnError) { if (code === undefined || !serial.connectionId || CONFIGURATOR.virtualMode) { if (callback_msp) { @@ -315,6 +319,7 @@ const MSP = { let requestExists = false; + // Check if request already exists in the queue for (const instance of MSP.callbacks) { if (instance.code === code) { // For MSP V1 we replace requests of the same type in the queue @@ -325,19 +330,19 @@ const MSP = { // which request a response belongs to. // TODO: Implement a way to identify which request a response belongs to // so that we can skip duplicate requests in the queue. - if (code < 255 && code !== MSPCodes.MSP_MULTIPLE_MSP) { - setTimeout(function () { - const index = MSP.callbacks.indexOf(instance); - if (index > -1) { - if (instance.timer) { - clearInterval(instance.timer); - } - MSP.callbacks.splice(index, 1); - } - }, 10); - } else { + // console.log(`Remove duplicate request ${code} (${this.getMSPCodeName(code)}) from queue and add updated request`); + // if (code < 255 && code !== MSPCodes.MSP_MULTIPLE_MSP) { + // const index = MSP.callbacks.indexOf(instance); + // if (index > -1) { + // if (instance.timer) { + // clearInterval(instance.timer); + // } + // MSP.callbacks.splice(index, 1); + // } + // } else { requestExists = true; - } + break; + // } } } @@ -352,14 +357,15 @@ const MSP = { }; if (!requestExists) { - obj.timer = setInterval(function () { + obj.timer = setTimeout(function () { console.warn(`MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} TIMEOUT: ${MSP.timeout} QUEUE: ${MSP.callbacks.length} (${MSP.callbacks.map(function (e) { return e.code; })})`); serial.send(bufferOut, function (_sendInfo) { + // TODO: handle error + // Determine new timeout obj.stop = performance.now(); const executionTime = Math.round(obj.stop - obj.start); MSP.timeout = Math.max(MSP.MIN_TIMEOUT, Math.min(executionTime, MSP.MAX_TIMEOUT)); }); - }, MSP.timeout); } diff --git a/src/js/msp/MSPHelper.js b/src/js/msp/MSPHelper.js index 24e87ce5b5c..fbd4610b763 100644 --- a/src/js/msp/MSPHelper.js +++ b/src/js/msp/MSPHelper.js @@ -2733,7 +2733,7 @@ MspHelper.prototype.writeConfiguration = async function(callback) { setTimeout(async function() { MSP.send_message(MSPCodes.MSP_EEPROM_WRITE, false, false, function() { gui_log(i18n.getMessage('configurationEepromSaved')); - + console.log('Configuration saved to EEPROM'); if (callback) { callback(); } diff --git a/src/js/protocols/stm32.js b/src/js/protocols/stm32.js index d43ea406553..2f252ce4724 100644 --- a/src/js/protocols/stm32.js +++ b/src/js/protocols/stm32.js @@ -205,13 +205,13 @@ STM32_protocol.prototype.connect = function (port, baud, hex, options, callback) function reboot() { const buffer = []; buffer.push8(rebootMode); - MSP.send_message(MSPCodes.MSP_SET_REBOOT, buffer, () => { - - // if firmware doesn't flush MSP/serial send buffers and gracefully shutdown VCP connections we won't get a reply, so don't wait for it. - - self.msp_connector.disconnect(disconnectionResult => onDisconnect(disconnectionResult)); - - }, () => console.log('Reboot request received by device')); + setTimeout(() => { + MSP.send_message(MSPCodes.MSP_SET_REBOOT, buffer, () => { + // if firmware doesn't flush MSP/serial send buffers and gracefully shutdown VCP connections we won't get a reply, so don't wait for it. + self.msp_connector.disconnect(disconnectionResult => onDisconnect(disconnectionResult)); + console.log('Reboot request received by device'); + }); + }, 100); } function onAbort() { diff --git a/src/js/tabs/ports.js b/src/js/tabs/ports.js index f8a0a4b33c9..f5f8b8de573 100644 --- a/src/js/tabs/ports.js +++ b/src/js/tabs/ports.js @@ -356,7 +356,7 @@ ports.initialize = function (callback) { GUI.content_ready(callback); } - function on_save_handler() { + function on_save_handler() { tracking.sendSaveAndChangeEvents(tracking.EVENT_CATEGORIES.FLIGHT_CONTROLLER, self.analyticsChanges, 'ports'); self.analyticsChanges = {}; @@ -486,11 +486,11 @@ ports.initialize = function (callback) { MSP.send_message(MSPCodes.MSP_SET_FEATURE_CONFIG, mspHelper.crunch(MSPCodes.MSP_SET_FEATURE_CONFIG), false, save_to_eeprom); } - async function save_to_eeprom() { - await mspHelper.writeConfiguration(); - - GUI.tab_switch_cleanup(function() { - MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, reinitializeConnection); + function save_to_eeprom() { + mspHelper.writeConfiguration(function() { + GUI.tab_switch_cleanup(function() { + MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, reinitializeConnection); + }); }); } }