From 104ed9ade1634286bb981810dd38599b72b14d64 Mon Sep 17 00:00:00 2001
From: Xenofon Foukas <x.foukas@sms.ed.ac.uk>
Date: Mon, 25 Apr 2016 17:09:53 +0100
Subject: [PATCH] Added fixes for multiple eNBs and simulation mode

---
 openair2/ENB_APP/enb_agent_common.c  | 17 ++++-----
 openair2/RRC/LITE/rrc_eNB.c          | 15 +++++---
 openair2/UTIL/OTG/otg_tx.c           |  5 ++-
 targets/SIMU/USER/oaisim.c           |  5 +--
 targets/SIMU/USER/oaisim_functions.c | 55 ++++++++++++++--------------
 5 files changed, 50 insertions(+), 47 deletions(-)

diff --git a/openair2/ENB_APP/enb_agent_common.c b/openair2/ENB_APP/enb_agent_common.c
index e3c2bcfcb5..1e477c6c3c 100644
--- a/openair2/ENB_APP/enb_agent_common.c
+++ b/openair2/ENB_APP/enb_agent_common.c
@@ -1418,6 +1418,9 @@ int enb_agent_ue_state_change(mid_t mod_id, uint32_t rnti, uint8_t state_change)
 	  		  config->has_extended_bsr_size = 1;
 	  		  config->extended_bsr_size = get_extended_bsr_size(mod_id,i);
 	  	  }
+		  //TODO: Set index of primary cell
+		  config->has_pcell_carrier_index = 1;
+		  config->pcell_carrier_index = UE_PCCID(mod_id, i);
 	  	  //TODO: Set carrier aggregation support (boolean)
 	  	  config->has_ca_support = 0;
 	  	  config->ca_support = 0;
@@ -1425,9 +1428,6 @@ int enb_agent_ue_state_change(mid_t mod_id, uint32_t rnti, uint8_t state_change)
 	  		  //TODO: Set cross carrier scheduling support (boolean)
 	  		  config->has_cross_carrier_sched_support = 1;
 	  		  config->cross_carrier_sched_support = 0;
-	  		  //TODO: Set index of primary cell
-	  		  config->has_pcell_carrier_index = 1;
-	  		  config->pcell_carrier_index = 1;
 	  		  //TODO: Set secondary cells configuration
 	  		  // We do not set it for now. No carrier aggregation support
 
