From 5918c03cd4370e7c15bf0470dff544fecb892280 Mon Sep 17 00:00:00 2001
From: Xue Song <xuesong@cn.fujitsu.com>
Date: Wed, 3 Feb 2021 17:00:44 +0900
Subject: [PATCH] add nrue nas task

---
 common/utils/ocp_itti/intertask_interface.h |   1 +
 openair2/RRC/NR_UE/rrc_UE.c                 | 126 ++-----------
 openair2/SIMULATION/NR_RRC/itti_sim.c       |   9 +
 openair3/NAS/NR_UE/nr_nas_msg_sim.c         | 198 ++++++++++++++++++++
 openair3/NAS/NR_UE/nr_nas_msg_sim.h         |   1 +
 5 files changed, 221 insertions(+), 114 deletions(-)

diff --git a/common/utils/ocp_itti/intertask_interface.h b/common/utils/ocp_itti/intertask_interface.h
index 18a8f7868c2..db4330d02e2 100644
--- a/common/utils/ocp_itti/intertask_interface.h
+++ b/common/utils/ocp_itti/intertask_interface.h
@@ -345,6 +345,7 @@ void *rrc_enb_process_msg(void *);
   TASK_DEF(TASK_DU_F1,    TASK_PRIORITY_MED,  200, NULL, NULL) \
   TASK_DEF(TASK_RRC_UE_SIM,   TASK_PRIORITY_MED,  200, NULL, NULL)  \
   TASK_DEF(TASK_RRC_GNB_SIM,  TASK_PRIORITY_MED,  200, NULL, NULL)  \
+  TASK_DEF(TASK_NAS_NRUE,     TASK_PRIORITY_MED,  200, NULL, NULL)  \
   TASK_DEF(TASK_MAX,      TASK_PRIORITY_MED,  200, NULL, NULL)
 
 #define TASK_DEF(TaskID, pRIO, qUEUEsIZE, FuNc, ThreadFunc)          { pRIO, qUEUEsIZE, #TaskID, FuNc, ThreadFunc },
diff --git a/openair2/RRC/NR_UE/rrc_UE.c b/openair2/RRC/NR_UE/rrc_UE.c
index b83297e4020..650d6bf19b2 100644
--- a/openair2/RRC/NR_UE/rrc_UE.c
+++ b/openair2/RRC/NR_UE/rrc_UE.c
@@ -127,7 +127,6 @@ nr_rrc_ue_generate_rrcReestablishmentComplete(
 );
 
 mui_t nr_rrc_mui=0;
