From dc9b0fa53a68a71b926dddf19b3e52c95be6dfef Mon Sep 17 00:00:00 2001
From: Rajeev Gangula <gangula@eurecom.fr>
Date: Wed, 9 May 2018 15:55:36 +0200
Subject: [PATCH] Added uplink channels SNR in config file.

---
 openair1/SCHED/phy_procedures_lte_eNb.c   | 10 +++++-----
 openair2/ENB_APP/MACRLC_paramdef.h        |  7 ++++++-
 openair2/ENB_APP/enb_config.c             |  2 ++
 openair2/LAYER2/MAC/defs.h                |  3 +++
 openair2/LAYER2/MAC/eNB_scheduler.c       |  4 ++--
 openair2/LAYER2/MAC/eNB_scheduler_dlsch.c | 10 ++++++----
 openair2/LAYER2/MAC/eNB_scheduler_ulsch.c | 12 +++++++-----
 openair2/LAYER2/MAC/main.c                | 14 +++++++++-----
 8 files changed, 40 insertions(+), 22 deletions(-)

diff --git a/openair1/SCHED/phy_procedures_lte_eNb.c b/openair1/SCHED/phy_procedures_lte_eNb.c
index ecdf8e5756..07a03699b2 100644
--- a/openair1/SCHED/phy_procedures_lte_eNb.c
+++ b/openair1/SCHED/phy_procedures_lte_eNb.c
@@ -643,7 +643,7 @@ void fill_sr_indication(PHY_VARS_eNB *eNB,uint16_t rnti,int frame,int subframe,u
   pdu->rx_ue_information.tl.tag                       = NFAPI_RX_UE_INFORMATION_TAG;
   pdu->rx_ue_information.rnti                         = rnti;
 
-  int SNRtimes10 = dB_fixed_times10(stat) - 200;//(10*eNB->measurements.n0_power_dB[0]);
+  int SNRtimes10 = dB_fixed_times10(stat) - 300;//(10*eNB->measurements.n0_power_dB[0]);
 
 
   pdu->ul_cqi_information.tl.tag = NFAPI_UL_CQI_INFORMATION_TAG;
@@ -1240,8 +1240,8 @@ void pusch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc)
             ulsch->rnti,
             dB_fixed(eNB->pusch_vars[i]->ulsch_power[0]),
             dB_fixed(eNB->pusch_vars[i]->ulsch_power[1]),
-            20,//eNB->measurements.n0_power_dB[0],
-            20,//eNB->measurements.n0_power_dB[1],
+            30,//eNB->measurements.n0_power_dB[0],
+            30,//eNB->measurements.n0_power_dB[1],
             ulsch_harq->o_ACK[0],
             ulsch_harq->o_ACK[1],
             ret,
@@ -1473,7 +1473,7 @@ void fill_rx_indication(PHY_VARS_eNB *eNB,int UE_id,int frame,int subframe)
   pdu->rx_indication_rel8.timing_advance = timing_advance_update;
 
   // estimate UL_CQI for MAC (from antenna port 0 only)
-  int SNRtimes10 = dB_fixed_times10(eNB->pusch_vars[UE_id]->ulsch_power[0]) - 200;//(10*eNB->measurements.n0_power_dB[0]);
+  int SNRtimes10 = dB_fixed_times10(eNB->pusch_vars[UE_id]->ulsch_power[0]) - 300;//(10*eNB->measurements.n0_power_dB[0]);
 
   if      (SNRtimes10 < -640) pdu->rx_indication_rel8.ul_cqi=0;
   else if (SNRtimes10 >  635) pdu->rx_indication_rel8.ul_cqi=255;
