From 569ab7dec3e280b520c73b0ef96563218f0e4a32 Mon Sep 17 00:00:00 2001
From: Lionel Gauthier <lionel.gauthier@eurecom.fr>
Date: Tue, 23 Dec 2014 14:01:01 +0000
Subject: [PATCH] S1AP UE context release

git-svn-id: http://svn.eurecom.fr/openair4G/trunk@6316 818b1a75-f10b-46b9-bf7c-635c3b92a50f
---
 .../s1ap_eNB_context_management_procedures.c  | 199 ++++++++++++++++++
 .../s1ap_eNB_context_management_procedures.h  |  40 ++++
 2 files changed, 239 insertions(+)
 create mode 100755 openair-cn/S1AP/s1ap_eNB_context_management_procedures.c
 create mode 100755 openair-cn/S1AP/s1ap_eNB_context_management_procedures.h

diff --git a/openair-cn/S1AP/s1ap_eNB_context_management_procedures.c b/openair-cn/S1AP/s1ap_eNB_context_management_procedures.c
new file mode 100755
index 00000000000..9362417aa20
--- /dev/null
+++ b/openair-cn/S1AP/s1ap_eNB_context_management_procedures.c
@@ -0,0 +1,199 @@
+/*******************************************************************************
+    OpenAirInterface
+    Copyright(c) 1999 - 2014 Eurecom
+
+    OpenAirInterface is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+
+    OpenAirInterface is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenAirInterface.The full GNU General Public License is
+   included in this distribution in the file called "COPYING". If not,
+   see <http://www.gnu.org/licenses/>.
+
+  Contact Information
+  OpenAirInterface Admin: openair_admin@eurecom.fr
+  OpenAirInterface Tech : openair_tech@eurecom.fr
+  OpenAirInterface Dev  : openair4g-devel@eurecom.fr
+
+  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.
+
+ *******************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+#include "assertions.h"
+#include "conversions.h"
+
+#include "intertask_interface.h"
+
+#include "s1ap_common.h"
+#include "s1ap_eNB_defs.h"
+
+#include "s1ap_eNB_itti_messaging.h"
+
+#include "s1ap_ies_defs.h"
+#include "s1ap_eNB_encoder.h"
+#include "s1ap_eNB_nnsf.h"
+#include "s1ap_eNB_ue_context.h"
+#include "s1ap_eNB_nas_procedures.h"
+#include "s1ap_eNB_management_procedures.h"
+#include "s1ap_eNB_context_management_procedures.h"
+
+
+int s1ap_ue_context_release_complete(instance_t instance,
+        s1ap_ue_release_complete_t *ue_release_complete_p)
+{
+    s1ap_eNB_instance_t          *s1ap_eNB_instance_p = NULL;
+    struct s1ap_eNB_ue_context_s *ue_context_p        = NULL;
+
+    S1ap_UEContextReleaseCompleteIEs_t *ue_ctxt_release_complete_ies_p = NULL;
+
+    s1ap_message  message;
+
+    uint8_t  *buffer;
+    uint32_t length;
+    int      ret = -1;
+
+    /* Retrieve the S1AP eNB instance associated with Mod_id */
+    s1ap_eNB_instance_p = s1ap_eNB_get_instance(instance);
+
+    DevAssert(ue_release_complete_p != NULL);
+    DevAssert(s1ap_eNB_instance_p != NULL);
+
+    if ((ue_context_p = s1ap_eNB_get_ue_context(s1ap_eNB_instance_p,
+            ue_release_complete_p->eNB_ue_s1ap_id)) == NULL)
+    {
+        /* The context for this eNB ue s1ap id doesn't exist in the map of eNB UEs */
+        S1AP_WARN("Failed to find ue context associated with eNB ue s1ap id: %u\n",
+                ue_release_complete_p->eNB_ue_s1ap_id);
+        return -1;
+    }
+
+    /* Prepare the S1AP message to encode */
+    memset(&message, 0, sizeof(s1ap_message));
+
+    message.direction     = S1AP_PDU_PR_successfulOutcome;
+    message.procedureCode = S1ap_ProcedureCode_id_UEContextRelease;
+    //message.criticality   = S1ap_Criticality_reject;
+
+    ue_ctxt_release_complete_ies_p = &message.msg.s1ap_UEContextReleaseCompleteIEs;
+
+    ue_ctxt_release_complete_ies_p->eNB_UE_S1AP_ID = ue_release_complete_p->eNB_ue_s1ap_id;
+    ue_ctxt_release_complete_ies_p->mme_ue_s1ap_id = ue_context_p->mme_ue_s1ap_id;
+    //ue_ctxt_release_complete_ies_p->criticalityDiagnostics
+    //ue_ctxt_release_complete_ies_p->presenceMask
+
+    if (s1ap_eNB_encode_pdu(&message, &buffer, &length) < 0) {
+        /* Encode procedure has failed... */
+        S1AP_ERROR("Failed to encode UE context release complete\n");
+        return -1;
+    }
+
+    /* UE associated signalling -> use the allocated stream */
+    s1ap_eNB_itti_send_sctp_data_req(s1ap_eNB_instance_p->instance,
+                                     ue_context_p->mme_ref->assoc_id, buffer,
+                                     length, ue_context_p->stream);
+
+
+    s1ap_eNB_itti_send_sctp_close_association(s1ap_eNB_instance_p->instance,
+                                     ue_context_p->mme_ref->assoc_id);
+
+
+
+    // release UE context
+    struct s1ap_eNB_ue_context_s *ue_context2_p = NULL;
+    if ((ue_context2_p = RB_REMOVE(s1ap_ue_map, &s1ap_eNB_instance_p->s1ap_ue_head, ue_context_p))
+            == NULL)
+    {
+        s1ap_eNB_free_ue_context(ue_context2_p);
+    }
+    return ret;
+}
+
+
+int s1ap_ue_context_release_req(instance_t instance,
+                                s1ap_ue_release_req_t *ue_release_req_p)
+{
+    s1ap_eNB_instance_t               *s1ap_eNB_instance_p           = NULL;
+    struct s1ap_eNB_ue_context_s      *ue_context_p                  = NULL;
+    S1ap_UEContextReleaseRequestIEs_t *ue_ctxt_release_request_ies_p = NULL;
+    s1ap_message                       message;
+    uint8_t                           *buffer                        = NULL;
+    uint32_t                           length;
+
+    /* Retrieve the S1AP eNB instance associated with Mod_id */
+    s1ap_eNB_instance_p = s1ap_eNB_get_instance(instance);
+
+    DevAssert(ue_release_req_p != NULL);
+    DevAssert(s1ap_eNB_instance_p != NULL);
+
+    if ((ue_context_p = s1ap_eNB_get_ue_context(s1ap_eNB_instance_p,
+                                                ue_release_req_p->eNB_ue_s1ap_id)) == NULL) {
+        /* The context for this eNB ue s1ap id doesn't exist in the map of eNB UEs */
+        S1AP_WARN("Failed to find ue context associated with eNB ue s1ap id: %u\n",
+                  ue_release_req_p->eNB_ue_s1ap_id);
+        return -1;
+    }
+
+    /* Prepare the S1AP message to encode */
+    memset(&message, 0, sizeof(s1ap_message));
+
+    message.direction     = S1AP_PDU_PR_initiatingMessage;
+    message.procedureCode = S1ap_ProcedureCode_id_UEContextReleaseRequest;
+    //message.criticality   = S1ap_Criticality_reject;
+
+    ue_ctxt_release_request_ies_p = &message.msg.s1ap_UEContextReleaseRequestIEs;
+
+    ue_ctxt_release_request_ies_p->eNB_UE_S1AP_ID = ue_release_req_p->eNB_ue_s1ap_id;
+    ue_ctxt_release_request_ies_p->mme_ue_s1ap_id = ue_context_p->mme_ue_s1ap_id;
+    switch (ue_release_req_p->cause) {
+        case S1AP_CAUSE_NOTHING:
+            ue_ctxt_release_request_ies_p->cause.present = S1ap_Cause_PR_NOTHING;
+            break;
+        case S1AP_CAUSE_RADIO_NETWORK:
+            ue_ctxt_release_request_ies_p->cause.present = S1ap_Cause_PR_radioNetwork;
+            ue_ctxt_release_request_ies_p->cause.choice.radioNetwork = ue_release_req_p->cause_value;
+            break;
+        case S1AP_CAUSE_TRANSPORT:
+            ue_ctxt_release_request_ies_p->cause.present = S1ap_Cause_PR_transport;
+            ue_ctxt_release_request_ies_p->cause.choice.transport = ue_release_req_p->cause_value;
+            break;
+        case S1AP_CAUSE_NAS:
+            ue_ctxt_release_request_ies_p->cause.present = S1ap_Cause_PR_nas;
+            ue_ctxt_release_request_ies_p->cause.choice.nas = ue_release_req_p->cause_value;
+            break;
+        case S1AP_CAUSE_PROTOCOL:
+            ue_ctxt_release_request_ies_p->cause.present = S1ap_Cause_PR_protocol;
+            ue_ctxt_release_request_ies_p->cause.choice.protocol = ue_release_req_p->cause_value;
+            break;
+        case S1AP_CAUSE_MISC:
+        default:
+            ue_ctxt_release_request_ies_p->cause.present = S1ap_Cause_PR_misc;
+            ue_ctxt_release_request_ies_p->cause.choice.misc = ue_release_req_p->cause_value;
+            break;
+    }
+
+    if (s1ap_eNB_encode_pdu(&message, &buffer, &length) < 0) {
+        /* Encode procedure has failed... */
+        S1AP_ERROR("Failed to encode UE context release complete\n");
+        return -1;
+    }
+
+    /* UE associated signalling -> use the allocated stream */
+    s1ap_eNB_itti_send_sctp_data_req(s1ap_eNB_instance_p->instance,
+                                     ue_context_p->mme_ref->assoc_id, buffer,
+                                     length, ue_context_p->stream);
+
+   return 0;
+}
+
diff --git a/openair-cn/S1AP/s1ap_eNB_context_management_procedures.h b/openair-cn/S1AP/s1ap_eNB_context_management_procedures.h
new file mode 100755
index 00000000000..5205082155d
--- /dev/null
+++ b/openair-cn/S1AP/s1ap_eNB_context_management_procedures.h
@@ -0,0 +1,40 @@
+/*******************************************************************************
+    OpenAirInterface
+    Copyright(c) 1999 - 2014 Eurecom
+
+    OpenAirInterface is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+
+    OpenAirInterface is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenAirInterface.The full GNU General Public License is
+   included in this distribution in the file called "COPYING". If not,
+   see <http://www.gnu.org/licenses/>.
+
+  Contact Information
+  OpenAirInterface Admin: openair_admin@eurecom.fr
+  OpenAirInterface Tech : openair_tech@eurecom.fr
+  OpenAirInterface Dev  : openair4g-devel@eurecom.fr
+
+  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.
+
+ *******************************************************************************/
+
+#ifndef S1AP_ENB_CONTEXT_MANAGEMENT_PROCEDURES_H_
+#define S1AP_ENB_CONTEXT_MANAGEMENT_PROCEDURES_H_
+
+
+int s1ap_ue_context_release_complete(instance_t instance,
+                             s1ap_ue_release_complete_t *ue_release_complete_p);
+
+int s1ap_ue_context_release_req(instance_t instance,
+                                s1ap_ue_release_req_t *ue_release_req_p);
+
+#endif /* S1AP_ENB_CONTEXT_MANAGEMENT_PROCEDURES_H_ */
-- 
GitLab