-uint8_t first_rrcreconfigurationcomplete = 0;
 
 static Rrc_State_NR_t nr_rrc_get_state (module_id_t ue_mod_idP) {
   return NR_UE_rrc_inst[ue_mod_idP].nrRrcState;
@@ -2252,34 +2251,11 @@ rrc_ue_process_rrcReconfiguration(
       for (list_count = 0; list_count < ie->nonCriticalExtension->dedicatedNAS_MessageList->list.count; list_count++) {
         pdu_length = ie->nonCriticalExtension->dedicatedNAS_MessageList->list.array[list_count]->size;
         pdu_buffer = ie->nonCriticalExtension->dedicatedNAS_MessageList->list.array[list_count]->buf;
-#ifdef ITTI_SIM
-        uint8_t msg_type = 0;
-        if((pdu_buffer + 1) != NULL){
-          if (*(pdu_buffer + 1) > 0 ) {
-            if((pdu_buffer + 9) != NULL){
-              msg_type = *(pdu_buffer + 9);
-            } else {
-              LOG_W(NR_RRC, "[UE] Received invalid downlink message\n");
-              return;
-            }
-          } else {
-            if((pdu_buffer + 2) != NULL){
-              msg_type = *(pdu_buffer + 2);
-            } else {
-              LOG_W(NR_RRC, "[UE] Received invalid downlink message\n");
-              return;
-            }
-          }
-        }
-        if(msg_type == REGISTRATION_ACCEPT){
-          LOG_I(NR_RRC, "[UE] Received REGISTRATION ACCEPT message\n");
-        }
-#endif
         msg_p = itti_alloc_new_message(TASK_RRC_NRUE, 0, NAS_CONN_ESTABLI_CNF);
         NAS_CONN_ESTABLI_CNF(msg_p).errCode = AS_SUCCESS;
         NAS_CONN_ESTABLI_CNF(msg_p).nasMsg.length = pdu_length;
         NAS_CONN_ESTABLI_CNF(msg_p).nasMsg.data = pdu_buffer;
-        itti_send_msg_to_task(TASK_NAS_UE, ctxt_pP->instance, msg_p);
+        itti_send_msg_to_task(TASK_NAS_NRUE, ctxt_pP->instance, msg_p);
       }
 
       free (ie->nonCriticalExtension->dedicatedNAS_MessageList);
@@ -2376,46 +2352,14 @@ nr_rrc_ue_decode_dcch(
 
             case NR_DL_DCCH_MessageType__c1_PR_rrcReconfiguration:
             {
-                rrc_ue_process_rrcReconfiguration(ctxt_pP,
-                                                    dl_dcch_msg->message.choice.c1->choice.rrcReconfiguration,
-                                                    gNB_indexP);
-                nr_rrc_ue_generate_RRCReconfigurationComplete(ctxt_pP,
-                                            gNB_indexP,
-                                            dl_dcch_msg->message.choice.c1->choice.rrcReconfiguration->rrc_TransactionIdentifier);
-
-                if (first_rrcreconfigurationcomplete == 0) {
-                    first_rrcreconfigurationcomplete = 1;
-#ifdef ITTI_SIM
-                  if (AMF_MODE_ENABLED) {
-                    as_nas_info_t initialNasMsg;
-                    memset(&initialNasMsg, 0, sizeof(as_nas_info_t));
-                    generateRegistrationComplete(&initialNasMsg, NULL);
-                    if(initialNasMsg.length > 0){
-                        MessageDef *message_p;
-                        message_p = itti_alloc_new_message(TASK_RRC_NRUE, 0, NAS_UPLINK_DATA_REQ);
-                        NAS_UPLINK_DATA_REQ(message_p).UEid          = ctxt_pP->module_id;
-                        NAS_UPLINK_DATA_REQ(message_p).nasMsg.data   = (uint8_t *)initialNasMsg.data;
-                        NAS_UPLINK_DATA_REQ(message_p).nasMsg.length = initialNasMsg.length;
-                        itti_send_msg_to_task(TASK_RRC_NRUE, ctxt_pP->instance, message_p);
-                        LOG_I(NR_RRC, " Send NAS_UPLINK_DATA_REQ message(RegistrationComplete)\n");
-                    }
-                    as_nas_info_t pduEstablishMsg;
-                    memset(&pduEstablishMsg, 0, sizeof(as_nas_info_t));
-                    generatePduSessionEstablishRequest(&pduEstablishMsg);
-                    if(initialNasMsg.length > 0){
-                        MessageDef *message_p;
-                        message_p = itti_alloc_new_message(TASK_RRC_NRUE, 0, NAS_UPLINK_DATA_REQ);
-                        NAS_UPLINK_DATA_REQ(message_p).UEid          = ctxt_pP->module_id;
-                        NAS_UPLINK_DATA_REQ(message_p).nasMsg.data   = (uint8_t *)pduEstablishMsg.data;
-                        NAS_UPLINK_DATA_REQ(message_p).nasMsg.length = pduEstablishMsg.length;
-                        itti_send_msg_to_task(TASK_RRC_NRUE, ctxt_pP->instance, message_p);
-                        LOG_I(NR_RRC, " Send NAS_UPLINK_DATA_REQ message(PduSessionEstablishRequest)\n");
-                    }
-                  }
-#endif
-                }
+              rrc_ue_process_rrcReconfiguration(ctxt_pP,
+                                                  dl_dcch_msg->message.choice.c1->choice.rrcReconfiguration,
+                                                  gNB_indexP);
+              nr_rrc_ue_generate_RRCReconfigurationComplete(ctxt_pP,
+                                          gNB_indexP,
+                                          dl_dcch_msg->message.choice.c1->choice.rrcReconfiguration->rrc_TransactionIdentifier);
+              break;
             }
-                break;
 
             case NR_DL_DCCH_MessageType__c1_PR_rrcResume:
             case NR_DL_DCCH_MessageType__c1_PR_rrcRelease:
@@ -2461,59 +2405,13 @@ nr_rrc_ue_decode_dcch(
                   /* This message hold a dedicated info NAS payload, forward it to NAS */
                   NR_DedicatedNAS_Message_t *dedicatedNAS_Message =
                       dlInformationTransfer->criticalExtensions.choice.dlInformationTransfer->dedicatedNAS_Message;
-                  uint32_t pdu_length;
-                  uint8_t *pdu_buffer;
-                  pdu_length = dedicatedNAS_Message->size;
-                  pdu_buffer = dedicatedNAS_Message->buf;
-#ifdef ITTI_SIM
-                  LOG_I(NR_RRC, "[UE %d] Received %s: UEid %u, length %u , buffer %p\n", ctxt_pP->module_id,  messages_info[NAS_DOWNLINK_DATA_IND].name,
-                        ctxt_pP->module_id, pdu_length, pdu_buffer);
-                  as_nas_info_t initialNasMsg;
-                  uint8_t msg_type = 0;
-                  memset(&initialNasMsg, 0, sizeof(as_nas_info_t));
-                  if((pdu_buffer + 1) != NULL){
-                    if (*(pdu_buffer + 1) > 0 ) {
-                      msg_type = *(pdu_buffer + 9);
-                    } else {
-                      msg_type = *(pdu_buffer + 2);
-                    }
-                  }
-                  if((pdu_buffer + 2) == NULL){
-                    LOG_W(NR_RRC, "[UE] Received invalid downlink message\n");
-                    return 0;
-                  }
-
-                  switch(msg_type){
-                    case FGS_IDENTITY_REQUEST:
-                       generateIdentityResponse(&initialNasMsg,*(pdu_buffer+3));
-                       break;
-                    case FGS_AUTHENTICATION_REQUEST:
-                       generateAuthenticationResp(&initialNasMsg, pdu_buffer);
-                       break;
-                    case FGS_SECURITY_MODE_COMMAND:
-                      generateSecurityModeComplete(&initialNasMsg);
-                      break;
-                    default:
-                       LOG_W(NR_RRC,"unknow message type %d\n",msg_type);
-                       break;
-                  }
-                  if(initialNasMsg.length > 0){
-                    MessageDef *message_p;
-                    message_p = itti_alloc_new_message(TASK_RRC_NRUE, 0, NAS_UPLINK_DATA_REQ);
-                    NAS_UPLINK_DATA_REQ(message_p).UEid          = ctxt_pP->module_id;
-                    NAS_UPLINK_DATA_REQ(message_p).nasMsg.data   = (uint8_t *)initialNasMsg.data;
-                    NAS_UPLINK_DATA_REQ(message_p).nasMsg.length = initialNasMsg.length;
-                    itti_send_msg_to_task(TASK_RRC_NRUE, ctxt_pP->instance, message_p);
-                    LOG_I(NR_RRC, " Send NAS_UPLINK_DATA_REQ message\n");
-                  }
-#else
+
                   MessageDef *msg_p;
                   msg_p = itti_alloc_new_message(TASK_RRC_NRUE, 0, NAS_DOWNLINK_DATA_IND);
                   NAS_DOWNLINK_DATA_IND(msg_p).UEid = ctxt_pP->module_id; // TODO set the UEid to something else ?
-                  NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.length = pdu_length;
-                  NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.data = pdu_buffer;
-                  itti_send_msg_to_task(TASK_NAS_UE, ctxt_pP->instance, msg_p);
-#endif
+                  NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.length = dedicatedNAS_Message->size;
+                  NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.data = dedicatedNAS_Message->buf;
+                  itti_send_msg_to_task(TASK_NAS_NRUE, ctxt_pP->instance, msg_p);
               }
             }
 
diff --git a/openair2/SIMULATION/NR_RRC/itti_sim.c b/openair2/SIMULATION/NR_RRC/itti_sim.c
index e11a92d6912..cf732bc40a7 100644
--- a/openair2/SIMULATION/NR_RRC/itti_sim.c
+++ b/openair2/SIMULATION/NR_RRC/itti_sim.c
@@ -81,6 +81,9 @@ unsigned short config_frames[4] = {2,9,11,13};
 #include "RRC/NR_UE/rrc_proto.h"
 #include "RRC/NR_UE/rrc_vars.h"
 #include "openair3/NAS/UE/nas_ue_task.h"
+#if ITTI_SIM
+#include "nr_nas_msg_sim.h"
+#endif
 
 pthread_cond_t nfapi_sync_cond;
 pthread_mutex_t nfapi_sync_mutex;
@@ -404,6 +407,12 @@ int create_tasks_nrue(uint32_t ue_nb) {
       LOG_E(NR_RRC, "Create task for RRC UE failed\n");
       return -1;
     }
+
+    printf("create TASK_NAS_NRUE\n");
+    if (itti_create_task (TASK_NAS_NRUE, nas_nrue_task, NULL) < 0) {
+      LOG_E(NR_RRC, "Create task for NAS UE failed\n");
+      return -1;
+    }
   }
 
 
diff --git a/openair3/NAS/NR_UE/nr_nas_msg_sim.c b/openair3/NAS/NR_UE/nr_nas_msg_sim.c
index 2c07de8dd5a..008ecc8e368 100644
--- a/openair3/NAS/NR_UE/nr_nas_msg_sim.c
+++ b/openair3/NAS/NR_UE/nr_nas_msg_sim.c
@@ -38,6 +38,8 @@
 #include "aka_functions.h"
 #include "secu_defs.h"
 #include "PduSessionEstablishRequest.h"
+# include "intertask_interface.h"
+
 char netName[] = "5G:mnc093.mcc208.3gppnetwork.org";
 char imsi[] = "2089300007487";
 // USIM_API_K: 5122250214c33e723a5dd523fc145fc0
@@ -690,3 +692,199 @@ void generatePduSessionEstablishRequest(as_nas_info_t *initialNasMsg){
      initialNasMsg->data[2+i] = mac[i];
   }
 }
