Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / extras / 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 extern "C" {
28   #include "e2sim_defs.h"
29   #include "E2AP-PDU.h"
30   #include "e2ap_asn1c_codec.h"
31   #include "GlobalE2node-ID.h"
32   #include "GlobalE2node-gNB-ID.h"
33   #include "GlobalgNB-ID.h"
34   #include "OCTET_STRING.h"
35   #include "asn_application.h"
36   #include "GNB-ID-Choice.h"
37   #include "ProtocolIE-Field.h"
38   #include "E2setupRequest.h"
39 }
40
41 using namespace std;
42
43 /*
44 struct {
45   type **array;
46   int count;
47   int size;
48   void (*free)(decltype(*array));
49
50 */
51
52 void encode_and_send_sctp_data(E2AP_PDU_t* pdu, int client_fd)
53 {
54   uint8_t       *buf;
55   sctp_buffer_t data;
56
57   data.len = e2ap_asn1c_encode_pdu(pdu, &buf);
58   memcpy(data.buffer, buf, min(data.len, MAX_SCTP_BUFFER));
59
60   sctp_send_data(client_fd, data);
61 }
62
63 void wait_for_sctp_data(int client_fd)
64 {
65   sctp_buffer_t recv_buf;
66   if(sctp_receive_data(client_fd, recv_buf) > 0)
67   {
68     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
69     e2ap_handle_sctp_data(client_fd, recv_buf);
70   }
71 }
72
73 int main(int argc, char* argv[]){
74   LOG_I("Start RIC Simulator");
75
76   options_t ops = read_input_options(argc, argv);
77   int client_fd = sctp_start_client(ops.server_ip, ops.server_port);
78
79   //1. Send ENDCX2Setup
80   //  E2AP_PDU_t* pdu_setup = e2ap_xml_to_pdu("E2AP_ENDCX2SetupRequest.xml");
81
82   printf("out0\n");
83
84   uint8_t *buf = (uint8_t *)"gnb1";
85
86   BIT_STRING_t gnb_bstring;
87   gnb_bstring.buf = buf;
88   gnb_bstring.size = 4;
89   gnb_bstring.bits_unused = 0;
90
91   printf("out1\n");
92   
93
94   uint8_t *buf2 = (uint8_t *)"plmn3";
95   OCTET_STRING_t plmn;
96   plmn.buf = buf2;
97   plmn.size = 5;
98
99   GNB_ID_Choice_t gnbchoice;
100   GNB_ID_Choice_PR pres2 = GNB_ID_Choice_PR_gnb_ID;
101   gnbchoice.present = pres2;
102   gnbchoice.choice.gnb_ID = gnb_bstring;
103
104   GlobalgNB_ID_t gnb;
105   gnb.plmn_id = plmn;
106   gnb.gnb_id = gnbchoice;
107
108
109
110   GlobalE2node_gNB_ID_t *e2gnb = (GlobalE2node_gNB_ID_t*)calloc(1, sizeof(GlobalE2node_gNB_ID_t));
111   e2gnb->global_gNB_ID = gnb;
112
113   /*
114   GlobalE2node_ID_t *globale2nodeid = (GlobalE2node_ID_t*)calloc(1, sizeof(GlobalE2node_ID_t));
115   GlobalE2node_ID_PR pres;
116   pres = GlobalE2node_ID_PR_gNB;
117   globale2nodeid->present = pres;
118   globale2nodeid->choice.gNB = e2gnb;
119   */
120
121   GlobalE2node_ID_t globale2nodeid;
122   GlobalE2node_ID_PR pres;
123   pres = GlobalE2node_ID_PR_gNB;
124   globale2nodeid.present = pres;
125   globale2nodeid.choice.gNB = e2gnb;  
126   
127   E2setupRequestIEs_t *e2setuprid = (E2setupRequestIEs_t*)calloc(1, sizeof(E2setupRequest_t));;
128   E2setupRequestIEs__value_PR pres3;
129   pres3 = E2setupRequestIEs__value_PR_GlobalE2node_ID;
130   e2setuprid->id = 4;
131   e2setuprid->criticality = 0;
132   e2setuprid->value.choice.GlobalE2node_ID = globale2nodeid;
133   e2setuprid->value.present = pres3;
134
135
136   E2setupRequest_t e2setupreq;
137   e2setupreq.protocolIEs.list.size = sizeof(E2setupRequestIEs);
138   e2setupreq.protocolIEs.list.count = 1;
139   e2setupreq.protocolIEs.list.array = &e2setuprid;
140
141   InitiatingMessage__value_PR pres4;
142   pres4 = InitiatingMessage__value_PR_E2setupRequest;
143   InitiatingMessage_t *initmsg = (InitiatingMessage_t*)calloc(1, sizeof(InitiatingMessage_t));
144   initmsg->procedureCode = 1;
145   initmsg->criticality = 0;
146   initmsg->value.present = pres4;
147   initmsg->value.choice.E2setupRequest = e2setupreq;
148
149   E2AP_PDU_PR pres5;
150   pres5 = E2AP_PDU_PR_initiatingMessage;
151   
152   E2AP_PDU_t *e2ap_pdu = (E2AP_PDU_t*)calloc(1, sizeof(E2AP_PDU_t));
153   e2ap_pdu->present = pres5;
154   e2ap_pdu->choice.initiatingMessage = initmsg;
155   
156   printf("over here\n");
157
158   asn_fprint(stdout, &asn_DEF_E2AP_PDU, e2ap_pdu);
159
160
161   printf("over here 2\n");
162   
163   xer_fprint(stdout, &asn_DEF_E2AP_PDU, e2ap_pdu);
164
165   printf("over here 55\n");
166
167   E2AP_PDU_t* pdu_setup = e2ap_xml_to_pdu("E2AP_SetupRequest2.xml");
168   e2ap_asn1c_print_pdu(pdu_setup);
169   encode_and_send_sctp_data(pdu_setup, client_fd);
170   LOG_I("[SCTP] Sent ENDC X2 SETUP REQUEST");
171
172
173   //  E2setupRequest_t* pdu_setup = smaller_e2ap_xml_to_pdu("E2AP_PartSetupRequest.xml");
174   //  e2ap_asn1c_print_pdu(pdu_setup);
175   //  encode_and_send_sctp_data(pdu_setup, client_fd);
176   LOG_I("[SCTP] Sent ENDC X2 SETUP REQUEST");
177
178   /*
179   GlobalE2node_ID_t* pdu_setup = smaller_e2ap_xml_to_pdu("GlobalE2node-ID.xml");
180
181   asn_fprint(stdout, &asn_DEF_GlobalE2node_ID, pdu_setup);
182
183   printf("over here 2\n");
184   
185   xer_fprint(stdout, &asn_DEF_GlobalE2node_ID, pdu_setup);  
186   */
187   /*
188   e2ap_asn1c_print_pdu(pdu_setup);
189   encode_and_send_sctp_data(pdu_setup, client_fd);
190   */
191   LOG_I("[SCTP] Sent ENDC X2 SETUP REQUEST");  
192
193   
194   //2. Receive ENDCX2SetupResponse
195   wait_for_sctp_data(client_fd);
196
197   //3. Send RICSubscriptionRequest
198   /*
199   E2AP_PDU_t* pdu_sub = e2ap_xml_to_pdu("E2AP_RICsubscriptionRequest_Ashwin.xml");
200   e2ap_asn1c_print_pdu(pdu_sub);
201   encode_and_send_sctp_data(pdu_sub, client_fd);
202   LOG_I("[SCTP] Sent RIC SUBSCRIPTION REQUEST");
203   */
204   //4. Receive RICSubscriptionResponse
205   while(1){
206     wait_for_sctp_data(client_fd);
207   }
208
209
210   //---------------------------------------
211   close(client_fd);
212   LOG_I("[SCTP] Connection closed.");
213
214   return 0;
215
216 }