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