@@ -211,6 +211,7 @@ void IridiumSBD::main_loop(int argc, char *argv[])
211
211
CDev::init ();
212
212
213
213
pthread_mutex_init (&_tx_buf_mutex, NULL );
214
+ pthread_mutex_init (&_rx_buf_mutex, NULL );
214
215
215
216
int arg_i = 3 ;
216
217
int arg_uart_name = 0 ;
@@ -350,26 +351,31 @@ void IridiumSBD::standby_loop(void)
350
351
if (_rx_session_pending && !_tx_session_pending) {
351
352
if (clear_mo_buffer ()) {
352
353
start_sbd_session ();
354
+ return ;
353
355
}
354
356
355
357
} else {
356
358
start_sbd_session ();
359
+ return ;
357
360
}
358
361
359
362
} else {
360
363
start_csq ();
364
+ return ;
361
365
}
362
366
}
363
367
364
368
// start a signal check if requested and not a switch to another mode is scheduled
365
369
if (((hrt_absolute_time () - _last_signal_check) > SATCOM_SIGNAL_REFRESH_DELAY)
366
370
&& (_new_state == SATCOM_STATE_STANDBY)) {
367
371
start_csq ();
372
+ return ;
368
373
}
369
374
370
375
// only read the MT buffer if the higher layer (mavlink app) read the previous message
371
- if (_rx_read_pending && (_rx_msg_read_idx == _rx_msg_end_idx)) {
376
+ if (_rx_read_pending && (_rx_msg_read_idx == _rx_msg_end_idx) && (_new_state == SATCOM_STATE_STANDBY) ) {
372
377
read_rx_buf ();
378
+ return ;
373
379
}
374
380
}
375
381
@@ -679,6 +685,7 @@ ssize_t IridiumSBD::write(struct file *filp, const char *buffer, size_t buflen)
679
685
680
686
ssize_t IridiumSBD::read (struct file *filp, char *buffer, size_t buflen)
681
687
{
688
+ pthread_mutex_lock (&_rx_buf_mutex);
682
689
VERBOSE_INFO (" READ: LEN %d, RX: %d RX END: %d" , buflen, _rx_msg_read_idx, _rx_msg_end_idx);
683
690
684
691
if (_rx_msg_read_idx < _rx_msg_end_idx) {
@@ -692,9 +699,11 @@ ssize_t IridiumSBD::read(struct file *filp, char *buffer, size_t buflen)
692
699
693
700
_rx_msg_read_idx += bytes_to_copy;
694
701
702
+ pthread_mutex_unlock (&_rx_buf_mutex);
695
703
return bytes_to_copy;
696
704
697
705
} else {
706
+ pthread_mutex_unlock (&_rx_buf_mutex);
698
707
return -EAGAIN;
699
708
}
700
709
}
@@ -767,18 +776,25 @@ void IridiumSBD::read_rx_buf(void)
767
776
return ;
768
777
}
769
778
779
+ pthread_mutex_lock (&_rx_buf_mutex);
780
+
781
+
770
782
write_at (" AT+SBDRB" );
771
783
772
784
if (read_at_msg () != SATCOM_RESULT_OK) {
773
785
VERBOSE_INFO (" READ SBD: MODEM NOT RESPONDING!" );
786
+ _rx_msg_read_idx = _rx_msg_end_idx;
787
+ pthread_mutex_unlock (&_rx_buf_mutex);
774
788
return ;
775
789
}
776
790
777
791
int data_len = (_rx_msg_buf[0 ] << 8 ) + _rx_msg_buf[1 ];
778
792
779
793
// rx_buf contains 2 byte length, data, 2 byte checksum and /r/n delimiter
780
794
if (data_len != _rx_msg_end_idx - 6 ) {
781
- VERBOSE_INFO (" READ SBD: WRONG DATA LENGTH" );
795
+ PX4_ERR (" READ SBD: WRONG DATA LENGTH" );
796
+ _rx_msg_read_idx = _rx_msg_end_idx;
797
+ pthread_mutex_unlock (&_rx_buf_mutex);
782
798
return ;
783
799
}
784
800
@@ -789,14 +805,17 @@ void IridiumSBD::read_rx_buf(void)
789
805
}
790
806
791
807
if ((checksum / 256 != _rx_msg_buf[_rx_msg_end_idx - 4 ]) || ((checksum & 255 ) != _rx_msg_buf[_rx_msg_end_idx - 3 ])) {
792
- VERBOSE_INFO (" READ SBD: WRONG DATA CHECKSUM" );
808
+ PX4_ERR (" READ SBD: WRONG DATA CHECKSUM" );
809
+ _rx_msg_read_idx = _rx_msg_end_idx;
810
+ pthread_mutex_unlock (&_rx_buf_mutex);
793
811
return ;
794
812
}
795
813
796
814
_rx_msg_read_idx = 2 ; // ignore the length
797
815
_rx_msg_end_idx -= 4 ; // ignore the checksum and delimiter
798
816
_rx_read_pending = false ;
799
817
818
+ pthread_mutex_unlock (&_rx_buf_mutex);
800
819
VERBOSE_INFO (" READ SBD: SUCCESS, LEN: %d" , data_len);
801
820
}
802
821
0 commit comments