From dcf25f35c7f27c53905aeaaa55e3dbde5999f488 Mon Sep 17 00:00:00 2001
From: Srushti <srushtipatil@iisc.ac.in>
Date: Fri, 21 Jun 2019 11:11:42 +0200
Subject: [PATCH] added PRACH UE Procedures into prachsim

---
 cmake_targets/build_oai                     |   2 +-
 openair1/PHY/defs_nr_UE.h                   |   1 +
 openair1/PHY/defs_nr_common.h               |   1 +
 openair1/SCHED_NR_UE/phy_procedures_nr_ue.c | 190 ++++++++++++++++++++
 openair1/SIMULATION/NR_PHY/prachsim.c       |  12 +-
 5 files changed, 201 insertions(+), 5 deletions(-)

diff --git a/cmake_targets/build_oai b/cmake_targets/build_oai
index c03d1780676..355bf090f3e 100755
--- a/cmake_targets/build_oai
+++ b/cmake_targets/build_oai
@@ -684,7 +684,7 @@ function main() {
     echo_info "Compiling unitary tests simulators"
     # TODO: fix: dlsim_tm4 pucchsim prachsim pdcchsim pbchsim mbmssim
     #simlist="dlsim_tm4 dlsim ulsim pucchsim prachsim pdcchsim pbchsim mbmssim"
-    simlist="nr_pucchsim dlsim ulsim polartest ldpctest smallblocktest nr_pbchsim nr_dlschsim nr_dlsim nr_ulschsim nr_prachsim"
+    simlist="nr_prachsim nr_pucchsim dlsim ulsim polartest ldpctest smallblocktest nr_pbchsim nr_dlschsim nr_dlsim nr_ulschsim "
     for f in $simlist ; do
       compilations \
       phy_simulators $f \
diff --git a/openair1/PHY/defs_nr_UE.h b/openair1/PHY/defs_nr_UE.h
index cb2200898c4..dd7127a8e27 100644
--- a/openair1/PHY/defs_nr_UE.h
+++ b/openair1/PHY/defs_nr_UE.h
@@ -1035,6 +1035,7 @@ typedef struct {
   int current_dlsch_cqi[NUMBER_OF_CONNECTED_eNB_MAX];
   unsigned char first_run_timing_advance[NUMBER_OF_CONNECTED_eNB_MAX];
   uint8_t               generate_prach;
+  uint8_t               generate_nr_prach;
   uint8_t               prach_cnt;
   uint8_t               prach_PreambleIndex;
   //  uint8_t               prach_timer;
diff --git a/openair1/PHY/defs_nr_common.h b/openair1/PHY/defs_nr_common.h
index ef41afe58a6..6f3d45a4c5b 100644
--- a/openair1/PHY/defs_nr_common.h
+++ b/openair1/PHY/defs_nr_common.h
@@ -242,6 +242,7 @@ typedef struct NR_DL_FRAME_PARMS {
   ///  total Number of Resource Block Groups: this is ceil(N_PRB/P)
   /// Frame type (0 FDD, 1 TDD)
   lte_frame_type_t frame_type;
+  uint8_t tdd_config;
   /// Cell ID
   uint16_t Nid_cell;
   /// subcarrier spacing (15,30,60,120)
diff --git a/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c b/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
index 57c48127b84..cec59ead510 100644
--- a/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
+++ b/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
@@ -4798,3 +4798,193 @@ uint8_t is_ri_TXOp(PHY_VARS_NR_UE *ue,UE_nr_rxtx_proc_t *proc,uint8_t gNB_id)
   else
     return(0);
 }
+
+void nr_ue_prach_procedures(PHY_VARS_NR_UE *ue,UE_nr_rxtx_proc_t *proc,uint8_t eNB_id,uint8_t abstraction_flag,runmode_t mode) {
+
+  int frame_tx = proc->frame_tx;
+  int nr_tti_tx = proc->nr_tti_tx;
+  int prach_power;
+  uint16_t preamble_tx=50;
+  PRACH_RESOURCES_t prach_resources;
+
+  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_UE_TX_PRACH, VCD_FUNCTION_IN);
+
+  ue->generate_nr_prach=0;
+ if (ue->mac_enabled==0){
+    ue->prach_resources[eNB_id] = &prach_resources;
+ ue->prach_resources[eNB_id]->ra_PreambleIndex = preamble_tx;
+  ue->prach_resources[eNB_id]->ra_TDD_map_index = 0;
+ }
+
+  if (ue->mac_enabled==1){
+
+    // ask L2 for RACH transport
+    if ((mode != rx_calib_ue) && (mode != rx_calib_ue_med) && (mode != rx_calib_ue_byp) && (mode != no_L2_connect) ) {
+      LOG_D(PHY,"Getting PRACH resources\n");
+      //ue->prach_resources[eNB_id] = mac_xface->ue_get_rach(ue->Mod_id,ue->CC_id,frame_tx,eNB_id,nr_tti_tx);   
+   // LOG_D(PHY,"Got prach_resources for eNB %d address %p, RRCCommon %p\n",eNB_id,ue->prach_resources[eNB_id],UE_mac_inst[ue->Mod_id].radioResourceConfigCommon);  
+   // LOG_D(PHY,"Prach resources %p\n",ue->prach_resources[eNB_id]);
+  }
+}
+
+if (ue->prach_resources[eNB_id]!=NULL) {
+
+  ue->generate_nr_prach=1;
+  ue->prach_cnt=0;
+#ifdef SMBV
+ue->prach_resources[eNB_id]->ra_PreambleIndex = preamble_tx;
+#endif
+
+#ifdef OAI_EMU
+  ue->prach_PreambleIndex=ue->prach_resources[eNB_id]->ra_PreambleIndex;
+#endif
+
+  if (abstraction_flag == 0) {
+
+    LOG_I(PHY,"mode %d\n",mode);
+
+    if ((ue->mac_enabled==1) && (mode != calib_prach_tx)) {
+
+      ue->tx_power_dBm[nr_tti_tx] = ue->prach_resources[eNB_id]->ra_PREAMBLE_RECEIVED_TARGET_POWER+get_nr_PL(ue,eNB_id);
+    }
+    else {
+      ue->tx_power_dBm[nr_tti_tx] = ue->tx_power_max_dBm;
+      ue->prach_resources[eNB_id]->ra_PreambleIndex = preamble_tx; 
+    }
+
+   LOG_I(PHY,"[UE  %d][RAPROC] Frame %d, nr_tti_rx %d : Generating PRACH, preamble %d,PL %d,  P0_PRACH %d, TARGET_RECEIVED_POWER %d dBm, PRACH TDD Resource index %d, RA-RNTI %d\n",
+	  ue->Mod_id,
+	  frame_tx,
+	  nr_tti_tx,
+	  ue->prach_resources[eNB_id]->ra_PreambleIndex,
+	  get_nr_PL(ue,eNB_id),
+	  ue->tx_power_dBm[nr_tti_tx],
+	  ue->prach_resources[eNB_id]->ra_PREAMBLE_RECEIVED_TARGET_POWER,
+	  ue->prach_resources[eNB_id]->ra_TDD_map_index,
+	  ue->prach_resources[eNB_id]->ra_RNTI);
+
+    ue->tx_total_RE[nr_tti_tx] = 96;
+
+#if defined(EXMIMO) || defined(OAI_USRP) || defined(OAI_BLADERF) || defined(OAI_LMSSDR) || defined(OAI_ADRV9371_ZC706)
+    ue->prach_vars[eNB_id]->amp = get_tx_amp(ue->tx_power_dBm[nr_tti_tx],
+					     ue->tx_power_max_dBm,
+					     ue->frame_parms.N_RB_UL,
+					     6);
+#else
+   ue->prach_vars[eNB_id]->amp = AMP;
+#endif
+   if ((mode == calib_prach_tx) && (((proc->frame_tx&0xfffe)%100)==0))
+      LOG_D(PHY,"[UE  %d][RAPROC] Frame %d, nr_tti_rx %d : PRACH TX power %d dBm, amp %d\n",
+	    ue->Mod_id,
+	    proc->frame_rx,
+	    proc->nr_tti_tx,
+	    ue->tx_power_dBm[nr_tti_tx],
+	    ue->prach_vars[eNB_id]->amp);
+
+
+   //       start_meas(&ue->tx_prach);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_GENERATE_PRACH, VCD_FUNCTION_IN);
+
+//    prach_power = generate_nr_prach(ue,eNB_id,nr_tti_tx,frame_tx);
+prach_power = generate_nr_prach(ue,0,9,0); //subframe number hardcoded according to the simulator
+
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_GENERATE_PRACH, VCD_FUNCTION_OUT);
+    //      stop_meas(&ue->tx_prach);
+    LOG_D(PHY,"[UE  %d][RAPROC] PRACH PL %d dB, power %d dBm, digital power %d dB (amp %d)\n",
+	  ue->Mod_id,
+	  get_nr_PL(ue,eNB_id),
+	  ue->tx_power_dBm[nr_tti_tx],
+	  dB_fixed(prach_power),
+	  ue->prach_vars[eNB_id]->amp);
+  }/* else {
+    UE_transport_info[ue->Mod_id][ue->CC_id].cntl.prach_flag=1;
+    UE_transport_info[ue->Mod_id][ue->CC_id].cntl.prach_id=ue->prach_resources[eNB_id]->ra_PreambleIndex;
+  }*/ // commented for compiling as abstraction flag is 0
+
+  if (ue->mac_enabled==1){
+    //mac_xface->Msg1_transmitted(ue->Mod_id,ue->CC_id,frame_tx,eNB_id);
+ }
+
+LOG_I(PHY,"[UE  %d][RAPROC] Frame %d, nr_tti_rx %d: Generating PRACH (eNB %d) preamble index %d for UL, TX power %d dBm (PL %d dB), l3msg \n",
+      ue->Mod_id,frame_tx,nr_tti_tx,eNB_id,
+      ue->prach_resources[eNB_id]->ra_PreambleIndex,
+      ue->prach_resources[eNB_id]->ra_PREAMBLE_RECEIVED_TARGET_POWER+get_nr_PL(ue,eNB_id),
+      get_nr_PL(ue,eNB_id));
+
+}
+
+
+// if we're calibrating the PRACH kill the pointer to its resources so that the RA protocol doesn't continue
+if (mode == calib_prach_tx)
+  ue->prach_resources[eNB_id]=NULL;
+
+LOG_D(PHY,"[UE %d] frame %d nr_tti_rx %d : generate_nr_prach %d, prach_cnt %d\n",
+      ue->Mod_id,frame_tx,nr_tti_tx,ue->generate_nr_prach,ue->prach_cnt);
+
+ue->prach_cnt++;
+
+if (ue->prach_cnt==3)
+  ue->generate_nr_prach=0;
+
+VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_UE_TX_PRACH, VCD_FUNCTION_OUT);
+}
+
+
+//Yet to be done
+/* void phy_procedures_nrUE_TX(PHY_VARS_NR_UE *ue,UE_nr_rxtx_proc_t *proc,uint8_t eNB_id,uint8_t abstraction_flag,runmode_t mode,relaying_type_t r_type) { 
+
+  NR_DL_FRAME_PARMS *frame_parms=&ue->frame_parms;
+  int32_t ulsch_start=0;
+  int nr_tti_tx = proc->nr_tti_tx;
+  int frame_tx = proc->frame_tx;
+  unsigned int aa;
+  uint8_t isSubframeSRS;
+
+  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_UE_TX,VCD_FUNCTION_IN);
+
+  LOG_D(PHY,"****** start TX-Chain for AbsSubframe %d.%d ******\n", frame_tx, nr_tti_tx);
+  ue->generate_ul_signal[eNB_id] = 0;
+
+
+#if UE_TIMING_TRACE
+  start_meas(&ue->phy_proc_tx);
+#endif
+
+  ue->tx_power_dBm[nr_tti_tx]=-127;
+printf("ue mode =%d\n",ue->UE_mode[eNB_id]);
+
+  if (abstraction_flag==0) {
+    for (aa=0; aa<frame_parms->nb_antennas_tx; aa++) {
+      memset(&ue->common_vars.txdataF[aa][nr_tti_tx*frame_parms->ofdm_symbol_size*frame_parms->symbols_per_tti],0,frame_parms->ofdm_symbol_size*frame_parms->symbols_per_tti*sizeof(int32_t));
+    }
+  } 
+
+if (abstraction_flag == 0) {
+    ulsch_common_procedures(ue,proc, (ue->generate_ul_signal[eNB_id] == 0));
+  } 
+
+ue->UE_mode[eNB_id]=1;
+ue->frame_parms.prach_config_common.prach_Config_enabled=1;
+printf("S: ue->UE_mode[eNB_id] == PRACH is %d and ue->frame_parms.prach_config_common.prach_Config_enabled is %d\n", ue->UE_mode[eNB_id],ue->frame_parms.prach_config_common.prach_Config_enabled);
+  if ((ue->UE_mode[eNB_id] == PRACH) &&
+     (ue->frame_parms.prach_config_common.prach_Config_enabled==1)) { 
+
+    // check if we have PRACH opportunity
+    if (is_nr_prach_subframe(&ue->frame_parms,frame_tx,nr_tti_tx)) {
+printf("the value of is_nr_prach subframe is %d\n",is_nr_prach_subframe(&ue->frame_parms,frame_tx,nr_tti_tx));
+      nr_ue_prach_procedures(ue,proc,eNB_id,abstraction_flag,mode);
+    }
+  } // mode is PRACH
+  else {
+    ue->generate_prach=0;
+  }
+
+LOG_D(PHY,"****** end TX-Chain for AbsSubframe %d.%d ******\n", frame_tx, nr_tti_tx);
+
+ VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_UE_TX, VCD_FUNCTION_OUT);
+#if UE_TIMING_TRACE
+  stop_meas(&ue->phy_proc_tx);
+#endif
+} */
+
+
diff --git a/openair1/SIMULATION/NR_PHY/prachsim.c b/openair1/SIMULATION/NR_PHY/prachsim.c
index f2296128c67..f3a83870d6e 100644
--- a/openair1/SIMULATION/NR_PHY/prachsim.c
+++ b/openair1/SIMULATION/NR_PHY/prachsim.c
@@ -114,7 +114,7 @@ int main(int argc, char **argv)
   int N_RB_UL=273;
   uint32_t prach_errors=0;
   uint8_t subframe=9;
