Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / e2sim.cpp
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2019 AT&T Intellectual Property                                  *
4 # Copyright 2019 Nokia                                                       *
5 #                                                                            *
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                                    *
9 #                                                                            *
10 #      http://www.apache.org/licenses/LICENSE-2.0                            *
11 #                                                                            *
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.                                             *
17 #                                                                            *
18 ******************************************************************************/
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <string>
23 #include <iostream>
24
25
26 #include "e2sim_defs.h"
27 #include "e2sim_sctp.hpp"
28 #include "e2ap_message_handler.hpp"
29 #include "encode_e2apv1.hpp"
30
31 using namespace std;
32
33 void encode_and_send_sctp_data(E2AP_PDU_t* pdu, int client_fd)
34 {
35   uint8_t       *buf;
36   sctp_buffer_t data;
37
38   data.len = e2ap_asn1c_encode_pdu(pdu, &buf);
39   memcpy(data.buffer, buf, min(data.len, MAX_SCTP_BUFFER));
40
41   sctp_send_data(client_fd, data);
42 }
43
44 void wait_for_sctp_data(int client_fd)
45 {
46   sctp_buffer_t recv_buf;
47   if(sctp_receive_data(client_fd, recv_buf) > 0)
48   {
49     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
50     e2ap_handle_sctp_data(client_fd, recv_buf, false);
51   }
52 }
53
54
55 int main(int argc, char* argv[]){
56   LOG_I("Start E2 Agent (E2 Simulator)");
57
58   bool xmlenc = true;
59
60   options_t ops = read_input_options(argc, argv);
61
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));
66
67   //  generate_e2apv1_subscription_request(pdu_setup);
68   generate_e2apv1_setup_request(pdu_setup);
69
70   xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu_setup);
71
72   auto buffer_size = MAX_SCTP_BUFFER;
73   unsigned char buffer[MAX_SCTP_BUFFER];
74   
75   sctp_buffer_t data;
76
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;
80
81   fprintf(stderr, "er encded is %d\n", er.encoded);
82
83   memcpy(data.buffer, buffer, er.encoded);
84
85   if(sctp_send_data(client_fd, data) > 0) {
86     LOG_I("[SCTP] Sent E2-SETUP-REQUEST");
87   } else {
88     LOG_E("[SCTP] Unable to send E2-SETUP-REQUEST to peer");
89   }
90
91   sctp_buffer_t recv_buf;
92
93   LOG_I("[SCTP] Waiting for SCTP data");
94
95   while(1) //constantly looking for data on SCTP interface
96   {
97     if(sctp_receive_data(client_fd, recv_buf) <= 0)
98       break;
99
100     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
101
102     e2ap_handle_sctp_data(client_fd, recv_buf, xmlenc);
103     if (xmlenc) xmlenc = false;
104   }
105
106   return 0;
107 }