Skip to content

Improve msp send #4510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2f6140b
Improve msp send
haslinghuis Jun 10, 2025
7b5f0de
Fixes per review coderabbit
haslinghuis Jun 10, 2025
d13638b
Refactor
haslinghuis Jun 10, 2025
bc14bc0
Mitigate failed retry attempts
haslinghuis Jun 10, 2025
5b1a700
More coderabbit improvements
haslinghuis Jun 10, 2025
8500754
Improve timeout optimization
haslinghuis Jun 10, 2025
bea8828
Add dynamic retries
haslinghuis Jun 10, 2025
3e025b5
Improve duplicate message detection by also checking payload
haslinghuis Jun 10, 2025
11141dc
Remove dynamic timeout overhead
haslinghuis Jun 10, 2025
424025d
Fix sonar
haslinghuis Jun 10, 2025
4d3f7b1
Add queue elements for debugging
haslinghuis Jun 11, 2025
1a30f95
Centralize timer cleanup
haslinghuis Jun 11, 2025
07ca99a
Add MSP debugging tools
haslinghuis Jun 12, 2025
12d3ddf
Suggestions per Coderabbit
haslinghuis Jun 12, 2025
21895a6
Sonar
haslinghuis Jun 12, 2025
97f35c9
Add lazy init
haslinghuis Jun 12, 2025
6a06ca1
More coderabbit
haslinghuis Jun 12, 2025
b218963
More coderabbit ...
haslinghuis Jun 12, 2025
03cf9e1
More coderabbit ......
haslinghuis Jun 12, 2025
d95bfdd
Make available
haslinghuis Jun 12, 2025
1ffc0e0
Increase queue limit
haslinghuis Jun 12, 2025
5444083
coderabbit again
haslinghuis Jun 12, 2025
f459caf
Review per coderabbit
haslinghuis Jun 15, 2025
88115e4
More
haslinghuis Jun 15, 2025
c31fa5d
More ...
haslinghuis Jun 15, 2025
faca02d
Prevent XSS attack
haslinghuis Jun 15, 2025
24cbe75
Fix performance violations
haslinghuis Jun 15, 2025
b793fe4
Prevent runtime errors
haslinghuis Jun 15, 2025
cdbdda0
More from coderabbit
haslinghuis Jun 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ import { updateTabList } from "./utils/updateTabList.js";
import * as THREE from "three";
import NotificationManager from "./utils/notifications.js";

import("./msp/msp_debug_tools.js")
.then(() => {
console.log("🔧 MSP Debug Tools loaded for development environment");
console.log("• Press Ctrl+Shift+M to toggle debug dashboard");
console.log("• Use MSPTestRunner.help() for all commands");
})
.catch((err) => {
console.warn("Failed to load MSP debug tools:", err);
});

if (typeof String.prototype.replaceAll === "undefined") {
String.prototype.replaceAll = function (match, replace) {
return this.replace(new RegExp(match, "g"), () => replace);
Expand Down Expand Up @@ -119,7 +129,8 @@ function startProcess() {
console.log(`Libraries: jQuery - ${$.fn.jquery}, three.js - ${THREE.REVISION}`);

// Check if this is the first visit
if (getConfig("firstRun").firstRun === undefined) {
const firstRunCfg = getConfig("firstRun") ?? {};
if (firstRunCfg.firstRun === undefined) {
setConfig({ firstRun: true });
import("./tabs/static_tab.js").then(({ staticTab }) => {
staticTab.initialize("options", () => {
Expand Down
42 changes: 15 additions & 27 deletions src/js/msp.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,13 @@ const MSP = {
message_buffer: null,
message_buffer_uint8_view: null,
message_checksum: 0,
messageIsJumboFrame: false,
crcError: false,

callbacks: [],
packet_error: 0,
unsupported: 0,

MIN_TIMEOUT: 200,
MAX_TIMEOUT: 2000,
timeout: 200,
TIMEOUT: 1000,

last_received_timestamp: null,
listeners: [],
Expand Down Expand Up @@ -374,28 +371,19 @@ const MSP = {
serial.send(bufferOut);
},
send_message(code, data, callback_sent, callback_msp, doCallbackOnError) {
const connected = serial.connected;

if (code === undefined || !connected || CONFIGURATOR.virtualMode) {
if (code === undefined || !serial.connected || CONFIGURATOR.virtualMode) {
if (callback_msp) {
callback_msp();
}
return false;
}

let requestExists = false;
for (const instance of this.callbacks) {
if (instance.code === code) {
requestExists = true;

break;
}
}
const requestExists = this.callbacks.some((instance) => instance.code === code);

const bufferOut = code <= 254 ? this.encode_message_v1(code, data) : this.encode_message_v2(code, data);

const obj = {
code: code,
code,
requestBuffer: bufferOut,
callback: callback_msp,
callbackOnError: doCallbackOnError,
Expand All @@ -412,31 +400,31 @@ const MSP = {
serial.send(bufferOut, (_sendInfo) => {
obj.stop = performance.now();
const executionTime = Math.round(obj.stop - obj.start);
this.timeout = Math.max(this.MIN_TIMEOUT, Math.min(executionTime, this.MAX_TIMEOUT));
// We should probably give up connection if the request takes too long ?
if (executionTime > 5000) {
console.warn(
`MSP: data request took too long: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} EXECUTION TIME: ${executionTime}ms`,
);
}

clearTimeout(obj.timer); // prevent leaks
});
}, this.timeout);
}, this.TIMEOUT);
}

this.callbacks.push(obj);

// always send messages with data payload (even when there is a message already in the queue)
if (data || !requestExists) {
if (this.timeout > this.MIN_TIMEOUT) {
this.timeout--;
}

serial.send(bufferOut, (sendInfo) => {
if (sendInfo.bytesSent === bufferOut.byteLength) {
if (callback_sent) {
callback_sent();
}
if (sendInfo.bytesSent === bufferOut.byteLength && callback_sent) {
callback_sent();
}
});
}

return true;
},

/**
* resolves: {command: code, data: data, length: message_length}
*/
Expand Down
Loading