Fix memory leakage
[sim/e2-interface.git] / e2sim / e2sm_examples / kpm_e2sm / src / kpm / kpm_callbacks.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 8171d3f..bcfb513
@@ -50,6 +50,8 @@ extern "C" {
 #include <thread>
 #include <chrono>
 
+#include "viavi_connector.hpp"
+
 using json = nlohmann::json;
 
 using namespace std;
@@ -64,6 +66,14 @@ int main(int argc, char* argv[]) {
 
   fprintf(stderr, "JSON Test\n");
 
+  uint8_t *nrcellid_buf = (uint8_t*)calloc(1,5);
+  nrcellid_buf[0] = 0x22;
+  nrcellid_buf[1] = 0x5B;
+  nrcellid_buf[2] = 0xD6;
+  nrcellid_buf[3] = 0x00;
+  nrcellid_buf[4] = 0x70;
+
+  
 
   asn_codec_ctx_t *opt_cod;
   
@@ -114,12 +124,43 @@ int main(int argc, char* argv[]) {
 
 }
 
-void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long actionId) {
+void get_cell_id(uint8_t *nrcellid_buf, char *cid_return_buf) {
+
+  uint8_t nr0 = nrcellid_buf[0] >> 4;
+  uint8_t nr1 = nrcellid_buf[0] << 4;
+  nr1 = nr1 >> 4;
+
+  uint8_t nr2 = nrcellid_buf[1] >> 4;
+  uint8_t nr3 = nrcellid_buf[1] << 4;
+  nr3 = nr3 >> 4;
+
+  uint8_t nr4 = nrcellid_buf[2] >> 4;
+  uint8_t nr5 = nrcellid_buf[2] << 4;
+  nr5 = nr5 >> 4;
 
-  std::ifstream input("/playpen/src/reports.json");
-  bool x = input.good();
+  uint8_t nr6 = nrcellid_buf[3] >> 4;
+  uint8_t nr7 = nrcellid_buf[3] << 4;
+  nr7 = nr7 >> 4;
 
-  fprintf(stderr, "%s\n", x ? "true" : "false");
+  uint8_t nr8 = nrcellid_buf[4] >> 4;
+  
+  sprintf(cid_return_buf, "373437%d%d%d%d%d%d%d%d%d", nr0,nr1,nr2,nr3,nr4,nr5,nr6,nr7,nr8);  
+
+}
+
+void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long actionId)
+{
+  std::filebuf reports_json;
+  std::streambuf *input_filebuf = &reports_json;
+
+  std::unique_ptr<viavi::RICTesterReceiver> viavi_connector;
+  if (!reports_json.open("/playpen/src/reports.json", std::ios::in)) {
+    std::cerr << "Can't open reports.json, enabling VIAVI connector instead..." << endl;
+       viavi_connector.reset(new viavi::RICTesterReceiver {3001, nullptr});
+       input_filebuf = viavi_connector->get_data_filebuf();
+  }
+
+  std::istream input {input_filebuf};
 
   long seqNum = 1;
 
@@ -129,6 +170,14 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
 
     json all_ues_json;
 
+    long fqival = 9;
+    long qcival = 9;
+
+    uint8_t *plmnid_buf = (uint8_t*)"747";
+    uint8_t *sst_buf = (uint8_t*)"1";
+    uint8_t *sd_buf = (uint8_t*)"100";
+    
+
     fprintf(stderr,"De line is %s\n", str.c_str());
 
     bool valid = false;
@@ -162,7 +211,13 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
          int nextRssinr;
          float tput;
          int prb_usage;
+         std::string ueId;
+         
          fprintf(stderr,"UE number %d\n", i);
+
+         json::json_pointer p001(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/ue-id");
+         ueId = all_ues_json[p001].get<std::string>();
+         fprintf(stderr, "UEID %s\n", ueId.c_str());     
          
          json::json_pointer p0(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/throughput");
          tput = all_ues_json[p0].get<float>();
@@ -170,7 +225,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
          
          json::json_pointer p00(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/prb_usage");
          prb_usage = all_ues_json[p00].get<int>();
-         fprintf(stderr, "Throughput %d\n", prb_usage);        
+         fprintf(stderr, "Throughput %d\n", prb_usage);
          
          json::json_pointer p1(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/nrCellIdentity");
          nextCellId = all_ues_json[p1].get<int>();
@@ -190,11 +245,28 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
          
          json::json_pointer p5(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/neighbourCellList");
          
-         uint8_t *crnti_buf = (uint8_t*)calloc(1,2);
-         
-         uint8_t *buf2 = (uint8_t*)"12";
-         memcpy(crnti_buf, buf2, 2);
+         uint8_t crnti_buf[3] = {0, };
+
+         if (ueId.find("Pedestrian") != string::npos) {
+           std::string ind = ueId.substr(11);
+           fprintf(stderr, "ind is %s\n", ind.c_str());
+
+           int indval = std::stoi(ind);
+
+           if (indval < 10) {
+             crnti_buf[1] = indval;
+             crnti_buf[0] = 0;
+           } else {
+             crnti_buf[0] = indval/10;
+             crnti_buf[1] = indval % 10;
+           }
+           
+         } else if (ueId.find("Car") != string::npos) {
+           crnti_buf[0] = 4;
+           crnti_buf[1] = 1;
+         }
          
+         //      uint8_t *buf2 = (uint8_t*)"12";
          
          std::string serving_str = "{\"rsrp\": " + std::to_string(nextRsrp) + ", \"rsrq\": " +
            std::to_string(nextRsrq) + ", \"rssinr\": " + std::to_string(nextRssinr) + "}";
@@ -232,8 +304,20 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
              neighbor_str += ",";
              
            }
+
+
+           uint8_t neighbor_cellid_buf[6] = {0, };
+           neighbor_cellid_buf[0] = 0x22;
+           neighbor_cellid_buf[1] = 0x5B;
+           neighbor_cellid_buf[2] = 0xD6;
+           neighbor_cellid_buf[3] = nextNbCell;
+           neighbor_cellid_buf[4] = 0x70;
+           
+           char cid_buf[25] = {0, };
+           get_cell_id(neighbor_cellid_buf,cid_buf);
+           
            
-           neighbor_str += "{\"CID\" : \"" + std::to_string(nextNbCell) + "\", \"Cell-RF\" : \"{\"rsrp\": " + std::to_string(nextNbRsrp) +
+           neighbor_str += "{\"CID\" : \"" + std::string(cid_buf) + "\", \"Cell-RF\" : {\"rsrp\": " + std::to_string(nextNbRsrp) +
              ", \"rsrq\": " + std::to_string(nextNbRsrq) + ", \"rssinr\": " + std::to_string(nextNbRssinr) + "}}";
            
          }
@@ -247,8 +331,28 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
          fprintf(stderr,"Creating UE-level RAN-Container CUCP message\n");
          
          const uint8_t *neighbor_buf = reinterpret_cast<const uint8_t*>(neighbor_str.c_str());
-         uint8_t *plmnid_buf = (uint8_t*)"747";
-         uint8_t *nrcellid_buf = (uint8_t*)"12340";
+
+
+         uint8_t nrcellid_buf[6] = {0, };
+         nrcellid_buf[0] = 0x22;
+         nrcellid_buf[1] = 0x5B;
+         nrcellid_buf[2] = 0xD6;
+         nrcellid_buf[3] = nextCellId;
+         nrcellid_buf[4] = 0x70;
+
+         uint8_t gnbid_buf[4] = {0, };
+         gnbid_buf[0] = 0x22;
+         gnbid_buf[1] = 0x5B;
+         gnbid_buf[2] = 0xD6;
+
+         uint8_t cuupid_buf[2] = {0, };
+         cuupid_buf[0] = 20000;
+
+         uint8_t duid_buf[2] = {0, };
+         duid_buf[0] = 20000;
+
+         uint8_t *cuupname_buf = (uint8_t*)"GNBCUUP5";   
+         
          
          E2SM_KPM_IndicationMessage_t *ind_msg_cucp_ue =
            (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
@@ -265,13 +369,14 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
                                                                   ATS_ALIGNED_BASIC_PER,
                                                                   &asn_DEF_E2SM_KPM_IndicationMessage,
                                                                   ind_msg_cucp_ue, e2sm_message_buf_cucp_ue, e2sm_message_buf_size_cucp_ue);
+         ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationMessage, ind_msg_cucp_ue);
          
          fprintf(stderr, "er encded is %d\n", er_message_cucp_ue.encoded);
          fprintf(stderr, "after encoding message\n");
 
          E2SM_KPM_IndicationHeader_t* ind_header_cucp_ue =
            (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
-         encode_e2sm_kpm_indication_header(ind_header_cucp_ue);
+         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);
          fprintf(stderr, "Now printing xer outside of function call\n");
          xer_fprint(stderr, &asn_DEF_E2SM_KPM_IndicationHeader, ind_header_cucp_ue);     
 
@@ -282,6 +387,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
                                                                  ATS_ALIGNED_BASIC_PER,
                                                                  &asn_DEF_E2SM_KPM_IndicationHeader,
                                                                  ind_header_cucp_ue, e2sm_header_buf_cucp_ue, e2sm_header_buf_size_cucp_ue);
+         ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationHeader, ind_header_cucp_ue);
 
          fprintf(stderr, "er encded is %d\n", er_header_cucp_ue.encoded);
          fprintf(stderr, "error buf is %s\n", (char*)e2sm_header_buf_cucp_ue);
@@ -300,7 +406,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
 
          seqNum++;
 
-         std::this_thread::sleep_for (std::chrono::seconds(2));
+         std::this_thread::sleep_for (std::chrono::milliseconds(50));
          
          //Creating UE-level RAN-Container CUUP message
          
@@ -323,13 +429,14 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
                                                               ATS_ALIGNED_BASIC_PER,
                                                               &asn_DEF_E2SM_KPM_IndicationMessage,
                                                               ind_msg_cuup_ue, e2sm_message_buf_cuup_ue, e2sm_message_buf_size_cuup_ue);
+         ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationMessage, ind_msg_cuup_ue);
          
          fprintf(stderr, "er encded is %d\n", er_message_cuup_ue.encoded);
          fprintf(stderr, "after encoding message\n");
 
          E2SM_KPM_IndicationHeader_t* ind_header_cuup_ue =
            (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
-         encode_e2sm_kpm_indication_header(ind_header_cuup_ue);
+         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);
 
          uint8_t e2sm_header_buf_cuup_ue[8192];
          size_t e2sm_header_buf_size_cuup_ue = 8192;
