1 /*****************************************************************************
3 # Copyright 2020 AT&T Intellectual Property *
4 # Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved. *
5 # Copyright (c) 2020 HCL Technologies Limited. *
7 # Licensed under the Apache License, Version 2.0 (the "License"); *
8 # you may not use this file except in compliance with the License. *
9 # You may obtain a copy of the License at *
11 # http://www.apache.org/licenses/LICENSE-2.0 *
13 # Unless required by applicable law or agreed to in writing, software *
14 # distributed under the License is distributed on an "AS IS" BASIS, *
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
16 # See the License for the specific language governing permissions and *
17 # limitations under the License. *
19 ******************************************************************************/
30 #include "e2sim_defs.h"
31 #include "e2sim_sctp.hpp"
32 #include "e2ap_message_handler.hpp"
33 #include "encode_e2apv1.hpp"
39 std::unordered_map<long, OCTET_STRING_t*> E2Sim::getRegistered_ran_functions() {
40 return ran_functions_registered;
43 void E2Sim::register_subscription_callback(long func_id, SubscriptionCallback cb) {
44 fprintf(stderr,"%%%%about to register callback for subscription for func_id %d\n", func_id);
45 subscription_callbacks[func_id] = cb;
49 SubscriptionCallback E2Sim::get_subscription_callback(long func_id) {
50 fprintf(stderr, "%%%%we are getting the subscription callback for func id %d\n", func_id);
51 SubscriptionCallback cb;
54 cb = subscription_callbacks.at(func_id);
55 } catch(const std::out_of_range& e) {
56 throw std::out_of_range("Function ID is not registered");
62 void E2Sim::register_e2sm(long func_id, OCTET_STRING_t *ostr) {
65 //If we already have an entry for func_id
67 printf("%%%%about to register e2sm func desc for %d\n", func_id);
69 ran_functions_registered[func_id] = ostr;
74 void E2Sim::encode_and_send_sctp_data(E2AP_PDU_t* pdu)
79 int procedureCodeValue = e2ap_asn1c_get_procedureCode(pdu);
81 data.len = e2ap_asn1c_encode_pdu(pdu, &buf);
82 memcpy(data.buffer, buf, min(data.len, MAX_SCTP_BUFFER));
84 if(procedureCodeValue == ProcedureCode_id_RICindication){
86 fprintf(stderr, "RIC Indication Procedurecode : %d \n",procedureCodeValue);
87 struct timeval ts_recv;
88 struct timeval ts_sent;
91 sctp_send_data(client_fd, data);
92 gettimeofday(&ts_sent, NULL);
94 sctp_buffer_t recv_buf;
96 if(sctp_receive_data(client_fd, recv_buf) > 0){
98 gettimeofday(&ts_recv, NULL);
100 E2AP_PDU_t* pdu = (E2AP_PDU_t*)calloc(1, sizeof(E2AP_PDU));
101 ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
102 asn_transfer_syntax syntax;
103 syntax = ATS_ALIGNED_BASIC_PER;
104 auto rval = asn_decode(nullptr, syntax, &asn_DEF_E2AP_PDU, (void **) &pdu,recv_buf.buffer, recv_buf.len);
106 int control_procedureCode = e2ap_asn1c_get_procedureCode(pdu);
107 fprintf(stderr, "Received Msg procedurecode is: %d\nfull buffer is : %s\n",control_procedureCode, recv_buf.buffer);
109 //Received control procedure code is 111 because bouncer xapp not doing any pdu contraction/encode for control msg and its simply sending the msg for round trip calculation
110 if(control_procedureCode == 111 ){
112 io_file.open("e2sim_timestamp.txt", std::ios::in|std::ios::out|std::ios::app);
113 io_file << "Sent RIC Indication at time: " << (ts_sent.tv_sec * 1000000) + (ts_sent.tv_usec) << std::endl;
114 fprintf(stderr, "Sent RIC Indication at time: %ld\n" ,((ts_sent.tv_sec * 1000000) + (ts_sent.tv_usec)));
116 io_file << "Received RIC Control Msg at time: " << (ts_recv.tv_sec * 1000000) + (ts_recv.tv_usec) << std::endl;
117 fprintf(stderr,"Received RIC Control Msg at time: %ld\n" ,((ts_recv.tv_sec * 1000000) + (ts_recv.tv_usec)));
118 io_file << "Time diff in Microseconds:" << ((ts_recv.tv_sec - ts_sent.tv_sec)*1000000 + (ts_recv.tv_usec - ts_sent.tv_usec)) << std::endl;
120 fprintf(stderr, "Time diff in Microseconds: %ld\n",(((ts_recv.tv_sec - ts_sent.tv_sec)*1000000 + (ts_recv.tv_usec - ts_sent.tv_usec))));
129 sctp_send_data(client_fd, data);
135 void E2Sim::wait_for_sctp_data()
137 sctp_buffer_t recv_buf;
138 if(sctp_receive_data(client_fd, recv_buf) > 0)
140 LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
141 e2ap_handle_sctp_data(client_fd, recv_buf, false, this);
147 void E2Sim::generate_e2apv1_subscription_response_success(E2AP_PDU *e2ap_pdu, long reqActionIdsAccepted[], long reqActionIdsRejected[], int accept_size, int reject_size, long reqRequestorId, long reqInstanceId) {
148 encoding::generate_e2apv1_subscription_response_success(e2ap_pdu, reqActionIdsAccepted, reqActionIdsRejected, accept_size, reject_size, reqRequestorId, reqInstanceId);
151 void E2Sim::generate_e2apv1_indication_request_parameterized(E2AP_PDU *e2ap_pdu, long requestorId, long instanceId, long ranFunctionId, long actionId, long seqNum, uint8_t *ind_header_buf, int header_length, uint8_t *ind_message_buf, int message_length) {
152 encoding::generate_e2apv1_indication_request_parameterized(e2ap_pdu, requestorId, instanceId, ranFunctionId, actionId, seqNum, ind_header_buf, header_length, ind_message_buf, message_length);
156 int E2Sim::run_loop(int argc, char* argv[]){
158 printf("Start E2 Agent (E2 Simulator\n");
163 simfile.open("simulation.txt", ios::in);
165 if (simfile.is_open()) {
167 while (getline(simfile, line)) {
168 cout << line << "\n";
177 options_t ops = read_input_options(argc, argv);
179 printf("After reading input options\n");
181 //E2 Agent will automatically restart upon sctp disconnection
182 // int server_fd = sctp_start_server(ops.server_ip, ops.server_port);
184 client_fd = sctp_start_client(ops.server_ip, ops.server_port);
185 E2AP_PDU_t* pdu_setup = (E2AP_PDU_t*)calloc(1,sizeof(E2AP_PDU));
187 printf("After starting client\n");
188 printf("client_fd value is %d\n", client_fd);
190 std::vector<encoding::ran_func_info> all_funcs;
192 //Loop through RAN function definitions that are registered
194 for (std::pair<long, OCTET_STRING_t*> elem : ran_functions_registered) {
195 printf("looping through ran func\n");
196 encoding::ran_func_info next_func;
198 next_func.ranFunctionId = elem.first;
199 next_func.ranFunctionDesc = elem.second;
200 next_func.ranFunctionRev = (long)2;
201 all_funcs.push_back(next_func);
204 printf("about to call setup request encode\n");
206 generate_e2apv1_setup_request_parameterized(pdu_setup, all_funcs);
208 printf("After generating e2setup req\n");
210 xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu_setup);
212 printf("After XER Encoding\n");
214 auto buffer_size = MAX_SCTP_BUFFER;
215 unsigned char buffer[MAX_SCTP_BUFFER];
219 char *error_buf = (char*)calloc(300, sizeof(char));
222 asn_check_constraints(&asn_DEF_E2AP_PDU, pdu_setup, error_buf, &errlen);
223 printf("error length %d\n", errlen);
224 printf("error buf %s\n", error_buf);
226 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu_setup, buffer, buffer_size);
228 data.len = er.encoded;
230 fprintf(stderr, "er encded is %d\n", er.encoded);
232 memcpy(data.buffer, buffer, er.encoded);
234 if(sctp_send_data(client_fd, data) > 0) {
235 LOG_I("[SCTP] Sent E2-SETUP-REQUEST");
237 LOG_E("[SCTP] Unable to send E2-SETUP-REQUEST to peer");
240 sctp_buffer_t recv_buf;
242 LOG_I("[SCTP] Waiting for SCTP data");
244 while(1) //constantly looking for data on SCTP interface
246 if(sctp_receive_data(client_fd, recv_buf) <= 0)
249 LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
251 e2ap_handle_sctp_data(client_fd, recv_buf, xmlenc, this);
252 if (xmlenc) xmlenc = false;