From ce8567776254b952a44490c7e324071ce6c0153a Mon Sep 17 00:00:00 2001
From: Xue Song <xuesong@cn.fujitsu.com>
Date: Fri, 23 Oct 2020 03:42:06 +0000
Subject: [PATCH] modify some issues

---
 openair2/RRC/NR/rrc_gNB.c               | 100 ++++++++++--------------
 openair2/SIMULATION/NR_RRC/itti_sim.c   |  27 +------
 openair3/NGAP/ngap_gNB_nas_procedures.c |   3 +
 3 files changed, 46 insertions(+), 84 deletions(-)

diff --git a/openair2/RRC/NR/rrc_gNB.c b/openair2/RRC/NR/rrc_gNB.c
index 99711d0e98..be6230e36d 100644
--- a/openair2/RRC/NR/rrc_gNB.c
+++ b/openair2/RRC/NR/rrc_gNB.c
@@ -378,33 +378,7 @@ rrc_gNB_generate_RRCSetup(
             PROTOCOL_NR_RRC_CTXT_UE_FMT" RRC_gNB --- MAC_CONFIG_REQ  (SRB1) ---> MAC_gNB\n",
             PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
 
-    // rrc_mac_config_req_eNB( ctxt_pP->module_id,
-    //                         ue_context_pP->ue_context.primaryCC_id,
-    //                         0,0,0,0,0, 0,
-    //                         ue_context_pP->ue_context.rnti,
-    //                         NULL,
-    //                         NULL,
-    //                         NULL,
-    //                         ue_context_pP->ue_context.physicalConfigDedicated,
-    //                         NULL,
-    //                         NULL,
-    //                         ue_context_pP->ue_context.mac_MainConfig,
-    //                         1,
-    //                         SRB1_logicalChannelConfig,
-    //                         ue_context_pP->ue_context.measGapConfig,
-    //                         NULL,
-    //                         NULL,
-    //                         NULL,
-    //                         0, NULL, NULL, NULL,
-    //                         0, NULL, NULL,
-    //                         NULL,
-    //                         0,
-    //                         NULL,
-    //                         NULL,
-    //                         NULL,
-    //                         NULL,
-    //                         NULL,
-    //                         NULL);
+    // rrc_mac_config_req_eNB
 
     MSC_LOG_TX_MESSAGE(
         MSC_RRC_GNB,
@@ -462,6 +436,14 @@ rrc_gNB_generate_RRCReject(
           PROTOCOL_NR_RRC_CTXT_UE_FMT" [RAPROC] Logical Channel DL-CCCH, Generating NR_RRCReject (bytes %d)\n",
           PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
           ue_p->Srb0.Tx_buffer.payload_size);
+
+#ifdef ITTI_SIM
+    MessageDef *message_p;
+    message_p = itti_alloc_new_message (TASK_RRC_GNB_SIM, GNB_RRC_CCCH_DATA_IND);
+    GNB_RRC_CCCH_DATA_IND (message_p).sdu = (uint8_t*)ue_p->Srb0.Tx_buffer.Payload;
+    GNB_RRC_CCCH_DATA_IND (message_p).size  = ue_p->Srb0.Tx_buffer.payload_size;
+    itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
+#endif
 }
 
 //-----------------------------------------------------------------------------
@@ -479,18 +461,13 @@ rrc_gNB_process_RRCSetupComplete(
     LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" [RAPROC] Logical Channel UL-DCCH, " "processing NR_RRCSetupComplete from UE (SRB1 Active)\n",
         PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
     ue_context_pP->ue_context.Srb1.Active = 1;
-    ue_context_pP->ue_context.Status = RRC_CONNECTED;
-    // T(T_GNB_RRC_SETUP_COMPLETE,
-    //     T_INT(ctxt_pP->module_id),
-    //     T_INT(ctxt_pP->frame),
-    //     T_INT(ctxt_pP->subframe),
-    //     T_INT(ctxt_pP->rnti));
-
-    // if (AMF_MODE_ENABLED) {
-    //     rrc_gNB_send_NGAP_NAS_FIRST_REQ(ctxt_pP, ue_context_pP, rrcSetupComplete);
-    // } else {
+    ue_context_pP->ue_context.Status = NR_RRC_CONNECTED;
+
+    if (AMF_MODE_ENABLED) {
+        rrc_gNB_send_NGAP_NAS_FIRST_REQ(ctxt_pP, ue_context_pP, rrcSetupComplete);
+    } else {
         rrc_gNB_generate_SecurityModeCommand(ctxt_pP, ue_context_pP);
-    // }
+    }
 }
 
 //-----------------------------------------------------------------------------
@@ -605,24 +582,26 @@ rrc_gNB_process_RRCReconfigurationComplete(
                      MSC_AS_TIME_ARGS(ctxt_pP),
                      ue_context_pP->ue_context.rnti);
 
-    // nr_rrc_pdcp_config_asn1_req(ctxt_pP,
-    //                             SRB_configList, // NULL,
-    //                             DRB_configList,
-    //                             DRB_Release_configList2,
-    //                             0xff, // already configured during the securitymodecommand
-    //                             kRRCenc,
-    //                             kRRCint,
-    //                             kUPenc,
-    //                             NULL,
-    //                             NULL,
-    //                             NULL);
-    // /* Refresh SRBs/DRBs */
-    // nr_rrc_rlc_config_asn1_req(ctxt_pP,
-    //                         SRB_configList, // NULL,
-    //                         DRB_configList,
-    //                         DRB_Release_configList2,
-    //                         NULL,
-    //                         NULL);
+#ifndef ITTI_SIM
+    nr_rrc_pdcp_config_asn1_req(ctxt_pP,
+                                SRB_configList, // NULL,
+                                DRB_configList,
+                                DRB_Release_configList2,
+                                0xff, // already configured during the securitymodecommand
+                                kRRCenc,
+                                kRRCint,
+                                kUPenc,
+                                NULL,
+                                NULL,
+                                NULL);
+    /* Refresh SRBs/DRBs */
+    nr_rrc_rlc_config_asn1_req(ctxt_pP,
+                            SRB_configList, // NULL,
+                            DRB_configList,
+                            DRB_Release_configList2,
+                            NULL,
+                            NULL);
+#endif
 
     /* Loop through DRBs and establish if necessary */
     /* Set the SRB active in UE context */
@@ -850,8 +829,13 @@ int nr_rrc_gNB_decode_ccch(protocol_ctxt_t    *const ctxt_pP,
                                 ue_context_p->ue_context.ng_5G_S_TMSI_Part1 = s_tmsi_part1;
                             }
                         }
