From 9653b954d3891eda58911dc99d069fa72be65557 Mon Sep 17 00:00:00 2001
From: cig <guido.casati@iis.fraunhofer.de>
Date: Mon, 10 Feb 2020 12:21:18 +0100
Subject: [PATCH] Contention resolution TS 38.321 ch 5.1.5

---
 openair1/SCHED_NR_UE/phy_procedures_nr_ue.c  | 11 +++++
 openair2/LAYER2/NR_MAC_UE/mac_proto.h        |  4 ++
 openair2/LAYER2/NR_MAC_UE/nr_ue_procedures.c | 46 ++++++++++++++++++++
 3 files changed, 61 insertions(+)

diff --git a/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c b/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
index 8afdd28ce9..3427181006 100644
--- a/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
+++ b/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
@@ -3273,6 +3273,17 @@ void nr_process_rar(nr_downlink_indication_t *dl_info) {
   }
 }
 
+// if contention resolution fails, go back to UE mode PRACH
+void nr_ra_failed(uint8_t Mod_id, uint8_t CC_id, uint8_t gNB_index) {
+
+  PHY_VARS_NR_UE *ue = PHY_vars_UE_g[Mod_id][CC_id];
+  ue->UE_mode[gNB_index] = PRACH;
+
+  for (int i=0; i <RX_NB_TH_MAX; i++ ) {
+    ue->pdcch_vars[i][gNB_index]->pdcch_config[0].rnti = 0;
+  }
+  LOG_E(PHY,"[UE %d] [RAPROC] Random-access procedure fails, going back to PRACH\n", Mod_id);
+}
 
 void nr_ue_dlsch_procedures(PHY_VARS_NR_UE *ue,
        UE_nr_rxtx_proc_t *proc,
diff --git a/openair2/LAYER2/NR_MAC_UE/mac_proto.h b/openair2/LAYER2/NR_MAC_UE/mac_proto.h
index 17dce1fc23..24ffb7347f 100755
--- a/openair2/LAYER2/NR_MAC_UE/mac_proto.h
+++ b/openair2/LAYER2/NR_MAC_UE/mac_proto.h
@@ -216,5 +216,9 @@ uint16_t nr_ue_process_rar(module_id_t mod_id,
 
 void nr_process_rar(nr_downlink_indication_t *dl_info);
 
+void ue_contention_resolution(module_id_t module_id, uint8_t gNB_index, int cc_id, frame_t tx_frame);
+
+void nr_ra_failed(uint8_t Mod_id, uint8_t CC_id, uint8_t gNB_index);
+
 #endif
 /** @}*/
diff --git a/openair2/LAYER2/NR_MAC_UE/nr_ue_procedures.c b/openair2/LAYER2/NR_MAC_UE/nr_ue_procedures.c
index 7c99ed36a6..d83baffa33 100644
--- a/openair2/LAYER2/NR_MAC_UE/nr_ue_procedures.c
+++ b/openair2/LAYER2/NR_MAC_UE/nr_ue_procedures.c
@@ -728,6 +728,9 @@ NR_UE_L2_STATE_t nr_ue_scheduler(const module_id_t module_id,
     */
   }
 
+  if (mac->RA_contention_resolution_timer_active == 1) {
+    ue_contention_resolution(module_id, gNB_index, cc_id, tx_frame);
+  }
 
   mac->scheduled_response.dl_config = dl_config;
     
@@ -735,6 +738,49 @@ NR_UE_L2_STATE_t nr_ue_scheduler(const module_id_t module_id,
   return UE_CONNECTION_OK;
 }
 
+////////////////////////////////////////////////////////////////////////////
+/////////* Random Access Contention Resolution (5.1.35 TS 38.321) */////////
+////////////////////////////////////////////////////////////////////////////
+// Handling contention resolution timer
+// WIP todo:
+// - beam failure recovery
+// - RA completed
+
+void ue_contention_resolution(module_id_t module_id, uint8_t gNB_index, int cc_id, frame_t tx_frame){
+  
+  NR_UE_MAC_INST_t *mac = get_mac_inst(module_id);
+  NR_RACH_ConfigCommon_t *rach_ConfigCommon;
+
+  if (mac->RA_contention_resolution_timer_active == 1) {
+    if (mac->nr_rach_ConfigCommon) {
+      rach_ConfigCommon = &mac->nr_rach_ConfigCommon;
+    } else {
+      // LOG_E(MAC, "FATAL: radioResourceConfigCommon is NULL!!!\n");
+      // VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER,VCD_FUNCTION_OUT);
+      // stop_meas(&mac->ue_scheduler);
+      // AssertFatal(1 == 0, "");
+      // #if UE_TIMING_TRACE
+      //   stop_meas(&mac->ue_scheduler);
+      // #endif
+    }
+
+    LOG_I(MAC, "Frame %d: Contention resolution timer %d/%ld\n", 
+      tx_frame,
+      mac->RA_contention_resolution_cnt,
+      ((1 + rach_ConfigCommon->ra_ContentionResolutionTimer) << 3));
+      mac->RA_contention_resolution_cnt++;
+
+    if (mac->RA_contention_resolution_cnt == ((1 + rach_ConfigCommon->ra_ContentionResolutionTimer) << 3)) {
+      mac->t_crnti = 0;
+      mac->RA_active = 0;
+      mac->RA_contention_resolution_timer_active = 0;
+      // Signal PHY to quit RA procedure
+      LOG_E(MAC, "[UE %u] [RAPROC] Contention resolution timer expired, RA failed, discarded TC-RNTI\n", module_id);
+      nr_ra_failed(module_id, cc_id, gNB_index);
+    }
+  }
+}
+
 #if 0
 uint16_t nr_dci_format_size (PHY_VARS_NR_UE *ue,
                              uint8_t slot,
-- 
2.26.2