From 143aaaa1369b4b450dcfe3a31db5b0cf2c4a72bd Mon Sep 17 00:00:00 2001
From: Nick Ho <nick133371@gmail.com>
Date: Tue, 5 Mar 2019 14:43:24 +0800
Subject: [PATCH] 7 byte msg4 can be recieved by UE now

---
 openair1/PHY/LTE_TRANSPORT/dci_tools_NB_IoT.c |  4 +-
 .../SCHED/phy_procedures_lte_eNb_NB_IoT.c     | 21 +++++-----
 openair2/LAYER2/MAC/eNB_scheduler_RA_NB_IoT.c | 39 +++++++++++++++++--
 .../LAYER2/MAC/eNB_scheduler_ulsch_NB_IoT.c   | 10 +++--
 openair2/LAYER2/MAC/proto_NB_IoT.h            |  1 +
 5 files changed, 57 insertions(+), 18 deletions(-)

diff --git a/openair1/PHY/LTE_TRANSPORT/dci_tools_NB_IoT.c b/openair1/PHY/LTE_TRANSPORT/dci_tools_NB_IoT.c
index 0ec597b152..0f6bf79f4e 100644
--- a/openair1/PHY/LTE_TRANSPORT/dci_tools_NB_IoT.c
+++ b/openair1/PHY/LTE_TRANSPORT/dci_tools_NB_IoT.c
@@ -414,8 +414,8 @@ int generate_eNB_dlsch_params_from_dci_NB_IoT(PHY_VARS_eNB      *eNB,
 
     DCI_flip[0]        = DCI_tmp[2]*2;
     DCI_flip[1]        = DCI_tmp[1]*2;
-    DCI_flip[2]        = DCI_tmp[0]*2;
-    
+    //DCI_flip[2]        = DCI_tmp[0]*2;
+    DCI_flip[2]        = 4;
     ndlcch->pdu[ncce_index]    = DCI_flip;
 
     printf("DCI N1 PDU content:");
diff --git a/openair1/SCHED/phy_procedures_lte_eNb_NB_IoT.c b/openair1/SCHED/phy_procedures_lte_eNb_NB_IoT.c
index ac687daae4..88988b8c55 100644
--- a/openair1/SCHED/phy_procedures_lte_eNb_NB_IoT.c
+++ b/openair1/SCHED/phy_procedures_lte_eNb_NB_IoT.c
@@ -857,19 +857,20 @@ void generate_eNB_dlsch_params_NB_IoT(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t * proc,n
               DCI_Content->DCIN1.ndi            = dl_config_pdu->npdcch_pdu.npdcch_pdu_rel13.new_data_indicator;
               DCI_Content->DCIN1.HARQackRes     = dl_config_pdu->npdcch_pdu.npdcch_pdu_rel13.harq_ack_resource;
               DCI_Content->DCIN1.DCIRep         = dl_config_pdu->npdcch_pdu.npdcch_pdu_rel13.dci_subframe_repetition_number;
+              npdcch               =  eNB->npdcch_DCI;
 
-              eNB->npdcch[(uint8_t)UE_id] = (NB_IoT_eNB_NPDCCH_t *) malloc(sizeof(NB_IoT_eNB_NPDCCH_t));
+              //eNB->npdcch[(uint8_t)UE_id] = (NB_IoT_eNB_NPDCCH_t *) malloc(sizeof(NB_IoT_eNB_NPDCCH_t));
               //set the NPDCCH UE-specific structure  (calculate R)
-              npdcch=eNB->npdcch[(uint8_t)UE_id];
-              AssertFatal(npdcch != NULL, "NPDCCH structure for UE specific is not exist\n");
-              npdcch->repetition_idx[(uint8_t)UE_id] = 0; //this is used for the encoding mechanism to understand that is the first transmission
-
-              if(dl_config_pdu->npdcch_pdu.npdcch_pdu_rel13.aggregation_level) //whenever aggregation level is =1 we have only 1 repetition for USS
-              npdcch->repetition_number[(uint8_t)UE_id] = 1;
-              else
-              {
+              //npdcch=eNB->npdcch[(uint8_t)UE_id];
+              //AssertFatal(npdcch != NULL, "NPDCCH structure for UE specific is not exist\n");
+              //npdcch->repetition_idx[(uint8_t)UE_id] = 0; //this is used for the encoding mechanism to understand that is the first transmission
+
+              //if(dl_config_pdu->npdcch_pdu.npdcch_pdu_rel13.aggregation_level) //whenever aggregation level is =1 we have only 1 repetition for USS
+             // npdcch->repetition_number[(uint8_t)UE_id] = 1;
+              //else
+              //{
                 //see TS 36.213 Table 16.1-1
-              }
+              //}
 
 
               //fill the ndlsch structure for UE and packed the DCI PD
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_RA_NB_IoT.c b/openair2/LAYER2/MAC/eNB_scheduler_RA_NB_IoT.c
index de36415d61..f6a2a6502c 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler_RA_NB_IoT.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_RA_NB_IoT.c
@@ -766,7 +766,7 @@ void schedule_msg4_NB_IoT(eNB_MAC_INST_NB_IoT *mac_inst, int abs_subframe){
 			}
 			//	check msg4 resource
 			rep = dl_rep[msg4_nodes->ce_level];
-		    msg4_length = fill_msg4_NB_IoT(mac_inst,msg4_nodes);
+		    msg4_length = fill_msg4_NB_IoT_fixed(mac_inst,msg4_nodes);
 			I_mcs = get_I_mcs(msg4_nodes->ce_level);
 			I_tbs = I_mcs;
 			TBS = get_max_tbs(I_tbs);
@@ -865,8 +865,8 @@ void schedule_msg4_NB_IoT(eNB_MAC_INST_NB_IoT *mac_inst, int abs_subframe){
 
 				harq_result = (schedule_result_t *)malloc(sizeof(schedule_result_t));
 				harq_result->rnti = msg4_nodes->ue_rnti;
-			    harq_result->output_subframe = harq_subframe;
-			    harq_result->end_subframe = harq_end_subframe;
+			    harq_result->output_subframe = harq_subframe+3;
+			    harq_result->end_subframe = harq_end_subframe+3;
 			    harq_result->sdu_length = 0;
 			    harq_result->direction = UL;
 			    harq_result->rnti_type = 3;
@@ -1024,3 +1024,36 @@ int fill_msg4_NB_IoT(
 */
 	return length;
 }
+
+//  Generate MSG4 MAC PDU
+int fill_msg4_NB_IoT_fixed(
+	eNB_MAC_INST_NB_IoT *inst, 
+	RA_TEMPLATE_NB_IoT *ra_template
+)
+{
+	int length = 0;
+	uint8_t *dlsch_buffer = &ra_template->msg4_buffer[0];
+	// we have three subheader here: 1 for Control element of Contention resolution, 2 for CCCH
+	SCH_SUBHEADER_FIXED_NB_IoT *msg4_sub_1 = (SCH_SUBHEADER_FIXED_NB_IoT*)dlsch_buffer;
+	msg4_sub_1->R = 0;
+	msg4_sub_1->E = 1;
+	msg4_sub_1->LCID = UE_CONTENTION_RESOLUTION;
+	length+=1;
+
+	uint8_t *con_res = (uint8_t *)(dlsch_buffer+1);
+
+	con_res[0] = ra_template->ccch_buffer[0];
+	con_res[1] = ra_template->ccch_buffer[1];
+	con_res[2] = ra_template->ccch_buffer[2];
+	con_res[3] = ra_template->ccch_buffer[3];
+	con_res[4] = ra_template->ccch_buffer[4];
+	con_res[5] = ra_template->ccch_buffer[5];
+	length+=6;
+/*
+	printf("MSG4 PDU = ");
+	for(int i=0; i<length;i++)
+		printf("%02x ",dlsch_buffer[i]);
+	printf("\n");
+*/
+	return length;
+}
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_ulsch_NB_IoT.c b/openair2/LAYER2/MAC/eNB_scheduler_ulsch_NB_IoT.c
index 34bfeb1d64..b44512502e 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler_ulsch_NB_IoT.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_ulsch_NB_IoT.c
@@ -221,8 +221,12 @@ void rx_sdu_NB_IoT(module_id_t module_id, int CC_id, frame_t frame, sub_frame_t
   //mac_NB_IoT_t *mac_inst;
   UE_TEMPLATE_NB_IoT *UE_info;
   uint8_t* msg4_rrc_pdu = NULL;
-
   LOG_D(MAC,"RX_SDU_IN\n");
+
+  uint8_t* first_6 = (uint8_t*) malloc(6*sizeof(uint8_t));
+
+  for(int a = 0; a<6;a++)
+    first_6[a]=sdu[a];
   //mac_inst = get_mac_inst(module_id);
 
   // note: if lcid < 25 this is sdu, otherwise this is CE
@@ -271,8 +275,8 @@ void rx_sdu_NB_IoT(module_id_t module_id, int CC_id, frame_t frame, sub_frame_t
                 rx_lengths[i]-=1;
                 LOG_D(MAC,"rx_lengths : %d\n", rx_lengths[i]);
                 msg4_rrc_pdu = mac_rrc_msg3_ind_NB_IoT(payload_ptr,rnti,rx_lengths[i]);
-                receive_msg3_NB_IoT(mac_inst,rnti,PHR,DVI_index,payload_ptr,msg4_rrc_pdu);
-                LOG_D(MAC,"recieve msg3 Successfully at MAC!\n");
+                receive_msg3_NB_IoT(mac_inst,rnti,PHR,DVI_index,first_6,msg4_rrc_pdu);
+                LOG_I(MAC,"recieve msg3 Successfully at MAC!\n");
                 //NB_IoT_mac_rrc_data_ind(payload_ptr,mac_inst,rnti);
                 //NB_IoT_receive_msg3(mac_inst,rnti,PHR,ul_total_buffer);
           break;
diff --git a/openair2/LAYER2/MAC/proto_NB_IoT.h b/openair2/LAYER2/MAC/proto_NB_IoT.h
index 021d3f22c4..d608bed6a2 100644
--- a/openair2/LAYER2/MAC/proto_NB_IoT.h
+++ b/openair2/LAYER2/MAC/proto_NB_IoT.h
@@ -127,6 +127,7 @@ void schedule_msg4_NB_IoT(eNB_MAC_INST_NB_IoT *mac_inst, int abs_subframe);
 void fill_rar_NB_IoT(eNB_MAC_INST_NB_IoT *inst, RA_TEMPLATE_NB_IoT *ra_template, uint8_t msg3_schedule_delay, uint8_t msg3_rep, sched_temp_UL_NB_IoT_t *schedule_template);
 void receive_msg4_ack_NB_IoT(eNB_MAC_INST_NB_IoT *mac_inst, rnti_t rnti);
 int fill_msg4_NB_IoT(eNB_MAC_INST_NB_IoT *inst, RA_TEMPLATE_NB_IoT *ra_template);
+int fill_msg4_NB_IoT_fixed(eNB_MAC_INST_NB_IoT *inst, RA_TEMPLATE_NB_IoT *ra_template);
 
 //USS
 void schedule_uss_NB_IoT(module_id_t module_id, eNB_MAC_INST_NB_IoT *mac_inst, uint32_t subframe, uint32_t frame, uint32_t hypersfn, int index_ss);
-- 
2.26.2