10 #include "OCUCP-PF-Container.h"
11 #include "OCTET_STRING.h"
12 #include "asn_application.h"
13 #include "E2SM-KPM-IndicationMessage.h"
14 #include "FQIPERSlicesPerPlmnListItem.h"
15 #include "E2SM-KPM-RANfunction-Description.h"
16 #include "Timestamp.h"
18 #include "RICsubscriptionRequest.h"
19 #include "RICsubscriptionResponse.h"
20 #include "RICactionType.h"
21 #include "ProtocolIE-Field.h"
22 #include "ProtocolIE-SingleContainer.h"
23 #include "InitiatingMessage.h"
26 #include "encode_kpm.hpp"
28 #include "encode_e2apv1.hpp"
30 #include <nlohmann/json.hpp>
38 using json = nlohmann::json;
42 struct neighbor_cell_entry {
50 void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long actionId, int socket_fd) {
52 //Process simulation file
59 simfile.open("simulation.txt", ios::in);
61 cout << "step1" << endl;
63 std::ifstream ue_stream("ueMeasReport.txt");
64 std::ifstream cell_stream("cellMeasReport.txt");
68 ue_stream >> all_ues_json;
72 cell_stream >> all_cells_json;
74 asn_codec_ctx_t *opt_cod;
76 cout << "UE RF Measurements" << endl;
77 cout << "******************" << endl;
79 int numMeasReports = (all_ues_json["/ueMeasReport/ueMeasReportList"_json_pointer]).size();
81 for (int i = 0; i < numMeasReports; i++) {
86 cout << "UE number " + i << endl;
87 cout << "**********" << endl;
88 json::json_pointer p1(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/nrCellIdentity");
89 nextCellId = all_ues_json[p1].get<int>();
90 cout << "Serving Cell " << nextCellId << endl;
92 json::json_pointer p2(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rsrp");
93 nextRsrp = all_ues_json[p2].get<int>();
94 cout << " RSRP " << nextRsrp << endl;
95 json::json_pointer p3(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rsrq");
96 nextRsrq = all_ues_json[p3].get<int>();
97 cout << " RSRQ " << nextRsrq << endl;
98 json::json_pointer p4(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rssinr");
99 nextRssinr = all_ues_json[p4].get<int>();
100 cout << " RSSINR " << nextRssinr << endl;
102 json::json_pointer p5(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/neighbourCellList");
104 int numNeighborCells = (all_ues_json[p5]).size();
107 //REPORT Message 3 -- Encode and send OCUCP user-level report
109 E2SM_KPM_IndicationMessage_t *ind_msg3 =
110 (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
111 E2AP_PDU *pdu3 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
113 uint8_t *crnti_buf = (uint8_t*)calloc(1,2);
115 if (nextCellId == 0) {
116 uint8_t *buf2 = (uint8_t*)"12";
117 memcpy(crnti_buf, buf2, 2);
118 } else if (nextCellId == 1) {
119 uint8_t *buf2 = (uint8_t*)"22";
120 memcpy(crnti_buf, buf2, 2);
123 std::string serving_str = "{\"rsrp\": " + std::to_string(nextRsrp) + ", \"rsrq\": " +
124 std::to_string(nextRsrq) + ", \"rssinr\": " + std::to_string(nextRssinr) + "}";
125 const uint8_t *serving_buf = reinterpret_cast<const uint8_t*>(serving_str.c_str());
127 std::string neighbor_str = "[";
134 for (int j = 0; j < numNeighborCells; j++) {
135 json::json_pointer p8(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/neighbourCellList/" + std::to_string(j) + "/nbCellIdentity");
136 nextNbCell = all_ues_json[p8].get<int>();
137 cout << "Neighbor Cell " << all_ues_json[p8] << endl;
138 json::json_pointer p9(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
139 +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rsrp");
140 nextNbRsrp = all_ues_json[p9].get<int>();
141 cout << " RSRP " << nextNbRsrp << endl;
143 json::json_pointer p10(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
144 +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rsrq");
145 nextNbRsrq = all_ues_json[p10].get<int>();
146 cout << " RSRQ " << nextNbRsrq << endl;
148 json::json_pointer p11(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
149 +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rssinr");
150 nextNbRssinr = all_ues_json[p11].get<int>();
151 cout << " RSSINR " << nextNbRssinr << endl;
158 neighbor_str += "{\"CID\" : \"" + std::to_string(nextNbCell) + "\", \"Cell-RF\" : \"{\"rsrp\": " + std::to_string(nextNbRsrp) +
159 ", \"rsrq\": " + std::to_string(nextNbRsrq) + ", \"rssinr\": " + std::to_string(nextNbRssinr) + "}}";
165 const uint8_t *neighbor_buf = reinterpret_cast<const uint8_t*>(neighbor_str.c_str());
167 printf("Neighbor string\n%s", neighbor_buf);
169 uint8_t *plmnid_buf = (uint8_t*)"747";
170 uint8_t *nrcellid_buf = (uint8_t*)"12340";
172 encode_kpm_report_rancontainer_cucp_parameterized(ind_msg3, plmnid_buf, nrcellid_buf, crnti_buf, serving_buf, neighbor_buf);
174 uint8_t e2smbuffer3[8192];
175 size_t e2smbuffer_size3 = 8192;
178 asn_enc_rval_t er3 = asn_encode_to_buffer(opt_cod,
179 ATS_ALIGNED_BASIC_PER,
180 &asn_DEF_E2SM_KPM_IndicationMessage,
181 ind_msg3, e2smbuffer3, e2smbuffer_size3);
183 fprintf(stderr, "er encded is %d\n", er3.encoded);
184 fprintf(stderr, "after encoding message\n");
185 uint8_t *e2smheader_buf3 = (uint8_t*)"header";
187 generate_e2apv1_indication_request_parameterized(pdu3, requestorId,
188 instanceId, ranFunctionId,
189 actionId, seqNum, e2smheader_buf3, 6, e2smbuffer3, er3.encoded);
191 encode_and_send_sctp_data(pdu3, socket_fd);
198 cout << "Cell Measurements" << endl;
199 cout << "******************" << endl;
201 int numCellMeasReports = (all_cells_json["/cellMeasReport/cellMeasReportList"_json_pointer]).size();
203 uint8_t *sst_buf = (uint8_t*)"1";
204 uint8_t *sd_buf = (uint8_t*)"100";
205 uint8_t *plmnid_buf = (uint8_t*)"747";
207 for (int i = 0; i < numCellMeasReports; i++) {
215 json::json_pointer p1(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/nrCellIdentity");
216 nextCellId = all_cells_json[p1].get<int>();
217 cout << std::string("Cell number ") << nextCellId << endl;
219 cout << "**********" << endl;
221 json::json_pointer p2(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/pdcpByteMeasReport/pdcpBytesDl");
222 nextPdcpBytesDL = all_cells_json[p2].get<int>();
223 cout << std::string(" PDCP Bytes DL ") << nextPdcpBytesDL << endl;
225 json::json_pointer p3(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/pdcpByteMeasReport/pdcpBytesUl");
226 nextPdcpBytesUL = all_cells_json[p3].get<int>();
227 cout << std::string(" PDCP Bytes UL ") << nextPdcpBytesUL << endl;
229 uint8_t *buf = (uint8_t*)"GNBCUUP5";
231 int bytes_dl = nextPdcpBytesDL;
233 int bytes_ul = nextPdcpBytesUL;
235 // int bytes_dl = 3905;
236 // int bytes_ul = 1609321;
238 E2SM_KPM_IndicationMessage_t *ind_msg2 =
239 (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
240 E2AP_PDU *pdu2 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
242 encode_kpm_report_style5_parameterized(ind_msg2 , buf, bytes_dl, bytes_ul, sst_buf, sd_buf, plmnid_buf);
244 uint8_t e2smbuffer2[8192];
245 size_t e2smbuffer_size2 = 8192;
247 asn_enc_rval_t er2 = asn_encode_to_buffer(opt_cod,
248 ATS_ALIGNED_BASIC_PER,
249 &asn_DEF_E2SM_KPM_IndicationMessage,
250 ind_msg2, e2smbuffer2, e2smbuffer_size2);
252 fprintf(stderr, "er encded is %d\n", er2.encoded);
253 fprintf(stderr, "after encoding message\n");
254 uint8_t *e2smheader_buf2 = (uint8_t*)"header";
256 generate_e2apv1_indication_request_parameterized(pdu2, requestorId,
257 instanceId, ranFunctionId,
258 actionId, seqNum, e2smheader_buf2, 6, e2smbuffer2, er2.encoded);
260 encode_and_send_sctp_data(pdu2, socket_fd);
266 json::json_pointer p4(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/prbMeasReport/availPrbDl");
267 nextPRBBytesDL = all_cells_json[p4].get<int>();
268 cout << std::string(" PRB Bytes DL ") << all_cells_json[p4] << endl;
270 json::json_pointer p5(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/prbMeasReport/availPrbUl");
271 nextPRBBytesUL = all_cells_json[p5].get<int>();
272 cout << std::string(" PRB Bytes UL ") << all_cells_json[p5] << endl;
275 //REPORT Message 1 -- Encode and send ODU cell-level report
277 E2SM_KPM_IndicationMessage_t *ind_msg1 =
278 (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
279 E2AP_PDU *pdu = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
283 uint8_t *nrcellid_buf = (uint8_t*)"12340";
284 long dl_prbs = nextPRBBytesDL;
285 long ul_prbs = nextPRBBytesUL;
287 encode_kpm_report_style1_parameterized(ind_msg1, fiveqi, dl_prbs, ul_prbs, sst_buf, sd_buf, plmnid_buf, nrcellid_buf, &dl_prbs, &ul_prbs);
289 uint8_t e2smbuffer[8192];
290 size_t e2smbuffer_size = 8192;
292 asn_enc_rval_t er = asn_encode_to_buffer(opt_cod,
293 ATS_ALIGNED_BASIC_PER,
294 &asn_DEF_E2SM_KPM_IndicationMessage,
295 ind_msg1, e2smbuffer, e2smbuffer_size);
297 fprintf(stderr, "er encded is %d\n", er.encoded);
298 fprintf(stderr, "after encoding message\n");
299 uint8_t *e2smheader_buf = (uint8_t*)"header";
301 uint8_t *cpid_buf = (uint8_t*)"CPID";
303 fprintf(stderr, "About to encode Indication\n");
304 generate_e2apv1_indication_request_parameterized(pdu, requestorId,
305 instanceId, ranFunctionId,
306 actionId, seqNum, e2smheader_buf, 6, e2smbuffer, er.encoded);
308 encode_and_send_sctp_data(pdu, socket_fd);
316 if (simfile.is_open()) {
318 while (getline(simfile, line)) {
319 cout << line << "\n";
321 //REPORT Message 1 -- Encode and send ODU cell-level report
323 E2SM_KPM_IndicationMessage_t *ind_msg1 =
324 (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
325 E2AP_PDU *pdu = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
328 uint8_t *sst_buf = (uint8_t*)"1";
329 uint8_t *sd_buf = (uint8_t*)"100";
330 uint8_t *plmnid_buf = (uint8_t*)"747";
331 uint8_t *nrcellid_buf = (uint8_t*)"12340";
335 encode_kpm_report_style1_parameterized(ind_msg1, fiveqi, dl_prbs, ul_prbs, sst_buf, sd_buf, plmnid_buf, nrcellid_buf, &dl_prbs, &ul_prbs);
337 uint8_t e2smbuffer[8192];
338 size_t e2smbuffer_size = 8192;
339 asn_codec_ctx_t *opt_cod;
341 asn_enc_rval_t er = asn_encode_to_buffer(opt_cod,
342 ATS_ALIGNED_BASIC_PER,
343 &asn_DEF_E2SM_KPM_IndicationMessage,
344 ind_msg1, e2smbuffer, e2smbuffer_size);
346 fprintf(stderr, "er encded is %d\n", er.encoded);
347 fprintf(stderr, "after encoding message\n");
348 uint8_t *e2smheader_buf = (uint8_t*)"header";
350 uint8_t *cpid_buf = (uint8_t*)"CPID";
352 fprintf(stderr, "About to encode Indication\n");
353 generate_e2apv1_indication_request_parameterized(pdu, requestorId,
354 instanceId, ranFunctionId,
355 actionId, seqNum, e2smheader_buf, 6, e2smbuffer, er.encoded);
357 encode_and_send_sctp_data(pdu, socket_fd);
361 //REPORT Message 2 -- Encode and send OCUUP cell-level report
363 uint8_t *buf = (uint8_t*)"GNBCUUP5";
365 int bytes_dl = 40000;
366 int bytes_ul = 50000;
368 E2SM_KPM_IndicationMessage_t *ind_msg2 =
369 (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
370 E2AP_PDU *pdu2 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
372 encode_kpm_report_style5_parameterized(ind_msg2 , buf, bytes_dl, bytes_ul, sst_buf, sd_buf, plmnid_buf);
374 uint8_t e2smbuffer2[8192];
375 size_t e2smbuffer_size2 = 8192;
378 asn_enc_rval_t er2 = asn_encode_to_buffer(opt_cod,
379 ATS_ALIGNED_BASIC_PER,
380 &asn_DEF_E2SM_KPM_IndicationMessage,
381 ind_msg2, e2smbuffer2, e2smbuffer_size2);
383 fprintf(stderr, "er encded is %d\n", er2.encoded);
384 fprintf(stderr, "after encoding message\n");
385 uint8_t *e2smheader_buf2 = (uint8_t*)"header";
387 generate_e2apv1_indication_request_parameterized(pdu2, requestorId,
388 instanceId, ranFunctionId,
389 actionId, seqNum, e2smheader_buf2, 6, e2smbuffer2, er2.encoded);
391 encode_and_send_sctp_data(pdu2, socket_fd);
395 //REPORT Message 3 -- Encode and send OCUCP user-level report
397 E2SM_KPM_IndicationMessage_t *ind_msg3 =
398 (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
399 E2AP_PDU *pdu3 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
401 uint8_t *crnti_buf = (uint8_t*)"12";
402 // uint8_t *serving_buf = (uint8_t*)"RSRP10";
403 //uint8_t *neighbor_buf = (uint8_t*)"-10,-15";
408 std::string serving_str = "{\"rsrp\": " + std::to_string(rsrpServ) + ", \"rsrq\": " +
409 std::to_string(rsrqServ) + ", \"rssinr\": " + std::to_string(rssinrServ) + "}";
410 const uint8_t *serving_buf = reinterpret_cast<const uint8_t*>(serving_str.c_str());
413 neighbor_cell_entry n_entries[3];
414 n_entries[0] = {"123", 10, 0, 0};
415 n_entries[1] = {"456", 10, 0, 0};
416 n_entries[2] = {"789", 10, 0, 0};
418 std::string neighbor_str = "[";
420 for (int i=0; i < sizeof(n_entries)/sizeof(n_entries[0]); i++) {
425 neighbor_str += "{\"CID\" : \"" + std::string(n_entries[i].cellid) + "\", \"Cell-RF\" : \"{\"rsrp\": " + std::to_string(n_entries[i].rsrp) +
426 ", \"rsrq\": " + std::to_string(n_entries[i].rsrq) + ", \"rssinr\": " + std::to_string(n_entries[i].rsrp) + "}}";
431 const uint8_t *neighbor_buf = reinterpret_cast<const uint8_t*>(neighbor_str.c_str());
433 printf("Neighbor string\n%s", neighbor_buf);
435 encode_kpm_report_rancontainer_cucp_parameterized(ind_msg3, plmnid_buf, nrcellid_buf, crnti_buf, serving_buf, neighbor_buf);
437 uint8_t e2smbuffer3[8192];
438 size_t e2smbuffer_size3 = 8192;
440 asn_enc_rval_t er3 = asn_encode_to_buffer(opt_cod,
441 ATS_ALIGNED_BASIC_PER,
442 &asn_DEF_E2SM_KPM_IndicationMessage,
443 ind_msg3, e2smbuffer3, e2smbuffer_size3);
445 fprintf(stderr, "er encded is %d\n", er3.encoded);
446 fprintf(stderr, "after encoding message\n");
447 uint8_t *e2smheader_buf3 = (uint8_t*)"header";
449 generate_e2apv1_indication_request_parameterized(pdu3, requestorId,
450 instanceId, ranFunctionId,
451 actionId, seqNum, e2smheader_buf3, 6, e2smbuffer3, er3.encoded);
453 encode_and_send_sctp_data(pdu3, socket_fd);
457 //Encode and send OCUUP user-level report
461 //Encode and send ODU user-level report
475 void callback_kpm_subscription_request(E2AP_PDU_t *sub_req_pdu, int socket_fd) {
478 //Record RIC Request ID
479 //Go through RIC action to be Setup List
480 //Find first entry with REPORT action Type
482 //Encode subscription response
484 RICsubscriptionRequest_t orig_req =
485 sub_req_pdu->choice.initiatingMessage->value.choice.RICsubscriptionRequest;
487 RICsubscriptionResponse_IEs_t *ricreqid =
488 (RICsubscriptionResponse_IEs_t*)calloc(1, sizeof(RICsubscriptionResponse_IEs_t));
490 int count = orig_req.protocolIEs.list.count;
491 int size = orig_req.protocolIEs.list.size;
493 RICsubscriptionRequest_IEs_t **ies = (RICsubscriptionRequest_IEs_t**)orig_req.protocolIEs.list.array;
495 fprintf(stderr, "count%d\n", count);
496 fprintf(stderr, "size%d\n", size);
498 RICsubscriptionRequest_IEs__value_PR pres;
504 std::vector<long> actionIdsAccept;
505 std::vector<long> actionIdsReject;
507 for (int i=0; i < count; i++) {
508 RICsubscriptionRequest_IEs_t *next_ie = ies[i];
509 pres = next_ie->value.present;
511 fprintf(stderr, "next present value %d\n", pres);
514 case RICsubscriptionRequest_IEs__value_PR_RICrequestID:
516 RICrequestID_t reqId = next_ie->value.choice.RICrequestID;
517 long requestorId = reqId.ricRequestorID;
518 long instanceId = reqId.ricInstanceID;
519 fprintf(stderr, "requestorId %d\n", requestorId);
520 fprintf(stderr, "instanceId %d\n", instanceId);
521 reqRequestorId = requestorId;
522 reqInstanceId = instanceId;
526 case RICsubscriptionRequest_IEs__value_PR_RANfunctionID:
528 case RICsubscriptionRequest_IEs__value_PR_RICsubscriptionDetails:
530 RICsubscriptionDetails_t subDetails = next_ie->value.choice.RICsubscriptionDetails;
531 RICeventTriggerDefinition_t triggerDef = subDetails.ricEventTriggerDefinition;
532 RICactions_ToBeSetup_List_t actionList = subDetails.ricAction_ToBeSetup_List;
534 //We are ignoring the trigger definition
536 //We identify the first action whose type is REPORT
537 //That is the only one accepted; all others are rejected
539 int actionCount = actionList.list.count;
540 fprintf(stderr, "action count%d\n", actionCount);
542 auto **item_array = actionList.list.array;
544 bool foundAction = false;
546 for (int i=0; i < actionCount; i++) {
548 auto *next_item = item_array[i];
549 RICactionID_t actionId = ((RICaction_ToBeSetup_ItemIEs*)next_item)->value.choice.RICaction_ToBeSetup_Item.ricActionID;
550 RICactionType_t actionType = ((RICaction_ToBeSetup_ItemIEs*)next_item)->value.choice.RICaction_ToBeSetup_Item.ricActionType;
552 if (!foundAction && actionType == RICactionType_report) {
553 reqActionId = actionId;
554 actionIdsAccept.push_back(reqActionId);
555 printf("adding accept\n");
558 reqActionId = actionId;
559 printf("adding reject\n");
560 actionIdsReject.push_back(reqActionId);
570 fprintf(stderr, "After Processing Subscription Request\n");
572 fprintf(stderr, "requestorId %d\n", reqRequestorId);
573 fprintf(stderr, "instanceId %d\n", reqInstanceId);
576 for (int i=0; i < actionIdsAccept.size(); i++) {
577 fprintf(stderr, "Action ID %d %ld\n", i, actionIdsAccept.at(i));
581 E2AP_PDU *e2ap_pdu = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
583 long *accept_array = &actionIdsAccept[0];
584 long *reject_array = &actionIdsReject[0];
585 int accept_size = actionIdsAccept.size();
586 int reject_size = actionIdsReject.size();
588 generate_e2apv1_subscription_response_success(e2ap_pdu, accept_array, reject_array, accept_size, reject_size, reqRequestorId, reqInstanceId);
590 encode_and_send_sctp_data(e2ap_pdu,socket_fd);
592 //Start thread for sending REPORT messages
594 // std::thread loop_thread;
598 run_report_loop(reqRequestorId, reqInstanceId, funcId, reqActionId, socket_fd);
600 // loop_thread = std::thread(&run_report_loop);