From 49d857cd177c1e82e2377cab40a2e1308c5054e8 Mon Sep 17 00:00:00 2001 From: Xavier Arteaga Date: Mon, 7 Jun 2021 13:17:44 +0200 Subject: [PATCH] Refactored decode_tb function --- lib/src/phy/phch/sch.c | 103 ++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 57 deletions(-) diff --git a/lib/src/phy/phch/sch.c b/lib/src/phy/phch/sch.c index c740659c9..50e41e0ec 100644 --- a/lib/src/phy/phch/sch.c +++ b/lib/src/phy/phch/sch.c @@ -504,72 +504,61 @@ static int decode_tb(srsran_sch_t* q, int16_t* e_bits, uint8_t* data) { - if (q != NULL && data != NULL && softbuffer != NULL && e_bits != NULL && cb_segm != NULL && Qm != 0) { - if (cb_segm->tbs == 0 || cb_segm->C == 0) { - return SRSRAN_SUCCESS; - } - - if (cb_segm->F) { - fprintf(stderr, "Error filler bits are not supported. Use standard TBS\n"); - return SRSRAN_ERROR_INVALID_INPUTS; - } + // Check inputs + if (q == NULL || data == NULL || softbuffer == NULL || e_bits == NULL || cb_segm == NULL || Qm == 0) { + ERROR("Missing inputs: data=%d, softbuffer=%d, e_bits=%d, cb_segm=%d Qm=%d", + data != 0, + softbuffer != 0, + e_bits != 0, + cb_segm != 0, + Qm); + return SRSRAN_ERROR_INVALID_INPUTS; + } - if (cb_segm->C > softbuffer->max_cb) { - fprintf(stderr, - "Error number of CB to decode (%d) exceeds soft buffer size (%d CBs)\n", - cb_segm->C, - softbuffer->max_cb); - return SRSRAN_ERROR_INVALID_INPUTS; - } + // Check segmentation is valid + if (cb_segm->tbs == 0 || cb_segm->C == 0) { + return SRSRAN_SUCCESS; + } - bool crc_ok = true; + if (cb_segm->F) { + fprintf(stderr, "Error filler bits are not supported. Use standard TBS\n"); + return SRSRAN_ERROR_INVALID_INPUTS; + } - data[cb_segm->tbs / 8 + 0] = 0; - data[cb_segm->tbs / 8 + 1] = 0; - data[cb_segm->tbs / 8 + 2] = 0; + if (cb_segm->C > softbuffer->max_cb) { + fprintf(stderr, + "Error number of CB to decode (%d) exceeds soft buffer size (%d CBs)\n", + cb_segm->C, + softbuffer->max_cb); + return SRSRAN_ERROR_INVALID_INPUTS; + } - // Process Codeblocks - crc_ok = decode_tb_cb(q, softbuffer, cb_segm, Qm, rv, nof_e_bits, e_bits, data); + // Process Codeblocks + bool cb_crc_ok = decode_tb_cb(q, softbuffer, cb_segm, Qm, rv, nof_e_bits, e_bits, data); - if (crc_ok) { - uint32_t par_rx = 0, par_tx = 0; + // If any of the CBs CRC is KO + if (!cb_crc_ok) { + INFO("Error in CB parity"); + return SRSRAN_ERROR; + } - // Compute transport block CRC - par_rx = srsran_crc_checksum_byte(&q->crc_tb, data, cb_segm->tbs); + // One CB CRC OK, means TB CRC is OK. + if (cb_segm->C == 1) { + INFO("TB decoded OK"); + return SRSRAN_SUCCESS; + } - // check parity bits - par_tx = ((uint32_t)data[cb_segm->tbs / 8 + 0]) << 16 | ((uint32_t)data[cb_segm->tbs / 8 + 1]) << 8 | - ((uint32_t)data[cb_segm->tbs / 8 + 2]); + // Check TB CRC for whole TB + if (srsran_crc_match_byte(&q->crc_tb, data, cb_segm->tbs)) { + INFO("TB decoded OK"); + return SRSRAN_SUCCESS; + } - // Check if all the bytes are zeros - bool all_zeros = true; - for (uint32_t i = 0; i < cb_segm->tbs / 8 && all_zeros; i++) { - all_zeros = (data[i] == 0); - } - if (all_zeros) { - INFO("Error in TB decode: it is all zeros!"); - return SRSRAN_ERROR; - } + // TB CRC check failed, as at least one CB had a false alarm, reset all CB CRC flags in the softbuffer + srsran_softbuffer_rx_reset_cb_crc(softbuffer, cb_segm->C); - if (par_rx == par_tx) { - INFO("TB decoded OK"); - return SRSRAN_SUCCESS; - } else { - INFO("Error in TB parity: par_tx=0x%x, par_rx=0x%x", par_tx, par_rx); - return SRSRAN_ERROR; - } - } else { - return SRSRAN_ERROR; - } - } else { - ERROR("Missing inputs: data=%d, softbuffer=%d, e_bits=%d, cb_segm=%d Qm=%d", - data != 0, - softbuffer != 0, - e_bits != 0, - cb_segm != 0, - Qm); - return SRSRAN_ERROR_INVALID_INPUTS; - } + INFO("Error in TB parity"); + return SRSRAN_ERROR; } int srsran_dlsch_decode(srsran_sch_t* q, srsran_pdsch_cfg_t* cfg, int16_t* e_bits, uint8_t* data)