From 616481b158ec440626aa160e24ee863851e07697 Mon Sep 17 00:00:00 2001
From: francescomani <francesco.mani@eurecom.fr>
Date: Mon, 4 Oct 2021 12:06:03 +0200
Subject: [PATCH] fix for ulsch missed detection using ndi

---
 openair1/PHY/NR_TRANSPORT/nr_ulsch_decoding.c | 21 ++++++++++++-------
 openair1/PHY/defs_gNB.h                       |  2 ++
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/openair1/PHY/NR_TRANSPORT/nr_ulsch_decoding.c b/openair1/PHY/NR_TRANSPORT/nr_ulsch_decoding.c
index febfc814cb..90f49be16b 100644
--- a/openair1/PHY/NR_TRANSPORT/nr_ulsch_decoding.c
+++ b/openair1/PHY/NR_TRANSPORT/nr_ulsch_decoding.c
@@ -368,7 +368,7 @@ void nr_processULSegment(void* arg) {
                                ulsch_harq->e[r],
                                ulsch_harq->C,
                                rv_index,
-                               (ulsch_harq->round==0)?1:0,
+                               ulsch_harq->new_rx,
                                E,
        ulsch_harq->F,
        Kr-ulsch_harq->F-2*(p_decoderParms->Z))==-1) {
@@ -507,12 +507,19 @@ uint32_t nr_ulsch_decoding(PHY_VARS_gNB *phy_vars_gNB,
   harq_process->TBS = pusch_pdu->pusch_data.tb_size;
   harq_process->round = nr_rv_round_map[pusch_pdu->pusch_data.rv_index];
 
-  // If the first detected transmission starts with RV 2 it would never decode. So we needed a way to set the round to 0
-  if( (pusch_pdu->pusch_data.rv_index==0) && (harq_process->round!=0) ) {
-    LOG_E(NR_PHY, "Mismatch between harq round %i and rv_index %i\n", harq_process->round, pusch_pdu->pusch_data.rv_index);
-    harq_process->round = 0;
+  harq_process->new_rx = false; // flag to indicate if this is a new reception for this harq (initialized to false)
+  if (harq_process->round == 0) {
+    harq_process->new_rx = true;
+    harq_process->ndi = pusch_pdu->pusch_data.new_data_indicator;
   }
- 
+
+  // this happens if there was a DTX in round 0
+  if (harq_process->ndi != pusch_pdu->pusch_data.new_data_indicator) {
+    harq_process->new_rx = true;
+    harq_process->ndi = pusch_pdu->pusch_data.new_data_indicator;
+    LOG_E(PHY,"Missed ULSCH detection. NDI toggled but rv %d does not correspond to first reception\n",pusch_pdu->pusch_data.rv_index);
+  }
+
   A   = (harq_process->TBS)<<3;
 
   LOG_D(PHY,"ULSCH Decoding, harq_pid %d TBS %d G %d mcs %d Nl %d nb_rb %d, Qm %d, n_layers %d, Coderate %d\n",harq_pid,A,G, mcs, n_layers, nb_rb, Qm, n_layers, R);
@@ -568,7 +575,7 @@ uint32_t nr_ulsch_decoding(PHY_VARS_gNB *phy_vars_gNB,
        stats->power[aarx]=dB_fixed_x10(pusch->ulsch_power[aarx]);
        stats->noise_power[aarx]=dB_fixed_x10(pusch->ulsch_noise_power[aarx]);
     }
-    if (harq_process->round == 0) {
+    if (harq_process->new_rx == 0) {
       stats->current_Qm = Qm;
       stats->current_RI = n_layers;
       stats->total_bytes_tx += harq_process->TBS;
diff --git a/openair1/PHY/defs_gNB.h b/openair1/PHY/defs_gNB.h
index 9a1ba7e780..855eab216c 100644
--- a/openair1/PHY/defs_gNB.h
+++ b/openair1/PHY/defs_gNB.h
@@ -251,6 +251,8 @@ typedef struct {
   uint32_t slot;
   /// Index of current HARQ round for this DLSCH
   uint8_t round;
+  uint8_t ndi;
+  bool new_rx;
   /// Last TPC command
   uint8_t TPC;
   /// MIMO mode for this DLSCH
-- 
2.26.2