From b5583d47de5c63a2759fb2ba4ee3d69a3b97deab Mon Sep 17 00:00:00 2001
From: francescomani <email@francescomani.it>
Date: Mon, 6 Jun 2022 11:52:12 +0200
Subject: [PATCH] temporary workaround for RRC passing CG to MAC (forced
 encoding of message at MAC)

---
 openair1/PHY/INIT/nr_init.c                   |  4 +--
 openair2/LAYER2/NR_MAC_gNB/config.c           | 18 ++++++++++++-
 .../NR_MAC_gNB/gNB_scheduler_primitives.c     | 10 +++++++-
 openair2/LAYER2/NR_MAC_gNB/nr_mac_gNB.h       |  2 ++
 openair2/RRC/NR/rrc_gNB.c                     | 25 ++++++++++---------
 5 files changed, 43 insertions(+), 16 deletions(-)

diff --git a/openair1/PHY/INIT/nr_init.c b/openair1/PHY/INIT/nr_init.c
index 9a570095783..8ab978614e9 100644
--- a/openair1/PHY/INIT/nr_init.c
+++ b/openair1/PHY/INIT/nr_init.c
@@ -544,8 +544,8 @@ int phy_init_nr_gNB(PHY_VARS_gNB *gNB,
   gNB->nr_gold_pdsch_dmrs = (uint32_t ****)malloc16(fp->slots_per_frame*sizeof(uint32_t ***));
   uint32_t ****pdsch_dmrs             = gNB->nr_gold_pdsch_dmrs;
 
-  // ceil(((NB_RB*6(k)*2(QPSK)/32) // 3 RE *2(QPSK)
-  int pdsch_dmrs_init_length =  ((fp->N_RB_DL*12)>>5)+1;
+  // ceil(((NB_RB*12(k)*2(QPSK)/32) // 3 RE *2(QPSK)
+  int pdsch_dmrs_init_length =  ((fp->N_RB_DL*24)>>5)+1;
   for (int slot=0; slot<fp->slots_per_frame; slot++) {
     pdsch_dmrs[slot] = (uint32_t ***)malloc16(fp->symbols_per_slot*sizeof(uint32_t **));
     AssertFatal(pdsch_dmrs[slot]!=NULL, "NR init: pdsch_dmrs for slot %d - malloc failed\n", slot);
diff --git a/openair2/LAYER2/NR_MAC_gNB/config.c b/openair2/LAYER2/NR_MAC_gNB/config.c
index 0e55b519abb..95ccc9a4d4d 100644
--- a/openair2/LAYER2/NR_MAC_gNB/config.c
+++ b/openair2/LAYER2/NR_MAC_gNB/config.c
@@ -654,7 +654,23 @@ int rrc_mac_config_req_gNB(module_id_t Mod_idP,
       }
       int target_ss;
 
-      UE->CellGroup = CellGroup;
+      /* copy CellGroup by calling asn1c encode
+         this is a temporary hack to avoid the gNB having
+         a pointer to RRC CellGroup structure
+         (otherwise it would be applied to early)
+         TODO remove once we have a proper implementation */
+      UE->enc_rval = uper_encode_to_buffer(&asn_DEF_NR_CellGroupConfig,
+                                           NULL,
+                                           (void *)CellGroup,
+                                           UE->cg_buf,
+                                           32768);
+
+      if(UE->enc_rval.encoded == -1) {
+        LOG_E(NR_MAC, "ASN1 message CellGroupConfig encoding failed (%s, %lu)!\n",
+              UE->enc_rval.failed_type->name, UE->enc_rval.encoded);
+        exit(1);
+      }
+
       LOG_I(NR_MAC,"Modified rnti %04x with CellGroup\n",rnti);
       process_CellGroup(CellGroup,&UE->UE_sched_ctrl);
       NR_UE_sched_ctrl_t *sched_ctrl = &UE->UE_sched_ctrl;
diff --git a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_primitives.c b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_primitives.c
index 65d2ce09c50..afc37d8d8a3 100644
--- a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_primitives.c
+++ b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_primitives.c
@@ -2903,7 +2903,15 @@ void nr_mac_update_timers(module_id_t module_id,
         LOG_I(NR_MAC, "(%d.%d) De-activating RRC processing timer for UE %04x\n", frame, slot, UE->rnti);
 
         const NR_SIB1_t *sib1 = RC.nrmac[module_id]->common_channels[0].sib1 ? RC.nrmac[module_id]->common_channels[0].sib1->message.choice.c1->choice.systemInformationBlockType1 : NULL;
-        NR_CellGroupConfig_t *cg = UE->CellGroup;
+
+        NR_CellGroupConfig_t *cg = NULL;
+        uper_decode(NULL,
+                    &asn_DEF_NR_CellGroupConfig,   //might be added prefix later
+                    (void **)&cg,
+                    (uint8_t *)UE->cg_buf,
+                    (UE->enc_rval.encoded+7)/8, 0, 0);
+        UE->CellGroup = cg;
+
         NR_ServingCellConfigCommon_t *scc = RC.nrmac[module_id]->common_channels[0].ServingCellConfigCommon;
         const NR_ServingCellConfig_t *spCellConfigDedicated = cg && cg->spCellConfig ? cg->spCellConfig->spCellConfigDedicated : NULL;
 
diff --git a/openair2/LAYER2/NR_MAC_gNB/nr_mac_gNB.h b/openair2/LAYER2/NR_MAC_gNB/nr_mac_gNB.h
index 974d6dff4a7..1d0d9750e6b 100644
--- a/openair2/LAYER2/NR_MAC_gNB/nr_mac_gNB.h
+++ b/openair2/LAYER2/NR_MAC_gNB/nr_mac_gNB.h
@@ -710,6 +710,8 @@ typedef struct {
   NR_UE_sched_ctrl_t UE_sched_ctrl;
   NR_mac_stats_t mac_stats;
   NR_CellGroupConfig_t *CellGroup;
+  char cg_buf[32768]; /* arbitrary size */
+  asn_enc_rval_t enc_rval;
   /// CCE indexing
   int m;
   // UE selected beam index
diff --git a/openair2/RRC/NR/rrc_gNB.c b/openair2/RRC/NR/rrc_gNB.c
index f5879a0c49a..d8c26114c25 100755
--- a/openair2/RRC/NR/rrc_gNB.c
+++ b/openair2/RRC/NR/rrc_gNB.c
@@ -734,6 +734,19 @@ rrc_gNB_generate_defaultRRCReconfiguration(
                                 NULL,
                                 ue_p->masterCellGroup);
 
+  rrc_mac_config_req_gNB(rrc->module_id,
+                         rrc->configuration.ssb_SubcarrierOffset,
+                         rrc->configuration.pdsch_AntennaPorts,
+                         rrc->configuration.pusch_AntennaPorts,
+                         rrc->configuration.sib1_tda,
+                         rrc->configuration.minRXTXTIME,
+                         NULL,
+                         NULL,
+                         NULL,
+                         0,
+                         ue_p->rnti,
+                         ue_p->masterCellGroup);
+
   free(ue_context_pP->ue_context.nas_pdu.buffer);
 
   LOG_DUMPMSG(NR_RRC, DEBUG_RRC,(char *)buffer, size, "[MSG] RRC Reconfiguration\n");
@@ -1430,18 +1443,6 @@ rrc_gNB_process_RRCReconfigurationComplete(
   /* Refresh SRBs/DRBs */
 
   if (!NODE_IS_CU(RC.nrrrc[ctxt_pP->module_id]->node_type)) {
-    rrc_mac_config_req_gNB(rrc->module_id,
-                           rrc->configuration.ssb_SubcarrierOffset,
-                           rrc->configuration.pdsch_AntennaPorts,
-                           rrc->configuration.pusch_AntennaPorts,
-                           rrc->configuration.sib1_tda,
-                           rrc->configuration.minRXTXTIME,
-                           NULL,
-                           NULL,
-                           NULL,
-                           0,
-                           ue_context_pP->ue_context.rnti,
-                           ue_context_pP->ue_context.masterCellGroup);
     LOG_D(NR_RRC,"Configuring RLC DRBs/SRBs for UE %x\n",ue_context_pP->ue_context.rnti);
     nr_rrc_rlc_config_asn1_req(ctxt_pP,
                                SRB_configList, // NULL,
-- 
GitLab