From 16c82f25984c2c35b2fe9f90640f8cde4aa2383b Mon Sep 17 00:00:00 2001
From: francescomani <email@francescomani.it>
Date: Thu, 15 Feb 2024 16:32:45 +0100
Subject: [PATCH] set cset0 and ss0 independently from BWP in generic MAC
 structure at UE

---
 openair2/LAYER2/NR_MAC_UE/config_ue.c         | 20 ++++++++++---------
 openair2/LAYER2/NR_MAC_UE/mac_defs.h          |  4 ++--
 openair2/LAYER2/NR_MAC_UE/main_ue_nr.c        |  3 +++
 .../NR_MAC_UE/nr_ue_dci_configuration.c       | 20 +++++++++----------
 4 files changed, 26 insertions(+), 21 deletions(-)

diff --git a/openair2/LAYER2/NR_MAC_UE/config_ue.c b/openair2/LAYER2/NR_MAC_UE/config_ue.c
index 4662a7eab6..c15d7d9a8b 100644
--- a/openair2/LAYER2/NR_MAC_UE/config_ue.c
+++ b/openair2/LAYER2/NR_MAC_UE/config_ue.c
@@ -420,9 +420,7 @@ void release_common_ss_cset(NR_BWP_PDCCH_t *pdcch)
   asn1cFreeStruc(asn_DEF_NR_SearchSpace, pdcch->otherSI_SS);
   asn1cFreeStruc(asn_DEF_NR_SearchSpace, pdcch->ra_SS);
   asn1cFreeStruc(asn_DEF_NR_SearchSpace, pdcch->paging_SS);
-  asn1cFreeStruc(asn_DEF_NR_SearchSpace, pdcch->search_space_zero);
   asn1cFreeStruc(asn_DEF_NR_ControlResourceSet, pdcch->commonControlResourceSet);
-  asn1cFreeStruc(asn_DEF_NR_ControlResourceSet, pdcch->coreset0);
 }
 
 static void modlist_ss(NR_SearchSpace_t *source, NR_SearchSpace_t *target)
@@ -443,12 +441,13 @@ static void modlist_ss(NR_SearchSpace_t *source, NR_SearchSpace_t *target)
     UPDATE_MAC_IE(target->searchSpaceType, source->searchSpaceType, struct NR_SearchSpace__searchSpaceType);
 }
 
