From 3ad4d26fd1f619ef98f3fa94f41e5973ce537493 Mon Sep 17 00:00:00 2001
From: Xenofon Foukas <x.foukas@sms.ed.ac.uk>
Date: Fri, 22 Jan 2016 16:26:06 +0000
Subject: [PATCH] Changed the structures for storing agent context info

---
 openair2/ENB_APP/enb_agent.c              | 103 +++++-----------------
 openair2/ENB_APP/enb_agent_common.c       |   4 +-
 openair2/ENB_APP/enb_agent_defs.h         |  29 ++----
 openair2/ENB_APP/enb_agent_extern.h       |   7 +-
 openair2/ENB_APP/enb_agent_mac_defs.h     |  10 ++-
 openair2/ENB_APP/enb_agent_mac_internal.h |   5 +-
 6 files changed, 47 insertions(+), 111 deletions(-)

diff --git a/openair2/ENB_APP/enb_agent.c b/openair2/ENB_APP/enb_agent.c
index fa24cacf8a..c48f8f67fb 100644
--- a/openair2/ENB_APP/enb_agent.c
+++ b/openair2/ENB_APP/enb_agent.c
@@ -37,6 +37,7 @@
 #include "enb_agent_common.h"
 #include "log.h"
 #include "enb_agent.h"
+#include "enb_agent_mac_defs.h"
 
 #include "enb_agent_extern.h"
 
@@ -47,10 +48,7 @@
 
 //#define TEST_TIMER
 
-enb_agent_instance_t enb_agent[NUM_MAX_ENB_AGENT];
-msg_context_t shared_ctxt[NUM_MAX_ENB_AGENT];
-/* this could also go into enb_agent struct*/ 
-enb_agent_info_t  enb_agent_info;
+enb_agent_instance_t enb_agent[NUM_MAX_ENB];
 
 char in_ip[40];
 static uint16_t in_port;
