From 5217b76448ad23be105fd81f2b45be6a7cbbf7a0 Mon Sep 17 00:00:00 2001
From: zhenghuangkun <zhenghuangkun@cn.fujitsu.com>
Date: Mon, 14 Sep 2020 06:51:14 +0000
Subject: [PATCH] Add DownlinkNASTransport Message Add UEContextReleaseCommand
 Message Modify NGSetup Message Encode failed

---
 common/utils/msc/msc.h                  |  1 +
 openair2/COMMON/ngap_messages_types.h   |  3 +++
 openair2/GNB_APP/gnb_paramdef.h         |  2 +-
 openair3/NGAP/ngap_gNB.c                | 19 +++++++++++++---
 openair3/NGAP/ngap_gNB_defs.h           |  4 ++--
 openair3/NGAP/ngap_gNB_handlers.c       | 30 ++++++++++++-------------
 openair3/NGAP/ngap_gNB_nas_procedures.c | 27 +++++++++++-----------
 7 files changed, 52 insertions(+), 34 deletions(-)

diff --git a/common/utils/msc/msc.h b/common/utils/msc/msc.h
index efb0f24d3e7..b218d58e3dc 100644
--- a/common/utils/msc/msc.h
+++ b/common/utils/msc/msc.h
@@ -49,6 +49,7 @@ typedef enum {
   MSC_RLC_ENB,
   MSC_PDCP_ENB,
   MSC_RRC_ENB,
+  MSC_RRC_GNB,
   MSC_IP_ENB,
   MSC_S1AP_ENB,
   MSC_NGAP_GNB,
diff --git a/openair2/COMMON/ngap_messages_types.h b/openair2/COMMON/ngap_messages_types.h
index 9dbdc7b81e9..a15c7df650d 100644
--- a/openair2/COMMON/ngap_messages_types.h
+++ b/openair2/COMMON/ngap_messages_types.h
@@ -410,6 +410,9 @@ typedef struct ngap_register_gnb_req_s {
   uint8_t  mnc_digit_length[PLMN_LIST_MAX_SIZE];
   uint8_t  num_plmn;
 
+  uint16_t              num_nssai[PLMN_LIST_MAX_SIZE];
+  ngap_allowed_NSSAI_t  s_nssai[PLMN_LIST_MAX_SIZE][8];
+
   /* Default Paging DRX of the gNB as defined in TS 36.304 */
   ngap_paging_drx_t default_drx;
 
diff --git a/openair2/GNB_APP/gnb_paramdef.h b/openair2/GNB_APP/gnb_paramdef.h
index 7c311e28814..cf16695c904 100644
--- a/openair2/GNB_APP/gnb_paramdef.h
+++ b/openair2/GNB_APP/gnb_paramdef.h
@@ -145,7 +145,7 @@ typedef struct ccparams_nr_x2 {
 {GNB_CONFIG_STRING_GNB_ID,                       NULL,   0,            uptr:NULL,   defintval:0,                 TYPE_UINT,      0},  \
 {GNB_CONFIG_STRING_CELL_TYPE,                    NULL,   0,            strptr:NULL, defstrval:"CELL_MACRO_GNB",  TYPE_STRING,    0},  \
 {GNB_CONFIG_STRING_GNB_NAME,                     NULL,   0,            strptr:NULL, defstrval:"OAIgNodeB",       TYPE_STRING,    0},  \
-{GNB_CONFIG_STRING_TRACKING_AREA_CODE,           NULL,   0,            strptr:NULL, defstrval:"0",               TYPE_STRING,    0},  \
+{GNB_CONFIG_STRING_TRACKING_AREA_CODE,           NULL,   0,            uptr:NULL,   defuintval:0,                TYPE_UINT,      0},  \
 {GNB_CONFIG_STRING_MOBILE_COUNTRY_CODE_OLD,      NULL,   0,            strptr:NULL, defstrval:NULL,              TYPE_STRING,    0},  \
 {GNB_CONFIG_STRING_MOBILE_NETWORK_CODE_OLD,      NULL,   0,            strptr:NULL, defstrval:NULL,              TYPE_STRING,    0},  \
 {GNB_CONFIG_STRING_TRANSPORT_S_PREFERENCE,       NULL,   0,            strptr:NULL, defstrval:"local_mac",       TYPE_STRING,    0},  \
diff --git a/openair3/NGAP/ngap_gNB.c b/openair3/NGAP/ngap_gNB.c
index 7345149806b..0e26a66ddeb 100644
--- a/openair3/NGAP/ngap_gNB.c
+++ b/openair3/NGAP/ngap_gNB.c
@@ -202,10 +202,25 @@ void ngap_gNB_handle_register_gNB(instance_t instance, ngap_register_gnb_req_t *
       new_instance->mcc[i]              = ngap_register_gNB->mcc[i];
       new_instance->mnc[i]              = ngap_register_gNB->mnc[i];
       new_instance->mnc_digit_length[i] = ngap_register_gNB->mnc_digit_length[i];
+      
+      new_instance->num_nssai[i]        = ngap_register_gNB->num_nssai[i];
     }
 
     new_instance->num_plmn         = ngap_register_gNB->num_plmn;
     new_instance->default_drx      = ngap_register_gNB->default_drx;
+
+    memcpy(new_instance->s_nssai, ngap_register_gNB->s_nssai, sizeof(ngap_register_gNB->s_nssai));
+
+    // config add? TBD
+    if(1) {
+      new_instance->num_nssai[0] = 1;
+      new_instance->s_nssai[0][0].sST = 1;
+      new_instance->s_nssai[0][0].sD_flag = 1;
+      new_instance->s_nssai[0][0].sD[0] = 1;
+      new_instance->s_nssai[0][0].sD[1] = 2;
+      new_instance->s_nssai[0][0].sD[2] = 3;
+    }
+    
     /* Add the new instance to the list of gNB (meaningfull in virtual mode) */
     ngap_gNB_insert_new_instance(new_instance);
     NGAP_INFO("Registered new gNB[%d] and %s gNB id %u\n",
@@ -489,7 +504,6 @@ static int ngap_gNB_generate_ng_setup_request(
     ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
   }
 
-#if 0
   /* mandatory */
   ie = (NGAP_NGSetupRequestIEs_t *)calloc(1, sizeof(NGAP_NGSetupRequestIEs_t));
   ie->id = NGAP_ProtocolIE_ID_id_SupportedTAList;
@@ -497,7 +511,7 @@ static int ngap_gNB_generate_ng_setup_request(
   ie->value.present = NGAP_NGSetupRequestIEs__value_PR_SupportedTAList;
   {
     ta = (NGAP_SupportedTAItem_t *)calloc(1, sizeof(NGAP_SupportedTAItem_t));
-    INT16_TO_OCTET_STRING(instance_p->tac, &ta->tAC);
+    INT24_TO_OCTET_STRING(instance_p->tac, &ta->tAC);
     {
       for (int i = 0; i < ngap_amf_data_p->broadcast_plmn_num; ++i) {
         plmn = (NGAP_BroadcastPLMNItem_t *)calloc(1, sizeof(NGAP_BroadcastPLMNItem_t));
@@ -530,7 +544,6 @@ static int ngap_gNB_generate_ng_setup_request(
   }
   ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
   
-#endif
   /* mandatory */
   ie = (NGAP_NGSetupRequestIEs_t *)calloc(1, sizeof(NGAP_NGSetupRequestIEs_t));
   ie->id = NGAP_ProtocolIE_ID_id_DefaultPagingDRX;
diff --git a/openair3/NGAP/ngap_gNB_defs.h b/openair3/NGAP/ngap_gNB_defs.h
index 313df231faf..ee97f0fa9fc 100644
--- a/openair3/NGAP/ngap_gNB_defs.h
+++ b/openair3/NGAP/ngap_gNB_defs.h
@@ -262,9 +262,9 @@ typedef struct ngap_gNB_instance_s {
   uint8_t   num_plmn;
 
   uint16_t   num_nssai[PLMN_LIST_MAX_SIZE];
-  ngap_gNB_NSSAI_t s_nssai[PLMN_LIST_MAX_SIZE][1024];
+  ngap_gNB_NSSAI_t s_nssai[PLMN_LIST_MAX_SIZE][8];
   
-  /* Default Paging DRX of the gNB as defined in TS 36.304 */
+  /* Default Paging DRX of the gNB as defined in TS 38.304 */
   ngap_paging_drx_t default_drx;
 } ngap_gNB_instance_t;
 
diff --git a/openair3/NGAP/ngap_gNB_handlers.c b/openair3/NGAP/ngap_gNB_handlers.c
index ae57d3896aa..5d407b84bed 100644
--- a/openair3/NGAP/ngap_gNB_handlers.c
+++ b/openair3/NGAP/ngap_gNB_handlers.c
@@ -1113,12 +1113,12 @@ static
 int ngap_gNB_handle_ue_context_release_command(uint32_t   assoc_id,
     uint32_t               stream,
     NGAP_NGAP_PDU_t       *pdu) {
-#if 0
+
   ngap_gNB_amf_data_t   *amf_desc_p       = NULL;
   ngap_gNB_ue_context_t *ue_desc_p        = NULL;
   MessageDef            *message_p        = NULL;
-  NGAP_AMF_UE_NGAP_ID_t  amf_ue_ngap_id;
-  NGAP_GNB_UE_NGAP_ID_t  enb_ue_ngap_id;
+  uint64_t                            amf_ue_ngap_id;
+  NGAP_RAN_UE_NGAP_ID_t               gnb_ue_ngap_id;
   NGAP_UEContextReleaseCommand_t     *container;
   NGAP_UEContextReleaseCommand_IEs_t *ie;
   DevAssert(pdu != NULL);
@@ -1136,23 +1136,23 @@ int ngap_gNB_handle_ue_context_release_command(uint32_t   assoc_id,
   if (ie != NULL) { /* checked by macro but cppcheck doesn't see it */
     switch (ie->value.choice.UE_NGAP_IDs.present) {
       case NGAP_UE_NGAP_IDs_PR_uE_NGAP_ID_pair:
-        enb_ue_ngap_id = ie->value.choice.UE_NGAP_IDs.choice.uE_NGAP_ID_pair.gNB_UE_NGAP_ID;
-        amf_ue_ngap_id = ie->value.choice.UE_NGAP_IDs.choice.uE_NGAP_ID_pair.mME_UE_NGAP_ID;
+        gnb_ue_ngap_id = ie->value.choice.UE_NGAP_IDs.choice.uE_NGAP_ID_pair.rAN_UE_NGAP_ID;
+        asn_INTEGER2ulong(&(ie->value.choice.UE_NGAP_IDs.choice.uE_NGAP_ID_pair.aMF_UE_NGAP_ID), &amf_ue_ngap_id);
         MSC_LOG_RX_MESSAGE(
           MSC_NGAP_GNB,
           MSC_NGAP_AMF,
           NULL,0,
           "0 UEContextRelease/%s gNB_ue_ngap_id "NGAP_UE_ID_FMT" amf_ue_ngap_id "NGAP_UE_ID_FMT" len %u",
           ngap_direction2String(pdu->present - 1),
-          enb_ue_ngap_id,
+          gnb_ue_ngap_id,
           amf_ue_ngap_id);
 
         if ((ue_desc_p = ngap_gNB_get_ue_context(amf_desc_p->ngap_gNB_instance,
-                         enb_ue_ngap_id)) == NULL) {
+                         gnb_ue_ngap_id)) == NULL) {
           NGAP_ERROR("[SCTP %d] Received UE context release command for non "
                      "existing UE context 0x%06lx\n",
                      assoc_id,
-                     enb_ue_ngap_id);
+                     gnb_ue_ngap_id);
           return -1;
         } else {
           MSC_LOG_TX_MESSAGE(
@@ -1160,25 +1160,25 @@ int ngap_gNB_handle_ue_context_release_command(uint32_t   assoc_id,
             MSC_RRC_GNB,
             NULL,0,
             "0 NGAP_UE_CONTEXT_RELEASE_COMMAND/%d gNB_ue_ngap_id "NGAP_UE_ID_FMT" ",
-            enb_ue_ngap_id);
+            gnb_ue_ngap_id);
           message_p    = itti_alloc_new_message(TASK_NGAP, NGAP_UE_CONTEXT_RELEASE_COMMAND);
 
           if (ue_desc_p->amf_ue_ngap_id == 0) { // case of Detach Request and switch off from RRC_IDLE mode
             ue_desc_p->amf_ue_ngap_id = amf_ue_ngap_id;
           }
 
-          NGAP_UE_CONTEXT_RELEASE_COMMAND(message_p).gNB_ue_ngap_id = enb_ue_ngap_id;
+          NGAP_UE_CONTEXT_RELEASE_COMMAND(message_p).gNB_ue_ngap_id = gnb_ue_ngap_id;
           itti_send_msg_to_task(TASK_RRC_GNB, ue_desc_p->gNB_instance->instance, message_p);
           return 0;
         }
 
         break;
 
-      //#warning "TODO mapping amf_ue_ngap_id  enb_ue_ngap_id?"
+      //#warning "TODO mapping amf_ue_ngap_id  gnb_ue_ngap_id?"
 
-      case NGAP_UE_NGAP_IDs_PR_mME_UE_NGAP_ID:
-        amf_ue_ngap_id = ie->value.choice.UE_NGAP_IDs.choice.uE_NGAP_ID_pair.mME_UE_NGAP_ID;
-        NGAP_ERROR("TO DO mapping amf_ue_ngap_id  enb_ue_ngap_id");
+      case NGAP_UE_NGAP_IDs_PR_aMF_UE_NGAP_ID:
+        asn_INTEGER2ulong(&(ie->value.choice.UE_NGAP_IDs.choice.aMF_UE_NGAP_ID), &amf_ue_ngap_id);
+        NGAP_ERROR("TO DO mapping amf_ue_ngap_id  gnb_ue_ngap_id");
         (void)amf_ue_ngap_id; /* TODO: remove - it's to remove gcc warning about unused var */
 
       case NGAP_UE_NGAP_IDs_PR_NOTHING:
@@ -1193,7 +1193,7 @@ int ngap_gNB_handle_ue_context_release_command(uint32_t   assoc_id,
   NGAP_FIND_PROTOCOLIE_BY_ID(NGAP_UEContextReleaseCommand_IEs_t, ie, container,
                              NGAP_ProtocolIE_ID_id_Cause, true);
   /* TBD */
-#endif
+
   return 0;
 }
 
diff --git a/openair3/NGAP/ngap_gNB_nas_procedures.c b/openair3/NGAP/ngap_gNB_nas_procedures.c
index 32567f940d1..91e3a3d590d 100644
--- a/openair3/NGAP/ngap_gNB_nas_procedures.c
+++ b/openair3/NGAP/ngap_gNB_nas_procedures.c
@@ -324,14 +324,14 @@ int ngap_gNB_handle_nas_downlink(uint32_t         assoc_id,
                                  NGAP_NGAP_PDU_t *pdu)
 //------------------------------------------------------------------------------
 {
-#if 0
+
     ngap_gNB_amf_data_t             *amf_desc_p        = NULL;
     ngap_gNB_ue_context_t           *ue_desc_p         = NULL;
     ngap_gNB_instance_t             *ngap_gNB_instance = NULL;
     NGAP_DownlinkNASTransport_t     *container;
     NGAP_DownlinkNASTransport_IEs_t *ie;
-    NGAP_GNB_UE_NGAP_ID_t            enb_ue_ngap_id;
-    NGAP_AMF_UE_NGAP_ID_t            amf_ue_ngap_id;
+    NGAP_RAN_UE_NGAP_ID_t            gnb_ue_ngap_id;
+    uint64_t                         amf_ue_ngap_id;
     DevAssert(pdu != NULL);
 
     /* UE-related procedure -> stream != 0 */
@@ -353,25 +353,26 @@ int ngap_gNB_handle_nas_downlink(uint32_t         assoc_id,
     container = &pdu->choice.initiatingMessage.value.choice.DownlinkNASTransport;
     NGAP_FIND_PROTOCOLIE_BY_ID(NGAP_DownlinkNASTransport_IEs_t, ie, container,
                                NGAP_ProtocolIE_ID_id_AMF_UE_NGAP_ID, true);
-    amf_ue_ngap_id = ie->value.choice.AMF_UE_NGAP_ID;
+    asn_INTEGER2ulong(&(ie->value.choice.AMF_UE_NGAP_ID), &amf_ue_ngap_id);
+
 
     NGAP_FIND_PROTOCOLIE_BY_ID(NGAP_DownlinkNASTransport_IEs_t, ie, container,
-                               NGAP_ProtocolIE_ID_id_gNB_UE_NGAP_ID, true);
-    enb_ue_ngap_id = ie->value.choice.GNB_UE_NGAP_ID;
+                               NGAP_ProtocolIE_ID_id_RAN_UE_NGAP_ID, true);
+    gnb_ue_ngap_id = ie->value.choice.RAN_UE_NGAP_ID;
 
     if ((ue_desc_p = ngap_gNB_get_ue_context(ngap_gNB_instance,
-                     enb_ue_ngap_id)) == NULL) {
+                     gnb_ue_ngap_id)) == NULL) {
         MSC_LOG_RX_DISCARDED_MESSAGE(
             MSC_NGAP_GNB,
             MSC_NGAP_AMF,
             NULL,
             0,
             MSC_AS_TIME_FMT" downlinkNASTransport  gNB_ue_ngap_id %u amf_ue_ngap_id %u",
-            enb_ue_ngap_id,
+            gnb_ue_ngap_id,
             amf_ue_ngap_id);
         NGAP_ERROR("[SCTP %d] Received NAS downlink message for non existing UE context gNB_UE_NGAP_ID: 0x%lx\n",
                    assoc_id,
-                   enb_ue_ngap_id);
+                   gnb_ue_ngap_id);
         return -1;
     }
 
@@ -391,10 +392,10 @@ int ngap_gNB_handle_nas_downlink(uint32_t         assoc_id,
     } else {
         /* We already have a amf ue ngap id check the received is the same */
         if (ue_desc_p->amf_ue_ngap_id != amf_ue_ngap_id) {
-            NGAP_ERROR("[SCTP %d] Mismatch in AMF UE NGAP ID (0x%lx != 0x%"PRIx32"\n",
+            NGAP_ERROR("[SCTP %d] Mismatch in AMF UE NGAP ID (0x%lx != 0x%"PRIx64"\n",
                        assoc_id,
                        amf_ue_ngap_id,
-                       ue_desc_p->amf_ue_ngap_id
+                       (uint64_t)ue_desc_p->amf_ue_ngap_id
                       );
             return -1;
         }
@@ -411,13 +412,13 @@ int ngap_gNB_handle_nas_downlink(uint32_t         assoc_id,
 
     NGAP_FIND_PROTOCOLIE_BY_ID(NGAP_DownlinkNASTransport_IEs_t, ie, container,
                                NGAP_ProtocolIE_ID_id_NAS_PDU, true);
-    /* Forward the NAS PDU to RRC */
+    /* Forward the NAS PDU to NR-RRC */
     ngap_gNB_itti_send_nas_downlink_ind(ngap_gNB_instance->instance,
                                         ue_desc_p->ue_initial_id,
                                         ue_desc_p->gNB_ue_ngap_id,
                                         ie->value.choice.NAS_PDU.buf,
                                         ie->value.choice.NAS_PDU.size);
-#endif
+
     return 0;
 }
 
-- 
GitLab