@@ -338,6 +445,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
                                                                  ATS_ALIGNED_BASIC_PER,
                                                                  &asn_DEF_E2SM_KPM_IndicationHeader,
                                                                  ind_header_cuup_ue, e2sm_header_buf_cuup_ue, e2sm_header_buf_size_cuup_ue);
+         ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationHeader, ind_header_cuup_ue);
          
          
          E2AP_PDU *pdu_cuup_ue = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));        
@@ -352,7 +460,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
 
          seqNum++;
 
-         std::this_thread::sleep_for (std::chrono::seconds(2));
+         std::this_thread::sleep_for (std::chrono::milliseconds(50));
          
          //Creating UE-level RAN-Container DU message
          
@@ -376,13 +484,14 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
                                                                 ind_message_du_ue,
                                                                 e2sm_message_buf_du_ue,
                                                                 e2sm_message_buf_size_du_ue);
+         ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationMessage, ind_message_du_ue);
          
          fprintf(stderr, "er encded is %d\n", er_message_du_ue.encoded);
          fprintf(stderr, "after encoding message\n");
 
          E2SM_KPM_IndicationHeader_t* ind_header_du_ue =
            (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
-         encode_e2sm_kpm_indication_header(ind_header_du_ue);    
+         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);
 
          uint8_t e2sm_header_buf_du_ue[8192];
          size_t e2sm_header_buf_size_du_ue = 8192;
