mac_sch_pdu_nr: add to_string() helper to format MAC PDUs in logs

master
Andre Puschmann 3 years ago
parent 1dfc884b65
commit 20c0e0bc41

@ -102,6 +102,8 @@ public:
// Used by BSR procedure to determine size of BSR types
static uint32_t sizeof_ce(uint32_t lcid, bool is_ul);
void to_string(fmt::memory_buffer& buffer);
private:
srslog::basic_logger* logger;
@ -141,6 +143,8 @@ public:
uint32_t get_remaing_len();
void to_string(fmt::memory_buffer& buffer);
private:
uint32_t size_header_sdu(const uint32_t lcid_, const uint32_t nbytes);
/// Private helper that adds a subPDU to the MAC PDU

@ -278,6 +278,64 @@ inline bool mac_sch_subpdu_nr::is_ul_ccch()
return (parent->is_ulsch() && (lcid == CCCH_SIZE_48 || lcid == CCCH_SIZE_64));
}
void mac_sch_subpdu_nr::to_string(fmt::memory_buffer& buffer)
{
// Add space for new subPDU
fmt::format_to(buffer, " ");
// print subPDU
if (is_sdu()) {
fmt::format_to(buffer, "LCID={} len={}", get_lcid(), get_sdu_length());
} else {
if (parent->is_ulsch()) {
// UL-SCH case
switch (get_lcid()) {
case mac_sch_subpdu_nr::CRNTI:
fmt::format_to(buffer, "CRNTI: {:#04x}", get_c_rnti());
break;
case mac_sch_subpdu_nr::SHORT_TRUNC_BSR:
fmt::format_to(buffer, "SHORT_TRUNC_BSR: len={}", get_total_length());
break;
case mac_sch_subpdu_nr::LONG_TRUNC_BSR:
fmt::format_to(buffer, "LONG_TRUNC_BSR: len={}", get_total_length());
break;
case mac_sch_subpdu_nr::SHORT_BSR: {
lcg_bsr_t sbsr = get_sbsr();
fmt::format_to(buffer, "SBSR: lcg={} bs={}", sbsr.lcg_id, sbsr.buffer_size);
} break;
case mac_sch_subpdu_nr::LONG_BSR:
fmt::format_to(buffer, "LBSR: len={}", get_total_length());
break;
case mac_sch_subpdu_nr::SE_PHR:
fmt::format_to(buffer, "SE_PHR: ph={} pc={}", get_phr(), get_pcmax());
break;
case mac_sch_subpdu_nr::PADDING:
fmt::format_to(buffer, "PAD: len={}", get_sdu_length());
break;
default:
fmt::format_to(buffer, "CE={}", get_lcid());
break;
}
} else {
// DL-SCH PDU
switch (get_lcid()) {
case mac_sch_subpdu_nr::TA_CMD:
fmt::format_to(buffer, "TA: id={} command={}", get_ta().tag_id, get_ta().ta_command);
break;
case mac_sch_subpdu_nr::CON_RES_ID:
fmt::format_to(buffer, "CONRES: len={}", get_total_length());
break;
case mac_sch_subpdu_nr::PADDING:
fmt::format_to(buffer, "PAD: len={}", get_sdu_length());
break;
default:
fmt::format_to(buffer, "CE={}", get_lcid());
break;
}
}
}
}
void mac_sch_pdu_nr::pack()
{
// SDU and CEs are written in-place, only add padding if needed
@ -436,4 +494,12 @@ uint32_t mac_sch_pdu_nr::add_sudpdu(mac_sch_subpdu_nr& subpdu)
return SRSRAN_SUCCESS;
}
void mac_sch_pdu_nr::to_string(fmt::memory_buffer& buffer)
{
fmt::format_to(buffer, "{}", is_ulsch() ? "UL" : "DL");
for (auto& subpdu : subpdus) {
subpdu.to_string(buffer);
}
}
} // namespace srsran

@ -1000,7 +1000,6 @@ uint8_t sch_subh::phr_report_table(float phr_value)
void rar_pdu::to_string(fmt::memory_buffer& buffer)
{
std::string msg("MAC PDU for RAR: ");
fmt::format_to(buffer, "MAC PDU for RAR: ");
pdu::to_string(buffer);
}

@ -11,6 +11,7 @@
*/
#include "srsran/common/mac_pcap.h"
#include "srsran/common/string_helpers.h"
#include "srsran/common/test_common.h"
#include "srsran/config.h"
#include "srsran/mac/mac_rar_pdu_nr.h"
@ -217,8 +218,13 @@ int mac_dl_sch_pdu_pack_test5()
pcap_handle->write_dl_crnti_nr(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI);
}
// pretty print PDU
fmt::memory_buffer buff;
tx_pdu.to_string(buff);
auto& mac_logger = srslog::fetch_basic_logger("MAC");
mac_logger.info(tx_buffer.msg, tx_buffer.N_bytes, "Generated MAC PDU (%d B)", tx_buffer.N_bytes);
mac_logger.info(
tx_buffer.msg, tx_buffer.N_bytes, "Generated MAC PDU (%d B): %s", tx_buffer.N_bytes, srsran::to_c_str(buff));
return SRSRAN_SUCCESS;
}
@ -587,6 +593,13 @@ int mac_dl_sch_pdu_unpack_and_pack_test6()
// 4th is padding
subpdu = pdu_rx.get_subpdu(3);
TESTASSERT(subpdu.get_lcid() == mac_sch_subpdu_nr::PADDING);
// pretty print PDU
fmt::memory_buffer buff;
pdu_rx.to_string(buff);
auto& mac_logger = srslog::fetch_basic_logger("MAC");
mac_logger.info("Received PDU: %s", srsran::to_c_str(buff));
}
// Let's pack the entire PDU again

Loading…
Cancel
Save