From ce2cbb9017e9f67c1c0ef4f983df2fca2d808cf5 Mon Sep 17 00:00:00 2001
From: Andrew Burger <aburger@episci.com>
Date: Fri, 2 Oct 2020 15:19:11 -0700
Subject: [PATCH] Episys/andrew/msg4 reception failure

---
 cmake_targets/build_oai              |  4 ++--
 nfapi/oai_integration/nfapi_vnf.c    |  9 +++----
 openair2/LAYER2/MAC/ue_procedures.c  |  2 +-
 openair2/PHY_INTERFACE/phy_stub_UE.c | 10 +++++++-
 targets/RT/USER/lte-ue.c             | 35 +++++++++++++++++++++++++++-
 5 files changed, 51 insertions(+), 9 deletions(-)

diff --git a/cmake_targets/build_oai b/cmake_targets/build_oai
index 9a5d5f370c4..1846551f38e 100755
--- a/cmake_targets/build_oai
+++ b/cmake_targets/build_oai
@@ -544,8 +544,8 @@ function main() {
       #fi
     fi
     echo_info "installing protobuf/protobuf-c for flexran agent support"
-    install_protobuf_from_source
-    install_protobuf_c_from_source
+   #install_protobuf_from_source
+   #install_protobuf_c_from_source
   fi
 
   if [ "$INSTALL_OPTIONAL" = "1" ] ; then
diff --git a/nfapi/oai_integration/nfapi_vnf.c b/nfapi/oai_integration/nfapi_vnf.c
index 8b03425dafc..d5090f1c27d 100644
--- a/nfapi/oai_integration/nfapi_vnf.c
+++ b/nfapi/oai_integration/nfapi_vnf.c
@@ -1220,7 +1220,7 @@ int oai_nfapi_tx_req(nfapi_tx_request_t *tx_req)
   nfapi_vnf_p7_config_t *p7_config = vnf.p7_vnfs[0].config;
   tx_req->header.phy_id = 1; // DJP HACK TODO FIXME - need to pass this around!!!!
   tx_req->header.message_id = NFAPI_TX_REQUEST;
-  //LOG_D(PHY, "[VNF] %s() TX_REQ sfn_sf:%d number_of_pdus:%d\n", __FUNCTION__, NFAPI_SFNSF2DEC(tx_req->sfn_sf), tx_req->tx_request_body.number_of_pdus);
+  LOG_D(PHY, "[VNF] %s() TX_REQ sfn_sf:%d number_of_pdus:%d\n", __FUNCTION__, NFAPI_SFNSF2DEC(tx_req->sfn_sf), tx_req->tx_request_body.number_of_pdus);
   int retval = nfapi_vnf_p7_tx_req(p7_config, tx_req);
 
   if (retval!=0) {
@@ -1282,14 +1282,15 @@ int oai_nfapi_ul_config_req(nfapi_ul_config_request_t *ul_config_req) {
   for (int i = 0; i < num_pdus; i++)
   {
     uint8_t pdu_type = pdu_list[i].pdu_type;
+
+    LOG_I(MAC, "ul_config_req num_pdus: %u pdu_number: %d pdu_type: %u SFN.SF: %d.%d\n",
+          num_pdus, i, pdu_type, ul_config_req->sfn_sf >> 4, ul_config_req->sfn_sf & 15);
+
     if (pdu_type != NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE)
     {
       continue;
     }
 
-    LOG_I(MAC, "ul_config_req num_pdus: %u pdu_number: %d pdu_type: %u SFN.SF: %d.%d\n",
-          num_pdus, i, pdu_type, ul_config_req->sfn_sf >> 4, ul_config_req->sfn_sf & 15);
-
     for (int j = i + 1; j < num_pdus; j++)
     {
       uint8_t pdu_type2 = pdu_list[j].pdu_type;
diff --git a/openair2/LAYER2/MAC/ue_procedures.c b/openair2/LAYER2/MAC/ue_procedures.c
index 531aee0267a..a0f18175d6d 100644
--- a/openair2/LAYER2/MAC/ue_procedures.c
+++ b/openair2/LAYER2/MAC/ue_procedures.c
@@ -414,7 +414,7 @@ ue_send_sdu(module_id_t module_idP,
 
   if (payload_ptr != NULL) {
     for (i = 0; i < num_ce; i++) {
-      //    printf("ce %d : %d\n",i,rx_ces[i]);
+      LOG_D(MAC, "ce %d : %d\n",i,rx_ces[i]);
       switch (rx_ces[i]) {
         case UE_CONT_RES:
           LOG_I(MAC,
diff --git a/openair2/PHY_INTERFACE/phy_stub_UE.c b/openair2/PHY_INTERFACE/phy_stub_UE.c
index 9eff945e305..d4adcf0692e 100644
--- a/openair2/PHY_INTERFACE/phy_stub_UE.c
+++ b/openair2/PHY_INTERFACE/phy_stub_UE.c
@@ -397,6 +397,12 @@ void fill_uci_harq_indication_UE_MAC(int Mod_id,
       pdu->harq_indication_fdd_rel13.harq_tb_n[0] =
           1; // Assuming always an ACK (No NACK or DTX)
 
+      // TODO: Fix ack/dtx -- needed for 5G
+      // 1.) if received dl_config_req (with c-rnti) store this info and corresponding subframe.
+      // 2.) if receiving ul_config_req for uci ack/nack or ulsch ack/nak in subframe n
+      //     go look to see if dl_config_req (with c-rnti) was received in subframe (n - 4)
+      // 3.) if the answer to #2 is yes then send ACK IF NOT send DTX
+
     } else if ((harq_information->harq_information_rel9_fdd.ack_nack_mode == 0)
                && (harq_information->harq_information_rel9_fdd.harq_size
                    == 2)) {
@@ -714,7 +720,7 @@ int ul_config_req_UE_MAC(nfapi_ul_config_request_t *req,
       handle_nfapi_ul_pdu_UE_MAC(
           Mod_id, pdu, sfn, sf, req->ul_config_request_body.srs_present, i, req);
     } else {
-      LOG_E(MAC, "UNKNOWN UL_CONFIG_REQ PDU_TYPE or RNTI not matching pdu type: %d\n", pdu_type);
+      LOG_D(MAC, "UNKNOWN UL_CONFIG_REQ PDU_TYPE or RNTI not matching pdu type: %d\n", pdu_type);
     }
   }
 
@@ -774,6 +780,8 @@ void dl_config_req_UE_MAC_dci(int sfn,
     return;
   }
 
+  LOG_D(MAC, "%s() rnti value: 0x%x rnti type: %d\n", __func__,
+        rnti, rnti_type);
   if (rnti_type == 1) { // C-RNTI (Normal DLSCH case)
     for (int ue_id = 0; ue_id < num_ue; ue_id++) {
       if (UE_mac_inst[ue_id].crnti == rnti) {
diff --git a/targets/RT/USER/lte-ue.c b/targets/RT/USER/lte-ue.c
index 149469c3720..6c4504ecf59 100644
--- a/targets/RT/USER/lte-ue.c
+++ b/targets/RT/USER/lte-ue.c
@@ -972,6 +972,16 @@ void ue_stub_rx_handler(unsigned int num_bytes,
   }
 }
 
+uint64_t clock_usec()
+{
+    struct timespec t;
+    if (clock_gettime(CLOCK_MONOTONIC, &t) == -1)
+    {   
+        abort();
+    }
+    return (uint64_t)t.tv_sec * 1000000 + (t.tv_nsec / 1000);
+}
+
 /*!
  * \brief This is the UE thread for RX subframe n and TX subframe n+4.
  * This thread performs the phy_procedures_UE_RX() on every received slot.
@@ -1048,8 +1058,31 @@ static void *UE_phy_stub_standalone_pnf_task(void *arg)
     }
     last_sfn_sf = sfn_sf;
 
-    nfapi_dl_config_request_t *dl_config_req = get_queue(&dl_config_req_queue);
     nfapi_tx_request_pdu_t *tx_request_pdu_list = get_queue(&tx_req_pdu_queue);
+    nfapi_dl_config_request_t *dl_config_req = get_queue(&dl_config_req_queue);
+    if (tx_request_pdu_list)
+    {
+      uint64_t deadline = clock_usec() + 1000;
+      if (!dl_config_req)
+      {
+        for (;;)
+        {
+          LOG_E(MAC, "Spinning and waiting for corresponding dl_config_req\n");
+          dl_config_req = get_queue(&dl_config_req_queue);
+          if (dl_config_req)
+          {
+            break;
+          }
+          if (clock_usec() >= deadline)
+          {
+            LOG_E(MAC, "Giving up waiting for dl_config_req\n");
+            break;
+          }
+          usleep(1);
+        }
+      }
+    }
+
     nfapi_ul_config_request_t *ul_config_req = get_queue(&ul_config_req_queue);
     nfapi_hi_dci0_request_t *hi_dci0_req = get_queue(&hi_dci0_req_queue);
 
-- 
GitLab