From 1a38a436aa8d8f278cef03eec188e9e95b3b9ca3 Mon Sep 17 00:00:00 2001
From: cucengineer <2702919197@qq.com>
Date: Tue, 24 Nov 2020 19:26:38 +0800
Subject: [PATCH] NAS ok except identity

---
 executables/nr-uesoftmodem.c       |   8 +-
 openair2/RRC/NR_UE/rrc_UE.c        |   2 +-
 openair3/NAS/NR_UE/nas_nrue_task.c | 129 ++++++++++++++++++++---------
 openair3/NAS/NR_UE/nas_nrue_task.h |   7 +-
 4 files changed, 99 insertions(+), 47 deletions(-)

diff --git a/executables/nr-uesoftmodem.c b/executables/nr-uesoftmodem.c
index b9d97caa09..95c77cbf36 100644
--- a/executables/nr-uesoftmodem.c
+++ b/executables/nr-uesoftmodem.c
@@ -597,7 +597,7 @@ void *rrc_enb_process_msg(void *notUsed) {
 }
 
 
-extern void tesths(void);
+// extern void tesths(void);//CUC:test
 
 int main( int argc, char **argv ) {
   
@@ -662,9 +662,9 @@ int main( int argc, char **argv ) {
 #endif
 #endif
 */
-  printf("1111111");
-  tesths ();
-  printf("1111111");
+  // printf("witcomm111witcomm: \n");//CUC:test
+  // tesths ();
+  // printf("witcomm111witcomm \n");
 
   NB_UE_INST=1;
   NB_INST=1;
diff --git a/openair2/RRC/NR_UE/rrc_UE.c b/openair2/RRC/NR_UE/rrc_UE.c
index 3bf6054303..2eef36a179 100755
--- a/openair2/RRC/NR_UE/rrc_UE.c
+++ b/openair2/RRC/NR_UE/rrc_UE.c
@@ -2445,7 +2445,7 @@ void *rrc_nrue_task( void *args_p ) {
         uint8_t *buffer;
         LOG_D(RRC, "[UE %d] Received %s: UEid %d\n", ue_mod_id, ITTI_MSG_NAME (msg_p), NAS_UPLINK_DATA_REQ (msg_p).UEid);
         /* Create message for PDCP (ULInformationTransfer_t) */
-        length = do_ULInformationTransfer(&buffer, NAS_UPLINK_DATA_REQ (msg_p).nasMsg.length, NAS_UPLINK_DATA_REQ (msg_p).nasMsg.data);
+        length = do_ULInformationTransfer(&buffer, NAS_UPLINK_DATA_REQ (msg_p).nasMsg.length, NAS_UPLINK_DATA_REQ (msg_p).nasMsg.data);//QUES:
         break;
       }
 
diff --git a/openair3/NAS/NR_UE/nas_nrue_task.c b/openair3/NAS/NR_UE/nas_nrue_task.c
index e62dee41dd..2be8981634 100644
--- a/openair3/NAS/NR_UE/nas_nrue_task.c
+++ b/openair3/NAS/NR_UE/nas_nrue_task.c
@@ -154,9 +154,8 @@ void *nas_ue_task(void *args_p)
 
 void nr_nas_proc_dl_transfer_ind (UENAS_msg *msg,  Byte_t *data, uint32_t len) { //QUES: 解出的msg干什么
   uint8_t buffer[100];
-  UENAS_msg *msg1;
+  UENAS_msg msg1;
   uint32_t len1=0;
-  nr_user_nas_t UErrc= {0};//QUES:user
   int size;
   decodeNasMsg(msg,data,len);
   switch (msg->header.message_type) {
@@ -172,36 +171,45 @@ void nr_nas_proc_dl_transfer_ind (UENAS_msg *msg,  Byte_t *data, uint32_t len) {
     //   break;
     //   }
       
-    case AUTHENTICATION_REQUEST: { //send authenticationResponse
-      len1 += authenticationResponse((void **)&msg1->identity_response, &UErrc);
-      size = encodeNasMsg(msg1, buffer, len1);
+    case Authenticationrequest: { //send authenticationResponse
+      len1 += authenticationResponse5g(&msg1.authentication_response);
+      size = encodeNasMsg(&msg1, buffer, len1);
       nas_itti_ul_data_req(0,buffer,size,0);
       break;
       }
 
-    case SECURITY_MODE_COMMAND: { 
-      len1 += securityModeComplete5g((void **)&msg1->securitymode_complete);
-      size = encodeNasMsg(msg1, buffer, len1);
+    case Securitymodecommand: { 
+      len1 += securityModeComplete5g(&msg1.securitymode_complete);
+      size = encodeNasMsg(&msg1, buffer, len1);
       nas_itti_ul_data_req(0,buffer,size,0);
       break;
       }
     
-    case REGISTRATION_ACCEPT: { 
-      len1 += registrationComplete5g((void **)&msg1->registration_complete);
-      size = encodeNasMsg(msg1, buffer, len1);
+    case Registrationaccept: { 
+      len1 += registrationComplete5g(&msg1.registration_complete);
+      size = encodeNasMsg(&msg1, buffer, len1);
       nas_itti_ul_data_req(0,buffer,size,0);
       break;
       }
     
   }
-  printf("aaaaaaaaaaaaa: ");
-  printf("%d\n",size);
+
+  //****************************** //CUC:test
+  printf("decodeaaadecode:");
+  for (int i = 0; i < len; i++)
+  {
+    printf("%02x ",*(data+i));
+
+  }
+  printf("decodeaaadecode \n ");
+  printf("encodeaaaencode:");
   for (int i = 0; i < size; i++)
   {
     printf("%02x ",*(buffer+i));
 
   }
-  printf("aaaaaaaaaaaaa \n ");
+  printf("encodeaaaencode \n ");
+  //******************************
 
 }
 
@@ -223,15 +231,29 @@ int string2ByteArray(char* input,uint8_t* output)
     return i;
 }
 
-void tesths(void)
+void tesths(void) //CUC:test
 {
-  
-  UENAS_msg msg;
-  char name[] = "7e005601020000217d003b4a2e3bb80403de19020f57b16a2010583f0d352eb89001539b2cb2cbf1da5c";
-  uint32_t len=42;
-  Byte_t *data= (uint8_t *)malloc(sizeof(uint8_t)*len);
-  string2ByteArray(name, data);
-  nr_nas_proc_dl_transfer_ind(&msg,data,len);
+  printf("Authentication: \n ");
+  UENAS_msg msg1;
+  char Authenticationrequest[] = "7e005601020000217d003b4a2e3bb80403de19020f57b16a2010583f0d352eb89001539b2cb2cbf1da5c";
+  uint32_t len1=84;
+  Byte_t *data1= (uint8_t *)malloc(sizeof(uint8_t)*len1);
+  string2ByteArray(Authenticationrequest, data1);
+  nr_nas_proc_dl_transfer_ind(&msg1,data1,len1);
+  printf("Security mode: \n ");
+  UENAS_msg msg2;
+  char Securitymodecommand[] = "7e005d0201028020e1360102";
+  uint32_t len2=24;
+  Byte_t *data2= (uint8_t *)malloc(sizeof(uint8_t)*len2);
+  string2ByteArray(Securitymodecommand, data2);
+  nr_nas_proc_dl_transfer_ind(&msg2,data2,len2);
+  printf("Registration: \n ");
+  UENAS_msg msg3;
+  char Registrationrequest[] = "7e0042010177000bf202f8398000410000000154070002f83900000115020101210200005e01be";
+  uint32_t len3=94;
+  Byte_t *data3= (uint8_t *)malloc(sizeof(uint8_t)*len3);
+  string2ByteArray(Registrationrequest, data3);
+  nr_nas_proc_dl_transfer_ind(&msg3,data3,len3);
 
   
 }
@@ -254,6 +276,21 @@ int decodeNasMsg(UENAS_msg *msg, uint8_t *buffer, uint32_t len) {
 
   switch(msg->header.message_type) { 
 
+    case Authenticationrequest: 
+      break;
+    
+    case Securitymodecommand:
+      break;
+
+    case Registrationaccept:
+      break;
+
+    default:
+      LOG_TRACE(ERROR, "NR_UE   - Unexpected message type: 0x%x",
+    		  msg->header.message_type);
+      decode_result = TLV_ENCODE_WRONG_MESSAGE_TYPE;
+      break;
+
   }
 
   LOG_FUNC_RETURN (header_result + decode_result);
@@ -280,17 +317,17 @@ int encodeNasMsg(UENAS_msg *msg, uint8_t *buffer, uint32_t len) { //QUES:UENAS_m
     //   encode_result = encode_identity_response(&msg->identity_response, buffer, len);
     //   break;
     // }
-    case AUTHENTICATION_RESPONSE: {
+    case Authenticationresponse: {
       encode_result = encode_authentication_response5g(&msg->authentication_response, buffer, len);
       break;
     }
 
-    case SECURITY_MODE_COMPLETE: {
+    case Securitymodecomplete: {
       encode_result = encode_security_mode_complete5g(&msg->securitymode_complete, buffer, len);//TODO:encode_security_mode_complete5g
       break;
     }
 
-    case REGISTRATION_COMPLETE: {
+    case Registrationcomplete: {
       encode_result = encode_registration_complete5g(&msg->registration_complete, buffer, len);//TODO:encode_security_mode_complete5g
       break;
     }
@@ -302,7 +339,9 @@ static int _nas_mm_msg_decode_header(mm_msg_header_t *header, const uint8_t *buf
   int size = 0;
 
   /* Check the buffer length */
-
+  if (len < sizeof(mm_msg_header_t)) {
+    return (TLV_ENCODE_BUFFER_TOO_SHORT);
+  }
 
   /* Encode the extendedprotocol discriminator */
   DECODE_U8(buffer + size, header->ex_protocol_discriminator, size);
@@ -312,7 +351,11 @@ static int _nas_mm_msg_decode_header(mm_msg_header_t *header, const uint8_t *buf
   DECODE_U8(buffer + size, header->message_type, size);
 
   /* Check the protocol discriminator */
-
+  if (header->ex_protocol_discriminator != FGS_MOBILITY_MANAGEMENT_MESSAGE) {
+    LOG_TRACE(ERROR, "ESM-MSG   - Unexpected extened protocol discriminator: 0x%x",
+              header->ex_protocol_discriminator);
+    return (TLV_ENCODE_PROTOCOL_NOT_SUPPORTED);
+  }
 
   return (size);
 }
@@ -376,20 +419,28 @@ int encode_registration_complete5g(registrationcomplete_t *registrationcomplete,
   return encoded;
 }
 
-int securityModeComplete5g(void **msg) {
-  myCalloc(resp, securityModeComplete_t);
-  resp->epd=SGSmobilitymanagementmessages;
-  resp->sh=0;
-  resp->mt=Registrationcomplete;
-  *msg=resp;
+
+int authenticationResponse5g(authenticationresponse_t *msg) {
+  msg->epd=SGSmobilitymanagementmessages;
+  msg->sh=0;
+  msg->mt=Authenticationresponse;
+  msg->iei=IEI_AuthenticationResponse;
+  msg->RESlen=sizeof(msg->RES); 
+  uint8_t AUTN[16]={0};
+  memcpy(&msg->RES,AUTN,msg->RESlen);
+  return sizeof(authenticationresponse_t);
+}
+
+int securityModeComplete5g(securityModeComplete_t *msg) {
+  msg->epd=SGSmobilitymanagementmessages;
+  msg->sh=0;
+  msg->mt=Securitymodecomplete;
   return sizeof(securityModeComplete_t);
 }
 
-int registrationComplete5g(void **msg) {
-  myCalloc(resp, registrationcomplete_t);
-  resp->epd=SGSmobilitymanagementmessages;
-  resp->sh=0;
-  resp->mt=Securitymodecomplete;
-  *msg=resp;
+int registrationComplete5g(registrationcomplete_t *msg) {
+  msg->epd=SGSmobilitymanagementmessages;
+  msg->sh=0;
+  msg->mt=Registrationcomplete;
   return sizeof(registrationcomplete_t);
 }
diff --git a/openair3/NAS/NR_UE/nas_nrue_task.h b/openair3/NAS/NR_UE/nas_nrue_task.h
index 7baedecd28..cad2330d13 100644
--- a/openair3/NAS/NR_UE/nas_nrue_task.h
+++ b/openair3/NAS/NR_UE/nas_nrue_task.h
@@ -80,10 +80,11 @@ int encodeNasMsg(UENAS_msg *msg, uint8_t *buffer, uint32_t len);
 int encode_authentication_response5g(authenticationresponse_t *authentication_response, uint8_t *buffer, uint32_t len);
 int encode_security_mode_complete5g(securityModeComplete_t *securitymodecomplete, uint8_t *buffer, uint32_t len);
 int encode_registration_complete5g(registrationcomplete_t *registrationcomplete, uint8_t *buffer, uint32_t len);
-int securityModeComplete5g(void **msg);
-int registrationComplete5g(void **msg);
+int authenticationResponse5g(authenticationresponse_t *msg);
+int securityModeComplete5g(securityModeComplete_t *msg);
+int registrationComplete5g(registrationcomplete_t *msg);
 
-int string2ByteArray(char* input,uint8_t* output);
+int string2ByteArray(char* input,uint8_t* output); //CUC:test
 void tesths(void);
 
 #endif /* NAS_TASK_H_ */
-- 
2.26.2