-  uint16_t preamble_energy_list[64],preamble_tx=99,preamble_delay_list[64];
+  uint16_t preamble_energy_list[64],preamble_tx=50,preamble_delay_list[64];
   uint16_t preamble_max,preamble_energy_max;
   PRACH_RESOURCES_t prach_resources;
   //uint8_t prach_fmt;
@@ -514,10 +514,14 @@ int main(int argc, char **argv)
   UE->prach_resources[0]->ra_PreambleIndex = preamble_tx;
   UE->prach_resources[0]->ra_TDD_map_index = 0;
 
-  tx_lev = generate_nr_prach(UE,
+  /*tx_lev = generate_nr_prach(UE,
 			     0, //gNB_id,
 			     subframe,
-			     0); //Nf
+			     0); //Nf */ //commented for testing purpose
+
+UE_nr_rxtx_proc_t proc={0};
+nr_ue_prach_procedures(UE,&proc,0,0,0);
+
 
   /* tx_lev_dB not used later, no need to set */
   //tx_lev_dB = (unsigned int) dB_fixed(tx_lev);
@@ -603,7 +607,7 @@ int main(int argc, char **argv)
         if (preamble_rx!=preamble_tx)
           prach_errors++;
         else {
-          delay_avg += (double)preamble_delay_list[preamble_max];
+          delay_avg += (double)preamble_delay_list[preamble_tx];
         }
 
         if (n_frames==1) {
-- 
GitLab