@@ -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 ) ;
@@ -378,25 +376,22 @@ const MSP = {
378376 serial . send ( bufferOut ) ;
379377 } ,
380378 send_message ( code , data , callback_sent , callback_msp , doCallbackOnError ) {
381- const connected = serial . connected ;
382-
383- if ( code === undefined || ! connected || CONFIGURATOR . virtualMode ) {
379+ if ( code === undefined || ! serial . connected || CONFIGURATOR . virtualMode ) {
384380 if ( callback_msp ) {
385381 callback_msp ( ) ;
386382 }
387383 return false ;
388384 }
389385
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-
399386 const bufferOut = code <= 254 ? this . encode_message_v1 ( code , data ) : this . encode_message_v2 ( code , data ) ;
387+ const view = new Uint8Array ( bufferOut ) ;
388+ const keyCrc = this . crc8_dvb_s2_data ( view , 0 , view . length ) ;
389+ const requestExists = this . callbacks . some (
390+ ( i ) =>
391+ i . code === code &&
392+ i . requestBuffer ?. byteLength === bufferOut . byteLength &&
393+ this . crc8_dvb_s2_data ( new Uint8Array ( i . requestBuffer ) , 0 , i . requestBuffer . byteLength ) === keyCrc ,
394+ ) ;
400395
401396 const obj = {
402397 code : code ,
@@ -409,31 +404,23 @@ const MSP = {
409404 if ( ! requestExists ) {
410405 obj . timer = setTimeout ( ( ) => {
411406 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 ) } )`,
407+ `MSP: data request timed-out: ${ code } ID: ${ serial . connectionId } TAB: ${ GUI . active_tab } QUEUE: ${ this . callbacks . length } (${ this . callbacks . map ( ( e ) => e . code ) } )` ,
415408 ) ;
416- serial . send ( bufferOut , ( _sendInfo ) => {
417- obj . stop = performance . now ( ) ;
418- const executionTime = Math . round ( obj . stop - obj . start ) ;
419- this . timeout = Math . max ( this . MIN_TIMEOUT , Math . min ( executionTime , this . MAX_TIMEOUT ) ) ;
409+ serial . send ( bufferOut , ( sendInfo ) => {
410+ if ( sendInfo . bytesSent === bufferOut . byteLength && callback_sent ) {
411+ callback_sent ( ) ;
412+ }
420413 } ) ;
421- } , this . timeout ) ;
414+ } , this . TIMEOUT ) ;
422415 }
423416
424417 this . callbacks . push ( obj ) ;
425418
426419 // always send messages with data payload (even when there is a message already in the queue)
427420 if ( data || ! requestExists ) {
428- if ( this . timeout > this . MIN_TIMEOUT ) {
429- this . timeout -- ;
430- }
431-
432421 serial . send ( bufferOut , ( sendInfo ) => {
433- if ( sendInfo . bytesSent === bufferOut . byteLength ) {
434- if ( callback_sent ) {
435- callback_sent ( ) ;
436- }
422+ if ( sendInfo . bytesSent === bufferOut . byteLength && callback_sent ) {
423+ callback_sent ( ) ;
437424 }
438425 } ) ;
439426 }
0 commit comments