Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / ricsim.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 <assert.h>
23
24 #include "e2sim_sctp.hpp"
25 #include "e2ap_message_handler.hpp"
26
27 #include "encode_e2apv1.hpp"
28 #include "encode_kpm.hpp"
29
30 extern "C" {
31   #include "e2sim_defs.h"
32   #include "E2AP-PDU.h"
33   #include "e2ap_asn1c_codec.h"
34   #include "GlobalE2node-ID.h"
35   #include "GlobalE2node-gNB-ID.h"
36   #include "GlobalgNB-ID.h"
37   #include "OCTET_STRING.h"
38   #include "asn_application.h"
39   #include "GNB-ID-Choice.h"
40   #include "ProtocolIE-Field.h"
41   #include "E2setupRequest.h"
42   #include "RICaction-ToBeSetup-Item.h"
43   #include "RICactions-ToBeSetup-List.h"
44   #include "RICeventTriggerDefinition.h"
45   #include "RICsubscriptionRequest.h"
46   #include "ProtocolIE-SingleContainer.h"
47   #include "RANfunctions-List.h"
48   #include "RICindication.h"
49   #include "RICsubsequentActionType.h"
50   #include "RICsubsequentAction.h"  
51   #include "RICtimeToWait.h"
52 }
53
54 using namespace std;
55
56 /*
57 struct {
58   type **array;
59   int count;
60   int size;
61   void (*free)(decltype(*array));
62
63 */
64
65 void encode_and_send_sctp_data(E2AP_PDU_t* pdu, int client_fd)
66 {
67   uint8_t       *buf;
68   sctp_buffer_t data;
69
70   data.len = e2ap_asn1c_encode_pdu(pdu, &buf);
71   memcpy(data.buffer, buf, min(data.len, MAX_SCTP_BUFFER));
72
73   sctp_send_data(client_fd, data);
74 }
75
76 void wait_for_sctp_data(int client_fd)
77 {
78   sctp_buffer_t recv_buf;
79   if(sctp_receive_data(client_fd, recv_buf) > 0)
80   {
81     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
82     e2ap_handle_sctp_data(client_fd, recv_buf, false);
83   }
84 }
85
86
87
88 int main(int argc, char* argv[]){
89   LOG_I("Start RIC Simulator");
90
91   bool xmlenc = true;
92
93   options_t ops = read_input_options(argc, argv);
94
95   int server_fd = sctp_start_server(ops.server_ip, ops.server_port);
96   int client_fd = sctp_accept_connection(ops.server_ip, server_fd);
97
98   sctp_buffer_t recv_buf;
99
100   LOG_I("[SCTP] Waiting for SCTP data");
101
102   while(1) //constantly looking for data on SCTP interface    
103   {
104     LOG_I("in while loop");
105     if(sctp_receive_data(client_fd, recv_buf) <= 0)
106       break;
107
108     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
109
110     e2ap_handle_sctp_data(client_fd, recv_buf, xmlenc);
111
112     if (xmlenc)
113       xmlenc = false;
114   }
115
116   return 0;  
117
118 }