From 713e31357f9ef3aec9b55e34a3074399d49b3829 Mon Sep 17 00:00:00 2001
From: matzakos <panagiotis.matzakos@eurecom.fr>
Date: Wed, 31 Jan 2018 19:47:22 +0100
Subject: [PATCH] Seemed to have fixed pnf(UE) -vnf(eNB) sync issue. Works up
 to the reception of RRConnectionReconfigurationComplete message at the eNB
 but crashing after that.

---
 openair1/SCHED/pusch_pc.c            |   7 +-
 openair2/LAYER2/MAC/config.c         |   3 +-
 openair2/PHY_INTERFACE/phy_stub_UE.c |  28 ++++++-
 openair2/RRC/LITE/rrc_UE.c           |   5 +-
 targets/RT/USER/lte-ue.c             | 117 +++++++++++++++------------
 5 files changed, 101 insertions(+), 59 deletions(-)

diff --git a/openair1/SCHED/pusch_pc.c b/openair1/SCHED/pusch_pc.c
index caef131627..a631839bf5 100644
--- a/openair1/SCHED/pusch_pc.c
+++ b/openair1/SCHED/pusch_pc.c
@@ -35,6 +35,8 @@
 #include "PHY/LTE_TRANSPORT/proto.h"
 #include "PHY/extern.h"
 
+extern uint8_t  nfapi_mode;
+
 // This is the formula from Section 5.1.1.1 in 36.213 100*10*log10((2^(MPR*Ks)-1)), where MPR is in the range [0,6] and Ks=1.25
 int16_t hundred_times_delta_TF[100] = {-32768,-1268,-956,-768,-631,-523,-431,-352,-282,-219,-161,-107,-57,-9,36,79,120,159,197,234,269,304,337,370,402,434,465,495,525,555,583,612,640,668,696,723,750,777,803,829,856,881,907,933,958,983,1008,1033,1058,1083,1108,1132,1157,1181,1205,1229,1254,1278,1302,1325,1349,1373,1397,1421,1444,1468,1491,1515,1538,1562,1585,1609,1632,1655,1679,1702,1725,1748,1772,1795,1818,1841,1864,1887,1910,1933,1956,1980,2003,2026,2049,2072,2095,2118,2141,2164,2186,2209,2232,2255};
 uint16_t hundred_times_log10_NPRB[100] = {0,301,477,602,698,778,845,903,954,1000,1041,1079,1113,1146,1176,1204,1230,1255,1278,1301,1322,1342,1361,1380,1397,1414,1431,1447,1462,1477,1491,1505,1518,1531,1544,1556,1568,1579,1591,1602,1612,1623,1633,1643,1653,1662,1672,1681,1690,1698,1707,1716,1724,1732,1740,1748,1755,1763,1770,1778,1785,1792,1799,1806,1812,1819,1826,1832,1838,1845,1851,1857,1863,1869,1875,1880,1886,1892,1897,1903,1908,1913,1919,1924,1929,1934,1939,1944,1949,1954,1959,1963,1968,1973,1977,1982,1986,1991,1995,2000};
