From 22d0f60e126b359aa348a062f53a5fcdc0f50bae Mon Sep 17 00:00:00 2001
From: Xenofon Foukas <x.foukas@sms.ed.ac.uk>
Date: Tue, 12 Jan 2016 18:08:57 +0200
Subject: [PATCH] Added support for creation of SR protocol messages and
 defined initial MAC-Agent interface

---
 openair2/ENB_APP/enb_agent_extern.h   |  50 +++++++++++
 openair2/ENB_APP/enb_agent_mac.c      | 121 ++++++++++++++++++++++++++
 openair2/ENB_APP/enb_agent_mac.h      |  21 +++--
 openair2/ENB_APP/enb_agent_mac_defs.h |  54 ++++++++++++
 4 files changed, 238 insertions(+), 8 deletions(-)
 create mode 100644 openair2/ENB_APP/enb_agent_extern.h
 create mode 100644 openair2/ENB_APP/enb_agent_mac_defs.h

diff --git a/openair2/ENB_APP/enb_agent_extern.h b/openair2/ENB_APP/enb_agent_extern.h
new file mode 100644
index 0000000000..2cd072f25a
--- /dev/null
+++ b/openair2/ENB_APP/enb_agent_extern.h
@@ -0,0 +1,50 @@
+/*******************************************************************************
+    OpenAirInterface
+    Copyright(c) 1999 - 2016 Eurecom
+
+    OpenAirInterface is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+
+    OpenAirInterface is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenAirInterface.The full GNU General Public License is
+   included in this distribution in the file called "COPYING". If not,
+   see <http://www.gnu.org/licenses/>.
+
+  Contact Information
+  OpenAirInterface Admin: openair_admin@eurecom.fr
+  OpenAirInterface Tech : openair_tech@eurecom.fr
+  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
+
+  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.
+
+ *******************************************************************************/
+
+/*! \file ENB_APP/extern.h
+ * \brief enb agent - mac interface primitives
+ * \author Xenofon Foukas
+ * \date 2016
+ * \version 0.1
+ * \mail x.foukas@sms.ed.ac.uk
+ */
+
+#ifndef __ENB_AGENT_EXTERN_H__
+#define __ENB_AGENT_EXTERN_H__
+
+#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 unsigned int mac_agent_registered[NUM_MAX_ENB_AGENT];
+
+#endif
diff --git a/openair2/ENB_APP/enb_agent_mac.c b/openair2/ENB_APP/enb_agent_mac.c
index 411480254f..36b7788d23 100644
--- a/openair2/ENB_APP/enb_agent_mac.c
+++ b/openair2/ENB_APP/enb_agent_mac.c
@@ -760,3 +760,124 @@ int enb_agent_mac_destroy_stats_reply(Protocol__ProgranMessage *msg) {
   //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
   return -1;
 }
