Skip to content

Commit 674994c

Browse files
committed
Improve msp
1 parent c5e676f commit 674994c

File tree

2 files changed

+32
-43
lines changed

2 files changed

+32
-43
lines changed

src/js/msp.js

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { serial } from "./serial.js";
44

55
const MSP = {
66
symbols: {
7-
BEGIN: "$".charCodeAt(0),
8-
PROTO_V1: "M".charCodeAt(0),
9-
PROTO_V2: "X".charCodeAt(0),
10-
FROM_MWC: ">".charCodeAt(0),
11-
TO_MWC: "<".charCodeAt(0),
12-
UNSUPPORTED: "!".charCodeAt(0),
7+
BEGIN: "$".codePointAt(0),
8+
PROTO_V1: "M".codePointAt(0),
9+
PROTO_V2: "X".codePointAt(0),
10+
FROM_MWC: ">".codePointAt(0),
11+
TO_MWC: "<".codePointAt(0),
12+
UNSUPPORTED: "!".codePointAt(0),
1313
START_OF_TEXT: 0x02,
1414
END_OF_TEXT: 0x03,
1515
END_OF_TRANSMISSION: 0x04,
@@ -58,9 +58,7 @@ const MSP = {
5858
packet_error: 0,
5959
unsupported: 0,
6060

61-
MIN_TIMEOUT: 200,
62-
MAX_TIMEOUT: 2000,
63-
timeout: 200,
61+
TIMEOUT: 1000,
6462

6563
last_received_timestamp: null,
6664
listeners: [],
@@ -289,7 +287,7 @@ const MSP = {
289287
});
290288
},
291289
listen(listener) {
292-
if (this.listeners.indexOf(listener) == -1) {
290+
if (this.listeners.indexOf(listener) === -1) {
293291
this.listeners.push(listener);
294292
}
295293
},
@@ -318,8 +316,8 @@ const MSP = {
318316
const dataLength = data ? data.length : 0;
319317
// always reserve 6 bytes for protocol overhead !
320318
const bufferSize = dataLength + 6;
321-
let bufferOut = new ArrayBuffer(bufferSize);
322-
let bufView = new Uint8Array(bufferOut);
319+
const bufferOut = new ArrayBuffer(bufferSize);
320+
const bufView = new Uint8Array(bufferOut);
323321

324322
bufView[0] = 36; // $
325323
bufView[1] = 77; // M
@@ -358,7 +356,7 @@ const MSP = {
358356
return bufferOut;
359357
},
360358
encode_message_cli(str) {
361-
const data = Array.from(str, (c) => c.charCodeAt(0));
359+
const data = Array.from(str, (c) => c.codePointAt(0));
362360
const dataLength = data ? data.length : 0;
363361
const bufferSize = dataLength + 3; // 3 bytes for protocol overhead
364362
const bufferOut = new ArrayBuffer(bufferSize);
@@ -380,23 +378,22 @@ const MSP = {
380378
send_message(code, data, callback_sent, callback_msp, doCallbackOnError) {
381379
const connected = serial.connected;
382380

383-
if (code === undefined || !connected || CONFIGURATOR.virtualMode) {
381+
if (code === undefined || !serial.connected || CONFIGURATOR.virtualMode) {
384382
if (callback_msp) {
385383
callback_msp();
386384
}
387385
return false;
388386
}
389387

390-
let requestExists = false;
391-
for (const instance of this.callbacks) {
392-
if (instance.code === code) {
393-
requestExists = true;
394-
395-
break;
396-
}
397-
}
398-
399388
const bufferOut = code <= 254 ? this.encode_message_v1(code, data) : this.encode_message_v2(code, data);
389+
const view = new Uint8Array(bufferOut);
390+
const keyCrc = this.crc8_dvb_s2_data(view, 0, view.length);
391+
const requestExists = this.callbacks.some(
392+
(i) =>
393+
i.code === code &&
394+
i.requestBuffer?.byteLength === bufferOut.byteLength &&
395+
this.crc8_dvb_s2_data(new Uint8Array(i.requestBuffer), 0, i.requestBuffer.byteLength) === keyCrc,
396+
);
400397

401398
const obj = {
402399
code: code,
@@ -409,31 +406,23 @@ const MSP = {
409406
if (!requestExists) {
410407
obj.timer = setTimeout(() => {
411408
console.warn(
412-
`MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} TIMEOUT: ${
413-
this.timeout
414-
} QUEUE: ${this.callbacks.length} (${this.callbacks.map((e) => e.code)})`,
409+
`MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} QUEUE: ${this.callbacks.length} (${this.callbacks.map((e) => e.code)})`,
415410
);
416411
serial.send(bufferOut, (_sendInfo) => {
417412
obj.stop = performance.now();
418413
const executionTime = Math.round(obj.stop - obj.start);
419-
this.timeout = Math.max(this.MIN_TIMEOUT, Math.min(executionTime, this.MAX_TIMEOUT));
414+
clearTimeout(obj.timer); // prevent leaks
420415
});
421-
}, this.timeout);
416+
}, this.TIMEOUT);
422417
}
423418

424419
this.callbacks.push(obj);
425420

426421
// always send messages with data payload (even when there is a message already in the queue)
427422
if (data || !requestExists) {
428-
if (this.timeout > this.MIN_TIMEOUT) {
429-
this.timeout--;
430-
}
431-
432423
serial.send(bufferOut, (sendInfo) => {
433-
if (sendInfo.bytesSent === bufferOut.byteLength) {
434-
if (callback_sent) {
435-
callback_sent();
436-
}
424+
if (sendInfo.bytesSent === bufferOut.byteLength && callback_sent) {
425+
callback_sent();
437426
}
438427
});
439428
}

src/js/msp/MSPHelper.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function MspHelper() {
9595
buffer.push8(size);
9696

9797
for (let i = 0; i < size; i++) {
98-
buffer.push8(config.charCodeAt(i));
98+
buffer.push8(config.codePointAt(i));
9999
}
100100
};
101101

@@ -1765,7 +1765,7 @@ MspHelper.prototype.process_data = function (dataHandler) {
17651765
const callbackOnError = dataHandler.callbacks[i].callbackOnError;
17661766

17671767
// remove timeout
1768-
clearInterval(dataHandler.callbacks[i].timer);
1768+
clearTimeout(dataHandler.callbacks[i].timer);
17691769

17701770
// remove object from array
17711771
dataHandler.callbacks.splice(i, 1);
@@ -2251,7 +2251,7 @@ MspHelper.prototype.crunch = function (code, modifierCode = undefined) {
22512251
case MSPCodes.MSP_SET_NAME:
22522252
const MSP_BUFFER_SIZE = 64;
22532253
for (let i = 0; i < FC.CONFIG.name.length && i < MSP_BUFFER_SIZE; i++) {
2254-
buffer.push8(FC.CONFIG.name.charCodeAt(i));
2254+
buffer.push8(FC.CONFIG.name.codePointAt(i));
22552255
}
22562256
break;
22572257

@@ -2358,7 +2358,7 @@ MspHelper.prototype.crunch = function (code, modifierCode = undefined) {
23582358
.push8(FC.VTXTABLE_POWERLEVEL.vtxtable_powerlevel_label.length);
23592359

23602360
for (let i = 0; i < FC.VTXTABLE_POWERLEVEL.vtxtable_powerlevel_label.length; i++) {
2361-
buffer.push8(FC.VTXTABLE_POWERLEVEL.vtxtable_powerlevel_label.charCodeAt(i));
2361+
buffer.push8(FC.VTXTABLE_POWERLEVEL.vtxtable_powerlevel_label.codePointAt(i));
23622362
}
23632363

23642364
break;
@@ -2368,13 +2368,13 @@ MspHelper.prototype.crunch = function (code, modifierCode = undefined) {
23682368

23692369
buffer.push8(FC.VTXTABLE_BAND.vtxtable_band_name.length);
23702370
for (let i = 0; i < FC.VTXTABLE_BAND.vtxtable_band_name.length; i++) {
2371-
buffer.push8(FC.VTXTABLE_BAND.vtxtable_band_name.charCodeAt(i));
2371+
buffer.push8(FC.VTXTABLE_BAND.vtxtable_band_name.codePointAt(i));
23722372
}
23732373

23742374
if (FC.VTXTABLE_BAND.vtxtable_band_letter != "") {
2375-
buffer.push8(FC.VTXTABLE_BAND.vtxtable_band_letter.charCodeAt(0));
2375+
buffer.push8(FC.VTXTABLE_BAND.vtxtable_band_letter.codePointAt(0));
23762376
} else {
2377-
buffer.push8(" ".charCodeAt(0));
2377+
buffer.push8(" ".codePointAt(0));
23782378
}
23792379
buffer.push8(FC.VTXTABLE_BAND.vtxtable_band_is_factory_band ? 1 : 0);
23802380

0 commit comments

Comments
 (0)