From 1d33305a709f9c6bf02f1f7c328dfa7408d9c813 Mon Sep 17 00:00:00 2001
From: Mario Joa-Ng <m.mjoa-ng@northeastern.edu>
Date: Tue, 24 Aug 2021 12:51:32 -0400
Subject: [PATCH] NR_UE: set mac->logicalChannelBearer_exist to false when RLC
 bearer is released.

---
 openair2/LAYER2/NR_MAC_UE/config_ue.c |  5 +++--
 openair2/LAYER2/NR_MAC_UE/mac_proto.h |  6 ++++--
 openair2/RRC/NR_UE/rrc_UE.c           | 19 +++++++++++++++----
 3 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/openair2/LAYER2/NR_MAC_UE/config_ue.c b/openair2/LAYER2/NR_MAC_UE/config_ue.c
index d972057f7f..54cf380dd2 100644
--- a/openair2/LAYER2/NR_MAC_UE/config_ue.c
+++ b/openair2/LAYER2/NR_MAC_UE/config_ue.c
@@ -677,9 +677,10 @@ int nr_rrc_mac_config_req_ue_logicalChannelBearer(
     module_id_t                     module_id,
     int                             cc_idP,
     uint8_t                         gNB_index,
-    long                            logicalChannelIdentity){
+    long                            logicalChannelIdentity,
+    boolean_t                       status){
     NR_UE_MAC_INST_t *mac = get_mac_inst(module_id);
-    mac->logicalChannelBearer_exist[logicalChannelIdentity] = true;
+    mac->logicalChannelBearer_exist[logicalChannelIdentity] = status;
     return 0;
 }
 