@@ -1754,16 +1754,15 @@ int enb_agent_ue_config_reply(mid_t mod_id, const void *params, Protocol__Progra
       //TODO: Set carrier aggregation support (boolean)
       ue_config[i]->has_ca_support = 0;
       ue_config[i]->ca_support = 0;
+      //TODO: Set index of primary cell
+      ue_config[i]->has_pcell_carrier_index = 1;
+      ue_config[i]->pcell_carrier_index = UE_PCCID(mod_id, i);
       if(ue_config[i]->has_ca_support){
 	//TODO: Set cross carrier scheduling support (boolean)
 	ue_config[i]->has_cross_carrier_sched_support = 1;
 	ue_config[i]->cross_carrier_sched_support = 0;
-	//TODO: Set index of primary cell
-	ue_config[i]->has_pcell_carrier_index = 1;
-	ue_config[i]->pcell_carrier_index = 1;
 	//TODO: Set secondary cells configuration
 	// We do not set it for now. No carrier aggregation support
-	
 	//TODO: Set deactivation timer for secondary cell
 	ue_config[i]->has_scell_deactivation_timer = 1;
 	ue_config[i]->scell_deactivation_timer = 1;
@@ -1874,9 +1873,9 @@ int enb_agent_enb_config_reply(mid_t mod_id, const void *params, Protocol__Progr
       protocol__prp_cell_config__init(cell_conf[i]);
       //TODO: Fill in with actual value, the PCI of this cell
       cell_conf[i]->phy_cell_id = 1;
-      cell_conf[i]->has_phy_cell_id = 1;
+      cell_conf[i]->has_phy_cell_id = get_cell_id(enb_id,i);
       //TODO: Fill in with actual value, the PLMN cell id of this cell
-      cell_conf[i]->cell_id = get_cell_id(enb_id,i);
+      cell_conf[i]->cell_id = i;
       cell_conf[i]->has_cell_id = 1;
       //TODO: Fill in with actual value, PUSCH resources in RBs for hopping
       cell_conf[i]->pusch_hopping_offset = get_hopping_offset(enb_id,i);
diff --git a/openair2/RRC/LITE/rrc_eNB.c b/openair2/RRC/LITE/rrc_eNB.c
index ed8d4cd850..4c0bb3f5c8 100644
--- a/openair2/RRC/LITE/rrc_eNB.c
+++ b/openair2/RRC/LITE/rrc_eNB.c
@@ -644,6 +644,9 @@ void rrc_eNB_emulation_notify_ue_module_id(
 
   // find enb_module_id
   for (enb_module_id = 0; enb_module_id < NUMBER_OF_eNB_MAX; enb_module_id++) {
+    if(enb_module_id>1){ /*FIX LATER*/
+      return;
+    }
     for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
       if (eNB_rrc_inst[enb_module_id].carrier[CC_id].sib1 != NULL) {
         if (
@@ -652,7 +655,6 @@ void rrc_eNB_emulation_notify_ue_module_id(
           (eNB_rrc_inst[enb_module_id].carrier[CC_id].sib1->cellAccessRelatedInfo.cellIdentity.buf[2] == cell_identity_byte2P) &&
           (eNB_rrc_inst[enb_module_id].carrier[CC_id].sib1->cellAccessRelatedInfo.cellIdentity.buf[3] == cell_identity_byte3P)
         ) {
-          oai_emulation.info.eNB_ue_module_id_to_rnti[enb_module_id][ue_module_idP] = rntiP;
           ue_context_p = rrc_eNB_get_ue_context(
                            &eNB_rrc_inst[enb_module_id],
                            rntiP
@@ -662,10 +664,11 @@ void rrc_eNB_emulation_notify_ue_module_id(
             oai_emulation.info.eNB_ue_local_uid_to_ue_module_id[enb_module_id][ue_context_p->local_uid] = ue_module_idP;
           }
 
-          return;
+          //return;
         }
       }
     }
+    oai_emulation.info.eNB_ue_module_id_to_rnti[enb_module_id][ue_module_idP] = rntiP;
   }
 
   AssertFatal(enb_module_id == NUMBER_OF_eNB_MAX,
@@ -4026,8 +4029,8 @@ rrc_eNB_decode_dcch(
               PROTOCOL_RRC_CTXT_UE_FMT" UE State = RRC_RECONFIGURED \n",
               PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP));
 	//WARNING:Inform the controller about the UE activation. Should be moved to RRC agent in the future
-	if (mac_agent_registered[ctxt_pP->eNB_index]) {
-	  agent_mac_xface[ctxt_pP->eNB_index]->enb_agent_notify_ue_state_change(ctxt_pP->eNB_index,
+	if (mac_agent_registered[ctxt_pP->module_id]) {
+	  agent_mac_xface[ctxt_pP->eNB_index]->enb_agent_notify_ue_state_change(ctxt_pP->module_id,
 										ue_context_p->ue_id_rnti,
 										PROTOCOL__PRP_UE_STATE_CHANGE_TYPE__PRUESC_UPDATED);
 	}
@@ -4116,8 +4119,8 @@ rrc_eNB_decode_dcch(
           LOG_I(RRC, PROTOCOL_RRC_CTXT_UE_FMT" UE State = RRC_CONNECTED \n",
                 PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP));
 	  //WARNING:Inform the controller about the UE activation. Should be moved to RRC agent in the future
-	  if (mac_agent_registered[ctxt_pP->eNB_index]) {
-	    agent_mac_xface[ctxt_pP->eNB_index]->enb_agent_notify_ue_state_change(ctxt_pP->eNB_index,
+	  if (mac_agent_registered[ctxt_pP->module_id]) {
+	    agent_mac_xface[ctxt_pP->eNB_index]->enb_agent_notify_ue_state_change(ctxt_pP->module_id,
 										  ue_context_p->ue_id_rnti,
 										  PROTOCOL__PRP_UE_STATE_CHANGE_TYPE__PRUESC_ACTIVATED);
 	  }
diff --git a/openair2/UTIL/OTG/otg_tx.c b/openair2/UTIL/OTG/otg_tx.c
index e70bccbc73..ea5fad63dc 100644
--- a/openair2/UTIL/OTG/otg_tx.c
+++ b/openair2/UTIL/OTG/otg_tx.c
@@ -270,13 +270,14 @@ unsigned char *packet_gen(
   char *header=NULL;
   int header_size = 0;
 
-
+  //LOG_I(OTG,"Src: %d - Dest: %d - before exit\n",src_instance,dst_instance);
   // check if the app is configured
   if (app >= g_otg->application_idx[src_instance][dst_instance]) {
+    //LOG_I(OTG,"Src: %d - Dest: %d - no packet to generate - no application\n",src_instance,dst_instance);
     return NULL;
   }
 
-  LOG_T(OTG,"[src %d] [dst %d ][APP %d] current time  %d\n",src_instance,  dst_instance, app, ctime);
+  //LOG_I(OTG,"[src %d] [dst %d ][APP %d] current time  %d\n",src_instance,  dst_instance, app, ctime);
 
   *pkt_size=0;
   init_packet_gen(src_instance, dst_instance,ctime);
diff --git a/targets/SIMU/USER/oaisim.c b/targets/SIMU/USER/oaisim.c
index 995516b6e8..c574527f62 100644
--- a/targets/SIMU/USER/oaisim.c
+++ b/targets/SIMU/USER/oaisim.c
@@ -498,7 +498,6 @@ l2l1_task (void *args_p)
     xargv[0] = xname;
     fl_initialize (&xargc, xargv, NULL, 0, 0);
     eNB_inst = 0;
-    
     for (UE_inst = 0; UE_inst < NB_UE_INST; UE_inst++) {
       for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
 	// DL scope at UEs
@@ -849,7 +848,7 @@ l2l1_task (void *args_p)
                   update_otg_UE (UE_inst, oai_emulation.info.time_ms);
 
                   //Access layer
-		  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, UE_inst, 0, ENB_FLAG_NO, NOT_A_RNTI, frame, next_slot);
+		  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, UE_inst, ENB_FLAG_NO, NOT_A_RNTI, frame, next_slot>>1, 0);
                   pdcp_run (&ctxt);
 #endif
 
@@ -857,7 +856,7 @@ l2l1_task (void *args_p)
                        CC_id++) {
                     phy_procedures_UE_lte (
                       PHY_vars_UE_g[UE_inst][CC_id],
-                      0, abstraction_flag,
+		      0, abstraction_flag,
                       normal_txrx, no_relay,
                       NULL);
                   }
diff --git a/targets/SIMU/USER/oaisim_functions.c b/targets/SIMU/USER/oaisim_functions.c
index 72153c3ee7..9a072ea2c4 100644
--- a/targets/SIMU/USER/oaisim_functions.c
+++ b/targets/SIMU/USER/oaisim_functions.c
@@ -1326,13 +1326,11 @@ void update_otg_eNB(module_id_t enb_module_idP, unsigned int ctime)
       // generate traffic if the ue is rrc reconfigured state
       //if ((rrc_state=mac_eNB_get_rrc_status(enb_module_idP, dst_id)) > 2 /*RRC_CONNECTED*/ ) {
       if (mac_eNB_get_rrc_status(enb_module_idP, oai_emulation.info.eNB_ue_module_id_to_rnti[enb_module_idP][dst_id]) > 2 ){ 
-	
 	if_times += 1;
 	
         for (app_id=0; app_id<MAX_NUM_APPLICATION; app_id++) {
           otg_pkt = malloc (sizeof(Packet_otg_elt_t));
 
-
           (otg_pkt->otg_pkt).sdu_buffer = (uint8_t*) packet_gen(enb_module_idP, dst_id + NB_eNB_INST, app_id, ctime, &((otg_pkt->otg_pkt).sdu_buffer_size));
 
           if ((otg_pkt->otg_pkt).sdu_buffer != NULL) {
@@ -1459,6 +1457,7 @@ void update_otg_UE(module_id_t ue_mod_idP, unsigned int ctime)
 {
 #if defined(USER_MODE) && defined(OAI_EMU)
 
+  int app_id;
   if (oai_emulation.info.otg_enabled ==1 ) {
     module_id_t dst_id, src_id; //dst_id = eNB_index
     module_id_t module_id = ue_mod_idP+NB_eNB_INST;
@@ -1468,31 +1467,33 @@ void update_otg_UE(module_id_t ue_mod_idP, unsigned int ctime)
     for (dst_id=0; dst_id<NB_SIG_CNX_UE; dst_id++) {
       // only consider the first attached eNB
       if (mac_UE_get_rrc_status(ue_mod_idP, dst_id ) > 2 /*RRC_CONNECTED*/) {
-        Packet_otg_elt_t *otg_pkt = malloc (sizeof(Packet_otg_elt_t));
-
-        if (otg_pkt!=NULL)
-          memset(otg_pkt,0,sizeof(Packet_otg_elt_t));
-        else {
-          LOG_E(OTG,"not enough memory\n");
-          exit(-1);
-        }// Manage to add this packet to the tail of your list
-
-        (otg_pkt->otg_pkt).sdu_buffer = (uint8_t*) packet_gen(src_id, dst_id, 0, ctime, &((otg_pkt->otg_pkt).sdu_buffer_size));
-
-        if ((otg_pkt->otg_pkt).sdu_buffer != NULL) {
-          (otg_pkt->otg_pkt).rb_id     = DTCH-2;
-          (otg_pkt->otg_pkt).module_id = module_id;
-          (otg_pkt->otg_pkt).dst_id    = dst_id;
-          (otg_pkt->otg_pkt).is_ue     = 1;
-          //Adding the packet to the OTG-PDCP buffer
-          (otg_pkt->otg_pkt).mode      = PDCP_TRANSMISSION_MODE_DATA;
-          pkt_list_add_tail_eurecom(otg_pkt, &(otg_pdcp_buffer[module_id]));
-          LOG_I(EMU, "[UE %d] ADD pkt to OTG buffer with size %d for dst %d on rb_id %d \n",
-                (otg_pkt->otg_pkt).module_id, otg_pkt->otg_pkt.sdu_buffer_size, (otg_pkt->otg_pkt).dst_id,(otg_pkt->otg_pkt).rb_id);
-        } else {
-          free(otg_pkt);
-          otg_pkt=NULL;
-        }
+        for (app_id=0; app_id<MAX_NUM_APPLICATION; app_id++) {
+	  Packet_otg_elt_t *otg_pkt = malloc (sizeof(Packet_otg_elt_t));
+	  
+	  if (otg_pkt!=NULL)
+	    memset(otg_pkt,0,sizeof(Packet_otg_elt_t));
+	  else {
+	    LOG_E(OTG,"not enough memory\n");
+	    exit(-1);
+	  }// Manage to add this packet to the tail of your list
+	  
+	  (otg_pkt->otg_pkt).sdu_buffer = (uint8_t*) packet_gen(src_id, dst_id, app_id, ctime, &((otg_pkt->otg_pkt).sdu_buffer_size));
+	  
+	  if ((otg_pkt->otg_pkt).sdu_buffer != NULL) {
+	    (otg_pkt->otg_pkt).rb_id     = DTCH-2;
+	    (otg_pkt->otg_pkt).module_id = module_id;
+	    (otg_pkt->otg_pkt).dst_id    = dst_id;
+	    (otg_pkt->otg_pkt).is_ue     = 1;
+	    //Adding the packet to the OTG-PDCP buffer
+	    (otg_pkt->otg_pkt).mode      = PDCP_TRANSMISSION_MODE_DATA;
+	    pkt_list_add_tail_eurecom(otg_pkt, &(otg_pdcp_buffer[module_id]));
+	    LOG_I(EMU, "[UE %d] ADD pkt to OTG buffer with size %d for dst %d on rb_id %d \n",
+		  (otg_pkt->otg_pkt).module_id, otg_pkt->otg_pkt.sdu_buffer_size, (otg_pkt->otg_pkt).dst_id,(otg_pkt->otg_pkt).rb_id);
+	  } else {
+	    free(otg_pkt);
+	    otg_pkt=NULL;
+	  }
+	}
       }
     }
   }
-- 
2.26.2