Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / src / X2AP / x2ap_message_handler.c
1 /*
2  *
3  * Copyright 2019 AT&T Intellectual Property
4  * Copyright 2019 Nokia
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <assert.h>
23
24 #include "x2ap_message_handler.h"
25
26 typedef int (*x2ap_message_decoded_handler)(X2AP_PDU_t *pdu, sctp_data_t *response);
27
28 static int handle_x2_setup_request(X2AP_PDU_t *pdu, sctp_data_t *response);
29
30 static int handle_x2_setup_response(X2AP_PDU_t *pdu, sctp_data_t *response);
31
32 static int handle_x2_setup_failure(X2AP_PDU_t *pdu, sctp_data_t *response);
33
34 /* Handlers matrix. Only eNB related procedure present here
35 /* rowID = procedureCode (start with 0)
36 */
37 x2ap_message_decoded_handler x2ap_messages_handler[][3] = {
38   { 0, 0, 0 }, /* handoverPreparation */
39   { 0, 0, 0 }, /* handoverCancel */
40   { 0, 0, 0 }, /* loadIndication */
41   { 0, 0, 0 }, /* errorIndication */
42   { 0, 0, 0 }, /* snStatusTransfer */
43   { 0, 0, 0 }, /* uEContextRelease */
44   { handle_x2_setup_request, handle_x2_setup_response, handle_x2_setup_failure }, /* x2Setup */
45   { 0, 0, 0 }, /* reset */
46   { 0, 0, 0 }, /* eNBConfigurationUpdate */
47   { 0, 0, 0 }, /* resourceStatusReportingInitiation */
48   { 0, 0, 0 }, /* resourceStatusReporting */
49   { 0, 0, 0 }, /* privateMessage */
50   { 0, 0, 0 }, /* mobilitySettingsChange */
51   { 0, 0, 0 }, /* rLFIndication */
52   { 0, 0, 0 }, /* handoverReport */
53   { 0, 0, 0 }, /* cellActivation */
54   { 0, 0, 0 }, /* x2Release */
55   { 0, 0, 0 }, /* x2APMessageTransfer */
56   { 0, 0, 0 }, /* x2Removal */
57   { 0, 0, 0 }, /* seNBAdditionPreparation */
58   { 0, 0, 0 }, /* seNBReconfigurationCompletion */
59   { 0, 0, 0 }, /* meNBinitiatedSeNBModificationPreparation */
60   { 0, 0, 0 }, /* seNBinitiatedSeNBModification */
61   { 0, 0, 0 }, /* meNBinitiatedSeNBRelease */
62   { 0, 0, 0 }, /* seNBinitiatedSeNBRelease */
63   { 0, 0, 0 }, /* seNBCounterCheck */
64   { 0, 0, 0 }  /* retrieveUEContext */
65 };
66
67 char *x2ap_direction2String(int x2ap_dir)
68 {
69   static char *x2ap_direction_String[] = {
70     "", /* Nothing */
71     "Originating message", /* originating message */
72     "Successfull outcome", /* successfull outcome */
73     "UnSuccessfull outcome", /* successfull outcome */
74   };
75   return(x2ap_direction_String[x2ap_dir]);
76 }
77
78 int x2ap_eNB_handle_message(const uint8_t *const data, const int data_len, sctp_data_t* response)
79 {
80   X2AP_PDU_t pdu;
81   int ret;
82
83   memset(&pdu, 0, sizeof(pdu));
84
85   //printf("Decode the PDU \n");
86
87   if (X2AP_ASN_decode(&pdu, data, data_len) < 0){
88     return -1;
89   }
90
91   /* Checking procedure Code and direction of message */
92   if (pdu.choice.initiatingMessage.procedureCode > sizeof(x2ap_messages_handler)
93         / (3 * sizeof(x2ap_message_decoded_handler))
94         || (pdu.present > X2AP_PDU_PR_unsuccessfulOutcome)) {
95
96     fprintf(stderr, "Either procedureCode %ld or direction %d exceed expected\n",
97          pdu.choice.initiatingMessage.procedureCode, pdu.present);
98     ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_X2AP_PDU, &pdu);
99     return -1;
100   }
101
102   if (x2ap_messages_handler[pdu.choice.initiatingMessage.procedureCode][pdu.present - 1] == NULL)
103   {
104     fprintf(stderr, "No handler for procedureCode %ld in direction %s\n",
105             pdu.choice.initiatingMessage.procedureCode,
106             x2ap_direction2String(pdu.present - 1));
107     ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_X2AP_PDU, &pdu);
108     return -1;
109   }
110
111   /*calling the appropriate handler */
112   ret = (*x2ap_messages_handler[pdu.choice.initiatingMessage.procedureCode][pdu.present - 1])(&pdu, response);
113   ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_X2AP_PDU, &pdu);
114   return ret;
115 }
116
117 int handle_x2_setup_request(X2AP_PDU_t *pdu, sctp_data_t* response)
118 {
119   fprintf(stderr, "Received X2 SETUP REQUEST\n");
120
121   /* Todo: when to generate X2 Setup Failure???
122   */
123
124   response->len = x2ap_generate_x2_setup_response(&response->data);
125
126   return 0;
127 }
128
129 int handle_x2_setup_response(X2AP_PDU_t *pdu, sctp_data_t* response)
130 {
131   fprintf(stderr, "Received X2 SETUP RESPONSE\n");
132   return 0;
133 }
134
135 int handle_x2_setup_failure(X2AP_PDU_t *pdu, sctp_data_t* response)
136 {
137   fprintf(stderr, "Received X2 SETUP FAILURE\n");
138   return 0;
139 }