From 4eed33b0824a4573f7d4c61dd17c95d83765b6c1 Mon Sep 17 00:00:00 2001
From: rmagueta <rmagueta@allbesmart.pt>
Date: Wed, 9 Dec 2020 14:59:55 +0000
Subject: [PATCH] Added documentation. Minor source code format fixes.

---
 doc/FEATURE_SET.md                            | 16 +++++----
 doc/RUNMODEM.md                               | 26 ++++++++++++++
 openair1/SCHED_NR_UE/phy_procedures_nr_ue.c   | 36 ++++++++++---------
 .../gnb.sa.band78.fr1.106PRB.usrpb210.conf    |  1 -
 4 files changed, 56 insertions(+), 23 deletions(-)

diff --git a/doc/FEATURE_SET.md b/doc/FEATURE_SET.md
index 1f8d8cfccf..2840eed73c 100644
--- a/doc/FEATURE_SET.md
+++ b/doc/FEATURE_SET.md
@@ -295,6 +295,7 @@ The following features are valid for the gNB and the 5G-NR UE.
 - NR RRC (38.331) Rel 15 messages using new asn1c 
 - LTE RRC (36.331) also updated to Rel 15 
 - Generation of CellGroupConfig (for eNB) and MIB
+- Generation of system information block 1 (SIB1)
 - Application to read configuration file and program gNB RRC
 - RRC can configure PDCP, RLC, MAC
 
@@ -305,7 +306,8 @@ The following features are valid for the gNB and the 5G-NR UE.
 **gNB MAC**
 - MAC -> PHY configuration using NR FAPI P5 interface
 - MAC <-> PHY data interface using FAPI P7 interface for BCH PDU, DCI PDU, PDSCH PDU
-- Scheduler for RA procedreat gNB
+- Scheduler procedures for SIB1
+- Scheduler procedures for RA
 - MAC downlink scheduler (fixed allocations)
 - MAC header generation (including timing advance)
 - ACK / NACK handling and HARQ procedures for downlink
@@ -350,11 +352,13 @@ The following features are valid for the gNB and the 5G-NR UE.
 
 ## UE Higher Layers ##
 
-**gNB MAC**
-- Initial sync and MIB detection
-- MAC -> PHY configuration of PHY via UE FAPI P5 interface
-- Basic MAC to control PHY via UE FAPI P7 interface
-- Random access procedure
+**UE MAC**
+*  Minimum system information (MSI)
+    - Initial sync and MIB detection
+    - System information block 1 (SIB1) reception
+*  MAC -> PHY configuration of PHY via UE FAPI P5 interface
+*  Basic MAC to control PHY via UE FAPI P7 interface
+*  Random access procedure
 
 
 **RLC**
diff --git a/doc/RUNMODEM.md b/doc/RUNMODEM.md
index af4a57041f..9f4564a0ca 100644
--- a/doc/RUNMODEM.md
+++ b/doc/RUNMODEM.md
@@ -132,6 +132,32 @@ With the RF simulator (on the same machine):
 
 `sudo RFSIMULATOR=127.0.0.1 ./nr-uesoftmodem --do-ra --rfsim --parallel-config PARALLEL_SINGLE_THREAD`
 
+## sa setup with OAI
+
+The sa flag is used to run gNB in standalone mode. Currently OAI in NR standalone mode transmits and receives SIB1.
+
+In order to run gNB in standalone mode, the following flag is needed at gNB:
+
+`--sa`
+
+### Run OAI in sa mode
+
+From the `cmake_targets/ran_build/build` folder:
+
+gNB on machine 1:
+
+`sudo ./nr-softmodem -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf --sa`
+
+UE on machine 2:
+
+`sudo ./nr-uesoftmodem --rrc_config_path . --phy-test`
+
+With the RF simulator (on the same machine):
+
+`sudo ./nr-softmodem -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf --rfsim --sa`
+
+`sudo ./nr-uesoftmodem --rrc_config_path . --rfsim --phy-test`
+
 ## IF setup with OAI
 
 The -C and -CO flags can be used together at UE side to set custom downlink and uplink FR1 intermediate frequencies for the IF equipment.
