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