From 1d66b5c41516e1d87a8a4956b68c0907420a03bb Mon Sep 17 00:00:00 2001
From: francescomani <francesco.mani@eurecom.fr>
Date: Mon, 26 Jul 2021 16:08:52 +0200
Subject: [PATCH] attempting to fix wrong implementation in NSA UE
 configuration

---
 openair2/LAYER2/NR_MAC_UE/config_ue.c |   4 +-
 openair2/RRC/NR_UE/rrc_UE.c           | 106 +++++++++++++-------------
 2 files changed, 57 insertions(+), 53 deletions(-)

diff --git a/openair2/LAYER2/NR_MAC_UE/config_ue.c b/openair2/LAYER2/NR_MAC_UE/config_ue.c
index 6dfc9ae220..6f9e36d047 100755
--- a/openair2/LAYER2/NR_MAC_UE/config_ue.c
+++ b/openair2/LAYER2/NR_MAC_UE/config_ue.c
@@ -316,7 +316,7 @@ void config_common_ue(NR_UE_MAC_INST_t *mac,
   // carrier config
   LOG_D(MAC, "Entering UE Config Common\n");
 
-  AssertFatal(scc==NULL,"scc cannot be null\n");
+  AssertFatal(scc!=NULL,"scc cannot be null\n");
 
   if (scc) {
     cfg->carrier_config.dl_bandwidth = config_bandwidth(scc->downlinkConfigCommon->frequencyInfoDL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing,
@@ -658,7 +658,7 @@ int nr_rrc_mac_config_req_ue(
           ra->rach_ConfigDedicated = scell_group_config->spCellConfig->reconfigurationWithSync->rach_ConfigDedicated->choice.uplink;
         }
         mac->scc = scell_group_config->spCellConfig->reconfigurationWithSync->spCellConfigCommon;
-	      mac->physCellId = *mac->scc->physCellId;
+        mac->physCellId = *mac->scc->physCellId;
         config_common_ue(mac,module_id,cc_idP);
         mac->crnti = scell_group_config->spCellConfig->reconfigurationWithSync->newUE_Identity;
         LOG_I(MAC,"Configuring CRNTI %x\n",mac->crnti);
diff --git a/openair2/RRC/NR_UE/rrc_UE.c b/openair2/RRC/NR_UE/rrc_UE.c
index 6bcddf7369..f272c16017 100644
--- a/openair2/RRC/NR_UE/rrc_UE.c
+++ b/openair2/RRC/NR_UE/rrc_UE.c
@@ -201,42 +201,38 @@ extern rlc_op_status_t nr_rrc_rlc_config_asn1_req (const protocol_ctxt_t   * con
     struct NR_CellGroupConfig__rlc_BearerToAddModList *rlc_bearer2add_list);
 
 // from LTE-RRC DL-DCCH RRCConnectionReconfiguration nr-secondary-cell-group-config (encoded)
-int8_t nr_rrc_ue_decode_secondary_cellgroup_config(
-    const module_id_t module_id,
-    const uint8_t *buffer,
-    const uint32_t size ){
+int8_t nr_rrc_ue_decode_secondary_cellgroup_config(const module_id_t module_id,
+                                                   const uint8_t *buffer,
+                                                   const uint32_t size){
     
-    NR_CellGroupConfig_t *cell_group_config = NULL;
-    uint32_t i;
-
-    asn_dec_rval_t dec_rval = uper_decode_complete( NULL,
-                                                    &asn_DEF_NR_CellGroupConfig,
-                                                    (void **)&cell_group_config,
-                                                    (uint8_t *)buffer,
-                                                    size ); 
-
-    if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
-            printf("NR_CellGroupConfig decode error\n");
-            for (i=0; i<size; i++){
-                printf("%02x ",buffer[i]);
-            }
-            printf("\n");
-            // free the memory
-            SEQUENCE_free( &asn_DEF_NR_CellGroupConfig, (void *)cell_group_config, 1 );
-            return -1;
-    }
+  NR_CellGroupConfig_t *cell_group_config = NULL;
+  uint32_t i;
 
-    if(NR_UE_rrc_inst[module_id].scell_group_config == NULL){
-        NR_UE_rrc_inst[module_id].scell_group_config = cell_group_config;
-        nr_rrc_ue_process_scg_config(module_id,cell_group_config);
-    }else{
-        nr_rrc_ue_process_scg_config(module_id,cell_group_config);
-        SEQUENCE_free(&asn_DEF_NR_CellGroupConfig, (void *)cell_group_config, 0);
-    }
+  asn_dec_rval_t dec_rval = uper_decode(NULL,
+                                        &asn_DEF_NR_CellGroupConfig,
+                                        (void **)&cell_group_config,
+                                        (uint8_t *)buffer,
+                                        size, 0, 0);
+
+  if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
+    printf("NR_CellGroupConfig decode error\n");
+    for (i=0; i<size; i++)
+      printf("%02x ",buffer[i]);
+    printf("\n");
+    // free the memory
+    SEQUENCE_free( &asn_DEF_NR_CellGroupConfig, (void *)cell_group_config, 1 );
+    return -1;
+  }
 
-    //nr_rrc_mac_config_req_ue( 0,0,0,NULL, cell_group_config->mac_CellGroupConfig, cell_group_config->physicalCellGroupConfig, cell_group_config->spCellConfig );
+  if(NR_UE_rrc_inst[module_id].scell_group_config == NULL){
+    NR_UE_rrc_inst[module_id].scell_group_config = cell_group_config;
+    nr_rrc_ue_process_scg_config(module_id,cell_group_config);
+  }else{
+    nr_rrc_ue_process_scg_config(module_id,cell_group_config);
+    SEQUENCE_free(&asn_DEF_NR_CellGroupConfig, (void *)cell_group_config, 0);
+  }
 
-    return 0;
+  return 0;
 }
 
 int8_t nr_rrc_ue_process_RadioBearerConfig(NR_RadioBearerConfig_t *RadioBearerConfig){
@@ -262,25 +258,33 @@ int8_t nr_rrc_ue_process_rrcReconfiguration(const module_id_t module_id, NR_RRCR
         }
       }
       if(rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->secondaryCellGroup != NULL){
-        NR_CellGroupConfig_t *cellGroupConfig = NULL;
-        uper_decode(NULL,
-                    &asn_DEF_NR_CellGroupConfig,   //might be added prefix later
-                    (void **)&cellGroupConfig,
-                    (uint8_t *)rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->secondaryCellGroup->buf,
-                    rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->secondaryCellGroup->size, 0, 0); 
-
-        xer_fprint(stdout, &asn_DEF_NR_CellGroupConfig, (const void*)cellGroupConfig);
-
-        if(NR_UE_rrc_inst[module_id].cell_group_config == NULL){
-          //  first time receive the configuration, just use the memory allocated from uper_decoder. TODO this is not good implementation, need to maintain RRC_INST own structure every time.
-          NR_UE_rrc_inst[module_id].cell_group_config = cellGroupConfig;
-          nr_rrc_ue_process_scg_config(module_id,cellGroupConfig);
-        }else{
-          //  after first time, update it and free the memory after.
-          SEQUENCE_free(&asn_DEF_NR_CellGroupConfig, (void *)NR_UE_rrc_inst[module_id].cell_group_config, 0);
-          NR_UE_rrc_inst[module_id].cell_group_config = cellGroupConfig;
-          nr_rrc_ue_process_scg_config(module_id,cellGroupConfig);
+
+        if(get_softmodem_params()->sa) {
+
+          NR_CellGroupConfig_t *cellGroupConfig = NULL;
+          uper_decode(NULL,
+                      &asn_DEF_NR_CellGroupConfig,   //might be added prefix later
+                      (void **)&cellGroupConfig,
+                      (uint8_t *)rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->secondaryCellGroup->buf,
+                      rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->secondaryCellGroup->size, 0, 0);
+
+          xer_fprint(stdout, &asn_DEF_NR_CellGroupConfig, (const void*)cellGroupConfig);
+
+          if(NR_UE_rrc_inst[module_id].cell_group_config == NULL){
+            //  first time receive the configuration, just use the memory allocated from uper_decoder. TODO this is not good implementation, need to maintain RRC_INST own structure every time.
+            NR_UE_rrc_inst[module_id].cell_group_config = cellGroupConfig;
+            nr_rrc_ue_process_scg_config(module_id,cellGroupConfig);
+          }else{
+            //  after first time, update it and free the memory after.
+            SEQUENCE_free(&asn_DEF_NR_CellGroupConfig, (void *)NR_UE_rrc_inst[module_id].cell_group_config, 0);
+            NR_UE_rrc_inst[module_id].cell_group_config = cellGroupConfig;
+            nr_rrc_ue_process_scg_config(module_id,cellGroupConfig);
+          }
         }
+        else
+          nr_rrc_ue_decode_secondary_cellgroup_config(module_id,
+                                                      rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->secondaryCellGroup->buf,
+                                                      rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->secondaryCellGroup->size);
       }
       if(rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->measConfig != NULL){
         if(NR_UE_rrc_inst[module_id].meas_config == NULL){
@@ -317,7 +321,7 @@ int8_t nr_rrc_ue_process_meas_config(NR_MeasConfig_t *meas_config){
 
 int8_t nr_rrc_ue_process_scg_config(const module_id_t module_id, NR_CellGroupConfig_t *cell_group_config){
   int i;
-  if(NR_UE_rrc_inst[module_id].cell_group_config==NULL){
+  if(cell_group_config==NULL){
     //  initial list
     if(cell_group_config->spCellConfig != NULL){
       if(cell_group_config->spCellConfig->spCellConfigDedicated != NULL){
-- 
2.26.2