@ -26,17 +26,24 @@ mac_pcap::~mac_pcap()
void mac_pcap : : enable ( bool enable_ )
void mac_pcap : : enable ( bool enable_ )
{
{
std : : lock_guard < std : : mutex > lock ( mutex ) ;
running = enable_ ;
running = enable_ ;
}
}
uint32_t mac_pcap : : open ( const char * filename , uint32_t ue_id_ )
uint32_t mac_pcap : : open ( const char * filename , uint32_t ue_id_ )
{
{
std : : lock_guard < std : : mutex > lock ( mutex ) ;
if ( pcap_file ! = nullptr ) {
if ( pcap_file ! = nullptr ) {
log - > error ( " PCAP writer already running. Close first. \n " ) ;
log - > error ( " PCAP writer already running. Close first. \n " ) ;
return SRSLTE_ERROR ;
return SRSLTE_ERROR ;
}
}
pcap_file = LTE_PCAP_Open ( MAC_LTE_DLT , filename ) ;
pcap_file = LTE_PCAP_Open ( MAC_LTE_DLT , filename ) ;
if ( pcap_file = = nullptr ) {
log - > error ( " Couldn't open file to write PCAP \n " ) ;
return SRSLTE_ERROR ;
}
ue_id = ue_id_ ;
ue_id = ue_id_ ;
running = true ;
running = true ;
@ -48,6 +55,8 @@ uint32_t mac_pcap::open(const char* filename, uint32_t ue_id_)
uint32_t mac_pcap : : close ( )
uint32_t mac_pcap : : close ( )
{
{
{
std : : lock_guard < std : : mutex > lock ( mutex ) ;
if ( running = = false | | pcap_file = = nullptr ) {
if ( running = = false | | pcap_file = = nullptr ) {
return SRSLTE_ERROR ;
return SRSLTE_ERROR ;
}
}
@ -56,13 +65,17 @@ uint32_t mac_pcap::close()
running = false ;
running = false ;
pcap_pdu_t pdu = { } ;
pcap_pdu_t pdu = { } ;
queue . push ( std : : move ( pdu ) ) ;
queue . push ( std : : move ( pdu ) ) ;
}
wait_thread_finish ( ) ;
wait_thread_finish ( ) ;
// close file handle
// close file handle
{
std : : lock_guard < std : : mutex > lock ( mutex ) ;
srslte : : console ( " Saving MAC PCAP file \n " ) ;
srslte : : console ( " Saving MAC PCAP file \n " ) ;
LTE_PCAP_Close ( pcap_file ) ;
LTE_PCAP_Close ( pcap_file ) ;
pcap_file = nullptr ;
pcap_file = nullptr ;
}
return SRSLTE_SUCCESS ;
return SRSLTE_SUCCESS ;
}
}
@ -79,10 +92,14 @@ void mac_pcap::run_thread()
// blocking write until stopped
// blocking write until stopped
while ( running ) {
while ( running ) {
pcap_pdu_t pdu = queue . wait_pop ( ) ;
pcap_pdu_t pdu = queue . wait_pop ( ) ;
{
std : : lock_guard < std : : mutex > lock ( mutex ) ;
write_pdu ( pdu ) ;
write_pdu ( pdu ) ;
}
}
}
// write remainder of queue
// write remainder of queue
std : : lock_guard < std : : mutex > lock ( mutex ) ;
pcap_pdu_t pdu = { } ;
pcap_pdu_t pdu = { } ;
while ( queue . try_pop ( & pdu ) ) {
while ( queue . try_pop ( & pdu ) ) {
write_pdu ( pdu ) ;
write_pdu ( pdu ) ;
@ -91,9 +108,11 @@ void mac_pcap::run_thread()
void mac_pcap : : set_ue_id ( uint16_t ue_id_ )
void mac_pcap : : set_ue_id ( uint16_t ue_id_ )
{
{
std : : lock_guard < std : : mutex > lock ( mutex ) ;
ue_id = ue_id_ ;
ue_id = ue_id_ ;
}
}
// Function called from PHY worker context, locking not needed as PDU queue is thread-safe
void mac_pcap : : pack_and_queue ( uint8_t * payload ,
void mac_pcap : : pack_and_queue ( uint8_t * payload ,
uint32_t payload_len ,
uint32_t payload_len ,
uint32_t reTX ,
uint32_t reTX ,