Issue-ID: SIM-18
[sim/e2-interface.git] / e2sim / e2apv1sim / e2sim / 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 #include <fstream>
25 #include <vector>
26
27 #include "e2sim.hpp"
28 #include "e2sim_defs.h"
29 #include "e2sim_sctp.hpp"
30 #include "e2ap_message_handler.hpp"
31 #include "encode_e2apv1.hpp"
32
33 using namespace std;
34
35 int client_fd = 0;
36
37 void E2Sim::register_subscription_callback(long func_id, SubscriptionCallback cb) {
38   printf("%%%%about to register callback for subscription for func_id %d\n", func_id);
39   subscription_callbacks[func_id] = cb;
40   
41 }
42
43 SubscriptionCallback E2Sim::get_subscription_callback(long func_id) {
44   printf("%%%%we are getting the subscription callback for func id %d\n", func_id);
45   SubscriptionCallback cb = subscription_callbacks[func_id];
46   return cb;
47
48 }
49
50 void E2Sim::register_e2sm(long func_id, OCTET_STRING_t *ostr) {
51
52   //Error conditions:
53   //If we already have an entry for func_id
54   
55   printf("%%%%about to register e2sm func desc for %d\n", func_id);
56
57   ran_functions_registered[func_id] = ostr;
58
59 }
60
61
62 void E2Sim::encode_and_send_sctp_data(E2AP_PDU_t* pdu)
63 {
64   uint8_t       *buf;
65   sctp_buffer_t data;
66
67   data.len = e2ap_asn1c_encode_pdu(pdu, &buf);
68   memcpy(data.buffer, buf, min(data.len, MAX_SCTP_BUFFER));
69
70   sctp_send_data(client_fd, data);
71 }
72
73
74 void E2Sim::wait_for_sctp_data()
75 {
76   sctp_buffer_t recv_buf;
77   if(sctp_receive_data(client_fd, recv_buf) > 0)
78   {
79     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
80     e2ap_handle_sctp_data(client_fd, recv_buf, false, this);
81   }
82 }
83
84
85 int E2Sim::run_loop(int argc, char* argv[]){
86
87   printf("Start E2 Agent (E2 Simulator\n");
88
89   ifstream simfile;
90   string line;
91
92   simfile.open("simulation.txt", ios::in);
93
94   if (simfile.is_open()) {
95
96     while (getline(simfile, line)) {
97       cout << line << "\n";
98     }
99
100     simfile.close();
101
102   }
103
104   bool xmlenc = false;
105
106   options_t ops = read_input_options(argc, argv);
107
108   printf("After reading input options\n");
109
110   //E2 Agent will automatically restart upon sctp disconnection
111   //  int server_fd = sctp_start_server(ops.server_ip, ops.server_port);
112
113   client_fd = sctp_start_client(ops.server_ip, ops.server_port);
114   E2AP_PDU_t* pdu_setup = (E2AP_PDU_t*)calloc(1,sizeof(E2AP_PDU));
115
116   printf("After starting client\n");
117   printf("client_fd value is %d\n", client_fd);
118   
119   std::vector<ran_func_info> all_funcs;
120
121   //Loop through RAN function definitions that are registered
122
123   for (std::pair<long, OCTET_STRING_t*> elem : ran_functions_registered) {
124     printf("looping through ran func\n");
125     ran_func_info next_func;
126
127     next_func.ranFunctionId = elem.first;
128     next_func.ranFunctionDesc = elem.second;
129     next_func.ranFunctionRev = (long)2;
130     all_funcs.push_back(next_func);
131   }
132     
133   printf("about to call setup request encode\n");
134   
135   generate_e2apv1_setup_request_parameterized(pdu_setup, all_funcs);
136
137   printf("After generating e2setup req\n");
138
139   xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu_setup);
140
141   printf("After XER Encoding\n");
142
143   auto buffer_size = MAX_SCTP_BUFFER;
144   unsigned char buffer[MAX_SCTP_BUFFER];
145   
146   sctp_buffer_t data;
147
148   char *error_buf = (char*)calloc(300, sizeof(char));
149   size_t errlen;
150
151   asn_check_constraints(&asn_DEF_E2AP_PDU, pdu_setup, error_buf, &errlen);
152   printf("error length %d\n", errlen);
153   printf("error buf %s\n", error_buf);
154
155   auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu_setup, buffer, buffer_size);
156
157   data.len = er.encoded;
158
159   fprintf(stderr, "er encded is %d\n", er.encoded);
160
161   memcpy(data.buffer, buffer, er.encoded);
162
163   if(sctp_send_data(client_fd, data) > 0) {
164     LOG_I("[SCTP] Sent E2-SETUP-REQUEST");
165   } else {
166     LOG_E("[SCTP] Unable to send E2-SETUP-REQUEST to peer");
167   }
168
169   sctp_buffer_t recv_buf;
170
171   LOG_I("[SCTP] Waiting for SCTP data");
172
173   while(1) //constantly looking for data on SCTP interface
174   {
175     if(sctp_receive_data(client_fd, recv_buf) <= 0)
176       break;
177
178     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
179
180     e2ap_handle_sctp_data(client_fd, recv_buf, xmlenc, this);
181     if (xmlenc) xmlenc = false;
182   }
183
184   return 0;
185 }