@@ -51,23 +51,18 @@ const MSP = {
5151 message_buffer : null ,
5252 message_buffer_uint8_view : null ,
5353 message_checksum : 0 ,
54- messageIsJumboFrame : false ,
5554 crcError : false ,
5655
5756 callbacks : [ ] ,
5857 packet_error : 0 ,
5958 unsupported : 0 ,
6059
61- MIN_TIMEOUT : 200 ,
62- MAX_TIMEOUT : 2000 ,
63- timeout : 200 ,
60+ TIMEOUT : 1000 ,
6461
6562 last_received_timestamp : null ,
6663 listeners : [ ] ,
6764
68- JUMBO_FRAME_SIZE_LIMIT : 255 ,
69-
70- cli_buffer : [ ] , // buffer for CLI charactor output
65+ cli_buffer : [ ] , // buffer for CLI character output
7166 cli_output : [ ] ,
7267 cli_callback : null ,
7368
@@ -280,7 +275,6 @@ const MSP = {
280275 // Reset variables
281276 this . message_length_received = 0 ;
282277 this . state = 0 ;
283- this . messageIsJumboFrame = false ;
284278 this . crcError = false ;
285279 } ,
286280 notify ( ) {
@@ -289,7 +283,7 @@ const MSP = {
289283 } ) ;
290284 } ,
291285 listen ( listener ) {
292- if ( this . listeners . indexOf ( listener ) == - 1 ) {
286+ if ( this . listeners . indexOf ( listener ) === - 1 ) {
293287 this . listeners . push ( listener ) ;
294288 }
295289 } ,
@@ -318,8 +312,8 @@ const MSP = {
318312 const dataLength = data ? data . length : 0 ;
319313 // always reserve 6 bytes for protocol overhead !
320314 const bufferSize = dataLength + 6 ;
321- let bufferOut = new ArrayBuffer ( bufferSize ) ;
322- let bufView = new Uint8Array ( bufferOut ) ;
315+ const bufferOut = new ArrayBuffer ( bufferSize ) ;
316+ const bufView = new Uint8Array ( bufferOut ) ;
323317
324318 bufView [ 0 ] = 36 ; // $
325319 bufView [ 1 ] = 77 ; // M
@@ -377,70 +371,64 @@ const MSP = {
377371
378372 serial . send ( bufferOut ) ;
379373 } ,
380- send_message ( code , data , callback_sent , callback_msp , doCallbackOnError ) {
381- const connected = serial . connected ;
382-
383- if ( code === undefined || ! connected || CONFIGURATOR . virtualMode ) {
374+ send_message ( code , data , callback_sent , callback_msp ) {
375+ if ( code === undefined || ! serial . connected || CONFIGURATOR . virtualMode ) {
384376 if ( callback_msp ) {
385377 callback_msp ( ) ;
386378 }
387379 return false ;
388380 }
389381
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-
399382 const bufferOut = code <= 254 ? this . encode_message_v1 ( code , data ) : this . encode_message_v2 ( code , data ) ;
383+ const view = new Uint8Array ( bufferOut ) ;
384+ const keyCrc = this . crc8_dvb_s2_data ( view , 0 , view . length ) ;
385+ const requestExists = this . callbacks . some (
386+ ( i ) =>
387+ i . code === code &&
388+ i . requestBuffer ?. byteLength === bufferOut . byteLength &&
389+ this . crc8_dvb_s2_data ( new Uint8Array ( i . requestBuffer ) , 0 , i . requestBuffer . byteLength ) === keyCrc ,
390+ ) ;
400391
401392 const obj = {
402- code : code ,
393+ code,
403394 requestBuffer : bufferOut ,
404395 callback : callback_msp ,
405- callbackOnError : doCallbackOnError ,
406396 start : performance . now ( ) ,
407397 } ;
408398
409399 if ( ! requestExists ) {
410400 obj . timer = setTimeout ( ( ) => {
411401 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 ) } )`,
402+ `MSP: data request timed-out: ${ code } ID: ${ serial . connectionId } TAB: ${ GUI . active_tab } QUEUE: ${ this . callbacks . length } (${ this . callbacks . map ( ( e ) => e . code ) } )` ,
415403 ) ;
416404 serial . send ( bufferOut , ( _sendInfo ) => {
417405 obj . stop = performance . now ( ) ;
418406 const executionTime = Math . round ( obj . stop - obj . start ) ;
419- this . timeout = Math . max ( this . MIN_TIMEOUT , Math . min ( executionTime , this . MAX_TIMEOUT ) ) ;
407+ // We should probably give up connection if the request takes too long ?
408+ if ( executionTime > 5000 ) {
409+ console . warn (
410+ `MSP: data request took too long: ${ code } ID: ${ serial . connectionId } TAB: ${ GUI . active_tab } EXECUTION TIME: ${ executionTime } ms` ,
411+ ) ;
412+ }
413+
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 }
440429
441430 return true ;
442431 } ,
443-
444432 /**
445433 * resolves: {command: code, data: data, length: message_length}
446434 */
0 commit comments