From d4cdda31abf878c3944c967e7332522c1e6cbc7b Mon Sep 17 00:00:00 2001
From: Raymond Knopp <florian.kaltenberger@eurecom.fr>
Date: Fri, 19 Feb 2016 15:42:32 +0100
Subject: [PATCH] Final changes for UE context management

---
 openair1/PHY/LTE_TRANSPORT/dlsch_coding.c     | 50 +++++++--------
 openair1/PHY/LTE_TRANSPORT/proto.h            |  4 +-
 openair1/PHY/LTE_TRANSPORT/ulsch_decoding.c   |  2 +-
 openair1/PHY/impl_defs_top.h                  |  2 +-
 openair1/SCHED/defs.h                         |  2 +-
 openair1/SCHED/phy_procedures_lte_eNb.c       | 64 ++++++++++---------
 openair2/LAYER2/MAC/eNB_scheduler.c           |  6 +-
 .../LAYER2/MAC/eNB_scheduler_primitives.c     | 48 +++++++-------
 openair2/LAYER2/MAC/main.c                    |  1 +
 openair2/LAYER2/MAC/proto.h                   |  2 +-
 openair2/PHY_INTERFACE/defs.h                 |  2 +-
 openair2/RRC/LITE/L2_interface.c              | 38 +++++++++--
 openair2/RRC/LITE/defs.h                      |  1 +
 openair2/RRC/LITE/proto.h                     |  6 +-
 openair2/RRC/LITE/rrc_common.c                | 19 +++++-
 openair2/RRC/LITE/rrc_eNB.c                   | 56 ++++++++--------
 16 files changed, 172 insertions(+), 131 deletions(-)

diff --git a/openair1/PHY/LTE_TRANSPORT/dlsch_coding.c b/openair1/PHY/LTE_TRANSPORT/dlsch_coding.c
index 95af038ab0..01102636fc 100644
--- a/openair1/PHY/LTE_TRANSPORT/dlsch_coding.c
+++ b/openair1/PHY/LTE_TRANSPORT/dlsch_coding.c
@@ -44,6 +44,7 @@
 #include "PHY/CODING/extern.h"
 #include "PHY/CODING/lte_interleaver_inline.h"
 #include "PHY/LTE_TRANSPORT/defs.h"
+#include "PHY/LTE_TRANSPORT/proto.h"
 #include "defs.h"
 #include "UTIL/LOG/vcd_signal_dumper.h"
 
@@ -91,11 +92,11 @@ void free_eNB_dlsch(LTE_eNB_DLSCH_t *dlsch)
 #endif
 
         for (r=0; r<MAX_NUM_DLSCH_SEGMENTS; r++) {
-
+	  
 #ifdef DEBUG_DLSCH_FREE
           msg("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;
@@ -104,17 +105,17 @@ 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->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)
@@ -204,28 +205,28 @@ LTE_eNB_DLSCH_t *new_eNB_dlsch(unsigned char Kmimo,unsigned char Mdlharq,uint32_
       for (i=0; i<Mdlharq; i++) {
         dlsch->harq_processes[i]->round=0;
 
-        if (abstraction_flag==0) {
-          for (j=0; j<96; j++)
-            for (r=0; r<MAX_NUM_DLSCH_SEGMENTS/bw_scaling; r++) {
-        //      printf("dlsch->harq_processes[%d]->d[%d] %p\n",i,r,dlsch->harq_processes[i]->d[r]);
-              dlsch->harq_processes[i]->d[r][j] = LTE_NULL;
-            }
-        }
+	for (j=0; j<96; j++)
+	  for (r=0; r<MAX_NUM_DLSCH_SEGMENTS/bw_scaling; r++) {
+	    //      printf("dlsch->harq_processes[%d]->d[%d] %p\n",i,r,dlsch->harq_processes[i]->d[r]);
+	    if (dlsch->harq_processes[i]->d[r])
+	      dlsch->harq_processes[i]->d[r][j] = LTE_NULL;
+	  }
+        
       }
 
       return(dlsch);
     }
   }
 
-  LOG_D(PHY, "new_eNB_dlsch exit flag %d, size of  %ld\n",
-        exit_flag, sizeof(LTE_eNB_DLSCH_t));
+  LOG_D(PHY,"new_eNB_dlsch exit flag %d, size of  %ld\n",
+	exit_flag, sizeof(LTE_eNB_DLSCH_t));
   free_eNB_dlsch(dlsch);
   return(NULL);
 
 
 }
 