@@ -392,6 +501,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
                                                                &asn_DEF_E2SM_KPM_IndicationHeader,
                                                                ind_header_du_ue, e2sm_header_buf_du_ue,
                                                                e2sm_header_buf_size_du_ue);
+         ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationHeader, ind_header_du_ue);
          
          
          E2AP_PDU *pdu_du_ue = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
@@ -406,7 +516,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
 
          seqNum++;
 
-         std::this_thread::sleep_for (std::chrono::seconds(2));
+         std::this_thread::sleep_for (std::chrono::milliseconds(50));
          
          fprintf(stderr, "done with ue meas report\n");
        }
@@ -424,8 +534,13 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
          float bytes_ul;
          int prb_dl;
          int prb_ul;
+         int cellid;
          
          fprintf(stderr,"UE number %d\n", i);
+
+         json::json_pointer p00(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/nrCellIdentity");
+         cellid = all_ues_json[p00].get<int>();
+         fprintf(stderr, "Cell ID %d\n", cellid);
          
          json::json_pointer p0(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/pdcpByteMeasReport/pdcpBytesDl");
          bytes_dl = all_ues_json[p0].get<float>();
@@ -443,12 +558,32 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
          prb_ul = all_ues_json[p3].get<int>();
          fprintf(stderr, "Avail PRB UL %d\n", prb_ul);
 
-         uint8_t *buf = (uint8_t*)"GNBCUUP5";
          
          uint8_t *sst_buf = (uint8_t*)"1";
          uint8_t *sd_buf = (uint8_t*)"100";
          uint8_t *plmnid_buf = (uint8_t*)"747";
 
+         uint8_t nrcellid_buf[6] = {0, };
+         nrcellid_buf[0] = 0x22;
+         nrcellid_buf[1] = 0x5B;
+         nrcellid_buf[2] = 0xD6;
+         nrcellid_buf[3] = cellid;
+         nrcellid_buf[4] = 0x70;
+
+         uint8_t gnbid_buf[4] = {0, };
+         gnbid_buf[0] = 0x22;
+         gnbid_buf[1] = 0x5B;
+         gnbid_buf[2] = 0xD6;
+
+         uint8_t cuupid_buf[2] = {0, };
+         cuupid_buf[0] = 20000;
+
+         uint8_t duid_buf[2] = {0, };
+         duid_buf[0] = 20000;
+
+         uint8_t *cuupname_buf = (uint8_t*)"GNBCUUP5";           
+         
+
          //Encoding Style 5 Message Body
          
          fprintf(stderr, "Encoding Style 5 Message body\n");
@@ -459,7 +594,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
            (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
          E2AP_PDU *pdu_style5 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
          
-         encode_kpm_report_style5_parameterized(ind_msg_style5 , buf, bytes_dl, bytes_ul, sst_buf, sd_buf, plmnid_buf);
+         encode_kpm_report_style5_parameterized(ind_msg_style5 , cuupname_buf, bytes_dl, bytes_ul, sst_buf, sd_buf, plmnid_buf);
          
          uint8_t e2sm_message_buf_style5[8192];
          size_t e2sm_message_buf_size_style5 = 8192;
@@ -468,6 +603,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
                                                                  ATS_ALIGNED_BASIC_PER,
                                                                  &asn_DEF_E2SM_KPM_IndicationMessage,
                                                                  ind_msg_style5, e2sm_message_buf_style5, e2sm_message_buf_size_style5);
+         ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationMessage, ind_msg_style5);
          
          fprintf(stderr, "er encded is %d\n", er_message_style5.encoded);
          fprintf(stderr, "after encoding message\n");
@@ -477,7 +613,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
 
          E2SM_KPM_IndicationHeader_t* ind_header_style5 =
            (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
-         encode_e2sm_kpm_indication_header(ind_header_style5);
+         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);
 
          uint8_t e2sm_header_buf_style5[8192];
          size_t e2sm_header_buf_size_style5 = 8192;
@@ -487,6 +623,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
                                                                 &asn_DEF_E2SM_KPM_IndicationHeader,
                                                                 ind_header_style5,
                                                                 e2sm_header_buf_style5, e2sm_header_buf_size_style5);    