@@ -1740,7 +1740,7 @@ void fill_uci_harq_indication(PHY_VARS_eNB *eNB,
 
   // estimate UL_CQI for MAC (from antenna port 0 only)
   pdu->ul_cqi_information.tl.tag = NFAPI_UL_CQI_INFORMATION_TAG;
-  int SNRtimes10 = dB_fixed_times10(uci->stat) - 200;//(10*eNB->measurements.n0_power_dB[0]);
+  int SNRtimes10 = dB_fixed_times10(uci->stat) - 300;//(10*eNB->measurements.n0_power_dB[0]);
 
   if (SNRtimes10 < -100) LOG_I(PHY,"uci->stat %d \n",uci->stat);
 
diff --git a/openair2/ENB_APP/MACRLC_paramdef.h b/openair2/ENB_APP/MACRLC_paramdef.h
index 4e732fe942..5496e96e4e 100644
--- a/openair2/ENB_APP/MACRLC_paramdef.h
+++ b/openair2/ENB_APP/MACRLC_paramdef.h
@@ -53,7 +53,8 @@
 #define CONFIG_STRING_MACRLC_REMOTE_S_PORTC                "remote_s_portc"
 #define CONFIG_STRING_MACRLC_LOCAL_S_PORTD                 "local_s_portd"
 #define CONFIG_STRING_MACRLC_REMOTE_S_PORTD                "remote_s_portd"
-
+#define CONFIG_MACRLC_PUSCH10xSNR                          "puSch10xSnr"
+#define CONFIG_MACRLC_PUCCH10xSNR                          "puCch10xSnr"
 
 /*-------------------------------------------------------------------------------------------------------------------------------------------------------*/
 /*                                            MacRLC  configuration parameters                                                                           */
@@ -77,6 +78,8 @@
 {CONFIG_STRING_MACRLC_REMOTE_S_PORTC,                    NULL,     0,          uptr:NULL,           defintval:50020,           TYPE_UINT,     0},        \
 {CONFIG_STRING_MACRLC_LOCAL_S_PORTD,                     NULL,     0,          uptr:NULL,           defintval:50021,           TYPE_UINT,     0},        \
 {CONFIG_STRING_MACRLC_REMOTE_S_PORTD,                    NULL,     0,          uptr:NULL,           defintval:50021,           TYPE_UINT,     0},        \
+{CONFIG_MACRLC_PUSCH10xSNR,  		                     NULL,     0,          iptr:NULL,           defintval:20,             TYPE_INT,     0},         \
+{CONFIG_MACRLC_PUCCH10xSNR,  		                     NULL,     0,          iptr:NULL,           defintval:20,             TYPE_INT,     0}         \
 }
 #define MACRLC_CC_IDX                                          0
 #define MACRLC_TRANSPORT_N_PREFERENCE_IDX                      1
@@ -95,4 +98,6 @@
 #define MACRLC_REMOTE_S_PORTC_IDX                              14
 #define MACRLC_LOCAL_S_PORTD_IDX                               15
 #define MACRLC_REMOTE_S_PORTD_IDX                              16
+#define MACRLC_PUSCH10xSNR_IDX                                 17
+#define MACRLC_PUCCH10xSNR_IDX                                 18
 /*---------------------------------------------------------------------------------------------------------------------------------------------------------*/