-                    } //else {
+                    } else {
                         /* TODO */
+                        memcpy(((uint8_t *) & random_value) + 3,
+                                rrcSetupRequest->ue_Identity.choice.randomValue.buf,
+                                rrcSetupRequest->ue_Identity.choice.randomValue.size);
+
+                        rrc_gNB_get_next_free_ue_context(ctxt_pP, RC.nrrrc[ctxt_pP->module_id], random_value);
                         LOG_E(NR_RRC,
                                 PROTOCOL_NR_RRC_CTXT_UE_FMT" RRCSetupRequest without random UE identity or S-TMSI not supported, let's reject the UE\n",
                                 PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
@@ -859,7 +843,7 @@ int nr_rrc_gNB_decode_ccch(protocol_ctxt_t    *const ctxt_pP,
                                                    rrc_gNB_get_ue_context(gnb_rrc_inst, ctxt_pP->rnti),
                                                    CC_id);
                         break;
-                    //}
+                    }
                 }
 
                 if (ue_context_p != NULL) {
diff --git a/openair2/SIMULATION/NR_RRC/itti_sim.c b/openair2/SIMULATION/NR_RRC/itti_sim.c
index db3da25e21..6d0ae5f7fc 100644
--- a/openair2/SIMULATION/NR_RRC/itti_sim.c
+++ b/openair2/SIMULATION/NR_RRC/itti_sim.c
@@ -584,7 +584,7 @@ int main( int argc, char **argv )
   // wait for end of program
   printf("TYPE <CTRL-C> TO TERMINATE\n");
 
-  usleep(10000);
+  usleep(100000);
   protocol_ctxt_t ctxt;
   struct rrc_gNB_ue_context_s *ue_context_p = NULL;
 
@@ -604,31 +604,6 @@ int main( int argc, char **argv )
 
   rrc_ue_generate_RRCSetupRequest(&ctxt, 0);
 
-#if 0
-  // test itti sim
-  usleep(10000);
-
-  protocol_ctxt_t ctxt;
-  struct rrc_gNB_ue_context_s *ue_context_p = NULL;
-
-
-  ue_context_p = rrc_gNB_allocate_new_UE_context(RC.nrrrc[0]);
-
-  if(ue_context_p == NULL){
-    printf("ue_context_p == NULL");
-  }
-  PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt,
-                                0,
-                                ENB_FLAG_YES,
-                                0,
-                                0,
-                                0);
-  rrc_gNB_generate_RRCSetup(&ctxt,
-                            ue_context_p,
-                            0);
-  // end test itti sim
-#endif
-
   printf("Entering ITTI signals handler\n");
   itti_wait_tasks_end();
   printf("Returned from ITTI signal handler\n");
diff --git a/openair3/NGAP/ngap_gNB_nas_procedures.c b/openair3/NGAP/ngap_gNB_nas_procedures.c
index d49f6951ac..9e99051e55 100644
--- a/openair3/NGAP/ngap_gNB_nas_procedures.c
+++ b/openair3/NGAP/ngap_gNB_nas_procedures.c
@@ -70,6 +70,7 @@ int ngap_gNB_handle_nas_first_req(
     DevAssert(instance_p != NULL);
     memset(&pdu, 0, sizeof(pdu));
     pdu.present = NGAP_NGAP_PDU_PR_initiatingMessage;
+    pdu.choice.initiatingMessage = (NGAP_InitiatingMessage_t *)calloc(1,sizeof(NGAP_InitiatingMessage_t));
     pdu.choice.initiatingMessage->procedureCode = NGAP_ProcedureCode_id_InitialUEMessage;
     pdu.choice.initiatingMessage->criticality = NGAP_Criticality_ignore;
     pdu.choice.initiatingMessage->value.present = NGAP_InitiatingMessage__value_PR_InitialUEMessage;
@@ -220,6 +221,8 @@ int ngap_gNB_handle_nas_first_req(
 
     ie->value.choice.UserLocationInformation.present = NGAP_UserLocationInformation_PR_userLocationInformationNR;
 
+    ie->value.choice.UserLocationInformation.choice.userLocationInformationNR = 
+        CALLOC(1, sizeof(*ie->value.choice.UserLocationInformation.choice.userLocationInformationNR));
     userinfo_nr_p = ie->value.choice.UserLocationInformation.choice.userLocationInformationNR;
 
     /* Set nRCellIdentity. default userLocationInformationNR */
-- 
2.26.2