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 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
508349e
Improve msp send
haslinghuis Jun 10, 2025
87ea524
Fixes per review coderabbit
haslinghuis Jun 10, 2025
b7f61eb
Refactor
haslinghuis Jun 10, 2025
dcafa04
Mitigate failed retry attempts
haslinghuis Jun 10, 2025
54ea03c
More coderabbit improvements
haslinghuis Jun 10, 2025
a71ae6d
Improve timeout optimization
haslinghuis Jun 10, 2025
bd2d333
Add dynamic retries
haslinghuis Jun 10, 2025
f002cf0
Improve duplicate message detection by also checking payload
haslinghuis Jun 10, 2025
018bfef
Remove dynamic timeout overhead
haslinghuis Jun 10, 2025
d481287
Fix sonar
haslinghuis Jun 10, 2025
dd20b80
Add queue elements for debugging
haslinghuis Jun 11, 2025
5789e00
Centralize timer cleanup
haslinghuis Jun 11, 2025
214cccb
Add MSP debugging tools
haslinghuis Jun 12, 2025
0e38adf
Suggestions per Coderabbit
haslinghuis Jun 12, 2025
af03b1e
Sonar
haslinghuis Jun 12, 2025
6ba3b0a
Add lazy init
haslinghuis Jun 12, 2025
6aee8a3
More coderabbit
haslinghuis Jun 12, 2025
3640c4b
More coderabbit ...
haslinghuis Jun 12, 2025
a74f07f
More coderabbit ......
haslinghuis Jun 12, 2025
014e7e0
Make available
haslinghuis Jun 12, 2025
89cdf85
Increase queue limit
haslinghuis Jun 12, 2025
29430c8
coderabbit again
haslinghuis Jun 12, 2025
9029769
Review per coderabbit
haslinghuis Jun 15, 2025
b5cc51e
More
haslinghuis Jun 15, 2025
76f410d
More ...
haslinghuis Jun 15, 2025
4f402b2
Prevent XSS attack
haslinghuis Jun 15, 2025
88c1bec
Fix performance violations
haslinghuis Jun 15, 2025
bd8bb8d
Prevent runtime errors
haslinghuis Jun 15, 2025
086ac12
More from coderabbit
haslinghuis Jun 23, 2025
bef646a
Use subpath for debug tools
haslinghuis Aug 3, 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 @@ -18,6 +18,16 @@ import { updateTabList } from "./utils/updateTabList.js";
import * as THREE from "three";
import NotificationManager from "./utils/notifications.js";

import("./msp/debug/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 @@ -118,7 +128,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 @@ -378,28 +375,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 @@ -416,31 +404,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