-void clean_eNb_dlsch(LTE_eNB_DLSCH_t *dlsch, uint8_t abstraction_flag)
+void clean_eNb_dlsch(LTE_eNB_DLSCH_t *dlsch)
 {
 
   unsigned char Mdlharq;
@@ -245,12 +246,11 @@ void clean_eNb_dlsch(LTE_eNB_DLSCH_t *dlsch, uint8_t abstraction_flag)
         dlsch->harq_processes[i]->status = 0;
         dlsch->harq_processes[i]->round  = 0;
 
-        if (abstraction_flag==0) {
-          for (j=0; j<96; j++)
-            for (r=0; r<MAX_NUM_DLSCH_SEGMENTS; r++)
-	      if (dlsch->harq_processes[i]->d[r])
-		dlsch->harq_processes[i]->d[r][j] = LTE_NULL;
-        }
+	for (j=0; j<96; j++)
+	  for (r=0; r<MAX_NUM_DLSCH_SEGMENTS; r++)
+	    if (dlsch->harq_processes[i]->d[r])
+	      dlsch->harq_processes[i]->d[r][j] = LTE_NULL;
+        
       }
     }
   }
diff --git a/openair1/PHY/LTE_TRANSPORT/proto.h b/openair1/PHY/LTE_TRANSPORT/proto.h
index 9afea790b3..15a8f53f54 100644
--- a/openair1/PHY/LTE_TRANSPORT/proto.h
+++ b/openair1/PHY/LTE_TRANSPORT/proto.h
@@ -54,7 +54,7 @@
 */
 void free_eNB_dlsch(LTE_eNB_DLSCH_t *dlsch);
 
