diff --git a/lib/include/srslte/upper/rlc_am_nr.h b/lib/include/srslte/upper/rlc_am_nr.h index aaf4a3468..f214f04d9 100644 --- a/lib/include/srslte/upper/rlc_am_nr.h +++ b/lib/include/srslte/upper/rlc_am_nr.h @@ -59,6 +59,18 @@ uint32_t rlc_am_nr_write_data_pdu_header(const rlc_am_nr_pdu_header_t& header, b uint32_t rlc_am_nr_packed_length(const rlc_am_nr_pdu_header_t& header); +uint32_t +rlc_am_nr_read_status_pdu(const byte_buffer_t* pdu, const rlc_am_nr_sn_size_t sn_size, rlc_am_nr_status_pdu_t* status); + +uint32_t rlc_am_nr_read_status_pdu(const uint8_t* payload, + const uint32_t nof_bytes, + const rlc_am_nr_sn_size_t sn_size, + rlc_am_nr_status_pdu_t* status); + +int32_t rlc_am_nr_write_status_pdu(const rlc_am_nr_status_pdu_t& status_pdu, + const rlc_am_nr_sn_size_t sn_size, + byte_buffer_t* pdu); + } // namespace srslte #endif // SRSLTE_RLC_AM_NR_H diff --git a/lib/include/srslte/upper/rlc_common.h b/lib/include/srslte/upper/rlc_common.h index 3b1286852..fee78d101 100644 --- a/lib/include/srslte/upper/rlc_common.h +++ b/lib/include/srslte/upper/rlc_common.h @@ -77,6 +77,13 @@ static inline uint8_t operator&(rlc_nr_si_field_t lhs, int rhs) static_cast::type>(rhs)); } +enum class rlc_am_nr_control_pdu_type_t : unsigned { status_pdu = 0b000, nulltype }; +inline std::string to_string(const rlc_am_nr_control_pdu_type_t& type) +{ + constexpr static const char* options[] = {"Control PDU"}; + return enum_to_text(options, (uint32_t)rlc_am_nr_control_pdu_type_t::nulltype, (uint32_t)type); +} + typedef enum { RLC_DC_FIELD_CONTROL_PDU = 0, RLC_DC_FIELD_DATA_PDU, @@ -150,9 +157,9 @@ struct rlc_amd_pdu_header_t{ } }; -// NACK helper +// NACK helper (for LTE and NR) struct rlc_status_nack_t{ - uint16_t nack_sn; + uint32_t nack_sn; bool has_so; uint16_t so_start; uint16_t so_end; @@ -181,6 +188,15 @@ typedef struct { uint16_t so; ///< Sequence offset } rlc_am_nr_pdu_header_t; +///< AM NR Status PDU header (perhaps merge with LTE version) +typedef struct { + rlc_am_nr_control_pdu_type_t cpt; + uint32_t ack_sn; ///< SN of the next not received RLC Data PDU + uint16_t N_nack; ///< number of NACKs + uint8_t nack_range; ///< number of consecutively lost RLC SDUs starting from and including NACK_SN + rlc_status_nack_t nacks[RLC_AM_WINDOW_SIZE]; +} rlc_am_nr_status_pdu_t; + /**************************************************************************** * RLC Common interface * Common interface for all RLC entities diff --git a/lib/src/upper/rlc_am_nr.cc b/lib/src/upper/rlc_am_nr.cc index fa4778800..73bfcef26 100644 --- a/lib/src/upper/rlc_am_nr.cc +++ b/lib/src/upper/rlc_am_nr.cc @@ -146,4 +146,114 @@ uint32_t rlc_am_nr_write_data_pdu_header(const rlc_am_nr_pdu_header_t& header, b return len; } +uint32_t +rlc_am_nr_read_status_pdu(const byte_buffer_t* pdu, const rlc_am_nr_sn_size_t sn_size, rlc_am_nr_status_pdu_t* status) +{ + return rlc_am_nr_read_status_pdu(pdu->msg, pdu->N_bytes, sn_size, status); +} + +uint32_t rlc_am_nr_read_status_pdu(const uint8_t* payload, + const uint32_t nof_bytes, + const rlc_am_nr_sn_size_t sn_size, + rlc_am_nr_status_pdu_t* status) +{ + uint8_t* ptr = const_cast(payload); + + // fixed part + status->cpt = (rlc_am_nr_control_pdu_type_t)((*ptr >> 4) & 0x07); // 3 bits CPT + + // sanity check + if (status->cpt != rlc_am_nr_control_pdu_type_t::status_pdu) { + fprintf(stderr, "Malformed PDU, reserved bits are set.\n"); + return 0; + } + + if (sn_size == rlc_am_nr_sn_size_t::size12bits) { + status->ack_sn = (*ptr & 0x0F) << 8; // first 4 bits SN + ptr++; + + status->ack_sn |= (*ptr & 0xFF); // last 8 bits SN + ptr++; + + // read E1 flag + uint8_t e1 = *ptr & 0x80; + + // sanity check for reserved bits + if ((*ptr & 0x7f) != 0) { + fprintf(stderr, "Malformed PDU, reserved bits are set.\n"); + return 0; + } + + // all good, continue with next byte depending on E1 + ptr++; + + // reset number of acks + status->N_nack = 0; + + if (e1) { + // E1 flag set, read a NACK_SN + rlc_status_nack_t nack = {}; + nack.nack_sn = (*ptr & 0xff) << 4; + ptr++; + // uint8_t len2 = (*ptr & 0xF0) >> 4; + nack.nack_sn |= (*ptr & 0xF0) >> 4; + status->nacks[status->N_nack] = nack; + + status->N_nack++; + } + } + + return SRSLTE_SUCCESS; +} + +/** + * Write a RLC AM NR status PDU to a PDU buffer and eets the length of the generate PDU accordingly + * @param status_pdu The status PDU + * @param pdu A pointer to a unique bytebuffer + * @return SRSLTE_SUCCESS if PDU was written, SRSLTE_ERROR otherwise + */ +int32_t rlc_am_nr_write_status_pdu(const rlc_am_nr_status_pdu_t& status_pdu, + const rlc_am_nr_sn_size_t sn_size, + byte_buffer_t* pdu) +{ + uint8_t* ptr = pdu->msg; + + // fixed header part + *ptr = 0; ///< 1 bit D/C field and 3bit CPT are all zero + + if (sn_size == rlc_am_nr_sn_size_t::size12bits) { + // write first 4 bit of ACK_SN + *ptr |= (status_pdu.ack_sn >> 8) & 0x0f; // 4 bit ACK_SN + ptr++; + *ptr = status_pdu.ack_sn & 0xff; // remaining 8 bit of SN + ptr++; + + // write E1 flag in octet 3 + *ptr = (status_pdu.N_nack > 0) ? 0x80 : 0x00; + ptr++; + + if (status_pdu.N_nack > 0) { + // write first 8 bit of NACK_SN + *ptr = (status_pdu.nacks[0].nack_sn >> 4) & 0xff; + ptr++; + + // write remaining 4 bits of NACK_SN + *ptr = status_pdu.nacks[0].nack_sn & 0xf0; + ptr++; + } + } else { + // 18bit SN + *ptr |= (status_pdu.ack_sn >> 14) & 0x0f; // 4 bit ACK_SN + ptr++; + *ptr = status_pdu.ack_sn >> 8; // bit 3 - 10 of SN + ptr++; + *ptr = (status_pdu.ack_sn & 0xff); // remaining 6 bit of SN + ptr++; + } + + pdu->N_bytes = ptr - pdu->msg; + + return SRSLTE_SUCCESS; +} + } // namespace srslte diff --git a/lib/test/upper/rlc_am_nr_pdu_test.cc b/lib/test/upper/rlc_am_nr_pdu_test.cc index b8af1b058..251a18ccd 100644 --- a/lib/test/upper/rlc_am_nr_pdu_test.cc +++ b/lib/test/upper/rlc_am_nr_pdu_test.cc @@ -234,6 +234,66 @@ int rlc_am_nr_pdu_test6() return SRSLTE_SUCCESS; } +///< Control PDU tests +// Status PDU for 12bit SN with ACK_SN=2065 and no further NACK_SN (E1 bit not set) +int rlc_am_nr_control_pdu_test1() +{ + const int len = 3; + std::array tv = {0x08, 0x11, 0x00}; + srslte::byte_buffer_t pdu = make_pdu_and_log(tv); + + TESTASSERT(rlc_am_is_control_pdu(pdu.msg) == true); + + // unpack PDU + rlc_am_nr_status_pdu_t status_pdu = {}; + TESTASSERT(rlc_am_nr_read_status_pdu(&pdu, srslte::rlc_am_nr_sn_size_t::size12bits, &status_pdu) == SRSLTE_SUCCESS); + TESTASSERT(status_pdu.ack_sn == 2065); + TESTASSERT(status_pdu.N_nack == 0); + + // reset status PDU + pdu.clear(); + + // pack again + TESTASSERT(rlc_am_nr_write_status_pdu(status_pdu, srslte::rlc_am_nr_sn_size_t::size12bits, &pdu) == SRSLTE_SUCCESS); + TESTASSERT(pdu.N_bytes == tv.size()); + + write_pdu_to_pcap(4, pdu.msg, pdu.N_bytes); + + TESTASSERT(memcmp(pdu.msg, tv.data(), pdu.N_bytes) == 0); + + return SRSLTE_SUCCESS; +} + +// Status PDU for 12bit SN with ACK_SN=2065 and NACK_SN=273 (E1 bit set) +int rlc_am_nr_control_pdu_test2() +{ + const int len = 5; + std::array tv = {0x08, 0x11, 0x80, 0x11, 0x10}; + srslte::byte_buffer_t pdu = make_pdu_and_log(tv); + + TESTASSERT(rlc_am_is_control_pdu(pdu.msg) == true); + + // unpack PDU + rlc_am_nr_status_pdu_t status_pdu = {}; + TESTASSERT(rlc_am_nr_read_status_pdu(&pdu, srslte::rlc_am_nr_sn_size_t::size12bits, &status_pdu) == SRSLTE_SUCCESS); + TESTASSERT(status_pdu.ack_sn == 2065); + TESTASSERT(status_pdu.N_nack == 1); + TESTASSERT(status_pdu.nacks[0].nack_sn == 273); + + // reset status PDU + pdu.clear(); + + // pack again + TESTASSERT(rlc_am_nr_write_status_pdu(status_pdu, srslte::rlc_am_nr_sn_size_t::size12bits, &pdu) == SRSLTE_SUCCESS); + // TESTASSERT(pdu.N_bytes == tv.size()); + + write_pdu_to_pcap(4, pdu.msg, pdu.N_bytes); + + TESTASSERT(memcmp(pdu.msg, tv.data(), pdu.N_bytes) == 0); + + return SRSLTE_SUCCESS; +} + int main(int argc, char** argv) { #if PCAP @@ -271,5 +331,15 @@ int main(int argc, char** argv) return SRSLTE_ERROR; } + if (rlc_am_nr_control_pdu_test1()) { + fprintf(stderr, "rlc_am_nr_control_pdu_test1() failed.\n"); + return SRSLTE_ERROR; + } + + if (rlc_am_nr_control_pdu_test2()) { + fprintf(stderr, "rlc_am_nr_control_pdu_test2() failed.\n"); + return SRSLTE_ERROR; + } + return SRSLTE_SUCCESS; }