From a3c337cc9fd1e4d4d3e70ea4080cbdd6d8c212b3 Mon Sep 17 00:00:00 2001
From: LAD <louisadrien.dufrene@orange.com>
Date: Wed, 30 Oct 2019 07:36:44 -0700
Subject: [PATCH] [to test] Add MAC itti loop and DRX config support

---
 openair2/COMMON/mac_messages_def.h           |  3 ++
 openair2/COMMON/mac_messages_types.h         | 14 ++++++
 openair2/F1AP/f1ap_du_rrc_message_transfer.c |  6 ++-
 openair2/LAYER2/MAC/config.c                 | 31 ++++++-------
 openair2/LAYER2/MAC/mac_proto.h              |  7 ++-
 openair2/LAYER2/MAC/main.c                   | 49 ++++++++++++++++++--
 openair2/RRC/LTE/MESSAGES/asn1_msg.c         |  1 +
 openair2/RRC/LTE/rrc_eNB.c                   | 18 ++++---
 targets/COMMON/create_tasks.c                | 16 ++++---
 9 files changed, 109 insertions(+), 36 deletions(-)

diff --git a/openair2/COMMON/mac_messages_def.h b/openair2/COMMON/mac_messages_def.h
index f69186e4b86..09b3f2aab48 100644
--- a/openair2/COMMON/mac_messages_def.h
+++ b/openair2/COMMON/mac_messages_def.h
@@ -45,3 +45,6 @@ MESSAGE_DEF(RRC_MAC_MCCH_DATA_REQ,      MESSAGE_PRIORITY_MED_PLUS, RrcMacMcchDat
 MESSAGE_DEF(RRC_MAC_MCCH_DATA_IND,      MESSAGE_PRIORITY_MED_PLUS, RrcMacMcchDataInd,           rrc_mac_mcch_data_ind)
 
 MESSAGE_DEF(RRC_MAC_PCCH_DATA_REQ,      MESSAGE_PRIORITY_MED_PLUS, RrcMacPcchDataReq,           rrc_mac_pcch_data_req)
+
+/* RRC configures DRX context (MAC timers) of a UE */
+MESSAGE_DEF(RRC_MAC_DRX_CONFIG_REQ, MESSAGE_PRIORITY_MED, rrc_mac_drx_config_req_t, rrc_mac_drx_config_req)
\ No newline at end of file
diff --git a/openair2/COMMON/mac_messages_types.h b/openair2/COMMON/mac_messages_types.h
index 3bbea66d0d6..e227296d05d 100644
--- a/openair2/COMMON/mac_messages_types.h
+++ b/openair2/COMMON/mac_messages_types.h
@@ -29,6 +29,8 @@
 #ifndef MAC_MESSAGES_TYPES_H_
 #define MAC_MESSAGES_TYPES_H_
 
+#include <LTE_DRX-Config.h>
+
 //-------------------------------------------------------------------------------------------//
 // Defines to access message fields.
 #define RRC_MAC_IN_SYNC_IND(mSGpTR)             (mSGpTR)->ittiMsg.rrc_mac_in_sync_ind
@@ -48,6 +50,8 @@
 #define RRC_MAC_MCCH_DATA_IND(mSGpTR)           (mSGpTR)->ittiMsg.rrc_mac_mcch_data_ind
 #define RRC_MAC_PCCH_DATA_REQ(mSGpTR)           (mSGpTR)->ittiMsg.rrc_mac_pcch_data_req
 
+#define RRC_MAC_DRX_CONFIG_REQ(mSGpTR)           (mSGpTR)->ittiMsg.rrc_mac_drx_config_req
+
 // Some constants from "LAYER2/MAC/defs.h"
 #define BCCH_SDU_SIZE                           (512)
 #define BCCH_SDU_MBMS_SIZE                      (512)
@@ -145,4 +149,14 @@ typedef struct RrcMacPcchDataReq_s {
   uint8_t   sdu[PCCH_SDU_SIZE];
   uint8_t   enb_index;
 } RrcMacPcchDataReq;
+
+/* RRC configures DRX context (MAC timers) of a UE */
+typedef struct rrc_mac_drx_config_req_s {
+  /* UE RNTI to configure */
+  rnti_t rnti;
+
+  /* DRX configuration from MacMainConfig to configure UE's local timers */
+  LTE_DRX_Config_t * drx_Configuration;
+} rrc_mac_drx_config_req_t;
+
 #endif /* MAC_MESSAGES_TYPES_H_ */
diff --git a/openair2/F1AP/f1ap_du_rrc_message_transfer.c b/openair2/F1AP/f1ap_du_rrc_message_transfer.c
index e6bd03bdf77..583ccfe6494 100644
--- a/openair2/F1AP/f1ap_du_rrc_message_transfer.c
+++ b/openair2/F1AP/f1ap_du_rrc_message_transfer.c
@@ -410,14 +410,16 @@ int DU_handle_DL_RRC_MESSAGE_TRANSFER(instance_t       instance,
                 mac_MainConfig = &rrcConnectionReconfiguration_r8->radioResourceConfigDedicated->mac_MainConfig->choice.explicitValue;
 
                 /* CDRX Configuration */
+                /*
                 if (mac_MainConfig->drx_Config == NULL) {
                   LOG_W(F1AP, "drx_Configuration parameter is NULL, cannot configure local UE parameters or CDRX is deactivated\n");
                 } else {
-                  /* Set timers and thresholds values in local MAC context of UE */
+                  //// Set timers and thresholds values in local MAC context of UE
                   eNB_Config_Local_DRX(ctxt.module_id, ctxt.rnti, mac_MainConfig->drx_Config);
                   LOG_D(F1AP, "DRX configured in MAC Main Configuration for RRC Connection Reconfiguration\n");
-                /* End of CDRX configuration */
                 }
+                */
+                /* End of CDRX configuration */
               }
 
               LTE_MeasGapConfig_t     *measGapConfig   = NULL;