-void clean_eNb_dlsch(LTE_eNB_DLSCH_t *dlsch, uint8_t abstraction_flag);
+void clean_eNb_dlsch(LTE_eNB_DLSCH_t *dlsch);
 
 /** \fn new_eNB_dlsch(uint8_t Kmimo,uint8_t Mdlharq,uint32_t Nsoft,uint8_t abstraction_flag)
     \brief This function allocates structures for a particular DLSCH at eNB
@@ -85,7 +85,7 @@ void free_ue_dlsch(LTE_UE_DLSCH_t *dlsch);
 LTE_UE_DLSCH_t *new_ue_dlsch(uint8_t Kmimo,uint8_t Mdlharq,uint32_t Nsoft,uint8_t max_turbo_iterations,uint8_t N_RB_DL, uint8_t abstraction_flag);
 
 
-void clean_eNb_ulsch(LTE_eNB_ULSCH_t *ulsch, uint8_t abstraction_flag);
+void clean_eNb_ulsch(LTE_eNB_ULSCH_t *ulsch);
 
 void free_ue_ulsch(LTE_UE_ULSCH_t *ulsch);
 
diff --git a/openair1/PHY/LTE_TRANSPORT/ulsch_decoding.c b/openair1/PHY/LTE_TRANSPORT/ulsch_decoding.c
index 25dae3ed63..ca4e8be933 100644
--- a/openair1/PHY/LTE_TRANSPORT/ulsch_decoding.c
+++ b/openair1/PHY/LTE_TRANSPORT/ulsch_decoding.c
@@ -179,7 +179,7 @@ LTE_eNB_ULSCH_t *new_eNB_ulsch(uint8_t Mdlharq,uint8_t max_turbo_iterations,uint
   return(NULL);
 }
 
-void clean_eNb_ulsch(LTE_eNB_ULSCH_t *ulsch, uint8_t abstraction_flag)
+void clean_eNb_ulsch(LTE_eNB_ULSCH_t *ulsch)
 {
 
   unsigned char Mdlharq;
diff --git a/openair1/PHY/impl_defs_top.h b/openair1/PHY/impl_defs_top.h
index e55e28e3be..4b0edcff29 100755
--- a/openair1/PHY/impl_defs_top.h
+++ b/openair1/PHY/impl_defs_top.h
@@ -256,7 +256,7 @@
 #define AMP_OVER_2 (AMP>>1)
 
 /// Threshold for PUCCH Format 1 detection
-#define PUCCH1_THRES 10
+#define PUCCH1_THRES 7
 /// Threshold for PUCCH Format 1a/1b detection
 #define PUCCH1a_THRES 4
 
diff --git a/openair1/SCHED/defs.h b/openair1/SCHED/defs.h
index 8692a31f12..af6acca880 100644
--- a/openair1/SCHED/defs.h
+++ b/openair1/SCHED/defs.h
@@ -405,7 +405,7 @@ void remove_harq_pid_from_freelist(LTE_eNB_DLSCH_t *DLSCH_ptr, int harq_pid);
 
 int8_t find_ue(uint16_t rnti, PHY_VARS_eNB *phy_vars_eNB);
 int32_t add_ue(int16_t rnti, PHY_VARS_eNB *phy_vars_eNB);
-int32_t remove_ue(uint16_t rnti, PHY_VARS_eNB *phy_vars_eNB,uint8_t abstraction_flag);
+int mac_phy_remove_ue(module_id_t Mod_idP,rnti_t rnti);
 
 void process_timing_advance(module_id_t Mod_id,uint8_t CC_id,int16_t timing_advance);
 void process_timing_advance_rar(PHY_VARS_UE *phy_vars_ue,uint16_t timing_advance);
diff --git a/openair1/SCHED/phy_procedures_lte_eNb.c b/openair1/SCHED/phy_procedures_lte_eNb.c
index a7796a9b69..ca1e8a3d37 100755
--- a/openair1/SCHED/phy_procedures_lte_eNb.c
+++ b/openair1/SCHED/phy_procedures_lte_eNb.c
@@ -198,41 +198,43 @@ int32_t add_ue(int16_t rnti, PHY_VARS_eNB *phy_vars_eNB)
   return(-1);
 }
 
-int32_t remove_ue(uint16_t rnti, PHY_VARS_eNB *phy_vars_eNB, uint8_t abstraction_flag)
-{
+int mac_phy_remove_ue(module_id_t Mod_idP,rnti_t rntiP) {
   uint8_t i;
-  int j;
-
-  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
-    if ((phy_vars_eNB->dlsch_eNB[i]==NULL) || (phy_vars_eNB->ulsch_eNB[i]==NULL)) {
-      MSC_LOG_EVENT(MSC_PHY_ENB, "0 Failed remove ue %"PRIx16" (ENOMEM)", rnti);
-      LOG_E(PHY,"Can't remove UE, not enough memory allocated\n");
-      return(-1);
-    } else {
-      if (phy_vars_eNB->eNB_UE_stats[i].crnti==rnti) {
-        MSC_LOG_EVENT(MSC_PHY_ENB, "0 Removed ue %"PRIx16" ", rnti);
+  int j,CC_id;
+  PHY_VARS_eNB *phy_vars_eNB;
+
+  for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
+    phy_vars_eNB = PHY_vars_eNB_g[Mod_idP][CC_id];
+    for (i=0; i<NUMBER_OF_UE_MAX; i++) {
+      if ((phy_vars_eNB->dlsch_eNB[i]==NULL) || (phy_vars_eNB->ulsch_eNB[i]==NULL)) {
+	MSC_LOG_EVENT(MSC_PHY_ENB, "0 Failed remove ue %"PRIx16" (ENOMEM)", rnti);
+	LOG_E(PHY,"Can't remove UE, not enough memory allocated\n");
+	return(-1);
+      } else {
+	if (phy_vars_eNB->eNB_UE_stats[i].crnti==rntiP) {
+	  MSC_LOG_EVENT(MSC_PHY_ENB, "0 Removed ue %"PRIx16" ", rntiP);
 #ifdef DEBUG_PHY_PROC
-	LOG_I(PHY,"eNB %d removing UE %d with rnti %x\n",phy_vars_eNB->Mod_id,i,rnti);
+	  LOG_I(PHY,"eNB %d removing UE %d with rnti %x\n",phy_vars_eNB->Mod_id,i,rnti);
 #endif
-        //msg("[PHY] UE_id %d\n",i);
-        clean_eNb_dlsch(phy_vars_eNB->dlsch_eNB[i][0], abstraction_flag);
-        clean_eNb_ulsch(phy_vars_eNB->ulsch_eNB[i],abstraction_flag);
-        //phy_vars_eNB->eNB_UE_stats[i].crnti = 0;
-        memset(&phy_vars_eNB->eNB_UE_stats[i],0,sizeof(LTE_eNB_UE_stats));
-        //  mac_exit_wrapper("Removing UE");
-
-        /* clear the harq pid freelist */
-        phy_vars_eNB->dlsch_eNB[i][0]->head_freelist = 0;
-        phy_vars_eNB->dlsch_eNB[i][0]->tail_freelist = 0;
-        for (j = 0; j < 8; j++)
-          put_harq_pid_in_freelist(phy_vars_eNB->dlsch_eNB[i][0], j);
-
-        return(i);
+	  //msg("[PHY] UE_id %d\n",i);
+	  clean_eNb_dlsch(phy_vars_eNB->dlsch_eNB[i][0]);
+	  clean_eNb_ulsch(phy_vars_eNB->ulsch_eNB[i]);
+	  //phy_vars_eNB->eNB_UE_stats[i].crnti = 0;
+	  memset(&phy_vars_eNB->eNB_UE_stats[i],0,sizeof(LTE_eNB_UE_stats));
+	  //  mac_exit_wrapper("Removing UE");
+	  
+	  /* clear the harq pid freelist */
+	  phy_vars_eNB->dlsch_eNB[i][0]->head_freelist = 0;
+	  phy_vars_eNB->dlsch_eNB[i][0]->tail_freelist = 0;
+	  for (j = 0; j < 8; j++)
+	    put_harq_pid_in_freelist(phy_vars_eNB->dlsch_eNB[i][0], j);
+	  
+	  return(i);
+	}
       }
     }
   }
-
-  MSC_LOG_EVENT(MSC_PHY_ENB, "0 Failed remove ue %"PRIx16" (not found)", rnti);
+  MSC_LOG_EVENT(MSC_PHY_ENB, "0 Failed remove ue %"PRIx16" (not found)", rntiP);
   return(-1);
 }
 
