From 04b234c8784a85e38d4dd261c281f66b190df209 Mon Sep 17 00:00:00 2001
From: rmagueta <rmagueta@allbesmart.pt>
Date: Tue, 12 Apr 2022 19:06:02 +0100
Subject: [PATCH] Send CSI-RS measurements from PHY to MAC at UE

---
 .../nfapi/public_inc/fapi_nr_ue_constants.h     |  1 +
 .../nfapi/public_inc/fapi_nr_ue_interface.h     | 11 +++++++----
 openair1/PHY/NR_UE_TRANSPORT/csi_rx.c           | 17 +++++++++++++++++
 openair1/SCHED_NR_UE/defs.h                     |  2 +-
 openair1/SCHED_NR_UE/phy_procedures_nr_ue.c     | 10 +++++++---
 openair1/SIMULATION/NR_PHY/nr_dummy_functions.c |  2 +-
 openair2/LAYER2/NR_MAC_UE/mac_defs.h            |  3 +++
 openair2/LAYER2/NR_MAC_UE/mac_proto.h           |  1 +
 openair2/LAYER2/NR_MAC_UE/nr_ue_procedures.c    | 10 ++++++++++
 openair2/NR_UE_PHY_INTERFACE/NR_IF_Module.c     | 12 +++++++++++-
 10 files changed, 59 insertions(+), 10 deletions(-)

diff --git a/nfapi/open-nFAPI/nfapi/public_inc/fapi_nr_ue_constants.h b/nfapi/open-nFAPI/nfapi/public_inc/fapi_nr_ue_constants.h
index 7fb42e566aa..6524fcb3967 100644
--- a/nfapi/open-nFAPI/nfapi/public_inc/fapi_nr_ue_constants.h
+++ b/nfapi/open-nFAPI/nfapi/public_inc/fapi_nr_ue_constants.h
@@ -19,6 +19,7 @@
 #define FAPI_NR_RX_PDU_TYPE_DLSCH 0x03 
 #define FAPI_NR_DCI_IND 0x04
 #define FAPI_NR_RX_PDU_TYPE_RAR 0x05
+#define FAPI_NR_CSIRS_IND 0x06
 
 #define FAPI_NR_SIBS_MASK_SIB1 0x1
 
diff --git a/nfapi/open-nFAPI/nfapi/public_inc/fapi_nr_ue_interface.h b/nfapi/open-nFAPI/nfapi/public_inc/fapi_nr_ue_interface.h
index 725c3890704..e67897b44d3 100644
--- a/nfapi/open-nFAPI/nfapi/public_inc/fapi_nr_ue_interface.h
+++ b/nfapi/open-nFAPI/nfapi/public_inc/fapi_nr_ue_interface.h
@@ -36,9 +36,6 @@
 */
 
 
-
-
-
 typedef struct {
   uint8_t uci_format;
   uint8_t uci_channel;
@@ -50,7 +47,12 @@ typedef struct {
   uint32_t sr;
 } fapi_nr_uci_pdu_rel15_t;
 
-    
+typedef struct {
+  uint8_t *rank_indicator;
+  uint8_t *i1;
+  uint8_t *i2;
+  uint8_t *cqi;
+} fapi_nr_csirs_measurements_t;
 
 typedef struct {
   /// frequency_domain_resource;
@@ -130,6 +132,7 @@ typedef struct {
     fapi_nr_pdsch_pdu_t pdsch_pdu;
     fapi_nr_ssb_pdu_t ssb_pdu;
     fapi_nr_sib_pdu_t sib_pdu;
+    fapi_nr_csirs_measurements_t csirs_measurements;
   };
 } fapi_nr_rx_indication_body_t;
 
diff --git a/openair1/PHY/NR_UE_TRANSPORT/csi_rx.c b/openair1/PHY/NR_UE_TRANSPORT/csi_rx.c
index 77b611d73c6..0b63c4a1495 100644
--- a/openair1/PHY/NR_UE_TRANSPORT/csi_rx.c
+++ b/openair1/PHY/NR_UE_TRANSPORT/csi_rx.c
@@ -676,5 +676,22 @@ int nr_ue_csi_rs_procedures(PHY_VARS_NR_UE *ue, UE_nr_rxtx_proc_t *proc, uint8_t
         *ue->nr_csi_rs_info->precoded_sinr_dB,
         *ue->nr_csi_rs_info->cqi);
 
+  // Send CSI measurements to MAC
+  fapi_nr_csirs_measurements_t csirs_measurements;
+  csirs_measurements.rank_indicator = ue->nr_csi_rs_info->rank_indicator;
+  csirs_measurements.i1 = ue->nr_csi_rs_info->i1;
+  csirs_measurements.i2 = ue->nr_csi_rs_info->i2;
+  csirs_measurements.cqi = ue->nr_csi_rs_info->cqi;
+  nr_downlink_indication_t *dl_indication = calloc(sizeof(*dl_indication),1);
+  fapi_nr_rx_indication_t *rx_ind = calloc(sizeof(*rx_ind),1);
+  nr_fill_dl_indication(dl_indication, NULL, rx_ind, proc, ue, gNB_id);
+  nr_fill_rx_indication(rx_ind, FAPI_NR_CSIRS_IND, gNB_id, ue, NULL, NULL, 1, proc, (void *)&csirs_measurements);
+  if (ue->if_inst && ue->if_inst->dl_indication) {
+    ue->if_inst->dl_indication(dl_indication, NULL);
+  } else {
+    free(dl_indication);
+    free(rx_ind);
+  }
+
   return 0;
 }
