From 874246b3f99d39fa2e0597ae185e810fa8315a3f Mon Sep 17 00:00:00 2001
From: Xue Song <xuesong@cn.fujitsu.com>
Date: Tue, 26 Jan 2021 18:07:13 +0900
Subject: [PATCH] fix the problem of f1ap setup response message encoding error

---
 openair2/F1AP/f1ap_cu_interface_management.c | 29 ++++++------
 openair2/RRC/NR/MESSAGES/asn1_msg.c          | 50 ++++++++++++++++++++
 openair2/RRC/NR/MESSAGES/asn1_msg.h          |  3 ++
 openair2/RRC/NR/nr_rrc_defs.h                |  3 ++
 openair2/RRC/NR/rrc_gNB.c                    | 17 +++++--
 5 files changed, 82 insertions(+), 20 deletions(-)

diff --git a/openair2/F1AP/f1ap_cu_interface_management.c b/openair2/F1AP/f1ap_cu_interface_management.c
index 4de208e2fe..5cd37ee7ca 100644
--- a/openair2/F1AP/f1ap_cu_interface_management.c
+++ b/openair2/F1AP/f1ap_cu_interface_management.c
@@ -353,7 +353,7 @@ int CU_send_F1_SETUP_RESPONSE(instance_t instance,
 
     /* optional */
     /* - gNB-CU System Information */
-    if (0) {
+    if (1) {
       /* 3.1.2 gNB-CUSystem Information */
       F1AP_Cells_to_be_Activated_List_ItemExtIEs_t *cells_to_be_activated_list_itemExtIEs;
       cells_to_be_activated_list_itemExtIEs = (F1AP_Cells_to_be_Activated_List_ItemExtIEs_t *)calloc(1, sizeof(F1AP_Cells_to_be_Activated_List_ItemExtIEs_t));
@@ -367,20 +367,19 @@ int CU_send_F1_SETUP_RESPONSE(instance_t instance,
       //  printf("%02x ", f1ap_setup_resp->SI_container[i][0][n]);
       //printf("\n");
 
-      if (0) {
-        for (int sIBtype=2;sIBtype<33;sIBtype++) {
-          if (f1ap_setup_resp->SI_container[i][sIBtype]!=NULL) {
-            AssertFatal(sIBtype < 6 || sIBtype == 9, "Illegal SI type %d\n",sIBtype);
-            F1AP_SibtypetobeupdatedListItem_t *sib_item = calloc(1,sizeof(*sib_item));
-            memset((void*)sib_item,0,sizeof(*sib_item));
-            sib_item->sIBtype = sIBtype;
-            OCTET_STRING_fromBuf(&sib_item->sIBmessage,
-                                (const char*)f1ap_setup_resp->SI_container[i][sIBtype], 
-                                f1ap_setup_resp->SI_container_length[i][sIBtype]);
-          
-            LOG_D(F1AP, "f1ap_setup_resp->SI_container_length[%d][%d] = %d \n", i,sIBtype,f1ap_setup_resp->SI_container_length[i][sIBtype]);
-            ASN_SEQUENCE_ADD(&gNB_CUSystemInformation->sibtypetobeupdatedlist.list,sib_item);
-          }
+      // for (int sIBtype=2;sIBtype<33;sIBtype++) { //21 ? 33 ?
+      for (int sIBtype=2;sIBtype<21;sIBtype++) {
+        if (f1ap_setup_resp->SI_container[i][sIBtype]!=NULL) {
+          AssertFatal(sIBtype < 6 || sIBtype == 9, "Illegal SI type %d\n",sIBtype);
+          F1AP_SibtypetobeupdatedListItem_t *sib_item = calloc(1,sizeof(*sib_item));
+          memset((void*)sib_item,0,sizeof(*sib_item));
+          sib_item->sIBtype = sIBtype;
+          OCTET_STRING_fromBuf(&sib_item->sIBmessage,
+                              (const char*)f1ap_setup_resp->SI_container[i][sIBtype], 
+                              f1ap_setup_resp->SI_container_length[i][sIBtype]);
+        
+          LOG_D(F1AP, "f1ap_setup_resp->SI_container_length[%d][%d] = %d \n", i,sIBtype,f1ap_setup_resp->SI_container_length[i][sIBtype]);
+          ASN_SEQUENCE_ADD(&gNB_CUSystemInformation->sibtypetobeupdatedlist.list,sib_item);
         }
       }
       cells_to_be_activated_list_itemExtIEs->extensionValue.choice.GNB_CUSystemInformation = *gNB_CUSystemInformation;
diff --git a/openair2/RRC/NR/MESSAGES/asn1_msg.c b/openair2/RRC/NR/MESSAGES/asn1_msg.c
index 9d19f3b342..d846de183d 100644
--- a/openair2/RRC/NR/MESSAGES/asn1_msg.c
+++ b/openair2/RRC/NR/MESSAGES/asn1_msg.c
@@ -379,6 +379,56 @@ uint8_t do_SIB1_NR(rrc_gNB_carrier_data_t *carrier,
   return((enc_rval.encoded+7)/8);
 }
 
+uint8_t do_SIB23_NR(rrc_gNB_carrier_data_t *carrier,
+                    gNB_RrcConfigurationReq *configuration) {
+  asn_enc_rval_t enc_rval;
+  SystemInformation_IEs__sib_TypeAndInfo__Member *sib2 = NULL;
+  SystemInformation_IEs__sib_TypeAndInfo__Member *sib3 = NULL;
+
+  NR_BCCH_DL_SCH_Message_t *sib_message = CALLOC(1,sizeof(NR_BCCH_DL_SCH_Message_t));
+  sib_message->message.present = NR_BCCH_DL_SCH_MessageType_PR_c1;
+  sib_message->message.choice.c1 = CALLOC(1,sizeof(struct NR_BCCH_DL_SCH_MessageType__c1));
+  sib_message->message.choice.c1->present = NR_BCCH_DL_SCH_MessageType__c1_PR_systemInformation;
+  sib_message->message.choice.c1->choice.systemInformation = CALLOC(1,sizeof(struct NR_SystemInformation));
+  
+  struct NR_SystemInformation *sib = sib_message->message.choice.c1->choice.systemInformation;
+  sib->criticalExtensions.present = NR_SystemInformation__criticalExtensions_PR_systemInformation;
+  sib->criticalExtensions.choice.systemInformation = CALLOC(1, sizeof(struct NR_SystemInformation_IEs));
+
+  struct NR_SystemInformation_IEs *ies = sib->criticalExtensions.choice.systemInformation;
+  sib2 = CALLOC(1, sizeof(SystemInformation_IEs__sib_TypeAndInfo__Member));
+  sib2->present = NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib2;
+  sib2->choice.sib2 = CALLOC(1, sizeof(struct NR_SIB2));
+  sib2->choice.sib2->cellReselectionInfoCommon.q_Hyst = NR_SIB2__cellReselectionInfoCommon__q_Hyst_dB1;
+  sib2->choice.sib2->cellReselectionServingFreqInfo.threshServingLowP = 2; // INTEGER (0..31)
+  sib2->choice.sib2->cellReselectionServingFreqInfo.cellReselectionPriority =  2; // INTEGER (0..7)
+  sib2->choice.sib2->intraFreqCellReselectionInfo.q_RxLevMin = -50; // INTEGER (-70..-22)
+  sib2->choice.sib2->intraFreqCellReselectionInfo.s_IntraSearchP = 2; // INTEGER (0..31)
+  sib2->choice.sib2->intraFreqCellReselectionInfo.t_ReselectionNR = 2; // INTEGER (0..7)
+  sib2->choice.sib2->intraFreqCellReselectionInfo.deriveSSB_IndexFromCell = true;
+  ASN_SEQUENCE_ADD(&ies->sib_TypeAndInfo.list, sib2);
+
+  sib3 = CALLOC(1, sizeof(SystemInformation_IEs__sib_TypeAndInfo__Member));
+  sib3->present = NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib3;
+  sib3->choice.sib3 = CALLOC(1, sizeof(struct NR_SIB3));
+  ASN_SEQUENCE_ADD(&ies->sib_TypeAndInfo.list, sib3);
+
+  //encode SIB to data
+  // carrier->SIB23 = (uint8_t *) malloc16(128);
+  enc_rval = uper_encode_to_buffer(&asn_DEF_NR_BCCH_DL_SCH_Message,
+                                   NULL,
+                                   (void *)sib_message,
+                                   carrier->SIB23,
+                                   100);
+  AssertFatal (enc_rval.encoded > 0, "ASN1 message encoding failed (%s, %lu)!\n",
+               enc_rval.failed_type->name, enc_rval.encoded);
+
+  if (enc_rval.encoded==-1) {
+    return(-1);
+  }
+
+  return((enc_rval.encoded+7)/8);
+}
 
 void  do_RLC_BEARER(uint8_t Mod_id,
                     int CC_id,
diff --git a/openair2/RRC/NR/MESSAGES/asn1_msg.h b/openair2/RRC/NR/MESSAGES/asn1_msg.h
index bfc4942138..7e7d09904f 100644
--- a/openair2/RRC/NR/MESSAGES/asn1_msg.h
+++ b/openair2/RRC/NR/MESSAGES/asn1_msg.h
@@ -65,6 +65,9 @@ uint8_t do_SIB1_NR(rrc_gNB_carrier_data_t *carrier,
   gNB_RrcConfigurationReq *configuration
                   );
 
+uint8_t do_SIB23_NR(rrc_gNB_carrier_data_t *carrier,
+                    gNB_RrcConfigurationReq *configuration);
+
 void do_RLC_BEARER(uint8_t Mod_id,
                     int CC_id,
                     struct NR_CellGroupConfig__rlc_BearerToAddModList *rlc_BearerToAddModList,
diff --git a/openair2/RRC/NR/nr_rrc_defs.h b/openair2/RRC/NR/nr_rrc_defs.h
index 8a0240a5f5..192e81a097 100644
--- a/openair2/RRC/NR/nr_rrc_defs.h
+++ b/openair2/RRC/NR/nr_rrc_defs.h
@@ -426,6 +426,9 @@ typedef struct {
   uint8_t                                   *SIB1;
   uint8_t                                   sizeof_SIB1;
 
+  uint8_t                                   *SIB23;
+  uint8_t                                   sizeof_SIB23;
+
   uint8_t                                   *ServingCellConfigCommon;
   uint8_t                                   sizeof_servingcellconfigcommon;
 
diff --git a/openair2/RRC/NR/rrc_gNB.c b/openair2/RRC/NR/rrc_gNB.c
index c25b70382c..e833f23574 100644
--- a/openair2/RRC/NR/rrc_gNB.c
+++ b/openair2/RRC/NR/rrc_gNB.c
@@ -216,6 +216,13 @@ static void init_NR_SI(gNB_RRC_INST *rrc, gNB_RrcConfigurationReq *configuration
   rrc->carrier.MIB             = (uint8_t *) malloc16(4);
   rrc->carrier.sizeof_MIB      = do_MIB_NR(rrc,0);
   rrc->carrier.sizeof_SIB1      = do_SIB1_NR(&rrc->carrier,configuration);
+  if (!NODE_IS_DU(rrc->node_type)) {
+    rrc->carrier.SIB23 = (uint8_t *) malloc16(100);
+    AssertFatal(rrc->carrier.SIB23 != NULL, "cannot allocate memory for SIB");
+    rrc->carrier.sizeof_SIB23 = do_SIB23_NR(&rrc->carrier, configuration);
+    LOG_I(NR_RRC,"do_SIB23_NR, size %d \n ", rrc->carrier.sizeof_SIB23);
+    AssertFatal(rrc->carrier.sizeof_SIB23 != 255,"FATAL, RC.nrrrc[mod].carrier[CC_id].sizeof_SIB23 == 255");
+  }
   LOG_I(NR_RRC,"Done init_NR_SI\n");
   if (!NODE_IS_CU(RC.nrrrc[0]->node_type)){
     rrc_mac_config_req_gNB(rrc->module_id,
@@ -2425,11 +2432,11 @@ void rrc_gNB_process_f1_setup_req(f1ap_setup_req_t *f1_setup_req) {
         F1AP_SETUP_RESP (msg_p).nrpci[cu_cell_ind]                         = f1_setup_req->nr_pci[i];
         int num_SI= 0;
 
-        // if (rrc->carrier.SIB23) {
-        //   F1AP_SETUP_RESP (msg_p).SI_container[cu_cell_ind][num_SI]        = rrc->carrier.SIB23;
-        //   F1AP_SETUP_RESP (msg_p).SI_container_length[cu_cell_ind][num_SI] = rrc->carrier.sizeof_SIB23;
-        //   num_SI++;
-        // }
+        if (rrc->carrier.SIB23) {
+          F1AP_SETUP_RESP (msg_p).SI_container[cu_cell_ind][2]        = rrc->carrier.SIB23;
+          F1AP_SETUP_RESP (msg_p).SI_container_length[cu_cell_ind][2] = rrc->carrier.sizeof_SIB23;
+          num_SI++;
+        }
 
         F1AP_SETUP_RESP (msg_p).num_SI[cu_cell_ind] = num_SI;
         cu_cell_ind++;
-- 
2.26.2