e28cbd4a985657f2e921306b741dbb9c200bf116
[sim/e2-interface.git] / e2sim / src / base / e2sim.cpp
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2020 AT&T Intellectual Property                                  *
4 # Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.      *
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 std::unordered_map<long, OCTET_STRING_t*> E2Sim::getRegistered_ran_functions() {
38   return ran_functions_registered;
39 }
40
41 void E2Sim::register_subscription_callback(long func_id, SubscriptionCallback cb) {
42   fprintf(stderr,"%%%%about to register callback for subscription for func_id %d\n", func_id);
43   subscription_callbacks[func_id] = cb;
44   
45 }
46
47 SubscriptionCallback E2Sim::get_subscription_callback(long func_id) {
48   fprintf(stderr, "%%%%we are getting the subscription callback for func id %d\n", func_id);
49   SubscriptionCallback cb;
50
51   try {
52     cb = subscription_callbacks.at(func_id);
53   } catch(const std::out_of_range& e) {
54     throw std::out_of_range("Function ID is not registered");
55   }
56   return cb;
57
58 }
59
60 void E2Sim::register_e2sm(long func_id, OCTET_STRING_t *ostr) {
61
62   //Error conditions:
63   //If we already have an entry for func_id
64   
65   printf("%%%%about to register e2sm func desc for %d\n", func_id);
66
67   ran_functions_registered[func_id] = ostr;
68
69 }
70
71
72 void E2Sim::encode_and_send_sctp_data(E2AP_PDU_t* pdu)
73 {
74   uint8_t       *buf;
75   sctp_buffer_t data;
76
77   data.len = e2ap_asn1c_encode_pdu(pdu, &buf);
78   memcpy(data.buffer, buf, min(data.len, MAX_SCTP_BUFFER));
79   if (buf) free(buf);
80
81   sctp_send_data(client_fd, data);
82 }
83
84
85 void E2Sim::wait_for_sctp_data()
86 {
87   sctp_buffer_t recv_buf;
88   if(sctp_receive_data(client_fd, recv_buf) > 0)
89   {
90     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
91     e2ap_handle_sctp_data(client_fd, recv_buf, false, this);
92   }
93 }
94
95
96
97 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) {
98   encoding::generate_e2apv1_subscription_response_success(e2ap_pdu, reqActionIdsAccepted, reqActionIdsRejected, accept_size, reject_size, reqRequestorId, reqInstanceId);
99 }
100
101 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) {
102   encoding::generate_e2apv1_indication_request_parameterized(e2ap_pdu, requestorId, instanceId, ranFunctionId, actionId, seqNum, ind_header_buf, header_length, ind_message_buf, message_length);
103
104 }
105
106 int E2Sim::run_loop(int argc, char* argv[]){
107
108   printf("Start E2 Agent (E2 Simulator\n");
109
110   ifstream simfile;
111   string line;
112
113   simfile.open("simulation.txt", ios::in);
114
115   if (simfile.is_open()) {
116
117     while (getline(simfile, line)) {
118       cout << line << "\n";
119     }
120
121     simfile.close();
122
123   }
124
125   bool xmlenc = false;
126
127   options_t ops = read_input_options(argc, argv);
128
129   printf("After reading input options\n");
130
131   //E2 Agent will automatically restart upon sctp disconnection
132   //  int server_fd = sctp_start_server(ops.server_ip, ops.server_port);
133
134   client_fd = sctp_start_client(ops.server_ip, ops.server_port);
135   E2AP_PDU_t* pdu_setup = (E2AP_PDU_t*)calloc(1,sizeof(E2AP_PDU));
136
137   printf("After starting client\n");
138   printf("client_fd value is %d\n", client_fd);
139   
140   std::vector<encoding::ran_func_info> all_funcs;
141
142   //Loop through RAN function definitions that are registered
143
144   for (std::pair<long, OCTET_STRING_t*> elem : ran_functions_registered) {
145     printf("looping through ran func\n");
146     encoding::ran_func_info next_func;
147
148     next_func.ranFunctionId = elem.first;
149     next_func.ranFunctionDesc = elem.second;
150     next_func.ranFunctionRev = (long)2;
151     all_funcs.push_back(next_func);
152   }
153     
154   printf("about to call setup request encode\n");
155   
156   generate_e2apv1_setup_request_parameterized(pdu_setup, all_funcs);
157
158   printf("After generating e2setup req\n");
159
160   xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu_setup);
161
162   printf("After XER Encoding\n");
163
164   auto buffer_size = MAX_SCTP_BUFFER;
165   unsigned char buffer[MAX_SCTP_BUFFER];
166   
167   sctp_buffer_t data;
168
169   char error_buf[300] = {0, };
170   size_t errlen;
171
172   asn_check_constraints(&asn_DEF_E2AP_PDU, pdu_setup, error_buf, &errlen);
173   printf("error length %d\n", errlen);
174   printf("error buf %s\n", error_buf);
175
176   auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu_setup, buffer, buffer_size);
177
178   data.len = er.encoded;
179
180   fprintf(stderr, "er encded is %d\n", er.encoded);
181
182   memcpy(data.buffer, buffer, er.encoded);
183
184   if(sctp_send_data(client_fd, data) > 0) {
185     LOG_I("[SCTP] Sent E2-SETUP-REQUEST");
186   } else {
187     LOG_E("[SCTP] Unable to send E2-SETUP-REQUEST to peer");
188   }
189
190   sctp_buffer_t recv_buf;
191
192   LOG_I("[SCTP] Waiting for SCTP data");
193
194   while(1) //constantly looking for data on SCTP interface
195   {
196     if(sctp_receive_data(client_fd, recv_buf) <= 0)
197       break;
198
199     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
200
201     e2ap_handle_sctp_data(client_fd, recv_buf, xmlenc, this);
202     if (xmlenc) xmlenc = false;
203   }
204
205   return 0;
206 }