Skip to content

Commit

Permalink
For MSP V1 we skip requests that are already in the queue
Browse files Browse the repository at this point in the history
  • Loading branch information
haslinghuis committed Apr 12, 2023
1 parent c8e6449 commit 25e7ccc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
80 changes: 43 additions & 37 deletions src/js/msp.js
@@ -1,7 +1,6 @@
import GUI from "./gui.js";
import CONFIGURATOR from "./data_storage.js";
import serial from "./serial.js";
import MSPCodes from "./msp/MSPCodes.js";

const MSP = {
symbols: {
Expand Down Expand Up @@ -62,10 +61,6 @@ const MSP = {

JUMBO_FRAME_SIZE_LIMIT: 255,

// function to get name of code
getMSPCodeName: function(code) {
return Object.keys(MSPCodes).find(key => MSPCodes[key] === code);
},
read: function (readInfo) {
if (CONFIGURATOR.virtualMode) {
return;
Expand Down Expand Up @@ -332,24 +327,31 @@ const MSP = {
return false;
}

let requestExists = false;

for (const instance of MSP.callbacks) {
if (instance.code === code) {
// request already exists in queue, remove existing instance from queue
// and add new instance to the end of the queue
// this is done to prevent the queue from getting clogged with the same request
// and to ensure that the callback is called in the order that the requests were made
// we allow existing request to timeout and be removed from the queue
setTimeout(function () {
const index = MSP.callbacks.indexOf(instance);
if (index > -1) {
if (instance.timer) {
clearInterval(instance.timer);
// For MSP V1 we skip requests that are already in the queue
// For MSP V2 we allow multiple requests of the same type to be in the queue
// This is because MSP V2 allows for multiple requests of the same type to be sent
// in a single frame, and we don't want to skip any of them.
// This is a workaround for the fact that we don't have a way to identify
// 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) {
setTimeout(function () {
const index = MSP.callbacks.indexOf(instance);
if (index > -1) {
if (instance.timer) {
clearInterval(instance.timer);
}
MSP.callbacks.splice(index, 1);
}
MSP.callbacks.splice(index, 1);
}
}, MSP.timeout);

console.log(`MSP.send_message: code: ${code} ${this.getMSPCodeName(code)} QUEUE: ${MSP.callbacks.length} updated in queue`);
}, MSP.timeout);
} else {
requestExists = true;
}
}
}

Expand All @@ -363,29 +365,33 @@ const MSP = {
'start': performance.now(),
};

obj.timer = setInterval(function () {
console.warn(`MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} TIMEOUT: ${MSP.timeout} QUEUE: ${MSP.callbacks.length}`);
serial.send(bufferOut, function (_sendInfo) {
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));
});
if (!requestExists) {
obj.timer = setInterval(function () {
console.warn(`MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} TIMEOUT: ${MSP.timeout} QUEUE: ${MSP.callbacks.length}`);
serial.send(bufferOut, function (_sendInfo) {
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);
}, MSP.timeout);
}

MSP.callbacks.push(obj);

serial.send(bufferOut, function (sendInfo) {
if (sendInfo.bytesSent === bufferOut.length) {
if (callback_sent) {
callback_sent();
}
// always send messages with data payload (even when there is a message already in the queue)
if (data || !requestExists) {
if (MSP.timeout > MSP.MIN_TIMEOUT) {
MSP.timeout--;
}
});

// Decrement timeout if it is above the minimum
if (MSP.timeout > MSP.MIN_TIMEOUT) {
MSP.timeout--;
serial.send(bufferOut, function (sendInfo) {
if (sendInfo.bytesSent === bufferOut.byteLength) {
if (callback_sent) {
callback_sent();
}
}
});
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion src/tabs/presets/presets.js
Expand Up @@ -404,7 +404,7 @@ presets.preselectFilterFields = function() {
const currentVersion = FC.CONFIG.flightControllerVersion;
const selectedVersions = [];

for (const bfVersion of this.presetsRepo.index.uniqueValues.firmware_version) {
for(const bfVersion of this.presetsRepo.index.uniqueValues.firmware_version) {
if (currentVersion.startsWith(bfVersion)) {
selectedVersions.push(bfVersion);
}
Expand Down

0 comments on commit 25e7ccc

Please sign in to comment.