diff --git a/openair1/SCHED_NR_UE/defs.h b/openair1/SCHED_NR_UE/defs.h
index c449dba1592..39faaa0cb3f 100644
--- a/openair1/SCHED_NR_UE/defs.h
+++ b/openair1/SCHED_NR_UE/defs.h
@@ -402,7 +402,7 @@ void nr_fill_rx_indication(fapi_nr_rx_indication_t *rx_ind,
                            NR_UE_DLSCH_t *dlsch1,
                            uint16_t n_pdus,
                            UE_nr_rxtx_proc_t *proc,
-			   void * typeSpecific);
+                           void *typeSpecific);
 
 bool nr_ue_dlsch_procedures(PHY_VARS_NR_UE *ue,
                             UE_nr_rxtx_proc_t *proc,
diff --git a/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c b/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
index 262acdec3a9..59b08d98234 100644
--- a/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
+++ b/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
@@ -118,9 +118,8 @@ void nr_fill_rx_indication(fapi_nr_rx_indication_t *rx_ind,
                            NR_UE_DLSCH_t *dlsch0,
                            NR_UE_DLSCH_t *dlsch1,
                            uint16_t n_pdus,
-			   UE_nr_rxtx_proc_t *proc,
-			   void * typeSpecific){
-
+                           UE_nr_rxtx_proc_t *proc,
+                           void *typeSpecific){
 
   NR_DL_FRAME_PARMS *frame_parms = &ue->frame_parms;
 
@@ -174,6 +173,11 @@ void nr_fill_rx_indication(fapi_nr_rx_indication_t *rx_ind,
       rx_ind->rx_indication_body[n_pdus - 1].ssb_pdu.ssb_start_subcarrier = frame_parms->ssb_start_subcarrier;
       rx_ind->rx_indication_body[n_pdus - 1].ssb_pdu.rsrp_dBm = ue->measurements.rsrp_dBm[gNB_id];
     break;
+    case FAPI_NR_CSIRS_IND:
+      memcpy(&rx_ind->rx_indication_body[n_pdus - 1].csirs_measurements,
+             (fapi_nr_csirs_measurements_t*)typeSpecific,
+             sizeof(*(fapi_nr_csirs_measurements_t*)typeSpecific));
+      break;
     default:
     break;
   }
diff --git a/openair1/SIMULATION/NR_PHY/nr_dummy_functions.c b/openair1/SIMULATION/NR_PHY/nr_dummy_functions.c
index 351c2f33c47..de715214b98 100644
--- a/openair1/SIMULATION/NR_PHY/nr_dummy_functions.c
+++ b/openair1/SIMULATION/NR_PHY/nr_dummy_functions.c
@@ -37,4 +37,4 @@ void nr_fill_rx_indication(fapi_nr_rx_indication_t *rx_ind,
                            NR_UE_DLSCH_t *dlsch1,
                            uint16_t n_pdus,
                            UE_nr_rxtx_proc_t *proc,
-			   void * typeSpecific ) {}
+                           void *typeSpecific ) {}
diff --git a/openair2/LAYER2/NR_MAC_UE/mac_defs.h b/openair2/LAYER2/NR_MAC_UE/mac_defs.h
index fc2c2d2c8b7..09153e6f13c 100644
--- a/openair2/LAYER2/NR_MAC_UE/mac_defs.h
+++ b/openair2/LAYER2/NR_MAC_UE/mac_defs.h
@@ -399,6 +399,9 @@ typedef struct {
   /// measured SSB RSRP in dBm
   short ssb_rsrp_dBm;
 
+  /// measurements from CSI-RS
+  fapi_nr_csirs_measurements_t csirs_measurements;
+
   /// Last NDI of UL HARQ processes
   uint8_t UL_ndi[NR_MAX_HARQ_PROCESSES];
   /// first ULTX of UL HARQ processes
diff --git a/openair2/LAYER2/NR_MAC_UE/mac_proto.h b/openair2/LAYER2/NR_MAC_UE/mac_proto.h
index 3e4d53b985b..c57c427f143 100644
--- a/openair2/LAYER2/NR_MAC_UE/mac_proto.h
+++ b/openair2/LAYER2/NR_MAC_UE/mac_proto.h
@@ -193,6 +193,7 @@ int nr_get_sf_retxBSRTimer(uint8_t retxBSR_Timer);
 
 int8_t nr_ue_process_dci(module_id_t module_id, int cc_id, uint8_t gNB_index, frame_t frame, int slot, dci_pdu_rel15_t *dci, fapi_nr_dci_indication_pdu_t *dci_ind);
 int nr_ue_process_dci_indication_pdu(module_id_t module_id, int cc_id, int gNB_index, frame_t frame, int slot, fapi_nr_dci_indication_pdu_t *dci);
+int8_t nr_ue_process_csirs_measurements(module_id_t module_id, frame_t frame, int slot, fapi_nr_csirs_measurements_t *csirs_measurements);
 
 uint32_t get_ssb_frame(uint32_t test);
 
diff --git a/openair2/LAYER2/NR_MAC_UE/nr_ue_procedures.c b/openair2/LAYER2/NR_MAC_UE/nr_ue_procedures.c
index 192fb25405f..220dc799e5c 100644
--- a/openair2/LAYER2/NR_MAC_UE/nr_ue_procedures.c
+++ b/openair2/LAYER2/NR_MAC_UE/nr_ue_procedures.c
@@ -1371,6 +1371,16 @@ int8_t nr_ue_process_dci(module_id_t module_id, int cc_id, uint8_t gNB_index, fr
 
 }
 
+int8_t nr_ue_process_csirs_measurements(module_id_t module_id,
+                                        frame_t frame,
+                                        int slot,
+                                        fapi_nr_csirs_measurements_t *csirs_measurements) {
+  LOG_D(NR_MAC,"(%d.%d) Received CSI-RS measurements\n", frame, slot);
+  NR_UE_MAC_INST_t *mac = get_mac_inst(module_id);
+  memcpy(&mac->csirs_measurements, csirs_measurements, sizeof(*csirs_measurements));
+  return 0;
+}
+
 void set_harq_status(NR_UE_MAC_INST_t *mac,
                      uint8_t pucch_id,
                      uint8_t harq_id,
diff --git a/openair2/NR_UE_PHY_INTERFACE/NR_IF_Module.c b/openair2/NR_UE_PHY_INTERFACE/NR_IF_Module.c
index 26c60fbb9bb..30aa9217658 100644
--- a/openair2/NR_UE_PHY_INTERFACE/NR_IF_Module.c
+++ b/openair2/NR_UE_PHY_INTERFACE/NR_IF_Module.c
@@ -1112,6 +1112,10 @@ int8_t handle_dlsch(nr_downlink_indication_t *dl_info, NR_UL_TIME_ALIGNMENT_t *u
   return 0;
 }
 
+int8_t handle_csirs_measurements(module_id_t module_id, frame_t frame, int slot, fapi_nr_csirs_measurements_t *csirs_measurements) {
+  return nr_ue_process_csirs_measurements(module_id, frame, slot, csirs_measurements);
+}
+
 void update_harq_status(module_id_t module_id, uint8_t harq_pid, uint8_t ack_nack) {
 
   NR_UE_MAC_INST_t *mac = get_mac_inst(module_id);
@@ -1236,7 +1240,7 @@ int nr_ue_dl_indication(nr_downlink_indication_t *dl_info, NR_UL_TIME_ALIGNMENT_
                                          (dl_info->rx_ind->rx_indication_body+i)->ssb_pdu.ssb_length,
                                          (dl_info->rx_ind->rx_indication_body+i)->ssb_pdu.ssb_start_subcarrier,
                                          (dl_info->rx_ind->rx_indication_body+i)->ssb_pdu.cell_id)) << FAPI_NR_RX_PDU_TYPE_SSB;
-	    free((dl_info->rx_ind->rx_indication_body+i)->ssb_pdu.pdu);
+            free((dl_info->rx_ind->rx_indication_body+i)->ssb_pdu.pdu);
             break;
           case FAPI_NR_RX_PDU_TYPE_SIB:
             ret_mask |= (handle_bcch_dlsch(dl_info->module_id,
@@ -1251,6 +1255,12 @@ int nr_ue_dl_indication(nr_downlink_indication_t *dl_info, NR_UL_TIME_ALIGNMENT_
           case FAPI_NR_RX_PDU_TYPE_RAR:
             ret_mask |= (handle_dlsch(dl_info, ul_time_alignment, i)) << FAPI_NR_RX_PDU_TYPE_RAR;
             break;
+          case FAPI_NR_CSIRS_IND:
+            ret_mask |= (handle_csirs_measurements(dl_info->module_id,
+                                                   dl_info->frame,
+                                                   dl_info->slot,
+                                                   &(dl_info->rx_ind->rx_indication_body+i)->csirs_measurements)) << FAPI_NR_CSIRS_IND;
+            break;
           default:
             break;
         }
-- 
GitLab