diff --git a/openair2/LAYER2/MAC/config.c b/openair2/LAYER2/MAC/config.c
index ad7bc2fbcd5..84b5720bc07 100644
--- a/openair2/LAYER2/MAC/config.c
+++ b/openair2/LAYER2/MAC/config.c
@@ -992,27 +992,24 @@ int rrc_mac_config_req_eNB(module_id_t Mod_idP,
 /*
 * Configure local CDRX timers and thresholds following the drx_configuration input
 */
-void eNB_Config_Local_DRX(
-  module_id_t Mod_id,
-  rnti_t rnti,
-  LTE_DRX_Config_t *const drx_Configuration
-)
+void eNB_Config_Local_DRX(instance_t Mod_id,
+                          rrc_mac_drx_config_req_t *rrc_mac_drx_config_req)
 //-----------------------------------------------------------------------------
 {
   UE_list_t *UE_list_mac = NULL;
-  int UE_id = -1;
   UE_sched_ctrl_t *UE_scheduling_control = NULL;
+  int UE_id = -1;
   
+  rnti_t rnti = rrc_mac_drx_config_req->rnti;
+  LTE_DRX_Config_t *const drx_Configuration = rrc_mac_drx_config_req->drx_Configuration;
+
   UE_list_mac = &(RC.mac[Mod_id]->UE_list);
 
   UE_id = find_UE_id(Mod_id, rnti);
 
   /* Check UE_id */
   if (UE_id == -1) {
-    LOG_E(MAC, "%s:%d:%s: ERROR, UE_id == -1\n",
-      __FILE__,
-      __LINE__,
-      __FUNCTION__);
+    LOG_E(MAC, "[eNB_Config_Local_DRX] UE_id == -1\n");
     return;
   }
 
@@ -1022,13 +1019,13 @@ void eNB_Config_Local_DRX(
 
   /* Check drx_Configuration */
   if (drx_Configuration == NULL) {
-    LOG_W(MAC, "drx_Configuration parameter is NULL, cannot configure local UE parameters for CDRX\n");
+    LOG_W(MAC, "[eNB_Config_Local_DRX] drx_Configuration parameter is NULL, cannot configure local UE parameters for CDRX\n");
     return;
   }
 
   /* Check if drx config present */
   if (drx_Configuration->present != LTE_DRX_Config_PR_setup) {
-    LOG_I(MAC, "No drx_Configuration present, don't configure local UE parameters for CDRX\n");
+    LOG_I(MAC, "[eNB_Config_Local_DRX] No drx_Configuration present, don't configure local UE parameters for CDRX\n");
     return;
   }
 
@@ -1090,7 +1087,7 @@ void eNB_Config_Local_DRX(
       UE_scheduling_control->on_duration_timer_thres = 200;
       break;
     default:
-      LOG_E(MAC, "Error in local DRX configuration, the on duration timer value specified is unknown\n");
+      LOG_E(MAC, "[eNB_Config_Local_DRX] Error in local DRX configuration, the on duration timer value specified is unknown\n");
       break;
   }
 
@@ -1166,7 +1163,7 @@ void eNB_Config_Local_DRX(
       UE_scheduling_control->drx_inactivity_timer_thres = 0;
       break;
     default:
-      LOG_E(MAC, "Error in local DRX configuration, the drx inactivity timer value specified is unknown\n");
+      LOG_E(MAC, "[eNB_Config_Local_DRX] Error in local DRX configuration, the drx inactivity timer value specified is unknown\n");
       break;
   }
 
@@ -1229,7 +1226,7 @@ void eNB_Config_Local_DRX(
         UE_scheduling_control->short_drx_cycle_duration = 640;
         break;
       default:
-        LOG_E(MAC, "Error in local DRX configuration, the short drx timer value specified is unknown\n");
+        LOG_E(MAC, "[eNB_Config_Local_DRX] Error in local DRX configuration, the short drx timer value specified is unknown\n");
         break;
     }
 
@@ -1305,7 +1302,7 @@ void eNB_Config_Local_DRX(
       UE_scheduling_control->drx_start_offset = (uint16_t) choiceSetup->longDRX_CycleStartOffset.choice.sf2560;
       break;
     default:
-      LOG_E(MAC, "Invalid long_DRX value in DRX local configuration\n");
+      LOG_E(MAC, "[eNB_Config_Local_DRX] Invalid long_DRX value in DRX local configuration\n");
       break;
   }
 
@@ -1336,7 +1333,7 @@ void eNB_Config_Local_DRX(
       memset(UE_scheduling_control->drx_retransmission_timer_thres, 33, sizeof(UE_scheduling_control->drx_retransmission_timer_thres));
       break;
     default:
-      LOG_E(MAC, "Error in local DRX configuration, the drx retransmission timer value specified is unknown\n");
+      LOG_E(MAC, "[eNB_Config_Local_DRX] Error in local DRX configuration, the drx retransmission timer value specified is unknown\n");
       break;
   }
 }
diff --git a/openair2/LAYER2/MAC/mac_proto.h b/openair2/LAYER2/MAC/mac_proto.h
index 695005cd1f8..bb35bffc329 100644
--- a/openair2/LAYER2/MAC/mac_proto.h
+++ b/openair2/LAYER2/MAC/mac_proto.h
@@ -31,6 +31,7 @@
 
 #include "LAYER2/MAC/mac.h"
 #include "PHY/defs_common.h" // for PRACH_RESOURCES_t and lte_subframe_t
+#include "openair2/COMMON/mac_messages_types.h"
 
 /** \addtogroup _mac
  *  @{
@@ -1318,9 +1319,13 @@ int ue_ul_slice_membership(module_id_t mod_id, int UE_id, int slice_idx);
 
 /* DRX Configuration */
 /* Configure local DRX timers and thresholds in UE context, following the drx_configuration input */
-void eNB_Config_Local_DRX(module_id_t Mod_id, rnti_t rnti, LTE_DRX_Config_t *drx_Configuration);
+void eNB_Config_Local_DRX(instance_t Mod_id, rrc_mac_drx_config_req_t *rrc_mac_drx_config_req);                        
 
 /* from here: prototypes to get rid of compilation warnings: doc to be written by function author */
 uint8_t ul_subframe2_k_phich(COMMON_channels_t * cc, sub_frame_t ul_subframe);
 #endif
 /** @}*/
+
+/* MAC ITTI messaging related functions */
+/* Main loop of MAC itti message handling */
+void *mac_enb_task(void *arg);
\ No newline at end of file
diff --git a/openair2/LAYER2/MAC/main.c b/openair2/LAYER2/MAC/main.c
index 8fb8d44e0ab..64cb03774bc 100644
--- a/openair2/LAYER2/MAC/main.c
+++ b/openair2/LAYER2/MAC/main.c
@@ -34,15 +34,12 @@
 #include "mac_proto.h"
 #include "mac_extern.h"
 #include "assertions.h"
-//#include "PHY_INTERFACE/phy_extern.h"
-//#include "PHY/defs_eNB.h"
-//#include "SCHED/sched_eNB.h"
 #include "LAYER2/PDCP_v10.1.0/pdcp.h"
 #include "RRC/LTE/rrc_defs.h"
 #include "common/utils/LOG/log.h"
 #include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
-
 #include "common/ran_context.h"
+#include "intertask_interface.h"
 
 extern RAN_CONTEXT_t RC;
 
@@ -223,3 +220,47 @@ int l2_init_eNB(void)
 
     return (1);
 }
+
+//-----------------------------------------------------------------------------
+/*
+ * Main loop of MAC itti message handling
+ */
+void *mac_enb_task(void *arg)
+//-----------------------------------------------------------------------------
+{
+  MessageDef *received_msg = NULL;
+  int         result;
+
+  itti_mark_task_ready(TASK_MAC_ENB); // void function 10/2019
+  LOG_I(MAC,"Starting main loop of MAC message task\n");
+
+  while (1) {
+    itti_receive_msg(TASK_MAC_ENB, &received_msg);
+
+    switch (ITTI_MSG_ID(received_msg)) {
+      case RRC_MAC_DRX_CONFIG_REQ:
+        LOG_I(MAC, "MAC Task Received RRC_MAC_DRX_CONFIG_REQ\n");
+        /* Set timers and thresholds values in local MAC context of UE */
+        eNB_Config_Local_DRX(ITTI_MESSAGE_GET_INSTANCE(received_msg), &received_msg->ittiMsg.rrc_mac_drx_config_req);
+        break;
+
+      case TERMINATE_MESSAGE:
+        LOG_W(MAC, " *** Exiting MAC thread\n");
+        itti_exit_task();
+        break;
+
+      default:
+        LOG_E(MAC, "MAC instance received unhandled message: %d:%s\n",
+              ITTI_MSG_ID(received_msg), 
+              ITTI_MSG_NAME(received_msg));
+        break;  
+    } // end switch
+
+    result = itti_free(ITTI_MSG_ORIGIN_ID(received_msg), received_msg);
+    AssertFatal(result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
+    
+    received_msg = NULL;
+  } // end while
+
+  return NULL;
+}
\ No newline at end of file
diff --git a/openair2/RRC/LTE/MESSAGES/asn1_msg.c b/openair2/RRC/LTE/MESSAGES/asn1_msg.c
index c2212a6c160..41eea1f7e6f 100644
--- a/openair2/RRC/LTE/MESSAGES/asn1_msg.c
+++ b/openair2/RRC/LTE/MESSAGES/asn1_msg.c
@@ -621,6 +621,7 @@ uint8_t do_SIB1_MBMS(rrc_eNB_carrier_data_t *carrier,
   return((enc_rval.encoded+7)/8);
 }
 #endif
+
 //-----------------------------------------------------------------------------
 /*
  * Generate the configuration structure for CDRX feature
diff --git a/openair2/RRC/LTE/rrc_eNB.c b/openair2/RRC/LTE/rrc_eNB.c
index 0be703b0ecf..5522652a26f 100644
--- a/openair2/RRC/LTE/rrc_eNB.c
+++ b/openair2/RRC/LTE/rrc_eNB.c
@@ -2967,6 +2967,7 @@ void rrc_eNB_generate_defaultRRCConnectionReconfiguration(const protocol_ctxt_t
   uint8_t   buffer[RRC_BUF_SIZE];
   uint16_t  size;
   int       i;
+  MessageDef *message_p = NULL;
   
   /* Configure SRB1/SRB2, PhysicalConfigDedicated, LTE_MAC_MainConfig for UE */
   eNB_RRC_INST                           *rrc_inst = RC.rrc[ctxt_pP->module_id];
@@ -3215,8 +3216,11 @@ void rrc_eNB_generate_defaultRRCConnectionReconfiguration(const protocol_ctxt_t
     if (mac_MainConfig->drx_Config == NULL) {
       LOG_W(RRC, "drx_Configuration parameter is NULL, cannot configure local UE parameters or CDRX is deactivated\n");
     } else {
-      /* Set timers and thresholds values in local MAC context of UE */
-      eNB_Config_Local_DRX(module_id, rnti, mac_MainConfig->drx_Config);
+      /* Send DRX configuration to MAC task to configure timers of local UE context */
+      message_p = itti_alloc_new_message(TASK_RRC_ENB, RRC_MAC_DRX_CONFIG_REQ);
+      RRC_MAC_DRX_CONFIG_REQ(message_p).rnti = rnti;
+      RRC_MAC_DRX_CONFIG_REQ(message_p).drx_Configuration = mac_MainConfig->drx_Config;
+      itti_send_msg_to_task(TASK_MAC_ENB, module_id, message_p);
       LOG_D(RRC, "DRX configured in MAC Main Configuration for RRC Connection Reconfiguration\n");
     }
   }
@@ -3732,8 +3736,6 @@ flexran_rrc_eNB_generate_defaultRRCConnectionReconfiguration(const protocol_ctxt
   LTE_C_RNTI_t                           *cba_RNTI                         = NULL;
   int                                    measurements_enabled;
   uint8_t xid = rrc_eNB_get_next_transaction_identifier(ctxt_pP->module_id);   //Transaction_id,
-  uint8_t cc_id = ue_context_pP->ue_context.primaryCC_id;
-  LTE_UE_EUTRA_Capability_t *UEcap = ue_context_pP->ue_context.UE_Capability;
 
 #ifdef CBA // Contention Based Access
   uint8_t                            *cba_RNTI_buf;
@@ -3916,6 +3918,9 @@ flexran_rrc_eNB_generate_defaultRRCConnectionReconfiguration(const protocol_ctxt
 
   if (NODE_IS_MONOLITHIC(RC.rrc[ctxt_pP->module_id]->node_type)) {
     /* CDRX Configuration */
+    /*
+    uint8_t cc_id = ue_context_pP->ue_context.primaryCC_id;
+    LTE_UE_EUTRA_Capability_t *UEcap = ue_context_pP->ue_context.UE_Capability;
     // Need to check if UE is a BR UE
     rnti_t rnti = ue_context_pP->ue_id_rnti;
     module_id_t module_id = ctxt_pP->module_id;
@@ -3931,14 +3936,14 @@ flexran_rrc_eNB_generate_defaultRRCConnectionReconfiguration(const protocol_ctxt
         
         LOG_D(RRC, "Processing the DRX configuration in RRC Connection Reconfiguration\n");
 
-        /* Process the IE drx_Config */
+        ///// Process the IE drx_Config /
         if (cc_id < MAX_NUM_CCs) {
           mac_MainConfig->drx_Config = do_DrxConfig(cc_id, &rrc_inst->configuration, UEcap); // drx_Config IE
 
           if (mac_MainConfig->drx_Config == NULL) {
             LOG_E(RRC, "drx_Configuration parameter is NULL, cannot configure local UE parameters\n");
           } else {
-            /* Set timers and thresholds values in local MAC context of UE */
+            ////// Set timers and thresholds values in local MAC context of UE /
             eNB_Config_Local_DRX(module_id, rnti, mac_MainConfig->drx_Config);
             LOG_D(RRC, "DRX configured in mac main config for RRC Connection Reconfiguration\n");
           }
@@ -3951,6 +3956,7 @@ flexran_rrc_eNB_generate_defaultRRCConnectionReconfiguration(const protocol_ctxt
     } else { // UE_id invalid
       LOG_E(RRC, "Invalid UE_id found!\n");
     }
+    */
     /* End of CDRX configuration */
   }
 
diff --git a/targets/COMMON/create_tasks.c b/targets/COMMON/create_tasks.c
index cfc2bc22155..2f7ff9d3a76 100644
--- a/targets/COMMON/create_tasks.c
+++ b/targets/COMMON/create_tasks.c
@@ -26,11 +26,11 @@
 # include "common/ran_context.h"
 
 #ifdef OPENAIR2
-    #include "sctp_eNB_task.h"
-    #include "x2ap_eNB.h"
-    #include "s1ap_eNB.h"
-    #include "udp_eNB_task.h"
-    #include "gtpv1u_eNB_task.h"
+  #include "sctp_eNB_task.h"
+  #include "x2ap_eNB.h"
+  #include "s1ap_eNB.h"
+  #include "udp_eNB_task.h"
+  #include "gtpv1u_eNB_task.h"
   #if ENABLE_RAL
     #include "lteRALue.h"
     #include "lteRALenb.h"
@@ -40,6 +40,7 @@
 # include "f1ap_cu_task.h"
 # include "f1ap_du_task.h"
 # include "enb_app.h"
+# include "openair2/LAYER2/MAC/mac_proto.h"
 
 extern RAN_CONTEXT_t RC;
 
@@ -63,7 +64,6 @@ int create_tasks(uint32_t enb_nb) {
     AssertFatal(rc >= 0, "Create task for SCTP failed\n");
   }
 
-
   if (EPC_MODE_ENABLED && !NODE_IS_DU(type)) {
     rc = itti_create_task(TASK_S1AP, s1ap_eNB_task, NULL);
     AssertFatal(rc >= 0, "Create task for S1AP failed\n");
@@ -91,5 +91,9 @@ int create_tasks(uint32_t enb_nb) {
     AssertFatal(rc >= 0, "Create task for DU F1AP failed\n");
   }
 
+  LOG_I(MAC,"Creating MAC eNB Task\n");
+  rc = itti_create_task(TASK_MAC_ENB, mac_enb_task, NULL);
+  AssertFatal(rc >= 0, "Create task for MAC eNB failed\n");
+
   return 0;
 }
-- 
GitLab