diff --git a/openair2/LAYER2/NR_MAC_UE/mac_proto.h b/openair2/LAYER2/NR_MAC_UE/mac_proto.h
index e4504acb6b..2389beb220 100644
--- a/openair2/LAYER2/NR_MAC_UE/mac_proto.h
+++ b/openair2/LAYER2/NR_MAC_UE/mac_proto.h
@@ -82,12 +82,14 @@ int8_t nr_ue_decode_BCCH_DL_SCH(module_id_t module_id,
    \param module_id                 module id
    \param cc_id                     component carrier id
    \param gNB_index                 gNB index
-   \param long                      logicalChannelIdentity*/
+   \param long                      logicalChannelIdentity
+   \param boolean_t                 status*/
 int nr_rrc_mac_config_req_ue_logicalChannelBearer(
 	    module_id_t                 module_id,
 	    int                         cc_idP,
 	    uint8_t                     gNB_index,
-	    long                        logicalChannelIdentity
+	    long                        logicalChannelIdentity,
+	    boolean_t                   status
 );
 
 /**\brief primitive from RRC layer to MAC layer for configuration L1/L2, now supported 4 rrc messages: MIB, cell_group_config for MAC/PHY, spcell_config(serving cell config)
diff --git a/openair2/RRC/NR_UE/rrc_UE.c b/openair2/RRC/NR_UE/rrc_UE.c
index c00a3c5b66..1755c59115 100644
--- a/openair2/RRC/NR_UE/rrc_UE.c
+++ b/openair2/RRC/NR_UE/rrc_UE.c
@@ -2083,7 +2083,7 @@ nr_rrc_ue_establish_srb2(
 
 	   LOG_I(NR_RRC, "[FRAME %05d][RRC_UE][MOD %02d][][--- MAC_CONFIG_REQ  (SRB1 gNB %d) --->][MAC_UE][MOD %02d][]\n",
 	       ctxt_pP->frame, ctxt_pP->module_id, gNB_index, ctxt_pP->module_id);
-	   nr_rrc_mac_config_req_ue_logicalChannelBearer(ctxt_pP->module_id,0,gNB_index,1);
+	   nr_rrc_mac_config_req_ue_logicalChannelBearer(ctxt_pP->module_id,0,gNB_index,1,true); //todo handle mac_LogicalChannelConfig
 	   // rrc_mac_config_req_ue
 	 }
        } else {
@@ -2099,7 +2099,7 @@ nr_rrc_ue_establish_srb2(
 
 	   LOG_I(NR_RRC, "[FRAME %05d][RRC_UE][MOD %02d][][--- MAC_CONFIG_REQ  (SRB2 gNB %d) --->][MAC_UE][MOD %02d][]\n",
 	       ctxt_pP->frame, ctxt_pP->module_id, gNB_index, ctxt_pP->module_id);
-	   nr_rrc_mac_config_req_ue_logicalChannelBearer(ctxt_pP->module_id,0,gNB_index,2);
+	   nr_rrc_mac_config_req_ue_logicalChannelBearer(ctxt_pP->module_id,0,gNB_index,2,true); //todo handle mac_LogicalChannelConfig
 	   // rrc_mac_config_req_ue
 	 }
        } // srb2
@@ -2128,9 +2128,11 @@ nr_rrc_ue_establish_srb2(
 	   for(j = 0; j < rlc_bearer2add_list->list.count; j++){
 	     if(rlc_bearer2add_list->list.array[j]->servedRadioBearer != NULL){
 	       if(rlc_bearer2add_list->list.array[j]->servedRadioBearer->present == NR_RLC_BearerConfig__servedRadioBearer_PR_drb_Identity){
+	         if(DRB_id == rlc_bearer2add_list->list.array[j]->servedRadioBearer->choice.drb_Identity){
 	           LOG_I(NR_RRC, "[FRAME %05d][RRC_UE][MOD %02d][][--- MAC_CONFIG_REQ (DRB lcid %ld gNB %d) --->][MAC_UE][MOD %02d][]\n",
-	            ctxt_pP->frame, ctxt_pP->module_id, rlc_bearer2add_list->list.array[j]->logicalChannelIdentity, 0, ctxt_pP->module_id);
-	         nr_rrc_mac_config_req_ue_logicalChannelBearer(ctxt_pP->module_id,0,0,rlc_bearer2add_list->list.array[j]->logicalChannelIdentity);
+	               ctxt_pP->frame, ctxt_pP->module_id, rlc_bearer2add_list->list.array[j]->logicalChannelIdentity, 0, ctxt_pP->module_id);
+	           nr_rrc_mac_config_req_ue_logicalChannelBearer(ctxt_pP->module_id,0,0,rlc_bearer2add_list->list.array[j]->logicalChannelIdentity,true); //todo handle mac_LogicalChannelConfig
+	         }
 	       }
 	     }
 	   }
@@ -2182,6 +2184,15 @@ nr_rrc_ue_establish_srb2(
      }
    }
 
+   if (NR_UE_rrc_inst[ctxt_pP->module_id].cell_group_config->rlc_BearerToReleaseList != NULL) {
+     for (i = 0; i < NR_UE_rrc_inst[ctxt_pP->module_id].cell_group_config->rlc_BearerToReleaseList->list.count; i++) {
+       int lcid = *NR_UE_rrc_inst[ctxt_pP->module_id].cell_group_config->rlc_BearerToReleaseList->list.array[i];
+       LOG_I(NR_RRC, "[FRAME %05d][RRC_UE][MOD %02d][][--- MAC_CONFIG_REQ (RB lcid %ld gNB %d release) --->][MAC_UE][MOD %02d][]\n",
+           ctxt_pP->frame, ctxt_pP->module_id, lcid, 0, ctxt_pP->module_id);
+       nr_rrc_mac_config_req_ue_logicalChannelBearer(ctxt_pP->module_id,0,0,lcid,false); //todo handle mac_LogicalChannelConfig
+     }
+   }
+
    NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].State = NR_RRC_CONNECTED;
    LOG_I(NR_RRC,"[UE %d] State = NR_RRC_CONNECTED (gNB %d)\n", ctxt_pP->module_id, gNB_index);
  }
-- 
2.26.2