diff --git a/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c b/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
index a7c01915da..ac559aa8dd 100644
--- a/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
+++ b/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
@@ -725,14 +725,12 @@ int nr_ue_pdsch_procedures(PHY_VARS_NR_UE *ue, UE_nr_rxtx_proc_t *proc, int eNB_
 
   if (!dlsch1)  {
     int harq_pid = dlsch0->current_harq_pid;
-    uint16_t BWPStart       = dlsch0->harq_processes[harq_pid]->BWPStart;
-    //    uint16_t BWPSize        = dlsch0->harq_processes[harq_pid]->BWPSize;
-    uint16_t pdsch_start_rb = dlsch0->harq_processes[harq_pid]->start_rb;
-    uint16_t pdsch_nb_rb    = dlsch0->harq_processes[harq_pid]->nb_rb;
-    uint16_t s0             = dlsch0->harq_processes[harq_pid]->start_symbol;
-    uint16_t s1             = dlsch0->harq_processes[harq_pid]->nb_symbols;
     NR_DL_UE_HARQ_t *dlsch0_harq = dlsch0->harq_processes[harq_pid];
-    uint16_t nb_rb = dlsch0_harq->nb_rb/ue->frame_parms.nb_antennas_rx;
+    uint16_t BWPStart       = dlsch0_harq->BWPStart;
+    uint16_t pdsch_start_rb = dlsch0_harq->start_rb;
+    uint16_t pdsch_nb_rb    = dlsch0_harq->nb_rb;
+    uint16_t s0             = dlsch0_harq->start_symbol;
+    uint16_t s1             = dlsch0_harq->nb_symbols;
 
     LOG_D(PHY,"[UE %d] PDSCH type %d active in nr_slot_rx %d, harq_pid %d (%d), rb_start %d, nb_rb %d, symbol_start %d, nb_symbols %d, DMRS mask %x\n",ue->Mod_id,pdsch,nr_slot_rx,harq_pid,dlsch0->harq_processes[harq_pid]->status,pdsch_start_rb,pdsch_nb_rb,s0,s1,dlsch0->harq_processes[harq_pid]->dlDmrsSymbPos);
 
@@ -766,10 +764,15 @@ int nr_ue_pdsch_procedures(PHY_VARS_NR_UE *ue, UE_nr_rxtx_proc_t *proc, int eNB_
     }
 
     uint16_t first_symbol_with_data = s0;
-    uint8_t pilots = ((1<<s0)&dlsch0_harq->dlDmrsSymbPos)>0 ? 1 : 0;
-    uint32_t len = (pilots==1)? ((ue->dmrs_DownlinkConfig.pdsch_dmrs_type==pdsch_dmrs_type1)?nb_rb*(12-6*dlsch0_harq->n_dmrs_cdm_groups): nb_rb*(12-4*dlsch0_harq->n_dmrs_cdm_groups)):(nb_rb*12);
-    if(len == 0) {
-      first_symbol_with_data = first_symbol_with_data + 1; // TODO: Replace "1" by dmrs duration
+    uint32_t dmrs_data_re;
+
+    if (ue->dmrs_DownlinkConfig.pdsch_dmrs_type == pdsch_dmrs_type1)
+      dmrs_data_re = 12 - 6 * dlsch0_harq->n_dmrs_cdm_groups;
+    else
+      dmrs_data_re = 12 - 4 * dlsch0_harq->n_dmrs_cdm_groups;
+
+    while ((dmrs_data_re == 0) && (dlsch0_harq->dlDmrsSymbPos & (1 << first_symbol_with_data))) {
+      first_symbol_with_data++;
     }
 
     for (m = s0; m < (s1 + s0); m++) {
@@ -778,9 +781,10 @@ int nr_ue_pdsch_procedures(PHY_VARS_NR_UE *ue, UE_nr_rxtx_proc_t *proc, int eNB_
       eNB_id_i = eNB_id+1;
       i_mod = 0;
       if (( m==first_symbol_with_data ) && (m<4))
-	first_symbol_flag = 1;
+	      first_symbol_flag = 1;
       else
-	first_symbol_flag = 0;
+	      first_symbol_flag = 0;
+
 #if UE_TIMING_TRACE
       uint8_t slot = 0;
       if(m >= ue->frame_parms.symbols_per_slot>>1)
@@ -805,8 +809,8 @@ int nr_ue_pdsch_procedures(PHY_VARS_NR_UE *ue, UE_nr_rxtx_proc_t *proc, int eNB_
                       return -1;
       }
       else { // This is to adjust the llr offset in the case of skipping over a dmrs symbol (i.e. in case of no PDSCH REs in DMRS)
-	if      (pdsch == RA_PDSCH) ue->pdsch_vars[proc->thread_id][eNB_id]->llr_offset[m]=ue->pdsch_vars[proc->thread_id][eNB_id]->llr_offset[m-1];
-  else if (pdsch == PDSCH || pdsch == SI_PDSCH) {
+	      if (pdsch == RA_PDSCH) ue->pdsch_vars[proc->thread_id][eNB_id]->llr_offset[m]=ue->pdsch_vars[proc->thread_id][eNB_id]->llr_offset[m-1];
+        else if (pdsch == PDSCH || pdsch == SI_PDSCH) {
           if (nr_rx_pdsch(ue,
                     proc,
                     pdsch,
@@ -821,7 +825,7 @@ int nr_ue_pdsch_procedures(PHY_VARS_NR_UE *ue, UE_nr_rxtx_proc_t *proc, int eNB_
                     dlsch0->current_harq_pid) < 0)
                       return -1;
         }
-	else AssertFatal(1==0,"Not RA_PDSCH, SI_PDSCH or PDSCH\n");
+	      else AssertFatal(1==0,"Not RA_PDSCH, SI_PDSCH or PDSCH\n");
       }
       if (pdsch == PDSCH)  LOG_D(PHY,"Done processing symbol %d : llr_offset %d\n",m,ue->pdsch_vars[proc->thread_id][eNB_id]->llr_offset[m]);
 #if UE_TIMING_TRACE
diff --git a/targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf b/targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf
index ce0505a4c0..3d4c3ff92e 100644
--- a/targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf
+++ b/targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf
@@ -274,7 +274,6 @@ RUs = (
          eNB_instances  = [0];
          #beamforming 1x4 matrix:
          bf_weights = [0x00007fff, 0x0000, 0x0000, 0x0000];
-         sdr_addrs = "addr=192.168.10.2,mgmt_addr=192.168.10.2,second_addr=192.168.20.2";
          clock_src = "internal";
     }
 );
-- 
2.26.2