fa62bede560f06ba884bc919aa1eac46efd79041
[sim/e2-interface.git] / e2sim / src / base / e2sim.cpp
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2020 AT&T Intellectual Property                                  *
4 #                                                                            *
5 # Licensed under the Apache License, Version 2.0 (the "License");            *
6 # you may not use this file except in compliance with the License.           *
7 # You may obtain a copy of the License at                                    *
8 #                                                                            *
9 #      http://www.apache.org/licenses/LICENSE-2.0                            *
10 #                                                                            *
11 # Unless required by applicable law or agreed to in writing, software        *
12 # distributed under the License is distributed on an "AS IS" BASIS,          *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
14 # See the License for the specific language governing permissions and        *
15 # limitations under the License.                                             *
16 #                                                                            *
17 ******************************************************************************/
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <string>
22 #include <iostream>
23 #include <fstream>
24 #include <vector>
25
26 #include "e2sim.hpp"
27 #include "e2sim_defs.h"
28 #include "e2sim_sctp.hpp"
29 #include "e2ap_message_handler.hpp"
30 #include "encode_e2apv1.hpp"
31
32 using namespace std;
33
34 int client_fd = 0;
35
36 void E2Sim::register_subscription_callback(long func_id, SubscriptionCallback cb) {
37   fprintf(stderr,"%%%%about to register callback for subscription for func_id %d\n", func_id);
38   subscription_callbacks[func_id] = cb;
39   
40 }
41
42 SubscriptionCallback E2Sim::get_subscription_callback(long func_id) {
43   fprintf(stderr, "%%%%we are getting the subscription callback for func id %d\n", func_id);
44   SubscriptionCallback cb;
45
46   try {
47     cb = subscription_callbacks.at(func_id);
48   } catch(const std::out_of_range& e) {
49     throw std::out_of_range("Function ID is not registered");
50   }
51   return cb;
52
53 }
54
55 void E2Sim::register_e2sm(long func_id, OCTET_STRING_t *ostr) {
56
57   //Error conditions:
58   //If we already have an entry for func_id
59   
60   printf("%%%%about to register e2sm func desc for %d\n", func_id);
61
62   ran_functions_registered[func_id] = ostr;
63
64 }
65
66
67 void E2Sim::encode_and_send_sctp_data(E2AP_PDU_t* pdu)
68 {
69   uint8_t       *buf;
70   sctp_buffer_t data;
71
72   data.len = e2ap_asn1c_encode_pdu(pdu, &buf);
73   memcpy(data.buffer, buf, min(data.len, MAX_SCTP_BUFFER));
74
75   sctp_send_data(client_fd, data);
76 }
77
78
79 void E2Sim::wait_for_sctp_data()
80 {
81   sctp_buffer_t recv_buf;
82   if(sctp_receive_data(client_fd, recv_buf) > 0)
83   {
84     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
85     e2ap_handle_sctp_data(client_fd, recv_buf, false, this);
86   }
87 }
88
89
90
91 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) {
92   encoding::generate_e2apv1_subscription_response_success(e2ap_pdu, reqActionIdsAccepted, reqActionIdsRejected, accept_size, reject_size, reqRequestorId, reqInstanceId);
93 }
94
95 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) {
96   encoding::generate_e2apv1_indication_request_parameterized(e2ap_pdu, requestorId, instanceId, ranFunctionId, actionId, seqNum, ind_header_buf, header_length, ind_message_buf, message_length);
97
98 }
99
100 int E2Sim::run_loop(int argc, char* argv[]){
101
102   printf("Start E2 Agent (E2 Simulator\n");
103
104   ifstream simfile;
105   string line;
106
107   simfile.open("simulation.txt", ios::in);
108
109   if (simfile.is_open()) {
110
111     while (getline(simfile, line)) {
112       cout << line << "\n";
113     }
114
115     simfile.close();
116
117   }
118
119   bool xmlenc = false;
120
121   options_t ops = read_input_options(argc, argv);
122
123   printf("After reading input options\n");
124
125   //E2 Agent will automatically restart upon sctp disconnection
126   //  int server_fd = sctp_start_server(ops.server_ip, ops.server_port);
127
128   client_fd = sctp_start_client(ops.server_ip, ops.server_port);
129   E2AP_PDU_t* pdu_setup = (E2AP_PDU_t*)calloc(1,sizeof(E2AP_PDU));
130
131   printf("After starting client\n");
132   printf("client_fd value is %d\n", client_fd);
133   
134   std::vector<encoding::ran_func_info> all_funcs;
135
136   //Loop through RAN function definitions that are registered
137
138   for (std::pair<long, OCTET_STRING_t*> elem : ran_functions_registered) {
139     printf("looping through ran func\n");
140     encoding::ran_func_info next_func;
141
142     next_func.ranFunctionId = elem.first;
143     next_func.ranFunctionDesc = elem.second;
144     next_func.ranFunctionRev = (long)2;
145     all_funcs.push_back(next_func);
146   }
147     
148   printf("about to call setup request encode\n");
149   
150   generate_e2apv1_setup_request_parameterized(pdu_setup, all_funcs);
151
152   printf("After generating e2setup req\n");
153
154   xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu_setup);
155
156   printf("After XER Encoding\n");
157
158   auto buffer_size = MAX_SCTP_BUFFER;
159   unsigned char buffer[MAX_SCTP_BUFFER];
160   
161   sctp_buffer_t data;
162
163   char *error_buf = (char*)calloc(300, sizeof(char));
164   size_t errlen;
165
166   asn_check_constraints(&asn_DEF_E2AP_PDU, pdu_setup, error_buf, &errlen);
167   printf("error length %d\n", errlen);
168   printf("error buf %s\n", error_buf);
169
170   auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu_setup, buffer, buffer_size);
171
172   data.len = er.encoded;
173
174   fprintf(stderr, "er encded is %d\n", er.encoded);
175
176   memcpy(data.buffer, buffer, er.encoded);
177
178   if(sctp_send_data(client_fd, data) > 0) {
179     LOG_I("[SCTP] Sent E2-SETUP-REQUEST");
180   } else {
181     LOG_E("[SCTP] Unable to send E2-SETUP-REQUEST to peer");
182   }
183
184   sctp_buffer_t recv_buf;
185
186   LOG_I("[SCTP] Waiting for SCTP data");
187
188   while(1) //constantly looking for data on SCTP interface
189   {
190     if(sctp_receive_data(client_fd, recv_buf) <= 0)
191       break;
192
193     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
194
195     e2ap_handle_sctp_data(client_fd, recv_buf, xmlenc, this);
196     if (xmlenc) xmlenc = false;
197   }
198
199   return 0;
200 }