1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
4 # Copyright 2019 Nokia *
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 ******************************************************************************/
26 #include "e2sim_defs.h"
27 #include "e2sim_sctp.hpp"
28 #include "e2ap_message_handler.hpp"
29 #include "encode_e2apv1.hpp"
33 void encode_and_send_sctp_data(E2AP_PDU_t* pdu, int client_fd)
38 data.len = e2ap_asn1c_encode_pdu(pdu, &buf);
39 memcpy(data.buffer, buf, min(data.len, MAX_SCTP_BUFFER));
41 sctp_send_data(client_fd, data);
44 void wait_for_sctp_data(int client_fd)
46 sctp_buffer_t recv_buf;
47 if(sctp_receive_data(client_fd, recv_buf) > 0)
49 LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
50 e2ap_handle_sctp_data(client_fd, recv_buf, false);
55 int main(int argc, char* argv[]){
56 LOG_I("Start E2 Agent (E2 Simulator)");
60 options_t ops = read_input_options(argc, argv);
62 //E2 Agent will automatically restart upon sctp disconnection
63 // int server_fd = sctp_start_server(ops.server_ip, ops.server_port);
64 int client_fd = sctp_start_client(ops.server_ip, ops.server_port);
65 E2AP_PDU_t* pdu_setup = (E2AP_PDU_t*)calloc(1,sizeof(E2AP_PDU));
67 // generate_e2apv1_subscription_request(pdu_setup);
68 generate_e2apv1_setup_request(pdu_setup);
70 xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu_setup);
72 auto buffer_size = MAX_SCTP_BUFFER;
73 unsigned char buffer[MAX_SCTP_BUFFER];
77 // auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu_setup, buffer, buffer_size);
78 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu_setup, buffer, buffer_size);
79 data.len = er.encoded;
81 fprintf(stderr, "er encded is %d\n", er.encoded);
83 memcpy(data.buffer, buffer, er.encoded);
85 if(sctp_send_data(client_fd, data) > 0) {
86 LOG_I("[SCTP] Sent E2-SETUP-REQUEST");
88 LOG_E("[SCTP] Unable to send E2-SETUP-REQUEST to peer");
91 sctp_buffer_t recv_buf;
93 LOG_I("[SCTP] Waiting for SCTP data");
95 while(1) //constantly looking for data on SCTP interface
97 if(sctp_receive_data(client_fd, recv_buf) <= 0)
100 LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
102 e2ap_handle_sctp_data(client_fd, recv_buf, xmlenc);
103 if (xmlenc) xmlenc = false;