@@ -4,12 +4,12 @@ import { serial } from "./serial.js";
44
55const 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 }
0 commit comments