@@ -74,6 +74,7 @@ var MSP = {
7474 message_buffer : null ,
7575 message_buffer_uint8_view : null ,
7676 message_checksum : 0 ,
77+ message_flag : 0 ,
7778 callbacks : [ ] ,
7879 packet_error : 0 ,
7980 unsupported : 0 ,
@@ -100,7 +101,13 @@ var MSP = {
100101 } ,
101102
102103 read : function ( readInfo ) {
103- var data = new Uint8Array ( readInfo . data ) ;
104+ var data ;
105+ try {
106+ data = new Uint8Array ( readInfo . data ) ;
107+ } catch ( e ) {
108+ console . error ( 'MSP read: Failed to create Uint8Array from readInfo.data:' , e , 'readInfo:' , readInfo ) ;
109+ return ;
110+ }
104111
105112 for ( var i = 0 ; i < data . length ; i ++ ) {
106113 switch ( this . state ) {
@@ -141,7 +148,8 @@ var MSP = {
141148 this . decoder_states . FLAG_V2 ;
142149 break ;
143150 case this . decoder_states . FLAG_V2 :
144- // Ignored for now
151+ // Store flag for CRC computation
152+ this . message_flag = data [ i ] ;
145153 this . state = this . decoder_states . CODE_V2_LOW ;
146154 break ;
147155 case this . decoder_states . PAYLOAD_LENGTH_V1 :
@@ -227,7 +235,7 @@ var MSP = {
227235 break ;
228236 case this . decoder_states . CHECKSUM_V2 :
229237 this . message_checksum = 0 ;
230- this . message_checksum = this . _crc8_dvb_s2 ( this . message_checksum , 0 ) ; // flag
238+ this . message_checksum = this . _crc8_dvb_s2 ( this . message_checksum , this . message_flag ) ; // flag
231239 this . message_checksum = this . _crc8_dvb_s2 ( this . message_checksum , this . code & 0xFF ) ;
232240 this . message_checksum = this . _crc8_dvb_s2 ( this . message_checksum , ( this . code & 0xFF00 ) >> 8 ) ;
233241 this . message_checksum = this . _crc8_dvb_s2 ( this . message_checksum , this . message_length_expected & 0xFF ) ;
@@ -254,26 +262,29 @@ var MSP = {
254262 } ,
255263
256264 _dispatch_message ( expected_checksum ) {
257- if ( this . message_checksum == expected_checksum ) {
258- // message received, process
259- this . processData ( this ) ;
260- this . lastFrameReceivedMs = Date . now ( ) ;
261- } else {
262- console . log ( 'code: ' + this . code + ' - crc failed' ) ;
263- this . packet_error ++ ;
264- $ ( 'span.packet-error' ) . html ( this . packet_error ) ;
265- }
266-
267- /*
268- * Free port
269- */
270- timeout . add ( 'delayedFreeHardLock' , function ( ) {
271- mspQueue . freeHardLock ( ) ;
272- } , 10 ) ;
265+ // Use try-finally to ensure state is ALWAYS reset, even if processData throws
266+ try {
267+ if ( this . message_checksum == expected_checksum ) {
268+ // message received, process
269+ this . processData ( this ) ;
270+ this . lastFrameReceivedMs = Date . now ( ) ;
271+ } else {
272+ console . log ( 'code: ' + this . code + ' - crc failed' ) ;
273+ this . packet_error ++ ;
274+ $ ( 'span.packet-error' ) . html ( this . packet_error ) ;
275+ }
273276
274- // Reset variables
275- this . message_length_received = 0 ;
276- this . state = this . decoder_states . IDLE ;
277+ /*
278+ * Free port
279+ */
280+ timeout . add ( 'delayedFreeHardLock' , function ( ) {
281+ mspQueue . freeHardLock ( ) ;
282+ } , 10 ) ;
283+ } finally {
284+ // Reset variables - MUST happen even if an exception occurred
285+ this . message_length_received = 0 ;
286+ this . state = this . decoder_states . IDLE ;
287+ }
277288 } ,
278289
279290 /**
0 commit comments