1 /*****************************************************************************
3 # Copyright 2020 AT&T Intellectual Property *
5 # Licensed under the Apache License, Version 2.0 (the "License"); *
6 # you may not use this file except in compliance with the License. *
7 # You may obtain a copy of the License at *
9 # http://www.apache.org/licenses/LICENSE-2.0 *
11 # Unless required by applicable law or agreed to in writing, software *
12 # distributed under the License is distributed on an "AS IS" BASIS, *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 # See the License for the specific language governing permissions and *
15 # limitations under the License. *
17 ******************************************************************************/
27 #include "e2sim_defs.h"
28 #include "e2sim_sctp.hpp"
29 #include "e2ap_message_handler.hpp"
30 #include "encode_e2apv1.hpp"
36 void E2Sim::register_subscription_callback(long func_id, SubscriptionCallback cb) {
37 printf("%%%%about to register callback for subscription for func_id %d\n", func_id);
38 subscription_callbacks[func_id] = cb;
42 SubscriptionCallback E2Sim::get_subscription_callback(long func_id) {
43 printf("%%%%we are getting the subscription callback for func id %d\n", func_id);
44 SubscriptionCallback cb = subscription_callbacks[func_id];
49 void E2Sim::register_e2sm(long func_id, OCTET_STRING_t *ostr) {
52 //If we already have an entry for func_id
54 printf("%%%%about to register e2sm func desc for %d\n", func_id);
56 ran_functions_registered[func_id] = ostr;
61 void E2Sim::encode_and_send_sctp_data(E2AP_PDU_t* pdu)
66 data.len = e2ap_asn1c_encode_pdu(pdu, &buf);
67 memcpy(data.buffer, buf, min(data.len, MAX_SCTP_BUFFER));
69 sctp_send_data(client_fd, data);
73 void E2Sim::wait_for_sctp_data()
75 sctp_buffer_t recv_buf;
76 if(sctp_receive_data(client_fd, recv_buf) > 0)
78 LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
79 e2ap_handle_sctp_data(client_fd, recv_buf, false, this);
85 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) {
86 encoding::generate_e2apv1_subscription_response_success(e2ap_pdu, reqActionIdsAccepted, reqActionIdsRejected, accept_size, reject_size, reqRequestorId, reqInstanceId);
89 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) {
90 encoding::generate_e2apv1_indication_request_parameterized(e2ap_pdu, requestorId, instanceId, ranFunctionId, actionId, seqNum, ind_header_buf, header_length, ind_message_buf, message_length);
94 int E2Sim::run_loop(int argc, char* argv[]){
96 printf("Start E2 Agent (E2 Simulator\n");
101 simfile.open("simulation.txt", ios::in);
103 if (simfile.is_open()) {
105 while (getline(simfile, line)) {
106 cout << line << "\n";
115 options_t ops = read_input_options(argc, argv);
117 printf("After reading input options\n");
119 //E2 Agent will automatically restart upon sctp disconnection
120 // int server_fd = sctp_start_server(ops.server_ip, ops.server_port);
122 client_fd = sctp_start_client(ops.server_ip, ops.server_port);
123 E2AP_PDU_t* pdu_setup = (E2AP_PDU_t*)calloc(1,sizeof(E2AP_PDU));
125 printf("After starting client\n");
126 printf("client_fd value is %d\n", client_fd);
128 std::vector<encoding::ran_func_info> all_funcs;
130 //Loop through RAN function definitions that are registered
132 for (std::pair<long, OCTET_STRING_t*> elem : ran_functions_registered) {
133 printf("looping through ran func\n");
134 encoding::ran_func_info next_func;
136 next_func.ranFunctionId = elem.first;
137 next_func.ranFunctionDesc = elem.second;
138 next_func.ranFunctionRev = (long)2;
139 all_funcs.push_back(next_func);
142 printf("about to call setup request encode\n");
144 generate_e2apv1_setup_request_parameterized(pdu_setup, all_funcs);
146 printf("After generating e2setup req\n");
148 xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu_setup);
150 printf("After XER Encoding\n");
152 auto buffer_size = MAX_SCTP_BUFFER;
153 unsigned char buffer[MAX_SCTP_BUFFER];
157 char *error_buf = (char*)calloc(300, sizeof(char));
160 asn_check_constraints(&asn_DEF_E2AP_PDU, pdu_setup, error_buf, &errlen);
161 printf("error length %d\n", errlen);
162 printf("error buf %s\n", error_buf);
164 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu_setup, buffer, buffer_size);
166 data.len = er.encoded;
168 fprintf(stderr, "er encded is %d\n", er.encoded);
170 memcpy(data.buffer, buffer, er.encoded);
172 if(sctp_send_data(client_fd, data) > 0) {
173 LOG_I("[SCTP] Sent E2-SETUP-REQUEST");
175 LOG_E("[SCTP] Unable to send E2-SETUP-REQUEST to peer");
178 sctp_buffer_t recv_buf;
180 LOG_I("[SCTP] Waiting for SCTP data");
182 while(1) //constantly looking for data on SCTP interface
184 if(sctp_receive_data(client_fd, recv_buf) <= 0)
187 LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
189 e2ap_handle_sctp_data(client_fd, recv_buf, xmlenc, this);
190 if (xmlenc) xmlenc = false;