diff --git a/cmake_targets/CMakeLists.txt b/cmake_targets/CMakeLists.txt
index 616adf3a82dadc9fc2eb55c30d19f252517d2cb4..d57bbc9e85ed0f60e6c41ea4593ed1e1c12c6b38 100644
--- a/cmake_targets/CMakeLists.txt
+++ b/cmake_targets/CMakeLists.txt
@@ -599,8 +599,6 @@ add_boolean_option(MESSAGE_CHART_GENERATOR False         "For generating sequenc
 add_boolean_option(MESSAGE_CHART_GENERATOR_RLC_MAC False "trace RLC-MAC exchanges in sequence diagrams")
 add_boolean_option(MESSAGE_CHART_GENERATOR_PHY     False "trace some PHY exchanges in sequence diagrams")
 
-add_boolean_option(FLEXRAN_AGENT_SB_IF             False         "enable FlexRAN agent to inteface with a SDN controller")
-
 ########################
 # Include order
 ##########################
@@ -766,6 +764,8 @@ include_directories("${OPENAIR_DIR}/targets/ARCH/EXMIMO/USERSPACE/LIB/")
 include_directories("${OPENAIR_DIR}/targets/ARCH/EXMIMO/DEFS")
 include_directories("${OPENAIR2_DIR}/ENB_APP")
 include_directories("${OPENAIR2_DIR}/ENB_APP/CONTROL_MODULES/MAC")
+include_directories("${OPENAIR2_DIR}/ENB_APP/CONTROL_MODULES/RRC")
+include_directories("${OPENAIR2_DIR}/ENB_APP/CONTROL_MODULES/PDCP")
 include_directories("${OPENAIR2_DIR}/UTIL/OSA")
 include_directories("${OPENAIR2_DIR}/UTIL/LFDS/liblfds6.1.1/liblfds611/inc")
 include_directories("${OPENAIR2_DIR}/UTIL/LFDS/liblfds7.0.0/liblfds700/inc")
@@ -787,92 +787,94 @@ include_directories("${OPENAIR_DIR}")
 
 # Utilities Library
 ################
-if (FLEXRAN_AGENT_SB_IF)
-  # set the version of protobuf messages, V3 not supported yet
-  add_list1_option(FLPT_VERSION V2 "FLPT MSG  protobuf  grammar version" V2 V3)
-
-  if (${FLPT_VERSION} STREQUAL "V2")
-    set (FLPTDIR V2)
-  elseif (${FLPT_VERSION} STREQUAL "V3")
-    set (FLPTDIR V3)
-  endif(${FLPT_VERSION} STREQUAL "V2")
-
-  set(FLPT_MSG_DIR ${OPENAIR2_DIR}/ENB_APP/MESSAGES/${FLPTDIR} )
-  set(FLPT_MSG_FILES
-    ${FLPT_MSG_DIR}/header.proto
-    ${FLPT_MSG_DIR}/flexran.proto
-    ${FLPT_MSG_DIR}/stats_common.proto
-    ${FLPT_MSG_DIR}/stats_messages.proto
-    ${FLPT_MSG_DIR}/time_common.proto
-    ${FLPT_MSG_DIR}/controller_commands.proto
-    ${FLPT_MSG_DIR}/mac_primitives.proto
-    ${FLPT_MSG_DIR}/config_messages.proto
-    ${FLPT_MSG_DIR}/config_common.proto
-    ${FLPT_MSG_DIR}/control_delegation.proto
-    )
+# set the version of protobuf messages, V3 not supported yet
+add_list1_option(FLPT_VERSION V2 "FLPT MSG  protobuf  grammar version" V2 V3)
+
+if (${FLPT_VERSION} STREQUAL "V2")
+  set (FLPTDIR V2)
+elseif (${FLPT_VERSION} STREQUAL "V3")
+  set (FLPTDIR V3)
+endif(${FLPT_VERSION} STREQUAL "V2")
+
+set(FLPT_MSG_DIR ${OPENAIR2_DIR}/ENB_APP/MESSAGES/${FLPTDIR} )
+set(FLPT_MSG_FILES
+  ${FLPT_MSG_DIR}/header.proto
+  ${FLPT_MSG_DIR}/flexran.proto
+  ${FLPT_MSG_DIR}/stats_common.proto
+  ${FLPT_MSG_DIR}/stats_messages.proto
+  ${FLPT_MSG_DIR}/time_common.proto
+  ${FLPT_MSG_DIR}/controller_commands.proto
+  ${FLPT_MSG_DIR}/mac_primitives.proto
+  ${FLPT_MSG_DIR}/config_messages.proto
+  ${FLPT_MSG_DIR}/config_common.proto
+  ${FLPT_MSG_DIR}/control_delegation.proto
+  )
 
-  set(FLPT_C_DIR ${protobuf_generated_dir}/${FLPTDIR})
-  #message("calling protoc_call=${protoc_call} FLPT_C_DIR=${FLPT_C_DIR} FLPT_MSG_FILES=${FLPT_MSG_FILES}")
-  execute_process(COMMAND ${protoc_call} ${FLPT_C_DIR} ${FLPT_MSG_DIR} ${FLPT_MSG_FILES})
-  file(GLOB FLPT_source ${FLPT_C_DIR}/*.c)
-  set(FLPT_OAI_generated
-    ${FLPT_C_DIR}/header.pb-c.c
-    ${FLPT_C_DIR}/flexran.pb-c.c
-    ${FLPT_C_DIR}/stats_common.pb-c.c
-    ${FLPT_C_DIR}/stats_messages.pb-c.c
-    ${FLPT_C_DIR}/time_common.pb-c.c
-    ${FLPT_C_DIR}/controller_commands.pb-c.c
-    ${FLPT_C_DIR}/mac_primitives.pb-c.c
-    ${FLPT_C_DIR}/config_messages.pb-c.c
-    ${FLPT_C_DIR}/config_common.pb-c.c
-    ${FLPT_C_DIR}/control_delegation.pb-c.c
-    )
+set(FLPT_C_DIR ${protobuf_generated_dir}/${FLPTDIR})
+#message("calling protoc_call=${protoc_call} FLPT_C_DIR=${FLPT_C_DIR} FLPT_MSG_FILES=${FLPT_MSG_FILES}")
+execute_process(COMMAND ${protoc_call} ${FLPT_C_DIR} ${FLPT_MSG_DIR} ${FLPT_MSG_FILES})
+file(GLOB FLPT_source ${FLPT_C_DIR}/*.c)
+set(FLPT_OAI_generated
+  ${FLPT_C_DIR}/header.pb-c.c
+  ${FLPT_C_DIR}/flexran.pb-c.c
+  ${FLPT_C_DIR}/stats_common.pb-c.c
+  ${FLPT_C_DIR}/stats_messages.pb-c.c
+  ${FLPT_C_DIR}/time_common.pb-c.c
+  ${FLPT_C_DIR}/controller_commands.pb-c.c
+  ${FLPT_C_DIR}/mac_primitives.pb-c.c
+  ${FLPT_C_DIR}/config_messages.pb-c.c
+  ${FLPT_C_DIR}/config_common.pb-c.c
+  ${FLPT_C_DIR}/control_delegation.pb-c.c
+  )
 
-  file(GLOB flpt_h ${FLPT_C_DIR}/*.h)
-  set(flpt_h ${flpt_h} )
+file(GLOB flpt_h ${FLPT_C_DIR}/*.h)
+set(flpt_h ${flpt_h} )
 
-  add_library(FLPT_MSG
-    ${FLPT_OAI_generated}
-    ${FLPT_source}
-    )
-  set(FLPT_MSG_LIB FLPT_MSG)
-  #message("prpt c dir is : ${FLPT_C_DIR}")
-  include_directories (${FLPT_C_DIR})
-
-  add_library(ASYNC_IF
-    ${OPENAIR2_DIR}/UTIL/ASYNC_IF/socket_link.c
-    ${OPENAIR2_DIR}/UTIL/ASYNC_IF/link_manager.c
-    ${OPENAIR2_DIR}/UTIL/ASYNC_IF/message_queue.c
-    ${OPENAIR2_DIR}/UTIL/ASYNC_IF/ringbuffer_queue.c
-    )
-  set(ASYNC_IF_LIB ASYNC_IF)
-  include_directories(${OPENAIR2_DIR}/UTIL/ASYNC_IF)
-
- add_library(FLEXRAN_AGENT
-    ${OPENAIR2_DIR}/ENB_APP/flexran_agent_handler.c
-    ${OPENAIR2_DIR}/ENB_APP/flexran_agent_common.c
-    ${OPENAIR2_DIR}/ENB_APP/flexran_agent_common_internal.c
-    ${OPENAIR2_DIR}/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac.c
-    ${OPENAIR2_DIR}/ENB_APP/flexran_agent.c
-    ${OPENAIR2_DIR}/ENB_APP/flexran_agent_task_manager.c
-    ${OPENAIR2_DIR}/ENB_APP/flexran_agent_net_comm.c
-    ${OPENAIR2_DIR}/ENB_APP/flexran_agent_async.c
-    ${OPENAIR2_DIR}/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac_internal.c
-    )
-  set(FLEXRAN_AGENT_LIB FLEXRAN_AGENT)
-  #include_directories(${OPENAIR2_DIR}/ENB_APP)
+add_library(FLPT_MSG
+  ${FLPT_OAI_generated}
+  ${FLPT_source}
+  )
+set(FLPT_MSG_LIB FLPT_MSG)
+#message("prpt c dir is : ${FLPT_C_DIR}")
+include_directories (${FLPT_C_DIR})
+
+add_library(ASYNC_IF
+  ${OPENAIR2_DIR}/UTIL/ASYNC_IF/socket_link.c
+  ${OPENAIR2_DIR}/UTIL/ASYNC_IF/link_manager.c
+  ${OPENAIR2_DIR}/UTIL/ASYNC_IF/message_queue.c
+  ${OPENAIR2_DIR}/UTIL/ASYNC_IF/ringbuffer_queue.c
+  )
+set(ASYNC_IF_LIB ASYNC_IF)
+include_directories(${OPENAIR2_DIR}/UTIL/ASYNC_IF)
+
+add_library(FLEXRAN_AGENT
+  ${OPENAIR2_DIR}/ENB_APP/flexran_agent_handler.c
+  ${OPENAIR2_DIR}/ENB_APP/flexran_agent_common.c
+  ${OPENAIR2_DIR}/ENB_APP/flexran_agent_ran_api.c
+  ${OPENAIR2_DIR}/ENB_APP/flexran_agent_timer.c
+  ${OPENAIR2_DIR}/ENB_APP/flexran_agent_common_internal.c
+  ${OPENAIR2_DIR}/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac.c
+  ${OPENAIR2_DIR}/ENB_APP/CONTROL_MODULES/RRC/flexran_agent_rrc.c
+  ${OPENAIR2_DIR}/ENB_APP/CONTROL_MODULES/PDCP/flexran_agent_pdcp.c
+  ${OPENAIR2_DIR}/ENB_APP/flexran_agent.c
+  ${OPENAIR2_DIR}/ENB_APP/flexran_agent_task_manager.c
+  ${OPENAIR2_DIR}/ENB_APP/flexran_agent_net_comm.c
+  ${OPENAIR2_DIR}/ENB_APP/flexran_agent_async.c
+  ${OPENAIR2_DIR}/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac_internal.c
+  )
+set(FLEXRAN_AGENT_LIB FLEXRAN_AGENT)
+#include_directories(${OPENAIR2_DIR}/ENB_APP)
 
-  set(PROTOBUF_LIB "protobuf-c")
+set(PROTOBUF_LIB "protobuf-c")
 
-  FIND_PATH(LIBYAML_INCLUDE_DIR NAMES yaml.h)
-  FIND_LIBRARY(LIBYAML_LIBRARIES NAMES yaml libyaml)
+FIND_PATH(LIBYAML_INCLUDE_DIR NAMES yaml.h)
+FIND_LIBRARY(LIBYAML_LIBRARIES NAMES yaml libyaml)
 
-  INCLUDE(FindPackageHandleStandardArgs)
-  FIND_PACKAGE_HANDLE_STANDARD_ARGS(Yaml DEFAULT_MSG LIBYAML_LIBRARIES LIBYAML_INCLUDE_DIR)
-  MARK_AS_ADVANCED(LIBYAML_INCLUDE_DIR LIBYAML_LIBRARIES)
+INCLUDE(FindPackageHandleStandardArgs)
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(Yaml DEFAULT_MSG LIBYAML_LIBRARIES LIBYAML_INCLUDE_DIR)
+MARK_AS_ADVANCED(LIBYAML_INCLUDE_DIR LIBYAML_LIBRARIES)
 
-  #set(PROTOBUF_LIB "protobuf") #for Cpp
-endif()
+#set(PROTOBUF_LIB "protobuf") #for Cpp
 
 
 add_library(HASHTABLE
@@ -1369,17 +1371,6 @@ set (MAC_SRC_UE
   ${MAC_DIR}/config_ue.c
  )
 
-
-if (FLEXRAN_AGENT_SB_IF)
-
-set (MAC_SRC ${MAC_SRC}
-  ${MAC_DIR}/flexran_agent_scheduler_dlsch_ue.c
-  ${MAC_DIR}/flexran_agent_scheduler_dataplane.c
-  ${MAC_DIR}/flexran_agent_scheduler_dlsch_ue_remote.c
-)
-
-endif()
-
 set (ENB_APP_SRC
   ${OPENAIR2_DIR}/ENB_APP/enb_app.c
   ${OPENAIR2_DIR}/ENB_APP/enb_config.c
@@ -1399,14 +1390,6 @@ add_library(L2_UE
 
 include_directories(${NFAPI_USER_DIR})
 
-if (FLEXRAN_AGENT_SB_IF)
-
-#Test for adding a shared library
-add_library(default_sched SHARED ${MAC_DIR}/flexran_agent_scheduler_dlsch_ue.c)
-add_library(remote_sched SHARED ${MAC_DIR}/flexran_agent_scheduler_dlsch_ue_remote.c)
-
-endif()
-
 # L3 Libs
 ##########################
 
@@ -1880,6 +1863,16 @@ if(EXISTS "/usr/include/atlas/cblas.h" OR EXISTS "/usr/include/cblas.h")
   endif()
 
   list(APPEND ATLAS_LIBRARIES lapack)
+
+# for ubuntu 17.10, directories are different
+elseif(EXISTS "/usr/include/x86_64-linux-gnu/cblas.h")
+
+  include_directories("/usr/include/x86_64-linux-gnu")
+  LINK_DIRECTORIES("/usr/lib/x86_64-linux-gnu")
+  list(APPEND ATLAS_LIBRARIES cblas)
+  list(APPEND ATLAS_LIBRARIES atlas)
+  list(APPEND ATLAS_LIBRARIES lapack)
+
 else()
   message("No Blas/Atlas libs found, some targets will fail")
 endif()
@@ -2065,7 +2058,6 @@ add_executable(oaisim
   ${OPENAIR3_DIR}/NAS/UE/nas_ue_task.c
   ${OPENAIR_DIR}/common/utils/utils.c
   ${OPENAIR_DIR}/common/utils/system.c
-  ${GTPU_need_ITTI}
   ${OPENAIR_TARGETS}/COMMON/create_tasks_ue.c
   ${XFORMS_SOURCE}
   ${T_SOURCE}
@@ -2077,14 +2069,13 @@ add_executable(oaisim
 target_include_directories(oaisim PUBLIC  ${OPENAIR_TARGETS}/SIMU/USER)
 target_link_libraries (oaisim
   -Wl,-ldl,--start-group
-  RRC_LIB S1AP_LIB S1AP_ENB X2AP_LIB GTPV1U SECU_CN UTIL HASHTABLE SCTP_CLIENT UDP SCHED_UE_LIB PHY_UE LFDS L2 ${MSC_LIB} LIB_NAS_UE SIMU SECU_OSA ${ITTI_LIB}  ${MIH_LIB}
-  NFAPI_COMMON_LIB NFAPI_LIB NFAPI_VNF_LIB NFAPI_PNF_LIB
-  NFAPI_USER_LIB
+  RRC_LIB S1AP_LIB S1AP_ENB X2AP_LIB SECU_CN UTIL HASHTABLE SCTP_CLIENT UDP SCHED_UE_LIB PHY_UE LFDS L2_UE ${MSC_LIB} LIB_NAS_UE SIMU SECU_OSA ${ITTI_LIB}  ${MIH_LIB}
+  ${FLPT_MSG_LIB} ${ASYNC_IF_LIB} ${FLEXRAN_AGENT_LIB} LFDS7
   -Wl,--end-group z dl)
 
 target_link_libraries (oaisim ${LIBXML2_LIBRARIES} ${LAPACK_LIBRARIES})
 target_link_libraries (oaisim pthread m ${CONFIG_LIBRARIES} rt crypt ${CRYPTO_LIBRARIES} ${OPENSSL_LIBRARIES}  ${NETTLE_LIBRARIES} sctp z
-  ${ATLAS_LIBRARIES} ${XFORMS_LIBRARIES} ${OPENPGM_LIBRARIES} )
+  ${ATLAS_LIBRARIES} ${XFORMS_LIBRARIES} ${OPENPGM_LIBRARIES} ${PROTOBUF_LIB} ${CMAKE_DL_LIBS} ${LIBYAML_LIBRARIES})
 #Force link with forms, regardless XFORMS option
 target_link_libraries (oaisim forms)
 target_link_libraries (oaisim ${T_LIB})
@@ -2297,7 +2288,7 @@ add_custom_command (
 # retrieve the compiler options to send it to gccxml
 get_directory_property(DirDefs COMPILE_DEFINITIONS )
 foreach( d ${DirDefs} )
-  set(module_cc_opt_tmp "${module_cc_opt_tmp} -D${d}")
+  set(module_cc_opt "${module_cc_opt} -D${d}")
 endforeach()
 get_directory_property( DirDefs INCLUDE_DIRECTORIES )
 foreach( d ${DirDefs} )
diff --git a/cmake_targets/build_oai b/cmake_targets/build_oai
index 0d48f51df91bc743d844a3306ef3577b4cd1cb5e..7e0c0ca169c5708b69527fd6db9c0e559032653b 100755
--- a/cmake_targets/build_oai
+++ b/cmake_targets/build_oai
@@ -42,7 +42,6 @@ conf_nvram_path=$OPENAIR_DIR/openair3/NAS/TOOLS/ue_eurecom_test_sfr.conf
 
 MSC_GEN="False"
 XFORMS="True"
-FLEXRAN_AGENT_SB_IF="True"
 PRINT_STATS="False"
 VCD_TIMING="False"
 DEADLINE_SCHEDULER_FLAG_USER="False"
@@ -100,8 +99,6 @@ Options
    Specify conf_nvram_path (default \"$conf_nvram_path\")
 --UE-gen-nvram [output path]
    Specify gen_nvram_path (default \"$gen_nvram_path\")
--a | --agent
-   Enables agent for software-defined control of the eNB
 -r | --3gpp-release
    default is Rel14,
    Rel8 limits the implementation to 3GPP Release 8 version
@@ -204,8 +201,7 @@ function main() {
             echo_info "Will compile eNB"
             shift;;
        -a | --agent)
-	    FLEXRAN_AGENT=1
-	    echo_info "Will compile eNB with agent support"
+	    echo_info "FlexRAN support is always compiled into the eNB"
 	    shift;;
        --UE)
             UE=1
@@ -465,11 +461,9 @@ function main() {
         flash_firmware_bladerf
       fi
     fi
-    if [ "$FLEXRAN_AGENT" == "1" ] ; then
-      echo_info "installing protobuf/protobuf-c for flexran agent support"
-      install_protobuf_from_source
-      install_protobuf_c_from_source
-    fi
+    echo_info "installing protobuf/protobuf-c for flexran agent support"
+    install_protobuf_from_source
+    install_protobuf_c_from_source
   fi
 
   if [ "$INSTALL_OPTIONAL" = "1" ] ; then
@@ -517,9 +511,6 @@ function main() {
     echo "set ( CMAKE_BUILD_TYPE $CMAKE_BUILD_TYPE )" >> $cmake_file
     echo "set ( CFLAGS_PROCESSOR_USER \"$CFLAGS_PROCESSOR_USER\" )" >>  $cmake_file
     echo "set ( XFORMS $XFORMS )"                  >>  $cmake_file
-    if [ "$FLEXRAN_AGENT" = "1" ] ; then
-	echo "set ( FLEXRAN_AGENT_SB_IF $FLEXRAN_AGENT_SB_IF )"      >>  $cmake_file
-    fi
     echo "set ( RRC_ASN1_VERSION \"${REL}\")"      >>  $cmake_file
     echo "set ( ENABLE_VCD_FIFO $VCD_TIMING )"     >>  $cmake_file
     echo "set ( RF_BOARD \"${HW}\")"               >>  $cmake_file
@@ -679,9 +670,6 @@ function main() {
     echo "set ( CMAKE_BUILD_TYPE $CMAKE_BUILD_TYPE )" >> $cmake_file
     echo "set ( CFLAGS_PROCESSOR_USER \"$CFLAGS_PROCESSOR_USER\" )" >>  $cmake_file
     echo "set ( XFORMS $XFORMS )" >>  $cmake_file
-    if [ "$FLEXRAN_AGENT" = "1" ] ; then
-	echo "set ( FLEXRAN_AGENT_SB_IF $FLEXRAN_AGENT_SB_IF )"      >>  $cmake_file
-    fi
     echo "set ( PRINT_STATS $PRINT_STATS )" >>  $cmake_file
     echo "set ( RRC_ASN1_VERSION \"${REL}\")" >>  $cmake_file
     echo "set ( ENABLE_VCD_FIFO $VCD_TIMING )" >>  $cmake_file
@@ -762,9 +750,6 @@ function main() {
     cp $DIR/oaisim_mme_build_oai/CMakeLists.template $cmake_file
     echo "set ( CMAKE_BUILD_TYPE $CMAKE_BUILD_TYPE )" >> $cmake_file
     echo "set ( XFORMS $XFORMS )" >>  $cmake_file
-    if [ "$FLEXRAN_AGENT" = "1" ] ; then
-	echo "set ( FLEXRAN_AGENT_SB_IF $FLEXRAN_AGENT_SB_IF )"      >>  $cmake_file
-    fi
     echo "set ( RRC_ASN1_VERSION \"${REL}\")" >>  $cmake_file
     echo "set ( ENABLE_VCD_FIFO $VCD_TIMING )" >>  $cmake_file
     echo "set ( T_TRACER $T_TRACER )"        >>  $cmake_file
diff --git a/common/ran_context.h b/common/ran_context.h
index 4e25729c86a2377da0fd69b3a23093f8317e92f8..f59fd8a79c466ae96522c93e671dc8811b5e23f9 100644
--- a/common/ran_context.h
+++ b/common/ran_context.h
@@ -40,6 +40,7 @@
 #include "PHY/impl_defs_top.h"
 #include "PHY/impl_defs_lte.h"
 #include "RRC/LITE/defs.h"
+#include "flexran_agent_defs.h"
 
 #include "gtpv1u.h"
 #include "NwGtpv1u.h"
@@ -64,6 +65,8 @@ typedef struct {
   int *nb_L1_CC;
   /// Number of RU instances in this node
   int nb_RU;
+  /// FlexRAN context variables
+  flexran_agent_info_t **flexran;
   /// eNB context variables
   struct PHY_VARS_eNB_s ***eNB;
   /// RRC context variables
diff --git a/common/utils/itti/intertask_interface.c b/common/utils/itti/intertask_interface.c
index 449fa434af09d62c38c3e556bf41cd614514451f..554f50a2a103bf35044030974e8347d5bf22491e 100644
--- a/common/utils/itti/intertask_interface.c
+++ b/common/utils/itti/intertask_interface.c
@@ -637,6 +637,19 @@ void itti_mark_task_ready(task_id_t task_id)
 
 void itti_exit_task(void)
 {
+  task_id_t task_id = itti_get_current_task_id();
+  thread_id_t thread_id = TASK_GET_THREAD_ID(task_id);
+
+#if defined(OAI_EMU) || defined(RTAI)
+  if (task_id > TASK_UNKNOWN) {
+    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLE_ITTI_RECV_MSG,
+                                            __sync_and_and_fetch (&itti_desc.vcd_receive_msg, ~(1L << task_id)));
+  }
+#endif
+
+  itti_desc.threads[thread_id].task_state = TASK_STATE_NOT_CONFIGURED;
+  itti_desc.created_tasks--;
+  ITTI_DEBUG(ITTI_DEBUG_EXIT, "Thread for task %s (%d) exits\n", itti_get_task_name(task_id), task_id);
   pthread_exit (NULL);
 }
 
diff --git a/common/utils/itti/itti_types.h b/common/utils/itti/itti_types.h
index d07853133bae343772b1d3b277602923b5fc3761..2607c003b130f1d261923c6ebae21cec657d42b0 100644
--- a/common/utils/itti/itti_types.h
+++ b/common/utils/itti/itti_types.h
@@ -27,7 +27,18 @@
 #ifndef _ITTI_TYPES_H_
 #define _ITTI_TYPES_H_
 
-#include <stdint.h>
+/* The current file is included in the ue_ip.ko compilation.
+ * For it to work we need to include linux/types.h and
+ * not stdint.h.
+ * A solution to this problem is to use #ifndef __KERNEL__.
+ * Maybe a better solution would be to clean things up
+ * so that ue_ip.ko does not include the current file.
+ */
+#ifndef __KERNEL__
+#  include <stdint.h>
+#else
+#  include <linux/types.h>
+#endif
 
 #define CHARS_TO_UINT32(c1, c2, c3, c4) (((c4) << 24) | ((c3) << 16) | ((c2) << 8) | (c1))
 
diff --git a/common/utils/msc/msc.c b/common/utils/msc/msc.c
index 307eeb30a51c8a1216d57f87f4c89a327a93d166..bb88b9b4b6a4441eba3b65ee40b31198efb8b5b0 100644
--- a/common/utils/msc/msc.c
+++ b/common/utils/msc/msc.c
@@ -99,6 +99,7 @@ void *msc_task(void *args_p)
         break;
 
         case TERMINATE_MESSAGE: {
+          fprintf(stderr, " *** Exiting MSC thread\n");
           timer_remove(timer_id);
     	  msc_end();
           itti_exit_task();
diff --git a/openair1/PHY/CODING/3gpplte_turbo_decoder_avx2_16bit.c b/openair1/PHY/CODING/3gpplte_turbo_decoder_avx2_16bit.c
index 616eed7446df4d611a4addba06cec2e90b1a32aa..83a50f214b202be4823acbc667d5c7041b61b1e6 100644
--- a/openair1/PHY/CODING/3gpplte_turbo_decoder_avx2_16bit.c
+++ b/openair1/PHY/CODING/3gpplte_turbo_decoder_avx2_16bit.c
@@ -830,10 +830,10 @@ void free_td16avx2(void)
   int ind;
 
   for (ind=0; ind<188; ind++) {
-    free(pi2tab16avx2[ind]);
-    free(pi5tab16avx2[ind]);
-    free(pi4tab16avx2[ind]);
-    free(pi6tab16avx2[ind]);
+    free_and_zero(pi2tab16avx2[ind]);
+    free_and_zero(pi5tab16avx2[ind]);
+    free_and_zero(pi4tab16avx2[ind]);
+    free_and_zero(pi6tab16avx2[ind]);
   }
 }
 
diff --git a/openair1/PHY/CODING/3gpplte_turbo_decoder_sse.c b/openair1/PHY/CODING/3gpplte_turbo_decoder_sse.c
index 9ec25367d9c435af597c124a13f74af80d35f577..f628bff55b7c08223ad13efff2807c8a9a4fb658 100644
--- a/openair1/PHY/CODING/3gpplte_turbo_decoder_sse.c
+++ b/openair1/PHY/CODING/3gpplte_turbo_decoder_sse.c
@@ -1907,6 +1907,18 @@ void compute_ext(llr_t* alpha,llr_t* beta,llr_t* m_11,llr_t* m_10,llr_t* ext, ll
 //int pi2[n],pi3[n+8],pi5[n+8],pi4[n+8],pi6[n+8],
 int *pi2tab[188],*pi5tab[188],*pi4tab[188],*pi6tab[188];
 
+void free_td()
+{
+  int ind;
+
+  for (ind = 0; ind < 188; ind++) {
+    free_and_zero(pi2tab[ind]);
+    free_and_zero(pi5tab[ind]);
+    free_and_zero(pi4tab[ind]);
+    free_and_zero(pi6tab[ind]);
+  }
+}
+
 void init_td()
 {
 
diff --git a/openair1/PHY/CODING/3gpplte_turbo_decoder_sse_16bit.c b/openair1/PHY/CODING/3gpplte_turbo_decoder_sse_16bit.c
index cb4a4e1f84124553c09391c6649b67921b2bafb7..a32edd711d338698b0acdaf5949d19b200a2b24e 100644
--- a/openair1/PHY/CODING/3gpplte_turbo_decoder_sse_16bit.c
+++ b/openair1/PHY/CODING/3gpplte_turbo_decoder_sse_16bit.c
@@ -1117,10 +1117,10 @@ void free_td16(void)
   int ind;
 
   for (ind=0; ind<188; ind++) {
-    free(pi2tab16[ind]);
-    free(pi5tab16[ind]);
-    free(pi4tab16[ind]);
-    free(pi6tab16[ind]);
+    free_and_zero(pi2tab16[ind]);
+    free_and_zero(pi5tab16[ind]);
+    free_and_zero(pi4tab16[ind]);
+    free_and_zero(pi6tab16[ind]);
   }
 }
 
diff --git a/openair1/PHY/CODING/3gpplte_turbo_decoder_sse_8bit.c b/openair1/PHY/CODING/3gpplte_turbo_decoder_sse_8bit.c
index d6ef84b218388dcdf0ad9e24e4391c85fe9bc82a..4754e26f38bc673595f16377682b37601869c713 100644
--- a/openair1/PHY/CODING/3gpplte_turbo_decoder_sse_8bit.c
+++ b/openair1/PHY/CODING/3gpplte_turbo_decoder_sse_8bit.c
@@ -838,10 +838,10 @@ void free_td8(void)
   int ind;
 
   for (ind=0; ind<188; ind++) {
-    free(pi2tab8[ind]);
-    free(pi5tab8[ind]);
-    free(pi4tab8[ind]);
-    free(pi6tab8[ind]);
+    free_and_zero(pi2tab8[ind]);
+    free_and_zero(pi5tab8[ind]);
+    free_and_zero(pi4tab8[ind]);
+    free_and_zero(pi6tab8[ind]);
   }
 }
 
diff --git a/openair1/PHY/CODING/defs.h b/openair1/PHY/CODING/defs.h
index 80e28b15867e79244181cfe8c02fc28000f22a56..22339953c39afee26c0808b18bdb7e43ad846a9a 100644
--- a/openair1/PHY/CODING/defs.h
+++ b/openair1/PHY/CODING/defs.h
@@ -352,10 +352,17 @@ void ccodedab_init_inv(void);
 \brief This function initializes the different crc tables.*/
 void crcTableInit (void);
 
+/*!\fn void free_td8(void)
+\brief This function frees the tables for 8-bit LLR Turbo decoder.*/
+void free_td8(void);
+
 /*!\fn void init_td8(void)
 \brief This function initializes the tables for 8-bit LLR Turbo decoder.*/
 void init_td8 (void);
 
+/*!\fn void free_td16(void)
+\brief This function frees the tables for 16-bit LLR Turbo decoder.*/
+void free_td16(void);
 
 /*!\fn void init_td16(void)
 \brief This function initializes the tables for 16-bit LLR Turbo decoder.*/
@@ -366,6 +373,9 @@ void init_td16 (void);
 \brief This function initializes the tables for 8-bit LLR Turbo decoder (AVX2).*/
 void init_td8avx2 (void);
 
+/*!\fn void free_td16avx2(void)
+\brief This function frees the tables for 16-bit LLR Turbo decoder (AVX2).*/
+void free_td16avx2(void);
 
 /*!\fn void init_td16(void)
 \brief This function initializes the tables for 16-bit LLR Turbo decoder (AVX2).*/
diff --git a/openair1/PHY/INIT/defs.h b/openair1/PHY/INIT/defs.h
index 744fb3d3ceb30ec24e58780daaaaae5e35353a20..55d220824e23df0808e6750fd21f5d656f76399e 100644
--- a/openair1/PHY/INIT/defs.h
+++ b/openair1/PHY/INIT/defs.h
@@ -90,6 +90,13 @@ int phy_init_lte_eNB(PHY_VARS_eNB *phy_vars_eNb,
                      unsigned char is_secondary_eNb,
                      unsigned char abstraction_flag);
 
+/*!
+\brief Free the PHY variables relevant to the LTE implementation (eNB).
+\details Only a subset of phy_vars_eNb is freed (those who have been allocated with phy_init_lte_eNB()).
+@param[in] phy_vars_eNb Pointer to eNB Variables
+ */
+void phy_free_lte_eNB(PHY_VARS_eNB *phy_vars_eNb);
+
 /** \brief Configure LTE_DL_FRAME_PARMS with components derived after initial synchronization (MIB decoding + primary/secondary synch).
 \details The basically allows configuration of \f$N_{\mathrm{RB}}^{\mathrm{DL}}\f$, the cell id  \f$N_{\mathrm{ID}}^{\mathrm{cell}}\f$, the normal/extended prefix mode, the frame type (FDD/TDD), \f$N_{\mathrm{cp}}\f$, the number of TX antennas at eNB (\f$p\f$) and the number of PHICH groups, \f$N_{\mathrm{group}}^{\mathrm{PHICH}}\f$
 @param lte_frame_parms pointer to LTE parameter structure
@@ -317,6 +324,7 @@ void phy_config_dedicated_eNB_step2(PHY_VARS_eNB *phy_vars_eNB);
  */
 int phy_init_secsys_eNB(PHY_VARS_eNB *phy_vars_eNb);
 
+void free_lte_top(void);
 
 void init_lte_top(LTE_DL_FRAME_PARMS *lte_frame_parms);
 
diff --git a/openair1/PHY/INIT/init_top.c b/openair1/PHY/INIT/init_top.c
index 43f2de92d764719d330949c4c7685eaf54901dd9..a1edd9d1eab1de74f6920f0e4afc401d9a3d6996 100644
--- a/openair1/PHY/INIT/init_top.c
+++ b/openair1/PHY/INIT/init_top.c
@@ -59,6 +59,18 @@ void init_lte_top(LTE_DL_FRAME_PARMS *frame_parms)
 
 }
 
+void free_lte_top(void)
+{
+  free_td8();
+  free_td16();
+#ifdef __AVX2__
+  free_td16avx2();
+#endif
+  lte_sync_time_free();
+
+  /* free_ul_ref_sigs() is called in phy_free_lte_eNB() */
+}
+
 
 /*
  * @}*/
diff --git a/openair1/PHY/INIT/lte_init.c b/openair1/PHY/INIT/lte_init.c
index 377a235da67c775740dcc70b3bdc2cbd43c41808..9df6e9fc66acf6d5c061a8a535dd78d3f3cf17c8 100644
--- a/openair1/PHY/INIT/lte_init.c
+++ b/openair1/PHY/INIT/lte_init.c
@@ -882,7 +882,7 @@ int phy_init_lte_eNB(PHY_VARS_eNB *eNB,
     AssertFatal(fp->N_RB_UL > 5, "fp->N_RB_UL %d < 6\n",fp->N_RB_UL);
     for (i=0; i<2; i++) {
       // RK 2 times because of output format of FFT!
-      // FIXME We should get rid of this
+      // FIXME We should get rid of this, consider also phy_free_lte_eNB()
       pusch_vars[UE_id]->rxdataF_ext[i]      = (int32_t*)malloc16_clear( 2*sizeof(int32_t)*fp->N_RB_UL*12*fp->symbols_per_tti );
       pusch_vars[UE_id]->rxdataF_ext2[i]     = (int32_t*)malloc16_clear( sizeof(int32_t)*fp->N_RB_UL*12*fp->symbols_per_tti );
       pusch_vars[UE_id]->drs_ch_estimates[i] = (int32_t*)malloc16_clear( sizeof(int32_t)*fp->N_RB_UL*12*fp->symbols_per_tti );
@@ -906,6 +906,80 @@ int phy_init_lte_eNB(PHY_VARS_eNB *eNB,
 
 }
 
+void phy_free_lte_eNB(PHY_VARS_eNB *eNB)
+{
+  LTE_DL_FRAME_PARMS* const fp       = &eNB->frame_parms;
+  LTE_eNB_COMMON* const common_vars  = &eNB->common_vars;
+  LTE_eNB_PUSCH** const pusch_vars   = eNB->pusch_vars;
+  LTE_eNB_SRS* const srs_vars        = eNB->srs_vars;
+  LTE_eNB_PRACH* const prach_vars    = &eNB->prach_vars;
+#ifdef Rel14
+  LTE_eNB_PRACH* const prach_vars_br = &eNB->prach_vars_br;
+#endif
+  int i, UE_id;
+
+  for (i = 0; i < NB_ANTENNA_PORTS_ENB; i++) {
+    if (i < fp->nb_antenna_ports_eNB || i == 5) {
+      free_and_zero(common_vars->txdataF[i]);
+      /* rxdataF[i] is not allocated -> don't free */
+    }
+  }
+  free_and_zero(common_vars->txdataF);
+  free_and_zero(common_vars->rxdataF);
+
+  // Channel estimates for SRS
+  for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
+    for (i=0; i<64; i++) {
+      free_and_zero(srs_vars[UE_id].srs_ch_estimates[i]);
+      free_and_zero(srs_vars[UE_id].srs_ch_estimates_time[i]);
+    }
+    free_and_zero(srs_vars[UE_id].srs_ch_estimates);
+    free_and_zero(srs_vars[UE_id].srs_ch_estimates_time);
+  } //UE_id
+
+  free_ul_ref_sigs();
+
+  for (UE_id=0; UE_id<NUMBER_OF_UE_MAX; UE_id++) free_and_zero(srs_vars[UE_id].srs);
+
+  free_and_zero(prach_vars->prachF);
+
+  for (i = 0; i < 64; i++) free_and_zero(prach_vars->prach_ifft[0][i]);
+  free_and_zero(prach_vars->prach_ifft[0]);
+
+#ifdef Rel14
+  for (int ce_level = 0; ce_level < 4; ce_level++) {
+    for (i = 0; i < 64; i++) free_and_zero(prach_vars_br->prach_ifft[ce_level][i]);
+    free_and_zero(prach_vars_br->prach_ifft[ce_level]);
+    free_and_zero(prach_vars->rxsigF[ce_level]);
+  }
+  free_and_zero(prach_vars_br->prachF);
+#endif
+  free_and_zero(prach_vars->rxsigF[0]);
+
+  for (UE_id=0; UE_id<NUMBER_OF_UE_MAX; UE_id++) {
+    for (i = 0; i < 2; i++) {
+      free_and_zero(pusch_vars[UE_id]->rxdataF_ext[i]);
+      free_and_zero(pusch_vars[UE_id]->rxdataF_ext2[i]);
+      free_and_zero(pusch_vars[UE_id]->drs_ch_estimates[i]);
+      free_and_zero(pusch_vars[UE_id]->drs_ch_estimates_time[i]);
+      free_and_zero(pusch_vars[UE_id]->rxdataF_comp[i]);
+      free_and_zero(pusch_vars[UE_id]->ul_ch_mag[i]);
+      free_and_zero(pusch_vars[UE_id]->ul_ch_magb[i]);
+    }
+    free_and_zero(pusch_vars[UE_id]->rxdataF_ext);
+    free_and_zero(pusch_vars[UE_id]->rxdataF_ext2);
+    free_and_zero(pusch_vars[UE_id]->drs_ch_estimates);
+    free_and_zero(pusch_vars[UE_id]->drs_ch_estimates_time);
+    free_and_zero(pusch_vars[UE_id]->rxdataF_comp);
+    free_and_zero(pusch_vars[UE_id]->ul_ch_mag);
+    free_and_zero(pusch_vars[UE_id]->ul_ch_magb);
+    free_and_zero(pusch_vars[UE_id]->llr);
+    free_and_zero(pusch_vars[UE_id]);
+  } //UE_id
+
+  for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) eNB->UE_stats_ptr[UE_id] = NULL;
+}
+
 void install_schedule_handlers(IF_Module_t *if_inst)
 {
   if_inst->PHY_config_req = phy_config_request;
diff --git a/openair1/PHY/INIT/lte_init_ru.c b/openair1/PHY/INIT/lte_init_ru.c
index 2d139e0afafcba2c2f72555744c74b5e3cd61e24..eb56cb04d3cbe159f9a2049803058247c12e8e42 100644
--- a/openair1/PHY/INIT/lte_init_ru.c
+++ b/openair1/PHY/INIT/lte_init_ru.c
@@ -148,3 +148,51 @@ int phy_init_RU(RU_t *ru) {
 
   return(0);
 }
+
+void phy_free_RU(RU_t *ru)
+{
+  int i,j;
+  int p;
+
+  LOG_I(PHY, "Feeing RU signal buffers (if_south %s) nb_tx %d\n", ru_if_types[ru->if_south], ru->nb_tx);
+
+  if (ru->if_south <= REMOTE_IF5) { // this means REMOTE_IF5 or LOCAL_RF, so free memory for time-domain signals
+    for (i = 0; i < ru->nb_tx; i++) free_and_zero(ru->common.txdata[i]);
+    for (i = 0; i < ru->nb_rx; i++) free_and_zero(ru->common.rxdata[i]);
+    free_and_zero(ru->common.txdata);
+    free_and_zero(ru->common.rxdata);
+  } // else: IF5 or local RF -> nothing to free()
+
+  if (ru->function != NGFI_RRU_IF5) { // we need to do RX/TX RU processing
+    for (i = 0; i < ru->nb_rx; i++) free_and_zero(ru->common.rxdata_7_5kHz[i]);
+    free_and_zero(ru->common.rxdata_7_5kHz);
+
+    // free IFFT input buffers (TX)
+    for (i = 0; i < ru->nb_tx; i++) free_and_zero(ru->common.txdataF_BF[i]);
+    free_and_zero(ru->common.txdataF_BF);
+
+    // free FFT output buffers (RX)
+    for (i = 0; i < ru->nb_rx; i++) free_and_zero(ru->common.rxdataF[i]);
+    free_and_zero(ru->common.rxdataF);
+
+    for (i = 0; i < ru->nb_rx; i++) {
+      free_and_zero(ru->prach_rxsigF[i]);
+#ifdef Rel14
+      for (j = 0; j < 4; j++) free_and_zero(ru->prach_rxsigF_br[j][i]);
+#endif
+    }
+    for (j = 0; j < 4; j++) free_and_zero(ru->prach_rxsigF_br[j]);
+    free_and_zero(ru->prach_rxsigF);
+    /* ru->prach_rxsigF_br is not allocated -> don't free */
+
+    for (i = 0; i < RC.nb_L1_inst; i++) {
+      for (p = 0; p < 15; p++) {
+	if (p < ru->eNB_list[i]->frame_parms.nb_antenna_ports_eNB || p == 5) {
+	  for (j=0; j<ru->nb_tx; j++) free_and_zero(ru->beam_weights[i][p][j]);
+	  free_and_zero(ru->beam_weights[i][p]);
+	}
+      }
+    }
+  }
+  free_and_zero(ru->common.sync_corr);
+}
diff --git a/openair1/PHY/LTE_REFSIG/lte_ul_ref.c b/openair1/PHY/LTE_REFSIG/lte_ul_ref.c
index cddb642b2b41818f8d814dfd3e2eeb0b4e260bd3..93ca7646e63cdaabfc0d229d2e8241cdf8480541 100644
--- a/openair1/PHY/LTE_REFSIG/lte_ul_ref.c
+++ b/openair1/PHY/LTE_REFSIG/lte_ul_ref.c
@@ -185,14 +185,18 @@ void free_ul_ref_sigs(void)
 
   unsigned int u,v,Msc_RS;
 
-  for (Msc_RS=2; Msc_RS<33; Msc_RS++) {
+  for (Msc_RS=0; Msc_RS<33; Msc_RS++) {
     for (u=0; u<30; u++) {
       for (v=0; v<2; v++) {
-        if (ul_ref_sigs[u][v][Msc_RS])
+        if (ul_ref_sigs[u][v][Msc_RS]) {
           free16(ul_ref_sigs[u][v][Msc_RS],2*sizeof(int16_t)*dftsizes[Msc_RS]);
+          ul_ref_sigs[u][v][Msc_RS] = NULL;
+        }
 
-        if (ul_ref_sigs_rx[u][v][Msc_RS])
+        if (ul_ref_sigs_rx[u][v][Msc_RS]) {
           free16(ul_ref_sigs_rx[u][v][Msc_RS],4*sizeof(int16_t)*dftsizes[Msc_RS]);
+          ul_ref_sigs_rx[u][v][Msc_RS] = NULL;
+        }
       }
     }
   }
diff --git a/openair1/PHY/LTE_TRANSPORT/dci_tools.c b/openair1/PHY/LTE_TRANSPORT/dci_tools.c
index c6c13225042f65637608c8b3e060d2e0b16830ea..086f77e14cbda7dd83ba74923c46d323f2d60ed0 100644
--- a/openair1/PHY/LTE_TRANSPORT/dci_tools.c
+++ b/openair1/PHY/LTE_TRANSPORT/dci_tools.c
@@ -6556,7 +6556,7 @@ uint8_t pdcch_alloc2ul_subframe(LTE_DL_FRAME_PARMS *frame_parms,uint8_t n)
   else
     ul_subframe = ((n+4)%10);
 
-  //  AssertFatal(frame_parms->frame_type == FDD || subframe_select(frame_parms,ul_subframe) == SF_UL,"illegal ul_subframe %d (n %d)\n",ul_subframe,n);
+  AssertFatal(frame_parms->frame_type == FDD || subframe_select(frame_parms,ul_subframe) == SF_UL,"illegal ul_subframe %d (n %d)\n",ul_subframe,n);
 
   LOG_D(PHY, "subframe %d: PUSCH subframe = %d\n", n, ul_subframe);
   return ul_subframe;
diff --git a/openair1/PHY/LTE_TRANSPORT/dlsch_coding.c b/openair1/PHY/LTE_TRANSPORT/dlsch_coding.c
index bd3c92454f4be0375d355b624bd83bae51415b07..f1cb58ba26bdd83c27ef1f61bd9484f534ec199b 100644
--- a/openair1/PHY/LTE_TRANSPORT/dlsch_coding.c
+++ b/openair1/PHY/LTE_TRANSPORT/dlsch_coding.c
@@ -56,42 +56,20 @@
 
 void free_eNB_dlsch(LTE_eNB_DLSCH_t *dlsch)
 {
-  int i;
-  int r;
+  int i, r, aa, layer;
 
   if (dlsch) {
-#ifdef DEBUG_DLSCH_FREE
-    printf("Freeing dlsch %p\n",dlsch);
-#endif
-
+    for (layer=0; layer<4; layer++) {
+      for (aa=0; aa<64; aa++) free16(dlsch->ue_spec_bf_weights[layer][aa], OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES*sizeof(int32_t));
+      free16(dlsch->ue_spec_bf_weights[layer], 64*sizeof(int32_t*));
+    }
     for (i=0; i<dlsch->Mdlharq; i++) {
-#ifdef DEBUG_DLSCH_FREE
-      printf("Freeing dlsch process %d\n",i);
-#endif
-
       if (dlsch->harq_processes[i]) {
-#ifdef DEBUG_DLSCH_FREE
-        printf("Freeing dlsch process %d (%p)\n",i,dlsch->harq_processes[i]);
-#endif
-
         if (dlsch->harq_processes[i]->b) {
           free16(dlsch->harq_processes[i]->b,MAX_DLSCH_PAYLOAD_BYTES);
           dlsch->harq_processes[i]->b = NULL;
-#ifdef DEBUG_DLSCH_FREE
-          printf("Freeing dlsch process %d b (%p)\n",i,dlsch->harq_processes[i]->b);
-#endif
         }
-
-#ifdef DEBUG_DLSCH_FREE
-        printf("Freeing dlsch process %d c (%p)\n",i,dlsch->harq_processes[i]->c);
-#endif
-
         for (r=0; r<MAX_NUM_DLSCH_SEGMENTS; r++) {
-
-#ifdef DEBUG_DLSCH_FREE
-          printf("Freeing dlsch process %d c[%d] (%p)\n",i,r,dlsch->harq_processes[i]->c[r]);
-#endif
-
           if (dlsch->harq_processes[i]->c[r]) {
             free16(dlsch->harq_processes[i]->c[r],((r==0)?8:0) + 3+768);
             dlsch->harq_processes[i]->c[r] = NULL;
@@ -100,17 +78,14 @@ void free_eNB_dlsch(LTE_eNB_DLSCH_t *dlsch)
             free16(dlsch->harq_processes[i]->d[r],(96+12+3+(3*6144)));
             dlsch->harq_processes[i]->d[r] = NULL;
           }
-
 	}
 	free16(dlsch->harq_processes[i],sizeof(LTE_DL_eNB_HARQ_t));
 	dlsch->harq_processes[i] = NULL;
       }
     }
-
     free16(dlsch,sizeof(LTE_eNB_DLSCH_t));
     dlsch = NULL;
-    }
-
+  }
 }
 
 LTE_eNB_DLSCH_t *new_eNB_dlsch(unsigned char Kmimo,unsigned char Mdlharq,uint32_t Nsoft,unsigned char N_RB_DL, uint8_t abstraction_flag, LTE_DL_FRAME_PARMS* frame_parms)
diff --git a/openair1/PHY/LTE_TRANSPORT/proto.h b/openair1/PHY/LTE_TRANSPORT/proto.h
index be6d2861f2ef6c60d2ab0befd9d04869d6aafd3a..199427cbc2fdb21c54b061ebd7a7a8340ce09dad 100644
--- a/openair1/PHY/LTE_TRANSPORT/proto.h
+++ b/openair1/PHY/LTE_TRANSPORT/proto.h
@@ -83,6 +83,12 @@ void clean_eNb_ulsch(LTE_eNB_ULSCH_t *ulsch);
 
 void free_ue_ulsch(LTE_UE_ULSCH_t *ulsch);
 
+/** \fn free_eNB_ulsch(LTE_eNB_DLSCH_t *dlsch)
+    \brief This function frees memory allocated for a particular ULSCH at eNB
+    @param ulsch Pointer to ULSCH to be removed
+*/
+void free_eNB_ulsch(LTE_eNB_ULSCH_t *ulsch);
+
 LTE_eNB_ULSCH_t *new_eNB_ulsch(uint8_t max_turbo_iterations,uint8_t N_RB_UL, uint8_t abstraction_flag);
 
 LTE_UE_ULSCH_t *new_ue_ulsch(unsigned char N_RB_UL, uint8_t abstraction_flag);
diff --git a/openair1/PHY/LTE_TRANSPORT/ulsch_coding.c b/openair1/PHY/LTE_TRANSPORT/ulsch_coding.c
index 87209e190b1749d27c9f96d3c04a3359eefb49c7..9b761f3cc3b4065ba462aa52f18299b62443fa94 100644
--- a/openair1/PHY/LTE_TRANSPORT/ulsch_coding.c
+++ b/openair1/PHY/LTE_TRANSPORT/ulsch_coding.c
@@ -66,33 +66,12 @@ void free_ue_ulsch(LTE_UE_ULSCH_t *ulsch)
 #endif
 
     for (i=0; i<8; i++) {
-#ifdef DEBUG_ULSCH_FREE
-      printf("Freeing ulsch process %d\n",i);
-#endif
-
       if (ulsch->harq_processes[i]) {
-#ifdef DEBUG_ULSCH_FREE
-        printf("Freeing ulsch process %d (%p)\n",i,ulsch->harq_processes[i]);
-#endif
-
         if (ulsch->harq_processes[i]->b) {
           free16(ulsch->harq_processes[i]->b,MAX_ULSCH_PAYLOAD_BYTES);
           ulsch->harq_processes[i]->b = NULL;
-#ifdef DEBUG_ULSCH_FREE
-          printf("Freeing ulsch process %d b (%p)\n",i,ulsch->harq_processes[i]->b);
-#endif
         }
-
-#ifdef DEBUG_ULSCH_FREE
-        printf("Freeing ulsch process %d c (%p)\n",i,ulsch->harq_processes[i]->c);
-#endif
-
         for (r=0; r<MAX_NUM_ULSCH_SEGMENTS; r++) {
-
-#ifdef DEBUG_ULSCH_FREE
-          printf("Freeing ulsch process %d c[%d] (%p)\n",i,r,ulsch->harq_processes[i]->c[r]);
-#endif
-
           if (ulsch->harq_processes[i]->c[r]) {
             free16(ulsch->harq_processes[i]->c[r],((r==0)?8:0) + 3+768);
             ulsch->harq_processes[i]->c[r] = NULL;
@@ -103,7 +82,6 @@ void free_ue_ulsch(LTE_UE_ULSCH_t *ulsch)
         ulsch->harq_processes[i] = NULL;
       }
     }
-
     free16(ulsch,sizeof(LTE_UE_ULSCH_t));
     ulsch = NULL;
   }
diff --git a/openair1/PHY/defs.h b/openair1/PHY/defs.h
index 38da7e4e1e41456a979ac69b1cab36267f0a5a05..46f37e943102c0a986302f2264c91bcf9da841eb 100644
--- a/openair1/PHY/defs.h
+++ b/openair1/PHY/defs.h
@@ -79,6 +79,12 @@
 #define bigmalloc16 malloc16
 #define openair_free(y,x) free((y))
 #define PAGE_SIZE 4096
+#define free_and_zero(PtR) do { \
+      if (PtR) {           \
+        free(PtR);         \
+        PtR = NULL;        \
+      }                    \
+    } while (0)
 
 #define RX_NB_TH_MAX 2
 #define RX_NB_TH 2
diff --git a/openair1/SCHED/fapi_l1.c b/openair1/SCHED/fapi_l1.c
index b9024bdfbf94d12ebd5ff26b125b99931cff765f..9c855d0ac844c0790b4f2eec9f7add5444f21835 100644
--- a/openair1/SCHED/fapi_l1.c
+++ b/openair1/SCHED/fapi_l1.c
@@ -629,7 +629,8 @@ void schedule_response(Sched_Rsp_t *Sched_INFO)
   fp          = &eNB->frame_parms;
   proc        = &eNB->proc.proc_rxtx[0];
 
-  if ((fp->frame_type == TDD) && (subframe_select(fp,subframe)==SF_UL)) return;
+  /* TODO: check that following line is correct - in the meantime it is disabled */
+  //if ((fp->frame_type == TDD) && (subframe_select(fp,subframe)==SF_UL)) return;
 
   ul_subframe = pdcch_alloc2ul_subframe(fp,subframe);
   ul_frame    = pdcch_alloc2ul_frame(fp,frame,subframe);
@@ -664,8 +665,10 @@ void schedule_response(Sched_Rsp_t *Sched_INFO)
 
   int do_oai =0;
   int dont_send =0;
-  if ((ul_subframe<10)&&
-      (subframe_select(fp,ul_subframe)==SF_UL)) { // This means that there is an ul_subframe that can be configured here
+  /* TODO: check the following test - in the meantime it is put back as it was before */
+  //if ((ul_subframe<10)&&
+  //    (subframe_select(fp,ul_subframe)==SF_UL)) { // This means that there is an ul_subframe that can be configured here
+  if (ul_subframe<10) { // This means that there is an ul_subframe that can be configured here
     LOG_D(PHY,"NFAPI: Clearing dci allocations for potential UL subframe %d\n",ul_subframe);
   
     harq_pid = subframe2harq_pid(fp,ul_frame,ul_subframe);
diff --git a/openair1/SCHED/phy_procedures_lte_eNb.c b/openair1/SCHED/phy_procedures_lte_eNb.c
index 2ae79b53b6d9bb688105355e51864e2f2bdbb26b..9aaf6752fb23835471e674384e137de434d08268 100644
--- a/openair1/SCHED/phy_procedures_lte_eNb.c
+++ b/openair1/SCHED/phy_procedures_lte_eNb.c
@@ -452,8 +452,10 @@ void phy_procedures_eNB_TX(PHY_VARS_eNB *eNB,
   }
 
   /* save old HARQ information needed for PHICH generation */
-  if ((ul_subframe < 10)&&
-      (subframe_select(fp,ul_subframe)==SF_UL)) { // This means that there is a potential UL subframe that will be scheduled here
+  /* TODO: check the following test - in the meantime it is put back as it was before */
+  //if ((ul_subframe < 10)&&
+  //    (subframe_select(fp,ul_subframe)==SF_UL)) { // This means that there is a potential UL subframe that will be scheduled here
+  if (ul_subframe < 10) { // This means that there is a potential UL subframe that will be scheduled here
     for (i=0; i<NUMBER_OF_UE_MAX; i++) {
       harq_pid = subframe2harq_pid(fp,ul_frame,ul_subframe);
       if (eNB->ulsch[i]) {
diff --git a/openair2/COMMON/platform_types.h b/openair2/COMMON/platform_types.h
index 1ce10c56bcea7929046cea6104f8cfdd8f8bf7a2..0ab8d9670145ceab059356b9d6675e94b786b2b7 100644
--- a/openair2/COMMON/platform_types.h
+++ b/openair2/COMMON/platform_types.h
@@ -69,6 +69,7 @@ typedef uint32_t              frame_t;
 typedef int32_t               sframe_t;
 typedef uint32_t              sub_frame_t;
 typedef uint8_t               module_id_t;
+typedef uint8_t               slice_id_t;
 typedef uint8_t               eNB_index_t;
 typedef uint16_t              ue_id_t;
 typedef int16_t               smodule_id_t;
@@ -99,6 +100,15 @@ typedef enum rb_type_e {
   RADIO_ACCESS_BEARER         = 2
 } rb_type_t;
 
+typedef enum {
+    CR_ROUND = 0,
+    CR_SRB12 = 1,
+    CR_HOL   = 2,
+    CR_LC    = 3,
+    CR_CQI   = 4,
+    CR_NUM   = 5
+} sorting_criterion_t;
+
 //-----------------------------------------------------------------------------
 // PHY TYPES
 //-----------------------------------------------------------------------------
diff --git a/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac.c b/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac.c
index fcf9fb537c013180a016509d5873505b1b60c961..230917f673e33af455b864cd06d4cd50b56fd4b8 100644
--- a/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac.c
+++ b/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac.c
@@ -31,10 +31,10 @@
 #include "flexran_agent_common.h"
 #include "flexran_agent_mac_internal.h"
 #include "flexran_agent_net_comm.h"
+#include "flexran_agent_timer.h"
+#include "flexran_agent_ran_api.h"
 
 #include "LAYER2/MAC/proto.h"
-#include "LAYER2/MAC/flexran_agent_mac_proto.h"
-#include "LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.h"
 
 #include "liblfds700.h"
 
@@ -54,683 +54,499 @@ struct lfds700_ringbuffer_element *dl_mac_config_array[NUM_MAX_ENB];
 struct lfds700_ringbuffer_state ringbuffer_state[NUM_MAX_ENB];
 
 
-int flexran_agent_mac_handle_stats(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg){
+int flexran_agent_mac_stats_reply(mid_t mod_id,       
+          const report_config_t *report_config,
+           Protocol__FlexUeStatsReport **ue_report,
+           Protocol__FlexCellStatsReport **cell_report) {
 
-  // TODO: Must deal with sanitization of input
-  // TODO: Must check if RNTIs and cell ids of the request actually exist
-  // TODO: Must resolve conflicts among stats requests
 
-  int i;
-  err_code_t err_code;
-  xid_t xid;
-  uint32_t usec_interval, sec_interval;
-
-  //TODO: We do not deal with multiple CCs at the moment and eNB id is 0
+  // Protocol__FlexHeader *header;
+  int i, j, k;
+  // int cc_id = 0;
   int enb_id = mod_id;
 
-  //eNB_MAC_INST *eNB = &eNB_mac_inst[enb_id];
-  //UE_list_t *eNB_UE_list=  &eNB->UE_list;
-
-  report_config_t report_config;
-
-  uint32_t ue_flags = 0;
-  uint32_t c_flags = 0;
-
-  Protocol__FlexranMessage *input = (Protocol__FlexranMessage *)params;
-
-  Protocol__FlexStatsRequest *stats_req = input->stats_request_msg;
-  xid = (stats_req->header)->xid;
-
-  // Check the type of request that is made
-  switch(stats_req->body_case) {
-  case PROTOCOL__FLEX_STATS_REQUEST__BODY_COMPLETE_STATS_REQUEST: ;
-    Protocol__FlexCompleteStatsRequest *comp_req = stats_req->complete_stats_request;
-    if (comp_req->report_frequency == PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_OFF) {
-      /*Disable both periodic and continuous updates*/
-      flexran_agent_disable_cont_mac_stats_update(mod_id);
-      flexran_agent_destroy_timer_by_task_id(xid);
-      *msg = NULL;
-      return 0;
-    } else { //One-off, periodical or continuous reporting
-      //Set the proper flags
-      ue_flags = comp_req->ue_report_flags;
-      c_flags = comp_req->cell_report_flags;
-      //Create a list of all eNB RNTIs and cells
-
-      //Set the number of UEs and create list with their RNTIs stats configs
-      report_config.nr_ue = flexran_get_num_ues(mod_id); //eNB_UE_list->num_UEs
-      report_config.ue_report_type = (ue_report_type_t *) malloc(sizeof(ue_report_type_t) * report_config.nr_ue);
-      if (report_config.ue_report_type == NULL) {
-	// TODO: Add appropriate error code
-	err_code = -100;
-	goto error;
-      }
-      for (i = 0; i < report_config.nr_ue; i++) {
-	report_config.ue_report_type[i].ue_rnti = flexran_get_ue_crnti(enb_id, i); //eNB_UE_list->eNB_UE_stats[UE_PCCID(enb_id,i)][i].crnti;
-	report_config.ue_report_type[i].ue_report_flags = ue_flags;
-      }
-      //Set the number of CCs and create a list with the cell stats configs
-      report_config.nr_cc = MAX_NUM_CCs;
-      report_config.cc_report_type = (cc_report_type_t *) malloc(sizeof(cc_report_type_t) * report_config.nr_cc);
-      if (report_config.cc_report_type == NULL) {
-	// TODO: Add appropriate error code
-	err_code = -100;
-	goto error;
-      }
-      for (i = 0; i < report_config.nr_cc; i++) {
-	//TODO: Must fill in the proper cell ids
-	report_config.cc_report_type[i].cc_id = i;
-	report_config.cc_report_type[i].cc_report_flags = c_flags;
-      }
-      /* Check if request was periodical */
-      if (comp_req->report_frequency == PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_PERIODICAL) {
-	/* Create a one off flexran message as an argument for the periodical task */
-	Protocol__FlexranMessage *timer_msg;
-	stats_request_config_t request_config;
-	request_config.report_type = PROTOCOL__FLEX_STATS_TYPE__FLST_COMPLETE_STATS;
-	request_config.report_frequency = PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_ONCE;
-	request_config.period = 0;
-	/* Need to make sure that the ue flags are saved (Bug) */
-	if (report_config.nr_ue == 0) {
-	  report_config.nr_ue = 1;
-	  report_config.ue_report_type = (ue_report_type_t *) malloc(sizeof(ue_report_type_t));
-	   if (report_config.ue_report_type == NULL) {
-	     // TODO: Add appropriate error code
-	     err_code = -100;
-	     goto error;
-	   }
-	   report_config.ue_report_type[0].ue_rnti = 0; // Dummy value
-	   report_config.ue_report_type[0].ue_report_flags = ue_flags;
-	}
-	request_config.config = &report_config;
-	flexran_agent_mac_stats_request(enb_id, xid, &request_config, &timer_msg);
-	/* Create a timer */
-	long timer_id = 0;
-	flexran_agent_timer_args_t *timer_args;
-	timer_args = malloc(sizeof(flexran_agent_timer_args_t));
-	memset (timer_args, 0, sizeof(flexran_agent_timer_args_t));
-	timer_args->mod_id = enb_id;
-	timer_args->msg = timer_msg;
-	/*Convert subframes to usec time*/
-	usec_interval = 1000*comp_req->sf;
-	sec_interval = 0;
-	/*add seconds if required*/
-	if (usec_interval >= 1000*1000) {
-	  sec_interval = usec_interval/(1000*1000);
-	  usec_interval = usec_interval%(1000*1000);
-	}
-	flexran_agent_create_timer(sec_interval, usec_interval, FLEXRAN_AGENT_DEFAULT, enb_id, FLEXRAN_AGENT_TIMER_TYPE_PERIODIC, xid, flexran_agent_handle_timed_task,(void*) timer_args, &timer_id);
-      } else if (comp_req->report_frequency == PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_CONTINUOUS) {
-	/*If request was for continuous updates, disable the previous configuration and
-	  set up a new one*/
-	flexran_agent_disable_cont_mac_stats_update(mod_id);
-	stats_request_config_t request_config;
-	request_config.report_type = PROTOCOL__FLEX_STATS_TYPE__FLST_COMPLETE_STATS;
-	request_config.report_frequency = PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_ONCE;
-	request_config.period = 0;
-	/* Need to make sure that the ue flags are saved (Bug) */
-	if (report_config.nr_ue == 0) {
-	  report_config.nr_ue = 1;
-	  report_config.ue_report_type = (ue_report_type_t *) malloc(sizeof(ue_report_type_t));
-	  if (report_config.ue_report_type == NULL) {
-	    // TODO: Add appropriate error code
-	    err_code = -100;
-	    goto error;
-	  }
-	  report_config.ue_report_type[0].ue_rnti = 0; // Dummy value
-	  report_config.ue_report_type[0].ue_report_flags = ue_flags;
-	}
-	request_config.config = &report_config;
-	flexran_agent_enable_cont_mac_stats_update(enb_id, xid, &request_config);
-      }
-    }
-    break;
-  case PROTOCOL__FLEX_STATS_REQUEST__BODY_CELL_STATS_REQUEST:;
-    Protocol__FlexCellStatsRequest *cell_req = stats_req->cell_stats_request;
-    // UE report config will be blank
-    report_config.nr_ue = 0;
-    report_config.ue_report_type = NULL;
-    report_config.nr_cc = cell_req->n_cell;
-    report_config.cc_report_type = (cc_report_type_t *) malloc(sizeof(cc_report_type_t) * report_config.nr_cc);
-    if (report_config.cc_report_type == NULL) {
-      // TODO: Add appropriate error code
-      err_code = -100;
-      goto error;
-    }
-    for (i = 0; i < report_config.nr_cc; i++) {
-	//TODO: Must fill in the proper cell ids
-      report_config.cc_report_type[i].cc_id = cell_req->cell[i];
-      report_config.cc_report_type[i].cc_report_flags = cell_req->flags;
-    }
-    break;
-  case PROTOCOL__FLEX_STATS_REQUEST__BODY_UE_STATS_REQUEST:;
-    Protocol__FlexUeStatsRequest *ue_req = stats_req->ue_stats_request;
-    // Cell report config will be blank
-    report_config.nr_cc = 0;
-    report_config.cc_report_type = NULL;
-    report_config.nr_ue = ue_req->n_rnti;
-    report_config.ue_report_type = (ue_report_type_t *) malloc(sizeof(ue_report_type_t) * report_config.nr_ue);
-    if (report_config.ue_report_type == NULL) {
-      // TODO: Add appropriate error code
-      err_code = -100;
-      goto error;
-    }
-    for (i = 0; i < report_config.nr_ue; i++) {
-      report_config.ue_report_type[i].ue_rnti = ue_req->rnti[i];
-      report_config.ue_report_type[i].ue_report_flags = ue_req->flags;
-    }
-    break;
-  default:
-    //TODO: Add appropriate error code
-    err_code = -100;
-    goto error;
-  }
-
-  if (flexran_agent_mac_stats_reply(enb_id, xid, &report_config, msg) < 0 ){
-    err_code = PROTOCOL__FLEXRAN_ERR__MSG_BUILD;
-    goto error;
-  }
-
-  free(report_config.ue_report_type);
-  free(report_config.cc_report_type);
-
-  return 0;
-
- error :
-  LOG_E(FLEXRAN_AGENT, "errno %d occured\n", err_code);
-  return err_code;
-}
-
-int flexran_agent_mac_stats_request(mid_t mod_id,
-				    xid_t xid,
-				    const stats_request_config_t *report_config,
-				    Protocol__FlexranMessage **msg) {
-  Protocol__FlexHeader *header;
-  int i;
+  /* Allocate memory for list of UE reports */
+  if (report_config->nr_ue > 0) {
 
-  Protocol__FlexStatsRequest *stats_request_msg;
-  stats_request_msg = malloc(sizeof(Protocol__FlexStatsRequest));
-  if(stats_request_msg == NULL)
-    goto error;
-  protocol__flex_stats_request__init(stats_request_msg);
+          
+          for (i = 0; i < report_config->nr_ue; i++) {
+
+
+
+                /* Check flag for creation of buffer status report */
+                if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_BSR) {
+                      //TODO should be automated
+                        ue_report[i]->n_bsr = 4; 
+                        uint32_t *elem;
+                        elem = (uint32_t *) malloc(sizeof(uint32_t)*ue_report[i]->n_bsr);
+                        if (elem == NULL)
+                               goto error;
+                        for (j = 0; j < ue_report[i]->n_bsr; j++) {
+                                // NN: we need to know the cc_id here, consider the first one 
+                                elem[j] = flexran_get_ue_bsr_ul_buffer_info (enb_id, i, j);
+                        }
+                          
+                        ue_report[i]->bsr = elem;
+                }
+                
+                /* Check flag for creation of PHR report */
+                if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PHR) {
+                        ue_report[i]->phr = flexran_get_ue_phr (enb_id, i); // eNB_UE_list->UE_template[UE_PCCID(enb_id,i)][i].phr_info;
+                        ue_report[i]->has_phr = 1;
+                      
+                }
+
+                /* Check flag for creation of RLC buffer status report */
+                if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_RLC_BS) {
+                        ue_report[i]->n_rlc_report = 3; // Set this to the number of LCs for this UE. This needs to be generalized for for LCs
+                        Protocol__FlexRlcBsr ** rlc_reports;
+                        rlc_reports = malloc(sizeof(Protocol__FlexRlcBsr *) * ue_report[i]->n_rlc_report);
+                        if (rlc_reports == NULL)
+                              goto error;
+
+                        // NN: see LAYER2/openair2_proc.c for rlc status
+                        for (j = 0; j < ue_report[i]->n_rlc_report; j++) {
+
+                              rlc_reports[j] = malloc(sizeof(Protocol__FlexRlcBsr));
+                              if (rlc_reports[j] == NULL)
+                                 goto error;
+                              protocol__flex_rlc_bsr__init(rlc_reports[j]);
+                              rlc_reports[j]->lc_id = j+1;
+                              rlc_reports[j]->has_lc_id = 1;
+                              rlc_reports[j]->tx_queue_size = flexran_get_tx_queue_size(enb_id, i, j + 1);
+                              rlc_reports[j]->has_tx_queue_size = 1;
+
+                              //TODO:Set tx queue head of line delay in ms
+                              rlc_reports[j]->tx_queue_hol_delay = flexran_get_hol_delay(enb_id, i, j + 1);
+                              rlc_reports[j]->has_tx_queue_hol_delay = 1;
+                              //TODO:Set retransmission queue size in bytes
+                              rlc_reports[j]->retransmission_queue_size = 10;
+                              rlc_reports[j]->has_retransmission_queue_size = 0;
+                              //TODO:Set retransmission queue head of line delay in ms
+                              rlc_reports[j]->retransmission_queue_hol_delay = 100;
+                              rlc_reports[j]->has_retransmission_queue_hol_delay = 0;
+                              //TODO DONE:Set current size of the pending message in bytes
+                              rlc_reports[j]->status_pdu_size = flexran_get_num_pdus_buffer(enb_id , i, j + 1);
+                              rlc_reports[j]->has_status_pdu_size = 1;
+
+                        }
+                        // Add RLC buffer status reports to the full report
+                        if (ue_report[i]->n_rlc_report > 0)
+                            ue_report[i]->rlc_report = rlc_reports;
+
+                      
+                }
+
+                /* Check flag for creation of MAC CE buffer status report */
+                if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_MAC_CE_BS) {
+                        // TODO: Fill in the actual MAC CE buffer status report
+                        ue_report[i]->pending_mac_ces = (flexran_get_MAC_CE_bitmap_TA(enb_id,i,0) | (0 << 1) | (0 << 2) | (0 << 3)) & 15; 
+                                      // Use as bitmap. Set one or more of the; /* Use as bitmap. Set one or more of the
+                                       // PROTOCOL__FLEX_CE_TYPE__FLPCET_ values
+                                       // found in stats_common.pb-c.h. See
+                                       // flex_ce_type in FlexRAN specification 
+                        ue_report[i]->has_pending_mac_ces = 1;
+                  
+                }
+
+                /* Check flag for creation of DL CQI report */
+                if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_DL_CQI) {
+                        // TODO: Fill in the actual DL CQI report for the UE based on its configuration
+                        Protocol__FlexDlCqiReport * dl_report;
+                        dl_report = malloc(sizeof(Protocol__FlexDlCqiReport));
+                        if (dl_report == NULL)
+                          goto error;
+                        protocol__flex_dl_cqi_report__init(dl_report);
+
+                        dl_report->sfn_sn = flexran_get_sfn_sf(enb_id);
+                        dl_report->has_sfn_sn = 1;
+                        //Set the number of DL CQI reports for this UE. One for each CC
+                        dl_report->n_csi_report = flexran_get_active_CC(enb_id,i);
+                        dl_report->n_csi_report = 1 ;
+                        //Create the actual CSI reports.
+                        Protocol__FlexDlCsi **csi_reports;
+                        csi_reports = malloc(sizeof(Protocol__FlexDlCsi *)*dl_report->n_csi_report);
+                        if (csi_reports == NULL)
+                          goto error;                    
+                        for (j = 0; j < dl_report->n_csi_report; j++) {
+
+                              csi_reports[j] = malloc(sizeof(Protocol__FlexDlCsi));
+                              if (csi_reports[j] == NULL)
+                                goto error;
+                              protocol__flex_dl_csi__init(csi_reports[j]);
+                              //The servCellIndex for this report
+                              csi_reports[j]->serv_cell_index = j;
+                              csi_reports[j]->has_serv_cell_index = 1;
+                              //The rank indicator value for this cc
+                              csi_reports[j]->ri = flexran_get_current_RI(enb_id,i,j);
+                              csi_reports[j]->has_ri = 1;
+                              //TODO: the type of CSI report based on the configuration of the UE
+                              //For now we only support type P10, which only needs a wideband value
+                              //The full set of types can be found in stats_common.pb-c.h and
+                              //in the FlexRAN specifications
+                              csi_reports[j]->type =  PROTOCOL__FLEX_CSI_TYPE__FLCSIT_P10;
+                              csi_reports[j]->has_type = 1;
+                              csi_reports[j]->report_case = PROTOCOL__FLEX_DL_CSI__REPORT_P10CSI;
+
+                              if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_P10CSI){
+
+                                    Protocol__FlexCsiP10 *csi10;
+                                    csi10 = malloc(sizeof(Protocol__FlexCsiP10));
+                                    if (csi10 == NULL)
+                                    goto error;
+                                    protocol__flex_csi_p10__init(csi10);
+                                    //TODO: set the wideband value
+                                    // NN: this is also depends on cc_id
+                                    csi10->wb_cqi = flexran_get_ue_wcqi (enb_id, i); //eNB_UE_list->eNB_UE_stats[UE_PCCID(enb_id,i)][i].dl_cqi;
+                                    csi10->has_wb_cqi = 1;
+                                    //Add the type of measurements to the csi report in the proper union type
+                                    csi_reports[j]->p10csi = csi10;
+                              }
+
+                              else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_P11CSI){
+
+
+                                    Protocol__FlexCsiP11 *csi11;
+                                    csi11 = malloc(sizeof(Protocol__FlexCsiP11));
+                                    if (csi11 == NULL)
+                                    goto error;
+                                    protocol__flex_csi_p11__init(csi11);
+                                  
+                                    csi11->wb_cqi = malloc(sizeof(csi11->wb_cqi));  
+				    csi11->n_wb_cqi = 1;
+				    csi11->wb_cqi[0] = flexran_get_ue_wcqi (enb_id, i);                                       		    
+                                    // According To spec 36.213                                  
+                                     
+                                    if (flexran_get_antenna_ports(enb_id, j) == 2 && csi_reports[j]->ri == 1) {
+                                        // TODO PMI
+                                        csi11->wb_pmi = flexran_get_ue_wpmi(enb_id, i, 0);
+                                        csi11->has_wb_pmi = 1;
+
+                                       }   
+
+                                      else if (flexran_get_antenna_ports(enb_id, j) == 2 && csi_reports[j]->ri == 2){
+                                        // TODO PMI
+                                        csi11->wb_pmi = flexran_get_ue_wpmi(enb_id, i, 0);
+                                        csi11->has_wb_pmi = 1;
+
+                                      }
+
+                                      else if (flexran_get_antenna_ports(enb_id, j) == 4 && csi_reports[j]->ri == 2){
+                                        // TODO PMI
+                                        csi11->wb_pmi = flexran_get_ue_wpmi(enb_id, i, 0);
+                                        csi11->has_wb_pmi = 1;
+
+
+                                      }
+
+                                      csi11->has_wb_pmi = 0;                                      
+
+                                      csi_reports[j]->p11csi = csi11;
+
+                               }
+
+                                      
+                                     
+
+
+                              else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_P20CSI){
+
+                                    Protocol__FlexCsiP20 *csi20;
+                                    csi20 = malloc(sizeof(Protocol__FlexCsiP20));
+                                    if (csi20 == NULL)
+                                    goto error;
+                                    protocol__flex_csi_p20__init(csi20);
+                                    
+                                    csi20->wb_cqi = flexran_get_ue_wcqi (enb_id, i);                                       
+                                    csi20->has_wb_cqi = 1;
 
-  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_STATS_REQUEST, &header) != 0)
-    goto error;
+                                      
+                                    csi20->bandwidth_part_index = 1 ;//TODO
+                                    csi20->has_bandwidth_part_index = 1;
 
-  stats_request_msg->header = header;
+                                    csi20->sb_index = 1 ;//TODO
+                                    csi20->has_sb_index = 1 ;                                     
 
-  stats_request_msg->type = report_config->report_type;
-  stats_request_msg->has_type = 1;
 
-  switch (report_config->report_type) {
-  case PROTOCOL__FLEX_STATS_TYPE__FLST_COMPLETE_STATS:
-    stats_request_msg->body_case =  PROTOCOL__FLEX_STATS_REQUEST__BODY_COMPLETE_STATS_REQUEST;
-    Protocol__FlexCompleteStatsRequest *complete_stats;
-    complete_stats = malloc(sizeof(Protocol__FlexCompleteStatsRequest));
-    if(complete_stats == NULL)
-      goto error;
-    protocol__flex_complete_stats_request__init(complete_stats);
-    complete_stats->report_frequency = report_config->report_frequency;
-    complete_stats->has_report_frequency = 1;
-    complete_stats->sf = report_config->period;
-    complete_stats->has_sf = 1;
-    complete_stats->has_cell_report_flags = 1;
-    complete_stats->has_ue_report_flags = 1;
-    if (report_config->config->nr_cc > 0) {
-      complete_stats->cell_report_flags = report_config->config->cc_report_type[0].cc_report_flags;
-    }
-    if (report_config->config->nr_ue > 0) {
-      complete_stats->ue_report_flags = report_config->config->ue_report_type[0].ue_report_flags;
-    }
-    stats_request_msg->complete_stats_request = complete_stats;
-    break;
-  case  PROTOCOL__FLEX_STATS_TYPE__FLST_CELL_STATS:
-    stats_request_msg->body_case = PROTOCOL__FLEX_STATS_REQUEST__BODY_CELL_STATS_REQUEST;
-     Protocol__FlexCellStatsRequest *cell_stats;
-     cell_stats = malloc(sizeof(Protocol__FlexCellStatsRequest));
-    if(cell_stats == NULL)
-      goto error;
-    protocol__flex_cell_stats_request__init(cell_stats);
-    cell_stats->n_cell = report_config->config->nr_cc;
-    cell_stats->has_flags = 1;
-    if (cell_stats->n_cell > 0) {
-      uint32_t *cells;
-      cells = (uint32_t *) malloc(sizeof(uint32_t)*cell_stats->n_cell);
-      for (i = 0; i < cell_stats->n_cell; i++) {
-	cells[i] = report_config->config->cc_report_type[i].cc_id;
-      }
-      cell_stats->cell = cells;
-      cell_stats->flags = report_config->config->cc_report_type[i].cc_report_flags;
-    }
-    stats_request_msg->cell_stats_request = cell_stats;
-    break;
-  case PROTOCOL__FLEX_STATS_TYPE__FLST_UE_STATS:
-    stats_request_msg->body_case = PROTOCOL__FLEX_STATS_REQUEST__BODY_UE_STATS_REQUEST;
-     Protocol__FlexUeStatsRequest *ue_stats;
-     ue_stats = malloc(sizeof(Protocol__FlexUeStatsRequest));
-    if(ue_stats == NULL)
-      goto error;
-    protocol__flex_ue_stats_request__init(ue_stats);
-    ue_stats->n_rnti = report_config->config->nr_ue;
-    ue_stats->has_flags = 1;
-    if (ue_stats->n_rnti > 0) {
-      uint32_t *ues;
-      ues = (uint32_t *) malloc(sizeof(uint32_t)*ue_stats->n_rnti);
-      for (i = 0; i < ue_stats->n_rnti; i++) {
-	ues[i] = report_config->config->ue_report_type[i].ue_rnti;
-      }
-      ue_stats->rnti = ues;
-      ue_stats->flags = report_config->config->ue_report_type[i].ue_report_flags;
-    }
-    stats_request_msg->ue_stats_request = ue_stats;
-    break;
-  default:
-    goto error;
-  }
-  *msg = malloc(sizeof(Protocol__FlexranMessage));
-  if(*msg == NULL)
-    goto error;
-  protocol__flexran_message__init(*msg);
-  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REQUEST_MSG;
-  (*msg)->msg_dir = PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
-  (*msg)->stats_request_msg = stats_request_msg;
-  return 0;
+                                    csi_reports[j]->p20csi = csi20;
 
- error:
-  // TODO: Need to make proper error handling
-  if (header != NULL)
-    free(header);
-  if (stats_request_msg != NULL)
-    free(stats_request_msg);
-  if(*msg != NULL)
-    free(*msg);
-  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
-  return -1;
-}
 
-int flexran_agent_mac_destroy_stats_request(Protocol__FlexranMessage *msg) {
-   if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REQUEST_MSG)
-    goto error;
-  free(msg->stats_request_msg->header);
-  if (msg->stats_request_msg->body_case == PROTOCOL__FLEX_STATS_REQUEST__BODY_CELL_STATS_REQUEST) {
-    free(msg->stats_request_msg->cell_stats_request->cell);
-  }
-  if (msg->stats_request_msg->body_case == PROTOCOL__FLEX_STATS_REQUEST__BODY_UE_STATS_REQUEST) {
-    free(msg->stats_request_msg->ue_stats_request->rnti);
-  }
-  free(msg->stats_request_msg);
-  free(msg);
-  return 0;
+                              }
 
- error:
-  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
-  return -1;
-}
+                              else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_P21CSI){
 
-int flexran_agent_mac_stats_reply(mid_t mod_id,
-				  xid_t xid,
-				  const report_config_t *report_config,
-				  Protocol__FlexranMessage **msg) {
-  Protocol__FlexHeader *header;
-  int i, j, k;
-  int enb_id = mod_id;
-  //eNB_MAC_INST *eNB = &eNB_mac_inst[enb_id];
-  //UE_list_t *eNB_UE_list=  &eNB->UE_list;
+                                  // Protocol__FlexCsiP21 *csi21;
+                                  // csi21 = malloc(sizeof(Protocol__FlexCsiP21));
+                                  // if (csi21 == NULL)
+                                  // goto error;
+                                  // protocol__flex_csi_p21__init(csi21);
+                                
+                                  // csi21->wb_cqi = flexran_get_ue_wcqi (enb_id, i);                                       
+                                  
+                                  
+                                  // csi21->wb_pmi = flexran_get_ue_pmi(enb_id); //TDO inside
+                                  // csi21->has_wb_pmi = 1;
 
-  Protocol__FlexStatsReply *stats_reply_msg;
-  stats_reply_msg = malloc(sizeof(Protocol__FlexStatsReply));
-  if (stats_reply_msg == NULL)
-    goto error;
-  protocol__flex_stats_reply__init(stats_reply_msg);
+                                  // csi21->sb_cqi = 1; // TODO 
+                                   
+                                  // csi21->bandwidth_part_index = 1 ; //TDO inside
+                                  // csi21->has_bandwidth_part_index = 1 ;   
 
-  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_STATS_REPLY, &header) != 0)
-    goto error;
+                                  // csi21->sb_index = 1 ;//TODO
+                                  // csi21->has_sb_index = 1 ;                                     
 
-  stats_reply_msg->header = header;
 
-  stats_reply_msg->n_ue_report = report_config->nr_ue;
-  stats_reply_msg->n_cell_report = report_config->nr_cc;
+                                  // csi_reports[j]->p20csi = csi21;
 
-  Protocol__FlexUeStatsReport **ue_report;
-  Protocol__FlexCellStatsReport **cell_report;
+                              }
 
+                              else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A12CSI){
 
-  /* Allocate memory for list of UE reports */
-  if (report_config->nr_ue > 0) {
-    ue_report = malloc(sizeof(Protocol__FlexUeStatsReport *) * report_config->nr_ue);
-    if (ue_report == NULL)
-      goto error;
-    for (i = 0; i < report_config->nr_ue; i++) {
-      ue_report[i] = malloc(sizeof(Protocol__FlexUeStatsReport));
-      protocol__flex_ue_stats_report__init(ue_report[i]);
-      ue_report[i]->rnti = report_config->ue_report_type[i].ue_rnti;
-      ue_report[i]->has_rnti = 1;
-      ue_report[i]->flags = report_config->ue_report_type[i].ue_report_flags;
-      ue_report[i]->has_flags = 1;
-      /* Check the types of reports that need to be constructed based on flag values */
-
-      /* Check flag for creation of buffer status report */
-      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_BSR) {
-	ue_report[i]->n_bsr = 4;
-	uint32_t *elem;
-	elem = (uint32_t *) malloc(sizeof(uint32_t)*ue_report[i]->n_bsr);
-	if (elem == NULL)
-	  goto error;
-	for (j = 0; j < ue_report[i]->n_bsr; j++) {
-	  // NN: we need to know the cc_id here, consider the first one
-	  elem[j] = flexran_get_ue_bsr (enb_id, i, j); 
-	}
-	ue_report[i]->bsr = elem;
-      }
 
-      /* Check flag for creation of PRH report */
-      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PRH) {
-	ue_report[i]->phr = flexran_get_ue_phr (enb_id, i); // eNB_UE_list->UE_template[UE_PCCID(enb_id,i)][i].phr_info;
-	ue_report[i]->has_phr = 1;
-      }
+                                  // Protocol__FlexCsiA12 *csi12;
+                                  // csi12 = malloc(sizeof(Protocol__FlexCsiA12));
+                                  // if (csi12 == NULL)
+                                  // goto error;
+                                  // protocol__flex_csi_a12__init(csi12);
+                                
+                                  // csi12->wb_cqi = flexran_get_ue_wcqi (enb_id, i);                                       
+                                  
+                                  // csi12->sb_pmi = 1 ; //TODO inside                                                                      
 
-      /* Check flag for creation of RLC buffer status report */
-      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_RLC_BS) {
-	ue_report[i]->n_rlc_report = 3; // Set this to the number of LCs for this UE. This needs to be generalized for for LCs
-	Protocol__FlexRlcBsr ** rlc_reports;
-	rlc_reports = malloc(sizeof(Protocol__FlexRlcBsr *) * ue_report[i]->n_rlc_report);
-	if (rlc_reports == NULL)
-	  goto error;
-
-	// NN: see LAYER2/openair2_proc.c for rlc status
-	for (j = 0; j < ue_report[i]->n_rlc_report; j++) {
-	  rlc_reports[j] = malloc(sizeof(Protocol__FlexRlcBsr));
-	  if (rlc_reports[j] == NULL)
-	    goto error;
-	  protocol__flex_rlc_bsr__init(rlc_reports[j]);
-	  rlc_reports[j]->lc_id = j + 1;
-	  rlc_reports[j]->has_lc_id = 1;
-	  rlc_reports[j]->tx_queue_size = flexran_get_tx_queue_size(enb_id,i,j + 1);
-	  rlc_reports[j]->has_tx_queue_size = 1;
-	  
-	  //TODO:Set tx queue head of line delay in ms
-	  rlc_reports[j]->tx_queue_hol_delay = flexran_get_hol_delay(enb_id, i, j+1);
-	  rlc_reports[j]->has_tx_queue_hol_delay = 1;
-	  //TODO:Set retransmission queue size in bytes
-	  rlc_reports[j]->retransmission_queue_size = 10;
-	  rlc_reports[j]->has_retransmission_queue_size = 0;
-	  //TODO:Set retransmission queue head of line delay in ms
-	  rlc_reports[j]->retransmission_queue_hol_delay = 100;
-	  rlc_reports[j]->has_retransmission_queue_hol_delay = 0;
-	  //TODO:Set current size of the pending message in bytes
-	  rlc_reports[j]->status_pdu_size = 100;
-	  rlc_reports[j]->has_status_pdu_size = 0;
-	}
-	// Add RLC buffer status reports to the full report
-	if (ue_report[i]->n_rlc_report > 0)
-	  ue_report[i]->rlc_report = rlc_reports;
-      }
+                                  // TODO continou
+                              }
 
-      /* Check flag for creation of MAC CE buffer status report */
-      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_MAC_CE_BS) {
-	// TODO: Fill in the actual MAC CE buffer status report
-	ue_report[i]->pending_mac_ces = (flexran_get_MAC_CE_bitmap_TA(enb_id,i,0) | (0 << 1) | (0 << 2) | (0 << 3)) & 15;  /* Use as bitmap. Set one or more of the
-					       PROTOCOL__FLEX_CE_TYPE__FLPCET_ values
-					       found in stats_common.pb-c.h. See
-					       flex_ce_type in FlexRAN specification */
-	ue_report[i]->has_pending_mac_ces = 1;
-      }
+                              else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A22CSI){
 
-      /* Check flag for creation of DL CQI report */
-      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_DL_CQI) {
-	// TODO: Fill in the actual DL CQI report for the UE based on its configuration
-	Protocol__FlexDlCqiReport * dl_report;
-	dl_report = malloc(sizeof(Protocol__FlexDlCqiReport));
-	if (dl_report == NULL)
-	  goto error;
-	protocol__flex_dl_cqi_report__init(dl_report);
-
-	dl_report->sfn_sn = flexran_get_sfn_sf(enb_id);
-	dl_report->has_sfn_sn = 1;
-	//Set the number of DL CQI reports for this UE. One for each CC
-	dl_report->n_csi_report = flexran_get_active_CC(enb_id,i);
-
-	//Create the actual CSI reports.
-	Protocol__FlexDlCsi **csi_reports;
-	csi_reports = malloc(sizeof(Protocol__FlexDlCsi *)*dl_report->n_csi_report);
-	if (csi_reports == NULL)
-	  goto error;
-	for (j = 0; j < dl_report->n_csi_report; j++) {
-	  csi_reports[j] = malloc(sizeof(Protocol__FlexDlCsi));
-	  if (csi_reports[j] == NULL)
-	    goto error;
-	  protocol__flex_dl_csi__init(csi_reports[j]);
-	  //The servCellIndex for this report
-	  csi_reports[j]->serv_cell_index = j;
-	  csi_reports[j]->has_serv_cell_index = 1;
-	  //The rank indicator value for this cc
-	  csi_reports[j]->ri = flexran_get_current_RI(enb_id,i,j);
-	  csi_reports[j]->has_ri = 1;
-	  //TODO: the type of CSI report based on the configuration of the UE
-	  //For now we only support type P10, which only needs a wideband value
-	  //The full set of types can be found in stats_common.pb-c.h and
-	  //in the FlexRAN specifications
-    csi_reports[j]->type =  PROTOCOL__FLEX_CSI_TYPE__FLCSIT_P10;
-		  csi_reports[j]->has_type = 1;
-		  csi_reports[j]->report_case = PROTOCOL__FLEX_DL_CSI__REPORT_P10CSI;
-		  if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_P10CSI){
-			  Protocol__FlexCsiP10 *csi10;
-			  csi10 = malloc(sizeof(Protocol__FlexCsiP10));
-			  if (csi10 == NULL)
-				goto error;
-			  protocol__flex_csi_p10__init(csi10);
-			  //TODO: set the wideband value
-			  // NN: this is also depends on cc_id
-			  csi10->wb_cqi = flexran_get_ue_wcqi (enb_id, i); //eNB_UE_list->eNB_UE_stats[UE_PCCID(enb_id,i)][i].dl_cqi;
-			  csi10->has_wb_cqi = 1;
-			  //Add the type of measurements to the csi report in the proper union type
-			  csi_reports[j]->p10csi = csi10;
-		  }
-		  else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_P11CSI){
-
-		  }
-		  else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_P20CSI){
-
-		  }
-		  else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_P21CSI){
-
-		  }
-		  else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A12CSI){
-
-		  }
-		  else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A22CSI){
-
-		  }
-		  else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A20CSI){
-
-		  }
-		  else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A30CSI){
-
-		  }
-		  else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A31CSI){
-
-		  }
-		}
-	//Add the csi reports to the full DL CQI report
-	dl_report->csi_report = csi_reports;
-	//Add the DL CQI report to the stats report
-	ue_report[i]->dl_cqi_report = dl_report;
-      }
+                                    // Protocol__FlexCsiA22 *csi22;
+                                    // csi22 = malloc(sizeof(Protocol__FlexCsiA22));
+                                    // if (csi22 == NULL)
+                                    // goto error;
+                                    // protocol__flex_csi_a22__init(csi22);
+                                  
+                                    // csi22->wb_cqi = flexran_get_ue_wcqi (enb_id, i);                                       
+                                    
+                                    // csi22->sb_cqi = 1 ; //TODO inside                                      
 
-      /* Check flag for creation of paging buffer status report */
-      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PBS) {
-	//TODO: Fill in the actual paging buffer status report. For this field to be valid, the RNTI
-	//set in the report must be a P-RNTI
-	Protocol__FlexPagingBufferReport *paging_report;
-	paging_report = malloc(sizeof(Protocol__FlexPagingBufferReport));
-	if (paging_report == NULL)
-	  goto error;
-	protocol__flex_paging_buffer_report__init(paging_report);
-	//Set the number of pending paging messages
-	paging_report->n_paging_info = 1;
-	//Provide a report for each pending paging message
-	Protocol__FlexPagingInfo **p_info;
-	p_info = malloc(sizeof(Protocol__FlexPagingInfo *) * paging_report->n_paging_info);
-	if (p_info == NULL)
-	  goto error;
-	for (j = 0; j < paging_report->n_paging_info; j++) {
-	  p_info[j] = malloc(sizeof(Protocol__FlexPagingInfo));
-	  if(p_info[j] == NULL)
-	    goto error;
-	  protocol__flex_paging_info__init(p_info[j]);
-	  //TODO: Set paging index. This index is the same that will be used for the scheduling of the
-	  //paging message by the controller
-	  p_info[j]->paging_index = 10;
-	  p_info[j]->has_paging_index = 0;
-	  //TODO:Set the paging message size
-	  p_info[j]->paging_message_size = 100;
-	  p_info[j]->has_paging_message_size = 0;
-	  //TODO: Set the paging subframe
-	  p_info[j]->paging_subframe = 10;
-	  p_info[j]->has_paging_subframe = 0;
-	  //TODO: Set the carrier index for the pending paging message
-	  p_info[j]->carrier_index = 0;
-	  p_info[j]->has_carrier_index = 0;
-	}
-	//Add all paging info to the paging buffer rerport
-	paging_report->paging_info = p_info;
-	//Add the paging report to the UE report
-	ue_report[i]->pbr = paging_report;
-      }
+                                    // csi22->wb_pmi = flexran_get_ue_wcqi (enb_id, i);                                       
+                                    // csi22->has_wb_pmi = 1;
+                                    
+                                    // csi22->sb_pmi = 1 ; //TODO inside                                                                            
+                                    // csi22->has_wb_pmi = 1;
 
-      /* Check flag for creation of UL CQI report */
-      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_UL_CQI) {
-	//Fill in the full UL CQI report of the UE
-	Protocol__FlexUlCqiReport *full_ul_report;
-	full_ul_report = malloc(sizeof(Protocol__FlexUlCqiReport));
-	if(full_ul_report == NULL)
-	  goto error;
-	protocol__flex_ul_cqi_report__init(full_ul_report);
-	//TODO:Set the SFN and SF of the generated report
-	full_ul_report->sfn_sn = flexran_get_sfn_sf(enb_id);
-	full_ul_report->has_sfn_sn = 1;
-	//TODO:Set the number of UL measurement reports based on the types of measurements
-	//configured for this UE and on the servCellIndex
-	full_ul_report->n_cqi_meas = 1;
-	Protocol__FlexUlCqi **ul_report;
-	ul_report = malloc(sizeof(Protocol__FlexUlCqi *) * full_ul_report->n_cqi_meas);
-	if(ul_report == NULL)
-	  goto error;
-	//Fill each UL report of the UE for each of the configured report types
-	for(j = 0; j < full_ul_report->n_cqi_meas; j++) {
-	  ul_report[j] = malloc(sizeof(Protocol__FlexUlCqi));
-	  if(ul_report[j] == NULL)
-	  goto error;
-	  protocol__flex_ul_cqi__init(ul_report[j]);
-	  //TODO: Set the type of the UL report. As an example set it to SRS UL report
-	  // See enum flex_ul_cqi_type in FlexRAN specification for more details
-	  ul_report[j]->type = PROTOCOL__FLEX_UL_CQI_TYPE__FLUCT_SRS;
-	  ul_report[j]->has_type = 1;
-	  //TODO:Set the number of SINR measurements based on the report type
-	  //See struct flex_ul_cqi in FlexRAN specification for more details
-	  ul_report[j]->n_sinr = 0;
-	  uint32_t *sinr_meas;
-	  sinr_meas = (uint32_t *) malloc(sizeof(uint32_t) * ul_report[j]->n_sinr);
-	  if (sinr_meas == NULL)
-	    goto error;
-	  //TODO:Set the SINR measurements for the specified type
-	  for (k = 0; k < ul_report[j]->n_sinr; k++) {
-	    sinr_meas[k] = 10;
-	  }
-	  ul_report[j]->sinr = sinr_meas;
-	  //TODO: Set the servCellIndex for this report
-	  ul_report[j]->serv_cell_index = 0;
-	  ul_report[j]->has_serv_cell_index = 1;
-	  
-	  //Set the list of UL reports of this UE to the full UL report
-	  full_ul_report->cqi_meas = ul_report;
-
-	  full_ul_report->n_pucch_dbm = MAX_NUM_CCs;
-	  full_ul_report->pucch_dbm = malloc(sizeof(Protocol__FlexPucchDbm *) * full_ul_report->n_pucch_dbm);
-
-	  for (j = 0; j < MAX_NUM_CCs; j++) {
-	    full_ul_report->pucch_dbm[j] = malloc(sizeof(Protocol__FlexPucchDbm));
-	    protocol__flex_pucch_dbm__init(full_ul_report->pucch_dbm[j]);
-	    full_ul_report->pucch_dbm[j]->has_serv_cell_index = 1;
-	    full_ul_report->pucch_dbm[j]->serv_cell_index = j;
-	    if(flexran_get_p0_pucch_dbm(enb_id,i, j) != -1){
-	      full_ul_report->pucch_dbm[j]->p0_pucch_dbm = flexran_get_p0_pucch_dbm(enb_id,i,j);
-	      full_ul_report->pucch_dbm[j]->has_p0_pucch_dbm = 1;
-	    }
-	    full_ul_report->pucch_dbm[j]->has_p0_pucch_updated = 1;
-	    full_ul_report->pucch_dbm[j]->p0_pucch_updated = flexran_get_p0_pucch_status(enb_id, i, j);
-	  }
+                                    // csi22->sb_list = flexran_get_ue_wcqi (enb_id, i);                                       
 
-	  //Add full UL CQI report to the UE report
-	  ue_report[i]->ul_cqi_report = full_ul_report;
-	}
-      }
-    }
-    /* Add list of all UE reports to the message */
-    stats_reply_msg->ue_report = ue_report;
-  }
+
+                                }
+
+                                else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A20CSI){
+
+                                    // Protocol__FlexCsiA20 *csi20;
+                                    // csi20 = malloc(sizeof(Protocol__FlexCsiA20));
+                                    // if (csi20 == NULL)
+                                    // goto error;
+                                    // protocol__flex_csi_a20__init(csi20);
+
+                                    // csi20->wb_cqi = flexran_get_ue_wcqi (enb_id, i);                                       
+                                    // csi20->has_wb_cqi = 1;
+
+                                    // csi20>sb_cqi = 1 ; //TODO inside                                      
+                                    // csi20>has_sb_cqi = 1 ;
+
+                                    // csi20->sb_list = 1; // TODO inside
+
+
+                                }
+
+                                else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A30CSI){
+
+                                }
+
+                                else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A31CSI){
+
+                                }
+
+                        }
+                     //Add the csi reports to the full DL CQI report
+                    dl_report->csi_report = csi_reports;
+                    //Add the DL CQI report to the stats report
+                     ue_report[i]->dl_cqi_report = dl_report;
+                      
+                }
+
+                /* Check flag for creation of paging buffer status report */
+                if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PBS) {
+                            //TODO: Fill in the actual paging buffer status report. For this field to be valid, the RNTI
+                            //set in the report must be a P-RNTI
+                            Protocol__FlexPagingBufferReport *paging_report;
+                            paging_report = malloc(sizeof(Protocol__FlexPagingBufferReport));
+                            if (paging_report == NULL)
+                              goto error;
+                            protocol__flex_paging_buffer_report__init(paging_report);
+                            //Set the number of pending paging messages
+                            paging_report->n_paging_info = 1;
+                            //Provide a report for each pending paging message
+                            Protocol__FlexPagingInfo **p_info;
+                            p_info = malloc(sizeof(Protocol__FlexPagingInfo *) * paging_report->n_paging_info);
+                            if (p_info == NULL)
+                              goto error;
+
+                            for (j = 0; j < paging_report->n_paging_info; j++) {
+
+                                    p_info[j] = malloc(sizeof(Protocol__FlexPagingInfo));
+                                    if(p_info[j] == NULL)
+                                      goto error;
+                                    protocol__flex_paging_info__init(p_info[j]);
+                                    //TODO: Set paging index. This index is the same that will be used for the scheduling of the
+                                    //paging message by the controller
+                                    p_info[j]->paging_index = 10;
+                                    p_info[j]->has_paging_index = 1;
+                                    //TODO:Set the paging message size
+                                    p_info[j]->paging_message_size = 100;
+                                    p_info[j]->has_paging_message_size = 1;
+                                    //TODO: Set the paging subframe
+                                    p_info[j]->paging_subframe = 10;
+                                    p_info[j]->has_paging_subframe = 1;
+                                    //TODO: Set the carrier index for the pending paging message
+                                    p_info[j]->carrier_index = 0;
+                                    p_info[j]->has_carrier_index = 1;
+
+                            }
+                            //Add all paging info to the paging buffer rerport
+                            paging_report->paging_info = p_info;
+                            //Add the paging report to the UE report
+                            ue_report[i]->pbr = paging_report;
+                }
+
+                  /* Check flag for creation of UL CQI report */
+                if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_UL_CQI) {
+
+                      //Fill in the full UL CQI report of the UE
+                      Protocol__FlexUlCqiReport *full_ul_report;
+                      full_ul_report = malloc(sizeof(Protocol__FlexUlCqiReport));
+                      if(full_ul_report == NULL)
+                        goto error;
+                      protocol__flex_ul_cqi_report__init(full_ul_report);
+                      //TODO:Set the SFN and SF of the generated report
+                      full_ul_report->sfn_sn = flexran_get_sfn_sf(enb_id);
+                      full_ul_report->has_sfn_sn = 1;
+                      //TODO:Set the number of UL measurement reports based on the types of measurements
+                      //configured for this UE and on the servCellIndex
+                      full_ul_report->n_cqi_meas = 1;
+                      Protocol__FlexUlCqi **ul_report;
+                      ul_report = malloc(sizeof(Protocol__FlexUlCqi *) * full_ul_report->n_cqi_meas);
+                      if(ul_report == NULL)
+                        goto error;
+                      //Fill each UL report of the UE for each of the configured report types
+                      for(j = 0; j < full_ul_report->n_cqi_meas; j++) {
+
+                              ul_report[j] = malloc(sizeof(Protocol__FlexUlCqi));
+                              if(ul_report[j] == NULL)
+                              goto error;
+                              protocol__flex_ul_cqi__init(ul_report[j]);
+                              //TODO: Set the type of the UL report. As an example set it to SRS UL report
+                              // See enum flex_ul_cqi_type in FlexRAN specification for more details
+                              ul_report[j]->type = PROTOCOL__FLEX_UL_CQI_TYPE__FLUCT_SRS;
+                              ul_report[j]->has_type = 1;
+                              //TODO:Set the number of SINR measurements based on the report type
+                              //See struct flex_ul_cqi in FlexRAN specification for more details
+                              ul_report[j]->n_sinr = 0;
+                              uint32_t *sinr_meas;
+                              sinr_meas = (uint32_t *) malloc(sizeof(uint32_t) * ul_report[j]->n_sinr);
+                              if (sinr_meas == NULL)
+                                goto error;
+                              //TODO:Set the SINR measurements for the specified type
+                              for (k = 0; k < ul_report[j]->n_sinr; k++) {
+                                      sinr_meas[k] = 10;
+                              }
+                              ul_report[j]->sinr = sinr_meas;
+                              //TODO: Set the servCellIndex for this report
+                              ul_report[j]->serv_cell_index = 0;
+                              ul_report[j]->has_serv_cell_index = 1;
+                              
+                              //Set the list of UL reports of this UE to the full UL report
+                              full_ul_report->cqi_meas = ul_report;
+
+                              full_ul_report->n_pucch_dbm = MAX_NUM_CCs;
+                              full_ul_report->pucch_dbm = malloc(sizeof(Protocol__FlexPucchDbm *) * full_ul_report->n_pucch_dbm);
+
+                              for (j = 0; j < MAX_NUM_CCs; j++) {
+
+                                      full_ul_report->pucch_dbm[j] = malloc(sizeof(Protocol__FlexPucchDbm));
+                                      protocol__flex_pucch_dbm__init(full_ul_report->pucch_dbm[j]);
+                                      full_ul_report->pucch_dbm[j]->has_serv_cell_index = 1;
+                                      full_ul_report->pucch_dbm[j]->serv_cell_index = j;
+
+                                      if(flexran_get_p0_pucch_dbm(enb_id,i, j) != -1){
+                                        full_ul_report->pucch_dbm[j]->p0_pucch_dbm = flexran_get_p0_pucch_dbm(enb_id,i,j);
+                                        full_ul_report->pucch_dbm[j]->has_p0_pucch_dbm = 1;
+                                      }
+                              }
+
+
+                          }
+                        //  Add full UL CQI report to the UE report
+                        ue_report[i]->ul_cqi_report = full_ul_report;
+                      
+
+                     }    
+                             
+                 
+
+                            
+             }       
+
+          
+         
+         
+     } 
 
   /* Allocate memory for list of cell reports */
   if (report_config->nr_cc > 0) {
-    cell_report = malloc(sizeof(Protocol__FlexCellStatsReport *) * report_config->nr_cc);
-    if (cell_report == NULL)
-      goto error;
-    // Fill in the Cell reports
-    for (i = 0; i < report_config->nr_cc; i++) {
-      cell_report[i] = malloc(sizeof(Protocol__FlexCellStatsReport));
-      if(cell_report[i] == NULL)
-	goto error;
-      protocol__flex_cell_stats_report__init(cell_report[i]);
-      cell_report[i]->carrier_index = report_config->cc_report_type[i].cc_id;
-      cell_report[i]->has_carrier_index = 1;
-      cell_report[i]->flags = report_config->cc_report_type[i].cc_report_flags;
-      cell_report[i]->has_flags = 1;
-
-      /* Check flag for creation of noise and interference report */
-      if(report_config->cc_report_type[i].cc_report_flags & PROTOCOL__FLEX_CELL_STATS_TYPE__FLCST_NOISE_INTERFERENCE) {
-	// TODO: Fill in the actual noise and interference report for this cell
-	Protocol__FlexNoiseInterferenceReport *ni_report;
-	ni_report = malloc(sizeof(Protocol__FlexNoiseInterferenceReport));
-	if(ni_report == NULL)
-	  goto error;
-	protocol__flex_noise_interference_report__init(ni_report);
-	// Current frame and subframe number
-	ni_report->sfn_sf = flexran_get_sfn_sf(enb_id);
-	ni_report->has_sfn_sf = 1;
-	//TODO:Received interference power in dbm
-	ni_report->rip = 0;
-	ni_report->has_rip = 0;
-	//TODO:Thermal noise power in dbm
-	ni_report->tnp = 0;
-	ni_report->has_tnp = 0;
-
-	ni_report->p0_nominal_pucch = flexran_get_p0_nominal_pucch(enb_id, 0);
-	ni_report->has_p0_nominal_pucch = 1;
-	cell_report[i]->noise_inter_report = ni_report;
-      }
-    }
-    /* Add list of all cell reports to the message */
-    stats_reply_msg->cell_report = cell_report;
+    
+            
+            // Fill in the Cell reports
+            for (i = 0; i < report_config->nr_cc; i++) {
+
+
+                      /* Check flag for creation of noise and interference report */
+                      if(report_config->cc_report_type[i].cc_report_flags & PROTOCOL__FLEX_CELL_STATS_TYPE__FLCST_NOISE_INTERFERENCE) {
+                            // TODO: Fill in the actual noise and interference report for this cell
+                            Protocol__FlexNoiseInterferenceReport *ni_report;
+                            ni_report = malloc(sizeof(Protocol__FlexNoiseInterferenceReport));
+                            if(ni_report == NULL)
+                              goto error;
+                            protocol__flex_noise_interference_report__init(ni_report);
+                            // Current frame and subframe number
+                            ni_report->sfn_sf = flexran_get_sfn_sf(enb_id);
+                            ni_report->has_sfn_sf = 1;
+                            //TODO:Received interference power in dbm
+                            ni_report->rip = 0;
+                            ni_report->has_rip = 1;
+                            //TODO:Thermal noise power in dbm
+                            ni_report->tnp = 0;
+                            ni_report->has_tnp = 1;
+
+                            ni_report->p0_nominal_pucch = flexran_get_p0_nominal_pucch(enb_id, 0);
+                            ni_report->has_p0_nominal_pucch = 1;
+                            cell_report[i]->noise_inter_report = ni_report;
+                      }
+            }
+            
+
+      
+            
   }
 
-  *msg = malloc(sizeof(Protocol__FlexranMessage));
-  if(*msg == NULL)
-    goto error;
-  protocol__flexran_message__init(*msg);
-  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REPLY_MSG;
-  (*msg)->msg_dir =  PROTOCOL__FLEXRAN_DIRECTION__SUCCESSFUL_OUTCOME;
-  (*msg)->stats_reply_msg = stats_reply_msg;
   return 0;
 
  error:
-  // TODO: Need to make proper error handling
-  if (header != NULL)
-    free(header);
-  if (stats_reply_msg != NULL)
-    free(stats_reply_msg);
-  if(*msg != NULL)
-    free(*msg);
-  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
+
+  if (cell_report != NULL)
+        free(cell_report);
+  if (ue_report != NULL)
+        free(ue_report);
+
   return -1;
 }
 
@@ -854,7 +670,7 @@ int flexran_agent_mac_destroy_stats_reply(Protocol__FlexranMessage *msg) {
 }
 
 int flexran_agent_mac_sr_info(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
-  Protocol__FlexHeader *header;
+  Protocol__FlexHeader *header = NULL;
   int i;
   const int xid = *((int *)params);
 
@@ -922,7 +738,7 @@ int flexran_agent_mac_destroy_sr_info(Protocol__FlexranMessage *msg) {
 }
 
 int flexran_agent_mac_sf_trigger(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
-  Protocol__FlexHeader *header;
+  Protocol__FlexHeader *header = NULL;
   int i, j, UE_id;
   
   int available_harq[NUMBER_OF_UE_MAX];
@@ -969,7 +785,7 @@ int flexran_agent_mac_sf_trigger(mid_t mod_id, const void *params, Protocol__Fle
 
   for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
     for (j = 0; j < 8; j++) {
-      if (harq_pid_updated[i][j] == 1) {
+      if (RC.mac && RC.mac[mod_id] && RC.mac[mod_id]->UE_list.eNB_UE_stats[UE_PCCID(mod_id,i)][i].harq_pid == 1) {
 	available_harq[i] = j;
 	sf_trigger_msg->n_dl_info++;
 	break;
@@ -1012,14 +828,15 @@ int flexran_agent_mac_sf_trigger(mid_t mod_id, const void *params, Protocol__Fle
       
       
       dl_info[i]->harq_process_id = available_harq[UE_id];
-      harq_pid_updated[UE_id][available_harq[UE_id]] = 0;
+      if (RC.mac && RC.mac[mod_id])
+        RC.mac[mod_id]->UE_list.eNB_UE_stats[UE_PCCID(mod_id,i)][UE_id].harq_pid = 0;
       dl_info[i]->has_harq_process_id = 1;
       /* Fill in the status of the HARQ process (2 TBs)*/
       dl_info[i]->n_harq_status = 2;
       dl_info[i]->harq_status = malloc(sizeof(uint32_t) * dl_info[i]->n_harq_status);
       for (j = 0; j < dl_info[i]->n_harq_status; j++) {
-	dl_info[i]->harq_status[j] = harq_pid_round[UE_id][available_harq[UE_id]];
-	// TODO: This should be different per TB
+        dl_info[i]->harq_status[j] = RC.mac[mod_id]->UE_list.UE_sched_ctrl[i].round[UE_PCCID(mod_id,i)][j];
+        // TODO: This should be different per TB
       }
       //      LOG_I(FLEXRAN_AGENT, "Sending subframe trigger for frame %d and subframe %d and harq %d (round %d)\n", flexran_get_current_frame(mod_id), (flexran_get_current_subframe(mod_id) + 1) % 10, dl_info[i]->harq_process_id, dl_info[i]->harq_status[0]);
       if(dl_info[i]->harq_status[0] > 0) {
@@ -1052,13 +869,16 @@ int flexran_agent_mac_sf_trigger(mid_t mod_id, const void *params, Protocol__Fle
       protocol__flex_ul_info__init(ul_info[i]);
       ul_info[i]->rnti = flexran_get_ue_crnti(mod_id, i);
       ul_info[i]->has_rnti = 1;
-      /*Fill in the Tx power control command for this UE (if available)*/
-      if(flexran_get_tpc(mod_id,i) != 1){
-    	  ul_info[i]->tpc = flexran_get_tpc(mod_id,i);
+      /* Fill in the Tx power control command for this UE (if available),
+       * primary carrier */
+      if(flexran_get_tpc(mod_id, i, 0) != 1){
+          /* assume primary carrier */
+          ul_info[i]->tpc = flexran_get_tpc(mod_id, i, 0);
           ul_info[i]->has_tpc = 1;
       }
       else{
-    	  ul_info[i]->tpc = flexran_get_tpc(mod_id,i);
+          /* assume primary carrier */
+          ul_info[i]->tpc = flexran_get_tpc(mod_id, i, 0);
     	  ul_info[i]->has_tpc = 0;
       }
       /*TODO: fill in the amount of data in bytes in the MAC SDU received in this subframe for the
@@ -1134,7 +954,7 @@ int flexran_agent_mac_destroy_sf_trigger(Protocol__FlexranMessage *msg) {
 int flexran_agent_mac_create_empty_dl_config(mid_t mod_id, Protocol__FlexranMessage **msg) {
 
   int xid = 0;
-  Protocol__FlexHeader *header;
+  Protocol__FlexHeader *header = NULL;
   if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_DL_MAC_CONFIG, &header) != 0)
     goto error;
 
@@ -1226,6 +1046,78 @@ int flexran_agent_mac_destroy_dl_config(Protocol__FlexranMessage *msg) {
   return -1;
 }
 
+int flexran_agent_mac_create_empty_ul_config(mid_t mod_id, Protocol__FlexranMessage **msg) {
+
+  int xid = 0;
+  Protocol__FlexHeader *header = NULL;
+  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_UL_MAC_CONFIG, &header) != 0)
+    goto error;
+
+  Protocol__FlexUlMacConfig *ul_mac_config_msg;
+  ul_mac_config_msg = malloc(sizeof(Protocol__FlexUlMacConfig));
+  if (ul_mac_config_msg == NULL) {
+    goto error;
+  }
+  protocol__flex_ul_mac_config__init(ul_mac_config_msg);
+
+  ul_mac_config_msg->header = header;
+  ul_mac_config_msg->has_sfn_sf = 1;
+  ul_mac_config_msg->sfn_sf = flexran_get_sfn_sf(mod_id);
+
+  *msg = malloc(sizeof(Protocol__FlexranMessage));
+  if(*msg == NULL)
+    goto error;
+  protocol__flexran_message__init(*msg);
+  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_UL_MAC_CONFIG_MSG;
+  (*msg)->msg_dir =  PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
+  (*msg)->ul_mac_config_msg = ul_mac_config_msg;
+
+  return 0;
+
+ error:
+  return -1;
+}
+
+
+int flexran_agent_mac_destroy_ul_config(Protocol__FlexranMessage *msg) {
+  int i; //,j, k;
+  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_UL_MAC_CONFIG_MSG)
+    goto error;
+
+  // Protocol__FlexUlDci *ul_dci;
+
+  free(msg->ul_mac_config_msg->header);
+  for (i = 0; i < msg->ul_mac_config_msg->n_ul_ue_data; i++) {
+    // TODO  uplink rlc ...
+    // free(msg->ul_mac_config_msg->dl_ue_data[i]->ce_bitmap); 
+  //   for (j = 0; j < msg->ul_mac_config_msg->ul_ue_data[i]->n_rlc_pdu; j++) {
+  //     for (k = 0; k <  msg->ul_mac_config_msg->ul_ue_data[i]->rlc_pdu[j]->n_rlc_pdu_tb; k++) {
+  // free(msg->ul_mac_config_msg->dl_ue_data[i]->rlc_pdu[j]->rlc_pdu_tb[k]);
+  //     }
+  //     free(msg->ul_mac_config_msg->ul_ue_data[i]->rlc_pdu[j]->rlc_pdu_tb);
+  //     free(msg->ul_mac_config_msg->ul_ue_data[i]->rlc_pdu[j]);
+  //   }
+    // free(msg->ul_mac_config_msg->ul_ue_data[i]->rlc_pdu);
+    // ul_dci = msg->ul_mac_config_msg->ul_ue_data[i]->ul_dci;
+    // free(dl_dci->tbs_size);
+    // free(ul_dci->mcs);
+    // free(ul_dci->ndi);
+    // free(ul_dci->rv);
+    // free(ul_dci);
+    // free(msg->ul_mac_config_msg->ul_ue_data[i]);
+  }
+  free(msg->ul_mac_config_msg->ul_ue_data);
+  
+  free(msg->ul_mac_config_msg);
+  free(msg);
+
+  return 0;
+
+ error:
+  return -1;
+}
+
+
 void flexran_agent_get_pending_dl_mac_config(mid_t mod_id, Protocol__FlexranMessage **msg) {
 
   struct lfds700_misc_prng_state ls;
@@ -1277,7 +1169,8 @@ void flexran_agent_init_mac_agent(mid_t mod_id) {
   lfds700_ringbuffer_init_valid_on_current_logical_core( &ringbuffer_state[mod_id], dl_mac_config_array[mod_id], num_elements, &ps[mod_id], NULL );
   for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
     for (j = 0; j < 8; j++) {
-      harq_pid_updated[i][j] = 0;
+      if (RC.mac && RC.mac[mod_id])
+        RC.mac[mod_id]->UE_list.eNB_UE_stats[UE_PCCID(mod_id,i)][i].harq_pid = 0;
     }
   }
 }
@@ -1346,58 +1239,7 @@ void flexran_agent_send_sf_trigger(mid_t mod_id) {
   LOG_D(FLEXRAN_AGENT, "Could not send sf trigger message\n");
 }
 
-void flexran_agent_send_update_mac_stats(mid_t mod_id) {
 
-  Protocol__FlexranMessage *current_report = NULL;
-  void *data;
-  int size;
-  err_code_t err_code;
-  int priority = 0;
-  
-  if (pthread_mutex_lock(mac_stats_context[mod_id].mutex)) {
-    goto error;
-  }
-
-  if (mac_stats_context[mod_id].cont_update == 1) {
-  
-    /*Create a fresh report with the required flags*/
-    err_code = flexran_agent_mac_handle_stats(mod_id, (void *) mac_stats_context[mod_id].stats_req, &current_report);
-    if (err_code < 0) {
-      goto error;
-    }
-  }
-  /* /\*TODO:Check if a previous reports exists and if yes, generate a report */
-  /*  *that is the diff between the old and the new report, */
-  /*  *respecting the thresholds. Otherwise send the new report*\/ */
-  /* if (mac_stats_context[mod_id].prev_stats_reply != NULL) { */
-
-  /*   msg = flexran_agent_generate_diff_mac_stats_report(current_report, mac_stats_context[mod_id].prev_stats_reply); */
-
-  /*   /\*Destroy the old stats*\/ */
-  /*    flexran_agent_destroy_flexran_message(mac_stats_context[mod_id].prev_stats_reply); */
-  /* } */
-  /* /\*Use the current report for future comparissons*\/ */
-  /* mac_stats_context[mod_id].prev_stats_reply = current_report; */
-
-
-  if (pthread_mutex_unlock(mac_stats_context[mod_id].mutex)) {
-    goto error;
-  }
-
-  if (current_report != NULL){
-    data=flexran_agent_pack_message(current_report, &size);
-    /*Send any stats updates using the MAC channel of the eNB*/
-    if (flexran_agent_msg_send(mod_id, FLEXRAN_AGENT_MAC, data, size, priority)) {
-      err_code = PROTOCOL__FLEXRAN_ERR__MSG_ENQUEUING;
-      goto error;
-    }
-
-    LOG_D(FLEXRAN_AGENT,"sent message with size %d\n", size);
-    return;
-  }
- error:
-  LOG_D(FLEXRAN_AGENT, "Could not send sf trigger message\n");
-}
 
 int flexran_agent_register_mac_xface(mid_t mod_id, AGENT_MAC_xface *xface) {
   if (mac_agent_registered[mod_id]) {
@@ -1408,14 +1250,11 @@ int flexran_agent_register_mac_xface(mid_t mod_id, AGENT_MAC_xface *xface) {
   //xface->agent_ctxt = &shared_ctxt[mod_id];
   xface->flexran_agent_send_sr_info = flexran_agent_send_sr_info;
   xface->flexran_agent_send_sf_trigger = flexran_agent_send_sf_trigger;
-  xface->flexran_agent_send_update_mac_stats = flexran_agent_send_update_mac_stats;
-  xface->flexran_agent_schedule_ue_spec = flexran_schedule_ue_spec_default;
-  //xface->flexran_agent_schedule_ue_spec = flexran_schedule_ue_spec_remote;
+  //xface->flexran_agent_send_update_mac_stats = flexran_agent_send_update_mac_stats;
   xface->flexran_agent_get_pending_dl_mac_config = flexran_agent_get_pending_dl_mac_config;
-  xface->flexran_agent_notify_ue_state_change = flexran_agent_ue_state_change;
   
   xface->dl_scheduler_loaded_lib = NULL;
-
+  xface->ul_scheduler_loaded_lib = NULL;
   mac_agent_registered[mod_id] = 1;
   agent_mac_xface[mod_id] = xface;
 
@@ -1427,13 +1266,11 @@ int flexran_agent_unregister_mac_xface(mid_t mod_id, AGENT_MAC_xface *xface) {
   //xface->agent_ctxt = NULL;
   xface->flexran_agent_send_sr_info = NULL;
   xface->flexran_agent_send_sf_trigger = NULL;
-  xface->flexran_agent_send_update_mac_stats = NULL;
-  xface->flexran_agent_schedule_ue_spec = NULL;
+  //xface->flexran_agent_send_update_mac_stats = NULL;
   xface->flexran_agent_get_pending_dl_mac_config = NULL;
-  xface->flexran_agent_notify_ue_state_change = NULL;
 
   xface->dl_scheduler_loaded_lib = NULL;
-
+  xface->ul_scheduler_loaded_lib = NULL;
   mac_agent_registered[mod_id] = 0;
   agent_mac_xface[mod_id] = NULL;
 
@@ -1441,89 +1278,7 @@ int flexran_agent_unregister_mac_xface(mid_t mod_id, AGENT_MAC_xface *xface) {
 }
 
 
-/******************************************************
- *Implementations of flexran_agent_mac_internal.h functions
- ******************************************************/
 
-err_code_t flexran_agent_init_cont_mac_stats_update(mid_t mod_id) {
-
-  /*Initialize the Mac stats update structure*/
-  /*Initially the continuous update is set to false*/
-  mac_stats_context[mod_id].cont_update = 0;
-  mac_stats_context[mod_id].is_initialized = 1;
-  mac_stats_context[mod_id].stats_req = NULL;
-  mac_stats_context[mod_id].prev_stats_reply = NULL;
-  mac_stats_context[mod_id].mutex = calloc(1, sizeof(pthread_mutex_t));
-  if (mac_stats_context[mod_id].mutex == NULL)
-    goto error;
-  if (pthread_mutex_init(mac_stats_context[mod_id].mutex, NULL))
-    goto error;;
 
-  return 0;
-
- error:
-  return -1;
-}
-
-err_code_t flexran_agent_destroy_cont_mac_stats_update(mid_t mod_id) {
-  /*Disable the continuous updates for the MAC*/
-  mac_stats_context[mod_id].cont_update = 0;
-  mac_stats_context[mod_id].is_initialized = 0;
-  flexran_agent_destroy_flexran_message(mac_stats_context[mod_id].stats_req);
-  flexran_agent_destroy_flexran_message(mac_stats_context[mod_id].prev_stats_reply);
-  free(mac_stats_context[mod_id].mutex);
-
-  mac_agent_registered[mod_id] = 0;
-  return 1;
-}
 
 
-err_code_t flexran_agent_enable_cont_mac_stats_update(mid_t mod_id,
-						      xid_t xid, stats_request_config_t *stats_req) {
-  /*Enable the continuous updates for the MAC*/
-  if (pthread_mutex_lock(mac_stats_context[mod_id].mutex)) {
-    goto error;
-  }
-
-  Protocol__FlexranMessage *req_msg;
-
-  flexran_agent_mac_stats_request(mod_id, xid, stats_req, &req_msg);
-  mac_stats_context[mod_id].stats_req = req_msg;
-  mac_stats_context[mod_id].prev_stats_reply = NULL;
-
-  mac_stats_context[mod_id].cont_update = 1;
-  mac_stats_context[mod_id].xid = xid;
-
-  if (pthread_mutex_unlock(mac_stats_context[mod_id].mutex)) {
-    goto error;
-  }
-  return 0;
-
- error:
-  LOG_E(FLEXRAN_AGENT, "mac_stats_context for eNB %d is not initialized\n", mod_id);
-  return -1;
-}
-
-err_code_t flexran_agent_disable_cont_mac_stats_update(mid_t mod_id) {
-  /*Disable the continuous updates for the MAC*/
-  if (pthread_mutex_lock(mac_stats_context[mod_id].mutex)) {
-    goto error;
-  }
-  mac_stats_context[mod_id].cont_update = 0;
-  mac_stats_context[mod_id].xid = 0;
-  if (mac_stats_context[mod_id].stats_req != NULL) {
-    flexran_agent_destroy_flexran_message(mac_stats_context[mod_id].stats_req);
-  }
-  if (mac_stats_context[mod_id].prev_stats_reply != NULL) {
-    flexran_agent_destroy_flexran_message(mac_stats_context[mod_id].prev_stats_reply);
-  }
-  if (pthread_mutex_unlock(mac_stats_context[mod_id].mutex)) {
-    goto error;
-  }
-  return 0;
-
- error:
-  LOG_E(FLEXRAN_AGENT, "mac_stats_context for eNB %d is not initialized\n", mod_id);
-  return -1;
-
-}
diff --git a/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac.h b/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac.h
index 090a599abef7bbdf81176013ddb4292b6fbcd1ca..03e7def95e04610e57503917c38dbafe9e6fb3d4 100644
--- a/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac.h
+++ b/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac.h
@@ -37,50 +37,10 @@
 #include "flexran_agent_common.h"
 #include "flexran_agent_extern.h"
 
-/* These types will be used to give
-   instructions for the type of stats reports
-   we need to create */
-typedef struct {
-  uint16_t ue_rnti;
-  uint32_t ue_report_flags; /* Indicates the report elements
-			       required for this UE id. See
-			       FlexRAN specification 1.2.4.2 */
-} ue_report_type_t;
-
-typedef struct {
-  uint16_t cc_id;
-  uint32_t cc_report_flags; /* Indicates the report elements
-			      required for this CC index. See
-			      FlexRAN specification 1.2.4.3 */
-} cc_report_type_t;
-
-typedef struct {
-  int nr_ue;
-  ue_report_type_t *ue_report_type;
-  int nr_cc;
-  cc_report_type_t *cc_report_type;
-} report_config_t;
-
-typedef struct stats_request_config_s{
-  uint8_t report_type;
-  uint8_t report_frequency;
-  uint16_t period; /*In number of subframes*/
-  report_config_t *config;
-} stats_request_config_t;
 
 /* Initialization function for the agent structures etc */
 void flexran_agent_init_mac_agent(mid_t mod_id);
 
-int flexran_agent_mac_handle_stats(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg);
-
-/* Statistics request protocol message constructor and destructor */
-int flexran_agent_mac_stats_request(mid_t mod_id, xid_t xid, const stats_request_config_t *report_config, Protocol__FlexranMessage **msg);
-int flexran_agent_mac_destroy_stats_request(Protocol__FlexranMessage *msg);
-
-/* Statistics reply protocol message constructor and destructor */
-int flexran_agent_mac_stats_reply(mid_t mod_id, xid_t xid, const report_config_t *report_config, Protocol__FlexranMessage **msg);
-int flexran_agent_mac_destroy_stats_reply(Protocol__FlexranMessage *msg);
-
 /* Scheduling request information protocol message constructor and estructor */
 int flexran_agent_mac_sr_info(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg);
 int flexran_agent_mac_destroy_sr_info(Protocol__FlexranMessage *msg);
@@ -89,10 +49,18 @@ int flexran_agent_mac_destroy_sr_info(Protocol__FlexranMessage *msg);
 int flexran_agent_mac_sf_trigger(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg);
 int flexran_agent_mac_destroy_sf_trigger(Protocol__FlexranMessage *msg);
 
+/* Statistics reply protocol message constructor and destructor */
+int flexran_agent_mac_stats_reply(mid_t mod_id, const report_config_t *report_config, Protocol__FlexUeStatsReport **ue_report, Protocol__FlexCellStatsReport **cell_report);
+int flexran_agent_mac_destroy_stats_reply(Protocol__FlexranMessage *msg);
+
 /* DL MAC scheduling decision protocol message constructor (empty command) and destructor */ 
 int flexran_agent_mac_create_empty_dl_config(mid_t mod_id, Protocol__FlexranMessage **msg);
 int flexran_agent_mac_destroy_dl_config(Protocol__FlexranMessage *msg);
 
+/* UL MAC scheduling decision protocol message constructor (empty command) and destructor */ 
+int flexran_agent_mac_create_empty_ul_config(mid_t mod_id, Protocol__FlexranMessage **msg);
+int flexran_agent_mac_destroy_ul_config(Protocol__FlexranMessage *msg);
+
 int flexran_agent_mac_handle_dl_mac_config(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg);
 
 
diff --git a/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac_defs.h b/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac_defs.h
index 70254bd7ba4ed28154820443b8f31954ecae4447..5c1fc1379c6454406b22f2e7fde198433819e965 100644
--- a/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac_defs.h
+++ b/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac_defs.h
@@ -48,26 +48,20 @@ typedef struct {
 
   /// Send to the controller all the mac stat updates that occured during this subframe
   /// based on the stats request configuration
-  void (*flexran_agent_send_update_mac_stats)(mid_t mod_id);
+  // void (*flexran_agent_send_update_mac_stats)(mid_t mod_id);
 
   /// Provide to the scheduler a pending dl_mac_config message
   void (*flexran_agent_get_pending_dl_mac_config)(mid_t mod_id,
 						  Protocol__FlexranMessage **msg);
   
-  /// Run the UE DL scheduler and fill the Protocol__FlexranMessage. Assumes that
-  /// dl_info is already initialized as flex_dl_mac_config and fills the
-  /// flex_dl_data part of it
-  void (*flexran_agent_schedule_ue_spec)(mid_t mod_id, uint32_t frame, uint32_t subframe,
-					 int *mbsfn_flag, Protocol__FlexranMessage **dl_info);
-  
-
   /// Notify the controller for a state change of a particular UE, by sending the proper
   /// UE state change message (ACTIVATION, DEACTIVATION, HANDOVER)
-  int (*flexran_agent_notify_ue_state_change)(mid_t mod_id, uint32_t rnti,
-					       uint8_t state_change);
+  // int (*flexran_agent_notify_ue_state_change)(mid_t mod_id, uint32_t rnti,
+		// 			       uint8_t state_change);
   
   
   void *dl_scheduler_loaded_lib;
+  void *ul_scheduler_loaded_lib;
   /*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/CONTROL_MODULES/MAC/flexran_agent_mac_internal.c b/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac_internal.c
index 5c37ba81321076b1e6ec26d070b31285dfa7fd99..2acee0686f62165881d256e96eb09500274d0a30 100644
--- a/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac_internal.c
+++ b/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac_internal.c
@@ -101,7 +101,7 @@ Protocol__FlexranMessage * flexran_agent_generate_diff_mac_stats_report(Protocol
   if (n_cell_report > 0 || n_ue_report > 0) {
     /*Create header*/
     int xid = old_report->header->xid;
-    Protocol__FlexHeader *header;
+    Protocol__FlexHeader *header = NULL;
     if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_STATS_REPLY, &header) != 0) {
     goto error;
     }
@@ -180,10 +180,12 @@ Protocol__FlexUeStatsReport * copy_ue_stats_report(Protocol__FlexUeStatsReport *
     }
   }
 
-  if (copy->flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PRH) {
-    copy->has_phr = original->has_phr;
-    copy->phr = original->phr;
-  }
+  
+
+   if (copy->flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PHR) { 
+     copy->has_phr = original->has_phr;
+     copy->phr = original->phr;
+   }
 
   if (copy->flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_RLC_BS) {
     copy->n_rlc_report = original->n_rlc_report; 
@@ -605,7 +607,10 @@ int parse_mac_config(mid_t mod_id, yaml_parser_t *parser) {
       } else if (strcmp((char *) event.data.scalar.value, "ul_scheduler") == 0) {
 	// Call the proper handler
 	LOG_D(ENB_APP, "This is for the ul_scheduler subsystem\n");
-	goto error;
+	if (parse_ul_scheduler_config(mod_id, parser) == -1) {
+    LOG_D(ENB_APP, "An error occured\n");
+    goto error;
+  }
 	// TODO
       } else if (strcmp((char *) event.data.scalar.value, "ra_scheduler") == 0) {
 	// Call the proper handler
@@ -698,6 +703,56 @@ int parse_dl_scheduler_config(mid_t mod_id, yaml_parser_t *parser) {
   return -1;
 }
 
+int parse_ul_scheduler_config(mid_t mod_id, yaml_parser_t *parser) {
+  
+  yaml_event_t event;
+
+  int done = 0;
+  int mapping_started = 0;
+
+  while (!done) {
+    
+    if (!yaml_parser_parse(parser, &event))
+      goto error;
+
+    switch (event.type) {
+      // We are expecting a mapping (behavior and parameters)
+    case YAML_MAPPING_START_EVENT:
+      LOG_D(ENB_APP, "The mapping of the subsystem started\n");
+      mapping_started = 1;
+      break;
+    case YAML_MAPPING_END_EVENT:
+      LOG_D(ENB_APP, "The mapping of the subsystem ended\n");
+      mapping_started = 0;
+      break;
+    case YAML_SCALAR_EVENT:
+      if (!mapping_started) {
+  goto error;
+      }
+      // Check what key needs to be set
+      if (strcmp((char *) event.data.scalar.value, "parameters") == 0) {
+  LOG_D(ENB_APP, "Now it is time to set the parameters for this subsystem\n");
+  if (parse_ul_scheduler_parameters(mod_id, parser) == -1) {
+    goto error;
+  }
+      }
+      break;
+    default:
+      goto error;
+    }
+
+    done = (event.type == YAML_MAPPING_END_EVENT);
+    yaml_event_delete(&event);
+  }
+
+  return 0;
+
+ error:
+  yaml_event_delete(&event);
+  return -1;
+}
+
+
 int parse_dl_scheduler_parameters(mid_t mod_id, yaml_parser_t *parser) {
   yaml_event_t event;
   
@@ -753,13 +808,68 @@ int parse_dl_scheduler_parameters(mid_t mod_id, yaml_parser_t *parser) {
   return -1;
 }
 
+int parse_ul_scheduler_parameters(mid_t mod_id, yaml_parser_t *parser) {
+  yaml_event_t event;
+  
+  void *param;
+  
+  int done = 0;
+  int mapping_started = 0;
+
+  while (!done) {
+    
+    if (!yaml_parser_parse(parser, &event))
+      goto error;
+
+    switch (event.type) {
+      // We are expecting a mapping of parameters
+    case YAML_MAPPING_START_EVENT:
+      LOG_D(ENB_APP, "The mapping of the parameters started\n");
+      mapping_started = 1;
+      break;
+    case YAML_MAPPING_END_EVENT:
+      LOG_D(ENB_APP, "The mapping of the parameters ended\n");
+      mapping_started = 0;
+      break;
+    case YAML_SCALAR_EVENT:
+      if (!mapping_started) {
+  goto error;
+      }
+      // Check what key needs to be set
+      if (mac_agent_registered[mod_id]) {
+  LOG_D(ENB_APP, "Setting parameter %s\n", event.data.scalar.value);
+  param = dlsym(agent_mac_xface[mod_id]->ul_scheduler_loaded_lib,
+          (char *) event.data.scalar.value);
+  if (param == NULL) {
+    goto error;
+  }
+  apply_parameter_modification(param, parser);
+      } else {
+  goto error;
+      }
+      break;
+    default:
+      goto error;
+    }
+
+    done = (event.type == YAML_MAPPING_END_EVENT);
+    yaml_event_delete(&event);
+  }
+
+  return 0;
+  
+ error:
+  yaml_event_delete(&event);
+  return -1;
+}
+
 int load_dl_scheduler_function(mid_t mod_id, const char *function_name) {
   void *lib;
 
   char lib_name[120];
   char target[512];
   snprintf(lib_name, sizeof(lib_name), "/%s.so", function_name);
-  strcpy(target, local_cache);
+  strcpy(target, RC.flexran[mod_id]->cache_name);
   strcat(target, lib_name);
   
   LOG_I(FLEXRAN_AGENT, "Opening pushed code: %s\n", target);
@@ -773,7 +883,6 @@ int load_dl_scheduler_function(mid_t mod_id, const char *function_name) {
   void *loaded_scheduler = dlsym(lib, function_name);
   if (loaded_scheduler) {
     if (mac_agent_registered[mod_id]) {
-      agent_mac_xface[mod_id]->flexran_agent_schedule_ue_spec = loaded_scheduler;
       if (agent_mac_xface[mod_id]->dl_scheduler_loaded_lib != NULL) {
 	dlclose(agent_mac_xface[mod_id]->dl_scheduler_loaded_lib);
       }
diff --git a/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac_internal.h b/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac_internal.h
index 263b85eb87fef9dfd70a13ba15d702ea14c06e5b..f69e2cde4098ad5d035a522c9815632bffb37312 100644
--- a/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac_internal.h
+++ b/openair2/ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac_internal.h
@@ -101,6 +101,10 @@ int parse_dl_scheduler_config(mid_t mod_id, yaml_parser_t *parser);
 
 int parse_dl_scheduler_parameters(mid_t mod_id, yaml_parser_t *parser);
 
+int parse_ul_scheduler_config(mid_t mod_id, yaml_parser_t *parser);
+
+int parse_ul_scheduler_parameters(mid_t mod_id, yaml_parser_t *parser);
+
 int load_dl_scheduler_function(mid_t mod_id, const char *function_name);
 
 #endif /*FLEXRAN_AGENT_MAC_INTERNAL_H_*/
diff --git a/openair2/ENB_APP/CONTROL_MODULES/PDCP/flexran_agent_pdcp.c b/openair2/ENB_APP/CONTROL_MODULES/PDCP/flexran_agent_pdcp.c
new file mode 100644
index 0000000000000000000000000000000000000000..25ec91912742b7a3fd38f026edd2da40395fce63
--- /dev/null
+++ b/openair2/ENB_APP/CONTROL_MODULES/PDCP/flexran_agent_pdcp.c
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */ 
+
+/*! \file flexran_agent_pdcp.c
+ * \brief FlexRAN agent Control Module PDCP 
+ * \author Navid Nikaein and shahab SHARIAT BAGHERI
+ * \date 2017
+ * \version 0.1
+ */
+
+#include "flexran_agent_pdcp.h"
+
+
+/*Trigger boolean for PDCP measurement*/
+bool triggered_pdcp = false;
+/*Flags showing if a pdcp agent has already been registered*/
+unsigned int pdcp_agent_registered[NUM_MAX_ENB];
+
+/*Array containing the Agent-PDCP interfaces*/
+AGENT_PDCP_xface *agent_pdcp_xface[NUM_MAX_ENB];
+
+// NUMBER_OF_UE_MAX
+
+void flexran_agent_pdcp_aggregate_stats(const mid_t mod_id,
+					const mid_t ue_id,
+					Protocol__FlexPdcpStats *pdcp_aggr_stats){
+
+  int lcid=0;
+  /* only calculate the DRBs */ 
+  //LOG_I(FLEXRAN_AGENT, "enb %d ue %d \n", mod_id, ue_id);
+  
+  for (lcid=NUM_MAX_SRB ; lcid < NUM_MAX_SRB + NUM_MAX_DRB; lcid++){
+    
+    pdcp_aggr_stats->pkt_tx += flexran_get_pdcp_tx(mod_id,ue_id,lcid);
+    pdcp_aggr_stats->pkt_tx_bytes += flexran_get_pdcp_tx_bytes(mod_id,ue_id,lcid);
+    pdcp_aggr_stats->pkt_tx_w += flexran_get_pdcp_tx_w(mod_id,ue_id,lcid);
+    pdcp_aggr_stats->pkt_tx_bytes_w += flexran_get_pdcp_tx_bytes_w(mod_id,ue_id,lcid);
+    pdcp_aggr_stats->pkt_tx_aiat += flexran_get_pdcp_tx_aiat(mod_id,ue_id,lcid);
+    pdcp_aggr_stats->pkt_tx_aiat_w += flexran_get_pdcp_tx_aiat_w(mod_id,ue_id,lcid);
+    
+      
+    pdcp_aggr_stats->pkt_rx += flexran_get_pdcp_rx(mod_id,ue_id,lcid);
+    pdcp_aggr_stats->pkt_rx_bytes += flexran_get_pdcp_rx_bytes(mod_id,ue_id,lcid);
+    pdcp_aggr_stats->pkt_rx_w += flexran_get_pdcp_rx_w(mod_id,ue_id,lcid);
+    pdcp_aggr_stats->pkt_rx_bytes_w += flexran_get_pdcp_rx_bytes_w(mod_id,ue_id,lcid);
+    pdcp_aggr_stats->pkt_rx_aiat += flexran_get_pdcp_rx_aiat(mod_id,ue_id,lcid);
+    pdcp_aggr_stats->pkt_rx_aiat_w += flexran_get_pdcp_rx_aiat_w(mod_id,ue_id,lcid);
+    pdcp_aggr_stats->pkt_rx_oo += flexran_get_pdcp_rx_oo(mod_id,ue_id,lcid);
+    
+  }
+  
+}
+
+
+int flexran_agent_pdcp_stats_reply(mid_t mod_id,       
+				   const report_config_t *report_config,
+				   Protocol__FlexUeStatsReport **ue_report,
+				   Protocol__FlexCellStatsReport **cell_report) {
+  
+  
+  // Protocol__FlexHeader *header;
+  int i;
+  // int cc_id = 0;
+ 
+  
+  /* Allocate memory for list of UE reports */
+  if (report_config->nr_ue > 0) {
+    
+    for (i = 0; i < report_config->nr_ue; i++) {
+
+      /* Check flag for creation of buffer status report */
+      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PDCP_STATS) {
+	
+	Protocol__FlexPdcpStats *pdcp_aggr_stats;
+	pdcp_aggr_stats = malloc(sizeof(Protocol__FlexPdcpStats));
+	if (pdcp_aggr_stats == NULL)
+	  goto error;
+	protocol__flex_pdcp_stats__init(pdcp_aggr_stats);
+	
+	flexran_agent_pdcp_aggregate_stats(mod_id, i, pdcp_aggr_stats);
+
+	pdcp_aggr_stats->has_pkt_tx=1;
+	pdcp_aggr_stats->has_pkt_tx_bytes =1;
+	pdcp_aggr_stats->has_pkt_tx_w=1; 
+	pdcp_aggr_stats->has_pkt_tx_bytes_w =1;
+	pdcp_aggr_stats->has_pkt_tx_aiat =1; 
+	pdcp_aggr_stats->has_pkt_tx_aiat_w =1;
+
+	pdcp_aggr_stats->pkt_tx_sn = flexran_get_pdcp_tx_sn(mod_id, i, DEFAULT_DRB);
+	pdcp_aggr_stats->has_pkt_tx_sn =1;
+	
+	pdcp_aggr_stats->has_pkt_rx =1;
+	pdcp_aggr_stats->has_pkt_rx_bytes =1; 
+	pdcp_aggr_stats->has_pkt_rx_w =1;
+	pdcp_aggr_stats->has_pkt_rx_bytes_w =1;
+	pdcp_aggr_stats->has_pkt_rx_aiat =1;
+	pdcp_aggr_stats->has_pkt_rx_aiat_w =1;
+	pdcp_aggr_stats->has_pkt_rx_oo =1;
+
+	pdcp_aggr_stats->pkt_rx_sn = flexran_get_pdcp_rx_sn(mod_id, i, DEFAULT_DRB);
+	pdcp_aggr_stats->has_pkt_rx_sn =1;
+
+	pdcp_aggr_stats->sfn = flexran_get_pdcp_sfn(mod_id);
+	pdcp_aggr_stats->has_sfn =1;
+
+	ue_report[i]->pdcp_stats = pdcp_aggr_stats;
+
+      }
+    }
+  }  else {
+    LOG_D(FLEXRAN_AGENT, "no UE\n");
+  }     
+  
+  return 0;
+  
+ error:
+  LOG_W(FLEXRAN_AGENT, "Can't allocate PDCP stats\n");
+  
+  /* if (cell_report != NULL)
+        free(cell_report);
+  if (ue_report != NULL)
+        free(ue_report);
+  */
+  return -1;
+}
+
+
+
+int flexran_agent_register_pdcp_xface(mid_t mod_id, AGENT_PDCP_xface *xface) {
+  if (pdcp_agent_registered[mod_id]) {
+    LOG_E(PDCP, "PDCP agent for eNB %d is already registered\n", mod_id);
+    return -1;
+  }
+
+  //xface->flexran_pdcp_stats_measurement = NULL;
+
+  pdcp_agent_registered[mod_id] = 1;
+  agent_pdcp_xface[mod_id] = xface;
+
+  return 0;
+}
+
+int flexran_agent_unregister_pdcp_xface(mid_t mod_id, AGENT_PDCP_xface *xface) {
+
+  //xface->agent_ctxt = NULL;
+  //xface->flexran_pdcp_stats_measurement = NULL;
+
+  
+  pdcp_agent_registered[mod_id] = 0;
+  agent_pdcp_xface[mod_id] = NULL;
+
+  return 0;
+}
diff --git a/openair2/ENB_APP/CONTROL_MODULES/PDCP/flexran_agent_pdcp.h b/openair2/ENB_APP/CONTROL_MODULES/PDCP/flexran_agent_pdcp.h
new file mode 100644
index 0000000000000000000000000000000000000000..a962390c00eaea5560f8717293ee76e40d49c76e
--- /dev/null
+++ b/openair2/ENB_APP/CONTROL_MODULES/PDCP/flexran_agent_pdcp.h
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */ 
+
+/*! \file flexran_agent_pdcp.h
+ * \brief FlexRAN agent Control Module PDCP header
+ * \author shahab SHARIAT BAGHERI 
+ * \date 2017
+ * \version 0.1
+ */
+
+#ifndef FLEXRAN_AGENT_PDCP_H_
+#define FLEXRAN_AGENT_PDCP_H_
+
+#include "header.pb-c.h"
+#include "flexran.pb-c.h"
+#include "stats_messages.pb-c.h"
+#include "stats_common.pb-c.h"
+
+
+#include "flexran_agent_common.h"
+#include "flexran_agent_defs.h"
+#include "flexran_agent_pdcp_defs.h"
+#include "flexran_agent_ran_api.h"
+
+/**********************************
+ * FlexRAN agent - technology PDCP API
+ **********************************/
+
+/* Send to the controller all the pdcp stat updates that occured during this subframe*/
+int flexran_agent_pdcp_stats_reply(mid_t mod_id,       
+          const report_config_t *report_config,
+           Protocol__FlexUeStatsReport **ue_report,
+           Protocol__FlexCellStatsReport **cell_report);
+
+/* Get the stats from RAN API and aggregate them per USER*/
+void flexran_agent_pdcp_aggregate_stats(const mid_t mod_id,
+					const mid_t ue_id,
+					Protocol__FlexPdcpStats *pdcp_aggr_stats);
+
+/*Register technology specific interface callbacks*/
+int flexran_agent_register_pdcp_xface(mid_t mod_id, AGENT_PDCP_xface *xface);
+
+/*Unregister technology specific callbacks*/
+int flexran_agent_unregister_pdcp_xface(mid_t mod_id, AGENT_PDCP_xface*xface);
+
+#endif
diff --git a/openair2/ENB_APP/CONTROL_MODULES/PDCP/flexran_agent_pdcp_defs.h b/openair2/ENB_APP/CONTROL_MODULES/PDCP/flexran_agent_pdcp_defs.h
new file mode 100644
index 0000000000000000000000000000000000000000..46bdf744de1a03008551f2e2193c339dc25d3ab1
--- /dev/null
+++ b/openair2/ENB_APP/CONTROL_MODULES/PDCP/flexran_agent_pdcp_defs.h
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */ 
+#ifndef __FLEXRAN_AGENT_PDCP_PRIMITIVES_H__
+#define __FLEXRAN_AGENT_PDCP_PRIMITIVES_H__
+
+#include "flexran_agent_defs.h"
+#include "flexran.pb-c.h"
+#include "header.pb-c.h"
+
+ /*PDCP aggregated Packet stats  */
+/*
+typedef struct  pdcp_aggr_stats_s {
+  int32_t rnti; 
+
+  int32_t pkt_tx;
+  int32_t pkt_tx_bytes;
+  int32_t pkt_tx_sn;
+  int32_t pkt_tx_rate_s;
+  int32_t pkt_tx_throughput_s;
+  int32_t pkt_tx_aiat;
+  int32_t pkt_tx_aiat_s;
+
+  int32_t pkt_rx;
+  int32_t pkt_rx_bytes;
+  int32_t pkt_rx_sn;
+  int32_t pkt_rx_rate_s;
+  int32_t pkt_rx_goodput_s;
+  int32_t pkt_rx_aiat;
+  int32_t pkt_rx_aiat_s;
+  int32_t pkt_rx_oo;
+
+  
+} pdcp_aggr_stats_t;
+*/
+
+/* FLEXRAN AGENT-PDCP Interface */
+typedef struct {
+  
+  
+  // PDCP statistics
+  void (*flexran_pdcp_stats_measurement)(mid_t mod_id, uint16_t rnti, uint16_t seq_num,  uint32_t size);
+  
+} AGENT_PDCP_xface;
+
+#endif
diff --git a/openair2/ENB_APP/CONTROL_MODULES/RRC/flexran_agent_rrc.c b/openair2/ENB_APP/CONTROL_MODULES/RRC/flexran_agent_rrc.c
new file mode 100644
index 0000000000000000000000000000000000000000..e88c4eaccfaa773eb98fcc7f5a3e2bb466f15d46
--- /dev/null
+++ b/openair2/ENB_APP/CONTROL_MODULES/RRC/flexran_agent_rrc.c
@@ -0,0 +1,673 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */ 
+
+/*! \file flexran_agent_mac.c
+ * \brief FlexRAN agent Control Module RRC 
+ * \author shahab SHARIAT BAGHERI
+ * \date 2017
+ * \version 0.1
+ */
+
+#include "flexran_agent_rrc.h"
+
+
+#include "liblfds700.h"
+
+#include "log.h"
+
+/*Trigger boolean for RRC measurement*/
+bool triggered_rrc = false;
+
+/*Flags showing if an rrc agent has already been registered*/
+unsigned int rrc_agent_registered[NUM_MAX_ENB];
+
+/*Array containing the Agent-RRC interfaces*/
+AGENT_RRC_xface *agent_rrc_xface[NUM_MAX_ENB];
+
+
+void flexran_agent_ue_state_change(mid_t mod_id, uint32_t rnti, uint8_t state_change) {
+  int size;
+  Protocol__FlexranMessage *msg = NULL;
+  Protocol__FlexHeader *header = NULL;
+  void *data;
+  int priority = 0;
+  err_code_t err_code;
+
+  int xid = 0;
+
+  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_UE_STATE_CHANGE, &header) != 0)
+    goto error;
+
+  Protocol__FlexUeStateChange *ue_state_change_msg;
+  ue_state_change_msg = malloc(sizeof(Protocol__FlexUeStateChange));
+  if(ue_state_change_msg == NULL) {
+    goto error;
+  }
+  protocol__flex_ue_state_change__init(ue_state_change_msg);
+  ue_state_change_msg->has_type = 1;
+  ue_state_change_msg->type = state_change;
+
+  Protocol__FlexUeConfig *config;
+  config = malloc(sizeof(Protocol__FlexUeConfig));
+  if (config == NULL) {
+    goto error;
+  }
+  protocol__flex_ue_config__init(config);
+  if (state_change == PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_DEACTIVATED) {
+    // Simply set the rnti of the UE
+    config->has_rnti = 1;
+    config->rnti = rnti;
+  } else if (state_change == PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_UPDATED
+       || state_change == PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_ACTIVATED) {
+        int i = find_UE_id(mod_id, rnti);
+      config->has_rnti = 1;
+      config->rnti = rnti;
+        if(flexran_get_time_alignment_timer(mod_id,i) != -1) {
+          config->time_alignment_timer = flexran_get_time_alignment_timer(mod_id,i);
+          config->has_time_alignment_timer = 1;
+        }
+        if(flexran_get_meas_gap_config(mod_id,i) != -1){
+          config->meas_gap_config_pattern = flexran_get_meas_gap_config(mod_id,i);
+            config->has_meas_gap_config_pattern = 1;
+        }
+        if(config->has_meas_gap_config_pattern == 1 &&
+         config->meas_gap_config_pattern != PROTOCOL__FLEX_MEAS_GAP_CONFIG_PATTERN__FLMGCP_OFF) {
+        config->meas_gap_config_sf_offset = flexran_get_meas_gap_config_offset(mod_id,i);
+        config->has_meas_gap_config_sf_offset = 1;
+        }
+        //TODO: Set the SPS configuration (Optional)
+        //Not supported for now, so we do not set it
+
+        //TODO: Set the SR configuration (Optional)
+        //We do not set it for now
+
+        //TODO: Set the CQI configuration (Optional)
+        //We do not set it for now
+      
+      if(flexran_get_ue_transmission_mode(mod_id,i) != -1) {
+          config->transmission_mode = flexran_get_ue_transmission_mode(mod_id,i);
+          config->has_transmission_mode = 1;
+        }
+
+      config->ue_aggregated_max_bitrate_ul = flexran_get_ue_aggregated_max_bitrate_ul(mod_id,i);
+        config->has_ue_aggregated_max_bitrate_ul = 1;
+
+      config->ue_aggregated_max_bitrate_dl = flexran_get_ue_aggregated_max_bitrate_dl(mod_id,i);
+        config->has_ue_aggregated_max_bitrate_dl = 1;
+
+        //TODO: Set the UE capabilities
+        Protocol__FlexUeCapabilities *c_capabilities;
+        c_capabilities = malloc(sizeof(Protocol__FlexUeCapabilities));
+        protocol__flex_ue_capabilities__init(c_capabilities);
+        //TODO: Set half duplex (FDD operation)
+        c_capabilities->has_half_duplex = 0;
+        c_capabilities->half_duplex = 1;//flexran_get_half_duplex(i);
+        //TODO: Set intra-frame hopping flag
+        c_capabilities->has_intra_sf_hopping = 0;
+        c_capabilities->intra_sf_hopping = 1;//flexran_get_intra_sf_hopping(i);
+        //TODO: Set support for type 2 hopping with n_sb > 1
+        c_capabilities->has_type2_sb_1 = 0;
+        c_capabilities->type2_sb_1 = 1;//flexran_get_type2_sb_1(i);
+        //TODO: Set ue category
+        c_capabilities->has_ue_category = 0;
+        c_capabilities->ue_category = 1;//flexran_get_ue_category(i);
+        //TODO: Set UE support for resource allocation type 1
+        c_capabilities->has_res_alloc_type1 = 0;
+        c_capabilities->res_alloc_type1 = 1;//flexran_get_res_alloc_type1(i);
+        //Set the capabilites to the message
+        config->capabilities = c_capabilities;
+      
+        if(flexran_get_ue_transmission_antenna(mod_id,i) != -1) {
+        config->has_ue_transmission_antenna = 1;
+        config->ue_transmission_antenna = flexran_get_ue_transmission_antenna(mod_id,i);
+        }
+
+        if(flexran_get_tti_bundling(mod_id,i) != -1) {
+        config->has_tti_bundling = 1;
+        config->tti_bundling = flexran_get_tti_bundling(mod_id,i);
+        }
+
+        if(flexran_get_maxHARQ_TX(mod_id,i) != -1){
+        config->has_max_harq_tx = 1;
+        config->max_harq_tx = flexran_get_maxHARQ_TX(mod_id,i);
+        }
+
+        if(flexran_get_beta_offset_ack_index(mod_id,i) != -1) {
+        config->has_beta_offset_ack_index = 1;
+        config->beta_offset_ack_index = flexran_get_beta_offset_ack_index(mod_id,i);
+        }
+
+        if(flexran_get_beta_offset_ri_index(mod_id,i) != -1) {
+        config->has_beta_offset_ri_index = 1;
+        config->beta_offset_ri_index = flexran_get_beta_offset_ri_index(mod_id,i);
+        }
+
+        if(flexran_get_beta_offset_cqi_index(mod_id,i) != -1) {
+        config->has_beta_offset_cqi_index = 1;
+        config->beta_offset_cqi_index = flexran_get_beta_offset_cqi_index(mod_id,i);
+        }
+
+        /* assume primary carrier */
+        if(flexran_get_ack_nack_simultaneous_trans(mod_id,i,0) != -1) {
+        config->has_ack_nack_simultaneous_trans = 1;
+        config->ack_nack_simultaneous_trans = flexran_get_ack_nack_simultaneous_trans(mod_id,i,0);
+        }
+
+        if(flexran_get_simultaneous_ack_nack_cqi(mod_id,i) != -1) {
+        config->has_simultaneous_ack_nack_cqi = 1;
+        config->simultaneous_ack_nack_cqi = flexran_get_simultaneous_ack_nack_cqi(mod_id,i);
+        }
+
+        if(flexran_get_aperiodic_cqi_rep_mode(mod_id,i) != -1) {
+        config->has_aperiodic_cqi_rep_mode = 1;
+        int mode = flexran_get_aperiodic_cqi_rep_mode(mod_id,i);
+        if (mode > 4) {
+          config->aperiodic_cqi_rep_mode = PROTOCOL__FLEX_APERIODIC_CQI_REPORT_MODE__FLACRM_NONE;
+        } else {
+          config->aperiodic_cqi_rep_mode = mode;
+        }
+        }
+
+        if(flexran_get_tdd_ack_nack_feedback_mode(mod_id, i) != -1) {
+        config->has_tdd_ack_nack_feedback = 1;
+        config->tdd_ack_nack_feedback = flexran_get_tdd_ack_nack_feedback_mode(mod_id,i);
+        }
+
+        if(flexran_get_ack_nack_repetition_factor(mod_id, i) != -1) {
+        config->has_ack_nack_repetition_factor = 1;
+        config->ack_nack_repetition_factor = flexran_get_ack_nack_repetition_factor(mod_id,i);
+        }
+
+        if(flexran_get_extended_bsr_size(mod_id, i) != -1) {
+        config->has_extended_bsr_size = 1;
+        config->extended_bsr_size = flexran_get_extended_bsr_size(mod_id,i);
+        }
+
+      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;
+        if(config->has_ca_support){
+        //TODO: Set cross carrier scheduling support (boolean)
+        config->has_cross_carrier_sched_support = 1;
+        config->cross_carrier_sched_support = 0;
+        //TODO: Set secondary cells configuration
+        // We do not set it for now. No carrier aggregation support
+        
+        //TODO: Set deactivation timer for secondary cell
+        config->has_scell_deactivation_timer = 0;
+        config->scell_deactivation_timer = 0;
+        }
+  } else if (state_change == PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_MOVED) {
+    // TODO: Not supported for now. Leave blank
+  }
+
+  ue_state_change_msg->config = config;
+  msg = malloc(sizeof(Protocol__FlexranMessage));
+  if (msg == NULL) {
+    goto error;
+  }
+  protocol__flexran_message__init(msg);
+  msg->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_UE_STATE_CHANGE_MSG;
+  msg->msg_dir = PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
+  msg->ue_state_change_msg = ue_state_change_msg;
+
+  data = flexran_agent_pack_message(msg, &size);
+  /*Send sr info using the MAC channel of the eNB*/
+  if (flexran_agent_msg_send(mod_id, FLEXRAN_AGENT_DEFAULT, data, size, priority)) {
+    err_code = PROTOCOL__FLEXRAN_ERR__MSG_ENQUEUING;
+    goto error;
+  }
+
+  LOG_D(FLEXRAN_AGENT,"sent message with size %d\n", size);
+  return;
+ error:
+  LOG_E(FLEXRAN_AGENT, "Could not send UE state message becasue of %d \n",err_code);
+}
+
+
+
+int flexran_agent_destroy_ue_state_change(Protocol__FlexranMessage *msg) {
+  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_UE_STATE_CHANGE_MSG)
+    goto error;
+  free(msg->ue_state_change_msg->header);
+  //TODO: Free the contents of the UE config structure
+  free(msg->ue_state_change_msg);
+  free(msg);
+  return 0;
+
+ error:
+  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
+  return -1;
+}
+
+/* this is called by RRC as a part of rrc xface  . The controller previously requested  this*/ 
+void flexran_trigger_rrc_measurements (mid_t mod_id, MeasResults_t*  measResults) {
+
+  int i;
+  // int                   priority = 0; // Warning Preventing
+  // void                  *data;
+  // int                   size;
+  // err_code_t             err_code = -100;
+  triggered_rrc = true;
+  int num;
+
+  num = flexran_get_num_ues (mod_id);
+
+  meas_stats = malloc(sizeof(rrc_meas_stats) * num); 
+
+  for (i = 0; i < num; i++){
+    meas_stats[i].rnti = flexran_get_ue_crnti(mod_id, i);
+    meas_stats[i].meas_id = flexran_get_rrc_pcell_measid(mod_id,i);
+    meas_stats[i].rsrp =  flexran_get_rrc_pcell_rsrp(mod_id,i) - 140;
+    // measResults->measResultPCell.rsrpResult - 140;
+    meas_stats[i].rsrq =  flexran_get_rrc_pcell_rsrq(mod_id,i)/2 - 20;
+    // (measResults->measResultPCell.rsrqResult)/2 - 20;                          
+    
+  }
+    // repl->neigh_meas = NULL;
+
+  // if (meas->measResultNeighCells != NULL) {
+  //   /*
+  //   * Neighboring cells measurements performed by UE.
+  //   */
+  //   NeighCellsMeasurements *neigh_meas;
+  //   neigh_meas = malloc(sizeof(NeighCellsMeasurements));
+  //   if (neigh_meas == NULL)
+  //     goto error;
+  //   neigh_cells_measurements__init(neigh_meas);
+
+  //   /* EUTRAN RRC Measurements. */
+  //   if (meas->measResultNeighCells->present ==
+  //         MeasResults__measResultNeighCells_PR_measResultListEUTRA) {
+
+  //     MeasResultListEUTRA_t meas_list = meas->measResultNeighCells->
+  //                         choice.measResultListEUTRA;
+  //     /* Set the number of EUTRAN measurements present in report. */
+  //     neigh_meas->n_eutra_meas = meas_list.list.count;
+  //     if (neigh_meas->n_eutra_meas > 0) {
+  //       /* Initialize EUTRAN measurements. */
+  //       EUTRAMeasurements **eutra_meas;
+  //       eutra_meas = malloc(sizeof(EUTRAMeasurements *) *
+  //                         neigh_meas->n_eutra_meas);
+  //       for (i = 0; i < neigh_meas->n_eutra_meas; i++) {
+  //         eutra_meas[i] = malloc(sizeof(EUTRAMeasurements));
+  //         eutra_measurements__init(eutra_meas[i]);
+  //         /* Fill in the physical cell identifier. */
+  //         eutra_meas[i]->has_phys_cell_id = 1;
+  //         eutra_meas[i]->phys_cell_id = meas_list.list.array[i]->
+  //                                 physCellId;
+  //         // log_i(agent,"PCI of Target %d", eutra_meas[i]->phys_cell_id);
+  //         /* Check for Reference signal measurements. */
+  //         if (&(meas_list.list.array[i]->measResult)) {
+  //           /* Initialize Ref. signal measurements. */
+  //           EUTRARefSignalMeas *meas_result;
+  //           meas_result = malloc(sizeof(EUTRARefSignalMeas));
+  //           eutra_ref_signal_meas__init(meas_result);
+
+  //           if (meas_list.list.array[i]->measResult.rsrpResult) {
+  //             meas_result->has_rsrp = 1;
+  //             meas_result->rsrp = RSRP_meas_mapping[*(meas_list.
+  //                   list.array[i]->measResult.rsrpResult)];
+  //             // log_i(agent,"RSRP of Target %d", meas_result->rsrp);
+  //           }
+
+  //           if (meas_list.list.array[i]->measResult.rsrqResult) {
+  //             meas_result->has_rsrq = 1;
+  //             meas_result->rsrq = RSRQ_meas_mapping[*(meas_list.
+  //                   list.array[i]->measResult.rsrqResult)];
+  //             // log_i(agent,"RSRQ of Target %d", meas_result->rsrq);
+  //           }
+  //           eutra_meas[i]->meas_result = meas_result;
+  //         }
+  //         /* Check for CGI measurements. */
+  //         if (meas_list.list.array[i]->cgi_Info) {
+  //           /* Initialize CGI measurements. */
+  //           EUTRACgiMeasurements *cgi_meas;
+  //           cgi_meas = malloc(sizeof(EUTRACgiMeasurements));
+  //           eutra_cgi_measurements__init(cgi_meas);
+
+  //           /* EUTRA Cell Global Identity (CGI). */
+  //           CellGlobalIdEUTRA *cgi;
+  //           cgi = malloc(sizeof(CellGlobalIdEUTRA));
+  //           cell_global_id__eutra__init(cgi);
+
+  //           cgi->has_cell_id = 1;
+  //           CellIdentity_t cId = meas_list.list.array[i]->
+  //                     cgi_Info->cellGlobalId.cellIdentity;
+  //           cgi->cell_id = (cId.buf[0] << 20) + (cId.buf[1] << 12) +
+  //                   (cId.buf[2] << 4) + (cId.buf[3] >> 4);
+
+  //           /* Public land mobile network identifier of neighbor
+  //            * cell.
+  //            */
+  //           PlmnIdentity *plmn_id;
+  //           plmn_id = malloc(sizeof(PlmnIdentity));
+  //           plmn_identity__init(plmn_id);
+
+  //           MNC_t mnc = meas_list.list.array[i]->
+  //                 cgi_Info->cellGlobalId.plmn_Identity.mnc;
+
+  //           plmn_id->has_mnc = 1;
+  //           plmn_id->mnc = 0;
+  //           for (m = 0; m < mnc.list.count; m++) {
+  //             plmn_id->mnc += *mnc.list.array[m] *
+  //               ((uint32_t) pow(10, mnc.list.count - m - 1));
+  //           }
+
+  //           MCC_t *mcc = meas_list.list.array[i]->
+  //                 cgi_Info->cellGlobalId.plmn_Identity.mcc;
+
+  //           plmn_id->has_mcc = 1;
+  //           plmn_id->mcc = 0;
+  //           for (m = 0; m < mcc->list.count; m++) {
+  //             plmn_id->mcc += *mcc->list.array[m] *
+  //               ((uint32_t) pow(10, mcc->list.count - m - 1));
+  //           }
+
+  //           TrackingAreaCode_t tac = meas_list.list.array[i]->
+  //                         cgi_Info->trackingAreaCode;
+
+  //           cgi_meas->has_tracking_area_code = 1;
+  //           cgi_meas->tracking_area_code = (tac.buf[0] << 8) +
+  //                               (tac.buf[1]);
+
+  //           PLMN_IdentityList2_t *plmn_l = meas_list.list.array[i]->
+  //                         cgi_Info->plmn_IdentityList;
+
+  //           cgi_meas->n_plmn_id = plmn_l->list.count;
+  //           /* Set the PLMN ID list in CGI measurements. */
+  //           PlmnIdentity **plmn_id_l;
+  //           plmn_id_l = malloc(sizeof(PlmnIdentity *) *
+  //                           cgi_meas->n_plmn_id);
+
+  //           MNC_t mnc2;
+  //           MCC_t *mcc2;
+  //           for (m = 0; m < cgi_meas->n_plmn_id; m++) {
+  //             plmn_id_l[m] = malloc(sizeof(PlmnIdentity));
+  //             plmn_identity__init(plmn_id_l[m]);
+
+  //             mnc2 = plmn_l->list.array[m]->mnc;
+  //             plmn_id_l[m]->has_mnc = 1;
+  //             plmn_id_l[m]->mnc = 0;
+  //             for (k = 0; k < mnc2.list.count; k++) {
+  //               plmn_id_l[m]->mnc += *mnc2.list.array[k] *
+  //               ((uint32_t) pow(10, mnc2.list.count - k - 1));
+  //             }
+
+  //             mcc2 = plmn_l->list.array[m]->mcc;
+  //             plmn_id_l[m]->has_mcc = 1;
+  //             plmn_id_l[m]->mcc = 0;
+  //             for (k = 0; k < mcc2->list.count; k++) {
+  //               plmn_id_l[m]->mcc += *mcc2->list.array[k] *
+  //               ((uint32_t) pow(10, mcc2->list.count - k - 1));
+  //             }
+  //           }
+  //           cgi_meas->plmn_id = plmn_id_l;
+  //           eutra_meas[i]->cgi_meas = cgi_meas;
+  //         }
+  //       }
+  //       neigh_meas->eutra_meas = eutra_meas;
+  //     }
+  //   }
+  //   repl->neigh_meas = neigh_meas;
+  // }
+  /* Attach the RRC measurement reply message to RRC measurements message. */
+  // mrrc_meas->repl = repl;
+  /* Attach RRC measurement message to triggered event message. */
+  // te->mrrc_meas = mrrc_meas;
+  // te->has_action = 0;
+  /* Attach the triggered event message to main message. */
+  // reply->te = te;
+
+  /* Send the report to controller. */
+  // if (flexran_agent_msg_send(b_id, reply) < 0) {
+  //   goto error;
+  // }
+
+  /* Free the measurement report received from UE. */
+  // ASN_STRUCT_FREE(asn_DEF_MeasResults, p->meas);
+  /* Free the params. */
+  // free(p);
+
+
+  // stats_reply_msg->cell_report = cell_report;
+    
+  // stats_reply_msg->ue_report = ue_report;
+  
+  // msg = malloc(sizeof(Protocol__FlexranMessage));
+  // if(msg == NULL)
+  //   goto error;
+  // protocol__flexran_message__init(msg);
+  // msg->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REPLY_MSG;
+  // msg->msg_dir = PROTOCOL__FLEXRAN_DIRECTION__SUCCESSFUL_OUTCOME;
+  // msg->stats_reply_msg = stats_reply_msg;
+  
+  // data = flexran_agent_pack_message(msg, &size);
+  
+  
+  // if (flexran_agent_msg_send(mod_id, FLEXRAN_AGENT_DEFAULT, data, size, priority)) {
+  
+  //   err_code = PROTOCOL__FLEXRAN_ERR__MSG_ENQUEUING;
+  //   goto error;
+  // }
+  
+  //  LOG_I(FLEXRAN_AGENT,"RRC Trigger is done  \n");
+
+  return;
+
+  // error:
+
+    // LOG_E(FLEXRAN_AGENT, "Could not send UE state message becasue of %d \n",err_code);
+    /* Free the measurement report received from UE. */
+    // ASN_STRUCT_FREE(asn_DEF_MeasResults, p->meas);
+    /* Free the params. */
+    // free(p);
+    // return -1;
+}
+
+
+int flexran_agent_rrc_stats_reply(mid_t mod_id,       
+          const report_config_t *report_config,
+           Protocol__FlexUeStatsReport **ue_report,
+           Protocol__FlexCellStatsReport **cell_report) {
+
+
+  // Protocol__FlexHeader *header;
+  int i,j;
+
+  /* Allocate memory for list of UE reports */
+  if (report_config->nr_ue > 0) {
+
+    for (i = 0; i < report_config->nr_ue; i++) {
+      
+      /* Check flag for creation of buffer status report */
+      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_RRC_MEASUREMENTS) {
+      	
+        /*Source Cell*/
+        Protocol__FlexRrcMeasurements *rrc_measurements;
+      	rrc_measurements = malloc(sizeof(Protocol__FlexRrcMeasurements));
+      	if (rrc_measurements == NULL)
+      	  goto error;
+      	protocol__flex_rrc_measurements__init(rrc_measurements);
+      	
+      	rrc_measurements->measid = flexran_get_rrc_pcell_measid(mod_id,i);
+      	rrc_measurements->has_measid = 1;
+      	
+      	rrc_measurements->pcell_rsrp = flexran_get_rrc_pcell_rsrp(mod_id,i);
+      	rrc_measurements->has_pcell_rsrp = 1;
+      	
+      	rrc_measurements->pcell_rsrq = flexran_get_rrc_pcell_rsrq(mod_id,i);
+      	rrc_measurements->has_pcell_rsrq = 1 ;
+
+        
+        /* Target Cell, Neghibouring*/
+        Protocol__FlexNeighCellsMeasurements *neigh_meas;
+        neigh_meas = malloc(sizeof(Protocol__FlexNeighCellsMeasurements));
+        if (neigh_meas == NULL)
+          goto error;
+        protocol__flex_neigh_cells_measurements__init(neigh_meas);
+         
+        
+        neigh_meas->n_eutra_meas = flexran_get_rrc_num_ncell(mod_id, i);
+
+        Protocol__FlexEutraMeasurements **eutra_meas = NULL;
+
+        if (neigh_meas->n_eutra_meas > 0){
+          
+          eutra_meas = malloc(sizeof(Protocol__FlexEutraMeasurements) * neigh_meas->n_eutra_meas);
+          if (eutra_meas == NULL)
+            goto error;
+          
+          for (j = 0; j < neigh_meas->n_eutra_meas; j++ ){
+
+              eutra_meas[j] = malloc(sizeof(Protocol__FlexEutraMeasurements));
+              if (eutra_meas[j] == NULL)
+                goto error;
+
+              protocol__flex_eutra_measurements__init(eutra_meas[j]);
+
+              eutra_meas[j]->phys_cell_id = flexran_get_rrc_neigh_phy_cell_id(mod_id, i, j);
+              eutra_meas[j]->has_phys_cell_id = 1;
+
+
+              /*TODO: Extend for CGI and PLMNID*/
+
+              Protocol__FlexEutraRefSignalMeas *meas_result;
+              meas_result = malloc(sizeof(Protocol__FlexEutraRefSignalMeas));
+
+              protocol__flex_eutra_ref_signal_meas__init(meas_result);     
+
+              meas_result->rsrp = flexran_get_rrc_neigh_rsrp(mod_id, i, eutra_meas[j]->phys_cell_id);
+              meas_result->has_rsrp = 1;
+
+              meas_result->rsrq = flexran_get_rrc_neigh_rsrq(mod_id, i, eutra_meas[j]->phys_cell_id);
+              meas_result->has_rsrq = 1;
+
+              eutra_meas[j]->meas_result = meas_result;
+             
+          }    
+
+           neigh_meas->eutra_meas = eutra_meas;   
+
+           rrc_measurements->neigh_meas = neigh_meas;
+       
+        }
+
+      	 ue_report[i]->rrc_measurements = rrc_measurements;
+      	
+      }
+
+    } 
+
+  }
+
+  /* To be considered for RRC signaling of cell*/ 
+  // if (report_config->nr_cc > 0) { 
+    
+            
+  //           // Fill in the Cell reports
+  //           for (i = 0; i < report_config->nr_cc; i++) {
+
+
+  //                     /* Check flag for creation of noise and interference report */
+  //                     if(report_config->cc_report_type[i].cc_report_flags & PROTOCOL__FLEX_CELL_STATS_TYPE__FLCST_NOISE_INTERFERENCE) {
+  //                           // TODO: Fill in the actual noise and interference report for this cell
+  //                           Protocol__FlexNoiseInterferenceReport *ni_report;
+  //                           ni_report = malloc(sizeof(Protocol__FlexNoiseInterferenceReport));
+  //                           if(ni_report == NULL)
+  //                             goto error;
+  //                           protocol__flex_noise_interference_report__init(ni_report);
+  //                           // Current frame and subframe number
+  //                           ni_report->sfn_sf = flexran_get_sfn_sf(enb_id);
+  //                           ni_report->has_sfn_sf = 1;
+  //                           //TODO:Received interference power in dbm
+  //                           ni_report->rip = 0;
+  //                           ni_report->has_rip = 1;
+  //                           //TODO:Thermal noise power in dbm
+  //                           ni_report->tnp = 0;
+  //                           ni_report->has_tnp = 1;
+
+  //                           ni_report->p0_nominal_pucch = flexran_get_p0_nominal_pucch(enb_id, 0);
+  //                           ni_report->has_p0_nominal_pucch = 1;
+  //                           cell_report[i]->noise_inter_report = ni_report;
+  //                     }
+  //           }
+            
+
+      
+            
+  // }
+
+  return 0;
+
+ error:
+
+  for (i = 0; i < report_config->nr_ue; i++){
+
+      if (ue_report[i]->rrc_measurements->neigh_meas != NULL){
+          for (j = 0; j < flexran_get_rrc_num_ncell(mod_id, i); j++){
+
+             free(ue_report[i]->rrc_measurements->neigh_meas->eutra_meas[j]);
+        }
+        free(ue_report[i]->rrc_measurements->neigh_meas);
+      }
+  }
+
+  if (cell_report != NULL)
+        free(cell_report);
+  if (ue_report != NULL)
+        free(ue_report);
+
+  return -1;
+}
+
+
+int flexran_agent_register_rrc_xface(mid_t mod_id, AGENT_RRC_xface *xface) {
+  if (rrc_agent_registered[mod_id]) {
+    LOG_E(RRC, "RRC agent for eNB %d is already registered\n", mod_id);
+    return -1;
+  }
+
+//  xface->flexran_agent_send_update_rrc_stats = flexran_agent_send_update_rrc_stats;
+  
+  xface->flexran_agent_notify_ue_state_change = flexran_agent_ue_state_change;
+  xface->flexran_trigger_rrc_measurements = flexran_trigger_rrc_measurements;
+
+  rrc_agent_registered[mod_id] = 1;
+  agent_rrc_xface[mod_id] = xface;
+
+  return 0;
+}
+
+int flexran_agent_unregister_rrc_xface(mid_t mod_id, AGENT_RRC_xface *xface) {
+
+  //xface->agent_ctxt = NULL;
+//  xface->flexran_agent_send_update_rrc_stats = NULL;
+
+  xface->flexran_agent_notify_ue_state_change = NULL;
+  xface->flexran_trigger_rrc_measurements = NULL;
+  rrc_agent_registered[mod_id] = 0;
+  agent_rrc_xface[mod_id] = NULL;
+
+  return 0;
+}
diff --git a/openair2/ENB_APP/CONTROL_MODULES/RRC/flexran_agent_rrc.h b/openair2/ENB_APP/CONTROL_MODULES/RRC/flexran_agent_rrc.h
new file mode 100644
index 0000000000000000000000000000000000000000..ae26b4162116268f8de7227a8c532539a0ca7081
--- /dev/null
+++ b/openair2/ENB_APP/CONTROL_MODULES/RRC/flexran_agent_rrc.h
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */ 
+
+/*! \file flexran_agent_rrc.h
+ * \brief FlexRAN agent Control Module RRC header
+ * \author shahab SHARIAT BAGHERI 
+ * \date 2017
+ * \version 0.1
+ */
+
+#ifndef FLEXRAN_AGENT_RRC_H_
+#define FLEXRAN_AGENT_RRC_H_
+
+#include "header.pb-c.h"
+#include "flexran.pb-c.h"
+#include "stats_messages.pb-c.h"
+#include "stats_common.pb-c.h"
+
+
+#include "flexran_agent_common.h"
+#include "flexran_agent_rrc_defs.h"
+
+
+/* Initialization function for the agent structures etc */
+void flexran_agent_init_rrc_agent(mid_t mod_id);
+
+/* UE state change message constructor and destructor */
+void flexran_agent_ue_state_change(mid_t mod_id, uint32_t rnti, uint8_t state_change);
+int flexran_agent_destroy_ue_state_change(Protocol__FlexranMessage *msg);
+
+
+/**********************************
+ * FlexRAN agent - technology RRC API
+ **********************************/
+
+/* Send to the controller all the rrc stat updates that occured during this subframe*/
+// void flexran_agent_send_update_rrc_stats(mid_t mod_id);
+
+/* this is called by RRC as a part of rrc xface  . The controller previously requested  this*/ 
+void flexran_trigger_rrc_measurements (mid_t mod_id, MeasResults_t *);
+
+/* Statistics reply protocol message constructor and destructor */
+int flexran_agent_rrc_stats_reply(mid_t mod_id, const report_config_t *report_config, Protocol__FlexUeStatsReport **ue_report, Protocol__FlexCellStatsReport **cell_report);
+int flexran_agent_rrc_destroy_stats_reply(Protocol__FlexranMessage *msg);
+
+/*Register technology specific interface callbacks*/
+int flexran_agent_register_rrc_xface(mid_t mod_id, AGENT_RRC_xface *xface);
+
+/*Unregister technology specific callbacks*/
+int flexran_agent_unregister_rrc_xface(mid_t mod_id, AGENT_RRC_xface*xface);
+
+#endif
diff --git a/openair2/ENB_APP/CONTROL_MODULES/RRC/flexran_agent_rrc_defs.h b/openair2/ENB_APP/CONTROL_MODULES/RRC/flexran_agent_rrc_defs.h
new file mode 100644
index 0000000000000000000000000000000000000000..cf20c477de83cea1304d540a254f16b0a5c22aa2
--- /dev/null
+++ b/openair2/ENB_APP/CONTROL_MODULES/RRC/flexran_agent_rrc_defs.h
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */ 
+
+
+/*! \file flexran_agent_rrc_defs.h
+ * \brief FlexRAN agent - RRC interface primitives
+ * \author shahab SHARIAT BAGHERI
+ * \date 2017
+ * \version 0.1
+ * \mail 
+ */
+
+#ifndef __FLEXRAN_AGENT_RRC_PRIMITIVES_H__
+#define __FLEXRAN_AGENT_RRC_PRIMITIVES_H__
+
+#include "flexran_agent_defs.h"
+#include "flexran.pb-c.h"
+#include "header.pb-c.h"
+#include "MeasResults.h"
+
+#define RINGBUFFER_SIZE 100
+
+
+typedef struct 
+{
+   int32_t rnti; 
+   int32_t meas_id;
+   int32_t rsrp;
+   int32_t rsrq;
+
+   /*To Be Extended*/
+}rrc_meas_stats;
+
+/* RRC CMI statistics */
+rrc_meas_stats * meas_stats;
+
+
+/* FLEXRAN AGENT-RRC Interface */
+typedef struct {
+  
+  
+   /// Notify the controller for a state change of a particular UE, by sending the proper
+  /// UE state change message (ACTIVATION, DEACTIVATION, HANDOVER)
+  void (*flexran_agent_notify_ue_state_change)(mid_t mod_id, uint32_t rnti,
+                 uint8_t state_change);
+
+  void (*flexran_trigger_rrc_measurements)(mid_t mod_id, MeasResults_t*  measResults);
+  
+} AGENT_RRC_xface;
+
+#endif
diff --git a/openair2/ENB_APP/MESSAGES/V2/config_messages.proto b/openair2/ENB_APP/MESSAGES/V2/config_messages.proto
index ac939b828cc13399511a808a92159919285b7d39..f734686f011dcb1899a3bb95a52a92f4c6076572 100644
--- a/openair2/ENB_APP/MESSAGES/V2/config_messages.proto
+++ b/openair2/ENB_APP/MESSAGES/V2/config_messages.proto
@@ -38,6 +38,11 @@ message flex_cell_config {
 	optional uint32 srs_mac_up_pts = 32;		// Boolean value. See TS 36.211, section 5.5.3.2. TDD only
 	optional uint32 enable_64QAM = 33;		// One of the FLEQ_* enum values
 	optional uint32 carrier_index = 34;		// Carrier component index
+	optional uint32 dl_freq = 35;        // operating downlink frequency
+	optional uint32 ul_freq = 36;       // operating uplink frequency 
+	optional uint32 eutra_band= 37;       // operating band 
+	optional int32 dl_pdsch_power = 38;       // operating downlink power 
+	optional int32 ul_pusch_power = 39;       // operating uplink power
 }
 
 message flex_ue_config {
@@ -76,6 +81,7 @@ message flex_ue_config {
 	optional uint32 pcell_carrier_index = 27;     // Index of primary cell
 	repeated flex_scell_config scell_config = 28;  // Secondary cells configuration
 	optional uint32 scell_deactivation_timer = 29;// Deactivation timer for secondary cell
+	optional uint64 imsi = 30;
 }
 
 message flex_lc_ue_config {
diff --git a/openair2/ENB_APP/MESSAGES/V2/control_delegation.proto b/openair2/ENB_APP/MESSAGES/V2/control_delegation.proto
index 0f7e34de156599a448265e2846b9b69c83aea5c6..ef0eea3bd2eb9ed7062ec11195e6856d246a806c 100644
--- a/openair2/ENB_APP/MESSAGES/V2/control_delegation.proto
+++ b/openair2/ENB_APP/MESSAGES/V2/control_delegation.proto
@@ -3,4 +3,4 @@ package protocol;
 
 enum flex_control_delegation_type {
      FLCDT_MAC_DL_UE_SCHEDULER = 1;		// DL UE scheduler delegation
-}
\ No newline at end of file
+}
diff --git a/openair2/ENB_APP/MESSAGES/V2/controller_commands.proto b/openair2/ENB_APP/MESSAGES/V2/controller_commands.proto
index c92f171b0ee9e21d163633b86c021e74aa47b641..29e072383550fce0fc6dbabc2265fd79e1e62ed5 100644
--- a/openair2/ENB_APP/MESSAGES/V2/controller_commands.proto
+++ b/openair2/ENB_APP/MESSAGES/V2/controller_commands.proto
@@ -16,6 +16,12 @@ message flex_dl_data {
 	optional uint32 act_deact_ce = 6; //Hex content of MAC CE for Activation/Deactivation in CA
 }
 
+
+message flex_ul_data {
+        optional uint32 rnti = 1;
+        optional flex_ul_dci ul_dci = 2;
+}
+
 //
 // Body of the RAR scheduler configuration
 //
@@ -56,4 +62,4 @@ message flex_pdcch_ofdm_sym_count {
 enum flex_broadcast_type {
      FLBT_BCCH = 0;
      FLBT_PCCH = 1;
-}
\ No newline at end of file
+}
diff --git a/openair2/ENB_APP/MESSAGES/V2/flexran.proto b/openair2/ENB_APP/MESSAGES/V2/flexran.proto
index 8c0c300efea152c1b5a3943807504d5d1295bfaf..b54fc171c6acb831466726e40ec33ed3e941bb17 100644
--- a/openair2/ENB_APP/MESSAGES/V2/flexran.proto
+++ b/openair2/ENB_APP/MESSAGES/V2/flexran.proto
@@ -8,6 +8,7 @@ import "config_messages.proto";
 import "controller_commands.proto";
 import "control_delegation.proto";
 
+
 message flexran_message {
        optional flexran_direction msg_dir = 100;
 	oneof msg {
@@ -28,6 +29,8 @@ message flexran_message {
 	      flex_ue_state_change ue_state_change_msg = 15;
 	      flex_control_delegation control_delegation_msg = 16;
 	      flex_agent_reconfiguration agent_reconfiguration_msg = 17;
+	      flex_rrc_triggering rrc_triggering = 18;
+	      flex_ul_mac_config ul_mac_config_msg = 19;
 	}
 }
 
@@ -130,6 +133,7 @@ message flex_enb_config_reply {
 	optional flex_header header = 1;
 	optional uint32 eNB_id = 2;		// Unique id to distinguish the eNB
 	repeated flex_cell_config cell_config = 3;
+        optional uint32 device_spec = 4;
 }
 
 message flex_ue_config_request {
@@ -163,6 +167,18 @@ message flex_dl_mac_config {
 	repeated flex_pdcch_ofdm_sym_count ofdm_sym = 6; // OFDM symbol count for each CC
 }
 
+message flex_ul_mac_config {
+	optional flex_header header = 1;
+ 	optional uint32 sfn_sf = 2;
+	repeated flex_ul_data ul_ue_data = 3;
+}
+
+message flex_rrc_triggering {
+
+	optional flex_header header = 1;
+	optional string rrc_trigger = 2;	
+}
+
 //
 // UE state change message
 //
@@ -190,7 +206,7 @@ message flex_control_delegation {
 
 message flex_agent_reconfiguration {
 	optional flex_header header = 1;
-	optional string policy = 2;		// The policy changes using YAML syntax in string format
+	optional string policy = 2;		// The policy changes using YAML syntax in string format    
 }
 
 // Extensions of the echo request and reply
diff --git a/openair2/ENB_APP/MESSAGES/V2/header.proto b/openair2/ENB_APP/MESSAGES/V2/header.proto
index 939429f51fe134c644d14dce5097154bea17e280..8900b934920eca605cce1746c2aa014ef8564bce 100644
--- a/openair2/ENB_APP/MESSAGES/V2/header.proto
+++ b/openair2/ENB_APP/MESSAGES/V2/header.proto
@@ -40,5 +40,7 @@ enum flex_type {
      // Control delegation messages
      FLPT_DELEGATE_CONTROL = 15;
      FLPT_RECONFIGURE_AGENT = 16;
+     FLPT_RRC_TRIGGERING = 17;
+     FLPT_UL_MAC_CONFIG = 18;
 }
 
diff --git a/openair2/ENB_APP/MESSAGES/V2/mac_primitives.proto b/openair2/ENB_APP/MESSAGES/V2/mac_primitives.proto
index 89b8f0e380605a12077aae640728b7fbf25fcc47..bd2b9cbe509a887b83f29fbfca13c2e29bf1bf2a 100644
--- a/openair2/ENB_APP/MESSAGES/V2/mac_primitives.proto
+++ b/openair2/ENB_APP/MESSAGES/V2/mac_primitives.proto
@@ -36,6 +36,39 @@ message flex_dl_dci {
 	optional uint32 cif = 27;     		// CIF for cross-carrier scheduling
 }
 
+message flex_ul_dci {
+        optional uint32 rnti = 1;
+        optional uint32 rb_start = 2;           // The start RB allocated to the UE 
+        optional uint32 rb_len = 3;             // The number of RBs allocated to the UE
+        optional uint32 mcs = 4;                // Modulation and coding scheme
+        optional uint32 cyclic_shift2 = 5;      //  match DCI format 0/4 PDU 
+        optional uint32 freq_hop_flag = 6;      // 0 no hopping, 1 hoppping
+        optional uint32 freq_hop_map = 7;       // Frequency hopping bits (0..4) 
+        optional uint32 ndi = 8;                // New data indicator
+        optional uint32 rv = 9;                 // Redundancy version
+        optional uint32 harq_pid = 10;          //  The harq process id
+        optional uint32 ultx_mode = 11;         //  A FLULM_* value
+        optional uint32 tbs_size = 12;          // The size of each TBS
+        optional uint32 n_srs = 13;             // Overlap indication with srs
+        optional uint32 res_alloc = 14;         // Type of resource allocation
+        optional uint32 size = 15;    // Size of the ULSCH PDU in bytes for UL Grant.
+
+        optional uint32 dai = 16;               // TDD only
+
+//      optional uint32 tb_swap = 17;           // Boolean. TB to codeword swap flag
+
+//      optional uint32 pdcch_order = 19;
+//      optional uint32 preamble_index = 20;    // Only valid if pdcch_order = 1
+//      optional uint32 prach_mask_index = 21;  // Only valid if pdcch_order = 1
+
+//      optional uint32 tbs_idx = 23;           // The TBS index for Format 1A
+
+
+
+
+}
+
+
 //
 // Messages related to the creation of RLC PDUs
 //
@@ -74,4 +107,4 @@ enum flex_vrb_format {
 enum flex_ngap_val {
      FLNGV_1 = 0;
      FLNGV_2 = 1;
-}
\ No newline at end of file
+}
diff --git a/openair2/ENB_APP/MESSAGES/V2/stats_common.proto b/openair2/ENB_APP/MESSAGES/V2/stats_common.proto
index fa985bf9cc4af4cdfa8318d640239364aac58af2..f651195ab14c51359788acf770a9bc9df42890c6 100644
--- a/openair2/ENB_APP/MESSAGES/V2/stats_common.proto
+++ b/openair2/ENB_APP/MESSAGES/V2/stats_common.proto
@@ -182,3 +182,90 @@ message flex_noise_interference_report {
 	optional int32 p0_nominal_pucch = 4;
 }
 
+//
+// RRC Measurements Primitives
+//
+
+
+message flex_rrc_measurements {
+	// Measurement identifier.
+	optional int32 measid = 1;
+	// Primary Cell Reference Signal Received Power (RSRP).
+	optional int32 pcell_rsrp = 2;
+	// Primary Cell Reference Signal Received Quality (RSRQ).
+	optional int32 pcell_rsrq = 3;
+	// Neighboring cells measurements performed by UE.
+	optional flex_neigh_cells_measurements neigh_meas = 4;
+}
+
+message flex_neigh_cells_measurements {
+	// Neighboring EUTRA cells measurements.
+	repeated flex_eutra_measurements eutra_meas = 1;
+}
+
+message flex_eutra_measurements {
+	// Physical Cell identifier.
+	optional int32 phys_cell_id = 1;
+	// EUTRA Cell Global Identity (CGI) measurement.
+	optional flex_eutra_cgi_measurements cgi_meas = 2;
+	// EUTRA nearby cell reference signal measurement.
+	optional flex_eutra_ref_signal_meas meas_result = 3;
+}
+
+message flex_eutra_cgi_measurements {
+	// EUTRA Cell Global Identity (CGI).
+	optional flex_cell_global_eutra_id cgi = 1;
+	// Tracking area code of the neighbor cell.
+	optional uint32 tracking_area_code = 2;
+	// Public land mobile network identifiers of neighbor cell.
+	repeated flex_plmn_identity plmn_id = 3;
+}
+
+message flex_cell_global_eutra_id {
+	// Public land mobile network identifier of neighbor cell.
+	optional flex_plmn_identity plmn_id = 1;
+	// Cell identifier of neighbor cell.
+	optional uint32 cell_id = 2;
+}
+
+message flex_plmn_identity {
+	// Mobile Network Code (MNC).
+	repeated uint32 mnc = 1;
+	// Mobile Country Code (MCC).
+	repeated uint32 mcc = 2;
+	// tracking area code 
+	repeated uint32 tac = 3;
+}
+
+message flex_eutra_ref_signal_meas {
+	// Neighboring Cell RSRP
+	optional int32 rsrp = 1;
+	// Neighboring Cell RSRQ	
+	optional int32 rsrq = 2;
+}
+
+//
+// PDCP Statistics
+//
+
+message flex_pdcp_stats {
+
+	optional uint32 pkt_tx = 1;
+	optional uint32 pkt_tx_bytes = 2;
+	optional uint32 pkt_tx_sn = 3;
+	optional uint32 pkt_tx_w = 4;
+	optional uint32 pkt_tx_bytes_w = 5;
+	optional uint32 pkt_tx_aiat = 7;
+	optional uint32 pkt_tx_aiat_w = 8;
+	
+	optional uint32 pkt_rx = 9;
+	optional uint32 pkt_rx_bytes = 10;
+	optional uint32 pkt_rx_sn = 11;
+	optional uint32 pkt_rx_w = 12;
+	optional uint32 pkt_rx_bytes_w = 13;
+	optional uint32 pkt_rx_aiat = 14;
+	optional uint32 pkt_rx_aiat_w = 15;
+	optional uint32 pkt_rx_oo = 16;
+
+	optional uint64 sfn=17;
+}
diff --git a/openair2/ENB_APP/MESSAGES/V2/stats_messages.proto b/openair2/ENB_APP/MESSAGES/V2/stats_messages.proto
index d8c0651fa16962a0f26e9c2c922cf0e9dbbd02c9..b8126c55f221a1d8733436a86d42b742391074fd 100644
--- a/openair2/ENB_APP/MESSAGES/V2/stats_messages.proto
+++ b/openair2/ENB_APP/MESSAGES/V2/stats_messages.proto
@@ -47,6 +47,8 @@ message flex_ue_stats_report {
 	optional flex_dl_cqi_report dl_cqi_report = 7;
 	optional flex_paging_buffer_report pbr = 8;
 	optional flex_ul_cqi_report ul_cqi_report = 9;
+	optional flex_rrc_measurements rrc_measurements = 10;
+    optional flex_pdcp_stats pdcp_stats = 11;
 }
 
 //
@@ -77,11 +79,15 @@ enum flex_cell_stats_type {
 // Flags for UE-related statistics
 enum flex_ue_stats_type {
      FLUST_BSR = 1;
-     FLUST_PRH = 2;
+     FLUST_PHR = 2;
      FLUST_RLC_BS = 4;
      FLUST_MAC_CE_BS = 8;
      FLUST_DL_CQI = 16;
      FLUST_PBS = 32;
      FLUST_UL_CQI = 64;
+
+     FLUST_PDCP_STATS = 1024;     
+     FLUST_RRC_MEASUREMENTS = 65536;
      // To be extended with more types of stats
-}
\ No newline at end of file
+    
+}
diff --git a/openair2/ENB_APP/MESSAGES/V2/time_common.proto b/openair2/ENB_APP/MESSAGES/V2/time_common.proto
index 8bd2497443daaed136dc55b39740c66776d95795..6c0affbe51e676a8b725de582098af960ad2ca87 100644
--- a/openair2/ENB_APP/MESSAGES/V2/time_common.proto
+++ b/openair2/ENB_APP/MESSAGES/V2/time_common.proto
@@ -25,6 +25,7 @@ message flex_ul_info {
 	repeated uint32 ul_reception = 2;
 	optional uint32 reception_status = 3;
 	optional uint32 tpc = 4;
-	optional uint32 serv_cell_index = 5; 
+	optional uint32 serv_cell_index = 5;
+	optional uint32 rssi = 6;
 }
 
diff --git a/openair2/ENB_APP/enb_app.c b/openair2/ENB_APP/enb_app.c
index ca404ddc23cb8ef2b6eba5aee687b9d4258cdbed..481bfaa521e961ce08466ff427be69c58e6a259d 100644
--- a/openair2/ENB_APP/enb_app.c
+++ b/openair2/ENB_APP/enb_app.c
@@ -46,10 +46,6 @@
 #   include "gtpv1u_eNB_task.h"
 # endif
 
-#if defined(FLEXRAN_AGENT_SB_IF)
-#   include "flexran_agent.h"
-#endif
-
 extern unsigned char NB_eNB_INST;
 #endif
 
@@ -98,102 +94,6 @@ static void configure_rrc(uint32_t enb_id)
   if (RC.rrc[enb_id]) {
     RCconfig_RRC(msg_p,enb_id, RC.rrc[enb_id]);
     
-  /*
-  RRC_CONFIGURATION_REQ (msg_p).cell_identity =   enb_properties->properties[enb_id]->eNB_id;
-  RRC_CONFIGURATION_REQ (msg_p).tac =             enb_properties->properties[enb_id]->tac;
-  RRC_CONFIGURATION_REQ (msg_p).mcc =             enb_properties->properties[enb_id]->mcc;
-  RRC_CONFIGURATION_REQ (msg_p).mnc =             enb_properties->properties[enb_id]->mnc;
-  RRC_CONFIGURATION_REQ (msg_p).mnc_digit_length = enb_properties->properties[enb_id]->mnc_digit_length;
-
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-    RRC_CONFIGURATION_REQ (msg_p).frame_type[CC_id]                               = enb_properties->properties[enb_id]->frame_type[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).tdd_config[CC_id]                               = enb_properties->properties[enb_id]->tdd_config[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).tdd_config_s[CC_id]                             = enb_properties->properties[enb_id]->tdd_config_s[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).eutra_band[CC_id]                               = enb_properties->properties[enb_id]->eutra_band[CC_id];
-
-    // RACH-Config
-    RRC_CONFIGURATION_REQ (msg_p).rach_numberOfRA_Preambles[CC_id]                = enb_properties->properties[enb_id]->rach_numberOfRA_Preambles[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).rach_preamblesGroupAConfig[CC_id]               = enb_properties->properties[enb_id]->rach_preamblesGroupAConfig[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).rach_sizeOfRA_PreamblesGroupA[CC_id]            = enb_properties->properties[enb_id]->rach_sizeOfRA_PreamblesGroupA[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).rach_messageSizeGroupA[CC_id]                   = enb_properties->properties[enb_id]->rach_messageSizeGroupA[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).rach_messagePowerOffsetGroupB[CC_id]            = enb_properties->properties[enb_id]->rach_messagePowerOffsetGroupB[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).rach_powerRampingStep[CC_id]                    = enb_properties->properties[enb_id]->rach_powerRampingStep[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).rach_preambleInitialReceivedTargetPower[CC_id]  = enb_properties->properties[enb_id]->rach_preambleInitialReceivedTargetPower[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).rach_preambleTransMax[CC_id]                    = enb_properties->properties[enb_id]->rach_preambleTransMax[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).rach_raResponseWindowSize[CC_id]                = enb_properties->properties[enb_id]->rach_raResponseWindowSize[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).rach_macContentionResolutionTimer[CC_id]        = enb_properties->properties[enb_id]->rach_macContentionResolutionTimer[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).rach_maxHARQ_Msg3Tx[CC_id]                      = enb_properties->properties[enb_id]->rach_maxHARQ_Msg3Tx[CC_id];
-
-    // BCCH-Config
-    RRC_CONFIGURATION_REQ (msg_p).bcch_modificationPeriodCoeff[CC_id]             = enb_properties->properties[enb_id]->bcch_modificationPeriodCoeff[CC_id];
-
-    // PCCH-Config
-    RRC_CONFIGURATION_REQ (msg_p).pcch_defaultPagingCycle[CC_id]                  = enb_properties->properties[enb_id]->pcch_defaultPagingCycle[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pcch_nB[CC_id]                                  = enb_properties->properties[enb_id]->pcch_nB[CC_id];
-
-    // PRACH-Config
-    RRC_CONFIGURATION_REQ (msg_p).prach_root[CC_id]                               = enb_properties->properties[enb_id]->prach_root[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).prach_config_index[CC_id]                       = enb_properties->properties[enb_id]->prach_config_index[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).prach_high_speed[CC_id]                         = enb_properties->properties[enb_id]->prach_high_speed[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).prach_zero_correlation[CC_id]                   = enb_properties->properties[enb_id]->prach_zero_correlation[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).prach_freq_offset[CC_id]                        = enb_properties->properties[enb_id]->prach_freq_offset[CC_id];
-
-    // PDSCH-Config
-    RRC_CONFIGURATION_REQ (msg_p).pdsch_referenceSignalPower[CC_id]               = enb_properties->properties[enb_id]->pdsch_referenceSignalPower[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pdsch_p_b[CC_id]                                = enb_properties->properties[enb_id]->pdsch_p_b[CC_id];
-
-    // PUSCH-Config
-    RRC_CONFIGURATION_REQ (msg_p).pusch_n_SB[CC_id]                               = enb_properties->properties[enb_id]->pusch_n_SB[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pusch_hoppingMode[CC_id]                        = enb_properties->properties[enb_id]->pusch_hoppingMode[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pusch_hoppingOffset[CC_id]                      = enb_properties->properties[enb_id]->pusch_hoppingOffset[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pusch_enable64QAM[CC_id]                        = enb_properties->properties[enb_id]->pusch_enable64QAM[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pusch_groupHoppingEnabled[CC_id]                = enb_properties->properties[enb_id]->pusch_groupHoppingEnabled[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pusch_groupAssignment[CC_id]                    = enb_properties->properties[enb_id]->pusch_groupAssignment[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pusch_sequenceHoppingEnabled[CC_id]             = enb_properties->properties[enb_id]->pusch_sequenceHoppingEnabled[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pusch_nDMRS1[CC_id]                             = enb_properties->properties[enb_id]->pusch_nDMRS1[CC_id];
-
-    // PUCCH-Config
-
-    RRC_CONFIGURATION_REQ (msg_p).pucch_delta_shift[CC_id]                        = enb_properties->properties[enb_id]->pucch_delta_shift[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pucch_nRB_CQI[CC_id]                            = enb_properties->properties[enb_id]->pucch_nRB_CQI[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pucch_nCS_AN[CC_id]                             = enb_properties->properties[enb_id]->pucch_nCS_AN[CC_id];
-#if !defined(Rel10) && !defined(Rel14)
-    RRC_CONFIGURATION_REQ (msg_p).pucch_n1_AN[CC_id]                              = enb_properties->properties[enb_id]->pucch_n1_AN[CC_id];
-#endif
-
-    // SRS Config
-    RRC_CONFIGURATION_REQ (msg_p).srs_enable[CC_id]                               = enb_properties->properties[enb_id]->srs_enable[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).srs_BandwidthConfig[CC_id]                      = enb_properties->properties[enb_id]->srs_BandwidthConfig[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).srs_SubframeConfig[CC_id]                       = enb_properties->properties[enb_id]->srs_SubframeConfig[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).srs_ackNackST[CC_id]                            = enb_properties->properties[enb_id]->srs_ackNackST[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).srs_MaxUpPts[CC_id]                             = enb_properties->properties[enb_id]->srs_MaxUpPts[CC_id];
-
-    // uplinkPowerControlCommon
-
-    RRC_CONFIGURATION_REQ (msg_p).pusch_p0_Nominal[CC_id]                         = enb_properties->properties[enb_id]->pusch_p0_Nominal[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pucch_p0_Nominal[CC_id]                         = enb_properties->properties[enb_id]->pucch_p0_Nominal[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pusch_alpha[CC_id]                              = enb_properties->properties[enb_id]->pusch_alpha[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pucch_deltaF_Format1[CC_id]                     = enb_properties->properties[enb_id]->pucch_deltaF_Format1[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pucch_deltaF_Format1b[CC_id]                    = enb_properties->properties[enb_id]->pucch_deltaF_Format1b[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pucch_deltaF_Format2[CC_id]                     = enb_properties->properties[enb_id]->pucch_deltaF_Format2[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pucch_deltaF_Format2a[CC_id]                    = enb_properties->properties[enb_id]->pucch_deltaF_Format2a[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).pucch_deltaF_Format2b[CC_id]                    = enb_properties->properties[enb_id]->pucch_deltaF_Format2b[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).msg3_delta_Preamble[CC_id]                      = enb_properties->properties[enb_id]->msg3_delta_Preamble[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).ul_CyclicPrefixLength[CC_id]                    = enb_properties->properties[enb_id]->ul_CyclicPrefixLength[CC_id];
-
-    // UE Timers and Constants
-
-    RRC_CONFIGURATION_REQ (msg_p).ue_TimersAndConstants_t300[CC_id]               = enb_properties->properties[enb_id]->ue_TimersAndConstants_t300[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).ue_TimersAndConstants_t301[CC_id]               = enb_properties->properties[enb_id]->ue_TimersAndConstants_t301[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).ue_TimersAndConstants_t310[CC_id]               = enb_properties->properties[enb_id]->ue_TimersAndConstants_t310[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).ue_TimersAndConstants_n310[CC_id]               = enb_properties->properties[enb_id]->ue_TimersAndConstants_n310[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).ue_TimersAndConstants_t311[CC_id]               = enb_properties->properties[enb_id]->ue_TimersAndConstants_t311[CC_id];
-    RRC_CONFIGURATION_REQ (msg_p).ue_TimersAndConstants_n311[CC_id]               = enb_properties->properties[enb_id]->ue_TimersAndConstants_n311[CC_id];
-
-    RRC_CONFIGURATION_REQ (msg_p).ue_TransmissionMode[CC_id]                      = enb_properties->properties[enb_id]->ue_TransmissionMode[CC_id];
-
-  }
-  */
 
     LOG_I(ENB_APP,"Sending configuration message to RRC task\n");
     itti_send_msg_to_task (TASK_RRC_ENB, ENB_MODULE_ID_TO_INSTANCE(enb_id), msg_p);
@@ -224,44 +124,6 @@ static uint32_t eNB_app_register(uint32_t enb_id_start, uint32_t enb_id_end)//,
       s1ap_register_eNB = &S1AP_REGISTER_ENB_REQ(msg_p);
       LOG_I(ENB_APP,"default drx %d\n",s1ap_register_eNB->default_drx);
 
-      /*
-   
-      s1ap_register_eNB->eNB_id           = enb_properties->properties[enb_id]->eNB_id;
-      s1ap_register_eNB->cell_type        = enb_properties->properties[enb_id]->cell_type;
-      s1ap_register_eNB->eNB_name         = enb_properties->properties[enb_id]->eNB_name;
-      s1ap_register_eNB->tac              = enb_properties->properties[enb_id]->tac;
-      s1ap_register_eNB->mcc              = enb_properties->properties[enb_id]->mcc;
-      s1ap_register_eNB->mnc              = enb_properties->properties[enb_id]->mnc;
-      s1ap_register_eNB->mnc_digit_length = enb_properties->properties[enb_id]->mnc_digit_length;
-      s1ap_register_eNB->default_drx      = enb_properties->properties[enb_id]->pcch_defaultPagingCycle[0];
-
-      s1ap_register_eNB->nb_mme =         enb_properties->properties[enb_id]->nb_mme;
-
-      AssertFatal (s1ap_register_eNB->nb_mme <= S1AP_MAX_NB_MME_IP_ADDRESS, "Too many MME for eNB %d (%d/%d)!", enb_id, s1ap_register_eNB->nb_mme,
-                   S1AP_MAX_NB_MME_IP_ADDRESS);
-
-      for (mme_id = 0; mme_id < s1ap_register_eNB->nb_mme; mme_id++) {
-        s1ap_register_eNB->mme_ip_address[mme_id].ipv4 = enb_properties->properties[enb_id]->mme_ip_address[mme_id].ipv4;
-        s1ap_register_eNB->mme_ip_address[mme_id].ipv6 = enb_properties->properties[enb_id]->mme_ip_address[mme_id].ipv6;
-        strncpy (s1ap_register_eNB->mme_ip_address[mme_id].ipv4_address,
-                 enb_properties->properties[enb_id]->mme_ip_address[mme_id].ipv4_address,
-                 sizeof(s1ap_register_eNB->mme_ip_address[0].ipv4_address));
-        strncpy (s1ap_register_eNB->mme_ip_address[mme_id].ipv6_address,
-                 enb_properties->properties[enb_id]->mme_ip_address[mme_id].ipv6_address,
-                 sizeof(s1ap_register_eNB->mme_ip_address[0].ipv6_address));
-      }
-
-      s1ap_register_eNB->sctp_in_streams       = enb_properties->properties[enb_id]->sctp_in_streams;
-      s1ap_register_eNB->sctp_out_streams      = enb_properties->properties[enb_id]->sctp_out_streams;
-
-
-      s1ap_register_eNB->enb_ip_address.ipv6 = 0;
-      s1ap_register_eNB->enb_ip_address.ipv4 = 1;
-      addr.s_addr = enb_properties->properties[enb_id]->enb_ipv4_address_for_S1_MME;
-      str = inet_ntoa(addr);
-      strcpy(s1ap_register_eNB->enb_ip_address.ipv4_address, str);
-
-      */
       LOG_I(ENB_APP,"[eNB %d] eNB_app_register for instance %d\n", enb_id, ENB_MODULE_ID_TO_INSTANCE(enb_id));
 
       itti_send_msg_to_task (TASK_S1AP, ENB_MODULE_ID_TO_INSTANCE(enb_id), msg_p);
@@ -288,8 +150,8 @@ void *eNB_app_task(void *args_p)
   long                            enb_register_retry_timer_id;
 # endif
   uint32_t                        enb_id;
-  MessageDef                      *msg_p           = NULL;
-  const char                      *msg_name        = NULL;
+  MessageDef                     *msg_p           = NULL;
+  const char                     *msg_name        = NULL;
   instance_t                      instance;
   int                             result;
   /* for no gcc warnings */
@@ -322,16 +184,6 @@ void *eNB_app_task(void *args_p)
     memset((void *)RC.rrc[enb_id],0,sizeof(eNB_RRC_INST));
     configure_rrc(enb_id);
   }
-  
-#if defined (FLEXRAN_AGENT_SB_IF)
-  
-  for (enb_id = enb_id_start; (enb_id < enb_id_end) ; enb_id++) {
-    printf("\n start enb agent %d\n", enb_id);
-    flexran_agent_start(enb_id, enb_properties_p);
-  }
-#endif 
-  
-
 
 # if defined(ENABLE_USE_MME)
   /* Try to register each eNB */
@@ -352,6 +204,7 @@ void *eNB_app_task(void *args_p)
 
     switch (ITTI_MSG_ID(msg_p)) {
     case TERMINATE_MESSAGE:
+      LOG_W(ENB_APP, " *** Exiting ENB_APP thread\n");
       itti_exit_task ();
       break;
 
diff --git a/openair2/ENB_APP/enb_app.h b/openair2/ENB_APP/enb_app.h
index 52f63806e8e9a27fd042b759ff12ace41f23c892..4dfea72eefbbbf6bc951f562b17f91b869e3ed4a 100644
--- a/openair2/ENB_APP/enb_app.h
+++ b/openair2/ENB_APP/enb_app.h
@@ -30,8 +30,12 @@
 #ifndef ENB_APP_H_
 #define ENB_APP_H_
 
+#include <stdint.h>
 
 
 void *eNB_app_task(void *args_p);
 
+/* needed for flexran: start PHY and RRC when restarting */
+void enb_app_start_phy_rrc(uint32_t enb_id_start, uint32_t enb_id_end);
+
 #endif /* ENB_APP_H_ */
diff --git a/openair2/ENB_APP/enb_config.c b/openair2/ENB_APP/enb_config.c
index 8caa4a17dc3894c13482cc742196fb28a711babd..a30dc28c941d2df60b277c5386004a0050ce1281 100644
--- a/openair2/ENB_APP/enb_config.c
+++ b/openair2/ENB_APP/enb_config.c
@@ -98,6 +98,39 @@ static int enb_check_band_frequencies(char* lib_config_file_name_pP,
   return errors;
 }
 
+void RCconfig_flexran()
+{
+  int i;
+
+  paramdef_t flexranParams[] = FLEXRANPARAMS_DESC;
+  config_get(flexranParams, sizeof(flexranParams)/sizeof(paramdef_t), CONFIG_STRING_NETWORK_CONTROLLER_CONFIG);
+
+  if (!RC.flexran) {
+    RC.flexran = calloc(RC.nb_L1_inst, sizeof(flexran_agent_info_t*));
+    AssertFatal(RC.flexran != NULL,
+                "can't ALLOCATE %zu Bytes for %d flexran agent info with size %zu\n",
+                RC.nb_L1_inst * sizeof(flexran_agent_info_t*),
+                RC.nb_L1_inst, sizeof(flexran_agent_info_t*));
+  }
+
+  /* For all agent instance, fill in the same controller configuration. */
+  for (i = 0; i < RC.nb_L1_inst; i++) {
+    RC.flexran[i] = calloc(1, sizeof(flexran_agent_info_t));
+    AssertFatal(RC.flexran[i] != NULL,
+                "can't ALLOCATE %zu Bytes for flexran agent info (iteration %d/%d)\n",
+                sizeof(flexran_agent_info_t), i + 1, RC.nb_L1_inst);
+    /* if config says "yes", enable Agent, in all other cases it's like "no" */
+    RC.flexran[i]->enabled          = strcmp(*(flexranParams[FLEXRAN_ENABLED].strptr), "yes") == 0;
+    RC.flexran[i]->interface_name   = strdup(*(flexranParams[FLEXRAN_INTERFACE_NAME_IDX].strptr));
+    //inet_ntop(AF_INET, &(enb_properties->properties[mod_id]->flexran_agent_ipv4_address), in_ip, INET_ADDRSTRLEN);
+    RC.flexran[i]->remote_ipv4_addr = strdup(*(flexranParams[FLEXRAN_IPV4_ADDRESS_IDX].strptr));
+    RC.flexran[i]->remote_port      = *(flexranParams[FLEXRAN_PORT_IDX].uptr);
+    RC.flexran[i]->cache_name       = strdup(*(flexranParams[FLEXRAN_CACHE_IDX].strptr));
+    RC.flexran[i]->node_ctrl_state  = strcmp(*(flexranParams[FLEXRAN_AWAIT_RECONF_IDX].strptr), "yes") == 0 ? ENB_WAIT : ENB_NORMAL_OPERATION;
+    RC.flexran[i]->enb_id           = i;
+  }
+}
+
 void RCconfig_L1(void) {
   int               i,j;
   paramdef_t L1_Params[] = L1PARAMS_DESC;
@@ -357,10 +390,6 @@ int RCconfig_RRC(MessageDef *msg_p, uint32_t i, eNB_RRC_INST *rrc) {
 
   
 /* 
-  char*             flexran_agent_interface_name      = NULL;
-  char*             flexran_agent_ipv4_address        = NULL;
-  int32_t     flexran_agent_port                = 0;
-  char*             flexran_agent_cache               = NULL;
   int32_t     otg_ue_id                     = 0;
   char*             otg_app_type                  = NULL;
   char*             otg_bg_traffic                = NULL;
@@ -1934,35 +1963,6 @@ int RCconfig_RRC(MessageDef *msg_p, uint32_t i, eNB_RRC_INST *rrc) {
 	      rrc->srb1_poll_byte             = PollByte_kBinfinity;
 	      rrc->srb1_max_retx_threshold    = UL_AM_RLC__maxRetxThreshold_t8;
 	    }
-	    /*
-	    // Network Controller 
-	    subsetting = config_setting_get_member (setting_enb, ENB_CONFIG_STRING_NETWORK_CONTROLLER_CONFIG);
-
-	    if (subsetting != NULL) {
-	      if (  (
-		     config_setting_lookup_string( subsetting, ENB_CONFIG_STRING_FLEXRAN_AGENT_INTERFACE_NAME,
-						   (const char **)&flexran_agent_interface_name)
-		     && config_setting_lookup_string( subsetting, ENB_CONFIG_STRING_FLEXRAN_AGENT_IPV4_ADDRESS,
-						      (const char **)&flexran_agent_ipv4_address)
-		     && config_setting_lookup_int(subsetting, ENB_CONFIG_STRING_FLEXRAN_AGENT_PORT,
-						  &flexran_agent_port)
-		     && config_setting_lookup_string( subsetting, ENB_CONFIG_STRING_FLEXRAN_AGENT_CACHE,
-						      (const char **)&flexran_agent_cache)
-		     )
-		    ) {
-		enb_properties_loc.properties[enb_properties_loc_index]->flexran_agent_interface_name = strdup(flexran_agent_interface_name);
-		cidr = flexran_agent_ipv4_address;
-		address = strtok(cidr, "/");
-		//enb_properties_loc.properties[enb_properties_loc_index]->flexran_agent_ipv4_address = strdup(address);
-		if (address) {
-		  IPV4_STR_ADDR_TO_INT_NWBO (address, enb_properties_loc.properties[enb_properties_loc_index]->flexran_agent_ipv4_address, "BAD IP ADDRESS FORMAT FOR eNB Agent !\n" );
-		}
-
-		enb_properties_loc.properties[enb_properties_loc_index]->flexran_agent_port = flexran_agent_port;
-		enb_properties_loc.properties[enb_properties_loc_index]->flexran_agent_cache = strdup(flexran_agent_cache);
-	      }
-	    }
-	    */	  
 
 	    /*
 	    // OTG _CONFIG
diff --git a/openair2/ENB_APP/enb_config.h b/openair2/ENB_APP/enb_config.h
index 0406e11e4cd04608df0b494596595ee4af89314c..42bef9c4e990c0746aeb027c950394d5faee0c0f 100644
--- a/openair2/ENB_APP/enb_config.h
+++ b/openair2/ENB_APP/enb_config.h
@@ -94,6 +94,7 @@ typedef struct ru_config_s {
 } ru_config_t;
 
 extern void RCconfig_RU(void);
+extern void RCconfig_flexran(void);
 extern void RCconfig_L1(void);
 extern void RCconfig_macrlc(void);
 extern int  RCconfig_gtpu(void );
diff --git a/openair2/ENB_APP/enb_paramdef.h b/openair2/ENB_APP/enb_paramdef.h
index 6a03687fab8917be9fdab8bd99f6da4451a1a039..2d65e09b58b038ac2dd0b3f6cbdebf9f3e607354 100755
--- a/openair2/ENB_APP/enb_paramdef.h
+++ b/openair2/ENB_APP/enb_paramdef.h
@@ -611,18 +611,29 @@ static int DEFENBS[] = {0};
 
 /*----------------------------------------------------------------------------------------------------------------------------------------------------*/
 /*----------------------------------------------------------------------------------------------------------------------------------------------------*/
-#define ENB_CONFIG_STRING_NETWORK_CONTROLLER_CONFIG         "NETWORK_CONTROLLER"
-
-#define ENB_CONFIG_STRING_FLEXRAN_AGENT_INTERFACE_NAME      "FLEXRAN_AGENT_INTERFACE_NAME"
-#define ENB_CONFIG_STRING_FLEXRAN_AGENT_IPV4_ADDRESS        "FLEXRAN_AGENT_IPV4_ADDRESS"
-#define ENB_CONFIG_STRING_FLEXRAN_AGENT_PORT                "FLEXRAN_AGENT_PORT"
-#define ENB_CONFIG_STRING_FLEXRAN_AGENT_CACHE               "FLEXRAN_AGENT_CACHE"
+#define CONFIG_STRING_NETWORK_CONTROLLER_CONFIG         "NETWORK_CONTROLLER"
+
+#define CONFIG_STRING_FLEXRAN_ENABLED             "FLEXRAN_ENABLED"
+#define CONFIG_STRING_FLEXRAN_INTERFACE_NAME      "FLEXRAN_INTERFACE_NAME"
+#define CONFIG_STRING_FLEXRAN_IPV4_ADDRESS        "FLEXRAN_IPV4_ADDRESS"
+#define CONFIG_STRING_FLEXRAN_PORT                "FLEXRAN_PORT"
+#define CONFIG_STRING_FLEXRAN_CACHE               "FLEXRAN_CACHE"
+#define CONFIG_STRING_FLEXRAN_AWAIT_RECONF        "FLEXRAN_AWAIT_RECONF"
+
+#define FLEXRAN_ENABLED                               0
+#define FLEXRAN_INTERFACE_NAME_IDX                    1
+#define FLEXRAN_IPV4_ADDRESS_IDX                      2
+#define FLEXRAN_PORT_IDX                              3
+#define FLEXRAN_CACHE_IDX                             4
+#define FLEXRAN_AWAIT_RECONF_IDX                      5
 
 #define FLEXRANPARAMS_DESC { \
-{ENB_CONFIG_STRING_FLEXRAN_AGENT_INTERFACE_NAME,         NULL,   0,   uptr:NULL,   defstrval:ENB_CONFIG_STRING_ASN1_VERBOSITY_NONE,   TYPE_STRING,   0},           \
-{ENB_CONFIG_STRING_FLEXRAN_AGENT_IPV4_ADDRESS,           NULL,   0,   uptr:NULL,   defstrval:ENB_CONFIG_STRING_ASN1_VERBOSITY_NONE,   TYPE_STRING,   0},           \
-{ENB_CONFIG_STRING_FLEXRAN_AGENT_PORT,                   NULL,   0,   uptr:NULL,   defstrval:ENB_CONFIG_STRING_ASN1_VERBOSITY_NONE,   TYPE_STRING,   0},           \
-{ENB_CONFIG_STRING_FLEXRAN_AGENT_CACHE,                  NULL,   0,   uptr:NULL,   defstrval:ENB_CONFIG_STRING_ASN1_VERBOSITY_NONE,   TYPE_STRING,   0}            \
+{CONFIG_STRING_FLEXRAN_ENABLED,                NULL,   0,   strptr:NULL,   defstrval:"no",                    TYPE_STRING,   0},           \
+{CONFIG_STRING_FLEXRAN_INTERFACE_NAME,         NULL,   0,   strptr:NULL,   defstrval:"lo",                    TYPE_STRING,   0},           \
+{CONFIG_STRING_FLEXRAN_IPV4_ADDRESS,           NULL,   0,   strptr:NULL,   defstrval:"127.0.0.1",             TYPE_STRING,   0},           \
+{CONFIG_STRING_FLEXRAN_PORT,                   NULL,   0,   uptr:NULL,     defintval:2210,                    TYPE_UINT,     0},           \
+{CONFIG_STRING_FLEXRAN_CACHE,                  NULL,   0,   strptr:NULL,   defstrval:"/mnt/oai_agent_cache",  TYPE_STRING,   0},           \
+{CONFIG_STRING_FLEXRAN_AWAIT_RECONF,           NULL,   0,   strptr:NULL,   defstrval:"no",                    TYPE_STRING,   0}            \
 }
 
 /*----------------------------------------------------------------------------------------------------------------------------------------------------*/
diff --git a/openair2/ENB_APP/flexran_agent.c b/openair2/ENB_APP/flexran_agent.c
index 4faa1f36243353aa7faf6e070e2aa8319a79e8f9..bd46ecc49d246f9e81b7d3d2942f42158d30f0a4 100644
--- a/openair2/ENB_APP/flexran_agent.c
+++ b/openair2/ENB_APP/flexran_agent.c
@@ -21,35 +21,15 @@
 
 /*! \file flexran_agent.h
  * \brief top level flexran agent receive thread and itti task
- * \author Xenofon Foukas and Navid Nikaein
- * \date 2016
+ * \author Xenofon Foukas and Navid Nikaein and shahab SHARIAT BAGHERI
+ * \date 2017
  * \version 0.1
  */
 
-#include "flexran_agent_common.h"
-#include "log.h"
 #include "flexran_agent.h"
-#include "flexran_agent_mac_defs.h"
-#include "flexran_agent_mac.h"
-#include "flexran_agent_mac_internal.h"
-
-#include "flexran_agent_extern.h"
-
-#include "assertions.h"
-
-#include "flexran_agent_net_comm.h"
-#include "flexran_agent_async.h"
 
 #include <arpa/inet.h>
 
-//#define TEST_TIMER
-
-flexran_agent_instance_t flexran_agent[NUM_MAX_ENB];
-
-char in_ip[40];
-static uint16_t in_port;
-char local_cache[40];
-
 void *send_thread(void *args);
 void *receive_thread(void *args);
 pthread_t new_thread(void *(*f)(void *), void *b);
@@ -63,7 +43,7 @@ int agent_task_created = 0;
 */
 void *flexran_agent_task(void *args){
 
-  //flexran_agent_instance_t         *d = (flexran_agent_instance_t *) args;
+  //flexran_agent_info_t         *d = (flexran_agent_info_t *) args;
   Protocol__FlexranMessage *msg;
   void *data;
   int size;
@@ -85,6 +65,7 @@ void *flexran_agent_task(void *args){
 
     switch (ITTI_MSG_ID(msg_p)) {
     case TERMINATE_MESSAGE:
+      LOG_W(FLEXRAN_AGENT, " *** Exiting FLEXRAN thread\n");
       itti_exit_task ();
       break;
 
@@ -123,7 +104,7 @@ void *flexran_agent_task(void *args){
 
 void *receive_thread(void *args) {
 
-  flexran_agent_instance_t         *d = args;
+  flexran_agent_info_t  *d = args;
   void                  *data;
   int                   size;
   int                   priority;
@@ -197,38 +178,25 @@ pthread_t new_thread(void *(*f)(void *), void *b) {
 }
 
 int channel_container_init = 0;
-int flexran_agent_start(mid_t mod_id, const Enb_properties_array_t* enb_properties){
-  
+int flexran_agent_start(mid_t mod_id)
+{
+  flexran_agent_info_t *flexran = RC.flexran[mod_id];
   int channel_id;
-  
-  flexran_set_enb_vars(mod_id, RAN_LTE_OAI);
-  flexran_agent[mod_id].enb_id = mod_id;
-  
-  /* 
-   * check the configuration
-   */ 
-  if (enb_properties->properties[mod_id]->flexran_agent_cache != NULL) {
-    strncpy(local_cache, enb_properties->properties[mod_id]->flexran_agent_cache, sizeof(local_cache));
-    local_cache[sizeof(local_cache) - 1] = 0;
-  } else {
-    strcpy(local_cache, DEFAULT_FLEXRAN_AGENT_CACHE);
-  }
-  
-  if (enb_properties->properties[mod_id]->flexran_agent_ipv4_address != 0) {
-    inet_ntop(AF_INET, &(enb_properties->properties[mod_id]->flexran_agent_ipv4_address), in_ip, INET_ADDRSTRLEN);
-  } else {
-    strcpy(in_ip, DEFAULT_FLEXRAN_AGENT_IPv4_ADDRESS ); 
+  char *in_ip = flexran->remote_ipv4_addr;
+  uint16_t in_port = flexran->remote_port;
+
+  /* if this agent is disabled, return and don't do anything */
+  if (!flexran->enabled) {
+    LOG_I(FLEXRAN_AGENT, "FlexRAN Agent for eNB %d is DISABLED\n", mod_id);
+    return 100;
   }
   
-  if (enb_properties->properties[mod_id]->flexran_agent_port != 0 ) {
-    in_port = enb_properties->properties[mod_id]->flexran_agent_port;
-  } else {
-    in_port = DEFAULT_FLEXRAN_AGENT_PORT ;
-  }
-  LOG_I(FLEXRAN_AGENT,"starting enb agent client for module id %d on ipv4 %s, port %d\n",  
-	flexran_agent[mod_id].enb_id,
-	in_ip,
-	in_port);
+  flexran->enb_id = mod_id;
+  /* assume for the moment the monolithic case, i.e. agent can provide
+   * information for all layers */
+  flexran->capability_mask = FLEXRAN_CAP_LOL1 | FLEXRAN_CAP_HIL1
+                           | FLEXRAN_CAP_LOL2 | FLEXRAN_CAP_HIL2
+                           | FLEXRAN_CAP_PDCP | FLEXRAN_CAP_RRC;
 
   /*
    * Initialize the channel container
@@ -264,10 +232,10 @@ int flexran_agent_start(mid_t mod_id, const Enb_properties_array_t* enb_properti
    *flexran_agent_register_channel(mod_id, channel, FLEXRAN_AGENT_MAC);
    */
 
-  /*Initialize the continuous MAC stats update mechanism*/
-  flexran_agent_init_cont_mac_stats_update(mod_id);
+  /*Initialize the continuous stats update mechanism*/
+  flexran_agent_init_cont_stats_update(mod_id);
   
-  new_thread(receive_thread, &flexran_agent[mod_id]);
+  new_thread(receive_thread, flexran);
 
   /*Initialize and register the mac xface. Must be modified later
    *for more flexibility in agent management */
@@ -275,6 +243,12 @@ int flexran_agent_start(mid_t mod_id, const Enb_properties_array_t* enb_properti
   AGENT_MAC_xface *mac_agent_xface = (AGENT_MAC_xface *) malloc(sizeof(AGENT_MAC_xface));
   flexran_agent_register_mac_xface(mod_id, mac_agent_xface);
   
+  AGENT_RRC_xface *rrc_agent_xface = (AGENT_RRC_xface *) malloc(sizeof(AGENT_RRC_xface));
+  flexran_agent_register_rrc_xface(mod_id, rrc_agent_xface);
+
+  AGENT_PDCP_xface *pdcp_agent_xface = (AGENT_PDCP_xface *) malloc(sizeof(AGENT_PDCP_xface));
+  flexran_agent_register_pdcp_xface(mod_id, pdcp_agent_xface);
+
   /* 
    * initilize a timer 
    */ 
@@ -290,14 +264,34 @@ int flexran_agent_start(mid_t mod_id, const Enb_properties_array_t* enb_properti
    * start the enb agent task for tx and interaction with the underlying network function
    */ 
   if (!agent_task_created) {
-    if (itti_create_task (TASK_FLEXRAN_AGENT, flexran_agent_task, (void *) &flexran_agent[mod_id]) < 0) {
+    if (itti_create_task (TASK_FLEXRAN_AGENT, flexran_agent_task, flexran) < 0) {
       LOG_E(FLEXRAN_AGENT, "Create task for FlexRAN Agent failed\n");
       return -1;
     }
     agent_task_created = 1;
   }
-  
-  LOG_I(FLEXRAN_AGENT,"client ends\n");
+
+  pthread_mutex_init(&flexran->mutex_node_ctrl, NULL);
+  pthread_cond_init(&flexran->cond_node_ctrl, NULL);
+
+  if (flexran->node_ctrl_state == ENB_WAIT) {
+    /* wait three seconds before showing message and waiting "for real".
+     * This way, the message is (hopefully...) the last one and the user knows
+     * what is happening. If the controller sends a reconfiguration message in
+     * the meantime, the softmodem will never wait */
+    sleep(3);
+    LOG_I(ENB_APP, " * eNB %d: Waiting for FlexRAN RTController command *\n", mod_id);
+    pthread_mutex_lock(&flexran->mutex_node_ctrl);
+    while (ENB_NORMAL_OPERATION != flexran->node_ctrl_state)
+      pthread_cond_wait(&flexran->cond_node_ctrl, &flexran->mutex_node_ctrl);
+    pthread_mutex_unlock(&flexran->mutex_node_ctrl);
+
+    /* reconfigure RRC again, the agent might have changed the configuration */
+    MessageDef *msg_p = itti_alloc_new_message(TASK_ENB_APP, RRC_CONFIGURATION_REQ);
+    RRC_CONFIGURATION_REQ(msg_p) = RC.rrc[mod_id]->configuration;
+    itti_send_msg_to_task(TASK_RRC_ENB, ENB_MODULE_ID_TO_INSTANCE(mod_id), msg_p);
+  }
+
   return 0;
 
 error:
diff --git a/openair2/ENB_APP/flexran_agent.h b/openair2/ENB_APP/flexran_agent.h
index 579bf3be2357b4a0ad7d942194fc5690d3eec978..aed8f9eb60dc386700e74fbd1eeb7e7fddd5f96b 100644
--- a/openair2/ENB_APP/flexran_agent.h
+++ b/openair2/ENB_APP/flexran_agent.h
@@ -22,20 +22,28 @@
 /*! \file flexran_agent.h
  * \brief top level flexran agent  
  * \author Navid Nikaein and Xenofon Foukas
- * \date 2016
+ * \date 2017
  * \version 0.1
  */
 
 #ifndef FLEXRAN_AGENT_H_
 #define FLEXRAN_AGENT_H_
 
-#include "enb_config.h" // for enb properties
 #include "flexran_agent_common.h"
+#include "flexran_agent_async.h"
+#include "flexran_agent_extern.h"
+#include "flexran_agent_timer.h"
+#include "flexran_agent_defs.h"
+#include "flexran_agent_net_comm.h"
+#include "flexran_agent_ran_api.h"
+#include "flexran_agent_mac.h"
+#include "flexran_agent_rrc.h"
+#include "flexran_agent_pdcp.h"
+#include "log.h"
+#include "assertions.h"
 
-
-/* Initiation and termination of the eNodeB agent */
-int flexran_agent_start(mid_t mod_id, const Enb_properties_array_t* enb_properties);
-int flexran_agent_stop(mid_t mod_id);
+/* Initiation of the eNodeB agent */
+int flexran_agent_start(mid_t mod_id);
 
 /* 
  * enb agent task mainly wakes up the tx thread for periodic and oneshot messages to the controller 
diff --git a/openair2/ENB_APP/flexran_agent_async.c b/openair2/ENB_APP/flexran_agent_async.c
index f1edc291c9cfab179ab46e5f932578e08ad85357..9e099fc21c31dd1b2ecaf39fc389f46e5770c90f 100644
--- a/openair2/ENB_APP/flexran_agent_async.c
+++ b/openair2/ENB_APP/flexran_agent_async.c
@@ -68,12 +68,12 @@ flexran_agent_async_channel_t * flexran_agent_async_channel_info(mid_t mod_id, c
 
  error:
   LOG_I(FLEXRAN_AGENT,"there was an error\n");
-  return 1;
+  return NULL;
 }
 
 int flexran_agent_async_msg_send(void *data, int size, int priority, void *channel_info) {
   flexran_agent_async_channel_t *channel;
-  channel = (flexran_agent_channel_t *)channel_info;
+  channel = (flexran_agent_async_channel_t *)channel_info;
 
   return message_put(channel->send_queue, data, size, priority);
 }
diff --git a/openair2/ENB_APP/flexran_agent_common.c b/openair2/ENB_APP/flexran_agent_common.c
index 69c786edb333e33f0d12679bd5f711e6dbaa0799..6d32aeedbb152facec421b7cfc4d176177320a6e 100644
--- a/openair2/ENB_APP/flexran_agent_common.c
+++ b/openair2/ENB_APP/flexran_agent_common.c
@@ -21,18 +21,20 @@
 
 /*! \file flexran_agent_common.c
  * \brief common primitives for all agents 
- * \author Xenofon Foukas, Mohamed Kassem and Navid Nikaein
- * \date 2016
+ * \author Xenofon Foukas, Mohamed Kassem and Navid Nikaein, shahab SHARIAT BAGHERI
+ * \date 2017
  * \version 0.1
  */
 
-#include<stdio.h>
+#include <stdio.h>
 #include <time.h>
+#include <sys/stat.h>
 
 #include "flexran_agent_common.h"
 #include "flexran_agent_common_internal.h"
 #include "flexran_agent_extern.h"
 #include "flexran_agent_net_comm.h"
+#include "flexran_agent_ran_api.h"
 #include "PHY/extern.h"
 #include "log.h"
 
@@ -41,10 +43,6 @@
 #include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
 #include "rrc_eNB_UE_context.h"
 
-void * enb[NUM_MAX_ENB];
-void * enb_ue[NUM_MAX_ENB];
-void * enb_rrc[NUM_MAX_ENB];
-
 /*
  * message primitives
  */
@@ -108,7 +106,7 @@ int flexran_create_header(xid_t xid, Protocol__FlexType type,  Protocol__FlexHea
 
 int flexran_agent_hello(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
  
-  Protocol__FlexHeader *header;
+  Protocol__FlexHeader *header = NULL;
   /*TODO: Need to set random xid or xid from received hello message*/
   xid_t xid = 1;
 
@@ -163,7 +161,7 @@ int flexran_agent_destroy_hello(Protocol__FlexranMessage *msg) {
 
 
 int flexran_agent_echo_request(mid_t mod_id, const void* params, Protocol__FlexranMessage **msg) {
-  Protocol__FlexHeader *header;
+  Protocol__FlexHeader *header = NULL;
   /*TODO: Need to set a random xid*/
   xid_t xid = 1;
 
@@ -218,6 +216,7 @@ int flexran_agent_destroy_echo_request(Protocol__FlexranMessage *msg) {
 int flexran_agent_echo_reply(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
   
   xid_t xid;
+  Protocol__FlexHeader *header = NULL;
   Protocol__FlexranMessage *input = (Protocol__FlexranMessage *)params;
   Protocol__FlexEchoRequest *echo_req = input->echo_request_msg;
   xid = (echo_req->header)->xid;
@@ -228,7 +227,6 @@ int flexran_agent_echo_reply(mid_t mod_id, const void *params, Protocol__Flexran
     goto error;
   protocol__flex_echo_reply__init(echo_reply_msg);
 
-  Protocol__FlexHeader *header;
   if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_ECHO_REPLY, &header) != 0)
     goto error;
 
@@ -344,19 +342,6 @@ int flexran_agent_destroy_lc_config_reply(Protocol__FlexranMessage *msg) {
   return -1;
 }
 
-int flexran_agent_destroy_ue_state_change(Protocol__FlexranMessage *msg) {
-  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_UE_STATE_CHANGE_MSG)
-    goto error;
-  free(msg->ue_state_change_msg->header);
-  //TODO: Free the contents of the UE config structure
-  free(msg->ue_state_change_msg);
-  free(msg);
-  return 0;
-
- error:
-  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
-  return -1;
-}
 
 int flexran_agent_destroy_enb_config_request(Protocol__FlexranMessage *msg) {
   if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_ENB_CONFIG_REQUEST_MSG)
@@ -402,14 +387,13 @@ int flexran_agent_control_delegation(mid_t mod_id, const void *params, Protocol_
   Protocol__FlexControlDelegation *control_delegation_msg = input->control_delegation_msg;
 
   //  struct timespec vartime = timer_start();
-  
   //Write the payload lib into a file in the cache and load the lib
   char lib_name[120];
   char target[512];
   snprintf(lib_name, sizeof(lib_name), "/%s.so", control_delegation_msg->name);
-  strcpy(target, local_cache);
+  strcpy(target, RC.flexran[mod_id]->cache_name);
   strcat(target, lib_name);
-
+  
   FILE *f;
   f = fopen(target, "wb");
   fwrite(control_delegation_msg->payload.data, control_delegation_msg->payload.len, 1, f);
@@ -442,1088 +426,10 @@ int flexran_agent_destroy_agent_reconfiguration(Protocol__FlexranMessage *msg) {
 }
 
 
-/*
- * get generic info from RAN
- */
-
-void flexran_set_enb_vars(mid_t mod_id, ran_name_t ran){
-
-  switch (ran){
-  case RAN_LTE_OAI :
-    enb[mod_id] =  (void *)&eNB_mac_inst[mod_id];
-    enb_ue[mod_id] = (void *)&eNB_mac_inst[mod_id].UE_list;
-    enb_rrc[mod_id] = (void *)&eNB_rrc_inst[mod_id];
-    break;
-  default :
-    goto error;
-  }
-
-  return; 
-
- error:
-  LOG_E(FLEXRAN_AGENT, "unknown RAN name %d\n", ran);
-}
-
-int flexran_get_current_time_ms (mid_t mod_id, int subframe_flag){
-
-  if (subframe_flag == 1){
-    return ((eNB_MAC_INST *)enb[mod_id])->frame*10 + ((eNB_MAC_INST *)enb[mod_id])->subframe;
-  }else {
-    return ((eNB_MAC_INST *)enb[mod_id])->frame*10;
-  }
-   
-}
-
-unsigned int flexran_get_current_frame (mid_t mod_id) {
-
-  //  #warning "SFN will not be in [0-1023] when oaisim is used"
-  return ((eNB_MAC_INST *)enb[mod_id])->frame;
-  
-}
-
-unsigned int flexran_get_current_system_frame_num(mid_t mod_id) {
-  return (flexran_get_current_frame(mod_id) %1024);
-}
-
-unsigned int flexran_get_current_subframe (mid_t mod_id) {
-
-  return ((eNB_MAC_INST *)enb[mod_id])->subframe;
-  
-}
-
-uint16_t flexran_get_sfn_sf (mid_t mod_id) {
-  
-  frame_t frame;
-  sub_frame_t subframe;
-  uint16_t sfn_sf, frame_mask, sf_mask;
-  
-  frame = (frame_t) flexran_get_current_system_frame_num(mod_id);
-  subframe = (sub_frame_t) flexran_get_current_subframe(mod_id);
-  frame_mask = ((1<<12) - 1);
-  sf_mask = ((1<<4) - 1);
-  sfn_sf = (subframe & sf_mask) | ((frame & frame_mask) << 4);
-  
-  return sfn_sf;
-}
-
-uint16_t flexran_get_future_sfn_sf (mid_t mod_id, int ahead_of_time) {
-  
-  frame_t frame;
-  sub_frame_t subframe;
-  uint16_t sfn_sf, frame_mask, sf_mask;
-  
-  frame = (frame_t) flexran_get_current_system_frame_num(mod_id);
-  subframe = (sub_frame_t) flexran_get_current_subframe(mod_id);
-
-  subframe = ((subframe + ahead_of_time) % 10);
-  
-  if (subframe < flexran_get_current_subframe(mod_id)) {
-    frame = (frame + 1) % 1024;
-  }
-  
-  int additional_frames = ahead_of_time / 10;
-  frame = (frame + additional_frames) % 1024;
-  
-  frame_mask = ((1<<12) - 1);
-  sf_mask = ((1<<4) - 1);
-  sfn_sf = (subframe & sf_mask) | ((frame & frame_mask) << 4);
-  
-  return sfn_sf;
-}
-
-int flexran_get_num_ues (mid_t mod_id){
-
-  return  ((UE_list_t *)enb_ue[mod_id])->num_UEs;
-}
-
-int flexran_get_ue_crnti (mid_t mod_id, mid_t ue_id) {
-
-  return  UE_RNTI(mod_id, ue_id);
-}
-
-int flexran_get_ue_bsr (mid_t mod_id, mid_t ue_id, lcid_t lcid) {
-
-  return ((UE_list_t *)enb_ue[mod_id])->UE_template[UE_PCCID(mod_id,ue_id)][ue_id].bsr_info[lcid];
-}
-
-int flexran_get_ue_phr (mid_t mod_id, mid_t ue_id) {
-
-  return ((UE_list_t *)enb_ue[mod_id])->UE_template[UE_PCCID(mod_id,ue_id)][ue_id].phr_info;
-}
-
-int flexran_get_ue_wcqi (mid_t mod_id, mid_t ue_id) {
-  LTE_eNB_UE_stats     *eNB_UE_stats     = NULL;
-  eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id, 0, UE_RNTI(mod_id, ue_id));
-  return eNB_UE_stats->DL_cqi[0];
-
-  //  return ((UE_list_t *)enb_ue[mod_id])->eNB_UE_stats[UE_PCCID(mod_id,ue_id)][ue_id].dl_cqi;
-}
-
-int flexran_get_tx_queue_size(mid_t mod_id, mid_t ue_id, logical_chan_id_t channel_id) {
-  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
-  uint16_t frame = (uint16_t) flexran_get_current_frame(mod_id);
-  uint16_t subframe = (uint16_t) flexran_get_current_subframe(mod_id);
-  mac_rlc_status_resp_t rlc_status = mac_rlc_status_ind(mod_id,rnti, mod_id,frame,subframe,ENB_FLAG_YES,MBMS_FLAG_NO, channel_id, 0);
-  return rlc_status.bytes_in_buffer;
-}
-
-int flexran_get_hol_delay(mid_t mod_id, mid_t ue_id, logical_chan_id_t channel_id) {
-  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
-  uint16_t frame = (uint16_t) flexran_get_current_frame(mod_id);
-  uint16_t subframe = (uint16_t) flexran_get_current_subframe(mod_id);
-  mac_rlc_status_resp_t rlc_status = mac_rlc_status_ind(mod_id, rnti, mod_id, frame, subframe, ENB_FLAG_YES, MBMS_FLAG_NO, channel_id, 0);
-  return rlc_status.head_sdu_creation_time;
-}
-
-short flexran_get_TA(mid_t mod_id, mid_t ue_id, int CC_id) {
-  
-  UE_list_t *UE_list=&eNB_mac_inst[mod_id].UE_list;
-  int rnti;
-
-  rnti = flexran_get_ue_crnti(mod_id, ue_id);
-
-  LTE_eNB_UE_stats		*eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
-  //ue_sched_ctl->ta_timer		      = 20;	// wait 20 subframes before taking TA measurement from PHY                                         
-  switch (PHY_vars_eNB_g[mod_id][CC_id]->frame_parms.N_RB_DL) {
-  case 6:
-    return eNB_UE_stats->timing_advance_update;
-  case 15:
-    return eNB_UE_stats->timing_advance_update/2;
-  case 25:
-    return eNB_UE_stats->timing_advance_update/4;
-  case 50:
-    return eNB_UE_stats->timing_advance_update/8;
-  case 75:
-    return eNB_UE_stats->timing_advance_update/12;
-  case 100:
-    if (PHY_vars_eNB_g[mod_id][CC_id]->frame_parms.threequarter_fs == 0) {
-      return eNB_UE_stats->timing_advance_update/16;
-    } else {
-      return eNB_UE_stats->timing_advance_update/12;
-    }
-  default:
-    return 0;
-  }
-}
-
-void flexran_update_TA(mid_t mod_id, mid_t ue_id, int CC_id) {
-  
-  UE_list_t *UE_list=&eNB_mac_inst[mod_id].UE_list;
-  UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[ue_id];
-
-  if (ue_sched_ctl->ta_timer == 0) {
-    
-    // WE SHOULD PROTECT the eNB_UE_stats with a mutex here ...                                                                         
-    //    LTE_eNB_UE_stats		*eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
-    //ue_sched_ctl->ta_timer		      = 20;	// wait 20 subframes before taking TA measurement from PHY                                         
-    ue_sched_ctl->ta_update = flexran_get_TA(mod_id, ue_id, CC_id);
-
-    // clear the update in case PHY does not have a new measurement after timer expiry                                               
-    //    eNB_UE_stats->timing_advance_update	      = 0;
-  } else {
-    ue_sched_ctl->ta_timer--;
-    ue_sched_ctl->ta_update		      = 0;	// don't trigger a timing advance command      
-  }
-}
-
-int flexran_get_MAC_CE_bitmap_TA(mid_t mod_id, mid_t ue_id,int CC_id) {
-  
-  UE_list_t			*UE_list      = &eNB_mac_inst[mod_id].UE_list;
-
-  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
-  LTE_eNB_UE_stats *eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id,CC_id,rnti);
-  
-  if (eNB_UE_stats == NULL) {
-    return 0;
-  }
-
-  if (flexran_get_TA(mod_id, ue_id, CC_id) != 0) {
-    return PROTOCOL__FLEX_CE_TYPE__FLPCET_TA;
-  } else {
-    return 0;
-  }
-
-}
-
-int flexran_get_active_CC(mid_t mod_id, mid_t ue_id) {
-	return ((UE_list_t *)enb_ue[mod_id])->numactiveCCs[ue_id];
-}
-
-int flexran_get_current_RI(mid_t mod_id, mid_t ue_id, int CC_id) {
-	LTE_eNB_UE_stats	*eNB_UE_stats = NULL;
-
-	rnti_t			 rnti	      = flexran_get_ue_crnti(mod_id,ue_id);
-
-	eNB_UE_stats			      = mac_xface->get_eNB_UE_stats(mod_id,CC_id,rnti);
-	
-	if (eNB_UE_stats == NULL) {
-	  return 0;
-	}
-
-	return eNB_UE_stats[CC_id].rank;
-}
-
-int flexran_get_tpc(mid_t mod_id, mid_t ue_id) {
-	LTE_eNB_UE_stats	*eNB_UE_stats = NULL;
-	int32_t			 normalized_rx_power, target_rx_power;
-	int			 tpc	      = 1;
-
-	int			 pCCid	      = UE_PCCID(mod_id,ue_id);
-	rnti_t			 rnti	      = flexran_get_ue_crnti(mod_id,ue_id);
-
-	eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id, pCCid, rnti);
-
-	target_rx_power = mac_xface->get_target_pusch_rx_power(mod_id,pCCid);
-
-	if (eNB_UE_stats == NULL) {
-	  normalized_rx_power = target_rx_power;
-	} else if (eNB_UE_stats->UL_rssi != NULL) {
-	  normalized_rx_power = eNB_UE_stats->UL_rssi[0];
-	} else {
-	  normalized_rx_power = target_rx_power;
-	}
-
-	if (normalized_rx_power>(target_rx_power+1)) {
-		tpc = 0;	//-1
-	} else if (normalized_rx_power<(target_rx_power-1)) {
-		tpc = 2;	//+1
-	} else {
-		tpc = 1;	//0
-	}
-	return tpc;
-}
-
-int flexran_get_harq(const mid_t mod_id, 
-		     const uint8_t CC_id, 
-		     const mid_t ue_id, 
-		     const int frame, 
-		     const uint8_t subframe, 
-		     uint8_t *id, 
-		     uint8_t *round)	{ //flag_id_status = 0 then id, else status
-	/*TODO: Add int TB in function parameters to get the status of the second TB. This can be done to by editing in
-	 * get_ue_active_harq_pid function in line 272 file: phy_procedures_lte_eNB.c to add
-	 * DLSCH_ptr = PHY_vars_eNB_g[Mod_id][CC_id]->dlsch_eNB[(uint32_t)UE_id][1];*/
-
-  uint8_t harq_pid;
-  uint8_t harq_round;
-  
-
-  uint16_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
-
-  mac_xface->get_ue_active_harq_pid(mod_id,CC_id,rnti,frame,subframe,&harq_pid,&harq_round,openair_harq_DL);
-
-  *id = harq_pid;
-  *round = harq_round;
-  /* if (round > 0) { */
-  /*   *status = 1; */
-  /* } else { */
-  /*   *status = 0; */
-  /* } */
-
-  /* return 0; */
-  return *round;
-}
-
-int flexran_get_p0_pucch_dbm(mid_t mod_id, mid_t ue_id, int CC_id) {
-  LTE_eNB_UE_stats *eNB_UE_stats = NULL;
-  uint32_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  eNB_UE_stats =  mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
-  
-  if (eNB_UE_stats == NULL) {
-    return -1;
-  }
-  
-  //	if(eNB_UE_stats->Po_PUCCH_update == 1) {
-  return eNB_UE_stats->Po_PUCCH_dBm;
-  //}
-  //else
-  //  return -1;
-}
-
-int flexran_get_p0_nominal_pucch(mid_t mod_id, int CC_id) {
-  int32_t pucch_rx_received = mac_xface->get_target_pucch_rx_power(mod_id, CC_id);
-  return pucch_rx_received;
-}
-
-int flexran_get_p0_pucch_status(mid_t mod_id, mid_t ue_id, int CC_id) {
-  LTE_eNB_UE_stats *eNB_UE_stats = NULL;
-  uint32_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  eNB_UE_stats =  mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
-  return eNB_UE_stats->Po_PUCCH_update;
-}
-
-int flexran_update_p0_pucch(mid_t mod_id, mid_t ue_id, int CC_id) {
-  LTE_eNB_UE_stats *eNB_UE_stats = NULL;
-  uint32_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  eNB_UE_stats =  mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
-  eNB_UE_stats->Po_PUCCH_update = 0;
-  
-  return 0;
-}
-
-
-/*
- * ************************************
- * Get Messages for eNB Configuration Reply
- * ************************************
- */
-
-int flexran_get_hopping_offset(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->pusch_config_common.pusch_HoppingOffset;
-}
-
-int flexran_get_hopping_mode(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->pusch_config_common.hoppingMode;
-}
-
-int flexran_get_n_SB(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->pusch_config_common.n_SB;
-}
-
-int flexran_get_enable64QAM(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->pusch_config_common.enable64QAM;
-}
-
-int flexran_get_phich_duration(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->phich_config_common.phich_duration;
-}
-
-int flexran_get_phich_resource(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	if(frame_parms->phich_config_common.phich_resource == oneSixth)
-		return 0;
-	else if(frame_parms->phich_config_common.phich_resource == half)
-		return 1;
-	else if(frame_parms->phich_config_common.phich_resource == one)
-		return 2;
-	else if(frame_parms->phich_config_common.phich_resource == two)
-		return 3;
-
-	return -1;
-}
-
-int flexran_get_n1pucch_an(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->pucch_config_common.n1PUCCH_AN;
-}
-
-int flexran_get_nRB_CQI(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->pucch_config_common.nRB_CQI;
-}
-
-int flexran_get_deltaPUCCH_Shift(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->pucch_config_common.deltaPUCCH_Shift;
-}
-
-int flexran_get_prach_ConfigIndex(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->prach_config_common.prach_ConfigInfo.prach_ConfigIndex;
-}
-
-int flexran_get_prach_FreqOffset(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->prach_config_common.prach_ConfigInfo.prach_FreqOffset;
-}
-
-int flexran_get_maxHARQ_Msg3Tx(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->maxHARQ_Msg3Tx;
-}
-
-int flexran_get_ul_cyclic_prefix_length(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->Ncp_UL;
-}
-
-int flexran_get_dl_cyclic_prefix_length(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->Ncp;
-}
-
-int flexran_get_cell_id(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->Nid_cell;
-}
-
-int flexran_get_srs_BandwidthConfig(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->soundingrs_ul_config_common.srs_BandwidthConfig;
-}
-
-int flexran_get_srs_SubframeConfig(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->soundingrs_ul_config_common.srs_SubframeConfig;
-}
-
-int flexran_get_srs_MaxUpPts(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->soundingrs_ul_config_common.srs_MaxUpPts;
-}
-
-int flexran_get_N_RB_DL(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->N_RB_DL;
-}
-
-int flexran_get_N_RB_UL(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->N_RB_UL;
-}
-
-int flexran_get_N_RBG(mid_t mod_id, int CC_id) {
-  	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->N_RBG;
-}
-
-int flexran_get_subframe_assignment(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->tdd_config;
-}
-
-int flexran_get_special_subframe_assignment(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	return frame_parms->tdd_config_S;
-}
-
-int flexran_get_ra_ResponseWindowSize(mid_t mod_id, int CC_id) {
-  return enb_config_get()->properties[mod_id]->rach_raResponseWindowSize[CC_id];
-}
-
-int flexran_get_mac_ContentionResolutionTimer(mid_t mod_id, int CC_id) {
-  return enb_config_get()->properties[mod_id]->rach_macContentionResolutionTimer[CC_id];
-}
-
-int flexran_get_duplex_mode(mid_t mod_id, int CC_id) {
-	LTE_DL_FRAME_PARMS   *frame_parms;
-
-	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-	if(frame_parms->frame_type == TDD)
-		return PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD;
-	else if (frame_parms->frame_type == FDD)
-		return PROTOCOL__FLEX_DUPLEX_MODE__FLDM_FDD;
-
-	return -1;
-}
-
-long flexran_get_si_window_length(mid_t mod_id, int CC_id) {
-	return  ((eNB_RRC_INST *)enb_rrc[mod_id])->carrier[CC_id].sib1->si_WindowLength;
-}
-
-int flexran_get_sib1_length(mid_t mod_id, int CC_id) {
-	return  ((eNB_RRC_INST *)enb_rrc[mod_id])->carrier[CC_id].sizeof_SIB1;
-}
-
-int flexran_get_num_pdcch_symb(mid_t mod_id, int CC_id) {
-  /* TODO: This should return the number of PDCCH symbols initially used by the cell CC_id */
-  return 0;
-  //(PHY_vars_UE_g[mod_id][CC_id]->lte_ue_pdcch_vars[mod_id]->num_pdcch_symbols);
-}
-
-
-
-/*
- * ************************************
- * Get Messages for UE Configuration Reply
- * ************************************
- */
-
-
-int flexran_get_time_alignment_timer(mid_t mod_id, mid_t ue_id) {
-  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
-  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
-  if(ue_context_p != NULL) {
-    if(ue_context_p->ue_context.mac_MainConfig != NULL) {
-      return ue_context_p->ue_context.mac_MainConfig->timeAlignmentTimerDedicated;
-    } else {
-      return -1;
-    }
-  } else {
-    return -1;
-  }
-}
-
-int flexran_get_meas_gap_config(mid_t mod_id, mid_t ue_id) {
-  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
-  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
-
-  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
-  if(ue_context_p != NULL) {
-    if(ue_context_p->ue_context.measGapConfig != NULL) {
-      if(ue_context_p->ue_context.measGapConfig->present == MeasGapConfig_PR_setup) {
-	if (ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.present == MeasGapConfig__setup__gapOffset_PR_gp0) {
-	  return PROTOCOL__FLEX_MEAS_GAP_CONFIG_PATTERN__FLMGCP_GP1;
-	} else if (ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.present == MeasGapConfig__setup__gapOffset_PR_gp1) {
-	  return PROTOCOL__FLEX_MEAS_GAP_CONFIG_PATTERN__FLMGCP_GP2;
-	} else {
-	  return PROTOCOL__FLEX_MEAS_GAP_CONFIG_PATTERN__FLMGCP_OFF;
-	}
-      }
-    }
-  }
-  return -1;
-}
-
-
-int flexran_get_meas_gap_config_offset(mid_t mod_id, mid_t ue_id) {
-  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
-  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
-  
-  if(ue_context_p != NULL) {
-    if(ue_context_p->ue_context.measGapConfig != NULL){
-      if(ue_context_p->ue_context.measGapConfig->present == MeasGapConfig_PR_setup) {
-	if (ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.present == MeasGapConfig__setup__gapOffset_PR_gp0) {
-	  return ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.choice.gp0;
-	} else if (ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.present == MeasGapConfig__setup__gapOffset_PR_gp1) {
-	  return ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.choice.gp0;
-	} 
-      }
-    }
-  }
-  return -1;
-}
-
-int flexran_get_ue_aggregated_max_bitrate_dl (mid_t mod_id, mid_t ue_id) {
-	return ((UE_list_t *)enb_ue[mod_id])->UE_sched_ctrl[ue_id].ue_AggregatedMaximumBitrateDL;
-}
-
-int flexran_get_ue_aggregated_max_bitrate_ul (mid_t mod_id, mid_t ue_id) {
-	return ((UE_list_t *)enb_ue[mod_id])->UE_sched_ctrl[ue_id].ue_AggregatedMaximumBitrateUL;
-}
-
-int flexran_get_half_duplex(mid_t ue_id) {
-  // TODO
-	//int halfduplex = 0;
-	//int bands_to_scan = ((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->rf_Parameters.supportedBandListEUTRA.list.count;
-	//for (int i =0; i < bands_to_scan; i++){
-		//if(((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->rf_Parameters.supportedBandListEUTRA.list.array[i]->halfDuplex > 0)
-		//	halfduplex = 1;
-	//}
-	//return halfduplex;
-  return 0;
-}
-
-int flexran_get_intra_sf_hopping(mid_t ue_id) {
-	//TODO:Get proper value
-	//temp = (((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->featureGroupIndicators->buf);
-	//return (0 & ( 1 << (31)));
-  return 0;
-}
-
-int flexran_get_type2_sb_1(mid_t ue_id) {
-	//TODO:Get proper value
-	//uint8_t temp = 0;
-	//temp = (((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->featureGroupIndicators->buf);
-	//return (temp & ( 1 << (11)));
-  return 0;
-}
-
-int flexran_get_ue_category(mid_t ue_id) {
-	//TODO:Get proper value
-	//return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->ue_Category);
-  return 0;
-}
-
-int flexran_get_res_alloc_type1(mid_t ue_id) {
-	//TODO:Get proper value
-	//uint8_t temp = 0;
-	//temp = (((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->featureGroupIndicators->buf);
-	//return (temp & ( 1 << (30)));
-  return 0;
-}
-
-int flexran_get_ue_transmission_mode(mid_t mod_id, mid_t ue_id) {
-  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
-  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
-  
-  if(ue_context_p != NULL) {
-    if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
-      return ue_context_p->ue_context.physicalConfigDedicated->antennaInfo->choice.explicitValue.transmissionMode;
-    } else {
-      return -1;
-    }
-  } else {
-    return -1;
-  }
-}
-
-int flexran_get_tti_bundling(mid_t mod_id, mid_t ue_id) {
-  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
-  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
-  if(ue_context_p != NULL) {
-    if(ue_context_p->ue_context.mac_MainConfig != NULL){
-      return ue_context_p->ue_context.mac_MainConfig->ul_SCH_Config->ttiBundling;
-    } else {
-      return -1;
-    }
-  }
-  else {
-    return -1;
-  }
-}
-
-int flexran_get_maxHARQ_TX(mid_t mod_id, mid_t ue_id) {
-  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
-  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
-  if(ue_context_p != NULL) {
-    if(ue_context_p->ue_context.mac_MainConfig != NULL){
-      return *ue_context_p->ue_context.mac_MainConfig->ul_SCH_Config->maxHARQ_Tx;
-    }
-  }
-  return -1;
-}
-
-int flexran_get_beta_offset_ack_index(mid_t mod_id, mid_t ue_id) {
-  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
-  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
-  if(ue_context_p != NULL) {
-    if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
-      return ue_context_p->ue_context.physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_ACK_Index;
-    } else {
-      return -1;
-    } 
-  } else {
-    return -1;
-  }
-}
-
-int flexran_get_beta_offset_ri_index(mid_t mod_id, mid_t ue_id) {
-  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
-  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
-  if(ue_context_p != NULL) {
-    if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
-      return ue_context_p->ue_context.physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_RI_Index;
-    } else {
-      return -1;
-    }
-  } else {
-    return -1;
-  }
-}
-
-int flexran_get_beta_offset_cqi_index(mid_t mod_id, mid_t ue_id) {
-  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
-  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
-  if(ue_context_p != NULL) {
-    if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
-      return ue_context_p->ue_context.physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_CQI_Index;
-    } else {
-      return -1;
-    }
-  }
-  else {
-    return -1;
-  }
-}
-
-int flexran_get_simultaneous_ack_nack_cqi(mid_t mod_id, mid_t ue_id) {
-  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
-  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
-  if(ue_context_p != NULL) {
-    if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
-      if (ue_context_p->ue_context.physicalConfigDedicated->cqi_ReportConfig->cqi_ReportPeriodic != NULL) {
-	return ue_context_p->ue_context.physicalConfigDedicated->cqi_ReportConfig->cqi_ReportPeriodic->choice.setup.simultaneousAckNackAndCQI;
-      }
-    }
-  }
-  return -1;
-}
-
-int flexran_get_ack_nack_simultaneous_trans(mid_t mod_id,mid_t ue_id) {
-	return (&eNB_rrc_inst[mod_id])->carrier[0].sib2->radioResourceConfigCommon.soundingRS_UL_ConfigCommon.choice.setup.ackNackSRS_SimultaneousTransmission;
-}
-
-int flexran_get_aperiodic_cqi_rep_mode(mid_t mod_id,mid_t ue_id) {
-  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
-  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
-  
-  if(ue_context_p != NULL) {
-    if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
-      return *ue_context_p->ue_context.physicalConfigDedicated->cqi_ReportConfig->cqi_ReportModeAperiodic;
-    }
-  }
-  return -1;
-}
-
-int flexran_get_tdd_ack_nack_feedback(mid_t mod_id, mid_t ue_id) {
-  // TODO: This needs fixing
-  return -1;
-
-  /* struct rrc_eNB_ue_context_s* ue_context_p = NULL; */
-  /* uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id); */
-  
-  /* ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP); */
-  
-  /* if(ue_context_p != NULL) { */
-  /*   if(ue_context_p->ue_context.physicalConfigDedicated != NULL){ */
-  /*     return ue_context_p->ue_context.physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode; */
-  /*   } else { */
-  /*     return -1; */
-  /*   } */
-  /* } else { */
-  /*   return -1; */
-  /* } */
-}
-
-int flexran_get_ack_nack_repetition_factor(mid_t mod_id, mid_t ue_id) {
-  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
-  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
-  if(ue_context_p != NULL) {
-    if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
-      return ue_context_p->ue_context.physicalConfigDedicated->pucch_ConfigDedicated->ackNackRepetition.choice.setup.repetitionFactor;
-    } else {
-      return -1;
-    }
-  } else {
-    return -1;
-  }
-}
-
-int flexran_get_extended_bsr_size(mid_t mod_id, mid_t ue_id) {
-  //TODO: need to double check
-  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
-  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
-
-  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
-  if(ue_context_p != NULL) {
-    if(ue_context_p->ue_context.mac_MainConfig != NULL){
-      if(ue_context_p->ue_context.mac_MainConfig->ext2 != NULL){
-	long val = (*(ue_context_p->ue_context.mac_MainConfig->ext2->mac_MainConfig_v1020->extendedBSR_Sizes_r10));
-	if (val > 0) {
-	  return 1;
-	}
-      }
-    }
-  }
-  return -1;
-}
-
-int flexran_get_ue_transmission_antenna(mid_t mod_id, mid_t ue_id) {
-  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
-  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
-  
-  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
-  
-  if(ue_context_p != NULL) {
-    if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
-      if(ue_context_p->ue_context.physicalConfigDedicated->antennaInfo->choice.explicitValue.ue_TransmitAntennaSelection.choice.setup == AntennaInfoDedicated__ue_TransmitAntennaSelection__setup_closedLoop) {
-	return 2;
-      } else if(ue_context_p->ue_context.physicalConfigDedicated->antennaInfo->choice.explicitValue.ue_TransmitAntennaSelection.choice.setup == AntennaInfoDedicated__ue_TransmitAntennaSelection__setup_openLoop) {
-	return 1;
-      } else {
-	return 0;
-      }
-    } else {
-      return -1;
-    }
-  } else {
-    return -1;
-  }
-}
-
-int flexran_get_lcg(mid_t ue_id, mid_t lc_id) {
-  if (UE_mac_inst == NULL) {
-    return -1;
-  }
-  if(UE_mac_inst[ue_id].logicalChannelConfig[lc_id] != NULL) {
-    return *UE_mac_inst[ue_id].logicalChannelConfig[lc_id]->ul_SpecificParameters->logicalChannelGroup;
-  } else {
-    return -1;
-  }
-}
-
-int flexran_get_direction(mid_t ue_id, mid_t lc_id) {
-	/*TODO: fill with the value for the rest of LCID*/
-  if(lc_id == DCCH || lc_id == DCCH1) {
-    return 2;
-  } else if(lc_id == DTCH) {
-    return 1;
-  } else {
-    return -1;
-  }
-}
-
-int flexran_agent_ue_state_change(mid_t mod_id, uint32_t rnti, uint8_t state_change) {
-  int size;
-  Protocol__FlexranMessage *msg;
-  Protocol__FlexHeader *header;
-  void *data;
-  int priority = 0;
-
-  int xid = 0;
-
-  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_UE_STATE_CHANGE, &header) != 0)
-    goto error;
-
-  Protocol__FlexUeStateChange *ue_state_change_msg;
-  ue_state_change_msg = malloc(sizeof(Protocol__FlexUeStateChange));
-  if(ue_state_change_msg == NULL) {
-    goto error;
-  }
-  protocol__flex_ue_state_change__init(ue_state_change_msg);
-  ue_state_change_msg->has_type = 1;
-  ue_state_change_msg->type = state_change;
-
-  Protocol__FlexUeConfig *config;
-  config = malloc(sizeof(Protocol__FlexUeConfig));
-  if (config == NULL) {
-    goto error;
-  }
-  protocol__flex_ue_config__init(config);
-  if (state_change == PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_DEACTIVATED) {
-    // Simply set the rnti of the UE
-    config->has_rnti = 1;
-    config->rnti = rnti;
-  } else if (state_change == PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_UPDATED
-	     || state_change == PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_ACTIVATED) {
-	  	  int i = find_UE_id(mod_id, rnti);
-		  config->has_rnti = 1;
-		  config->rnti = rnti;
-	  	  if(flexran_get_time_alignment_timer(mod_id,i) != -1) {
-	  		  config->time_alignment_timer = flexran_get_time_alignment_timer(mod_id,i);
-	  		  config->has_time_alignment_timer = 1;
-	  	  }
-	  	  if(flexran_get_meas_gap_config(mod_id,i) != -1){
-	  		  config->meas_gap_config_pattern = flexran_get_meas_gap_config(mod_id,i);
-	  	  	  config->has_meas_gap_config_pattern = 1;
-	  	  }
-	  	  if(config->has_meas_gap_config_pattern == 1 &&
-		     config->meas_gap_config_pattern != PROTOCOL__FLEX_MEAS_GAP_CONFIG_PATTERN__FLMGCP_OFF) {
-		    config->meas_gap_config_sf_offset = flexran_get_meas_gap_config_offset(mod_id,i);
-		    config->has_meas_gap_config_sf_offset = 1;
-	  	  }
-	  	  //TODO: Set the SPS configuration (Optional)
-	  	  //Not supported for now, so we do not set it
-
-	  	  //TODO: Set the SR configuration (Optional)
-	  	  //We do not set it for now
-
-	  	  //TODO: Set the CQI configuration (Optional)
-	  	  //We do not set it for now
-		  
-		  if(flexran_get_ue_transmission_mode(mod_id,i) != -1) {
-	  		  config->transmission_mode = flexran_get_ue_transmission_mode(mod_id,i);
-	  		  config->has_transmission_mode = 1;
-	  	  }
-
-		  config->ue_aggregated_max_bitrate_ul = flexran_get_ue_aggregated_max_bitrate_ul(mod_id,i);
-	  	  config->has_ue_aggregated_max_bitrate_ul = 1;
-
-		  config->ue_aggregated_max_bitrate_dl = flexran_get_ue_aggregated_max_bitrate_dl(mod_id,i);
-	  	  config->has_ue_aggregated_max_bitrate_dl = 1;
-
-	  	  //TODO: Set the UE capabilities
-	  	  Protocol__FlexUeCapabilities *c_capabilities;
-	  	  c_capabilities = malloc(sizeof(Protocol__FlexUeCapabilities));
-	  	  protocol__flex_ue_capabilities__init(c_capabilities);
-	  	  //TODO: Set half duplex (FDD operation)
-	  	  c_capabilities->has_half_duplex = 0;
-	  	  c_capabilities->half_duplex = 1;//flexran_get_half_duplex(i);
-	  	  //TODO: Set intra-frame hopping flag
-	  	  c_capabilities->has_intra_sf_hopping = 0;
-	  	  c_capabilities->intra_sf_hopping = 1;//flexran_get_intra_sf_hopping(i);
-	  	  //TODO: Set support for type 2 hopping with n_sb > 1
-	  	  c_capabilities->has_type2_sb_1 = 0;
-	  	  c_capabilities->type2_sb_1 = 1;//flexran_get_type2_sb_1(i);
-	  	  //TODO: Set ue category
-	  	  c_capabilities->has_ue_category = 0;
-	  	  c_capabilities->ue_category = 1;//flexran_get_ue_category(i);
-	  	  //TODO: Set UE support for resource allocation type 1
-	  	  c_capabilities->has_res_alloc_type1 = 0;
-	  	  c_capabilities->res_alloc_type1 = 1;//flexran_get_res_alloc_type1(i);
-	  	  //Set the capabilites to the message
-	  	  config->capabilities = c_capabilities;
-		  
-	  	  if(flexran_get_ue_transmission_antenna(mod_id,i) != -1) {
-		    config->has_ue_transmission_antenna = 1;
-		    config->ue_transmission_antenna = flexran_get_ue_transmission_antenna(mod_id,i);
-	  	  }
-
-	  	  if(flexran_get_tti_bundling(mod_id,i) != -1) {
-		    config->has_tti_bundling = 1;
-		    config->tti_bundling = flexran_get_tti_bundling(mod_id,i);
-	  	  }
-
-	  	  if(flexran_get_maxHARQ_TX(mod_id,i) != -1){
-		    config->has_max_harq_tx = 1;
-		    config->max_harq_tx = flexran_get_maxHARQ_TX(mod_id,i);
-	  	  }
-
-	  	  if(flexran_get_beta_offset_ack_index(mod_id,i) != -1) {
-		    config->has_beta_offset_ack_index = 1;
-		    config->beta_offset_ack_index = flexran_get_beta_offset_ack_index(mod_id,i);
-	  	  }
-
-	  	  if(flexran_get_beta_offset_ri_index(mod_id,i) != -1) {
-		    config->has_beta_offset_ri_index = 1;
-		    config->beta_offset_ri_index = flexran_get_beta_offset_ri_index(mod_id,i);
-	  	  }
-
-	  	  if(flexran_get_beta_offset_cqi_index(mod_id,i) != -1) {
-		    config->has_beta_offset_cqi_index = 1;
-		    config->beta_offset_cqi_index = flexran_get_beta_offset_cqi_index(mod_id,i);
-	  	  }
-
-	  	  if(flexran_get_ack_nack_simultaneous_trans(mod_id,i) != -1) {
-		    config->has_ack_nack_simultaneous_trans = 1;
-		    config->ack_nack_simultaneous_trans = flexran_get_ack_nack_simultaneous_trans(mod_id,i);
-	  	  }
-
-	  	  if(flexran_get_simultaneous_ack_nack_cqi(mod_id,i) != -1) {
-		    config->has_simultaneous_ack_nack_cqi = 1;
-		    config->simultaneous_ack_nack_cqi = flexran_get_simultaneous_ack_nack_cqi(mod_id,i);
-	  	  }
-
-	  	  if(flexran_get_aperiodic_cqi_rep_mode(mod_id,i) != -1) {
-		    config->has_aperiodic_cqi_rep_mode = 1;
-		    int mode = flexran_get_aperiodic_cqi_rep_mode(mod_id,i);
-		    if (mode > 4) {
-		      config->aperiodic_cqi_rep_mode = PROTOCOL__FLEX_APERIODIC_CQI_REPORT_MODE__FLACRM_NONE;
-		    } else {
-		      config->aperiodic_cqi_rep_mode = mode;
-		    }
-	  	  }
-
-	  	  if(flexran_get_tdd_ack_nack_feedback(mod_id, i) != -1) {
-		    config->has_tdd_ack_nack_feedback = 1;
-		    config->tdd_ack_nack_feedback = flexran_get_tdd_ack_nack_feedback(mod_id,i);
-	  	  }
-
-	  	  if(flexran_get_ack_nack_repetition_factor(mod_id, i) != -1) {
-		    config->has_ack_nack_repetition_factor = 1;
-		    config->ack_nack_repetition_factor = flexran_get_ack_nack_repetition_factor(mod_id,i);
-	  	  }
-
-	  	  if(flexran_get_extended_bsr_size(mod_id, i) != -1) {
-		    config->has_extended_bsr_size = 1;
-		    config->extended_bsr_size = flexran_get_extended_bsr_size(mod_id,i);
-	  	  }
-
-		  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;
-	  	  if(config->has_ca_support){
-		    //TODO: Set cross carrier scheduling support (boolean)
-		    config->has_cross_carrier_sched_support = 1;
-		    config->cross_carrier_sched_support = 0;
-		    //TODO: Set secondary cells configuration
-		    // We do not set it for now. No carrier aggregation support
-		    
-		    //TODO: Set deactivation timer for secondary cell
-		    config->has_scell_deactivation_timer = 0;
-		    config->scell_deactivation_timer = 0;
-	  	  }
-  } else if (state_change == PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_MOVED) {
-    // TODO: Not supported for now. Leave blank
-  }
-
-  ue_state_change_msg->config = config;
-  msg = malloc(sizeof(Protocol__FlexranMessage));
-  if (msg == NULL) {
-    goto error;
-  }
-  protocol__flexran_message__init(msg);
-  msg->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_UE_STATE_CHANGE_MSG;
-  msg->msg_dir = PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
-  msg->ue_state_change_msg = ue_state_change_msg;
-
-  data = flexran_agent_pack_message(msg, &size);
-  /*Send sr info using the MAC channel of the eNB*/
-  if (flexran_agent_msg_send(mod_id, FLEXRAN_AGENT_DEFAULT, data, size, priority)) {
-    goto error;
-  }
-
-  LOG_D(FLEXRAN_AGENT,"sent message with size %d\n", size);
-  return 0;
- error:
-  LOG_D(FLEXRAN_AGENT, "Could not send UE state message\n");
-  return -1;
-}
-
-
-
 int flexran_agent_lc_config_reply(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
 
   xid_t xid;
+  Protocol__FlexHeader *header = NULL;
   Protocol__FlexranMessage *input = (Protocol__FlexranMessage *)params;
   Protocol__FlexLcConfigRequest *lc_config_request_msg = input->lc_config_request_msg;
   xid = (lc_config_request_msg->header)->xid;
@@ -1536,7 +442,6 @@ int flexran_agent_lc_config_reply(mid_t mod_id, const void *params, Protocol__Fl
     goto error;
   protocol__flex_lc_config_reply__init(lc_config_reply_msg);
 
-  Protocol__FlexHeader *header;
   if(flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_GET_LC_CONFIG_REPLY, &header) != 0)
     goto error;
 
@@ -1562,6 +467,7 @@ int flexran_agent_lc_config_reply(mid_t mod_id, const void *params, Protocol__Fl
       //Set this according to the current state of the UE. This is only a temporary fix
       int status = 0;
       status = mac_eNB_get_rrc_status(mod_id, flexran_get_ue_crnti(mod_id, i));
+      /* TODO needs to be revised and appropriate API to be implemented */
       if (status < RRC_CONNECTED) {
 	lc_ue_config[i]->n_lc_config = 0;
       } else if (status == RRC_CONNECTED) {
@@ -1583,10 +489,10 @@ int flexran_agent_lc_config_reply(mid_t mod_id, const void *params, Protocol__Fl
 	  lc_config[j]->has_lcid = 1;
 	  lc_config[j]->lcid = j+1;
 	 
-	  int lcg = flexran_get_lcg(i, j+1);
+	  int lcg = flexran_get_lcg(mod_id, i, j+1);
 	  if (lcg >= 0 && lcg <= 3) {
 	    lc_config[j]->has_lcg = 1;
-	    lc_config[j]->lcg = flexran_get_lcg(i,j+1);
+	    lc_config[j]->lcg = flexran_get_lcg(mod_id, i,j+1);
 	  }
 	 
 	  lc_config[j]->has_direction = 1;
@@ -1600,6 +506,7 @@ int flexran_agent_lc_config_reply(mid_t mod_id, const void *params, Protocol__Fl
 	  lc_config[j]->has_qci = 1;
 	  lc_config[j]->qci = 1;
 	  if (lc_config[j]->direction == PROTOCOL__FLEX_QOS_BEARER_TYPE__FLQBT_GBR) {
+            /* TODO all of the need to be taken from API */
 	    //TODO: Set the max bitrate (UL)
 	    lc_config[j]->has_e_rab_max_bitrate_ul = 0;
 	    lc_config[j]->e_rab_max_bitrate_ul = 0;
@@ -1650,6 +557,7 @@ int flexran_agent_lc_config_reply(mid_t mod_id, const void *params, Protocol__Fl
 int flexran_agent_ue_config_reply(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
 
   xid_t xid;
+  Protocol__FlexHeader *header = NULL;
   Protocol__FlexranMessage *input = (Protocol__FlexranMessage *)params;
   Protocol__FlexUeConfigRequest *ue_config_request_msg = input->ue_config_request_msg;
   xid = (ue_config_request_msg->header)->xid;
@@ -1662,7 +570,6 @@ int flexran_agent_ue_config_reply(mid_t mod_id, const void *params, Protocol__Fl
     goto error;
   protocol__flex_ue_config_reply__init(ue_config_reply_msg);
 
-  Protocol__FlexHeader *header;
   if(flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_GET_UE_CONFIG_REPLY, &header) != 0)
     goto error;
 
@@ -1682,6 +589,8 @@ int flexran_agent_ue_config_reply(mid_t mod_id, const void *params, Protocol__Fl
 
       ue_config[i]->rnti = flexran_get_ue_crnti(mod_id,i);
       ue_config[i]->has_rnti = 1;
+      ue_config[i]->imsi = flexran_get_ue_imsi(mod_id, i);
+      ue_config[i]->has_imsi = 1;
       //TODO: Set the DRX configuration (optional)
       //Not supported for now, so we do not set it
 
@@ -1723,21 +632,16 @@ int flexran_agent_ue_config_reply(mid_t mod_id, const void *params, Protocol__Fl
       Protocol__FlexUeCapabilities *capabilities;
       capabilities = malloc(sizeof(Protocol__FlexUeCapabilities));
       protocol__flex_ue_capabilities__init(capabilities);
-      //TODO: Set half duplex (FDD operation)
-      capabilities->has_half_duplex = 0;
-      capabilities->half_duplex = 0;//flexran_get_half_duplex(i);
-      //TODO: Set intra-frame hopping flag
-      capabilities->has_intra_sf_hopping = 0;
-      capabilities->intra_sf_hopping = 1;//flexran_get_intra_sf_hopping(i);
-      //TODO: Set support for type 2 hopping with n_sb > 1
-      capabilities->has_type2_sb_1 = 0;
-      capabilities->type2_sb_1 = 1;//flexran_get_type2_sb_1(i);
-      //TODO: Set ue category
-      capabilities->has_ue_category = 0;
-      capabilities->ue_category = 1;//flexran_get_ue_category(i);
-      //TODO: Set UE support for resource allocation type 1
-      capabilities->has_res_alloc_type1 = 0;
-      capabilities->res_alloc_type1 = 1;//flexran_get_res_alloc_type1(i);
+      capabilities->has_half_duplex = 1;
+      capabilities->half_duplex = flexran_get_half_duplex(mod_id, i);
+      capabilities->has_intra_sf_hopping = 1;
+      capabilities->intra_sf_hopping = flexran_get_intra_sf_hopping(mod_id, i);
+      capabilities->has_type2_sb_1 = 1;
+      capabilities->type2_sb_1 = flexran_get_type2_sb_1(mod_id, i);
+      capabilities->has_ue_category = 1;
+      capabilities->ue_category = flexran_get_ue_category(mod_id, i);
+      capabilities->has_res_alloc_type1 = 1;
+      capabilities->res_alloc_type1 = flexran_get_res_alloc_type1(mod_id, i);
       //Set the capabilites to the message
       ue_config[i]->capabilities = capabilities;
 
@@ -1771,9 +675,10 @@ int flexran_agent_ue_config_reply(mid_t mod_id, const void *params, Protocol__Fl
 	ue_config[i]->beta_offset_cqi_index = flexran_get_beta_offset_cqi_index(mod_id,i);
       }
       
-      if (flexran_get_ack_nack_simultaneous_trans(mod_id,i) != -1) {
+      /* assume primary carrier */
+      if (flexran_get_ack_nack_simultaneous_trans(mod_id, i, 0) != -1) {
 	ue_config[i]->has_ack_nack_simultaneous_trans = 1;
-	ue_config[i]->ack_nack_simultaneous_trans = flexran_get_ack_nack_simultaneous_trans(mod_id,i);
+	ue_config[i]->ack_nack_simultaneous_trans = flexran_get_ack_nack_simultaneous_trans(mod_id, i, 0);
       }
       
       if (flexran_get_simultaneous_ack_nack_cqi(mod_id,i) != -1) {
@@ -1791,9 +696,9 @@ int flexran_agent_ue_config_reply(mid_t mod_id, const void *params, Protocol__Fl
 	}
       }
       
-      if (flexran_get_tdd_ack_nack_feedback(mod_id, i) != -1) {
+      if (flexran_get_tdd_ack_nack_feedback_mode(mod_id, i) != -1) {
 	ue_config[i]->has_tdd_ack_nack_feedback = 1;
-	ue_config[i]->tdd_ack_nack_feedback = flexran_get_tdd_ack_nack_feedback(mod_id,i);
+	ue_config[i]->tdd_ack_nack_feedback = flexran_get_tdd_ack_nack_feedback_mode(mod_id,i);
       }
       
       if(flexran_get_ack_nack_repetition_factor(mod_id, i) != -1) {
@@ -1854,7 +759,7 @@ int flexran_agent_ue_config_reply(mid_t mod_id, const void *params, Protocol__Fl
 
 int flexran_agent_enb_config_request(mid_t mod_id, const void* params, Protocol__FlexranMessage **msg) {
 
-	Protocol__FlexHeader *header;
+	Protocol__FlexHeader *header = NULL;
 	xid_t xid = 1;
 
 	Protocol__FlexEnbConfigRequest *enb_config_request_msg;
@@ -1893,6 +798,7 @@ int flexran_agent_enb_config_request(mid_t mod_id, const void* params, Protocol_
 int flexran_agent_enb_config_reply(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
 
   xid_t xid;
+  Protocol__FlexHeader *header = NULL;
   Protocol__FlexranMessage *input = (Protocol__FlexranMessage *)params;
   Protocol__FlexEnbConfigRequest *enb_config_req_msg = input->enb_config_request_msg;
   xid = (enb_config_req_msg->header)->xid;
@@ -1906,7 +812,6 @@ int flexran_agent_enb_config_reply(mid_t mod_id, const void *params, Protocol__F
     goto error;
   protocol__flex_enb_config_reply__init(enb_config_reply_msg);
 
-  Protocol__FlexHeader *header;
   if(flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_GET_ENB_CONFIG_REPLY, &header) != 0)
     goto error;
   
@@ -2105,6 +1010,22 @@ int flexran_agent_enb_config_reply(mid_t mod_id, const void *params, Protocol__F
       cell_conf[i]->srs_mac_up_pts = flexran_get_srs_MaxUpPts(enb_id,i);
       cell_conf[i]->has_srs_mac_up_pts = 1;
 
+      cell_conf[i]->dl_freq = flexran_agent_get_operating_dl_freq (enb_id,i);
+      cell_conf[i]->has_dl_freq = 1;
+
+      cell_conf[i]->ul_freq = flexran_agent_get_operating_ul_freq (enb_id, i);
+      cell_conf[i]->has_ul_freq = 1;
+
+      cell_conf[i]->eutra_band = flexran_agent_get_operating_eutra_band (enb_id,i);
+      cell_conf[i]->has_eutra_band = 1;
+
+      cell_conf[i]->dl_pdsch_power = flexran_agent_get_operating_pdsch_refpower(enb_id, i);
+      cell_conf[i]->has_dl_pdsch_power = 1;
+
+      cell_conf[i]->ul_pusch_power = flexran_agent_get_operating_pusch_p0 (enb_id,i);
+      cell_conf[i]->has_ul_pusch_power = 1;
+ 
+
       if (flexran_get_enable64QAM(enb_id,i) == 0) {
 	cell_conf[i]->enable_64qam = PROTOCOL__FLEX_QAM__FLEQ_MOD_16QAM;
       } else if(flexran_get_enable64QAM(enb_id,i) == 1) {
@@ -2140,191 +1061,39 @@ int flexran_agent_enb_config_reply(mid_t mod_id, const void *params, Protocol__F
 }
 
 
+int flexran_agent_rrc_measurement(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
 
-/*
- * timer primitives
- */
+  protocol_ctxt_t  ctxt;
+
+  Protocol__FlexranMessage *input = (Protocol__FlexranMessage *)params;
+  Protocol__FlexRrcTriggering *triggering = input->rrc_triggering;
 
-//struct flexran_agent_map agent_map;
-flexran_agent_timer_instance_t timer_instance;
-int agent_timer_init = 0;
-err_code_t flexran_agent_init_timer(void){
+  agent_reconf_rrc *reconf_param = malloc(sizeof(agent_reconf_rrc));
   
-  LOG_I(FLEXRAN_AGENT, "init RB tree\n");
-  if (!agent_timer_init) {
-    RB_INIT(&timer_instance.flexran_agent_head);
-    agent_timer_init = 1;
-  }
- 
- return PROTOCOL__FLEXRAN_ERR__NO_ERR;
-}
 
-RB_GENERATE(flexran_agent_map, flexran_agent_timer_element_s, entry, flexran_agent_compare_timer);
+  reconf_param->trigger_policy = triggering->rrc_trigger;
 
-/* The timer_id might not be the best choice for the comparison */
-int flexran_agent_compare_timer(struct flexran_agent_timer_element_s *a, struct flexran_agent_timer_element_s *b){
+  struct rrc_eNB_ue_context_s   *ue_context_p = NULL;
 
-  if (a->timer_id < b->timer_id) return -1;
-  if (a->timer_id > b->timer_id) return 1;
+  RB_FOREACH(ue_context_p, rrc_ue_tree_s, &(RC.rrc[mod_id]->rrc_ue_head)){
 
-  // equal timers
-  return 0;
-}
 
-err_code_t flexran_agent_create_timer(uint32_t interval_sec,
-				      uint32_t interval_usec,
-				      agent_id_t     agent_id,
-				      instance_t     instance,
-				      uint32_t timer_type,
-				      xid_t xid,
-				      flexran_agent_timer_callback_t cb,
-				      void*    timer_args,
-				      long *timer_id){
-  
-  struct flexran_agent_timer_element_s *e = calloc(1, sizeof(*e));
-  DevAssert(e != NULL);
-  
-//uint32_t timer_id;
-  int ret=-1;
+  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, mod_id, ENB_FLAG_YES, ue_context_p->ue_context.rnti, flexran_get_current_frame(mod_id), flexran_get_current_subframe (mod_id), mod_id);
   
-  if ((interval_sec == 0) && (interval_usec == 0 ))
-    return TIMER_NULL;
-  
-  if (timer_type >= FLEXRAN_AGENT_TIMER_TYPE_MAX)
-    return TIMER_TYPE_INVALIDE;
-  
-  if (timer_type  ==   FLEXRAN_AGENT_TIMER_TYPE_ONESHOT){ 
-    ret = timer_setup(interval_sec, 
-		      interval_usec, 
-		      TASK_FLEXRAN_AGENT, 
-		      instance, 
-		      TIMER_ONE_SHOT,
-		      timer_args,
-		      timer_id);
-    
-    e->type = TIMER_ONE_SHOT;
-  }
-  else if (timer_type  ==   FLEXRAN_AGENT_TIMER_TYPE_PERIODIC ){
-    ret = timer_setup(interval_sec, 
-		      interval_usec, 
-		      TASK_FLEXRAN_AGENT, 
-		      instance, 
-		      TIMER_PERIODIC,
-		      timer_args,
-		      timer_id);
-    
-    e->type = TIMER_PERIODIC;
-  }
-  
-  if (ret < 0 ) {
-    return TIMER_SETUP_FAILED; 
-  }
-
-  e->agent_id = agent_id;
-  e->instance = instance;
-  e->state = FLEXRAN_AGENT_TIMER_STATE_ACTIVE;
-  e->timer_id = *timer_id;
-  e->xid = xid;
-  e->timer_args = timer_args; 
-  e->cb = cb;
-  /*element should be a real pointer*/
-  RB_INSERT(flexran_agent_map, &timer_instance.flexran_agent_head, e); 
-  
-  LOG_I(FLEXRAN_AGENT,"Created a new timer with id 0x%lx for agent %d, instance %d \n",
-	e->timer_id, e->agent_id, e->instance);
-  
-  return 0; 
-}
+  flexran_rrc_eNB_generate_defaultRRCConnectionReconfiguration(&ctxt, ue_context_p, 0, reconf_param);  
 
-err_code_t flexran_agent_destroy_timer(long timer_id){
-  
-  struct flexran_agent_timer_element_s *e = get_timer_entry(timer_id);
-
-  if (e != NULL ) {
-    RB_REMOVE(flexran_agent_map, &timer_instance.flexran_agent_head, e);
-    flexran_agent_destroy_flexran_message(e->timer_args->msg);
-    free(e);
   }
   
-  if (timer_remove(timer_id) < 0 ) 
-    goto error;
   
+  *msg = NULL;
   return 0;
-
- error:
-  LOG_E(FLEXRAN_AGENT, "timer can't be removed\n");
-  return TIMER_REMOVED_FAILED ;
-}
-
-err_code_t flexran_agent_destroy_timer_by_task_id(xid_t xid) {
-  struct flexran_agent_timer_element_s *e = NULL;
-  long timer_id;
-  RB_FOREACH(e, flexran_agent_map, &timer_instance.flexran_agent_head) {
-    if (e->xid == xid) {
-      timer_id = e->timer_id;
-      RB_REMOVE(flexran_agent_map, &timer_instance.flexran_agent_head, e);
-      flexran_agent_destroy_flexran_message(e->timer_args->msg);
-      free(e);
-      if (timer_remove(timer_id) < 0 ) { 
-	goto error;
-      }
-    }
-  }
-  return 0;
-
- error:
-  LOG_E(FLEXRAN_AGENT, "timer can't be removed\n");
-  return TIMER_REMOVED_FAILED ;
 }
 
-err_code_t flexran_agent_destroy_timers(void){
-  
-  struct flexran_agent_timer_element_s *e = NULL;
-  
-  RB_FOREACH(e, flexran_agent_map, &timer_instance.flexran_agent_head) {
-    RB_REMOVE(flexran_agent_map, &timer_instance.flexran_agent_head, e);
-    timer_remove(e->timer_id);
-    flexran_agent_destroy_flexran_message(e->timer_args->msg);
-    free(e);
-  }  
 
+int flexran_agent_destroy_rrc_measurement(Protocol__FlexranMessage *msg){
+  // TODO
   return 0;
-
 }
 
-void flexran_agent_sleep_until(struct timespec *ts, int delay) {
-  ts->tv_nsec += delay;
-  if(ts->tv_nsec >= 1000*1000*1000){
-    ts->tv_nsec -= 1000*1000*1000;
-    ts->tv_sec++;
-  }
-  clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, ts,  NULL);
-}
-
-
-err_code_t flexran_agent_stop_timer(long timer_id){
-  
-  struct flexran_agent_timer_element_s *e=NULL;
-  struct flexran_agent_timer_element_s search;
-  memset(&search, 0, sizeof(struct flexran_agent_timer_element_s));
-  search.timer_id = timer_id;
 
-  e = RB_FIND(flexran_agent_map, &timer_instance.flexran_agent_head, &search);
 
-  if (e != NULL ) {
-    e->state =  FLEXRAN_AGENT_TIMER_STATE_STOPPED;
-  }
-  
-  timer_remove(timer_id);
-  
-  return 0;
-}
-
-struct flexran_agent_timer_element_s * get_timer_entry(long timer_id) {
-  
-  struct flexran_agent_timer_element_s search;
-  memset(&search, 0, sizeof(struct flexran_agent_timer_element_s));
-  search.timer_id = timer_id;
-
-  return  RB_FIND(flexran_agent_map, &timer_instance.flexran_agent_head, &search); 
-}
diff --git a/openair2/ENB_APP/flexran_agent_common.h b/openair2/ENB_APP/flexran_agent_common.h
index 7ab89acb4b1e4690b6ea6b1d3249427a9ecd853e..242cb55ca86364db4254f250a6a97657ad73e71d 100644
--- a/openair2/ENB_APP/flexran_agent_common.h
+++ b/openair2/ENB_APP/flexran_agent_common.h
@@ -21,8 +21,8 @@
 
 /*! \file flexran_agent_common.h
  * \brief common message primitves and utilities 
- * \author Xenofon Foukas, Mohamed Kassem and Navid Nikaein
- * \date 2016
+ * \author Xenofon Foukas, Mohamed Kassem and Navid Nikaein and shahab SHARIAT BAGHERI
+ * \date 2017
  * \version 0.1
  */
 
@@ -37,7 +37,8 @@
 #include "flexran.pb-c.h"
 #include "stats_messages.pb-c.h"
 #include "stats_common.pb-c.h"
-
+#include "flexran_agent_ran_api.h"
+#include "flexran_agent_net_comm.h"
 #include "flexran_agent_defs.h"
 #include "enb_config.h"
 
@@ -60,6 +61,19 @@ typedef int (*flexran_agent_message_destruction_callback)(
 	Protocol__FlexranMessage *msg
 );
 
+typedef struct {
+ 
+  uint8_t is_initialized;
+  volatile uint8_t cont_update;
+  xid_t xid;
+  Protocol__FlexranMessage *stats_req;
+  Protocol__FlexranMessage *prev_stats_reply;
+
+  pthread_mutex_t *mutex;
+} stats_updates_context_t;
+
+stats_updates_context_t stats_context[NUM_MAX_ENB];
+
 /**********************************
  * FlexRAN protocol messages helper 
  * functions and generic handlers
@@ -116,10 +130,6 @@ int flexran_agent_destroy_ue_config_request(Protocol__FlexranMessage *msg);
 /* TODO: Need to define and implement destructor */
 int flexran_agent_destroy_lc_config_request(Protocol__FlexranMessage *msg);
 
-/* UE state change message constructor and destructor */
-int flexran_agent_ue_state_change(mid_t mod_id, uint32_t rnti, uint8_t state_change);
-int flexran_agent_destroy_ue_state_change(Protocol__FlexranMessage *msg);
-
 /* Control delegation message constructor and destructor */
 int flexran_agent_control_delegation(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg);
 int flexran_agent_destroy_control_delegation(Protocol__FlexranMessage *msg);
@@ -128,6 +138,11 @@ int flexran_agent_destroy_control_delegation(Protocol__FlexranMessage *msg);
 int flexran_agent_reconfiguration(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg);
 int flexran_agent_destroy_agent_reconfiguration(Protocol__FlexranMessage *msg);
 
+/* rrc triggering measurement message constructor and destructor */
+int flexran_agent_rrc_measurement(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg);
+int flexran_agent_destroy_rrc_measurement(Protocol__FlexranMessage *msg);
+
+
 /* FlexRAN protocol message dispatcher function */
 Protocol__FlexranMessage* flexran_agent_handle_message (mid_t mod_id, 
 						    uint8_t *data, 
@@ -136,367 +151,21 @@ Protocol__FlexranMessage* flexran_agent_handle_message (mid_t mod_id,
 /* Function to be used to send a message to a dispatcher once the appropriate event is triggered. */
 Protocol__FlexranMessage *flexran_agent_handle_timed_task(void *args);
 
+/*Top level Statistics hanlder*/
+int flexran_agent_handle_stats(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg);
 
+/* Function to be used to handle reply message . */
+int flexran_agent_stats_reply(mid_t enb_id, xid_t xid, const report_config_t *report_config, Protocol__FlexranMessage **msg);
 
+/* Top level Statistics request protocol message constructor and destructor */
+int flexran_agent_stats_request(mid_t mod_id, xid_t xid, const stats_request_config_t *report_config, Protocol__FlexranMessage **msg);
+int flexran_agent_destroy_stats_request(Protocol__FlexranMessage *msg);
 
-/****************************
- * get generic info from RAN
- ****************************/
-
-void flexran_set_enb_vars(mid_t mod_id, ran_name_t ran);
-
-int flexran_get_current_time_ms (mid_t mod_id, int subframe_flag);
-
-/*Return the current frame number
- *Could be using implementation specific numbering of frames
- */
-unsigned int flexran_get_current_frame(mid_t mod_id);
-
-/*Return the current SFN (0-1023)*/ 
-unsigned int flexran_get_current_system_frame_num(mid_t mod_id);
-
-unsigned int flexran_get_current_subframe(mid_t mod_id);
-
-/*Return the frame and subframe number in compact 16-bit format.
-  Bits 0-3 subframe, rest for frame. Required by FlexRAN protocol*/
-uint16_t flexran_get_sfn_sf (mid_t mod_id);
-
-/* Return a future frame and subframe number that is ahead_of_time
-   subframes later in compact 16-bit format. Bits 0-3 subframe,
-   rest for frame */
-uint16_t flexran_get_future_sfn_sf(mid_t mod_id, int ahead_of_time);
-
-/* Return the number of attached UEs */
-int flexran_get_num_ues(mid_t mod_id);
-
-/* Get the rnti of a UE with id ue_id */
-int flexran_get_ue_crnti (mid_t mod_id, mid_t ue_id);
-
-/* Get the RLC buffer status report of a ue for a designated
-   logical channel id */
-int flexran_get_ue_bsr (mid_t mod_id, mid_t ue_id, lcid_t lcid);
-
-/* Get power headroom of UE with id ue_id */
-int flexran_get_ue_phr (mid_t mod_id, mid_t ue_id);
-
-/* Get the UE wideband CQI */
-int flexran_get_ue_wcqi (mid_t mod_id, mid_t ue_id);
-
-/* Get the transmission queue size for a UE with a channel_id logical channel id */
-int flexran_get_tx_queue_size(mid_t mod_id, mid_t ue_id, logical_chan_id_t channel_id);
-
-/* Get the head of line delay for a UE with a channel_id logical channel id */
-int flexran_get_hol_delay(mid_t mod_id, mid_t ue_id, logical_chan_id_t channel_id);
-
-/* Check the status of the timing advance for a UE */
-short flexran_get_TA(mid_t mod_id, mid_t ue_id, int CC_id);
-
-/* Update the timing advance status (find out whether a timing advance command is required) */
-void flexran_update_TA(mid_t mod_id, mid_t ue_id, int CC_id);
-
-/* Return timing advance MAC control element for a designated cell and UE */
-int flexran_get_MAC_CE_bitmap_TA(mid_t mod_id, mid_t ue_id, int CC_id);
-
-/* Get the number of active component carriers for a specific UE */
-int flexran_get_active_CC(mid_t mod_id, mid_t ue_id);
-
-/* Get the rank indicator for a designated cell and UE */
-int flexran_get_current_RI(mid_t mod_id, mid_t ue_id, int CC_id);
-
-/* See TS 36.213, section 10.1 */
-int flexran_get_n1pucch_an(mid_t mod_id, int CC_id);
-
-/* See TS 36.211, section 5.4 */
-int flexran_get_nRB_CQI(mid_t mod_id, int CC_id);
-
-/* See TS 36.211, section 5.4 */
-int flexran_get_deltaPUCCH_Shift(mid_t mod_id, int CC_id);
-
-/* See TS 36.211, section 5.7.1 */
-int flexran_get_prach_ConfigIndex(mid_t mod_id, int CC_id);
-
-/* See TS 36.211, section 5.7.1 */
-int flexran_get_prach_FreqOffset(mid_t mod_id, int CC_id);
-
-/* See TS 36.321 */
-int flexran_get_maxHARQ_Msg3Tx(mid_t mod_id, int CC_id);
-
-/* Get the length of the UL cyclic prefix */
-int flexran_get_ul_cyclic_prefix_length(mid_t mod_id, int CC_id);
-
-/* Get the length of the DL cyclic prefix */
-int flexran_get_dl_cyclic_prefix_length(mid_t mod_id, int CC_id);
-
-/* Get the physical cell id of a cell */
-int flexran_get_cell_id(mid_t mod_id, int CC_id);
-
-/* See TS 36.211, section 5.5.3.2 */
-int flexran_get_srs_BandwidthConfig(mid_t mod_id, int CC_id);
-
-/* See TS 36.211, table 5.5.3.3-1 and 2 */
-int flexran_get_srs_SubframeConfig(mid_t mod_id, int CC_id);
-
-/* Boolean value. See TS 36.211,
-   section 5.5.3.2. TDD only */
-int flexran_get_srs_MaxUpPts(mid_t mod_id, int CC_id);
-
-/* Get number of DL resource blocks */
-int flexran_get_N_RB_DL(mid_t mod_id, int CC_id);
-
-/* Get number of UL resource blocks */
-int flexran_get_N_RB_UL(mid_t mod_id, int CC_id);
-
-/* Get number of resource block groups */
-int flexran_get_N_RBG(mid_t mod_id, int CC_id);
-
-/* Get DL/UL subframe assignment. TDD only */
-int flexran_get_subframe_assignment(mid_t mod_id, int CC_id);
-
-/* TDD only. See TS 36.211, table 4.2.1 */
-int flexran_get_special_subframe_assignment(mid_t mod_id, int CC_id);
-
-/* Get the duration of the random access response window in subframes */
-int flexran_get_ra_ResponseWindowSize(mid_t mod_id, int CC_id);
-
-/* Get timer used for random access */
-int flexran_get_mac_ContentionResolutionTimer(mid_t mod_id, int CC_id);
-
-/* Get type of duplex mode (FDD/TDD) */
-int flexran_get_duplex_mode(mid_t mod_id, int CC_id);
-
-/* Get the SI window length */
-long flexran_get_si_window_length(mid_t mod_id, int CC_id);
-
-/* Get the number of PDCCH symbols configured for the cell */
-int flexran_get_num_pdcch_symb(mid_t mod_id, int CC_id);
-
-/* See TS 36.213, sec 5.1.1.1 */
-int flexran_get_tpc(mid_t mod_id, mid_t ue_id);
-
-/* Get the first available HARQ process for a specific cell and UE during 
-   a designated frame and subframe. Returns 0 for success. The id and the 
-   status of the HARQ process are stored in id and status respectively */
-int flexran_get_harq(const mid_t mod_id, const uint8_t CC_id, const mid_t ue_id,
-		     const int frame, const uint8_t subframe, unsigned char *id, unsigned char *round);
-
-/* Uplink power control management*/
-int flexran_get_p0_pucch_dbm(mid_t mod_id, mid_t ue_id, int CC_id);
-
-int flexran_get_p0_nominal_pucch(mid_t mod_id, int CC_id);
-
-int flexran_get_p0_pucch_status(mid_t mod_id, mid_t ue_id, int CC_id);
-
-int flexran_update_p0_pucch(mid_t mod_id, mid_t ue_id, int CC_id);
-
-
-/*
- * ************************************
- * Get Messages for UE Configuration Reply
- * ************************************
- */
-
-/* Get timer in subframes. Controls the synchronization
-   status of the UE, not the actual timing 
-   advance procedure. See TS 36.321 */
-int flexran_get_time_alignment_timer(mid_t mod_id, mid_t ue_id);
-
-/* Get measurement gap configuration. See TS 36.133 */
-int flexran_get_meas_gap_config(mid_t mod_id, mid_t ue_id);
-
-/* Get measurement gap configuration offset if applicable */
-int flexran_get_meas_gap_config_offset(mid_t mod_id, mid_t ue_id);
-
-/* DL aggregated bit-rate of non-gbr bearer
-   per UE. See TS 36.413 */
-int flexran_get_ue_aggregated_max_bitrate_dl (mid_t mod_id, mid_t ue_id);
-
-/* UL aggregated bit-rate of non-gbr bearer
-   per UE. See TS 36.413 */
-int flexran_get_ue_aggregated_max_bitrate_ul (mid_t mod_id, mid_t ue_id);
-
-/* Only half-duplex support. FDD
-   operation. Boolean value */
-int flexran_get_half_duplex(mid_t ue_id);
-
-/* Support of intra-subframe hopping.
-   Boolean value */
-int flexran_get_intra_sf_hopping(mid_t ue_id);
-
-/* UE support for type 2 hopping with
-   n_sb>1 */
-int flexran_get_type2_sb_1(mid_t ue_id);
-
-/* Get the UE category */
-int flexran_get_ue_category(mid_t ue_id);
-
-/* UE support for resource allocation
-   type 1 */
-int flexran_get_res_alloc_type1(mid_t ue_id);
-
-/* Get UE transmission mode */
-int flexran_get_ue_transmission_mode(mid_t mod_id, mid_t ue_id);
-
-/* Boolean value. See TS 36.321 */
-int flexran_get_tti_bundling(mid_t mod_id, mid_t ue_id);
-
-/* The max HARQ retransmission for UL.
-   See TS 36.321 */
-int flexran_get_maxHARQ_TX(mid_t mod_id, mid_t ue_id);
-
-/* See TS 36.213 */
-int flexran_get_beta_offset_ack_index(mid_t mod_id, mid_t ue_id);
-
-/* See TS 36.213 */
-int flexran_get_beta_offset_ri_index(mid_t mod_id, mid_t ue_id);
-
-/* See TS 36.213 */
-int flexran_get_beta_offset_cqi_index(mid_t mod_id, mid_t ue_id);
-
-/* Boolean. See TS36.213, Section 10.1 */
-int flexran_get_simultaneous_ack_nack_cqi(mid_t mod_id, mid_t ue_id);
-
-/* Boolean. See TS 36.213, Section 8.2 */
-int flexran_get_ack_nack_simultaneous_trans(mid_t mod_id,mid_t ue_id);
-
-/* Get aperiodic CQI report mode */
-int flexran_get_aperiodic_cqi_rep_mode(mid_t mod_id,mid_t ue_id);
-
-/* Get ACK/NACK feedback mode. TDD only */
-int flexran_get_tdd_ack_nack_feedback(mid_t mod_id, mid_t ue_id);
-
-/* See TS36.213, section 10.1 */
-int flexran_get_ack_nack_repetition_factor(mid_t mod_id, mid_t ue_id);
-
-/* Boolean. Extended buffer status report size */
-int flexran_get_extended_bsr_size(mid_t mod_id, mid_t ue_id);
-
-/* Get number of UE transmission antennas */
-int flexran_get_ue_transmission_antenna(mid_t mod_id, mid_t ue_id);
-
-/* Get logical channel group of a channel with id lc_id */
-int flexran_get_lcg(mid_t ue_id, mid_t lc_id);
-
-/* Get direction of logical channel with id lc_id */
-int flexran_get_direction(mid_t ue_id, mid_t lc_id);
-
-/*******************
- * timer primitves
- *******************/
-
-#define TIMER_NULL                 -1 
-#define TIMER_TYPE_INVALIDE        -2
-#define	TIMER_SETUP_FAILED         -3
-#define	TIMER_REMOVED_FAILED       -4
-#define	TIMER_ELEMENT_NOT_FOUND    -5
-
-
-/* Type of the callback executed when the timer expired */
-typedef Protocol__FlexranMessage *(*flexran_agent_timer_callback_t)(void*);
-
-typedef enum {
-  /* oneshot timer:  */
-  FLEXRAN_AGENT_TIMER_TYPE_ONESHOT = 0x0,
-
-  /* periodic timer  */
-  FLEXRAN_AGENT_TIMER_TYPE_PERIODIC = 0x1,
-
-  /* Inactive state: initial state for any timer. */
-  FLEXRAN_AGENT_TIMER_TYPE_EVENT_DRIVEN = 0x2,
-  
-  /* Max number of states available */
-  FLEXRAN_AGENT_TIMER_TYPE_MAX,
-} flexran_agent_timer_type_t;
-
-typedef enum {
-  /* Inactive state: initial state for any timer. */
-  FLEXRAN_AGENT_TIMER_STATE_INACTIVE = 0x0,
-
-  /* Inactive state: initial state for any timer. */
-  FLEXRAN_AGENT_TIMER_STATE_ACTIVE = 0x1,
-
-  /* Inactive state: initial state for any timer. */
-  FLEXRAN_AGENT_TIMER_STATE_STOPPED = 0x2,
-  
-  /* Max number of states available */
-  FLEXRAN_AGENT_TIMER_STATE_MAX,
-} flexran_agent_timer_state_t;
-
-typedef struct flexran_agent_timer_args_s{
-  mid_t            mod_id;
-  Protocol__FlexranMessage *msg;
-} flexran_agent_timer_args_t;
-
-
-
-typedef struct flexran_agent_timer_element_s{
-  RB_ENTRY(flexran_agent_timer_element_s) entry;
-
-  agent_id_t             agent_id;
-  instance_t       instance;
-  
-  flexran_agent_timer_type_t  type;
-  flexran_agent_timer_state_t state;
-
-  uint32_t interval_sec;
-  uint32_t interval_usec;
-
-  long timer_id;  /* Timer id returned by the timer API*/
-  xid_t xid; /*The id of the task as received by the controller
-	       message*/
-  
-  flexran_agent_timer_callback_t cb;
-  flexran_agent_timer_args_t *timer_args;
-  
-} flexran_agent_timer_element_t;
-
-typedef struct flexran_agent_timer_instance_s{
-  RB_HEAD(flexran_agent_map, flexran_agent_timer_element_s) flexran_agent_head;
-}flexran_agent_timer_instance_t;
-
-
-err_code_t flexran_agent_init_timer(void);
-
-/* Create a timer for some agent related event with id xid. Will store the id 
-   of the generated timer in timer_id */
-err_code_t flexran_agent_create_timer(uint32_t interval_sec,
-				  uint32_t interval_usec,
-				  agent_id_t     agent_id,
-				  instance_t     instance,
-				  uint32_t timer_type,
-				  xid_t xid,
-				  flexran_agent_timer_callback_t cb,
-				  void*    timer_args,
-				  long *timer_id);
-
-/* Destroy all existing timers */
-err_code_t flexran_agent_destroy_timers(void);
-
-/* Destroy the timer with the given timer_id */
-err_code_t flexran_agent_destroy_timer(long timer_id);
-
-/* Destroy the timer for task with id xid */
-err_code_t flexran_agent_destroy_timer_by_task_id(xid_t xid);
-
-/* Stop a timer */
-err_code_t flexran_agent_stop_timer(long timer_id);
-
-/* Restart the given timer */
-err_code_t flexran_agent_restart_timer(long *timer_id);
-
-/* Find the timer with the given timer_id */
-struct flexran_agent_timer_element_s * get_timer_entry(long timer_id);
-
-/* Obtain the protocol message stored in the given expired timer */
-Protocol__FlexranMessage * flexran_agent_process_timeout(long timer_id, void* timer_args);
-
-/* Comparator function comparing two timers. Decides the ordering of the timers */
-int flexran_agent_compare_timer(struct flexran_agent_timer_element_s *a, struct flexran_agent_timer_element_s *b);
-
-/*Specify a delay in nanoseconds to timespec and sleep until then*/
-void flexran_agent_sleep_until(struct timespec *ts, int delay);
+err_code_t flexran_agent_init_cont_stats_update(mid_t mod_id);
 
-/* RB_PROTOTYPE is for .h files */
-RB_PROTOTYPE(flexran_agent_map, flexran_agent_timer_element_s, entry, flexran_agent_compare_timer);
+void flexran_agent_send_update_stats(mid_t mod_id);
 
+err_code_t flexran_agent_enable_cont_stats_update(mid_t mod_id, xid_t xid, stats_request_config_t *stats_req) ;
+err_code_t flexran_agent_disable_cont_stats_update(mid_t mod_id);
 
 #endif
diff --git a/openair2/ENB_APP/flexran_agent_common_internal.c b/openair2/ENB_APP/flexran_agent_common_internal.c
index 240856b948254f45d558848a0050b1720595b2f2..8874a0f64852f06ac619ba4eed39e7d02fe292ec 100644
--- a/openair2/ENB_APP/flexran_agent_common_internal.c
+++ b/openair2/ENB_APP/flexran_agent_common_internal.c
@@ -21,8 +21,8 @@
 
 /*! \file flexran_agent_common_internal.c
  * \brief internal functions for common message primitves and utilities 
- * \author Xenofon Foukas
- * \date 2016
+ * \author Xenofon Foukas and N. Nikaein
+ * \date 2017
  * \version 0.1
  */
 
@@ -32,6 +32,57 @@
 #include "flexran_agent_common_internal.h"
 #include "flexran_agent_mac_internal.h"
 
+/* needed to soft-restart the lte-softmodem */
+#include "targets/RT/USER/lte-softmodem.h"
+
+void handle_reconfiguration(mid_t mod_id)
+{
+  struct timespec start, end;
+  clock_gettime(CLOCK_MONOTONIC, &start);
+  flexran_agent_info_t *flexran = RC.flexran[mod_id];
+
+  if (ENB_WAIT == flexran->node_ctrl_state) {
+    /* this is already waiting, just release */
+    pthread_mutex_lock(&flexran->mutex_node_ctrl);
+    flexran->node_ctrl_state = ENB_NORMAL_OPERATION;
+    pthread_mutex_unlock(&flexran->mutex_node_ctrl);
+    pthread_cond_signal(&flexran->cond_node_ctrl);
+    return;
+  }
+
+  if (stop_L1L2(mod_id) < 0) {
+    LOG_E(ENB_APP, "can not stop lte-softmodem, aborting restart\n");
+    return;
+  }
+
+  /* node_ctrl_state should have value ENB_MAKE_WAIT only if this method is not
+   * executed by the FlexRAN thread */
+  if (ENB_MAKE_WAIT == flexran->node_ctrl_state) {
+    LOG_I(ENB_APP, " * eNB %d: Waiting for FlexRAN RTController command *\n", mod_id);
+    pthread_mutex_lock(&flexran->mutex_node_ctrl);
+    flexran->node_ctrl_state = ENB_WAIT;
+    while (ENB_NORMAL_OPERATION != flexran->node_ctrl_state)
+      pthread_cond_wait(&flexran->cond_node_ctrl, &flexran->mutex_node_ctrl);
+    pthread_mutex_unlock(&flexran->mutex_node_ctrl);
+  }
+
+  if (restart_L1L2(mod_id) < 0) {
+    LOG_F(ENB_APP, "can not restart, killing lte-softmodem\n");
+    itti_terminate_tasks(TASK_PHY_ENB);
+    return;
+  }
+
+  clock_gettime(CLOCK_MONOTONIC, &end);
+  end.tv_sec -= start.tv_sec;
+  if (end.tv_nsec >= start.tv_nsec) {
+    end.tv_nsec -= start.tv_nsec;
+  } else {
+    end.tv_sec -= 1;
+    end.tv_nsec = end.tv_nsec - start.tv_nsec + 1000000000;
+  }
+  LOG_I(ENB_APP, "lte-softmodem restart succeeded in %ld.%ld s\n", end.tv_sec, end.tv_nsec / 1000000);
+}
+
 int apply_reconfiguration_policy(mid_t mod_id, const char *policy, size_t policy_length) {
 
   yaml_parser_t parser;
@@ -64,7 +115,17 @@ int apply_reconfiguration_policy(mid_t mod_id, const char *policy, size_t policy
       break;
     case YAML_SCALAR_EVENT:
       // Check the system name and call the proper handler
-      if (strcmp((char *) event.data.scalar.value, "mac") == 0) {
+      // Check the system name and call the proper handler
+      if (strcmp((char *) event.data.scalar.value, "enb") == 0) {
+	LOG_I(ENB_APP, "This is intended for the enb system\n");
+	// Call the enb handler
+	if (parse_enb_id(mod_id, &parser) == -1) {
+          LOG_E(ENB_APP, "cannot parse data for eNB\n");
+	  goto error;
+	} else { // succeful parse and setting 
+          handle_reconfiguration(mod_id);
+	}
+      } else if (strcmp((char *) event.data.scalar.value, "mac") == 0) {
 	LOG_D(ENB_APP, "This is intended for the mac system\n");
 	// Call the mac handler
 	if (parse_mac_config(mod_id, &parser) == -1) {
@@ -90,8 +151,8 @@ int apply_reconfiguration_policy(mid_t mod_id, const char *policy, size_t policy
 	// TODO : Just skip it for now
 	if (skip_system_section(&parser) == -1) {
 	  goto error;
-	}
-      } else {
+	} 
+      } else { 
 	goto error;
       }
       break;
@@ -115,6 +176,145 @@ int apply_reconfiguration_policy(mid_t mod_id, const char *policy, size_t policy
 
 }
 
+int parse_enb_id(mid_t mod_id, yaml_parser_t *parser) {
+  yaml_event_t event;
+  
+  char *endptr;
+  // int is_array;
+  
+  int done = 0;
+  int mapping_started = 0;
+
+  while (!done) {
+    
+    if (!yaml_parser_parse(parser, &event))
+      goto error;
+
+    switch (event.type) {
+      // We are expecting a mapping of parameters
+    case YAML_SEQUENCE_START_EVENT:
+      // is_array = 1;
+      break;
+    case YAML_MAPPING_START_EVENT:
+      LOG_D(ENB_APP, "The mapping of the parameters started\n");
+      mapping_started = 1;
+      break;
+    case YAML_MAPPING_END_EVENT:
+      LOG_D(ENB_APP, "The mapping of the parameters ended\n");
+      mapping_started = 0;
+      break;
+    case YAML_SCALAR_EVENT:
+      if (!mapping_started) {
+	goto error;
+      }
+      // Check what key needs to be set
+      // use eNB egistered
+      if (mac_agent_registered[mod_id]) {
+	LOG_I(ENB_APP, "Setting parameter for eNB %s\n", event.data.scalar.value);
+	if (strcmp((char *) event.data.scalar.tag, YAML_INT_TAG) == 0) { // if int 
+	  if ((strtol((char *) event.data.scalar.value, &endptr, 10))== mod_id ) { // enb_id == mod_id: right enb instance to be configured
+	    if (parse_enb_config_parameters(mod_id, parser) == -1) {
+	      goto error;
+	    } 
+	  }
+	  else{
+	    goto error; // not the expected type
+	  }
+	}
+      }
+      break;
+    default:
+      goto error;
+    }
+
+    done = (event.type == YAML_MAPPING_END_EVENT);
+    yaml_event_delete(&event);
+  }
+
+  return 0;
+  
+ error:
+  yaml_event_delete(&event);
+  return -1;
+}
+
+int parse_enb_config_parameters(mid_t mod_id, yaml_parser_t *parser) {
+  yaml_event_t event;
+  
+  char *endptr;
+  
+  int done = 0;
+  int mapping_started = 0;
+
+  while (!done) {
+    
+    if (!yaml_parser_parse(parser, &event))
+      goto error;
+
+    switch (event.type) {
+      // We are expecting a mapping of parameters
+    case YAML_MAPPING_START_EVENT:
+      LOG_D(ENB_APP, "The mapping of the parameters started\n");
+      mapping_started = 1;
+      break;
+    case YAML_MAPPING_END_EVENT:
+      LOG_D(ENB_APP, "The mapping of the parameters ended\n");
+      mapping_started = 0;
+      break;
+    case YAML_SCALAR_EVENT:
+      if (!mapping_started) {
+	goto error;
+      }
+      // Check what key needs to be set
+      if (strcmp((char *) event.data.scalar.value, "dl_freq") == 0) {
+        if (!yaml_parser_parse(parser, &event))
+          goto error;
+	flexran_agent_set_operating_dl_freq(mod_id,
+					    0,
+					    strtol((char *) event.data.scalar.value, &endptr, 10));
+        LOG_I(ENB_APP, "Setting dl_freq to %s\n", event.data.scalar.value);
+      } else if (strcmp((char *) event.data.scalar.value, "ul_freq_offset") == 0) {
+        if (!yaml_parser_parse(parser, &event))
+          goto error;
+        flexran_agent_set_operating_ul_freq(mod_id,
+                                            0,
+                                            strtol((char *) event.data.scalar.value, &endptr, 10));
+        LOG_I(ENB_APP, "Setting ul_freq_offset to %s\n", event.data.scalar.value);
+      } else if (strcmp((char *) event.data.scalar.value, "bandwidth") == 0) {
+        if (!yaml_parser_parse(parser, &event))
+          goto error;
+	flexran_agent_set_operating_bandwidth(mod_id,
+					    0,
+					    strtol((char *) event.data.scalar.value, &endptr, 10));
+        LOG_I(ENB_APP, "Setting bandwidth to %s\n", event.data.scalar.value);
+      } else if (strcmp((char *) event.data.scalar.value, "frame_type") == 0) {
+        if (!yaml_parser_parse(parser, &event))
+          goto error;
+	flexran_agent_set_operating_frame_type (mod_id,
+					    0,
+					    strtol((char *) event.data.scalar.value, &endptr, 10));
+        LOG_I(ENB_APP, "Setting frame_type to %s\n", event.data.scalar.value);
+      }else { // not supported tag  
+        LOG_E(FLEXRAN_AGENT, "Unsupported tag %s\n", event.data.scalar.value);
+	goto error;
+      }
+      
+      break;
+    default:
+      goto error;
+    }
+
+    done = (event.type == YAML_MAPPING_END_EVENT);
+    yaml_event_delete(&event);
+  }
+
+  return 0;
+  
+ error:
+  yaml_event_delete(&event);
+  return -1;
+}
+
 int skip_system_section(yaml_parser_t *parser) {
   yaml_event_t event;
   
diff --git a/openair2/ENB_APP/flexran_agent_common_internal.h b/openair2/ENB_APP/flexran_agent_common_internal.h
index d91c2dc7f6578db33ece11dfeb642ea701c35ae9..bf908ac13232b8743c72411481a8be649d884519 100644
--- a/openair2/ENB_APP/flexran_agent_common_internal.h
+++ b/openair2/ENB_APP/flexran_agent_common_internal.h
@@ -21,8 +21,8 @@
 
 /*! \file flexran_agent_common_internal.h
  * \brief internal agent functions for common message primitves and utilities
- * \author Xenofon Foukas
- * \date 2016
+ * \author Xenofon Foukas and N. Nikaein
+ * \date 2017
  * \version 0.1
  */
 
@@ -37,6 +37,10 @@ int apply_reconfiguration_policy(mid_t mod_id, const char *policy, size_t policy
 
 int apply_parameter_modification(void *parameter, yaml_parser_t *parser);
 
+int parse_enb_id(mid_t mod_id, yaml_parser_t *parser);
+int parse_enb_config_parameters(mid_t mod_id, yaml_parser_t *parser) ;
+
+
 // This can be used when parsing for a specific system that is not yet implmeneted
 // in order to skip its configuration, without affecting the rest
 int skip_system_section(yaml_parser_t *parser);
diff --git a/openair2/ENB_APP/flexran_agent_defs.h b/openair2/ENB_APP/flexran_agent_defs.h
index 12e3c56dffa16ffeb71a8141bf0499e8db585bba..6364bc628191ec7f9c22bdfa98150170b6dbe101 100644
--- a/openair2/ENB_APP/flexran_agent_defs.h
+++ b/openair2/ENB_APP/flexran_agent_defs.h
@@ -21,8 +21,8 @@
 
 /*! \file flexran_agent_defs.h
  * \brief FlexRAN agent common definitions 
- * \author Navid Nikaein and Xenofon Foukas
- * \date 2016
+ * \author Navid Nikaein and Xenofon Foukas and shahab SHARIAT BAGHERI
+ * \date 2017
  * \version 0.1
  */
 #ifndef FLEXRAN_AGENT_DEFS_H_
@@ -32,11 +32,16 @@
 #include <stdlib.h>
 #include <pthread.h>
 #include <string.h>
+#include <stdbool.h>
+#include <time.h>
 
 #include "link_manager.h"
 
 #define NUM_MAX_ENB 2
+#define NUM_MAX_DRB 8
+#define NUM_MAX_SRB 3
 #define NUM_MAX_UE 2048
+#define DEFAULT_DRB 3
 #define DEFAULT_FLEXRAN_AGENT_IPv4_ADDRESS "127.0.0.1"
 #define DEFAULT_FLEXRAN_AGENT_PORT          2210
 #define DEFAULT_FLEXRAN_AGENT_CACHE        "/mnt/oai_agent_cache"
@@ -102,10 +107,66 @@ typedef uint8_t lcid_t;
 typedef int32_t  err_code_t; 
 
 
+/*---------Timer Enums --------- */
+
+typedef enum {
+  /* oneshot timer:  */
+  FLEXRAN_AGENT_TIMER_TYPE_ONESHOT = 0,
+
+  /* periodic timer  */
+  FLEXRAN_AGENT_TIMER_TYPE_PERIODIC = 1,
+
+  /* Inactive state: initial state for any timer. */
+  FLEXRAN_AGENT_TIMER_TYPE_EVENT_DRIVEN = 2,
+  
+  /* Max number of states available */
+  FLEXRAN_AGENT_TIMER_TYPE_MAX,
+} flexran_agent_timer_type_t;
+
+
+typedef enum {
+  /* Inactive state: initial state for any timer. */
+  FLEXRAN_AGENT_TIMER_STATE_INACTIVE = 0x0,
+
+  /* Inactive state: initial state for any timer. */
+  FLEXRAN_AGENT_TIMER_STATE_ACTIVE = 0x1,
+
+  /* Inactive state: initial state for any timer. */
+  FLEXRAN_AGENT_TIMER_STATE_STOPPED = 0x2,
+  
+  /* Max number of states available */
+  FLEXRAN_AGENT_TIMER_STATE_MAX,
+} flexran_agent_timer_state_t;
+
+#define FLEXRAN_CAP_LOL1 0x1
+#define FLEXRAN_CAP_HIL1 0x2
+#define FLEXRAN_CAP_LOL2 0x4   // is: MAC
+#define FLEXRAN_CAP_HIL2 0x8   // is: RLC
+#define FLEXRAN_CAP_PDCP 0x16
+#define FLEXRAN_CAP_RRC  0x32
+
+typedef enum {
+  ENB_NORMAL_OPERATION = 0x0,
+  ENB_WAIT             = 0x1,
+  ENB_MAKE_WAIT        = 0x2,
+} flexran_enb_state_t;
 
 typedef struct {
   /* general info */ 
- 
+  int      enabled;
+  char    *interface_name;
+  char    *remote_ipv4_addr;
+  uint16_t remote_port;
+  char    *cache_name;
+
+  int      enb_id;
+  uint8_t  capability_mask;
+
+  /* lock for waiting before starting or soft-restart */
+  pthread_cond_t      cond_node_ctrl;
+  pthread_mutex_t     mutex_node_ctrl;
+  flexran_enb_state_t node_ctrl_state;
+
   /* stats */
 
   uint32_t total_rx_msg;
@@ -116,10 +177,51 @@ typedef struct {
 
 } flexran_agent_info_t;
 
+
+/*
+rrc triggering
+ */
+
+
 typedef struct {
-  mid_t enb_id;
-  flexran_agent_info_t agent_info;
-  
-} flexran_agent_instance_t;
+   char   * trigger_policy;
+   uint32_t report_interval;
+   uint32_t report_amount;
+
+} agent_reconf_rrc;
+
+
+/* These structs will be used to give
+   instructions for the type of stats reports
+   we need to create */
+
+
+typedef struct {
+  uint16_t ue_rnti;
+  uint32_t ue_report_flags; /* Indicates the report elements
+             required for this UE id. See
+             FlexRAN specification 1.2.4.2 */
+} ue_report_type_t;
+
+typedef struct {
+  uint16_t cc_id;
+  uint32_t cc_report_flags; /* Indicates the report elements
+            required for this CC index. See
+            FlexRAN specification 1.2.4.3 */
+} cc_report_type_t;
+
+typedef struct {
+  int nr_ue;
+  ue_report_type_t *ue_report_type;
+  int nr_cc;
+  cc_report_type_t *cc_report_type;
+} report_config_t;
+
+typedef struct stats_request_config_s{
+  uint8_t report_type;
+  uint8_t report_frequency;
+  uint16_t period; /*In number of subframes*/
+  report_config_t *config;
+} stats_request_config_t;
 
 #endif 
diff --git a/openair2/ENB_APP/flexran_agent_extern.h b/openair2/ENB_APP/flexran_agent_extern.h
index 6237f78367cad368d6115e42f7b6e02cdae88f75..ae77e9227aa960afa13823ff9fe67996253d356a 100644
--- a/openair2/ENB_APP/flexran_agent_extern.h
+++ b/openair2/ENB_APP/flexran_agent_extern.h
@@ -20,9 +20,9 @@
  */ 
 
 /*! \file ENB_APP/extern.h
- * \brief FlexRAN agent - mac interface primitives
- * \author Xenofon Foukas
- * \date 2016
+ * \brief FlexRAN agent - Extern VSF xfaces
+ * \author Xenofon Foukas and shahab SHARIAT BAGHERI
+ * \date 2017
  * \version 0.1
  * \mail x.foukas@sms.ed.ac.uk
  */
@@ -32,12 +32,8 @@
 
 #include "flexran_agent_defs.h"
 #include "flexran_agent_mac_defs.h"
-
-
-//extern msg_context_t shared_ctxt[NUM_MAX_ENB][FLEXRAN_AGENT_MAX];
-
-/* full path of the local cache for storing VSFs */
-extern char local_cache[40];
+#include "flexran_agent_rrc_defs.h"
+#include "flexran_agent_pdcp_defs.h"
 
 /* Control module interface for the communication of the MAC Control Module with the agent */
 extern AGENT_MAC_xface *agent_mac_xface[NUM_MAX_ENB];
@@ -45,8 +41,20 @@ extern AGENT_MAC_xface *agent_mac_xface[NUM_MAX_ENB];
 /* Flag indicating whether the VSFs for the MAC control module have been registered */
 extern unsigned int mac_agent_registered[NUM_MAX_ENB];
 
+/* Control module interface for the communication of the RRC Control Module with the agent */
+extern AGENT_RRC_xface *agent_rrc_xface[NUM_MAX_ENB];
+
+/* Flag indicating whether the VSFs for the RRC control module have been registered */
+extern unsigned int rrc_agent_registered[NUM_MAX_ENB];
+
+/* Control module interface for the communication of the RRC Control Module with the agent */
+extern AGENT_PDCP_xface *agent_pdcp_xface[NUM_MAX_ENB];
+
+/* Flag indicating whether the VSFs for the RRC control module have been registered */
+extern unsigned int pdcp_agent_registered[NUM_MAX_ENB];
+
 /* Requried to know which UEs had a harq updated over some subframe */
-extern int harq_pid_updated[NUMBER_OF_UE_MAX][8];
-extern int harq_pid_round[NUMBER_OF_UE_MAX][8];
+extern int harq_pid_updated[NUM_MAX_UE][8];
+extern int harq_pid_round[NUM_MAX_UE][8];
 
 #endif
diff --git a/openair2/ENB_APP/flexran_agent_handler.c b/openair2/ENB_APP/flexran_agent_handler.c
index 51188bea86216a8ce6640e396d31bdae1046ea45..6c20c635a19365df6e6541306c88b2ab0fbe57c2 100644
--- a/openair2/ENB_APP/flexran_agent_handler.c
+++ b/openair2/ENB_APP/flexran_agent_handler.c
@@ -21,14 +21,18 @@
 
 /*! \file flexran_agent_handler.c
  * \brief FlexRAN agent tx and rx message handler 
- * \author Xenofon Foukas and Navid Nikaein
- * \date 2016
+ * \author Xenofon Foukas and Navid Nikaein and shahab SHARIAT BAGHERI
+ * \date 2017
  * \version 0.1
  */
 
-
+#include "flexran_agent_defs.h"
 #include "flexran_agent_common.h"
 #include "flexran_agent_mac.h"
+#include "flexran_agent_rrc.h"
+#include "flexran_agent_pdcp.h"
+#include "flexran_agent_timer.h"
+#include "flexran_agent_ran_api.h"
 #include "log.h"
 
 #include "assertions.h"
@@ -37,7 +41,7 @@ flexran_agent_message_decoded_callback agent_messages_callback[][3] = {
   {flexran_agent_hello, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_HELLO_MSG*/
   {flexran_agent_echo_reply, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_ECHO_REQUEST_MSG*/
   {0, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_ECHO_REPLY_MSG*/ //Must add handler when receiving echo reply
-  {flexran_agent_mac_handle_stats, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REQUEST_MSG*/
+  {flexran_agent_handle_stats, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REQUEST_MSG*/
   {0, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REPLY_MSG*/
   {0, 0, 0}, /*PROTOCOK__FLEXRAN_MESSAGE__MSG_SF_TRIGGER_MSG*/
   {0, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_UL_SR_INFO_MSG*/
@@ -51,13 +55,14 @@ flexran_agent_message_decoded_callback agent_messages_callback[][3] = {
   {0, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_UE_STATE_CHANGE_MSG*/
   {flexran_agent_control_delegation, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_CONTROL_DELEGATION_MSG*/
   {flexran_agent_reconfiguration, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_AGENT_RECONFIGURATION_MSG*/
+  {flexran_agent_rrc_measurement, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_RRC_TRIGGERING_MSG*/
 };
 
 flexran_agent_message_destruction_callback message_destruction_callback[] = {
   flexran_agent_destroy_hello,
   flexran_agent_destroy_echo_request,
   flexran_agent_destroy_echo_reply,
-  flexran_agent_mac_destroy_stats_request,
+  flexran_agent_destroy_stats_request,
   flexran_agent_mac_destroy_stats_reply,
   flexran_agent_mac_destroy_sf_trigger,
   flexran_agent_mac_destroy_sr_info,
@@ -93,7 +98,6 @@ Protocol__FlexranMessage* flexran_agent_handle_message (mid_t mod_id,
     err_code= PROTOCOL__FLEXRAN_ERR__MSG_DECODING;
     goto error; 
   }
-  
   if ((decoded_message->msg_case > sizeof(agent_messages_callback) / (3 * sizeof(flexran_agent_message_decoded_callback))) || 
       (decoded_message->msg_dir > PROTOCOL__FLEXRAN_DIRECTION__UNSUCCESSFUL_OUTCOME)){
     err_code= PROTOCOL__FLEXRAN_ERR__MSG_NOT_HANDLED;
@@ -139,7 +143,7 @@ void * flexran_agent_pack_message(Protocol__FlexranMessage *msg,
   
   DevAssert(buffer !=NULL);
   
-  LOG_D(FLEXRAN_AGENT,"Serilized the enb mac stats reply (size %d)\n", *size);
+  LOG_D(FLEXRAN_AGENT,"Serilized the eNB-UE stats reply (size %d)\n", *size);
   
   return buffer;
   
@@ -187,3 +191,587 @@ Protocol__FlexranMessage* flexran_agent_process_timeout(long timer_id, void* tim
 err_code_t flexran_agent_destroy_flexran_message(Protocol__FlexranMessage *msg) {
   return ((*message_destruction_callback[msg->msg_case-1])(msg));
 }
+
+
+/* 
+  Top Level Statistics Report
+
+ */
+
+
+
+int flexran_agent_handle_stats(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg){
+
+  // TODO: Must deal with sanitization of input
+  // TODO: Must check if RNTIs and cell ids of the request actually exist
+  // TODO: Must resolve conflicts among stats requests
+
+  int i;
+  err_code_t err_code;
+  xid_t xid;
+  uint32_t usec_interval, sec_interval;
+
+  //TODO: We do not deal with multiple CCs at the moment and eNB id is 0
+  int enb_id = mod_id;
+
+  //eNB_MAC_INST *eNB = &eNB_mac_inst[enb_id];
+  //UE_list_t *eNB_UE_list=  &eNB->UE_list;
+
+  report_config_t report_config;
+
+  uint32_t ue_flags = 0;
+  uint32_t c_flags = 0;
+
+  Protocol__FlexranMessage *input = (Protocol__FlexranMessage *)params;
+
+  Protocol__FlexStatsRequest *stats_req = input->stats_request_msg;
+  xid = (stats_req->header)->xid;
+
+  // Check the type of request that is made
+  switch(stats_req->body_case) {
+  case PROTOCOL__FLEX_STATS_REQUEST__BODY_COMPLETE_STATS_REQUEST: ;
+    Protocol__FlexCompleteStatsRequest *comp_req = stats_req->complete_stats_request;
+    if (comp_req->report_frequency == PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_OFF) {
+      /*Disable both periodic and continuous updates*/
+      // flexran_agent_disable_cont_stats_update(mod_id);
+      flexran_agent_destroy_timer_by_task_id(xid);
+      *msg = NULL;
+      return 0;
+    } else { //One-off, periodical or continuous reporting
+      //Set the proper flags
+      ue_flags = comp_req->ue_report_flags;
+      c_flags = comp_req->cell_report_flags;
+      //Create a list of all eNB RNTIs and cells
+
+      //Set the number of UEs and create list with their RNTIs stats configs
+      report_config.nr_ue = flexran_get_num_ues(mod_id); //eNB_UE_list->num_UEs
+      report_config.ue_report_type = (ue_report_type_t *) malloc(sizeof(ue_report_type_t) * report_config.nr_ue);
+      if (report_config.ue_report_type == NULL) {
+  // TODO: Add appropriate error code
+  err_code = -100;
+  goto error;
+      }
+      for (i = 0; i < report_config.nr_ue; i++) {
+  report_config.ue_report_type[i].ue_rnti = flexran_get_ue_crnti(enb_id, i); //eNB_UE_list->eNB_UE_stats[UE_PCCID(enb_id,i)][i].crnti;
+  report_config.ue_report_type[i].ue_report_flags = ue_flags;
+      }
+      //Set the number of CCs and create a list with the cell stats configs
+      report_config.nr_cc = MAX_NUM_CCs;
+      report_config.cc_report_type = (cc_report_type_t *) malloc(sizeof(cc_report_type_t) * report_config.nr_cc);
+      if (report_config.cc_report_type == NULL) {
+  // TODO: Add appropriate error code
+  err_code = -100;
+  goto error;
+      }
+      for (i = 0; i < report_config.nr_cc; i++) {
+  //TODO: Must fill in the proper cell ids
+  report_config.cc_report_type[i].cc_id = i;
+  report_config.cc_report_type[i].cc_report_flags = c_flags;
+      }
+      /* Check if request was periodical */
+      if (comp_req->report_frequency == PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_PERIODICAL) {
+  /* Create a one off flexran message as an argument for the periodical task */
+  Protocol__FlexranMessage *timer_msg;
+  stats_request_config_t request_config;
+  request_config.report_type = PROTOCOL__FLEX_STATS_TYPE__FLST_COMPLETE_STATS;
+  request_config.report_frequency = PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_ONCE;
+  request_config.period = 0;
+  /* Need to make sure that the ue flags are saved (Bug) */
+  if (report_config.nr_ue == 0) {
+    report_config.nr_ue = 1;
+    report_config.ue_report_type = (ue_report_type_t *) malloc(sizeof(ue_report_type_t));
+     if (report_config.ue_report_type == NULL) {
+       // TODO: Add appropriate error code
+       err_code = -100;
+       goto error;
+     }
+     report_config.ue_report_type[0].ue_rnti = 0; // Dummy value
+     report_config.ue_report_type[0].ue_report_flags = ue_flags;
+  }
+  request_config.config = &report_config;
+  flexran_agent_stats_request(enb_id, xid, &request_config, &timer_msg);
+  /* Create a timer */
+  long timer_id = 0;
+  flexran_agent_timer_args_t *timer_args;
+  timer_args = malloc(sizeof(flexran_agent_timer_args_t));
+  memset (timer_args, 0, sizeof(flexran_agent_timer_args_t));
+  timer_args->mod_id = enb_id;
+  timer_args->msg = timer_msg;
+  /*Convert subframes to usec time*/
+  usec_interval = 1000*comp_req->sf;
+  sec_interval = 0;
+  /*add seconds if required*/
+  if (usec_interval >= 1000*1000) {
+    sec_interval = usec_interval/(1000*1000);
+    usec_interval = usec_interval%(1000*1000);
+  }
+  flexran_agent_create_timer(sec_interval, usec_interval, FLEXRAN_AGENT_DEFAULT, enb_id, FLEXRAN_AGENT_TIMER_TYPE_PERIODIC, xid, flexran_agent_handle_timed_task,(void*) timer_args, &timer_id);
+      } else if (comp_req->report_frequency == PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_CONTINUOUS) {
+  /*If request was for continuous updates, disable the previous configuration and
+    set up a new one*/
+  flexran_agent_disable_cont_stats_update(mod_id);
+  stats_request_config_t request_config;
+  request_config.report_type = PROTOCOL__FLEX_STATS_TYPE__FLST_COMPLETE_STATS;
+  request_config.report_frequency = PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_ONCE;
+  request_config.period = 0;
+  /* Need to make sure that the ue flags are saved (Bug) */
+  if (report_config.nr_ue == 0) {
+    report_config.nr_ue = 1;
+    report_config.ue_report_type = (ue_report_type_t *) malloc(sizeof(ue_report_type_t));
+    if (report_config.ue_report_type == NULL) {
+      // TODO: Add appropriate error code
+      err_code = -100;
+      goto error;
+    }
+    report_config.ue_report_type[0].ue_rnti = 0; // Dummy value
+    report_config.ue_report_type[0].ue_report_flags = ue_flags;
+  }
+  request_config.config = &report_config;
+  flexran_agent_enable_cont_stats_update(enb_id, xid, &request_config);
+      }
+    }
+    break;
+  case PROTOCOL__FLEX_STATS_REQUEST__BODY_CELL_STATS_REQUEST:;
+    Protocol__FlexCellStatsRequest *cell_req = stats_req->cell_stats_request;
+    // UE report config will be blank
+    report_config.nr_ue = 0;
+    report_config.ue_report_type = NULL;
+    report_config.nr_cc = cell_req->n_cell;
+    report_config.cc_report_type = (cc_report_type_t *) malloc(sizeof(cc_report_type_t) * report_config.nr_cc);
+    if (report_config.cc_report_type == NULL) {
+      // TODO: Add appropriate error code
+      err_code = -100;
+      goto error;
+    }
+    for (i = 0; i < report_config.nr_cc; i++) {
+  //TODO: Must fill in the proper cell ids
+      report_config.cc_report_type[i].cc_id = cell_req->cell[i];
+      report_config.cc_report_type[i].cc_report_flags = cell_req->flags;
+    }
+    break;
+  case PROTOCOL__FLEX_STATS_REQUEST__BODY_UE_STATS_REQUEST:;
+    Protocol__FlexUeStatsRequest *ue_req = stats_req->ue_stats_request;
+    // Cell report config will be blank
+    report_config.nr_cc = 0;
+    report_config.cc_report_type = NULL;
+    report_config.nr_ue = ue_req->n_rnti;
+    report_config.ue_report_type = (ue_report_type_t *) malloc(sizeof(ue_report_type_t) * report_config.nr_ue);
+    if (report_config.ue_report_type == NULL) {
+      // TODO: Add appropriate error code
+      err_code = -100;
+      goto error;
+    }
+    for (i = 0; i < report_config.nr_ue; i++) {
+      report_config.ue_report_type[i].ue_rnti = ue_req->rnti[i];
+      report_config.ue_report_type[i].ue_report_flags = ue_req->flags;
+    }
+    break;
+  default:
+    //TODO: Add appropriate error code
+    err_code = -100;
+    goto error;
+  }
+
+   if (flexran_agent_stats_reply(enb_id, xid, &report_config, msg )){  
+      err_code = PROTOCOL__FLEXRAN_ERR__MSG_BUILD;
+      goto error;
+    }
+
+  free(report_config.ue_report_type);
+  free(report_config.cc_report_type);
+
+  return 0;
+
+ error :
+  LOG_E(FLEXRAN_AGENT, "errno %d occured\n", err_code);
+  return err_code;
+}
+
+/*
+  Top level reply 
+ */
+
+int flexran_agent_stats_reply(mid_t enb_id, xid_t xid, const report_config_t *report_config, Protocol__FlexranMessage **msg){
+
+  Protocol__FlexHeader *header = NULL;
+  err_code_t err_code;
+  int i;
+
+  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_STATS_REPLY, &header) != 0)
+    goto error;
+
+  
+  Protocol__FlexStatsReply *stats_reply_msg;
+
+  stats_reply_msg = malloc(sizeof(Protocol__FlexStatsReply));
+
+  if (stats_reply_msg == NULL)
+    goto error;
+
+  protocol__flex_stats_reply__init(stats_reply_msg);
+  stats_reply_msg->header = header;
+
+  stats_reply_msg->n_ue_report = report_config->nr_ue;
+  stats_reply_msg->n_cell_report = report_config->nr_cc;
+
+  // UE report
+
+  Protocol__FlexUeStatsReport **ue_report;
+  
+
+  ue_report = malloc(sizeof(Protocol__FlexUeStatsReport *) * report_config->nr_ue);
+          if (ue_report == NULL)
+            goto error;
+  
+  for (i = 0; i < report_config->nr_ue; i++) {
+
+      ue_report[i] = malloc(sizeof(Protocol__FlexUeStatsReport));
+      protocol__flex_ue_stats_report__init(ue_report[i]);
+      ue_report[i]->rnti = report_config->ue_report_type[i].ue_rnti;
+      ue_report[i]->has_rnti = 1;
+      ue_report[i]->flags = report_config->ue_report_type[i].ue_report_flags;
+      ue_report[i]->has_flags = 1;
+  
+  }
+
+  // cell rpoert 
+
+  Protocol__FlexCellStatsReport **cell_report;
+
+  
+  cell_report = malloc(sizeof(Protocol__FlexCellStatsReport *) * report_config->nr_cc);
+  if (cell_report == NULL)
+    goto error;
+  
+  for (i = 0; i < report_config->nr_cc; i++) {
+
+      cell_report[i] = malloc(sizeof(Protocol__FlexCellStatsReport));
+      if(cell_report[i] == NULL)
+          goto error;
+
+      protocol__flex_cell_stats_report__init(cell_report[i]);
+      cell_report[i]->carrier_index = report_config->cc_report_type[i].cc_id;
+      cell_report[i]->has_carrier_index = 1;
+      cell_report[i]->flags = report_config->cc_report_type[i].cc_report_flags;
+      cell_report[i]->has_flags = 1;
+
+  }
+
+      /*
+      MAC reply split
+     */
+
+
+    if (flexran_agent_mac_stats_reply(enb_id, report_config,  ue_report, cell_report) < 0 ) {
+        err_code = PROTOCOL__FLEXRAN_ERR__MSG_BUILD;
+        goto error;
+    }
+
+    /*
+      RRC reply split
+     */
+
+    if (flexran_agent_rrc_stats_reply(enb_id, report_config,  ue_report, cell_report) < 0 ) {
+        err_code = PROTOCOL__FLEXRAN_ERR__MSG_BUILD;
+        goto error;
+    }
+   
+
+    /*
+      PDCP reply split
+    */
+    
+    if (flexran_agent_pdcp_stats_reply(enb_id, report_config,  ue_report, cell_report) < 0 ) {
+      err_code = PROTOCOL__FLEXRAN_ERR__MSG_BUILD;
+      goto error;
+    }
+
+       
+  stats_reply_msg->cell_report = cell_report;
+  stats_reply_msg->ue_report = ue_report;
+
+ *msg = malloc(sizeof(Protocol__FlexranMessage));
+  if(*msg == NULL)
+    goto error;
+  protocol__flexran_message__init(*msg);
+  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REPLY_MSG;
+  (*msg)->msg_dir =  PROTOCOL__FLEXRAN_DIRECTION__SUCCESSFUL_OUTCOME;
+  (*msg)->stats_reply_msg = stats_reply_msg;
+
+  return 0;
+
+error :
+  LOG_E(FLEXRAN_AGENT, "errno %d occured\n", err_code);
+  return err_code;
+
+}
+
+/*
+  Top Level Request 
+ */
+
+int flexran_agent_stats_request(mid_t mod_id,
+            xid_t xid,
+            const stats_request_config_t *report_config,
+            Protocol__FlexranMessage **msg) {
+  Protocol__FlexHeader *header = NULL;
+  int i;
+
+  Protocol__FlexStatsRequest *stats_request_msg;
+  stats_request_msg = malloc(sizeof(Protocol__FlexStatsRequest));
+  if(stats_request_msg == NULL)
+    goto error;
+  protocol__flex_stats_request__init(stats_request_msg);
+
+  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_STATS_REQUEST, &header) != 0)
+    goto error;
+
+  stats_request_msg->header = header;
+
+  stats_request_msg->type = report_config->report_type;
+  stats_request_msg->has_type = 1;
+
+  switch (report_config->report_type) {
+  case PROTOCOL__FLEX_STATS_TYPE__FLST_COMPLETE_STATS:
+    stats_request_msg->body_case =  PROTOCOL__FLEX_STATS_REQUEST__BODY_COMPLETE_STATS_REQUEST;
+    Protocol__FlexCompleteStatsRequest *complete_stats;
+    complete_stats = malloc(sizeof(Protocol__FlexCompleteStatsRequest));
+    if(complete_stats == NULL)
+      goto error;
+    protocol__flex_complete_stats_request__init(complete_stats);
+    complete_stats->report_frequency = report_config->report_frequency;
+    complete_stats->has_report_frequency = 1;
+    complete_stats->sf = report_config->period;
+    complete_stats->has_sf = 1;
+    complete_stats->has_cell_report_flags = 1;
+    complete_stats->has_ue_report_flags = 1;
+    if (report_config->config->nr_cc > 0) {
+      complete_stats->cell_report_flags = report_config->config->cc_report_type[0].cc_report_flags;
+    }
+    if (report_config->config->nr_ue > 0) {
+      complete_stats->ue_report_flags = report_config->config->ue_report_type[0].ue_report_flags;
+    }
+    stats_request_msg->complete_stats_request = complete_stats;
+    break;
+  case  PROTOCOL__FLEX_STATS_TYPE__FLST_CELL_STATS:
+    stats_request_msg->body_case = PROTOCOL__FLEX_STATS_REQUEST__BODY_CELL_STATS_REQUEST;
+     Protocol__FlexCellStatsRequest *cell_stats;
+     cell_stats = malloc(sizeof(Protocol__FlexCellStatsRequest));
+    if(cell_stats == NULL)
+      goto error;
+    protocol__flex_cell_stats_request__init(cell_stats);
+    cell_stats->n_cell = report_config->config->nr_cc;
+    cell_stats->has_flags = 1;
+    if (cell_stats->n_cell > 0) {
+      uint32_t *cells;
+      cells = (uint32_t *) malloc(sizeof(uint32_t)*cell_stats->n_cell);
+      for (i = 0; i < cell_stats->n_cell; i++) {
+  cells[i] = report_config->config->cc_report_type[i].cc_id;
+      }
+      cell_stats->cell = cells;
+      cell_stats->flags = report_config->config->cc_report_type[i].cc_report_flags;
+    }
+    stats_request_msg->cell_stats_request = cell_stats;
+    break;
+  case PROTOCOL__FLEX_STATS_TYPE__FLST_UE_STATS:
+    stats_request_msg->body_case = PROTOCOL__FLEX_STATS_REQUEST__BODY_UE_STATS_REQUEST;
+     Protocol__FlexUeStatsRequest *ue_stats;
+     ue_stats = malloc(sizeof(Protocol__FlexUeStatsRequest));
+    if(ue_stats == NULL)
+      goto error;
+    protocol__flex_ue_stats_request__init(ue_stats);
+    ue_stats->n_rnti = report_config->config->nr_ue;
+    ue_stats->has_flags = 1;
+    if (ue_stats->n_rnti > 0) {
+      uint32_t *ues;
+      ues = (uint32_t *) malloc(sizeof(uint32_t)*ue_stats->n_rnti);
+      for (i = 0; i < ue_stats->n_rnti; i++) {
+  ues[i] = report_config->config->ue_report_type[i].ue_rnti;
+      }
+      ue_stats->rnti = ues;
+      ue_stats->flags = report_config->config->ue_report_type[i].ue_report_flags;
+    }
+    stats_request_msg->ue_stats_request = ue_stats;
+    break;
+  default:
+    goto error;
+  }
+  *msg = malloc(sizeof(Protocol__FlexranMessage));
+  if(*msg == NULL)
+    goto error;
+  protocol__flexran_message__init(*msg);
+  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REQUEST_MSG;
+  (*msg)->msg_dir = PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
+  (*msg)->stats_request_msg = stats_request_msg;
+  return 0;
+
+ error:
+  // TODO: Need to make proper error handling
+  if (header != NULL)
+    free(header);
+  if (stats_request_msg != NULL)
+    free(stats_request_msg);
+  if(*msg != NULL)
+    free(*msg);
+  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
+  return -1;
+}
+
+int flexran_agent_destroy_stats_request(Protocol__FlexranMessage *msg) {
+   if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REQUEST_MSG)
+    goto error;
+  free(msg->stats_request_msg->header);
+  if (msg->stats_request_msg->body_case == PROTOCOL__FLEX_STATS_REQUEST__BODY_CELL_STATS_REQUEST) {
+    free(msg->stats_request_msg->cell_stats_request->cell);
+  }
+  if (msg->stats_request_msg->body_case == PROTOCOL__FLEX_STATS_REQUEST__BODY_UE_STATS_REQUEST) {
+    free(msg->stats_request_msg->ue_stats_request->rnti);
+  }
+  free(msg->stats_request_msg);
+  free(msg);
+  return 0;
+
+ error:
+  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
+  return -1;
+}
+
+/*
+  Top Level Update 
+ */
+
+void flexran_agent_send_update_stats(mid_t mod_id) {
+
+  Protocol__FlexranMessage *current_report = NULL;
+  void *data;
+  int size;
+  err_code_t err_code;
+  int priority = 0;
+  
+  if (pthread_mutex_lock(stats_context[mod_id].mutex)) {
+    goto error;
+  }
+
+  if (stats_context[mod_id].cont_update == 1) {
+  
+    /*Create a fresh report with the required flags*/
+    err_code = flexran_agent_handle_stats(mod_id, (void *) stats_context[mod_id].stats_req, &current_report);
+    if (err_code < 0) {
+      goto error;
+    }
+  }
+  /* /\*TODO:Check if a previous reports exists and if yes, generate a report */
+  /*  *that is the diff between the old and the new report, */
+  /*  *respecting the thresholds. Otherwise send the new report*\/ */
+  /* if (stats_context[mod_id].prev_stats_reply != NULL) { */
+
+  /*   msg = flexran_agent_generate_diff_mac_stats_report(current_report, stats_context[mod_id].prev_stats_reply); */
+
+  /*   /\*Destroy the old stats*\/ */
+  /*    flexran_agent_destroy_flexran_message(stats_context[mod_id].prev_stats_reply); */
+  /* } */
+  /* /\*Use the current report for future comparissons*\/ */
+  /* stats_context[mod_id].prev_stats_reply = current_report; */
+
+
+  if (pthread_mutex_unlock(stats_context[mod_id].mutex)) {
+    goto error;
+  }
+
+  if (current_report != NULL){
+    data=flexran_agent_pack_message(current_report, &size);
+    /*Send any stats updates using the MAC channel of the eNB*/
+    if (flexran_agent_msg_send(mod_id, FLEXRAN_AGENT_MAC, data, size, priority)) {
+      err_code = PROTOCOL__FLEXRAN_ERR__MSG_ENQUEUING;
+      goto error;
+    }
+
+    LOG_D(FLEXRAN_AGENT,"sent message with size %d\n", size);
+    return;
+  }
+ error:
+  LOG_D(FLEXRAN_AGENT, "Could not send sf trigger message\n");
+}
+
+err_code_t flexran_agent_disable_cont_stats_update(mid_t mod_id) {
+  /*Disable the continuous updates for the MAC*/
+  if (pthread_mutex_lock(stats_context[mod_id].mutex)) {
+    goto error;
+  }
+  stats_context[mod_id].cont_update = 0;
+  stats_context[mod_id].xid = 0;
+  if (stats_context[mod_id].stats_req != NULL) {
+    flexran_agent_destroy_flexran_message(stats_context[mod_id].stats_req);
+  }
+  if (stats_context[mod_id].prev_stats_reply != NULL) {
+    flexran_agent_destroy_flexran_message(stats_context[mod_id].prev_stats_reply);
+  }
+  if (pthread_mutex_unlock(stats_context[mod_id].mutex)) {
+    goto error;
+  }
+  return 0;
+
+ error:
+  LOG_E(FLEXRAN_AGENT, "stats_context for eNB %d is not initialized\n", mod_id);
+  return -1;
+
+}
+
+err_code_t flexran_agent_enable_cont_stats_update(mid_t mod_id,
+                  xid_t xid, stats_request_config_t *stats_req) {
+  
+  if (pthread_mutex_lock(stats_context[mod_id].mutex)) {
+    goto error;
+  }
+
+  Protocol__FlexranMessage *req_msg;
+
+  flexran_agent_stats_request(mod_id, xid, stats_req, &req_msg);
+  stats_context[mod_id].stats_req = req_msg;
+  stats_context[mod_id].prev_stats_reply = NULL;
+
+  stats_context[mod_id].cont_update = 1;
+  stats_context[mod_id].xid = xid;
+
+  if (pthread_mutex_unlock(stats_context[mod_id].mutex)) {
+    goto error;
+  }
+  return 0;
+
+ error:
+  LOG_E(FLEXRAN_AGENT, "stats_context for eNB %d is not initialized\n", mod_id);
+  return -1;
+}
+
+
+err_code_t flexran_agent_init_cont_stats_update(mid_t mod_id) {
+
+  
+  /*Initially the continuous update is set to false*/
+  stats_context[mod_id].cont_update = 0;
+  stats_context[mod_id].is_initialized = 1;
+  stats_context[mod_id].stats_req = NULL;
+  stats_context[mod_id].prev_stats_reply = NULL;
+  stats_context[mod_id].mutex = calloc(1, sizeof(pthread_mutex_t));
+  if (stats_context[mod_id].mutex == NULL)
+    goto error;
+  if (pthread_mutex_init(stats_context[mod_id].mutex, NULL))
+    goto error;
+
+  return 0;
+
+ error:
+  return -1;
+}
+
+err_code_t flexran_agent_destroy_cont_stats_update(mid_t mod_id) {
+  
+  stats_context[mod_id].cont_update = 0;
+  stats_context[mod_id].is_initialized = 0;
+  flexran_agent_destroy_flexran_message(stats_context[mod_id].stats_req);
+  flexran_agent_destroy_flexran_message(stats_context[mod_id].prev_stats_reply);
+  free(stats_context[mod_id].mutex);
+
+  // mac_agent_registered[mod_id] = 0;
+  return 1;
+}
diff --git a/openair2/ENB_APP/flexran_agent_net_comm.c b/openair2/ENB_APP/flexran_agent_net_comm.c
index e6442512095963885e6361f7a3c7595d7dd0b9b9..6971c2975048fae72be383fa2a9ab38d00fe7579 100644
--- a/openair2/ENB_APP/flexran_agent_net_comm.c
+++ b/openair2/ENB_APP/flexran_agent_net_comm.c
@@ -118,7 +118,7 @@ int flexran_agent_create_channel(void *channel_info,
   /*element should be a real pointer*/
   RB_INSERT(flexran_agent_channel_map, &channel_instance.flexran_agent_head, channel); 
   
-  LOG_I(FLEXRAN_AGENT,"Created a new channel with id 0x%lx\n", channel->channel_id);
+  LOG_I(FLEXRAN_AGENT,"Created a new channel with id %d \n", channel->channel_id);
  
   return channel_id; 
 }
@@ -141,9 +141,9 @@ int flexran_agent_destroy_channel(int channel_id) {
   for (i = 0; i < NUM_MAX_ENB; i++) {
     for (j = 0; j < FLEXRAN_AGENT_MAX; j++) {
       if (agent_channel[i][j] != NULL) {
-	if (agent_channel[i][j]->channel_id == e->channel_id) {
-	  agent_channel[i][j] == NULL;
-	}
+        if (agent_channel[i][j]->channel_id == e->channel_id) {
+            free(agent_channel[i][j]);
+        }
       }
     }
   }
@@ -164,7 +164,9 @@ err_code_t flexran_agent_init_channel_container(void) {
   
   for (i = 0; i < NUM_MAX_ENB; i++) {
     for (j = 0; j < FLEXRAN_AGENT_MAX; j++) {
-    agent_channel[i][j] == NULL;
+      agent_channel[i][j] = malloc(sizeof(flexran_agent_channel_t));
+      if (!agent_channel[i][j])
+        return -1;
     }
   }
 
diff --git a/openair2/ENB_APP/flexran_agent_ran_api.c b/openair2/ENB_APP/flexran_agent_ran_api.c
new file mode 100644
index 0000000000000000000000000000000000000000..c1743bf744f821df542c8029928dbf1c6d54b22f
--- /dev/null
+++ b/openair2/ENB_APP/flexran_agent_ran_api.c
@@ -0,0 +1,1250 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */ 
+
+/*! \file flexran_agent_ran_api.c
+ * \brief FlexRAN RAN API abstraction 
+ * \author N. Nikaein, X. Foukas, S. SHARIAT BAGHERI and R. Schmidt
+ * \date 2017
+ * \version 0.1
+ */
+
+#include "flexran_agent_ran_api.h"
+
+static inline int phy_is_present(mid_t mod_id, uint8_t cc_id)
+{
+  return RC.eNB && RC.eNB[mod_id] && RC.eNB[mod_id][cc_id];
+}
+
+static inline int mac_is_present(mid_t mod_id)
+{
+  return RC.mac && RC.mac[mod_id];
+}
+
+static inline int rrc_is_present(mid_t mod_id)
+{
+  return RC.rrc && RC.rrc[mod_id];
+}
+
+uint32_t flexran_get_current_time_ms(mid_t mod_id, int subframe_flag)
+{
+  if (!mac_is_present(mod_id)) return 0;
+  if (subframe_flag == 1)
+    return RC.mac[mod_id]->frame*10 + RC.mac[mod_id]->subframe;
+  else
+    return RC.mac[mod_id]->frame*10;
+}
+
+frame_t flexran_get_current_frame(mid_t mod_id)
+{
+  if (!mac_is_present(mod_id)) return 0;
+  //  #warning "SFN will not be in [0-1023] when oaisim is used"
+  return RC.mac[mod_id]->frame;
+}
+
+frame_t flexran_get_current_system_frame_num(mid_t mod_id)
+{
+  return flexran_get_current_frame(mod_id) % 1024;
+}
+
+sub_frame_t flexran_get_current_subframe(mid_t mod_id)
+{
+  if (!mac_is_present(mod_id)) return 0;
+  return RC.mac[mod_id]->subframe;
+}
+
+/* Why uint16_t, frame_t and sub_frame_t are defined as uint32_t? */
+uint16_t flexran_get_sfn_sf(mid_t mod_id)
+{
+  frame_t frame = flexran_get_current_system_frame_num(mod_id);
+  sub_frame_t subframe = flexran_get_current_subframe(mod_id);
+  uint16_t sfn_sf, frame_mask, sf_mask;
+
+  frame_mask = (1 << 12) - 1;
+  sf_mask = (1 << 4) - 1;
+  sfn_sf = (subframe & sf_mask) | ((frame & frame_mask) << 4);
+
+  return sfn_sf;
+}
+
+uint16_t flexran_get_future_sfn_sf(mid_t mod_id, int ahead_of_time)
+{
+  frame_t frame = flexran_get_current_system_frame_num(mod_id);
+  sub_frame_t subframe = flexran_get_current_subframe(mod_id);
+  uint16_t sfn_sf, frame_mask, sf_mask;
+  int additional_frames;
+
+  subframe = (subframe + ahead_of_time) % 10;
+
+  if (subframe < flexran_get_current_subframe(mod_id))
+    frame = (frame + 1) % 1024;
+
+  additional_frames = ahead_of_time / 10;
+  frame = (frame + additional_frames) % 1024;
+
+  frame_mask = (1 << 12) - 1;
+  sf_mask = (1 << 4) - 1;
+  sfn_sf = (subframe & sf_mask) | ((frame & frame_mask) << 4);
+
+  return sfn_sf;
+}
+
+int flexran_get_num_ues(mid_t mod_id)
+{
+  if (!mac_is_present(mod_id)) return 0;
+  return RC.mac[mod_id]->UE_list.num_UEs;
+}
+
+rnti_t flexran_get_ue_crnti(mid_t mod_id, mid_t ue_id)
+{
+  return UE_RNTI(mod_id, ue_id);
+}
+
+int flexran_get_ue_bsr_ul_buffer_info(mid_t mod_id, mid_t ue_id, lcid_t lcid)
+{
+  if (!mac_is_present(mod_id)) return -1;
+  return RC.mac[mod_id]->UE_list.UE_template[UE_PCCID(mod_id, ue_id)][ue_id].ul_buffer_info[lcid];
+}
+
+int8_t flexran_get_ue_phr(mid_t mod_id, mid_t ue_id)
+{
+  if (!mac_is_present(mod_id)) return 0;
+  return RC.mac[mod_id]->UE_list.UE_template[UE_PCCID(mod_id, ue_id)][ue_id].phr_info;
+}
+
+uint8_t flexran_get_ue_wcqi(mid_t mod_id, mid_t ue_id)
+{
+  if (!phy_is_present(mod_id, 0)) return 0;
+  return RC.eNB[mod_id][0]->UE_stats[ue_id].DL_cqi[0];
+}
+
+rlc_buffer_occupancy_t flexran_get_tx_queue_size(mid_t mod_id, mid_t ue_id, logical_chan_id_t channel_id)
+{
+  rnti_t rnti = flexran_get_ue_crnti(mod_id, ue_id);
+  frame_t frame = flexran_get_current_frame(mod_id);
+  sub_frame_t subframe = flexran_get_current_subframe(mod_id);
+  mac_rlc_status_resp_t rlc_status = mac_rlc_status_ind(mod_id,rnti, mod_id, frame, subframe, ENB_FLAG_YES,MBMS_FLAG_NO, channel_id, 0);
+  return rlc_status.bytes_in_buffer;
+}
+
+rlc_buffer_occupancy_t flexran_get_num_pdus_buffer(mid_t mod_id, mid_t ue_id, logical_chan_id_t channel_id)
+{
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  frame_t frame = flexran_get_current_frame(mod_id);
+  sub_frame_t subframe = flexran_get_current_subframe(mod_id);
+  mac_rlc_status_resp_t rlc_status = mac_rlc_status_ind(mod_id,rnti, mod_id, frame, subframe, ENB_FLAG_YES,MBMS_FLAG_NO, channel_id, 0);
+  return rlc_status.pdus_in_buffer;
+}
+
+frame_t flexran_get_hol_delay(mid_t mod_id, mid_t ue_id, logical_chan_id_t channel_id)
+{
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  frame_t frame = flexran_get_current_frame(mod_id);
+  sub_frame_t subframe = flexran_get_current_subframe(mod_id);
+  mac_rlc_status_resp_t rlc_status = mac_rlc_status_ind(mod_id, rnti, mod_id, frame, subframe, ENB_FLAG_YES, MBMS_FLAG_NO, channel_id, 0);
+  return rlc_status.head_sdu_creation_time;
+}
+
+int32_t flexran_get_TA(mid_t mod_id, mid_t ue_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+
+  int32_t tau = RC.eNB[mod_id][cc_id]->UE_stats[ue_id].timing_advance_update;
+  switch (flexran_get_N_RB_DL(mod_id, cc_id)) {
+  case 6:
+    return tau;
+  case 15:
+    return tau / 2;
+  case 25:
+    return tau / 4;
+  case 50:
+    return tau / 8;
+  case 75:
+    return tau / 12;
+  case 100:
+    if (flexran_get_threequarter_fs(mod_id, cc_id) == 0)
+      return tau / 16;
+    else
+      return tau / 12;
+  default:
+    return 0;
+  }
+}
+
+uint8_t flexran_get_ue_wpmi(mid_t mod_id, mid_t ue_id, uint8_t cc_id)
+{
+  if (!mac_is_present(mod_id)) return 0;
+  return RC.mac[mod_id]->UE_list.UE_sched_ctrl[ue_id].periodic_wideband_pmi[cc_id];
+}
+
+/* TODO needs to be revised */
+void flexran_update_TA(mid_t mod_id, mid_t ue_id, uint8_t cc_id)
+{
+/*
+  UE_list_t *UE_list=&eNB_mac_inst[mod_id].UE_list;
+  UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[ue_id];
+
+  if (ue_sched_ctl->ta_timer == 0) {
+    
+    // WE SHOULD PROTECT the eNB_UE_stats with a mutex here ...                                                                         
+    //    LTE_eNB_UE_stats		*eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
+    //ue_sched_ctl->ta_timer		      = 20;	// wait 20 subframes before taking TA measurement from PHY                                         
+    ue_sched_ctl->ta_update = flexran_get_TA(mod_id, ue_id, CC_id);
+
+    // clear the update in case PHY does not have a new measurement after timer expiry                                               
+    //    eNB_UE_stats->timing_advance_update	      = 0;
+  } else {
+    ue_sched_ctl->ta_timer--;
+    ue_sched_ctl->ta_update		      = 0;	// don't trigger a timing advance command      
+  }
+*/
+#warning "Implement flexran_update_TA() in RAN API"
+}
+
+/* TODO needs to be revised, looks suspicious: why do we need UE stats? */
+int flexran_get_MAC_CE_bitmap_TA(mid_t mod_id, mid_t ue_id, uint8_t cc_id)
+{
+#warning "Implement flexran_get_MAC_CE_bitmap_TA() in RAN API"
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+
+  /* UE_stats can not be null, they are an array in RC
+  LTE_eNB_UE_stats *eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id,CC_id,rnti);
+  
+  if (eNB_UE_stats == NULL) {
+    return 0;
+  }
+  */
+
+  if (flexran_get_TA(mod_id, ue_id, cc_id) != 0) {
+    return PROTOCOL__FLEX_CE_TYPE__FLPCET_TA;
+  } else {
+    return 0;
+  }
+}
+
+int flexran_get_active_CC(mid_t mod_id, mid_t ue_id)
+{
+  if (!mac_is_present(mod_id)) return 0;
+  return RC.mac[mod_id]->UE_list.numactiveCCs[ue_id];
+}
+
+uint8_t flexran_get_current_RI(mid_t mod_id, mid_t ue_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->UE_stats[ue_id].rank;
+}
+
+int flexran_get_tpc(mid_t mod_id, mid_t ue_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+
+  /* before: tested that UL_rssi != NULL and set parameter ([0]), but it is a
+   * static array -> target_rx_power is useless in old ifs?! */
+  int pCCid = UE_PCCID(mod_id,ue_id);
+  int32_t target_rx_power = RC.eNB[mod_id][pCCid]->frame_parms.ul_power_control_config_common.p0_NominalPUSCH;
+  int32_t normalized_rx_power = RC.eNB[mod_id][cc_id]->UE_stats[ue_id].UL_rssi[0];
+
+  int tpc;
+  if (normalized_rx_power > target_rx_power + 1)
+    tpc = 0;	//-1
+  else if (normalized_rx_power < target_rx_power - 1)
+    tpc = 2;	//+1
+  else
+    tpc = 1;	//0
+  return tpc;
+}
+
+int flexran_get_harq(mid_t       mod_id,
+                     uint8_t     cc_id,
+                     mid_t       ue_id,
+                     frame_t     frame,
+                     sub_frame_t subframe,
+                     uint8_t    *pid,
+                     uint8_t    *round,
+                     uint8_t     harq_flag)
+{
+  /* TODO: Add int TB in function parameters to get the status of the second
+   * TB. This can be done to by editing in get_ue_active_harq_pid function in
+   * line 272 file: phy_procedures_lte_eNB.c to add DLSCH_ptr =
+   * PHY_vars_eNB_g[Mod_id][CC_id]->dlsch_eNB[(uint32_t)UE_id][1];*/
+
+  /* TODO IMPLEMENT */
+  /*
+  uint8_t harq_pid;
+  uint8_t harq_round;
+  
+  if (mac_xface_not_ready()) return 0 ;
+
+  uint16_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  if (harq_flag == openair_harq_DL){
+
+      mac_xface->get_ue_active_harq_pid(mod_id,CC_id,rnti,frame,subframe,&harq_pid,&harq_round,openair_harq_DL);
+
+   } else if (harq_flag == openair_harq_UL){
+
+     mac_xface->get_ue_active_harq_pid(mod_id,CC_id,rnti,frame,subframe,&harq_pid,round,openair_harq_UL);    
+   }
+   else {
+
+      LOG_W(FLEXRAN_AGENT,"harq_flag is not recongnized");
+   }
+
+
+  *pid = harq_pid;
+  *round = harq_round;*/
+  /* if (round > 0) { */
+  /*   *status = 1; */
+  /* } else { */
+  /*   *status = 0; */
+  /* } */
+  /*return *round;*/
+#warning "Implement flexran_get_harq() in RAN API"
+  return 0;
+}
+
+int32_t flexran_get_p0_pucch_dbm(mid_t mod_id, mid_t ue_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->UE_stats[ue_id].Po_PUCCH_dBm;
+}
+
+int8_t flexran_get_p0_nominal_pucch(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.ul_power_control_config_common.p0_NominalPUCCH;
+}
+
+int32_t flexran_get_p0_pucch_status(mid_t mod_id, mid_t ue_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->UE_stats[ue_id].Po_PUCCH_update;
+}
+
+int flexran_update_p0_pucch(mid_t mod_id, mid_t ue_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  RC.eNB[mod_id][cc_id]->UE_stats[ue_id].Po_PUCCH_update = 0;
+  return 0;
+}
+
+
+/*
+ * ************************************
+ * Get Messages for eNB Configuration Reply
+ * ************************************
+ */
+uint8_t flexran_get_threequarter_fs(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.threequarter_fs;
+}
+
+
+uint8_t flexran_get_hopping_offset(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.pusch_config_common.pusch_HoppingOffset;
+}
+
+PUSCH_HOPPING_t flexran_get_hopping_mode(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.pusch_config_common.hoppingMode;
+}
+
+uint8_t flexran_get_n_SB(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.pusch_config_common.n_SB;
+}
+
+uint8_t flexran_get_enable64QAM(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.pusch_config_common.enable64QAM;
+}
+
+PHICH_DURATION_t flexran_get_phich_duration(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.phich_config_common.phich_duration;
+}
+
+int flexran_get_phich_resource(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  switch (RC.eNB[mod_id][cc_id]->frame_parms.phich_config_common.phich_resource) {
+  case oneSixth:
+    return 0;
+  case half:
+    return 1;
+  case one:
+    return 2;
+  case two:
+    return 3;
+  default:
+    return -1;
+  }
+}
+
+uint16_t flexran_get_n1pucch_an(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.pucch_config_common.n1PUCCH_AN;
+}
+
+uint8_t flexran_get_nRB_CQI(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.pucch_config_common.nRB_CQI;
+}
+
+uint8_t flexran_get_deltaPUCCH_Shift(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.pucch_config_common.deltaPUCCH_Shift;
+}
+
+uint8_t flexran_get_prach_ConfigIndex(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.prach_config_common.prach_ConfigInfo.prach_ConfigIndex;
+}
+
+uint8_t flexran_get_prach_FreqOffset(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.prach_config_common.prach_ConfigInfo.prach_FreqOffset;
+}
+
+uint8_t flexran_get_maxHARQ_Msg3Tx(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.maxHARQ_Msg3Tx;
+}
+
+lte_prefix_type_t flexran_get_ul_cyclic_prefix_length(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.Ncp_UL;
+}
+
+lte_prefix_type_t flexran_get_dl_cyclic_prefix_length(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.Ncp;
+}
+
+uint16_t flexran_get_cell_id(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.Nid_cell;
+}
+
+uint8_t flexran_get_srs_BandwidthConfig(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.soundingrs_ul_config_common.srs_BandwidthConfig;
+}
+
+uint8_t flexran_get_srs_SubframeConfig(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.soundingrs_ul_config_common.srs_SubframeConfig;
+}
+
+uint8_t flexran_get_srs_MaxUpPts(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.soundingrs_ul_config_common.srs_MaxUpPts;
+}
+
+uint8_t flexran_get_N_RB_DL(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.N_RB_DL;
+}
+
+uint8_t flexran_get_N_RB_UL(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.N_RB_UL;
+}
+
+uint8_t flexran_get_N_RBG(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.N_RBG;
+}
+
+uint8_t flexran_get_subframe_assignment(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.tdd_config;
+}
+
+uint8_t flexran_get_special_subframe_assignment(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.tdd_config_S;
+}
+
+long flexran_get_ra_ResponseWindowSize(mid_t mod_id, uint8_t cc_id)
+{
+  if (!rrc_is_present(mod_id)) return 0;
+  return RC.rrc[mod_id]->configuration.rach_raResponseWindowSize[cc_id];
+}
+
+long flexran_get_mac_ContentionResolutionTimer(mid_t mod_id, uint8_t cc_id)
+{
+  if (!rrc_is_present(mod_id)) return 0;
+  return RC.rrc[mod_id]->configuration.rach_macContentionResolutionTimer[cc_id];
+}
+
+Protocol__FlexDuplexMode flexran_get_duplex_mode(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  switch (RC.eNB[mod_id][cc_id]->frame_parms.frame_type) {
+  case TDD:
+    return PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD;
+  case FDD:
+    return PROTOCOL__FLEX_DUPLEX_MODE__FLDM_FDD;
+  default:
+    return -1;
+  }
+}
+
+long flexran_get_si_window_length(mid_t mod_id, uint8_t cc_id)
+{
+  if (!rrc_is_present(mod_id) || !RC.rrc[mod_id]->carrier[cc_id].sib1) return 0;
+  return RC.rrc[mod_id]->carrier[cc_id].sib1->si_WindowLength;
+}
+
+uint8_t flexran_get_sib1_length(mid_t mod_id, uint8_t cc_id)
+{
+  if (!rrc_is_present(mod_id)) return 0;
+  return RC.rrc[mod_id]->carrier[cc_id].sizeof_SIB1;
+}
+
+uint8_t flexran_get_num_pdcch_symb(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->pdcch_vars[0].num_pdcch_symbols;
+}
+
+
+
+/*
+ * ************************************
+ * Get Messages for UE Configuration Reply
+ * ************************************
+ */
+
+
+TimeAlignmentTimer_t flexran_get_time_alignment_timer(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.mac_MainConfig) return -1;
+  return ue_context_p->ue_context.mac_MainConfig->timeAlignmentTimerDedicated;
+}
+
+Protocol__FlexMeasGapConfigPattern flexran_get_meas_gap_config(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.measGapConfig) return -1;
+  if (ue_context_p->ue_context.measGapConfig->present != MeasGapConfig_PR_setup) return -1;
+  switch (ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.present) {
+  case MeasGapConfig__setup__gapOffset_PR_gp0:
+    return PROTOCOL__FLEX_MEAS_GAP_CONFIG_PATTERN__FLMGCP_GP1;
+  case MeasGapConfig__setup__gapOffset_PR_gp1:
+    return PROTOCOL__FLEX_MEAS_GAP_CONFIG_PATTERN__FLMGCP_GP2;
+  default:
+    return PROTOCOL__FLEX_MEAS_GAP_CONFIG_PATTERN__FLMGCP_OFF;
+  }
+}
+
+
+long flexran_get_meas_gap_config_offset(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.measGapConfig) return -1;
+  if (ue_context_p->ue_context.measGapConfig->present != MeasGapConfig_PR_setup) return -1;
+  switch (ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.present) {
+  case MeasGapConfig__setup__gapOffset_PR_gp0:
+    return ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.choice.gp0;
+  case MeasGapConfig__setup__gapOffset_PR_gp1:
+    return ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.choice.gp1;
+  default:
+    return -1;
+  }
+}
+
+uint8_t flexran_get_rrc_status(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return 0;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id, ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return RRC_INACTIVE;
+  return ue_context_p->ue_context.Status;
+}
+
+uint64_t flexran_get_ue_aggregated_max_bitrate_dl(mid_t mod_id, mid_t ue_id)
+{
+  if (!mac_is_present(mod_id)) return 0;
+  return RC.mac[mod_id]->UE_list.UE_sched_ctrl[ue_id].ue_AggregatedMaximumBitrateDL;
+}
+
+uint64_t flexran_get_ue_aggregated_max_bitrate_ul(mid_t mod_id, mid_t ue_id)
+{
+  if (!mac_is_present(mod_id)) return 0;
+  return RC.mac[mod_id]->UE_list.UE_sched_ctrl[ue_id].ue_AggregatedMaximumBitrateUL;
+}
+
+int flexran_get_half_duplex(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.UE_Capability) return -1;
+  SupportedBandListEUTRA_t *bands = &ue_context_p->ue_context.UE_Capability->rf_Parameters.supportedBandListEUTRA;
+  for (int i = 0; i < bands->list.count; i++) {
+    if (bands->list.array[i]->halfDuplex > 0) return 1;
+  }
+  return 0;
+}
+
+int flexran_get_intra_sf_hopping(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.UE_Capability) return -1;
+  if (!ue_context_p->ue_context.UE_Capability->featureGroupIndicators) return -1;
+  /* According to TS 36.331 Annex B.1, Intra SF Hopping is bit 1 (leftmost bit)
+   * in this bitmap, i.e. the eighth bit (from right) in the first bye (from
+   * left) */
+  BIT_STRING_t *fgi = ue_context_p->ue_context.UE_Capability->featureGroupIndicators;
+  uint8_t buf = fgi->buf[0];
+  return (buf >> 7) & 1;
+}
+
+int flexran_get_type2_sb_1(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.UE_Capability) return -1;
+  if (!ue_context_p->ue_context.UE_Capability->featureGroupIndicators) return -1;
+  /* According to TS 36.331 Annex B.1, Predefined intra- and inter-sf or
+   * predfined inter-sf frequency hopping for PUSCH with N_sb>1 is bit 21 (bit
+   * 1 is leftmost bit) in this bitmap, i.e. the fourth bit (from right) in the
+   * third byte (from left) */
+  BIT_STRING_t *fgi = ue_context_p->ue_context.UE_Capability->featureGroupIndicators;
+  uint8_t buf = fgi->buf[2];
+  return (buf >> 3) & 1;
+}
+
+long flexran_get_ue_category(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.UE_Capability) return -1;
+  return ue_context_p->ue_context.UE_Capability->ue_Category;
+}
+
+int flexran_get_res_alloc_type1(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.UE_Capability) return -1;
+  if (!ue_context_p->ue_context.UE_Capability->featureGroupIndicators) return -1;
+  /* According to TS 36.331 Annex B.1, Resource allocation type 1 for PDSCH is
+   * bit 2 (bit 1 is leftmost bit) in this bitmap, i.e. the seventh bit (from
+   * right) in the first byte (from left) */
+  BIT_STRING_t *fgi = ue_context_p->ue_context.UE_Capability->featureGroupIndicators;
+  uint8_t buf = fgi->buf[0];
+  return (buf >> 6) & 1;
+}
+
+long flexran_get_ue_transmission_mode(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated->antennaInfo) return -1;
+  return ue_context_p->ue_context.physicalConfigDedicated->antennaInfo->choice.explicitValue.transmissionMode;
+}
+
+BOOLEAN_t flexran_get_tti_bundling(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.mac_MainConfig) return -1;
+  if (!ue_context_p->ue_context.mac_MainConfig->ul_SCH_Config) return -1;
+  return ue_context_p->ue_context.mac_MainConfig->ul_SCH_Config->ttiBundling;
+}
+
+long flexran_get_maxHARQ_TX(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.mac_MainConfig) return -1;
+  if (!ue_context_p->ue_context.mac_MainConfig->ul_SCH_Config) return -1;
+  return *(ue_context_p->ue_context.mac_MainConfig->ul_SCH_Config->maxHARQ_Tx);
+}
+
+long flexran_get_beta_offset_ack_index(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated->pusch_ConfigDedicated) return -1;
+  return ue_context_p->ue_context.physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_ACK_Index;
+}
+
+long flexran_get_beta_offset_ri_index(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated->pusch_ConfigDedicated) return -1;
+  return ue_context_p->ue_context.physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_RI_Index;
+}
+
+long flexran_get_beta_offset_cqi_index(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated->pusch_ConfigDedicated) return -1;
+  return ue_context_p->ue_context.physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_CQI_Index;
+}
+
+BOOLEAN_t flexran_get_simultaneous_ack_nack_cqi(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated->cqi_ReportConfig) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated->cqi_ReportConfig->cqi_ReportPeriodic) return -1;
+  return ue_context_p->ue_context.physicalConfigDedicated->cqi_ReportConfig->cqi_ReportPeriodic->choice.setup.simultaneousAckNackAndCQI;
+}
+
+BOOLEAN_t flexran_get_ack_nack_simultaneous_trans(mid_t mod_id, mid_t ue_id, uint8_t cc_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+  if (!RC.rrc[mod_id]->carrier[cc_id].sib2) return -1;
+  return RC.rrc[mod_id]->carrier[cc_id].sib2->radioResourceConfigCommon.soundingRS_UL_ConfigCommon.choice.setup.ackNackSRS_SimultaneousTransmission;
+}
+
+CQI_ReportModeAperiodic_t flexran_get_aperiodic_cqi_rep_mode(mid_t mod_id,mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated->cqi_ReportConfig) return -1;
+  return *ue_context_p->ue_context.physicalConfigDedicated->cqi_ReportConfig->cqi_ReportModeAperiodic;
+}
+
+long flexran_get_tdd_ack_nack_feedback_mode(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated->pucch_ConfigDedicated) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode) return -1;
+  return *(ue_context_p->ue_context.physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode);
+}
+
+long flexran_get_ack_nack_repetition_factor(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated->pucch_ConfigDedicated) return -1;
+  return ue_context_p->ue_context.physicalConfigDedicated->pucch_ConfigDedicated->ackNackRepetition.choice.setup.repetitionFactor;
+}
+
+long flexran_get_extended_bsr_size(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.mac_MainConfig) return -1;
+  if (!ue_context_p->ue_context.mac_MainConfig->ext2) return -1;
+  if (!ue_context_p->ue_context.mac_MainConfig->ext2->mac_MainConfig_v1020) return -1;
+  return *(ue_context_p->ue_context.mac_MainConfig->ext2->mac_MainConfig_v1020->extendedBSR_Sizes_r10);
+}
+
+int flexran_get_ue_transmission_antenna(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated) return -1;
+  if (!ue_context_p->ue_context.physicalConfigDedicated->antennaInfo) return -1;
+  switch (ue_context_p->ue_context.physicalConfigDedicated->antennaInfo->choice.explicitValue.ue_TransmitAntennaSelection.choice.setup) {
+  case AntennaInfoDedicated__ue_TransmitAntennaSelection__setup_closedLoop:
+    return 2;
+  case AntennaInfoDedicated__ue_TransmitAntennaSelection__setup_openLoop:
+    return 1;
+  default:
+    return 0;
+  }
+}
+
+uint64_t flexran_get_ue_imsi(mid_t mod_id, mid_t ue_id)
+{
+  uint64_t imsi;
+  if (!rrc_is_present(mod_id)) return 0;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return 0;
+
+  imsi  = ue_context_p->ue_context.imsi.digit15;
+  imsi += ue_context_p->ue_context.imsi.digit14 * 10;              // pow(10, 1)
+  imsi += ue_context_p->ue_context.imsi.digit13 * 100;             // pow(10, 2)
+  imsi += ue_context_p->ue_context.imsi.digit12 * 1000;            // pow(10, 3)
+  imsi += ue_context_p->ue_context.imsi.digit11 * 10000;           // pow(10, 4)
+  imsi += ue_context_p->ue_context.imsi.digit10 * 100000;          // pow(10, 5)
+  imsi += ue_context_p->ue_context.imsi.digit9  * 1000000;         // pow(10, 6)
+  imsi += ue_context_p->ue_context.imsi.digit8  * 10000000;        // pow(10, 7)
+  imsi += ue_context_p->ue_context.imsi.digit7  * 100000000;       // pow(10, 8)
+  imsi += ue_context_p->ue_context.imsi.digit6  * 1000000000;      // pow(10, 9)
+  imsi += ue_context_p->ue_context.imsi.digit5  * 10000000000;     // pow(10, 10)
+  imsi += ue_context_p->ue_context.imsi.digit4  * 100000000000;    // pow(10, 11)
+  imsi += ue_context_p->ue_context.imsi.digit3  * 1000000000000;   // pow(10, 12)
+  imsi += ue_context_p->ue_context.imsi.digit2  * 10000000000000;  // pow(10, 13)
+  imsi += ue_context_p->ue_context.imsi.digit1  * 100000000000000; // pow(10, 14)
+  return imsi;
+}
+
+long flexran_get_lcg(mid_t mod_id, mid_t ue_id, mid_t lc_id)
+{
+  if (!mac_is_present(mod_id)) return 0;
+  return RC.mac[mod_id]->UE_list.UE_template[UE_PCCID(mod_id, ue_id)][ue_id].lcgidmap[lc_id];
+}
+
+/* TODO Navid: needs to be revised */
+int flexran_get_direction(mid_t ue_id, mid_t lc_id)
+{
+  switch (lc_id) {
+  case DCCH:
+  case DCCH1:
+    return 2;
+  case DTCH:
+    return 1;
+  default:
+    return -1;
+  }
+}
+
+uint8_t flexran_get_antenna_ports(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.nb_antenna_ports_eNB;
+}
+
+uint32_t flexran_agent_get_operating_dl_freq(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.dl_CarrierFreq / 1000000;
+}
+
+uint32_t flexran_agent_get_operating_ul_freq(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.ul_CarrierFreq / 1000000;
+}
+
+uint8_t flexran_agent_get_operating_eutra_band(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.eutra_band;
+}
+
+int8_t flexran_agent_get_operating_pdsch_refpower(mid_t mod_id, uint8_t cc_id)
+{
+  if (!phy_is_present(mod_id, cc_id)) return 0;
+  return RC.eNB[mod_id][cc_id]->frame_parms.pdsch_config_common.referenceSignalPower;
+}
+
+long flexran_agent_get_operating_pusch_p0(mid_t mod_id, uint8_t cc_id)
+{
+  if (!rrc_is_present(mod_id)) return 0;
+  return RC.rrc[mod_id]->configuration.pusch_p0_Nominal[cc_id];
+}
+
+void flexran_agent_set_operating_dl_freq(mid_t mod_id, uint8_t cc_id, uint32_t dl_freq_mhz)
+{
+  if (phy_is_present(mod_id, cc_id)) {
+    RC.eNB[mod_id][cc_id]->frame_parms.dl_CarrierFreq = dl_freq_mhz * 1000000;
+  } else {
+    LOG_E(FLEXRAN_AGENT, "can not set dl_CarrierFreq to %d MHz in PHY: PHY is not present\n", dl_freq_mhz);
+  }
+  if (rrc_is_present(mod_id)) {
+    RC.rrc[mod_id]->configuration.downlink_frequency[cc_id] = dl_freq_mhz * 1000000;
+  } else {
+    LOG_E(FLEXRAN_AGENT, "can not set downlink_frequency to %d MHz in RRC: RRC is not present\n", dl_freq_mhz);
+  }
+}
+
+void flexran_agent_set_operating_ul_freq(mid_t mod_id, uint8_t cc_id, int32_t ul_freq_mhz_offset)
+{
+  if (phy_is_present(mod_id, cc_id)) {
+    uint32_t new_ul_freq_mhz = flexran_agent_get_operating_dl_freq(mod_id, cc_id) + ul_freq_mhz_offset;
+    RC.eNB[mod_id][cc_id]->frame_parms.ul_CarrierFreq = new_ul_freq_mhz * 1000000;
+  } else {
+    LOG_E(FLEXRAN_AGENT, "can not set ul_CarrierFreq using offset %d MHz in PHY: PHY is not present\n", ul_freq_mhz_offset);
+  }
+  if (rrc_is_present(mod_id)) {
+    RC.rrc[mod_id]->configuration.uplink_frequency_offset[cc_id] = ul_freq_mhz_offset;
+  } else {
+    LOG_E(FLEXRAN_AGENT, "can not set uplink_frequency_offset to %d MHz in RRC: RRC is not present\n", ul_freq_mhz_offset);
+  }
+}
+
+void flexran_agent_set_operating_eutra_band(mid_t mod_id, uint8_t cc_id, uint8_t eutra_band)
+{
+  if (phy_is_present(mod_id, cc_id)) {
+    RC.eNB[mod_id][cc_id]->frame_parms.eutra_band = eutra_band;
+  } else {
+    LOG_E(FLEXRAN_AGENT, "can not set eutra_band to %d in PHY: PHY is not present\n", eutra_band);
+  }
+  if (rrc_is_present(mod_id)) {
+    RC.rrc[mod_id]->configuration.eutra_band[cc_id] = eutra_band;
+  } else {
+    LOG_E(FLEXRAN_AGENT, "can not set eutra_band to %d in RRC: RRC is not present\n", eutra_band);
+  }
+}
+
+/* Sets both DL/UL */
+void flexran_agent_set_operating_bandwidth(mid_t mod_id, uint8_t cc_id, uint8_t N_RB)
+{
+  if (phy_is_present(mod_id, cc_id)) {
+    RC.eNB[mod_id][cc_id]->frame_parms.N_RB_DL = N_RB;
+    RC.eNB[mod_id][cc_id]->frame_parms.N_RB_UL = N_RB;
+  } else {
+    LOG_E(FLEXRAN_AGENT, "can not set N_RB_DL and N_RB_UL to %d in PHY: PHY is not present\n", N_RB);
+  }
+  if (rrc_is_present(mod_id)) {
+    RC.rrc[mod_id]->configuration.N_RB_DL[cc_id] = N_RB;
+  } else {
+    LOG_E(FLEXRAN_AGENT, "can not set N_RB_DL to %d in RRC: RRC is not present\n", N_RB);
+  }
+}
+
+void flexran_agent_set_operating_frame_type(mid_t mod_id, uint8_t cc_id, lte_frame_type_t frame_type)
+{
+  if (phy_is_present(mod_id, cc_id)) {
+    RC.eNB[mod_id][cc_id]->frame_parms.frame_type = frame_type;
+  } else {
+    LOG_E(FLEXRAN_AGENT, "can not set frame_type to %d in PHY: PHY is not present\n", frame_type);
+  }
+  if (rrc_is_present(mod_id)) {
+    RC.rrc[mod_id]->configuration.frame_type[cc_id] = frame_type;
+  } else {
+    LOG_E(FLEXRAN_AGENT, "can not set frame_type to %d in RRC: RRC is not present\n", frame_type);
+  }
+}
+
+/*********** PDCP  *************/
+/*PDCP super frame counter flexRAN*/
+uint32_t flexran_get_pdcp_sfn(const mid_t mod_id){
+  return pdcp_enb[mod_id].sfn;
+}
+
+/*PDCP super frame counter flexRAN*/
+void flexran_set_pdcp_tx_stat_window(const mid_t mod_id, const mid_t ue_id, uint16_t obs_window){
+  if (obs_window > 0 ){
+    Pdcp_stats_tx_window_ms[mod_id][ue_id]=obs_window;
+  }
+  else{
+    Pdcp_stats_tx_window_ms[mod_id][ue_id]=1000;
+  }
+}
+
+/*PDCP super frame counter flexRAN*/
+void flexran_set_pdcp_rx_stat_window(const mid_t mod_id, const mid_t ue_id, uint16_t obs_window){
+  if (obs_window > 0 ){
+    Pdcp_stats_rx_window_ms[mod_id][ue_id]=obs_window;
+  }
+  else{
+    Pdcp_stats_rx_window_ms[mod_id][ue_id]=1000;
+  }
+}
+
+/*PDCP num tx pdu status flexRAN*/
+uint32_t flexran_get_pdcp_tx(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  if (mod_id <0 || mod_id> MAX_NUM_CCs || ue_id<0 || ue_id> NUMBER_OF_UE_MAX || lcid<0 || lcid>NB_RB_MAX)
+    return -1;
+  return Pdcp_stats_tx[mod_id][ue_id][lcid];
+}
+
+/*PDCP num tx bytes status flexRAN*/
+uint32_t flexran_get_pdcp_tx_bytes(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  return Pdcp_stats_tx_bytes[mod_id][ue_id][lcid];
+}
+
+/*PDCP number of transmit packet / second status flexRAN*/
+uint32_t flexran_get_pdcp_tx_w(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  return Pdcp_stats_tx_w[mod_id][ue_id][lcid];
+}
+
+/*PDCP throughput (bit/s) status flexRAN*/
+uint32_t flexran_get_pdcp_tx_bytes_w(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  return Pdcp_stats_tx_bytes_w[mod_id][ue_id][lcid];
+}
+
+/*PDCP tx sequence number flexRAN*/
+uint32_t flexran_get_pdcp_tx_sn(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  return Pdcp_stats_tx_sn[mod_id][ue_id][lcid];
+}
+
+/*PDCP tx aggregated packet arrival  flexRAN*/
+uint32_t flexran_get_pdcp_tx_aiat(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  return Pdcp_stats_tx_aiat[mod_id][ue_id][lcid];
+}
+
+/*PDCP tx aggregated packet arrival  flexRAN*/
+uint32_t flexran_get_pdcp_tx_aiat_w(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  return Pdcp_stats_tx_aiat_w[mod_id][ue_id][lcid];
+}
+
+
+/*PDCP num rx pdu status flexRAN*/
+uint32_t flexran_get_pdcp_rx(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  return Pdcp_stats_rx[mod_id][ue_id][lcid];
+}
+
+/*PDCP num rx bytes status flexRAN*/
+uint32_t flexran_get_pdcp_rx_bytes(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  return Pdcp_stats_rx_bytes[mod_id][ue_id][lcid];
+}
+
+/*PDCP number of received packet / second  flexRAN*/
+uint32_t flexran_get_pdcp_rx_w(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  return Pdcp_stats_rx_w[mod_id][ue_id][lcid];
+}
+
+/*PDCP gootput (bit/s) status flexRAN*/
+uint32_t flexran_get_pdcp_rx_bytes_w(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  return Pdcp_stats_rx_bytes_w[mod_id][ue_id][lcid];
+}
+
+/*PDCP rx sequence number flexRAN*/
+uint32_t flexran_get_pdcp_rx_sn(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  return Pdcp_stats_rx_sn[mod_id][ue_id][lcid];
+}
+
+/*PDCP rx aggregated packet arrival  flexRAN*/
+uint32_t flexran_get_pdcp_rx_aiat(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  return Pdcp_stats_rx_aiat[mod_id][ue_id][lcid];
+}
+
+/*PDCP rx aggregated packet arrival  flexRAN*/
+uint32_t flexran_get_pdcp_rx_aiat_w(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  return Pdcp_stats_rx_aiat_w[mod_id][ue_id][lcid];
+}
+
+/*PDCP num of received outoforder pdu status flexRAN*/
+uint32_t flexran_get_pdcp_rx_oo(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid){
+  return Pdcp_stats_rx_outoforder[mod_id][ue_id][lcid];
+}
+
+/******************** RRC *****************************/
+
+MeasId_t flexran_get_rrc_pcell_measid(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.measResults) return -1;
+  return ue_context_p->ue_context.measResults->measId;
+}
+
+float flexran_get_rrc_pcell_rsrp(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.measResults) return -1;
+  return RSRP_meas_mapping[ue_context_p->ue_context.measResults->measResultPCell.rsrpResult];
+}
+
+float flexran_get_rrc_pcell_rsrq(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.measResults) return -1;
+  return RSRQ_meas_mapping[ue_context_p->ue_context.measResults->measResultPCell.rsrqResult];
+}
+
+/*Number of neighbouring cells for specific UE*/
+int flexran_get_rrc_num_ncell(mid_t mod_id, mid_t ue_id)
+{
+  if (!rrc_is_present(mod_id)) return 0;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return 0;
+  if (!ue_context_p->ue_context.measResults) return 0;
+  if (!ue_context_p->ue_context.measResults->measResultNeighCells) return 0;
+  if (ue_context_p->ue_context.measResults->measResultNeighCells->present != MeasResults__measResultNeighCells_PR_measResultListEUTRA) return 0;
+  return ue_context_p->ue_context.measResults->measResultNeighCells->choice.measResultListEUTRA.list.count;
+}
+
+PhysCellId_t flexran_get_rrc_neigh_phy_cell_id(mid_t mod_id, mid_t ue_id, int cell_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.measResults) return -1;
+  if (!ue_context_p->ue_context.measResults->measResultNeighCells) return -1;
+  if (ue_context_p->ue_context.measResults->measResultNeighCells->present != MeasResults__measResultNeighCells_PR_measResultListEUTRA) return -1;
+  if (!ue_context_p->ue_context.measResults->measResultNeighCells->choice.measResultListEUTRA.list.array[cell_id]) return -1;
+  return ue_context_p->ue_context.measResults->measResultNeighCells->choice.measResultListEUTRA.list.array[cell_id]->physCellId;
+}
+
+float flexran_get_rrc_neigh_rsrp(mid_t mod_id, mid_t ue_id, int cell_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.measResults) return -1;
+  if (!ue_context_p->ue_context.measResults->measResultNeighCells) return -1;
+  if (ue_context_p->ue_context.measResults->measResultNeighCells->present != MeasResults__measResultNeighCells_PR_measResultListEUTRA) return -1;
+  if (!ue_context_p->ue_context.measResults->measResultNeighCells->choice.measResultListEUTRA.list.array[cell_id]) return -1;
+  if (!ue_context_p->ue_context.measResults->measResultNeighCells->choice.measResultListEUTRA.list.array[cell_id]->measResult.rsrpResult) return 0;
+  return RSRP_meas_mapping[*(ue_context_p->ue_context.measResults->measResultNeighCells->choice.measResultListEUTRA.list.array[cell_id]->measResult.rsrpResult)];
+}
+
+float flexran_get_rrc_neigh_rsrq(mid_t mod_id, mid_t ue_id, int cell_id)
+{
+  if (!rrc_is_present(mod_id)) return -1;
+
+  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
+  struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[mod_id], rnti);
+
+  if (!ue_context_p) return -1;
+  if (!ue_context_p->ue_context.measResults) return -1;
+  if (!ue_context_p->ue_context.measResults->measResultNeighCells) return -1;
+  if (ue_context_p->ue_context.measResults->measResultNeighCells->present != MeasResults__measResultNeighCells_PR_measResultListEUTRA) return -1;
+  if (!ue_context_p->ue_context.measResults->measResultNeighCells->choice.measResultListEUTRA.list.array[cell_id]->measResult.rsrqResult) return 0;
+  return RSRQ_meas_mapping[*(ue_context_p->ue_context.measResults->measResultNeighCells->choice.measResultListEUTRA.list.array[cell_id]->measResult.rsrqResult)];
+}
diff --git a/openair2/ENB_APP/flexran_agent_ran_api.h b/openair2/ENB_APP/flexran_agent_ran_api.h
new file mode 100644
index 0000000000000000000000000000000000000000..aafb82077e986f989e577149350dc1b4f8c0b8cc
--- /dev/null
+++ b/openair2/ENB_APP/flexran_agent_ran_api.h
@@ -0,0 +1,432 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */ 
+
+/*! \file flexran_agent_ran_api.h
+ * \brief FlexRAN RAN API abstraction header 
+ * \author N. Nikaein, X. Foukas and S. SHARIAT BAGHERI
+ * \date 2017
+ * \version 0.1
+ */
+
+#include <stdio.h>
+#include <time.h>
+
+#include "flexran_agent_common.h"
+#include "flexran_agent_common_internal.h"
+#include "flexran_agent_extern.h"
+#include "flexran_agent_defs.h"
+
+
+#include "enb_config.h"
+#include "LAYER2/MAC/extern.h"
+#include "LAYER2/RLC/rlc.h"
+#include "SCHED/defs.h"
+#include "pdcp.h"
+#include "RRC/LITE/extern.h"
+#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
+#include "RRC/LITE/rrc_eNB_UE_context.h"
+#include "PHY/extern.h"
+#include "log.h"
+
+/****************************
+ * get generic info from RAN
+ ****************************/
+
+uint32_t flexran_get_current_time_ms(mid_t mod_id, int subframe_flag);
+
+/*Return the current frame number
+ *Could be using implementation specific numbering of frames
+ */
+frame_t flexran_get_current_frame(mid_t mod_id);
+
+/*Return the current SFN (0-1023)*/ 
+frame_t flexran_get_current_system_frame_num(mid_t mod_id);
+
+sub_frame_t flexran_get_current_subframe(mid_t mod_id);
+
+/*Return the frame and subframe number in compact 16-bit format.
+  Bits 0-3 subframe, rest for frame. Required by FlexRAN protocol*/
+uint16_t flexran_get_sfn_sf(mid_t mod_id);
+
+/* Return a future frame and subframe number that is ahead_of_time
+   subframes later in compact 16-bit format. Bits 0-3 subframe,
+   rest for frame */
+uint16_t flexran_get_future_sfn_sf(mid_t mod_id, int ahead_of_time);
+
+/* Return the number of attached UEs */
+int flexran_get_num_ues(mid_t mod_id);
+
+/* Get the rnti of a UE with id ue_id */
+rnti_t flexran_get_ue_crnti(mid_t mod_id, mid_t ue_id);
+
+/* Get the RLC buffer status report in bytes of a ue for a designated
+ * logical channel id */
+int flexran_get_ue_bsr_ul_buffer_info(mid_t mod_id, mid_t ue_id, lcid_t lcid);
+
+/* Get power headroom of UE with id ue_id */
+int8_t flexran_get_ue_phr(mid_t mod_id, mid_t ue_id);
+
+/* Get the UE wideband CQI */
+uint8_t flexran_get_ue_wcqi(mid_t mod_id, mid_t ue_id);
+
+/* Get the transmission queue size for a UE with a channel_id logical channel id */
+rlc_buffer_occupancy_t flexran_get_tx_queue_size(mid_t mod_id, mid_t ue_id, logical_chan_id_t channel_id);
+
+/*Get number of pdus in RLC buffer*/
+rlc_buffer_occupancy_t flexran_get_num_pdus_buffer(mid_t mod_id, mid_t ue_id, logical_chan_id_t channel_id);
+
+/* Get the head of line delay for a UE with a channel_id logical channel id */
+frame_t flexran_get_hol_delay(mid_t mod_id, mid_t ue_id, logical_chan_id_t channel_id);
+
+/* Check the status of the timing advance for a UE */
+int32_t flexran_get_TA(mid_t mod_id, mid_t ue_id, uint8_t cc_id);
+
+/* Update the timing advance status(find out whether a timing advance command is required) */
+/* currently broken
+void flexran_update_TA(mid_t mod_id, mid_t ue_id, uint8_t cc_id); */
+
+/* Return timing advance MAC control element for a designated cell and UE */
+/* this function is broken */
+int flexran_get_MAC_CE_bitmap_TA(mid_t mod_id, mid_t ue_id, uint8_t cc_id);
+
+/* Get the number of active component carriers for a specific UE */
+int flexran_get_active_CC(mid_t mod_id, mid_t ue_id);
+
+/* Get the rank indicator for a designated cell and UE */
+uint8_t flexran_get_current_RI(mid_t mod_id, mid_t ue_id, uint8_t cc_id);
+
+/* See TS 36.213, section 10.1 */
+uint16_t flexran_get_n1pucch_an(mid_t mod_id, uint8_t cc_id);
+
+/* See TS 36.211, section 5.4 */
+uint8_t flexran_get_nRB_CQI(mid_t mod_id, uint8_t cc_id);
+
+/* See TS 36.211, section 5.4 */
+uint8_t flexran_get_deltaPUCCH_Shift(mid_t mod_id, uint8_t cc_id);
+
+/* See TS 36.211, section 5.7.1 */
+uint8_t flexran_get_prach_ConfigIndex(mid_t mod_id, uint8_t cc_id);
+
+/* See TS 36.211, section 5.7.1 */
+uint8_t flexran_get_prach_FreqOffset(mid_t mod_id, uint8_t cc_id);
+
+/* See TS 36.321 */
+uint8_t flexran_get_maxHARQ_Msg3Tx(mid_t mod_id, uint8_t cc_id);
+
+/* Get the length of the UL cyclic prefix */
+lte_prefix_type_t flexran_get_ul_cyclic_prefix_length(mid_t mod_id, uint8_t cc_id);
+
+/* Get the length of the DL cyclic prefix */
+lte_prefix_type_t flexran_get_dl_cyclic_prefix_length(mid_t mod_id, uint8_t cc_id);
+
+/* Get the physical cell id of a cell */
+uint16_t flexran_get_cell_id(mid_t mod_id, uint8_t cc_id);
+
+/* See TS 36.211, section 5.5.3.2 */
+uint8_t flexran_get_srs_BandwidthConfig(mid_t mod_id, uint8_t cc_id);
+
+/* See TS 36.211, table 5.5.3.3-1 and 2 */
+uint8_t flexran_get_srs_SubframeConfig(mid_t mod_id, uint8_t cc_id);
+
+/* Boolean value. See TS 36.211,
+   section 5.5.3.2. TDD only */
+uint8_t flexran_get_srs_MaxUpPts(mid_t mod_id, uint8_t cc_id);
+
+/* Get number of DL resource blocks */
+uint8_t flexran_get_N_RB_DL(mid_t mod_id, uint8_t cc_id);
+
+/* Get number of UL resource blocks */
+uint8_t flexran_get_N_RB_UL(mid_t mod_id, uint8_t cc_id);
+
+/* Get number of resource block groups */
+uint8_t flexran_get_N_RBG(mid_t mod_id, uint8_t cc_id);
+
+/* Get DL/UL subframe assignment. TDD only */
+uint8_t flexran_get_subframe_assignment(mid_t mod_id, uint8_t cc_id);
+
+/* TDD only. See TS 36.211, table 4.2.1 */
+uint8_t flexran_get_special_subframe_assignment(mid_t mod_id, uint8_t cc_id);
+
+/* Get the duration of the random access response window in subframes */
+long flexran_get_ra_ResponseWindowSize(mid_t mod_id, uint8_t cc_id);
+
+/* Get timer used for random access */
+long flexran_get_mac_ContentionResolutionTimer(mid_t mod_id, uint8_t cc_id);
+
+/* Get type of duplex mode(FDD/TDD) */
+Protocol__FlexDuplexMode flexran_get_duplex_mode(mid_t mod_id, uint8_t cc_id);
+
+/* Get the SI window length */
+long flexran_get_si_window_length(mid_t mod_id, uint8_t cc_id);
+
+/* Get length of SystemInformationBlock1 */
+uint8_t flexran_get_sib1_length(mid_t mod_id, uint8_t cc_id);
+
+/* Get the number of PDCCH symbols configured for the cell */
+uint8_t flexran_get_num_pdcch_symb(mid_t mod_id, uint8_t cc_id);
+
+uint8_t flexran_get_antenna_ports(mid_t mod_id, uint8_t cc_id);
+
+/* See TS 36.213, sec 5.1.1.1 */
+int flexran_get_tpc(mid_t mod_id, mid_t ue_id, uint8_t cc_id);
+
+uint8_t flexran_get_ue_wpmi(mid_t mod_id, mid_t ue_id, uint8_t cc_id);
+
+/* Get the first available HARQ process for a specific cell and UE during 
+   a designated frame and subframe. Returns 0 for success. The id and the 
+   status of the HARQ process are stored in id and status respectively */
+/* currently broken
+int flexran_get_harq(mid_t mod_id, uint8_t cc_id, mid_t ue_id, frame_t frame,
+                     sub_frame_t subframe, unsigned char *id, unsigned char *round,
+                     uint8_t harq_flag); */
+
+/* Uplink power control management*/
+int32_t flexran_get_p0_pucch_dbm(mid_t mod_id, mid_t ue_id, uint8_t cc_id);
+
+int8_t flexran_get_p0_nominal_pucch(mid_t mod_id, uint8_t cc_id);
+
+int32_t flexran_get_p0_pucch_status(mid_t mod_id, mid_t ue_id, uint8_t cc_id);
+
+int flexran_update_p0_pucch(mid_t mod_id, mid_t ue_id, uint8_t cc_id);
+
+uint8_t flexran_get_threequarter_fs(mid_t mod_id, uint8_t cc_id);
+
+PUSCH_HOPPING_t flexran_get_hopping_mode(mid_t mod_id, uint8_t cc_id);
+
+uint8_t flexran_get_hopping_offset(mid_t mod_id, uint8_t cc_id);
+
+uint8_t flexran_get_n_SB(mid_t mod_id, uint8_t cc_id);
+
+int flexran_get_phich_resource(mid_t mod_id, uint8_t cc_id);
+
+uint8_t flexran_get_enable64QAM(mid_t mod_id, uint8_t cc_id);
+
+PHICH_DURATION_t flexran_get_phich_duration(mid_t mod_id, uint8_t cc_id);
+
+/*
+ * ************************************
+ * Get Messages for UE Configuration Reply
+ * ************************************
+ */
+
+/* Get timer in subframes. Controls the synchronization
+   status of the UE, not the actual timing 
+   advance procedure. See TS 36.321 */
+TimeAlignmentTimer_t flexran_get_time_alignment_timer(mid_t mod_id, mid_t ue_id);
+
+/* Get measurement gap configuration. See TS 36.133 */
+Protocol__FlexMeasGapConfigPattern flexran_get_meas_gap_config(mid_t mod_id, mid_t ue_id);
+
+/* Get measurement gap configuration offset if applicable */
+long flexran_get_meas_gap_config_offset(mid_t mod_id, mid_t ue_id);
+
+/* DL aggregated bit-rate of non-gbr bearer
+   per UE. See TS 36.413 */
+uint64_t flexran_get_ue_aggregated_max_bitrate_dl(mid_t mod_id, mid_t ue_id);
+
+/* UL aggregated bit-rate of non-gbr bearer
+   per UE. See TS 36.413 */
+uint64_t flexran_get_ue_aggregated_max_bitrate_ul(mid_t mod_id, mid_t ue_id);
+
+/* Only half-duplex support. FDD operation. Boolean value */
+int flexran_get_half_duplex(mid_t mod_id, mid_t ue_id);
+
+/* Support of intra-subframe hopping.  Boolean value */
+int flexran_get_intra_sf_hopping(mid_t mod_id, mid_t ue_id);
+
+/* UE support for type 2 hopping with n_sb>1 */
+int flexran_get_type2_sb_1(mid_t mod_id, mid_t ue_id);
+
+/* Get the UE category */
+long flexran_get_ue_category(mid_t mod_id, mid_t ue_id);
+
+/* UE support for resource allocation type 1 */
+int flexran_get_res_alloc_type1(mid_t mod_id, mid_t ue_id);
+
+/* Get UE transmission mode */
+long flexran_get_ue_transmission_mode(mid_t mod_id, mid_t ue_id);
+
+/* Boolean value. See TS 36.321 */
+BOOLEAN_t flexran_get_tti_bundling(mid_t mod_id, mid_t ue_id);
+
+/* The max HARQ retransmission for UL.
+   See TS 36.321 */
+long flexran_get_maxHARQ_TX(mid_t mod_id, mid_t ue_id);
+
+/* See TS 36.213 */
+long flexran_get_beta_offset_ack_index(mid_t mod_id, mid_t ue_id);
+
+/* See TS 36.213 */
+long flexran_get_beta_offset_ri_index(mid_t mod_id, mid_t ue_id);
+
+/* See TS 36.213 */
+long flexran_get_beta_offset_cqi_index(mid_t mod_id, mid_t ue_id);
+
+/* Boolean. See TS36.213, Section 10.1 */
+BOOLEAN_t flexran_get_simultaneous_ack_nack_cqi(mid_t mod_id, mid_t ue_id);
+
+/* Boolean. See TS 36.213, Section 8.2 */
+BOOLEAN_t flexran_get_ack_nack_simultaneous_trans(mid_t mod_id, mid_t ue_id, uint8_t cc_id);
+
+/* Get aperiodic CQI report mode */
+CQI_ReportModeAperiodic_t flexran_get_aperiodic_cqi_rep_mode(mid_t mod_id,mid_t ue_id);
+
+/* Get ACK/NACK feedback mode. TDD only */
+long flexran_get_tdd_ack_nack_feedback_mode(mid_t mod_id, mid_t ue_id);
+
+/* See TS36.213, section 10.1 */
+long flexran_get_ack_nack_repetition_factor(mid_t mod_id, mid_t ue_id);
+
+/* Boolean. Extended buffer status report size */
+long flexran_get_extended_bsr_size(mid_t mod_id, mid_t ue_id);
+
+/* Get number of UE transmission antennas */
+int flexran_get_ue_transmission_antenna(mid_t mod_id, mid_t ue_id);
+
+/* Get the IMSI of UE */
+uint64_t flexran_get_ue_imsi(mid_t mod_id, mid_t ue_id);
+
+/* Get logical channel group of a channel with id lc_id */
+long flexran_get_lcg(mid_t mod_id, mid_t ue_id, mid_t lc_id);
+
+/* Get direction of logical channel with id lc_id */
+int flexran_get_direction(mid_t ue_id, mid_t lc_id);
+
+/*Get downlink frequency*/
+uint32_t flexran_agent_get_operating_dl_freq(mid_t mod_id, uint8_t cc_id);
+
+/*Get uplink frequency*/
+uint32_t flexran_agent_get_operating_ul_freq(mid_t mod_id, uint8_t cc_id);
+
+/*Get eutra band*/
+uint8_t flexran_agent_get_operating_eutra_band(mid_t mod_id, uint8_t cc_id);
+
+/*Get downlink ref signal power*/
+int8_t flexran_agent_get_operating_pdsch_refpower(mid_t mod_id, uint8_t cc_id);
+
+/*Get uplink power*/
+long flexran_agent_get_operating_pusch_p0(mid_t mod_id, uint8_t cc_id);
+
+/*set the dl freq */
+void flexran_agent_set_operating_dl_freq(mid_t mod_id, uint8_t cc_id, uint32_t dl_freq_mhz);
+
+/* set the ul freq */
+void flexran_agent_set_operating_ul_freq(mid_t mod_id, uint8_t cc_id, int32_t ul_freq_mhz_offset);
+
+/*set the the band */
+void flexran_agent_set_operating_eutra_band(mid_t mod_id, uint8_t cc_id, uint8_t eutra_band);
+
+/* set the bandwidth (in RB) */
+void flexran_agent_set_operating_bandwidth(mid_t mod_id, uint8_t cc_id, uint8_t N_RB);
+
+/*set frame type*/
+void flexran_agent_set_operating_frame_type(mid_t mod_id, uint8_t cc_id, lte_frame_type_t frame_type);
+
+/*RRC status flexRAN*/
+uint8_t flexran_get_rrc_status(mid_t mod_id, mid_t ue_id);
+
+
+/***************************** PDCP ***********************/
+
+/*PDCP superframe numberflexRAN*/
+uint32_t flexran_get_pdcp_sfn(const mid_t mod_id);
+
+/*PDCP pdcp tx stats window*/
+void flexran_set_pdcp_tx_stat_window(const mid_t mod_id, const mid_t ue_id, uint16_t obs_window);
+
+/*PDCP pdcp rx stats window*/
+void flexran_set_pdcp_rx_stat_window(const mid_t mod_id, const mid_t ue_id, uint16_t obs_window);
+
+/*PDCP num tx pdu status flexRAN*/
+uint32_t flexran_get_pdcp_tx(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+/*PDCP num tx bytes status flexRAN*/
+uint32_t flexran_get_pdcp_tx_bytes(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+/*PDCP number of transmit packet / second status flexRAN*/
+uint32_t flexran_get_pdcp_tx_w(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+/*PDCP pdcp tx bytes in a given window flexRAN*/
+uint32_t flexran_get_pdcp_tx_bytes_w(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+/*PDCP tx sequence number flexRAN*/
+uint32_t flexran_get_pdcp_tx_sn(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+/*PDCP tx aggregated packet arrival  flexRAN*/
+uint32_t flexran_get_pdcp_tx_aiat(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+/*PDCP tx aggregated packet arrival per second flexRAN*/
+uint32_t flexran_get_pdcp_tx_aiat_w(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+
+/*PDCP num rx pdu status flexRAN*/
+uint32_t flexran_get_pdcp_rx(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+/*PDCP num rx bytes status flexRAN*/
+uint32_t flexran_get_pdcp_rx_bytes(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+/*PDCP number of received packet / second  flexRAN*/
+uint32_t flexran_get_pdcp_rx_w(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+/*PDCP gootput (bit/s) status flexRAN*/
+uint32_t flexran_get_pdcp_rx_bytes_w(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+/*PDCP rx sequence number flexRAN*/
+uint32_t flexran_get_pdcp_rx_sn(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+/*PDCP rx aggregated packet arrival  flexRAN*/
+uint32_t flexran_get_pdcp_rx_aiat(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+/*PDCP rx aggregated packet arrival per second flexRAN*/
+uint32_t flexran_get_pdcp_rx_aiat_w(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+/*PDCP num of received outoforder pdu status flexRAN*/
+uint32_t flexran_get_pdcp_rx_oo(const mid_t mod_id,  const mid_t ue_id, const lcid_t lcid);
+
+/*********************RRC**********************/
+/*Get primary cell measuremeant id flexRAN*/
+MeasId_t flexran_get_rrc_pcell_measid(mid_t mod_id, mid_t ue_id);
+
+/*Get primary cell RSRP measurement flexRAN*/  
+float flexran_get_rrc_pcell_rsrp(mid_t mod_id, mid_t ue_id);
+
+/*Get primary cell RSRQ measurement flexRAN*/
+float flexran_get_rrc_pcell_rsrq(mid_t mod_id, mid_t ue_id);
+
+/* Get RRC neighbouring measurement */
+int flexran_get_rrc_num_ncell(mid_t mod_id, mid_t ue_id);
+
+/*Get physical cell id*/
+PhysCellId_t flexran_get_rrc_neigh_phy_cell_id(mid_t mod_id, mid_t ue_id, int cell_id);
+
+/*Get RSRP of neighbouring Cell*/
+float flexran_get_rrc_neigh_rsrp(mid_t mod_id, mid_t ue_id, int cell_id);
+
+/*Get RSRQ of neighbouring Cell*/
+float flexran_get_rrc_neigh_rsrq(mid_t mod_id, mid_t ue_id, int cell_id);
+
+/*Get MCC PLMN identity neighbouring Cell*/
+/* currently not implemented
+int flexran_get_rrc_neigh_plmn_mcc(mid_t mod_id, mid_t ue_id, int cell_id); */
+
+/*Get MNC PLMN identity neighbouring Cell*/
+/* currently not implemented
+int flexran_get_rrc_neigh_plmn_mnc(mid_t mod_id, mid_t ue_id, int cell_id); */
diff --git a/openair2/ENB_APP/flexran_agent_timer.c b/openair2/ENB_APP/flexran_agent_timer.c
new file mode 100644
index 0000000000000000000000000000000000000000..f5436e4d01dcf192744dac2506978986ad5bc02e
--- /dev/null
+++ b/openair2/ENB_APP/flexran_agent_timer.c
@@ -0,0 +1,217 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */ 
+
+/*! \file flexran_agent_timer.c
+ * \brief FlexRAN Timer  
+ * \author shahab SHARIAT BAGHERI
+ * \date 2017
+ * \version 0.1
+ */
+
+/*
+ * timer primitives
+ */
+
+#include "flexran_agent_timer.h"
+
+//struct flexran_agent_map agent_map;
+flexran_agent_timer_instance_t timer_instance;
+int agent_timer_init = 0;
+err_code_t flexran_agent_init_timer(void){
+  
+  LOG_I(FLEXRAN_AGENT, "init RB tree\n");
+  if (!agent_timer_init) {
+    RB_INIT(&timer_instance.flexran_agent_head);
+    agent_timer_init = 1;
+  }
+ 
+ return PROTOCOL__FLEXRAN_ERR__NO_ERR;
+}
+
+RB_GENERATE(flexran_agent_map, flexran_agent_timer_element_s, entry, flexran_agent_compare_timer);
+
+/* The timer_id might not be the best choice for the comparison */
+int flexran_agent_compare_timer(struct flexran_agent_timer_element_s *a, struct flexran_agent_timer_element_s *b){
+
+  if (a->timer_id < b->timer_id) return -1;
+  if (a->timer_id > b->timer_id) return 1;
+
+  // equal timers
+  return 0;
+}
+
+err_code_t flexran_agent_create_timer(uint32_t interval_sec,
+				      uint32_t interval_usec,
+				      agent_id_t     agent_id,
+				      instance_t     instance,
+				      uint32_t timer_type,
+				      xid_t xid,
+				      flexran_agent_timer_callback_t cb,
+				      void*    timer_args,
+				      long *timer_id){
+  
+  struct flexran_agent_timer_element_s *e = calloc(1, sizeof(*e));
+  DevAssert(e != NULL);
+  
+//uint32_t timer_id;
+  int ret=-1;
+  
+  if ((interval_sec == 0) && (interval_usec == 0 ))
+    return TIMER_NULL;
+  
+  if (timer_type >= FLEXRAN_AGENT_TIMER_TYPE_MAX)
+    return TIMER_TYPE_INVALIDE;
+  
+  if (timer_type  ==   FLEXRAN_AGENT_TIMER_TYPE_ONESHOT){ 
+    ret = timer_setup(interval_sec, 
+		      interval_usec, 
+		      TASK_FLEXRAN_AGENT, 
+		      instance, 
+		      TIMER_ONE_SHOT,
+		      timer_args,
+		      timer_id);
+    
+    e->type = TIMER_ONE_SHOT;
+  }
+  else if (timer_type  ==   FLEXRAN_AGENT_TIMER_TYPE_PERIODIC ){
+    ret = timer_setup(interval_sec, 
+		      interval_usec, 
+		      TASK_FLEXRAN_AGENT, 
+		      instance, 
+		      TIMER_PERIODIC,
+		      timer_args,
+		      timer_id);
+    
+    e->type = TIMER_PERIODIC;
+  }
+  
+  if (ret < 0 ) {
+    return TIMER_SETUP_FAILED; 
+  }
+
+  e->agent_id = agent_id;
+  e->instance = instance;
+  e->state = FLEXRAN_AGENT_TIMER_STATE_ACTIVE;
+  e->timer_id = *timer_id;
+  e->xid = xid;
+  e->timer_args = timer_args; 
+  e->cb = cb;
+  /*element should be a real pointer*/
+  RB_INSERT(flexran_agent_map, &timer_instance.flexran_agent_head, e); 
+  
+  LOG_I(FLEXRAN_AGENT,"Created a new timer with id 0x%lx for agent %d, instance %d \n",
+	e->timer_id, e->agent_id, e->instance);
+  
+  return 0; 
+}
+
+err_code_t flexran_agent_destroy_timer(long timer_id){
+  
+  struct flexran_agent_timer_element_s *e = get_timer_entry(timer_id);
+
+  if (e != NULL ) {
+    RB_REMOVE(flexran_agent_map, &timer_instance.flexran_agent_head, e);
+    flexran_agent_destroy_flexran_message(e->timer_args->msg);
+    free(e);
+  }
+  
+  if (timer_remove(timer_id) < 0 ) 
+    goto error;
+  
+  return 0;
+
+ error:
+  LOG_E(FLEXRAN_AGENT, "timer can't be removed\n");
+  return TIMER_REMOVED_FAILED ;
+}
+
+err_code_t flexran_agent_destroy_timer_by_task_id(xid_t xid) {
+  struct flexran_agent_timer_element_s *e = NULL;
+  long timer_id;
+  RB_FOREACH(e, flexran_agent_map, &timer_instance.flexran_agent_head) {
+    if (e->xid == xid) {
+      timer_id = e->timer_id;
+      RB_REMOVE(flexran_agent_map, &timer_instance.flexran_agent_head, e);
+      flexran_agent_destroy_flexran_message(e->timer_args->msg);
+      free(e);
+      if (timer_remove(timer_id) < 0 ) { 
+	goto error;
+      }
+    }
+  }
+  return 0;
+
+ error:
+  LOG_E(FLEXRAN_AGENT, "timer can't be removed\n");
+  return TIMER_REMOVED_FAILED ;
+}
+
+err_code_t flexran_agent_destroy_timers(void){
+  
+  struct flexran_agent_timer_element_s *e = NULL;
+  
+  RB_FOREACH(e, flexran_agent_map, &timer_instance.flexran_agent_head) {
+    RB_REMOVE(flexran_agent_map, &timer_instance.flexran_agent_head, e);
+    timer_remove(e->timer_id);
+    flexran_agent_destroy_flexran_message(e->timer_args->msg);
+    free(e);
+  }  
+
+  return 0;
+
+}
+
+void flexran_agent_sleep_until(struct timespec *ts, int delay) {
+  ts->tv_nsec += delay;
+  if(ts->tv_nsec >= 1000*1000*1000){
+    ts->tv_nsec -= 1000*1000*1000;
+    ts->tv_sec++;
+  }
+  clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, ts,  NULL);
+}
+
+
+err_code_t flexran_agent_stop_timer(long timer_id){
+  
+  struct flexran_agent_timer_element_s *e=NULL;
+  struct flexran_agent_timer_element_s search;
+  memset(&search, 0, sizeof(struct flexran_agent_timer_element_s));
+  search.timer_id = timer_id;
+
+  e = RB_FIND(flexran_agent_map, &timer_instance.flexran_agent_head, &search);
+
+  if (e != NULL ) {
+    e->state =  FLEXRAN_AGENT_TIMER_STATE_STOPPED;
+  }
+  
+  timer_remove(timer_id);
+  
+  return 0;
+}
+
+struct flexran_agent_timer_element_s * get_timer_entry(long timer_id) {
+  
+  struct flexran_agent_timer_element_s search;
+  memset(&search, 0, sizeof(struct flexran_agent_timer_element_s));
+  search.timer_id = timer_id;
+
+  return  RB_FIND(flexran_agent_map, &timer_instance.flexran_agent_head, &search); 
+}
diff --git a/openair2/ENB_APP/flexran_agent_timer.h b/openair2/ENB_APP/flexran_agent_timer.h
new file mode 100644
index 0000000000000000000000000000000000000000..86e0d07825c094ed2fc2efa80f929414931db02d
--- /dev/null
+++ b/openair2/ENB_APP/flexran_agent_timer.h
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */ 
+
+/*! \file flexran_agent_timer.h
+ * \brief FlexRAN Timer  header
+ * \author shahab SHARIAT BAGHERI
+ * \date 2017
+ * \version 0.1
+ */
+
+#include <stdio.h>
+#include <time.h>
+
+#include "flexran_agent_common.h"
+#include "flexran_agent_common_internal.h"
+#include "flexran_agent_extern.h"
+#include "flexran_agent_defs.h"
+
+
+# include "tree.h"
+# include "intertask_interface.h"
+# include "timer.h"
+
+
+
+/*******************
+ * timer primitves
+ *******************/
+
+#define TIMER_NULL                 -1 
+#define TIMER_TYPE_INVALIDE        -2
+#define	TIMER_SETUP_FAILED         -3
+#define	TIMER_REMOVED_FAILED       -4
+#define	TIMER_ELEMENT_NOT_FOUND    -5
+
+
+/* Type of the callback executed when the timer expired */
+typedef Protocol__FlexranMessage *(*flexran_agent_timer_callback_t)(void*);
+
+
+typedef struct flexran_agent_timer_args_s{
+  mid_t            mod_id;
+  Protocol__FlexranMessage *msg;
+} flexran_agent_timer_args_t;
+
+
+typedef struct flexran_agent_timer_element_s{
+  RB_ENTRY(flexran_agent_timer_element_s) entry;
+
+  agent_id_t             agent_id;
+  instance_t       instance;
+  
+  flexran_agent_timer_type_t  type;
+  flexran_agent_timer_state_t state;
+
+  uint32_t interval_sec;
+  uint32_t interval_usec;
+
+  long timer_id;  /* Timer id returned by the timer API*/
+  xid_t xid; /*The id of the task as received by the controller
+	       message*/
+  
+  flexran_agent_timer_callback_t cb;
+  flexran_agent_timer_args_t *timer_args;
+  
+} flexran_agent_timer_element_t;
+
+typedef struct flexran_agent_timer_instance_s{
+  RB_HEAD(flexran_agent_map, flexran_agent_timer_element_s) flexran_agent_head;
+}flexran_agent_timer_instance_t;
+
+
+err_code_t flexran_agent_init_timer(void);
+
+/* Create a timer for some agent related event with id xid. Will store the id 
+   of the generated timer in timer_id */
+err_code_t flexran_agent_create_timer(uint32_t interval_sec,
+				  uint32_t interval_usec,
+				  agent_id_t     agent_id,
+				  instance_t     instance,
+				  uint32_t timer_type,
+				  xid_t xid,
+				  flexran_agent_timer_callback_t cb,
+				  void*    timer_args,
+				  long *timer_id);
+
+/* Destroy all existing timers */
+err_code_t flexran_agent_destroy_timers(void);
+
+/* Destroy the timer with the given timer_id */
+err_code_t flexran_agent_destroy_timer(long timer_id);
+
+/* Destroy the timer for task with id xid */
+err_code_t flexran_agent_destroy_timer_by_task_id(xid_t xid);
+
+/* Stop a timer */
+err_code_t flexran_agent_stop_timer(long timer_id);
+
+/* Restart the given timer */
+err_code_t flexran_agent_restart_timer(long *timer_id);
+
+/* Find the timer with the given timer_id */
+struct flexran_agent_timer_element_s * get_timer_entry(long timer_id);
+
+/* Obtain the protocol message stored in the given expired timer */
+Protocol__FlexranMessage * flexran_agent_process_timeout(long timer_id, void* timer_args);
+
+/* Comparator function comparing two timers. Decides the ordering of the timers */
+int flexran_agent_compare_timer(struct flexran_agent_timer_element_s *a, struct flexran_agent_timer_element_s *b);
+
+/*Specify a delay in nanoseconds to timespec and sleep until then*/
+void flexran_agent_sleep_until(struct timespec *ts, int delay);
+
+/* RB_PROTOTYPE is for .h files */
+RB_PROTOTYPE(flexran_agent_map, flexran_agent_timer_element_s, entry, flexran_agent_compare_timer);
diff --git a/openair2/LAYER2/MAC/config.c b/openair2/LAYER2/MAC/config.c
index 55585acd23e90ff745d61395ac3f2281d80c7511..f45cc45af1523fc46fe5805815dfe37c8e7fbcdf 100644
--- a/openair2/LAYER2/MAC/config.c
+++ b/openair2/LAYER2/MAC/config.c
@@ -437,59 +437,31 @@ config_sib2(int Mod_idP,
     cfg->num_tlv++;
 
     
-    nfapi_config_request_t *cfg = &RC.mac[Mod_idP]->config[CC_idP];
-    
-    cfg->subframe_config.pb.value               = radioResourceConfigCommonP->pdsch_ConfigCommon.p_b;
-    cfg->rf_config.reference_signal_power.value = radioResourceConfigCommonP->pdsch_ConfigCommon.referenceSignalPower;
-    cfg->nfapi_config.max_transmit_power.value  = cfg->rf_config.reference_signal_power.value + 
-      power_off_dB[cfg->rf_config.dl_channel_bandwidth.value];
-    
-    cfg->prach_config.configuration_index.value                 = radioResourceConfigCommonP->prach_Config.prach_ConfigInfo.prach_ConfigIndex;
-    cfg->prach_config.root_sequence_index.value                 = radioResourceConfigCommonP->prach_Config.rootSequenceIndex;
-    cfg->prach_config.zero_correlation_zone_configuration.value = radioResourceConfigCommonP->prach_Config.prach_ConfigInfo.zeroCorrelationZoneConfig;
-    cfg->prach_config.high_speed_flag.value                     = radioResourceConfigCommonP->prach_Config.prach_ConfigInfo.highSpeedFlag;
-    cfg->prach_config.frequency_offset.value                    = radioResourceConfigCommonP->prach_Config.prach_ConfigInfo.prach_FreqOffset;
-    
-    cfg->pusch_config.hopping_mode.value       = radioResourceConfigCommonP->pusch_ConfigCommon.pusch_ConfigBasic.hoppingMode;
-    cfg->pusch_config.number_of_subbands.value = radioResourceConfigCommonP->pusch_ConfigCommon.pusch_ConfigBasic.n_SB;
-    cfg->pusch_config.hopping_offset.value     = radioResourceConfigCommonP->pusch_ConfigCommon.pusch_ConfigBasic.pusch_HoppingOffset;
-    
-    cfg->pucch_config.delta_pucch_shift.value = radioResourceConfigCommonP->pucch_ConfigCommon.deltaPUCCH_Shift;
-    cfg->pucch_config.n_cqi_rb.value          = radioResourceConfigCommonP->pucch_ConfigCommon.nRB_CQI;
-    cfg->pucch_config.n_an_cs.value           = radioResourceConfigCommonP->pucch_ConfigCommon.nCS_AN;
-    cfg->pucch_config.n1_pucch_an.value       = radioResourceConfigCommonP->pucch_ConfigCommon.n1PUCCH_AN;
-    
-    if (radioResourceConfigCommonP->pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.groupHoppingEnabled == true)
-      cfg->uplink_reference_signal_config.uplink_rs_hopping.value = 1;
-    else if (radioResourceConfigCommonP->pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.sequenceHoppingEnabled == true)
-      cfg->uplink_reference_signal_config.uplink_rs_hopping.value = 2;
-    else			// No hopping
-      cfg->uplink_reference_signal_config.uplink_rs_hopping.value = 0;
-    
-    cfg->uplink_reference_signal_config.group_assignment.value        = radioResourceConfigCommonP->pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.groupAssignmentPUSCH;
-    cfg->uplink_reference_signal_config.cyclic_shift_1_for_drms.value = radioResourceConfigCommonP->pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.cyclicShift;
-    
-    // how to enable/disable SRS?
-    if (radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.present == SoundingRS_UL_ConfigCommon_PR_setup) {
-      cfg->srs_config.bandwidth_configuration.value                   = radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.choice.setup.srs_BandwidthConfig;
-      cfg->srs_config.srs_subframe_configuration.value                = radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.choice.setup.srs_SubframeConfig;
-      cfg->srs_config.srs_acknack_srs_simultaneous_transmission.value = radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.choice.setup.ackNackSRS_SimultaneousTransmission;
-      
-      if (radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.choice.setup.srs_MaxUpPts)
-	cfg->srs_config.max_up_pts.value = 1;
-      else
-	cfg->srs_config.max_up_pts.value = 0;
+    if (radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.choice.setup.srs_MaxUpPts) {
+       cfg->srs_config.max_up_pts.value                                 = 1;
+    }
+    else {
+       cfg->srs_config.max_up_pts.value                                 = 0;
     }
+    cfg->srs_config.max_up_pts.tl.tag = NFAPI_SRS_CONFIG_MAX_UP_PTS_TAG;
+    cfg->num_tlv++;
   }
+
 #ifdef Rel14
   if (RC.mac[Mod_idP]->common_channels[CC_idP].mib->message.schedulingInfoSIB1_BR_r13 > 0) {
-    AssertFatal(radioResourceConfigCommon_BRP != NULL,
-		"radioResource rou is missing\n");
-    AssertFatal(radioResourceConfigCommon_BRP->ext4 != NULL,
-		"ext4 is missing\n");
+    AssertFatal(radioResourceConfigCommon_BRP != NULL, "radioResource rou is missing\n");
+    AssertFatal(radioResourceConfigCommon_BRP->ext4 != NULL, "ext4 is missing\n");
     cfg->emtc_config.prach_catm_root_sequence_index.value = radioResourceConfigCommon_BRP->prach_Config.rootSequenceIndex;
+    cfg->emtc_config.prach_catm_root_sequence_index.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CATM_ROOT_SEQUENCE_INDEX_TAG;
+    cfg->num_tlv++;
+
     cfg->emtc_config.prach_catm_zero_correlation_zone_configuration.value = radioResourceConfigCommon_BRP->prach_Config.prach_ConfigInfo.zeroCorrelationZoneConfig;
+    cfg->emtc_config.prach_catm_zero_correlation_zone_configuration.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CATM_ZERO_CORRELATION_ZONE_CONFIGURATION_TAG;
+    cfg->num_tlv++;
+
     cfg->emtc_config.prach_catm_high_speed_flag.value = radioResourceConfigCommon_BRP->prach_Config.prach_ConfigInfo.highSpeedFlag;
+    cfg->emtc_config.prach_catm_high_speed_flag.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CATM_HIGH_SPEED_FLAG;
+    cfg->num_tlv++;
 
     struct PRACH_ConfigSIB_v1310 *ext4_prach = radioResourceConfigCommon_BRP->ext4->prach_ConfigCommon_v1310;
 
@@ -497,94 +469,186 @@ config_sib2(int Mod_idP,
 
     PRACH_ParametersCE_r13_t *p;
     cfg->emtc_config.prach_ce_level_0_enable.value = 0;
+    cfg->emtc_config.prach_ce_level_0_enable.tl.tag=NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_0_ENABLE_TAG;
+    cfg->num_tlv++;
+
     cfg->emtc_config.prach_ce_level_1_enable.value = 0;
+    cfg->emtc_config.prach_ce_level_1_enable.tl.tag=NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_1_ENABLE_TAG;
+    cfg->num_tlv++;
+
     cfg->emtc_config.prach_ce_level_2_enable.value = 0;
+    cfg->emtc_config.prach_ce_level_2_enable.tl.tag=NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_2_ENABLE_TAG;
+    cfg->num_tlv++;
+
     cfg->emtc_config.prach_ce_level_3_enable.value = 0;
+    cfg->emtc_config.prach_ce_level_3_enable.tl.tag=NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_3_ENABLE_TAG;
+    cfg->num_tlv++;
+
     switch (prach_ParametersListCE_r13->list.count) {
     case 4:
       p = prach_ParametersListCE_r13->list.array[3];
       cfg->emtc_config.prach_ce_level_3_enable.value = 1;
+      cfg->emtc_config.prach_ce_level_3_enable.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_3_ENABLE_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_3_configuration_index.value               = p->prach_ConfigIndex_r13;
+      cfg->emtc_config.prach_ce_level_3_configuration_index.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_3_CONFIGURATION_INDEX_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_3_frequency_offset.value                  = p->prach_FreqOffset_r13;
+      cfg->emtc_config.prach_ce_level_3_frequency_offset.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_3_FREQUENCY_OFFSET_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_3_number_of_repetitions_per_attempt.value = p->numRepetitionPerPreambleAttempt_r13;
-      if (p->prach_StartingSubframe_r13)
+      cfg->emtc_config.prach_ce_level_3_number_of_repetitions_per_attempt.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_3_NUMBER_OF_REPETITIONS_PER_ATTEMPT_TAG;
+      cfg->num_tlv++;
+
+      if (p->prach_StartingSubframe_r13) {
 	cfg->emtc_config.prach_ce_level_3_starting_subframe_periodicity.value   = *p->prach_StartingSubframe_r13;
+        cfg->emtc_config.prach_ce_level_3_starting_subframe_periodicity.tl.tag  = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_3_STARTING_SUBFRAME_PERIODICITY_TAG;
+        cfg->num_tlv++;
+      }
+
       cfg->emtc_config.prach_ce_level_3_hopping_enable.value                    = p->prach_HoppingConfig_r13;
+      cfg->emtc_config.prach_ce_level_3_hopping_enable.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_3_HOPPING_ENABLE_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_3_hopping_offset.value                    = cfg->rf_config.ul_channel_bandwidth.value - 6;
+      cfg->emtc_config.prach_ce_level_3_hopping_offset.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_3_HOPPING_OFFSET_TAG;
+      cfg->num_tlv++;
+
     case 3:
       p = prach_ParametersListCE_r13->list.array[2];
       cfg->emtc_config.prach_ce_level_2_enable.value = 1;
+      cfg->emtc_config.prach_ce_level_2_enable.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_2_ENABLE_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_2_configuration_index.value               = p->prach_ConfigIndex_r13;
+      cfg->emtc_config.prach_ce_level_2_configuration_index.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_2_CONFIGURATION_INDEX_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_2_frequency_offset.value                  = p->prach_FreqOffset_r13;
+      cfg->emtc_config.prach_ce_level_2_frequency_offset.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_2_FREQUENCY_OFFSET_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_2_number_of_repetitions_per_attempt.value = p->numRepetitionPerPreambleAttempt_r13;
-      if (p->prach_StartingSubframe_r13)
+      cfg->emtc_config.prach_ce_level_2_number_of_repetitions_per_attempt.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_2_NUMBER_OF_REPETITIONS_PER_ATTEMPT_TAG;
+      cfg->num_tlv++;
+
+      if (p->prach_StartingSubframe_r13) {
 	cfg->emtc_config.prach_ce_level_2_starting_subframe_periodicity.value   = *p->prach_StartingSubframe_r13;
+        cfg->emtc_config.prach_ce_level_2_starting_subframe_periodicity.tl.tag  = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_2_STARTING_SUBFRAME_PERIODICITY_TAG;
+        cfg->num_tlv++;
+      }
+
       cfg->emtc_config.prach_ce_level_2_hopping_enable.value                    = p->prach_HoppingConfig_r13;
+      cfg->emtc_config.prach_ce_level_2_hopping_enable.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_2_HOPPING_ENABLE_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_2_hopping_offset.value                    = cfg->rf_config.ul_channel_bandwidth.value - 6;
+      cfg->emtc_config.prach_ce_level_2_hopping_offset.tl.tag                   = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_2_HOPPING_OFFSET_TAG;
+      cfg->num_tlv++;
+
     case 2:
       p = prach_ParametersListCE_r13->list.array[1];
       cfg->emtc_config.prach_ce_level_1_enable.value = 1;
+      cfg->emtc_config.prach_ce_level_1_enable.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_1_ENABLE_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_1_configuration_index.value               = p->prach_ConfigIndex_r13;
+      cfg->emtc_config.prach_ce_level_1_configuration_index.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_1_CONFIGURATION_INDEX_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_1_frequency_offset.value                  = p->prach_FreqOffset_r13;
+      cfg->emtc_config.prach_ce_level_1_frequency_offset.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_1_FREQUENCY_OFFSET_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_1_number_of_repetitions_per_attempt.value = p->numRepetitionPerPreambleAttempt_r13;
-      if (p->prach_StartingSubframe_r13)
+      cfg->emtc_config.prach_ce_level_1_number_of_repetitions_per_attempt.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_1_NUMBER_OF_REPETITIONS_PER_ATTEMPT_TAG;
+      cfg->num_tlv++;
+
+      if (p->prach_StartingSubframe_r13) {
 	cfg->emtc_config.prach_ce_level_1_starting_subframe_periodicity.value   = *p->prach_StartingSubframe_r13;
+        cfg->emtc_config.prach_ce_level_1_starting_subframe_periodicity.tl.tag  = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_1_STARTING_SUBFRAME_PERIODICITY_TAG;
+        cfg->num_tlv++;
+      }
+
       cfg->emtc_config.prach_ce_level_1_hopping_enable.value                    = p->prach_HoppingConfig_r13;
+      cfg->emtc_config.prach_ce_level_1_hopping_enable.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_1_HOPPING_ENABLE_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_1_hopping_offset.value                    = cfg->rf_config.ul_channel_bandwidth.value - 6;
+      cfg->emtc_config.prach_ce_level_1_hopping_offset.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_1_HOPPING_OFFSET_TAG;
+      cfg->num_tlv++;
+
     case 1:
       p = prach_ParametersListCE_r13->list.array[0];
       cfg->emtc_config.prach_ce_level_0_enable.value                            = 1;
+      cfg->emtc_config.prach_ce_level_0_enable.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_0_ENABLE_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_0_configuration_index.value               = p->prach_ConfigIndex_r13;
+      cfg->emtc_config.prach_ce_level_0_configuration_index.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_0_CONFIGURATION_INDEX_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_0_frequency_offset.value                  = p->prach_FreqOffset_r13;
+      cfg->emtc_config.prach_ce_level_0_frequency_offset.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_0_FREQUENCY_OFFSET_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_0_number_of_repetitions_per_attempt.value = p->numRepetitionPerPreambleAttempt_r13;
-      if (p->prach_StartingSubframe_r13)
+      cfg->emtc_config.prach_ce_level_0_number_of_repetitions_per_attempt.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_0_NUMBER_OF_REPETITIONS_PER_ATTEMPT_TAG;
+      cfg->num_tlv++;
+
+      if (p->prach_StartingSubframe_r13) {
 	cfg->emtc_config.prach_ce_level_0_starting_subframe_periodicity.value   = *p->prach_StartingSubframe_r13;
+        cfg->emtc_config.prach_ce_level_0_starting_subframe_periodicity.tl.tag  = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_0_STARTING_SUBFRAME_PERIODICITY_TAG;
+        cfg->num_tlv++;
+      }
+
       cfg->emtc_config.prach_ce_level_0_hopping_enable.value                    = p->prach_HoppingConfig_r13;
+      cfg->emtc_config.prach_ce_level_0_hopping_enable.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_0_HOPPING_ENABLE_TAG;
+      cfg->num_tlv++;
+
       cfg->emtc_config.prach_ce_level_0_hopping_offset.value                    = cfg->rf_config.ul_channel_bandwidth.value - 6;
+      cfg->emtc_config.prach_ce_level_0_hopping_offset.tl.tag = NFAPI_EMTC_CONFIG_PRACH_CE_LEVEL_0_HOPPING_OFFSET_TAG;
+      cfg->num_tlv++;
     }
 
     struct FreqHoppingParameters_r13 *ext4_freqHoppingParameters = radioResourceConfigCommonP->ext4->freqHoppingParameters_r13;
-    if ((ext4_freqHoppingParameters)&&
-	(ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13))
-      {
-	switch (ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13->present) {
-	case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeA_r13_PR_NOTHING:	/* No components present */
-	  break;
-	case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeA_r13_PR_interval_FDD_r13:
-	  cfg->emtc_config.pucch_interval_ulhoppingconfigcommonmodea.value =
-	    ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13->
-	    choice.interval_FDD_r13;
-	  break;
-	case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeA_r13_PR_interval_TDD_r13:
-	  cfg->emtc_config.
-	    pucch_interval_ulhoppingconfigcommonmodea.value =
-	    ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13->
-	    choice.interval_TDD_r13;
-	  break;
-	}
+    if ((ext4_freqHoppingParameters) &&
+        (ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13)){
+      switch(ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13->present) {
+      case      FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeA_r13_PR_NOTHING:  /* No components present */
+        break;
+      case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeA_r13_PR_interval_FDD_r13:
+        cfg->emtc_config.pucch_interval_ulhoppingconfigcommonmodea.value = ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13->choice.interval_FDD_r13;
+        cfg->emtc_config.pucch_interval_ulhoppingconfigcommonmodea.tl.tag = NFAPI_EMTC_CONFIG_PUCCH_INTERVAL_ULHOPPINGCONFIGCOMMONMODEA_TAG;
+        cfg->num_tlv++;
+        break;
+      case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeA_r13_PR_interval_TDD_r13:
+        cfg->emtc_config.pucch_interval_ulhoppingconfigcommonmodea.value = ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13->choice.interval_TDD_r13;
+        cfg->emtc_config.pucch_interval_ulhoppingconfigcommonmodea.tl.tag = NFAPI_EMTC_CONFIG_PUCCH_INTERVAL_ULHOPPINGCONFIGCOMMONMODEA_TAG;
+        cfg->num_tlv++;
+        break;
       }
+    }
     if ((ext4_freqHoppingParameters) &&
-	(ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13))
-      {
-	switch
-	  (ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13->
-	   present) {
-	case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeB_r13_PR_NOTHING:	/* No components present */
-	  break;
-	case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeB_r13_PR_interval_FDD_r13:
-	  cfg->emtc_config.
-	    pucch_interval_ulhoppingconfigcommonmodeb.value =
-	    ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13->
-	    choice.interval_FDD_r13;
-	  break;
-	case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeB_r13_PR_interval_TDD_r13:
-	  cfg->emtc_config.
-	    pucch_interval_ulhoppingconfigcommonmodeb.value =
-	    ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13->
-	    choice.interval_TDD_r13;
-	  break;
-	}
+        (ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13)){
+      switch(ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13->present) {
+      case      FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeB_r13_PR_NOTHING:  /* No components present */
+        break;
+      case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeB_r13_PR_interval_FDD_r13:
+        cfg->emtc_config.pucch_interval_ulhoppingconfigcommonmodeb.value = ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13->choice.interval_FDD_r13;
+        cfg->emtc_config.pucch_interval_ulhoppingconfigcommonmodeb.tl.tag = NFAPI_EMTC_CONFIG_PUCCH_INTERVAL_ULHOPPINGCONFIGCOMMONMODEB_TAG;
+        cfg->num_tlv++;
+        break;
+      case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeB_r13_PR_interval_TDD_r13:
+        cfg->emtc_config.pucch_interval_ulhoppingconfigcommonmodeb.value = ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13->choice.interval_TDD_r13;
+        cfg->emtc_config.pucch_interval_ulhoppingconfigcommonmodeb.tl.tag = NFAPI_EMTC_CONFIG_PUCCH_INTERVAL_ULHOPPINGCONFIGCOMMONMODEB_TAG;
+        cfg->num_tlv++;
+        break;
       }
+    }
   }
 #endif
 
diff --git a/openair2/LAYER2/MAC/defs.h b/openair2/LAYER2/MAC/defs.h
index 3f721288b498f4e727e4427f6d2f0bcda7880d20..ef1d7101389d6fa96502c928eb029b9d3f4f7daf 100644
--- a/openair2/LAYER2/MAC/defs.h
+++ b/openair2/LAYER2/MAC/defs.h
@@ -692,7 +692,7 @@ typedef struct {
     uint16_t cshift[8];		// num_max_harq
 
     /// Number of Allocated RBs by the ulsch preprocessor
-    uint8_t pre_allocated_nb_rb_ul;
+    uint8_t pre_allocated_nb_rb_ul[MAX_NUM_SLICES];
 
     /// index of Allocated RBs by the ulsch preprocessor
     int8_t pre_allocated_rb_table_index_ul;
@@ -803,7 +803,8 @@ typedef struct {
     ///Contention resolution timer used during random access
     uint8_t mac_ContentionResolutionTimer;
 
-    uint16_t max_allowed_rbs[MAX_NUM_LCID];
+    uint16_t max_rbs_allowed_slice[MAX_NUM_CCs][MAX_NUM_SLICES];
+    uint16_t max_rbs_allowed_slice_uplink[MAX_NUM_CCs][MAX_NUM_SLICES];
 
     uint8_t max_mcs[MAX_NUM_LCID];
 
@@ -962,6 +963,10 @@ typedef struct {
     int avail;
     int num_UEs;
     boolean_t active[NUMBER_OF_UE_MAX];
+
+    /// Sorting criteria for the UE list in the MAC preprocessor
+    uint16_t sorting_criteria[MAX_NUM_SLICES][CR_NUM];
+
 } UE_list_t;
 
 /*! \brief eNB common channels */
diff --git a/openair2/LAYER2/MAC/eNB_scheduler.c b/openair2/LAYER2/MAC/eNB_scheduler.c
index 3c6471212afc0ff89a4a7aec4da01375962488ae..a7c79a70d5537d341bfe6d3b82a613e8e7ac6000 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler.c
@@ -52,12 +52,9 @@
 //#include "LAYER2/MAC/pre_processor.c"
 #include "pdcp.h"
 
-#if defined(FLEXRAN_AGENT_SB_IF)
 //Agent-related headers
 #include "flexran_agent_extern.h"
 #include "flexran_agent_mac.h"
-#include "flexran_agent_mac_proto.h"
-#endif
 
 #if defined(ENABLE_ITTI)
 #include "intertask_interface.h"
@@ -416,6 +413,13 @@ check_ul_failure(module_id_t module_idP, int CC_id, int UE_id,
       mac_eNB_rrc_ul_failure(module_idP, CC_id, frameP, subframeP,rnti);
       UE_list->UE_sched_ctrl[UE_id].ul_failure_timer = 0;
       UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync   = 1;
+
+      //Inform the controller about the UE deactivation. Should be moved to RRC agent in the future
+      if (rrc_agent_registered[module_idP]) {
+        LOG_W(MAC, "notify flexran Agent of UE state change\n");
+        agent_rrc_xface[module_idP]->flexran_agent_notify_ue_state_change(module_idP,
+            rnti, PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_DEACTIVATED);
+      }
     }
   }				// ul_failure_timer>0
 
@@ -497,22 +501,20 @@ eNB_dlsch_ulsch_scheduler(module_id_t module_idP, frame_t frameP,
   int               mbsfn_status[MAX_NUM_CCs];
   protocol_ctxt_t   ctxt;
 
-  int               CC_id, i;
+  int               CC_id, i = -1;
   UE_list_t         *UE_list = &RC.mac[module_idP]->UE_list;
   rnti_t            rnti;
 
   COMMON_channels_t *cc      = RC.mac[module_idP]->common_channels;
 
-#if defined(FLEXRAN_AGENT_SB_IF)
-  Protocol__FlexranMessage *msg;
-#endif
-
-
   start_meas(&RC.mac[module_idP]->eNB_scheduler);
   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
     (VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ULSCH_SCHEDULER,
      VCD_FUNCTION_IN);
 
+  RC.mac[module_idP]->frame    = frameP;
+  RC.mac[module_idP]->subframe = subframeP;
+
   for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
     mbsfn_status[CC_id] = 0;
 
@@ -524,9 +526,29 @@ eNB_dlsch_ulsch_scheduler(module_id_t module_idP, frame_t frameP,
 #if defined(Rel10) || defined(Rel14)
     cc[CC_id].mcch_active        = 0;
 #endif
-    RC.mac[module_idP]->frame    = frameP;
-    RC.mac[module_idP]->subframe = subframeP;
 
+    clear_nfapi_information(RC.mac[module_idP], CC_id, frameP, subframeP);
+  }
+
+  // refresh UE list based on UEs dropped by PHY in previous subframe
+  for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
+    if (UE_list->active[i] != TRUE)
+      continue;
+
+    rnti = UE_RNTI(module_idP, i);
+    CC_id = UE_PCCID(module_idP, i);
+
+    if ((frameP == 0) && (subframeP == 0)) {
+      LOG_I(MAC,
+            "UE  rnti %x : %s, PHR %d dB DL CQI %d PUSCH SNR %d PUCCH SNR %d\n",
+            rnti,
+            UE_list->UE_sched_ctrl[i].ul_out_of_sync ==
+            0 ? "in synch" : "out of sync",
+            UE_list->UE_template[CC_id][i].phr_info,
+            UE_list->UE_sched_ctrl[i].dl_cqi[CC_id],
+            (UE_list->UE_sched_ctrl[i].pusch_snr[CC_id] - 128) / 2,
+            (UE_list->UE_sched_ctrl[i].pucch1_snr[CC_id] - 128) / 2);
+    }
 
     RC.eNB[module_idP][CC_id]->pusch_stats_bsr[i][(frameP * 10) +
 						  subframeP] = -63;
@@ -594,42 +616,6 @@ eNB_dlsch_ulsch_scheduler(module_id_t module_idP, frame_t frameP,
     }
   }
 
-    // refresh UE list based on UEs dropped by PHY in previous subframe
-  for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
-    if (UE_list->active[i] != TRUE) continue;
-
-    rnti  = UE_RNTI(module_idP, i);
-    CC_id = UE_PCCID(module_idP, i);
-
-    if ((frameP == 0) && (subframeP == 0)) {
-      LOG_I(MAC,
-	    "UE  rnti %x : %s, PHR %d dB DL CQI %d PUSCH SNR %d PUCCH SNR %d\n",
-	    rnti,
-	    UE_list->UE_sched_ctrl[i].ul_out_of_sync ==
-	    0 ? "in synch" : "out of sync",
-	    UE_list->UE_template[CC_id][i].phr_info,
-	    UE_list->UE_sched_ctrl[i].dl_cqi[CC_id],
-	    (UE_list->UE_sched_ctrl[i].pusch_snr[CC_id] - 128) / 2,
-	    (UE_list->UE_sched_ctrl[i].pucch1_snr[CC_id] - 128) / 2);
-    }
-
-    RC.eNB[module_idP][CC_id]->pusch_stats_bsr[i][(frameP * 10) + subframeP] = -63;
-    if (i == UE_list->head)
-      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME
-	(VCD_SIGNAL_DUMPER_VARIABLES_UE0_BSR,
-	 RC.eNB[module_idP][CC_id]->
-	 pusch_stats_bsr[i][(frameP * 10) + subframeP]);
-    // increment this, it is cleared when we receive an sdu
-    RC.mac[module_idP]->UE_list.UE_sched_ctrl[i].ul_inactivity_timer++;
-
-    RC.mac[module_idP]->UE_list.UE_sched_ctrl[i].cqi_req_timer++;
-    LOG_D(MAC, "UE %d/%x : ul_inactivity %d, cqi_req %d\n", i, rnti,
-	  RC.mac[module_idP]->UE_list.UE_sched_ctrl[i].
-	  ul_inactivity_timer,
-	  RC.mac[module_idP]->UE_list.UE_sched_ctrl[i].cqi_req_timer);
-    check_ul_failure(module_idP, CC_id, i, frameP, subframeP);
-  }
-
   PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, module_idP, ENB_FLAG_YES,
 				 NOT_A_RNTI, frameP, subframeP,
 				 module_idP);
@@ -652,7 +638,7 @@ eNB_dlsch_ulsch_scheduler(module_id_t module_idP, frame_t frameP,
 
   // This schedules MIB
   if ((subframeP == 0) && (frameP & 3) == 0)
-    schedule_mib(module_idP, frameP, subframeP);
+      schedule_mib(module_idP, frameP, subframeP);
   // This schedules SI for legacy LTE and eMTC starting in subframeP
   schedule_SI(module_idP, frameP, subframeP);
   // This schedules Paging in subframeP
@@ -669,18 +655,19 @@ eNB_dlsch_ulsch_scheduler(module_id_t module_idP, frame_t frameP,
   schedule_SR(module_idP, frameP, subframeP);
   // This schedules UCI_CSI in subframeP
   schedule_CSI(module_idP, frameP, subframeP);
-  
   // This schedules DLSCH in subframeP
-  schedule_ue_spec(module_idP, frameP, subframeP, mbsfn_status);
+  schedule_dlsch(module_idP, frameP, subframeP, mbsfn_status);
+
+  if (RC.flexran[module_idP]->enabled)
+    flexran_agent_send_update_stats(module_idP);
   
   // Allocate CCEs for good after scheduling is done
-  
   for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++)
-    allocate_CCEs(module_idP, CC_id, subframeP, 0);
-  
-  
+      allocate_CCEs(module_idP, CC_id, subframeP, 0);
+
   stop_meas(&RC.mac[module_idP]->eNB_scheduler);
-  
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ULSCH_SCHEDULER,
-					  VCD_FUNCTION_OUT);
+
+  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+      (VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ULSCH_SCHEDULER,
+      VCD_FUNCTION_OUT);
 }
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_dlsch.c b/openair2/LAYER2/MAC/eNB_scheduler_dlsch.c
index 4d298f69f5c957732a250d0a8d68ce948ff352cc..48b19b091942ac19dd02f1ac102190781792023f 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler_dlsch.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_dlsch.c
@@ -57,6 +57,12 @@
 #include "intertask_interface.h"
 #endif
 
+#include "ENB_APP/flexran_agent_defs.h"
+#include "flexran_agent_ran_api.h"
+#include "header.pb-c.h"
+#include "flexran.pb-c.h"
+#include <dlfcn.h>
+
 #include "T.h"
 
 #define ENABLE_MAC_PAYLOAD_DEBUG
@@ -65,6 +71,40 @@
 extern RAN_CONTEXT_t RC;
 extern uint8_t nfapi_mode;
 
+
+// number of active slices for  past and current time
+int n_active_slices = 1;
+int n_active_slices_current = 1;
+
+// RB share for each slice for past and current time
+float avg_slice_percentage=0.25;
+float slice_percentage[MAX_NUM_SLICES] = {1.0, 0.0, 0.0, 0.0};
+float slice_percentage_current[MAX_NUM_SLICES] = {1.0, 0.0, 0.0, 0.0};
+float total_slice_percentage = 0;
+float total_slice_percentage_current = 0;
+
+// MAX MCS for each slice for past and current time
+int slice_maxmcs[MAX_NUM_SLICES] = { 28, 28, 28, 28 };
+int slice_maxmcs_current[MAX_NUM_SLICES] = { 28, 28, 28, 28 };
+
+int update_dl_scheduler[MAX_NUM_SLICES] = { 1, 1, 1, 1 };
+int update_dl_scheduler_current[MAX_NUM_SLICES] = { 1, 1, 1, 1 };
+
+// name of available scheduler
+char *dl_scheduler_type[MAX_NUM_SLICES] =
+  { "schedule_ue_spec",
+    "schedule_ue_spec",
+    "schedule_ue_spec",
+    "schedule_ue_spec"
+  };
+
+// The lists of criteria that enforce the sorting policies of the slices
+uint32_t sorting_policy[MAX_NUM_SLICES] = {0x01234, 0x01234, 0x01234, 0x01234};
+uint32_t sorting_policy_current[MAX_NUM_SLICES] = {0x01234, 0x01234, 0x01234, 0x01234};
+
+// pointer to the slice specific scheduler
+slice_scheduler_dl slice_sched_dl[MAX_NUM_SLICES] = {0};
+
 //------------------------------------------------------------------------------
 void
 add_ue_dlsch_info(module_id_t module_idP,
@@ -404,12 +444,122 @@ set_ul_DAI(int module_idP, int UE_idP, int CC_idP, int frameP,
   }
 }
 
+//------------------------------------------------------------------------------
+void
+schedule_dlsch(module_id_t module_idP,
+	        frame_t frameP, sub_frame_t subframeP, int *mbsfn_flag)
+//------------------------------------------------------------------------------{
+{
+
+  int i = 0;
+
+  total_slice_percentage=0;
+  avg_slice_percentage=1.0/n_active_slices;
+
+  // reset the slice percentage for inactive slices
+  for (i = n_active_slices; i< MAX_NUM_SLICES; i++) {
+    slice_percentage[i]=0;
+  }
+  for (i = 0; i < n_active_slices; i++) {
+    if (slice_percentage[i] < 0 ){
+      LOG_W(MAC, "[eNB %d] frame %d subframe %d:invalid slice %d percentage %f. resetting to zero",
+	    module_idP, frameP, subframeP, i, slice_percentage[i]);
+      slice_percentage[i]=0;
+    }
+    total_slice_percentage+=slice_percentage[i];
+  }
+
+  for (i = 0; i < n_active_slices; i++) {
+
+    // Load any updated functions
+    if (update_dl_scheduler[i] > 0 ) {
+      slice_sched_dl[i] = dlsym(NULL, dl_scheduler_type[i]);
+      update_dl_scheduler[i] = 0 ;
+      update_dl_scheduler_current[i] = 0;
+      LOG_N(MAC,"update dl scheduler slice %d\n", i);
+    }
+
+    if (total_slice_percentage <= 1.0){ // the new total RB share is within the range
+
+      // check if the number of slices has changed, and log
+      if (n_active_slices_current != n_active_slices ){
+	if ((n_active_slices > 0) && (n_active_slices <= MAX_NUM_SLICES)) {
+	  LOG_N(MAC,"[eNB %d]frame %d subframe %d: number of active DL slices has changed: %d-->%d\n",
+		module_idP, frameP, subframeP, n_active_slices_current, n_active_slices);
+
+	  n_active_slices_current = n_active_slices;
+
+	} else {
+	  LOG_W(MAC,"invalid number of DL slices %d, revert to the previous value %d\n",n_active_slices, n_active_slices_current);
+	  n_active_slices = n_active_slices_current;
+	}
+      }
+
+      // check if the slice rb share has changed, and log the console
+      if (slice_percentage_current[i] != slice_percentage[i]){ // new slice percentage
+	LOG_N(MAC,"[eNB %d][SLICE %d][DL] frame %d subframe %d: total percentage %f-->%f, slice RB percentage has changed: %f-->%f\n",
+	      module_idP, i, frameP, subframeP, total_slice_percentage_current, total_slice_percentage, slice_percentage_current[i], slice_percentage[i]);
+	total_slice_percentage_current= total_slice_percentage;
+	slice_percentage_current[i] = slice_percentage[i];
+
+      }
+
+      // check if the slice max MCS, and log the console
+      if (slice_maxmcs_current[i] != slice_maxmcs[i]){
+	if ((slice_maxmcs[i] >= 0) && (slice_maxmcs[i] < 29)){
+	  LOG_N(MAC,"[eNB %d][SLICE %d][DL] frame %d subframe %d: slice MAX MCS has changed: %d-->%d\n",
+		module_idP, i, frameP, subframeP, slice_maxmcs_current[i], slice_maxmcs[i]);
+	  slice_maxmcs_current[i] = slice_maxmcs[i];
+	} else {
+	  LOG_W(MAC,"[eNB %d][SLICE %d][DL] invalid slice max mcs %d, revert the previous value %d\n",module_idP, i, slice_maxmcs[i],slice_maxmcs_current[i]);
+	  slice_maxmcs[i]= slice_maxmcs_current[i];
+	}
+      }
+
+      // check if a new scheduler, and log the console
+      if (update_dl_scheduler_current[i] != update_dl_scheduler[i]){
+	LOG_N(MAC,"[eNB %d][SLICE %d][DL] frame %d subframe %d: DL scheduler for this slice is updated: %s \n",
+	      module_idP, i, frameP, subframeP, dl_scheduler_type[i]);
+	update_dl_scheduler_current[i] = update_dl_scheduler[i];
+      }
+
+    } else {
+      // here we can correct the values, e.g. reduce proportionally
+
+      if (n_active_slices == n_active_slices_current){
+	LOG_W(MAC,"[eNB %d][SLICE %d][DL] invalid total RB share (%f->%f), reduce proportionally the RB share by 0.1\n",
+	      module_idP, i, total_slice_percentage_current, total_slice_percentage);
+	if (slice_percentage[i] >= avg_slice_percentage){
+	  slice_percentage[i]-=0.1;
+	  total_slice_percentage-=0.1;
+	}
+      } else {
+	LOG_W(MAC,"[eNB %d][SLICE %d][DL] invalid total RB share (%f->%f), revert the number of slice to its previous value (%d->%d)\n",
+	      module_idP, i, total_slice_percentage_current, total_slice_percentage,
+	      n_active_slices, n_active_slices_current );
+	n_active_slices = n_active_slices_current;
+	slice_percentage[i] = slice_percentage_current[i];
+      }
+    }
+
+    // Check for new sorting policy
+    if (sorting_policy_current[i] != sorting_policy[i]) {
+      LOG_N(MAC,"[eNB %d][SLICE %d][DL] frame %d subframe %d: UE sorting policy has changed (%x-->%x)\n",
+            module_idP, i, frameP, subframeP, sorting_policy_current[i], sorting_policy[i]);
+      sorting_policy_current[i] = sorting_policy[i];
+    }
+
+    // Run each enabled slice-specific schedulers one by one
+    slice_sched_dl[i](module_idP, i, frameP, subframeP, mbsfn_flag/*, dl_info*/);
+  }
+
+}
 
 // changes to pre-processor for eMTC
 
 //------------------------------------------------------------------------------
 void
-schedule_ue_spec(module_id_t module_idP,
+schedule_ue_spec(module_id_t module_idP,slice_id_t slice_idP,
 		 frame_t frameP, sub_frame_t subframeP, int *mbsfn_flag)
 //------------------------------------------------------------------------------
 {
@@ -529,13 +679,18 @@ schedule_ue_spec(module_id_t module_idP,
 
   /// CALLING Pre_Processor for downlink scheduling (Returns estimation of RBs required by each UE and the allocation on sub-band)
 
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_DLSCH_PREPROCESSOR, VCD_FUNCTION_IN);
+  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+      (VCD_SIGNAL_DUMPER_FUNCTIONS_DLSCH_PREPROCESSOR, VCD_FUNCTION_IN);
   start_meas(&eNB->schedule_dlsch_preprocessor);
   dlsch_scheduler_pre_processor(module_idP,
-				frameP, subframeP, N_RBG, mbsfn_flag);
+                                slice_idP,
+                                frameP,
+                                subframeP,
+                                N_RBG,
+                                mbsfn_flag);
   stop_meas(&eNB->schedule_dlsch_preprocessor);
   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
-    (VCD_SIGNAL_DUMPER_FUNCTIONS_DLSCH_PREPROCESSOR, VCD_FUNCTION_OUT);
+      (VCD_SIGNAL_DUMPER_FUNCTIONS_DLSCH_PREPROCESSOR, VCD_FUNCTION_OUT);
 
   for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
     LOG_D(MAC, "doing schedule_ue_spec for CC_id %d\n", CC_id);
@@ -560,9 +715,11 @@ schedule_ue_spec(module_id_t module_idP,
       }
 
       if (eNB_UE_stats == NULL) {
-	LOG_D(MAC, "[eNB] Cannot find eNB_UE_stats\n");
-	continue_flag = 1;
+          LOG_D(MAC, "[eNB] Cannot find eNB_UE_stats\n");
+          continue_flag = 1;
       }
+      if (!ue_slice_membership(UE_id, slice_idP))
+                  continue;
 
       if (continue_flag != 1) {
 	switch (get_tmode(module_idP, CC_id, UE_id)) {
@@ -643,12 +800,12 @@ schedule_ue_spec(module_id_t module_idP,
 	eNB_UE_stats->dl_cqi, MIN_CQI_VALUE, MAX_CQI_VALUE);
       */
       if (nfapi_mode) {
-	eNB_UE_stats->dlsch_mcs1 = 10;//cqi_to_mcs[ue_sched_ctl->dl_cqi[CC_id]];
+		  eNB_UE_stats->dlsch_mcs1 = 10;//cqi_to_mcs[ue_sched_ctl->dl_cqi[CC_id]];
       }
-      else {
-	eNB_UE_stats->dlsch_mcs1 = cqi_to_mcs[ue_sched_ctl->dl_cqi[CC_id]];
+      else { // this operation is also done in the preprocessor
+		  eNB_UE_stats->dlsch_mcs1 = cmin(eNB_UE_stats->dlsch_mcs1, slice_maxmcs[slice_idP]);  //cmin(eNB_UE_stats->dlsch_mcs1, openair_daq_vars.target_ue_dl_mcs);
       }
-      eNB_UE_stats->dlsch_mcs1 = eNB_UE_stats->dlsch_mcs1;	//cmin(eNB_UE_stats->dlsch_mcs1, openair_daq_vars.target_ue_dl_mcs);
+
 
 
       // store stats
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_primitives.c b/openair2/LAYER2/MAC/eNB_scheduler_primitives.c
index 166f9480e597461c29701c392f1f498854f6a7d6..341db67934ecfed3ace6cd1ece1b83a0177f0593 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler_primitives.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_primitives.c
@@ -61,6 +61,8 @@
 #define ENABLE_MAC_PAYLOAD_DEBUG
 #define DEBUG_eNB_SCHEDULER 1
 
+extern int n_active_slices;
+
 int choose(int n, int k)
 {
   int res = 1;
@@ -1739,6 +1741,7 @@ int UE_num_active_CC(UE_list_t * listP, int ue_idP)
 int UE_PCCID(module_id_t mod_idP, int ue_idP)
 //------------------------------------------------------------------------------
 {
+  if (!RC.mac || !RC.mac[mod_idP]) return 0;
   return (RC.mac[mod_idP]->UE_list.pCC_id[ue_idP]);
 }
 
@@ -1746,6 +1749,7 @@ int UE_PCCID(module_id_t mod_idP, int ue_idP)
 rnti_t UE_RNTI(module_id_t mod_idP, int ue_idP)
 //------------------------------------------------------------------------------
 {
+  if (!RC.mac || !RC.mac[mod_idP]) return 0;
   rnti_t rnti =
     RC.mac[mod_idP]->
     UE_list.UE_template[UE_PCCID(mod_idP, ue_idP)][ue_idP].rnti;
@@ -1763,6 +1767,7 @@ rnti_t UE_RNTI(module_id_t mod_idP, int ue_idP)
 boolean_t is_UE_active(module_id_t mod_idP, int ue_idP)
 //------------------------------------------------------------------------------
 {
+  if (!RC.mac || !RC.mac[mod_idP]) return 0;
   return (RC.mac[mod_idP]->UE_list.active[ue_idP]);
 }
 
@@ -4037,3 +4042,22 @@ harq_indication(module_id_t mod_idP, int CC_idP, frame_t frameP,
     sched_ctl->pucch1_cqi_update[CC_idP] = 1;
   }
 }
+
+// Flexran Slicing functions
+
+uint16_t flexran_nb_rbs_allowed_slice(float rb_percentage, int total_rbs)
+{
+    return (uint16_t) floor(rb_percentage * total_rbs);
+}
+
+int ue_slice_membership(int UE_id, int slice_id)
+{
+  if ((slice_id < 0) || (slice_id > n_active_slices))
+    LOG_W(MAC, "out of range slice id %d\n", slice_id);
+
+
+  if ((UE_id % n_active_slices) == slice_id) {
+    return 1;	// this ue is a member of this slice
+  }
+  return 0;
+}
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_ulsch.c b/openair2/LAYER2/MAC/eNB_scheduler_ulsch.c
index 7fe5e8ab901fadd778294cab7b1a40479d237318..ac87498560f9ca258460e7b9d7d5fd56ea57b3e3 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler_ulsch.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_ulsch.c
@@ -57,6 +57,13 @@
 #include "intertask_interface.h"
 #endif
 
+#include "ENB_APP/flexran_agent_defs.h"
+#include "flexran_agent_ran_api.h"
+#include "header.pb-c.h"
+#include "flexran.pb-c.h"
+#include "flexran_agent_mac.h"
+#include <dlfcn.h>
+
 #include "T.h"
 
 #define ENABLE_MAC_PAYLOAD_DEBUG
@@ -76,6 +83,37 @@ uint8_t rb_table[34] =
     36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80, 81, 90, 96, 100
   };
 
+/* number of active slices for  past and current time*/
+int n_active_slices_uplink = 1;
+int n_active_slices_current_uplink = 1;
+
+/* RB share for each slice for past and current time*/
+float avg_slice_percentage_uplink=0.25;
+float slice_percentage_uplink[MAX_NUM_SLICES] = {1.0, 0.0, 0.0, 0.0};
+float slice_percentage_current_uplink[MAX_NUM_SLICES] = {1.0, 0.0, 0.0, 0.0};
+float total_slice_percentage_uplink = 0;
+float total_slice_percentage_current_uplink = 0;
+
+// MAX MCS for each slice for past and current time
+int slice_maxmcs_uplink[MAX_NUM_SLICES] = {20, 20, 20, 20};
+int slice_maxmcs_current_uplink[MAX_NUM_SLICES] = {20,20,20,20};
+
+/*resource blocks allowed*/
+uint16_t         nb_rbs_allowed_slice_uplink[MAX_NUM_CCs][MAX_NUM_SLICES];
+/*Slice Update */
+int update_ul_scheduler[MAX_NUM_SLICES] = {1, 1, 1, 1};
+int update_ul_scheduler_current[MAX_NUM_SLICES] = {1, 1, 1, 1};
+
+/* name of available scheduler*/
+char *ul_scheduler_type[MAX_NUM_SLICES] = {"schedule_ulsch_rnti",
+					   "schedule_ulsch_rnti",
+					   "schedule_ulsch_rnti",
+					   "schedule_ulsch_rnti"
+};
+
+/* Slice Function Pointer */
+slice_scheduler_ul slice_sched_ul[MAX_NUM_SLICES] = {0};
+
 void
 rx_sdu(const module_id_t enb_mod_idP,
        const int CC_idP,
@@ -804,13 +842,13 @@ unsigned char *parse_ulsch_header(unsigned char *mac_header,
  * (done below in schedule_ulsch).
  */
 void
-set_msg3_subframe(module_id_t Mod_id,
+set_msg3_subframe(module_id_t mod_id,
 		  int CC_id,
 		  int frame,
 		  int subframe, int rnti, int Msg3_frame,
 		  int Msg3_subframe)
 {
-  eNB_MAC_INST *mac = RC.mac[Mod_id];
+  eNB_MAC_INST *mac = RC.mac[mod_id];
   int i;
   for (i = 0; i < NB_RA_PROC_MAX; i++) {
     if (mac->common_channels[CC_id].ra[i].state != IDLE &&
@@ -822,7 +860,6 @@ set_msg3_subframe(module_id_t Mod_id,
   }
 }
 
-
 void
 schedule_ulsch(module_id_t module_idP, frame_t frameP,
 	       sub_frame_t subframeP)
@@ -935,14 +972,110 @@ schedule_ulsch(module_id_t module_idP, frame_t frameP,
     }
   }
 
-  schedule_ulsch_rnti(module_idP, frameP, subframeP, sched_subframe,
-		      first_rb);
+  // perform slice-specifc operations
+
+  total_slice_percentage_uplink=0;
+  avg_slice_percentage_uplink=1.0/n_active_slices_uplink;
+
+  // reset the slice percentage for inactive slices
+  for (i = n_active_slices_uplink; i< MAX_NUM_SLICES; i++) {
+    slice_percentage_uplink[i]=0;
+  }
+  for (i = 0; i < n_active_slices_uplink; i++) {
+    if (slice_percentage_uplink[i] < 0 ){
+      LOG_W(MAC, "[eNB %d] frame %d subframe %d:invalid slice %d percentage %f. resetting to zero",
+            module_idP, frameP, subframeP, i, slice_percentage_uplink[i]);
+      slice_percentage_uplink[i]=0;
+    }
+    total_slice_percentage_uplink+=slice_percentage_uplink[i];
+  }
+
+  for (i = 0; i < n_active_slices_uplink; i++) {
+
+    // Load any updated functions
+    if (update_ul_scheduler[i] > 0 ) {
+      slice_sched_ul[i] = dlsym(NULL, ul_scheduler_type[i]);
+      update_ul_scheduler[i] = 0;
+      update_ul_scheduler_current[i] = 0;
+      //slice_percentage_current_uplink[i]= slice_percentage_uplink[i];
+      //total_slice_percentage_current_uplink+=slice_percentage_uplink[i];
+      //if (total_slice_percentage_current_uplink> 1)
+      //total_slice_percentage_current_uplink=1;
+      LOG_N(MAC,"update ul scheduler slice %d\n", i);
+    }
+    // the new total RB share is within the range
+    if (total_slice_percentage_uplink <= 1.0){
+
+      // check if the number of slices has changed, and log
+      if (n_active_slices_current_uplink != n_active_slices_uplink ){
+        if ((n_active_slices_uplink > 0) && (n_active_slices_uplink <= MAX_NUM_SLICES)) {
+          LOG_N(MAC,"[eNB %d]frame %d subframe %d: number of active UL slices has changed: %d-->%d\n",
+                module_idP, frameP, subframeP, n_active_slices_current_uplink, n_active_slices_uplink);
+          n_active_slices_current_uplink = n_active_slices_uplink;
+        } else {
+          LOG_W(MAC,"invalid number of UL slices %d, revert to the previous value %d\n",
+                n_active_slices_uplink, n_active_slices_current_uplink);
+          n_active_slices_uplink = n_active_slices_current_uplink;
+        }
+      }
+
+      // check if the slice rb share has changed, and log the console
+      if (slice_percentage_current_uplink[i] != slice_percentage_uplink[i]){
+        LOG_N(MAC,"[eNB %d][SLICE %d][UL] frame %d subframe %d: total percentage %f-->%f, slice RB percentage has changed: %f-->%f\n",
+              module_idP, i, frameP, subframeP, total_slice_percentage_current_uplink,
+              total_slice_percentage_uplink, slice_percentage_current_uplink[i], slice_percentage_uplink[i]);
+        total_slice_percentage_current_uplink = total_slice_percentage_uplink;
+        slice_percentage_current_uplink[i] = slice_percentage_uplink[i];
+      }
+
+      // check if the slice max MCS, and log the console
+      if (slice_maxmcs_current_uplink[i] != slice_maxmcs_uplink[i]){
+        if ((slice_maxmcs_uplink[i] >= 0) && (slice_maxmcs_uplink[i] <= 16)){
+          LOG_N(MAC,"[eNB %d][SLICE %d][UL] frame %d subframe %d: slice MAX MCS has changed: %d-->%d\n",
+                module_idP, i, frameP, subframeP, slice_maxmcs_current_uplink[i], slice_maxmcs_uplink[i]);
+          slice_maxmcs_current_uplink[i] = slice_maxmcs_uplink[i];
+        } else {
+          LOG_W(MAC,"[eNB %d][SLICE %d][UL] invalid slice max mcs %d, revert the previous value %d\n",
+                module_idP, i, slice_maxmcs_uplink[i],slice_maxmcs_current_uplink[i]);
+          slice_maxmcs_uplink[i] = slice_maxmcs_current_uplink[i];
+        }
+      }
+
+      // check if a new scheduler, and log the console
+      if (update_ul_scheduler_current[i] != update_ul_scheduler[i]){
+        LOG_N(MAC,"[eNB %d][SLICE %d][UL] frame %d subframe %d: UL scheduler for this slice is updated: %s \n",
+              module_idP, i, frameP, subframeP, ul_scheduler_type[i]);
+        update_ul_scheduler_current[i] = update_ul_scheduler[i];
+      }
+    } else {
+      if (n_active_slices_uplink == n_active_slices_current_uplink) {
+        LOG_W(MAC,"[eNB %d][SLICE %d][UL] invalid total RB share (%f->%f), reduce proportionally the RB share by 0.1\n",
+              module_idP, i, total_slice_percentage_current_uplink, total_slice_percentage_uplink);
+        if (slice_percentage_uplink[i] > avg_slice_percentage_uplink) {
+          slice_percentage_uplink[i] -= 0.1;
+          total_slice_percentage_uplink -= 0.1;
+        }
+      } else {
+        // here we can correct the values, e.g. reduce proportionally
+        LOG_W(MAC,"[eNB %d][SLICE %d][UL] invalid total RB share (%f->%f), revert the  number of slice to its previous value (%d->%d)\n",
+              module_idP, i, total_slice_percentage_current_uplink,
+              total_slice_percentage_uplink, n_active_slices_uplink,
+              n_active_slices_current_uplink);
+        n_active_slices_uplink = n_active_slices_current_uplink;
+        slice_percentage_uplink[i] = slice_percentage_current_uplink[i];
+      }
+    }
+
+    // Run each enabled slice-specific schedulers one by one
+    slice_sched_ul[i](module_idP, i, frameP, subframeP, sched_subframe, first_rb);
+  }
 
   stop_meas(&mac->schedule_ulsch);
 }
 
 void
 schedule_ulsch_rnti(module_id_t module_idP,
+					slice_id_t slice_id,
 		    frame_t frameP,
 		    sub_frame_t subframeP,
 		    unsigned char sched_subframeP, uint16_t * first_rb)
@@ -971,7 +1104,7 @@ schedule_ulsch_rnti(module_id_t module_idP,
   int rvidx_tab[4] = { 0, 2, 3, 1 };
 
   if (sched_subframeP < subframeP)
-    sched_frame++;
+      sched_frame++;
 
   nfapi_hi_dci0_request_t        *hi_dci0_req = &mac->HI_DCI0_req[CC_id];
   nfapi_hi_dci0_request_body_t   *hi_dci0_req_body = &hi_dci0_req->hi_dci0_request_body;
@@ -981,7 +1114,7 @@ schedule_ulsch_rnti(module_id_t module_idP,
   nfapi_ul_config_request_body_t *ul_req_tmp_body  = &ul_req_tmp->ul_config_request_body;
 
   //LOG_D(MAC, "entering ulsch preprocesor\n");
-  ulsch_scheduler_pre_processor(module_idP, frameP, subframeP, first_rb);
+  ulsch_scheduler_pre_processor(module_idP, slice_id, frameP, subframeP, first_rb);
 
   //LOG_D(MAC, "exiting ulsch preprocesor\n");
 
@@ -991,13 +1124,16 @@ schedule_ulsch_rnti(module_id_t module_idP,
   for (UE_id = UE_list->head_ul; UE_id >= 0;
        UE_id = UE_list->next_ul[UE_id]) {
 
+    if (!ue_slice_membership(UE_id, slice_id))
+        continue;
+
     // don't schedule if Msg4 is not received yet
     if (UE_list->UE_template[UE_PCCID(module_idP, UE_id)][UE_id].
-	configured == FALSE) {
-      LOG_D(MAC,
-	    "[eNB %d] frame %d subfarme %d, UE %d: not configured, skipping UE scheduling \n",
-	    module_idP, frameP, subframeP, UE_id);
-      continue;
+        configured == FALSE) {
+        LOG_D(MAC,
+              "[eNB %d] frame %d subfarme %d, UE %d: not configured, skipping UE scheduling \n",
+              module_idP, frameP, subframeP, UE_id);
+        continue;
     }
 
     rnti = UE_RNTI(module_idP, UE_id);
@@ -1153,14 +1289,13 @@ schedule_ulsch_rnti(module_id_t module_idP,
 
 	    ndi = 1 - UE_template->oldNDI_UL[harq_pid];
 	    UE_template->oldNDI_UL[harq_pid] = ndi;
-	    UE_list->eNB_UE_stats[CC_id][UE_id].
-	      normalized_rx_power = normalized_rx_power;
+	    UE_list->eNB_UE_stats[CC_id][UE_id].normalized_rx_power = normalized_rx_power;
 	    UE_list->eNB_UE_stats[CC_id][UE_id].target_rx_power = target_rx_power;
-	    UE_list->eNB_UE_stats[CC_id][UE_id].ulsch_mcs1 = UE_template->pre_assigned_mcs_ul;
-	    UE_template->mcs_UL[harq_pid] = UE_template->pre_assigned_mcs_ul;	//cmin (UE_template->pre_assigned_mcs_ul, openair_daq_vars.target_ue_ul_mcs); // adjust, based on user-defined MCS
+		UE_template->mcs_UL[harq_pid] = cmin(UE_template->pre_assigned_mcs_ul, slice_maxmcs_uplink[slice_id]);
+		UE_list->eNB_UE_stats[CC_id][UE_id].ulsch_mcs1= UE_template->mcs_UL[harq_pid];
+		//cmin (UE_template->pre_assigned_mcs_ul, openair_daq_vars.target_ue_ul_mcs); // adjust, based on user-defined MCS
 	    if (UE_template->pre_allocated_rb_table_index_ul >= 0) {
-	      rb_table_index =
-		UE_template->pre_allocated_rb_table_index_ul;
+	      rb_table_index = UE_template->pre_allocated_rb_table_index_ul;
 	    } else {
 	      UE_template->mcs_UL[harq_pid] = 10;	//cmin (10, openair_daq_vars.target_ue_ul_mcs);
 	      rb_table_index = 5;	// for PHR
@@ -1170,10 +1305,9 @@ schedule_ulsch_rnti(module_id_t module_idP,
 	    //            buffer_occupancy = UE_template->ul_total_buffer;
 
 
-	    while (((rb_table[rb_table_index] >
-		     (N_RB_UL - 1 - first_rb[CC_id]))
-		    || (rb_table[rb_table_index] > 45))
-		   && (rb_table_index > 0)) {
+	    while (((rb_table[rb_table_index] > (N_RB_UL - 1 - first_rb[CC_id]))
+                    || (rb_table[rb_table_index] > 45))
+                    && (rb_table_index > 0)) {
 	      rb_table_index--;
 	    }
 
diff --git a/openair2/LAYER2/MAC/flexran_agent_mac_proto.h b/openair2/LAYER2/MAC/flexran_agent_mac_proto.h
deleted file mode 100644
index d3170aaf919e2a24e4599399729b4078e9d6c254..0000000000000000000000000000000000000000
--- a/openair2/LAYER2/MAC/flexran_agent_mac_proto.h
+++ /dev/null
@@ -1,216 +0,0 @@
-/*
- * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The OpenAirInterface Software Alliance licenses this file to You under
- * the OAI Public License, Version 1.1  (the "License"); you may not use this file
- * except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.openairinterface.org/?page_id=698
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *-------------------------------------------------------------------------------
- * For more information about the OpenAirInterface (OAI) Software Alliance:
- *      contact@openairinterface.org
- */
-
-/*! \file flexran_agent_mac_proto.h
- * \brief MAC functions for FlexRAN agent
- * \author Xenofon Foukas and Navid Nikaein
- * \date 2016
- * \email: x.foukas@sms.ed.ac.uk
- * \version 0.1
- * @ingroup _mac
-
- */
-
-#ifndef __LAYER2_MAC_FLEXRAN_AGENT_MAC_PROTO_H__
-#define __LAYER2_MAC_FLEXRAN_AGENT_MAC_PROTO_H__
-
-#include "flexran_agent_defs.h"
-#include "header.pb-c.h"
-#include "flexran.pb-c.h"
-
-/*
- * slice specific scheduler 
- */
-typedef void (*slice_scheduler) (module_id_t mod_id,
-				 int slice_id,
-				 uint32_t frame,
-				 uint32_t subframe,
-				 int *mbsfn_flag,
-				 Protocol__FlexranMessage ** dl_info);
-
-
-
-/*
- * top level flexran scheduler used by the eNB scheduler
- */
-void flexran_schedule_ue_spec_default(mid_t mod_id,
-				      uint32_t frame,
-				      uint32_t subframe,
-				      int *mbsfn_flag,
-				      Protocol__FlexranMessage ** dl_info);
-/*
- * slice specific scheduler for embb
- */
-void
-flexran_schedule_ue_spec_embb(mid_t mod_id,
-			      int slice_id,
-			      uint32_t frame,
-			      uint32_t subframe,
-			      int *mbsfn_flag,
-			      Protocol__FlexranMessage ** dl_info);
-/*
- * slice specific scheduler for urllc
- */
-void
-flexran_schedule_ue_spec_urllc(mid_t mod_id,
-			       int slice_id,
-			       uint32_t frame,
-			       uint32_t subframe,
-			       int *mbsfn_flag,
-			       Protocol__FlexranMessage ** dl_info);
-
-/*
- * slice specific scheduler for mmtc
- */
-void
-flexran_schedule_ue_spec_mmtc(mid_t mod_id,
-			      int slice_id,
-			      uint32_t frame,
-			      uint32_t subframe,
-			      int *mbsfn_flag,
-			      Protocol__FlexranMessage ** dl_info);
-/*
- * slice specific scheduler for best effort traffic 
- */
-void
-flexran_schedule_ue_spec_be(mid_t mod_id,
-			    int slice_id,
-			    uint32_t frame,
-			    uint32_t subframe,
-			    int *mbsfn_flag,
-			    Protocol__FlexranMessage ** dl_info);
-
-/*
- * common flexran scheduler function
- */
-void
-flexran_schedule_ue_spec_common(mid_t mod_id,
-				int slice_id,
-				uint32_t frame,
-				uint32_t subframe,
-				int *mbsfn_flag,
-				Protocol__FlexranMessage ** dl_info);
-
-uint16_t flexran_nb_rbs_allowed_slice(float rb_percentage, int total_rbs);
-
-int flexran_slice_member(int UE_id, int slice_id);
-
-int flexran_slice_maxmcs(int slice_id);
-
-void _store_dlsch_buffer(module_id_t Mod_id,
-			 int slice_id,
-			 frame_t frameP, sub_frame_t subframeP);
-
-
-void _assign_rbs_required(module_id_t Mod_id,
-			  int slice_id,
-			  frame_t frameP,
-			  sub_frame_t subframe,
-			  uint16_t
-			  nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-			  uint16_t
-			  nb_rbs_allowed_slice[MAX_NUM_CCs]
-			  [MAX_NUM_SLICES], int min_rb_unit[MAX_NUM_CCs]);
-
-void _dlsch_scheduler_pre_processor(module_id_t Mod_id,
-				    int slice_id,
-				    frame_t frameP,
-				    sub_frame_t subframeP,
-				    int N_RBG[MAX_NUM_CCs],
-				    int *mbsfn_flag);
-
-void _dlsch_scheduler_pre_processor_reset(int module_idP,
-					  int UE_id,
-					  uint8_t CC_id,
-					  int frameP,
-					  int subframeP,
-					  int N_RBG,
-					  uint16_t
-					  nb_rbs_required[MAX_NUM_CCs]
-					  [NUMBER_OF_UE_MAX],
-					  uint16_t
-					  nb_rbs_required_remaining
-					  [MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-					  uint16_t
-					  nb_rbs_allowed_slice[MAX_NUM_CCs]
-					  [MAX_NUM_SLICES],
-					  unsigned char
-					  rballoc_sub[MAX_NUM_CCs]
-					  [N_RBG_MAX],
-					  unsigned char
-					  MIMO_mode_indicator[MAX_NUM_CCs]
-					  [N_RBG_MAX]);
-
-void _dlsch_scheduler_pre_processor_allocate(module_id_t Mod_id,
-					     int UE_id,
-					     uint8_t CC_id,
-					     int N_RBG,
-					     int transmission_mode,
-					     int min_rb_unit,
-					     uint8_t N_RB_DL,
-					     uint16_t
-					     nb_rbs_required[MAX_NUM_CCs]
-					     [NUMBER_OF_UE_MAX],
-					     uint16_t
-					     nb_rbs_required_remaining
-					     [MAX_NUM_CCs]
-					     [NUMBER_OF_UE_MAX],
-					     unsigned char
-					     rballoc_sub[MAX_NUM_CCs]
-					     [N_RBG_MAX],
-					     unsigned char
-					     MIMO_mode_indicator
-					     [MAX_NUM_CCs][N_RBG_MAX]);
-
-/*
- * Default scheduler used by the eNB agent
- */
-void flexran_schedule_ue_spec_default(mid_t mod_id, uint32_t frame,
-				      uint32_t subframe, int *mbsfn_flag,
-				      Protocol__FlexranMessage ** dl_info);
-
-/*
- * Data plane function for applying the DL decisions of the scheduler
- */
-void flexran_apply_dl_scheduling_decisions(mid_t mod_id, uint32_t frame,
-					   uint32_t subframe,
-					   int *mbsfn_flag,
-					   Protocol__FlexranMessage *
-					   dl_scheduling_info);
-
-/*
- * Data plane function for applying the UE specific DL decisions of the scheduler
- */
-void flexran_apply_ue_spec_scheduling_decisions(mid_t mod_id,
-						uint32_t frame,
-						uint32_t subframe,
-						int *mbsfn_flag,
-						uint32_t n_dl_ue_data,
-						Protocol__FlexDlData **
-						dl_ue_data);
-
-/*
- * Data plane function for filling the DCI structure
- */
-void flexran_fill_oai_dci(mid_t mod_id, uint32_t CC_id, uint32_t rnti,
-			  Protocol__FlexDlDci * dl_dci);
-
-#endif
diff --git a/openair2/LAYER2/MAC/flexran_agent_scheduler_dataplane.c b/openair2/LAYER2/MAC/flexran_agent_scheduler_dataplane.c
deleted file mode 100644
index 44052a8749191780e5c6c62c39935bfa91081792..0000000000000000000000000000000000000000
--- a/openair2/LAYER2/MAC/flexran_agent_scheduler_dataplane.c
+++ /dev/null
@@ -1,588 +0,0 @@
-/*
- * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The OpenAirInterface Software Alliance licenses this file to You under
- * the OAI Public License, Version 1.1  (the "License"); you may not use this file
- * except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.openairinterface.org/?page_id=698
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *-------------------------------------------------------------------------------
- * For more information about the OpenAirInterface (OAI) Software Alliance:
- *      contact@openairinterface.org
- */
-
-/*! \file flexran_agent_scheduler_dataplane.c
- * \brief data plane procedures related to eNB scheduling
- * \author Xenofon Foukas
- * \date 2016
- * \email: x.foukas@sms.ed.ac.uk
- * \version 0.1
- * @ingroup _mac
-
- */
-
-#include "assertions.h"
-#include "PHY/defs.h"
-#include "PHY/extern.h"
-
-#include "SCHED/defs.h"
-#include "SCHED/extern.h"
-
-#include "LAYER2/MAC/flexran_agent_mac_proto.h"
-#include "LAYER2/MAC/defs.h"
-#include "LAYER2/MAC/proto.h"
-#include "LAYER2/MAC/extern.h"
-#include "LAYER2/MAC/flexran_dci_conversions.h"
-
-#include "UTIL/LOG/log.h"
-#include "UTIL/LOG/vcd_signal_dumper.h"
-#include "UTIL/OPT/opt.h"
-#include "OCG.h"
-#include "OCG_extern.h"
-
-#include "RRC/LITE/extern.h"
-#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
-
-#include "header.pb-c.h"
-#include "flexran.pb-c.h"
-#include "flexran_agent_extern.h"
-
-#include "flexran_agent_common.h"
-
-#include "SIMULATION/TOOLS/defs.h"	// for taus
-
-void
-flexran_apply_dl_scheduling_decisions(mid_t mod_id,
-				      uint32_t frame,
-				      uint32_t subframe,
-				      int *mbsfn_flag,
-				      Protocol__FlexranMessage *
-				      dl_scheduling_info)
-{
-
-    Protocol__FlexDlMacConfig *mac_config =
-	dl_scheduling_info->dl_mac_config_msg;
-
-    // Check if there is anything to schedule for random access
-    if (mac_config->n_dl_rar > 0) {
-	/*TODO: call the random access data plane function */
-    }
-    // Check if there is anything to schedule for paging/broadcast
-    if (mac_config->n_dl_broadcast > 0) {
-	/*TODO: call the broadcast/paging data plane function */
-    }
-    // Check if there is anything to schedule for the UEs
-    if (mac_config->n_dl_ue_data > 0) {
-	flexran_apply_ue_spec_scheduling_decisions(mod_id, frame, subframe,
-						   mbsfn_flag,
-						   mac_config->
-						   n_dl_ue_data,
-						   mac_config->dl_ue_data);
-    }
-
-}
-
-
-void
-flexran_apply_ue_spec_scheduling_decisions(mid_t mod_id,
-					   uint32_t frame,
-					   uint32_t subframe,
-					   int *mbsfn_flag,
-					   uint32_t n_dl_ue_data,
-					   Protocol__FlexDlData **
-					   dl_ue_data)
-{
-
-    uint8_t CC_id;
-    int UE_id;
-    mac_rlc_status_resp_t rlc_status;
-    unsigned char ta_len = 0;
-    unsigned char header_len = 0, header_len_tmp = 0;
-    unsigned char sdu_lcids[11], offset, num_sdus = 0;
-    uint16_t nb_rb;
-    uint16_t TBS, sdu_lengths[11], rnti, padding = 0, post_padding = 0;
-    unsigned char dlsch_buffer[MAX_DLSCH_PAYLOAD_BYTES];
-    uint8_t round = 0;
-    uint8_t harq_pid = 0;
-    //  LTE_DL_FRAME_PARMS   *frame_parms[MAX_NUM_CCs];
-    LTE_eNB_UE_stats *eNB_UE_stats = NULL;
-    uint16_t sdu_length_total = 0;
-    short ta_update = 0;
-    eNB_MAC_INST *eNB = &eNB_mac_inst[mod_id];
-    UE_list_t *UE_list = &eNB->UE_list;
-    //  static int32_t          tpc_accumulated=0;
-    UE_sched_ctrl *ue_sched_ctl;
-
-    int last_sdu_header_len = 0;
-
-    int i, j;
-
-    Protocol__FlexDlData *dl_data;
-    Protocol__FlexDlDci *dl_dci;
-
-    uint32_t rlc_size, n_lc, lcid;
-
-    // For each UE-related command
-    for (i = 0; i < n_dl_ue_data; i++) {
-
-	dl_data = dl_ue_data[i];
-	dl_dci = dl_data->dl_dci;
-
-	CC_id = dl_data->serv_cell_index;
-	//    frame_parms[CC_id] = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-
-	rnti = dl_data->rnti;
-	UE_id = find_ue(rnti, PHY_vars_eNB_g[mod_id][CC_id]);
-
-	ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-	eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
-
-	round = dl_dci->rv[0];
-	harq_pid = dl_dci->harq_process;
-
-	//LOG_I(FLEXRAN_AGENT, "[Frame %d][Subframe %d] Scheduling harq %d\n", frame, subframe, harq_pid);
-	//    LOG_I(FLEXRAN_AGENT, "[Frame %d][Subframe %d]Now scheduling harq_pid %d (round %d)\n", frame, subframe, harq_pid, round);
-
-	// If this is a new transmission
-	if (round == 0) {
-	    // First we have to deal with the creation of the PDU based on the message instructions
-	    rlc_status.bytes_in_buffer = 0;
-
-	    TBS = dl_dci->tbs_size[0];
-
-	    if (dl_data->n_ce_bitmap > 0) {
-		//Check if there is TA command and set the length appropriately
-		ta_len =
-		    (dl_data->
-		     ce_bitmap[0] & PROTOCOL__FLEX_CE_TYPE__FLPCET_TA) ? 2
-		    : 0;
-	    }
-
-	    num_sdus = 0;
-	    sdu_length_total = 0;
-
-	    n_lc = dl_data->n_rlc_pdu;
-	    // Go through each one of the channel commands and create SDUs
-	    header_len = 0;
-	    last_sdu_header_len = 0;
-	    for (j = 0; j < n_lc; j++) {
-		sdu_lengths[j] = 0;
-		lcid =
-		    dl_data->rlc_pdu[j]->rlc_pdu_tb[0]->logical_channel_id;
-		rlc_size = dl_data->rlc_pdu[j]->rlc_pdu_tb[0]->size;
-		LOG_D(MAC,
-		      "[TEST] [eNB %d] [Frame %d] [Subframe %d], LCID %d, CC_id %d, Requesting %d bytes from RLC (RRC message)\n",
-		      mod_id, frame, subframe, lcid, CC_id, rlc_size);
-		if (rlc_size > 0) {
-
-		    rlc_status = mac_rlc_status_ind(mod_id,
-						    rnti,
-						    mod_id,
-						    frame,
-						    subframe,
-						    ENB_FLAG_YES,
-						    MBMS_FLAG_NO, lcid, 0);
-
-		    if (rlc_status.bytes_in_buffer > 0) {
-
-			if (rlc_status.bytes_in_buffer < rlc_size) {
-			    rlc_size = rlc_status.bytes_in_buffer;
-			}
-
-			if (rlc_size <= 2) {
-			    rlc_size = 3;
-			}
-
-			rlc_status = mac_rlc_status_ind(mod_id, rnti, mod_id, frame, subframe, ENB_FLAG_YES, MBMS_FLAG_NO, lcid, rlc_size);	// transport block set size
-
-			LOG_D(MAC,
-			      "[TEST] RLC can give %d bytes for LCID %d during second call\n",
-			      rlc_status.bytes_in_buffer, lcid);
-
-			if (rlc_status.bytes_in_buffer > 0) {
-
-			    sdu_lengths[j] = mac_rlc_data_req(mod_id, rnti, mod_id, frame, ENB_FLAG_YES, MBMS_FLAG_NO, lcid, rlc_size,	//not used
-							      (char *)
-							      &dlsch_buffer
-							      [sdu_length_total]);
-
-			    LOG_D(MAC,
-				  "[eNB %d][LCID %d] CC_id %d Got %d bytes from RLC\n",
-				  mod_id, lcid, CC_id, sdu_lengths[j]);
-			    sdu_length_total += sdu_lengths[j];
-			    sdu_lcids[j] = lcid;
-
-			    UE_list->
-				eNB_UE_stats[CC_id][UE_id].num_pdu_tx[lcid]
-				+= 1;
-			    UE_list->
-				eNB_UE_stats[CC_id][UE_id].num_bytes_tx
-				[lcid] += sdu_lengths[j];
-
-			    if (sdu_lengths[j] < 128) {
-				header_len += 2;
-				last_sdu_header_len = 2;
-			    } else {
-				header_len += 3;
-				last_sdu_header_len = 3;
-			    }
-			    num_sdus++;
-			}
-		    }
-		}
-	    }			// SDU creation end
-
-
-	    if (((sdu_length_total + header_len + ta_len) > 0)) {
-
-		header_len_tmp = header_len;
-
-		// If we have only a single SDU, header length becomes 1
-		if ((num_sdus) == 1) {
-		    //if (header_len == 2 || header_len == 3) {
-		    header_len = 1;
-		} else {
-		    header_len = (header_len - last_sdu_header_len) + 1;
-		}
-
-		// If we need a 1 or 2 bit padding or no padding at all
-		if ((TBS - header_len - sdu_length_total - ta_len) <= 2 || (TBS - header_len - sdu_length_total - ta_len) > TBS) {	//protect from overflow
-		    padding =
-			(TBS - header_len - sdu_length_total - ta_len);
-		    post_padding = 0;
-		} else {	// The last sdu needs to have a length field, since we add padding
-		    padding = 0;
-		    header_len = header_len_tmp;
-		    post_padding = TBS - sdu_length_total - header_len - ta_len;	// 1 is for the postpadding header
-		}
-
-		if (ta_len > 0) {
-		    // Reset the measurement
-		    ta_update = flexran_get_TA(mod_id, UE_id, CC_id);
-		    ue_sched_ctl->ta_timer = 20;
-		    eNB_UE_stats->timing_advance_update = 0;
-		} else {
-		    ta_update = 0;
-		}
-
-		// If there is nothing to schedule, just leave
-		if ((sdu_length_total) <= 0) {
-		    harq_pid_updated[UE_id][harq_pid] = 1;
-		    harq_pid_round[UE_id][harq_pid] = 0;
-		    continue;
-		}
-		//      LOG_I(FLEXRAN_AGENT, "[Frame %d][Subframe %d] TBS is %d and bytes are %d\n", frame, subframe, TBS, sdu_length_total);
-
-		offset = generate_dlsch_header((unsigned char *) UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0], num_sdus,	//num_sdus
-					       sdu_lengths,	//
-					       sdu_lcids, 255,	// no drx
-					       ta_update,	// timing advance
-					       NULL,	// contention res id
-					       padding, post_padding);
-
-
-
-
-
-#ifdef DEBUG_eNB_SCHEDULER
-		LOG_T(MAC, "[eNB %d] First 16 bytes of DLSCH : \n");
-
-		for (i = 0; i < 16; i++) {
-		    LOG_T(MAC, "%x.", dlsch_buffer[i]);
-		}
-
-		LOG_T(MAC, "\n");
-#endif
-		// cycle through SDUs and place in dlsch_buffer
-		memcpy(&UE_list->DLSCH_pdu[CC_id][0][UE_id].
-		       payload[0][offset], dlsch_buffer, sdu_length_total);
-		// memcpy(&eNB_mac_inst[0].DLSCH_pdu[0][0].payload[0][offset],dcch_buffer,sdu_lengths[0]);
-
-		// fill remainder of DLSCH with random data
-		for (j = 0; j < (TBS - sdu_length_total - offset); j++) {
-		    UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0][offset +
-								   sdu_length_total
-								   + j] =
-			(char) (taus() & 0xff);
-		}
-
-		//eNB_mac_inst[0].DLSCH_pdu[0][0].payload[0][offset+sdu_lengths[0]+j] = (char)(taus()&0xff);
-		if (opt_enabled == 1) {
-		    trace_pdu(1,
-			      (uint8_t *)
-			      UE_list->DLSCH_pdu[CC_id][0][UE_id].
-			      payload[0], TBS, mod_id, 3, UE_RNTI(mod_id,
-								  UE_id),
-			      eNB->frame, eNB->subframe, 0, 0);
-		    LOG_D(OPT,
-			  "[eNB %d][DLSCH] CC_id %d Frame %d  rnti %x  with size %d\n",
-			  mod_id, CC_id, frame, UE_RNTI(mod_id, UE_id),
-			  TBS);
-		}
-		// store stats
-		eNB->eNB_stats[CC_id].dlsch_bytes_tx += sdu_length_total;
-		eNB->eNB_stats[CC_id].dlsch_pdus_tx += 1;
-		UE_list->eNB_UE_stats[CC_id][UE_id].dl_cqi =
-		    eNB_UE_stats->DL_cqi[0];
-
-		UE_list->eNB_UE_stats[CC_id][UE_id].crnti = rnti;
-		UE_list->eNB_UE_stats[CC_id][UE_id].rrc_status =
-		    mac_eNB_get_rrc_status(mod_id, rnti);
-		UE_list->eNB_UE_stats[CC_id][UE_id].harq_pid = harq_pid;
-		UE_list->eNB_UE_stats[CC_id][UE_id].harq_round = round;
-
-		//nb_rb = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
-		//Find the number of resource blocks and set them to the template for retransmissions
-		nb_rb = get_min_rb_unit(mod_id, CC_id);
-		uint16_t stats_tbs =
-		    mac_xface->get_TBS_DL(dl_dci->mcs[0], nb_rb);
-
-		while (stats_tbs < TBS) {
-		    nb_rb += get_min_rb_unit(mod_id, CC_id);
-		    stats_tbs =
-			mac_xface->get_TBS_DL(dl_dci->mcs[0], nb_rb);
-		}
-
-		//      LOG_I(FLEXRAN_AGENT, "The MCS was %d\n", dl_dci->mcs[0]);
-
-		UE_list->eNB_UE_stats[CC_id][UE_id].rbs_used = nb_rb;
-		UE_list->eNB_UE_stats[CC_id][UE_id].total_rbs_used +=
-		    nb_rb;
-		UE_list->eNB_UE_stats[CC_id][UE_id].dlsch_mcs1 =
-		    dl_dci->mcs[0];
-		UE_list->eNB_UE_stats[CC_id][UE_id].dlsch_mcs2 =
-		    dl_dci->mcs[0];
-		UE_list->eNB_UE_stats[CC_id][UE_id].TBS = TBS;
-
-		UE_list->eNB_UE_stats[CC_id][UE_id].overhead_bytes =
-		    TBS - sdu_length_total;
-		UE_list->eNB_UE_stats[CC_id][UE_id].total_sdu_bytes +=
-		    sdu_length_total;
-		UE_list->eNB_UE_stats[CC_id][UE_id].total_pdu_bytes += TBS;
-		UE_list->eNB_UE_stats[CC_id][UE_id].total_num_pdus += 1;
-
-		//eNB_UE_stats->dlsch_mcs1 = cqi_to_mcs[eNB_UE_stats->DL_cqi[0]];
-		//eNB_UE_stats->dlsch_mcs1 = cmin(eNB_UE_stats->dlsch_mcs1, openair_daq_vars.target_ue_dl_mcs);
-	    } else {
-		LOG_D(FLEXRAN_AGENT,
-		      "No need to schedule a dci after all. Just drop it\n");
-		harq_pid_updated[UE_id][harq_pid] = 1;
-		harq_pid_round[UE_id][harq_pid] = 0;
-		continue;
-	    }
-	} else {
-	    // No need to create anything apart of DCI in case of retransmission
-	    /*TODO: Must add these */
-	    //      eNB_UE_stats->dlsch_trials[round]++;
-	    //UE_list->eNB_UE_stats[CC_id][UE_id].num_retransmission+=1;
-	    //UE_list->eNB_UE_stats[CC_id][UE_id].rbs_used_retx=nb_rb;
-	    //UE_list->eNB_UE_stats[CC_id][UE_id].total_rbs_used_retx+=nb_rb;
-	    //UE_list->eNB_UE_stats[CC_id][UE_id].ncce_used_retx=nCCECC_id];
-	}
-
-	//    UE_list->UE_template[CC_id][UE_id].oldNDI[dl_dci->harq_process] = dl_dci->ndi[0];
-	//    eNB_UE_stats->dlsch_mcs1 = dl_dci->mcs[0];
-
-	//Fill the proper DCI of OAI
-	flexran_fill_oai_dci(mod_id, CC_id, rnti, dl_dci);
-    }
-}
-
-void
-flexran_fill_oai_dci(mid_t mod_id, uint32_t CC_id, uint32_t rnti,
-		     Protocol__FlexDlDci * dl_dci)
-{
-
-    void *DLSCH_dci = NULL;
-    DCI_PDU *DCI_pdu;
-
-    unsigned char harq_pid = 0;
-    //  unsigned char round = 0;
-    LTE_DL_FRAME_PARMS *frame_parms[MAX_NUM_CCs];
-    int size_bits = 0, size_bytes = 0;
-    eNB_MAC_INST *eNB = &eNB_mac_inst[mod_id];
-    UE_list_t *UE_list = &eNB->UE_list;
-    LTE_eNB_UE_stats *eNB_UE_stats = NULL;
-
-    int UE_id = find_ue(rnti, PHY_vars_eNB_g[mod_id][CC_id]);
-
-    uint32_t format;
-
-    harq_pid = dl_dci->harq_process;
-    //  round = dl_dci->rv[0];
-
-    // Note this code is for a specific DCI format
-    DLSCH_dci =
-	(void *) UE_list->UE_template[CC_id][UE_id].DLSCH_DCI[harq_pid];
-    DCI_pdu = &eNB->common_channels[CC_id].DCI_pdu;
-
-    frame_parms[CC_id] = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-
-    if (dl_dci->has_tpc == 1) {
-	// Check if tpc has been set and reset measurement */
-	if ((dl_dci->tpc == 0) || (dl_dci->tpc == 2)) {
-	    eNB_UE_stats =
-		mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
-	    eNB_UE_stats->Po_PUCCH_update = 0;
-	}
-    }
-
-
-    switch (frame_parms[CC_id]->N_RB_DL) {
-    case 6:
-	if (frame_parms[CC_id]->frame_type == TDD) {
-	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-		FILL_DCI_TDD_1(DCI1_1_5MHz_TDD_t, DLSCH_dci, dl_dci);
-		size_bytes = sizeof(DCI1_1_5MHz_TDD_t);
-		size_bits = sizeof_DCI1_1_5MHz_TDD_t;
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-		//TODO
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-		//TODO
-	    }
-	} else {
-	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-		FILL_DCI_FDD_1(DCI1_1_5MHz_FDD_t, DLSCH_dci, dl_dci);
-		size_bytes = sizeof(DCI1_1_5MHz_FDD_t);
-		size_bits = sizeof_DCI1_1_5MHz_FDD_t;
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-		//TODO
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-		//TODO
-	    }
-	}
-	break;
-    case 25:
-	if (frame_parms[CC_id]->frame_type == TDD) {
-	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-		FILL_DCI_TDD_1(DCI1_5MHz_TDD_t, DLSCH_dci, dl_dci);
-		size_bytes = sizeof(DCI1_5MHz_TDD_t);
-		size_bits = sizeof_DCI1_5MHz_TDD_t;
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-		//TODO
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-		//TODO
-	    }
-	} else {
-	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-		FILL_DCI_FDD_1(DCI1_5MHz_FDD_t, DLSCH_dci, dl_dci);
-		size_bytes = sizeof(DCI1_5MHz_FDD_t);
-		size_bits = sizeof_DCI1_5MHz_FDD_t;
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-		//TODO
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-		//TODO
-	    }
-	}
-	break;
-    case 50:
-	if (frame_parms[CC_id]->frame_type == TDD) {
-	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-		FILL_DCI_TDD_1(DCI1_10MHz_TDD_t, DLSCH_dci, dl_dci);
-		size_bytes = sizeof(DCI1_10MHz_TDD_t);
-		size_bits = sizeof_DCI1_10MHz_TDD_t;
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-		//TODO
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-		//TODO
-	    }
-	} else {
-	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-		FILL_DCI_FDD_1(DCI1_10MHz_FDD_t, DLSCH_dci, dl_dci);
-		size_bytes = sizeof(DCI1_10MHz_FDD_t);
-		size_bits = sizeof_DCI1_10MHz_FDD_t;
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-		//TODO
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-		//TODO
-	    }
-	}
-	break;
-    case 100:
-	if (frame_parms[CC_id]->frame_type == TDD) {
-	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-		FILL_DCI_TDD_1(DCI1_20MHz_TDD_t, DLSCH_dci, dl_dci);
-		size_bytes = sizeof(DCI1_20MHz_TDD_t);
-		size_bits = sizeof_DCI1_20MHz_TDD_t;
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-		//TODO
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-		//TODO
-	    }
-	} else {
-	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-		FILL_DCI_FDD_1(DCI1_20MHz_FDD_t, DLSCH_dci, dl_dci);
-		size_bytes = sizeof(DCI1_20MHz_FDD_t);
-		size_bits = sizeof_DCI1_20MHz_FDD_t;
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-		//TODO
-	    } else if (dl_dci->format ==
-		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-		//TODO
-	    }
-	}
-	break;
-    }
-
-    //Set format to the proper type
-    switch (dl_dci->format) {
-    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1:
-	format = format1;
-	break;
-    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1A:
-	format = format1A;
-	break;
-    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1B:
-	format = format1B;
-	break;
-    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1C:
-	format = format1C;
-	break;
-    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D:
-	format = format1E_2A_M10PRB;
-	break;
-    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2:
-	format = format2;
-	break;
-    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A:
-	format = format2A;
-	break;
-    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2B:
-	format = format2B;
-	break;
-    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_3:
-	format = 3;
-	break;
-    default:
-	/*TODO: Need to deal with unsupported DCI type */
-	return;
-    }
-
-    add_ue_spec_dci(DCI_pdu,
-		    DLSCH_dci,
-		    rnti,
-		    size_bytes, dl_dci->aggr_level, size_bits, format, 0);
-}
diff --git a/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue.c b/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue.c
deleted file mode 100644
index 3a491bd7ea8488d31953d8084935517a6ab81e19..0000000000000000000000000000000000000000
--- a/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue.c
+++ /dev/null
@@ -1,2005 +0,0 @@
-/*
- * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The OpenAirInterface Software Alliance licenses this file to You under
- * the OAI Public License, Version 1.1  (the "License"); you may not use this file
- * except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.openairinterface.org/?page_id=698
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *-------------------------------------------------------------------------------
- * For more information about the OpenAirInterface (OAI) Software Alliance:
- *      contact@openairinterface.org
- */
-
-/*! \file flexran_agent_scheduler_dlsch_ue.c
- * \brief procedures related to eNB for the DLSCH transport channel
- * \author Xenofon Foukas, Navid Nikaein and Raymond Knopp
- * \date 2016
- * \email: x.foukas@sms.ed.ac.uk
- * \version 0.1
- * @ingroup _mac
-
- */
-
-#include "assertions.h"
-#include "PHY/defs.h"
-#include "PHY/extern.h"
-
-#include "SCHED/defs.h"
-#include "SCHED/extern.h"
-
-#include "LAYER2/MAC/flexran_agent_mac_proto.h"
-#include "LAYER2/MAC/defs.h"
-#include "LAYER2/MAC/proto.h"
-#include "LAYER2/MAC/extern.h"
-#include "UTIL/LOG/log.h"
-#include "UTIL/LOG/vcd_signal_dumper.h"
-#include "UTIL/OPT/opt.h"
-#include "OCG.h"
-#include "OCG_extern.h"
-
-#include "RRC/LITE/extern.h"
-#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
-
-#include "ENB_APP/flexran_agent_defs.h"
-
-#include "pdcp.h"
-
-#include "header.pb-c.h"
-#include "flexran.pb-c.h"
-#include "flexran_agent_mac.h"
-#include <dlfcn.h>
-
-#include "SIMULATION/TOOLS/defs.h"	// for taus
-
-#if defined(ENABLE_ITTI)
-#include "intertask_interface.h"
-#endif
-
-#define ENABLE_MAC_PAYLOAD_DEBUG
-
-/**
- * Local variables to support slicing
- * 
- */
-
-
-/*!\brief  UE ULSCH scheduling states*/
-typedef enum {
-    MIN_SLICE_STRATEGY = 0,
-    SLICE_MASK,
-    UEID_TO_SLICEID,
-    MAX_SLICE_STRATEGY
-} SLICING_STRATEGY;
-
-// this assumes a max of of 16 UE per eNB/CC
-#define SLICE0_MASK 0x000f
-#define SLICE1_MASK 0x00f0
-#define SLICE2_MASK 0x0f00
-#define SLICE3_MASK 0xf000
-
-
-// number of active slices for  past and current time
-int n_active_slices = 1;
-int n_active_slices_current = 1;
-
-// ue to slice mapping
-int slicing_strategy = UEID_TO_SLICEID;
-int slicing_strategy_current = UEID_TO_SLICEID;
-
-// RB share for each slice for past and current time
-float slice_percentage[MAX_NUM_SLICES] = { 1.0, 0.0, 0.0, 0.0 };
-float slice_percentage_current[MAX_NUM_SLICES] = { 1.0, 0.0, 0.0, 0.0 };
-
-float total_slice_percentage = 0;
-
-// MAX MCS for each slice for past and current time
-int slice_maxmcs[MAX_NUM_SLICES] = { 28, 28, 28, 28 };
-int slice_maxmcs_current[MAX_NUM_SLICES] = { 28, 28, 28, 28 };
-
-int update_dl_scheduler[MAX_NUM_SLICES] = { 1, 1, 1, 1 };
-int update_dl_scheduler_current[MAX_NUM_SLICES] = { 1, 1, 1, 1 };
-
-// name of available scheduler
-char *dl_scheduler_type[MAX_NUM_SLICES] =
-    { "flexran_schedule_ue_spec_embb",
-    "flexran_schedule_ue_spec_urllc",
-    "flexran_schedule_ue_spec_mmtc",
-    "flexran_schedule_ue_spec_be"	// best effort 
-};
-
-// pointer to the slice specific scheduler 
-slice_scheduler slice_sched[MAX_NUM_SLICES] = { 0 };
-
-
-/**
- * preprocessor functions for scheduling
- *
- */
-
-
-// This function stores the downlink buffer for all the logical channels
-void
-_store_dlsch_buffer(module_id_t Mod_id,
-		    int slice_id, frame_t frameP, sub_frame_t subframeP)
-{
-
-    int UE_id, i;
-    rnti_t rnti;
-    mac_rlc_status_resp_t rlc_status;
-    UE_list_t *UE_list = &eNB_mac_inst[Mod_id].UE_list;
-    UE_TEMPLATE *UE_template;
-
-    for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
-	if (UE_list->active[UE_id] != TRUE)
-	    continue;
-
-	if (flexran_slice_member(UE_id, slice_id) == 0)
-	    continue;
-
-	UE_template =
-	    &UE_list->UE_template[UE_PCCID(Mod_id, UE_id)][UE_id];
-
-	// clear logical channel interface variables
-	UE_template->dl_buffer_total = 0;
-	UE_template->dl_pdus_total = 0;
-
-	for (i = 0; i < MAX_NUM_LCID; i++) {
-	    UE_template->dl_buffer_info[i] = 0;
-	    UE_template->dl_pdus_in_buffer[i] = 0;
-	    UE_template->dl_buffer_head_sdu_creation_time[i] = 0;
-	    UE_template->dl_buffer_head_sdu_remaining_size_to_send[i] = 0;
-	}
-
-	rnti = UE_RNTI(Mod_id, UE_id);
-
-	for (i = 0; i < MAX_NUM_LCID; i++) {	// loop over all the logical channels
-
-	    rlc_status =
-		mac_rlc_status_ind(Mod_id, rnti, Mod_id, frameP, subframeP,
-				   ENB_FLAG_YES, MBMS_FLAG_NO, i, 0);
-	    UE_template->dl_buffer_info[i] = rlc_status.bytes_in_buffer;	//storing the dlsch buffer for each logical channel
-	    UE_template->dl_pdus_in_buffer[i] = rlc_status.pdus_in_buffer;
-	    UE_template->dl_buffer_head_sdu_creation_time[i] =
-		rlc_status.head_sdu_creation_time;
-	    UE_template->dl_buffer_head_sdu_creation_time_max =
-		cmax(UE_template->dl_buffer_head_sdu_creation_time_max,
-		     rlc_status.head_sdu_creation_time);
-	    UE_template->dl_buffer_head_sdu_remaining_size_to_send[i] =
-		rlc_status.head_sdu_remaining_size_to_send;
-	    UE_template->dl_buffer_head_sdu_is_segmented[i] =
-		rlc_status.head_sdu_is_segmented;
-	    UE_template->dl_buffer_total += UE_template->dl_buffer_info[i];	//storing the total dlsch buffer
-	    UE_template->dl_pdus_total +=
-		UE_template->dl_pdus_in_buffer[i];
-
-#ifdef DEBUG_eNB_SCHEDULER
-
-	    /* note for dl_buffer_head_sdu_remaining_size_to_send[i] :
-	     * 0 if head SDU has not been segmented (yet), else remaining size not already segmented and sent
-	     */
-	    if (UE_template->dl_buffer_info[i] > 0)
-		LOG_D(MAC,
-		      "[eNB %d][SLICE %d] Frame %d Subframe %d : RLC status for UE %d in LCID%d: total of %d pdus and size %d, head sdu queuing time %d, remaining size %d, is segmeneted %d \n",
-		      Mod_id, slice_id, frameP, subframeP, UE_id,
-		      i, UE_template->dl_pdus_in_buffer[i],
-		      UE_template->dl_buffer_info[i],
-		      UE_template->dl_buffer_head_sdu_creation_time[i],
-		      UE_template->
-		      dl_buffer_head_sdu_remaining_size_to_send[i],
-		      UE_template->dl_buffer_head_sdu_is_segmented[i]);
-
-#endif
-
-	}
-
-	//#ifdef DEBUG_eNB_SCHEDULER
-	if (UE_template->dl_buffer_total > 0)
-	    LOG_D(MAC,
-		  "[eNB %d] Frame %d Subframe %d : RLC status for UE %d : total DL buffer size %d and total number of pdu %d \n",
-		  Mod_id, frameP, subframeP, UE_id,
-		  UE_template->dl_buffer_total,
-		  UE_template->dl_pdus_total);
-
-	//#endif
-    }
-}
-
-
-// This function returns the estimated number of RBs required by each UE for downlink scheduling
-void
-_assign_rbs_required(module_id_t Mod_id,
-		     int slice_id,
-		     frame_t frameP,
-		     sub_frame_t subframe,
-		     uint16_t
-		     nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-		     uint16_t nb_rbs_allowed_slice[MAX_NUM_CCs]
-		     [MAX_NUM_SLICES], int min_rb_unit[MAX_NUM_CCs])
-{
-
-
-    rnti_t rnti;
-    uint16_t TBS = 0;
-    LTE_eNB_UE_stats *eNB_UE_stats[MAX_NUM_CCs];
-    int UE_id, n, i, j, CC_id, pCCid, tmp;
-    UE_list_t *UE_list = &eNB_mac_inst[Mod_id].UE_list;
-    //  UE_TEMPLATE           *UE_template;
-
-    // clear rb allocations across all CC_ids
-    for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
-	if (UE_list->active[UE_id] != TRUE)
-	    continue;
-
-	if (flexran_slice_member(UE_id, slice_id) == 0)
-	    continue;
-
-	pCCid = UE_PCCID(Mod_id, UE_id);
-	rnti = UE_list->UE_template[pCCid][UE_id].rnti;
-
-	/* skip UE not present in PHY (for any of its active CCs) */
-	if (!phy_stats_exist(Mod_id, rnti))
-	    continue;
-
-	//update CQI information across component carriers
-	for (n = 0; n < UE_list->numactiveCCs[UE_id]; n++) {
-	    CC_id = UE_list->ordered_CCids[n][UE_id];
-	    eNB_UE_stats[CC_id] =
-		mac_xface->get_eNB_UE_stats(Mod_id, CC_id, rnti);
-	    eNB_UE_stats[CC_id]->dlsch_mcs1 =
-		cqi_to_mcs[flexran_get_ue_wcqi(Mod_id, UE_id)];
-	}
-
-	// provide the list of CCs sorted according to MCS
-	for (i = 0; i < UE_list->numactiveCCs[UE_id]; i++) {
-	    for (j = i + 1; j < UE_list->numactiveCCs[UE_id]; j++) {
-		DevAssert(j < MAX_NUM_CCs);
-
-		if (eNB_UE_stats[UE_list->ordered_CCids[i][UE_id]]->
-		    dlsch_mcs1 >
-		    eNB_UE_stats[UE_list->ordered_CCids[j][UE_id]]->
-		    dlsch_mcs1) {
-		    tmp = UE_list->ordered_CCids[i][UE_id];
-		    UE_list->ordered_CCids[i][UE_id] =
-			UE_list->ordered_CCids[j][UE_id];
-		    UE_list->ordered_CCids[j][UE_id] = tmp;
-		}
-	    }
-	}
-
-	/* NN --> RK
-	 * check the index of UE_template"
-	 */
-	if (UE_list->UE_template[pCCid][UE_id].dl_buffer_total > 0) {
-	    LOG_D(MAC, "[preprocessor] assign RB for UE %d\n", UE_id);
-
-	    for (i = 0; i < UE_list->numactiveCCs[UE_id]; i++) {
-		CC_id = UE_list->ordered_CCids[i][UE_id];
-		eNB_UE_stats[CC_id] =
-		    mac_xface->get_eNB_UE_stats(Mod_id, CC_id, rnti);
-
-		if (cqi_to_mcs[flexran_get_ue_wcqi(Mod_id, UE_id)] == 0) {	//eNB_UE_stats[CC_id]->dlsch_mcs1==0) {
-		    nb_rbs_required[CC_id][UE_id] = 4;	// don't let the TBS get too small
-		} else {
-		    nb_rbs_required[CC_id][UE_id] = min_rb_unit[CC_id];
-		}
-
-		TBS =
-		    mac_xface->get_TBS_DL(cqi_to_mcs
-					  [flexran_get_ue_wcqi
-					   (Mod_id, UE_id)],
-					  nb_rbs_required[CC_id][UE_id]);
-		nb_rbs_allowed_slice[CC_id][slice_id] =
-		    flexran_nb_rbs_allowed_slice(slice_percentage
-						 [slice_id],
-						 flexran_get_N_RB_DL
-						 (Mod_id, CC_id));
-		LOG_D(MAC,
-		      "[preprocessor] start RB assignement for UE %d CC_id %d dl buffer %d (RB unit %d, MCS %d, TBS %d) \n",
-		      UE_id, CC_id,
-		      UE_list->UE_template[pCCid][UE_id].dl_buffer_total,
-		      nb_rbs_required[CC_id][UE_id],
-		      flexran_get_ue_wcqi(Mod_id, UE_id), TBS);
-
-		/* calculating required number of RBs for each UE */
-		while (TBS <
-		       UE_list->UE_template[pCCid][UE_id].
-		       dl_buffer_total) {
-		    nb_rbs_required[CC_id][UE_id] += min_rb_unit[CC_id];
-
-		    if (nb_rbs_required[CC_id][UE_id] >
-			nb_rbs_allowed_slice[CC_id][slice_id]) {
-			TBS =
-			    mac_xface->get_TBS_DL(flexran_get_ue_wcqi
-						  (Mod_id, UE_id),
-						  nb_rbs_allowed_slice
-						  [CC_id]
-						  [slice_id]);
-			nb_rbs_required[CC_id][UE_id] =
-			    nb_rbs_allowed_slice[CC_id][slice_id];
-			break;
-		    }
-
-		    TBS =
-			mac_xface->get_TBS_DL(cqi_to_mcs
-					      [flexran_get_ue_wcqi
-					       (Mod_id, UE_id)],
-					      nb_rbs_required[CC_id]
-					      [UE_id]);
-		}		// end of while
-
-		LOG_D(MAC,
-		      "[eNB %d][SLICE %d] Frame %d: UE %d on CC %d: RB unit %d,  nb_required RB %d (TBS %d, mcs %d)\n",
-		      Mod_id, slice_id, frameP, UE_id, CC_id,
-		      min_rb_unit[CC_id], nb_rbs_required[CC_id][UE_id],
-		      TBS, cqi_to_mcs[flexran_get_ue_wcqi(Mod_id, UE_id)]);
-	    }
-	}
-    }
-}
-
-void
-_dlsch_scheduler_pre_processor_allocate(module_id_t Mod_id,
-					int UE_id,
-					uint8_t CC_id,
-					int N_RBG,
-					int transmission_mode,
-					int min_rb_unit,
-					uint8_t N_RB_DL,
-					uint16_t
-					nb_rbs_required[MAX_NUM_CCs]
-					[NUMBER_OF_UE_MAX],
-					uint16_t
-					nb_rbs_required_remaining
-					[MAX_NUM_CCs]
-					[NUMBER_OF_UE_MAX], unsigned char
-					rballoc_sub[MAX_NUM_CCs]
-					[N_RBG_MAX], unsigned char
-					MIMO_mode_indicator
-					[MAX_NUM_CCs][N_RBG_MAX])
-{
-    int i;
-    UE_list_t *UE_list = &eNB_mac_inst[Mod_id].UE_list;
-    UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-
-    for (i = 0; i < N_RBG; i++) {
-
-	if ((rballoc_sub[CC_id][i] == 0) &&
-	    (ue_sched_ctl->rballoc_sub_UE[CC_id][i] == 0) &&
-	    (nb_rbs_required_remaining[CC_id][UE_id] > 0) &&
-	    (ue_sched_ctl->pre_nb_available_rbs[CC_id] <
-	     nb_rbs_required[CC_id][UE_id])) {
-
-	    // if this UE is not scheduled for TM5
-	    if (ue_sched_ctl->dl_pow_off[CC_id] != 0) {
-
-		if ((i == N_RBG - 1)
-		    && ((N_RB_DL == 25) || (N_RB_DL == 50))) {
-		    rballoc_sub[CC_id][i] = 1;
-		    ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 1;
-		    MIMO_mode_indicator[CC_id][i] = 1;
-		    if (transmission_mode == 5) {
-			ue_sched_ctl->dl_pow_off[CC_id] = 1;
-		    }
-		    nb_rbs_required_remaining[CC_id][UE_id] =
-			nb_rbs_required_remaining[CC_id][UE_id] -
-			min_rb_unit + 1;
-		    ue_sched_ctl->pre_nb_available_rbs[CC_id] =
-			ue_sched_ctl->pre_nb_available_rbs[CC_id] +
-			min_rb_unit - 1;
-		} else {
-		    if (nb_rbs_required_remaining[CC_id][UE_id] >=
-			min_rb_unit) {
-			rballoc_sub[CC_id][i] = 1;
-			ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 1;
-			MIMO_mode_indicator[CC_id][i] = 1;
-			if (transmission_mode == 5) {
-			    ue_sched_ctl->dl_pow_off[CC_id] = 1;
-			}
-			nb_rbs_required_remaining[CC_id][UE_id] =
-			    nb_rbs_required_remaining[CC_id][UE_id] -
-			    min_rb_unit;
-			ue_sched_ctl->pre_nb_available_rbs[CC_id] =
-			    ue_sched_ctl->pre_nb_available_rbs[CC_id] +
-			    min_rb_unit;
-		    }
-		}
-	    }			// dl_pow_off[CC_id][UE_id] ! = 0
-	}
-    }
-}
-
-void
-_dlsch_scheduler_pre_processor_reset(int module_idP,
-				     int UE_id,
-				     uint8_t CC_id,
-				     int frameP,
-				     int subframeP,
-				     int N_RBG,
-				     uint16_t nb_rbs_required[MAX_NUM_CCs]
-				     [NUMBER_OF_UE_MAX],
-				     uint16_t
-				     nb_rbs_required_remaining
-				     [MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-				     uint16_t
-				     nb_rbs_allowed_slice[MAX_NUM_CCs]
-				     [MAX_NUM_SLICES], unsigned char
-				     rballoc_sub[MAX_NUM_CCs]
-				     [N_RBG_MAX], unsigned char
-				     MIMO_mode_indicator[MAX_NUM_CCs]
-				     [N_RBG_MAX])
-{
-    int i, j;
-    UE_list_t *UE_list = &eNB_mac_inst[module_idP].UE_list;
-    UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-    uint8_t *vrb_map =
-	eNB_mac_inst[module_idP].common_channels[CC_id].vrb_map;
-    int RBGsize =
-	PHY_vars_eNB_g[module_idP][CC_id]->frame_parms.N_RB_DL / N_RBG;
-#ifdef SF05_LIMIT
-    //int subframe05_limit=0;
-    int sf05_upper = -1, sf05_lower = -1;
-#endif
-    //  LTE_eNB_UE_stats *eNB_UE_stats = mac_xface->get_eNB_UE_stats(module_idP,CC_id,rnti);
-
-    flexran_update_TA(module_idP, UE_id, CC_id);
-
-    if (UE_id == 0) {
-	VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME
-	    (VCD_SIGNAL_DUMPER_VARIABLES_UE0_TIMING_ADVANCE,
-	     ue_sched_ctl->ta_update);
-    }
-    nb_rbs_required[CC_id][UE_id] = 0;
-    ue_sched_ctl->pre_nb_available_rbs[CC_id] = 0;
-    ue_sched_ctl->dl_pow_off[CC_id] = 2;
-    nb_rbs_required_remaining[CC_id][UE_id] = 0;
-    for (i = 0; i < n_active_slices; i++)
-	nb_rbs_allowed_slice[CC_id][i] = 0;
-#ifdef SF05_LIMIT
-    switch (N_RBG) {
-    case 6:
-	sf05_lower = 0;
-	sf05_upper = 5;
-	break;
-    case 8:
-	sf05_lower = 2;
-	sf05_upper = 5;
-	break;
-    case 13:
-	sf05_lower = 4;
-	sf05_upper = 7;
-	break;
-    case 17:
-	sf05_lower = 7;
-	sf05_upper = 9;
-	break;
-    case 25:
-	sf05_lower = 11;
-	sf05_upper = 13;
-	break;
-    }
-#endif
-    // Initialize Subbands according to VRB map
-    for (i = 0; i < N_RBG; i++) {
-	ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 0;
-	rballoc_sub[CC_id][i] = 0;
-#ifdef SF05_LIMIT
-	// for avoiding 6+ PRBs around DC in subframe 0-5 (avoid excessive errors)
-
-	if ((subframeP == 0 || subframeP == 5) &&
-	    (i >= sf05_lower && i <= sf05_upper))
-	    rballoc_sub[CC_id][i] = 1;
-#endif
-	// for SI-RNTI,RA-RNTI and P-RNTI allocations
-	for (j = 0; j < RBGsize; j++) {
-	    if (vrb_map[j + (i * RBGsize)] != 0) {
-		rballoc_sub[CC_id][i] = 1;
-		LOG_D(MAC, "Frame %d, subframe %d : vrb %d allocated\n",
-		      frameP, subframeP, j + (i * RBGsize));
-		break;
-	    }
-	}
-	LOG_D(MAC, "Frame %d Subframe %d CC_id %d RBG %i : rb_alloc %d\n",
-	      frameP, subframeP, CC_id, i, rballoc_sub[CC_id][i]);
-	MIMO_mode_indicator[CC_id][i] = 2;
-    }
-}
-
-// This function assigns pre-available RBS to each UE in specified sub-bands before scheduling is done
-void
-_dlsch_scheduler_pre_processor(module_id_t Mod_id,
-			       int slice_id,
-			       frame_t frameP,
-			       sub_frame_t subframeP,
-			       int N_RBG[MAX_NUM_CCs], int *mbsfn_flag)
-{
-
-    unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX], total_ue_count;
-    unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX];
-    int UE_id, i;
-    uint8_t round = 0;
-    uint8_t harq_pid = 0;
-    uint16_t ii, j;
-    uint16_t nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
-    uint16_t nb_rbs_allowed_slice[MAX_NUM_CCs][MAX_NUM_SLICES];
-    uint16_t nb_rbs_required_remaining[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
-    uint16_t nb_rbs_required_remaining_1[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
-    uint16_t average_rbs_per_user[MAX_NUM_CCs] = { 0 };
-    rnti_t rnti;
-    int min_rb_unit[MAX_NUM_CCs];
-    uint16_t r1 = 0;
-    uint8_t CC_id;
-    UE_list_t *UE_list = &eNB_mac_inst[Mod_id].UE_list;
-    LTE_DL_FRAME_PARMS *frame_parms[MAX_NUM_CCs] = { 0 };
-
-    int transmission_mode = 0;
-    UE_sched_ctrl *ue_sched_ctl;
-
-
-    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
-
-	if (mbsfn_flag[CC_id] > 0)	// If this CC is allocated for MBSFN skip it here
-	    continue;
-
-	frame_parms[CC_id] = mac_xface->get_lte_frame_parms(Mod_id, CC_id);
-
-
-	min_rb_unit[CC_id] = get_min_rb_unit(Mod_id, CC_id);
-
-	for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
-	    if (UE_list->active[i] != TRUE)
-		continue;
-
-	    UE_id = i;
-	    // Initialize scheduling information for all active UEs
-
-	    //if (flexran_slice_member(UE_id, slice_id) == 0)
-	    //continue;
-	    _dlsch_scheduler_pre_processor_reset(Mod_id,
-						 UE_id,
-						 CC_id,
-						 frameP,
-						 subframeP,
-						 N_RBG[CC_id],
-						 nb_rbs_required,
-						 nb_rbs_required_remaining,
-						 nb_rbs_allowed_slice,
-						 rballoc_sub,
-						 MIMO_mode_indicator);
-
-	}
-    }
-
-    // Store the DLSCH buffer for each logical channel
-    _store_dlsch_buffer(Mod_id, slice_id, frameP, subframeP);
-
-    // Calculate the number of RBs required by each UE on the basis of logical channel's buffer
-    _assign_rbs_required(Mod_id, slice_id, frameP, subframeP,
-			 nb_rbs_required, nb_rbs_allowed_slice,
-			 min_rb_unit);
-
-    // Sorts the user on the basis of dlsch logical channel buffer and CQI
-    sort_UEs(Mod_id, frameP, subframeP);
-
-    total_ue_count = 0;
-
-    // loop over all active UEs
-    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
-	rnti = flexran_get_ue_crnti(Mod_id, i);
-	if (rnti == NOT_A_RNTI)
-	    continue;
-	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-	    continue;
-
-	UE_id = i;
-
-	if (flexran_slice_member(UE_id, slice_id) == 0)
-	    continue;
-
-	if (!phy_stats_exist(Mod_id, rnti))
-	    continue;
-
-	for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
-	    CC_id = UE_list->ordered_CCids[ii][UE_id];
-	    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-	    ue_sched_ctl->max_allowed_rbs[CC_id] =
-		nb_rbs_allowed_slice[CC_id][slice_id];
-	    flexran_get_harq(Mod_id, CC_id, UE_id, frameP, subframeP,
-			     &harq_pid, &round);
-
-	    // if there is no available harq_process, skip the UE
-	    if (UE_list->UE_sched_ctrl[UE_id].harq_pid[CC_id] < 0)
-		continue;
-
-	    average_rbs_per_user[CC_id] = 0;
-
-	    frame_parms[CC_id] =
-		mac_xface->get_lte_frame_parms(Mod_id, CC_id);
-
-	    //      mac_xface->get_ue_active_harq_pid(Mod_id,CC_id,rnti,frameP,subframeP,&harq_pid,&round,0);
-
-	    if (round > 0) {
-		nb_rbs_required[CC_id][UE_id] =
-		    UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
-	    }
-	    //nb_rbs_required_remaining[UE_id] = nb_rbs_required[UE_id];
-	    if (nb_rbs_required[CC_id][UE_id] > 0) {
-		total_ue_count = total_ue_count + 1;
-	    }
-	    // hypotetical assignement
-	    /*
-	     * If schedule is enabled and if the priority of the UEs is modified
-	     * The average rbs per logical channel per user will depend on the level of
-	     * priority. Concerning the hypothetical assignement, we should assign more
-	     * rbs to prioritized users. Maybe, we can do a mapping between the
-	     * average rbs per user and the level of priority or multiply the average rbs
-	     * per user by a coefficient which represents the degree of priority.
-	     */
-
-	    if (total_ue_count == 0) {
-		average_rbs_per_user[CC_id] = 0;
-	    } else if ((min_rb_unit[CC_id] * total_ue_count) <=
-		       nb_rbs_allowed_slice[CC_id][slice_id]) {
-		average_rbs_per_user[CC_id] =
-		    (uint16_t) floor(nb_rbs_allowed_slice[CC_id][slice_id]
-				     / total_ue_count);
-	    } else {
-		average_rbs_per_user[CC_id] = min_rb_unit[CC_id];	// consider the total number of use that can be scheduled UE
-	    }
-	}
-    }
-
-    // note: nb_rbs_required is assigned according to total_buffer_dl
-    // extend nb_rbs_required to capture per LCID RB required
-    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
-	rnti = UE_RNTI(Mod_id, i);
-
-	if (rnti == NOT_A_RNTI)
-	    continue;
-
-	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-	    continue;
-
-	if (!phy_stats_exist(Mod_id, rnti))
-	    continue;
-
-	if (flexran_slice_member(i, slice_id) == 0)
-	    continue;
-
-	for (ii = 0; ii < UE_num_active_CC(UE_list, i); ii++) {
-	    CC_id = UE_list->ordered_CCids[ii][i];
-
-	    // control channel
-	    if (mac_eNB_get_rrc_status(Mod_id, rnti) < RRC_RECONFIGURED) {
-		nb_rbs_required_remaining_1[CC_id][i] =
-		    nb_rbs_required[CC_id][i];
-	    } else {
-		nb_rbs_required_remaining_1[CC_id][i] =
-		    cmin(average_rbs_per_user[CC_id],
-			 nb_rbs_required[CC_id][i]);
-
-	    }
-	}
-    }
-
-    //Allocation to UEs is done in 2 rounds,
-    // 1st stage: average number of RBs allocated to each UE
-    // 2nd stage: remaining RBs are allocated to high priority UEs
-    for (r1 = 0; r1 < 2; r1++) {
-
-	for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
-
-	    if (flexran_slice_member(i, slice_id) == 0)
-		continue;
-
-	    for (ii = 0; ii < UE_num_active_CC(UE_list, i); ii++) {
-		CC_id = UE_list->ordered_CCids[ii][i];
-
-		if (r1 == 0) {
-		    nb_rbs_required_remaining[CC_id][i] =
-			nb_rbs_required_remaining_1[CC_id][i];
-		} else {	// rb required based only on the buffer - rb allloctaed in the 1st round + extra reaming rb form the 1st round
-		    nb_rbs_required_remaining[CC_id][i] =
-			nb_rbs_required[CC_id][i] -
-			nb_rbs_required_remaining_1[CC_id][i] +
-			nb_rbs_required_remaining[CC_id][i];
-		}
-
-		if (nb_rbs_required[CC_id][i] > 0)
-		    LOG_D(MAC,
-			  "round %d : nb_rbs_required_remaining[%d][%d]= %d (remaining_1 %d, required %d,  pre_nb_available_rbs %d, N_RBG %d, rb_unit %d)\n",
-			  r1, CC_id, i,
-			  nb_rbs_required_remaining[CC_id][i],
-			  nb_rbs_required_remaining_1[CC_id][i],
-			  nb_rbs_required[CC_id][i],
-			  UE_list->UE_sched_ctrl[i].
-			  pre_nb_available_rbs[CC_id], N_RBG[CC_id],
-			  min_rb_unit[CC_id]);
-
-	    }
-	}
-
-	if (total_ue_count > 0) {
-	    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
-		UE_id = i;
-
-		if (flexran_slice_member(UE_id, slice_id) == 0)
-		    continue;
-
-		for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
-		    CC_id = UE_list->ordered_CCids[ii][UE_id];
-		    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-		    flexran_get_harq(Mod_id, CC_id, UE_id, frameP,
-				     subframeP, &harq_pid, &round);
-		    rnti = UE_RNTI(Mod_id, UE_id);
-
-		    // LOG_D(MAC,"UE %d rnti 0x\n", UE_id, rnti );
-		    if (rnti == NOT_A_RNTI)
-			continue;
-
-		    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1)
-			continue;
-
-		    if (!phy_stats_exist(Mod_id, rnti))
-			continue;
-
-		    transmission_mode =
-			mac_xface->get_transmission_mode(Mod_id, CC_id,
-							 rnti);
-		    //rrc_status = mac_eNB_get_rrc_status(Mod_id,rnti);
-		    /* 1st allocate for the retx */
-
-		    // retransmission in data channels
-		    // control channel in the 1st transmission
-		    // data channel for all TM
-		    LOG_T(MAC,
-			  "calling dlsch_scheduler_pre_processor_allocate .. \n ");
-		    _dlsch_scheduler_pre_processor_allocate(Mod_id, UE_id,
-							    CC_id,
-							    N_RBG[CC_id],
-							    transmission_mode,
-							    min_rb_unit
-							    [CC_id],
-							    frame_parms
-							    [CC_id]->
-							    N_RB_DL,
-							    nb_rbs_required,
-							    nb_rbs_required_remaining,
-							    rballoc_sub,
-							    MIMO_mode_indicator);
-		}
-	    }
-	}			// total_ue_count
-    }				// end of for for r1 and r2
-
-    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
-	UE_id = i;
-	ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-
-	if (flexran_slice_member(UE_id, slice_id) == 0)
-	    continue;
-
-	for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
-	    CC_id = UE_list->ordered_CCids[ii][UE_id];
-	    //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].dl_pow_off = dl_pow_off[UE_id];
-
-	    if (ue_sched_ctl->pre_nb_available_rbs[CC_id] > 0) {
-		LOG_D(MAC,
-		      "******************DL Scheduling Information for UE%d ************************\n",
-		      UE_id);
-		LOG_D(MAC, "dl power offset UE%d = %d \n", UE_id,
-		      ue_sched_ctl->dl_pow_off[CC_id]);
-		LOG_D(MAC,
-		      "***********RB Alloc for every subband for UE%d ***********\n",
-		      UE_id);
-
-		for (j = 0; j < N_RBG[CC_id]; j++) {
-		    //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].rballoc_sub[i] = rballoc_sub_UE[CC_id][UE_id][i];
-		    LOG_D(MAC, "RB Alloc for UE%d and Subband%d = %d\n",
-			  UE_id, j,
-			  ue_sched_ctl->rballoc_sub_UE[CC_id][j]);
-		}
-
-		//PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].pre_nb_available_rbs = pre_nb_available_rbs[CC_id][UE_id];
-		LOG_D(MAC,
-		      "[eNB %d][SLICE %d] Total RBs allocated for UE%d = %d\n",
-		      Mod_id, slice_id, UE_id,
-		      ue_sched_ctl->pre_nb_available_rbs[CC_id]);
-	    }
-	}
-    }
-}
-
-#define SF05_LIMIT 1
-
-/*
- * Main scheduling functions to support slicing
- *
- */
-
-void
-flexran_schedule_ue_spec_default(mid_t mod_id,
-				 uint32_t frame,
-				 uint32_t subframe,
-				 int *mbsfn_flag,
-				 Protocol__FlexranMessage ** dl_info)
-//------------------------------------------------------------------------------
-{
-    int i = 0;
-
-    flexran_agent_mac_create_empty_dl_config(mod_id, dl_info);
-
-    for (i = 0; i < n_active_slices; i++) {
-
-	// Load any updated functions
-	if (update_dl_scheduler[i] > 0) {
-	    slice_sched[i] = dlsym(NULL, dl_scheduler_type[i]);
-	    update_dl_scheduler[i] = 0;
-	    update_dl_scheduler_current[i] = 0;
-	    slice_percentage_current[i] = slice_percentage[i];
-	    total_slice_percentage += slice_percentage[i];
-	    LOG_N(MAC, "update dl scheduler slice %d\n", i);
-	}
-	// check if the number of slices has changed, and log 
-	if (n_active_slices_current != n_active_slices) {
-	    if ((n_active_slices > 0)
-		&& (n_active_slices <= MAX_NUM_SLICES)) {
-		LOG_N(MAC,
-		      "[eNB %d]frame %d subframe %d: number of active slices has changed: %d-->%d\n",
-		      mod_id, frame, subframe, n_active_slices_current,
-		      n_active_slices);
-		n_active_slices_current = n_active_slices;
-	    } else {
-		LOG_W(MAC,
-		      "invalid number of slices %d, revert to the previous value %d\n",
-		      n_active_slices, n_active_slices_current);
-		n_active_slices = n_active_slices_current;
-	    }
-	}
-	// check if the slice rb share has changed, and log the console
-	if (slice_percentage_current[i] != slice_percentage[i]) {
-	    if ((slice_percentage[i] >= 0.0)
-		&& (slice_percentage[i] <= 1.0)) {
-		if ((total_slice_percentage - slice_percentage_current[i] +
-		     slice_percentage[i]) <= 1.0) {
-		    total_slice_percentage =
-			total_slice_percentage -
-			slice_percentage_current[i] + slice_percentage[i];
-		    LOG_N(MAC,
-			  "[eNB %d][SLICE %d] frame %d subframe %d: total percentage %f, slice RB percentage has changed: %f-->%f\n",
-			  mod_id, i, frame, subframe,
-			  total_slice_percentage,
-			  slice_percentage_current[i],
-			  slice_percentage[i]);
-		    slice_percentage_current[i] = slice_percentage[i];
-		} else {
-		    LOG_W(MAC,
-			  "[eNB %d][SLICE %d] invalid total RB share (%f->%f), revert the previous value (%f->%f)\n",
-			  mod_id, i, total_slice_percentage,
-			  total_slice_percentage -
-			  slice_percentage_current[i] +
-			  slice_percentage[i], slice_percentage[i],
-			  slice_percentage_current[i]);
-		    slice_percentage[i] = slice_percentage_current[i];
-
-		}
-	    } else {
-		LOG_W(MAC,
-		      "[eNB %d][SLICE %d] invalid slice RB share, revert the previous value (%f->%f)\n",
-		      mod_id, i, slice_percentage[i],
-		      slice_percentage_current[i]);
-		slice_percentage[i] = slice_percentage_current[i];
-
-	    }
-	}
-	// check if the slice max MCS, and log the console
-	if (slice_maxmcs_current[i] != slice_maxmcs[i]) {
-	    if ((slice_maxmcs[i] >= 0) && (slice_maxmcs[i] < 29)) {
-		LOG_N(MAC,
-		      "[eNB %d][SLICE %d] frame %d subframe %d: slice MAX MCS has changed: %d-->%d\n",
-		      mod_id, i, frame, subframe, slice_maxmcs_current[i],
-		      slice_maxmcs[i]);
-		slice_maxmcs_current[i] = slice_maxmcs[i];
-	    } else {
-		LOG_W(MAC,
-		      "[eNB %d][SLICE %d] invalid slice max mcs %d, revert the previous value %d\n",
-		      mod_id, i, slice_percentage[i], slice_percentage[i]);
-		slice_maxmcs[i] = slice_maxmcs_current[i];
-	    }
-	}
-	// check if a new scheduler, and log the console
-	if (update_dl_scheduler_current[i] != update_dl_scheduler[i]) {
-	    LOG_N(MAC,
-		  "[eNB %d][SLICE %d] frame %d subframe %d: DL scheduler for this slice is updated: %s \n",
-		  mod_id, i, frame, subframe, dl_scheduler_type[i]);
-	    update_dl_scheduler_current[i] = update_dl_scheduler[i];
-	}
-	// Run each enabled slice-specific schedulers one by one
-	//LOG_N(MAC,"[eNB %d]frame %d subframe %d slice %d: calling the scheduler\n", mod_id, frame, subframe,i);
-	slice_sched[i] (mod_id, i, frame, subframe, mbsfn_flag, dl_info);
-
-    }
-
-}
-
-uint16_t flexran_nb_rbs_allowed_slice(float rb_percentage, int total_rbs)
-{
-    return (uint16_t) floor(rb_percentage * total_rbs);
-}
-
-int flexran_slice_maxmcs(int slice_id)
-{
-    return slice_maxmcs[slice_id];
-}
-
-int flexran_slice_member(int UE_id, int slice_id)
-{
-    // group membership definition
-    int slice_member = 0;
-
-    if ((slice_id < 0) || (slice_id > n_active_slices))
-	LOG_W(MAC, "out of range slice id %d\n", slice_id);
-
-    switch (slicing_strategy) {
-    case SLICE_MASK:
-	switch (slice_id) {
-	case 0:
-	    if (SLICE0_MASK & UE_id) {
-		slice_member = 1;
-	    }
-	    break;
-	case 1:
-	    if (SLICE1_MASK & UE_id) {
-		slice_member = 1;
-	    }
-	    break;
-	case 2:
-	    if (SLICE2_MASK & UE_id) {
-		slice_member = 1;
-	    }
-	    break;
-	case 3:
-	    if (SLICE3_MASK & UE_id) {
-		slice_member = 1;
-	    }
-	    break;
-	default:
-	    LOG_W(MAC, "unknown slice_id %d\n", slice_id);
-	    break;
-
-	}
-	break;
-    case UEID_TO_SLICEID:
-    default:
-	if ((UE_id % n_active_slices) == slice_id) {
-	    slice_member = 1;	// this ue is a member of this slice
-	}
-	break;
-    }
-
-    return slice_member;
-}
-
-/* more aggressive rb and mcs allocation with medium priority and the traffic qci */
-void
-flexran_schedule_ue_spec_embb(mid_t mod_id,
-			      int slice_id,
-			      uint32_t frame,
-			      uint32_t subframe,
-			      int *mbsfn_flag,
-			      Protocol__FlexranMessage ** dl_info)
-{
-    flexran_schedule_ue_spec_common(mod_id,
-				    slice_id,
-				    frame, subframe, mbsfn_flag, dl_info);
-
-}
-
-/* more conservative mcs allocation with high priority and the traffic qci */
-void
-flexran_schedule_ue_spec_urllc(mid_t mod_id,
-			       int slice_id,
-			       uint32_t frame,
-			       uint32_t subframe,
-			       int *mbsfn_flag,
-			       Protocol__FlexranMessage ** dl_info)
-{
-    flexran_schedule_ue_spec_common(mod_id,
-				    slice_id,
-				    frame, subframe, mbsfn_flag, dl_info);
-
-}
-
-/* constant rb allocation with low mcs with low priority and given the UE capabilities */
-void
-flexran_schedule_ue_spec_mmtc(mid_t mod_id,
-			      int slice_id,
-			      uint32_t frame,
-			      uint32_t subframe,
-			      int *mbsfn_flag,
-			      Protocol__FlexranMessage ** dl_info)
-{
-
-    flexran_schedule_ue_spec_common(mod_id,
-				    slice_id,
-				    frame, subframe, mbsfn_flag, dl_info);
-
-}
-
-/* regular rb and mcs allocation with low priority */
-void
-flexran_schedule_ue_spec_be(mid_t mod_id,
-			    int slice_id,
-			    uint32_t frame,
-			    uint32_t subframe,
-			    int *mbsfn_flag,
-			    Protocol__FlexranMessage ** dl_info)
-{
-
-    flexran_schedule_ue_spec_common(mod_id,
-				    slice_id,
-				    frame, subframe, mbsfn_flag, dl_info);
-
-}
-
-//------------------------------------------------------------------------------
-void
-flexran_schedule_ue_spec_common(mid_t mod_id,
-				int slice_id,
-				uint32_t frame,
-				uint32_t subframe,
-				int *mbsfn_flag,
-				Protocol__FlexranMessage ** dl_info)
-//------------------------------------------------------------------------------
-{
-    uint8_t CC_id;
-    int UE_id;
-    int N_RBG[MAX_NUM_CCs];
-    unsigned char aggregation;
-    mac_rlc_status_resp_t rlc_status;
-    unsigned char header_len = 0, header_len_last = 0, ta_len = 0;
-    uint16_t nb_rb, nb_rb_temp, total_nb_available_rb[MAX_NUM_CCs],
-	nb_available_rb;
-    uint16_t TBS, j, rnti;
-    uint8_t round = 0;
-    uint8_t harq_pid = 0;
-    uint16_t sdu_length_total = 0;
-    int mcs, mcs_tmp;
-    uint16_t min_rb_unit[MAX_NUM_CCs];
-    eNB_MAC_INST *eNB = &eNB_mac_inst[mod_id];
-    /* TODO: Must move the helper structs to scheduler implementation */
-    UE_list_t *UE_list = &eNB->UE_list;
-    int32_t normalized_rx_power, target_rx_power;
-    int32_t tpc = 1;
-    static int32_t tpc_accumulated = 0;
-    UE_sched_ctrl *ue_sched_ctl;
-    LTE_eNB_UE_stats *eNB_UE_stats = NULL;
-    Protocol__FlexDlData *dl_data[NUM_MAX_UE];
-    int num_ues_added = 0;
-    int channels_added = 0;
-
-    Protocol__FlexDlDci *dl_dci;
-    Protocol__FlexRlcPdu *rlc_pdus[11];
-    uint32_t ce_flags = 0;
-
-    uint8_t rballoc_sub[25];
-    int i;
-    uint32_t data_to_request;
-    uint32_t dci_tbs;
-    uint8_t ue_has_transmission = 0;
-    uint32_t ndi;
-
-#if 0
-
-    if (UE_list->head == -1) {
-	return;
-    }
-#endif
-
-    start_meas(&eNB->schedule_dlsch);
-    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
-	(VCD_SIGNAL_DUMPER_FUNCTIONS_SCHEDULE_DLSCH, VCD_FUNCTION_IN);
-
-    //weight = get_ue_weight(module_idP,UE_id);
-    aggregation = 1;		// set to the maximum aggregation level
-
-    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
-	min_rb_unit[CC_id] = get_min_rb_unit(mod_id, CC_id);
-	// get number of PRBs less those used by common channels
-	total_nb_available_rb[CC_id] = flexran_get_N_RB_DL(mod_id, CC_id);
-	for (i = 0; i < flexran_get_N_RB_DL(mod_id, CC_id); i++)
-	    if (eNB->common_channels[CC_id].vrb_map[i] != 0)
-		total_nb_available_rb[CC_id]--;
-
-	N_RBG[CC_id] = flexran_get_N_RBG(mod_id, CC_id);
-
-	// store the global enb stats:
-	eNB->eNB_stats[CC_id].num_dlactive_UEs = UE_list->num_UEs;
-	eNB->eNB_stats[CC_id].available_prbs =
-	    total_nb_available_rb[CC_id];
-	eNB->eNB_stats[CC_id].total_available_prbs +=
-	    total_nb_available_rb[CC_id];
-	eNB->eNB_stats[CC_id].dlsch_bytes_tx = 0;
-	eNB->eNB_stats[CC_id].dlsch_pdus_tx = 0;
-    }
-
-    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
-	(VCD_SIGNAL_DUMPER_FUNCTIONS_DLSCH_PREPROCESSOR, VCD_FUNCTION_IN);
-
-    start_meas(&eNB->schedule_dlsch_preprocessor);
-    _dlsch_scheduler_pre_processor(mod_id,
-				   slice_id,
-				   frame, subframe, N_RBG, mbsfn_flag);
-    stop_meas(&eNB->schedule_dlsch_preprocessor);
-    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
-	(VCD_SIGNAL_DUMPER_FUNCTIONS_DLSCH_PREPROCESSOR, VCD_FUNCTION_OUT);
-
-    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
-	LOG_D(MAC, "doing schedule_ue_spec for CC_id %d\n", CC_id);
-
-	if (mbsfn_flag[CC_id] > 0)
-	    continue;
-
-	for (UE_id = UE_list->head; UE_id >= 0;
-	     UE_id = UE_list->next[UE_id]) {
-
-	    rnti = flexran_get_ue_crnti(mod_id, UE_id);
-	    eNB_UE_stats =
-		mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
-	    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-
-	    if (eNB_UE_stats == NULL) {
-		LOG_D(MAC, "[eNB] Cannot find eNB_UE_stats\n");
-		//  mac_xface->macphy_exit("[MAC][eNB] Cannot find eNB_UE_stats\n");
-		continue;
-	    }
-
-	    if (flexran_slice_member(UE_id, slice_id) == 0)
-		continue;
-
-	    if (rnti == NOT_A_RNTI) {
-		LOG_D(MAC, "Cannot find rnti for UE_id %d (num_UEs %d)\n",
-		      UE_id, UE_list->num_UEs);
-		// mac_xface->macphy_exit("Cannot find rnti for UE_id");
-		continue;
-	    }
-
-	    switch (mac_xface->get_transmission_mode(mod_id, CC_id, rnti)) {
-	    case 1:
-	    case 2:
-	    case 7:
-		aggregation = get_aggregation(get_bw_index(mod_id, CC_id),
-					      eNB_UE_stats->DL_cqi[0],
-					      format1);
-		break;
-	    case 3:
-		aggregation = get_aggregation(get_bw_index(mod_id, CC_id),
-					      eNB_UE_stats->DL_cqi[0],
-					      format2A);
-		break;
-	    default:
-		LOG_W(MAC, "Unsupported transmission mode %d\n",
-		      mac_xface->get_transmission_mode(mod_id, CC_id,
-						       rnti));
-		aggregation = 2;
-	    }
-
-	    if ((ue_sched_ctl->pre_nb_available_rbs[CC_id] == 0) ||	// no RBs allocated 
-		CCE_allocation_infeasible(mod_id, CC_id, 0, subframe,
-					  aggregation, rnti)) {
-		LOG_D(MAC,
-		      "[eNB %d] Frame %d : no RB allocated for UE %d on CC_id %d: continue \n",
-		      mod_id, frame, UE_id, CC_id);
-		//if(mac_xface->get_transmission_mode(module_idP,rnti)==5)
-		continue;	//to next user (there might be rbs availiable for other UEs in TM5
-		// else
-		//  break;
-	    }
-
-	    if (flexran_get_duplex_mode(mod_id, CC_id) ==
-		PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-		set_ue_dai(subframe,
-			   flexran_get_subframe_assignment(mod_id, CC_id),
-			   UE_id, CC_id, UE_list);
-		//TODO: update UL DAI after DLSCH scheduling
-		//set_ul_DAI(mod_id, UE_id, CC_id, frame, subframe,frame_parms);
-	    }
-
-	    channels_added = 0;
-
-	    // After this point all the UEs will be scheduled
-	    dl_data[num_ues_added] =
-		(Protocol__FlexDlData *)
-		malloc(sizeof(Protocol__FlexDlData));
-	    protocol__flex_dl_data__init(dl_data[num_ues_added]);
-	    dl_data[num_ues_added]->has_rnti = 1;
-	    dl_data[num_ues_added]->rnti = rnti;
-	    dl_data[num_ues_added]->n_rlc_pdu = 0;
-	    dl_data[num_ues_added]->has_serv_cell_index = 1;
-	    dl_data[num_ues_added]->serv_cell_index = CC_id;
-
-	    nb_available_rb = ue_sched_ctl->pre_nb_available_rbs[CC_id];
-	    flexran_get_harq(mod_id, CC_id, UE_id, frame, subframe,
-			     &harq_pid, &round);
-	    sdu_length_total = 0;
-	    mcs = cqi_to_mcs[flexran_get_ue_wcqi(mod_id, UE_id)];
-	    //      LOG_I(FLEXRAN_AGENT, "The MCS is %d\n", mcs);
-	    mcs = cmin(mcs, flexran_slice_maxmcs(slice_id));
-#ifdef EXMIMO
-
-	    if (mac_xface->get_transmission_mode(mod_id, CC_id, rnti) == 5) {
-		mcs = cqi_to_mcs[flexran_get_ue_wcqi(mod_id, UE_id)];
-		mcs = cmin(mcs, 16);
-	    }
-#endif
-
-	    // initializing the rb allocation indicator for each UE
-	    for (j = 0; j < flexran_get_N_RBG(mod_id, CC_id); j++) {
-		UE_list->
-		    UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j]
-		    = 0;
-		rballoc_sub[j] = 0;
-	    }
-
-	    /* LOG_D(MAC,"[eNB %d] Frame %d: Scheduling UE %d on CC_id %d (rnti %x, harq_pid %d, round %d, rb %d, cqi %d, mcs %d, rrc %d)\n", */
-	    /*       mod_id, frame, UE_id, CC_id, rnti, harq_pid, round, nb_available_rb, */
-	    /*       eNB_UE_stats->DL_cqi[0], mcs, */
-	    /*       UE_list->eNB_UE_stats[CC_id][UE_id].rrc_status); */
-
-	    dl_dci =
-		(Protocol__FlexDlDci *)
-		malloc(sizeof(Protocol__FlexDlDci));
-	    protocol__flex_dl_dci__init(dl_dci);
-	    dl_data[num_ues_added]->dl_dci = dl_dci;
-
-
-	    dl_dci->has_rnti = 1;
-	    dl_dci->rnti = rnti;
-	    dl_dci->has_harq_process = 1;
-	    dl_dci->harq_process = harq_pid;
-
-	    /* process retransmission  */
-	    if (round > 0) {
-
-		LOG_D(FLEXRAN_AGENT,
-		      "There was a retransmission just now and the round was %d\n",
-		      round);
-
-		if (flexran_get_duplex_mode(mod_id, CC_id) ==
-		    PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-		    UE_list->UE_template[CC_id][UE_id].DAI++;
-		    update_ul_dci(mod_id, CC_id, rnti,
-				  UE_list->UE_template[CC_id][UE_id].DAI);
-		    LOG_D(MAC,
-			  "DAI update: CC_id %d subframeP %d: UE %d, DAI %d\n",
-			  CC_id, subframe, UE_id,
-			  UE_list->UE_template[CC_id][UE_id].DAI);
-		}
-
-		mcs = UE_list->UE_template[CC_id][UE_id].mcs[harq_pid];
-
-		// get freq_allocation
-		nb_rb = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
-
-		/*TODO: Must add this to FlexRAN agent API */
-		dci_tbs = mac_xface->get_TBS_DL(mcs, nb_rb);
-
-		if (nb_rb <= nb_available_rb) {
-
-		    if (nb_rb == ue_sched_ctl->pre_nb_available_rbs[CC_id]) {
-			for (j = 0; j < flexran_get_N_RBG(mod_id, CC_id); j++) {	// for indicating the rballoc for each sub-band
-			    UE_list->UE_template[CC_id][UE_id].
-				rballoc_subband[harq_pid][j] =
-				ue_sched_ctl->rballoc_sub_UE[CC_id][j];
-			}
-		    } else {
-			nb_rb_temp = nb_rb;
-			j = 0;
-
-			while ((nb_rb_temp > 0)
-			       && (j < flexran_get_N_RBG(mod_id, CC_id))) {
-			    if (ue_sched_ctl->rballoc_sub_UE[CC_id][j] ==
-				1) {
-				UE_list->
-				    UE_template[CC_id]
-				    [UE_id].rballoc_subband[harq_pid][j] =
-				    ue_sched_ctl->rballoc_sub_UE[CC_id][j];
-
-				if ((j ==
-				     flexran_get_N_RBG(mod_id, CC_id) - 1)
-				    &&
-				    ((flexran_get_N_RB_DL(mod_id, CC_id) ==
-				      25)
-				     || (flexran_get_N_RB_DL(mod_id, CC_id)
-					 == 50))) {
-				    nb_rb_temp =
-					nb_rb_temp - min_rb_unit[CC_id] +
-					1;
-				} else {
-				    nb_rb_temp =
-					nb_rb_temp - min_rb_unit[CC_id];
-				}
-			    }
-			    j = j + 1;
-			}
-		    }
-
-		    nb_available_rb -= nb_rb;
-		    PHY_vars_eNB_g[mod_id][CC_id]->
-			mu_mimo_mode[UE_id].pre_nb_available_rbs = nb_rb;
-		    PHY_vars_eNB_g[mod_id][CC_id]->
-			mu_mimo_mode[UE_id].dl_pow_off =
-			ue_sched_ctl->dl_pow_off[CC_id];
-
-		    for (j = 0; j < flexran_get_N_RBG(mod_id, CC_id); j++) {
-			PHY_vars_eNB_g[mod_id][CC_id]->
-			    mu_mimo_mode[UE_id].rballoc_sub[j] =
-			    UE_list->
-			    UE_template[CC_id][UE_id].rballoc_subband
-			    [harq_pid][j];
-			rballoc_sub[j] =
-			    UE_list->
-			    UE_template[CC_id][UE_id].rballoc_subband
-			    [harq_pid][j];
-		    }
-
-		    // Keep the old NDI, do not toggle
-		    ndi =
-			UE_list->UE_template[CC_id][UE_id].
-			oldNDI[harq_pid];
-		    tpc =
-			UE_list->UE_template[CC_id][UE_id].
-			oldTPC[harq_pid];
-		    UE_list->UE_template[CC_id][UE_id].mcs[harq_pid] = mcs;
-
-		    ue_has_transmission = 1;
-		    num_ues_added++;
-		} else {
-		    LOG_D(MAC,
-			  "[eNB %d] Frame %d CC_id %d : don't schedule UE %d, its retransmission takes more resources than we have\n",
-			  mod_id, frame, CC_id, UE_id);
-		    ue_has_transmission = 0;
-		}
-		//End of retransmission
-	    } else {		/* This is a potentially new SDU opportunity */
-		rlc_status.bytes_in_buffer = 0;
-		// Now check RLC information to compute number of required RBs
-		// get maximum TBS size for RLC request
-		//TBS = mac_xface->get_TBS(eNB_UE_stats->DL_cqi[0]<<1,nb_available_rb);
-		TBS = mac_xface->get_TBS_DL(mcs, nb_available_rb);
-		dci_tbs = TBS;
-		LOG_D(FLEXRAN_AGENT, "TBS is %d\n", TBS);
-
-		// check first for RLC data on DCCH
-		// add the length for  all the control elements (timing adv, drx, etc) : header + payload
-
-		ta_len = (ue_sched_ctl->ta_update != 0) ? 2 : 0;
-
-		dl_data[num_ues_added]->n_ce_bitmap = 2;
-		dl_data[num_ues_added]->ce_bitmap =
-		    (uint32_t *) calloc(2, sizeof(uint32_t));
-
-		if (ta_len > 0) {
-		    ce_flags |= PROTOCOL__FLEX_CE_TYPE__FLPCET_TA;
-		}
-
-		/*TODO: Add other flags if DRX and other CE are required */
-
-		// Add the control element flags to the flexran message
-		dl_data[num_ues_added]->ce_bitmap[0] = ce_flags;
-		dl_data[num_ues_added]->ce_bitmap[1] = ce_flags;
-
-		// TODO : Need to prioritize DRBs
-		// Loop through the UE logical channels (DCCH, DCCH1, DTCH for now)
-		header_len = 0;
-		header_len_last = 0;
-		sdu_length_total = 0;
-		for (j = 1; j < NB_RB_MAX; j++) {
-		    header_len += 3;
-		    // Need to see if we have space for data from this channel
-		    if (dci_tbs - ta_len - header_len - sdu_length_total >
-			0) {
-			LOG_D(MAC,
-			      "[TEST]Requested %d bytes from RLC buffer on channel %d during first call\n",
-			      dci_tbs - ta_len - header_len);
-			//If we have space, we need to see how much data we can request at most (if any available)
-			rlc_status = mac_rlc_status_ind(mod_id, rnti, mod_id, frame, subframe, ENB_FLAG_YES, MBMS_FLAG_NO, j, (dci_tbs - ta_len - header_len - sdu_length_total));	// transport block set size
-
-			//If data are available in channel j
-			if (rlc_status.bytes_in_buffer > 0) {
-			    LOG_D(MAC,
-				  "[TEST]Have %d bytes in DCCH buffer during first call\n",
-				  rlc_status.bytes_in_buffer);
-			    //Fill in as much as possible
-			    data_to_request =
-				cmin(dci_tbs - ta_len - header_len -
-				     sdu_length_total,
-				     rlc_status.bytes_in_buffer);
-			    LOG_D(FLEXRAN_AGENT,
-				  "Will request %d bytes from channel %d\n",
-				  data_to_request, j);
-			    if (data_to_request < 128) {	//The header will be one byte less
-				header_len--;
-				header_len_last = 2;
-			    } else {
-				header_len_last = 3;
-			    }
-			    /* if (j == 1 || j == 2) {
-			       data_to_request+=0; 
-			       } */
-			    LOG_D(MAC,
-				  "[TEST]Will request %d from channel %d\n",
-				  data_to_request, j);
-			    rlc_pdus[channels_added] =
-				(Protocol__FlexRlcPdu *)
-				malloc(sizeof(Protocol__FlexRlcPdu));
-			    protocol__flex_rlc_pdu__init(rlc_pdus
-							 [channels_added]);
-			    rlc_pdus[channels_added]->n_rlc_pdu_tb = 2;
-			    rlc_pdus[channels_added]->rlc_pdu_tb =
-				(Protocol__FlexRlcPduTb **)
-				malloc(sizeof(Protocol__FlexRlcPduTb *) *
-				       2);
-			    rlc_pdus[channels_added]->rlc_pdu_tb[0] =
-				(Protocol__FlexRlcPduTb *)
-				malloc(sizeof(Protocol__FlexRlcPduTb));
-			    protocol__flex_rlc_pdu_tb__init(rlc_pdus
-							    [channels_added]->rlc_pdu_tb
-							    [0]);
-			    rlc_pdus[channels_added]->
-				rlc_pdu_tb[0]->has_logical_channel_id = 1;
-			    rlc_pdus[channels_added]->
-				rlc_pdu_tb[0]->logical_channel_id = j;
-			    rlc_pdus[channels_added]->rlc_pdu_tb[0]->
-				has_size = 1;
-			    rlc_pdus[channels_added]->rlc_pdu_tb[0]->size =
-				data_to_request;
-			    rlc_pdus[channels_added]->rlc_pdu_tb[1] =
-				(Protocol__FlexRlcPduTb *)
-				malloc(sizeof(Protocol__FlexRlcPduTb));
-			    protocol__flex_rlc_pdu_tb__init(rlc_pdus
-							    [channels_added]->rlc_pdu_tb
-							    [1]);
-			    rlc_pdus[channels_added]->
-				rlc_pdu_tb[1]->has_logical_channel_id = 1;
-			    rlc_pdus[channels_added]->
-				rlc_pdu_tb[1]->logical_channel_id = j;
-			    rlc_pdus[channels_added]->rlc_pdu_tb[1]->
-				has_size = 1;
-			    rlc_pdus[channels_added]->rlc_pdu_tb[1]->size =
-				data_to_request;
-			    dl_data[num_ues_added]->n_rlc_pdu++;
-			    channels_added++;
-			    //Set this to the max value that we might request
-			    sdu_length_total += data_to_request;
-			} else {
-			    //Take back the assumption of a header for this channel
-			    header_len -= 3;
-			}	//End rlc_status.bytes_in_buffer <= 0
-		    }		//end of if dci_tbs - ta_len - header_len > 0
-		}		// End of iterating the logical channels
-
-		// Add rlc_pdus to the dl_data message
-		dl_data[num_ues_added]->rlc_pdu = (Protocol__FlexRlcPdu **)
-		    malloc(sizeof(Protocol__FlexRlcPdu *) *
-			   dl_data[num_ues_added]->n_rlc_pdu);
-		for (i = 0; i < dl_data[num_ues_added]->n_rlc_pdu; i++) {
-		    dl_data[num_ues_added]->rlc_pdu[i] = rlc_pdus[i];
-		}
-
-		if (header_len == 0) {
-		    LOG_D(FLEXRAN_AGENT, "Header was empty\n");
-		    header_len_last = 0;
-		}
-		// there is a payload
-		if ((dl_data[num_ues_added]->n_rlc_pdu > 0)) {
-		    // Now compute number of required RBs for total sdu length
-		    // Assume RAH format 2
-		    // adjust  header lengths
-		    LOG_D(FLEXRAN_AGENT, "We have %d bytes to transfer\n",
-			  sdu_length_total);
-		    if (header_len != 0) {
-			LOG_D(FLEXRAN_AGENT, "Header length was %d ",
-			      header_len);
-			header_len_last--;
-			header_len -= header_len_last;
-			LOG_D(FLEXRAN_AGENT, "so we resized it to %d\n",
-			      header_len);
-		    }
-
-		    /* if (header_len == 2 || header_len == 3) { //Only one SDU, remove length field */
-		    /*   header_len = 1; */
-		    /* } else { //Remove length field from the last SDU */
-		    /*   header_len--; */
-		    /* } */
-
-		    mcs_tmp = mcs;
-		    if (mcs_tmp == 0) {
-			nb_rb = 4;	// don't let the TBS get too small
-		    } else {
-			nb_rb = min_rb_unit[CC_id];
-		    }
-
-		    LOG_D(MAC,
-			  "[TEST]The initial number of resource blocks was %d\n",
-			  nb_rb);
-		    LOG_D(MAC, "[TEST] The initial mcs was %d\n", mcs_tmp);
-
-		    TBS = mac_xface->get_TBS_DL(mcs_tmp, nb_rb);
-		    LOG_D(MAC,
-			  "[TEST]The TBS during rate matching was %d\n",
-			  TBS);
-
-		    while (TBS < (sdu_length_total + header_len + ta_len)) {
-			nb_rb += min_rb_unit[CC_id];	//
-			LOG_D(MAC,
-			      "[TEST]Had to increase the number of RBs\n");
-			if (nb_rb > nb_available_rb) {	// if we've gone beyond the maximum number of RBs
-			    // (can happen if N_RB_DL is odd)
-			    TBS =
-				mac_xface->get_TBS_DL(mcs_tmp,
-						      nb_available_rb);
-			    nb_rb = nb_available_rb;
-			    break;
-			}
-
-			TBS = mac_xface->get_TBS_DL(mcs_tmp, nb_rb);
-		    }
-
-		    if (nb_rb == ue_sched_ctl->pre_nb_available_rbs[CC_id]) {
-			LOG_D(MAC,
-			      "[TEST]We had the exact number of rbs. Time to fill the rballoc subband\n");
-			for (j = 0; j < flexran_get_N_RBG(mod_id, CC_id); j++) {	// for indicating the rballoc for each sub-band
-			    UE_list->UE_template[CC_id][UE_id].
-				rballoc_subband[harq_pid][j] =
-				ue_sched_ctl->rballoc_sub_UE[CC_id][j];
-			}
-		    } else {
-			nb_rb_temp = nb_rb;
-			j = 0;
-			LOG_D(MAC,
-			      "[TEST]Will only partially fill the bitmap\n");
-			while ((nb_rb_temp > 0)
-			       && (j < flexran_get_N_RBG(mod_id, CC_id))) {
-			    if (ue_sched_ctl->rballoc_sub_UE[CC_id][j] ==
-				1) {
-				UE_list->
-				    UE_template[CC_id]
-				    [UE_id].rballoc_subband[harq_pid][j] =
-				    ue_sched_ctl->rballoc_sub_UE[CC_id][j];
-				if ((j ==
-				     flexran_get_N_RBG(mod_id, CC_id) - 1)
-				    &&
-				    ((flexran_get_N_RB_DL(mod_id, CC_id) ==
-				      25)
-				     || (flexran_get_N_RB_DL(mod_id, CC_id)
-					 == 50))) {
-				    nb_rb_temp =
-					nb_rb_temp - min_rb_unit[CC_id] +
-					1;
-				} else {
-				    nb_rb_temp =
-					nb_rb_temp - min_rb_unit[CC_id];
-				}
-			    }
-			    j = j + 1;
-			}
-		    }
-
-		    PHY_vars_eNB_g[mod_id][CC_id]->
-			mu_mimo_mode[UE_id].pre_nb_available_rbs = nb_rb;
-		    PHY_vars_eNB_g[mod_id][CC_id]->
-			mu_mimo_mode[UE_id].dl_pow_off =
-			ue_sched_ctl->dl_pow_off[CC_id];
-
-		    for (j = 0; j < flexran_get_N_RBG(mod_id, CC_id); j++) {
-			PHY_vars_eNB_g[mod_id][CC_id]->
-			    mu_mimo_mode[UE_id].rballoc_sub[j] =
-			    UE_list->
-			    UE_template[CC_id][UE_id].rballoc_subband
-			    [harq_pid][j];
-		    }
-
-		    // decrease mcs until TBS falls below required length
-		    while ((TBS > (sdu_length_total + header_len + ta_len))
-			   && (mcs_tmp > 0)) {
-			mcs_tmp--;
-			TBS = mac_xface->get_TBS_DL(mcs_tmp, nb_rb);
-		    }
-
-		    // if we have decreased too much or we don't have enough RBs, increase MCS
-		    while ((TBS < (sdu_length_total + header_len + ta_len))
-			   && (((ue_sched_ctl->dl_pow_off[CC_id] > 0)
-				&& (mcs_tmp < 28))
-			       || ((ue_sched_ctl->dl_pow_off[CC_id] == 0)
-				   && (mcs_tmp <= 15)))) {
-			mcs_tmp++;
-			TBS = mac_xface->get_TBS_DL(mcs_tmp, nb_rb);
-		    }
-
-		    dci_tbs = TBS;
-		    mcs = mcs_tmp;
-		    LOG_D(FLEXRAN_AGENT, "Final mcs was %d\n", mcs);
-
-		    dl_dci->has_aggr_level = 1;
-		    dl_dci->aggr_level = aggregation;
-
-		    UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid] =
-			nb_rb;
-
-		    if (flexran_get_duplex_mode(mod_id, CC_id) ==
-			PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-			UE_list->UE_template[CC_id][UE_id].DAI++;
-			//  printf("DAI update: subframeP %d: UE %d, DAI %d\n",subframeP,UE_id,UE_list->UE_template[CC_id][UE_id].DAI);
-			//#warning only for 5MHz channel
-			update_ul_dci(mod_id, CC_id, rnti,
-				      UE_list->UE_template[CC_id][UE_id].
-				      DAI);
-		    }
-		    // do PUCCH power control
-		    // this is the normalized RX power
-		    normalized_rx_power = flexran_get_p0_pucch_dbm(mod_id, UE_id, CC_id);	//eNB_UE_stats->Po_PUCCH_dBm; 
-		    target_rx_power = flexran_get_p0_nominal_pucch(mod_id, CC_id) + 20;	//mac_xface->get_target_pucch_rx_power(mod_id, CC_id) + 20;
-		    // this assumes accumulated tpc
-		    // make sure that we are only sending a tpc update once a frame, otherwise the control loop will freak out
-		    int32_t framex10psubframe =
-			UE_list->UE_template[CC_id][UE_id].
-			pucch_tpc_tx_frame * 10 +
-			UE_list->UE_template[CC_id][UE_id].
-			pucch_tpc_tx_subframe;
-
-		    if (((framex10psubframe + 10) <= (frame * 10 + subframe)) ||	//normal case
-			((framex10psubframe > (frame * 10 + subframe)) && (((10240 - framex10psubframe + frame * 10 + subframe) >= 10))))	//frame wrap-around
-			if (flexran_get_p0_pucch_status
-			    (mod_id, UE_id, CC_id) == 1) {
-			    flexran_update_p0_pucch(mod_id, UE_id, CC_id);
-
-			    UE_list->
-				UE_template[CC_id]
-				[UE_id].pucch_tpc_tx_frame = frame;
-			    UE_list->
-				UE_template[CC_id]
-				[UE_id].pucch_tpc_tx_subframe = subframe;
-			    if (normalized_rx_power >
-				(target_rx_power + 1)) {
-				tpc = 0;	//-1
-				tpc_accumulated--;
-			    } else if (normalized_rx_power <
-				       (target_rx_power - 1)) {
-				tpc = 2;	//+1
-				tpc_accumulated++;
-			    } else {
-				tpc = 1;	//0
-			    }
-			    LOG_D(MAC,
-				  "[eNB %d] DLSCH scheduler: frame %d, subframe %d, harq_pid %d, tpc %d, accumulated %d, normalized/target rx power %d/%d\n",
-				  mod_id, frame, subframe, harq_pid, tpc,
-				  tpc_accumulated, normalized_rx_power,
-				  target_rx_power);
-			}	// Po_PUCCH has been updated 
-			else {
-			    tpc = 1;	//0
-			}	// time to do TPC update 
-		    else {
-			tpc = 1;	//0
-		    }
-
-		    for (i = 0;
-			 i <
-			 PHY_vars_eNB_g[mod_id][CC_id]->frame_parms.N_RBG;
-			 i++) {
-			rballoc_sub[i] =
-			    UE_list->
-			    UE_template[CC_id][UE_id].rballoc_subband
-			    [harq_pid][i];
-		    }
-
-		    // Toggle NDI
-		    LOG_D(MAC,
-			  "CC_id %d Frame %d, subframeP %d: Toggling Format1 NDI for UE %d (rnti %x/%d) oldNDI %d\n",
-			  CC_id, frame, subframe, UE_id,
-			  UE_list->UE_template[CC_id][UE_id].rnti,
-			  harq_pid,
-			  UE_list->UE_template[CC_id][UE_id].
-			  oldNDI[harq_pid]);
-		    UE_list->UE_template[CC_id][UE_id].oldNDI[harq_pid] =
-			1 -
-			UE_list->UE_template[CC_id][UE_id].
-			oldNDI[harq_pid];
-		    ndi =
-			UE_list->UE_template[CC_id][UE_id].
-			oldNDI[harq_pid];
-
-		    UE_list->UE_template[CC_id][UE_id].mcs[harq_pid] = mcs;
-		    UE_list->UE_template[CC_id][UE_id].oldTPC[harq_pid] =
-			tpc;
-
-		    // Increase the pointer for the number of scheduled UEs
-		    num_ues_added++;
-		    ue_has_transmission = 1;
-		} else {	// There is no data from RLC or MAC header, so don't schedule
-		    ue_has_transmission = 0;
-		}
-	    }			// End of new scheduling
-
-	    // If we has transmission or retransmission
-	    if (ue_has_transmission) {
-		switch (mac_xface->
-			get_transmission_mode(mod_id, CC_id, rnti)) {
-		case 1:
-		case 2:
-		default:
-		    dl_dci->has_res_alloc = 1;
-		    dl_dci->res_alloc = 0;
-		    dl_dci->has_vrb_format = 1;
-		    dl_dci->vrb_format =
-			PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
-		    dl_dci->has_format = 1;
-		    dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1;
-		    dl_dci->has_rb_bitmap = 1;
-		    dl_dci->rb_bitmap =
-			allocate_prbs_sub(nb_rb, rballoc_sub);
-		    dl_dci->has_rb_shift = 1;
-		    dl_dci->rb_shift = 0;
-		    dl_dci->n_ndi = 1;
-		    dl_dci->ndi =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_ndi);
-		    dl_dci->ndi[0] = ndi;
-		    dl_dci->n_rv = 1;
-		    dl_dci->rv =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_rv);
-		    dl_dci->rv[0] = round & 3;
-		    dl_dci->has_tpc = 1;
-		    dl_dci->tpc = tpc;
-		    dl_dci->n_mcs = 1;
-		    dl_dci->mcs =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_mcs);
-		    dl_dci->mcs[0] = mcs;
-		    dl_dci->n_tbs_size = 1;
-		    dl_dci->tbs_size =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_tbs_size);
-		    dl_dci->tbs_size[0] = dci_tbs;
-		    if (flexran_get_duplex_mode(mod_id, CC_id) ==
-			PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-			dl_dci->has_dai = 1;
-			dl_dci->dai =
-			    (UE_list->UE_template[CC_id][UE_id].DAI -
-			     1) & 3;
-		    }
-		    break;
-		case 3:
-		    dl_dci->has_res_alloc = 1;
-		    dl_dci->res_alloc = 0;
-		    dl_dci->has_vrb_format = 1;
-		    dl_dci->vrb_format =
-			PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
-		    dl_dci->has_format = 1;
-		    dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A;
-		    dl_dci->has_rb_bitmap = 1;
-		    dl_dci->rb_bitmap =
-			allocate_prbs_sub(nb_rb, rballoc_sub);
-		    dl_dci->has_rb_shift = 1;
-		    dl_dci->rb_shift = 0;
-		    dl_dci->n_ndi = 2;
-		    dl_dci->ndi =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_ndi);
-		    dl_dci->ndi[0] = ndi;
-		    dl_dci->ndi[1] = ndi;
-		    dl_dci->n_rv = 2;
-		    dl_dci->rv =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_rv);
-		    dl_dci->rv[0] = round & 3;
-		    dl_dci->rv[1] = round & 3;
-		    dl_dci->has_tpc = 1;
-		    dl_dci->tpc = tpc;
-		    dl_dci->n_mcs = 2;
-		    dl_dci->mcs =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_mcs);
-		    dl_dci->mcs[0] = mcs;
-		    dl_dci->mcs[1] = mcs;
-		    dl_dci->n_tbs_size = 2;
-		    dl_dci->tbs_size =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_tbs_size);
-		    dl_dci->tbs_size[0] = dci_tbs;
-		    dl_dci->tbs_size[1] = dci_tbs;
-		    if (flexran_get_duplex_mode(mod_id, CC_id) ==
-			PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-			dl_dci->has_dai = 1;
-			dl_dci->dai =
-			    (UE_list->UE_template[CC_id][UE_id].DAI -
-			     1) & 3;
-		    }
-		    break;
-		case 4:
-		    dl_dci->has_res_alloc = 1;
-		    dl_dci->res_alloc = 0;
-		    dl_dci->has_vrb_format = 1;
-		    dl_dci->vrb_format =
-			PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
-		    dl_dci->has_format = 1;
-		    dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A;
-		    dl_dci->has_rb_bitmap = 1;
-		    dl_dci->rb_bitmap =
-			allocate_prbs_sub(nb_rb, rballoc_sub);
-		    dl_dci->has_rb_shift = 1;
-		    dl_dci->rb_shift = 0;
-		    dl_dci->n_ndi = 2;
-		    dl_dci->ndi =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_ndi);
-		    dl_dci->ndi[0] = ndi;
-		    dl_dci->ndi[1] = ndi;
-		    dl_dci->n_rv = 2;
-		    dl_dci->rv =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_rv);
-		    dl_dci->rv[0] = round & 3;
-		    dl_dci->rv[1] = round & 3;
-		    dl_dci->has_tpc = 1;
-		    dl_dci->tpc = tpc;
-		    dl_dci->n_mcs = 2;
-		    dl_dci->mcs =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_mcs);
-		    dl_dci->mcs[0] = mcs;
-		    dl_dci->mcs[1] = mcs;
-		    dl_dci->n_tbs_size = 2;
-		    dl_dci->tbs_size =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_tbs_size);
-		    dl_dci->tbs_size[0] = dci_tbs;
-		    dl_dci->tbs_size[1] = dci_tbs;
-		    if (flexran_get_duplex_mode(mod_id, CC_id) ==
-			PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-			dl_dci->has_dai = 1;
-			dl_dci->dai =
-			    (UE_list->UE_template[CC_id][UE_id].DAI -
-			     1) & 3;
-		    }
-		    break;
-		case 5:
-		    dl_dci->has_res_alloc = 1;
-		    dl_dci->res_alloc = 0;
-		    dl_dci->has_vrb_format = 1;
-		    dl_dci->vrb_format =
-			PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
-		    dl_dci->has_format = 1;
-		    dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D;
-		    dl_dci->has_rb_bitmap = 1;
-		    dl_dci->rb_bitmap =
-			allocate_prbs_sub(nb_rb, rballoc_sub);
-		    dl_dci->has_rb_shift = 1;
-		    dl_dci->rb_shift = 0;
-		    dl_dci->n_ndi = 1;
-		    dl_dci->ndi[0] = ndi;
-		    dl_dci->n_rv = 1;
-		    dl_dci->rv =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_rv);
-		    dl_dci->rv[0] = round & 3;
-		    dl_dci->has_tpc = 1;
-		    dl_dci->tpc = tpc;
-		    dl_dci->n_mcs = 1;
-		    dl_dci->mcs =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_mcs);
-		    dl_dci->mcs[0] = mcs;
-		    dl_dci->n_tbs_size = 1;
-		    dl_dci->tbs_size =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_tbs_size);
-		    dl_dci->tbs_size[0] = dci_tbs;
-		    if (flexran_get_duplex_mode(mod_id, CC_id) ==
-			PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-			dl_dci->has_dai = 1;
-			dl_dci->dai =
-			    (UE_list->UE_template[CC_id][UE_id].DAI -
-			     1) & 3;
-		    }
-
-		    if (ue_sched_ctl->dl_pow_off[CC_id] == 2) {
-			ue_sched_ctl->dl_pow_off[CC_id] = 1;
-		    }
-
-		    dl_dci->has_dl_power_offset = 1;
-		    dl_dci->dl_power_offset =
-			ue_sched_ctl->dl_pow_off[CC_id];
-		    dl_dci->has_precoding_info = 1;
-		    dl_dci->precoding_info = 5;	// Is this right??
-
-		    break;
-		case 6:
-		    dl_dci->has_res_alloc = 1;
-		    dl_dci->res_alloc = 0;
-		    dl_dci->has_vrb_format = 1;
-		    dl_dci->vrb_format =
-			PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
-		    dl_dci->has_format = 1;
-		    dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D;
-		    dl_dci->has_rb_bitmap = 1;
-		    dl_dci->rb_bitmap =
-			allocate_prbs_sub(nb_rb, rballoc_sub);
-		    dl_dci->has_rb_shift = 1;
-		    dl_dci->rb_shift = 0;
-		    dl_dci->n_ndi = 1;
-		    dl_dci->ndi =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_ndi);
-		    dl_dci->ndi[0] = ndi;
-		    dl_dci->n_rv = 1;
-		    dl_dci->rv =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_rv);
-		    dl_dci->rv[0] = round & 3;
-		    dl_dci->has_tpc = 1;
-		    dl_dci->tpc = tpc;
-		    dl_dci->n_mcs = 1;
-		    dl_dci->mcs =
-			(uint32_t *) malloc(sizeof(uint32_t) *
-					    dl_dci->n_mcs);
-		    dl_dci->mcs[0] = mcs;
-		    if (flexran_get_duplex_mode(mod_id, CC_id) ==
-			PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-			dl_dci->has_dai = 1;
-			dl_dci->dai =
-			    (UE_list->UE_template[CC_id][UE_id].DAI -
-			     1) & 3;
-		    }
-
-		    dl_dci->has_dl_power_offset = 1;
-		    dl_dci->dl_power_offset =
-			ue_sched_ctl->dl_pow_off[CC_id];
-		    dl_dci->has_precoding_info = 1;
-		    dl_dci->precoding_info = 5;	// Is this right??
-		    break;
-		}
-	    }
-
-	    if (flexran_get_duplex_mode(mod_id, CC_id) ==
-		PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-
-		/* TODO */
-		//set_ul_DAI(mod_id, UE_id, CC_id, frame, subframe, frame_parms);
-	    }
-	}			// UE_id loop
-    }				// CC_id loop
-
-    // Add all the dl_data elements to the flexran message
-    int offset = (*dl_info)->dl_mac_config_msg->n_dl_ue_data;
-    (*dl_info)->dl_mac_config_msg->n_dl_ue_data += num_ues_added;
-    if (num_ues_added > 0) {
-	(*dl_info)->dl_mac_config_msg->dl_ue_data =
-	    (Protocol__FlexDlData **)
-	    realloc((*dl_info)->dl_mac_config_msg->dl_ue_data,
-		    sizeof(Protocol__FlexDlData *) *
-		    ((*dl_info)->dl_mac_config_msg->n_dl_ue_data));
-	if ((*dl_info)->dl_mac_config_msg->dl_ue_data == NULL) {
-	    LOG_E(MAC, "Request for memory reallocation failed\n");
-	    return;
-	}
-	for (i = 0; i < num_ues_added; i++) {
-	    (*dl_info)->dl_mac_config_msg->dl_ue_data[offset + i] =
-		dl_data[i];
-	}
-    }
-
-    stop_meas(&eNB->schedule_dlsch);
-    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
-	(VCD_SIGNAL_DUMPER_FUNCTIONS_SCHEDULE_DLSCH, VCD_FUNCTION_OUT);
-}
diff --git a/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.c b/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.c
deleted file mode 100644
index 68c86f1da54671e4110c5bb6bb8a610be210725a..0000000000000000000000000000000000000000
--- a/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.c
+++ /dev/null
@@ -1,202 +0,0 @@
-/*
- * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The OpenAirInterface Software Alliance licenses this file to You under
- * the OAI Public License, Version 1.1  (the "License"); you may not use this file
- * except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.openairinterface.org/?page_id=698
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *-------------------------------------------------------------------------------
- * For more information about the OpenAirInterface (OAI) Software Alliance:
- *      contact@openairinterface.org
- */
-
-/*! \file flexran_agent_scheduler_dlsch_ue_remote.c
- * \brief procedures related to remote scheduling in the DLSCH transport channel
- * \author Xenofon Foukas
- * \date 2016
- * \email: x.foukas@sms.ed.ac.uk
- * \version 0.1
- * @ingroup _mac
-
- */
-
-#include "flexran_agent_common_internal.h"
-
-#include "flexran_agent_scheduler_dlsch_ue_remote.h"
-
-#include "LAYER2/MAC/defs.h"
-#include "LAYER2/MAC/extern.h"
-
-struct DlMacConfigHead queue_head;
-
-int queue_initialized = 0;
-
-//uint32_t skip_subframe = 1;
-//uint32_t period = 10;
-//uint32_t sched [] = {1, 2, 3};
-
-void
-flexran_schedule_ue_spec_remote(mid_t mod_id, uint32_t frame,
-				uint32_t subframe, int *mbsfn_flag,
-				Protocol__FlexranMessage ** dl_info)
-{
-
-
-    //if ((subframe == skip_subframe) && (frame % period == 0)) {
-    //  LOG_I(MAC, "Will skip subframe %d %d\n", subframe, frame);
-    //  for (int i = 0; i < 3; i++) {
-    //    LOG_I(MAC, "%d\n", sched[i]);
-    //  }
-    //}
-
-    /* if (frame == 500 && subframe == 1) { */
-    /*   char policy[] = "rrc: \n - ul_scheduler: \n    behavior : tester_function\n    parameters:\n      period: !!int 3\nmac: \n - dl_scheduler: \n    parameters: \n      period : !!int 40\n      skip_subframe : !!int 3\n      sched : [!!int 4, !!int 5, !!int 6]"; */
-    /*   apply_reconfiguration_policy(mod_id, policy, strlen(policy)); */
-    /* } */
-
-    eNB_MAC_INST *eNB;
-
-    if (!queue_initialized) {
-	TAILQ_INIT(&queue_head);
-	queue_initialized = 1;
-    }
-
-    eNB = &eNB_mac_inst[mod_id];
-
-    dl_mac_config_element_t *dl_config_elem;
-
-    int diff;
-    LOG_D(MAC, "[TEST] Current frame and subframe %d, %d\n", frame,
-	  subframe);
-    // First we check to see if we have a scheduling decision for this sfn_sf already in our queue
-    while (queue_head.tqh_first != NULL) {
-	dl_config_elem = queue_head.tqh_first;
-
-	diff =
-	    get_sf_difference(mod_id,
-			      dl_config_elem->dl_info->
-			      dl_mac_config_msg->sfn_sf);
-	// Check if this decision is for now, for a later or a previous subframe
-	if (diff == 0) {	// Now
-	    LOG_D(MAC,
-		  "Found a decision for this subframe in the queue. Let's use it!\n");
-	    TAILQ_REMOVE(&queue_head, queue_head.tqh_first, configs);
-	    *dl_info = dl_config_elem->dl_info;
-	    free(dl_config_elem);
-	    eNB->eNB_stats[mod_id].sched_decisions++;
-	    return;
-	} else if (diff < 0) {	//previous subframe , delete message and free memory
-	    LOG_D(MAC,
-		  "Found a decision for a previous subframe in the queue. Let's get rid of it\n");
-	    TAILQ_REMOVE(&queue_head, queue_head.tqh_first, configs);
-	    flexran_agent_mac_destroy_dl_config(dl_config_elem->dl_info);
-	    free(dl_config_elem);
-	    eNB->eNB_stats[mod_id].sched_decisions++;
-	    eNB->eNB_stats[mod_id].missed_deadlines++;
-	} else {		// next subframe, nothing to do now
-	    LOG_D(MAC,
-		  "Found a decision for a future subframe in the queue. Nothing to do now\n");
-	    flexran_agent_mac_create_empty_dl_config(mod_id, dl_info);
-	    return;
-	}
-    }
-
-    //Done with the local cache. Now we need to check if something new arrived
-    flexran_agent_get_pending_dl_mac_config(mod_id, dl_info);
-    while (*dl_info != NULL) {
-
-	diff =
-	    get_sf_difference(mod_id,
-			      (*dl_info)->dl_mac_config_msg->sfn_sf);
-	if (diff == 0) {	// Got a command for this sfn_sf
-	    LOG_D(MAC,
-		  "Found a decision for this subframe pending. Let's use it\n");
-	    eNB->eNB_stats[mod_id].sched_decisions++;
-	    return;
-	} else if (diff < 0) {
-	    LOG_D(MAC,
-		  "Found a decision for a previous subframe. Let's get rid of it\n");
-	    flexran_agent_mac_destroy_dl_config(*dl_info);
-	    *dl_info = NULL;
-	    flexran_agent_get_pending_dl_mac_config(mod_id, dl_info);
-	    eNB->eNB_stats[mod_id].sched_decisions++;
-	    eNB->eNB_stats[mod_id].missed_deadlines++;
-	} else {		// Intended for future subframe. Store it in local cache
-	    LOG_D(MAC,
-		  "Found a decision for a future subframe in the queue. Let's store it in the cache\n");
-	    dl_mac_config_element_t *e =
-		malloc(sizeof(dl_mac_config_element_t));
-	    e->dl_info = *dl_info;
-	    TAILQ_INSERT_TAIL(&queue_head, e, configs);
-	    flexran_agent_mac_create_empty_dl_config(mod_id, dl_info);
-	    // No need to look for another. Messages arrive ordered
-	    return;
-	}
-    }
-
-    // We found no pending command, so we will simply pass an empty one
-    flexran_agent_mac_create_empty_dl_config(mod_id, dl_info);
-}
-
-int get_sf_difference(mid_t mod_id, uint32_t sfn_sf)
-{
-    int diff_in_subframes;
-
-    uint16_t current_frame = flexran_get_current_system_frame_num(mod_id);
-    uint16_t current_subframe = flexran_get_current_subframe(mod_id);
-    uint32_t current_sfn_sf = flexran_get_sfn_sf(mod_id);
-
-    if (sfn_sf == current_sfn_sf) {
-	return 0;
-    }
-
-    uint16_t frame_mask = ((1 << 12) - 1);
-    uint16_t frame = (sfn_sf & (frame_mask << 4)) >> 4;
-
-    uint16_t sf_mask = ((1 << 4) - 1);
-    uint16_t subframe = (sfn_sf & sf_mask);
-
-    LOG_D(MAC, "[TEST] Target frame and subframe %d, %d\n", frame,
-	  subframe);
-
-    if (frame == current_frame) {
-	return subframe - current_subframe;
-    } else if (frame > current_frame) {
-	diff_in_subframes =
-	    ((frame * 10) + subframe) - ((current_frame * 10) +
-					 current_subframe);
-
-	//    diff_in_subframes = 9 - current_subframe;
-	//diff_in_subframes += (subframe + 1);
-	//diff_in_subframes += (frame-2) * 10;
-	if (diff_in_subframes > SCHED_AHEAD_SUBFRAMES) {
-	    return -1;
-	} else {
-	    return 1;
-	}
-    } else {			//frame < current_frame
-	//diff_in_subframes = 9 - current_subframe;
-	//diff_in_subframes += (subframe + 1);
-	//if (frame > 0) {
-	//  diff_in_subframes += (frame - 1) * 10;
-	//}
-	//diff_in_subframes += (1023 - current_frame) * 10;
-	diff_in_subframes =
-	    10240 - ((current_frame * 10) + current_subframe) +
-	    ((frame * 10) + subframe);
-	if (diff_in_subframes > SCHED_AHEAD_SUBFRAMES) {
-	    return -1;
-	} else {
-	    return 1;
-	}
-    }
-}
diff --git a/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.h b/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.h
deleted file mode 100644
index 449ba1e8cde85e8c6001178f9e68534590b9952f..0000000000000000000000000000000000000000
--- a/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.h
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The OpenAirInterface Software Alliance licenses this file to You under
- * the OAI Public License, Version 1.1  (the "License"); you may not use this file
- * except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.openairinterface.org/?page_id=698
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *-------------------------------------------------------------------------------
- * For more information about the OpenAirInterface (OAI) Software Alliance:
- *      contact@openairinterface.org
- */
-
-/*! \file flexran_agent_scheduler_dlsch_ue_remote.h
- * \brief Local stub for remote scheduler used by the controller
- * \author Xenofon Foukas
- * \date 2016
- * \email: x.foukas@sms.ed.ac.uk
- * \version 0.1
- * @ingroup _mac
-
- */
-
-#ifndef __LAYER2_MAC_FLEXRAN_AGENT_SCHEDULER_DLSCH_UE_REMOTE_H__
-#define __LAYER2_MAC_FLEXRAN_AGENT_SCHEDULER_DLSCH_UE_REMOTE_H___
-
-#include "flexran.pb-c.h"
-#include "header.pb-c.h"
-
-#include "ENB_APP/flexran_agent_defs.h"
-#include "flexran_agent_mac.h"
-#include "LAYER2/MAC/flexran_agent_mac_proto.h"
-
-#include <sys/queue.h>
-
-// Maximum value of schedule ahead of time
-// Required to identify if a dl_command is for the future or not
-#define SCHED_AHEAD_SUBFRAMES 20
-
-typedef struct dl_mac_config_element_s {
-    Protocol__FlexranMessage *dl_info;
-     TAILQ_ENTRY(dl_mac_config_element_s) configs;
-} dl_mac_config_element_t;
-
-TAILQ_HEAD(DlMacConfigHead, dl_mac_config_element_s);
-
-/*
- * Default scheduler used by the eNB agent
- */
-void flexran_schedule_ue_spec_remote(mid_t mod_id, uint32_t frame,
-				     uint32_t subframe, int *mbsfn_flag,
-				     Protocol__FlexranMessage ** dl_info);
-
-
-// Find the difference in subframes from the given subframe
-// negative for older value
-// 0 for equal
-// positive for future value
-// Based on  
-int get_sf_difference(mid_t mod_id, uint32_t sfn_sf);
-
-#endif
diff --git a/openair2/LAYER2/MAC/flexran_dci_conversions.h b/openair2/LAYER2/MAC/flexran_dci_conversions.h
deleted file mode 100644
index 5c18b431043f47033b739f314c1ea98ea3ab20cb..0000000000000000000000000000000000000000
--- a/openair2/LAYER2/MAC/flexran_dci_conversions.h
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The OpenAirInterface Software Alliance licenses this file to You under
- * the OAI Public License, Version 1.1  (the "License"); you may not use this file
- * except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.openairinterface.org/?page_id=698
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *-------------------------------------------------------------------------------
- * For more information about the OpenAirInterface (OAI) Software Alliance:
- *      contact@openairinterface.org
- */
-
-/*! \file flexran_dci_conversions.h
- * \brief Conversion helpers from flexran messages to OAI formats DCI  
- * \author Xenofon Foukas
- * \date 2016
- * \version 0.1
- */
-
-#ifndef LAYER2_MAC_FLEXRAN_DCI_CONVERISIONS_H__
-#define LAYER2_MAC_DCI_FLEXRAN_CONVERISIONS_H__
-
-#define FILL_DCI_FDD_1(TYPE, DCI, FLEXRAN_DCI) \
-  ((TYPE*)DCI)->harq_pid = FLEXRAN_DCI->harq_process; \
-  ((TYPE*)DCI)->rv = FLEXRAN_DCI->rv[0]; \
-  ((TYPE*)DCI)->rballoc = FLEXRAN_DCI->rb_bitmap; \
-  ((TYPE*)DCI)->rah = FLEXRAN_DCI->res_alloc; \
-  ((TYPE*)DCI)->mcs = FLEXRAN_DCI->mcs[0]; \
-  ((TYPE*)DCI)->TPC = FLEXRAN_DCI->tpc; \
-  ((TYPE*)DCI)->ndi = FLEXRAN_DCI->ndi[0];
-
-#define FILL_DCI_TDD_1(TYPE, DCI, FLEXRAN_DCI) \
-  ((TYPE*)DCI)->harq_pid = FLEXRAN_DCI->harq_process; \
-  ((TYPE*)DCI)->rv = FLEXRAN_DCI->rv[0]; \
-  ((TYPE*)DCI)->dai = FLEXRAN_DCI->dai; \
-  ((TYPE*)DCI)->rballoc = FLEXRAN_DCI->rb_bitmap; \
-  ((TYPE*)DCI)->rah = FLEXRAN_DCI->res_alloc; \
-  ((TYPE*)DCI)->mcs = FLEXRAN_DCI->mcs[0]; \
-  ((TYPE*)DCI)->TPC = FLEXRAN_DCI->tpc; \
-  ((TYPE*)DCI)->ndi = FLEXRAN_DCI->ndi[0];
-
-#endif
diff --git a/openair2/LAYER2/MAC/pre_processor.c b/openair2/LAYER2/MAC/pre_processor.c
index 0d6a6ec3882530d7d4caca0d3ec91d1d240e5a21..759bc591dd5133cdbcc302ebf7b61f48b03a86a1 100644
--- a/openair2/LAYER2/MAC/pre_processor.c
+++ b/openair2/LAYER2/MAC/pre_processor.c
@@ -52,11 +52,18 @@
 #include "rlc.h"
 
 
-
 #define DEBUG_eNB_SCHEDULER 1
 #define DEBUG_HEADER_PARSING 1
 //#define DEBUG_PACKET_TRACE 1
 
+extern float slice_percentage[MAX_NUM_SLICES];
+extern float slice_percentage_uplink[MAX_NUM_SLICES];
+extern uint32_t sorting_policy[MAX_NUM_SLICES];
+
+extern int slice_maxmcs[MAX_NUM_SLICES];
+extern int slice_maxmcs_uplink[MAX_NUM_SLICES];
+
+
 //#define ICIC 0
 
 /* this function checks that get_eNB_UE_stats returns
@@ -90,7 +97,7 @@ int phy_stats_exist(module_id_t Mod_id, int rnti)
 
 // This function stores the downlink buffer for all the logical channels
 void
-store_dlsch_buffer(module_id_t Mod_id, frame_t frameP,
+store_dlsch_buffer(module_id_t Mod_id, slice_id_t slice_id, frame_t frameP,
 		   sub_frame_t subframeP)
 {
 
@@ -104,6 +111,9 @@ store_dlsch_buffer(module_id_t Mod_id, frame_t frameP,
 	if (UE_list->active[UE_id] != TRUE)
 	    continue;
 
+	if (!ue_slice_membership(UE_id, slice_id))
+	    continue;
+
 	UE_template =
 	    &UE_list->UE_template[UE_PCCID(Mod_id, UE_id)][UE_id];
 
@@ -147,8 +157,8 @@ store_dlsch_buffer(module_id_t Mod_id, frame_t frameP,
 	     */
 	    if (UE_template->dl_buffer_info[i] > 0)
 		LOG_D(MAC,
-		      "[eNB %d] Frame %d Subframe %d : RLC status for UE %d in LCID%d: total of %d pdus and size %d, head sdu queuing time %d, remaining size %d, is segmeneted %d \n",
-		      Mod_id, frameP, subframeP, UE_id,
+		      "[eNB %d][SLICE %d] Frame %d Subframe %d : RLC status for UE %d in LCID%d: total of %d pdus and size %d, head sdu queuing time %d, remaining size %d, is segmeneted %d \n",
+		      Mod_id, slice_id, frameP, subframeP, UE_id,
 		      i, UE_template->dl_pdus_in_buffer[i],
 		      UE_template->dl_buffer_info[i],
 		      UE_template->dl_buffer_head_sdu_creation_time[i],
@@ -176,6 +186,7 @@ store_dlsch_buffer(module_id_t Mod_id, frame_t frameP,
 // This function returns the estimated number of RBs required by each UE for downlink scheduling
 void
 assign_rbs_required(module_id_t Mod_id,
+			slice_id_t slice_id,
 		    frame_t frameP,
 		    sub_frame_t subframe,
 		    uint16_t
@@ -194,7 +205,8 @@ assign_rbs_required(module_id_t Mod_id,
     for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
 	if (UE_list->active[UE_id] != TRUE)
 	    continue;
-
+	if (!ue_slice_membership(UE_id, slice_id))
+	    continue;
 	pCCid = UE_PCCID(Mod_id, UE_id);
 
 	//update CQI information across component carriers
@@ -203,8 +215,8 @@ assign_rbs_required(module_id_t Mod_id,
 	    CC_id = UE_list->ordered_CCids[n][UE_id];
 	    eNB_UE_stats = &UE_list->eNB_UE_stats[CC_id][UE_id];
 
-	    eNB_UE_stats->dlsch_mcs1 =
-		cqi_to_mcs[UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id]];
+	    eNB_UE_stats->dlsch_mcs1 =cmin(cqi_to_mcs[UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id]],
+									   slice_maxmcs[slice_id]);
 
 	}
 
@@ -256,16 +268,17 @@ assign_rbs_required(module_id_t Mod_id,
 		    to_prb(RC.mac[Mod_id]->common_channels[CC_id].
 			   mib->message.dl_Bandwidth);
 
+		UE_list->UE_sched_ctrl[UE_id].max_rbs_allowed_slice[CC_id][slice_id]= flexran_nb_rbs_allowed_slice(slice_percentage[slice_id],N_RB_DL);
+
 		/* calculating required number of RBs for each UE */
 		while (TBS <
 		       UE_list->UE_template[pCCid][UE_id].
 		       dl_buffer_total) {
 		    nb_rbs_required[CC_id][UE_id] += min_rb_unit[CC_id];
 
-		    if (nb_rbs_required[CC_id][UE_id] > N_RB_DL) {
-			TBS =
-			    get_TBS_DL(eNB_UE_stats->dlsch_mcs1, N_RB_DL);
-			nb_rbs_required[CC_id][UE_id] = N_RB_DL;
+		    if (nb_rbs_required[CC_id][UE_id] > UE_list->UE_sched_ctrl[UE_id].max_rbs_allowed_slice[CC_id][slice_id]) {
+			TBS = get_TBS_DL(eNB_UE_stats->dlsch_mcs1, UE_list->UE_sched_ctrl[UE_id].max_rbs_allowed_slice[CC_id][slice_id]);
+			nb_rbs_required[CC_id][UE_id] = UE_list->UE_sched_ctrl[UE_id].max_rbs_allowed_slice[CC_id][slice_id];
 			break;
 		    }
 
@@ -339,6 +352,7 @@ struct sort_ue_dl_params {
     int Mod_idP;
     int frameP;
     int subframeP;
+    int slice_id;
 };
 
 static int ue_dl_compare(const void *_a, const void *_b, void *_params)
@@ -346,62 +360,73 @@ static int ue_dl_compare(const void *_a, const void *_b, void *_params)
     struct sort_ue_dl_params *params = _params;
     UE_list_t *UE_list = &RC.mac[params->Mod_idP]->UE_list;
 
+	int i;
+	int slice_id = params->slice_id;
     int UE_id1 = *(const int *) _a;
     int UE_id2 = *(const int *) _b;
 
     int rnti1 = UE_RNTI(params->Mod_idP, UE_id1);
     int pCC_id1 = UE_PCCID(params->Mod_idP, UE_id1);
-    int round1 =
-	maxround(params->Mod_idP, rnti1, params->frameP, params->subframeP,
-		 1);
+    int round1 = maxround(params->Mod_idP, rnti1, params->frameP, params->subframeP, 1);
 
     int rnti2 = UE_RNTI(params->Mod_idP, UE_id2);
     int pCC_id2 = UE_PCCID(params->Mod_idP, UE_id2);
-    int round2 =
-	maxround(params->Mod_idP, rnti2, params->frameP, params->subframeP,
-		 1);
+    int round2 = maxround(params->Mod_idP, rnti2, params->frameP, params->subframeP, 1);
 
     int cqi1 = maxcqi(params->Mod_idP, UE_id1);
     int cqi2 = maxcqi(params->Mod_idP, UE_id2);
 
-    if (round1 > round2)
-	return -1;
-    if (round1 < round2)
-	return 1;
-
-    if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] +
-	UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2] >
-	UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] +
-	UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])
-	return -1;
-    if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] +
-	UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2] <
-	UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] +
-	UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])
-	return 1;
-
-    if (UE_list->
-	UE_template[pCC_id1][UE_id1].dl_buffer_head_sdu_creation_time_max >
-	UE_list->
-	UE_template[pCC_id2][UE_id2].dl_buffer_head_sdu_creation_time_max)
-	return -1;
-    if (UE_list->
-	UE_template[pCC_id1][UE_id1].dl_buffer_head_sdu_creation_time_max <
-	UE_list->
-	UE_template[pCC_id2][UE_id2].dl_buffer_head_sdu_creation_time_max)
-	return 1;
-
-    if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total >
-	UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total)
-	return -1;
-    if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total <
-	UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total)
-	return 1;
-
-    if (cqi1 > cqi2)
-	return -1;
-    if (cqi1 < cqi2)
-	return 1;
+  for (i = 0; i < CR_NUM; ++i) {
+    switch (UE_list->sorting_criteria[slice_id][i]) {
+
+      case CR_ROUND :
+        if (round1 > round2)
+          return -1;
+        if (round1 < round2)
+          return 1;
+        break;
+
+      case CR_SRB12 :
+        if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] +
+            UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2] >
+            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] +
+            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])
+          return -1;
+        if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] +
+            UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2] <
+            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] +
+            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])
+          return 1;
+        break;
+
+      case CR_HOL :
+        if (UE_list-> UE_template[pCC_id1][UE_id1].dl_buffer_head_sdu_creation_time_max >
+            UE_list-> UE_template[pCC_id2][UE_id2].dl_buffer_head_sdu_creation_time_max)
+          return -1;
+        if (UE_list-> UE_template[pCC_id1][UE_id1].dl_buffer_head_sdu_creation_time_max <
+            UE_list-> UE_template[pCC_id2][UE_id2].dl_buffer_head_sdu_creation_time_max)
+          return 1;
+        break;
+
+      case CR_LC :
+        if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total >
+            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total)
+          return -1;
+        if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total <
+            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total)
+          return 1;
+        break;
+
+      case CR_CQI :
+        if (cqi1 > cqi2)
+          return -1;
+        if (cqi1 < cqi2)
+          return 1;
+
+      default :
+        break;
+    }
+  }
 
     return 0;
 #if 0
@@ -436,40 +461,64 @@ static int ue_dl_compare(const void *_a, const void *_b, void *_params)
 #endif
 }
 
+void decode_sorting_policy(module_id_t Mod_idP, slice_id_t slice_id) {
+	int i;
+
+	UE_list_t *UE_list = &RC.mac[Mod_idP]->UE_list;
+	uint32_t policy = sorting_policy[slice_id];
+	uint32_t mask = 0x0000000F;
+    uint16_t criterion;
+
+	for(i = 0; i < CR_NUM; ++i) {
+		criterion = (uint16_t)(policy >> 4*(CR_NUM - 1 - i) & mask);
+        if (criterion >= CR_NUM) {
+          LOG_W(MAC, "Invalid criterion in slice %d policy, revert to default policy \n", slice_id);
+          sorting_policy[slice_id] = 0x1234;
+          break;
+        }
+      UE_list->sorting_criteria[slice_id][i] = criterion;
+	}
+}
+
+
 // This fuction sorts the UE in order their dlsch buffer and CQI
-void sort_UEs(module_id_t Mod_idP, int frameP, sub_frame_t subframeP)
+void sort_UEs(module_id_t Mod_idP, slice_id_t slice_id, int frameP, sub_frame_t subframeP)
 {
     int i;
     int list[NUMBER_OF_UE_MAX];
     int list_size = 0;
     int rnti;
-    struct sort_ue_dl_params params = { Mod_idP, frameP, subframeP };
+    struct sort_ue_dl_params params = { Mod_idP, frameP, subframeP, slice_id };
 
     UE_list_t *UE_list = &RC.mac[Mod_idP]->UE_list;
 
-    for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
+	for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
 
-	if (UE_list->active[i] == FALSE)
-	    continue;
-	if ((rnti = UE_RNTI(Mod_idP, i)) == NOT_A_RNTI)
-	    continue;
-	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-	    continue;
+		if (UE_list->active[i] == FALSE)
+			continue;
+		if ((rnti = UE_RNTI(Mod_idP, i)) == NOT_A_RNTI)
+			continue;
+		if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+			continue;
+		if (!ue_slice_membership(i, slice_id))
+			continue;
 
-	list[list_size] = i;
-	list_size++;
-    }
+		list[list_size] = i;
+		list_size++;
+	}
 
-    qsort_r(list, list_size, sizeof(int), ue_dl_compare, &params);
+	decode_sorting_policy(Mod_idP, slice_id);
 
-    if (list_size) {
-	for (i = 0; i < list_size - 1; i++)
-	    UE_list->next[list[i]] = list[i + 1];
-	UE_list->next[list[list_size - 1]] = -1;
-	UE_list->head = list[0];
-    } else {
-	UE_list->head = -1;
-    }
+	qsort_r(list, list_size, sizeof(int), ue_dl_compare, &params);
+
+	if (list_size) {
+		for (i = 0; i < list_size - 1; i++)
+			UE_list->next[list[i]] = list[i + 1];
+		UE_list->next[list[list_size - 1]] = -1;
+		UE_list->head = list[0];
+	} else {
+		UE_list->head = -1;
+	}
 
 #if 0
 
@@ -550,13 +599,14 @@ void sort_UEs(module_id_t Mod_idP, int frameP, sub_frame_t subframeP)
 // This function assigns pre-available RBS to each UE in specified sub-bands before scheduling is done
 void
 dlsch_scheduler_pre_processor(module_id_t Mod_id,
+				  slice_id_t slice_id,
 			      frame_t frameP,
 			      sub_frame_t subframeP,
 			      int N_RBG[MAX_NUM_CCs], int *mbsfn_flag)
 {
 
     unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX], harq_pid =
-	0, round = 0, total_ue_count;
+		0, round = 0, total_ue_count[MAX_NUM_CCs], total_rbs_used[MAX_NUM_CCs];
     unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX];
     int UE_id, i;
     uint16_t ii, j;
@@ -600,6 +650,9 @@ dlsch_scheduler_pre_processor(module_id_t Mod_id,
 	    if (UE_list->active[i] != TRUE)
 		continue;
 
+            if (!ue_slice_membership(i, slice_id))
+                continue;
+
 	    UE_id = i;
 	    // Initialize scheduling information for all active UEs
 
@@ -613,6 +666,8 @@ dlsch_scheduler_pre_processor(module_id_t Mod_id,
 						N_RBG[CC_id],
 						nb_rbs_required,
 						nb_rbs_required_remaining,
+						total_ue_count,
+						total_rbs_used,
 						rballoc_sub,
 						MIMO_mode_indicator);
 
@@ -621,78 +676,103 @@ dlsch_scheduler_pre_processor(module_id_t Mod_id,
 
 
     // Store the DLSCH buffer for each logical channel
-    store_dlsch_buffer(Mod_id, frameP, subframeP);
+    store_dlsch_buffer(Mod_id, slice_id,frameP, subframeP);
 
 
 
     // Calculate the number of RBs required by each UE on the basis of logical channel's buffer
-    assign_rbs_required(Mod_id, frameP, subframeP, nb_rbs_required,
+    assign_rbs_required(Mod_id, slice_id, frameP, subframeP, nb_rbs_required,
 			min_rb_unit);
 
 
 
     // Sorts the user on the basis of dlsch logical channel buffer and CQI
-    sort_UEs(Mod_id, frameP, subframeP);
-
+    sort_UEs(Mod_id, slice_id, frameP, subframeP);
 
 
-    total_ue_count = 0;
-
     // loop over all active UEs
     for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
-	rnti = UE_RNTI(Mod_id, i);
+		rnti = UE_RNTI(Mod_id, i);
 
-	if (rnti == NOT_A_RNTI)
-	    continue;
-	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-	    continue;
-	UE_id = i;
+		if (rnti == NOT_A_RNTI)
+			continue;
+		if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+			continue;
+		UE_id = i;
+		if (!ue_slice_membership(UE_id, slice_id))
+			continue;
 
-	for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
-	    CC_id = UE_list->ordered_CCids[ii][UE_id];
-	    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-	    cc = &RC.mac[Mod_id]->common_channels[ii];
-	    if (cc->tdd_Config)
-		harq_pid = ((frameP * 10) + subframeP) % 10;
-	    else
-		harq_pid = ((frameP * 10) + subframeP) & 7;
-	    round = ue_sched_ctl->round[CC_id][harq_pid];
+		for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
+			CC_id = UE_list->ordered_CCids[ii][UE_id];
+			ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+			cc = &RC.mac[Mod_id]->common_channels[ii];
+			if (cc->tdd_Config)
+				harq_pid = ((frameP * 10) + subframeP) % 10;
+			else
+				harq_pid = ((frameP * 10) + subframeP) & 7;
+			round = ue_sched_ctl->round[CC_id][harq_pid];
+
+			average_rbs_per_user[CC_id] = 0;
+
+
+			if (round != 8) {
+				nb_rbs_required[CC_id][UE_id] =
+					UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
+				total_rbs_used[CC_id]+=nb_rbs_required[CC_id][UE_id];
+			}
 
-	    average_rbs_per_user[CC_id] = 0;
+			//nb_rbs_required_remaining[UE_id] = nb_rbs_required[UE_id];
+			if (nb_rbs_required[CC_id][UE_id] > 0) {
+				total_ue_count[CC_id] = total_ue_count[CC_id] + 1;
+			}
 
+		}
+    }
 
-	    if (round != 8) {
-		nb_rbs_required[CC_id][UE_id] =
-		    UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
-	    }
-	    //nb_rbs_required_remaining[UE_id] = nb_rbs_required[UE_id];
-	    if (nb_rbs_required[CC_id][UE_id] > 0) {
-		total_ue_count = total_ue_count + 1;
-	    }
-	    // hypothetical assignment
-	    /*
-	     * If schedule is enabled and if the priority of the UEs is modified
-	     * The average rbs per logical channel per user will depend on the level of
-	     * priority. Concerning the hypothetical assignement, we should assign more
-	     * rbs to prioritized users. Maybe, we can do a mapping between the
-	     * average rbs per user and the level of priority or multiply the average rbs
-	     * per user by a coefficient which represents the degree of priority.
-	     */
+ // loop over all active UEs and calculate avg rb per user based on total active UEs
+    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
+		rnti = UE_RNTI(Mod_id, i);
 
-	    N_RB_DL =
-		to_prb(RC.mac[Mod_id]->common_channels[CC_id].mib->
-		       message.dl_Bandwidth);
+		if (rnti == NOT_A_RNTI)
+			continue;
+		if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+			continue;
+		UE_id = i;
+		if (!ue_slice_membership(UE_id, slice_id))
+			continue;
 
-	    if (total_ue_count == 0) {
-		average_rbs_per_user[CC_id] = 0;
-	    } else if ((min_rb_unit[CC_id] * total_ue_count) <= (N_RB_DL)) {
-		average_rbs_per_user[CC_id] =
-		    (uint16_t) floor(N_RB_DL / total_ue_count);
-	    } else {
-		average_rbs_per_user[CC_id] = min_rb_unit[CC_id];	// consider the total number of use that can be scheduled UE
-	    }
+		for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
+			CC_id = UE_list->ordered_CCids[ii][UE_id];
+
+			// hypothetical assignment
+			/*
+			 * If schedule is enabled and if the priority of the UEs is modified
+			 * The average rbs per logical channel per user will depend on the level of
+			 * priority. Concerning the hypothetical assignement, we should assign more
+			 * rbs to prioritized users. Maybe, we can do a mapping between the
+			 * average rbs per user and the level of priority or multiply the average rbs
+			 * per user by a coefficient which represents the degree of priority.
+			 */
+
+
+			N_RB_DL =
+				to_prb(RC.mac[Mod_id]->common_channels[CC_id].mib->
+					   message.dl_Bandwidth) - total_rbs_used[CC_id];
+
+			//recalcualte based on the what is left after retransmission
+			ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+			ue_sched_ctl->max_rbs_allowed_slice[CC_id][slice_id]= flexran_nb_rbs_allowed_slice(slice_percentage[slice_id],N_RB_DL);
+
+			if (total_ue_count[CC_id] == 0) {
+				average_rbs_per_user[CC_id] = 0;
+			} else if ((min_rb_unit[CC_id] * total_ue_count[CC_id]) <= (ue_sched_ctl->max_rbs_allowed_slice[CC_id][slice_id])) {
+				average_rbs_per_user[CC_id] =
+					(uint16_t) floor(ue_sched_ctl->max_rbs_allowed_slice[CC_id][slice_id] / total_ue_count[CC_id]);
+			} else {
+				average_rbs_per_user[CC_id] = min_rb_unit[CC_id];	// consider the total number of use that can be scheduled UE
+			}
+		}
 	}
-    }
 
     // note: nb_rbs_required is assigned according to total_buffer_dl
     // extend nb_rbs_required to capture per LCID RB required
@@ -703,6 +783,8 @@ dlsch_scheduler_pre_processor(module_id_t Mod_id,
 	    continue;
 	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
 	    continue;
+	if (!ue_slice_membership(i, slice_id))
+		continue;
 
 	for (ii = 0; ii < UE_num_active_CC(UE_list, i); ii++) {
 	    CC_id = UE_list->ordered_CCids[ii][i];
@@ -759,11 +841,14 @@ dlsch_scheduler_pre_processor(module_id_t Mod_id,
 	    }
 	}
 
-	if (total_ue_count > 0) {
-	    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
+
+	for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
 		UE_id = i;
 
 		for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
+
+			// if there are UEs with traffic
+			if (total_ue_count [CC_id] > 0) {
 		    CC_id = UE_list->ordered_CCids[ii][UE_id];
 		    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
 		    round = ue_sched_ctl->round[CC_id][harq_pid];
@@ -775,6 +860,8 @@ dlsch_scheduler_pre_processor(module_id_t Mod_id,
 			continue;
 		    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1)
 			continue;
+                    if (!ue_slice_membership(i, slice_id))
+                        continue;
 
 		    transmission_mode = get_tmode(Mod_id, CC_id, UE_id);
 		    //          mac_xface->get_ue_active_harq_pid(Mod_id,CC_id,rnti,frameP,subframeP,&harq_pid,&round,0);
@@ -963,9 +1050,9 @@ dlsch_scheduler_pre_processor(module_id_t Mod_id,
 			}
 		    }
 #endif
-		}
+		}	// total_ue_count
 	    }
-	}			// total_ue_count
+	}
     }				// end of for for r1 and r2
 
 #ifdef TM5
@@ -1038,7 +1125,7 @@ dlsch_scheduler_pre_processor(module_id_t Mod_id,
 		}
 
 		//PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].pre_nb_available_rbs = pre_nb_available_rbs[CC_id][UE_id];
-		LOG_D(MAC, "Total RBs allocated for UE%d = %d\n", UE_id,
+		LOG_D(MAC, "[eNB %d][SLICE %d]Total RBs allocated for UE%d = %d\n",  Mod_id, slice_id, UE_id,
 		      ue_sched_ctl->pre_nb_available_rbs[CC_id]);
 	    }
 	}
@@ -1059,9 +1146,12 @@ dlsch_scheduler_pre_processor_reset(int module_idP,
 				    uint16_t
 				    nb_rbs_required_remaining
 				    [MAX_NUM_CCs][NUMBER_OF_UE_MAX],
+					unsigned char total_ue_count[MAX_NUM_CCs],
+					unsigned char total_rbs_used[MAX_NUM_CCs],
 				    unsigned char
 				    rballoc_sub[MAX_NUM_CCs]
-				    [N_RBG_MAX], unsigned char
+				    [N_RBG_MAX],
+				unsigned char
 				    MIMO_mode_indicator[MAX_NUM_CCs]
 				    [N_RBG_MAX])
 {
@@ -1148,7 +1238,8 @@ dlsch_scheduler_pre_processor_reset(int module_idP,
     ue_sched_ctl->pre_nb_available_rbs[CC_id] = 0;
     ue_sched_ctl->dl_pow_off[CC_id] = 2;
     nb_rbs_required_remaining[CC_id][UE_id] = 0;
-
+	total_ue_count[CC_id]=0;
+	total_rbs_used[CC_id]=0;
     switch (N_RB_DL) {
     case 6:
 	RBGsize = 1;
@@ -1316,7 +1407,7 @@ dlsch_scheduler_pre_processor_allocate(module_id_t Mod_id,
 
 void
 ulsch_scheduler_pre_processor(module_id_t module_idP,
-			      int frameP,
+			      slice_id_t slice_id, int frameP,
 			      sub_frame_t subframeP, uint16_t * first_rb)
 {
 
@@ -1327,16 +1418,16 @@ ulsch_scheduler_pre_processor(module_id_t module_idP,
 	total_allocated_rbs[MAX_NUM_CCs],
 	average_rbs_per_user[MAX_NUM_CCs];
     int16_t total_remaining_rbs[MAX_NUM_CCs];
-    uint16_t max_num_ue_to_be_scheduled = 0;
-    uint16_t total_ue_count = 0;
+    uint16_t total_ue_count[MAX_NUM_CCs];
     rnti_t rnti = -1;
     UE_list_t *UE_list = &RC.mac[module_idP]->UE_list;
     UE_TEMPLATE *UE_template = 0;
-    int N_RB_DL;
-    int N_RB_UL;
+    UE_sched_ctrl *ue_sched_ctl;
+    int N_RB_UL = 0;
+
     LOG_D(MAC, "In ulsch_preprocessor: assign max mcs min rb\n");
     // maximize MCS and then allocate required RB according to the buffer occupancy with the limit of max available UL RB
-    assign_max_mcs_min_rb(module_idP, frameP, subframeP, first_rb);
+    assign_max_mcs_min_rb(module_idP, slice_id, frameP, subframeP, first_rb);
 
     LOG_D(MAC, "In ulsch_preprocessor: sort ue \n");
     // sort ues
@@ -1346,324 +1437,315 @@ ulsch_scheduler_pre_processor(module_id_t module_idP,
     // we need to distribute RBs among UEs
     // step1:  reset the vars
     for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
-	N_RB_DL =
-	    to_prb(RC.mac[module_idP]->common_channels[CC_id].mib->
-		   message.dl_Bandwidth);
-	N_RB_UL =
-	    to_prb(RC.mac[module_idP]->common_channels[CC_id].
-		   ul_Bandwidth);
-	total_allocated_rbs[CC_id] = 0;
-	total_remaining_rbs[CC_id] = 0;
-	average_rbs_per_user[CC_id] = 0;
-
-	for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
-	    nb_allocated_rbs[CC_id][i] = 0;
-	}
+      total_allocated_rbs[CC_id] = 0;
+      total_remaining_rbs[CC_id] = 0;
+      average_rbs_per_user[CC_id] = 0;
+      total_ue_count[CC_id] = 0;
+    }
+
+    // Step 1.5: Calculate total_ue_count
+    for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
+      for (n = 0; n < UE_list->numactiveULCCs[i]; n++) {
+        // This is the actual CC_id in the list
+        CC_id = UE_list->ordered_ULCCids[n][i];
+        UE_template = &UE_list->UE_template[CC_id][i];
+        if (!ue_slice_membership(i, slice_id))
+          continue;
+        if (UE_template->pre_allocated_nb_rb_ul[slice_id] > 0) {
+          total_ue_count[CC_id] += 1;
+        }
+      }
     }
 
     LOG_D(MAC, "In ulsch_preprocessor: step2 \n");
     // step 2: calculate the average rb per UE
-    total_ue_count = 0;
-    max_num_ue_to_be_scheduled = 0;
-
     for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
 
-	rnti = UE_RNTI(module_idP, i);
-
-	if (rnti == NOT_A_RNTI)
-	    continue;
-
-	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-	    continue;
-
-
-	UE_id = i;
-
-	LOG_D(MAC, "In ulsch_preprocessor: handling UE %d/%x\n", UE_id,
-	      rnti);
-	for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
-	    // This is the actual CC_id in the list
-	    CC_id = UE_list->ordered_ULCCids[n][UE_id];
-	    LOG_D(MAC,
-		  "In ulsch_preprocessor: handling UE %d/%x CCid %d\n",
-		  UE_id, rnti, CC_id);
-	    UE_template = &UE_list->UE_template[CC_id][UE_id];
-	    average_rbs_per_user[CC_id] = 0;
-
-	    if (UE_template->pre_allocated_nb_rb_ul > 0) {
-		total_ue_count += 1;
-	    }
-	    /*
-	       if((mac_xface->get_nCCE_max(module_idP,CC_id,3,subframeP) - nCCE_to_be_used[CC_id])  > (1<<aggregation)) {
-	       nCCE_to_be_used[CC_id] = nCCE_to_be_used[CC_id] + (1<<aggregation);
-	       max_num_ue_to_be_scheduled+=1;
-	       } */
-
-	    max_num_ue_to_be_scheduled += 1;
-
-	    if (total_ue_count == 0) {
-		average_rbs_per_user[CC_id] = 0;
-	    } else if (total_ue_count == 1) {	// increase the available RBs, special case,
-		average_rbs_per_user[CC_id] =
-		    N_RB_UL - first_rb[CC_id] + 1;
-	    } else if ((total_ue_count <= (N_RB_DL - first_rb[CC_id]))
-		       && (total_ue_count <= max_num_ue_to_be_scheduled)) {
-		average_rbs_per_user[CC_id] =
-		    (uint16_t) floor((N_RB_UL - first_rb[CC_id]) /
-				     total_ue_count);
-	    } else if (max_num_ue_to_be_scheduled > 0) {
-		average_rbs_per_user[CC_id] =
-		    (uint16_t) floor((N_RB_UL - first_rb[CC_id]) /
-				     max_num_ue_to_be_scheduled);
-	    } else {
-		average_rbs_per_user[CC_id] = 1;
-		LOG_W(MAC,
-		      "[eNB %d] frame %d subframe %d: UE %d CC %d: can't get average rb per user (should not be here)\n",
-		      module_idP, frameP, subframeP, UE_id, CC_id);
-	    }
-	}
+      rnti = UE_RNTI(module_idP, i);
+      UE_id = i;
+
+      if (rnti == NOT_A_RNTI)
+        continue;
+
+      if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+        continue;
+
+      if (!ue_slice_membership(UE_id, slice_id))
+          continue;
+
+      LOG_D(MAC, "In ulsch_preprocessor: handling UE %d/%x\n", UE_id,
+            rnti);
+      for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
+        // This is the actual CC_id in the list
+        CC_id = UE_list->ordered_ULCCids[n][UE_id];
+        LOG_D(MAC,
+              "In ulsch_preprocessor: handling UE %d/%x CCid %d\n",
+              UE_id, rnti, CC_id);
+
+        average_rbs_per_user[CC_id] = 0;
+
+        /*
+           if((mac_xface->get_nCCE_max(module_idP,CC_id,3,subframeP) - nCCE_to_be_used[CC_id])  > (1<<aggregation)) {
+           nCCE_to_be_used[CC_id] = nCCE_to_be_used[CC_id] + (1<<aggregation);
+           max_num_ue_to_be_scheduled+=1;
+           } */
+
+
+
+        N_RB_UL = to_prb(RC.mac[module_idP]->common_channels[CC_id].ul_Bandwidth);
+        ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+        ue_sched_ctl->max_rbs_allowed_slice_uplink[CC_id][slice_id] = flexran_nb_rbs_allowed_slice(slice_percentage_uplink[slice_id],N_RB_UL);
+
+        if (total_ue_count[CC_id] == 0) {
+          average_rbs_per_user[CC_id] = 0;
+        } else if (total_ue_count[CC_id] == 1) {    // increase the available RBs, special case,
+          average_rbs_per_user[CC_id] = ue_sched_ctl->max_rbs_allowed_slice_uplink[CC_id][slice_id] - first_rb[CC_id] + 1;
+        } else if (total_ue_count[CC_id] <= (ue_sched_ctl->max_rbs_allowed_slice_uplink[CC_id][slice_id] - first_rb[CC_id])) {
+          average_rbs_per_user[CC_id] = (uint16_t) floor((ue_sched_ctl->max_rbs_allowed_slice_uplink[CC_id][slice_id] - first_rb[CC_id]) / total_ue_count[CC_id]);
+        } else {
+          average_rbs_per_user[CC_id] = 1;
+          LOG_W(MAC,
+                "[eNB %d] frame %d subframe %d: UE %d CC %d: can't get average rb per user (should not be here)\n",
+                module_idP, frameP, subframeP, UE_id, CC_id);
+        }
+        if (total_ue_count[CC_id] > 0)
+          LOG_D(MAC, "[eNB %d] Frame %d subframe %d: total ue to be scheduled %d\n",
+                module_idP, frameP, subframeP, total_ue_count[CC_id]);
+      }
     }
-    if (total_ue_count > 0)
-	LOG_D(MAC,
-	      "[eNB %d] Frame %d subframe %d: total ue to be scheduled %d/%d\n",
-	      module_idP, frameP, subframeP, total_ue_count,
-	      max_num_ue_to_be_scheduled);
-
-    //LOG_D(MAC,"step3\n");
 
     // step 3: assigne RBS
     for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
-	rnti = UE_RNTI(module_idP, i);
-
-	if (rnti == NOT_A_RNTI)
-	    continue;
-	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-	    continue;
-
-	UE_id = i;
-
-	for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
-	    // This is the actual CC_id in the list
-	    CC_id = UE_list->ordered_ULCCids[n][UE_id];
-	    harq_pid =
-		subframe2harqpid(&RC.mac[module_idP]->
-				 common_channels[CC_id], frameP,
-				 subframeP);
-
-
-	    //      mac_xface->get_ue_active_harq_pid(module_idP,CC_id,rnti,frameP,subframeP,&harq_pid,&round,openair_harq_UL);
-
-	    if (UE_list->UE_sched_ctrl[UE_id].round_UL[CC_id] > 0) {
-		nb_allocated_rbs[CC_id][UE_id] =
-		    UE_list->UE_template[CC_id][UE_id].nb_rb_ul[harq_pid];
-	    } else {
-		nb_allocated_rbs[CC_id][UE_id] =
-		    cmin(UE_list->
-			 UE_template[CC_id][UE_id].pre_allocated_nb_rb_ul,
-			 average_rbs_per_user[CC_id]);
-	    }
-
-	    total_allocated_rbs[CC_id] += nb_allocated_rbs[CC_id][UE_id];
-	    LOG_D(MAC,
-		  "In ulsch_preprocessor: assigning %d RBs for UE %d/%x CCid %d, harq_pid %d\n",
-		  nb_allocated_rbs[CC_id][UE_id], UE_id, rnti, CC_id,
-		  harq_pid);
-	}
+      rnti = UE_RNTI(module_idP, i);
+
+      if (rnti == NOT_A_RNTI)
+        continue;
+      if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+        continue;
+      if (!ue_slice_membership(i, slice_id))
+        continue;
+
+
+      UE_id = i;
+
+      for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
+        // This is the actual CC_id in the list
+        CC_id = UE_list->ordered_ULCCids[n][UE_id];
+        UE_template = &UE_list->UE_template[CC_id][UE_id];
+        harq_pid = subframe2harqpid(&RC.mac[module_idP]->common_channels[CC_id],
+                                    frameP, subframeP);
+
+        //      mac_xface->get_ue_active_harq_pid(module_idP,CC_id,rnti,frameP,subframeP,&harq_pid,&round,openair_harq_UL);
+
+        if (UE_list->UE_sched_ctrl[UE_id].round_UL[CC_id] > 0) {
+          nb_allocated_rbs[CC_id][UE_id] = UE_list->UE_template[CC_id][UE_id].nb_rb_ul[harq_pid];
+        } else {
+          nb_allocated_rbs[CC_id][UE_id] =
+                  cmin(UE_list->UE_template[CC_id][UE_id].pre_allocated_nb_rb_ul[slice_id],
+                       average_rbs_per_user[CC_id]);
+        }
+
+        total_allocated_rbs[CC_id] += nb_allocated_rbs[CC_id][UE_id];
+        LOG_D(MAC,
+              "In ulsch_preprocessor: assigning %d RBs for UE %d/%x CCid %d, harq_pid %d\n",
+              nb_allocated_rbs[CC_id][UE_id], UE_id, rnti, CC_id,
+              harq_pid);
+      }
     }
 
     // step 4: assigne the remaining RBs and set the pre_allocated rbs accordingly
-    for (r = 0; r < 2; r++) {
-
-	for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
-	    rnti = UE_RNTI(module_idP, i);
-
-	    if (rnti == NOT_A_RNTI)
-		continue;
-	    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-		continue;
-	    UE_id = i;
-
-	    for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
-		// This is the actual CC_id in the list
-		CC_id = UE_list->ordered_ULCCids[n][UE_id];
-		UE_template = &UE_list->UE_template[CC_id][UE_id];
-		total_remaining_rbs[CC_id] =
-		    N_RB_UL - first_rb[CC_id] - total_allocated_rbs[CC_id];
-
-		if (total_ue_count == 1) {
-		    total_remaining_rbs[CC_id] += 1;
-		}
+  for (r = 0; r < 2; r++) {
 
-		if (r == 0) {
-		    while ((UE_template->pre_allocated_nb_rb_ul > 0) &&
-			   (nb_allocated_rbs[CC_id][UE_id] <
-			    UE_template->pre_allocated_nb_rb_ul)
-			   && (total_remaining_rbs[CC_id] > 0)) {
-			nb_allocated_rbs[CC_id][UE_id] =
-			    cmin(nb_allocated_rbs[CC_id][UE_id] + 1,
-				 UE_template->pre_allocated_nb_rb_ul);
-			total_remaining_rbs[CC_id]--;
-			total_allocated_rbs[CC_id]++;
-		    }
-		} else {
-		    UE_template->pre_allocated_nb_rb_ul =
-			nb_allocated_rbs[CC_id][UE_id];
-		    LOG_D(MAC,
-			  "******************UL Scheduling Information for UE%d CC_id %d ************************\n",
-			  UE_id, CC_id);
-		    LOG_D(MAC,
-			  "[eNB %d] total RB allocated for UE%d CC_id %d  = %d\n",
-			  module_idP, UE_id, CC_id,
-			  UE_template->pre_allocated_nb_rb_ul);
-		}
-	    }
-	}
+    for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
+      rnti = UE_RNTI(module_idP, i);
+
+      if (rnti == NOT_A_RNTI)
+        continue;
+      if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+        continue;
+      if (!ue_slice_membership(i, slice_id))
+        continue;
+
+      UE_id = i;
+      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+
+      for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
+        // This is the actual CC_id in the list
+        CC_id = UE_list->ordered_ULCCids[n][UE_id];
+        UE_template = &UE_list->UE_template[CC_id][UE_id];
+        total_remaining_rbs[CC_id] =
+                ue_sched_ctl->max_rbs_allowed_slice_uplink[CC_id][slice_id] - first_rb[CC_id] - total_allocated_rbs[CC_id];
+
+        if (total_ue_count[CC_id] == 1) {
+          total_remaining_rbs[CC_id] += 1;
+        }
+
+        if (r == 0) {
+          while ((UE_template->pre_allocated_nb_rb_ul[slice_id] > 0)
+                 && (nb_allocated_rbs[CC_id][UE_id] < UE_template->pre_allocated_nb_rb_ul[slice_id])
+                 && (total_remaining_rbs[CC_id] > 0)) {
+            nb_allocated_rbs[CC_id][UE_id] =
+                    cmin(nb_allocated_rbs[CC_id][UE_id] + 1,
+                         UE_template->pre_allocated_nb_rb_ul[slice_id]);
+            total_remaining_rbs[CC_id]--;
+            total_allocated_rbs[CC_id]++;
+          }
+        } else {
+          UE_template->pre_allocated_nb_rb_ul[slice_id] =
+                  nb_allocated_rbs[CC_id][UE_id];
+          LOG_D(MAC,
+                "******************UL Scheduling Information for UE%d CC_id %d ************************\n",
+                UE_id, CC_id);
+          LOG_D(MAC,
+                "[eNB %d] total RB allocated for UE%d CC_id %d  = %d\n",
+                module_idP, UE_id, CC_id,
+                UE_template->pre_allocated_nb_rb_ul[slice_id]);
+        }
+      }
     }
+  }
 
     for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
 
 	if (total_allocated_rbs[CC_id] > 0) {
 	    LOG_D(MAC, "[eNB %d] total RB allocated for all UEs = %d/%d\n",
 		  module_idP, total_allocated_rbs[CC_id],
-		  N_RB_UL - first_rb[CC_id]);
+              ue_sched_ctl->max_rbs_allowed_slice_uplink[CC_id][slice_id] - first_rb[CC_id]);
 	}
     }
 }
 
 
 void
-assign_max_mcs_min_rb(module_id_t module_idP, int frameP,
+assign_max_mcs_min_rb(module_id_t module_idP, int slice_id, int frameP,
 		      sub_frame_t subframeP, uint16_t * first_rb)
 {
 
-    int i;
-    uint16_t n, UE_id;
-    uint8_t CC_id;
-    rnti_t rnti = -1;
-    int mcs;
-    int rb_table_index = 0, tbs, tx_power;
-    eNB_MAC_INST *eNB = RC.mac[module_idP];
-    UE_list_t *UE_list = &eNB->UE_list;
-
-    UE_TEMPLATE *UE_template;
-    int Ncp;
-    int N_RB_UL;
-
-    for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
-	if (UE_list->active[i] != TRUE)
-	    continue;
-
-	rnti = UE_RNTI(module_idP, i);
-
-	if (rnti == NOT_A_RNTI)
-	    continue;
-	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-	    continue;
-
-	if (UE_list->UE_sched_ctrl[i].phr_received == 1)
-	    mcs = 20;		// if we've received the power headroom information the UE, we can go to maximum mcs
-	else
-	    mcs = 10;		// otherwise, limit to QPSK PUSCH
-
-	UE_id = i;
-
-	for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
-	    // This is the actual CC_id in the list
-	    CC_id = UE_list->ordered_ULCCids[n][UE_id];
-
-	    if (CC_id >= MAX_NUM_CCs) {
-		LOG_E(MAC,
-		      "CC_id %u should be < %u, loop n=%u < numactiveULCCs[%u]=%u",
-		      CC_id, MAX_NUM_CCs, n, UE_id,
-		      UE_list->numactiveULCCs[UE_id]);
-	    }
-
-	    AssertFatal(CC_id < MAX_NUM_CCs,
-			"CC_id %u should be < %u, loop n=%u < numactiveULCCs[%u]=%u",
-			CC_id, MAX_NUM_CCs, n, UE_id,
-			UE_list->numactiveULCCs[UE_id]);
-
-	    UE_template = &UE_list->UE_template[CC_id][UE_id];
-
-	    Ncp = RC.mac[module_idP]->common_channels[CC_id].Ncp;
-	    N_RB_UL =
-		to_prb(RC.mac[module_idP]->common_channels[CC_id].
-		       ul_Bandwidth);
-	    // if this UE has UL traffic
-            int bytes_to_schedule = UE_template->estimated_ul_buffer - UE_template->scheduled_ul_bytes;
-            if (bytes_to_schedule < 0) bytes_to_schedule = 0;
-            int bits_to_schedule = bytes_to_schedule * 8;
-
-	    if (bits_to_schedule > 0) {
-		tbs = get_TBS_UL(mcs, 3) << 3;	// 1 or 2 PRB with cqi enabled does not work well!
-		rb_table_index = 2;
-
-		// fixme: set use_srs flag
-		tx_power =
-		    estimate_ue_tx_power(tbs, rb_table[rb_table_index], 0,
-					 Ncp, 0);
-
-		while ((((UE_template->phr_info - tx_power) < 0)
-			|| (tbs > bits_to_schedule))
-		       && (mcs > 3)) {
-		    // LOG_I(MAC,"UE_template->phr_info %d tx_power %d mcs %d\n", UE_template->phr_info,tx_power, mcs);
-		    mcs--;
-		    tbs = get_TBS_UL(mcs, rb_table[rb_table_index]) << 3;
-		    tx_power = estimate_ue_tx_power(tbs, rb_table[rb_table_index], 0, Ncp, 0);	// fixme: set use_srs
-		}
-
-		while ((tbs < bits_to_schedule) &&
-		       (rb_table[rb_table_index] <
-			(N_RB_UL - first_rb[CC_id]))
-		       && ((UE_template->phr_info - tx_power) > 0)
-		       && (rb_table_index < 32)) {
-
-		    rb_table_index++;
-		    tbs = get_TBS_UL(mcs, rb_table[rb_table_index]) << 3;
-		    tx_power =
-			estimate_ue_tx_power(tbs, rb_table[rb_table_index],
-					     0, Ncp, 0);
-		}
-
-		UE_template->ue_tx_power = tx_power;
-
-		if (rb_table[rb_table_index] >
-		    (N_RB_UL - first_rb[CC_id] - 1)) {
-		    rb_table_index--;
-		}
-		// 1 or 2 PRB with cqi enabled does not work well
-		if (rb_table[rb_table_index] < 3) {
-		    rb_table_index = 2;	//3PRB
-		}
+  int i;
+  uint16_t n, UE_id;
+  uint8_t CC_id;
+  rnti_t rnti = -1;
+  int mcs;
+  int rb_table_index = 0, tbs, tx_power;
+  eNB_MAC_INST *eNB = RC.mac[module_idP];
+  UE_list_t *UE_list = &eNB->UE_list;
+
+  UE_TEMPLATE *UE_template;
+  UE_sched_ctrl *ue_sched_ctl;
+  int Ncp;
+  int N_RB_UL;
+
+  for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
+    if (UE_list->active[i] != TRUE)
+      continue;
+
+    rnti = UE_RNTI(module_idP, i);
+
+    if (rnti == NOT_A_RNTI)
+      continue;
+    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+      continue;
+    if (!ue_slice_membership(i, slice_id))
+      continue;
+
+    if (UE_list->UE_sched_ctrl[i].phr_received == 1) {
+      /* if we've received the power headroom information the UE, we can go to
+       * maximum mcs */
+      mcs = cmin(20, slice_maxmcs_uplink[slice_id]);
+    } else {
+      /* otherwise, limit to QPSK PUSCH */
+      mcs = cmin(10, slice_maxmcs_uplink[slice_id]);
+    }
 
-		UE_template->pre_assigned_mcs_ul = mcs;
-		UE_template->pre_allocated_rb_table_index_ul =
-		    rb_table_index;
-		UE_template->pre_allocated_nb_rb_ul =
-		    rb_table[rb_table_index];
-		LOG_D(MAC,
-		      "[eNB %d] frame %d subframe %d: for UE %d CC %d: pre-assigned mcs %d, pre-allocated rb_table[%d]=%d RBs (phr %d, tx power %d)\n",
-		      module_idP, frameP, subframeP, UE_id, CC_id,
-		      UE_template->pre_assigned_mcs_ul,
-		      UE_template->pre_allocated_rb_table_index_ul,
-		      UE_template->pre_allocated_nb_rb_ul,
-		      UE_template->phr_info, tx_power);
-	    } else {
-		/* if UE has pending scheduling request then pre-allocate 3 RBs */
-		//if (UE_template->ul_active == 1 && UE_template->ul_SR == 1) {
-		if (UE_is_to_be_scheduled(module_idP, CC_id, i)) {
-		    /* use QPSK mcs */
-		    UE_template->pre_assigned_mcs_ul = 10;
-		    UE_template->pre_allocated_rb_table_index_ul = 2;
-		    UE_template->pre_allocated_nb_rb_ul = 3;
-		} else {
-		    UE_template->pre_assigned_mcs_ul = 0;
-		    UE_template->pre_allocated_rb_table_index_ul = -1;
-		    UE_template->pre_allocated_nb_rb_ul = 0;
-		}
-	    }
-	}
+    UE_id = i;
+
+    for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
+      // This is the actual CC_id in the list
+      CC_id = UE_list->ordered_ULCCids[n][UE_id];
+
+      if (CC_id >= MAX_NUM_CCs) {
+        LOG_E(MAC, "CC_id %u should be < %u, loop n=%u < numactiveULCCs[%u]=%u",
+              CC_id, MAX_NUM_CCs, n, UE_id, UE_list->numactiveULCCs[UE_id]);
+      }
+
+      AssertFatal(CC_id < MAX_NUM_CCs,
+                  "CC_id %u should be < %u, loop n=%u < numactiveULCCs[%u]=%u",
+                  CC_id, MAX_NUM_CCs, n, UE_id,
+                  UE_list->numactiveULCCs[UE_id]);
+
+      UE_template = &UE_list->UE_template[CC_id][UE_id];
+      UE_template->pre_assigned_mcs_ul = mcs;
+      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+
+      Ncp = RC.mac[module_idP]->common_channels[CC_id].Ncp;
+      N_RB_UL = to_prb(RC.mac[module_idP]->common_channels[CC_id].ul_Bandwidth);
+      ue_sched_ctl->max_rbs_allowed_slice_uplink[CC_id][slice_id] = flexran_nb_rbs_allowed_slice(slice_percentage_uplink[slice_id],N_RB_UL);
+
+      int bytes_to_schedule = UE_template->estimated_ul_buffer - UE_template->scheduled_ul_bytes;
+      if (bytes_to_schedule < 0) bytes_to_schedule = 0;
+      int bits_to_schedule = bytes_to_schedule * 8;
+
+      // if this UE has UL traffic
+      if (bits_to_schedule > 0) {
+        tbs = get_TBS_UL(UE_template->pre_assigned_mcs_ul, 3) << 3; // 1 or 2 PRB with cqi enabled does not work well!
+        rb_table_index = 2;
+
+        // fixme: set use_srs flag
+        tx_power = estimate_ue_tx_power(tbs, rb_table[rb_table_index], 0, Ncp, 0);
+
+        while ((((UE_template->phr_info - tx_power) < 0)
+              || (tbs > bits_to_schedule))
+              && (UE_template->pre_assigned_mcs_ul > 3)) {
+          // LOG_I(MAC,"UE_template->phr_info %d tx_power %d mcs %d\n", UE_template->phr_info,tx_power, mcs);
+          UE_template->pre_assigned_mcs_ul--;
+          tbs = get_TBS_UL(UE_template->pre_assigned_mcs_ul, rb_table[rb_table_index]) << 3;
+          tx_power = estimate_ue_tx_power(tbs, rb_table[rb_table_index], 0, Ncp, 0);   // fixme: set use_srs
+        }
+
+        while ((tbs < bits_to_schedule)
+                && (rb_table[rb_table_index] < (ue_sched_ctl->max_rbs_allowed_slice_uplink[CC_id][slice_id] - first_rb[CC_id]))
+                && ((UE_template->phr_info - tx_power) > 0)
+                && (rb_table_index < 32)) {
+          rb_table_index++;
+          tbs = get_TBS_UL(UE_template->pre_assigned_mcs_ul, rb_table[rb_table_index]) << 3;
+          tx_power = estimate_ue_tx_power(tbs, rb_table[rb_table_index], 0, Ncp, 0);
+        }
+
+        UE_template->ue_tx_power = tx_power;
+
+        if (rb_table[rb_table_index] > (ue_sched_ctl->max_rbs_allowed_slice_uplink[CC_id][slice_id] - first_rb[CC_id] - 1)) {
+          rb_table_index--;
+        }
+        // 1 or 2 PRB with cqi enabled does not work well
+        if (rb_table[rb_table_index] < 3) {
+            rb_table_index = 2;	//3PRB
+        }
+
+        UE_template->pre_allocated_rb_table_index_ul = rb_table_index;
+        UE_template->pre_allocated_nb_rb_ul[slice_id] = rb_table[rb_table_index];
+        LOG_D(MAC,
+              "[eNB %d] frame %d subframe %d: for UE %d CC %d: pre-assigned mcs %d, pre-allocated rb_table[%d]=%d RBs (phr %d, tx power %d)\n",
+              module_idP, frameP, subframeP, UE_id, CC_id,
+              UE_template->pre_assigned_mcs_ul,
+              UE_template->pre_allocated_rb_table_index_ul,
+              UE_template->pre_allocated_nb_rb_ul[slice_id],
+              UE_template->phr_info, tx_power);
+      } else {
+        /* if UE has pending scheduling request then pre-allocate 3 RBs */
+        //if (UE_template->ul_active == 1 && UE_template->ul_SR == 1) {
+        if (UE_is_to_be_scheduled(module_idP, CC_id, i)) {
+          /* use QPSK mcs */
+          UE_template->pre_assigned_mcs_ul = 10;
+          UE_template->pre_allocated_rb_table_index_ul = 2;
+          UE_template->pre_allocated_nb_rb_ul[slice_id] = 3;
+        } else {
+          UE_template->pre_assigned_mcs_ul = 0;
+          UE_template->pre_allocated_rb_table_index_ul = -1;
+          UE_template->pre_allocated_nb_rb_ul[slice_id] = 0;
+        }
+      }
     }
+  }
 }
 
 struct sort_ue_ul_params {
diff --git a/openair2/LAYER2/MAC/proto.h b/openair2/LAYER2/MAC/proto.h
index 46a8b005e4476d8fb81a3bcbe46e0a0e204b325d..348b080d1e96eb20f0b0145fb68d268525953f07 100644
--- a/openair2/LAYER2/MAC/proto.h
+++ b/openair2/LAYER2/MAC/proto.h
@@ -35,6 +35,22 @@
  *  @{
  */
 
+/**
+ * slice specific scheduler
+ */
+typedef void (*slice_scheduler_dl)(module_id_t mod_id,
+				   slice_id_t  slice_id,
+				   frame_t     frame,
+				   sub_frame_t subframe,
+				   int        *mbsfn_flag);
+
+typedef void (*slice_scheduler_ul)(module_id_t mod_id,
+                                   slice_id_t  slice_id,
+				   frame_t       frame,
+				   sub_frame_t   subframe,
+				   unsigned char sched_subframe,
+                                   uint16_t     *first_rb);
+
 /** \fn void schedule_mib(module_id_t module_idP,frame_t frameP,sub_frame_t subframe);
 \brief MIB scheduling for PBCH. This function requests the MIB from RRC and provides it to L1.
 @param Mod_id Instance ID of eNB
@@ -102,11 +118,12 @@ void schedule_ulsch(module_id_t module_idP, frame_t frameP,
 
 /** \brief ULSCH Scheduling per RNTI
 @param Mod_id Instance ID of eNB
+@param slice_id Instance slice for this eNB
 @param frame Frame index
 @param subframe Subframe number on which to act
 @param sched_subframe Subframe number where PUSCH is transmitted (for DAI lookup)
 */
-void schedule_ulsch_rnti(module_id_t module_idP, frame_t frameP,
+void schedule_ulsch_rnti(module_id_t module_idP, slice_id_t slice_idP, frame_t frameP,
 			 sub_frame_t subframe,
 			 unsigned char sched_subframe,
 			 uint16_t * first_rb);
@@ -127,9 +144,12 @@ void fill_DLSCH_dci(module_id_t module_idP, frame_t frameP,
 
 @param mbsfn_flag  Indicates that MCH/MCCH is in this subframe
 */
-void schedule_ue_spec(module_id_t module_idP, frame_t frameP,
+void schedule_dlsch(module_id_t module_idP, frame_t frameP,
 		      sub_frame_t subframe, int *mbsfn_flag);
 
+void schedule_ue_spec(module_id_t module_idP, slice_id_t slice_idP,
+		      frame_t frameP,sub_frame_t subframe, int *mbsfn_flag);
+
 
 /** \brief Function for UE/PHY to compute PUSCH transmit power in power-control procedure.
     @param Mod_id Module id of UE
@@ -192,6 +212,8 @@ void dlsch_scheduler_pre_processor_reset(int module_idP, int UE_id,
 					 uint16_t
 					 nb_rbs_required_remaining
 					 [MAX_NUM_CCs][NUMBER_OF_UE_MAX],
+					 unsigned char total_ue_count[MAX_NUM_CCs],
+					 unsigned char total_rbs_used[MAX_NUM_CCs],
 					 unsigned char
 					 rballoc_sub[MAX_NUM_CCs]
 					 [N_RBG_MAX],
@@ -209,6 +231,7 @@ void dlsch_scheduler_pre_processor_reset(int module_idP, int UE_id,
 
 
 void dlsch_scheduler_pre_processor(module_id_t module_idP,
+				   slice_id_t slice_idP,
 				   frame_t frameP,
 				   sub_frame_t subframe,
 				   int N_RBG[MAX_NUM_CCs],
@@ -641,17 +664,18 @@ int UE_PCCID(module_id_t mod_idP, int ue_idP);
 rnti_t UE_RNTI(module_id_t mod_idP, int ue_idP);
 
 
-void ulsch_scheduler_pre_processor(module_id_t module_idP, int frameP,
+void ulsch_scheduler_pre_processor(module_id_t module_idP, slice_id_t slice_id, int frameP,
 				   sub_frame_t subframeP,
 				   uint16_t * first_rb);
 void store_ulsch_buffer(module_id_t module_idP, int frameP,
 			sub_frame_t subframeP);
 void sort_ue_ul(module_id_t module_idP, int frameP, sub_frame_t subframeP);
-void assign_max_mcs_min_rb(module_id_t module_idP, int frameP,
+void assign_max_mcs_min_rb(module_id_t module_idP, int slice_id, int frameP,
 			   sub_frame_t subframeP, uint16_t * first_rb);
 void adjust_bsr_info(int buffer_occupancy, uint16_t TBS,
 		     UE_TEMPLATE * UE_template);
 int phy_stats_exist(module_id_t Mod_id, int rnti);
+void sort_UEs(module_id_t Mod_idP, slice_id_t slice_id, int frameP, sub_frame_t subframeP);
 
 /*! \fn  UE_L2_state_t ue_scheduler(const module_id_t module_idP,const frame_t frameP, const sub_frame_t subframe, const lte_subframe_t direction,const uint8_t eNB_index)
    \brief UE scheduler where all the ue background tasks are done.  This function performs the following:  1) Trigger PDCP every 5ms 2) Call RRC for link status return to PHY3) Perform SR/BSR procedures for scheduling feedback 4) Perform PHR procedures.
@@ -1165,5 +1189,10 @@ int32_t get_uldl_offset(int eutra_bandP);
 int l2_init_ue(int eMBMS_active, char *uecap_xer, uint8_t cba_group_active,
 	       uint8_t HO_active);
 
+/*Slice related functions */
+uint16_t flexran_nb_rbs_allowed_slice(float rb_percentage, int total_rbs);
+
+int ue_slice_membership(int UE_id, int slice_id);
+
 #endif
 /** @}*/
diff --git a/openair2/LAYER2/PDCP_v10.1.0/pdcp.c b/openair2/LAYER2/PDCP_v10.1.0/pdcp.c
index 91621ab50fdf2d471e9df5286ac5f9fc5f47b438..00557ce7e075a04efb439120c2da73c879c8d2b0 100644
--- a/openair2/LAYER2/PDCP_v10.1.0/pdcp.c
+++ b/openair2/LAYER2/PDCP_v10.1.0/pdcp.c
@@ -99,6 +99,8 @@ boolean_t pdcp_data_req(
 
   hash_key_t         key             = HASHTABLE_NOT_A_KEY_VALUE;
   hashtable_rc_t     h_rc;
+  uint8_t            rb_offset= (srb_flagP == 0) ? DTCH -1 : 0;
+  uint16_t           pdcp_uid=0;
   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PDCP_DATA_REQ,VCD_FUNCTION_IN);
   CHECK_CTXT_ARGS(ctxt_pP);
 
@@ -144,7 +146,7 @@ boolean_t pdcp_data_req(
     ctxt_pP->configured=TRUE;
   }
     
-  if (ctxt_pP->enb_flag == ENB_FLAG_NO) {
+  if (ctxt_pP->enb_flag == ENB_FLAG_YES) {
     start_meas(&eNB_pdcp_stats[ctxt_pP->module_id].data_req);
   } else {
     start_meas(&UE_pdcp_stats[ctxt_pP->module_id].data_req);
@@ -215,7 +217,7 @@ boolean_t pdcp_data_req(
           LOG_E(PDCP, PROTOCOL_PDCP_CTXT_FMT" Cannot fill PDU buffer with relevant header fields!\n",
                 PROTOCOL_PDCP_CTXT_ARGS(ctxt_pP,pdcp_p));
 
-          if (ctxt_pP->enb_flag == ENB_FLAG_NO) {
+          if (ctxt_pP->enb_flag == ENB_FLAG_YES) {
             stop_meas(&eNB_pdcp_stats[ctxt_pP->module_id].data_req);
           } else {
             stop_meas(&UE_pdcp_stats[ctxt_pP->module_id].data_req);
@@ -233,8 +235,9 @@ boolean_t pdcp_data_req(
         if (pdcp_serialize_user_plane_data_pdu_with_long_sn_buffer((unsigned char*)pdcp_pdu_p->data, &pdu_header) == FALSE) {
           LOG_E(PDCP, PROTOCOL_PDCP_CTXT_FMT" Cannot fill PDU buffer with relevant header fields!\n",
                 PROTOCOL_PDCP_CTXT_ARGS(ctxt_pP,pdcp_p));
+         
+          if (ctxt_pP->enb_flag == ENB_FLAG_YES) {
 
-          if (ctxt_pP->enb_flag == ENB_FLAG_NO) {
             stop_meas(&eNB_pdcp_stats[ctxt_pP->module_id].data_req);
           } else {
             stop_meas(&UE_pdcp_stats[ctxt_pP->module_id].data_req);
@@ -256,7 +259,7 @@ boolean_t pdcp_data_req(
 
         free_mem_block(pdcp_pdu_p, __func__);
 
-        if (ctxt_pP->enb_flag == ENB_FLAG_NO) {
+        if (ctxt_pP->enb_flag == ENB_FLAG_YES) {
           stop_meas(&eNB_pdcp_stats[ctxt_pP->module_id].data_req);
         } else {
           stop_meas(&UE_pdcp_stats[ctxt_pP->module_id].data_req);
@@ -284,7 +287,7 @@ boolean_t pdcp_data_req(
           (((pdcp_p->cipheringAlgorithm) != 0) ||
            ((pdcp_p->integrityProtAlgorithm) != 0))) {
 
-        if (ctxt_pP->enb_flag == ENB_FLAG_NO) {
+        if (ctxt_pP->enb_flag == ENB_FLAG_YES) {
           start_meas(&eNB_pdcp_stats[ctxt_pP->module_id].apply_security);
         } else {
           start_meas(&UE_pdcp_stats[ctxt_pP->module_id].apply_security);
@@ -379,7 +382,7 @@ boolean_t pdcp_data_req(
     break;
   }
 
-  if (ctxt_pP->enb_flag == ENB_FLAG_NO) {
+  if (ctxt_pP->enb_flag == ENB_FLAG_YES) {
     stop_meas(&eNB_pdcp_stats[ctxt_pP->module_id].data_req);
   } else {
     stop_meas(&UE_pdcp_stats[ctxt_pP->module_id].data_req);
@@ -389,16 +392,23 @@ boolean_t pdcp_data_req(
    * Control arrives here only if rlc_data_req() returns RLC_OP_STATUS_OK
    * so we return TRUE afterwards
    */
-  /*
-   if (rb_id>=DTCH) {
-    if (ctxt_pP->enb_flag == 1) {
-      Pdcp_stats_tx[module_id][(rb_id & RAB_OFFSET2 )>> RAB_SHIFT2][(rb_id & RAB_OFFSET)-DTCH]++;
-      Pdcp_stats_tx_bytes[module_id][(rb_id & RAB_OFFSET2 )>> RAB_SHIFT2][(rb_id & RAB_OFFSET)-DTCH] += sdu_buffer_size;
-    } else {
-      Pdcp_stats_tx[module_id][(rb_id & RAB_OFFSET2 )>> RAB_SHIFT2][(rb_id & RAB_OFFSET)-DTCH]++;
-      Pdcp_stats_tx_bytes[module_id][(rb_id & RAB_OFFSET2 )>> RAB_SHIFT2][(rb_id & RAB_OFFSET)-DTCH] += sdu_buffer_size;
-    }
-    }*/
+  
+  for (pdcp_uid=0; pdcp_uid< NUMBER_OF_UE_MAX;pdcp_uid++){
+    if (pdcp_enb[ctxt_pP->module_id].rnti[pdcp_uid] == ctxt_pP->rnti ) 
+      break;
+  }
+
+  //LOG_I(PDCP,"ueid %d lcid %d tx seq num %d\n", pdcp_uid, rb_idP+rb_offset, current_sn);
+  Pdcp_stats_tx[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]++;
+  Pdcp_stats_tx_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]++;
+  Pdcp_stats_tx_bytes[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]+=sdu_buffer_sizeP;
+  Pdcp_stats_tx_bytes_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]+=sdu_buffer_sizeP;
+  Pdcp_stats_tx_sn[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]=current_sn;
+
+  Pdcp_stats_tx_aiat[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]+= (pdcp_enb[ctxt_pP->module_id].sfn - Pdcp_stats_tx_iat[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]);
+  Pdcp_stats_tx_aiat_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]+= (pdcp_enb[ctxt_pP->module_id].sfn - Pdcp_stats_tx_iat[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]); 
+  Pdcp_stats_tx_iat[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]=pdcp_enb[ctxt_pP->module_id].sfn;
+    
   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PDCP_DATA_REQ,VCD_FUNCTION_OUT);
   return ret;
 
@@ -428,6 +438,9 @@ pdcp_data_ind(
   boolean_t    packet_forwarded = FALSE;
   hash_key_t      key             = HASHTABLE_NOT_A_KEY_VALUE;
   hashtable_rc_t  h_rc;
+  uint8_t      rb_offset= (srb_flagP == 0) ? DTCH -1 :0;
+  uint16_t     pdcp_uid=0;      
+  uint8_t      oo_flag=0;
 #if defined(LINK_ENB_PDCP_TO_GTPV1U)
   MessageDef  *message_p        = NULL;
   uint8_t     *gtpu_buffer_p    = NULL;
@@ -577,6 +590,7 @@ pdcp_data_ind(
       else
       LOG_D(PDCP, "Passing piggybacked SDU to RRC ...\n");*/
     } else {
+      oo_flag=1;
       LOG_W(PDCP,
             PROTOCOL_PDCP_CTXT_FMT"Incoming PDU has an unexpected sequence number (%d), RX window synchronisation have probably been lost!\n",
             PROTOCOL_PDCP_CTXT_ARGS(ctxt_pP, pdcp_p),
@@ -634,6 +648,7 @@ pdcp_data_ind(
 		   rb_id,
 		   sdu_buffer_sizeP - pdcp_header_len - pdcp_tailer_len,
 		   (uint8_t*)&sdu_buffer_pP->data[pdcp_header_len]);
+        free_mem_block(sdu_buffer_pP, __func__);
 
 
       // free_mem_block(new_sdu, __func__);
@@ -729,9 +744,9 @@ pdcp_data_ind(
     GTPV1U_ENB_TUNNEL_DATA_REQ(message_p).rnti         = ctxt_pP->rnti;
     GTPV1U_ENB_TUNNEL_DATA_REQ(message_p).rab_id       = rb_id + 4;
     itti_send_msg_to_task(TASK_GTPV1_U, INSTANCE_DEFAULT, message_p);
-    packet_forwarded = TRUE;
+    packet_forwarded = TRUE;    
   }
-
+  
 #else
   packet_forwarded = FALSE;
 #endif
@@ -776,28 +791,44 @@ pdcp_data_ind(
              sdu_buffer_sizeP - payload_offset);
       list_add_tail_eurecom (new_sdu_p, sdu_list_p);
 
-      /* Print octets of incoming data in hexadecimal form */
-      LOG_D(PDCP, "Following content has been received from RLC (%d,%d)(PDCP header has already been removed):\n",
-            sdu_buffer_sizeP  - payload_offset + (int)sizeof(pdcp_data_ind_header_t),
-            sdu_buffer_sizeP  - payload_offset);
-      //util_print_hex_octets(PDCP, &new_sdu_p->data[sizeof (pdcp_data_ind_header_t)], sdu_buffer_sizeP - payload_offset);
-      //util_flush_hex_octets(PDCP, &new_sdu_p->data[sizeof (pdcp_data_ind_header_t)], sdu_buffer_sizeP - payload_offset);
-
-      /*
-       * Update PDCP statistics
-       * XXX Following two actions are identical, is there a merge error?
-       */
-
-      /*if (ctxt_pP->enb_flag == 1) {
-          Pdcp_stats_rx[module_id][(rb_idP & RAB_OFFSET2) >> RAB_SHIFT2][(rb_idP & RAB_OFFSET) - DTCH]++;
-          Pdcp_stats_rx_bytes[module_id][(rb_idP & RAB_OFFSET2) >> RAB_SHIFT2][(rb_idP & RAB_OFFSET) - DTCH] += sdu_buffer_sizeP;
-        } else {
-          Pdcp_stats_rx[module_id][(rb_idP & RAB_OFFSET2) >> RAB_SHIFT2][(rb_idP & RAB_OFFSET) - DTCH]++;
-          Pdcp_stats_rx_bytes[module_id][(rb_idP & RAB_OFFSET2) >> RAB_SHIFT2][(rb_idP & RAB_OFFSET) - DTCH] += sdu_buffer_sizeP;
-        }*/
+      
+      
     }
   }
 
+  /* Print octets of incoming data in hexadecimal form */
+  LOG_D(PDCP, "Following content has been received from RLC (%d,%d)(PDCP header has already been removed):\n",
+	sdu_buffer_sizeP  - payload_offset + (int)sizeof(pdcp_data_ind_header_t),
+	sdu_buffer_sizeP  - payload_offset);
+  //util_print_hex_octets(PDCP, &new_sdu_p->data[sizeof (pdcp_data_ind_header_t)], sdu_buffer_sizeP - payload_offset);
+  //util_flush_hex_octets(PDCP, &new_sdu_p->data[sizeof (pdcp_data_ind_header_t)], sdu_buffer_sizeP - payload_offset);
+  
+  /*
+     * Update PDCP statistics
+   * XXX Following two actions are identical, is there a merge error?
+   */
+  
+  for (pdcp_uid=0; pdcp_uid< NUMBER_OF_UE_MAX;pdcp_uid++){
+    if (pdcp_enb[ctxt_pP->module_id].rnti[pdcp_uid] == ctxt_pP->rnti ){
+      break;
+    }
+  }	
+  
+  Pdcp_stats_rx[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]++;
+  Pdcp_stats_rx_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]++;
+  Pdcp_stats_rx_bytes[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]+=(sdu_buffer_sizeP  - payload_offset);
+  Pdcp_stats_rx_bytes_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]+=(sdu_buffer_sizeP  - payload_offset);
+  
+  Pdcp_stats_rx_sn[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]=sequence_number;
+  
+  if (oo_flag == 1 )
+    Pdcp_stats_rx_outoforder[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]++;
+  
+  Pdcp_stats_rx_aiat[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]+= (pdcp_enb[ctxt_pP->module_id].sfn - Pdcp_stats_rx_iat[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]);
+  Pdcp_stats_rx_aiat_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]+=(pdcp_enb[ctxt_pP->module_id].sfn - Pdcp_stats_rx_iat[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]);
+  Pdcp_stats_rx_iat[ctxt_pP->module_id][pdcp_uid][rb_idP+rb_offset]=pdcp_enb[ctxt_pP->module_id].sfn;
+
+  
 #if defined(STOP_ON_IP_TRAFFIC_OVERLOAD)
   else {
     AssertFatal(0, PROTOCOL_PDCP_CTXT_FMT" PDCP_DATA_IND SDU DROPPED, OUT OF MEMORY \n",
@@ -818,6 +849,53 @@ pdcp_data_ind(
   return TRUE;
 }
 
+void pdcp_update_stats(const protocol_ctxt_t* const  ctxt_pP){
+
+  uint8_t            pdcp_uid = 0;
+  uint8_t            rb_id     = 0;
+  
+ // these stats are measured for both eNB and UE on per seond basis 
+  for (rb_id =0; rb_id < NB_RB_MAX; rb_id ++){
+    for (pdcp_uid=0; pdcp_uid< NUMBER_OF_UE_MAX;pdcp_uid++){
+      //printf("frame %d and subframe %d \n", pdcp_enb[ctxt_pP->module_id].frame, pdcp_enb[ctxt_pP->module_id].subframe);
+      // tx stats
+      if (pdcp_enb[ctxt_pP->module_id].sfn % Pdcp_stats_tx_window_ms[ctxt_pP->module_id][pdcp_uid] == 0){
+	// unit: bit/s
+	Pdcp_stats_tx_throughput_w[ctxt_pP->module_id][pdcp_uid][rb_id]=Pdcp_stats_tx_bytes_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id]*8;
+	Pdcp_stats_tx_w[ctxt_pP->module_id][pdcp_uid][rb_id]= Pdcp_stats_tx_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id];
+	Pdcp_stats_tx_bytes_w[ctxt_pP->module_id][pdcp_uid][rb_id]= Pdcp_stats_tx_bytes_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id];
+	if (Pdcp_stats_tx_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id] > 0){
+	  Pdcp_stats_tx_aiat_w[ctxt_pP->module_id][pdcp_uid][rb_id]=(Pdcp_stats_tx_aiat_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id]/Pdcp_stats_tx_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id]);
+	}else {
+	  Pdcp_stats_tx_aiat_w[ctxt_pP->module_id][pdcp_uid][rb_id]=0;
+	}
+	// reset the tmp vars 
+	Pdcp_stats_tx_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id]=0;
+	Pdcp_stats_tx_bytes_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id]=0;
+	Pdcp_stats_tx_aiat_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id]=0;
+	
+      }
+      if (pdcp_enb[ctxt_pP->module_id].sfn % Pdcp_stats_rx_window_ms[ctxt_pP->module_id][pdcp_uid] == 0){
+	// rx stats
+	Pdcp_stats_rx_goodput_w[ctxt_pP->module_id][pdcp_uid][rb_id]=Pdcp_stats_rx_bytes_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id]*8;
+	Pdcp_stats_rx_w[ctxt_pP->module_id][pdcp_uid][rb_id]= 	Pdcp_stats_rx_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id];
+	Pdcp_stats_rx_bytes_w[ctxt_pP->module_id][pdcp_uid][rb_id]= Pdcp_stats_rx_bytes_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id];
+	
+	if(Pdcp_stats_rx_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id] > 0){
+	  Pdcp_stats_rx_aiat_w[ctxt_pP->module_id][pdcp_uid][rb_id]= (Pdcp_stats_rx_aiat_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id]/Pdcp_stats_rx_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id]);
+	} else {
+	  Pdcp_stats_rx_aiat_w[ctxt_pP->module_id][pdcp_uid][rb_id]=0;
+	}
+	
+	// reset the tmp vars 
+	Pdcp_stats_rx_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id]=0;
+	Pdcp_stats_rx_bytes_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id]=0;
+	Pdcp_stats_rx_aiat_tmp_w[ctxt_pP->module_id][pdcp_uid][rb_id]=0;
+      } 
+    }
+    
+  }
+}
 //-----------------------------------------------------------------------------
 void
 pdcp_run (
@@ -825,6 +903,7 @@ pdcp_run (
 )
 //-----------------------------------------------------------------------------
 {
+  
 #if defined(ENABLE_ITTI)
   MessageDef   *msg_p;
   const char   *msg_name;
@@ -833,12 +912,18 @@ pdcp_run (
   protocol_ctxt_t  ctxt;
 #endif
 
+  
   if (ctxt_pP->enb_flag) {
     start_meas(&eNB_pdcp_stats[ctxt_pP->module_id].pdcp_run);
   } else {
     start_meas(&UE_pdcp_stats[ctxt_pP->module_id].pdcp_run);
   }
 
+  pdcp_enb[ctxt_pP->module_id].sfn++; // range: 0 to 18,446,744,073,709,551,615
+  pdcp_enb[ctxt_pP->module_id].frame=ctxt_pP->frame; // 1023 
+  pdcp_enb[ctxt_pP->module_id].subframe= ctxt_pP->subframe;
+  pdcp_update_stats(ctxt_pP);
+   
   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PDCP_RUN, VCD_FUNCTION_IN);
 
 #if defined(ENABLE_ITTI)
@@ -971,6 +1056,28 @@ pdcp_run (
   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PDCP_RUN, VCD_FUNCTION_OUT);
 }
 
+void pdcp_add_UE(const protocol_ctxt_t* const  ctxt_pP){
+  int i, ue_flag=1; //, ret=-1; to be decied later
+  for (i=0; i < NUMBER_OF_UE_MAX; i++){
+    if (pdcp_enb[ctxt_pP->module_id].rnti[i] == ctxt_pP->rnti) {
+      ue_flag=-1;
+      break;
+    }
+  }
+  if (ue_flag == 1 ){
+    for (i=0; i < NUMBER_OF_UE_MAX ; i++){
+      if (pdcp_enb[ctxt_pP->module_id].rnti[i] == 0 ){
+	pdcp_enb[ctxt_pP->module_id].rnti[i]=ctxt_pP->rnti;
+	pdcp_enb[ctxt_pP->module_id].uid[i]=i;
+	pdcp_enb[ctxt_pP->module_id].num_ues++;
+	printf("add new uid is %d %x\n\n", i, ctxt_pP->rnti);
+	// ret=1;
+	break;
+      }
+    }
+  }
+  //return ret;
+}
 
 //-----------------------------------------------------------------------------
 boolean_t
@@ -983,8 +1090,8 @@ pdcp_remove_UE(
   DRB_Identity_t  drb_id         = 0;
   hash_key_t      key            = HASHTABLE_NOT_A_KEY_VALUE;
   hashtable_rc_t  h_rc;
-
-  // check and remove SRBs first
+  int i; 
+   // check and remove SRBs first
 
   for(int i = 0;i<NUMBER_OF_UE_MAX;i++){
     if(pdcp_eNB_UE_instance_to_rnti[i] == ctxt_pP->rnti){
@@ -993,7 +1100,7 @@ pdcp_remove_UE(
     }
   }
 
-  for (srb_id=0; srb_id<2; srb_id++) {
+  for (srb_id=1; srb_id<3; srb_id++) {
     key = PDCP_COLL_KEY_VALUE(ctxt_pP->module_id, ctxt_pP->rnti, ctxt_pP->enb_flag, srb_id, SRB_FLAG_YES);
     h_rc = hashtable_remove(pdcp_coll_p, key);
   }
@@ -1006,6 +1113,19 @@ pdcp_remove_UE(
 
   (void)h_rc; /* remove gcc warning "set but not used" */
 
+  // remove ue for pdcp enb inst
+   for (i=0; i < NUMBER_OF_UE_MAX; i++) {
+    if (pdcp_enb[ctxt_pP->module_id].rnti[i] == ctxt_pP->rnti ) {
+      LOG_I(PDCP, "remove uid is %d/%d %x\n", i,
+	    pdcp_enb[ctxt_pP->module_id].uid[i],
+	    pdcp_enb[ctxt_pP->module_id].rnti[i]);
+      pdcp_enb[ctxt_pP->module_id].uid[i]=0;
+      pdcp_enb[ctxt_pP->module_id].rnti[i]=0;
+      pdcp_enb[ctxt_pP->module_id].num_ues--;
+      break;
+    }
+  }
+   
   return 1;
 }
 
@@ -1090,7 +1210,7 @@ rrc_pdcp_config_asn1_req (
           return TRUE;
 
       } else {
-          LOG_D(PDCP, PROTOCOL_PDCP_CTXT_FMT" CONFIG_ACTION_ADD key 0x%"PRIx64"\n",
+	  LOG_D(PDCP, PROTOCOL_PDCP_CTXT_FMT" CONFIG_ACTION_ADD key 0x%"PRIx64"\n",
                 PROTOCOL_PDCP_CTXT_ARGS(ctxt_pP, pdcp_p),
                 key);
         }
@@ -1400,7 +1520,6 @@ rrc_pdcp_config_asn1_req (
   return 0;
 }
 
-
 //-----------------------------------------------------------------------------
 boolean_t
 pdcp_config_req_asn1 (
@@ -1421,12 +1540,14 @@ pdcp_config_req_asn1 (
   uint8_t         *const        kUPenc_pP)
 //-----------------------------------------------------------------------------
 {
-
+  
   switch (actionP) {
   case CONFIG_ACTION_ADD:
     DevAssert(pdcp_pP != NULL);
     if (ctxt_pP->enb_flag == ENB_FLAG_YES) {
       pdcp_pP->is_ue = FALSE;
+      pdcp_add_UE(ctxt_pP);
+      
       //pdcp_eNB_UE_instance_to_rnti[ctxtP->module_id] = ctxt_pP->rnti;
 //      pdcp_eNB_UE_instance_to_rnti[pdcp_eNB_UE_instance_to_rnti_index] = ctxt_pP->rnti;
       if( srb_flagP == SRB_FLAG_NO ) {
@@ -1536,6 +1657,10 @@ pdcp_config_req_asn1 (
           lc_idP,
           rb_idP);
 
+   if (ctxt_pP->enb_flag == ENB_FLAG_YES) {
+     // pdcp_remove_UE(ctxt_pP);
+   }
+
     /* Security keys */
     if (pdcp_pP->kUPenc != NULL) {
       free(pdcp_pP->kUPenc);
@@ -1787,13 +1912,14 @@ rrc_pdcp_config_req (
 
 
 //-----------------------------------------------------------------------------
-// TODO PDCP module initialization code might be removed
+ 
 int
 pdcp_module_init (
   void
 )
 //-----------------------------------------------------------------------------
 {
+ 
 #ifdef PDCP_USE_RT_FIFO
   int ret;
 
@@ -1869,6 +1995,7 @@ void pdcp_layer_init(void)
 {
 
   module_id_t       instance;
+  int i,j;
 #if defined(Rel10) || defined(Rel14)
   mbms_session_id_t session_id;
   mbms_service_id_t service_id;
@@ -1912,15 +2039,42 @@ void pdcp_layer_init(void)
   pdcp_output_header_bytes_to_write=0;
   pdcp_input_sdu_remaining_size_to_read=0;
 
+  memset(pdcp_enb, 0, sizeof(pdcp_enb_t));
+
+  
+  memset(Pdcp_stats_tx_window_ms, 0, sizeof(Pdcp_stats_tx_window_ms));
+  memset(Pdcp_stats_rx_window_ms, 0, sizeof(Pdcp_stats_rx_window_ms));
+  for (i =0; i< MAX_NUM_CCs ; i ++){
+    for (j=0; j< NUMBER_OF_UE_MAX;j++){
+      Pdcp_stats_tx_window_ms[i][j]=100;
+      Pdcp_stats_rx_window_ms[i][j]=100;
+    }
+  }
+  
   memset(Pdcp_stats_tx, 0, sizeof(Pdcp_stats_tx));
+  memset(Pdcp_stats_tx_w, 0, sizeof(Pdcp_stats_tx_w));
+  memset(Pdcp_stats_tx_tmp_w, 0, sizeof(Pdcp_stats_tx_tmp_w));
   memset(Pdcp_stats_tx_bytes, 0, sizeof(Pdcp_stats_tx_bytes));
-  memset(Pdcp_stats_tx_bytes_last, 0, sizeof(Pdcp_stats_tx_bytes_last));
-  memset(Pdcp_stats_tx_rate, 0, sizeof(Pdcp_stats_tx_rate));
+  memset(Pdcp_stats_tx_bytes_w, 0, sizeof(Pdcp_stats_tx_bytes_w));
+  memset(Pdcp_stats_tx_bytes_tmp_w, 0, sizeof(Pdcp_stats_tx_bytes_tmp_w));
+  memset(Pdcp_stats_tx_sn, 0, sizeof(Pdcp_stats_tx_sn));
+  memset(Pdcp_stats_tx_throughput_w, 0, sizeof(Pdcp_stats_tx_throughput_w));
+  memset(Pdcp_stats_tx_aiat, 0, sizeof(Pdcp_stats_tx_aiat));
+  memset(Pdcp_stats_tx_iat, 0, sizeof(Pdcp_stats_tx_iat));
+  
 
   memset(Pdcp_stats_rx, 0, sizeof(Pdcp_stats_rx));
+  memset(Pdcp_stats_rx_w, 0, sizeof(Pdcp_stats_rx_w));
+  memset(Pdcp_stats_rx_tmp_w, 0, sizeof(Pdcp_stats_rx_tmp_w));
   memset(Pdcp_stats_rx_bytes, 0, sizeof(Pdcp_stats_rx_bytes));
-  memset(Pdcp_stats_rx_bytes_last, 0, sizeof(Pdcp_stats_rx_bytes_last));
-  memset(Pdcp_stats_rx_rate, 0, sizeof(Pdcp_stats_rx_rate));
+  memset(Pdcp_stats_rx_bytes_w, 0, sizeof(Pdcp_stats_rx_bytes_w));
+  memset(Pdcp_stats_rx_bytes_tmp_w, 0, sizeof(Pdcp_stats_rx_bytes_tmp_w));
+  memset(Pdcp_stats_rx_sn, 0, sizeof(Pdcp_stats_rx_sn));
+  memset(Pdcp_stats_rx_goodput_w, 0, sizeof(Pdcp_stats_rx_goodput_w));
+  memset(Pdcp_stats_rx_aiat, 0, sizeof(Pdcp_stats_rx_aiat));
+  memset(Pdcp_stats_rx_iat, 0, sizeof(Pdcp_stats_rx_iat));
+  memset(Pdcp_stats_rx_outoforder, 0, sizeof(Pdcp_stats_rx_outoforder));
+    
 }
 
 //-----------------------------------------------------------------------------
diff --git a/openair2/LAYER2/PDCP_v10.1.0/pdcp.h b/openair2/LAYER2/PDCP_v10.1.0/pdcp.h
index 30a45acc72fc9d7183b156a4bd85f5e8112bf251..7c8f2dd033571f3faac8d484ea99be494b020a81 100644
--- a/openair2/LAYER2/PDCP_v10.1.0/pdcp.h
+++ b/openair2/LAYER2/PDCP_v10.1.0/pdcp.h
@@ -94,15 +94,54 @@ extern int             pdcp_instance_cnt;
 int init_pdcp_thread(void);
 void cleanup_pdcp_thread(void);
 
-
-public_pdcp(unsigned int Pdcp_stats_tx[NB_MODULES_MAX][NB_CNX_CH][NB_RAB_MAX]);
-public_pdcp(unsigned int Pdcp_stats_tx_bytes[NB_MODULES_MAX][NB_CNX_CH][NB_RAB_MAX]);
-public_pdcp(unsigned int Pdcp_stats_tx_bytes_last[NB_MODULES_MAX][NB_CNX_CH][NB_RAB_MAX]);
-public_pdcp(unsigned int Pdcp_stats_tx_rate[NB_MODULES_MAX][NB_CNX_CH][NB_RAB_MAX]);
-public_pdcp(unsigned int Pdcp_stats_rx[NB_MODULES_MAX][NB_CNX_CH][NB_RAB_MAX]);
-public_pdcp(unsigned int Pdcp_stats_rx_bytes[NB_MODULES_MAX][NB_CNX_CH][NB_RAB_MAX]);
-public_pdcp(unsigned int Pdcp_stats_rx_bytes_last[NB_MODULES_MAX][NB_CNX_CH][NB_RAB_MAX]);
-public_pdcp(unsigned int Pdcp_stats_rx_rate[NB_MODULES_MAX][NB_CNX_CH][NB_RAB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_tx_window_ms[MAX_NUM_CCs][NUMBER_OF_UE_MAX]);
+public_pdcp(uint32_t Pdcp_stats_tx_bytes[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_tx_bytes_w[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_tx_bytes_tmp_w[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_tx[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_tx_w[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_tx_tmp_w[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_tx_sn[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_tx_throughput_w[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_tx_aiat[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_tx_aiat_w[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_tx_aiat_tmp_w[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_tx_iat[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+
+public_pdcp(uint32_t Pdcp_stats_rx_window_ms[MAX_NUM_CCs][NUMBER_OF_UE_MAX]);
+public_pdcp(uint32_t Pdcp_stats_rx[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_rx_w[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_rx_tmp_w[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_rx_bytes[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_rx_bytes_w[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_rx_bytes_tmp_w[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_rx_sn[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_rx_goodput_w[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_rx_aiat[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_rx_aiat_w[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_rx_aiat_tmp_w[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_rx_iat[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+public_pdcp(uint32_t Pdcp_stats_rx_outoforder[MAX_NUM_CCs][NUMBER_OF_UE_MAX][NB_RB_MAX]);
+
+public_pdcp(void pdcp_update_perioidical_stats(const protocol_ctxt_t* const  ctxt_pP));
+
+
+/*Packet Probing for agent PDCP*/
+//public_pdcp(uint64_t *pdcp_packet_counter);
+//public_pdcp(uint64_t *pdcp_size_packet);
+typedef struct pdcp_enb_s {
+  // used for eNB stats generation
+  uint16_t uid[NUMBER_OF_UE_MAX];
+  rnti_t rnti[NUMBER_OF_UE_MAX];
+  uint16_t num_ues;
+  
+  uint64_t sfn;
+  frame_t  frame;
+  sub_frame_t subframe;
+  
+} pdcp_enb_t; 
+
+public_pdcp(pdcp_enb_t pdcp_enb[MAX_NUM_CCs]);
 
 typedef struct pdcp_stats_s {
   time_stats_t pdcp_run;
@@ -125,7 +164,7 @@ typedef struct pdcp_s {
   boolean_t is_ue;
   boolean_t is_srb;
 
-  /* Configured security algorithms */
+   /* Configured security algorithms */
   uint8_t cipheringAlgorithm;
   uint8_t integrityProtAlgorithm;
 
@@ -327,9 +366,15 @@ public_pdcp(boolean_t pdcp_config_req_asn1 (
               uint8_t         *const kRRCint,
               uint8_t         *const kUPenc));
 
-
+/*! \fn void pdcp_add_UE(const protocol_ctxt_t* const  ctxt_pP)
+* \brief  Function (for RRC) to add a new UE in PDCP module
+* \param[in]  ctxt_pP           Running context.
+* \return     A status about the processing, OK or error code.
+*/
+public_pdcp(void pdcp_add_UE(const protocol_ctxt_t* const  ctxt_pP));
+  
 /*! \fn boolean_t pdcp_remove_UE(const protocol_ctxt_t* const  ctxt_pP)
-* \brief  Function for RRC to configure a Radio Bearer clear all PDCP resources for a particular UE
+* \brief  Function for RRC to remove UE from PDCP module hashtable 
 * \param[in]  ctxt_pP           Running context.
 * \return     A status about the processing, OK or error code.
 */
diff --git a/openair2/LAYER2/openair2_proc.c b/openair2/LAYER2/openair2_proc.c
index f66d1142abf5c668f240de1fcda9bc2aa65337ff..7cb6ce0d2c28c6b82fa7a06e1fcb5d791550dee6 100644
--- a/openair2/LAYER2/openair2_proc.c
+++ b/openair2/LAYER2/openair2_proc.c
@@ -228,7 +228,9 @@ int dump_eNB_l2_stats(char *buffer, int length)
       PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt,
 				     eNB_id,
 				     ENB_FLAG_YES,
-				     UE_list->eNB_UE_stats[0][UE_id].crnti,//UE_PCCID(eNB_id,UE_id)][UE_id].crnti, 
+                                     /* the following is suspicious, let's put back what was there before */
+				     /*UE_list->eNB_UE_stats[0][UE_id].crnti,//UE_PCCID(eNB_id,UE_id)][UE_id].crnti, */
+				     UE_list->eNB_UE_stats[UE_PCCID(eNB_id,UE_id)][UE_id].crnti,
 				     eNB->frame,
 				     eNB->subframe,
 				     eNB_id);
diff --git a/openair2/RRC/LITE/L2_interface.c b/openair2/RRC/LITE/L2_interface.c
index ab4fd170c0678729ef5ce14939e46ae945ac2aa7..91834d9c9d64f4742cec1cd2b9d538a8ca9b8065 100644
--- a/openair2/RRC/LITE/L2_interface.c
+++ b/openair2/RRC/LITE/L2_interface.c
@@ -53,6 +53,8 @@ extern UE_MAC_INST *UE_mac_inst;
 # include "intertask_interface.h"
 #endif
 
+#include "flexran_agent_extern.h"
+
 //#define RRC_DATA_REQ_DEBUG
 //#define DEBUG_RRC 1
 
@@ -466,6 +468,11 @@ void mac_eNB_rrc_ul_failure(const module_id_t Mod_instP,
   else {
     LOG_W(RRC,"Frame %d, Subframe %d: UL failure: UE %x unknown \n",frameP,subframeP,rntiP);
   }
+  if (rrc_agent_registered[Mod_instP]) {
+    agent_rrc_xface[Mod_instP]->flexran_agent_notify_ue_state_change(Mod_instP,
+								     rntiP,
+								     PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_DEACTIVATED);
+  }
 //  rrc_mac_remove_ue(Mod_instP,rntiP);
 }
 
diff --git a/openair2/RRC/LITE/defs.h b/openair2/RRC/LITE/defs.h
index 507d70e459ac7fbc0466fa9275480a18e2ff008f..08ac30f8363f9453921c73598a3b8447fc7abc93 100644
--- a/openair2/RRC/LITE/defs.h
+++ b/openair2/RRC/LITE/defs.h
@@ -65,6 +65,9 @@
 #include "UE-EUTRA-Capability.h"
 #include "MeasResults.h"
 
+/* for ImsiMobileIdentity_t */
+#include "MobileIdentity.h"
+
 /* correct Rel(8|10)/Rel14 differences
  * the code is in favor of Rel14, those defines do the translation
  */
@@ -391,6 +394,10 @@ typedef struct eNB_RRC_UE_s {
   SRB_INFO_TABLE_ENTRY               Srb2;
   MeasConfig_t*                      measConfig;
   HANDOVER_INFO*                     handover_info;
+  MeasResults_t*                     measResults;
+
+  UE_EUTRA_Capability_t*             UE_Capability;
+  ImsiMobileIdentity_t               imsi;
 
 #if defined(ENABLE_SECURITY)
   /* KeNB as derived from KASME received from EPC */
diff --git a/openair2/RRC/LITE/extern.h b/openair2/RRC/LITE/extern.h
index 71d8c9d0dcf8780dd91542ee1b5019ae12aa38d6..b45dbd11e9557db9667b6c2ea24f1906eb67f774 100644
--- a/openair2/RRC/LITE/extern.h
+++ b/openair2/RRC/LITE/extern.h
@@ -74,8 +74,8 @@ extern uint16_t N310[8];
 extern uint16_t N311[8];
 extern uint32_t T304[8];
 extern uint32_t timeToTrigger_ms[16];
-extern float RSRP_meas_mapping[100];
-extern float RSRQ_meas_mapping[33];
+extern float RSRP_meas_mapping[98];
+extern float RSRQ_meas_mapping[35];
 
 extern UE_PF_PO_t UE_PF_PO[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
 extern pthread_mutex_t ue_pf_po_mutex;
diff --git a/openair2/RRC/LITE/proto.h b/openair2/RRC/LITE/proto.h
index 4cebb778605011f5e78b8bb278cb6da45ac028bd..9ece5bcfdd7b2a2415cab98c544b13f3da0addab 100644
--- a/openair2/RRC/LITE/proto.h
+++ b/openair2/RRC/LITE/proto.h
@@ -33,6 +33,8 @@
 
 #include "RRC/LITE/defs.h"
 
+#include "flexran_agent_extern.h"
+
 //main.c
 int rrc_init_global_param(void);
 int L3_xface_init(void);
@@ -269,6 +271,15 @@ rrc_eNB_generate_defaultRRCConnectionReconfiguration(
   const uint8_t                ho_state
 );
 
+
+void
+flexran_rrc_eNB_generate_defaultRRCConnectionReconfiguration(
+							     const protocol_ctxt_t* const ctxt_pP,
+							     rrc_eNB_ue_context_t* const ue_context_pP,
+							     const uint8_t ho_state,
+							     agent_reconf_rrc * trig_param
+							     );
+
 int freq_to_arfcn10(int band, unsigned long freq);
 
 void
@@ -465,7 +476,7 @@ rrc_eNB_generate_SecurityModeCommand(
 void
 rrc_eNB_process_MeasurementReport(
   const protocol_ctxt_t* const ctxt_pP,
-  rrc_eNB_ue_context_t*          const ue_context_pP,
+  rrc_eNB_ue_context_t*        ue_context_pP,
   const MeasResults_t*   const measResults2
 );
 
diff --git a/openair2/RRC/LITE/rrc_UE.c b/openair2/RRC/LITE/rrc_UE.c
index a8b24c326e2cf2481ade0be33622d55cc6669684..8d1f47daf5b61fc9ebd9792864b5ce9a897fac02 100644
--- a/openair2/RRC/LITE/rrc_UE.c
+++ b/openair2/RRC/LITE/rrc_UE.c
@@ -4312,6 +4312,7 @@ void *rrc_ue_task( void *args_p )
 
     switch (ITTI_MSG_ID(msg_p)) {
     case TERMINATE_MESSAGE:
+      LOG_W(RRC, " *** Exiting RRC thread\n");
       itti_exit_task ();
       break;
 
diff --git a/openair2/RRC/LITE/rrc_eNB.c b/openair2/RRC/LITE/rrc_eNB.c
index 946da42920cae5f7af523fe3061f7a9798130862..2a8967a531573b187e677c400c8ac6621b9e49d8 100644
--- a/openair2/RRC/LITE/rrc_eNB.c
+++ b/openair2/RRC/LITE/rrc_eNB.c
@@ -95,9 +95,6 @@
 
 #include "SIMULATION/TOOLS/defs.h" // for taus
 
-#if defined(FLEXRAN_AGENT_SB_IF)
-#include "flexran_agent_extern.h"
-#endif
 //#define XER_PRINT
 
 extern RAN_CONTEXT_t RC;
@@ -874,6 +871,11 @@ rrc_eNB_free_UE(const module_id_t enb_mod_idP,const struct rrc_eNB_ue_context_s*
         }
       }
 
+      if (rrc_agent_registered[enb_mod_idP]) {
+        agent_rrc_xface[enb_mod_idP]->flexran_agent_notify_ue_state_change(enb_mod_idP,
+                              rnti, PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_DEACTIVATED);
+      }
+
       for(j = 0; j < 10; j++){
         ul_req_tmp = &eNB_MAC->UL_req_tmp[CC_id][j].ul_config_request_body;
         if(ul_req_tmp){
@@ -2998,25 +3000,728 @@ rrc_eNB_generate_defaultRRCConnectionReconfiguration(const protocol_ctxt_t* cons
   MeasId3->reportConfigId = 4;
   ASN_SEQUENCE_ADD(&MeasId_list->list, MeasId3);
 
-  MeasId4 = CALLOC(1, sizeof(*MeasId4));
-  MeasId4->measId = 5;
-  MeasId4->measObjectId = 1;
-  MeasId4->reportConfigId = 5;
-  ASN_SEQUENCE_ADD(&MeasId_list->list, MeasId4);
+  MeasId4 = CALLOC(1, sizeof(*MeasId4));
+  MeasId4->measId = 5;
+  MeasId4->measObjectId = 1;
+  MeasId4->reportConfigId = 5;
+  ASN_SEQUENCE_ADD(&MeasId_list->list, MeasId4);
+
+  MeasId5 = CALLOC(1, sizeof(*MeasId5));
+  MeasId5->measId = 6;
+  MeasId5->measObjectId = 1;
+  MeasId5->reportConfigId = 6;
+  ASN_SEQUENCE_ADD(&MeasId_list->list, MeasId5);
+
+  //  rrcConnectionReconfiguration->criticalExtensions.choice.c1.choice.rrcConnectionReconfiguration_r8.measConfig->measIdToAddModList = MeasId_list;
+
+  // Add one EUTRA Measurement Object
+  MeasObj_list = CALLOC(1, sizeof(*MeasObj_list));
+  memset((void *)MeasObj_list, 0, sizeof(*MeasObj_list));
+
+  // Configure MeasObject
+
+  MeasObj = CALLOC(1, sizeof(*MeasObj));
+  memset((void *)MeasObj, 0, sizeof(*MeasObj));
+
+  MeasObj->measObjectId = 1;
+  MeasObj->measObject.present = MeasObjectToAddMod__measObject_PR_measObjectEUTRA;
+  MeasObj->measObject.choice.measObjectEUTRA.carrierFreq = 3350; //band 7, 2.68GHz
+  //MeasObj->measObject.choice.measObjectEUTRA.carrierFreq = 36090; //band 33, 1.909GHz
+  MeasObj->measObject.choice.measObjectEUTRA.allowedMeasBandwidth = AllowedMeasBandwidth_mbw25;
+  MeasObj->measObject.choice.measObjectEUTRA.presenceAntennaPort1 = 1;
+  MeasObj->measObject.choice.measObjectEUTRA.neighCellConfig.buf = CALLOC(1, sizeof(uint8_t));
+  MeasObj->measObject.choice.measObjectEUTRA.neighCellConfig.buf[0] = 0;
+  MeasObj->measObject.choice.measObjectEUTRA.neighCellConfig.size = 1;
+  MeasObj->measObject.choice.measObjectEUTRA.neighCellConfig.bits_unused = 6;
+  MeasObj->measObject.choice.measObjectEUTRA.offsetFreq = NULL;   // Default is 15 or 0dB
+
+  MeasObj->measObject.choice.measObjectEUTRA.cellsToAddModList =
+    (CellsToAddModList_t *) CALLOC(1, sizeof(*CellsToAddModList));
+
+  CellsToAddModList = MeasObj->measObject.choice.measObjectEUTRA.cellsToAddModList;
+
+  // Add adjacent cell lists (6 per eNB)
+  for (i = 0; i < 6; i++) {
+    CellToAdd = (CellsToAddMod_t *) CALLOC(1, sizeof(*CellToAdd));
+    CellToAdd->cellIndex = i + 1;
+    CellToAdd->physCellId = get_adjacent_cell_id(ctxt_pP->module_id, i);
+    CellToAdd->cellIndividualOffset = Q_OffsetRange_dB0;
+
+    ASN_SEQUENCE_ADD(&CellsToAddModList->list, CellToAdd);
+  }
+
+  ASN_SEQUENCE_ADD(&MeasObj_list->list, MeasObj);
+  //  rrcConnectionReconfiguration->criticalExtensions.choice.c1.choice.rrcConnectionReconfiguration_r8.measConfig->measObjectToAddModList = MeasObj_list;
+
+  // Report Configurations for periodical, A1-A5 events
+  ReportConfig_list = CALLOC(1, sizeof(*ReportConfig_list));
+
+  ReportConfig_per = CALLOC(1, sizeof(*ReportConfig_per));
+
+  ReportConfig_A1 = CALLOC(1, sizeof(*ReportConfig_A1));
+
+  ReportConfig_A2 = CALLOC(1, sizeof(*ReportConfig_A2));
+
+  ReportConfig_A3 = CALLOC(1, sizeof(*ReportConfig_A3));
+
+  ReportConfig_A4 = CALLOC(1, sizeof(*ReportConfig_A4));
+
+  ReportConfig_A5 = CALLOC(1, sizeof(*ReportConfig_A5));
+
+  ReportConfig_per->reportConfigId = 1;
+  ReportConfig_per->reportConfig.present = ReportConfigToAddMod__reportConfig_PR_reportConfigEUTRA;
+  ReportConfig_per->reportConfig.choice.reportConfigEUTRA.triggerType.present =
+    ReportConfigEUTRA__triggerType_PR_periodical;
+  ReportConfig_per->reportConfig.choice.reportConfigEUTRA.triggerType.choice.periodical.purpose =
+    ReportConfigEUTRA__triggerType__periodical__purpose_reportStrongestCells;
+  ReportConfig_per->reportConfig.choice.reportConfigEUTRA.triggerQuantity = ReportConfigEUTRA__triggerQuantity_rsrp;
+  ReportConfig_per->reportConfig.choice.reportConfigEUTRA.reportQuantity = ReportConfigEUTRA__reportQuantity_both;
+  ReportConfig_per->reportConfig.choice.reportConfigEUTRA.maxReportCells = 2;
+  ReportConfig_per->reportConfig.choice.reportConfigEUTRA.reportInterval = ReportInterval_ms120;
+  ReportConfig_per->reportConfig.choice.reportConfigEUTRA.reportAmount = ReportConfigEUTRA__reportAmount_infinity;
+
+  ASN_SEQUENCE_ADD(&ReportConfig_list->list, ReportConfig_per);
+
+  ReportConfig_A1->reportConfigId = 2;
+  ReportConfig_A1->reportConfig.present = ReportConfigToAddMod__reportConfig_PR_reportConfigEUTRA;
+  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.triggerType.present =
+    ReportConfigEUTRA__triggerType_PR_event;
+  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.present =
+    ReportConfigEUTRA__triggerType__event__eventId_PR_eventA1;
+  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.eventA1.
+  a1_Threshold.present = ThresholdEUTRA_PR_threshold_RSRP;
+  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.eventA1.
+  a1_Threshold.choice.threshold_RSRP = 10;
+
+  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.triggerQuantity = ReportConfigEUTRA__triggerQuantity_rsrp;
+  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.reportQuantity = ReportConfigEUTRA__reportQuantity_both;
+  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.maxReportCells = 2;
+  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.reportInterval = ReportInterval_ms120;
+  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.reportAmount = ReportConfigEUTRA__reportAmount_infinity;
+
+  ASN_SEQUENCE_ADD(&ReportConfig_list->list, ReportConfig_A1);
+  
+  if (ho_state == 1 /*HO_MEASURMENT */ ) {
+    LOG_I(RRC, "[eNB %d] frame %d: requesting A2, A3, A4, A5, and A6 event reporting\n",
+          ctxt_pP->module_id, ctxt_pP->frame);
+    ReportConfig_A2->reportConfigId = 3;
+    ReportConfig_A2->reportConfig.present = ReportConfigToAddMod__reportConfig_PR_reportConfigEUTRA;
+    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.triggerType.present =
+      ReportConfigEUTRA__triggerType_PR_event;
+    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.present =
+      ReportConfigEUTRA__triggerType__event__eventId_PR_eventA2;
+    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
+    eventA2.a2_Threshold.present = ThresholdEUTRA_PR_threshold_RSRP;
+    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
+    eventA2.a2_Threshold.choice.threshold_RSRP = 10;
+
+    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.triggerQuantity =
+      ReportConfigEUTRA__triggerQuantity_rsrp;
+    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.reportQuantity = ReportConfigEUTRA__reportQuantity_both;
+    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.maxReportCells = 2;
+    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.reportInterval = ReportInterval_ms120;
+    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.reportAmount = ReportConfigEUTRA__reportAmount_infinity;
+
+    ASN_SEQUENCE_ADD(&ReportConfig_list->list, ReportConfig_A2);
+
+    ReportConfig_A3->reportConfigId = 4;
+    ReportConfig_A3->reportConfig.present = ReportConfigToAddMod__reportConfig_PR_reportConfigEUTRA;
+    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.triggerType.present =
+      ReportConfigEUTRA__triggerType_PR_event;
+    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.present =
+      ReportConfigEUTRA__triggerType__event__eventId_PR_eventA3;
+
+    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.eventA3.a3_Offset = 1;   //10;
+    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
+    eventA3.reportOnLeave = 1;
+
+    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.triggerQuantity =
+      ReportConfigEUTRA__triggerQuantity_rsrp;
+    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.reportQuantity = ReportConfigEUTRA__reportQuantity_both;
+    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.maxReportCells = 2;
+    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.reportInterval = ReportInterval_ms120;
+    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.reportAmount = ReportConfigEUTRA__reportAmount_infinity;
+
+    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.hysteresis = 0.5; // FIXME ...hysteresis is of type long!
+    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.timeToTrigger =
+      TimeToTrigger_ms40;
+    ASN_SEQUENCE_ADD(&ReportConfig_list->list, ReportConfig_A3);
+
+    ReportConfig_A4->reportConfigId = 5;
+    ReportConfig_A4->reportConfig.present = ReportConfigToAddMod__reportConfig_PR_reportConfigEUTRA;
+    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.triggerType.present =
+      ReportConfigEUTRA__triggerType_PR_event;
+    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.present =
+      ReportConfigEUTRA__triggerType__event__eventId_PR_eventA4;
+    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
+    eventA4.a4_Threshold.present = ThresholdEUTRA_PR_threshold_RSRP;
+    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
+    eventA4.a4_Threshold.choice.threshold_RSRP = 10;
+
+    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.triggerQuantity =
+      ReportConfigEUTRA__triggerQuantity_rsrp;
+    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.reportQuantity = ReportConfigEUTRA__reportQuantity_both;
+    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.maxReportCells = 2;
+    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.reportInterval = ReportInterval_ms120;
+    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.reportAmount = ReportConfigEUTRA__reportAmount_infinity;
+
+    ASN_SEQUENCE_ADD(&ReportConfig_list->list, ReportConfig_A4);
+
+    ReportConfig_A5->reportConfigId = 6;
+    ReportConfig_A5->reportConfig.present = ReportConfigToAddMod__reportConfig_PR_reportConfigEUTRA;
+    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.triggerType.present =
+      ReportConfigEUTRA__triggerType_PR_event;
+    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.present =
+      ReportConfigEUTRA__triggerType__event__eventId_PR_eventA5;
+    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
+    eventA5.a5_Threshold1.present = ThresholdEUTRA_PR_threshold_RSRP;
+    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
+    eventA5.a5_Threshold2.present = ThresholdEUTRA_PR_threshold_RSRP;
+    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
+    eventA5.a5_Threshold1.choice.threshold_RSRP = 10;
+    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
+    eventA5.a5_Threshold2.choice.threshold_RSRP = 10;
+
+    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.triggerQuantity =
+      ReportConfigEUTRA__triggerQuantity_rsrp;
+    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.reportQuantity = ReportConfigEUTRA__reportQuantity_both;
+    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.maxReportCells = 2;
+    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.reportInterval = ReportInterval_ms120;
+    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.reportAmount = ReportConfigEUTRA__reportAmount_infinity;
+
+    ASN_SEQUENCE_ADD(&ReportConfig_list->list, ReportConfig_A5);
+    //  rrcConnectionReconfiguration->criticalExtensions.choice.c1.choice.rrcConnectionReconfiguration_r8.measConfig->reportConfigToAddModList = ReportConfig_list;
+
+    rsrp = CALLOC(1, sizeof(RSRP_Range_t));
+    *rsrp = 20;
+
+    Sparams = CALLOC(1, sizeof(*Sparams));
+    Sparams->present = MeasConfig__speedStatePars_PR_setup;
+    Sparams->choice.setup.timeToTrigger_SF.sf_High = SpeedStateScaleFactors__sf_Medium_oDot75;
+    Sparams->choice.setup.timeToTrigger_SF.sf_Medium = SpeedStateScaleFactors__sf_High_oDot5;
+    Sparams->choice.setup.mobilityStateParameters.n_CellChangeHigh = 10;
+    Sparams->choice.setup.mobilityStateParameters.n_CellChangeMedium = 5;
+    Sparams->choice.setup.mobilityStateParameters.t_Evaluation = MobilityStateParameters__t_Evaluation_s60;
+    Sparams->choice.setup.mobilityStateParameters.t_HystNormal = MobilityStateParameters__t_HystNormal_s120;
+
+    quantityConfig = CALLOC(1, sizeof(*quantityConfig));
+    memset((void *)quantityConfig, 0, sizeof(*quantityConfig));
+    quantityConfig->quantityConfigEUTRA = CALLOC(1, sizeof(struct QuantityConfigEUTRA));
+    memset((void *)quantityConfig->quantityConfigEUTRA, 0, sizeof(*quantityConfig->quantityConfigEUTRA));
+    quantityConfig->quantityConfigCDMA2000 = NULL;
+    quantityConfig->quantityConfigGERAN = NULL;
+    quantityConfig->quantityConfigUTRA = NULL;
+    quantityConfig->quantityConfigEUTRA->filterCoefficientRSRP =
+      CALLOC(1, sizeof(*(quantityConfig->quantityConfigEUTRA->filterCoefficientRSRP)));
+    quantityConfig->quantityConfigEUTRA->filterCoefficientRSRQ =
+      CALLOC(1, sizeof(*(quantityConfig->quantityConfigEUTRA->filterCoefficientRSRQ)));
+    *quantityConfig->quantityConfigEUTRA->filterCoefficientRSRP = FilterCoefficient_fc4;
+    *quantityConfig->quantityConfigEUTRA->filterCoefficientRSRQ = FilterCoefficient_fc4;
+
+    LOG_I(RRC,
+          "[eNB %d] Frame %d: potential handover preparation: store the information in an intermediate structure in case of failure\n",
+          ctxt_pP->module_id, ctxt_pP->frame);
+    // store the information in an intermediate structure for Hanodver management
+    //rrc_inst->handover_info.as_config.sourceRadioResourceConfig.srb_ToAddModList = CALLOC(1,sizeof());
+    ue_context_pP->ue_context.handover_info = CALLOC(1, sizeof(*(ue_context_pP->ue_context.handover_info)));
+    //memcpy((void *)rrc_inst->handover_info[ue_mod_idP]->as_config.sourceRadioResourceConfig.srb_ToAddModList,(void *)SRB_list,sizeof(SRB_ToAddModList_t));
+    ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.srb_ToAddModList = *SRB_configList2;
+    //memcpy((void *)rrc_inst->handover_info[ue_mod_idP]->as_config.sourceRadioResourceConfig.drb_ToAddModList,(void *)DRB_list,sizeof(DRB_ToAddModList_t));
+    ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.drb_ToAddModList = *DRB_configList;
+    ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.drb_ToReleaseList = NULL;
+    ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.mac_MainConfig =
+      CALLOC(1, sizeof(*ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.mac_MainConfig));
+    memcpy((void*)ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.mac_MainConfig,
+           (void *)mac_MainConfig, sizeof(MAC_MainConfig_t));
+    ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.physicalConfigDedicated =
+      CALLOC(1, sizeof(PhysicalConfigDedicated_t));
+    memcpy((void*)ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.physicalConfigDedicated,
+           (void*)ue_context_pP->ue_context.physicalConfigDedicated, sizeof(PhysicalConfigDedicated_t));
+    ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.sps_Config = NULL;
+    //memcpy((void *)rrc_inst->handover_info[ue_mod_idP]->as_config.sourceRadioResourceConfig.sps_Config,(void *)rrc_inst->sps_Config[ue_mod_idP],sizeof(SPS_Config_t));
+
+  }
+
+#if defined(ENABLE_ITTI)
+  /* Initialize NAS list */
+  dedicatedInfoNASList = CALLOC(1, sizeof(struct RRCConnectionReconfiguration_r8_IEs__dedicatedInfoNASList));
+
+  /* Add all NAS PDUs to the list */
+  for (i = 0; i < ue_context_pP->ue_context.nb_of_e_rabs; i++) {
+    if (ue_context_pP->ue_context.e_rab[i].param.nas_pdu.buffer != NULL) {
+      dedicatedInfoNas = CALLOC(1, sizeof(DedicatedInfoNAS_t));
+      memset(dedicatedInfoNas, 0, sizeof(OCTET_STRING_t));
+      OCTET_STRING_fromBuf(dedicatedInfoNas, 
+			   (char*)ue_context_pP->ue_context.e_rab[i].param.nas_pdu.buffer,
+                           ue_context_pP->ue_context.e_rab[i].param.nas_pdu.length);
+      ASN_SEQUENCE_ADD(&dedicatedInfoNASList->list, dedicatedInfoNas);
+    }
+
+    /* TODO parameters yet to process ... */
+    {
+      //      ue_context_pP->ue_context.e_rab[i].param.qos;
+      //      ue_context_pP->ue_context.e_rab[i].param.sgw_addr;
+      //      ue_context_pP->ue_context.e_rab[i].param.gtp_teid;
+    }
+
+    /* TODO should test if e RAB are Ok before! */
+    ue_context_pP->ue_context.e_rab[i].status = E_RAB_STATUS_DONE;
+    LOG_D(RRC, "setting the status for the default DRB (index %d) to (%d,%s)\n", 
+	  i, ue_context_pP->ue_context.e_rab[i].status, "E_RAB_STATUS_DONE");
+  }
+
+  /* If list is empty free the list and reset the address */
+  if (dedicatedInfoNASList->list.count == 0) {
+    free(dedicatedInfoNASList);
+    dedicatedInfoNASList = NULL;
+  }
+
+#endif
+
+  memset(buffer, 0, RRC_BUF_SIZE);
+
+  size = do_RRCConnectionReconfiguration(ctxt_pP,
+                                         buffer,
+                                         xid,   //Transaction_id,
+                                         (SRB_ToAddModList_t*)*SRB_configList2, // SRB_configList
+                                         (DRB_ToAddModList_t*)*DRB_configList,
+                                         (DRB_ToReleaseList_t*)NULL,  // DRB2_list,
+                                         (struct SPS_Config*)NULL,    // *sps_Config,
+                                         (struct PhysicalConfigDedicated*)*physicalConfigDedicated,
+#ifdef EXMIMO_IOT
+                                         NULL, NULL, NULL,NULL,
+#else
+                                         (MeasObjectToAddModList_t*)MeasObj_list,
+                                         (ReportConfigToAddModList_t*)ReportConfig_list,
+                                         (QuantityConfig_t*)quantityConfig,
+                                         (MeasIdToAddModList_t*)MeasId_list,
+#endif
+                                         (MAC_MainConfig_t*)mac_MainConfig,
+                                         (MeasGapConfig_t*)NULL,
+                                         (MobilityControlInfo_t*)NULL,
+                                         (struct MeasConfig__speedStatePars*)Sparams,
+                                         (RSRP_Range_t*)rsrp,
+                                         (C_RNTI_t*)cba_RNTI,
+                                         (struct RRCConnectionReconfiguration_r8_IEs__dedicatedInfoNASList*)dedicatedInfoNASList
+#if defined(Rel10) || defined(Rel14)
+                                         , (SCellToAddMod_r10_t*)NULL
+#endif
+                                        );
+
+#ifdef RRC_MSG_PRINT
+  LOG_F(RRC,"[MSG] RRC Connection Reconfiguration\n");
+  for (i = 0; i < size; i++) {
+    LOG_F(RRC,"%02x ", ((uint8_t*)buffer)[i]);
+  }
+  LOG_F(RRC,"\n");
+  ////////////////////////////////////////
+#endif
+
+#if defined(ENABLE_ITTI)
+
+  /* Free all NAS PDUs */
+  for (i = 0; i < ue_context_pP->ue_context.nb_of_e_rabs; i++) {
+    if (ue_context_pP->ue_context.e_rab[i].param.nas_pdu.buffer != NULL) {
+      /* Free the NAS PDU buffer and invalidate it */
+      free(ue_context_pP->ue_context.e_rab[i].param.nas_pdu.buffer);
+      ue_context_pP->ue_context.e_rab[i].param.nas_pdu.buffer = NULL;
+    }
+  }
+
+#endif
+
+  LOG_I(RRC,
+        "[eNB %d] Frame %d, Logical Channel DL-DCCH, Generate RRCConnectionReconfiguration (bytes %d, UE id %x)\n",
+        ctxt_pP->module_id, ctxt_pP->frame, size, ue_context_pP->ue_context.rnti);
+
+  LOG_D(RRC,
+        "[FRAME %05d][RRC_eNB][MOD %u][][--- PDCP_DATA_REQ/%d Bytes (rrcConnectionReconfiguration to UE %x MUI %d) --->][PDCP][MOD %u][RB %u]\n",
+        ctxt_pP->frame, ctxt_pP->module_id, size, ue_context_pP->ue_context.rnti, rrc_eNB_mui, ctxt_pP->module_id, DCCH);
+
+  MSC_LOG_TX_MESSAGE(
+    MSC_RRC_ENB,
+    MSC_RRC_UE,
+    buffer,
+    size,
+    MSC_AS_TIME_FMT" rrcConnectionReconfiguration UE %x MUI %d size %u",
+    MSC_AS_TIME_ARGS(ctxt_pP),
+    ue_context_pP->ue_context.rnti,
+    rrc_eNB_mui,
+    size);
+
+  rrc_data_req(
+	       ctxt_pP,
+	       DCCH,
+	       rrc_eNB_mui++,
+	       SDU_CONFIRM_NO,
+	       size,
+	       buffer,
+	       PDCP_TRANSMISSION_MODE_CONTROL);
+}
+
+//-----------------------------------------------------------------------------
+void
+flexran_rrc_eNB_generate_defaultRRCConnectionReconfiguration(const protocol_ctxt_t* const ctxt_pP,
+                 rrc_eNB_ue_context_t*          const ue_context_pP,
+                 const uint8_t                ho_state,
+                 agent_reconf_rrc * trig_param
+                 )
+//-----------------------------------------------------------------------------
+{
+  uint8_t                             buffer[RRC_BUF_SIZE];
+  uint16_t                            size;
+  int                                 i;
+ 
+  // configure SRB1/SRB2, PhysicalConfigDedicated, MAC_MainConfig for UE
+  eNB_RRC_INST*                       rrc_inst = RC.rrc[ctxt_pP->module_id];
+  struct PhysicalConfigDedicated**    physicalConfigDedicated = &ue_context_pP->ue_context.physicalConfigDedicated;
+
+  struct SRB_ToAddMod                *SRB2_config                      = NULL;
+  struct SRB_ToAddMod__rlc_Config    *SRB2_rlc_config                  = NULL;
+  struct SRB_ToAddMod__logicalChannelConfig *SRB2_lchan_config         = NULL;
+  struct LogicalChannelConfig__ul_SpecificParameters
+      *SRB2_ul_SpecificParameters       = NULL;
+  SRB_ToAddModList_t*                 SRB_configList = ue_context_pP->ue_context.SRB_configList;
+  SRB_ToAddModList_t                 **SRB_configList2                  = NULL;
+
+  struct DRB_ToAddMod                *DRB_config                       = NULL;
+  struct RLC_Config                  *DRB_rlc_config                   = NULL;
+  struct PDCP_Config                 *DRB_pdcp_config                  = NULL;
+  struct PDCP_Config__rlc_AM         *PDCP_rlc_AM                      = NULL;
+  struct PDCP_Config__rlc_UM         *PDCP_rlc_UM                      = NULL;
+  struct LogicalChannelConfig        *DRB_lchan_config                 = NULL;
+  struct LogicalChannelConfig__ul_SpecificParameters
+      *DRB_ul_SpecificParameters        = NULL;
+  DRB_ToAddModList_t**                DRB_configList = &ue_context_pP->ue_context.DRB_configList;
+  DRB_ToAddModList_t**                DRB_configList2 = NULL;
+   MAC_MainConfig_t                   *mac_MainConfig                   = NULL;
+  MeasObjectToAddModList_t           *MeasObj_list                     = NULL;
+  MeasObjectToAddMod_t               *MeasObj                          = NULL;
+  ReportConfigToAddModList_t         *ReportConfig_list                = NULL;
+  ReportConfigToAddMod_t             *ReportConfig_per;//, *ReportConfig_A1,
+                                     // *ReportConfig_A2, *ReportConfig_A3, *ReportConfig_A4, *ReportConfig_A5;
+  MeasIdToAddModList_t               *MeasId_list                      = NULL;
+  MeasIdToAddMod_t                   *MeasId0; //, *MeasId1, *MeasId2, *MeasId3, *MeasId4, *MeasId5;
+#if Rel10
+  long                               *sr_ProhibitTimer_r9              = NULL;
+  //     uint8_t sCellIndexToAdd = rrc_find_free_SCell_index(enb_mod_idP, ue_mod_idP, 1);
+  //uint8_t                            sCellIndexToAdd = 0;
+#endif
+
+  long                               *logicalchannelgroup, *logicalchannelgroup_drb;
+  long                               *maxHARQ_Tx, *periodicBSR_Timer;
+
+  RSRP_Range_t                       *rsrp                             = NULL;
+  struct MeasConfig__speedStatePars  *Sparams                          = NULL;
+  QuantityConfig_t                   *quantityConfig                   = NULL;
+  CellsToAddMod_t                    *CellToAdd                        = NULL;
+  CellsToAddModList_t                *CellsToAddModList                = NULL;
+  struct RRCConnectionReconfiguration_r8_IEs__dedicatedInfoNASList *dedicatedInfoNASList = NULL;
+  DedicatedInfoNAS_t                 *dedicatedInfoNas                 = NULL;
+  /* for no gcc warnings */
+  (void)dedicatedInfoNas;
+
+  C_RNTI_t                           *cba_RNTI                         = NULL;
+
+  uint8_t xid = rrc_eNB_get_next_transaction_identifier(ctxt_pP->module_id);   //Transaction_id,
+
+#ifdef CBA
+  //struct PUSCH_CBAConfigDedicated_vlola  *pusch_CBAConfigDedicated_vlola;
+  uint8_t                            *cba_RNTI_buf;
+  cba_RNTI = CALLOC(1, sizeof(C_RNTI_t));
+  cba_RNTI_buf = CALLOC(1, 2 * sizeof(uint8_t));
+  cba_RNTI->buf = cba_RNTI_buf;
+  cba_RNTI->size = 2;
+  cba_RNTI->bits_unused = 0;
+
+  // associate UEs to the CBa groups as a function of their UE id
+  if (rrc_inst->num_active_cba_groups) {
+    cba_RNTI->buf[0] = rrc_inst->cba_rnti[ue_mod_idP % rrc_inst->num_active_cba_groups] & 0xff;
+    cba_RNTI->buf[1] = 0xff;
+    LOG_D(RRC,
+          "[eNB %d] Frame %d: cba_RNTI = %x in group %d is attribued to UE %d\n",
+          enb_mod_idP, frameP,
+          rrc_inst->cba_rnti[ue_mod_idP % rrc_inst->num_active_cba_groups],
+          ue_mod_idP % rrc_inst->num_active_cba_groups, ue_mod_idP);
+  } else {
+    cba_RNTI->buf[0] = 0x0;
+    cba_RNTI->buf[1] = 0x0;
+    LOG_D(RRC, "[eNB %d] Frame %d: no cba_RNTI is configured for UE %d\n", enb_mod_idP, frameP, ue_mod_idP);
+  }
+
+#endif
+
+  T(T_ENB_RRC_CONNECTION_RECONFIGURATION, T_INT(ctxt_pP->module_id), T_INT(ctxt_pP->frame),
+    T_INT(ctxt_pP->subframe), T_INT(ctxt_pP->rnti));
+
+  // Configure SRB2
+  /// SRB2
+  SRB_configList2=&ue_context_pP->ue_context.SRB_configList2[xid];
+  if (*SRB_configList2) {
+    free(*SRB_configList2);
+  }
+  *SRB_configList2 = CALLOC(1, sizeof(**SRB_configList2));
+  memset(*SRB_configList2, 0, sizeof(**SRB_configList2));
+  SRB2_config = CALLOC(1, sizeof(*SRB2_config));
+
+  SRB2_config->srb_Identity = 2;
+  SRB2_rlc_config = CALLOC(1, sizeof(*SRB2_rlc_config));
+  SRB2_config->rlc_Config = SRB2_rlc_config;
+
+  SRB2_rlc_config->present = SRB_ToAddMod__rlc_Config_PR_explicitValue;
+  SRB2_rlc_config->choice.explicitValue.present = RLC_Config_PR_am;
+  SRB2_rlc_config->choice.explicitValue.choice.am.ul_AM_RLC.t_PollRetransmit = T_PollRetransmit_ms15;
+  SRB2_rlc_config->choice.explicitValue.choice.am.ul_AM_RLC.pollPDU = PollPDU_p8;
+  SRB2_rlc_config->choice.explicitValue.choice.am.ul_AM_RLC.pollByte = PollByte_kB1000;
+  SRB2_rlc_config->choice.explicitValue.choice.am.ul_AM_RLC.maxRetxThreshold = UL_AM_RLC__maxRetxThreshold_t32;
+  SRB2_rlc_config->choice.explicitValue.choice.am.dl_AM_RLC.t_Reordering = T_Reordering_ms35;
+  SRB2_rlc_config->choice.explicitValue.choice.am.dl_AM_RLC.t_StatusProhibit = T_StatusProhibit_ms10;
+
+  SRB2_lchan_config = CALLOC(1, sizeof(*SRB2_lchan_config));
+  SRB2_config->logicalChannelConfig = SRB2_lchan_config;
+
+  SRB2_lchan_config->present = SRB_ToAddMod__logicalChannelConfig_PR_explicitValue;
+
+  SRB2_ul_SpecificParameters = CALLOC(1, sizeof(*SRB2_ul_SpecificParameters));
+
+  SRB2_ul_SpecificParameters->priority = 3; // let some priority for SRB1 and dedicated DRBs
+  SRB2_ul_SpecificParameters->prioritisedBitRate =
+    LogicalChannelConfig__ul_SpecificParameters__prioritisedBitRate_infinity;
+  SRB2_ul_SpecificParameters->bucketSizeDuration =
+    LogicalChannelConfig__ul_SpecificParameters__bucketSizeDuration_ms50;
+
+  // LCG for CCCH and DCCH is 0 as defined in 36331
+  logicalchannelgroup = CALLOC(1, sizeof(long));
+  *logicalchannelgroup = 0;
+
+  SRB2_ul_SpecificParameters->logicalChannelGroup = logicalchannelgroup;
+
+  SRB2_lchan_config->choice.explicitValue.ul_SpecificParameters = SRB2_ul_SpecificParameters;
+  // this list has the configuration for SRB1 and SRB2
+  ASN_SEQUENCE_ADD(&SRB_configList->list, SRB2_config);
+  // this list has only the configuration for SRB2
+  ASN_SEQUENCE_ADD(&(*SRB_configList2)->list, SRB2_config);
+
+  // Configure DRB
+  //*DRB_configList = CALLOC(1, sizeof(*DRB_configList));
+  // list for all the configured DRB
+  if (*DRB_configList) {
+    free(*DRB_configList);
+  }
+  *DRB_configList = CALLOC(1, sizeof(**DRB_configList));
+  memset(*DRB_configList, 0, sizeof(**DRB_configList));
+
+  // list for the configured DRB for a this xid
+  DRB_configList2=&ue_context_pP->ue_context.DRB_configList2[xid];
+  if (*DRB_configList2) {
+    free(*DRB_configList2);
+  }
+  *DRB_configList2 = CALLOC(1, sizeof(**DRB_configList2));
+  memset(*DRB_configList2, 0, sizeof(**DRB_configList2));
+
+
+  /// DRB
+  DRB_config = CALLOC(1, sizeof(*DRB_config));
+
+  DRB_config->eps_BearerIdentity = CALLOC(1, sizeof(long));
+  *(DRB_config->eps_BearerIdentity) = 5L; // LW set to first value, allowed value 5..15, value : x+4
+  // DRB_config->drb_Identity = (DRB_Identity_t) 1; //allowed values 1..32
+  // NN: this is the 1st DRB for this ue, so set it to 1
+  DRB_config->drb_Identity = (DRB_Identity_t) 1;  // (ue_mod_idP+1); //allowed values 1..32, value: x
+  DRB_config->logicalChannelIdentity = CALLOC(1, sizeof(long));
+  *(DRB_config->logicalChannelIdentity) = (long)3; // value : x+2
+  DRB_rlc_config = CALLOC(1, sizeof(*DRB_rlc_config));
+  DRB_config->rlc_Config = DRB_rlc_config;
+
+#ifdef RRC_DEFAULT_RAB_IS_AM
+  DRB_rlc_config->present = RLC_Config_PR_am;
+  DRB_rlc_config->choice.am.ul_AM_RLC.t_PollRetransmit = T_PollRetransmit_ms50;
+  DRB_rlc_config->choice.am.ul_AM_RLC.pollPDU = PollPDU_p16;
+  DRB_rlc_config->choice.am.ul_AM_RLC.pollByte = PollByte_kBinfinity;
+  DRB_rlc_config->choice.am.ul_AM_RLC.maxRetxThreshold = UL_AM_RLC__maxRetxThreshold_t8;
+  DRB_rlc_config->choice.am.dl_AM_RLC.t_Reordering = T_Reordering_ms35;
+  DRB_rlc_config->choice.am.dl_AM_RLC.t_StatusProhibit = T_StatusProhibit_ms25;
+#else
+  DRB_rlc_config->present = RLC_Config_PR_um_Bi_Directional;
+  DRB_rlc_config->choice.um_Bi_Directional.ul_UM_RLC.sn_FieldLength = SN_FieldLength_size10;
+  DRB_rlc_config->choice.um_Bi_Directional.dl_UM_RLC.sn_FieldLength = SN_FieldLength_size10;
+#ifdef CBA
+  DRB_rlc_config->choice.um_Bi_Directional.dl_UM_RLC.t_Reordering   = T_Reordering_ms5;//T_Reordering_ms25;
+#else
+  DRB_rlc_config->choice.um_Bi_Directional.dl_UM_RLC.t_Reordering = T_Reordering_ms35;
+#endif
+#endif
+
+  DRB_pdcp_config = CALLOC(1, sizeof(*DRB_pdcp_config));
+  DRB_config->pdcp_Config = DRB_pdcp_config;
+  DRB_pdcp_config->discardTimer = CALLOC(1, sizeof(long));
+  *DRB_pdcp_config->discardTimer = PDCP_Config__discardTimer_infinity;
+  DRB_pdcp_config->rlc_AM = NULL;
+  DRB_pdcp_config->rlc_UM = NULL;
+
+  /* avoid gcc warnings */
+  (void)PDCP_rlc_AM;
+  (void)PDCP_rlc_UM;
+
+#ifdef RRC_DEFAULT_RAB_IS_AM // EXMIMO_IOT
+  PDCP_rlc_AM = CALLOC(1, sizeof(*PDCP_rlc_AM));
+  DRB_pdcp_config->rlc_AM = PDCP_rlc_AM;
+  PDCP_rlc_AM->statusReportRequired = FALSE;
+#else
+  PDCP_rlc_UM = CALLOC(1, sizeof(*PDCP_rlc_UM));
+  DRB_pdcp_config->rlc_UM = PDCP_rlc_UM;
+  PDCP_rlc_UM->pdcp_SN_Size = PDCP_Config__rlc_UM__pdcp_SN_Size_len12bits;
+#endif
+  DRB_pdcp_config->headerCompression.present = PDCP_Config__headerCompression_PR_notUsed;
+
+  DRB_lchan_config = CALLOC(1, sizeof(*DRB_lchan_config));
+  DRB_config->logicalChannelConfig = DRB_lchan_config;
+  DRB_ul_SpecificParameters = CALLOC(1, sizeof(*DRB_ul_SpecificParameters));
+  DRB_lchan_config->ul_SpecificParameters = DRB_ul_SpecificParameters;
+
+  DRB_ul_SpecificParameters->priority = 12;    // lower priority than srb1, srb2 and other dedicated bearer
+  DRB_ul_SpecificParameters->prioritisedBitRate =LogicalChannelConfig__ul_SpecificParameters__prioritisedBitRate_kBps8 ;
+    //LogicalChannelConfig__ul_SpecificParameters__prioritisedBitRate_infinity;
+  DRB_ul_SpecificParameters->bucketSizeDuration =
+    LogicalChannelConfig__ul_SpecificParameters__bucketSizeDuration_ms50;
+
+  // LCG for DTCH can take the value from 1 to 3 as defined in 36331: normally controlled by upper layers (like RRM)
+  logicalchannelgroup_drb = CALLOC(1, sizeof(long));
+  *logicalchannelgroup_drb = 1;
+  DRB_ul_SpecificParameters->logicalChannelGroup = logicalchannelgroup_drb;
+
+  ASN_SEQUENCE_ADD(&(*DRB_configList)->list, DRB_config);
+  ASN_SEQUENCE_ADD(&(*DRB_configList2)->list, DRB_config);
+
+  //ue_context_pP->ue_context.DRB_configList2[0] = &(*DRB_configList);
+
+  mac_MainConfig = CALLOC(1, sizeof(*mac_MainConfig));
+  // ue_context_pP->ue_context.mac_MainConfig = mac_MainConfig;
+
+  mac_MainConfig->ul_SCH_Config = CALLOC(1, sizeof(*mac_MainConfig->ul_SCH_Config));
+
+  maxHARQ_Tx = CALLOC(1, sizeof(long));
+  *maxHARQ_Tx = MAC_MainConfig__ul_SCH_Config__maxHARQ_Tx_n5;
+  mac_MainConfig->ul_SCH_Config->maxHARQ_Tx = maxHARQ_Tx;
+  periodicBSR_Timer = CALLOC(1, sizeof(long));
+  *periodicBSR_Timer = PeriodicBSR_Timer_r12_sf64;
+  mac_MainConfig->ul_SCH_Config->periodicBSR_Timer = periodicBSR_Timer;
+  mac_MainConfig->ul_SCH_Config->retxBSR_Timer = RetxBSR_Timer_r12_sf320;
+  mac_MainConfig->ul_SCH_Config->ttiBundling = 0; // FALSE
+
+  mac_MainConfig->timeAlignmentTimerDedicated = TimeAlignmentTimer_infinity;
+
+  mac_MainConfig->drx_Config = NULL;
+
+  mac_MainConfig->phr_Config = CALLOC(1, sizeof(*mac_MainConfig->phr_Config));
+
+  mac_MainConfig->phr_Config->present = MAC_MainConfig__phr_Config_PR_setup;
+  mac_MainConfig->phr_Config->choice.setup.periodicPHR_Timer = MAC_MainConfig__phr_Config__setup__periodicPHR_Timer_sf20; // sf20 = 20 subframes
+
+  mac_MainConfig->phr_Config->choice.setup.prohibitPHR_Timer = MAC_MainConfig__phr_Config__setup__prohibitPHR_Timer_sf20; // sf20 = 20 subframes
+
+  mac_MainConfig->phr_Config->choice.setup.dl_PathlossChange = MAC_MainConfig__phr_Config__setup__dl_PathlossChange_dB1;  // Value dB1 =1 dB, dB3 = 3 dB
+
+#ifdef Rel10
+  sr_ProhibitTimer_r9 = CALLOC(1, sizeof(long));
+  *sr_ProhibitTimer_r9 = 0;   // SR tx on PUCCH, Value in number of SR period(s). Value 0 = no timer for SR, Value 2= 2*SR
+  mac_MainConfig->ext1 = CALLOC(1, sizeof(struct MAC_MainConfig__ext1));
+  mac_MainConfig->ext1->sr_ProhibitTimer_r9 = sr_ProhibitTimer_r9;
+  //sps_RA_ConfigList_rlola = NULL;
+#endif
+
+  //change the transmission mode for the primary component carrier
+  //TODO: add codebook subset restriction here
+  //TODO: change TM for secondary CC in SCelltoaddmodlist
+  if (*physicalConfigDedicated) {
+    if ((*physicalConfigDedicated)->antennaInfo) {
+      (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.transmissionMode = rrc_inst->configuration.ue_TransmissionMode[0];
+      LOG_D(RRC,"Setting transmission mode to %ld+1\n",rrc_inst->configuration.ue_TransmissionMode[0]);
+      if (rrc_inst->configuration.ue_TransmissionMode[0]==AntennaInfoDedicated__transmissionMode_tm3) {
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction=     
+    CALLOC(1,sizeof(AntennaInfoDedicated__codebookSubsetRestriction_PR));
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->present =
+    AntennaInfoDedicated__codebookSubsetRestriction_PR_n2TxAntenna_tm3;
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm3.buf= MALLOC(1);
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm3.buf[0] = 0xc0;
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm3.size=1;
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm3.bits_unused=6;
+      }
+      else if (rrc_inst->configuration.ue_TransmissionMode[0]==AntennaInfoDedicated__transmissionMode_tm4) {
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction=     
+    CALLOC(1,sizeof(AntennaInfoDedicated__codebookSubsetRestriction_PR));
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->present =
+    AntennaInfoDedicated__codebookSubsetRestriction_PR_n2TxAntenna_tm4;
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm4.buf= MALLOC(1);
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm4.buf[0] = 0xfc;
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm4.size=1;
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm4.bits_unused=2;
+
+      }
+      else if (rrc_inst->configuration.ue_TransmissionMode[0]==AntennaInfoDedicated__transmissionMode_tm5) {
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction=     
+    CALLOC(1,sizeof(AntennaInfoDedicated__codebookSubsetRestriction_PR));
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->present =
+    AntennaInfoDedicated__codebookSubsetRestriction_PR_n2TxAntenna_tm5;
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm5.buf= MALLOC(1);
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm5.buf[0] = 0xf0;
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm5.size=1;
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm5.bits_unused=4;
+      }
+      else if (rrc_inst->configuration.ue_TransmissionMode[0]==AntennaInfoDedicated__transmissionMode_tm6) {
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction=     
+    CALLOC(1,sizeof(AntennaInfoDedicated__codebookSubsetRestriction_PR));
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->present =
+    AntennaInfoDedicated__codebookSubsetRestriction_PR_n2TxAntenna_tm6;
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm6.buf= MALLOC(1);
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm6.buf[0] = 0xf0;
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm6.size=1;
+  (*physicalConfigDedicated)->antennaInfo->choice.explicitValue.codebookSubsetRestriction->choice.n2TxAntenna_tm6.bits_unused=4;
+      }
+    }
+    else {
+      LOG_E(RRC,"antenna_info not present in physical_config_dedicated. Not reconfiguring!\n");
+    }
+    if ((*physicalConfigDedicated)->cqi_ReportConfig) {
+      if ((rrc_inst->configuration.ue_TransmissionMode[0]==AntennaInfoDedicated__transmissionMode_tm4) ||
+    (rrc_inst->configuration.ue_TransmissionMode[0]==AntennaInfoDedicated__transmissionMode_tm5) ||
+    (rrc_inst->configuration.ue_TransmissionMode[0]==AntennaInfoDedicated__transmissionMode_tm6)) {
+  //feedback mode needs to be set as well
+  //TODO: I think this is taken into account in the PHY automatically based on the transmission mode variable
+  printf("setting cqi reporting mode to rm31\n");
+#if defined(Rel10) || defined(Rel14)
+  *((*physicalConfigDedicated)->cqi_ReportConfig->cqi_ReportModeAperiodic)=CQI_ReportModeAperiodic_rm31;
+#else
+  *((*physicalConfigDedicated)->cqi_ReportConfig->cqi_ReportModeAperiodic)=CQI_ReportConfig__cqi_ReportModeAperiodic_rm31; // HLC CQI, no PMI
+#endif
+      }
+    }
+    else {
+      LOG_E(RRC,"cqi_ReportConfig not present in physical_config_dedicated. Not reconfiguring!\n");
+    }
+  }
+  else {
+    LOG_E(RRC,"physical_config_dedicated not present in RRCConnectionReconfiguration. Not reconfiguring!\n");
+  }
+
+  // Measurement ID list
+  MeasId_list = CALLOC(1, sizeof(*MeasId_list));
+  memset((void *)MeasId_list, 0, sizeof(*MeasId_list));
 
-  MeasId5 = CALLOC(1, sizeof(*MeasId5));
-  MeasId5->measId = 6;
-  MeasId5->measObjectId = 1;
-  MeasId5->reportConfigId = 6;
-  ASN_SEQUENCE_ADD(&MeasId_list->list, MeasId5);
+  MeasId0 = CALLOC(1, sizeof(*MeasId0));
+  MeasId0->measId = 1;
+  MeasId0->measObjectId = 1;
+  MeasId0->reportConfigId = 1;
+  ASN_SEQUENCE_ADD(&MeasId_list->list, MeasId0);
 
-  //  rrcConnectionReconfiguration->criticalExtensions.choice.c1.choice.rrcConnectionReconfiguration_r8.measConfig->measIdToAddModList = MeasId_list;
+  /*
+   * Add one EUTRA Measurement Object
+  */
 
-  // Add one EUTRA Measurement Object
   MeasObj_list = CALLOC(1, sizeof(*MeasObj_list));
   memset((void *)MeasObj_list, 0, sizeof(*MeasObj_list));
 
-  // Configure MeasObject
+  // Configure MeasObject 
 
   MeasObj = CALLOC(1, sizeof(*MeasObj));
   memset((void *)MeasObj, 0, sizeof(*MeasObj));
@@ -3052,155 +3757,65 @@ rrc_eNB_generate_defaultRRCConnectionReconfiguration(const protocol_ctxt_t* cons
   //  rrcConnectionReconfiguration->criticalExtensions.choice.c1.choice.rrcConnectionReconfiguration_r8.measConfig->measObjectToAddModList = MeasObj_list;
 
   // Report Configurations for periodical, A1-A5 events
-  ReportConfig_list = CALLOC(1, sizeof(*ReportConfig_list));
 
-  ReportConfig_per = CALLOC(1, sizeof(*ReportConfig_per));
+  /* RRC Strategy Measurement */
 
-  ReportConfig_A1 = CALLOC(1, sizeof(*ReportConfig_A1));
 
-  ReportConfig_A2 = CALLOC(1, sizeof(*ReportConfig_A2));
+  if (strcmp("one_shot", trig_param->trigger_policy) == 0){
 
-  ReportConfig_A3 = CALLOC(1, sizeof(*ReportConfig_A3));
+      trig_param->report_interval = 0;
+      trig_param->report_amount = 0;
 
-  ReportConfig_A4 = CALLOC(1, sizeof(*ReportConfig_A4));
+  }
 
-  ReportConfig_A5 = CALLOC(1, sizeof(*ReportConfig_A5));
+  else if (strcmp("event_driven", trig_param->trigger_policy) == 0){
 
-  ReportConfig_per->reportConfigId = 1;
-  ReportConfig_per->reportConfig.present = ReportConfigToAddMod__reportConfig_PR_reportConfigEUTRA;
-  ReportConfig_per->reportConfig.choice.reportConfigEUTRA.triggerType.present =
-    ReportConfigEUTRA__triggerType_PR_periodical;
-  ReportConfig_per->reportConfig.choice.reportConfigEUTRA.triggerType.choice.periodical.purpose =
-    ReportConfigEUTRA__triggerType__periodical__purpose_reportStrongestCells;
-  ReportConfig_per->reportConfig.choice.reportConfigEUTRA.triggerQuantity = ReportConfigEUTRA__triggerQuantity_rsrp;
-  ReportConfig_per->reportConfig.choice.reportConfigEUTRA.reportQuantity = ReportConfigEUTRA__reportQuantity_both;
-  ReportConfig_per->reportConfig.choice.reportConfigEUTRA.maxReportCells = 2;
-  ReportConfig_per->reportConfig.choice.reportConfigEUTRA.reportInterval = ReportInterval_ms120;
-  ReportConfig_per->reportConfig.choice.reportConfigEUTRA.reportAmount = ReportConfigEUTRA__reportAmount_infinity;
+      trig_param->report_interval = 6;
+      trig_param->report_amount = 2;
 
-  ASN_SEQUENCE_ADD(&ReportConfig_list->list, ReportConfig_per);
+  }
 
-  ReportConfig_A1->reportConfigId = 2;
-  ReportConfig_A1->reportConfig.present = ReportConfigToAddMod__reportConfig_PR_reportConfigEUTRA;
-  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.triggerType.present =
-    ReportConfigEUTRA__triggerType_PR_event;
-  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.present =
-    ReportConfigEUTRA__triggerType__event__eventId_PR_eventA1;
-  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.eventA1.
-  a1_Threshold.present = ThresholdEUTRA_PR_threshold_RSRP;
-  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.eventA1.
-  a1_Threshold.choice.threshold_RSRP = 10;
+  else if (strcmp("periodical", trig_param->trigger_policy) == 0){
 
-  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.triggerQuantity = ReportConfigEUTRA__triggerQuantity_rsrp;
-  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.reportQuantity = ReportConfigEUTRA__reportQuantity_both;
-  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.maxReportCells = 2;
-  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.reportInterval = ReportInterval_ms120;
-  ReportConfig_A1->reportConfig.choice.reportConfigEUTRA.reportAmount = ReportConfigEUTRA__reportAmount_infinity;
+      trig_param->report_interval = 1;
+      trig_param->report_amount = 7;
 
-  ASN_SEQUENCE_ADD(&ReportConfig_list->list, ReportConfig_A1);
+  }
 
-  if (ho_state == 1 /*HO_MEASURMENT */ ) {
-    LOG_I(RRC, "[eNB %d] frame %d: requesting A2, A3, A4, A5, and A6 event reporting\n",
-          ctxt_pP->module_id, ctxt_pP->frame);
-    ReportConfig_A2->reportConfigId = 3;
-    ReportConfig_A2->reportConfig.present = ReportConfigToAddMod__reportConfig_PR_reportConfigEUTRA;
-    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.triggerType.present =
-      ReportConfigEUTRA__triggerType_PR_event;
-    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.present =
-      ReportConfigEUTRA__triggerType__event__eventId_PR_eventA2;
-    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
-    eventA2.a2_Threshold.present = ThresholdEUTRA_PR_threshold_RSRP;
-    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
-    eventA2.a2_Threshold.choice.threshold_RSRP = 10;
+  else {
 
-    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.triggerQuantity =
-      ReportConfigEUTRA__triggerQuantity_rsrp;
-    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.reportQuantity = ReportConfigEUTRA__reportQuantity_both;
-    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.maxReportCells = 2;
-    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.reportInterval = ReportInterval_ms120;
-    ReportConfig_A2->reportConfig.choice.reportConfigEUTRA.reportAmount = ReportConfigEUTRA__reportAmount_infinity;
+     LOG_E(FLEXRAN_AGENT, "There is something wrong on RRC agent!");
+  }
 
-    ASN_SEQUENCE_ADD(&ReportConfig_list->list, ReportConfig_A2);
 
-    ReportConfig_A3->reportConfigId = 4;
-    ReportConfig_A3->reportConfig.present = ReportConfigToAddMod__reportConfig_PR_reportConfigEUTRA;
-    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.triggerType.present =
-      ReportConfigEUTRA__triggerType_PR_event;
-    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.present =
-      ReportConfigEUTRA__triggerType__event__eventId_PR_eventA3;
 
-    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.eventA3.a3_Offset = 1;   //10;
-    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
-    eventA3.reportOnLeave = 1;
+  ReportConfig_list = CALLOC(1, sizeof(*ReportConfig_list));
 
-    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.triggerQuantity =
-      ReportConfigEUTRA__triggerQuantity_rsrp;
-    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.reportQuantity = ReportConfigEUTRA__reportQuantity_both;
-    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.maxReportCells = 2;
-    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.reportInterval = ReportInterval_ms120;
-    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.reportAmount = ReportConfigEUTRA__reportAmount_infinity;
+  ReportConfig_per = CALLOC(1, sizeof(*ReportConfig_per));
 
-    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.hysteresis = 0.5; // FIXME ...hysteresis is of type long!
-    ReportConfig_A3->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.timeToTrigger =
-      TimeToTrigger_ms40;
-    ASN_SEQUENCE_ADD(&ReportConfig_list->list, ReportConfig_A3);
+    // Periodical Measurement Report
 
-    ReportConfig_A4->reportConfigId = 5;
-    ReportConfig_A4->reportConfig.present = ReportConfigToAddMod__reportConfig_PR_reportConfigEUTRA;
-    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.triggerType.present =
-      ReportConfigEUTRA__triggerType_PR_event;
-    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.present =
-      ReportConfigEUTRA__triggerType__event__eventId_PR_eventA4;
-    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
-    eventA4.a4_Threshold.present = ThresholdEUTRA_PR_threshold_RSRP;
-    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
-    eventA4.a4_Threshold.choice.threshold_RSRP = 10;
+  ReportConfig_per->reportConfigId = 1;
+  ReportConfig_per->reportConfig.present = ReportConfigToAddMod__reportConfig_PR_reportConfigEUTRA;
 
-    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.triggerQuantity =
-      ReportConfigEUTRA__triggerQuantity_rsrp;
-    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.reportQuantity = ReportConfigEUTRA__reportQuantity_both;
-    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.maxReportCells = 2;
-    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.reportInterval = ReportInterval_ms120;
-    ReportConfig_A4->reportConfig.choice.reportConfigEUTRA.reportAmount = ReportConfigEUTRA__reportAmount_infinity;
+    ReportConfig_per->reportConfig.choice.reportConfigEUTRA.triggerType.present =
+      ReportConfigEUTRA__triggerType_PR_periodical;
 
-    ASN_SEQUENCE_ADD(&ReportConfig_list->list, ReportConfig_A4);
+    ReportConfig_per->reportConfig.choice.reportConfigEUTRA.triggerType.choice.periodical.purpose =
+      ReportConfigEUTRA__triggerType__periodical__purpose_reportStrongestCells;
 
-    ReportConfig_A5->reportConfigId = 6;
-    ReportConfig_A5->reportConfig.present = ReportConfigToAddMod__reportConfig_PR_reportConfigEUTRA;
-    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.triggerType.present =
-      ReportConfigEUTRA__triggerType_PR_event;
-    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.present =
-      ReportConfigEUTRA__triggerType__event__eventId_PR_eventA5;
-    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
-    eventA5.a5_Threshold1.present = ThresholdEUTRA_PR_threshold_RSRP;
-    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
-    eventA5.a5_Threshold2.present = ThresholdEUTRA_PR_threshold_RSRP;
-    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
-    eventA5.a5_Threshold1.choice.threshold_RSRP = 10;
-    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.eventId.choice.
-    eventA5.a5_Threshold2.choice.threshold_RSRP = 10;
+    // ReportConfig_per->reportConfig.choice.reportConfigEUTRA.triggerType.choice.event.timeToTrigger = TimeToTrigger_ms40;  
+    ReportConfig_per->reportConfig.choice.reportConfigEUTRA.triggerQuantity = ReportConfigEUTRA__triggerQuantity_rsrp;
+   ReportConfig_per->reportConfig.choice.reportConfigEUTRA.reportQuantity = ReportConfigEUTRA__reportQuantity_both;
+   ReportConfig_per->reportConfig.choice.reportConfigEUTRA.maxReportCells = 2;
+   ReportConfig_per->reportConfig.choice.reportConfigEUTRA.reportInterval = trig_param->report_interval ;//ReportInterval_ms2048; // RRC counter frame- ms1024 is 1ms   
 
-    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.triggerQuantity =
-      ReportConfigEUTRA__triggerQuantity_rsrp;
-    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.reportQuantity = ReportConfigEUTRA__reportQuantity_both;
-    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.maxReportCells = 2;
-    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.reportInterval = ReportInterval_ms120;
-    ReportConfig_A5->reportConfig.choice.reportConfigEUTRA.reportAmount = ReportConfigEUTRA__reportAmount_infinity;
+   ReportConfig_per->reportConfig.choice.reportConfigEUTRA.reportAmount = trig_param->report_amount; //ReportConfigEUTRA__reportAmount_r2; // put r1 to see once, r2 for 2 times and ...
 
-    ASN_SEQUENCE_ADD(&ReportConfig_list->list, ReportConfig_A5);
-    //  rrcConnectionReconfiguration->criticalExtensions.choice.c1.choice.rrcConnectionReconfiguration_r8.measConfig->reportConfigToAddModList = ReportConfig_list;
 
-    rsrp = CALLOC(1, sizeof(RSRP_Range_t));
-    *rsrp = 20;
+  ASN_SEQUENCE_ADD(&ReportConfig_list->list, ReportConfig_per);
+
 
-    Sparams = CALLOC(1, sizeof(*Sparams));
-    Sparams->present = MeasConfig__speedStatePars_PR_setup;
-    Sparams->choice.setup.timeToTrigger_SF.sf_High = SpeedStateScaleFactors__sf_Medium_oDot75;
-    Sparams->choice.setup.timeToTrigger_SF.sf_Medium = SpeedStateScaleFactors__sf_High_oDot5;
-    Sparams->choice.setup.mobilityStateParameters.n_CellChangeHigh = 10;
-    Sparams->choice.setup.mobilityStateParameters.n_CellChangeMedium = 5;
-    Sparams->choice.setup.mobilityStateParameters.t_Evaluation = MobilityStateParameters__t_Evaluation_s60;
-    Sparams->choice.setup.mobilityStateParameters.t_HystNormal = MobilityStateParameters__t_HystNormal_s120;
 
     quantityConfig = CALLOC(1, sizeof(*quantityConfig));
     memset((void *)quantityConfig, 0, sizeof(*quantityConfig));
@@ -3216,30 +3831,7 @@ rrc_eNB_generate_defaultRRCConnectionReconfiguration(const protocol_ctxt_t* cons
     *quantityConfig->quantityConfigEUTRA->filterCoefficientRSRP = FilterCoefficient_fc4;
     *quantityConfig->quantityConfigEUTRA->filterCoefficientRSRQ = FilterCoefficient_fc4;
 
-    LOG_I(RRC,
-          "[eNB %d] Frame %d: potential handover preparation: store the information in an intermediate structure in case of failure\n",
-          ctxt_pP->module_id, ctxt_pP->frame);
-    // store the information in an intermediate structure for Hanodver management
-    //rrc_inst->handover_info.as_config.sourceRadioResourceConfig.srb_ToAddModList = CALLOC(1,sizeof());
-    ue_context_pP->ue_context.handover_info = CALLOC(1, sizeof(*(ue_context_pP->ue_context.handover_info)));
-    //memcpy((void *)rrc_inst->handover_info[ue_mod_idP]->as_config.sourceRadioResourceConfig.srb_ToAddModList,(void *)SRB_list,sizeof(SRB_ToAddModList_t));
-    ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.srb_ToAddModList = *SRB_configList2;
-    //memcpy((void *)rrc_inst->handover_info[ue_mod_idP]->as_config.sourceRadioResourceConfig.drb_ToAddModList,(void *)DRB_list,sizeof(DRB_ToAddModList_t));
-    ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.drb_ToAddModList = *DRB_configList;
-    ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.drb_ToReleaseList = NULL;
-    ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.mac_MainConfig =
-      CALLOC(1, sizeof(*ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.mac_MainConfig));
-    memcpy((void*)ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.mac_MainConfig,
-           (void *)mac_MainConfig, sizeof(MAC_MainConfig_t));
-    ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.physicalConfigDedicated =
-      CALLOC(1, sizeof(PhysicalConfigDedicated_t));
-    memcpy((void*)ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.physicalConfigDedicated,
-           (void*)ue_context_pP->ue_context.physicalConfigDedicated, sizeof(PhysicalConfigDedicated_t));
-    ue_context_pP->ue_context.handover_info->as_config.sourceRadioResourceConfig.sps_Config = NULL;
-    //memcpy((void *)rrc_inst->handover_info[ue_mod_idP]->as_config.sourceRadioResourceConfig.sps_Config,(void *)rrc_inst->sps_Config[ue_mod_idP],sizeof(SPS_Config_t));
-
-  }
-
+  
 #if defined(ENABLE_ITTI)
   /* Initialize NAS list */
   dedicatedInfoNASList = CALLOC(1, sizeof(struct RRCConnectionReconfiguration_r8_IEs__dedicatedInfoNASList));
@@ -3250,22 +3842,22 @@ rrc_eNB_generate_defaultRRCConnectionReconfiguration(const protocol_ctxt_t* cons
       dedicatedInfoNas = CALLOC(1, sizeof(DedicatedInfoNAS_t));
       memset(dedicatedInfoNas, 0, sizeof(OCTET_STRING_t));
       OCTET_STRING_fromBuf(dedicatedInfoNas, 
-			   (char*)ue_context_pP->ue_context.e_rab[i].param.nas_pdu.buffer,
+         (char*)ue_context_pP->ue_context.e_rab[i].param.nas_pdu.buffer,
                            ue_context_pP->ue_context.e_rab[i].param.nas_pdu.length);
       ASN_SEQUENCE_ADD(&dedicatedInfoNASList->list, dedicatedInfoNas);
     }
 
     /* TODO parameters yet to process ... */
-    {
+    // {
       //      ue_context_pP->ue_context.e_rab[i].param.qos;
       //      ue_context_pP->ue_context.e_rab[i].param.sgw_addr;
       //      ue_context_pP->ue_context.e_rab[i].param.gtp_teid;
-    }
+    // }
 
     /* TODO should test if e RAB are Ok before! */
     ue_context_pP->ue_context.e_rab[i].status = E_RAB_STATUS_DONE;
     LOG_D(RRC, "setting the status for the default DRB (index %d) to (%d,%s)\n", 
-	  i, ue_context_pP->ue_context.e_rab[i].status, "E_RAB_STATUS_DONE");
+    i, ue_context_pP->ue_context.e_rab[i].status, "E_RAB_STATUS_DONE");
   }
 
   /* If list is empty free the list and reset the address */
@@ -3277,23 +3869,23 @@ rrc_eNB_generate_defaultRRCConnectionReconfiguration(const protocol_ctxt_t* cons
 #endif
 
   memset(buffer, 0, RRC_BUF_SIZE);
-
+  
   size = do_RRCConnectionReconfiguration(ctxt_pP,
                                          buffer,
                                          xid,   //Transaction_id,
-                                         (SRB_ToAddModList_t*)*SRB_configList2, // SRB_configList
-                                         (DRB_ToAddModList_t*)*DRB_configList,
+                                         (SRB_ToAddModList_t*)NULL, // SRB_configList
+                                         (DRB_ToAddModList_t*)NULL,
                                          (DRB_ToReleaseList_t*)NULL,  // DRB2_list,
                                          (struct SPS_Config*)NULL,    // *sps_Config,
                                          (struct PhysicalConfigDedicated*)*physicalConfigDedicated,
-#ifdef EXMIMO_IOT
-                                         NULL, NULL, NULL,NULL,
-#else
+// #ifdef EXMIMO_IOT
+//                                          NULL, NULL, NULL,NULL,
+// #else
                                          (MeasObjectToAddModList_t*)MeasObj_list,
                                          (ReportConfigToAddModList_t*)ReportConfig_list,
                                          (QuantityConfig_t*)quantityConfig,
                                          (MeasIdToAddModList_t*)MeasId_list,
-#endif
+// #endif
                                          (MAC_MainConfig_t*)mac_MainConfig,
                                          (MeasGapConfig_t*)NULL,
                                          (MobilityControlInfo_t*)NULL,
@@ -3348,17 +3940,16 @@ rrc_eNB_generate_defaultRRCConnectionReconfiguration(const protocol_ctxt_t* cons
     size);
 
   rrc_data_req(
-	       ctxt_pP,
-	       DCCH,
-	       rrc_eNB_mui++,
-	       SDU_CONFIRM_NO,
-	       size,
-	       buffer,
-	       PDCP_TRANSMISSION_MODE_CONTROL);
+         ctxt_pP,
+         DCCH,
+         rrc_eNB_mui++,
+         SDU_CONFIRM_NO,
+         size,
+         buffer,
+         PDCP_TRANSMISSION_MODE_CONTROL);
 }
 
 
-
 //-----------------------------------------------------------------------------
 int
 rrc_eNB_generate_RRCConnectionReconfiguration_SCell(
@@ -3440,46 +4031,72 @@ rrc_eNB_generate_RRCConnectionReconfiguration_SCell(
 void
 rrc_eNB_process_MeasurementReport(
   const protocol_ctxt_t* const ctxt_pP,
-  rrc_eNB_ue_context_t*          const ue_context_pP,
+  rrc_eNB_ue_context_t*         ue_context_pP,
   const MeasResults_t*   const measResults2
 )
 //-----------------------------------------------------------------------------
 {
+  int i=0;
+  int neighboring_cells=-1;
+  
   T(T_ENB_RRC_MEASUREMENT_REPORT, T_INT(ctxt_pP->module_id), T_INT(ctxt_pP->frame),
     T_INT(ctxt_pP->subframe), T_INT(ctxt_pP->rnti));
 
+  if (measResults2 == NULL )
+    return;
+  
+  if (measResults2->measId > 0 ){
+     if (ue_context_pP->ue_context.measResults == NULL) {
+       ue_context_pP->ue_context.measResults = CALLOC(1, sizeof(MeasResults_t));
+     }
+     ue_context_pP->ue_context.measResults->measId=measResults2->measId; 
+     ue_context_pP->ue_context.measResults->measResultPCell.rsrpResult=measResults2->measResultPCell.rsrpResult;
+     ue_context_pP->ue_context.measResults->measResultPCell.rsrqResult=measResults2->measResultPCell.rsrqResult;
+     LOG_D(RRC, "[eNB %d]Frame %d: UE %x (Measurement Id %d): RSRP of Source %ld\n", ctxt_pP->module_id, ctxt_pP->frame, ctxt_pP->rnti, (int)measResults2->measId, ue_context_pP->ue_context.measResults->measResultPCell.rsrpResult-140);
+     LOG_D(RRC, "[eNB %d]Frame %d: UE %x (Measurement Id %d): RSRQ of Source %ld\n", ctxt_pP->module_id, ctxt_pP->frame, ctxt_pP->rnti, (int)measResults2->measId, ue_context_pP->ue_context.measResults->measResultPCell.rsrqResult/2 - 20);
+   }
+   if (measResults2->measResultNeighCells == NULL)
+     return;
 
-  LOG_I(RRC, "[eNB %d] Frame %d: Process Measurement Report From UE %x (Measurement Id %d)\n",
-        ctxt_pP->module_id, ctxt_pP->frame, ctxt_pP->rnti, (int)measResults2->measId);
-
-  if (measResults2->measResultNeighCells->choice.measResultListEUTRA.list.count > 0) {
-    LOG_I(RRC, "Physical Cell Id %d\n",
-          (int)measResults2->measResultNeighCells->choice.measResultListEUTRA.list.array[0]->physCellId);
-    LOG_I(RRC, "RSRP of Target %d\n",
-          (int)*(measResults2->measResultNeighCells->choice.measResultListEUTRA.list.array[0]->
-                 measResult.rsrpResult));
-    LOG_I(RRC, "RSRQ of Target %d\n",
-          (int)*(measResults2->measResultNeighCells->choice.measResultListEUTRA.list.array[0]->
-                 measResult.rsrqResult));
-  }
+   if (measResults2->measResultNeighCells->choice.measResultListEUTRA.list.count > 0) {
+     neighboring_cells = measResults2->measResultNeighCells->choice.measResultListEUTRA.list.count;
+     
+     if (ue_context_pP->ue_context.measResults->measResultNeighCells == NULL) {
+       
+       ue_context_pP->ue_context.measResults->measResultNeighCells = CALLOC(1, sizeof(*measResults2->measResultNeighCells)*neighboring_cells);
+     }
+     ue_context_pP->ue_context.measResults->measResultNeighCells->choice.measResultListEUTRA.list.count = neighboring_cells;
+     for (i=0; i < neighboring_cells; i++){
+       memcpy (ue_context_pP->ue_context.measResults->measResultNeighCells->choice.measResultListEUTRA.list.array[i],
+	       measResults2->measResultNeighCells->choice.measResultListEUTRA.list.array[i],
+	       sizeof(MeasResultListEUTRA_t));
+       
+       LOG_D(RRC, "Physical Cell Id %d\n",
+	     (int)ue_context_pP->ue_context.measResults->measResultNeighCells->choice.measResultListEUTRA.list.array[i]->physCellId);
+       LOG_D(RRC, "RSRP of Target %d\n",
+	     (int)*(ue_context_pP->ue_context.measResults->measResultNeighCells->choice.measResultListEUTRA.list.array[i]->measResult.rsrpResult));
+       LOG_D(RRC, "RSRQ of Target %d\n",
+	     (int)*(ue_context_pP->ue_context.measResults->measResultNeighCells->choice.measResultListEUTRA.list.array[i]->measResult.rsrqResult));
+     }
+   }
 
-#if defined(Rel10) || defined(Rel14)
-  LOG_I(RRC, "RSRP of Source %ld\n", measResults2->measResultPCell.rsrpResult);
-  LOG_I(RRC, "RSRQ of Source %ld\n", measResults2->measResultPCell.rsrqResult);
-#else
-  LOG_I(RRC, "RSRP of Source %ld\n", measResults2->measResultServCell.rsrpResult);
-  LOG_I(RRC, "RSRQ of Source %ld\n", measResults2->measResultServCell.rsrqResult);
-#endif
+// #if defined(Rel10) || defined(Rel14)
 
-  if (ue_context_pP->ue_context.handover_info->ho_prepare != 0xF0) {
-    rrc_eNB_generate_HandoverPreparationInformation(ctxt_pP,
-        ue_context_pP,
-        measResults2->measResultNeighCells->choice.
-        measResultListEUTRA.list.array[0]->physCellId);
-  } else {
-    LOG_D(RRC, "[eNB %d] Frame %d: Ignoring MeasReport from UE %x as Handover is in progress... \n", ctxt_pP->module_id, ctxt_pP->frame,
-          ctxt_pP->rnti);
-  }
+  
+// #else
+  // LOG_I(RRC, "RSRP of Source %d\n", measResults2->measResultServCell.rsrpResult);
+  // LOG_I(RRC, "RSRQ of Source %d\n", measResults2->measResultServCell.rsrqResult);
+// #endif
+
+  // if (ue_context_pP->ue_context.handover_info->ho_prepare != 0xF0) {
+  //   rrc_eNB_generate_HandoverPreparationInformation(ctxt_pP,
+  //       ue_context_pP,
+  //       measResults2->measResultNeighCells->choice.
+  //       measResultListEUTRA.list.array[0]->physCellId);
+  // } else {
+  //   LOG_D(RRC, "[eNB %d] Frame %d: Ignoring MeasReport from UE %x as Handover is in progress... \n", ctxt_pP->module_id, ctxt_pP->frame,
+  //         ctxt_pP->rnti);
+  // }
 
   //Look for IP address of the target eNB
   //Send Handover Request -> target eNB
@@ -5672,6 +6289,11 @@ rrc_eNB_decode_ccch(
 
         } else {
           // no context available
+	  if (rrc_agent_registered[ctxt_pP->module_id]) {
+	    agent_rrc_xface[ctxt_pP->module_id]->flexran_agent_notify_ue_state_change(ctxt_pP->module_id,
+										      ctxt_pP->rnti,
+										      PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_DEACTIVATED);
+	  }
           LOG_I(RRC, PROTOCOL_RRC_CTXT_UE_FMT" Can't create new context for UE random UE identity (0x%" PRIx64 ")\n",
                 PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP),
                 random_value);
@@ -5778,7 +6400,6 @@ rrc_eNB_decode_dcch(
   asn_dec_rval_t                      dec_rval;
   //UL_DCCH_Message_t uldcchmsg;
   UL_DCCH_Message_t                  *ul_dcch_msg = NULL; //&uldcchmsg;
-  UE_EUTRA_Capability_t              *UE_EUTRA_Capability = NULL;
   int i;
   struct rrc_eNB_ue_context_s*        ue_context_p = NULL;
 #if defined(ENABLE_ITTI)
@@ -5934,14 +6555,12 @@ rrc_eNB_decode_dcch(
           ue_context_p,
 	  ul_dcch_msg->message.choice.c1.choice.rrcConnectionReconfigurationComplete.rrc_TransactionIdentifier);
 
-#if defined(FLEXRAN_AGENT_SB_IF)
 	//WARNING:Inform the controller about the UE activation. Should be moved to RRC agent in the future
-	if (mac_agent_registered[ctxt_pP->module_id]) {
-	  agent_mac_xface[ctxt_pP->eNB_index]->flexran_agent_notify_ue_state_change(ctxt_pP->module_id,
+	if (rrc_agent_registered[ctxt_pP->module_id]) {
+	  agent_rrc_xface[ctxt_pP->eNB_index]->flexran_agent_notify_ue_state_change(ctxt_pP->module_id,
 										ue_context_p->ue_id_rnti,
 										PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_UPDATED);
 	}
-#endif
       }
 #if defined(ENABLE_ITTI)
 #   if defined(ENABLE_USE_MME)
@@ -6076,14 +6695,12 @@ if (ue_context_p->ue_context.nb_of_modify_e_rabs > 0) {
               ul_dcch_msg->message.choice.c1.choice.rrcConnectionReestablishmentComplete.rrc_TransactionIdentifier,
               &ul_dcch_msg->message.choice.c1.choice.rrcConnectionReestablishmentComplete.criticalExtensions.choice.rrcConnectionReestablishmentComplete_r8);
 
-#if defined(FLEXRAN_AGENT_SB_IF)
           //WARNING:Inform the controller about the UE activation. Should be moved to RRC agent in the future
           if (mac_agent_registered[ctxt_pP->module_id]) {
-            agent_mac_xface[ctxt_pP->eNB_index]->flexran_agent_notify_ue_state_change(ctxt_pP->module_id,
+            agent_rrc_xface[ctxt_pP->eNB_index]->flexran_agent_notify_ue_state_change(ctxt_pP->module_id,
                                                                                       ue_context_p->ue_id_rnti,
                                                                                       PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_ACTIVATED);
           }
-#endif
         }
         //ue_context_p->ue_context.ue_release_timer = 0;
         ue_context_p->ue_context.ue_reestablishment_timer = 1;
@@ -6133,14 +6750,12 @@ if (ue_context_p->ue_context.nb_of_modify_e_rabs > 0) {
           LOG_I(RRC, PROTOCOL_RRC_CTXT_UE_FMT" UE State = RRC_CONNECTED \n",
                 PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP));
 	  
-#if defined(FLEXRAN_AGENT_SB_IF)
 	  //WARNING:Inform the controller about the UE activation. Should be moved to RRC agent in the future
-	  if (mac_agent_registered[ctxt_pP->module_id]) {
-	    agent_mac_xface[ctxt_pP->eNB_index]->flexran_agent_notify_ue_state_change(ctxt_pP->module_id,
+	  if (rrc_agent_registered[ctxt_pP->module_id]) {
+	    agent_rrc_xface[ctxt_pP->eNB_index]->flexran_agent_notify_ue_state_change(ctxt_pP->module_id,
 										  ue_context_p->ue_id_rnti,
 										  PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_ACTIVATED);
 	  }
-#endif
         }
       }
 
@@ -6270,18 +6885,33 @@ if (ue_context_p->ue_context.nb_of_modify_e_rabs > 0) {
       xer_fprint(stdout, &asn_DEF_UL_DCCH_Message, (void *)ul_dcch_msg);
 #endif
       LOG_I(RRC, "got UE capabilities for UE %x\n", ctxt_pP->rnti);
+      if (ue_context_p->ue_context.UE_Capability) {
+        LOG_I(RRC, "freeing old UE capabilities for UE %x\n", ctxt_pP->rnti);
+        asn_DEF_UE_EUTRA_Capability.free_struct(&asn_DEF_UE_EUTRA_Capability,
+              ue_context_p->ue_context.UE_Capability, 0);
+        ue_context_p->ue_context.UE_Capability = 0;
+      }
       dec_rval = uper_decode(NULL,
                              &asn_DEF_UE_EUTRA_Capability,
-                             (void **)&UE_EUTRA_Capability,
+                             (void **)&ue_context_p->ue_context.UE_Capability,
                              ul_dcch_msg->message.choice.c1.choice.ueCapabilityInformation.criticalExtensions.
                              choice.c1.choice.ueCapabilityInformation_r8.ue_CapabilityRAT_ContainerList.list.
                              array[0]->ueCapabilityRAT_Container.buf,
                              ul_dcch_msg->message.choice.c1.choice.ueCapabilityInformation.criticalExtensions.
                              choice.c1.choice.ueCapabilityInformation_r8.ue_CapabilityRAT_ContainerList.list.
                              array[0]->ueCapabilityRAT_Container.size, 0, 0);
-      //#ifdef XER_PRINT
-      //xer_fprint(stdout, &asn_DEF_UE_EUTRA_Capability, (void *)UE_EUTRA_Capability);
-      //#endif
+#ifdef XER_PRINT
+      xer_fprint(stdout, &asn_DEF_UE_EUTRA_Capability, ue_context_p->ue_context.UE_Capability);
+#endif
+
+      if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
+        LOG_E(RRC, PROTOCOL_RRC_CTXT_UE_FMT" Failed to decode UE capabilities (%zu bytes)\n",
+              PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP),
+              dec_rval.consumed);
+        asn_DEF_UE_EUTRA_Capability.free_struct(&asn_DEF_UE_EUTRA_Capability,
+              ue_context_p->ue_context.UE_Capability, 0);
+        ue_context_p->ue_context.UE_Capability = 0;
+      }
 
 #if defined(ENABLE_USE_MME)
 
@@ -6469,6 +7099,7 @@ rrc_enb_task(
 
     switch (ITTI_MSG_ID(msg_p)) {
     case TERMINATE_MESSAGE:
+      LOG_W(RRC, " *** Exiting RRC thread\n");
       itti_exit_task();
       break;
 
@@ -6682,26 +7313,58 @@ rrc_rx_tx(
     RB_FOREACH(ue_context_p, rrc_ue_tree_s, &(RC.rrc[ctxt_pP->module_id]->rrc_ue_head)) {
       if ((ctxt_pP->frame == 0) && (ctxt_pP->subframe==0)) {
 	if (ue_context_p->ue_context.Initialue_identity_s_TMSI.presence == TRUE) {
-	  LOG_I(RRC,"UE rnti %x:S-TMSI %x failure timer %d/20000\n",
+	  LOG_I(RRC,"UE rnti %x:S-TMSI %x failure timer %d/8\n",
 		ue_context_p->ue_context.rnti,
 		ue_context_p->ue_context.Initialue_identity_s_TMSI.m_tmsi,
 		ue_context_p->ue_context.ul_failure_timer);
 	}
 	else {
-	  LOG_I(RRC,"UE rnti %x failure timer %d/20000\n",
+	  LOG_I(RRC,"UE rnti %x failure timer %d/8\n",
 		ue_context_p->ue_context.rnti,
 		ue_context_p->ue_context.ul_failure_timer);
 	}
       }
       if (ue_context_p->ue_context.ul_failure_timer>0) {
 	ue_context_p->ue_context.ul_failure_timer++;
-	if (ue_context_p->ue_context.ul_failure_timer >= 20000) {
+	if (ue_context_p->ue_context.ul_failure_timer >= 8) {
 	  // remove UE after 20 seconds after MAC has indicated UL failure
 	  LOG_I(RRC,"Removing UE %x instance\n",ue_context_p->ue_context.rnti);
 	  ue_to_be_removed = ue_context_p;
 	  break;
 	}
       }
+      if (ue_context_p->ue_context.ue_release_timer_s1>0) {
+        ue_context_p->ue_context.ue_release_timer_s1++;
+        if (ue_context_p->ue_context.ue_release_timer_s1 >=
+            ue_context_p->ue_context.ue_release_timer_thres_s1) {
+          LOG_I(RRC,"Removing UE %x instance Because of UE_CONTEXT_RELEASE_COMMAND not received after %d ms from sending request\n",
+                         ue_context_p->ue_context.rnti, ue_context_p->ue_context.ue_release_timer_thres_s1);
+          ue_to_be_removed = ue_context_p;
+          break;
+        }
+      }
+
+      if (ue_context_p->ue_context.ue_release_timer_rrc>0) {
+        ue_context_p->ue_context.ue_release_timer_rrc++;
+        if (ue_context_p->ue_context.ue_release_timer_rrc >=
+          ue_context_p->ue_context.ue_release_timer_thres_rrc) {
+          LOG_I(RRC,"Removing UE %x instance After UE_CONTEXT_RELEASE_Complete\n", ue_context_p->ue_context.rnti);
+          ue_to_be_removed = ue_context_p;
+          break;
+        }
+      }
+
+      if (ue_context_p->ue_context.ue_reestablishment_timer>0) {
+        ue_context_p->ue_context.ue_reestablishment_timer++;
+        if (ue_context_p->ue_context.ue_reestablishment_timer >=
+            ue_context_p->ue_context.ue_reestablishment_timer_thres) {
+          LOG_I(RRC,"UE %d reestablishment_timer max\n",ue_context_p->ue_context.rnti);
+          ue_context_p->ue_context.ul_failure_timer = 20000;
+          ue_to_be_removed = ue_context_p;
+          ue_context_p->ue_context.ue_reestablishment_timer = 0;
+          break;
+        }
+      }
       if (ue_context_p->ue_context.ue_release_timer>0) {
 	ue_context_p->ue_context.ue_release_timer++;
 	if (ue_context_p->ue_context.ue_release_timer >= 
@@ -6712,8 +7375,18 @@ rrc_rx_tx(
 	}
       }
     }
-    if (ue_to_be_removed)
+    if (ue_to_be_removed) {
+      if(ue_to_be_removed->ue_context.ul_failure_timer >= 8) {
+          ue_to_be_removed->ue_context.ue_release_timer_s1 = 1;
+          ue_to_be_removed->ue_context.ue_release_timer_thres_s1 = 100;
+          ue_to_be_removed->ue_context.ue_release_timer = 0;
+          ue_to_be_removed->ue_context.ue_reestablishment_timer = 0;
+      }
       rrc_eNB_free_UE(ctxt_pP->module_id,ue_to_be_removed);
+      if(ue_to_be_removed->ue_context.ul_failure_timer >= 8){
+        ue_to_be_removed->ue_context.ul_failure_timer = 0;
+      }
+    }
 
 #ifdef RRC_LOCALIZATION
 
diff --git a/openair2/RRC/LITE/rrc_eNB_S1AP.c b/openair2/RRC/LITE/rrc_eNB_S1AP.c
index d2c0d0acbe8f7375370fdc3699af8f6d6be1ca86..80ae1c9ba57e9e276f5f4581c6dea7c9c7e49bec 100644
--- a/openair2/RRC/LITE/rrc_eNB_S1AP.c
+++ b/openair2/RRC/LITE/rrc_eNB_S1AP.c
@@ -59,6 +59,10 @@
 #include "gtpv1u_eNB_task.h"
 #include "RRC/LITE/rrc_eNB_GTPV1U.h"
 
+#include "TLVDecoder.h"
+#include "S1ap-NAS-PDU.h"
+#include "flexran_agent_common_internal.h"
+
 extern RAN_CONTEXT_t RC;
 
 /* Value to indicate an invalid UE initial id */
@@ -82,8 +86,80 @@ static const uint16_t S1AP_INTEGRITY_EIA2_MASK = 0x4000;
 #endif
 #endif
 
+void extract_imsi(uint8_t *pdu_buf, uint32_t pdu_len, rrc_eNB_ue_context_t *ue_context_pP)
+{
+  /* Process NAS message locally to get the IMSI */
+  nas_message_t nas_msg;
+  memset(&nas_msg, 0, sizeof(nas_message_t));
+
+  int size = 0;
+
+  nas_message_security_header_t *header = &nas_msg.header;
+  /* Decode the first octet of the header (security header type or EPS
+  * bearer identity, and protocol discriminator) */
+  DECODE_U8((char *) pdu_buf, *(uint8_t*) (header), size);
+
+  /* Decode NAS message only if decodable*/
+  if (!(header->security_header_type <= SECURITY_HEADER_TYPE_INTEGRITY_PROTECTED
+      && header->protocol_discriminator == EPS_MOBILITY_MANAGEMENT_MESSAGE
+      && pdu_len > NAS_MESSAGE_SECURITY_HEADER_SIZE))
+    return;
+
+  if (header->security_header_type != SECURITY_HEADER_TYPE_NOT_PROTECTED) {
+    /* Decode the message authentication code */
+    DECODE_U32((char *) pdu_buf+size, header->message_authentication_code, size);
+    /* Decode the sequence number */
+    DECODE_U8((char *) pdu_buf+size, header->sequence_number, size);
+  }
+
+  /* Note: the value of the pointer (i.e. the address) is given by value, so we
+   * can modify it as we want. The callee retains the original address! */
+  pdu_buf += size;
+  pdu_len -= size;
+
+  /* Decode plain NAS message */
+  EMM_msg *e_msg = &nas_msg.plain.emm;
+  emm_msg_header_t *emm_header = &e_msg->header;
+
+  /* First decode the EMM message header */
+  int e_head_size = 0;
+
+  /* Check that buffer contains more than only the header */
+  if (pdu_len <= sizeof(emm_msg_header_t))
+    return;
+
+  /* Decode the security header type and the protocol discriminator */
+  DECODE_U8(pdu_buf + e_head_size, *(uint8_t *)(emm_header), e_head_size);
+  /* Decode the message type */
+  DECODE_U8(pdu_buf + e_head_size, emm_header->message_type, e_head_size);
+
+  /* Check that this is the right message */
+  if (emm_header->protocol_discriminator != EPS_MOBILITY_MANAGEMENT_MESSAGE)
+    return;
 
+  pdu_buf += e_head_size;
+  pdu_len -= e_head_size;
 
+  if (emm_header->message_type == IDENTITY_RESPONSE) {
+    decode_identity_response(&e_msg->identity_response, pdu_buf, pdu_len);
+
+    if (e_msg->identity_response.mobileidentity.imsi.typeofidentity == MOBILE_IDENTITY_IMSI) {
+      memcpy(&ue_context_pP->ue_context.imsi,
+             &e_msg->identity_response.mobileidentity.imsi,
+             sizeof(ImsiMobileIdentity_t));
+    }
+  } else if (emm_header->message_type == ATTACH_REQUEST) {
+    decode_attach_request(&e_msg->attach_request, pdu_buf, pdu_len);
+
+    if (e_msg->attach_request.oldgutiorimsi.imsi.typeofidentity == MOBILE_IDENTITY_IMSI) {
+      /* the following is very dirty, we cast (implicitly) from
+       * ImsiEpsMobileIdentity_t to ImsiMobileIdentity_t*/
+      memcpy(&ue_context_pP->ue_context.imsi,
+             &e_msg->attach_request.oldgutiorimsi.imsi,
+             sizeof(ImsiMobileIdentity_t));
+    }
+  }
+}
 
 # if defined(ENABLE_ITTI)
 //------------------------------------------------------------------------------
@@ -542,6 +618,10 @@ rrc_eNB_send_S1AP_UPLINK_NAS(
       S1AP_UPLINK_NAS (msg_p).nas_pdu.length = pdu_length;
       S1AP_UPLINK_NAS (msg_p).nas_pdu.buffer = pdu_buffer;
 
+      extract_imsi(S1AP_UPLINK_NAS (msg_p).nas_pdu.buffer,
+                   S1AP_UPLINK_NAS (msg_p).nas_pdu.length,
+                   ue_context_pP);
+
       itti_send_msg_to_task (TASK_S1AP, ctxt_pP->instance, msg_p);
     }
   }
@@ -563,16 +643,17 @@ rrc_eNB_send_S1AP_UPLINK_NAS(
         &ulInformationTransfer->criticalExtensions.choice.
         c1.choice.ulInformationTransfer_r8;
 
-        if (ulInformationTransferR8->dedicatedInfoType.
-        present ==
-        ULInformationTransfer_r8_IEs__dedicatedInfoType_PR_dedicatedInfoNAS)
+        if (ulInformationTransferR8->dedicatedInfoType.present ==
+            ULInformationTransfer_r8_IEs__dedicatedInfoType_PR_dedicatedInfoNAS) {
+
+          extract_imsi(ulInformationTransferR8->dedicatedInfoType.choice.dedicatedInfoNAS.buf,
+                       ulInformationTransferR8->dedicatedInfoType.choice.dedicatedInfoNAS.size,
+                       ue_context_pP);
+
           s1ap_eNB_new_data_request (mod_id, ue_index,
-          ulInformationTransferR8->
-          dedicatedInfoType.choice.
-          dedicatedInfoNAS.buf,
-          ulInformationTransferR8->
-          dedicatedInfoType.choice.
-          dedicatedInfoNAS.size);
+              ulInformationTransferR8->dedicatedInfoType.choice.dedicatedInfoNAS.buf,
+              ulInformationTransferR8->dedicatedInfoType.choice.dedicatedInfoNAS.size);
+        }
       }
     }
   }
@@ -674,6 +755,10 @@ rrc_eNB_send_S1AP_NAS_FIRST_REQ(
     rrcConnectionSetupComplete->dedicatedInfoNAS.buf;
     S1AP_NAS_FIRST_REQ (message_p).nas_pdu.length = rrcConnectionSetupComplete->dedicatedInfoNAS.size;
 
+    extract_imsi(S1AP_NAS_FIRST_REQ (message_p).nas_pdu.buffer,
+                 S1AP_NAS_FIRST_REQ (message_p).nas_pdu.length,
+                 ue_context_pP);
+
     /* Fill UE identities with available information */
     {
       S1AP_NAS_FIRST_REQ (message_p).ue_identity.presenceMask = UE_IDENTITIES_NONE;
diff --git a/openair2/UTIL/ASYNC_IF/ringbuffer_queue.c b/openair2/UTIL/ASYNC_IF/ringbuffer_queue.c
index 1a3701ed5cc043ccb69b4c6c8a7b899158cb6409..b178d3a5d665d7d2864f762a2a39e3b7888cf2bd 100644
--- a/openair2/UTIL/ASYNC_IF/ringbuffer_queue.c
+++ b/openair2/UTIL/ASYNC_IF/ringbuffer_queue.c
@@ -117,7 +117,7 @@ int message_get(message_queue_t *queue, void **data, int *size, int *priority) {
   return 0;
 }
 
-message_queue_t destroy_message_queue(message_queue_t *queue) {
+void destroy_message_queue(message_queue_t *queue) {
   struct lfds700_misc_prng_state ls;
 
   message_t *m;
diff --git a/openair2/UTIL/ASYNC_IF/ringbuffer_queue.h b/openair2/UTIL/ASYNC_IF/ringbuffer_queue.h
index 185d23daf59d136e69c9293f9ffac722fb210047..04414cbbb2b9618379ad1284a056943e04539c23 100644
--- a/openair2/UTIL/ASYNC_IF/ringbuffer_queue.h
+++ b/openair2/UTIL/ASYNC_IF/ringbuffer_queue.h
@@ -48,6 +48,6 @@ typedef struct {
 message_queue_t * new_message_queue(int size);
 int message_put(message_queue_t *queue, void *data, int size, int priority);
 int message_get(message_queue_t *queue, void **data, int *size, int *priority);
-message_queue_t destroy_message_queue(message_queue_t *queue);
+void destroy_message_queue(message_queue_t *queue);
 
 #endif /* RINGBUFFER_QUEUE_H */
diff --git a/openair2/X2AP/x2ap.c b/openair2/X2AP/x2ap.c
index bde21c0dbe1e57cc0d5abeac07d3f5a9b2e8fc8d..8002867e15c31c8b78ada0894c9bbef20ae2cef1 100644
--- a/openair2/X2AP/x2ap.c
+++ b/openair2/X2AP/x2ap.c
@@ -49,6 +49,7 @@ void *x2ap_task(void *arg)
 
     switch (ITTI_MSG_ID(received_msg)) {
     case TERMINATE_MESSAGE:
+      X2AP_WARN(" *** Exiting X2AP thread\n");
       itti_exit_task();
       break;
 
diff --git a/openair3/GTPV1-U/gtpv1u_eNB.c b/openair3/GTPV1-U/gtpv1u_eNB.c
index 4234bc373d70a725414c60706b97aa9b5bbe63ea..a2517e07b84b162dd4fbfe55a02669f1fb7505f4 100644
--- a/openair3/GTPV1-U/gtpv1u_eNB.c
+++ b/openair3/GTPV1-U/gtpv1u_eNB.c
@@ -1219,6 +1219,7 @@ void *gtpv1u_eNB_task(void *args)
         hashtable_destroy (RC.gtpv1u_data_g->teid_mapping);
       }
 
+      LOG_W(GTPU, " *** Exiting GTPU thread\n");
       itti_exit_task();
     }
     break;
diff --git a/openair3/GTPV1-U/gtpv1u_task.c b/openair3/GTPV1-U/gtpv1u_task.c
index a6dd6fa06151ae08e6373e5d78d9005bbd37db6d..287c22b7e7ab28d162f6b36815b11eb14b629588 100644
--- a/openair3/GTPV1-U/gtpv1u_task.c
+++ b/openair3/GTPV1-U/gtpv1u_task.c
@@ -423,6 +423,7 @@ static void *gtpv1u_thread(void *args)
     switch (ITTI_MSG_ID(received_message_p)) {
 
     case TERMINATE_MESSAGE: {
+      GTPU_WARN(" *** Exiting GTPU thread\n");
       itti_exit_task();
     }
     break;
diff --git a/openair3/S1AP/s1ap_eNB.c b/openair3/S1AP/s1ap_eNB.c
index 301df6e81600fdff0e6f3ffae37e69e2d8d13019..8085c239c304d85cdefeb6671fe57b3e2561c18c 100644
--- a/openair3/S1AP/s1ap_eNB.c
+++ b/openair3/S1AP/s1ap_eNB.c
@@ -307,6 +307,7 @@ void *s1ap_eNB_task(void *arg)
 
     switch (ITTI_MSG_ID(received_msg)) {
     case TERMINATE_MESSAGE:
+      S1AP_WARN(" *** Exiting S1AP thread\n");
       itti_exit_task();
       break;
 
diff --git a/openair3/SCTP/sctp_eNB_task.c b/openair3/SCTP/sctp_eNB_task.c
index 7771cab712f22b747f7dbc451e98c01495250004..256bee0cd12ecca1d8c8cd28a42f105e745da12d 100644
--- a/openair3/SCTP/sctp_eNB_task.c
+++ b/openair3/SCTP/sctp_eNB_task.c
@@ -841,6 +841,7 @@ void *sctp_eNB_task(void *arg)
         break;
 
       case TERMINATE_MESSAGE:
+        SCTP_WARN("*** Exiting SCTP thread\n");
         itti_exit_task();
         break;
 
diff --git a/openair3/TEST/EPC_TEST/play_scenario_s1ap.c b/openair3/TEST/EPC_TEST/play_scenario_s1ap.c
index f10126d5b55928aaba1641fc24c606e1e4fabf32..95df0fc306c0bf2ee72ca6275da11363166f693e 100644
--- a/openair3/TEST/EPC_TEST/play_scenario_s1ap.c
+++ b/openair3/TEST/EPC_TEST/play_scenario_s1ap.c
@@ -1089,6 +1089,7 @@ void *et_s1ap_eNB_task(void *arg)
 
     switch (ITTI_MSG_ID(received_msg)) {
     case TERMINATE_MESSAGE:
+      S1AP_WARN("*** Exiting S1AP thread\n");
       itti_exit_task();
       break;
 
diff --git a/openair3/UDP/udp_eNB_task.c b/openair3/UDP/udp_eNB_task.c
index 868d86ad9cec84dcb97a55749ce8131256b296ca..527715eb7a2470c3577bb1a3b5a4fa40303af74b 100644
--- a/openair3/UDP/udp_eNB_task.c
+++ b/openair3/UDP/udp_eNB_task.c
@@ -389,7 +389,7 @@ void *udp_eNB_task(void *args_p)
       break;
 
       case TERMINATE_MESSAGE: {
-        LOG_W(UDP_, "Received TERMINATE_MESSAGE\n");
+        LOG_W(UDP_, " *** Exiting UDP thread\n");
         itti_exit_task();
       }
       break;
diff --git a/targets/PROJECTS/GENERIC-LTE-EPC/CONF/enb.band7.tm1.50PRB.usrpb210.conf b/targets/PROJECTS/GENERIC-LTE-EPC/CONF/enb.band7.tm1.50PRB.usrpb210.conf
index cf322bc7f3ef9488686185e4ab7bf8a9cb68cba0..c4307f8ff2333d9bcbe202e719917912dbb947d9 100644
--- a/targets/PROJECTS/GENERIC-LTE-EPC/CONF/enb.band7.tm1.50PRB.usrpb210.conf
+++ b/targets/PROJECTS/GENERIC-LTE-EPC/CONF/enb.band7.tm1.50PRB.usrpb210.conf
@@ -189,6 +189,16 @@ RUs = (
     }
 );  
 
+NETWORK_CONTROLLER :
+{
+    FLEXRAN_ENABLED        = "no";
+    FLEXRAN_INTERFACE_NAME = "lo";
+    FLEXRAN_IPV4_ADDRESS   = "127.0.0.1";
+    FLEXRAN_PORT           = 2210;
+    FLEXRAN_CACHE          = "/mnt/oai_agent_cache";
+    FLEXRAN_AWAIT_RECONF   = "no";
+};
+
      log_config :
      {
        global_log_level                      ="info";
diff --git a/targets/RT/USER/lte-enb.c b/targets/RT/USER/lte-enb.c
index f447f6589dab3fe2e3f4a5c1bcc19fb9ee214670..01a2c68bf3b5147e45945fff430a1dad2acdf2fb 100644
--- a/targets/RT/USER/lte-enb.c
+++ b/targets/RT/USER/lte-enb.c
@@ -344,7 +344,7 @@ static void* eNB_thread_rxtx( void* param ) {
 
   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_RXTX0+(proc->subframe_rx&1), 0 );
 
-  printf( "Exiting eNB thread RXn_TXnp4\n");
+  LOG_D(PHY, " *** Exiting eNB thread RXn_TXnp4\n");
 
   eNB_thread_rxtx_status = 0;
   return &eNB_thread_rxtx_status;
@@ -817,20 +817,24 @@ void kill_eNB_proc(int inst) {
     
     proc = &eNB->proc;
     proc_rxtx = &proc->proc_rxtx[0];
-    
 
     LOG_I(PHY, "Killing TX CC_id %d inst %d\n", CC_id, inst );
 
     if (eNB->single_thread_flag==0) {
-      proc_rxtx[0].instance_cnt_rxtx = 0; // FIXME data race!
-      proc_rxtx[1].instance_cnt_rxtx = 0; // FIXME data race!
-      pthread_cond_signal( &proc_rxtx[0].cond_rxtx );    
-      pthread_cond_signal( &proc_rxtx[1].cond_rxtx );
+      pthread_mutex_lock(&proc_rxtx[0].mutex_rxtx);
+      proc_rxtx[0].instance_cnt_rxtx = 0;
+      pthread_mutex_unlock(&proc_rxtx[0].mutex_rxtx);
+      pthread_mutex_lock(&proc_rxtx[1].mutex_rxtx);
+      proc_rxtx[1].instance_cnt_rxtx = 0;
+      pthread_mutex_unlock(&proc_rxtx[1].mutex_rxtx);
     }
     proc->instance_cnt_prach = 0;
     pthread_cond_signal( &proc->cond_prach );
 
+    pthread_cond_signal( &proc->cond_asynch_rxtx );
     pthread_cond_broadcast(&sync_phy_proc.cond_phy_proc_tx);
+
+    LOG_D(PHY, "joining pthread_prach\n");
     pthread_join( proc->pthread_prach, (void**)&status );    
 
     LOG_I(PHY, "Destroying prach mutex/cond\n");
@@ -886,6 +890,21 @@ void print_opp_meas(void) {
   }
 }
 
+void free_transport(PHY_VARS_eNB *eNB)
+{
+  int i;
+  int j;
+
+  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
+    LOG_I(PHY, "Freeing Transport Channel Buffers for DLSCH, UE %d\n",i);
+    for (j=0; j<2; j++) free_eNB_dlsch(eNB->dlsch[i][j]);
+
+    LOG_I(PHY, "Freeing Transport Channel Buffer for ULSCH, UE %d\n",i);
+    free_eNB_ulsch(eNB->ulsch[1+i]);
+  }
+  free_eNB_ulsch(eNB->ulsch[0]);
+}
+
 void init_transport(PHY_VARS_eNB *eNB) {
 
   int i;
diff --git a/targets/RT/USER/lte-ru.c b/targets/RT/USER/lte-ru.c
index 65d945dd7eac3832d81e75ba57503bdd8394331b..06aad94d3099630f17484b8ebc0ee91428d00dd4 100644
--- a/targets/RT/USER/lte-ru.c
+++ b/targets/RT/USER/lte-ru.c
@@ -117,9 +117,10 @@ extern volatile int                    oai_exit;
 
 
 extern void  phy_init_RU(RU_t*);
+extern void  phy_free_RU(RU_t*);
 
 void init_RU(char*);
-void stop_RU(RU_t *ru);
+void stop_RU(int nb_ru);
 void do_ru_sync(RU_t *ru);
 
 void configure_ru(int idx,
@@ -1273,8 +1274,8 @@ void fill_rf_config(RU_t *ru, char *rf_config_file) {
     cfg->tx_freq[i] = (double)fp->dl_CarrierFreq;
     cfg->rx_freq[i] = (double)fp->ul_CarrierFreq;
 
-    cfg->tx_gain[i] = (double)fp->att_tx;
-    cfg->rx_gain[i] = ru->max_rxgain-(double)fp->att_rx;
+    cfg->tx_gain[i] = ru->att_tx;
+    cfg->rx_gain[i] = ru->max_rxgain-ru->att_rx;
 
     cfg->configFilename = rf_config_file;
     printf("channel %d, Setting tx_gain offset %f, rx_gain offset %f, tx_freq %f, rx_freq %f\n",
@@ -1597,8 +1598,6 @@ void *ru_thread_synch(void *arg) {
     if (release_thread(&ru->proc.mutex_synch,&ru->proc.instance_cnt_synch,"ru_synch_thread") < 0) break;
   } // oai_exit
 
-  lte_sync_time_free();
-
   ru_thread_synch_status = 0;
   return &ru_thread_synch_status;
 
@@ -1656,11 +1655,13 @@ void init_RU_proc(RU_t *ru) {
   pthread_mutex_init( &proc->mutex_asynch_rxtx, NULL);
   pthread_mutex_init( &proc->mutex_synch,NULL);
   pthread_mutex_init( &proc->mutex_FH,NULL);
+  pthread_mutex_init( &proc->mutex_eNBs, NULL);
   
   pthread_cond_init( &proc->cond_prach, NULL);
   pthread_cond_init( &proc->cond_FH, NULL);
   pthread_cond_init( &proc->cond_asynch_rxtx, NULL);
   pthread_cond_init( &proc->cond_synch,NULL);
+  pthread_cond_init( &proc->cond_eNBs, NULL);
   
   pthread_attr_init( &proc->attr_FH);
   pthread_attr_init( &proc->attr_prach);
@@ -1716,6 +1717,116 @@ void init_RU_proc(RU_t *ru) {
   
 }
 
+void kill_RU_proc(int inst)
+{
+  RU_t *ru = RC.ru[inst];
+  RU_proc_t *proc = &ru->proc;
+
+  pthread_mutex_lock(&proc->mutex_FH);
+  proc->instance_cnt_FH = 0;
+  pthread_mutex_unlock(&proc->mutex_FH);
+  pthread_cond_signal(&proc->cond_FH);
+
+  pthread_mutex_lock(&proc->mutex_prach);
+  proc->instance_cnt_prach = 0;
+  pthread_mutex_unlock(&proc->mutex_prach);
+  pthread_cond_signal(&proc->cond_prach);
+
+#ifdef Rel14
+  pthread_mutex_lock(&proc->mutex_prach_br);
+  proc->instance_cnt_prach_br = 0;
+  pthread_mutex_unlock(&proc->mutex_prach_br);
+  pthread_cond_signal(&proc->cond_prach_br);
+#endif
+
+  pthread_mutex_lock(&proc->mutex_synch);
+  proc->instance_cnt_synch = 0;
+  pthread_mutex_unlock(&proc->mutex_synch);
+  pthread_cond_signal(&proc->cond_synch);
+
+  pthread_mutex_lock(&proc->mutex_eNBs);
+  proc->instance_cnt_eNBs = 0;
+  pthread_mutex_unlock(&proc->mutex_eNBs);
+  pthread_cond_signal(&proc->cond_eNBs);
+
+  pthread_mutex_lock(&proc->mutex_asynch_rxtx);
+  proc->instance_cnt_asynch_rxtx = 0;
+  pthread_mutex_unlock(&proc->mutex_asynch_rxtx);
+  pthread_cond_signal(&proc->cond_asynch_rxtx);
+
+  LOG_D(PHY, "Joining pthread_FH\n");
+  pthread_join(proc->pthread_FH, NULL);
+  if (ru->function == NGFI_RRU_IF4p5) {
+    LOG_D(PHY, "Joining pthread_prach\n");
+    pthread_join(proc->pthread_prach, NULL);
+#ifdef Rel14
+    LOG_D(PHY, "Joining pthread_prach_br\n");
+    pthread_join(proc->pthread_prach_br, NULL);
+#endif
+    if (ru->is_slave) {
+      LOG_D(PHY, "Joining pthread_\n");
+      pthread_join(proc->pthread_synch, NULL);
+    }
+
+    if ((ru->if_timing == synch_to_other) ||
+        (ru->function == NGFI_RRU_IF5) ||
+        (ru->function == NGFI_RRU_IF4p5)) {
+      LOG_D(PHY, "Joining pthread_asynch_rxtx\n");
+      pthread_join(proc->pthread_asynch_rxtx, NULL);
+    }
+  }
+  if (get_nprocs() >= 2) {
+    if (ru->feprx) {
+      pthread_mutex_lock(&proc->mutex_fep);
+      proc->instance_cnt_fep = 0;
+      pthread_mutex_unlock(&proc->mutex_fep);
+      pthread_cond_signal(&proc->cond_fep);
+      LOG_D(PHY, "Joining pthread_fep\n");
+      pthread_join(proc->pthread_fep, NULL);
+      pthread_mutex_destroy(&proc->mutex_fep);
+      pthread_cond_destroy(&proc->cond_fep);
+    }
+    if (ru->feptx_ofdm) {
+      pthread_mutex_lock(&proc->mutex_feptx);
+      proc->instance_cnt_feptx = 0;
+      pthread_mutex_unlock(&proc->mutex_feptx);
+      pthread_cond_signal(&proc->cond_feptx);
+      LOG_D(PHY, "Joining pthread_feptx\n");
+      pthread_join(proc->pthread_feptx, NULL);
+      pthread_mutex_destroy(&proc->mutex_feptx);
+      pthread_cond_destroy(&proc->cond_feptx);
+    }
+  }
+  if (opp_enabled) {
+    LOG_D(PHY, "Joining ru_stats_thread\n");
+    pthread_join(ru->ru_stats_thread, NULL);
+  }
+
+  pthread_mutex_destroy(&proc->mutex_prach);
+  pthread_mutex_destroy(&proc->mutex_asynch_rxtx);
+  pthread_mutex_destroy(&proc->mutex_synch);
+  pthread_mutex_destroy(&proc->mutex_FH);
+  pthread_mutex_destroy(&proc->mutex_eNBs);
+
+  pthread_cond_destroy(&proc->cond_prach);
+  pthread_cond_destroy(&proc->cond_FH);
+  pthread_cond_destroy(&proc->cond_asynch_rxtx);
+  pthread_cond_destroy(&proc->cond_synch);
+  pthread_cond_destroy(&proc->cond_eNBs);
+
+  pthread_attr_destroy(&proc->attr_FH);
+  pthread_attr_destroy(&proc->attr_prach);
+  pthread_attr_destroy(&proc->attr_synch);
+  pthread_attr_destroy(&proc->attr_asynch_rxtx);
+  pthread_attr_destroy(&proc->attr_fep);
+
+#ifdef Rel14
+  pthread_mutex_destroy(&proc->mutex_prach_br);
+  pthread_cond_destroy(&proc->cond_prach_br);
+  pthread_attr_destroy(&proc->attr_prach_br);
+#endif
+}
+
 int check_capabilities(RU_t *ru,RRU_capabilities_t *cap) {
 
   FH_fmt_options_t fmt = cap->FH_fmt;
@@ -1897,13 +2008,159 @@ void init_precoding_weights(PHY_VARS_eNB *eNB) {
   }
 }
 
+void set_function_spec_param(RU_t *ru)
+{
+  int ret;
+
+  switch (ru->if_south) {
+  case LOCAL_RF:   // this is an RU with integrated RF (RRU, eNB)
+    if (ru->function ==  NGFI_RRU_IF5) {                 // IF5 RRU
+      ru->do_prach              = 0;                      // no prach processing in RU
+      ru->fh_north_in           = NULL;                   // no shynchronous incoming fronthaul from north
+      ru->fh_north_out          = fh_if5_north_out;       // need only to do send_IF5  reception
+      ru->fh_south_out          = tx_rf;                  // send output to RF
+      ru->fh_north_asynch_in    = fh_if5_north_asynch_in; // TX packets come asynchronously
+      ru->feprx                 = NULL;                   // nothing (this is a time-domain signal)
+      ru->feptx_ofdm            = NULL;                   // nothing (this is a time-domain signal)
+      ru->feptx_prec            = NULL;                   // nothing (this is a time-domain signal)
+      ru->start_if              = start_if;               // need to start the if interface for if5
+      ru->ifdevice.host_type    = RRU_HOST;
+      ru->rfdevice.host_type    = RRU_HOST;
+      ru->ifdevice.eth_params   = &ru->eth_params;
+      reset_meas(&ru->rx_fhaul);
+      reset_meas(&ru->tx_fhaul);
+      reset_meas(&ru->compression);
+      reset_meas(&ru->transport);
+
+      ret = openair0_transport_load(&ru->ifdevice,&ru->openair0_cfg,&ru->eth_params);
+      printf("openair0_transport_init returns %d for ru_id %d\n", ret, ru->idx);
+      if (ret<0) {
+        printf("Exiting, cannot initialize transport protocol\n");
+        exit(-1);
+      }
+    }
+    else if (ru->function == NGFI_RRU_IF4p5) {
+      ru->do_prach              = 1;                        // do part of prach processing in RU
+      ru->fh_north_in           = NULL;                     // no synchronous incoming fronthaul from north
+      ru->fh_north_out          = fh_if4p5_north_out;       // send_IF4p5 on reception
+      ru->fh_south_out          = tx_rf;                    // send output to RF
+      ru->fh_north_asynch_in    = fh_if4p5_north_asynch_in; // TX packets come asynchronously
+      ru->feprx                 = (get_nprocs()<=2) ? fep_full :ru_fep_full_2thread;                 // RX DFTs
+      ru->feptx_ofdm            = (get_nprocs()<=2) ? feptx_ofdm : feptx_ofdm_2thread;               // this is fep with idft only (no precoding in RRU)
+      ru->feptx_prec            = NULL;
+      ru->start_if              = start_if;                 // need to start the if interface for if4p5
+      ru->ifdevice.host_type    = RRU_HOST;
+      ru->rfdevice.host_type    = RRU_HOST;
+      ru->ifdevice.eth_params   = &ru->eth_params;
+      reset_meas(&ru->rx_fhaul);
+      reset_meas(&ru->tx_fhaul);
+      reset_meas(&ru->compression);
+      reset_meas(&ru->transport);
+
+      ret = openair0_transport_load(&ru->ifdevice,&ru->openair0_cfg,&ru->eth_params);
+      printf("openair0_transport_init returns %d for ru_id %d\n", ret, ru->idx);
+      if (ret<0) {
+        printf("Exiting, cannot initialize transport protocol\n");
+        exit(-1);
+      }
+      malloc_IF4p5_buffer(ru);
+    }
+    else if (ru->function == eNodeB_3GPP) {
+      ru->do_prach             = 0;                       // no prach processing in RU
+      ru->feprx                = (get_nprocs()<=2) ? fep_full : ru_fep_full_2thread;                // RX DFTs
+      ru->feptx_ofdm           = (get_nprocs()<=2) ? feptx_ofdm : feptx_ofdm_2thread;              // this is fep with idft and precoding
+      ru->feptx_prec           = feptx_prec;              // this is fep with idft and precoding
+      ru->fh_north_in          = NULL;                    // no incoming fronthaul from north
+      ru->fh_north_out         = NULL;                    // no outgoing fronthaul to north
+      ru->start_if             = NULL;                    // no if interface
+      ru->rfdevice.host_type   = RAU_HOST;
+    }
+    ru->fh_south_in            = rx_rf;                               // local synchronous RF RX
+    ru->fh_south_out           = tx_rf;                               // local synchronous RF TX
+    ru->start_rf               = start_rf;                            // need to start the local RF interface
+    printf("configuring ru_id %d (start_rf %p)\n", ru->idx, start_rf);
+/*
+    if (ru->function == eNodeB_3GPP) { // configure RF parameters only for 3GPP eNodeB, we need to get them from RAU otherwise
+      fill_rf_config(ru,rf_config_file);
+      init_frame_parms(&ru->frame_parms,1);
+      phy_init_RU(ru);
+    }
+
+    ret = openair0_device_load(&ru->rfdevice,&ru->openair0_cfg);
+    if (setup_RU_buffers(ru)!=0) {
+      printf("Exiting, cannot initialize RU Buffers\n");
+      exit(-1);
+    }*/
+    break;
+
+  case REMOTE_IF5: // the remote unit is IF5 RRU
+    ru->do_prach               = 0;
+    ru->feprx                  = (get_nprocs()<=2) ? fep_full : fep_full;                   // this is frequency-shift + DFTs
+    ru->feptx_prec             = feptx_prec;                 // need to do transmit Precoding + IDFTs
+    ru->feptx_ofdm             = (get_nprocs()<=2) ? feptx_ofdm : feptx_ofdm_2thread;                 // need to do transmit Precoding + IDFTs
+    if (ru->if_timing == synch_to_other) {
+      ru->fh_south_in          = fh_slave_south_in;                  // synchronize to master
+      ru->fh_south_out         = fh_if5_mobipass_south_out;          // use send_IF5 for mobipass
+      ru->fh_south_asynch_in   = fh_if5_south_asynch_in_mobipass;    // UL is asynchronous
+    }
+    else {
+      ru->fh_south_in          = fh_if5_south_in;     // synchronous IF5 reception
+      ru->fh_south_out         = fh_if5_south_out;    // synchronous IF5 transmission
+      ru->fh_south_asynch_in   = NULL;                // no asynchronous UL
+    }
+    ru->start_rf               = NULL;                 // no local RF
+    ru->start_if               = start_if;             // need to start if interface for IF5
+    ru->ifdevice.host_type     = RAU_HOST;
+    ru->ifdevice.eth_params    = &ru->eth_params;
+    ru->ifdevice.configure_rru = configure_ru;
+
+    ret = openair0_transport_load(&ru->ifdevice,&ru->openair0_cfg,&ru->eth_params);
+    printf("openair0_transport_init returns %d for ru_id %d\n", ret, ru->idx);
+    if (ret<0) {
+      printf("Exiting, cannot initialize transport protocol\n");
+      exit(-1);
+    }
+    break;
+
+  case REMOTE_IF4p5:
+    ru->do_prach               = 0;
+    ru->feprx                  = NULL;                // DFTs
+    ru->feptx_prec             = feptx_prec;          // Precoding operation
+    ru->feptx_ofdm             = NULL;                // no OFDM mod
+    ru->fh_south_in            = fh_if4p5_south_in;   // synchronous IF4p5 reception
+    ru->fh_south_out           = fh_if4p5_south_out;  // synchronous IF4p5 transmission
+    ru->fh_south_asynch_in     = (ru->if_timing == synch_to_other) ? fh_if4p5_south_in : NULL;                // asynchronous UL if synch_to_other
+    ru->fh_north_out           = NULL;
+    ru->fh_north_asynch_in     = NULL;
+    ru->start_rf               = NULL;                // no local RF
+    ru->start_if               = start_if;            // need to start if interface for IF4p5
+    ru->ifdevice.host_type     = RAU_HOST;
+    ru->ifdevice.eth_params    = &ru->eth_params;
+    ru->ifdevice.configure_rru = configure_ru;
+
+    ret = openair0_transport_load(&ru->ifdevice, &ru->openair0_cfg, &ru->eth_params);
+    printf("openair0_transport_init returns %d for ru_id %d\n", ret, ru->idx);
+    if (ret<0) {
+      printf("Exiting, cannot initialize transport protocol\n");
+      exit(-1);
+    }
+
+    malloc_IF4p5_buffer(ru);
+
+    break;
+
+  default:
+    LOG_E(PHY,"RU with invalid or unknown southbound interface type %d\n",ru->if_south);
+    break;
+  } // switch on interface type
+}
+
 extern void RCconfig_RU(void);
 
 void init_RU(char *rf_config_file) {
   
   int ru_id;
   RU_t *ru;
-  int ret;
   PHY_VARS_eNB *eNB0= (PHY_VARS_eNB *)NULL;
   int i;
   int CC_id;
@@ -1971,152 +2228,7 @@ void init_RU(char *rf_config_file) {
     }
     //    LOG_I(PHY,"Initializing RRU descriptor %d : (%s,%s,%d)\n",ru_id,ru_if_types[ru->if_south],eNB_timing[ru->if_timing],ru->function);
 
-
-    LOG_D(PHY,"ru->if_south:%d\n", ru->if_south);
-
-    switch (ru->if_south) {
-    case LOCAL_RF:   // this is an RU with integrated RF (RRU, eNB)
-      if (ru->function ==  NGFI_RRU_IF5) {                 // IF5 RRU
-	ru->do_prach              = 0;                      // no prach processing in RU
-	ru->fh_north_in           = NULL;                   // no shynchronous incoming fronthaul from north
-	ru->fh_north_out          = fh_if5_north_out;       // need only to do send_IF5  reception
-	ru->fh_south_out          = tx_rf;                  // send output to RF
-	ru->fh_north_asynch_in    = fh_if5_north_asynch_in; // TX packets come asynchronously 
-	ru->feprx                 = NULL;                   // nothing (this is a time-domain signal)
-	ru->feptx_ofdm            = NULL;                   // nothing (this is a time-domain signal)
-	ru->feptx_prec            = NULL;                   // nothing (this is a time-domain signal)
-	ru->start_if              = start_if;               // need to start the if interface for if5
-	ru->ifdevice.host_type    = RRU_HOST;
-	ru->rfdevice.host_type    = RRU_HOST;
-	ru->ifdevice.eth_params   = &ru->eth_params;
-    reset_meas(&ru->rx_fhaul);
-    reset_meas(&ru->tx_fhaul);
-    reset_meas(&ru->compression);
-    reset_meas(&ru->transport);
-
-	ret = openair0_transport_load(&ru->ifdevice,&ru->openair0_cfg,&ru->eth_params);
-	printf("openair0_transport_init returns %d for ru_id %d\n",ret,ru_id);
-	if (ret<0) {
-	  printf("Exiting, cannot initialize transport protocol\n");
-	  exit(-1);
-	}
-      }
-      else if (ru->function == NGFI_RRU_IF4p5) {
-	ru->do_prach              = 1;                        // do part of prach processing in RU
-	ru->fh_north_in           = NULL;                     // no synchronous incoming fronthaul from north
-	ru->fh_north_out          = fh_if4p5_north_out;       // send_IF4p5 on reception
-	ru->fh_south_out          = tx_rf;                    // send output to RF
-	ru->fh_north_asynch_in    = fh_if4p5_north_asynch_in; // TX packets come asynchronously
-	ru->feprx                 = (get_nprocs()<=2) ? fep_full :ru_fep_full_2thread;                 // RX DFTs
-	ru->feptx_ofdm            = (get_nprocs()<=2) ? feptx_ofdm : feptx_ofdm_2thread;               // this is fep with idft only (no precoding in RRU)
-	ru->feptx_prec            = NULL;
-	ru->start_if              = start_if;                 // need to start the if interface for if4p5
-	ru->ifdevice.host_type    = RRU_HOST;
-	ru->rfdevice.host_type    = RRU_HOST;
-	ru->ifdevice.eth_params   = &ru->eth_params;
-    reset_meas(&ru->rx_fhaul);
-    reset_meas(&ru->tx_fhaul);
-    reset_meas(&ru->compression);
-    reset_meas(&ru->transport);
-
-	ret = openair0_transport_load(&ru->ifdevice,&ru->openair0_cfg,&ru->eth_params);
-	printf("openair0_transport_init returns %d for ru_id %d\n",ret,ru_id);
-	if (ret<0) {
-	  printf("Exiting, cannot initialize transport protocol\n");
-	  exit(-1);
-	}
-	malloc_IF4p5_buffer(ru);
-      }
-      else if (ru->function == eNodeB_3GPP) {  
-	ru->do_prach             = 0;                       // no prach processing in RU            
-	ru->feprx                = (get_nprocs()<=2) ? fep_full : ru_fep_full_2thread;                // RX DFTs
-	ru->feptx_ofdm           = (get_nprocs()<=2) ? feptx_ofdm : feptx_ofdm_2thread;              // this is fep with idft and precoding
-	ru->feptx_prec           = feptx_prec;              // this is fep with idft and precoding
-	ru->fh_north_in          = NULL;                    // no incoming fronthaul from north
-	ru->fh_north_out         = NULL;                    // no outgoing fronthaul to north
-	ru->start_if             = NULL;                    // no if interface
-	ru->rfdevice.host_type   = RAU_HOST;
-      }
-      ru->fh_south_in            = rx_rf;                               // local synchronous RF RX
-      ru->fh_south_out           = tx_rf;                               // local synchronous RF TX
-      ru->start_rf               = start_rf;                            // need to start the local RF interface
-      printf("configuring ru_id %d (start_rf %p)\n",ru_id,start_rf);
-/*
-      if (ru->function == eNodeB_3GPP) { // configure RF parameters only for 3GPP eNodeB, we need to get them from RAU otherwise
-	fill_rf_config(ru,rf_config_file);      
-	init_frame_parms(&ru->frame_parms,1);
-	phy_init_RU(ru);
-      }
-
-      ret = openair0_device_load(&ru->rfdevice,&ru->openair0_cfg);
-      if (setup_RU_buffers(ru)!=0) {
-	printf("Exiting, cannot initialize RU Buffers\n");
-	exit(-1);
-      }
-      */
-      break;
-
-    case REMOTE_IF5: // the remote unit is IF5 RRU
-      ru->do_prach               = 0;
-      ru->feprx                  = (get_nprocs()<=2) ? fep_full : fep_full;                   // this is frequency-shift + DFTs
-      ru->feptx_prec             = feptx_prec;                 // need to do transmit Precoding + IDFTs 
-      ru->feptx_ofdm             = (get_nprocs()<=2) ? feptx_ofdm : feptx_ofdm_2thread;                 // need to do transmit Precoding + IDFTs 
-      if (ru->if_timing == synch_to_other) {
-	ru->fh_south_in          = fh_slave_south_in;                  // synchronize to master
-	ru->fh_south_out         = fh_if5_mobipass_south_out;          // use send_IF5 for mobipass
-	ru->fh_south_asynch_in   = fh_if5_south_asynch_in_mobipass;    // UL is asynchronous
-      }
-      else {
-	ru->fh_south_in          = fh_if5_south_in;     // synchronous IF5 reception
-	ru->fh_south_out         = fh_if5_south_out;    // synchronous IF5 transmission
-	ru->fh_south_asynch_in   = NULL;                // no asynchronous UL
-      }
-      ru->start_rf               = NULL;                 // no local RF
-      ru->start_if               = start_if;             // need to start if interface for IF5 
-      ru->ifdevice.host_type     = RAU_HOST;
-      ru->ifdevice.eth_params    = &ru->eth_params;
-      ru->ifdevice.configure_rru = configure_ru;
-
-      ret = openair0_transport_load(&ru->ifdevice,&ru->openair0_cfg,&ru->eth_params);
-      printf("openair0_transport_init returns %d for ru_id %d\n",ret,ru_id);
-      if (ret<0) {
-	printf("Exiting, cannot initialize transport protocol\n");
-	exit(-1);
-      }
-      break;
-
-    case REMOTE_IF4p5:
-      ru->do_prach               = 0;
-      ru->feprx                  = NULL;                // DFTs
-      ru->feptx_prec             = feptx_prec;          // Precoding operation
-      ru->feptx_ofdm             = NULL;                // no OFDM mod
-      ru->fh_south_in            = fh_if4p5_south_in;   // synchronous IF4p5 reception
-      ru->fh_south_out           = fh_if4p5_south_out;  // synchronous IF4p5 transmission
-      ru->fh_south_asynch_in     = (ru->if_timing == synch_to_other) ? fh_if4p5_south_in : NULL;                // asynchronous UL if synch_to_other
-      ru->fh_north_out           = NULL;
-      ru->fh_north_asynch_in     = NULL;
-      ru->start_rf               = NULL;                // no local RF
-      ru->start_if               = start_if;            // need to start if interface for IF4p5 
-      ru->ifdevice.host_type     = RAU_HOST;
-      ru->ifdevice.eth_params    = &ru->eth_params;
-      ru->ifdevice.configure_rru = configure_ru;
-
-      ret = openair0_transport_load(&ru->ifdevice, &ru->openair0_cfg, &ru->eth_params);
-      printf("openair0_transport_init returns %d for ru_id %d\n",ret,ru_id);
-      if (ret<0) {
-	printf("Exiting, cannot initialize transport protocol\n");
-	exit(-1);
-      }
-      
-      malloc_IF4p5_buffer(ru);
-      
-      break;
-
-    default:
-      LOG_E(PHY,"RU with invalid or unknown southbound interface type %d\n",ru->if_south);
-      break;
-    } // switch on interface type 
-
+    set_function_spec_param(ru);
     LOG_I(PHY,"Starting ru_thread %d\n",ru_id);
 
     init_RU_proc(ru);
@@ -2134,10 +2246,12 @@ void init_RU(char *rf_config_file) {
 
 
 
-void stop_ru(RU_t *ru) {
-
-  printf("Stopping RU %p processing threads\n",(void*)ru);
-  
+void stop_RU(int nb_ru)
+{
+  for (int inst = 0; inst < nb_ru; inst++) {
+    LOG_I(PHY, "Stopping RU %d processing threads\n", inst);
+    kill_RU_proc(inst);
+  }
 }
 
 
@@ -2256,13 +2370,13 @@ void RCconfig_RU(void) {
 	  RC.ru[j]->if_timing                    = synch_to_other;
 	  RC.ru[j]->eth_params.transp_preference = ETH_RAW_IF5_MOBIPASS;
 	}
-	RC.ru[j]->att_tx                         = *(RUParamList.paramarray[j][RU_ATT_TX_IDX].uptr); 
-	RC.ru[j]->att_rx                         = *(RUParamList.paramarray[j][RU_ATT_TX_IDX].uptr); 
       }  /* strcmp(local_rf, "yes") != 0 */
 
       RC.ru[j]->nb_tx                             = *(RUParamList.paramarray[j][RU_NB_TX_IDX].uptr);
       RC.ru[j]->nb_rx                             = *(RUParamList.paramarray[j][RU_NB_RX_IDX].uptr);
       
+      RC.ru[j]->att_tx                            = *(RUParamList.paramarray[j][RU_ATT_TX_IDX].uptr);
+      RC.ru[j]->att_rx                            = *(RUParamList.paramarray[j][RU_ATT_RX_IDX].uptr);
     }// j=0..num_rus
   } else {
     RC.nb_RU = 0;	    
diff --git a/targets/RT/USER/lte-softmodem.c b/targets/RT/USER/lte-softmodem.c
index 6044bab9fd648591d128d9677b2ee6c3ab0e4f93..f6789086b4ebc3fa4bc594de44f84b04a7269a82 100644
--- a/targets/RT/USER/lte-softmodem.c
+++ b/targets/RT/USER/lte-softmodem.c
@@ -518,6 +518,7 @@ void *l2l1_task(void *arg) {
       case TERMINATE_MESSAGE:
 	printf("received terminate message\n");
 	oai_exit=1;
+        start_eNB = 0;
 	itti_exit_task ();
 	break;
 
@@ -855,13 +856,11 @@ void wait_RUs(void) {
 
   // wait for all RUs to be configured over fronthaul
   pthread_mutex_lock(&RC.ru_mutex);
-
-
-
   while (RC.ru_mask>0) {
     pthread_cond_wait(&RC.ru_cond,&RC.ru_mutex);
     printf("RC.ru_mask:%02lx\n", RC.ru_mask);
   }
+  pthread_mutex_unlock(&RC.ru_mutex);
 
   LOG_I(PHY,"RUs configured\n");
 }
@@ -891,6 +890,131 @@ void wait_eNBs(void) {
   printf("eNB L1 are configured\n");
 }
 
+#if defined(ENABLE_ITTI)
+/*
+ * helper function to terminate a certain ITTI task
+ */
+void terminate_task(task_id_t task_id, module_id_t mod_id)
+{
+  LOG_I(ENB_APP, "sending TERMINATE_MESSAGE to task %s (%d)\n", itti_get_task_name(task_id), task_id);
+  MessageDef *msg;
+  msg = itti_alloc_new_message (ENB_APP, TERMINATE_MESSAGE);
+  itti_send_msg_to_task (task_id, ENB_MODULE_ID_TO_INSTANCE(mod_id), msg);
+}
+
+extern void  free_transport(PHY_VARS_eNB *);
+extern void  phy_free_RU(RU_t*);
+
+int stop_L1L2(module_id_t enb_id)
+{
+  LOG_W(ENB_APP, "stopping lte-softmodem\n");
+  oai_exit = 1;
+
+  if (!RC.ru) {
+    LOG_F(ENB_APP, "no RU configured\n");
+    return -1;
+  }
+
+  /* stop trx devices, multiple carrier currently not supported by RU */
+  if (RC.ru[enb_id]) {
+    if (RC.ru[enb_id]->rfdevice.trx_stop_func) {
+      RC.ru[enb_id]->rfdevice.trx_stop_func(&RC.ru[enb_id]->rfdevice);
+      LOG_I(ENB_APP, "turned off RU rfdevice\n");
+    } else {
+      LOG_W(ENB_APP, "can not turn off rfdevice due to missing trx_stop_func callback, proceding anyway!\n");
+    }
+    if (RC.ru[enb_id]->ifdevice.trx_stop_func) {
+      RC.ru[enb_id]->ifdevice.trx_stop_func(&RC.ru[enb_id]->ifdevice);
+      LOG_I(ENB_APP, "turned off RU ifdevice\n");
+    } else {
+      LOG_W(ENB_APP, "can not turn off ifdevice due to missing trx_stop_func callback, proceding anyway!\n");
+    }
+  } else {
+    LOG_W(ENB_APP, "no RU found for index %d\n", enb_id);
+    return -1;
+  }
+
+  /* these tasks need to pick up new configuration */
+  terminate_task(TASK_RRC_ENB, enb_id);
+  terminate_task(TASK_L2L1, enb_id);
+  LOG_I(ENB_APP, "calling kill_eNB_proc() for instance %d\n", enb_id);
+  kill_eNB_proc(enb_id);
+  LOG_I(ENB_APP, "calling kill_RU_proc() for instance %d\n", enb_id);
+  kill_RU_proc(enb_id);
+  oai_exit = 0;
+  for (int cc_id = 0; cc_id < RC.nb_CC[enb_id]; cc_id++) {
+    free_transport(RC.eNB[enb_id][cc_id]);
+    phy_free_lte_eNB(RC.eNB[enb_id][cc_id]);
+  }
+  phy_free_RU(RC.ru[enb_id]);
+  free_lte_top();
+  return 0;
+}
+
+/*
+ * Restart the lte-softmodem after it has been soft-stopped with stop_L1L2()
+ */
+int restart_L1L2(module_id_t enb_id)
+{
+  RU_t *ru = RC.ru[enb_id];
+  int cc_id;
+  MessageDef *msg_p = NULL;
+
+  LOG_W(ENB_APP, "restarting lte-softmodem\n");
+
+  /* block threads */
+  sync_var = -1;
+
+  for (cc_id = 0; cc_id < RC.nb_L1_CC[enb_id]; cc_id++) {
+    RC.eNB[enb_id][cc_id]->configured = 0;
+  }
+
+  RC.ru_mask |= (1 << ru->idx);
+  /* copy the changed frame parameters to the RU */
+  /* TODO this should be done for all RUs associated to this eNB */
+  memcpy(&ru->frame_parms, &RC.eNB[enb_id][0]->frame_parms, sizeof(LTE_DL_FRAME_PARMS));
+  set_function_spec_param(RC.ru[enb_id]);
+
+  LOG_I(ENB_APP, "attempting to create ITTI tasks\n");
+  if (itti_create_task (TASK_RRC_ENB, rrc_enb_task, NULL) < 0) {
+    LOG_E(RRC, "Create task for RRC eNB failed\n");
+    return -1;
+  } else {
+    LOG_I(RRC, "Re-created task for RRC eNB successfully\n");
+  }
+  if (itti_create_task (TASK_L2L1, l2l1_task, NULL) < 0) {
+    LOG_E(PDCP, "Create task for L2L1 failed\n");
+    return -1;
+  } else {
+    LOG_I(PDCP, "Re-created task for L2L1 successfully\n");
+  }
+
+  /* pass a reconfiguration request which will configure everything down to
+   * RC.eNB[i][j]->frame_parms, too */
+  msg_p = itti_alloc_new_message(TASK_ENB_APP, RRC_CONFIGURATION_REQ);
+  RRC_CONFIGURATION_REQ(msg_p) = RC.rrc[enb_id]->configuration;
+  itti_send_msg_to_task(TASK_RRC_ENB, ENB_MODULE_ID_TO_INSTANCE(enb_id), msg_p);
+
+  /* TODO XForms might need to be restarted, but it is currently (09/02/18)
+   * broken, so we cannot test it */
+
+  wait_eNBs();
+  init_RU_proc(ru);
+  ru->rf_map.card = 0;
+  ru->rf_map.chain = 0; /* CC_id + chain_offset;*/
+  wait_RUs();
+  init_eNB_afterRU();
+
+  printf("Sending sync to all threads\n");
+  pthread_mutex_lock(&sync_mutex);
+  sync_var=0;
+  pthread_cond_broadcast(&sync_cond);
+  pthread_mutex_unlock(&sync_mutex);
+
+  return 0;
+}
+#endif
+
 static inline void wait_nfapi_init(char *thread_name) {
 
   printf( "waiting for NFAPI PNF connection and population of global structure (%s)\n",thread_name);
@@ -1063,7 +1187,6 @@ int main( int argc, char **argv )
 
   for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
 
-
     if (UE_flag==1) {     
       NB_UE_INST=1;     
       NB_INST=1;     
@@ -1196,10 +1319,15 @@ int main( int argc, char **argv )
   }
 #endif
 
-    // init UE_PF_PO and mutex lock
-    pthread_mutex_init(&ue_pf_po_mutex, NULL);
-    memset (&UE_PF_PO[0][0], 0, sizeof(UE_PF_PO_t)*NUMBER_OF_UE_MAX*MAX_NUM_CCs);
-  
+  /* Start the agent. If it is turned off in the configuration, it won't start */
+  RCconfig_flexran();
+  for (i = 0; i < RC.nb_L1_inst; i++) {
+    flexran_agent_start(i);
+  }
+
+  // init UE_PF_PO and mutex lock
+  pthread_mutex_init(&ue_pf_po_mutex, NULL);
+  memset (&UE_PF_PO[0][0], 0, sizeof(UE_PF_PO_t)*NUMBER_OF_UE_MAX*MAX_NUM_CCs);
   
   mlockall(MCL_CURRENT | MCL_FUTURE);
   
@@ -1458,7 +1586,20 @@ int main( int argc, char **argv )
   // cleanup
   if (UE_flag == 1) {
   } else {
-    stop_eNB(1);
+    stop_eNB(NB_eNB_INST);
+    stop_RU(NB_RU);
+    /* release memory used by the RU/eNB threads (incomplete), after all
+     * threads have been stopped (they partially use the same memory) */
+    for (int inst = 0; inst < NB_eNB_INST; inst++) {
+      for (int cc_id = 0; cc_id < RC.nb_CC[inst]; cc_id++) {
+        free_transport(RC.eNB[inst][cc_id]);
+        phy_free_lte_eNB(RC.eNB[inst][cc_id]);
+      }
+    }
+    for (int inst = 0; inst < NB_RU; inst++) {
+      phy_free_RU(RC.ru[inst]);
+    }
+    free_lte_top();
   }
 
 
@@ -1491,6 +1632,8 @@ int main( int argc, char **argv )
     terminate_opt();
   
   logClean();
+
+  printf("Bye.\n");
   
   return 0;
 }
diff --git a/targets/RT/USER/lte-softmodem.h b/targets/RT/USER/lte-softmodem.h
index e1624760385f720b99885622b4e263c54b41b054..5cc4d5fea2d70208ba76151f08240671628f137c 100644
--- a/targets/RT/USER/lte-softmodem.h
+++ b/targets/RT/USER/lte-softmodem.h
@@ -31,6 +31,8 @@
 #include "PHY/defs.h"
 #include "SIMULATION/ETH_TRANSPORT/proto.h"
 
+#include "flexran_agent.h"
+
 #if defined(ENABLE_ITTI)
 #if defined(ENABLE_USE_MME)
 #include "s1ap_eNB.h"
@@ -244,6 +246,10 @@ extern void kill_eNB_proc(int inst);
 
 // In lte-ru.c
 extern void init_RU(const char*);
+extern void init_RU_proc(RU_t *ru);
+extern void stop_RU(int nb_ru);
+extern void kill_RU_proc(int inst);
+extern void set_function_spec_param(RU_t *ru);
 
 // In lte-ue.c
 extern int setup_ue_buffers(PHY_VARS_UE **phy_vars_ue, openair0_config_t *openair0_cfg);
@@ -264,4 +270,7 @@ PHY_VARS_UE* init_ue_vars(LTE_DL_FRAME_PARMS *frame_parms,
                           uint8_t abstraction_flag);
 void init_eNB_afterRU(void);
 
+extern int stop_L1L2(module_id_t enb_id);
+extern int restart_L1L2(module_id_t enb_id);
+
 #endif
diff --git a/targets/SIMU/USER/event_handler.c b/targets/SIMU/USER/event_handler.c
index 7c9f6b73fc8186c29b1d0be642df36857a240289..dbdce23df2ab4e568822a64476bb6d2396f7ac06 100644
--- a/targets/SIMU/USER/event_handler.c
+++ b/targets/SIMU/USER/event_handler.c
@@ -524,9 +524,9 @@ void update_mac(Event_t event)
 
               for(j=0; j<MAX_NUM_LCID; j++) {
                 oai_emulation->mac_config[i].max_allowed_rbs[j]= mac_config[i].max_allowed_rbs[j];
-                UE_list->UE_sched_ctrl[i].max_allowed_rbs[j] = oai_emulation->mac_config[i].max_allowed_rbs[j];
+                UE_list->UE_sched_ctrl[i].max_rbs_allowed_slice[j][0] = oai_emulation->mac_config[i].max_allowed_rbs[j];
                 LOG_I(EMU,"max_allowed_rbs UE %d LCID %d:",i,j);
-                LOG_I(EMU,"%" PRIu16 "\n",UE_list->UE_sched_ctrl[i].max_allowed_rbs[j]);
+                LOG_I(EMU,"%" PRIu16 "\n",UE_list->UE_sched_ctrl[i].max_rbs_allowed_slice[j][0]);
               }
             }
 
@@ -646,9 +646,9 @@ void update_mac(Event_t event)
           if(mac_config->max_allowed_rbs !=NULL) {
 
             oai_emulation->mac_config[i].max_allowed_rbs[j]= mac_config[i].max_allowed_rbs[j];
-            UE_list->UE_sched_ctrl[i].max_allowed_rbs[j] = oai_emulation->mac_config[i].max_allowed_rbs[j];
+            UE_list->UE_sched_ctrl[i].max_rbs_allowed_slice[j][0] = oai_emulation->mac_config[i].max_allowed_rbs[j];
             LOG_I(EMU,"max_allowed_rbs UE %d LCID %d:",i,j);
-            LOG_I(EMU,"%" PRIu16 "\n",UE_list->UE_sched_ctrl[i].max_allowed_rbs[j]);
+            LOG_I(EMU,"%" PRIu16 "\n",UE_list->UE_sched_ctrl[i].max_rbs_allowed_slice[j][0]);
 
           }
 
@@ -951,18 +951,18 @@ void update_mac(Event_t event)
 
               if(&mac_config[i].max_allowed_rbs[j]!=NULL) {
                 oai_emulation->mac_config[i].max_allowed_rbs[j]= mac_config[i].max_allowed_rbs[j];
-                UE_list->UE_sched_ctrl[i].max_allowed_rbs[j] = oai_emulation->mac_config[i].max_allowed_rbs[j];
+                UE_list->UE_sched_ctrl[i].max_rbs_allowed_slice[j][0] = oai_emulation->mac_config[i].max_allowed_rbs[j];
                 LOG_I(EMU,"max_allowed_rbs UE %d LCID %d:",i,j);
-                LOG_I(EMU,"%" PRIu16 "\n",UE_list->UE_sched_ctrl[i].max_allowed_rbs[j]);
+                LOG_I(EMU,"%" PRIu16 "\n",UE_list->UE_sched_ctrl[i].max_rbs_allowed_slice[j][0]);
               }
             }
           }
         }
       } else {
         oai_emulation->mac_config[event.ue].max_allowed_rbs[event.lcid]= mac_config[event.ue].max_allowed_rbs[event.lcid];
-        UE_list->UE_sched_ctrl[event.ue].max_allowed_rbs[event.lcid] = oai_emulation->mac_config[event.ue].max_allowed_rbs[event.lcid];
+        UE_list->UE_sched_ctrl[event.ue].max_rbs_allowed_slice[event.lcid][0] = oai_emulation->mac_config[event.ue].max_allowed_rbs[event.lcid];
         LOG_I(EMU,"max_allowed_rbs UE %d LCID %d:",event.ue,event.lcid);
-        LOG_I(EMU,"%" PRIu16 "\n",UE_list->UE_sched_ctrl[event.ue].max_allowed_rbs[event.lcid]);
+        LOG_I(EMU,"%" PRIu16 "\n",UE_list->UE_sched_ctrl[event.ue].max_rbs_allowed_slice[event.lcid][0]);
       }
 
     } else if(!strcmp((char *) event.key, "max_mcs") && event.value!=NULL && validate_mac(event)) {
diff --git a/targets/SIMU/USER/oaisim.c b/targets/SIMU/USER/oaisim.c
index 7d8b33f12c0b9f9feb729958ac9873a440f1b66a..3a282e1d44ecaf3158d57b03889780f2eafa417e 100644
--- a/targets/SIMU/USER/oaisim.c
+++ b/targets/SIMU/USER/oaisim.c
@@ -76,9 +76,7 @@ uint8_t config_smbv = 0;
 char smbv_ip[16];
 #endif
 
-#if defined(FLEXRAN_AGENT_SB_IF)
-#   include "flexran_agent.h"
-#endif
+#include "flexran_agent.h"
 
 
 #include "oaisim_functions.h"
@@ -990,6 +988,23 @@ l2l1_task (void *args_p)
   return NULL;
 }
 
+/*
+ * The following two functions are meant to restart *the lte-softmodem* and are
+ * here to make oaisim compile. A restart command from the controller will be
+ * ignored in oaisim.
+ */
+int stop_L1L2(int enb_id)
+{
+  LOG_W(FLEXRAN_AGENT, "stop_L1L2() not supported in oaisim\n");
+  return 0;
+}
+
+int restart_L1L2(int enb_id)
+{
+  LOG_W(FLEXRAN_AGENT, "restart_L1L2() not supported in oaisim\n");
+  return 0;
+}
+
 #if T_TRACER
 int T_wait = 1;       /* by default we wait for the tracer */
 int T_port = 2021;    /* default port to listen to to wait for the tracer */