@@ -3523,7 +3525,7 @@ void phy_procedures_eNB_RX(const unsigned char sched_subframe,PHY_VARS_eNB *phy_
                                       frame,
                                       phy_vars_eNB->eNB_UE_stats[i].crnti);
 #endif
-            remove_ue(phy_vars_eNB->eNB_UE_stats[i].crnti,phy_vars_eNB,abstraction_flag);
+            mac_phy_remove_ue(phy_vars_eNB->Mod_id,phy_vars_eNB->eNB_UE_stats[i].crnti);
             phy_vars_eNB->ulsch_eNB[(uint32_t)i]->Msg3_active = 0;
             //phy_vars_eNB->ulsch_eNB[i]->harq_processes[harq_pid]->phich_active = 0;
 
@@ -3670,7 +3672,7 @@ void phy_procedures_eNB_RX(const unsigned char sched_subframe,PHY_VARS_eNB *phy_
                                       phy_vars_eNB->CC_id,
                                       frame,
                                       phy_vars_eNB->eNB_UE_stats[i].crnti);
-            remove_ue(phy_vars_eNB->eNB_UE_stats[i].crnti,phy_vars_eNB,abstraction_flag);
+            mac_phy_remove_ue(phy_vars_eNB->Mod_id,phy_vars_eNB->eNB_UE_stats[i].crnti);
             phy_vars_eNB->ulsch_eNB[(uint32_t)i]->Msg3_active = 0;
           }
 
diff --git a/openair2/LAYER2/MAC/eNB_scheduler.c b/openair2/LAYER2/MAC/eNB_scheduler.c
index d1d08390d7..dbc094034b 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler.c
@@ -127,9 +127,9 @@ void eNB_dlsch_ulsch_scheduler(module_id_t module_idP,uint8_t cooperation_flag,
 
     // increment this, it is cleared when we receive an sdu
     eNB_mac_inst[module_idP].UE_list.UE_sched_ctrl[i].ul_inactivity_timer++;
-
+    
     if (mac_xface->get_eNB_UE_stats(module_idP, CC_id, rnti)==NULL) {
-      mac_remove_ue(module_idP, i, frameP, subframeP);
+      //      mac_remove_ue(module_idP, i, frameP, subframeP);
     }
     else {
       // check uplink failure
@@ -210,7 +210,7 @@ void eNB_dlsch_ulsch_scheduler(module_id_t module_idP,uint8_t cooperation_flag,
 			  0);
 	}
 	else { // ra_pdcch_sent==1
-	  LOG_I(MAC,"UE %d rnti %x: sent PDCCH order for RAPROC waiting (failure timer %d) \n",i,rnti,UE_list->UE_sched_ctrl[i].ul_failure_timer);	    	    
+	  LOG_D(MAC,"UE %d rnti %x: sent PDCCH order for RAPROC waiting (failure timer %d) \n",i,rnti,UE_list->UE_sched_ctrl[i].ul_failure_timer);	    	    
 	  if ((UE_list->UE_sched_ctrl[i].ul_failure_timer % 40) == 0)
 	    UE_list->UE_sched_ctrl[i].ra_pdcch_order_sent=0; // resend every 4 frames	      
 	}
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_primitives.c b/openair2/LAYER2/MAC/eNB_scheduler_primitives.c
index e23f007a07..7162501f89 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler_primitives.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_primitives.c
@@ -287,44 +287,38 @@ int add_new_ue(module_id_t mod_idP, int cc_idP, rnti_t rntiP,int harq_pidP)
 }
 
 //------------------------------------------------------------------------------