@@ -201,7 +203,10 @@ void pusch_power_cntl(PHY_VARS_UE *ue,UE_rxtx_proc_t *proc,uint8_t eNB_id,uint8_
 int8_t get_PHR(uint8_t Mod_id, uint8_t CC_id,uint8_t eNB_index)
 {
 
-  return PHY_vars_UE_g[Mod_id][CC_id]->ulsch[eNB_index]->PHR;
+	if(nfapi_mode!=3)
+		return PHY_vars_UE_g[Mod_id][CC_id]->ulsch[eNB_index]->PHR;
+	else
+		return 40; // For nfapi_mode=3 consider ideal conditions
 }
 
 // uint8_t eNB_id,uint8_t harq_pid, uint8_t UE_id,
diff --git a/openair2/LAYER2/MAC/config.c b/openair2/LAYER2/MAC/config.c
index d5d77f4ea8..17681ec9de 100644
--- a/openair2/LAYER2/MAC/config.c
+++ b/openair2/LAYER2/MAC/config.c
@@ -1121,7 +1121,8 @@ rrc_mac_config_req_ue(
       } else {
 	UE_mac_inst[Mod_idP].scheduling_info.maxHARQ_Tx     = (uint16_t) MAC_MainConfig__ul_SCH_Config__maxHARQ_Tx_n5;
       }
-      phy_config_harq_ue(Mod_idP,0,eNB_index,UE_mac_inst[Mod_idP].scheduling_info.maxHARQ_Tx);
+      if(nfapi_mode!=3)
+    	  phy_config_harq_ue(Mod_idP,0,eNB_index,UE_mac_inst[Mod_idP].scheduling_info.maxHARQ_Tx);
       
       if (mac_MainConfig->ul_SCH_Config->retxBSR_Timer) {
 	UE_mac_inst[Mod_idP].scheduling_info.retxBSR_Timer     = (uint16_t) mac_MainConfig->ul_SCH_Config->retxBSR_Timer;
diff --git a/openair2/PHY_INTERFACE/phy_stub_UE.c b/openair2/PHY_INTERFACE/phy_stub_UE.c
index 8aac0915c5..adcc8d1a22 100644
--- a/openair2/PHY_INTERFACE/phy_stub_UE.c
+++ b/openair2/PHY_INTERFACE/phy_stub_UE.c
@@ -127,7 +127,17 @@ void fill_rx_indication_UE_MAC(module_id_t Mod_id,int frame,int subframe, UL_IND
 void fill_sr_indication_UE_MAC(int Mod_id,int frame,int subframe, UL_IND_t *UL_INFO, uint16_t rnti) {
 
   pthread_mutex_lock(&UE_mac_inst[Mod_id].UL_INFO_mutex);
-  nfapi_sr_indication_pdu_t *pdu =   &UL_INFO->sr_ind.sr_indication_body.sr_pdu_list[UL_INFO->rx_ind.rx_indication_body.number_of_pdus];
+
+  nfapi_sr_indication_t       *sr_ind = &UL_INFO->sr_ind;
+  nfapi_sr_indication_body_t  *sr_ind_body =    &sr_ind->sr_indication_body;
+  nfapi_sr_indication_pdu_t *pdu =   &sr_ind_body->sr_pdu_list[sr_ind_body->number_of_srs];
+
+  //nfapi_sr_indication_pdu_t *pdu =   &UL_INFO->sr_ind.sr_indication_body.sr_pdu_list[UL_INFO->rx_ind.rx_indication_body.number_of_pdus];
+
+  sr_ind->sfn_sf = frame<<4|subframe;
+  sr_ind->header.message_id = NFAPI_RX_SR_INDICATION;
+
+  sr_ind_body->tl.tag = NFAPI_SR_INDICATION_BODY_TAG;
 
   pdu->instance_length                                = 0; // don't know what to do with this
   //  pdu->rx_ue_information.handle                       = handle;
@@ -139,13 +149,16 @@ void fill_sr_indication_UE_MAC(int Mod_id,int frame,int subframe, UL_IND_t *UL_I
   //int SNRtimes10 = dB_fixed_times10(stat) - 200;//(10*eNB->measurements.n0_power_dB[0]);
   int SNRtimes10 = 640;
 
+  pdu->ul_cqi_information.tl.tag = NFAPI_UL_CQI_INFORMATION_TAG;
+
 
   if      (SNRtimes10 < -640) pdu->ul_cqi_information.ul_cqi=0;
   else if (SNRtimes10 >  635) pdu->ul_cqi_information.ul_cqi=255;
   else                        pdu->ul_cqi_information.ul_cqi=(640+SNRtimes10)/5;
   pdu->ul_cqi_information.channel = 0;
 
-  UL_INFO->rx_ind.rx_indication_body.number_of_pdus++;
+  //UL_INFO->rx_ind.rx_indication_body.number_of_pdus++;
+  sr_ind_body->number_of_srs++;
   pthread_mutex_unlock(&UE_mac_inst[Mod_id].UL_INFO_mutex);
 }
 
@@ -763,6 +776,7 @@ int ul_config_req_UE_MAC(nfapi_ul_config_request_t* req, int timer_frame, int ti
   UL_INFO->rx_ind.rx_indication_body.rx_pdu_list = (nfapi_rx_indication_pdu_t*)malloc(req->ul_config_request_body.number_of_pdus*sizeof(nfapi_rx_indication_pdu_t));
   UL_INFO->crc_ind.crc_indication_body.crc_pdu_list = (nfapi_crc_indication_pdu_t*)malloc(req->ul_config_request_body.number_of_pdus*sizeof(nfapi_crc_indication_pdu_t));
   UL_INFO->harq_ind.harq_indication_body.harq_pdu_list = (nfapi_harq_indication_pdu_t*)malloc(req->ul_config_request_body.number_of_pdus*sizeof(nfapi_harq_indication_pdu_t));
+  UL_INFO->sr_ind.sr_indication_body.sr_pdu_list = (nfapi_harq_indication_pdu_t*)malloc(req->ul_config_request_body.number_of_pdus*sizeof(nfapi_harq_indication_pdu_t));
 
   //Panos: Additional checks needed here to check if the UE is in PRACH mode.
 
@@ -860,6 +874,12 @@ int ul_config_req_UE_MAC(nfapi_ul_config_request_t* req, int timer_frame, int ti
 	  UL_INFO->harq_ind.harq_indication_body.number_of_harqs =0;
 
   }
+  if(UL_INFO->sr_ind.sr_indication_body.number_of_srs>0)
+  {
+	  LOG_I(MAC, "Panos-D: ul_config_req_UE_MAC 2.5, SFN/SF in REQ:%d.%d, SFN/SF of PNF counter:%d.%d \n", sfn, sf, timer_frame, timer_subframe);
+	  oai_nfapi_sr_indication(&UL_INFO->sr_ind);
+	  UL_INFO->sr_ind.sr_indication_body.number_of_srs = 0;
+  }
 
   // Free ul_config_request
   /*if(req->ul_config_request_body.ul_config_pdu_list != NULL){
@@ -883,6 +903,10 @@ int ul_config_req_UE_MAC(nfapi_ul_config_request_t* req, int timer_frame, int ti
 	  free(UL_INFO->harq_ind.harq_indication_body.harq_pdu_list);
 	  UL_INFO->harq_ind.harq_indication_body.harq_pdu_list = NULL;
   }
+  if(UL_INFO->sr_ind.sr_indication_body.sr_pdu_list!=NULL){
+	  free(UL_INFO->sr_ind.sr_indication_body.sr_pdu_list);
+	  UL_INFO->sr_ind.sr_indication_body.sr_pdu_list = NULL;
+  }
   free(UL_INFO);
   UL_INFO = NULL;
 	}
diff --git a/openair2/RRC/LITE/rrc_UE.c b/openair2/RRC/LITE/rrc_UE.c
index 022d01c86c..bbd1fef3ab 100644
--- a/openair2/RRC/LITE/rrc_UE.c
+++ b/openair2/RRC/LITE/rrc_UE.c
@@ -2113,7 +2113,8 @@ rrc_ue_process_rrcConnectionReconfiguration(
 
       //TTN for D2D
       //if RRCConnectionReconfiguration message includes the sl-CommConfig
-      if (rrcConnectionReconfiguration_r8->nonCriticalExtension->nonCriticalExtension->nonCriticalExtension->nonCriticalExtension->nonCriticalExtension->sl_CommConfig_r12->commTxResources_r12->present != SL_CommConfig_r12__commTxResources_r12_PR_NOTHING){
+      //***Panos: Comment lines 2117-2137 temporarily
+      /*if (rrcConnectionReconfiguration_r8->nonCriticalExtension->nonCriticalExtension->nonCriticalExtension->nonCriticalExtension->nonCriticalExtension->sl_CommConfig_r12->commTxResources_r12->present != SL_CommConfig_r12__commTxResources_r12_PR_NOTHING){
          LOG_I(RRC,"sl-CommConfig is present\n");
          //process sl-CommConfig
          rrc_ue_process_sidelink_radioResourceConfig(ctxt_pP->module_id,eNB_index,
@@ -2133,7 +2134,7 @@ rrc_ue_process_rrcConnectionReconfiguration(
                (SL_CommConfig_r12_t* )NULL,
                rrcConnectionReconfiguration_r8->nonCriticalExtension->nonCriticalExtension->nonCriticalExtension->nonCriticalExtension->nonCriticalExtension->sl_DiscConfig_r12
                );
-      }
+      }*/
 
 
 #if defined(ENABLE_ITTI)
diff --git a/targets/RT/USER/lte-ue.c b/targets/RT/USER/lte-ue.c
index b012670b89..aa9bdef925 100644
--- a/targets/RT/USER/lte-ue.c
+++ b/targets/RT/USER/lte-ue.c
@@ -1519,6 +1519,11 @@ int setup_ue_buffers(PHY_VARS_UE **phy_vars_ue, openair0_config_t *openair0_cfg)
 // Panos: This timer thread is used only in the phy_sub mode as an independent timer
 // which will be ticking and provide the SFN/SF values that will be used from the UE threads
 // playing the role of nfapi-pnf.
+
+
+
+
+
 static void* timer_thread( void* param ) {
   thread_top_init("timer_thread",1,870000L,1000000L,1000000L);
   timer_subframe =9;
@@ -1530,6 +1535,15 @@ static void* timer_thread( void* param ) {
   double t_diff;
   int external_timer = 0;
 
+
+  //struct timespec pselect_start;
+
+
+  //struct timespec sf_duration;
+  //sf_duration.tv_sec = 0;
+  //sf_duration.tv_nsec = 1e6;
+
+
   wait_sync("timer_thread");
 
   //pthread_mutex_init(&phy_stub_ticking->mutex_ticking,NULL);
@@ -1556,6 +1570,18 @@ static void* timer_thread( void* param ) {
   else LOG_I(PHY,"Running with internal timer\n");
   }
 
+  struct timespec t_start;
+  struct timespec t_now;
+  struct timespec t_sleep;
+  uint64_t T_0;
+  uint64_t T_now;
+  uint64_t T_next_SF;
+  uint64_t T_sleep;
+  uint64_t sf_cnt = 0; //Total Subframe counter
+
+  clock_gettime(CLOCK_MONOTONIC, &t_start);
+  T_0 = (uint64_t) t_start.tv_sec*1000000000 + t_start.tv_nsec;
+
   while (!oai_exit) {
 
     // these are local subframe/frame counters to check that we are in synch with the fronthaul timing.
@@ -1589,7 +1615,22 @@ static void* timer_thread( void* param ) {
 
     //clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); // get initial time-stamp
     if (external_timer == 0) {
-      usleep(1000);
+    	clock_gettime(CLOCK_MONOTONIC, &t_now);
+    	sf_cnt++;
+    	T_next_SF = T_0 + sf_cnt*1000000;
+    	T_now =(uint64_t) t_now.tv_sec*1000000000 + t_now.tv_nsec;
+    	if(T_now > T_next_SF){
+    		t_sleep.tv_sec =0;
+    	    t_sleep.tv_nsec =0;
+    		//T_sleep=0;
+    	}
+    	else{
+    		T_sleep = T_next_SF - T_now;
+    		t_sleep.tv_sec =0;
+    		t_sleep.tv_nsec = (__syscall_slong_t) T_sleep;
+    	}
+      nanosleep(&t_sleep, (struct timespec *)NULL);
+      //usleep(1000);
       UE_tport_t pdu;
       pdu.header.packet_type = TTI_SYNC;
       pdu.header.absSF = (timer_frame*10)+timer_subframe;
@@ -1646,56 +1687,20 @@ static void* timer_thread( void* param ) {
 
 
 
-/*static void* timer_thread( void* param ) {
-<<<<<<< Updated upstream
-=======
-	thread_top_init("timer_thread",1,870000L,1000000L,1000000L);
-	timer_subframe =9;
-	timer_frame    =1023;
-	//phy_stub_ticking = (SF_ticking*)malloc(sizeof(SF_ticking));
-	phy_stub_ticking->ticking_var = -1;
-	PHY_VARS_UE *UE;
-	UE = PHY_vars_UE_g[0][0];
-	double t_diff;
-	wait_sync("timer_thread");
-    //pthread_mutex_init(&phy_stub_ticking->mutex_ticking,NULL);
-    //pthread_cond_init(&phy_stub_ticking->cond_ticking,NULL);
-
-	struct timespec start = {0};
-	struct timespec end = {0};
-	//sleepValue.tv_nsec = 1000000;
-	opp_enabled = 1;
-	while (!oai_exit) {
-
-	    // these are local subframe/frame counters to check that we are in synch with the fronthaul timing.
-	    // They are set on the first rx/tx in the underly FH routines.
-	    if (timer_subframe==9) {
-	    	timer_subframe=0;
-	    	timer_frame++;
-	    	timer_frame&=1023;
-	    } else {
-	    	timer_subframe++;
-	    }
-	    //printf("[timer_thread] Frame: %d, Subframe: %d \n", timer_frame, timer_subframe);
-	    //LOG_I(MAC," Panos-D [timer_thread] Frame: %d, Subframe: %d \n", timer_frame, timer_subframe);
-	    //AssertFatal( 0 == pthread_cond_signal(&phy_stub_ticking->cond_ticking), "");
-	    AssertFatal(pthread_mutex_lock(&phy_stub_ticking->mutex_ticking) ==0,"");
-	    phy_stub_ticking->ticking_var++;
-	    // This should probably be a call to pthread_cond_broadcast when we introduce support for multiple UEs (threads)
-	    if(phy_stub_ticking->ticking_var == 0){
-	    //AssertFatal(phy_stub_ticking->ticking_var == 0,"phy_stub_ticking->ticking_var = %d",
-	    //		phy_stub_ticking->ticking_var);
-	    if (pthread_cond_signal(&phy_stub_ticking->cond_ticking) != 0) {
-	    		//LOG_E( PHY, "[SCHED][UE %d] ERROR pthread_cond_signal for UE RX thread\n", UE->Mod_id);
-	    		LOG_E( PHY, "timer_thread ERROR pthread_cond_signal for UE_thread\n");
-	    		exit_fun("nothing to add");
-	    }
-	    }
-	    else{
-	    	LOG_I(MAC," Panos-D: Problemmm");
-	    }
->>>>>>> Stashed changes
 
+
+
+
+
+
+
+
+
+
+
+
+
+/*static void* timer_thread( void* param ) {
   thread_top_init("timer_thread",1,870000L,1000000L,1000000L);
   timer_subframe =9;
   timer_frame    =1023;
@@ -1717,6 +1722,7 @@ static void* timer_thread( void* param ) {
   opp_enabled = 1;
 
   // first check if we are receiving timing indications
+  if(nfapi_mode==4) {
   usleep(10000);
   if (UE->instance_cnt_timer > 0) {
     external_timer = 1;
@@ -1729,6 +1735,7 @@ static void* timer_thread( void* param ) {
     LOG_I(PHY,"Running with external timer\n");
   }
   else LOG_I(PHY,"Running with internal timer\n");
+  }
 
   while (!oai_exit) {
 
@@ -1791,11 +1798,8 @@ static void* timer_thread( void* param ) {
     //if (t_diff > 1100)
 
 
-<<<<<<< Updated upstream
     //    LOG_E(MAC," Panos-D Absolute time: %f\n", t_diff);
-=======
 	    //LOG_E(MAC," Panos-D Absolute time: %f\n", t_diff);
->>>>>>> Stashed changes
 
     //printf("Panos-D: Absolute time: %f", t_diff);
 
@@ -1818,6 +1822,13 @@ static void* timer_thread( void* param ) {
 
 }*/
 
+
+
+
+
+
+
+
 int init_timer_thread(void) {
 	// Panos: CAREFUL Originally this was set to PHY_VARS_UE *UE=PHY_vars_UE_g[0]
   //PHY_VARS_UE *UE=PHY_vars_UE_g[0];
-- 
2.26.2