From f31928926f6c85fc87aeb7cba319e6686d0b7015 Mon Sep 17 00:00:00 2001
From: matzakos <panagiotis.matzakos@eurecom.fr>
Date: Wed, 20 Dec 2017 15:29:40 +0100
Subject: [PATCH] Works up to the reception of the RA_RESPONSE at the UE.
 Crashing at fill_rx_indication_UE_MAC just before sending Msg3.

---
 openair1/PHY/LTE_TRANSPORT/prach.c   |   4 +
 openair2/LAYER2/MAC/ra_procedures.c  |  13 ++-
 openair2/LAYER2/MAC/ue_procedures.c  |   2 +-
 openair2/PHY_INTERFACE/phy_stub_UE.c | 126 ++++++++++++++++++++++-----
 targets/RT/USER/lte-ue.c             |  27 +++++-
 5 files changed, 145 insertions(+), 27 deletions(-)

diff --git a/openair1/PHY/LTE_TRANSPORT/prach.c b/openair1/PHY/LTE_TRANSPORT/prach.c
index 5f1b3ce448a..e6b2720b770 100644
--- a/openair1/PHY/LTE_TRANSPORT/prach.c
+++ b/openair1/PHY/LTE_TRANSPORT/prach.c
@@ -584,9 +584,12 @@ int is_prach_subframe0(LTE_DL_FRAME_PARMS *frame_parms,uint8_t prach_ConfigIndex
 }
 
 int is_prach_subframe(LTE_DL_FRAME_PARMS *frame_parms,uint32_t frame, uint8_t subframe) {
+
   
   uint8_t prach_ConfigIndex  = frame_parms->prach_config_common.prach_ConfigInfo.prach_ConfigIndex;
+  //LOG_I(MAC, "Panos-D: is_prach_subframe 2 \n");
   int prach_mask             = is_prach_subframe0(frame_parms,prach_ConfigIndex,frame,subframe);
+  //LOG_I(MAC, "Panos-D: is_prach_subframe 3 \n");
 
 #ifdef Rel14
   int i;
@@ -596,6 +599,7 @@ int is_prach_subframe(LTE_DL_FRAME_PARMS *frame_parms,uint32_t frame, uint8_t su
       prach_mask|=(is_prach_subframe0(frame_parms,frame_parms->prach_emtc_config_common.prach_ConfigInfo.prach_ConfigIndex[i],frame,subframe)<<(i+1));
   }
 #endif
+  //LOG_I(MAC, "Panos-D: is_prach_subframe 4 \n");
   return(prach_mask);
 }
 