-int mac_remove_ue(module_id_t mod_idP, int ue_idP, int frameP, sub_frame_t subframeP)
+int rrc_mac_remove_ue(module_id_t mod_idP,rnti_t rntiP) 
 //------------------------------------------------------------------------------
 {
 
   int prev,i, ret=-1;
 
-  rnti_t  rnti;
+
   UE_list_t *UE_list = &eNB_mac_inst[mod_idP].UE_list;
-  int pCC_id = UE_PCCID(mod_idP,ue_idP);
+  int UE_id = find_UE_id(mod_idP,rntiP);
+  int pCC_id = UE_PCCID(mod_idP,UE_id);
 
-  rnti = UE_list->UE_template[pCC_id][ue_idP].rnti;
-  LOG_I(MAC,"Removing UE %d from Primary CC_id %d (rnti %x)\n",ue_idP,pCC_id, rnti);
+  LOG_I(MAC,"Removing UE %d from Primary CC_id %d (rnti %x)\n",UE_id,pCC_id, rntiP);
   dump_ue_list(UE_list,0);
 
   // clear all remaining pending transmissions
-  UE_list->UE_template[pCC_id][ue_idP].bsr_info[LCGID0]  = 0;
-  UE_list->UE_template[pCC_id][ue_idP].bsr_info[LCGID1]  = 0;
-  UE_list->UE_template[pCC_id][ue_idP].bsr_info[LCGID2]  = 0;
-  UE_list->UE_template[pCC_id][ue_idP].bsr_info[LCGID3]  = 0;
-
-  UE_list->UE_template[pCC_id][ue_idP].ul_SR             = 0;
-  UE_list->UE_template[pCC_id][ue_idP].rnti              = NOT_A_RNTI;
-  UE_list->UE_template[pCC_id][ue_idP].ul_active         = FALSE;
-  eNB_ulsch_info[mod_idP][pCC_id][ue_idP].rnti                        = NOT_A_RNTI;
-  eNB_ulsch_info[mod_idP][pCC_id][ue_idP].status                      = S_UL_NONE;
-  eNB_dlsch_info[mod_idP][pCC_id][ue_idP].rnti                        = NOT_A_RNTI;
-  eNB_dlsch_info[mod_idP][pCC_id][ue_idP].status                      = S_DL_NONE;
-
-  rrc_eNB_free_UE(
-    mod_idP,
-    rnti,
-    frameP,
-    subframeP);
+  UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID0]  = 0;
+  UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID1]  = 0;
+  UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID2]  = 0;
+  UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID3]  = 0;
+
+  UE_list->UE_template[pCC_id][UE_id].ul_SR             = 0;
+  UE_list->UE_template[pCC_id][UE_id].rnti              = NOT_A_RNTI;
+  UE_list->UE_template[pCC_id][UE_id].ul_active         = FALSE;
+  eNB_ulsch_info[mod_idP][pCC_id][UE_id].rnti                        = NOT_A_RNTI;
+  eNB_ulsch_info[mod_idP][pCC_id][UE_id].status                      = S_UL_NONE;
+  eNB_dlsch_info[mod_idP][pCC_id][UE_id].rnti                        = NOT_A_RNTI;
+  eNB_dlsch_info[mod_idP][pCC_id][UE_id].status                      = S_DL_NONE;
 
   prev = UE_list->head;
 
   for (i=UE_list->head; i>=0; i=UE_list->next[i]) {
-    if (i == ue_idP) {
+    if (i == UE_id) {
       // link prev to next in Active list
       //if (prev==UE_list->head)
       if (i==UE_list->head) {
@@ -349,7 +343,7 @@ int mac_remove_ue(module_id_t mod_idP, int ue_idP, int frameP, sub_frame_t subfr
   prev = UE_list->head_ul;
 
   for (i=UE_list->head_ul; i>=0; i=UE_list->next_ul[i]) {
-    if (i == ue_idP) {
+    if (i == UE_id) {
       // link prev to next in Active list
       if (prev==UE_list->head_ul) {
         UE_list->head_ul = UE_list->next_ul[i];
@@ -366,11 +360,13 @@ int mac_remove_ue(module_id_t mod_idP, int ue_idP, int frameP, sub_frame_t subfr
     prev=i;
   }
 
+  mac_phy_remove_ue(mod_idP,rntiP);
+
   if (ret == 0) {
     return (0);
   }
 
-  LOG_E(MAC,"error in mac_remove_ue(), could not find previous to %d in UE_list, should never happen, Dumping UE list\n",ue_idP);
+  LOG_E(MAC,"error in mac_remove_ue(), could not find previous to %d in UE_list, should never happen, Dumping UE list\n",UE_id);
   dump_ue_list(UE_list,0);
   mac_xface->macphy_exit("mac_remove_ue: Problem in UE_list");
   return(-1);
diff --git a/openair2/LAYER2/MAC/main.c b/openair2/LAYER2/MAC/main.c
index 32cb3b1738..d5a445744d 100644
--- a/openair2/LAYER2/MAC/main.c
+++ b/openair2/LAYER2/MAC/main.c
@@ -476,6 +476,7 @@ int l2_init(LTE_DL_FRAME_PARMS *frame_parms,int eMBMS_active, char *uecap_xer,ui
   mac_xface->Msg1_transmitted          = Msg1_tx;
   mac_xface->ra_failed                 = ra_failed;
   mac_xface->ra_succeeded              = ra_succeeded;
+  mac_xface->mac_phy_remove_ue         = mac_phy_remove_ue;
 
   LOG_I(MAC,"[MAIN] init UE MAC functions \n");
   mac_xface->ue_decode_si              = ue_decode_si;
diff --git a/openair2/LAYER2/MAC/proto.h b/openair2/LAYER2/MAC/proto.h
index 632d8c8360..0e1d69d065 100644
--- a/openair2/LAYER2/MAC/proto.h
+++ b/openair2/LAYER2/MAC/proto.h
@@ -499,7 +499,7 @@ uint8_t *parse_ulsch_header(uint8_t *mac_header,
 int l2_init(LTE_DL_FRAME_PARMS *frame_parms,int eMBMS_active, char *uecap_xer, uint8_t cba_group_active, uint8_t HO_active);
 int mac_init(void);
 int add_new_ue(module_id_t Mod_id, int CC_id, rnti_t rnti,int harq_pid);
-int mac_remove_ue(module_id_t Mod_id, int UE_id,int frameP, sub_frame_t subframeP);
+int rrc_mac_remove_ue(module_id_t Mod_id, rnti_t rntiP);
 
 
 int maxround(module_id_t Mod_id,uint16_t rnti,int frame,sub_frame_t subframe,uint8_t ul_flag);
diff --git a/openair2/PHY_INTERFACE/defs.h b/openair2/PHY_INTERFACE/defs.h
index 194b085118..df8cfbdc26 100755
--- a/openair2/PHY_INTERFACE/defs.h
+++ b/openair2/PHY_INTERFACE/defs.h
@@ -134,7 +134,7 @@ typedef struct {
   /// get delta mcs for fast UL AMC
   int16_t (*estimate_ue_tx_power)(uint32_t tbs, uint32_t nb_rb, uint8_t control_only, lte_prefix_type_t ncp, uint8_t use_srs);
 
-
+  int (*mac_phy_remove_ue)(module_id_t Mod_idP,rnti_t rntiP);
   /// UE functions
 
   /// reset the ue phy
diff --git a/openair2/RRC/LITE/L2_interface.c b/openair2/RRC/LITE/L2_interface.c
index 7a4a69e2d2..ad8a9c9c2e 100644
--- a/openair2/RRC/LITE/L2_interface.c
+++ b/openair2/RRC/LITE/L2_interface.c
@@ -716,19 +716,47 @@ mac_eNB_get_rrc_status(
 }
 
 void mac_eNB_rrc_ul_failure(const module_id_t Mod_instP, 
-			    const int CC_id, 
+			    const int CC_idP, 
 			    const frame_t frameP,
 			    const sub_frame_t subframeP,
-			    const rnti_t rnti) {
+			    const rnti_t rntiP) {
 
-  return;
+  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
+  ue_context_p = rrc_eNB_get_ue_context(
+                   &eNB_rrc_inst[Mod_instP],
+                   rntiP);
+
+  if (ue_context_p != NULL) {
+    LOG_I(RRC,"Frame %d, Subframe %d: UE %x UL failure, activating timer\n",rntiP);
+    ue_context_p->ue_context.ul_failure_timer=1;
+  }
+  else {
+    LOG_W(RRC,"Frame %d, Subframe %d: UE %x unknown \n",frameP,subframeP,rntiP);
+    rrc_mac_remove_ue(Mod_instP,rntiP);
+  }
+  
 }
 
 void mac_eNB_rrc_ul_in_sync(const module_id_t Mod_instP, 
-			    const int CC_id, 
+			    const int CC_idP, 
 			    const frame_t frameP,
 			    const sub_frame_t subframeP,
-			    const rnti_t rnti) {
+			    const rnti_t rntiP) {
+
+  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
+  ue_context_p = rrc_eNB_get_ue_context(
+                   &eNB_rrc_inst[Mod_instP],
+                   rntiP);
+
+
+
+  if (ue_context_p != NULL) {
+    LOG_I(RRC,"Frame %d, Subframe %d: UE %x to UL in synch\n",rntiP);
+    ue_context_p->ue_context.ul_failure_timer=0;
+  }
+  else {
+    LOG_E(RRC,"Frame %d, Subframe %d: UE %x unknown \n",rntiP);
+  }
 
   return;
 }
diff --git a/openair2/RRC/LITE/defs.h b/openair2/RRC/LITE/defs.h
index 78240de082..49dc4d06ad 100644
--- a/openair2/RRC/LITE/defs.h
+++ b/openair2/RRC/LITE/defs.h
@@ -357,6 +357,7 @@ typedef struct eNB_RRC_UE_s {
   transport_layer_addr_t             enb_gtp_addrs[S1AP_MAX_E_RAB];
   rb_id_t                            enb_gtp_ebi[S1AP_MAX_E_RAB];
 #endif
+  uint32_t                           ul_failure_timer;
 } eNB_RRC_UE_t;
 
 typedef uid_t ue_uid_t;
diff --git a/openair2/RRC/LITE/proto.h b/openair2/RRC/LITE/proto.h
index 920d4d85f3..d14b129479 100644
--- a/openair2/RRC/LITE/proto.h
+++ b/openair2/RRC/LITE/proto.h
@@ -439,10 +439,8 @@ rrc_eNB_free_mem_UE_context(
 
 void
 rrc_eNB_free_UE(
-  const module_id_t enb_mod_idP,
-  const rnti_t      rntiP,
-  const frame_t     frameP,
-  const sub_frame_t subframeP
+		const module_id_t enb_mod_idP,
+		const struct rrc_eNB_ue_context_s*         const ue_context_pP
 );
 
 long binary_search_int(int elements[], long numElem, int value);
diff --git a/openair2/RRC/LITE/rrc_common.c b/openair2/RRC/LITE/rrc_common.c
index a27199bdab..595e87a534 100644
--- a/openair2/RRC/LITE/rrc_common.c
+++ b/openair2/RRC/LITE/rrc_common.c
@@ -48,6 +48,7 @@
 #include "asn1_msg.h"
 #include "pdcp.h"
 #include "UTIL/LOG/vcd_signal_dumper.h"
+#include "rrc_eNB_UE_context.h"
 
 #ifdef LOCALIZATION
 #include <sys/time.h>
@@ -414,9 +415,10 @@ rrc_rx_tx(
   uint8_t        UE_id;
   int32_t        current_timestamp_ms, ref_timestamp_ms;
   struct timeval ts;
+  struct rrc_eNB_ue_context_s   *ue_context_p = NULL,*ue_to_be_removed = NULL;
+
 #ifdef LOCALIZATION
   double                         estimated_distance;
-  struct rrc_eNB_ue_context_s*   ue_context_p = NULL;
   protocol_ctxt_t                ctxt;
 #endif
   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_RRC_RX_TX,VCD_FUNCTION_IN);
@@ -517,6 +519,21 @@ rrc_rx_tx(
   } else { // eNB
     check_handovers(ctxt_pP);
     // counetr, and get the value and aggregate
+
+    // check for UL failure
+    RB_FOREACH(ue_context_p, rrc_ue_tree_s, &(eNB_rrc_inst[ctxt_pP->module_id].rrc_ue_head)) {
+      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) {
+	  // 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_to_be_removed)
+      rrc_eNB_free_UE(ctxt_pP->module_id,ue_to_be_removed);
 #ifdef LOCALIZATION
 
     /* for the localization, only primary CC_id might be relevant*/
diff --git a/openair2/RRC/LITE/rrc_eNB.c b/openair2/RRC/LITE/rrc_eNB.c
index befdc32c63..6683e70045 100644
--- a/openair2/RRC/LITE/rrc_eNB.c
+++ b/openair2/RRC/LITE/rrc_eNB.c
@@ -790,35 +790,32 @@ rrc_eNB_free_mem_UE_context(
 }
 
 //-----------------------------------------------------------------------------
-// called by MAC layer only
 // should be called when UE is lost by eNB
 void
-rrc_eNB_free_UE(
-  const module_id_t enb_mod_idP,
-  const rnti_t      rntiP,
-  const frame_t     frameP,
-  const sub_frame_t subframeP
-)
+rrc_eNB_free_UE(const module_id_t enb_mod_idP,const struct rrc_eNB_ue_context_s*        const ue_context_pP)
 //-----------------------------------------------------------------------------
 {
 
-  struct rrc_eNB_ue_context_s*        ue_context_p = NULL;
+
   protocol_ctxt_t                     ctxt;
 #if !defined(ENABLE_USE_MME)
   module_id_t                         ue_module_id;
 #endif
-  AssertFatal(enb_mod_idP < NB_eNB_INST, "eNB inst invalid (%d/%d) for UE %x!", enb_mod_idP, NB_eNB_INST, rntiP);
-  ue_context_p = rrc_eNB_get_ue_context(
+  rnti_t rnti = ue_context_pP->ue_context.rnti;
+
+
+  AssertFatal(enb_mod_idP < NB_eNB_INST, "eNB inst invalid (%d/%d) for UE %x!", enb_mod_idP, NB_eNB_INST, rnti);
+  /*  ue_context_p = rrc_eNB_get_ue_context(
                    &eNB_rrc_inst[enb_mod_idP],
                    rntiP
                  );
-
-  if (NULL != ue_context_p) {
-    PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, enb_mod_idP, ENB_FLAG_YES, rntiP, frameP, subframeP,enb_mod_idP);
-    LOG_W(RRC, "[eNB %d] Removing UE RNTI %x\n", enb_mod_idP, rntiP);
+  */
+  if (NULL != ue_context_pP) {
+    PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, enb_mod_idP, ENB_FLAG_YES, rnti, 0, 0,enb_mod_idP);
+    LOG_W(RRC, "[eNB %d] Removing UE RNTI %x\n", enb_mod_idP, rnti);
 
 #if defined(ENABLE_USE_MME)
-    rrc_eNB_send_S1AP_UE_CONTEXT_RELEASE_REQ(enb_mod_idP, ue_context_p, S1AP_CAUSE_RADIO_NETWORK, 21); // send cause 21: connection with ue lost
+    rrc_eNB_send_S1AP_UE_CONTEXT_RELEASE_REQ(enb_mod_idP, ue_context_pP, S1AP_CAUSE_RADIO_NETWORK, 21); // send cause 21: connection with ue lost
     /* From 3GPP 36300v10 p129 : 19.2.2.2.2 S1 UE Context Release Request (eNB triggered)
      * If the E-UTRAN internal reason is a radio link failure detected in the eNB, the eNB shall wait a sufficient time before
      *  triggering the S1 UE Context Release Request procedure
@@ -827,22 +824,22 @@ rrc_eNB_free_UE(
      */
 #else
 #if defined(OAI_EMU)
-    AssertFatal(ue_context_p->local_uid < NUMBER_OF_UE_MAX, "local_uid invalid (%d<%d) for UE %x!", ue_context_p->local_uid, NUMBER_OF_UE_MAX, rntiP);
-    ue_module_id = oai_emulation.info.eNB_ue_local_uid_to_ue_module_id[enb_mod_idP][ue_context_p->local_uid];
-    AssertFatal(ue_module_id < NUMBER_OF_UE_MAX, "ue_module_id invalid (%d<%d) for UE %x!", ue_module_id, NUMBER_OF_UE_MAX, rntiP);
-    oai_emulation.info.eNB_ue_local_uid_to_ue_module_id[enb_mod_idP][ue_context_p->local_uid] = -1;
+    AssertFatal(ue_context_pP->local_uid < NUMBER_OF_UE_MAX, "local_uid invalid (%d<%d) for UE %x!", ue_context_pP->local_uid, NUMBER_OF_UE_MAX, rnti);
+    ue_module_id = oai_emulation.info.eNB_ue_local_uid_to_ue_module_id[enb_mod_idP][ue_context_pP->local_uid];
+    AssertFatal(ue_module_id < NUMBER_OF_UE_MAX, "ue_module_id invalid (%d<%d) for UE %x!", ue_module_id, NUMBER_OF_UE_MAX, rnti);
+    oai_emulation.info.eNB_ue_local_uid_to_ue_module_id[enb_mod_idP][ue_context_pP->local_uid] = -1;
     oai_emulation.info.eNB_ue_module_id_to_rnti[enb_mod_idP][ue_module_id] = NOT_A_RNTI;
 #endif
 #endif
-    ue_context_p->ue_context.Status = RRC_IDLE;
 
+    rrc_mac_remove_ue(enb_mod_idP,rnti);
     rrc_rlc_remove_ue(&ctxt);
     pdcp_remove_UE(&ctxt);
 
     rrc_eNB_remove_ue_context(
       &ctxt,
       &eNB_rrc_inst[enb_mod_idP],
-      ue_context_p);
+      ue_context_pP);
   }
 }
 
@@ -3554,6 +3551,7 @@ rrc_eNB_decode_ccch(
   int                                 i, rval;
   struct rrc_eNB_ue_context_s*                  ue_context_p = NULL;
   uint64_t                                      random_value = 0;
+  int                                           stmsi_received = 0;
 
   //memset(ul_ccch_msg,0,sizeof(UL_CCCH_Message_t));
 
@@ -3724,17 +3722,16 @@ rrc_eNB_decode_ccch(
             random_value = (((uint64_t)mme_code) << 32) | m_tmsi;
             if ((ue_context_p = rrc_eNB_ue_context_stmsi_exist(ctxt_pP, mme_code, m_tmsi))) {
 #warning "TODO: stmsi_exist: remove UE from MAC/PHY (how?)"
-	      LOG_I(RRC,PROTOCOL_RRC_CTXT_UE_FMT" S-TMSI exists, ue_context_p %p\n",ue_context_p);
+	      LOG_I(RRC," S-TMSI exists, ue_context_p %p\n",ue_context_p);
+	      stmsi_received=1;
 	      //   AssertFatal(0 == 1, "TODO: remove UE from MAC/PHY (how?)");
-              ue_context_p = NULL;
+	      //              ue_context_p = NULL;
             } else {
               ue_context_p = rrc_eNB_get_next_free_ue_context(ctxt_pP, NOT_A_RANDOM_UE_IDENTITY);
+	      ue_context_p->ue_context.Initialue_identity_s_TMSI.presence = TRUE;
+	      ue_context_p->ue_context.Initialue_identity_s_TMSI.mme_code = mme_code;
+	      ue_context_p->ue_context.Initialue_identity_s_TMSI.m_tmsi = m_tmsi;
             }
-	    if (ue_context_p==NULL)
-	      AssertFatal(0 == 1, "ue_context_p is null");
-            ue_context_p->ue_context.Initialue_identity_s_TMSI.presence = TRUE;
-            ue_context_p->ue_context.Initialue_identity_s_TMSI.mme_code = mme_code;
-            ue_context_p->ue_context.Initialue_identity_s_TMSI.m_tmsi = m_tmsi;
 
             MSC_LOG_RX_MESSAGE(
               MSC_RRC_ENB,
@@ -3780,7 +3777,8 @@ rrc_eNB_decode_ccch(
                 PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP),
                 ue_context_p->ue_context.random_ue_identity);
 #endif
-          eNB_rrc_inst[ctxt_pP->module_id].Nb_ue++;
+          if (stmsi_received == 0)
+	    eNB_rrc_inst[ctxt_pP->module_id].Nb_ue++;
 
         } else {
           // no context available
-- 
GitLab