From 65bc749ee675202a9bcb6fb005d537052a70f35d Mon Sep 17 00:00:00 2001
From: francescomani <email@francescomani.it>
Date: Thu, 5 Dec 2024 19:58:50 +0100
Subject: [PATCH] start the rar reception window in PRACH slots but take into
 account the additional slots difference wrt when we actually would need to
 start it (in first DCI occasion)

---
 openair2/LAYER2/NR_MAC_UE/mac_defs.h          |  2 +-
 openair2/LAYER2/NR_MAC_UE/mac_proto.h         |  1 +
 openair2/LAYER2/NR_MAC_UE/nr_ra_procedures.c  |  4 +---
 .../NR_MAC_UE/nr_ue_dci_configuration.c       | 13 ++++-------
 openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c   | 22 +++++++++++++++++--
 5 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/openair2/LAYER2/NR_MAC_UE/mac_defs.h b/openair2/LAYER2/NR_MAC_UE/mac_defs.h
index 95df46fb724..555a7b993c5 100644
--- a/openair2/LAYER2/NR_MAC_UE/mac_defs.h
+++ b/openair2/LAYER2/NR_MAC_UE/mac_defs.h
@@ -331,7 +331,7 @@ typedef struct {
   int ra_ssb;
   /// Random-access response window timer
   NR_timer_t response_window_timer;
-  bool start_response_window;
+  int response_window_setup_time;
   /// Random-access backoff timer
   NR_timer_t RA_backoff_timer;
   int RA_backoff_limit;
diff --git a/openair2/LAYER2/NR_MAC_UE/mac_proto.h b/openair2/LAYER2/NR_MAC_UE/mac_proto.h
index 38188d86c61..530ff952403 100644
--- a/openair2/LAYER2/NR_MAC_UE/mac_proto.h
+++ b/openair2/LAYER2/NR_MAC_UE/mac_proto.h
@@ -302,6 +302,7 @@ void configure_initial_pucch(PUCCH_sched_t *pucch, int res_ind);
 void release_PUCCH_SRS(NR_UE_MAC_INST_t *mac);
 void nr_ue_reset_sync_state(NR_UE_MAC_INST_t *mac);
 void nr_ue_send_synch_request(NR_UE_MAC_INST_t *mac, module_id_t module_id, int cc_id, const fapi_nr_synch_request_t *sync_req);
+bool is_ss_monitor_occasion(const int frame, const int slot, const int slots_per_frame, const NR_SearchSpace_t *ss);
 
 /**
  * @brief   Get UE sync state
diff --git a/openair2/LAYER2/NR_MAC_UE/nr_ra_procedures.c b/openair2/LAYER2/NR_MAC_UE/nr_ra_procedures.c
index 3a93c881027..04285e672a2 100644
--- a/openair2/LAYER2/NR_MAC_UE/nr_ra_procedures.c
+++ b/openair2/LAYER2/NR_MAC_UE/nr_ra_procedures.c
@@ -820,7 +820,7 @@ static void setup_ra_response_window(RA_config_t *ra,
     ta_Common_slots = (int)ceil(ta_Common_ms * slots_per_frame / 10);
   }
 
-  nr_timer_setup(&ra->response_window_timer, respwind_value + ta_Common_slots, 1);
+  ra->response_window_setup_time = respwind_value + ta_Common_slots;
 }
 
 // Random Access procedure initialization as per 5.1.1 and initialization of variables specific
@@ -1067,8 +1067,6 @@ bool init_RA(NR_UE_MAC_INST_t *mac, int frame)
                            rach_ConfigGeneric,
                            twostep_generic,
                            &mac->ntn_ta);
-  ra->start_response_window = false;
-
   return true;
 }
 
diff --git a/openair2/LAYER2/NR_MAC_UE/nr_ue_dci_configuration.c b/openair2/LAYER2/NR_MAC_UE/nr_ue_dci_configuration.c
index 237838e4dc9..3817e067593 100644
--- a/openair2/LAYER2/NR_MAC_UE/nr_ue_dci_configuration.c
+++ b/openair2/LAYER2/NR_MAC_UE/nr_ue_dci_configuration.c
@@ -560,18 +560,13 @@ void ue_dci_configuration(NR_UE_MAC_INST_t *mac, fapi_nr_dl_config_request_t *dl
       config_dci_pdu(mac, dl_config, TYPE_SI_RNTI_, slot, ss);
     }
   }
-  if (mac->state == UE_PERFORMING_RA && mac->ra.ra_state >= nrRA_WAIT_RAR) {
+  RA_config_t *ra = &mac->ra;
+  if (mac->state == UE_PERFORMING_RA && ra->ra_state >= nrRA_WAIT_RAR) {
     // if RA is ongoing use RA search space
     if (is_ss_monitor_occasion(frame, slot, slots_per_frame, pdcch_config->ra_SS)) {
-      // The RA response window starts at the first symbol of the earliest CORESET
-      // the UE is configured to receive PDCCH for Type1-PDCCH CSS set
-      if (mac->ra.start_response_window) {
-        nr_timer_start(&mac->ra.response_window_timer);
-        mac->ra.start_response_window = false;
-      }
       nr_rnti_type_t rnti_type = 0;
-      if (mac->ra.ra_type == RA_4_STEP) {
-        rnti_type = mac->ra.ra_state == nrRA_WAIT_RAR ? TYPE_RA_RNTI_ : TYPE_TC_RNTI_;
+      if (ra->ra_type == RA_4_STEP) {
+        rnti_type = ra->ra_state == nrRA_WAIT_RAR ? TYPE_RA_RNTI_ : TYPE_TC_RNTI_;
       } else {
         rnti_type = TYPE_MSGB_RNTI_;
       }
diff --git a/openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c b/openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c
index fc69b5e5175..af7e063481a 100644
--- a/openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c
+++ b/openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c
@@ -2180,13 +2180,31 @@ static void nr_ue_prach_scheduler(NR_UE_MAC_INST_t *mac, frame_t frameP, slot_t
       T(T_UE_PHY_INITIATE_RA_PROCEDURE, T_INT(frameP), T_INT(pdu->prach_config_pdu.prach_slot),
         T_INT(pdu->prach_config_pdu.ra_PreambleIndex), T_INT(pdu->prach_config_pdu.prach_tx_power));
 
+      const int n_slots_frame = mac->frame_structure.numb_slots_frame;
       if (ra->ra_type == RA_4_STEP) {
         ra->ra_state = nrRA_WAIT_RAR;
-        ra->start_response_window = true;
+        // we start to monitor DCI for RAR in the first valid occasion after transmitting RACH
+        // the response window timer should be started at that time
+        // but for processing reasons it is better to start it here and to add the slot difference
+        // that also takes into account the rx to tx slot offset
+        int next_slot = (slotP + 1) % n_slots_frame;
+        int next_frame = (frameP + (next_slot < slotP)) % MAX_FRAME_NUMBER;
+        int add_slots = 1;
+        NR_BWP_PDCCH_t *pdcch_config = &mac->config_BWP_PDCCH[mac->current_DL_BWP->bwp_id];
+        while (!is_dl_slot(next_slot, &mac->frame_structure)
+               || !is_ss_monitor_occasion(next_frame, next_slot, n_slots_frame, pdcch_config->ra_SS)) {
+          int temp_slot = (next_slot + 1) % n_slots_frame;
+          next_frame = (next_frame + (temp_slot < next_slot)) % MAX_FRAME_NUMBER;
+          next_slot = temp_slot;
+          add_slots++;
+        }
+        nr_timer_setup(&ra->response_window_timer,
+                       ra->response_window_setup_time + add_slots + GET_DURATION_RX_TO_TX(&mac->ntn_ta),
+                       1);
+        nr_timer_start(&ra->response_window_timer);
       } else if (ra->ra_type == RA_2_STEP) {
         NR_MsgA_PUSCH_Resource_r16_t *msgA_PUSCH_Resource =
             mac->current_UL_BWP->msgA_ConfigCommon_r16->msgA_PUSCH_Config_r16->msgA_PUSCH_ResourceGroupA_r16;
-        const int n_slots_frame = mac->frame_structure.numb_slots_frame;
         slot_t msgA_pusch_slot = (slotP + msgA_PUSCH_Resource->msgA_PUSCH_TimeDomainOffset_r16) % n_slots_frame;
         frame_t msgA_pusch_frame =
             (frameP + ((slotP + msgA_PUSCH_Resource->msgA_PUSCH_TimeDomainOffset_r16) / n_slots_frame)) % 1024;
-- 
GitLab