@@ -66,7 +64,7 @@ Protocol__ProgranMessage *enb_agent_timeout(void* args);
 */
 void *enb_agent_task(void *args){
 
-  msg_context_t         *d = (msg_context_t *) args;
+  enb_agent_instance_t         *d = (enb_agent_instance_t *) args;
   Protocol__ProgranMessage *msg;
   void *data;
   int size;
@@ -101,15 +99,11 @@ void *enb_agent_task(void *args){
       msg = enb_agent_process_timeout(msg_p->ittiMsg.timer_has_expired.timer_id, msg_p->ittiMsg.timer_has_expired.arg);
       if (msg != NULL){
 	data=enb_agent_pack_message(msg,&size);
-	if (enb_agent_msg_send(d->mod_id, ENB_AGENT_DEFAULT, data, size, priority)) {
+	if (enb_agent_msg_send(d->enb_id, ENB_AGENT_DEFAULT, data, size, priority)) {
 	  err_code = PROTOCOL__PROGRAN_ERR__MSG_ENQUEUING;
 	  goto error;
 	}
 
-	/* if (message_put(d->tx_mq, data, size, priority)){ */
-	/*   err_code = PROTOCOL__PROGRAN_ERR__MSG_ENQUEUING; */
-	/*   goto error; */
-	/* } */
 	LOG_D(ENB_AGENT,"sent message with size %d\n", size);
       }
       break;
@@ -131,7 +125,7 @@ void *enb_agent_task(void *args){
 
 void *receive_thread(void *args) {
 
-  msg_context_t         *d = args;
+  enb_agent_instance_t         *d = args;
   void                  *data;
   int                   size;
   int                   priority;
@@ -140,38 +134,27 @@ void *receive_thread(void *args) {
   Protocol__ProgranMessage *msg;
   
   while (1) {
-    if (enb_agent_msg_recv(d->mod_id, ENB_AGENT_DEFAULT, &data, &size, &priority)) {
+    if (enb_agent_msg_recv(d->enb_id, ENB_AGENT_DEFAULT, &data, &size, &priority)) {
       err_code = PROTOCOL__PROGRAN_ERR__MSG_DEQUEUING;
       goto error;
     }
 
-    /* if (message_get(d->rx_mq, &data, &size, &priority)){ */
-    /*   err_code = PROTOCOL__PROGRAN_ERR__MSG_DEQUEUING; */
-    /*   goto error; */
-    /* } */
     LOG_D(ENB_AGENT,"received message with size %d\n", size);
   
     
-    msg=enb_agent_handle_message(d->mod_id, data, size);
+    msg=enb_agent_handle_message(d->enb_id, data, size);
 
     free(data);
     
-    d->rx_xid = ((d->rx_xid)+1)%4;
-    d->tx_xid = d->rx_xid;
-  
     // check if there is something to send back to the controller
     if (msg != NULL){
       data=enb_agent_pack_message(msg,&size);
      
-      if (enb_agent_msg_send(d->mod_id, ENB_AGENT_DEFAULT, data, size, priority)) {
+      if (enb_agent_msg_send(d->enb_id, ENB_AGENT_DEFAULT, data, size, priority)) {
 	err_code = PROTOCOL__PROGRAN_ERR__MSG_ENQUEUING;
 	goto error;
       }
       
-      /* if (message_put(d->tx_mq, data, size, priority)){ */
-      /* 	err_code = PROTOCOL__PROGRAN_ERR__MSG_ENQUEUING; */
-      /* 	goto error; */
-      /* } */
       LOG_D(ENB_AGENT,"sent message with size %d\n", size);
     }
     
@@ -214,10 +197,8 @@ int enb_agent_start(mid_t mod_id, const Enb_properties_array_t* enb_properties){
   
   int channel_id;
   
-  // 
   set_enb_vars(mod_id, RAN_LTE_OAI);
-  enb_agent[mod_id].mod_id = mod_id;
-  enb_agent_info.nb_modules+=1;
+  enb_agent[mod_id].enb_id = mod_id;
   
   /* 
    * check the configuration
@@ -235,7 +216,7 @@ int enb_agent_start(mid_t mod_id, const Enb_properties_array_t* enb_properties){
     in_port = DEFAULT_ENB_AGENT_PORT ;
   }
   LOG_I(ENB_AGENT,"starting enb agent client for module id %d on ipv4 %s, port %d\n",  
-	enb_agent[mod_id].mod_id,
+	enb_agent[mod_id].enb_id,
 	in_ip,
 	in_port);
 
@@ -271,46 +252,10 @@ int enb_agent_start(mid_t mod_id, const Enb_properties_array_t* enb_properties){
    *enb_agent_register_channel(mod_id, channel, ENB_AGENT_MAC);
    */
 
-  /* /\*  */
-  /*  * create a socket  */
-  /*  *\/  */
-  /* enb_agent[mod_id].link = new_link_client(in_ip, in_port); */
-  /* if (enb_agent[mod_id].link == NULL) goto error; */
-  
-  /* LOG_I(ENB_AGENT,"starting enb agent client for module id %d on ipv4 %s, port %d\n",   */
-  /* 	enb_agent[mod_id].mod_id, */
-  /* 	in_ip, */
-  /* 	in_port); */
-  /* /\*  */
-  /*  * create a message queue */
-  /*  *\/  */
+  /*Initialize the continuous MAC stats update mechanism*/
+  enb_agent_init_cont_mac_stats_update(mod_id);
   
-  /* enb_agent[mod_id].send_queue = new_message_queue(); */
-  /* if (enb_agent[mod_id].send_queue == NULL) goto error; */
-  /* enb_agent[mod_id].receive_queue = new_message_queue(); */
-  /* if (enb_agent[mod_id].receive_queue == NULL) goto error; */
-  
-  /* /\*  */
-  /*  * create a link manager  */
-  /*  *\/  */
-  
-  /* enb_agent[mod_id].manager = create_link_manager(enb_agent[mod_id].send_queue, enb_agent[mod_id].receive_queue, enb_agent[mod_id].link); */
-  /* if (enb_agent[mod_id].manager == NULL) goto error; */
-
-  /* memset(&shared_ctxt, 0, sizeof(msg_context_t)); */
-  
-  /* shared_ctxt[mod_id].mod_id = mod_id; */
-  /* shared_ctxt[mod_id].tx_mq =  enb_agent[mod_id].send_queue; */
-  /* shared_ctxt[mod_id].rx_mq =  enb_agent[mod_id].receive_queue; */
-
-  /* 
-   * start the enb agent rx thread 
-   */ 
-  
-  memset(&shared_ctxt, 0, sizeof(msg_context_t));
-  shared_ctxt[mod_id].mod_id = mod_id;
-  
-  new_thread(receive_thread, &shared_ctxt[mod_id]);
+  new_thread(receive_thread, &enb_agent[mod_id]);
   
   /* 
    * initilize a timer 
@@ -322,7 +267,7 @@ int enb_agent_start(mid_t mod_id, const Enb_properties_array_t* enb_properties){
    * start the enb agent task for tx and interaction with the underlying network function
    */ 
   
-  if (itti_create_task (TASK_ENB_AGENT, enb_agent_task, (void *) &shared_ctxt[mod_id]) < 0) {
+  if (itti_create_task (TASK_ENB_AGENT, enb_agent_task, (void *) &enb_agent[mod_id]) < 0) {
     LOG_E(ENB_AGENT, "Create task for eNB Agent failed\n");
     return -1;
   } 
@@ -338,21 +283,21 @@ error:
 
 
 
-int enb_agent_stop(mid_t mod_id){
+/* int enb_agent_stop(mid_t mod_id){ */
   
-  int i=0;
+/*   int i=0; */
 
-  enb_agent_destroy_timers();
-  for ( i =0; i < enb_agent_info.nb_modules; i++) {
+/*   enb_agent_destroy_timers(); */
+/*   for ( i =0; i < enb_agent_info.nb_modules; i++) { */
   
-    destroy_link_manager(enb_agent[i].manager);
+/*     destroy_link_manager(enb_agent[i].manager); */
   
-    destroy_message_queue(enb_agent[i].send_queue);
-    destroy_message_queue(enb_agent[i].receive_queue);
+/*     destroy_message_queue(enb_agent[i].send_queue); */
+/*     destroy_message_queue(enb_agent[i].receive_queue); */
   
-    close_link(enb_agent[i].link);
-  }
-}
+/*     close_link(enb_agent[i].link); */
+/*   } */
+/* } */
 
 
 
diff --git a/openair2/ENB_APP/enb_agent_common.c b/openair2/ENB_APP/enb_agent_common.c
index ce838766ee..ab9546036f 100644
--- a/openair2/ENB_APP/enb_agent_common.c
+++ b/openair2/ENB_APP/enb_agent_common.c
@@ -39,8 +39,8 @@
 #include "log.h"
 
 
-void * enb[NUM_MAX_ENB_AGENT];
-void * enb_ue[NUM_MAX_ENB_AGENT];
+void * enb[NUM_MAX_ENB];
+void * enb_ue[NUM_MAX_ENB];
 /*
  * message primitives
  */
diff --git a/openair2/ENB_APP/enb_agent_defs.h b/openair2/ENB_APP/enb_agent_defs.h
index 0ddbc2064e..020c631ff1 100644
--- a/openair2/ENB_APP/enb_agent_defs.h
+++ b/openair2/ENB_APP/enb_agent_defs.h
@@ -43,7 +43,7 @@
 
 #include "link_manager.h"
 
-#define NUM_MAX_ENB_AGENT 2
+#define NUM_MAX_ENB 2
 #define NUM_MAX_UE 2048
 #define DEFAULT_ENB_AGENT_IPv4_ADDRESS "127.0.0.1"
 #define DEFAULT_ENB_AGENT_PORT          2210
@@ -108,36 +108,25 @@ typedef uint8_t mid_t;  // module or enb id
 typedef uint8_t lcid_t;
 typedef int32_t  err_code_t; 
 
-// tx and rx shared context 
-typedef struct {
-  mid_t            mod_id;
-  socket_link_t   *link;
-  message_queue_t *send_queue;
-  message_queue_t *receive_queue;
-  link_manager_t  *manager;
-}enb_agent_instance_t;
+
 
 typedef struct {
   /* general info */ 
-  uint32_t   nb_modules;
-  
-
+ 
   /* stats */
 
   uint32_t total_rx_msg;
   uint32_t total_tx_msg;
    
-  uint32_t rx_msg[NUM_MAX_ENB_AGENT];
-  uint32_t tx_msg[NUM_MAX_ENB_AGENT];
+  uint32_t rx_msg[NUM_MAX_ENB];
+  uint32_t tx_msg[NUM_MAX_ENB];
 
 }enb_agent_info_t;
 
 typedef struct {
-  mid_t                  mod_id;
-  // message_queue_t       *tx_mq;
-  // message_queue_t       *rx_mq;
-  xid_t                  tx_xid;
-  xid_t                  rx_xid; 
-} msg_context_t;
+  mid_t enb_id;
+  enb_agent_info_t agent_info;
+  
+}enb_agent_instance_t;
 
 #endif 
diff --git a/openair2/ENB_APP/enb_agent_extern.h b/openair2/ENB_APP/enb_agent_extern.h
index 2cd072f25a..f191d86907 100644
--- a/openair2/ENB_APP/enb_agent_extern.h
+++ b/openair2/ENB_APP/enb_agent_extern.h
@@ -41,10 +41,11 @@
 #include "enb_agent_defs.h"
 #include "enb_agent_mac_defs.h"
 
-extern msg_context_t shared_ctxt[NUM_MAX_ENB_AGENT];
 
-extern AGENT_MAC_xface *agent_mac_xface;
+//extern msg_context_t shared_ctxt[NUM_MAX_ENB][ENB_AGENT_MAX];
 
-extern unsigned int mac_agent_registered[NUM_MAX_ENB_AGENT];
+extern AGENT_MAC_xface *agent_mac_xface[NUM_MAX_ENB][ENB_AGENT_MAX];
+
+extern unsigned int mac_agent_registered[NUM_MAX_ENB];
 
 #endif
diff --git a/openair2/ENB_APP/enb_agent_mac_defs.h b/openair2/ENB_APP/enb_agent_mac_defs.h
index a570165919..f6dccb1570 100644
--- a/openair2/ENB_APP/enb_agent_mac_defs.h
+++ b/openair2/ENB_APP/enb_agent_mac_defs.h
@@ -42,14 +42,18 @@
 
 /* ENB AGENT-MAC Interface */
 typedef struct {
-  msg_context_t *agent_ctxt;
+  //msg_context_t *agent_ctxt;
 
   /// Inform the controller about the scheduling requests received during the subframe
-  void (*enb_agent_send_sr_info)(mid_t mod_id, msg_context_t *context);
+  void (*enb_agent_send_sr_info)(mid_t mod_id);
   
   /// Inform the controller about the current UL/DL subframe
-  void (*enb_agent_send_sf_trigger)(mid_t mod_id, msg_context_t *context);
+  void (*enb_agent_send_sf_trigger)(mid_t mod_id);
 
+  /// Send to the controller all the mac stat updates that occured during this subframe
+  /// based on the stats request configuration
+  void (*enb_agent_send_update_mac_stats)(mid_t mod_id);
+  
   /*TODO: Fill in with the rest of the MAC layer technology specific callbacks (UL/DL scheduling, RACH info etc)*/
 
 } AGENT_MAC_xface;
diff --git a/openair2/ENB_APP/enb_agent_mac_internal.h b/openair2/ENB_APP/enb_agent_mac_internal.h
index 98c15e083f..394d3e7730 100644
--- a/openair2/ENB_APP/enb_agent_mac_internal.h
+++ b/openair2/ENB_APP/enb_agent_mac_internal.h
@@ -43,9 +43,6 @@
 #include "enb_agent_common.h"
 
 
-/*Flags showing if a mac agent has already been registered*/
-unsigned int mac_agent_registered[NUM_MAX_ENB_AGENT];
-
 /*This will be used for producing continuous status updates for the MAC
  *Needs to be thread-safe
  */
@@ -61,7 +58,7 @@ typedef struct {
 } mac_stats_updates_context_t;
 
 /*Array holding the last stats reports for each eNB. Used for continuous reporting*/
-mac_stats_updates_context_t mac_stats_context[NUM_MAX_ENB_AGENT];
+mac_stats_updates_context_t mac_stats_context[NUM_MAX_ENB];
 
 /*Functions to initialize and destroy the struct required for the
  *continuous stats update report*/
-- 
2.26.2