diff --git a/openair2/ENB_APP/enb_config.c b/openair2/ENB_APP/enb_config.c
index 19c168503f..e69b432a10 100644
--- a/openair2/ENB_APP/enb_config.c
+++ b/openair2/ENB_APP/enb_config.c
@@ -296,6 +296,8 @@ void RCconfig_macrlc() {
     RC.nb_macrlc_inst=MacRLC_ParamList.numelt; 
     mac_top_init_eNB();   
     for (j=0;j<RC.nb_macrlc_inst;j++) {
+    	RC.mac[j]->puSch10xSnr = *(MacRLC_ParamList.paramarray[j][MACRLC_PUSCH10xSNR_IDX ].iptr);
+    	RC.mac[j]->puCch10xSnr = *(MacRLC_ParamList.paramarray[j][MACRLC_PUCCH10xSNR_IDX ].iptr);
 
       //RC.mac[j]->phy_test = *(MacRLC_ParamList.paramarray[j][MACRLC_PHY_TEST_IDX].iptr);
       //printf("PHY_TEST = %d,%d\n", RC.mac[j]->phy_test, j);
diff --git a/openair2/LAYER2/MAC/defs.h b/openair2/LAYER2/MAC/defs.h
index 107483b35a..9ac4634f5a 100644
--- a/openair2/LAYER2/MAC/defs.h
+++ b/openair2/LAYER2/MAC/defs.h
@@ -1104,6 +1104,9 @@ typedef struct eNB_MAC_INST_s {
     /// UL handle
     uint32_t ul_handle;
     UE_list_t UE_list;
+    //Target UL channel SNRs (10 times)
+    int16_t puSch10xSnr;
+    int16_t puCch10xSnr;
 
     ///subband bitmap configuration
     SBMAP_CONF sbmap_conf;
diff --git a/openair2/LAYER2/MAC/eNB_scheduler.c b/openair2/LAYER2/MAC/eNB_scheduler.c
index 874b63c6c8..9742300bf0 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler.c
@@ -550,8 +550,8 @@ eNB_dlsch_ulsch_scheduler(module_id_t module_idP, frame_t frameP,
             0 ? "in synch" : "out of sync",
             UE_list->UE_template[CC_id][i].phr_info,
             UE_list->UE_sched_ctrl[i].dl_cqi[CC_id],
-            (UE_list->UE_sched_ctrl[i].pusch_snr[CC_id] - 128) / 2,
-            (UE_list->UE_sched_ctrl[i].pucch1_snr[CC_id] - 128) / 2);
+            (5*UE_list->UE_sched_ctrl[i].pusch_snr[CC_id] - 640) / 10,
+            (5*UE_list->UE_sched_ctrl[i].pucch1_snr[CC_id] - 640) / 10);
     }
 
     RC.eNB[module_idP][CC_id]->pusch_stats_bsr[i][(frameP * 10) +
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_dlsch.c b/openair2/LAYER2/MAC/eNB_scheduler_dlsch.c
index 0729073eba..75d47f0743 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler_dlsch.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_dlsch.c
@@ -1354,10 +1354,12 @@ schedule_ue_spec(module_id_t module_idP,slice_id_t slice_idP,
 	  // this is the normalized RX power
 	  eNB_UE_stats = &UE_list->eNB_UE_stats[CC_id][UE_id];
 
-	  /* TODO: fix how we deal with power, unit is not dBm, it's special from nfapi */
-	  normalized_rx_power = ue_sched_ctl->pucch1_snr[CC_id];
-	  target_rx_power = 208;
-
+	  /* unit is not dBm, it's special from nfapi */
+	  //normalized_rx_power = ue_sched_ctl->pucch1_snr[CC_id];
+	  // converting to dBm: ToDo: Noise power hard coded to 30
+	  normalized_rx_power = (5*ue_sched_ctl->pucch1_snr[CC_id]-640)/10+30;
+	  target_rx_power= eNB->puCch10xSnr/10 + 30;
+printf("\n eNB->puCch10xSnr = %d,  normalized_rx_power = %d, target_rx_power = %d \n",eNB->puCch10xSnr,normalized_rx_power,target_rx_power);
 	  // this assumes accumulated tpc
 	  // make sure that we are only sending a tpc update once a frame, otherwise the control loop will freak out
 	  int32_t framex10psubframe = UE_list->UE_template[CC_id][UE_id].pucch_tpc_tx_frame * 10 + UE_list->UE_template[CC_id][UE_id].pucch_tpc_tx_subframe;
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_ulsch.c b/openair2/LAYER2/MAC/eNB_scheduler_ulsch.c
index 9ccb0e66ab..6f0b9d849c 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler_ulsch.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_ulsch.c
@@ -1115,8 +1115,7 @@ schedule_ulsch_rnti(module_id_t module_idP,
   uint8_t status = 0;
   uint8_t rb_table_index = -1;
   uint32_t cqi_req, cshift, ndi, tpc;
-  int32_t normalized_rx_power;
-  int32_t target_rx_power = -90;
+  int32_t normalized_rx_power, target_rx_power;
   static int32_t tpc_accumulated = 0;
   int n;
   int CC_id = 0;
@@ -1280,9 +1279,12 @@ schedule_ulsch_rnti(module_id_t module_idP,
 	  //compute the expected ULSCH RX power (for the stats)
 
 	  // this is the normalized RX power and this should be constant (regardless of mcs
-	  normalized_rx_power = UE_sched_ctrl->pusch_snr[CC_id];
-	  target_rx_power = 178;
-
+	  //is not in dBm, unit from nfapi
+	  //normalized_rx_power = UE_sched_ctrl->pusch_snr[CC_id];
+	  // converting to dBm: ToDo: Noise power hard coded to 30
+	  normalized_rx_power = (5*UE_sched_ctrl->pusch_snr[CC_id]-640)/10+30;
+	  target_rx_power= mac->puSch10xSnr/10 + 30;
+	  printf("\n mac->puSch10xSnr = %d,  normalized_rx_power = %d, target_rx_power = %d \n",mac->puSch10xSnr,normalized_rx_power,target_rx_power);
 	  // this assumes accumulated tpc
 	  // make sure that we are only sending a tpc update once a frame, otherwise the control loop will freak out
 	  int32_t framex10psubframe = UE_template->pusch_tpc_tx_frame * 10 + UE_template->pusch_tpc_tx_subframe;
diff --git a/openair2/LAYER2/MAC/main.c b/openair2/LAYER2/MAC/main.c
index afee163203..6f5f1f715a 100644
--- a/openair2/LAYER2/MAC/main.c
+++ b/openair2/LAYER2/MAC/main.c
@@ -61,15 +61,18 @@ void mac_top_init_eNB(void)
 	  RC.nb_macrlc_inst);
 
     if (RC.nb_macrlc_inst > 0) {
-	RC.mac =
-	    (eNB_MAC_INST **) malloc16(RC.nb_macrlc_inst *
-				       sizeof(eNB_MAC_INST *));
+      if (RC.mac == NULL){
+		RC.mac =
+			(eNB_MAC_INST **) malloc16(RC.nb_macrlc_inst *
+						   sizeof(eNB_MAC_INST *));
+        }
 	AssertFatal(RC.mac != NULL,
 		    "can't ALLOCATE %zu Bytes for %d eNB_MAC_INST with size %zu \n",
 		    RC.nb_macrlc_inst * sizeof(eNB_MAC_INST *),
 		    RC.nb_macrlc_inst, sizeof(eNB_MAC_INST));
 	for (i = 0; i < RC.nb_macrlc_inst; i++) {
-	    RC.mac[i] = (eNB_MAC_INST *) malloc16(sizeof(eNB_MAC_INST));
+		if (RC.mac[i] == NULL)
+			RC.mac[i] = (eNB_MAC_INST *) malloc16(sizeof(eNB_MAC_INST));
 	    AssertFatal(RC.mac != NULL,
 			"can't ALLOCATE %zu Bytes for %d eNB_MAC_INST with size %zu \n",
 			RC.nb_macrlc_inst * sizeof(eNB_MAC_INST *),
@@ -77,7 +80,8 @@ void mac_top_init_eNB(void)
 	    LOG_D(MAC,
 		  "[MAIN] ALLOCATE %zu Bytes for %d eNB_MAC_INST @ %p\n",
 		  sizeof(eNB_MAC_INST), RC.nb_macrlc_inst, RC.mac);
-	    bzero(RC.mac[i], sizeof(eNB_MAC_INST));
+	    if (RC.mac[i] == NULL) bzero(RC.mac[i], sizeof(eNB_MAC_INST));
+	      // bzero(RC.mac[i], sizeof(eNB_MAC_INST));
 	    RC.mac[i]->Mod_id = i;
 	    for (j = 0; j < MAX_NUM_CCs; j++) {
 		RC.mac[i]->DL_req[j].dl_config_request_body.
-- 
2.26.2