+         ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationHeader, ind_header_style5);
          
          
          encoding::generate_e2apv1_indication_request_parameterized(pdu_style5, requestorId,
@@ -499,7 +636,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
 
          seqNum++;
 
-         std::this_thread::sleep_for (std::chrono::seconds(2));          
+         std::this_thread::sleep_for (std::chrono::milliseconds(50));    
          
 
          //Encoding Style 1 Message Body
@@ -514,8 +651,8 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
          
          long fiveqi = 7;
          
-         uint8_t *nrcellid_buf = (uint8_t*)"12340";
 
+         
          long l_dl_prbs = prb_dl;
          long l_ul_prbs = prb_ul;
          
@@ -531,6 +668,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
                                                                  &asn_DEF_E2SM_KPM_IndicationMessage,
                                                                  ind_message_style1,
                                                                  e2sm_message_buf_style1, e2sm_message_buf_size_style1);
+         ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationMessage, ind_message_style1);
          
          fprintf(stderr, "er encded is %d\n", er_message_style1.encoded);
          fprintf(stderr, "after encoding message\n");
@@ -542,7 +680,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
 
          E2SM_KPM_IndicationHeader_t* ind_header_style1 =
            (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
-         encode_e2sm_kpm_indication_header(ind_header_style1);
+         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);
 
          uint8_t e2sm_header_buf_style1[8192];
          size_t e2sm_header_buf_size_style1 = 8192;
@@ -552,6 +690,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
                                                                 &asn_DEF_E2SM_KPM_IndicationHeader,
                                                                 ind_header_style1,
                                                                 e2sm_header_buf_style1, e2sm_header_buf_size_style1);
+         ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationHeader, ind_header_style1);
          
          encoding::generate_e2apv1_indication_request_parameterized(pdu_style1, requestorId,
                                                                     instanceId, ranFunctionId,
@@ -561,7 +700,7 @@ void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long
 
          e2sim.encode_and_send_sctp_data(pdu_style1);
          seqNum++;
-         std::this_thread::sleep_for (std::chrono::seconds(2));          
+         std::this_thread::sleep_for (std::chrono::milliseconds(50));    
          
        }
       }