Fix wrong usage of memory.
[sim/e2-interface.git] / e2sim / e2sm_examples / kpm_e2sm / src / kpm / kpm_callbacks.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 <iostream>
20 #include <fstream>
21 #include <vector>
22
23
24
25 extern "C" {
26   #include "OCUCP-PF-Container.h"
27   #include "OCTET_STRING.h"
28   #include "asn_application.h"
29   #include "E2SM-KPM-IndicationMessage.h"
30   #include "FQIPERSlicesPerPlmnListItem.h"
31   #include "E2SM-KPM-RANfunction-Description.h"
32   #include "E2SM-KPM-IndicationHeader-Format1.h"
33   #include "E2SM-KPM-IndicationHeader.h"
34   #include "Timestamp.h"
35   #include "E2AP-PDU.h"
36   #include "RICsubscriptionRequest.h"
37   #include "RICsubscriptionResponse.h"
38   #include "RICactionType.h"
39   #include "ProtocolIE-Field.h"
40   #include "ProtocolIE-SingleContainer.h"
41   #include "InitiatingMessage.h"
42 }
43
44 #include "kpm_callbacks.hpp"
45 #include "encode_kpm.hpp"
46
47 #include "encode_e2apv1.hpp"
48
49 #include <nlohmann/json.hpp>
50 #include <thread>
51 #include <chrono>
52
53 #include "viavi_connector.hpp"
54
55 using json = nlohmann::json;
56
57 using namespace std;
58 class E2Sim;
59
60
61 E2Sim e2sim;
62
63 int main(int argc, char* argv[]) {
64
65   fprintf(stderr, "Starting KPM processor sim");
66
67   fprintf(stderr, "JSON Test\n");
68
69   uint8_t *nrcellid_buf = (uint8_t*)calloc(1,5);
70   nrcellid_buf[0] = 0x22;
71   nrcellid_buf[1] = 0x5B;
72   nrcellid_buf[2] = 0xD6;
73   nrcellid_buf[3] = 0x00;
74   nrcellid_buf[4] = 0x70;
75
76   
77
78   asn_codec_ctx_t *opt_cod;
79   
80   E2SM_KPM_RANfunction_Description_t *ranfunc_desc =
81     (E2SM_KPM_RANfunction_Description_t*)calloc(1,sizeof(E2SM_KPM_RANfunction_Description_t));
82   encode_kpm_function_description(ranfunc_desc);
83   
84   uint8_t e2smbuffer[8192] = {0, };
85   size_t e2smbuffer_size = 8192;
86   
87   asn_enc_rval_t er =
88     asn_encode_to_buffer(opt_cod,
89                          ATS_ALIGNED_BASIC_PER,
90                          &asn_DEF_E2SM_KPM_RANfunction_Description,
91                          ranfunc_desc, e2smbuffer, e2smbuffer_size);
92   
93   fprintf(stderr, "er encded is %d\n", er.encoded);
94   fprintf(stderr, "after encoding message\n");
95   fprintf(stderr, "here is encoded message %s\n", e2smbuffer);
96   
97   uint8_t *ranfuncdesc = (uint8_t*)calloc(1,er.encoded);
98   memcpy(ranfuncdesc, e2smbuffer, er.encoded);
99   
100   printf("this is the char array %s\n", (char*)ranfuncdesc);
101   
102   OCTET_STRING_t *ranfunc_ostr = (OCTET_STRING_t*)calloc(1,sizeof(OCTET_STRING_t));
103   ranfunc_ostr->buf = (uint8_t*)calloc(1,er.encoded);
104   ranfunc_ostr->size = er.encoded;
105   memcpy(ranfunc_ostr->buf,e2smbuffer,er.encoded);
106
107   printf("!!!lenth of ranfuncdesc is %d\n", strlen((char*)ranfuncdesc));
108   printf("value of this index is %d\n", ranfuncdesc[0]);
109   printf("value of this index is %d\n", ranfuncdesc[1]);
110   printf("value of this index is %d\n", ranfuncdesc[2]);
111   printf("value of this index is %d\n", ranfuncdesc[3]);
112   printf("value of this index is %d\n", ranfuncdesc[4]);
113   printf("value of this index is %d\n", ranfuncdesc[5]);
114   printf("value of this index is %d\n", ranfuncdesc[6]);
115   printf("value of this index is %d\n", ranfuncdesc[10]);
116   printf("value of this index is %d\n", ranfuncdesc[15]);
117   printf("value of this index is %d\n", ranfuncdesc[100]);
118   printf("value of this index is %d\n", ranfuncdesc[101]);
119   
120   e2sim.register_e2sm(0,ranfunc_ostr);
121   e2sim.register_subscription_callback(0,&callback_kpm_subscription_request);
122
123   e2sim.run_loop(argc, argv);
124
125 }
126
127 void get_cell_id(uint8_t *nrcellid_buf, char *cid_return_buf) {
128
129   uint8_t nr0 = nrcellid_buf[0] >> 4;
130   uint8_t nr1 = nrcellid_buf[0] << 4;
131   nr1 = nr1 >> 4;
132
133   uint8_t nr2 = nrcellid_buf[1] >> 4;
134   uint8_t nr3 = nrcellid_buf[1] << 4;
135   nr3 = nr3 >> 4;
136
137   uint8_t nr4 = nrcellid_buf[2] >> 4;
138   uint8_t nr5 = nrcellid_buf[2] << 4;
139   nr5 = nr5 >> 4;
140
141   uint8_t nr6 = nrcellid_buf[3] >> 4;
142   uint8_t nr7 = nrcellid_buf[3] << 4;
143   nr7 = nr7 >> 4;
144
145   uint8_t nr8 = nrcellid_buf[4] >> 4;
146   
147   sprintf(cid_return_buf, "373437%d%d%d%d%d%d%d%d%d", nr0,nr1,nr2,nr3,nr4,nr5,nr6,nr7,nr8);  
148
149 }
150
151 void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long actionId)
152 {
153   std::filebuf reports_json;
154   std::streambuf *input_filebuf = &reports_json;
155
156   std::unique_ptr<viavi::RICTesterReceiver> viavi_connector;
157   if (!reports_json.open("/playpen/src/reports.json", std::ios::in)) {
158     std::cerr << "Can't open reports.json, enabling VIAVI connector instead..." << endl;
159         viavi_connector.reset(new viavi::RICTesterReceiver {3001, nullptr});
160         input_filebuf = viavi_connector->get_data_filebuf();
161   }
162
163   std::istream input {input_filebuf};
164
165   long seqNum = 1;
166
167   std::string str;
168   
169   while ( getline(input, str) ) {
170
171     json all_ues_json;
172
173     long fqival = 9;
174     long qcival = 9;
175
176     uint8_t *plmnid_buf = (uint8_t*)"747";
177     uint8_t *sst_buf = (uint8_t*)"1";
178     uint8_t *sd_buf = (uint8_t*)"100";
179     
180
181     fprintf(stderr,"De line is %s\n", str.c_str());
182
183     bool valid = false;
184
185     try {
186       all_ues_json = json::parse(str);
187
188       valid = true;
189     } catch (...) {
190       fprintf(stderr, "Exception on reading json\n");
191     }
192
193     if (valid) {
194
195       fprintf(stderr, "key is valid\n");
196
197       std::string first_key = all_ues_json.begin().key();
198       fprintf(stderr, "first key is %s\n", first_key.c_str());
199       
200       if (first_key.compare("ueMeasReport") == 0) {
201         
202         fprintf(stderr, "it is equal to ue meas report\n");
203
204         int numMeasReports = (all_ues_json["/ueMeasReport/ueMeasReportList"_json_pointer]).size();
205         
206       
207         for (int i = 0; i < numMeasReports; i++) {
208           int nextCellId;
209           int nextRsrp;
210           int nextRsrq;
211           int nextRssinr;
212           float tput;
213           int prb_usage;
214           std::string ueId;
215           
216           fprintf(stderr,"UE number %d\n", i);
217
218           json::json_pointer p001(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/ue-id");
219           ueId = all_ues_json[p001].get<std::string>();
220           fprintf(stderr, "UEID %s\n", ueId.c_str());     
221           
222           json::json_pointer p0(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/throughput");
223           tput = all_ues_json[p0].get<float>();
224           fprintf(stderr, "Throughput %f\n", tput);
225           
226           json::json_pointer p00(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/prb_usage");
227           prb_usage = all_ues_json[p00].get<int>();
228           fprintf(stderr, "Throughput %d\n", prb_usage);
229           
230           json::json_pointer p1(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/nrCellIdentity");
231           nextCellId = all_ues_json[p1].get<int>();
232           fprintf(stderr, "Serving Cell %d\n",nextCellId);
233           
234           json::json_pointer p2(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rsrp");
235           nextRsrp = all_ues_json[p2].get<int>();
236           fprintf(stderr,"  RSRP %d\n", nextRsrp);
237           
238           json::json_pointer p3(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rsrq");
239           nextRsrq = all_ues_json[p3].get<int>();
240           fprintf(stderr,"  RSRQ %d\n",nextRsrq);
241           
242           json::json_pointer p4(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rssinr");
243           nextRssinr = all_ues_json[p4].get<int>();
244           fprintf(stderr,"  RSSINR %d\n", nextRssinr);
245           
246           json::json_pointer p5(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/neighbourCellList");
247           
248           uint8_t crnti_buf[3] = {0, };
249
250           if (ueId.find("Pedestrian") != string::npos) {
251             std::string ind = ueId.substr(11);
252             fprintf(stderr, "ind is %s\n", ind.c_str());
253
254             int indval = std::stoi(ind);
255
256             if (indval < 10) {
257               crnti_buf[1] = indval;
258               crnti_buf[0] = 0;
259             } else {
260               crnti_buf[0] = indval/10;
261               crnti_buf[1] = indval % 10;
262             }
263             
264           } else if (ueId.find("Car") != string::npos) {
265             crnti_buf[0] = 4;
266             crnti_buf[1] = 1;
267           }
268           
269           //      uint8_t *buf2 = (uint8_t*)"12";
270           
271           std::string serving_str = "{\"rsrp\": " + std::to_string(nextRsrp) + ", \"rsrq\": " +
272             std::to_string(nextRsrq) + ", \"rssinr\": " + std::to_string(nextRssinr) + "}";
273           const uint8_t *serving_buf = reinterpret_cast<const uint8_t*>(serving_str.c_str());   
274           
275           int numNeighborCells = (all_ues_json[p5]).size();
276           
277           std::string neighbor_str = "[";
278           
279           int nextNbCell;
280           int nextNbRsrp;
281           int nextNbRsrq;
282           int nextNbRssinr;
283           
284           for (int j = 0; j < numNeighborCells; j++) {
285             json::json_pointer p8(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/neighbourCellList/" + std::to_string(j) + "/nbCellIdentity");
286             nextNbCell = all_ues_json[p8].get<int>();
287             //cout << "Neighbor Cell " << all_ues_json[p8] << endl;
288             json::json_pointer p9(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
289                                   +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rsrp");
290             nextNbRsrp = all_ues_json[p9].get<int>();
291             //cout << "  RSRP " << nextNbRsrp << endl;
292             
293             json::json_pointer p10(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
294                                    +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rsrq");
295             nextNbRsrq = all_ues_json[p10].get<int>();
296             //cout << "  RSRQ " << nextNbRsrq << endl;
297             
298             json::json_pointer p11(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
299                                    +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rssinr");
300             nextNbRssinr = all_ues_json[p11].get<int>();
301             //cout << "  RSSINR " << nextNbRssinr << endl;
302             
303             if (j != 0) {
304               neighbor_str += ",";
305               
306             }
307
308
309             uint8_t neighbor_cellid_buf[6] = {0, };
310             neighbor_cellid_buf[0] = 0x22;
311             neighbor_cellid_buf[1] = 0x5B;
312             neighbor_cellid_buf[2] = 0xD6;
313             neighbor_cellid_buf[3] = nextNbCell;
314             neighbor_cellid_buf[4] = 0x70;
315             
316             char cid_buf[25] = {0, };
317             get_cell_id(neighbor_cellid_buf,cid_buf);
318             
319             
320             neighbor_str += "{\"CID\" : \"" + std::string(cid_buf) + "\", \"Cell-RF\" : {\"rsrp\": " + std::to_string(nextNbRsrp) +
321               ", \"rsrq\": " + std::to_string(nextNbRsrq) + ", \"rssinr\": " + std::to_string(nextNbRssinr) + "}}";
322             
323           }
324           
325           neighbor_str += "]";
326           
327           fprintf(stderr,"This is neighbor str %s\n", neighbor_str.c_str());
328           
329           //Creating UE-level RAN-Container CUCP message
330           
331           fprintf(stderr,"Creating UE-level RAN-Container CUCP message\n");
332           
333           const uint8_t *neighbor_buf = reinterpret_cast<const uint8_t*>(neighbor_str.c_str());
334
335
336           uint8_t nrcellid_buf[6] = {0, };
337           nrcellid_buf[0] = 0x22;
338           nrcellid_buf[1] = 0x5B;
339           nrcellid_buf[2] = 0xD6;
340           nrcellid_buf[3] = nextCellId;
341           nrcellid_buf[4] = 0x70;
342
343           uint8_t gnbid_buf[4] = {0, };
344           gnbid_buf[0] = 0x22;
345           gnbid_buf[1] = 0x5B;
346           gnbid_buf[2] = 0xD6;
347
348           uint8_t cuupid_buf[2] = {0, };
349           cuupid_buf[0] = 20000;
350
351           uint8_t duid_buf[2] = {0, };
352           duid_buf[0] = 20000;
353
354           uint8_t *cuupname_buf = (uint8_t*)"GNBCUUP5";   
355           
356           
357           E2SM_KPM_IndicationMessage_t *ind_msg_cucp_ue =
358             (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
359           
360           encode_kpm_report_rancontainer_cucp_parameterized(ind_msg_cucp_ue, plmnid_buf, nrcellid_buf, crnti_buf, serving_buf, neighbor_buf);
361           
362           uint8_t e2sm_message_buf_cucp_ue[8192] = {0, };
363           size_t e2sm_message_buf_size_cucp_ue = 8192;
364           
365           asn_codec_ctx_t *opt_cod;
366
367           
368           asn_enc_rval_t er_message_cucp_ue = asn_encode_to_buffer(opt_cod,
369                                                                    ATS_ALIGNED_BASIC_PER,
370                                                                    &asn_DEF_E2SM_KPM_IndicationMessage,
371                                                                    ind_msg_cucp_ue, e2sm_message_buf_cucp_ue, e2sm_message_buf_size_cucp_ue);
372           ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationMessage, ind_msg_cucp_ue);
373           
374           fprintf(stderr, "er encded is %d\n", er_message_cucp_ue.encoded);
375           fprintf(stderr, "after encoding message\n");
376
377           E2SM_KPM_IndicationHeader_t* ind_header_cucp_ue =
378             (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
379           encode_e2sm_kpm_indication_header(ind_header_cucp_ue, plmnid_buf, sst_buf, sd_buf, fqival, qcival, nrcellid_buf, gnbid_buf, 0, cuupid_buf, duid_buf, cuupname_buf);
380           fprintf(stderr, "Now printing xer outside of function call\n");
381           xer_fprint(stderr, &asn_DEF_E2SM_KPM_IndicationHeader, ind_header_cucp_ue);     
382
383           uint8_t e2sm_header_buf_cucp_ue[8192] = {0, };
384           size_t e2sm_header_buf_size_cucp_ue = 8192;
385           
386           asn_enc_rval_t er_header_cucp_ue = asn_encode_to_buffer(opt_cod,
387                                                                   ATS_ALIGNED_BASIC_PER,
388                                                                   &asn_DEF_E2SM_KPM_IndicationHeader,
389                                                                   ind_header_cucp_ue, e2sm_header_buf_cucp_ue, e2sm_header_buf_size_cucp_ue);
390           ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationHeader, ind_header_cucp_ue);
391
392           fprintf(stderr, "er encded is %d\n", er_header_cucp_ue.encoded);
393           fprintf(stderr, "error buf is %s\n", (char*)e2sm_header_buf_cucp_ue);
394           fprintf(stderr, "after encoding header\n");     
395           
396           E2AP_PDU *pdu_cucp_ue = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
397           
398           encoding::generate_e2apv1_indication_request_parameterized(pdu_cucp_ue, requestorId,
399                                                                      instanceId, ranFunctionId,
400                                                                      actionId, seqNum, e2sm_header_buf_cucp_ue,
401                                                                      er_header_cucp_ue.encoded, e2sm_message_buf_cucp_ue,
402                                                                      er_message_cucp_ue.encoded);
403           
404           
405           e2sim.encode_and_send_sctp_data(pdu_cucp_ue);
406
407           seqNum++;
408
409           std::this_thread::sleep_for (std::chrono::milliseconds(50));
410           
411           //Creating UE-level RAN-Container CUUP message
412           
413           fprintf(stderr,"Creating UE-level RAN-Container CUUP message\n");     
414           
415           E2SM_KPM_IndicationMessage_t *ind_msg_cuup_ue =
416             (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
417           
418           long bytes = (long)(tput * 100);
419           
420           encode_kpm_report_rancontainer_cuup_parameterized(ind_msg_cuup_ue, plmnid_buf, nrcellid_buf, crnti_buf, bytes, 0);
421           
422           uint8_t e2sm_message_buf_cuup_ue[8192] = {0, };
423           size_t e2sm_message_buf_size_cuup_ue = 8192;
424           
425           asn_codec_ctx_t *opt_cod2;
426           
427           
428           asn_enc_rval_t er_message_cuup_ue = asn_encode_to_buffer(opt_cod2,
429                                                                ATS_ALIGNED_BASIC_PER,
430                                                                &asn_DEF_E2SM_KPM_IndicationMessage,
431                                                                ind_msg_cuup_ue, e2sm_message_buf_cuup_ue, e2sm_message_buf_size_cuup_ue);
432           ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationMessage, ind_msg_cuup_ue);
433           
434           fprintf(stderr, "er encded is %d\n", er_message_cuup_ue.encoded);
435           fprintf(stderr, "after encoding message\n");
436
437           E2SM_KPM_IndicationHeader_t* ind_header_cuup_ue =
438             (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
439           encode_e2sm_kpm_indication_header(ind_header_cuup_ue, plmnid_buf, sst_buf, sd_buf, fqival, qcival, nrcellid_buf, gnbid_buf, 0, cuupid_buf, duid_buf, cuupname_buf);
440
441           uint8_t e2sm_header_buf_cuup_ue[8192] = {0, };
442           size_t e2sm_header_buf_size_cuup_ue = 8192;
443           
444           asn_enc_rval_t er_header_cuup_ue = asn_encode_to_buffer(opt_cod,
445                                                                   ATS_ALIGNED_BASIC_PER,
446                                                                   &asn_DEF_E2SM_KPM_IndicationHeader,
447                                                                   ind_header_cuup_ue, e2sm_header_buf_cuup_ue, e2sm_header_buf_size_cuup_ue);
448           ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationHeader, ind_header_cuup_ue);
449           
450           
451           E2AP_PDU *pdu_cuup_ue = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));        
452           
453           encoding::generate_e2apv1_indication_request_parameterized(pdu_cuup_ue, requestorId,
454                                                                      instanceId, ranFunctionId,
455                                                                      actionId, seqNum, e2sm_header_buf_cuup_ue,
456                                                                      er_header_cuup_ue.encoded,
457                                                                      e2sm_message_buf_cuup_ue, er_message_cuup_ue.encoded);
458
459           e2sim.encode_and_send_sctp_data(pdu_cuup_ue);
460
461           seqNum++;
462
463           std::this_thread::sleep_for (std::chrono::milliseconds(50));
464           
465           //Creating UE-level RAN-Container DU message
466           
467           fprintf(stderr,"Creating UE-level RAN-Container DU message\n");
468           
469           E2SM_KPM_IndicationMessage_t *ind_message_du_ue =
470             (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
471           
472           encode_kpm_report_rancontainer_du_parameterized(ind_message_du_ue,
473                                                           plmnid_buf, nrcellid_buf, crnti_buf, prb_usage, 0);
474           
475           uint8_t e2sm_message_buf_du_ue[8192] = {0, };
476           size_t e2sm_message_buf_size_du_ue = 8192;
477           
478           asn_codec_ctx_t *opt_cod3;
479           
480           
481           asn_enc_rval_t er_message_du_ue = asn_encode_to_buffer(opt_cod3,
482                                                                  ATS_ALIGNED_BASIC_PER,
483                                                                  &asn_DEF_E2SM_KPM_IndicationMessage,
484                                                                  ind_message_du_ue,
485                                                                  e2sm_message_buf_du_ue,
486                                                                  e2sm_message_buf_size_du_ue);
487           ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationMessage, ind_message_du_ue);
488           
489           fprintf(stderr, "er encded is %d\n", er_message_du_ue.encoded);
490           fprintf(stderr, "after encoding message\n");
491
492           E2SM_KPM_IndicationHeader_t* ind_header_du_ue =
493             (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
494           encode_e2sm_kpm_indication_header(ind_header_du_ue, plmnid_buf, sst_buf, sd_buf, fqival, qcival, nrcellid_buf, gnbid_buf, 0, cuupid_buf, duid_buf, cuupname_buf);
495
496           uint8_t e2sm_header_buf_du_ue[8192] = {0, };
497           size_t e2sm_header_buf_size_du_ue = 8192;
498           
499           asn_enc_rval_t er_header_du_ue = asn_encode_to_buffer(opt_cod,
500                                                                 ATS_ALIGNED_BASIC_PER,
501                                                                 &asn_DEF_E2SM_KPM_IndicationHeader,
502                                                                 ind_header_du_ue, e2sm_header_buf_du_ue,
503                                                                 e2sm_header_buf_size_du_ue);
504           ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationHeader, ind_header_du_ue);
505           
506           
507           E2AP_PDU *pdu_du_ue = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
508           
509           encoding::generate_e2apv1_indication_request_parameterized(pdu_du_ue, requestorId,
510                                                                      instanceId, ranFunctionId,
511                                                                      actionId, seqNum,
512                                                                      e2sm_header_buf_du_ue, er_header_du_ue.encoded,
513                                                                      e2sm_message_buf_du_ue, er_message_du_ue.encoded);
514
515           e2sim.encode_and_send_sctp_data(pdu_du_ue);
516
517           seqNum++;
518
519           std::this_thread::sleep_for (std::chrono::milliseconds(50));
520           
521           fprintf(stderr, "done with ue meas report\n");
522         }
523         
524       } else if (first_key.compare("cellMeasReport") == 0) {
525
526         fprintf(stderr, "it is equal to cell meas report\n");
527         
528         int numMeasReports = (all_ues_json["/cellMeasReport/cellMeasReportList"_json_pointer]).size();
529         
530         for (int i = 0; i < numMeasReports; i++) {
531           int nextCellId;
532
533           float bytes_dl;
534           float bytes_ul;
535           int prb_dl;
536           int prb_ul;
537           int cellid;
538           
539           fprintf(stderr,"UE number %d\n", i);
540
541           json::json_pointer p00(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/nrCellIdentity");
542           cellid = all_ues_json[p00].get<int>();
543           fprintf(stderr, "Cell ID %d\n", cellid);
544           
545           json::json_pointer p0(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/pdcpByteMeasReport/pdcpBytesDl");
546           bytes_dl = all_ues_json[p0].get<float>();
547           fprintf(stderr, "Bytes DL %f\n", bytes_dl);
548
549           json::json_pointer p1(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/pdcpByteMeasReport/pdcpBytesUl");
550           bytes_ul = all_ues_json[p1].get<float>();
551           fprintf(stderr, "Bytes UL %f\n", bytes_ul);     
552           
553           json::json_pointer p2(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/prbMeasReport/availPrbDl");
554           prb_dl = all_ues_json[p2].get<int>();
555           fprintf(stderr, "Avail PRB DL %d\n", prb_dl); 
556
557           json::json_pointer p3(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/prbMeasReport/availPrbUl");
558           prb_ul = all_ues_json[p3].get<int>();
559           fprintf(stderr, "Avail PRB UL %d\n", prb_ul);
560
561           
562           uint8_t *sst_buf = (uint8_t*)"1";
563           uint8_t *sd_buf = (uint8_t*)"100";
564           uint8_t *plmnid_buf = (uint8_t*)"747";
565
566           uint8_t nrcellid_buf[6] = {0, };
567           nrcellid_buf[0] = 0x22;
568           nrcellid_buf[1] = 0x5B;
569           nrcellid_buf[2] = 0xD6;
570           nrcellid_buf[3] = cellid;
571           nrcellid_buf[4] = 0x70;
572
573           uint8_t gnbid_buf[4] = {0, };
574           gnbid_buf[0] = 0x22;
575           gnbid_buf[1] = 0x5B;
576           gnbid_buf[2] = 0xD6;
577
578           uint8_t cuupid_buf[2] = {0, };
579           cuupid_buf[0] = 20000;
580
581           uint8_t duid_buf[2] = {0, };
582           duid_buf[0] = 20000;
583
584           uint8_t *cuupname_buf = (uint8_t*)"GNBCUUP5";           
585           
586
587           //Encoding Style 5 Message Body
588           
589           fprintf(stderr, "Encoding Style 5 Message body\n");
590
591           asn_codec_ctx_t *opt_cod;
592
593           E2SM_KPM_IndicationMessage_t *ind_msg_style5 =
594             (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
595           E2AP_PDU *pdu_style5 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
596           
597           encode_kpm_report_style5_parameterized(ind_msg_style5 , cuupname_buf, bytes_dl, bytes_ul, sst_buf, sd_buf, plmnid_buf);
598           
599           uint8_t e2sm_message_buf_style5[8192] = {0, };
600           size_t e2sm_message_buf_size_style5 = 8192;
601           
602           asn_enc_rval_t er_message_style5 = asn_encode_to_buffer(opt_cod,
603                                                                   ATS_ALIGNED_BASIC_PER,
604                                                                   &asn_DEF_E2SM_KPM_IndicationMessage,
605                                                                   ind_msg_style5, e2sm_message_buf_style5, e2sm_message_buf_size_style5);
606           ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationMessage, ind_msg_style5);
607           
608           fprintf(stderr, "er encded is %d\n", er_message_style5.encoded);
609           fprintf(stderr, "after encoding message\n");
610           
611           int seqNum0 = 1;
612
613
614           E2SM_KPM_IndicationHeader_t* ind_header_style5 =
615             (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
616           encode_e2sm_kpm_indication_header(ind_header_style5, plmnid_buf, sst_buf, sd_buf, fqival, qcival, nrcellid_buf, gnbid_buf, 0, cuupid_buf, duid_buf, cuupname_buf);
617
618           uint8_t e2sm_header_buf_style5[8192] = {0, };
619           size_t e2sm_header_buf_size_style5 = 8192;
620           
621           asn_enc_rval_t er_header_style5 = asn_encode_to_buffer(opt_cod,
622                                                                  ATS_ALIGNED_BASIC_PER,
623                                                                  &asn_DEF_E2SM_KPM_IndicationHeader,
624                                                                  ind_header_style5,
625                                                                  e2sm_header_buf_style5, e2sm_header_buf_size_style5);    
626           ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationHeader, ind_header_style5);
627           
628           
629           encoding::generate_e2apv1_indication_request_parameterized(pdu_style5, requestorId,
630                                                                      instanceId, ranFunctionId,
631                                                                      actionId, seqNum0, e2sm_header_buf_style5,
632                                                                      er_header_style5.encoded, e2sm_message_buf_style5,
633                                                                      er_message_style5.encoded);
634
635           e2sim.encode_and_send_sctp_data(pdu_style5);
636
637           seqNum++;
638
639           std::this_thread::sleep_for (std::chrono::milliseconds(50));    
640           
641
642           //Encoding Style 1 Message Body
643           
644           fprintf(stderr, "Encoding Style 1 Message body\n");     
645           asn_codec_ctx_t *opt_cod2;
646           
647           
648           E2SM_KPM_IndicationMessage_t *ind_message_style1 =
649             (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
650           E2AP_PDU *pdu_style1 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
651           
652           long fiveqi = 7;
653           
654
655           
656           long *l_dl_prbs = (long*)calloc(1, sizeof(long));
657           long *l_ul_prbs = (long*)calloc(1, sizeof(long));
658           *l_dl_prbs = (long)prb_dl;
659           *l_ul_prbs = (long)prb_ul;
660           
661           encode_kpm_report_style1_parameterized(ind_message_style1, fiveqi,
662                                                  prb_dl, prb_ul, sst_buf, sd_buf,
663                                                  plmnid_buf, nrcellid_buf, l_dl_prbs, l_ul_prbs);
664           
665           uint8_t e2sm_message_buf_style1[8192] = {0, };
666           size_t e2sm_message_buf_size_style1 = 8192;
667           
668           asn_enc_rval_t er_message_style1 = asn_encode_to_buffer(opt_cod2,
669                                                                   ATS_ALIGNED_BASIC_PER,
670                                                                   &asn_DEF_E2SM_KPM_IndicationMessage,
671                                                                   ind_message_style1,
672                                                                   e2sm_message_buf_style1, e2sm_message_buf_size_style1);
673           ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationMessage, ind_message_style1);
674           
675           fprintf(stderr, "er encded is %d\n", er_message_style1.encoded);
676           fprintf(stderr, "after encoding message\n");
677
678           
679           uint8_t *cpid_buf2 = (uint8_t*)"CPID";
680           
681           fprintf(stderr, "About to encode Indication\n");
682
683           E2SM_KPM_IndicationHeader_t* ind_header_style1 =
684             (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
685           encode_e2sm_kpm_indication_header(ind_header_style1, plmnid_buf, sst_buf, sd_buf, fqival, qcival, nrcellid_buf, gnbid_buf, 0, cuupid_buf, duid_buf, cuupname_buf);
686
687           uint8_t e2sm_header_buf_style1[8192] = {0, };
688           size_t e2sm_header_buf_size_style1 = 8192;
689           
690           asn_enc_rval_t er_header_style1 = asn_encode_to_buffer(opt_cod,
691                                                                  ATS_ALIGNED_BASIC_PER,
692                                                                  &asn_DEF_E2SM_KPM_IndicationHeader,
693                                                                  ind_header_style1,
694                                                                  e2sm_header_buf_style1, e2sm_header_buf_size_style1);
695           ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationHeader, ind_header_style1);
696           
697           encoding::generate_e2apv1_indication_request_parameterized(pdu_style1, requestorId,
698                                                                      instanceId, ranFunctionId,
699                                                                      actionId, seqNum, e2sm_header_buf_style1,
700                                                                      er_header_style1.encoded,
701                                                                      e2sm_message_buf_style1, er_message_style1.encoded);
702
703           e2sim.encode_and_send_sctp_data(pdu_style1);
704           seqNum++;
705           std::this_thread::sleep_for (std::chrono::milliseconds(50));    
706           
707         }
708       }                                            
709     }
710   }
711 }
712
713
714 void callback_kpm_subscription_request(E2AP_PDU_t *sub_req_pdu) {
715
716   fprintf(stderr, "Calling callback_kpm_subscription_request\n");
717
718   //Record RIC Request ID
719   //Go through RIC action to be Setup List
720   //Find first entry with REPORT action Type
721   //Record ricActionID
722   //Encode subscription response
723
724   RICsubscriptionRequest_t orig_req =
725     sub_req_pdu->choice.initiatingMessage->value.choice.RICsubscriptionRequest;
726   
727   RICsubscriptionResponse_IEs_t *ricreqid =
728     (RICsubscriptionResponse_IEs_t*)calloc(1, sizeof(RICsubscriptionResponse_IEs_t));
729                                            
730   int count = orig_req.protocolIEs.list.count;
731   int size = orig_req.protocolIEs.list.size;
732   
733   RICsubscriptionRequest_IEs_t **ies = (RICsubscriptionRequest_IEs_t**)orig_req.protocolIEs.list.array;
734
735   fprintf(stderr, "count%d\n", count);
736   fprintf(stderr, "size%d\n", size);
737
738   RICsubscriptionRequest_IEs__value_PR pres;
739
740   long reqRequestorId;
741   long reqInstanceId;
742   long reqActionId;
743
744   std::vector<long> actionIdsAccept;
745   std::vector<long> actionIdsReject;
746
747   for (int i=0; i < count; i++) {
748     RICsubscriptionRequest_IEs_t *next_ie = ies[i];
749     pres = next_ie->value.present;
750     
751     fprintf(stderr, "The next present value %d\n", pres);
752
753     switch(pres) {
754     case RICsubscriptionRequest_IEs__value_PR_RICrequestID:
755       {
756         fprintf(stderr,"in case request id\n"); 
757         RICrequestID_t reqId = next_ie->value.choice.RICrequestID;
758         long requestorId = reqId.ricRequestorID;
759         long instanceId = reqId.ricInstanceID;
760         fprintf(stderr, "requestorId %d\n", requestorId);
761         fprintf(stderr, "instanceId %d\n", instanceId);
762         reqRequestorId = requestorId;
763         reqInstanceId = instanceId;
764
765         break;
766       }
767     case RICsubscriptionRequest_IEs__value_PR_RANfunctionID:
768       {
769         fprintf(stderr,"in case ran func id\n");        
770         break;
771       }
772     case RICsubscriptionRequest_IEs__value_PR_RICsubscriptionDetails:
773       {
774         fprintf(stderr,"in case subscription details\n");
775         RICsubscriptionDetails_t subDetails = next_ie->value.choice.RICsubscriptionDetails;
776         fprintf(stderr,"in case subscription details 1\n");     
777         RICeventTriggerDefinition_t triggerDef = subDetails.ricEventTriggerDefinition;
778         fprintf(stderr,"in case subscription details 2\n");     
779         RICactions_ToBeSetup_List_t actionList = subDetails.ricAction_ToBeSetup_List;
780         fprintf(stderr,"in case subscription details 3\n");
781         //We are ignoring the trigger definition
782
783         //We identify the first action whose type is REPORT
784         //That is the only one accepted; all others are rejected
785         
786         int actionCount = actionList.list.count;
787         fprintf(stderr, "action count%d\n", actionCount);
788
789         auto **item_array = actionList.list.array;
790
791         bool foundAction = false;
792
793         for (int i=0; i < actionCount; i++) {
794
795           auto *next_item = item_array[i];
796           RICactionID_t actionId = ((RICaction_ToBeSetup_ItemIEs*)next_item)->value.choice.RICaction_ToBeSetup_Item.ricActionID;
797           RICactionType_t actionType = ((RICaction_ToBeSetup_ItemIEs*)next_item)->value.choice.RICaction_ToBeSetup_Item.ricActionType;
798
799           if (!foundAction && actionType == RICactionType_report) {
800             reqActionId = actionId;
801             actionIdsAccept.push_back(reqActionId);
802             printf("adding accept\n");
803             foundAction = true;
804           } else {
805             reqActionId = actionId;
806             printf("adding reject\n");
807             actionIdsReject.push_back(reqActionId);
808           }
809         }
810         
811         break;
812       }
813     default:
814       {
815         fprintf(stderr,"in case default\n");    
816         break;
817       }      
818     }
819     
820   }
821
822   fprintf(stderr, "After Processing Subscription Request\n");
823
824   fprintf(stderr, "requestorId %d\n", reqRequestorId);
825   fprintf(stderr, "instanceId %d\n", reqInstanceId);
826
827
828   for (int i=0; i < actionIdsAccept.size(); i++) {
829     fprintf(stderr, "Action ID %d %ld\n", i, actionIdsAccept.at(i));
830     
831   }
832
833   E2AP_PDU *e2ap_pdu = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
834
835   long *accept_array = &actionIdsAccept[0];
836   long *reject_array = &actionIdsReject[0];
837   int accept_size = actionIdsAccept.size();
838   int reject_size = actionIdsReject.size();
839
840   encoding::generate_e2apv1_subscription_response_success(e2ap_pdu, accept_array, reject_array, accept_size, reject_size, reqRequestorId, reqInstanceId);
841
842   e2sim.encode_and_send_sctp_data(e2ap_pdu);
843
844   //Start thread for sending REPORT messages
845
846   //  std::thread loop_thread;
847
848   long funcId = 0;
849
850   run_report_loop(reqRequestorId, reqInstanceId, funcId, reqActionId);
851
852   //  loop_thread = std::thread(&run_report_loop);
853
854 }