From ec10d48b243c881638caf1be2b3c99f359da4e5e Mon Sep 17 00:00:00 2001
From: Robert Schmidt <robert.schmidt@eurecom.fr>
Date: Thu, 26 Nov 2020 11:51:14 +0100
Subject: [PATCH] Reformat schedule_nr_prach()

---
 openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_RA.c | 125 ++++++++++--------
 1 file changed, 68 insertions(+), 57 deletions(-)

diff --git a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_RA.c b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_RA.c
index 6a432dc31f..df12b56281 100644
--- a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_RA.c
+++ b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_RA.c
@@ -195,15 +195,17 @@ void find_SSB_and_RO_available(module_id_t module_idP) {
 
 }		
 		
-void schedule_nr_prach(module_id_t module_idP, frame_t frameP, sub_frame_t slotP) {
-
+void schedule_nr_prach(module_id_t module_idP, frame_t frameP, sub_frame_t slotP)
+{
   gNB_MAC_INST *gNB = RC.nrmac[module_idP];
   NR_COMMON_channels_t *cc = gNB->common_channels;
   NR_ServingCellConfigCommon_t *scc = cc->ServingCellConfigCommon;
   nfapi_nr_ul_tti_request_t *UL_tti_req = &RC.nrmac[module_idP]->UL_tti_req_ahead[0][slotP];
   nfapi_nr_config_request_scf_t *cfg = &RC.nrmac[module_idP]->config[0];
 
-  if (is_nr_UL_slot(scc,slotP)) {
+  if (!is_nr_UL_slot(scc,slotP))
+    return;
+
   uint8_t config_index = scc->uplinkConfigCommon->initialUplinkBWP->rach_ConfigCommon->choice.setup->rach_ConfigGeneric.prach_ConfigurationIndex;
   uint8_t mu,N_dur,N_t_slot,start_symbol = 0,N_RA_slot;
   uint16_t RA_sfn_index = -1;
@@ -219,7 +221,7 @@ void schedule_nr_prach(module_id_t module_idP, frame_t frameP, sub_frame_t slotP
 
   uint8_t fdm = cfg->prach_config.num_prach_fd_occasions.value;
   // prach is scheduled according to configuration index and tables 6.3.3.2.2 to 6.3.3.2.4
-  if ( get_nr_prach_info_from_index(config_index,
+  if (!get_nr_prach_info_from_index(config_index,
                                     (int)frameP,
                                     (int)slotP,
                                     scc->downlinkConfigCommon->frequencyInfoDL->absoluteFrequencyPointA,
@@ -231,58 +233,68 @@ void schedule_nr_prach(module_id_t module_idP, frame_t frameP, sub_frame_t slotP
                                     &N_dur,
                                     &RA_sfn_index,
                                     &N_RA_slot,
-																		&config_period) ) {
-    uint16_t format0 = format&0xff;      // first column of format from table
-    uint16_t format1 = (format>>8)&0xff; // second column of format from table
-
-    if (N_RA_slot > 1) { //more than 1 PRACH slot in a subframe
-      if (slotP%2 == 1){
-	      slot_index = 1;
-      }	
-      else {
-	      slot_index = 0;
-			}	
-    }else if (N_RA_slot <= 1) { //1 PRACH slot in a subframe
-       slot_index = 0;
-    }
-
+																		&config_period) )
+    return;
 
-    AssertFatal(UL_tti_req->SFN == frameP && UL_tti_req->Slot == slotP,
-                "%d.%d UL_tti_req frame.slot %d.%d does not match PRACH %d.%d\n",
-                frameP, slotP,
-                UL_tti_req->SFN,
-                UL_tti_req->Slot,
-                frameP, slotP);
-    for (int fdm_index=0; fdm_index < fdm; fdm_index++) { // one structure per frequency domain occasion
-    for (int td_index=0; td_index<N_t_slot; td_index++) {
+  uint16_t format0 = format&0xff;      // first column of format from table
+  uint16_t format1 = (format>>8)&0xff; // second column of format from table
 
-      prach_occasion_id = (((frameP % (cc->max_association_period * config_period))/config_period) * cc->total_prach_occasions_per_config_period) + (RA_sfn_index + slot_index) * N_t_slot * fdm + td_index * fdm + fdm_index;
-			if((prach_occasion_id < cc->total_prach_occasions) && (td_index == 0)){  
+  if (N_RA_slot > 1) { // more than 1 PRACH slot in a subframe
+    slot_index = slotP % 2 == 1;
+  } else if (N_RA_slot <= 1) { // 1 PRACH slot in a subframe
+    slot_index = 0;
+  }
 
-      UL_tti_req->pdus_list[UL_tti_req->n_pdus].pdu_type = NFAPI_NR_UL_CONFIG_PRACH_PDU_TYPE;
-      UL_tti_req->pdus_list[UL_tti_req->n_pdus].pdu_size = sizeof(nfapi_nr_prach_pdu_t);
-      nfapi_nr_prach_pdu_t  *prach_pdu = &UL_tti_req->pdus_list[UL_tti_req->n_pdus].prach_pdu;
-      memset(prach_pdu,0,sizeof(nfapi_nr_prach_pdu_t));
-      UL_tti_req->n_pdus+=1;
+  AssertFatal(UL_tti_req->SFN == frameP && UL_tti_req->Slot == slotP,
+              "%d.%d UL_tti_req frame.slot %d.%d does not match PRACH %d.%d\n",
+              frameP, slotP,
+              UL_tti_req->SFN,
+              UL_tti_req->Slot,
+              frameP, slotP);
+
+  for (int fdm_index=0; fdm_index < fdm; fdm_index++) { // one structure per frequency domain occasion
+    for (int td_index = 0; td_index < N_t_slot; td_index++) {
+      prach_occasion_id = (((frameP % (cc->max_association_period * config_period)) / config_period)
+                            * cc->total_prach_occasions_per_config_period)
+                          + (RA_sfn_index + slot_index) * N_t_slot * fdm + td_index * fdm + fdm_index;
+      if (!((prach_occasion_id < cc->total_prach_occasions) && (td_index == 0)))
+        continue;
+
+      UL_tti_req->pdus_list[UL_tti_req->n_pdus].pdu_type =
+          NFAPI_NR_UL_CONFIG_PRACH_PDU_TYPE;
+      UL_tti_req->pdus_list[UL_tti_req->n_pdus].pdu_size =
+          sizeof(nfapi_nr_prach_pdu_t);
+      nfapi_nr_prach_pdu_t *prach_pdu =
+          &UL_tti_req->pdus_list[UL_tti_req->n_pdus].prach_pdu;
+      memset(prach_pdu, 0, sizeof(nfapi_nr_prach_pdu_t));
+      UL_tti_req->n_pdus += 1;
 
       // filling the prach fapi structure
       prach_pdu->phys_cell_id = *scc->physCellId;
       prach_pdu->num_prach_ocas = N_t_slot;
       prach_pdu->prach_start_symbol = start_symbol;
       prach_pdu->num_ra = fdm_index;
-      prach_pdu->num_cs = get_NCS(scc->uplinkConfigCommon->initialUplinkBWP->rach_ConfigCommon->choice.setup->rach_ConfigGeneric.zeroCorrelationZoneConfig,
-                                  format0,
-                                  scc->uplinkConfigCommon->initialUplinkBWP->rach_ConfigCommon->choice.setup->restrictedSetConfig);
-      
-      LOG_D(MAC, "Frame %d, Slot %d: Prach Occasion id = %u  fdm index = %u start symbol = %u slot index = %u subframe index = %u \n",
-	    frameP, slotP,
-	    prach_occasion_id, prach_pdu->num_ra,
-	    prach_pdu->prach_start_symbol,
-	    slot_index, RA_sfn_index);
+      prach_pdu->num_cs = get_NCS(
+          scc->uplinkConfigCommon->initialUplinkBWP->rach_ConfigCommon->choice
+              .setup->rach_ConfigGeneric.zeroCorrelationZoneConfig,
+          format0,
+          scc->uplinkConfigCommon->initialUplinkBWP->rach_ConfigCommon->choice
+              .setup->restrictedSetConfig);
+
+      LOG_D(MAC,
+            "Frame %d, Slot %d: Prach Occasion id = %u  fdm index = %u start "
+            "symbol = %u slot index = %u subframe index = %u \n",
+            frameP,
+            slotP,
+            prach_occasion_id,
+            prach_pdu->num_ra,
+            prach_pdu->prach_start_symbol,
+            slot_index,
+            RA_sfn_index);
       // SCF PRACH PDU format field does not consider A1/B1 etc. possibilities
       // We added 9 = A1/B1 10 = A2/B2 11 A3/B3
-      if (format1!=0xff) {
-        switch(format0) {
+      if (format1 != 0xff) {
+        switch (format0) {
           case 0xa1:
             prach_pdu->prach_format = 11;
             break;
@@ -292,12 +304,13 @@ void schedule_nr_prach(module_id_t module_idP, frame_t frameP, sub_frame_t slotP
           case 0xa3:
             prach_pdu->prach_format = 13;
             break;
-        default:
-          AssertFatal(1==0,"Only formats A1/B1 A2/B2 A3/B3 are valid for dual format");
+          default:
+            AssertFatal(
+                1 == 0,
+                "Only formats A1/B1 A2/B2 A3/B3 are valid for dual format");
         }
-      }
-      else{
-        switch(format0) {
+      } else {
+        switch (format0) {
           case 0:
             prach_pdu->prach_format = 0;
             break;
@@ -331,10 +344,10 @@ void schedule_nr_prach(module_id_t module_idP, frame_t frameP, sub_frame_t slotP
           case 0xc2:
             prach_pdu->prach_format = 10;
             break;
-        default:
-          AssertFatal(1==0,"Invalid PRACH format");
+          default:
+            AssertFatal(1 == 0, "Invalid PRACH format");
         }
-      }		
+      }
       const int start_rb = cfg->prach_config.num_prach_fd_occasions_list[fdm_index].k1.value;
       const int pusch_mu = scc->uplinkConfigCommon->frequencyInfoUL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing;
       const int num_rb = get_N_RA_RB(cfg->prach_config.prach_sub_c_spacing.value, pusch_mu);
@@ -343,16 +356,14 @@ void schedule_nr_prach(module_id_t module_idP, frame_t frameP, sub_frame_t slotP
       const uint16_t symb_mask = ((1 << N_dur) - 1) << start_symbol;
       for (int i = start_rb; i < start_rb + num_rb; ++i) {
         AssertFatal((vrb_map_UL[i] & symb_mask) == 0,
-                    "cannot reserve resources for PRACH: at RB %d, vrb_map_UL %x for symbols %x!\n",
+                    "cannot reserve resources for PRACH: at RB %d, "
+                    "vrb_map_UL %x for symbols %x!\n",
                     i,
                     vrb_map_UL[i],
                     symb_mask);
         vrb_map_UL[i] |= symb_mask;
       }
-     }
     }
-   }
-  }
   }
 }
 
-- 
2.26.2