+
+int enb_agent_mac_sr_info(mid_t mod_id, const void *params, Protocol__ProgranMessage **msg) {
+  Protocol__PrpHeader *header;
+  int i;
+  const int xid = *((int *)params);
+  if (prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_UL_SR_INFO, &header) != 0)
+    goto error;
+
+  Protocol__PrpUlSrInfo *ul_sr_info_msg;
+  ul_sr_info_msg = malloc(sizeof(Protocol__PrpUlSrInfo));
+  if (ul_sr_info_msg == NULL) {
+    goto error;
+  }
+  protocol__prp_ul_sr_info__init(ul_sr_info_msg);
+  
+  ul_sr_info_msg->header = header;
+  ul_sr_info_msg->has_sfn_sf = 1;
+  ul_sr_info_msg->sfn_sf = get_sfn_sf(mod_id);
+  /*TODO: Set the number of UEs that sent an SR */
+  ul_sr_info_msg->n_rnti = 1;
+  ul_sr_info_msg->rnti = (uint32_t *) malloc(ul_sr_info_msg->n_rnti * sizeof(uint32_t));
+
+  if(ul_sr_info_msg->rnti == NULL) {
+    goto error;
+  }
+  /*TODO:Set the rnti of the UEs that sent an SR */
+  for (i = 0; i < ul_sr_info_msg->n_rnti; i++) {
+    ul_sr_info_msg->rnti[i] = 1;
+  }
+    
+  *msg = malloc(sizeof(Protocol__ProgranMessage));
+  if(*msg == NULL)
+    goto error;
+  protocol__progran_message__init(*msg);
+  (*msg)->msg_case = PROTOCOL__PROGRAN_MESSAGE__MSG_UL_SR_INFO_MSG;
+  (*msg)->msg_dir =  PROTOCOL__PROGRAN_DIRECTION__INITIATING_MESSAGE;
+  (*msg)->ul_sr_info_msg = ul_sr_info_msg;
+  return 0;
+  
+ error:
+  // TODO: Need to make proper error handling
+  if (header != NULL)
+    free(header);
+  if (ul_sr_info_msg != NULL) {
+    free(ul_sr_info_msg->rnti);
+    free(ul_sr_info_msg);
+  }
+  if(*msg != NULL)
+    free(*msg);
+  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
+  return -1;
+}
+
+int enb_agent_mac_destroy_sr_info(Protocol__ProgranMessage *msg) {
+   if(msg->msg_case != PROTOCOL__PROGRAN_MESSAGE__MSG_UL_SR_INFO_MSG)
+     goto error;
+
+   free(msg->ul_sr_info_msg->header);
+   free(msg->ul_sr_info_msg->rnti);
+   free(msg->ul_sr_info_msg);
+   free(msg);
+   return 0;
+
+ error:
+   //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
+   return -1;
+}
+
+void enb_agent_send_sr_info(mid_t mod_id, msg_context_t *context) {
+  int size;
+  Protocol__ProgranMessage *msg;
+  void *data;
+  int priority;
+  err_code_t err_code;
+
+  /*TODO: Must use a proper xid*/
+  int xid = 1;
+  err_code = enb_agent_mac_sr_info(mod_id, (void *) &xid, &msg);
+  if (err_code < 0) {
+    goto error;
+  }
+
+  if (msg != NULL){
+    data=enb_agent_pack_message(msg, &size);
+    
+    if (message_put(context->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);
+  }
+ error:
+  LOG_D(ENB_AGENT, "Could not send sr message\n");
+}
+
+int enb_agent_register_mac_xface(mid_t mod_id, AGENT_MAC_xface *xface) {
+  if (!mac_agent_registered[mod_id]) {
+    LOG_E(MAC, "MAC agent for eNB %d is already registered\n", mod_id);
+    return -1;
+  }
+
+  xface->agent_ctxt = &shared_ctxt[mod_id];
+  xface->enb_agent_send_sr_info = enb_agent_send_sr_info;
+
+  mac_agent_registered[mod_id] = 1;
+  return 1;
+}
+
+int enb_agent_unregister_mac_xface(mid_t mod_id, AGENT_MAC_xface *xface) {
+  
+  if(mac_agent_registered[mod_id]) {
+    LOG_E(MAC, "MAC agent for eNB %d is already registered\n", mod_id);
+    return -1;
+  }
+  
+  xface->agent_ctxt = NULL;
+  xface->enb_agent_send_sr_info = NULL;
+
+  mac_agent_registered[mod_id] = NULL;
+  return 1;
+}
diff --git a/openair2/ENB_APP/enb_agent_mac.h b/openair2/ENB_APP/enb_agent_mac.h
index 11e981a51b..7945082410 100644
--- a/openair2/ENB_APP/enb_agent_mac.h
+++ b/openair2/ENB_APP/enb_agent_mac.h
@@ -43,7 +43,10 @@
 #include "stats_common.pb-c.h"
 
 #include "enb_agent_common.h"
+#include "enb_agent_extern.h"
 
+/*Flags showing if a mac agent has already been registered*/
+unsigned int mac_agent_registered[NUM_MAX_ENB_AGENT];
 
 /* These types will be used to give
    instructions for the type of stats reports
@@ -87,20 +90,22 @@ int enb_agent_mac_stats_reply(mid_t mod_id, xid_t xid, const report_config_t *re
 
 int enb_agent_mac_destroy_stats_reply(Protocol__ProgranMessage *msg);
 
+int enb_agent_mac_sr_info(mid_t mod_id, const void *params, Protocol__ProgranMessage **msg);
+
+int enb_agent_mac_destroy_sr_info(Protocol__ProgranMessage *msg);
+
 
 /**********************************
  * eNB agent - technology mac API
  **********************************/
 
-/*Set DCI for particular RNTI in defined subframe (UL/DL)*/
-int enb_agent_mac_dl_config(mid_t mod_id, Protocol__ProgranMessage *msg);
-
-int enb_agent_mac_ul_config(mid_t mod_id, Protocol__ProgranMessage *msg);
-
-/*Push controller configurations to the eNB for cells and UEs*/
-int enb_agent_set_cell_config(mid_t mod_id, Protocol__ProgranMessage *msg);
+/*Inform controller about received scheduling requests during a subframe*/
+void enb_agent_send_sr_info(mid_t mod_id, msg_context_t *context);
 
-int enb_agent_set_ue_config(mid_t mod_id, Protocol__ProgranMessage *msg);
+/*Register technology specific interface callbacks*/
+int enb_agent_register_mac_xface(mid_t mod_id, AGENT_MAC_xface *xface);
 
+/*Unregister technology specific callbacks*/
+int enb_agent_unregister_mac_xface(mid_t mod_id, AGENT_MAC_xface*xface);
 
 #endif
diff --git a/openair2/ENB_APP/enb_agent_mac_defs.h b/openair2/ENB_APP/enb_agent_mac_defs.h
new file mode 100644
index 0000000000..28a424706b
--- /dev/null
+++ b/openair2/ENB_APP/enb_agent_mac_defs.h
@@ -0,0 +1,54 @@
+/*******************************************************************************
+    OpenAirInterface
+    Copyright(c) 1999 - 2016 Eurecom
+
+    OpenAirInterface is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+
+    OpenAirInterface is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenAirInterface.The full GNU General Public License is
+   included in this distribution in the file called "COPYING". If not,
+   see <http://www.gnu.org/licenses/>.
+
+  Contact Information
+  OpenAirInterface Admin: openair_admin@eurecom.fr
+  OpenAirInterface Tech : openair_tech@eurecom.fr
+  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
+
+  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.
+
+ *******************************************************************************/
+
+/*! \file enb_agent_mac_defs.h
+ * \brief enb agent - mac interface primitives
+ * \author Xenofon Foukas
+ * \date 2016
+ * \version 0.1
+ * \mail x.foukas@sms.ed.ac.uk
+ */
+
+#ifndef __ENB_AGENT_MAC_PRIMITIVES_H__
+#define __ENB_AGENT_MAC_PRIMITIVES_H__
+
+#include "enb_agent_defs.h"
+
+/* ENB AGENT-MAC Interface */
+typedef struct {
+  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);
+
+  /*TODO: Fill in with the rest of the MAC layer technology specific callbacks (UL/DL scheduling, RACH info etc)*/
+
+} AGENT_MAC_xface;
+
+#endif
-- 
2.26.2