From 05f4c15e9a58dc2918ac599e954f3b086d2b814c Mon Sep 17 00:00:00 2001
From: "masayuki.harada" <masayuki.harada@jp.fujitsu.com>
Date: Wed, 7 Apr 2021 20:31:39 +0900
Subject: [PATCH] Fix Msg5 procedure in sa mode.

---
 executables/nr-softmodem.c                       |  2 +-
 executables/nr-ue.c                              |  2 +-
 executables/nr-uesoftmodem.c                     |  2 +-
 openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c      |  4 ++--
 openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c |  2 +-
 openair2/LAYER2/nr_pdcp/nr_pdcp_oai_api.c        | 10 +++-------
 openair2/LAYER2/nr_rlc/nr_rlc_oai_api.c          |  2 +-
 7 files changed, 10 insertions(+), 14 deletions(-)

diff --git a/executables/nr-softmodem.c b/executables/nr-softmodem.c
index a26bf5c2e8..16a858b5bb 100644
--- a/executables/nr-softmodem.c
+++ b/executables/nr-softmodem.c
@@ -344,7 +344,7 @@ int create_gNB_tasks(uint32_t gnb_nb) {
   }
 
 
-  if (AMF_MODE_ENABLED && (get_softmodem_params()->phy_test==0 && get_softmodem_params()->do_ra==0 && get_softmodem_params()->sa==0)) {
+  if (AMF_MODE_ENABLED && (get_softmodem_params()->phy_test==0 && get_softmodem_params()->do_ra==0)) {
     if (gnb_nb > 0) {
       /*
       if (itti_create_task (TASK_SCTP, sctp_eNB_task, NULL) < 0) {
diff --git a/executables/nr-ue.c b/executables/nr-ue.c
index 47ddd7896e..9b0cae7c46 100644
--- a/executables/nr-ue.c
+++ b/executables/nr-ue.c
@@ -327,7 +327,7 @@ void processSlotRX( PHY_VARS_NR_UE *UE, UE_nr_rxtx_proc_t *proc) {
     LOG_D(PHY, "In %s: slot %d, time %lu\n", __FUNCTION__, proc->nr_slot_rx, (rdtsc()-a)/3500);
 #endif
 
-    if(IS_SOFTMODEM_NOS1){
+    if(IS_SOFTMODEM_NOS1 || get_softmodem_params()->sa ){
       NR_UE_MAC_INST_t *mac = get_mac_inst(0);
       protocol_ctxt_t ctxt;
       PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, UE->Mod_id, ENB_FLAG_NO, mac->crnti, proc->frame_rx, proc->nr_slot_rx, 0);
diff --git a/executables/nr-uesoftmodem.c b/executables/nr-uesoftmodem.c
index de055b9e23..274307c369 100644
--- a/executables/nr-uesoftmodem.c
+++ b/executables/nr-uesoftmodem.c
@@ -514,7 +514,7 @@ int main( int argc, char **argv ) {
   RC.nrrrc = (gNB_RRC_INST **)malloc(1*sizeof(gNB_RRC_INST *));
   RC.nrrrc[0] = (gNB_RRC_INST*)malloc(sizeof(gNB_RRC_INST));
   RC.nrrrc[0]->node_type = ngran_gNB;
-  nr_rrc_ue_generate_RRCSetupRequest(ctxt_pP.module_id, 0); //TODO need to set ctxt_pP
+  //nr_rrc_ue_generate_RRCSetupRequest(ctxt_pP.module_id, 0); //TODO need to set ctxt_pP
   
   if (create_tasks_nrue(1) < 0) {
     printf("cannot create ITTI tasks\n");
diff --git a/openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c b/openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c
index bc15353e9a..63eeccd180 100644
--- a/openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c
+++ b/openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c
@@ -896,7 +896,7 @@ NR_UE_L2_STATE_t nr_ue_scheduler(nr_downlink_indication_t *dl_info, nr_uplink_in
               LOG_D(NR_MAC,"(%i): 0x%x\n",k,mac->ulsch_pdu.payload[k]);
             }
           } else {
-            if (IS_SOFTMODEM_NOS1 && (mac->UL_ndi[ulcfg_pdu->pusch_config_pdu.pusch_data.harq_process_id] != ulcfg_pdu->pusch_config_pdu.pusch_data.new_data_indicator)){
+            if ((IS_SOFTMODEM_NOS1 || get_softmodem_params()->sa) && (mac->UL_ndi[ulcfg_pdu->pusch_config_pdu.pusch_data.harq_process_id] != ulcfg_pdu->pusch_config_pdu.pusch_data.new_data_indicator)){
               // Getting IP traffic to be transmitted
               data_existing = nr_ue_get_sdu(mod_id,
                                             cc_id,
@@ -910,7 +910,7 @@ NR_UE_L2_STATE_t nr_ue_scheduler(nr_downlink_indication_t *dl_info, nr_uplink_in
 
             mac->UL_ndi[ulcfg_pdu->pusch_config_pdu.pusch_data.harq_process_id] = ulcfg_pdu->pusch_config_pdu.pusch_data.new_data_indicator;
             //Random traffic to be transmitted if there is no IP traffic available for this Tx opportunity
-            if (!IS_SOFTMODEM_NOS1 || !data_existing) {
+            if (!(IS_SOFTMODEM_NOS1 || get_softmodem_params()->sa) || !data_existing) {
               //Use zeros for the header bytes in noS1 mode, in order to make sure that the LCID is not valid
               //and block this traffic from being forwarded to the upper layers at the gNB
               LOG_D(PHY, "In %s: Random data to be transmitted: TBS_bytes %d \n", __FUNCTION__, TBS_bytes);
diff --git a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c
index b584661757..73b069ddcf 100644
--- a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c
+++ b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c
@@ -738,7 +738,7 @@ void pf_ul(module_id_t module_id,
 
     /* Calculate TBS from MCS */
     NR_sched_pusch_t *sched_pusch = &sched_ctrl->sched_pusch;
-    const int mcs = 9;
+    const int mcs = 4;
     sched_pusch->mcs = mcs;
     sched_pusch->R = nr_get_code_rate_ul(mcs, ps->mcs_table);
     sched_pusch->Qm = nr_get_Qm_ul(mcs, ps->mcs_table);
diff --git a/openair2/LAYER2/nr_pdcp/nr_pdcp_oai_api.c b/openair2/LAYER2/nr_pdcp/nr_pdcp_oai_api.c
index e89280edaf..ada69d0f89 100644
--- a/openair2/LAYER2/nr_pdcp/nr_pdcp_oai_api.c
+++ b/openair2/LAYER2/nr_pdcp/nr_pdcp_oai_api.c
@@ -672,13 +672,8 @@ rb_found:
   ctxt.brOption = 0;
   ctxt.rnti = ue->rnti;
 
+  nr_rrc_data_ind( &ctxt, srb_id, size, buf);
 
-  if (ccch_or_dcch == 0) {
-    nr_rrc_data_ind_ccch( &ctxt, srb_id, size, buf);
-    ccch_or_dcch = 1;
-  } else {
-    nr_rrc_data_ind( &ctxt, srb_id, size, buf);
-  }
   return;
 }
 
@@ -1045,7 +1040,7 @@ boolean_t nr_rrc_pdcp_config_asn1_req(
       //kUPenc != NULL ||
       pmch_InfoList_r9 != NULL /*||
       defaultDRB != NULL */) {
-    TODO;
+    //TODO;
   }
 
   if (srb2add_list != NULL) {
@@ -1302,6 +1297,7 @@ static boolean_t pdcp_data_req_srb(
   if (rb == NULL) {
     LOG_E(PDCP, "%s:%d:%s: no SRB found (rnti %d, rb_id %ld)\n",
           __FILE__, __LINE__, __FUNCTION__, rnti, rb_id);
+    nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
     return 0;
   }
 
diff --git a/openair2/LAYER2/nr_rlc/nr_rlc_oai_api.c b/openair2/LAYER2/nr_rlc/nr_rlc_oai_api.c
index 422513c66a..1cb90cc339 100644
--- a/openair2/LAYER2/nr_rlc/nr_rlc_oai_api.c
+++ b/openair2/LAYER2/nr_rlc/nr_rlc_oai_api.c
@@ -666,7 +666,7 @@ static void add_srb(int rnti, struct NR_SRB_ToAddMod *s, NR_RLC_BearerConfig_t *
   if (channel_id != srb_id) {
     LOG_E(RLC, "%s:%d:%s: todo, remove this limitation\n",
           __FILE__, __LINE__, __FUNCTION__);
-    exit(1);
+    //exit(1);
   }
 
   logical_channel_group = *l->ul_SpecificParameters->logicalChannelGroup;
-- 
2.26.2