1 /*****************************************************************************
3 # Copyright 2020 AT&T Intellectual Property *
4 # Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved. *
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 *
10 # http://www.apache.org/licenses/LICENSE-2.0 *
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. *
18 ******************************************************************************/
28 #include "e2sim_defs.h"
29 #include "e2sim_sctp.hpp"
30 #include "e2ap_message_handler.hpp"
31 #include "encode_e2apv1.hpp"
32 #include "RANfunctionOID.h"
38 std::unordered_map<long, OCTET_STRING_t*> E2Sim::getRegistered_ran_functions() {
39 return ran_functions_registered;
42 void E2Sim::register_subscription_callback(long func_id, SubscriptionCallback cb) {
43 LOG_I("About to register callback for subscription for RAN function with ID %d", func_id);
44 subscription_callbacks[func_id] = cb;
48 SubscriptionCallback E2Sim::get_subscription_callback(long func_id) {
49 LOG_I("We are getting the subscription callback for func id %d", func_id);
50 SubscriptionCallback cb;
53 cb = subscription_callbacks.at(func_id);
54 } catch(const std::out_of_range& e) {
55 throw std::out_of_range("Function ID is not registered");
61 void E2Sim::register_e2sm(long func_id, OCTET_STRING_t *ostr) {
64 //If we already have an entry for func_id
65 LOG_I("About to register E2SM RAN function description with ID %d", func_id);
66 ran_functions_registered[func_id] = ostr;
69 void E2Sim::encode_and_send_sctp_data(E2AP_PDU_t* pdu)
74 data.len = e2ap_asn1c_encode_pdu(pdu, &buf);
75 memcpy(data.buffer, buf, min(data.len, MAX_SCTP_BUFFER));
77 sctp_send_data(client_fd, data);
81 void E2Sim::wait_for_sctp_data()
83 sctp_buffer_t recv_buf;
84 if(sctp_receive_data(client_fd, recv_buf) > 0)
86 LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
87 e2ap_handle_sctp_data(client_fd, recv_buf, false, this);
93 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) {
94 encoding::generate_e2apv1_subscription_response_success(e2ap_pdu, reqActionIdsAccepted, reqActionIdsRejected, accept_size, reject_size, reqRequestorId, reqInstanceId);
97 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) {
98 encoding::generate_e2apv1_indication_request_parameterized(e2ap_pdu, requestorId, instanceId, ranFunctionId, actionId, seqNum, ind_header_buf, header_length, ind_message_buf, message_length);
102 int E2Sim::run_loop(int argc, char* argv[]){
104 LOG_I("Start E2 Agent (E2 Simulator)");
109 simfile.open("simulation.txt", ios::in);
111 if (simfile.is_open()) {
113 while (getline(simfile, line)) {
114 cout << line << "\n";
123 options_t ops = read_input_options(argc, argv);
125 LOG_I("After reading input options");
127 //E2 Agent will automatically restart upon sctp disconnection
128 // int server_fd = sctp_start_server(ops.server_ip, ops.server_port);
130 client_fd = sctp_start_client(ops.server_ip, ops.server_port);
131 E2AP_PDU_t* pdu_setup = (E2AP_PDU_t*)calloc(1,sizeof(E2AP_PDU));
133 LOG_I("SCTP client has been started");
135 std::vector<encoding::ran_func_info> all_funcs;
136 RANfunctionOID_t *ranFunctionOIDe = (RANfunctionOID_t*)calloc(1,sizeof(RANfunctionOID_t));
137 uint8_t *buf = (uint8_t*)"OID123";
138 ranFunctionOIDe->buf = (uint8_t*)calloc(1,strlen((char*)buf)+1);
139 memcpy(ranFunctionOIDe->buf, buf, strlen((char*)buf)+1);
140 ranFunctionOIDe->size = strlen((char*)buf);
142 //Loop through RAN function definitions that are registered
143 LOG_I("Constructing a list of RAN functions based on registered information");
144 for (std::pair<long, OCTET_STRING_t*> elem : ran_functions_registered) {
145 char* ran_desc = (char*) calloc(1, elem.second->size+1);
146 ran_desc = (char*)elem.second->buf;
147 ran_desc[elem.second->size] = '\0';
149 LOG_I("Adding RAN function ID %ld, description: %s to the list", elem.first, ran_desc);
151 encoding::ran_func_info next_func;
153 next_func.ranFunctionId = elem.first;
154 next_func.ranFunctionDesc = elem.second;
155 next_func.ranFunctionRev = (long)2;
156 next_func.ranFunctionOId = ranFunctionOIDe;
158 all_funcs.push_back(next_func);
161 LOG_I("Generate E2AP v1 setup request for all registered RAN functions");
162 generate_e2apv1_setup_request_parameterized(pdu_setup, all_funcs);
164 xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu_setup);
166 auto buffer_size = MAX_SCTP_BUFFER;
167 unsigned char buffer[MAX_SCTP_BUFFER];
171 char error_buf[300] = {0, };
174 asn_check_constraints(&asn_DEF_E2AP_PDU, pdu_setup, error_buf, &errlen);
175 LOG_I("Error length %d, error buf %s", errlen, error_buf);
177 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu_setup, buffer, buffer_size);
179 data.len = er.encoded;
181 LOG_I("Error encoded %d", er.encoded);
183 memcpy(data.buffer, buffer, er.encoded);
185 if(sctp_send_data(client_fd, data) > 0) {
186 LOG_I("Sent E2-SETUP-REQUEST as E2AP message");
188 LOG_E("Fail to send E2-SETUP-REQUEST to peer");
191 buffer_size = MAX_SCTP_BUFFER;
192 memset(buffer, '\0', sizeof(buffer));
194 sctp_buffer_t recv_buf;
196 LOG_I("Waiting for SCTP data");
198 while(1) //constantly looking for data on SCTP interface
200 if(sctp_receive_data(client_fd, recv_buf) <= 0)
203 LOG_I("Received new data of size %d", recv_buf.len);
205 e2ap_handle_sctp_data(client_fd, recv_buf, xmlenc, this);
206 if (xmlenc) xmlenc = false;