diff --git a/openair2/LAYER2/MAC/ra_procedures.c b/openair2/LAYER2/MAC/ra_procedures.c
index ef19a4c532c..b8d09180085 100644
--- a/openair2/LAYER2/MAC/ra_procedures.c
+++ b/openair2/LAYER2/MAC/ra_procedures.c
@@ -106,6 +106,7 @@ void get_prach_resources(module_id_t module_idP,
                          uint8_t first_Msg3,
                          RACH_ConfigDedicated_t *rach_ConfigDedicated)
 {
+	LOG_I(MAC, "Panos-D: get_prach_resources 1");
 
   uint8_t Msg3_size = UE_mac_inst[module_idP].RA_Msg3_size;
   PRACH_RESOURCES_t *prach_resources = &UE_mac_inst[module_idP].RA_prach_resources;
@@ -195,17 +196,20 @@ void get_prach_resources(module_id_t module_idP,
 
   if (first_Msg3 == 1) {
     if (noGroupB == 1) {
+    	LOG_I(MAC, "Panos-D: get_prach_resources 2");
       // use Group A procedure
       UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = (taus())%numberOfRA_Preambles;
       UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
       UE_mac_inst[module_idP].RA_usedGroupA = 1;
     } else if ((Msg3_size <messageSizeGroupA) ||
                (get_PL(module_idP,0,eNB_index) > PLThreshold)) {
+    	LOG_I(MAC, "Panos-D: get_prach_resources 3");
       // use Group A procedure
       UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = (taus())%sizeOfRA_PreamblesGroupA;
       UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
       UE_mac_inst[module_idP].RA_usedGroupA = 1;
     } else { // use Group B
+    	LOG_I(MAC, "Panos-D: get_prach_resources 4");
       UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = sizeOfRA_PreamblesGroupA +
         (taus())%(numberOfRA_Preambles - sizeOfRA_PreamblesGroupA);
       UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
@@ -246,6 +250,7 @@ void get_prach_resources(module_id_t module_idP,
 
   // choose RA-RNTI
   UE_mac_inst[module_idP].RA_prach_resources.ra_RNTI = 1 + t_id + 10*f_id;
+  LOG_I(MAC, "Panos-D: get_prach_resources 4");
 }
 
 void Msg1_transmitted(module_id_t module_idP,uint8_t CC_id,frame_t frameP, uint8_t eNB_id)
@@ -290,12 +295,13 @@ void Msg3_transmitted(module_id_t module_idP,uint8_t CC_id,frame_t frameP, uint8
 PRACH_RESOURCES_t *ue_get_rach(module_id_t module_idP,int CC_id,frame_t frameP, uint8_t eNB_indexP,sub_frame_t subframeP)
 {
 
-
+  LOG_I(MAC, "Panos-D: ue_get_rach 1");
   uint8_t                  Size               = 0;
   UE_MODE_t UE_mode;
   // Panos: Modification for phy_stub_ue operation
   if(nfapi_mode == 3) { // Panos: phy_stub_ue mode
 	  UE_mode = UE_mac_inst[module_idP].UE_mode[0];
+	  LOG_I(MAC, "Panos-D: ue_get_rach 2, E_mode: %d", UE_mode);
   }
   else { // Full stack mode
 	  UE_mode = get_ue_mode(module_idP,0,eNB_indexP);
@@ -315,13 +321,16 @@ PRACH_RESOURCES_t *ue_get_rach(module_id_t module_idP,int CC_id,frame_t frameP,
 	      "Transmission on secondary CCs is not supported yet\n");
 
   if (UE_mode == PRACH) {
+	  LOG_I(MAC, "Panos-D: ue_get_rach 3");
     if (UE_mac_inst[module_idP].radioResourceConfigCommon) {
       rach_ConfigCommon = &UE_mac_inst[module_idP].radioResourceConfigCommon->rach_ConfigCommon;
     } else {
+    	//AssertFatal(UE_mac_inst[module_idP].radioResourceConfigCommon!=NULL,"RadioResourceConfigCommon Null");
       return(NULL);
     }
 
     if (UE_mac_inst[module_idP].RA_active == 0) {
+    	LOG_I(MAC, "Panos-D: ue_get_rach 4");
       LOG_I(MAC,"RA not active\n");
       // check if RRC is ready to initiate the RA procedure
       Size = mac_rrc_data_req(module_idP,
@@ -339,7 +348,7 @@ PRACH_RESOURCES_t *ue_get_rach(module_id_t module_idP,int CC_id,frame_t frameP,
       LOG_I(MAC,"[UE %d] Frame %d: Requested RRCConnectionRequest, got %d bytes\n",module_idP,frameP,Size);
       
       if (Size>0) {
-	
+    	  LOG_I(MAC, "Panos-D: ue_get_rach 5");
 	UE_mac_inst[module_idP].RA_active                        = 1;
 	UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER = 1;
 	UE_mac_inst[module_idP].RA_Msg3_size                     = Size+sizeof(SCH_SUBHEADER_SHORT)+sizeof(SCH_SUBHEADER_SHORT);
diff --git a/openair2/LAYER2/MAC/ue_procedures.c b/openair2/LAYER2/MAC/ue_procedures.c
index db7304c70a9..48831f7b70b 100644
--- a/openair2/LAYER2/MAC/ue_procedures.c
+++ b/openair2/LAYER2/MAC/ue_procedures.c
@@ -139,7 +139,7 @@ void ue_init_mac(module_id_t module_idP)
 
   if(nfapi_mode == 3) {
 	  pthread_mutex_init(&UE_mac_inst[module_idP].UL_INFO_mutex,NULL);
-	  UE_mac_inst[module_idP].UE_mode[0] = PRACH;
+	  UE_mac_inst[module_idP].UE_mode[0] = NOT_SYNCHED; //PRACH;
 	  UE_mac_inst[module_idP].first_ULSCH_Tx =0;
 	  UE_mac_inst[module_idP].dl_config_req = NULL;
 	  UE_mac_inst[module_idP].ul_config_req = NULL;
diff --git a/openair2/PHY_INTERFACE/phy_stub_UE.c b/openair2/PHY_INTERFACE/phy_stub_UE.c
index 8926e4aa5bd..6d4b8034d4a 100644
--- a/openair2/PHY_INTERFACE/phy_stub_UE.c
+++ b/openair2/PHY_INTERFACE/phy_stub_UE.c
@@ -160,19 +160,30 @@ void fill_crc_indication_UE_MAC(int Mod_id,int frame,int subframe, UL_IND_t *UL_
 
 void fill_rach_indication_UE_MAC(int Mod_id,int frame,int subframe, UL_IND_t *UL_INFO, uint8_t ra_PreambleIndex, uint16_t ra_RNTI) {
 
+	//LOG_I(MAC, "Panos-D: fill_rach_indication_UE_MAC 1 \n");
 	pthread_mutex_lock(&UE_mac_inst[Mod_id].UL_INFO_mutex);
+	UL_INFO = (UL_IND_t*)malloc(sizeof(UL_IND_t));
 
+
+
+	//LOG_I(MAC, "Panos-D: fill_rach_indication_UE_MAC 2 \n");
 	    UL_INFO->rach_ind.rach_indication_body.number_of_preambles                 = 1;
+
 	    //eNB->UL_INFO.rach_ind.preamble_list                       = &eNB->preamble_list[0];
+	    UL_INFO->rach_ind.header.message_id                         = NFAPI_RACH_INDICATION;
+	    UL_INFO->rach_ind.sfn_sf                                    = frame<<4 | subframe;
+
 	    UL_INFO->rach_ind.rach_indication_body.tl.tag                              = NFAPI_RACH_INDICATION_BODY_TAG;
 
+
+	    UL_INFO->rach_ind.rach_indication_body.preamble_list = (nfapi_preamble_pdu_t*)malloc(UL_INFO->rach_ind.rach_indication_body.number_of_preambles*sizeof(nfapi_preamble_pdu_t));
 	    UL_INFO->rach_ind.rach_indication_body.preamble_list[0].preamble_rel8.tl.tag   		= NFAPI_PREAMBLE_REL8_TAG;
 	    UL_INFO->rach_ind.rach_indication_body.preamble_list[0].preamble_rel8.timing_advance = 0; //Panos: Not sure about that
 
 	    //Panos: The two following should get extracted from the call to get_prach_resources().
 	    UL_INFO->rach_ind.rach_indication_body.preamble_list[0].preamble_rel8.preamble = ra_PreambleIndex;
 	    UL_INFO->rach_ind.rach_indication_body.preamble_list[0].preamble_rel8.rnti 	  = ra_RNTI;
-	    UL_INFO->rach_ind.rach_indication_body.number_of_preambles++;
+	    //UL_INFO->rach_ind.rach_indication_body.number_of_preambles++;
 
 
 	    UL_INFO->rach_ind.rach_indication_body.preamble_list[0].preamble_rel13.rach_resource_type = 0;
@@ -200,8 +211,9 @@ void fill_rach_indication_UE_MAC(int Mod_id,int frame,int subframe, UL_IND_t *UL
 
 	          //Panos: This function is currently defined only in the nfapi-RU-RAU-split so we should call it when we merge
 	          // with that branch.
-	          //oai_nfapi_rach_ind(&rach_ind);
-
+	          oai_nfapi_rach_ind(&UL_INFO->rach_ind);
+	          free(UL_INFO->rach_ind.rach_indication_body.preamble_list);
+	          free(UL_INFO);
 
 	        //}
 	      pthread_mutex_unlock(&UE_mac_inst[Mod_id].UL_INFO_mutex);
@@ -477,12 +489,14 @@ void handle_nfapi_ul_pdu_UE_MAC(module_id_t Mod_id,
                          uint16_t frame,uint8_t subframe,uint8_t srs_present)
 {
   nfapi_ul_config_ulsch_pdu_rel8_t *rel8 = &ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8;
+  LOG_I(MAC, "Panos-D: handle_nfapi_ul_pdu_UE_MAC 1 \n");
 
   //int8_t UE_id;
 
   // check if we have received a dci for this ue and ulsch descriptor is configured
 
   if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_ULSCH_PDU_TYPE) {
+	  LOG_I(MAC, "Panos-D: handle_nfapi_ul_pdu_UE_MAC 2.1 \n");
     //AssertFatal((UE_id = find_ulsch(ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.rnti,eNB,SEARCH_EXIST_OR_FREE))>=0,
     //            "No existing UE ULSCH for rnti %x\n",rel8->rnti);
     LOG_D(PHY,"Applying UL config for UE, rnti %x for frame %d, subframe %d\n",
@@ -494,7 +508,8 @@ void handle_nfapi_ul_pdu_UE_MAC(module_id_t Mod_id,
     uint8_t access_mode=SCHEDULED_ACCESS;
     if(buflen>0){
     	if(UE_mac_inst[Mod_id].first_ULSCH_Tx == 1){ // Msg3 case
-    		fill_crc_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, 0);
+    		LOG_I(MAC, "Panos-D: handle_nfapi_ul_pdu_UE_MAC 2.2 \n");
+    		//fill_crc_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, 0);
     		fill_rx_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, UE_mac_inst[Mod_id].RA_prach_resources.Msg3,buflen, rnti);
     		Msg3_transmitted(Mod_id, 0, frame, 0);
 
@@ -502,14 +517,16 @@ void handle_nfapi_ul_pdu_UE_MAC(module_id_t Mod_id,
     		//UE_mac_inst[Mod_id].first_ULSCH_Tx = 0;
     	}
     	else {
+    		LOG_I(MAC, "Panos-D: handle_nfapi_ul_pdu_UE_MAC 2.3 \n");
     		ue_get_sdu( Mod_id, 0, frame, subframe, 0, ulsch_buffer, buflen, &access_mode);
-    		fill_crc_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, 0);
+    		//fill_crc_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, 0);
     		fill_rx_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, ulsch_buffer,buflen, rnti);
     	}
     }
   }
 
   else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE) {
+	  LOG_I(MAC, "Panos-D: handle_nfapi_ul_pdu_UE_MAC 3 \n");
     //AssertFatal((UE_id = find_ulsch(ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti,eNB,SEARCH_EXIST_OR_FREE))>=0,
     //            "No available UE ULSCH for rnti %x\n",ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti);
 	  uint8_t ulsch_buffer[5477] __attribute__ ((aligned(32)));
@@ -537,6 +554,7 @@ void handle_nfapi_ul_pdu_UE_MAC(module_id_t Mod_id,
 
   }
   else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE) {
+	  LOG_I(MAC, "Panos-D: handle_nfapi_ul_pdu_UE_MAC 4 \n");
     //AssertFatal((UE_id = find_ulsch(ul_config_pdu->ulsch_cqi_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti,
     //                                eNB,SEARCH_EXIST_OR_FREE))>=0,
     //            "No available UE ULSCH for rnti %x\n",ul_config_pdu->ulsch_cqi_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti);
@@ -562,6 +580,7 @@ void handle_nfapi_ul_pdu_UE_MAC(module_id_t Mod_id,
 
   }
   else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE) {
+	  LOG_I(MAC, "Panos-D: handle_nfapi_ul_pdu_UE_MAC 5 \n");
     //AssertFatal((UE_id = find_ulsch(ul_config_pdu->ulsch_cqi_harq_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti,
     //                                eNB,SEARCH_EXIST_OR_FREE))>=0,
     //            "No available UE ULSCH for rnti %x\n",ul_config_pdu->ulsch_cqi_harq_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti);
@@ -592,6 +611,7 @@ void handle_nfapi_ul_pdu_UE_MAC(module_id_t Mod_id,
 
   }
   else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE) {
+	  LOG_I(MAC, "Panos-D: handle_nfapi_ul_pdu_UE_MAC 6 \n");
   //  AssertFatal((UE_id = find_uci(ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.rnti,
   //                                proc->frame_tx,proc->subframe_tx,eNB,SEARCH_EXIST_OR_FREE))>=0,
   //              "No available UE UCI for rnti %x\n",ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.rnti);
@@ -612,6 +632,7 @@ void handle_nfapi_ul_pdu_UE_MAC(module_id_t Mod_id,
     AssertFatal(1==0,"NFAPI_UL_CONFIG_UCI_CQI_SR_PDU_TYPE not handled yet\n");
   }
   else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE) {
+	  LOG_I(MAC, "Panos-D: handle_nfapi_ul_pdu_UE_MAC 7 \n");
     //AssertFatal((UE_id = find_uci(ul_config_pdu->uci_sr_pdu.ue_information.ue_information_rel8.rnti,
     //                              proc->frame_tx,proc->subframe_tx,eNB,SEARCH_EXIST_OR_FREE))>=0,
     //            "No available UE UCI for rnti %x\n",ul_config_pdu->uci_sr_pdu.ue_information.ue_information_rel8.rnti);
@@ -621,6 +642,7 @@ void handle_nfapi_ul_pdu_UE_MAC(module_id_t Mod_id,
 
   }
   else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE) {
+	  LOG_I(MAC, "Panos-D: handle_nfapi_ul_pdu_UE_MAC 8 \n");
     //AssertFatal((UE_id = find_uci(rel8->rnti,proc->frame_tx,proc->subframe_tx,eNB,SEARCH_EXIST_OR_FREE))>=0,
     //            "No available UE UCI for rnti %x\n",ul_config_pdu->uci_sr_harq_pdu.ue_information.ue_information_rel8.rnti);
 
@@ -683,18 +705,20 @@ int ul_config_req_UE_MAC(nfapi_ul_config_request_t* req)
 
   module_id_t Mod_id = 0; //Panos: Currently static (only for one UE) but this should change.
   nfapi_ul_config_request_pdu_t* ul_config_pdu_list = req->ul_config_request_body.ul_config_pdu_list;
+  LOG_I(MAC, "Panos-D: ul_config_req_UE_MAC 1 \n");
 
   //Panos: Not sure whether we should put the memory allocation here.
   //*** Note we should find the right place to call free(UL_INFO).
   UL_INFO = (UL_IND_t*)malloc(sizeof(UL_IND_t));
 
   //Panos: Additional checks needed here to check if the UE is in PRACH mode.
-  uint8_t is_rach = req->ul_config_request_body.rach_prach_frequency_resources;
+
+  /*uint8_t is_rach = req->ul_config_request_body.rach_prach_frequency_resources;
   if(is_rach && UE_mac_inst[Mod_id].UE_mode[0] == PRACH) {
 	  PRACH_RESOURCES_t *prach_resources = ue_get_rach(Mod_id, 0, sfn, 0, sf);
 	  fill_rach_indication_UE_MAC(Mod_id, sfn ,sf, UL_INFO, prach_resources->ra_PreambleIndex, prach_resources->ra_RNTI);
 	  Msg1_transmitted(Mod_id, 0, sfn, 0);
-  }
+  }*/
 
 
   // subframe works off TX SFN/SF which is 4 ahead, need to put it back to RX SFN/SF
@@ -705,31 +729,34 @@ int ul_config_req_UE_MAC(nfapi_ul_config_request_t* req)
   //subtract_subframe(&sfn, &sf, 4);
 
 
+
   for (int i=0;i<req->ul_config_request_body.number_of_pdus;i++)
   {
     //NFAPI_TRACE(NFAPI_TRACE_INFO, "%s() sfn/sf:%d PDU[%d] size:%d\n", __FUNCTION__, NFAPI_SFNSF2DEC(req->sfn_sf), i, ul_config_pdu_list[i].pdu_size);
 
+	LOG_I(MAC, "Panos-D: ul_config_req_UE_MAC 2.0 \n");
     if (
-        ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_PDU_TYPE ||
-        ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE ||
-        ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE ||
-        ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE ||
-        ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE ||
-        ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE ||
-        ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE
+    		req->ul_config_request_body.ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_PDU_TYPE ||
+    		req->ul_config_request_body.ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE ||
+    		req->ul_config_request_body.ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE ||
+    		req->ul_config_request_body.ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE ||
+    		req->ul_config_request_body.ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE ||
+    		req->ul_config_request_body.ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE ||
+    		req->ul_config_request_body.ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE
        )
     {
       //NFAPI_TRACE(NFAPI_TRACE_INFO, "%s() handle_nfapi_ul_pdu() for PDU:%d\n", __FUNCTION__, i);
 
-
-      handle_nfapi_ul_pdu_UE_MAC(Mod_id,&ul_config_pdu_list[i],sfn,sf,req->ul_config_request_body.srs_present);
-      free(UL_INFO);
+    	LOG_I(MAC, "Panos-D: ul_config_req_UE_MAC 2.1 \n");
+      handle_nfapi_ul_pdu_UE_MAC(Mod_id,&req->ul_config_request_body.ul_config_pdu_list[i],sfn,sf,req->ul_config_request_body.srs_present);
+      LOG_I(MAC, "Panos-D: ul_config_req_UE_MAC 3 \n");
     }
     else
     {
       //NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s() PDU:%i UNKNOWN type :%d\n", __FUNCTION__, i, ul_config_pdu_list[i].pdu_type);
     }
   }
+  free(UL_INFO);
 
   return 0;
 }
@@ -774,6 +801,7 @@ int tx_req_UE_MAC(nfapi_tx_request_t* req)
 
 int dl_config_req_UE_MAC(nfapi_dl_config_request_t* req)
 {
+	if (req!=NULL && tx_request_pdu_list!=NULL){
 	//LOG_I(MAC, "Panos-D: dl_config_req_UE_MAC 1 \n");
   int sfn = NFAPI_SFNSF2SFN(req->sfn_sf);
   int sf = NFAPI_SFNSF2SF(req->sfn_sf);
@@ -845,7 +873,7 @@ int dl_config_req_UE_MAC(nfapi_dl_config_request_t* req)
 			}
 			else if(dl_config_pdu_tmp->pdu_type == NFAPI_DL_CONFIG_DLSCH_PDU_TYPE) {
 				// RA-RNTI case
-
+				LOG_E(MAC,"dl_config_req_UE_MAC 4 Received RAR? \n");
 				// RNTI parameter not actually used. Provided only to comply with existing function definition.
 				// Not sure about parameters to fill the preamble index.
 				//rnti_t c_rnti = UE_mac_inst[Mod_id].crnti;
@@ -853,7 +881,7 @@ int dl_config_req_UE_MAC(nfapi_dl_config_request_t* req)
 				if ((UE_mac_inst[Mod_id].UE_mode[0] != PUSCH) &&
 				  (UE_mac_inst[Mod_id].RA_prach_resources.Msg3!=NULL) &&
 				  (tx_request_pdu_list + dl_config_pdu_tmp->dlsch_pdu.dlsch_pdu_rel8.pdu_index!= NULL)) {
-
+					LOG_E(MAC,"dl_config_req_UE_MAC 5 Received RAR \n");
 					ue_process_rar(Mod_id, 0, sfn,
 							ra_rnti, //RA-RNTI
 							tx_request_pdu_list[dl_config_pdu_tmp->dlsch_pdu.dlsch_pdu_rel8.pdu_index].segments[0].segment_data,
@@ -877,6 +905,10 @@ int dl_config_req_UE_MAC(nfapi_dl_config_request_t* req)
     	// for our case.
     	LOG_E(MAC,"dl_config_req_UE_MAC 4 Received MIB: sfn/sf: %d.%d \n", sfn, sf);
     	dl_phy_sync_success(Mod_id,sfn,0, 0);
+    	if(UE_mac_inst[Mod_id].UE_mode[0] == NOT_SYNCHED){
+    		LOG_E(MAC,"dl_config_req_UE_MAC 5 Received MIB: UE_mode: %d\n", UE_mac_inst[Mod_id].UE_mode[0]);
+    		UE_mac_inst[Mod_id].UE_mode[0]=PRACH;
+    	}
 
     }
 
@@ -890,11 +922,26 @@ int dl_config_req_UE_MAC(nfapi_dl_config_request_t* req)
   if(req->vendor_extension)
     free(req->vendor_extension);
 
-  /*if(tx_request_pdu_list)
-	  free(tx_request_pdu_list);
-  free(req);*/
+  /*if(req->vendor_extension!=NULL){
+    free(req->vendor_extension);
+    req->vendor_extension = NULL;
+  }*/
 
+  if(tx_request_pdu_list!=NULL){
+	  free(tx_request_pdu_list);
+  	  tx_request_pdu_list = NULL;
+  }
+  free(req);
+  req = NULL;
   return 0;
+	}
+	else {
+		return -1;
+	}
+
+
+
+
 }
 
 
@@ -961,8 +1008,15 @@ int memcpy_dl_config_req (nfapi_pnf_p7_config_t* pnf_p7, nfapi_dl_config_request
 	LOG_I(MAC, "Panos-D: memcpy_dl_config_req 1 \n");
 
 
+	//UE_mac_inst[Mod_id].dl_config_req->header = req->header;
 	UE_mac_inst[Mod_id].dl_config_req->sfn_sf = req->sfn_sf;
+	//vendor_extension = &UE_mac_inst[Mod_id].dl_config_req->vendor_extension;
+	//vendor_extension->tag = req->vendor_extension.tag;
+	//vendor_extension->length = req->vendor_extension.length;
+
 	UE_mac_inst[Mod_id].dl_config_req->vendor_extension = req->vendor_extension;
+	//UE_mac_inst[Mod_id].dl_config_req->vendor_extension.tag = req->vendor_extension.tag;
+	//UE_mac_inst[Mod_id].dl_config_req->vendor_extension.length = req->vendor_extension.length;
 
 	UE_mac_inst[Mod_id].dl_config_req->dl_config_request_body.number_dci = req->dl_config_request_body.number_dci;
 	UE_mac_inst[Mod_id].dl_config_req->dl_config_request_body.number_pdcch_ofdm_symbols = req->dl_config_request_body.number_pdcch_ofdm_symbols;
@@ -983,8 +1037,34 @@ int memcpy_dl_config_req (nfapi_pnf_p7_config_t* pnf_p7, nfapi_dl_config_request
 int memcpy_ul_config_req (nfapi_pnf_p7_config_t* pnf_p7, nfapi_ul_config_request_t* req)
 {
 	LOG_I(MAC, "Panos-D: memcpy_ul_config_req 1 \n");
+
 	module_id_t Mod_id = 0; //Panos: Currently static (only for one UE) but this should change.
-	UE_mac_inst[Mod_id].ul_config_req = req;
+
+	UE_mac_inst[Mod_id].ul_config_req = (nfapi_ul_config_request_t*)malloc(sizeof(nfapi_ul_config_request_t));
+	//UE_mac_inst[Mod_id].ul_config_req->header = req->header;
+	UE_mac_inst[Mod_id].ul_config_req->sfn_sf = req->sfn_sf;
+	UE_mac_inst[Mod_id].ul_config_req->vendor_extension = req->vendor_extension;
+	//UE_mac_inst[Mod_id].ul_config_req->vendor_extension->tag = req->vendor_extension.tag;
+	//UE_mac_inst[Mod_id].ul_config_req->vendor_extension->length = req->vendor_extension.length;
+
+	LOG_I(MAC, "Panos-D: memcpy_ul_config_req SFN_SF: MINE: %d \n", UE_mac_inst[Mod_id].ul_config_req->sfn_sf);
+	LOG_I(MAC, "Panos-D: memcpy_ul_config_req SFN_SF: REQ: %d \n", req->sfn_sf);
+
+	UE_mac_inst[Mod_id].ul_config_req->ul_config_request_body.number_of_pdus = req->ul_config_request_body.number_of_pdus;
+	UE_mac_inst[Mod_id].ul_config_req->ul_config_request_body.rach_prach_frequency_resources = req->ul_config_request_body.rach_prach_frequency_resources;
+	UE_mac_inst[Mod_id].ul_config_req->ul_config_request_body.srs_present = req->ul_config_request_body.srs_present;
+	UE_mac_inst[Mod_id].ul_config_req->ul_config_request_body.tl = req->ul_config_request_body.tl;
+
+	LOG_I(MAC, "Panos-D: memcpy_ul_config_req 1 #ofULPDUs: %d \n", UE_mac_inst[Mod_id].ul_config_req->ul_config_request_body.number_of_pdus); //req->ul_config_request_body.number_of_pdus);
+	UE_mac_inst[Mod_id].ul_config_req->ul_config_request_body.ul_config_pdu_list = (nfapi_ul_config_request_pdu_t*) malloc(req->ul_config_request_body.number_of_pdus*sizeof(nfapi_ul_config_request_pdu_t));
+	for(int i=0; i<UE_mac_inst[Mod_id].ul_config_req->ul_config_request_body.number_of_pdus; i++) {
+			UE_mac_inst[Mod_id].ul_config_req->ul_config_request_body.ul_config_pdu_list[i] = req->ul_config_request_body.ul_config_pdu_list[i];
+			LOG_I(MAC, "Panos-D: memcpy_ul_config_req TAG MINE: %d \n", UE_mac_inst[Mod_id].ul_config_req->ul_config_request_body.ul_config_pdu_list[i].pdu_type);
+			LOG_I(MAC, "Panos-D: memcpy_ul_config_req TAG REQ: %d \n", req->ul_config_request_body.ul_config_pdu_list[i].pdu_type);
+		}
+
+
+	//UE_mac_inst[Mod_id].ul_config_req = req;
 	return 0;
 }
 
diff --git a/targets/RT/USER/lte-ue.c b/targets/RT/USER/lte-ue.c
index bf8b46176bf..4cbae474186 100644
--- a/targets/RT/USER/lte-ue.c
+++ b/targets/RT/USER/lte-ue.c
@@ -955,13 +955,38 @@ static void *UE_phy_stub_thread_rxn_txnp4(void *arg) {
         if ((subframe_select( &UE->frame_parms, proc->subframe_tx) == SF_UL) ||
 	    (UE->frame_parms.frame_type == FDD) )
             if (UE->mode != loop_through_memory){
+
+            	if ((UE_mac_inst[Mod_id].UE_mode[0] == PRACH) ) {
+            		LOG_I(MAC, "Panos-D: UE_phy_stub_thread_rxn_txnp4 before RACH \n");
+
+            	    // check if we have PRACH opportunity
+
+            	    if (is_prach_subframe(&UE->frame_parms,proc->frame_tx, proc->subframe_tx)) {
+            	    	LOG_I(MAC, "Panos-D: UE_phy_stub_thread_rxn_txnp4 before RACH 2 \n");
+            	    	PRACH_RESOURCES_t *prach_resources = ue_get_rach(Mod_id, 0, proc->frame_tx, 0, proc->subframe_tx);
+            	    	if(prach_resources!=NULL) {
+            	    		LOG_I(MAC, "Panos-D: UE_phy_stub_thread_rxn_txnp4 before RACH 3 \n");
+            	    		fill_rach_indication_UE_MAC(Mod_id, proc->frame_tx ,proc->subframe_tx, UL_INFO, prach_resources->ra_PreambleIndex, prach_resources->ra_RNTI);
+            	    		Msg1_transmitted(Mod_id, 0, proc->frame_tx, 0);
+            	    		UE_mac_inst[Mod_id].UE_mode[0] = RA_RESPONSE;
+            	    	}
+
+            	    	//ue_prach_procedures(ue,proc,eNB_id,abstraction_flag,mode);
+            	    }
+            	  } // mode is PRACH
+
             	// Panos: Substitute call to phy_procedures Tx with call to phy_stub functions in order to trigger
                 // UE Tx procedures directly at the MAC layer, based on the received ul_config requests from the vnf (eNB).
             	// Generate UL_indications which corresponf to UL traffic.
             	if(UE_mac_inst[Mod_id].ul_config_req!= NULL){
+            		LOG_I(MAC, "Panos-D: UE_phy_stub_thread_rxn_txnp4 ul_config_req is not NULL \n");
             		ul_config_req_UE_MAC(UE_mac_inst[Mod_id].ul_config_req);
-            		UL_indication(UL_INFO);
+            		//UL_indication(UL_INFO);
             	}
+            	/*else{
+            		AssertFatal(UE_mac_inst[Mod_id].ul_config_req!= NULL, "Panos-D: Copy of ul_config_req is NULL");
+            	}*/
+
             }
 
 
-- 
GitLab