+
+void *nas_nrue_task(void *args_p)
+{
+  MessageDef           *msg_p;
+  instance_t            instance;
+  unsigned int          Mod_id;
+  int                   result;
+  uint8_t               msg_type = 0;
+  uint8_t              *pdu_buffer = NULL;
+
+  itti_mark_task_ready (TASK_NAS_NRUE);
+  MSC_START_USE();
+  
+  while(1) {
+    // Wait for a message or an event
+    itti_receive_msg (TASK_NAS_NRUE, &msg_p);
+
+    if (msg_p != NULL) {
+      instance = msg_p->ittiMsgHeader.originInstance;
+      Mod_id = instance ;
+      if (instance == INSTANCE_DEFAULT) {
+        printf("%s:%d: FATAL: instance is INSTANCE_DEFAULT, should not happen.\n",
+               __FILE__, __LINE__);
+        exit_fun("exit... \n");
+      }
+
+      switch (ITTI_MSG_ID(msg_p)) {
+      case INITIALIZE_MESSAGE:
+        LOG_I(NAS, "[UE %d] Received %s\n", Mod_id,  ITTI_MSG_NAME (msg_p));
+
+        break;
+
+      case TERMINATE_MESSAGE:
+        itti_exit_task ();
+        break;
+
+      case MESSAGE_TEST:
+        LOG_I(NAS, "[UE %d] Received %s\n", Mod_id,  ITTI_MSG_NAME (msg_p));
+        break;
+
+      case NAS_CELL_SELECTION_CNF:
+        LOG_I(NAS, "[UE %d] Received %s: errCode %u, cellID %u, tac %u\n", Mod_id,  ITTI_MSG_NAME (msg_p),
+              NAS_CELL_SELECTION_CNF (msg_p).errCode, NAS_CELL_SELECTION_CNF (msg_p).cellID, NAS_CELL_SELECTION_CNF (msg_p).tac);
+        // as_stmsi_t s_tmsi={0, 0};
+        // as_nas_info_t nas_info;
+        // plmn_t plmnID={0, 0, 0, 0};
+        // generateRegistrationRequest(&nas_info);
+        // nr_nas_itti_nas_establish_req(0, AS_TYPE_ORIGINATING_SIGNAL, s_tmsi, plmnID, nas_info.data, nas_info.length, 0);
+        break;
+
+      case NAS_CELL_SELECTION_IND:
+        LOG_I(NAS, "[UE %d] Received %s: cellID %u, tac %u\n", Mod_id,  ITTI_MSG_NAME (msg_p),
+              NAS_CELL_SELECTION_IND (msg_p).cellID, NAS_CELL_SELECTION_IND (msg_p).tac);
+
+        /* TODO not processed by NAS currently */
+        break;
+
+      case NAS_PAGING_IND:
+        LOG_I(NAS, "[UE %d] Received %s: cause %u\n", Mod_id,  ITTI_MSG_NAME (msg_p),
+              NAS_PAGING_IND (msg_p).cause);
+
+        /* TODO not processed by NAS currently */
+        break;
+
+      case NAS_CONN_ESTABLI_CNF:
+      {
+        LOG_I(NAS, "[UE %d] Received %s: errCode %u, length %u\n", Mod_id,  ITTI_MSG_NAME (msg_p),
+              NAS_CONN_ESTABLI_CNF (msg_p).errCode, NAS_CONN_ESTABLI_CNF (msg_p).nasMsg.length);
+
+        pdu_buffer = NAS_CONN_ESTABLI_CNF (msg_p).nasMsg.data;
+        if((pdu_buffer + 1) != NULL){
+          if (*(pdu_buffer + 1) > 0 ) {
+            if((pdu_buffer + 9) != NULL){
+                msg_type = *(pdu_buffer + 9);
+            } else {
+              LOG_W(NAS, "[UE] Received invalid downlink message\n");
+              break;
+            }
+          } else {
+            if((pdu_buffer + 2) != NULL){
+              msg_type = *(pdu_buffer + 2);
+            } else {
+                LOG_W(NAS, "[UE] Received invalid downlink message\n");
+                break;
+            }
+          }
+        }
+        if(msg_type == REGISTRATION_ACCEPT){
+          LOG_I(NAS, "[UE] Received REGISTRATION ACCEPT message\n");
+
+          as_nas_info_t initialNasMsg;
+          memset(&initialNasMsg, 0, sizeof(as_nas_info_t));
+          generateRegistrationComplete(&initialNasMsg, NULL);
+          if(initialNasMsg.length > 0){
+            MessageDef *message_p;
+            message_p = itti_alloc_new_message(TASK_NAS_NRUE, 0, NAS_UPLINK_DATA_REQ);
+            NAS_UPLINK_DATA_REQ(message_p).UEid          = Mod_id;
+            NAS_UPLINK_DATA_REQ(message_p).nasMsg.data   = (uint8_t *)initialNasMsg.data;
+            NAS_UPLINK_DATA_REQ(message_p).nasMsg.length = initialNasMsg.length;
+            itti_send_msg_to_task(TASK_RRC_NRUE, instance, message_p);
+            LOG_I(NAS, "Send NAS_UPLINK_DATA_REQ message(RegistrationComplete)\n");
+          }
+
+          as_nas_info_t pduEstablishMsg;
+          memset(&pduEstablishMsg, 0, sizeof(as_nas_info_t));
+          generatePduSessionEstablishRequest(&pduEstablishMsg);
+          if(pduEstablishMsg.length > 0){
+            MessageDef *message_p;
+            message_p = itti_alloc_new_message(TASK_NAS_NRUE, 0, NAS_UPLINK_DATA_REQ);
+            NAS_UPLINK_DATA_REQ(message_p).UEid          = Mod_id;
+            NAS_UPLINK_DATA_REQ(message_p).nasMsg.data   = (uint8_t *)pduEstablishMsg.data;
+            NAS_UPLINK_DATA_REQ(message_p).nasMsg.length = pduEstablishMsg.length;
+            itti_send_msg_to_task(TASK_RRC_NRUE, instance, message_p);
+            LOG_I(NAS, "Send NAS_UPLINK_DATA_REQ message(PduSessionEstablishRequest)\n");
+          }
+        }
+
+        break;
+      }
+
+      case NAS_CONN_RELEASE_IND:
+        LOG_I(NAS, "[UE %d] Received %s: cause %u\n", Mod_id, ITTI_MSG_NAME (msg_p),
+              NAS_CONN_RELEASE_IND (msg_p).cause);
+
+        break;
+
+      case NAS_UPLINK_DATA_CNF:
+        LOG_I(NAS, "[UE %d] Received %s: UEid %u, errCode %u\n", Mod_id, ITTI_MSG_NAME (msg_p),
+              NAS_UPLINK_DATA_CNF (msg_p).UEid, NAS_UPLINK_DATA_CNF (msg_p).errCode);
+
+        break;
+
+      case NAS_DOWNLINK_DATA_IND:
+      {
+        LOG_I(NAS, "[UE %d] Received %s: UEid %u, length %u , buffer %p\n", Mod_id,
+                                                                            ITTI_MSG_NAME (msg_p),
+                                                                            Mod_id,
+                                                                            NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.length,
+                                                                            NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.data);
+        as_nas_info_t initialNasMsg;
+        memset(&initialNasMsg, 0, sizeof(as_nas_info_t));
+
+        pdu_buffer = NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.data;
+        if((pdu_buffer + 1) != NULL){
+          if (*(pdu_buffer + 1) > 0 ) {
+            msg_type = *(pdu_buffer + 9);
+          } else {
+            msg_type = *(pdu_buffer + 2);
+          }
+        }
+        if((pdu_buffer + 2) == NULL){
+          LOG_W(NAS, "[UE] Received invalid downlink message\n");
+          return 0;
+        }
+
+        switch(msg_type){
+          case FGS_IDENTITY_REQUEST:
+              generateIdentityResponse(&initialNasMsg,*(pdu_buffer+3));
+              break;
+          case FGS_AUTHENTICATION_REQUEST:
+              generateAuthenticationResp(&initialNasMsg, pdu_buffer);
+              break;
+          case FGS_SECURITY_MODE_COMMAND:
+            generateSecurityModeComplete(&initialNasMsg);
+            break;
+          default:
+              LOG_W(NR_RRC,"unknow message type %d\n",msg_type);
+              break;
+        }
+
+        if(initialNasMsg.length > 0){
+          MessageDef *message_p;
+          message_p = itti_alloc_new_message(TASK_NAS_NRUE, 0, NAS_UPLINK_DATA_REQ);
+          NAS_UPLINK_DATA_REQ(message_p).UEid          = Mod_id;
+          NAS_UPLINK_DATA_REQ(message_p).nasMsg.data   = (uint8_t *)initialNasMsg.data;
+          NAS_UPLINK_DATA_REQ(message_p).nasMsg.length = initialNasMsg.length;
+          itti_send_msg_to_task(TASK_RRC_NRUE, instance, message_p);
+          LOG_I(NAS, "Send NAS_UPLINK_DATA_REQ message\n");
+        }
+
+        break;
+      }
+
+      default:
+        LOG_E(NAS, "[UE %d] Received unexpected message %s\n", Mod_id,  ITTI_MSG_NAME (msg_p));
+        break;
+      }
+
+      result = itti_free (ITTI_MSG_ORIGIN_ID(msg_p), msg_p);
+      AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
+      msg_p = NULL;
+    }
+  }
+
+  return NULL;
+}
diff --git a/openair3/NAS/NR_UE/nr_nas_msg_sim.h b/openair3/NAS/NR_UE/nr_nas_msg_sim.h
index fef4671f434..2f123a6f048 100644
--- a/openair3/NAS/NR_UE/nr_nas_msg_sim.h
+++ b/openair3/NAS/NR_UE/nr_nas_msg_sim.h
@@ -120,6 +120,7 @@ void generateAuthenticationResp(as_nas_info_t *initialNasMsg, uint8_t *buf);
 void generateSecurityModeComplete(as_nas_info_t *initialNasMsg);
 void generateRegistrationComplete(as_nas_info_t *initialNasMsg, SORTransparentContainer *sortransparentcontainer);
 void generatePduSessionEstablishRequest(as_nas_info_t *initialNasMsg);
+void *nas_nrue_task(void *args_p);
 
 #endif /* __NR_NAS_MSG_SIM_H__*/
 
-- 
GitLab