-static NR_SearchSpace_t *get_common_search_space(const struct NR_PDCCH_ConfigCommon__commonSearchSpaceList *commonSearchSpaceList,
+static NR_SearchSpace_t *get_common_search_space(const NR_UE_MAC_INST_t *mac,
+                                                 const struct NR_PDCCH_ConfigCommon__commonSearchSpaceList *commonSearchSpaceList,
                                                  const NR_BWP_PDCCH_t *pdcch,
                                                  const NR_SearchSpaceId_t ss_id)
 {
   if (ss_id == 0)
-    return pdcch->search_space_zero;
+    return mac->search_space_zero;
 
   NR_SearchSpace_t *css = NULL;
   for (int i = 0; i < commonSearchSpaceList->list.count; i++) {
@@ -462,12 +461,15 @@ static NR_SearchSpace_t *get_common_search_space(const struct NR_PDCCH_ConfigCom
   return css;
 }
 
-static void configure_common_ss_coreset(NR_BWP_PDCCH_t *pdcch, NR_PDCCH_ConfigCommon_t *pdcch_ConfigCommon)
+static void configure_common_ss_coreset(const NR_UE_MAC_INST_t *mac,
+                                        NR_BWP_PDCCH_t *pdcch,
+                                        NR_PDCCH_ConfigCommon_t *pdcch_ConfigCommon)
 {
   if (pdcch_ConfigCommon) {
     asn1cFreeStruc(asn_DEF_NR_SearchSpace, pdcch->otherSI_SS);
     if (pdcch_ConfigCommon->searchSpaceOtherSystemInformation)
-      pdcch->otherSI_SS = get_common_search_space(pdcch_ConfigCommon->commonSearchSpaceList,
+      pdcch->otherSI_SS = get_common_search_space(mac,
+                                                  pdcch_ConfigCommon->commonSearchSpaceList,
                                                   pdcch,
                                                   *pdcch_ConfigCommon->searchSpaceOtherSystemInformation);
 
@@ -477,7 +479,7 @@ static void configure_common_ss_coreset(NR_BWP_PDCCH_t *pdcch, NR_PDCCH_ConfigCo
         pdcch->ra_SS = pdcch->otherSI_SS;
       else
         pdcch->ra_SS =
-            get_common_search_space(pdcch_ConfigCommon->commonSearchSpaceList, pdcch, *pdcch_ConfigCommon->ra_SearchSpace);
+            get_common_search_space(mac, pdcch_ConfigCommon->commonSearchSpaceList, pdcch, *pdcch_ConfigCommon->ra_SearchSpace);
     }
 
     asn1cFreeStruc(asn_DEF_NR_SearchSpace, pdcch->paging_SS);
@@ -488,7 +490,7 @@ static void configure_common_ss_coreset(NR_BWP_PDCCH_t *pdcch, NR_PDCCH_ConfigCo
         pdcch->paging_SS = pdcch->ra_SS;
       if (!pdcch->paging_SS)
         pdcch->paging_SS =
-            get_common_search_space(pdcch_ConfigCommon->commonSearchSpaceList, pdcch, *pdcch_ConfigCommon->pagingSearchSpace);
+            get_common_search_space(mac, pdcch_ConfigCommon->commonSearchSpaceList, pdcch, *pdcch_ConfigCommon->pagingSearchSpace);
     }
 
     UPDATE_MAC_IE(pdcch->commonControlResourceSet, pdcch_ConfigCommon->commonControlResourceSet, NR_ControlResourceSet_t);
@@ -1304,7 +1306,7 @@ static void configure_common_BWP_dl(NR_UE_MAC_INST_t *mac, int bwp_id, NR_BWP_Do
     NR_BWP_PDCCH_t *pdcch = &mac->config_BWP_PDCCH[bwp_id];
     if (dl_common->pdcch_ConfigCommon) {
       if (dl_common->pdcch_ConfigCommon->present == NR_SetupRelease_PDCCH_ConfigCommon_PR_setup)
-        configure_common_ss_coreset(pdcch, dl_common->pdcch_ConfigCommon->choice.setup);
+        configure_common_ss_coreset(mac, pdcch, dl_common->pdcch_ConfigCommon->choice.setup);
       if (dl_common->pdcch_ConfigCommon->present == NR_SetupRelease_PDCCH_ConfigCommon_PR_release)
         release_common_ss_cset(pdcch);
     }
diff --git a/openair2/LAYER2/NR_MAC_UE/mac_defs.h b/openair2/LAYER2/NR_MAC_UE/mac_defs.h
index f5ec77d8af..4ba4ade48d 100644
--- a/openair2/LAYER2/NR_MAC_UE/mac_defs.h
+++ b/openair2/LAYER2/NR_MAC_UE/mac_defs.h
@@ -446,9 +446,7 @@ typedef struct {
   NR_SearchSpace_t *otherSI_SS;
   NR_SearchSpace_t *ra_SS;
   NR_SearchSpace_t *paging_SS;
-  NR_ControlResourceSet_t *coreset0;
   NR_ControlResourceSet_t *commonControlResourceSet;
-  NR_SearchSpace_t *search_space_zero;
   A_SEQUENCE_OF(NR_ControlResourceSet_t) list_Coreset;
   A_SEQUENCE_OF(NR_SearchSpace_t) list_SS;
 } NR_BWP_PDCCH_t;
@@ -472,6 +470,8 @@ typedef struct NR_UE_MAC_INST_s {
   A_SEQUENCE_OF(NR_UE_DL_BWP_t) dl_BWPs;
   A_SEQUENCE_OF(NR_UE_UL_BWP_t) ul_BWPs;
   NR_BWP_PDCCH_t config_BWP_PDCCH[MAX_NUM_BWP_UE];
+  NR_ControlResourceSet_t *coreset0;
+  NR_SearchSpace_t *search_space_zero;
   NR_UE_DL_BWP_t *current_DL_BWP;
   NR_UE_UL_BWP_t *current_UL_BWP;
 
diff --git a/openair2/LAYER2/NR_MAC_UE/main_ue_nr.c b/openair2/LAYER2/NR_MAC_UE/main_ue_nr.c
index 4e23a5a26d..9acfed9008 100644
--- a/openair2/LAYER2/NR_MAC_UE/main_ue_nr.c
+++ b/openair2/LAYER2/NR_MAC_UE/main_ue_nr.c
@@ -220,6 +220,9 @@ void release_mac_configuration(NR_UE_MAC_INST_t *mac)
   for (int i = 0; i < mac->ul_BWPs.count; i++)
     release_ul_BWP(mac, i);
 
+  asn1cFreeStruc(asn_DEF_NR_SearchSpace, mac->search_space_zero);
+  asn1cFreeStruc(asn_DEF_NR_ControlResourceSet, mac->coreset0);
+
   for (int i = 0; i < mac->lc_ordered_list.count; i++) {
     nr_lcordered_info_t *lc_info = mac->lc_ordered_list.array[i];
     asn_sequence_del(&mac->lc_ordered_list, i, 0);
diff --git a/openair2/LAYER2/NR_MAC_UE/nr_ue_dci_configuration.c b/openair2/LAYER2/NR_MAC_UE/nr_ue_dci_configuration.c
index 85f577a51a..5eb895c52e 100644
--- a/openair2/LAYER2/NR_MAC_UE/nr_ue_dci_configuration.c
+++ b/openair2/LAYER2/NR_MAC_UE/nr_ue_dci_configuration.c
@@ -121,7 +121,7 @@ void config_dci_pdu(NR_UE_MAC_INST_t *mac,
     coreset = ue_get_coreset(pdcch_config, coreset_id);
     rel15->coreset.CoreSetType = NFAPI_NR_CSET_CONFIG_PDCCH_CONFIG;
   } else {
-    coreset = pdcch_config->coreset0;
+    coreset = mac->coreset0;
     rel15->coreset.CoreSetType = NFAPI_NR_CSET_CONFIG_MIB_SIB1;
   }
 
@@ -483,22 +483,22 @@ void ue_dci_configuration(NR_UE_MAC_INST_t *mac, fapi_nr_dl_config_request_t *dl
                                           mac->mib_ssb,
                                           1, // If the UE is not configured with a periodicity, the UE assumes a periodicity of a half frame
                                           ssb_offset_point_a);
-    if (pdcch_config->search_space_zero == NULL)
-      pdcch_config->search_space_zero = calloc(1, sizeof(*pdcch_config->search_space_zero));
-    if (pdcch_config->coreset0 == NULL)
-      pdcch_config->coreset0 = calloc(1, sizeof(*pdcch_config->coreset0));
-    fill_coresetZero(pdcch_config->coreset0, &mac->type0_PDCCH_CSS_config);
-    fill_searchSpaceZero(pdcch_config->search_space_zero, slots_per_frame, &mac->type0_PDCCH_CSS_config);
-    if (is_ss_monitor_occasion(frame, slot, slots_per_frame, pdcch_config->search_space_zero)) {
+    if (mac->search_space_zero == NULL)
+      mac->search_space_zero = calloc(1, sizeof(*mac->search_space_zero));
+    if (mac->coreset0 == NULL)
+      mac->coreset0 = calloc(1, sizeof(*mac->coreset0));
+    fill_coresetZero(mac->coreset0, &mac->type0_PDCCH_CSS_config);
+    fill_searchSpaceZero(mac->search_space_zero, slots_per_frame, &mac->type0_PDCCH_CSS_config);
+    if (is_ss_monitor_occasion(frame, slot, slots_per_frame, mac->search_space_zero)) {
       LOG_D(NR_MAC, "Monitoring DCI for SIB1 in frame %d slot %d\n", frame, slot);
-      config_dci_pdu(mac, dl_config, TYPE_SI_RNTI_, slot, pdcch_config->search_space_zero);
+      config_dci_pdu(mac, dl_config, TYPE_SI_RNTI_, slot, mac->search_space_zero);
     }
   }
   if (mac->get_otherSI) {
     // If searchSpaceOtherSystemInformation is set to zero,
     // PDCCH monitoring occasions for SI message reception in SI-window
     // are same as PDCCH monitoring occasions for SIB1
-    const NR_SearchSpace_t *ss = pdcch_config->otherSI_SS ? pdcch_config->otherSI_SS : pdcch_config->search_space_zero;
+    const NR_SearchSpace_t *ss = pdcch_config->otherSI_SS ? pdcch_config->otherSI_SS : mac->search_space_zero;
     // TODO configure SI-window
     if (monitior_dci_for_other_SI(mac, ss, slots_per_frame, frame, slot)) {
       LOG_D(NR_MAC, "Monitoring DCI for other SIs in frame %d slot %d\n", frame, slot);
-- 
2.26.2