3 /*****************************************************************************
\r
5 # Copyright 2020 AT&T Intellectual Property *
\r
6 # Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved. *
\r
8 # Licensed under the Apache License, Version 2.0 (the "License"); *
\r
9 # you may not use this file except in compliance with the License. *
\r
10 # You may obtain a copy of the License at *
\r
12 # http://www.apache.org/licenses/LICENSE-2.0 *
\r
14 # Unless required by applicable law or agreed to in writing, software *
\r
15 # distributed under the License is distributed on an "AS IS" BASIS, *
\r
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
\r
17 # See the License for the specific language governing permissions and *
\r
18 # limitations under the License. *
\r
20 ******************************************************************************/
\r
21 #include "e2ap_message_handler.hpp"
\r
23 //#include <iostream>
\r
26 #include "encode_e2apv1.hpp"
\r
31 void e2ap_handle_sctp_data(int &socket_fd, sctp_buffer_t &data, bool xmlenc, E2Sim *e2sim)
\r
33 fprintf(stderr, "in e2ap_handle_sctp_data()\n");
\r
34 //decode the data into E2AP-PDU
\r
35 E2AP_PDU_t* pdu = (E2AP_PDU_t*)calloc(1, sizeof(E2AP_PDU));
\r
36 ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
\r
38 fprintf(stderr, "decoding...\n");
\r
40 asn_transfer_syntax syntax;
\r
43 syntax = ATS_ALIGNED_BASIC_PER;
\r
46 fprintf(stderr, "full buffer\n%s\n", data.buffer);
\r
47 // e2ap_asn1c_decode_pdu(pdu, data.buffer, data.len);
\r
49 auto rval = asn_decode(nullptr, syntax, &asn_DEF_E2AP_PDU, (void **) &pdu,
\r
50 data.buffer, data.len);
\r
53 int index = (int)pdu->present;
\r
54 fprintf(stderr, "length of data %d\n", rval.consumed);
\r
55 fprintf(stderr, "result %d\n", rval.code);
\r
56 fprintf(stderr, "index is %d\n", index);
\r
58 fprintf(stderr, "showing xer of data\n");
\r
60 xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu);
\r
62 int procedureCode = e2ap_asn1c_get_procedureCode(pdu);
\r
63 index = (int)pdu->present;
\r
65 LOG_D("[E2AP] Unpacked E2AP-PDU: index = %d, procedureCode = %d\n",
\r
66 index, procedureCode);
\r
68 switch(procedureCode)
\r
71 case ProcedureCode_id_E2setup:
\r
74 case E2AP_PDU_PR_initiatingMessage:
\r
75 e2ap_handle_E2SetupRequest(pdu, socket_fd);
\r
76 LOG_I("[E2AP] Received SETUP-REQUEST");
\r
79 case E2AP_PDU_PR_successfulOutcome:
\r
80 LOG_I("[E2AP] Received SETUP-RESPONSE-SUCCESS");
\r
83 case E2AP_PDU_PR_unsuccessfulOutcome:
\r
84 LOG_I("[E2AP] Received SETUP-RESPONSE-FAILURE");
\r
88 LOG_E("[E2AP] Invalid message index=%d in E2AP-PDU", index);
\r
93 case ProcedureCode_id_Reset: //reset = 7
\r
96 case E2AP_PDU_PR_initiatingMessage:
\r
97 LOG_I("[E2AP] Received RESET-REQUEST");
\r
100 case E2AP_PDU_PR_successfulOutcome:
\r
103 case E2AP_PDU_PR_unsuccessfulOutcome:
\r
107 LOG_E("[E2AP] Invalid message index=%d in E2AP-PDU", index);
\r
112 case ProcedureCode_id_RICsubscription: //RIC SUBSCRIPTION = 201
\r
115 case E2AP_PDU_PR_initiatingMessage: { //initiatingMessage
\r
116 LOG_I("[E2AP] Received RIC-SUBSCRIPTION-REQUEST");
\r
117 // e2ap_handle_RICSubscriptionRequest(pdu, socket_fd);
\r
118 long func_id = encoding::get_function_id_from_subscription(pdu);
\r
119 fprintf(stderr, "Function Id of message is %d\n", func_id);
\r
120 SubscriptionCallback cb;
\r
122 bool func_exists = true;
\r
125 cb = e2sim->get_subscription_callback(func_id);
\r
126 } catch(const std::out_of_range& e) {
\r
127 func_exists = false;
\r
131 fprintf(stderr, "Calling callback function\n");
\r
134 fprintf(stderr, "Error: No RAN Function with this ID exists\n");
\r
136 // callback_kpm_subscription_request(pdu, socket_fd);
\r
141 case E2AP_PDU_PR_successfulOutcome:
\r
142 LOG_I("[E2AP] Received RIC-SUBSCRIPTION-RESPONSE");
\r
145 case E2AP_PDU_PR_unsuccessfulOutcome:
\r
146 LOG_I("[E2AP] Received RIC-SUBSCRIPTION-FAILURE");
\r
150 LOG_E("[E2AP] Invalid message index=%d in E2AP-PDU", index);
\r
155 case ProcedureCode_id_RICindication: // 205
\r
158 case E2AP_PDU_PR_initiatingMessage: //initiatingMessage
\r
159 LOG_I("[E2AP] Received RIC-INDICATION-REQUEST");
\r
160 // e2ap_handle_RICSubscriptionRequest(pdu, socket_fd);
\r
162 case E2AP_PDU_PR_successfulOutcome:
\r
163 LOG_I("[E2AP] Received RIC-INDICATION-RESPONSE");
\r
166 case E2AP_PDU_PR_unsuccessfulOutcome:
\r
167 LOG_I("[E2AP] Received RIC-INDICATION-FAILURE");
\r
171 LOG_E("[E2AP] Invalid message index=%d in E2AP-PDU %d", index,
\r
172 (int)ProcedureCode_id_RICindication);
\r
177 case ProcedureCode_id_RICserviceQuery:
\r
180 case E2AP_PDU_PR_initiatingMessage:
\r
181 LOG_I("[E2AP] Received RIC-Service-Query")
\r
182 e2ap_handle_E2SeviceRequest(pdu, socket_fd, e2sim);
\r
186 LOG_E("[E2AP] Invalid message index=%d in E2AP-PDU %d", index,
\r
187 (int)ProcedureCode_id_RICserviceQuery);
\r
192 case ProcedureCode_id_RICserviceUpdate:
\r
195 case E2AP_PDU_PR_successfulOutcome:
\r
196 LOG_I("[E2AP] Received RIC-SERVICE-UPDATE-SUCCESS")
\r
199 case E2AP_PDU_PR_unsuccessfulOutcome:
\r
200 LOG_I("[E2AP] Received RIC-SERVICE-UPDATE-FAILURE")
\r
204 LOG_E("[E2AP] Invalid message index=%d in E2AP-PDU %d", index,
\r
205 (int)ProcedureCode_id_RICserviceUpdate);
\r
212 LOG_E("[E2AP] No available handler for procedureCode=%d", procedureCode);
\r
218 void e2ap_handle_E2SeviceRequest(E2AP_PDU_t* pdu, int &socket_fd, E2Sim *e2sim) {
\r
220 auto buffer_size = MAX_SCTP_BUFFER;
\r
221 unsigned char buffer[MAX_SCTP_BUFFER];
\r
222 E2AP_PDU_t* res_pdu = (E2AP_PDU_t*)calloc(1,sizeof(E2AP_PDU));
\r
224 // prepare ran function defination
\r
225 std::vector<encoding::ran_func_info> all_funcs;
\r
227 //Loop through RAN function definitions that are registered
\r
229 for (std::pair<long, OCTET_STRING_t*> elem : e2sim->getRegistered_ran_functions()) {
\r
230 printf("looping through ran func\n");
\r
231 encoding::ran_func_info next_func;
\r
233 next_func.ranFunctionId = elem.first;
\r
234 next_func.ranFunctionDesc = elem.second;
\r
235 next_func.ranFunctionRev = (long)3;
\r
236 all_funcs.push_back(next_func);
\r
239 printf("about to call service update encode\n");
\r
241 encoding::generate_e2apv1_service_update(res_pdu, all_funcs);
\r
243 LOG_D("[E2AP] Created E2-SERVICE-UPDATE");
\r
245 e2ap_asn1c_print_pdu(res_pdu);
\r
247 sctp_buffer_t data;
\r
249 char *error_buf = (char*)calloc(300, sizeof(char));
\r
252 asn_check_constraints(&asn_DEF_E2AP_PDU, res_pdu, error_buf, &errlen);
\r
253 printf("error length %d\n", errlen);
\r
254 printf("error buf %s\n", error_buf);
\r
256 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, res_pdu, buffer, buffer_size);
\r
258 data.len = er.encoded;
\r
259 fprintf(stderr, "er encoded is %d\n", er.encoded);
\r
261 memcpy(data.buffer, buffer, er.encoded);
\r
263 //send response data over sctp
\r
264 if(sctp_send_data(socket_fd, data) > 0) {
\r
265 LOG_I("[SCTP] Sent E2-SERVICE-UPDATE");
\r
267 LOG_E("[SCTP] Unable to send E2-SERVICE-UPDATE to peer");
\r
271 void e2ap_handle_E2SetupRequest(E2AP_PDU_t* pdu, int &socket_fd) {
\r
274 E2AP_PDU_t* res_pdu = (E2AP_PDU_t*)calloc(1, sizeof(E2AP_PDU));
\r
275 encoding::generate_e2apv1_setup_response(res_pdu);
\r
278 LOG_D("[E2AP] Created E2-SETUP-RESPONSE");
\r
280 e2ap_asn1c_print_pdu(res_pdu);
\r
283 auto buffer_size = MAX_SCTP_BUFFER;
\r
284 unsigned char buffer[MAX_SCTP_BUFFER];
\r
286 sctp_buffer_t data;
\r
287 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, res_pdu, buffer, buffer_size);
\r
289 data.len = er.encoded;
\r
290 fprintf(stderr, "er encoded is %d\n", er.encoded);
\r
292 //data.len = e2ap_asn1c_encode_pdu(res_pdu, &buf);
\r
293 memcpy(data.buffer, buffer, er.encoded);
\r
295 //send response data over sctp
\r
296 if(sctp_send_data(socket_fd, data) > 0) {
\r
297 LOG_I("[SCTP] Sent E2-SETUP-RESPONSE");
\r
299 LOG_E("[SCTP] Unable to send E2-SETUP-RESPONSE to peer");
\r
304 //Sending Subscription Request
\r
306 E2AP_PDU_t* pdu_sub = (E2AP_PDU_t*)calloc(1,sizeof(E2AP_PDU));
\r
308 encoding::generate_e2apv1_subscription_request(pdu_sub);
\r
310 xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu_sub);
\r
312 auto buffer_size2 = MAX_SCTP_BUFFER;
\r
313 unsigned char buffer2[MAX_SCTP_BUFFER];
\r
315 sctp_buffer_t data2;
\r
317 auto er2 = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu_sub, buffer2, buffer_size2);
\r
319 data2.len = er2.encoded;
\r
320 memcpy(data2.buffer, buffer2, er2.encoded);
\r
322 fprintf(stderr, "er encded is %d\n", er2.encoded);
\r
324 if(sctp_send_data(socket_fd, data2) > 0) {
\r
325 LOG_I("[SCTP] Sent E2-SUBSCRIPTION-REQUEST");
\r
327 LOG_E("[SCTP] Unable to send E2-SUBSCRIPTION-REQUEST to peer");
\r
334 void e2ap_handle_RICSubscriptionRequest(E2AP_PDU_t* pdu, int &socket_fd)
\r
337 //Send back Subscription Success Response
\r
339 E2AP_PDU_t* pdu_resp = (E2AP_PDU_t*)calloc(1,sizeof(E2AP_PDU));
\r
341 generate_e2apv1_subscription_response(pdu_resp, pdu);
\r
343 fprintf(stderr, "Subscription Response\n");
\r
345 xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu_resp);
\r
347 auto buffer_size2 = MAX_SCTP_BUFFER;
\r
348 unsigned char buffer2[MAX_SCTP_BUFFER];
\r
350 sctp_buffer_t data2;
\r
352 auto er2 = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu_resp, buffer2, buffer_size2);
\r
353 data2.len = er2.encoded;
\r
355 fprintf(stderr, "er encded is %d\n", er2.encoded);
\r
357 memcpy(data2.buffer, buffer2, er2.encoded);
\r
359 if(sctp_send_data(socket_fd, data2) > 0) {
\r
360 LOG_I("[SCTP] Sent RIC-SUBSCRIPTION-RESPONSE");
\r
362 LOG_E("[SCTP] Unable to send RIC-SUBSCRIPTION-RESPONSE to peer");
\r
366 //Send back an Indication
\r
368 E2AP_PDU_t* pdu_ind = (E2AP_PDU_t*)calloc(1,sizeof(E2AP_PDU));
\r
370 generate_e2apv1_indication_request(pdu_ind);
\r
372 xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu_ind);
\r
374 auto buffer_size = MAX_SCTP_BUFFER;
\r
375 unsigned char buffer[MAX_SCTP_BUFFER];
\r
377 sctp_buffer_t data;
\r
379 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu_ind, buffer, buffer_size);
\r
380 data.len = er.encoded;
\r
382 fprintf(stderr, "er encded is %d\n", er.encoded);
\r
384 memcpy(data.buffer, buffer, er.encoded);
\r
386 if(sctp_send_data(socket_fd, data) > 0) {
\r
387 LOG_I("[SCTP] Sent RIC-INDICATION-REQUEST");
\r
389 LOG_E("[SCTP] Unable to send RIC-INDICATION-REQUEST to peer");
\r