diff --git a/lib/include/srslte/common/mac_pcap.h b/lib/include/srslte/common/mac_pcap.h index 06ef6ebd4..43ecaa9b9 100644 --- a/lib/include/srslte/common/mac_pcap.h +++ b/lib/include/srslte/common/mac_pcap.h @@ -19,9 +19,9 @@ #include "srslte/common/logmap.h" #include "srslte/common/pcap.h" #include "srslte/common/threads.h" +#include #include #include - namespace srslte { class mac_pcap : srslte::thread { @@ -71,6 +71,7 @@ private: unique_byte_buffer_t pdu; } pcap_pdu_t; block_queue queue; + std::mutex mutex; void write_pdu(pcap_pdu_t& pdu); void run_thread() final; diff --git a/lib/src/common/mac_pcap.cc b/lib/src/common/mac_pcap.cc index 8de3ab555..ca5581c99 100644 --- a/lib/src/common/mac_pcap.cc +++ b/lib/src/common/mac_pcap.cc @@ -26,17 +26,24 @@ mac_pcap::~mac_pcap() void mac_pcap::enable(bool enable_) { + std::lock_guard lock(mutex); running = enable_; } uint32_t mac_pcap::open(const char* filename, uint32_t ue_id_) { + std::lock_guard lock(mutex); if (pcap_file != nullptr) { log->error("PCAP writer already running. Close first.\n"); return SRSLTE_ERROR; } 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_; running = true; @@ -48,21 +55,27 @@ uint32_t mac_pcap::open(const char* filename, uint32_t ue_id_) uint32_t mac_pcap::close() { - if (running == false || pcap_file == nullptr) { - return SRSLTE_ERROR; - } + { + std::lock_guard lock(mutex); + if (running == false || pcap_file == nullptr) { + return SRSLTE_ERROR; + } - // tell writer thread to stop - running = false; - pcap_pdu_t pdu = {}; - queue.push(std::move(pdu)); + // tell writer thread to stop + running = false; + pcap_pdu_t pdu = {}; + queue.push(std::move(pdu)); + } wait_thread_finish(); // close file handle - srslte::console("Saving MAC PCAP file\n"); - LTE_PCAP_Close(pcap_file); - pcap_file = nullptr; + { + std::lock_guard lock(mutex); + srslte::console("Saving MAC PCAP file\n"); + LTE_PCAP_Close(pcap_file); + pcap_file = nullptr; + } return SRSLTE_SUCCESS; } @@ -79,11 +92,15 @@ void mac_pcap::run_thread() // blocking write until stopped while (running) { pcap_pdu_t pdu = queue.wait_pop(); - write_pdu(pdu); + { + std::lock_guard lock(mutex); + write_pdu(pdu); + } } // write remainder of queue - pcap_pdu_t pdu = {}; + std::lock_guard lock(mutex); + pcap_pdu_t pdu = {}; while (queue.try_pop(&pdu)) { write_pdu(pdu); } @@ -91,9 +108,11 @@ void mac_pcap::run_thread() void mac_pcap::set_ue_id(uint16_t ue_id_) { + std::lock_guard lock(mutex); 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, uint32_t payload_len, uint32_t reTX,