diff --git a/openair2/LAYER2/MAC/config.c b/openair2/LAYER2/MAC/config.c
index 6d5ebae7d495793cfe261f6ff0a71c0db2c8c2e1..764c762f6a7da8bc2ca5f47521063a72040a6dfd 100644
--- a/openair2/LAYER2/MAC/config.c
+++ b/openair2/LAYER2/MAC/config.c
@@ -60,37 +60,37 @@
 extern RAN_CONTEXT_t RC;
 
 /* sec 5.9, 36.321: MAC Reset Procedure */
-void ue_mac_reset(module_id_t module_idP,uint8_t eNB_index)
+void ue_mac_reset(module_id_t module_idP, uint8_t eNB_index)
 {
 
-  //Resetting Bj
-  UE_mac_inst[module_idP].scheduling_info.Bj[0] = 0;
-  UE_mac_inst[module_idP].scheduling_info.Bj[1] = 0;
-  UE_mac_inst[module_idP].scheduling_info.Bj[2] = 0;
+    //Resetting Bj
+    UE_mac_inst[module_idP].scheduling_info.Bj[0] = 0;
+    UE_mac_inst[module_idP].scheduling_info.Bj[1] = 0;
+    UE_mac_inst[module_idP].scheduling_info.Bj[2] = 0;
 
-  //Stopping all timers
+    //Stopping all timers
 
-  //timeAlignmentTimer expires
+    //timeAlignmentTimer expires
 
-  // PHY changes for UE MAC reset
-  phy_reset_ue(module_idP,0,eNB_index);
+    // PHY changes for UE MAC reset
+    phy_reset_ue(module_idP, 0, eNB_index);
 
-  // notify RRC to relase PUCCH/SRS
-  // cancel all pending SRs
-  UE_mac_inst[module_idP].scheduling_info.SR_pending=0;
-  UE_mac_inst[module_idP].scheduling_info.SR_COUNTER=0;
+    // notify RRC to relase PUCCH/SRS
+    // cancel all pending SRs
+    UE_mac_inst[module_idP].scheduling_info.SR_pending = 0;
+    UE_mac_inst[module_idP].scheduling_info.SR_COUNTER = 0;
 
 //Set BSR Trigger Bmp and remove timer flags
-  UE_mac_inst[module_idP].BSR_reporting_active = BSR_TRIGGER_NONE;
+    UE_mac_inst[module_idP].BSR_reporting_active = BSR_TRIGGER_NONE;
 
-  // stop ongoing RACH procedure
+    // stop ongoing RACH procedure
 
-  // discard explicitly signaled ra_PreambleIndex and ra_RACH_MaskIndex, if any
-  UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = 0; // check!
-  UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
+    // discard explicitly signaled ra_PreambleIndex and ra_RACH_MaskIndex, if any
+    UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex = 0;	// check!
+    UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
 
 
-  ue_init_mac(module_idP); //This will hopefully do the rest of the MAC reset procedure
+    ue_init_mac(module_idP);	//This will hopefully do the rest of the MAC reset procedure
 
 }
 
@@ -115,912 +115,1236 @@ typedef struct band_info_s {
 
 
 static const eutra_bandentry_t eutra_bandtable[] = {
-  { 1, 19200, 19800, 21100, 21700, 0},
-  { 2, 18500, 19100, 19300, 19900, 6000},
-  { 3, 17100, 17850, 18050, 18800, 12000},
-  { 4, 17100, 17550, 21100, 21550, 19500},
-  { 5,  8240,  8490,  8690,  8940, 24000},
-  { 6,  8300,  8400,  8750,  8850, 26500},
-  { 7, 25000, 25700, 26200, 26900, 27500},
-  { 8,  8800, 9150 ,  9250,  9600, 34500},
-  { 9, 17499, 17849, 18449, 18799, 38000},
-  {10, 17100, 17700, 21100, 21700, 41500},
-  {11, 14279, 14529, 14759, 15009, 47500},
-  {12,  6980,  7160,  7280,  7460, 50100},
-  {13,  7770,  7870,  7460,  7560, 51800},
-  {14,  7880,  7980,  7580,  7680, 52800},
-  {17,  7040,  7160,  7340,  7460, 57300},
-  {18,  8150,  9650,  8600, 10100, 58500},
-  {19,  8300,  8450,  8750,  8900, 60000},
-  {20,  8320,  8620,  7910,  8210, 61500},
-  {21, 14479, 14629, 14959, 15109, 64500},
-  {22, 34100, 34900, 35100, 35900, 66000},
-  {23, 20000, 20200, 21800, 22000, 75000},
-  {24, 16126, 16605, 15250, 15590, 77000},
-  {25, 18500, 19150, 19300, 19950, 80400},
-  {26, 8140 ,  8490,  8590,  8940, 86900},
-  {27, 8070 ,  8240,  8520,  8690, 90400},
-  {28, 7030 ,  7580,  7580,  8130, 92100},
-  {29, 0    ,  0   ,  7170,  7280, 96600},
-  {30, 23050, 23250, 23500, 23600, 97700},
-  {31, 45250, 34900, 46250, 35900, 98700},
-  {32, 0    , 0    , 14520, 14960, 99200},
-  {33, 19000, 19200, 19000, 19200, 36000},
-  {34, 20100, 20250, 20100, 20250, 36200},
-  {35, 18500, 19100, 18500, 19100, 36350},
-  {36, 19300, 19900, 19300, 19900, 36950},
-  {37, 19100, 19300, 19100, 19300, 37550},
-  {38, 25700, 26200, 25700, 26300, 37750},
-  {39, 18800, 19200, 18800, 19200, 38250},
-  {40, 23000, 24000, 23000, 24000, 38650},
-  {41, 24960, 26900, 24960, 26900, 39650},
-  {42, 34000, 36000, 34000, 36000, 41590},
-  {43, 36000, 38000, 36000, 38000, 43590},
-  {44, 7030 ,  8030,  7030,  8030, 45590},
-  {45, 14470, 14670, 14470, 14670, 46590},
-  {46, 51500, 59250, 51500, 59250, 46790},
-  {65, 19200, 20100, 21100, 22000, 65536},
-  {66, 17100, 18000, 21100, 22000, 66436},
-  {67, 0    , 0    ,  7380,  7580, 67336},
-  {68, 6980 , 7280 ,  7530,  7830, 67536}};
-
-uint32_t to_earfcn(int eutra_bandP,uint32_t dl_CarrierFreq,uint32_t bw) {
-
-  uint32_t dl_CarrierFreq_by_100k = dl_CarrierFreq/100000;
-  int bw_by_100                   = bw/100;
-
-  int i;
-
-  AssertFatal(eutra_bandP < 69,"eutra_band %d > 68\n",eutra_bandP);
-  for (i=0;i<69 && eutra_bandtable[i].band!=eutra_bandP;i++);
-
-  AssertFatal(dl_CarrierFreq_by_100k>=eutra_bandtable[i].dl_min,
-	      "Band %d, bw %u : DL carrier frequency %u Hz < %u\n",
-	      eutra_bandP,bw,dl_CarrierFreq,eutra_bandtable[i].dl_min);
-  AssertFatal(dl_CarrierFreq_by_100k<=(eutra_bandtable[i].dl_max-bw_by_100),
-	      "Band %d, bw %u: DL carrier frequency %u Hz > %d\n",
-	      eutra_bandP,bw,dl_CarrierFreq,eutra_bandtable[i].dl_max-bw_by_100);
-
-
-  return(dl_CarrierFreq_by_100k - eutra_bandtable[i].dl_min + (eutra_bandtable[i].N_OFFs_DL/10));
+    {1, 19200, 19800, 21100, 21700, 0},
+    {2, 18500, 19100, 19300, 19900, 6000},
+    {3, 17100, 17850, 18050, 18800, 12000},
+    {4, 17100, 17550, 21100, 21550, 19500},
+    {5, 8240, 8490, 8690, 8940, 24000},
+    {6, 8300, 8400, 8750, 8850, 26500},
+    {7, 25000, 25700, 26200, 26900, 27500},
+    {8, 8800, 9150, 9250, 9600, 34500},
+    {9, 17499, 17849, 18449, 18799, 38000},
+    {10, 17100, 17700, 21100, 21700, 41500},
+    {11, 14279, 14529, 14759, 15009, 47500},
+    {12, 6980, 7160, 7280, 7460, 50100},
+    {13, 7770, 7870, 7460, 7560, 51800},
+    {14, 7880, 7980, 7580, 7680, 52800},
+    {17, 7040, 7160, 7340, 7460, 57300},
+    {18, 8150, 9650, 8600, 10100, 58500},
+    {19, 8300, 8450, 8750, 8900, 60000},
+    {20, 8320, 8620, 7910, 8210, 61500},
+    {21, 14479, 14629, 14959, 15109, 64500},
+    {22, 34100, 34900, 35100, 35900, 66000},
+    {23, 20000, 20200, 21800, 22000, 75000},
+    {24, 16126, 16605, 15250, 15590, 77000},
+    {25, 18500, 19150, 19300, 19950, 80400},
+    {26, 8140, 8490, 8590, 8940, 86900},
+    {27, 8070, 8240, 8520, 8690, 90400},
+    {28, 7030, 7580, 7580, 8130, 92100},
+    {29, 0, 0, 7170, 7280, 96600},
+    {30, 23050, 23250, 23500, 23600, 97700},
+    {31, 45250, 34900, 46250, 35900, 98700},
+    {32, 0, 0, 14520, 14960, 99200},
+    {33, 19000, 19200, 19000, 19200, 36000},
+    {34, 20100, 20250, 20100, 20250, 36200},
+    {35, 18500, 19100, 18500, 19100, 36350},
+    {36, 19300, 19900, 19300, 19900, 36950},
+    {37, 19100, 19300, 19100, 19300, 37550},
+    {38, 25700, 26200, 25700, 26300, 37750},
+    {39, 18800, 19200, 18800, 19200, 38250},
+    {40, 23000, 24000, 23000, 24000, 38650},
+    {41, 24960, 26900, 24960, 26900, 39650},
+    {42, 34000, 36000, 34000, 36000, 41590},
+    {43, 36000, 38000, 36000, 38000, 43590},
+    {44, 7030, 8030, 7030, 8030, 45590},
+    {45, 14470, 14670, 14470, 14670, 46590},
+    {46, 51500, 59250, 51500, 59250, 46790},
+    {65, 19200, 20100, 21100, 22000, 65536},
+    {66, 17100, 18000, 21100, 22000, 66436},
+    {67, 0, 0, 7380, 7580, 67336},
+    {68, 6980, 7280, 7530, 7830, 67536}
+};
+
+uint32_t to_earfcn(int eutra_bandP, uint32_t dl_CarrierFreq, uint32_t bw)
+{
+
+    uint32_t dl_CarrierFreq_by_100k = dl_CarrierFreq / 100000;
+    int bw_by_100 = bw / 100;
+
+    int i;
+
+    AssertFatal(eutra_bandP < 69, "eutra_band %d > 68\n", eutra_bandP);
+    for (i = 0; i < 69 && eutra_bandtable[i].band != eutra_bandP; i++);
+
+    AssertFatal(dl_CarrierFreq_by_100k >= eutra_bandtable[i].dl_min,
+		"Band %d, bw %u : DL carrier frequency %u Hz < %u\n",
+		eutra_bandP, bw, dl_CarrierFreq,
+		eutra_bandtable[i].dl_min);
+    AssertFatal(dl_CarrierFreq_by_100k <=
+		(eutra_bandtable[i].dl_max - bw_by_100),
+		"Band %d, bw %u: DL carrier frequency %u Hz > %d\n",
+		eutra_bandP, bw, dl_CarrierFreq,
+		eutra_bandtable[i].dl_max - bw_by_100);
+
+
+    return (dl_CarrierFreq_by_100k - eutra_bandtable[i].dl_min +
+	    (eutra_bandtable[i].N_OFFs_DL / 10));
 }
 
-uint32_t from_earfcn(int eutra_bandP,uint32_t dl_earfcn) {
+uint32_t from_earfcn(int eutra_bandP, uint32_t dl_earfcn)
+{
 
-  int i;
+    int i;
 
-  AssertFatal(eutra_bandP < 69,"eutra_band %d > 68\n",eutra_bandP);
-  for (i=0;i<69 && eutra_bandtable[i].band!=eutra_bandP;i++);
+    AssertFatal(eutra_bandP < 69, "eutra_band %d > 68\n", eutra_bandP);
+    for (i = 0; i < 69 && eutra_bandtable[i].band != eutra_bandP; i++);
 
-  return(eutra_bandtable[i].dl_min + (dl_earfcn-(eutra_bandtable[i].N_OFFs_DL/10)))*100000;
+    return (eutra_bandtable[i].dl_min +
+	    (dl_earfcn - (eutra_bandtable[i].N_OFFs_DL / 10))) * 100000;
 }
 
 
-int32_t get_uldl_offset(int eutra_bandP) {
-  int i;
+int32_t get_uldl_offset(int eutra_bandP)
+{
+    int i;
 
-  for (i=0;i<69 && eutra_bandtable[i].band!=eutra_bandP;i++);
-  return(eutra_bandtable[i].dl_min - eutra_bandtable[i].ul_min);
+    for (i = 0; i < 69 && eutra_bandtable[i].band != eutra_bandP; i++);
+    return (eutra_bandtable[i].dl_min - eutra_bandtable[i].ul_min);
 }
 
-uint32_t bw_table[6] = {6*180,15*180,25*180,50*180,75*180,100*180};
-
-void config_mib(int                 Mod_idP,
-		int                 CC_idP,
-		int                 eutra_bandP,  
-		int                 dl_BandwidthP,
-		PHICH_Config_t      *phich_configP,
-		int                 Nid_cellP,
-		int                 NcpP,
-		int                 p_eNBP,
-		uint32_t            dl_CarrierFreqP,
-		uint32_t            ul_CarrierFreqP,
-		uint32_t            pbch_repetitionP) {
-
-  nfapi_config_request_t *cfg = &RC.mac[Mod_idP]->config[CC_idP];
-		
-  cfg->subframe_config.pcfich_power_offset.value   = 6000;  // 0dB
-  cfg->subframe_config.dl_cyclic_prefix_type.value = NcpP;
-  cfg->subframe_config.ul_cyclic_prefix_type.value = NcpP;
- 
-  LOG_I(MAC,"Ncp %d,p_eNB %d\n",NcpP,p_eNBP);
-
-  cfg->rf_config.dl_channel_bandwidth.value        = dl_BandwidthP;
-  cfg->rf_config.ul_channel_bandwidth.value        = dl_BandwidthP;
-  cfg->rf_config.tx_antenna_ports.value            = p_eNBP;
-  cfg->rf_config.rx_antenna_ports.value            = 2;
-
-  cfg->nfapi_config.earfcn.value                   = to_earfcn(eutra_bandP,dl_CarrierFreqP,bw_table[dl_BandwidthP]/100);
-  cfg->nfapi_config.rf_bands.number_rf_bands       = 1;
-  cfg->nfapi_config.rf_bands.rf_band[0]            = eutra_bandP;  
-  cfg->phich_config.phich_resource.value           = phich_configP->phich_Resource;
-  cfg->phich_config.phich_duration.value           = phich_configP->phich_Duration;
-  cfg->phich_config.phich_power_offset.value       = 6000;  // 0dB
-
-  cfg->sch_config.primary_synchronization_signal_epre_eprers.value   = 6000; // 0dB
-  cfg->sch_config.secondary_synchronization_signal_epre_eprers.value = 6000; // 0dB
-  cfg->sch_config.physical_cell_id.value                             = Nid_cellP;
+uint32_t bw_table[6] =
+    { 6 * 180, 15 * 180, 25 * 180, 50 * 180, 75 * 180, 100 * 180 };
+
+void
+config_mib(int Mod_idP,
+	   int CC_idP,
+	   int eutra_bandP,
+	   int dl_BandwidthP,
+	   PHICH_Config_t * phich_configP,
+	   int Nid_cellP,
+	   int NcpP,
+	   int p_eNBP,
+	   uint32_t dl_CarrierFreqP,
+	   uint32_t ul_CarrierFreqP, uint32_t pbch_repetitionP)
+{
+
+    nfapi_config_request_t *cfg = &RC.mac[Mod_idP]->config[CC_idP];
+
+    cfg->subframe_config.pcfich_power_offset.value = 6000;	// 0dB
+    cfg->subframe_config.dl_cyclic_prefix_type.value = NcpP;
+    cfg->subframe_config.ul_cyclic_prefix_type.value = NcpP;
+
+    LOG_I(MAC, "Ncp %d,p_eNB %d\n", NcpP, p_eNBP);
+
+    cfg->rf_config.dl_channel_bandwidth.value = dl_BandwidthP;
+    cfg->rf_config.ul_channel_bandwidth.value = dl_BandwidthP;
+    cfg->rf_config.tx_antenna_ports.value = p_eNBP;
+    cfg->rf_config.rx_antenna_ports.value = 2;
+
+    cfg->nfapi_config.earfcn.value =
+	to_earfcn(eutra_bandP, dl_CarrierFreqP,
+		  bw_table[dl_BandwidthP] / 100);
+    cfg->nfapi_config.rf_bands.number_rf_bands = 1;
+    cfg->nfapi_config.rf_bands.rf_band[0] = eutra_bandP;
+    cfg->phich_config.phich_resource.value = phich_configP->phich_Resource;
+    cfg->phich_config.phich_duration.value = phich_configP->phich_Duration;
+    cfg->phich_config.phich_power_offset.value = 6000;	// 0dB
+
+    cfg->sch_config.primary_synchronization_signal_epre_eprers.value = 6000;	// 0dB
+    cfg->sch_config.secondary_synchronization_signal_epre_eprers.value = 6000;	// 0dB
+    cfg->sch_config.physical_cell_id.value = Nid_cellP;
 
 #ifdef Rel14
-  cfg->emtc_config.pbch_repetitions_enable_r13.value                 = pbch_repetitionP;
-#endif  
+    cfg->emtc_config.pbch_repetitions_enable_r13.value = pbch_repetitionP;
+#endif
 }
 
-void config_sib1(int Mod_idP,
-		 int CC_idP,
-		 TDD_Config_t *tdd_ConfigP) {
+void config_sib1(int Mod_idP, int CC_idP, TDD_Config_t * tdd_ConfigP)
+{
 
-  nfapi_config_request_t *cfg = &RC.mac[Mod_idP]->config[CC_idP];
+    nfapi_config_request_t *cfg = &RC.mac[Mod_idP]->config[CC_idP];
+
+    if (tdd_ConfigP) {		//TDD
+	cfg->subframe_config.duplex_mode.value = 0;
+	cfg->tdd_frame_structure_config.subframe_assignment.value =
+	    tdd_ConfigP->subframeAssignment;
+	cfg->tdd_frame_structure_config.special_subframe_patterns.value =
+	    tdd_ConfigP->specialSubframePatterns;
+    } else {			// FDD
+	cfg->subframe_config.duplex_mode.value = 1;
+	// Note no half-duplex here
+    }
 
-  if (tdd_ConfigP)   { //TDD
-    cfg->subframe_config.duplex_mode.value                          = 0;
-    cfg->tdd_frame_structure_config.subframe_assignment.value       = tdd_ConfigP->subframeAssignment;
-    cfg->tdd_frame_structure_config.special_subframe_patterns.value = tdd_ConfigP->specialSubframePatterns;
-  }
-  else { // FDD
-    cfg->subframe_config.duplex_mode.value                          = 1;
-    // Note no half-duplex here
-  }
 
-  
 }
 
-int power_off_dB[6] = {78,118,140,170,188,200};
+int power_off_dB[6] = { 78, 118, 140, 170, 188, 200 };
 
-void config_sib2(int Mod_idP, 
-		 int CC_idP, 
-		 RadioResourceConfigCommonSIB_t   *radioResourceConfigCommonP,
+void
+config_sib2(int Mod_idP,
+	    int CC_idP,
+	    RadioResourceConfigCommonSIB_t * radioResourceConfigCommonP,
 #ifdef Rel14
-		 RadioResourceConfigCommonSIB_t   *radioResourceConfigCommon_BRP,
+	    RadioResourceConfigCommonSIB_t * radioResourceConfigCommon_BRP,
 #endif
-		 ARFCN_ValueEUTRA_t *ul_CArrierFreqP,
-		 long *ul_BandwidthP,
-		 AdditionalSpectrumEmission_t *additionalSpectrumEmissionP,
-		 struct MBSFN_SubframeConfigList  *mbsfn_SubframeConfigListP) {
-
-  nfapi_config_request_t *cfg = &RC.mac[Mod_idP]->config[CC_idP];
-
-  cfg->subframe_config.pb.value               = radioResourceConfigCommonP->pdsch_ConfigCommon.p_b;
-  cfg->rf_config.reference_signal_power.value = radioResourceConfigCommonP->pdsch_ConfigCommon.referenceSignalPower;
-  cfg->nfapi_config.max_transmit_power.value  = cfg->rf_config.reference_signal_power.value + power_off_dB[cfg->rf_config.dl_channel_bandwidth.value];
-
-  cfg->prach_config.configuration_index.value                 = radioResourceConfigCommonP->prach_Config.prach_ConfigInfo.prach_ConfigIndex;
-  cfg->prach_config.root_sequence_index.value                 = radioResourceConfigCommonP->prach_Config.rootSequenceIndex;
-  cfg->prach_config.zero_correlation_zone_configuration.value = radioResourceConfigCommonP->prach_Config.prach_ConfigInfo.zeroCorrelationZoneConfig;
-  cfg->prach_config.high_speed_flag.value                     = radioResourceConfigCommonP->prach_Config.prach_ConfigInfo.highSpeedFlag;
-  cfg->prach_config.frequency_offset.value                    = radioResourceConfigCommonP->prach_Config.prach_ConfigInfo.prach_FreqOffset;
-
-  cfg->pusch_config.hopping_mode.value                        = radioResourceConfigCommonP->pusch_ConfigCommon.pusch_ConfigBasic.hoppingMode;
-  cfg->pusch_config.number_of_subbands.value                  = radioResourceConfigCommonP->pusch_ConfigCommon.pusch_ConfigBasic.n_SB;
-  cfg->pusch_config.hopping_offset.value                      = radioResourceConfigCommonP->pusch_ConfigCommon.pusch_ConfigBasic.pusch_HoppingOffset;
-
-
-		
-  cfg->pucch_config.delta_pucch_shift.value                         = radioResourceConfigCommonP->pucch_ConfigCommon.deltaPUCCH_Shift;
-  cfg->pucch_config.n_cqi_rb.value                                  = radioResourceConfigCommonP->pucch_ConfigCommon.nRB_CQI;
-  cfg->pucch_config.n_an_cs.value                                   = radioResourceConfigCommonP->pucch_ConfigCommon.nCS_AN;
-  cfg->pucch_config.n1_pucch_an.value                               = radioResourceConfigCommonP->pucch_ConfigCommon.n1PUCCH_AN;
-
-  if (radioResourceConfigCommonP->pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.groupHoppingEnabled == true)
-    cfg->uplink_reference_signal_config.uplink_rs_hopping.value     = 1;
-  else if (radioResourceConfigCommonP->pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.sequenceHoppingEnabled == true)
-    cfg->uplink_reference_signal_config.uplink_rs_hopping.value     = 2;
-  else // No hopping
-    cfg->uplink_reference_signal_config.uplink_rs_hopping.value     = 0;
-
-  cfg->uplink_reference_signal_config.group_assignment.value        = radioResourceConfigCommonP->pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.groupAssignmentPUSCH;
-  cfg->uplink_reference_signal_config.cyclic_shift_1_for_drms.value = radioResourceConfigCommonP->pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.cyclicShift;
-
-  // how to enable/disable SRS?
-  if (radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.present==SoundingRS_UL_ConfigCommon_PR_setup) {
-    cfg->srs_config.bandwidth_configuration.value                       = radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.choice.setup.srs_BandwidthConfig;
-    cfg->srs_config.srs_subframe_configuration.value                    = radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.choice.setup.srs_SubframeConfig;
-    cfg->srs_config.srs_acknack_srs_simultaneous_transmission.value     = radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.choice.setup.ackNackSRS_SimultaneousTransmission;
-
-    if (radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.choice.setup.srs_MaxUpPts)
-       cfg->srs_config.max_up_pts.value                                 = 1;
-    else
-       cfg->srs_config.max_up_pts.value                                 = 0;
-  }
-
-#ifdef Rel14
-  if (RC.mac[Mod_idP]->common_channels[CC_idP].mib->message.schedulingInfoSIB1_BR_r13>0) {
-    AssertFatal(radioResourceConfigCommon_BRP!=NULL,"radioResource rou is missing\n");
-    AssertFatal(radioResourceConfigCommon_BRP->ext4!=NULL,"ext4 is missing\n");
-    cfg->emtc_config.prach_catm_root_sequence_index.value                 = radioResourceConfigCommon_BRP->prach_Config.rootSequenceIndex;
-    cfg->emtc_config.prach_catm_zero_correlation_zone_configuration.value = radioResourceConfigCommon_BRP->prach_Config.prach_ConfigInfo.zeroCorrelationZoneConfig;
-    cfg->emtc_config.prach_catm_high_speed_flag.value                     = radioResourceConfigCommon_BRP->prach_Config.prach_ConfigInfo.highSpeedFlag;
- 
-    struct PRACH_ConfigSIB_v1310 *ext4_prach=radioResourceConfigCommon_BRP->ext4->prach_ConfigCommon_v1310; 
-
-    PRACH_ParametersListCE_r13_t	 *prach_ParametersListCE_r13 = &ext4_prach->prach_ParametersListCE_r13;
-
-    PRACH_ParametersCE_r13_t *p;
-    cfg->emtc_config.prach_ce_level_0_enable.value=0;
-    cfg->emtc_config.prach_ce_level_1_enable.value=0;
-    cfg->emtc_config.prach_ce_level_2_enable.value=0;
-    cfg->emtc_config.prach_ce_level_3_enable.value=0;
-    switch (prach_ParametersListCE_r13->list.count) {
-    case 4:
-      p=prach_ParametersListCE_r13->list.array[3];
-      cfg->emtc_config.prach_ce_level_3_enable.value                            = 1;
-      cfg->emtc_config.prach_ce_level_3_configuration_index.value               = p->prach_ConfigIndex_r13;
-      cfg->emtc_config.prach_ce_level_3_frequency_offset.value                  = p->prach_FreqOffset_r13;
-      cfg->emtc_config.prach_ce_level_3_number_of_repetitions_per_attempt.value = p->numRepetitionPerPreambleAttempt_r13;
-      if (p->prach_StartingSubframe_r13) 
-	cfg->emtc_config.prach_ce_level_3_starting_subframe_periodicity.value   = *p->prach_StartingSubframe_r13;
-      cfg->emtc_config.prach_ce_level_3_hopping_enable.value                    = p->prach_HoppingConfig_r13;
-      cfg->emtc_config.prach_ce_level_3_hopping_offset.value                    = cfg->rf_config.ul_channel_bandwidth.value-6;
-    case 3:
-      p=prach_ParametersListCE_r13->list.array[2];
-      cfg->emtc_config.prach_ce_level_2_enable.value                            = 1;
-      cfg->emtc_config.prach_ce_level_2_configuration_index.value               = p->prach_ConfigIndex_r13;
-      cfg->emtc_config.prach_ce_level_2_frequency_offset.value                  = p->prach_FreqOffset_r13;
-      cfg->emtc_config.prach_ce_level_2_number_of_repetitions_per_attempt.value = p->numRepetitionPerPreambleAttempt_r13;
-      if (p->prach_StartingSubframe_r13) 
-	cfg->emtc_config.prach_ce_level_2_starting_subframe_periodicity.value   = *p->prach_StartingSubframe_r13;
-      cfg->emtc_config.prach_ce_level_2_hopping_enable.value                    = p->prach_HoppingConfig_r13;
-      cfg->emtc_config.prach_ce_level_2_hopping_offset.value                    = cfg->rf_config.ul_channel_bandwidth.value-6;
-    case 2:
-      p=prach_ParametersListCE_r13->list.array[1];
-      cfg->emtc_config.prach_ce_level_1_enable.value                            = 1;
-      cfg->emtc_config.prach_ce_level_1_configuration_index.value               = p->prach_ConfigIndex_r13;
-      cfg->emtc_config.prach_ce_level_1_frequency_offset.value                  = p->prach_FreqOffset_r13;
-      cfg->emtc_config.prach_ce_level_1_number_of_repetitions_per_attempt.value = p->numRepetitionPerPreambleAttempt_r13;
-      if (p->prach_StartingSubframe_r13) 
-	cfg->emtc_config.prach_ce_level_1_starting_subframe_periodicity.value   = *p->prach_StartingSubframe_r13;
-      cfg->emtc_config.prach_ce_level_1_hopping_enable.value                    = p->prach_HoppingConfig_r13;
-      cfg->emtc_config.prach_ce_level_1_hopping_offset.value                    = cfg->rf_config.ul_channel_bandwidth.value-6;
-    case 1:
-      p=prach_ParametersListCE_r13->list.array[0];
-      cfg->emtc_config.prach_ce_level_0_enable.value                            = 1;
-      cfg->emtc_config.prach_ce_level_0_configuration_index.value               = p->prach_ConfigIndex_r13;
-      cfg->emtc_config.prach_ce_level_0_frequency_offset.value                  = p->prach_FreqOffset_r13;
-      cfg->emtc_config.prach_ce_level_0_number_of_repetitions_per_attempt.value = p->numRepetitionPerPreambleAttempt_r13;
-      if (p->prach_StartingSubframe_r13) 
-	cfg->emtc_config.prach_ce_level_0_starting_subframe_periodicity.value   = *p->prach_StartingSubframe_r13;
-      cfg->emtc_config.prach_ce_level_0_hopping_enable.value                    = p->prach_HoppingConfig_r13;
-      cfg->emtc_config.prach_ce_level_0_hopping_offset.value                    = cfg->rf_config.ul_channel_bandwidth.value-6;
-    }
+	    ARFCN_ValueEUTRA_t * ul_CArrierFreqP,
+	    long *ul_BandwidthP,
+	    AdditionalSpectrumEmission_t *
+	    additionalSpectrumEmissionP,
+	    struct MBSFN_SubframeConfigList *mbsfn_SubframeConfigListP)
+{
 
-    struct FreqHoppingParameters_r13 *ext4_freqHoppingParameters = radioResourceConfigCommonP->ext4->freqHoppingParameters_r13;
-    if ((ext4_freqHoppingParameters) && 
-	(ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13)){
-      switch(ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13->present) {
-      case 	FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeA_r13_PR_NOTHING:	/* No components present */
-	break;
-      case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeA_r13_PR_interval_FDD_r13:
-	cfg->emtc_config.pucch_interval_ulhoppingconfigcommonmodea.value = ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13->choice.interval_FDD_r13;
-	break;
-      case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeA_r13_PR_interval_TDD_r13:
-	cfg->emtc_config.pucch_interval_ulhoppingconfigcommonmodea.value = ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13->choice.interval_TDD_r13;
-	break;
-      }
+    nfapi_config_request_t *cfg = &RC.mac[Mod_idP]->config[CC_idP];
+
+    cfg->subframe_config.pb.value =
+	radioResourceConfigCommonP->pdsch_ConfigCommon.p_b;
+    cfg->rf_config.reference_signal_power.value =
+	radioResourceConfigCommonP->pdsch_ConfigCommon.
+	referenceSignalPower;
+    cfg->nfapi_config.max_transmit_power.value =
+	cfg->rf_config.reference_signal_power.value +
+	power_off_dB[cfg->rf_config.dl_channel_bandwidth.value];
+
+    cfg->prach_config.configuration_index.value =
+	radioResourceConfigCommonP->prach_Config.
+	prach_ConfigInfo.prach_ConfigIndex;
+    cfg->prach_config.root_sequence_index.value =
+	radioResourceConfigCommonP->prach_Config.rootSequenceIndex;
+    cfg->prach_config.zero_correlation_zone_configuration.value =
+	radioResourceConfigCommonP->prach_Config.
+	prach_ConfigInfo.zeroCorrelationZoneConfig;
+    cfg->prach_config.high_speed_flag.value =
+	radioResourceConfigCommonP->prach_Config.prach_ConfigInfo.
+	highSpeedFlag;
+    cfg->prach_config.frequency_offset.value =
+	radioResourceConfigCommonP->prach_Config.
+	prach_ConfigInfo.prach_FreqOffset;
+
+    cfg->pusch_config.hopping_mode.value =
+	radioResourceConfigCommonP->pusch_ConfigCommon.
+	pusch_ConfigBasic.hoppingMode;
+    cfg->pusch_config.number_of_subbands.value =
+	radioResourceConfigCommonP->pusch_ConfigCommon.pusch_ConfigBasic.
+	n_SB;
+    cfg->pusch_config.hopping_offset.value =
+	radioResourceConfigCommonP->pusch_ConfigCommon.
+	pusch_ConfigBasic.pusch_HoppingOffset;
+
+
+
+    cfg->pucch_config.delta_pucch_shift.value =
+	radioResourceConfigCommonP->pucch_ConfigCommon.deltaPUCCH_Shift;
+    cfg->pucch_config.n_cqi_rb.value =
+	radioResourceConfigCommonP->pucch_ConfigCommon.nRB_CQI;
+    cfg->pucch_config.n_an_cs.value =
+	radioResourceConfigCommonP->pucch_ConfigCommon.nCS_AN;
+    cfg->pucch_config.n1_pucch_an.value =
+	radioResourceConfigCommonP->pucch_ConfigCommon.n1PUCCH_AN;
+
+    if (radioResourceConfigCommonP->
+	pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.groupHoppingEnabled ==
+	true)
+	cfg->uplink_reference_signal_config.uplink_rs_hopping.value = 1;
+    else if (radioResourceConfigCommonP->
+	     pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.
+	     sequenceHoppingEnabled == true)
+	cfg->uplink_reference_signal_config.uplink_rs_hopping.value = 2;
+    else			// No hopping
+	cfg->uplink_reference_signal_config.uplink_rs_hopping.value = 0;
+
+    cfg->uplink_reference_signal_config.group_assignment.value =
+	radioResourceConfigCommonP->
+	pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.groupAssignmentPUSCH;
+    cfg->uplink_reference_signal_config.cyclic_shift_1_for_drms.value =
+	radioResourceConfigCommonP->
+	pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.cyclicShift;
+
+    // how to enable/disable SRS?
+    if (radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.present ==
+	SoundingRS_UL_ConfigCommon_PR_setup) {
+	cfg->srs_config.bandwidth_configuration.value =
+	    radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.
+	    choice.setup.srs_BandwidthConfig;
+	cfg->srs_config.srs_subframe_configuration.value =
+	    radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.
+	    choice.setup.srs_SubframeConfig;
+	cfg->srs_config.srs_acknack_srs_simultaneous_transmission.value =
+	    radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.
+	    choice.setup.ackNackSRS_SimultaneousTransmission;
+
+	if (radioResourceConfigCommonP->soundingRS_UL_ConfigCommon.
+	    choice.setup.srs_MaxUpPts)
+	    cfg->srs_config.max_up_pts.value = 1;
+	else
+	    cfg->srs_config.max_up_pts.value = 0;
     }
-    if ((ext4_freqHoppingParameters) && 
-	(ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13)){
-      switch(ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13->present) {
-      case 	FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeB_r13_PR_NOTHING:	/* No components present */
-	break;
-      case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeB_r13_PR_interval_FDD_r13:
-	cfg->emtc_config.pucch_interval_ulhoppingconfigcommonmodeb.value = ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13->choice.interval_FDD_r13;
-	break;
-      case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeB_r13_PR_interval_TDD_r13:
-	cfg->emtc_config.pucch_interval_ulhoppingconfigcommonmodeb.value = ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13->choice.interval_TDD_r13;
-	break;
-      }
+#ifdef Rel14
+    if (RC.mac[Mod_idP]->common_channels[CC_idP].mib->
+	message.schedulingInfoSIB1_BR_r13 > 0) {
+	AssertFatal(radioResourceConfigCommon_BRP != NULL,
+		    "radioResource rou is missing\n");
+	AssertFatal(radioResourceConfigCommon_BRP->ext4 != NULL,
+		    "ext4 is missing\n");
+	cfg->emtc_config.prach_catm_root_sequence_index.value =
+	    radioResourceConfigCommon_BRP->prach_Config.rootSequenceIndex;
+	cfg->emtc_config.prach_catm_zero_correlation_zone_configuration.
+	    value =
+	    radioResourceConfigCommon_BRP->prach_Config.
+	    prach_ConfigInfo.zeroCorrelationZoneConfig;
+	cfg->emtc_config.prach_catm_high_speed_flag.value =
+	    radioResourceConfigCommon_BRP->prach_Config.
+	    prach_ConfigInfo.highSpeedFlag;
+
+	struct PRACH_ConfigSIB_v1310 *ext4_prach =
+	    radioResourceConfigCommon_BRP->ext4->prach_ConfigCommon_v1310;
+
+	PRACH_ParametersListCE_r13_t *prach_ParametersListCE_r13 =
+	    &ext4_prach->prach_ParametersListCE_r13;
+
+	PRACH_ParametersCE_r13_t *p;
+	cfg->emtc_config.prach_ce_level_0_enable.value = 0;
+	cfg->emtc_config.prach_ce_level_1_enable.value = 0;
+	cfg->emtc_config.prach_ce_level_2_enable.value = 0;
+	cfg->emtc_config.prach_ce_level_3_enable.value = 0;
+	switch (prach_ParametersListCE_r13->list.count) {
+	case 4:
+	    p = prach_ParametersListCE_r13->list.array[3];
+	    cfg->emtc_config.prach_ce_level_3_enable.value = 1;
+	    cfg->emtc_config.prach_ce_level_3_configuration_index.value =
+		p->prach_ConfigIndex_r13;
+	    cfg->emtc_config.prach_ce_level_3_frequency_offset.value =
+		p->prach_FreqOffset_r13;
+	    cfg->
+		emtc_config.prach_ce_level_3_number_of_repetitions_per_attempt.
+		value = p->numRepetitionPerPreambleAttempt_r13;
+	    if (p->prach_StartingSubframe_r13)
+		cfg->
+		    emtc_config.prach_ce_level_3_starting_subframe_periodicity.
+		    value = *p->prach_StartingSubframe_r13;
+	    cfg->emtc_config.prach_ce_level_3_hopping_enable.value =
+		p->prach_HoppingConfig_r13;
+	    cfg->emtc_config.prach_ce_level_3_hopping_offset.value =
+		cfg->rf_config.ul_channel_bandwidth.value - 6;
+	case 3:
+	    p = prach_ParametersListCE_r13->list.array[2];
+	    cfg->emtc_config.prach_ce_level_2_enable.value = 1;
+	    cfg->emtc_config.prach_ce_level_2_configuration_index.value =
+		p->prach_ConfigIndex_r13;
+	    cfg->emtc_config.prach_ce_level_2_frequency_offset.value =
+		p->prach_FreqOffset_r13;
+	    cfg->
+		emtc_config.prach_ce_level_2_number_of_repetitions_per_attempt.
+		value = p->numRepetitionPerPreambleAttempt_r13;
+	    if (p->prach_StartingSubframe_r13)
+		cfg->
+		    emtc_config.prach_ce_level_2_starting_subframe_periodicity.
+		    value = *p->prach_StartingSubframe_r13;
+	    cfg->emtc_config.prach_ce_level_2_hopping_enable.value =
+		p->prach_HoppingConfig_r13;
+	    cfg->emtc_config.prach_ce_level_2_hopping_offset.value =
+		cfg->rf_config.ul_channel_bandwidth.value - 6;
+	case 2:
+	    p = prach_ParametersListCE_r13->list.array[1];
+	    cfg->emtc_config.prach_ce_level_1_enable.value = 1;
+	    cfg->emtc_config.prach_ce_level_1_configuration_index.value =
+		p->prach_ConfigIndex_r13;
+	    cfg->emtc_config.prach_ce_level_1_frequency_offset.value =
+		p->prach_FreqOffset_r13;
+	    cfg->
+		emtc_config.prach_ce_level_1_number_of_repetitions_per_attempt.
+		value = p->numRepetitionPerPreambleAttempt_r13;
+	    if (p->prach_StartingSubframe_r13)
+		cfg->
+		    emtc_config.prach_ce_level_1_starting_subframe_periodicity.
+		    value = *p->prach_StartingSubframe_r13;
+	    cfg->emtc_config.prach_ce_level_1_hopping_enable.value =
+		p->prach_HoppingConfig_r13;
+	    cfg->emtc_config.prach_ce_level_1_hopping_offset.value =
+		cfg->rf_config.ul_channel_bandwidth.value - 6;
+	case 1:
+	    p = prach_ParametersListCE_r13->list.array[0];
+	    cfg->emtc_config.prach_ce_level_0_enable.value = 1;
+	    cfg->emtc_config.prach_ce_level_0_configuration_index.value =
+		p->prach_ConfigIndex_r13;
+	    cfg->emtc_config.prach_ce_level_0_frequency_offset.value =
+		p->prach_FreqOffset_r13;
+	    cfg->
+		emtc_config.prach_ce_level_0_number_of_repetitions_per_attempt.
+		value = p->numRepetitionPerPreambleAttempt_r13;
+	    if (p->prach_StartingSubframe_r13)
+		cfg->
+		    emtc_config.prach_ce_level_0_starting_subframe_periodicity.
+		    value = *p->prach_StartingSubframe_r13;
+	    cfg->emtc_config.prach_ce_level_0_hopping_enable.value =
+		p->prach_HoppingConfig_r13;
+	    cfg->emtc_config.prach_ce_level_0_hopping_offset.value =
+		cfg->rf_config.ul_channel_bandwidth.value - 6;
+	}
+
+	struct FreqHoppingParameters_r13 *ext4_freqHoppingParameters =
+	    radioResourceConfigCommonP->ext4->freqHoppingParameters_r13;
+	if ((ext4_freqHoppingParameters)
+	    &&
+	    (ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13))
+	{
+	    switch
+		(ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13->
+		 present) {
+	    case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeA_r13_PR_NOTHING:	/* No components present */
+		break;
+	    case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeA_r13_PR_interval_FDD_r13:
+		cfg->emtc_config.
+		    pucch_interval_ulhoppingconfigcommonmodea.value =
+		    ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13->
+		    choice.interval_FDD_r13;
+		break;
+	    case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeA_r13_PR_interval_TDD_r13:
+		cfg->emtc_config.
+		    pucch_interval_ulhoppingconfigcommonmodea.value =
+		    ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeA_r13->
+		    choice.interval_TDD_r13;
+		break;
+	    }
+	}
+	if ((ext4_freqHoppingParameters) &&
+	    (ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13))
+	{
+	    switch
+		(ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13->
+		 present) {
+	    case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeB_r13_PR_NOTHING:	/* No components present */
+		break;
+	    case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeB_r13_PR_interval_FDD_r13:
+		cfg->emtc_config.
+		    pucch_interval_ulhoppingconfigcommonmodeb.value =
+		    ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13->
+		    choice.interval_FDD_r13;
+		break;
+	    case FreqHoppingParameters_r13__interval_ULHoppingConfigCommonModeB_r13_PR_interval_TDD_r13:
+		cfg->emtc_config.
+		    pucch_interval_ulhoppingconfigcommonmodeb.value =
+		    ext4_freqHoppingParameters->interval_ULHoppingConfigCommonModeB_r13->
+		    choice.interval_TDD_r13;
+		break;
+	    }
+	}
     }
-  }
 #endif
 
 }
 
-void config_dedicated(int Mod_idP, 
-		      int CC_idP, 
-		      uint16_t rnti, 
-		      struct PhysicalConfigDedicated  *physicalConfigDedicated) {
+void
+config_dedicated(int Mod_idP,
+		 int CC_idP,
+		 uint16_t rnti,
+		 struct PhysicalConfigDedicated *physicalConfigDedicated)
+{
 
 }
 
-void config_dedicated_scell(int Mod_idP, 
-			    uint16_t rnti, 
-			    SCellToAddMod_r10_t *sCellToAddMod_r10) {
+void
+config_dedicated_scell(int Mod_idP,
+		       uint16_t rnti,
+		       SCellToAddMod_r10_t * sCellToAddMod_r10)
+{
 
 }
 
-int rrc_mac_config_req_eNB(module_id_t                      Mod_idP,
-			   int                              CC_idP,
-			   int                              physCellId,
-			   int                              p_eNB,
-			   int                              Ncp,
-			   int                              eutra_band,
-			   uint32_t                         dl_CarrierFreq,
+int
+rrc_mac_config_req_eNB(module_id_t Mod_idP,
+		       int CC_idP,
+		       int physCellId,
+		       int p_eNB,
+		       int Ncp, int eutra_band, uint32_t dl_CarrierFreq,
 #ifdef Rel14
-                           int                              pbch_repetition,
+		       int pbch_repetition,
 #endif
-			   rnti_t                           rntiP,
-			   BCCH_BCH_Message_t               *mib,
-			   RadioResourceConfigCommonSIB_t   *radioResourceConfigCommon,
+		       rnti_t rntiP,
+		       BCCH_BCH_Message_t * mib,
+		       RadioResourceConfigCommonSIB_t *
+		       radioResourceConfigCommon,
 #ifdef Rel14
-			   RadioResourceConfigCommonSIB_t   *radioResourceConfigCommon_BR,
+		       RadioResourceConfigCommonSIB_t *
+		       radioResourceConfigCommon_BR,
 #endif
-			   struct PhysicalConfigDedicated  *physicalConfigDedicated,
+		       struct PhysicalConfigDedicated
+		       *physicalConfigDedicated,
 #if defined(Rel10) || defined(Rel14)
-			   SCellToAddMod_r10_t *sCellToAddMod_r10,
-			   //struct PhysicalConfigDedicatedSCell_r10 *physicalConfigDedicatedSCell_r10,
+		       SCellToAddMod_r10_t * sCellToAddMod_r10,
+		       //struct PhysicalConfigDedicatedSCell_r10 *physicalConfigDedicatedSCell_r10,
 #endif
-			   MeasObjectToAddMod_t                    **measObj,
-			   MAC_MainConfig_t                        *mac_MainConfig,
-			   long                                    logicalChannelIdentity,
-			   LogicalChannelConfig_t                  *logicalChannelConfig,
-			   MeasGapConfig_t                         *measGapConfig,
-			   TDD_Config_t                            *tdd_Config,
-			   MobilityControlInfo_t                   *mobilityControlInfo,
-			   SchedulingInfoList_t                    *schedulingInfoList,
-			   uint32_t                                ul_CarrierFreq,
-			   long                                    *ul_Bandwidth,
-			   AdditionalSpectrumEmission_t            *additionalSpectrumEmission,
-			   struct MBSFN_SubframeConfigList         *mbsfn_SubframeConfigList
+		       MeasObjectToAddMod_t ** measObj,
+		       MAC_MainConfig_t * mac_MainConfig,
+		       long logicalChannelIdentity,
+		       LogicalChannelConfig_t * logicalChannelConfig,
+		       MeasGapConfig_t * measGapConfig,
+		       TDD_Config_t * tdd_Config,
+		       MobilityControlInfo_t * mobilityControlInfo,
+		       SchedulingInfoList_t * schedulingInfoList,
+		       uint32_t ul_CarrierFreq,
+		       long *ul_Bandwidth,
+		       AdditionalSpectrumEmission_t *
+		       additionalSpectrumEmission,
+		       struct MBSFN_SubframeConfigList
+		       *mbsfn_SubframeConfigList
 #if defined(Rel10) || defined(Rel14)
-			   ,uint8_t                                MBMS_Flag,
-			   MBSFN_AreaInfoList_r9_t                 *mbsfn_AreaInfoList,
-			   PMCH_InfoList_r9_t                      *pmch_InfoList
+		       , uint8_t MBMS_Flag,
+		       MBSFN_AreaInfoList_r9_t * mbsfn_AreaInfoList,
+		       PMCH_InfoList_r9_t * pmch_InfoList
 #endif
 #ifdef Rel14
-			   ,
-			   SystemInformationBlockType1_v1310_IEs_t *sib1_v13ext
+		       ,
+		       SystemInformationBlockType1_v1310_IEs_t *
+		       sib1_v13ext
 #endif
-			   ) {
-			   
-  int i;
-
-  int UE_id = -1;
-  eNB_MAC_INST *eNB = RC.mac[Mod_idP];
-  UE_list_t *UE_list= &eNB->UE_list;
-
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_RRC_MAC_CONFIG, VCD_FUNCTION_IN);
-
-  
-  if (mib!=NULL) {
-    if (RC.mac == NULL) l2_init_eNB();
-
-    mac_top_init_eNB();
-
-    RC.mac[Mod_idP]->common_channels[CC_idP].mib             = mib;
-    RC.mac[Mod_idP]->common_channels[CC_idP].physCellId      = physCellId;
-    RC.mac[Mod_idP]->common_channels[CC_idP].p_eNB           = p_eNB;
-    RC.mac[Mod_idP]->common_channels[CC_idP].Ncp             = Ncp;
-    RC.mac[Mod_idP]->common_channels[CC_idP].eutra_band      = eutra_band;
-    RC.mac[Mod_idP]->common_channels[CC_idP].dl_CarrierFreq  = dl_CarrierFreq;
-
-    LOG_I(MAC,
-	  "Configuring MIB for instance %d, CCid %d : (band %d,N_RB_DL %d,Nid_cell %d,p %d,DL freq %u,phich_config.resource %d, phich_config.duration %d)\n",
-	  Mod_idP, 
-	  CC_idP, 
-	  eutra_band, 
-	  to_prb((int)mib->message.dl_Bandwidth), 
-	  physCellId, 
-	  p_eNB,
-	  dl_CarrierFreq,
-	  (int)mib->message.phich_Config.phich_Resource,
-	  (int)mib->message.phich_Config.phich_Duration);
-
-    config_mib(Mod_idP,CC_idP,
-	       eutra_band,
-	       mib->message.dl_Bandwidth,
-	       &mib->message.phich_Config,
-	       physCellId,
-	       Ncp,
-	       p_eNB,
-	       dl_CarrierFreq,
-	       ul_CarrierFreq
+    )
+{
+
+    int i;
+
+    int UE_id = -1;
+    eNB_MAC_INST *eNB = RC.mac[Mod_idP];
+    UE_list_t *UE_list = &eNB->UE_list;
+
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_RRC_MAC_CONFIG, VCD_FUNCTION_IN);
+
+
+    if (mib != NULL) {
+	if (RC.mac == NULL)
+	    l2_init_eNB();
+
+	mac_top_init_eNB();
+
+	RC.mac[Mod_idP]->common_channels[CC_idP].mib = mib;
+	RC.mac[Mod_idP]->common_channels[CC_idP].physCellId = physCellId;
+	RC.mac[Mod_idP]->common_channels[CC_idP].p_eNB = p_eNB;
+	RC.mac[Mod_idP]->common_channels[CC_idP].Ncp = Ncp;
+	RC.mac[Mod_idP]->common_channels[CC_idP].eutra_band = eutra_band;
+	RC.mac[Mod_idP]->common_channels[CC_idP].dl_CarrierFreq =
+	    dl_CarrierFreq;
+
+	LOG_I(MAC,
+	      "Configuring MIB for instance %d, CCid %d : (band %d,N_RB_DL %d,Nid_cell %d,p %d,DL freq %u,phich_config.resource %d, phich_config.duration %d)\n",
+	      Mod_idP,
+	      CC_idP,
+	      eutra_band,
+	      to_prb((int) mib->message.dl_Bandwidth),
+	      physCellId,
+	      p_eNB,
+	      dl_CarrierFreq,
+	      (int) mib->message.phich_Config.phich_Resource,
+	      (int) mib->message.phich_Config.phich_Duration);
+
+	config_mib(Mod_idP, CC_idP,
+		   eutra_band,
+		   mib->message.dl_Bandwidth,
+		   &mib->message.phich_Config,
+		   physCellId, Ncp, p_eNB, dl_CarrierFreq, ul_CarrierFreq
 #ifdef Rel14
-	       ,pbch_repetition
+		   , pbch_repetition
 #endif
-	       );
-
-    mac_init_cell_params(Mod_idP,CC_idP);
-  }
-  if (schedulingInfoList!=NULL)  {
-    RC.mac[Mod_idP]->common_channels[CC_idP].tdd_Config         = tdd_Config;    
-    RC.mac[Mod_idP]->common_channels[CC_idP].schedulingInfoList = schedulingInfoList;    
-    config_sib1(Mod_idP,CC_idP,tdd_Config);
-  }
+	    );
+
+	mac_init_cell_params(Mod_idP, CC_idP);
+    }
+    if (schedulingInfoList != NULL) {
+	RC.mac[Mod_idP]->common_channels[CC_idP].tdd_Config = tdd_Config;
+	RC.mac[Mod_idP]->common_channels[CC_idP].schedulingInfoList =
+	    schedulingInfoList;
+	config_sib1(Mod_idP, CC_idP, tdd_Config);
+    }
 #ifdef Rel14
-  if (sib1_v13ext != NULL) {
-    RC.mac[Mod_idP]->common_channels[CC_idP].sib1_v13ext        = sib1_v13ext;    
-  }
+    if (sib1_v13ext != NULL) {
+	RC.mac[Mod_idP]->common_channels[CC_idP].sib1_v13ext = sib1_v13ext;
+    }
 #endif
-  if (radioResourceConfigCommon!=NULL) {
-      LOG_I(MAC,"[CONFIG]SIB2/3 Contents (partial)\n");
-      LOG_I(MAC,"[CONFIG]pusch_config_common.n_SB = %ld\n",radioResourceConfigCommon->pusch_ConfigCommon.pusch_ConfigBasic.n_SB);
-      LOG_I(MAC,"[CONFIG]pusch_config_common.hoppingMode = %ld\n",radioResourceConfigCommon->pusch_ConfigCommon.pusch_ConfigBasic.hoppingMode);
-      LOG_I(MAC,"[CONFIG]pusch_config_common.pusch_HoppingOffset = %ld\n",  radioResourceConfigCommon->pusch_ConfigCommon.pusch_ConfigBasic.pusch_HoppingOffset);
-      LOG_I(MAC,"[CONFIG]pusch_config_common.enable64QAM = %d\n",radioResourceConfigCommon->pusch_ConfigCommon.pusch_ConfigBasic.enable64QAM);
-      LOG_I(MAC,"[CONFIG]pusch_config_common.groupHoppingEnabled = %d\n",radioResourceConfigCommon->pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.groupHoppingEnabled);
-      LOG_I(MAC,"[CONFIG]pusch_config_common.groupAssignmentPUSCH = %ld\n",radioResourceConfigCommon->pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.groupAssignmentPUSCH);
-      LOG_I(MAC,"[CONFIG]pusch_config_common.sequenceHoppingEnabled = %d\n",radioResourceConfigCommon->pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.sequenceHoppingEnabled);
-      LOG_I(MAC,"[CONFIG]pusch_config_common.cyclicShift  = %ld\n",radioResourceConfigCommon->pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.cyclicShift);
-
-      AssertFatal(radioResourceConfigCommon->rach_ConfigCommon.maxHARQ_Msg3Tx > 0,
-		  "radioResourceconfigCommon %d == 0\n",
-		  (int)radioResourceConfigCommon->rach_ConfigCommon.maxHARQ_Msg3Tx);
-
-      RC.mac[Mod_idP]->common_channels[CC_idP].radioResourceConfigCommon = radioResourceConfigCommon;
-      if (ul_CarrierFreq>0) RC.mac[Mod_idP]->common_channels[CC_idP].ul_CarrierFreq          = ul_CarrierFreq;
-      if (ul_Bandwidth) RC.mac[Mod_idP]->common_channels[CC_idP].ul_Bandwidth                = *ul_Bandwidth;
-      else RC.mac[Mod_idP]->common_channels[CC_idP].ul_Bandwidth                             = RC.mac[Mod_idP]->common_channels[CC_idP].mib->message.dl_Bandwidth;
-
-      config_sib2(Mod_idP, CC_idP, 
-		  radioResourceConfigCommon, 
+    if (radioResourceConfigCommon != NULL) {
+	LOG_I(MAC, "[CONFIG]SIB2/3 Contents (partial)\n");
+	LOG_I(MAC, "[CONFIG]pusch_config_common.n_SB = %ld\n",
+	      radioResourceConfigCommon->
+	      pusch_ConfigCommon.pusch_ConfigBasic.n_SB);
+	LOG_I(MAC, "[CONFIG]pusch_config_common.hoppingMode = %ld\n",
+	      radioResourceConfigCommon->
+	      pusch_ConfigCommon.pusch_ConfigBasic.hoppingMode);
+	LOG_I(MAC,
+	      "[CONFIG]pusch_config_common.pusch_HoppingOffset = %ld\n",
+	      radioResourceConfigCommon->
+	      pusch_ConfigCommon.pusch_ConfigBasic.pusch_HoppingOffset);
+	LOG_I(MAC, "[CONFIG]pusch_config_common.enable64QAM = %d\n",
+	      radioResourceConfigCommon->
+	      pusch_ConfigCommon.pusch_ConfigBasic.enable64QAM);
+	LOG_I(MAC,
+	      "[CONFIG]pusch_config_common.groupHoppingEnabled = %d\n",
+	      radioResourceConfigCommon->
+	      pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.
+	      groupHoppingEnabled);
+	LOG_I(MAC,
+	      "[CONFIG]pusch_config_common.groupAssignmentPUSCH = %ld\n",
+	      radioResourceConfigCommon->
+	      pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.
+	      groupAssignmentPUSCH);
+	LOG_I(MAC,
+	      "[CONFIG]pusch_config_common.sequenceHoppingEnabled = %d\n",
+	      radioResourceConfigCommon->
+	      pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.
+	      sequenceHoppingEnabled);
+	LOG_I(MAC, "[CONFIG]pusch_config_common.cyclicShift  = %ld\n",
+	      radioResourceConfigCommon->
+	      pusch_ConfigCommon.ul_ReferenceSignalsPUSCH.cyclicShift);
+
+	AssertFatal(radioResourceConfigCommon->
+		    rach_ConfigCommon.maxHARQ_Msg3Tx > 0,
+		    "radioResourceconfigCommon %d == 0\n",
+		    (int) radioResourceConfigCommon->
+		    rach_ConfigCommon.maxHARQ_Msg3Tx);
+
+	RC.mac[Mod_idP]->common_channels[CC_idP].
+	    radioResourceConfigCommon = radioResourceConfigCommon;
+	if (ul_CarrierFreq > 0)
+	    RC.mac[Mod_idP]->common_channels[CC_idP].ul_CarrierFreq =
+		ul_CarrierFreq;
+	if (ul_Bandwidth)
+	    RC.mac[Mod_idP]->common_channels[CC_idP].ul_Bandwidth =
+		*ul_Bandwidth;
+	else
+	    RC.mac[Mod_idP]->common_channels[CC_idP].ul_Bandwidth =
+		RC.mac[Mod_idP]->common_channels[CC_idP].mib->message.
+		dl_Bandwidth;
+
+	config_sib2(Mod_idP, CC_idP, radioResourceConfigCommon,
 #ifdef Rel14
-		  radioResourceConfigCommon_BR, 
+		    radioResourceConfigCommon_BR,
 #endif
-		  NULL, ul_Bandwidth, additionalSpectrumEmission, mbsfn_SubframeConfigList);
+		    NULL, ul_Bandwidth, additionalSpectrumEmission,
+		    mbsfn_SubframeConfigList);
 
 
-  }
-
-
-  // SRB2_lchan_config->choice.explicitValue.ul_SpecificParameters->logicalChannelGroup
-  if (logicalChannelConfig!= NULL) { // check for eMTC specific things
-    UE_id = find_UE_id(Mod_idP, rntiP);
-
-    if (UE_id == -1) {
-      LOG_E(MAC,"%s:%d:%s: ERROR, UE_id == -1\n", __FILE__, __LINE__, __FUNCTION__);
-    } else {
-      if (logicalChannelConfig)
-	UE_list->UE_template[CC_idP][UE_id].lcgidmap[logicalChannelIdentity] = *logicalChannelConfig->ul_SpecificParameters->logicalChannelGroup;
-      else
-	UE_list->UE_template[CC_idP][UE_id].lcgidmap[logicalChannelIdentity] = 0;
     }
-  }
-  
-
-  if (physicalConfigDedicated != NULL) {
-    UE_id = find_UE_id(Mod_idP, rntiP);
+    // SRB2_lchan_config->choice.explicitValue.ul_SpecificParameters->logicalChannelGroup
+    if (logicalChannelConfig != NULL) {	// check for eMTC specific things
+	UE_id = find_UE_id(Mod_idP, rntiP);
+
+	if (UE_id == -1) {
+	    LOG_E(MAC, "%s:%d:%s: ERROR, UE_id == -1\n", __FILE__,
+		  __LINE__, __FUNCTION__);
+	} else {
+	    if (logicalChannelConfig)
+		UE_list->
+		    UE_template[CC_idP][UE_id].lcgidmap
+		    [logicalChannelIdentity] =
+		    *logicalChannelConfig->
+		    ul_SpecificParameters->logicalChannelGroup;
+	    else
+		UE_list->
+		    UE_template[CC_idP][UE_id].lcgidmap
+		    [logicalChannelIdentity] = 0;
+	}
+    }
 
-    if (UE_id == -1)
-      LOG_E(MAC,"%s:%d:%s: ERROR, UE_id == -1\n", __FILE__, __LINE__, __FUNCTION__);
-    else
-      UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated=physicalConfigDedicated;
-  } 
 
+    if (physicalConfigDedicated != NULL) {
+	UE_id = find_UE_id(Mod_idP, rntiP);
 
+	if (UE_id == -1)
+	    LOG_E(MAC, "%s:%d:%s: ERROR, UE_id == -1\n", __FILE__,
+		  __LINE__, __FUNCTION__);
+	else
+	    UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated =
+		physicalConfigDedicated;
+    }
 
 
 #if defined(Rel10) || defined(Rel14)
 
-  if (sCellToAddMod_r10 != NULL) {
-    UE_id = find_UE_id(Mod_idP, rntiP);
-    if (UE_id == -1)
-      LOG_E(MAC,"%s:%d:%s: ERROR, UE_id == -1\n", __FILE__, __LINE__, __FUNCTION__);
-    else
-      config_dedicated_scell(Mod_idP,rntiP,sCellToAddMod_r10);
-    
-  }
+    if (sCellToAddMod_r10 != NULL) {
+	UE_id = find_UE_id(Mod_idP, rntiP);
+	if (UE_id == -1)
+	    LOG_E(MAC, "%s:%d:%s: ERROR, UE_id == -1\n", __FILE__,
+		  __LINE__, __FUNCTION__);
+	else
+	    config_dedicated_scell(Mod_idP, rntiP, sCellToAddMod_r10);
 
+    }
 #endif
 
 
 
 
-  if (mbsfn_SubframeConfigList != NULL) {
-    LOG_I(MAC,"[eNB %d][CONFIG] Received %d subframe allocation pattern for MBSFN\n", Mod_idP, mbsfn_SubframeConfigList->list.count);
-    RC.mac[Mod_idP]->common_channels[0].num_sf_allocation_pattern= mbsfn_SubframeConfigList->list.count;
-    
-    for (i=0; i<mbsfn_SubframeConfigList->list.count; i++) {
-      RC.mac[Mod_idP]->common_channels[0].mbsfn_SubframeConfig[i] = mbsfn_SubframeConfigList->list.array[i];
-      LOG_I(MAC, "[eNB %d][CONFIG] MBSFN_SubframeConfig[%d] pattern is  %x\n", Mod_idP, i,
-	    RC.mac[Mod_idP]->common_channels[0].mbsfn_SubframeConfig[i]->subframeAllocation.choice.oneFrame.buf[0]);
-    }
-    
+    if (mbsfn_SubframeConfigList != NULL) {
+	LOG_I(MAC,
+	      "[eNB %d][CONFIG] Received %d subframe allocation pattern for MBSFN\n",
+	      Mod_idP, mbsfn_SubframeConfigList->list.count);
+	RC.mac[Mod_idP]->common_channels[0].num_sf_allocation_pattern =
+	    mbsfn_SubframeConfigList->list.count;
+
+	for (i = 0; i < mbsfn_SubframeConfigList->list.count; i++) {
+	    RC.mac[Mod_idP]->common_channels[0].mbsfn_SubframeConfig[i] =
+		mbsfn_SubframeConfigList->list.array[i];
+	    LOG_I(MAC,
+		  "[eNB %d][CONFIG] MBSFN_SubframeConfig[%d] pattern is  %x\n",
+		  Mod_idP, i,
+		  RC.mac[Mod_idP]->
+		  common_channels[0].mbsfn_SubframeConfig[i]->
+		  subframeAllocation.choice.oneFrame.buf[0]);
+	}
+
 #ifdef Rel10
-    RC.mac[Mod_idP]->common_channels[0].MBMS_flag = MBMS_Flag;
+	RC.mac[Mod_idP]->common_channels[0].MBMS_flag = MBMS_Flag;
 #endif
-  }
-
+    }
 #if defined(Rel10) || defined(Rel14)
 
-  if (mbsfn_AreaInfoList != NULL) {
-      // One eNB could be part of multiple mbsfn syc area, this could change over time so reset each time
-    LOG_I(MAC,"[eNB %d][CONFIG] Received %d MBSFN Area Info\n", Mod_idP, mbsfn_AreaInfoList->list.count);
-    RC.mac[Mod_idP]->common_channels[0].num_active_mbsfn_area = mbsfn_AreaInfoList->list.count;
-    
-    for (i =0; i< mbsfn_AreaInfoList->list.count; i++) {
-      RC.mac[Mod_idP]->common_channels[0].mbsfn_AreaInfo[i] = mbsfn_AreaInfoList->list.array[i];
-      LOG_I(MAC,"[eNB %d][CONFIG] MBSFN_AreaInfo[%d]: MCCH Repetition Period = %ld\n", Mod_idP,i,
-	    RC.mac[Mod_idP]->common_channels[0].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_RepetitionPeriod_r9);
-      //      config_sib13(Mod_idP,0,i,RC.mac[Mod_idP]->common_channels[0].mbsfn_AreaInfo[i]->mbsfn_AreaId_r9);
-    }
-  } 
-
-  if (pmch_InfoList != NULL) {
-
-    //    LOG_I(MAC,"DUY: lcid when entering rrc_mac config_req is %02d\n",(pmch_InfoList->list.array[0]->mbms_SessionInfoList_r9.list.array[0]->logicalChannelIdentity_r9));
-
-    LOG_I(MAC, "[CONFIG] Number of PMCH in this MBSFN Area %d\n", pmch_InfoList->list.count);
-    
-    for (i =0; i< pmch_InfoList->list.count; i++) {
-      RC.mac[Mod_idP]->common_channels[0].pmch_Config[i] = &pmch_InfoList->list.array[i]->pmch_Config_r9;
-      
-      LOG_I(MAC, "[CONFIG] PMCH[%d]: This PMCH stop (sf_AllocEnd_r9) at subframe  %ldth\n", i,
-	    RC.mac[Mod_idP]->common_channels[0].pmch_Config[i]->sf_AllocEnd_r9);
-      LOG_I(MAC, "[CONFIG] PMCH[%d]: mch_Scheduling_Period = %ld\n", i,
-	    RC.mac[Mod_idP]->common_channels[0].pmch_Config[i]->mch_SchedulingPeriod_r9);
-      LOG_I(MAC, "[CONFIG] PMCH[%d]: dataMCS = %ld\n", i,
-	    RC.mac[Mod_idP]->common_channels[0].pmch_Config[i]->dataMCS_r9);
-      
-      // MBMS session info list in each MCH
-      RC.mac[Mod_idP]->common_channels[0].mbms_SessionList[i] = &pmch_InfoList->list.array[i]->mbms_SessionInfoList_r9;
-      LOG_I(MAC, "PMCH[%d] Number of session (MTCH) is: %d\n",i, RC.mac[Mod_idP]->common_channels[0].mbms_SessionList[i]->list.count);
+    if (mbsfn_AreaInfoList != NULL) {
+	// One eNB could be part of multiple mbsfn syc area, this could change over time so reset each time
+	LOG_I(MAC, "[eNB %d][CONFIG] Received %d MBSFN Area Info\n",
+	      Mod_idP, mbsfn_AreaInfoList->list.count);
+	RC.mac[Mod_idP]->common_channels[0].num_active_mbsfn_area =
+	    mbsfn_AreaInfoList->list.count;
+
+	for (i = 0; i < mbsfn_AreaInfoList->list.count; i++) {
+	    RC.mac[Mod_idP]->common_channels[0].mbsfn_AreaInfo[i] =
+		mbsfn_AreaInfoList->list.array[i];
+	    LOG_I(MAC,
+		  "[eNB %d][CONFIG] MBSFN_AreaInfo[%d]: MCCH Repetition Period = %ld\n",
+		  Mod_idP, i,
+		  RC.mac[Mod_idP]->common_channels[0].
+		  mbsfn_AreaInfo[i]->mcch_Config_r9.
+		  mcch_RepetitionPeriod_r9);
+	    //      config_sib13(Mod_idP,0,i,RC.mac[Mod_idP]->common_channels[0].mbsfn_AreaInfo[i]->mbsfn_AreaId_r9);
+	}
     }
-  }
 
+    if (pmch_InfoList != NULL) {
+
+	//    LOG_I(MAC,"DUY: lcid when entering rrc_mac config_req is %02d\n",(pmch_InfoList->list.array[0]->mbms_SessionInfoList_r9.list.array[0]->logicalChannelIdentity_r9));
+
+	LOG_I(MAC, "[CONFIG] Number of PMCH in this MBSFN Area %d\n",
+	      pmch_InfoList->list.count);
+
+	for (i = 0; i < pmch_InfoList->list.count; i++) {
+	    RC.mac[Mod_idP]->common_channels[0].pmch_Config[i] =
+		&pmch_InfoList->list.array[i]->pmch_Config_r9;
+
+	    LOG_I(MAC,
+		  "[CONFIG] PMCH[%d]: This PMCH stop (sf_AllocEnd_r9) at subframe  %ldth\n",
+		  i,
+		  RC.mac[Mod_idP]->common_channels[0].
+		  pmch_Config[i]->sf_AllocEnd_r9);
+	    LOG_I(MAC, "[CONFIG] PMCH[%d]: mch_Scheduling_Period = %ld\n",
+		  i,
+		  RC.mac[Mod_idP]->common_channels[0].
+		  pmch_Config[i]->mch_SchedulingPeriod_r9);
+	    LOG_I(MAC, "[CONFIG] PMCH[%d]: dataMCS = %ld\n", i,
+		  RC.mac[Mod_idP]->common_channels[0].
+		  pmch_Config[i]->dataMCS_r9);
+
+	    // MBMS session info list in each MCH
+	    RC.mac[Mod_idP]->common_channels[0].mbms_SessionList[i] =
+		&pmch_InfoList->list.array[i]->mbms_SessionInfoList_r9;
+	    LOG_I(MAC, "PMCH[%d] Number of session (MTCH) is: %d\n", i,
+		  RC.mac[Mod_idP]->common_channels[0].
+		  mbms_SessionList[i]->list.count);
+	}
+    }
 #endif
 
-  if (radioResourceConfigCommon!=NULL) {
-    AssertFatal(RC.mac[Mod_idP]->if_inst->PHY_config_req != NULL,"if_inst->phy_config_request is null\n");
-    PHY_Config_t phycfg;
-    phycfg.Mod_id = Mod_idP;
-    phycfg.CC_id  = CC_idP;
-    phycfg.cfg    = &RC.mac[Mod_idP]->config[CC_idP];
-    
-    if (RC.mac[Mod_idP]->if_inst->PHY_config_req) RC.mac[Mod_idP]->if_inst->PHY_config_req(&phycfg); 
-  }
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_RRC_MAC_CONFIG, VCD_FUNCTION_OUT);
-  
-  return(0);			     
+    if (radioResourceConfigCommon != NULL) {
+	AssertFatal(RC.mac[Mod_idP]->if_inst->PHY_config_req != NULL,
+		    "if_inst->phy_config_request is null\n");
+	PHY_Config_t phycfg;
+	phycfg.Mod_id = Mod_idP;
+	phycfg.CC_id = CC_idP;
+	phycfg.cfg = &RC.mac[Mod_idP]->config[CC_idP];
+
+	if (RC.mac[Mod_idP]->if_inst->PHY_config_req)
+	    RC.mac[Mod_idP]->if_inst->PHY_config_req(&phycfg);
+    }
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_RRC_MAC_CONFIG, VCD_FUNCTION_OUT);
+
+    return (0);
 }
 
 int
-rrc_mac_config_req_ue(
-  module_id_t                      Mod_idP,
-  int                              CC_idP,
-  uint8_t                          eNB_index,
-  RadioResourceConfigCommonSIB_t  *radioResourceConfigCommon,
-  struct PhysicalConfigDedicated  *physicalConfigDedicated,
+rrc_mac_config_req_ue(module_id_t Mod_idP,
+		      int CC_idP,
+		      uint8_t eNB_index,
+		      RadioResourceConfigCommonSIB_t *
+		      radioResourceConfigCommon,
+		      struct PhysicalConfigDedicated
+		      *physicalConfigDedicated,
 #if defined(Rel10) || defined(Rel14)
-  SCellToAddMod_r10_t *sCellToAddMod_r10,
-  //struct PhysicalConfigDedicatedSCell_r10 *physicalConfigDedicatedSCell_r10,
+		      SCellToAddMod_r10_t * sCellToAddMod_r10,
+		      //struct PhysicalConfigDedicatedSCell_r10 *physicalConfigDedicatedSCell_r10,
 #endif
-  MeasObjectToAddMod_t           **measObj,
-  MAC_MainConfig_t                *mac_MainConfig,
-  long                             logicalChannelIdentity,
-  LogicalChannelConfig_t          *logicalChannelConfig,
-  MeasGapConfig_t                 *measGapConfig,
-  TDD_Config_t                    *tdd_Config,
-  MobilityControlInfo_t           *mobilityControlInfo,
-  uint8_t                              *SIwindowsize,
-  uint16_t                             *SIperiod,
-  ARFCN_ValueEUTRA_t              *ul_CarrierFreq,
-  long                            *ul_Bandwidth,
-  AdditionalSpectrumEmission_t    *additionalSpectrumEmission,
-  struct MBSFN_SubframeConfigList *mbsfn_SubframeConfigList
+		      MeasObjectToAddMod_t ** measObj,
+		      MAC_MainConfig_t * mac_MainConfig,
+		      long logicalChannelIdentity,
+		      LogicalChannelConfig_t * logicalChannelConfig,
+		      MeasGapConfig_t * measGapConfig,
+		      TDD_Config_t * tdd_Config,
+		      MobilityControlInfo_t * mobilityControlInfo,
+		      uint8_t * SIwindowsize,
+		      uint16_t * SIperiod,
+		      ARFCN_ValueEUTRA_t * ul_CarrierFreq,
+		      long *ul_Bandwidth,
+		      AdditionalSpectrumEmission_t *
+		      additionalSpectrumEmission,
+		      struct MBSFN_SubframeConfigList
+		      *mbsfn_SubframeConfigList
 #if defined(Rel10) || defined(Rel14)
-  ,uint8_t                              MBMS_Flag,
-  MBSFN_AreaInfoList_r9_t         *mbsfn_AreaInfoList,
-  PMCH_InfoList_r9_t              *pmch_InfoList
+		      , uint8_t MBMS_Flag,
+		      MBSFN_AreaInfoList_r9_t * mbsfn_AreaInfoList,
+		      PMCH_InfoList_r9_t * pmch_InfoList
 #endif
 #ifdef CBA
-  ,uint8_t                              num_active_cba_groups,
-  uint16_t                              cba_rnti
+		      , uint8_t num_active_cba_groups, uint16_t cba_rnti
 #endif
-                      )
+    )
 {
 
-  int i;
-
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_RRC_MAC_CONFIG, VCD_FUNCTION_IN);
-
-  LOG_I(MAC,"[CONFIG][UE %d] Configuring MAC/PHY from eNB %d\n",Mod_idP,eNB_index);
-  
-  if (tdd_Config != NULL) {
-    UE_mac_inst[Mod_idP].tdd_Config = tdd_Config;
-  }
- 
-
-  if (tdd_Config && SIwindowsize && SIperiod) {
-    phy_config_sib1_ue(Mod_idP,0,eNB_index,tdd_Config,*SIwindowsize,*SIperiod);
-  }
-
-  if (radioResourceConfigCommon!=NULL) {
-    UE_mac_inst[Mod_idP].radioResourceConfigCommon = radioResourceConfigCommon;
-    phy_config_sib2_ue(Mod_idP,0,eNB_index,radioResourceConfigCommon,ul_CarrierFreq,ul_Bandwidth,additionalSpectrumEmission,mbsfn_SubframeConfigList);
-  }
-
-  // SRB2_lchan_config->choice.explicitValue.ul_SpecificParameters->logicalChannelGroup
-  if (logicalChannelConfig!= NULL) {
-    LOG_I(MAC,"[CONFIG][UE %d] Applying RRC logicalChannelConfig from eNB%d\n",Mod_idP,eNB_index);
-    UE_mac_inst[Mod_idP].logicalChannelConfig[logicalChannelIdentity]=logicalChannelConfig;
-    UE_mac_inst[Mod_idP].scheduling_info.Bj[logicalChannelIdentity]=0; // initilize the bucket for this lcid
-    
-    AssertFatal(logicalChannelConfig->ul_SpecificParameters!=NULL,
-		"[UE %d] LCID %ld NULL ul_SpecificParameters\n",
-		Mod_idP,logicalChannelIdentity);
-    UE_mac_inst[Mod_idP].scheduling_info.bucket_size[logicalChannelIdentity]=logicalChannelConfig->ul_SpecificParameters->prioritisedBitRate *
-      logicalChannelConfig->ul_SpecificParameters->bucketSizeDuration; // set the max bucket size
-    if (logicalChannelConfig->ul_SpecificParameters->logicalChannelGroup != NULL) {
-      UE_mac_inst[Mod_idP].scheduling_info.LCGID[logicalChannelIdentity]=*logicalChannelConfig->ul_SpecificParameters->logicalChannelGroup;
-      LOG_D(MAC,"[CONFIG][UE %d] LCID %ld is attached to the LCGID %ld\n",Mod_idP,logicalChannelIdentity,*logicalChannelConfig->ul_SpecificParameters->logicalChannelGroup);
+    int i;
+
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_RRC_MAC_CONFIG, VCD_FUNCTION_IN);
+
+    LOG_I(MAC, "[CONFIG][UE %d] Configuring MAC/PHY from eNB %d\n",
+	  Mod_idP, eNB_index);
+
+    if (tdd_Config != NULL) {
+	UE_mac_inst[Mod_idP].tdd_Config = tdd_Config;
     }
-    else {
-      UE_mac_inst[Mod_idP].scheduling_info.LCGID[logicalChannelIdentity] = MAX_NUM_LCGID;
+
+
+    if (tdd_Config && SIwindowsize && SIperiod) {
+	phy_config_sib1_ue(Mod_idP, 0, eNB_index, tdd_Config,
+			   *SIwindowsize, *SIperiod);
     }
-    UE_mac_inst[Mod_idP].scheduling_info.LCID_buffer_remain[logicalChannelIdentity] = 0;
-  }
-
-  if (mac_MainConfig != NULL) {
-    LOG_I(MAC,"[CONFIG][UE%d] Applying RRC macMainConfig from eNB%d\n",Mod_idP,eNB_index);
-    UE_mac_inst[Mod_idP].macConfig=mac_MainConfig;
-    UE_mac_inst[Mod_idP].measGapConfig=measGapConfig;
-    
-    if (mac_MainConfig->ul_SCH_Config) {
-      
-      if (mac_MainConfig->ul_SCH_Config->periodicBSR_Timer) {
-	UE_mac_inst[Mod_idP].scheduling_info.periodicBSR_Timer = (uint16_t) *mac_MainConfig->ul_SCH_Config->periodicBSR_Timer;
-      } else {
-	UE_mac_inst[Mod_idP].scheduling_info.periodicBSR_Timer = 
+
+    if (radioResourceConfigCommon != NULL) {
+	UE_mac_inst[Mod_idP].radioResourceConfigCommon =
+	    radioResourceConfigCommon;
+	phy_config_sib2_ue(Mod_idP, 0, eNB_index,
+			   radioResourceConfigCommon, ul_CarrierFreq,
+			   ul_Bandwidth, additionalSpectrumEmission,
+			   mbsfn_SubframeConfigList);
+    }
+    // SRB2_lchan_config->choice.explicitValue.ul_SpecificParameters->logicalChannelGroup
+    if (logicalChannelConfig != NULL) {
+	LOG_I(MAC,
+	      "[CONFIG][UE %d] Applying RRC logicalChannelConfig from eNB%d\n",
+	      Mod_idP, eNB_index);
+	UE_mac_inst[Mod_idP].logicalChannelConfig[logicalChannelIdentity] =
+	    logicalChannelConfig;
+	UE_mac_inst[Mod_idP].scheduling_info.Bj[logicalChannelIdentity] = 0;	// initilize the bucket for this lcid
+
+	AssertFatal(logicalChannelConfig->ul_SpecificParameters != NULL,
+		    "[UE %d] LCID %ld NULL ul_SpecificParameters\n",
+		    Mod_idP, logicalChannelIdentity);
+	UE_mac_inst[Mod_idP].scheduling_info.bucket_size[logicalChannelIdentity] = logicalChannelConfig->ul_SpecificParameters->prioritisedBitRate * logicalChannelConfig->ul_SpecificParameters->bucketSizeDuration;	// set the max bucket size
+	if (logicalChannelConfig->ul_SpecificParameters->
+	    logicalChannelGroup != NULL) {
+	    UE_mac_inst[Mod_idP].scheduling_info.
+		LCGID[logicalChannelIdentity] =
+		*logicalChannelConfig->ul_SpecificParameters->
+		logicalChannelGroup;
+	    LOG_D(MAC,
+		  "[CONFIG][UE %d] LCID %ld is attached to the LCGID %ld\n",
+		  Mod_idP, logicalChannelIdentity,
+		  *logicalChannelConfig->
+		  ul_SpecificParameters->logicalChannelGroup);
+	} else {
+	    UE_mac_inst[Mod_idP].scheduling_info.
+		LCGID[logicalChannelIdentity] = MAX_NUM_LCGID;
+	}
+	UE_mac_inst[Mod_idP].
+	    scheduling_info.LCID_buffer_remain[logicalChannelIdentity] = 0;
+    }
+
+    if (mac_MainConfig != NULL) {
+	LOG_I(MAC,
+	      "[CONFIG][UE%d] Applying RRC macMainConfig from eNB%d\n",
+	      Mod_idP, eNB_index);
+	UE_mac_inst[Mod_idP].macConfig = mac_MainConfig;
+	UE_mac_inst[Mod_idP].measGapConfig = measGapConfig;
+
+	if (mac_MainConfig->ul_SCH_Config) {
+
+	    if (mac_MainConfig->ul_SCH_Config->periodicBSR_Timer) {
+		UE_mac_inst[Mod_idP].scheduling_info.periodicBSR_Timer =
+		    (uint16_t) *
+		    mac_MainConfig->ul_SCH_Config->periodicBSR_Timer;
+	    } else {
+		UE_mac_inst[Mod_idP].scheduling_info.periodicBSR_Timer =
 #ifndef Rel14
-	  (uint16_t) MAC_MainConfig__ul_SCH_Config__periodicBSR_Timer_infinity
+		    (uint16_t)
+		    MAC_MainConfig__ul_SCH_Config__periodicBSR_Timer_infinity
 #else
-	  (uint16_t) PeriodicBSR_Timer_r12_infinity;
+		    (uint16_t) PeriodicBSR_Timer_r12_infinity;
 #endif
-	  ;
-      }
-      
-      if (mac_MainConfig->ul_SCH_Config->maxHARQ_Tx) {
-	UE_mac_inst[Mod_idP].scheduling_info.maxHARQ_Tx     = (uint16_t) *mac_MainConfig->ul_SCH_Config->maxHARQ_Tx;
-      } else {
-	UE_mac_inst[Mod_idP].scheduling_info.maxHARQ_Tx     = (uint16_t) MAC_MainConfig__ul_SCH_Config__maxHARQ_Tx_n5;
-      }
-      phy_config_harq_ue(Mod_idP,0,eNB_index,UE_mac_inst[Mod_idP].scheduling_info.maxHARQ_Tx);
-      
-      if (mac_MainConfig->ul_SCH_Config->retxBSR_Timer) {
-	UE_mac_inst[Mod_idP].scheduling_info.retxBSR_Timer     = (uint16_t) mac_MainConfig->ul_SCH_Config->retxBSR_Timer;
-      } else {
+		;
+	    }
+
+	    if (mac_MainConfig->ul_SCH_Config->maxHARQ_Tx) {
+		UE_mac_inst[Mod_idP].scheduling_info.maxHARQ_Tx =
+		    (uint16_t) * mac_MainConfig->ul_SCH_Config->maxHARQ_Tx;
+	    } else {
+		UE_mac_inst[Mod_idP].scheduling_info.maxHARQ_Tx =
+		    (uint16_t)
+		    MAC_MainConfig__ul_SCH_Config__maxHARQ_Tx_n5;
+	    }
+	    phy_config_harq_ue(Mod_idP, 0, eNB_index,
+			       UE_mac_inst[Mod_idP].
+			       scheduling_info.maxHARQ_Tx);
+
+	    if (mac_MainConfig->ul_SCH_Config->retxBSR_Timer) {
+		UE_mac_inst[Mod_idP].scheduling_info.retxBSR_Timer =
+		    (uint16_t) mac_MainConfig->ul_SCH_Config->
+		    retxBSR_Timer;
+	    } else {
 #ifndef Rel14
-	UE_mac_inst[Mod_idP].scheduling_info.retxBSR_Timer     = (uint16_t)MAC_MainConfig__ul_SCH_Config__retxBSR_Timer_sf2560;
+		UE_mac_inst[Mod_idP].scheduling_info.retxBSR_Timer =
+		    (uint16_t)
+		    MAC_MainConfig__ul_SCH_Config__retxBSR_Timer_sf2560;
 #else
-	UE_mac_inst[Mod_idP].scheduling_info.retxBSR_Timer     = (uint16_t)RetxBSR_Timer_r12_sf2560;
+		UE_mac_inst[Mod_idP].scheduling_info.retxBSR_Timer =
+		    (uint16_t) RetxBSR_Timer_r12_sf2560;
 #endif
-      }
-    }
-
-
+	    }
+	}
 #if defined(Rel10) || defined(Rel14)
-    
-    if (mac_MainConfig->ext1 && mac_MainConfig->ext1->sr_ProhibitTimer_r9) {
-      UE_mac_inst[Mod_idP].scheduling_info.sr_ProhibitTimer  = (uint16_t) *mac_MainConfig->ext1->sr_ProhibitTimer_r9;
-    } else {
-      UE_mac_inst[Mod_idP].scheduling_info.sr_ProhibitTimer  = 0;
-    }
-    
-    if (mac_MainConfig->ext2 && mac_MainConfig->ext2->mac_MainConfig_v1020) {
-      if (mac_MainConfig->ext2->mac_MainConfig_v1020->extendedBSR_Sizes_r10) {
-	UE_mac_inst[Mod_idP].scheduling_info.extendedBSR_Sizes_r10 = (uint16_t) *mac_MainConfig->ext2->mac_MainConfig_v1020->extendedBSR_Sizes_r10;
-      } else {
-	UE_mac_inst[Mod_idP].scheduling_info.extendedBSR_Sizes_r10 = (uint16_t)0;
-      }
-      if (mac_MainConfig->ext2->mac_MainConfig_v1020->extendedPHR_r10) {
-	UE_mac_inst[Mod_idP].scheduling_info.extendedPHR_r10 = (uint16_t) *mac_MainConfig->ext2->mac_MainConfig_v1020->extendedPHR_r10;
-      } else {
-	UE_mac_inst[Mod_idP].scheduling_info.extendedPHR_r10 = (uint16_t)0;
-      }
-    } else {
-      UE_mac_inst[Mod_idP].scheduling_info.extendedBSR_Sizes_r10 = (uint16_t)0;
-      UE_mac_inst[Mod_idP].scheduling_info.extendedPHR_r10 = (uint16_t)0;
-    }
+
+	if (mac_MainConfig->ext1
+	    && mac_MainConfig->ext1->sr_ProhibitTimer_r9) {
+	    UE_mac_inst[Mod_idP].scheduling_info.sr_ProhibitTimer =
+		(uint16_t) * mac_MainConfig->ext1->sr_ProhibitTimer_r9;
+	} else {
+	    UE_mac_inst[Mod_idP].scheduling_info.sr_ProhibitTimer = 0;
+	}
+
+	if (mac_MainConfig->ext2
+	    && mac_MainConfig->ext2->mac_MainConfig_v1020) {
+	    if (mac_MainConfig->ext2->
+		mac_MainConfig_v1020->extendedBSR_Sizes_r10) {
+		UE_mac_inst[Mod_idP].scheduling_info.
+		    extendedBSR_Sizes_r10 =
+		    (uint16_t) *
+		    mac_MainConfig->ext2->
+		    mac_MainConfig_v1020->extendedBSR_Sizes_r10;
+	    } else {
+		UE_mac_inst[Mod_idP].scheduling_info.
+		    extendedBSR_Sizes_r10 = (uint16_t) 0;
+	    }
+	    if (mac_MainConfig->ext2->mac_MainConfig_v1020->
+		extendedPHR_r10) {
+		UE_mac_inst[Mod_idP].scheduling_info.extendedPHR_r10 =
+		    (uint16_t) *
+		    mac_MainConfig->ext2->mac_MainConfig_v1020->
+		    extendedPHR_r10;
+	    } else {
+		UE_mac_inst[Mod_idP].scheduling_info.extendedPHR_r10 =
+		    (uint16_t) 0;
+	    }
+	} else {
+	    UE_mac_inst[Mod_idP].scheduling_info.extendedBSR_Sizes_r10 =
+		(uint16_t) 0;
+	    UE_mac_inst[Mod_idP].scheduling_info.extendedPHR_r10 =
+		(uint16_t) 0;
+	}
 #endif
-    UE_mac_inst[Mod_idP].scheduling_info.periodicBSR_SF  = MAC_UE_BSR_TIMER_NOT_RUNNING;
-    UE_mac_inst[Mod_idP].scheduling_info.retxBSR_SF     = MAC_UE_BSR_TIMER_NOT_RUNNING;
-    
-    UE_mac_inst[Mod_idP].BSR_reporting_active = BSR_TRIGGER_NONE;
-    
-    LOG_D(MAC,"[UE %d]: periodic BSR %d (SF), retx BSR %d (SF)\n",
-	  Mod_idP,
-	  UE_mac_inst[Mod_idP].scheduling_info.periodicBSR_SF,
-	  UE_mac_inst[Mod_idP].scheduling_info.retxBSR_SF);
-    
-    UE_mac_inst[Mod_idP].scheduling_info.drx_config     = mac_MainConfig->drx_Config;
-    UE_mac_inst[Mod_idP].scheduling_info.phr_config     = mac_MainConfig->phr_Config;
-    
-    if (mac_MainConfig->phr_Config) {
-      UE_mac_inst[Mod_idP].PHR_state = mac_MainConfig->phr_Config->present;
-      UE_mac_inst[Mod_idP].PHR_reconfigured = 1;
-      UE_mac_inst[Mod_idP].scheduling_info.periodicPHR_Timer = mac_MainConfig->phr_Config->choice.setup.periodicPHR_Timer;
-      UE_mac_inst[Mod_idP].scheduling_info.prohibitPHR_Timer = mac_MainConfig->phr_Config->choice.setup.prohibitPHR_Timer;
-      UE_mac_inst[Mod_idP].scheduling_info.PathlossChange = mac_MainConfig->phr_Config->choice.setup.dl_PathlossChange;
-    } else {
-      UE_mac_inst[Mod_idP].PHR_reconfigured = 0;
-      UE_mac_inst[Mod_idP].PHR_state = MAC_MainConfig__phr_Config_PR_setup;
-      UE_mac_inst[Mod_idP].scheduling_info.periodicPHR_Timer = MAC_MainConfig__phr_Config__setup__periodicPHR_Timer_sf20;
-      UE_mac_inst[Mod_idP].scheduling_info.prohibitPHR_Timer = MAC_MainConfig__phr_Config__setup__prohibitPHR_Timer_sf20;
-      UE_mac_inst[Mod_idP].scheduling_info.PathlossChange = MAC_MainConfig__phr_Config__setup__dl_PathlossChange_dB1;
+	UE_mac_inst[Mod_idP].scheduling_info.periodicBSR_SF =
+	    MAC_UE_BSR_TIMER_NOT_RUNNING;
+	UE_mac_inst[Mod_idP].scheduling_info.retxBSR_SF =
+	    MAC_UE_BSR_TIMER_NOT_RUNNING;
+
+	UE_mac_inst[Mod_idP].BSR_reporting_active = BSR_TRIGGER_NONE;
+
+	LOG_D(MAC, "[UE %d]: periodic BSR %d (SF), retx BSR %d (SF)\n",
+	      Mod_idP,
+	      UE_mac_inst[Mod_idP].scheduling_info.periodicBSR_SF,
+	      UE_mac_inst[Mod_idP].scheduling_info.retxBSR_SF);
+
+	UE_mac_inst[Mod_idP].scheduling_info.drx_config =
+	    mac_MainConfig->drx_Config;
+	UE_mac_inst[Mod_idP].scheduling_info.phr_config =
+	    mac_MainConfig->phr_Config;
+
+	if (mac_MainConfig->phr_Config) {
+	    UE_mac_inst[Mod_idP].PHR_state =
+		mac_MainConfig->phr_Config->present;
+	    UE_mac_inst[Mod_idP].PHR_reconfigured = 1;
+	    UE_mac_inst[Mod_idP].scheduling_info.periodicPHR_Timer =
+		mac_MainConfig->phr_Config->choice.setup.periodicPHR_Timer;
+	    UE_mac_inst[Mod_idP].scheduling_info.prohibitPHR_Timer =
+		mac_MainConfig->phr_Config->choice.setup.prohibitPHR_Timer;
+	    UE_mac_inst[Mod_idP].scheduling_info.PathlossChange =
+		mac_MainConfig->phr_Config->choice.setup.dl_PathlossChange;
+	} else {
+	    UE_mac_inst[Mod_idP].PHR_reconfigured = 0;
+	    UE_mac_inst[Mod_idP].PHR_state =
+		MAC_MainConfig__phr_Config_PR_setup;
+	    UE_mac_inst[Mod_idP].scheduling_info.periodicPHR_Timer =
+		MAC_MainConfig__phr_Config__setup__periodicPHR_Timer_sf20;
+	    UE_mac_inst[Mod_idP].scheduling_info.prohibitPHR_Timer =
+		MAC_MainConfig__phr_Config__setup__prohibitPHR_Timer_sf20;
+	    UE_mac_inst[Mod_idP].scheduling_info.PathlossChange =
+		MAC_MainConfig__phr_Config__setup__dl_PathlossChange_dB1;
+	}
+
+	UE_mac_inst[Mod_idP].scheduling_info.periodicPHR_SF =
+	    get_sf_perioidicPHR_Timer(UE_mac_inst[Mod_idP].
+				      scheduling_info.periodicPHR_Timer);
+	UE_mac_inst[Mod_idP].scheduling_info.prohibitPHR_SF =
+	    get_sf_prohibitPHR_Timer(UE_mac_inst[Mod_idP].
+				     scheduling_info.prohibitPHR_Timer);
+	UE_mac_inst[Mod_idP].scheduling_info.PathlossChange_db =
+	    get_db_dl_PathlossChange(UE_mac_inst[Mod_idP].
+				     scheduling_info.PathlossChange);
+	UE_mac_inst[Mod_idP].PHR_reporting_active = 0;
+	LOG_D(MAC,
+	      "[UE %d] config PHR (%d): periodic %d (SF) prohibit %d (SF)  pathlosschange %d (db) \n",
+	      Mod_idP,
+	      (mac_MainConfig->phr_Config) ? mac_MainConfig->
+	      phr_Config->present : -1,
+	      UE_mac_inst[Mod_idP].scheduling_info.periodicPHR_SF,
+	      UE_mac_inst[Mod_idP].scheduling_info.prohibitPHR_SF,
+	      UE_mac_inst[Mod_idP].scheduling_info.PathlossChange_db);
     }
-    
-    UE_mac_inst[Mod_idP].scheduling_info.periodicPHR_SF =  get_sf_perioidicPHR_Timer(UE_mac_inst[Mod_idP].scheduling_info.periodicPHR_Timer);
-    UE_mac_inst[Mod_idP].scheduling_info.prohibitPHR_SF =  get_sf_prohibitPHR_Timer(UE_mac_inst[Mod_idP].scheduling_info.prohibitPHR_Timer);
-    UE_mac_inst[Mod_idP].scheduling_info.PathlossChange_db =  get_db_dl_PathlossChange(UE_mac_inst[Mod_idP].scheduling_info.PathlossChange);
-    UE_mac_inst[Mod_idP].PHR_reporting_active = 0;
-    LOG_D(MAC,"[UE %d] config PHR (%d): periodic %d (SF) prohibit %d (SF)  pathlosschange %d (db) \n",
-	  Mod_idP,
-	  (mac_MainConfig->phr_Config)?mac_MainConfig->phr_Config->present:-1,
-	  UE_mac_inst[Mod_idP].scheduling_info.periodicPHR_SF,
-	  UE_mac_inst[Mod_idP].scheduling_info.prohibitPHR_SF,
-	  UE_mac_inst[Mod_idP].scheduling_info.PathlossChange_db);
-  }
-
-
-  if (physicalConfigDedicated != NULL) {
-    phy_config_dedicated_ue(Mod_idP,0,eNB_index,physicalConfigDedicated);
-    UE_mac_inst[Mod_idP].physicalConfigDedicated=physicalConfigDedicated; // for SR proc
-  }
 
-#if defined(Rel10) || defined(Rel14)
 
-  if (sCellToAddMod_r10 != NULL) {
+    if (physicalConfigDedicated != NULL) {
+	phy_config_dedicated_ue(Mod_idP, 0, eNB_index,
+				physicalConfigDedicated);
+	UE_mac_inst[Mod_idP].physicalConfigDedicated = physicalConfigDedicated;	// for SR proc
+    }
+#if defined(Rel10) || defined(Rel14)
 
+    if (sCellToAddMod_r10 != NULL) {
 
-    phy_config_dedicated_scell_ue(Mod_idP,eNB_index,sCellToAddMod_r10,1);
-    UE_mac_inst[Mod_idP].physicalConfigDedicatedSCell_r10 = sCellToAddMod_r10->radioResourceConfigDedicatedSCell_r10->physicalConfigDedicatedSCell_r10; // using SCell index 0
-  }
-  
 
+	phy_config_dedicated_scell_ue(Mod_idP, eNB_index,
+				      sCellToAddMod_r10, 1);
+	UE_mac_inst[Mod_idP].physicalConfigDedicatedSCell_r10 = sCellToAddMod_r10->radioResourceConfigDedicatedSCell_r10->physicalConfigDedicatedSCell_r10;	// using SCell index 0
+    }
 #endif
 
-  if (measObj!= NULL) {
-    if (measObj[0]!= NULL) {
-      UE_mac_inst[Mod_idP].n_adj_cells = measObj[0]->measObject.choice.measObjectEUTRA.cellsToAddModList->list.count;
-      LOG_I(MAC,"Number of adjacent cells %d\n",UE_mac_inst[Mod_idP].n_adj_cells);
-      
-      for (i=0; i<UE_mac_inst[Mod_idP].n_adj_cells; i++) {
-	UE_mac_inst[Mod_idP].adj_cell_id[i] = measObj[0]->measObject.choice.measObjectEUTRA.cellsToAddModList->list.array[i]->physCellId;
-	LOG_I(MAC,"Cell %d : Nid_cell %d\n",i,UE_mac_inst[Mod_idP].adj_cell_id[i]);
-      }
-      
-      phy_config_meas_ue(Mod_idP,0,eNB_index,UE_mac_inst[Mod_idP].n_adj_cells,UE_mac_inst[Mod_idP].adj_cell_id);
-    }
-  }
-
-
-  if(mobilityControlInfo != NULL) {
-    
-    LOG_D(MAC,"[UE%d] MAC Reset procedure triggered by RRC eNB %d \n",Mod_idP,eNB_index);
-    ue_mac_reset(Mod_idP,eNB_index);
-    
-    if(mobilityControlInfo->radioResourceConfigCommon.rach_ConfigCommon) {
-      memcpy((void *)&UE_mac_inst[Mod_idP].radioResourceConfigCommon->rach_ConfigCommon,
-	     (void *)mobilityControlInfo->radioResourceConfigCommon.rach_ConfigCommon,
-	     sizeof(RACH_ConfigCommon_t));
-    }
-    
-    memcpy((void *)&UE_mac_inst[Mod_idP].radioResourceConfigCommon->prach_Config.prach_ConfigInfo,
-	   (void *)mobilityControlInfo->radioResourceConfigCommon.prach_Config.prach_ConfigInfo,
-	   sizeof(PRACH_ConfigInfo_t));
-    UE_mac_inst[Mod_idP].radioResourceConfigCommon->prach_Config.rootSequenceIndex = mobilityControlInfo->radioResourceConfigCommon.prach_Config.rootSequenceIndex;
-    
-    if(mobilityControlInfo->radioResourceConfigCommon.pdsch_ConfigCommon) {
-      memcpy((void *)&UE_mac_inst[Mod_idP].radioResourceConfigCommon->pdsch_ConfigCommon,
-	     (void *)mobilityControlInfo->radioResourceConfigCommon.pdsch_ConfigCommon,
-	     sizeof(PDSCH_ConfigCommon_t));
-    }
-    
-    // not a pointer: mobilityControlInfo->radioResourceConfigCommon.pusch_ConfigCommon
-    memcpy((void *)&UE_mac_inst[Mod_idP].radioResourceConfigCommon->pusch_ConfigCommon,
-	   (void *)&mobilityControlInfo->radioResourceConfigCommon.pusch_ConfigCommon,
-	   sizeof(PUSCH_ConfigCommon_t));
-    
-    if(mobilityControlInfo->radioResourceConfigCommon.phich_Config) {
-      /* memcpy((void *)&UE_mac_inst[Mod_idP].radioResourceConfigCommon->phich_Config,
-	 (void *)mobilityControlInfo->radioResourceConfigCommon.phich_Config,
-	 sizeof(PHICH_Config_t)); */
-    }
-    
-    if(mobilityControlInfo->radioResourceConfigCommon.pucch_ConfigCommon) {
-      memcpy((void *)&UE_mac_inst[Mod_idP].radioResourceConfigCommon->pucch_ConfigCommon,
-	     (void *)mobilityControlInfo->radioResourceConfigCommon.pucch_ConfigCommon,
-	     sizeof(PUCCH_ConfigCommon_t));
-    }
-    
-    if(mobilityControlInfo->radioResourceConfigCommon.soundingRS_UL_ConfigCommon) {
-      memcpy((void *)&UE_mac_inst[Mod_idP].radioResourceConfigCommon->soundingRS_UL_ConfigCommon,
-	     (void *)mobilityControlInfo->radioResourceConfigCommon.soundingRS_UL_ConfigCommon,
-	     sizeof(SoundingRS_UL_ConfigCommon_t));
-    }
-    
-    if(mobilityControlInfo->radioResourceConfigCommon.uplinkPowerControlCommon) {
-      memcpy((void *)&UE_mac_inst[Mod_idP].radioResourceConfigCommon->uplinkPowerControlCommon,
-	     (void *)mobilityControlInfo->radioResourceConfigCommon.uplinkPowerControlCommon,
-	     sizeof(UplinkPowerControlCommon_t));
-    }
-    
-    //configure antennaInfoCommon somewhere here..
-    if(mobilityControlInfo->radioResourceConfigCommon.p_Max) {
-      //to be configured
-    }
-    
-    if(mobilityControlInfo->radioResourceConfigCommon.tdd_Config) {
-      UE_mac_inst[Mod_idP].tdd_Config = mobilityControlInfo->radioResourceConfigCommon.tdd_Config;
-    }
-    
-    if(mobilityControlInfo->radioResourceConfigCommon.ul_CyclicPrefixLength) {
-      memcpy((void *)&UE_mac_inst[Mod_idP].radioResourceConfigCommon->ul_CyclicPrefixLength,
-	     (void *)mobilityControlInfo->radioResourceConfigCommon.ul_CyclicPrefixLength,
-	     sizeof(UL_CyclicPrefixLength_t));
-    }
-    
-    // store the previous rnti in case of failure, and set thenew rnti
-    UE_mac_inst[Mod_idP].crnti_before_ho = UE_mac_inst[Mod_idP].crnti;
-    UE_mac_inst[Mod_idP].crnti = ((mobilityControlInfo->newUE_Identity.buf[0])|(mobilityControlInfo->newUE_Identity.buf[1]<<8));
-    LOG_I(MAC,"[UE %d] Received new identity %x from %d\n", Mod_idP, UE_mac_inst[Mod_idP].crnti, eNB_index);
-    UE_mac_inst[Mod_idP].rach_ConfigDedicated = malloc(sizeof(*mobilityControlInfo->rach_ConfigDedicated));
-    
-    if (mobilityControlInfo->rach_ConfigDedicated) {
-      memcpy((void*)UE_mac_inst[Mod_idP].rach_ConfigDedicated,
-	     (void*)mobilityControlInfo->rach_ConfigDedicated,
-	     sizeof(*mobilityControlInfo->rach_ConfigDedicated));
+    if (measObj != NULL) {
+	if (measObj[0] != NULL) {
+	    UE_mac_inst[Mod_idP].n_adj_cells =
+		measObj[0]->measObject.choice.
+		measObjectEUTRA.cellsToAddModList->list.count;
+	    LOG_I(MAC, "Number of adjacent cells %d\n",
+		  UE_mac_inst[Mod_idP].n_adj_cells);
+
+	    for (i = 0; i < UE_mac_inst[Mod_idP].n_adj_cells; i++) {
+		UE_mac_inst[Mod_idP].adj_cell_id[i] =
+		    measObj[0]->measObject.choice.
+		    measObjectEUTRA.cellsToAddModList->list.array[i]->
+		    physCellId;
+		LOG_I(MAC, "Cell %d : Nid_cell %d\n", i,
+		      UE_mac_inst[Mod_idP].adj_cell_id[i]);
+	    }
+
+	    phy_config_meas_ue(Mod_idP, 0, eNB_index,
+			       UE_mac_inst[Mod_idP].n_adj_cells,
+			       UE_mac_inst[Mod_idP].adj_cell_id);
+	}
     }
-    
-    phy_config_afterHO_ue(Mod_idP,0,eNB_index,mobilityControlInfo,0);
-  }
-
-
-  if (mbsfn_SubframeConfigList != NULL) {
-    LOG_I(MAC,"[UE %d][CONFIG] Received %d subframe allocation pattern for MBSFN\n", Mod_idP, mbsfn_SubframeConfigList->list.count);
-    UE_mac_inst[Mod_idP].num_sf_allocation_pattern= mbsfn_SubframeConfigList->list.count;
-    
-    for (i=0; i<mbsfn_SubframeConfigList->list.count; i++) {
-      LOG_I(MAC, "[UE %d] Configuring MBSFN_SubframeConfig %d from received SIB2 \n", Mod_idP, i);
-      UE_mac_inst[Mod_idP].mbsfn_SubframeConfig[i] = mbsfn_SubframeConfigList->list.array[i];
-      //  LOG_I("[UE %d] MBSFN_SubframeConfig[%d] pattern is  %ld\n", Mod_idP,
-      //    UE_mac_inst[Mod_idP].mbsfn_SubframeConfig[i]->subframeAllocation.choice.oneFrame.buf[0]);
+
+
+    if (mobilityControlInfo != NULL) {
+
+	LOG_D(MAC, "[UE%d] MAC Reset procedure triggered by RRC eNB %d \n",
+	      Mod_idP, eNB_index);
+	ue_mac_reset(Mod_idP, eNB_index);
+
+	if (mobilityControlInfo->radioResourceConfigCommon.
+	    rach_ConfigCommon) {
+	    memcpy((void *)
+		   &UE_mac_inst[Mod_idP].radioResourceConfigCommon->
+		   rach_ConfigCommon,
+		   (void *) mobilityControlInfo->
+		   radioResourceConfigCommon.rach_ConfigCommon,
+		   sizeof(RACH_ConfigCommon_t));
+	}
+
+	memcpy((void *) &UE_mac_inst[Mod_idP].
+	       radioResourceConfigCommon->prach_Config.prach_ConfigInfo,
+	       (void *) mobilityControlInfo->
+	       radioResourceConfigCommon.prach_Config.prach_ConfigInfo,
+	       sizeof(PRACH_ConfigInfo_t));
+	UE_mac_inst[Mod_idP].radioResourceConfigCommon->
+	    prach_Config.rootSequenceIndex =
+	    mobilityControlInfo->radioResourceConfigCommon.
+	    prach_Config.rootSequenceIndex;
+
+	if (mobilityControlInfo->radioResourceConfigCommon.
+	    pdsch_ConfigCommon) {
+	    memcpy((void *)
+		   &UE_mac_inst[Mod_idP].radioResourceConfigCommon->
+		   pdsch_ConfigCommon,
+		   (void *) mobilityControlInfo->
+		   radioResourceConfigCommon.pdsch_ConfigCommon,
+		   sizeof(PDSCH_ConfigCommon_t));
+	}
+	// not a pointer: mobilityControlInfo->radioResourceConfigCommon.pusch_ConfigCommon
+	memcpy((void *) &UE_mac_inst[Mod_idP].
+	       radioResourceConfigCommon->pusch_ConfigCommon,
+	       (void *) &mobilityControlInfo->
+	       radioResourceConfigCommon.pusch_ConfigCommon,
+	       sizeof(PUSCH_ConfigCommon_t));
+
+	if (mobilityControlInfo->radioResourceConfigCommon.phich_Config) {
+	    /* memcpy((void *)&UE_mac_inst[Mod_idP].radioResourceConfigCommon->phich_Config,
+	       (void *)mobilityControlInfo->radioResourceConfigCommon.phich_Config,
+	       sizeof(PHICH_Config_t)); */
+	}
+
+	if (mobilityControlInfo->radioResourceConfigCommon.
+	    pucch_ConfigCommon) {
+	    memcpy((void *)
+		   &UE_mac_inst[Mod_idP].radioResourceConfigCommon->
+		   pucch_ConfigCommon,
+		   (void *) mobilityControlInfo->
+		   radioResourceConfigCommon.pucch_ConfigCommon,
+		   sizeof(PUCCH_ConfigCommon_t));
+	}
+
+	if (mobilityControlInfo->
+	    radioResourceConfigCommon.soundingRS_UL_ConfigCommon) {
+	    memcpy((void *)
+		   &UE_mac_inst[Mod_idP].radioResourceConfigCommon->
+		   soundingRS_UL_ConfigCommon,
+		   (void *) mobilityControlInfo->
+		   radioResourceConfigCommon.soundingRS_UL_ConfigCommon,
+		   sizeof(SoundingRS_UL_ConfigCommon_t));
+	}
+
+	if (mobilityControlInfo->
+	    radioResourceConfigCommon.uplinkPowerControlCommon) {
+	    memcpy((void *)
+		   &UE_mac_inst[Mod_idP].radioResourceConfigCommon->
+		   uplinkPowerControlCommon,
+		   (void *) mobilityControlInfo->
+		   radioResourceConfigCommon.uplinkPowerControlCommon,
+		   sizeof(UplinkPowerControlCommon_t));
+	}
+	//configure antennaInfoCommon somewhere here..
+	if (mobilityControlInfo->radioResourceConfigCommon.p_Max) {
+	    //to be configured
+	}
+
+	if (mobilityControlInfo->radioResourceConfigCommon.tdd_Config) {
+	    UE_mac_inst[Mod_idP].tdd_Config =
+		mobilityControlInfo->radioResourceConfigCommon.tdd_Config;
+	}
+
+	if (mobilityControlInfo->
+	    radioResourceConfigCommon.ul_CyclicPrefixLength) {
+	    memcpy((void *)
+		   &UE_mac_inst[Mod_idP].radioResourceConfigCommon->
+		   ul_CyclicPrefixLength,
+		   (void *) mobilityControlInfo->
+		   radioResourceConfigCommon.ul_CyclicPrefixLength,
+		   sizeof(UL_CyclicPrefixLength_t));
+	}
+	// store the previous rnti in case of failure, and set thenew rnti
+	UE_mac_inst[Mod_idP].crnti_before_ho = UE_mac_inst[Mod_idP].crnti;
+	UE_mac_inst[Mod_idP].crnti =
+	    ((mobilityControlInfo->
+	      newUE_Identity.buf[0]) | (mobilityControlInfo->
+					newUE_Identity.buf[1] << 8));
+	LOG_I(MAC, "[UE %d] Received new identity %x from %d\n", Mod_idP,
+	      UE_mac_inst[Mod_idP].crnti, eNB_index);
+	UE_mac_inst[Mod_idP].rach_ConfigDedicated =
+	    malloc(sizeof(*mobilityControlInfo->rach_ConfigDedicated));
+
+	if (mobilityControlInfo->rach_ConfigDedicated) {
+	    memcpy((void *) UE_mac_inst[Mod_idP].rach_ConfigDedicated,
+		   (void *) mobilityControlInfo->rach_ConfigDedicated,
+		   sizeof(*mobilityControlInfo->rach_ConfigDedicated));
+	}
+
+	phy_config_afterHO_ue(Mod_idP, 0, eNB_index, mobilityControlInfo,
+			      0);
     }
-  }
-  
 
-#if defined(Rel10) || defined(Rel14)
 
-  if (mbsfn_AreaInfoList != NULL) {
-    LOG_I(MAC,"[UE %d][CONFIG] Received %d MBSFN Area Info\n", Mod_idP, mbsfn_AreaInfoList->list.count);
-    UE_mac_inst[Mod_idP].num_active_mbsfn_area = mbsfn_AreaInfoList->list.count;
-    
-    for (i =0; i< mbsfn_AreaInfoList->list.count; i++) {
-      UE_mac_inst[Mod_idP].mbsfn_AreaInfo[i] = mbsfn_AreaInfoList->list.array[i];
-      LOG_I(MAC,"[UE %d] MBSFN_AreaInfo[%d]: MCCH Repetition Period = %ld\n",Mod_idP, i,
-	    UE_mac_inst[Mod_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_RepetitionPeriod_r9);
-      phy_config_sib13_ue(Mod_idP,0,eNB_index,i,UE_mac_inst[Mod_idP].mbsfn_AreaInfo[i]->mbsfn_AreaId_r9);
+    if (mbsfn_SubframeConfigList != NULL) {
+	LOG_I(MAC,
+	      "[UE %d][CONFIG] Received %d subframe allocation pattern for MBSFN\n",
+	      Mod_idP, mbsfn_SubframeConfigList->list.count);
+	UE_mac_inst[Mod_idP].num_sf_allocation_pattern =
+	    mbsfn_SubframeConfigList->list.count;
+
+	for (i = 0; i < mbsfn_SubframeConfigList->list.count; i++) {
+	    LOG_I(MAC,
+		  "[UE %d] Configuring MBSFN_SubframeConfig %d from received SIB2 \n",
+		  Mod_idP, i);
+	    UE_mac_inst[Mod_idP].mbsfn_SubframeConfig[i] =
+		mbsfn_SubframeConfigList->list.array[i];
+	    //  LOG_I("[UE %d] MBSFN_SubframeConfig[%d] pattern is  %ld\n", Mod_idP,
+	    //    UE_mac_inst[Mod_idP].mbsfn_SubframeConfig[i]->subframeAllocation.choice.oneFrame.buf[0]);
+	}
     }
-  }
-  
-  if (pmch_InfoList != NULL) {
-    
-    //    LOG_I(MAC,"DUY: lcid when entering rrc_mac config_req is %02d\n",(pmch_InfoList->list.array[0]->mbms_SessionInfoList_r9.list.array[0]->logicalChannelIdentity_r9));
-
-    LOG_I(MAC, "[UE %d] Configuring PMCH_config from MCCH MESSAGE \n",Mod_idP);
-    
-    for (i =0; i< pmch_InfoList->list.count; i++) {
-      UE_mac_inst[Mod_idP].pmch_Config[i] = &pmch_InfoList->list.array[i]->pmch_Config_r9;
-      LOG_I(MAC, "[UE %d] PMCH[%d]: MCH_Scheduling_Period = %ld\n", Mod_idP, i,
-	    UE_mac_inst[Mod_idP].pmch_Config[i]->mch_SchedulingPeriod_r9);
+#if defined(Rel10) || defined(Rel14)
+
+    if (mbsfn_AreaInfoList != NULL) {
+	LOG_I(MAC, "[UE %d][CONFIG] Received %d MBSFN Area Info\n",
+	      Mod_idP, mbsfn_AreaInfoList->list.count);
+	UE_mac_inst[Mod_idP].num_active_mbsfn_area =
+	    mbsfn_AreaInfoList->list.count;
+
+	for (i = 0; i < mbsfn_AreaInfoList->list.count; i++) {
+	    UE_mac_inst[Mod_idP].mbsfn_AreaInfo[i] =
+		mbsfn_AreaInfoList->list.array[i];
+	    LOG_I(MAC,
+		  "[UE %d] MBSFN_AreaInfo[%d]: MCCH Repetition Period = %ld\n",
+		  Mod_idP, i,
+		  UE_mac_inst[Mod_idP].mbsfn_AreaInfo[i]->
+		  mcch_Config_r9.mcch_RepetitionPeriod_r9);
+	    phy_config_sib13_ue(Mod_idP, 0, eNB_index, i,
+				UE_mac_inst[Mod_idP].
+				mbsfn_AreaInfo[i]->mbsfn_AreaId_r9);
+	}
     }
-    
-    UE_mac_inst[Mod_idP].mcch_status = 1;
-  }
 
+    if (pmch_InfoList != NULL) {
+
+	//    LOG_I(MAC,"DUY: lcid when entering rrc_mac config_req is %02d\n",(pmch_InfoList->list.array[0]->mbms_SessionInfoList_r9.list.array[0]->logicalChannelIdentity_r9));
 
+	LOG_I(MAC, "[UE %d] Configuring PMCH_config from MCCH MESSAGE \n",
+	      Mod_idP);
+
+	for (i = 0; i < pmch_InfoList->list.count; i++) {
+	    UE_mac_inst[Mod_idP].pmch_Config[i] =
+		&pmch_InfoList->list.array[i]->pmch_Config_r9;
+	    LOG_I(MAC, "[UE %d] PMCH[%d]: MCH_Scheduling_Period = %ld\n",
+		  Mod_idP, i,
+		  UE_mac_inst[Mod_idP].
+		  pmch_Config[i]->mch_SchedulingPeriod_r9);
+	}
+
+	UE_mac_inst[Mod_idP].mcch_status = 1;
+    }
 #endif
 #ifdef CBA
 
-  if (cba_rnti) {
-    UE_mac_inst[Mod_idP].cba_rnti[num_active_cba_groups-1] = cba_rnti;
-    LOG_D(MAC,"[UE %d] configure CBA group %d RNTI %x for eNB %d (total active cba group %d)\n",
-	  Mod_idP,Mod_idP%num_active_cba_groups, cba_rnti,eNB_index,num_active_cba_groups);
-    phy_config_cba_rnti(Mod_idP,CC_idP,eNB_flagP,eNB_index,cba_rnti,num_active_cba_groups-1, num_active_cba_groups);
-  }
+    if (cba_rnti) {
+	UE_mac_inst[Mod_idP].cba_rnti[num_active_cba_groups - 1] =
+	    cba_rnti;
+	LOG_D(MAC,
+	      "[UE %d] configure CBA group %d RNTI %x for eNB %d (total active cba group %d)\n",
+	      Mod_idP, Mod_idP % num_active_cba_groups, cba_rnti,
+	      eNB_index, num_active_cba_groups);
+	phy_config_cba_rnti(Mod_idP, CC_idP, eNB_flagP, eNB_index,
+			    cba_rnti, num_active_cba_groups - 1,
+			    num_active_cba_groups);
+    }
 #endif
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_RRC_MAC_CONFIG, VCD_FUNCTION_OUT);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_RRC_MAC_CONFIG, VCD_FUNCTION_OUT);
 
-  return(0);
+    return (0);
 }
-
diff --git a/openair2/LAYER2/MAC/defs.h b/openair2/LAYER2/MAC/defs.h
index ba02a3569419dbb1afb07b3e4bbe157abfade4ae..65e090e370ec5660cec4449ddff0c531a6c2249c 100644
--- a/openair2/LAYER2/MAC/defs.h
+++ b/openair2/LAYER2/MAC/defs.h
@@ -90,12 +90,12 @@
 #if defined(Rel10) || defined(Rel14)
 
 // Mask for identifying subframe for MBMS
-#define MBSFN_TDD_SF3 0x80// for TDD
+#define MBSFN_TDD_SF3 0x80	// for TDD
 #define MBSFN_TDD_SF4 0x40
 #define MBSFN_TDD_SF7 0x20
 #define MBSFN_TDD_SF8 0x10
 #define MBSFN_TDD_SF9 0x08
-#define MBSFN_FDD_SF1 0x80// for FDD
+#define MBSFN_FDD_SF1 0x80	// for FDD
 #define MBSFN_FDD_SF2 0x40
 #define MBSFN_FDD_SF3 0x20
 #define MBSFN_FDD_SF6 0x10
@@ -111,7 +111,7 @@
 
 #ifdef USER_MODE
 #define printk printf
-#endif //USER_MODE
+#endif				//USER_MODE
 
 /*!\brief Maximum number of logical channl group IDs */
 #define MAX_NUM_LCGID 4
@@ -132,17 +132,17 @@
 /*!\brief size of buffer status report table */
 #define BSR_TABLE_SIZE 64
 /*!\brief The power headroom reporting range is from -23 ...+40 dB and beyond, with step 1 */
-#define PHR_MAPPING_OFFSET 23  // if ( x>= -23 ) val = floor (x + 23) 
+#define PHR_MAPPING_OFFSET 23	// if ( x>= -23 ) val = floor (x + 23)
 /*!\brief maximum number of resource block groups */
-#define N_RBG_MAX 25 // for 20MHz channel BW
+#define N_RBG_MAX 25		// for 20MHz channel BW
 /*!\brief minimum value for channel quality indicator */
 #define MIN_CQI_VALUE  0
 /*!\brief maximum value for channel quality indicator */
 #define MAX_CQI_VALUE  15
 /*!\briefmaximum number of supported bandwidth (1.4, 5, 10, 20 MHz) */
-#define MAX_SUPPORTED_BW  4  
+#define MAX_SUPPORTED_BW  4
 /*!\brief CQI values range from 1 to 15 (4 bits) */
-#define CQI_VALUE_RANGE 16 
+#define CQI_VALUE_RANGE 16
 
 /*!\brief value for indicating BSR Timer is not running */
 #define MAC_UE_BSR_TIMER_NOT_RUNNING   (0xFFFF)
@@ -157,30 +157,30 @@
 #define MIN_MAC_HDR_RLC_SIZE    (1 + MIN_RLC_PDU_SIZE)
 
 /*!\brief maximum number of slices / groups */
-#define MAX_NUM_SLICES 4 
+#define MAX_NUM_SLICES 4
 
 /* 
  * eNB part 
- */ 
+ */
 
 
 /* 
  * UE/ENB common part 
- */ 
+ */
 /*!\brief MAC header of Random Access Response for Random access preamble identifier (RAPID) */
 typedef struct {
-  uint8_t RAPID:6;
-  uint8_t T:1;
-  uint8_t E:1;
-} __attribute__((__packed__))RA_HEADER_RAPID;
+    uint8_t RAPID:6;
+    uint8_t T:1;
+    uint8_t E:1;
+} __attribute__ ((__packed__)) RA_HEADER_RAPID;
 
 /*!\brief  MAC header of Random Access Response for backoff indicator (BI)*/
 typedef struct {
-  uint8_t BI:4;
-  uint8_t R:2;
-  uint8_t T:1;
-  uint8_t E:1;
-} __attribute__((__packed__))RA_HEADER_BI;
+    uint8_t BI:4;
+    uint8_t R:2;
+    uint8_t T:1;
+    uint8_t E:1;
+} __attribute__ ((__packed__)) RA_HEADER_BI;
 /*
 typedef struct {
   uint64_t padding:16;
@@ -212,124 +212,124 @@ typedef struct {
 */
 /*!\brief  MAC subheader short with 7bit Length field */
 typedef struct {
-  uint8_t LCID:5;  // octet 1 LSB
-  uint8_t E:1;
-  uint8_t R:2;     // octet 1 MSB
-  uint8_t L:7;     // octet 2 LSB
-  uint8_t F:1;     // octet 2 MSB
-} __attribute__((__packed__))SCH_SUBHEADER_SHORT;
+    uint8_t LCID:5;		// octet 1 LSB
+    uint8_t E:1;
+    uint8_t R:2;		// octet 1 MSB
+    uint8_t L:7;		// octet 2 LSB
+    uint8_t F:1;		// octet 2 MSB
+} __attribute__ ((__packed__)) SCH_SUBHEADER_SHORT;
 /*!\brief  MAC subheader long  with 15bit Length field */
 typedef struct {
-  uint8_t LCID:5;   // octet 1 LSB
-  uint8_t E:1;
-  uint8_t R:2;      // octet 1 MSB
-  uint8_t L_MSB:7;
-  uint8_t F:1;      // octet 2 MSB
-  uint8_t L_LSB:8;
-  uint8_t padding;
-} __attribute__((__packed__))SCH_SUBHEADER_LONG;
+    uint8_t LCID:5;		// octet 1 LSB
+    uint8_t E:1;
+    uint8_t R:2;		// octet 1 MSB
+    uint8_t L_MSB:7;
+    uint8_t F:1;		// octet 2 MSB
+    uint8_t L_LSB:8;
+    uint8_t padding;
+} __attribute__ ((__packed__)) SCH_SUBHEADER_LONG;
 /*!\brief MAC subheader short without length field */
 typedef struct {
-  uint8_t LCID:5;
-  uint8_t E:1;
-  uint8_t R:2;
-} __attribute__((__packed__))SCH_SUBHEADER_FIXED;
+    uint8_t LCID:5;
+    uint8_t E:1;
+    uint8_t R:2;
+} __attribute__ ((__packed__)) SCH_SUBHEADER_FIXED;
 
 /*!\brief  mac control element: short buffer status report for a specific logical channel group ID*/
 typedef struct {
-  uint8_t Buffer_size:6;  // octet 1 LSB
-  uint8_t LCGID:2;        // octet 1 MSB
-} __attribute__((__packed__))BSR_SHORT;
+    uint8_t Buffer_size:6;	// octet 1 LSB
+    uint8_t LCGID:2;		// octet 1 MSB
+} __attribute__ ((__packed__)) BSR_SHORT;
 
 typedef BSR_SHORT BSR_TRUNCATED;
 /*!\brief  mac control element: long buffer status report for all logical channel group ID*/
 typedef struct {
-  uint8_t Buffer_size3:6;
-  uint8_t Buffer_size2:6;
-  uint8_t Buffer_size1:6;
-  uint8_t Buffer_size0:6;
-} __attribute__((__packed__))BSR_LONG;
+    uint8_t Buffer_size3:6;
+    uint8_t Buffer_size2:6;
+    uint8_t Buffer_size1:6;
+    uint8_t Buffer_size0:6;
+} __attribute__ ((__packed__)) BSR_LONG;
 
 #define BSR_LONG_SIZE  (sizeof(BSR_LONG))
 /*!\brief  mac control element: timing advance  */
 typedef struct {
-  uint8_t TA:6;
-  uint8_t R:2;
-} __attribute__((__packed__))TIMING_ADVANCE_CMD;
+    uint8_t TA:6;
+    uint8_t R:2;
+} __attribute__ ((__packed__)) TIMING_ADVANCE_CMD;
 /*!\brief  mac control element: power headroom report  */
 typedef struct {
-  uint8_t PH:6;
-  uint8_t R:2;
-} __attribute__((__packed__))POWER_HEADROOM_CMD;
+    uint8_t PH:6;
+    uint8_t R:2;
+} __attribute__ ((__packed__)) POWER_HEADROOM_CMD;
 
 /*! \brief MIB payload */
 typedef struct {
-  uint8_t payload[3] ;
-} __attribute__((__packed__))MIB_PDU;
+    uint8_t payload[3];
+} __attribute__ ((__packed__)) MIB_PDU;
 /*! \brief CCCH payload */
 typedef struct {
-  uint8_t payload[CCCH_PAYLOAD_SIZE_MAX] ;
-} __attribute__((__packed__))CCCH_PDU;
+    uint8_t payload[CCCH_PAYLOAD_SIZE_MAX];
+} __attribute__ ((__packed__)) CCCH_PDU;
 /*! \brief BCCH payload */
 typedef struct {
-  uint8_t payload[BCCH_PAYLOAD_SIZE_MAX] ;
-} __attribute__((__packed__))BCCH_PDU;
+    uint8_t payload[BCCH_PAYLOAD_SIZE_MAX];
+} __attribute__ ((__packed__)) BCCH_PDU;
 /*! \brief RAR payload */
 typedef struct {
-  uint8_t payload[RAR_PAYLOAD_SIZE_MAX];
+    uint8_t payload[RAR_PAYLOAD_SIZE_MAX];
 } __attribute__ ((__packed__)) RAR_PDU;
 /*! \brief BCCH payload */
 typedef struct {
-  uint8_t payload[PCCH_PAYLOAD_SIZE_MAX] ;
-} __attribute__((__packed__))PCCH_PDU;
+    uint8_t payload[PCCH_PAYLOAD_SIZE_MAX];
+} __attribute__ ((__packed__)) PCCH_PDU;
 
 #if defined(Rel10) || defined(Rel14)
 /*! \brief MCCH payload */
 typedef struct {
-  uint8_t payload[MCCH_PAYLOAD_SIZE_MAX] ;
-} __attribute__((__packed__))MCCH_PDU;
+    uint8_t payload[MCCH_PAYLOAD_SIZE_MAX];
+} __attribute__ ((__packed__)) MCCH_PDU;
 /*!< \brief MAC control element for activation and deactivation of component carriers */
 typedef struct {
-  uint8_t C7:1;/*!< \brief Component carrier 7 */
-  uint8_t C6:1;/*!< \brief Component carrier 6 */
-  uint8_t C5:1;/*!< \brief Component carrier 5 */
-  uint8_t C4:1;/*!< \brief Component carrier 4 */
-  uint8_t C3:1;/*!< \brief Component carrier 3 */
-  uint8_t C2:1;/*!< \brief Component carrier 2 */
-  uint8_t C1:1;/*!< \brief Component carrier 1 */
-  uint8_t R:1;/*!< \brief Reserved  */
-} __attribute__((__packed__))CC_ELEMENT;
+    uint8_t C7:1;		/*!< \brief Component carrier 7 */
+    uint8_t C6:1;		/*!< \brief Component carrier 6 */
+    uint8_t C5:1;		/*!< \brief Component carrier 5 */
+    uint8_t C4:1;		/*!< \brief Component carrier 4 */
+    uint8_t C3:1;		/*!< \brief Component carrier 3 */
+    uint8_t C2:1;		/*!< \brief Component carrier 2 */
+    uint8_t C1:1;		/*!< \brief Component carrier 1 */
+    uint8_t R:1;		/*!< \brief Reserved  */
+} __attribute__ ((__packed__)) CC_ELEMENT;
 /*! \brief MAC control element: MCH Scheduling Information */
 typedef struct {
-  uint8_t stop_sf_MSB:3; // octet 1 LSB
-  uint8_t lcid:5;        // octet 2 MSB
-  uint8_t stop_sf_LSB:8;
-} __attribute__((__packed__))MSI_ELEMENT;
+    uint8_t stop_sf_MSB:3;	// octet 1 LSB
+    uint8_t lcid:5;		// octet 2 MSB
+    uint8_t stop_sf_LSB:8;
+} __attribute__ ((__packed__)) MSI_ELEMENT;
 #endif
-/*! \brief Values of CCCH LCID for DLSCH */ 
+/*! \brief Values of CCCH LCID for DLSCH */
 #define CCCH_LCHANID 0
 /*!\brief Values of BCCH logical channel (fake)*/
-#define BCCH 3  // SI 
+#define BCCH 3			// SI
 /*!\brief Values of PCCH logical channel (fake)*/
-#define PCCH 4  // Paging 
+#define PCCH 4			// Paging
 /*!\brief Values of PCCH logical channel (fake) */
-#define MIBCH 5  // MIB 
+#define MIBCH 5			// MIB
 /*!\brief Values of BCCH SIB1_BR logical channel (fake) */
-#define BCCH_SIB1_BR 6  // SIB1_BR 
+#define BCCH_SIB1_BR 6		// SIB1_BR
 /*!\brief Values of BCCH SIB_BR logical channel (fake) */
-#define BCCH_SI_BR 7  // SI-BR 
+#define BCCH_SI_BR 7		// SI-BR
 /*!\brief Value of CCCH / SRB0 logical channel */
-#define CCCH 0  // srb0
+#define CCCH 0			// srb0
 /*!\brief DCCH / SRB1 logical channel */
-#define DCCH 1  // srb1
+#define DCCH 1			// srb1
 /*!\brief DCCH1 / SRB2  logical channel */
-#define DCCH1 2 // srb2
+#define DCCH1 2			// srb2
 /*!\brief DTCH DRB1  logical channel */
-#define DTCH 3 // LCID
+#define DTCH 3			// LCID
 /*!\brief MCCH logical channel */
-#define MCCH 4 
+#define MCCH 4
 /*!\brief MTCH logical channel */
-#define MTCH 1 
+#define MTCH 1
 // DLSCH LCHAN ID
 /*!\brief LCID of UE contention resolution identity for DLSCH*/
 #define UE_CONT_RES 28
@@ -364,972 +364,963 @@ typedef struct {
 /*!\brief LCID of long BSR for ULSCH */
 #define LONG_BSR 30
 /*!\bitmaps for BSR Triggers */
-#define	BSR_TRIGGER_NONE		(0)			/* No BSR Trigger */
-#define	BSR_TRIGGER_REGULAR		(1)			/* For Regular and ReTxBSR Expiry Triggers */
-#define	BSR_TRIGGER_PERIODIC	(2)			/* For BSR Periodic Timer Expiry Trigger */
-#define	BSR_TRIGGER_PADDING		(4)			/* For Padding BSR Trigger */
+#define	BSR_TRIGGER_NONE		(0)	/* No BSR Trigger */
+#define	BSR_TRIGGER_REGULAR		(1)	/* For Regular and ReTxBSR Expiry Triggers */
+#define	BSR_TRIGGER_PERIODIC	(2)	/* For BSR Periodic Timer Expiry Trigger */
+#define	BSR_TRIGGER_PADDING		(4)	/* For Padding BSR Trigger */
 
 
 /*! \brief Downlink SCH PDU Structure */
 typedef struct {
-  uint8_t payload[8][SCH_PAYLOAD_SIZE_MAX];
-  uint16_t Pdu_size[8];
+    uint8_t payload[8][SCH_PAYLOAD_SIZE_MAX];
+    uint16_t Pdu_size[8];
 } __attribute__ ((__packed__)) DLSCH_PDU;
 
 
 /*! \brief MCH PDU Structure */
 typedef struct {
-  int8_t payload[SCH_PAYLOAD_SIZE_MAX];
-  uint16_t Pdu_size;
-  uint8_t mcs;
-  uint8_t sync_area;
-  uint8_t msi_active;
-  uint8_t mcch_active;
-  uint8_t mtch_active;
+    int8_t payload[SCH_PAYLOAD_SIZE_MAX];
+    uint16_t Pdu_size;
+    uint8_t mcs;
+    uint8_t sync_area;
+    uint8_t msi_active;
+    uint8_t mcch_active;
+    uint8_t mtch_active;
 } __attribute__ ((__packed__)) MCH_PDU;
 
 /*! \brief Uplink SCH PDU Structure */
 typedef struct {
-  int8_t payload[SCH_PAYLOAD_SIZE_MAX];         /*!< \brief SACH payload */
-  uint16_t Pdu_size;
+    int8_t payload[SCH_PAYLOAD_SIZE_MAX];	/*!< \brief SACH payload */
+    uint16_t Pdu_size;
 } __attribute__ ((__packed__)) ULSCH_PDU;
 
 #include "PHY/impl_defs_top.h"
 
+/*!\brief RA process state*/
+typedef enum {
+    IDLE = 0,
+    MSG2,
+    WAITMSG3,
+    MSG4,
+    WAITMSG4ACK
+} RA_state;
+
 /*!\brief  UE ULSCH scheduling states*/
 typedef enum {
-  S_UL_NONE =0,
-  S_UL_WAITING,
-  S_UL_SCHEDULED,
-  S_UL_BUFFERED,
-  S_UL_NUM_STATUS
+    S_UL_NONE = 0,
+    S_UL_WAITING,
+    S_UL_SCHEDULED,
+    S_UL_BUFFERED,
+    S_UL_NUM_STATUS
 } UE_ULSCH_STATUS;
 
 /*!\brief  UE DLSCH scheduling states*/
 typedef enum {
-  S_DL_NONE =0,
-  S_DL_WAITING,
-  S_DL_SCHEDULED,
-  S_DL_BUFFERED,
-  S_DL_NUM_STATUS
+    S_DL_NONE = 0,
+    S_DL_WAITING,
+    S_DL_SCHEDULED,
+    S_DL_BUFFERED,
+    S_DL_NUM_STATUS
 } UE_DLSCH_STATUS;
 
 /*!\brief  scheduling policy for the contention-based access */
 typedef enum {
-  CBA_ES=0, /// equal share of RB among groups w
-  CBA_ES_S,  /// equal share of RB among groups with small allocation
-  CBA_PF, /// proportional fair (kind of)
-  CBA_PF_S,  /// proportional fair (kind of) with small RB allocation
-  CBA_RS /// random allocation
+    CBA_ES = 0,			/// equal share of RB among groups w
+    CBA_ES_S,			/// equal share of RB among groups with small allocation
+    CBA_PF,			/// proportional fair (kind of)
+    CBA_PF_S,			/// proportional fair (kind of) with small RB allocation
+    CBA_RS			/// random allocation
 } CBA_POLICY;
 
 
 /*! \brief temporary struct for ULSCH sched */
 typedef struct {
-  rnti_t rnti;
-  uint16_t subframe;
-  uint16_t serving_num;
-  UE_ULSCH_STATUS status;
+    rnti_t rnti;
+    uint16_t subframe;
+    uint16_t serving_num;
+    UE_ULSCH_STATUS status;
 } eNB_ULSCH_INFO;
 /*! \brief temp struct for DLSCH sched */
 typedef struct {
-  rnti_t rnti;
-  uint16_t weight;
-  uint16_t subframe;
-  uint16_t serving_num;
-  UE_DLSCH_STATUS status;
+    rnti_t rnti;
+    uint16_t weight;
+    uint16_t subframe;
+    uint16_t serving_num;
+    UE_DLSCH_STATUS status;
 } eNB_DLSCH_INFO;
 /*! \brief eNB overall statistics */
 typedef struct {
-  /// num BCCH PDU per CC 
-  uint32_t total_num_bcch_pdu;
-  /// BCCH buffer size  
-  uint32_t bcch_buffer;
-  /// total BCCH buffer size  
-  uint32_t total_bcch_buffer;
-  /// BCCH MCS
-  uint32_t bcch_mcs;
-
-  /// num CCCH PDU per CC 
-  uint32_t total_num_ccch_pdu;
-  /// BCCH buffer size  
-  uint32_t ccch_buffer;
-  /// total BCCH buffer size  
-  uint32_t total_ccch_buffer;
-  /// BCCH MCS
-  uint32_t ccch_mcs;
+    /// num BCCH PDU per CC 
+    uint32_t total_num_bcch_pdu;
+    /// BCCH buffer size  
+    uint32_t bcch_buffer;
+    /// total BCCH buffer size  
+    uint32_t total_bcch_buffer;
+    /// BCCH MCS
+    uint32_t bcch_mcs;
+
+    /// num CCCH PDU per CC 
+    uint32_t total_num_ccch_pdu;
+    /// BCCH buffer size  
+    uint32_t ccch_buffer;
+    /// total BCCH buffer size  
+    uint32_t total_ccch_buffer;
+    /// BCCH MCS
+    uint32_t ccch_mcs;
 
 /// num active users
-  uint16_t num_dlactive_UEs;
-  ///  available number of PRBs for a give SF
-  uint16_t available_prbs;
-  /// total number of PRB available for the user plane
-  uint32_t total_available_prbs;
-  /// aggregation
-  /// total avilable nccc : num control channel element
-  uint16_t available_ncces;
-  // only for a new transmission, should be extended for retransmission
-  // current dlsch  bit rate for all transport channels
-  uint32_t dlsch_bitrate;
-  //
-  uint32_t dlsch_bytes_tx;
-  //
-  uint32_t dlsch_pdus_tx;
-  //
-  uint32_t total_dlsch_bitrate;
-  //
-  uint32_t total_dlsch_bytes_tx;
-  //
-  uint32_t total_dlsch_pdus_tx;
-  
-  // here for RX
-  //
-  uint32_t ulsch_bitrate;
-  //
-  uint32_t ulsch_bytes_rx;
-  //
-  uint64_t ulsch_pdus_rx; 
-
-  uint32_t total_ulsch_bitrate;
-  //
-  uint32_t total_ulsch_bytes_rx;
-  //
-  uint32_t total_ulsch_pdus_rx;
-  
-  
-  /// MAC agent-related stats
-  /// total number of scheduling decisions
-  int sched_decisions;
-  /// missed deadlines
-  int missed_deadlines;
+    uint16_t num_dlactive_UEs;
+    ///  available number of PRBs for a give SF
+    uint16_t available_prbs;
+    /// total number of PRB available for the user plane
+    uint32_t total_available_prbs;
+    /// aggregation
+    /// total avilable nccc : num control channel element
+    uint16_t available_ncces;
+    // only for a new transmission, should be extended for retransmission
+    // current dlsch  bit rate for all transport channels
+    uint32_t dlsch_bitrate;
+    //
+    uint32_t dlsch_bytes_tx;
+    //
+    uint32_t dlsch_pdus_tx;
+    //
+    uint32_t total_dlsch_bitrate;
+    //
+    uint32_t total_dlsch_bytes_tx;
+    //
+    uint32_t total_dlsch_pdus_tx;
+
+    // here for RX
+    //
+    uint32_t ulsch_bitrate;
+    //
+    uint32_t ulsch_bytes_rx;
+    //
+    uint64_t ulsch_pdus_rx;
+
+    uint32_t total_ulsch_bitrate;
+    //
+    uint32_t total_ulsch_bytes_rx;
+    //
+    uint32_t total_ulsch_pdus_rx;
+
+
+    /// MAC agent-related stats
+    /// total number of scheduling decisions
+    int sched_decisions;
+    /// missed deadlines
+    int missed_deadlines;
 
 } eNB_STATS;
 /*! \brief eNB statistics for the connected UEs*/
 typedef struct {
 
-  /// CRNTI of UE
-  rnti_t crnti; ///user id (rnti) of connected UEs
-  // rrc status
-  uint8_t rrc_status;
-  /// harq pid
-  uint8_t harq_pid;
-  /// harq rounf
-  uint8_t harq_round;
-  /// total available number of PRBs for a new transmission
-  uint16_t rbs_used;
-  /// total available number of PRBs for a retransmission
-  uint16_t rbs_used_retx;
-  /// total nccc used for a new transmission: num control channel element
-  uint16_t ncce_used;
-  /// total avilable nccc for a retransmission: num control channel element
-  uint16_t ncce_used_retx;
-
-  // mcs1 before the rate adaptaion
-  uint8_t dlsch_mcs1;
-  /// Target mcs2 after rate-adaptation
-  uint8_t dlsch_mcs2;
-  //  current TBS with mcs2
-  uint32_t TBS;
-  //  total TBS with mcs2
-  //  uint32_t total_TBS;
-  //  total rb used for a new transmission
-  uint32_t total_rbs_used;
-  //  total rb used for retransmission
-  uint32_t total_rbs_used_retx;
-
-   /// TX
-  /// Num pkt
-  uint32_t num_pdu_tx[NB_RB_MAX];
-  /// num bytes
-  uint32_t num_bytes_tx[NB_RB_MAX];
-  /// num retransmission / harq
-  uint32_t num_retransmission;
-  /// instantaneous tx throughput for each TTI
-  //  uint32_t tti_throughput[NB_RB_MAX];
-
-  /// overall
-  //
-  uint32_t  dlsch_bitrate;
-  //total
-  uint32_t  total_dlsch_bitrate;
-  /// headers+ CE +  padding bytes for a MAC PDU
-  uint64_t overhead_bytes;
-  /// headers+ CE +  padding bytes for a MAC PDU
-  uint64_t total_overhead_bytes;
-  /// headers+ CE +  padding bytes for a MAC PDU
-  uint64_t avg_overhead_bytes;
-  // MAC multiplexed payload
-  uint64_t total_sdu_bytes;
-  // total MAC pdu bytes
-  uint64_t total_pdu_bytes;
-
-  // total num pdu
-  uint32_t total_num_pdus;
-  //
-  //  uint32_t avg_pdu_size;
-
-  /// RX
-
-  /// PUCCH1a/b power (dBm)
-  int32_t Po_PUCCH_dBm;
-  /// Indicator that Po_PUCCH has been updated by PHY
-  int32_t Po_PUCCH_update;
-  /// Uplink measured RSSI
-  int32_t UL_rssi;
-  /// preassigned mcs after rate adaptation
-  uint8_t ulsch_mcs1;
-  /// adjusted mcs
-  uint8_t ulsch_mcs2;
-
-  /// estimated average pdu inter-departure time
-  uint32_t avg_pdu_idt;
-  /// estimated average pdu size
-  uint32_t avg_pdu_ps;
-  ///
-  uint32_t aggregated_pdu_size;
-  uint32_t aggregated_pdu_arrival;
-
-  ///  uplink transport block size
-  uint32_t ulsch_TBS;
-
-  ///  total rb used for a new uplink transmission
-  uint32_t num_retransmission_rx;
-  ///  total rb used for a new uplink transmission
-  uint32_t rbs_used_rx;
-   ///  total rb used for a new uplink retransmission
-  uint32_t rbs_used_retx_rx;
-  ///  total rb used for a new uplink transmission
-  uint32_t total_rbs_used_rx;
-  /// normalized rx power 
-  int32_t      normalized_rx_power;
-   /// target rx power 
-  int32_t    target_rx_power;
-
-  /// num rx pdu
-  uint32_t num_pdu_rx[NB_RB_MAX];
-  /// num bytes rx
-  uint32_t num_bytes_rx[NB_RB_MAX];
-  /// instantaneous rx throughput for each TTI
-  //  uint32_t tti_goodput[NB_RB_MAX];
-  /// errors
-  uint32_t num_errors_rx;
-  
-  uint64_t overhead_bytes_rx;
-  /// headers+ CE +  padding bytes for a MAC PDU
-  uint64_t total_overhead_bytes_rx;
-  /// headers+ CE +  padding bytes for a MAC PDU
-  uint64_t avg_overhead_bytes_rx;
- //
-  uint32_t  ulsch_bitrate;
-  //total
-  uint32_t  total_ulsch_bitrate;
-  /// overall
-  ///  MAC pdu bytes
-  uint64_t pdu_bytes_rx;
-  /// total MAC pdu bytes
-  uint64_t total_pdu_bytes_rx;
-  /// total num pdu
-  uint32_t total_num_pdus_rx;
-  /// num of error pdus
-  uint32_t total_num_errors_rx;
+    /// CRNTI of UE
+    rnti_t crnti;		///user id (rnti) of connected UEs
+    // rrc status
+    uint8_t rrc_status;
+    /// harq pid
+    uint8_t harq_pid;
+    /// harq rounf
+    uint8_t harq_round;
+    /// total available number of PRBs for a new transmission
+    uint16_t rbs_used;
+    /// total available number of PRBs for a retransmission
+    uint16_t rbs_used_retx;
+    /// total nccc used for a new transmission: num control channel element
+    uint16_t ncce_used;
+    /// total avilable nccc for a retransmission: num control channel element
+    uint16_t ncce_used_retx;
+
+    // mcs1 before the rate adaptaion
+    uint8_t dlsch_mcs1;
+    /// Target mcs2 after rate-adaptation
+    uint8_t dlsch_mcs2;
+    //  current TBS with mcs2
+    uint32_t TBS;
+    //  total TBS with mcs2
+    //  uint32_t total_TBS;
+    //  total rb used for a new transmission
+    uint32_t total_rbs_used;
+    //  total rb used for retransmission
+    uint32_t total_rbs_used_retx;
+
+    /// TX
+    /// Num pkt
+    uint32_t num_pdu_tx[NB_RB_MAX];
+    /// num bytes
+    uint32_t num_bytes_tx[NB_RB_MAX];
+    /// num retransmission / harq
+    uint32_t num_retransmission;
+    /// instantaneous tx throughput for each TTI
+    //  uint32_t tti_throughput[NB_RB_MAX];
+
+    /// overall
+    //
+    uint32_t dlsch_bitrate;
+    //total
+    uint32_t total_dlsch_bitrate;
+    /// headers+ CE +  padding bytes for a MAC PDU
+    uint64_t overhead_bytes;
+    /// headers+ CE +  padding bytes for a MAC PDU
+    uint64_t total_overhead_bytes;
+    /// headers+ CE +  padding bytes for a MAC PDU
+    uint64_t avg_overhead_bytes;
+    // MAC multiplexed payload
+    uint64_t total_sdu_bytes;
+    // total MAC pdu bytes
+    uint64_t total_pdu_bytes;
+
+    // total num pdu
+    uint32_t total_num_pdus;
+    //
+    //  uint32_t avg_pdu_size;
+
+    /// RX
+
+    /// PUCCH1a/b power (dBm)
+    int32_t Po_PUCCH_dBm;
+    /// Indicator that Po_PUCCH has been updated by PHY
+    int32_t Po_PUCCH_update;
+    /// Uplink measured RSSI
+    int32_t UL_rssi;
+    /// preassigned mcs after rate adaptation
+    uint8_t ulsch_mcs1;
+    /// adjusted mcs
+    uint8_t ulsch_mcs2;
+
+    /// estimated average pdu inter-departure time
+    uint32_t avg_pdu_idt;
+    /// estimated average pdu size
+    uint32_t avg_pdu_ps;
+    ///
+    uint32_t aggregated_pdu_size;
+    uint32_t aggregated_pdu_arrival;
+
+    ///  uplink transport block size
+    uint32_t ulsch_TBS;
+
+    ///  total rb used for a new uplink transmission
+    uint32_t num_retransmission_rx;
+    ///  total rb used for a new uplink transmission
+    uint32_t rbs_used_rx;
+    ///  total rb used for a new uplink retransmission
+    uint32_t rbs_used_retx_rx;
+    ///  total rb used for a new uplink transmission
+    uint32_t total_rbs_used_rx;
+    /// normalized rx power 
+    int32_t normalized_rx_power;
+    /// target rx power 
+    int32_t target_rx_power;
+
+    /// num rx pdu
+    uint32_t num_pdu_rx[NB_RB_MAX];
+    /// num bytes rx
+    uint32_t num_bytes_rx[NB_RB_MAX];
+    /// instantaneous rx throughput for each TTI
+    //  uint32_t tti_goodput[NB_RB_MAX];
+    /// errors
+    uint32_t num_errors_rx;
+
+    uint64_t overhead_bytes_rx;
+    /// headers+ CE +  padding bytes for a MAC PDU
+    uint64_t total_overhead_bytes_rx;
+    /// headers+ CE +  padding bytes for a MAC PDU
+    uint64_t avg_overhead_bytes_rx;
+    //
+    uint32_t ulsch_bitrate;
+    //total
+    uint32_t total_ulsch_bitrate;
+    /// overall
+    ///  MAC pdu bytes
+    uint64_t pdu_bytes_rx;
+    /// total MAC pdu bytes
+    uint64_t total_pdu_bytes_rx;
+    /// total num pdu
+    uint32_t total_num_pdus_rx;
+    /// num of error pdus
+    uint32_t total_num_errors_rx;
 
 } eNB_UE_STATS;
 /*! \brief eNB template for UE context information  */
 typedef struct {
-  /// C-RNTI of UE
-  rnti_t rnti;
-  /// NDI from last scheduling
-  uint8_t oldNDI[8];
-  /// mcs1 from last scheduling
-  uint8_t oldmcs1[8];
-  /// mcs2 from last scheduling
-  uint8_t oldmcs2[8];
-  /// NDI from last UL scheduling
-  uint8_t oldNDI_UL[8];
-  /// mcs from last UL scheduling
-  uint8_t mcs_UL[8];
-  /// TBS from last UL scheduling
-  uint8_t TBS_UL[8];
-  /// Flag to indicate UL has been scheduled at least once
-  boolean_t ul_active;
-  /// Flag to indicate UE has been configured (ACK from RRCConnectionSetup received)
-  boolean_t configured;
-
-  /// MCS from last scheduling
-  uint8_t mcs[8];
-
-  /// TPC from last scheduling
-  uint8_t oldTPC[8];
-
-  // PHY interface info
-
-  /// Number of Allocated RBs for DL after scheduling (prior to frequency allocation)
-  uint16_t nb_rb[8]; // num_max_harq
-
-  /// Number of Allocated RBs for UL after scheduling
-  uint16_t nb_rb_ul[8]; // num_max_harq
-
-  /// Number of Allocated RBs for UL after scheduling
-  uint16_t first_rb_ul[8]; // num_max_harq
-
-  /// Cyclic shift for DMRS after scheduling
-  uint16_t cshift[8]; // num_max_harq
-
-  /// Number of Allocated RBs by the ulsch preprocessor
-  uint8_t pre_allocated_nb_rb_ul;
-
-  /// index of Allocated RBs by the ulsch preprocessor
-  int8_t pre_allocated_rb_table_index_ul;
-
-  /// total allocated RBs
-  int8_t total_allocated_rbs;
-
-  /// pre-assigned MCS by the ulsch preprocessor
-  uint8_t pre_assigned_mcs_ul;
-
-  /// assigned MCS by the ulsch scheduler
-  uint8_t assigned_mcs_ul;
-
-  /// DL DAI
-  uint8_t DAI;
-
-  /// UL DAI
-  uint8_t DAI_ul[10];
-
-  /// UL Scheduling Request Received
-  uint8_t ul_SR;
-
-  ///Resource Block indication for each sub-band in MU-MIMO
-  uint8_t rballoc_subband[8][50];
-
-  // Logical channel info for link with RLC
-
-  /// Last received UE BSR info for each logical channel group id
-  uint8_t bsr_info[MAX_NUM_LCGID];
-
-  /// LCGID mapping
-  long lcgidmap[11];
-
-  /// phr information
-  int8_t phr_info;
-
-  /// phr information
-  int8_t phr_info_configured;
-
-  ///dl buffer info
-  uint32_t dl_buffer_info[MAX_NUM_LCID];
-  /// total downlink buffer info
-  uint32_t dl_buffer_total;
-  /// total downlink pdus
-  uint32_t dl_pdus_total;
-  /// downlink pdus for each LCID
-  uint32_t dl_pdus_in_buffer[MAX_NUM_LCID];
-  /// creation time of the downlink buffer head for each LCID
-  uint32_t dl_buffer_head_sdu_creation_time[MAX_NUM_LCID];
-  /// maximum creation time of the downlink buffer head across all LCID
-  uint32_t  dl_buffer_head_sdu_creation_time_max;
-  /// a flag indicating that the downlink head SDU is segmented  
-  uint8_t    dl_buffer_head_sdu_is_segmented[MAX_NUM_LCID];
-  /// size of remaining size to send for the downlink head SDU
-  uint32_t dl_buffer_head_sdu_remaining_size_to_send[MAX_NUM_LCID];
-
-  /// total uplink buffer size 
-  uint32_t ul_total_buffer;
-  /// uplink buffer creation time for each LCID
-  uint32_t ul_buffer_creation_time[MAX_NUM_LCGID];
-  /// maximum uplink buffer creation time across all the LCIDs
-  uint32_t ul_buffer_creation_time_max;
-  /// uplink buffer size per LCID
-  uint32_t ul_buffer_info[MAX_NUM_LCGID];
-
-  /// UE tx power
-  int32_t ue_tx_power;
+    /// C-RNTI of UE
+    rnti_t rnti;
+    /// NDI from last scheduling
+    uint8_t oldNDI[8];
+    /// mcs1 from last scheduling
+    uint8_t oldmcs1[8];
+    /// mcs2 from last scheduling
+    uint8_t oldmcs2[8];
+    /// NDI from last UL scheduling
+    uint8_t oldNDI_UL[8];
+    /// mcs from last UL scheduling
+    uint8_t mcs_UL[8];
+    /// TBS from last UL scheduling
+    uint8_t TBS_UL[8];
+    /// Flag to indicate UL has been scheduled at least once
+    boolean_t ul_active;
+    /// Flag to indicate UE has been configured (ACK from RRCConnectionSetup received)
+    boolean_t configured;
+
+    /// MCS from last scheduling
+    uint8_t mcs[8];
+
+    /// TPC from last scheduling
+    uint8_t oldTPC[8];
+
+    // PHY interface info
+
+    /// Number of Allocated RBs for DL after scheduling (prior to frequency allocation)
+    uint16_t nb_rb[8];		// num_max_harq
+
+    /// Number of Allocated RBs for UL after scheduling
+    uint16_t nb_rb_ul[8];	// num_max_harq
+
+    /// Number of Allocated RBs for UL after scheduling
+    uint16_t first_rb_ul[8];	// num_max_harq
+
+    /// Cyclic shift for DMRS after scheduling
+    uint16_t cshift[8];		// num_max_harq
+
+    /// Number of Allocated RBs by the ulsch preprocessor
+    uint8_t pre_allocated_nb_rb_ul;
+
+    /// index of Allocated RBs by the ulsch preprocessor
+    int8_t pre_allocated_rb_table_index_ul;
+
+    /// total allocated RBs
+    int8_t total_allocated_rbs;
+
+    /// pre-assigned MCS by the ulsch preprocessor
+    uint8_t pre_assigned_mcs_ul;
+
+    /// assigned MCS by the ulsch scheduler
+    uint8_t assigned_mcs_ul;
+
+    /// DL DAI
+    uint8_t DAI;
+
+    /// UL DAI
+    uint8_t DAI_ul[10];
+
+    /// UL Scheduling Request Received
+    uint8_t ul_SR;
+
+    ///Resource Block indication for each sub-band in MU-MIMO
+    uint8_t rballoc_subband[8][50];
+
+    // Logical channel info for link with RLC
+
+    /// Last received UE BSR info for each logical channel group id
+    uint8_t bsr_info[MAX_NUM_LCGID];
+
+    /// LCGID mapping
+    long lcgidmap[11];
+
+    /// phr information
+    int8_t phr_info;
+
+    /// phr information
+    int8_t phr_info_configured;
+
+    ///dl buffer info
+    uint32_t dl_buffer_info[MAX_NUM_LCID];
+    /// total downlink buffer info
+    uint32_t dl_buffer_total;
+    /// total downlink pdus
+    uint32_t dl_pdus_total;
+    /// downlink pdus for each LCID
+    uint32_t dl_pdus_in_buffer[MAX_NUM_LCID];
+    /// creation time of the downlink buffer head for each LCID
+    uint32_t dl_buffer_head_sdu_creation_time[MAX_NUM_LCID];
+    /// maximum creation time of the downlink buffer head across all LCID
+    uint32_t dl_buffer_head_sdu_creation_time_max;
+    /// a flag indicating that the downlink head SDU is segmented  
+    uint8_t dl_buffer_head_sdu_is_segmented[MAX_NUM_LCID];
+    /// size of remaining size to send for the downlink head SDU
+    uint32_t dl_buffer_head_sdu_remaining_size_to_send[MAX_NUM_LCID];
+
+    /// total uplink buffer size 
+    uint32_t ul_total_buffer;
+    /// uplink buffer creation time for each LCID
+    uint32_t ul_buffer_creation_time[MAX_NUM_LCGID];
+    /// maximum uplink buffer creation time across all the LCIDs
+    uint32_t ul_buffer_creation_time_max;
+    /// uplink buffer size per LCID
+    uint32_t ul_buffer_info[MAX_NUM_LCGID];
+
+    /// UE tx power
+    int32_t ue_tx_power;
 
-  /// stores the frame where the last TPC was transmitted
-  uint32_t pusch_tpc_tx_frame;
-  uint32_t pusch_tpc_tx_subframe;
-  uint32_t pucch_tpc_tx_frame;
-  uint32_t pucch_tpc_tx_subframe;
+    /// stores the frame where the last TPC was transmitted
+    uint32_t pusch_tpc_tx_frame;
+    uint32_t pusch_tpc_tx_subframe;
+    uint32_t pucch_tpc_tx_frame;
+    uint32_t pucch_tpc_tx_subframe;
 
 #ifdef LOCALIZATION
-  eNB_UE_estimated_distances distance;
+    eNB_UE_estimated_distances distance;
 #endif
 
 #ifdef Rel14
-  uint8_t rach_resource_type;
-  uint16_t mpdcch_repetition_cnt;
-  frame_t Msg2_frame;
+    uint8_t rach_resource_type;
+    uint16_t mpdcch_repetition_cnt;
+    frame_t Msg2_frame;
 #endif
-  sub_frame_t Msg2_subframe;
+    sub_frame_t Msg2_subframe;
 
-  PhysicalConfigDedicated_t  *physicalConfigDedicated;
+    PhysicalConfigDedicated_t *physicalConfigDedicated;
 
 } UE_TEMPLATE;
 
 /*! \brief scheduling control information set through an API (not used)*/
 typedef struct {
-  ///UL transmission bandwidth in RBs
-  uint8_t ul_bandwidth[MAX_NUM_LCID];
-  ///DL transmission bandwidth in RBs
-  uint8_t dl_bandwidth[MAX_NUM_LCID];
-
-  //To do GBR bearer
-  uint8_t min_ul_bandwidth[MAX_NUM_LCID];
-
-  uint8_t min_dl_bandwidth[MAX_NUM_LCID];
-
-  ///aggregated bit rate of non-gbr bearer per UE
-  uint64_t  ue_AggregatedMaximumBitrateDL;
-  ///aggregated bit rate of non-gbr bearer per UE
-  uint64_t  ue_AggregatedMaximumBitrateUL;
-  ///CQI scheduling interval in subframes.
-  uint16_t cqiSchedInterval;
-  ///Contention resolution timer used during random access
-  uint8_t mac_ContentionResolutionTimer;
-
-  uint16_t max_allowed_rbs[MAX_NUM_LCID];
-
-  uint8_t max_mcs[MAX_NUM_LCID];
-
-  uint16_t priority[MAX_NUM_LCID];
-
-  // resource scheduling information
-  
-  /// Current DL harq round per harq_pid on each CC
-  uint8_t       round[MAX_NUM_CCs][10];
-  /// Current Active TBs per harq_pid on each CC
-  uint8_t       tbcnt[MAX_NUM_CCs][10];
-  /// Current UL harq round per harq_pid on each CC
-  uint8_t       round_UL[MAX_NUM_CCs][8];
-  uint8_t       dl_pow_off[MAX_NUM_CCs];
-  uint16_t      pre_nb_available_rbs[MAX_NUM_CCs];
-  unsigned char rballoc_sub_UE[MAX_NUM_CCs][N_RBG_MAX];
-  uint16_t      ta_timer;
-  int16_t       ta_update;
-  uint16_t      ul_consecutive_errors;
-  int32_t       context_active_timer;
-  int32_t       cqi_req_timer;
-  int32_t       ul_inactivity_timer;
-  int32_t       ul_failure_timer; 
-  int32_t       ul_scheduled;
-  int32_t       ra_pdcch_order_sent;
-  int32_t       ul_out_of_sync;
-  int32_t       phr_received;
-  uint8_t       periodic_ri_received[NFAPI_CC_MAX];
-  uint8_t       aperiodic_ri_received[NFAPI_CC_MAX];
-  uint8_t       pucch1_cqi_update[NFAPI_CC_MAX];
-  uint8_t       pucch1_snr[NFAPI_CC_MAX];
-  uint8_t       pucch2_cqi_update[NFAPI_CC_MAX];
-  uint8_t       pucch2_snr[NFAPI_CC_MAX];
-  uint8_t       pucch3_cqi_update[NFAPI_CC_MAX];
-  uint8_t       pucch3_snr[NFAPI_CC_MAX];
-  uint8_t       pusch_snr[NFAPI_CC_MAX];
-  uint16_t      feedback_cnt[NFAPI_CC_MAX];
-  uint16_t      timing_advance;
-  uint16_t      timing_advance_r9;
-  uint8_t       periodic_wideband_cqi[NFAPI_CC_MAX];
-  uint8_t       periodic_wideband_spatial_diffcqi[NFAPI_CC_MAX];
-  uint8_t       periodic_wideband_pmi[NFAPI_CC_MAX];
-  uint8_t       periodic_subband_cqi[NFAPI_CC_MAX][16];
-  uint8_t       periodic_subband_spatial_diffcqi[NFAPI_CC_MAX][16];
-  uint8_t       aperiodic_subband_cqi0[NFAPI_CC_MAX][25];
-  uint8_t       aperiodic_subband_pmi[NFAPI_CC_MAX][25];
-  uint8_t       aperiodic_subband_diffcqi0[NFAPI_CC_MAX][25];
-  uint8_t       aperiodic_subband_cqi1[NFAPI_CC_MAX][25];
-  uint8_t       aperiodic_subband_diffcqi1[NFAPI_CC_MAX][25];
-  uint8_t       aperiodic_wideband_cqi0[NFAPI_CC_MAX];
-  uint8_t       aperiodic_wideband_pmi[NFAPI_CC_MAX];
-  uint8_t       aperiodic_wideband_cqi1[NFAPI_CC_MAX];
-  uint8_t       aperiodic_wideband_pmi1[NFAPI_CC_MAX];
-  uint8_t       dl_cqi[NFAPI_CC_MAX];
+    ///UL transmission bandwidth in RBs
+    uint8_t ul_bandwidth[MAX_NUM_LCID];
+    ///DL transmission bandwidth in RBs
+    uint8_t dl_bandwidth[MAX_NUM_LCID];
+
+    //To do GBR bearer
+    uint8_t min_ul_bandwidth[MAX_NUM_LCID];
+
+    uint8_t min_dl_bandwidth[MAX_NUM_LCID];
+
+    ///aggregated bit rate of non-gbr bearer per UE
+    uint64_t ue_AggregatedMaximumBitrateDL;
+    ///aggregated bit rate of non-gbr bearer per UE
+    uint64_t ue_AggregatedMaximumBitrateUL;
+    ///CQI scheduling interval in subframes.
+    uint16_t cqiSchedInterval;
+    ///Contention resolution timer used during random access
+    uint8_t mac_ContentionResolutionTimer;
+
+    uint16_t max_allowed_rbs[MAX_NUM_LCID];
+
+    uint8_t max_mcs[MAX_NUM_LCID];
+
+    uint16_t priority[MAX_NUM_LCID];
+
+    // resource scheduling information
+
+    /// Current DL harq round per harq_pid on each CC
+    uint8_t round[MAX_NUM_CCs][10];
+    /// Current Active TBs per harq_pid on each CC
+    uint8_t tbcnt[MAX_NUM_CCs][10];
+    /// Current UL harq round per harq_pid on each CC
+    uint8_t round_UL[MAX_NUM_CCs][8];
+    uint8_t dl_pow_off[MAX_NUM_CCs];
+    uint16_t pre_nb_available_rbs[MAX_NUM_CCs];
+    unsigned char rballoc_sub_UE[MAX_NUM_CCs][N_RBG_MAX];
+    uint16_t ta_timer;
+    int16_t ta_update;
+    uint16_t ul_consecutive_errors;
+    int32_t context_active_timer;
+    int32_t cqi_req_timer;
+    int32_t ul_inactivity_timer;
+    int32_t ul_failure_timer;
+    int32_t ul_scheduled;
+    int32_t ra_pdcch_order_sent;
+    int32_t ul_out_of_sync;
+    int32_t phr_received;
+    uint8_t periodic_ri_received[NFAPI_CC_MAX];
+    uint8_t aperiodic_ri_received[NFAPI_CC_MAX];
+    uint8_t pucch1_cqi_update[NFAPI_CC_MAX];
+    uint8_t pucch1_snr[NFAPI_CC_MAX];
+    uint8_t pucch2_cqi_update[NFAPI_CC_MAX];
+    uint8_t pucch2_snr[NFAPI_CC_MAX];
+    uint8_t pucch3_cqi_update[NFAPI_CC_MAX];
+    uint8_t pucch3_snr[NFAPI_CC_MAX];
+    uint8_t pusch_snr[NFAPI_CC_MAX];
+    uint16_t feedback_cnt[NFAPI_CC_MAX];
+    uint16_t timing_advance;
+    uint16_t timing_advance_r9;
+    uint8_t periodic_wideband_cqi[NFAPI_CC_MAX];
+    uint8_t periodic_wideband_spatial_diffcqi[NFAPI_CC_MAX];
+    uint8_t periodic_wideband_pmi[NFAPI_CC_MAX];
+    uint8_t periodic_subband_cqi[NFAPI_CC_MAX][16];
+    uint8_t periodic_subband_spatial_diffcqi[NFAPI_CC_MAX][16];
+    uint8_t aperiodic_subband_cqi0[NFAPI_CC_MAX][25];
+    uint8_t aperiodic_subband_pmi[NFAPI_CC_MAX][25];
+    uint8_t aperiodic_subband_diffcqi0[NFAPI_CC_MAX][25];
+    uint8_t aperiodic_subband_cqi1[NFAPI_CC_MAX][25];
+    uint8_t aperiodic_subband_diffcqi1[NFAPI_CC_MAX][25];
+    uint8_t aperiodic_wideband_cqi0[NFAPI_CC_MAX];
+    uint8_t aperiodic_wideband_pmi[NFAPI_CC_MAX];
+    uint8_t aperiodic_wideband_cqi1[NFAPI_CC_MAX];
+    uint8_t aperiodic_wideband_pmi1[NFAPI_CC_MAX];
+    uint8_t dl_cqi[NFAPI_CC_MAX];
 } UE_sched_ctrl;
 /*! \brief eNB template for the Random access information */
 typedef struct {
-  /// Flag to indicate this process is active
-  boolean_t RA_active;
-  /// Size of DCI for RA-Response (bytes)
-  uint8_t RA_dci_size_bytes1;
-  /// Size of DCI for RA-Response (bits)
-  uint8_t RA_dci_size_bits1;
-  /// Actual DCI to transmit for RA-Response
-  uint8_t RA_alloc_pdu1[(MAX_DCI_SIZE_BITS>>3)+1];
-  /// DCI format for RA-Response (should be 1A)
-  uint8_t RA_dci_fmt1;
-  /// Size of DCI for Msg4/ContRes (bytes)
-  uint8_t RA_dci_size_bytes2;
-  /// Size of DCI for Msg4/ContRes (bits)
-  uint8_t RA_dci_size_bits2;
-  /// Actual DCI to transmit for Msg4/ContRes
-  uint8_t RA_alloc_pdu2[(MAX_DCI_SIZE_BITS>>3)+1];
-  /// DCI format for Msg4/ContRes (should be 1A)
-  uint8_t RA_dci_fmt2;
-  /// Flag to indicate the eNB should generate RAR.  This is triggered by detection of PRACH
-  uint8_t generate_rar;
-  /// Subframe where preamble was received
-  uint8_t preamble_subframe;
-  /// Subframe where Msg2 is to be sent
-  uint8_t Msg2_subframe;
-  /// Frame where Msg2 is to be sent
-  frame_t Msg2_frame;
-  /// Subframe where Msg3 is to be sent
-  sub_frame_t Msg3_subframe;
-  /// Frame where Msg3 is to be sent
-  frame_t Msg3_frame;
-  /// Subframe where Msg4 is to be sent
-  sub_frame_t Msg4_subframe;
-  /// Frame where Msg4 is to be sent
-  frame_t Msg4_frame;
-  /// Flag to indicate the eNB should generate Msg4 upon reception of SDU from RRC.  This is triggered by first ULSCH reception at eNB for new user.
-  uint8_t generate_Msg4;
-  /// Flag to indicate that eNB is waiting for ACK that UE has received Msg3.
-  uint8_t wait_ack_Msg4;
-  /// harq_pid used for Msg4 transmission
-  uint8_t harq_pid;
-  /// UE RNTI allocated during RAR
-  rnti_t rnti;
-  /// RA RNTI allocated from received PRACH
-  uint16_t RA_rnti;
-  /// Received preamble_index
-  uint8_t preamble_index;
-  /// Received UE Contention Resolution Identifier
-  uint8_t cont_res_id[6];
-  /// Timing offset indicated by PHY
-  int16_t timing_offset;
-  /// Timeout for RRC connection
-  int16_t RRC_timer;
-  /// Msg3 first RB
-  uint8_t msg3_first_rb;
-  /// Msg3 number of RB
-  uint8_t msg3_nb_rb;
-  /// Msg3 MCS
-  uint8_t msg3_mcs;
-  /// Msg3 TPC command
-  uint8_t msg3_TPC;
-  /// Msg3 ULdelay command
-  uint8_t msg3_ULdelay;
-  /// Msg3 cqireq command
-  uint8_t msg3_cqireq;
-  /// Round of Msg3 HARQ
-  uint8_t msg3_round;
-  /// TBS used for Msg4
-  int msg4_TBsize;
-  /// MCS used for Msg4
-  int msg4_mcs;
+    /// Flag to indicate this process is active
+    RA_state state;
+    /// Subframe where preamble was received
+    uint8_t preamble_subframe;
+    /// Subframe where Msg2 is to be sent
+    uint8_t Msg2_subframe;
+    /// Frame where Msg2 is to be sent
+    frame_t Msg2_frame;
+    /// Subframe where Msg3 is to be sent
+    sub_frame_t Msg3_subframe;
+    /// Frame where Msg3 is to be sent
+    frame_t Msg3_frame;
+    /// Subframe where Msg4 is to be sent
+    sub_frame_t Msg4_subframe;
+    /// Frame where Msg4 is to be sent
+    frame_t Msg4_frame;
+    /// harq_pid used for Msg4 transmission
+    uint8_t harq_pid;
+    /// UE RNTI allocated during RAR
+    rnti_t rnti;
+    /// RA RNTI allocated from received PRACH
+    uint16_t RA_rnti;
+    /// Received preamble_index
+    uint8_t preamble_index;
+    /// Received UE Contention Resolution Identifier
+    uint8_t cont_res_id[6];
+    /// Timing offset indicated by PHY
+    int16_t timing_offset;
+    /// Timeout for RRC connection
+    int16_t RRC_timer;
+    /// Msg3 first RB
+    uint8_t msg3_first_rb;
+    /// Msg3 number of RB
+    uint8_t msg3_nb_rb;
+    /// Msg3 MCS
+    uint8_t msg3_mcs;
+    /// Msg3 TPC command
+    uint8_t msg3_TPC;
+    /// Msg3 ULdelay command
+    uint8_t msg3_ULdelay;
+    /// Msg3 cqireq command
+    uint8_t msg3_cqireq;
+    /// Round of Msg3 HARQ
+    uint8_t msg3_round;
+    /// TBS used for Msg4
+    int msg4_TBsize;
+    /// MCS used for Msg4
+    int msg4_mcs;
 #ifdef Rel14
-  uint8_t rach_resource_type;
-  uint8_t msg2_mpdcch_repetition_cnt;
-  uint8_t msg4_mpdcch_repetition_cnt;
-  uint8_t msg2_narrowband;
-  uint8_t msg34_narrowband;
+    uint8_t rach_resource_type;
+    uint8_t msg2_mpdcch_repetition_cnt;
+    uint8_t msg4_mpdcch_repetition_cnt;
+    uint8_t msg2_narrowband;
+    uint8_t msg34_narrowband;
 #endif
-} RA_TEMPLATE;
+} RA_t;
 
 
 /*! \brief subband bitmap confguration (for ALU icic algo purpose), in test phase */
 typedef struct {
-  uint8_t sbmap[NUMBER_OF_SUBBANDS_MAX]; //13 = number of SB MAX for 100 PRB
-  uint8_t periodicity;
-  uint8_t first_subframe;
-  uint8_t sb_size;
-  uint8_t nb_active_sb;
+    uint8_t sbmap[NUMBER_OF_SUBBANDS_MAX];	//13 = number of SB MAX for 100 PRB
+    uint8_t periodicity;
+    uint8_t first_subframe;
+    uint8_t sb_size;
+    uint8_t nb_active_sb;
 } SBMAP_CONF;
-/*! \brief UE list used by eNB to order UEs/CC for scheduling*/ 
+/*! \brief UE list used by eNB to order UEs/CC for scheduling*/
 typedef struct {
-  /// Dedicated information for UEs
-  struct PhysicalConfigDedicated  *physicalConfigDedicated[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
-  /// DLSCH pdu 
-  DLSCH_PDU DLSCH_pdu[MAX_NUM_CCs][2][NUMBER_OF_UE_MAX];
-  /// DCI template and MAC connection parameters for UEs
-  UE_TEMPLATE UE_template[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
-  /// DCI template and MAC connection for RA processes
-  int pCC_id[NUMBER_OF_UE_MAX];
-  /// sorted downlink component carrier for the scheduler 
-  int ordered_CCids[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
-  /// number of downlink active component carrier 
-  int numactiveCCs[NUMBER_OF_UE_MAX];
-  /// sorted uplink component carrier for the scheduler 
-  int ordered_ULCCids[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
-  /// number of uplink active component carrier 
-  int numactiveULCCs[NUMBER_OF_UE_MAX];
-  /// number of downlink active component carrier 
-  uint8_t dl_CC_bitmap[NUMBER_OF_UE_MAX];
-  /// eNB to UE statistics
-  eNB_UE_STATS eNB_UE_stats[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
-  /// scheduling control info
-  UE_sched_ctrl UE_sched_ctrl[NUMBER_OF_UE_MAX];
-  int next[NUMBER_OF_UE_MAX];
-  int head;
-  int next_ul[NUMBER_OF_UE_MAX];
-  int head_ul;
-  int avail;
-  int num_UEs;
-  boolean_t active[NUMBER_OF_UE_MAX];
+    /// Dedicated information for UEs
+    struct PhysicalConfigDedicated
+	*physicalConfigDedicated[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
+    /// DLSCH pdu 
+    DLSCH_PDU DLSCH_pdu[MAX_NUM_CCs][2][NUMBER_OF_UE_MAX];
+    /// DCI template and MAC connection parameters for UEs
+    UE_TEMPLATE UE_template[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
+    /// DCI template and MAC connection for RA processes
+    int pCC_id[NUMBER_OF_UE_MAX];
+    /// sorted downlink component carrier for the scheduler 
+    int ordered_CCids[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
+    /// number of downlink active component carrier 
+    int numactiveCCs[NUMBER_OF_UE_MAX];
+    /// sorted uplink component carrier for the scheduler 
+    int ordered_ULCCids[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
+    /// number of uplink active component carrier 
+    int numactiveULCCs[NUMBER_OF_UE_MAX];
+    /// number of downlink active component carrier 
+    uint8_t dl_CC_bitmap[NUMBER_OF_UE_MAX];
+    /// eNB to UE statistics
+    eNB_UE_STATS eNB_UE_stats[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
+    /// scheduling control info
+    UE_sched_ctrl UE_sched_ctrl[NUMBER_OF_UE_MAX];
+    int next[NUMBER_OF_UE_MAX];
+    int head;
+    int next_ul[NUMBER_OF_UE_MAX];
+    int head_ul;
+    int avail;
+    int num_UEs;
+    boolean_t active[NUMBER_OF_UE_MAX];
 } UE_list_t;
 
-/*! \brief eNB common channels */ 
+/*! \brief eNB common channels */
 typedef struct {
-  int                              physCellId;
-  int                              p_eNB;
-  int                              Ncp;
-  int                              eutra_band;
-  uint32_t                         dl_CarrierFreq;
-  BCCH_BCH_Message_t               *mib;
-  RadioResourceConfigCommonSIB_t   *radioResourceConfigCommon;
+    int physCellId;
+    int p_eNB;
+    int Ncp;
+    int eutra_band;
+    uint32_t dl_CarrierFreq;
+    BCCH_BCH_Message_t *mib;
+    RadioResourceConfigCommonSIB_t *radioResourceConfigCommon;
 #ifdef Rel14
-  RadioResourceConfigCommonSIB_t   *radioResourceConfigCommon_BR;  
+    RadioResourceConfigCommonSIB_t *radioResourceConfigCommon_BR;
 #endif
-  TDD_Config_t                     *tdd_Config;
-  SchedulingInfoList_t             *schedulingInfoList;
-  ARFCN_ValueEUTRA_t               ul_CarrierFreq;
-  long                             ul_Bandwidth;
-  /// Outgoing MIB PDU for PHY
-  MIB_PDU MIB_pdu;
-  /// Outgoing BCCH pdu for PHY
-  BCCH_PDU BCCH_pdu;
-  /// Outgoing BCCH DCI allocation
-  uint32_t BCCH_alloc_pdu;
-  /// Outgoing CCCH pdu for PHY
-  CCCH_PDU CCCH_pdu;
-  /// Outgoing RAR pdu for PHY
-  RAR_PDU RAR_pdu;
-  /// Template for RA computations
-  RA_TEMPLATE RA_template[NB_RA_PROC_MAX];
-  /// VRB map for common channels
-  uint8_t vrb_map[100];
-  /// VRB map for common channels and retransmissions by PHICH
-  uint8_t vrb_map_UL[100];
-  /// MBSFN SubframeConfig
-  struct MBSFN_SubframeConfig *mbsfn_SubframeConfig[8];
-  /// number of subframe allocation pattern available for MBSFN sync area
-  uint8_t num_sf_allocation_pattern;
+    TDD_Config_t *tdd_Config;
+    SchedulingInfoList_t *schedulingInfoList;
+    ARFCN_ValueEUTRA_t ul_CarrierFreq;
+    long ul_Bandwidth;
+    /// Outgoing MIB PDU for PHY
+    MIB_PDU MIB_pdu;
+    /// Outgoing BCCH pdu for PHY
+    BCCH_PDU BCCH_pdu;
+    /// Outgoing BCCH DCI allocation
+    uint32_t BCCH_alloc_pdu;
+    /// Outgoing CCCH pdu for PHY
+    CCCH_PDU CCCH_pdu;
+    /// Outgoing RAR pdu for PHY
+    RAR_PDU RAR_pdu;
+    /// Template for RA computations
+    RA_t ra[NB_RA_PROC_MAX];
+    /// VRB map for common channels
+    uint8_t vrb_map[100];
+    /// VRB map for common channels and retransmissions by PHICH
+    uint8_t vrb_map_UL[100];
+    /// MBSFN SubframeConfig
+    struct MBSFN_SubframeConfig *mbsfn_SubframeConfig[8];
+    /// number of subframe allocation pattern available for MBSFN sync area
+    uint8_t num_sf_allocation_pattern;
 #if defined(Rel10) || defined(Rel14)
-  /// MBMS Flag
-  uint8_t MBMS_flag;
-  /// Outgoing MCCH pdu for PHY
-  MCCH_PDU MCCH_pdu;
-  /// MCCH active flag
-  uint8_t msi_active;
-  /// MCCH active flag
-  uint8_t mcch_active;
-  /// MTCH active flag
-  uint8_t mtch_active;
-  /// number of active MBSFN area
-  uint8_t num_active_mbsfn_area;
-  /// MBSFN Area Info
-  struct  MBSFN_AreaInfo_r9 *mbsfn_AreaInfo[MAX_MBSFN_AREA];
-  /// PMCH Config
-  struct PMCH_Config_r9 *pmch_Config[MAX_PMCH_perMBSFN];
-  /// MBMS session info list
-  struct MBMS_SessionInfoList_r9 *mbms_SessionList[MAX_PMCH_perMBSFN];
-  /// Outgoing MCH pdu for PHY
-  MCH_PDU MCH_pdu;
+    /// MBMS Flag
+    uint8_t MBMS_flag;
+    /// Outgoing MCCH pdu for PHY
+    MCCH_PDU MCCH_pdu;
+    /// MCCH active flag
+    uint8_t msi_active;
+    /// MCCH active flag
+    uint8_t mcch_active;
+    /// MTCH active flag
+    uint8_t mtch_active;
+    /// number of active MBSFN area
+    uint8_t num_active_mbsfn_area;
+    /// MBSFN Area Info
+    struct MBSFN_AreaInfo_r9 *mbsfn_AreaInfo[MAX_MBSFN_AREA];
+    /// PMCH Config
+    struct PMCH_Config_r9 *pmch_Config[MAX_PMCH_perMBSFN];
+    /// MBMS session info list
+    struct MBMS_SessionInfoList_r9 *mbms_SessionList[MAX_PMCH_perMBSFN];
+    /// Outgoing MCH pdu for PHY
+    MCH_PDU MCH_pdu;
 #endif
 #ifdef Rel14
-  /// Rel13 parameters from SIB1
-  SystemInformationBlockType1_v1310_IEs_t *sib1_v13ext;
-  /// Counter for SIB1-BR scheduling
-  int SIB1_BR_cnt;
-  /// Outgoing BCCH-BR pdu for PHY
-  BCCH_PDU BCCH_BR_pdu[20];
+    /// Rel13 parameters from SIB1
+    SystemInformationBlockType1_v1310_IEs_t *sib1_v13ext;
+    /// Counter for SIB1-BR scheduling
+    int SIB1_BR_cnt;
+    /// Outgoing BCCH-BR pdu for PHY
+    BCCH_PDU BCCH_BR_pdu[20];
 #endif
 } COMMON_channels_t;
-/*! \brief top level eNB MAC structure */ 
+/*! \brief top level eNB MAC structure */
 typedef struct eNB_MAC_INST_s {
-  /// Ethernet parameters for northbound midhaul interface
-  eth_params_t         eth_params_n;
-  /// Ethernet parameters for fronthaul interface
-  eth_params_t         eth_params_s;
-  ///
-  module_id_t Mod_id;
-  /// frame counter
-  frame_t frame;
-  /// subframe counter
-  sub_frame_t subframe;
-  /// Pointer to IF module instance for PHY
-  IF_Module_t *if_inst;
-  /// Common cell resources
-  COMMON_channels_t common_channels[MAX_NUM_CCs];
-  /// current PDU index (BCH,MCH,DLSCH)
-  uint16_t pdu_index[MAX_NUM_CCs];
-
-  /// NFAPI Config Request Structure
-  nfapi_config_request_t config[MAX_NUM_CCs];
-  /// Preallocated DL pdu list
-  nfapi_dl_config_request_pdu_t dl_config_pdu_list[MAX_NUM_CCs][MAX_NUM_DL_PDU];
-  /// NFAPI DL Config Request Structure
-  nfapi_dl_config_request_t DL_req[MAX_NUM_CCs];
-  /// Preallocated UL pdu list
-  nfapi_ul_config_request_pdu_t ul_config_pdu_list[MAX_NUM_CCs][MAX_NUM_UL_PDU];
-  /// Preallocated UL pdu list for ULSCH (n+k delay)
-  nfapi_ul_config_request_pdu_t ul_config_pdu_list_tmp[MAX_NUM_CCs][10][MAX_NUM_UL_PDU];
-  /// NFAPI UL Config Request Structure, send to L1 4 subframes before processing takes place
-  nfapi_ul_config_request_t UL_req[MAX_NUM_CCs];
-  /// NFAPI "Temporary" UL Config Request Structure, holds future UL_config requests
-  nfapi_ul_config_request_t UL_req_tmp[MAX_NUM_CCs][10];
-  /// Preallocated HI_DCI0 pdu list 
-  nfapi_hi_dci0_request_pdu_t hi_dci0_pdu_list[MAX_NUM_CCs][MAX_NUM_HI_DCI0_PDU];
-  /// NFAPI HI/DCI0 Config Request Structure
-  nfapi_hi_dci0_request_t HI_DCI0_req[MAX_NUM_CCs];
-  /// Prealocated TX pdu list
-  nfapi_tx_request_pdu_t tx_request_pdu[MAX_NUM_CCs][MAX_NUM_TX_REQUEST_PDU];
-  /// NFAPI DL PDU structure
-  nfapi_tx_request_t TX_req[MAX_NUM_CCs];
-  /// UL handle
-  uint32_t ul_handle;
-  UE_list_t UE_list;
-
-  ///subband bitmap configuration
-  SBMAP_CONF sbmap_conf;
-  /// CCE table used to build DCI scheduling information
-  int CCE_table[MAX_NUM_CCs][800];
-  ///  active flag for Other lcid
-  uint8_t lcid_active[NB_RB_MAX];
-  /// eNB stats
-  eNB_STATS eNB_stats[MAX_NUM_CCs];
-  // MAC function execution peformance profiler
-  /// processing time of eNB scheduler 
-  time_stats_t eNB_scheduler;
-  /// processing time of eNB scheduler for SI 
-  time_stats_t schedule_si;
-  /// processing time of eNB scheduler for Random access
-  time_stats_t schedule_ra;
-  /// processing time of eNB ULSCH scheduler 
-  time_stats_t schedule_ulsch;
-  /// processing time of eNB DCI generation
-  time_stats_t fill_DLSCH_dci;
-  /// processing time of eNB MAC preprocessor
-  time_stats_t schedule_dlsch_preprocessor;
-  /// processing time of eNB DLSCH scheduler 
-  time_stats_t schedule_dlsch; // include rlc_data_req + MAC header + preprocessor
-  /// processing time of eNB MCH scheduler 
-  time_stats_t schedule_mch;
-  /// processing time of eNB ULSCH reception
-  time_stats_t rx_ulsch_sdu; // include rlc_data_ind
+    /// Ethernet parameters for northbound midhaul interface
+    eth_params_t eth_params_n;
+    /// Ethernet parameters for fronthaul interface
+    eth_params_t eth_params_s;
+    ///
+    module_id_t Mod_id;
+    /// frame counter
+    frame_t frame;
+    /// subframe counter
+    sub_frame_t subframe;
+    /// Pointer to IF module instance for PHY
+    IF_Module_t *if_inst;
+    /// Common cell resources
+    COMMON_channels_t common_channels[MAX_NUM_CCs];
+    /// current PDU index (BCH,MCH,DLSCH)
+    uint16_t pdu_index[MAX_NUM_CCs];
+
+    /// NFAPI Config Request Structure
+    nfapi_config_request_t config[MAX_NUM_CCs];
+    /// Preallocated DL pdu list
+    nfapi_dl_config_request_pdu_t
+	dl_config_pdu_list[MAX_NUM_CCs][MAX_NUM_DL_PDU];
+    /// NFAPI DL Config Request Structure
+    nfapi_dl_config_request_t DL_req[MAX_NUM_CCs];
+    /// Preallocated UL pdu list
+    nfapi_ul_config_request_pdu_t
+	ul_config_pdu_list[MAX_NUM_CCs][MAX_NUM_UL_PDU];
+    /// Preallocated UL pdu list for ULSCH (n+k delay)
+    nfapi_ul_config_request_pdu_t
+	ul_config_pdu_list_tmp[MAX_NUM_CCs][10][MAX_NUM_UL_PDU];
+    /// NFAPI UL Config Request Structure, send to L1 4 subframes before processing takes place
+    nfapi_ul_config_request_t UL_req[MAX_NUM_CCs];
+    /// NFAPI "Temporary" UL Config Request Structure, holds future UL_config requests
+    nfapi_ul_config_request_t UL_req_tmp[MAX_NUM_CCs][10];
+    /// Preallocated HI_DCI0 pdu list 
+    nfapi_hi_dci0_request_pdu_t
+	hi_dci0_pdu_list[MAX_NUM_CCs][MAX_NUM_HI_DCI0_PDU];
+    /// NFAPI HI/DCI0 Config Request Structure
+    nfapi_hi_dci0_request_t HI_DCI0_req[MAX_NUM_CCs];
+    /// Prealocated TX pdu list
+    nfapi_tx_request_pdu_t
+	tx_request_pdu[MAX_NUM_CCs][MAX_NUM_TX_REQUEST_PDU];
+    /// NFAPI DL PDU structure
+    nfapi_tx_request_t TX_req[MAX_NUM_CCs];
+    /// UL handle
+    uint32_t ul_handle;
+    UE_list_t UE_list;
+
+    ///subband bitmap configuration
+    SBMAP_CONF sbmap_conf;
+    /// CCE table used to build DCI scheduling information
+    int CCE_table[MAX_NUM_CCs][800];
+    ///  active flag for Other lcid
+    uint8_t lcid_active[NB_RB_MAX];
+    /// eNB stats
+    eNB_STATS eNB_stats[MAX_NUM_CCs];
+    // MAC function execution peformance profiler
+    /// processing time of eNB scheduler 
+    time_stats_t eNB_scheduler;
+    /// processing time of eNB scheduler for SI 
+    time_stats_t schedule_si;
+    /// processing time of eNB scheduler for Random access
+    time_stats_t schedule_ra;
+    /// processing time of eNB ULSCH scheduler 
+    time_stats_t schedule_ulsch;
+    /// processing time of eNB DCI generation
+    time_stats_t fill_DLSCH_dci;
+    /// processing time of eNB MAC preprocessor
+    time_stats_t schedule_dlsch_preprocessor;
+    /// processing time of eNB DLSCH scheduler 
+    time_stats_t schedule_dlsch;	// include rlc_data_req + MAC header + preprocessor
+    /// processing time of eNB MCH scheduler 
+    time_stats_t schedule_mch;
+    /// processing time of eNB ULSCH reception
+    time_stats_t rx_ulsch_sdu;	// include rlc_data_ind
 } eNB_MAC_INST;
 
 /* 
  * UE part 
- */ 
+ */
 
 typedef enum {
-  TYPE0,
-  TYPE1,
-  TYPE1A,
-  TYPE2,
-  TYPE2A,
-  TYPEUESPEC
+    TYPE0,
+    TYPE1,
+    TYPE1A,
+    TYPE2,
+    TYPE2A,
+    TYPEUESPEC
 } MPDCCH_TYPES_t;
 
 /*!\brief UE layer 2 status */
 typedef enum {
-  CONNECTION_OK=0,
-  CONNECTION_LOST,
-  PHY_RESYNCH,
-  PHY_HO_PRACH
+    CONNECTION_OK = 0,
+    CONNECTION_LOST,
+    PHY_RESYNCH,
+    PHY_HO_PRACH
 } UE_L2_STATE_t;
 
 /*!\brief UE scheduling info */
 typedef struct {
-  /// buffer status for each lcgid
-  uint8_t  BSR[MAX_NUM_LCGID]; // should be more for mesh topology
-  /// keep the number of bytes in rlc buffer for each lcgid
-  int32_t  BSR_bytes[MAX_NUM_LCGID];
-  /// after multiplexing buffer remain for each lcid
-  int32_t  LCID_buffer_remain[MAX_NUM_LCID];
-  /// sum of all lcid buffer size
-  uint16_t  All_lcid_buffer_size_lastTTI;
-  /// buffer status for each lcid
-  uint8_t  LCID_status[MAX_NUM_LCID];
-  /// SR pending as defined in 36.321
-  uint8_t  SR_pending;
-  /// SR_COUNTER as defined in 36.321
-  uint16_t SR_COUNTER;
-  /// logical channel group ide for each LCID
-  uint8_t  LCGID[MAX_NUM_LCID];
-  /// retxBSR-Timer, default value is sf2560
-  uint16_t retxBSR_Timer;
-  /// retxBSR_SF, number of subframe before triggering a regular BSR
-  uint16_t retxBSR_SF;
-  /// periodicBSR-Timer, default to infinity
-  uint16_t periodicBSR_Timer;
-  /// periodicBSR_SF, number of subframe before triggering a periodic BSR
-  uint16_t periodicBSR_SF;
-  /// default value is 0: not configured
-  uint16_t sr_ProhibitTimer;
-  /// sr ProhibitTime running
-  uint8_t sr_ProhibitTimer_Running;
-  ///  default value to n5
-  uint16_t maxHARQ_Tx;
-  /// default value is false
-  uint16_t ttiBundling;
-  /// default value is release
-  struct DRX_Config *drx_config;
-  /// default value is release
-  struct MAC_MainConfig__phr_Config *phr_config;
-  ///timer before triggering a periodic PHR
-  uint16_t periodicPHR_Timer;
-  ///timer before triggering a prohibit PHR
-  uint16_t prohibitPHR_Timer;
-  ///DL Pathloss change value
-  uint16_t PathlossChange;
-  ///number of subframe before triggering a periodic PHR
-  int16_t periodicPHR_SF;
-  ///number of subframe before triggering a prohibit PHR
-  int16_t prohibitPHR_SF;
-  ///DL Pathloss Change in db
-  uint16_t PathlossChange_db;
-
-  /// default value is false
-  uint16_t extendedBSR_Sizes_r10;
-  /// default value is false
-  uint16_t extendedPHR_r10;
-
-  //Bj bucket usage per  lcid
-  int16_t Bj[MAX_NUM_LCID];
-  // Bucket size per lcid
-  int16_t bucket_size[MAX_NUM_LCID];
+    /// buffer status for each lcgid
+    uint8_t BSR[MAX_NUM_LCGID];	// should be more for mesh topology
+    /// keep the number of bytes in rlc buffer for each lcgid
+    int32_t BSR_bytes[MAX_NUM_LCGID];
+    /// after multiplexing buffer remain for each lcid
+    int32_t LCID_buffer_remain[MAX_NUM_LCID];
+    /// sum of all lcid buffer size
+    uint16_t All_lcid_buffer_size_lastTTI;
+    /// buffer status for each lcid
+    uint8_t LCID_status[MAX_NUM_LCID];
+    /// SR pending as defined in 36.321
+    uint8_t SR_pending;
+    /// SR_COUNTER as defined in 36.321
+    uint16_t SR_COUNTER;
+    /// logical channel group ide for each LCID
+    uint8_t LCGID[MAX_NUM_LCID];
+    /// retxBSR-Timer, default value is sf2560
+    uint16_t retxBSR_Timer;
+    /// retxBSR_SF, number of subframe before triggering a regular BSR
+    uint16_t retxBSR_SF;
+    /// periodicBSR-Timer, default to infinity
+    uint16_t periodicBSR_Timer;
+    /// periodicBSR_SF, number of subframe before triggering a periodic BSR
+    uint16_t periodicBSR_SF;
+    /// default value is 0: not configured
+    uint16_t sr_ProhibitTimer;
+    /// sr ProhibitTime running
+    uint8_t sr_ProhibitTimer_Running;
+    ///  default value to n5
+    uint16_t maxHARQ_Tx;
+    /// default value is false
+    uint16_t ttiBundling;
+    /// default value is release
+    struct DRX_Config *drx_config;
+    /// default value is release
+    struct MAC_MainConfig__phr_Config *phr_config;
+    ///timer before triggering a periodic PHR
+    uint16_t periodicPHR_Timer;
+    ///timer before triggering a prohibit PHR
+    uint16_t prohibitPHR_Timer;
+    ///DL Pathloss change value
+    uint16_t PathlossChange;
+    ///number of subframe before triggering a periodic PHR
+    int16_t periodicPHR_SF;
+    ///number of subframe before triggering a prohibit PHR
+    int16_t prohibitPHR_SF;
+    ///DL Pathloss Change in db
+    uint16_t PathlossChange_db;
+
+    /// default value is false
+    uint16_t extendedBSR_Sizes_r10;
+    /// default value is false
+    uint16_t extendedPHR_r10;
+
+    //Bj bucket usage per  lcid
+    int16_t Bj[MAX_NUM_LCID];
+    // Bucket size per lcid
+    int16_t bucket_size[MAX_NUM_LCID];
 } UE_SCHEDULING_INFO;
 /*!\brief Top level UE MAC structure */
 typedef struct {
-  uint16_t Node_id;
-  /// RX frame counter
-  frame_t     rxFrame;
-  /// RX subframe counter
-  sub_frame_t rxSubframe;
-  /// TX frame counter
-  frame_t     txFrame;
-  /// TX subframe counter
-  sub_frame_t txSubframe;
-  /// C-RNTI of UE
-  uint16_t crnti;
-  /// C-RNTI of UE before HO
-  rnti_t crnti_before_ho; ///user id (rnti) of connected UEs
-  /// uplink active flag
-  uint8_t ul_active;
-  /// pointer to RRC PHY configuration
-  RadioResourceConfigCommonSIB_t *radioResourceConfigCommon;
-  /// pointer to RACH_ConfigDedicated (NULL when not active, i.e. upon HO completion or T304 expiry)
-  struct RACH_ConfigDedicated *rach_ConfigDedicated;
-  /// pointer to RRC PHY configuration
-  struct PhysicalConfigDedicated *physicalConfigDedicated;
+    uint16_t Node_id;
+    /// RX frame counter
+    frame_t rxFrame;
+    /// RX subframe counter
+    sub_frame_t rxSubframe;
+    /// TX frame counter
+    frame_t txFrame;
+    /// TX subframe counter
+    sub_frame_t txSubframe;
+    /// C-RNTI of UE
+    uint16_t crnti;
+    /// C-RNTI of UE before HO
+    rnti_t crnti_before_ho;	///user id (rnti) of connected UEs
+    /// uplink active flag
+    uint8_t ul_active;
+    /// pointer to RRC PHY configuration
+    RadioResourceConfigCommonSIB_t *radioResourceConfigCommon;
+    /// pointer to RACH_ConfigDedicated (NULL when not active, i.e. upon HO completion or T304 expiry)
+    struct RACH_ConfigDedicated *rach_ConfigDedicated;
+    /// pointer to RRC PHY configuration
+    struct PhysicalConfigDedicated *physicalConfigDedicated;
 #if defined(Rel10) || defined(Rel14)
-  /// pointer to RRC PHY configuration SCEll
-  struct PhysicalConfigDedicatedSCell_r10 *physicalConfigDedicatedSCell_r10;
+    /// pointer to RRC PHY configuration SCEll
+    struct PhysicalConfigDedicatedSCell_r10
+	*physicalConfigDedicatedSCell_r10;
 #endif
-  /// pointer to TDD Configuration (NULL for FDD)
-  TDD_Config_t *tdd_Config;
-  /// Number of adjacent cells to measure
-  uint8_t  n_adj_cells;
-  /// Array of adjacent physical cell ids
-  uint32_t adj_cell_id[6];
-  /// Pointer to RRC MAC configuration
-  MAC_MainConfig_t *macConfig;
-  /// Pointer to RRC Measurement gap configuration
-  MeasGapConfig_t  *measGapConfig;
-  /// Pointers to LogicalChannelConfig indexed by LogicalChannelIdentity. Note NULL means LCHAN is inactive.
-  LogicalChannelConfig_t *logicalChannelConfig[MAX_NUM_LCID];
-  /// Scheduling Information
-  UE_SCHEDULING_INFO scheduling_info;
-  /// Outgoing CCCH pdu for PHY
-  CCCH_PDU CCCH_pdu;
-  /// Outgoing RAR pdu for PHY
-  RAR_PDU RAR_pdu;
-  /// Incoming DLSCH pdu for PHY
-  DLSCH_PDU DLSCH_pdu[NUMBER_OF_UE_MAX][2];
-  /// number of attempt for rach
-  uint8_t RA_attempt_number;
-  /// Random-access procedure flag
-  uint8_t RA_active;
-  /// Random-access window counter
-  int8_t RA_window_cnt;
-  /// Random-access Msg3 size in bytes
-  uint8_t RA_Msg3_size;
-  /// Random-access prachMaskIndex
-  uint8_t RA_prachMaskIndex;
-  /// Flag indicating Preamble set (A,B) used for first Msg3 transmission
-  uint8_t RA_usedGroupA;
-  /// Random-access Resources
-  PRACH_RESOURCES_t RA_prach_resources;
-  /// Random-access PREAMBLE_TRANSMISSION_COUNTER
-  uint8_t RA_PREAMBLE_TRANSMISSION_COUNTER;
-  /// Random-access backoff counter
-  int16_t RA_backoff_cnt;
-  /// Random-access variable for window calculation (frame of last change in window counter)
-  uint32_t RA_tx_frame;
-  /// Random-access variable for window calculation (subframe of last change in window counter)
-  uint8_t RA_tx_subframe;
-  /// Random-access Group B maximum path-loss
-  /// Random-access variable for backoff (frame of last change in backoff counter)
-  uint32_t RA_backoff_frame;
-  /// Random-access variable for backoff (subframe of last change in backoff counter)
-  uint8_t RA_backoff_subframe;
-  /// Random-access Group B maximum path-loss
-  uint16_t RA_maxPL;
-  /// Random-access Contention Resolution Timer active flag
-  uint8_t RA_contention_resolution_timer_active;
-  /// Random-access Contention Resolution Timer count value
-  uint8_t RA_contention_resolution_cnt;
-  /// power headroom reporitng reconfigured
-  uint8_t PHR_reconfigured;
-  /// power headroom state as configured by the higher layers
-  uint8_t PHR_state;
-  /// power backoff due to power management (as allowed by P-MPRc) for this cell
-  uint8_t PHR_reporting_active;
-  /// power backoff due to power management (as allowed by P-MPRc) for this cell
-  uint8_t power_backoff_db[NUMBER_OF_eNB_MAX];
-  /// BSR report falg management
-  uint8_t BSR_reporting_active;
-  /// retxBSR-Timer expires flag
-  uint8_t retxBSRTimer_expires_flag;
-  /// periodBSR-Timer expires flag
-  uint8_t periodBSRTimer_expires_flag;
-
-  /// MBSFN_Subframe Configuration
-  struct MBSFN_SubframeConfig *mbsfn_SubframeConfig[8]; // FIXME replace 8 by MAX_MBSFN_AREA?
-  /// number of subframe allocation pattern available for MBSFN sync area
-  uint8_t num_sf_allocation_pattern;
+    /// pointer to TDD Configuration (NULL for FDD)
+    TDD_Config_t *tdd_Config;
+    /// Number of adjacent cells to measure
+    uint8_t n_adj_cells;
+    /// Array of adjacent physical cell ids
+    uint32_t adj_cell_id[6];
+    /// Pointer to RRC MAC configuration
+    MAC_MainConfig_t *macConfig;
+    /// Pointer to RRC Measurement gap configuration
+    MeasGapConfig_t *measGapConfig;
+    /// Pointers to LogicalChannelConfig indexed by LogicalChannelIdentity. Note NULL means LCHAN is inactive.
+    LogicalChannelConfig_t *logicalChannelConfig[MAX_NUM_LCID];
+    /// Scheduling Information
+    UE_SCHEDULING_INFO scheduling_info;
+    /// Outgoing CCCH pdu for PHY
+    CCCH_PDU CCCH_pdu;
+    /// Outgoing RAR pdu for PHY
+    RAR_PDU RAR_pdu;
+    /// Incoming DLSCH pdu for PHY
+    DLSCH_PDU DLSCH_pdu[NUMBER_OF_UE_MAX][2];
+    /// number of attempt for rach
+    uint8_t RA_attempt_number;
+    /// Random-access procedure flag
+    uint8_t RA_active;
+    /// Random-access window counter
+    int8_t RA_window_cnt;
+    /// Random-access Msg3 size in bytes
+    uint8_t RA_Msg3_size;
+    /// Random-access prachMaskIndex
+    uint8_t RA_prachMaskIndex;
+    /// Flag indicating Preamble set (A,B) used for first Msg3 transmission
+    uint8_t RA_usedGroupA;
+    /// Random-access Resources
+    PRACH_RESOURCES_t RA_prach_resources;
+    /// Random-access PREAMBLE_TRANSMISSION_COUNTER
+    uint8_t RA_PREAMBLE_TRANSMISSION_COUNTER;
+    /// Random-access backoff counter
+    int16_t RA_backoff_cnt;
+    /// Random-access variable for window calculation (frame of last change in window counter)
+    uint32_t RA_tx_frame;
+    /// Random-access variable for window calculation (subframe of last change in window counter)
+    uint8_t RA_tx_subframe;
+    /// Random-access Group B maximum path-loss
+    /// Random-access variable for backoff (frame of last change in backoff counter)
+    uint32_t RA_backoff_frame;
+    /// Random-access variable for backoff (subframe of last change in backoff counter)
+    uint8_t RA_backoff_subframe;
+    /// Random-access Group B maximum path-loss
+    uint16_t RA_maxPL;
+    /// Random-access Contention Resolution Timer active flag
+    uint8_t RA_contention_resolution_timer_active;
+    /// Random-access Contention Resolution Timer count value
+    uint8_t RA_contention_resolution_cnt;
+    /// power headroom reporitng reconfigured
+    uint8_t PHR_reconfigured;
+    /// power headroom state as configured by the higher layers
+    uint8_t PHR_state;
+    /// power backoff due to power management (as allowed by P-MPRc) for this cell
+    uint8_t PHR_reporting_active;
+    /// power backoff due to power management (as allowed by P-MPRc) for this cell
+    uint8_t power_backoff_db[NUMBER_OF_eNB_MAX];
+    /// BSR report falg management
+    uint8_t BSR_reporting_active;
+    /// retxBSR-Timer expires flag
+    uint8_t retxBSRTimer_expires_flag;
+    /// periodBSR-Timer expires flag
+    uint8_t periodBSRTimer_expires_flag;
+
+    /// MBSFN_Subframe Configuration
+    struct MBSFN_SubframeConfig *mbsfn_SubframeConfig[8];	// FIXME replace 8 by MAX_MBSFN_AREA?
+    /// number of subframe allocation pattern available for MBSFN sync area
+    uint8_t num_sf_allocation_pattern;
 #if defined(Rel10) || defined(Rel14)
-  /// number of active MBSFN area
-  uint8_t num_active_mbsfn_area;
-  /// MBSFN Area Info
-  struct  MBSFN_AreaInfo_r9 *mbsfn_AreaInfo[MAX_MBSFN_AREA];
-  /// PMCH Config
-  struct PMCH_Config_r9 *pmch_Config[MAX_PMCH_perMBSFN];
-  /// MCCH status
-  uint8_t mcch_status;
-  /// MSI status
-  uint8_t msi_status;// could be an array if there are >1 MCH in one MBSFN area
+    /// number of active MBSFN area
+    uint8_t num_active_mbsfn_area;
+    /// MBSFN Area Info
+    struct MBSFN_AreaInfo_r9 *mbsfn_AreaInfo[MAX_MBSFN_AREA];
+    /// PMCH Config
+    struct PMCH_Config_r9 *pmch_Config[MAX_PMCH_perMBSFN];
+    /// MCCH status
+    uint8_t mcch_status;
+    /// MSI status
+    uint8_t msi_status;		// could be an array if there are >1 MCH in one MBSFN area
 #endif
-  //#ifdef CBA
-  /// CBA RNTI for each group 
-  uint16_t cba_rnti[NUM_MAX_CBA_GROUP];
-  /// last SFN for CBA channel access 
-  uint8_t cba_last_access[NUM_MAX_CBA_GROUP];
-  //#endif
-  /// total UE scheduler processing time 
-  time_stats_t ue_scheduler; // total
-  /// UE ULSCH tx  processing time inlcuding RLC interface (rlc_data_req) and mac header generation 
-  time_stats_t tx_ulsch_sdu;  
-  /// UE DLSCH rx  processing time inlcuding RLC interface (mac_rrc_data_ind or mac_rlc_status_ind+mac_rlc_data_ind) and mac header parser
-  time_stats_t rx_dlsch_sdu ; 
-  /// UE query for MCH subframe processing time 
-  time_stats_t ue_query_mch;
-  /// UE MCH rx processing time 
-  time_stats_t rx_mch_sdu;
-  /// UE BCCH rx processing time including RLC interface (mac_rrc_data_ind) 
-  time_stats_t rx_si; 
-  /// UE PCCH rx processing time including RLC interface (mac_rrc_data_ind) 
-  time_stats_t rx_p; 
+    //#ifdef CBA
+    /// CBA RNTI for each group 
+    uint16_t cba_rnti[NUM_MAX_CBA_GROUP];
+    /// last SFN for CBA channel access 
+    uint8_t cba_last_access[NUM_MAX_CBA_GROUP];
+    //#endif
+    /// total UE scheduler processing time 
+    time_stats_t ue_scheduler;	// total
+    /// UE ULSCH tx  processing time inlcuding RLC interface (rlc_data_req) and mac header generation 
+    time_stats_t tx_ulsch_sdu;
+    /// UE DLSCH rx  processing time inlcuding RLC interface (mac_rrc_data_ind or mac_rlc_status_ind+mac_rlc_data_ind) and mac header parser
+    time_stats_t rx_dlsch_sdu;
+    /// UE query for MCH subframe processing time 
+    time_stats_t ue_query_mch;
+    /// UE MCH rx processing time 
+    time_stats_t rx_mch_sdu;
+    /// UE BCCH rx processing time including RLC interface (mac_rrc_data_ind) 
+    time_stats_t rx_si;
+    /// UE PCCH rx processing time including RLC interface (mac_rrc_data_ind) 
+    time_stats_t rx_p;
 } UE_MAC_INST;
 /*! \brief ID of the neighboring cells used for HO*/
 typedef struct {
-  uint16_t cell_ids[6];
-  uint8_t n_adj_cells;
+    uint16_t cell_ids[6];
+    uint8_t n_adj_cells;
 } neigh_cell_id_t;
 
 #include "proto.h"
 /*@}*/
 #endif /*__LAYER2_MAC_DEFS_H__ */
-
-
-
diff --git a/openair2/LAYER2/MAC/eNB_scheduler.c b/openair2/LAYER2/MAC/eNB_scheduler.c
index 6bcbc131068dddd4279f803880be89c34f1190ae..f7c3f5829d6b9005d632dcc9b5866011b86c1ed0 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler.c
@@ -60,7 +60,7 @@
 #endif
 
 #if defined(ENABLE_ITTI)
-# include "intertask_interface.h"
+#include "intertask_interface.h"
 #endif
 
 #define ENABLE_MAC_PAYLOAD_DEBUG
@@ -68,481 +68,693 @@
 
 extern RAN_CONTEXT_t RC;
 
-uint16_t pdcch_order_table[6] = {31,31,511,2047,2047,8191};
+uint16_t pdcch_order_table[6] = { 31, 31, 511, 2047, 2047, 8191 };
 
 
-void schedule_SRS(module_id_t module_idP,frame_t frameP,sub_frame_t subframeP)
+void
+schedule_SRS(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP)
 {
-  eNB_MAC_INST                   *eNB            = RC.mac[module_idP];
-  UE_list_t                      *UE_list        = &eNB->UE_list;
-  nfapi_ul_config_request_body_t *ul_req;
-  int CC_id,UE_id;
-  COMMON_channels_t                   *cc        = RC.mac[module_idP]->common_channels;
-  SoundingRS_UL_ConfigCommon_t      *soundingRS_UL_ConfigCommon;
-  struct SoundingRS_UL_ConfigDedicated     *soundingRS_UL_ConfigDedicated;
-  uint8_t  TSFC;
-  uint16_t deltaTSFC; // bitmap
-  uint8_t  srs_SubframeConfig;
-
-  // table for TSFC (Period) and deltaSFC (offset)
-  const uint16_t deltaTSFCTabType1[15][2] = { {1,1},{1,2},{2,2},{1,5},{2,5},{4,5},{8,5},{3,5},{12,5},{1,10},{2,10},{4,10},{8,10},{351,10},{383,10} };      // Table 5.5.3.3-2 3GPP 36.211 FDD
-  const uint16_t deltaTSFCTabType2[14][2] = { {2,5},{6,5},{10,5},{18,5},{14,5},{22,5},{26,5},{30,5},{70,10},{74,10},{194,10},{326,10},{586,10},{210,10} }; // Table 5.5.3.3-2 3GPP 36.211 TDD
-
-  uint16_t srsPeriodicity,srsOffset;
-
-  for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
-    soundingRS_UL_ConfigCommon = &cc[CC_id].radioResourceConfigCommon->soundingRS_UL_ConfigCommon;
-    // check if SRS is enabled in this frame/subframe
-    if (soundingRS_UL_ConfigCommon) {
-      srs_SubframeConfig = soundingRS_UL_ConfigCommon->choice.setup.srs_SubframeConfig;
-      if (cc[CC_id].tdd_Config == NULL) { // FDD
-	deltaTSFC = deltaTSFCTabType1[srs_SubframeConfig][0];
-	TSFC      = deltaTSFCTabType1[srs_SubframeConfig][1];
-      }
-      else { // TDD
-	deltaTSFC = deltaTSFCTabType2[srs_SubframeConfig][0];
-	TSFC      = deltaTSFCTabType2[srs_SubframeConfig][1];
-      }
-      // Sounding reference signal subframes are the subframes satisfying ns/2 mod TSFC (- deltaTSFC
-      uint16_t tmp = (subframeP %  TSFC);
-
-      if((1<<tmp) & deltaTSFC) {
-	// This is an SRS subframe, loop over UEs
-	for (UE_id=0; UE_id<NUMBER_OF_UE_MAX; UE_id++) {
-	  if (RC.mac[module_idP]->UE_list.active[UE_id]!=TRUE) continue;
-	  ul_req        = &RC.mac[module_idP]->UL_req[CC_id].ul_config_request_body;
-
-
-          // drop the allocation if the UE hasn't send RRCConnectionSetupComplete yet
-          if (mac_eNB_get_rrc_status(module_idP,UE_RNTI(module_idP,UE_id)) < RRC_CONNECTED) continue;
-
-	  AssertFatal(UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated != NULL, "physicalConfigDedicated is null for UE %d\n",UE_id);
-
-	  if ((soundingRS_UL_ConfigDedicated = UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->soundingRS_UL_ConfigDedicated)!=NULL) {
-	    if (soundingRS_UL_ConfigDedicated->present == SoundingRS_UL_ConfigDedicated_PR_setup) {
-	      get_srs_pos(&cc[CC_id], soundingRS_UL_ConfigDedicated->choice.setup.srs_ConfigIndex, &srsPeriodicity, &srsOffset);
-	      if (((10*frameP+subframeP) % srsPeriodicity) == srsOffset) {
-		// Prorgram SRS
-		ul_req->srs_present                                                 = 1;
-		nfapi_ul_config_request_pdu_t* ul_config_pdu                        = &ul_req->ul_config_pdu_list[ul_req->number_of_pdus];
-		memset((void*)ul_config_pdu,0,sizeof(nfapi_ul_config_request_pdu_t));
-		ul_config_pdu->pdu_type                                             = NFAPI_UL_CONFIG_SRS_PDU_TYPE;
-		ul_config_pdu->pdu_size                                             = 2+(uint8_t)(2+sizeof(nfapi_ul_config_srs_pdu));
-		ul_config_pdu->srs_pdu.srs_pdu_rel8.size                            = (uint8_t)sizeof(nfapi_ul_config_srs_pdu);;
-		ul_config_pdu->srs_pdu.srs_pdu_rel8.rnti                            = UE_list->UE_template[CC_id][UE_id].rnti;
-		ul_config_pdu->srs_pdu.srs_pdu_rel8.srs_bandwidth                   = soundingRS_UL_ConfigDedicated->choice.setup.srs_Bandwidth;
-		ul_config_pdu->srs_pdu.srs_pdu_rel8.frequency_domain_position       = soundingRS_UL_ConfigDedicated->choice.setup.freqDomainPosition;
-		ul_config_pdu->srs_pdu.srs_pdu_rel8.srs_hopping_bandwidth           = soundingRS_UL_ConfigDedicated->choice.setup.srs_HoppingBandwidth;;
-		ul_config_pdu->srs_pdu.srs_pdu_rel8.transmission_comb               = soundingRS_UL_ConfigDedicated->choice.setup.transmissionComb;
-		ul_config_pdu->srs_pdu.srs_pdu_rel8.i_srs                           = soundingRS_UL_ConfigDedicated->choice.setup.srs_ConfigIndex;
-		ul_config_pdu->srs_pdu.srs_pdu_rel8.sounding_reference_cyclic_shift = soundingRS_UL_ConfigDedicated->choice.setup.cyclicShift;
-		//		ul_config_pdu->srs_pdu.srs_pdu_rel10.antenna_port                   = ;//
-		//		ul_config_pdu->srs_pdu.srs_pdu_rel13.number_of_combs                = ;//
-		RC.mac[module_idP]->UL_req[CC_id].sfn_sf = (frameP<<4)+subframeP;
-		ul_req->number_of_pdus++;
-	      } // if (((10*frameP+subframeP) % srsPeriodicity) == srsOffset)
-	    } // if (soundingRS_UL_ConfigDedicated->present == SoundingRS_UL_ConfigDedicated_PR_setup)
-	  } // if ((soundingRS_UL_ConfigDedicated = UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->soundingRS_UL_ConfigDedicated)!=NULL)
-	} // for (UE_id ...
-      } // if((1<<tmp) & deltaTSFC)
-
-    }// SRS config
-  }
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    UE_list_t *UE_list = &eNB->UE_list;
+    nfapi_ul_config_request_body_t *ul_req;
+    int CC_id, UE_id;
+    COMMON_channels_t *cc = RC.mac[module_idP]->common_channels;
+    SoundingRS_UL_ConfigCommon_t *soundingRS_UL_ConfigCommon;
+    struct SoundingRS_UL_ConfigDedicated *soundingRS_UL_ConfigDedicated;
+    uint8_t TSFC;
+    uint16_t deltaTSFC;		// bitmap
+    uint8_t srs_SubframeConfig;
+
+    // table for TSFC (Period) and deltaSFC (offset)
+    const uint16_t deltaTSFCTabType1[15][2] = { {1, 1}, {1, 2}, {2, 2}, {1, 5}, {2, 5}, {4, 5}, {8, 5}, {3, 5}, {12, 5}, {1, 10}, {2, 10}, {4, 10}, {8, 10}, {351, 10}, {383, 10} };	// Table 5.5.3.3-2 3GPP 36.211 FDD
+    const uint16_t deltaTSFCTabType2[14][2] = { {2, 5}, {6, 5}, {10, 5}, {18, 5}, {14, 5}, {22, 5}, {26, 5}, {30, 5}, {70, 10}, {74, 10}, {194, 10}, {326, 10}, {586, 10}, {210, 10} };	// Table 5.5.3.3-2 3GPP 36.211 TDD
+
+    uint16_t srsPeriodicity, srsOffset;
+
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+	soundingRS_UL_ConfigCommon =
+	    &cc[CC_id].radioResourceConfigCommon->
+	    soundingRS_UL_ConfigCommon;
+	// check if SRS is enabled in this frame/subframe
+	if (soundingRS_UL_ConfigCommon) {
+	    srs_SubframeConfig =
+		soundingRS_UL_ConfigCommon->choice.setup.
+		srs_SubframeConfig;
+	    if (cc[CC_id].tdd_Config == NULL) {	// FDD
+		deltaTSFC = deltaTSFCTabType1[srs_SubframeConfig][0];
+		TSFC = deltaTSFCTabType1[srs_SubframeConfig][1];
+	    } else {		// TDD
+		deltaTSFC = deltaTSFCTabType2[srs_SubframeConfig][0];
+		TSFC = deltaTSFCTabType2[srs_SubframeConfig][1];
+	    }
+	    // Sounding reference signal subframes are the subframes satisfying ns/2 mod TSFC (- deltaTSFC
+	    uint16_t tmp = (subframeP % TSFC);
+
+	    if ((1 << tmp) & deltaTSFC) {
+		// This is an SRS subframe, loop over UEs
+		for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
+		    if (RC.mac[module_idP]->UE_list.active[UE_id] != TRUE)
+			continue;
+		    ul_req =
+			&RC.mac[module_idP]->UL_req[CC_id].
+			ul_config_request_body;
+
+
+		    // drop the allocation if the UE hasn't send RRCConnectionSetupComplete yet
+		    if (mac_eNB_get_rrc_status
+			(module_idP,
+			 UE_RNTI(module_idP, UE_id)) < RRC_CONNECTED)
+			continue;
+
+		    AssertFatal(UE_list->UE_template[CC_id]
+				[UE_id].physicalConfigDedicated != NULL,
+				"physicalConfigDedicated is null for UE %d\n",
+				UE_id);
+
+		    if ((soundingRS_UL_ConfigDedicated =
+			 UE_list->UE_template[CC_id]
+			 [UE_id].
+			 physicalConfigDedicated->soundingRS_UL_ConfigDedicated)
+			!= NULL) {
+			if (soundingRS_UL_ConfigDedicated->present ==
+			    SoundingRS_UL_ConfigDedicated_PR_setup) {
+			    get_srs_pos(&cc[CC_id],
+					soundingRS_UL_ConfigDedicated->choice.
+					setup.srs_ConfigIndex,
+					&srsPeriodicity, &srsOffset);
+			    if (((10 * frameP +
+				  subframeP) % srsPeriodicity) ==
+				srsOffset) {
+				// Prorgram SRS
+				ul_req->srs_present = 1;
+				nfapi_ul_config_request_pdu_t
+				    * ul_config_pdu =
+				    &ul_req->
+				    ul_config_pdu_list
+				    [ul_req->number_of_pdus];
+				memset((void *) ul_config_pdu, 0,
+				       sizeof
+				       (nfapi_ul_config_request_pdu_t));
+				ul_config_pdu->pdu_type =
+				    NFAPI_UL_CONFIG_SRS_PDU_TYPE;
+				ul_config_pdu->pdu_size =
+				    2 + (uint8_t) (2 +
+						   sizeof
+						   (nfapi_ul_config_srs_pdu));
+				ul_config_pdu->srs_pdu.srs_pdu_rel8.size =
+				    (uint8_t)
+				    sizeof(nfapi_ul_config_srs_pdu);;
+				ul_config_pdu->srs_pdu.srs_pdu_rel8.rnti =
+				    UE_list->UE_template[CC_id][UE_id].
+				    rnti;
+				ul_config_pdu->srs_pdu.
+				    srs_pdu_rel8.srs_bandwidth =
+				    soundingRS_UL_ConfigDedicated->
+				    choice.setup.srs_Bandwidth;
+				ul_config_pdu->srs_pdu.
+				    srs_pdu_rel8.frequency_domain_position
+				    =
+				    soundingRS_UL_ConfigDedicated->
+				    choice.setup.freqDomainPosition;
+				ul_config_pdu->srs_pdu.
+				    srs_pdu_rel8.srs_hopping_bandwidth =
+				    soundingRS_UL_ConfigDedicated->
+				    choice.setup.srs_HoppingBandwidth;;
+				ul_config_pdu->srs_pdu.
+				    srs_pdu_rel8.transmission_comb =
+				    soundingRS_UL_ConfigDedicated->
+				    choice.setup.transmissionComb;
+				ul_config_pdu->srs_pdu.srs_pdu_rel8.i_srs =
+				    soundingRS_UL_ConfigDedicated->
+				    choice.setup.srs_ConfigIndex;
+				ul_config_pdu->srs_pdu.
+				    srs_pdu_rel8.sounding_reference_cyclic_shift
+				    =
+				    soundingRS_UL_ConfigDedicated->
+				    choice.setup.cyclicShift;
+				//              ul_config_pdu->srs_pdu.srs_pdu_rel10.antenna_port                   = ;//
+				//              ul_config_pdu->srs_pdu.srs_pdu_rel13.number_of_combs                = ;//
+				RC.mac[module_idP]->UL_req[CC_id].sfn_sf =
+				    (frameP << 4) + subframeP;
+				ul_req->number_of_pdus++;
+			    }	// if (((10*frameP+subframeP) % srsPeriodicity) == srsOffset)
+			}	// if (soundingRS_UL_ConfigDedicated->present == SoundingRS_UL_ConfigDedicated_PR_setup)
+		    }		// if ((soundingRS_UL_ConfigDedicated = UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->soundingRS_UL_ConfigDedicated)!=NULL)
+		}		// for (UE_id ...
+	    }			// if((1<<tmp) & deltaTSFC)
+
+	}			// SRS config
+    }
 }
 
-void schedule_CSI(module_id_t module_idP,frame_t frameP,sub_frame_t subframeP)
+void
+schedule_CSI(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP)
 {
-  eNB_MAC_INST                   *eNB          = RC.mac[module_idP];
-  UE_list_t                      *UE_list      = &eNB->UE_list;
-  COMMON_channels_t              *cc;
-  nfapi_ul_config_request_body_t *ul_req;
-  int CC_id,UE_id;
-  struct CQI_ReportPeriodic *cqi_ReportPeriodic;
-  uint16_t Npd,N_OFFSET_CQI;
-  int H;
-
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-
-    cc = &eNB->common_channels[CC_id];
-    for (UE_id=0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
-      if (UE_list->active[UE_id] != TRUE) continue;
-
-      ul_req        = &RC.mac[module_idP]->UL_req[CC_id].ul_config_request_body;
-
-      // drop the allocation if the UE hasn't send RRCConnectionSetupComplete yet
-      if (mac_eNB_get_rrc_status(module_idP,UE_RNTI(module_idP,UE_id)) < RRC_CONNECTED) continue;
-
-      AssertFatal(UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated != NULL, "physicalConfigDedicated is null for UE %d\n",UE_id);
-
-      if (UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->cqi_ReportConfig) {
-	if ((cqi_ReportPeriodic = UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->cqi_ReportConfig->cqi_ReportPeriodic)!=NULL &&
-	    (cqi_ReportPeriodic->present!=CQI_ReportPeriodic_PR_release)) {
-	  //Rel8 Periodic CQI/PMI/RI reporting
-
-	  get_csi_params(cc,cqi_ReportPeriodic,&Npd,&N_OFFSET_CQI,&H);
-
-	  if ((((frameP*10)+subframeP)%Npd) == N_OFFSET_CQI) {  // CQI opportunity
-	    UE_list->UE_sched_ctrl[UE_id].feedback_cnt[CC_id]=(((frameP*10)+subframeP)/Npd)%H;
-	    // Program CQI
-	    nfapi_ul_config_request_pdu_t* ul_config_pdu                                     = &ul_req->ul_config_pdu_list[ul_req->number_of_pdus];
-	    memset((void*)ul_config_pdu,0,sizeof(nfapi_ul_config_request_pdu_t));
-	    ul_config_pdu->pdu_type                                                          = NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE;
-	    ul_config_pdu->pdu_size                                                          = 2+(uint8_t)(2+sizeof(nfapi_ul_config_uci_cqi_pdu));
-	    ul_config_pdu->uci_cqi_pdu.ue_information.ue_information_rel8.rnti               = UE_list->UE_template[CC_id][UE_id].rnti;
-	    ul_config_pdu->uci_cqi_pdu.cqi_information.cqi_information_rel8.pucch_index      = cqi_ReportPeriodic->choice.setup.cqi_PUCCH_ResourceIndex;
-	    ul_config_pdu->uci_cqi_pdu.cqi_information.cqi_information_rel8.dl_cqi_pmi_size  = get_rel8_dl_cqi_pmi_size(&UE_list->UE_sched_ctrl[UE_id],
-															CC_id,
-															cc,
-															get_tmode(module_idP,CC_id,UE_id),
-															cqi_ReportPeriodic);
-	    ul_req->number_of_pdus++;
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    UE_list_t *UE_list = &eNB->UE_list;
+    COMMON_channels_t *cc;
+    nfapi_ul_config_request_body_t *ul_req;
+    int CC_id, UE_id;
+    struct CQI_ReportPeriodic *cqi_ReportPeriodic;
+    uint16_t Npd, N_OFFSET_CQI;
+    int H;
+
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+
+	cc = &eNB->common_channels[CC_id];
+	for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
+	    if (UE_list->active[UE_id] != TRUE)
+		continue;
+
+	    ul_req =
+		&RC.mac[module_idP]->UL_req[CC_id].ul_config_request_body;
+
+	    // drop the allocation if the UE hasn't send RRCConnectionSetupComplete yet
+	    if (mac_eNB_get_rrc_status
+		(module_idP, UE_RNTI(module_idP, UE_id)) < RRC_CONNECTED)
+		continue;
+
+	    AssertFatal(UE_list->
+			UE_template[CC_id][UE_id].physicalConfigDedicated
+			!= NULL,
+			"physicalConfigDedicated is null for UE %d\n",
+			UE_id);
+
+	    if (UE_list->
+		UE_template[CC_id][UE_id].physicalConfigDedicated->
+		cqi_ReportConfig) {
+		if ((cqi_ReportPeriodic =
+		     UE_list->
+		     UE_template[CC_id][UE_id].physicalConfigDedicated->
+		     cqi_ReportConfig->cqi_ReportPeriodic) != NULL
+		    && (cqi_ReportPeriodic->present !=
+			CQI_ReportPeriodic_PR_release)) {
+		    //Rel8 Periodic CQI/PMI/RI reporting
+
+		    get_csi_params(cc, cqi_ReportPeriodic, &Npd,
+				   &N_OFFSET_CQI, &H);
+
+		    if ((((frameP * 10) + subframeP) % Npd) == N_OFFSET_CQI) {	// CQI opportunity
+			UE_list->UE_sched_ctrl[UE_id].feedback_cnt[CC_id] =
+			    (((frameP * 10) + subframeP) / Npd) % H;
+			// Program CQI
+			nfapi_ul_config_request_pdu_t *ul_config_pdu =
+			    &ul_req->ul_config_pdu_list[ul_req->
+							number_of_pdus];
+			memset((void *) ul_config_pdu, 0,
+			       sizeof(nfapi_ul_config_request_pdu_t));
+			ul_config_pdu->pdu_type =
+			    NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE;
+			ul_config_pdu->pdu_size =
+			    2 + (uint8_t) (2 +
+					   sizeof
+					   (nfapi_ul_config_uci_cqi_pdu));
+			ul_config_pdu->uci_cqi_pdu.
+			    ue_information.ue_information_rel8.rnti =
+			    UE_list->UE_template[CC_id][UE_id].rnti;
+			ul_config_pdu->uci_cqi_pdu.
+			    cqi_information.cqi_information_rel8.
+			    pucch_index =
+			    cqi_ReportPeriodic->choice.
+			    setup.cqi_PUCCH_ResourceIndex;
+			ul_config_pdu->uci_cqi_pdu.
+			    cqi_information.cqi_information_rel8.
+			    dl_cqi_pmi_size =
+			    get_rel8_dl_cqi_pmi_size
+			    (&UE_list->UE_sched_ctrl[UE_id], CC_id, cc,
+			     get_tmode(module_idP, CC_id, UE_id),
+			     cqi_ReportPeriodic);
+			ul_req->number_of_pdus++;
 
 #if defined(Rel10) || defined(Rel14)
-	    // PUT rel10-13 UCI options here
+			// PUT rel10-13 UCI options here
 #endif
-	  }
-	  else if ((cqi_ReportPeriodic->choice.setup.ri_ConfigIndex) &&
-		   ((((frameP*10)+subframeP)%((H*Npd)<<(*cqi_ReportPeriodic->choice.setup.ri_ConfigIndex/161)))==
-		    N_OFFSET_CQI + (*cqi_ReportPeriodic->choice.setup.ri_ConfigIndex%161))) {  // RI opportunity
-	    // Program RI
-	    nfapi_ul_config_request_pdu_t* ul_config_pdu                                     = &ul_req->ul_config_pdu_list[ul_req->number_of_pdus];
-	    memset((void*)ul_config_pdu,0,sizeof(nfapi_ul_config_request_pdu_t));
-	    ul_config_pdu->pdu_type                                                          = NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE;
-	    ul_config_pdu->pdu_size                                                          = 2+(uint8_t)(2+sizeof(nfapi_ul_config_uci_cqi_pdu));
-	    ul_config_pdu->uci_cqi_pdu.ue_information.ue_information_rel8.rnti               = UE_list->UE_template[CC_id][UE_id].rnti;
-	    ul_config_pdu->uci_cqi_pdu.cqi_information.cqi_information_rel8.pucch_index      = cqi_ReportPeriodic->choice.setup.cqi_PUCCH_ResourceIndex;
-	    ul_config_pdu->uci_cqi_pdu.cqi_information.cqi_information_rel8.dl_cqi_pmi_size  = (cc->p_eNB==2)?1:2;
-	    RC.mac[module_idP]->UL_req[CC_id].sfn_sf = (frameP<<4)+subframeP;
-	    ul_req->number_of_pdus++;
-	  }
-
-	} // if ((cqi_ReportPeriodic = cqi_ReportConfig->cqi_ReportPeriodic)!=NULL) {
-      } // if (UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->cqi_ReportConfig)
-    } // for (UE_id=UE_list->head; UE_id>=0; UE_id=UE_list->next[UE_id]) {
-  } // for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
+		    } else
+			if ((cqi_ReportPeriodic->choice.setup.
+			     ri_ConfigIndex)
+			    && ((((frameP * 10) + subframeP) % ((H * Npd) << (*cqi_ReportPeriodic->choice.setup.ri_ConfigIndex / 161))) == N_OFFSET_CQI + (*cqi_ReportPeriodic->choice.setup.ri_ConfigIndex % 161))) {	// RI opportunity
+			// Program RI
+			nfapi_ul_config_request_pdu_t *ul_config_pdu =
+			    &ul_req->ul_config_pdu_list[ul_req->
+							number_of_pdus];
+			memset((void *) ul_config_pdu, 0,
+			       sizeof(nfapi_ul_config_request_pdu_t));
+			ul_config_pdu->pdu_type =
+			    NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE;
+			ul_config_pdu->pdu_size =
+			    2 + (uint8_t) (2 +
+					   sizeof
+					   (nfapi_ul_config_uci_cqi_pdu));
+			ul_config_pdu->uci_cqi_pdu.
+			    ue_information.ue_information_rel8.rnti =
+			    UE_list->UE_template[CC_id][UE_id].rnti;
+			ul_config_pdu->uci_cqi_pdu.
+			    cqi_information.cqi_information_rel8.
+			    pucch_index =
+			    cqi_ReportPeriodic->choice.
+			    setup.cqi_PUCCH_ResourceIndex;
+			ul_config_pdu->uci_cqi_pdu.
+			    cqi_information.cqi_information_rel8.
+			    dl_cqi_pmi_size = (cc->p_eNB == 2) ? 1 : 2;
+			RC.mac[module_idP]->UL_req[CC_id].sfn_sf =
+			    (frameP << 4) + subframeP;
+			ul_req->number_of_pdus++;
+		    }
+
+		}		// if ((cqi_ReportPeriodic = cqi_ReportConfig->cqi_ReportPeriodic)!=NULL) {
+	    }			// if (UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->cqi_ReportConfig)
+	}			// for (UE_id=UE_list->head; UE_id>=0; UE_id=UE_list->next[UE_id]) {
+    }				// for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
 }
 
-void schedule_SR(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP)
+void
+schedule_SR(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP)
 {
-  eNB_MAC_INST                   *eNB        = RC.mac[module_idP];
-  UE_list_t                      *UE_list    = &eNB->UE_list;
-  nfapi_ul_config_request_body_t *ul_req;
-  int                             CC_id;
-  int                             UE_id;
-  SchedulingRequestConfig_t      *SRconfig;
-  int                             skip_ue;
-  int                             is_harq;
-  nfapi_ul_config_sr_information  sr;
-  int                             i;
-
-  for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
-    RC.mac[module_idP]->UL_req[CC_id].sfn_sf = (frameP << 4) + subframeP;
-
-    for (UE_id = 0; UE_id<NUMBER_OF_UE_MAX; UE_id++) {
-      if (RC.mac[module_idP]->UE_list.active[UE_id]!=TRUE) continue;
-
-      ul_req        = &RC.mac[module_idP]->UL_req[CC_id].ul_config_request_body;
-
-      AssertFatal(UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated != NULL, "physicalConfigDedicated is null for UE %d\n",UE_id);
-
-      // drop the allocation if the UE hasn't send RRCConnectionSetupComplete yet
-      if (mac_eNB_get_rrc_status(module_idP,UE_RNTI(module_idP,UE_id)) < RRC_CONNECTED) continue;
-
-      if ((SRconfig = UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->schedulingRequestConfig)!=NULL) {
-        if (SRconfig->present == SchedulingRequestConfig_PR_setup) {
-          if (SRconfig->choice.setup.sr_ConfigIndex <= 4) {        // 5 ms SR period
-            if ((subframeP%5) != SRconfig->choice.setup.sr_ConfigIndex)
-              continue;
-          } else if (SRconfig->choice.setup.sr_ConfigIndex <= 14) { // 10 ms SR period
-            if (subframeP!=(SRconfig->choice.setup.sr_ConfigIndex-5))
-              continue;
-          } else if (SRconfig->choice.setup.sr_ConfigIndex <= 34) { // 20 ms SR period
-            if ((10*(frameP&1)+subframeP) != (SRconfig->choice.setup.sr_ConfigIndex-15))
-              continue;
-          } else if (SRconfig->choice.setup.sr_ConfigIndex <= 74) { // 40 ms SR period
-            if ((10*(frameP&3)+subframeP) != (SRconfig->choice.setup.sr_ConfigIndex-35))
-              continue;
-          } else if (SRconfig->choice.setup.sr_ConfigIndex <= 154) { // 80 ms SR period
-            if ((10*(frameP&7)+subframeP) != (SRconfig->choice.setup.sr_ConfigIndex-75))
-              continue;
-          }
-        } // SRconfig->present == SchedulingRequestConfig_PR_setup)
-      } // SRconfig = UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->schedulingRequestConfig)!=NULL)
-
-      // if we get here there is some PUCCH1 reception to schedule for SR
-
-      skip_ue=0;
-      is_harq = 0;
-      // check that there is no existing UL grant for ULSCH which overrides the SR
-      for (i = 0; i < ul_req->number_of_pdus; i++) {
-        if (((ul_req->ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_PDU_TYPE)||
-             (ul_req->ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE)||
-             (ul_req->ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE)||
-             (ul_req->ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE))&&
-            (ul_req->ul_config_pdu_list[i].ulsch_pdu.ulsch_pdu_rel8.rnti == UE_list->UE_template[CC_id][UE_id].rnti)) {
-            skip_ue=1;
-            break;
-        }
-        /* if there is already an HARQ pdu, convert to SR_HARQ */
-        else if ((ul_req->ul_config_pdu_list[i].pdu_type == NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE)&&
-                 (ul_req->ul_config_pdu_list[i].uci_harq_pdu.ue_information.ue_information_rel8.rnti == UE_list->UE_template[CC_id][UE_id].rnti)) {
-          is_harq = 1;
-          break;
-        }
-      }
-
-      // drop the allocation because ULSCH with handle it with BSR
-      if (skip_ue==1) continue;
-
-      LOG_D(MAC,"Frame %d, Subframe %d : Scheduling SR for UE %d/%x\n",frameP,subframeP,UE_id,UE_list->UE_template[CC_id][UE_id].rnti);
-
-      // check Rel10 or Rel8 SR
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    UE_list_t *UE_list = &eNB->UE_list;
+    nfapi_ul_config_request_body_t *ul_req;
+    int CC_id;
+    int UE_id;
+    SchedulingRequestConfig_t *SRconfig;
+    int skip_ue;
+    int is_harq;
+    nfapi_ul_config_sr_information sr;
+    int i;
+
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+	RC.mac[module_idP]->UL_req[CC_id].sfn_sf =
+	    (frameP << 4) + subframeP;
+
+	for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
+	    if (RC.mac[module_idP]->UE_list.active[UE_id] != TRUE)
+		continue;
+
+	    ul_req =
+		&RC.mac[module_idP]->UL_req[CC_id].ul_config_request_body;
+
+	    AssertFatal(UE_list->
+			UE_template[CC_id][UE_id].physicalConfigDedicated
+			!= NULL,
+			"physicalConfigDedicated is null for UE %d\n",
+			UE_id);
+
+	    // drop the allocation if the UE hasn't send RRCConnectionSetupComplete yet
+	    if (mac_eNB_get_rrc_status
+		(module_idP, UE_RNTI(module_idP, UE_id)) < RRC_CONNECTED)
+		continue;
+
+	    if ((SRconfig =
+		 UE_list->
+		 UE_template[CC_id][UE_id].physicalConfigDedicated->
+		 schedulingRequestConfig) != NULL) {
+		if (SRconfig->present == SchedulingRequestConfig_PR_setup) {
+		    if (SRconfig->choice.setup.sr_ConfigIndex <= 4) {	// 5 ms SR period
+			if ((subframeP % 5) !=
+			    SRconfig->choice.setup.sr_ConfigIndex)
+			    continue;
+		    } else if (SRconfig->choice.setup.sr_ConfigIndex <= 14) {	// 10 ms SR period
+			if (subframeP !=
+			    (SRconfig->choice.setup.sr_ConfigIndex - 5))
+			    continue;
+		    } else if (SRconfig->choice.setup.sr_ConfigIndex <= 34) {	// 20 ms SR period
+			if ((10 * (frameP & 1) + subframeP) !=
+			    (SRconfig->choice.setup.sr_ConfigIndex - 15))
+			    continue;
+		    } else if (SRconfig->choice.setup.sr_ConfigIndex <= 74) {	// 40 ms SR period
+			if ((10 * (frameP & 3) + subframeP) !=
+			    (SRconfig->choice.setup.sr_ConfigIndex - 35))
+			    continue;
+		    } else if (SRconfig->choice.setup.sr_ConfigIndex <= 154) {	// 80 ms SR period
+			if ((10 * (frameP & 7) + subframeP) !=
+			    (SRconfig->choice.setup.sr_ConfigIndex - 75))
+			    continue;
+		    }
+		}		// SRconfig->present == SchedulingRequestConfig_PR_setup)
+	    }			// SRconfig = UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->schedulingRequestConfig)!=NULL)
+
+	    // if we get here there is some PUCCH1 reception to schedule for SR
+
+	    skip_ue = 0;
+	    is_harq = 0;
+	    // check that there is no existing UL grant for ULSCH which overrides the SR
+	    for (i = 0; i < ul_req->number_of_pdus; i++) {
+		if (((ul_req->ul_config_pdu_list[i].pdu_type ==
+		      NFAPI_UL_CONFIG_ULSCH_PDU_TYPE)
+		     || (ul_req->ul_config_pdu_list[i].pdu_type ==
+			 NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE)
+		     || (ul_req->ul_config_pdu_list[i].pdu_type ==
+			 NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE)
+		     || (ul_req->ul_config_pdu_list[i].pdu_type ==
+			 NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE))
+		    && (ul_req->ul_config_pdu_list[i].
+			ulsch_pdu.ulsch_pdu_rel8.rnti ==
+			UE_list->UE_template[CC_id][UE_id].rnti)) {
+		    skip_ue = 1;
+		    break;
+		}
+		/* if there is already an HARQ pdu, convert to SR_HARQ */
+		else if ((ul_req->ul_config_pdu_list[i].pdu_type ==
+			  NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE)
+			 && (ul_req->ul_config_pdu_list[i].
+			     uci_harq_pdu.ue_information.
+			     ue_information_rel8.rnti ==
+			     UE_list->UE_template[CC_id][UE_id].rnti)) {
+		    is_harq = 1;
+		    break;
+		}
+	    }
+
+	    // drop the allocation because ULSCH with handle it with BSR
+	    if (skip_ue == 1)
+		continue;
+
+	    LOG_D(MAC,
+		  "Frame %d, Subframe %d : Scheduling SR for UE %d/%x\n",
+		  frameP, subframeP, UE_id,
+		  UE_list->UE_template[CC_id][UE_id].rnti);
+
+	    // check Rel10 or Rel8 SR
 #if defined(Rel10) || defined(Rel14)
-      if ((UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->ext2) &&
-          (UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->ext2->schedulingRequestConfig_v1020)&&
-          (UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->ext2->schedulingRequestConfig_v1020)) {
-        sr.sr_information_rel10.number_of_pucch_resources = 1;
-        sr.sr_information_rel10.pucch_index_p1 = *UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->ext2->schedulingRequestConfig_v1020->sr_PUCCH_ResourceIndexP1_r10;
-      } else
+	    if ((UE_list->
+		 UE_template[CC_id][UE_id].physicalConfigDedicated->ext2)
+		&& (UE_list->
+		    UE_template[CC_id][UE_id].physicalConfigDedicated->
+		    ext2->schedulingRequestConfig_v1020)
+		&& (UE_list->
+		    UE_template[CC_id][UE_id].physicalConfigDedicated->
+		    ext2->schedulingRequestConfig_v1020)) {
+		sr.sr_information_rel10.number_of_pucch_resources = 1;
+		sr.sr_information_rel10.pucch_index_p1 =
+		    *UE_list->
+		    UE_template[CC_id][UE_id].physicalConfigDedicated->
+		    ext2->
+		    schedulingRequestConfig_v1020->sr_PUCCH_ResourceIndexP1_r10;
+	    } else
 #endif
-      {
-        sr.sr_information_rel8.pucch_index = UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->schedulingRequestConfig->choice.setup.sr_PUCCH_ResourceIndex;
-      }
-
-      /* if there is already an HARQ pdu, convert to SR_HARQ */
-      if (is_harq) {
-        nfapi_ul_config_harq_information h = ul_req->ul_config_pdu_list[i].uci_harq_pdu.harq_information;
-        ul_req->ul_config_pdu_list[i].pdu_type                         = NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE;
-        ul_req->ul_config_pdu_list[i].uci_sr_harq_pdu.sr_information   = sr;
-        ul_req->ul_config_pdu_list[i].uci_sr_harq_pdu.harq_information = h;
-      } else {
-        ul_req->ul_config_pdu_list[ul_req->number_of_pdus].pdu_type                                           = NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE;
-        ul_req->ul_config_pdu_list[ul_req->number_of_pdus].uci_sr_pdu.ue_information.ue_information_rel8.rnti = UE_list->UE_template[CC_id][UE_id].rnti;
-        ul_req->ul_config_pdu_list[ul_req->number_of_pdus].uci_sr_pdu.sr_information                          = sr;
-        ul_req->number_of_pdus++;
-      } /* if (is_harq) */
-    } // for (UE_id=UE_list->head; UE_id>=0; UE_id=UE_list->next[UE_id])
-  } // for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++)
+	    {
+		sr.sr_information_rel8.pucch_index =
+		    UE_list->
+		    UE_template[CC_id][UE_id].physicalConfigDedicated->
+		    schedulingRequestConfig->choice.setup.
+		    sr_PUCCH_ResourceIndex;
+	    }
+
+	    /* if there is already an HARQ pdu, convert to SR_HARQ */
+	    if (is_harq) {
+		nfapi_ul_config_harq_information h =
+		    ul_req->ul_config_pdu_list[i].uci_harq_pdu.
+		    harq_information;
+		ul_req->ul_config_pdu_list[i].pdu_type =
+		    NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE;
+		ul_req->ul_config_pdu_list[i].uci_sr_harq_pdu.
+		    sr_information = sr;
+		ul_req->ul_config_pdu_list[i].uci_sr_harq_pdu.
+		    harq_information = h;
+	    } else {
+		ul_req->ul_config_pdu_list[ul_req->number_of_pdus].
+		    pdu_type = NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE;
+		ul_req->ul_config_pdu_list[ul_req->
+					   number_of_pdus].uci_sr_pdu.
+		    ue_information.ue_information_rel8.rnti =
+		    UE_list->UE_template[CC_id][UE_id].rnti;
+		ul_req->ul_config_pdu_list[ul_req->
+					   number_of_pdus].uci_sr_pdu.
+		    sr_information = sr;
+		ul_req->number_of_pdus++;
+	    }			/* if (is_harq) */
+	}			// for (UE_id=UE_list->head; UE_id>=0; UE_id=UE_list->next[UE_id])
+    }				// for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++)
 }
 
-void check_ul_failure(module_id_t module_idP,int CC_id,int UE_id,
-		      frame_t frameP, sub_frame_t subframeP)
+void
+check_ul_failure(module_id_t module_idP, int CC_id, int UE_id,
+		 frame_t frameP, sub_frame_t subframeP)
 {
-  UE_list_t                           *UE_list      = &RC.mac[module_idP]->UE_list;
-  nfapi_dl_config_request_t           *DL_req       = &RC.mac[module_idP]->DL_req[0];
-  uint16_t                            rnti          = UE_RNTI(module_idP,UE_id);
-  COMMON_channels_t                   *cc           = RC.mac[module_idP]->common_channels;
-
-  // check uplink failure
-  if ((UE_list->UE_sched_ctrl[UE_id].ul_failure_timer>0)&&
-      (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync==0)) {
-    LOG_I(MAC,"UE %d rnti %x: UL Failure timer %d \n",UE_id,rnti,UE_list->UE_sched_ctrl[UE_id].ul_failure_timer);
-    if (UE_list->UE_sched_ctrl[UE_id].ra_pdcch_order_sent==0) {
-      UE_list->UE_sched_ctrl[UE_id].ra_pdcch_order_sent=1;
-
-      // add a format 1A dci for this UE to request an RA procedure (only one UE per subframe)
-      nfapi_dl_config_request_pdu_t* dl_config_pdu                    = &DL_req[CC_id].dl_config_request_body.dl_config_pdu_list[DL_req[CC_id].dl_config_request_body.number_pdu];
-      memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-      dl_config_pdu->pdu_type                                         = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
-      dl_config_pdu->pdu_size                                         = (uint8_t)(2+sizeof(nfapi_dl_config_dci_dl_pdu));
-      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format            = NFAPI_DL_DCI_FORMAT_1A;
-      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level     = get_aggregation(get_bw_index(module_idP,CC_id),UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id],format1A);
-      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti                  = rnti;
-      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type             = 1;    // CRNTI : see Table 4-10 from SCF082 - nFAPI specifications
-      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power    = 6000; // equal to RS power
-
-      AssertFatal((cc[CC_id].mib->message.dl_Bandwidth >=0) && (cc[CC_id].mib->message.dl_Bandwidth<6),
-		  "illegal dl_Bandwidth %d\n",(int)cc[CC_id].mib->message.dl_Bandwidth);
-      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_block_coding = pdcch_order_table[cc[CC_id].mib->message.dl_Bandwidth];
-      DL_req[CC_id].dl_config_request_body.number_dci++;
-      DL_req[CC_id].dl_config_request_body.number_pdu++;
-      LOG_I(MAC,"UE %d rnti %x: sending PDCCH order for RAPROC (failure timer %d), resource_block_coding %d \n",UE_id,rnti,UE_list->UE_sched_ctrl[UE_id].ul_failure_timer,dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_block_coding);
-    }
-    else { // ra_pdcch_sent==1
-      LOG_I(MAC,"UE %d rnti %x: sent PDCCH order for RAPROC waiting (failure timer %d) \n",UE_id,rnti,UE_list->UE_sched_ctrl[UE_id].ul_failure_timer);
-      if ((UE_list->UE_sched_ctrl[UE_id].ul_failure_timer % 40) == 0)
-	UE_list->UE_sched_ctrl[UE_id].ra_pdcch_order_sent=0; // resend every 4 frames
-    }
-
-    UE_list->UE_sched_ctrl[UE_id].ul_failure_timer++;
-    // check threshold
-    if (UE_list->UE_sched_ctrl[UE_id].ul_failure_timer > 200) {
-      // inform RRC of failure and clear timer
-      LOG_I(MAC,"UE %d rnti %x: UL Failure after repeated PDCCH orders: Triggering RRC \n",UE_id,rnti);
-      mac_eNB_rrc_ul_failure(module_idP,CC_id,frameP,subframeP,rnti);
-      UE_list->UE_sched_ctrl[UE_id].ul_failure_timer=0;
-      UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync=1;
-    }
-  } // ul_failure_timer>0
+    UE_list_t *UE_list = &RC.mac[module_idP]->UE_list;
+    nfapi_dl_config_request_t *DL_req = &RC.mac[module_idP]->DL_req[0];
+    uint16_t rnti = UE_RNTI(module_idP, UE_id);
+    COMMON_channels_t *cc = RC.mac[module_idP]->common_channels;
+
+    // check uplink failure
+    if ((UE_list->UE_sched_ctrl[UE_id].ul_failure_timer > 0) &&
+	(UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 0)) {
+	LOG_I(MAC, "UE %d rnti %x: UL Failure timer %d \n", UE_id, rnti,
+	      UE_list->UE_sched_ctrl[UE_id].ul_failure_timer);
+	if (UE_list->UE_sched_ctrl[UE_id].ra_pdcch_order_sent == 0) {
+	    UE_list->UE_sched_ctrl[UE_id].ra_pdcch_order_sent = 1;
+
+	    // add a format 1A dci for this UE to request an RA procedure (only one UE per subframe)
+	    nfapi_dl_config_request_pdu_t *dl_config_pdu =
+		&DL_req[CC_id].
+		dl_config_request_body.dl_config_pdu_list[DL_req[CC_id].
+							  dl_config_request_body.number_pdu];
+	    memset((void *) dl_config_pdu, 0,
+		   sizeof(nfapi_dl_config_request_pdu_t));
+	    dl_config_pdu->pdu_type = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
+	    dl_config_pdu->pdu_size =
+		(uint8_t) (2 + sizeof(nfapi_dl_config_dci_dl_pdu));
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format =
+		NFAPI_DL_DCI_FORMAT_1A;
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level =
+		get_aggregation(get_bw_index(module_idP, CC_id),
+				UE_list->UE_sched_ctrl[UE_id].
+				dl_cqi[CC_id], format1A);
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti = rnti;
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type = 1;	// CRNTI : see Table 4-10 from SCF082 - nFAPI specifications
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power = 6000;	// equal to RS power
+
+	    AssertFatal((cc[CC_id].mib->message.dl_Bandwidth >= 0)
+			&& (cc[CC_id].mib->message.dl_Bandwidth < 6),
+			"illegal dl_Bandwidth %d\n",
+			(int) cc[CC_id].mib->message.dl_Bandwidth);
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+		resource_block_coding =
+		pdcch_order_table[cc[CC_id].mib->message.dl_Bandwidth];
+	    DL_req[CC_id].dl_config_request_body.number_dci++;
+	    DL_req[CC_id].dl_config_request_body.number_pdu++;
+	    LOG_I(MAC,
+		  "UE %d rnti %x: sending PDCCH order for RAPROC (failure timer %d), resource_block_coding %d \n",
+		  UE_id, rnti,
+		  UE_list->UE_sched_ctrl[UE_id].ul_failure_timer,
+		  dl_config_pdu->dci_dl_pdu.
+		  dci_dl_pdu_rel8.resource_block_coding);
+	} else {		// ra_pdcch_sent==1
+	    LOG_I(MAC,
+		  "UE %d rnti %x: sent PDCCH order for RAPROC waiting (failure timer %d) \n",
+		  UE_id, rnti,
+		  UE_list->UE_sched_ctrl[UE_id].ul_failure_timer);
+	    if ((UE_list->UE_sched_ctrl[UE_id].ul_failure_timer % 40) == 0)
+		UE_list->UE_sched_ctrl[UE_id].ra_pdcch_order_sent = 0;	// resend every 4 frames
+	}
+
+	UE_list->UE_sched_ctrl[UE_id].ul_failure_timer++;
+	// check threshold
+	if (UE_list->UE_sched_ctrl[UE_id].ul_failure_timer > 200) {
+	    // inform RRC of failure and clear timer
+	    LOG_I(MAC,
+		  "UE %d rnti %x: UL Failure after repeated PDCCH orders: Triggering RRC \n",
+		  UE_id, rnti);
+	    mac_eNB_rrc_ul_failure(module_idP, CC_id, frameP, subframeP,
+				   rnti);
+	    UE_list->UE_sched_ctrl[UE_id].ul_failure_timer = 0;
+	    UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync = 1;
+	}
+    }				// ul_failure_timer>0
 
 }
 
-void clear_nfapi_information(eNB_MAC_INST *eNB,int CC_idP,frame_t frameP,sub_frame_t subframeP)
+void
+clear_nfapi_information(eNB_MAC_INST * eNB, int CC_idP,
+			frame_t frameP, sub_frame_t subframeP)
 {
-  nfapi_dl_config_request_t *DL_req         = &eNB->DL_req[0];
-  nfapi_ul_config_request_t *UL_req         = &eNB->UL_req[0];
-  nfapi_hi_dci0_request_t   *HI_DCI0_req    = &eNB->HI_DCI0_req[0];
-  nfapi_tx_request_t        *TX_req         = &eNB->TX_req[0];
+    nfapi_dl_config_request_t *DL_req = &eNB->DL_req[0];
+    nfapi_ul_config_request_t *UL_req = &eNB->UL_req[0];
+    nfapi_hi_dci0_request_t *HI_DCI0_req = &eNB->HI_DCI0_req[0];
+    nfapi_tx_request_t *TX_req = &eNB->TX_req[0];
 
 
-  eNB->pdu_index[CC_idP]                                     = 0;
-  DL_req[CC_idP].dl_config_request_body.number_pdcch_ofdm_symbols           = 1;
-  DL_req[CC_idP].dl_config_request_body.number_dci                          = 0;
-  DL_req[CC_idP].dl_config_request_body.number_pdu                          = 0;
-  DL_req[CC_idP].dl_config_request_body.number_pdsch_rnti                   = 0;
-  DL_req[CC_idP].dl_config_request_body.transmission_power_pcfich           = 6000;
+    eNB->pdu_index[CC_idP] = 0;
+    DL_req[CC_idP].dl_config_request_body.number_pdcch_ofdm_symbols = 1;
+    DL_req[CC_idP].dl_config_request_body.number_dci = 0;
+    DL_req[CC_idP].dl_config_request_body.number_pdu = 0;
+    DL_req[CC_idP].dl_config_request_body.number_pdsch_rnti = 0;
+    DL_req[CC_idP].dl_config_request_body.transmission_power_pcfich = 6000;
 
-  HI_DCI0_req[CC_idP].hi_dci0_request_body.sfnsf                            = subframeP + (frameP<<4);
-  HI_DCI0_req[CC_idP].hi_dci0_request_body.number_of_dci                    = 0;
+    HI_DCI0_req[CC_idP].hi_dci0_request_body.sfnsf =
+	subframeP + (frameP << 4);
+    HI_DCI0_req[CC_idP].hi_dci0_request_body.number_of_dci = 0;
 
 
-  UL_req[CC_idP].ul_config_request_body.number_of_pdus                      = 0;
-  UL_req[CC_idP].ul_config_request_body.rach_prach_frequency_resources      = 0; // ignored, handled by PHY for now
-  UL_req[CC_idP].ul_config_request_body.srs_present                         = 0; // ignored, handled by PHY for now
+    UL_req[CC_idP].ul_config_request_body.number_of_pdus = 0;
+    UL_req[CC_idP].ul_config_request_body.rach_prach_frequency_resources = 0;	// ignored, handled by PHY for now
+    UL_req[CC_idP].ul_config_request_body.srs_present = 0;	// ignored, handled by PHY for now
 
 
-  TX_req[CC_idP].tx_request_body.number_of_pdus                 = 0;
+    TX_req[CC_idP].tx_request_body.number_of_pdus = 0;
 
 
 }
 
-void copy_ulreq(module_id_t module_idP,frame_t frameP, sub_frame_t subframeP)
+void
+copy_ulreq(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP)
 {
-  int CC_id;
-  eNB_MAC_INST *eNB;
-  nfapi_ul_config_request_body_t *ul_req_tmp;
-  nfapi_ul_config_request_body_t *ul_req;
+    int CC_id;
+    eNB_MAC_INST *eNB;
+    nfapi_ul_config_request_body_t *ul_req_tmp;
+    nfapi_ul_config_request_body_t *ul_req;
 
-  eNB = RC.mac[module_idP];
+    eNB = RC.mac[module_idP];
 
-  for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
 
-    ul_req_tmp       = &eNB->UL_req_tmp[CC_id][subframeP].ul_config_request_body;
-    ul_req           = &eNB->UL_req[CC_id].ul_config_request_body;
+	ul_req_tmp =
+	    &eNB->UL_req_tmp[CC_id][subframeP].ul_config_request_body;
+	ul_req = &eNB->UL_req[CC_id].ul_config_request_body;
 
-    eNB->UL_req[CC_id].sfn_sf   = (frameP<<4) + subframeP;
-    ul_req->number_of_pdus                     = ul_req_tmp->number_of_pdus;
-    ul_req_tmp->number_of_pdus = 0;
+	eNB->UL_req[CC_id].sfn_sf = (frameP << 4) + subframeP;
+	ul_req->number_of_pdus = ul_req_tmp->number_of_pdus;
+	ul_req_tmp->number_of_pdus = 0;
 
-    memcpy((void*)ul_req->ul_config_pdu_list,
-	   (void*)ul_req_tmp->ul_config_pdu_list,
-	   ul_req->number_of_pdus*sizeof(nfapi_ul_config_request_pdu_t));
+	memcpy((void *) ul_req->ul_config_pdu_list,
+	       (void *) ul_req_tmp->ul_config_pdu_list,
+	       ul_req->number_of_pdus *
+	       sizeof(nfapi_ul_config_request_pdu_t));
 
-  }
+    }
 }
 
-void eNB_dlsch_ulsch_scheduler(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP)
+void
+eNB_dlsch_ulsch_scheduler(module_id_t module_idP, frame_t frameP,
+			  sub_frame_t subframeP)
 {
 
-  int mbsfn_status[MAX_NUM_CCs];
-  protocol_ctxt_t   ctxt;
+    int mbsfn_status[MAX_NUM_CCs];
+    protocol_ctxt_t ctxt;
 
-  int CC_id,i; //,next_i;
-  UE_list_t *UE_list=&RC.mac[module_idP]->UE_list;
-  rnti_t rnti;
+    int CC_id, i;		//,next_i;
+    UE_list_t *UE_list = &RC.mac[module_idP]->UE_list;
+    rnti_t rnti;
 
-  COMMON_channels_t         *cc             = RC.mac[module_idP]->common_channels;
+    COMMON_channels_t *cc = RC.mac[module_idP]->common_channels;
 
 #if defined(FLEXRAN_AGENT_SB_IF)
-  Protocol__FlexranMessage *msg;
+    Protocol__FlexranMessage *msg;
 #endif
 
 
-  start_meas(&RC.mac[module_idP]->eNB_scheduler);
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ULSCH_SCHEDULER,VCD_FUNCTION_IN);
+    start_meas(&RC.mac[module_idP]->eNB_scheduler);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ULSCH_SCHEDULER,
+	 VCD_FUNCTION_IN);
 
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-    mbsfn_status[CC_id]=0;
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+	mbsfn_status[CC_id] = 0;
 
-    // clear vrb_maps
-    memset(cc[CC_id].vrb_map,0,100);
-    memset(cc[CC_id].vrb_map_UL,0,100);
+	// clear vrb_maps
+	memset(cc[CC_id].vrb_map, 0, 100);
+	memset(cc[CC_id].vrb_map_UL, 0, 100);
 
 
 #if defined(Rel10) || defined(Rel14)
-    cc[CC_id].mcch_active =0;
+	cc[CC_id].mcch_active = 0;
 #endif
-    RC.mac[module_idP]->frame    = frameP;
-    RC.mac[module_idP]->subframe = subframeP;
-
-    clear_nfapi_information(RC.mac[module_idP],CC_id,frameP,subframeP);
-  }
-
-  // refresh UE list based on UEs dropped by PHY in previous subframe
-  for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
-    if (UE_list->active[i] != TRUE) continue;
-
-    rnti         = UE_RNTI(module_idP, i);
-    CC_id        = UE_PCCID(module_idP, i);
-
-    if ((frameP==0)&&(subframeP==0)) {
-      LOG_I(MAC,"UE  rnti %x : %s, PHR %d dB DL CQI %d PUSCH SNR %d PUCCH SNR %d\n", rnti,
-            UE_list->UE_sched_ctrl[i].ul_out_of_sync==0 ? "in synch" : "out of sync",
-            UE_list->UE_template[CC_id][i].phr_info,
-            UE_list->UE_sched_ctrl[i].dl_cqi[CC_id],
-	    (UE_list->UE_sched_ctrl[i].pusch_snr[CC_id]-128)/2,
-	    (UE_list->UE_sched_ctrl[i].pucch1_snr[CC_id]-128)/2);
-    }
+	RC.mac[module_idP]->frame = frameP;
+	RC.mac[module_idP]->subframe = subframeP;
 
-    RC.eNB[module_idP][CC_id]->pusch_stats_bsr[i][(frameP*10)+subframeP]=-63;
-    if (i==UE_list->head)
-      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_BSR,RC.eNB[module_idP][CC_id]->pusch_stats_bsr[i][(frameP*10)+subframeP]);
-    // increment this, it is cleared when we receive an sdu
-    RC.mac[module_idP]->UE_list.UE_sched_ctrl[i].ul_inactivity_timer++;
+	clear_nfapi_information(RC.mac[module_idP], CC_id, frameP,
+				subframeP);
+    }
 
-    RC.mac[module_idP]->UE_list.UE_sched_ctrl[i].cqi_req_timer++;
-    LOG_D(MAC,"UE %d/%x : ul_inactivity %d, cqi_req %d\n",i,rnti,
-	  RC.mac[module_idP]->UE_list.UE_sched_ctrl[i].ul_inactivity_timer,
-	  RC.mac[module_idP]->UE_list.UE_sched_ctrl[i].cqi_req_timer);
-    check_ul_failure(module_idP,CC_id,i,frameP,subframeP);
+    // refresh UE list based on UEs dropped by PHY in previous subframe
+    for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
+	if (UE_list->active[i] != TRUE)
+	    continue;
+
+	rnti = UE_RNTI(module_idP, i);
+	CC_id = UE_PCCID(module_idP, i);
+
+	if ((frameP == 0) && (subframeP == 0)) {
+	    LOG_I(MAC,
+		  "UE  rnti %x : %s, PHR %d dB DL CQI %d PUSCH SNR %d PUCCH SNR %d\n",
+		  rnti,
+		  UE_list->UE_sched_ctrl[i].ul_out_of_sync ==
+		  0 ? "in synch" : "out of sync",
+		  UE_list->UE_template[CC_id][i].phr_info,
+		  UE_list->UE_sched_ctrl[i].dl_cqi[CC_id],
+		  (UE_list->UE_sched_ctrl[i].pusch_snr[CC_id] - 128) / 2,
+		  (UE_list->UE_sched_ctrl[i].pucch1_snr[CC_id] - 128) / 2);
+	}
+
+	RC.eNB[module_idP][CC_id]->pusch_stats_bsr[i][(frameP * 10) +
+						      subframeP] = -63;
+	if (i == UE_list->head)
+	    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME
+		(VCD_SIGNAL_DUMPER_VARIABLES_UE0_BSR,
+		 RC.eNB[module_idP][CC_id]->
+		 pusch_stats_bsr[i][(frameP * 10) + subframeP]);
+	// increment this, it is cleared when we receive an sdu
+	RC.mac[module_idP]->UE_list.UE_sched_ctrl[i].ul_inactivity_timer++;
+
+	RC.mac[module_idP]->UE_list.UE_sched_ctrl[i].cqi_req_timer++;
+	LOG_D(MAC, "UE %d/%x : ul_inactivity %d, cqi_req %d\n", i, rnti,
+	      RC.mac[module_idP]->UE_list.UE_sched_ctrl[i].
+	      ul_inactivity_timer,
+	      RC.mac[module_idP]->UE_list.UE_sched_ctrl[i].cqi_req_timer);
+	check_ul_failure(module_idP, CC_id, i, frameP, subframeP);
 
-  }
+    }
 
-  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, module_idP, ENB_FLAG_YES, NOT_A_RNTI, frameP, subframeP,module_idP);
-  pdcp_run(&ctxt);
+    PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, module_idP, ENB_FLAG_YES,
+				   NOT_A_RNTI, frameP, subframeP,
+				   module_idP);
+    pdcp_run(&ctxt);
 
-  rrc_rx_tx(&ctxt,
-            0, // eNB index, unused in eNB
-            CC_id);
+    rrc_rx_tx(&ctxt, 0,		// eNB index, unused in eNB
+	      CC_id);
 
 #if defined(Rel10) || defined(Rel14)
 
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-    if (cc[CC_id].MBMS_flag >0) {
-      start_meas(&RC.mac[module_idP]->schedule_mch);
-      mbsfn_status[CC_id] = schedule_MBMS(module_idP,CC_id,frameP,subframeP);
-      stop_meas(&RC.mac[module_idP]->schedule_mch);
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+	if (cc[CC_id].MBMS_flag > 0) {
+	    start_meas(&RC.mac[module_idP]->schedule_mch);
+	    mbsfn_status[CC_id] =
+		schedule_MBMS(module_idP, CC_id, frameP, subframeP);
+	    stop_meas(&RC.mac[module_idP]->schedule_mch);
+	}
     }
-  }
 
 #endif
 
-  // This schedules MIB
-  if ((subframeP==0) && (frameP&3) == 0) schedule_mib(module_idP,frameP,subframeP);
-  // This schedules SI for legacy LTE and eMTC starting in subframeP
-  schedule_SI(module_idP,frameP,subframeP);
-  // This schedules Random-Access for legacy LTE and eMTC starting in subframeP
-  schedule_RA(module_idP,frameP,subframeP);
-  // copy previously scheduled UL resources (ULSCH + HARQ)
-  copy_ulreq(module_idP,frameP,subframeP);
-  // This schedules SRS in subframeP
-  schedule_SRS(module_idP,frameP,subframeP);
-  // This schedules ULSCH in subframeP (dci0)
-  schedule_ulsch(module_idP,frameP,subframeP);
-  // This schedules UCI_SR in subframeP
-  schedule_SR(module_idP,frameP,subframeP);
-  // This schedules UCI_CSI in subframeP
-  schedule_CSI(module_idP, frameP, subframeP);
-
-  // This schedules DLSCH in subframeP
-  schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
-
-  // Allocate CCEs for good after scheduling is done
-
-  for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) allocate_CCEs(module_idP,CC_id,subframeP,0);
-  
-
-  stop_meas(&RC.mac[module_idP]->eNB_scheduler);
-
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ULSCH_SCHEDULER,VCD_FUNCTION_OUT);
+    // This schedules MIB
+    if ((subframeP == 0) && (frameP & 3) == 0)
+	schedule_mib(module_idP, frameP, subframeP);
+    // This schedules SI for legacy LTE and eMTC starting in subframeP
+    schedule_SI(module_idP, frameP, subframeP);
+    // This schedules Random-Access for legacy LTE and eMTC starting in subframeP
+    schedule_RA(module_idP, frameP, subframeP);
+    // copy previously scheduled UL resources (ULSCH + HARQ)
+    copy_ulreq(module_idP, frameP, subframeP);
+    // This schedules SRS in subframeP
+    schedule_SRS(module_idP, frameP, subframeP);
+    // This schedules ULSCH in subframeP (dci0)
+    schedule_ulsch(module_idP, frameP, subframeP);
+    // This schedules UCI_SR in subframeP
+    schedule_SR(module_idP, frameP, subframeP);
+    // This schedules UCI_CSI in subframeP
+    schedule_CSI(module_idP, frameP, subframeP);
+
+    // This schedules DLSCH in subframeP
+    schedule_ue_spec(module_idP, frameP, subframeP, mbsfn_status);
+
+    // Allocate CCEs for good after scheduling is done
+
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++)
+	allocate_CCEs(module_idP, CC_id, subframeP, 0);
+
+
+    stop_meas(&RC.mac[module_idP]->eNB_scheduler);
+
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ULSCH_SCHEDULER,
+	 VCD_FUNCTION_OUT);
 }
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_RA.c b/openair2/LAYER2/MAC/eNB_scheduler_RA.c
index c6df031876cfba8b7ff1f5a371519ee97250957b..939f35f2ebd405d0624729efbe87012a774b12cd 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler_RA.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_RA.c
@@ -29,6 +29,9 @@
 
  */
 
+/* indented with: indent -kr eNB_scheduler_RA.c */
+
+
 #include "assertions.h"
 #include "platform_types.h"
 #include "PHY/defs.h"
@@ -55,1240 +58,1578 @@
 #include "pdcp.h"
 
 #if defined(ENABLE_ITTI)
-# include "intertask_interface.h"
+#include "intertask_interface.h"
 #endif
 
-#include "SIMULATION/TOOLS/defs.h" // for taus
+#include "SIMULATION/TOOLS/defs.h"	// for taus
 
 #include "T.h"
 
-void add_msg3(module_id_t module_idP,int CC_id, RA_TEMPLATE *RA_template, frame_t frameP, sub_frame_t subframeP) {
-
-  eNB_MAC_INST                    *eNB = RC.mac[module_idP];
-  COMMON_channels_t               *cc  = &eNB->common_channels[CC_id];
-  uint8_t                         j;
-  nfapi_ul_config_request_t      *ul_req;
-  nfapi_ul_config_request_pdu_t  *ul_config_pdu;
-  nfapi_ul_config_request_body_t *ul_req_body;
-  nfapi_hi_dci0_request_body_t   *hi_dci0_req;
-  nfapi_hi_dci0_request_pdu_t    *hi_dci0_pdu;
+void
+add_msg3(module_id_t module_idP, int CC_id, RA_t * ra, frame_t frameP,
+	 sub_frame_t subframeP)
+{
+    eNB_MAC_INST *mac = RC.mac[module_idP];
+    COMMON_channels_t *cc = &mac->common_channels[CC_id];
+    uint8_t j;
+    nfapi_ul_config_request_t *ul_req;
+    nfapi_ul_config_request_pdu_t *ul_config_pdu;
+    nfapi_ul_config_request_body_t *ul_req_body;
+    nfapi_hi_dci0_request_body_t *hi_dci0_req;
+    nfapi_hi_dci0_request_pdu_t *hi_dci0_pdu;
 
-  uint8_t rvseq[4] = {0,2,3,1};
+    uint8_t rvseq[4] = { 0, 2, 3, 1 };
 
 
-  hi_dci0_req   = &eNB->HI_DCI0_req[CC_id].hi_dci0_request_body;
-  ul_req        = &eNB->UL_req_tmp[CC_id][RA_template->Msg3_subframe];
-  ul_req_body   = &ul_req->ul_config_request_body;
-  AssertFatal(RA_template->RA_active == TRUE,"RA is not active for RA %X\n",RA_template->rnti);
+    hi_dci0_req = &mac->HI_DCI0_req[CC_id].hi_dci0_request_body;
+    ul_req = &mac->UL_req_tmp[CC_id][ra->Msg3_subframe];
+    ul_req_body = &ul_req->ul_config_request_body;
+    AssertFatal(ra->state != IDLE, "RA is not active for RA %X\n",
+		ra->rnti);
 
 #ifdef Rel14
-  if (RA_template->rach_resource_type>0) {
-    LOG_D(MAC,"[eNB %d][RAPROC] Frame %d, Subframe %d : CC_id %d CE level %d is active, Msg3 in (%d,%d)\n",
-	  module_idP,frameP,subframeP,CC_id,RA_template->rach_resource_type-1,
-	  RA_template->Msg3_frame,RA_template->Msg3_subframe);
-    LOG_D(MAC,"Frame %d, Subframe %d Adding Msg3 UL Config Request for (%d,%d) : (%d,%d)\n",
-	  frameP,subframeP,RA_template->Msg3_frame,RA_template->Msg3_subframe,RA_template->msg3_nb_rb,RA_template->msg3_round);
-
-    ul_config_pdu                                                                  = &ul_req_body->ul_config_pdu_list[ul_req_body->number_of_pdus]; 
-    
-    memset((void*)ul_config_pdu,0,sizeof(nfapi_ul_config_request_pdu_t));
-    ul_config_pdu->pdu_type                                                        = NFAPI_UL_CONFIG_ULSCH_PDU_TYPE; 
-    ul_config_pdu->pdu_size                                                        = (uint8_t)(2+sizeof(nfapi_ul_config_ulsch_pdu));
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.handle                                 = eNB->ul_handle++;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.rnti                                   = RA_template->rnti;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.resource_block_start                   = narrowband_to_first_rb(cc,RA_template->msg34_narrowband)+RA_template->msg3_first_rb;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks              = RA_template->msg3_nb_rb;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.modulation_type                        = 2;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.cyclic_shift_2_for_drms                = 0;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.frequency_hopping_enabled_flag         = 0;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.frequency_hopping_bits                 = 0;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.new_data_indication                    = 0;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.redundancy_version                     = rvseq[RA_template->msg3_round];
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.harq_process_number                    = ((10*RA_template->Msg3_frame)+RA_template->Msg3_subframe)&7;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.ul_tx_mode                             = 0;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.current_tx_nb                          = 0;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.n_srs                                  = 1;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.size                                   = get_TBS_UL(RA_template->msg3_mcs,
-												RA_template->msg3_nb_rb);
-    // Re13 fields
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.ue_type                               = RA_template->rach_resource_type>2 ? 2 : 1;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.total_number_of_repetitions           = 1;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.repetition_number                     = 1;
-    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.initial_transmission_sf_io            = (RA_template->Msg3_frame*10)+RA_template->Msg3_subframe;
-    ul_req_body->number_of_pdus++;
-  } //  if (RA_template->rach_resource_type>0) {	 
-  else
+    if (ra->rach_resource_type > 0) {
+	LOG_D(MAC,
+	      "[eNB %d][RAPROC] Frame %d, Subframe %d : CC_id %d CE level %d is active, Msg3 in (%d,%d)\n",
+	      module_idP, frameP, subframeP, CC_id,
+	      ra->rach_resource_type - 1, ra->Msg3_frame,
+	      ra->Msg3_subframe);
+	LOG_D(MAC,
+	      "Frame %d, Subframe %d Adding Msg3 UL Config Request for (%d,%d) : (%d,%d)\n",
+	      frameP, subframeP, ra->Msg3_frame, ra->Msg3_subframe,
+	      ra->msg3_nb_rb, ra->msg3_round);
+
+	ul_config_pdu =
+	    &ul_req_body->ul_config_pdu_list[ul_req_body->number_of_pdus];
+
+	memset((void *) ul_config_pdu, 0,
+	       sizeof(nfapi_ul_config_request_pdu_t));
+	ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_PDU_TYPE;
+	ul_config_pdu->pdu_size =
+	    (uint8_t) (2 + sizeof(nfapi_ul_config_ulsch_pdu));
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.handle = mac->ul_handle++;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.rnti = ra->rnti;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.resource_block_start =
+	    narrowband_to_first_rb(cc,
+				   ra->msg34_narrowband) +
+	    ra->msg3_first_rb;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks =
+	    ra->msg3_nb_rb;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.modulation_type = 2;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.cyclic_shift_2_for_drms =
+	    0;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.
+	    frequency_hopping_enabled_flag = 0;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.frequency_hopping_bits = 0;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.new_data_indication = 0;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.redundancy_version =
+	    rvseq[ra->msg3_round];
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.harq_process_number =
+	    ((10 * ra->Msg3_frame) + ra->Msg3_subframe) & 7;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.ul_tx_mode = 0;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.current_tx_nb = 0;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.n_srs = 1;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.size =
+	    get_TBS_UL(ra->msg3_mcs, ra->msg3_nb_rb);
+	// Re13 fields
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.ue_type =
+	    ra->rach_resource_type > 2 ? 2 : 1;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.
+	    total_number_of_repetitions = 1;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.repetition_number = 1;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.
+	    initial_transmission_sf_io =
+	    (ra->Msg3_frame * 10) + ra->Msg3_subframe;
+	ul_req_body->number_of_pdus++;
+    }				//  if (ra->rach_resource_type>0) {  
+    else
 #endif
     {
-      LOG_D(MAC,"[eNB %d][RAPROC] Frame %d, Subframe %d : CC_id %d RA is active, Msg3 in (%d,%d)\n",
-	    module_idP,frameP,subframeP,CC_id,RA_template->Msg3_frame,RA_template->Msg3_subframe);
-	    
-      LOG_D(MAC,"Frame %d, Subframe %d Adding Msg3 UL Config Request for (%d,%d) : (%d,%d,%d)\n",
-	    frameP,subframeP,RA_template->Msg3_frame,RA_template->Msg3_subframe,
-	    RA_template->msg3_nb_rb,RA_template->msg3_first_rb,RA_template->msg3_round);
-      
-      ul_config_pdu                                                                  = &ul_req_body->ul_config_pdu_list[ul_req_body->number_of_pdus]; 
-      
-      memset((void*)ul_config_pdu,0,sizeof(nfapi_ul_config_request_pdu_t));
-      ul_config_pdu->pdu_type                                                        = NFAPI_UL_CONFIG_ULSCH_PDU_TYPE; 
-      ul_config_pdu->pdu_size                                                        = (uint8_t)(2+sizeof(nfapi_ul_config_ulsch_pdu));
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.handle                                 = eNB->ul_handle++;
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.rnti                                   = RA_template->rnti;
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.resource_block_start                   = RA_template->msg3_first_rb;
-      AssertFatal(RA_template->msg3_nb_rb > 0, "nb_rb = 0\n");
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks              = RA_template->msg3_nb_rb;
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.modulation_type                        = 2;
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.cyclic_shift_2_for_drms                = 0;
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.frequency_hopping_enabled_flag         = 0;
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.frequency_hopping_bits                 = 0;
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.new_data_indication                    = 0;
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.redundancy_version                     = rvseq[RA_template->msg3_round];
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.harq_process_number                    = subframe2harqpid(cc,RA_template->Msg3_frame,RA_template->Msg3_subframe);
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.ul_tx_mode                             = 0;
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.current_tx_nb                          = 0;
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.n_srs                                  = 1;
-      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.size                                   = get_TBS_UL(10,RA_template->msg3_nb_rb);
-      ul_req_body->number_of_pdus++;
-      // save UL scheduling information for preprocessor
-      for (j=0;j<RA_template->msg3_nb_rb;j++) cc->vrb_map_UL[RA_template->msg3_first_rb+j]=1;
-      
-      
-      if (RA_template->msg3_round != 0) { // program HI too
-	hi_dci0_pdu                                                         = &hi_dci0_req->hi_dci0_pdu_list[hi_dci0_req->number_of_dci+hi_dci0_req->number_of_hi]; 	
-	memset((void*)hi_dci0_pdu,0,sizeof(nfapi_hi_dci0_request_pdu_t));
-	hi_dci0_pdu->pdu_type                                               = NFAPI_HI_DCI0_HI_PDU_TYPE; 
-	hi_dci0_pdu->pdu_size                                               = 2+sizeof(nfapi_hi_dci0_hi_pdu);
-	hi_dci0_pdu->hi_pdu.hi_pdu_rel8.resource_block_start                = RA_template->msg3_first_rb; 
-	hi_dci0_pdu->hi_pdu.hi_pdu_rel8.cyclic_shift_2_for_drms             = 0;
-	hi_dci0_pdu->hi_pdu.hi_pdu_rel8.hi_value                            = 0;
-	hi_dci0_req->number_of_hi++;
+	LOG_D(MAC,
+	      "[eNB %d][RAPROC] Frame %d, Subframe %d : CC_id %d RA is active, Msg3 in (%d,%d)\n",
+	      module_idP, frameP, subframeP, CC_id, ra->Msg3_frame,
+	      ra->Msg3_subframe);
+
+	LOG_D(MAC,
+	      "Frame %d, Subframe %d Adding Msg3 UL Config Request for (%d,%d) : (%d,%d,%d)\n",
+	      frameP, subframeP, ra->Msg3_frame, ra->Msg3_subframe,
+	      ra->msg3_nb_rb, ra->msg3_first_rb, ra->msg3_round);
+
+	ul_config_pdu =
+	    &ul_req_body->ul_config_pdu_list[ul_req_body->number_of_pdus];
+
+	memset((void *) ul_config_pdu, 0,
+	       sizeof(nfapi_ul_config_request_pdu_t));
+	ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_PDU_TYPE;
+	ul_config_pdu->pdu_size =
+	    (uint8_t) (2 + sizeof(nfapi_ul_config_ulsch_pdu));
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.handle = mac->ul_handle++;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.rnti = ra->rnti;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.resource_block_start =
+	    ra->msg3_first_rb;
+	AssertFatal(ra->msg3_nb_rb > 0, "nb_rb = 0\n");
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks =
+	    ra->msg3_nb_rb;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.modulation_type = 2;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.cyclic_shift_2_for_drms =
+	    0;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.
+	    frequency_hopping_enabled_flag = 0;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.frequency_hopping_bits = 0;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.new_data_indication = 0;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.redundancy_version =
+	    rvseq[ra->msg3_round];
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.harq_process_number =
+	    subframe2harqpid(cc, ra->Msg3_frame, ra->Msg3_subframe);
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.ul_tx_mode = 0;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.current_tx_nb = 0;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.n_srs = 1;
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.size =
+	    get_TBS_UL(10, ra->msg3_nb_rb);
+	ul_req_body->number_of_pdus++;
 	// save UL scheduling information for preprocessor
-	for (j=0;j<RA_template->msg3_nb_rb;j++) cc->vrb_map_UL[RA_template->msg3_first_rb+j]=1;
-	
-	LOG_D(MAC,"[eNB %d][PUSCH-RA %x] CC_id %d Frame %d subframeP %d Scheduled (PHICH) RA (mcs %d, first rb %d, nb_rb %d,round %d)\n",
-	      module_idP,RA_template->rnti,CC_id,frameP,subframeP,10,
-	      1,1,
-	      RA_template->msg3_round-1);
-      } //       if (RA_template->msg3_round != 0) { // program HI too
-    } // non-BL/CE UE case
+	for (j = 0; j < ra->msg3_nb_rb; j++)
+	    cc->vrb_map_UL[ra->msg3_first_rb + j] = 1;
+
+
+	if (ra->msg3_round != 0) {	// program HI too
+	    hi_dci0_pdu =
+		&hi_dci0_req->hi_dci0_pdu_list[hi_dci0_req->number_of_dci +
+					       hi_dci0_req->number_of_hi];
+	    memset((void *) hi_dci0_pdu, 0,
+		   sizeof(nfapi_hi_dci0_request_pdu_t));
+	    hi_dci0_pdu->pdu_type = NFAPI_HI_DCI0_HI_PDU_TYPE;
+	    hi_dci0_pdu->pdu_size = 2 + sizeof(nfapi_hi_dci0_hi_pdu);
+	    hi_dci0_pdu->hi_pdu.hi_pdu_rel8.resource_block_start =
+		ra->msg3_first_rb;
+	    hi_dci0_pdu->hi_pdu.hi_pdu_rel8.cyclic_shift_2_for_drms = 0;
+	    hi_dci0_pdu->hi_pdu.hi_pdu_rel8.hi_value = 0;
+	    hi_dci0_req->number_of_hi++;
+	    // save UL scheduling information for preprocessor
+	    for (j = 0; j < ra->msg3_nb_rb; j++)
+		cc->vrb_map_UL[ra->msg3_first_rb + j] = 1;
+
+	    LOG_D(MAC,
+		  "[eNB %d][PUSCH-RA %x] CC_id %d Frame %d subframeP %d Scheduled (PHICH) RA (mcs %d, first rb %d, nb_rb %d,round %d)\n",
+		  module_idP, ra->rnti, CC_id, frameP, subframeP, 10, 1, 1,
+		  ra->msg3_round - 1);
+	}			//       if (ra->msg3_round != 0) { // program HI too
+    }				// non-BL/CE UE case
 }
 
-void generate_Msg2(module_id_t module_idP,int CC_idP,frame_t frameP,sub_frame_t subframeP,RA_TEMPLATE *RA_template) {
+void
+generate_Msg2(module_id_t module_idP, int CC_idP, frame_t frameP,
+	      sub_frame_t subframeP, RA_t * ra)
+{
+
+    eNB_MAC_INST *mac = RC.mac[module_idP];
+    COMMON_channels_t *cc = mac->common_channels;
 
-  eNB_MAC_INST                    *eNB = RC.mac[module_idP];
-  COMMON_channels_t               *cc  = eNB->common_channels;
- 
-  uint8_t                         *vrb_map;
-  int                             first_rb;
-  int                             N_RB_DL;
-  nfapi_dl_config_request_pdu_t   *dl_config_pdu;
-  nfapi_tx_request_pdu_t          *TX_req;
-  nfapi_dl_config_request_body_t *dl_req;
+    uint8_t *vrb_map;
+    int first_rb;
+    int N_RB_DL;
+    nfapi_dl_config_request_pdu_t *dl_config_pdu;
+    nfapi_tx_request_pdu_t *TX_req;
+    nfapi_dl_config_request_body_t *dl_req;
 
-  vrb_map       = cc[CC_idP].vrb_map;
-  dl_req        = &eNB->DL_req[CC_idP].dl_config_request_body;
-  dl_config_pdu = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
-  N_RB_DL       = to_prb(cc[CC_idP].mib->message.dl_Bandwidth);  
+    vrb_map = cc[CC_idP].vrb_map;
+    dl_req = &mac->DL_req[CC_idP].dl_config_request_body;
+    dl_config_pdu = &dl_req->dl_config_pdu_list[dl_req->number_pdu];
+    N_RB_DL = to_prb(cc[CC_idP].mib->message.dl_Bandwidth);
 
 #ifdef Rel14
-  int rmax            = 0;
-  int rep             = 0; 
-  int reps            = 0;
-  int num_nb          = 0;
-
-  first_rb        = 0;
-  struct PRACH_ConfigSIB_v1310 *ext4_prach;
-  PRACH_ParametersListCE_r13_t *prach_ParametersListCE_r13;
-  PRACH_ParametersCE_r13_t *p[4]={NULL,NULL,NULL,NULL};
-
-  uint16_t absSF      = (10*frameP)+subframeP;
-  uint16_t absSF_Msg2 = (10*RA_template->Msg2_frame)+RA_template->Msg2_subframe;
-
-  if (absSF>absSF_Msg2) return; // we're not ready yet, need to be to start ==  
-  
-  if (cc[CC_idP].radioResourceConfigCommon_BR) {
-
-    ext4_prach                 = cc[CC_idP].radioResourceConfigCommon_BR->ext4->prach_ConfigCommon_v1310;
-    prach_ParametersListCE_r13 = &ext4_prach->prach_ParametersListCE_r13;
-        
-    switch (prach_ParametersListCE_r13->list.count) {
-    case 4:
-      p[3]=prach_ParametersListCE_r13->list.array[3];
-    case 3:
-      p[2]=prach_ParametersListCE_r13->list.array[2];
-    case 2:
-      p[1]=prach_ParametersListCE_r13->list.array[1];
-    case 1:
-      p[0]=prach_ParametersListCE_r13->list.array[0];
-      break;
-    default:
-      AssertFatal(1==0,"Illegal count for prach_ParametersListCE_r13 %d\n",(int)prach_ParametersListCE_r13->list.count);
-      break;
-    }
-  }
-
-  if (RA_template->rach_resource_type > 0) {
-    
-    // This uses an MPDCCH Type 2 common allocation according to Section 9.1.5 36-213
-    // Parameters:
-    //    p=2+4 PRB set (number of PRB pairs 3)
-    //    rmax = mpdcch-NumRepetition-RA-r13 => Table 9.1.5-3
-    //    if CELevel = 0,1 => Table 9.1.5-1b for MPDCCH candidates
-    //    if CELevel = 2,3 => Table 9.1.5-2b for MPDCCH candidates
-    //    distributed transmission
-    
-    // rmax from SIB2 information
-    AssertFatal(rmax<9,"rmax>8!\n");
-    rmax           = 1<<p[RA_template->rach_resource_type-1]->mpdcch_NumRepetition_RA_r13;
-    // choose r1 by default for RAR (Table 9.1.5-5)
-    rep            = 0; 
-    // get actual repetition count from Table 9.1.5-3
-    reps           = (rmax<=8)?(1<<rep):(rmax>>(3-rep));
-    // get narrowband according to higher-layer config 
-    num_nb = p[RA_template->rach_resource_type-1]->mpdcch_NarrowbandsToMonitor_r13.list.count;
-    RA_template->msg2_narrowband = *p[RA_template->rach_resource_type-1]->mpdcch_NarrowbandsToMonitor_r13.list.array[RA_template->preamble_index%num_nb];
-    first_rb = narrowband_to_first_rb(&cc[CC_idP],RA_template->msg2_narrowband);
-    
-    if ((RA_template->msg2_mpdcch_repetition_cnt == 0) &&
-	(mpdcch_sf_condition(eNB,CC_idP,frameP,subframeP,rmax,TYPE2,-1)>0)){
-      // MPDCCH configuration for RAR
-      LOG_D(MAC,"[eNB %d][RAPROC] Frame %d, Subframe %d : In generate_Msg2, Programming MPDCCH %d repetitions\n",
-	    module_idP,frameP,subframeP,reps);
-      
-      
-      memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-      dl_config_pdu->pdu_type                                                                  = NFAPI_DL_CONFIG_MPDCCH_PDU_TYPE; 
-      dl_config_pdu->pdu_size                                                                  = (uint8_t)(2+sizeof(nfapi_dl_config_mpdcch_pdu));
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.dci_format                                    = (RA_template->rach_resource_type > 1) ? 11 : 10;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.mpdcch_narrow_band                            = RA_template->msg2_narrowband;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.number_of_prb_pairs                           = 6;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.resource_block_assignment                     = 0; // Note: this can be dynamic
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.mpdcch_tansmission_type                       = 1; // imposed (9.1.5 in 213) for Type 2 Common search space  
-      AssertFatal(cc[CC_idP].sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13!=NULL,
-		  "cc[CC_id].sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13 is null\n");
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.start_symbol                                  = cc[CC_idP].sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->startSymbolBR_r13;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.ecce_index                                    = 0;  // Note: this should be dynamic
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.aggregation_level                             = 16; // OK for CEModeA r1-3 (9.1.5-1b) or CEModeB r1-4
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.rnti_type                                     = 2;  // RA-RNTI
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.rnti                                          = RA_template->RA_rnti;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.ce_mode                                       = (RA_template->rach_resource_type < 3) ? 1 : 2;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.drms_scrambling_init                          = cc[CC_idP].physCellId;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.initial_transmission_sf_io                    = (frameP*10)+subframeP;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.transmission_power                            = 6000; // 0dB
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.resource_block_coding                         = getRIV(6,0,6);  // Note: still to be checked if it should not be (getRIV(N_RB_DL,first_rb,6)) : Check nFAPI specifications and what is done L1 with this parameter
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.mcs                                           = 4; // adjust according to size of RAR, 208 bits with N1A_PRB=3
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.pdsch_reptition_levels                        = 4; // fix to 4 for now
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.redundancy_version                            = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.new_data_indicator                            = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.harq_process                                  = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.tpmi_length                                   = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.tpmi                                          = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.pmi_flag                                      = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.pmi                                           = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.harq_resource_offset                          = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.dci_subframe_repetition_number                = rep; 
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.tpc                                           = 1;// N1A_PRB=3 (36.212); => 208 bits for mcs=4, choose mcs according t message size TBD
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.downlink_assignment_index_length              = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.downlink_assignment_index                     = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.allocate_prach_flag                           = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.preamble_index                                = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.prach_mask_index                              = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.starting_ce_level                             = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.srs_request                                   = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.antenna_ports_and_scrambling_identity_flag    = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.antenna_ports_and_scrambling_identity         = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.frequency_hopping_enabled_flag                = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.paging_direct_indication_differentiation_flag = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.direct_indication                             = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.total_dci_length_including_padding            = 0; // this is not needed by OAI L1, but should be filled in
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.number_of_tx_antenna_ports                    = 1;
-      RA_template->msg2_mpdcch_repetition_cnt++;
-      dl_req->number_pdu++;
-      RA_template->Msg2_subframe = (RA_template->Msg2_subframe+9)%10;
-
-    } //repetition_count==0 && SF condition met
-    if (RA_template->msg2_mpdcch_repetition_cnt>0) { // we're in a stream of repetitions
-      LOG_D(MAC,"[eNB %d][RAPROC] Frame %d, Subframe %d : In generate_Msg2, MPDCCH repetition %d\n",
-	    module_idP,frameP,subframeP,RA_template->msg2_mpdcch_repetition_cnt);
-
-      if (RA_template->msg2_mpdcch_repetition_cnt==reps) { // this is the last mpdcch repetition
-	if (cc[CC_idP].tdd_Config==NULL) { // FDD case
-	  // wait 2 subframes for PDSCH transmission
-	  if (subframeP>7) RA_template->Msg2_frame = (frameP+1)&1023;
-	  else             RA_template->Msg2_frame = frameP;
-	  RA_template->Msg2_subframe               = (subframeP+2)%10; // +2 is the "n+x" from Section 7.1.11  in 36.213
+    int rmax = 0;
+    int rep = 0;
+    int reps = 0;
+    int num_nb = 0;
+
+    first_rb = 0;
+    struct PRACH_ConfigSIB_v1310 *ext4_prach;
+    PRACH_ParametersListCE_r13_t *prach_ParametersListCE_r13;
+    PRACH_ParametersCE_r13_t *p[4] = { NULL, NULL, NULL, NULL };
+
+    uint16_t absSF = (10 * frameP) + subframeP;
+    uint16_t absSF_Msg2 = (10 * ra->Msg2_frame) + ra->Msg2_subframe;
+
+    if (absSF > absSF_Msg2)
+	return;			// we're not ready yet, need to be to start ==  
+
+    if (cc[CC_idP].radioResourceConfigCommon_BR) {
+
+	ext4_prach =
+	    cc[CC_idP].radioResourceConfigCommon_BR->ext4->
+	    prach_ConfigCommon_v1310;
+	prach_ParametersListCE_r13 =
+	    &ext4_prach->prach_ParametersListCE_r13;
+
+	switch (prach_ParametersListCE_r13->list.count) {
+	case 4:
+	    p[3] = prach_ParametersListCE_r13->list.array[3];
+	case 3:
+	    p[2] = prach_ParametersListCE_r13->list.array[2];
+	case 2:
+	    p[1] = prach_ParametersListCE_r13->list.array[1];
+	case 1:
+	    p[0] = prach_ParametersListCE_r13->list.array[0];
+	    break;
+	default:
+	    AssertFatal(1 == 0,
+			"Illegal count for prach_ParametersListCE_r13 %d\n",
+			(int) prach_ParametersListCE_r13->list.count);
+	    break;
 	}
-	else {
-	  AssertFatal(1==0,"TDD case not done yet\n");
+    }
+
+    if (ra->rach_resource_type > 0) {
+
+	// This uses an MPDCCH Type 2 common allocation according to Section 9.1.5 36-213
+	// Parameters:
+	//    p=2+4 PRB set (number of PRB pairs 3)
+	//    rmax = mpdcch-NumRepetition-RA-r13 => Table 9.1.5-3
+	//    if CELevel = 0,1 => Table 9.1.5-1b for MPDCCH candidates
+	//    if CELevel = 2,3 => Table 9.1.5-2b for MPDCCH candidates
+	//    distributed transmission
+
+	// rmax from SIB2 information
+	AssertFatal(rmax < 9, "rmax>8!\n");
+	rmax =
+	    1 << p[ra->rach_resource_type -
+		   1]->mpdcch_NumRepetition_RA_r13;
+	// choose r1 by default for RAR (Table 9.1.5-5)
+	rep = 0;
+	// get actual repetition count from Table 9.1.5-3
+	reps = (rmax <= 8) ? (1 << rep) : (rmax >> (3 - rep));
+	// get narrowband according to higher-layer config 
+	num_nb =
+	    p[ra->rach_resource_type -
+	      1]->mpdcch_NarrowbandsToMonitor_r13.list.count;
+	ra->msg2_narrowband =
+	    *p[ra->rach_resource_type -
+	       1]->mpdcch_NarrowbandsToMonitor_r13.list.array[ra->
+							      preamble_index
+							      % num_nb];
+	first_rb =
+	    narrowband_to_first_rb(&cc[CC_idP], ra->msg2_narrowband);
+
+	if ((ra->msg2_mpdcch_repetition_cnt == 0) &&
+	    (mpdcch_sf_condition
+	     (mac, CC_idP, frameP, subframeP, rmax, TYPE2, -1) > 0)) {
+	    // MPDCCH configuration for RAR
+	    LOG_D(MAC,
+		  "[eNB %d][RAPROC] Frame %d, Subframe %d : In generate_Msg2, Programming MPDCCH %d repetitions\n",
+		  module_idP, frameP, subframeP, reps);
+
+
+	    memset((void *) dl_config_pdu, 0,
+		   sizeof(nfapi_dl_config_request_pdu_t));
+	    dl_config_pdu->pdu_type = NFAPI_DL_CONFIG_MPDCCH_PDU_TYPE;
+	    dl_config_pdu->pdu_size =
+		(uint8_t) (2 + sizeof(nfapi_dl_config_mpdcch_pdu));
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.dci_format =
+		(ra->rach_resource_type > 1) ? 11 : 10;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.mpdcch_narrow_band =
+		ra->msg2_narrowband;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.
+		number_of_prb_pairs = 6;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.resource_block_assignment = 0;	// Note: this can be dynamic
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.mpdcch_tansmission_type = 1;	// imposed (9.1.5 in 213) for Type 2 Common search space  
+	    AssertFatal(cc[CC_idP].
+			sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13
+			!= NULL,
+			"cc[CC_id].sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13 is null\n");
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.start_symbol =
+		cc[CC_idP].sib1_v13ext->
+		bandwidthReducedAccessRelatedInfo_r13->startSymbolBR_r13;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.ecce_index = 0;	// Note: this should be dynamic
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.aggregation_level = 16;	// OK for CEModeA r1-3 (9.1.5-1b) or CEModeB r1-4
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.rnti_type = 2;	// RA-RNTI
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.rnti = ra->RA_rnti;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.ce_mode =
+		(ra->rach_resource_type < 3) ? 1 : 2;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.
+		drms_scrambling_init = cc[CC_idP].physCellId;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.
+		initial_transmission_sf_io = (frameP * 10) + subframeP;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.transmission_power = 6000;	// 0dB
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.resource_block_coding = getRIV(6, 0, 6);	// Note: still to be checked if it should not be (getRIV(N_RB_DL,first_rb,6)) : Check nFAPI specifications and what is done L1 with this parameter
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.mcs = 4;	// adjust according to size of RAR, 208 bits with N1A_PRB=3
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.pdsch_reptition_levels = 4;	// fix to 4 for now
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.redundancy_version =
+		0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.new_data_indicator =
+		0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.harq_process = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.tpmi_length = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.tpmi = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.pmi_flag = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.pmi = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.
+		harq_resource_offset = 0;
+	    dl_config_pdu->mpdcch_pdu.
+		mpdcch_pdu_rel13.dci_subframe_repetition_number = rep;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.tpc = 1;	// N1A_PRB=3 (36.212); => 208 bits for mcs=4, choose mcs according t message size TBD
+	    dl_config_pdu->mpdcch_pdu.
+		mpdcch_pdu_rel13.downlink_assignment_index_length = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.
+		downlink_assignment_index = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.
+		allocate_prach_flag = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.preamble_index = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.prach_mask_index =
+		0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.starting_ce_level =
+		0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.srs_request = 0;
+	    dl_config_pdu->mpdcch_pdu.
+		mpdcch_pdu_rel13.antenna_ports_and_scrambling_identity_flag
+		= 0;
+	    dl_config_pdu->mpdcch_pdu.
+		mpdcch_pdu_rel13.antenna_ports_and_scrambling_identity = 0;
+	    dl_config_pdu->mpdcch_pdu.
+		mpdcch_pdu_rel13.frequency_hopping_enabled_flag = 0;
+	    dl_config_pdu->mpdcch_pdu.
+		mpdcch_pdu_rel13.paging_direct_indication_differentiation_flag
+		= 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.direct_indication =
+		0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.total_dci_length_including_padding = 0;	// this is not needed by OAI L1, but should be filled in
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.
+		number_of_tx_antenna_ports = 1;
+	    ra->msg2_mpdcch_repetition_cnt++;
+	    dl_req->number_pdu++;
+	    ra->Msg2_subframe = (ra->Msg2_subframe + 9) % 10;
+
+	}			//repetition_count==0 && SF condition met
+	if (ra->msg2_mpdcch_repetition_cnt > 0) {	// we're in a stream of repetitions
+	    LOG_D(MAC,
+		  "[eNB %d][RAPROC] Frame %d, Subframe %d : In generate_Msg2, MPDCCH repetition %d\n",
+		  module_idP, frameP, subframeP,
+		  ra->msg2_mpdcch_repetition_cnt);
+
+	    if (ra->msg2_mpdcch_repetition_cnt == reps) {	// this is the last mpdcch repetition
+		if (cc[CC_idP].tdd_Config == NULL) {	// FDD case
+		    // wait 2 subframes for PDSCH transmission
+		    if (subframeP > 7)
+			ra->Msg2_frame = (frameP + 1) & 1023;
+		    else
+			ra->Msg2_frame = frameP;
+		    ra->Msg2_subframe = (subframeP + 2) % 10;	// +2 is the "n+x" from Section 7.1.11  in 36.213
+		} else {
+		    AssertFatal(1 == 0, "TDD case not done yet\n");
+		}
+	    }			// mpdcch_repetition_count == reps
+	    ra->msg2_mpdcch_repetition_cnt++;
+
+	    if ((ra->Msg2_frame == frameP)
+		&& (ra->Msg2_subframe == subframeP)) {
+		// Program PDSCH
+		LOG_D(MAC,
+		      "[eNB %d][RAPROC] Frame %d, Subframe %d : In generate_Msg2, Programming PDSCH\n",
+		      module_idP, frameP, subframeP);
+		ra->state = WAITMSG3;
+
+		dl_config_pdu =
+		    &dl_req->dl_config_pdu_list[dl_req->number_pdu];
+		memset((void *) dl_config_pdu, 0,
+		       sizeof(nfapi_dl_config_request_pdu_t));
+		dl_config_pdu->pdu_type = NFAPI_DL_CONFIG_DLSCH_PDU_TYPE;
+		dl_config_pdu->pdu_size =
+		    (uint8_t) (2 + sizeof(nfapi_dl_config_dlsch_pdu));
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index =
+		    mac->pdu_index[CC_idP];
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti = ra->RA_rnti;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    resource_allocation_type = 2;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = 0;	// localized
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    resource_block_coding = getRIV(N_RB_DL, first_rb, 6);
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation = 2;	//QPSK
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    redundancy_version = 0;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks = 1;	// first block
+		dl_config_pdu->dlsch_pdu.
+		    dlsch_pdu_rel8.transport_block_to_codeword_swap_flag =
+		    0;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    transmission_scheme = (cc->p_eNB == 1) ? 0 : 1;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_layers =
+		    1;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    number_of_subbands = 1;
+		//      dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = ;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    ue_category_capacity = 1;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa = 4;	// 0 dB
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    delta_power_offset_index = 0;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap = 0;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb = get_subbandsize(cc->mib->message.dl_Bandwidth);	// ignored
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_mode =
+		    (cc->p_eNB == 1) ? 1 : 2;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    num_bf_prb_per_subband = 1;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector = 1;
+		//      dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.bf_vector                    = ; 
+
+		// Rel10 fields
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel10.pdsch_start =
+		    cc[CC_idP].
+		    sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->
+		    startSymbolBR_r13;
+		// Rel13 fields
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.ue_type =
+		    (ra->rach_resource_type < 3) ? 1 : 2;;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.pdsch_payload_type = 2;	// not SI message
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.
+		    initial_transmission_sf_io = (10 * frameP) + subframeP;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.drms_table_flag =
+		    0;
+		dl_req->number_pdu++;
+
+		// Program UL processing for Msg3, same as regular LTE
+		get_Msg3alloc(&cc[CC_idP], subframeP, frameP,
+			      &ra->Msg3_frame, &ra->Msg3_subframe);
+		add_msg3(module_idP, CC_idP, ra, frameP, subframeP);
+		fill_rar_br(mac, CC_idP, ra, frameP, subframeP,
+			    cc[CC_idP].RAR_pdu.payload,
+			    ra->rach_resource_type - 1);
+		// DL request
+		mac->TX_req[CC_idP].sfn_sf = (frameP << 4) + subframeP;
+		TX_req =
+		    &mac->TX_req[CC_idP].tx_request_body.tx_pdu_list[mac->
+								     TX_req
+								     [CC_idP].
+								     tx_request_body.number_of_pdus];
+		TX_req->pdu_length = 7;	// This should be changed if we have more than 1 preamble 
+		TX_req->pdu_index = mac->pdu_index[CC_idP]++;
+		TX_req->num_segments = 1;
+		TX_req->segments[0].segment_length = 7;
+		TX_req->segments[0].segment_data =
+		    cc[CC_idP].RAR_pdu.payload;
+		mac->TX_req[CC_idP].tx_request_body.number_of_pdus++;
+	    }
 	}
-      }// mpdcch_repetition_count == reps
-      RA_template->msg2_mpdcch_repetition_cnt++;	      
-
-      if ((RA_template->Msg2_frame == frameP) && (RA_template->Msg2_subframe == subframeP)) {
-	// Program PDSCH
-	LOG_D(MAC,"[eNB %d][RAPROC] Frame %d, Subframe %d : In generate_Msg2, Programming PDSCH\n",
-	      module_idP,frameP,subframeP);
-	RA_template->generate_rar = 0;	      
-	
-	dl_config_pdu                                                                  = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
-	memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-	dl_config_pdu->pdu_type                                                        = NFAPI_DL_CONFIG_DLSCH_PDU_TYPE; 
-	dl_config_pdu->pdu_size                                                        = (uint8_t)(2+sizeof(nfapi_dl_config_dlsch_pdu));
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index                              = eNB->pdu_index[CC_idP];
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti                                   = RA_template->RA_rnti;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type               = 2;   
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = 0;   // localized
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_block_coding                  = getRIV(N_RB_DL,first_rb,6);
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation                             = 2; //QPSK
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.redundancy_version                     = 0;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks                       = 1;// first block
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_block_to_codeword_swap_flag  = 0;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_scheme                    = (cc->p_eNB==1 ) ? 0 : 1;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_layers                       = 1;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_subbands                     = 1;
-	//	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = ;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ue_category_capacity                   = 1;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa                                     = 4; // 0 dB
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.delta_power_offset_index               = 0;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap                                   = 0;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb                                   = get_subbandsize(cc->mib->message.dl_Bandwidth); // ignored
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_mode                      = (cc->p_eNB==1 ) ? 1 : 2;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_prb_per_subband                 = 1;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector                          = 1;
-	//	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.bf_vector                    = ; 
-
-	// Rel10 fields
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel10.pdsch_start                           = cc[CC_idP].sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->startSymbolBR_r13;
-	// Rel13 fields
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.ue_type                               = (RA_template->rach_resource_type < 3) ? 1 : 2;;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.pdsch_payload_type                    = 2;  // not SI message
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.initial_transmission_sf_io            = (10*frameP)+subframeP;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.drms_table_flag                       = 0;
-	dl_req->number_pdu++;
-	
-	// Program UL processing for Msg3, same as regular LTE
-	get_Msg3alloc(&cc[CC_idP],subframeP,frameP,&RA_template->Msg3_frame,&RA_template->Msg3_subframe);
-	add_msg3(module_idP,CC_idP, RA_template,frameP,subframeP);
-	fill_rar_br(eNB,CC_idP,RA_template,frameP,subframeP,cc[CC_idP].RAR_pdu.payload,RA_template->rach_resource_type-1);
-	// DL request
-	eNB->TX_req[CC_idP].sfn_sf                                            = (frameP<<4)+subframeP;
-	TX_req                                                                = &eNB->TX_req[CC_idP].tx_request_body.tx_pdu_list[eNB->TX_req[CC_idP].tx_request_body.number_of_pdus]; 		     
-	TX_req->pdu_length                                                    = 7;  // This should be changed if we have more than 1 preamble 
-	TX_req->pdu_index                                                     = eNB->pdu_index[CC_idP]++;
-	TX_req->num_segments                                                  = 1;
-	TX_req->segments[0].segment_length                                    = 7;
-	TX_req->segments[0].segment_data                                      = cc[CC_idP].RAR_pdu.payload;
-	eNB->TX_req[CC_idP].tx_request_body.number_of_pdus++;
-      }
-    }      
-    
-  }		
-  else
+
+    } else
 #endif
     {
 
-      if ((RA_template->Msg2_frame == frameP) && (RA_template->Msg2_subframe == subframeP)) {
-	LOG_D(MAC,"[eNB %d] CC_id %d Frame %d, subframeP %d: Generating RAR DCI, RA_active %d format 1A (%d,%d))\n",
-	      module_idP, CC_idP, frameP, subframeP,
-	      RA_template->RA_active,
-	      
-	      RA_template->RA_dci_fmt1,
-	      RA_template->RA_dci_size_bits1);
-
-	// Allocate 4 PRBS starting in RB 0
-	first_rb = 0;
-	vrb_map[first_rb] = 1;
-	vrb_map[first_rb+1] = 1;
-	vrb_map[first_rb+2] = 1;
-	vrb_map[first_rb+3] = 1;
-	
-	memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-	dl_config_pdu->pdu_type                                                          = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE; 
-	dl_config_pdu->pdu_size                                                          = (uint8_t)(2+sizeof(nfapi_dl_config_dci_dl_pdu));
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format                             = NFAPI_DL_DCI_FORMAT_1A;
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level                      = 4;
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti                                   = RA_template->RA_rnti;
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type                              = 2;    // RA-RNTI : see Table 4-10 from SCF082 - nFAPI specifications
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power                     = 6000; // equal to RS power
-	
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.harq_process                           = 0;
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tpc                                    = 1; // no TPC
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.new_data_indicator_1                   = 1;
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.mcs_1                                  = 0;
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.redundancy_version_1                   = 0;
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.virtual_resource_block_assignment_flag = 0;
-	
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_block_coding                  = getRIV(N_RB_DL,first_rb,4);      
-
-	// This checks if the above DCI allocation is feasible in current subframe
-	if (!CCE_allocation_infeasible(module_idP,CC_idP,0,subframeP,dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,RA_template->RA_rnti)) {
-	  LOG_D(MAC,"Frame %d: Subframe %d : Adding common DCI for RA_RNTI %x\n",
-		frameP,subframeP,RA_template->RA_rnti);
-	  dl_req->number_dci++;
-	  dl_req->number_pdu++;
-	  
-	  dl_config_pdu                                                                  = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
-	  memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-	  dl_config_pdu->pdu_type                                                        = NFAPI_DL_CONFIG_DLSCH_PDU_TYPE; 
-	  dl_config_pdu->pdu_size                                                        = (uint8_t)(2+sizeof(nfapi_dl_config_dlsch_pdu));
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index                              = eNB->pdu_index[CC_idP];
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti                                   = RA_template->RA_rnti;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type               = 2;   // format 1A/1B/1D
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = 0;   // localized
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_block_coding                  = getRIV(N_RB_DL,first_rb,4);
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation                             = 2; //QPSK
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.redundancy_version                     = 0;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks                       = 1;// first block
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_block_to_codeword_swap_flag  = 0;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_scheme                    = (cc->p_eNB==1 ) ? 0 : 1;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_layers                       = 1;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_subbands                     = 1;
-	  //	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = ;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ue_category_capacity                   = 1;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa                                     = 4; // 0 dB
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.delta_power_offset_index               = 0;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap                                   = 0;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb                                   = get_subbandsize(cc->mib->message.dl_Bandwidth); // ignored
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_mode                      = (cc->p_eNB==1 ) ? 1 : 2;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_prb_per_subband                 = 1;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector                          = 1;
-	  //	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.bf_vector                    = ; 
-	  dl_req->number_pdu++;
-	  
-	  // Program UL processing for Msg3
-	  get_Msg3alloc(&cc[CC_idP],subframeP,frameP,&RA_template->Msg3_frame,&RA_template->Msg3_subframe);
-
-	  LOG_D(MAC,"Frame %d, Subframe %d: Setting Msg3 reception for Frame %d Subframe %d\n",
-		frameP,subframeP,RA_template->Msg3_frame,RA_template->Msg3_subframe);
-	  
-	  fill_rar(module_idP,CC_idP,frameP,cc[CC_idP].RAR_pdu.payload,N_RB_DL,7);
-	  add_msg3(module_idP,CC_idP, RA_template,frameP,subframeP);
-
-	  // DL request
-	  eNB->TX_req[CC_idP].sfn_sf                                             = (frameP<<4)+subframeP;
-	  TX_req                                                                = &eNB->TX_req[CC_idP].tx_request_body.tx_pdu_list[eNB->TX_req[CC_idP].tx_request_body.number_of_pdus]; 		     
-	  TX_req->pdu_length                                                    = 7;  // This should be changed if we have more than 1 preamble 
-	  TX_req->pdu_index                                                     = eNB->pdu_index[CC_idP]++;
-	  TX_req->num_segments                                                  = 1;
-	  TX_req->segments[0].segment_length                                    = 7;
-	  TX_req->segments[0].segment_data                                      = cc[CC_idP].RAR_pdu.payload;
-	  eNB->TX_req[CC_idP].tx_request_body.number_of_pdus++;
-	} // PDCCH CCE allocation is feasible
-      } // Msg2 frame/subframe condition
-    } // else BL/CE
+	if ((ra->Msg2_frame == frameP) && (ra->Msg2_subframe == subframeP)) {
+	    LOG_D(MAC,
+		  "[eNB %d] CC_id %d Frame %d, subframeP %d: Generating RAR DCI, state %d\n",
+		  module_idP, CC_idP, frameP, subframeP, ra->state);
+
+	    // Allocate 4 PRBS starting in RB 0
+	    first_rb = 0;
+	    vrb_map[first_rb] = 1;
+	    vrb_map[first_rb + 1] = 1;
+	    vrb_map[first_rb + 2] = 1;
+	    vrb_map[first_rb + 3] = 1;
+
+	    memset((void *) dl_config_pdu, 0,
+		   sizeof(nfapi_dl_config_request_pdu_t));
+	    dl_config_pdu->pdu_type = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
+	    dl_config_pdu->pdu_size =
+		(uint8_t) (2 + sizeof(nfapi_dl_config_dci_dl_pdu));
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format =
+		NFAPI_DL_DCI_FORMAT_1A;
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level =
+		4;
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti = ra->RA_rnti;
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type = 2;	// RA-RNTI : see Table 4-10 from SCF082 - nFAPI specifications
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power = 6000;	// equal to RS power
+
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.harq_process = 0;
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tpc = 1;	// no TPC
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+		new_data_indicator_1 = 1;
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.mcs_1 = 0;
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+		redundancy_version_1 = 0;
+	    dl_config_pdu->dci_dl_pdu.
+		dci_dl_pdu_rel8.virtual_resource_block_assignment_flag = 0;
+
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+		resource_block_coding = getRIV(N_RB_DL, first_rb, 4);
+
+	    // This checks if the above DCI allocation is feasible in current subframe
+	    if (!CCE_allocation_infeasible
+		(module_idP, CC_idP, 0, subframeP,
+		 dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+		 aggregation_level, ra->RA_rnti)) {
+		LOG_D(MAC,
+		      "Frame %d: Subframe %d : Adding common DCI for RA_RNTI %x\n",
+		      frameP, subframeP, ra->RA_rnti);
+		dl_req->number_dci++;
+		dl_req->number_pdu++;
+
+		dl_config_pdu =
+		    &dl_req->dl_config_pdu_list[dl_req->number_pdu];
+		memset((void *) dl_config_pdu, 0,
+		       sizeof(nfapi_dl_config_request_pdu_t));
+		dl_config_pdu->pdu_type = NFAPI_DL_CONFIG_DLSCH_PDU_TYPE;
+		dl_config_pdu->pdu_size =
+		    (uint8_t) (2 + sizeof(nfapi_dl_config_dlsch_pdu));
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index =
+		    mac->pdu_index[CC_idP];
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti = ra->RA_rnti;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type = 2;	// format 1A/1B/1D
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = 0;	// localized
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    resource_block_coding = getRIV(N_RB_DL, first_rb, 4);
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation = 2;	//QPSK
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    redundancy_version = 0;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks = 1;	// first block
+		dl_config_pdu->dlsch_pdu.
+		    dlsch_pdu_rel8.transport_block_to_codeword_swap_flag =
+		    0;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    transmission_scheme = (cc->p_eNB == 1) ? 0 : 1;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_layers =
+		    1;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    number_of_subbands = 1;
+		//    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = ;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    ue_category_capacity = 1;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa = 4;	// 0 dB
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    delta_power_offset_index = 0;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap = 0;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb = get_subbandsize(cc->mib->message.dl_Bandwidth);	// ignored
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_mode =
+		    (cc->p_eNB == 1) ? 1 : 2;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    num_bf_prb_per_subband = 1;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector = 1;
+		//    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.bf_vector                    = ; 
+		dl_req->number_pdu++;
+
+		// Program UL processing for Msg3
+		get_Msg3alloc(&cc[CC_idP], subframeP, frameP,
+			      &ra->Msg3_frame, &ra->Msg3_subframe);
+
+		LOG_D(MAC,
+		      "Frame %d, Subframe %d: Setting Msg3 reception for Frame %d Subframe %d\n",
+		      frameP, subframeP, ra->Msg3_frame,
+		      ra->Msg3_subframe);
+
+		fill_rar(module_idP, CC_idP, ra, frameP,
+			 cc[CC_idP].RAR_pdu.payload, N_RB_DL, 7);
+		add_msg3(module_idP, CC_idP, ra, frameP, subframeP);
+		ra->state = WAITMSG3;
+
+		// DL request
+		mac->TX_req[CC_idP].sfn_sf = (frameP << 4) + subframeP;
+		TX_req =
+		    &mac->TX_req[CC_idP].tx_request_body.tx_pdu_list[mac->
+								     TX_req
+								     [CC_idP].
+								     tx_request_body.number_of_pdus];
+		TX_req->pdu_length = 7;	// This should be changed if we have more than 1 preamble 
+		TX_req->pdu_index = mac->pdu_index[CC_idP]++;
+		TX_req->num_segments = 1;
+		TX_req->segments[0].segment_length = 7;
+		TX_req->segments[0].segment_data =
+		    cc[CC_idP].RAR_pdu.payload;
+		mac->TX_req[CC_idP].tx_request_body.number_of_pdus++;
+	    }			// PDCCH CCE allocation is feasible
+	}			// Msg2 frame/subframe condition
+    }				// else BL/CE
 }
 
-void generate_Msg4(module_id_t module_idP,int CC_idP,frame_t frameP,sub_frame_t subframeP,RA_TEMPLATE *RA_template){
+void
+generate_Msg4(module_id_t module_idP, int CC_idP, frame_t frameP,
+	      sub_frame_t subframeP, RA_t * ra)
+{
 
 
-  eNB_MAC_INST                    *eNB = RC.mac[module_idP];
-  COMMON_channels_t               *cc  = eNB->common_channels;
-  int16_t                         rrc_sdu_length;
-  int                             UE_id           = -1;
-  uint16_t                        msg4_padding;
-  uint16_t                        msg4_post_padding;
-  uint16_t                        msg4_header;
+    eNB_MAC_INST *mac = RC.mac[module_idP];
+    COMMON_channels_t *cc = mac->common_channels;
+    int16_t rrc_sdu_length;
+    int UE_id = -1;
+    uint16_t msg4_padding;
+    uint16_t msg4_post_padding;
+    uint16_t msg4_header;
 
-  uint8_t                         *vrb_map;
-  int                             first_rb;
-  int                             N_RB_DL;
-  nfapi_dl_config_request_pdu_t   *dl_config_pdu;
-  nfapi_ul_config_request_pdu_t   *ul_config_pdu;
-  nfapi_tx_request_pdu_t          *TX_req;
-  UE_list_t                       *UE_list=&eNB->UE_list;
-  nfapi_dl_config_request_body_t *dl_req;
-  nfapi_ul_config_request_body_t *ul_req;
-  uint8_t                         lcid;
-  uint8_t                         offset;
+    uint8_t *vrb_map;
+    int first_rb;
+    int N_RB_DL;
+    nfapi_dl_config_request_pdu_t *dl_config_pdu;
+    nfapi_ul_config_request_pdu_t *ul_config_pdu;
+    nfapi_tx_request_pdu_t *TX_req;
+    UE_list_t *UE_list = &mac->UE_list;
+    nfapi_dl_config_request_body_t *dl_req;
+    nfapi_ul_config_request_body_t *ul_req;
+    uint8_t lcid;
+    uint8_t offset;
 
 
 #ifdef Rel14
-  int rmax            = 0;
-  int rep             = 0; 
-  int reps            = 0;
-
-
-  first_rb        = 0;
-  struct PRACH_ConfigSIB_v1310 *ext4_prach;
-  struct PUCCH_ConfigCommon_v1310 *ext4_pucch;
-  PRACH_ParametersListCE_r13_t *prach_ParametersListCE_r13;
-  struct N1PUCCH_AN_InfoList_r13 *pucch_N1PUCCH_AN_InfoList_r13;
-  PRACH_ParametersCE_r13_t *p[4]={NULL,NULL,NULL,NULL};
-  int pucchreps[4]={1,1,1,1};
-  int n1pucchan[4]={0,0,0,0};
-
-  if (cc[CC_idP].radioResourceConfigCommon_BR) {
-
-    ext4_prach                    = cc[CC_idP].radioResourceConfigCommon_BR->ext4->prach_ConfigCommon_v1310;
-    ext4_pucch                    = cc[CC_idP].radioResourceConfigCommon_BR->ext4->pucch_ConfigCommon_v1310;
-    prach_ParametersListCE_r13    = &ext4_prach->prach_ParametersListCE_r13;
-    pucch_N1PUCCH_AN_InfoList_r13 = ext4_pucch->n1PUCCH_AN_InfoList_r13;
-    AssertFatal(prach_ParametersListCE_r13!=NULL,"prach_ParametersListCE_r13 is null\n");
-    AssertFatal(pucch_N1PUCCH_AN_InfoList_r13!=NULL,"pucch_N1PUCCH_AN_InfoList_r13 is null\n");
-    // check to verify CE-Level compatibility in SIB2_BR
-    AssertFatal(prach_ParametersListCE_r13->list.count == pucch_N1PUCCH_AN_InfoList_r13->list.count,
-		"prach_ParametersListCE_r13->list.count!= pucch_N1PUCCH_AN_InfoList_r13->list.count\n");
-
-    switch (prach_ParametersListCE_r13->list.count) {
-    case 4:
-      p[3]         = prach_ParametersListCE_r13->list.array[3];
-      n1pucchan[3] = *pucch_N1PUCCH_AN_InfoList_r13->list.array[3]; 
-      AssertFatal(ext4_pucch->pucch_NumRepetitionCE_Msg4_Level3_r13!=NULL,"pucch_NumRepetitionCE_Msg4_Level3 shouldn't be NULL\n");
-      pucchreps[3] = (int)(4<<*ext4_pucch->pucch_NumRepetitionCE_Msg4_Level3_r13);
-      
-    case 3:
-      p[2]         = prach_ParametersListCE_r13->list.array[2];
-      n1pucchan[2] = *pucch_N1PUCCH_AN_InfoList_r13->list.array[2]; 
-      AssertFatal(ext4_pucch->pucch_NumRepetitionCE_Msg4_Level2_r13!=NULL,"pucch_NumRepetitionCE_Msg4_Level2 shouldn't be NULL\n");
-      pucchreps[2] = (int)(4<<*ext4_pucch->pucch_NumRepetitionCE_Msg4_Level2_r13);
-    case 2:
-      p[1]         =prach_ParametersListCE_r13->list.array[1];
-      n1pucchan[1] = *pucch_N1PUCCH_AN_InfoList_r13->list.array[1]; 
-      AssertFatal(ext4_pucch->pucch_NumRepetitionCE_Msg4_Level2_r13!=NULL,"pucch_NumRepetitionCE_Msg4_Level1 shouldn't be NULL\n");
-      pucchreps[1] = (int)(1<<*ext4_pucch->pucch_NumRepetitionCE_Msg4_Level1_r13);
-    case 1:
-      p[0]         = prach_ParametersListCE_r13->list.array[0];
-      n1pucchan[0] = *pucch_N1PUCCH_AN_InfoList_r13->list.array[0]; 
-      AssertFatal(ext4_pucch->pucch_NumRepetitionCE_Msg4_Level2_r13!=NULL,"pucch_NumRepetitionCE_Msg4_Level0 shouldn't be NULL\n");
-      pucchreps[0] = (int)(1<<*ext4_pucch->pucch_NumRepetitionCE_Msg4_Level0_r13);
-    default:
-      AssertFatal(1==0,"Illegal count for prach_ParametersListCE_r13 %d\n",prach_ParametersListCE_r13->list.count);
+    int rmax = 0;
+    int rep = 0;
+    int reps = 0;
+
+
+    first_rb = 0;
+    struct PRACH_ConfigSIB_v1310 *ext4_prach;
+    struct PUCCH_ConfigCommon_v1310 *ext4_pucch;
+    PRACH_ParametersListCE_r13_t *prach_ParametersListCE_r13;
+    struct N1PUCCH_AN_InfoList_r13 *pucch_N1PUCCH_AN_InfoList_r13;
+    PRACH_ParametersCE_r13_t *p[4] = { NULL, NULL, NULL, NULL };
+    int pucchreps[4] = { 1, 1, 1, 1 };
+    int n1pucchan[4] = { 0, 0, 0, 0 };
+
+    if (cc[CC_idP].radioResourceConfigCommon_BR) {
+
+	ext4_prach =
+	    cc[CC_idP].radioResourceConfigCommon_BR->ext4->
+	    prach_ConfigCommon_v1310;
+	ext4_pucch =
+	    cc[CC_idP].radioResourceConfigCommon_BR->ext4->
+	    pucch_ConfigCommon_v1310;
+	prach_ParametersListCE_r13 =
+	    &ext4_prach->prach_ParametersListCE_r13;
+	pucch_N1PUCCH_AN_InfoList_r13 =
+	    ext4_pucch->n1PUCCH_AN_InfoList_r13;
+	AssertFatal(prach_ParametersListCE_r13 != NULL,
+		    "prach_ParametersListCE_r13 is null\n");
+	AssertFatal(pucch_N1PUCCH_AN_InfoList_r13 != NULL,
+		    "pucch_N1PUCCH_AN_InfoList_r13 is null\n");
+	// check to verify CE-Level compatibility in SIB2_BR
+	AssertFatal(prach_ParametersListCE_r13->list.count ==
+		    pucch_N1PUCCH_AN_InfoList_r13->list.count,
+		    "prach_ParametersListCE_r13->list.count!= pucch_N1PUCCH_AN_InfoList_r13->list.count\n");
+
+	switch (prach_ParametersListCE_r13->list.count) {
+	case 4:
+	    p[3] = prach_ParametersListCE_r13->list.array[3];
+	    n1pucchan[3] = *pucch_N1PUCCH_AN_InfoList_r13->list.array[3];
+	    AssertFatal(ext4_pucch->pucch_NumRepetitionCE_Msg4_Level3_r13
+			!= NULL,
+			"pucch_NumRepetitionCE_Msg4_Level3 shouldn't be NULL\n");
+	    pucchreps[3] =
+		(int) (4 << *ext4_pucch->
+		       pucch_NumRepetitionCE_Msg4_Level3_r13);
+
+	case 3:
+	    p[2] = prach_ParametersListCE_r13->list.array[2];
+	    n1pucchan[2] = *pucch_N1PUCCH_AN_InfoList_r13->list.array[2];
+	    AssertFatal(ext4_pucch->pucch_NumRepetitionCE_Msg4_Level2_r13
+			!= NULL,
+			"pucch_NumRepetitionCE_Msg4_Level2 shouldn't be NULL\n");
+	    pucchreps[2] =
+		(int) (4 << *ext4_pucch->
+		       pucch_NumRepetitionCE_Msg4_Level2_r13);
+	case 2:
+	    p[1] = prach_ParametersListCE_r13->list.array[1];
+	    n1pucchan[1] = *pucch_N1PUCCH_AN_InfoList_r13->list.array[1];
+	    AssertFatal(ext4_pucch->pucch_NumRepetitionCE_Msg4_Level2_r13
+			!= NULL,
+			"pucch_NumRepetitionCE_Msg4_Level1 shouldn't be NULL\n");
+	    pucchreps[1] =
+		(int) (1 << *ext4_pucch->
+		       pucch_NumRepetitionCE_Msg4_Level1_r13);
+	case 1:
+	    p[0] = prach_ParametersListCE_r13->list.array[0];
+	    n1pucchan[0] = *pucch_N1PUCCH_AN_InfoList_r13->list.array[0];
+	    AssertFatal(ext4_pucch->pucch_NumRepetitionCE_Msg4_Level2_r13
+			!= NULL,
+			"pucch_NumRepetitionCE_Msg4_Level0 shouldn't be NULL\n");
+	    pucchreps[0] =
+		(int) (1 << *ext4_pucch->
+		       pucch_NumRepetitionCE_Msg4_Level0_r13);
+	default:
+	    AssertFatal(1 == 0,
+			"Illegal count for prach_ParametersListCE_r13 %d\n",
+			prach_ParametersListCE_r13->list.count);
+	}
     }
-  }
 #endif
 
-  vrb_map       = cc[CC_idP].vrb_map;
-  
-  dl_req        = &eNB->DL_req[CC_idP].dl_config_request_body;
-  dl_config_pdu = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
-  N_RB_DL       = to_prb(cc[CC_idP].mib->message.dl_Bandwidth);
-
-  UE_id = find_UE_id(module_idP,RA_template->rnti);
-  AssertFatal(UE_id>=0,"Can't find UE for t-crnti\n");
-  
-  // set HARQ process round to 0 for this UE
-  
-  if (cc->tdd_Config) RA_template->harq_pid = ((frameP*10)+subframeP)%10;
-  else RA_template->harq_pid = ((frameP*10)+subframeP)&7;
-
-  
-  // Get RRCConnectionSetup for Piggyback
-  rrc_sdu_length = mac_rrc_data_req(module_idP,
-				    CC_idP,
-				    frameP,
-				    CCCH,
-				    1, // 1 transport block
-				    &cc[CC_idP].CCCH_pdu.payload[0],
-				    ENB_FLAG_YES,
-				    module_idP,
-				    0); // not used in this case
-  
-  AssertFatal(rrc_sdu_length>0,
-	      "[MAC][eNB Scheduler] CCCH not allocated\n");
-  
-  
-  LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d, subframeP %d: UE_id %d, rrc_sdu_length %d\n",
-	module_idP, CC_idP, frameP, subframeP,UE_id, rrc_sdu_length);
-  
-  
+    vrb_map = cc[CC_idP].vrb_map;
+
+    dl_req = &mac->DL_req[CC_idP].dl_config_request_body;
+    dl_config_pdu = &dl_req->dl_config_pdu_list[dl_req->number_pdu];
+    N_RB_DL = to_prb(cc[CC_idP].mib->message.dl_Bandwidth);
+
+    UE_id = find_UE_id(module_idP, ra->rnti);
+    AssertFatal(UE_id >= 0, "Can't find UE for t-crnti\n");
+
+    // set HARQ process round to 0 for this UE
+
+    if (cc->tdd_Config)
+	ra->harq_pid = ((frameP * 10) + subframeP) % 10;
+    else
+	ra->harq_pid = ((frameP * 10) + subframeP) & 7;
+
+
+    // Get RRCConnectionSetup for Piggyback
+    rrc_sdu_length = mac_rrc_data_req(module_idP, CC_idP, frameP, CCCH, 1,	// 1 transport block
+				      &cc[CC_idP].CCCH_pdu.payload[0], ENB_FLAG_YES, module_idP, 0);	// not used in this case
+
+    AssertFatal(rrc_sdu_length > 0,
+		"[MAC][eNB Scheduler] CCCH not allocated\n");
+
+
+    LOG_D(MAC,
+	  "[eNB %d][RAPROC] CC_id %d Frame %d, subframeP %d: UE_id %d, rrc_sdu_length %d\n",
+	  module_idP, CC_idP, frameP, subframeP, UE_id, rrc_sdu_length);
+
+
 #ifdef Rel14
-  if (RA_template->rach_resource_type>0) {
-    
-    // Generate DCI + repetitions first
-    // This uses an MPDCCH Type 2 allocation according to Section 9.1.5 36-213, Type2 common allocation according to Table 7.1-8 (36-213)
-    // Parameters:
-    //    p=2+4 PRB set (number of PRB pairs 6)
-    //    rmax = mpdcch-NumRepetition-RA-r13 => Table 9.1.5-3
-    //    if CELevel = 0,1 => Table 9.1.5-1b for MPDCCH candidates
-    //    if CELevel = 2,3 => Table 9.1.5-2b for MPDCCH candidates
-    //    distributed transmission
-    
-    // rmax from SIB2 information
-    rmax           = p[RA_template->rach_resource_type-1]->mpdcch_NumRepetition_RA_r13;
-    AssertFatal(rmax>=4,"choose rmax>=4 for enough repeititions, or reduce rep to 1 or 2\n");
-
-    // choose r3 by default for Msg4 (this is ok from table 9.1.5-3 for rmax = >=4, if we choose rmax <4 it has to be less
-    rep            = 2; 
-    // get actual repetition count from Table 9.1.5-3
-    reps           = (rmax<=8)?(1<<rep):(rmax>>(3-rep));
-    // get first narrowband
-    first_rb = narrowband_to_first_rb(&cc[CC_idP],RA_template->msg34_narrowband);
-
-    if ((RA_template->msg4_mpdcch_repetition_cnt == 0) &&
-    (mpdcch_sf_condition(eNB,CC_idP,frameP,subframeP,rmax,TYPE2,-1)>0)){
-      // MPDCCH configuration for RAR
-      
-      memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-      dl_config_pdu->pdu_type                                                                  = NFAPI_DL_CONFIG_MPDCCH_PDU_TYPE; 
-      dl_config_pdu->pdu_size                                                                  = (uint8_t)(2+sizeof(nfapi_dl_config_mpdcch_pdu));
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.dci_format                                    = (RA_template->rach_resource_type > 1) ? 11 : 10;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.mpdcch_narrow_band                            = RA_template->msg34_narrowband;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.number_of_prb_pairs                           = 6;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.resource_block_assignment                     = 0; // Note: this can be dynamic
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.mpdcch_tansmission_type                       = 1;
-      AssertFatal(cc[CC_idP].sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13!=NULL,
-		  "cc[CC_idP].sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13 is null\n");
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.start_symbol                                  = cc[CC_idP].sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->startSymbolBR_r13;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.ecce_index                                    = 0;  // Note: this should be dynamic
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.aggregation_level                             = 16; // OK for CEModeA r1-3 (9.1.5-1b) or CEModeB r1-4
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.rnti_type                                     = 0;  // t-C-RNTI
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.rnti                                          = RA_template->RA_rnti;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.ce_mode                                       = (RA_template->rach_resource_type < 3) ? 1 : 2;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.drms_scrambling_init                          = cc[CC_idP].physCellId;  /// Check this is still N_id_cell for type2 common
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.initial_transmission_sf_io                    = (frameP*10)+subframeP;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.transmission_power                            = 6000; // 0dB
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.resource_block_coding                         = getRIV(6,0,6);// check if not getRIV(N_RB_DL,first_rb,6);
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.mcs                                           = 4; // adjust according to size of Msg4, 208 bits with N1A_PRB=3
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.pdsch_reptition_levels                        = 4; // fix to 4 for now
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.redundancy_version                            = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.new_data_indicator                            = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.harq_process                                  = RA_template->harq_pid;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.tpmi_length                                   = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.tpmi                                          = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.pmi_flag                                      = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.pmi                                           = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.harq_resource_offset                          = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.dci_subframe_repetition_number                = rep; 
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.tpc                                           = 1;// N1A_PRB=3; => 208 bits
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.downlink_assignment_index_length              = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.downlink_assignment_index                     = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.allocate_prach_flag                           = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.preamble_index                                = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.prach_mask_index                              = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.starting_ce_level                             = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.srs_request                                   = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.antenna_ports_and_scrambling_identity_flag    = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.antenna_ports_and_scrambling_identity         = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.frequency_hopping_enabled_flag                = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.paging_direct_indication_differentiation_flag = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.direct_indication                             = 0;
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.total_dci_length_including_padding            = 0; // this is not needed by OAI L1, but should be filled in
-      dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.number_of_tx_antenna_ports                    = 1;
-      RA_template->msg4_mpdcch_repetition_cnt++;
-      dl_req->number_pdu++;
-      
-    } //repetition_count==0 && SF condition met
-    else if (RA_template->msg4_mpdcch_repetition_cnt>0) { // we're in a stream of repetitions
-      RA_template->msg4_mpdcch_repetition_cnt++;	      
-      if (RA_template->msg4_mpdcch_repetition_cnt==reps) { // this is the last mpdcch repetition
-	if (cc[CC_idP].tdd_Config==NULL) { // FDD case
-	  // wait 2 subframes for PDSCH transmission
-	  if (subframeP>7) RA_template->Msg4_frame = (frameP+1)&1023;
-	  else             RA_template->Msg4_frame = frameP;
-	  RA_template->Msg4_subframe               = (subframeP+2)%10; 
-	}
-	else {
-	  AssertFatal(1==0,"TDD case not done yet\n");
-	}
-      } // mpdcch_repetition_count == reps
-      if ((RA_template->Msg4_frame == frameP) && (RA_template->Msg4_subframe == subframeP)) {
-	
-	// Program PDSCH
-	
-	LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d, subframeP %d: Generating Msg4 BR with RRC Piggyback (ce_level %d RNTI %x)\n",
-	      module_idP, CC_idP, frameP, subframeP,RA_template->rach_resource_type-1,RA_template->rnti);
-
-	AssertFatal(1==0,"Msg4 generation not finished for BL/CE UE\n");
-	dl_config_pdu                                                                  = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
-	memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-	dl_config_pdu->pdu_type                                                        = NFAPI_DL_CONFIG_DLSCH_PDU_TYPE; 
-	dl_config_pdu->pdu_size                                                        = (uint8_t)(2+sizeof(nfapi_dl_config_dlsch_pdu));
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index                              = eNB->pdu_index[CC_idP];
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti                                   = RA_template->rnti;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type               = 2;   // format 1A/1B/1D
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = 0;   // localized
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_block_coding                  = getRIV(N_RB_DL,first_rb,6);  // check that this isn't getRIV(6,0,6)
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation                             = 2; //QPSK
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.redundancy_version                     = 0;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks                       = 1;// first block
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_block_to_codeword_swap_flag  = 0;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_scheme                    = (cc->p_eNB==1 ) ? 0 : 1;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_layers                       = 1;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_subbands                     = 1;
-	//	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = ;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ue_category_capacity                   = 1;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa                                     = 4; // 0 dB
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.delta_power_offset_index               = 0;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap                                   = 0;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb                                   = get_subbandsize(cc->mib->message.dl_Bandwidth); // ignored
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_mode                      = (cc->p_eNB==1 ) ? 1 : 2;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_prb_per_subband                 = 1;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector                          = 1;
-	//	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.bf_vector                    = ; 
-
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel10.pdsch_start                           = cc[CC_idP].sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->startSymbolBR_r13;
-	
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.ue_type                               = (RA_template->rach_resource_type < 3) ? 1 : 2;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.pdsch_payload_type                    = 2;  // not SI message
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.initial_transmission_sf_io            = (10*frameP)+subframeP;
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.drms_table_flag                       = 0;
-	dl_req->number_pdu++;
-	
-	RA_template->generate_Msg4=0;
-	RA_template->wait_ack_Msg4=1;
-
-	lcid=0;
-	
-	UE_list->UE_sched_ctrl[UE_id].round[CC_idP][RA_template->harq_pid] = 0;
-	msg4_header = 1+6+1;  // CR header, CR CE, SDU header
-	
-	if ((RA_template->msg4_TBsize - rrc_sdu_length - msg4_header) <= 2) {
-	  msg4_padding = RA_template->msg4_TBsize - rrc_sdu_length - msg4_header;
-	  msg4_post_padding = 0;
-	} else {
-	  msg4_padding = 0;
-	  msg4_post_padding = RA_template->msg4_TBsize - rrc_sdu_length - msg4_header -1;
-	}
-	
-	LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d subframeP %d Msg4 : TBS %d, sdu_len %d, msg4_header %d, msg4_padding %d, msg4_post_padding %d\n",
-	      module_idP,CC_idP,frameP,subframeP,RA_template->msg4_TBsize,rrc_sdu_length,msg4_header,msg4_padding,msg4_post_padding);
-	DevAssert( UE_id != UE_INDEX_INVALID ); // FIXME not sure how to gracefully return
-	// CHECK THIS: &cc[CC_idP].CCCH_pdu.payload[0]
-	offset = generate_dlsch_header((unsigned char*)eNB->UE_list.DLSCH_pdu[CC_idP][0][(unsigned char)UE_id].payload[0],
-				       1,                           //num_sdus
-				       (unsigned short*)&rrc_sdu_length,             //
-				       &lcid,                       // sdu_lcid
-				       255,                         // no drx
-				       31,                          // no timing advance
-				       RA_template->cont_res_id,    // contention res id
-				       msg4_padding,                // no padding
-				       msg4_post_padding);
-	
-	memcpy((void*)&eNB->UE_list.DLSCH_pdu[CC_idP][0][(unsigned char)UE_id].payload[0][(unsigned char)offset],
-	       &cc[CC_idP].CCCH_pdu.payload[0],
-	       rrc_sdu_length);
-	
-	// DL request
-	eNB->TX_req[CC_idP].sfn_sf                                             = (frameP<<4)+subframeP;
-	TX_req                                                                = &eNB->TX_req[CC_idP].tx_request_body.tx_pdu_list[eNB->TX_req[CC_idP].tx_request_body.number_of_pdus]; 		     	      
-	TX_req->pdu_length                                                    = rrc_sdu_length;
-	TX_req->pdu_index                                                     = eNB->pdu_index[CC_idP]++;
-	TX_req->num_segments                                                  = 1;
-	TX_req->segments[0].segment_length                                    = rrc_sdu_length;
-	TX_req->segments[0].segment_data                                      = eNB->UE_list.DLSCH_pdu[CC_idP][0][(unsigned char)UE_id].payload[0];
-	eNB->TX_req[CC_idP].tx_request_body.number_of_pdus++;
-
-	// Program ACK/NAK for Msg4 PDSCH
-	int absSF = (RA_template->Msg3_frame*10)+RA_template->Msg3_subframe;
-	// see Section 10.2 from 36.213
-	int ackNAK_absSF = absSF + reps + 4;
-	AssertFatal(reps>2,"Have to handle programming of ACK when PDSCH repetitions is > 2\n");
-	ul_req        = &eNB->UL_req_tmp[CC_idP][ackNAK_absSF%10].ul_config_request_body;
-	ul_config_pdu = &ul_req->ul_config_pdu_list[ul_req->number_of_pdus]; 
-
-	ul_config_pdu->pdu_type                                                                     = NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE; 
-	ul_config_pdu->pdu_size                                                                     = (uint8_t)(2+sizeof(nfapi_ul_config_uci_harq_pdu));
-	ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.handle                       = 0; // don't know how to use this
-	ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.rnti                         = RA_template->rnti;
-	ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel13.ue_type                     = (RA_template->rach_resource_type < 3) ? 1 : 2;
-	ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel13.empty_symbols               = 0;
-	ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel13.total_number_of_repetitions = pucchreps[RA_template->rach_resource_type-1];
-	ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel13.repetition_number           = 0;
-	// Note need to keep sending this across reptitions!!!! Not really for PUCCH, to ask small-cell forum, we'll see for the other messages, maybe parameters change across repetitions and FAPI has to provide for that
-	if (cc[CC_idP].tdd_Config==NULL) { // FDD case
-	  ul_config_pdu->uci_harq_pdu.harq_information.harq_information_rel8_fdd.n_pucch_1_0   = n1pucchan[RA_template->rach_resource_type-1];
-	  // NOTE: How to fill in the rest of the n_pucch_1_0 information 213 Section 10.1.2.1 in the general case
-	  // = N_ECCE_q + Delta_ARO + n1pucchan[ce_level]
-	  // higher in the MPDCCH configuration, N_ECCE_q is hard-coded to 0, and harq resource offset to 0 =>
-	  // Delta_ARO = 0 from Table 10.1.2.1-1
-	  ul_config_pdu->uci_harq_pdu.harq_information.harq_information_rel8_fdd.harq_size     = 1;  // 1-bit ACK/NAK
-	}
-	else {
-	  AssertFatal(1==0,"PUCCH configuration for ACK/NAK not handled yet for TDD BL/CE case\n");
-	}
-	ul_req->number_of_pdus++;
-	T(T_ENB_MAC_UE_DL_PDU_WITH_DATA, T_INT(module_idP), T_INT(CC_idP), T_INT(RA_template->rnti), T_INT(frameP), T_INT(subframeP),
-	  T_INT(0 /*harq_pid always 0?*/), T_BUFFER(&eNB->UE_list.DLSCH_pdu[CC_idP][0][UE_id].payload[0], RA_template->msg4_TBsize));
-	
-	if (opt_enabled==1) {
-	  trace_pdu(1, (uint8_t *)eNB->UE_list.DLSCH_pdu[CC_idP][0][(unsigned char)UE_id].payload[0],
-		    rrc_sdu_length, UE_id, 3, UE_RNTI(module_idP, UE_id),
-		    eNB->frame, eNB->subframe,0,0);
-	  LOG_D(OPT,"[eNB %d][DLSCH] CC_id %d Frame %d trace pdu for rnti %x with size %d\n",
-		module_idP, CC_idP, frameP, UE_RNTI(module_idP,UE_id), rrc_sdu_length);
-	}
-      } // Msg4 frame/subframe
-    } // msg4_mpdcch_repetition_count
-  } // rach_resource_type > 0 
-  else
-#endif
-    { // This is normal LTE case
-      if ((RA_template->Msg4_frame == frameP) && (RA_template->Msg4_subframe == subframeP)) {	      
-	LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d, subframeP %d: Generating Msg4 with RRC Piggyback (RNTI %x)\n",
-	      module_idP, CC_idP, frameP, subframeP,RA_template->rnti);
-	
-	/// Choose first 4 RBs for Msg4, should really check that these are free!
-	first_rb=0;
-	
-	vrb_map[first_rb] = 1;
-	vrb_map[first_rb+1] = 1;
-	vrb_map[first_rb+2] = 1;
-	vrb_map[first_rb+3] = 1;
-	
-	
-	// Compute MCS/TBS for 3 PRB (coded on 4 vrb)
-	msg4_header = 1+6+1;  // CR header, CR CE, SDU header
-	
-	if ((rrc_sdu_length+msg4_header) <= 22) {
-	  RA_template->msg4_mcs                       = 4;
-	  RA_template->msg4_TBsize = 22;
-	} else if ((rrc_sdu_length+msg4_header) <= 28) {
-	  RA_template->msg4_mcs                       = 5;
-	  RA_template->msg4_TBsize = 28;
-	} else if ((rrc_sdu_length+msg4_header) <= 32) {
-	  RA_template->msg4_mcs                       = 6;
-	  RA_template->msg4_TBsize = 32;
-	} else if ((rrc_sdu_length+msg4_header) <= 41) {
-	  RA_template->msg4_mcs                       = 7;
-	  RA_template->msg4_TBsize = 41;
-	} else if ((rrc_sdu_length+msg4_header) <= 49) {
-	  RA_template->msg4_mcs                       = 8;
-	  RA_template->msg4_TBsize = 49;
-	} else if ((rrc_sdu_length+msg4_header) <= 57) {
-	  RA_template->msg4_mcs    = 9;
-	  RA_template->msg4_TBsize = 57;
-	}
+    if (ra->rach_resource_type > 0) {
+
+	// Generate DCI + repetitions first
+	// This uses an MPDCCH Type 2 allocation according to Section 9.1.5 36-213, Type2 common allocation according to Table 7.1-8 (36-213)
+	// Parameters:
+	//    p=2+4 PRB set (number of PRB pairs 6)
+	//    rmax = mpdcch-NumRepetition-RA-r13 => Table 9.1.5-3
+	//    if CELevel = 0,1 => Table 9.1.5-1b for MPDCCH candidates
+	//    if CELevel = 2,3 => Table 9.1.5-2b for MPDCCH candidates
+	//    distributed transmission
+
+	// rmax from SIB2 information
+	rmax = p[ra->rach_resource_type - 1]->mpdcch_NumRepetition_RA_r13;
+	AssertFatal(rmax >= 4,
+		    "choose rmax>=4 for enough repeititions, or reduce rep to 1 or 2\n");
+
+	// choose r3 by default for Msg4 (this is ok from table 9.1.5-3 for rmax = >=4, if we choose rmax <4 it has to be less
+	rep = 2;
+	// get actual repetition count from Table 9.1.5-3
+	reps = (rmax <= 8) ? (1 << rep) : (rmax >> (3 - rep));
+	// get first narrowband
+	first_rb =
+	    narrowband_to_first_rb(&cc[CC_idP], ra->msg34_narrowband);
+
+	if ((ra->msg4_mpdcch_repetition_cnt == 0) &&
+	    (mpdcch_sf_condition
+	     (mac, CC_idP, frameP, subframeP, rmax, TYPE2, -1) > 0)) {
+	    // MPDCCH configuration for RAR
+
+	    memset((void *) dl_config_pdu, 0,
+		   sizeof(nfapi_dl_config_request_pdu_t));
+	    dl_config_pdu->pdu_type = NFAPI_DL_CONFIG_MPDCCH_PDU_TYPE;
+	    dl_config_pdu->pdu_size =
+		(uint8_t) (2 + sizeof(nfapi_dl_config_mpdcch_pdu));
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.dci_format =
+		(ra->rach_resource_type > 1) ? 11 : 10;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.mpdcch_narrow_band =
+		ra->msg34_narrowband;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.
+		number_of_prb_pairs = 6;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.resource_block_assignment = 0;	// Note: this can be dynamic
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.
+		mpdcch_tansmission_type = 1;
+	    AssertFatal(cc[CC_idP].
+			sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13
+			!= NULL,
+			"cc[CC_idP].sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13 is null\n");
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.start_symbol =
+		cc[CC_idP].sib1_v13ext->
+		bandwidthReducedAccessRelatedInfo_r13->startSymbolBR_r13;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.ecce_index = 0;	// Note: this should be dynamic
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.aggregation_level = 16;	// OK for CEModeA r1-3 (9.1.5-1b) or CEModeB r1-4
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.rnti_type = 0;	// t-C-RNTI
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.rnti = ra->RA_rnti;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.ce_mode =
+		(ra->rach_resource_type < 3) ? 1 : 2;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.drms_scrambling_init = cc[CC_idP].physCellId;	/// Check this is still N_id_cell for type2 common
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.
+		initial_transmission_sf_io = (frameP * 10) + subframeP;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.transmission_power = 6000;	// 0dB
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.resource_block_coding = getRIV(6, 0, 6);	// check if not getRIV(N_RB_DL,first_rb,6);
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.mcs = 4;	// adjust according to size of Msg4, 208 bits with N1A_PRB=3
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.pdsch_reptition_levels = 4;	// fix to 4 for now
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.redundancy_version =
+		0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.new_data_indicator =
+		0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.harq_process =
+		ra->harq_pid;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.tpmi_length = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.tpmi = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.pmi_flag = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.pmi = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.
+		harq_resource_offset = 0;
+	    dl_config_pdu->mpdcch_pdu.
+		mpdcch_pdu_rel13.dci_subframe_repetition_number = rep;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.tpc = 1;	// N1A_PRB=3; => 208 bits
+	    dl_config_pdu->mpdcch_pdu.
+		mpdcch_pdu_rel13.downlink_assignment_index_length = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.
+		downlink_assignment_index = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.
+		allocate_prach_flag = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.preamble_index = 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.prach_mask_index =
+		0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.starting_ce_level =
+		0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.srs_request = 0;
+	    dl_config_pdu->mpdcch_pdu.
+		mpdcch_pdu_rel13.antenna_ports_and_scrambling_identity_flag
+		= 0;
+	    dl_config_pdu->mpdcch_pdu.
+		mpdcch_pdu_rel13.antenna_ports_and_scrambling_identity = 0;
+	    dl_config_pdu->mpdcch_pdu.
+		mpdcch_pdu_rel13.frequency_hopping_enabled_flag = 0;
+	    dl_config_pdu->mpdcch_pdu.
+		mpdcch_pdu_rel13.paging_direct_indication_differentiation_flag
+		= 0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.direct_indication =
+		0;
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.total_dci_length_including_padding = 0;	// this is not needed by OAI L1, but should be filled in
+	    dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.
+		number_of_tx_antenna_ports = 1;
+	    ra->msg4_mpdcch_repetition_cnt++;
+	    dl_req->number_pdu++;
 
-	fill_nfapi_dl_dci_1A(dl_config_pdu,
-			     4,                           // aggregation_level
-			     RA_template->rnti,           // rnti
-			     1,                           // rnti_type, CRNTI
-			     RA_template->harq_pid,       // harq_process
-			     1,                           // tpc, none
-			     getRIV(N_RB_DL,first_rb,4),  // resource_block_coding
-			     RA_template->msg4_mcs,       // mcs
-			     1,                           // ndi
-			     0,                           // rv
-			     0);                          // vrb_flag
-    LOG_D(MAC,"Frame %d, subframe %d: Msg4 DCI pdu_num %d (rnti %x,rnti_type %d,harq_pid %d, resource_block_coding (%p) %d\n",
-		  frameP,
-	      subframeP,
-          dl_req->number_pdu,
-          dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti,
-          dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type,
-          dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.harq_process,
-          &dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_block_coding,
-          dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_block_coding);
-    AssertFatal(dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_block_coding < 8192,
-				"resource_block_coding %u < 8192\n",
-                dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_block_coding);	
-	if (!CCE_allocation_infeasible(module_idP,CC_idP,1,
-				       subframeP,dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
-				       RA_template->rnti)) {
-	  dl_req->number_dci++;
-	  dl_req->number_pdu++;
-	  
-	  RA_template->generate_Msg4=0;
-	  RA_template->wait_ack_Msg4=1;
-	  
-	  // increment Absolute subframe by 8 for Msg4 retransmission
-	  LOG_D(MAC,"Frame %d, Subframe %d: Preparing for Msg4 retransmission currently %d.%d\n",
-		frameP,subframeP,RA_template->Msg4_frame,RA_template->Msg4_subframe);
-	  if (RA_template->Msg4_subframe > 1) RA_template->Msg4_frame++;
-	  RA_template->Msg4_frame&=1023;
-	  RA_template->Msg4_subframe = (RA_template->Msg4_subframe+8)%10;
-	  LOG_D(MAC,"Frame %d, Subframe %d: Msg4 retransmission in %d.%d\n",
-		frameP,subframeP,RA_template->Msg4_frame,RA_template->Msg4_subframe);
-	  lcid=0;
-	  
-	  // put HARQ process round to 0
-	  if (cc->tdd_Config) RA_template->harq_pid = ((frameP*10)+subframeP)%10;
-	  else RA_template->harq_pid = ((frameP*10)+subframeP)&7;
-	  UE_list->UE_sched_ctrl[UE_id].round[CC_idP][RA_template->harq_pid] = 0;
-	  
-	  if ((RA_template->msg4_TBsize - rrc_sdu_length - msg4_header) <= 2) {
-	    msg4_padding = RA_template->msg4_TBsize - rrc_sdu_length - msg4_header;
-	    msg4_post_padding = 0;
-	  } else {
-	    msg4_padding = 0;
-	    msg4_post_padding = RA_template->msg4_TBsize - rrc_sdu_length - msg4_header -1;
-	  }
-	  
-	  LOG_D(MAC,"[eNB %d][RAPROC] CC_idP %d Frame %d subframeP %d Msg4 : TBS %d, sdu_len %d, msg4_header %d, msg4_padding %d, msg4_post_padding %d\n",
-		module_idP,CC_idP,frameP,subframeP,RA_template->msg4_TBsize,rrc_sdu_length,msg4_header,msg4_padding,msg4_post_padding);
-	  DevAssert( UE_id != UE_INDEX_INVALID ); // FIXME not sure how to gracefully return
-	  // CHECK THIS: &cc[CC_idP].CCCH_pdu.payload[0]
-	  offset = generate_dlsch_header((unsigned char*)eNB->UE_list.DLSCH_pdu[CC_idP][0][(unsigned char)UE_id].payload[0],
-					 1,                           //num_sdus
-					 (unsigned short*)&rrc_sdu_length,             //
-					 &lcid,                       // sdu_lcid
-					 255,                         // no drx
-					 31,                          // no timing advance
-					 RA_template->cont_res_id,    // contention res id
-					 msg4_padding,                // no padding
-					 msg4_post_padding);
-	  
-	  memcpy((void*)&eNB->UE_list.DLSCH_pdu[CC_idP][0][(unsigned char)UE_id].payload[0][(unsigned char)offset],
-		 &cc[CC_idP].CCCH_pdu.payload[0],
-		 rrc_sdu_length);
-	  
-	  // DLSCH Config
-	  fill_nfapi_dlsch_config(eNB,
-				  dl_req,
-				  RA_template->msg4_TBsize,
-				  eNB->pdu_index[CC_idP],
-				  RA_template->rnti,
-				  2,                           // resource_allocation_type : format 1A/1B/1D
-				  0,                           // virtual_resource_block_assignment_flag : localized
-				  getRIV(N_RB_DL,first_rb,4),  // resource_block_coding : RIV, 4 PRB
-				  2,                           // modulation: QPSK
-				  0,                           // redundancy version
-				  1,                           // transport_blocks
-				  0,                           // transport_block_to_codeword_swap_flag (0)
-				  (cc->p_eNB==1 ) ? 0 : 1,     // transmission_scheme
-				  1,                           // number of layers
-				  1,                           // number of subbands
-				  //0,                         // codebook index 
-				  1,                           // ue_category_capacity
-				  4,                           // pa: 0 dB
-				  0,                           // delta_power_offset_index
-				  0,                           // ngap
-				  1,                           // NPRB = 3 like in DCI
-				  (cc->p_eNB==1 ) ? 1 : 2,     // transmission mode
-				  1,                           // num_bf_prb_per_subband
-				  1);                          // num_bf_vector
-      LOG_D(MAC,"Filled DLSCH config, pdu number %d, non-dci pdu_index %d\n",dl_req->number_pdu,eNB->pdu_index[CC_idP]);
-
-	  // DL request
-	  eNB->TX_req[CC_idP].sfn_sf = fill_nfapi_tx_req(&eNB->TX_req[CC_idP].tx_request_body,
-							 (frameP*10)+subframeP,
-							 rrc_sdu_length,
-							 eNB->pdu_index[CC_idP],
-							 eNB->UE_list.DLSCH_pdu[CC_idP][0][(unsigned char)UE_id].payload[0]); 
-	  eNB->pdu_index[CC_idP]++;
-
-	  LOG_D(MAC,"Filling UCI ACK/NAK information, cce_idx %d\n",dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.cce_idx);
-	  // Program PUCCH1a for ACK/NAK
-	  // Program ACK/NAK for Msg4 PDSCH
-	  fill_nfapi_uci_acknak(module_idP,
-				CC_idP,
-				RA_template->rnti,
-				(frameP*10)+subframeP,
-				dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.cce_idx);
-
-	  
-	  T(T_ENB_MAC_UE_DL_PDU_WITH_DATA, T_INT(module_idP), T_INT(CC_idP), T_INT(RA_template->rnti), T_INT(frameP), T_INT(subframeP),
-	    T_INT(0 /*harq_pid always 0?*/), T_BUFFER(&eNB->UE_list.DLSCH_pdu[CC_idP][0][UE_id].payload[0], RA_template->msg4_TBsize));
-	  
-	  if (opt_enabled==1) {
-	    trace_pdu(1, (uint8_t *)eNB->UE_list.DLSCH_pdu[CC_idP][0][(unsigned char)UE_id].payload[0],
-		      rrc_sdu_length, UE_id, 3, UE_RNTI(module_idP, UE_id),
-		      eNB->frame, eNB->subframe,0,0);
-	    LOG_D(OPT,"[eNB %d][DLSCH] CC_id %d Frame %d trace pdu for rnti %x with size %d\n",
-		  module_idP, CC_idP, frameP, UE_RNTI(module_idP,UE_id), rrc_sdu_length);
-	  }
-	  
-	} // CCE Allocation feasible
-      } // msg4 frame/subframe
-    } // else rach_resource_type
+	}			//repetition_count==0 && SF condition met
+	else if (ra->msg4_mpdcch_repetition_cnt > 0) {	// we're in a stream of repetitions
+	    ra->msg4_mpdcch_repetition_cnt++;
+	    if (ra->msg4_mpdcch_repetition_cnt == reps) {	// this is the last mpdcch repetition
+		if (cc[CC_idP].tdd_Config == NULL) {	// FDD case
+		    // wait 2 subframes for PDSCH transmission
+		    if (subframeP > 7)
+			ra->Msg4_frame = (frameP + 1) & 1023;
+		    else
+			ra->Msg4_frame = frameP;
+		    ra->Msg4_subframe = (subframeP + 2) % 10;
+		} else {
+		    AssertFatal(1 == 0, "TDD case not done yet\n");
+		}
+	    }			// mpdcch_repetition_count == reps
+	    if ((ra->Msg4_frame == frameP)
+		&& (ra->Msg4_subframe == subframeP)) {
+
+		// Program PDSCH
+
+		LOG_D(MAC,
+		      "[eNB %d][RAPROC] CC_id %d Frame %d, subframeP %d: Generating Msg4 BR with RRC Piggyback (ce_level %d RNTI %x)\n",
+		      module_idP, CC_idP, frameP, subframeP,
+		      ra->rach_resource_type - 1, ra->rnti);
+
+		AssertFatal(1 == 0,
+			    "Msg4 generation not finished for BL/CE UE\n");
+		dl_config_pdu =
+		    &dl_req->dl_config_pdu_list[dl_req->number_pdu];
+		memset((void *) dl_config_pdu, 0,
+		       sizeof(nfapi_dl_config_request_pdu_t));
+		dl_config_pdu->pdu_type = NFAPI_DL_CONFIG_DLSCH_PDU_TYPE;
+		dl_config_pdu->pdu_size =
+		    (uint8_t) (2 + sizeof(nfapi_dl_config_dlsch_pdu));
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index =
+		    mac->pdu_index[CC_idP];
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti = ra->rnti;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type = 2;	// format 1A/1B/1D
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = 0;	// localized
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_block_coding = getRIV(N_RB_DL, first_rb, 6);	// check that this isn't getRIV(6,0,6)
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation = 2;	//QPSK
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    redundancy_version = 0;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks = 1;	// first block
+		dl_config_pdu->dlsch_pdu.
+		    dlsch_pdu_rel8.transport_block_to_codeword_swap_flag =
+		    0;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    transmission_scheme = (cc->p_eNB == 1) ? 0 : 1;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_layers =
+		    1;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    number_of_subbands = 1;
+		//      dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = ;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    ue_category_capacity = 1;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa = 4;	// 0 dB
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    delta_power_offset_index = 0;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap = 0;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb = get_subbandsize(cc->mib->message.dl_Bandwidth);	// ignored
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_mode =
+		    (cc->p_eNB == 1) ? 1 : 2;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+		    num_bf_prb_per_subband = 1;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector = 1;
+		//      dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.bf_vector                    = ; 
+
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel10.pdsch_start =
+		    cc[CC_idP].
+		    sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->
+		    startSymbolBR_r13;
+
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.ue_type =
+		    (ra->rach_resource_type < 3) ? 1 : 2;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.pdsch_payload_type = 2;	// not SI message
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.
+		    initial_transmission_sf_io = (10 * frameP) + subframeP;
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.drms_table_flag =
+		    0;
+		dl_req->number_pdu++;
+
+		ra->state = WAITMSG4ACK;
+
+		lcid = 0;
+
+		UE_list->UE_sched_ctrl[UE_id].round[CC_idP][ra->harq_pid] =
+		    0;
+		msg4_header = 1 + 6 + 1;	// CR header, CR CE, SDU header
+
+		if ((ra->msg4_TBsize - rrc_sdu_length - msg4_header) <= 2) {
+		    msg4_padding =
+			ra->msg4_TBsize - rrc_sdu_length - msg4_header;
+		    msg4_post_padding = 0;
+		} else {
+		    msg4_padding = 0;
+		    msg4_post_padding =
+			ra->msg4_TBsize - rrc_sdu_length - msg4_header - 1;
+		}
+
+		LOG_D(MAC,
+		      "[eNB %d][RAPROC] CC_id %d Frame %d subframeP %d Msg4 : TBS %d, sdu_len %d, msg4_header %d, msg4_padding %d, msg4_post_padding %d\n",
+		      module_idP, CC_idP, frameP, subframeP,
+		      ra->msg4_TBsize, rrc_sdu_length, msg4_header,
+		      msg4_padding, msg4_post_padding);
+		DevAssert(UE_id != UE_INDEX_INVALID);	// FIXME not sure how to gracefully return
+		// CHECK THIS: &cc[CC_idP].CCCH_pdu.payload[0]
+		offset = generate_dlsch_header((unsigned char *) mac->UE_list.DLSCH_pdu[CC_idP][0][(unsigned char) UE_id].payload[0], 1,	//num_sdus
+					       (unsigned short *) &rrc_sdu_length,	//
+					       &lcid,	// sdu_lcid
+					       255,	// no drx
+					       31,	// no timing advance
+					       ra->cont_res_id,	// contention res id
+					       msg4_padding,	// no padding
+					       msg4_post_padding);
+
+		memcpy((void *) &mac->UE_list.
+		       DLSCH_pdu[CC_idP][0][(unsigned char)
+					    UE_id].payload[0][(unsigned
+							       char)
+							      offset],
+		       &cc[CC_idP].CCCH_pdu.payload[0], rrc_sdu_length);
+
+		// DL request
+		mac->TX_req[CC_idP].sfn_sf = (frameP << 4) + subframeP;
+		TX_req =
+		    &mac->TX_req[CC_idP].tx_request_body.tx_pdu_list[mac->
+								     TX_req
+								     [CC_idP].
+								     tx_request_body.number_of_pdus];
+		TX_req->pdu_length = rrc_sdu_length;
+		TX_req->pdu_index = mac->pdu_index[CC_idP]++;
+		TX_req->num_segments = 1;
+		TX_req->segments[0].segment_length = rrc_sdu_length;
+		TX_req->segments[0].segment_data =
+		    mac->UE_list.
+		    DLSCH_pdu[CC_idP][0][(unsigned char) UE_id].payload[0];
+		mac->TX_req[CC_idP].tx_request_body.number_of_pdus++;
+
+		// Program ACK/NAK for Msg4 PDSCH
+		int absSF = (ra->Msg3_frame * 10) + ra->Msg3_subframe;
+		// see Section 10.2 from 36.213
+		int ackNAK_absSF = absSF + reps + 4;
+		AssertFatal(reps > 2,
+			    "Have to handle programming of ACK when PDSCH repetitions is > 2\n");
+		ul_req =
+		    &mac->UL_req_tmp[CC_idP][ackNAK_absSF %
+					     10].ul_config_request_body;
+		ul_config_pdu =
+		    &ul_req->ul_config_pdu_list[ul_req->number_of_pdus];
+
+		ul_config_pdu->pdu_type =
+		    NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE;
+		ul_config_pdu->pdu_size =
+		    (uint8_t) (2 + sizeof(nfapi_ul_config_uci_harq_pdu));
+		ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.handle = 0;	// don't know how to use this
+		ul_config_pdu->uci_harq_pdu.
+		    ue_information.ue_information_rel8.rnti = ra->rnti;
+		ul_config_pdu->uci_harq_pdu.
+		    ue_information.ue_information_rel13.ue_type =
+		    (ra->rach_resource_type < 3) ? 1 : 2;
+		ul_config_pdu->uci_harq_pdu.
+		    ue_information.ue_information_rel13.empty_symbols = 0;
+		ul_config_pdu->uci_harq_pdu.ue_information.
+		    ue_information_rel13.total_number_of_repetitions =
+		    pucchreps[ra->rach_resource_type - 1];
+		ul_config_pdu->uci_harq_pdu.
+		    ue_information.ue_information_rel13.repetition_number =
+		    0;
+		// Note need to keep sending this across reptitions!!!! Not really for PUCCH, to ask small-cell forum, we'll see for the other messages, maybe parameters change across repetitions and FAPI has to provide for that
+		if (cc[CC_idP].tdd_Config == NULL) {	// FDD case
+		    ul_config_pdu->uci_harq_pdu.
+			harq_information.harq_information_rel8_fdd.
+			n_pucch_1_0 =
+			n1pucchan[ra->rach_resource_type - 1];
+		    // NOTE: How to fill in the rest of the n_pucch_1_0 information 213 Section 10.1.2.1 in the general case
+		    // = N_ECCE_q + Delta_ARO + n1pucchan[ce_level]
+		    // higher in the MPDCCH configuration, N_ECCE_q is hard-coded to 0, and harq resource offset to 0 =>
+		    // Delta_ARO = 0 from Table 10.1.2.1-1
+		    ul_config_pdu->uci_harq_pdu.harq_information.harq_information_rel8_fdd.harq_size = 1;	// 1-bit ACK/NAK
+		} else {
+		    AssertFatal(1 == 0,
+				"PUCCH configuration for ACK/NAK not handled yet for TDD BL/CE case\n");
+		}
+		ul_req->number_of_pdus++;
+		T(T_ENB_MAC_UE_DL_PDU_WITH_DATA, T_INT(module_idP),
+		  T_INT(CC_idP), T_INT(ra->rnti), T_INT(frameP),
+		  T_INT(subframeP), T_INT(0 /*harq_pid always 0? */ ),
+		  T_BUFFER(&mac->UE_list.DLSCH_pdu[CC_idP][0][UE_id].
+			   payload[0], ra->msg4_TBsize));
+
+		if (opt_enabled == 1) {
+		    trace_pdu(1,
+			      (uint8_t *) mac->
+			      UE_list.DLSCH_pdu[CC_idP][0][(unsigned char)
+							   UE_id].payload
+			      [0], rrc_sdu_length, UE_id, 3,
+			      UE_RNTI(module_idP, UE_id), mac->frame,
+			      mac->subframe, 0, 0);
+		    LOG_D(OPT,
+			  "[eNB %d][DLSCH] CC_id %d Frame %d trace pdu for rnti %x with size %d\n",
+			  module_idP, CC_idP, frameP, UE_RNTI(module_idP,
+							      UE_id),
+			  rrc_sdu_length);
+		}
+	    }			// Msg4 frame/subframe
+	}			// msg4_mpdcch_repetition_count
+    }				// rach_resource_type > 0 
+    else
+#endif
+    {				// This is normal LTE case
+	if ((ra->Msg4_frame == frameP) && (ra->Msg4_subframe == subframeP)) {
+	    LOG_D(MAC,
+		  "[eNB %d][RAPROC] CC_id %d Frame %d, subframeP %d: Generating Msg4 with RRC Piggyback (RNTI %x)\n",
+		  module_idP, CC_idP, frameP, subframeP, ra->rnti);
+
+	    /// Choose first 4 RBs for Msg4, should really check that these are free!
+	    first_rb = 0;
+
+	    vrb_map[first_rb] = 1;
+	    vrb_map[first_rb + 1] = 1;
+	    vrb_map[first_rb + 2] = 1;
+	    vrb_map[first_rb + 3] = 1;
+
+
+	    // Compute MCS/TBS for 3 PRB (coded on 4 vrb)
+	    msg4_header = 1 + 6 + 1;	// CR header, CR CE, SDU header
+
+	    if ((rrc_sdu_length + msg4_header) <= 22) {
+		ra->msg4_mcs = 4;
+		ra->msg4_TBsize = 22;
+	    } else if ((rrc_sdu_length + msg4_header) <= 28) {
+		ra->msg4_mcs = 5;
+		ra->msg4_TBsize = 28;
+	    } else if ((rrc_sdu_length + msg4_header) <= 32) {
+		ra->msg4_mcs = 6;
+		ra->msg4_TBsize = 32;
+	    } else if ((rrc_sdu_length + msg4_header) <= 41) {
+		ra->msg4_mcs = 7;
+		ra->msg4_TBsize = 41;
+	    } else if ((rrc_sdu_length + msg4_header) <= 49) {
+		ra->msg4_mcs = 8;
+		ra->msg4_TBsize = 49;
+	    } else if ((rrc_sdu_length + msg4_header) <= 57) {
+		ra->msg4_mcs = 9;
+		ra->msg4_TBsize = 57;
+	    }
+
+	    fill_nfapi_dl_dci_1A(dl_config_pdu, 4,	// aggregation_level
+				 ra->rnti,	// rnti
+				 1,	// rnti_type, CRNTI
+				 ra->harq_pid,	// harq_process
+				 1,	// tpc, none
+				 getRIV(N_RB_DL, first_rb, 4),	// resource_block_coding
+				 ra->msg4_mcs,	// mcs
+				 1,	// ndi
+				 0,	// rv
+				 0);	// vrb_flag
+	    LOG_D(MAC,
+		  "Frame %d, subframe %d: Msg4 DCI pdu_num %d (rnti %x,rnti_type %d,harq_pid %d, resource_block_coding (%p) %d\n",
+		  frameP, subframeP, dl_req->number_pdu,
+		  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti,
+		  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type,
+		  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.harq_process,
+		  &dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+		  resource_block_coding,
+		  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+		  resource_block_coding);
+	    AssertFatal(dl_config_pdu->dci_dl_pdu.
+			dci_dl_pdu_rel8.resource_block_coding < 8192,
+			"resource_block_coding %u < 8192\n",
+			dl_config_pdu->dci_dl_pdu.
+			dci_dl_pdu_rel8.resource_block_coding);
+	    if (!CCE_allocation_infeasible
+		(module_idP, CC_idP, 1, subframeP,
+		 dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+		 aggregation_level, ra->rnti)) {
+		dl_req->number_dci++;
+		dl_req->number_pdu++;
+
+		ra->state = WAITMSG4ACK;
+
+		// increment Absolute subframe by 8 for Msg4 retransmission
+		LOG_D(MAC,
+		      "Frame %d, Subframe %d: Preparing for Msg4 retransmission currently %d.%d\n",
+		      frameP, subframeP, ra->Msg4_frame,
+		      ra->Msg4_subframe);
+		if (ra->Msg4_subframe > 1)
+		    ra->Msg4_frame++;
+		ra->Msg4_frame &= 1023;
+		ra->Msg4_subframe = (ra->Msg4_subframe + 8) % 10;
+		LOG_D(MAC,
+		      "Frame %d, Subframe %d: Msg4 retransmission in %d.%d\n",
+		      frameP, subframeP, ra->Msg4_frame,
+		      ra->Msg4_subframe);
+		lcid = 0;
+
+		// put HARQ process round to 0
+		if (cc->tdd_Config)
+		    ra->harq_pid = ((frameP * 10) + subframeP) % 10;
+		else
+		    ra->harq_pid = ((frameP * 10) + subframeP) & 7;
+		UE_list->UE_sched_ctrl[UE_id].round[CC_idP][ra->harq_pid] =
+		    0;
+
+		if ((ra->msg4_TBsize - rrc_sdu_length - msg4_header) <= 2) {
+		    msg4_padding =
+			ra->msg4_TBsize - rrc_sdu_length - msg4_header;
+		    msg4_post_padding = 0;
+		} else {
+		    msg4_padding = 0;
+		    msg4_post_padding =
+			ra->msg4_TBsize - rrc_sdu_length - msg4_header - 1;
+		}
+
+		LOG_D(MAC,
+		      "[eNB %d][RAPROC] CC_idP %d Frame %d subframeP %d Msg4 : TBS %d, sdu_len %d, msg4_header %d, msg4_padding %d, msg4_post_padding %d\n",
+		      module_idP, CC_idP, frameP, subframeP,
+		      ra->msg4_TBsize, rrc_sdu_length, msg4_header,
+		      msg4_padding, msg4_post_padding);
+		DevAssert(UE_id != UE_INDEX_INVALID);	// FIXME not sure how to gracefully return
+		// CHECK THIS: &cc[CC_idP].CCCH_pdu.payload[0]
+		offset = generate_dlsch_header((unsigned char *) mac->UE_list.DLSCH_pdu[CC_idP][0][(unsigned char) UE_id].payload[0], 1,	//num_sdus
+					       (unsigned short *) &rrc_sdu_length,	//
+					       &lcid,	// sdu_lcid
+					       255,	// no drx
+					       31,	// no timing advance
+					       ra->cont_res_id,	// contention res id
+					       msg4_padding,	// no padding
+					       msg4_post_padding);
+
+		memcpy((void *) &mac->UE_list.
+		       DLSCH_pdu[CC_idP][0][(unsigned char)
+					    UE_id].payload[0][(unsigned
+							       char)
+							      offset],
+		       &cc[CC_idP].CCCH_pdu.payload[0], rrc_sdu_length);
+
+		// DLSCH Config
+		fill_nfapi_dlsch_config(mac, dl_req, ra->msg4_TBsize, mac->pdu_index[CC_idP], ra->rnti, 2,	// resource_allocation_type : format 1A/1B/1D
+					0,	// virtual_resource_block_assignment_flag : localized
+					getRIV(N_RB_DL, first_rb, 4),	// resource_block_coding : RIV, 4 PRB
+					2,	// modulation: QPSK
+					0,	// redundancy version
+					1,	// transport_blocks
+					0,	// transport_block_to_codeword_swap_flag (0)
+					(cc->p_eNB == 1) ? 0 : 1,	// transmission_scheme
+					1,	// number of layers
+					1,	// number of subbands
+					//0,                         // codebook index 
+					1,	// ue_category_capacity
+					4,	// pa: 0 dB
+					0,	// delta_power_offset_index
+					0,	// ngap
+					1,	// NPRB = 3 like in DCI
+					(cc->p_eNB == 1) ? 1 : 2,	// transmission mode
+					1,	// num_bf_prb_per_subband
+					1);	// num_bf_vector
+		LOG_D(MAC,
+		      "Filled DLSCH config, pdu number %d, non-dci pdu_index %d\n",
+		      dl_req->number_pdu, mac->pdu_index[CC_idP]);
+
+		// DL request
+		mac->TX_req[CC_idP].sfn_sf =
+		    fill_nfapi_tx_req(&mac->TX_req[CC_idP].tx_request_body,
+				      (frameP * 10) + subframeP,
+				      rrc_sdu_length,
+				      mac->pdu_index[CC_idP],
+				      mac->UE_list.
+				      DLSCH_pdu[CC_idP][0][(unsigned char)
+							   UE_id].payload
+				      [0]);
+		mac->pdu_index[CC_idP]++;
+
+		LOG_D(MAC, "Filling UCI ACK/NAK information, cce_idx %d\n",
+		      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.cce_idx);
+		// Program PUCCH1a for ACK/NAK
+		// Program ACK/NAK for Msg4 PDSCH
+		fill_nfapi_uci_acknak(module_idP,
+				      CC_idP,
+				      ra->rnti,
+				      (frameP * 10) + subframeP,
+				      dl_config_pdu->dci_dl_pdu.
+				      dci_dl_pdu_rel8.cce_idx);
+
+
+		T(T_ENB_MAC_UE_DL_PDU_WITH_DATA, T_INT(module_idP),
+		  T_INT(CC_idP), T_INT(ra->rnti), T_INT(frameP),
+		  T_INT(subframeP), T_INT(0 /*harq_pid always 0? */ ),
+		  T_BUFFER(&mac->UE_list.DLSCH_pdu[CC_idP][0][UE_id].
+			   payload[0], ra->msg4_TBsize));
+
+		if (opt_enabled == 1) {
+		    trace_pdu(1,
+			      (uint8_t *) mac->
+			      UE_list.DLSCH_pdu[CC_idP][0][(unsigned char)
+							   UE_id].payload
+			      [0], rrc_sdu_length, UE_id, 3,
+			      UE_RNTI(module_idP, UE_id), mac->frame,
+			      mac->subframe, 0, 0);
+		    LOG_D(OPT,
+			  "[eNB %d][DLSCH] CC_id %d Frame %d trace pdu for rnti %x with size %d\n",
+			  module_idP, CC_idP, frameP, UE_RNTI(module_idP,
+							      UE_id),
+			  rrc_sdu_length);
+		}
+
+	    }			// CCE Allocation feasible
+	}			// msg4 frame/subframe
+    }				// else rach_resource_type
 }
 
-void check_Msg4_retransmission(module_id_t module_idP,int CC_idP,frame_t frameP,sub_frame_t subframeP,RA_TEMPLATE *RA_template) {
- 
-  eNB_MAC_INST                    *eNB = RC.mac[module_idP];
-  COMMON_channels_t               *cc  = eNB->common_channels;
-  int                             UE_id           = -1;
-  uint8_t                         *vrb_map;
-  int                             first_rb;
-  int                             N_RB_DL;
-  nfapi_dl_config_request_pdu_t   *dl_config_pdu;
-  UE_list_t                       *UE_list=&eNB->UE_list;
-  nfapi_dl_config_request_body_t *dl_req;
-
-  int                             round;
-  /*
+void
+check_Msg4_retransmission(module_id_t module_idP, int CC_idP,
+			  frame_t frameP, sub_frame_t subframeP, RA_t * ra)
+{
+
+    eNB_MAC_INST *mac = RC.mac[module_idP];
+    COMMON_channels_t *cc = mac->common_channels;
+    int UE_id = -1;
+    uint8_t *vrb_map;
+    int first_rb;
+    int N_RB_DL;
+    nfapi_dl_config_request_pdu_t *dl_config_pdu;
+    UE_list_t *UE_list = &mac->UE_list;
+    nfapi_dl_config_request_body_t *dl_req;
+
+    int round;
+    /*
+       #ifdef Rel14
+       COMMON_channels_t               *cc  = mac->common_channels;
+
+       int rmax            = 0;
+       int rep             = 0; 
+       int reps            = 0;
+
+       first_rb        = 0;
+       struct PRACH_ConfigSIB_v1310 *ext4_prach;
+       PRACH_ParametersListCE_r13_t *prach_ParametersListCE_r13;
+       PRACH_ParametersCE_r13_t *p[4];
+
+       if (cc[CC_idP].radioResourceConfigCommon_BR) {
+
+       ext4_prach                 = cc[CC_idP].radioResourceConfigCommon_BR->ext4->prach_ConfigCommon_v1310;
+       prach_ParametersListCE_r13 = &ext4_prach->prach_ParametersListCE_r13;
+
+       switch (prach_ParametersListCE_r13->list.count) {
+       case 4:
+       p[3]=prach_ParametersListCE_r13->list.array[3];
+       case 3:
+       p[2]=prach_ParametersListCE_r13->list.array[2];
+       case 2:
+       p[1]=prach_ParametersListCE_r13->list.array[1];
+       case 1:
+       p[0]=prach_ParametersListCE_r13->list.array[0];
+       default:
+       AssertFatal(1==0,"Illegal count for prach_ParametersListCE_r13 %d\n",prach_ParametersListCE_r13->list.count);
+       }
+       }
+       #endif
+     */
+
+    // check HARQ status and retransmit if necessary
+
+
+    UE_id = find_UE_id(module_idP, ra->rnti);
+    AssertFatal(UE_id >= 0, "Can't find UE for t-crnti\n");
+
+    round = UE_list->UE_sched_ctrl[UE_id].round[CC_idP][ra->harq_pid];
+    vrb_map = cc[CC_idP].vrb_map;
+
+    dl_req = &mac->DL_req[CC_idP].dl_config_request_body;
+    dl_config_pdu = &dl_req->dl_config_pdu_list[dl_req->number_pdu];
+    N_RB_DL = to_prb(cc[CC_idP].mib->message.dl_Bandwidth);
+
+    LOG_D(MAC,
+	  "[eNB %d][RAPROC] CC_id %d Frame %d, subframeP %d: Checking if Msg4 for harq_pid %d was acknowledged (round %d)\n",
+	  module_idP, CC_idP, frameP, subframeP, ra->harq_pid, round);
+
+    if (round != 8) {
+
 #ifdef Rel14
-  COMMON_channels_t               *cc  = eNB->common_channels;
-
-  int rmax            = 0;
-  int rep             = 0; 
-  int reps            = 0;
-
-  first_rb        = 0;
-  struct PRACH_ConfigSIB_v1310 *ext4_prach;
-  PRACH_ParametersListCE_r13_t *prach_ParametersListCE_r13;
-  PRACH_ParametersCE_r13_t *p[4];
-
-  if (cc[CC_idP].radioResourceConfigCommon_BR) {
-
-    ext4_prach                 = cc[CC_idP].radioResourceConfigCommon_BR->ext4->prach_ConfigCommon_v1310;
-    prach_ParametersListCE_r13 = &ext4_prach->prach_ParametersListCE_r13;
-        
-    switch (prach_ParametersListCE_r13->list.count) {
-    case 4:
-      p[3]=prach_ParametersListCE_r13->list.array[3];
-    case 3:
-      p[2]=prach_ParametersListCE_r13->list.array[2];
-    case 2:
-      p[1]=prach_ParametersListCE_r13->list.array[1];
-    case 1:
-      p[0]=prach_ParametersListCE_r13->list.array[0];
-    default:
-      AssertFatal(1==0,"Illegal count for prach_ParametersListCE_r13 %d\n",prach_ParametersListCE_r13->list.count);
-    }
-  }
+	if (ra->rach_resource_type > 0) {
+	    AssertFatal(1 == 0,
+			"Msg4 Retransmissions not handled yet for BL/CE UEs\n");
+	} else
 #endif
-  */
-
-  // check HARQ status and retransmit if necessary
-
-  
-  UE_id = find_UE_id(module_idP,RA_template->rnti);
-  AssertFatal(UE_id>=0,"Can't find UE for t-crnti\n");
-
-  round = UE_list->UE_sched_ctrl[UE_id].round[CC_idP][RA_template->harq_pid];
-  vrb_map       = cc[CC_idP].vrb_map;
-  
-  dl_req        = &eNB->DL_req[CC_idP].dl_config_request_body;
-  dl_config_pdu = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
-  N_RB_DL       = to_prb(cc[CC_idP].mib->message.dl_Bandwidth);
-  
-  LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d, subframeP %d: Checking if Msg4 for harq_pid %d was acknowledged (round %d)\n",
-	module_idP,CC_idP,frameP,subframeP,RA_template->harq_pid,round);
-
-  if (round!=8) {
-    
-#ifdef Rel14
-    if (RA_template->rach_resource_type>0) {
-      AssertFatal(1==0,"Msg4 Retransmissions not handled yet for BL/CE UEs\n");
+	{
+	    if ((ra->Msg4_frame == frameP)
+		&& (ra->Msg4_subframe == subframeP)) {
+
+		//ra->wait_ack_Msg4++;
+		// we have to schedule a retransmission
+
+		first_rb = 0;
+		vrb_map[first_rb] = 1;
+		vrb_map[first_rb + 1] = 1;
+		vrb_map[first_rb + 2] = 1;
+		vrb_map[first_rb + 3] = 1;
+
+		fill_nfapi_dl_dci_1A(dl_config_pdu, 4,	// aggregation_level
+				     ra->rnti,	// rnti
+				     1,	// rnti_type, CRNTI
+				     ra->harq_pid,	// harq_process
+				     1,	// tpc, none
+				     getRIV(N_RB_DL, first_rb, 4),	// resource_block_coding
+				     ra->msg4_mcs,	// mcs
+				     1,	// ndi
+				     round & 3,	// rv
+				     0);	// vrb_flag
+
+		if (!CCE_allocation_infeasible
+		    (module_idP, CC_idP, 1, subframeP,
+		     dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+		     aggregation_level, ra->rnti)) {
+		    dl_req->number_dci++;
+		    dl_req->number_pdu++;
+
+		    LOG_D(MAC,
+			  "msg4 retransmission for rnti %x (round %d) fsf %d/%d\n",
+			  ra->rnti, round, frameP, subframeP);
+		    // DLSCH Config
+		    fill_nfapi_dlsch_config(mac, dl_req, ra->msg4_TBsize,
+					    -1
+					    /* retransmission, no pdu_index */
+					    , ra->rnti, 2,	// resource_allocation_type : format 1A/1B/1D
+					    0,	// virtual_resource_block_assignment_flag : localized
+					    getRIV(N_RB_DL, first_rb, 4),	// resource_block_coding : RIV, 4 PRB
+					    2,	// modulation: QPSK
+					    round & 3,	// redundancy version
+					    1,	// transport_blocks
+					    0,	// transport_block_to_codeword_swap_flag (0)
+					    (cc->p_eNB == 1) ? 0 : 1,	// transmission_scheme
+					    1,	// number of layers
+					    1,	// number of subbands
+					    //0,                         // codebook index 
+					    1,	// ue_category_capacity
+					    4,	// pa: 0 dB
+					    0,	// delta_power_offset_index
+					    0,	// ngap
+					    1,	// NPRB = 3 like in DCI
+					    (cc->p_eNB == 1) ? 1 : 2,	// transmission mode
+					    1,	// num_bf_prb_per_subband
+					    1);	// num_bf_vector
+		} else
+		    LOG_D(MAC,
+			  "msg4 retransmission for rnti %x (round %d) fsf %d/%d CCE allocation failed!\n",
+			  ra->rnti, round, frameP, subframeP);
+
+
+		// Program PUCCH1a for ACK/NAK
+
+
+		fill_nfapi_uci_acknak(module_idP, CC_idP,
+				      ra->rnti,
+				      (frameP * 10) + subframeP,
+				      dl_config_pdu->dci_dl_pdu.
+				      dci_dl_pdu_rel8.cce_idx);
+
+		// prepare frame for retransmission
+		if (ra->Msg4_subframe > 1)
+		    ra->Msg4_frame++;
+		ra->Msg4_frame &= 1023;
+		ra->Msg4_subframe = (ra->Msg4_subframe + 8) % 10;
+
+		LOG_W(MAC,
+		      "[eNB %d][RAPROC] CC_id %d Frame %d, subframeP %d: Msg4 not acknowledged, adding ue specific dci (rnti %x) for RA (Msg4 Retransmission round %d in %d.%d)\n",
+		      module_idP, CC_idP, frameP, subframeP, ra->rnti,
+		      round, ra->Msg4_frame, ra->Msg4_subframe);
+
+	    }			// Msg4 frame/subframe
+	}			// regular LTE case
+    } else {
+	LOG_D(MAC,
+	      "[eNB %d][RAPROC] CC_id %d Frame %d, subframeP %d : Msg4 acknowledged\n",
+	      module_idP, CC_idP, frameP, subframeP);
+	ra->state = IDLE;
+	UE_id = find_UE_id(module_idP, ra->rnti);
+	DevAssert(UE_id != -1);
+	mac->UE_list.UE_template[UE_PCCID(module_idP, UE_id)][UE_id].
+	    configured = TRUE;
     }
-    else
-#endif 
-      {
-	if ( (RA_template->Msg4_frame == frameP) && (RA_template->Msg4_subframe == subframeP)) {	       
-	  
-	  //RA_template->wait_ack_Msg4++;
-	  // we have to schedule a retransmission
-	  
-	  first_rb=0;
-	  vrb_map[first_rb] = 1;
-	  vrb_map[first_rb+1] = 1;
-	  vrb_map[first_rb+2] = 1;
-	  vrb_map[first_rb+3] = 1;
-	  
-	  fill_nfapi_dl_dci_1A(dl_config_pdu,
-			       4,                           // aggregation_level
-			       RA_template->rnti,           // rnti
-			       1,                           // rnti_type, CRNTI
-			       RA_template->harq_pid,       // harq_process
-			       1,                           // tpc, none
-			       getRIV(N_RB_DL,first_rb,4),  // resource_block_coding
-			       RA_template->msg4_mcs,       // mcs
-			       1,                           // ndi
-			       round&3,                       // rv
-			       0);                          // vrb_flag
-	  
-	  if (!CCE_allocation_infeasible(module_idP,CC_idP,1,subframeP,dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,RA_template->rnti)) {
-	    dl_req->number_dci++;
-	    dl_req->number_pdu++;
-	    
-	    LOG_D(MAC,"msg4 retransmission for rnti %x (round %d) fsf %d/%d\n", RA_template->rnti, round, frameP, subframeP);
-	    	  // DLSCH Config
-	    fill_nfapi_dlsch_config(eNB,
-				    dl_req,
-				    RA_template->msg4_TBsize,
-				    -1                           /* retransmission, no pdu_index */,
-				    RA_template->rnti,
-				    2,                           // resource_allocation_type : format 1A/1B/1D
-				    0,                           // virtual_resource_block_assignment_flag : localized
-				    getRIV(N_RB_DL,first_rb,4),  // resource_block_coding : RIV, 4 PRB
-				    2,                           // modulation: QPSK
-				    round&3,                     // redundancy version
-				    1,                           // transport_blocks
-				    0,                           // transport_block_to_codeword_swap_flag (0)
-				    (cc->p_eNB==1 ) ? 0 : 1,     // transmission_scheme
-				    1,                           // number of layers
-				    1,                           // number of subbands
-				    //0,                         // codebook index 
-				    1,                           // ue_category_capacity
-				    4,                           // pa: 0 dB
-				    0,                           // delta_power_offset_index
-				    0,                           // ngap
-				    1,                           // NPRB = 3 like in DCI
-				    (cc->p_eNB==1 ) ? 1 : 2,     // transmission mode
-				    1,                           // num_bf_prb_per_subband
-				    1);                          // num_bf_vector
-	  }
-	  else
-	    LOG_D(MAC,"msg4 retransmission for rnti %x (round %d) fsf %d/%d CCE allocation failed!\n", RA_template->rnti, round, frameP, subframeP);
-	  
-	  
-	  // Program PUCCH1a for ACK/NAK
-	  
-
-	  fill_nfapi_uci_acknak(module_idP,CC_idP,
-				RA_template->rnti,
-				(frameP*10)+subframeP,
-				dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.cce_idx);
-	  
-	  // prepare frame for retransmission
-	  if (RA_template->Msg4_subframe>1) RA_template->Msg4_frame++;
-	  RA_template->Msg4_frame&=1023;
-	  RA_template->Msg4_subframe=(RA_template->Msg4_subframe+8)%10;
-
-	  LOG_W(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d, subframeP %d: Msg4 not acknowledged, adding ue specific dci (rnti %x) for RA (Msg4 Retransmission round %d in %d.%d)\n",
-		module_idP,CC_idP,frameP,subframeP,RA_template->rnti,round,RA_template->Msg4_frame,RA_template->Msg4_subframe);
-
-	} // Msg4 frame/subframe
-      } // regular LTE case
-  } else {
-    LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d, subframeP %d : Msg4 acknowledged\n",module_idP,CC_idP,frameP,subframeP);
-    RA_template->wait_ack_Msg4=0;
-    RA_template->RA_active=FALSE;
-    UE_id = find_UE_id(module_idP,RA_template->rnti);
-    DevAssert( UE_id != -1 );
-    eNB->UE_list.UE_template[UE_PCCID(module_idP,UE_id)][UE_id].configured=TRUE;
-  }
-} 
-
-void schedule_RA(module_id_t module_idP,frame_t frameP, sub_frame_t subframeP)
-{
-
-  int                             CC_id;
-  eNB_MAC_INST                    *eNB = RC.mac[module_idP];
-  COMMON_channels_t               *cc  = eNB->common_channels;
-  RA_TEMPLATE                     *RA_template;
-  uint8_t                         i;
+}
 
-  start_meas(&eNB->schedule_ra);
+void
+schedule_RA(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP)
+{
 
+    int CC_id;
+    eNB_MAC_INST *mac = RC.mac[module_idP];
+    COMMON_channels_t *cc = mac->common_channels;
+    RA_t *ra;
+    uint8_t i;
 
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-    // skip UL component carriers if TDD
-    if (is_UL_sf(&cc[CC_id],subframeP)==1) continue;
+    start_meas(&mac->schedule_ra);
 
 
-    for (i=0; i<NB_RA_PROC_MAX; i++) {
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+	// skip UL component carriers if TDD
+	if (is_UL_sf(&cc[CC_id], subframeP) == 1)
+	    continue;
 
-      RA_template = (RA_TEMPLATE *)&cc[CC_id].RA_template[i];
 
-      if (RA_template->RA_active == TRUE) {
+	for (i = 0; i < NB_RA_PROC_MAX; i++) {
 
-        LOG_D(MAC,"[eNB %d][RAPROC] Frame %d, Subframe %d : CC_id %d RA %d is active (generate RAR %d, generate_Msg4 %d, wait_ack_Msg4 %d, rnti %x)\n",
-              module_idP,frameP,subframeP,CC_id,i,RA_template->generate_rar,RA_template->generate_Msg4,RA_template->wait_ack_Msg4, RA_template->rnti);
+	    ra = (RA_t *) & cc[CC_id].ra[i];
 
-        if      (RA_template->generate_rar == 1)  generate_Msg2(module_idP,CC_id,frameP,subframeP,RA_template);
-	else if (RA_template->generate_Msg4 == 1) generate_Msg4(module_idP,CC_id,frameP,subframeP,RA_template);
- 	else if (RA_template->wait_ack_Msg4==1)   check_Msg4_retransmission(module_idP,CC_id,frameP,subframeP,RA_template);
+	    if (ra->state == MSG2)
+		generate_Msg2(module_idP, CC_id, frameP, subframeP, ra);
+	    else if (ra->state == MSG4)
+		generate_Msg4(module_idP, CC_id, frameP, subframeP, ra);
+	    else if (ra->state == WAITMSG4ACK)
+		check_Msg4_retransmission(module_idP, CC_id, frameP,
+					  subframeP, ra);
 
+	}			// for i=0 .. N_RA_PROC-1 
+    }				// CC_id
 
-      } // RA_active == TRUE
-    } // for i=0 .. N_RA_PROC-1 
-  } // CC_id
-  
-  stop_meas(&eNB->schedule_ra);
+    stop_meas(&mac->schedule_ra);
 }
 
 
 // handles the event of MSG1 reception
-void initiate_ra_proc(module_id_t module_idP, 
-		      int CC_id,
-		      frame_t frameP, 
-		      sub_frame_t subframeP,
-		      uint16_t preamble_index,
-		      int16_t timing_offset,
-		      uint16_t ra_rnti
+void
+initiate_ra_proc(module_id_t module_idP,
+		 int CC_id,
+		 frame_t frameP,
+		 sub_frame_t subframeP,
+		 uint16_t preamble_index,
+		 int16_t timing_offset, uint16_t ra_rnti
 #ifdef Rel14
-		      ,
-		      uint8_t rach_resource_type
+		 , uint8_t rach_resource_type
 #endif
-		      )
+    )
 {
 
-  uint8_t i;
+    uint8_t i;
 
-  COMMON_channels_t   *cc  = &RC.mac[module_idP]->common_channels[CC_id];
-  RA_TEMPLATE *RA_template = &cc->RA_template[0];
+    COMMON_channels_t *cc = &RC.mac[module_idP]->common_channels[CC_id];
+    RA_t *ra = &cc->ra[0];
 
-  struct PRACH_ConfigSIB_v1310 *ext4_prach = NULL;
-  PRACH_ParametersListCE_r13_t *prach_ParametersListCE_r13 = NULL;
+    struct PRACH_ConfigSIB_v1310 *ext4_prach = NULL;
+    PRACH_ParametersListCE_r13_t *prach_ParametersListCE_r13 = NULL;
 
-  if (cc->radioResourceConfigCommon_BR && cc->radioResourceConfigCommon_BR->ext4) {
-    ext4_prach=cc->radioResourceConfigCommon_BR->ext4->prach_ConfigCommon_v1310;
-    prach_ParametersListCE_r13= &ext4_prach->prach_ParametersListCE_r13;
-  }
-  LOG_I(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d, Subframe %d  Initiating RA procedure for preamble index %d\n",module_idP,CC_id,frameP,subframeP,preamble_index);
+    if (cc->radioResourceConfigCommon_BR
+	&& cc->radioResourceConfigCommon_BR->ext4) {
+	ext4_prach =
+	    cc->radioResourceConfigCommon_BR->ext4->
+	    prach_ConfigCommon_v1310;
+	prach_ParametersListCE_r13 =
+	    &ext4_prach->prach_ParametersListCE_r13;
+    }
+    LOG_I(MAC,
+	  "[eNB %d][RAPROC] CC_id %d Frame %d, Subframe %d  Initiating RA procedure for preamble index %d\n",
+	  module_idP, CC_id, frameP, subframeP, preamble_index);
 #ifdef Rel14
-  LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d, Subframe %d  PRACH resource type %d\n",module_idP,CC_id,frameP,subframeP,rach_resource_type);
+    LOG_D(MAC,
+	  "[eNB %d][RAPROC] CC_id %d Frame %d, Subframe %d  PRACH resource type %d\n",
+	  module_idP, CC_id, frameP, subframeP, rach_resource_type);
 #endif
 
-  if (prach_ParametersListCE_r13 && 
-      prach_ParametersListCE_r13->list.count<rach_resource_type) {
-    LOG_E(MAC,"[eNB %d][RAPROC] CC_id %d Received impossible PRACH resource type %d, only %d CE levels configured\n",
-	  module_idP,CC_id,
-	  rach_resource_type,
-	  (int)prach_ParametersListCE_r13->list.count);
-    return;
-  }
-
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_INITIATE_RA_PROC,1);
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_INITIATE_RA_PROC,0);
-
-  for (i=0; i<NB_RA_PROC_MAX; i++) {
-    if (RA_template[i].RA_active==FALSE &&
-        RA_template[i].wait_ack_Msg4 == 0) {
-      int loop = 0;
-      LOG_D(MAC,"Frame %d, Subframe %d: Activating RA process %d\n",frameP,subframeP,i);
-      RA_template[i].RA_active          = TRUE;
-      RA_template[i].generate_rar       = 1;
-      RA_template[i].generate_Msg4      = 0;
-      RA_template[i].wait_ack_Msg4      = 0;
-      RA_template[i].timing_offset      = timing_offset;
-      RA_template[i].preamble_subframe  = subframeP;
+    if (prach_ParametersListCE_r13 &&
+	prach_ParametersListCE_r13->list.count < rach_resource_type) {
+	LOG_E(MAC,
+	      "[eNB %d][RAPROC] CC_id %d Received impossible PRACH resource type %d, only %d CE levels configured\n",
+	      module_idP, CC_id, rach_resource_type,
+	      (int) prach_ParametersListCE_r13->list.count);
+	return;
+    }
+
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_INITIATE_RA_PROC, 1);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_INITIATE_RA_PROC, 0);
+
+    for (i = 0; i < NB_RA_PROC_MAX; i++) {
+	if (ra[i].state == IDLE) {
+	    int loop = 0;
+	    LOG_D(MAC, "Frame %d, Subframe %d: Activating RA process %d\n",
+		  frameP, subframeP, i);
+	    ra[i].state = MSG2;
+	    ra[i].timing_offset = timing_offset;
+	    ra[i].preamble_subframe = subframeP;
 #ifdef Rel14
-      RA_template[i].rach_resource_type = rach_resource_type;
-      RA_template[i].msg2_mpdcch_repetition_cnt = 0;		      
-      RA_template[i].msg4_mpdcch_repetition_cnt = 0;		      
+	    ra[i].rach_resource_type = rach_resource_type;
+	    ra[i].msg2_mpdcch_repetition_cnt = 0;
+	    ra[i].msg4_mpdcch_repetition_cnt = 0;
 #endif
-      RA_template[i].Msg2_frame         = frameP+((subframeP>5)?1:0);
-      RA_template[i].Msg2_subframe      = (subframeP+4)%10;
-      /* TODO: find better procedure to allocate RNTI */
-      do {
-        RA_template[i].rnti = taus();
-        loop++;
-      } while (loop != 100 &&
-               /* TODO: this is not correct, the rnti may be in use without
-                * being in the MAC yet. To be refined.
-                */
-               !(find_UE_id(module_idP, RA_template[i].rnti) == -1 &&
-                 /* 1024 and 60000 arbirarily chosen, not coming from standard */
-                 RA_template[i].rnti >= 1024 && RA_template[i].rnti < 60000));
-      if (loop == 100) { printf("%s:%d:%s: FATAL ERROR! contact the authors\n", __FILE__, __LINE__, __FUNCTION__); abort(); }
-      RA_template[i].RA_rnti        = ra_rnti;
-      RA_template[i].preamble_index = preamble_index;
-      LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d Activating RAR generation in Frame %d, subframe %d for process %d, rnti %x, RA_active %d\n",
-            module_idP,CC_id,frameP,
-	    RA_template[i].Msg2_frame,
-	    RA_template[i].Msg2_subframe,
-	    i,RA_template[i].rnti,
-            RA_template[i].RA_active);
-
-      return;
+	    ra[i].Msg2_frame = frameP + ((subframeP > 5) ? 1 : 0);
+	    ra[i].Msg2_subframe = (subframeP + 4) % 10;
+	    /* TODO: find better procedure to allocate RNTI */
+	    do {
+		ra[i].rnti = taus();
+		loop++;
+	    }
+	    while (loop != 100 &&
+		   /* TODO: this is not correct, the rnti may be in use without
+		    * being in the MAC yet. To be refined.
+		    */
+		   !(find_UE_id(module_idP, ra[i].rnti) == -1 &&
+		     /* 1024 and 60000 arbirarily chosen, not coming from standard */
+		     ra[i].rnti >= 1024 && ra[i].rnti < 60000));
+	    if (loop == 100) {
+		printf("%s:%d:%s: FATAL ERROR! contact the authors\n",
+		       __FILE__, __LINE__, __FUNCTION__);
+		abort();
+	    }
+	    ra[i].RA_rnti = ra_rnti;
+	    ra[i].preamble_index = preamble_index;
+	    LOG_D(MAC,
+		  "[eNB %d][RAPROC] CC_id %d Frame %d Activating RAR generation in Frame %d, subframe %d for process %d, rnti %x, state %d\n",
+		  module_idP, CC_id, frameP, ra[i].Msg2_frame,
+		  ra[i].Msg2_subframe, i, ra[i].rnti, ra[i].state);
+
+	    return;
+	}
     }
-  }
 
-  LOG_E(MAC,"[eNB %d][RAPROC] FAILURE: CC_id %d Frame %d Initiating RA procedure for preamble index %d\n",module_idP,CC_id,frameP,preamble_index);
+    LOG_E(MAC,
+	  "[eNB %d][RAPROC] FAILURE: CC_id %d Frame %d Initiating RA procedure for preamble index %d\n",
+	  module_idP, CC_id, frameP, preamble_index);
 }
 
-void cancel_ra_proc(module_id_t module_idP, int CC_id, frame_t frameP, rnti_t rnti)
+void
+cancel_ra_proc(module_id_t module_idP, int CC_id, frame_t frameP,
+	       rnti_t rnti)
 {
-  unsigned char i;
-  RA_TEMPLATE *RA_template = (RA_TEMPLATE *)&RC.mac[module_idP]->common_channels[CC_id].RA_template[0];
-
-  MSC_LOG_EVENT(MSC_PHY_ENB, "RA Cancelling procedure ue %"PRIx16" ", rnti);
-  LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d Cancelling RA procedure for UE rnti %x\n",module_idP,CC_id,frameP,rnti);
-
-  for (i=0; i<NB_RA_PROC_MAX; i++) {
-    if (rnti == RA_template[i].rnti) {
-      RA_template[i].RA_active=FALSE;
-      RA_template[i].generate_rar=0;
-      RA_template[i].generate_Msg4=0;
-      RA_template[i].wait_ack_Msg4=0;
-      RA_template[i].timing_offset=0;
-      RA_template[i].RRC_timer=20;
-      RA_template[i].rnti = 0;
-      RA_template[i].msg3_round = 0;
+    unsigned char i;
+    RA_t *ra = (RA_t *) & RC.mac[module_idP]->common_channels[CC_id].ra[0];
+
+    MSC_LOG_EVENT(MSC_PHY_ENB, "RA Cancelling procedure ue %" PRIx16 " ",
+		  rnti);
+    LOG_D(MAC,
+	  "[eNB %d][RAPROC] CC_id %d Frame %d Cancelling RA procedure for UE rnti %x\n",
+	  module_idP, CC_id, frameP, rnti);
+
+    for (i = 0; i < NB_RA_PROC_MAX; i++) {
+	if (rnti == ra[i].rnti) {
+	  ra[i].state = IDLE;
+	  ra[i].timing_offset = 0;
+	  ra[i].RRC_timer = 20;
+	  ra[i].rnti = 0;
+	  ra[i].msg3_round = 0;
+	}
     }
-  }
 }
-
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_bch.c b/openair2/LAYER2/MAC/eNB_scheduler_bch.c
index afc1141a222a52db31b00c07c1c37e117d77f854..71be9f9b9d7aa970e9c45377bc159a1c8c37f269 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler_bch.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_bch.c
@@ -52,7 +52,7 @@
 #include "pdcp.h"
 
 #if defined(ENABLE_ITTI)
-# include "intertask_interface.h"
+#include "intertask_interface.h"
 #endif
 
 #define ENABLE_MAC_PAYLOAD_DEBUG
@@ -65,714 +65,822 @@
 #ifdef Rel14
 
 #define size_Sj25 2
-int Sj25[size_Sj25]={0,3};
+int Sj25[size_Sj25] = { 0, 3 };
+
 #define size_Sj50 6
-int Sj50[size_Sj50]={0,1,2,5,6,7};
+int Sj50[size_Sj50] = { 0, 1, 2, 5, 6, 7 };
+
 #define size_Sj75 10
-int Sj75[size_Sj75]={0,1,2,3,4,7,8,9,10,11};
+int Sj75[size_Sj75] = { 0, 1, 2, 3, 4, 7, 8, 9, 10, 11 };
+
 #define size_Sj100 14
-int Sj100[size_Sj100]={0,1,2,3,4,5,6,9,10,11,12,13,14,15};
+int Sj100[size_Sj100] = { 0, 1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 14, 15 };
 
-int SIB1_BR_TBS_table[6] = {208,256,328,504,712,936};
+int SIB1_BR_TBS_table[6] = { 208, 256, 328, 504, 712, 936 };
 
 //------------------------------------------------------------------------------
 void
-schedule_SIB1_BR(
-		 module_id_t   module_idP,
-		 frame_t       frameP,
-		 sub_frame_t   subframeP)
-
+schedule_SIB1_BR(module_id_t module_idP,
+		 frame_t frameP, sub_frame_t subframeP)
 //------------------------------------------------------------------------------
 {
 
-  int8_t                         bcch_sdu_length;
-  int                            CC_id;
-  eNB_MAC_INST                   *eNB = RC.mac[module_idP];
-  COMMON_channels_t              *cc;
-  uint8_t                        *vrb_map;
-  int                            first_rb = -1;
-  int                            N_RB_DL;
-  nfapi_dl_config_request_pdu_t  *dl_config_pdu;
-  nfapi_tx_request_pdu_t         *TX_req;
-  nfapi_dl_config_request_body_t *dl_req;
-  int                            m,i,N_S_NB;
-  int                            *Sj;
-  int                            n_NB = 0;
-  int                            TBS;
-  int                            k=0,rvidx;
-
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-
-    cc              = &eNB->common_channels[CC_id];
-    vrb_map         = (void*)&cc->vrb_map;
-    N_RB_DL         = to_prb(cc->mib->message.dl_Bandwidth); 
-    dl_req          = &eNB->DL_req[CC_id].dl_config_request_body;
-
-    int foffset  = cc->physCellId&1;
-    int sfoffset = (cc->tdd_Config==NULL) ? 0 : 1;
-
-    // Time-domain scheduling
-    if (cc->mib->message.schedulingInfoSIB1_BR_r13==0) continue;
-    else 
-      switch ((cc->mib->message.schedulingInfoSIB1_BR_r13-1)%3) {
-	case 0: // repetition 4
-	  k = (frameP>>1)&3;
-	  if ((subframeP!=(4+sfoffset)) || ((frameP&1)!=foffset)) continue;
-	  break;
-	case 1: // repetition 8
-	  k = frameP&3;
-	  AssertFatal(N_RB_DL>15,"SIB1-BR repetition 8 not allowed for N_RB_DL= %d\n",N_RB_DL);
-	  if      ((foffset==0) && (subframeP!=(4+sfoffset))) continue;
-	  else if ((foffset==1) && (subframeP!=((9+sfoffset)%10))) continue;
-	  break;
-	case 2: // repetition 16
-	  k = ((10*frameP) + subframeP)&3;
-	  AssertFatal(N_RB_DL>15,"SIB1-BR repetition 16 not allowed for N_RB_DL= %d\n",N_RB_DL);
-	  if      ((sfoffset == 1) && ((subframeP!=0)||(subframeP!=5))) continue;
-	  else if ((sfoffset == 0) && (foffset==0) && (subframeP!=4) && (subframeP!=9)) continue;
-	  else if ((sfoffset == 0) && (foffset==1) && (subframeP!=0) && (subframeP!=9)) continue;
-	  break;
-      }
-    // if we get here we have to schedule SIB1_BR in this frame/subframe
-
-    // keep counter of SIB1_BR repetitions in 8 frame period to choose narrowband on which to transmit
-    if ((frameP&7) == 0) cc->SIB1_BR_cnt=0;
-    else                 cc->SIB1_BR_cnt++;
-
-    // Frequency-domain scheduling
-    switch (N_RB_DL) {
-    case 6:
-    case 15:
-    default:
-      m=1;
-      n_NB=0;
-      N_S_NB=0;
-      Sj=NULL;
-      break;
-    case 25:
-      m=2;
-      N_S_NB = 2;
-      Sj = Sj25;
-      break;
-    case 50:
-      m=2;
-      N_S_NB = 6;
-      Sj = Sj50; 
-      break;
-    case 75:
-      m=4;
-      N_S_NB = 10;
-      Sj = Sj75;
-      break;
-    case 100:
-      m=4;
-      N_S_NB = 14;
-      Sj = Sj100;
-      break;
-    }
-    // Note: definition of k above and rvidx from 36.321 section 5.3.1
-    rvidx = (((3*k)>>1) + (k&1))&3;
-    
-    i = cc->SIB1_BR_cnt & (m-1);
-    
-    n_NB =  Sj[((cc->physCellId % N_S_NB) + (i*N_S_NB/m))%N_S_NB];
-
-    
-    bcch_sdu_length = mac_rrc_data_req(module_idP,
-                                       CC_id,
-				       frameP,
-                                       BCCH_SIB1_BR,1,
-                                       &cc->BCCH_BR_pdu[0].payload[0],
-                                       1,
-                                       module_idP,
-                                       0); // not used in this case
-
-    AssertFatal(cc->mib->message.schedulingInfoSIB1_BR_r13<19,"schedulingInfoSIB1_BR_r13 %d > 18\n",
-		(int)cc->mib->message.schedulingInfoSIB1_BR_r13);
-
-    AssertFatal(bcch_sdu_length>0,"RRC returned 0 bytes for SIB1-BR\n");
-
-    TBS = SIB1_BR_TBS_table[(cc->mib->message.schedulingInfoSIB1_BR_r13-1)/3]>>3;
-
-    AssertFatal(bcch_sdu_length <= TBS, "length returned by RRC %d is not compatible with the TBS %d from MIB\n",bcch_sdu_length,TBS);
-
-    if ((frameP&1023) < 200) LOG_D(MAC,"[eNB %d] Frame %d Subframe %d: SIB1_BR->DLSCH CC_id %d, Received %d bytes, scheduling on NB %d (i %d,m %d,N_S_NB %d)  rvidx %d\n",module_idP,frameP,subframeP,CC_id,bcch_sdu_length,n_NB,i,m,N_S_NB,rvidx);
-    
-    // allocate all 6 PRBs in narrowband for SIB1_BR
-
-    first_rb = narrowband_to_first_rb(cc,n_NB);
-
-    vrb_map[first_rb] = 1;
-    vrb_map[first_rb+1] = 1;
-    vrb_map[first_rb+2] = 1;
-    vrb_map[first_rb+3] = 1;
-    vrb_map[first_rb+4] = 1;
-    vrb_map[first_rb+5] = 1;
-    
-    dl_config_pdu                                                                  = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
-    memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-    dl_config_pdu->pdu_type                                                        = NFAPI_DL_CONFIG_DLSCH_PDU_TYPE; 
-    dl_config_pdu->pdu_size                                                        = (uint8_t)(2+sizeof(nfapi_dl_config_dlsch_pdu));
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.length                                 = TBS;
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index                              = eNB->pdu_index[CC_id];
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti                                   = 0xFFFF;
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type               = 2;   // format 1A/1B/1D
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = 0;   // localized
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_block_coding                  = getRIV(N_RB_DL,first_rb,6);
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation                             = 2; //QPSK
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.redundancy_version                     = rvidx;
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks                       = 1;// first block
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_block_to_codeword_swap_flag  = 0;
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_scheme                    = (cc->p_eNB==1 ) ? 0 : 1;
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_layers                       = 1;
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_subbands                     = 1;
-    //	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = ;
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ue_category_capacity                   = 1;
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa                                     = 4; // 0 dB
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.delta_power_offset_index               = 0;
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap                                   = 0;
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb                                   = get_subbandsize(cc->mib->message.dl_Bandwidth); // ignored
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_mode                      = (cc->p_eNB==1 ) ? 1 : 2;
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_prb_per_subband                 = 1;
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector                          = 1;
-    // Rel10 fields
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel10.pdsch_start                           = 3;
-    // Rel13 fields
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.ue_type                               = 1; // CEModeA UE
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.pdsch_payload_type                    = 0; // SIB1-BR
-    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.initial_transmission_sf_io            = 0xFFFF; // absolute SFx
-    
-    //	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.bf_vector                    = ; 
-    dl_req->number_pdu++;
-    
-    // Program TX Request
-    TX_req                                                                = &eNB->TX_req[CC_id].tx_request_body.tx_pdu_list[eNB->TX_req[CC_id].tx_request_body.number_of_pdus]; 
-    TX_req->pdu_length                                                    = bcch_sdu_length;
-    TX_req->pdu_index                                                     = eNB->pdu_index[CC_id]++;
-    TX_req->num_segments                                                  = 1;
-    TX_req->segments[0].segment_length                                    = bcch_sdu_length;
-    TX_req->segments[0].segment_data                                      = cc->BCCH_BR_pdu[0].payload;
-    eNB->TX_req[CC_id].tx_request_body.number_of_pdus++;
-    
-    
-    
-    if (opt_enabled == 1) {
-      trace_pdu(1,
-		&cc->BCCH_BR_pdu[0].payload[0],
-		bcch_sdu_length,
-		0xffff,
-		4,
-		0xffff,
-		eNB->frame,
-		eNB->subframe,
-		0,
-		0);
-      LOG_D(OPT,"[eNB %d][BCH] Frame %d trace pdu for CC_id %d rnti %x with size %d\n",
-	    module_idP, frameP, CC_id, 0xffff, bcch_sdu_length);
-    }
-    if (cc->tdd_Config!=NULL) { //TDD
-      LOG_D(MAC,"[eNB] Frame %d : Scheduling BCCH-BR 0->DLSCH (TDD) for CC_id %d SIB1-BR %d bytes\n",
-	    frameP,
-	    CC_id,
-	    bcch_sdu_length);
-    } else {
-      LOG_D(MAC,"[eNB] Frame %d : Scheduling BCCH-BR 0->DLSCH (FDD) for CC_id %d SIB1-BR %d bytes\n",
-	    frameP,
-	    CC_id,
-	    bcch_sdu_length);
-    }
+    int8_t bcch_sdu_length;
+    int CC_id;
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    COMMON_channels_t *cc;
+    uint8_t *vrb_map;
+    int first_rb = -1;
+    int N_RB_DL;
+    nfapi_dl_config_request_pdu_t *dl_config_pdu;
+    nfapi_tx_request_pdu_t *TX_req;
+    nfapi_dl_config_request_body_t *dl_req;
+    int m, i, N_S_NB;
+    int *Sj;
+    int n_NB = 0;
+    int TBS;
+    int k = 0, rvidx;
+
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+
+	cc = &eNB->common_channels[CC_id];
+	vrb_map = (void *) &cc->vrb_map;
+	N_RB_DL = to_prb(cc->mib->message.dl_Bandwidth);
+	dl_req = &eNB->DL_req[CC_id].dl_config_request_body;
+
+	int foffset = cc->physCellId & 1;
+	int sfoffset = (cc->tdd_Config == NULL) ? 0 : 1;
+
+	// Time-domain scheduling
+	if (cc->mib->message.schedulingInfoSIB1_BR_r13 == 0)
+	    continue;
+	else
+	    switch ((cc->mib->message.schedulingInfoSIB1_BR_r13 - 1) % 3) {
+	    case 0:		// repetition 4
+		k = (frameP >> 1) & 3;
+		if ((subframeP != (4 + sfoffset))
+		    || ((frameP & 1) != foffset))
+		    continue;
+		break;
+	    case 1:		// repetition 8
+		k = frameP & 3;
+		AssertFatal(N_RB_DL > 15,
+			    "SIB1-BR repetition 8 not allowed for N_RB_DL= %d\n",
+			    N_RB_DL);
+		if ((foffset == 0) && (subframeP != (4 + sfoffset)))
+		    continue;
+		else if ((foffset == 1)
+			 && (subframeP != ((9 + sfoffset) % 10)))
+		    continue;
+		break;
+	    case 2:		// repetition 16
+		k = ((10 * frameP) + subframeP) & 3;
+		AssertFatal(N_RB_DL > 15,
+			    "SIB1-BR repetition 16 not allowed for N_RB_DL= %d\n",
+			    N_RB_DL);
+		if ((sfoffset == 1)
+		    && ((subframeP != 0) || (subframeP != 5)))
+		    continue;
+		else if ((sfoffset == 0) && (foffset == 0)
+			 && (subframeP != 4) && (subframeP != 9))
+		    continue;
+		else if ((sfoffset == 0) && (foffset == 1)
+			 && (subframeP != 0) && (subframeP != 9))
+		    continue;
+		break;
+	    }
+	// if we get here we have to schedule SIB1_BR in this frame/subframe
+
+	// keep counter of SIB1_BR repetitions in 8 frame period to choose narrowband on which to transmit
+	if ((frameP & 7) == 0)
+	    cc->SIB1_BR_cnt = 0;
+	else
+	    cc->SIB1_BR_cnt++;
+
+	// Frequency-domain scheduling
+	switch (N_RB_DL) {
+	case 6:
+	case 15:
+	default:
+	    m = 1;
+	    n_NB = 0;
+	    N_S_NB = 0;
+	    Sj = NULL;
+	    break;
+	case 25:
+	    m = 2;
+	    N_S_NB = 2;
+	    Sj = Sj25;
+	    break;
+	case 50:
+	    m = 2;
+	    N_S_NB = 6;
+	    Sj = Sj50;
+	    break;
+	case 75:
+	    m = 4;
+	    N_S_NB = 10;
+	    Sj = Sj75;
+	    break;
+	case 100:
+	    m = 4;
+	    N_S_NB = 14;
+	    Sj = Sj100;
+	    break;
+	}
+	// Note: definition of k above and rvidx from 36.321 section 5.3.1
+	rvidx = (((3 * k) >> 1) + (k & 1)) & 3;
+
+	i = cc->SIB1_BR_cnt & (m - 1);
+
+	n_NB = Sj[((cc->physCellId % N_S_NB) + (i * N_S_NB / m)) % N_S_NB];
+
+
+	bcch_sdu_length = mac_rrc_data_req(module_idP, CC_id, frameP, BCCH_SIB1_BR, 1, &cc->BCCH_BR_pdu[0].payload[0], 1, module_idP, 0);	// not used in this case
+
+	AssertFatal(cc->mib->message.schedulingInfoSIB1_BR_r13 < 19,
+		    "schedulingInfoSIB1_BR_r13 %d > 18\n",
+		    (int) cc->mib->message.schedulingInfoSIB1_BR_r13);
+
+	AssertFatal(bcch_sdu_length > 0,
+		    "RRC returned 0 bytes for SIB1-BR\n");
+
+	TBS =
+	    SIB1_BR_TBS_table[(cc->mib->message.schedulingInfoSIB1_BR_r13 -
+			       1) / 3] >> 3;
+
+	AssertFatal(bcch_sdu_length <= TBS,
+		    "length returned by RRC %d is not compatible with the TBS %d from MIB\n",
+		    bcch_sdu_length, TBS);
+
+	if ((frameP & 1023) < 200)
+	    LOG_D(MAC,
+		  "[eNB %d] Frame %d Subframe %d: SIB1_BR->DLSCH CC_id %d, Received %d bytes, scheduling on NB %d (i %d,m %d,N_S_NB %d)  rvidx %d\n",
+		  module_idP, frameP, subframeP, CC_id, bcch_sdu_length,
+		  n_NB, i, m, N_S_NB, rvidx);
+
+	// allocate all 6 PRBs in narrowband for SIB1_BR
+
+	first_rb = narrowband_to_first_rb(cc, n_NB);
+
+	vrb_map[first_rb] = 1;
+	vrb_map[first_rb + 1] = 1;
+	vrb_map[first_rb + 2] = 1;
+	vrb_map[first_rb + 3] = 1;
+	vrb_map[first_rb + 4] = 1;
+	vrb_map[first_rb + 5] = 1;
+
+	dl_config_pdu = &dl_req->dl_config_pdu_list[dl_req->number_pdu];
+	memset((void *) dl_config_pdu, 0,
+	       sizeof(nfapi_dl_config_request_pdu_t));
+	dl_config_pdu->pdu_type = NFAPI_DL_CONFIG_DLSCH_PDU_TYPE;
+	dl_config_pdu->pdu_size =
+	    (uint8_t) (2 + sizeof(nfapi_dl_config_dlsch_pdu));
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.length = TBS;
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index =
+	    eNB->pdu_index[CC_id];
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti = 0xFFFF;
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type = 2;	// format 1A/1B/1D
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = 0;	// localized
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_block_coding =
+	    getRIV(N_RB_DL, first_rb, 6);
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation = 2;	//QPSK
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.redundancy_version = rvidx;
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks = 1;	// first block
+	dl_config_pdu->dlsch_pdu.
+	    dlsch_pdu_rel8.transport_block_to_codeword_swap_flag = 0;
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_scheme =
+	    (cc->p_eNB == 1) ? 0 : 1;
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_layers = 1;
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_subbands = 1;
+	//  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = ;
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ue_category_capacity = 1;
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa = 4;	// 0 dB
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.delta_power_offset_index =
+	    0;
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap = 0;
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb = get_subbandsize(cc->mib->message.dl_Bandwidth);	// ignored
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_mode =
+	    (cc->p_eNB == 1) ? 1 : 2;
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_prb_per_subband = 1;
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector = 1;
+	// Rel10 fields
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel10.pdsch_start = 3;
+	// Rel13 fields
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.ue_type = 1;	// CEModeA UE
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.pdsch_payload_type = 0;	// SIB1-BR
+	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.initial_transmission_sf_io = 0xFFFF;	// absolute SFx
+
+	//  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.bf_vector                    = ; 
+	dl_req->number_pdu++;
+
+	// Program TX Request
+	TX_req =
+	    &eNB->TX_req[CC_id].tx_request_body.tx_pdu_list[eNB->
+							    TX_req
+							    [CC_id].tx_request_body.number_of_pdus];
+	TX_req->pdu_length = bcch_sdu_length;
+	TX_req->pdu_index = eNB->pdu_index[CC_id]++;
+	TX_req->num_segments = 1;
+	TX_req->segments[0].segment_length = bcch_sdu_length;
+	TX_req->segments[0].segment_data = cc->BCCH_BR_pdu[0].payload;
+	eNB->TX_req[CC_id].tx_request_body.number_of_pdus++;
+
+
+
+	if (opt_enabled == 1) {
+	    trace_pdu(1,
+		      &cc->BCCH_BR_pdu[0].payload[0],
+		      bcch_sdu_length,
+		      0xffff, 4, 0xffff, eNB->frame, eNB->subframe, 0, 0);
+	    LOG_D(OPT,
+		  "[eNB %d][BCH] Frame %d trace pdu for CC_id %d rnti %x with size %d\n",
+		  module_idP, frameP, CC_id, 0xffff, bcch_sdu_length);
+	}
+	if (cc->tdd_Config != NULL) {	//TDD
+	    LOG_D(MAC,
+		  "[eNB] Frame %d : Scheduling BCCH-BR 0->DLSCH (TDD) for CC_id %d SIB1-BR %d bytes\n",
+		  frameP, CC_id, bcch_sdu_length);
+	} else {
+	    LOG_D(MAC,
+		  "[eNB] Frame %d : Scheduling BCCH-BR 0->DLSCH (FDD) for CC_id %d SIB1-BR %d bytes\n",
+		  frameP, CC_id, bcch_sdu_length);
+	}
 
-  }
+    }
 
-  return;
+    return;
 }
 
-int si_WindowLength_BR_r13tab[SystemInformationBlockType1_v1310_IEs__bandwidthReducedAccessRelatedInfo_r13__si_WindowLength_BR_r13_spare] = 
-  {20,40,60,80,120,160,200};
-int si_TBS_r13tab[SchedulingInfo_BR_r13__si_TBS_r13_b936+1] = {152,208,256,328,408,504,600,712,808,936};
+int si_WindowLength_BR_r13tab
+    [SystemInformationBlockType1_v1310_IEs__bandwidthReducedAccessRelatedInfo_r13__si_WindowLength_BR_r13_spare]
+= { 20, 40, 60, 80, 120, 160, 200 };
+int si_TBS_r13tab[SchedulingInfo_BR_r13__si_TBS_r13_b936 + 1] =
+    { 152, 208, 256, 328, 408, 504, 600, 712, 808, 936 };
 
 //------------------------------------------------------------------------------
 void
-schedule_SI_BR(
-	       module_id_t   module_idP,
-	       frame_t       frameP,
-	       sub_frame_t   subframeP)
-  
+schedule_SI_BR(module_id_t module_idP, frame_t frameP,
+	       sub_frame_t subframeP)
 //------------------------------------------------------------------------------
 {
 
-  int8_t                                  bcch_sdu_length;
-  int                                     CC_id;
-  eNB_MAC_INST                            *eNB = RC.mac[module_idP];
-  COMMON_channels_t                       *cc;
-  uint8_t                                 *vrb_map;
-  int                                     first_rb = -1;
-  int                                     N_RB_DL;
-  nfapi_dl_config_request_pdu_t           *dl_config_pdu;
-  nfapi_tx_request_pdu_t                  *TX_req;
-  nfapi_dl_config_request_body_t          *dl_req;
-  int                                     i;
-  int                                     rvidx;
-  int                                     absSF = (frameP*10)+subframeP;
-
-
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-
-    cc              = &eNB->common_channels[CC_id];
-    vrb_map         = (void*)&cc->vrb_map;
-    N_RB_DL         = to_prb(cc->mib->message.dl_Bandwidth);
-    dl_req          = &eNB->DL_req[CC_id].dl_config_request_body;
-
-    // Time-domain scheduling
-    if (cc->mib->message.schedulingInfoSIB1_BR_r13==0) continue;
-    else  {
-
-
-      AssertFatal(cc->sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13!=NULL,
-		  "sib_v13ext->bandwidthReducedAccessRelatedInfo_r13 is null\n");
-
-      SchedulingInfoList_BR_r13_t *schedulingInfoList_BR_r13 = cc->sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->schedulingInfoList_BR_r13;
-      AssertFatal(schedulingInfoList_BR_r13!=NULL,
-		  "sib_v13ext->schedulingInfoList_BR_r13 is null\n");
-
-      SchedulingInfoList_t *schedulingInfoList = cc->schedulingInfoList;
-      AssertFatal(schedulingInfoList_BR_r13->list.count==schedulingInfoList->list.count,
-		  "schedulingInfolist_BR.r13->list.count %d != schedulingInfoList.list.count %d\n",
-		  schedulingInfoList_BR_r13->list.count,schedulingInfoList->list.count); 
-
-      AssertFatal(cc->sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->si_WindowLength_BR_r13<=SystemInformationBlockType1_v1310_IEs__bandwidthReducedAccessRelatedInfo_r13__si_WindowLength_BR_r13_ms200,
-		  "si_WindowLength_BR_r13 %d > %d\n",
-		  (int)cc->sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->si_WindowLength_BR_r13,
-		  SystemInformationBlockType1_v1310_IEs__bandwidthReducedAccessRelatedInfo_r13__si_WindowLength_BR_r13_ms200);
-
-      // check that SI frequency-hopping is disabled
-      AssertFatal(cc->sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->si_HoppingConfigCommon_r13==SystemInformationBlockType1_v1310_IEs__bandwidthReducedAccessRelatedInfo_r13__si_HoppingConfigCommon_r13_off,
-		  "Deactivate SI_HoppingConfigCommon_r13 in configuration file, not supported for now\n");
-      long si_WindowLength_BR_r13   = si_WindowLength_BR_r13tab[cc->sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->si_WindowLength_BR_r13];
-
-      long si_RepetitionPattern_r13 = cc->sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->si_RepetitionPattern_r13;
-      AssertFatal(si_RepetitionPattern_r13<=SystemInformationBlockType1_v1310_IEs__bandwidthReducedAccessRelatedInfo_r13__si_RepetitionPattern_r13_every8thRF,
-		  "si_RepetitionPattern_r13 %d > %d\n",
-		  (int)si_RepetitionPattern_r13,
-		  SystemInformationBlockType1_v1310_IEs__bandwidthReducedAccessRelatedInfo_r13__si_RepetitionPattern_r13_every8thRF);
-      // cycle through SIB list
-
-      for (i=0;i<schedulingInfoList_BR_r13->list.count;i++) {
-	long si_Periodicity           = schedulingInfoList->list.array[i]->si_Periodicity;
-	long si_Narrowband_r13        = schedulingInfoList_BR_r13->list.array[i]->si_Narrowband_r13;
-	long si_TBS_r13               = si_TBS_r13tab[schedulingInfoList_BR_r13->list.array[i]->si_TBS_r13];
-
-	// check if the SI is to be scheduled now
-	int period_in_sf              = 80<<si_Periodicity; // 2^i * 80 subframes, note: si_Periodicity is 2^i * 80ms
-	int sf_mod_period             = absSF%period_in_sf;
-	int k                         = sf_mod_period&3;
-	// Note: definition of k and rvidx from 36.321 section 5.3.1
-	rvidx = (((3*k)>>1) + (k&1))&3;
-	
-        if ((sf_mod_period < si_WindowLength_BR_r13) &&
-	    ((frameP&(((1<<si_RepetitionPattern_r13)-1)))==0)) { // this SIB is to be scheduled
-
-	  bcch_sdu_length = mac_rrc_data_req(module_idP,
-					     CC_id,
-					     frameP,
-					     BCCH_SI_BR+i,1,
-					     &cc->BCCH_BR_pdu[i+1].payload[0],
-					     1,
-					     module_idP,
-					     0); // not used in this case
-	  
-	  AssertFatal(bcch_sdu_length>0,"RRC returned 0 bytes for SI-BR %d\n",i);
-	  
-	  if (bcch_sdu_length > 0) {
-	    AssertFatal(bcch_sdu_length <= (si_TBS_r13>>3),
-			"RRC provided bcch with length %d > %d (si_TBS_r13 %d)\n",
-			bcch_sdu_length,(int)(si_TBS_r13>>3),(int)schedulingInfoList_BR_r13->list.array[i]->si_TBS_r13);
-
-	    // allocate all 6 PRBs in narrowband for SIB1_BR
-
-	    // check that SIB1 didn't take this narrowband
-	    if (vrb_map[first_rb] > 0) continue;
-
-	    first_rb = narrowband_to_first_rb(cc,si_Narrowband_r13-1);
-	    vrb_map[first_rb]   = 1;
-	    vrb_map[first_rb+1] = 1;
-	    vrb_map[first_rb+2] = 1;
-	    vrb_map[first_rb+4] = 1;
-	    vrb_map[first_rb+5] = 1;
-
-	    if ((frameP&1023) < 200) LOG_D(MAC,"[eNB %d] Frame %d Subframe %d: SI_BR->DLSCH CC_id %d, Narrowband %d rvidx %d (sf_mod_period %d : si_WindowLength_BR_r13 %d : si_RepetitionPattern_r13 %d) bcch_sdu_length %d\n",
-					   module_idP,frameP,subframeP,CC_id,(int)si_Narrowband_r13-1,rvidx,
-					   sf_mod_period,(int)si_WindowLength_BR_r13,(int)si_RepetitionPattern_r13,
-					   bcch_sdu_length);	    
-
-
-
-	    
-	    dl_config_pdu                                                                  = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
-	    memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-	    dl_config_pdu->pdu_type                                                        = NFAPI_DL_CONFIG_DLSCH_PDU_TYPE; 
-	    dl_config_pdu->pdu_size                                                        = (uint8_t)(2+sizeof(nfapi_dl_config_dlsch_pdu));
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.length                                 = si_TBS_r13>>3;
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index                              = eNB->pdu_index[CC_id];
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti                                   = 0xFFFF;
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type               = 2;   // format 1A/1B/1D
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = 0;   // localized
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_block_coding                  = getRIV(N_RB_DL,first_rb,6);
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation                             = 2; //QPSK
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.redundancy_version                     = rvidx;
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks                       = 1;// first block
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_block_to_codeword_swap_flag  = 0;
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_scheme                    = (cc->p_eNB==1 ) ? 0 : 1;
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_layers                       = 1;
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_subbands                     = 1;
-	    //	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = ;
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ue_category_capacity                   = 1;
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa                                     = 4; // 0 dB
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.delta_power_offset_index               = 0;
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap                                   = 0;
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb                                   = get_subbandsize(cc->mib->message.dl_Bandwidth); // ignored
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_mode                      = (cc->p_eNB==1 ) ? 1 : 2;
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_prb_per_subband                 = 1;
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector                          = 1;
-	    // Rel10 fields (for PDSCH starting symbol)
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel10.pdsch_start                           = cc[CC_id].sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->startSymbolBR_r13;
-	    // Rel13 fields
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.ue_type                               = 1; // CEModeA UE
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.pdsch_payload_type                    = 1; // SI-BR
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.initial_transmission_sf_io            = absSF - sf_mod_period; 
-	    
-	    //	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.bf_vector                    = ; 
-	    dl_req->number_pdu++;
-	    
-	    // Program TX Request
-	    TX_req                                                                = &eNB->TX_req[CC_id].tx_request_body.tx_pdu_list[eNB->TX_req[CC_id].tx_request_body.number_of_pdus]; 
-	    TX_req->pdu_length                                                    = bcch_sdu_length;
-	    TX_req->pdu_index                                                     = eNB->pdu_index[CC_id]++;
-	    TX_req->num_segments                                                  = 1;
-	    TX_req->segments[0].segment_length                                    = bcch_sdu_length;
-	    TX_req->segments[0].segment_data                                      = cc->BCCH_BR_pdu[i+1].payload;
-	    eNB->TX_req[CC_id].tx_request_body.number_of_pdus++;
-	    
-	    if (opt_enabled == 1) {
-	      trace_pdu(1,
-			&cc->BCCH_BR_pdu[i+1].payload[0],
-			bcch_sdu_length,
-			0xffff,
-			4,
-			0xffff,
-			eNB->frame,
-			eNB->subframe,
-			0,
-			0);
-	      LOG_D(OPT,"[eNB %d][BCH] Frame %d trace pdu for CC_id %d rnti %x with size %d\n",
-		    module_idP, frameP, CC_id, 0xffff, bcch_sdu_length);
-	    }
-	    if (cc->tdd_Config!=NULL) { //TDD
-	      LOG_D(MAC,"[eNB] Frame %d : Scheduling BCCH-BR %d->DLSCH (TDD) for CC_id %d SI-BR %d bytes\n",
-		    frameP,i,
-		    CC_id,
-		    bcch_sdu_length);
-	    } else {
-	      LOG_D(MAC,"[eNB] Frame %d : Scheduling BCCH-BR %d->DLSCH (FDD) for CC_id %d SI-BR %d bytes\n",
-		    frameP,i,
-		    CC_id,
-		    bcch_sdu_length);
-	    }
-	  }
-	} // scheduling in current frame/subframe
-      } //for SI List
-    } // eMTC is activated
-  } // CC_id
-  return;
+    int8_t bcch_sdu_length;
+    int CC_id;
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    COMMON_channels_t *cc;
+    uint8_t *vrb_map;
+    int first_rb = -1;
+    int N_RB_DL;
+    nfapi_dl_config_request_pdu_t *dl_config_pdu;
+    nfapi_tx_request_pdu_t *TX_req;
+    nfapi_dl_config_request_body_t *dl_req;
+    int i;
+    int rvidx;
+    int absSF = (frameP * 10) + subframeP;
+
+
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+
+	cc = &eNB->common_channels[CC_id];
+	vrb_map = (void *) &cc->vrb_map;
+	N_RB_DL = to_prb(cc->mib->message.dl_Bandwidth);
+	dl_req = &eNB->DL_req[CC_id].dl_config_request_body;
+
+	// Time-domain scheduling
+	if (cc->mib->message.schedulingInfoSIB1_BR_r13 == 0)
+	    continue;
+	else {
+
+
+	    AssertFatal(cc->
+			sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13
+			!= NULL,
+			"sib_v13ext->bandwidthReducedAccessRelatedInfo_r13 is null\n");
+
+	    SchedulingInfoList_BR_r13_t *schedulingInfoList_BR_r13 =
+		cc->sib1_v13ext->
+		bandwidthReducedAccessRelatedInfo_r13->schedulingInfoList_BR_r13;
+	    AssertFatal(schedulingInfoList_BR_r13 != NULL,
+			"sib_v13ext->schedulingInfoList_BR_r13 is null\n");
+
+	    SchedulingInfoList_t *schedulingInfoList =
+		cc->schedulingInfoList;
+	    AssertFatal(schedulingInfoList_BR_r13->list.count ==
+			schedulingInfoList->list.count,
+			"schedulingInfolist_BR.r13->list.count %d != schedulingInfoList.list.count %d\n",
+			schedulingInfoList_BR_r13->list.count,
+			schedulingInfoList->list.count);
+
+	    AssertFatal(cc->
+			sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->si_WindowLength_BR_r13
+			<=
+			SystemInformationBlockType1_v1310_IEs__bandwidthReducedAccessRelatedInfo_r13__si_WindowLength_BR_r13_ms200,
+			"si_WindowLength_BR_r13 %d > %d\n",
+			(int) cc->
+			sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->si_WindowLength_BR_r13,
+			SystemInformationBlockType1_v1310_IEs__bandwidthReducedAccessRelatedInfo_r13__si_WindowLength_BR_r13_ms200);
+
+	    // check that SI frequency-hopping is disabled
+	    AssertFatal(cc->
+			sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->si_HoppingConfigCommon_r13
+			==
+			SystemInformationBlockType1_v1310_IEs__bandwidthReducedAccessRelatedInfo_r13__si_HoppingConfigCommon_r13_off,
+			"Deactivate SI_HoppingConfigCommon_r13 in configuration file, not supported for now\n");
+	    long si_WindowLength_BR_r13 =
+		si_WindowLength_BR_r13tab[cc->
+					  sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->si_WindowLength_BR_r13];
+
+	    long si_RepetitionPattern_r13 =
+		cc->sib1_v13ext->
+		bandwidthReducedAccessRelatedInfo_r13->si_RepetitionPattern_r13;
+	    AssertFatal(si_RepetitionPattern_r13 <=
+			SystemInformationBlockType1_v1310_IEs__bandwidthReducedAccessRelatedInfo_r13__si_RepetitionPattern_r13_every8thRF,
+			"si_RepetitionPattern_r13 %d > %d\n",
+			(int) si_RepetitionPattern_r13,
+			SystemInformationBlockType1_v1310_IEs__bandwidthReducedAccessRelatedInfo_r13__si_RepetitionPattern_r13_every8thRF);
+	    // cycle through SIB list
+
+	    for (i = 0; i < schedulingInfoList_BR_r13->list.count; i++) {
+		long si_Periodicity =
+		    schedulingInfoList->list.array[i]->si_Periodicity;
+		long si_Narrowband_r13 =
+		    schedulingInfoList_BR_r13->list.array[i]->
+		    si_Narrowband_r13;
+		long si_TBS_r13 =
+		    si_TBS_r13tab[schedulingInfoList_BR_r13->
+				  list.array[i]->si_TBS_r13];
+
+		// check if the SI is to be scheduled now
+		int period_in_sf = 80 << si_Periodicity;	// 2^i * 80 subframes, note: si_Periodicity is 2^i * 80ms
+		int sf_mod_period = absSF % period_in_sf;
+		int k = sf_mod_period & 3;
+		// Note: definition of k and rvidx from 36.321 section 5.3.1
+		rvidx = (((3 * k) >> 1) + (k & 1)) & 3;
+
+		if ((sf_mod_period < si_WindowLength_BR_r13)
+		    && ((frameP & (((1 << si_RepetitionPattern_r13) - 1))) == 0)) {	// this SIB is to be scheduled
+
+		    bcch_sdu_length = mac_rrc_data_req(module_idP, CC_id, frameP, BCCH_SI_BR + i, 1, &cc->BCCH_BR_pdu[i + 1].payload[0], 1, module_idP, 0);	// not used in this case
+
+		    AssertFatal(bcch_sdu_length > 0,
+				"RRC returned 0 bytes for SI-BR %d\n", i);
+
+		    if (bcch_sdu_length > 0) {
+			AssertFatal(bcch_sdu_length <= (si_TBS_r13 >> 3),
+				    "RRC provided bcch with length %d > %d (si_TBS_r13 %d)\n",
+				    bcch_sdu_length,
+				    (int) (si_TBS_r13 >> 3),
+				    (int) schedulingInfoList_BR_r13->
+				    list.array[i]->si_TBS_r13);
+
+			// allocate all 6 PRBs in narrowband for SIB1_BR
+
+			// check that SIB1 didn't take this narrowband
+			if (vrb_map[first_rb] > 0)
+			    continue;
+
+			first_rb =
+			    narrowband_to_first_rb(cc,
+						   si_Narrowband_r13 - 1);
+			vrb_map[first_rb] = 1;
+			vrb_map[first_rb + 1] = 1;
+			vrb_map[first_rb + 2] = 1;
+			vrb_map[first_rb + 4] = 1;
+			vrb_map[first_rb + 5] = 1;
+
+			if ((frameP & 1023) < 200)
+			    LOG_D(MAC,
+				  "[eNB %d] Frame %d Subframe %d: SI_BR->DLSCH CC_id %d, Narrowband %d rvidx %d (sf_mod_period %d : si_WindowLength_BR_r13 %d : si_RepetitionPattern_r13 %d) bcch_sdu_length %d\n",
+				  module_idP, frameP, subframeP, CC_id,
+				  (int) si_Narrowband_r13 - 1, rvidx,
+				  sf_mod_period,
+				  (int) si_WindowLength_BR_r13,
+				  (int) si_RepetitionPattern_r13,
+				  bcch_sdu_length);
+
+
+
+
+			dl_config_pdu =
+			    &dl_req->dl_config_pdu_list[dl_req->
+							number_pdu];
+			memset((void *) dl_config_pdu, 0,
+			       sizeof(nfapi_dl_config_request_pdu_t));
+			dl_config_pdu->pdu_type =
+			    NFAPI_DL_CONFIG_DLSCH_PDU_TYPE;
+			dl_config_pdu->pdu_size =
+			    (uint8_t) (2 +
+				       sizeof(nfapi_dl_config_dlsch_pdu));
+			dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.length =
+			    si_TBS_r13 >> 3;
+			dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index =
+			    eNB->pdu_index[CC_id];
+			dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti =
+			    0xFFFF;
+			dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type = 2;	// format 1A/1B/1D
+			dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = 0;	// localized
+			dl_config_pdu->dlsch_pdu.
+			    dlsch_pdu_rel8.resource_block_coding =
+			    getRIV(N_RB_DL, first_rb, 6);
+			dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation = 2;	//QPSK
+			dl_config_pdu->dlsch_pdu.
+			    dlsch_pdu_rel8.redundancy_version = rvidx;
+			dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks = 1;	// first block
+			dl_config_pdu->dlsch_pdu.
+			    dlsch_pdu_rel8.transport_block_to_codeword_swap_flag
+			    = 0;
+			dl_config_pdu->dlsch_pdu.
+			    dlsch_pdu_rel8.transmission_scheme =
+			    (cc->p_eNB == 1) ? 0 : 1;
+			dl_config_pdu->dlsch_pdu.
+			    dlsch_pdu_rel8.number_of_layers = 1;
+			dl_config_pdu->dlsch_pdu.
+			    dlsch_pdu_rel8.number_of_subbands = 1;
+			//  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = ;
+			dl_config_pdu->dlsch_pdu.
+			    dlsch_pdu_rel8.ue_category_capacity = 1;
+			dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa = 4;	// 0 dB
+			dl_config_pdu->dlsch_pdu.
+			    dlsch_pdu_rel8.delta_power_offset_index = 0;
+			dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap = 0;
+			dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb = get_subbandsize(cc->mib->message.dl_Bandwidth);	// ignored
+			dl_config_pdu->dlsch_pdu.
+			    dlsch_pdu_rel8.transmission_mode =
+			    (cc->p_eNB == 1) ? 1 : 2;
+			dl_config_pdu->dlsch_pdu.
+			    dlsch_pdu_rel8.num_bf_prb_per_subband = 1;
+			dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+			    num_bf_vector = 1;
+			// Rel10 fields (for PDSCH starting symbol)
+			dl_config_pdu->dlsch_pdu.dlsch_pdu_rel10.
+			    pdsch_start =
+			    cc[CC_id].
+			    sib1_v13ext->bandwidthReducedAccessRelatedInfo_r13->startSymbolBR_r13;
+			// Rel13 fields
+			dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.ue_type = 1;	// CEModeA UE
+			dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.pdsch_payload_type = 1;	// SI-BR
+			dl_config_pdu->dlsch_pdu.
+			    dlsch_pdu_rel13.initial_transmission_sf_io =
+			    absSF - sf_mod_period;
+
+			//  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.bf_vector                    = ; 
+			dl_req->number_pdu++;
+
+			// Program TX Request
+			TX_req =
+			    &eNB->TX_req[CC_id].
+			    tx_request_body.tx_pdu_list[eNB->TX_req[CC_id].
+							tx_request_body.number_of_pdus];
+			TX_req->pdu_length = bcch_sdu_length;
+			TX_req->pdu_index = eNB->pdu_index[CC_id]++;
+			TX_req->num_segments = 1;
+			TX_req->segments[0].segment_length =
+			    bcch_sdu_length;
+			TX_req->segments[0].segment_data =
+			    cc->BCCH_BR_pdu[i + 1].payload;
+			eNB->TX_req[CC_id].tx_request_body.
+			    number_of_pdus++;
+
+			if (opt_enabled == 1) {
+			    trace_pdu(1,
+				      &cc->BCCH_BR_pdu[i + 1].payload[0],
+				      bcch_sdu_length,
+				      0xffff,
+				      4,
+				      0xffff, eNB->frame, eNB->subframe, 0,
+				      0);
+			    LOG_D(OPT,
+				  "[eNB %d][BCH] Frame %d trace pdu for CC_id %d rnti %x with size %d\n",
+				  module_idP, frameP, CC_id, 0xffff,
+				  bcch_sdu_length);
+			}
+			if (cc->tdd_Config != NULL) {	//TDD
+			    LOG_D(MAC,
+				  "[eNB] Frame %d : Scheduling BCCH-BR %d->DLSCH (TDD) for CC_id %d SI-BR %d bytes\n",
+				  frameP, i, CC_id, bcch_sdu_length);
+			} else {
+			    LOG_D(MAC,
+				  "[eNB] Frame %d : Scheduling BCCH-BR %d->DLSCH (FDD) for CC_id %d SI-BR %d bytes\n",
+				  frameP, i, CC_id, bcch_sdu_length);
+			}
+		    }
+		}		// scheduling in current frame/subframe
+	    }			//for SI List
+	}			// eMTC is activated
+    }				// CC_id
+    return;
 }
 #endif
 
-void schedule_mib(module_id_t   module_idP,
-		  frame_t       frameP,
-		  sub_frame_t   subframeP) {
-
-  eNB_MAC_INST                   *eNB = RC.mac[module_idP];
-  COMMON_channels_t              *cc;
-  nfapi_dl_config_request_pdu_t  *dl_config_pdu;
-  nfapi_tx_request_pdu_t         *TX_req;
-  int mib_sdu_length;
-  int CC_id;
-  nfapi_dl_config_request_body_t *dl_req;
-
-  AssertFatal(subframeP==0,"Subframe must be 0\n");
-  AssertFatal((frameP&3)==0,"Frame must be a multiple of 4\n");
-
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-
-    dl_req          = &eNB->DL_req[CC_id].dl_config_request_body;
-    cc              = &eNB->common_channels[CC_id];
-
-    mib_sdu_length = mac_rrc_data_req(module_idP,
-				      CC_id,
-				      frameP,
-				      MIBCH,1,
-				      &cc->MIB_pdu.payload[0],
-				      1,
-				      module_idP,
-				      0); // not used in this case
-
-    LOG_D(MAC,"Frame %d, subframe %d: BCH PDU length %d\n",
-	  frameP,subframeP,mib_sdu_length);
-
-    if (mib_sdu_length > 0) {
-
-      LOG_D(MAC,"Frame %d, subframe %d: Adding BCH PDU in position %d (length %d)\n",
-	    frameP,subframeP,dl_req->number_pdu,mib_sdu_length);
-
-      if ((frameP&1023) < 40) LOG_D(MAC,"[eNB %d] Frame %d : MIB->BCH  CC_id %d, Received %d bytes (cc->mib->message.schedulingInfoSIB1_BR_r13 %d)\n",module_idP,frameP,CC_id,mib_sdu_length,(int)cc->mib->message.schedulingInfoSIB1_BR_r13);
-
-      dl_config_pdu                                                         = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
-      memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-      dl_config_pdu->pdu_type                                               = NFAPI_DL_CONFIG_BCH_PDU_TYPE,
-      dl_config_pdu->pdu_size                                               = 2+sizeof(nfapi_dl_config_bch_pdu);
-      dl_config_pdu->bch_pdu.bch_pdu_rel8.length                            = mib_sdu_length;
-      dl_config_pdu->bch_pdu.bch_pdu_rel8.pdu_index                         = eNB->pdu_index[CC_id];
-      dl_config_pdu->bch_pdu.bch_pdu_rel8.transmission_power                = 6000;
-      dl_req->number_pdu++;
-
-      LOG_D(MAC,"eNB->DL_req[0].number_pdu %d (%p)\n",
-	    dl_req->number_pdu,&dl_req->number_pdu);
-      // DL request
-
-      TX_req                                                                = &eNB->TX_req[CC_id].tx_request_body.tx_pdu_list[eNB->TX_req[CC_id].tx_request_body.number_of_pdus]; 
-      TX_req->pdu_length                                                    = 3;
-      TX_req->pdu_index                                                     = eNB->pdu_index[CC_id]++;
-      TX_req->num_segments                                                  = 1;
-      TX_req->segments[0].segment_length                                    = 0;
-      TX_req->segments[0].segment_data                                      = cc[CC_id].MIB_pdu.payload;
-      eNB->TX_req[CC_id].tx_request_body.number_of_pdus++;
+void
+schedule_mib(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP)
+{
+
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    COMMON_channels_t *cc;
+    nfapi_dl_config_request_pdu_t *dl_config_pdu;
+    nfapi_tx_request_pdu_t *TX_req;
+    int mib_sdu_length;
+    int CC_id;
+    nfapi_dl_config_request_body_t *dl_req;
+
+    AssertFatal(subframeP == 0, "Subframe must be 0\n");
+    AssertFatal((frameP & 3) == 0, "Frame must be a multiple of 4\n");
+
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+
+	dl_req = &eNB->DL_req[CC_id].dl_config_request_body;
+	cc = &eNB->common_channels[CC_id];
+
+	mib_sdu_length = mac_rrc_data_req(module_idP, CC_id, frameP, MIBCH, 1, &cc->MIB_pdu.payload[0], 1, module_idP, 0);	// not used in this case
+
+	LOG_D(MAC, "Frame %d, subframe %d: BCH PDU length %d\n",
+	      frameP, subframeP, mib_sdu_length);
+
+	if (mib_sdu_length > 0) {
+
+	    LOG_D(MAC,
+		  "Frame %d, subframe %d: Adding BCH PDU in position %d (length %d)\n",
+		  frameP, subframeP, dl_req->number_pdu, mib_sdu_length);
+
+	    if ((frameP & 1023) < 40)
+		LOG_D(MAC,
+		      "[eNB %d] Frame %d : MIB->BCH  CC_id %d, Received %d bytes (cc->mib->message.schedulingInfoSIB1_BR_r13 %d)\n",
+		      module_idP, frameP, CC_id, mib_sdu_length,
+		      (int) cc->mib->message.schedulingInfoSIB1_BR_r13);
+
+	    dl_config_pdu =
+		&dl_req->dl_config_pdu_list[dl_req->number_pdu];
+	    memset((void *) dl_config_pdu, 0,
+		   sizeof(nfapi_dl_config_request_pdu_t));
+	    dl_config_pdu->pdu_type =
+		NFAPI_DL_CONFIG_BCH_PDU_TYPE, dl_config_pdu->pdu_size =
+		2 + sizeof(nfapi_dl_config_bch_pdu);
+	    dl_config_pdu->bch_pdu.bch_pdu_rel8.length = mib_sdu_length;
+	    dl_config_pdu->bch_pdu.bch_pdu_rel8.pdu_index =
+		eNB->pdu_index[CC_id];
+	    dl_config_pdu->bch_pdu.bch_pdu_rel8.transmission_power = 6000;
+	    dl_req->number_pdu++;
+
+	    LOG_D(MAC, "eNB->DL_req[0].number_pdu %d (%p)\n",
+		  dl_req->number_pdu, &dl_req->number_pdu);
+	    // DL request
+
+	    TX_req =
+		&eNB->TX_req[CC_id].tx_request_body.tx_pdu_list[eNB->TX_req
+								[CC_id].tx_request_body.number_of_pdus];
+	    TX_req->pdu_length = 3;
+	    TX_req->pdu_index = eNB->pdu_index[CC_id]++;
+	    TX_req->num_segments = 1;
+	    TX_req->segments[0].segment_length = 0;
+	    TX_req->segments[0].segment_data = cc[CC_id].MIB_pdu.payload;
+	    eNB->TX_req[CC_id].tx_request_body.number_of_pdus++;
+	}
     }
-  }
 }
 
 
 //------------------------------------------------------------------------------
 void
-schedule_SI(
-  module_id_t   module_idP,
-  frame_t       frameP,
-  sub_frame_t   subframeP)
-
+schedule_SI(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP)
 //------------------------------------------------------------------------------
 {
 
-  int8_t                         bcch_sdu_length;
-  int                            mcs = -1;
-  int                            CC_id;
-  eNB_MAC_INST                   *eNB = RC.mac[module_idP];
-  COMMON_channels_t              *cc;
-  uint8_t                        *vrb_map;
-  int                            first_rb = -1;
-  int                            N_RB_DL;
-  nfapi_dl_config_request_pdu_t  *dl_config_pdu;
-  nfapi_tx_request_pdu_t         *TX_req;
-  nfapi_dl_config_request_body_t *dl_req;
-
-  start_meas(&eNB->schedule_si);
-  
-  // Only schedule LTE System Information in subframe 5
-  if (subframeP == 5) {
-
-    for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-      
-      cc              = &eNB->common_channels[CC_id];
-      vrb_map         = (void*)&cc->vrb_map;
-      N_RB_DL         = to_prb(cc->mib->message.dl_Bandwidth); 
-      dl_req          = &eNB->DL_req[CC_id].dl_config_request_body;
-      
-      
-      bcch_sdu_length = mac_rrc_data_req(module_idP,
-					 CC_id,
-					 frameP,
-					 BCCH,1,
-					 &cc->BCCH_pdu.payload[0],
-					 1,
-					 module_idP,
-					 0); // not used in this case
-      
-      if (bcch_sdu_length > 0) {
-	LOG_D(MAC,"[eNB %d] Frame %d : BCCH->DLSCH CC_id %d, Received %d bytes \n",module_idP,frameP,CC_id,bcch_sdu_length);
-	
-	// Allocate 4 PRBs in a random location
-	/*
-	  while (1) {
-	  first_rb = (unsigned char)(taus()%(PHY_vars_eNB_g[module_idP][CC_id]->frame_parms.N_RB_DL-4));
-	  if ((vrb_map[first_rb] != 1) && 
-	  (vrb_map[first_rb+1] != 1) && 
-	  (vrb_map[first_rb+2] != 1) && 
-	  (vrb_map[first_rb+3] != 1))
-	  break;
-	  }
-	*/
-	switch (N_RB_DL) {
-	case 6:
-	  first_rb = 0;
-	  break;
-	case 15:
-	  first_rb = 6;
-	  break;
-	case 25:
-	  first_rb = 11;
-	  break;
-	case 50:
-	  first_rb = 23;
-	  break;
-	case 100:
-	  first_rb = 48;
-	  break;
-	}
-	
-	vrb_map[first_rb] = 1;
-	vrb_map[first_rb+1] = 1;
-	vrb_map[first_rb+2] = 1;
-	vrb_map[first_rb+3] = 1;
-	
-	// Get MCS for length of SI, 3 PRBs
-	if (bcch_sdu_length <= 7) {
-	  mcs=0;
-	} else if (bcch_sdu_length <= 11) {
-	  mcs=1;
-	} else if (bcch_sdu_length <= 18) {
-	  mcs=2;
-	} else if (bcch_sdu_length <= 22) {
-	  mcs=3;
-	} else if (bcch_sdu_length <= 26) {
-	  mcs=4;
-	} else if (bcch_sdu_length <= 28) {
-	  mcs=5;
-	} else if (bcch_sdu_length <= 32) {
-	  mcs=6;
-	} else if (bcch_sdu_length <= 41) {
-	  mcs=7;
-	} else if (bcch_sdu_length <= 49) {
-	  mcs=8;
-	}
-	
-	
-	dl_config_pdu                                                         = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
-	memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-	dl_config_pdu->pdu_type                                               = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE; 
-	dl_config_pdu->pdu_size                                               = (uint8_t)(2+sizeof(nfapi_dl_config_dci_dl_pdu));
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format                  = NFAPI_DL_DCI_FORMAT_1A;
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level           = 4;
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti                        = 0xFFFF;
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type                   = 2;    // S-RNTI : see Table 4-10 from SCF082 - nFAPI specifications
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power          = 6000; // equal to RS power
-	
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.harq_process                = 0;
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tpc                         = 1; // no TPC
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.new_data_indicator_1        = 0;
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.mcs_1                       = mcs;
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.redundancy_version_1        = 0;
-	
-	dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_block_coding       = getRIV(N_RB_DL,first_rb,4);      
+    int8_t bcch_sdu_length;
+    int mcs = -1;
+    int CC_id;
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    COMMON_channels_t *cc;
+    uint8_t *vrb_map;
+    int first_rb = -1;
+    int N_RB_DL;
+    nfapi_dl_config_request_pdu_t *dl_config_pdu;
+    nfapi_tx_request_pdu_t *TX_req;
+    nfapi_dl_config_request_body_t *dl_req;
+
+    start_meas(&eNB->schedule_si);
+
+    // Only schedule LTE System Information in subframe 5
+    if (subframeP == 5) {
+
+	for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+
+	    cc = &eNB->common_channels[CC_id];
+	    vrb_map = (void *) &cc->vrb_map;
+	    N_RB_DL = to_prb(cc->mib->message.dl_Bandwidth);
+	    dl_req = &eNB->DL_req[CC_id].dl_config_request_body;
+
+
+	    bcch_sdu_length = mac_rrc_data_req(module_idP, CC_id, frameP, BCCH, 1, &cc->BCCH_pdu.payload[0], 1, module_idP, 0);	// not used in this case
+
+	    if (bcch_sdu_length > 0) {
+		LOG_D(MAC,
+		      "[eNB %d] Frame %d : BCCH->DLSCH CC_id %d, Received %d bytes \n",
+		      module_idP, frameP, CC_id, bcch_sdu_length);
+
+		// Allocate 4 PRBs in a random location
+		/*
+		   while (1) {
+		   first_rb = (unsigned char)(taus()%(PHY_vars_eNB_g[module_idP][CC_id]->frame_parms.N_RB_DL-4));
+		   if ((vrb_map[first_rb] != 1) && 
+		   (vrb_map[first_rb+1] != 1) && 
+		   (vrb_map[first_rb+2] != 1) && 
+		   (vrb_map[first_rb+3] != 1))
+		   break;
+		   }
+		 */
+		switch (N_RB_DL) {
+		case 6:
+		    first_rb = 0;
+		    break;
+		case 15:
+		    first_rb = 6;
+		    break;
+		case 25:
+		    first_rb = 11;
+		    break;
+		case 50:
+		    first_rb = 23;
+		    break;
+		case 100:
+		    first_rb = 48;
+		    break;
+		}
+
+		vrb_map[first_rb] = 1;
+		vrb_map[first_rb + 1] = 1;
+		vrb_map[first_rb + 2] = 1;
+		vrb_map[first_rb + 3] = 1;
+
+		// Get MCS for length of SI, 3 PRBs
+		if (bcch_sdu_length <= 7) {
+		    mcs = 0;
+		} else if (bcch_sdu_length <= 11) {
+		    mcs = 1;
+		} else if (bcch_sdu_length <= 18) {
+		    mcs = 2;
+		} else if (bcch_sdu_length <= 22) {
+		    mcs = 3;
+		} else if (bcch_sdu_length <= 26) {
+		    mcs = 4;
+		} else if (bcch_sdu_length <= 28) {
+		    mcs = 5;
+		} else if (bcch_sdu_length <= 32) {
+		    mcs = 6;
+		} else if (bcch_sdu_length <= 41) {
+		    mcs = 7;
+		} else if (bcch_sdu_length <= 49) {
+		    mcs = 8;
+		}
+
+
+		dl_config_pdu =
+		    &dl_req->dl_config_pdu_list[dl_req->number_pdu];
+		memset((void *) dl_config_pdu, 0,
+		       sizeof(nfapi_dl_config_request_pdu_t));
+		dl_config_pdu->pdu_type = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
+		dl_config_pdu->pdu_size =
+		    (uint8_t) (2 + sizeof(nfapi_dl_config_dci_dl_pdu));
+		dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format =
+		    NFAPI_DL_DCI_FORMAT_1A;
+		dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+		    aggregation_level = 4;
+		dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti = 0xFFFF;
+		dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type = 2;	// S-RNTI : see Table 4-10 from SCF082 - nFAPI specifications
+		dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power = 6000;	// equal to RS power
+
+		dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.harq_process = 0;
+		dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tpc = 1;	// no TPC
+		dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+		    new_data_indicator_1 = 0;
+		dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.mcs_1 = mcs;
+		dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+		    redundancy_version_1 = 0;
+
+		dl_config_pdu->dci_dl_pdu.
+		    dci_dl_pdu_rel8.resource_block_coding =
+		    getRIV(N_RB_DL, first_rb, 4);
+
+		// Rel10 fields
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel10.pdsch_start = 3;
+		// Rel13 fields
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.ue_type = 0;	// regular UE
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.pdsch_payload_type = 2;	// not BR
+		dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.initial_transmission_sf_io = 0xFFFF;	// absolute SF 
+
+		if (!CCE_allocation_infeasible
+		    (module_idP, CC_id, 0, subframeP,
+		     dl_config_pdu->dci_dl_pdu.
+		     dci_dl_pdu_rel8.aggregation_level, SI_RNTI)) {
+		    LOG_D(MAC,
+			  "Frame %d: Subframe %d : Adding common DCI for S_RNTI\n",
+			  frameP, subframeP);
+		    dl_req->number_dci++;
+		    dl_req->number_pdu++;
+		    dl_config_pdu =
+			&dl_req->dl_config_pdu_list[dl_req->number_pdu];
+		    memset((void *) dl_config_pdu, 0,
+			   sizeof(nfapi_dl_config_request_pdu_t));
+		    dl_config_pdu->pdu_type =
+			NFAPI_DL_CONFIG_DLSCH_PDU_TYPE;
+		    dl_config_pdu->pdu_size =
+			(uint8_t) (2 + sizeof(nfapi_dl_config_dlsch_pdu));
+		    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index =
+			eNB->pdu_index[CC_id];
+		    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti = 0xFFFF;
+		    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type = 2;	// format 1A/1B/1D
+		    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = 0;	// localized
+		    dl_config_pdu->dlsch_pdu.
+			dlsch_pdu_rel8.resource_block_coding =
+			getRIV(N_RB_DL, first_rb, 4);
+		    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation = 2;	//QPSK
+		    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+			redundancy_version = 0;
+		    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks = 1;	// first block
+		    dl_config_pdu->dlsch_pdu.
+			dlsch_pdu_rel8.transport_block_to_codeword_swap_flag
+			= 0;
+		    dl_config_pdu->dlsch_pdu.
+			dlsch_pdu_rel8.transmission_scheme =
+			(cc->p_eNB == 1) ? 0 : 1;
+		    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+			number_of_layers = 1;
+		    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+			number_of_subbands = 1;
+		    //    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = ;
+		    dl_config_pdu->dlsch_pdu.
+			dlsch_pdu_rel8.ue_category_capacity = 1;
+		    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa = 4;	// 0 dB
+		    dl_config_pdu->dlsch_pdu.
+			dlsch_pdu_rel8.delta_power_offset_index = 0;
+		    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap = 0;
+		    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb = get_subbandsize(cc->mib->message.dl_Bandwidth);	// ignored
+		    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+			transmission_mode = (cc->p_eNB == 1) ? 1 : 2;
+		    dl_config_pdu->dlsch_pdu.
+			dlsch_pdu_rel8.num_bf_prb_per_subband = 1;
+		    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector =
+			1;
+		    //    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.bf_vector                    = ; 
+		    dl_req->number_pdu++;
+
+		    // Program TX Request
+		    TX_req =
+			&eNB->TX_req[CC_id].
+			tx_request_body.tx_pdu_list[eNB->TX_req[CC_id].
+						    tx_request_body.number_of_pdus];
+		    TX_req->pdu_length = bcch_sdu_length;
+		    TX_req->pdu_index = eNB->pdu_index[CC_id]++;
+		    TX_req->num_segments = 1;
+		    TX_req->segments[0].segment_length = bcch_sdu_length;
+		    TX_req->segments[0].segment_data =
+			cc->BCCH_pdu.payload;
+		    eNB->TX_req[CC_id].tx_request_body.number_of_pdus++;
+
+		} else {
+		    LOG_E(MAC,
+			  "[eNB %d] CCid %d Frame %d, subframe %d : Cannot add DCI 1A for SI\n",
+			  module_idP, CC_id, frameP, subframeP);
+		}
+
+		if (opt_enabled == 1) {
+		    trace_pdu(1,
+			      &cc->BCCH_pdu.payload[0],
+			      bcch_sdu_length,
+			      0xffff,
+			      4, 0xffff, eNB->frame, eNB->subframe, 0, 0);
+		    LOG_D(OPT,
+			  "[eNB %d][BCH] Frame %d trace pdu for CC_id %d rnti %x with size %d\n",
+			  module_idP, frameP, CC_id, 0xffff,
+			  bcch_sdu_length);
+		}
+		if (cc->tdd_Config != NULL) {	//TDD
+		    LOG_D(MAC,
+			  "[eNB] Frame %d : Scheduling BCCH->DLSCH (TDD) for CC_id %d SI %d bytes (mcs %d, rb 3)\n",
+			  frameP, CC_id, bcch_sdu_length, mcs);
+		} else {
+		    LOG_D(MAC,
+			  "[eNB] Frame %d : Scheduling BCCH->DLSCH (FDD) for CC_id %d SI %d bytes (mcs %d, rb 3)\n",
+			  frameP, CC_id, bcch_sdu_length, mcs);
+		}
+
+
+		eNB->eNB_stats[CC_id].total_num_bcch_pdu += 1;
+		eNB->eNB_stats[CC_id].bcch_buffer = bcch_sdu_length;
+		eNB->eNB_stats[CC_id].total_bcch_buffer += bcch_sdu_length;
+		eNB->eNB_stats[CC_id].bcch_mcs = mcs;
+	    } else {
 
-	// Rel10 fields
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel10.pdsch_start                           = 3;
-	// Rel13 fields
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.ue_type                               = 0; // regular UE
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.pdsch_payload_type                    = 2; // not BR
-	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel13.initial_transmission_sf_io            = 0xFFFF; // absolute SF	
-
-	if (!CCE_allocation_infeasible(module_idP,CC_id,0,subframeP,
-				       dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,SI_RNTI)) {
-	  LOG_D(MAC,"Frame %d: Subframe %d : Adding common DCI for S_RNTI\n",
-		frameP,subframeP);
-	  dl_req->number_dci++;
-	  dl_req->number_pdu++;
-	  dl_config_pdu                                                                  = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
-	  memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-	  dl_config_pdu->pdu_type                                                        = NFAPI_DL_CONFIG_DLSCH_PDU_TYPE; 
-	  dl_config_pdu->pdu_size                                                        = (uint8_t)(2+sizeof(nfapi_dl_config_dlsch_pdu));
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index                              = eNB->pdu_index[CC_id];
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti                                   = 0xFFFF;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type               = 2;   // format 1A/1B/1D
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = 0;   // localized
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_block_coding                  = getRIV(N_RB_DL,first_rb,4);
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation                             = 2; //QPSK
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.redundancy_version                     = 0;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks                       = 1;// first block
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_block_to_codeword_swap_flag  = 0;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_scheme                    = (cc->p_eNB==1 ) ? 0 : 1;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_layers                       = 1;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_subbands                     = 1;
-	  //	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = ;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ue_category_capacity                   = 1;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa                                     = 4; // 0 dB
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.delta_power_offset_index               = 0;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap                                   = 0;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb                                   = get_subbandsize(cc->mib->message.dl_Bandwidth); // ignored
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_mode                      = (cc->p_eNB==1 ) ? 1 : 2;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_prb_per_subband                 = 1;
-	  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector                          = 1;
-	  //	dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.bf_vector                    = ; 
-	  dl_req->number_pdu++;
-	  
-	  // Program TX Request
-	  TX_req                                                                = &eNB->TX_req[CC_id].tx_request_body.tx_pdu_list[eNB->TX_req[CC_id].tx_request_body.number_of_pdus]; 
-	  TX_req->pdu_length                                                    = bcch_sdu_length;
-	  TX_req->pdu_index                                                     = eNB->pdu_index[CC_id]++;
-	  TX_req->num_segments                                                  = 1;
-	  TX_req->segments[0].segment_length                                    = bcch_sdu_length;
-	  TX_req->segments[0].segment_data                                      = cc->BCCH_pdu.payload;
-	  eNB->TX_req[CC_id].tx_request_body.number_of_pdus++;
-	  
-	}
-	else {
-	  LOG_E(MAC,"[eNB %d] CCid %d Frame %d, subframe %d : Cannot add DCI 1A for SI\n",module_idP, CC_id,frameP,subframeP);
-	}
-	
-	if (opt_enabled == 1) {
-	  trace_pdu(1,
-		    &cc->BCCH_pdu.payload[0],
-		    bcch_sdu_length,
-		    0xffff,
-		    4,
-		    0xffff,
-		    eNB->frame,
-		    eNB->subframe,
-		    0,
-		    0);
-	  LOG_D(OPT,"[eNB %d][BCH] Frame %d trace pdu for CC_id %d rnti %x with size %d\n",
-		module_idP, frameP, CC_id, 0xffff, bcch_sdu_length);
-	}
-	if (cc->tdd_Config!=NULL) { //TDD
-	  LOG_D(MAC,"[eNB] Frame %d : Scheduling BCCH->DLSCH (TDD) for CC_id %d SI %d bytes (mcs %d, rb 3)\n",
-		frameP,
-		CC_id,
-		bcch_sdu_length,
-		mcs);
-	} else {
-	  LOG_D(MAC,"[eNB] Frame %d : Scheduling BCCH->DLSCH (FDD) for CC_id %d SI %d bytes (mcs %d, rb 3)\n",
-		frameP,
-		CC_id,
-		bcch_sdu_length,
-		mcs);
+		//LOG_D(MAC,"[eNB %d] Frame %d : BCCH not active \n",Mod_id,frame);
+	    }
 	}
-	
-	
-	eNB->eNB_stats[CC_id].total_num_bcch_pdu+=1;
-	eNB->eNB_stats[CC_id].bcch_buffer=bcch_sdu_length;
-	eNB->eNB_stats[CC_id].total_bcch_buffer+=bcch_sdu_length;
-	eNB->eNB_stats[CC_id].bcch_mcs=mcs;
-      } else {
-	
-	//LOG_D(MAC,"[eNB %d] Frame %d : BCCH not active \n",Mod_id,frame);
-      }
     }
-  }
-
 #ifdef Rel14
-  schedule_SIB1_BR(module_idP,frameP,subframeP);
-  schedule_SI_BR(module_idP,frameP,subframeP);
+    schedule_SIB1_BR(module_idP, frameP, subframeP);
+    schedule_SI_BR(module_idP, frameP, subframeP);
 #endif
 
-  stop_meas(&eNB->schedule_si);
-  return;
+    stop_meas(&eNB->schedule_si);
+    return;
 }
- 
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_dlsch.c b/openair2/LAYER2/MAC/eNB_scheduler_dlsch.c
index 3e767ec516b076fa8d21d49dfca9f8f32708e1c6..9dbbb39d0346c563e9acbf208ca114ad3bb49570 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler_dlsch.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_dlsch.c
@@ -51,10 +51,10 @@
 //#include "LAYER2/MAC/pre_processor.c"
 #include "pdcp.h"
 
-#include "SIMULATION/TOOLS/defs.h" // for taus
+#include "SIMULATION/TOOLS/defs.h"	// for taus
 
 #if defined(ENABLE_ITTI)
-# include "intertask_interface.h"
+#include "intertask_interface.h"
 #endif
 
 #include "T.h"
@@ -66,347 +66,346 @@ extern RAN_CONTEXT_t RC;
 
 //------------------------------------------------------------------------------
 void
-add_ue_dlsch_info(
-  module_id_t module_idP,
-  int CC_id,
-  int UE_id,
-  sub_frame_t subframeP,
-  UE_DLSCH_STATUS status
-)
+add_ue_dlsch_info(module_id_t module_idP,
+		  int CC_id,
+		  int UE_id, sub_frame_t subframeP, UE_DLSCH_STATUS status)
 //------------------------------------------------------------------------------
 {
 
-  eNB_dlsch_info[module_idP][CC_id][UE_id].rnti             = UE_RNTI(module_idP,UE_id);
-  //  eNB_dlsch_info[module_idP][CC_id][ue_mod_idP].weight           = weight;
-  eNB_dlsch_info[module_idP][CC_id][UE_id].subframe         = subframeP;
-  eNB_dlsch_info[module_idP][CC_id][UE_id].status           = status;
+    eNB_dlsch_info[module_idP][CC_id][UE_id].rnti =
+	UE_RNTI(module_idP, UE_id);
+    //  eNB_dlsch_info[module_idP][CC_id][ue_mod_idP].weight           = weight;
+    eNB_dlsch_info[module_idP][CC_id][UE_id].subframe = subframeP;
+    eNB_dlsch_info[module_idP][CC_id][UE_id].status = status;
 
-  eNB_dlsch_info[module_idP][CC_id][UE_id].serving_num++;
+    eNB_dlsch_info[module_idP][CC_id][UE_id].serving_num++;
 
 }
 
 //------------------------------------------------------------------------------
 int
-schedule_next_dlue(
-  module_id_t module_idP,
-  int CC_id,
-  sub_frame_t subframeP
-)
+schedule_next_dlue(module_id_t module_idP, int CC_id,
+		   sub_frame_t subframeP)
 //------------------------------------------------------------------------------
 {
 
-  int next_ue;
-  UE_list_t *UE_list=&RC.mac[module_idP]->UE_list;
+    int next_ue;
+    UE_list_t *UE_list = &RC.mac[module_idP]->UE_list;
 
-  for (next_ue=UE_list->head; next_ue>=0; next_ue=UE_list->next[next_ue] ) {
-    if  (eNB_dlsch_info[module_idP][CC_id][next_ue].status == S_DL_WAITING) {
-      return next_ue;
+    for (next_ue = UE_list->head; next_ue >= 0;
+	 next_ue = UE_list->next[next_ue]) {
+	if (eNB_dlsch_info[module_idP][CC_id][next_ue].status ==
+	    S_DL_WAITING) {
+	    return next_ue;
+	}
     }
-  }
 
-  for (next_ue=UE_list->head; next_ue>=0; next_ue=UE_list->next[next_ue] ) {
-    if  (eNB_dlsch_info[module_idP][CC_id][next_ue].status == S_DL_BUFFERED) {
-      eNB_dlsch_info[module_idP][CC_id][next_ue].status = S_DL_WAITING;
+    for (next_ue = UE_list->head; next_ue >= 0;
+	 next_ue = UE_list->next[next_ue]) {
+	if (eNB_dlsch_info[module_idP][CC_id][next_ue].status ==
+	    S_DL_BUFFERED) {
+	    eNB_dlsch_info[module_idP][CC_id][next_ue].status =
+		S_DL_WAITING;
+	}
     }
-  }
 
-  return(-1);//next_ue;
+    return (-1);		//next_ue;
 
 }
 
 //------------------------------------------------------------------------------
 unsigned char
-generate_dlsch_header(
-  unsigned char* mac_header,
-  unsigned char num_sdus,
-  unsigned short *sdu_lengths,
-  unsigned char *sdu_lcids,
-  unsigned char drx_cmd,
-  unsigned short timing_advance_cmd,
-  unsigned char *ue_cont_res_id,
-  unsigned char short_padding,
-  unsigned short post_padding
-)
+generate_dlsch_header(unsigned char *mac_header,
+		      unsigned char num_sdus,
+		      unsigned short *sdu_lengths,
+		      unsigned char *sdu_lcids,
+		      unsigned char drx_cmd,
+		      unsigned short timing_advance_cmd,
+		      unsigned char *ue_cont_res_id,
+		      unsigned char short_padding,
+		      unsigned short post_padding)
 //------------------------------------------------------------------------------
 {
 
-  SCH_SUBHEADER_FIXED *mac_header_ptr = (SCH_SUBHEADER_FIXED *)mac_header;
-  uint8_t first_element=0,last_size=0,i;
-  uint8_t mac_header_control_elements[16],*ce_ptr;
-
-  ce_ptr = &mac_header_control_elements[0];
-
-  // compute header components
-
-  if ((short_padding == 1) || (short_padding == 2)) {
-    mac_header_ptr->R    = 0;
-    mac_header_ptr->E    = 0;
-    mac_header_ptr->LCID = SHORT_PADDING;
-    first_element=1;
-    last_size=1;
-  }
-
-  if (short_padding == 2) {
-    mac_header_ptr->E = 1;
-    mac_header_ptr++;
-    mac_header_ptr->R = 0;
-    mac_header_ptr->E    = 0;
-    mac_header_ptr->LCID = SHORT_PADDING;
-    last_size=1;
-  }
-
-  if (drx_cmd != 255) {
-    if (first_element>0) {
-      mac_header_ptr->E = 1;
-      mac_header_ptr++;
-    } else {
-      first_element=1;
+    SCH_SUBHEADER_FIXED *mac_header_ptr =
+	(SCH_SUBHEADER_FIXED *) mac_header;
+    uint8_t first_element = 0, last_size = 0, i;
+    uint8_t mac_header_control_elements[16], *ce_ptr;
+
+    ce_ptr = &mac_header_control_elements[0];
+
+    // compute header components
+
+    if ((short_padding == 1) || (short_padding == 2)) {
+	mac_header_ptr->R = 0;
+	mac_header_ptr->E = 0;
+	mac_header_ptr->LCID = SHORT_PADDING;
+	first_element = 1;
+	last_size = 1;
     }
 
-    mac_header_ptr->R = 0;
-    mac_header_ptr->E    = 0;
-    mac_header_ptr->LCID = DRX_CMD;
-    last_size=1;
-  }
+    if (short_padding == 2) {
+	mac_header_ptr->E = 1;
+	mac_header_ptr++;
+	mac_header_ptr->R = 0;
+	mac_header_ptr->E = 0;
+	mac_header_ptr->LCID = SHORT_PADDING;
+	last_size = 1;
+    }
 
-  if (timing_advance_cmd != 31) {
-    if (first_element>0) {
-      mac_header_ptr->E = 1;
-      mac_header_ptr++;
-    } else {
-      first_element=1;
+    if (drx_cmd != 255) {
+	if (first_element > 0) {
+	    mac_header_ptr->E = 1;
+	    mac_header_ptr++;
+	} else {
+	    first_element = 1;
+	}
+
+	mac_header_ptr->R = 0;
+	mac_header_ptr->E = 0;
+	mac_header_ptr->LCID = DRX_CMD;
+	last_size = 1;
     }
 
-    mac_header_ptr->R = 0;
-    mac_header_ptr->E    = 0;
-    mac_header_ptr->LCID = TIMING_ADV_CMD;
-    last_size=1;
-    //    msg("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
-    ((TIMING_ADVANCE_CMD *)ce_ptr)->R=0;
-    AssertFatal(timing_advance_cmd < 64,"timing_advance_cmd %d > 63\n",timing_advance_cmd);
-    ((TIMING_ADVANCE_CMD *)ce_ptr)->TA=timing_advance_cmd;//(timing_advance_cmd+31)&0x3f;
-    LOG_D(MAC,"timing advance =%d (%d)\n",timing_advance_cmd,((TIMING_ADVANCE_CMD *)ce_ptr)->TA);
-    ce_ptr+=sizeof(TIMING_ADVANCE_CMD);
-    //msg("offset %d\n",ce_ptr-mac_header_control_elements);
-  }
-
-  if (ue_cont_res_id) {
-    if (first_element>0) {
-      mac_header_ptr->E = 1;
-      /*
-      printf("[eNB][MAC] last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
-      */
-      mac_header_ptr++;
-    } else {
-      first_element=1;
+    if (timing_advance_cmd != 31) {
+	if (first_element > 0) {
+	    mac_header_ptr->E = 1;
+	    mac_header_ptr++;
+	} else {
+	    first_element = 1;
+	}
+
+	mac_header_ptr->R = 0;
+	mac_header_ptr->E = 0;
+	mac_header_ptr->LCID = TIMING_ADV_CMD;
+	last_size = 1;
+	//    msg("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
+	((TIMING_ADVANCE_CMD *) ce_ptr)->R = 0;
+	AssertFatal(timing_advance_cmd < 64,
+		    "timing_advance_cmd %d > 63\n", timing_advance_cmd);
+	((TIMING_ADVANCE_CMD *) ce_ptr)->TA = timing_advance_cmd;	//(timing_advance_cmd+31)&0x3f;
+	LOG_D(MAC, "timing advance =%d (%d)\n", timing_advance_cmd,
+	      ((TIMING_ADVANCE_CMD *) ce_ptr)->TA);
+	ce_ptr += sizeof(TIMING_ADVANCE_CMD);
+	//msg("offset %d\n",ce_ptr-mac_header_control_elements);
     }
 
-    mac_header_ptr->R = 0;
-    mac_header_ptr->E    = 0;
-    mac_header_ptr->LCID = UE_CONT_RES;
-    last_size=1;
-
-    LOG_T(MAC,"[eNB ][RAPROC] Generate contention resolution msg: %x.%x.%x.%x.%x.%x\n",
-          ue_cont_res_id[0],
-          ue_cont_res_id[1],
-          ue_cont_res_id[2],
-          ue_cont_res_id[3],
-          ue_cont_res_id[4],
-          ue_cont_res_id[5]);
-
-    memcpy(ce_ptr,ue_cont_res_id,6);
-    ce_ptr+=6;
-    // msg("(cont_res) : offset %d\n",ce_ptr-mac_header_control_elements);
-  }
-
-  //msg("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
-
-  for (i=0; i<num_sdus; i++) {
-    LOG_T(MAC,"[eNB] Generate DLSCH header num sdu %d len sdu %d\n",num_sdus, sdu_lengths[i]);
-
-    if (first_element>0) {
-      mac_header_ptr->E = 1;
-      /*msg("last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
-      */
-      mac_header_ptr+=last_size;
-      //msg("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
-    } else {
-      first_element=1;
+    if (ue_cont_res_id) {
+	if (first_element > 0) {
+	    mac_header_ptr->E = 1;
+	    /*
+	       printf("[eNB][MAC] last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
+	     */
+	    mac_header_ptr++;
+	} else {
+	    first_element = 1;
+	}
+
+	mac_header_ptr->R = 0;
+	mac_header_ptr->E = 0;
+	mac_header_ptr->LCID = UE_CONT_RES;
+	last_size = 1;
+
+	LOG_T(MAC,
+	      "[eNB ][RAPROC] Generate contention resolution msg: %x.%x.%x.%x.%x.%x\n",
+	      ue_cont_res_id[0], ue_cont_res_id[1], ue_cont_res_id[2],
+	      ue_cont_res_id[3], ue_cont_res_id[4], ue_cont_res_id[5]);
+
+	memcpy(ce_ptr, ue_cont_res_id, 6);
+	ce_ptr += 6;
+	// msg("(cont_res) : offset %d\n",ce_ptr-mac_header_control_elements);
     }
+    //msg("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
+
+    for (i = 0; i < num_sdus; i++) {
+	LOG_T(MAC, "[eNB] Generate DLSCH header num sdu %d len sdu %d\n",
+	      num_sdus, sdu_lengths[i]);
+
+	if (first_element > 0) {
+	    mac_header_ptr->E = 1;
+	    /*msg("last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
+	     */
+	    mac_header_ptr += last_size;
+	    //msg("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
+	} else {
+	    first_element = 1;
+	}
 
-    if (sdu_lengths[i] < 128) {
-      ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->R    = 0;
-      ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->E    = 0;
-      ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->F    = 0;
-      ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->LCID = sdu_lcids[i];
-      ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->L    = (unsigned char)sdu_lengths[i];
-      last_size=2;
-    } else {
-      ((SCH_SUBHEADER_LONG *)mac_header_ptr)->R    = 0;
-      ((SCH_SUBHEADER_LONG *)mac_header_ptr)->E    = 0;
-      ((SCH_SUBHEADER_LONG *)mac_header_ptr)->F    = 1;
-      ((SCH_SUBHEADER_LONG *)mac_header_ptr)->LCID = sdu_lcids[i];
-      ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_MSB    = ((unsigned short) sdu_lengths[i]>>8)&0x7f;
-      ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_LSB    = (unsigned short) sdu_lengths[i]&0xff;
-      ((SCH_SUBHEADER_LONG *)mac_header_ptr)->padding   = 0x00;
-      last_size=3;
+	if (sdu_lengths[i] < 128) {
+	    ((SCH_SUBHEADER_SHORT *) mac_header_ptr)->R = 0;
+	    ((SCH_SUBHEADER_SHORT *) mac_header_ptr)->E = 0;
+	    ((SCH_SUBHEADER_SHORT *) mac_header_ptr)->F = 0;
+	    ((SCH_SUBHEADER_SHORT *) mac_header_ptr)->LCID = sdu_lcids[i];
+	    ((SCH_SUBHEADER_SHORT *) mac_header_ptr)->L =
+		(unsigned char) sdu_lengths[i];
+	    last_size = 2;
+	} else {
+	    ((SCH_SUBHEADER_LONG *) mac_header_ptr)->R = 0;
+	    ((SCH_SUBHEADER_LONG *) mac_header_ptr)->E = 0;
+	    ((SCH_SUBHEADER_LONG *) mac_header_ptr)->F = 1;
+	    ((SCH_SUBHEADER_LONG *) mac_header_ptr)->LCID = sdu_lcids[i];
+	    ((SCH_SUBHEADER_LONG *) mac_header_ptr)->L_MSB =
+		((unsigned short) sdu_lengths[i] >> 8) & 0x7f;
+	    ((SCH_SUBHEADER_LONG *) mac_header_ptr)->L_LSB =
+		(unsigned short) sdu_lengths[i] & 0xff;
+	    ((SCH_SUBHEADER_LONG *) mac_header_ptr)->padding = 0x00;
+	    last_size = 3;
 #ifdef DEBUG_HEADER_PARSING
-      LOG_D(MAC,"[eNB] generate long sdu, size %x (MSB %x, LSB %x)\n",
-            sdu_lengths[i],
-            ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_MSB,
-            ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_LSB);
+	    LOG_D(MAC,
+		  "[eNB] generate long sdu, size %x (MSB %x, LSB %x)\n",
+		  sdu_lengths[i],
+		  ((SCH_SUBHEADER_LONG *) mac_header_ptr)->L_MSB,
+		  ((SCH_SUBHEADER_LONG *) mac_header_ptr)->L_LSB);
 #endif
+	}
     }
-  }
-
-  /*
 
-    printf("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
-
-    printf("last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
-    ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
-    ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
-    ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
+    /*
+
+       printf("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
+
+       printf("last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
+       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
+       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
+       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
+
+
+       if (((SCH_SUBHEADER_FIXED*)mac_header_ptr)->LCID < UE_CONT_RES) {
+       if (((SCH_SUBHEADER_SHORT*)mac_header_ptr)->F == 0)
+       printf("F = 0, sdu len (L field) %d\n",(((SCH_SUBHEADER_SHORT*)mac_header_ptr)->L));
+       else
+       printf("F = 1, sdu len (L field) %d\n",(((SCH_SUBHEADER_LONG*)mac_header_ptr)->L));
+       }
+     */
+    if (post_padding > 0) {	// we have lots of padding at the end of the packet
+	mac_header_ptr->E = 1;
+	mac_header_ptr += last_size;
+	// add a padding element
+	mac_header_ptr->R = 0;
+	mac_header_ptr->E = 0;
+	mac_header_ptr->LCID = SHORT_PADDING;
+	mac_header_ptr++;
+    } else {			// no end of packet padding
+	// last SDU subhead is of fixed type (sdu length implicitly to be computed at UE)
+	mac_header_ptr++;
+    }
 
+    //msg("After subheaders %d\n",(uint8_t*)mac_header_ptr - mac_header);
 
-    if (((SCH_SUBHEADER_FIXED*)mac_header_ptr)->LCID < UE_CONT_RES) {
-    if (((SCH_SUBHEADER_SHORT*)mac_header_ptr)->F == 0)
-    printf("F = 0, sdu len (L field) %d\n",(((SCH_SUBHEADER_SHORT*)mac_header_ptr)->L));
-    else
-    printf("F = 1, sdu len (L field) %d\n",(((SCH_SUBHEADER_LONG*)mac_header_ptr)->L));
+    if ((ce_ptr - mac_header_control_elements) > 0) {
+	// printf("Copying %d bytes for control elements\n",ce_ptr-mac_header_control_elements);
+	memcpy((void *) mac_header_ptr, mac_header_control_elements,
+	       ce_ptr - mac_header_control_elements);
+	mac_header_ptr +=
+	    (unsigned char) (ce_ptr - mac_header_control_elements);
     }
-  */
-  if (post_padding>0) {// we have lots of padding at the end of the packet
-    mac_header_ptr->E = 1;
-    mac_header_ptr+=last_size;
-    // add a padding element
-    mac_header_ptr->R    = 0;
-    mac_header_ptr->E    = 0;
-    mac_header_ptr->LCID = SHORT_PADDING;
-    mac_header_ptr++;
-  } else { // no end of packet padding
-    // last SDU subhead is of fixed type (sdu length implicitly to be computed at UE)
-    mac_header_ptr++;
-  }
-
-  //msg("After subheaders %d\n",(uint8_t*)mac_header_ptr - mac_header);
-
-  if ((ce_ptr-mac_header_control_elements) > 0) {
-    // printf("Copying %d bytes for control elements\n",ce_ptr-mac_header_control_elements);
-    memcpy((void*)mac_header_ptr,mac_header_control_elements,ce_ptr-mac_header_control_elements);
-    mac_header_ptr+=(unsigned char)(ce_ptr-mac_header_control_elements);
-  }
-
-  //msg("After CEs %d\n",(uint8_t*)mac_header_ptr - mac_header);
-
-  return((unsigned char*)mac_header_ptr - mac_header);
+    //msg("After CEs %d\n",(uint8_t*)mac_header_ptr - mac_header);
+
+    return ((unsigned char *) mac_header_ptr - mac_header);
 
 }
 
 //------------------------------------------------------------------------------
 void
-set_ul_DAI(
-  int module_idP,
-  int UE_idP,
-  int CC_idP,
-  int frameP,
-  int subframeP
-) 
+set_ul_DAI(int module_idP, int UE_idP, int CC_idP, int frameP,
+	   int subframeP)
 //------------------------------------------------------------------------------
 {
 
-  eNB_MAC_INST         *eNB      = RC.mac[module_idP];
-  UE_list_t            *UE_list  = &eNB->UE_list;
-  unsigned char         DAI;
-  COMMON_channels_t    *cc       = &eNB->common_channels[CC_idP];
-  if (cc->tdd_Config != NULL) {  //TDD
-    DAI = (UE_list->UE_template[CC_idP][UE_idP].DAI-1)&3;
-    LOG_D(MAC,"[eNB %d] CC_id %d Frame %d, subframe %d: DAI %d for UE %d\n",module_idP,CC_idP,frameP,subframeP,DAI,UE_idP);
-    // Save DAI for Format 0 DCI
-
-    switch (cc->tdd_Config->subframeAssignment) {
-    case 0:
-      //      if ((subframeP==0)||(subframeP==1)||(subframeP==5)||(subframeP==6))
-      break;
-
-    case 1:
-      switch (subframeP) {
-      case 1:
-        UE_list->UE_template[CC_idP][UE_idP].DAI_ul[7] = DAI;
-        break;
-
-      case 4:
-        UE_list->UE_template[CC_idP][UE_idP].DAI_ul[8] = DAI;
-        break;
-
-      case 6:
-        UE_list->UE_template[CC_idP][UE_idP].DAI_ul[2] = DAI;
-        break;
-
-      case 9:
-        UE_list->UE_template[CC_idP][UE_idP].DAI_ul[3] = DAI;
-        break;
-      }
-
-    case 2:
-      //      if ((subframeP==3)||(subframeP==8))
-      //  UE_list->UE_template[CC_idP][UE_idP].DAI_ul = DAI;
-      break;
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    UE_list_t *UE_list = &eNB->UE_list;
+    unsigned char DAI;
+    COMMON_channels_t *cc = &eNB->common_channels[CC_idP];
+    if (cc->tdd_Config != NULL) {	//TDD
+	DAI = (UE_list->UE_template[CC_idP][UE_idP].DAI - 1) & 3;
+	LOG_D(MAC,
+	      "[eNB %d] CC_id %d Frame %d, subframe %d: DAI %d for UE %d\n",
+	      module_idP, CC_idP, frameP, subframeP, DAI, UE_idP);
+	// Save DAI for Format 0 DCI
+
+	switch (cc->tdd_Config->subframeAssignment) {
+	case 0:
+	    //      if ((subframeP==0)||(subframeP==1)||(subframeP==5)||(subframeP==6))
+	    break;
+
+	case 1:
+	    switch (subframeP) {
+	    case 1:
+		UE_list->UE_template[CC_idP][UE_idP].DAI_ul[7] = DAI;
+		break;
+
+	    case 4:
+		UE_list->UE_template[CC_idP][UE_idP].DAI_ul[8] = DAI;
+		break;
+
+	    case 6:
+		UE_list->UE_template[CC_idP][UE_idP].DAI_ul[2] = DAI;
+		break;
+
+	    case 9:
+		UE_list->UE_template[CC_idP][UE_idP].DAI_ul[3] = DAI;
+		break;
+	    }
 
-    case 3:
+	case 2:
+	    //      if ((subframeP==3)||(subframeP==8))
+	    //  UE_list->UE_template[CC_idP][UE_idP].DAI_ul = DAI;
+	    break;
+
+	case 3:
+
+	    //if ((subframeP==6)||(subframeP==8)||(subframeP==0)) {
+	    //  LOG_D(MAC,"schedule_ue_spec: setting UL DAI to %d for subframeP %d => %d\n",DAI,subframeP, ((subframeP+8)%10)>>1);
+	    //  UE_list->UE_template[CC_idP][UE_idP].DAI_ul[((subframeP+8)%10)>>1] = DAI;
+	    //}
+	    switch (subframeP) {
+	    case 5:
+	    case 6:
+	    case 1:
+		UE_list->UE_template[CC_idP][UE_idP].DAI_ul[2] = DAI;
+		break;
+
+	    case 7:
+	    case 8:
+		UE_list->UE_template[CC_idP][UE_idP].DAI_ul[3] = DAI;
+		break;
+
+	    case 9:
+	    case 0:
+		UE_list->UE_template[CC_idP][UE_idP].DAI_ul[4] = DAI;
+		break;
+
+	    default:
+		break;
+	    }
 
-      //if ((subframeP==6)||(subframeP==8)||(subframeP==0)) {
-      //  LOG_D(MAC,"schedule_ue_spec: setting UL DAI to %d for subframeP %d => %d\n",DAI,subframeP, ((subframeP+8)%10)>>1);
-      //  UE_list->UE_template[CC_idP][UE_idP].DAI_ul[((subframeP+8)%10)>>1] = DAI;
-      //}
-      switch (subframeP) {
-      case 5:
-      case 6:
-      case 1:
-        UE_list->UE_template[CC_idP][UE_idP].DAI_ul[2] = DAI;
-        break;
-
-      case 7:
-      case 8:
-        UE_list->UE_template[CC_idP][UE_idP].DAI_ul[3] = DAI;
-        break;
-
-      case 9:
-      case 0:
-        UE_list->UE_template[CC_idP][UE_idP].DAI_ul[4] = DAI;
-        break;
-
-      default:
-        break;
-      }
-
-      break;
+	    break;
 
-    case 4:
-      //      if ((subframeP==8)||(subframeP==9))
-      //  UE_list->UE_template[CC_idP][UE_idP].DAI_ul = DAI;
-      break;
+	case 4:
+	    //      if ((subframeP==8)||(subframeP==9))
+	    //  UE_list->UE_template[CC_idP][UE_idP].DAI_ul = DAI;
+	    break;
 
-    case 5:
-      //      if (subframeP==8)
-      //  UE_list->UE_template[CC_idP][UE_idP].DAI_ul = DAI;
-      break;
+	case 5:
+	    //      if (subframeP==8)
+	    //  UE_list->UE_template[CC_idP][UE_idP].DAI_ul = DAI;
+	    break;
 
-    case 6:
-      //      if ((subframeP==1)||(subframeP==4)||(subframeP==6)||(subframeP==9))
-      //  UE_list->UE_template[CC_idP][UE_idP].DAI_ul = DAI;
-      break;
+	case 6:
+	    //      if ((subframeP==1)||(subframeP==4)||(subframeP==6)||(subframeP==9))
+	    //  UE_list->UE_template[CC_idP][UE_idP].DAI_ul = DAI;
+	    break;
 
-    default:
-      break;
+	default:
+	    break;
+	}
     }
-  }
 }
 
 
@@ -414,1130 +413,1342 @@ set_ul_DAI(
 
 //------------------------------------------------------------------------------
 void
-schedule_ue_spec(
-  module_id_t   module_idP,
-  frame_t       frameP,
-  sub_frame_t   subframeP,
-  int*          mbsfn_flag
-)
+schedule_ue_spec(module_id_t module_idP,
+		 frame_t frameP, sub_frame_t subframeP, int *mbsfn_flag)
 //------------------------------------------------------------------------------
 {
 
 
-  uint8_t                        CC_id;
-  int                            UE_id;
-  unsigned char                  aggregation;
-  mac_rlc_status_resp_t          rlc_status;
-  unsigned char                  header_len_dcch=0, header_len_dcch_tmp=0; 
-  unsigned char                  header_len_dtch=0, header_len_dtch_tmp=0, header_len_dtch_last=0; 
-  unsigned char                  ta_len=0;
-  unsigned char                  sdu_lcids[NB_RB_MAX],lcid,offset,num_sdus=0;
-  uint16_t                       nb_rb,nb_rb_temp,nb_available_rb;
-  uint16_t                       TBS,j,sdu_lengths[NB_RB_MAX],rnti,padding=0,post_padding=0;
-  unsigned char                  dlsch_buffer[MAX_DLSCH_PAYLOAD_BYTES];
-  unsigned char                  round             = 0;
-  unsigned char                  harq_pid          = 0;
-  eNB_UE_STATS                   *eNB_UE_stats     = NULL;
-  uint16_t                       sdu_length_total = 0;
-
-  eNB_MAC_INST                   *eNB      = RC.mac[module_idP];
-  COMMON_channels_t              *cc       = eNB->common_channels;
-  UE_list_t                      *UE_list  = &eNB->UE_list;
-  int                            continue_flag=0;
-  int32_t                        normalized_rx_power, target_rx_power;
-  int32_t                        tpc=1;
-  static int32_t                 tpc_accumulated=0;
-  UE_sched_ctrl                  *ue_sched_ctl;
-  int                            mcs;
-  int                            i;
-  int                            min_rb_unit[MAX_NUM_CCs];
-  int                            N_RB_DL[MAX_NUM_CCs];
-  int                            total_nb_available_rb[MAX_NUM_CCs];
-  int                            N_RBG[MAX_NUM_CCs];
-  nfapi_dl_config_request_body_t *dl_req;
-  nfapi_dl_config_request_pdu_t  *dl_config_pdu;
-  int                            tdd_sfa;
-  int                            ta_update;
+    uint8_t CC_id;
+    int UE_id;
+    unsigned char aggregation;
+    mac_rlc_status_resp_t rlc_status;
+    unsigned char header_len_dcch = 0, header_len_dcch_tmp = 0;
+    unsigned char header_len_dtch = 0, header_len_dtch_tmp =
+	0, header_len_dtch_last = 0;
+    unsigned char ta_len = 0;
+    unsigned char sdu_lcids[NB_RB_MAX], lcid, offset, num_sdus = 0;
+    uint16_t nb_rb, nb_rb_temp, nb_available_rb;
+    uint16_t TBS, j, sdu_lengths[NB_RB_MAX], rnti, padding =
+	0, post_padding = 0;
+    unsigned char dlsch_buffer[MAX_DLSCH_PAYLOAD_BYTES];
+    unsigned char round = 0;
+    unsigned char harq_pid = 0;
+    eNB_UE_STATS *eNB_UE_stats = NULL;
+    uint16_t sdu_length_total = 0;
+
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    COMMON_channels_t *cc = eNB->common_channels;
+    UE_list_t *UE_list = &eNB->UE_list;
+    int continue_flag = 0;
+    int32_t normalized_rx_power, target_rx_power;
+    int32_t tpc = 1;
+    static int32_t tpc_accumulated = 0;
+    UE_sched_ctrl *ue_sched_ctl;
+    int mcs;
+    int i;
+    int min_rb_unit[MAX_NUM_CCs];
+    int N_RB_DL[MAX_NUM_CCs];
+    int total_nb_available_rb[MAX_NUM_CCs];
+    int N_RBG[MAX_NUM_CCs];
+    nfapi_dl_config_request_body_t *dl_req;
+    nfapi_dl_config_request_pdu_t *dl_config_pdu;
+    int tdd_sfa;
+    int ta_update;
 
 #if 0
-  if (UE_list->head==-1) {
-    return;
-  }
+    if (UE_list->head == -1) {
+	return;
+    }
 #endif
 
-  start_meas(&eNB->schedule_dlsch);
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_SCHEDULE_DLSCH,VCD_FUNCTION_IN);
+    start_meas(&eNB->schedule_dlsch);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_SCHEDULE_DLSCH, VCD_FUNCTION_IN);
+
+
+    // for TDD: check that we have to act here, otherwise return
+    if (cc[0].tdd_Config) {
+	tdd_sfa = cc[0].tdd_Config->subframeAssignment;
+	switch (subframeP) {
+	case 0:
+	    // always continue
+	    break;
+	case 1:
+	    return;
+	    break;
+	case 2:
+	    return;
+	    break;
+	case 3:
+	    if ((tdd_sfa != 2) && (tdd_sfa != 5))
+		return;
+	    break;
+	case 4:
+	    if ((tdd_sfa != 1) && (tdd_sfa != 2) && (tdd_sfa != 4)
+		&& (tdd_sfa != 5))
+		return;
+	    break;
+	case 5:
+	    break;
+	case 6:
+	case 7:
+	    if ((tdd_sfa != 1) && (tdd_sfa != 2) && (tdd_sfa != 4)
+		&& (tdd_sfa != 5))
+		return;
+	    break;
+	case 8:
+	    if ((tdd_sfa != 2) && (tdd_sfa != 3) && (tdd_sfa != 4)
+		&& (tdd_sfa != 5))
+		return;
+	    break;
+	case 9:
+	    if ((tdd_sfa != 1) && (tdd_sfa != 3) && (tdd_sfa != 4)
+		&& (tdd_sfa != 6))
+		return;
+	    break;
 
-
-  // for TDD: check that we have to act here, otherwise return
-  if (cc[0].tdd_Config) {
-    tdd_sfa = cc[0].tdd_Config->subframeAssignment;
-    switch (subframeP) {
-    case 0:
-      // always continue
-       break;
-    case 1:
-      return;
-	break;
-    case 2:
-      return;
-      break;
-    case 3:
-      if ((tdd_sfa!=2) && (tdd_sfa!=5)) return;
-      break;
-    case 4:
-      if ((tdd_sfa!=1)&&(tdd_sfa!=2)&&(tdd_sfa!=4)&&(tdd_sfa!=5)) return;
-      break;
-    case 5:
-      break;
-    case 6:
-    case 7:
-      if ((tdd_sfa!=1)&&(tdd_sfa!=2)&&(tdd_sfa!=4)&&(tdd_sfa!=5)) return;
-      break;
-    case 8:
-      if ((tdd_sfa!=2)&&(tdd_sfa!=3)&&(tdd_sfa!=4)&&(tdd_sfa!=5)) return;
-      break;
-    case 9:
-      if ((tdd_sfa!=1)&&(tdd_sfa!=3)&&(tdd_sfa!=4)&&(tdd_sfa!=6)) return;
-      break;
-   
+	}
+    }
+    //weight = get_ue_weight(module_idP,UE_id);
+    aggregation = 2;
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+	N_RB_DL[CC_id] = to_prb(cc[CC_id].mib->message.dl_Bandwidth);
+	min_rb_unit[CC_id] = get_min_rb_unit(module_idP, CC_id);
+	// get number of PRBs less those used by common channels
+	total_nb_available_rb[CC_id] = N_RB_DL[CC_id];
+	for (i = 0; i < N_RB_DL[CC_id]; i++)
+	    if (cc[CC_id].vrb_map[i] != 0)
+		total_nb_available_rb[CC_id]--;
+
+	N_RBG[CC_id] = to_rbg(cc[CC_id].mib->message.dl_Bandwidth);
+
+	// store the global enb stats:
+	eNB->eNB_stats[CC_id].num_dlactive_UEs = UE_list->num_UEs;
+	eNB->eNB_stats[CC_id].available_prbs =
+	    total_nb_available_rb[CC_id];
+	eNB->eNB_stats[CC_id].total_available_prbs +=
+	    total_nb_available_rb[CC_id];
+	eNB->eNB_stats[CC_id].dlsch_bytes_tx = 0;
+	eNB->eNB_stats[CC_id].dlsch_pdus_tx = 0;
     }
-  }
-
-  //weight = get_ue_weight(module_idP,UE_id);
-  aggregation = 2; 
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-    N_RB_DL[CC_id] = to_prb(cc[CC_id].mib->message.dl_Bandwidth);
-    min_rb_unit[CC_id]=get_min_rb_unit(module_idP,CC_id);
-    // get number of PRBs less those used by common channels
-    total_nb_available_rb[CC_id] = N_RB_DL[CC_id];
-    for (i=0;i<N_RB_DL[CC_id];i++)
-      if (cc[CC_id].vrb_map[i]!=0)
-	total_nb_available_rb[CC_id]--;
-
-    N_RBG[CC_id] = to_rbg(cc[CC_id].mib->message.dl_Bandwidth);
-
-    // store the global enb stats:
-    eNB->eNB_stats[CC_id].num_dlactive_UEs      =  UE_list->num_UEs;
-    eNB->eNB_stats[CC_id].available_prbs        =  total_nb_available_rb[CC_id];
-    eNB->eNB_stats[CC_id].total_available_prbs +=  total_nb_available_rb[CC_id];
-    eNB->eNB_stats[CC_id].dlsch_bytes_tx        = 0;
-    eNB->eNB_stats[CC_id].dlsch_pdus_tx         = 0;
-  }
-
-  /// CALLING Pre_Processor for downlink scheduling (Returns estimation of RBs required by each UE and the allocation on sub-band)
-
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_DLSCH_PREPROCESSOR,VCD_FUNCTION_IN);
-  start_meas(&eNB->schedule_dlsch_preprocessor);
-  dlsch_scheduler_pre_processor(module_idP,
-                                frameP,
-                                subframeP,
-                                N_RBG,
-                                mbsfn_flag);
-  stop_meas(&eNB->schedule_dlsch_preprocessor);
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_DLSCH_PREPROCESSOR,VCD_FUNCTION_OUT);
-
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-    LOG_D(MAC, "doing schedule_ue_spec for CC_id %d\n",CC_id);
-
-    dl_req        = &eNB->DL_req[CC_id].dl_config_request_body;
-
-    if (mbsfn_flag[CC_id]>0)
-      continue;
-
-    for (UE_id=UE_list->head; UE_id>=0; UE_id=UE_list->next[UE_id]) {
-      continue_flag=0; // reset the flag to allow allocation for the remaining UEs
-      rnti = UE_RNTI(module_idP,UE_id);
-      eNB_UE_stats = &UE_list->eNB_UE_stats[CC_id][UE_id];
-      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-
-
-      if (rnti==NOT_A_RNTI) {
-        LOG_D(MAC,"Cannot find rnti for UE_id %d (num_UEs %d)\n",UE_id,UE_list->num_UEs);
-        continue_flag=1;
-      }
-
-      if (eNB_UE_stats==NULL) {
-        LOG_D(MAC,"[eNB] Cannot find eNB_UE_stats\n");
-        continue_flag=1;
-      }
-
-      if (continue_flag != 1){
-        switch(get_tmode(module_idP,CC_id,UE_id)){
-        case 1:
-        case 2:
-        case 7:
-	  aggregation = get_aggregation(get_bw_index(module_idP,CC_id), 
+
+    /// CALLING Pre_Processor for downlink scheduling (Returns estimation of RBs required by each UE and the allocation on sub-band)
+
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_DLSCH_PREPROCESSOR, VCD_FUNCTION_IN);
+    start_meas(&eNB->schedule_dlsch_preprocessor);
+    dlsch_scheduler_pre_processor(module_idP,
+				  frameP, subframeP, N_RBG, mbsfn_flag);
+    stop_meas(&eNB->schedule_dlsch_preprocessor);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_DLSCH_PREPROCESSOR, VCD_FUNCTION_OUT);
+
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+	LOG_D(MAC, "doing schedule_ue_spec for CC_id %d\n", CC_id);
+
+	dl_req = &eNB->DL_req[CC_id].dl_config_request_body;
+
+	if (mbsfn_flag[CC_id] > 0)
+	    continue;
+
+	for (UE_id = UE_list->head; UE_id >= 0;
+	     UE_id = UE_list->next[UE_id]) {
+	    continue_flag = 0;	// reset the flag to allow allocation for the remaining UEs
+	    rnti = UE_RNTI(module_idP, UE_id);
+	    eNB_UE_stats = &UE_list->eNB_UE_stats[CC_id][UE_id];
+	    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+
+
+	    if (rnti == NOT_A_RNTI) {
+		LOG_D(MAC, "Cannot find rnti for UE_id %d (num_UEs %d)\n",
+		      UE_id, UE_list->num_UEs);
+		continue_flag = 1;
+	    }
+
+	    if (eNB_UE_stats == NULL) {
+		LOG_D(MAC, "[eNB] Cannot find eNB_UE_stats\n");
+		continue_flag = 1;
+	    }
+
+	    if (continue_flag != 1) {
+		switch (get_tmode(module_idP, CC_id, UE_id)) {
+		case 1:
+		case 2:
+		case 7:
+		    aggregation =
+			get_aggregation(get_bw_index(module_idP, CC_id),
 					ue_sched_ctl->dl_cqi[CC_id],
 					format1);
-	  break;
-        case 3:
-	  aggregation = get_aggregation(get_bw_index(module_idP,CC_id), 
+		    break;
+		case 3:
+		    aggregation =
+			get_aggregation(get_bw_index(module_idP, CC_id),
 					ue_sched_ctl->dl_cqi[CC_id],
 					format2A);
-	  break;
-        default:
-	  LOG_W(MAC,"Unsupported transmission mode %d\n", get_tmode(module_idP,CC_id,UE_id));
-	  aggregation = 2;
-        }
-      } /* if (continue_flag != 1 */
-
-      if ((ue_sched_ctl->pre_nb_available_rbs[CC_id] == 0) ||  // no RBs allocated 
-	  CCE_allocation_infeasible(module_idP,CC_id,1,subframeP,aggregation,rnti)
-	  ) {
-        LOG_D(MAC,"[eNB %d] Frame %d : no RB allocated for UE %d on CC_id %d: continue \n",
-              module_idP, frameP, UE_id, CC_id);
-        continue_flag=1; //to next user (there might be rbs availiable for other UEs in TM5
-      }
-
-      if (cc[CC_id].tdd_Config != NULL) { //TDD
-        set_ue_dai (subframeP,
-                    UE_id,
-                    CC_id,
-		    cc[CC_id].tdd_Config->subframeAssignment,
-                    UE_list);
-        // update UL DAI after DLSCH scheduling
-        set_ul_DAI(module_idP,UE_id,CC_id,frameP,subframeP);
-      }
-
-      if (continue_flag == 1 ) {
-        add_ue_dlsch_info(module_idP,
-                          CC_id,
-                          UE_id,
-                          subframeP,
-                          S_DL_NONE);
-        continue;
-      }
+		    break;
+		default:
+		    LOG_W(MAC, "Unsupported transmission mode %d\n",
+			  get_tmode(module_idP, CC_id, UE_id));
+		    aggregation = 2;
+		}
+	    }
+	    /* if (continue_flag != 1 */
+	    if ((ue_sched_ctl->pre_nb_available_rbs[CC_id] == 0) ||	// no RBs allocated 
+		CCE_allocation_infeasible(module_idP, CC_id, 1, subframeP,
+					  aggregation, rnti)) {
+		LOG_D(MAC,
+		      "[eNB %d] Frame %d : no RB allocated for UE %d on CC_id %d: continue \n",
+		      module_idP, frameP, UE_id, CC_id);
+		continue_flag = 1;	//to next user (there might be rbs availiable for other UEs in TM5
+	    }
 
+	    if (cc[CC_id].tdd_Config != NULL) {	//TDD
+		set_ue_dai(subframeP,
+			   UE_id,
+			   CC_id,
+			   cc[CC_id].tdd_Config->subframeAssignment,
+			   UE_list);
+		// update UL DAI after DLSCH scheduling
+		set_ul_DAI(module_idP, UE_id, CC_id, frameP, subframeP);
+	    }
+
+	    if (continue_flag == 1) {
+		add_ue_dlsch_info(module_idP,
+				  CC_id, UE_id, subframeP, S_DL_NONE);
+		continue;
+	    }
 #warning RK->CR This old API call has to be revisited for FAPI, or logic must be changed
 #if 0
-      /* add "fake" DCI to have CCE_allocation_infeasible work properly for next allocations */
-      /* if we don't add it, next allocations may succeed but overall allocations may fail */
-      /* will be removed at the end of this function */
-      add_ue_spec_dci(&eNB->common_channels[CC_id].DCI_pdu,
-                      &(char[]){0},
-                      rnti,
-                      1,
-                      aggregation,
-                      1,
-                      format1,
-                      0);
+	    /* add "fake" DCI to have CCE_allocation_infeasible work properly for next allocations */
+	    /* if we don't add it, next allocations may succeed but overall allocations may fail */
+	    /* will be removed at the end of this function */
+	    add_ue_spec_dci(&eNB->common_channels[CC_id].DCI_pdu, &(char[]) {
+			    0}, rnti, 1, aggregation, 1, format1, 0);
 #endif
 
-      nb_available_rb = ue_sched_ctl->pre_nb_available_rbs[CC_id];
+	    nb_available_rb = ue_sched_ctl->pre_nb_available_rbs[CC_id];
 
-      if (cc->tdd_Config) harq_pid = ((frameP*10)+subframeP)%10;
-      else harq_pid = ((frameP*10)+subframeP)&7;
-
-      round = ue_sched_ctl->round[CC_id][harq_pid];
-
-      UE_list->eNB_UE_stats[CC_id][UE_id].crnti= rnti;
-      UE_list->eNB_UE_stats[CC_id][UE_id].rrc_status=mac_eNB_get_rrc_status(module_idP,rnti);
-      UE_list->eNB_UE_stats[CC_id][UE_id].harq_pid = harq_pid; 
-      UE_list->eNB_UE_stats[CC_id][UE_id].harq_round = round;
-
-
-      if (UE_list->eNB_UE_stats[CC_id][UE_id].rrc_status < RRC_CONNECTED) continue;
-
-      sdu_length_total=0;
-      num_sdus=0;
-
-      /*
-      DevCheck(((eNB_UE_stats->dl_cqi < MIN_CQI_VALUE) || (eNB_UE_stats->dl_cqi > MAX_CQI_VALUE)),
-      eNB_UE_stats->dl_cqi, MIN_CQI_VALUE, MAX_CQI_VALUE);
-      */
-      eNB_UE_stats->dlsch_mcs1 = cqi_to_mcs[ue_sched_ctl->dl_cqi[CC_id]];
-      eNB_UE_stats->dlsch_mcs1 = eNB_UE_stats->dlsch_mcs1;//cmin(eNB_UE_stats->dlsch_mcs1, openair_daq_vars.target_ue_dl_mcs);
-
-
-      // store stats
-      //UE_list->eNB_UE_stats[CC_id][UE_id].dl_cqi= eNB_UE_stats->dl_cqi;
-
-      // initializing the rb allocation indicator for each UE
-      for(j=0; j<N_RBG[CC_id]; j++) {
-        UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j] = 0;
-      }
-
-      LOG_D(MAC,"[eNB %d] Frame %d: Scheduling UE %d on CC_id %d (rnti %x, harq_pid %d, round %d, rb %d, cqi %d, mcs %d, rrc %d)\n",
-            module_idP, frameP, UE_id,CC_id,rnti,harq_pid, round,nb_available_rb,
-            ue_sched_ctl->dl_cqi[CC_id], eNB_UE_stats->dlsch_mcs1,
-	    UE_list->eNB_UE_stats[CC_id][UE_id].rrc_status);
-
-
-
-      /* process retransmission  */
-
-      if (round != 8) {
-
-        // get freq_allocation
-        nb_rb = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
-	TBS = get_TBS_DL(UE_list->UE_template[CC_id][UE_id].oldmcs1[harq_pid],nb_rb);
-
-        if (nb_rb <= nb_available_rb) {
-          if (cc[CC_id].tdd_Config != NULL) {
-            UE_list->UE_template[CC_id][UE_id].DAI++;
-            update_ul_dci(module_idP,CC_id,rnti,UE_list->UE_template[CC_id][UE_id].DAI);
-            LOG_D(MAC,"DAI update: CC_id %d subframeP %d: UE %d, DAI %d\n", CC_id,subframeP,UE_id,UE_list->UE_template[CC_id][UE_id].DAI);
-          }
-
-          if(nb_rb == ue_sched_ctl->pre_nb_available_rbs[CC_id]) {
-            for(j=0; j<N_RBG[CC_id]; j++) { // for indicating the rballoc for each sub-band
-              UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j] = ue_sched_ctl->rballoc_sub_UE[CC_id][j];
-            }
-          } else {
-            nb_rb_temp = nb_rb;
-            j = 0;
-
-            while((nb_rb_temp > 0) && (j<N_RBG[CC_id])) {
-              if(ue_sched_ctl->rballoc_sub_UE[CC_id][j] == 1) {
-                if (UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j]) printf("WARN: rballoc_subband not free for retrans?\n");
-                UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j] = ue_sched_ctl->rballoc_sub_UE[CC_id][j];
-
-                if((j == N_RBG[CC_id]-1) &&
-                    ((N_RB_DL[CC_id] == 25)||
-                     (N_RB_DL[CC_id] == 50))) {
-                  nb_rb_temp = nb_rb_temp - min_rb_unit[CC_id]+1;
-                } else {
-                  nb_rb_temp = nb_rb_temp - min_rb_unit[CC_id];
-                }
-              }
-
-              j = j+1;
-            }
-          }
-
-          nb_available_rb -= nb_rb;
-	  /*
-          eNB->mu_mimo_mode[UE_id].pre_nb_available_rbs = nb_rb;
-          eNB->mu_mimo_mode[UE_id].dl_pow_off = ue_sched_ctl->dl_pow_off[CC_id];
-
-          for(j=0; j<N_RBG[CC_id]; j++) {
-            eNB->mu_mimo_mode[UE_id].rballoc_sub[j] = UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j];
-          }
-	  */
-
-          switch (get_tmode(module_idP,CC_id,UE_id)) {
-          case 1:
-          case 2:
-          case 7:
-          default:
-	    dl_config_pdu                                                         = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
-	    memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-	    dl_config_pdu->pdu_type                                               = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE; 
-	    dl_config_pdu->pdu_size                                               = (uint8_t)(2+sizeof(nfapi_dl_config_dci_dl_pdu));
-	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format                  = NFAPI_DL_DCI_FORMAT_1;
-	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level           = get_aggregation(get_bw_index(module_idP,CC_id),ue_sched_ctl->dl_cqi[CC_id],format1);
-	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti                        = rnti;
-	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type                   = 1;    // CRNTI : see Table 4-10 from SCF082 - nFAPI specifications
-	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power          = 6000; // equal to RS power
-
-	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.harq_process                = harq_pid;
-	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tpc                         = 1; // dont adjust power when retransmitting
-	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.new_data_indicator_1        = UE_list->UE_template[CC_id][UE_id].oldNDI[harq_pid];
-	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.mcs_1                       = UE_list->UE_template[CC_id][UE_id].oldmcs1[harq_pid];
-	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.redundancy_version_1        = round&3;
-
-	    if (cc[CC_id].tdd_Config != NULL) { //TDD
-	      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.downlink_assignment_index = (UE_list->UE_template[CC_id][UE_id].DAI-1)&3;
-	      LOG_D(MAC,"[eNB %d] Retransmission CC_id %d : harq_pid %d, round %d, dai %d, mcs %d\n",
-		    module_idP,CC_id,harq_pid,round,
-		    (UE_list->UE_template[CC_id][UE_id].DAI-1),
-		    UE_list->UE_template[CC_id][UE_id].oldmcs1[harq_pid]);
-	    } else {
-	      LOG_D(MAC,"[eNB %d] Retransmission CC_id %d : harq_pid %d, round %d, mcs %d\n",
-		    module_idP,CC_id,harq_pid,round,
-		    UE_list->UE_template[CC_id][UE_id].oldmcs1[harq_pid]);
-	      
-	    }
-	    if (!CCE_allocation_infeasible(module_idP,CC_id,1,subframeP,
-					   dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
-					   rnti)) {
-	      dl_req->number_dci++;
-	      dl_req->number_pdu++;
-
-	      fill_nfapi_dlsch_config(eNB,dl_req,
-				      TBS,
-				      -1            /* retransmission, no pdu_index */,
-				      rnti,
-				      0, // type 0 allocation from 7.1.6 in 36.213
-				      0, // virtual_resource_block_assignment_flag, unused here
-				      0,          // resource_block_coding, to be filled in later
-				      getQm(UE_list->UE_template[CC_id][UE_id].oldmcs1[harq_pid]),
-				      round&3   , // redundancy version
-				      1, // transport blocks
-				      0, // transport block to codeword swap flag
-				      cc[CC_id].p_eNB == 1 ? 0 : 1, // transmission_scheme
-				      1, // number of layers
-				      1, // number of subbands
-			     //			     uint8_t codebook_index,
-				      4, // UE category capacity
-				      UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->pdsch_ConfigDedicated->p_a, 
-				      0, // delta_power_offset for TM5
-				      0, // ngap
-				      0, // nprb
-				      cc[CC_id].p_eNB == 1 ? 1 : 2, // transmission mode
-				      0, //number of PRBs treated as one subband, not used here
-				      0 // number of beamforming vectors, not used here
-				      );
-
-	      LOG_D(MAC,"Filled NFAPI configuration for DCI/DLSCH %d, retransmission round %d\n",eNB->pdu_index[CC_id],round);
-
-	      program_dlsch_acknak(module_idP,CC_id,UE_id,frameP,subframeP,dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.cce_idx);
-	      // No TX request for retransmission (check if null request for FAPI)
-	    }
-	    else {
-	      LOG_W(MAC,"Frame %d, Subframe %d: Dropping DLSCH allocation for UE %d\%x, infeasible CCE allocation\n",
-		    frameP,subframeP,UE_id,rnti);
+	    if (cc->tdd_Config)
+		harq_pid = ((frameP * 10) + subframeP) % 10;
+	    else
+		harq_pid = ((frameP * 10) + subframeP) & 7;
+
+	    round = ue_sched_ctl->round[CC_id][harq_pid];
+
+	    UE_list->eNB_UE_stats[CC_id][UE_id].crnti = rnti;
+	    UE_list->eNB_UE_stats[CC_id][UE_id].rrc_status =
+		mac_eNB_get_rrc_status(module_idP, rnti);
+	    UE_list->eNB_UE_stats[CC_id][UE_id].harq_pid = harq_pid;
+	    UE_list->eNB_UE_stats[CC_id][UE_id].harq_round = round;
+
+
+	    if (UE_list->eNB_UE_stats[CC_id][UE_id].rrc_status <
+		RRC_CONNECTED)
+		continue;
+
+	    sdu_length_total = 0;
+	    num_sdus = 0;
+
+	    /*
+	       DevCheck(((eNB_UE_stats->dl_cqi < MIN_CQI_VALUE) || (eNB_UE_stats->dl_cqi > MAX_CQI_VALUE)),
+	       eNB_UE_stats->dl_cqi, MIN_CQI_VALUE, MAX_CQI_VALUE);
+	     */
+	    eNB_UE_stats->dlsch_mcs1 =
+		cqi_to_mcs[ue_sched_ctl->dl_cqi[CC_id]];
+	    eNB_UE_stats->dlsch_mcs1 = eNB_UE_stats->dlsch_mcs1;	//cmin(eNB_UE_stats->dlsch_mcs1, openair_daq_vars.target_ue_dl_mcs);
+
+
+	    // store stats
+	    //UE_list->eNB_UE_stats[CC_id][UE_id].dl_cqi= eNB_UE_stats->dl_cqi;
+
+	    // initializing the rb allocation indicator for each UE
+	    for (j = 0; j < N_RBG[CC_id]; j++) {
+		UE_list->
+		    UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j]
+		    = 0;
 	    }
-	  }
-
-
-	  add_ue_dlsch_info(module_idP,
-			    CC_id,
-			    UE_id,
-			    subframeP,
-			    S_DL_SCHEDULED);
-	  
-	  //eNB_UE_stats->dlsch_trials[round]++;
-	  UE_list->eNB_UE_stats[CC_id][UE_id].num_retransmission+=1;
-	  UE_list->eNB_UE_stats[CC_id][UE_id].rbs_used_retx=nb_rb;
-	  UE_list->eNB_UE_stats[CC_id][UE_id].total_rbs_used_retx+=nb_rb;
-	  UE_list->eNB_UE_stats[CC_id][UE_id].dlsch_mcs1=eNB_UE_stats->dlsch_mcs1;
-	  UE_list->eNB_UE_stats[CC_id][UE_id].dlsch_mcs2=eNB_UE_stats->dlsch_mcs1;
-	} else {
-          LOG_D(MAC,"[eNB %d] Frame %d CC_id %d : don't schedule UE %d, its retransmission takes more resources than we have\n",
-                module_idP, frameP, CC_id, UE_id);
-        }
-      } else { /* This is a potentially new SDU opportunity */
-	
-        rlc_status.bytes_in_buffer = 0;
-        // Now check RLC information to compute number of required RBs
-        // get maximum TBS size for RLC request
-        TBS = get_TBS_DL(eNB_UE_stats->dlsch_mcs1,nb_available_rb);
-        // check first for RLC data on DCCH
-        // add the length for  all the control elements (timing adv, drx, etc) : header + payload
-
-        if (ue_sched_ctl->ta_timer == 0) {
-          ta_update = ue_sched_ctl->ta_update;
-          /* if we send TA then set timer to not send it for a while */
-          if (ta_update != 31)
-            ue_sched_ctl->ta_timer = 20;
-          /* reset ta_update */
-          ue_sched_ctl->ta_update = 31;
-        } else {
-          ta_update = 31;
-        }
-
-        ta_len = (ta_update != 31) ? 2 : 0;
-
-        header_len_dcch = 2; // 2 bytes DCCH SDU subheader
-
-        if ( TBS-ta_len-header_len_dcch > 0 ) {
-          rlc_status = mac_rlc_status_ind(
-                         module_idP,
-                         rnti,
-			 module_idP,
-                         frameP,
-			 subframeP,
-                         ENB_FLAG_YES,
-                         MBMS_FLAG_NO,
-                         DCCH,
-                         (TBS-ta_len-header_len_dcch)); // transport block set size
-
-          sdu_lengths[0]=0;
-
-          if (rlc_status.bytes_in_buffer > 0) {  // There is DCCH to transmit
-            LOG_D(MAC,"[eNB %d] Frame %d, DL-DCCH->DLSCH CC_id %d, Requesting %d bytes from RLC (RRC message)\n",
-                  module_idP,frameP,CC_id,TBS-header_len_dcch);
-            sdu_lengths[0] = mac_rlc_data_req(
-					      module_idP,
-					      rnti,
-					      module_idP,
-					      frameP,
-					      ENB_FLAG_YES,
-					      MBMS_FLAG_NO,
-					      DCCH,
-					      TBS, //not used
-					      (char *)&dlsch_buffer[0]);
-
-            T(T_ENB_MAC_UE_DL_SDU, T_INT(module_idP), T_INT(CC_id), T_INT(rnti), T_INT(frameP), T_INT(subframeP),
-              T_INT(harq_pid), T_INT(DCCH), T_INT(sdu_lengths[0]));
-
-            LOG_D(MAC,"[eNB %d][DCCH] CC_id %d Got %d bytes from RLC\n",module_idP,CC_id,sdu_lengths[0]);
-            sdu_length_total = sdu_lengths[0];
-            sdu_lcids[0] = DCCH;
-            UE_list->eNB_UE_stats[CC_id][UE_id].num_pdu_tx[DCCH]+=1;
-            UE_list->eNB_UE_stats[CC_id][UE_id].num_bytes_tx[DCCH]+=sdu_lengths[0];
-            num_sdus = 1;
+
+	    LOG_D(MAC,
+		  "[eNB %d] Frame %d: Scheduling UE %d on CC_id %d (rnti %x, harq_pid %d, round %d, rb %d, cqi %d, mcs %d, rrc %d)\n",
+		  module_idP, frameP, UE_id, CC_id, rnti, harq_pid, round,
+		  nb_available_rb, ue_sched_ctl->dl_cqi[CC_id],
+		  eNB_UE_stats->dlsch_mcs1,
+		  UE_list->eNB_UE_stats[CC_id][UE_id].rrc_status);
+
+
+
+	    /* process retransmission  */
+
+	    if (round != 8) {
+
+		// get freq_allocation
+		nb_rb = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
+		TBS =
+		    get_TBS_DL(UE_list->
+			       UE_template[CC_id][UE_id].oldmcs1[harq_pid],
+			       nb_rb);
+
+		if (nb_rb <= nb_available_rb) {
+		    if (cc[CC_id].tdd_Config != NULL) {
+			UE_list->UE_template[CC_id][UE_id].DAI++;
+			update_ul_dci(module_idP, CC_id, rnti,
+				      UE_list->UE_template[CC_id][UE_id].
+				      DAI);
+			LOG_D(MAC,
+			      "DAI update: CC_id %d subframeP %d: UE %d, DAI %d\n",
+			      CC_id, subframeP, UE_id,
+			      UE_list->UE_template[CC_id][UE_id].DAI);
+		    }
+
+		    if (nb_rb == ue_sched_ctl->pre_nb_available_rbs[CC_id]) {
+			for (j = 0; j < N_RBG[CC_id]; j++) {	// for indicating the rballoc for each sub-band
+			    UE_list->UE_template[CC_id][UE_id].
+				rballoc_subband[harq_pid][j] =
+				ue_sched_ctl->rballoc_sub_UE[CC_id][j];
+			}
+		    } else {
+			nb_rb_temp = nb_rb;
+			j = 0;
+
+			while ((nb_rb_temp > 0) && (j < N_RBG[CC_id])) {
+			    if (ue_sched_ctl->rballoc_sub_UE[CC_id][j] ==
+				1) {
+				if (UE_list->
+				    UE_template[CC_id]
+				    [UE_id].rballoc_subband[harq_pid][j])
+				    printf
+					("WARN: rballoc_subband not free for retrans?\n");
+				UE_list->
+				    UE_template[CC_id]
+				    [UE_id].rballoc_subband[harq_pid][j] =
+				    ue_sched_ctl->rballoc_sub_UE[CC_id][j];
+
+				if ((j == N_RBG[CC_id] - 1) &&
+				    ((N_RB_DL[CC_id] == 25) ||
+				     (N_RB_DL[CC_id] == 50))) {
+				    nb_rb_temp =
+					nb_rb_temp - min_rb_unit[CC_id] +
+					1;
+				} else {
+				    nb_rb_temp =
+					nb_rb_temp - min_rb_unit[CC_id];
+				}
+			    }
+
+			    j = j + 1;
+			}
+		    }
+
+		    nb_available_rb -= nb_rb;
+		    /*
+		       eNB->mu_mimo_mode[UE_id].pre_nb_available_rbs = nb_rb;
+		       eNB->mu_mimo_mode[UE_id].dl_pow_off = ue_sched_ctl->dl_pow_off[CC_id];
+
+		       for(j=0; j<N_RBG[CC_id]; j++) {
+		       eNB->mu_mimo_mode[UE_id].rballoc_sub[j] = UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j];
+		       }
+		     */
+
+		    switch (get_tmode(module_idP, CC_id, UE_id)) {
+		    case 1:
+		    case 2:
+		    case 7:
+		    default:
+			dl_config_pdu =
+			    &dl_req->dl_config_pdu_list[dl_req->
+							number_pdu];
+			memset((void *) dl_config_pdu, 0,
+			       sizeof(nfapi_dl_config_request_pdu_t));
+			dl_config_pdu->pdu_type =
+			    NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
+			dl_config_pdu->pdu_size =
+			    (uint8_t) (2 +
+				       sizeof(nfapi_dl_config_dci_dl_pdu));
+			dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+			    dci_format = NFAPI_DL_DCI_FORMAT_1;
+			dl_config_pdu->dci_dl_pdu.
+			    dci_dl_pdu_rel8.aggregation_level =
+			    get_aggregation(get_bw_index
+					    (module_idP, CC_id),
+					    ue_sched_ctl->dl_cqi[CC_id],
+					    format1);
+			dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti =
+			    rnti;
+			dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type = 1;	// CRNTI : see Table 4-10 from SCF082 - nFAPI specifications
+			dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power = 6000;	// equal to RS power
+
+			dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+			    harq_process = harq_pid;
+			dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tpc = 1;	// dont adjust power when retransmitting
+			dl_config_pdu->dci_dl_pdu.
+			    dci_dl_pdu_rel8.new_data_indicator_1 =
+			    UE_list->UE_template[CC_id][UE_id].
+			    oldNDI[harq_pid];
+			dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.mcs_1 =
+			    UE_list->UE_template[CC_id][UE_id].
+			    oldmcs1[harq_pid];
+			dl_config_pdu->dci_dl_pdu.
+			    dci_dl_pdu_rel8.redundancy_version_1 =
+			    round & 3;
+
+			if (cc[CC_id].tdd_Config != NULL) {	//TDD
+			    dl_config_pdu->dci_dl_pdu.
+				dci_dl_pdu_rel8.downlink_assignment_index =
+				(UE_list->UE_template[CC_id][UE_id].DAI -
+				 1) & 3;
+			    LOG_D(MAC,
+				  "[eNB %d] Retransmission CC_id %d : harq_pid %d, round %d, dai %d, mcs %d\n",
+				  module_idP, CC_id, harq_pid, round,
+				  (UE_list->UE_template[CC_id][UE_id].DAI -
+				   1),
+				  UE_list->
+				  UE_template[CC_id][UE_id].oldmcs1
+				  [harq_pid]);
+			} else {
+			    LOG_D(MAC,
+				  "[eNB %d] Retransmission CC_id %d : harq_pid %d, round %d, mcs %d\n",
+				  module_idP, CC_id, harq_pid, round,
+				  UE_list->
+				  UE_template[CC_id][UE_id].oldmcs1
+				  [harq_pid]);
+
+			}
+			if (!CCE_allocation_infeasible
+			    (module_idP, CC_id, 1, subframeP,
+			     dl_config_pdu->dci_dl_pdu.
+			     dci_dl_pdu_rel8.aggregation_level, rnti)) {
+			    dl_req->number_dci++;
+			    dl_req->number_pdu++;
+
+			    fill_nfapi_dlsch_config(eNB, dl_req, TBS, -1
+						    /* retransmission, no pdu_index */
+						    , rnti, 0,	// type 0 allocation from 7.1.6 in 36.213
+						    0,	// virtual_resource_block_assignment_flag, unused here
+						    0,	// resource_block_coding, to be filled in later
+						    getQm(UE_list->UE_template[CC_id][UE_id].oldmcs1[harq_pid]), round & 3,	// redundancy version
+						    1,	// transport blocks
+						    0,	// transport block to codeword swap flag
+						    cc[CC_id].p_eNB == 1 ? 0 : 1,	// transmission_scheme
+						    1,	// number of layers
+						    1,	// number of subbands
+						    //                      uint8_t codebook_index,
+						    4,	// UE category capacity
+						    UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->pdsch_ConfigDedicated->p_a, 0,	// delta_power_offset for TM5
+						    0,	// ngap
+						    0,	// nprb
+						    cc[CC_id].p_eNB == 1 ? 1 : 2,	// transmission mode
+						    0,	//number of PRBs treated as one subband, not used here
+						    0	// number of beamforming vectors, not used here
+				);
+
+			    LOG_D(MAC,
+				  "Filled NFAPI configuration for DCI/DLSCH %d, retransmission round %d\n",
+				  eNB->pdu_index[CC_id], round);
+
+			    program_dlsch_acknak(module_idP, CC_id, UE_id,
+						 frameP, subframeP,
+						 dl_config_pdu->
+						 dci_dl_pdu.dci_dl_pdu_rel8.
+						 cce_idx);
+			    // No TX request for retransmission (check if null request for FAPI)
+			} else {
+			    LOG_W(MAC,
+				  "Frame %d, Subframe %d: Dropping DLSCH allocation for UE %d\%x, infeasible CCE allocation\n",
+				  frameP, subframeP, UE_id, rnti);
+			}
+		    }
+
+
+		    add_ue_dlsch_info(module_idP,
+				      CC_id, UE_id, subframeP,
+				      S_DL_SCHEDULED);
+
+		    //eNB_UE_stats->dlsch_trials[round]++;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].
+			num_retransmission += 1;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].rbs_used_retx =
+			nb_rb;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].
+			total_rbs_used_retx += nb_rb;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].dlsch_mcs1 =
+			eNB_UE_stats->dlsch_mcs1;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].dlsch_mcs2 =
+			eNB_UE_stats->dlsch_mcs1;
+		} else {
+		    LOG_D(MAC,
+			  "[eNB %d] Frame %d CC_id %d : don't schedule UE %d, its retransmission takes more resources than we have\n",
+			  module_idP, frameP, CC_id, UE_id);
+		}
+	    } else {		/* This is a potentially new SDU opportunity */
+
+		rlc_status.bytes_in_buffer = 0;
+		// Now check RLC information to compute number of required RBs
+		// get maximum TBS size for RLC request
+		TBS =
+		    get_TBS_DL(eNB_UE_stats->dlsch_mcs1, nb_available_rb);
+		// check first for RLC data on DCCH
+		// add the length for  all the control elements (timing adv, drx, etc) : header + payload
+
+		if (ue_sched_ctl->ta_timer == 0) {
+		    ta_update = ue_sched_ctl->ta_update;
+		    /* if we send TA then set timer to not send it for a while */
+		    if (ta_update != 31)
+			ue_sched_ctl->ta_timer = 20;
+		    /* reset ta_update */
+		    ue_sched_ctl->ta_update = 31;
+		} else {
+		    ta_update = 31;
+		}
+
+		ta_len = (ta_update != 31) ? 2 : 0;
+
+		header_len_dcch = 2;	// 2 bytes DCCH SDU subheader
+
+		if (TBS - ta_len - header_len_dcch > 0) {
+		    rlc_status = mac_rlc_status_ind(module_idP, rnti, module_idP, frameP, subframeP, ENB_FLAG_YES, MBMS_FLAG_NO, DCCH, (TBS - ta_len - header_len_dcch));	// transport block set size
+
+		    sdu_lengths[0] = 0;
+
+		    if (rlc_status.bytes_in_buffer > 0) {	// There is DCCH to transmit
+			LOG_D(MAC,
+			      "[eNB %d] Frame %d, DL-DCCH->DLSCH CC_id %d, Requesting %d bytes from RLC (RRC message)\n",
+			      module_idP, frameP, CC_id,
+			      TBS - header_len_dcch);
+			sdu_lengths[0] = mac_rlc_data_req(module_idP, rnti, module_idP, frameP, ENB_FLAG_YES, MBMS_FLAG_NO, DCCH, TBS,	//not used
+							  (char *)
+							  &dlsch_buffer
+							  [0]);
+
+			T(T_ENB_MAC_UE_DL_SDU, T_INT(module_idP),
+			  T_INT(CC_id), T_INT(rnti), T_INT(frameP),
+			  T_INT(subframeP), T_INT(harq_pid), T_INT(DCCH),
+			  T_INT(sdu_lengths[0]));
+
+			LOG_D(MAC,
+			      "[eNB %d][DCCH] CC_id %d Got %d bytes from RLC\n",
+			      module_idP, CC_id, sdu_lengths[0]);
+			sdu_length_total = sdu_lengths[0];
+			sdu_lcids[0] = DCCH;
+			UE_list->eNB_UE_stats[CC_id][UE_id].
+			    num_pdu_tx[DCCH] += 1;
+			UE_list->
+			    eNB_UE_stats[CC_id][UE_id].num_bytes_tx[DCCH]
+			    += sdu_lengths[0];
+			num_sdus = 1;
 #ifdef DEBUG_eNB_SCHEDULER
-            LOG_T(MAC,"[eNB %d][DCCH] CC_id %d Got %d bytes :",module_idP,CC_id,sdu_lengths[0]);
+			LOG_T(MAC,
+			      "[eNB %d][DCCH] CC_id %d Got %d bytes :",
+			      module_idP, CC_id, sdu_lengths[0]);
 
-            for (j=0; j<sdu_lengths[0]; j++) {
-              LOG_T(MAC,"%x ",dlsch_buffer[j]);
-            }
+			for (j = 0; j < sdu_lengths[0]; j++) {
+			    LOG_T(MAC, "%x ", dlsch_buffer[j]);
+			}
 
-            LOG_T(MAC,"\n");
+			LOG_T(MAC, "\n");
 #endif
-          } else {
-            header_len_dcch = 0;
-            sdu_length_total = 0;
-          }
-        }
-	
-        // check for DCCH1 and update header information (assume 2 byte sub-header)
-        if (TBS-ta_len-header_len_dcch-sdu_length_total > 0 ) {
-          rlc_status = mac_rlc_status_ind(
-                         module_idP,
-                         rnti,
-			 module_idP,
-                         frameP,
-						 subframeP,
-                         ENB_FLAG_YES,
-                         MBMS_FLAG_NO,
-                         DCCH+1,
-                         (TBS-ta_len-header_len_dcch-sdu_length_total)); // transport block set size less allocations for timing advance and
-          // DCCH SDU
-	  sdu_lengths[num_sdus] = 0;
-
-          if (rlc_status.bytes_in_buffer > 0) {
-            LOG_D(MAC,"[eNB %d], Frame %d, DCCH1->DLSCH, CC_id %d, Requesting %d bytes from RLC (RRC message)\n",
-                  module_idP,frameP,CC_id,TBS-header_len_dcch-sdu_length_total);
-            sdu_lengths[num_sdus] += mac_rlc_data_req(
-                                       module_idP,
-                                       rnti,
-				       module_idP,
-                                       frameP,
-                                       ENB_FLAG_YES,
-                                       MBMS_FLAG_NO,
-                                       DCCH+1,
-									   TBS, //not used
-                                       (char *)&dlsch_buffer[sdu_length_total]);
-
-            T(T_ENB_MAC_UE_DL_SDU, T_INT(module_idP), T_INT(CC_id), T_INT(rnti), T_INT(frameP), T_INT(subframeP),
-              T_INT(harq_pid), T_INT(DCCH+1), T_INT(sdu_lengths[num_sdus]));
-
-            sdu_lcids[num_sdus] = DCCH1;
-            sdu_length_total += sdu_lengths[num_sdus];
-            header_len_dcch += 2;
-            UE_list->eNB_UE_stats[CC_id][UE_id].num_pdu_tx[DCCH1]+=1;
-            UE_list->eNB_UE_stats[CC_id][UE_id].num_bytes_tx[DCCH1]+=sdu_lengths[num_sdus];
-	    num_sdus++;
+		    } else {
+			header_len_dcch = 0;
+			sdu_length_total = 0;
+		    }
+		}
+		// check for DCCH1 and update header information (assume 2 byte sub-header)
+		if (TBS - ta_len - header_len_dcch - sdu_length_total > 0) {
+		    rlc_status = mac_rlc_status_ind(module_idP, rnti, module_idP, frameP, subframeP, ENB_FLAG_YES, MBMS_FLAG_NO, DCCH + 1, (TBS - ta_len - header_len_dcch - sdu_length_total));	// transport block set size less allocations for timing advance and
+		    // DCCH SDU
+		    sdu_lengths[num_sdus] = 0;
+
+		    if (rlc_status.bytes_in_buffer > 0) {
+			LOG_D(MAC,
+			      "[eNB %d], Frame %d, DCCH1->DLSCH, CC_id %d, Requesting %d bytes from RLC (RRC message)\n",
+			      module_idP, frameP, CC_id,
+			      TBS - header_len_dcch - sdu_length_total);
+			sdu_lengths[num_sdus] += mac_rlc_data_req(module_idP, rnti, module_idP, frameP, ENB_FLAG_YES, MBMS_FLAG_NO, DCCH + 1, TBS,	//not used
+								  (char *)
+								  &dlsch_buffer
+								  [sdu_length_total]);
+
+			T(T_ENB_MAC_UE_DL_SDU, T_INT(module_idP),
+			  T_INT(CC_id), T_INT(rnti), T_INT(frameP),
+			  T_INT(subframeP), T_INT(harq_pid),
+			  T_INT(DCCH + 1), T_INT(sdu_lengths[num_sdus]));
+
+			sdu_lcids[num_sdus] = DCCH1;
+			sdu_length_total += sdu_lengths[num_sdus];
+			header_len_dcch += 2;
+			UE_list->eNB_UE_stats[CC_id][UE_id].
+			    num_pdu_tx[DCCH1] += 1;
+			UE_list->
+			    eNB_UE_stats[CC_id][UE_id].num_bytes_tx[DCCH1]
+			    += sdu_lengths[num_sdus];
+			num_sdus++;
 #ifdef DEBUG_eNB_SCHEDULER
-            LOG_T(MAC,"[eNB %d][DCCH1] CC_id %d Got %d bytes :",module_idP,CC_id,sdu_lengths[num_sdus]);
+			LOG_T(MAC,
+			      "[eNB %d][DCCH1] CC_id %d Got %d bytes :",
+			      module_idP, CC_id, sdu_lengths[num_sdus]);
 
-            for (j=0; j<sdu_lengths[num_sdus]; j++) {
-              LOG_T(MAC,"%x ",dlsch_buffer[j]);
-            }
+			for (j = 0; j < sdu_lengths[num_sdus]; j++) {
+			    LOG_T(MAC, "%x ", dlsch_buffer[j]);
+			}
 
-            LOG_T(MAC,"\n");
+			LOG_T(MAC, "\n");
 #endif
 
-	  }
-        }
-
-	// assume the max dtch header size, and adjust it later
-	header_len_dtch=0;
-	header_len_dtch_last=0; // the header length of the last mac sdu
-	// lcid has to be sorted before the actual allocation (similar struct as ue_list).
-	for (lcid=NB_RB_MAX-1; lcid>=DTCH ; lcid--){
-	  // TBD: check if the lcid is active
-	  
-	  header_len_dtch+=3; 
-	  header_len_dtch_last=3;
-	  LOG_D(MAC,"[eNB %d], Frame %d, DTCH%d->DLSCH, Checking RLC status (tbs %d, len %d)\n",
-		module_idP,frameP,lcid,TBS,
-		TBS-ta_len-header_len_dcch-sdu_length_total-header_len_dtch);
-	  
-	  if (TBS-ta_len-header_len_dcch-sdu_length_total-header_len_dtch > 0 ) { // NN: > 2 ? 
-	    rlc_status = mac_rlc_status_ind(module_idP,
-					    rnti,
-					    module_idP,
-					    frameP,
-						subframeP,
-					    ENB_FLAG_YES,
-					    MBMS_FLAG_NO,
-					    lcid,
-					    TBS-ta_len-header_len_dcch-sdu_length_total-header_len_dtch);
-	   
-
-	    if (rlc_status.bytes_in_buffer > 0) {
-	      
-	      LOG_D(MAC,"[eNB %d][USER-PLANE DEFAULT DRB] Frame %d : DTCH->DLSCH, Requesting %d bytes from RLC (lcid %d total hdr len %d)\n",
-		    module_idP,frameP,TBS-header_len_dcch-sdu_length_total-header_len_dtch,lcid, header_len_dtch);
-	      sdu_lengths[num_sdus] = mac_rlc_data_req(module_idP,
-						       rnti,
-						       module_idP,
-						       frameP,
-						       ENB_FLAG_YES,
-						       MBMS_FLAG_NO,
-						       lcid,
-							   TBS,	//not used
-						       (char*)&dlsch_buffer[sdu_length_total]);
-	      T(T_ENB_MAC_UE_DL_SDU, T_INT(module_idP), T_INT(CC_id), T_INT(rnti), T_INT(frameP), T_INT(subframeP),
-              T_INT(harq_pid), T_INT(lcid), T_INT(sdu_lengths[num_sdus]));
-
-	      LOG_D(MAC,"[eNB %d][USER-PLANE DEFAULT DRB] Got %d bytes for DTCH %d \n",module_idP,sdu_lengths[num_sdus],lcid);
-	      sdu_lcids[num_sdus] = lcid;
-	      sdu_length_total += sdu_lengths[num_sdus];
-	      UE_list->eNB_UE_stats[CC_id][UE_id].num_pdu_tx[lcid]+=1;
-	      UE_list->eNB_UE_stats[CC_id][UE_id].num_bytes_tx[lcid]+=sdu_lengths[num_sdus];
-	      if (sdu_lengths[num_sdus] < 128) {
-		header_len_dtch--;
-		header_len_dtch_last--;
-	      }
-	      num_sdus++;
-	    } // no data for this LCID
-	    else {
-	      header_len_dtch-=3;
-	    }
-	  } // no TBS left
-	  else {
-	    header_len_dtch-=3;
-	    break; 
-	  }
-	}
-	if (header_len_dtch == 0 )
-	  header_len_dtch_last= 0;
-	// there is at least one SDU 
-	// if (num_sdus > 0 ){
-	if ((sdu_length_total + header_len_dcch + header_len_dtch )> 0) {
-	  
-	  // Now compute number of required RBs for total sdu length
-	  // Assume RAH format 2
-	  // adjust  header lengths
-	  header_len_dcch_tmp = header_len_dcch;
-	  header_len_dtch_tmp = header_len_dtch;
-	  if (header_len_dtch==0) {
-	    header_len_dcch = (header_len_dcch >0) ? 1 : 0;//header_len_dcch;  // remove length field
-	  } else {
-	    header_len_dtch_last-=1; // now use it to find how many bytes has to be removed for the last MAC SDU 
-	    header_len_dtch = (header_len_dtch > 0) ? header_len_dtch - header_len_dtch_last  :header_len_dtch;     // remove length field for the last SDU
-	  }
-
-          mcs = eNB_UE_stats->dlsch_mcs1;
-
-          if (mcs==0) {
-            nb_rb = 4;  // don't let the TBS get too small
-          } else {
-            nb_rb=min_rb_unit[CC_id];
-          }
-
-          TBS = get_TBS_DL(mcs,nb_rb);
-
-          while (TBS < (sdu_length_total + header_len_dcch + header_len_dtch + ta_len))  {
-            nb_rb += min_rb_unit[CC_id];  //
-
-            if (nb_rb>nb_available_rb) { // if we've gone beyond the maximum number of RBs
-              // (can happen if N_RB_DL is odd)
-              TBS = get_TBS_DL(eNB_UE_stats->dlsch_mcs1,nb_available_rb);
-              nb_rb = nb_available_rb;
-              break;
-            }
-
-            TBS = get_TBS_DL(eNB_UE_stats->dlsch_mcs1,nb_rb);
-          }
-
-          if(nb_rb == ue_sched_ctl->pre_nb_available_rbs[CC_id]) {
-            for(j=0; j<N_RBG[CC_id]; j++) { // for indicating the rballoc for each sub-band
-              UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j] = ue_sched_ctl->rballoc_sub_UE[CC_id][j];
-            }
-          } else {
-            nb_rb_temp = nb_rb;
-            j = 0;
-
-            while((nb_rb_temp > 0) && (j<N_RBG[CC_id])) {
-              if(ue_sched_ctl->rballoc_sub_UE[CC_id][j] == 1) {
-                UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j] = ue_sched_ctl->rballoc_sub_UE[CC_id][j];
-
-                if ((j == N_RBG[CC_id]-1) &&
-                    ((N_RB_DL[CC_id] == 25)||
-                     (N_RB_DL[CC_id] == 50))) {
-                  nb_rb_temp = nb_rb_temp - min_rb_unit[CC_id]+1;
-                } else {
-                  nb_rb_temp = nb_rb_temp - min_rb_unit[CC_id];
-                }
-              }
-
-              j = j+1;
-            }
-          }
-
-          // decrease mcs until TBS falls below required length
-          while ((TBS > (sdu_length_total + header_len_dcch + header_len_dtch + ta_len)) && (mcs>0)) {
-            mcs--;
-            TBS = get_TBS_DL(mcs,nb_rb);
-          }
-
-          // if we have decreased too much or we don't have enough RBs, increase MCS
-          while ((TBS < (sdu_length_total + header_len_dcch + header_len_dtch + ta_len)) && ((( ue_sched_ctl->dl_pow_off[CC_id]>0) && (mcs<28))
-                 || ( (ue_sched_ctl->dl_pow_off[CC_id]==0) && (mcs<=15)))) {
-            mcs++;
-            TBS = get_TBS_DL(mcs,nb_rb);
-          }
-
-          LOG_D(MAC,"dlsch_mcs before and after the rate matching = (%d, %d)\n",eNB_UE_stats->dlsch_mcs1, mcs);
+		    }
+		}
+		// assume the max dtch header size, and adjust it later
+		header_len_dtch = 0;
+		header_len_dtch_last = 0;	// the header length of the last mac sdu
+		// lcid has to be sorted before the actual allocation (similar struct as ue_list).
+		for (lcid = NB_RB_MAX - 1; lcid >= DTCH; lcid--) {
+		    // TBD: check if the lcid is active
+
+		    header_len_dtch += 3;
+		    header_len_dtch_last = 3;
+		    LOG_D(MAC,
+			  "[eNB %d], Frame %d, DTCH%d->DLSCH, Checking RLC status (tbs %d, len %d)\n",
+			  module_idP, frameP, lcid, TBS,
+			  TBS - ta_len - header_len_dcch -
+			  sdu_length_total - header_len_dtch);
+
+		    if (TBS - ta_len - header_len_dcch - sdu_length_total - header_len_dtch > 0) {	// NN: > 2 ? 
+			rlc_status = mac_rlc_status_ind(module_idP,
+							rnti,
+							module_idP,
+							frameP,
+							subframeP,
+							ENB_FLAG_YES,
+							MBMS_FLAG_NO,
+							lcid,
+							TBS - ta_len -
+							header_len_dcch -
+							sdu_length_total -
+							header_len_dtch);
+
+
+			if (rlc_status.bytes_in_buffer > 0) {
+
+			    LOG_D(MAC,
+				  "[eNB %d][USER-PLANE DEFAULT DRB] Frame %d : DTCH->DLSCH, Requesting %d bytes from RLC (lcid %d total hdr len %d)\n",
+				  module_idP, frameP,
+				  TBS - header_len_dcch -
+				  sdu_length_total - header_len_dtch, lcid,
+				  header_len_dtch);
+			    sdu_lengths[num_sdus] = mac_rlc_data_req(module_idP, rnti, module_idP, frameP, ENB_FLAG_YES, MBMS_FLAG_NO, lcid, TBS,	//not used
+								     (char
+								      *)
+								     &dlsch_buffer
+								     [sdu_length_total]);
+			    T(T_ENB_MAC_UE_DL_SDU, T_INT(module_idP),
+			      T_INT(CC_id), T_INT(rnti), T_INT(frameP),
+			      T_INT(subframeP), T_INT(harq_pid),
+			      T_INT(lcid), T_INT(sdu_lengths[num_sdus]));
+
+			    LOG_D(MAC,
+				  "[eNB %d][USER-PLANE DEFAULT DRB] Got %d bytes for DTCH %d \n",
+				  module_idP, sdu_lengths[num_sdus], lcid);
+			    sdu_lcids[num_sdus] = lcid;
+			    sdu_length_total += sdu_lengths[num_sdus];
+			    UE_list->
+				eNB_UE_stats[CC_id][UE_id].num_pdu_tx[lcid]
+				+= 1;
+			    UE_list->
+				eNB_UE_stats[CC_id][UE_id].num_bytes_tx
+				[lcid] += sdu_lengths[num_sdus];
+			    if (sdu_lengths[num_sdus] < 128) {
+				header_len_dtch--;
+				header_len_dtch_last--;
+			    }
+			    num_sdus++;
+			}	// no data for this LCID
+			else {
+			    header_len_dtch -= 3;
+			}
+		    }		// no TBS left
+		    else {
+			header_len_dtch -= 3;
+			break;
+		    }
+		}
+		if (header_len_dtch == 0)
+		    header_len_dtch_last = 0;
+		// there is at least one SDU 
+		// if (num_sdus > 0 ){
+		if ((sdu_length_total + header_len_dcch +
+		     header_len_dtch) > 0) {
+
+		    // Now compute number of required RBs for total sdu length
+		    // Assume RAH format 2
+		    // adjust  header lengths
+		    header_len_dcch_tmp = header_len_dcch;
+		    header_len_dtch_tmp = header_len_dtch;
+		    if (header_len_dtch == 0) {
+			header_len_dcch = (header_len_dcch > 0) ? 1 : 0;	//header_len_dcch;  // remove length field
+		    } else {
+			header_len_dtch_last -= 1;	// now use it to find how many bytes has to be removed for the last MAC SDU 
+			header_len_dtch = (header_len_dtch > 0) ? header_len_dtch - header_len_dtch_last : header_len_dtch;	// remove length field for the last SDU
+		    }
+
+		    mcs = eNB_UE_stats->dlsch_mcs1;
+
+		    if (mcs == 0) {
+			nb_rb = 4;	// don't let the TBS get too small
+		    } else {
+			nb_rb = min_rb_unit[CC_id];
+		    }
+
+		    TBS = get_TBS_DL(mcs, nb_rb);
+
+		    while (TBS <
+			   (sdu_length_total + header_len_dcch +
+			    header_len_dtch + ta_len)) {
+			nb_rb += min_rb_unit[CC_id];	//
+
+			if (nb_rb > nb_available_rb) {	// if we've gone beyond the maximum number of RBs
+			    // (can happen if N_RB_DL is odd)
+			    TBS =
+				get_TBS_DL(eNB_UE_stats->dlsch_mcs1,
+					   nb_available_rb);
+			    nb_rb = nb_available_rb;
+			    break;
+			}
+
+			TBS = get_TBS_DL(eNB_UE_stats->dlsch_mcs1, nb_rb);
+		    }
+
+		    if (nb_rb == ue_sched_ctl->pre_nb_available_rbs[CC_id]) {
+			for (j = 0; j < N_RBG[CC_id]; j++) {	// for indicating the rballoc for each sub-band
+			    UE_list->UE_template[CC_id][UE_id].
+				rballoc_subband[harq_pid][j] =
+				ue_sched_ctl->rballoc_sub_UE[CC_id][j];
+			}
+		    } else {
+			nb_rb_temp = nb_rb;
+			j = 0;
+
+			while ((nb_rb_temp > 0) && (j < N_RBG[CC_id])) {
+			    if (ue_sched_ctl->rballoc_sub_UE[CC_id][j] ==
+				1) {
+				UE_list->
+				    UE_template[CC_id]
+				    [UE_id].rballoc_subband[harq_pid][j] =
+				    ue_sched_ctl->rballoc_sub_UE[CC_id][j];
+
+				if ((j == N_RBG[CC_id] - 1) &&
+				    ((N_RB_DL[CC_id] == 25) ||
+				     (N_RB_DL[CC_id] == 50))) {
+				    nb_rb_temp =
+					nb_rb_temp - min_rb_unit[CC_id] +
+					1;
+				} else {
+				    nb_rb_temp =
+					nb_rb_temp - min_rb_unit[CC_id];
+				}
+			    }
+
+			    j = j + 1;
+			}
+		    }
+
+		    // decrease mcs until TBS falls below required length
+		    while ((TBS >
+			    (sdu_length_total + header_len_dcch +
+			     header_len_dtch + ta_len)) && (mcs > 0)) {
+			mcs--;
+			TBS = get_TBS_DL(mcs, nb_rb);
+		    }
+
+		    // if we have decreased too much or we don't have enough RBs, increase MCS
+		    while ((TBS <
+			    (sdu_length_total + header_len_dcch +
+			     header_len_dtch + ta_len))
+			   && (((ue_sched_ctl->dl_pow_off[CC_id] > 0)
+				&& (mcs < 28))
+			       || ((ue_sched_ctl->dl_pow_off[CC_id] == 0)
+				   && (mcs <= 15)))) {
+			mcs++;
+			TBS = get_TBS_DL(mcs, nb_rb);
+		    }
+
+		    LOG_D(MAC,
+			  "dlsch_mcs before and after the rate matching = (%d, %d)\n",
+			  eNB_UE_stats->dlsch_mcs1, mcs);
 
 #ifdef DEBUG_eNB_SCHEDULER
-          LOG_D(MAC,"[eNB %d] CC_id %d Generated DLSCH header (mcs %d, TBS %d, nb_rb %d)\n",
-                module_idP,CC_id,mcs,TBS,nb_rb);
-          // msg("[MAC][eNB ] Reminder of DLSCH with random data %d %d %d %d \n",
-          //  TBS, sdu_length_total, offset, TBS-sdu_length_total-offset);
+		    LOG_D(MAC,
+			  "[eNB %d] CC_id %d Generated DLSCH header (mcs %d, TBS %d, nb_rb %d)\n",
+			  module_idP, CC_id, mcs, TBS, nb_rb);
+		    // msg("[MAC][eNB ] Reminder of DLSCH with random data %d %d %d %d \n",
+		    //  TBS, sdu_length_total, offset, TBS-sdu_length_total-offset);
 #endif
 
-          if ((TBS - header_len_dcch - header_len_dtch - sdu_length_total - ta_len) <= 2) {
-            padding = (TBS - header_len_dcch - header_len_dtch - sdu_length_total - ta_len);
-            post_padding = 0;
-          } else {
-            padding = 0;
-
-            // adjust the header len
-            if (header_len_dtch==0) {
-              header_len_dcch = header_len_dcch_tmp;
-            } else { //if (( header_len_dcch==0)&&((header_len_dtch==1)||(header_len_dtch==2)))
-              header_len_dtch = header_len_dtch_tmp;
-            }
-
-            post_padding = TBS - sdu_length_total - header_len_dcch - header_len_dtch - ta_len ; // 1 is for the postpadding header
-          }
-
-
-          offset = generate_dlsch_header((unsigned char*)UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0],
-                                         num_sdus,              //num_sdus
-                                         sdu_lengths,  //
-                                         sdu_lcids,
-                                         255,                                   // no drx
-                                         ta_update, // timing advance
-                                         NULL,                                  // contention res id
-                                         padding,
-                                         post_padding);
-
-          //#ifdef DEBUG_eNB_SCHEDULER
-          if (ta_update != 31) {
-            LOG_D(MAC,
-                  "[eNB %d][DLSCH] Frame %d Generate header for UE_id %d on CC_id %d: sdu_length_total %d, num_sdus %d, sdu_lengths[0] %d, sdu_lcids[0] %d => payload offset %d,timing advance value : %d, padding %d,post_padding %d,(mcs %d, TBS %d, nb_rb %d),header_dcch %d, header_dtch %d\n",
-                  module_idP,frameP, UE_id, CC_id, sdu_length_total,num_sdus,sdu_lengths[0],sdu_lcids[0],offset,
-                  ta_update,padding,post_padding,mcs,TBS,nb_rb,header_len_dcch,header_len_dtch);
-	  }
-          //#endif
+		    if ((TBS - header_len_dcch - header_len_dtch -
+			 sdu_length_total - ta_len) <= 2) {
+			padding =
+			    (TBS - header_len_dcch - header_len_dtch -
+			     sdu_length_total - ta_len);
+			post_padding = 0;
+		    } else {
+			padding = 0;
+
+			// adjust the header len
+			if (header_len_dtch == 0) {
+			    header_len_dcch = header_len_dcch_tmp;
+			} else {	//if (( header_len_dcch==0)&&((header_len_dtch==1)||(header_len_dtch==2)))
+			    header_len_dtch = header_len_dtch_tmp;
+			}
+
+			post_padding = TBS - sdu_length_total - header_len_dcch - header_len_dtch - ta_len;	// 1 is for the postpadding header
+		    }
+
+
+		    offset = generate_dlsch_header((unsigned char *) UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0], num_sdus,	//num_sdus
+						   sdu_lengths,	//
+						   sdu_lcids, 255,	// no drx
+						   ta_update,	// timing advance
+						   NULL,	// contention res id
+						   padding, post_padding);
+
+		    //#ifdef DEBUG_eNB_SCHEDULER
+		    if (ta_update != 31) {
+			LOG_D(MAC,
+			      "[eNB %d][DLSCH] Frame %d Generate header for UE_id %d on CC_id %d: sdu_length_total %d, num_sdus %d, sdu_lengths[0] %d, sdu_lcids[0] %d => payload offset %d,timing advance value : %d, padding %d,post_padding %d,(mcs %d, TBS %d, nb_rb %d),header_dcch %d, header_dtch %d\n",
+			      module_idP, frameP, UE_id, CC_id,
+			      sdu_length_total, num_sdus, sdu_lengths[0],
+			      sdu_lcids[0], offset, ta_update, padding,
+			      post_padding, mcs, TBS, nb_rb,
+			      header_len_dcch, header_len_dtch);
+		    }
+		    //#endif
 #ifdef DEBUG_eNB_SCHEDULER
-          LOG_T(MAC,"[eNB %d] First 16 bytes of DLSCH : \n");
+		    LOG_T(MAC, "[eNB %d] First 16 bytes of DLSCH : \n");
 
-          for (i=0; i<16; i++) {
-            LOG_T(MAC,"%x.",dlsch_buffer[i]);
-          }
+		    for (i = 0; i < 16; i++) {
+			LOG_T(MAC, "%x.", dlsch_buffer[i]);
+		    }
 
-          LOG_T(MAC,"\n");
+		    LOG_T(MAC, "\n");
 #endif
 
-          // cycle through SDUs and place in dlsch_buffer
-          memcpy(&UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0][offset],dlsch_buffer,sdu_length_total);
-          // memcpy(RC.mac[0].DLSCH_pdu[0][0].payload[0][offset],dcch_buffer,sdu_lengths[0]);
-
-          // fill remainder of DLSCH with random data
-          for (j=0; j<(TBS-sdu_length_total-offset); j++) {
-            UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0][offset+sdu_length_total+j] = (char)(taus()&0xff);
-          }
-
-
-          if (opt_enabled == 1) {
-            trace_pdu(1, (uint8_t *)UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0],
-                      TBS, module_idP, 3, UE_RNTI(module_idP,UE_id),
-                      eNB->frame, eNB->subframe,0,0);
-            LOG_D(OPT,"[eNB %d][DLSCH] CC_id %d Frame %d  rnti %x  with size %d\n",
-                  module_idP, CC_id, frameP, UE_RNTI(module_idP,UE_id), TBS);
-          }
-
-          T(T_ENB_MAC_UE_DL_PDU_WITH_DATA, T_INT(module_idP), T_INT(CC_id), T_INT(rnti), T_INT(frameP), T_INT(subframeP),
-            T_INT(harq_pid), T_BUFFER(UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0], TBS));
-
-	  UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid] = nb_rb;
-
-          add_ue_dlsch_info(module_idP,
-                            CC_id,
-                            UE_id,
-                            subframeP,
-                            S_DL_SCHEDULED);
-          // store stats
-          eNB->eNB_stats[CC_id].dlsch_bytes_tx+=sdu_length_total;
-          eNB->eNB_stats[CC_id].dlsch_pdus_tx+=1;
-
-          UE_list->eNB_UE_stats[CC_id][UE_id].rbs_used = nb_rb;
-          UE_list->eNB_UE_stats[CC_id][UE_id].total_rbs_used += nb_rb;
-          UE_list->eNB_UE_stats[CC_id][UE_id].dlsch_mcs1=eNB_UE_stats->dlsch_mcs1;
-          UE_list->eNB_UE_stats[CC_id][UE_id].dlsch_mcs2=mcs;
-          UE_list->eNB_UE_stats[CC_id][UE_id].TBS = TBS;
-
-          UE_list->eNB_UE_stats[CC_id][UE_id].overhead_bytes= TBS- sdu_length_total;
-          UE_list->eNB_UE_stats[CC_id][UE_id].total_sdu_bytes+= sdu_length_total;
-          UE_list->eNB_UE_stats[CC_id][UE_id].total_pdu_bytes+= TBS;
-          UE_list->eNB_UE_stats[CC_id][UE_id].total_num_pdus+=1;
-
-          if (cc[CC_id].tdd_Config != NULL) { // TDD
-            UE_list->UE_template[CC_id][UE_id].DAI++;
-            update_ul_dci(module_idP,CC_id,rnti,UE_list->UE_template[CC_id][UE_id].DAI);
-          }
-
-	  // do PUCCH power control
-          // this is the normalized RX power
-	  eNB_UE_stats =  &UE_list->eNB_UE_stats[CC_id][UE_id];
-
-          /* TODO: fix how we deal with power, unit is not dBm, it's special from nfapi */
-	  normalized_rx_power = ue_sched_ctl->pucch1_snr[CC_id];
-	  target_rx_power = 208;
-	    
-          // this assumes accumulated tpc
-	  // make sure that we are only sending a tpc update once a frame, otherwise the control loop will freak out
-	  int32_t framex10psubframe = UE_list->UE_template[CC_id][UE_id].pucch_tpc_tx_frame*10+UE_list->UE_template[CC_id][UE_id].pucch_tpc_tx_subframe;
-          if (((framex10psubframe+10)<=(frameP*10+subframeP)) || //normal case
-	      ((framex10psubframe>(frameP*10+subframeP)) && (((10240-framex10psubframe+frameP*10+subframeP)>=10)))) //frame wrap-around
-	    if (ue_sched_ctl->pucch1_cqi_update[CC_id] == 1) { 
-	      ue_sched_ctl->pucch1_cqi_update[CC_id] = 0;
-
-	      UE_list->UE_template[CC_id][UE_id].pucch_tpc_tx_frame=frameP;
-	      UE_list->UE_template[CC_id][UE_id].pucch_tpc_tx_subframe=subframeP;
-	      
-	      if (normalized_rx_power>(target_rx_power+4)) {
-		tpc = 0; //-1
-		tpc_accumulated--;
-	      } else if (normalized_rx_power<(target_rx_power-4)) {
-		tpc = 2; //+1
-		tpc_accumulated++;
-	      } else {
-		tpc = 1; //0
-	      }
-	      	      
-	      LOG_D(MAC,"[eNB %d] DLSCH scheduler: frame %d, subframe %d, harq_pid %d, tpc %d, accumulated %d, normalized/target rx power %d/%d\n",
-		    module_idP,frameP, subframeP,harq_pid,tpc,
-		    tpc_accumulated,normalized_rx_power,target_rx_power);
-
-	    } // Po_PUCCH has been updated 
-	    else {
-	      tpc = 1; //0
-	    } // time to do TPC update 
-	  else {
-	    tpc = 1; //0
-	  }
-
-	  dl_config_pdu                                                         = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
-	  memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-	  dl_config_pdu->pdu_type                                               = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE; 
-	  dl_config_pdu->pdu_size                                               = (uint8_t)(2+sizeof(nfapi_dl_config_dci_dl_pdu));
-	  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format                  = NFAPI_DL_DCI_FORMAT_1;
-	  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level           = get_aggregation(get_bw_index(module_idP,CC_id),ue_sched_ctl->dl_cqi[CC_id],format1);
-	  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti                        = rnti;
-	  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type                   = 1;    // CRNTI : see Table 4-10 from SCF082 - nFAPI specifications
-	  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power          = 6000; // equal to RS power
-	  
-	  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.harq_process                = harq_pid;
-	  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tpc                         = tpc; // dont adjust power when retransmitting
-	  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.new_data_indicator_1        = 1-UE_list->UE_template[CC_id][UE_id].oldNDI[harq_pid];
-	  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.mcs_1                       = mcs;
-	  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.redundancy_version_1        = 0;
-	  //deactivate second codeword
-	  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.mcs_2                       = 0;
-	  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.redundancy_version_2        = 1;
-	  if (cc[CC_id].tdd_Config != NULL) { //TDD
-	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.downlink_assignment_index = (UE_list->UE_template[CC_id][UE_id].DAI-1)&3;
-	    LOG_D(MAC,"[eNB %d] Initial transmission CC_id %d : harq_pid %d, dai %d, mcs %d\n",
-		  module_idP,CC_id,harq_pid,
-		  (UE_list->UE_template[CC_id][UE_id].DAI-1),
-		  mcs);
-	  } else {
-	    LOG_D(MAC,"[eNB %d] Initial transmission CC_id %d : harq_pid %d, mcs %d\n",
-		  module_idP,CC_id,harq_pid,mcs);
-	    
-	  }
-	  LOG_D(MAC,"Checking feasibility pdu %d (new sdu)\n",dl_req->number_pdu);
-	  if (!CCE_allocation_infeasible(module_idP,CC_id,1,subframeP,dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,rnti)) {
-
-
-	    ue_sched_ctl->round[CC_id][harq_pid] = 0;
-	    dl_req->number_dci++;
-	    dl_req->number_pdu++;
-	    
-	    // Toggle NDI for next time
-	    LOG_D(MAC,"CC_id %d Frame %d, subframeP %d: Toggling Format1 NDI for UE %d (rnti %x/%d) oldNDI %d\n",
-		  CC_id, frameP,subframeP,UE_id,
-		  rnti,harq_pid,UE_list->UE_template[CC_id][UE_id].oldNDI[harq_pid]);
-	    
-	    UE_list->UE_template[CC_id][UE_id].oldNDI[harq_pid]=1-UE_list->UE_template[CC_id][UE_id].oldNDI[harq_pid];
-	    UE_list->UE_template[CC_id][UE_id].oldmcs1[harq_pid] = mcs;
-	    UE_list->UE_template[CC_id][UE_id].oldmcs2[harq_pid] = 0;
-	    AssertFatal(UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated!=NULL,"physicalConfigDedicated is NULL\n");
-	    AssertFatal(UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->pdsch_ConfigDedicated!=NULL,"physicalConfigDedicated->pdsch_ConfigDedicated is NULL\n");
-	    
-	    fill_nfapi_dlsch_config(eNB,dl_req,
-				    TBS,
-				    eNB->pdu_index[CC_id],
-				    rnti,
-				    0, // type 0 allocation from 7.1.6 in 36.213
-				    0, // virtual_resource_block_assignment_flag, unused here
-				    0, // resource_block_coding, to be filled in later
-				    getQm(mcs),
-				    0, // redundancy version
-				    1, // transport blocks
-				    0, // transport block to codeword swap flag
-				    cc[CC_id].p_eNB == 1 ? 0 : 1, // transmission_scheme
-				    1, // number of layers
-				    1, // number of subbands
-				    //			     uint8_t codebook_index,
-				    4, // UE category capacity
-				    UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->pdsch_ConfigDedicated->p_a, 
-				    0, // delta_power_offset for TM5
-				    0, // ngap
-				    0, // nprb
-				    cc[CC_id].p_eNB == 1 ? 1 : 2, // transmission mode
-				    0, //number of PRBs treated as one subband, not used here
-				    0 // number of beamforming vectors, not used here
-				    );  
-	    eNB->TX_req[CC_id].sfn_sf = fill_nfapi_tx_req(&eNB->TX_req[CC_id].tx_request_body,
-							  (frameP*10)+subframeP,
-							  TBS,
-							  eNB->pdu_index[CC_id],
-							  eNB->UE_list.DLSCH_pdu[CC_id][0][(unsigned char)UE_id].payload[0]);
-	    
-	    LOG_D(MAC,"Filled NFAPI configuration for DCI/DLSCH/TXREQ %d, new SDU\n",eNB->pdu_index[CC_id]);
-
-	    eNB->pdu_index[CC_id]++;
-	    program_dlsch_acknak(module_idP,CC_id,UE_id,frameP,subframeP,dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.cce_idx);
-
-	  }
-	  else {
-	    LOG_W(MAC,"Frame %d, Subframe %d: Dropping DLSCH allocation for UE %d/%x, infeasible CCE allocations\n",
-		  frameP,subframeP,UE_id,rnti);
-	  }
-        } else {  // There is no data from RLC or MAC header, so don't schedule
-
-        }
-      }
-
-      if (cc[CC_id].tdd_Config != NULL){ // TDD
-        set_ul_DAI(module_idP,UE_id,CC_id,frameP,subframeP);
-      }
-
-    } // UE_id loop
-  }  // CC_id loop
-
-
-     
-  fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_flag);
-
-  stop_meas(&eNB->schedule_dlsch);
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_SCHEDULE_DLSCH,VCD_FUNCTION_OUT);
+		    // cycle through SDUs and place in dlsch_buffer
+		    memcpy(&UE_list->
+			   DLSCH_pdu[CC_id][0][UE_id].payload[0][offset],
+			   dlsch_buffer, sdu_length_total);
+		    // memcpy(RC.mac[0].DLSCH_pdu[0][0].payload[0][offset],dcch_buffer,sdu_lengths[0]);
+
+		    // fill remainder of DLSCH with random data
+		    for (j = 0; j < (TBS - sdu_length_total - offset); j++) {
+			UE_list->DLSCH_pdu[CC_id][0][UE_id].
+			    payload[0][offset + sdu_length_total + j] =
+			    (char) (taus() & 0xff);
+		    }
+
+
+		    if (opt_enabled == 1) {
+			trace_pdu(1, (uint8_t *)
+				  UE_list->DLSCH_pdu[CC_id][0][UE_id].
+				  payload[0], TBS, module_idP, 3,
+				  UE_RNTI(module_idP, UE_id), eNB->frame,
+				  eNB->subframe, 0, 0);
+			LOG_D(OPT,
+			      "[eNB %d][DLSCH] CC_id %d Frame %d  rnti %x  with size %d\n",
+			      module_idP, CC_id, frameP,
+			      UE_RNTI(module_idP, UE_id), TBS);
+		    }
+
+		    T(T_ENB_MAC_UE_DL_PDU_WITH_DATA, T_INT(module_idP),
+		      T_INT(CC_id), T_INT(rnti), T_INT(frameP),
+		      T_INT(subframeP), T_INT(harq_pid),
+		      T_BUFFER(UE_list->DLSCH_pdu[CC_id][0][UE_id].
+			       payload[0], TBS));
+
+		    UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid] =
+			nb_rb;
+
+		    add_ue_dlsch_info(module_idP,
+				      CC_id, UE_id, subframeP,
+				      S_DL_SCHEDULED);
+		    // store stats
+		    eNB->eNB_stats[CC_id].dlsch_bytes_tx +=
+			sdu_length_total;
+		    eNB->eNB_stats[CC_id].dlsch_pdus_tx += 1;
+
+		    UE_list->eNB_UE_stats[CC_id][UE_id].rbs_used = nb_rb;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].total_rbs_used +=
+			nb_rb;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].dlsch_mcs1 =
+			eNB_UE_stats->dlsch_mcs1;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].dlsch_mcs2 = mcs;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].TBS = TBS;
+
+		    UE_list->eNB_UE_stats[CC_id][UE_id].overhead_bytes =
+			TBS - sdu_length_total;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].total_sdu_bytes +=
+			sdu_length_total;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].total_pdu_bytes +=
+			TBS;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].total_num_pdus +=
+			1;
+
+		    if (cc[CC_id].tdd_Config != NULL) {	// TDD
+			UE_list->UE_template[CC_id][UE_id].DAI++;
+			update_ul_dci(module_idP, CC_id, rnti,
+				      UE_list->UE_template[CC_id][UE_id].
+				      DAI);
+		    }
+		    // do PUCCH power control
+		    // this is the normalized RX power
+		    eNB_UE_stats = &UE_list->eNB_UE_stats[CC_id][UE_id];
+
+		    /* TODO: fix how we deal with power, unit is not dBm, it's special from nfapi */
+		    normalized_rx_power = ue_sched_ctl->pucch1_snr[CC_id];
+		    target_rx_power = 208;
+
+		    // this assumes accumulated tpc
+		    // make sure that we are only sending a tpc update once a frame, otherwise the control loop will freak out
+		    int32_t framex10psubframe =
+			UE_list->UE_template[CC_id][UE_id].
+			pucch_tpc_tx_frame * 10 +
+			UE_list->UE_template[CC_id][UE_id].
+			pucch_tpc_tx_subframe;
+		    if (((framex10psubframe + 10) <= (frameP * 10 + subframeP)) ||	//normal case
+			((framex10psubframe > (frameP * 10 + subframeP)) && (((10240 - framex10psubframe + frameP * 10 + subframeP) >= 10))))	//frame wrap-around
+			if (ue_sched_ctl->pucch1_cqi_update[CC_id] == 1) {
+			    ue_sched_ctl->pucch1_cqi_update[CC_id] = 0;
+
+			    UE_list->
+				UE_template[CC_id]
+				[UE_id].pucch_tpc_tx_frame = frameP;
+			    UE_list->
+				UE_template[CC_id]
+				[UE_id].pucch_tpc_tx_subframe = subframeP;
+
+			    if (normalized_rx_power >
+				(target_rx_power + 4)) {
+				tpc = 0;	//-1
+				tpc_accumulated--;
+			    } else if (normalized_rx_power <
+				       (target_rx_power - 4)) {
+				tpc = 2;	//+1
+				tpc_accumulated++;
+			    } else {
+				tpc = 1;	//0
+			    }
+
+			    LOG_D(MAC,
+				  "[eNB %d] DLSCH scheduler: frame %d, subframe %d, harq_pid %d, tpc %d, accumulated %d, normalized/target rx power %d/%d\n",
+				  module_idP, frameP, subframeP, harq_pid,
+				  tpc, tpc_accumulated,
+				  normalized_rx_power, target_rx_power);
+
+			}	// Po_PUCCH has been updated 
+			else {
+			    tpc = 1;	//0
+			}	// time to do TPC update 
+		    else {
+			tpc = 1;	//0
+		    }
+
+		    dl_config_pdu =
+			&dl_req->dl_config_pdu_list[dl_req->number_pdu];
+		    memset((void *) dl_config_pdu, 0,
+			   sizeof(nfapi_dl_config_request_pdu_t));
+		    dl_config_pdu->pdu_type =
+			NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
+		    dl_config_pdu->pdu_size =
+			(uint8_t) (2 + sizeof(nfapi_dl_config_dci_dl_pdu));
+		    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format =
+			NFAPI_DL_DCI_FORMAT_1;
+		    dl_config_pdu->dci_dl_pdu.
+			dci_dl_pdu_rel8.aggregation_level =
+			get_aggregation(get_bw_index(module_idP, CC_id),
+					ue_sched_ctl->dl_cqi[CC_id],
+					format1);
+		    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti = rnti;
+		    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type = 1;	// CRNTI : see Table 4-10 from SCF082 - nFAPI specifications
+		    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power = 6000;	// equal to RS power
+
+		    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+			harq_process = harq_pid;
+		    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tpc = tpc;	// dont adjust power when retransmitting
+		    dl_config_pdu->dci_dl_pdu.
+			dci_dl_pdu_rel8.new_data_indicator_1 =
+			1 -
+			UE_list->UE_template[CC_id][UE_id].
+			oldNDI[harq_pid];
+		    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.mcs_1 = mcs;
+		    dl_config_pdu->dci_dl_pdu.
+			dci_dl_pdu_rel8.redundancy_version_1 = 0;
+		    //deactivate second codeword
+		    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.mcs_2 = 0;
+		    dl_config_pdu->dci_dl_pdu.
+			dci_dl_pdu_rel8.redundancy_version_2 = 1;
+		    if (cc[CC_id].tdd_Config != NULL) {	//TDD
+			dl_config_pdu->dci_dl_pdu.
+			    dci_dl_pdu_rel8.downlink_assignment_index =
+			    (UE_list->UE_template[CC_id][UE_id].DAI -
+			     1) & 3;
+			LOG_D(MAC,
+			      "[eNB %d] Initial transmission CC_id %d : harq_pid %d, dai %d, mcs %d\n",
+			      module_idP, CC_id, harq_pid,
+			      (UE_list->UE_template[CC_id][UE_id].DAI - 1),
+			      mcs);
+		    } else {
+			LOG_D(MAC,
+			      "[eNB %d] Initial transmission CC_id %d : harq_pid %d, mcs %d\n",
+			      module_idP, CC_id, harq_pid, mcs);
+
+		    }
+		    LOG_D(MAC, "Checking feasibility pdu %d (new sdu)\n",
+			  dl_req->number_pdu);
+		    if (!CCE_allocation_infeasible
+			(module_idP, CC_id, 1, subframeP,
+			 dl_config_pdu->dci_dl_pdu.
+			 dci_dl_pdu_rel8.aggregation_level, rnti)) {
+
+
+			ue_sched_ctl->round[CC_id][harq_pid] = 0;
+			dl_req->number_dci++;
+			dl_req->number_pdu++;
+
+			// Toggle NDI for next time
+			LOG_D(MAC,
+			      "CC_id %d Frame %d, subframeP %d: Toggling Format1 NDI for UE %d (rnti %x/%d) oldNDI %d\n",
+			      CC_id, frameP, subframeP, UE_id, rnti,
+			      harq_pid,
+			      UE_list->
+			      UE_template[CC_id][UE_id].oldNDI[harq_pid]);
+
+			UE_list->UE_template[CC_id][UE_id].
+			    oldNDI[harq_pid] =
+			    1 -
+			    UE_list->UE_template[CC_id][UE_id].
+			    oldNDI[harq_pid];
+			UE_list->UE_template[CC_id][UE_id].
+			    oldmcs1[harq_pid] = mcs;
+			UE_list->UE_template[CC_id][UE_id].
+			    oldmcs2[harq_pid] = 0;
+			AssertFatal(UE_list->UE_template[CC_id]
+				    [UE_id].physicalConfigDedicated !=
+				    NULL,
+				    "physicalConfigDedicated is NULL\n");
+			AssertFatal(UE_list->UE_template[CC_id]
+				    [UE_id].
+				    physicalConfigDedicated->pdsch_ConfigDedicated
+				    != NULL,
+				    "physicalConfigDedicated->pdsch_ConfigDedicated is NULL\n");
+
+			fill_nfapi_dlsch_config(eNB, dl_req, TBS, eNB->pdu_index[CC_id], rnti, 0,	// type 0 allocation from 7.1.6 in 36.213
+						0,	// virtual_resource_block_assignment_flag, unused here
+						0,	// resource_block_coding, to be filled in later
+						getQm(mcs), 0,	// redundancy version
+						1,	// transport blocks
+						0,	// transport block to codeword swap flag
+						cc[CC_id].p_eNB == 1 ? 0 : 1,	// transmission_scheme
+						1,	// number of layers
+						1,	// number of subbands
+						//                       uint8_t codebook_index,
+						4,	// UE category capacity
+						UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->pdsch_ConfigDedicated->p_a, 0,	// delta_power_offset for TM5
+						0,	// ngap
+						0,	// nprb
+						cc[CC_id].p_eNB == 1 ? 1 : 2,	// transmission mode
+						0,	//number of PRBs treated as one subband, not used here
+						0	// number of beamforming vectors, not used here
+			    );
+			eNB->TX_req[CC_id].sfn_sf =
+			    fill_nfapi_tx_req(&eNB->
+					      TX_req
+					      [CC_id].tx_request_body,
+					      (frameP * 10) + subframeP,
+					      TBS, eNB->pdu_index[CC_id],
+					      eNB->
+					      UE_list.DLSCH_pdu[CC_id][0][(unsigned char)
+									  UE_id].payload
+					      [0]);
+
+			LOG_D(MAC,
+			      "Filled NFAPI configuration for DCI/DLSCH/TXREQ %d, new SDU\n",
+			      eNB->pdu_index[CC_id]);
+
+			eNB->pdu_index[CC_id]++;
+			program_dlsch_acknak(module_idP, CC_id, UE_id,
+					     frameP, subframeP,
+					     dl_config_pdu->
+					     dci_dl_pdu.dci_dl_pdu_rel8.
+					     cce_idx);
+
+		    } else {
+			LOG_W(MAC,
+			      "Frame %d, Subframe %d: Dropping DLSCH allocation for UE %d/%x, infeasible CCE allocations\n",
+			      frameP, subframeP, UE_id, rnti);
+		    }
+		} else {	// There is no data from RLC or MAC header, so don't schedule
+
+		}
+	    }
+
+	    if (cc[CC_id].tdd_Config != NULL) {	// TDD
+		set_ul_DAI(module_idP, UE_id, CC_id, frameP, subframeP);
+	    }
+
+	}			// UE_id loop
+    }				// CC_id loop
+
+
+
+    fill_DLSCH_dci(module_idP, frameP, subframeP, mbsfn_flag);
+
+    stop_meas(&eNB->schedule_dlsch);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_SCHEDULE_DLSCH, VCD_FUNCTION_OUT);
 
 }
 
 //------------------------------------------------------------------------------
 void
-fill_DLSCH_dci(
-	       module_id_t module_idP,
-	       frame_t frameP,
-	       sub_frame_t subframeP,
-	       int* mbsfn_flagP
-	       )
+fill_DLSCH_dci(module_id_t module_idP,
+	       frame_t frameP, sub_frame_t subframeP, int *mbsfn_flagP)
 //------------------------------------------------------------------------------
 {
 
-  // loop over all allocated UEs and compute frequency allocations for PDSCH
-  int   UE_id = -1;
-  uint8_t            /* first_rb, */ nb_rb=3;
-  rnti_t        rnti;
-  //unsigned char *vrb_map;
-  uint8_t            rballoc_sub[25];
-  //uint8_t number_of_subbands=13;
-
-  //unsigned char round;
-  unsigned char     harq_pid;
-  int               i;
-  int               CC_id;
-  eNB_MAC_INST      *eNB  =RC.mac[module_idP];
-  UE_list_t         *UE_list = &eNB->UE_list;
-  int               N_RBG;
-  int               N_RB_DL;
-  COMMON_channels_t *cc;
-
-  start_meas(&eNB->fill_DLSCH_dci);
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_FILL_DLSCH_DCI,VCD_FUNCTION_IN);
-
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-    LOG_D(MAC,"Doing fill DCI for CC_id %d\n",CC_id);
-
-    if (mbsfn_flagP[CC_id]>0)
-      continue;
-
-    cc              = &eNB->common_channels[CC_id];
-    N_RBG           = to_rbg(cc->mib->message.dl_Bandwidth);
-    N_RB_DL         = to_prb(cc->mib->message.dl_Bandwidth);
-
-    // UE specific DCIs
-    for (UE_id=UE_list->head; UE_id>=0; UE_id=UE_list->next[UE_id]) {
-      LOG_T(MAC,"CC_id %d, UE_id: %d => status %d\n",CC_id,UE_id,eNB_dlsch_info[module_idP][CC_id][UE_id].status);
-
-      if (eNB_dlsch_info[module_idP][CC_id][UE_id].status == S_DL_SCHEDULED) {
-
-        // clear scheduling flag
-        eNB_dlsch_info[module_idP][CC_id][UE_id].status = S_DL_WAITING;
-        rnti = UE_RNTI(module_idP,UE_id);
-	if (cc->tdd_Config) harq_pid = ((frameP*10)+subframeP)%10;
-	else harq_pid = ((frameP*10)+subframeP)&7;
-        nb_rb = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
-
-
-	
-        /// Synchronizing rballoc with rballoc_sub
-        for(i=0; i<N_RBG; i++) {
-          rballoc_sub[i] = UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][i];
-        }
-
-	nfapi_dl_config_request_t      *DL_req         = &RC.mac[module_idP]->DL_req[0];
-	nfapi_dl_config_request_pdu_t* dl_config_pdu;
-
-	for (i=0;i<DL_req[CC_id].dl_config_request_body.number_pdu;i++) {
-	  dl_config_pdu                    = &DL_req[CC_id].dl_config_request_body.dl_config_pdu_list[i];
-	  if ((dl_config_pdu->pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)&&
-	      (dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti == rnti) &&
-          (dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format != 1)) {
-	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_block_coding    = allocate_prbs_sub(nb_rb,N_RB_DL,N_RBG,rballoc_sub);
-	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_allocation_type = 0;
-	  }
-	  else if ((dl_config_pdu->pdu_type == NFAPI_DL_CONFIG_DLSCH_PDU_TYPE)&&
-		       (dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti == rnti) &&
-               (dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type==0)) {
-	    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_block_coding    = allocate_prbs_sub(nb_rb,N_RB_DL,N_RBG,rballoc_sub);
-	  }
+    // loop over all allocated UEs and compute frequency allocations for PDSCH
+    int UE_id = -1;
+    uint8_t /* first_rb, */ nb_rb = 3;
+    rnti_t rnti;
+    //unsigned char *vrb_map;
+    uint8_t rballoc_sub[25];
+    //uint8_t number_of_subbands=13;
+
+    //unsigned char round;
+    unsigned char harq_pid;
+    int i;
+    int CC_id;
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    UE_list_t *UE_list = &eNB->UE_list;
+    int N_RBG;
+    int N_RB_DL;
+    COMMON_channels_t *cc;
+
+    start_meas(&eNB->fill_DLSCH_dci);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_FILL_DLSCH_DCI, VCD_FUNCTION_IN);
+
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+	LOG_D(MAC, "Doing fill DCI for CC_id %d\n", CC_id);
+
+	if (mbsfn_flagP[CC_id] > 0)
+	    continue;
+
+	cc = &eNB->common_channels[CC_id];
+	N_RBG = to_rbg(cc->mib->message.dl_Bandwidth);
+	N_RB_DL = to_prb(cc->mib->message.dl_Bandwidth);
+
+	// UE specific DCIs
+	for (UE_id = UE_list->head; UE_id >= 0;
+	     UE_id = UE_list->next[UE_id]) {
+	    LOG_T(MAC, "CC_id %d, UE_id: %d => status %d\n", CC_id, UE_id,
+		  eNB_dlsch_info[module_idP][CC_id][UE_id].status);
+
+	    if (eNB_dlsch_info[module_idP][CC_id][UE_id].status ==
+		S_DL_SCHEDULED) {
+
+		// clear scheduling flag
+		eNB_dlsch_info[module_idP][CC_id][UE_id].status =
+		    S_DL_WAITING;
+		rnti = UE_RNTI(module_idP, UE_id);
+		if (cc->tdd_Config)
+		    harq_pid = ((frameP * 10) + subframeP) % 10;
+		else
+		    harq_pid = ((frameP * 10) + subframeP) & 7;
+		nb_rb = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
+
+
+
+		/// Synchronizing rballoc with rballoc_sub
+		for (i = 0; i < N_RBG; i++) {
+		    rballoc_sub[i] =
+			UE_list->
+			UE_template[CC_id][UE_id].rballoc_subband[harq_pid]
+			[i];
+		}
+
+		nfapi_dl_config_request_t *DL_req =
+		    &RC.mac[module_idP]->DL_req[0];
+		nfapi_dl_config_request_pdu_t *dl_config_pdu;
+
+		for (i = 0;
+		     i < DL_req[CC_id].dl_config_request_body.number_pdu;
+		     i++) {
+		    dl_config_pdu =
+			&DL_req[CC_id].
+			dl_config_request_body.dl_config_pdu_list[i];
+		    if ((dl_config_pdu->pdu_type ==
+			 NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
+			&& (dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+			    rnti == rnti)
+			&& (dl_config_pdu->dci_dl_pdu.
+			    dci_dl_pdu_rel8.dci_format != 1)) {
+			dl_config_pdu->dci_dl_pdu.
+			    dci_dl_pdu_rel8.resource_block_coding =
+			    allocate_prbs_sub(nb_rb, N_RB_DL, N_RBG,
+					      rballoc_sub);
+			dl_config_pdu->dci_dl_pdu.
+			    dci_dl_pdu_rel8.resource_allocation_type = 0;
+		    } else
+			if ((dl_config_pdu->pdu_type ==
+			     NFAPI_DL_CONFIG_DLSCH_PDU_TYPE)
+			    && (dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.
+				rnti == rnti)
+			    && (dl_config_pdu->dlsch_pdu.
+				dlsch_pdu_rel8.resource_allocation_type ==
+				0)) {
+			dl_config_pdu->dlsch_pdu.
+			    dlsch_pdu_rel8.resource_block_coding =
+			    allocate_prbs_sub(nb_rb, N_RB_DL, N_RBG,
+					      rballoc_sub);
+		    }
+		}
+	    }
 	}
-      }
-    }
 
-  }
+    }
 
-  stop_meas(&eNB->fill_DLSCH_dci);
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_FILL_DLSCH_DCI,VCD_FUNCTION_OUT);
+    stop_meas(&eNB->fill_DLSCH_dci);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_FILL_DLSCH_DCI, VCD_FUNCTION_OUT);
 }
 
 //------------------------------------------------------------------------------
-unsigned char*
-get_dlsch_sdu(
-  module_id_t module_idP,
-  int CC_id,
-  frame_t frameP,
-  rnti_t rntiP,
-  uint8_t TBindex
-)
+unsigned char *get_dlsch_sdu(module_id_t module_idP,
+			     int CC_id, frame_t frameP, rnti_t rntiP,
+			     uint8_t TBindex)
 //------------------------------------------------------------------------------
 {
 
-  int UE_id;
-  eNB_MAC_INST *eNB=RC.mac[module_idP];
+    int UE_id;
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
 
-  if (rntiP==SI_RNTI) {
-    LOG_D(MAC,"[eNB %d] CC_id %d Frame %d Get DLSCH sdu for BCCH \n", module_idP, CC_id, frameP);
+    if (rntiP == SI_RNTI) {
+	LOG_D(MAC, "[eNB %d] CC_id %d Frame %d Get DLSCH sdu for BCCH \n",
+	      module_idP, CC_id, frameP);
 
-    return((unsigned char *)&eNB->common_channels[CC_id].BCCH_pdu.payload[0]);
-  }
+	return ((unsigned char *) &eNB->common_channels[CC_id].
+		BCCH_pdu.payload[0]);
+    }
 
-  UE_id = find_UE_id(module_idP,rntiP);
+    UE_id = find_UE_id(module_idP, rntiP);
 
-  if (UE_id != -1) {
-    LOG_D(MAC,"[eNB %d] Frame %d:  CC_id %d Get DLSCH sdu for rnti %x => UE_id %d\n",module_idP,frameP,CC_id,rntiP,UE_id);
-    return((unsigned char *)&eNB->UE_list.DLSCH_pdu[CC_id][TBindex][UE_id].payload[0]);
-  } else {
-    LOG_E(MAC,"[eNB %d] Frame %d: CC_id %d UE with RNTI %x does not exist\n", module_idP,frameP,CC_id,rntiP);
-    return NULL;
-  }
+    if (UE_id != -1) {
+	LOG_D(MAC,
+	      "[eNB %d] Frame %d:  CC_id %d Get DLSCH sdu for rnti %x => UE_id %d\n",
+	      module_idP, frameP, CC_id, rntiP, UE_id);
+	return ((unsigned char *) &eNB->
+		UE_list.DLSCH_pdu[CC_id][TBindex][UE_id].payload[0]);
+    } else {
+	LOG_E(MAC,
+	      "[eNB %d] Frame %d: CC_id %d UE with RNTI %x does not exist\n",
+	      module_idP, frameP, CC_id, rntiP);
+	return NULL;
+    }
 
 }
 
 
 //------------------------------------------------------------------------------
-void update_ul_dci(module_id_t module_idP,
-		   uint8_t CC_idP,
-		   rnti_t rntiP,
-		   uint8_t daiP)
+void
+update_ul_dci(module_id_t module_idP,
+	      uint8_t CC_idP, rnti_t rntiP, uint8_t daiP)
 //------------------------------------------------------------------------------
 {
 
-  nfapi_hi_dci0_request_t *HI_DCI0_req    = &RC.mac[module_idP]->HI_DCI0_req[CC_idP];
-  nfapi_hi_dci0_request_pdu_t  *hi_dci0_pdu    = &HI_DCI0_req->hi_dci0_request_body.hi_dci0_pdu_list[0];
-  COMMON_channels_t    *cc                     = &RC.mac[module_idP]->common_channels[CC_idP];
-  int i;
+    nfapi_hi_dci0_request_t *HI_DCI0_req =
+	&RC.mac[module_idP]->HI_DCI0_req[CC_idP];
+    nfapi_hi_dci0_request_pdu_t *hi_dci0_pdu =
+	&HI_DCI0_req->hi_dci0_request_body.hi_dci0_pdu_list[0];
+    COMMON_channels_t *cc = &RC.mac[module_idP]->common_channels[CC_idP];
+    int i;
+
 
+    if (cc->tdd_Config != NULL) {	// TDD 
+	for (i = 0;
+	     i <
+	     HI_DCI0_req->hi_dci0_request_body.number_of_dci +
+	     HI_DCI0_req->hi_dci0_request_body.number_of_dci; i++) {
 
-  if (cc->tdd_Config != NULL) { // TDD 
-    for (i=0; i<HI_DCI0_req->hi_dci0_request_body.number_of_dci + HI_DCI0_req->hi_dci0_request_body.number_of_dci; i++) {
+	    if ((hi_dci0_pdu[i].pdu_type == NFAPI_HI_DCI0_DCI_PDU_TYPE) &&
+		(hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.rnti == rntiP))
+		hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.dl_assignment_index =
+		    (daiP - 1) & 3;
 
-      if ((hi_dci0_pdu[i].pdu_type == NFAPI_HI_DCI0_DCI_PDU_TYPE)  && 
-	  (hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.rnti == rntiP))
-        hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.dl_assignment_index = (daiP-1)&3;
-      
+	}
     }
-  }
 }
 
 
 //------------------------------------------------------------------------------
-void set_ue_dai(
-  sub_frame_t   subframeP,
-  int           UE_id,
-  uint8_t       CC_id,
-  uint8_t       tdd_config,
-  UE_list_t*    UE_list
-)
+void
+set_ue_dai(sub_frame_t subframeP,
+	   int UE_id, uint8_t CC_id, uint8_t tdd_config,
+	   UE_list_t * UE_list)
 //------------------------------------------------------------------------------
 {
-  switch (tdd_config) {
-  case 0:
-    if ((subframeP==0)||(subframeP==1)||(subframeP==3)||(subframeP==5)||(subframeP==6)||(subframeP==8)) {
-      UE_list->UE_template[CC_id][UE_id].DAI = 0;
-    }
+    switch (tdd_config) {
+    case 0:
+	if ((subframeP == 0) || (subframeP == 1) || (subframeP == 3)
+	    || (subframeP == 5) || (subframeP == 6) || (subframeP == 8)) {
+	    UE_list->UE_template[CC_id][UE_id].DAI = 0;
+	}
 
-    break;
+	break;
 
-  case 1:
-    if ((subframeP==0)||(subframeP==4)||(subframeP==5)||(subframeP==9)) {
-      UE_list->UE_template[CC_id][UE_id].DAI = 0;
-    }
+    case 1:
+	if ((subframeP == 0) || (subframeP == 4) || (subframeP == 5)
+	    || (subframeP == 9)) {
+	    UE_list->UE_template[CC_id][UE_id].DAI = 0;
+	}
 
-    break;
+	break;
 
-  case 2:
-    if ((subframeP==4)||(subframeP==5)) {
-      UE_list->UE_template[CC_id][UE_id].DAI = 0;
-    }
+    case 2:
+	if ((subframeP == 4) || (subframeP == 5)) {
+	    UE_list->UE_template[CC_id][UE_id].DAI = 0;
+	}
 
-    break;
+	break;
 
-  case 3:
-    if ((subframeP==5)||(subframeP==7)||(subframeP==9)) {
-      UE_list->UE_template[CC_id][UE_id].DAI = 0;
-    }
+    case 3:
+	if ((subframeP == 5) || (subframeP == 7) || (subframeP == 9)) {
+	    UE_list->UE_template[CC_id][UE_id].DAI = 0;
+	}
 
-    break;
+	break;
 
-  case 4:
-    if ((subframeP==0)||(subframeP==6)) {
-      UE_list->UE_template[CC_id][UE_id].DAI = 0;
-    }
+    case 4:
+	if ((subframeP == 0) || (subframeP == 6)) {
+	    UE_list->UE_template[CC_id][UE_id].DAI = 0;
+	}
 
-    break;
+	break;
 
-  case 5:
-    if (subframeP==9) {
-      UE_list->UE_template[CC_id][UE_id].DAI = 0;
-    }
+    case 5:
+	if (subframeP == 9) {
+	    UE_list->UE_template[CC_id][UE_id].DAI = 0;
+	}
 
-    break;
+	break;
 
-  case 6:
-    if ((subframeP==0)||(subframeP==1)||(subframeP==5)||(subframeP==6)||(subframeP==9)) {
-      UE_list->UE_template[CC_id][UE_id].DAI = 0;
-    }
+    case 6:
+	if ((subframeP == 0) || (subframeP == 1) || (subframeP == 5)
+	    || (subframeP == 6) || (subframeP == 9)) {
+	    UE_list->UE_template[CC_id][UE_id].DAI = 0;
+	}
 
-    break;
+	break;
 
-  default:
-    UE_list->UE_template[CC_id][UE_id].DAI = 0;
-    LOG_N(MAC,"unknow TDD config %d\n",tdd_config);
-    break;
-  }
+    default:
+	UE_list->UE_template[CC_id][UE_id].DAI = 0;
+	LOG_N(MAC, "unknow TDD config %d\n", tdd_config);
+	break;
+    }
 }
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_mch.c b/openair2/LAYER2/MAC/eNB_scheduler_mch.c
index 3b05850a1d58d851c609d9b22985208265360ff3..7ce5a5b12c3e4f737de710c27dada2c647ab5ced 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler_mch.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_mch.c
@@ -52,10 +52,10 @@
 #include "pdcp.h"
 
 #if defined(ENABLE_ITTI)
-# include "intertask_interface.h"
+#include "intertask_interface.h"
 #endif
 
-#include "SIMULATION/TOOLS/defs.h" // for taus
+#include "SIMULATION/TOOLS/defs.h"	// for taus
 
 #define ENABLE_MAC_PAYLOAD_DEBUG
 #define DEBUG_eNB_SCHEDULER 1
@@ -63,607 +63,746 @@
 extern RAN_CONTEXT_t RC;
 
 #if defined(Rel10) || defined(Rel14)
-int8_t get_mbsfn_sf_alloction (module_id_t module_idP, uint8_t CC_id, uint8_t mbsfn_sync_area)
+int8_t
+get_mbsfn_sf_alloction(module_id_t module_idP, uint8_t CC_id,
+		       uint8_t mbsfn_sync_area)
 {
-  // currently there is one-to-one mapping between sf allocation pattern and sync area
-  if (mbsfn_sync_area > MAX_MBSFN_AREA) {
-    LOG_W(MAC,"[eNB %d] CC_id %d MBSFN synchronization area %d out of range\n ", module_idP, CC_id, mbsfn_sync_area);
-    return -1;
-  } else if (RC.mac[module_idP]->common_channels[CC_id].mbsfn_SubframeConfig[mbsfn_sync_area] != NULL) {
-    return mbsfn_sync_area;
-  } else {
-    LOG_W(MAC,"[eNB %d] CC_id %d MBSFN Subframe Config pattern %d not found \n ", module_idP, CC_id, mbsfn_sync_area);
-    return -1;
-  }
+    // currently there is one-to-one mapping between sf allocation pattern and sync area
+    if (mbsfn_sync_area > MAX_MBSFN_AREA) {
+	LOG_W(MAC,
+	      "[eNB %d] CC_id %d MBSFN synchronization area %d out of range\n ",
+	      module_idP, CC_id, mbsfn_sync_area);
+	return -1;
+    } else if (RC.mac[module_idP]->
+	       common_channels[CC_id].mbsfn_SubframeConfig[mbsfn_sync_area]
+	       != NULL) {
+	return mbsfn_sync_area;
+    } else {
+	LOG_W(MAC,
+	      "[eNB %d] CC_id %d MBSFN Subframe Config pattern %d not found \n ",
+	      module_idP, CC_id, mbsfn_sync_area);
+	return -1;
+    }
 }
 
-int schedule_MBMS(module_id_t module_idP, uint8_t CC_id, frame_t frameP, sub_frame_t subframeP)
+int
+schedule_MBMS(module_id_t module_idP, uint8_t CC_id, frame_t frameP,
+	      sub_frame_t subframeP)
 {
 
-  int mcch_flag=0,mtch_flag=0, msi_flag=0;
-  int mbsfn_period =0;// 1<<(RC.mac[module_idP]->mbsfn_SubframeConfig[0]->radioframeAllocationPeriod);
-  int mcch_period = 0;//32<<(RC.mac[module_idP]->mbsfn_AreaInfo[0]->mcch_Config_r9.mcch_RepetitionPeriod_r9);
-  int mch_scheduling_period = 8<<(RC.mac[module_idP]->common_channels[CC_id].pmch_Config[0]->mch_SchedulingPeriod_r9);
-  unsigned char mcch_sdu_length;
-  unsigned char header_len_mcch=0,header_len_msi=0,header_len_mtch=0, header_len_mtch_temp=0, header_len_mcch_temp=0, header_len_msi_temp=0;
-  int ii=0, msi_pos=0;
-  int mcch_mcs = -1;
-  uint16_t TBS,j=-1,padding=0,post_padding=0;
-  mac_rlc_status_resp_t rlc_status;
-  int num_mtch;
-  int msi_length,i,k;
-  unsigned char sdu_lcids[11], num_sdus=0, offset=0;
-  uint16_t sdu_lengths[11], sdu_length_total=0;
-  unsigned char mch_buffer[MAX_DLSCH_PAYLOAD_BYTES]; // check the max value, this is for dlsch only
-
-  COMMON_channels_t *cc = &RC.mac[module_idP]->common_channels[CC_id];
-
-  cc->MCH_pdu.Pdu_size=0;
-
-  for (i=0;
-       i< cc->num_active_mbsfn_area;
-       i++ ) {
-    // assume, that there is always a mapping
-    if ((j=get_mbsfn_sf_alloction(module_idP,CC_id,i)) == -1) {
-      return 0;
-    }
-
-    mbsfn_period = 1<<(cc->mbsfn_SubframeConfig[j]->radioframeAllocationPeriod);
-    mcch_period = 32<<(cc->mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_RepetitionPeriod_r9);
-    msi_pos=0;
-    ii=0;
-    LOG_D(MAC,"[eNB %d] CC_id %d Frame %d subframeP %d : Checking MBSFN Sync Area %d/%d with SF allocation %d/%d for MCCH and MTCH (mbsfn period %d, mcch period %d)\n",
-          module_idP, CC_id, frameP, subframeP,i,cc->num_active_mbsfn_area,
-          j,cc->num_sf_allocation_pattern,mbsfn_period,mcch_period);
-
-
-    switch (cc->mbsfn_AreaInfo[i]->mcch_Config_r9.signallingMCS_r9) {
-    case 0:
-      mcch_mcs = 2;
-      break;
-
-    case 1:
-      mcch_mcs = 7;
-      break;
-
-    case 2:
-      mcch_mcs = 13;
-      break;
-
-    case 3:
-      mcch_mcs = 19;
-      break;
-    }
-
-    // 1st: Check the MBSFN subframes from SIB2 info (SF allocation pattern i, max 8 non-overlapping patterns exist)
-    if (frameP %  mbsfn_period == cc->mbsfn_SubframeConfig[j]->radioframeAllocationOffset) { // MBSFN frameP
-      if (cc->mbsfn_SubframeConfig[j]->subframeAllocation.present == MBSFN_SubframeConfig__subframeAllocation_PR_oneFrame) { // one-frameP format
-
-        //  Find the first subframeP in this MCH to transmit MSI
-        if (frameP % mch_scheduling_period == cc->mbsfn_SubframeConfig[j]->radioframeAllocationOffset ) {
-          while (ii == 0) {
-            ii = cc->mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & (0x80>>msi_pos);
-            msi_pos++;
-          }
-
-          LOG_D(MAC,"[eNB %d] CC_id %d Frame %d subframeP %d : sync area %d sf allocation pattern %d sf alloc %x msi pos is %d \n",
-                module_idP, CC_id, frameP, subframeP,i,j,
-                cc->mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0], msi_pos);
-        }
-
-        // Check if the subframeP is for MSI, MCCH or MTCHs and Set the correspoding flag to 1
-        switch (subframeP) {
-        case 1:
-          if (cc->tdd_Config == NULL) {
-            if ((cc->mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF1) == MBSFN_FDD_SF1) {
-              if (msi_pos == 1) {
-                msi_flag = 1;
-              }
-
-              if ( (frameP % mcch_period == cc->mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((cc->mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF1) == MBSFN_FDD_SF1) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-
-        case 2:
-          if (cc->tdd_Config == NULL) {
-            if ((cc->mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF2) == MBSFN_FDD_SF2) {
-              if (msi_pos == 2) {
-                msi_flag = 1;
-              }
-
-              if ( (frameP % mcch_period == cc->mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((cc->mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF2) == MBSFN_FDD_SF2) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-
-        case 3:
-          if (cc->tdd_Config != NULL) { // TDD
-            if ((cc->mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF3) == MBSFN_TDD_SF3) {
-              if (msi_pos == 1) {
-                msi_flag = 1;
-              }
-
-              if ( (frameP % mcch_period == cc->mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((cc->mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF3) == MBSFN_TDD_SF3) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          } else { // FDD
-            if ((cc->mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF3) == MBSFN_FDD_SF3) {
-              if (msi_pos == 3) {
-                msi_flag = 1;
-              }
-
-              if ( (frameP % mcch_period == cc->mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((cc->mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF3) == MBSFN_FDD_SF3) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-
-        case 4:
-          if (cc->tdd_Config != NULL) {
-            if ((cc->mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF4) == MBSFN_TDD_SF4) {
-              if (msi_pos == 2) {
-                msi_flag = 1;
-              }
-
-              if ( (frameP % mcch_period == cc->mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((cc->mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF4) == MBSFN_TDD_SF4) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-
-        case 6:
-          if (cc->tdd_Config == NULL) {
-            if ((cc->mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF6) == MBSFN_FDD_SF6) {
-              if (msi_pos == 4) {
-                msi_flag = 1;
-              }
-
-              if ( (frameP % mcch_period == cc->mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((cc->mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF6) == MBSFN_FDD_SF6) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-
-        case 7:
-          if (cc->tdd_Config != NULL) { // TDD
-            if ((cc->mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF7) == MBSFN_TDD_SF7) {
-              if (msi_pos == 3) {
-                msi_flag = 1;
-              }
-
-              if ( (frameP % mcch_period == cc->mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((cc->mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF7) == MBSFN_TDD_SF7) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          } else { // FDD
-            if ((cc->mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF7) == MBSFN_FDD_SF7) {
-              if (msi_pos == 5) {
-                msi_flag = 1;
-              }
-
-              if ( (frameP % mcch_period == cc->mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((cc->mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF7) == MBSFN_FDD_SF7) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-
-        case 8:
-          if (cc->tdd_Config != NULL) { //TDD
-            if ((cc->mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF8) == MBSFN_TDD_SF8) {
-              if (msi_pos == 4) {
-                msi_flag = 1;
-              }
-
-              if ( (frameP % mcch_period == cc->mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((cc->mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF8) == MBSFN_TDD_SF8) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          } else { // FDD
-            if ((cc->mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF8) == MBSFN_FDD_SF8) {
-              if (msi_pos == 6) {
-                msi_flag = 1;
-              }
-
-              if ( (frameP % mcch_period == cc->mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((cc->mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF8) == MBSFN_FDD_SF8) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-
-        case 9:
-          if (cc->tdd_Config != NULL) {
-            if ((cc->mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF9) == MBSFN_TDD_SF9) {
-              if (msi_pos == 5) {
-                msi_flag = 1;
-              }
-
-              if ( (frameP % mcch_period == cc->mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((cc->mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF9) == MBSFN_TDD_SF9) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-        }// end switch
-
-        // sf allocation is non-overlapping
-        if ((msi_flag==1) || (mcch_flag==1) || (mtch_flag==1)) {
-          LOG_D(MAC,"[eNB %d] CC_id %d Frame %d Subframe %d: sync area %d SF alloc %d: msi flag %d, mcch flag %d, mtch flag %d\n",
-                module_idP, CC_id, frameP, subframeP,i,j,msi_flag,mcch_flag,mtch_flag);
-          break;
-        }
-      } else { // four-frameP format
-      }
+    int mcch_flag = 0, mtch_flag = 0, msi_flag = 0;
+    int mbsfn_period = 0;	// 1<<(RC.mac[module_idP]->mbsfn_SubframeConfig[0]->radioframeAllocationPeriod);
+    int mcch_period = 0;	//32<<(RC.mac[module_idP]->mbsfn_AreaInfo[0]->mcch_Config_r9.mcch_RepetitionPeriod_r9);
+    int mch_scheduling_period =
+	8 << (RC.mac[module_idP]->common_channels[CC_id].
+	      pmch_Config[0]->mch_SchedulingPeriod_r9);
+    unsigned char mcch_sdu_length;
+    unsigned char header_len_mcch = 0, header_len_msi =
+	0, header_len_mtch = 0, header_len_mtch_temp =
+	0, header_len_mcch_temp = 0, header_len_msi_temp = 0;
+    int ii = 0, msi_pos = 0;
+    int mcch_mcs = -1;
+    uint16_t TBS, j = -1, padding = 0, post_padding = 0;
+    mac_rlc_status_resp_t rlc_status;
+    int num_mtch;
+    int msi_length, i, k;
+    unsigned char sdu_lcids[11], num_sdus = 0, offset = 0;
+    uint16_t sdu_lengths[11], sdu_length_total = 0;
+    unsigned char mch_buffer[MAX_DLSCH_PAYLOAD_BYTES];	// check the max value, this is for dlsch only
+
+    COMMON_channels_t *cc = &RC.mac[module_idP]->common_channels[CC_id];
+
+    cc->MCH_pdu.Pdu_size = 0;
+
+    for (i = 0; i < cc->num_active_mbsfn_area; i++) {
+	// assume, that there is always a mapping
+	if ((j = get_mbsfn_sf_alloction(module_idP, CC_id, i)) == -1) {
+	    return 0;
+	}
+
+	mbsfn_period =
+	    1 << (cc->mbsfn_SubframeConfig[j]->radioframeAllocationPeriod);
+	mcch_period =
+	    32 << (cc->mbsfn_AreaInfo[i]->
+		   mcch_Config_r9.mcch_RepetitionPeriod_r9);
+	msi_pos = 0;
+	ii = 0;
+	LOG_D(MAC,
+	      "[eNB %d] CC_id %d Frame %d subframeP %d : Checking MBSFN Sync Area %d/%d with SF allocation %d/%d for MCCH and MTCH (mbsfn period %d, mcch period %d)\n",
+	      module_idP, CC_id, frameP, subframeP, i,
+	      cc->num_active_mbsfn_area, j, cc->num_sf_allocation_pattern,
+	      mbsfn_period, mcch_period);
+
+
+	switch (cc->mbsfn_AreaInfo[i]->mcch_Config_r9.signallingMCS_r9) {
+	case 0:
+	    mcch_mcs = 2;
+	    break;
+
+	case 1:
+	    mcch_mcs = 7;
+	    break;
+
+	case 2:
+	    mcch_mcs = 13;
+	    break;
+
+	case 3:
+	    mcch_mcs = 19;
+	    break;
+	}
+
+	// 1st: Check the MBSFN subframes from SIB2 info (SF allocation pattern i, max 8 non-overlapping patterns exist)
+	if (frameP % mbsfn_period == cc->mbsfn_SubframeConfig[j]->radioframeAllocationOffset) {	// MBSFN frameP
+	    if (cc->mbsfn_SubframeConfig[j]->subframeAllocation.present == MBSFN_SubframeConfig__subframeAllocation_PR_oneFrame) {	// one-frameP format
+
+		//  Find the first subframeP in this MCH to transmit MSI
+		if (frameP % mch_scheduling_period ==
+		    cc->mbsfn_SubframeConfig[j]->
+		    radioframeAllocationOffset) {
+		    while (ii == 0) {
+			ii = cc->
+			    mbsfn_SubframeConfig[j]->subframeAllocation.
+			    choice.oneFrame.buf[0] & (0x80 >> msi_pos);
+			msi_pos++;
+		    }
+
+		    LOG_D(MAC,
+			  "[eNB %d] CC_id %d Frame %d subframeP %d : sync area %d sf allocation pattern %d sf alloc %x msi pos is %d \n",
+			  module_idP, CC_id, frameP, subframeP, i, j,
+			  cc->mbsfn_SubframeConfig[j]->
+			  subframeAllocation.choice.oneFrame.buf[0],
+			  msi_pos);
+		}
+		// Check if the subframeP is for MSI, MCCH or MTCHs and Set the correspoding flag to 1
+		switch (subframeP) {
+		case 1:
+		    if (cc->tdd_Config == NULL) {
+			if ((cc->
+			     mbsfn_SubframeConfig[j]->subframeAllocation.
+			     choice.oneFrame.buf[0] & MBSFN_FDD_SF1) ==
+			    MBSFN_FDD_SF1) {
+			    if (msi_pos == 1) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 cc->mbsfn_AreaInfo[i]->
+				 mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((cc->mbsfn_AreaInfo[i]->
+				  mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_FDD_SF1) ==
+				 MBSFN_FDD_SF1)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+
+		case 2:
+		    if (cc->tdd_Config == NULL) {
+			if ((cc->
+			     mbsfn_SubframeConfig[j]->subframeAllocation.
+			     choice.oneFrame.buf[0] & MBSFN_FDD_SF2) ==
+			    MBSFN_FDD_SF2) {
+			    if (msi_pos == 2) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 cc->mbsfn_AreaInfo[i]->
+				 mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((cc->mbsfn_AreaInfo[i]->
+				  mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_FDD_SF2) ==
+				 MBSFN_FDD_SF2)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+
+		case 3:
+		    if (cc->tdd_Config != NULL) {	// TDD
+			if ((cc->
+			     mbsfn_SubframeConfig[j]->subframeAllocation.
+			     choice.oneFrame.buf[0] & MBSFN_TDD_SF3) ==
+			    MBSFN_TDD_SF3) {
+			    if (msi_pos == 1) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 cc->mbsfn_AreaInfo[i]->
+				 mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((cc->mbsfn_AreaInfo[i]->
+				  mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_TDD_SF3) ==
+				 MBSFN_TDD_SF3)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    } else {	// FDD
+			if ((cc->
+			     mbsfn_SubframeConfig[j]->subframeAllocation.
+			     choice.oneFrame.buf[0] & MBSFN_FDD_SF3) ==
+			    MBSFN_FDD_SF3) {
+			    if (msi_pos == 3) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 cc->mbsfn_AreaInfo[i]->
+				 mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((cc->mbsfn_AreaInfo[i]->
+				  mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_FDD_SF3) ==
+				 MBSFN_FDD_SF3)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+
+		case 4:
+		    if (cc->tdd_Config != NULL) {
+			if ((cc->
+			     mbsfn_SubframeConfig[j]->subframeAllocation.
+			     choice.oneFrame.buf[0] & MBSFN_TDD_SF4) ==
+			    MBSFN_TDD_SF4) {
+			    if (msi_pos == 2) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 cc->mbsfn_AreaInfo[i]->
+				 mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((cc->mbsfn_AreaInfo[i]->
+				  mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_TDD_SF4) ==
+				 MBSFN_TDD_SF4)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+
+		case 6:
+		    if (cc->tdd_Config == NULL) {
+			if ((cc->
+			     mbsfn_SubframeConfig[j]->subframeAllocation.
+			     choice.oneFrame.buf[0] & MBSFN_FDD_SF6) ==
+			    MBSFN_FDD_SF6) {
+			    if (msi_pos == 4) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 cc->mbsfn_AreaInfo[i]->
+				 mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((cc->mbsfn_AreaInfo[i]->
+				  mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_FDD_SF6) ==
+				 MBSFN_FDD_SF6)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+
+		case 7:
+		    if (cc->tdd_Config != NULL) {	// TDD
+			if ((cc->
+			     mbsfn_SubframeConfig[j]->subframeAllocation.
+			     choice.oneFrame.buf[0] & MBSFN_TDD_SF7) ==
+			    MBSFN_TDD_SF7) {
+			    if (msi_pos == 3) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 cc->mbsfn_AreaInfo[i]->
+				 mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((cc->mbsfn_AreaInfo[i]->
+				  mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_TDD_SF7) ==
+				 MBSFN_TDD_SF7)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    } else {	// FDD
+			if ((cc->
+			     mbsfn_SubframeConfig[j]->subframeAllocation.
+			     choice.oneFrame.buf[0] & MBSFN_FDD_SF7) ==
+			    MBSFN_FDD_SF7) {
+			    if (msi_pos == 5) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 cc->mbsfn_AreaInfo[i]->
+				 mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((cc->mbsfn_AreaInfo[i]->
+				  mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_FDD_SF7) ==
+				 MBSFN_FDD_SF7)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+
+		case 8:
+		    if (cc->tdd_Config != NULL) {	//TDD
+			if ((cc->
+			     mbsfn_SubframeConfig[j]->subframeAllocation.
+			     choice.oneFrame.buf[0] & MBSFN_TDD_SF8) ==
+			    MBSFN_TDD_SF8) {
+			    if (msi_pos == 4) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 cc->mbsfn_AreaInfo[i]->
+				 mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((cc->mbsfn_AreaInfo[i]->
+				  mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_TDD_SF8) ==
+				 MBSFN_TDD_SF8)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    } else {	// FDD
+			if ((cc->
+			     mbsfn_SubframeConfig[j]->subframeAllocation.
+			     choice.oneFrame.buf[0] & MBSFN_FDD_SF8) ==
+			    MBSFN_FDD_SF8) {
+			    if (msi_pos == 6) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 cc->mbsfn_AreaInfo[i]->
+				 mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((cc->mbsfn_AreaInfo[i]->
+				  mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_FDD_SF8) ==
+				 MBSFN_FDD_SF8)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+
+		case 9:
+		    if (cc->tdd_Config != NULL) {
+			if ((cc->
+			     mbsfn_SubframeConfig[j]->subframeAllocation.
+			     choice.oneFrame.buf[0] & MBSFN_TDD_SF9) ==
+			    MBSFN_TDD_SF9) {
+			    if (msi_pos == 5) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 cc->mbsfn_AreaInfo[i]->
+				 mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((cc->mbsfn_AreaInfo[i]->
+				  mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_TDD_SF9) ==
+				 MBSFN_TDD_SF9)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+		}		// end switch
+
+		// sf allocation is non-overlapping
+		if ((msi_flag == 1) || (mcch_flag == 1)
+		    || (mtch_flag == 1)) {
+		    LOG_D(MAC,
+			  "[eNB %d] CC_id %d Frame %d Subframe %d: sync area %d SF alloc %d: msi flag %d, mcch flag %d, mtch flag %d\n",
+			  module_idP, CC_id, frameP, subframeP, i, j,
+			  msi_flag, mcch_flag, mtch_flag);
+		    break;
+		}
+	    } else {		// four-frameP format
+	    }
+	}
+    }				// end of for loop
+
+    cc->msi_active = 0;
+    cc->mcch_active = 0;
+    cc->mtch_active = 0;
+
+    // Calculate the mcs
+    if ((msi_flag == 1) || (mcch_flag == 1)) {
+	cc->MCH_pdu.mcs = mcch_mcs;
+    } else if (mtch_flag == 1) {	// only MTCH in this subframeP
+	cc->MCH_pdu.mcs = cc->pmch_Config[0]->dataMCS_r9;
     }
-  } // end of for loop
-
-  cc->msi_active=0;
-  cc->mcch_active=0;
-  cc->mtch_active=0;
-
-  // Calculate the mcs
-  if ((msi_flag==1) || (mcch_flag==1)) {
-    cc->MCH_pdu.mcs = mcch_mcs;
-  } else if (mtch_flag == 1) { // only MTCH in this subframeP
-    cc->MCH_pdu.mcs = cc->pmch_Config[0]->dataMCS_r9;
-  }
-
-
-  // 2nd: Create MSI, get MCCH from RRC and MTCHs from RLC
-
-  // there is MSI (MCH Scheduling Info)
-  if (msi_flag == 1) {
-    // Create MSI here
-    uint16_t msi_control_element[29], *msi_ptr;
-
-    msi_ptr = &msi_control_element[0];
-    ((MSI_ELEMENT *) msi_ptr)->lcid = MCCH_LCHANID; //MCCH
-
-    if (mcch_flag==1) {
-      ((MSI_ELEMENT *) msi_ptr)->stop_sf_MSB = 0;
-      ((MSI_ELEMENT *) msi_ptr)->stop_sf_LSB = 0;
-    } else {                  // no mcch for this MSP
-      ((MSI_ELEMENT *) msi_ptr)->stop_sf_MSB = 0x7;// stop value is 2047
-      ((MSI_ELEMENT *) msi_ptr)->stop_sf_LSB = 0xff;
+    // 2nd: Create MSI, get MCCH from RRC and MTCHs from RLC
+
+    // there is MSI (MCH Scheduling Info)
+    if (msi_flag == 1) {
+	// Create MSI here
+	uint16_t msi_control_element[29], *msi_ptr;
+
+	msi_ptr = &msi_control_element[0];
+	((MSI_ELEMENT *) msi_ptr)->lcid = MCCH_LCHANID;	//MCCH
+
+	if (mcch_flag == 1) {
+	    ((MSI_ELEMENT *) msi_ptr)->stop_sf_MSB = 0;
+	    ((MSI_ELEMENT *) msi_ptr)->stop_sf_LSB = 0;
+	} else {		// no mcch for this MSP
+	    ((MSI_ELEMENT *) msi_ptr)->stop_sf_MSB = 0x7;	// stop value is 2047
+	    ((MSI_ELEMENT *) msi_ptr)->stop_sf_LSB = 0xff;
+	}
+
+	msi_ptr += sizeof(MSI_ELEMENT);
+
+	//Header for MTCHs
+	num_mtch = cc->mbms_SessionList[0]->list.count;
+
+	for (k = 0; k < num_mtch; k++) {	// loop for all session in this MCH (MCH[0]) at this moment
+	    ((MSI_ELEMENT *) msi_ptr)->lcid = cc->mbms_SessionList[0]->list.array[k]->logicalChannelIdentity_r9;	//mtch_lcid;
+	    ((MSI_ELEMENT *) msi_ptr)->stop_sf_MSB = 0;	// last subframeP of this mtch (only one mtch now)
+	    ((MSI_ELEMENT *) msi_ptr)->stop_sf_LSB = 0xB;
+	    msi_ptr += sizeof(MSI_ELEMENT);
+	}
+
+	msi_length = msi_ptr - msi_control_element;
+
+	if (msi_length < 128) {
+	    header_len_msi = 2;
+	} else {
+	    header_len_msi = 3;
+	}
+
+	LOG_D(MAC,
+	      "[eNB %d] CC_id %d Frame %d : MSI->MCH, length of MSI is %d bytes \n",
+	      module_idP, CC_id, frameP, msi_length);
+	//LOG_D(MAC,"Scheduler: MSI is transmitted in this subframeP \n" );
+
+	//   LOG_D(MAC,"Scheduler: MSI length is %d bytes\n",msi_length);
+	// Store MSI data to mch_buffer[0]
+	memcpy((char *) &mch_buffer[sdu_length_total],
+	       msi_control_element, msi_length);
+
+	sdu_lcids[num_sdus] = MCH_SCHDL_INFO;
+	sdu_lengths[num_sdus] = msi_length;
+	sdu_length_total += sdu_lengths[num_sdus];
+	LOG_I(MAC, "[eNB %d] CC_id %d Create %d bytes for MSI\n",
+	      module_idP, CC_id, sdu_lengths[num_sdus]);
+	num_sdus++;
+	cc->msi_active = 1;
     }
-
-    msi_ptr+= sizeof(MSI_ELEMENT);
-
-    //Header for MTCHs
-    num_mtch = cc->mbms_SessionList[0]->list.count;
-
-    for (k=0; k<num_mtch; k++) { // loop for all session in this MCH (MCH[0]) at this moment
-      ((MSI_ELEMENT *) msi_ptr)->lcid = cc->mbms_SessionList[0]->list.array[k]->logicalChannelIdentity_r9;//mtch_lcid;
-      ((MSI_ELEMENT *) msi_ptr)->stop_sf_MSB = 0; // last subframeP of this mtch (only one mtch now)
-      ((MSI_ELEMENT *) msi_ptr)->stop_sf_LSB = 0xB;
-      msi_ptr+=sizeof(MSI_ELEMENT);
+    // there is MCCH
+    if (mcch_flag == 1) {
+	LOG_D(MAC,
+	      "[eNB %d] CC_id %d Frame %d Subframe %d: Schedule MCCH MESSAGE (area %d, sfAlloc %d)\n",
+	      module_idP, CC_id, frameP, subframeP, i, j);
+
+	mcch_sdu_length = mac_rrc_data_req(module_idP, CC_id, frameP, MCCH, 1, &cc->MCCH_pdu.payload[0], 1,	// this is eNB
+					   module_idP,	// index
+					   i);	// this is the mbsfn sync area index
+
+	if (mcch_sdu_length > 0) {
+	    LOG_D(MAC,
+		  "[eNB %d] CC_id %d Frame %d subframeP %d : MCCH->MCH, Received %d bytes from RRC \n",
+		  module_idP, CC_id, frameP, subframeP, mcch_sdu_length);
+
+	    header_len_mcch = 2;
+
+	    if (cc->tdd_Config != NULL) {
+		LOG_D(MAC,
+		      "[eNB %d] CC_id %d Frame %d subframeP %d: Scheduling MCCH->MCH (TDD) for MCCH message %d bytes (mcs %d )\n",
+		      module_idP, CC_id, frameP, subframeP,
+		      mcch_sdu_length, mcch_mcs);
+	    } else {
+		LOG_I(MAC,
+		      "[eNB %d] CC_id %d Frame %d subframeP %d: Scheduling MCCH->MCH (FDD) for MCCH message %d bytes (mcs %d)\n",
+		      module_idP, CC_id, frameP, subframeP,
+		      mcch_sdu_length, mcch_mcs);
+	    }
+
+	    cc->mcch_active = 1;
+
+	    memcpy((char *) &mch_buffer[sdu_length_total],
+		   &cc->MCCH_pdu.payload[0], mcch_sdu_length);
+	    sdu_lcids[num_sdus] = MCCH_LCHANID;
+	    sdu_lengths[num_sdus] = mcch_sdu_length;
+
+	    if (sdu_lengths[num_sdus] > 128) {
+		header_len_mcch = 3;
+	    }
+
+	    sdu_length_total += sdu_lengths[num_sdus];
+	    LOG_D(MAC,
+		  "[eNB %d] CC_id %d Got %d bytes for MCCH from RRC \n",
+		  module_idP, CC_id, sdu_lengths[num_sdus]);
+	    num_sdus++;
+	}
     }
 
-    msi_length = msi_ptr-msi_control_element;
-
-    if (msi_length<128) {
-      header_len_msi = 2;
-    } else {
-      header_len_msi = 3;
-    }
-
-    LOG_D(MAC,"[eNB %d] CC_id %d Frame %d : MSI->MCH, length of MSI is %d bytes \n",
-          module_idP,CC_id,frameP,msi_length);
-    //LOG_D(MAC,"Scheduler: MSI is transmitted in this subframeP \n" );
-
-    //   LOG_D(MAC,"Scheduler: MSI length is %d bytes\n",msi_length);
-    // Store MSI data to mch_buffer[0]
-    memcpy((char *)&mch_buffer[sdu_length_total],
-           msi_control_element,
-           msi_length);
-
-    sdu_lcids[num_sdus] = MCH_SCHDL_INFO;
-    sdu_lengths[num_sdus] = msi_length;
-    sdu_length_total += sdu_lengths[num_sdus];
-    LOG_I(MAC,"[eNB %d] CC_id %d Create %d bytes for MSI\n", module_idP, CC_id, sdu_lengths[num_sdus]);
-    num_sdus++;
-    cc->msi_active=1;
-  }
-
-  // there is MCCH
-  if (mcch_flag == 1) {
-    LOG_D(MAC,"[eNB %d] CC_id %d Frame %d Subframe %d: Schedule MCCH MESSAGE (area %d, sfAlloc %d)\n",
-          module_idP, CC_id, frameP, subframeP, i, j);
-
-    mcch_sdu_length = mac_rrc_data_req(module_idP,
-                                       CC_id,
-                                       frameP,
-                                       MCCH,1,
-                                       &cc->MCCH_pdu.payload[0],
-                                       1,// this is eNB
-                                       module_idP, // index
-                                       i); // this is the mbsfn sync area index
-
-    if (mcch_sdu_length > 0) {
-      LOG_D(MAC,"[eNB %d] CC_id %d Frame %d subframeP %d : MCCH->MCH, Received %d bytes from RRC \n",
-            module_idP,CC_id,frameP,subframeP,mcch_sdu_length);
-
-      header_len_mcch = 2;
-
-      if (cc->tdd_Config != NULL) {
-        LOG_D(MAC,"[eNB %d] CC_id %d Frame %d subframeP %d: Scheduling MCCH->MCH (TDD) for MCCH message %d bytes (mcs %d )\n",
-              module_idP, CC_id,
-              frameP,subframeP,
-              mcch_sdu_length,
-              mcch_mcs);
-      } else {
-        LOG_I(MAC,"[eNB %d] CC_id %d Frame %d subframeP %d: Scheduling MCCH->MCH (FDD) for MCCH message %d bytes (mcs %d)\n",
-              module_idP, CC_id,
-              frameP, subframeP,
-              mcch_sdu_length,
-              mcch_mcs);
-      }
-
-      cc->mcch_active=1;
-
-      memcpy((char *)&mch_buffer[sdu_length_total],
-             &cc->MCCH_pdu.payload[0],
-             mcch_sdu_length);
-      sdu_lcids[num_sdus] = MCCH_LCHANID;
-      sdu_lengths[num_sdus] = mcch_sdu_length;
-
-      if (sdu_lengths[num_sdus]>128) {
-        header_len_mcch = 3;
-      }
-
-      sdu_length_total += sdu_lengths[num_sdus];
-      LOG_D(MAC,"[eNB %d] CC_id %d Got %d bytes for MCCH from RRC \n",module_idP,CC_id,sdu_lengths[num_sdus]);
-      num_sdus++;
-    }
-  }
-
-  TBS = get_TBS_DL(cc->MCH_pdu.mcs, to_prb(cc->mib->message.dl_Bandwidth));
+    TBS =
+	get_TBS_DL(cc->MCH_pdu.mcs, to_prb(cc->mib->message.dl_Bandwidth));
 #if defined(Rel10) || defined(Rel14)
-  // do not let mcch and mtch multiplexing when relaying is active
-  // for sync area 1, so not transmit data
-  //if ((i == 0) && ((RC.mac[module_idP]->MBMS_flag != multicast_relay) || (RC.mac[module_idP]->mcch_active==0))) {
+    // do not let mcch and mtch multiplexing when relaying is active
+    // for sync area 1, so not transmit data
+    //if ((i == 0) && ((RC.mac[module_idP]->MBMS_flag != multicast_relay) || (RC.mac[module_idP]->mcch_active==0))) {
 #endif
 
-  // there is MTCHs, loop if there are more than 1
-  if (mtch_flag == 1) {
-    // Calculate TBS
-    /* if ((msi_flag==1) || (mcch_flag==1)) {
-     TBS = mac_xface->get_TBS(mcch_mcs, mac_xface->frame_parms->N_RB_DL);
-     }
-     else { // only MTCH in this subframeP
-     TBS = mac_xface->get_TBS(RC.mac[module_idP]->pmch_Config[0]->dataMCS_r9, mac_xface->frame_parms->N_RB_DL);
-     }
-
-    // get MTCH data from RLC (like for DTCH)
-    LOG_D(MAC,"[eNB %d] CC_id %d Frame %d subframe %d: Schedule MTCH (area %d, sfAlloc %d)\n",Mod_id,CC_id,frame,subframe,i,j);
-
-    header_len_mtch = 3;
-    LOG_D(MAC,"[eNB %d], CC_id %d, Frame %d, MTCH->MCH, Checking RLC status (rab %d, tbs %d, len %d)\n",
-    Mod_id,CC_id,frame,MTCH,TBS,
-    TBS-header_len_mcch-header_len_msi-sdu_length_total-header_len_mtch);
-
-    rlc_status = mac_rlc_status_ind(Mod_id,frame,1,RLC_MBMS_YES,MTCH+ (maxDRB + 3) * MAX_MOBILES_PER_RG,
-          TBS-header_len_mcch-header_len_msi-sdu_length_total-header_len_mtch);
-    printf("frame %d, subframe %d,  rlc_status.bytes_in_buffer is %d\n",frame,subframe, rlc_status.bytes_in_buffer);
-
-     */
-
-    // get MTCH data from RLC (like for DTCH)
-    LOG_D(MAC,"[eNB %d] CC_id %d Frame %d subframeP %d: Schedule MTCH (area %d, sfAlloc %d)\n",module_idP,CC_id,frameP,subframeP,i,j);
-
-    header_len_mtch = 3;
-    LOG_D(MAC,"[eNB %d], CC_id %d, Frame %d, MTCH->MCH, Checking RLC status (rab %d, tbs %d, len %d)\n",
-          module_idP,CC_id,frameP,MTCH,TBS,
-          TBS-header_len_mcch-header_len_msi-sdu_length_total-header_len_mtch);
-
-    rlc_status = mac_rlc_status_ind(module_idP,0,frameP,subframeP,module_idP,ENB_FLAG_YES,MBMS_FLAG_YES,MTCH,
-                                    TBS-header_len_mcch-header_len_msi-sdu_length_total-header_len_mtch);
-    LOG_D(MAC,"e-MBMS log channel %u frameP %d, subframeP %d,  rlc_status.bytes_in_buffer is %d\n",
-          MTCH,frameP,subframeP, rlc_status.bytes_in_buffer);
-
-    if (rlc_status.bytes_in_buffer >0) {
-      LOG_I(MAC,"[eNB %d][MBMS USER-PLANE], CC_id %d, Frame %d, MTCH->MCH, Requesting %d bytes from RLC (header len mtch %d)\n",
-            module_idP,CC_id,frameP,TBS-header_len_mcch-header_len_msi-sdu_length_total-header_len_mtch,header_len_mtch);
-
-      sdu_lengths[num_sdus] = mac_rlc_data_req(
-                                module_idP,
-                                0,
-				module_idP,
-                                frameP,
-                                ENB_FLAG_YES,
-                                MBMS_FLAG_YES,
-                                MTCH,
-								0,	//not used
-                                (char*)&mch_buffer[sdu_length_total]);
-      //sdu_lengths[num_sdus] = mac_rlc_data_req(module_idP,frameP, MBMS_FLAG_NO,  MTCH+(MAX_NUM_RB*(NUMBER_OF_UE_MAX+1)), (char*)&mch_buffer[sdu_length_total]);
-      LOG_I(MAC,"[eNB %d][MBMS USER-PLANE] CC_id %d Got %d bytes for MTCH %d\n",module_idP,CC_id,sdu_lengths[num_sdus],MTCH);
-      cc->mtch_active=1;
-      sdu_lcids[num_sdus] = MTCH;
-      sdu_length_total += sdu_lengths[num_sdus];
-
-      if (sdu_lengths[num_sdus] < 128) {
-        header_len_mtch = 2;
-      }
-
-      num_sdus++;
-    } else {
-      header_len_mtch = 0;
+    // there is MTCHs, loop if there are more than 1
+    if (mtch_flag == 1) {
+	// Calculate TBS
+	/* if ((msi_flag==1) || (mcch_flag==1)) {
+	   TBS = mac_xface->get_TBS(mcch_mcs, mac_xface->frame_parms->N_RB_DL);
+	   }
+	   else { // only MTCH in this subframeP
+	   TBS = mac_xface->get_TBS(RC.mac[module_idP]->pmch_Config[0]->dataMCS_r9, mac_xface->frame_parms->N_RB_DL);
+	   }
+
+	   // get MTCH data from RLC (like for DTCH)
+	   LOG_D(MAC,"[eNB %d] CC_id %d Frame %d subframe %d: Schedule MTCH (area %d, sfAlloc %d)\n",Mod_id,CC_id,frame,subframe,i,j);
+
+	   header_len_mtch = 3;
+	   LOG_D(MAC,"[eNB %d], CC_id %d, Frame %d, MTCH->MCH, Checking RLC status (rab %d, tbs %d, len %d)\n",
+	   Mod_id,CC_id,frame,MTCH,TBS,
+	   TBS-header_len_mcch-header_len_msi-sdu_length_total-header_len_mtch);
+
+	   rlc_status = mac_rlc_status_ind(Mod_id,frame,1,RLC_MBMS_YES,MTCH+ (maxDRB + 3) * MAX_MOBILES_PER_RG,
+	   TBS-header_len_mcch-header_len_msi-sdu_length_total-header_len_mtch);
+	   printf("frame %d, subframe %d,  rlc_status.bytes_in_buffer is %d\n",frame,subframe, rlc_status.bytes_in_buffer);
+
+	 */
+
+	// get MTCH data from RLC (like for DTCH)
+	LOG_D(MAC,
+	      "[eNB %d] CC_id %d Frame %d subframeP %d: Schedule MTCH (area %d, sfAlloc %d)\n",
+	      module_idP, CC_id, frameP, subframeP, i, j);
+
+	header_len_mtch = 3;
+	LOG_D(MAC,
+	      "[eNB %d], CC_id %d, Frame %d, MTCH->MCH, Checking RLC status (rab %d, tbs %d, len %d)\n",
+	      module_idP, CC_id, frameP, MTCH, TBS,
+	      TBS - header_len_mcch - header_len_msi - sdu_length_total -
+	      header_len_mtch);
+
+	rlc_status =
+	    mac_rlc_status_ind(module_idP, 0, frameP, subframeP,
+			       module_idP, ENB_FLAG_YES, MBMS_FLAG_YES,
+			       MTCH,
+			       TBS - header_len_mcch - header_len_msi -
+			       sdu_length_total - header_len_mtch);
+	LOG_D(MAC,
+	      "e-MBMS log channel %u frameP %d, subframeP %d,  rlc_status.bytes_in_buffer is %d\n",
+	      MTCH, frameP, subframeP, rlc_status.bytes_in_buffer);
+
+	if (rlc_status.bytes_in_buffer > 0) {
+	    LOG_I(MAC,
+		  "[eNB %d][MBMS USER-PLANE], CC_id %d, Frame %d, MTCH->MCH, Requesting %d bytes from RLC (header len mtch %d)\n",
+		  module_idP, CC_id, frameP,
+		  TBS - header_len_mcch - header_len_msi -
+		  sdu_length_total - header_len_mtch, header_len_mtch);
+
+	    sdu_lengths[num_sdus] = mac_rlc_data_req(module_idP, 0, module_idP, frameP, ENB_FLAG_YES, MBMS_FLAG_YES, MTCH, 0,	//not used
+						     (char *)
+						     &mch_buffer
+						     [sdu_length_total]);
+	    //sdu_lengths[num_sdus] = mac_rlc_data_req(module_idP,frameP, MBMS_FLAG_NO,  MTCH+(MAX_NUM_RB*(NUMBER_OF_UE_MAX+1)), (char*)&mch_buffer[sdu_length_total]);
+	    LOG_I(MAC,
+		  "[eNB %d][MBMS USER-PLANE] CC_id %d Got %d bytes for MTCH %d\n",
+		  module_idP, CC_id, sdu_lengths[num_sdus], MTCH);
+	    cc->mtch_active = 1;
+	    sdu_lcids[num_sdus] = MTCH;
+	    sdu_length_total += sdu_lengths[num_sdus];
+
+	    if (sdu_lengths[num_sdus] < 128) {
+		header_len_mtch = 2;
+	    }
+
+	    num_sdus++;
+	} else {
+	    header_len_mtch = 0;
+	}
     }
-  }
-
 #if defined(Rel10) || defined(Rel14)
-  //  }
+    //  }
 #endif
 
-  // FINAL STEP: Prepare and multiplexe MSI, MCCH and MTCHs
-  if ((sdu_length_total + header_len_msi + header_len_mcch + header_len_mtch) >0) {
-    // Adjust the last subheader
-    /*                                 if ((msi_flag==1) || (mcch_flag==1)) {
-                                       RC.mac[module_idP]->MCH_pdu.mcs = mcch_mcs;
-                                        }
-                                      else if (mtch_flag == 1) { // only MTCH in this subframeP
-                                     RC.mac[module_idP]->MCH_pdu.mcs = RC.mac[module_idP]->pmch_Config[0]->dataMCS_r9;
-                                        }
-     */
-    header_len_mtch_temp = header_len_mtch;
-    header_len_mcch_temp = header_len_mcch;
-    header_len_msi_temp = header_len_msi;
-
-    if (header_len_mtch>0) {
-      header_len_mtch=1;         // remove Length field in the  subheader for the last PDU
-    } else if (header_len_mcch>0) {
-      header_len_mcch=1;
+    // FINAL STEP: Prepare and multiplexe MSI, MCCH and MTCHs
+    if ((sdu_length_total + header_len_msi + header_len_mcch +
+	 header_len_mtch) > 0) {
+	// Adjust the last subheader
+	/*                                 if ((msi_flag==1) || (mcch_flag==1)) {
+	   RC.mac[module_idP]->MCH_pdu.mcs = mcch_mcs;
+	   }
+	   else if (mtch_flag == 1) { // only MTCH in this subframeP
+	   RC.mac[module_idP]->MCH_pdu.mcs = RC.mac[module_idP]->pmch_Config[0]->dataMCS_r9;
+	   }
+	 */
+	header_len_mtch_temp = header_len_mtch;
+	header_len_mcch_temp = header_len_mcch;
+	header_len_msi_temp = header_len_msi;
+
+	if (header_len_mtch > 0) {
+	    header_len_mtch = 1;	// remove Length field in the  subheader for the last PDU
+	} else if (header_len_mcch > 0) {
+	    header_len_mcch = 1;
+	} else {
+	    header_len_msi = 1;
+	}
+
+	// Calculate the padding
+	if ((TBS - header_len_mtch - header_len_mcch - header_len_msi -
+	     sdu_length_total) < 0) {
+	    LOG_E(MAC, "Error in building MAC PDU, TBS %d < PDU %d \n",
+		  TBS,
+		  header_len_mtch + header_len_mcch + header_len_msi +
+		  sdu_length_total);
+	    return 0;
+	} else
+	    if ((TBS - header_len_mtch - header_len_mcch - header_len_msi -
+		 sdu_length_total) <= 2) {
+	    padding =
+		(TBS - header_len_mtch - header_len_mcch - header_len_msi -
+		 sdu_length_total);
+	    post_padding = 0;
+	} else {		// using post_padding, give back the Length field of subheader  for the last PDU
+	    padding = 0;
+
+	    if (header_len_mtch > 0) {
+		header_len_mtch = header_len_mtch_temp;
+	    } else if (header_len_mcch > 0) {
+		header_len_mcch = header_len_mcch_temp;
+	    } else {
+		header_len_msi = header_len_msi_temp;
+	    }
+
+	    post_padding =
+		TBS - sdu_length_total - header_len_msi - header_len_mcch -
+		header_len_mtch;
+	}
+
+	// Generate the MAC Header for MCH
+	// here we use the function for DLSCH because DLSCH & MCH have the same Header structure
+	offset = generate_dlsch_header((unsigned char *) cc->MCH_pdu.payload, num_sdus, sdu_lengths, sdu_lcids, 255,	// no drx
+				       31,	// no timing advance
+				       NULL,	// no contention res id
+				       padding, post_padding);
+
+	cc->MCH_pdu.Pdu_size = TBS;
+	cc->MCH_pdu.sync_area = i;
+	cc->MCH_pdu.msi_active = cc->msi_active;
+	cc->MCH_pdu.mcch_active = cc->mcch_active;
+	cc->MCH_pdu.mtch_active = cc->mtch_active;
+	LOG_D(MAC,
+	      " MCS for this sf is %d (mcch active %d, mtch active %d)\n",
+	      cc->MCH_pdu.mcs, cc->MCH_pdu.mcch_active,
+	      cc->MCH_pdu.mtch_active);
+	LOG_I(MAC,
+	      "[eNB %d][MBMS USER-PLANE ] CC_id %d Generate header : sdu_length_total %d, num_sdus %d, sdu_lengths[0] %d, sdu_lcids[0] %d => payload offset %d,padding %d,post_padding %d (mcs %d, TBS %d), header MTCH %d, header MCCH %d, header MSI %d\n",
+	      module_idP, CC_id, sdu_length_total, num_sdus,
+	      sdu_lengths[0], sdu_lcids[0], offset, padding, post_padding,
+	      cc->MCH_pdu.mcs, TBS, header_len_mtch, header_len_mcch,
+	      header_len_msi);
+	// copy SDU to mch_pdu after the MAC Header
+	memcpy(&cc->MCH_pdu.payload[offset], mch_buffer, sdu_length_total);
+
+	// filling remainder of MCH with random data if necessery
+	for (j = 0; j < (TBS - sdu_length_total - offset); j++) {
+	    cc->MCH_pdu.payload[offset + sdu_length_total + j] =
+		(char) (taus() & 0xff);
+	}
+
+	/* Tracing of PDU is done on UE side */
+	if (opt_enabled == 1) {
+	    trace_pdu(1, (uint8_t *) cc->MCH_pdu.payload, TBS, module_idP, 6, 0xffff,	// M_RNTI = 6 in wirehsark
+		      RC.mac[module_idP]->frame,
+		      RC.mac[module_idP]->subframe, 0, 0);
+	    LOG_D(OPT,
+		  "[eNB %d][MCH] CC_id %d Frame %d : MAC PDU with size %d\n",
+		  module_idP, CC_id, frameP, TBS);
+	}
+
+	/*
+	   for (j=0;j<sdu_length_total;j++)
+	   printf("%2x.",RC.mac[module_idP]->MCH_pdu.payload[j+offset]);
+	   printf(" \n"); */
+	return 1;
     } else {
-      header_len_msi=1;
-    }
-
-    // Calculate the padding
-    if ((TBS - header_len_mtch - header_len_mcch - header_len_msi - sdu_length_total) < 0) {
-      LOG_E(MAC,"Error in building MAC PDU, TBS %d < PDU %d \n",
-            TBS, header_len_mtch + header_len_mcch + header_len_msi + sdu_length_total);
-      return 0;
-    } else if ((TBS - header_len_mtch - header_len_mcch - header_len_msi - sdu_length_total) <= 2) {
-      padding = (TBS - header_len_mtch - header_len_mcch - header_len_msi - sdu_length_total);
-      post_padding = 0;
-    } else { // using post_padding, give back the Length field of subheader  for the last PDU
-      padding = 0;
-
-      if (header_len_mtch>0) {
-        header_len_mtch = header_len_mtch_temp;
-      } else if (header_len_mcch>0) {
-        header_len_mcch = header_len_mcch_temp;
-      } else {
-        header_len_msi = header_len_msi_temp;
-      }
-
-      post_padding = TBS - sdu_length_total - header_len_msi - header_len_mcch - header_len_mtch;
-    }
-
-    // Generate the MAC Header for MCH
-    // here we use the function for DLSCH because DLSCH & MCH have the same Header structure
-    offset = generate_dlsch_header((unsigned char*)cc->MCH_pdu.payload,
-                                   num_sdus,
-                                   sdu_lengths,
-                                   sdu_lcids,
-                                   255,    // no drx
-                                   31,     // no timing advance
-                                   NULL,   // no contention res id
-                                   padding,
-                                   post_padding);
-
-    cc->MCH_pdu.Pdu_size=TBS;
-    cc->MCH_pdu.sync_area=i;
-    cc->MCH_pdu.msi_active= cc->msi_active;
-    cc->MCH_pdu.mcch_active= cc->mcch_active;
-    cc->MCH_pdu.mtch_active= cc->mtch_active;
-    LOG_D(MAC," MCS for this sf is %d (mcch active %d, mtch active %d)\n", cc->MCH_pdu.mcs,
-          cc->MCH_pdu.mcch_active,cc->MCH_pdu.mtch_active );
-    LOG_I(MAC,
-          "[eNB %d][MBMS USER-PLANE ] CC_id %d Generate header : sdu_length_total %d, num_sdus %d, sdu_lengths[0] %d, sdu_lcids[0] %d => payload offset %d,padding %d,post_padding %d (mcs %d, TBS %d), header MTCH %d, header MCCH %d, header MSI %d\n",
-          module_idP,CC_id,sdu_length_total,num_sdus,sdu_lengths[0],sdu_lcids[0],offset,padding,post_padding,cc->MCH_pdu.mcs,TBS,
-          header_len_mtch, header_len_mcch, header_len_msi);
-    // copy SDU to mch_pdu after the MAC Header
-    memcpy(&cc->MCH_pdu.payload[offset],mch_buffer,sdu_length_total);
-
-    // filling remainder of MCH with random data if necessery
-    for (j=0; j<(TBS-sdu_length_total-offset); j++) {
-      cc->MCH_pdu.payload[offset+sdu_length_total+j] = (char)(taus()&0xff);
-    }
-
-    /* Tracing of PDU is done on UE side */
-    if (opt_enabled ==1 ) {
-      trace_pdu(1, (uint8_t *)cc->MCH_pdu.payload,
-                TBS, module_idP, 6, 0xffff,  // M_RNTI = 6 in wirehsark
-                RC.mac[module_idP]->frame, RC.mac[module_idP]->subframe,0,0);
-      LOG_D(OPT,"[eNB %d][MCH] CC_id %d Frame %d : MAC PDU with size %d\n",
-            module_idP, CC_id, frameP, TBS);
+	cc->MCH_pdu.Pdu_size = 0;
+	cc->MCH_pdu.sync_area = 0;
+	cc->MCH_pdu.msi_active = 0;
+	cc->MCH_pdu.mcch_active = 0;
+	cc->MCH_pdu.mtch_active = 0;
+	// for testing purpose, fill with random data
+	//for (j=0;j<(TBS-sdu_length_total-offset);j++)
+	//  RC.mac[module_idP]->MCH_pdu.payload[offset+sdu_length_total+j] = (char)(taus()&0xff);
+	return 0;
     }
 
+    //this is for testing
     /*
-    for (j=0;j<sdu_length_total;j++)
-    printf("%2x.",RC.mac[module_idP]->MCH_pdu.payload[j+offset]);
-    printf(" \n");*/
-    return 1;
-  } else {
-    cc->MCH_pdu.Pdu_size=0;
-    cc->MCH_pdu.sync_area=0;
-    cc->MCH_pdu.msi_active=0;
-    cc->MCH_pdu.mcch_active=0;
-    cc->MCH_pdu.mtch_active=0;
-    // for testing purpose, fill with random data
-    //for (j=0;j<(TBS-sdu_length_total-offset);j++)
-    //  RC.mac[module_idP]->MCH_pdu.payload[offset+sdu_length_total+j] = (char)(taus()&0xff);
-    return 0;
-  }
-
-  //this is for testing
-  /*
-  if (mtch_flag == 1) {
-  //  LOG_D(MAC,"DUY: mch_buffer length so far is : %ld\n", &mch_buffer[sdu_length_total]-&mch_buffer[0]);
-  return 1;
-  }
-  else
-  return 0;
-   */
+       if (mtch_flag == 1) {
+       //  LOG_D(MAC,"DUY: mch_buffer length so far is : %ld\n", &mch_buffer[sdu_length_total]-&mch_buffer[0]);
+       return 1;
+       }
+       else
+       return 0;
+     */
 }
 
-MCH_PDU *get_mch_sdu(module_id_t module_idP, int CC_id, frame_t frameP, sub_frame_t subframeP)
+MCH_PDU *get_mch_sdu(module_id_t module_idP, int CC_id, frame_t frameP,
+		     sub_frame_t subframeP)
 {
-  //  RC.mac[module_idP]->MCH_pdu.mcs=0;
-  //LOG_D(MAC," MCH_pdu.mcs is %d\n", RC.mac[module_idP]->MCH_pdu.mcs);
+    //  RC.mac[module_idP]->MCH_pdu.mcs=0;
+    //LOG_D(MAC," MCH_pdu.mcs is %d\n", RC.mac[module_idP]->MCH_pdu.mcs);
 //#warning "MCH pdu should take the CC_id index"
-  return(&RC.mac[module_idP]->common_channels[CC_id].MCH_pdu);
+    return (&RC.mac[module_idP]->common_channels[CC_id].MCH_pdu);
 }
 
 #endif
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_primitives.c b/openair2/LAYER2/MAC/eNB_scheduler_primitives.c
index 19ff3cf39fa2ab77426feff29581365eefc95b32..42c50d8063a5450574b0ccc9610e6b234cb22621 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler_primitives.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_primitives.c
@@ -53,7 +53,7 @@
 #include "pdcp.h"
 
 #if defined(ENABLE_ITTI)
-# include "intertask_interface.h"
+#include "intertask_interface.h"
 #endif
 
 #include "T.h"
@@ -61,1449 +61,1781 @@
 #define ENABLE_MAC_PAYLOAD_DEBUG
 #define DEBUG_eNB_SCHEDULER 1
 
-int choose(int n,int k)
+int choose(int n, int k)
 {
-  int res  = 1;
-  int res2 = 1;
-  int i;
+    int res = 1;
+    int res2 = 1;
+    int i;
 
-  if (k>n) return(0);
-  if (n==k) return(1);
+    if (k > n)
+	return (0);
+    if (n == k)
+	return (1);
 
-  for (i=n;i>k;i--) res*=i;
-  for (i=2;i<=(n-k);i++) res2*=i;
+    for (i = n; i > k; i--)
+	res *= i;
+    for (i = 2; i <= (n - k); i++)
+	res2 *= i;
 
-  return(res/res2);
+    return (res / res2);
 }
 
 // Patented algorithm from Yang et al, US Patent 2009, "Channel Quality Indexing and Reverse Indexing"
-void reverse_index(int N,int M,int r,int *v)
-{
-  int BaseValue=0;
-  int IncreaseValue,ThresholdValue;
-  int sumV;
-  int i;
-
-  r = choose(N,M) - 1 - r;
-  memset((void*)v,0,M*sizeof(int));
-
-  sumV=0;
-  i=M;
-  while (i>0 && r>0) {
-    IncreaseValue = choose(N-M+1-sumV-v[i-1]+i-2,i-1);
-    ThresholdValue = BaseValue+IncreaseValue;
-    if (r>=ThresholdValue) {
-      v[i-1]++;
-      BaseValue=ThresholdValue;
-    }
-    else {
-      r=r-BaseValue;
-      sumV+=v[i-1];
-      i--;
-      BaseValue=0;
+void reverse_index(int N, int M, int r, int *v)
+{
+    int BaseValue = 0;
+    int IncreaseValue, ThresholdValue;
+    int sumV;
+    int i;
+
+    r = choose(N, M) - 1 - r;
+    memset((void *) v, 0, M * sizeof(int));
+
+    sumV = 0;
+    i = M;
+    while (i > 0 && r > 0) {
+	IncreaseValue = choose(N - M + 1 - sumV - v[i - 1] + i - 2, i - 1);
+	ThresholdValue = BaseValue + IncreaseValue;
+	if (r >= ThresholdValue) {
+	    v[i - 1]++;
+	    BaseValue = ThresholdValue;
+	} else {
+	    r = r - BaseValue;
+	    sumV += v[i - 1];
+	    i--;
+	    BaseValue = 0;
+	}
     }
-  }
 }
 
 int to_prb(int dl_Bandwidth)
 {
-  int prbmap[6] = {6,15,25,50,75,100};
+    int prbmap[6] = { 6, 15, 25, 50, 75, 100 };
 
-  AssertFatal(dl_Bandwidth < 6,"dl_Bandwidth is 0..5\n");
-  return(prbmap[dl_Bandwidth]);
+    AssertFatal(dl_Bandwidth < 6, "dl_Bandwidth is 0..5\n");
+    return (prbmap[dl_Bandwidth]);
 }
 
 int to_rbg(int dl_Bandwidth)
 {
-  int rbgmap[6] = {6,8,13,17,19,25};
+    int rbgmap[6] = { 6, 8, 13, 17, 19, 25 };
 
-  AssertFatal(dl_Bandwidth < 6,"dl_Bandwidth is 0..5\n");
-  return(rbgmap[dl_Bandwidth]);
+    AssertFatal(dl_Bandwidth < 6, "dl_Bandwidth is 0..5\n");
+    return (rbgmap[dl_Bandwidth]);
 }
 
-int get_phich_resource_times6(COMMON_channels_t *cc)
+int get_phich_resource_times6(COMMON_channels_t * cc)
 {
-  int phichmap[4] = {1,3,6,12};
-  AssertFatal(cc!=NULL,"cc is null\n");
-  AssertFatal(cc->mib!=NULL,"cc->mib is null\n");
-  AssertFatal((cc->mib->message.phich_Config.phich_Resource>=0) &&
-              (cc->mib->message.phich_Config.phich_Resource<4),
-              "phich_Resource %d not in 0..3\n",(int)cc->mib->message.phich_Config.phich_Resource);
-
-  return(phichmap[cc->mib->message.phich_Config.phich_Resource]);
+    int phichmap[4] = { 1, 3, 6, 12 };
+    AssertFatal(cc != NULL, "cc is null\n");
+    AssertFatal(cc->mib != NULL, "cc->mib is null\n");
+    AssertFatal((cc->mib->message.phich_Config.phich_Resource >= 0) &&
+		(cc->mib->message.phich_Config.phich_Resource < 4),
+		"phich_Resource %d not in 0..3\n",
+		(int) cc->mib->message.phich_Config.phich_Resource);
+
+    return (phichmap[cc->mib->message.phich_Config.phich_Resource]);
 }
 
-uint16_t mac_computeRIV(uint16_t N_RB_DL,uint16_t RBstart,uint16_t Lcrbs)
+uint16_t mac_computeRIV(uint16_t N_RB_DL, uint16_t RBstart, uint16_t Lcrbs)
 {
-  uint16_t RIV;
+    uint16_t RIV;
 
-  if (Lcrbs<=(1+(N_RB_DL>>1)))
-    RIV = (N_RB_DL*(Lcrbs-1)) + RBstart;
-  else
-    RIV = (N_RB_DL*(N_RB_DL+1-Lcrbs)) + (N_RB_DL-1-RBstart);
+    if (Lcrbs <= (1 + (N_RB_DL >> 1)))
+	RIV = (N_RB_DL * (Lcrbs - 1)) + RBstart;
+    else
+	RIV = (N_RB_DL * (N_RB_DL + 1 - Lcrbs)) + (N_RB_DL - 1 - RBstart);
 
-  return(RIV);
+    return (RIV);
 }
 
 uint8_t getQm(uint8_t mcs)
 {
-  if (mcs<10) return(2);
-  else if (mcs<17) return(4);
-  else return (6);
+    if (mcs < 10)
+	return (2);
+    else if (mcs < 17)
+	return (4);
+    else
+	return (6);
 }
 
-void get_Msg3alloc(COMMON_channels_t *cc,
-
-                   sub_frame_t current_subframe,
-                   frame_t current_frame,
-                   frame_t *frame,
-                   sub_frame_t *subframe)
+void
+get_Msg3alloc(COMMON_channels_t * cc,
+	      sub_frame_t current_subframe,
+	      frame_t current_frame, frame_t * frame,
+	      sub_frame_t * subframe)
 {
-  // Fill in other TDD Configuration!!!!
-
-  if (cc->tdd_Config==NULL) { // FDD
-    *subframe = current_subframe+6;
-
-    if (*subframe>9) {
-      *subframe = *subframe-10;
-      *frame = (current_frame+1) & 1023;
-    } else {
-      *frame=current_frame;
-    }
-  } else { // TDD
-    if (cc->tdd_Config->subframeAssignment == 1) {
-      switch (current_subframe) {
-
-      case 0:
-        *subframe = 7;
-        *frame = current_frame;
-        break;
-
-      case 4:
-        *subframe = 2;
-        *frame = (current_frame+1) & 1023;
-        break;
-
-      case 5:
-        *subframe = 2;
-        *frame = (current_frame+1) & 1023;
-        break;
-
-      case 9:
-        *subframe = 7;
-        *frame = (current_frame+1) & 1023;
-        break;
-      }
-    } else if (cc->tdd_Config->subframeAssignment == 3) {
-      switch (current_subframe) {
-
-      case 0:
-      case 5:
-      case 6:
-        *subframe = 2;
-        *frame = (current_frame+1) & 1023;
-        break;
-
-      case 7:
-        *subframe = 3;
-        *frame = (current_frame+1) & 1023;
-        break;
-
-      case 8:
-        *subframe = 4;
-        *frame = (current_frame+1) & 1023;
-        break;
-
-      case 9:
-        *subframe = 2;
-        *frame = (current_frame+2) & 1023;
-        break;
-      }
-    } else if (cc->tdd_Config->subframeAssignment == 4) {
-      switch (current_subframe) {
-
-      case 0:
-      case 4:
-      case 5:
-      case 6:
-        *subframe = 2;
-        *frame = (current_frame+1) & 1023;
-        break;
-
-      case 7:
-        *subframe = 3;
-        *frame = (current_frame+1) & 1023;
-        break;
-
-      case 8:
-      case 9:
-        *subframe = 2;
-        *frame = (current_frame+2) & 1023;
-        break;
-      }
-    } else if (cc->tdd_Config->subframeAssignment == 5) {
-      switch (current_subframe) {
-
-      case 0:
-      case 4:
-      case 5:
-      case 6:
-        *subframe = 2;
-        *frame = (current_frame+1) & 1023;
-        break;
-
-      case 7:
-      case 8:
-      case 9:
-        *subframe = 2;
-        *frame = (current_frame+2) & 1023;
-        break;
-      }
+    // Fill in other TDD Configuration!!!!
+
+    if (cc->tdd_Config == NULL) {	// FDD
+	*subframe = current_subframe + 6;
+
+	if (*subframe > 9) {
+	    *subframe = *subframe - 10;
+	    *frame = (current_frame + 1) & 1023;
+	} else {
+	    *frame = current_frame;
+	}
+    } else {			// TDD
+	if (cc->tdd_Config->subframeAssignment == 1) {
+	    switch (current_subframe) {
+
+	    case 0:
+		*subframe = 7;
+		*frame = current_frame;
+		break;
+
+	    case 4:
+		*subframe = 2;
+		*frame = (current_frame + 1) & 1023;
+		break;
+
+	    case 5:
+		*subframe = 2;
+		*frame = (current_frame + 1) & 1023;
+		break;
+
+	    case 9:
+		*subframe = 7;
+		*frame = (current_frame + 1) & 1023;
+		break;
+	    }
+	} else if (cc->tdd_Config->subframeAssignment == 3) {
+	    switch (current_subframe) {
+
+	    case 0:
+	    case 5:
+	    case 6:
+		*subframe = 2;
+		*frame = (current_frame + 1) & 1023;
+		break;
+
+	    case 7:
+		*subframe = 3;
+		*frame = (current_frame + 1) & 1023;
+		break;
+
+	    case 8:
+		*subframe = 4;
+		*frame = (current_frame + 1) & 1023;
+		break;
+
+	    case 9:
+		*subframe = 2;
+		*frame = (current_frame + 2) & 1023;
+		break;
+	    }
+	} else if (cc->tdd_Config->subframeAssignment == 4) {
+	    switch (current_subframe) {
+
+	    case 0:
+	    case 4:
+	    case 5:
+	    case 6:
+		*subframe = 2;
+		*frame = (current_frame + 1) & 1023;
+		break;
+
+	    case 7:
+		*subframe = 3;
+		*frame = (current_frame + 1) & 1023;
+		break;
+
+	    case 8:
+	    case 9:
+		*subframe = 2;
+		*frame = (current_frame + 2) & 1023;
+		break;
+	    }
+	} else if (cc->tdd_Config->subframeAssignment == 5) {
+	    switch (current_subframe) {
+
+	    case 0:
+	    case 4:
+	    case 5:
+	    case 6:
+		*subframe = 2;
+		*frame = (current_frame + 1) & 1023;
+		break;
+
+	    case 7:
+	    case 8:
+	    case 9:
+		*subframe = 2;
+		*frame = (current_frame + 2) & 1023;
+		break;
+	    }
+	}
     }
-  }
 }
 
 
 
-void get_Msg3allocret(COMMON_channels_t *cc,
-                      sub_frame_t current_subframe,
-                      frame_t current_frame,
-                      frame_t *frame,
-                      sub_frame_t *subframe)
+void
+get_Msg3allocret(COMMON_channels_t * cc,
+		 sub_frame_t current_subframe,
+		 frame_t current_frame,
+		 frame_t * frame, sub_frame_t * subframe)
 {
-  if (cc->tdd_Config == NULL) { //FDD
-    /* always retransmit in n+8 */
-    *subframe = current_subframe + 8;
-
-    if (*subframe > 9) {
-      *subframe = *subframe - 10;
-      *frame = (current_frame + 1) & 1023;
+    if (cc->tdd_Config == NULL) {	//FDD
+	/* always retransmit in n+8 */
+	*subframe = current_subframe + 8;
+
+	if (*subframe > 9) {
+	    *subframe = *subframe - 10;
+	    *frame = (current_frame + 1) & 1023;
+	} else {
+	    *frame = current_frame;
+	}
     } else {
-      *frame = current_frame;
-    }
-  } else {
-    if (cc->tdd_Config->subframeAssignment == 1) {
-      // original PUSCH in 2, PHICH in 6 (S), ret in 2
-      // original PUSCH in 3, PHICH in 9, ret in 3
-      // original PUSCH in 7, PHICH in 1 (S), ret in 7
-      // original PUSCH in 8, PHICH in 4, ret in 8
-      *frame = (current_frame+1) & 1023;
-    } else if (cc->tdd_Config->subframeAssignment == 3) {
-      // original PUSCH in 2, PHICH in 8, ret in 2 next frame
-      // original PUSCH in 3, PHICH in 9, ret in 3 next frame
-      // original PUSCH in 4, PHICH in 0, ret in 4 next frame
-      *frame=(current_frame+1) & 1023;
-    } else if (cc->tdd_Config->subframeAssignment == 4) {
-        // original PUSCH in 2, PHICH in 8, ret in 2 next frame
-        // original PUSCH in 3, PHICH in 9, ret in 3 next frame
-        *frame=(current_frame+1) & 1023;
-    } else if (cc->tdd_Config->subframeAssignment == 5) {
-        // original PUSCH in 2, PHICH in 8, ret in 2 next frame
-        *frame=(current_frame+1) & 1023;
+	if (cc->tdd_Config->subframeAssignment == 1) {
+	    // original PUSCH in 2, PHICH in 6 (S), ret in 2
+	    // original PUSCH in 3, PHICH in 9, ret in 3
+	    // original PUSCH in 7, PHICH in 1 (S), ret in 7
+	    // original PUSCH in 8, PHICH in 4, ret in 8
+	    *frame = (current_frame + 1) & 1023;
+	} else if (cc->tdd_Config->subframeAssignment == 3) {
+	    // original PUSCH in 2, PHICH in 8, ret in 2 next frame
+	    // original PUSCH in 3, PHICH in 9, ret in 3 next frame
+	    // original PUSCH in 4, PHICH in 0, ret in 4 next frame
+	    *frame = (current_frame + 1) & 1023;
+	} else if (cc->tdd_Config->subframeAssignment == 4) {
+	    // original PUSCH in 2, PHICH in 8, ret in 2 next frame
+	    // original PUSCH in 3, PHICH in 9, ret in 3 next frame
+	    *frame = (current_frame + 1) & 1023;
+	} else if (cc->tdd_Config->subframeAssignment == 5) {
+	    // original PUSCH in 2, PHICH in 8, ret in 2 next frame
+	    *frame = (current_frame + 1) & 1023;
+	}
     }
-  }
 }
 
-uint8_t subframe2harqpid(COMMON_channels_t *cc,frame_t frame,sub_frame_t subframe)
+uint8_t
+subframe2harqpid(COMMON_channels_t * cc, frame_t frame,
+		 sub_frame_t subframe)
 {
-  uint8_t ret = 255;
+    uint8_t ret = 255;
 
-  AssertFatal(cc!=NULL,"cc is null\n");
+    AssertFatal(cc != NULL, "cc is null\n");
 
-  if (cc->tdd_Config == NULL) { // FDD
-    ret = (((frame<<1)+subframe)&7);
-  } else {
-    switch (cc->tdd_Config->subframeAssignment) {
-    case 1:
-      if ((subframe==2) ||
-          (subframe==3) ||
-          (subframe==7) ||
-          (subframe==8))
-        switch (subframe) {
-        case 2:
-        case 3:
-          ret = (subframe-2);
-          break;
-
-        case 7:
-        case 8:
-          ret = (subframe-5);
-          break;
-
-        default:
-          AssertFatal(1==0,"subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframe,(int)cc->tdd_Config->subframeAssignment);
-          break;
-        }
-
-      break;
+    if (cc->tdd_Config == NULL) {	// FDD
+	ret = (((frame << 1) + subframe) & 7);
+    } else {
+	switch (cc->tdd_Config->subframeAssignment) {
+	case 1:
+	    if ((subframe == 2) ||
+		(subframe == 3) || (subframe == 7) || (subframe == 8))
+		switch (subframe) {
+		case 2:
+		case 3:
+		    ret = (subframe - 2);
+		    break;
+
+		case 7:
+		case 8:
+		    ret = (subframe - 5);
+		    break;
+
+		default:
+		    AssertFatal(1 == 0,
+				"subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
+				subframe,
+				(int) cc->tdd_Config->subframeAssignment);
+		    break;
+		}
+
+	    break;
+
+	case 2:
+	    AssertFatal((subframe == 2) || (subframe == 7),
+			"subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
+			subframe,
+			(int) cc->tdd_Config->subframeAssignment);
+
+	    ret = (subframe / 7);
+	    break;
+
+	case 3:
+	    AssertFatal((subframe > 1) && (subframe < 5),
+			"subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
+			subframe,
+			(int) cc->tdd_Config->subframeAssignment);
+	    ret = (subframe - 2);
+	    break;
+
+	case 4:
+	    AssertFatal((subframe > 1) && (subframe < 4),
+			"subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
+			subframe,
+			(int) cc->tdd_Config->subframeAssignment);
+	    ret = (subframe - 2);
+	    break;
+
+	case 5:
+	    AssertFatal(subframe == 2,
+			"subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
+			subframe,
+			(int) cc->tdd_Config->subframeAssignment);
+	    ret = (subframe - 2);
+	    break;
+
+	default:
+	    AssertFatal(1 == 0,
+			"subframe2_harq_pid, Unsupported TDD mode %d\n",
+			(int) cc->tdd_Config->subframeAssignment);
+	}
+    }
+    return ret;
+}
 
-    case 2:
-      AssertFatal((subframe==2) || (subframe==7),
-                  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframe,(int)cc->tdd_Config->subframeAssignment);
+uint8_t
+get_Msg3harqpid(COMMON_channels_t * cc,
+		frame_t frame, sub_frame_t current_subframe)
+{
+    uint8_t ul_subframe = 0;
+    uint32_t ul_frame = 0;
+
+    if (cc->tdd_Config == NULL) {	// FDD
+	ul_subframe =
+	    (current_subframe >
+	     3) ? (current_subframe - 4) : (current_subframe + 6);
+	ul_frame = (current_subframe > 3) ? ((frame + 1) & 1023) : frame;
+    } else {
+	switch (cc->tdd_Config->subframeAssignment) {
+	case 1:
+	    switch (current_subframe) {
+	    case 9:
+	    case 0:
+		ul_subframe = 7;
+		break;
+
+	    case 5:
+	    case 7:
+		ul_subframe = 2;
+		break;
+
+	    }
+
+	    break;
+
+	case 3:
+	    switch (current_subframe) {
+	    case 0:
+	    case 5:
+	    case 6:
+		ul_subframe = 2;
+		break;
+
+	    case 7:
+		ul_subframe = 3;
+		break;
+
+	    case 8:
+		ul_subframe = 4;
+		break;
+
+	    case 9:
+		ul_subframe = 2;
+		break;
+	    }
+
+	    break;
+
+	case 4:
+	    switch (current_subframe) {
+	    case 0:
+	    case 5:
+	    case 6:
+	    case 8:
+	    case 9:
+		ul_subframe = 2;
+		break;
+
+	    case 7:
+		ul_subframe = 3;
+		break;
+	    }
+
+	    break;
+
+	case 5:
+	    ul_subframe = 2;
+	    break;
+
+	default:
+	    LOG_E(PHY,
+		  "get_Msg3_harq_pid: Unsupported TDD configuration %d\n",
+		  (int) cc->tdd_Config->subframeAssignment);
+	    AssertFatal(1 == 0,
+			"get_Msg3_harq_pid: Unsupported TDD configuration");
+	    break;
+	}
+    }
 
-      ret = (subframe/7);
-      break;
+    return (subframe2harqpid(cc, ul_frame, ul_subframe));
+}
 
-    case 3:
-      AssertFatal((subframe>1) && (subframe<5),
-                  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframe,(int)cc->tdd_Config->subframeAssignment);
-      ret = (subframe-2);
-      break;
+uint32_t
+pdcchalloc2ulframe(COMMON_channels_t * ccP, uint32_t frame, uint8_t n)
+{
+    uint32_t ul_frame;
+
+    if ((ccP->tdd_Config) && (ccP->tdd_Config->subframeAssignment == 1) && ((n == 1) || (n == 6)))	// tdd_config 0,1 SF 1,5
+	ul_frame = (frame + (n == 1 ? 0 : 1));
+    else if ((ccP->tdd_Config) &&
+	     (ccP->tdd_Config->subframeAssignment == 6) &&
+	     ((n == 0) || (n == 1) || (n == 5) || (n == 6)))
+	ul_frame = (frame + (n >= 5 ? 1 : 0));
+    else if ((ccP->tdd_Config) && (ccP->tdd_Config->subframeAssignment == 6) && (n == 9))	// tdd_config 6 SF 9
+	ul_frame = (frame + 1);
+    else
+	ul_frame = (frame + (n >= 6 ? 1 : 0));
 
-    case 4:
-      AssertFatal((subframe>1) && (subframe<4),
-                  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframe,(int)cc->tdd_Config->subframeAssignment);
-      ret = (subframe-2);
-      break;
+    LOG_D(PHY, "frame %d subframe %d: PUSCH frame = %d\n", frame, n,
+	  ul_frame);
+    return ul_frame;
+}
 
-    case 5:
-      AssertFatal(subframe==2,
-                  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframe,(int)cc->tdd_Config->subframeAssignment);
-      ret = (subframe-2);
-      break;
+uint8_t pdcchalloc2ulsubframe(COMMON_channels_t * ccP, uint8_t n)
+{
+    uint8_t ul_subframe;
+
+    if ((ccP->tdd_Config) && (ccP->tdd_Config->subframeAssignment == 1) && ((n == 1) || (n == 6)))	// tdd_config 0,1 SF 1,5
+	ul_subframe = ((n + 6) % 10);
+    else if ((ccP->tdd_Config) &&
+	     (ccP->tdd_Config->subframeAssignment == 6) &&
+	     ((n == 0) || (n == 1) || (n == 5) || (n == 6)))
+	ul_subframe = ((n + 7) % 10);
+    else if ((ccP->tdd_Config) && (ccP->tdd_Config->subframeAssignment == 6) && (n == 9))	// tdd_config 6 SF 9
+	ul_subframe = ((n + 5) % 10);
+    else
+	ul_subframe = ((n + 4) % 10);
 
-    default:
-      AssertFatal(1==0,"subframe2_harq_pid, Unsupported TDD mode %d\n",(int)cc->tdd_Config->subframeAssignment);
-    }
-  }
-  return ret;
+    LOG_D(PHY, "subframe %d: PUSCH subframe = %d\n", n, ul_subframe);
+    return ul_subframe;
 }
 
-uint8_t get_Msg3harqpid(COMMON_channels_t *cc,
-                        frame_t frame,
-                        sub_frame_t current_subframe)
+int is_UL_sf(COMMON_channels_t * ccP, sub_frame_t subframeP)
 {
-  uint8_t ul_subframe=0;
-  uint32_t ul_frame=0;
+    // if FDD return dummy value
+    if (ccP->tdd_Config == NULL)
+	return (0);
 
-  if (cc->tdd_Config == NULL) { // FDD
-    ul_subframe = (current_subframe>3) ? (current_subframe-4) : (current_subframe+6);
-    ul_frame    = (current_subframe>3) ? ((frame+1)&1023) : frame;
-  } else {
-    switch (cc->tdd_Config->subframeAssignment) {
+    switch (ccP->tdd_Config->subframeAssignment) {
     case 1:
-      switch (current_subframe) {
-      case 9:
-      case 0:
-        ul_subframe = 7;
-        break;
-
-      case 5:
-      case 7:
-        ul_subframe = 2;
-        break;
-
-      }
-
-      break;
+	switch (subframeP) {
+	case 0:
+	case 4:
+	case 5:
+	case 9:
+	    return (0);
+	    break;
+
+	case 2:
+	case 3:
+	case 7:
+	case 8:
+	    return (1);
+	    break;
+
+	default:
+	    return (0);
+	    break;
+	}
+	break;
 
     case 3:
-      switch (current_subframe) {
-      case 0:
-      case 5:
-      case 6:
-        ul_subframe = 2;
-        break;
-
-      case 7:
-        ul_subframe = 3;
-        break;
-
-      case 8:
-        ul_subframe = 4;
-        break;
-
-      case 9:
-        ul_subframe = 2;
-        break;
-      }
-
-      break;
+	if ((subframeP <= 1) || (subframeP >= 5))
+	    return (0);
+	else if ((subframeP > 1) && (subframeP < 5))
+	    return (1);
+	else
+	    AssertFatal(1 == 0, "Unknown subframe number\n");
+	break;
 
     case 4:
-      switch (current_subframe) {
-      case 0:
-      case 5:
-      case 6:
-      case 8:
-      case 9:
-        ul_subframe = 2;
-        break;
-
-      case 7:
-        ul_subframe = 3;
-        break;
-      }
-
-      break;
+	if ((subframeP <= 1) || (subframeP >= 4))
+	    return (0);
+	else if ((subframeP > 1) && (subframeP < 4))
+	    return (1);
+	else
+	    AssertFatal(1 == 0, "Unknown subframe number\n");
+	break;
 
     case 5:
-      ul_subframe =2;
-      break;
+	if ((subframeP <= 1) || (subframeP >= 3))
+	    return (0);
+	else if ((subframeP > 1) && (subframeP < 3))
+	    return (1);
+	else
+	    AssertFatal(1 == 0, "Unknown subframe number\n");
+	break;
 
     default:
-      LOG_E(PHY,"get_Msg3_harq_pid: Unsupported TDD configuration %d\n",(int)cc->tdd_Config->subframeAssignment);
-      AssertFatal(1==0,"get_Msg3_harq_pid: Unsupported TDD configuration");
-      break;
+	AssertFatal(1 == 0,
+		    "subframe %d Unsupported TDD configuration %d\n",
+		    subframeP, (int) ccP->tdd_Config->subframeAssignment);
+	break;
     }
-  }
-
-  return(subframe2harqpid(cc,ul_frame,ul_subframe));
 }
 
-uint32_t pdcchalloc2ulframe(COMMON_channels_t *ccP,uint32_t frame, uint8_t n)
+uint16_t get_pucch1_absSF(COMMON_channels_t * cc, uint16_t dlsch_absSF)
 {
-  uint32_t ul_frame;
-
-  if ((ccP->tdd_Config) &&
-      (ccP->tdd_Config->subframeAssignment == 1) &&
-      ((n==1)||(n==6))) // tdd_config 0,1 SF 1,5
-    ul_frame = (frame + (n==1 ? 0 : 1));
-  else if ((ccP->tdd_Config) &&
-           (ccP->tdd_Config->subframeAssignment == 6) &&
-           ((n==0)||(n==1)||(n==5)||(n==6)))
-    ul_frame = (frame + (n>=5 ? 1 : 0));
-  else if ((ccP->tdd_Config) &&
-           (ccP->tdd_Config->subframeAssignment == 6) &&
-           (n==9)) // tdd_config 6 SF 9
-    ul_frame = (frame+1);
-  else
-    ul_frame = (frame+(n>=6 ? 1 : 0));
+    uint16_t sf, f, nextf;
 
-  LOG_D(PHY, "frame %d subframe %d: PUSCH frame = %d\n", frame, n, ul_frame);
-  return ul_frame;
+    if (cc->tdd_Config == NULL) {	//FDD n+4
+	return ((dlsch_absSF + 4) % 10240);
+    } else {
+	sf = dlsch_absSF % 10;
+	f = dlsch_absSF / 10;
+	nextf = (f + 1) & 1023;
+
+	switch (cc->tdd_Config->subframeAssignment) {
+	case 0:
+	    AssertFatal(1 == 0, "SFA 0 to be filled in now, :-)\n");
+	    break;
+	case 1:
+	    if ((sf == 5) || (sf == 6))
+		return ((10 * nextf) + 2);	// ACK/NAK in SF 2 next frame
+	    else if (sf == 9)
+		return ((10 * nextf) + 3);	// ACK/NAK in SF 3 next frame
+	    else if ((sf == 0) || (sf == 1))
+		return ((10 * f) + 2);	// ACK/NAK in SF 7 same frame
+	    else
+		AssertFatal(1 == 0,
+			    "Impossible dlsch subframe %d for TDD configuration 3\n",
+			    sf);
+	    break;
+	case 2:
+	    if ((sf == 4) || (sf == 5) || (sf == 6) || (sf == 8))
+		return ((10 * nextf) + 2);	// ACK/NAK in SF 2 next frame
+	    else if (sf == 9)
+		return ((10 * nextf) + 7);	// ACK/NAK in SF 7 next frame
+	    else if ((sf == 0) || (sf == 1) || (sf == 3))
+		return ((10 * f) + 7);	// ACK/NAK in SF 7 same frame
+	    else
+		AssertFatal(1 == 0,
+			    "Impossible dlsch subframe %d for TDD configuration 3\n",
+			    sf);
+	    break;
+	case 3:
+	    if ((sf == 5) || (sf == 6) || (sf == 7) || (sf == 8)
+		|| (sf == 9))
+		return ((10 * nextf) + (sf >> 1));	// ACK/NAK in 2,3,4 resp. next frame
+	    else if (sf == 1)
+		return ((10 * nextf) + 2);	// ACK/NAK in 2 next frame
+	    else if (sf == 0)
+		return ((10 * f) + 4);	// ACK/NAK in 4 same frame
+	    else
+		AssertFatal(1 == 0,
+			    "Impossible dlsch subframe %d for TDD configuration 3\n",
+			    sf);
+	    break;
+	case 4:
+	    if ((sf == 6) || (sf == 7) || (sf == 8) || (sf == 9))
+		return (((10 * nextf) + 3) % 10240);	// ACK/NAK in SF 3 next frame
+	    else if ((sf == 0) || (sf == 1) || (sf == 4) || (sf == 5))
+		return (((10 * nextf) + 2) % 10240);	// ACK/NAK in SF 2 next frame
+	    else
+		AssertFatal(1 == 0,
+			    "Impossible dlsch subframe %d for TDD configuration 4\n",
+			    sf);
+	    break;
+	case 5:
+	    if ((sf == 0) || (sf == 1) || (sf == 3) || (sf == 4)
+		|| (sf == 5) || (sf == 6) || (sf == 7) || (sf == 8))
+		return (((10 * nextf) + 2) % 10240);	// ACK/NAK in SF 3 next frame
+	    else if (sf == 9)
+		return (((10 * (1 + nextf)) + 2) % 10240);	// ACK/NAK in SF 2 next frame
+	    else
+		AssertFatal(1 == 0,
+			    "Impossible dlsch subframe %d for TDD configuration 5\n",
+			    sf);
+	    break;
+	case 6:
+	    AssertFatal(1 == 0, "SFA 6 To be filled in now, :-)\n");
+	    break;
+	default:
+	    AssertFatal(1 == 0, "Illegal TDD subframe Assigment %d\n",
+			(int) cc->tdd_Config->subframeAssignment);
+	    break;
+	}
+    }
+    AssertFatal(1 == 0, "Shouldn't get here\n");
 }
 
-uint8_t pdcchalloc2ulsubframe(COMMON_channels_t *ccP,uint8_t n)
+void
+get_srs_pos(COMMON_channels_t * cc, uint16_t isrs,
+	    uint16_t * psrsPeriodicity, uint16_t * psrsOffset)
 {
-  uint8_t ul_subframe;
-
-  if ((ccP->tdd_Config) &&
-      (ccP->tdd_Config->subframeAssignment == 1) &&
-      ((n==1)||(n==6))) // tdd_config 0,1 SF 1,5
-    ul_subframe = ((n+6)%10);
-  else if ((ccP->tdd_Config) &&
-           (ccP->tdd_Config->subframeAssignment == 6) &&
-           ((n==0)||(n==1)||(n==5)||(n==6)))
-    ul_subframe = ((n+7)%10);
-  else if ((ccP->tdd_Config) &&
-           (ccP->tdd_Config->subframeAssignment == 6) &&
-           (n==9)) // tdd_config 6 SF 9
-    ul_subframe = ((n+5)%10);
-  else
-    ul_subframe = ((n+4)%10);
-
-  LOG_D(PHY, "subframe %d: PUSCH subframe = %d\n", n, ul_subframe);
-  return ul_subframe;
+    if (cc->tdd_Config) {	// TDD
+	AssertFatal(isrs >= 10, "2 ms SRS periodicity not supported");
+
+	if ((isrs > 9) && (isrs < 15)) {
+	    *psrsPeriodicity = 5;
+	    *psrsOffset = isrs - 10;
+	}
+	if ((isrs > 14) && (isrs < 25)) {
+	    *psrsPeriodicity = 10;
+	    *psrsOffset = isrs - 15;
+	}
+	if ((isrs > 24) && (isrs < 45)) {
+	    *psrsPeriodicity = 20;
+	    *psrsOffset = isrs - 25;
+	}
+	if ((isrs > 44) && (isrs < 85)) {
+	    *psrsPeriodicity = 40;
+	    *psrsOffset = isrs - 45;
+	}
+	if ((isrs > 84) && (isrs < 165)) {
+	    *psrsPeriodicity = 80;
+	    *psrsOffset = isrs - 85;
+	}
+	if ((isrs > 164) && (isrs < 325)) {
+	    *psrsPeriodicity = 160;
+	    *psrsOffset = isrs - 165;
+	}
+	if ((isrs > 324) && (isrs < 645)) {
+	    *psrsPeriodicity = 320;
+	    *psrsOffset = isrs - 325;
+	}
+
+	AssertFatal(isrs <= 644, "Isrs out of range %d>644\n", isrs);
+    }				// TDD
+    else {			// FDD
+	if (isrs < 2) {
+	    *psrsPeriodicity = 2;
+	    *psrsOffset = isrs;
+	}
+	if ((isrs > 1) && (isrs < 7)) {
+	    *psrsPeriodicity = 5;
+	    *psrsOffset = isrs - 2;
+	}
+	if ((isrs > 6) && (isrs < 17)) {
+	    *psrsPeriodicity = 10;
+	    *psrsOffset = isrs - 7;
+	}
+	if ((isrs > 16) && (isrs < 37)) {
+	    *psrsPeriodicity = 20;
+	    *psrsOffset = isrs - 17;
+	}
+	if ((isrs > 36) && (isrs < 77)) {
+	    *psrsPeriodicity = 40;
+	    *psrsOffset = isrs - 37;
+	}
+	if ((isrs > 76) && (isrs < 157)) {
+	    *psrsPeriodicity = 80;
+	    *psrsOffset = isrs - 77;
+	}
+	if ((isrs > 156) && (isrs < 317)) {
+	    *psrsPeriodicity = 160;
+	    *psrsOffset = isrs - 157;
+	}
+	if ((isrs > 316) && (isrs < 637)) {
+	    *psrsPeriodicity = 320;
+	    *psrsOffset = isrs - 317;
+	}
+	AssertFatal(isrs <= 636, "Isrs out of range %d>636\n", isrs);
+    }
 }
 
-int is_UL_sf(COMMON_channels_t *ccP,sub_frame_t subframeP)
+void
+get_csi_params(COMMON_channels_t * cc,
+	       struct CQI_ReportPeriodic *cqi_ReportPeriodic,
+	       uint16_t * Npd, uint16_t * N_OFFSET_CQI, int *H)
 {
-  // if FDD return dummy value
-  if (ccP->tdd_Config == NULL)
-    return(0);
-
-  switch (ccP->tdd_Config->subframeAssignment) {
-  case 1:
-    switch (subframeP) {
-    case 0:
-    case 4:
-    case 5:
-    case 9:
-      return(0);
-      break;
-
-    case 2:
-    case 3:
-    case 7:
-    case 8:
-      return(1);
-      break;
-
-    default:
-      return(0);
-      break;
+    uint16_t cqi_PMI_ConfigIndex =
+	cqi_ReportPeriodic->choice.setup.cqi_pmi_ConfigIndex;
+    uint8_t Jtab[6] = { 0, 2, 2, 3, 4, 4 };
+
+    AssertFatal(cqi_ReportPeriodic != NULL,
+		"cqi_ReportPeriodic is null!\n");
+
+    if (cc->tdd_Config == NULL) {	//FDD
+	if (cqi_PMI_ConfigIndex <= 1) {	// 2 ms CQI_PMI period
+	    *Npd = 2;
+	    *N_OFFSET_CQI = cqi_PMI_ConfigIndex;
+	} else if (cqi_PMI_ConfigIndex <= 6) {	// 5 ms CQI_PMI period
+	    *Npd = 5;
+	    *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 2;
+	} else if (cqi_PMI_ConfigIndex <= 16) {	// 10ms CQI_PMI period
+	    *Npd = 10;
+	    *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 7;
+	} else if (cqi_PMI_ConfigIndex <= 36) {	// 20 ms CQI_PMI period
+	    *Npd = 20;
+	    *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 17;
+	} else if (cqi_PMI_ConfigIndex <= 76) {	// 40 ms CQI_PMI period
+	    *Npd = 40;
+	    *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 37;
+	} else if (cqi_PMI_ConfigIndex <= 156) {	// 80 ms CQI_PMI period
+	    *Npd = 80;
+	    *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 77;
+	} else if (cqi_PMI_ConfigIndex <= 316) {	// 160 ms CQI_PMI period
+	    *Npd = 160;
+	    *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 157;
+	} else if (cqi_PMI_ConfigIndex > 317) {
+
+	    if (cqi_PMI_ConfigIndex <= 349) {	// 32 ms CQI_PMI period
+		*Npd = 32;
+		*N_OFFSET_CQI = cqi_PMI_ConfigIndex - 318;
+	    } else if (cqi_PMI_ConfigIndex <= 413) {	// 64 ms CQI_PMI period
+		*Npd = 64;
+		*N_OFFSET_CQI = cqi_PMI_ConfigIndex - 350;
+	    } else if (cqi_PMI_ConfigIndex <= 541) {	// 128 ms CQI_PMI period
+		*Npd = 128;
+		*N_OFFSET_CQI = cqi_PMI_ConfigIndex - 414;
+	    }
+	}
+    } else {			// TDD
+	if (cqi_PMI_ConfigIndex == 0) {	// all UL subframes
+	    *Npd = 1;
+	    *N_OFFSET_CQI = 0;
+	} else if (cqi_PMI_ConfigIndex <= 6) {	// 5 ms CQI_PMI period
+	    *Npd = 5;
+	    *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 1;
+	} else if (cqi_PMI_ConfigIndex <= 16) {	// 10ms CQI_PMI period
+	    *Npd = 10;
+	    *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 6;
+	} else if (cqi_PMI_ConfigIndex <= 36) {	// 20 ms CQI_PMI period
+	    *Npd = 20;
+	    *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 16;
+	} else if (cqi_PMI_ConfigIndex <= 76) {	// 40 ms CQI_PMI period
+	    *Npd = 40;
+	    *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 36;
+	} else if (cqi_PMI_ConfigIndex <= 156) {	// 80 ms CQI_PMI period
+	    *Npd = 80;
+	    *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 76;
+	} else if (cqi_PMI_ConfigIndex <= 316) {	// 160 ms CQI_PMI period
+	    *Npd = 160;
+	    *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 156;
+	}
     }
-    break;
-
-  case 3:
-    if  ((subframeP<=1) || (subframeP>=5))
-      return(0);
-    else if ((subframeP>1) && (subframeP < 5))
-      return(1);
-    else AssertFatal(1==0,"Unknown subframe number\n");
-    break;
-
-  case 4:
-    if  ((subframeP<=1) || (subframeP>=4))
-      return(0);
-    else if ((subframeP>1) && (subframeP < 4))
-      return(1);
-    else AssertFatal(1==0,"Unknown subframe number\n");
-    break;
-
-  case 5:
-    if  ((subframeP<=1) || (subframeP>=3))
-      return(0);
-    else if ((subframeP>1) && (subframeP < 3))
-      return(1);
-    else AssertFatal(1==0,"Unknown subframe number\n");
-    break;
 
-  default:
-    AssertFatal(1==0,"subframe %d Unsupported TDD configuration %d\n",
-                subframeP,(int)ccP->tdd_Config->subframeAssignment);
-    break;
-  }
+    // get H
+    if (cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.
+	present ==
+	CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_subbandCQI)
+	*H = 1 +
+	    (Jtab[cc->mib->message.dl_Bandwidth] *
+	     cqi_ReportPeriodic->choice.setup.
+	     cqi_FormatIndicatorPeriodic.choice.subbandCQI.k);
+    else
+	*H = 1;
 }
 
-uint16_t get_pucch1_absSF(COMMON_channels_t *cc,uint16_t dlsch_absSF)
+uint8_t
+get_dl_cqi_pmi_size_pusch(COMMON_channels_t * cc, uint8_t tmode,
+			  uint8_t ri,
+			  CQI_ReportModeAperiodic_t *
+			  cqi_ReportModeAperiodic)
 {
-  uint16_t sf,f,nextf;
-
-  if (cc->tdd_Config==NULL) { //FDD n+4
-    return((dlsch_absSF + 4)%10240);
-  }
-  else {
-    sf    = dlsch_absSF%10;
-    f     = dlsch_absSF/10;
-    nextf = (f+1)&1023;
+    int Ntab[6] = { 0, 4, 7, 9, 10, 13 };
+    int N = Ntab[cc->mib->message.dl_Bandwidth];
+    int Ltab_uesel[6] = { 0, 6, 9, 13, 15, 18 };
+    int L = Ltab_uesel[cc->mib->message.dl_Bandwidth];
+
+    AssertFatal(cqi_ReportModeAperiodic != NULL,
+		"cqi_ReportPeriodic is null!\n");
+
+    switch (*cqi_ReportModeAperiodic) {
+    case CQI_ReportModeAperiodic_rm12:
+	AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9
+		    || tmode == 10,
+		    "Illegal TM (%d) for CQI_ReportModeAperiodic_rm12\n",
+		    tmode);
+	AssertFatal(cc->p_eNB <= 4,
+		    "only up to 4 antenna ports supported here\n");
+	if (ri == 1 && cc->p_eNB == 2)
+	    return (4 + (N << 1));
+	else if (ri == 2 && cc->p_eNB == 2)
+	    return (8 + N);
+	else if (ri == 1 && cc->p_eNB == 4)
+	    return (4 + (N << 2));
+	else if (ri > 1 && cc->p_eNB == 4)
+	    return (8 + (N << 2));
+	break;
+    case CQI_ReportModeAperiodic_rm20:
+	// Table 5.2.2.6.3-1 (36.212)
+	AssertFatal(tmode == 1 || tmode == 2 || tmode == 3 || tmode == 7
+		    || tmode == 9
+		    || tmode == 10,
+		    "Illegal TM (%d) for CQI_ReportModeAperiodic_rm20\n",
+		    tmode);
+	AssertFatal(tmode != 9
+		    && tmode != 10,
+		    "TM9/10 will be handled later for CQI_ReportModeAperiodic_rm20\n");
+	return (4 + 2 + L);
+	break;
+    case CQI_ReportModeAperiodic_rm22:
+	// Table 5.2.2.6.3-2 (36.212)
+	AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9
+		    || tmode == 10,
+		    "Illegal TM (%d) for CQI_ReportModeAperiodic_rm22\n",
+		    tmode);
+	AssertFatal(tmode != 9
+		    && tmode != 10,
+		    "TM9/10 will be handled later for CQI_ReportModeAperiodic_rm22\n");
+	if (ri == 1 && cc->p_eNB == 2)
+	    return (4 + 2 + 0 + 0 + L + 4);
+	if (ri == 2 && cc->p_eNB == 2)
+	    return (4 + 2 + 4 + 2 + L + 2);
+	if (ri == 1 && cc->p_eNB == 4)
+	    return (4 + 2 + 0 + 0 + L + 8);
+	if (ri >= 2 && cc->p_eNB == 4)
+	    return (4 + 2 + 4 + 2 + L + 8);
+	break;
+    case CQI_ReportModeAperiodic_rm30:
+	// Table 5.2.2.6.2-1 (36.212)
+	AssertFatal(tmode == 1 || tmode == 2 || tmode == 3 || tmode == 7
+		    || tmode == 8 || tmode == 9
+		    || tmode == 10,
+		    "Illegal TM (%d) for CQI_ReportModeAperiodic_rm30\n",
+		    tmode);
+	AssertFatal(tmode != 8 && tmode != 9
+		    && tmode != 10,
+		    "TM8/9/10 will be handled later for CQI_ReportModeAperiodic_rm30\n");
+	return (4 + (N << 1));
+	break;
+    case CQI_ReportModeAperiodic_rm31:
+	// Table 5.2.2.6.2-2 (36.212)
+	AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9
+		    || tmode == 10,
+		    "Illegal TM (%d) for CQI_ReportModeAperiodic_rm31\n",
+		    tmode);
+	AssertFatal(tmode != 8 && tmode != 9
+		    && tmode != 10,
+		    "TM8/9/10 will be handled later for CQI_ReportModeAperiodic_rm31\n");
+	if (ri == 1 && cc->p_eNB == 2)
+	    return (4 + (N << 1) + 0 + 0 + 2);
+	else if (ri == 2 && cc->p_eNB == 2)
+	    return (4 + (N << 1) + 4 + (N << 1) + 1);
+	else if (ri == 1 && cc->p_eNB == 4)
+	    return (4 + (N << 1) + 0 + 0 + 4);
+	else if (ri >= 2 && cc->p_eNB == 4)
+	    return (4 + (N << 1) + 4 + (N << 1) + 4);
+	break;
+    case CQI_ReportModeAperiodic_rm32_v1250:
+	AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9
+		    || tmode == 10,
+		    "Illegal TM (%d) for CQI_ReportModeAperiodic_rm32\n",
+		    tmode);
+	AssertFatal(1 == 0,
+		    "CQI_ReportModeAperiodic_rm32_v1250 not supported yet\n");
+	break;
+    case CQI_ReportModeAperiodic_rm10_v1310:
+	// Table 5.2.2.6.1-1F/G (36.212)
+	if (ri == 1)
+	    return (4);		// F
+	else
+	    return (7);		// G
+	break;
+    case CQI_ReportModeAperiodic_rm11_v1310:
+	// Table 5.2.2.6.1-1H (36.212)
+	AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9
+		    || tmode == 10,
+		    "Illegal TM (%d) for CQI_ReportModeAperiodic_rm11\n",
+		    tmode);
+	AssertFatal(cc->p_eNB <= 4,
+		    "only up to 4 antenna ports supported here\n");
+	if (ri == 1 && cc->p_eNB == 2)
+	    return (4 + 0 + 2);
+	else if (ri == 2 && cc->p_eNB == 2)
+	    return (4 + 4 + 1);
+	else if (ri == 1 && cc->p_eNB == 4)
+	    return (4 + 0 + 4);
+	else if (ri > 1 && cc->p_eNB == 4)
+	    return (4 + 4 + 4);
+
+	break;
+    }
+    AssertFatal(1 == 0, "Shouldn't get here\n");
+    return (0);
+}
 
-    switch (cc->tdd_Config->subframeAssignment) {
-    case 0:
-      AssertFatal(1==0,"SFA 0 to be filled in now, :-)\n");
-      break;
+uint8_t
+get_rel8_dl_cqi_pmi_size(UE_sched_ctrl * sched_ctl, int CC_idP,
+			 COMMON_channels_t * cc, uint8_t tmode,
+			 struct CQI_ReportPeriodic * cqi_ReportPeriodic)
+{
+    int no_pmi = 0;
+    //    Ltab[6] = {0,log2(15/4/2),log2(25/4/2),log2(50/6/3),log2(75/8/4),log2(100/8/4)};
+
+    uint8_t Ltab[6] = { 0, 1, 2, 2, 2, 2 };
+    uint8_t ri = sched_ctl->periodic_ri_received[CC_idP];
+
+    AssertFatal(cqi_ReportPeriodic != NULL,
+		"cqi_ReportPeriodic is null!\n");
+    AssertFatal(cqi_ReportPeriodic->present !=
+		CQI_ReportPeriodic_PR_NOTHING,
+		"cqi_ReportPeriodic->present == CQI_ReportPeriodic_PR_NOTHING!\n");
+    AssertFatal(cqi_ReportPeriodic->choice.
+		setup.cqi_FormatIndicatorPeriodic.present !=
+		CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING,
+		"cqi_ReportPeriodic->cqi_FormatIndicatorPeriodic.choice.setup.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING!\n");
+
+    switch (tmode) {
     case 1:
-      if      ((sf==5) || (sf==6)) return((10*nextf) + 2);                                        // ACK/NAK in SF 2 next frame
-      else if (sf==9)              return((10*nextf) + 3);                                        // ACK/NAK in SF 3 next frame
-      else if ((sf==0) || (sf==1)) return((10*f) + 2);                                            // ACK/NAK in SF 7 same frame
-      else AssertFatal(1==0,"Impossible dlsch subframe %d for TDD configuration 3\n",sf);
-      break;
     case 2:
-      if      ((sf==4) || (sf==5) || (sf==6) || (sf==8)) return((10*nextf) + 2);                  // ACK/NAK in SF 2 next frame
-      else if (sf==9)                                    return((10*nextf) + 7);                  // ACK/NAK in SF 7 next frame
-      else if ((sf==0) || (sf==1) || (sf==3))            return((10*f) + 7);                      // ACK/NAK in SF 7 same frame
-      else AssertFatal(1==0,"Impossible dlsch subframe %d for TDD configuration 3\n",sf);
-      break;
-    case 3:
-      if      ((sf==5) || (sf==6) || (sf==7) || (sf==8) || (sf==9)) return((10*nextf) + (sf>>1)); // ACK/NAK in 2,3,4 resp. next frame
-      else if (sf==1)                                               return((10*nextf) + 2);       // ACK/NAK in 2 next frame
-      else if (sf==0)                                               return((10*f) + 4);           // ACK/NAK in 4 same frame
-      else AssertFatal(1==0,"Impossible dlsch subframe %d for TDD configuration 3\n",sf);
-      break;
-    case 4:
-      if      ((sf==6) || (sf==7) || (sf==8) || (sf==9)) return(((10*nextf) + 3)%10240);          // ACK/NAK in SF 3 next frame
-      else if ((sf==0) || (sf==1) || (sf==4) || (sf==5)) return(((10*nextf) + 2)%10240);          // ACK/NAK in SF 2 next frame
-      else AssertFatal(1==0,"Impossible dlsch subframe %d for TDD configuration 4\n",sf);
-      break;
     case 5:
-      if      ((sf==0) || (sf==1) || (sf==3) || (sf==4) || (sf==5) || (sf==6) || (sf==7) || (sf==8)) return(((10*nextf) + 2)%10240);     // ACK/NAK in SF 3 next frame
-      else if (sf==9)                                                                                return(((10*(1+nextf)) + 2)%10240); // ACK/NAK in SF 2 next frame
-      else AssertFatal(1==0,"Impossible dlsch subframe %d for TDD configuration 5\n",sf);
-      break;
     case 6:
-      AssertFatal(1==0,"SFA 6 To be filled in now, :-)\n");
-      break;
+    case 7:
+	no_pmi = 1;
+	break;
     default:
-      AssertFatal(1==0,"Illegal TDD subframe Assigment %d\n",(int)cc->tdd_Config->subframeAssignment);
-      break;
-    }
-  }
-  AssertFatal(1==0,"Shouldn't get here\n");
-}
-
-void get_srs_pos(COMMON_channels_t *cc,uint16_t isrs,uint16_t *psrsPeriodicity,uint16_t *psrsOffset)
-{
-  if(cc->tdd_Config) { // TDD
-    AssertFatal(isrs>=10,"2 ms SRS periodicity not supported");
-
-    if ((isrs>9)&&(isrs<15)) {
-      *psrsPeriodicity=5;
-      *psrsOffset=isrs-10;
-    }
-    if ((isrs>14)&&(isrs<25)) {
-      *psrsPeriodicity=10;
-      *psrsOffset=isrs-15;
-    }
-    if ((isrs>24)&&(isrs<45)) {
-      *psrsPeriodicity=20;
-      *psrsOffset=isrs-25;
-    }
-    if ((isrs>44)&&(isrs<85)) {
-      *psrsPeriodicity=40;
-      *psrsOffset=isrs-45;
-    }
-    if ((isrs>84)&&(isrs<165)) {
-      *psrsPeriodicity=80;
-      *psrsOffset=isrs-85;
-    }
-    if ((isrs>164)&&(isrs<325)) {
-      *psrsPeriodicity=160;
-      *psrsOffset=isrs-165;
-    }
-    if ((isrs>324)&&(isrs<645)) {
-      *psrsPeriodicity=320;
-      *psrsOffset=isrs-325;
-    }
-
-    AssertFatal(isrs<=644,"Isrs out of range %d>644\n",isrs);
-  } // TDD
-  else { // FDD
-    if (isrs<2) {
-      *psrsPeriodicity=2;
-      *psrsOffset=isrs;
-    }
-    if ((isrs>1)&&(isrs<7)) {
-      *psrsPeriodicity=5;
-      *psrsOffset=isrs-2;
-    }
-    if ((isrs>6)&&(isrs<17)) {
-      *psrsPeriodicity=10;
-      *psrsOffset=isrs-7;
-    }
-    if ((isrs>16)&&(isrs<37)) {
-      *psrsPeriodicity=20;
-      *psrsOffset=isrs-17;
+	no_pmi = 0;
     }
-    if ((isrs>36)&&(isrs<77)) {
-      *psrsPeriodicity=40;
-      *psrsOffset=isrs-37;
-    }
-    if ((isrs>76)&&(isrs<157)) {
-      *psrsPeriodicity=80;
-      *psrsOffset=isrs-77;
-    }
-    if ((isrs>156)&&(isrs<317)) {
-      *psrsPeriodicity=160;
-      *psrsOffset=isrs-157;
-    }
-    if ((isrs>316)&&(isrs<637)) {
-      *psrsPeriodicity=320;
-      *psrsOffset=isrs-317;
-    }
-    AssertFatal(isrs<=636,"Isrs out of range %d>636\n",isrs);
-  }
-}
-
-void get_csi_params(COMMON_channels_t *cc,struct CQI_ReportPeriodic *cqi_ReportPeriodic,uint16_t *Npd,uint16_t *N_OFFSET_CQI,int *H)
-{
-  uint16_t cqi_PMI_ConfigIndex = cqi_ReportPeriodic->choice.setup.cqi_pmi_ConfigIndex;
-  uint8_t Jtab[6] = {0,2,2,3,4,4};
-
-  AssertFatal(cqi_ReportPeriodic!=NULL,"cqi_ReportPeriodic is null!\n");
-
-  if (cc->tdd_Config==NULL) { //FDD
-    if (cqi_PMI_ConfigIndex <= 1) {        // 2 ms CQI_PMI period
-      *Npd = 2;
-      *N_OFFSET_CQI = cqi_PMI_ConfigIndex;
-    } else if (cqi_PMI_ConfigIndex <= 6) { // 5 ms CQI_PMI period
-      *Npd = 5;
-      *N_OFFSET_CQI = cqi_PMI_ConfigIndex-2;
-    } else if (cqi_PMI_ConfigIndex <=16) { // 10ms CQI_PMI period
-      *Npd = 10;
-      *N_OFFSET_CQI = cqi_PMI_ConfigIndex-7;
-    } else if (cqi_PMI_ConfigIndex <= 36) { // 20 ms CQI_PMI period
-      *Npd = 20;
-      *N_OFFSET_CQI = cqi_PMI_ConfigIndex-17;
-    } else if (cqi_PMI_ConfigIndex <= 76) { // 40 ms CQI_PMI period
-      *Npd = 40;
-      *N_OFFSET_CQI = cqi_PMI_ConfigIndex-37;
-    } else if (cqi_PMI_ConfigIndex <= 156) { // 80 ms CQI_PMI period
-      *Npd = 80;
-      *N_OFFSET_CQI = cqi_PMI_ConfigIndex-77;
-    } else if (cqi_PMI_ConfigIndex <= 316) { // 160 ms CQI_PMI period
-      *Npd = 160;
-      *N_OFFSET_CQI = cqi_PMI_ConfigIndex-157;
-    }
-    else if (cqi_PMI_ConfigIndex > 317) {
 
-      if (cqi_PMI_ConfigIndex <= 349) { // 32 ms CQI_PMI period
-        *Npd = 32;
-      *N_OFFSET_CQI = cqi_PMI_ConfigIndex-318;
-      }
-      else if (cqi_PMI_ConfigIndex <= 413) { // 64 ms CQI_PMI period
-        *Npd = 64;
-        *N_OFFSET_CQI = cqi_PMI_ConfigIndex-350;
-      }
-      else if (cqi_PMI_ConfigIndex <= 541) { // 128 ms CQI_PMI period
-        *Npd = 128;
-        *N_OFFSET_CQI = cqi_PMI_ConfigIndex-414;
-      }
+    if ((cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.
+	 present ==
+	 CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_widebandCQI)
+	|| (sched_ctl->feedback_cnt[CC_idP] == 0)) {
+	// send wideband report every opportunity if wideband reporting mode is selected, else every H opportunities
+	if (no_pmi == 1)
+	    return (4);
+	else if ((cc->p_eNB == 2) && (ri == 1))
+	    return (6);
+	else if ((cc->p_eNB == 2) && (ri == 2))
+	    return (8);
+	else if ((cc->p_eNB == 4) && (ri == 1))
+	    return (8);
+	else if ((cc->p_eNB == 4) && (ri == 2))
+	    return (11);
+	else
+	    AssertFatal(1 == 0,
+			"illegal combination p %d, ri %d, no_pmi %d\n",
+			cc->p_eNB, ri, no_pmi);
+    } else if (cqi_ReportPeriodic->choice.
+	       setup.cqi_FormatIndicatorPeriodic.present ==
+	       CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_subbandCQI)
+    {
+	if ((no_pmi == 1) || ri == 1)
+	    return (4 + Ltab[cc->mib->message.dl_Bandwidth]);
+	else
+	    return (7 + Ltab[cc->mib->message.dl_Bandwidth]);
     }
-  }
-  else { // TDD
-   if (cqi_PMI_ConfigIndex == 0) {        // all UL subframes
-     *Npd = 1;
-     *N_OFFSET_CQI = 0;
-   } else if (cqi_PMI_ConfigIndex <= 6) { // 5 ms CQI_PMI period
-     *Npd = 5;
-     *N_OFFSET_CQI = cqi_PMI_ConfigIndex-1;
-   } else if (cqi_PMI_ConfigIndex <=16) { // 10ms CQI_PMI period
-     *Npd = 10;
-     *N_OFFSET_CQI = cqi_PMI_ConfigIndex-6;
-   } else if (cqi_PMI_ConfigIndex <= 36) { // 20 ms CQI_PMI period
-     *Npd = 20;
-     *N_OFFSET_CQI = cqi_PMI_ConfigIndex-16;
-   } else if (cqi_PMI_ConfigIndex <= 76) { // 40 ms CQI_PMI period
-     *Npd = 40;
-     *N_OFFSET_CQI = cqi_PMI_ConfigIndex-36;
-   } else if (cqi_PMI_ConfigIndex <= 156) { // 80 ms CQI_PMI period
-     *Npd = 80;
-     *N_OFFSET_CQI = cqi_PMI_ConfigIndex-76;
-   } else if (cqi_PMI_ConfigIndex <= 316) { // 160 ms CQI_PMI period
-     *Npd = 160;
-     *N_OFFSET_CQI = cqi_PMI_ConfigIndex-156;
-   }
-  }
-
-  // get H
-  if (cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_subbandCQI)
-    *H = 1+(Jtab[cc->mib->message.dl_Bandwidth]*cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.choice.subbandCQI.k);
-  else
-    *H=1;
+    AssertFatal(1 == 0,
+		"Shouldn't get here : cqi_ReportPeriodic->present %d\n",
+		cqi_ReportPeriodic->choice.
+		setup.cqi_FormatIndicatorPeriodic.present);
 }
 
-uint8_t get_dl_cqi_pmi_size_pusch(COMMON_channels_t *cc,uint8_t tmode,uint8_t ri, CQI_ReportModeAperiodic_t *cqi_ReportModeAperiodic)
+void
+fill_nfapi_dl_dci_1A(nfapi_dl_config_request_pdu_t * dl_config_pdu,
+		     uint8_t aggregation_level,
+		     uint16_t rnti,
+		     uint8_t rnti_type,
+		     uint8_t harq_process,
+		     uint8_t tpc,
+		     uint16_t resource_block_coding,
+		     uint8_t mcs, uint8_t ndi, uint8_t rv,
+		     uint8_t vrb_flag)
 {
-  int Ntab[6]       = {0,4,7,9,10,13};
-  int N             = Ntab[cc->mib->message.dl_Bandwidth];
-  int Ltab_uesel[6] = {0,6,9,13,15,18};
-  int L             = Ltab_uesel[cc->mib->message.dl_Bandwidth];
-
-  AssertFatal(cqi_ReportModeAperiodic != NULL,"cqi_ReportPeriodic is null!\n");
-
-  switch (*cqi_ReportModeAperiodic) {
-  case CQI_ReportModeAperiodic_rm12:
-    AssertFatal(tmode==4 || tmode==6 || tmode==8 || tmode==9 || tmode==10,"Illegal TM (%d) for CQI_ReportModeAperiodic_rm12\n",tmode);
-    AssertFatal(cc->p_eNB<=4,"only up to 4 antenna ports supported here\n");
-    if (ri==1 && cc->p_eNB==2) return(4+(N<<1));
-    else if (ri==2 && cc->p_eNB==2) return(8+N);
-    else if (ri==1 && cc->p_eNB==4) return(4+(N<<2));
-    else if (ri>1  && cc->p_eNB==4) return(8+(N<<2));
-    break;
-  case CQI_ReportModeAperiodic_rm20:
-    // Table 5.2.2.6.3-1 (36.212)
-    AssertFatal(tmode==1 || tmode==2 || tmode==3 || tmode==7 || tmode==9 || tmode==10,"Illegal TM (%d) for CQI_ReportModeAperiodic_rm20\n",tmode);
-    AssertFatal(tmode!=9 && tmode!=10,"TM9/10 will be handled later for CQI_ReportModeAperiodic_rm20\n");
-    return(4+2+L);
-    break;
-  case CQI_ReportModeAperiodic_rm22:
-    // Table 5.2.2.6.3-2 (36.212)
-    AssertFatal(tmode==4 || tmode==6 || tmode==8 || tmode==9 || tmode==10,"Illegal TM (%d) for CQI_ReportModeAperiodic_rm22\n",tmode);
-    AssertFatal(tmode!=9 && tmode!=10,"TM9/10 will be handled later for CQI_ReportModeAperiodic_rm22\n");
-    if (ri==1 && cc->p_eNB==2) return(4+2+0+0+L+4);
-    if (ri==2 && cc->p_eNB==2) return(4+2+4+2+L+2);
-    if (ri==1 && cc->p_eNB==4) return(4+2+0+0+L+8);
-    if (ri>=2 && cc->p_eNB==4) return(4+2+4+2+L+8);
-    break;
-  case CQI_ReportModeAperiodic_rm30:
-    // Table 5.2.2.6.2-1 (36.212)
-    AssertFatal(tmode==1 || tmode==2 || tmode==3 || tmode==7 || tmode==8 || tmode==9 || tmode==10,"Illegal TM (%d) for CQI_ReportModeAperiodic_rm30\n",tmode);
-    AssertFatal(tmode!=8 && tmode!=9 && tmode!=10,"TM8/9/10 will be handled later for CQI_ReportModeAperiodic_rm30\n");
-    return(4+(N<<1));
-    break;
-  case CQI_ReportModeAperiodic_rm31:
-    // Table 5.2.2.6.2-2 (36.212)
-    AssertFatal(tmode==4 || tmode==6 || tmode==8 || tmode==9 || tmode==10,"Illegal TM (%d) for CQI_ReportModeAperiodic_rm31\n",tmode);
-    AssertFatal(tmode!=8 && tmode!=9 && tmode!=10,"TM8/9/10 will be handled later for CQI_ReportModeAperiodic_rm31\n");
-    if (ri==1 && cc->p_eNB==2) return(4+(N<<1)+0+0+2);
-    else if (ri==2 && cc->p_eNB==2) return(4+(N<<1)+4+(N<<1)+1);
-    else if (ri==1 && cc->p_eNB==4) return(4+(N<<1)+0+0+4);
-    else if (ri>=2 && cc->p_eNB==4) return(4+(N<<1)+4+(N<<1)+4);
-    break;
-  case CQI_ReportModeAperiodic_rm32_v1250:
-    AssertFatal(tmode==4 || tmode==6 || tmode==8 || tmode==9 || tmode==10,"Illegal TM (%d) for CQI_ReportModeAperiodic_rm32\n",tmode);
-    AssertFatal(1==0,"CQI_ReportModeAperiodic_rm32_v1250 not supported yet\n");
-    break;
-  case CQI_ReportModeAperiodic_rm10_v1310:
-    // Table 5.2.2.6.1-1F/G (36.212)
-    if (ri==1) return(4); // F
-    else return(7); // G
-    break;
-  case CQI_ReportModeAperiodic_rm11_v1310:
-    // Table 5.2.2.6.1-1H (36.212)
-    AssertFatal(tmode==4 || tmode==6 || tmode==8 || tmode==9 || tmode==10,"Illegal TM (%d) for CQI_ReportModeAperiodic_rm11\n",tmode);
-    AssertFatal(cc->p_eNB<=4,"only up to 4 antenna ports supported here\n");
-    if (ri==1 && cc->p_eNB==2) return(4+0+2);
-    else if (ri==2 && cc->p_eNB==2) return(4+4+1);
-    else if (ri==1 && cc->p_eNB==4) return(4+0+4);
-    else if (ri>1  && cc->p_eNB==4) return(4+4+4);
-
-    break;
-  }
-  AssertFatal(1==0,"Shouldn't get here\n");
-  return(0);
+    memset((void *) dl_config_pdu, 0,
+	   sizeof(nfapi_dl_config_request_pdu_t));
+    dl_config_pdu->pdu_type = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
+    dl_config_pdu->pdu_size =
+	(uint8_t) (2 + sizeof(nfapi_dl_config_dci_dl_pdu));
+    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format =
+	NFAPI_DL_DCI_FORMAT_1A;
+    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level =
+	aggregation_level;
+    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti = rnti;
+    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type = rnti_type;
+    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power = 6000;	// equal to RS power
+    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.harq_process = harq_process;
+    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tpc = tpc;	// no TPC
+    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_block_coding =
+	resource_block_coding;
+    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.mcs_1 = mcs;
+    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.new_data_indicator_1 = ndi;
+    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.redundancy_version_1 = rv;
+    dl_config_pdu->dci_dl_pdu.
+	dci_dl_pdu_rel8.virtual_resource_block_assignment_flag = vrb_flag;
 }
 
-uint8_t get_rel8_dl_cqi_pmi_size(UE_sched_ctrl *sched_ctl,int CC_idP,COMMON_channels_t *cc,uint8_t tmode, struct CQI_ReportPeriodic *cqi_ReportPeriodic)
+void
+program_dlsch_acknak(module_id_t module_idP, int CC_idP, int UE_idP,
+		     frame_t frameP, sub_frame_t subframeP,
+		     uint8_t cce_idx)
 {
-  int no_pmi=0;
-  //    Ltab[6] = {0,log2(15/4/2),log2(25/4/2),log2(50/6/3),log2(75/8/4),log2(100/8/4)};
-
-  uint8_t Ltab[6] = {0,1,2,2,2,2};
-  uint8_t ri = sched_ctl->periodic_ri_received[CC_idP];
-
-  AssertFatal(cqi_ReportPeriodic != NULL,"cqi_ReportPeriodic is null!\n");
-  AssertFatal(cqi_ReportPeriodic->present != CQI_ReportPeriodic_PR_NOTHING,
-              "cqi_ReportPeriodic->present == CQI_ReportPeriodic_PR_NOTHING!\n");
-  AssertFatal(cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present != CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING,
-              "cqi_ReportPeriodic->cqi_FormatIndicatorPeriodic.choice.setup.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING!\n");
-
-  switch(tmode) {
-  case 1:
-  case 2:
-  case 5:
-  case 6:
-  case 7:
-    no_pmi=1;
-    break;
-  default:
-    no_pmi=0;
-  }
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    COMMON_channels_t *cc = eNB->common_channels;
+    UE_list_t *UE_list = &eNB->UE_list;
+    rnti_t rnti = UE_RNTI(module_idP, UE_idP);
+    nfapi_ul_config_request_body_t *ul_req;
+    nfapi_ul_config_request_pdu_t *ul_config_pdu;
 
-  if ((cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_widebandCQI) ||
-      (sched_ctl->feedback_cnt[CC_idP] == 0)) {
-    // send wideband report every opportunity if wideband reporting mode is selected, else every H opportunities
-    if (no_pmi == 1)
-      return(4);
-    else if ((cc->p_eNB==2) && (ri==1)) return(6);
-    else if ((cc->p_eNB==2) && (ri==2)) return(8);
-    else if ((cc->p_eNB==4) && (ri==1)) return(8);
-    else if ((cc->p_eNB==4) && (ri==2)) return(11);
-    else AssertFatal(1==0,"illegal combination p %d, ri %d, no_pmi %d\n",cc->p_eNB,ri,no_pmi);
-  }
-  else if (cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_subbandCQI) {
-    if ((no_pmi == 1)||ri==1) return(4+Ltab[cc->mib->message.dl_Bandwidth]);
-    else
-      return(7+Ltab[cc->mib->message.dl_Bandwidth]);
-  }
-  AssertFatal(1==0,"Shouldn't get here : cqi_ReportPeriodic->present %d\n",cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present);
-}
-
-void fill_nfapi_dl_dci_1A(nfapi_dl_config_request_pdu_t   *dl_config_pdu,
-                          uint8_t aggregation_level,
-                          uint16_t rnti,
-                          uint8_t rnti_type,
-                          uint8_t harq_process,
-                          uint8_t tpc,
-                          uint16_t resource_block_coding,
-                          uint8_t mcs,
-                          uint8_t ndi,
-                          uint8_t rv,
-                          uint8_t vrb_flag)
-{
-  memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-  dl_config_pdu->pdu_type                                                          = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
-  dl_config_pdu->pdu_size                                                          = (uint8_t)(2+sizeof(nfapi_dl_config_dci_dl_pdu));
-  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format                             = NFAPI_DL_DCI_FORMAT_1A;
-  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level                      = aggregation_level;
-  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti                                   = rnti;
-  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type                              = rnti_type;
-  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power                     = 6000; // equal to RS power
-  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.harq_process                           = harq_process;
-  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tpc                                    = tpc; // no TPC
-  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_block_coding                  = resource_block_coding;
-  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.mcs_1                                  = mcs;
-  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.new_data_indicator_1                   = ndi;
-  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.redundancy_version_1                   = rv;
-  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.virtual_resource_block_assignment_flag = vrb_flag;
-}
-
-void program_dlsch_acknak(module_id_t module_idP, int CC_idP,int UE_idP, frame_t frameP, sub_frame_t subframeP,uint8_t cce_idx)
-{
-  eNB_MAC_INST                   *eNB      = RC.mac[module_idP];
-  COMMON_channels_t              *cc       = eNB->common_channels;
-  UE_list_t                      *UE_list  = &eNB->UE_list;
-  rnti_t                         rnti      = UE_RNTI(module_idP,UE_idP);
-  nfapi_ul_config_request_body_t *ul_req;
-  nfapi_ul_config_request_pdu_t  *ul_config_pdu;
-
-  int use_simultaneous_pucch_pusch=0;
-  nfapi_ul_config_ulsch_harq_information *ulsch_harq_information = NULL;
-  nfapi_ul_config_harq_information *harq_information = NULL;
+    int use_simultaneous_pucch_pusch = 0;
+    nfapi_ul_config_ulsch_harq_information *ulsch_harq_information = NULL;
+    nfapi_ul_config_harq_information *harq_information = NULL;
 
 #if defined(Rel10) || defined(Rel14)
 
-  if ((UE_list->UE_template[CC_idP][UE_idP].physicalConfigDedicated->ext2) &&
-      (UE_list->UE_template[CC_idP][UE_idP].physicalConfigDedicated->ext2->pucch_ConfigDedicated_v1020) &&
-      (UE_list->UE_template[CC_idP][UE_idP].physicalConfigDedicated->ext2->pucch_ConfigDedicated_v1020->simultaneousPUCCH_PUSCH_r10) &&
-      (*UE_list->UE_template[CC_idP][UE_idP].physicalConfigDedicated->ext2->pucch_ConfigDedicated_v1020->simultaneousPUCCH_PUSCH_r10 ==
-          PUCCH_ConfigDedicated_v1020__simultaneousPUCCH_PUSCH_r10_true))
-    use_simultaneous_pucch_pusch=1;
+    if ((UE_list->UE_template[CC_idP][UE_idP].physicalConfigDedicated->
+	 ext2)
+	&& (UE_list->UE_template[CC_idP][UE_idP].
+	    physicalConfigDedicated->ext2->pucch_ConfigDedicated_v1020)
+	&& (UE_list->UE_template[CC_idP][UE_idP].
+	    physicalConfigDedicated->ext2->pucch_ConfigDedicated_v1020->
+	    simultaneousPUCCH_PUSCH_r10)
+	&& (*UE_list->UE_template[CC_idP][UE_idP].
+	    physicalConfigDedicated->ext2->pucch_ConfigDedicated_v1020->
+	    simultaneousPUCCH_PUSCH_r10 ==
+	    PUCCH_ConfigDedicated_v1020__simultaneousPUCCH_PUSCH_r10_true))
+	use_simultaneous_pucch_pusch = 1;
 #endif
 
-  // pucch1 and pusch feedback is similar, namely in n+k subframes from now
-  // This is used in the following "if/else" condition to check if there isn't or is already an UL grant in n+k
-  int16_t ul_absSF = get_pucch1_absSF(&cc[CC_idP],subframeP+(10*frameP));
-
-  if ((ul_config_pdu = has_ul_grant(module_idP,CC_idP,
-                                    ul_absSF,
-                                    rnti)) == NULL) {
-    // no UL grant so
-    // Program ACK/NAK alone Format 1a/b or 3
-
-    ul_req        = &RC.mac[module_idP]->UL_req_tmp[CC_idP][ul_absSF%10].ul_config_request_body;
-    ul_config_pdu = &ul_req->ul_config_pdu_list[ul_req->number_of_pdus];
-    // Do PUCCH
-    fill_nfapi_uci_acknak(module_idP,
-                          CC_idP,
-                          rnti,
-                          (frameP*10)+subframeP,
-                          cce_idx);
-  }
-  else {
-    /* there is already an existing UL grant so update it if needed
-     * on top of some other UL resource (PUSCH,combined SR/CQI/HARQ on PUCCH, etc)
-     */
-    switch(ul_config_pdu->pdu_type) {
-
-    /* [ulsch] to [ulsch + harq] or [ulsch + harq on pucch] */
-
-    case NFAPI_UL_CONFIG_ULSCH_PDU_TYPE:
-      if (use_simultaneous_pucch_pusch==1) {
-        // Convert it to an NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE
-        harq_information = &ul_config_pdu->ulsch_uci_harq_pdu.harq_information;
-        ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE;
-        LOG_D(MAC,"Frame %d, Subframe %d: Switched UCI HARQ to ULSCH UCI HARQ\n",frameP,subframeP);
-      }
-      else {
-        // Convert it to an NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE
-        ulsch_harq_information = &ul_config_pdu->ulsch_harq_pdu.harq_information;
-        ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE;
-        ul_config_pdu->ulsch_harq_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.n_srs_initial=0; // last symbol not punctured
-        ul_config_pdu->ulsch_harq_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.initial_number_of_resource_blocks=
-          ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks; // we don't change the number of resource blocks across retransmissions yet
-        LOG_D(MAC,"Frame %d, Subframe %d: Switched UCI HARQ to ULSCH HARQ\n",frameP,subframeP);
-      }
-      break;
-    case NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE:
-      AssertFatal(use_simultaneous_pucch_pusch == 1,
-                  "Cannot be NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE, simultaneous_pucch_pusch is active");
-      break;
-    case NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE:
-      AssertFatal(use_simultaneous_pucch_pusch == 0,
-                  "Cannot be NFAPI_UL_CONFIG_ULSCH_UCI_PDU_TYPE, simultaneous_pucch_pusch is inactive\n");
-      break;
-
-    /* [ulsch + cqi] to [ulsch + cqi + harq] */
-
-    case NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE:
-      // Convert it to an NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE
-      ulsch_harq_information = &ul_config_pdu->ulsch_cqi_harq_ri_pdu.harq_information;
-      ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE;
-      /* TODO: check this - when converting from nfapi_ul_config_ulsch_cqi_ri_pdu to
-       * nfapi_ul_config_ulsch_cqi_harq_ri_pdu, shouldn't we copy initial_transmission_parameters
-       * from the one to the other?
-       * Those two types are not compatible. 'initial_transmission_parameters' is not at the
-       * place in both.
-       */
-      ul_config_pdu->ulsch_cqi_harq_ri_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.n_srs_initial=0; // last symbol not punctured
-      ul_config_pdu->ulsch_cqi_harq_ri_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.initial_number_of_resource_blocks=
-        ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks; // we don't change the number of resource blocks across retransmissions yet
-      break;
-    case NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE:
-      AssertFatal(use_simultaneous_pucch_pusch == 0,
-                  "Cannot be NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE, simultaneous_pucch_pusch is active\n");
-      break;
-
-    /* [ulsch + cqi on pucch] to [ulsch + cqi on pucch + harq on pucch] */
-
-    case NFAPI_UL_CONFIG_ULSCH_UCI_CSI_PDU_TYPE:
-      // convert it to an NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE
-      harq_information = &ul_config_pdu->ulsch_csi_uci_harq_pdu.harq_information;
-      ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE;
-      break;
-    case NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE:
-      AssertFatal(use_simultaneous_pucch_pusch == 1,
-                  "Cannot be NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE, simultaneous_pucch_pusch is inactive\n");
-      break;
-
-    /* [sr] to [sr + harq] */
-
-    case NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE:
-      // convert to NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE
-      ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE;
-      harq_information = &ul_config_pdu->uci_sr_harq_pdu.harq_information;
-      break;
-    case NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE:
-      /* nothing to do */
-      break;
-
-    /* [cqi] to [cqi + harq] */
-
-    case NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE:
-      // convert to NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE
-      ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE;
-      harq_information = &ul_config_pdu->uci_cqi_harq_pdu.harq_information;
-      break;
-    case NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE:
-      /* nothing to do */
-      break;
-
-    /* [cqi + sr] to [cqr + sr + harq] */
-
-    case NFAPI_UL_CONFIG_UCI_CQI_SR_PDU_TYPE:
-      // convert to NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE
-      ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE;
-      harq_information = &ul_config_pdu->uci_cqi_sr_harq_pdu.harq_information;
-      break;
-    case NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE:
-      /* nothing to do */
-      break;
+    // pucch1 and pusch feedback is similar, namely in n+k subframes from now
+    // This is used in the following "if/else" condition to check if there isn't or is already an UL grant in n+k
+    int16_t ul_absSF =
+	get_pucch1_absSF(&cc[CC_idP], subframeP + (10 * frameP));
+
+    if ((ul_config_pdu = has_ul_grant(module_idP, CC_idP,
+				      ul_absSF, rnti)) == NULL) {
+	// no UL grant so
+	// Program ACK/NAK alone Format 1a/b or 3
+
+	ul_req =
+	    &RC.mac[module_idP]->UL_req_tmp[CC_idP][ul_absSF %
+						    10].
+	    ul_config_request_body;
+	ul_config_pdu =
+	    &ul_req->ul_config_pdu_list[ul_req->number_of_pdus];
+	// Do PUCCH
+	fill_nfapi_uci_acknak(module_idP,
+			      CC_idP,
+			      rnti, (frameP * 10) + subframeP, cce_idx);
+    } else {
+	/* there is already an existing UL grant so update it if needed
+	 * on top of some other UL resource (PUSCH,combined SR/CQI/HARQ on PUCCH, etc)
+	 */
+	switch (ul_config_pdu->pdu_type) {
+
+	    /* [ulsch] to [ulsch + harq] or [ulsch + harq on pucch] */
+
+	case NFAPI_UL_CONFIG_ULSCH_PDU_TYPE:
+	    if (use_simultaneous_pucch_pusch == 1) {
+		// Convert it to an NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE
+		harq_information =
+		    &ul_config_pdu->ulsch_uci_harq_pdu.harq_information;
+		ul_config_pdu->pdu_type =
+		    NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE;
+		LOG_D(MAC,
+		      "Frame %d, Subframe %d: Switched UCI HARQ to ULSCH UCI HARQ\n",
+		      frameP, subframeP);
+	    } else {
+		// Convert it to an NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE
+		ulsch_harq_information =
+		    &ul_config_pdu->ulsch_harq_pdu.harq_information;
+		ul_config_pdu->pdu_type =
+		    NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE;
+		ul_config_pdu->ulsch_harq_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.n_srs_initial = 0;	// last symbol not punctured
+		ul_config_pdu->ulsch_harq_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.initial_number_of_resource_blocks = ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks;	// we don't change the number of resource blocks across retransmissions yet
+		LOG_D(MAC,
+		      "Frame %d, Subframe %d: Switched UCI HARQ to ULSCH HARQ\n",
+		      frameP, subframeP);
+	    }
+	    break;
+	case NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE:
+	    AssertFatal(use_simultaneous_pucch_pusch == 1,
+			"Cannot be NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE, simultaneous_pucch_pusch is active");
+	    break;
+	case NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE:
+	    AssertFatal(use_simultaneous_pucch_pusch == 0,
+			"Cannot be NFAPI_UL_CONFIG_ULSCH_UCI_PDU_TYPE, simultaneous_pucch_pusch is inactive\n");
+	    break;
+
+	    /* [ulsch + cqi] to [ulsch + cqi + harq] */
+
+	case NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE:
+	    // Convert it to an NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE
+	    ulsch_harq_information =
+		&ul_config_pdu->ulsch_cqi_harq_ri_pdu.harq_information;
+	    ul_config_pdu->pdu_type =
+		NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE;
+	    /* TODO: check this - when converting from nfapi_ul_config_ulsch_cqi_ri_pdu to
+	     * nfapi_ul_config_ulsch_cqi_harq_ri_pdu, shouldn't we copy initial_transmission_parameters
+	     * from the one to the other?
+	     * Those two types are not compatible. 'initial_transmission_parameters' is not at the
+	     * place in both.
+	     */
+	    ul_config_pdu->ulsch_cqi_harq_ri_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.n_srs_initial = 0;	// last symbol not punctured
+	    ul_config_pdu->ulsch_cqi_harq_ri_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.initial_number_of_resource_blocks = ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks;	// we don't change the number of resource blocks across retransmissions yet
+	    break;
+	case NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE:
+	    AssertFatal(use_simultaneous_pucch_pusch == 0,
+			"Cannot be NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE, simultaneous_pucch_pusch is active\n");
+	    break;
+
+	    /* [ulsch + cqi on pucch] to [ulsch + cqi on pucch + harq on pucch] */
+
+	case NFAPI_UL_CONFIG_ULSCH_UCI_CSI_PDU_TYPE:
+	    // convert it to an NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE
+	    harq_information =
+		&ul_config_pdu->ulsch_csi_uci_harq_pdu.harq_information;
+	    ul_config_pdu->pdu_type =
+		NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE;
+	    break;
+	case NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE:
+	    AssertFatal(use_simultaneous_pucch_pusch == 1,
+			"Cannot be NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE, simultaneous_pucch_pusch is inactive\n");
+	    break;
+
+	    /* [sr] to [sr + harq] */
+
+	case NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE:
+	    // convert to NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE
+	    ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE;
+	    harq_information =
+		&ul_config_pdu->uci_sr_harq_pdu.harq_information;
+	    break;
+	case NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE:
+	    /* nothing to do */
+	    break;
+
+	    /* [cqi] to [cqi + harq] */
+
+	case NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE:
+	    // convert to NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE
+	    ul_config_pdu->pdu_type =
+		NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE;
+	    harq_information =
+		&ul_config_pdu->uci_cqi_harq_pdu.harq_information;
+	    break;
+	case NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE:
+	    /* nothing to do */
+	    break;
+
+	    /* [cqi + sr] to [cqr + sr + harq] */
+
+	case NFAPI_UL_CONFIG_UCI_CQI_SR_PDU_TYPE:
+	    // convert to NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE
+	    ul_config_pdu->pdu_type =
+		NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE;
+	    harq_information =
+		&ul_config_pdu->uci_cqi_sr_harq_pdu.harq_information;
+	    break;
+	case NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE:
+	    /* nothing to do */
+	    break;
+	}
     }
-  }
 
-  if (ulsch_harq_information) fill_nfapi_ulsch_harq_information(module_idP,CC_idP,
-                                                                rnti,
-                                                                ulsch_harq_information);
+    if (ulsch_harq_information)
+	fill_nfapi_ulsch_harq_information(module_idP, CC_idP,
+					  rnti, ulsch_harq_information);
 
-  if (harq_information) fill_nfapi_harq_information(module_idP,CC_idP,
-                                                    rnti,
-                                                    (frameP*10)+subframeP,
-                                                    harq_information,
-                                                    cce_idx);
+    if (harq_information)
+	fill_nfapi_harq_information(module_idP, CC_idP,
+				    rnti,
+				    (frameP * 10) + subframeP,
+				    harq_information, cce_idx);
 }
 
-uint8_t get_V_UL_DAI(module_id_t module_idP,int CC_idP,uint16_t rntiP)
+uint8_t get_V_UL_DAI(module_id_t module_idP, int CC_idP, uint16_t rntiP)
 {
-  nfapi_hi_dci0_request_body_t        *HI_DCI0_req         = &RC.mac[module_idP]->HI_DCI0_req[CC_idP].hi_dci0_request_body;
-  nfapi_hi_dci0_request_pdu_t         *hi_dci0_pdu         = &HI_DCI0_req->hi_dci0_pdu_list[0];
+    nfapi_hi_dci0_request_body_t *HI_DCI0_req =
+	&RC.mac[module_idP]->HI_DCI0_req[CC_idP].hi_dci0_request_body;
+    nfapi_hi_dci0_request_pdu_t *hi_dci0_pdu =
+	&HI_DCI0_req->hi_dci0_pdu_list[0];
+
+    for (int i = 0; i < HI_DCI0_req->number_of_dci; i++) {
+	if ((hi_dci0_pdu[i].pdu_type == NFAPI_HI_DCI0_DCI_PDU_TYPE) &&
+	    (hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.rnti == rntiP))
+	    return (hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.
+		    dl_assignment_index);
+    }
+    return (4);			// this is rule from Section 7.3 in 36.213
+}
 
-  for (int i=0;i<HI_DCI0_req->number_of_dci;i++) {
-    if ((hi_dci0_pdu[i].pdu_type == NFAPI_HI_DCI0_DCI_PDU_TYPE)&&
-        (hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.rnti == rntiP))
-        return(hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.dl_assignment_index);
-  }
-  return(4); // this is rule from Section 7.3 in 36.213
+void
+fill_nfapi_ulsch_harq_information(module_id_t module_idP,
+				  int CC_idP,
+				  uint16_t rntiP,
+				  nfapi_ul_config_ulsch_harq_information
+				  * harq_information)
+{
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    COMMON_channels_t *cc = &eNB->common_channels[CC_idP];
+    UE_list_t *UE_list = &eNB->UE_list;
+
+    int UE_id = find_UE_id(module_idP, rntiP);
+
+    PUSCH_ConfigDedicated_t *puschConfigDedicated;
+    //  PUSCH_ConfigDedicated_v1020_t        *puschConfigDedicated_v1020;
+    //  PUSCH_ConfigDedicated_v1130_t        *puschConfigDedicated_v1130;
+    //  PUSCH_ConfigDedicated_v1250_t        *puschConfigDedicated_v1250;
+
+    AssertFatal(UE_id >= 0, "UE_id cannot be found, impossible\n");
+    AssertFatal(UE_list != NULL, "UE_list is null\n");
+    AssertFatal(UE_list->UE_template[CC_idP][UE_id].
+		physicalConfigDedicated != NULL,
+		"physicalConfigDedicated for rnti %x is null\n", rntiP);
+    AssertFatal((puschConfigDedicated = (PUSCH_ConfigDedicated_t *)
+		 UE_list->UE_template[CC_idP][UE_id].
+		 physicalConfigDedicated->pusch_ConfigDedicated) != NULL,
+		"physicalConfigDedicated->puschConfigDedicated for rnti %x is null\n",
+		rntiP);
+#if defined(Rel14) || defined(Rel14)
+    /*  if (UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext2) puschConfigDedicated_v1020 =  UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext2->pusch_ConfigDedicated_v1020;
+       #endif
+       #ifdef Rel14
+       if (UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext4) puschConfigDedicated_v1130 =  UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext4->pusch_ConfigDedicated_v1130;
+       if (UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext5) puschConfigDedicated_v1250 =  UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext5->pusch_ConfigDedicated_v1250;
+     */
+#endif
+    harq_information->harq_information_rel10.delta_offset_harq =
+	puschConfigDedicated->betaOffset_ACK_Index;
+    AssertFatal(UE_list->
+		UE_template[CC_idP][UE_id].physicalConfigDedicated->
+		pucch_ConfigDedicated != NULL,
+		"pucch_ConfigDedicated is null!\n");
+    if ((UE_list->UE_template[CC_idP][UE_id].
+	 physicalConfigDedicated->pucch_ConfigDedicated->
+	 tdd_AckNackFeedbackMode != NULL)
+	&& (*UE_list->UE_template[CC_idP][UE_id].
+	    physicalConfigDedicated->pucch_ConfigDedicated->
+	    tdd_AckNackFeedbackMode ==
+	    PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing))
+	harq_information->harq_information_rel10.ack_nack_mode = 1;	// multiplexing
+    else
+	harq_information->harq_information_rel10.ack_nack_mode = 0;	// bundling
+
+    switch (get_tmode(module_idP, CC_idP, UE_id)) {
+    case 1:
+    case 2:
+    case 5:
+    case 6:
+    case 7:
+	if (cc->tdd_Config == NULL)	// FDD
+	    harq_information->harq_information_rel10.harq_size = 1;
+	else {
+	    if (harq_information->harq_information_rel10.ack_nack_mode ==
+		1)
+		harq_information->harq_information_rel10.harq_size =
+		    get_V_UL_DAI(module_idP, CC_idP, rntiP);
+	    else
+		harq_information->harq_information_rel10.harq_size = 1;
+	}
+	break;
+    default:			// for any other TM we need 2 bits harq
+	if (cc->tdd_Config == NULL) {
+	    harq_information->harq_information_rel10.harq_size = 2;
+	} else {
+	    if (harq_information->harq_information_rel10.ack_nack_mode ==
+		1)
+		harq_information->harq_information_rel10.harq_size =
+		    get_V_UL_DAI(module_idP, CC_idP, rntiP);
+	    else
+		harq_information->harq_information_rel10.harq_size = 2;
+	}
+	break;
+    }				// get Tmode
 }
 
-void fill_nfapi_ulsch_harq_information(module_id_t module_idP,
-                                       int CC_idP,
-                                       uint16_t rntiP,
-                                       nfapi_ul_config_ulsch_harq_information *harq_information)
+void
+fill_nfapi_harq_information(module_id_t module_idP,
+			    int CC_idP,
+			    uint16_t rntiP,
+			    uint16_t absSFP,
+			    nfapi_ul_config_harq_information *
+			    harq_information, uint8_t cce_idxP)
 {
-  eNB_MAC_INST                   *eNB      = RC.mac[module_idP];
-  COMMON_channels_t              *cc       = &eNB->common_channels[CC_idP];
-  UE_list_t                      *UE_list  = &eNB->UE_list;
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    COMMON_channels_t *cc = &eNB->common_channels[CC_idP];
+    UE_list_t *UE_list = &eNB->UE_list;
 
-  int UE_id                                = find_UE_id(module_idP,rntiP);
+    int UE_id = find_UE_id(module_idP, rntiP);
 
-  PUSCH_ConfigDedicated_t              *puschConfigDedicated;
-  //  PUSCH_ConfigDedicated_v1020_t        *puschConfigDedicated_v1020;
-  //  PUSCH_ConfigDedicated_v1130_t        *puschConfigDedicated_v1130;
-  //  PUSCH_ConfigDedicated_v1250_t        *puschConfigDedicated_v1250;
+    AssertFatal(UE_id >= 0, "UE_id cannot be found, impossible\n");
+    AssertFatal(UE_list != NULL, "UE_list is null\n");
+    AssertFatal(UE_list->UE_template[CC_idP][UE_id].
+		physicalConfigDedicated != NULL,
+		"physicalConfigDedicated for rnti %x is null\n", rntiP);
 
-  AssertFatal(UE_id>=0,"UE_id cannot be found, impossible\n");
-  AssertFatal(UE_list!=NULL,"UE_list is null\n");
-  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated!=NULL,"physicalConfigDedicated for rnti %x is null\n",rntiP);
-  AssertFatal((puschConfigDedicated = (PUSCH_ConfigDedicated_t *)UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pusch_ConfigDedicated)!=NULL,"physicalConfigDedicated->puschConfigDedicated for rnti %x is null\n",rntiP);
-#if defined(Rel14) || defined(Rel14)
-  /*  if (UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext2) puschConfigDedicated_v1020 =  UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext2->pusch_ConfigDedicated_v1020;
-#endif
-#ifdef Rel14
-  if (UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext4) puschConfigDedicated_v1130 =  UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext4->pusch_ConfigDedicated_v1130;
-  if (UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext5) puschConfigDedicated_v1250 =  UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext5->pusch_ConfigDedicated_v1250;
-  */
-#endif
-  harq_information->harq_information_rel10.delta_offset_harq = puschConfigDedicated->betaOffset_ACK_Index;
-  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated!=NULL,"pucch_ConfigDedicated is null!\n");
-  if ((UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode!=NULL)&&
-      (*UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode==PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing))
-      harq_information->harq_information_rel10.ack_nack_mode = 1;  // multiplexing
-  else
-      harq_information->harq_information_rel10.ack_nack_mode = 0;  // bundling
-
-  switch(get_tmode(module_idP,CC_idP,UE_id)) {
-  case 1:
-  case 2:
-  case 5:
-  case 6:
-  case 7:
-    if (cc->tdd_Config==NULL) // FDD
-      harq_information->harq_information_rel10.harq_size = 1;
-    else {
-      if (harq_information->harq_information_rel10.ack_nack_mode == 1)
-        harq_information->harq_information_rel10.harq_size = get_V_UL_DAI(module_idP,CC_idP,rntiP);
-      else
-        harq_information->harq_information_rel10.harq_size = 1;
-    }
-    break;
-  default: // for any other TM we need 2 bits harq
-    if (cc->tdd_Config==NULL) {
-        harq_information->harq_information_rel10.harq_size     = 2;
-    }
-    else {
-      if (harq_information->harq_information_rel10.ack_nack_mode == 1)
-        harq_information->harq_information_rel10.harq_size     = get_V_UL_DAI(module_idP,CC_idP,rntiP);
-      else
-        harq_information->harq_information_rel10.harq_size     = 2;
-    }
-    break;
-  } // get Tmode
+    harq_information->harq_information_rel11.num_ant_ports = 1;
+
+    switch (get_tmode(module_idP, CC_idP, UE_id)) {
+    case 1:
+    case 2:
+    case 5:
+    case 6:
+    case 7:
+	if (cc->tdd_Config != NULL) {
+	    AssertFatal(UE_list->
+			UE_template[CC_idP]
+			[UE_id].physicalConfigDedicated->
+			pucch_ConfigDedicated != NULL,
+			"pucch_ConfigDedicated is null for TDD!\n");
+	    if ((UE_list->
+		 UE_template[CC_idP][UE_id].physicalConfigDedicated->
+		 pucch_ConfigDedicated->tdd_AckNackFeedbackMode != NULL)
+		&& (*UE_list->
+		    UE_template[CC_idP][UE_id].physicalConfigDedicated->
+		    pucch_ConfigDedicated->tdd_AckNackFeedbackMode ==
+		    PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing))
+	    {
+		harq_information->harq_information_rel10_tdd.harq_size = 2;	// 2-bit ACK/NAK
+		harq_information->harq_information_rel10_tdd.ack_nack_mode = 1;	// multiplexing
+	    } else {
+		harq_information->harq_information_rel10_tdd.harq_size = 1;	// 1-bit ACK/NAK
+		harq_information->harq_information_rel10_tdd.ack_nack_mode = 0;	// bundling
+	    }
+	    harq_information->harq_information_rel10_tdd.n_pucch_1_0 =
+		cc->radioResourceConfigCommon->pucch_ConfigCommon.
+		n1PUCCH_AN + cce_idxP;
+	    harq_information->
+		harq_information_rel10_tdd.number_of_pucch_resources = 1;
+	} else {
+	    harq_information->
+		harq_information_rel9_fdd.number_of_pucch_resources = 1;
+	    harq_information->harq_information_rel9_fdd.harq_size = 1;	// 1-bit ACK/NAK
+	    harq_information->harq_information_rel9_fdd.n_pucch_1_0 =
+		cc->radioResourceConfigCommon->pucch_ConfigCommon.
+		n1PUCCH_AN + cce_idxP;
+	}
+	break;
+    default:			// for any other TM we need 2 bits harq
+	if (cc->tdd_Config != NULL) {
+	    AssertFatal(UE_list->
+			UE_template[CC_idP]
+			[UE_id].physicalConfigDedicated->
+			pucch_ConfigDedicated != NULL,
+			"pucch_ConfigDedicated is null for TDD!\n");
+	    if ((UE_list->
+		 UE_template[CC_idP][UE_id].physicalConfigDedicated->
+		 pucch_ConfigDedicated->tdd_AckNackFeedbackMode != NULL)
+		&& (*UE_list->
+		    UE_template[CC_idP][UE_id].physicalConfigDedicated->
+		    pucch_ConfigDedicated->tdd_AckNackFeedbackMode ==
+		    PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing))
+	    {
+		harq_information->harq_information_rel10_tdd.ack_nack_mode = 1;	// multiplexing
+	    } else {
+		harq_information->harq_information_rel10_tdd.ack_nack_mode = 0;	// bundling
+	    }
+	    harq_information->harq_information_rel10_tdd.harq_size = 2;
+	    harq_information->harq_information_rel10_tdd.n_pucch_1_0 =
+		cc->radioResourceConfigCommon->pucch_ConfigCommon.
+		n1PUCCH_AN + cce_idxP;
+	    harq_information->
+		harq_information_rel10_tdd.number_of_pucch_resources = 1;
+	} else {
+	    harq_information->
+		harq_information_rel9_fdd.number_of_pucch_resources = 1;
+	    harq_information->harq_information_rel9_fdd.ack_nack_mode = 0;	// 1a/b
+	    harq_information->harq_information_rel9_fdd.harq_size = 2;
+	    harq_information->harq_information_rel9_fdd.n_pucch_1_0 =
+		cc->radioResourceConfigCommon->pucch_ConfigCommon.
+		n1PUCCH_AN + cce_idxP;
+	}
+	break;
+    }				// get Tmode
 }
 
-void fill_nfapi_harq_information(module_id_t module_idP,
-                                 int CC_idP,
-                                 uint16_t rntiP,
-                                 uint16_t absSFP,
-                                 nfapi_ul_config_harq_information *harq_information,
-                                 uint8_t cce_idxP)
+uint16_t
+fill_nfapi_uci_acknak(module_id_t module_idP,
+		      int CC_idP,
+		      uint16_t rntiP, uint16_t absSFP, uint8_t cce_idxP)
 {
-  eNB_MAC_INST                   *eNB      = RC.mac[module_idP];
-  COMMON_channels_t              *cc       = &eNB->common_channels[CC_idP];
-  UE_list_t                      *UE_list  = &eNB->UE_list;
-
-  int UE_id                                = find_UE_id(module_idP,rntiP);
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    COMMON_channels_t *cc = &eNB->common_channels[CC_idP];
+
+    int ackNAK_absSF = get_pucch1_absSF(cc, absSFP);
+    nfapi_ul_config_request_body_t *ul_req =
+	&eNB->UL_req_tmp[CC_idP][ackNAK_absSF % 10].ul_config_request_body;
+    nfapi_ul_config_request_pdu_t *ul_config_pdu =
+	&ul_req->ul_config_pdu_list[ul_req->number_of_pdus];
+
+    memset((void *) ul_config_pdu, 0,
+	   sizeof(nfapi_ul_config_request_pdu_t));
+    ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE;
+    ul_config_pdu->pdu_size =
+	(uint8_t) (2 + sizeof(nfapi_ul_config_uci_harq_pdu));
+    ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.handle = 0;	// don't know how to use this
+    ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.rnti =
+	rntiP;
+
+    fill_nfapi_harq_information(module_idP, CC_idP,
+				rntiP,
+				absSFP,
+				&ul_config_pdu->uci_harq_pdu.
+				harq_information, cce_idxP);
+    LOG_D(MAC,
+	  "Filled in UCI HARQ request for rnti %x SF %d.%d acknakSF %d.%d, cce_idxP %d-> n1_pucch %d\n",
+	  rntiP, absSFP / 10, absSFP % 10, ackNAK_absSF / 10,
+	  ackNAK_absSF % 10, cce_idxP,
+	  ul_config_pdu->uci_harq_pdu.
+	  harq_information.harq_information_rel9_fdd.n_pucch_1_0);
+
+    ul_req->number_of_pdus++;
+
+    return (((ackNAK_absSF / 10) << 4) + (ackNAK_absSF % 10));
+}
 
-  AssertFatal(UE_id>=0,"UE_id cannot be found, impossible\n");
-  AssertFatal(UE_list!=NULL,"UE_list is null\n");
-  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated!=NULL,"physicalConfigDedicated for rnti %x is null\n",rntiP);
+void
+fill_nfapi_dlsch_config(eNB_MAC_INST * eNB,
+			nfapi_dl_config_request_body_t * dl_req,
+			uint16_t length,
+			uint16_t pdu_index,
+			uint16_t rnti,
+			uint8_t resource_allocation_type,
+			uint8_t
+			virtual_resource_block_assignment_flag,
+			uint16_t resource_block_coding,
+			uint8_t modulation,
+			uint8_t redundancy_version,
+			uint8_t transport_blocks,
+			uint8_t transport_block_to_codeword_swap_flag,
+			uint8_t transmission_scheme,
+			uint8_t number_of_layers,
+			uint8_t number_of_subbands,
+			//                             uint8_t codebook_index,
+			uint8_t ue_category_capacity,
+			uint8_t pa,
+			uint8_t delta_power_offset_index,
+			uint8_t ngap,
+			uint8_t nprb,
+			uint8_t transmission_mode,
+			uint8_t num_bf_prb_per_subband,
+			uint8_t num_bf_vector)
+{
+    nfapi_dl_config_request_pdu_t *dl_config_pdu =
+	&dl_req->dl_config_pdu_list[dl_req->number_pdu];
+    memset((void *) dl_config_pdu, 0,
+	   sizeof(nfapi_dl_config_request_pdu_t));
+
+    dl_config_pdu->pdu_type = NFAPI_DL_CONFIG_DLSCH_PDU_TYPE;
+    dl_config_pdu->pdu_size =
+	(uint8_t) (2 + sizeof(nfapi_dl_config_dlsch_pdu));
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.length = length;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index = pdu_index;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti = rnti;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type =
+	resource_allocation_type;
+    dl_config_pdu->dlsch_pdu.
+	dlsch_pdu_rel8.virtual_resource_block_assignment_flag =
+	virtual_resource_block_assignment_flag;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_block_coding =
+	resource_block_coding;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation = modulation;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.redundancy_version =
+	redundancy_version;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks =
+	transport_blocks;
+    dl_config_pdu->dlsch_pdu.
+	dlsch_pdu_rel8.transport_block_to_codeword_swap_flag =
+	transport_block_to_codeword_swap_flag;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_scheme =
+	transmission_scheme;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_layers =
+	number_of_layers;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_subbands =
+	number_of_subbands;
+    //  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = codebook_index;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ue_category_capacity =
+	ue_category_capacity;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa = pa;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.delta_power_offset_index =
+	delta_power_offset_index;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap = ngap;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb = nprb;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_mode =
+	transmission_mode;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_prb_per_subband =
+	num_bf_prb_per_subband;
+    dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector = num_bf_vector;
+    dl_req->number_pdu++;
+}
 
-  harq_information->harq_information_rel11.num_ant_ports = 1;
+uint16_t
+fill_nfapi_tx_req(nfapi_tx_request_body_t * tx_req_body,
+		  uint16_t absSF, uint16_t pdu_length,
+		  uint16_t pdu_index, uint8_t * pdu)
+{
+    nfapi_tx_request_pdu_t *TX_req =
+	&tx_req_body->tx_pdu_list[tx_req_body->number_of_pdus];
+    LOG_D(MAC, "Filling TX_req %d for pdu length %d\n",
+	  tx_req_body->number_of_pdus, pdu_length);
+    TX_req->pdu_length = pdu_length;
+    TX_req->pdu_index = pdu_index;
+    TX_req->num_segments = 1;
+    TX_req->segments[0].segment_length = pdu_length;
+    TX_req->segments[0].segment_data = pdu;
+    tx_req_body->number_of_pdus++;
+
+    return (((absSF / 10) << 4) + (absSF % 10));
+}
 
-  switch(get_tmode(module_idP,CC_idP,UE_id)) {
-  case 1:
-  case 2:
-  case 5:
-  case 6:
-  case 7:
-    if (cc->tdd_Config!=NULL) {
-      AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated!=NULL,
-                  "pucch_ConfigDedicated is null for TDD!\n");
-      if ((UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode!=NULL)&&
-          (*UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode==PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing)) {
-        harq_information->harq_information_rel10_tdd.harq_size     = 2;  // 2-bit ACK/NAK
-        harq_information->harq_information_rel10_tdd.ack_nack_mode = 1;  // multiplexing
-      }
-      else {
-        harq_information->harq_information_rel10_tdd.harq_size     = 1;  // 1-bit ACK/NAK
-        harq_information->harq_information_rel10_tdd.ack_nack_mode = 0;  // bundling
-      }
-      harq_information->harq_information_rel10_tdd.n_pucch_1_0   = cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + cce_idxP;
-      harq_information->harq_information_rel10_tdd.number_of_pucch_resources = 1;
-    } else {
-      harq_information->harq_information_rel9_fdd.number_of_pucch_resources = 1;
-      harq_information->harq_information_rel9_fdd.harq_size                 = 1;  // 1-bit ACK/NAK
-      harq_information->harq_information_rel9_fdd.n_pucch_1_0               = cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + cce_idxP;
-    }
-    break;
-  default: // for any other TM we need 2 bits harq
-    if (cc->tdd_Config!=NULL) {
-      AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated!=NULL,
-                  "pucch_ConfigDedicated is null for TDD!\n");
-      if ((UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode!=NULL)&&
-          (*UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode==PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing)) {
-        harq_information->harq_information_rel10_tdd.ack_nack_mode = 1;  // multiplexing
-      }
-      else {
-        harq_information->harq_information_rel10_tdd.ack_nack_mode = 0;  // bundling
-      }
-      harq_information->harq_information_rel10_tdd.harq_size       = 2;
-      harq_information->harq_information_rel10_tdd.n_pucch_1_0   = cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + cce_idxP;
-      harq_information->harq_information_rel10_tdd.number_of_pucch_resources = 1;
-    }
-    else {
-      harq_information->harq_information_rel9_fdd.number_of_pucch_resources = 1;
-      harq_information->harq_information_rel9_fdd.ack_nack_mode    = 0;  // 1a/b
-      harq_information->harq_information_rel9_fdd.harq_size        = 2;
-      harq_information->harq_information_rel9_fdd.n_pucch_1_0      = cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + cce_idxP;
+void
+fill_nfapi_ulsch_config_request_rel8(nfapi_ul_config_request_pdu_t *
+				     ul_config_pdu, uint8_t cqi_req,
+				     COMMON_channels_t * cc,
+				     struct PhysicalConfigDedicated
+				     *physicalConfigDedicated,
+				     uint8_t tmode, uint32_t handle,
+				     uint16_t rnti,
+				     uint8_t resource_block_start,
+				     uint8_t
+				     number_of_resource_blocks,
+				     uint8_t mcs,
+				     uint8_t cyclic_shift_2_for_drms,
+				     uint8_t
+				     frequency_hopping_enabled_flag,
+				     uint8_t frequency_hopping_bits,
+				     uint8_t new_data_indication,
+				     uint8_t redundancy_version,
+				     uint8_t harq_process_number,
+				     uint8_t ul_tx_mode,
+				     uint8_t current_tx_nb,
+				     uint8_t n_srs, uint16_t size)
+{
+    memset((void *) ul_config_pdu, 0,
+	   sizeof(nfapi_ul_config_request_pdu_t));
+
+    ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_PDU_TYPE;
+    ul_config_pdu->pdu_size =
+	(uint8_t) (2 + sizeof(nfapi_ul_config_ulsch_pdu));
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.handle = handle;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.rnti = rnti;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.resource_block_start =
+	resource_block_start;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks =
+	number_of_resource_blocks;
+    if (mcs < 11)
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.modulation_type = 2;
+    else if (mcs < 21)
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.modulation_type = 4;
+    else
+	ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.modulation_type = 6;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.cyclic_shift_2_for_drms =
+	cyclic_shift_2_for_drms;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.
+	frequency_hopping_enabled_flag = frequency_hopping_enabled_flag;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.frequency_hopping_bits =
+	frequency_hopping_bits;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.new_data_indication =
+	new_data_indication;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.redundancy_version =
+	redundancy_version;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.harq_process_number =
+	harq_process_number;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.ul_tx_mode = ul_tx_mode;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.current_tx_nb = current_tx_nb;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.n_srs = n_srs;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.size = size;
+
+    if (cqi_req == 1) {
+	// Add CQI portion
+	ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE;
+	ul_config_pdu->pdu_size =
+	    (uint8_t) (2 + sizeof(nfapi_ul_config_ulsch_cqi_ri_pdu));
+	ul_config_pdu->ulsch_cqi_ri_pdu.
+	    cqi_ri_information.cqi_ri_information_rel9.report_type = 1;
+	ul_config_pdu->ulsch_cqi_ri_pdu.
+	    cqi_ri_information.cqi_ri_information_rel9.
+	    aperiodic_cqi_pmi_ri_report.number_of_cc = 1;
+	LOG_D(MAC, "report_type %d\n",
+	      ul_config_pdu->ulsch_cqi_ri_pdu.
+	      cqi_ri_information.cqi_ri_information_rel9.report_type);
+
+	if (cc->p_eNB <= 2
+	    && (tmode == 3 || tmode == 4 || tmode == 8 || tmode == 9
+		|| tmode == 10))
+	    ul_config_pdu->ulsch_cqi_ri_pdu.
+		cqi_ri_information.cqi_ri_information_rel9.
+		aperiodic_cqi_pmi_ri_report.cc[0].ri_size = 1;
+	else if (cc->p_eNB <= 2)
+	    ul_config_pdu->ulsch_cqi_ri_pdu.
+		cqi_ri_information.cqi_ri_information_rel9.
+		aperiodic_cqi_pmi_ri_report.cc[0].ri_size = 0;
+	else if (cc->p_eNB == 4)
+	    ul_config_pdu->ulsch_cqi_ri_pdu.
+		cqi_ri_information.cqi_ri_information_rel9.
+		aperiodic_cqi_pmi_ri_report.cc[0].ri_size = 2;
+
+	AssertFatal(physicalConfigDedicated->cqi_ReportConfig != NULL,
+		    "physicalConfigDedicated->cqi_ReportConfig is null!\n");
+	AssertFatal(physicalConfigDedicated->
+		    cqi_ReportConfig->cqi_ReportModeAperiodic != NULL,
+		    "physicalConfigDedicated->cqi_ReportModeAperiodic is null!\n");
+	AssertFatal(physicalConfigDedicated->pusch_ConfigDedicated != NULL,
+		    "physicalConfigDedicated->puschConfigDedicated is null!\n");
+
+	for (int ri = 0;
+	     ri <
+	     (1 << ul_config_pdu->ulsch_cqi_ri_pdu.
+	      cqi_ri_information.cqi_ri_information_rel9.
+	      aperiodic_cqi_pmi_ri_report.cc[0].ri_size); ri++)
+	    ul_config_pdu->ulsch_cqi_ri_pdu.
+		cqi_ri_information.cqi_ri_information_rel9.
+		aperiodic_cqi_pmi_ri_report.cc[0].dl_cqi_pmi_size[ri] =
+		get_dl_cqi_pmi_size_pusch(cc, tmode, 1 + ri,
+					  physicalConfigDedicated->cqi_ReportConfig->cqi_ReportModeAperiodic);
+
+	ul_config_pdu->ulsch_cqi_ri_pdu.
+	    cqi_ri_information.cqi_ri_information_rel9.delta_offset_cqi =
+	    physicalConfigDedicated->pusch_ConfigDedicated->
+	    betaOffset_CQI_Index;
+	ul_config_pdu->ulsch_cqi_ri_pdu.
+	    cqi_ri_information.cqi_ri_information_rel9.delta_offset_ri =
+	    physicalConfigDedicated->pusch_ConfigDedicated->
+	    betaOffset_RI_Index;
     }
-    break;
-  } // get Tmode
-}
-
-uint16_t fill_nfapi_uci_acknak(module_id_t module_idP,
-                               int CC_idP,
-                               uint16_t rntiP,
-                               uint16_t absSFP,
-                               uint8_t cce_idxP)
-{
-  eNB_MAC_INST                   *eNB          = RC.mac[module_idP];
-  COMMON_channels_t              *cc           = &eNB->common_channels[CC_idP];
-
-  int ackNAK_absSF                             = get_pucch1_absSF(cc,absSFP);
-  nfapi_ul_config_request_body_t *ul_req       = &eNB->UL_req_tmp[CC_idP][ackNAK_absSF%10].ul_config_request_body;
-  nfapi_ul_config_request_pdu_t *ul_config_pdu = &ul_req->ul_config_pdu_list[ul_req->number_of_pdus];
-
-  memset((void*)ul_config_pdu,0,sizeof(nfapi_ul_config_request_pdu_t));
-  ul_config_pdu->pdu_type                                                              = NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE;
-  ul_config_pdu->pdu_size                                                              = (uint8_t)(2+sizeof(nfapi_ul_config_uci_harq_pdu));
-  ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.handle                = 0; // don't know how to use this
-  ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.rnti                  = rntiP;
-
-  fill_nfapi_harq_information(module_idP,CC_idP,
-                              rntiP,
-                              absSFP,
-                              &ul_config_pdu->uci_harq_pdu.harq_information,
-                              cce_idxP);
-  LOG_D(MAC,"Filled in UCI HARQ request for rnti %x SF %d.%d acknakSF %d.%d, cce_idxP %d-> n1_pucch %d\n",rntiP,
-        absSFP/10,absSFP%10,ackNAK_absSF/10,ackNAK_absSF%10,cce_idxP,ul_config_pdu->uci_harq_pdu.harq_information.harq_information_rel9_fdd.n_pucch_1_0);
-
-  ul_req->number_of_pdus++;
-
-  return(((ackNAK_absSF/10)<<4) + (ackNAK_absSF%10));
-}
-
-void fill_nfapi_dlsch_config(eNB_MAC_INST *eNB,
-                             nfapi_dl_config_request_body_t *dl_req,
-                             uint16_t length,
-                             uint16_t pdu_index,
-                             uint16_t rnti,
-                             uint8_t resource_allocation_type,
-                             uint8_t virtual_resource_block_assignment_flag,
-                             uint16_t resource_block_coding,
-                             uint8_t modulation,
-                             uint8_t redundancy_version,
-                             uint8_t transport_blocks,
-                             uint8_t transport_block_to_codeword_swap_flag,
-                             uint8_t transmission_scheme,
-                             uint8_t number_of_layers,
-                             uint8_t number_of_subbands,
-                             //                             uint8_t codebook_index,
-                             uint8_t ue_category_capacity,
-                             uint8_t pa,
-                             uint8_t delta_power_offset_index,
-                             uint8_t ngap,
-                             uint8_t nprb,
-                             uint8_t transmission_mode,
-                             uint8_t num_bf_prb_per_subband,
-                             uint8_t num_bf_vector
-                             )
-{
-  nfapi_dl_config_request_pdu_t   *dl_config_pdu = &dl_req->dl_config_pdu_list[dl_req->number_pdu];
-  memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
-
-  dl_config_pdu->pdu_type                                                        = NFAPI_DL_CONFIG_DLSCH_PDU_TYPE;
-  dl_config_pdu->pdu_size                                                        = (uint8_t)(2+sizeof(nfapi_dl_config_dlsch_pdu));
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.length                                 = length;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index                              = pdu_index;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti                                   = rnti;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type               = resource_allocation_type;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = virtual_resource_block_assignment_flag;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_block_coding                  = resource_block_coding;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation                             = modulation;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.redundancy_version                     = redundancy_version;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks                       = transport_blocks;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_block_to_codeword_swap_flag  = transport_block_to_codeword_swap_flag;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_scheme                    = transmission_scheme;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_layers                       = number_of_layers;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_subbands                     = number_of_subbands;
-  //  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = codebook_index;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ue_category_capacity                   = ue_category_capacity;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa                                     = pa;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.delta_power_offset_index               = delta_power_offset_index;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap                                   = ngap;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb                                   = nprb;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_mode                      = transmission_mode;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_prb_per_subband                 = num_bf_prb_per_subband;
-  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector                          = num_bf_vector;
-  dl_req->number_pdu++;
-}
-
-uint16_t fill_nfapi_tx_req(nfapi_tx_request_body_t *tx_req_body,uint16_t absSF,uint16_t pdu_length, uint16_t pdu_index, uint8_t *pdu)
-{
-  nfapi_tx_request_pdu_t *TX_req        = &tx_req_body->tx_pdu_list[tx_req_body->number_of_pdus];
-  LOG_D(MAC,"Filling TX_req %d for pdu length %d\n",tx_req_body->number_of_pdus,pdu_length);
-  TX_req->pdu_length                    = pdu_length;
-  TX_req->pdu_index                     = pdu_index;
-  TX_req->num_segments                  = 1;
-  TX_req->segments[0].segment_length    = pdu_length;
-  TX_req->segments[0].segment_data      = pdu;
-  tx_req_body->number_of_pdus++;
-
-  return(((absSF/10)<<4) + (absSF%10));
-}
-
-void fill_nfapi_ulsch_config_request_rel8(nfapi_ul_config_request_pdu_t  *ul_config_pdu,
-                                          uint8_t                        cqi_req,
-                                          COMMON_channels_t              *cc,
-                                          struct PhysicalConfigDedicated  *physicalConfigDedicated,
-                                          uint8_t                        tmode,
-                                          uint32_t                       handle,
-                                          uint16_t                       rnti,
-                                          uint8_t                        resource_block_start,
-                                          uint8_t                        number_of_resource_blocks,
-                                          uint8_t                        mcs,
-                                          uint8_t                        cyclic_shift_2_for_drms,
-                                          uint8_t                        frequency_hopping_enabled_flag,
-                                          uint8_t                        frequency_hopping_bits,
-                                          uint8_t                        new_data_indication,
-                                          uint8_t                        redundancy_version,
-                                          uint8_t                        harq_process_number,
-                                          uint8_t                        ul_tx_mode,
-                                          uint8_t                        current_tx_nb,
-                                          uint8_t                        n_srs,
-                                          uint16_t                       size
-                                          )
-{
-  memset((void*)ul_config_pdu,0,sizeof(nfapi_ul_config_request_pdu_t));
-
-  ul_config_pdu->pdu_type                                                        = NFAPI_UL_CONFIG_ULSCH_PDU_TYPE;
-  ul_config_pdu->pdu_size                                                        = (uint8_t)(2+sizeof(nfapi_ul_config_ulsch_pdu));
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.handle                                 = handle;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.rnti                                   = rnti;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.resource_block_start                   = resource_block_start;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks              = number_of_resource_blocks;
-  if      (mcs<11) ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.modulation_type       = 2;
-  else if (mcs<21) ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.modulation_type       = 4;
-  else             ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.modulation_type       = 6;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.cyclic_shift_2_for_drms                = cyclic_shift_2_for_drms;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.frequency_hopping_enabled_flag         = frequency_hopping_enabled_flag;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.frequency_hopping_bits                 = frequency_hopping_bits;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.new_data_indication                    = new_data_indication;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.redundancy_version                     = redundancy_version;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.harq_process_number                    = harq_process_number;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.ul_tx_mode                             = ul_tx_mode;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.current_tx_nb                          = current_tx_nb;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.n_srs                                  = n_srs;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.size                                   = size;
-
-  if (cqi_req == 1) {
-    // Add CQI portion
-    ul_config_pdu->pdu_type                                                           = NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE;
-    ul_config_pdu->pdu_size                                                           = (uint8_t)(2+sizeof(nfapi_ul_config_ulsch_cqi_ri_pdu));
-    ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.report_type             = 1;
-    ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.aperiodic_cqi_pmi_ri_report.number_of_cc = 1;
-    LOG_D(MAC,"report_type %d\n",ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.report_type);
-
-    if (cc->p_eNB<=2 && (tmode==3||tmode==4||tmode==8||tmode==9||tmode==10))
-      ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.aperiodic_cqi_pmi_ri_report.cc[0].ri_size = 1;
-    else if (cc->p_eNB<=2)
-      ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.aperiodic_cqi_pmi_ri_report.cc[0].ri_size = 0;
-    else if (cc->p_eNB==4)
-      ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.aperiodic_cqi_pmi_ri_report.cc[0].ri_size = 2;
-
-    AssertFatal(physicalConfigDedicated->cqi_ReportConfig!=NULL,"physicalConfigDedicated->cqi_ReportConfig is null!\n");
-    AssertFatal(physicalConfigDedicated->cqi_ReportConfig->cqi_ReportModeAperiodic!=NULL,"physicalConfigDedicated->cqi_ReportModeAperiodic is null!\n");
-    AssertFatal(physicalConfigDedicated->pusch_ConfigDedicated!=NULL,"physicalConfigDedicated->puschConfigDedicated is null!\n");
-
-    for (int ri=0;
-         ri<(1<<ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.aperiodic_cqi_pmi_ri_report.cc[0].ri_size);
-         ri++)
-      ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.aperiodic_cqi_pmi_ri_report.cc[0].dl_cqi_pmi_size[ri] =
-        get_dl_cqi_pmi_size_pusch(cc,
-                                  tmode,
-                                  1+ri,
-                                  physicalConfigDedicated->cqi_ReportConfig->cqi_ReportModeAperiodic);
-
-    ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.delta_offset_cqi        = physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_CQI_Index;
-    ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.delta_offset_ri         = physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_RI_Index;
-  }
 }
 
 #ifdef Rel14
-void fill_nfapi_ulsch_config_request_emtc(nfapi_ul_config_request_pdu_t  *ul_config_pdu,
-                                          uint8_t ue_type,
-                                          uint16_t total_number_of_repetitions,
-                                          uint16_t repetition_number,
-                                          uint16_t initial_transmission_sf_io)
+void
+fill_nfapi_ulsch_config_request_emtc(nfapi_ul_config_request_pdu_t *
+				     ul_config_pdu, uint8_t ue_type,
+				     uint16_t
+				     total_number_of_repetitions,
+				     uint16_t repetition_number,
+				     uint16_t initial_transmission_sf_io)
 {
-  // Re13 fields
-
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.ue_type                               = ue_type;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.total_number_of_repetitions           = total_number_of_repetitions;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.repetition_number                     = repetition_number;
-  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.initial_transmission_sf_io            = initial_transmission_sf_io;
+    // Re13 fields
+
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.ue_type = ue_type;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.total_number_of_repetitions =
+	total_number_of_repetitions;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.repetition_number =
+	repetition_number;
+    ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.initial_transmission_sf_io =
+	initial_transmission_sf_io;
 }
 
 int get_numnarrowbands(long dl_Bandwidth)
 {
-  int nb_tab[6] = {1,2,4,8,12,16};
+    int nb_tab[6] = { 1, 2, 4, 8, 12, 16 };
 
-  AssertFatal(dl_Bandwidth<7 || dl_Bandwidth>=0,"dl_Bandwidth not in [0..6]\n");
-  return(nb_tab[dl_Bandwidth]);
+    AssertFatal(dl_Bandwidth < 7
+		|| dl_Bandwidth >= 0, "dl_Bandwidth not in [0..6]\n");
+    return (nb_tab[dl_Bandwidth]);
 }
 
 int get_numnarrowbandbits(long dl_Bandwidth)
 {
-  int nbbits_tab[6] = {0,1,2,3,4,4};
+    int nbbits_tab[6] = { 0, 1, 2, 3, 4, 4 };
 
-  AssertFatal(dl_Bandwidth<7 || dl_Bandwidth>=0,"dl_Bandwidth not in [0..6]\n");
-  return(nbbits_tab[dl_Bandwidth]);
+    AssertFatal(dl_Bandwidth < 7
+		|| dl_Bandwidth >= 0, "dl_Bandwidth not in [0..6]\n");
+    return (nbbits_tab[dl_Bandwidth]);
 }
 
 //This implements the frame/subframe condition for first subframe of MPDCCH transmission (Section 9.1.5 36.213, Rel 13/14)
-int startSF_fdd_RA_times2[8] = {2,3,4,5,8,10,16,20};
-int startSF_tdd_RA[7]        = {1,2,4,5,8,10,20};
+int startSF_fdd_RA_times2[8] = { 2, 3, 4, 5, 8, 10, 16, 20 };
+int startSF_tdd_RA[7] = { 1, 2, 4, 5, 8, 10, 20 };
 
-int mpdcch_sf_condition(eNB_MAC_INST *eNB,int CC_id, frame_t frameP,sub_frame_t subframeP,int rmax,MPDCCH_TYPES_t mpdcch_type,int UE_id)
+int
+mpdcch_sf_condition(eNB_MAC_INST * eNB, int CC_id, frame_t frameP,
+		    sub_frame_t subframeP, int rmax,
+		    MPDCCH_TYPES_t mpdcch_type, int UE_id)
 {
-  struct PRACH_ConfigSIB_v1310 *ext4_prach = eNB->common_channels[CC_id].radioResourceConfigCommon_BR->ext4->prach_ConfigCommon_v1310;
-
-  int T;
-  EPDCCH_SetConfig_r11_t *epdcch_setconfig_r11;
-
-  switch (mpdcch_type) {
-  case TYPE0:
-    AssertFatal(1==0,"MPDCCH Type 0 not handled yet\n");
-    break;
-  case TYPE1:
-    AssertFatal(1==0,"MPDCCH Type 1 not handled yet\n");
-    break;
-  case TYPE1A:
-    AssertFatal(1==0,"MPDCCH Type 1A not handled yet\n");
-    break;
-  case TYPE2: // RAR
-    AssertFatal(ext4_prach->mpdcch_startSF_CSS_RA_r13!=NULL,
-                "mpdcch_startSF_CSS_RA_r13 is null\n");
-    AssertFatal(rmax>0,"rmax is 0!\b");
-    if (eNB->common_channels[CC_id].tdd_Config==NULL) //FDD
-      T = rmax*startSF_fdd_RA_times2[ext4_prach->mpdcch_startSF_CSS_RA_r13->choice.fdd_r13]>>1;
-    else //TDD
-      T = rmax*startSF_tdd_RA[ext4_prach->mpdcch_startSF_CSS_RA_r13->choice.tdd_r13];
-    break;
-  case TYPE2A:
-    AssertFatal(1==0,"MPDCCH Type 2A not handled yet\n");
-    break;
-  case TYPEUESPEC:
-    epdcch_setconfig_r11= eNB->UE_list.UE_template[CC_id][UE_id].physicalConfigDedicated->ext4->epdcch_Config_r11->config_r11.choice.setup.setConfigToAddModList_r11->list.array[0] ;
-
-    AssertFatal(epdcch_setconfig_r11 != NULL," epdcch_setconfig_r11 is null for UE specific \n");
-    AssertFatal(epdcch_setconfig_r11->ext2 != NULL," ext2 doesn't exist in epdcch config ' \n");
-
-    if (eNB->common_channels[CC_id].tdd_Config==NULL) //FDD
-      T = rmax*startSF_fdd_RA_times2[epdcch_setconfig_r11->ext2->mpdcch_config_r13->choice.setup.mpdcch_StartSF_UESS_r13.choice.fdd_r13]>>1;
-    else //TDD
-      T = rmax*startSF_tdd_RA[epdcch_setconfig_r11->ext2->mpdcch_config_r13->choice.setup.mpdcch_StartSF_UESS_r13.choice.tdd_r13];
-
-    break;
-  default:
-    return(0);
-  }
+    struct PRACH_ConfigSIB_v1310 *ext4_prach =
+	eNB->common_channels[CC_id].radioResourceConfigCommon_BR->
+	ext4->prach_ConfigCommon_v1310;
+
+    int T;
+    EPDCCH_SetConfig_r11_t *epdcch_setconfig_r11;
+
+    switch (mpdcch_type) {
+    case TYPE0:
+	AssertFatal(1 == 0, "MPDCCH Type 0 not handled yet\n");
+	break;
+    case TYPE1:
+	AssertFatal(1 == 0, "MPDCCH Type 1 not handled yet\n");
+	break;
+    case TYPE1A:
+	AssertFatal(1 == 0, "MPDCCH Type 1A not handled yet\n");
+	break;
+    case TYPE2:		// RAR
+	AssertFatal(ext4_prach->mpdcch_startSF_CSS_RA_r13 != NULL,
+		    "mpdcch_startSF_CSS_RA_r13 is null\n");
+	AssertFatal(rmax > 0, "rmax is 0!\b");
+	if (eNB->common_channels[CC_id].tdd_Config == NULL)	//FDD
+	    T = rmax *
+		startSF_fdd_RA_times2[ext4_prach->
+				      mpdcch_startSF_CSS_RA_r13->
+				      choice.fdd_r13] >> 1;
+	else			//TDD
+	    T = rmax *
+		startSF_tdd_RA[ext4_prach->
+			       mpdcch_startSF_CSS_RA_r13->choice.tdd_r13];
+	break;
+    case TYPE2A:
+	AssertFatal(1 == 0, "MPDCCH Type 2A not handled yet\n");
+	break;
+    case TYPEUESPEC:
+	epdcch_setconfig_r11 =
+	    eNB->UE_list.UE_template[CC_id][UE_id].
+	    physicalConfigDedicated->ext4->epdcch_Config_r11->config_r11.
+	    choice.setup.setConfigToAddModList_r11->list.array[0];
+
+	AssertFatal(epdcch_setconfig_r11 != NULL,
+		    " epdcch_setconfig_r11 is null for UE specific \n");
+	AssertFatal(epdcch_setconfig_r11->ext2 != NULL,
+		    " ext2 doesn't exist in epdcch config ' \n");
+
+	if (eNB->common_channels[CC_id].tdd_Config == NULL)	//FDD
+	    T = rmax *
+		startSF_fdd_RA_times2[epdcch_setconfig_r11->
+				      ext2->mpdcch_config_r13->choice.
+				      setup.mpdcch_StartSF_UESS_r13.choice.
+				      fdd_r13] >> 1;
+	else			//TDD
+	    T = rmax *
+		startSF_tdd_RA[epdcch_setconfig_r11->
+			       ext2->mpdcch_config_r13->choice.
+			       setup.mpdcch_StartSF_UESS_r13.choice.
+			       tdd_r13];
+
+	break;
+    default:
+	return (0);
+    }
 
-  AssertFatal(T>0,"T is 0!\n");
-  if (((10*frameP) + subframeP)%T == 0) return(1);
-  else return(0);
+    AssertFatal(T > 0, "T is 0!\n");
+    if (((10 * frameP) + subframeP) % T == 0)
+	return (1);
+    else
+	return (0);
 }
 
-int narrowband_to_first_rb(COMMON_channels_t *cc, int nb_index)
+int narrowband_to_first_rb(COMMON_channels_t * cc, int nb_index)
 {
-  switch (cc->mib->message.dl_Bandwidth) {
-  case 0: // 6 PRBs, N_NB=1, i_0=0
-    return(0);
-    break;
-  case 3: // 50 PRBs, N_NB=8, i_0=1
-    return((int)(1+(6*nb_index)));
-    break;
-  case 5: // 100 PRBs, N_NB=16, i_0=2
-    return((int)(2+(6*nb_index)));
-    break;
-  case 1: // 15 PRBs  N_NB=2, i_0=1
-    if (nb_index>0) return(1);
-    else            return(0);
-    break;
-  case 2: // 25 PRBs, N_NB=4, i_0=0
-    if (nb_index>1) return(1+(6*nb_index));
-    else            return((6*nb_index));
-    break;
-  case 4: // 75 PRBs, N_NB=12, i_0=1
-    if (nb_index>5) return(2+(6*nb_index));
-    else            return(1+(6*nb_index));
-    break;
-  default:
-    AssertFatal(1==0,"Impossible dl_Bandwidth %d\n",(int)cc->mib->message.dl_Bandwidth);
-    break;
-  }
+    switch (cc->mib->message.dl_Bandwidth) {
+    case 0:			// 6 PRBs, N_NB=1, i_0=0
+	return (0);
+	break;
+    case 3:			// 50 PRBs, N_NB=8, i_0=1
+	return ((int) (1 + (6 * nb_index)));
+	break;
+    case 5:			// 100 PRBs, N_NB=16, i_0=2
+	return ((int) (2 + (6 * nb_index)));
+	break;
+    case 1:			// 15 PRBs  N_NB=2, i_0=1
+	if (nb_index > 0)
+	    return (1);
+	else
+	    return (0);
+	break;
+    case 2:			// 25 PRBs, N_NB=4, i_0=0
+	if (nb_index > 1)
+	    return (1 + (6 * nb_index));
+	else
+	    return ((6 * nb_index));
+	break;
+    case 4:			// 75 PRBs, N_NB=12, i_0=1
+	if (nb_index > 5)
+	    return (2 + (6 * nb_index));
+	else
+	    return (1 + (6 * nb_index));
+	break;
+    default:
+	AssertFatal(1 == 0, "Impossible dl_Bandwidth %d\n",
+		    (int) cc->mib->message.dl_Bandwidth);
+	break;
+    }
 }
 #endif
 
@@ -1511,23 +1843,23 @@ int narrowband_to_first_rb(COMMON_channels_t *cc, int nb_index)
 void init_ue_sched_info(void)
 //------------------------------------------------------------------------------
 {
-  module_id_t i,j,k;
-
-  for (i=0; i<NUMBER_OF_eNB_MAX; i++) {
-    for (k=0; k<MAX_NUM_CCs; k++) {
-      for (j=0; j<NUMBER_OF_UE_MAX; j++) {
-        // init DL
-        eNB_dlsch_info[i][k][j].weight           = 0;
-        eNB_dlsch_info[i][k][j].subframe         = 0;
-        eNB_dlsch_info[i][k][j].serving_num      = 0;
-        eNB_dlsch_info[i][k][j].status           = S_DL_NONE;
-        // init UL
-        eNB_ulsch_info[i][k][j].subframe         = 0;
-        eNB_ulsch_info[i][k][j].serving_num      = 0;
-        eNB_ulsch_info[i][k][j].status           = S_UL_NONE;
-      }
+    module_id_t i, j, k;
+
+    for (i = 0; i < NUMBER_OF_eNB_MAX; i++) {
+	for (k = 0; k < MAX_NUM_CCs; k++) {
+	    for (j = 0; j < NUMBER_OF_UE_MAX; j++) {
+		// init DL
+		eNB_dlsch_info[i][k][j].weight = 0;
+		eNB_dlsch_info[i][k][j].subframe = 0;
+		eNB_dlsch_info[i][k][j].serving_num = 0;
+		eNB_dlsch_info[i][k][j].status = S_DL_NONE;
+		// init UL
+		eNB_ulsch_info[i][k][j].subframe = 0;
+		eNB_ulsch_info[i][k][j].serving_num = 0;
+		eNB_ulsch_info[i][k][j].status = S_UL_NONE;
+	    }
+	}
     }
-  }
 }
 
 
@@ -1536,82 +1868,85 @@ void init_ue_sched_info(void)
 unsigned char get_ue_weight(module_id_t module_idP, int CC_idP, int ue_idP)
 //------------------------------------------------------------------------------
 {
-  return(eNB_dlsch_info[module_idP][CC_idP][ue_idP].weight);
+    return (eNB_dlsch_info[module_idP][CC_idP][ue_idP].weight);
 }
 
 //------------------------------------------------------------------------------
 int find_UE_id(module_id_t mod_idP, rnti_t rntiP)
 //------------------------------------------------------------------------------
 {
-  int UE_id;
-  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
-
-  for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
-    if (UE_list->active[UE_id] != TRUE) continue;
-    if (UE_list->UE_template[UE_PCCID(mod_idP,UE_id)][UE_id].rnti==rntiP) {
-      return(UE_id);
+    int UE_id;
+    UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
+
+    for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
+	if (UE_list->active[UE_id] != TRUE)
+	    continue;
+	if (UE_list->UE_template[UE_PCCID(mod_idP, UE_id)][UE_id].rnti ==
+	    rntiP) {
+	    return (UE_id);
+	}
     }
-  }
 
-  return(-1);
+    return (-1);
 }
 
 //------------------------------------------------------------------------------
 int find_RA_id(module_id_t mod_idP, int CC_idP, rnti_t rntiP)
 //------------------------------------------------------------------------------
 {
-  int RA_id;
-  AssertFatal(RC.mac[mod_idP],"RC.mac[%d] is null\n",mod_idP);
+    int RA_id;
+    AssertFatal(RC.mac[mod_idP], "RC.mac[%d] is null\n", mod_idP);
 
-  RA_TEMPLATE *RA_template = (RA_TEMPLATE *)&RC.mac[mod_idP]->common_channels[CC_idP].RA_template[0];
+    RA_t *ra = (RA_t *) & RC.mac[mod_idP]->common_channels[CC_idP].ra[0];
 
-  for (RA_id = 0; RA_id < NB_RA_PROC_MAX; RA_id++) {
-    LOG_D(MAC,"Checking RA_id %d for %x : RA_active %d, wait_ack_Msg4 %d\n",
-          RA_id,rntiP,
-          RA_template[RA_id].RA_active,
-          RA_template[RA_id].wait_ack_Msg4);
+    for (RA_id = 0; RA_id < NB_RA_PROC_MAX; RA_id++) {
+	LOG_D(MAC,
+	      "Checking RA_id %d for %x : state %d\n",
+	      RA_id, rntiP, ra[RA_id].state);
 
-    if (RA_template[RA_id].RA_active==TRUE &&
-        RA_template[RA_id].wait_ack_Msg4 == 0 &&
-        RA_template[RA_id].rnti == rntiP) return(RA_id);
-  }
-  return(-1);
+	if (ra[RA_id].state != IDLE &&
+	    ra[RA_id].rnti == rntiP)
+	    return (RA_id);
+    }
+    return (-1);
 }
 
 //------------------------------------------------------------------------------
-int UE_num_active_CC(UE_list_t *listP,int ue_idP)
+int UE_num_active_CC(UE_list_t * listP, int ue_idP)
 //------------------------------------------------------------------------------
 {
-  return(listP->numactiveCCs[ue_idP]);
+    return (listP->numactiveCCs[ue_idP]);
 }
 
 //------------------------------------------------------------------------------
-int UE_PCCID(module_id_t mod_idP,int ue_idP)
+int UE_PCCID(module_id_t mod_idP, int ue_idP)
 //------------------------------------------------------------------------------
 {
-  return(RC.mac[mod_idP]->UE_list.pCC_id[ue_idP]);
+    return (RC.mac[mod_idP]->UE_list.pCC_id[ue_idP]);
 }
 
 //------------------------------------------------------------------------------
 rnti_t UE_RNTI(module_id_t mod_idP, int ue_idP)
 //------------------------------------------------------------------------------
 {
-  rnti_t rnti = RC.mac[mod_idP]->UE_list.UE_template[UE_PCCID(mod_idP,ue_idP)][ue_idP].rnti;
+    rnti_t rnti =
+	RC.mac[mod_idP]->
+	UE_list.UE_template[UE_PCCID(mod_idP, ue_idP)][ue_idP].rnti;
 
-  if (rnti>0) {
-    return (rnti);
-  }
+    if (rnti > 0) {
+	return (rnti);
+    }
 
-  LOG_D(MAC,"[eNB %d] Couldn't find RNTI for UE %d\n",mod_idP,ue_idP);
-  //display_backtrace();
-  return(NOT_A_RNTI);
+    LOG_D(MAC, "[eNB %d] Couldn't find RNTI for UE %d\n", mod_idP, ue_idP);
+    //display_backtrace();
+    return (NOT_A_RNTI);
 }
 
 //------------------------------------------------------------------------------
 boolean_t is_UE_active(module_id_t mod_idP, int ue_idP)
 //------------------------------------------------------------------------------
 {
-  return(RC.mac[mod_idP]->UE_list.active[ue_idP]);
+    return (RC.mac[mod_idP]->UE_list.active[ue_idP]);
 }
 
 /*
@@ -1639,317 +1974,333 @@ uint8_t find_active_UEs(module_id_t module_idP,int CC_id){
 
 
 // get aggregation (L) form phy for a give UE
-unsigned char get_aggregation (uint8_t bw_index, uint8_t cqi, uint8_t dci_fmt)
+unsigned char
+get_aggregation(uint8_t bw_index, uint8_t cqi, uint8_t dci_fmt)
 {
-  unsigned char aggregation=3;
+    unsigned char aggregation = 3;
+
+    switch (dci_fmt) {
+    case format0:
+	aggregation = cqi2fmt0_agg[bw_index][cqi];
+	break;
+    case format1:
+    case format1A:
+    case format1B:
+    case format1D:
+	aggregation = cqi2fmt1x_agg[bw_index][cqi];
+	break;
+    case format2:
+    case format2A:
+    case format2B:
+    case format2C:
+    case format2D:
+	aggregation = cqi2fmt2x_agg[bw_index][cqi];
+	break;
+    case format1C:
+    case format1E_2A_M10PRB:
+    case format3:
+    case format3A:
+    case format4:
+    default:
+	LOG_W(MAC, "unsupported DCI format %d\n", dci_fmt);
+    }
 
-  switch (dci_fmt){
-  case format0:
-    aggregation = cqi2fmt0_agg[bw_index][cqi];
-    break;
-  case format1:
-  case format1A:
-  case format1B:
-  case format1D:
-    aggregation = cqi2fmt1x_agg[bw_index][cqi];
-    break;
-  case format2:
-  case format2A:
-  case format2B:
-  case format2C:
-  case format2D:
-    aggregation = cqi2fmt2x_agg[bw_index][cqi];
-    break;
-  case format1C:
-  case format1E_2A_M10PRB:
-  case format3:
-  case format3A:
-  case format4:
-  default:
-    LOG_W(MAC,"unsupported DCI format %d\n",dci_fmt);
-  }
-
-  LOG_D(MAC,"Aggregation level %d (cqi %d, bw_index %d, format %d)\n",
-        1<<aggregation, cqi,bw_index,dci_fmt);
+    LOG_D(MAC, "Aggregation level %d (cqi %d, bw_index %d, format %d)\n",
+	  1 << aggregation, cqi, bw_index, dci_fmt);
 
-  return 1<<aggregation;
+    return 1 << aggregation;
 }
 
-void dump_ue_list(UE_list_t *listP, int ul_flag)
+void dump_ue_list(UE_list_t * listP, int ul_flag)
 {
-  int j;
+    int j;
 
-  if ( ul_flag == 0 ) {
-    for (j=listP->head; j>=0; j=listP->next[j]) {
-      LOG_T(MAC,"node %d => %d\n",j,listP->next[j]);
-    }
-  } else {
-    for (j=listP->head_ul; j>=0; j=listP->next_ul[j]) {
-      LOG_T(MAC,"node %d => %d\n",j,listP->next_ul[j]);
+    if (ul_flag == 0) {
+	for (j = listP->head; j >= 0; j = listP->next[j]) {
+	    LOG_T(MAC, "node %d => %d\n", j, listP->next[j]);
+	}
+    } else {
+	for (j = listP->head_ul; j >= 0; j = listP->next_ul[j]) {
+	    LOG_T(MAC, "node %d => %d\n", j, listP->next_ul[j]);
+	}
     }
-  }
 }
 
-int add_new_ue(module_id_t mod_idP, int cc_idP, rnti_t rntiP,int harq_pidP
-               #ifdef Rel14
-                 ,uint8_t rach_resource_type
-               #endif
-               )
+int add_new_ue(module_id_t mod_idP, int cc_idP, rnti_t rntiP, int harq_pidP
+#ifdef Rel14
+	       , uint8_t rach_resource_type
+#endif
+    )
 {
-  int UE_id;
-  int i, j;
-
-  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
-
-  LOG_D(MAC,"[eNB %d, CC_id %d] Adding UE with rnti %x (next avail %d, num_UEs %d)\n",mod_idP,cc_idP,rntiP,UE_list->avail,UE_list->num_UEs);
-  dump_ue_list(UE_list,0);
-
-  for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
-    if (UE_list->active[i] == TRUE) continue;
-    UE_id = i;
-    memset(&UE_list->UE_template[cc_idP][UE_id], 0, sizeof(UE_TEMPLATE));
-    UE_list->UE_template[cc_idP][UE_id].rnti       = rntiP;
-    UE_list->UE_template[cc_idP][UE_id].configured = FALSE;
-    UE_list->numactiveCCs[UE_id]                   = 1;
-    UE_list->numactiveULCCs[UE_id]                 = 1;
-    UE_list->pCC_id[UE_id]                         = cc_idP;
-    UE_list->ordered_CCids[0][UE_id]               = cc_idP;
-    UE_list->ordered_ULCCids[0][UE_id]             = cc_idP;
-    UE_list->num_UEs++;
-    UE_list->active[UE_id]                         = TRUE;
+    int UE_id;
+    int i, j;
+
+    UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
+
+    LOG_D(MAC,
+	  "[eNB %d, CC_id %d] Adding UE with rnti %x (next avail %d, num_UEs %d)\n",
+	  mod_idP, cc_idP, rntiP, UE_list->avail, UE_list->num_UEs);
+    dump_ue_list(UE_list, 0);
+
+    for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
+	if (UE_list->active[i] == TRUE)
+	    continue;
+	UE_id = i;
+	memset(&UE_list->UE_template[cc_idP][UE_id], 0,
+	       sizeof(UE_TEMPLATE));
+	UE_list->UE_template[cc_idP][UE_id].rnti = rntiP;
+	UE_list->UE_template[cc_idP][UE_id].configured = FALSE;
+	UE_list->numactiveCCs[UE_id] = 1;
+	UE_list->numactiveULCCs[UE_id] = 1;
+	UE_list->pCC_id[UE_id] = cc_idP;
+	UE_list->ordered_CCids[0][UE_id] = cc_idP;
+	UE_list->ordered_ULCCids[0][UE_id] = cc_idP;
+	UE_list->num_UEs++;
+	UE_list->active[UE_id] = TRUE;
 
 #ifdef Rel14
-    UE_list->UE_template[cc_idP][UE_id].rach_resource_type = rach_resource_type ;
+	UE_list->UE_template[cc_idP][UE_id].rach_resource_type =
+	    rach_resource_type;
 #endif
 
-    memset((void*)&UE_list->UE_sched_ctrl[UE_id],0,sizeof(UE_sched_ctrl));
-    memset((void*)&UE_list->eNB_UE_stats[cc_idP][UE_id],0,sizeof(eNB_UE_STATS));
-
-    UE_list->UE_sched_ctrl[UE_id].ta_update = 31;
-
-    for (j=0; j<8; j++) {
-      UE_list->UE_template[cc_idP][UE_id].oldNDI[j]    = (j==0)?1:0;   // 1 because first transmission is with format1A (Msg4) for harq_pid 0
-      UE_list->UE_template[cc_idP][UE_id].oldNDI_UL[j] = (j==harq_pidP)?0:1; // 1st transmission is with Msg3;
-      UE_list->UE_sched_ctrl[UE_id].round[cc_idP][j]   = 8;
-      UE_list->UE_sched_ctrl[UE_id].round_UL[cc_idP][j]   = 0;
+	memset((void *) &UE_list->UE_sched_ctrl[UE_id], 0,
+	       sizeof(UE_sched_ctrl));
+	memset((void *) &UE_list->eNB_UE_stats[cc_idP][UE_id], 0,
+	       sizeof(eNB_UE_STATS));
+
+	UE_list->UE_sched_ctrl[UE_id].ta_update = 31;
+
+	for (j = 0; j < 8; j++) {
+	    UE_list->UE_template[cc_idP][UE_id].oldNDI[j] = (j == 0) ? 1 : 0;	// 1 because first transmission is with format1A (Msg4) for harq_pid 0
+	    UE_list->UE_template[cc_idP][UE_id].oldNDI_UL[j] = (j == harq_pidP) ? 0 : 1;	// 1st transmission is with Msg3;
+	    UE_list->UE_sched_ctrl[UE_id].round[cc_idP][j] = 8;
+	    UE_list->UE_sched_ctrl[UE_id].round_UL[cc_idP][j] = 0;
+	}
+
+	eNB_ulsch_info[mod_idP][cc_idP][UE_id].status = S_UL_WAITING;
+	eNB_dlsch_info[mod_idP][cc_idP][UE_id].status = S_DL_WAITING;
+	LOG_D(MAC, "[eNB %d] Add UE_id %d on Primary CC_id %d: rnti %x\n",
+	      mod_idP, UE_id, cc_idP, rntiP);
+	dump_ue_list(UE_list, 0);
+	return (UE_id);
     }
 
-    eNB_ulsch_info[mod_idP][cc_idP][UE_id].status = S_UL_WAITING;
-    eNB_dlsch_info[mod_idP][cc_idP][UE_id].status = S_DL_WAITING;
-    LOG_D(MAC,"[eNB %d] Add UE_id %d on Primary CC_id %d: rnti %x\n",mod_idP,UE_id,cc_idP,rntiP);
-    dump_ue_list(UE_list,0);
-    return(UE_id);
-  }
-
-  printf("MAC: cannot add new UE for rnti %x\n", rntiP);
-  LOG_E(MAC,"error in add_new_ue(), could not find space in UE_list, Dumping UE list\n");
-  dump_ue_list(UE_list,0);
-  return(-1);
+    printf("MAC: cannot add new UE for rnti %x\n", rntiP);
+    LOG_E(MAC,
+	  "error in add_new_ue(), could not find space in UE_list, Dumping UE list\n");
+    dump_ue_list(UE_list, 0);
+    return (-1);
 }
 
 //------------------------------------------------------------------------------
-int rrc_mac_remove_ue(module_id_t mod_idP,rnti_t rntiP)
+int rrc_mac_remove_ue(module_id_t mod_idP, rnti_t rntiP)
 //------------------------------------------------------------------------------
 {
-  int i;
-  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
-  int UE_id = find_UE_id(mod_idP,rntiP);
-  int pCC_id;
-
-  if (UE_id == -1) {
-    LOG_W(MAC,"rrc_mac_remove_ue: UE %x not found\n", rntiP);
-    return 0;
-  }
-
-  pCC_id = UE_PCCID(mod_idP,UE_id);
-
-  LOG_I(MAC,"Removing UE %d from Primary CC_id %d (rnti %x)\n",UE_id,pCC_id, rntiP);
-  dump_ue_list(UE_list,0);
-
-  UE_list->active[UE_id] = FALSE;
-  UE_list->num_UEs--;
-
-  if (UE_list->head == UE_id) UE_list->head=UE_list->next[UE_id];
-  else UE_list->next[prev(UE_list,UE_id,0)]=UE_list->next[UE_id];
-  if (UE_list->head_ul == UE_id) UE_list->head_ul=UE_list->next_ul[UE_id];
-  else UE_list->next_ul[prev(UE_list,UE_id,0)]=UE_list->next_ul[UE_id];
-
-  // clear all remaining pending transmissions
-  UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID0]  = 0;
-  UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID1]  = 0;
-  UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID2]  = 0;
-  UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID3]  = 0;
-
-  UE_list->UE_template[pCC_id][UE_id].ul_SR             = 0;
-  UE_list->UE_template[pCC_id][UE_id].rnti              = NOT_A_RNTI;
-  UE_list->UE_template[pCC_id][UE_id].ul_active         = FALSE;
-  eNB_ulsch_info[mod_idP][pCC_id][UE_id].rnti                        = NOT_A_RNTI;
-  eNB_ulsch_info[mod_idP][pCC_id][UE_id].status                      = S_UL_NONE;
-  eNB_dlsch_info[mod_idP][pCC_id][UE_id].rnti                        = NOT_A_RNTI;
-  eNB_dlsch_info[mod_idP][pCC_id][UE_id].status                      = S_DL_NONE;
-
-  // check if this has an RA process active
-  RA_TEMPLATE *RA_template;
-  for (i=0;i<NB_RA_PROC_MAX;i++) {
-    RA_template = (RA_TEMPLATE *)&RC.mac[mod_idP]->common_channels[pCC_id].RA_template[i];
-    if (RA_template->rnti == rntiP){
-      RA_template->RA_active=FALSE;
-      RA_template->generate_rar=0;
-      RA_template->generate_Msg4=0;
-      RA_template->wait_ack_Msg4=0;
-      RA_template->timing_offset=0;
-      RA_template->RRC_timer=20;
-      RA_template->rnti = 0;
-      //break;
+    int i;
+    UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
+    int UE_id = find_UE_id(mod_idP, rntiP);
+    int pCC_id;
+
+    if (UE_id == -1) {
+	LOG_W(MAC, "rrc_mac_remove_ue: UE %x not found\n", rntiP);
+	return 0;
     }
-  }
 
-  return 0;
-}
+    pCC_id = UE_PCCID(mod_idP, UE_id);
 
-int prev(UE_list_t *listP, int nodeP, int ul_flag)
-{
-  int j;
+    LOG_I(MAC, "Removing UE %d from Primary CC_id %d (rnti %x)\n", UE_id,
+	  pCC_id, rntiP);
+    dump_ue_list(UE_list, 0);
 
-  if (ul_flag == 0 ) {
-    if (nodeP==listP->head) {
-      return(nodeP);
-    }
+    UE_list->active[UE_id] = FALSE;
+    UE_list->num_UEs--;
 
-    for (j=listP->head; j>=0; j=listP->next[j]) {
-      if (listP->next[j]==nodeP) {
-        return(j);
-    }
-    }
-  } else {
-    if (nodeP==listP->head_ul) {
-      return(nodeP);
-    }
-
-    for (j=listP->head_ul; j>=0; j=listP->next_ul[j]) {
-      if (listP->next_ul[j]==nodeP) {
-        return(j);
-      }
+    if (UE_list->head == UE_id)
+	UE_list->head = UE_list->next[UE_id];
+    else
+	UE_list->next[prev(UE_list, UE_id, 0)] = UE_list->next[UE_id];
+    if (UE_list->head_ul == UE_id)
+	UE_list->head_ul = UE_list->next_ul[UE_id];
+    else
+	UE_list->next_ul[prev(UE_list, UE_id, 0)] =
+	    UE_list->next_ul[UE_id];
+
+    // clear all remaining pending transmissions
+    UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID0] = 0;
+    UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID1] = 0;
+    UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID2] = 0;
+    UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID3] = 0;
+
+    UE_list->UE_template[pCC_id][UE_id].ul_SR = 0;
+    UE_list->UE_template[pCC_id][UE_id].rnti = NOT_A_RNTI;
+    UE_list->UE_template[pCC_id][UE_id].ul_active = FALSE;
+    eNB_ulsch_info[mod_idP][pCC_id][UE_id].rnti = NOT_A_RNTI;
+    eNB_ulsch_info[mod_idP][pCC_id][UE_id].status = S_UL_NONE;
+    eNB_dlsch_info[mod_idP][pCC_id][UE_id].rnti = NOT_A_RNTI;
+    eNB_dlsch_info[mod_idP][pCC_id][UE_id].status = S_DL_NONE;
+
+    // check if this has an RA process active
+    RA_t *ra;
+    for (i = 0; i < NB_RA_PROC_MAX; i++) {
+	ra = (RA_t *) & RC.mac[mod_idP]->common_channels[pCC_id].ra[i];
+	if (ra->rnti == rntiP) {
+	    ra->state = IDLE;
+	    ra->timing_offset = 0;
+	    ra->RRC_timer = 20;
+	    ra->rnti = 0;
+	    //break;
+	}
     }
-  }
-
-  LOG_E(MAC,"error in prev(), could not find previous to %d in UE_list %s, should never happen, Dumping UE list\n",
-        nodeP, (ul_flag == 0)? "DL" : "UL");
-  dump_ue_list(listP, ul_flag);
 
-  return(-1);
+    return 0;
 }
 
-void swap_UEs(UE_list_t *listP,int nodeiP, int nodejP, int ul_flag)
+int prev(UE_list_t * listP, int nodeP, int ul_flag)
 {
-  int prev_i,prev_j,next_i,next_j;
-
-  LOG_T(MAC,"Swapping UE %d,%d\n",nodeiP,nodejP);
-  dump_ue_list(listP,ul_flag);
-
-  prev_i = prev(listP,nodeiP,ul_flag);
-  prev_j = prev(listP,nodejP,ul_flag);
+    int j;
+
+    if (ul_flag == 0) {
+	if (nodeP == listP->head) {
+	    return (nodeP);
+	}
+
+	for (j = listP->head; j >= 0; j = listP->next[j]) {
+	    if (listP->next[j] == nodeP) {
+		return (j);
+	    }
+	}
+    } else {
+	if (nodeP == listP->head_ul) {
+	    return (nodeP);
+	}
+
+	for (j = listP->head_ul; j >= 0; j = listP->next_ul[j]) {
+	    if (listP->next_ul[j] == nodeP) {
+		return (j);
+	    }
+	}
+    }
 
-  AssertFatal((prev_i>=0) && (prev_j>=0),
-              "swap_UEs: problem");
+    LOG_E(MAC,
+	  "error in prev(), could not find previous to %d in UE_list %s, should never happen, Dumping UE list\n",
+	  nodeP, (ul_flag == 0) ? "DL" : "UL");
+    dump_ue_list(listP, ul_flag);
 
-  if (ul_flag == 0) {
-    next_i = listP->next[nodeiP];
-    next_j = listP->next[nodejP];
-  } else {
-    next_i = listP->next_ul[nodeiP];
-    next_j = listP->next_ul[nodejP];
-  }
+    return (-1);
+}
 
-  LOG_T(MAC,"[%s] next_i %d, next_i, next_j %d, head %d \n",
-        (ul_flag == 0)? "DL" : "UL",
-        next_i,next_j,listP->head);
+void swap_UEs(UE_list_t * listP, int nodeiP, int nodejP, int ul_flag)
+{
+    int prev_i, prev_j, next_i, next_j;
 
-  if (ul_flag == 0 ) {
+    LOG_T(MAC, "Swapping UE %d,%d\n", nodeiP, nodejP);
+    dump_ue_list(listP, ul_flag);
 
-    if (next_i == nodejP) {   // case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...
-      LOG_T(MAC,"Case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...\n");
+    prev_i = prev(listP, nodeiP, ul_flag);
+    prev_j = prev(listP, nodejP, ul_flag);
 
-      listP->next[nodeiP] = next_j;
-      listP->next[nodejP] = nodeiP;
+    AssertFatal((prev_i >= 0) && (prev_j >= 0), "swap_UEs: problem");
 
-      if (nodeiP==listP->head) { // case i j n(j)
-        listP->head = nodejP;
-      } else {
-        listP->next[prev_i] = nodejP;
-      }
-    } else if (next_j == nodeiP) {  // case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...
-      LOG_T(MAC,"Case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...\n");
-      listP->next[nodejP] = next_i;
-      listP->next[nodeiP] = nodejP;
-
-      if (nodejP==listP->head) { // case j i n(i)
-        listP->head = nodeiP;
-      } else {
-        listP->next[prev_j] = nodeiP;
-      }
-    } else {  // case ...  p(i) i n(i) ... p(j) j n(j) ...
-      listP->next[nodejP] = next_i;
-      listP->next[nodeiP] = next_j;
-
-      if (nodeiP==listP->head) {
-        LOG_T(MAC,"changing head to %d\n",nodejP);
-        listP->head=nodejP;
-        listP->next[prev_j] = nodeiP;
-      } else if (nodejP==listP->head) {
-        LOG_D(MAC,"changing head to %d\n",nodeiP);
-        listP->head=nodeiP;
-        listP->next[prev_i] = nodejP;
-      } else {
-        listP->next[prev_i] = nodejP;
-        listP->next[prev_j] = nodeiP;
-      }
+    if (ul_flag == 0) {
+	next_i = listP->next[nodeiP];
+	next_j = listP->next[nodejP];
+    } else {
+	next_i = listP->next_ul[nodeiP];
+	next_j = listP->next_ul[nodejP];
     }
-  } else { // ul_flag
 
-    if (next_i == nodejP) {   // case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...
-      LOG_T(MAC,"[UL] Case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...\n");
-
-      listP->next_ul[nodeiP] = next_j;
-      listP->next_ul[nodejP] = nodeiP;
-
-      if (nodeiP==listP->head_ul) { // case i j n(j)
-        listP->head_ul = nodejP;
-      } else {
-        listP->next_ul[prev_i] = nodejP;
-      }
-    } else if (next_j == nodeiP) {  // case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...
-      LOG_T(MAC,"[UL]Case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...\n");
-      listP->next_ul[nodejP] = next_i;
-      listP->next_ul[nodeiP] = nodejP;
-
-      if (nodejP==listP->head_ul) { // case j i n(i)
-        listP->head_ul = nodeiP;
-      } else {
-        listP->next_ul[prev_j] = nodeiP;
-      }
-    } else {  // case ...  p(i) i n(i) ... p(j) j n(j) ...
-
-      listP->next_ul[nodejP] = next_i;
-      listP->next_ul[nodeiP] = next_j;
-
-      if (nodeiP==listP->head_ul) {
-        LOG_T(MAC,"[UL]changing head to %d\n",nodejP);
-        listP->head_ul=nodejP;
-        listP->next_ul[prev_j] = nodeiP;
-      } else if (nodejP==listP->head_ul) {
-        LOG_T(MAC,"[UL]changing head to %d\n",nodeiP);
-        listP->head_ul=nodeiP;
-        listP->next_ul[prev_i] = nodejP;
-      } else {
-        listP->next_ul[prev_i] = nodejP;
-        listP->next_ul[prev_j] = nodeiP;
-      }
+    LOG_T(MAC, "[%s] next_i %d, next_i, next_j %d, head %d \n",
+	  (ul_flag == 0) ? "DL" : "UL", next_i, next_j, listP->head);
+
+    if (ul_flag == 0) {
+
+	if (next_i == nodejP) {	// case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...
+	    LOG_T(MAC,
+		  "Case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...\n");
+
+	    listP->next[nodeiP] = next_j;
+	    listP->next[nodejP] = nodeiP;
+
+	    if (nodeiP == listP->head) {	// case i j n(j)
+		listP->head = nodejP;
+	    } else {
+		listP->next[prev_i] = nodejP;
+	    }
+	} else if (next_j == nodeiP) {	// case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...
+	    LOG_T(MAC,
+		  "Case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...\n");
+	    listP->next[nodejP] = next_i;
+	    listP->next[nodeiP] = nodejP;
+
+	    if (nodejP == listP->head) {	// case j i n(i)
+		listP->head = nodeiP;
+	    } else {
+		listP->next[prev_j] = nodeiP;
+	    }
+	} else {		// case ...  p(i) i n(i) ... p(j) j n(j) ...
+	    listP->next[nodejP] = next_i;
+	    listP->next[nodeiP] = next_j;
+
+	    if (nodeiP == listP->head) {
+		LOG_T(MAC, "changing head to %d\n", nodejP);
+		listP->head = nodejP;
+		listP->next[prev_j] = nodeiP;
+	    } else if (nodejP == listP->head) {
+		LOG_D(MAC, "changing head to %d\n", nodeiP);
+		listP->head = nodeiP;
+		listP->next[prev_i] = nodejP;
+	    } else {
+		listP->next[prev_i] = nodejP;
+		listP->next[prev_j] = nodeiP;
+	    }
+	}
+    } else {			// ul_flag
+
+	if (next_i == nodejP) {	// case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...
+	    LOG_T(MAC,
+		  "[UL] Case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...\n");
+
+	    listP->next_ul[nodeiP] = next_j;
+	    listP->next_ul[nodejP] = nodeiP;
+
+	    if (nodeiP == listP->head_ul) {	// case i j n(j)
+		listP->head_ul = nodejP;
+	    } else {
+		listP->next_ul[prev_i] = nodejP;
+	    }
+	} else if (next_j == nodeiP) {	// case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...
+	    LOG_T(MAC,
+		  "[UL]Case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...\n");
+	    listP->next_ul[nodejP] = next_i;
+	    listP->next_ul[nodeiP] = nodejP;
+
+	    if (nodejP == listP->head_ul) {	// case j i n(i)
+		listP->head_ul = nodeiP;
+	    } else {
+		listP->next_ul[prev_j] = nodeiP;
+	    }
+	} else {		// case ...  p(i) i n(i) ... p(j) j n(j) ...
+
+	    listP->next_ul[nodejP] = next_i;
+	    listP->next_ul[nodeiP] = next_j;
+
+	    if (nodeiP == listP->head_ul) {
+		LOG_T(MAC, "[UL]changing head to %d\n", nodejP);
+		listP->head_ul = nodejP;
+		listP->next_ul[prev_j] = nodeiP;
+	    } else if (nodejP == listP->head_ul) {
+		LOG_T(MAC, "[UL]changing head to %d\n", nodeiP);
+		listP->head_ul = nodeiP;
+		listP->next_ul[prev_i] = nodejP;
+	    } else {
+		listP->next_ul[prev_i] = nodejP;
+		listP->next_ul[prev_j] = nodeiP;
+	    }
+	}
     }
-  }
 
-  LOG_T(MAC,"After swap\n");
-  dump_ue_list(listP,ul_flag);
+    LOG_T(MAC, "After swap\n");
+    dump_ue_list(listP, ul_flag);
 }
 
 /*
@@ -2066,818 +2417,959 @@ void swap_UEs(UE_list_t *listP,int nodeiP, int nodejP, int ul_flag)
  */
 
 // This has to be updated to include BSR information
-uint8_t UE_is_to_be_scheduled(module_id_t module_idP,int CC_id,uint8_t UE_id)
+uint8_t
+UE_is_to_be_scheduled(module_id_t module_idP, int CC_id, uint8_t UE_id)
 {
-  UE_TEMPLATE *UE_template    = &RC.mac[module_idP]->UE_list.UE_template[CC_id][UE_id];
-  UE_sched_ctrl *UE_sched_ctl = &RC.mac[module_idP]->UE_list.UE_sched_ctrl[UE_id];
-
-  // do not schedule UE if UL is not working
-  if (UE_sched_ctl->ul_failure_timer>0)
-    return(0);
-  if (UE_sched_ctl->ul_out_of_sync>0)
-    return(0);
-
-  LOG_D(MAC,"[eNB %d][PUSCH] Checking UL requirements UE %d/%x\n",module_idP,UE_id,UE_RNTI(module_idP,UE_id));
-
-  if ((UE_template->bsr_info[LCGID0]>0) ||
-      (UE_template->bsr_info[LCGID1]>0) ||
-      (UE_template->bsr_info[LCGID2]>0) ||
-      (UE_template->bsr_info[LCGID3]>0) ||
-      (UE_template->ul_SR>0) || // uplink scheduling request
-      ((UE_sched_ctl->ul_inactivity_timer>20)&&
-       (UE_sched_ctl->ul_scheduled==0))||  // every 2 frames when RRC_CONNECTED
-      ((UE_sched_ctl->ul_inactivity_timer>10)&&
-       (UE_sched_ctl->ul_scheduled==0)&&
-       (mac_eNB_get_rrc_status(module_idP,UE_RNTI(module_idP,UE_id)) < RRC_CONNECTED))) // every Frame when not RRC_CONNECTED
-  {
-    LOG_D(MAC,"[eNB %d][PUSCH] UE %d/%x should be scheduled (BSR0 %d,SR %d)\n",module_idP,UE_id,UE_RNTI(module_idP,UE_id),
-          UE_template->bsr_info[LCGID0],
-          UE_template->ul_SR);
-    return(1);
-  } else {
-    return(0);
-  }
+    UE_TEMPLATE *UE_template =
+	&RC.mac[module_idP]->UE_list.UE_template[CC_id][UE_id];
+    UE_sched_ctrl *UE_sched_ctl =
+	&RC.mac[module_idP]->UE_list.UE_sched_ctrl[UE_id];
+
+    // do not schedule UE if UL is not working
+    if (UE_sched_ctl->ul_failure_timer > 0)
+	return (0);
+    if (UE_sched_ctl->ul_out_of_sync > 0)
+	return (0);
+
+    LOG_D(MAC, "[eNB %d][PUSCH] Checking UL requirements UE %d/%x\n",
+	  module_idP, UE_id, UE_RNTI(module_idP, UE_id));
+
+    if ((UE_template->bsr_info[LCGID0] > 0) || (UE_template->bsr_info[LCGID1] > 0) || (UE_template->bsr_info[LCGID2] > 0) || (UE_template->bsr_info[LCGID3] > 0) || (UE_template->ul_SR > 0) ||	// uplink scheduling request
+	((UE_sched_ctl->ul_inactivity_timer > 20) && (UE_sched_ctl->ul_scheduled == 0)) ||	// every 2 frames when RRC_CONNECTED
+	((UE_sched_ctl->ul_inactivity_timer > 10) && (UE_sched_ctl->ul_scheduled == 0) && (mac_eNB_get_rrc_status(module_idP, UE_RNTI(module_idP, UE_id)) < RRC_CONNECTED)))	// every Frame when not RRC_CONNECTED
+    {
+	LOG_D(MAC,
+	      "[eNB %d][PUSCH] UE %d/%x should be scheduled (BSR0 %d,SR %d)\n",
+	      module_idP, UE_id, UE_RNTI(module_idP, UE_id),
+	      UE_template->bsr_info[LCGID0], UE_template->ul_SR);
+	return (1);
+    } else {
+	return (0);
+    }
 }
 
-uint8_t get_tmode(module_id_t module_idP,int CC_idP,int UE_idP)
+uint8_t get_tmode(module_id_t module_idP, int CC_idP, int UE_idP)
 {
-  eNB_MAC_INST         *eNB                                = RC.mac[module_idP];
-  COMMON_channels_t    *cc                                 = &eNB->common_channels[CC_idP];
-
-  struct PhysicalConfigDedicated  *physicalConfigDedicated = eNB->UE_list.physicalConfigDedicated[CC_idP][UE_idP];
-
-  if (physicalConfigDedicated == NULL ) { // RRCConnectionSetup not received by UE yet
-    AssertFatal(cc->p_eNB<=2,"p_eNB is %d, should be <2\n",cc->p_eNB);
-    return(cc->p_eNB);
-  }
-  else {
-    AssertFatal(physicalConfigDedicated->antennaInfo!=NULL,
-                "antennaInfo is null for CCId %d, UEid %d\n",CC_idP,UE_idP);
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    COMMON_channels_t *cc = &eNB->common_channels[CC_idP];
 
-    AssertFatal(physicalConfigDedicated->antennaInfo->present != PhysicalConfigDedicated__antennaInfo_PR_NOTHING,
-                "antennaInfo (mod_id %d, CC_id %d) is set to NOTHING\n",module_idP,CC_idP);
+    struct PhysicalConfigDedicated *physicalConfigDedicated =
+	eNB->UE_list.physicalConfigDedicated[CC_idP][UE_idP];
 
-    if (physicalConfigDedicated->antennaInfo->present == PhysicalConfigDedicated__antennaInfo_PR_explicitValue) {
-      return(physicalConfigDedicated->antennaInfo->choice.explicitValue.transmissionMode);
-    }
-    else if (physicalConfigDedicated->antennaInfo->present == PhysicalConfigDedicated__antennaInfo_PR_defaultValue) {
-      AssertFatal(cc->p_eNB<=2,"p_eNB is %d, should be <2\n",cc->p_eNB);
-      return(cc->p_eNB);
+    if (physicalConfigDedicated == NULL) {	// RRCConnectionSetup not received by UE yet
+	AssertFatal(cc->p_eNB <= 2, "p_eNB is %d, should be <2\n",
+		    cc->p_eNB);
+	return (cc->p_eNB);
+    } else {
+	AssertFatal(physicalConfigDedicated->antennaInfo != NULL,
+		    "antennaInfo is null for CCId %d, UEid %d\n", CC_idP,
+		    UE_idP);
+
+	AssertFatal(physicalConfigDedicated->antennaInfo->present !=
+		    PhysicalConfigDedicated__antennaInfo_PR_NOTHING,
+		    "antennaInfo (mod_id %d, CC_id %d) is set to NOTHING\n",
+		    module_idP, CC_idP);
+
+	if (physicalConfigDedicated->antennaInfo->present ==
+	    PhysicalConfigDedicated__antennaInfo_PR_explicitValue) {
+	    return (physicalConfigDedicated->antennaInfo->
+		    choice.explicitValue.transmissionMode);
+	} else if (physicalConfigDedicated->antennaInfo->present ==
+		   PhysicalConfigDedicated__antennaInfo_PR_defaultValue) {
+	    AssertFatal(cc->p_eNB <= 2, "p_eNB is %d, should be <2\n",
+			cc->p_eNB);
+	    return (cc->p_eNB);
+	} else
+	    AssertFatal(1 == 0, "Shouldn't be here\n");
     }
-    else AssertFatal(1==0,"Shouldn't be here\n");
-  }
 }
 
-int8_t get_ULharq(module_id_t module_idP,int CC_idP,uint16_t frameP,uint8_t subframeP)
+int8_t
+get_ULharq(module_id_t module_idP, int CC_idP, uint16_t frameP,
+	   uint8_t subframeP)
 {
-  uint8_t           ret       = -1;
-  eNB_MAC_INST      *eNB      = RC.mac[module_idP];
-  COMMON_channels_t *cc       = &eNB->common_channels[CC_idP];
-
-  if (cc->tdd_Config==NULL) { // FDD
-    ret = (((frameP<<1)+subframeP)&7);
-  } else {
-    switch (cc->tdd_Config->subframeAssignment) {
-    case 1:
-      if ((subframeP==2) ||
-          (subframeP==3) ||
-          (subframeP==7) ||
-          (subframeP==8))
-        switch (subframeP) {
-        case 2:
-        case 3:
-          ret = (subframeP-2);
-          break;
-
-        case 7:
-        case 8:
-          ret = (subframeP-5);
-          break;
-
-        default:
-          AssertFatal(1==0,"subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframeP,(int)cc->tdd_Config->subframeAssignment);
-          break;
-        }
-
-      break;
+    uint8_t ret = -1;
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    COMMON_channels_t *cc = &eNB->common_channels[CC_idP];
 
-    case 2:
-      AssertFatal((subframeP==2) || (subframeP==7),
-                  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframeP,(int)cc->tdd_Config->subframeAssignment);
-      ret = (subframeP/7);
-      break;
-
-    case 3:
-      AssertFatal((subframeP>1) && (subframeP<5),
-                  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframeP,(int)cc->tdd_Config->subframeAssignment);
-      ret = (subframeP-2);
-      break;
-
-    case 4:
-      AssertFatal((subframeP>1) && (subframeP<4),
-                  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframeP,(int)cc->tdd_Config->subframeAssignment);
-      ret = (subframeP-2);
-      break;
-
-    case 5:
-      AssertFatal(subframeP==2,
-                  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframeP,(int)cc->tdd_Config->subframeAssignment);
-      ret = (subframeP-2);
-      break;
-
-    default:
-      AssertFatal(1==0,"subframe2_harq_pid, Unsupported TDD mode %d\n",(int)cc->tdd_Config->subframeAssignment);
-      break;
+    if (cc->tdd_Config == NULL) {	// FDD
+	ret = (((frameP << 1) + subframeP) & 7);
+    } else {
+	switch (cc->tdd_Config->subframeAssignment) {
+	case 1:
+	    if ((subframeP == 2) ||
+		(subframeP == 3) || (subframeP == 7) || (subframeP == 8))
+		switch (subframeP) {
+		case 2:
+		case 3:
+		    ret = (subframeP - 2);
+		    break;
+
+		case 7:
+		case 8:
+		    ret = (subframeP - 5);
+		    break;
+
+		default:
+		    AssertFatal(1 == 0,
+				"subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
+				subframeP,
+				(int) cc->tdd_Config->subframeAssignment);
+		    break;
+		}
+
+	    break;
+
+	case 2:
+	    AssertFatal((subframeP == 2) || (subframeP == 7),
+			"subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
+			subframeP,
+			(int) cc->tdd_Config->subframeAssignment);
+	    ret = (subframeP / 7);
+	    break;
+
+	case 3:
+	    AssertFatal((subframeP > 1) && (subframeP < 5),
+			"subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
+			subframeP,
+			(int) cc->tdd_Config->subframeAssignment);
+	    ret = (subframeP - 2);
+	    break;
+
+	case 4:
+	    AssertFatal((subframeP > 1) && (subframeP < 4),
+			"subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
+			subframeP,
+			(int) cc->tdd_Config->subframeAssignment);
+	    ret = (subframeP - 2);
+	    break;
+
+	case 5:
+	    AssertFatal(subframeP == 2,
+			"subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
+			subframeP,
+			(int) cc->tdd_Config->subframeAssignment);
+	    ret = (subframeP - 2);
+	    break;
+
+	default:
+	    AssertFatal(1 == 0,
+			"subframe2_harq_pid, Unsupported TDD mode %d\n",
+			(int) cc->tdd_Config->subframeAssignment);
+	    break;
+	}
     }
-  }
 
-  AssertFatal(ret!=-1,
-              "invalid harq_pid(%d) at SFN/SF = %d/%d\n", (int8_t)ret, frameP, subframeP);
-  return ret;
+    AssertFatal(ret != -1,
+		"invalid harq_pid(%d) at SFN/SF = %d/%d\n", (int8_t) ret,
+		frameP, subframeP);
+    return ret;
 }
 
 
-uint16_t getRIV(uint16_t N_RB_DL,uint16_t RBstart,uint16_t Lcrbs)
+uint16_t getRIV(uint16_t N_RB_DL, uint16_t RBstart, uint16_t Lcrbs)
 {
-  uint16_t RIV;
+    uint16_t RIV;
 
-  if (Lcrbs<=(1+(N_RB_DL>>1)))
-    RIV = (N_RB_DL*(Lcrbs-1)) + RBstart;
-  else
-    RIV = (N_RB_DL*(N_RB_DL+1-Lcrbs)) + (N_RB_DL-1-RBstart);
+    if (Lcrbs <= (1 + (N_RB_DL >> 1)))
+	RIV = (N_RB_DL * (Lcrbs - 1)) + RBstart;
+    else
+	RIV = (N_RB_DL * (N_RB_DL + 1 - Lcrbs)) + (N_RB_DL - 1 - RBstart);
 
-  return(RIV);
+    return (RIV);
 }
 
-uint32_t allocate_prbs(int UE_id,unsigned char nb_rb, int N_RB_DL, uint32_t *rballoc)
+uint32_t
+allocate_prbs(int UE_id, unsigned char nb_rb, int N_RB_DL,
+	      uint32_t * rballoc)
 {
-  int i;
-  uint32_t rballoc_dci=0;
-  unsigned char nb_rb_alloc=0;
-
-  for (i=0; i<(N_RB_DL-2); i+=2) {
-    if (((*rballoc>>i)&3)==0) {
-      *rballoc |= (3<<i);
-      rballoc_dci |= (1<<((12-i)>>1));
-      nb_rb_alloc+=2;
+    int i;
+    uint32_t rballoc_dci = 0;
+    unsigned char nb_rb_alloc = 0;
+
+    for (i = 0; i < (N_RB_DL - 2); i += 2) {
+	if (((*rballoc >> i) & 3) == 0) {
+	    *rballoc |= (3 << i);
+	    rballoc_dci |= (1 << ((12 - i) >> 1));
+	    nb_rb_alloc += 2;
+	}
+
+	if (nb_rb_alloc == nb_rb) {
+	    return (rballoc_dci);
+	}
     }
 
-    if (nb_rb_alloc==nb_rb) {
-      return(rballoc_dci);
+    if ((N_RB_DL & 1) == 1) {
+	if ((*rballoc >> (N_RB_DL - 1) & 1) == 0) {
+	    *rballoc |= (1 << (N_RB_DL - 1));
+	    rballoc_dci |= 1;
+	}
     }
-  }
 
-  if ((N_RB_DL&1)==1) {
-    if ((*rballoc>>(N_RB_DL-1)&1)==0) {
-      *rballoc |= (1<<(N_RB_DL-1));
-      rballoc_dci |= 1;
-    }
-  }
-
-  return(rballoc_dci);
+    return (rballoc_dci);
 }
 
 int get_bw_index(module_id_t module_id, uint8_t CC_id)
 {
-  int bw_index=0;
+    int bw_index = 0;
 
-  int N_RB_DL = to_prb(RC.mac[module_id]->common_channels[CC_id].mib->message.dl_Bandwidth);
+    int N_RB_DL =
+	to_prb(RC.mac[module_id]->common_channels[CC_id].mib->
+	       message.dl_Bandwidth);
 
-  switch (N_RB_DL) {
-  case 6: // 1.4 MHz
-    bw_index=0;
-    break;
+    switch (N_RB_DL) {
+    case 6:			// 1.4 MHz
+	bw_index = 0;
+	break;
 
-  case 25: // 5HMz
-    bw_index=1;
-    break;
+    case 25:			// 5HMz
+	bw_index = 1;
+	break;
 
-  case 50: // 10HMz
-    bw_index=2;
-    break;
+    case 50:			// 10HMz
+	bw_index = 2;
+	break;
 
-  case 100: // 20HMz
-    bw_index=3;
-    break;
+    case 100:			// 20HMz
+	bw_index = 3;
+	break;
 
-  default:
-    bw_index=1;
-    LOG_W(MAC,"[eNB %d] N_RB_DL %d unknown for CC_id %d, setting bw_index to 1\n", module_id, N_RB_DL,CC_id);
-    break;
-  }
+    default:
+	bw_index = 1;
+	LOG_W(MAC,
+	      "[eNB %d] N_RB_DL %d unknown for CC_id %d, setting bw_index to 1\n",
+	      module_id, N_RB_DL, CC_id);
+	break;
+    }
 
-  return bw_index;
+    return bw_index;
 }
 
 int get_min_rb_unit(module_id_t module_id, uint8_t CC_id)
 {
-  int min_rb_unit=0;
-  int N_RB_DL = to_prb(RC.mac[module_id]->common_channels[CC_id].mib->message.dl_Bandwidth);
+    int min_rb_unit = 0;
+    int N_RB_DL =
+	to_prb(RC.mac[module_id]->common_channels[CC_id].mib->
+	       message.dl_Bandwidth);
 
-  switch (N_RB_DL) {
-  case 6: // 1.4 MHz
-    min_rb_unit=1;
-    break;
+    switch (N_RB_DL) {
+    case 6:			// 1.4 MHz
+	min_rb_unit = 1;
+	break;
 
-  case 25: // 5HMz
-    min_rb_unit=2;
-    break;
+    case 25:			// 5HMz
+	min_rb_unit = 2;
+	break;
 
-  case 50: // 10HMz
-    min_rb_unit=3;
-    break;
+    case 50:			// 10HMz
+	min_rb_unit = 3;
+	break;
 
-  case 100: // 20HMz
-    min_rb_unit=4;
-    break;
+    case 100:			// 20HMz
+	min_rb_unit = 4;
+	break;
 
-  default:
-    min_rb_unit=2;
-    LOG_W(MAC,"[eNB %d] N_DL_RB %d unknown for CC_id %d, setting min_rb_unit to 2\n",
-          module_id, N_RB_DL, CC_id);
-    break;
-  }
+    default:
+	min_rb_unit = 2;
+	LOG_W(MAC,
+	      "[eNB %d] N_DL_RB %d unknown for CC_id %d, setting min_rb_unit to 2\n",
+	      module_id, N_RB_DL, CC_id);
+	break;
+    }
 
-  return min_rb_unit;
+    return min_rb_unit;
 }
 
-uint32_t allocate_prbs_sub(int nb_rb, int N_RB_DL, int N_RBG, uint8_t *rballoc)
+uint32_t
+allocate_prbs_sub(int nb_rb, int N_RB_DL, int N_RBG, uint8_t * rballoc)
 {
-  int check=0;//check1=0,check2=0;
-  uint32_t rballoc_dci=0;
-  //uint8_t number_of_subbands=13;
-
-  LOG_T(MAC,"*****Check1RBALLOC****: %d%d%d%d (nb_rb %d,N_RBG %d)\n",
-        rballoc[3],rballoc[2],rballoc[1],rballoc[0],nb_rb,N_RBG);
-
-  while((nb_rb >0) && (check < N_RBG)) {
-    //printf("rballoc[%d] %d\n",check,rballoc[check]);
-    if(rballoc[check] == 1) {
-      rballoc_dci |= (1<<((N_RBG-1)-check));
-
-      switch (N_RB_DL) {
-      case 6:
-        nb_rb--;
-        break;
-
-      case 25:
-        if ((check == N_RBG-1)) {
-          nb_rb--;
-        } else {
-          nb_rb-=2;
-        }
-
-        break;
-
-      case 50:
-        if ((check == N_RBG-1)) {
-          nb_rb-=2;
-        } else {
-          nb_rb-=3;
-        }
-
-        break;
-
-      case 100:
-        nb_rb-=4;
-        break;
-      }
+    int check = 0;		//check1=0,check2=0;
+    uint32_t rballoc_dci = 0;
+    //uint8_t number_of_subbands=13;
+
+    LOG_T(MAC, "*****Check1RBALLOC****: %d%d%d%d (nb_rb %d,N_RBG %d)\n",
+	  rballoc[3], rballoc[2], rballoc[1], rballoc[0], nb_rb, N_RBG);
+
+    while ((nb_rb > 0) && (check < N_RBG)) {
+	//printf("rballoc[%d] %d\n",check,rballoc[check]);
+	if (rballoc[check] == 1) {
+	    rballoc_dci |= (1 << ((N_RBG - 1) - check));
+
+	    switch (N_RB_DL) {
+	    case 6:
+		nb_rb--;
+		break;
+
+	    case 25:
+		if ((check == N_RBG - 1)) {
+		    nb_rb--;
+		} else {
+		    nb_rb -= 2;
+		}
+
+		break;
+
+	    case 50:
+		if ((check == N_RBG - 1)) {
+		    nb_rb -= 2;
+		} else {
+		    nb_rb -= 3;
+		}
+
+		break;
+
+	    case 100:
+		nb_rb -= 4;
+		break;
+	    }
+	}
+	//      printf("rb_alloc %x\n",rballoc_dci);
+	check = check + 1;
+	//    check1 = check1+2;
     }
 
-    //      printf("rb_alloc %x\n",rballoc_dci);
-    check = check+1;
-    //    check1 = check1+2;
-  }
-
-  // rballoc_dci = (rballoc_dci)&(0x1fff);
-  LOG_T(MAC,"*********RBALLOC : %x\n",rballoc_dci);
-  // exit(-1);
-  return (rballoc_dci);
+    // rballoc_dci = (rballoc_dci)&(0x1fff);
+    LOG_T(MAC, "*********RBALLOC : %x\n", rballoc_dci);
+    // exit(-1);
+    return (rballoc_dci);
 }
 
 int get_subbandsize(uint8_t dl_Bandwidth)
 {
-  uint8_t ss[6] = {6,4,4,6,8,8};
+    uint8_t ss[6] = { 6, 4, 4, 6, 8, 8 };
 
-  AssertFatal(dl_Bandwidth<6,"dl_Bandwidth %d is out of bounds\n",dl_Bandwidth);
+    AssertFatal(dl_Bandwidth < 6, "dl_Bandwidth %d is out of bounds\n",
+		dl_Bandwidth);
 
-  return(ss[dl_Bandwidth]);
+    return (ss[dl_Bandwidth]);
 }
 
 int get_nb_subband(int N_RB_DL)
 {
-  int nb_sb=0;
+    int nb_sb = 0;
 
-  switch (N_RB_DL) {
-  case 6:
-    nb_sb=0;
-    break;
+    switch (N_RB_DL) {
+    case 6:
+	nb_sb = 0;
+	break;
 
-  case 15:
-    nb_sb = 4;  // sb_size =4
+    case 15:
+	nb_sb = 4;		// sb_size =4
 
-  case 25:
-    nb_sb = 7; // sb_size =4, 1 sb with 1PRB, 6 with 2 RBG, each has 2 PRBs
-    break;
+    case 25:
+	nb_sb = 7;		// sb_size =4, 1 sb with 1PRB, 6 with 2 RBG, each has 2 PRBs
+	break;
 
-  case 50:    // sb_size =6
-    nb_sb = 9;
-    break;
+    case 50:			// sb_size =6
+	nb_sb = 9;
+	break;
 
-  case 75:  // sb_size =8
-    nb_sb = 10;
-    break;
+    case 75:			// sb_size =8
+	nb_sb = 10;
+	break;
 
-  case 100: // sb_size =8 , 1 sb with 1 RBG + 12 sb with 2RBG, each RBG has 4 PRBs
-    nb_sb = 13;
-    break;
+    case 100:			// sb_size =8 , 1 sb with 1 RBG + 12 sb with 2RBG, each RBG has 4 PRBs
+	nb_sb = 13;
+	break;
 
-  default:
-    nb_sb=0;
-    break;
-  }
+    default:
+	nb_sb = 0;
+	break;
+    }
 
-  return nb_sb;
+    return nb_sb;
 }
 
-void init_CCE_table(int module_idP,int CC_idP)
+void init_CCE_table(int module_idP, int CC_idP)
 {
-  memset(RC.mac[module_idP]->CCE_table[CC_idP],0,800*sizeof(int));
+    memset(RC.mac[module_idP]->CCE_table[CC_idP], 0, 800 * sizeof(int));
 }
 
 
-int get_nCCE_offset(int *CCE_table,
-                    const unsigned char L,
-                    const int nCCE,
-                    const int common_dci,
-                    const unsigned short rnti,
-                    const unsigned char subframe)
+int
+get_nCCE_offset(int *CCE_table,
+		const unsigned char L,
+		const int nCCE,
+		const int common_dci,
+		const unsigned short rnti, const unsigned char subframe)
 {
-  int search_space_free,m,nb_candidates = 0,l,i;
-  unsigned int Yk;
-   /*
-    printf("CCE Allocation: ");
-    for (i=0;i<nCCE;i++)
-    printf("%d.",CCE_table[i]);
-    printf("\n");
-  */
-  if (common_dci == 1) {
-    // check CCE(0 ... L-1)
-    nb_candidates = (L==4) ? 4 : 2;
-    nb_candidates = min(nb_candidates,nCCE/L);
+    int search_space_free, m, nb_candidates = 0, l, i;
+    unsigned int Yk;
+    /*
+       printf("CCE Allocation: ");
+       for (i=0;i<nCCE;i++)
+       printf("%d.",CCE_table[i]);
+       printf("\n");
+     */
+    if (common_dci == 1) {
+	// check CCE(0 ... L-1)
+	nb_candidates = (L == 4) ? 4 : 2;
+	nb_candidates = min(nb_candidates, nCCE / L);
 
-    //    printf("Common DCI nb_candidates %d, L %d\n",nb_candidates,L);
+	//    printf("Common DCI nb_candidates %d, L %d\n",nb_candidates,L);
 
-    for (m = nb_candidates-1 ; m >=0 ; m--) {
+	for (m = nb_candidates - 1; m >= 0; m--) {
 
-      search_space_free = 1;
-      for (l=0; l<L; l++) {
+	    search_space_free = 1;
+	    for (l = 0; l < L; l++) {
 
-        //        printf("CCE_table[%d] %d\n",(m*L)+l,CCE_table[(m*L)+l]);
-        if (CCE_table[(m*L) + l] == 1) {
-          search_space_free = 0;
-          break;
-        }
-      }
+		//        printf("CCE_table[%d] %d\n",(m*L)+l,CCE_table[(m*L)+l]);
+		if (CCE_table[(m * L) + l] == 1) {
+		    search_space_free = 0;
+		    break;
+		}
+	    }
 
-      if (search_space_free == 1) {
+	    if (search_space_free == 1) {
 
-        //        printf("returning %d\n",m*L);
+		//        printf("returning %d\n",m*L);
 
-        for (l=0; l<L; l++)
-          CCE_table[(m*L)+l]=1;
-        return(m*L);
-      }
-    }
+		for (l = 0; l < L; l++)
+		    CCE_table[(m * L) + l] = 1;
+		return (m * L);
+	    }
+	}
 
-    return(-1);
+	return (-1);
 
-  } else { // Find first available in ue specific search space
-    // according to procedure in Section 9.1.1 of 36.213 (v. 8.6)
-    // compute Yk
-    Yk = (unsigned int)rnti;
+    } else {			// Find first available in ue specific search space
+	// according to procedure in Section 9.1.1 of 36.213 (v. 8.6)
+	// compute Yk
+	Yk = (unsigned int) rnti;
 
-    for (i=0; i<=subframe; i++)
-      Yk = (Yk*39827)%65537;
+	for (i = 0; i <= subframe; i++)
+	    Yk = (Yk * 39827) % 65537;
 
-    Yk = Yk % (nCCE/L);
+	Yk = Yk % (nCCE / L);
 
-    switch (L) {
-    case 1:
-    case 2:
-      nb_candidates = 6;
-      break;
+	switch (L) {
+	case 1:
+	case 2:
+	    nb_candidates = 6;
+	    break;
 
-    case 4:
-    case 8:
-      nb_candidates = 2;
-      break;
+	case 4:
+	case 8:
+	    nb_candidates = 2;
+	    break;
 
-    default:
-      DevParam(L, nCCE, rnti);
-      break;
-    }
+	default:
+	    DevParam(L, nCCE, rnti);
+	    break;
+	}
 
-    LOG_D(MAC,"rnti %x, Yk = %d, nCCE %d (nCCE/L %d),nb_cand %d\n",rnti,Yk,nCCE,nCCE/L,nb_candidates);
+	LOG_D(MAC, "rnti %x, Yk = %d, nCCE %d (nCCE/L %d),nb_cand %d\n",
+	      rnti, Yk, nCCE, nCCE / L, nb_candidates);
 
-    for (m = 0 ; m < nb_candidates ; m++) {
-      search_space_free = 1;
+	for (m = 0; m < nb_candidates; m++) {
+	    search_space_free = 1;
 
-      for (l=0; l<L; l++) {
-        int cce = (((Yk+m)%(nCCE/L))*L) + l;
-        if (cce >= nCCE || CCE_table[cce] == 1) {
-          search_space_free = 0;
-          break;
-        }
-      }
+	    for (l = 0; l < L; l++) {
+		int cce = (((Yk + m) % (nCCE / L)) * L) + l;
+		if (cce >= nCCE || CCE_table[cce] == 1) {
+		    search_space_free = 0;
+		    break;
+		}
+	    }
 
-      if (search_space_free == 1) {
-        for (l=0; l<L; l++)
-          CCE_table[(((Yk+m)%(nCCE/L))*L)+l]=1;
+	    if (search_space_free == 1) {
+		for (l = 0; l < L; l++)
+		    CCE_table[(((Yk + m) % (nCCE / L)) * L) + l] = 1;
 
-        return(((Yk+m)%(nCCE/L))*L);
-      }
-    }
+		return (((Yk + m) % (nCCE / L)) * L);
+	    }
+	}
 
-    return(-1);
-  }
+	return (-1);
+    }
 }
 
-void dump_CCE_table(int *CCE_table,const int nCCE,const unsigned short rnti,const int subframe,int L)
+void
+dump_CCE_table(int *CCE_table, const int nCCE,
+	       const unsigned short rnti, const int subframe, int L)
 {
-  int nb_candidates = 0,i;
-  unsigned int Yk;
-
-  printf("CCE 0: ");
-  for (i=0;i<nCCE;i++) {
-    printf("%1d.",CCE_table[i]);
-    if ((i&7) == 7)
-      printf("\n CCE %d: ",i);
-  }
+    int nb_candidates = 0, i;
+    unsigned int Yk;
+
+    printf("CCE 0: ");
+    for (i = 0; i < nCCE; i++) {
+	printf("%1d.", CCE_table[i]);
+	if ((i & 7) == 7)
+	    printf("\n CCE %d: ", i);
+    }
 
-  Yk = (unsigned int)rnti;
+    Yk = (unsigned int) rnti;
 
-  for (i=0; i<=subframe; i++)
-    Yk = (Yk*39827)%65537;
+    for (i = 0; i <= subframe; i++)
+	Yk = (Yk * 39827) % 65537;
 
-  Yk = Yk % (nCCE/L);
+    Yk = Yk % (nCCE / L);
 
-  switch (L) {
-  case 1:
-  case 2:
-    nb_candidates = 6;
-    break;
+    switch (L) {
+    case 1:
+    case 2:
+	nb_candidates = 6;
+	break;
 
-  case 4:
-  case 8:
-    nb_candidates = 2;
-    break;
+    case 4:
+    case 8:
+	nb_candidates = 2;
+	break;
 
-  default:
-    DevParam(L, nCCE, rnti);
-    break;
-  }
+    default:
+	DevParam(L, nCCE, rnti);
+	break;
+    }
 
-  printf("rnti %x, Yk*L = %d, nCCE %d (nCCE/L %d),nb_cand*L %d\n",rnti,Yk*L,nCCE,nCCE/L,nb_candidates*L);
+    printf("rnti %x, Yk*L = %d, nCCE %d (nCCE/L %d),nb_cand*L %d\n", rnti,
+	   Yk * L, nCCE, nCCE / L, nb_candidates * L);
 }
 
-uint16_t getnquad(COMMON_channels_t *cc, uint8_t num_pdcch_symbols,uint8_t mi)
+uint16_t
+getnquad(COMMON_channels_t * cc, uint8_t num_pdcch_symbols, uint8_t mi)
 {
-  uint16_t Nreg=0;
+    uint16_t Nreg = 0;
 
-  AssertFatal(cc!=NULL,"cc is null\n");
-  AssertFatal(cc->mib!=NULL,"cc->mib is null\n");
+    AssertFatal(cc != NULL, "cc is null\n");
+    AssertFatal(cc->mib != NULL, "cc->mib is null\n");
 
-  int N_RB_DL        = to_prb(cc->mib->message.dl_Bandwidth);
-  int phich_resource = get_phich_resource_times6(cc);
+    int N_RB_DL = to_prb(cc->mib->message.dl_Bandwidth);
+    int phich_resource = get_phich_resource_times6(cc);
 
-  uint8_t Ngroup_PHICH = (phich_resource*N_RB_DL)/48;
+    uint8_t Ngroup_PHICH = (phich_resource * N_RB_DL) / 48;
 
-  if (((phich_resource*N_RB_DL)%48) > 0)
-    Ngroup_PHICH++;
+    if (((phich_resource * N_RB_DL) % 48) > 0)
+	Ngroup_PHICH++;
 
-  if (cc->Ncp == 1) {
-    Ngroup_PHICH<<=1;
-  }
-
-  Ngroup_PHICH*=mi;
+    if (cc->Ncp == 1) {
+	Ngroup_PHICH <<= 1;
+    }
 
-  if ((num_pdcch_symbols>0) && (num_pdcch_symbols<4))
-    switch (N_RB_DL) {
-    case 6:
-      Nreg=12+(num_pdcch_symbols-1)*18;
-      break;
+    Ngroup_PHICH *= mi;
 
-    case 25:
-      Nreg=50+(num_pdcch_symbols-1)*75;
-      break;
+    if ((num_pdcch_symbols > 0) && (num_pdcch_symbols < 4))
+	switch (N_RB_DL) {
+	case 6:
+	    Nreg = 12 + (num_pdcch_symbols - 1) * 18;
+	    break;
 
-    case 50:
-      Nreg=100+(num_pdcch_symbols-1)*150;
-      break;
+	case 25:
+	    Nreg = 50 + (num_pdcch_symbols - 1) * 75;
+	    break;
 
-    case 100:
-      Nreg=200+(num_pdcch_symbols-1)*300;
-      break;
+	case 50:
+	    Nreg = 100 + (num_pdcch_symbols - 1) * 150;
+	    break;
 
-    default:
-      return(0);
-    }
+	case 100:
+	    Nreg = 200 + (num_pdcch_symbols - 1) * 300;
+	    break;
 
-  //   printf("Nreg %d (%d)\n",Nreg,Nreg - 4 - (3*Ngroup_PHICH));
-  return(Nreg - 4 - (3*Ngroup_PHICH));
+	default:
+	    return (0);
+	}
+    //   printf("Nreg %d (%d)\n",Nreg,Nreg - 4 - (3*Ngroup_PHICH));
+    return (Nreg - 4 - (3 * Ngroup_PHICH));
 }
 
-uint16_t getnCCE(COMMON_channels_t *cc, uint8_t num_pdcch_symbols, uint8_t mi)
+uint16_t
+getnCCE(COMMON_channels_t * cc, uint8_t num_pdcch_symbols, uint8_t mi)
 {
-  AssertFatal(cc!=NULL,"cc is null\n");
-  return(getnquad(cc,num_pdcch_symbols,mi)/9);
+    AssertFatal(cc != NULL, "cc is null\n");
+    return (getnquad(cc, num_pdcch_symbols, mi) / 9);
 }
 
-uint8_t getmi(COMMON_channels_t *cc,int subframe)
+uint8_t getmi(COMMON_channels_t * cc, int subframe)
 {
-  AssertFatal(cc!=NULL,"cc is null\n");
+    AssertFatal(cc != NULL, "cc is null\n");
 
-  // for FDD
-  if (cc->tdd_Config==NULL) // FDD
-    return 1;
+    // for FDD
+    if (cc->tdd_Config == NULL)	// FDD
+	return 1;
 
-  // for TDD
-  switch (cc->tdd_Config->subframeAssignment) {
-  case 0:
-    if ((subframe==0) || (subframe==5))
-      return(2);
-    else return(1);
+    // for TDD
+    switch (cc->tdd_Config->subframeAssignment) {
+    case 0:
+	if ((subframe == 0) || (subframe == 5))
+	    return (2);
+	else
+	    return (1);
 
-    break;
+	break;
 
-  case 1:
-    if ((subframe==0) || (subframe==5))
-      return(0);
-    else return(1);
+    case 1:
+	if ((subframe == 0) || (subframe == 5))
+	    return (0);
+	else
+	    return (1);
 
-    break;
+	break;
 
-  case 2:
-    if ((subframe==3) || (subframe==8))
-      return(1);
-    else return(0);
+    case 2:
+	if ((subframe == 3) || (subframe == 8))
+	    return (1);
+	else
+	    return (0);
 
-    break;
+	break;
 
-  case 3:
-    if ((subframe==0) || (subframe==8) || (subframe==9))
-      return(1);
-    else return(0);
+    case 3:
+	if ((subframe == 0) || (subframe == 8) || (subframe == 9))
+	    return (1);
+	else
+	    return (0);
 
-    break;
+	break;
 
-  case 4:
-    if ((subframe==8) || (subframe==9))
-      return(1);
-    else return(0);
+    case 4:
+	if ((subframe == 8) || (subframe == 9))
+	    return (1);
+	else
+	    return (0);
 
-    break;
+	break;
 
-  case 5:
-    if (subframe==8)
-      return(1);
-    else return(0);
+    case 5:
+	if (subframe == 8)
+	    return (1);
+	else
+	    return (0);
 
-    break;
+	break;
 
-  case 6:
-    return(1);
-    break;
+    case 6:
+	return (1);
+	break;
 
-  default:
-    return(0);
-  }
+    default:
+	return (0);
+    }
 }
 
-uint16_t get_nCCE_max(COMMON_channels_t *cc, int num_pdcch_symbols,int subframe)
+uint16_t
+get_nCCE_max(COMMON_channels_t * cc, int num_pdcch_symbols, int subframe)
 {
-  AssertFatal(cc!=NULL,"cc is null\n");
-  return(getnCCE(cc,num_pdcch_symbols,
-                 getmi(cc,subframe)));
+    AssertFatal(cc != NULL, "cc is null\n");
+    return (getnCCE(cc, num_pdcch_symbols, getmi(cc, subframe)));
 }
 
 // Allocate the CCEs
-int allocate_CCEs(int module_idP,
-                  int CC_idP,
-                  int subframeP,
-                  int test_onlyP)
-{
-  int                                 *CCE_table           = RC.mac[module_idP]->CCE_table[CC_idP];
-  nfapi_dl_config_request_body_t      *DL_req              = &RC.mac[module_idP]->DL_req[CC_idP].dl_config_request_body;
-  nfapi_hi_dci0_request_body_t        *HI_DCI0_req         = &RC.mac[module_idP]->HI_DCI0_req[CC_idP].hi_dci0_request_body;
-  nfapi_dl_config_request_pdu_t       *dl_config_pdu       = &DL_req->dl_config_pdu_list[0];
-  nfapi_hi_dci0_request_pdu_t         *hi_dci0_pdu         = &HI_DCI0_req->hi_dci0_pdu_list[0];
-  int                                 nCCE_max             = get_nCCE_max(&RC.mac[module_idP]->common_channels[CC_idP],1,subframeP);
-  int fCCE;
-  int i,j,idci;
-  int nCCE=0;
-
-  LOG_D(MAC,"Allocate CCEs subframe %d, test %d : (DL PDU %d, DL DCI %d, UL %d)\n",subframeP,test_onlyP,DL_req->number_pdu,DL_req->number_dci,HI_DCI0_req->number_of_dci);
-  DL_req->number_pdcch_ofdm_symbols=1;
-
-try_again:
-  init_CCE_table(module_idP,CC_idP);
-  nCCE=0;
-
-  for (i=0,idci=0;i<DL_req->number_pdu;i++) {
-    // allocate DL common DCIs first
-    if ((dl_config_pdu[i].pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)&&
-        (dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti_type==2)
-        ) {
-      LOG_D(MAC,"Trying to allocate COMMON DCI %d/%d (%d,%d) : rnti %x, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
-            idci,DL_req->number_dci+HI_DCI0_req->number_of_dci,
-            DL_req->number_dci,HI_DCI0_req->number_of_dci,
-            dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti,
-            dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
-            nCCE,nCCE_max, DL_req->number_pdcch_ofdm_symbols);
-
-      if (nCCE + (dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level) > nCCE_max) {
-        if (DL_req->number_pdcch_ofdm_symbols == 3)
-          goto failed;
-        LOG_D(MAC,"Can't fit DCI allocations with %d PDCCH symbols, increasing by 1\n",DL_req->number_pdcch_ofdm_symbols);
-        DL_req->number_pdcch_ofdm_symbols++;
-        nCCE_max = get_nCCE_max(&RC.mac[module_idP]->common_channels[CC_idP],DL_req->number_pdcch_ofdm_symbols,subframeP);
-        goto try_again;
-      }
-
-      // number of CCEs left can potentially hold this allocation
-      fCCE = get_nCCE_offset(CCE_table,
-                             dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
-                             nCCE_max,
-                             1,
-                             dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti,
-                             subframeP);
-      if (fCCE == -1) {
-        if (DL_req->number_pdcch_ofdm_symbols == 3) {
-          LOG_D(MAC,"subframe %d: Dropping Allocation for RNTI %x\n",
-                subframeP,dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti);
-          for (j=0;j<=i;j++){
-            if (dl_config_pdu[j].pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
-              LOG_D(MAC,"DCI %d/%d (%d,%d) : rnti %x dci format %d, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
-                    j,DL_req->number_dci+HI_DCI0_req->number_of_dci,
-                    DL_req->number_dci,HI_DCI0_req->number_of_dci,
-                    dl_config_pdu[j].dci_dl_pdu.dci_dl_pdu_rel8.rnti,
-                    dl_config_pdu[j].dci_dl_pdu.dci_dl_pdu_rel8.dci_format,
-                    dl_config_pdu[j].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
-                    nCCE,nCCE_max,DL_req->number_pdcch_ofdm_symbols);
-          }
-          //dump_CCE_table(CCE_table,nCCE_max,subframeP,dci_alloc->rnti,1<<dci_alloc->L);
-          goto failed;
-        }
-        LOG_D(MAC,"Can't fit DCI allocations with %d PDCCH symbols (rnti condition), increasing by 1\n",DL_req->number_pdcch_ofdm_symbols);
-
-        DL_req->number_pdcch_ofdm_symbols++;
-        nCCE_max = get_nCCE_max(&RC.mac[module_idP]->common_channels[CC_idP],DL_req->number_pdcch_ofdm_symbols,subframeP);
-        goto try_again;
-      } // fCCE==-1
-
-      // the allocation is feasible, rnti rule passes
-      nCCE += dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level;
-      LOG_D(MAC,"Allocating at nCCE %d\n",fCCE);
-      if (test_onlyP == 0) {
-        dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.cce_idx=fCCE;
-        LOG_D(MAC,"Allocate COMMON DCI CCEs subframe %d, test %d => L %d fCCE %d\n",subframeP,test_onlyP,dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,fCCE);
-      }
-      idci++;
-    }
-  } // for i = 0 ... num_DL_DCIs
-
-  // no try to allocate UL DCIs
-  for (i=0;i<HI_DCI0_req->number_of_dci+HI_DCI0_req->number_of_hi;i++) {
-
-    // allocate UL DCIs
-    if (hi_dci0_pdu[i].pdu_type == NFAPI_HI_DCI0_DCI_PDU_TYPE) {
-
-      LOG_D(MAC,"Trying to allocate format 0 DCI %d/%d (%d,%d) : rnti %x, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
-            idci,DL_req->number_dci+HI_DCI0_req->number_of_dci,
-            DL_req->number_dci,HI_DCI0_req->number_of_dci,
-            hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.rnti,hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.aggregation_level,
-            nCCE,nCCE_max, DL_req->number_pdcch_ofdm_symbols);
-
-      if (nCCE + (hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.aggregation_level) > nCCE_max) {
-        if (DL_req->number_pdcch_ofdm_symbols == 3)
-          goto failed;
-        LOG_D(MAC,"Can't fit DCI allocations with %d PDCCH symbols, increasing by 1\n",DL_req->number_pdcch_ofdm_symbols);
-
-        DL_req->number_pdcch_ofdm_symbols++;
-        nCCE_max = get_nCCE_max(&RC.mac[module_idP]->common_channels[CC_idP],DL_req->number_pdcch_ofdm_symbols,subframeP);
-        goto try_again;
-      }
-
-      // number of CCEs left can potentially hold this allocation
-      fCCE = get_nCCE_offset(CCE_table,
-                             hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.aggregation_level,
-                             nCCE_max,
-                             0,
-                             hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.rnti,
-                             subframeP);
-      if (fCCE == -1) {
-        if (DL_req->number_pdcch_ofdm_symbols == 3) {
-          LOG_D(MAC,"subframe %d: Dropping Allocation for RNTI %x\n",
-                subframeP,hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.rnti);
-          for (j=0;j<=i;j++){
-            if (hi_dci0_pdu[j].pdu_type == NFAPI_HI_DCI0_DCI_PDU_TYPE)
-              LOG_D(MAC,"DCI %d/%d (%d,%d) : rnti %x dci format %d, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
-                    j,DL_req->number_dci+HI_DCI0_req->number_of_dci,
-                    DL_req->number_dci,HI_DCI0_req->number_of_dci,
-                    hi_dci0_pdu[j].dci_pdu.dci_pdu_rel8.rnti,
-                    hi_dci0_pdu[j].dci_pdu.dci_pdu_rel8.dci_format,
-                    hi_dci0_pdu[j].dci_pdu.dci_pdu_rel8.aggregation_level,
-                    nCCE,nCCE_max,DL_req->number_pdcch_ofdm_symbols);
-          }
-          //dump_CCE_table(CCE_table,nCCE_max,subframeP,dci_alloc->rnti,1<<dci_alloc->L);
-          goto failed;
-        }
-        LOG_D(MAC,"Can't fit DCI allocations with %d PDCCH symbols (rnti condition), increasing by 1\n",DL_req->number_pdcch_ofdm_symbols);
-
-        DL_req->number_pdcch_ofdm_symbols++;
-        nCCE_max = get_nCCE_max(&RC.mac[module_idP]->common_channels[CC_idP],DL_req->number_pdcch_ofdm_symbols,subframeP);
-        goto try_again;
-      } // fCCE==-1
-
-      // the allocation is feasible, rnti rule passes
-      nCCE += hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.aggregation_level;
-      LOG_D(MAC,"Allocating at nCCE %d\n",fCCE);
-      if (test_onlyP == 0) {
-        hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.cce_index=fCCE;
-        LOG_D(MAC,"Allocate CCEs subframe %d, test %d\n",subframeP,test_onlyP);
-      }
-      idci++;
-    }
-  } // for i = 0 ... num_UL_DCIs
-
-  for (i=0;i<DL_req->number_pdu;i++) {
-    // allocate DL UE specific DCIs
-    if ((dl_config_pdu[i].pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)&&
-        (dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti_type==1)) {
-      LOG_D(MAC,"Trying to allocate DL UE-SPECIFIC DCI %d/%d (%d,%d) : rnti %x, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
-            idci,DL_req->number_dci+HI_DCI0_req->number_of_dci,
-            DL_req->number_dci,HI_DCI0_req->number_of_dci,
-            dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti,dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
-            nCCE,nCCE_max, DL_req->number_pdcch_ofdm_symbols);
-
-      if (nCCE + (dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level) > nCCE_max) {
-        if (DL_req->number_pdcch_ofdm_symbols == 3)
-          goto failed;
-        LOG_D(MAC,"Can't fit DCI allocations with %d PDCCH symbols, increasing by 1\n",DL_req->number_pdcch_ofdm_symbols);
-
-        DL_req->number_pdcch_ofdm_symbols++;
-        nCCE_max = get_nCCE_max(&RC.mac[module_idP]->common_channels[CC_idP],DL_req->number_pdcch_ofdm_symbols,subframeP);
-        goto try_again;
-      }
-
-      // number of CCEs left can potentially hold this allocation
-      fCCE = get_nCCE_offset(CCE_table,
-                             dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
-                             nCCE_max,
-                             0,
-                             dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti,
-                             subframeP);
-      if (fCCE == -1) {
-        if (DL_req->number_pdcch_ofdm_symbols == 3) {
-          LOG_I(MAC,"subframe %d: Dropping Allocation for RNTI %x\n",
-                subframeP,dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti);
-          for (j=0;j<=i;j++){
-            if (dl_config_pdu[j].pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
-              LOG_I(MAC,"DCI %d/%d (%d,%d) : rnti %x dci format %d, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
-                    j,DL_req->number_dci+HI_DCI0_req->number_of_dci,
-                    DL_req->number_dci,HI_DCI0_req->number_of_dci,
-                    dl_config_pdu[j].dci_dl_pdu.dci_dl_pdu_rel8.rnti,
-                    dl_config_pdu[j].dci_dl_pdu.dci_dl_pdu_rel8.dci_format,
-                    dl_config_pdu[j].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
-                    nCCE,nCCE_max,DL_req->number_pdcch_ofdm_symbols);
-          }
-          //dump_CCE_table(CCE_table,nCCE_max,subframeP,dci_alloc->rnti,1<<dci_alloc->L);
-          goto failed;
-        }
-        LOG_D(MAC,"Can't fit DCI allocations with %d PDCCH symbols (rnti condition), increasing by 1\n",DL_req->number_pdcch_ofdm_symbols);
-
-        DL_req->number_pdcch_ofdm_symbols++;
-        nCCE_max = get_nCCE_max(&RC.mac[module_idP]->common_channels[CC_idP],DL_req->number_pdcch_ofdm_symbols,subframeP);
-        goto try_again;
-      } // fCCE==-1
-
-      // the allocation is feasible, rnti rule passes
-      nCCE += dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level;
-      LOG_D(MAC,"Allocating at nCCE %d\n",fCCE);
-      if (test_onlyP == 0) {
-        dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.cce_idx=fCCE;
-        LOG_D(MAC,"Allocate CCEs subframe %d, test %d\n",subframeP,test_onlyP);
-      }
-      idci++;
-    }
-  } // for i = 0 ... num_DL_DCIs
+int
+allocate_CCEs(int module_idP, int CC_idP, int subframeP, int test_onlyP)
+{
+    int *CCE_table = RC.mac[module_idP]->CCE_table[CC_idP];
+    nfapi_dl_config_request_body_t *DL_req =
+	&RC.mac[module_idP]->DL_req[CC_idP].dl_config_request_body;
+    nfapi_hi_dci0_request_body_t *HI_DCI0_req =
+	&RC.mac[module_idP]->HI_DCI0_req[CC_idP].hi_dci0_request_body;
+    nfapi_dl_config_request_pdu_t *dl_config_pdu =
+	&DL_req->dl_config_pdu_list[0];
+    nfapi_hi_dci0_request_pdu_t *hi_dci0_pdu =
+	&HI_DCI0_req->hi_dci0_pdu_list[0];
+    int nCCE_max =
+	get_nCCE_max(&RC.mac[module_idP]->common_channels[CC_idP], 1,
+		     subframeP);
+    int fCCE;
+    int i, j, idci;
+    int nCCE = 0;
+
+    LOG_D(MAC,
+	  "Allocate CCEs subframe %d, test %d : (DL PDU %d, DL DCI %d, UL %d)\n",
+	  subframeP, test_onlyP, DL_req->number_pdu, DL_req->number_dci,
+	  HI_DCI0_req->number_of_dci);
+    DL_req->number_pdcch_ofdm_symbols = 1;
+
+  try_again:
+    init_CCE_table(module_idP, CC_idP);
+    nCCE = 0;
+
+    for (i = 0, idci = 0; i < DL_req->number_pdu; i++) {
+	// allocate DL common DCIs first
+	if ((dl_config_pdu[i].pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
+	    && (dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti_type ==
+		2)) {
+	    LOG_D(MAC,
+		  "Trying to allocate COMMON DCI %d/%d (%d,%d) : rnti %x, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
+		  idci, DL_req->number_dci + HI_DCI0_req->number_of_dci,
+		  DL_req->number_dci, HI_DCI0_req->number_of_dci,
+		  dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti,
+		  dl_config_pdu[i].dci_dl_pdu.
+		  dci_dl_pdu_rel8.aggregation_level, nCCE, nCCE_max,
+		  DL_req->number_pdcch_ofdm_symbols);
+
+	    if (nCCE +
+		(dl_config_pdu[i].dci_dl_pdu.
+		 dci_dl_pdu_rel8.aggregation_level) > nCCE_max) {
+		if (DL_req->number_pdcch_ofdm_symbols == 3)
+		    goto failed;
+		LOG_D(MAC,
+		      "Can't fit DCI allocations with %d PDCCH symbols, increasing by 1\n",
+		      DL_req->number_pdcch_ofdm_symbols);
+		DL_req->number_pdcch_ofdm_symbols++;
+		nCCE_max =
+		    get_nCCE_max(&RC.mac[module_idP]->
+				 common_channels[CC_idP],
+				 DL_req->number_pdcch_ofdm_symbols,
+				 subframeP);
+		goto try_again;
+	    }
+	    // number of CCEs left can potentially hold this allocation
+	    fCCE = get_nCCE_offset(CCE_table,
+				   dl_config_pdu[i].
+				   dci_dl_pdu.dci_dl_pdu_rel8.
+				   aggregation_level, nCCE_max, 1,
+				   dl_config_pdu[i].
+				   dci_dl_pdu.dci_dl_pdu_rel8.rnti,
+				   subframeP);
+	    if (fCCE == -1) {
+		if (DL_req->number_pdcch_ofdm_symbols == 3) {
+		    LOG_D(MAC,
+			  "subframe %d: Dropping Allocation for RNTI %x\n",
+			  subframeP,
+			  dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.
+			  rnti);
+		    for (j = 0; j <= i; j++) {
+			if (dl_config_pdu[j].pdu_type ==
+			    NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
+			    LOG_D(MAC,
+				  "DCI %d/%d (%d,%d) : rnti %x dci format %d, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
+				  j,
+				  DL_req->number_dci +
+				  HI_DCI0_req->number_of_dci,
+				  DL_req->number_dci,
+				  HI_DCI0_req->number_of_dci,
+				  dl_config_pdu[j].
+				  dci_dl_pdu.dci_dl_pdu_rel8.rnti,
+				  dl_config_pdu[j].
+				  dci_dl_pdu.dci_dl_pdu_rel8.dci_format,
+				  dl_config_pdu[j].
+				  dci_dl_pdu.dci_dl_pdu_rel8.
+				  aggregation_level, nCCE, nCCE_max,
+				  DL_req->number_pdcch_ofdm_symbols);
+		    }
+		    //dump_CCE_table(CCE_table,nCCE_max,subframeP,dci_alloc->rnti,1<<dci_alloc->L);
+		    goto failed;
+		}
+		LOG_D(MAC,
+		      "Can't fit DCI allocations with %d PDCCH symbols (rnti condition), increasing by 1\n",
+		      DL_req->number_pdcch_ofdm_symbols);
+
+		DL_req->number_pdcch_ofdm_symbols++;
+		nCCE_max =
+		    get_nCCE_max(&RC.mac[module_idP]->
+				 common_channels[CC_idP],
+				 DL_req->number_pdcch_ofdm_symbols,
+				 subframeP);
+		goto try_again;
+	    }			// fCCE==-1
+
+	    // the allocation is feasible, rnti rule passes
+	    nCCE +=
+		dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.
+		aggregation_level;
+	    LOG_D(MAC, "Allocating at nCCE %d\n", fCCE);
+	    if (test_onlyP == 0) {
+		dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.cce_idx = fCCE;
+		LOG_D(MAC,
+		      "Allocate COMMON DCI CCEs subframe %d, test %d => L %d fCCE %d\n",
+		      subframeP, test_onlyP,
+		      dl_config_pdu[i].dci_dl_pdu.
+		      dci_dl_pdu_rel8.aggregation_level, fCCE);
+	    }
+	    idci++;
+	}
+    }				// for i = 0 ... num_DL_DCIs
+
+    // no try to allocate UL DCIs
+    for (i = 0; i < HI_DCI0_req->number_of_dci + HI_DCI0_req->number_of_hi;
+	 i++) {
+
+	// allocate UL DCIs
+	if (hi_dci0_pdu[i].pdu_type == NFAPI_HI_DCI0_DCI_PDU_TYPE) {
+
+	    LOG_D(MAC,
+		  "Trying to allocate format 0 DCI %d/%d (%d,%d) : rnti %x, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
+		  idci, DL_req->number_dci + HI_DCI0_req->number_of_dci,
+		  DL_req->number_dci, HI_DCI0_req->number_of_dci,
+		  hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.rnti,
+		  hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.aggregation_level,
+		  nCCE, nCCE_max, DL_req->number_pdcch_ofdm_symbols);
+
+	    if (nCCE +
+		(hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.aggregation_level) >
+		nCCE_max) {
+		if (DL_req->number_pdcch_ofdm_symbols == 3)
+		    goto failed;
+		LOG_D(MAC,
+		      "Can't fit DCI allocations with %d PDCCH symbols, increasing by 1\n",
+		      DL_req->number_pdcch_ofdm_symbols);
+
+		DL_req->number_pdcch_ofdm_symbols++;
+		nCCE_max =
+		    get_nCCE_max(&RC.mac[module_idP]->
+				 common_channels[CC_idP],
+				 DL_req->number_pdcch_ofdm_symbols,
+				 subframeP);
+		goto try_again;
+	    }
+	    // number of CCEs left can potentially hold this allocation
+	    fCCE = get_nCCE_offset(CCE_table,
+				   hi_dci0_pdu[i].dci_pdu.
+				   dci_pdu_rel8.aggregation_level,
+				   nCCE_max, 0,
+				   hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.
+				   rnti, subframeP);
+	    if (fCCE == -1) {
+		if (DL_req->number_pdcch_ofdm_symbols == 3) {
+		    LOG_D(MAC,
+			  "subframe %d: Dropping Allocation for RNTI %x\n",
+			  subframeP,
+			  hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.rnti);
+		    for (j = 0; j <= i; j++) {
+			if (hi_dci0_pdu[j].pdu_type ==
+			    NFAPI_HI_DCI0_DCI_PDU_TYPE)
+			    LOG_D(MAC,
+				  "DCI %d/%d (%d,%d) : rnti %x dci format %d, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
+				  j,
+				  DL_req->number_dci +
+				  HI_DCI0_req->number_of_dci,
+				  DL_req->number_dci,
+				  HI_DCI0_req->number_of_dci,
+				  hi_dci0_pdu[j].dci_pdu.dci_pdu_rel8.rnti,
+				  hi_dci0_pdu[j].dci_pdu.dci_pdu_rel8.
+				  dci_format,
+				  hi_dci0_pdu[j].dci_pdu.
+				  dci_pdu_rel8.aggregation_level, nCCE,
+				  nCCE_max,
+				  DL_req->number_pdcch_ofdm_symbols);
+		    }
+		    //dump_CCE_table(CCE_table,nCCE_max,subframeP,dci_alloc->rnti,1<<dci_alloc->L);
+		    goto failed;
+		}
+		LOG_D(MAC,
+		      "Can't fit DCI allocations with %d PDCCH symbols (rnti condition), increasing by 1\n",
+		      DL_req->number_pdcch_ofdm_symbols);
+
+		DL_req->number_pdcch_ofdm_symbols++;
+		nCCE_max =
+		    get_nCCE_max(&RC.mac[module_idP]->
+				 common_channels[CC_idP],
+				 DL_req->number_pdcch_ofdm_symbols,
+				 subframeP);
+		goto try_again;
+	    }			// fCCE==-1
+
+	    // the allocation is feasible, rnti rule passes
+	    nCCE += hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.aggregation_level;
+	    LOG_D(MAC, "Allocating at nCCE %d\n", fCCE);
+	    if (test_onlyP == 0) {
+		hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.cce_index = fCCE;
+		LOG_D(MAC, "Allocate CCEs subframe %d, test %d\n",
+		      subframeP, test_onlyP);
+	    }
+	    idci++;
+	}
+    }				// for i = 0 ... num_UL_DCIs
+
+    for (i = 0; i < DL_req->number_pdu; i++) {
+	// allocate DL UE specific DCIs
+	if ((dl_config_pdu[i].pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
+	    && (dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti_type ==
+		1)) {
+	    LOG_D(MAC,
+		  "Trying to allocate DL UE-SPECIFIC DCI %d/%d (%d,%d) : rnti %x, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
+		  idci, DL_req->number_dci + HI_DCI0_req->number_of_dci,
+		  DL_req->number_dci, HI_DCI0_req->number_of_dci,
+		  dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti,
+		  dl_config_pdu[i].dci_dl_pdu.
+		  dci_dl_pdu_rel8.aggregation_level, nCCE, nCCE_max,
+		  DL_req->number_pdcch_ofdm_symbols);
+
+	    if (nCCE +
+		(dl_config_pdu[i].dci_dl_pdu.
+		 dci_dl_pdu_rel8.aggregation_level) > nCCE_max) {
+		if (DL_req->number_pdcch_ofdm_symbols == 3)
+		    goto failed;
+		LOG_D(MAC,
+		      "Can't fit DCI allocations with %d PDCCH symbols, increasing by 1\n",
+		      DL_req->number_pdcch_ofdm_symbols);
+
+		DL_req->number_pdcch_ofdm_symbols++;
+		nCCE_max =
+		    get_nCCE_max(&RC.mac[module_idP]->
+				 common_channels[CC_idP],
+				 DL_req->number_pdcch_ofdm_symbols,
+				 subframeP);
+		goto try_again;
+	    }
+	    // number of CCEs left can potentially hold this allocation
+	    fCCE = get_nCCE_offset(CCE_table,
+				   dl_config_pdu[i].
+				   dci_dl_pdu.dci_dl_pdu_rel8.
+				   aggregation_level, nCCE_max, 0,
+				   dl_config_pdu[i].
+				   dci_dl_pdu.dci_dl_pdu_rel8.rnti,
+				   subframeP);
+	    if (fCCE == -1) {
+		if (DL_req->number_pdcch_ofdm_symbols == 3) {
+		    LOG_I(MAC,
+			  "subframe %d: Dropping Allocation for RNTI %x\n",
+			  subframeP,
+			  dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.
+			  rnti);
+		    for (j = 0; j <= i; j++) {
+			if (dl_config_pdu[j].pdu_type ==
+			    NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
+			    LOG_I(MAC,
+				  "DCI %d/%d (%d,%d) : rnti %x dci format %d, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
+				  j,
+				  DL_req->number_dci +
+				  HI_DCI0_req->number_of_dci,
+				  DL_req->number_dci,
+				  HI_DCI0_req->number_of_dci,
+				  dl_config_pdu[j].
+				  dci_dl_pdu.dci_dl_pdu_rel8.rnti,
+				  dl_config_pdu[j].
+				  dci_dl_pdu.dci_dl_pdu_rel8.dci_format,
+				  dl_config_pdu[j].
+				  dci_dl_pdu.dci_dl_pdu_rel8.
+				  aggregation_level, nCCE, nCCE_max,
+				  DL_req->number_pdcch_ofdm_symbols);
+		    }
+		    //dump_CCE_table(CCE_table,nCCE_max,subframeP,dci_alloc->rnti,1<<dci_alloc->L);
+		    goto failed;
+		}
+		LOG_D(MAC,
+		      "Can't fit DCI allocations with %d PDCCH symbols (rnti condition), increasing by 1\n",
+		      DL_req->number_pdcch_ofdm_symbols);
+
+		DL_req->number_pdcch_ofdm_symbols++;
+		nCCE_max =
+		    get_nCCE_max(&RC.mac[module_idP]->
+				 common_channels[CC_idP],
+				 DL_req->number_pdcch_ofdm_symbols,
+				 subframeP);
+		goto try_again;
+	    }			// fCCE==-1
+
+	    // the allocation is feasible, rnti rule passes
+	    nCCE +=
+		dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.
+		aggregation_level;
+	    LOG_D(MAC, "Allocating at nCCE %d\n", fCCE);
+	    if (test_onlyP == 0) {
+		dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.cce_idx = fCCE;
+		LOG_D(MAC, "Allocate CCEs subframe %d, test %d\n",
+		      subframeP, test_onlyP);
+	    }
+	    idci++;
+	}
+    }				// for i = 0 ... num_DL_DCIs
 
-  return 0;
+    return 0;
 
-failed:
-  return -1;
+  failed:
+    return -1;
 }
 
 /*
@@ -2917,750 +3409,1046 @@ uint8_t get_ul_req_index(module_id_t module_idP, int CC_idP, sub_frame_t subfram
 }
 */
 
-nfapi_ul_config_request_pdu_t* has_ul_grant(module_id_t module_idP,int CC_idP,uint16_t absSFP,uint16_t rnti)
-{
-  nfapi_ul_config_request_body_t *ul_req;
-  nfapi_ul_config_request_pdu_t *ul_config_pdu;
-
-  ul_req        = &RC.mac[module_idP]->UL_req_tmp[CC_idP][absSFP%10].ul_config_request_body;
-  ul_config_pdu = &ul_req->ul_config_pdu_list[0];
-  LOG_D(MAC,"Checking for rnti %x UL grant in subframeP %d (num pdu %d)\n",rnti,absSFP%10,ul_req->number_of_pdus);
-
-  for (int i=0; i<ul_req->number_of_pdus;i++){
-    LOG_D(MAC,"PDU %d : type %d,rnti %x\n",i,ul_config_pdu[i].pdu_type,rnti);
-    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_PDU_TYPE)&&
-        (ul_config_pdu[i].ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)) return(&ul_config_pdu[i]);
-    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE)&&
-        (ul_config_pdu[i].ulsch_cqi_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)) return(&ul_config_pdu[i]);
-    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE)&&
-        (ul_config_pdu[i].ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)) return(&ul_config_pdu[i]);
-    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE)&&
-        (ul_config_pdu[i].ulsch_cqi_harq_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)) return(&ul_config_pdu[i]);
-
-    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE)&&
-        (ul_config_pdu[i].uci_cqi_pdu.ue_information.ue_information_rel8.rnti == rnti)) return(&ul_config_pdu[i]);
-    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE)&&
-        (ul_config_pdu[i].uci_sr_pdu.ue_information.ue_information_rel8.rnti == rnti)) return(&ul_config_pdu[i]);
-    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE)&&
-        (ul_config_pdu[i].uci_harq_pdu.ue_information.ue_information_rel8.rnti == rnti)) return(&ul_config_pdu[i]);
-    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE)&&
-        (ul_config_pdu[i].uci_sr_harq_pdu.ue_information.ue_information_rel8.rnti == rnti)) return(&ul_config_pdu[i]);
-    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE)&&
-        (ul_config_pdu[i].uci_cqi_harq_pdu.ue_information.ue_information_rel8.rnti == rnti)) return(&ul_config_pdu[i]);
-    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_CQI_SR_PDU_TYPE)&&
-        (ul_config_pdu[i].uci_cqi_sr_pdu.ue_information.ue_information_rel8.rnti == rnti)) return(&ul_config_pdu[i]);
-    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE) &&
-        (ul_config_pdu[i].uci_cqi_sr_harq_pdu.ue_information.ue_information_rel8.rnti == rnti)) return(&ul_config_pdu[i]);
-
-    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_UCI_CSI_PDU_TYPE)&&
-        (ul_config_pdu[i].ulsch_uci_csi_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)) return(&ul_config_pdu[i]);
-    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE)&&
-        (ul_config_pdu[i].ulsch_uci_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)) return(&ul_config_pdu[i]);
-    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE)&&
-        (ul_config_pdu[i].ulsch_csi_uci_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)) return(&ul_config_pdu[i]);
-  }
+nfapi_ul_config_request_pdu_t *has_ul_grant(module_id_t module_idP,
+					    int CC_idP, uint16_t absSFP,
+					    uint16_t rnti)
+{
+    nfapi_ul_config_request_body_t *ul_req;
+    nfapi_ul_config_request_pdu_t *ul_config_pdu;
+
+    ul_req =
+	&RC.mac[module_idP]->UL_req_tmp[CC_idP][absSFP %
+						10].ul_config_request_body;
+    ul_config_pdu = &ul_req->ul_config_pdu_list[0];
+    LOG_D(MAC,
+	  "Checking for rnti %x UL grant in subframeP %d (num pdu %d)\n",
+	  rnti, absSFP % 10, ul_req->number_of_pdus);
+
+    for (int i = 0; i < ul_req->number_of_pdus; i++) {
+	LOG_D(MAC, "PDU %d : type %d,rnti %x\n", i,
+	      ul_config_pdu[i].pdu_type, rnti);
+	if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_PDU_TYPE)
+	    && (ul_config_pdu[i].ulsch_pdu.ulsch_pdu_rel8.rnti == rnti))
+	    return (&ul_config_pdu[i]);
+	if ((ul_config_pdu[i].pdu_type ==
+	     NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE)
+	    && (ul_config_pdu[i].ulsch_cqi_ri_pdu.ulsch_pdu.
+		ulsch_pdu_rel8.rnti == rnti))
+	    return (&ul_config_pdu[i]);
+	if ((ul_config_pdu[i].pdu_type ==
+	     NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE)
+	    && (ul_config_pdu[i].ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.
+		rnti == rnti))
+	    return (&ul_config_pdu[i]);
+	if ((ul_config_pdu[i].pdu_type ==
+	     NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE)
+	    && (ul_config_pdu[i].ulsch_cqi_harq_ri_pdu.
+		ulsch_pdu.ulsch_pdu_rel8.rnti == rnti))
+	    return (&ul_config_pdu[i]);
+
+	if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE)
+	    && (ul_config_pdu[i].uci_cqi_pdu.
+		ue_information.ue_information_rel8.rnti == rnti))
+	    return (&ul_config_pdu[i]);
+	if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE)
+	    && (ul_config_pdu[i].uci_sr_pdu.
+		ue_information.ue_information_rel8.rnti == rnti))
+	    return (&ul_config_pdu[i]);
+	if ((ul_config_pdu[i].pdu_type ==
+	     NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE)
+	    && (ul_config_pdu[i].uci_harq_pdu.
+		ue_information.ue_information_rel8.rnti == rnti))
+	    return (&ul_config_pdu[i]);
+	if ((ul_config_pdu[i].pdu_type ==
+	     NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE)
+	    && (ul_config_pdu[i].uci_sr_harq_pdu.
+		ue_information.ue_information_rel8.rnti == rnti))
+	    return (&ul_config_pdu[i]);
+	if ((ul_config_pdu[i].pdu_type ==
+	     NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE)
+	    && (ul_config_pdu[i].uci_cqi_harq_pdu.
+		ue_information.ue_information_rel8.rnti == rnti))
+	    return (&ul_config_pdu[i]);
+	if ((ul_config_pdu[i].pdu_type ==
+	     NFAPI_UL_CONFIG_UCI_CQI_SR_PDU_TYPE)
+	    && (ul_config_pdu[i].uci_cqi_sr_pdu.
+		ue_information.ue_information_rel8.rnti == rnti))
+	    return (&ul_config_pdu[i]);
+	if ((ul_config_pdu[i].pdu_type ==
+	     NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE)
+	    && (ul_config_pdu[i].uci_cqi_sr_harq_pdu.
+		ue_information.ue_information_rel8.rnti == rnti))
+	    return (&ul_config_pdu[i]);
+
+	if ((ul_config_pdu[i].pdu_type ==
+	     NFAPI_UL_CONFIG_ULSCH_UCI_CSI_PDU_TYPE)
+	    && (ul_config_pdu[i].ulsch_uci_csi_pdu.
+		ulsch_pdu.ulsch_pdu_rel8.rnti == rnti))
+	    return (&ul_config_pdu[i]);
+	if ((ul_config_pdu[i].pdu_type ==
+	     NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE)
+	    && (ul_config_pdu[i].ulsch_uci_harq_pdu.
+		ulsch_pdu.ulsch_pdu_rel8.rnti == rnti))
+	    return (&ul_config_pdu[i]);
+	if ((ul_config_pdu[i].pdu_type ==
+	     NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE)
+	    && (ul_config_pdu[i].ulsch_csi_uci_harq_pdu.
+		ulsch_pdu.ulsch_pdu_rel8.rnti == rnti))
+	    return (&ul_config_pdu[i]);
+    }
 
-  return(NULL); // no ul grant at all for this UE
+    return (NULL);		// no ul grant at all for this UE
 }
 
-boolean_t CCE_allocation_infeasible(int module_idP,
-                                    int CC_idP,
-                                    int format_flag,
-                                    int subframe,
-                                    int aggregation,
-                                    int rnti)
+boolean_t
+CCE_allocation_infeasible(int module_idP,
+			  int CC_idP,
+			  int format_flag,
+			  int subframe, int aggregation, int rnti)
 {
-  nfapi_dl_config_request_body_t *DL_req        = &RC.mac[module_idP]->DL_req[CC_idP].dl_config_request_body;
-  nfapi_dl_config_request_pdu_t  *dl_config_pdu = &DL_req->dl_config_pdu_list[DL_req->number_pdu];
-  nfapi_hi_dci0_request_body_t   *HI_DCI0_req   = &RC.mac[module_idP]->HI_DCI0_req[CC_idP].hi_dci0_request_body;
-  nfapi_hi_dci0_request_pdu_t    *hi_dci0_pdu   = &HI_DCI0_req->hi_dci0_pdu_list[HI_DCI0_req->number_of_dci+HI_DCI0_req->number_of_hi];
-  int ret;
-  boolean_t res = FALSE;
-
-  if (format_flag != 2) { // DL DCI
-    if (DL_req->number_pdu == MAX_NUM_DL_PDU) {
-      LOG_W(MAC, "Subframe %d: FAPI DL structure is full, skip scheduling UE %d\n",
-            subframe, rnti);
-    } else {
-      dl_config_pdu->pdu_type                                     = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
-      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti              = rnti;
-      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type         = (format_flag == 0)?2:1;
-      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level = aggregation;
-      DL_req->number_pdu++;
-      LOG_D(MAC,"Subframe %d: Checking CCE feasibility format %d : (%x,%d) (%x,%d,%d)\n",
-        subframe,format_flag,rnti,aggregation,
-          dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti,
-          dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
-          dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type);
-      ret = allocate_CCEs(module_idP,CC_idP,subframe,0);
-      if (ret==-1)
-        res = TRUE;
-      DL_req->number_pdu--;
-    }
-  }
-  else { // ue-specific UL DCI
-    if (HI_DCI0_req->number_of_dci+HI_DCI0_req->number_of_hi == MAX_NUM_HI_DCI0_PDU) {
-      LOG_W(MAC, "Subframe %d: FAPI UL structure is full, skip scheduling UE %d\n",
-            subframe, rnti);
-    } else {
-      hi_dci0_pdu->pdu_type                               = NFAPI_HI_DCI0_DCI_PDU_TYPE;
-      hi_dci0_pdu->dci_pdu.dci_pdu_rel8.rnti              = rnti;
-      hi_dci0_pdu->dci_pdu.dci_pdu_rel8.aggregation_level = aggregation;
-      HI_DCI0_req->number_of_dci++;
-      ret = allocate_CCEs(module_idP,CC_idP,subframe,0);
-      if (ret==-1)
-        res = TRUE;
-      HI_DCI0_req->number_of_dci--;
+    nfapi_dl_config_request_body_t *DL_req =
+	&RC.mac[module_idP]->DL_req[CC_idP].dl_config_request_body;
+    nfapi_dl_config_request_pdu_t *dl_config_pdu =
+	&DL_req->dl_config_pdu_list[DL_req->number_pdu];
+    nfapi_hi_dci0_request_body_t *HI_DCI0_req =
+	&RC.mac[module_idP]->HI_DCI0_req[CC_idP].hi_dci0_request_body;
+    nfapi_hi_dci0_request_pdu_t *hi_dci0_pdu =
+	&HI_DCI0_req->hi_dci0_pdu_list[HI_DCI0_req->number_of_dci +
+				       HI_DCI0_req->number_of_hi];
+    int ret;
+    boolean_t res = FALSE;
+
+    if (format_flag != 2) {	// DL DCI
+	if (DL_req->number_pdu == MAX_NUM_DL_PDU) {
+	    LOG_W(MAC,
+		  "Subframe %d: FAPI DL structure is full, skip scheduling UE %d\n",
+		  subframe, rnti);
+	} else {
+	    dl_config_pdu->pdu_type = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti = rnti;
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type =
+		(format_flag == 0) ? 2 : 1;
+	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level =
+		aggregation;
+	    DL_req->number_pdu++;
+	    LOG_D(MAC,
+		  "Subframe %d: Checking CCE feasibility format %d : (%x,%d) (%x,%d,%d)\n",
+		  subframe, format_flag, rnti, aggregation,
+		  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti,
+		  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
+		  aggregation_level,
+		  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type);
+	    ret = allocate_CCEs(module_idP, CC_idP, subframe, 0);
+	    if (ret == -1)
+		res = TRUE;
+	    DL_req->number_pdu--;
+	}
+    } else {			// ue-specific UL DCI
+	if (HI_DCI0_req->number_of_dci + HI_DCI0_req->number_of_hi ==
+	    MAX_NUM_HI_DCI0_PDU) {
+	    LOG_W(MAC,
+		  "Subframe %d: FAPI UL structure is full, skip scheduling UE %d\n",
+		  subframe, rnti);
+	} else {
+	    hi_dci0_pdu->pdu_type = NFAPI_HI_DCI0_DCI_PDU_TYPE;
+	    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.rnti = rnti;
+	    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.aggregation_level =
+		aggregation;
+	    HI_DCI0_req->number_of_dci++;
+	    ret = allocate_CCEs(module_idP, CC_idP, subframe, 0);
+	    if (ret == -1)
+		res = TRUE;
+	    HI_DCI0_req->number_of_dci--;
+	}
     }
-  }
 
-  return res;
+    return res;
 }
 
-void extract_harq(module_id_t mod_idP,int CC_idP,int UE_id,frame_t frameP,sub_frame_t subframeP,void *harq_indication,int format)
+void
+extract_harq(module_id_t mod_idP, int CC_idP, int UE_id,
+	     frame_t frameP, sub_frame_t subframeP,
+	     void *harq_indication, int format)
 {
-  UE_list_t *UE_list       = &RC.mac[mod_idP]->UE_list;
-  UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-  rnti_t rnti              = UE_RNTI(mod_idP,UE_id);
-  COMMON_channels_t *cc    = &RC.mac[mod_idP]->common_channels[CC_idP];
-  nfapi_harq_indication_fdd_rel13_t *harq_indication_fdd;
-  nfapi_harq_indication_tdd_rel13_t *harq_indication_tdd;
-  uint16_t num_ack_nak;
-  int numCC = UE_list->numactiveCCs[UE_id];
-  int pCCid = UE_list->pCC_id[UE_id];
-  int spatial_bundling = 0;
-  int tmode[5];
-  int i,j;
-  uint8_t *pdu;
+    UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
+    UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+    rnti_t rnti = UE_RNTI(mod_idP, UE_id);
+    COMMON_channels_t *cc = &RC.mac[mod_idP]->common_channels[CC_idP];
+    nfapi_harq_indication_fdd_rel13_t *harq_indication_fdd;
+    nfapi_harq_indication_tdd_rel13_t *harq_indication_tdd;
+    uint16_t num_ack_nak;
+    int numCC = UE_list->numactiveCCs[UE_id];
+    int pCCid = UE_list->pCC_id[UE_id];
+    int spatial_bundling = 0;
+    int tmode[5];
+    int i, j;
+    uint8_t *pdu;
 
 #ifdef Rel14
-  AssertFatal(UE_list->UE_template[pCCid][UE_id].physicalConfigDedicated->pucch_ConfigDedicated!=NULL,"pucch_ConfigDedicated is null!\n");
-  if ((UE_list->UE_template[pCCid][UE_id].physicalConfigDedicated->ext7) &&
-      (UE_list->UE_template[pCCid][UE_id].physicalConfigDedicated->ext7->pucch_ConfigDedicated_r13) &&
-      (((UE_list->UE_template[pCCid][UE_id].physicalConfigDedicated->ext7->pucch_ConfigDedicated_r13->spatialBundlingPUCCH_r13) &&
-        (format==0)) ||
-       ((UE_list->UE_template[pCCid][UE_id].physicalConfigDedicated->ext7->pucch_ConfigDedicated_r13->spatialBundlingPUSCH_r13) &&
-        (format==1))))
-    spatial_bundling = 1;
+    AssertFatal(UE_list->
+		UE_template[pCCid][UE_id].physicalConfigDedicated->
+		pucch_ConfigDedicated != NULL,
+		"pucch_ConfigDedicated is null!\n");
+    if ((UE_list->UE_template[pCCid][UE_id].physicalConfigDedicated->ext7)
+	&& (UE_list->UE_template[pCCid][UE_id].
+	    physicalConfigDedicated->ext7->pucch_ConfigDedicated_r13)
+	&&
+	(((UE_list->UE_template[pCCid][UE_id].
+	   physicalConfigDedicated->ext7->pucch_ConfigDedicated_r13->
+	   spatialBundlingPUCCH_r13)
+	  && (format == 0))
+	 ||
+	 ((UE_list->UE_template[pCCid][UE_id].
+	   physicalConfigDedicated->ext7->pucch_ConfigDedicated_r13->
+	   spatialBundlingPUSCH_r13)
+	  && (format == 1))))
+	spatial_bundling = 1;
 #endif
 
-  for (i=0;i<numCC;i++) tmode[i] = get_tmode(mod_idP,i,UE_id);
-
-  if (cc->tdd_Config) {
-    harq_indication_tdd = (nfapi_harq_indication_tdd_rel13_t *)harq_indication;
-    //    pdu = &harq_indication_tdd->harq_tb_n[0];
-
-    num_ack_nak = harq_indication_tdd->number_of_ack_nack;
-
-    switch (harq_indication_tdd->mode) {
-      case 0: // Format 1a/b
-        AssertFatal(numCC==1,"numCC %d > 1, should not be using Format1a/b\n",numCC);
-        break;
-      case 1: // Channel Selection
-        break;
-      case 2: // Format 3
-        break;
-      case 3: // Format 4
-        break;
-      case 4: // Format 5
-        break;
-    }
-  }
-  else {
-    harq_indication_fdd = (nfapi_harq_indication_fdd_rel13_t *)harq_indication;
-    num_ack_nak = harq_indication_fdd->number_of_ack_nack;
-    pdu = &harq_indication_fdd->harq_tb_n[0];
-
-    uint8_t harq_pid = ((10*frameP) + subframeP + 10236)&7;
-
-    switch (harq_indication_fdd->mode) {
-      case 0: // Format 1a/b (10.1.2.1)
-        AssertFatal(numCC==1,"numCC %d > 1, should not be using Format1a/b\n",numCC);
-        if (tmode[0]==1 || tmode[0]==2 || tmode[0]==5 || tmode[0]==6 || tmode[0]==7) { // NOTE: have to handle the case of TM9-10 with 1 antenna port
-          // single ACK/NAK bit
-          AssertFatal(num_ack_nak==1,"num_ack_nak %d > 1 for 1 CC and single-layer transmission\n",num_ack_nak);
-          AssertFatal(sched_ctl->round[CC_idP][harq_pid]<8,"Got ACK/NAK for inactive harq_pid %d for UE %d/%x\n",harq_pid,UE_id,rnti);
-          AssertFatal(pdu[0] == 1 || pdu[0] == 2 || pdu[0] == 4,
-                      "Received ACK/NAK %d which is not 1 or 2 for harq_pid %d from UE %d/%x\n",pdu[0],harq_pid,UE_id,rnti);
-          LOG_D(MAC,"Received %d for harq_pid %d\n",pdu[0],harq_pid);
-
-          if (pdu[0] == 1) { // ACK
-            sched_ctl->round[CC_idP][harq_pid]=8; // release HARQ process
-            sched_ctl->tbcnt[CC_idP][harq_pid]=0;
-          }
-          else if (pdu[0] == 2 || pdu[0] == 4) // NAK (treat DTX as NAK)
-            sched_ctl->round[CC_idP][harq_pid]++; // increment round
-        }
-        else {
-          // one or two ACK/NAK bits
-          AssertFatal(num_ack_nak>2,"num_ack_nak %d > 2 for 1 CC and TM3/4/8/9/10\n",num_ack_nak);
-          if ((num_ack_nak==2) && (sched_ctl->round[CC_idP][harq_pid]<8) && (sched_ctl->tbcnt[CC_idP][harq_pid]==1) && (pdu[0] == 1) && (pdu[1] == 1)) {
-            sched_ctl->round[CC_idP][harq_pid]=8;
-            sched_ctl->tbcnt[CC_idP][harq_pid]=0;
-          }
-          if ((num_ack_nak==2) && (sched_ctl->round[CC_idP][harq_pid]<8) && (sched_ctl->tbcnt[CC_idP][harq_pid]==1) && (pdu[0] == 2) && (pdu[1] == 2))
-            sched_ctl->round[CC_idP][harq_pid]++;
-          else if (((num_ack_nak==2) && (sched_ctl->round[CC_idP][harq_pid]<8) && (sched_ctl->tbcnt[0][harq_pid]==2) && (pdu[0] == 1) && (pdu[1] == 2)) ||
-                   ((num_ack_nak==2) && (sched_ctl->round[CC_idP][harq_pid]<8) && (sched_ctl->tbcnt[CC_idP][harq_pid]==2) && (pdu[0] == 2) && (pdu[1] == 1))) {
-            sched_ctl->round[CC_idP][harq_pid]++;
-            sched_ctl->tbcnt[CC_idP][harq_pid]=1;
-          }
-          else if ((num_ack_nak==2) && (sched_ctl->round[CC_idP][harq_pid]<8) && (sched_ctl->tbcnt[CC_idP][harq_pid]==2) && (pdu[0] == 2) && (pdu[1] == 2))
-            sched_ctl->round[CC_idP][harq_pid]++;
-          else AssertFatal(1==0,"Illegal ACK/NAK/round combination (%d,%d,%d,%d,%d) for harq_pid %d, UE %d/%x\n",
-                           num_ack_nak,sched_ctl->round[CC_idP][harq_pid],sched_ctl->round[CC_idP][harq_pid],pdu[0],pdu[1], harq_pid,UE_id,
-                           rnti);
-        }
-        break;
-      case 1: // FDD Channel Selection (10.1.2.2.1), must be received for 2 serving cells
-        AssertFatal(numCC==2,"Should not receive harq indication with channel selection with %d active CCs\n",
-                    numCC);
-
-        if ((num_ack_nak == 2) && (sched_ctl->round[pCCid][harq_pid]<8) && (sched_ctl->round[1-pCCid][harq_pid]<8) && (sched_ctl->tbcnt[pCCid][harq_pid]==1) && (sched_ctl->tbcnt[1-pCCid][harq_pid]==1)) {
-          AssertFatal(pdu[0]<=3,"pdu[0] %d is not ACK/NAK/DTX\n",pdu[0]);
-          AssertFatal(pdu[1]<=3,"pdu[1] %d is not ACK/NAK/DTX\n",pdu[1]);
-          if (pdu[0] == 1) sched_ctl->round[pCCid][harq_pid]=8;
-          else             sched_ctl->round[pCCid][harq_pid]++;
-          if (pdu[1] == 1) sched_ctl->round[1-pCCid][harq_pid]=8;
-          else             sched_ctl->round[1-pCCid][harq_pid]++;
-        } // A=2
-        else if ((num_ack_nak == 3) && (sched_ctl->round[pCCid][harq_pid]<8) && (sched_ctl->tbcnt[pCCid][harq_pid]==2) && (sched_ctl->round[1-pCCid][harq_pid]<8) && (sched_ctl->tbcnt[1-pCCid][harq_pid]==1)) {
-          AssertFatal(pdu[0]<=3,"pdu[0] %d is not ACK/NAK/DTX\n",pdu[0]);
-          AssertFatal(pdu[1]<=3,"pdu[1] %d is not ACK/NAK/DTX\n",pdu[1]);
-          AssertFatal(pdu[2]<=3,"pdu[2] %d is not ACK/NAK/DTX\n",pdu[2]);
-          AssertFatal(sched_ctl->tbcnt[pCCid][harq_pid] == 2,"sched_ctl->tbcnt[%d][%d] != 2 for UE %d/%x\n",pCCid,harq_pid,UE_id,rnti);
-          AssertFatal(sched_ctl->tbcnt[1-pCCid][harq_pid] == 1,"sched_ctl->tbcnt[%d][%d] != 1 for UE %d/%x\n",1-pCCid,harq_pid,UE_id,rnti);
-          if ((pdu[0] == 1) && (pdu[1] == 1)) { // both ACK
-              sched_ctl->round[pCCid][harq_pid]=8;
-              sched_ctl->tbcnt[pCCid][harq_pid]=0;
-          }
-          else if (((pdu[0] == 2) && (pdu[1] == 1))||
-                   ((pdu[0] == 1) && (pdu[1] == 2))){
-            sched_ctl->round[pCCid][harq_pid]++;
-            sched_ctl->tbcnt[pCCid][harq_pid]=1;
-          }
-          else
-             sched_ctl->round[pCCid][harq_pid]++;
-
-          if (pdu[2] == 1) sched_ctl->round[1-pCCid][harq_pid]=8;
-          else             sched_ctl->round[1-pCCid][harq_pid]++;
-        } // A=3 primary cell has 2 TBs
-        else if ((num_ack_nak == 3) && (sched_ctl->round[1-pCCid][harq_pid]<8) && (sched_ctl->round[pCCid][harq_pid]<8) && (sched_ctl->tbcnt[1-pCCid][harq_pid]==2) && (sched_ctl->tbcnt[pCCid][harq_pid]==1)) {
-          AssertFatal(pdu[0]<=3,"pdu[0] %d is not ACK/NAK/DTX\n",pdu[0]);
-          AssertFatal(pdu[1]<=3,"pdu[1] %d is not ACK/NAK/DTX\n",pdu[1]);
-          AssertFatal(pdu[2]<=3,"pdu[2] %d is not ACK/NAK/DTX\n",pdu[2]);
-          AssertFatal(sched_ctl->tbcnt[1-pCCid][harq_pid] == 2,"sched_ctl->tbcnt[%d][%d] != 2 for UE %d/%x\n",1-pCCid,harq_pid,UE_id,rnti);
-          AssertFatal(sched_ctl->tbcnt[pCCid][harq_pid] == 1,"sched_ctl->tbcnt[%d][%d] != 1 for UE %d/%x\n",pCCid,harq_pid,UE_id,rnti);
-          if ((pdu[0] == 1) && (pdu[1] == 1)) { // both ACK
-              sched_ctl->round[1-pCCid][harq_pid]=8;
-              sched_ctl->tbcnt[1-pCCid][harq_pid]=0;
-          }
-          else if (((pdu[0] >= 2) && (pdu[1] == 1))||
-                   ((pdu[0] == 1) && (pdu[1] >= 2))){ // one ACK
-            sched_ctl->round[1-pCCid][harq_pid]++;
-            sched_ctl->tbcnt[1-pCCid][harq_pid]=1;
-          }
-          else  // both NAK/DTX
-             sched_ctl->round[1-pCCid][harq_pid]++;
-
-          if (pdu[2] == 1) sched_ctl->round[pCCid][harq_pid]=8;
-          else             sched_ctl->round[pCCid][harq_pid]++;
-        } // A=3 secondary cell has 2 TBs
+    for (i = 0; i < numCC; i++)
+	tmode[i] = get_tmode(mod_idP, i, UE_id);
+
+    if (cc->tdd_Config) {
+	harq_indication_tdd =
+	    (nfapi_harq_indication_tdd_rel13_t *) harq_indication;
+	//    pdu = &harq_indication_tdd->harq_tb_n[0];
+
+	num_ack_nak = harq_indication_tdd->number_of_ack_nack;
+
+	switch (harq_indication_tdd->mode) {
+	case 0:		// Format 1a/b
+	    AssertFatal(numCC == 1,
+			"numCC %d > 1, should not be using Format1a/b\n",
+			numCC);
+	    break;
+	case 1:		// Channel Selection
+	    break;
+	case 2:		// Format 3
+	    break;
+	case 3:		// Format 4
+	    break;
+	case 4:		// Format 5
+	    break;
+	}
+    } else {
+	harq_indication_fdd =
+	    (nfapi_harq_indication_fdd_rel13_t *) harq_indication;
+	num_ack_nak = harq_indication_fdd->number_of_ack_nack;
+	pdu = &harq_indication_fdd->harq_tb_n[0];
+
+	uint8_t harq_pid = ((10 * frameP) + subframeP + 10236) & 7;
+
+	switch (harq_indication_fdd->mode) {
+	case 0:		// Format 1a/b (10.1.2.1)
+	    AssertFatal(numCC == 1,
+			"numCC %d > 1, should not be using Format1a/b\n",
+			numCC);
+	    if (tmode[0] == 1 || tmode[0] == 2 || tmode[0] == 5 || tmode[0] == 6 || tmode[0] == 7) {	// NOTE: have to handle the case of TM9-10 with 1 antenna port
+		// single ACK/NAK bit
+		AssertFatal(num_ack_nak == 1,
+			    "num_ack_nak %d > 1 for 1 CC and single-layer transmission\n",
+			    num_ack_nak);
+		AssertFatal(sched_ctl->round[CC_idP][harq_pid] < 8,
+			    "Got ACK/NAK for inactive harq_pid %d for UE %d/%x\n",
+			    harq_pid, UE_id, rnti);
+		AssertFatal(pdu[0] == 1 || pdu[0] == 2
+			    || pdu[0] == 4,
+			    "Received ACK/NAK %d which is not 1 or 2 for harq_pid %d from UE %d/%x\n",
+			    pdu[0], harq_pid, UE_id, rnti);
+		LOG_D(MAC, "Received %d for harq_pid %d\n", pdu[0],
+		      harq_pid);
+
+		if (pdu[0] == 1) {	// ACK
+		    sched_ctl->round[CC_idP][harq_pid] = 8;	// release HARQ process
+		    sched_ctl->tbcnt[CC_idP][harq_pid] = 0;
+		} else if (pdu[0] == 2 || pdu[0] == 4)	// NAK (treat DTX as NAK)
+		    sched_ctl->round[CC_idP][harq_pid]++;	// increment round
+	    } else {
+		// one or two ACK/NAK bits
+		AssertFatal(num_ack_nak > 2,
+			    "num_ack_nak %d > 2 for 1 CC and TM3/4/8/9/10\n",
+			    num_ack_nak);
+		if ((num_ack_nak == 2)
+		    && (sched_ctl->round[CC_idP][harq_pid] < 8)
+		    && (sched_ctl->tbcnt[CC_idP][harq_pid] == 1)
+		    && (pdu[0] == 1) && (pdu[1] == 1)) {
+		    sched_ctl->round[CC_idP][harq_pid] = 8;
+		    sched_ctl->tbcnt[CC_idP][harq_pid] = 0;
+		}
+		if ((num_ack_nak == 2)
+		    && (sched_ctl->round[CC_idP][harq_pid] < 8)
+		    && (sched_ctl->tbcnt[CC_idP][harq_pid] == 1)
+		    && (pdu[0] == 2) && (pdu[1] == 2))
+		    sched_ctl->round[CC_idP][harq_pid]++;
+		else if (((num_ack_nak == 2)
+			  && (sched_ctl->round[CC_idP][harq_pid] < 8)
+			  && (sched_ctl->tbcnt[0][harq_pid] == 2)
+			  && (pdu[0] == 1) && (pdu[1] == 2))
+			 || ((num_ack_nak == 2)
+			     && (sched_ctl->round[CC_idP][harq_pid] < 8)
+			     && (sched_ctl->tbcnt[CC_idP][harq_pid] == 2)
+			     && (pdu[0] == 2) && (pdu[1] == 1))) {
+		    sched_ctl->round[CC_idP][harq_pid]++;
+		    sched_ctl->tbcnt[CC_idP][harq_pid] = 1;
+		} else if ((num_ack_nak == 2)
+			   && (sched_ctl->round[CC_idP][harq_pid] < 8)
+			   && (sched_ctl->tbcnt[CC_idP][harq_pid] == 2)
+			   && (pdu[0] == 2) && (pdu[1] == 2))
+		    sched_ctl->round[CC_idP][harq_pid]++;
+		else
+		    AssertFatal(1 == 0,
+				"Illegal ACK/NAK/round combination (%d,%d,%d,%d,%d) for harq_pid %d, UE %d/%x\n",
+				num_ack_nak,
+				sched_ctl->round[CC_idP][harq_pid],
+				sched_ctl->round[CC_idP][harq_pid], pdu[0],
+				pdu[1], harq_pid, UE_id, rnti);
+	    }
+	    break;
+	case 1:		// FDD Channel Selection (10.1.2.2.1), must be received for 2 serving cells
+	    AssertFatal(numCC == 2,
+			"Should not receive harq indication with channel selection with %d active CCs\n",
+			numCC);
+
+	    if ((num_ack_nak == 2)
+		&& (sched_ctl->round[pCCid][harq_pid] < 8)
+		&& (sched_ctl->round[1 - pCCid][harq_pid] < 8)
+		&& (sched_ctl->tbcnt[pCCid][harq_pid] == 1)
+		&& (sched_ctl->tbcnt[1 - pCCid][harq_pid] == 1)) {
+		AssertFatal(pdu[0] <= 3, "pdu[0] %d is not ACK/NAK/DTX\n",
+			    pdu[0]);
+		AssertFatal(pdu[1] <= 3, "pdu[1] %d is not ACK/NAK/DTX\n",
+			    pdu[1]);
+		if (pdu[0] == 1)
+		    sched_ctl->round[pCCid][harq_pid] = 8;
+		else
+		    sched_ctl->round[pCCid][harq_pid]++;
+		if (pdu[1] == 1)
+		    sched_ctl->round[1 - pCCid][harq_pid] = 8;
+		else
+		    sched_ctl->round[1 - pCCid][harq_pid]++;
+	    }			// A=2
+	    else if ((num_ack_nak == 3)
+		     && (sched_ctl->round[pCCid][harq_pid] < 8)
+		     && (sched_ctl->tbcnt[pCCid][harq_pid] == 2)
+		     && (sched_ctl->round[1 - pCCid][harq_pid] < 8)
+		     && (sched_ctl->tbcnt[1 - pCCid][harq_pid] == 1)) {
+		AssertFatal(pdu[0] <= 3, "pdu[0] %d is not ACK/NAK/DTX\n",
+			    pdu[0]);
+		AssertFatal(pdu[1] <= 3, "pdu[1] %d is not ACK/NAK/DTX\n",
+			    pdu[1]);
+		AssertFatal(pdu[2] <= 3, "pdu[2] %d is not ACK/NAK/DTX\n",
+			    pdu[2]);
+		AssertFatal(sched_ctl->tbcnt[pCCid][harq_pid] == 2,
+			    "sched_ctl->tbcnt[%d][%d] != 2 for UE %d/%x\n",
+			    pCCid, harq_pid, UE_id, rnti);
+		AssertFatal(sched_ctl->tbcnt[1 - pCCid][harq_pid] == 1,
+			    "sched_ctl->tbcnt[%d][%d] != 1 for UE %d/%x\n",
+			    1 - pCCid, harq_pid, UE_id, rnti);
+		if ((pdu[0] == 1) && (pdu[1] == 1)) {	// both ACK
+		    sched_ctl->round[pCCid][harq_pid] = 8;
+		    sched_ctl->tbcnt[pCCid][harq_pid] = 0;
+		} else if (((pdu[0] == 2) && (pdu[1] == 1)) ||
+			   ((pdu[0] == 1) && (pdu[1] == 2))) {
+		    sched_ctl->round[pCCid][harq_pid]++;
+		    sched_ctl->tbcnt[pCCid][harq_pid] = 1;
+		} else
+		    sched_ctl->round[pCCid][harq_pid]++;
+
+		if (pdu[2] == 1)
+		    sched_ctl->round[1 - pCCid][harq_pid] = 8;
+		else
+		    sched_ctl->round[1 - pCCid][harq_pid]++;
+	    }			// A=3 primary cell has 2 TBs
+	    else if ((num_ack_nak == 3)
+		     && (sched_ctl->round[1 - pCCid][harq_pid] < 8)
+		     && (sched_ctl->round[pCCid][harq_pid] < 8)
+		     && (sched_ctl->tbcnt[1 - pCCid][harq_pid] == 2)
+		     && (sched_ctl->tbcnt[pCCid][harq_pid] == 1)) {
+		AssertFatal(pdu[0] <= 3, "pdu[0] %d is not ACK/NAK/DTX\n",
+			    pdu[0]);
+		AssertFatal(pdu[1] <= 3, "pdu[1] %d is not ACK/NAK/DTX\n",
+			    pdu[1]);
+		AssertFatal(pdu[2] <= 3, "pdu[2] %d is not ACK/NAK/DTX\n",
+			    pdu[2]);
+		AssertFatal(sched_ctl->tbcnt[1 - pCCid][harq_pid] == 2,
+			    "sched_ctl->tbcnt[%d][%d] != 2 for UE %d/%x\n",
+			    1 - pCCid, harq_pid, UE_id, rnti);
+		AssertFatal(sched_ctl->tbcnt[pCCid][harq_pid] == 1,
+			    "sched_ctl->tbcnt[%d][%d] != 1 for UE %d/%x\n",
+			    pCCid, harq_pid, UE_id, rnti);
+		if ((pdu[0] == 1) && (pdu[1] == 1)) {	// both ACK
+		    sched_ctl->round[1 - pCCid][harq_pid] = 8;
+		    sched_ctl->tbcnt[1 - pCCid][harq_pid] = 0;
+		} else if (((pdu[0] >= 2) && (pdu[1] == 1))
+			   || ((pdu[0] == 1) && (pdu[1] >= 2))) {	// one ACK
+		    sched_ctl->round[1 - pCCid][harq_pid]++;
+		    sched_ctl->tbcnt[1 - pCCid][harq_pid] = 1;
+		} else		// both NAK/DTX
+		    sched_ctl->round[1 - pCCid][harq_pid]++;
+
+		if (pdu[2] == 1)
+		    sched_ctl->round[pCCid][harq_pid] = 8;
+		else
+		    sched_ctl->round[pCCid][harq_pid]++;
+	    }			// A=3 secondary cell has 2 TBs
 #if MAX_NUM_CCs>1
-        else if ((num_ack_nak == 4) && (sched_ctl->round[0][harq_pid]<8) && (sched_ctl->round[1][harq_pid]<8) && (sched_ctl->tbcnt[1-pCCid][harq_pid]==2) && (sched_ctl->tbcnt[pCCid][harq_pid]==2)) {
-          AssertFatal(pdu[0]<=3,"pdu[0] %d is not ACK/NAK/DTX\n",pdu[0]);
-          AssertFatal(pdu[1]<=3,"pdu[1] %d is not ACK/NAK/DTX\n",pdu[1]);
-          AssertFatal(pdu[2]<=3,"pdu[2] %d is not ACK/NAK/DTX\n",pdu[2]);
-          AssertFatal(pdu[3]<=3,"pdu[3] %d is not ACK/NAK/DTX\n",pdu[3]);
-          AssertFatal(sched_ctl->tbcnt[0][harq_pid] == 2,"sched_ctl->tbcnt[0][%d] != 2 for UE %d/%x\n",harq_pid,UE_id,rnti);
-          AssertFatal(sched_ctl->tbcnt[1][harq_pid] == 2,"sched_ctl->tbcnt[1][%d] != 2 for UE %d/%x\n",harq_pid,UE_id,rnti);
-          if ((pdu[0] == 1) && (pdu[1] == 1)) { // both ACK
-              sched_ctl->round[0][harq_pid]=8;
-              sched_ctl->tbcnt[0][harq_pid]=0;
-          }
-          else if (((pdu[0] >= 2) && (pdu[1] == 1))||
-                   ((pdu[0] == 1) && (pdu[1] >= 2))){ // one ACK
-            sched_ctl->round[0][harq_pid]++;
-            sched_ctl->tbcnt[0][harq_pid]=1;
-          }
-          else  // both NAK/DTX
-             sched_ctl->round[0][harq_pid]++;
-
-          if ((pdu[2] == 1) && (pdu[3] == 1)) { // both ACK
-              sched_ctl->round[1][harq_pid]=8;
-              sched_ctl->tbcnt[1][harq_pid]=0;
-          }
-          else if (((pdu[2] >= 2) && (pdu[3] == 1))||
-                   ((pdu[2] == 1) && (pdu[3] >= 2))){ // one ACK
-            sched_ctl->round[1][harq_pid]++;
-            sched_ctl->tbcnt[1][harq_pid]=1;
-          }
-          else  // both NAK/DTX
-             sched_ctl->round[1][harq_pid]++;
-        } // A=4 both serving cells have 2 TBs
+	    else if ((num_ack_nak == 4)
+		     && (sched_ctl->round[0][harq_pid] < 8)
+		     && (sched_ctl->round[1][harq_pid] < 8)
+		     && (sched_ctl->tbcnt[1 - pCCid][harq_pid] == 2)
+		     && (sched_ctl->tbcnt[pCCid][harq_pid] == 2)) {
+		AssertFatal(pdu[0] <= 3, "pdu[0] %d is not ACK/NAK/DTX\n",
+			    pdu[0]);
+		AssertFatal(pdu[1] <= 3, "pdu[1] %d is not ACK/NAK/DTX\n",
+			    pdu[1]);
+		AssertFatal(pdu[2] <= 3, "pdu[2] %d is not ACK/NAK/DTX\n",
+			    pdu[2]);
+		AssertFatal(pdu[3] <= 3, "pdu[3] %d is not ACK/NAK/DTX\n",
+			    pdu[3]);
+		AssertFatal(sched_ctl->tbcnt[0][harq_pid] == 2,
+			    "sched_ctl->tbcnt[0][%d] != 2 for UE %d/%x\n",
+			    harq_pid, UE_id, rnti);
+		AssertFatal(sched_ctl->tbcnt[1][harq_pid] == 2,
+			    "sched_ctl->tbcnt[1][%d] != 2 for UE %d/%x\n",
+			    harq_pid, UE_id, rnti);
+		if ((pdu[0] == 1) && (pdu[1] == 1)) {	// both ACK
+		    sched_ctl->round[0][harq_pid] = 8;
+		    sched_ctl->tbcnt[0][harq_pid] = 0;
+		} else if (((pdu[0] >= 2) && (pdu[1] == 1))
+			   || ((pdu[0] == 1) && (pdu[1] >= 2))) {	// one ACK
+		    sched_ctl->round[0][harq_pid]++;
+		    sched_ctl->tbcnt[0][harq_pid] = 1;
+		} else		// both NAK/DTX
+		    sched_ctl->round[0][harq_pid]++;
+
+		if ((pdu[2] == 1) && (pdu[3] == 1)) {	// both ACK
+		    sched_ctl->round[1][harq_pid] = 8;
+		    sched_ctl->tbcnt[1][harq_pid] = 0;
+		} else if (((pdu[2] >= 2) && (pdu[3] == 1))
+			   || ((pdu[2] == 1) && (pdu[3] >= 2))) {	// one ACK
+		    sched_ctl->round[1][harq_pid]++;
+		    sched_ctl->tbcnt[1][harq_pid] = 1;
+		} else		// both NAK/DTX
+		    sched_ctl->round[1][harq_pid]++;
+	    }			// A=4 both serving cells have 2 TBs
 #endif
-        break;
-      case 2: // Format 3
-        AssertFatal(numCC>2,"Should not receive harq indication with FDD format 3 with %d < 3 active CCs\n",
-                    numCC);
-        for (i=0,j=0;i<numCC;i++) {
-          if ((sched_ctl->round[i][harq_pid]<8)) {
-            if (tmode[i]==1 || tmode[i]==2 || tmode[0]==5 || tmode[0]==6 || tmode[0]==7) {
-              if (pdu[j] == 1) {
-                sched_ctl->round[i][harq_pid]=8;
-                sched_ctl->tbcnt[i][harq_pid]=0;
-              }
-              else if (pdu[j] == 2) sched_ctl->round[i][harq_pid]++;
-              else AssertFatal(1==0,"Illegal harq_ack value for CC %d harq_pid %d (%d) UE %d/%x\n",
-                               i,harq_pid,pdu[j],UE_id,rnti);
-              j++;
-            }
-            else if (spatial_bundling == 0) {
-              if      ((sched_ctl->tbcnt[i][harq_pid]==2) && (pdu[j] == 1) && (pdu[j+1]==1)) {
-                sched_ctl->round[i][harq_pid]=8;
-                sched_ctl->tbcnt[i][harq_pid]=0;
-              }
-              else if ((sched_ctl->tbcnt[i][harq_pid]==2) && (pdu[j] == 1) && (pdu[j+1]==2)) {
-                sched_ctl->round[i][harq_pid]++;
-                sched_ctl->tbcnt[i][harq_pid]=1;
-              }
-              else if ((sched_ctl->tbcnt[i][harq_pid]==2) && (pdu[j] == 2) && (pdu[j+1]==1)) {
-                sched_ctl->round[i][harq_pid]++;
-                sched_ctl->tbcnt[i][harq_pid]=1;
-              }
-              else if ((sched_ctl->tbcnt[i][harq_pid]==2) && (pdu[j] == 2) && (pdu[j+1]==2)) {
-                sched_ctl->round[i][harq_pid]++;
-              }
-              else AssertFatal(1==0,"Illegal combination for CC %d harq_pid %d (%d,%d,%d) UE %d/%x\n",
-                               i,harq_pid,sched_ctl->tbcnt[i][harq_pid],pdu[j],pdu[j+1],UE_id,rnti);
-              j+=2;
-            }
-            else if (spatial_bundling == 1) {
-              if      (pdu[j] == 1) {
-                sched_ctl->round[i][harq_pid]=8;
-                sched_ctl->tbcnt[i][harq_pid]=0;
-              }
-              else if (pdu[j] == 2) {
-                sched_ctl->round[i][harq_pid]++;
-              }
-              else AssertFatal(1==0,"Illegal hack_nak value %d for CC %d harq_pid %d UE %d/%x\n",
-                               pdu[j],i,harq_pid,UE_id,rnti);
-              j++;
-            }
-            else AssertFatal(1==0,"Illegal value for spatial_bundling %d\n",spatial_bundling);
-          }
-        }
-        break;
-      case 3: // Format 4
-        AssertFatal(1==0,"Should not receive harq indication with Format 4\n");
-        break;
-      case 4: // Format 5
-        AssertFatal(1==0,"Should not receive harq indication with Format 5\n");
-        break;
+	    break;
+	case 2:		// Format 3
+	    AssertFatal(numCC > 2,
+			"Should not receive harq indication with FDD format 3 with %d < 3 active CCs\n",
+			numCC);
+	    for (i = 0, j = 0; i < numCC; i++) {
+		if ((sched_ctl->round[i][harq_pid] < 8)) {
+		    if (tmode[i] == 1 || tmode[i] == 2 || tmode[0] == 5
+			|| tmode[0] == 6 || tmode[0] == 7) {
+			if (pdu[j] == 1) {
+			    sched_ctl->round[i][harq_pid] = 8;
+			    sched_ctl->tbcnt[i][harq_pid] = 0;
+			} else if (pdu[j] == 2)
+			    sched_ctl->round[i][harq_pid]++;
+			else
+			    AssertFatal(1 == 0,
+					"Illegal harq_ack value for CC %d harq_pid %d (%d) UE %d/%x\n",
+					i, harq_pid, pdu[j], UE_id, rnti);
+			j++;
+		    } else if (spatial_bundling == 0) {
+			if ((sched_ctl->tbcnt[i][harq_pid] == 2)
+			    && (pdu[j] == 1) && (pdu[j + 1] == 1)) {
+			    sched_ctl->round[i][harq_pid] = 8;
+			    sched_ctl->tbcnt[i][harq_pid] = 0;
+			} else if ((sched_ctl->tbcnt[i][harq_pid] == 2)
+				   && (pdu[j] == 1) && (pdu[j + 1] == 2)) {
+			    sched_ctl->round[i][harq_pid]++;
+			    sched_ctl->tbcnt[i][harq_pid] = 1;
+			} else if ((sched_ctl->tbcnt[i][harq_pid] == 2)
+				   && (pdu[j] == 2) && (pdu[j + 1] == 1)) {
+			    sched_ctl->round[i][harq_pid]++;
+			    sched_ctl->tbcnt[i][harq_pid] = 1;
+			} else if ((sched_ctl->tbcnt[i][harq_pid] == 2)
+				   && (pdu[j] == 2) && (pdu[j + 1] == 2)) {
+			    sched_ctl->round[i][harq_pid]++;
+			} else
+			    AssertFatal(1 == 0,
+					"Illegal combination for CC %d harq_pid %d (%d,%d,%d) UE %d/%x\n",
+					i, harq_pid,
+					sched_ctl->tbcnt[i][harq_pid],
+					pdu[j], pdu[j + 1], UE_id, rnti);
+			j += 2;
+		    } else if (spatial_bundling == 1) {
+			if (pdu[j] == 1) {
+			    sched_ctl->round[i][harq_pid] = 8;
+			    sched_ctl->tbcnt[i][harq_pid] = 0;
+			} else if (pdu[j] == 2) {
+			    sched_ctl->round[i][harq_pid]++;
+			} else
+			    AssertFatal(1 == 0,
+					"Illegal hack_nak value %d for CC %d harq_pid %d UE %d/%x\n",
+					pdu[j], i, harq_pid, UE_id, rnti);
+			j++;
+		    } else
+			AssertFatal(1 == 0,
+				    "Illegal value for spatial_bundling %d\n",
+				    spatial_bundling);
+		}
+	    }
+	    break;
+	case 3:		// Format 4
+	    AssertFatal(1 == 0,
+			"Should not receive harq indication with Format 4\n");
+	    break;
+	case 4:		// Format 5
+	    AssertFatal(1 == 0,
+			"Should not receive harq indication with Format 5\n");
+	    break;
+	}
     }
-  }
 }
 
-void extract_pucch_csi(module_id_t mod_idP,int CC_idP,int UE_id, frame_t frameP,sub_frame_t subframeP,uint8_t *pdu, uint8_t length)
+void
+extract_pucch_csi(module_id_t mod_idP, int CC_idP, int UE_id,
+		  frame_t frameP, sub_frame_t subframeP,
+		  uint8_t * pdu, uint8_t length)
 {
-  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
-  UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-  COMMON_channels_t *cc=&RC.mac[mod_idP]->common_channels[CC_idP];
-  struct CQI_ReportPeriodic *cqi_ReportPeriodic;
-  int no_pmi;
-  uint8_t Ltab[6] = {0,2,4,4,4,4};
-  uint8_t Jtab[6] = {0,2,2,3,4,4};
-  int feedback_cnt;
-
-  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated != NULL, "physicalConfigDedicated is null for UE %d\n",UE_id);
-  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->cqi_ReportConfig != NULL,"cqi_ReportConfig is null for UE %d\n",UE_id);
-  AssertFatal((cqi_ReportPeriodic = UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->cqi_ReportConfig->cqi_ReportPeriodic)!=NULL,
-              "cqi_ReportPeriodic is null for UE %d\n",UE_id);
-
-  // determine feedback mode
-  AssertFatal(cqi_ReportPeriodic->present != CQI_ReportPeriodic_PR_NOTHING,
-              "cqi_ReportPeriodic->present == CQI_ReportPeriodic_PR_NOTHING!\n");
-  AssertFatal(cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present != CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING,
-              "cqi_ReportPeriodic->cqi_FormatIndicatorPeriodic.choice.setup.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING!\n");
-
-  uint16_t Npd,N_OFFSET_CQI;
-  int H,K,bandwidth_part,L,Lmask;
-  int ri    = sched_ctl->periodic_ri_received[CC_idP];
-
-  get_csi_params(cc,cqi_ReportPeriodic,&Npd,&N_OFFSET_CQI,&H);
-  K            =(H-1)/Jtab[cc->mib->message.dl_Bandwidth];
-  L            = Ltab[cc->mib->message.dl_Bandwidth];
-  Lmask        =L-1;
-  feedback_cnt = (((frameP*10)+subframeP)/Npd)%H;
-
-  if (feedback_cnt>0) bandwidth_part = (feedback_cnt-1)%K;
-  else                bandwidth_part = 0;
-
-  switch(get_tmode(mod_idP,CC_idP,UE_id)) {
-  case 1:
-  case 2:
-  case 3:
-  case 7:
-    no_pmi=1;
-    break;
-  case 4:
-  case 5:
-  case 6:
-    no_pmi=0;
-    break;
-  default:
-    // note: need to check TM8-10 without PMI/RI or with 1 antenna port (see Section 5.2.3.3.1 from 36.213)
-    no_pmi=0;
-  }
-
-  if ((cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_widebandCQI) ||
-      (feedback_cnt==0)){
-      // Note: This implements only Tables: 5.3.3.1-1,5.3.3.1-1A and 5.3.3.1-2 from 36.213 (1,2,4 antenna ports Wideband CQI/PMI)
+    UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
+    UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+    COMMON_channels_t *cc = &RC.mac[mod_idP]->common_channels[CC_idP];
+    struct CQI_ReportPeriodic *cqi_ReportPeriodic;
+    int no_pmi;
+    uint8_t Ltab[6] = { 0, 2, 4, 4, 4, 4 };
+    uint8_t Jtab[6] = { 0, 2, 2, 3, 4, 4 };
+    int feedback_cnt;
+
+    AssertFatal(UE_list->UE_template[CC_idP][UE_id].
+		physicalConfigDedicated != NULL,
+		"physicalConfigDedicated is null for UE %d\n", UE_id);
+    AssertFatal(UE_list->
+		UE_template[CC_idP][UE_id].physicalConfigDedicated->
+		cqi_ReportConfig != NULL,
+		"cqi_ReportConfig is null for UE %d\n", UE_id);
+    AssertFatal((cqi_ReportPeriodic =
+		 UE_list->
+		 UE_template[CC_idP][UE_id].physicalConfigDedicated->
+		 cqi_ReportConfig->cqi_ReportPeriodic) != NULL,
+		"cqi_ReportPeriodic is null for UE %d\n", UE_id);
+
+    // determine feedback mode
+    AssertFatal(cqi_ReportPeriodic->present !=
+		CQI_ReportPeriodic_PR_NOTHING,
+		"cqi_ReportPeriodic->present == CQI_ReportPeriodic_PR_NOTHING!\n");
+    AssertFatal(cqi_ReportPeriodic->choice.
+		setup.cqi_FormatIndicatorPeriodic.present !=
+		CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING,
+		"cqi_ReportPeriodic->cqi_FormatIndicatorPeriodic.choice.setup.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING!\n");
+
+    uint16_t Npd, N_OFFSET_CQI;
+    int H, K, bandwidth_part, L, Lmask;
+    int ri = sched_ctl->periodic_ri_received[CC_idP];
+
+    get_csi_params(cc, cqi_ReportPeriodic, &Npd, &N_OFFSET_CQI, &H);
+    K = (H - 1) / Jtab[cc->mib->message.dl_Bandwidth];
+    L = Ltab[cc->mib->message.dl_Bandwidth];
+    Lmask = L - 1;
+    feedback_cnt = (((frameP * 10) + subframeP) / Npd) % H;
+
+    if (feedback_cnt > 0)
+	bandwidth_part = (feedback_cnt - 1) % K;
+    else
+	bandwidth_part = 0;
 
-    if (no_pmi == 1) { // get spatial_diffcqi if needed
-      sched_ctl->periodic_wideband_cqi[CC_idP]             = pdu[0]&0xF;
-      sched_ctl->periodic_wideband_spatial_diffcqi[CC_idP] = (pdu[0]>>4)&7;
-    }
-    else if ((cc->p_eNB==2) && (ri==1))  {
-      // p=2 Rank 1 wideband CQI/PMI 6 bits
-      sched_ctl->periodic_wideband_cqi[CC_idP]             = pdu[0]&0xF;
-      sched_ctl->periodic_wideband_pmi[CC_idP]             = (pdu[0]>>4)&3;
-    }
-    else if ((cc->p_eNB==2) && (ri>1)) {
-      // p=2 Rank 2 wideband CQI/PMI 8 bits
-      sched_ctl->periodic_wideband_cqi[CC_idP]             = pdu[0]&0xF;
-      sched_ctl->periodic_wideband_spatial_diffcqi[CC_idP] = (pdu[0]>>4)&7;
-      sched_ctl->periodic_wideband_pmi[CC_idP]             = (pdu[0]>>7)&1;
+    switch (get_tmode(mod_idP, CC_idP, UE_id)) {
+    case 1:
+    case 2:
+    case 3:
+    case 7:
+	no_pmi = 1;
+	break;
+    case 4:
+    case 5:
+    case 6:
+	no_pmi = 0;
+	break;
+    default:
+	// note: need to check TM8-10 without PMI/RI or with 1 antenna port (see Section 5.2.3.3.1 from 36.213)
+	no_pmi = 0;
     }
-    else if ((cc->p_eNB==4) && (ri==1)) {
-      // p=4 Rank 1 wideband CQI/PMI 8 bits
-      sched_ctl->periodic_wideband_cqi[CC_idP]             = pdu[0]&0xF;
-      sched_ctl->periodic_wideband_pmi[CC_idP]             = (pdu[0]>>4)&0x0F;
 
+    if ((cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.
+	 present ==
+	 CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_widebandCQI)
+	|| (feedback_cnt == 0)) {
+	// Note: This implements only Tables: 5.3.3.1-1,5.3.3.1-1A and 5.3.3.1-2 from 36.213 (1,2,4 antenna ports Wideband CQI/PMI)
+
+	if (no_pmi == 1) {	// get spatial_diffcqi if needed
+	    sched_ctl->periodic_wideband_cqi[CC_idP] = pdu[0] & 0xF;
+	    sched_ctl->periodic_wideband_spatial_diffcqi[CC_idP] =
+		(pdu[0] >> 4) & 7;
+	} else if ((cc->p_eNB == 2) && (ri == 1)) {
+	    // p=2 Rank 1 wideband CQI/PMI 6 bits
+	    sched_ctl->periodic_wideband_cqi[CC_idP] = pdu[0] & 0xF;
+	    sched_ctl->periodic_wideband_pmi[CC_idP] = (pdu[0] >> 4) & 3;
+	} else if ((cc->p_eNB == 2) && (ri > 1)) {
+	    // p=2 Rank 2 wideband CQI/PMI 8 bits
+	    sched_ctl->periodic_wideband_cqi[CC_idP] = pdu[0] & 0xF;
+	    sched_ctl->periodic_wideband_spatial_diffcqi[CC_idP] =
+		(pdu[0] >> 4) & 7;
+	    sched_ctl->periodic_wideband_pmi[CC_idP] = (pdu[0] >> 7) & 1;
+	} else if ((cc->p_eNB == 4) && (ri == 1)) {
+	    // p=4 Rank 1 wideband CQI/PMI 8 bits
+	    sched_ctl->periodic_wideband_cqi[CC_idP] = pdu[0] & 0xF;
+	    sched_ctl->periodic_wideband_pmi[CC_idP] =
+		(pdu[0] >> 4) & 0x0F;
+
+	} else if ((cc->p_eNB == 4) && (ri > 1)) {
+	    // p=4 Rank 2 wideband CQI/PMI 11 bits
+	    sched_ctl->periodic_wideband_cqi[CC_idP] = pdu[0] & 0xF;
+	    sched_ctl->periodic_wideband_spatial_diffcqi[CC_idP] =
+		(pdu[0] >> 4) & 7;
+	    sched_ctl->periodic_wideband_pmi[CC_idP] = (pdu[0] >> 7) & 0xF;
+	} else
+	    AssertFatal(1 == 0,
+			"illegal combination p %d, ri %d, no_pmi %d\n",
+			cc->p_eNB, ri, no_pmi);
+    } else if (cqi_ReportPeriodic->choice.
+	       setup.cqi_FormatIndicatorPeriodic.present ==
+	       CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_subbandCQI)
+    {
+	// This is Table 5.2.3.3.2-2 for 36.213
+	if (ri == 1) {
+	    //4+Ltab[cc->mib->message.dl_Bandwidth] bits
+	    sched_ctl->periodic_subband_cqi[CC_idP][(bandwidth_part * L) +
+						    ((pdu[0] >> 4) &
+						     Lmask)] =
+		pdu[0] & 0xF;
+	} else if (ri > 1) {
+	    //7+Ltab[cc->mib->message.dl_Bandwidth] bits;
+	    sched_ctl->
+		periodic_subband_spatial_diffcqi[CC_idP][(bandwidth_part *
+							  L) +
+							 ((pdu[0] >> 7) &
+							  Lmask)] =
+		(pdu[0] >> 4) & 7;
+	    sched_ctl->periodic_subband_cqi[CC_idP][(bandwidth_part * L) +
+						    ((pdu[0] >> 7) &
+						     Lmask)] =
+		pdu[0] & 0xF;
+	}
     }
-    else if ((cc->p_eNB==4) && (ri>1)) {
-      // p=4 Rank 2 wideband CQI/PMI 11 bits
-      sched_ctl->periodic_wideband_cqi[CC_idP]             = pdu[0]&0xF;
-      sched_ctl->periodic_wideband_spatial_diffcqi[CC_idP] = (pdu[0]>>4)&7;
-      sched_ctl->periodic_wideband_pmi[CC_idP]             = (pdu[0]>>7)&0xF;
-    }
-    else AssertFatal(1==0,"illegal combination p %d, ri %d, no_pmi %d\n",cc->p_eNB,ri,no_pmi);
-  }
-  else if (cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_subbandCQI) {
-    // This is Table 5.2.3.3.2-2 for 36.213
-    if (ri==1) {
-      //4+Ltab[cc->mib->message.dl_Bandwidth] bits
-      sched_ctl->periodic_subband_cqi[CC_idP][(bandwidth_part*L)+((pdu[0]>>4)&Lmask)]              = pdu[0]&0xF;
-    }
-    else if (ri>1) {
-      //7+Ltab[cc->mib->message.dl_Bandwidth] bits;
-      sched_ctl->periodic_subband_spatial_diffcqi[CC_idP][(bandwidth_part*L)+((pdu[0]>>7)&Lmask)]  = (pdu[0]>>4)&7;
-      sched_ctl->periodic_subband_cqi[CC_idP][(bandwidth_part*L)+((pdu[0]>>7)&Lmask)]              = pdu[0]&0xF;
-    }
-  }
 }
 
-void extract_pusch_csi(module_id_t mod_idP,int CC_idP,int UE_id, frame_t frameP,sub_frame_t subframeP,uint8_t *pdu, uint8_t length)
-{
-  UE_list_t *UE_list    = &RC.mac[mod_idP]->UE_list;
-  COMMON_channels_t *cc = &RC.mac[mod_idP]->common_channels[CC_idP];
-  UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-  int Ntab[6]       = {0,4,7,9,10,13};
-  int Ntab_uesel[6] = {0,8,13,17,19,25};
-  int Ltab_uesel[6] = {0,6,9,13,15,18};
-  int Mtab_uesel[6] = {0,1,3,5,6,6};
-  int v[6];
-  int i;
-  uint64_t p = *(uint64_t*)pdu;
-  int curbyte, curbit;
-  CQI_ReportModeAperiodic_t *cqi_ReportModeAperiodic;
-
-  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated != NULL, "physicalConfigDedicated is null for UE %d\n",UE_id);
-  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->cqi_ReportConfig != NULL,"cqi_ReportConfig is null for UE %d\n",UE_id);
-  AssertFatal((cqi_ReportModeAperiodic = UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->cqi_ReportConfig->cqi_ReportModeAperiodic)!=NULL,
-              "cqi_ReportModeAperiodic is null for UE %d\n",UE_id);
-
-  int N     = Ntab[cc->mib->message.dl_Bandwidth];
-  int tmode = get_tmode(mod_idP,CC_idP,UE_id);
-  int ri    = sched_ctl->aperiodic_ri_received[CC_idP];
-  int r,diffcqi0=0,diffcqi1=0,pmi_uesel=0;
-  int bw = cc->mib->message.dl_Bandwidth;
-  int m;
-
-  switch(*cqi_ReportModeAperiodic) {
-  case CQI_ReportModeAperiodic_rm12:
-    AssertFatal(0==1, "to be fixed, don't use p but pdu directly\n");
-    // wideband multiple PMI (TM4/6), Table 5.2.2.6.1-1 (for TM4/6)
-    AssertFatal(tmode==4 || tmode==6 || tmode==8|| tmode==9 || tmode==10,"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm12\n",tmode);
-    if (tmode <= 6) { //Table 5.2.2.6.1-1 36.213
-      if ((ri==1) && (cc->p_eNB==2)) {
-        sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-        for (i=0;i<N;i++) {
-          sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t)(p&0x03);
-          p>>=2;
-        }
-      }
-      if ((ri==2) && (cc->p_eNB==2)) {
-        sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-        sched_ctl->aperiodic_wideband_cqi1[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-        for (i=0;i<N;i++) {
-          sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t)(p&0x01);
-          p>>=1;
-        }
-      }
-      if ((ri==1) && (cc->p_eNB==4)) {
-        sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-        for (i=0;i<N;i++) {
-          sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t)(p&0x03);
-          p>>=4;
-        }
-      }
-      if ((ri==2) && (cc->p_eNB==4)) {
-        sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-        sched_ctl->aperiodic_wideband_cqi1[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-        for (i=0;i<N;i++) {
-          sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t)(p&0x01);
-          p>>=4;
-        }
-      }
-    } // if (tmode <= 6) { //Table 5.2.2.6.1-1 36.213
-    else {
-      AssertFatal(1==0,"support for TM 8-10 to be done\n");
-    }
-
-   break;
-  case CQI_ReportModeAperiodic_rm20:
-    AssertFatal(0==1, "to be fixed, don't use p but pdu directly\n");
-    // UE-selected subband CQI no PMI (TM1/2/3/7) , Table 5.2.2.6.3-1 from 36.213
-    AssertFatal(tmode==1 || tmode==2 || tmode==3 || tmode==7,"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm20\n",tmode);
-
-    sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-    diffcqi0 = (uint8_t)(p&0x03); p>>=2;
-    r = (uint8_t)(p&((1>>Ltab_uesel[bw])-1));
-    reverse_index(Ntab_uesel[bw],Mtab_uesel[bw],r,v);
-    for (m=0;m<Mtab_uesel[bw];m++) sched_ctl->aperiodic_subband_diffcqi0[CC_idP][v[m]] = diffcqi0;
-    break;
-  case CQI_ReportModeAperiodic_rm22:
-    AssertFatal(0==1, "to be fixed, don't use p but pdu directly\n");
-    // UE-selected subband CQI multiple PMI (TM4/6) Table 5.2.2.6.3-2 from 36.213
-
-    AssertFatal(tmode==4 || tmode==6 || tmode==8|| tmode==9 || tmode==10,"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm22\n",tmode);
-
-    sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-    diffcqi0 = (uint8_t)(p&0x03); p>>=2;
-
-    if (ri>1) {
-      sched_ctl->aperiodic_wideband_cqi1[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-      diffcqi1 = (uint8_t)(p&0x03); p>>=2;
-    }
-    r = (uint8_t)(p&((1>>Ltab_uesel[bw])-1)); p>>=Ltab_uesel[bw];
-    reverse_index(Ntab_uesel[bw],Mtab_uesel[bw],r,v);
-    if ((ri==1)&&(cc->p_eNB==2)) {
-      pmi_uesel                                  = p&0x3; p>>=2;
-      sched_ctl->aperiodic_wideband_pmi[CC_idP]  = p&0x3;
-    }
-    else if ((ri==2)&&(cc->p_eNB==2)) {
-      pmi_uesel                                  = p&0x1; p>>=1;
-      sched_ctl->aperiodic_wideband_pmi[CC_idP]  = p&0x1;
-    }
-    else if (cc->p_eNB==4) {
-      pmi_uesel                                  = p&0x0F; p>>=4;
-      sched_ctl->aperiodic_wideband_pmi[CC_idP]  = p&0x0F;
-    }
-    for (m=0;m<Mtab_uesel[bw];m++) {
-      sched_ctl->aperiodic_subband_diffcqi0[CC_idP][v[m]] = diffcqi0;
-      if (ri>1) sched_ctl->aperiodic_subband_diffcqi1[CC_idP][v[m]] = diffcqi1;
-      sched_ctl->aperiodic_subband_pmi[CC_idP][v[m]]      = pmi_uesel;
-    }
-    break;
-  case CQI_ReportModeAperiodic_rm30:
-    //subband CQI no PMI (TM1/2/3/7)
-    AssertFatal(tmode==1 || tmode==2 || tmode==3 || tmode==7,"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm30\n",tmode);
-    sched_ctl->aperiodic_wideband_cqi0[CC_idP] = pdu[0]>>4;
-    curbyte = 0;
-    curbit = 3;
-    for (i=0;i<N;i++) {
-      sched_ctl->aperiodic_subband_diffcqi0[CC_idP][i] = (pdu[curbyte] >> (curbit-1)) & 0x03;
-      curbit -= 2;
-      if (curbit < 0) {
-        curbit = 7;
-        curbyte++;
-      }
-    }
-    sched_ctl->dl_cqi[CC_idP] = sched_ctl->aperiodic_wideband_cqi0[CC_idP];
-    break;
-  case CQI_ReportModeAperiodic_rm31:
-    AssertFatal(0==1, "to be fixed, don't use p but pdu directly\n");
-    //subband CQI single PMI (TM4/5/6)
-    AssertFatal(tmode==4 || tmode==5 || tmode==6 || tmode==8|| tmode==9|| tmode==10,"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm31\n",tmode);
-
-    if ((ri==1) && (cc->p_eNB==2)) {
-      sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-      for (i=0;i<N;i++) {
-        sched_ctl->aperiodic_subband_diffcqi0[CC_idP][i] = (uint8_t)(p&0x03);
-        p>>=2;
-      }
-      sched_ctl->aperiodic_wideband_pmi[CC_idP] = p&0x03;
-    }
-    if ((ri==2) && (cc->p_eNB==2)) {
-      sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-      for (i=0;i<N;i++) {
-        sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t)(p&0x01);
-        p>>=1;
-      }
-      sched_ctl->aperiodic_wideband_cqi1[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-      for (i=0;i<N;i++) {
-        sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t)(p&0x01);
-        p>>=1;
-      }
-      sched_ctl->aperiodic_wideband_pmi[CC_idP] = p&0x01;
-    }
-    if ((ri==1) && (cc->p_eNB==4)) {
-      sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-      for (i=0;i<N;i++) {
-        sched_ctl->aperiodic_subband_diffcqi0[CC_idP][i] = (uint8_t)(p&0x03);
-        p>>=2;
-      }
-      sched_ctl->aperiodic_wideband_pmi[CC_idP] = p&0x0F;
-    }
-    if ((ri>1) && (cc->p_eNB==4)) { // Note : 64 bits for 20 MHz
-      sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-      for (i=0;i<N;i++) {
-        sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t)(p&0x01);
-        p>>=1;
-      }
-      sched_ctl->aperiodic_wideband_cqi1[CC_idP] = (uint8_t)(p&0x0F); p>>=4;
-      for (i=0;i<N;i++) {
-        sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t)(p&0x01);
-        p>>=2;
-      }
-      sched_ctl->aperiodic_wideband_pmi[CC_idP] = p&0x0F;
+void
+extract_pusch_csi(module_id_t mod_idP, int CC_idP, int UE_id,
+		  frame_t frameP, sub_frame_t subframeP,
+		  uint8_t * pdu, uint8_t length)
+{
+    UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
+    COMMON_channels_t *cc = &RC.mac[mod_idP]->common_channels[CC_idP];
+    UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+    int Ntab[6] = { 0, 4, 7, 9, 10, 13 };
+    int Ntab_uesel[6] = { 0, 8, 13, 17, 19, 25 };
+    int Ltab_uesel[6] = { 0, 6, 9, 13, 15, 18 };
+    int Mtab_uesel[6] = { 0, 1, 3, 5, 6, 6 };
+    int v[6];
+    int i;
+    uint64_t p = *(uint64_t *) pdu;
+    int curbyte, curbit;
+    CQI_ReportModeAperiodic_t *cqi_ReportModeAperiodic;
+
+    AssertFatal(UE_list->UE_template[CC_idP][UE_id].
+		physicalConfigDedicated != NULL,
+		"physicalConfigDedicated is null for UE %d\n", UE_id);
+    AssertFatal(UE_list->
+		UE_template[CC_idP][UE_id].physicalConfigDedicated->
+		cqi_ReportConfig != NULL,
+		"cqi_ReportConfig is null for UE %d\n", UE_id);
+    AssertFatal((cqi_ReportModeAperiodic =
+		 UE_list->
+		 UE_template[CC_idP][UE_id].physicalConfigDedicated->
+		 cqi_ReportConfig->cqi_ReportModeAperiodic) != NULL,
+		"cqi_ReportModeAperiodic is null for UE %d\n", UE_id);
+
+    int N = Ntab[cc->mib->message.dl_Bandwidth];
+    int tmode = get_tmode(mod_idP, CC_idP, UE_id);
+    int ri = sched_ctl->aperiodic_ri_received[CC_idP];
+    int r, diffcqi0 = 0, diffcqi1 = 0, pmi_uesel = 0;
+    int bw = cc->mib->message.dl_Bandwidth;
+    int m;
+
+    switch (*cqi_ReportModeAperiodic) {
+    case CQI_ReportModeAperiodic_rm12:
+	AssertFatal(0 == 1, "to be fixed, don't use p but pdu directly\n");
+	// wideband multiple PMI (TM4/6), Table 5.2.2.6.1-1 (for TM4/6)
+	AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9
+		    || tmode == 10,
+		    "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm12\n",
+		    tmode);
+	if (tmode <= 6) {	//Table 5.2.2.6.1-1 36.213
+	    if ((ri == 1) && (cc->p_eNB == 2)) {
+		sched_ctl->aperiodic_wideband_cqi0[CC_idP] =
+		    (uint8_t) (p & 0x0F);
+		p >>= 4;
+		for (i = 0; i < N; i++) {
+		    sched_ctl->aperiodic_subband_pmi[CC_idP][i] =
+			(uint8_t) (p & 0x03);
+		    p >>= 2;
+		}
+	    }
+	    if ((ri == 2) && (cc->p_eNB == 2)) {
+		sched_ctl->aperiodic_wideband_cqi0[CC_idP] =
+		    (uint8_t) (p & 0x0F);
+		p >>= 4;
+		sched_ctl->aperiodic_wideband_cqi1[CC_idP] =
+		    (uint8_t) (p & 0x0F);
+		p >>= 4;
+		for (i = 0; i < N; i++) {
+		    sched_ctl->aperiodic_subband_pmi[CC_idP][i] =
+			(uint8_t) (p & 0x01);
+		    p >>= 1;
+		}
+	    }
+	    if ((ri == 1) && (cc->p_eNB == 4)) {
+		sched_ctl->aperiodic_wideband_cqi0[CC_idP] =
+		    (uint8_t) (p & 0x0F);
+		p >>= 4;
+		for (i = 0; i < N; i++) {
+		    sched_ctl->aperiodic_subband_pmi[CC_idP][i] =
+			(uint8_t) (p & 0x03);
+		    p >>= 4;
+		}
+	    }
+	    if ((ri == 2) && (cc->p_eNB == 4)) {
+		sched_ctl->aperiodic_wideband_cqi0[CC_idP] =
+		    (uint8_t) (p & 0x0F);
+		p >>= 4;
+		sched_ctl->aperiodic_wideband_cqi1[CC_idP] =
+		    (uint8_t) (p & 0x0F);
+		p >>= 4;
+		for (i = 0; i < N; i++) {
+		    sched_ctl->aperiodic_subband_pmi[CC_idP][i] =
+			(uint8_t) (p & 0x01);
+		    p >>= 4;
+		}
+	    }
+	}			// if (tmode <= 6) { //Table 5.2.2.6.1-1 36.213
+	else {
+	    AssertFatal(1 == 0, "support for TM 8-10 to be done\n");
+	}
+
+	break;
+    case CQI_ReportModeAperiodic_rm20:
+	AssertFatal(0 == 1, "to be fixed, don't use p but pdu directly\n");
+	// UE-selected subband CQI no PMI (TM1/2/3/7) , Table 5.2.2.6.3-1 from 36.213
+	AssertFatal(tmode == 1 || tmode == 2 || tmode == 3
+		    || tmode == 7,
+		    "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm20\n",
+		    tmode);
+
+	sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t) (p & 0x0F);
+	p >>= 4;
+	diffcqi0 = (uint8_t) (p & 0x03);
+	p >>= 2;
+	r = (uint8_t) (p & ((1 >> Ltab_uesel[bw]) - 1));
+	reverse_index(Ntab_uesel[bw], Mtab_uesel[bw], r, v);
+	for (m = 0; m < Mtab_uesel[bw]; m++)
+	    sched_ctl->aperiodic_subband_diffcqi0[CC_idP][v[m]] = diffcqi0;
+	break;
+    case CQI_ReportModeAperiodic_rm22:
+	AssertFatal(0 == 1, "to be fixed, don't use p but pdu directly\n");
+	// UE-selected subband CQI multiple PMI (TM4/6) Table 5.2.2.6.3-2 from 36.213
+
+	AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9
+		    || tmode == 10,
+		    "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm22\n",
+		    tmode);
+
+	sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t) (p & 0x0F);
+	p >>= 4;
+	diffcqi0 = (uint8_t) (p & 0x03);
+	p >>= 2;
+
+	if (ri > 1) {
+	    sched_ctl->aperiodic_wideband_cqi1[CC_idP] =
+		(uint8_t) (p & 0x0F);
+	    p >>= 4;
+	    diffcqi1 = (uint8_t) (p & 0x03);
+	    p >>= 2;
+	}
+	r = (uint8_t) (p & ((1 >> Ltab_uesel[bw]) - 1));
+	p >>= Ltab_uesel[bw];
+	reverse_index(Ntab_uesel[bw], Mtab_uesel[bw], r, v);
+	if ((ri == 1) && (cc->p_eNB == 2)) {
+	    pmi_uesel = p & 0x3;
+	    p >>= 2;
+	    sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x3;
+	} else if ((ri == 2) && (cc->p_eNB == 2)) {
+	    pmi_uesel = p & 0x1;
+	    p >>= 1;
+	    sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x1;
+	} else if (cc->p_eNB == 4) {
+	    pmi_uesel = p & 0x0F;
+	    p >>= 4;
+	    sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x0F;
+	}
+	for (m = 0; m < Mtab_uesel[bw]; m++) {
+	    sched_ctl->aperiodic_subband_diffcqi0[CC_idP][v[m]] = diffcqi0;
+	    if (ri > 1)
+		sched_ctl->aperiodic_subband_diffcqi1[CC_idP][v[m]] =
+		    diffcqi1;
+	    sched_ctl->aperiodic_subband_pmi[CC_idP][v[m]] = pmi_uesel;
+	}
+	break;
+    case CQI_ReportModeAperiodic_rm30:
+	//subband CQI no PMI (TM1/2/3/7)
+	AssertFatal(tmode == 1 || tmode == 2 || tmode == 3
+		    || tmode == 7,
+		    "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm30\n",
+		    tmode);
+	sched_ctl->aperiodic_wideband_cqi0[CC_idP] = pdu[0] >> 4;
+	curbyte = 0;
+	curbit = 3;
+	for (i = 0; i < N; i++) {
+	    sched_ctl->aperiodic_subband_diffcqi0[CC_idP][i] =
+		(pdu[curbyte] >> (curbit - 1)) & 0x03;
+	    curbit -= 2;
+	    if (curbit < 0) {
+		curbit = 7;
+		curbyte++;
+	    }
+	}
+	sched_ctl->dl_cqi[CC_idP] =
+	    sched_ctl->aperiodic_wideband_cqi0[CC_idP];
+	break;
+    case CQI_ReportModeAperiodic_rm31:
+	AssertFatal(0 == 1, "to be fixed, don't use p but pdu directly\n");
+	//subband CQI single PMI (TM4/5/6)
+	AssertFatal(tmode == 4 || tmode == 5 || tmode == 6 || tmode == 8
+		    || tmode == 9
+		    || tmode == 10,
+		    "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm31\n",
+		    tmode);
+
+	if ((ri == 1) && (cc->p_eNB == 2)) {
+	    sched_ctl->aperiodic_wideband_cqi0[CC_idP] =
+		(uint8_t) (p & 0x0F);
+	    p >>= 4;
+	    for (i = 0; i < N; i++) {
+		sched_ctl->aperiodic_subband_diffcqi0[CC_idP][i] =
+		    (uint8_t) (p & 0x03);
+		p >>= 2;
+	    }
+	    sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x03;
+	}
+	if ((ri == 2) && (cc->p_eNB == 2)) {
+	    sched_ctl->aperiodic_wideband_cqi0[CC_idP] =
+		(uint8_t) (p & 0x0F);
+	    p >>= 4;
+	    for (i = 0; i < N; i++) {
+		sched_ctl->aperiodic_subband_pmi[CC_idP][i] =
+		    (uint8_t) (p & 0x01);
+		p >>= 1;
+	    }
+	    sched_ctl->aperiodic_wideband_cqi1[CC_idP] =
+		(uint8_t) (p & 0x0F);
+	    p >>= 4;
+	    for (i = 0; i < N; i++) {
+		sched_ctl->aperiodic_subband_pmi[CC_idP][i] =
+		    (uint8_t) (p & 0x01);
+		p >>= 1;
+	    }
+	    sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x01;
+	}
+	if ((ri == 1) && (cc->p_eNB == 4)) {
+	    sched_ctl->aperiodic_wideband_cqi0[CC_idP] =
+		(uint8_t) (p & 0x0F);
+	    p >>= 4;
+	    for (i = 0; i < N; i++) {
+		sched_ctl->aperiodic_subband_diffcqi0[CC_idP][i] =
+		    (uint8_t) (p & 0x03);
+		p >>= 2;
+	    }
+	    sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x0F;
+	}
+	if ((ri > 1) && (cc->p_eNB == 4)) {	// Note : 64 bits for 20 MHz
+	    sched_ctl->aperiodic_wideband_cqi0[CC_idP] =
+		(uint8_t) (p & 0x0F);
+	    p >>= 4;
+	    for (i = 0; i < N; i++) {
+		sched_ctl->aperiodic_subband_pmi[CC_idP][i] =
+		    (uint8_t) (p & 0x01);
+		p >>= 1;
+	    }
+	    sched_ctl->aperiodic_wideband_cqi1[CC_idP] =
+		(uint8_t) (p & 0x0F);
+	    p >>= 4;
+	    for (i = 0; i < N; i++) {
+		sched_ctl->aperiodic_subband_pmi[CC_idP][i] =
+		    (uint8_t) (p & 0x01);
+		p >>= 2;
+	    }
+	    sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x0F;
+	}
+
+	break;
+    case CQI_ReportModeAperiodic_rm32_v1250:
+	AssertFatal(tmode == 4 || tmode == 5 || tmode == 6 || tmode == 8
+		    || tmode == 9
+		    || tmode == 10,
+		    "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm32\n",
+		    tmode);
+	AssertFatal(1 == 0, "CQI_ReportModeAperiodic_rm32 to be done\n");
+	break;
+    case CQI_ReportModeAperiodic_rm10_v1310:
+	AssertFatal(tmode == 1 || tmode == 2 || tmode == 3
+		    || tmode == 7,
+		    "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm10\n",
+		    tmode);
+	AssertFatal(1 == 0, "CQI_ReportModeAperiodic_rm10 to be done\n");
+	break;
+    case CQI_ReportModeAperiodic_rm11_v1310:
+	AssertFatal(tmode == 4 || tmode == 5 || tmode == 6 || tmode == 8
+		    || tmode == 9
+		    || tmode == 10,
+		    "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm11\n",
+		    tmode);
+	AssertFatal(1 == 0, "CQI_ReportModeAperiodic_rm11 to be done\n");
+	break;
     }
-
-    break;
-  case CQI_ReportModeAperiodic_rm32_v1250:
-    AssertFatal(tmode==4 || tmode==5 || tmode==6 || tmode==8|| tmode==9|| tmode==10,"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm32\n",tmode);
-    AssertFatal(1==0,"CQI_ReportModeAperiodic_rm32 to be done\n");
-    break;
-  case CQI_ReportModeAperiodic_rm10_v1310:
-    AssertFatal(tmode==1 || tmode==2 || tmode==3 || tmode==7,"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm10\n",tmode);
-    AssertFatal(1==0,"CQI_ReportModeAperiodic_rm10 to be done\n");
-    break;
-  case CQI_ReportModeAperiodic_rm11_v1310:
-    AssertFatal(tmode==4 || tmode==5 || tmode==6 || tmode==8|| tmode==9|| tmode==10,"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm11\n",tmode);
-    AssertFatal(1==0,"CQI_ReportModeAperiodic_rm11 to be done\n");
-    break;
-  }
 }
 
-void cqi_indication(module_id_t mod_idP, int CC_idP, frame_t frameP, sub_frame_t subframeP, rnti_t rntiP,
-                    nfapi_cqi_indication_rel9_t *rel9,uint8_t *pdu,
-                    nfapi_ul_cqi_information_t *ul_cqi_information)
+void
+cqi_indication(module_id_t mod_idP, int CC_idP, frame_t frameP,
+	       sub_frame_t subframeP, rnti_t rntiP,
+	       nfapi_cqi_indication_rel9_t * rel9, uint8_t * pdu,
+	       nfapi_ul_cqi_information_t * ul_cqi_information)
 {
-  int UE_id = find_UE_id(mod_idP, rntiP);
-  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
-  if (UE_id == -1) {
-    LOG_W(MAC, "cqi_indication: UE %x not found\n", rntiP);
-    return;
-  }
-  UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+    int UE_id = find_UE_id(mod_idP, rntiP);
+    UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
+    if (UE_id == -1) {
+	LOG_W(MAC, "cqi_indication: UE %x not found\n", rntiP);
+	return;
+    }
+    UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
 
-  if (UE_id  >= 0) {
+    if (UE_id >= 0) {
 
-    if (ul_cqi_information->channel == 0) { // PUCCH
+	if (ul_cqi_information->channel == 0) {	// PUCCH
 
-      // extract pucch csi information before changing RI information
-      extract_pucch_csi(mod_idP,CC_idP,UE_id,frameP,subframeP,pdu,rel9->length);
+	    // extract pucch csi information before changing RI information
+	    extract_pucch_csi(mod_idP, CC_idP, UE_id, frameP, subframeP,
+			      pdu, rel9->length);
 
-      memcpy((void*)sched_ctl->periodic_ri_received,
-             (void*)rel9->ri,
-             rel9->number_of_cc_reported);
+	    memcpy((void *) sched_ctl->periodic_ri_received,
+		   (void *) rel9->ri, rel9->number_of_cc_reported);
 
-      // SNR for PUCCH2
-      sched_ctl->pucch2_snr[CC_idP] = ul_cqi_information->ul_cqi;
-    }
-    else { //PUSCH
-      memcpy((void*)sched_ctl->aperiodic_ri_received,
-             (void*)rel9->ri,
-             rel9->number_of_cc_reported);
+	    // SNR for PUCCH2
+	    sched_ctl->pucch2_snr[CC_idP] = ul_cqi_information->ul_cqi;
+	} else {		//PUSCH
+	    memcpy((void *) sched_ctl->aperiodic_ri_received,
+		   (void *) rel9->ri, rel9->number_of_cc_reported);
 
-      extract_pusch_csi(mod_idP,CC_idP,UE_id,frameP,subframeP,pdu,rel9->length);
+	    extract_pusch_csi(mod_idP, CC_idP, UE_id, frameP, subframeP,
+			      pdu, rel9->length);
 
-    }
+	}
 
-    // timing advance
-    sched_ctl->timing_advance    = rel9->timing_advance;
-    sched_ctl->timing_advance_r9 = rel9->timing_advance_r9;
-  }
+	// timing advance
+	sched_ctl->timing_advance = rel9->timing_advance;
+	sched_ctl->timing_advance_r9 = rel9->timing_advance_r9;
+    }
 }
 
-void SR_indication(module_id_t mod_idP, int cc_idP, frame_t frameP, sub_frame_t subframeP, rnti_t rntiP, uint8_t ul_cqi)
+void
+SR_indication(module_id_t mod_idP, int cc_idP, frame_t frameP,
+	      sub_frame_t subframeP, rnti_t rntiP, uint8_t ul_cqi)
 {
-  T(T_ENB_MAC_SCHEDULING_REQUEST, T_INT(mod_idP), T_INT(cc_idP), T_INT(frameP), T_INT(subframeP), T_INT(rntiP));
+    T(T_ENB_MAC_SCHEDULING_REQUEST, T_INT(mod_idP), T_INT(cc_idP),
+      T_INT(frameP), T_INT(subframeP), T_INT(rntiP));
 
-  int UE_id = find_UE_id(mod_idP, rntiP);
-  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
+    int UE_id = find_UE_id(mod_idP, rntiP);
+    UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
 
-  if (UE_id  != -1) {
-    if (mac_eNB_get_rrc_status(mod_idP,UE_RNTI(mod_idP,UE_id)) < RRC_CONNECTED)
-      LOG_D(MAC,"[eNB %d][SR %x] Frame %d subframeP %d Signaling SR for UE %d on CC_id %d\n",mod_idP,rntiP,frameP,subframeP, UE_id,cc_idP);
+    if (UE_id != -1) {
+	if (mac_eNB_get_rrc_status(mod_idP, UE_RNTI(mod_idP, UE_id)) <
+	    RRC_CONNECTED)
+	    LOG_D(MAC,
+		  "[eNB %d][SR %x] Frame %d subframeP %d Signaling SR for UE %d on CC_id %d\n",
+		  mod_idP, rntiP, frameP, subframeP, UE_id, cc_idP);
 
 #if 0
-    UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+	UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
 
-    /* for the moment don't use ul_cqi from SR, value is too different from harq */
-    sched_ctl->pucch1_snr[cc_idP]        = ul_cqi;
-    sched_ctl->pucch1_cqi_update[cc_idP] = 1;
+	/* for the moment don't use ul_cqi from SR, value is too different from harq */
+	sched_ctl->pucch1_snr[cc_idP] = ul_cqi;
+	sched_ctl->pucch1_cqi_update[cc_idP] = 1;
 #endif
 
-    UE_list->UE_template[cc_idP][UE_id].ul_SR = 1;
-    UE_list->UE_template[cc_idP][UE_id].ul_active = TRUE;
-    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_SR_INDICATION,1);
-    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_SR_INDICATION,0);
-  } else {
-    //     AssertFatal(0, "find_UE_id(%u,rnti %d) not found", enb_mod_idP, rntiP);
-    //    AssertError(0, 0, "Frame %d: find_UE_id(%u,rnti %d) not found\n", frameP, enb_mod_idP, rntiP);
-    LOG_D(MAC,"[eNB %d][SR %x] Frame %d subframeP %d Signaling SR for UE %d (unknown UEid) on CC_id %d\n",mod_idP,rntiP,frameP,subframeP, UE_id,cc_idP);
-  }
+	UE_list->UE_template[cc_idP][UE_id].ul_SR = 1;
+	UE_list->UE_template[cc_idP][UE_id].ul_active = TRUE;
+	VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	    (VCD_SIGNAL_DUMPER_FUNCTIONS_SR_INDICATION, 1);
+	VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	    (VCD_SIGNAL_DUMPER_FUNCTIONS_SR_INDICATION, 0);
+    } else {
+	//     AssertFatal(0, "find_UE_id(%u,rnti %d) not found", enb_mod_idP, rntiP);
+	//    AssertError(0, 0, "Frame %d: find_UE_id(%u,rnti %d) not found\n", frameP, enb_mod_idP, rntiP);
+	LOG_D(MAC,
+	      "[eNB %d][SR %x] Frame %d subframeP %d Signaling SR for UE %d (unknown UEid) on CC_id %d\n",
+	      mod_idP, rntiP, frameP, subframeP, UE_id, cc_idP);
+    }
 }
 
-void UL_failure_indication(module_id_t mod_idP, int cc_idP, frame_t frameP, rnti_t rntiP, sub_frame_t subframeP)
+void
+UL_failure_indication(module_id_t mod_idP, int cc_idP, frame_t frameP,
+		      rnti_t rntiP, sub_frame_t subframeP)
 {
-  int UE_id = find_UE_id(mod_idP, rntiP);
-  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
-
-  if (UE_id  != -1) {
-    LOG_D(MAC,"[eNB %d][UE %d/%x] Frame %d subframeP %d Signaling UL Failure for UE %d on CC_id %d (timer %d)\n",
-          mod_idP,UE_id,rntiP,frameP,subframeP, UE_id,cc_idP,
-          UE_list->UE_sched_ctrl[UE_id].ul_failure_timer);
-    if (UE_list->UE_sched_ctrl[UE_id].ul_failure_timer == 0)
-      UE_list->UE_sched_ctrl[UE_id].ul_failure_timer=1;
-  } else {
-    //     AssertFatal(0, "find_UE_id(%u,rnti %d) not found", enb_mod_idP, rntiP);
-    //    AssertError(0, 0, "Frame %d: find_UE_id(%u,rnti %d) not found\n", frameP, enb_mod_idP, rntiP);
-    LOG_W(MAC,"[eNB %d][SR %x] Frame %d subframeP %d Signaling UL Failure for UE %d (unknown UEid) on CC_id %d\n",mod_idP,rntiP,frameP,subframeP, UE_id,cc_idP);
-  }
+    int UE_id = find_UE_id(mod_idP, rntiP);
+    UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
+
+    if (UE_id != -1) {
+	LOG_D(MAC,
+	      "[eNB %d][UE %d/%x] Frame %d subframeP %d Signaling UL Failure for UE %d on CC_id %d (timer %d)\n",
+	      mod_idP, UE_id, rntiP, frameP, subframeP, UE_id, cc_idP,
+	      UE_list->UE_sched_ctrl[UE_id].ul_failure_timer);
+	if (UE_list->UE_sched_ctrl[UE_id].ul_failure_timer == 0)
+	    UE_list->UE_sched_ctrl[UE_id].ul_failure_timer = 1;
+    } else {
+	//     AssertFatal(0, "find_UE_id(%u,rnti %d) not found", enb_mod_idP, rntiP);
+	//    AssertError(0, 0, "Frame %d: find_UE_id(%u,rnti %d) not found\n", frameP, enb_mod_idP, rntiP);
+	LOG_W(MAC,
+	      "[eNB %d][SR %x] Frame %d subframeP %d Signaling UL Failure for UE %d (unknown UEid) on CC_id %d\n",
+	      mod_idP, rntiP, frameP, subframeP, UE_id, cc_idP);
+    }
 }
 
-void harq_indication(module_id_t mod_idP, int CC_idP, frame_t frameP, sub_frame_t subframeP, nfapi_harq_indication_pdu_t *harq_pdu)
+void
+harq_indication(module_id_t mod_idP, int CC_idP, frame_t frameP,
+		sub_frame_t subframeP,
+		nfapi_harq_indication_pdu_t * harq_pdu)
 {
-  rnti_t rnti              = harq_pdu->rx_ue_information.rnti;
-  uint8_t ul_cqi           = harq_pdu->ul_cqi_information.ul_cqi;
-  uint8_t channel          = harq_pdu->ul_cqi_information.channel;
-  int UE_id                = find_UE_id(mod_idP, rnti);
-  if (UE_id == -1) {
-    LOG_W(MAC, "harq_indication: UE %x not found\n", rnti);
-    return;
-  }
-  UE_list_t *UE_list       = &RC.mac[mod_idP]->UE_list;
-  UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-  COMMON_channels_t *cc    = &RC.mac[mod_idP]->common_channels[CC_idP];
+    rnti_t rnti = harq_pdu->rx_ue_information.rnti;
+    uint8_t ul_cqi = harq_pdu->ul_cqi_information.ul_cqi;
+    uint8_t channel = harq_pdu->ul_cqi_information.channel;
+    int UE_id = find_UE_id(mod_idP, rnti);
+    if (UE_id == -1) {
+	LOG_W(MAC, "harq_indication: UE %x not found\n", rnti);
+	return;
+    }
+    UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
+    UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+    COMMON_channels_t *cc = &RC.mac[mod_idP]->common_channels[CC_idP];
     // extract HARQ Information
-  LOG_D(MAC,"Frame %d, subframe %d: Received harq indication (%d) from UE %d/%x, ul_cqi %d\n",frameP,subframeP,channel,UE_id,rnti,ul_cqi);
-  if (cc->tdd_Config) extract_harq(mod_idP,CC_idP,UE_id,frameP,subframeP,(void*)&harq_pdu->harq_indication_tdd_rel13,channel);
-  else                extract_harq(mod_idP,CC_idP,UE_id,frameP,subframeP,(void*)&harq_pdu->harq_indication_fdd_rel13,channel);
-  if (channel == 0) {
-    sched_ctl->pucch1_snr[CC_idP]       = ul_cqi;
-    sched_ctl->pucch1_cqi_update[CC_idP] = 1;
-  }
+    LOG_D(MAC,
+	  "Frame %d, subframe %d: Received harq indication (%d) from UE %d/%x, ul_cqi %d\n",
+	  frameP, subframeP, channel, UE_id, rnti, ul_cqi);
+    if (cc->tdd_Config)
+	extract_harq(mod_idP, CC_idP, UE_id, frameP, subframeP,
+		     (void *) &harq_pdu->harq_indication_tdd_rel13,
+		     channel);
+    else
+	extract_harq(mod_idP, CC_idP, UE_id, frameP, subframeP,
+		     (void *) &harq_pdu->harq_indication_fdd_rel13,
+		     channel);
+    if (channel == 0) {
+	sched_ctl->pucch1_snr[CC_idP] = ul_cqi;
+	sched_ctl->pucch1_cqi_update[CC_idP] = 1;
+    }
 }
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_ulsch.c b/openair2/LAYER2/MAC/eNB_scheduler_ulsch.c
index 1fcd07c1d3f889d80827c4da6a0afb4ada035dad..343d391538c27573a5cbbac883f519ee7bf34b99 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler_ulsch.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_ulsch.c
@@ -29,6 +29,8 @@
 
  */
 
+/* indented with: indent -kr eNB_scheduler_RA.c */
+
 #include "assertions.h"
 #include "PHY/defs.h"
 #include "PHY/extern.h"
@@ -52,7 +54,7 @@
 #include "pdcp.h"
 
 #if defined(ENABLE_ITTI)
-# include "intertask_interface.h"
+#include "intertask_interface.h"
 #endif
 
 #include "T.h"
@@ -61,645 +63,838 @@
 #define DEBUG_eNB_SCHEDULER 1
 
 // This table holds the allowable PRB sizes for ULSCH transmissions
-uint8_t rb_table[34] = {1,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,36,40,45,48,50,54,60,64,72,75,80,81,90,96,100};
-
-void rx_sdu(const module_id_t enb_mod_idP,
-            const int         CC_idP,
-            const frame_t     frameP,
-            const sub_frame_t subframeP,
-            const rnti_t      rntiP,
-            uint8_t          *sduP,
-            const uint16_t    sdu_lenP,
-            const uint16_t    timing_advance,
-            const uint8_t     ul_cqi)
+uint8_t rb_table[34] =
+    { 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32,
+    36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80, 81, 90, 96, 100
+};
+
+void
+rx_sdu(const module_id_t enb_mod_idP,
+       const int CC_idP,
+       const frame_t frameP,
+       const sub_frame_t subframeP,
+       const rnti_t rntiP,
+       uint8_t * sduP,
+       const uint16_t sdu_lenP,
+       const uint16_t timing_advance, const uint8_t ul_cqi)
 {
-  int current_rnti = rntiP;
-  unsigned char  rx_ces[MAX_NUM_CE],num_ce,num_sdu,i,*payload_ptr;
-  unsigned char  rx_lcids[NB_RB_MAX];
-  unsigned short rx_lengths[NB_RB_MAX];
-  int    UE_id = find_UE_id(enb_mod_idP, current_rnti);
-  int RA_id;
-  int ii,j;
-  eNB_MAC_INST *eNB = RC.mac[enb_mod_idP];
-  int    harq_pid = subframe2harqpid(&eNB->common_channels[CC_idP],frameP,subframeP);
-
-  UE_list_t *UE_list= &eNB->UE_list;
-  int crnti_rx=0;
-  int old_buffer_info;
-  RA_TEMPLATE *RA_template = (RA_TEMPLATE *)&RC.mac[enb_mod_idP]->common_channels[CC_idP].RA_template[0];
-  int first_rb = 0;
-
-  start_meas(&eNB->rx_ulsch_sdu);
-
-  if ((UE_id >  NUMBER_OF_UE_MAX) || (UE_id == -1)  )
-    for(ii=0; ii<NB_RB_MAX; ii++) {
-      rx_lengths[ii] = 0;
-    }
-
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_RX_SDU,1);
-  if (opt_enabled == 1) {
-    trace_pdu(0, sduP,sdu_lenP, 0, 3, current_rnti, frameP, subframeP, 0,0);
-    LOG_D(OPT,"[eNB %d][ULSCH] Frame %d  rnti %x  with size %d\n",
-                      enb_mod_idP, frameP, current_rnti, sdu_lenP);
-  }
-
-  if (UE_id!=-1) {
-    LOG_D(MAC,"[eNB %d][PUSCH %d] CC_id %d Received ULSCH sdu round %d from PHY (rnti %x, UE_id %d) ul_cqi %d\n",enb_mod_idP,harq_pid,CC_idP, UE_list->UE_sched_ctrl[UE_id].round_UL[CC_idP][harq_pid],
-          current_rnti, UE_id,ul_cqi);
-
-    AssertFatal(UE_list->UE_sched_ctrl[UE_id].round_UL[CC_idP][harq_pid] < 8,
-                "round >= 8\n");
-    if (sduP!=NULL) {
-       UE_list->UE_sched_ctrl[UE_id].ul_inactivity_timer   = 0;
-       UE_list->UE_sched_ctrl[UE_id].ul_failure_timer      = 0;
-       UE_list->UE_sched_ctrl[UE_id].ul_scheduled         &= (~(1<<harq_pid));
-       /* Update with smoothing: 3/4 of old value and 1/4 of new.
-        * This is the logic that was done in the function
-        * lte_est_timing_advance_pusch, maybe it's not necessary?
-        * maybe it's even not correct at all?
-        */
-       UE_list->UE_sched_ctrl[UE_id].ta_update             =
-           (UE_list->UE_sched_ctrl[UE_id].ta_update * 3 + timing_advance)/4;
-       UE_list->UE_sched_ctrl[UE_id].pusch_snr[CC_idP]       = ul_cqi;
-       UE_list->UE_sched_ctrl[UE_id].ul_consecutive_errors = 0;
-       first_rb =  UE_list->UE_template[CC_idP][UE_id].first_rb_ul[harq_pid];
-
-       if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync > 0) {
-         UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync=0;
-         mac_eNB_rrc_ul_in_sync(enb_mod_idP,CC_idP,frameP,subframeP,UE_RNTI(enb_mod_idP,UE_id));
-       }
+    int current_rnti = rntiP;
+    unsigned char rx_ces[MAX_NUM_CE], num_ce, num_sdu, i, *payload_ptr;
+    unsigned char rx_lcids[NB_RB_MAX];
+    unsigned short rx_lengths[NB_RB_MAX];
+    int UE_id = find_UE_id(enb_mod_idP, current_rnti);
+    int RA_id;
+    int ii, j;
+    eNB_MAC_INST *mac = RC.mac[enb_mod_idP];
+    int harq_pid =
+	subframe2harqpid(&mac->common_channels[CC_idP], frameP, subframeP);
+
+    UE_list_t *UE_list = &mac->UE_list;
+    int crnti_rx = 0;
+    int old_buffer_info;
+    RA_t *ra =
+	(RA_t *) & RC.mac[enb_mod_idP]->common_channels[CC_idP].ra[0];
+    int first_rb = 0;
+
+    start_meas(&mac->rx_ulsch_sdu);
+
+    if ((UE_id > NUMBER_OF_UE_MAX) || (UE_id == -1))
+	for (ii = 0; ii < NB_RB_MAX; ii++) {
+	    rx_lengths[ii] = 0;
+	}
+
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_RX_SDU, 1);
+    if (opt_enabled == 1) {
+	trace_pdu(0, sduP, sdu_lenP, 0, 3, current_rnti, frameP, subframeP,
+		  0, 0);
+	LOG_D(OPT, "[eNB %d][ULSCH] Frame %d  rnti %x  with size %d\n",
+	      enb_mod_idP, frameP, current_rnti, sdu_lenP);
     }
-    else { // we've got an error
-      LOG_D(MAC,"[eNB %d][PUSCH %d] CC_id %d ULSCH in error in round %d, ul_cqi %d\n",enb_mod_idP,harq_pid,CC_idP,
-                UE_list->UE_sched_ctrl[UE_id].round_UL[CC_idP][harq_pid],ul_cqi);
-
-      //      AssertFatal(1==0,"ulsch in error\n");
-      if (UE_list->UE_sched_ctrl[UE_id].round_UL[CC_idP][harq_pid] == 3) {
-             UE_list->UE_sched_ctrl[UE_id].ul_scheduled       &= (~(1<<harq_pid));
-             UE_list->UE_sched_ctrl[UE_id].round_UL[CC_idP][harq_pid]=0;
-             if (UE_list->UE_sched_ctrl[UE_id].ul_consecutive_errors++ == 10)
-            UE_list->UE_sched_ctrl[UE_id].ul_failure_timer = 1;
-      }
-      else UE_list->UE_sched_ctrl[UE_id].round_UL[CC_idP][harq_pid]++;
-      return;
 
+    if (UE_id != -1) {
+	LOG_D(MAC,
+	      "[eNB %d][PUSCH %d] CC_id %d Received ULSCH sdu round %d from PHY (rnti %x, UE_id %d) ul_cqi %d\n",
+	      enb_mod_idP, harq_pid, CC_idP,
+	      UE_list->UE_sched_ctrl[UE_id].round_UL[CC_idP][harq_pid],
+	      current_rnti, UE_id, ul_cqi);
+
+	AssertFatal(UE_list->UE_sched_ctrl[UE_id].
+		    round_UL[CC_idP][harq_pid] < 8, "round >= 8\n");
+	if (sduP != NULL) {
+	    UE_list->UE_sched_ctrl[UE_id].ul_inactivity_timer = 0;
+	    UE_list->UE_sched_ctrl[UE_id].ul_failure_timer = 0;
+	    UE_list->UE_sched_ctrl[UE_id].ul_scheduled &=
+		(~(1 << harq_pid));
+	    /* Update with smoothing: 3/4 of old value and 1/4 of new.
+	     * This is the logic that was done in the function
+	     * lte_est_timing_advance_pusch, maybe it's not necessary?
+	     * maybe it's even not correct at all?
+	     */
+	    UE_list->UE_sched_ctrl[UE_id].ta_update =
+		(UE_list->UE_sched_ctrl[UE_id].ta_update * 3 +
+		 timing_advance) / 4;
+	    UE_list->UE_sched_ctrl[UE_id].pusch_snr[CC_idP] = ul_cqi;
+	    UE_list->UE_sched_ctrl[UE_id].ul_consecutive_errors = 0;
+	    first_rb =
+		UE_list->UE_template[CC_idP][UE_id].first_rb_ul[harq_pid];
+
+	    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync > 0) {
+		UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync = 0;
+		mac_eNB_rrc_ul_in_sync(enb_mod_idP, CC_idP, frameP,
+				       subframeP, UE_RNTI(enb_mod_idP,
+							  UE_id));
+	    }
+	} else {		// we've got an error
+	    LOG_D(MAC,
+		  "[eNB %d][PUSCH %d] CC_id %d ULSCH in error in round %d, ul_cqi %d\n",
+		  enb_mod_idP, harq_pid, CC_idP,
+		  UE_list->UE_sched_ctrl[UE_id].round_UL[CC_idP][harq_pid],
+		  ul_cqi);
+
+	    //      AssertFatal(1==0,"ulsch in error\n");
+	    if (UE_list->UE_sched_ctrl[UE_id].round_UL[CC_idP][harq_pid] ==
+		3) {
+		UE_list->UE_sched_ctrl[UE_id].ul_scheduled &=
+		    (~(1 << harq_pid));
+		UE_list->UE_sched_ctrl[UE_id].round_UL[CC_idP][harq_pid] =
+		    0;
+		if (UE_list->UE_sched_ctrl[UE_id].
+		    ul_consecutive_errors++ == 10)
+		    UE_list->UE_sched_ctrl[UE_id].ul_failure_timer = 1;
+	    } else
+		UE_list->UE_sched_ctrl[UE_id].round_UL[CC_idP][harq_pid]++;
+	    return;
+
+	}
+    } else if ((RA_id = find_RA_id(enb_mod_idP, CC_idP, current_rnti)) != -1) {	// Check if this is an RA process for the rnti
+	AssertFatal(mac->common_channels[CC_idP].
+		    radioResourceConfigCommon->rach_ConfigCommon.
+		    maxHARQ_Msg3Tx > 1,
+		    "maxHARQ %d should be greater than 1\n",
+		    (int) mac->common_channels[CC_idP].
+		    radioResourceConfigCommon->rach_ConfigCommon.
+		    maxHARQ_Msg3Tx);
+
+	LOG_D(MAC,
+	      "[eNB %d][PUSCH %d] CC_id %d Received ULSCH sdu round %d from PHY (rnti %x, RA_id %d) ul_cqi %d\n",
+	      enb_mod_idP, harq_pid, CC_idP, ra[RA_id].msg3_round,
+	      current_rnti, RA_id, ul_cqi);
+
+	first_rb = ra->msg3_first_rb;
+
+	if (sduP == NULL) {	// we've got an error on Msg3
+	    LOG_D(MAC,
+		  "[eNB %d] CC_id %d, RA %d ULSCH in error in round %d/%d\n",
+		  enb_mod_idP, CC_idP, RA_id,
+		  ra[RA_id].msg3_round,
+		  (int) mac->common_channels[CC_idP].
+		  radioResourceConfigCommon->rach_ConfigCommon.
+		  maxHARQ_Msg3Tx);
+	    if (ra[RA_id].msg3_round ==
+		mac->common_channels[CC_idP].radioResourceConfigCommon->
+		rach_ConfigCommon.maxHARQ_Msg3Tx - 1) {
+		cancel_ra_proc(enb_mod_idP, CC_idP, frameP, current_rnti);
+	    }
+
+	    else {
+		first_rb =
+		    UE_list->UE_template[CC_idP][UE_id].
+		    first_rb_ul[harq_pid];
+		ra[RA_id].msg3_round++;
+		// prepare handling of retransmission
+		ra[RA_id].Msg3_frame +=
+		    ((ra[RA_id].Msg3_subframe > 1) ? 1 : 0);
+		ra[RA_id].Msg3_subframe =
+		    (ra[RA_id].Msg3_subframe + 8) % 10;
+		add_msg3(enb_mod_idP, CC_idP, &ra[RA_id], frameP,
+			 subframeP);
+	    }
+	    return;
+	}
+    } else {
+	LOG_W(MAC,
+	      "Cannot find UE or RA corresponding to ULSCH rnti %x, dropping it\n",
+	      current_rnti);
+	return;
     }
-  }
-  else if ((RA_id = find_RA_id(enb_mod_idP,CC_idP,current_rnti))!=-1) { // Check if this is an RA process for the rnti
-    AssertFatal(eNB->common_channels[CC_idP].radioResourceConfigCommon->rach_ConfigCommon.maxHARQ_Msg3Tx>1,
-                "maxHARQ %d should be greater than 1\n",
-                (int)eNB->common_channels[CC_idP].radioResourceConfigCommon->rach_ConfigCommon.maxHARQ_Msg3Tx);
-
-    LOG_D(MAC,"[eNB %d][PUSCH %d] CC_id %d Received ULSCH sdu round %d from PHY (rnti %x, RA_id %d) ul_cqi %d\n",enb_mod_idP,harq_pid,CC_idP,
-     RA_template[RA_id].msg3_round,
-     current_rnti,RA_id,ul_cqi);
-
-    first_rb                   = RA_template->msg3_first_rb;
-
-    if (sduP==NULL) { // we've got an error on Msg3
-      LOG_D(MAC,"[eNB %d] CC_id %d, RA %d ULSCH in error in round %d/%d\n",enb_mod_idP,CC_idP,RA_id,
-            RA_template[RA_id].msg3_round,
-            (int)eNB->common_channels[CC_idP].radioResourceConfigCommon->rach_ConfigCommon.maxHARQ_Msg3Tx);
-      if (RA_template[RA_id].msg3_round == eNB->common_channels[CC_idP].radioResourceConfigCommon->rach_ConfigCommon.maxHARQ_Msg3Tx-1) {
-        cancel_ra_proc(enb_mod_idP,CC_idP,frameP,current_rnti);
-      }
-
-      else {
-        first_rb =  UE_list->UE_template[CC_idP][UE_id].first_rb_ul[harq_pid];
-        RA_template[RA_id].msg3_round++;
-        // prepare handling of retransmission
-        RA_template[RA_id].Msg3_frame += ((RA_template[RA_id].Msg3_subframe>1) ? 1 : 0);
-        RA_template[RA_id].Msg3_subframe = (RA_template[RA_id].Msg3_subframe+8)%10;
-        add_msg3(enb_mod_idP,CC_idP, &RA_template[RA_id],frameP,subframeP);
-      }
-      return;
-    }
-  }
-  else  {
-    LOG_W(MAC,"Cannot find UE or RA corresponding to ULSCH rnti %x, dropping it\n", current_rnti);
-    return;
-  }
-  payload_ptr = parse_ulsch_header(sduP,&num_ce,&num_sdu,rx_ces,rx_lcids,rx_lengths,sdu_lenP);
-
-  T(T_ENB_MAC_UE_UL_PDU, T_INT(enb_mod_idP), T_INT(CC_idP), T_INT(current_rnti), T_INT(frameP), T_INT(subframeP),
-    T_INT(harq_pid), T_INT(sdu_lenP), T_INT(num_ce), T_INT(num_sdu));
-  T(T_ENB_MAC_UE_UL_PDU_WITH_DATA, T_INT(enb_mod_idP), T_INT(CC_idP), T_INT(current_rnti), T_INT(frameP), T_INT(subframeP),
-    T_INT(harq_pid), T_INT(sdu_lenP), T_INT(num_ce), T_INT(num_sdu), T_BUFFER(sduP, sdu_lenP));
-
-  eNB->eNB_stats[CC_idP].ulsch_bytes_rx=sdu_lenP;
-  eNB->eNB_stats[CC_idP].total_ulsch_bytes_rx+=sdu_lenP;
-  eNB->eNB_stats[CC_idP].total_ulsch_pdus_rx+=1;
-
-  UE_list->UE_sched_ctrl[UE_id].round_UL[CC_idP][harq_pid] = 0;
-
-  // control element
-  for (i=0; i<num_ce; i++) {
-
-    T(T_ENB_MAC_UE_UL_CE, T_INT(enb_mod_idP), T_INT(CC_idP), T_INT(current_rnti), T_INT(frameP), T_INT(subframeP),
-      T_INT(rx_ces[i]));
-
-    switch (rx_ces[i]) { // implement and process BSR + CRNTI +
-    case POWER_HEADROOM:
-      if (UE_id != -1) {
-        UE_list->UE_template[CC_idP][UE_id].phr_info =  (payload_ptr[0] & 0x3f) - PHR_MAPPING_OFFSET;
-        LOG_D(MAC, "[eNB %d] CC_id %d MAC CE_LCID %d : Received PHR PH = %d (db)\n",
-              enb_mod_idP, CC_idP, rx_ces[i], UE_list->UE_template[CC_idP][UE_id].phr_info);
-        UE_list->UE_template[CC_idP][UE_id].phr_info_configured=1;
-        UE_list->UE_sched_ctrl[UE_id].phr_received = 1;
-      }
-      payload_ptr+=sizeof(POWER_HEADROOM_CMD);
-      break;
-
-    case CRNTI: {
-      int old_rnti = (((uint16_t)payload_ptr[0])<<8) + payload_ptr[1];
-      int old_UE_id = find_UE_id(enb_mod_idP, old_rnti);
-      LOG_D(MAC, "[eNB %d] Frame %d, Subframe %d CC_id %d MAC CE_LCID %d (ce %d/%d): CRNTI %x (UE_id %d) in Msg3\n",
-            enb_mod_idP, frameP, subframeP, CC_idP, rx_ces[i], i, num_ce, old_rnti, old_UE_id);
-      if (old_UE_id != -1) {
-        /* TODO: if the UE did random access (followed by a MAC uplink with
-         * CRNTI) because none of its scheduling request was granted, then
-         * according to 36.321 5.4.4 the UE's MAC will notify RRC to release
-         * PUCCH/SRS. According to 36.331 5.3.13 the UE will then apply
-         * default configuration for CQI reporting and scheduling requests,
-         * which basically means that the CQI requests won't work anymore and
-         * that the UE won't do any scheduling request anymore as long as the
-         * eNB doesn't reconfigure the UE.
-         * We have to take care of this. As the code is, nothing is done and
-         * the UE state in the eNB is wrong.
-         */
-        UE_id = old_UE_id;
-        UE_list->UE_sched_ctrl[UE_id].ul_inactivity_timer = 0;
-        UE_list->UE_sched_ctrl[UE_id].ul_failure_timer    = 0;
-        if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync > 0) {
-          UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync = 0;
-          mac_eNB_rrc_ul_in_sync(enb_mod_idP, CC_idP, frameP, subframeP, old_rnti);
-        }
-        /* receiving CRNTI means that the current rnti has to go away */
-        cancel_ra_proc(enb_mod_idP,CC_idP,frameP,current_rnti);
-        current_rnti = old_rnti;
-      }
-      crnti_rx=1;
-      payload_ptr+=2;
-      break;
+    payload_ptr =
+	parse_ulsch_header(sduP, &num_ce, &num_sdu, rx_ces, rx_lcids,
+			   rx_lengths, sdu_lenP);
+
+    T(T_ENB_MAC_UE_UL_PDU, T_INT(enb_mod_idP), T_INT(CC_idP),
+      T_INT(current_rnti), T_INT(frameP), T_INT(subframeP),
+      T_INT(harq_pid), T_INT(sdu_lenP), T_INT(num_ce), T_INT(num_sdu));
+    T(T_ENB_MAC_UE_UL_PDU_WITH_DATA, T_INT(enb_mod_idP), T_INT(CC_idP),
+      T_INT(current_rnti), T_INT(frameP), T_INT(subframeP),
+      T_INT(harq_pid), T_INT(sdu_lenP), T_INT(num_ce), T_INT(num_sdu),
+      T_BUFFER(sduP, sdu_lenP));
+
+    mac->eNB_stats[CC_idP].ulsch_bytes_rx = sdu_lenP;
+    mac->eNB_stats[CC_idP].total_ulsch_bytes_rx += sdu_lenP;
+    mac->eNB_stats[CC_idP].total_ulsch_pdus_rx += 1;
+
+    UE_list->UE_sched_ctrl[UE_id].round_UL[CC_idP][harq_pid] = 0;
+
+    // control element
+    for (i = 0; i < num_ce; i++) {
+
+	T(T_ENB_MAC_UE_UL_CE, T_INT(enb_mod_idP), T_INT(CC_idP),
+	  T_INT(current_rnti), T_INT(frameP), T_INT(subframeP),
+	  T_INT(rx_ces[i]));
+
+	switch (rx_ces[i]) {	// implement and process BSR + CRNTI +
+	case POWER_HEADROOM:
+	    if (UE_id != -1) {
+		UE_list->UE_template[CC_idP][UE_id].phr_info =
+		    (payload_ptr[0] & 0x3f) - PHR_MAPPING_OFFSET;
+		LOG_D(MAC,
+		      "[eNB %d] CC_id %d MAC CE_LCID %d : Received PHR PH = %d (db)\n",
+		      enb_mod_idP, CC_idP, rx_ces[i],
+		      UE_list->UE_template[CC_idP][UE_id].phr_info);
+		UE_list->UE_template[CC_idP][UE_id].phr_info_configured =
+		    1;
+		UE_list->UE_sched_ctrl[UE_id].phr_received = 1;
+	    }
+	    payload_ptr += sizeof(POWER_HEADROOM_CMD);
+	    break;
+
+	case CRNTI:
+	    {
+		int old_rnti =
+		    (((uint16_t) payload_ptr[0]) << 8) + payload_ptr[1];
+		int old_UE_id = find_UE_id(enb_mod_idP, old_rnti);
+		LOG_D(MAC,
+		      "[eNB %d] Frame %d, Subframe %d CC_id %d MAC CE_LCID %d (ce %d/%d): CRNTI %x (UE_id %d) in Msg3\n",
+		      enb_mod_idP, frameP, subframeP, CC_idP, rx_ces[i], i,
+		      num_ce, old_rnti, old_UE_id);
+		if (old_UE_id != -1) {
+		    /* TODO: if the UE did random access (followed by a MAC uplink with
+		     * CRNTI) because none of its scheduling request was granted, then
+		     * according to 36.321 5.4.4 the UE's MAC will notify RRC to release
+		     * PUCCH/SRS. According to 36.331 5.3.13 the UE will then apply
+		     * default configuration for CQI reporting and scheduling requests,
+		     * which basically means that the CQI requests won't work anymore and
+		     * that the UE won't do any scheduling request anymore as long as the
+		     * eNB doesn't reconfigure the UE.
+		     * We have to take care of this. As the code is, nothing is done and
+		     * the UE state in the eNB is wrong.
+		     */
+		    UE_id = old_UE_id;
+		    UE_list->UE_sched_ctrl[UE_id].ul_inactivity_timer = 0;
+		    UE_list->UE_sched_ctrl[UE_id].ul_failure_timer = 0;
+		    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync > 0) {
+			UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync = 0;
+			mac_eNB_rrc_ul_in_sync(enb_mod_idP, CC_idP, frameP,
+					       subframeP, old_rnti);
+		    }
+		    /* receiving CRNTI means that the current rnti has to go away */
+		    cancel_ra_proc(enb_mod_idP, CC_idP, frameP,
+				   current_rnti);
+		    current_rnti = old_rnti;
+		}
+		crnti_rx = 1;
+		payload_ptr += 2;
+		break;
+	    }
+
+	case TRUNCATED_BSR:
+	case SHORT_BSR:
+	    {
+		uint8_t lcgid;
+		lcgid = (payload_ptr[0] >> 6);
+
+		LOG_D(MAC,
+		      "[eNB %d] CC_id %d MAC CE_LCID %d : Received short BSR LCGID = %u bsr = %d\n",
+		      enb_mod_idP, CC_idP, rx_ces[i], lcgid,
+		      payload_ptr[0] & 0x3f);
+
+		if (crnti_rx == 1)
+		    LOG_D(MAC,
+			  "[eNB %d] CC_id %d MAC CE_LCID %d : Received short BSR LCGID = %u bsr = %d\n",
+			  enb_mod_idP, CC_idP, rx_ces[i], lcgid,
+			  payload_ptr[0] & 0x3f);
+		if (UE_id != -1) {
+
+		    UE_list->UE_template[CC_idP][UE_id].bsr_info[lcgid] =
+			(payload_ptr[0] & 0x3f);
+
+		    // update buffer info
+
+		    UE_list->UE_template[CC_idP][UE_id].
+			ul_buffer_info[lcgid] =
+			BSR_TABLE[UE_list->UE_template[CC_idP][UE_id].
+				  bsr_info[lcgid]];
+
+		    UE_list->UE_template[CC_idP][UE_id].ul_total_buffer =
+			UE_list->UE_template[CC_idP][UE_id].
+			ul_buffer_info[lcgid];
+
+		    RC.eNB[enb_mod_idP][CC_idP]->
+			pusch_stats_bsr[UE_id][(frameP * 10) + subframeP]
+			= (payload_ptr[0] & 0x3f);
+		    if (UE_id == UE_list->head)
+			VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME
+			    (VCD_SIGNAL_DUMPER_VARIABLES_UE0_BSR,
+			     RC.eNB[enb_mod_idP][CC_idP]->pusch_stats_bsr
+			     [UE_id][(frameP * 10) + subframeP]);
+		    if (UE_list->UE_template[CC_idP][UE_id].
+			ul_buffer_creation_time[lcgid]
+			== 0) {
+			UE_list->UE_template[CC_idP]
+			    [UE_id].ul_buffer_creation_time[lcgid] =
+			    frameP;
+		    }
+		    if (mac_eNB_get_rrc_status
+			(enb_mod_idP,
+			 UE_RNTI(enb_mod_idP, UE_id)) < RRC_CONNECTED)
+			LOG_D(MAC,
+			      "[eNB %d] CC_id %d MAC CE_LCID %d : ul_total_buffer = %d (lcg increment %d)\n",
+			      enb_mod_idP, CC_idP, rx_ces[i],
+			      UE_list->UE_template[CC_idP][UE_id].
+			      ul_total_buffer,
+			      UE_list->UE_template[CC_idP][UE_id].
+			      ul_buffer_info[lcgid]);
+		} else {
+
+		}
+		payload_ptr += 1;	//sizeof(SHORT_BSR); // fixme
+	    }
+	    break;
+
+	case LONG_BSR:
+	    if (UE_id != -1) {
+		UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID0] =
+		    ((payload_ptr[0] & 0xFC) >> 2);
+		UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID1] =
+		    ((payload_ptr[0] & 0x03) << 4) |
+		    ((payload_ptr[1] & 0xF0) >> 4);
+		UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID2] =
+		    ((payload_ptr[1] & 0x0F) << 2) |
+		    ((payload_ptr[2] & 0xC0) >> 6);
+		UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID3] =
+		    (payload_ptr[2] & 0x3F);
+
+		// update buffer info
+		old_buffer_info =
+		    UE_list->UE_template[CC_idP][UE_id].
+		    ul_buffer_info[LCGID0];
+		UE_list->UE_template[CC_idP][UE_id].
+		    ul_buffer_info[LCGID0] =
+		    BSR_TABLE[UE_list->UE_template[CC_idP][UE_id].
+			      bsr_info[LCGID0]];
+
+		UE_list->UE_template[CC_idP][UE_id].ul_total_buffer +=
+		    UE_list->UE_template[CC_idP][UE_id].
+		    ul_buffer_info[LCGID0];
+		if (UE_list->UE_template[CC_idP][UE_id].ul_total_buffer >=
+		    old_buffer_info)
+		    UE_list->UE_template[CC_idP][UE_id].ul_total_buffer -=
+			old_buffer_info;
+		else
+		    UE_list->UE_template[CC_idP][UE_id].ul_total_buffer =
+			0;
+
+		old_buffer_info =
+		    UE_list->UE_template[CC_idP][UE_id].
+		    ul_buffer_info[LCGID1];
+		UE_list->UE_template[CC_idP][UE_id].
+		    ul_buffer_info[LCGID1] =
+		    BSR_TABLE[UE_list->UE_template[CC_idP][UE_id].
+			      bsr_info[LCGID1]];
+		UE_list->UE_template[CC_idP][UE_id].ul_total_buffer +=
+		    UE_list->UE_template[CC_idP][UE_id].
+		    ul_buffer_info[LCGID1];
+		if (UE_list->UE_template[CC_idP][UE_id].ul_total_buffer >=
+		    old_buffer_info)
+		    UE_list->UE_template[CC_idP][UE_id].ul_total_buffer -=
+			old_buffer_info;
+		else
+		    UE_list->UE_template[CC_idP][UE_id].ul_total_buffer =
+			0;
+
+		old_buffer_info =
+		    UE_list->UE_template[CC_idP][UE_id].
+		    ul_buffer_info[LCGID2];
+		UE_list->UE_template[CC_idP][UE_id].
+		    ul_buffer_info[LCGID2] =
+		    BSR_TABLE[UE_list->UE_template[CC_idP][UE_id].
+			      bsr_info[LCGID2]];
+		UE_list->UE_template[CC_idP][UE_id].ul_total_buffer +=
+		    UE_list->UE_template[CC_idP][UE_id].
+		    ul_buffer_info[LCGID2];
+		if (UE_list->UE_template[CC_idP][UE_id].ul_total_buffer >=
+		    old_buffer_info)
+		    UE_list->UE_template[CC_idP][UE_id].ul_total_buffer -=
+			old_buffer_info;
+		else
+		    UE_list->UE_template[CC_idP][UE_id].ul_total_buffer =
+			0;
+
+		old_buffer_info =
+		    UE_list->UE_template[CC_idP][UE_id].
+		    ul_buffer_info[LCGID3];
+		UE_list->UE_template[CC_idP][UE_id].
+		    ul_buffer_info[LCGID3] =
+		    BSR_TABLE[UE_list->UE_template[CC_idP][UE_id].
+			      bsr_info[LCGID3]];
+		UE_list->UE_template[CC_idP][UE_id].ul_total_buffer +=
+		    UE_list->UE_template[CC_idP][UE_id].
+		    ul_buffer_info[LCGID3];
+		if (UE_list->UE_template[CC_idP][UE_id].ul_total_buffer >=
+		    old_buffer_info)
+		    UE_list->UE_template[CC_idP][UE_id].ul_total_buffer -=
+			old_buffer_info;
+		else
+		    UE_list->UE_template[CC_idP][UE_id].ul_total_buffer =
+			0;
+
+		LOG_D(MAC,
+		      "[eNB %d] CC_id %d MAC CE_LCID %d: Received long BSR LCGID0 = %u LCGID1 = "
+		      "%u LCGID2 = %u LCGID3 = %u\n", enb_mod_idP, CC_idP,
+		      rx_ces[i],
+		      UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID0],
+		      UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID1],
+		      UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID2],
+		      UE_list->UE_template[CC_idP][UE_id].
+		      bsr_info[LCGID3]);
+		if (crnti_rx == 1)
+		    LOG_D(MAC,
+			  "[eNB %d] CC_id %d MAC CE_LCID %d: Received long BSR LCGID0 = %u LCGID1 = "
+			  "%u LCGID2 = %u LCGID3 = %u\n", enb_mod_idP,
+			  CC_idP, rx_ces[i],
+			  UE_list->UE_template[CC_idP][UE_id].
+			  bsr_info[LCGID0],
+			  UE_list->UE_template[CC_idP][UE_id].
+			  bsr_info[LCGID1],
+			  UE_list->UE_template[CC_idP][UE_id].
+			  bsr_info[LCGID2],
+			  UE_list->UE_template[CC_idP][UE_id].
+			  bsr_info[LCGID3]);
+
+		if (UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID0] ==
+		    0) {
+		    UE_list->UE_template[CC_idP][UE_id].
+			ul_buffer_creation_time[LCGID0] = 0;
+		} else if (UE_list->UE_template[CC_idP]
+			   [UE_id].ul_buffer_creation_time[LCGID0] == 0) {
+		    UE_list->UE_template[CC_idP][UE_id].
+			ul_buffer_creation_time[LCGID0] = frameP;
+		}
+
+		if (UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID1] ==
+		    0) {
+		    UE_list->UE_template[CC_idP][UE_id].
+			ul_buffer_creation_time[LCGID1] = 0;
+		} else if (UE_list->UE_template[CC_idP]
+			   [UE_id].ul_buffer_creation_time[LCGID1] == 0) {
+		    UE_list->UE_template[CC_idP][UE_id].
+			ul_buffer_creation_time[LCGID1] = frameP;
+		}
+
+		if (UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID2] ==
+		    0) {
+		    UE_list->UE_template[CC_idP][UE_id].
+			ul_buffer_creation_time[LCGID2] = 0;
+		} else if (UE_list->UE_template[CC_idP]
+			   [UE_id].ul_buffer_creation_time[LCGID2] == 0) {
+		    UE_list->UE_template[CC_idP][UE_id].
+			ul_buffer_creation_time[LCGID2] = frameP;
+		}
+
+		if (UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID3] ==
+		    0) {
+		    UE_list->UE_template[CC_idP][UE_id].
+			ul_buffer_creation_time[LCGID3] = 0;
+		} else if (UE_list->UE_template[CC_idP]
+			   [UE_id].ul_buffer_creation_time[LCGID3] == 0) {
+		    UE_list->UE_template[CC_idP][UE_id].
+			ul_buffer_creation_time[LCGID3] = frameP;
+
+		}
+	    }
+
+	    payload_ptr += 3;	////sizeof(LONG_BSR);
+	    break;
+
+	default:
+	    LOG_E(MAC,
+		  "[eNB %d] CC_id %d Received unknown MAC header (0x%02x)\n",
+		  enb_mod_idP, CC_idP, rx_ces[i]);
+	    break;
+	}
     }
 
-    case TRUNCATED_BSR:
-    case SHORT_BSR: {
-      uint8_t lcgid;
-      lcgid = (payload_ptr[0] >> 6);
-
-      LOG_D(MAC, "[eNB %d] CC_id %d MAC CE_LCID %d : Received short BSR LCGID = %u bsr = %d\n",
-            enb_mod_idP, CC_idP, rx_ces[i], lcgid, payload_ptr[0] & 0x3f);
-
-      if (crnti_rx==1)
-        LOG_D(MAC, "[eNB %d] CC_id %d MAC CE_LCID %d : Received short BSR LCGID = %u bsr = %d\n",
-              enb_mod_idP, CC_idP, rx_ces[i], lcgid, payload_ptr[0] & 0x3f);
-      if (UE_id  != -1) {
-
-        UE_list->UE_template[CC_idP][UE_id].bsr_info[lcgid] = (payload_ptr[0] & 0x3f);
-
-        // update buffer info
-
-        UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[lcgid]=BSR_TABLE[UE_list->UE_template[CC_idP][UE_id].bsr_info[lcgid]];
-
-        UE_list->UE_template[CC_idP][UE_id].ul_total_buffer= UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[lcgid];
-
-        RC.eNB[enb_mod_idP][CC_idP]->pusch_stats_bsr[UE_id][(frameP*10)+subframeP] = (payload_ptr[0] & 0x3f);
-        if (UE_id == UE_list->head)
-          VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_BSR,RC.eNB[enb_mod_idP][CC_idP]->pusch_stats_bsr[UE_id][(frameP*10)+subframeP]);
-        if (UE_list->UE_template[CC_idP][UE_id].ul_buffer_creation_time[lcgid] == 0 ) {
-          UE_list->UE_template[CC_idP][UE_id].ul_buffer_creation_time[lcgid]=frameP;
-        }
-        if (mac_eNB_get_rrc_status(enb_mod_idP,UE_RNTI(enb_mod_idP,UE_id)) < RRC_CONNECTED)
-          LOG_D(MAC, "[eNB %d] CC_id %d MAC CE_LCID %d : ul_total_buffer = %d (lcg increment %d)\n",
-                enb_mod_idP, CC_idP, rx_ces[i], UE_list->UE_template[CC_idP][UE_id].ul_total_buffer,
-                UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[lcgid]);
-      }
-      else {
-
-      }
-      payload_ptr += 1;//sizeof(SHORT_BSR); // fixme
-    }
-    break;
-
-    case LONG_BSR:
-      if (UE_id  != -1) {
-        UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID0] = ((payload_ptr[0] & 0xFC) >> 2);
-        UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID1] =
-          ((payload_ptr[0] & 0x03) << 4) | ((payload_ptr[1] & 0xF0) >> 4);
-        UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID2] =
-          ((payload_ptr[1] & 0x0F) << 2) | ((payload_ptr[2] & 0xC0) >> 6);
-        UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID3] = (payload_ptr[2] & 0x3F);
-
-        // update buffer info
-        old_buffer_info = UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[LCGID0];
-        UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[LCGID0]=BSR_TABLE[UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID0]];
-
-        UE_list->UE_template[CC_idP][UE_id].ul_total_buffer+= UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[LCGID0];
-        if (UE_list->UE_template[CC_idP][UE_id].ul_total_buffer >= old_buffer_info)
-          UE_list->UE_template[CC_idP][UE_id].ul_total_buffer -= old_buffer_info;
-        else
-          UE_list->UE_template[CC_idP][UE_id].ul_total_buffer = 0;
-
-        old_buffer_info = UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[LCGID1];
-        UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[LCGID1]=BSR_TABLE[UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID1]];
-        UE_list->UE_template[CC_idP][UE_id].ul_total_buffer+= UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[LCGID1];
-        if (UE_list->UE_template[CC_idP][UE_id].ul_total_buffer >= old_buffer_info)
-          UE_list->UE_template[CC_idP][UE_id].ul_total_buffer -= old_buffer_info;
-        else
-          UE_list->UE_template[CC_idP][UE_id].ul_total_buffer = 0;
-
-        old_buffer_info = UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[LCGID2];
-        UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[LCGID2]=BSR_TABLE[UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID2]];
-        UE_list->UE_template[CC_idP][UE_id].ul_total_buffer+= UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[LCGID2];
-        if (UE_list->UE_template[CC_idP][UE_id].ul_total_buffer >= old_buffer_info)
-          UE_list->UE_template[CC_idP][UE_id].ul_total_buffer -= old_buffer_info;
-        else
-          UE_list->UE_template[CC_idP][UE_id].ul_total_buffer = 0;
-
-        old_buffer_info = UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[LCGID3];
-        UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[LCGID3]=BSR_TABLE[UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID3]];
-        UE_list->UE_template[CC_idP][UE_id].ul_total_buffer+= UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[LCGID3];
-        if (UE_list->UE_template[CC_idP][UE_id].ul_total_buffer >= old_buffer_info)
-          UE_list->UE_template[CC_idP][UE_id].ul_total_buffer -= old_buffer_info;
-        else
-          UE_list->UE_template[CC_idP][UE_id].ul_total_buffer = 0;
-
-        LOG_D(MAC, "[eNB %d] CC_id %d MAC CE_LCID %d: Received long BSR LCGID0 = %u LCGID1 = "
-              "%u LCGID2 = %u LCGID3 = %u\n",
-              enb_mod_idP, CC_idP,
-              rx_ces[i],
-              UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID0],
-              UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID1],
-              UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID2],
-              UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID3]);
-      if (crnti_rx==1)
-        LOG_D(MAC, "[eNB %d] CC_id %d MAC CE_LCID %d: Received long BSR LCGID0 = %u LCGID1 = "
-              "%u LCGID2 = %u LCGID3 = %u\n",
-              enb_mod_idP, CC_idP,
-              rx_ces[i],
-              UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID0],
-              UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID1],
-              UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID2],
-              UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID3]);
-
-        if (UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID0] == 0 ) {
-          UE_list->UE_template[CC_idP][UE_id].ul_buffer_creation_time[LCGID0]=0;
-        } else if (UE_list->UE_template[CC_idP][UE_id].ul_buffer_creation_time[LCGID0] == 0) {
-          UE_list->UE_template[CC_idP][UE_id].ul_buffer_creation_time[LCGID0]=frameP;
-        }
-
-        if (UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID1] == 0 ) {
-          UE_list->UE_template[CC_idP][UE_id].ul_buffer_creation_time[LCGID1]=0;
-        } else if (UE_list->UE_template[CC_idP][UE_id].ul_buffer_creation_time[LCGID1] == 0) {
-          UE_list->UE_template[CC_idP][UE_id].ul_buffer_creation_time[LCGID1]=frameP;
-        }
-
-        if (UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID2] == 0 ) {
-          UE_list->UE_template[CC_idP][UE_id].ul_buffer_creation_time[LCGID2]=0;
-        } else if (UE_list->UE_template[CC_idP][UE_id].ul_buffer_creation_time[LCGID2] == 0) {
-          UE_list->UE_template[CC_idP][UE_id].ul_buffer_creation_time[LCGID2]=frameP;
-        }
-
-        if (UE_list->UE_template[CC_idP][UE_id].bsr_info[LCGID3] == 0 ) {
-          UE_list->UE_template[CC_idP][UE_id].ul_buffer_creation_time[LCGID3]= 0;
-        } else if (UE_list->UE_template[CC_idP][UE_id].ul_buffer_creation_time[LCGID3] == 0) {
-          UE_list->UE_template[CC_idP][UE_id].ul_buffer_creation_time[LCGID3]=frameP;
-
-        }
-      }
-
-      payload_ptr += 3;////sizeof(LONG_BSR);
-      break;
-
-    default:
-      LOG_E(MAC, "[eNB %d] CC_id %d Received unknown MAC header (0x%02x)\n", enb_mod_idP, CC_idP, rx_ces[i]);
-      break;
-    }
-  }
-
-  for (i=0; i<num_sdu; i++) {
-    LOG_D(MAC,"SDU Number %d MAC Subheader SDU_LCID %d, length %d\n",i,rx_lcids[i],rx_lengths[i]);
-
-    T(T_ENB_MAC_UE_UL_SDU, T_INT(enb_mod_idP), T_INT(CC_idP), T_INT(current_rnti), T_INT(frameP), T_INT(subframeP),
-      T_INT(rx_lcids[i]), T_INT(rx_lengths[i]));
-    T(T_ENB_MAC_UE_UL_SDU_WITH_DATA, T_INT(enb_mod_idP), T_INT(CC_idP), T_INT(current_rnti), T_INT(frameP), T_INT(subframeP),
-      T_INT(rx_lcids[i]), T_INT(rx_lengths[i]), T_BUFFER(payload_ptr, rx_lengths[i]));
-
-    switch (rx_lcids[i]) {
-    case CCCH :
-      if (rx_lengths[i] > CCCH_PAYLOAD_SIZE_MAX) {
-        LOG_E(MAC, "[eNB %d/%d] frame %d received CCCH of size %d (too big, maximum allowed is %d, sdu_len %d), dropping packet\n",
-              enb_mod_idP, CC_idP, frameP, rx_lengths[i], CCCH_PAYLOAD_SIZE_MAX,sdu_lenP);
-        break;
-      }
-      LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d, Received CCCH:  %x.%x.%x.%x.%x.%x, Terminating RA procedure for UE rnti %x\n",
-            enb_mod_idP,CC_idP,frameP,
-            payload_ptr[0],payload_ptr[1],payload_ptr[2],payload_ptr[3],payload_ptr[4], payload_ptr[5], current_rnti);
-      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_TERMINATE_RA_PROC,1);
-      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_TERMINATE_RA_PROC,0);
-      for (ii=0; ii<NB_RA_PROC_MAX; ii++) {
-        RA_TEMPLATE *RA_template = &eNB->common_channels[CC_idP].RA_template[ii];
-
-        LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Checking proc %d : rnti (%x, %x), active %d\n",
-              enb_mod_idP, CC_idP, ii,
-              RA_template->rnti, current_rnti,
-              RA_template->RA_active);
-
-        if ((RA_template->rnti==current_rnti) &&
-            (RA_template->RA_active==TRUE)) {
-
-          //payload_ptr = parse_ulsch_header(msg3,&num_ce,&num_sdu,rx_ces,rx_lcids,rx_lengths,msg3_len);
-
-          if (UE_id < 0) {
-            memcpy(&RA_template->cont_res_id[0],payload_ptr,6);
-            LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d CCCH: Received Msg3: length %d, offset %ld\n",
-                  enb_mod_idP,CC_idP,frameP,rx_lengths[i],payload_ptr-sduP);
-
-            if ((UE_id=add_new_ue(enb_mod_idP,CC_idP,eNB->common_channels[CC_idP].RA_template[ii].rnti,harq_pid
-                      #ifdef Rel14
-                        ,eNB->common_channels[CC_idP].RA_template[ii].rach_resource_type
-                      #endif
-                      )) == -1 ) {
-              AssertFatal(1==0,"[MAC][eNB] Max user count reached\n");
-              // kill RA procedure
-            } else
-              LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d Added user with rnti %x => UE %d\n",
-                    enb_mod_idP,CC_idP,frameP,RA_template->rnti,UE_id);
-          } else {
-            LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d CCCH: Received Msg3 from already registered UE %d: length %d, offset %ld\n",
-                  enb_mod_idP,CC_idP,frameP,UE_id,rx_lengths[i],payload_ptr-sduP);
-            // kill RA procedure
-          }
-
-          mac_rrc_data_ind(
-                           enb_mod_idP,
-                           CC_idP,
-                           frameP,subframeP,
-                           current_rnti,
-                           CCCH,
-                           (uint8_t*)payload_ptr,
-                           rx_lengths[i],
-                           ENB_FLAG_YES,
-                           enb_mod_idP,
-                           0
-                           );
-
-
-          if (num_ce >0) {  // handle msg3 which is not RRCConnectionRequest
-            //  process_ra_message(msg3,num_ce,rx_lcids,rx_ces);
-          }
-
-          // prepare transmission of Msg4
-          RA_template->generate_Msg4 = 1;
-          RA_template->wait_ack_Msg4 = 0;
-
-
-
-          // Program Msg4 PDCCH+DLSCH/MPDCCH transmission 4 subframes from now, // Check if this is ok for BL/CE, or if the rule is different
-          RA_template->Msg4_frame    = frameP + ((subframeP>5) ? 1 : 0);
-          RA_template->Msg4_subframe = (subframeP+4)%10;
-
-        } // if process is active
-      } // loop on RA processes
-
-      break ;
-
-    case DCCH :
-    case DCCH1 :
-      //      if(eNB_mac_inst[module_idP][CC_idP].Dcch_lchan[UE_id].Active==1){
+    for (i = 0; i < num_sdu; i++) {
+	LOG_D(MAC, "SDU Number %d MAC Subheader SDU_LCID %d, length %d\n",
+	      i, rx_lcids[i], rx_lengths[i]);
+
+	T(T_ENB_MAC_UE_UL_SDU, T_INT(enb_mod_idP), T_INT(CC_idP),
+	  T_INT(current_rnti), T_INT(frameP), T_INT(subframeP),
+	  T_INT(rx_lcids[i]), T_INT(rx_lengths[i]));
+	T(T_ENB_MAC_UE_UL_SDU_WITH_DATA, T_INT(enb_mod_idP), T_INT(CC_idP),
+	  T_INT(current_rnti), T_INT(frameP), T_INT(subframeP),
+	  T_INT(rx_lcids[i]), T_INT(rx_lengths[i]), T_BUFFER(payload_ptr,
+							     rx_lengths
+							     [i]));
+
+	switch (rx_lcids[i]) {
+	case CCCH:
+	    if (rx_lengths[i] > CCCH_PAYLOAD_SIZE_MAX) {
+		LOG_E(MAC,
+		      "[eNB %d/%d] frame %d received CCCH of size %d (too big, maximum allowed is %d, sdu_len %d), dropping packet\n",
+		      enb_mod_idP, CC_idP, frameP, rx_lengths[i],
+		      CCCH_PAYLOAD_SIZE_MAX, sdu_lenP);
+		break;
+	    }
+	    LOG_D(MAC,
+		  "[eNB %d][RAPROC] CC_id %d Frame %d, Received CCCH:  %x.%x.%x.%x.%x.%x, Terminating RA procedure for UE rnti %x\n",
+		  enb_mod_idP, CC_idP, frameP, payload_ptr[0],
+		  payload_ptr[1], payload_ptr[2], payload_ptr[3],
+		  payload_ptr[4], payload_ptr[5], current_rnti);
+	    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+		(VCD_SIGNAL_DUMPER_FUNCTIONS_TERMINATE_RA_PROC, 1);
+	    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+		(VCD_SIGNAL_DUMPER_FUNCTIONS_TERMINATE_RA_PROC, 0);
+	    for (ii = 0; ii < NB_RA_PROC_MAX; ii++) {
+		RA_t *ra = &mac->common_channels[CC_idP].ra[ii];
+
+		LOG_D(MAC,
+		      "[mac %d][RAPROC] CC_id %d Checking proc %d : rnti (%x, %x), state %d\n",
+		      enb_mod_idP, CC_idP, ii, ra->rnti,
+		      current_rnti, ra->state);
+
+		if ((ra->rnti == current_rnti) && (ra->state != IDLE)) {
+
+		    //payload_ptr = parse_ulsch_header(msg3,&num_ce,&num_sdu,rx_ces,rx_lcids,rx_lengths,msg3_len);
+
+		    if (UE_id < 0) {
+			memcpy(&ra->cont_res_id[0], payload_ptr, 6);
+			LOG_D(MAC,
+			      "[eNB %d][RAPROC] CC_id %d Frame %d CCCH: Received Msg3: length %d, offset %ld\n",
+			      enb_mod_idP, CC_idP, frameP, rx_lengths[i],
+			      payload_ptr - sduP);
+
+			if ((UE_id =
+			     add_new_ue(enb_mod_idP, CC_idP,
+					mac->common_channels[CC_idP].
+					ra[ii].rnti, harq_pid
+#ifdef Rel14
+					,
+					mac->common_channels[CC_idP].
+					ra[ii].rach_resource_type
+#endif
+			     )) == -1) {
+			    AssertFatal(1 == 0,
+					"[MAC][eNB] Max user count reached\n");
+			    // kill RA procedure
+			} else
+			    LOG_D(MAC,
+				  "[eNB %d][RAPROC] CC_id %d Frame %d Added user with rnti %x => UE %d\n",
+				  enb_mod_idP, CC_idP, frameP, ra->rnti,
+				  UE_id);
+		    } else {
+			LOG_D(MAC,
+			      "[eNB %d][RAPROC] CC_id %d Frame %d CCCH: Received Msg3 from already registered UE %d: length %d, offset %ld\n",
+			      enb_mod_idP, CC_idP, frameP, UE_id,
+			      rx_lengths[i], payload_ptr - sduP);
+			// kill RA procedure
+		    }
+
+		    mac_rrc_data_ind(enb_mod_idP,
+				     CC_idP,
+				     frameP, subframeP,
+				     current_rnti,
+				     CCCH,
+				     (uint8_t *) payload_ptr,
+				     rx_lengths[i],
+				     ENB_FLAG_YES, enb_mod_idP, 0);
+
+
+		    if (num_ce > 0) {	// handle msg3 which is not RRCConnectionRequest
+			//  process_ra_message(msg3,num_ce,rx_lcids,rx_ces);
+		    }
+		    // prepare transmission of Msg4
+		    ra->state = MSG4;
+
+
+
+		    // Program Msg4 PDCCH+DLSCH/MPDCCH transmission 4 subframes from now, // Check if this is ok for BL/CE, or if the rule is different
+		    ra->Msg4_frame = frameP + ((subframeP > 5) ? 1 : 0);
+		    ra->Msg4_subframe = (subframeP + 4) % 10;
+
+		}		// if process is active
+	    }			// loop on RA processes
+
+	    break;
+
+	case DCCH:
+	case DCCH1:
+	    //      if(eNB_mac_inst[module_idP][CC_idP].Dcch_lchan[UE_id].Active==1){
 
 
 #if defined(ENABLE_MAC_PAYLOAD_DEBUG)
-      LOG_T(MAC,"offset: %d\n",(unsigned char)((unsigned char*)payload_ptr-sduP));
-      for (j=0; j<32; j++) {
-        LOG_T(MAC,"%x ",payload_ptr[j]);
-      }
-      LOG_T(MAC,"\n");
+	    LOG_T(MAC, "offset: %d\n",
+		  (unsigned char) ((unsigned char *) payload_ptr - sduP));
+	    for (j = 0; j < 32; j++) {
+		LOG_T(MAC, "%x ", payload_ptr[j]);
+	    }
+	    LOG_T(MAC, "\n");
 #endif
 
-      if (UE_id != -1) {
-        // adjust buffer occupancy of the correponding logical channel group
-        if (UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[UE_list->UE_template[CC_idP][UE_id].lcgidmap[rx_lcids[i]]] >= rx_lengths[i])
-          UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[UE_list->UE_template[CC_idP][UE_id].lcgidmap[rx_lcids[i]]] -= rx_lengths[i];
-        else
-          UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[UE_list->UE_template[CC_idP][UE_id].lcgidmap[rx_lcids[i]]] = 0;
-
-          LOG_D(MAC,"[eNB %d] CC_id %d Frame %d : ULSCH -> UL-DCCH, received %d bytes form UE %d on LCID %d \n",
-                enb_mod_idP,CC_idP,frameP, rx_lengths[i], UE_id, rx_lcids[i]);
-
-          mac_rlc_data_ind(
-                           enb_mod_idP,
-                           current_rnti,
-                           enb_mod_idP,
-                           frameP,
-                           ENB_FLAG_YES,
-                           MBMS_FLAG_NO,
-                           rx_lcids[i],
-                           (char *)payload_ptr,
-                           rx_lengths[i],
-                           1,
-                           NULL);//(unsigned int*)crc_status);
-          UE_list->eNB_UE_stats[CC_idP][UE_id].num_pdu_rx[rx_lcids[i]]+=1;
-          UE_list->eNB_UE_stats[CC_idP][UE_id].num_bytes_rx[rx_lcids[i]]+=rx_lengths[i];
-
-
-      } /* UE_id != -1 */
-
-
-      // }
-      break;
-
-      // all the DRBS
-    case DTCH:
-    default :
+	    if (UE_id != -1) {
+		// adjust buffer occupancy of the correponding logical channel group
+		if (UE_list->UE_template[CC_idP][UE_id].
+		    ul_buffer_info[UE_list->UE_template[CC_idP]
+				   [UE_id].lcgidmap[rx_lcids[i]]] >=
+		    rx_lengths[i])
+		    UE_list->UE_template[CC_idP][UE_id].
+			ul_buffer_info[UE_list->UE_template[CC_idP]
+				       [UE_id].lcgidmap[rx_lcids[i]]] -=
+			rx_lengths[i];
+		else
+		    UE_list->UE_template[CC_idP][UE_id].
+			ul_buffer_info[UE_list->UE_template[CC_idP]
+				       [UE_id].lcgidmap[rx_lcids[i]]] = 0;
+
+		LOG_D(MAC,
+		      "[eNB %d] CC_id %d Frame %d : ULSCH -> UL-DCCH, received %d bytes form UE %d on LCID %d \n",
+		      enb_mod_idP, CC_idP, frameP, rx_lengths[i], UE_id,
+		      rx_lcids[i]);
+
+		mac_rlc_data_ind(enb_mod_idP, current_rnti, enb_mod_idP, frameP, ENB_FLAG_YES, MBMS_FLAG_NO, rx_lcids[i], (char *) payload_ptr, rx_lengths[i], 1, NULL);	//(unsigned int*)crc_status);
+		UE_list->eNB_UE_stats[CC_idP][UE_id].
+		    num_pdu_rx[rx_lcids[i]] += 1;
+		UE_list->eNB_UE_stats[CC_idP][UE_id].
+		    num_bytes_rx[rx_lcids[i]]
+		    += rx_lengths[i];
+
+
+	    }
+
+	    /* UE_id != -1 */
+	    // }
+	    break;
+
+	    // all the DRBS
+	case DTCH:
+	default:
 
 #if defined(ENABLE_MAC_PAYLOAD_DEBUG)
-      LOG_T(MAC,"offset: %d\n",(unsigned char)((unsigned char*)payload_ptr-sduP));
-      for (j=0; j<32; j++) {
-        LOG_T(MAC,"%x ",payload_ptr[j]);
-      }
-      LOG_T(MAC,"\n");
+	    LOG_T(MAC, "offset: %d\n",
+		  (unsigned char) ((unsigned char *) payload_ptr - sduP));
+	    for (j = 0; j < 32; j++) {
+		LOG_T(MAC, "%x ", payload_ptr[j]);
+	    }
+	    LOG_T(MAC, "\n");
 #endif
-      if (rx_lcids[i]  < NB_RB_MAX ) {
-        LOG_D(MAC,"[eNB %d] CC_id %d Frame %d : ULSCH -> UL-DTCH, received %d bytes from UE %d for lcid %d\n",
-              enb_mod_idP,CC_idP,frameP, rx_lengths[i], UE_id, rx_lcids[i]);
-
-        if (UE_id != -1) {
-          // adjust buffer occupancy of the correponding logical channel group
-          LOG_D(MAC,"[eNB %d] CC_id %d Frame %d : ULSCH -> UL-DTCH, received %d bytes from UE %d for lcid %d, removing from LCGID %ld, %d\n",
-                enb_mod_idP,CC_idP,frameP, rx_lengths[i], UE_id,rx_lcids[i],
-                UE_list->UE_template[CC_idP][UE_id].lcgidmap[rx_lcids[i]],
-                UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[UE_list->UE_template[CC_idP][UE_id].lcgidmap[rx_lcids[i]]]);
-
-          if (UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[UE_list->UE_template[CC_idP][UE_id].lcgidmap[rx_lcids[i]]] >= rx_lengths[i])
-            UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[UE_list->UE_template[CC_idP][UE_id].lcgidmap[rx_lcids[i]]] -= rx_lengths[i];
-          else
-            UE_list->UE_template[CC_idP][UE_id].ul_buffer_info[UE_list->UE_template[CC_idP][UE_id].lcgidmap[rx_lcids[i]]] = 0;
-          if ((rx_lengths[i] <SCH_PAYLOAD_SIZE_MAX) &&  (rx_lengths[i] > 0) ) {   // MAX SIZE OF transport block
-            mac_rlc_data_ind(
-                             enb_mod_idP,
-                             current_rnti,
-                             enb_mod_idP,
-                             frameP,
-                             ENB_FLAG_YES,
-                             MBMS_FLAG_NO,
-                             rx_lcids[i],
-                             (char *)payload_ptr,
-                             rx_lengths[i],
-                             1,
-                             NULL);//(unsigned int*)crc_status);
-
-            UE_list->eNB_UE_stats[CC_idP][UE_id].num_pdu_rx[rx_lcids[i]]+=1;
-            UE_list->eNB_UE_stats[CC_idP][UE_id].num_bytes_rx[rx_lcids[i]]+=rx_lengths[i];
-          }
-          else { /* rx_length[i] */
-            UE_list->eNB_UE_stats[CC_idP][UE_id].num_errors_rx+=1;
-            LOG_E(MAC,"[eNB %d] CC_id %d Frame %d : Max size of transport block reached LCID %d from UE %d ",
-                  enb_mod_idP, CC_idP, frameP, rx_lcids[i], UE_id);
-          }
-        }
-        else {/*(UE_id != -1*/
-          LOG_E(MAC,"[eNB %d] CC_id %d Frame %d : received unsupported or unknown LCID %d from UE %d ",
-                enb_mod_idP, CC_idP, frameP, rx_lcids[i], UE_id);
-        }
-      }
-
-      break;
+	    if (rx_lcids[i] < NB_RB_MAX) {
+		LOG_D(MAC,
+		      "[eNB %d] CC_id %d Frame %d : ULSCH -> UL-DTCH, received %d bytes from UE %d for lcid %d\n",
+		      enb_mod_idP, CC_idP, frameP, rx_lengths[i], UE_id,
+		      rx_lcids[i]);
+
+		if (UE_id != -1) {
+		    // adjust buffer occupancy of the correponding logical channel group
+		    LOG_D(MAC,
+			  "[eNB %d] CC_id %d Frame %d : ULSCH -> UL-DTCH, received %d bytes from UE %d for lcid %d, removing from LCGID %ld, %d\n",
+			  enb_mod_idP, CC_idP, frameP, rx_lengths[i],
+			  UE_id, rx_lcids[i],
+			  UE_list->UE_template[CC_idP][UE_id].
+			  lcgidmap[rx_lcids[i]],
+			  UE_list->UE_template[CC_idP][UE_id].
+			  ul_buffer_info[UE_list->UE_template[CC_idP]
+					 [UE_id].lcgidmap[rx_lcids[i]]]);
+
+		    if (UE_list->UE_template[CC_idP][UE_id].
+			ul_buffer_info[UE_list->UE_template[CC_idP]
+				       [UE_id].lcgidmap[rx_lcids[i]]]
+			>= rx_lengths[i])
+			UE_list->UE_template[CC_idP][UE_id].
+			    ul_buffer_info[UE_list->UE_template[CC_idP]
+					   [UE_id].lcgidmap[rx_lcids[i]]]
+			    -= rx_lengths[i];
+		    else
+			UE_list->UE_template[CC_idP][UE_id].ul_buffer_info
+			    [UE_list->UE_template[CC_idP][UE_id].lcgidmap
+			     [rx_lcids[i]]] = 0;
+		    if ((rx_lengths[i] < SCH_PAYLOAD_SIZE_MAX)
+			&& (rx_lengths[i] > 0)) {	// MAX SIZE OF transport block
+			mac_rlc_data_ind(enb_mod_idP, current_rnti, enb_mod_idP, frameP, ENB_FLAG_YES, MBMS_FLAG_NO, rx_lcids[i], (char *) payload_ptr, rx_lengths[i], 1, NULL);	//(unsigned int*)crc_status);
+
+			UE_list->eNB_UE_stats[CC_idP][UE_id].
+			    num_pdu_rx[rx_lcids[i]] += 1;
+			UE_list->eNB_UE_stats[CC_idP][UE_id].
+			    num_bytes_rx[rx_lcids[i]] += rx_lengths[i];
+		    } else {	/* rx_length[i] */
+			UE_list->eNB_UE_stats[CC_idP][UE_id].
+			    num_errors_rx += 1;
+			LOG_E(MAC,
+			      "[eNB %d] CC_id %d Frame %d : Max size of transport block reached LCID %d from UE %d ",
+			      enb_mod_idP, CC_idP, frameP, rx_lcids[i],
+			      UE_id);
+		    }
+		} else {	/*(UE_id != -1 */
+		    LOG_E(MAC,
+			  "[eNB %d] CC_id %d Frame %d : received unsupported or unknown LCID %d from UE %d ",
+			  enb_mod_idP, CC_idP, frameP, rx_lcids[i], UE_id);
+		}
+	    }
+
+	    break;
+	}
+
+	payload_ptr += rx_lengths[i];
     }
 
-    payload_ptr+=rx_lengths[i];
-  }
-
-  // Program ACK for PHICH
-  LOG_D(MAC,"Programming PHICH ACK for rnti %x harq_pid %d (first_rb %d)\n",current_rnti,harq_pid,first_rb);
-  nfapi_hi_dci0_request_body_t   *hi_dci0_req = &eNB->HI_DCI0_req[CC_idP].hi_dci0_request_body;
-  nfapi_hi_dci0_request_pdu_t    *hi_dci0_pdu = &hi_dci0_req->hi_dci0_pdu_list[hi_dci0_req->number_of_dci+hi_dci0_req->number_of_hi];
-  memset((void*)hi_dci0_pdu,0,sizeof(nfapi_hi_dci0_request_pdu_t));
-  hi_dci0_pdu->pdu_type                                               = NFAPI_HI_DCI0_HI_PDU_TYPE;
-  hi_dci0_pdu->pdu_size                                               = 2+sizeof(nfapi_hi_dci0_hi_pdu);
-  hi_dci0_pdu->hi_pdu.hi_pdu_rel8.resource_block_start                = first_rb;
-  hi_dci0_pdu->hi_pdu.hi_pdu_rel8.cyclic_shift_2_for_drms             = 0;
-  hi_dci0_pdu->hi_pdu.hi_pdu_rel8.hi_value                            = 1;
-  hi_dci0_req->number_of_hi++;
-
-  /* NN--> FK: we could either check the payload, or use a phy helper to detect a false msg3 */
-  if ((num_sdu == 0) && (num_ce==0)) {
-    if (UE_id != -1)
-      UE_list->eNB_UE_stats[CC_idP][UE_id].total_num_errors_rx+=1;
-    /*
-    if (msg3_flagP != NULL) {
-      if( *msg3_flagP == 1 ) {
-        LOG_N(MAC,"[eNB %d] CC_id %d frame %d : false msg3 detection: signal phy to canceling RA and remove the UE\n", enb_mod_idP, CC_idP, frameP);
-        *msg3_flagP=0;
-      }
-      }*/
-  } else {
-    if (UE_id != -1) {
-      UE_list->eNB_UE_stats[CC_idP][UE_id].pdu_bytes_rx=sdu_lenP;
-      UE_list->eNB_UE_stats[CC_idP][UE_id].total_pdu_bytes_rx+=sdu_lenP;
-      UE_list->eNB_UE_stats[CC_idP][UE_id].total_num_pdus_rx+=1;
+    // Program ACK for PHICH
+    LOG_D(MAC,
+	  "Programming PHICH ACK for rnti %x harq_pid %d (first_rb %d)\n",
+	  current_rnti, harq_pid, first_rb);
+    nfapi_hi_dci0_request_body_t *hi_dci0_req =
+	&mac->HI_DCI0_req[CC_idP].hi_dci0_request_body;
+    nfapi_hi_dci0_request_pdu_t *hi_dci0_pdu =
+	&hi_dci0_req->hi_dci0_pdu_list[hi_dci0_req->number_of_dci +
+				       hi_dci0_req->number_of_hi];
+    memset((void *) hi_dci0_pdu, 0, sizeof(nfapi_hi_dci0_request_pdu_t));
+    hi_dci0_pdu->pdu_type = NFAPI_HI_DCI0_HI_PDU_TYPE;
+    hi_dci0_pdu->pdu_size = 2 + sizeof(nfapi_hi_dci0_hi_pdu);
+    hi_dci0_pdu->hi_pdu.hi_pdu_rel8.resource_block_start = first_rb;
+    hi_dci0_pdu->hi_pdu.hi_pdu_rel8.cyclic_shift_2_for_drms = 0;
+    hi_dci0_pdu->hi_pdu.hi_pdu_rel8.hi_value = 1;
+    hi_dci0_req->number_of_hi++;
+
+    /* NN--> FK: we could either check the payload, or use a phy helper to detect a false msg3 */
+    if ((num_sdu == 0) && (num_ce == 0)) {
+	if (UE_id != -1)
+	    UE_list->eNB_UE_stats[CC_idP][UE_id].total_num_errors_rx += 1;
+	/*
+	   if (msg3_flagP != NULL) {
+	   if( *msg3_flagP == 1 ) {
+	   LOG_N(MAC,"[eNB %d] CC_id %d frame %d : false msg3 detection: signal phy to canceling RA and remove the UE\n", enb_mod_idP, CC_idP, frameP);
+	   *msg3_flagP=0;
+	   }
+	   } */
+    } else {
+	if (UE_id != -1) {
+	    UE_list->eNB_UE_stats[CC_idP][UE_id].pdu_bytes_rx = sdu_lenP;
+	    UE_list->eNB_UE_stats[CC_idP][UE_id].total_pdu_bytes_rx +=
+		sdu_lenP;
+	    UE_list->eNB_UE_stats[CC_idP][UE_id].total_num_pdus_rx += 1;
+	}
     }
-  }
 
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_RX_SDU,0);
-  stop_meas(&eNB->rx_ulsch_sdu);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_RX_SDU, 0);
+    stop_meas(&mac->rx_ulsch_sdu);
 }
 
 uint32_t bytes_to_bsr_index(int32_t nbytes)
 {
-  uint32_t i=0;
+    uint32_t i = 0;
 
-  if (nbytes<0) {
-    return(0);
-  }
+    if (nbytes < 0) {
+	return (0);
+    }
 
-  while ((i<BSR_TABLE_SIZE)&&
-         (BSR_TABLE[i]<=nbytes)) {
-    i++;
-  }
+    while ((i < BSR_TABLE_SIZE) && (BSR_TABLE[i] <= nbytes)) {
+	i++;
+    }
 
-  return(i-1);
+    return (i - 1);
 }
 
-void add_ue_ulsch_info(module_id_t module_idP, int CC_id, int UE_id, sub_frame_t subframeP, UE_ULSCH_STATUS status)
+void
+add_ue_ulsch_info(module_id_t module_idP, int CC_id, int UE_id,
+		  sub_frame_t subframeP, UE_ULSCH_STATUS status)
 {
-  eNB_ulsch_info[module_idP][CC_id][UE_id].rnti             = UE_RNTI(module_idP,UE_id);
-  eNB_ulsch_info[module_idP][CC_id][UE_id].subframe         = subframeP;
-  eNB_ulsch_info[module_idP][CC_id][UE_id].status           = status;
+    eNB_ulsch_info[module_idP][CC_id][UE_id].rnti =
+	UE_RNTI(module_idP, UE_id);
+    eNB_ulsch_info[module_idP][CC_id][UE_id].subframe = subframeP;
+    eNB_ulsch_info[module_idP][CC_id][UE_id].status = status;
 
-  eNB_ulsch_info[module_idP][CC_id][UE_id].serving_num++;
+    eNB_ulsch_info[module_idP][CC_id][UE_id].serving_num++;
 }
 
 unsigned char *parse_ulsch_header(unsigned char *mac_header,
-                                  unsigned char *num_ce,
-                                  unsigned char *num_sdu,
-                                  unsigned char *rx_ces,
-                                  unsigned char *rx_lcids,
-                                  unsigned short *rx_lengths,
-                                  unsigned short tb_length)
+				  unsigned char *num_ce,
+				  unsigned char *num_sdu,
+				  unsigned char *rx_ces,
+				  unsigned char *rx_lcids,
+				  unsigned short *rx_lengths,
+				  unsigned short tb_length)
 {
-  unsigned char not_done=1,num_ces=0,num_sdus=0,lcid,num_sdu_cnt;
-  unsigned char *mac_header_ptr = mac_header;
-  unsigned short length, ce_len=0;
-
-  while (not_done==1) {
-
-    if (((SCH_SUBHEADER_FIXED*)mac_header_ptr)->E == 0) {
-      not_done = 0;
-    }
-
-    lcid = ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID;
-
-    if (lcid < EXTENDED_POWER_HEADROOM) {
-      if (not_done==0) { // last MAC SDU, length is implicit
-        mac_header_ptr++;
-        length = tb_length-(mac_header_ptr-mac_header)-ce_len;
-
-        for (num_sdu_cnt=0; num_sdu_cnt < num_sdus ; num_sdu_cnt++) {
-          length -= rx_lengths[num_sdu_cnt];
-        }
-      } else {
-        if (((SCH_SUBHEADER_SHORT *)mac_header_ptr)->F == 0) {
-          length = ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->L;
-          mac_header_ptr += 2;//sizeof(SCH_SUBHEADER_SHORT);
-        } else { // F = 1
-          length = ((((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_MSB & 0x7f ) << 8 ) | (((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_LSB & 0xff);
-          mac_header_ptr += 3;//sizeof(SCH_SUBHEADER_LONG);
-        }
-      }
-
-      LOG_D(MAC,"[eNB] sdu %d lcid %d tb_length %d length %d (offset now %ld)\n",
-            num_sdus,lcid,tb_length, length,mac_header_ptr-mac_header);
-      rx_lcids[num_sdus] = lcid;
-      rx_lengths[num_sdus] = length;
-      num_sdus++;
-    } else { // This is a control element subheader POWER_HEADROOM, BSR and CRNTI
-      if (lcid == SHORT_PADDING) {
-        mac_header_ptr++;
-      } else {
-        rx_ces[num_ces] = lcid;
-        num_ces++;
-        mac_header_ptr++;
-
-        if (lcid==LONG_BSR) {
-          ce_len+=3;
-        } else if (lcid==CRNTI) {
-          ce_len+=2;
-        } else if ((lcid==POWER_HEADROOM) || (lcid==TRUNCATED_BSR)|| (lcid== SHORT_BSR)) {
-          ce_len++;
-        } else {
-          LOG_E(MAC,"unknown CE %d \n", lcid);
-          AssertFatal(1==0,"unknown CE");
-        }
-      }
+    unsigned char not_done = 1, num_ces = 0, num_sdus =
+	0, lcid, num_sdu_cnt;
+    unsigned char *mac_header_ptr = mac_header;
+    unsigned short length, ce_len = 0;
+
+    while (not_done == 1) {
+
+	if (((SCH_SUBHEADER_FIXED *) mac_header_ptr)->E == 0) {
+	    not_done = 0;
+	}
+
+	lcid = ((SCH_SUBHEADER_FIXED *) mac_header_ptr)->LCID;
+
+	if (lcid < EXTENDED_POWER_HEADROOM) {
+	    if (not_done == 0) {	// last MAC SDU, length is implicit
+		mac_header_ptr++;
+		length =
+		    tb_length - (mac_header_ptr - mac_header) - ce_len;
+
+		for (num_sdu_cnt = 0; num_sdu_cnt < num_sdus;
+		     num_sdu_cnt++) {
+		    length -= rx_lengths[num_sdu_cnt];
+		}
+	    } else {
+		if (((SCH_SUBHEADER_SHORT *) mac_header_ptr)->F == 0) {
+		    length = ((SCH_SUBHEADER_SHORT *) mac_header_ptr)->L;
+		    mac_header_ptr += 2;	//sizeof(SCH_SUBHEADER_SHORT);
+		} else {	// F = 1
+		    length =
+			((((SCH_SUBHEADER_LONG *) mac_header_ptr)->L_MSB &
+			  0x7f) << 8) | (((SCH_SUBHEADER_LONG *)
+					  mac_header_ptr)->L_LSB & 0xff);
+		    mac_header_ptr += 3;	//sizeof(SCH_SUBHEADER_LONG);
+		}
+	    }
+
+	    LOG_D(MAC,
+		  "[eNB] sdu %d lcid %d tb_length %d length %d (offset now %ld)\n",
+		  num_sdus, lcid, tb_length, length,
+		  mac_header_ptr - mac_header);
+	    rx_lcids[num_sdus] = lcid;
+	    rx_lengths[num_sdus] = length;
+	    num_sdus++;
+	} else {		// This is a control element subheader POWER_HEADROOM, BSR and CRNTI
+	    if (lcid == SHORT_PADDING) {
+		mac_header_ptr++;
+	    } else {
+		rx_ces[num_ces] = lcid;
+		num_ces++;
+		mac_header_ptr++;
+
+		if (lcid == LONG_BSR) {
+		    ce_len += 3;
+		} else if (lcid == CRNTI) {
+		    ce_len += 2;
+		} else if ((lcid == POWER_HEADROOM)
+			   || (lcid == TRUNCATED_BSR)
+			   || (lcid == SHORT_BSR)) {
+		    ce_len++;
+		} else {
+		    LOG_E(MAC, "unknown CE %d \n", lcid);
+		    AssertFatal(1 == 0, "unknown CE");
+		}
+	    }
+	}
     }
-  }
 
-  *num_ce = num_ces;
-  *num_sdu = num_sdus;
+    *num_ce = num_ces;
+    *num_sdu = num_sdus;
 
-  return(mac_header_ptr);
+    return (mac_header_ptr);
 }
 
 /* This function is called by PHY layer when it schedules some
@@ -707,538 +902,629 @@ unsigned char *parse_ulsch_header(unsigned char *mac_header,
  * The MAC scheduler has to skip the RBs used by this message 3
  * (done below in schedule_ulsch).
  */
-void set_msg3_subframe(module_id_t Mod_id,
-                       int CC_id,
-                       int frame,
-                       int subframe,
-                       int rnti,
-                       int Msg3_frame,
-                       int Msg3_subframe)
+void
+set_msg3_subframe(module_id_t Mod_id,
+		  int CC_id,
+		  int frame,
+		  int subframe, int rnti, int Msg3_frame,
+		  int Msg3_subframe)
 {
-  eNB_MAC_INST *eNB=RC.mac[Mod_id];
-  int i;
-  for (i=0; i<NB_RA_PROC_MAX; i++) {
-    if (eNB->common_channels[CC_id].RA_template[i].RA_active == TRUE &&
-        eNB->common_channels[CC_id].RA_template[i].rnti == rnti) {
-      eNB->common_channels[CC_id].RA_template[i].Msg3_subframe = Msg3_subframe;
-      break;
+    eNB_MAC_INST *mac = RC.mac[Mod_id];
+    int i;
+    for (i = 0; i < NB_RA_PROC_MAX; i++) {
+	if (mac->common_channels[CC_id].ra[i].state != IDLE &&
+	    mac->common_channels[CC_id].ra[i].rnti == rnti) {
+	    mac->common_channels[CC_id].ra[i].Msg3_subframe =
+		Msg3_subframe;
+	    break;
+	}
     }
-  }
 }
 
 
-void schedule_ulsch(module_id_t module_idP,
-                    frame_t frameP,
-                    sub_frame_t subframeP)
+void
+schedule_ulsch(module_id_t module_idP, frame_t frameP,
+	       sub_frame_t subframeP)
 {
-  uint16_t first_rb[MAX_NUM_CCs],i;
-  int CC_id;
-  eNB_MAC_INST *eNB=RC.mac[module_idP];
-  COMMON_channels_t *cc;
-
-  start_meas(&eNB->schedule_ulsch);
-
-  int sched_subframe = (subframeP+4)%10;
-
-  cc = &eNB->common_channels[0];
-  int tdd_sfa;
-  // for TDD: check subframes where we have to act and return if nothing should be done now
-  if (cc->tdd_Config) {
-    tdd_sfa = cc->tdd_Config->subframeAssignment;
-    switch (subframeP) {
-    case 0:
-      if ((tdd_sfa == 0)||
-          (tdd_sfa == 3)||
-          (tdd_sfa == 6)) sched_subframe = 4;
-      else return;
-      break;
-    case 1:
-      if ((tdd_sfa==0)||
-          (tdd_sfa==1)) sched_subframe = 7;
-      else if (tdd_sfa==6) sched_subframe = 8;
-      break;
-    default:
-      return;
-
-    case 2: // Don't schedule UL in subframe 2 for TDD
-      return;
-    case 3:
-      if (tdd_sfa==2) sched_subframe = 7;
-      else return;
-      break;
-    case 4:
-      if (tdd_sfa==1) sched_subframe = 8;
-      else return;
-      break;
-    case 5:
-      if (tdd_sfa==0)      sched_subframe = 9;
-      else if (tdd_sfa==6) sched_subframe = 3;
-      else return;
-      break;
-    case 6:
-      if (tdd_sfa==1)      sched_subframe = 2;
-      else if (tdd_sfa==6) sched_subframe = 3;
-      else return;
-      break;
-    case 7:
-      return;
-    case 8:
-      if ((tdd_sfa>=2) || (tdd_sfa<=5)) sched_subframe=2;
-      else return;
-      break;
-    case 9:
-      if ((tdd_sfa==1) || (tdd_sfa==3) || (tdd_sfa==4)) sched_subframe=3;
-      else if (tdd_sfa==6) sched_subframe=4;
-      else return;
-      break;
+    uint16_t first_rb[MAX_NUM_CCs], i;
+    int CC_id;
+    eNB_MAC_INST *mac = RC.mac[module_idP];
+    COMMON_channels_t *cc;
+
+    start_meas(&mac->schedule_ulsch);
+
+    int sched_subframe = (subframeP + 4) % 10;
+
+    cc = &mac->common_channels[0];
+    int tdd_sfa;
+    // for TDD: check subframes where we have to act and return if nothing should be done now
+    if (cc->tdd_Config) {
+	tdd_sfa = cc->tdd_Config->subframeAssignment;
+	switch (subframeP) {
+	case 0:
+	    if ((tdd_sfa == 0) || (tdd_sfa == 3) || (tdd_sfa == 6))
+		sched_subframe = 4;
+	    else
+		return;
+	    break;
+	case 1:
+	    if ((tdd_sfa == 0) || (tdd_sfa == 1))
+		sched_subframe = 7;
+	    else if (tdd_sfa == 6)
+		sched_subframe = 8;
+	    break;
+	default:
+	    return;
+
+	case 2:		// Don't schedule UL in subframe 2 for TDD
+	    return;
+	case 3:
+	    if (tdd_sfa == 2)
+		sched_subframe = 7;
+	    else
+		return;
+	    break;
+	case 4:
+	    if (tdd_sfa == 1)
+		sched_subframe = 8;
+	    else
+		return;
+	    break;
+	case 5:
+	    if (tdd_sfa == 0)
+		sched_subframe = 9;
+	    else if (tdd_sfa == 6)
+		sched_subframe = 3;
+	    else
+		return;
+	    break;
+	case 6:
+	    if (tdd_sfa == 1)
+		sched_subframe = 2;
+	    else if (tdd_sfa == 6)
+		sched_subframe = 3;
+	    else
+		return;
+	    break;
+	case 7:
+	    return;
+	case 8:
+	    if ((tdd_sfa >= 2) || (tdd_sfa <= 5))
+		sched_subframe = 2;
+	    else
+		return;
+	    break;
+	case 9:
+	    if ((tdd_sfa == 1) || (tdd_sfa == 3) || (tdd_sfa == 4))
+		sched_subframe = 3;
+	    else if (tdd_sfa == 6)
+		sched_subframe = 4;
+	    else
+		return;
+	    break;
+	}
     }
-  }
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
 
 
 
-    //leave out first RB for PUCCH
-    first_rb[CC_id] = 1;
+	//leave out first RB for PUCCH
+	first_rb[CC_id] = 1;
 
-    // UE data info;
-    // check which UE has data to transmit
-    // function to decide the scheduling
-    // e.g. scheduling_rslt = Greedy(granted_UEs, nb_RB)
+	// UE data info;
+	// check which UE has data to transmit
+	// function to decide the scheduling
+	// e.g. scheduling_rslt = Greedy(granted_UEs, nb_RB)
 
-    // default function for default scheduling
-    //
+	// default function for default scheduling
+	//
 
-    // output of scheduling, the UE numbers in RBs, where it is in the code???
-    // check if RA (Msg3) is active in this subframeP, if so skip the PRBs used for Msg3
-    // Msg3 is using 1 PRB so we need to increase first_rb accordingly
-    // not sure about the break (can there be more than 1 active RA procedure?)
+	// output of scheduling, the UE numbers in RBs, where it is in the code???
+	// check if RA (Msg3) is active in this subframeP, if so skip the PRBs used for Msg3
+	// Msg3 is using 1 PRB so we need to increase first_rb accordingly
+	// not sure about the break (can there be more than 1 active RA procedure?)
 
 
-    for (i=0; i<NB_RA_PROC_MAX; i++) {
-      if ((cc->RA_template[i].RA_active == TRUE) &&
-          (cc->RA_template[i].generate_rar == 0) &&
-          (cc->RA_template[i].generate_Msg4 == 0) &&
-          (cc->RA_template[i].wait_ack_Msg4 == 0) &&
-          (cc->RA_template[i].Msg3_subframe == sched_subframe)) {
-        first_rb[CC_id]++;
-        //    cc->RA_template[i].Msg3_subframe = -1;
-        break;
-      }
+	for (i = 0; i < NB_RA_PROC_MAX; i++) {
+	    if ((cc->ra[i].state == WAITMSG3) &&
+		(cc->ra[i].Msg3_subframe == sched_subframe)) {
+		first_rb[CC_id]++;
+		//    cc->ray[i].Msg3_subframe = -1;
+		break;
+	    }
+	}
     }
-  }
 
-  schedule_ulsch_rnti(module_idP, frameP, subframeP, sched_subframe,first_rb);
+    schedule_ulsch_rnti(module_idP, frameP, subframeP, sched_subframe,
+			first_rb);
 
-  stop_meas(&eNB->schedule_ulsch);
+    stop_meas(&mac->schedule_ulsch);
 }
 
-void schedule_ulsch_rnti(module_id_t   module_idP,
-                         frame_t       frameP,
-                         sub_frame_t   subframeP,
-                         unsigned char sched_subframeP,
-                         uint16_t     *first_rb)
+void
+schedule_ulsch_rnti(module_id_t module_idP,
+		    frame_t frameP,
+		    sub_frame_t subframeP,
+		    unsigned char sched_subframeP, uint16_t * first_rb)
 {
-  int               UE_id;
-  uint8_t           aggregation    = 2;
-  rnti_t            rnti           = -1;
-  uint8_t           round          = 0;
-  uint8_t           harq_pid       = 0;
-  uint8_t           status         = 0;
-  uint8_t           rb_table_index = -1;
-  uint32_t          cqi_req,cshift,ndi,tpc;
-  int32_t           normalized_rx_power;
-  int32_t           target_rx_power=-90;
-  static int32_t    tpc_accumulated=0;
-  int               n;
-  int               CC_id = 0;
-  int               drop_ue=0;
-  int               N_RB_UL;
-  eNB_MAC_INST      *eNB = RC.mac[module_idP];
-  COMMON_channels_t *cc  = eNB->common_channels;
-  UE_list_t         *UE_list=&eNB->UE_list;
-  UE_TEMPLATE       *UE_template;
-  UE_sched_ctrl     *UE_sched_ctrl;
-  int               sched_frame=frameP;
-  int               rvidx_tab[4] = {0,2,3,1};
-
-  if (sched_subframeP<subframeP) sched_frame++;
-
-  nfapi_hi_dci0_request_body_t   *hi_dci0_req = &eNB->HI_DCI0_req[CC_id].hi_dci0_request_body;
-  nfapi_hi_dci0_request_pdu_t    *hi_dci0_pdu;
-
-  nfapi_ul_config_request_body_t *ul_req_tmp       = &eNB->UL_req_tmp[CC_id][sched_subframeP].ul_config_request_body;
-
-  LOG_D(MAC,"entering ulsch preprocesor\n");
-  ulsch_scheduler_pre_processor(module_idP,
-                                frameP,
-                                subframeP,
-                                first_rb);
-
-  LOG_D(MAC,"exiting ulsch preprocesor\n");
-
-  eNB->HI_DCI0_req[CC_id].sfn_sf = (frameP<<4)+subframeP;
-
-  // loop over all active UEs
-  for (UE_id=UE_list->head_ul; UE_id>=0; UE_id=UE_list->next_ul[UE_id]) {
-
-    // don't schedule if Msg4 is not received yet
-    if (UE_list->UE_template[UE_PCCID(module_idP,UE_id)][UE_id].configured==FALSE) {
-      LOG_D(MAC,"[eNB %d] frame %d subfarme %d, UE %d: not configured, skipping UE scheduling \n",
-            module_idP,frameP,subframeP,UE_id);
-      continue;
-    }
-
-    rnti = UE_RNTI(module_idP,UE_id);
-
-    if (rnti==NOT_A_RNTI) {
-      LOG_W(MAC,"[eNB %d] frame %d subfarme %d, UE %d: no RNTI \n", module_idP,frameP,subframeP,UE_id);
-      continue;
-    }
-
-    drop_ue = 0;
-    /* let's drop the UE if get_eNB_UE_stats returns NULL when calling it with any of the UE's active UL CCs */
-    /* TODO: refine?
-
-    for (n=0; n<UE_list->numactiveULCCs[UE_id]; n++) {
-      CC_id = UE_list->ordered_ULCCids[n][UE_id];
-
-      if (mac_xface->get_eNB_UE_stats(module_idP,CC_id,rnti) == NULL) {
-        LOG_W(MAC,"[eNB %d] frame %d subframe %d, UE %d/%x CC %d: no PHY context\n", module_idP,frameP,subframeP,UE_id,rnti,CC_id);
-        drop_ue = 1;
-        break;
-      }
-      }*/
-    if (drop_ue == 1) {
+    int UE_id;
+    uint8_t aggregation = 2;
+    rnti_t rnti = -1;
+    uint8_t round = 0;
+    uint8_t harq_pid = 0;
+    uint8_t status = 0;
+    uint8_t rb_table_index = -1;
+    uint32_t cqi_req, cshift, ndi, tpc;
+    int32_t normalized_rx_power;
+    int32_t target_rx_power = -90;
+    static int32_t tpc_accumulated = 0;
+    int n;
+    int CC_id = 0;
+    int drop_ue = 0;
+    int N_RB_UL;
+    eNB_MAC_INST *mac = RC.mac[module_idP];
+    COMMON_channels_t *cc = mac->common_channels;
+    UE_list_t *UE_list = &mac->UE_list;
+    UE_TEMPLATE *UE_template;
+    UE_sched_ctrl *UE_sched_ctrl;
+    int sched_frame = frameP;
+    int rvidx_tab[4] = { 0, 2, 3, 1 };
+
+    if (sched_subframeP < subframeP)
+	sched_frame++;
+
+    nfapi_hi_dci0_request_body_t *hi_dci0_req =
+	&mac->HI_DCI0_req[CC_id].hi_dci0_request_body;
+    nfapi_hi_dci0_request_pdu_t *hi_dci0_pdu;
+
+    nfapi_ul_config_request_body_t *ul_req_tmp =
+	&mac->UL_req_tmp[CC_id][sched_subframeP].ul_config_request_body;
+
+    LOG_D(MAC, "entering ulsch preprocesor\n");
+    ulsch_scheduler_pre_processor(module_idP, frameP, subframeP, first_rb);
+
+    LOG_D(MAC, "exiting ulsch preprocesor\n");
+
+    mac->HI_DCI0_req[CC_id].sfn_sf = (frameP << 4) + subframeP;
+
+    // loop over all active UEs
+    for (UE_id = UE_list->head_ul; UE_id >= 0;
+	 UE_id = UE_list->next_ul[UE_id]) {
+
+	// don't schedule if Msg4 is not received yet
+	if (UE_list->UE_template[UE_PCCID(module_idP, UE_id)][UE_id].
+	    configured == FALSE) {
+	    LOG_D(MAC,
+		  "[eNB %d] frame %d subfarme %d, UE %d: not configured, skipping UE scheduling \n",
+		  module_idP, frameP, subframeP, UE_id);
+	    continue;
+	}
+
+	rnti = UE_RNTI(module_idP, UE_id);
+
+	if (rnti == NOT_A_RNTI) {
+	    LOG_W(MAC, "[eNB %d] frame %d subfarme %d, UE %d: no RNTI \n",
+		  module_idP, frameP, subframeP, UE_id);
+	    continue;
+	}
+
+	drop_ue = 0;
+	/* let's drop the UE if get_eNB_UE_stats returns NULL when calling it with any of the UE's active UL CCs */
+	/* TODO: refine?
+
+	   for (n=0; n<UE_list->numactiveULCCs[UE_id]; n++) {
+	   CC_id = UE_list->ordered_ULCCids[n][UE_id];
+
+	   if (mac_xface->get_eNB_UE_stats(module_idP,CC_id,rnti) == NULL) {
+	   LOG_W(MAC,"[eNB %d] frame %d subframe %d, UE %d/%x CC %d: no PHY context\n", module_idP,frameP,subframeP,UE_id,rnti,CC_id);
+	   drop_ue = 1;
+	   break;
+	   }
+	   } */
+	if (drop_ue == 1) {
 /* we can't come here, ulsch_scheduler_pre_processor won't put in the list a UE with no PHY context */
-abort();
-      /* TODO: this is a hack. Sometimes the UE has no PHY context but
-       * is still present in the MAC with 'ul_failure_timer' = 0 and
-       * 'ul_out_of_sync' = 0. It seems wrong and the UE stays there forever. Let's
-       * start an UL out of sync procedure in this case.
-       * The root cause of this problem has to be found and corrected.
-       * In the meantime, this hack...
-       */
-      if (UE_list->UE_sched_ctrl[UE_id].ul_failure_timer == 0 &&
-          UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 0) {
-        LOG_W(MAC,"[eNB %d] frame %d subframe %d, UE %d/%x CC %d: UE in weird state, let's put it 'out of sync'\n",
-              module_idP,frameP,subframeP,UE_id,rnti,CC_id);
-        // inform RRC of failure and clear timer
-        mac_eNB_rrc_ul_failure(module_idP,CC_id,frameP,subframeP,rnti);
-        UE_list->UE_sched_ctrl[UE_id].ul_failure_timer=0;
-        UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync=1;
-      }
-      continue;
-    }
-
-    // loop over all active UL CC_ids for this UE
-    for (n=0; n<UE_list->numactiveULCCs[UE_id]; n++) {
-      // This is the actual CC_id in the list
-      CC_id        = UE_list->ordered_ULCCids[n][UE_id];
-      N_RB_UL      = to_prb(cc[CC_id].ul_Bandwidth);
-
-      /*
-      aggregation=get_aggregation(get_bw_index(module_idP,CC_id),
-                                  eNB_UE_stats->dl_cqi,
-                                  format0);
-      */
-
-      if (CCE_allocation_infeasible(module_idP,CC_id,1,subframeP,aggregation,rnti)) {
-        LOG_W(MAC,"[eNB %d] frame %d subframe %d, UE %d/%x CC %d: not enough nCCE\n", module_idP,frameP,subframeP,UE_id,rnti,CC_id);
-        continue; // break;
-      }
-
-      /* be sure that there are some free RBs */
-      if (first_rb[CC_id] >= N_RB_UL-1) {
-        LOG_W(MAC,"[eNB %d] frame %d subframe %d, UE %d/%x CC %d: dropping, not enough RBs\n",
-              module_idP,frameP,subframeP,UE_id,rnti,CC_id);
-        continue;
-      }
-
-      //      if (eNB_UE_stats->mode == PUSCH) { // ue has a ulsch channel
-
-      UE_template   = &UE_list->UE_template[CC_id][UE_id];
-      UE_sched_ctrl = &UE_list->UE_sched_ctrl[UE_id];
-      harq_pid      = subframe2harqpid(&cc[CC_id],sched_frame,sched_subframeP);
-      round         = UE_sched_ctrl->round_UL[CC_id][harq_pid];
-      AssertFatal(round<8,"round %d > 7 for UE %d/%x\n",round,UE_id,rnti);
-      LOG_D(MAC,"[eNB %d] frame %d subframe %d,Checking PUSCH %d for UE %d/%x CC %d : aggregation level %d, N_RB_UL %d\n",
-            module_idP,frameP,subframeP,harq_pid,UE_id,rnti,CC_id, aggregation,N_RB_UL);
-
-      RC.eNB[module_idP][CC_id]->pusch_stats_BO[UE_id][(frameP*10)+subframeP] = UE_template->ul_total_buffer;
-      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_BO,RC.eNB[module_idP][CC_id]->pusch_stats_BO[UE_id][(frameP*10)+subframeP]);
-      if (UE_is_to_be_scheduled(module_idP,CC_id,UE_id) > 0 || round > 0)// || ((frameP%10)==0))
-        // if there is information on bsr of DCCH, DTCH or if there is UL_SR, or if there is a packet to retransmit, or we want to schedule a periodic feedback every 10 frames
-        {
-          LOG_D(MAC,"[eNB %d][PUSCH %d] Frame %d subframe %d Scheduling UE %d/%x in round %d(SR %d,UL_inactivity timer %d,UL_failure timer %d,cqi_req_timer %d)\n",
-                module_idP,harq_pid,frameP,subframeP,UE_id,rnti,round,UE_template->ul_SR,
-                UE_sched_ctrl->ul_inactivity_timer,
-
-                UE_sched_ctrl->ul_failure_timer,
-
-                UE_sched_ctrl->cqi_req_timer);
-          // reset the scheduling request
-          UE_template->ul_SR = 0;
-          status = mac_eNB_get_rrc_status(module_idP,rnti);
-          if (status < RRC_CONNECTED)
-            cqi_req = 0;
-          else if (UE_sched_ctrl->cqi_req_timer>30) {
-            cqi_req = 1;
-            UE_sched_ctrl->cqi_req_timer=0;
-          }
-          else
-            cqi_req = 0;
-
-          //power control
-          //compute the expected ULSCH RX power (for the stats)
-
-          // this is the normalized RX power and this should be constant (regardless of mcs
-          normalized_rx_power = UE_sched_ctrl->pusch_snr[CC_id];
-          target_rx_power = 178;
-
-          // this assumes accumulated tpc
-          // make sure that we are only sending a tpc update once a frame, otherwise the control loop will freak out
-          int32_t framex10psubframe = UE_template->pusch_tpc_tx_frame*10+UE_template->pusch_tpc_tx_subframe;
-          if (((framex10psubframe+10)<=(frameP*10+subframeP)) || //normal case
-              ((framex10psubframe>(frameP*10+subframeP)) && (((10240-framex10psubframe+frameP*10+subframeP)>=10)))) //frame wrap-around
-            {
-              UE_template->pusch_tpc_tx_frame=frameP;
-              UE_template->pusch_tpc_tx_subframe=subframeP;
-              if (normalized_rx_power>(target_rx_power+4)) {
-                tpc = 0; //-1
-                tpc_accumulated--;
-              } else if (normalized_rx_power<(target_rx_power-4)) {
-                tpc = 2; //+1
-                tpc_accumulated++;
-              } else {
-                tpc = 1; //0
-              }
-            } else {
-            tpc = 1; //0
-          }
-          //tpc = 1;
-          if (tpc!=1) {
-            LOG_D(MAC,"[eNB %d] ULSCH scheduler: frame %d, subframe %d, harq_pid %d, tpc %d, accumulated %d, normalized/target rx power %d/%d\n",
-                  module_idP,frameP,subframeP,harq_pid,tpc,
-                  tpc_accumulated,normalized_rx_power,target_rx_power);
-          }
-
-          // new transmission
-          if (round==0) {
-
-            ndi = 1-UE_template->oldNDI_UL[harq_pid];
-            UE_template->oldNDI_UL[harq_pid]=ndi;
-            UE_list->eNB_UE_stats[CC_id][UE_id].normalized_rx_power=normalized_rx_power;
-            UE_list->eNB_UE_stats[CC_id][UE_id].target_rx_power=target_rx_power;
-            UE_list->eNB_UE_stats[CC_id][UE_id].ulsch_mcs1=UE_template->pre_assigned_mcs_ul;
-            UE_template->mcs_UL[harq_pid] = UE_template->pre_assigned_mcs_ul;//cmin (UE_template->pre_assigned_mcs_ul, openair_daq_vars.target_ue_ul_mcs); // adjust, based on user-defined MCS
-            if (UE_template->pre_allocated_rb_table_index_ul >=0) {
-              rb_table_index=UE_template->pre_allocated_rb_table_index_ul;
-            } else {
-              UE_template->mcs_UL[harq_pid]=10;//cmin (10, openair_daq_vars.target_ue_ul_mcs);
-              rb_table_index=5; // for PHR
-            }
-
-            UE_list->eNB_UE_stats[CC_id][UE_id].ulsch_mcs2=UE_template->mcs_UL[harq_pid];
-            //            buffer_occupancy = UE_template->ul_total_buffer;
-
-
-            while (((rb_table[rb_table_index]>(N_RB_UL-1-first_rb[CC_id])) ||
-                    (rb_table[rb_table_index]>45)) &&
-                   (rb_table_index>0)) {
-              rb_table_index--;
-            }
-
-            UE_template->TBS_UL[harq_pid] = get_TBS_UL(UE_template->mcs_UL[harq_pid],rb_table[rb_table_index]);
-            UE_list->eNB_UE_stats[CC_id][UE_id].total_rbs_used_rx+=rb_table[rb_table_index];
-            UE_list->eNB_UE_stats[CC_id][UE_id].ulsch_TBS=UE_template->TBS_UL[harq_pid];
-            //            buffer_occupancy -= TBS;
-
-            T(T_ENB_MAC_UE_UL_SCHEDULE, T_INT(module_idP), T_INT(CC_id), T_INT(rnti), T_INT(frameP),
-              T_INT(subframeP), T_INT(harq_pid), T_INT(UE_template->mcs_UL[harq_pid]), T_INT(first_rb[CC_id]), T_INT(rb_table[rb_table_index]),
-              T_INT(UE_template->TBS_UL[harq_pid]), T_INT(ndi));
-
-            if (mac_eNB_get_rrc_status(module_idP,rnti) < RRC_CONNECTED)
-              LOG_D(MAC,"[eNB %d][PUSCH %d/%x] CC_id %d Frame %d subframeP %d Scheduled UE %d (mcs %d, first rb %d, nb_rb %d, rb_table_index %d, TBS %d, harq_pid %d)\n",
-                    module_idP,harq_pid,rnti,CC_id,frameP,subframeP,UE_id,UE_template->mcs_UL[harq_pid],
-                    first_rb[CC_id],rb_table[rb_table_index],
-                    rb_table_index,UE_template->TBS_UL[harq_pid],harq_pid);
-
-            // bad indices : 20 (40 PRB), 21 (45 PRB), 22 (48 PRB)
-            //store for possible retransmission
-            UE_template->nb_rb_ul[harq_pid]    = rb_table[rb_table_index];
-            UE_template->first_rb_ul[harq_pid] = first_rb[CC_id];
-
-            UE_sched_ctrl->ul_scheduled |= (1<<harq_pid);
-            if (UE_id == UE_list->head)
-              VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_SCHEDULED,UE_sched_ctrl->ul_scheduled);
-
-            // adjust total UL buffer status by TBS, wait for UL sdus to do final update
-            LOG_D(MAC,"[eNB %d] CC_id %d UE %d/%x : adjusting ul_total_buffer, old %d, TBS %d\n", module_idP,CC_id,UE_id,rnti,UE_template->ul_total_buffer,UE_template->TBS_UL[harq_pid]);
-            if (UE_template->ul_total_buffer > UE_template->TBS_UL[harq_pid])
-              UE_template->ul_total_buffer -= UE_template->TBS_UL[harq_pid];
-            else
-              UE_template->ul_total_buffer = 0;
-            LOG_D(MAC,"ul_total_buffer, new %d\n", UE_template->ul_total_buffer);
-            // Cyclic shift for DM RS
-            cshift = 0;// values from 0 to 7 can be used for mapping the cyclic shift (36.211 , Table 5.5.2.1.1-1)
-            // save it for a potential retransmission
-            UE_template->cshift[harq_pid] = cshift;
-
-            hi_dci0_pdu                                                         = &hi_dci0_req->hi_dci0_pdu_list[eNB->HI_DCI0_req[CC_id].hi_dci0_request_body.number_of_dci+eNB->HI_DCI0_req[CC_id].hi_dci0_request_body.number_of_hi];
-            memset((void*)hi_dci0_pdu,0,sizeof(nfapi_hi_dci0_request_pdu_t));
-            hi_dci0_pdu->pdu_type                                               = NFAPI_HI_DCI0_DCI_PDU_TYPE;
-            hi_dci0_pdu->pdu_size                                               = 2+sizeof(nfapi_hi_dci0_dci_pdu);
-            hi_dci0_pdu->dci_pdu.dci_pdu_rel8.dci_format                        = NFAPI_UL_DCI_FORMAT_0;
-            hi_dci0_pdu->dci_pdu.dci_pdu_rel8.aggregation_level                 = aggregation;
-            hi_dci0_pdu->dci_pdu.dci_pdu_rel8.rnti                              = rnti;
-            hi_dci0_pdu->dci_pdu.dci_pdu_rel8.transmission_power                = 6000;
-            hi_dci0_pdu->dci_pdu.dci_pdu_rel8.resource_block_start              = first_rb[CC_id];
-            hi_dci0_pdu->dci_pdu.dci_pdu_rel8.number_of_resource_block          = rb_table[rb_table_index];
-            hi_dci0_pdu->dci_pdu.dci_pdu_rel8.mcs_1                             = UE_template->mcs_UL[harq_pid];
-            hi_dci0_pdu->dci_pdu.dci_pdu_rel8.cyclic_shift_2_for_drms           = cshift;
-            hi_dci0_pdu->dci_pdu.dci_pdu_rel8.frequency_hopping_enabled_flag    = 0;
-            hi_dci0_pdu->dci_pdu.dci_pdu_rel8.new_data_indication_1             = ndi;
-            hi_dci0_pdu->dci_pdu.dci_pdu_rel8.tpc                               = tpc;
-            hi_dci0_pdu->dci_pdu.dci_pdu_rel8.cqi_csi_request                   = cqi_req;
-            hi_dci0_pdu->dci_pdu.dci_pdu_rel8.dl_assignment_index               = UE_template->DAI_ul[sched_subframeP];
-
-            eNB->HI_DCI0_req[CC_id].hi_dci0_request_body.number_of_dci++;
-
-            LOG_D(MAC,"[PUSCH %d] Frame %d, Subframe %d: Adding UL CONFIG.Request for UE %d/%x, ulsch_frame %d, ulsch_subframe %d\n",
-                  harq_pid,frameP,subframeP,UE_id,rnti,sched_frame,sched_subframeP);
-
-            // Add UL_config PDUs
-            fill_nfapi_ulsch_config_request_rel8(&ul_req_tmp->ul_config_pdu_list[ul_req_tmp->number_of_pdus],
-                                                 cqi_req,
-                                                 cc,
-                                                 UE_template->physicalConfigDedicated,
-                                                 get_tmode(module_idP,CC_id,UE_id),
-                                                 eNB->ul_handle,
-                                                 rnti,
-                                                 first_rb[CC_id], // resource_block_start
-                                                 rb_table[rb_table_index], // number_of_resource_blocks
-                                                 UE_template->mcs_UL[harq_pid],
-                                                 cshift, // cyclic_shift_2_for_drms
-                                                 0, // frequency_hopping_enabled_flag
-                                                 0, // frequency_hopping_bits
-                                                 ndi, // new_data_indication
-                                                 0, // redundancy_version
-                                                 harq_pid, // harq_process_number
-                                                 0, // ul_tx_mode
-                                                 0, // current_tx_nb
-                                                 0, // n_srs
-                                                 get_TBS_UL(UE_template->mcs_UL[harq_pid],
-                                                            rb_table[rb_table_index])
-                                                 );
+	    abort();
+	    /* TODO: this is a hack. Sometimes the UE has no PHY context but
+	     * is still present in the MAC with 'ul_failure_timer' = 0 and
+	     * 'ul_out_of_sync' = 0. It seems wrong and the UE stays there forever. Let's
+	     * start an UL out of sync procedure in this case.
+	     * The root cause of this problem has to be found and corrected.
+	     * In the meantime, this hack...
+	     */
+	    if (UE_list->UE_sched_ctrl[UE_id].ul_failure_timer == 0 &&
+		UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 0) {
+		LOG_W(MAC,
+		      "[eNB %d] frame %d subframe %d, UE %d/%x CC %d: UE in weird state, let's put it 'out of sync'\n",
+		      module_idP, frameP, subframeP, UE_id, rnti, CC_id);
+		// inform RRC of failure and clear timer
+		mac_eNB_rrc_ul_failure(module_idP, CC_id, frameP,
+				       subframeP, rnti);
+		UE_list->UE_sched_ctrl[UE_id].ul_failure_timer = 0;
+		UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync = 1;
+	    }
+	    continue;
+	}
+	// loop over all active UL CC_ids for this UE
+	for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
+	    // This is the actual CC_id in the list
+	    CC_id = UE_list->ordered_ULCCids[n][UE_id];
+	    N_RB_UL = to_prb(cc[CC_id].ul_Bandwidth);
+
+	    /*
+	       aggregation=get_aggregation(get_bw_index(module_idP,CC_id),
+	       eNB_UE_stats->dl_cqi,
+	       format0);
+	     */
+
+	    if (CCE_allocation_infeasible
+		(module_idP, CC_id, 1, subframeP, aggregation, rnti)) {
+		LOG_W(MAC,
+		      "[eNB %d] frame %d subframe %d, UE %d/%x CC %d: not enough nCCE\n",
+		      module_idP, frameP, subframeP, UE_id, rnti, CC_id);
+		continue;	// break;
+	    }
+
+	    /* be sure that there are some free RBs */
+	    if (first_rb[CC_id] >= N_RB_UL - 1) {
+		LOG_W(MAC,
+		      "[eNB %d] frame %d subframe %d, UE %d/%x CC %d: dropping, not enough RBs\n",
+		      module_idP, frameP, subframeP, UE_id, rnti, CC_id);
+		continue;
+	    }
+	    //      if (eNB_UE_stats->mode == PUSCH) { // ue has a ulsch channel
+
+	    UE_template = &UE_list->UE_template[CC_id][UE_id];
+	    UE_sched_ctrl = &UE_list->UE_sched_ctrl[UE_id];
+	    harq_pid =
+		subframe2harqpid(&cc[CC_id], sched_frame, sched_subframeP);
+	    round = UE_sched_ctrl->round_UL[CC_id][harq_pid];
+	    AssertFatal(round < 8, "round %d > 7 for UE %d/%x\n", round,
+			UE_id, rnti);
+	    LOG_D(MAC,
+		  "[eNB %d] frame %d subframe %d,Checking PUSCH %d for UE %d/%x CC %d : aggregation level %d, N_RB_UL %d\n",
+		  module_idP, frameP, subframeP, harq_pid, UE_id, rnti,
+		  CC_id, aggregation, N_RB_UL);
+
+	    RC.eNB[module_idP][CC_id]->pusch_stats_BO[UE_id][(frameP *
+							      10) +
+							     subframeP] =
+		UE_template->ul_total_buffer;
+	    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME
+		(VCD_SIGNAL_DUMPER_VARIABLES_UE0_BO,
+		 RC.eNB[module_idP][CC_id]->pusch_stats_BO[UE_id][(frameP *
+								   10) +
+								  subframeP]);
+	    if (UE_is_to_be_scheduled(module_idP, CC_id, UE_id) > 0 || round > 0)	// || ((frameP%10)==0))
+		// if there is information on bsr of DCCH, DTCH or if there is UL_SR, or if there is a packet to retransmit, or we want to schedule a periodic feedback every 10 frames
+	    {
+		LOG_D(MAC,
+		      "[eNB %d][PUSCH %d] Frame %d subframe %d Scheduling UE %d/%x in round %d(SR %d,UL_inactivity timer %d,UL_failure timer %d,cqi_req_timer %d)\n",
+		      module_idP, harq_pid, frameP, subframeP, UE_id, rnti,
+		      round, UE_template->ul_SR,
+		      UE_sched_ctrl->ul_inactivity_timer,
+		      UE_sched_ctrl->ul_failure_timer,
+		      UE_sched_ctrl->cqi_req_timer);
+		// reset the scheduling request
+		UE_template->ul_SR = 0;
+		status = mac_eNB_get_rrc_status(module_idP, rnti);
+		if (status < RRC_CONNECTED)
+		    cqi_req = 0;
+		else if (UE_sched_ctrl->cqi_req_timer > 30) {
+		    cqi_req = 1;
+		    UE_sched_ctrl->cqi_req_timer = 0;
+		} else
+		    cqi_req = 0;
+
+		//power control
+		//compute the expected ULSCH RX power (for the stats)
+
+		// this is the normalized RX power and this should be constant (regardless of mcs
+		normalized_rx_power = UE_sched_ctrl->pusch_snr[CC_id];
+		target_rx_power = 178;
+
+		// this assumes accumulated tpc
+		// make sure that we are only sending a tpc update once a frame, otherwise the control loop will freak out
+		int32_t framex10psubframe =
+		    UE_template->pusch_tpc_tx_frame * 10 +
+		    UE_template->pusch_tpc_tx_subframe;
+		if (((framex10psubframe + 10) <= (frameP * 10 + subframeP)) ||	//normal case
+		    ((framex10psubframe > (frameP * 10 + subframeP)) && (((10240 - framex10psubframe + frameP * 10 + subframeP) >= 10))))	//frame wrap-around
+		{
+		    UE_template->pusch_tpc_tx_frame = frameP;
+		    UE_template->pusch_tpc_tx_subframe = subframeP;
+		    if (normalized_rx_power > (target_rx_power + 4)) {
+			tpc = 0;	//-1
+			tpc_accumulated--;
+		    } else if (normalized_rx_power < (target_rx_power - 4)) {
+			tpc = 2;	//+1
+			tpc_accumulated++;
+		    } else {
+			tpc = 1;	//0
+		    }
+		} else {
+		    tpc = 1;	//0
+		}
+		//tpc = 1;
+		if (tpc != 1) {
+		    LOG_D(MAC,
+			  "[eNB %d] ULSCH scheduler: frame %d, subframe %d, harq_pid %d, tpc %d, accumulated %d, normalized/target rx power %d/%d\n",
+			  module_idP, frameP, subframeP, harq_pid, tpc,
+			  tpc_accumulated, normalized_rx_power,
+			  target_rx_power);
+		}
+		// new transmission
+		if (round == 0) {
+
+		    ndi = 1 - UE_template->oldNDI_UL[harq_pid];
+		    UE_template->oldNDI_UL[harq_pid] = ndi;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].
+			normalized_rx_power = normalized_rx_power;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].target_rx_power =
+			target_rx_power;
+		    UE_list->eNB_UE_stats[CC_id][UE_id].ulsch_mcs1 =
+			UE_template->pre_assigned_mcs_ul;
+		    UE_template->mcs_UL[harq_pid] = UE_template->pre_assigned_mcs_ul;	//cmin (UE_template->pre_assigned_mcs_ul, openair_daq_vars.target_ue_ul_mcs); // adjust, based on user-defined MCS
+		    if (UE_template->pre_allocated_rb_table_index_ul >= 0) {
+			rb_table_index =
+			    UE_template->pre_allocated_rb_table_index_ul;
+		    } else {
+			UE_template->mcs_UL[harq_pid] = 10;	//cmin (10, openair_daq_vars.target_ue_ul_mcs);
+			rb_table_index = 5;	// for PHR
+		    }
+
+		    UE_list->eNB_UE_stats[CC_id][UE_id].ulsch_mcs2 =
+			UE_template->mcs_UL[harq_pid];
+		    //            buffer_occupancy = UE_template->ul_total_buffer;
+
+
+		    while (((rb_table[rb_table_index] >
+			     (N_RB_UL - 1 - first_rb[CC_id]))
+			    || (rb_table[rb_table_index] > 45))
+			   && (rb_table_index > 0)) {
+			rb_table_index--;
+		    }
+
+		    UE_template->TBS_UL[harq_pid] =
+			get_TBS_UL(UE_template->mcs_UL[harq_pid],
+				   rb_table[rb_table_index]);
+		    UE_list->eNB_UE_stats[CC_id][UE_id].
+			total_rbs_used_rx += rb_table[rb_table_index];
+		    UE_list->eNB_UE_stats[CC_id][UE_id].ulsch_TBS =
+			UE_template->TBS_UL[harq_pid];
+		    //            buffer_occupancy -= TBS;
+
+		    T(T_ENB_MAC_UE_UL_SCHEDULE, T_INT(module_idP),
+		      T_INT(CC_id), T_INT(rnti), T_INT(frameP),
+		      T_INT(subframeP), T_INT(harq_pid),
+		      T_INT(UE_template->mcs_UL[harq_pid]),
+		      T_INT(first_rb[CC_id]),
+		      T_INT(rb_table[rb_table_index]),
+		      T_INT(UE_template->TBS_UL[harq_pid]), T_INT(ndi));
+
+		    if (mac_eNB_get_rrc_status(module_idP, rnti) <
+			RRC_CONNECTED)
+			LOG_D(MAC,
+			      "[eNB %d][PUSCH %d/%x] CC_id %d Frame %d subframeP %d Scheduled UE %d (mcs %d, first rb %d, nb_rb %d, rb_table_index %d, TBS %d, harq_pid %d)\n",
+			      module_idP, harq_pid, rnti, CC_id, frameP,
+			      subframeP, UE_id,
+			      UE_template->mcs_UL[harq_pid],
+			      first_rb[CC_id], rb_table[rb_table_index],
+			      rb_table_index,
+			      UE_template->TBS_UL[harq_pid], harq_pid);
+
+		    // bad indices : 20 (40 PRB), 21 (45 PRB), 22 (48 PRB)
+		    //store for possible retransmission
+		    UE_template->nb_rb_ul[harq_pid] =
+			rb_table[rb_table_index];
+		    UE_template->first_rb_ul[harq_pid] = first_rb[CC_id];
+
+		    UE_sched_ctrl->ul_scheduled |= (1 << harq_pid);
+		    if (UE_id == UE_list->head)
+			VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME
+			    (VCD_SIGNAL_DUMPER_VARIABLES_UE0_SCHEDULED,
+			     UE_sched_ctrl->ul_scheduled);
+
+		    // adjust total UL buffer status by TBS, wait for UL sdus to do final update
+		    LOG_D(MAC,
+			  "[eNB %d] CC_id %d UE %d/%x : adjusting ul_total_buffer, old %d, TBS %d\n",
+			  module_idP, CC_id, UE_id, rnti,
+			  UE_template->ul_total_buffer,
+			  UE_template->TBS_UL[harq_pid]);
+		    if (UE_template->ul_total_buffer >
+			UE_template->TBS_UL[harq_pid])
+			UE_template->ul_total_buffer -=
+			    UE_template->TBS_UL[harq_pid];
+		    else
+			UE_template->ul_total_buffer = 0;
+		    LOG_D(MAC, "ul_total_buffer, new %d\n",
+			  UE_template->ul_total_buffer);
+		    // Cyclic shift for DM RS
+		    cshift = 0;	// values from 0 to 7 can be used for mapping the cyclic shift (36.211 , Table 5.5.2.1.1-1)
+		    // save it for a potential retransmission
+		    UE_template->cshift[harq_pid] = cshift;
+
+		    hi_dci0_pdu =
+			&hi_dci0_req->hi_dci0_pdu_list[mac->HI_DCI0_req
+						       [CC_id].
+						       hi_dci0_request_body.
+						       number_of_dci +
+						       mac->HI_DCI0_req
+						       [CC_id].
+						       hi_dci0_request_body.
+						       number_of_hi];
+		    memset((void *) hi_dci0_pdu, 0,
+			   sizeof(nfapi_hi_dci0_request_pdu_t));
+		    hi_dci0_pdu->pdu_type = NFAPI_HI_DCI0_DCI_PDU_TYPE;
+		    hi_dci0_pdu->pdu_size =
+			2 + sizeof(nfapi_hi_dci0_dci_pdu);
+		    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.dci_format =
+			NFAPI_UL_DCI_FORMAT_0;
+		    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.aggregation_level =
+			aggregation;
+		    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.rnti = rnti;
+		    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.transmission_power =
+			6000;
+		    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.
+			resource_block_start = first_rb[CC_id];
+		    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.
+			number_of_resource_block =
+			rb_table[rb_table_index];
+		    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.mcs_1 =
+			UE_template->mcs_UL[harq_pid];
+		    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.
+			cyclic_shift_2_for_drms = cshift;
+		    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.
+			frequency_hopping_enabled_flag = 0;
+		    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.
+			new_data_indication_1 = ndi;
+		    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.tpc = tpc;
+		    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.cqi_csi_request =
+			cqi_req;
+		    hi_dci0_pdu->dci_pdu.dci_pdu_rel8.dl_assignment_index =
+			UE_template->DAI_ul[sched_subframeP];
+
+		    mac->HI_DCI0_req[CC_id].hi_dci0_request_body.
+			number_of_dci++;
+
+		    LOG_D(MAC,
+			  "[PUSCH %d] Frame %d, Subframe %d: Adding UL CONFIG.Request for UE %d/%x, ulsch_frame %d, ulsch_subframe %d\n",
+			  harq_pid, frameP, subframeP, UE_id, rnti,
+			  sched_frame, sched_subframeP);
+
+		    // Add UL_config PDUs
+		    fill_nfapi_ulsch_config_request_rel8(&ul_req_tmp->ul_config_pdu_list[ul_req_tmp->number_of_pdus], cqi_req, cc, UE_template->physicalConfigDedicated, get_tmode(module_idP, CC_id, UE_id), mac->ul_handle, rnti, first_rb[CC_id],	// resource_block_start
+							 rb_table[rb_table_index],	// number_of_resource_blocks
+							 UE_template->mcs_UL[harq_pid], cshift,	// cyclic_shift_2_for_drms
+							 0,	// frequency_hopping_enabled_flag
+							 0,	// frequency_hopping_bits
+							 ndi,	// new_data_indication
+							 0,	// redundancy_version
+							 harq_pid,	// harq_process_number
+							 0,	// ul_tx_mode
+							 0,	// current_tx_nb
+							 0,	// n_srs
+							 get_TBS_UL
+							 (UE_template->
+							  mcs_UL[harq_pid],
+							  rb_table
+							  [rb_table_index]));
 #ifdef Rel14
-            if (UE_template->rach_resource_type>0) { // This is a BL/CE UE allocation
-              fill_nfapi_ulsch_config_request_emtc(&ul_req_tmp->ul_config_pdu_list[ul_req_tmp->number_of_pdus],
-                                                   UE_template->rach_resource_type>2 ? 2 : 1,
-                                                   1, //total_number_of_repetitions
-                                                   1, //repetition_number
-                                                   (frameP*10)+subframeP);
-            }
+		    if (UE_template->rach_resource_type > 0) {	// This is a BL/CE UE allocation
+			fill_nfapi_ulsch_config_request_emtc(&ul_req_tmp->ul_config_pdu_list[ul_req_tmp->number_of_pdus], UE_template->rach_resource_type > 2 ? 2 : 1, 1,	//total_number_of_repetitions
+							     1,	//repetition_number
+							     (frameP *
+							      10) +
+							     subframeP);
+		    }
 #endif
-            ul_req_tmp->number_of_pdus++;
-            eNB->ul_handle++;
-
-            add_ue_ulsch_info(module_idP,
-                              CC_id,
-                              UE_id,
-                              subframeP,
-                              S_UL_SCHEDULED);
-
-            LOG_D(MAC,"[eNB %d] CC_id %d Frame %d, subframeP %d: Generated ULSCH DCI for next UE_id %d, format 0\n", module_idP,CC_id,frameP,subframeP,UE_id);
-
-            // increment first rb for next UE allocation
-            first_rb[CC_id]+=rb_table[rb_table_index];
-          }
-          else { // round > 0 => retransmission
-            T(T_ENB_MAC_UE_UL_SCHEDULE_RETRANSMISSION, T_INT(module_idP), T_INT(CC_id), T_INT(rnti), T_INT(frameP),
-              T_INT(subframeP), T_INT(harq_pid), T_INT(UE_template->mcs_UL[harq_pid]), T_INT(first_rb[CC_id]), T_INT(rb_table[rb_table_index]),
-              T_INT(round));
-
-            // fill in NAK information
-
-            hi_dci0_pdu                                                         = &hi_dci0_req->hi_dci0_pdu_list[hi_dci0_req->number_of_dci+hi_dci0_req->number_of_hi];
-            memset((void*)hi_dci0_pdu,0,sizeof(nfapi_hi_dci0_request_pdu_t));
-            hi_dci0_pdu->pdu_type                                               = NFAPI_HI_DCI0_HI_PDU_TYPE;
-            hi_dci0_pdu->pdu_size                                               = 2+sizeof(nfapi_hi_dci0_hi_pdu);
-            hi_dci0_pdu->hi_pdu.hi_pdu_rel8.resource_block_start                = UE_template->first_rb_ul[harq_pid];
-            hi_dci0_pdu->hi_pdu.hi_pdu_rel8.cyclic_shift_2_for_drms             = UE_template->cshift[harq_pid];
-            hi_dci0_pdu->hi_pdu.hi_pdu_rel8.hi_value                            = 0;
-            hi_dci0_req->number_of_hi++;
-            LOG_D(MAC,"[eNB %d][PUSCH %d/%x] CC_id %d Frame %d subframeP %d Scheduled (PHICH) UE %d (mcs %d, first rb %d, nb_rb %d, TBS %d, round %d)\n",
-                  module_idP,harq_pid,rnti,CC_id,frameP,subframeP,UE_id,UE_template->mcs_UL[harq_pid],
-                  UE_template->first_rb_ul[harq_pid], UE_template->nb_rb_ul[harq_pid],
-                  UE_template->TBS_UL[harq_pid],round);
-            // Add UL_config PDUs
-            LOG_D(MAC,"[PUSCH %d] Frame %d, Subframe %d: Adding UL CONFIG.Request for UE %d/%x, ulsch_frame %d, ulsch_subframe %d\n",
-                  harq_pid,frameP,subframeP,UE_id,rnti,sched_frame,sched_subframeP);
-            fill_nfapi_ulsch_config_request_rel8(&ul_req_tmp->ul_config_pdu_list[ul_req_tmp->number_of_pdus],
-                                                 cqi_req,
-                                                 cc,
-                                                 UE_template->physicalConfigDedicated,
-                                                 get_tmode(module_idP,CC_id,UE_id),
-                                                 eNB->ul_handle,
-                                                 rnti,
-                                                 UE_template->first_rb_ul[harq_pid], // resource_block_start
-                                                 UE_template->nb_rb_ul[harq_pid], // number_of_resource_blocks
-                                                 UE_template->mcs_UL[harq_pid],
-                                                 cshift, // cyclic_shift_2_for_drms
-                                                 0, // frequency_hopping_enabled_flag
-                                                 0, // frequency_hopping_bits
-                                                 UE_template->oldNDI_UL[harq_pid], // new_data_indication
-                                                 rvidx_tab[round&3], // redundancy_version
-                                                 harq_pid, // harq_process_number
-                                                 0, // ul_tx_mode
-                                                 0, // current_tx_nb
-                                                 0, // n_srs
-                                                 UE_template->TBS_UL[harq_pid]
-                                                 );
+		    ul_req_tmp->number_of_pdus++;
+		    mac->ul_handle++;
+
+		    add_ue_ulsch_info(module_idP,
+				      CC_id, UE_id, subframeP,
+				      S_UL_SCHEDULED);
+
+		    LOG_D(MAC,
+			  "[eNB %d] CC_id %d Frame %d, subframeP %d: Generated ULSCH DCI for next UE_id %d, format 0\n",
+			  module_idP, CC_id, frameP, subframeP, UE_id);
+
+		    // increment first rb for next UE allocation
+		    first_rb[CC_id] += rb_table[rb_table_index];
+		} else {	// round > 0 => retransmission
+		    T(T_ENB_MAC_UE_UL_SCHEDULE_RETRANSMISSION,
+		      T_INT(module_idP), T_INT(CC_id), T_INT(rnti),
+		      T_INT(frameP), T_INT(subframeP), T_INT(harq_pid),
+		      T_INT(UE_template->mcs_UL[harq_pid]),
+		      T_INT(first_rb[CC_id]),
+		      T_INT(rb_table[rb_table_index]), T_INT(round));
+
+		    // fill in NAK information
+
+		    hi_dci0_pdu =
+			&hi_dci0_req->hi_dci0_pdu_list[hi_dci0_req->
+						       number_of_dci +
+						       hi_dci0_req->
+						       number_of_hi];
+		    memset((void *) hi_dci0_pdu, 0,
+			   sizeof(nfapi_hi_dci0_request_pdu_t));
+		    hi_dci0_pdu->pdu_type = NFAPI_HI_DCI0_HI_PDU_TYPE;
+		    hi_dci0_pdu->pdu_size =
+			2 + sizeof(nfapi_hi_dci0_hi_pdu);
+		    hi_dci0_pdu->hi_pdu.hi_pdu_rel8.resource_block_start =
+			UE_template->first_rb_ul[harq_pid];
+		    hi_dci0_pdu->hi_pdu.hi_pdu_rel8.
+			cyclic_shift_2_for_drms =
+			UE_template->cshift[harq_pid];
+		    hi_dci0_pdu->hi_pdu.hi_pdu_rel8.hi_value = 0;
+		    hi_dci0_req->number_of_hi++;
+		    LOG_D(MAC,
+			  "[eNB %d][PUSCH %d/%x] CC_id %d Frame %d subframeP %d Scheduled (PHICH) UE %d (mcs %d, first rb %d, nb_rb %d, TBS %d, round %d)\n",
+			  module_idP, harq_pid, rnti, CC_id, frameP,
+			  subframeP, UE_id, UE_template->mcs_UL[harq_pid],
+			  UE_template->first_rb_ul[harq_pid],
+			  UE_template->nb_rb_ul[harq_pid],
+			  UE_template->TBS_UL[harq_pid], round);
+		    // Add UL_config PDUs
+		    LOG_D(MAC,
+			  "[PUSCH %d] Frame %d, Subframe %d: Adding UL CONFIG.Request for UE %d/%x, ulsch_frame %d, ulsch_subframe %d\n",
+			  harq_pid, frameP, subframeP, UE_id, rnti,
+			  sched_frame, sched_subframeP);
+		    fill_nfapi_ulsch_config_request_rel8(&ul_req_tmp->ul_config_pdu_list[ul_req_tmp->number_of_pdus], cqi_req, cc, UE_template->physicalConfigDedicated, get_tmode(module_idP, CC_id, UE_id), mac->ul_handle, rnti, UE_template->first_rb_ul[harq_pid],	// resource_block_start
+							 UE_template->nb_rb_ul[harq_pid],	// number_of_resource_blocks
+							 UE_template->mcs_UL[harq_pid], cshift,	// cyclic_shift_2_for_drms
+							 0,	// frequency_hopping_enabled_flag
+							 0,	// frequency_hopping_bits
+							 UE_template->oldNDI_UL[harq_pid],	// new_data_indication
+							 rvidx_tab[round & 3],	// redundancy_version
+							 harq_pid,	// harq_process_number
+							 0,	// ul_tx_mode
+							 0,	// current_tx_nb
+							 0,	// n_srs
+							 UE_template->
+							 TBS_UL[harq_pid]);
 #ifdef Rel14
-            if (UE_template->rach_resource_type>0) { // This is a BL/CE UE allocation
-              fill_nfapi_ulsch_config_request_emtc(&ul_req_tmp->ul_config_pdu_list[ul_req_tmp->number_of_pdus],
-                                                   UE_template->rach_resource_type>2 ? 2 : 1,
-                                                   1, //total_number_of_repetitions
-                                                   1, //repetition_number
-                                                   (frameP*10)+subframeP);
-            }
+		    if (UE_template->rach_resource_type > 0) {	// This is a BL/CE UE allocation
+			fill_nfapi_ulsch_config_request_emtc(&ul_req_tmp->ul_config_pdu_list[ul_req_tmp->number_of_pdus], UE_template->rach_resource_type > 2 ? 2 : 1, 1,	//total_number_of_repetitions
+							     1,	//repetition_number
+							     (frameP *
+							      10) +
+							     subframeP);
+		    }
 #endif
-              ul_req_tmp->number_of_pdus++;
-              eNB->ul_handle++;
-
-          }/*
-          else if (round > 0) { //we schedule a retransmission
-
-            ndi = UE_template->oldNDI_UL[harq_pid];
-
-            if ((round&3)==0) {
-              mcs = openair_daq_vars.target_ue_ul_mcs;
-            } else {
-              mcs = rvidx_tab[round&3] + 28; //not correct for round==4!
-
-            }
-
-            LOG_I(MAC,"[eNB %d][PUSCH %d/%x] CC_id %d Frame %d subframeP %d Scheduled UE retransmission (mcs %d, first rb %d, nb_rb %d, harq_pid %d, round %d)\n",
-                  module_idP,UE_id,rnti,CC_id,frameP,subframeP,mcs,
-                  first_rb[CC_id],UE_template->nb_rb_ul[harq_pid],
-                  harq_pid, round);
-
-            rballoc = mac_xface->computeRIV(frame_parms->N_RB_UL,
-                                            first_rb[CC_id],
-                                            UE_template->nb_rb_ul[harq_pid]);
-            first_rb[CC_id]+=UE_template->nb_rb_ul[harq_pid];  // increment for next UE allocation
-
-            UE_list->eNB_UE_stats[CC_id][UE_id].num_retransmission_rx+=1;
-            UE_list->eNB_UE_stats[CC_id][UE_id].rbs_used_retx_rx=UE_template->nb_rb_ul[harq_pid];
-            UE_list->eNB_UE_stats[CC_id][UE_id].total_rbs_used_rx+=UE_template->nb_rb_ul[harq_pid];
-            UE_list->eNB_UE_stats[CC_id][UE_id].ulsch_mcs1=mcs;
-            UE_list->eNB_UE_stats[CC_id][UE_id].ulsch_mcs2=mcs;
-          }
-           */
-
-        } // UE_is_to_be_scheduled
-    } // loop over UE_id
-  } // loop of CC_id
+		    ul_req_tmp->number_of_pdus++;
+		    mac->ul_handle++;
+
+		}		/*
+				   else if (round > 0) { //we schedule a retransmission
+
+				   ndi = UE_template->oldNDI_UL[harq_pid];
+
+				   if ((round&3)==0) {
+				   mcs = openair_daq_vars.target_ue_ul_mcs;
+				   } else {
+				   mcs = rvidx_tab[round&3] + 28; //not correct for round==4!
+
+				   }
+
+				   LOG_I(MAC,"[eNB %d][PUSCH %d/%x] CC_id %d Frame %d subframeP %d Scheduled UE retransmission (mcs %d, first rb %d, nb_rb %d, harq_pid %d, round %d)\n",
+				   module_idP,UE_id,rnti,CC_id,frameP,subframeP,mcs,
+				   first_rb[CC_id],UE_template->nb_rb_ul[harq_pid],
+				   harq_pid, round);
+
+				   rballoc = mac_xface->computeRIV(frame_parms->N_RB_UL,
+				   first_rb[CC_id],
+				   UE_template->nb_rb_ul[harq_pid]);
+				   first_rb[CC_id]+=UE_template->nb_rb_ul[harq_pid];  // increment for next UE allocation
+
+				   UE_list->eNB_UE_stats[CC_id][UE_id].num_retransmission_rx+=1;
+				   UE_list->eNB_UE_stats[CC_id][UE_id].rbs_used_retx_rx=UE_template->nb_rb_ul[harq_pid];
+				   UE_list->eNB_UE_stats[CC_id][UE_id].total_rbs_used_rx+=UE_template->nb_rb_ul[harq_pid];
+				   UE_list->eNB_UE_stats[CC_id][UE_id].ulsch_mcs1=mcs;
+				   UE_list->eNB_UE_stats[CC_id][UE_id].ulsch_mcs2=mcs;
+				   }
+				 */
+
+	    }			// UE_is_to_be_scheduled
+	}			// loop over UE_id
+    }				// loop of CC_id
 }
diff --git a/openair2/LAYER2/MAC/extern.h b/openair2/LAYER2/MAC/extern.h
index f192cd7e127ef01b1ea66adc14c03c4d2217f7a3..50ea5634b93dd13ca57c40875e4fda4d9930e2a7 100644
--- a/openair2/LAYER2/MAC/extern.h
+++ b/openair2/LAYER2/MAC/extern.h
@@ -35,7 +35,7 @@
 
 #ifdef USER_MODE
 //#include "stdio.h"
-#endif //USER_MODE
+#endif				//USER_MODE
 #include "PHY/defs.h"
 #include "defs.h"
 
@@ -56,8 +56,8 @@ extern UE_RRC_INST *UE_rrc_inst;
 extern UE_MAC_INST *UE_mac_inst;
 
 
-extern eNB_ULSCH_INFO eNB_ulsch_info[NUMBER_OF_eNB_MAX][MAX_NUM_CCs][NUMBER_OF_UE_MAX]; // eNBxUE = 8x8
-extern eNB_DLSCH_INFO eNB_dlsch_info[NUMBER_OF_eNB_MAX][MAX_NUM_CCs][NUMBER_OF_UE_MAX]; // eNBxUE = 8x8
+extern eNB_ULSCH_INFO eNB_ulsch_info[NUMBER_OF_eNB_MAX][MAX_NUM_CCs][NUMBER_OF_UE_MAX];	// eNBxUE = 8x8
+extern eNB_DLSCH_INFO eNB_dlsch_info[NUMBER_OF_eNB_MAX][MAX_NUM_CCs][NUMBER_OF_UE_MAX];	// eNBxUE = 8x8
 
 
 
@@ -79,28 +79,26 @@ extern uint32_t RRC_CONNECTION_FLAG;
 
 extern uint8_t rb_table[34];
 
-extern DCI0_5MHz_TDD_1_6_t       UL_alloc_pdu;
+extern DCI0_5MHz_TDD_1_6_t UL_alloc_pdu;
 
-extern DCI1A_5MHz_TDD_1_6_t      RA_alloc_pdu;
-extern DCI1A_5MHz_TDD_1_6_t      DLSCH_alloc_pdu1A;
-extern DCI1A_5MHz_TDD_1_6_t      BCCH_alloc_pdu;
+extern DCI1A_5MHz_TDD_1_6_t RA_alloc_pdu;
+extern DCI1A_5MHz_TDD_1_6_t DLSCH_alloc_pdu1A;
+extern DCI1A_5MHz_TDD_1_6_t BCCH_alloc_pdu;
 
-extern DCI1A_5MHz_TDD_1_6_t      CCCH_alloc_pdu;
-extern DCI1_5MHz_TDD_t           DLSCH_alloc_pdu;
+extern DCI1A_5MHz_TDD_1_6_t CCCH_alloc_pdu;
+extern DCI1_5MHz_TDD_t DLSCH_alloc_pdu;
 
-extern DCI0_5MHz_FDD_t       UL_alloc_pdu_fdd;
+extern DCI0_5MHz_FDD_t UL_alloc_pdu_fdd;
 
-extern DCI1A_5MHz_FDD_t      DLSCH_alloc_pdu1A_fdd;
-extern DCI1A_5MHz_FDD_t      RA_alloc_pdu_fdd;
-extern DCI1A_5MHz_FDD_t      BCCH_alloc_pdu_fdd;
+extern DCI1A_5MHz_FDD_t DLSCH_alloc_pdu1A_fdd;
+extern DCI1A_5MHz_FDD_t RA_alloc_pdu_fdd;
+extern DCI1A_5MHz_FDD_t BCCH_alloc_pdu_fdd;
 
-extern DCI1A_5MHz_FDD_t      CCCH_alloc_pdu_fdd;
-extern DCI1_5MHz_FDD_t       DLSCH_alloc_pdu_fdd;
+extern DCI1A_5MHz_FDD_t CCCH_alloc_pdu_fdd;
+extern DCI1_5MHz_FDD_t DLSCH_alloc_pdu_fdd;
 
 extern DCI2_5MHz_2A_TDD_t DLSCH_alloc_pdu1;
 extern DCI2_5MHz_2A_TDD_t DLSCH_alloc_pdu2;
 extern DCI1E_5MHz_2A_M10PRB_TDD_t DLSCH_alloc_pdu1E;
 
-#endif //DEF_H
-
-
+#endif				//DEF_H
diff --git a/openair2/LAYER2/MAC/flexran_agent_mac_proto.h b/openair2/LAYER2/MAC/flexran_agent_mac_proto.h
index 5b152a1138c80536876d0e83335368aeb90392c9..d3170aaf919e2a24e4599399729b4078e9d6c254 100644
--- a/openair2/LAYER2/MAC/flexran_agent_mac_proto.h
+++ b/openair2/LAYER2/MAC/flexran_agent_mac_proto.h
@@ -17,7 +17,7 @@
  *-------------------------------------------------------------------------------
  * For more information about the OpenAirInterface (OAI) Software Alliance:
  *      contact@openairinterface.org
- */ 
+ */
 
 /*! \file flexran_agent_mac_proto.h
  * \brief MAC functions for FlexRAN agent
@@ -39,150 +39,178 @@
 /*
  * slice specific scheduler 
  */
-typedef void (*slice_scheduler)(module_id_t mod_id, 
-				int slice_id, 
-				uint32_t frame, 
-				uint32_t subframe,
-				int *mbsfn_flag,
-				Protocol__FlexranMessage **dl_info);
+typedef void (*slice_scheduler) (module_id_t mod_id,
+				 int slice_id,
+				 uint32_t frame,
+				 uint32_t subframe,
+				 int *mbsfn_flag,
+				 Protocol__FlexranMessage ** dl_info);
 
 
 
 /*
  * top level flexran scheduler used by the eNB scheduler
  */
-void flexran_schedule_ue_spec_default(mid_t mod_id, 
-				      uint32_t frame, 
+void flexran_schedule_ue_spec_default(mid_t mod_id,
+				      uint32_t frame,
 				      uint32_t subframe,
-				      int *mbsfn_flag, 
-				      Protocol__FlexranMessage **dl_info);
+				      int *mbsfn_flag,
+				      Protocol__FlexranMessage ** dl_info);
 /*
  * slice specific scheduler for embb
  */
 void
-flexran_schedule_ue_spec_embb(mid_t   mod_id,
-			      int       slice_id, 
-			      uint32_t      frame,
-			      uint32_t      subframe,
-			      int           *mbsfn_flag,
-			      Protocol__FlexranMessage **dl_info);
+flexran_schedule_ue_spec_embb(mid_t mod_id,
+			      int slice_id,
+			      uint32_t frame,
+			      uint32_t subframe,
+			      int *mbsfn_flag,
+			      Protocol__FlexranMessage ** dl_info);
 /*
  * slice specific scheduler for urllc
  */
 void
-flexran_schedule_ue_spec_urllc(mid_t   mod_id,
-			      int       slice_id, 
-			      uint32_t      frame,
-			      uint32_t      subframe,
-			      int           *mbsfn_flag,
-			      Protocol__FlexranMessage **dl_info);
+flexran_schedule_ue_spec_urllc(mid_t mod_id,
+			       int slice_id,
+			       uint32_t frame,
+			       uint32_t subframe,
+			       int *mbsfn_flag,
+			       Protocol__FlexranMessage ** dl_info);
 
 /*
  * slice specific scheduler for mmtc
  */
 void
-flexran_schedule_ue_spec_mmtc(mid_t   mod_id,
-			      int       slice_id, 
-			      uint32_t      frame,
-			      uint32_t      subframe,
-			      int           *mbsfn_flag,
-			      Protocol__FlexranMessage **dl_info);
+flexran_schedule_ue_spec_mmtc(mid_t mod_id,
+			      int slice_id,
+			      uint32_t frame,
+			      uint32_t subframe,
+			      int *mbsfn_flag,
+			      Protocol__FlexranMessage ** dl_info);
 /*
  * slice specific scheduler for best effort traffic 
  */
 void
-flexran_schedule_ue_spec_be(mid_t   mod_id,
-			    int       slice_id, 
-			    uint32_t      frame,
-			    uint32_t      subframe,
-			    int           *mbsfn_flag,
-			    Protocol__FlexranMessage **dl_info);
+flexran_schedule_ue_spec_be(mid_t mod_id,
+			    int slice_id,
+			    uint32_t frame,
+			    uint32_t subframe,
+			    int *mbsfn_flag,
+			    Protocol__FlexranMessage ** dl_info);
 
 /*
  * common flexran scheduler function
  */
 void
-flexran_schedule_ue_spec_common(mid_t   mod_id,
-				int       slice_id, 
-				uint32_t      frame,
-				uint32_t      subframe,
-				int           *mbsfn_flag,
-				Protocol__FlexranMessage **dl_info);
-
-uint16_t flexran_nb_rbs_allowed_slice(float rb_percentage, 
-				      int total_rbs);
-
-int flexran_slice_member(int UE_id, 
-			 int slice_id);
-
-int flexran_slice_maxmcs(int slice_id) ;
-
-void _store_dlsch_buffer (module_id_t Mod_id,
-			  int         slice_id,
-			  frame_t     frameP,
-			  sub_frame_t subframeP);
-
-
-void _assign_rbs_required (module_id_t Mod_id,
-			   int         slice_id,
-			   frame_t     frameP,
-			   sub_frame_t subframe,
-			   uint16_t    nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-			   uint16_t    nb_rbs_allowed_slice[MAX_NUM_CCs][MAX_NUM_SLICES], 
-			   int         min_rb_unit[MAX_NUM_CCs]);
-
-void _dlsch_scheduler_pre_processor (module_id_t   Mod_id,
-				     int           slice_id,
-				     frame_t       frameP,
-				     sub_frame_t   subframeP,
-				     int           N_RBG[MAX_NUM_CCs],
-				     int           *mbsfn_flag);
-
-void _dlsch_scheduler_pre_processor_reset (int module_idP,
-					   int UE_id,
-					   uint8_t  CC_id,
-					   int frameP,
-					   int subframeP,					  
-					   int N_RBG,
-					   uint16_t nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-					   uint16_t nb_rbs_required_remaining[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-					   uint16_t nb_rbs_allowed_slice[MAX_NUM_CCs][MAX_NUM_SLICES],
-					   unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX],
-					   unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX]);
-
-void _dlsch_scheduler_pre_processor_allocate (module_id_t   Mod_id,
-					      int           UE_id,
-					      uint8_t       CC_id,
-					      int           N_RBG,
-					      int           transmission_mode,
-					      int           min_rb_unit,
-					      uint8_t       N_RB_DL,
-					      uint16_t      nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-					      uint16_t      nb_rbs_required_remaining[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-					      unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX],
-					      unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX]);
+flexran_schedule_ue_spec_common(mid_t mod_id,
+				int slice_id,
+				uint32_t frame,
+				uint32_t subframe,
+				int *mbsfn_flag,
+				Protocol__FlexranMessage ** dl_info);
+
+uint16_t flexran_nb_rbs_allowed_slice(float rb_percentage, int total_rbs);
+
+int flexran_slice_member(int UE_id, int slice_id);
+
+int flexran_slice_maxmcs(int slice_id);
+
+void _store_dlsch_buffer(module_id_t Mod_id,
+			 int slice_id,
+			 frame_t frameP, sub_frame_t subframeP);
+
+
+void _assign_rbs_required(module_id_t Mod_id,
+			  int slice_id,
+			  frame_t frameP,
+			  sub_frame_t subframe,
+			  uint16_t
+			  nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
+			  uint16_t
+			  nb_rbs_allowed_slice[MAX_NUM_CCs]
+			  [MAX_NUM_SLICES], int min_rb_unit[MAX_NUM_CCs]);
+
+void _dlsch_scheduler_pre_processor(module_id_t Mod_id,
+				    int slice_id,
+				    frame_t frameP,
+				    sub_frame_t subframeP,
+				    int N_RBG[MAX_NUM_CCs],
+				    int *mbsfn_flag);
+
+void _dlsch_scheduler_pre_processor_reset(int module_idP,
+					  int UE_id,
+					  uint8_t CC_id,
+					  int frameP,
+					  int subframeP,
+					  int N_RBG,
+					  uint16_t
+					  nb_rbs_required[MAX_NUM_CCs]
+					  [NUMBER_OF_UE_MAX],
+					  uint16_t
+					  nb_rbs_required_remaining
+					  [MAX_NUM_CCs][NUMBER_OF_UE_MAX],
+					  uint16_t
+					  nb_rbs_allowed_slice[MAX_NUM_CCs]
+					  [MAX_NUM_SLICES],
+					  unsigned char
+					  rballoc_sub[MAX_NUM_CCs]
+					  [N_RBG_MAX],
+					  unsigned char
+					  MIMO_mode_indicator[MAX_NUM_CCs]
+					  [N_RBG_MAX]);
+
+void _dlsch_scheduler_pre_processor_allocate(module_id_t Mod_id,
+					     int UE_id,
+					     uint8_t CC_id,
+					     int N_RBG,
+					     int transmission_mode,
+					     int min_rb_unit,
+					     uint8_t N_RB_DL,
+					     uint16_t
+					     nb_rbs_required[MAX_NUM_CCs]
+					     [NUMBER_OF_UE_MAX],
+					     uint16_t
+					     nb_rbs_required_remaining
+					     [MAX_NUM_CCs]
+					     [NUMBER_OF_UE_MAX],
+					     unsigned char
+					     rballoc_sub[MAX_NUM_CCs]
+					     [N_RBG_MAX],
+					     unsigned char
+					     MIMO_mode_indicator
+					     [MAX_NUM_CCs][N_RBG_MAX]);
 
 /*
  * Default scheduler used by the eNB agent
  */
-void flexran_schedule_ue_spec_default(mid_t mod_id, uint32_t frame, uint32_t subframe,
-				      int *mbsfn_flag, Protocol__FlexranMessage **dl_info);
+void flexran_schedule_ue_spec_default(mid_t mod_id, uint32_t frame,
+				      uint32_t subframe, int *mbsfn_flag,
+				      Protocol__FlexranMessage ** dl_info);
 
 /*
  * Data plane function for applying the DL decisions of the scheduler
  */
-void flexran_apply_dl_scheduling_decisions(mid_t mod_id, uint32_t frame, uint32_t subframe, int *mbsfn_flag,
-					   Protocol__FlexranMessage *dl_scheduling_info);
+void flexran_apply_dl_scheduling_decisions(mid_t mod_id, uint32_t frame,
+					   uint32_t subframe,
+					   int *mbsfn_flag,
+					   Protocol__FlexranMessage *
+					   dl_scheduling_info);
 
 /*
  * Data plane function for applying the UE specific DL decisions of the scheduler
  */
-void flexran_apply_ue_spec_scheduling_decisions(mid_t mod_id, uint32_t frame, uint32_t subframe, int *mbsfn_flag,
-						uint32_t n_dl_ue_data, Protocol__FlexDlData **dl_ue_data);
+void flexran_apply_ue_spec_scheduling_decisions(mid_t mod_id,
+						uint32_t frame,
+						uint32_t subframe,
+						int *mbsfn_flag,
+						uint32_t n_dl_ue_data,
+						Protocol__FlexDlData **
+						dl_ue_data);
 
 /*
  * Data plane function for filling the DCI structure
  */
-void flexran_fill_oai_dci(mid_t mod_id, uint32_t CC_id, uint32_t rnti, Protocol__FlexDlDci *dl_dci);
+void flexran_fill_oai_dci(mid_t mod_id, uint32_t CC_id, uint32_t rnti,
+			  Protocol__FlexDlDci * dl_dci);
 
 #endif
diff --git a/openair2/LAYER2/MAC/flexran_agent_scheduler_dataplane.c b/openair2/LAYER2/MAC/flexran_agent_scheduler_dataplane.c
index 018d490f4430e696e70870383eb72c8b2d18e049..44052a8749191780e5c6c62c39935bfa91081792 100644
--- a/openair2/LAYER2/MAC/flexran_agent_scheduler_dataplane.c
+++ b/openair2/LAYER2/MAC/flexran_agent_scheduler_dataplane.c
@@ -17,7 +17,7 @@
  *-------------------------------------------------------------------------------
  * For more information about the OpenAirInterface (OAI) Software Alliance:
  *      contact@openairinterface.org
- */ 
+ */
 
 /*! \file flexran_agent_scheduler_dataplane.c
  * \brief data plane procedures related to eNB scheduling
@@ -57,495 +57,532 @@
 
 #include "flexran_agent_common.h"
 
-#include "SIMULATION/TOOLS/defs.h" // for taus
+#include "SIMULATION/TOOLS/defs.h"	// for taus
+
+void
+flexran_apply_dl_scheduling_decisions(mid_t mod_id,
+				      uint32_t frame,
+				      uint32_t subframe,
+				      int *mbsfn_flag,
+				      Protocol__FlexranMessage *
+				      dl_scheduling_info)
+{
+
+    Protocol__FlexDlMacConfig *mac_config =
+	dl_scheduling_info->dl_mac_config_msg;
+
+    // Check if there is anything to schedule for random access
+    if (mac_config->n_dl_rar > 0) {
+	/*TODO: call the random access data plane function */
+    }
+    // Check if there is anything to schedule for paging/broadcast
+    if (mac_config->n_dl_broadcast > 0) {
+	/*TODO: call the broadcast/paging data plane function */
+    }
+    // Check if there is anything to schedule for the UEs
+    if (mac_config->n_dl_ue_data > 0) {
+	flexran_apply_ue_spec_scheduling_decisions(mod_id, frame, subframe,
+						   mbsfn_flag,
+						   mac_config->
+						   n_dl_ue_data,
+						   mac_config->dl_ue_data);
+    }
+
+}
+
 
-void flexran_apply_dl_scheduling_decisions(mid_t mod_id,
+void
+flexran_apply_ue_spec_scheduling_decisions(mid_t mod_id,
 					   uint32_t frame,
 					   uint32_t subframe,
 					   int *mbsfn_flag,
-					   Protocol__FlexranMessage *dl_scheduling_info) {
-
-  Protocol__FlexDlMacConfig *mac_config = dl_scheduling_info->dl_mac_config_msg;
-
-  // Check if there is anything to schedule for random access
-  if (mac_config->n_dl_rar > 0) {
-    /*TODO: call the random access data plane function*/
-  }
-
-  // Check if there is anything to schedule for paging/broadcast
-  if (mac_config->n_dl_broadcast > 0) {
-    /*TODO: call the broadcast/paging data plane function*/
-  }
-
-  // Check if there is anything to schedule for the UEs
-  if (mac_config->n_dl_ue_data > 0) {
-    flexran_apply_ue_spec_scheduling_decisions(mod_id, frame, subframe, mbsfn_flag,
-					       mac_config->n_dl_ue_data, mac_config->dl_ue_data);
-  }
-  
-}
+					   uint32_t n_dl_ue_data,
+					   Protocol__FlexDlData **
+					   dl_ue_data)
+{
+
+    uint8_t CC_id;
+    int UE_id;
+    mac_rlc_status_resp_t rlc_status;
+    unsigned char ta_len = 0;
+    unsigned char header_len = 0, header_len_tmp = 0;
+    unsigned char sdu_lcids[11], offset, num_sdus = 0;
+    uint16_t nb_rb;
+    uint16_t TBS, sdu_lengths[11], rnti, padding = 0, post_padding = 0;
+    unsigned char dlsch_buffer[MAX_DLSCH_PAYLOAD_BYTES];
+    uint8_t round = 0;
+    uint8_t harq_pid = 0;
+    //  LTE_DL_FRAME_PARMS   *frame_parms[MAX_NUM_CCs];
+    LTE_eNB_UE_stats *eNB_UE_stats = NULL;
+    uint16_t sdu_length_total = 0;
+    short ta_update = 0;
+    eNB_MAC_INST *eNB = &eNB_mac_inst[mod_id];
+    UE_list_t *UE_list = &eNB->UE_list;
+    //  static int32_t          tpc_accumulated=0;
+    UE_sched_ctrl *ue_sched_ctl;
+
+    int last_sdu_header_len = 0;
+
+    int i, j;
+
+    Protocol__FlexDlData *dl_data;
+    Protocol__FlexDlDci *dl_dci;
+
+    uint32_t rlc_size, n_lc, lcid;
+
+    // For each UE-related command
+    for (i = 0; i < n_dl_ue_data; i++) {
+
+	dl_data = dl_ue_data[i];
+	dl_dci = dl_data->dl_dci;
+
+	CC_id = dl_data->serv_cell_index;
+	//    frame_parms[CC_id] = mac_xface->get_lte_frame_parms(mod_id, CC_id);
+
+	rnti = dl_data->rnti;
+	UE_id = find_ue(rnti, PHY_vars_eNB_g[mod_id][CC_id]);
+
+	ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+	eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
+
+	round = dl_dci->rv[0];
+	harq_pid = dl_dci->harq_process;
+
+	//LOG_I(FLEXRAN_AGENT, "[Frame %d][Subframe %d] Scheduling harq %d\n", frame, subframe, harq_pid);
+	//    LOG_I(FLEXRAN_AGENT, "[Frame %d][Subframe %d]Now scheduling harq_pid %d (round %d)\n", frame, subframe, harq_pid, round);
+
+	// If this is a new transmission
+	if (round == 0) {
+	    // First we have to deal with the creation of the PDU based on the message instructions
+	    rlc_status.bytes_in_buffer = 0;
+
+	    TBS = dl_dci->tbs_size[0];
+
+	    if (dl_data->n_ce_bitmap > 0) {
+		//Check if there is TA command and set the length appropriately
+		ta_len =
+		    (dl_data->
+		     ce_bitmap[0] & PROTOCOL__FLEX_CE_TYPE__FLPCET_TA) ? 2
+		    : 0;
+	    }
+
+	    num_sdus = 0;
+	    sdu_length_total = 0;
+
+	    n_lc = dl_data->n_rlc_pdu;
+	    // Go through each one of the channel commands and create SDUs
+	    header_len = 0;
+	    last_sdu_header_len = 0;
+	    for (j = 0; j < n_lc; j++) {
+		sdu_lengths[j] = 0;
+		lcid =
+		    dl_data->rlc_pdu[j]->rlc_pdu_tb[0]->logical_channel_id;
+		rlc_size = dl_data->rlc_pdu[j]->rlc_pdu_tb[0]->size;
+		LOG_D(MAC,
+		      "[TEST] [eNB %d] [Frame %d] [Subframe %d], LCID %d, CC_id %d, Requesting %d bytes from RLC (RRC message)\n",
+		      mod_id, frame, subframe, lcid, CC_id, rlc_size);
+		if (rlc_size > 0) {
+
+		    rlc_status = mac_rlc_status_ind(mod_id,
+						    rnti,
+						    mod_id,
+						    frame,
+						    subframe,
+						    ENB_FLAG_YES,
+						    MBMS_FLAG_NO, lcid, 0);
+
+		    if (rlc_status.bytes_in_buffer > 0) {
+
+			if (rlc_status.bytes_in_buffer < rlc_size) {
+			    rlc_size = rlc_status.bytes_in_buffer;
+			}
+
+			if (rlc_size <= 2) {
+			    rlc_size = 3;
+			}
+
+			rlc_status = mac_rlc_status_ind(mod_id, rnti, mod_id, frame, subframe, ENB_FLAG_YES, MBMS_FLAG_NO, lcid, rlc_size);	// transport block set size
+
+			LOG_D(MAC,
+			      "[TEST] RLC can give %d bytes for LCID %d during second call\n",
+			      rlc_status.bytes_in_buffer, lcid);
+
+			if (rlc_status.bytes_in_buffer > 0) {
+
+			    sdu_lengths[j] = mac_rlc_data_req(mod_id, rnti, mod_id, frame, ENB_FLAG_YES, MBMS_FLAG_NO, lcid, rlc_size,	//not used
+							      (char *)
+							      &dlsch_buffer
+							      [sdu_length_total]);
+
+			    LOG_D(MAC,
+				  "[eNB %d][LCID %d] CC_id %d Got %d bytes from RLC\n",
+				  mod_id, lcid, CC_id, sdu_lengths[j]);
+			    sdu_length_total += sdu_lengths[j];
+			    sdu_lcids[j] = lcid;
+
+			    UE_list->
+				eNB_UE_stats[CC_id][UE_id].num_pdu_tx[lcid]
+				+= 1;
+			    UE_list->
+				eNB_UE_stats[CC_id][UE_id].num_bytes_tx
+				[lcid] += sdu_lengths[j];
+
+			    if (sdu_lengths[j] < 128) {
+				header_len += 2;
+				last_sdu_header_len = 2;
+			    } else {
+				header_len += 3;
+				last_sdu_header_len = 3;
+			    }
+			    num_sdus++;
+			}
+		    }
+		}
+	    }			// SDU creation end
+
+
+	    if (((sdu_length_total + header_len + ta_len) > 0)) {
+
+		header_len_tmp = header_len;
+
+		// If we have only a single SDU, header length becomes 1
+		if ((num_sdus) == 1) {
+		    //if (header_len == 2 || header_len == 3) {
+		    header_len = 1;
+		} else {
+		    header_len = (header_len - last_sdu_header_len) + 1;
+		}
+
+		// If we need a 1 or 2 bit padding or no padding at all
+		if ((TBS - header_len - sdu_length_total - ta_len) <= 2 || (TBS - header_len - sdu_length_total - ta_len) > TBS) {	//protect from overflow
+		    padding =
+			(TBS - header_len - sdu_length_total - ta_len);
+		    post_padding = 0;
+		} else {	// The last sdu needs to have a length field, since we add padding
+		    padding = 0;
+		    header_len = header_len_tmp;
+		    post_padding = TBS - sdu_length_total - header_len - ta_len;	// 1 is for the postpadding header
+		}
+
+		if (ta_len > 0) {
+		    // Reset the measurement
+		    ta_update = flexran_get_TA(mod_id, UE_id, CC_id);
+		    ue_sched_ctl->ta_timer = 20;
+		    eNB_UE_stats->timing_advance_update = 0;
+		} else {
+		    ta_update = 0;
+		}
+
+		// If there is nothing to schedule, just leave
+		if ((sdu_length_total) <= 0) {
+		    harq_pid_updated[UE_id][harq_pid] = 1;
+		    harq_pid_round[UE_id][harq_pid] = 0;
+		    continue;
+		}
+		//      LOG_I(FLEXRAN_AGENT, "[Frame %d][Subframe %d] TBS is %d and bytes are %d\n", frame, subframe, TBS, sdu_length_total);
+
+		offset = generate_dlsch_header((unsigned char *) UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0], num_sdus,	//num_sdus
+					       sdu_lengths,	//
+					       sdu_lcids, 255,	// no drx
+					       ta_update,	// timing advance
+					       NULL,	// contention res id
+					       padding, post_padding);
+
 
 
-void flexran_apply_ue_spec_scheduling_decisions(mid_t mod_id,
-						uint32_t frame,
-						uint32_t subframe,
-						int *mbsfn_flag,
-						uint32_t n_dl_ue_data,
-						Protocol__FlexDlData **dl_ue_data) {
-
-  uint8_t               CC_id;
-  int                   UE_id;
-  mac_rlc_status_resp_t rlc_status;
-  unsigned char         ta_len=0;
-  unsigned char         header_len = 0, header_len_tmp = 0;
-  unsigned char         sdu_lcids[11],offset,num_sdus=0;
-  uint16_t              nb_rb;
-  uint16_t              TBS, sdu_lengths[11],rnti,padding=0,post_padding=0;
-  unsigned char         dlsch_buffer[MAX_DLSCH_PAYLOAD_BYTES];
-  uint8_t         round            = 0;
-  uint8_t         harq_pid         = 0;
-  //  LTE_DL_FRAME_PARMS   *frame_parms[MAX_NUM_CCs];
-  LTE_eNB_UE_stats     *eNB_UE_stats     = NULL;
-  uint16_t              sdu_length_total = 0;
-  short                 ta_update        = 0;
-  eNB_MAC_INST         *eNB      = &eNB_mac_inst[mod_id];
-  UE_list_t            *UE_list  = &eNB->UE_list;
-  //  static int32_t          tpc_accumulated=0;
-  UE_sched_ctrl           *ue_sched_ctl;
-
-  int last_sdu_header_len = 0;
-
-  int i, j;
-
-  Protocol__FlexDlData *dl_data;
-  Protocol__FlexDlDci *dl_dci;
-
-  uint32_t rlc_size, n_lc, lcid;
-  
-  // For each UE-related command
-  for (i = 0; i < n_dl_ue_data; i++) {
-    
-    dl_data = dl_ue_data[i];
-    dl_dci = dl_data->dl_dci;
-
-    CC_id = dl_data->serv_cell_index;
-    //    frame_parms[CC_id] = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-    
-    rnti = dl_data->rnti;
-    UE_id = find_ue(rnti, PHY_vars_eNB_g[mod_id][CC_id]);
-
-    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-    eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id,CC_id,rnti);
-    
-    round = dl_dci->rv[0];
-    harq_pid = dl_dci->harq_process;
-    
-    //LOG_I(FLEXRAN_AGENT, "[Frame %d][Subframe %d] Scheduling harq %d\n", frame, subframe, harq_pid);
-    //    LOG_I(FLEXRAN_AGENT, "[Frame %d][Subframe %d]Now scheduling harq_pid %d (round %d)\n", frame, subframe, harq_pid, round);
-
-    // If this is a new transmission
-    if (round == 0) {
-      // First we have to deal with the creation of the PDU based on the message instructions
-      rlc_status.bytes_in_buffer = 0;
-      
-      TBS = dl_dci->tbs_size[0];
-      
-      if (dl_data->n_ce_bitmap > 0) {
-	//Check if there is TA command and set the length appropriately
-	ta_len = (dl_data->ce_bitmap[0] & PROTOCOL__FLEX_CE_TYPE__FLPCET_TA) ? 2 : 0; 
-      }
-
-      num_sdus = 0;
-      sdu_length_total = 0;
-
-      n_lc = dl_data->n_rlc_pdu;
-      // Go through each one of the channel commands and create SDUs
-      header_len = 0;
-      last_sdu_header_len = 0;
-      for (j = 0; j < n_lc; j++) {
-	sdu_lengths[j] = 0;
-	lcid = dl_data->rlc_pdu[j]->rlc_pdu_tb[0]->logical_channel_id;
-	rlc_size = dl_data->rlc_pdu[j]->rlc_pdu_tb[0]->size;
-	LOG_D(MAC,"[TEST] [eNB %d] [Frame %d] [Subframe %d], LCID %d, CC_id %d, Requesting %d bytes from RLC (RRC message)\n",
-	      mod_id, frame, subframe, lcid, CC_id, rlc_size);
-	if (rlc_size > 0) {
-	  
-	  rlc_status = mac_rlc_status_ind(mod_id,
-	   				  rnti,
-	   				  mod_id,
-	   				  frame,
-					  subframe,
-	   				  ENB_FLAG_YES,
-	   				  MBMS_FLAG_NO,
-	   				  lcid,
-	   				  0);
-
-	  if (rlc_status.bytes_in_buffer > 0) {
-
-	    if (rlc_status.bytes_in_buffer < rlc_size) {
-	      rlc_size = rlc_status.bytes_in_buffer;
-	    }
 
-	    if (rlc_size <= 2) { 
-	      rlc_size = 3; 
-	    } 
-
-	    rlc_status = mac_rlc_status_ind(mod_id,
-					    rnti,
-					    mod_id,
-					    frame,
-						subframe,
-					    ENB_FLAG_YES,
-					    MBMS_FLAG_NO,
-					    lcid,
-					    rlc_size); // transport block set size
-	  
-	    LOG_D(MAC, "[TEST] RLC can give %d bytes for LCID %d during second call\n", rlc_status.bytes_in_buffer, lcid);
-	  
-	    if (rlc_status.bytes_in_buffer > 0) {
-	      
-	      sdu_lengths[j] = mac_rlc_data_req(mod_id,
-						rnti,
-						mod_id,
-						frame,
-						ENB_FLAG_YES,
-						MBMS_FLAG_NO,
-						lcid,
-						rlc_size, //not used
-						(char *)&dlsch_buffer[sdu_length_total]);
-	      
-	      LOG_D(MAC,"[eNB %d][LCID %d] CC_id %d Got %d bytes from RLC\n",mod_id, lcid, CC_id, sdu_lengths[j]);
-	      sdu_length_total += sdu_lengths[j];
-	      sdu_lcids[j] = lcid;
-	      
-	      UE_list->eNB_UE_stats[CC_id][UE_id].num_pdu_tx[lcid] += 1;
-	      UE_list->eNB_UE_stats[CC_id][UE_id].num_bytes_tx[lcid] += sdu_lengths[j];
-	      
-	      if (sdu_lengths[j] < 128) {
-		header_len += 2;
-		last_sdu_header_len = 2;
-	      } else {
-		header_len += 3;
-		last_sdu_header_len = 3;
-	      }
-	      num_sdus++;
+
+#ifdef DEBUG_eNB_SCHEDULER
+		LOG_T(MAC, "[eNB %d] First 16 bytes of DLSCH : \n");
+
+		for (i = 0; i < 16; i++) {
+		    LOG_T(MAC, "%x.", dlsch_buffer[i]);
+		}
+
+		LOG_T(MAC, "\n");
+#endif
+		// cycle through SDUs and place in dlsch_buffer
+		memcpy(&UE_list->DLSCH_pdu[CC_id][0][UE_id].
+		       payload[0][offset], dlsch_buffer, sdu_length_total);
+		// memcpy(&eNB_mac_inst[0].DLSCH_pdu[0][0].payload[0][offset],dcch_buffer,sdu_lengths[0]);
+
+		// fill remainder of DLSCH with random data
+		for (j = 0; j < (TBS - sdu_length_total - offset); j++) {
+		    UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0][offset +
+								   sdu_length_total
+								   + j] =
+			(char) (taus() & 0xff);
+		}
+
+		//eNB_mac_inst[0].DLSCH_pdu[0][0].payload[0][offset+sdu_lengths[0]+j] = (char)(taus()&0xff);
+		if (opt_enabled == 1) {
+		    trace_pdu(1,
+			      (uint8_t *)
+			      UE_list->DLSCH_pdu[CC_id][0][UE_id].
+			      payload[0], TBS, mod_id, 3, UE_RNTI(mod_id,
+								  UE_id),
+			      eNB->frame, eNB->subframe, 0, 0);
+		    LOG_D(OPT,
+			  "[eNB %d][DLSCH] CC_id %d Frame %d  rnti %x  with size %d\n",
+			  mod_id, CC_id, frame, UE_RNTI(mod_id, UE_id),
+			  TBS);
+		}
+		// store stats
+		eNB->eNB_stats[CC_id].dlsch_bytes_tx += sdu_length_total;
+		eNB->eNB_stats[CC_id].dlsch_pdus_tx += 1;
+		UE_list->eNB_UE_stats[CC_id][UE_id].dl_cqi =
+		    eNB_UE_stats->DL_cqi[0];
+
+		UE_list->eNB_UE_stats[CC_id][UE_id].crnti = rnti;
+		UE_list->eNB_UE_stats[CC_id][UE_id].rrc_status =
+		    mac_eNB_get_rrc_status(mod_id, rnti);
+		UE_list->eNB_UE_stats[CC_id][UE_id].harq_pid = harq_pid;
+		UE_list->eNB_UE_stats[CC_id][UE_id].harq_round = round;
+
+		//nb_rb = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
+		//Find the number of resource blocks and set them to the template for retransmissions
+		nb_rb = get_min_rb_unit(mod_id, CC_id);
+		uint16_t stats_tbs =
+		    mac_xface->get_TBS_DL(dl_dci->mcs[0], nb_rb);
+
+		while (stats_tbs < TBS) {
+		    nb_rb += get_min_rb_unit(mod_id, CC_id);
+		    stats_tbs =
+			mac_xface->get_TBS_DL(dl_dci->mcs[0], nb_rb);
+		}
+
+		//      LOG_I(FLEXRAN_AGENT, "The MCS was %d\n", dl_dci->mcs[0]);
+
+		UE_list->eNB_UE_stats[CC_id][UE_id].rbs_used = nb_rb;
+		UE_list->eNB_UE_stats[CC_id][UE_id].total_rbs_used +=
+		    nb_rb;
+		UE_list->eNB_UE_stats[CC_id][UE_id].dlsch_mcs1 =
+		    dl_dci->mcs[0];
+		UE_list->eNB_UE_stats[CC_id][UE_id].dlsch_mcs2 =
+		    dl_dci->mcs[0];
+		UE_list->eNB_UE_stats[CC_id][UE_id].TBS = TBS;
+
+		UE_list->eNB_UE_stats[CC_id][UE_id].overhead_bytes =
+		    TBS - sdu_length_total;
+		UE_list->eNB_UE_stats[CC_id][UE_id].total_sdu_bytes +=
+		    sdu_length_total;
+		UE_list->eNB_UE_stats[CC_id][UE_id].total_pdu_bytes += TBS;
+		UE_list->eNB_UE_stats[CC_id][UE_id].total_num_pdus += 1;
+
+		//eNB_UE_stats->dlsch_mcs1 = cqi_to_mcs[eNB_UE_stats->DL_cqi[0]];
+		//eNB_UE_stats->dlsch_mcs1 = cmin(eNB_UE_stats->dlsch_mcs1, openair_daq_vars.target_ue_dl_mcs);
+	    } else {
+		LOG_D(FLEXRAN_AGENT,
+		      "No need to schedule a dci after all. Just drop it\n");
+		harq_pid_updated[UE_id][harq_pid] = 1;
+		harq_pid_round[UE_id][harq_pid] = 0;
+		continue;
 	    }
-	  }
-	}
-      } // SDU creation end
-      
-      
-      if (((sdu_length_total + header_len + ta_len) > 0)) {
-	
-	header_len_tmp = header_len;
-	
-	// If we have only a single SDU, header length becomes 1
-	if ((num_sdus) == 1) {
-	  //if (header_len == 2 || header_len == 3) {
-	  header_len = 1;
 	} else {
-	  header_len = (header_len - last_sdu_header_len) + 1;
-	}
-	
-	// If we need a 1 or 2 bit padding or no padding at all
-	if ((TBS - header_len - sdu_length_total - ta_len) <= 2
-	    || (TBS - header_len - sdu_length_total - ta_len) > TBS) { //protect from overflow
-	  padding = (TBS - header_len - sdu_length_total - ta_len);
-	  post_padding = 0;
-	} else { // The last sdu needs to have a length field, since we add padding
-	  padding = 0;
-	  header_len = header_len_tmp;
-	  post_padding = TBS - sdu_length_total - header_len - ta_len; // 1 is for the postpadding header
-	}
-		
-	if (ta_len > 0) {
-	  // Reset the measurement
-	  ta_update = flexran_get_TA(mod_id, UE_id, CC_id);
-	  ue_sched_ctl->ta_timer = 20;
-	  eNB_UE_stats->timing_advance_update = 0;
-	} else {
-	  ta_update = 0;
+	    // No need to create anything apart of DCI in case of retransmission
+	    /*TODO: Must add these */
+	    //      eNB_UE_stats->dlsch_trials[round]++;
+	    //UE_list->eNB_UE_stats[CC_id][UE_id].num_retransmission+=1;
+	    //UE_list->eNB_UE_stats[CC_id][UE_id].rbs_used_retx=nb_rb;
+	    //UE_list->eNB_UE_stats[CC_id][UE_id].total_rbs_used_retx+=nb_rb;
+	    //UE_list->eNB_UE_stats[CC_id][UE_id].ncce_used_retx=nCCECC_id];
 	}
 
-	// If there is nothing to schedule, just leave
-	if ((sdu_length_total) <= 0) { 
-	  harq_pid_updated[UE_id][harq_pid] = 1;
-	  harq_pid_round[UE_id][harq_pid] = 0;
-	  continue;
-	}
+	//    UE_list->UE_template[CC_id][UE_id].oldNDI[dl_dci->harq_process] = dl_dci->ndi[0];
+	//    eNB_UE_stats->dlsch_mcs1 = dl_dci->mcs[0];
 
-	//	LOG_I(FLEXRAN_AGENT, "[Frame %d][Subframe %d] TBS is %d and bytes are %d\n", frame, subframe, TBS, sdu_length_total);
-	
-	offset = generate_dlsch_header((unsigned char*)UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0],
-				       num_sdus,              //num_sdus
-				       sdu_lengths,  //
-				       sdu_lcids,
-				       255,                                   // no drx
-				       ta_update, // timing advance
-				       NULL,                                  // contention res id
-				       padding,
-				       post_padding);
+	//Fill the proper DCI of OAI
+	flexran_fill_oai_dci(mod_id, CC_id, rnti, dl_dci);
+    }
+}
 
-	
+void
+flexran_fill_oai_dci(mid_t mod_id, uint32_t CC_id, uint32_t rnti,
+		     Protocol__FlexDlDci * dl_dci)
+{
 
+    void *DLSCH_dci = NULL;
+    DCI_PDU *DCI_pdu;
 
-	
-#ifdef DEBUG_eNB_SCHEDULER
-	LOG_T(MAC,"[eNB %d] First 16 bytes of DLSCH : \n");
-	
-	for (i=0; i<16; i++) {
-	  LOG_T(MAC,"%x.",dlsch_buffer[i]);
+    unsigned char harq_pid = 0;
+    //  unsigned char round = 0;
+    LTE_DL_FRAME_PARMS *frame_parms[MAX_NUM_CCs];
+    int size_bits = 0, size_bytes = 0;
+    eNB_MAC_INST *eNB = &eNB_mac_inst[mod_id];
+    UE_list_t *UE_list = &eNB->UE_list;
+    LTE_eNB_UE_stats *eNB_UE_stats = NULL;
+
+    int UE_id = find_ue(rnti, PHY_vars_eNB_g[mod_id][CC_id]);
+
+    uint32_t format;
+
+    harq_pid = dl_dci->harq_process;
+    //  round = dl_dci->rv[0];
+
+    // Note this code is for a specific DCI format
+    DLSCH_dci =
+	(void *) UE_list->UE_template[CC_id][UE_id].DLSCH_DCI[harq_pid];
+    DCI_pdu = &eNB->common_channels[CC_id].DCI_pdu;
+
+    frame_parms[CC_id] = mac_xface->get_lte_frame_parms(mod_id, CC_id);
+
+    if (dl_dci->has_tpc == 1) {
+	// Check if tpc has been set and reset measurement */
+	if ((dl_dci->tpc == 0) || (dl_dci->tpc == 2)) {
+	    eNB_UE_stats =
+		mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
+	    eNB_UE_stats->Po_PUCCH_update = 0;
 	}
-	
-	LOG_T(MAC,"\n");
-#endif
-	// cycle through SDUs and place in dlsch_buffer
-	memcpy(&UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0][offset],dlsch_buffer,sdu_length_total);
-	// memcpy(&eNB_mac_inst[0].DLSCH_pdu[0][0].payload[0][offset],dcch_buffer,sdu_lengths[0]);
-	
-	// fill remainder of DLSCH with random data
-	for (j=0; j<(TBS-sdu_length_total-offset); j++) {
-	  UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0][offset+sdu_length_total+j] = (char)(taus()&0xff);
+    }
+
+
+    switch (frame_parms[CC_id]->N_RB_DL) {
+    case 6:
+	if (frame_parms[CC_id]->frame_type == TDD) {
+	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
+		FILL_DCI_TDD_1(DCI1_1_5MHz_TDD_t, DLSCH_dci, dl_dci);
+		size_bytes = sizeof(DCI1_1_5MHz_TDD_t);
+		size_bits = sizeof_DCI1_1_5MHz_TDD_t;
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
+		//TODO
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
+		//TODO
+	    }
+	} else {
+	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
+		FILL_DCI_FDD_1(DCI1_1_5MHz_FDD_t, DLSCH_dci, dl_dci);
+		size_bytes = sizeof(DCI1_1_5MHz_FDD_t);
+		size_bits = sizeof_DCI1_1_5MHz_FDD_t;
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
+		//TODO
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
+		//TODO
+	    }
+	}
+	break;
+    case 25:
+	if (frame_parms[CC_id]->frame_type == TDD) {
+	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
+		FILL_DCI_TDD_1(DCI1_5MHz_TDD_t, DLSCH_dci, dl_dci);
+		size_bytes = sizeof(DCI1_5MHz_TDD_t);
+		size_bits = sizeof_DCI1_5MHz_TDD_t;
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
+		//TODO
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
+		//TODO
+	    }
+	} else {
+	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
+		FILL_DCI_FDD_1(DCI1_5MHz_FDD_t, DLSCH_dci, dl_dci);
+		size_bytes = sizeof(DCI1_5MHz_FDD_t);
+		size_bits = sizeof_DCI1_5MHz_FDD_t;
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
+		//TODO
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
+		//TODO
+	    }
 	}
-	
-	//eNB_mac_inst[0].DLSCH_pdu[0][0].payload[0][offset+sdu_lengths[0]+j] = (char)(taus()&0xff);
-	if (opt_enabled == 1) {
-	  trace_pdu(1, (uint8_t *)UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0],
-		    TBS, mod_id, 3, UE_RNTI(mod_id, UE_id),
-		    eNB->frame, eNB->subframe,0,0);
-	  LOG_D(OPT,"[eNB %d][DLSCH] CC_id %d Frame %d  rnti %x  with size %d\n",
-		mod_id, CC_id, frame, UE_RNTI(mod_id,UE_id), TBS);
+	break;
+    case 50:
+	if (frame_parms[CC_id]->frame_type == TDD) {
+	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
+		FILL_DCI_TDD_1(DCI1_10MHz_TDD_t, DLSCH_dci, dl_dci);
+		size_bytes = sizeof(DCI1_10MHz_TDD_t);
+		size_bits = sizeof_DCI1_10MHz_TDD_t;
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
+		//TODO
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
+		//TODO
+	    }
+	} else {
+	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
+		FILL_DCI_FDD_1(DCI1_10MHz_FDD_t, DLSCH_dci, dl_dci);
+		size_bytes = sizeof(DCI1_10MHz_FDD_t);
+		size_bits = sizeof_DCI1_10MHz_FDD_t;
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
+		//TODO
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
+		//TODO
+	    }
 	}
-	
-	// store stats
-	eNB->eNB_stats[CC_id].dlsch_bytes_tx+=sdu_length_total;
-	eNB->eNB_stats[CC_id].dlsch_pdus_tx+=1;
-	UE_list->eNB_UE_stats[CC_id][UE_id].dl_cqi= eNB_UE_stats->DL_cqi[0];
-	
-	UE_list->eNB_UE_stats[CC_id][UE_id].crnti= rnti;
-	UE_list->eNB_UE_stats[CC_id][UE_id].rrc_status=mac_eNB_get_rrc_status(mod_id, rnti);
-	UE_list->eNB_UE_stats[CC_id][UE_id].harq_pid = harq_pid; 
-	UE_list->eNB_UE_stats[CC_id][UE_id].harq_round = round;
-	
-	//nb_rb = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
-	//Find the number of resource blocks and set them to the template for retransmissions
-	nb_rb = get_min_rb_unit(mod_id, CC_id);
-	uint16_t stats_tbs = mac_xface->get_TBS_DL(dl_dci->mcs[0], nb_rb);
-
-	while (stats_tbs < TBS) {
-	  nb_rb += get_min_rb_unit(mod_id, CC_id);
-	  stats_tbs = mac_xface->get_TBS_DL(dl_dci->mcs[0], nb_rb);
+	break;
+    case 100:
+	if (frame_parms[CC_id]->frame_type == TDD) {
+	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
+		FILL_DCI_TDD_1(DCI1_20MHz_TDD_t, DLSCH_dci, dl_dci);
+		size_bytes = sizeof(DCI1_20MHz_TDD_t);
+		size_bits = sizeof_DCI1_20MHz_TDD_t;
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
+		//TODO
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
+		//TODO
+	    }
+	} else {
+	    if (dl_dci->format == PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
+		FILL_DCI_FDD_1(DCI1_20MHz_FDD_t, DLSCH_dci, dl_dci);
+		size_bytes = sizeof(DCI1_20MHz_FDD_t);
+		size_bits = sizeof_DCI1_20MHz_FDD_t;
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
+		//TODO
+	    } else if (dl_dci->format ==
+		       PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
+		//TODO
+	    }
 	}
-
-	//	LOG_I(FLEXRAN_AGENT, "The MCS was %d\n", dl_dci->mcs[0]);
-	
-	UE_list->eNB_UE_stats[CC_id][UE_id].rbs_used = nb_rb;
-	UE_list->eNB_UE_stats[CC_id][UE_id].total_rbs_used += nb_rb;
-	UE_list->eNB_UE_stats[CC_id][UE_id].dlsch_mcs1=dl_dci->mcs[0];
-	UE_list->eNB_UE_stats[CC_id][UE_id].dlsch_mcs2=dl_dci->mcs[0];
-	UE_list->eNB_UE_stats[CC_id][UE_id].TBS = TBS;
-	
-	UE_list->eNB_UE_stats[CC_id][UE_id].overhead_bytes= TBS - sdu_length_total;
-	UE_list->eNB_UE_stats[CC_id][UE_id].total_sdu_bytes+= sdu_length_total;
-	UE_list->eNB_UE_stats[CC_id][UE_id].total_pdu_bytes+= TBS;
-	UE_list->eNB_UE_stats[CC_id][UE_id].total_num_pdus+=1;
-	
-	//eNB_UE_stats->dlsch_mcs1 = cqi_to_mcs[eNB_UE_stats->DL_cqi[0]];
-	//eNB_UE_stats->dlsch_mcs1 = cmin(eNB_UE_stats->dlsch_mcs1, openair_daq_vars.target_ue_dl_mcs);
-      } else {
-	LOG_D(FLEXRAN_AGENT, "No need to schedule a dci after all. Just drop it\n");
-	harq_pid_updated[UE_id][harq_pid] = 1;
-	harq_pid_round[UE_id][harq_pid] = 0;
-	continue;
-      }
-    } else {
-      // No need to create anything apart of DCI in case of retransmission
-      /*TODO: Must add these */
-      //      eNB_UE_stats->dlsch_trials[round]++;
-      //UE_list->eNB_UE_stats[CC_id][UE_id].num_retransmission+=1;
-      //UE_list->eNB_UE_stats[CC_id][UE_id].rbs_used_retx=nb_rb;
-      //UE_list->eNB_UE_stats[CC_id][UE_id].total_rbs_used_retx+=nb_rb;
-      //UE_list->eNB_UE_stats[CC_id][UE_id].ncce_used_retx=nCCECC_id];
+	break;
     }
 
-    //    UE_list->UE_template[CC_id][UE_id].oldNDI[dl_dci->harq_process] = dl_dci->ndi[0];
-    //    eNB_UE_stats->dlsch_mcs1 = dl_dci->mcs[0];
-
-    //Fill the proper DCI of OAI
-    flexran_fill_oai_dci(mod_id, CC_id, rnti, dl_dci);
-  }
-}
-void flexran_fill_oai_dci(mid_t mod_id, uint32_t CC_id, uint32_t rnti,
-		  Protocol__FlexDlDci *dl_dci) {
-
-  void         *DLSCH_dci        = NULL;
-  DCI_PDU      *DCI_pdu;
-
-  unsigned char         harq_pid         = 0;
-  //  unsigned char round = 0;
-  LTE_DL_FRAME_PARMS   *frame_parms[MAX_NUM_CCs];
-  int           size_bits = 0, size_bytes = 0;
-  eNB_MAC_INST         *eNB      = &eNB_mac_inst[mod_id];
-  UE_list_t            *UE_list  = &eNB->UE_list;
-  LTE_eNB_UE_stats *eNB_UE_stats = NULL;
-
-  int UE_id = find_ue(rnti, PHY_vars_eNB_g[mod_id][CC_id]);
-
-  uint32_t format;
-
-  harq_pid = dl_dci->harq_process;
-  //  round = dl_dci->rv[0];
-  
-  // Note this code is for a specific DCI format
-  DLSCH_dci = (void *)UE_list->UE_template[CC_id][UE_id].DLSCH_DCI[harq_pid];
-  DCI_pdu = &eNB->common_channels[CC_id].DCI_pdu;
-  
-  frame_parms[CC_id] = mac_xface->get_lte_frame_parms(mod_id, CC_id);
-
-  if (dl_dci->has_tpc == 1) {
-    // Check if tpc has been set and reset measurement */
-    if ((dl_dci->tpc == 0) || (dl_dci->tpc == 2)) {
-      eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
-      eNB_UE_stats->Po_PUCCH_update = 0;
-    }
-  }
-  
-  
-  switch (frame_parms[CC_id]->N_RB_DL) {
-  case 6:
-    if (frame_parms[CC_id]->frame_type == TDD) {
-      if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-	FILL_DCI_TDD_1(DCI1_1_5MHz_TDD_t, DLSCH_dci, dl_dci);
-	size_bytes = sizeof(DCI1_1_5MHz_TDD_t);
-	size_bits  = sizeof_DCI1_1_5MHz_TDD_t;
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-	//TODO
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-	//TODO
-      }
-    } else {
-      if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-	FILL_DCI_FDD_1(DCI1_1_5MHz_FDD_t, DLSCH_dci, dl_dci);
-	size_bytes = sizeof(DCI1_1_5MHz_FDD_t);
-	size_bits  = sizeof_DCI1_1_5MHz_FDD_t;
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-	//TODO
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-	  //TODO
-      }
-    }
-    break;
-  case 25:
-    if (frame_parms[CC_id]->frame_type == TDD) {
-      if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-	FILL_DCI_TDD_1(DCI1_5MHz_TDD_t, DLSCH_dci, dl_dci);
-	size_bytes = sizeof(DCI1_5MHz_TDD_t);
-	size_bits  = sizeof_DCI1_5MHz_TDD_t;
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-	//TODO
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-	//TODO
-      }
-    } else {
-      if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-	FILL_DCI_FDD_1(DCI1_5MHz_FDD_t, DLSCH_dci, dl_dci);
-	size_bytes = sizeof(DCI1_5MHz_FDD_t);
-	size_bits  = sizeof_DCI1_5MHz_FDD_t;
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-	//TODO
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-	//TODO
-      }
+    //Set format to the proper type
+    switch (dl_dci->format) {
+    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1:
+	format = format1;
+	break;
+    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1A:
+	format = format1A;
+	break;
+    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1B:
+	format = format1B;
+	break;
+    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1C:
+	format = format1C;
+	break;
+    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D:
+	format = format1E_2A_M10PRB;
+	break;
+    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2:
+	format = format2;
+	break;
+    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A:
+	format = format2A;
+	break;
+    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2B:
+	format = format2B;
+	break;
+    case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_3:
+	format = 3;
+	break;
+    default:
+	/*TODO: Need to deal with unsupported DCI type */
+	return;
     }
-    break;
-  case 50:
-    if (frame_parms[CC_id]->frame_type == TDD) {
-      if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-	FILL_DCI_TDD_1(DCI1_10MHz_TDD_t, DLSCH_dci, dl_dci);
-	size_bytes = sizeof(DCI1_10MHz_TDD_t);
-	size_bits  = sizeof_DCI1_10MHz_TDD_t;
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-	//TODO
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-	//TODO
-      }
-    } else {
-      if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-	FILL_DCI_FDD_1(DCI1_10MHz_FDD_t, DLSCH_dci, dl_dci);
-	size_bytes = sizeof(DCI1_10MHz_FDD_t);
-	size_bits  = sizeof_DCI1_10MHz_FDD_t;
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-	//TODO
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-	//TODO
-      }
-    }
-    break;
-  case 100:
-    if (frame_parms[CC_id]->frame_type == TDD) {
-      if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-	FILL_DCI_TDD_1(DCI1_20MHz_TDD_t, DLSCH_dci, dl_dci);
-	size_bytes = sizeof(DCI1_20MHz_TDD_t);
-	size_bits  = sizeof_DCI1_20MHz_TDD_t;
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-	//TODO
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-	//TODO
-      }
-    } else {
-      if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1) {
-	FILL_DCI_FDD_1(DCI1_20MHz_FDD_t, DLSCH_dci, dl_dci);
-	size_bytes = sizeof(DCI1_20MHz_FDD_t);
-	size_bits  = sizeof_DCI1_20MHz_FDD_t;
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A) {
-	//TODO
-      } else if (dl_dci->format ==  PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D) {
-	//TODO
-      }
-    }
-    break;
-  }
-
-  //Set format to the proper type
-  switch(dl_dci->format) {
-  case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1:
-    format = format1;
-    break;
-  case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1A:
-    format = format1A;
-    break;
-  case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1B:
-    format = format1B;
-    break;
-  case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1C:
-    format = format1C;
-    break;
-  case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D:
-    format = format1E_2A_M10PRB;
-    break;
-  case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2:
-    format  = format2;
-    break;
-  case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A:
-    format = format2A;
-    break;
-  case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2B:
-    format = format2B;
-    break;
-  case PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_3:
-    format = 3;
-    break;
-  default:
-    /*TODO: Need to deal with unsupported DCI type*/
-    return;
-  }
-  
-  add_ue_spec_dci(DCI_pdu,
-		  DLSCH_dci,
-		  rnti,
-		  size_bytes,
-		  dl_dci->aggr_level,
-		  size_bits,
-		  format,
-		  0);
+
+    add_ue_spec_dci(DCI_pdu,
+		    DLSCH_dci,
+		    rnti,
+		    size_bytes, dl_dci->aggr_level, size_bits, format, 0);
 }
diff --git a/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue.c b/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue.c
index ee0e7c55829672ba8bf28b62abd77f0903885cdd..3a491bd7ea8488d31953d8084935517a6ab81e19 100644
--- a/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue.c
+++ b/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue.c
@@ -17,7 +17,7 @@
  *-------------------------------------------------------------------------------
  * For more information about the OpenAirInterface (OAI) Software Alliance:
  *      contact@openairinterface.org
- */ 
+ */
 
 /*! \file flexran_agent_scheduler_dlsch_ue.c
  * \brief procedures related to eNB for the DLSCH transport channel
@@ -58,10 +58,10 @@
 #include "flexran_agent_mac.h"
 #include <dlfcn.h>
 
-#include "SIMULATION/TOOLS/defs.h" // for taus
+#include "SIMULATION/TOOLS/defs.h"	// for taus
 
 #if defined(ENABLE_ITTI)
-# include "intertask_interface.h"
+#include "intertask_interface.h"
 #endif
 
 #define ENABLE_MAC_PAYLOAD_DEBUG
@@ -74,14 +74,14 @@
 
 /*!\brief  UE ULSCH scheduling states*/
 typedef enum {
-  MIN_SLICE_STRATEGY = 0,
-  SLICE_MASK,
-  UEID_TO_SLICEID,
-  MAX_SLICE_STRATEGY
+    MIN_SLICE_STRATEGY = 0,
+    SLICE_MASK,
+    UEID_TO_SLICEID,
+    MAX_SLICE_STRATEGY
 } SLICING_STRATEGY;
 
 // this assumes a max of of 16 UE per eNB/CC
-#define SLICE0_MASK 0x000f 
+#define SLICE0_MASK 0x000f
 #define SLICE1_MASK 0x00f0
 #define SLICE2_MASK 0x0f00
 #define SLICE3_MASK 0xf000
@@ -96,26 +96,28 @@ int slicing_strategy = UEID_TO_SLICEID;
 int slicing_strategy_current = UEID_TO_SLICEID;
 
 // RB share for each slice for past and current time
-float slice_percentage[MAX_NUM_SLICES] = {1.0, 0.0, 0.0, 0.0};
-float slice_percentage_current[MAX_NUM_SLICES] = {1.0, 0.0, 0.0, 0.0};
+float slice_percentage[MAX_NUM_SLICES] = { 1.0, 0.0, 0.0, 0.0 };
+float slice_percentage_current[MAX_NUM_SLICES] = { 1.0, 0.0, 0.0, 0.0 };
+
 float total_slice_percentage = 0;
 
 // MAX MCS for each slice for past and current time
-int slice_maxmcs[MAX_NUM_SLICES] = {28, 28, 28, 28};
-int slice_maxmcs_current[MAX_NUM_SLICES] = {28, 28, 28, 28};
+int slice_maxmcs[MAX_NUM_SLICES] = { 28, 28, 28, 28 };
+int slice_maxmcs_current[MAX_NUM_SLICES] = { 28, 28, 28, 28 };
 
-int update_dl_scheduler[MAX_NUM_SLICES] = {1, 1, 1, 1};
-int update_dl_scheduler_current[MAX_NUM_SLICES] = {1, 1, 1, 1};
+int update_dl_scheduler[MAX_NUM_SLICES] = { 1, 1, 1, 1 };
+int update_dl_scheduler_current[MAX_NUM_SLICES] = { 1, 1, 1, 1 };
 
 // name of available scheduler
-char *dl_scheduler_type[MAX_NUM_SLICES] = {"flexran_schedule_ue_spec_embb",
-					   "flexran_schedule_ue_spec_urllc",
-					   "flexran_schedule_ue_spec_mmtc",
-					   "flexran_schedule_ue_spec_be"      // best effort 
+char *dl_scheduler_type[MAX_NUM_SLICES] =
+    { "flexran_schedule_ue_spec_embb",
+    "flexran_schedule_ue_spec_urllc",
+    "flexran_schedule_ue_spec_mmtc",
+    "flexran_schedule_ue_spec_be"	// best effort 
 };
 
 // pointer to the slice specific scheduler 
-slice_scheduler slice_sched[MAX_NUM_SLICES] = {0};
+slice_scheduler slice_sched[MAX_NUM_SLICES] = { 0 };
 
 
 /**
@@ -125,585 +127,695 @@ slice_scheduler slice_sched[MAX_NUM_SLICES] = {0};
 
 
 // This function stores the downlink buffer for all the logical channels
-void _store_dlsch_buffer (module_id_t Mod_id,
-			  int         slice_id,
-			  frame_t     frameP,
-			  sub_frame_t subframeP)
+void
+_store_dlsch_buffer(module_id_t Mod_id,
+		    int slice_id, frame_t frameP, sub_frame_t subframeP)
 {
 
-  int                   UE_id,i;
-  rnti_t                rnti;
-  mac_rlc_status_resp_t rlc_status;
-  UE_list_t             *UE_list = &eNB_mac_inst[Mod_id].UE_list;
-  UE_TEMPLATE           *UE_template;
-
-  for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
-    if (UE_list->active[UE_id] != TRUE) continue;
-    
-    if (flexran_slice_member(UE_id, slice_id) == 0)
-      continue;
-    
-    UE_template = &UE_list->UE_template[UE_PCCID(Mod_id,UE_id)][UE_id];
-
-    // clear logical channel interface variables
-    UE_template->dl_buffer_total = 0;
-    UE_template->dl_pdus_total = 0;
-
-    for(i=0; i< MAX_NUM_LCID; i++) {
-      UE_template->dl_buffer_info[i]=0;
-      UE_template->dl_pdus_in_buffer[i]=0;
-      UE_template->dl_buffer_head_sdu_creation_time[i]=0;
-      UE_template->dl_buffer_head_sdu_remaining_size_to_send[i]=0;
-    }
+    int UE_id, i;
+    rnti_t rnti;
+    mac_rlc_status_resp_t rlc_status;
+    UE_list_t *UE_list = &eNB_mac_inst[Mod_id].UE_list;
+    UE_TEMPLATE *UE_template;
+
+    for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
+	if (UE_list->active[UE_id] != TRUE)
+	    continue;
+
+	if (flexran_slice_member(UE_id, slice_id) == 0)
+	    continue;
 
-    rnti = UE_RNTI(Mod_id,UE_id);
+	UE_template =
+	    &UE_list->UE_template[UE_PCCID(Mod_id, UE_id)][UE_id];
 
-    for(i=0; i< MAX_NUM_LCID; i++) { // loop over all the logical channels
+	// clear logical channel interface variables
+	UE_template->dl_buffer_total = 0;
+	UE_template->dl_pdus_total = 0;
+
+	for (i = 0; i < MAX_NUM_LCID; i++) {
+	    UE_template->dl_buffer_info[i] = 0;
+	    UE_template->dl_pdus_in_buffer[i] = 0;
+	    UE_template->dl_buffer_head_sdu_creation_time[i] = 0;
+	    UE_template->dl_buffer_head_sdu_remaining_size_to_send[i] = 0;
+	}
 
-      rlc_status = mac_rlc_status_ind(Mod_id,rnti, Mod_id,frameP,subframeP,ENB_FLAG_YES,MBMS_FLAG_NO,i,0 );
-      UE_template->dl_buffer_info[i] = rlc_status.bytes_in_buffer; //storing the dlsch buffer for each logical channel
-      UE_template->dl_pdus_in_buffer[i] = rlc_status.pdus_in_buffer;
-      UE_template->dl_buffer_head_sdu_creation_time[i] = rlc_status.head_sdu_creation_time ;
-      UE_template->dl_buffer_head_sdu_creation_time_max = cmax(UE_template->dl_buffer_head_sdu_creation_time_max,
-          rlc_status.head_sdu_creation_time );
-      UE_template->dl_buffer_head_sdu_remaining_size_to_send[i] = rlc_status.head_sdu_remaining_size_to_send;
-      UE_template->dl_buffer_head_sdu_is_segmented[i] = rlc_status.head_sdu_is_segmented;
-      UE_template->dl_buffer_total += UE_template->dl_buffer_info[i];//storing the total dlsch buffer
-      UE_template->dl_pdus_total   += UE_template->dl_pdus_in_buffer[i];
+	rnti = UE_RNTI(Mod_id, UE_id);
+
+	for (i = 0; i < MAX_NUM_LCID; i++) {	// loop over all the logical channels
+
+	    rlc_status =
+		mac_rlc_status_ind(Mod_id, rnti, Mod_id, frameP, subframeP,
+				   ENB_FLAG_YES, MBMS_FLAG_NO, i, 0);
+	    UE_template->dl_buffer_info[i] = rlc_status.bytes_in_buffer;	//storing the dlsch buffer for each logical channel
+	    UE_template->dl_pdus_in_buffer[i] = rlc_status.pdus_in_buffer;
+	    UE_template->dl_buffer_head_sdu_creation_time[i] =
+		rlc_status.head_sdu_creation_time;
+	    UE_template->dl_buffer_head_sdu_creation_time_max =
+		cmax(UE_template->dl_buffer_head_sdu_creation_time_max,
+		     rlc_status.head_sdu_creation_time);
+	    UE_template->dl_buffer_head_sdu_remaining_size_to_send[i] =
+		rlc_status.head_sdu_remaining_size_to_send;
+	    UE_template->dl_buffer_head_sdu_is_segmented[i] =
+		rlc_status.head_sdu_is_segmented;
+	    UE_template->dl_buffer_total += UE_template->dl_buffer_info[i];	//storing the total dlsch buffer
+	    UE_template->dl_pdus_total +=
+		UE_template->dl_pdus_in_buffer[i];
 
 #ifdef DEBUG_eNB_SCHEDULER
 
-      /* note for dl_buffer_head_sdu_remaining_size_to_send[i] :
-       * 0 if head SDU has not been segmented (yet), else remaining size not already segmented and sent
-       */
-      if (UE_template->dl_buffer_info[i]>0)
-        LOG_D(MAC,
-              "[eNB %d][SLICE %d] Frame %d Subframe %d : RLC status for UE %d in LCID%d: total of %d pdus and size %d, head sdu queuing time %d, remaining size %d, is segmeneted %d \n",
-              Mod_id, slice_id,frameP, subframeP, UE_id,
-              i, UE_template->dl_pdus_in_buffer[i],UE_template->dl_buffer_info[i],
-              UE_template->dl_buffer_head_sdu_creation_time[i],
-              UE_template->dl_buffer_head_sdu_remaining_size_to_send[i],
-              UE_template->dl_buffer_head_sdu_is_segmented[i]
-             );
+	    /* note for dl_buffer_head_sdu_remaining_size_to_send[i] :
+	     * 0 if head SDU has not been segmented (yet), else remaining size not already segmented and sent
+	     */
+	    if (UE_template->dl_buffer_info[i] > 0)
+		LOG_D(MAC,
+		      "[eNB %d][SLICE %d] Frame %d Subframe %d : RLC status for UE %d in LCID%d: total of %d pdus and size %d, head sdu queuing time %d, remaining size %d, is segmeneted %d \n",
+		      Mod_id, slice_id, frameP, subframeP, UE_id,
+		      i, UE_template->dl_pdus_in_buffer[i],
+		      UE_template->dl_buffer_info[i],
+		      UE_template->dl_buffer_head_sdu_creation_time[i],
+		      UE_template->
+		      dl_buffer_head_sdu_remaining_size_to_send[i],
+		      UE_template->dl_buffer_head_sdu_is_segmented[i]);
 
 #endif
 
-    }
+	}
 
-    //#ifdef DEBUG_eNB_SCHEDULER
-    if ( UE_template->dl_buffer_total>0)
-      LOG_D(MAC,"[eNB %d] Frame %d Subframe %d : RLC status for UE %d : total DL buffer size %d and total number of pdu %d \n",
-            Mod_id, frameP, subframeP, UE_id,
-            UE_template->dl_buffer_total,
-            UE_template->dl_pdus_total
-           );
+	//#ifdef DEBUG_eNB_SCHEDULER
+	if (UE_template->dl_buffer_total > 0)
+	    LOG_D(MAC,
+		  "[eNB %d] Frame %d Subframe %d : RLC status for UE %d : total DL buffer size %d and total number of pdu %d \n",
+		  Mod_id, frameP, subframeP, UE_id,
+		  UE_template->dl_buffer_total,
+		  UE_template->dl_pdus_total);
 
-    //#endif
-  }
+	//#endif
+    }
 }
 
 
 // This function returns the estimated number of RBs required by each UE for downlink scheduling
-void _assign_rbs_required (module_id_t Mod_id,
-			   int         slice_id,
-			   frame_t     frameP,
-			   sub_frame_t subframe,
-			   uint16_t    nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-			   uint16_t    nb_rbs_allowed_slice[MAX_NUM_CCs][MAX_NUM_SLICES],
-			   int         min_rb_unit[MAX_NUM_CCs])
+void
+_assign_rbs_required(module_id_t Mod_id,
+		     int slice_id,
+		     frame_t frameP,
+		     sub_frame_t subframe,
+		     uint16_t
+		     nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
+		     uint16_t nb_rbs_allowed_slice[MAX_NUM_CCs]
+		     [MAX_NUM_SLICES], int min_rb_unit[MAX_NUM_CCs])
 {
 
 
-  rnti_t           rnti;
-  uint16_t         TBS = 0;
-  LTE_eNB_UE_stats *eNB_UE_stats[MAX_NUM_CCs];
-  int              UE_id,n,i,j,CC_id,pCCid,tmp;
-  UE_list_t        *UE_list = &eNB_mac_inst[Mod_id].UE_list;
-  //  UE_TEMPLATE           *UE_template;
-
-  // clear rb allocations across all CC_ids
-  for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
-    if (UE_list->active[UE_id] != TRUE) continue;
-    
-    if (flexran_slice_member(UE_id, slice_id) == 0)
-      continue;
-    
-    pCCid = UE_PCCID(Mod_id,UE_id);
-    rnti = UE_list->UE_template[pCCid][UE_id].rnti;
-
-    /* skip UE not present in PHY (for any of its active CCs) */
-    if (!phy_stats_exist(Mod_id, rnti))
-      continue;
-
-    //update CQI information across component carriers
-    for (n=0; n<UE_list->numactiveCCs[UE_id]; n++) {
-      CC_id = UE_list->ordered_CCids[n][UE_id];
-      eNB_UE_stats[CC_id] = mac_xface->get_eNB_UE_stats(Mod_id,CC_id,rnti);
-      eNB_UE_stats[CC_id]->dlsch_mcs1 = cqi_to_mcs[flexran_get_ue_wcqi(Mod_id, UE_id)];
-    }
+    rnti_t rnti;
+    uint16_t TBS = 0;
+    LTE_eNB_UE_stats *eNB_UE_stats[MAX_NUM_CCs];
+    int UE_id, n, i, j, CC_id, pCCid, tmp;
+    UE_list_t *UE_list = &eNB_mac_inst[Mod_id].UE_list;
+    //  UE_TEMPLATE           *UE_template;
 
-    // provide the list of CCs sorted according to MCS
-    for (i=0; i<UE_list->numactiveCCs[UE_id]; i++) {
-      for (j=i+1; j<UE_list->numactiveCCs[UE_id]; j++) {
-        DevAssert( j < MAX_NUM_CCs );
-
-        if (eNB_UE_stats[UE_list->ordered_CCids[i][UE_id]]->dlsch_mcs1 >
-            eNB_UE_stats[UE_list->ordered_CCids[j][UE_id]]->dlsch_mcs1) {
-          tmp = UE_list->ordered_CCids[i][UE_id];
-          UE_list->ordered_CCids[i][UE_id] = UE_list->ordered_CCids[j][UE_id];
-          UE_list->ordered_CCids[j][UE_id] = tmp;
-        }
-      }
-    }
+    // clear rb allocations across all CC_ids
+    for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
+	if (UE_list->active[UE_id] != TRUE)
+	    continue;
+
+	if (flexran_slice_member(UE_id, slice_id) == 0)
+	    continue;
+
+	pCCid = UE_PCCID(Mod_id, UE_id);
+	rnti = UE_list->UE_template[pCCid][UE_id].rnti;
+
+	/* skip UE not present in PHY (for any of its active CCs) */
+	if (!phy_stats_exist(Mod_id, rnti))
+	    continue;
+
+	//update CQI information across component carriers
+	for (n = 0; n < UE_list->numactiveCCs[UE_id]; n++) {
+	    CC_id = UE_list->ordered_CCids[n][UE_id];
+	    eNB_UE_stats[CC_id] =
+		mac_xface->get_eNB_UE_stats(Mod_id, CC_id, rnti);
+	    eNB_UE_stats[CC_id]->dlsch_mcs1 =
+		cqi_to_mcs[flexran_get_ue_wcqi(Mod_id, UE_id)];
+	}
+
+	// provide the list of CCs sorted according to MCS
+	for (i = 0; i < UE_list->numactiveCCs[UE_id]; i++) {
+	    for (j = i + 1; j < UE_list->numactiveCCs[UE_id]; j++) {
+		DevAssert(j < MAX_NUM_CCs);
+
+		if (eNB_UE_stats[UE_list->ordered_CCids[i][UE_id]]->
+		    dlsch_mcs1 >
+		    eNB_UE_stats[UE_list->ordered_CCids[j][UE_id]]->
+		    dlsch_mcs1) {
+		    tmp = UE_list->ordered_CCids[i][UE_id];
+		    UE_list->ordered_CCids[i][UE_id] =
+			UE_list->ordered_CCids[j][UE_id];
+		    UE_list->ordered_CCids[j][UE_id] = tmp;
+		}
+	    }
+	}
+
+	/* NN --> RK
+	 * check the index of UE_template"
+	 */
+	if (UE_list->UE_template[pCCid][UE_id].dl_buffer_total > 0) {
+	    LOG_D(MAC, "[preprocessor] assign RB for UE %d\n", UE_id);
+
+	    for (i = 0; i < UE_list->numactiveCCs[UE_id]; i++) {
+		CC_id = UE_list->ordered_CCids[i][UE_id];
+		eNB_UE_stats[CC_id] =
+		    mac_xface->get_eNB_UE_stats(Mod_id, CC_id, rnti);
+
+		if (cqi_to_mcs[flexran_get_ue_wcqi(Mod_id, UE_id)] == 0) {	//eNB_UE_stats[CC_id]->dlsch_mcs1==0) {
+		    nb_rbs_required[CC_id][UE_id] = 4;	// don't let the TBS get too small
+		} else {
+		    nb_rbs_required[CC_id][UE_id] = min_rb_unit[CC_id];
+		}
 
-    /* NN --> RK
-     * check the index of UE_template"
-     */
-    if (UE_list->UE_template[pCCid][UE_id].dl_buffer_total> 0) {
-      LOG_D(MAC,"[preprocessor] assign RB for UE %d\n",UE_id);
-
-      for (i=0; i<UE_list->numactiveCCs[UE_id]; i++) {
-        CC_id = UE_list->ordered_CCids[i][UE_id];
-	eNB_UE_stats[CC_id] = mac_xface->get_eNB_UE_stats(Mod_id,CC_id,rnti);
-
-        if (cqi_to_mcs[flexran_get_ue_wcqi(Mod_id, UE_id)] == 0) {//eNB_UE_stats[CC_id]->dlsch_mcs1==0) {
-          nb_rbs_required[CC_id][UE_id] = 4;  // don't let the TBS get too small
-        } else {
-          nb_rbs_required[CC_id][UE_id] = min_rb_unit[CC_id];
-        }
-
-        TBS = mac_xface->get_TBS_DL(cqi_to_mcs[flexran_get_ue_wcqi(Mod_id, UE_id)], nb_rbs_required[CC_id][UE_id]);
-	nb_rbs_allowed_slice[CC_id][slice_id] = flexran_nb_rbs_allowed_slice(slice_percentage[slice_id],
-									     flexran_get_N_RB_DL(Mod_id, CC_id));
-        LOG_D(MAC,"[preprocessor] start RB assignement for UE %d CC_id %d dl buffer %d (RB unit %d, MCS %d, TBS %d) \n",
-              UE_id, CC_id, UE_list->UE_template[pCCid][UE_id].dl_buffer_total,
-              nb_rbs_required[CC_id][UE_id], flexran_get_ue_wcqi(Mod_id, UE_id), TBS);
-
-        /* calculating required number of RBs for each UE */
-        while (TBS < UE_list->UE_template[pCCid][UE_id].dl_buffer_total)  {
-          nb_rbs_required[CC_id][UE_id] += min_rb_unit[CC_id];
-
-          if (nb_rbs_required[CC_id][UE_id] > nb_rbs_allowed_slice[CC_id][slice_id]) {
-            TBS = mac_xface->get_TBS_DL(flexran_get_ue_wcqi(Mod_id, UE_id), nb_rbs_allowed_slice[CC_id][slice_id]);
-            nb_rbs_required[CC_id][UE_id] = nb_rbs_allowed_slice[CC_id][slice_id];
-            break;
-          }
-
-          TBS = mac_xface->get_TBS_DL(cqi_to_mcs[flexran_get_ue_wcqi(Mod_id, UE_id)], nb_rbs_required[CC_id][UE_id]);
-        } // end of while
-
-        LOG_D(MAC,"[eNB %d][SLICE %d] Frame %d: UE %d on CC %d: RB unit %d,  nb_required RB %d (TBS %d, mcs %d)\n",
-              Mod_id, slice_id,frameP,UE_id, CC_id,  min_rb_unit[CC_id], nb_rbs_required[CC_id][UE_id], TBS, cqi_to_mcs[flexran_get_ue_wcqi(Mod_id, UE_id)]);
-      }
+		TBS =
+		    mac_xface->get_TBS_DL(cqi_to_mcs
+					  [flexran_get_ue_wcqi
+					   (Mod_id, UE_id)],
+					  nb_rbs_required[CC_id][UE_id]);
+		nb_rbs_allowed_slice[CC_id][slice_id] =
+		    flexran_nb_rbs_allowed_slice(slice_percentage
+						 [slice_id],
+						 flexran_get_N_RB_DL
+						 (Mod_id, CC_id));
+		LOG_D(MAC,
+		      "[preprocessor] start RB assignement for UE %d CC_id %d dl buffer %d (RB unit %d, MCS %d, TBS %d) \n",
+		      UE_id, CC_id,
+		      UE_list->UE_template[pCCid][UE_id].dl_buffer_total,
+		      nb_rbs_required[CC_id][UE_id],
+		      flexran_get_ue_wcqi(Mod_id, UE_id), TBS);
+
+		/* calculating required number of RBs for each UE */
+		while (TBS <
+		       UE_list->UE_template[pCCid][UE_id].
+		       dl_buffer_total) {
+		    nb_rbs_required[CC_id][UE_id] += min_rb_unit[CC_id];
+
+		    if (nb_rbs_required[CC_id][UE_id] >
+			nb_rbs_allowed_slice[CC_id][slice_id]) {
+			TBS =
+			    mac_xface->get_TBS_DL(flexran_get_ue_wcqi
+						  (Mod_id, UE_id),
+						  nb_rbs_allowed_slice
+						  [CC_id]
+						  [slice_id]);
+			nb_rbs_required[CC_id][UE_id] =
+			    nb_rbs_allowed_slice[CC_id][slice_id];
+			break;
+		    }
+
+		    TBS =
+			mac_xface->get_TBS_DL(cqi_to_mcs
+					      [flexran_get_ue_wcqi
+					       (Mod_id, UE_id)],
+					      nb_rbs_required[CC_id]
+					      [UE_id]);
+		}		// end of while
+
+		LOG_D(MAC,
+		      "[eNB %d][SLICE %d] Frame %d: UE %d on CC %d: RB unit %d,  nb_required RB %d (TBS %d, mcs %d)\n",
+		      Mod_id, slice_id, frameP, UE_id, CC_id,
+		      min_rb_unit[CC_id], nb_rbs_required[CC_id][UE_id],
+		      TBS, cqi_to_mcs[flexran_get_ue_wcqi(Mod_id, UE_id)]);
+	    }
+	}
     }
-  }
 }
 
-void _dlsch_scheduler_pre_processor_allocate (module_id_t   Mod_id,
-					      int           UE_id,
-					      uint8_t       CC_id,
-					      int           N_RBG,
-					      int           transmission_mode,
-					      int           min_rb_unit,
-					      uint8_t       N_RB_DL,
-					      uint16_t      nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-					      uint16_t      nb_rbs_required_remaining[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-					      unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX],
-					      unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX]) {
-  int i;
-  UE_list_t *UE_list=&eNB_mac_inst[Mod_id].UE_list;
-  UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-
-  for(i=0; i<N_RBG; i++) {
-
-    if((rballoc_sub[CC_id][i] == 0)           &&
-        (ue_sched_ctl->rballoc_sub_UE[CC_id][i] == 0) &&
-        (nb_rbs_required_remaining[CC_id][UE_id]>0)   &&
-        (ue_sched_ctl->pre_nb_available_rbs[CC_id] < nb_rbs_required[CC_id][UE_id])) {
-
-      // if this UE is not scheduled for TM5
-      if (ue_sched_ctl->dl_pow_off[CC_id] != 0 )  {
-
-	if ((i == N_RBG-1) && ((N_RB_DL == 25) || (N_RB_DL == 50))) {
-	  rballoc_sub[CC_id][i] = 1;
-	  ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 1;
-	  MIMO_mode_indicator[CC_id][i] = 1;
-	  if (transmission_mode == 5 ) {
-	    ue_sched_ctl->dl_pow_off[CC_id] = 1;
-	  }   
-	  nb_rbs_required_remaining[CC_id][UE_id] = nb_rbs_required_remaining[CC_id][UE_id] - min_rb_unit+1;
-          ue_sched_ctl->pre_nb_available_rbs[CC_id] = ue_sched_ctl->pre_nb_available_rbs[CC_id] + min_rb_unit - 1;
-        } else {
-	  if (nb_rbs_required_remaining[CC_id][UE_id] >=  min_rb_unit){
-	    rballoc_sub[CC_id][i] = 1;
-	    ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 1;
-	    MIMO_mode_indicator[CC_id][i] = 1;
-	    if (transmission_mode == 5 ) {
-	      ue_sched_ctl->dl_pow_off[CC_id] = 1;
-	    }
-	    nb_rbs_required_remaining[CC_id][UE_id] = nb_rbs_required_remaining[CC_id][UE_id] - min_rb_unit;
-	    ue_sched_ctl->pre_nb_available_rbs[CC_id] = ue_sched_ctl->pre_nb_available_rbs[CC_id] + min_rb_unit;
-	  }
+void
+_dlsch_scheduler_pre_processor_allocate(module_id_t Mod_id,
+					int UE_id,
+					uint8_t CC_id,
+					int N_RBG,
+					int transmission_mode,
+					int min_rb_unit,
+					uint8_t N_RB_DL,
+					uint16_t
+					nb_rbs_required[MAX_NUM_CCs]
+					[NUMBER_OF_UE_MAX],
+					uint16_t
+					nb_rbs_required_remaining
+					[MAX_NUM_CCs]
+					[NUMBER_OF_UE_MAX], unsigned char
+					rballoc_sub[MAX_NUM_CCs]
+					[N_RBG_MAX], unsigned char
+					MIMO_mode_indicator
+					[MAX_NUM_CCs][N_RBG_MAX])
+{
+    int i;
+    UE_list_t *UE_list = &eNB_mac_inst[Mod_id].UE_list;
+    UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+
+    for (i = 0; i < N_RBG; i++) {
+
+	if ((rballoc_sub[CC_id][i] == 0) &&
+	    (ue_sched_ctl->rballoc_sub_UE[CC_id][i] == 0) &&
+	    (nb_rbs_required_remaining[CC_id][UE_id] > 0) &&
+	    (ue_sched_ctl->pre_nb_available_rbs[CC_id] <
+	     nb_rbs_required[CC_id][UE_id])) {
+
+	    // if this UE is not scheduled for TM5
+	    if (ue_sched_ctl->dl_pow_off[CC_id] != 0) {
+
+		if ((i == N_RBG - 1)
+		    && ((N_RB_DL == 25) || (N_RB_DL == 50))) {
+		    rballoc_sub[CC_id][i] = 1;
+		    ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 1;
+		    MIMO_mode_indicator[CC_id][i] = 1;
+		    if (transmission_mode == 5) {
+			ue_sched_ctl->dl_pow_off[CC_id] = 1;
+		    }
+		    nb_rbs_required_remaining[CC_id][UE_id] =
+			nb_rbs_required_remaining[CC_id][UE_id] -
+			min_rb_unit + 1;
+		    ue_sched_ctl->pre_nb_available_rbs[CC_id] =
+			ue_sched_ctl->pre_nb_available_rbs[CC_id] +
+			min_rb_unit - 1;
+		} else {
+		    if (nb_rbs_required_remaining[CC_id][UE_id] >=
+			min_rb_unit) {
+			rballoc_sub[CC_id][i] = 1;
+			ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 1;
+			MIMO_mode_indicator[CC_id][i] = 1;
+			if (transmission_mode == 5) {
+			    ue_sched_ctl->dl_pow_off[CC_id] = 1;
+			}
+			nb_rbs_required_remaining[CC_id][UE_id] =
+			    nb_rbs_required_remaining[CC_id][UE_id] -
+			    min_rb_unit;
+			ue_sched_ctl->pre_nb_available_rbs[CC_id] =
+			    ue_sched_ctl->pre_nb_available_rbs[CC_id] +
+			    min_rb_unit;
+		    }
+		}
+	    }			// dl_pow_off[CC_id][UE_id] ! = 0
 	}
-      } // dl_pow_off[CC_id][UE_id] ! = 0
     }
-  }
 }
 
-void _dlsch_scheduler_pre_processor_reset (int module_idP,
-					   int UE_id,
-					   uint8_t  CC_id,
-					   int frameP,
-					   int subframeP,					  
-					   int N_RBG,
-					   uint16_t nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-					   uint16_t nb_rbs_required_remaining[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-					   uint16_t nb_rbs_allowed_slice[MAX_NUM_CCs][MAX_NUM_SLICES],
-					   unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX],
-					   unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX]) {
-  int i,j;
-  UE_list_t *UE_list=&eNB_mac_inst[module_idP].UE_list;
-  UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-  uint8_t *vrb_map = eNB_mac_inst[module_idP].common_channels[CC_id].vrb_map;
-  int RBGsize = PHY_vars_eNB_g[module_idP][CC_id]->frame_parms.N_RB_DL/N_RBG;
+void
+_dlsch_scheduler_pre_processor_reset(int module_idP,
+				     int UE_id,
+				     uint8_t CC_id,
+				     int frameP,
+				     int subframeP,
+				     int N_RBG,
+				     uint16_t nb_rbs_required[MAX_NUM_CCs]
+				     [NUMBER_OF_UE_MAX],
+				     uint16_t
+				     nb_rbs_required_remaining
+				     [MAX_NUM_CCs][NUMBER_OF_UE_MAX],
+				     uint16_t
+				     nb_rbs_allowed_slice[MAX_NUM_CCs]
+				     [MAX_NUM_SLICES], unsigned char
+				     rballoc_sub[MAX_NUM_CCs]
+				     [N_RBG_MAX], unsigned char
+				     MIMO_mode_indicator[MAX_NUM_CCs]
+				     [N_RBG_MAX])
+{
+    int i, j;
+    UE_list_t *UE_list = &eNB_mac_inst[module_idP].UE_list;
+    UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+    uint8_t *vrb_map =
+	eNB_mac_inst[module_idP].common_channels[CC_id].vrb_map;
+    int RBGsize =
+	PHY_vars_eNB_g[module_idP][CC_id]->frame_parms.N_RB_DL / N_RBG;
 #ifdef SF05_LIMIT
-  //int subframe05_limit=0;
-  int sf05_upper=-1,sf05_lower=-1;
+    //int subframe05_limit=0;
+    int sf05_upper = -1, sf05_lower = -1;
 #endif
-  //  LTE_eNB_UE_stats *eNB_UE_stats = mac_xface->get_eNB_UE_stats(module_idP,CC_id,rnti);
-  
-  flexran_update_TA(module_idP, UE_id, CC_id);
-
-  if (UE_id==0) {
-    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_TIMING_ADVANCE,ue_sched_ctl->ta_update);
-  }
-  nb_rbs_required[CC_id][UE_id]=0;
-  ue_sched_ctl->pre_nb_available_rbs[CC_id] = 0;
-  ue_sched_ctl->dl_pow_off[CC_id] = 2;
-  nb_rbs_required_remaining[CC_id][UE_id] = 0;
-  for (i=0; i<n_active_slices;i++)
-    nb_rbs_allowed_slice[CC_id][i] = 0;
-#ifdef SF05_LIMIT  
-  switch (N_RBG) {
-  case 6:
-    sf05_lower=0;
-    sf05_upper=5;
-    break;
-  case 8:
-    sf05_lower=2;
-    sf05_upper=5;
-    break;
-  case 13:
-    sf05_lower=4;
-    sf05_upper=7;
-    break;
-  case 17:
-    sf05_lower=7;
-    sf05_upper=9;
-    break;
-  case 25:
-    sf05_lower=11;
-    sf05_upper=13;
-    break;
-  }
+    //  LTE_eNB_UE_stats *eNB_UE_stats = mac_xface->get_eNB_UE_stats(module_idP,CC_id,rnti);
+
+    flexran_update_TA(module_idP, UE_id, CC_id);
+
+    if (UE_id == 0) {
+	VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME
+	    (VCD_SIGNAL_DUMPER_VARIABLES_UE0_TIMING_ADVANCE,
+	     ue_sched_ctl->ta_update);
+    }
+    nb_rbs_required[CC_id][UE_id] = 0;
+    ue_sched_ctl->pre_nb_available_rbs[CC_id] = 0;
+    ue_sched_ctl->dl_pow_off[CC_id] = 2;
+    nb_rbs_required_remaining[CC_id][UE_id] = 0;
+    for (i = 0; i < n_active_slices; i++)
+	nb_rbs_allowed_slice[CC_id][i] = 0;
+#ifdef SF05_LIMIT
+    switch (N_RBG) {
+    case 6:
+	sf05_lower = 0;
+	sf05_upper = 5;
+	break;
+    case 8:
+	sf05_lower = 2;
+	sf05_upper = 5;
+	break;
+    case 13:
+	sf05_lower = 4;
+	sf05_upper = 7;
+	break;
+    case 17:
+	sf05_lower = 7;
+	sf05_upper = 9;
+	break;
+    case 25:
+	sf05_lower = 11;
+	sf05_upper = 13;
+	break;
+    }
 #endif
-  // Initialize Subbands according to VRB map
-  for (i=0; i<N_RBG; i++) {
-    ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 0;
-    rballoc_sub[CC_id][i] = 0;
+    // Initialize Subbands according to VRB map
+    for (i = 0; i < N_RBG; i++) {
+	ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 0;
+	rballoc_sub[CC_id][i] = 0;
 #ifdef SF05_LIMIT
-    // for avoiding 6+ PRBs around DC in subframe 0-5 (avoid excessive errors)
+	// for avoiding 6+ PRBs around DC in subframe 0-5 (avoid excessive errors)
 
-    if ((subframeP==0 || subframeP==5) && 
-	(i>=sf05_lower && i<=sf05_upper))
-      rballoc_sub[CC_id][i]=1;
+	if ((subframeP == 0 || subframeP == 5) &&
+	    (i >= sf05_lower && i <= sf05_upper))
+	    rballoc_sub[CC_id][i] = 1;
 #endif
-    // for SI-RNTI,RA-RNTI and P-RNTI allocations
-    for (j=0;j<RBGsize;j++) {
-      if (vrb_map[j+(i*RBGsize)]!=0)  {
-	rballoc_sub[CC_id][i] = 1;
-	LOG_D(MAC,"Frame %d, subframe %d : vrb %d allocated\n",frameP,subframeP,j+(i*RBGsize));
-	break;
-      }
+	// for SI-RNTI,RA-RNTI and P-RNTI allocations
+	for (j = 0; j < RBGsize; j++) {
+	    if (vrb_map[j + (i * RBGsize)] != 0) {
+		rballoc_sub[CC_id][i] = 1;
+		LOG_D(MAC, "Frame %d, subframe %d : vrb %d allocated\n",
+		      frameP, subframeP, j + (i * RBGsize));
+		break;
+	    }
+	}
+	LOG_D(MAC, "Frame %d Subframe %d CC_id %d RBG %i : rb_alloc %d\n",
+	      frameP, subframeP, CC_id, i, rballoc_sub[CC_id][i]);
+	MIMO_mode_indicator[CC_id][i] = 2;
     }
-    LOG_D(MAC,"Frame %d Subframe %d CC_id %d RBG %i : rb_alloc %d\n",frameP,subframeP,CC_id,i,rballoc_sub[CC_id][i]);
-    MIMO_mode_indicator[CC_id][i] = 2;
-  }
 }
 
 // This function assigns pre-available RBS to each UE in specified sub-bands before scheduling is done
-void _dlsch_scheduler_pre_processor (module_id_t   Mod_id,
-				     int      slice_id,
-				     frame_t       frameP,
-				     sub_frame_t   subframeP,
-				     int           N_RBG[MAX_NUM_CCs],
-				     int           *mbsfn_flag)
+void
+_dlsch_scheduler_pre_processor(module_id_t Mod_id,
+			       int slice_id,
+			       frame_t frameP,
+			       sub_frame_t subframeP,
+			       int N_RBG[MAX_NUM_CCs], int *mbsfn_flag)
 {
 
-  unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX], total_ue_count;
-  unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX];
-  int                     UE_id, i;
-  uint8_t round = 0;
-  uint8_t harq_pid = 0;
-  uint16_t                ii,j;
-  uint16_t                nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
-  uint16_t                nb_rbs_allowed_slice[MAX_NUM_CCs][MAX_NUM_SLICES];
-  uint16_t                nb_rbs_required_remaining[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
-  uint16_t                nb_rbs_required_remaining_1[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
-  uint16_t                average_rbs_per_user[MAX_NUM_CCs] = {0};
-  rnti_t             rnti;
-  int                min_rb_unit[MAX_NUM_CCs];
-  uint16_t r1=0;
-  uint8_t CC_id;
-  UE_list_t *UE_list = &eNB_mac_inst[Mod_id].UE_list;
-  LTE_DL_FRAME_PARMS   *frame_parms[MAX_NUM_CCs] = {0};
-
-  int transmission_mode = 0;
-  UE_sched_ctrl *ue_sched_ctl;
-  
-  
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-    
-    if (mbsfn_flag[CC_id]>0)  // If this CC is allocated for MBSFN skip it here
-      continue;
-    
-    frame_parms[CC_id] = mac_xface->get_lte_frame_parms(Mod_id,CC_id);
-    
-    
-    min_rb_unit[CC_id]=get_min_rb_unit(Mod_id,CC_id);
-    
-    for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
-      if (UE_list->active[i] != TRUE) continue;
-
-      UE_id = i;
-      // Initialize scheduling information for all active UEs
-      
-      //if (flexran_slice_member(UE_id, slice_id) == 0)
-      //continue;
-      _dlsch_scheduler_pre_processor_reset(Mod_id,
-					   UE_id,
-					   CC_id,
-					   frameP,
-					   subframeP,
-					   N_RBG[CC_id],
-					   nb_rbs_required,
-					   nb_rbs_required_remaining,
-					   nb_rbs_allowed_slice, 
-					   rballoc_sub,
-					   MIMO_mode_indicator);
+    unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX], total_ue_count;
+    unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX];
+    int UE_id, i;
+    uint8_t round = 0;
+    uint8_t harq_pid = 0;
+    uint16_t ii, j;
+    uint16_t nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
+    uint16_t nb_rbs_allowed_slice[MAX_NUM_CCs][MAX_NUM_SLICES];
+    uint16_t nb_rbs_required_remaining[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
+    uint16_t nb_rbs_required_remaining_1[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
+    uint16_t average_rbs_per_user[MAX_NUM_CCs] = { 0 };
+    rnti_t rnti;
+    int min_rb_unit[MAX_NUM_CCs];
+    uint16_t r1 = 0;
+    uint8_t CC_id;
+    UE_list_t *UE_list = &eNB_mac_inst[Mod_id].UE_list;
+    LTE_DL_FRAME_PARMS *frame_parms[MAX_NUM_CCs] = { 0 };
+
+    int transmission_mode = 0;
+    UE_sched_ctrl *ue_sched_ctl;
+
+
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+
+	if (mbsfn_flag[CC_id] > 0)	// If this CC is allocated for MBSFN skip it here
+	    continue;
 
+	frame_parms[CC_id] = mac_xface->get_lte_frame_parms(Mod_id, CC_id);
+
+
+	min_rb_unit[CC_id] = get_min_rb_unit(Mod_id, CC_id);
+
+	for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
+	    if (UE_list->active[i] != TRUE)
+		continue;
+
+	    UE_id = i;
+	    // Initialize scheduling information for all active UEs
+
+	    //if (flexran_slice_member(UE_id, slice_id) == 0)
+	    //continue;
+	    _dlsch_scheduler_pre_processor_reset(Mod_id,
+						 UE_id,
+						 CC_id,
+						 frameP,
+						 subframeP,
+						 N_RBG[CC_id],
+						 nb_rbs_required,
+						 nb_rbs_required_remaining,
+						 nb_rbs_allowed_slice,
+						 rballoc_sub,
+						 MIMO_mode_indicator);
+
+	}
     }
-  }
-  
-  // Store the DLSCH buffer for each logical channel
-  _store_dlsch_buffer (Mod_id,slice_id,frameP,subframeP);
-
-  // Calculate the number of RBs required by each UE on the basis of logical channel's buffer
-  _assign_rbs_required (Mod_id,slice_id, frameP,subframeP,nb_rbs_required,nb_rbs_allowed_slice,min_rb_unit);
-
-  // Sorts the user on the basis of dlsch logical channel buffer and CQI
-  sort_UEs (Mod_id,frameP,subframeP);
-
-  total_ue_count = 0;
-
-  // loop over all active UEs
-  for (i=UE_list->head; i>=0; i=UE_list->next[i]) { 
-    rnti = flexran_get_ue_crnti(Mod_id, i);
-    if(rnti == NOT_A_RNTI)
-      continue;
-    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-      continue;
-
-    UE_id = i;
-    
-    if (flexran_slice_member(UE_id, slice_id) == 0)
-      continue;
-    
-    if (!phy_stats_exist(Mod_id, rnti))
-      continue;
-
-    for (ii=0; ii < UE_num_active_CC(UE_list,UE_id); ii++) {
-      CC_id = UE_list->ordered_CCids[ii][UE_id];
-      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-      ue_sched_ctl->max_allowed_rbs[CC_id]=nb_rbs_allowed_slice[CC_id][slice_id];
-      flexran_get_harq(Mod_id, CC_id, UE_id, frameP, subframeP, &harq_pid, &round);
-
-      // if there is no available harq_process, skip the UE
-      if (UE_list->UE_sched_ctrl[UE_id].harq_pid[CC_id]<0)
-        continue;
-
-      average_rbs_per_user[CC_id]=0;
-
-      frame_parms[CC_id] = mac_xface->get_lte_frame_parms(Mod_id,CC_id);
-
-      //      mac_xface->get_ue_active_harq_pid(Mod_id,CC_id,rnti,frameP,subframeP,&harq_pid,&round,0);
-
-      if(round>0) {
-        nb_rbs_required[CC_id][UE_id] = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
-      }
-
-      //nb_rbs_required_remaining[UE_id] = nb_rbs_required[UE_id];
-      if (nb_rbs_required[CC_id][UE_id] > 0) {
-        total_ue_count = total_ue_count + 1;
-      }
-
-
-      // hypotetical assignement
-      /*
-       * If schedule is enabled and if the priority of the UEs is modified
-       * The average rbs per logical channel per user will depend on the level of
-       * priority. Concerning the hypothetical assignement, we should assign more
-       * rbs to prioritized users. Maybe, we can do a mapping between the
-       * average rbs per user and the level of priority or multiply the average rbs
-       * per user by a coefficient which represents the degree of priority.
-       */
-
-      if (total_ue_count == 0) {
-        average_rbs_per_user[CC_id] = 0;
-      } else if( (min_rb_unit[CC_id] * total_ue_count) <= nb_rbs_allowed_slice[CC_id][slice_id] ) {
-        average_rbs_per_user[CC_id] = (uint16_t) floor(nb_rbs_allowed_slice[CC_id][slice_id]/total_ue_count);
-      } else {
-        average_rbs_per_user[CC_id] = min_rb_unit[CC_id]; // consider the total number of use that can be scheduled UE
-      }
-    }
-  }
-
-  // note: nb_rbs_required is assigned according to total_buffer_dl
-  // extend nb_rbs_required to capture per LCID RB required
-  for(i=UE_list->head; i>=0; i=UE_list->next[i]) {
-    rnti = UE_RNTI(Mod_id,i);
-   
-    if(rnti == NOT_A_RNTI)
-      continue;
-
-    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-      continue;
-
-    if (!phy_stats_exist(Mod_id, rnti))
-      continue;
-
-    if (flexran_slice_member(i, slice_id) == 0)
-      continue;
-    
-    for (ii=0; ii<UE_num_active_CC(UE_list,i); ii++) {
-      CC_id = UE_list->ordered_CCids[ii][i];
-
-      // control channel
-      if (mac_eNB_get_rrc_status(Mod_id,rnti) < RRC_RECONFIGURED) {
-        nb_rbs_required_remaining_1[CC_id][i] = nb_rbs_required[CC_id][i];
-      } else {
-        nb_rbs_required_remaining_1[CC_id][i] = cmin(average_rbs_per_user[CC_id],nb_rbs_required[CC_id][i]);
-
-      }
+
+    // Store the DLSCH buffer for each logical channel
+    _store_dlsch_buffer(Mod_id, slice_id, frameP, subframeP);
+
+    // Calculate the number of RBs required by each UE on the basis of logical channel's buffer
+    _assign_rbs_required(Mod_id, slice_id, frameP, subframeP,
+			 nb_rbs_required, nb_rbs_allowed_slice,
+			 min_rb_unit);
+
+    // Sorts the user on the basis of dlsch logical channel buffer and CQI
+    sort_UEs(Mod_id, frameP, subframeP);
+
+    total_ue_count = 0;
+
+    // loop over all active UEs
+    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
+	rnti = flexran_get_ue_crnti(Mod_id, i);
+	if (rnti == NOT_A_RNTI)
+	    continue;
+	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+	    continue;
+
+	UE_id = i;
+
+	if (flexran_slice_member(UE_id, slice_id) == 0)
+	    continue;
+
+	if (!phy_stats_exist(Mod_id, rnti))
+	    continue;
+
+	for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
+	    CC_id = UE_list->ordered_CCids[ii][UE_id];
+	    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+	    ue_sched_ctl->max_allowed_rbs[CC_id] =
+		nb_rbs_allowed_slice[CC_id][slice_id];
+	    flexran_get_harq(Mod_id, CC_id, UE_id, frameP, subframeP,
+			     &harq_pid, &round);
+
+	    // if there is no available harq_process, skip the UE
+	    if (UE_list->UE_sched_ctrl[UE_id].harq_pid[CC_id] < 0)
+		continue;
+
+	    average_rbs_per_user[CC_id] = 0;
+
+	    frame_parms[CC_id] =
+		mac_xface->get_lte_frame_parms(Mod_id, CC_id);
+
+	    //      mac_xface->get_ue_active_harq_pid(Mod_id,CC_id,rnti,frameP,subframeP,&harq_pid,&round,0);
+
+	    if (round > 0) {
+		nb_rbs_required[CC_id][UE_id] =
+		    UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
+	    }
+	    //nb_rbs_required_remaining[UE_id] = nb_rbs_required[UE_id];
+	    if (nb_rbs_required[CC_id][UE_id] > 0) {
+		total_ue_count = total_ue_count + 1;
+	    }
+	    // hypotetical assignement
+	    /*
+	     * If schedule is enabled and if the priority of the UEs is modified
+	     * The average rbs per logical channel per user will depend on the level of
+	     * priority. Concerning the hypothetical assignement, we should assign more
+	     * rbs to prioritized users. Maybe, we can do a mapping between the
+	     * average rbs per user and the level of priority or multiply the average rbs
+	     * per user by a coefficient which represents the degree of priority.
+	     */
+
+	    if (total_ue_count == 0) {
+		average_rbs_per_user[CC_id] = 0;
+	    } else if ((min_rb_unit[CC_id] * total_ue_count) <=
+		       nb_rbs_allowed_slice[CC_id][slice_id]) {
+		average_rbs_per_user[CC_id] =
+		    (uint16_t) floor(nb_rbs_allowed_slice[CC_id][slice_id]
+				     / total_ue_count);
+	    } else {
+		average_rbs_per_user[CC_id] = min_rb_unit[CC_id];	// consider the total number of use that can be scheduled UE
+	    }
+	}
     }
-  }
-
-  //Allocation to UEs is done in 2 rounds,
-  // 1st stage: average number of RBs allocated to each UE
-  // 2nd stage: remaining RBs are allocated to high priority UEs
-  for(r1=0; r1<2; r1++) {
-
-    for(i=UE_list->head; i>=0; i=UE_list->next[i]) {
-      
-      if (flexran_slice_member(i, slice_id) == 0)
-	continue;
-      
-      for (ii=0; ii<UE_num_active_CC(UE_list,i); ii++) {
-        CC_id = UE_list->ordered_CCids[ii][i];
-
-        if(r1 == 0) {
-          nb_rbs_required_remaining[CC_id][i] = nb_rbs_required_remaining_1[CC_id][i];
-        } else { // rb required based only on the buffer - rb allloctaed in the 1st round + extra reaming rb form the 1st round
-          nb_rbs_required_remaining[CC_id][i] = nb_rbs_required[CC_id][i]-nb_rbs_required_remaining_1[CC_id][i]+nb_rbs_required_remaining[CC_id][i];
-        }
-
-        if (nb_rbs_required[CC_id][i]> 0 )
-          LOG_D(MAC,"round %d : nb_rbs_required_remaining[%d][%d]= %d (remaining_1 %d, required %d,  pre_nb_available_rbs %d, N_RBG %d, rb_unit %d)\n",
-                r1, CC_id, i,
-                nb_rbs_required_remaining[CC_id][i],
-                nb_rbs_required_remaining_1[CC_id][i],
-                nb_rbs_required[CC_id][i],
-                UE_list->UE_sched_ctrl[i].pre_nb_available_rbs[CC_id],
-                N_RBG[CC_id],
-                min_rb_unit[CC_id]);
-
-      }
+
+    // note: nb_rbs_required is assigned according to total_buffer_dl
+    // extend nb_rbs_required to capture per LCID RB required
+    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
+	rnti = UE_RNTI(Mod_id, i);
+
+	if (rnti == NOT_A_RNTI)
+	    continue;
+
+	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+	    continue;
+
+	if (!phy_stats_exist(Mod_id, rnti))
+	    continue;
+
+	if (flexran_slice_member(i, slice_id) == 0)
+	    continue;
+
+	for (ii = 0; ii < UE_num_active_CC(UE_list, i); ii++) {
+	    CC_id = UE_list->ordered_CCids[ii][i];
+
+	    // control channel
+	    if (mac_eNB_get_rrc_status(Mod_id, rnti) < RRC_RECONFIGURED) {
+		nb_rbs_required_remaining_1[CC_id][i] =
+		    nb_rbs_required[CC_id][i];
+	    } else {
+		nb_rbs_required_remaining_1[CC_id][i] =
+		    cmin(average_rbs_per_user[CC_id],
+			 nb_rbs_required[CC_id][i]);
+
+	    }
+	}
     }
 
-    if (total_ue_count > 0 ) {
-      for(i=UE_list->head; i>=0; i=UE_list->next[i]) {
-        UE_id = i;
-	
+    //Allocation to UEs is done in 2 rounds,
+    // 1st stage: average number of RBs allocated to each UE
+    // 2nd stage: remaining RBs are allocated to high priority UEs
+    for (r1 = 0; r1 < 2; r1++) {
+
+	for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
+
+	    if (flexran_slice_member(i, slice_id) == 0)
+		continue;
+
+	    for (ii = 0; ii < UE_num_active_CC(UE_list, i); ii++) {
+		CC_id = UE_list->ordered_CCids[ii][i];
+
+		if (r1 == 0) {
+		    nb_rbs_required_remaining[CC_id][i] =
+			nb_rbs_required_remaining_1[CC_id][i];
+		} else {	// rb required based only on the buffer - rb allloctaed in the 1st round + extra reaming rb form the 1st round
+		    nb_rbs_required_remaining[CC_id][i] =
+			nb_rbs_required[CC_id][i] -
+			nb_rbs_required_remaining_1[CC_id][i] +
+			nb_rbs_required_remaining[CC_id][i];
+		}
+
+		if (nb_rbs_required[CC_id][i] > 0)
+		    LOG_D(MAC,
+			  "round %d : nb_rbs_required_remaining[%d][%d]= %d (remaining_1 %d, required %d,  pre_nb_available_rbs %d, N_RBG %d, rb_unit %d)\n",
+			  r1, CC_id, i,
+			  nb_rbs_required_remaining[CC_id][i],
+			  nb_rbs_required_remaining_1[CC_id][i],
+			  nb_rbs_required[CC_id][i],
+			  UE_list->UE_sched_ctrl[i].
+			  pre_nb_available_rbs[CC_id], N_RBG[CC_id],
+			  min_rb_unit[CC_id]);
+
+	    }
+	}
+
+	if (total_ue_count > 0) {
+	    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
+		UE_id = i;
+
+		if (flexran_slice_member(UE_id, slice_id) == 0)
+		    continue;
+
+		for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
+		    CC_id = UE_list->ordered_CCids[ii][UE_id];
+		    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+		    flexran_get_harq(Mod_id, CC_id, UE_id, frameP,
+				     subframeP, &harq_pid, &round);
+		    rnti = UE_RNTI(Mod_id, UE_id);
+
+		    // LOG_D(MAC,"UE %d rnti 0x\n", UE_id, rnti );
+		    if (rnti == NOT_A_RNTI)
+			continue;
+
+		    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1)
+			continue;
+
+		    if (!phy_stats_exist(Mod_id, rnti))
+			continue;
+
+		    transmission_mode =
+			mac_xface->get_transmission_mode(Mod_id, CC_id,
+							 rnti);
+		    //rrc_status = mac_eNB_get_rrc_status(Mod_id,rnti);
+		    /* 1st allocate for the retx */
+
+		    // retransmission in data channels
+		    // control channel in the 1st transmission
+		    // data channel for all TM
+		    LOG_T(MAC,
+			  "calling dlsch_scheduler_pre_processor_allocate .. \n ");
+		    _dlsch_scheduler_pre_processor_allocate(Mod_id, UE_id,
+							    CC_id,
+							    N_RBG[CC_id],
+							    transmission_mode,
+							    min_rb_unit
+							    [CC_id],
+							    frame_parms
+							    [CC_id]->
+							    N_RB_DL,
+							    nb_rbs_required,
+							    nb_rbs_required_remaining,
+							    rballoc_sub,
+							    MIMO_mode_indicator);
+		}
+	    }
+	}			// total_ue_count
+    }				// end of for for r1 and r2
+
+    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
+	UE_id = i;
+	ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+
 	if (flexran_slice_member(UE_id, slice_id) == 0)
-	  continue;
-        
-	for (ii=0; ii<UE_num_active_CC(UE_list,UE_id); ii++) {
-          CC_id = UE_list->ordered_CCids[ii][UE_id];
-	  ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-	  flexran_get_harq(Mod_id, CC_id, UE_id, frameP, subframeP, &harq_pid, &round);	  
-          rnti = UE_RNTI(Mod_id,UE_id);
-
-          // LOG_D(MAC,"UE %d rnti 0x\n", UE_id, rnti );
-          if(rnti == NOT_A_RNTI)
-            continue;
-
-	  if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1)
 	    continue;
 
-	  if (!phy_stats_exist(Mod_id, rnti))
-            continue;
-
-          transmission_mode = mac_xface->get_transmission_mode(Mod_id,CC_id,rnti);
-          //rrc_status = mac_eNB_get_rrc_status(Mod_id,rnti);
-          /* 1st allocate for the retx */
-
-          // retransmission in data channels
-          // control channel in the 1st transmission
-          // data channel for all TM
-          LOG_T(MAC,"calling dlsch_scheduler_pre_processor_allocate .. \n ");
-          _dlsch_scheduler_pre_processor_allocate (Mod_id,
-						   UE_id,
-						   CC_id,
-						   N_RBG[CC_id],
-						   transmission_mode,
-						   min_rb_unit[CC_id],
-						   frame_parms[CC_id]->N_RB_DL,
-						   nb_rbs_required,
-						   nb_rbs_required_remaining,
-						   rballoc_sub,
-						   MIMO_mode_indicator);
-        }
-      }
-    } // total_ue_count
-  } // end of for for r1 and r2
-
-  for(i=UE_list->head; i>=0; i=UE_list->next[i]) {
-    UE_id = i;
-    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
- 
-    if (flexran_slice_member(UE_id, slice_id) == 0)
-      continue;
-
-    for (ii=0; ii<UE_num_active_CC(UE_list,UE_id); ii++) {
-      CC_id = UE_list->ordered_CCids[ii][UE_id];
-      //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].dl_pow_off = dl_pow_off[UE_id];
-
-      if (ue_sched_ctl->pre_nb_available_rbs[CC_id] > 0 ) {
-        LOG_D(MAC,"******************DL Scheduling Information for UE%d ************************\n",UE_id);
-        LOG_D(MAC,"dl power offset UE%d = %d \n",UE_id,ue_sched_ctl->dl_pow_off[CC_id]);
-        LOG_D(MAC,"***********RB Alloc for every subband for UE%d ***********\n",UE_id);
-
-        for(j=0; j<N_RBG[CC_id]; j++) {
-          //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].rballoc_sub[i] = rballoc_sub_UE[CC_id][UE_id][i];
-          LOG_D(MAC,"RB Alloc for UE%d and Subband%d = %d\n",UE_id,j,ue_sched_ctl->rballoc_sub_UE[CC_id][j]);
-        }
-
-        //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].pre_nb_available_rbs = pre_nb_available_rbs[CC_id][UE_id];
-        LOG_D(MAC,"[eNB %d][SLICE %d] Total RBs allocated for UE%d = %d\n",
-	      Mod_id, slice_id, UE_id,ue_sched_ctl->pre_nb_available_rbs[CC_id]);
-      }
+	for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
+	    CC_id = UE_list->ordered_CCids[ii][UE_id];
+	    //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].dl_pow_off = dl_pow_off[UE_id];
+
+	    if (ue_sched_ctl->pre_nb_available_rbs[CC_id] > 0) {
+		LOG_D(MAC,
+		      "******************DL Scheduling Information for UE%d ************************\n",
+		      UE_id);
+		LOG_D(MAC, "dl power offset UE%d = %d \n", UE_id,
+		      ue_sched_ctl->dl_pow_off[CC_id]);
+		LOG_D(MAC,
+		      "***********RB Alloc for every subband for UE%d ***********\n",
+		      UE_id);
+
+		for (j = 0; j < N_RBG[CC_id]; j++) {
+		    //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].rballoc_sub[i] = rballoc_sub_UE[CC_id][UE_id][i];
+		    LOG_D(MAC, "RB Alloc for UE%d and Subband%d = %d\n",
+			  UE_id, j,
+			  ue_sched_ctl->rballoc_sub_UE[CC_id][j]);
+		}
+
+		//PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].pre_nb_available_rbs = pre_nb_available_rbs[CC_id][UE_id];
+		LOG_D(MAC,
+		      "[eNB %d][SLICE %d] Total RBs allocated for UE%d = %d\n",
+		      Mod_id, slice_id, UE_id,
+		      ue_sched_ctl->pre_nb_available_rbs[CC_id]);
+	    }
+	}
     }
-  }
 }
 
 #define SF05_LIMIT 1
@@ -714,956 +826,1180 @@ void _dlsch_scheduler_pre_processor (module_id_t   Mod_id,
  */
 
 void
-flexran_schedule_ue_spec_default(mid_t   mod_id,
-				 uint32_t      frame,
-				 uint32_t      subframe,
-				 int           *mbsfn_flag,
-				 Protocol__FlexranMessage **dl_info)
+flexran_schedule_ue_spec_default(mid_t mod_id,
+				 uint32_t frame,
+				 uint32_t subframe,
+				 int *mbsfn_flag,
+				 Protocol__FlexranMessage ** dl_info)
 //------------------------------------------------------------------------------
 {
-  int i=0;
-  
-  flexran_agent_mac_create_empty_dl_config(mod_id, dl_info);
-   
-  for (i = 0; i < n_active_slices; i++) {
-    
-    // Load any updated functions
-    if (update_dl_scheduler[i] > 0 ) {
-      slice_sched[i] = dlsym(NULL, dl_scheduler_type[i]); 
-      update_dl_scheduler[i] = 0;
-      update_dl_scheduler_current[i] = 0;
-      slice_percentage_current[i]= slice_percentage[i];
-      total_slice_percentage+=slice_percentage[i];
-      LOG_N(MAC,"update dl scheduler slice %d\n", i);
-    }
- 
-    // check if the number of slices has changed, and log 
-    if (n_active_slices_current != n_active_slices ){
-      if ((n_active_slices > 0) && (n_active_slices <= MAX_NUM_SLICES)) {
-	LOG_N(MAC,"[eNB %d]frame %d subframe %d: number of active slices has changed: %d-->%d\n",
-	      mod_id, frame, subframe, n_active_slices_current, n_active_slices);
-	n_active_slices_current = n_active_slices;
-      } else {
-	LOG_W(MAC,"invalid number of slices %d, revert to the previous value %d\n",n_active_slices, n_active_slices_current);
-	n_active_slices = n_active_slices_current;
-      }
-    }
-    
-    // check if the slice rb share has changed, and log the console
-    if (slice_percentage_current[i] != slice_percentage[i]){
-      if ((slice_percentage[i] >= 0.0) && (slice_percentage[i] <= 1.0)){
-	if ((total_slice_percentage - slice_percentage_current[i]  + slice_percentage[i]) <= 1.0) {
-	  total_slice_percentage=total_slice_percentage - slice_percentage_current[i]  + slice_percentage[i];
-	  LOG_N(MAC,"[eNB %d][SLICE %d] frame %d subframe %d: total percentage %f, slice RB percentage has changed: %f-->%f\n",
-		mod_id, i, frame, subframe, total_slice_percentage, slice_percentage_current[i], slice_percentage[i]);
-	  slice_percentage_current[i] = slice_percentage[i];
-	} else {
-	  LOG_W(MAC,"[eNB %d][SLICE %d] invalid total RB share (%f->%f), revert the previous value (%f->%f)\n",
-		mod_id,i,  
-		total_slice_percentage,
-		total_slice_percentage - slice_percentage_current[i]  + slice_percentage[i],
-		slice_percentage[i],slice_percentage_current[i]);
-	  slice_percentage[i]= slice_percentage_current[i];
+    int i = 0;
+
+    flexran_agent_mac_create_empty_dl_config(mod_id, dl_info);
 
+    for (i = 0; i < n_active_slices; i++) {
+
+	// Load any updated functions
+	if (update_dl_scheduler[i] > 0) {
+	    slice_sched[i] = dlsym(NULL, dl_scheduler_type[i]);
+	    update_dl_scheduler[i] = 0;
+	    update_dl_scheduler_current[i] = 0;
+	    slice_percentage_current[i] = slice_percentage[i];
+	    total_slice_percentage += slice_percentage[i];
+	    LOG_N(MAC, "update dl scheduler slice %d\n", i);
 	}
-      } else {
-	LOG_W(MAC,"[eNB %d][SLICE %d] invalid slice RB share, revert the previous value (%f->%f)\n",mod_id, i,  slice_percentage[i],slice_percentage_current[i]);
-	slice_percentage[i]= slice_percentage_current[i];
+	// check if the number of slices has changed, and log 
+	if (n_active_slices_current != n_active_slices) {
+	    if ((n_active_slices > 0)
+		&& (n_active_slices <= MAX_NUM_SLICES)) {
+		LOG_N(MAC,
+		      "[eNB %d]frame %d subframe %d: number of active slices has changed: %d-->%d\n",
+		      mod_id, frame, subframe, n_active_slices_current,
+		      n_active_slices);
+		n_active_slices_current = n_active_slices;
+	    } else {
+		LOG_W(MAC,
+		      "invalid number of slices %d, revert to the previous value %d\n",
+		      n_active_slices, n_active_slices_current);
+		n_active_slices = n_active_slices_current;
+	    }
+	}
+	// check if the slice rb share has changed, and log the console
+	if (slice_percentage_current[i] != slice_percentage[i]) {
+	    if ((slice_percentage[i] >= 0.0)
+		&& (slice_percentage[i] <= 1.0)) {
+		if ((total_slice_percentage - slice_percentage_current[i] +
+		     slice_percentage[i]) <= 1.0) {
+		    total_slice_percentage =
+			total_slice_percentage -
+			slice_percentage_current[i] + slice_percentage[i];
+		    LOG_N(MAC,
+			  "[eNB %d][SLICE %d] frame %d subframe %d: total percentage %f, slice RB percentage has changed: %f-->%f\n",
+			  mod_id, i, frame, subframe,
+			  total_slice_percentage,
+			  slice_percentage_current[i],
+			  slice_percentage[i]);
+		    slice_percentage_current[i] = slice_percentage[i];
+		} else {
+		    LOG_W(MAC,
+			  "[eNB %d][SLICE %d] invalid total RB share (%f->%f), revert the previous value (%f->%f)\n",
+			  mod_id, i, total_slice_percentage,
+			  total_slice_percentage -
+			  slice_percentage_current[i] +
+			  slice_percentage[i], slice_percentage[i],
+			  slice_percentage_current[i]);
+		    slice_percentage[i] = slice_percentage_current[i];
 
-      }
-    }
-  
-    // check if the slice max MCS, and log the console
-    if (slice_maxmcs_current[i] != slice_maxmcs[i]){
-      if ((slice_maxmcs[i] >= 0) && (slice_maxmcs[i] < 29)){
-	LOG_N(MAC,"[eNB %d][SLICE %d] frame %d subframe %d: slice MAX MCS has changed: %d-->%d\n",
-	      mod_id, i, frame, subframe, slice_maxmcs_current[i], slice_maxmcs[i]);
-	slice_maxmcs_current[i] = slice_maxmcs[i];
-      } else {
-	LOG_W(MAC,"[eNB %d][SLICE %d] invalid slice max mcs %d, revert the previous value %d\n",mod_id, i,  slice_percentage[i],slice_percentage[i]);
-	slice_maxmcs[i]= slice_maxmcs_current[i];
-      }
-    }
-    
-    // check if a new scheduler, and log the console
-    if (update_dl_scheduler_current[i] != update_dl_scheduler[i]){
-      LOG_N(MAC,"[eNB %d][SLICE %d] frame %d subframe %d: DL scheduler for this slice is updated: %s \n",
-	    mod_id, i, frame, subframe, dl_scheduler_type[i]);
-      update_dl_scheduler_current[i] = update_dl_scheduler[i];
-    }
+		}
+	    } else {
+		LOG_W(MAC,
+		      "[eNB %d][SLICE %d] invalid slice RB share, revert the previous value (%f->%f)\n",
+		      mod_id, i, slice_percentage[i],
+		      slice_percentage_current[i]);
+		slice_percentage[i] = slice_percentage_current[i];
 
-    // Run each enabled slice-specific schedulers one by one
-    //LOG_N(MAC,"[eNB %d]frame %d subframe %d slice %d: calling the scheduler\n", mod_id, frame, subframe,i);
-    slice_sched[i](mod_id, i, frame, subframe, mbsfn_flag,dl_info);
+	    }
+	}
+	// check if the slice max MCS, and log the console
+	if (slice_maxmcs_current[i] != slice_maxmcs[i]) {
+	    if ((slice_maxmcs[i] >= 0) && (slice_maxmcs[i] < 29)) {
+		LOG_N(MAC,
+		      "[eNB %d][SLICE %d] frame %d subframe %d: slice MAX MCS has changed: %d-->%d\n",
+		      mod_id, i, frame, subframe, slice_maxmcs_current[i],
+		      slice_maxmcs[i]);
+		slice_maxmcs_current[i] = slice_maxmcs[i];
+	    } else {
+		LOG_W(MAC,
+		      "[eNB %d][SLICE %d] invalid slice max mcs %d, revert the previous value %d\n",
+		      mod_id, i, slice_percentage[i], slice_percentage[i]);
+		slice_maxmcs[i] = slice_maxmcs_current[i];
+	    }
+	}
+	// check if a new scheduler, and log the console
+	if (update_dl_scheduler_current[i] != update_dl_scheduler[i]) {
+	    LOG_N(MAC,
+		  "[eNB %d][SLICE %d] frame %d subframe %d: DL scheduler for this slice is updated: %s \n",
+		  mod_id, i, frame, subframe, dl_scheduler_type[i]);
+	    update_dl_scheduler_current[i] = update_dl_scheduler[i];
+	}
+	// Run each enabled slice-specific schedulers one by one
+	//LOG_N(MAC,"[eNB %d]frame %d subframe %d slice %d: calling the scheduler\n", mod_id, frame, subframe,i);
+	slice_sched[i] (mod_id, i, frame, subframe, mbsfn_flag, dl_info);
+
+    }
 
-  }
-  
 }
 
-uint16_t flexran_nb_rbs_allowed_slice(float rb_percentage, int total_rbs){
-  return  (uint16_t) floor(rb_percentage * total_rbs); 
+uint16_t flexran_nb_rbs_allowed_slice(float rb_percentage, int total_rbs)
+{
+    return (uint16_t) floor(rb_percentage * total_rbs);
 }
 
-int flexran_slice_maxmcs(int slice_id) {
-  return slice_maxmcs[slice_id];
+int flexran_slice_maxmcs(int slice_id)
+{
+    return slice_maxmcs[slice_id];
 }
 
-int flexran_slice_member(int UE_id, int slice_id){
-  // group membership definition
-  int slice_member = 0 ;
-  
-  if ((slice_id < 0) || (slice_id > n_active_slices))
-    LOG_W(MAC,"out of range slice id %d\n",slice_id);
-
-  switch (slicing_strategy) {
-  case SLICE_MASK:
-    switch (slice_id){
-    case 0:
-      if (SLICE0_MASK&UE_id){
-	slice_member=1;
-      }
-      break;
-    case 1:
-      if (SLICE1_MASK&UE_id){
-	slice_member=1;
-      }
-      break;
-    case 2:
-      if (SLICE2_MASK&UE_id){
-	slice_member=1;
-      }
-       break;
-    case 3:
-      if (SLICE3_MASK&UE_id){
-	slice_member=1;
-      }
-      break;
-    default :
-      LOG_W(MAC,"unknown slice_id %d\n", slice_id);
-      break;
-      
-    }
-    break;
-  case UEID_TO_SLICEID:
-  default:
-    if ((UE_id % n_active_slices) == slice_id){
-      slice_member= 1; // this ue is a member of this slice
+int flexran_slice_member(int UE_id, int slice_id)
+{
+    // group membership definition
+    int slice_member = 0;
+
+    if ((slice_id < 0) || (slice_id > n_active_slices))
+	LOG_W(MAC, "out of range slice id %d\n", slice_id);
+
+    switch (slicing_strategy) {
+    case SLICE_MASK:
+	switch (slice_id) {
+	case 0:
+	    if (SLICE0_MASK & UE_id) {
+		slice_member = 1;
+	    }
+	    break;
+	case 1:
+	    if (SLICE1_MASK & UE_id) {
+		slice_member = 1;
+	    }
+	    break;
+	case 2:
+	    if (SLICE2_MASK & UE_id) {
+		slice_member = 1;
+	    }
+	    break;
+	case 3:
+	    if (SLICE3_MASK & UE_id) {
+		slice_member = 1;
+	    }
+	    break;
+	default:
+	    LOG_W(MAC, "unknown slice_id %d\n", slice_id);
+	    break;
+
+	}
+	break;
+    case UEID_TO_SLICEID:
+    default:
+	if ((UE_id % n_active_slices) == slice_id) {
+	    slice_member = 1;	// this ue is a member of this slice
+	}
+	break;
     }
-    break;
-  }
-  
-  return slice_member;
+
+    return slice_member;
 }
+
 /* more aggressive rb and mcs allocation with medium priority and the traffic qci */
 void
-flexran_schedule_ue_spec_embb(mid_t         mod_id,
-			      int           slice_id, 
-			      uint32_t      frame,
-			      uint32_t      subframe,
-			      int           *mbsfn_flag,
-			      Protocol__FlexranMessage **dl_info)
-
+flexran_schedule_ue_spec_embb(mid_t mod_id,
+			      int slice_id,
+			      uint32_t frame,
+			      uint32_t subframe,
+			      int *mbsfn_flag,
+			      Protocol__FlexranMessage ** dl_info)
 {
-  flexran_schedule_ue_spec_common(mod_id,
-				  slice_id,
-				  frame,
-				  subframe,
-				  mbsfn_flag,
-				  dl_info);
-  
+    flexran_schedule_ue_spec_common(mod_id,
+				    slice_id,
+				    frame, subframe, mbsfn_flag, dl_info);
+
 }
+
 /* more conservative mcs allocation with high priority and the traffic qci */
 void
-flexran_schedule_ue_spec_urllc(mid_t         mod_id,
-			       int           slice_id, 
-			       uint32_t      frame,
-			       uint32_t      subframe,
-			       int           *mbsfn_flag,
-			       Protocol__FlexranMessage **dl_info)
-
+flexran_schedule_ue_spec_urllc(mid_t mod_id,
+			       int slice_id,
+			       uint32_t frame,
+			       uint32_t subframe,
+			       int *mbsfn_flag,
+			       Protocol__FlexranMessage ** dl_info)
 {
-  flexran_schedule_ue_spec_common(mod_id,
-				  slice_id,
-				  frame,
-				  subframe,
-				  mbsfn_flag,
-				  dl_info);
-  
+    flexran_schedule_ue_spec_common(mod_id,
+				    slice_id,
+				    frame, subframe, mbsfn_flag, dl_info);
+
 }
+
 /* constant rb allocation with low mcs with low priority and given the UE capabilities */
 void
-flexran_schedule_ue_spec_mmtc(mid_t         mod_id,
-			      int           slice_id, 
-			      uint32_t      frame,
-			      uint32_t      subframe,
-			      int           *mbsfn_flag,
-			      Protocol__FlexranMessage **dl_info)
-  
+flexran_schedule_ue_spec_mmtc(mid_t mod_id,
+			      int slice_id,
+			      uint32_t frame,
+			      uint32_t subframe,
+			      int *mbsfn_flag,
+			      Protocol__FlexranMessage ** dl_info)
 {
-  
-  flexran_schedule_ue_spec_common(mod_id,
-				  slice_id,
-				  frame,
-				  subframe,
-				  mbsfn_flag,
-				  dl_info);
-  
+
+    flexran_schedule_ue_spec_common(mod_id,
+				    slice_id,
+				    frame, subframe, mbsfn_flag, dl_info);
+
 }
+
 /* regular rb and mcs allocation with low priority */
 void
-flexran_schedule_ue_spec_be(mid_t         mod_id,
-			    int           slice_id, 
-			    uint32_t      frame,
-			    uint32_t      subframe,
-			    int           *mbsfn_flag,
-			    Protocol__FlexranMessage **dl_info)
-  
+flexran_schedule_ue_spec_be(mid_t mod_id,
+			    int slice_id,
+			    uint32_t frame,
+			    uint32_t subframe,
+			    int *mbsfn_flag,
+			    Protocol__FlexranMessage ** dl_info)
 {
-  
-  flexran_schedule_ue_spec_common(mod_id,
-				  slice_id,
-				  frame,
-				  subframe,
-				  mbsfn_flag,
-				  dl_info);
-  
+
+    flexran_schedule_ue_spec_common(mod_id,
+				    slice_id,
+				    frame, subframe, mbsfn_flag, dl_info);
+
 }
 
 //------------------------------------------------------------------------------
 void
-flexran_schedule_ue_spec_common(mid_t   mod_id,
-				int           slice_id, 
-				uint32_t      frame,
-				uint32_t      subframe,
-				int           *mbsfn_flag,
-				Protocol__FlexranMessage **dl_info)
+flexran_schedule_ue_spec_common(mid_t mod_id,
+				int slice_id,
+				uint32_t frame,
+				uint32_t subframe,
+				int *mbsfn_flag,
+				Protocol__FlexranMessage ** dl_info)
 //------------------------------------------------------------------------------
 {
-  uint8_t               CC_id;
-  int                   UE_id;
-  int                   N_RBG[MAX_NUM_CCs];
-  unsigned char         aggregation;
-  mac_rlc_status_resp_t rlc_status;
-  unsigned char         header_len = 0, header_len_last = 0, ta_len = 0;
-  uint16_t              nb_rb, nb_rb_temp, total_nb_available_rb[MAX_NUM_CCs], nb_available_rb;
-  uint16_t              TBS, j, rnti;
-  uint8_t         round            = 0;
-  uint8_t         harq_pid         = 0;
-  uint16_t              sdu_length_total = 0;
-  int                   mcs, mcs_tmp;
-  uint16_t              min_rb_unit[MAX_NUM_CCs];
-  eNB_MAC_INST         *eNB      = &eNB_mac_inst[mod_id];
-  /* TODO: Must move the helper structs to scheduler implementation */
-  UE_list_t            *UE_list  = &eNB->UE_list;
-  int32_t                 normalized_rx_power, target_rx_power;
-  int32_t                 tpc = 1;
-  static int32_t          tpc_accumulated=0;
-  UE_sched_ctrl           *ue_sched_ctl;
-  LTE_eNB_UE_stats     *eNB_UE_stats     = NULL;
-  Protocol__FlexDlData *dl_data[NUM_MAX_UE];
-  int num_ues_added = 0;
-  int channels_added = 0;
-
-  Protocol__FlexDlDci *dl_dci;
-  Protocol__FlexRlcPdu *rlc_pdus[11];
-  uint32_t ce_flags = 0;
-
-  uint8_t            rballoc_sub[25];
-  int i;
-  uint32_t data_to_request;
-  uint32_t dci_tbs;
-  uint8_t ue_has_transmission = 0;
-  uint32_t ndi;
-  
+    uint8_t CC_id;
+    int UE_id;
+    int N_RBG[MAX_NUM_CCs];
+    unsigned char aggregation;
+    mac_rlc_status_resp_t rlc_status;
+    unsigned char header_len = 0, header_len_last = 0, ta_len = 0;
+    uint16_t nb_rb, nb_rb_temp, total_nb_available_rb[MAX_NUM_CCs],
+	nb_available_rb;
+    uint16_t TBS, j, rnti;
+    uint8_t round = 0;
+    uint8_t harq_pid = 0;
+    uint16_t sdu_length_total = 0;
+    int mcs, mcs_tmp;
+    uint16_t min_rb_unit[MAX_NUM_CCs];
+    eNB_MAC_INST *eNB = &eNB_mac_inst[mod_id];
+    /* TODO: Must move the helper structs to scheduler implementation */
+    UE_list_t *UE_list = &eNB->UE_list;
+    int32_t normalized_rx_power, target_rx_power;
+    int32_t tpc = 1;
+    static int32_t tpc_accumulated = 0;
+    UE_sched_ctrl *ue_sched_ctl;
+    LTE_eNB_UE_stats *eNB_UE_stats = NULL;
+    Protocol__FlexDlData *dl_data[NUM_MAX_UE];
+    int num_ues_added = 0;
+    int channels_added = 0;
+
+    Protocol__FlexDlDci *dl_dci;
+    Protocol__FlexRlcPdu *rlc_pdus[11];
+    uint32_t ce_flags = 0;
+
+    uint8_t rballoc_sub[25];
+    int i;
+    uint32_t data_to_request;
+    uint32_t dci_tbs;
+    uint8_t ue_has_transmission = 0;
+    uint32_t ndi;
+
 #if 0
-  
-  if (UE_list->head==-1) {
-    return;
-  }
-  
+
+    if (UE_list->head == -1) {
+	return;
+    }
 #endif
 
-  start_meas(&eNB->schedule_dlsch);
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_SCHEDULE_DLSCH,VCD_FUNCTION_IN);
-
-  //weight = get_ue_weight(module_idP,UE_id);
-  aggregation = 1; // set to the maximum aggregation level
-
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-    min_rb_unit[CC_id] = get_min_rb_unit(mod_id, CC_id);
-    // get number of PRBs less those used by common channels
-    total_nb_available_rb[CC_id] = flexran_get_N_RB_DL(mod_id, CC_id);
-    for (i=0;i < flexran_get_N_RB_DL(mod_id, CC_id); i++)
-      if (eNB->common_channels[CC_id].vrb_map[i] != 0)
-	total_nb_available_rb[CC_id]--;
-    
-    N_RBG[CC_id] = flexran_get_N_RBG(mod_id, CC_id);
-
-    // store the global enb stats:
-    eNB->eNB_stats[CC_id].num_dlactive_UEs =  UE_list->num_UEs;
-    eNB->eNB_stats[CC_id].available_prbs =  total_nb_available_rb[CC_id];
-    eNB->eNB_stats[CC_id].total_available_prbs +=  total_nb_available_rb[CC_id];
-    eNB->eNB_stats[CC_id].dlsch_bytes_tx=0;
-    eNB->eNB_stats[CC_id].dlsch_pdus_tx=0;
-  }
-
-   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_DLSCH_PREPROCESSOR,VCD_FUNCTION_IN);
-
-   start_meas(&eNB->schedule_dlsch_preprocessor);
-   _dlsch_scheduler_pre_processor(mod_id,
-				  slice_id,
-				  frame,
-				  subframe,
-				  N_RBG,
-				  mbsfn_flag);
-   stop_meas(&eNB->schedule_dlsch_preprocessor);
-   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_DLSCH_PREPROCESSOR,VCD_FUNCTION_OUT);
-
-   for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-    LOG_D(MAC, "doing schedule_ue_spec for CC_id %d\n",CC_id);
-
-    if (mbsfn_flag[CC_id]>0)
-      continue;
-    
-    for (UE_id=UE_list->head; UE_id>=0; UE_id=UE_list->next[UE_id]) {
-  
-      rnti = flexran_get_ue_crnti(mod_id, UE_id);
-      eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id,CC_id,rnti);
-      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-      
-      if (eNB_UE_stats==NULL) {
-	LOG_D(MAC,"[eNB] Cannot find eNB_UE_stats\n");
-        //  mac_xface->macphy_exit("[MAC][eNB] Cannot find eNB_UE_stats\n");
-	continue;
-      }
-      
-      if (flexran_slice_member(UE_id, slice_id) == 0)
-      	continue;
-      
-      if (rnti==NOT_A_RNTI) {
-        LOG_D(MAC,"Cannot find rnti for UE_id %d (num_UEs %d)\n", UE_id,UE_list->num_UEs);
-        // mac_xface->macphy_exit("Cannot find rnti for UE_id");
-        continue;
-      }
-
-      switch(mac_xface->get_transmission_mode(mod_id,CC_id,rnti)){
-      case 1:
-      case 2:
-      case 7:
-	aggregation = get_aggregation(get_bw_index(mod_id,CC_id), 
-				      eNB_UE_stats->DL_cqi[0],
-				      format1);
-	break;
-      case 3:
-	aggregation = get_aggregation(get_bw_index(mod_id,CC_id), 
-				      eNB_UE_stats->DL_cqi[0],
-				      format2A);
-	break;
-      default:
-	LOG_W(MAC,"Unsupported transmission mode %d\n", mac_xface->get_transmission_mode(mod_id,CC_id,rnti));
-	aggregation = 2;
-      }
-     
-      if ((ue_sched_ctl->pre_nb_available_rbs[CC_id] == 0) ||  // no RBs allocated 
-	  CCE_allocation_infeasible(mod_id, CC_id, 0, subframe, aggregation, rnti)) {
-        LOG_D(MAC,"[eNB %d] Frame %d : no RB allocated for UE %d on CC_id %d: continue \n",
-              mod_id, frame, UE_id, CC_id);
-        //if(mac_xface->get_transmission_mode(module_idP,rnti)==5)
-        continue; //to next user (there might be rbs availiable for other UEs in TM5
-        // else
-        //  break;
-      }
-
-      if (flexran_get_duplex_mode(mod_id, CC_id) == PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD)  {
-        set_ue_dai (subframe,
-                    flexran_get_subframe_assignment(mod_id, CC_id),
-                    UE_id,
-                    CC_id,
-                    UE_list);
-        //TODO: update UL DAI after DLSCH scheduling
-        //set_ul_DAI(mod_id, UE_id, CC_id, frame, subframe,frame_parms);
-      }
-
-      channels_added = 0;
-
-      // After this point all the UEs will be scheduled
-      dl_data[num_ues_added] = (Protocol__FlexDlData *) malloc(sizeof(Protocol__FlexDlData));
-      protocol__flex_dl_data__init(dl_data[num_ues_added]);
-      dl_data[num_ues_added]->has_rnti = 1;
-      dl_data[num_ues_added]->rnti = rnti;
-      dl_data[num_ues_added]->n_rlc_pdu = 0;
-      dl_data[num_ues_added]->has_serv_cell_index = 1;
-      dl_data[num_ues_added]->serv_cell_index = CC_id;
-      
-      nb_available_rb = ue_sched_ctl->pre_nb_available_rbs[CC_id];
-      flexran_get_harq(mod_id, CC_id, UE_id, frame, subframe, &harq_pid, &round);
-      sdu_length_total=0;
-      mcs = cqi_to_mcs[flexran_get_ue_wcqi(mod_id, UE_id)];
-      //      LOG_I(FLEXRAN_AGENT, "The MCS is %d\n", mcs);
-      mcs = cmin(mcs,flexran_slice_maxmcs(slice_id));
-#ifdef EXMIMO
+    start_meas(&eNB->schedule_dlsch);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_SCHEDULE_DLSCH, VCD_FUNCTION_IN);
+
+    //weight = get_ue_weight(module_idP,UE_id);
+    aggregation = 1;		// set to the maximum aggregation level
+
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+	min_rb_unit[CC_id] = get_min_rb_unit(mod_id, CC_id);
+	// get number of PRBs less those used by common channels
+	total_nb_available_rb[CC_id] = flexran_get_N_RB_DL(mod_id, CC_id);
+	for (i = 0; i < flexran_get_N_RB_DL(mod_id, CC_id); i++)
+	    if (eNB->common_channels[CC_id].vrb_map[i] != 0)
+		total_nb_available_rb[CC_id]--;
+
+	N_RBG[CC_id] = flexran_get_N_RBG(mod_id, CC_id);
+
+	// store the global enb stats:
+	eNB->eNB_stats[CC_id].num_dlactive_UEs = UE_list->num_UEs;
+	eNB->eNB_stats[CC_id].available_prbs =
+	    total_nb_available_rb[CC_id];
+	eNB->eNB_stats[CC_id].total_available_prbs +=
+	    total_nb_available_rb[CC_id];
+	eNB->eNB_stats[CC_id].dlsch_bytes_tx = 0;
+	eNB->eNB_stats[CC_id].dlsch_pdus_tx = 0;
+    }
+
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_DLSCH_PREPROCESSOR, VCD_FUNCTION_IN);
 
-       if (mac_xface->get_transmission_mode(mod_id, CC_id, rnti) == 5) {
-	  mcs = cqi_to_mcs[flexran_get_ue_wcqi(mod_id, UE_id)];
-	  mcs =  cmin(mcs,16);
-       }
+    start_meas(&eNB->schedule_dlsch_preprocessor);
+    _dlsch_scheduler_pre_processor(mod_id,
+				   slice_id,
+				   frame, subframe, N_RBG, mbsfn_flag);
+    stop_meas(&eNB->schedule_dlsch_preprocessor);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_DLSCH_PREPROCESSOR, VCD_FUNCTION_OUT);
+
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+	LOG_D(MAC, "doing schedule_ue_spec for CC_id %d\n", CC_id);
+
+	if (mbsfn_flag[CC_id] > 0)
+	    continue;
+
+	for (UE_id = UE_list->head; UE_id >= 0;
+	     UE_id = UE_list->next[UE_id]) {
+
+	    rnti = flexran_get_ue_crnti(mod_id, UE_id);
+	    eNB_UE_stats =
+		mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
+	    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+
+	    if (eNB_UE_stats == NULL) {
+		LOG_D(MAC, "[eNB] Cannot find eNB_UE_stats\n");
+		//  mac_xface->macphy_exit("[MAC][eNB] Cannot find eNB_UE_stats\n");
+		continue;
+	    }
+
+	    if (flexran_slice_member(UE_id, slice_id) == 0)
+		continue;
+
+	    if (rnti == NOT_A_RNTI) {
+		LOG_D(MAC, "Cannot find rnti for UE_id %d (num_UEs %d)\n",
+		      UE_id, UE_list->num_UEs);
+		// mac_xface->macphy_exit("Cannot find rnti for UE_id");
+		continue;
+	    }
+
+	    switch (mac_xface->get_transmission_mode(mod_id, CC_id, rnti)) {
+	    case 1:
+	    case 2:
+	    case 7:
+		aggregation = get_aggregation(get_bw_index(mod_id, CC_id),
+					      eNB_UE_stats->DL_cqi[0],
+					      format1);
+		break;
+	    case 3:
+		aggregation = get_aggregation(get_bw_index(mod_id, CC_id),
+					      eNB_UE_stats->DL_cqi[0],
+					      format2A);
+		break;
+	    default:
+		LOG_W(MAC, "Unsupported transmission mode %d\n",
+		      mac_xface->get_transmission_mode(mod_id, CC_id,
+						       rnti));
+		aggregation = 2;
+	    }
+
+	    if ((ue_sched_ctl->pre_nb_available_rbs[CC_id] == 0) ||	// no RBs allocated 
+		CCE_allocation_infeasible(mod_id, CC_id, 0, subframe,
+					  aggregation, rnti)) {
+		LOG_D(MAC,
+		      "[eNB %d] Frame %d : no RB allocated for UE %d on CC_id %d: continue \n",
+		      mod_id, frame, UE_id, CC_id);
+		//if(mac_xface->get_transmission_mode(module_idP,rnti)==5)
+		continue;	//to next user (there might be rbs availiable for other UEs in TM5
+		// else
+		//  break;
+	    }
 
+	    if (flexran_get_duplex_mode(mod_id, CC_id) ==
+		PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
+		set_ue_dai(subframe,
+			   flexran_get_subframe_assignment(mod_id, CC_id),
+			   UE_id, CC_id, UE_list);
+		//TODO: update UL DAI after DLSCH scheduling
+		//set_ul_DAI(mod_id, UE_id, CC_id, frame, subframe,frame_parms);
+	    }
+
+	    channels_added = 0;
+
+	    // After this point all the UEs will be scheduled
+	    dl_data[num_ues_added] =
+		(Protocol__FlexDlData *)
+		malloc(sizeof(Protocol__FlexDlData));
+	    protocol__flex_dl_data__init(dl_data[num_ues_added]);
+	    dl_data[num_ues_added]->has_rnti = 1;
+	    dl_data[num_ues_added]->rnti = rnti;
+	    dl_data[num_ues_added]->n_rlc_pdu = 0;
+	    dl_data[num_ues_added]->has_serv_cell_index = 1;
+	    dl_data[num_ues_added]->serv_cell_index = CC_id;
+
+	    nb_available_rb = ue_sched_ctl->pre_nb_available_rbs[CC_id];
+	    flexran_get_harq(mod_id, CC_id, UE_id, frame, subframe,
+			     &harq_pid, &round);
+	    sdu_length_total = 0;
+	    mcs = cqi_to_mcs[flexran_get_ue_wcqi(mod_id, UE_id)];
+	    //      LOG_I(FLEXRAN_AGENT, "The MCS is %d\n", mcs);
+	    mcs = cmin(mcs, flexran_slice_maxmcs(slice_id));
+#ifdef EXMIMO
+
+	    if (mac_xface->get_transmission_mode(mod_id, CC_id, rnti) == 5) {
+		mcs = cqi_to_mcs[flexran_get_ue_wcqi(mod_id, UE_id)];
+		mcs = cmin(mcs, 16);
+	    }
 #endif
 
-      // initializing the rb allocation indicator for each UE
-       for(j = 0; j < flexran_get_N_RBG(mod_id, CC_id); j++) {
-        UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j] = 0;
-	rballoc_sub[j] = 0;
-      }
-
-      /* LOG_D(MAC,"[eNB %d] Frame %d: Scheduling UE %d on CC_id %d (rnti %x, harq_pid %d, round %d, rb %d, cqi %d, mcs %d, rrc %d)\n", */
-      /*       mod_id, frame, UE_id, CC_id, rnti, harq_pid, round, nb_available_rb, */
-      /*       eNB_UE_stats->DL_cqi[0], mcs, */
-      /*       UE_list->eNB_UE_stats[CC_id][UE_id].rrc_status); */
-
-      dl_dci = (Protocol__FlexDlDci*) malloc(sizeof(Protocol__FlexDlDci));
-      protocol__flex_dl_dci__init(dl_dci);
-      dl_data[num_ues_added]->dl_dci = dl_dci;
-
-      
-      dl_dci->has_rnti = 1;
-      dl_dci->rnti = rnti;
-      dl_dci->has_harq_process = 1;
-      dl_dci->harq_process = harq_pid;
-      
-      /* process retransmission  */
-      if (round > 0) {
-
-	LOG_D(FLEXRAN_AGENT, "There was a retransmission just now and the round was %d\n", round);
-
-	if (flexran_get_duplex_mode(mod_id, CC_id) == PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-	  UE_list->UE_template[CC_id][UE_id].DAI++;
-	  update_ul_dci(mod_id, CC_id, rnti, UE_list->UE_template[CC_id][UE_id].DAI);
-	  LOG_D(MAC,"DAI update: CC_id %d subframeP %d: UE %d, DAI %d\n",
-		CC_id, subframe,UE_id,UE_list->UE_template[CC_id][UE_id].DAI);
-	}
+	    // initializing the rb allocation indicator for each UE
+	    for (j = 0; j < flexran_get_N_RBG(mod_id, CC_id); j++) {
+		UE_list->
+		    UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j]
+		    = 0;
+		rballoc_sub[j] = 0;
+	    }
 
-	mcs = UE_list->UE_template[CC_id][UE_id].mcs[harq_pid];
-
-	  // get freq_allocation
-	nb_rb = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
-	  
-	/*TODO: Must add this to FlexRAN agent API */
-	dci_tbs = mac_xface->get_TBS_DL(mcs, nb_rb);
-
-	if (nb_rb <= nb_available_rb) {
-	  
-	  if(nb_rb == ue_sched_ctl->pre_nb_available_rbs[CC_id]) {
-	    for(j = 0; j < flexran_get_N_RBG(mod_id, CC_id); j++) { // for indicating the rballoc for each sub-band
-	      UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j] = ue_sched_ctl->rballoc_sub_UE[CC_id][j];
-            }
-	  } else {
-	    nb_rb_temp = nb_rb;
-	    j = 0;
-
-	    while((nb_rb_temp > 0) && (j < flexran_get_N_RBG(mod_id, CC_id))) {
-	      if(ue_sched_ctl->rballoc_sub_UE[CC_id][j] == 1) {
-		UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j] = ue_sched_ctl->rballoc_sub_UE[CC_id][j];
-		
-		if((j == flexran_get_N_RBG(mod_id, CC_id) - 1) &&
-		   ((flexran_get_N_RB_DL(mod_id, CC_id) == 25)||
-		    (flexran_get_N_RB_DL(mod_id, CC_id) == 50))) {
-		  nb_rb_temp = nb_rb_temp - min_rb_unit[CC_id]+1;
+	    /* LOG_D(MAC,"[eNB %d] Frame %d: Scheduling UE %d on CC_id %d (rnti %x, harq_pid %d, round %d, rb %d, cqi %d, mcs %d, rrc %d)\n", */
+	    /*       mod_id, frame, UE_id, CC_id, rnti, harq_pid, round, nb_available_rb, */
+	    /*       eNB_UE_stats->DL_cqi[0], mcs, */
+	    /*       UE_list->eNB_UE_stats[CC_id][UE_id].rrc_status); */
+
+	    dl_dci =
+		(Protocol__FlexDlDci *)
+		malloc(sizeof(Protocol__FlexDlDci));
+	    protocol__flex_dl_dci__init(dl_dci);
+	    dl_data[num_ues_added]->dl_dci = dl_dci;
+
+
+	    dl_dci->has_rnti = 1;
+	    dl_dci->rnti = rnti;
+	    dl_dci->has_harq_process = 1;
+	    dl_dci->harq_process = harq_pid;
+
+	    /* process retransmission  */
+	    if (round > 0) {
+
+		LOG_D(FLEXRAN_AGENT,
+		      "There was a retransmission just now and the round was %d\n",
+		      round);
+
+		if (flexran_get_duplex_mode(mod_id, CC_id) ==
+		    PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
+		    UE_list->UE_template[CC_id][UE_id].DAI++;
+		    update_ul_dci(mod_id, CC_id, rnti,
+				  UE_list->UE_template[CC_id][UE_id].DAI);
+		    LOG_D(MAC,
+			  "DAI update: CC_id %d subframeP %d: UE %d, DAI %d\n",
+			  CC_id, subframe, UE_id,
+			  UE_list->UE_template[CC_id][UE_id].DAI);
+		}
+
+		mcs = UE_list->UE_template[CC_id][UE_id].mcs[harq_pid];
+
+		// get freq_allocation
+		nb_rb = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
+
+		/*TODO: Must add this to FlexRAN agent API */
+		dci_tbs = mac_xface->get_TBS_DL(mcs, nb_rb);
+
+		if (nb_rb <= nb_available_rb) {
+
+		    if (nb_rb == ue_sched_ctl->pre_nb_available_rbs[CC_id]) {
+			for (j = 0; j < flexran_get_N_RBG(mod_id, CC_id); j++) {	// for indicating the rballoc for each sub-band
+			    UE_list->UE_template[CC_id][UE_id].
+				rballoc_subband[harq_pid][j] =
+				ue_sched_ctl->rballoc_sub_UE[CC_id][j];
+			}
+		    } else {
+			nb_rb_temp = nb_rb;
+			j = 0;
+
+			while ((nb_rb_temp > 0)
+			       && (j < flexran_get_N_RBG(mod_id, CC_id))) {
+			    if (ue_sched_ctl->rballoc_sub_UE[CC_id][j] ==
+				1) {
+				UE_list->
+				    UE_template[CC_id]
+				    [UE_id].rballoc_subband[harq_pid][j] =
+				    ue_sched_ctl->rballoc_sub_UE[CC_id][j];
+
+				if ((j ==
+				     flexran_get_N_RBG(mod_id, CC_id) - 1)
+				    &&
+				    ((flexran_get_N_RB_DL(mod_id, CC_id) ==
+				      25)
+				     || (flexran_get_N_RB_DL(mod_id, CC_id)
+					 == 50))) {
+				    nb_rb_temp =
+					nb_rb_temp - min_rb_unit[CC_id] +
+					1;
+				} else {
+				    nb_rb_temp =
+					nb_rb_temp - min_rb_unit[CC_id];
+				}
+			    }
+			    j = j + 1;
+			}
+		    }
+
+		    nb_available_rb -= nb_rb;
+		    PHY_vars_eNB_g[mod_id][CC_id]->
+			mu_mimo_mode[UE_id].pre_nb_available_rbs = nb_rb;
+		    PHY_vars_eNB_g[mod_id][CC_id]->
+			mu_mimo_mode[UE_id].dl_pow_off =
+			ue_sched_ctl->dl_pow_off[CC_id];
+
+		    for (j = 0; j < flexran_get_N_RBG(mod_id, CC_id); j++) {
+			PHY_vars_eNB_g[mod_id][CC_id]->
+			    mu_mimo_mode[UE_id].rballoc_sub[j] =
+			    UE_list->
+			    UE_template[CC_id][UE_id].rballoc_subband
+			    [harq_pid][j];
+			rballoc_sub[j] =
+			    UE_list->
+			    UE_template[CC_id][UE_id].rballoc_subband
+			    [harq_pid][j];
+		    }
+
+		    // Keep the old NDI, do not toggle
+		    ndi =
+			UE_list->UE_template[CC_id][UE_id].
+			oldNDI[harq_pid];
+		    tpc =
+			UE_list->UE_template[CC_id][UE_id].
+			oldTPC[harq_pid];
+		    UE_list->UE_template[CC_id][UE_id].mcs[harq_pid] = mcs;
+
+		    ue_has_transmission = 1;
+		    num_ues_added++;
 		} else {
-		  nb_rb_temp = nb_rb_temp - min_rb_unit[CC_id];
+		    LOG_D(MAC,
+			  "[eNB %d] Frame %d CC_id %d : don't schedule UE %d, its retransmission takes more resources than we have\n",
+			  mod_id, frame, CC_id, UE_id);
+		    ue_has_transmission = 0;
+		}
+		//End of retransmission
+	    } else {		/* This is a potentially new SDU opportunity */
+		rlc_status.bytes_in_buffer = 0;
+		// Now check RLC information to compute number of required RBs
+		// get maximum TBS size for RLC request
+		//TBS = mac_xface->get_TBS(eNB_UE_stats->DL_cqi[0]<<1,nb_available_rb);
+		TBS = mac_xface->get_TBS_DL(mcs, nb_available_rb);
+		dci_tbs = TBS;
+		LOG_D(FLEXRAN_AGENT, "TBS is %d\n", TBS);
+
+		// check first for RLC data on DCCH
+		// add the length for  all the control elements (timing adv, drx, etc) : header + payload
+
+		ta_len = (ue_sched_ctl->ta_update != 0) ? 2 : 0;
+
+		dl_data[num_ues_added]->n_ce_bitmap = 2;
+		dl_data[num_ues_added]->ce_bitmap =
+		    (uint32_t *) calloc(2, sizeof(uint32_t));
+
+		if (ta_len > 0) {
+		    ce_flags |= PROTOCOL__FLEX_CE_TYPE__FLPCET_TA;
+		}
+
+		/*TODO: Add other flags if DRX and other CE are required */
+
+		// Add the control element flags to the flexran message
+		dl_data[num_ues_added]->ce_bitmap[0] = ce_flags;
+		dl_data[num_ues_added]->ce_bitmap[1] = ce_flags;
+
+		// TODO : Need to prioritize DRBs
+		// Loop through the UE logical channels (DCCH, DCCH1, DTCH for now)
+		header_len = 0;
+		header_len_last = 0;
+		sdu_length_total = 0;
+		for (j = 1; j < NB_RB_MAX; j++) {
+		    header_len += 3;
+		    // Need to see if we have space for data from this channel
+		    if (dci_tbs - ta_len - header_len - sdu_length_total >
+			0) {
+			LOG_D(MAC,
+			      "[TEST]Requested %d bytes from RLC buffer on channel %d during first call\n",
+			      dci_tbs - ta_len - header_len);
+			//If we have space, we need to see how much data we can request at most (if any available)
+			rlc_status = mac_rlc_status_ind(mod_id, rnti, mod_id, frame, subframe, ENB_FLAG_YES, MBMS_FLAG_NO, j, (dci_tbs - ta_len - header_len - sdu_length_total));	// transport block set size
+
+			//If data are available in channel j
+			if (rlc_status.bytes_in_buffer > 0) {
+			    LOG_D(MAC,
+				  "[TEST]Have %d bytes in DCCH buffer during first call\n",
+				  rlc_status.bytes_in_buffer);
+			    //Fill in as much as possible
+			    data_to_request =
+				cmin(dci_tbs - ta_len - header_len -
+				     sdu_length_total,
+				     rlc_status.bytes_in_buffer);
+			    LOG_D(FLEXRAN_AGENT,
+				  "Will request %d bytes from channel %d\n",
+				  data_to_request, j);
+			    if (data_to_request < 128) {	//The header will be one byte less
+				header_len--;
+				header_len_last = 2;
+			    } else {
+				header_len_last = 3;
+			    }
+			    /* if (j == 1 || j == 2) {
+			       data_to_request+=0; 
+			       } */
+			    LOG_D(MAC,
+				  "[TEST]Will request %d from channel %d\n",
+				  data_to_request, j);
+			    rlc_pdus[channels_added] =
+				(Protocol__FlexRlcPdu *)
+				malloc(sizeof(Protocol__FlexRlcPdu));
+			    protocol__flex_rlc_pdu__init(rlc_pdus
+							 [channels_added]);
+			    rlc_pdus[channels_added]->n_rlc_pdu_tb = 2;
+			    rlc_pdus[channels_added]->rlc_pdu_tb =
+				(Protocol__FlexRlcPduTb **)
+				malloc(sizeof(Protocol__FlexRlcPduTb *) *
+				       2);
+			    rlc_pdus[channels_added]->rlc_pdu_tb[0] =
+				(Protocol__FlexRlcPduTb *)
+				malloc(sizeof(Protocol__FlexRlcPduTb));
+			    protocol__flex_rlc_pdu_tb__init(rlc_pdus
+							    [channels_added]->rlc_pdu_tb
+							    [0]);
+			    rlc_pdus[channels_added]->
+				rlc_pdu_tb[0]->has_logical_channel_id = 1;
+			    rlc_pdus[channels_added]->
+				rlc_pdu_tb[0]->logical_channel_id = j;
+			    rlc_pdus[channels_added]->rlc_pdu_tb[0]->
+				has_size = 1;
+			    rlc_pdus[channels_added]->rlc_pdu_tb[0]->size =
+				data_to_request;
+			    rlc_pdus[channels_added]->rlc_pdu_tb[1] =
+				(Protocol__FlexRlcPduTb *)
+				malloc(sizeof(Protocol__FlexRlcPduTb));
+			    protocol__flex_rlc_pdu_tb__init(rlc_pdus
+							    [channels_added]->rlc_pdu_tb
+							    [1]);
+			    rlc_pdus[channels_added]->
+				rlc_pdu_tb[1]->has_logical_channel_id = 1;
+			    rlc_pdus[channels_added]->
+				rlc_pdu_tb[1]->logical_channel_id = j;
+			    rlc_pdus[channels_added]->rlc_pdu_tb[1]->
+				has_size = 1;
+			    rlc_pdus[channels_added]->rlc_pdu_tb[1]->size =
+				data_to_request;
+			    dl_data[num_ues_added]->n_rlc_pdu++;
+			    channels_added++;
+			    //Set this to the max value that we might request
+			    sdu_length_total += data_to_request;
+			} else {
+			    //Take back the assumption of a header for this channel
+			    header_len -= 3;
+			}	//End rlc_status.bytes_in_buffer <= 0
+		    }		//end of if dci_tbs - ta_len - header_len > 0
+		}		// End of iterating the logical channels
+
+		// Add rlc_pdus to the dl_data message
+		dl_data[num_ues_added]->rlc_pdu = (Protocol__FlexRlcPdu **)
+		    malloc(sizeof(Protocol__FlexRlcPdu *) *
+			   dl_data[num_ues_added]->n_rlc_pdu);
+		for (i = 0; i < dl_data[num_ues_added]->n_rlc_pdu; i++) {
+		    dl_data[num_ues_added]->rlc_pdu[i] = rlc_pdus[i];
+		}
+
+		if (header_len == 0) {
+		    LOG_D(FLEXRAN_AGENT, "Header was empty\n");
+		    header_len_last = 0;
+		}
+		// there is a payload
+		if ((dl_data[num_ues_added]->n_rlc_pdu > 0)) {
+		    // Now compute number of required RBs for total sdu length
+		    // Assume RAH format 2
+		    // adjust  header lengths
+		    LOG_D(FLEXRAN_AGENT, "We have %d bytes to transfer\n",
+			  sdu_length_total);
+		    if (header_len != 0) {
+			LOG_D(FLEXRAN_AGENT, "Header length was %d ",
+			      header_len);
+			header_len_last--;
+			header_len -= header_len_last;
+			LOG_D(FLEXRAN_AGENT, "so we resized it to %d\n",
+			      header_len);
+		    }
+
+		    /* if (header_len == 2 || header_len == 3) { //Only one SDU, remove length field */
+		    /*   header_len = 1; */
+		    /* } else { //Remove length field from the last SDU */
+		    /*   header_len--; */
+		    /* } */
+
+		    mcs_tmp = mcs;
+		    if (mcs_tmp == 0) {
+			nb_rb = 4;	// don't let the TBS get too small
+		    } else {
+			nb_rb = min_rb_unit[CC_id];
+		    }
+
+		    LOG_D(MAC,
+			  "[TEST]The initial number of resource blocks was %d\n",
+			  nb_rb);
+		    LOG_D(MAC, "[TEST] The initial mcs was %d\n", mcs_tmp);
+
+		    TBS = mac_xface->get_TBS_DL(mcs_tmp, nb_rb);
+		    LOG_D(MAC,
+			  "[TEST]The TBS during rate matching was %d\n",
+			  TBS);
+
+		    while (TBS < (sdu_length_total + header_len + ta_len)) {
+			nb_rb += min_rb_unit[CC_id];	//
+			LOG_D(MAC,
+			      "[TEST]Had to increase the number of RBs\n");
+			if (nb_rb > nb_available_rb) {	// if we've gone beyond the maximum number of RBs
+			    // (can happen if N_RB_DL is odd)
+			    TBS =
+				mac_xface->get_TBS_DL(mcs_tmp,
+						      nb_available_rb);
+			    nb_rb = nb_available_rb;
+			    break;
+			}
+
+			TBS = mac_xface->get_TBS_DL(mcs_tmp, nb_rb);
+		    }
+
+		    if (nb_rb == ue_sched_ctl->pre_nb_available_rbs[CC_id]) {
+			LOG_D(MAC,
+			      "[TEST]We had the exact number of rbs. Time to fill the rballoc subband\n");
+			for (j = 0; j < flexran_get_N_RBG(mod_id, CC_id); j++) {	// for indicating the rballoc for each sub-band
+			    UE_list->UE_template[CC_id][UE_id].
+				rballoc_subband[harq_pid][j] =
+				ue_sched_ctl->rballoc_sub_UE[CC_id][j];
+			}
+		    } else {
+			nb_rb_temp = nb_rb;
+			j = 0;
+			LOG_D(MAC,
+			      "[TEST]Will only partially fill the bitmap\n");
+			while ((nb_rb_temp > 0)
+			       && (j < flexran_get_N_RBG(mod_id, CC_id))) {
+			    if (ue_sched_ctl->rballoc_sub_UE[CC_id][j] ==
+				1) {
+				UE_list->
+				    UE_template[CC_id]
+				    [UE_id].rballoc_subband[harq_pid][j] =
+				    ue_sched_ctl->rballoc_sub_UE[CC_id][j];
+				if ((j ==
+				     flexran_get_N_RBG(mod_id, CC_id) - 1)
+				    &&
+				    ((flexran_get_N_RB_DL(mod_id, CC_id) ==
+				      25)
+				     || (flexran_get_N_RB_DL(mod_id, CC_id)
+					 == 50))) {
+				    nb_rb_temp =
+					nb_rb_temp - min_rb_unit[CC_id] +
+					1;
+				} else {
+				    nb_rb_temp =
+					nb_rb_temp - min_rb_unit[CC_id];
+				}
+			    }
+			    j = j + 1;
+			}
+		    }
+
+		    PHY_vars_eNB_g[mod_id][CC_id]->
+			mu_mimo_mode[UE_id].pre_nb_available_rbs = nb_rb;
+		    PHY_vars_eNB_g[mod_id][CC_id]->
+			mu_mimo_mode[UE_id].dl_pow_off =
+			ue_sched_ctl->dl_pow_off[CC_id];
+
+		    for (j = 0; j < flexran_get_N_RBG(mod_id, CC_id); j++) {
+			PHY_vars_eNB_g[mod_id][CC_id]->
+			    mu_mimo_mode[UE_id].rballoc_sub[j] =
+			    UE_list->
+			    UE_template[CC_id][UE_id].rballoc_subband
+			    [harq_pid][j];
+		    }
+
+		    // decrease mcs until TBS falls below required length
+		    while ((TBS > (sdu_length_total + header_len + ta_len))
+			   && (mcs_tmp > 0)) {
+			mcs_tmp--;
+			TBS = mac_xface->get_TBS_DL(mcs_tmp, nb_rb);
+		    }
+
+		    // if we have decreased too much or we don't have enough RBs, increase MCS
+		    while ((TBS < (sdu_length_total + header_len + ta_len))
+			   && (((ue_sched_ctl->dl_pow_off[CC_id] > 0)
+				&& (mcs_tmp < 28))
+			       || ((ue_sched_ctl->dl_pow_off[CC_id] == 0)
+				   && (mcs_tmp <= 15)))) {
+			mcs_tmp++;
+			TBS = mac_xface->get_TBS_DL(mcs_tmp, nb_rb);
+		    }
+
+		    dci_tbs = TBS;
+		    mcs = mcs_tmp;
+		    LOG_D(FLEXRAN_AGENT, "Final mcs was %d\n", mcs);
+
+		    dl_dci->has_aggr_level = 1;
+		    dl_dci->aggr_level = aggregation;
+
+		    UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid] =
+			nb_rb;
+
+		    if (flexran_get_duplex_mode(mod_id, CC_id) ==
+			PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
+			UE_list->UE_template[CC_id][UE_id].DAI++;
+			//  printf("DAI update: subframeP %d: UE %d, DAI %d\n",subframeP,UE_id,UE_list->UE_template[CC_id][UE_id].DAI);
+			//#warning only for 5MHz channel
+			update_ul_dci(mod_id, CC_id, rnti,
+				      UE_list->UE_template[CC_id][UE_id].
+				      DAI);
+		    }
+		    // do PUCCH power control
+		    // this is the normalized RX power
+		    normalized_rx_power = flexran_get_p0_pucch_dbm(mod_id, UE_id, CC_id);	//eNB_UE_stats->Po_PUCCH_dBm; 
+		    target_rx_power = flexran_get_p0_nominal_pucch(mod_id, CC_id) + 20;	//mac_xface->get_target_pucch_rx_power(mod_id, CC_id) + 20;
+		    // this assumes accumulated tpc
+		    // make sure that we are only sending a tpc update once a frame, otherwise the control loop will freak out
+		    int32_t framex10psubframe =
+			UE_list->UE_template[CC_id][UE_id].
+			pucch_tpc_tx_frame * 10 +
+			UE_list->UE_template[CC_id][UE_id].
+			pucch_tpc_tx_subframe;
+
+		    if (((framex10psubframe + 10) <= (frame * 10 + subframe)) ||	//normal case
+			((framex10psubframe > (frame * 10 + subframe)) && (((10240 - framex10psubframe + frame * 10 + subframe) >= 10))))	//frame wrap-around
+			if (flexran_get_p0_pucch_status
+			    (mod_id, UE_id, CC_id) == 1) {
+			    flexran_update_p0_pucch(mod_id, UE_id, CC_id);
+
+			    UE_list->
+				UE_template[CC_id]
+				[UE_id].pucch_tpc_tx_frame = frame;
+			    UE_list->
+				UE_template[CC_id]
+				[UE_id].pucch_tpc_tx_subframe = subframe;
+			    if (normalized_rx_power >
+				(target_rx_power + 1)) {
+				tpc = 0;	//-1
+				tpc_accumulated--;
+			    } else if (normalized_rx_power <
+				       (target_rx_power - 1)) {
+				tpc = 2;	//+1
+				tpc_accumulated++;
+			    } else {
+				tpc = 1;	//0
+			    }
+			    LOG_D(MAC,
+				  "[eNB %d] DLSCH scheduler: frame %d, subframe %d, harq_pid %d, tpc %d, accumulated %d, normalized/target rx power %d/%d\n",
+				  mod_id, frame, subframe, harq_pid, tpc,
+				  tpc_accumulated, normalized_rx_power,
+				  target_rx_power);
+			}	// Po_PUCCH has been updated 
+			else {
+			    tpc = 1;	//0
+			}	// time to do TPC update 
+		    else {
+			tpc = 1;	//0
+		    }
+
+		    for (i = 0;
+			 i <
+			 PHY_vars_eNB_g[mod_id][CC_id]->frame_parms.N_RBG;
+			 i++) {
+			rballoc_sub[i] =
+			    UE_list->
+			    UE_template[CC_id][UE_id].rballoc_subband
+			    [harq_pid][i];
+		    }
+
+		    // Toggle NDI
+		    LOG_D(MAC,
+			  "CC_id %d Frame %d, subframeP %d: Toggling Format1 NDI for UE %d (rnti %x/%d) oldNDI %d\n",
+			  CC_id, frame, subframe, UE_id,
+			  UE_list->UE_template[CC_id][UE_id].rnti,
+			  harq_pid,
+			  UE_list->UE_template[CC_id][UE_id].
+			  oldNDI[harq_pid]);
+		    UE_list->UE_template[CC_id][UE_id].oldNDI[harq_pid] =
+			1 -
+			UE_list->UE_template[CC_id][UE_id].
+			oldNDI[harq_pid];
+		    ndi =
+			UE_list->UE_template[CC_id][UE_id].
+			oldNDI[harq_pid];
+
+		    UE_list->UE_template[CC_id][UE_id].mcs[harq_pid] = mcs;
+		    UE_list->UE_template[CC_id][UE_id].oldTPC[harq_pid] =
+			tpc;
+
+		    // Increase the pointer for the number of scheduled UEs
+		    num_ues_added++;
+		    ue_has_transmission = 1;
+		} else {	// There is no data from RLC or MAC header, so don't schedule
+		    ue_has_transmission = 0;
+		}
+	    }			// End of new scheduling
+
+	    // If we has transmission or retransmission
+	    if (ue_has_transmission) {
+		switch (mac_xface->
+			get_transmission_mode(mod_id, CC_id, rnti)) {
+		case 1:
+		case 2:
+		default:
+		    dl_dci->has_res_alloc = 1;
+		    dl_dci->res_alloc = 0;
+		    dl_dci->has_vrb_format = 1;
+		    dl_dci->vrb_format =
+			PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
+		    dl_dci->has_format = 1;
+		    dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1;
+		    dl_dci->has_rb_bitmap = 1;
+		    dl_dci->rb_bitmap =
+			allocate_prbs_sub(nb_rb, rballoc_sub);
+		    dl_dci->has_rb_shift = 1;
+		    dl_dci->rb_shift = 0;
+		    dl_dci->n_ndi = 1;
+		    dl_dci->ndi =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_ndi);
+		    dl_dci->ndi[0] = ndi;
+		    dl_dci->n_rv = 1;
+		    dl_dci->rv =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_rv);
+		    dl_dci->rv[0] = round & 3;
+		    dl_dci->has_tpc = 1;
+		    dl_dci->tpc = tpc;
+		    dl_dci->n_mcs = 1;
+		    dl_dci->mcs =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_mcs);
+		    dl_dci->mcs[0] = mcs;
+		    dl_dci->n_tbs_size = 1;
+		    dl_dci->tbs_size =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_tbs_size);
+		    dl_dci->tbs_size[0] = dci_tbs;
+		    if (flexran_get_duplex_mode(mod_id, CC_id) ==
+			PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
+			dl_dci->has_dai = 1;
+			dl_dci->dai =
+			    (UE_list->UE_template[CC_id][UE_id].DAI -
+			     1) & 3;
+		    }
+		    break;
+		case 3:
+		    dl_dci->has_res_alloc = 1;
+		    dl_dci->res_alloc = 0;
+		    dl_dci->has_vrb_format = 1;
+		    dl_dci->vrb_format =
+			PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
+		    dl_dci->has_format = 1;
+		    dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A;
+		    dl_dci->has_rb_bitmap = 1;
+		    dl_dci->rb_bitmap =
+			allocate_prbs_sub(nb_rb, rballoc_sub);
+		    dl_dci->has_rb_shift = 1;
+		    dl_dci->rb_shift = 0;
+		    dl_dci->n_ndi = 2;
+		    dl_dci->ndi =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_ndi);
+		    dl_dci->ndi[0] = ndi;
+		    dl_dci->ndi[1] = ndi;
+		    dl_dci->n_rv = 2;
+		    dl_dci->rv =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_rv);
+		    dl_dci->rv[0] = round & 3;
+		    dl_dci->rv[1] = round & 3;
+		    dl_dci->has_tpc = 1;
+		    dl_dci->tpc = tpc;
+		    dl_dci->n_mcs = 2;
+		    dl_dci->mcs =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_mcs);
+		    dl_dci->mcs[0] = mcs;
+		    dl_dci->mcs[1] = mcs;
+		    dl_dci->n_tbs_size = 2;
+		    dl_dci->tbs_size =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_tbs_size);
+		    dl_dci->tbs_size[0] = dci_tbs;
+		    dl_dci->tbs_size[1] = dci_tbs;
+		    if (flexran_get_duplex_mode(mod_id, CC_id) ==
+			PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
+			dl_dci->has_dai = 1;
+			dl_dci->dai =
+			    (UE_list->UE_template[CC_id][UE_id].DAI -
+			     1) & 3;
+		    }
+		    break;
+		case 4:
+		    dl_dci->has_res_alloc = 1;
+		    dl_dci->res_alloc = 0;
+		    dl_dci->has_vrb_format = 1;
+		    dl_dci->vrb_format =
+			PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
+		    dl_dci->has_format = 1;
+		    dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A;
+		    dl_dci->has_rb_bitmap = 1;
+		    dl_dci->rb_bitmap =
+			allocate_prbs_sub(nb_rb, rballoc_sub);
+		    dl_dci->has_rb_shift = 1;
+		    dl_dci->rb_shift = 0;
+		    dl_dci->n_ndi = 2;
+		    dl_dci->ndi =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_ndi);
+		    dl_dci->ndi[0] = ndi;
+		    dl_dci->ndi[1] = ndi;
+		    dl_dci->n_rv = 2;
+		    dl_dci->rv =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_rv);
+		    dl_dci->rv[0] = round & 3;
+		    dl_dci->rv[1] = round & 3;
+		    dl_dci->has_tpc = 1;
+		    dl_dci->tpc = tpc;
+		    dl_dci->n_mcs = 2;
+		    dl_dci->mcs =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_mcs);
+		    dl_dci->mcs[0] = mcs;
+		    dl_dci->mcs[1] = mcs;
+		    dl_dci->n_tbs_size = 2;
+		    dl_dci->tbs_size =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_tbs_size);
+		    dl_dci->tbs_size[0] = dci_tbs;
+		    dl_dci->tbs_size[1] = dci_tbs;
+		    if (flexran_get_duplex_mode(mod_id, CC_id) ==
+			PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
+			dl_dci->has_dai = 1;
+			dl_dci->dai =
+			    (UE_list->UE_template[CC_id][UE_id].DAI -
+			     1) & 3;
+		    }
+		    break;
+		case 5:
+		    dl_dci->has_res_alloc = 1;
+		    dl_dci->res_alloc = 0;
+		    dl_dci->has_vrb_format = 1;
+		    dl_dci->vrb_format =
+			PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
+		    dl_dci->has_format = 1;
+		    dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D;
+		    dl_dci->has_rb_bitmap = 1;
+		    dl_dci->rb_bitmap =
+			allocate_prbs_sub(nb_rb, rballoc_sub);
+		    dl_dci->has_rb_shift = 1;
+		    dl_dci->rb_shift = 0;
+		    dl_dci->n_ndi = 1;
+		    dl_dci->ndi[0] = ndi;
+		    dl_dci->n_rv = 1;
+		    dl_dci->rv =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_rv);
+		    dl_dci->rv[0] = round & 3;
+		    dl_dci->has_tpc = 1;
+		    dl_dci->tpc = tpc;
+		    dl_dci->n_mcs = 1;
+		    dl_dci->mcs =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_mcs);
+		    dl_dci->mcs[0] = mcs;
+		    dl_dci->n_tbs_size = 1;
+		    dl_dci->tbs_size =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_tbs_size);
+		    dl_dci->tbs_size[0] = dci_tbs;
+		    if (flexran_get_duplex_mode(mod_id, CC_id) ==
+			PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
+			dl_dci->has_dai = 1;
+			dl_dci->dai =
+			    (UE_list->UE_template[CC_id][UE_id].DAI -
+			     1) & 3;
+		    }
+
+		    if (ue_sched_ctl->dl_pow_off[CC_id] == 2) {
+			ue_sched_ctl->dl_pow_off[CC_id] = 1;
+		    }
+
+		    dl_dci->has_dl_power_offset = 1;
+		    dl_dci->dl_power_offset =
+			ue_sched_ctl->dl_pow_off[CC_id];
+		    dl_dci->has_precoding_info = 1;
+		    dl_dci->precoding_info = 5;	// Is this right??
+
+		    break;
+		case 6:
+		    dl_dci->has_res_alloc = 1;
+		    dl_dci->res_alloc = 0;
+		    dl_dci->has_vrb_format = 1;
+		    dl_dci->vrb_format =
+			PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
+		    dl_dci->has_format = 1;
+		    dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D;
+		    dl_dci->has_rb_bitmap = 1;
+		    dl_dci->rb_bitmap =
+			allocate_prbs_sub(nb_rb, rballoc_sub);
+		    dl_dci->has_rb_shift = 1;
+		    dl_dci->rb_shift = 0;
+		    dl_dci->n_ndi = 1;
+		    dl_dci->ndi =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_ndi);
+		    dl_dci->ndi[0] = ndi;
+		    dl_dci->n_rv = 1;
+		    dl_dci->rv =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_rv);
+		    dl_dci->rv[0] = round & 3;
+		    dl_dci->has_tpc = 1;
+		    dl_dci->tpc = tpc;
+		    dl_dci->n_mcs = 1;
+		    dl_dci->mcs =
+			(uint32_t *) malloc(sizeof(uint32_t) *
+					    dl_dci->n_mcs);
+		    dl_dci->mcs[0] = mcs;
+		    if (flexran_get_duplex_mode(mod_id, CC_id) ==
+			PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
+			dl_dci->has_dai = 1;
+			dl_dci->dai =
+			    (UE_list->UE_template[CC_id][UE_id].DAI -
+			     1) & 3;
+		    }
+
+		    dl_dci->has_dl_power_offset = 1;
+		    dl_dci->dl_power_offset =
+			ue_sched_ctl->dl_pow_off[CC_id];
+		    dl_dci->has_precoding_info = 1;
+		    dl_dci->precoding_info = 5;	// Is this right??
+		    break;
 		}
-	      }
-	      j = j + 1;
 	    }
-	  }
-
-	  nb_available_rb -= nb_rb;
-	  PHY_vars_eNB_g[mod_id][CC_id]->mu_mimo_mode[UE_id].pre_nb_available_rbs = nb_rb;
-	  PHY_vars_eNB_g[mod_id][CC_id]->mu_mimo_mode[UE_id].dl_pow_off = ue_sched_ctl->dl_pow_off[CC_id];
-	  
-	  for(j=0; j < flexran_get_N_RBG(mod_id, CC_id); j++) {
-	    PHY_vars_eNB_g[mod_id][CC_id]->mu_mimo_mode[UE_id].rballoc_sub[j] = UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j];
-	    rballoc_sub[j] = UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j];
-	  }
-
-	  // Keep the old NDI, do not toggle
-	  ndi = UE_list->UE_template[CC_id][UE_id].oldNDI[harq_pid];
-	  tpc = UE_list->UE_template[CC_id][UE_id].oldTPC[harq_pid];
-	  UE_list->UE_template[CC_id][UE_id].mcs[harq_pid] = mcs;
-
-	  ue_has_transmission = 1;
-	  num_ues_added++;
-	} else {
-	  LOG_D(MAC,"[eNB %d] Frame %d CC_id %d : don't schedule UE %d, its retransmission takes more resources than we have\n",
-                mod_id, frame, CC_id, UE_id);
-	  ue_has_transmission = 0;
-	}
-	//End of retransmission
-      } else { /* This is a potentially new SDU opportunity */
-	rlc_status.bytes_in_buffer = 0;
-        // Now check RLC information to compute number of required RBs
-        // get maximum TBS size for RLC request
-        //TBS = mac_xface->get_TBS(eNB_UE_stats->DL_cqi[0]<<1,nb_available_rb);
-        TBS = mac_xface->get_TBS_DL(mcs, nb_available_rb);
-	dci_tbs = TBS;
-	LOG_D(FLEXRAN_AGENT, "TBS is %d\n", TBS);
-	
-        // check first for RLC data on DCCH
-        // add the length for  all the control elements (timing adv, drx, etc) : header + payload
-
-	ta_len = (ue_sched_ctl->ta_update != 0) ? 2 : 0;
-	
-	dl_data[num_ues_added]->n_ce_bitmap = 2;
-	dl_data[num_ues_added]->ce_bitmap = (uint32_t *) calloc(2, sizeof(uint32_t));
-
-	if (ta_len > 0) {
-	  ce_flags |= PROTOCOL__FLEX_CE_TYPE__FLPCET_TA;
-	}
 
-	/*TODO: Add other flags if DRX and other CE are required*/
-	
-	// Add the control element flags to the flexran message
-	dl_data[num_ues_added]->ce_bitmap[0] = ce_flags;
-	dl_data[num_ues_added]->ce_bitmap[1] = ce_flags;
-
-	// TODO : Need to prioritize DRBs
-	// Loop through the UE logical channels (DCCH, DCCH1, DTCH for now)
-	header_len = 0;
-	header_len_last = 0;
-	sdu_length_total = 0;
-	for (j = 1; j < NB_RB_MAX; j++) {
-	  header_len += 3;
-	  // Need to see if we have space for data from this channel
-	  if (dci_tbs - ta_len - header_len - sdu_length_total > 0) {
-	     LOG_D(MAC, "[TEST]Requested %d bytes from RLC buffer on channel %d during first call\n", dci_tbs-ta_len-header_len);
-	     //If we have space, we need to see how much data we can request at most (if any available)
-	     rlc_status = mac_rlc_status_ind(mod_id,
-					     rnti,
-					     mod_id,
-					     frame,
-						 subframe,
-					     ENB_FLAG_YES,
-					     MBMS_FLAG_NO,
-					     j,
-					     (dci_tbs - ta_len - header_len - sdu_length_total)); // transport block set size
-
-	     //If data are available in channel j
-	     if (rlc_status.bytes_in_buffer > 0) {
-	       LOG_D(MAC, "[TEST]Have %d bytes in DCCH buffer during first call\n", rlc_status.bytes_in_buffer);
-	       //Fill in as much as possible
-	       data_to_request = cmin(dci_tbs - ta_len - header_len - sdu_length_total, rlc_status.bytes_in_buffer);
-	       LOG_D(FLEXRAN_AGENT, "Will request %d bytes from channel %d\n", data_to_request, j);
-	       if (data_to_request < 128) { //The header will be one byte less
-		 header_len--;
-		 header_len_last = 2;
-	       } else {
-		 header_len_last = 3;
-	       }
-	       /* if (j == 1 || j == 2) {
-		  data_to_request+=0; 
-		  } */
-	       LOG_D(MAC, "[TEST]Will request %d from channel %d\n", data_to_request, j);
-	       rlc_pdus[channels_added] = (Protocol__FlexRlcPdu *) malloc(sizeof(Protocol__FlexRlcPdu));
-	       protocol__flex_rlc_pdu__init(rlc_pdus[channels_added]);
-	       rlc_pdus[channels_added]->n_rlc_pdu_tb = 2;
-	       rlc_pdus[channels_added]->rlc_pdu_tb = (Protocol__FlexRlcPduTb **) malloc(sizeof(Protocol__FlexRlcPduTb *) * 2);
-	       rlc_pdus[channels_added]->rlc_pdu_tb[0] = (Protocol__FlexRlcPduTb *) malloc(sizeof(Protocol__FlexRlcPduTb));
-	       protocol__flex_rlc_pdu_tb__init(rlc_pdus[channels_added]->rlc_pdu_tb[0]);
-	       rlc_pdus[channels_added]->rlc_pdu_tb[0]->has_logical_channel_id = 1;
-	       rlc_pdus[channels_added]->rlc_pdu_tb[0]->logical_channel_id = j;
-	       rlc_pdus[channels_added]->rlc_pdu_tb[0]->has_size = 1;
-	       rlc_pdus[channels_added]->rlc_pdu_tb[0]->size = data_to_request;
-	       rlc_pdus[channels_added]->rlc_pdu_tb[1] = (Protocol__FlexRlcPduTb *) malloc(sizeof(Protocol__FlexRlcPduTb));
-	       protocol__flex_rlc_pdu_tb__init(rlc_pdus[channels_added]->rlc_pdu_tb[1]);
-	       rlc_pdus[channels_added]->rlc_pdu_tb[1]->has_logical_channel_id = 1;
-	       rlc_pdus[channels_added]->rlc_pdu_tb[1]->logical_channel_id = j;
-	       rlc_pdus[channels_added]->rlc_pdu_tb[1]->has_size = 1;
-	       rlc_pdus[channels_added]->rlc_pdu_tb[1]->size = data_to_request;
-	       dl_data[num_ues_added]->n_rlc_pdu++;
-	       channels_added++;
-	       //Set this to the max value that we might request
-	       sdu_length_total += data_to_request;
-	     } else {
-	       //Take back the assumption of a header for this channel
-	       header_len -= 3;
-	     } //End rlc_status.bytes_in_buffer <= 0
-	  } //end of if dci_tbs - ta_len - header_len > 0
-	} // End of iterating the logical channels
-	
-	// Add rlc_pdus to the dl_data message
-	dl_data[num_ues_added]->rlc_pdu = (Protocol__FlexRlcPdu **) malloc(sizeof(Protocol__FlexRlcPdu *) *
-									  dl_data[num_ues_added]->n_rlc_pdu);
-	for (i = 0; i < dl_data[num_ues_added]->n_rlc_pdu; i++) {
-	  dl_data[num_ues_added]->rlc_pdu[i] = rlc_pdus[i];
+	    if (flexran_get_duplex_mode(mod_id, CC_id) ==
+		PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
+
+		/* TODO */
+		//set_ul_DAI(mod_id, UE_id, CC_id, frame, subframe, frame_parms);
+	    }
+	}			// UE_id loop
+    }				// CC_id loop
+
+    // Add all the dl_data elements to the flexran message
+    int offset = (*dl_info)->dl_mac_config_msg->n_dl_ue_data;
+    (*dl_info)->dl_mac_config_msg->n_dl_ue_data += num_ues_added;
+    if (num_ues_added > 0) {
+	(*dl_info)->dl_mac_config_msg->dl_ue_data =
+	    (Protocol__FlexDlData **)
+	    realloc((*dl_info)->dl_mac_config_msg->dl_ue_data,
+		    sizeof(Protocol__FlexDlData *) *
+		    ((*dl_info)->dl_mac_config_msg->n_dl_ue_data));
+	if ((*dl_info)->dl_mac_config_msg->dl_ue_data == NULL) {
+	    LOG_E(MAC, "Request for memory reallocation failed\n");
+	    return;
 	}
-	
-	if (header_len == 0) {
-	  LOG_D(FLEXRAN_AGENT, "Header was empty\n");
-	  header_len_last = 0;
-	} 
-	
-	// there is a payload
-        if ((dl_data[num_ues_added]->n_rlc_pdu > 0)) {
-	  // Now compute number of required RBs for total sdu length
-          // Assume RAH format 2
-          // adjust  header lengths
-	  LOG_D(FLEXRAN_AGENT, "We have %d bytes to transfer\n", sdu_length_total);
-	  if (header_len != 0) {
-	    LOG_D(FLEXRAN_AGENT, "Header length was %d ", header_len);
-	    header_len_last--;
-	    header_len -= header_len_last;
-	    LOG_D(FLEXRAN_AGENT, "so we resized it to %d\n", header_len);
-	  }
-	  
-	  /* if (header_len == 2 || header_len == 3) { //Only one SDU, remove length field */
-	  /*   header_len = 1; */
-	  /* } else { //Remove length field from the last SDU */
-	  /*   header_len--; */
-	  /* } */
-
-	  mcs_tmp = mcs;
-	  if (mcs_tmp == 0) {
-            nb_rb = 4;  // don't let the TBS get too small
-          } else {
-            nb_rb=min_rb_unit[CC_id];
-          }
-
-	  LOG_D(MAC,"[TEST]The initial number of resource blocks was %d\n", nb_rb);
-	  LOG_D(MAC,"[TEST] The initial mcs was %d\n", mcs_tmp);
-
-	  TBS = mac_xface->get_TBS_DL(mcs_tmp, nb_rb);
-	  LOG_D(MAC,"[TEST]The TBS during rate matching was %d\n", TBS);
-
-	  while (TBS < (sdu_length_total + header_len + ta_len))  {
-            nb_rb += min_rb_unit[CC_id];  //
-	    LOG_D(MAC, "[TEST]Had to increase the number of RBs\n");
-            if (nb_rb > nb_available_rb) { // if we've gone beyond the maximum number of RBs
-              // (can happen if N_RB_DL is odd)
-              TBS = mac_xface->get_TBS_DL(mcs_tmp, nb_available_rb);
-              nb_rb = nb_available_rb;
-              break;
-            }
-
-            TBS = mac_xface->get_TBS_DL(mcs_tmp, nb_rb);
-          }
-
-	  if(nb_rb == ue_sched_ctl->pre_nb_available_rbs[CC_id]) {
-	    LOG_D(MAC, "[TEST]We had the exact number of rbs. Time to fill the rballoc subband\n");
-            for(j = 0; j < flexran_get_N_RBG(mod_id, CC_id); j++) { // for indicating the rballoc for each sub-band
-              UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j] = ue_sched_ctl->rballoc_sub_UE[CC_id][j];
-            }
-          } else {
-	    nb_rb_temp = nb_rb;
-            j = 0;
-	    LOG_D(MAC, "[TEST]Will only partially fill the bitmap\n");
-	    while((nb_rb_temp > 0) && (j < flexran_get_N_RBG(mod_id, CC_id))) {
-              if(ue_sched_ctl->rballoc_sub_UE[CC_id][j] == 1) {
-                UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j] = ue_sched_ctl->rballoc_sub_UE[CC_id][j];
-                if ((j == flexran_get_N_RBG(mod_id, CC_id) - 1) &&
-                    ((flexran_get_N_RB_DL(mod_id, CC_id) == 25)||
-                     (flexran_get_N_RB_DL(mod_id, CC_id) == 50))) {
-                  nb_rb_temp = nb_rb_temp - min_rb_unit[CC_id] + 1;
-                } else {
-                  nb_rb_temp = nb_rb_temp - min_rb_unit[CC_id];
-                }
-              }
-              j = j+1;
-            }
-	  }
-	  
-	  PHY_vars_eNB_g[mod_id][CC_id]->mu_mimo_mode[UE_id].pre_nb_available_rbs = nb_rb;
-          PHY_vars_eNB_g[mod_id][CC_id]->mu_mimo_mode[UE_id].dl_pow_off = ue_sched_ctl->dl_pow_off[CC_id];
-
-	  for(j = 0; j < flexran_get_N_RBG(mod_id, CC_id); j++) {
-            PHY_vars_eNB_g[mod_id][CC_id]->mu_mimo_mode[UE_id].rballoc_sub[j] = UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][j];
-          }
-
-	  // decrease mcs until TBS falls below required length
-          while ((TBS > (sdu_length_total + header_len + ta_len)) && (mcs_tmp > 0)) {
-            mcs_tmp--;
-            TBS = mac_xface->get_TBS_DL(mcs_tmp, nb_rb);
-          }
-
-	  // if we have decreased too much or we don't have enough RBs, increase MCS
-          while ((TBS < (sdu_length_total + header_len + ta_len)) &&
-		 ((( ue_sched_ctl->dl_pow_off[CC_id] > 0) && (mcs_tmp < 28))											     || ( (ue_sched_ctl->dl_pow_off[CC_id]==0) && (mcs_tmp <= 15)))) {
-            mcs_tmp++;
-            TBS = mac_xface->get_TBS_DL(mcs_tmp, nb_rb);
-          }
-
-	  dci_tbs = TBS;
-	  mcs = mcs_tmp;
-	  LOG_D(FLEXRAN_AGENT, "Final mcs was %d\n", mcs); 
-	  
-	  dl_dci->has_aggr_level = 1;
-	  dl_dci->aggr_level = aggregation;
-	  
-          UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid] = nb_rb;
-
-	  if (flexran_get_duplex_mode(mod_id, CC_id) == PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-            UE_list->UE_template[CC_id][UE_id].DAI++;
-            //  printf("DAI update: subframeP %d: UE %d, DAI %d\n",subframeP,UE_id,UE_list->UE_template[CC_id][UE_id].DAI);
-	    //#warning only for 5MHz channel
-            update_ul_dci(mod_id, CC_id, rnti, UE_list->UE_template[CC_id][UE_id].DAI);
-          }
-
-	  // do PUCCH power control
-          // this is the normalized RX power
-	  normalized_rx_power = flexran_get_p0_pucch_dbm(mod_id,UE_id, CC_id); //eNB_UE_stats->Po_PUCCH_dBm; 
-	  target_rx_power = flexran_get_p0_nominal_pucch(mod_id, CC_id) + 20; //mac_xface->get_target_pucch_rx_power(mod_id, CC_id) + 20;
-	  // this assumes accumulated tpc
-	  // make sure that we are only sending a tpc update once a frame, otherwise the control loop will freak out
-	  int32_t framex10psubframe = UE_list->UE_template[CC_id][UE_id].pucch_tpc_tx_frame*10+UE_list->UE_template[CC_id][UE_id].pucch_tpc_tx_subframe;
-
-	  if (((framex10psubframe+10)<=(frame*10+subframe)) || //normal case
-	      ((framex10psubframe>(frame*10+subframe)) && (((10240-framex10psubframe+frame*10+subframe)>=10)))) //frame wrap-around
-	    if (flexran_get_p0_pucch_status(mod_id, UE_id, CC_id) == 1) {
-  	      flexran_update_p0_pucch(mod_id, UE_id, CC_id);
-
-	      UE_list->UE_template[CC_id][UE_id].pucch_tpc_tx_frame = frame;
-	      UE_list->UE_template[CC_id][UE_id].pucch_tpc_tx_subframe = subframe;
-	      if (normalized_rx_power>(target_rx_power+1)) {
-		tpc = 0; //-1
-		tpc_accumulated--;
-	      } else if (normalized_rx_power<(target_rx_power-1)) {
-		tpc = 2; //+1
-		tpc_accumulated++;
-	      } else {
-		tpc = 1; //0
-	      }
-	      LOG_D(MAC,"[eNB %d] DLSCH scheduler: frame %d, subframe %d, harq_pid %d, tpc %d, accumulated %d, normalized/target rx power %d/%d\n",
-		    mod_id, frame, subframe, harq_pid, tpc,
-		    tpc_accumulated, normalized_rx_power, target_rx_power);
-	    } // Po_PUCCH has been updated 
-	    else {
-	      tpc = 1; //0
-	    } // time to do TPC update 
-	  else {
-	    tpc = 1; //0
-	  }
-
-	  for(i=0; i<PHY_vars_eNB_g[mod_id][CC_id]->frame_parms.N_RBG; i++) {
-	    rballoc_sub[i] = UE_list->UE_template[CC_id][UE_id].rballoc_subband[harq_pid][i];
-          }	
-
-	   // Toggle NDI
-          LOG_D(MAC,"CC_id %d Frame %d, subframeP %d: Toggling Format1 NDI for UE %d (rnti %x/%d) oldNDI %d\n",
-                CC_id, frame, subframe, UE_id,
-                UE_list->UE_template[CC_id][UE_id].rnti,harq_pid, UE_list->UE_template[CC_id][UE_id].oldNDI[harq_pid]);
-          UE_list->UE_template[CC_id][UE_id].oldNDI[harq_pid]= 1 - UE_list->UE_template[CC_id][UE_id].oldNDI[harq_pid];
-	  ndi =  UE_list->UE_template[CC_id][UE_id].oldNDI[harq_pid];
-	  
-	  UE_list->UE_template[CC_id][UE_id].mcs[harq_pid] = mcs;
-	  UE_list->UE_template[CC_id][UE_id].oldTPC[harq_pid] = tpc;
-
-	  // Increase the pointer for the number of scheduled UEs
-	  num_ues_added++;
-	  ue_has_transmission = 1;
-	}  else { // There is no data from RLC or MAC header, so don't schedule
-	  ue_has_transmission = 0;
+	for (i = 0; i < num_ues_added; i++) {
+	    (*dl_info)->dl_mac_config_msg->dl_ue_data[offset + i] =
+		dl_data[i];
 	}
-      } // End of new scheduling
-      
-      // If we has transmission or retransmission
-      if (ue_has_transmission) {
-	switch (mac_xface->get_transmission_mode(mod_id, CC_id, rnti)) {
-	case 1:
-	case 2:
-	default:
-	  dl_dci->has_res_alloc = 1;
-	  dl_dci->res_alloc = 0;
-	  dl_dci->has_vrb_format = 1;
-	  dl_dci->vrb_format = PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
-	  dl_dci->has_format = 1;
-	  dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1;
-	  dl_dci->has_rb_bitmap = 1;
-	  dl_dci->rb_bitmap = allocate_prbs_sub(nb_rb, rballoc_sub);
-	  dl_dci->has_rb_shift = 1;
-	  dl_dci->rb_shift = 0;
-	  dl_dci->n_ndi = 1;
-	  dl_dci->ndi = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_ndi);
-	  dl_dci->ndi[0] = ndi;
-	  dl_dci->n_rv = 1;
-	  dl_dci->rv = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_rv);
-	  dl_dci->rv[0] = round & 3;
-	  dl_dci->has_tpc = 1;
-	  dl_dci->tpc = tpc;
-	  dl_dci->n_mcs = 1;
-	  dl_dci->mcs = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_mcs);
-	  dl_dci->mcs[0] = mcs;
-	  dl_dci->n_tbs_size = 1;
-	  dl_dci->tbs_size = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_tbs_size);
-	  dl_dci->tbs_size[0] = dci_tbs;
-	  if (flexran_get_duplex_mode(mod_id, CC_id) == PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-	    dl_dci->has_dai = 1;
-	    dl_dci->dai = (UE_list->UE_template[CC_id][UE_id].DAI-1)&3;
-	  }
-	  break;
-	case 3:
-	  dl_dci->has_res_alloc = 1;
-	  dl_dci->res_alloc = 0;
-	  dl_dci->has_vrb_format = 1;
-	  dl_dci->vrb_format = PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
-	  dl_dci->has_format = 1;
-	  dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A;
-	  dl_dci->has_rb_bitmap = 1;
-	  dl_dci->rb_bitmap = allocate_prbs_sub(nb_rb, rballoc_sub);
-	  dl_dci->has_rb_shift = 1;
-	  dl_dci->rb_shift = 0;
-	  dl_dci->n_ndi = 2;
-	  dl_dci->ndi = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_ndi);
-	  dl_dci->ndi[0] = ndi;
-	  dl_dci->ndi[1] = ndi;
-	  dl_dci->n_rv = 2;
-	  dl_dci->rv = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_rv);
-	  dl_dci->rv[0] = round & 3;
-	  dl_dci->rv[1] = round & 3;
-	  dl_dci->has_tpc = 1;
-	  dl_dci->tpc = tpc;
-	  dl_dci->n_mcs = 2;
-	  dl_dci->mcs = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_mcs);
-	  dl_dci->mcs[0] = mcs;
-	  dl_dci->mcs[1] = mcs;
-	  dl_dci->n_tbs_size = 2;
-	  dl_dci->tbs_size = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_tbs_size);
-	  dl_dci->tbs_size[0] = dci_tbs;
-	  dl_dci->tbs_size[1] = dci_tbs;
-	  if (flexran_get_duplex_mode(mod_id, CC_id) == PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-	    dl_dci->has_dai = 1;
-	    dl_dci->dai = (UE_list->UE_template[CC_id][UE_id].DAI-1)&3;
-	  }
-	  break;
-	case 4:
-	  dl_dci->has_res_alloc = 1;
-	  dl_dci->res_alloc = 0;
-	  dl_dci->has_vrb_format = 1;
-	  dl_dci->vrb_format = PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
-	  dl_dci->has_format = 1;
-	  dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_2A;
-	  dl_dci->has_rb_bitmap = 1;
-	  dl_dci->rb_bitmap = allocate_prbs_sub(nb_rb, rballoc_sub);
-	  dl_dci->has_rb_shift = 1;
-	  dl_dci->rb_shift = 0;
-	  dl_dci->n_ndi = 2;
-	  dl_dci->ndi = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_ndi);
-	  dl_dci->ndi[0] = ndi;
-	  dl_dci->ndi[1] = ndi;
-	  dl_dci->n_rv = 2;
-	  dl_dci->rv = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_rv);
-	  dl_dci->rv[0] = round & 3;
-	  dl_dci->rv[1] = round & 3;
-	  dl_dci->has_tpc = 1;
-	  dl_dci->tpc = tpc;
-	  dl_dci->n_mcs = 2;
-	  dl_dci->mcs = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_mcs);
-	  dl_dci->mcs[0] = mcs;
-	  dl_dci->mcs[1] = mcs;
-	  dl_dci->n_tbs_size = 2;
-	  dl_dci->tbs_size = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_tbs_size);
-	  dl_dci->tbs_size[0] = dci_tbs;
-	  dl_dci->tbs_size[1] = dci_tbs;
-	  if (flexran_get_duplex_mode(mod_id, CC_id) == PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-	    dl_dci->has_dai = 1;
-	    dl_dci->dai = (UE_list->UE_template[CC_id][UE_id].DAI-1)&3;
-	  }
-	  break;
-	case 5:
-	  dl_dci->has_res_alloc = 1;
-	  dl_dci->res_alloc = 0;
-	  dl_dci->has_vrb_format = 1;
-	  dl_dci->vrb_format = PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
-	  dl_dci->has_format = 1;
-	  dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D;
-	  dl_dci->has_rb_bitmap = 1;
-	  dl_dci->rb_bitmap = allocate_prbs_sub(nb_rb, rballoc_sub);
-	  dl_dci->has_rb_shift = 1;
-	  dl_dci->rb_shift = 0;
-	  dl_dci->n_ndi = 1;
-	  dl_dci->ndi[0] = ndi;
-	  dl_dci->n_rv = 1;
-	  dl_dci->rv = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_rv);
-	  dl_dci->rv[0] = round & 3;
-	  dl_dci->has_tpc = 1;
-	  dl_dci->tpc = tpc;
-	  dl_dci->n_mcs = 1;
-	  dl_dci->mcs = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_mcs);
-	  dl_dci->mcs[0] = mcs;
-	  dl_dci->n_tbs_size = 1;
-	  dl_dci->tbs_size = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_tbs_size);
-	  dl_dci->tbs_size[0] = dci_tbs;
-	  if (flexran_get_duplex_mode(mod_id, CC_id) == PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-	    dl_dci->has_dai = 1;
-	    dl_dci->dai = (UE_list->UE_template[CC_id][UE_id].DAI-1)&3;
-	  }
-	  
-	  if(ue_sched_ctl->dl_pow_off[CC_id] == 2) {
-	    ue_sched_ctl->dl_pow_off[CC_id] = 1;
-	  }
-	  
-	  dl_dci->has_dl_power_offset = 1;
-	  dl_dci->dl_power_offset = ue_sched_ctl->dl_pow_off[CC_id];
-	  dl_dci->has_precoding_info = 1;
-	  dl_dci->precoding_info = 5; // Is this right??
-	  
-	  break;
-	case 6:
-	  dl_dci->has_res_alloc = 1;
-	  dl_dci->res_alloc = 0;
-	  dl_dci->has_vrb_format = 1;
-	  dl_dci->vrb_format = PROTOCOL__FLEX_VRB_FORMAT__FLVRBF_LOCALIZED;
-	  dl_dci->has_format = 1;
-	  dl_dci->format = PROTOCOL__FLEX_DCI_FORMAT__FLDCIF_1D;
-	  dl_dci->has_rb_bitmap = 1;
-	  dl_dci->rb_bitmap = allocate_prbs_sub(nb_rb, rballoc_sub);
-	  dl_dci->has_rb_shift = 1;
-	  dl_dci->rb_shift = 0;
-	  dl_dci->n_ndi = 1;
-	  dl_dci->ndi = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_ndi);
-	  dl_dci->ndi[0] = ndi;
-	  dl_dci->n_rv = 1;
-	  dl_dci->rv = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_rv);
-	  dl_dci->rv[0] = round & 3;
-	  dl_dci->has_tpc = 1;
-	  dl_dci->tpc = tpc;
-	  dl_dci->n_mcs = 1;
-	  dl_dci->mcs = (uint32_t *) malloc(sizeof(uint32_t) * dl_dci->n_mcs);
-	  dl_dci->mcs[0] = mcs;
-	  if (flexran_get_duplex_mode(mod_id, CC_id) == PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-	    dl_dci->has_dai = 1;
-	    dl_dci->dai = (UE_list->UE_template[CC_id][UE_id].DAI-1)&3;
-	  }
-
-	  dl_dci->has_dl_power_offset = 1;
-	  dl_dci->dl_power_offset = ue_sched_ctl->dl_pow_off[CC_id];
-	  dl_dci->has_precoding_info = 1;
-	  dl_dci->precoding_info = 5; // Is this right??
-	  break;
-	}
-      }
-      
-      if (flexran_get_duplex_mode(mod_id, CC_id) == PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD) {
-        
-	/* TODO */
-	//set_ul_DAI(mod_id, UE_id, CC_id, frame, subframe, frame_parms);
-      }
-    } // UE_id loop
-   } // CC_id loop
-
-   // Add all the dl_data elements to the flexran message
-   int offset = (*dl_info)->dl_mac_config_msg->n_dl_ue_data;
-   (*dl_info)->dl_mac_config_msg->n_dl_ue_data += num_ues_added;
-   if ( num_ues_added > 0 ){
-     (*dl_info)->dl_mac_config_msg->dl_ue_data = (Protocol__FlexDlData **) realloc( (*dl_info)->dl_mac_config_msg->dl_ue_data,
-										    sizeof(Protocol__FlexDlData *) * ((*dl_info)->dl_mac_config_msg->n_dl_ue_data));
-     if ((*dl_info)->dl_mac_config_msg->dl_ue_data == NULL ){
-       LOG_E(MAC, "Request for memory reallocation failed\n");
-       return;
-     }
-     for (i = 0; i < num_ues_added; i++) {
-       (*dl_info)->dl_mac_config_msg->dl_ue_data[offset+i] = dl_data[i];
-     }
-   }
-      
-   stop_meas(&eNB->schedule_dlsch);
-   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_SCHEDULE_DLSCH,VCD_FUNCTION_OUT);
+    }
+
+    stop_meas(&eNB->schedule_dlsch);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_SCHEDULE_DLSCH, VCD_FUNCTION_OUT);
 }
diff --git a/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.c b/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.c
index 17424b27970c806dd1bb8413e4ed852530d9b878..68c86f1da54671e4110c5bb6bb8a610be210725a 100644
--- a/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.c
+++ b/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.c
@@ -17,7 +17,7 @@
  *-------------------------------------------------------------------------------
  * For more information about the OpenAirInterface (OAI) Software Alliance:
  *      contact@openairinterface.org
- */ 
+ */
 
 /*! \file flexran_agent_scheduler_dlsch_ue_remote.c
  * \brief procedures related to remote scheduling in the DLSCH transport channel
@@ -44,137 +44,159 @@ int queue_initialized = 0;
 //uint32_t period = 10;
 //uint32_t sched [] = {1, 2, 3};
 
-void flexran_schedule_ue_spec_remote(mid_t mod_id, uint32_t frame, uint32_t subframe,
-				     int *mbsfn_flag, Protocol__FlexranMessage **dl_info) {
-
-  
-  //if ((subframe == skip_subframe) && (frame % period == 0)) {
-  //  LOG_I(MAC, "Will skip subframe %d %d\n", subframe, frame);
-  //  for (int i = 0; i < 3; i++) {
-  //    LOG_I(MAC, "%d\n", sched[i]);
-  //  }
-  //}
-
-  /* if (frame == 500 && subframe == 1) { */
-  /*   char policy[] = "rrc: \n - ul_scheduler: \n    behavior : tester_function\n    parameters:\n      period: !!int 3\nmac: \n - dl_scheduler: \n    parameters: \n      period : !!int 40\n      skip_subframe : !!int 3\n      sched : [!!int 4, !!int 5, !!int 6]"; */
-  /*   apply_reconfiguration_policy(mod_id, policy, strlen(policy)); */
-  /* } */
-
-  eNB_MAC_INST *eNB;
-
-  if (!queue_initialized) {
-    TAILQ_INIT(&queue_head);
-    queue_initialized = 1;
-  }
-
-  eNB = &eNB_mac_inst[mod_id]; 
-  
-  dl_mac_config_element_t *dl_config_elem;
-
-  int diff;
-  LOG_D(MAC, "[TEST] Current frame and subframe %d, %d\n", frame, subframe);
-  // First we check to see if we have a scheduling decision for this sfn_sf already in our queue
-  while(queue_head.tqh_first != NULL) {
-    dl_config_elem = queue_head.tqh_first;
-  
-    diff = get_sf_difference(mod_id, dl_config_elem->dl_info->dl_mac_config_msg->sfn_sf);
-    // Check if this decision is for now, for a later or a previous subframe
-    if ( diff == 0) { // Now
-      LOG_D(MAC, "Found a decision for this subframe in the queue. Let's use it!\n");
-      TAILQ_REMOVE(&queue_head, queue_head.tqh_first, configs);
-      *dl_info = dl_config_elem->dl_info;
-      free(dl_config_elem);
-      eNB->eNB_stats[mod_id].sched_decisions++;
-      return;
-    } else if (diff < 0) { //previous subframe , delete message and free memory
-      LOG_D(MAC, "Found a decision for a previous subframe in the queue. Let's get rid of it\n");
-      TAILQ_REMOVE(&queue_head, queue_head.tqh_first, configs);
-      flexran_agent_mac_destroy_dl_config(dl_config_elem->dl_info);
-      free(dl_config_elem);
-      eNB->eNB_stats[mod_id].sched_decisions++;
-      eNB->eNB_stats[mod_id].missed_deadlines++;
-    } else { // next subframe, nothing to do now
-      LOG_D(MAC, "Found a decision for a future subframe in the queue. Nothing to do now\n");
-      flexran_agent_mac_create_empty_dl_config(mod_id, dl_info);
-      return;
+void
+flexran_schedule_ue_spec_remote(mid_t mod_id, uint32_t frame,
+				uint32_t subframe, int *mbsfn_flag,
+				Protocol__FlexranMessage ** dl_info)
+{
+
+
+    //if ((subframe == skip_subframe) && (frame % period == 0)) {
+    //  LOG_I(MAC, "Will skip subframe %d %d\n", subframe, frame);
+    //  for (int i = 0; i < 3; i++) {
+    //    LOG_I(MAC, "%d\n", sched[i]);
+    //  }
+    //}
+
+    /* if (frame == 500 && subframe == 1) { */
+    /*   char policy[] = "rrc: \n - ul_scheduler: \n    behavior : tester_function\n    parameters:\n      period: !!int 3\nmac: \n - dl_scheduler: \n    parameters: \n      period : !!int 40\n      skip_subframe : !!int 3\n      sched : [!!int 4, !!int 5, !!int 6]"; */
+    /*   apply_reconfiguration_policy(mod_id, policy, strlen(policy)); */
+    /* } */
+
+    eNB_MAC_INST *eNB;
+
+    if (!queue_initialized) {
+	TAILQ_INIT(&queue_head);
+	queue_initialized = 1;
     }
-  }
-
-  //Done with the local cache. Now we need to check if something new arrived
-  flexran_agent_get_pending_dl_mac_config(mod_id, dl_info);
-  while (*dl_info != NULL) {
-
-    diff = get_sf_difference(mod_id, (*dl_info)->dl_mac_config_msg->sfn_sf);
-    if (diff == 0) { // Got a command for this sfn_sf
-      LOG_D(MAC, "Found a decision for this subframe pending. Let's use it\n");
-      eNB->eNB_stats[mod_id].sched_decisions++;
-      return;
-    } else if (diff < 0) {
-      LOG_D(MAC, "Found a decision for a previous subframe. Let's get rid of it\n");
-      flexran_agent_mac_destroy_dl_config(*dl_info);
-      *dl_info = NULL;
-      flexran_agent_get_pending_dl_mac_config(mod_id, dl_info);
-      eNB->eNB_stats[mod_id].sched_decisions++;
-      eNB->eNB_stats[mod_id].missed_deadlines++;
-    } else { // Intended for future subframe. Store it in local cache
-      LOG_D(MAC, "Found a decision for a future subframe in the queue. Let's store it in the cache\n");
-      dl_mac_config_element_t *e = malloc(sizeof(dl_mac_config_element_t));
-      e->dl_info = *dl_info;
-      TAILQ_INSERT_TAIL(&queue_head, e, configs);
-      flexran_agent_mac_create_empty_dl_config(mod_id, dl_info);
-      // No need to look for another. Messages arrive ordered
-      return;
+
+    eNB = &eNB_mac_inst[mod_id];
+
+    dl_mac_config_element_t *dl_config_elem;
+
+    int diff;
+    LOG_D(MAC, "[TEST] Current frame and subframe %d, %d\n", frame,
+	  subframe);
+    // First we check to see if we have a scheduling decision for this sfn_sf already in our queue
+    while (queue_head.tqh_first != NULL) {
+	dl_config_elem = queue_head.tqh_first;
+
+	diff =
+	    get_sf_difference(mod_id,
+			      dl_config_elem->dl_info->
+			      dl_mac_config_msg->sfn_sf);
+	// Check if this decision is for now, for a later or a previous subframe
+	if (diff == 0) {	// Now
+	    LOG_D(MAC,
+		  "Found a decision for this subframe in the queue. Let's use it!\n");
+	    TAILQ_REMOVE(&queue_head, queue_head.tqh_first, configs);
+	    *dl_info = dl_config_elem->dl_info;
+	    free(dl_config_elem);
+	    eNB->eNB_stats[mod_id].sched_decisions++;
+	    return;
+	} else if (diff < 0) {	//previous subframe , delete message and free memory
+	    LOG_D(MAC,
+		  "Found a decision for a previous subframe in the queue. Let's get rid of it\n");
+	    TAILQ_REMOVE(&queue_head, queue_head.tqh_first, configs);
+	    flexran_agent_mac_destroy_dl_config(dl_config_elem->dl_info);
+	    free(dl_config_elem);
+	    eNB->eNB_stats[mod_id].sched_decisions++;
+	    eNB->eNB_stats[mod_id].missed_deadlines++;
+	} else {		// next subframe, nothing to do now
+	    LOG_D(MAC,
+		  "Found a decision for a future subframe in the queue. Nothing to do now\n");
+	    flexran_agent_mac_create_empty_dl_config(mod_id, dl_info);
+	    return;
+	}
     }
-  }
-  
-  // We found no pending command, so we will simply pass an empty one
-  flexran_agent_mac_create_empty_dl_config(mod_id, dl_info);
+
+    //Done with the local cache. Now we need to check if something new arrived
+    flexran_agent_get_pending_dl_mac_config(mod_id, dl_info);
+    while (*dl_info != NULL) {
+
+	diff =
+	    get_sf_difference(mod_id,
+			      (*dl_info)->dl_mac_config_msg->sfn_sf);
+	if (diff == 0) {	// Got a command for this sfn_sf
+	    LOG_D(MAC,
+		  "Found a decision for this subframe pending. Let's use it\n");
+	    eNB->eNB_stats[mod_id].sched_decisions++;
+	    return;
+	} else if (diff < 0) {
+	    LOG_D(MAC,
+		  "Found a decision for a previous subframe. Let's get rid of it\n");
+	    flexran_agent_mac_destroy_dl_config(*dl_info);
+	    *dl_info = NULL;
+	    flexran_agent_get_pending_dl_mac_config(mod_id, dl_info);
+	    eNB->eNB_stats[mod_id].sched_decisions++;
+	    eNB->eNB_stats[mod_id].missed_deadlines++;
+	} else {		// Intended for future subframe. Store it in local cache
+	    LOG_D(MAC,
+		  "Found a decision for a future subframe in the queue. Let's store it in the cache\n");
+	    dl_mac_config_element_t *e =
+		malloc(sizeof(dl_mac_config_element_t));
+	    e->dl_info = *dl_info;
+	    TAILQ_INSERT_TAIL(&queue_head, e, configs);
+	    flexran_agent_mac_create_empty_dl_config(mod_id, dl_info);
+	    // No need to look for another. Messages arrive ordered
+	    return;
+	}
+    }
+
+    // We found no pending command, so we will simply pass an empty one
+    flexran_agent_mac_create_empty_dl_config(mod_id, dl_info);
 }
 
-int get_sf_difference(mid_t mod_id, uint32_t sfn_sf) {
-  int diff_in_subframes;
-  
-  uint16_t current_frame = flexran_get_current_system_frame_num(mod_id);
-  uint16_t current_subframe = flexran_get_current_subframe(mod_id);
-  uint32_t current_sfn_sf = flexran_get_sfn_sf(mod_id);
-  
-  if (sfn_sf == current_sfn_sf) {
-    return 0;
-  }
-  
-  uint16_t frame_mask = ((1<<12) - 1);
-  uint16_t frame = (sfn_sf & (frame_mask << 4)) >> 4;
-  
-  uint16_t sf_mask = ((1<<4) - 1);
-  uint16_t subframe = (sfn_sf & sf_mask);
-
- LOG_D(MAC, "[TEST] Target frame and subframe %d, %d\n", frame, subframe);
-  
-  if (frame == current_frame) {
-    return subframe - current_subframe;
-  } else if (frame > current_frame) {
-    diff_in_subframes = ((frame*10)+subframe) - ((current_frame*10)+current_subframe);
-    
-    //    diff_in_subframes = 9 - current_subframe;
-    //diff_in_subframes += (subframe + 1);
-    //diff_in_subframes += (frame-2) * 10;
-    if (diff_in_subframes > SCHED_AHEAD_SUBFRAMES) {
-      return -1;
-    } else {
-      return 1;
+int get_sf_difference(mid_t mod_id, uint32_t sfn_sf)
+{
+    int diff_in_subframes;
+
+    uint16_t current_frame = flexran_get_current_system_frame_num(mod_id);
+    uint16_t current_subframe = flexran_get_current_subframe(mod_id);
+    uint32_t current_sfn_sf = flexran_get_sfn_sf(mod_id);
+
+    if (sfn_sf == current_sfn_sf) {
+	return 0;
     }
-  } else { //frame < current_frame
-    //diff_in_subframes = 9 - current_subframe;
-    //diff_in_subframes += (subframe + 1);
-    //if (frame > 0) {
-    //  diff_in_subframes += (frame - 1) * 10;
-    //}
-    //diff_in_subframes += (1023 - current_frame) * 10;
-    diff_in_subframes = 10240 - ((current_frame*10)+current_subframe) + ((frame*10)+subframe);
-    if (diff_in_subframes > SCHED_AHEAD_SUBFRAMES) {
-      return -1;
-    } else {
-      return 1;
+
+    uint16_t frame_mask = ((1 << 12) - 1);
+    uint16_t frame = (sfn_sf & (frame_mask << 4)) >> 4;
+
+    uint16_t sf_mask = ((1 << 4) - 1);
+    uint16_t subframe = (sfn_sf & sf_mask);
+
+    LOG_D(MAC, "[TEST] Target frame and subframe %d, %d\n", frame,
+	  subframe);
+
+    if (frame == current_frame) {
+	return subframe - current_subframe;
+    } else if (frame > current_frame) {
+	diff_in_subframes =
+	    ((frame * 10) + subframe) - ((current_frame * 10) +
+					 current_subframe);
+
+	//    diff_in_subframes = 9 - current_subframe;
+	//diff_in_subframes += (subframe + 1);
+	//diff_in_subframes += (frame-2) * 10;
+	if (diff_in_subframes > SCHED_AHEAD_SUBFRAMES) {
+	    return -1;
+	} else {
+	    return 1;
+	}
+    } else {			//frame < current_frame
+	//diff_in_subframes = 9 - current_subframe;
+	//diff_in_subframes += (subframe + 1);
+	//if (frame > 0) {
+	//  diff_in_subframes += (frame - 1) * 10;
+	//}
+	//diff_in_subframes += (1023 - current_frame) * 10;
+	diff_in_subframes =
+	    10240 - ((current_frame * 10) + current_subframe) +
+	    ((frame * 10) + subframe);
+	if (diff_in_subframes > SCHED_AHEAD_SUBFRAMES) {
+	    return -1;
+	} else {
+	    return 1;
+	}
     }
-  }
 }
diff --git a/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.h b/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.h
index 997b9d1d24d5dc4b9cc454decf2cb7563b781e8b..449ba1e8cde85e8c6001178f9e68534590b9952f 100644
--- a/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.h
+++ b/openair2/LAYER2/MAC/flexran_agent_scheduler_dlsch_ue_remote.h
@@ -17,7 +17,7 @@
  *-------------------------------------------------------------------------------
  * For more information about the OpenAirInterface (OAI) Software Alliance:
  *      contact@openairinterface.org
- */ 
+ */
 
 /*! \file flexran_agent_scheduler_dlsch_ue_remote.h
  * \brief Local stub for remote scheduler used by the controller
@@ -46,8 +46,8 @@
 #define SCHED_AHEAD_SUBFRAMES 20
 
 typedef struct dl_mac_config_element_s {
-  Protocol__FlexranMessage *dl_info;
-  TAILQ_ENTRY(dl_mac_config_element_s) configs;
+    Protocol__FlexranMessage *dl_info;
+     TAILQ_ENTRY(dl_mac_config_element_s) configs;
 } dl_mac_config_element_t;
 
 TAILQ_HEAD(DlMacConfigHead, dl_mac_config_element_s);
@@ -55,8 +55,9 @@ TAILQ_HEAD(DlMacConfigHead, dl_mac_config_element_s);
 /*
  * Default scheduler used by the eNB agent
  */
-void flexran_schedule_ue_spec_remote(mid_t mod_id, uint32_t frame, uint32_t subframe,
-			     int *mbsfn_flag, Protocol__FlexranMessage **dl_info);
+void flexran_schedule_ue_spec_remote(mid_t mod_id, uint32_t frame,
+				     uint32_t subframe, int *mbsfn_flag,
+				     Protocol__FlexranMessage ** dl_info);
 
 
 // Find the difference in subframes from the given subframe
diff --git a/openair2/LAYER2/MAC/flexran_dci_conversions.h b/openair2/LAYER2/MAC/flexran_dci_conversions.h
index f52ae5b807207426cdc6e79298bd1d11515436cd..5c18b431043f47033b739f314c1ea98ea3ab20cb 100644
--- a/openair2/LAYER2/MAC/flexran_dci_conversions.h
+++ b/openair2/LAYER2/MAC/flexran_dci_conversions.h
@@ -17,7 +17,7 @@
  *-------------------------------------------------------------------------------
  * For more information about the OpenAirInterface (OAI) Software Alliance:
  *      contact@openairinterface.org
- */ 
+ */
 
 /*! \file flexran_dci_conversions.h
  * \brief Conversion helpers from flexran messages to OAI formats DCI  
@@ -47,5 +47,5 @@
   ((TYPE*)DCI)->mcs = FLEXRAN_DCI->mcs[0]; \
   ((TYPE*)DCI)->TPC = FLEXRAN_DCI->tpc; \
   ((TYPE*)DCI)->ndi = FLEXRAN_DCI->ndi[0];
-  
+
 #endif
diff --git a/openair2/LAYER2/MAC/l1_helpers.c b/openair2/LAYER2/MAC/l1_helpers.c
index 2b5a38eda7cfc802e84da5391ab0724591676010..6e71f66ec5f56d32a52863db3fde89d6594b9b20 100644
--- a/openair2/LAYER2/MAC/l1_helpers.c
+++ b/openair2/LAYER2/MAC/l1_helpers.c
@@ -34,28 +34,36 @@
 #include "UTIL/LOG/log.h"
 #include "proto.h"
 
-int8_t get_Po_NOMINAL_PUSCH(module_id_t module_idP,uint8_t CC_id)
+int8_t get_Po_NOMINAL_PUSCH(module_id_t module_idP, uint8_t CC_id)
 {
-  RACH_ConfigCommon_t *rach_ConfigCommon = NULL;
+    RACH_ConfigCommon_t *rach_ConfigCommon = NULL;
 
-  AssertFatal(CC_id==0,
-	      "Transmission on secondary CCs is not supported yet\n");
-  AssertFatal(UE_mac_inst[module_idP].radioResourceConfigCommon!=NULL,
-	      "[UE %d] CCid %d FATAL radioResourceConfigCommon is NULL !!!\n",module_idP,CC_id);	      
+    AssertFatal(CC_id == 0,
+		"Transmission on secondary CCs is not supported yet\n");
+    AssertFatal(UE_mac_inst[module_idP].radioResourceConfigCommon != NULL,
+		"[UE %d] CCid %d FATAL radioResourceConfigCommon is NULL !!!\n",
+		module_idP, CC_id);
 
-  rach_ConfigCommon = &UE_mac_inst[module_idP].radioResourceConfigCommon->rach_ConfigCommon;
+    rach_ConfigCommon =
+	&UE_mac_inst[module_idP].radioResourceConfigCommon->
+	rach_ConfigCommon;
 
-  return(-120 + (rach_ConfigCommon->powerRampingParameters.preambleInitialReceivedTargetPower<<1) +
-         get_DELTA_PREAMBLE(module_idP,CC_id));
+    return (-120 +
+	    (rach_ConfigCommon->
+	     powerRampingParameters.preambleInitialReceivedTargetPower <<
+	     1) + get_DELTA_PREAMBLE(module_idP, CC_id));
 }
 
-int8_t get_deltaP_rampup(module_id_t module_idP,uint8_t CC_id)
+int8_t get_deltaP_rampup(module_id_t module_idP, uint8_t CC_id)
 {
 
-  AssertFatal(CC_id==0,
-	      "Transmission on secondary CCs is not supported yet\n");
+    AssertFatal(CC_id == 0,
+		"Transmission on secondary CCs is not supported yet\n");
 
-  LOG_D(MAC,"[PUSCH]%d dB\n",UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER<<1);
-  return((int8_t)(UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER<<1));
+    LOG_D(MAC, "[PUSCH]%d dB\n",
+	  UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER << 1);
+    return ((int8_t)
+	    (UE_mac_inst[module_idP].
+	     RA_PREAMBLE_TRANSMISSION_COUNTER << 1));
 
 }
diff --git a/openair2/LAYER2/MAC/lte_transport_init.c b/openair2/LAYER2/MAC/lte_transport_init.c
index 1f67d8913c9c7392a959cb2a6282c8307b646aaa..e1f21f21e44935e7bf071b9377e6e69548201506 100644
--- a/openair2/LAYER2/MAC/lte_transport_init.c
+++ b/openair2/LAYER2/MAC/lte_transport_init.c
@@ -29,104 +29,104 @@
 void init_transport_channels(unsigned char transmission_mode)
 {
 
-  // init DCI structures for testing
-
-  UL_alloc_pdu.type    = 0;
-  UL_alloc_pdu.hopping = 0;
-  UL_alloc_pdu.rballoc = UL_RB_ALLOC;
-  UL_alloc_pdu.mcs     = 2;
-  UL_alloc_pdu.ndi     = 1;
-  UL_alloc_pdu.TPC     = 0;
-  UL_alloc_pdu.cqi_req = 1;
-
-  /*
-  BCCH_alloc_pdu.type               = 1;
-  BCCH_alloc_pdu.vrb_type           = 0;
-  BCCH_alloc_pdu.rballoc            = BCCH_RB_ALLOC;
-  BCCH_alloc_pdu.ndi      = 1;
-  BCCH_alloc_pdu.rv       = 1;
-  BCCH_alloc_pdu.mcs      = 1;
-  BCCH_alloc_pdu.harq_pid = 0;
-  BCCH_alloc_pdu.TPC      = 1;      // set to 3 PRB
-
-  // for FDD mode
-  BCCH_alloc_pdu_fdd.type               = 1;
-  BCCH_alloc_pdu_fdd.vrb_type           = 0;
-  BCCH_alloc_pdu_fdd.rballoc            = BCCH_RB_ALLOC;
-  BCCH_alloc_pdu_fdd.ndi      = 1;
-  BCCH_alloc_pdu_fdd.rv       = 1;
-  BCCH_alloc_pdu_fdd.mcs      = 1;
-  BCCH_alloc_pdu_fdd.harq_pid = 0;
-  BCCH_alloc_pdu_fdd.TPC      = 1;      // set to 3 PRB
-  */
-
-  DLSCH_alloc_pdu1A.type               = 1;
-  DLSCH_alloc_pdu1A.vrb_type           = 0;
-  DLSCH_alloc_pdu1A.rballoc            = BCCH_RB_ALLOC;
-  DLSCH_alloc_pdu1A.ndi      = 1;
-  DLSCH_alloc_pdu1A.rv       = 1;
-  DLSCH_alloc_pdu1A.mcs      = 2;
-  DLSCH_alloc_pdu1A.harq_pid = 0;
-  DLSCH_alloc_pdu1A.TPC      = 1;   // set to 3 PRB
-
-  DLSCH_alloc_pdu1A_fdd.type               = 1;
-  DLSCH_alloc_pdu1A_fdd.vrb_type           = 0;
-  DLSCH_alloc_pdu1A_fdd.rballoc            = BCCH_RB_ALLOC;
-  DLSCH_alloc_pdu1A_fdd.ndi      = 1;
-  DLSCH_alloc_pdu1A_fdd.rv       = 1;
-  DLSCH_alloc_pdu1A_fdd.mcs      = 2;
-  DLSCH_alloc_pdu1A_fdd.harq_pid = 0;
-  DLSCH_alloc_pdu1A_fdd.TPC      = 1;   // set to 3 PRB
-
-  RA_alloc_pdu.type               = 1;
-  RA_alloc_pdu.vrb_type           = 0;
-  RA_alloc_pdu.rballoc            = RA_RB_ALLOC;
-  RA_alloc_pdu.ndi      = 1;
-  RA_alloc_pdu.rv       = 0;
-  RA_alloc_pdu.mcs      = 0;
-  RA_alloc_pdu.harq_pid = 0;
-  RA_alloc_pdu.TPC      = 1;
-
-  RA_alloc_pdu_fdd.type               = 1;
-  RA_alloc_pdu_fdd.vrb_type           = 0;
-  RA_alloc_pdu_fdd.rballoc            = RA_RB_ALLOC;
-  RA_alloc_pdu_fdd.ndi      = 1;
-  RA_alloc_pdu_fdd.rv       = 1;
-  RA_alloc_pdu_fdd.mcs      = 1;
-  RA_alloc_pdu_fdd.harq_pid = 0;
-  RA_alloc_pdu_fdd.TPC      = 1;
-
-
-  DLSCH_alloc_pdu1.rballoc          = 0xf;
-  DLSCH_alloc_pdu1.TPC              = 0;
-  DLSCH_alloc_pdu1.dai              = 0;
-  DLSCH_alloc_pdu1.harq_pid         = 0;
-  DLSCH_alloc_pdu1.tb_swap          = 0;
-  DLSCH_alloc_pdu1.mcs1             = 4;
-  DLSCH_alloc_pdu1.ndi1             = 1;
-  DLSCH_alloc_pdu1.rv1              = 0;
-
-  // Forget second codeword
-  if (transmission_mode == 6) {
-    DLSCH_alloc_pdu1.tpmi           = 5;  // PUSCH_PRECODING0
-  } else {
-    DLSCH_alloc_pdu1.tpmi             = 0;
-  }
-
-  DLSCH_alloc_pdu2.rah              = 0;
-  DLSCH_alloc_pdu2.rballoc          = DLSCH_RB_ALLOC;
-  DLSCH_alloc_pdu2.TPC              = 0;
-  DLSCH_alloc_pdu2.dai              = 0;
-  DLSCH_alloc_pdu2.harq_pid         = 0;
-  DLSCH_alloc_pdu2.tb_swap          = 0;
-  DLSCH_alloc_pdu2.mcs1             = 4;
-  DLSCH_alloc_pdu2.ndi1             = 1;
-  DLSCH_alloc_pdu2.rv1              = 0;
-
-  // Forget second codeword
-  if (transmission_mode == 6) {
-    DLSCH_alloc_pdu2.tpmi           = 5;  // PUSCH_PRECODING0
-  } else {
-    DLSCH_alloc_pdu2.tpmi             = 0;
-  }
+    // init DCI structures for testing
+
+    UL_alloc_pdu.type = 0;
+    UL_alloc_pdu.hopping = 0;
+    UL_alloc_pdu.rballoc = UL_RB_ALLOC;
+    UL_alloc_pdu.mcs = 2;
+    UL_alloc_pdu.ndi = 1;
+    UL_alloc_pdu.TPC = 0;
+    UL_alloc_pdu.cqi_req = 1;
+
+    /*
+       BCCH_alloc_pdu.type               = 1;
+       BCCH_alloc_pdu.vrb_type           = 0;
+       BCCH_alloc_pdu.rballoc            = BCCH_RB_ALLOC;
+       BCCH_alloc_pdu.ndi      = 1;
+       BCCH_alloc_pdu.rv       = 1;
+       BCCH_alloc_pdu.mcs      = 1;
+       BCCH_alloc_pdu.harq_pid = 0;
+       BCCH_alloc_pdu.TPC      = 1;      // set to 3 PRB
+
+       // for FDD mode
+       BCCH_alloc_pdu_fdd.type               = 1;
+       BCCH_alloc_pdu_fdd.vrb_type           = 0;
+       BCCH_alloc_pdu_fdd.rballoc            = BCCH_RB_ALLOC;
+       BCCH_alloc_pdu_fdd.ndi      = 1;
+       BCCH_alloc_pdu_fdd.rv       = 1;
+       BCCH_alloc_pdu_fdd.mcs      = 1;
+       BCCH_alloc_pdu_fdd.harq_pid = 0;
+       BCCH_alloc_pdu_fdd.TPC      = 1;      // set to 3 PRB
+     */
+
+    DLSCH_alloc_pdu1A.type = 1;
+    DLSCH_alloc_pdu1A.vrb_type = 0;
+    DLSCH_alloc_pdu1A.rballoc = BCCH_RB_ALLOC;
+    DLSCH_alloc_pdu1A.ndi = 1;
+    DLSCH_alloc_pdu1A.rv = 1;
+    DLSCH_alloc_pdu1A.mcs = 2;
+    DLSCH_alloc_pdu1A.harq_pid = 0;
+    DLSCH_alloc_pdu1A.TPC = 1;	// set to 3 PRB
+
+    DLSCH_alloc_pdu1A_fdd.type = 1;
+    DLSCH_alloc_pdu1A_fdd.vrb_type = 0;
+    DLSCH_alloc_pdu1A_fdd.rballoc = BCCH_RB_ALLOC;
+    DLSCH_alloc_pdu1A_fdd.ndi = 1;
+    DLSCH_alloc_pdu1A_fdd.rv = 1;
+    DLSCH_alloc_pdu1A_fdd.mcs = 2;
+    DLSCH_alloc_pdu1A_fdd.harq_pid = 0;
+    DLSCH_alloc_pdu1A_fdd.TPC = 1;	// set to 3 PRB
+
+    RA_alloc_pdu.type = 1;
+    RA_alloc_pdu.vrb_type = 0;
+    RA_alloc_pdu.rballoc = RA_RB_ALLOC;
+    RA_alloc_pdu.ndi = 1;
+    RA_alloc_pdu.rv = 0;
+    RA_alloc_pdu.mcs = 0;
+    RA_alloc_pdu.harq_pid = 0;
+    RA_alloc_pdu.TPC = 1;
+
+    RA_alloc_pdu_fdd.type = 1;
+    RA_alloc_pdu_fdd.vrb_type = 0;
+    RA_alloc_pdu_fdd.rballoc = RA_RB_ALLOC;
+    RA_alloc_pdu_fdd.ndi = 1;
+    RA_alloc_pdu_fdd.rv = 1;
+    RA_alloc_pdu_fdd.mcs = 1;
+    RA_alloc_pdu_fdd.harq_pid = 0;
+    RA_alloc_pdu_fdd.TPC = 1;
+
+
+    DLSCH_alloc_pdu1.rballoc = 0xf;
+    DLSCH_alloc_pdu1.TPC = 0;
+    DLSCH_alloc_pdu1.dai = 0;
+    DLSCH_alloc_pdu1.harq_pid = 0;
+    DLSCH_alloc_pdu1.tb_swap = 0;
+    DLSCH_alloc_pdu1.mcs1 = 4;
+    DLSCH_alloc_pdu1.ndi1 = 1;
+    DLSCH_alloc_pdu1.rv1 = 0;
+
+    // Forget second codeword
+    if (transmission_mode == 6) {
+	DLSCH_alloc_pdu1.tpmi = 5;	// PUSCH_PRECODING0
+    } else {
+	DLSCH_alloc_pdu1.tpmi = 0;
+    }
+
+    DLSCH_alloc_pdu2.rah = 0;
+    DLSCH_alloc_pdu2.rballoc = DLSCH_RB_ALLOC;
+    DLSCH_alloc_pdu2.TPC = 0;
+    DLSCH_alloc_pdu2.dai = 0;
+    DLSCH_alloc_pdu2.harq_pid = 0;
+    DLSCH_alloc_pdu2.tb_swap = 0;
+    DLSCH_alloc_pdu2.mcs1 = 4;
+    DLSCH_alloc_pdu2.ndi1 = 1;
+    DLSCH_alloc_pdu2.rv1 = 0;
+
+    // Forget second codeword
+    if (transmission_mode == 6) {
+	DLSCH_alloc_pdu2.tpmi = 5;	// PUSCH_PRECODING0
+    } else {
+	DLSCH_alloc_pdu2.tpmi = 0;
+    }
 }
diff --git a/openair2/LAYER2/MAC/main.c b/openair2/LAYER2/MAC/main.c
index 83f639a89d4a4451d3a135bca0a77054c01c7e33..fcaab0b80b035ec1bf45f9c19c14fc84857a144a 100644
--- a/openair2/LAYER2/MAC/main.c
+++ b/openair2/LAYER2/MAC/main.c
@@ -48,66 +48,72 @@
 
 extern RAN_CONTEXT_t RC;
 
-void dl_phy_sync_success(module_id_t   module_idP,
-                         frame_t       frameP,
-                         unsigned char eNB_index,
-                         uint8_t       first_sync)   //init as MR
+void dl_phy_sync_success(module_id_t module_idP, frame_t frameP, unsigned char eNB_index, uint8_t first_sync)	//init as MR
 {
-  LOG_D(MAC,"[UE %d] Frame %d: PHY Sync to eNB_index %d successful \n", module_idP, frameP, eNB_index);
+    LOG_D(MAC, "[UE %d] Frame %d: PHY Sync to eNB_index %d successful \n",
+	  module_idP, frameP, eNB_index);
 #if defined(ENABLE_USE_MME)
-  int mme_enabled=1;
+    int mme_enabled = 1;
 #else
-  int mme_enabled=0;
+    int mme_enabled = 0;
 #endif
 
-  if (first_sync==1 && !(mme_enabled==1)) {
-    //layer2_init_UE(module_idP);
-    openair_rrc_ue_init(module_idP,eNB_index);
-  } else
-  {
-    rrc_in_sync_ind(module_idP,frameP,eNB_index);
-  }
+    if (first_sync == 1 && !(mme_enabled == 1)) {
+	//layer2_init_UE(module_idP);
+	openair_rrc_ue_init(module_idP, eNB_index);
+    } else {
+	rrc_in_sync_ind(module_idP, frameP, eNB_index);
+    }
 }
 
-void mac_UE_out_of_sync_ind(module_id_t module_idP, frame_t frameP, uint16_t eNB_index)
+void
+mac_UE_out_of_sync_ind(module_id_t module_idP, frame_t frameP,
+		       uint16_t eNB_index)
 {
 
-  //  Mac_rlc_xface->mac_out_of_sync_ind(Mod_id, frameP, eNB_index);
+    //  Mac_rlc_xface->mac_out_of_sync_ind(Mod_id, frameP, eNB_index);
 }
 
 
-int mac_top_init_ue(int eMBMS_active, char *uecap_xer, uint8_t cba_group_active, uint8_t HO_active)
+int
+mac_top_init_ue(int eMBMS_active, char *uecap_xer,
+		uint8_t cba_group_active, uint8_t HO_active)
 {
 
-  int i;
+    int i;
 
-  LOG_I(MAC,"[MAIN] Init function start:Nb_UE_INST=%d\n",NB_UE_INST);
+    LOG_I(MAC, "[MAIN] Init function start:Nb_UE_INST=%d\n", NB_UE_INST);
 
-  if (NB_UE_INST>0) {
-    UE_mac_inst = (UE_MAC_INST*)malloc16(NB_UE_INST*sizeof(UE_MAC_INST));
+    if (NB_UE_INST > 0) {
+	UE_mac_inst =
+	    (UE_MAC_INST *) malloc16(NB_UE_INST * sizeof(UE_MAC_INST));
 
-    AssertFatal(UE_mac_inst!=NULL,
-		"[MAIN] Can't ALLOCATE %zu Bytes for %d UE_MAC_INST with size %zu \n",NB_UE_INST*sizeof(UE_MAC_INST),NB_UE_INST,sizeof(UE_MAC_INST));
+	AssertFatal(UE_mac_inst != NULL,
+		    "[MAIN] Can't ALLOCATE %zu Bytes for %d UE_MAC_INST with size %zu \n",
+		    NB_UE_INST * sizeof(UE_MAC_INST), NB_UE_INST,
+		    sizeof(UE_MAC_INST));
 
-    LOG_D(MAC,"[MAIN] ALLOCATE %zu Bytes for %d UE_MAC_INST @ %p\n",NB_UE_INST*sizeof(UE_MAC_INST),NB_UE_INST,UE_mac_inst);
+	LOG_D(MAC, "[MAIN] ALLOCATE %zu Bytes for %d UE_MAC_INST @ %p\n",
+	      NB_UE_INST * sizeof(UE_MAC_INST), NB_UE_INST, UE_mac_inst);
 
-    bzero(UE_mac_inst,NB_UE_INST*sizeof(UE_MAC_INST));
+	bzero(UE_mac_inst, NB_UE_INST * sizeof(UE_MAC_INST));
 
-    for(i=0; i<NB_UE_INST; i++) {
-      ue_init_mac(i);
+	for (i = 0; i < NB_UE_INST; i++) {
+	    ue_init_mac(i);
+	}
+    } else {
+	UE_mac_inst = NULL;
     }
-  } else {
-    UE_mac_inst = NULL;
-  }
 
 
-  LOG_I(MAC,"[MAIN] calling RRC\n");
-  openair_rrc_top_init_ue(eMBMS_active, uecap_xer, cba_group_active,HO_active);
+    LOG_I(MAC, "[MAIN] calling RRC\n");
+    openair_rrc_top_init_ue(eMBMS_active, uecap_xer, cba_group_active,
+			    HO_active);
 
-  
-  LOG_I(MAC,"[MAIN][INIT] Init function finished\n");
 
-  return(0);
+    LOG_I(MAC, "[MAIN][INIT] Init function finished\n");
+
+    return (0);
 
 }
 
@@ -115,183 +121,107 @@ int mac_top_init_ue(int eMBMS_active, char *uecap_xer, uint8_t cba_group_active,
 void mac_top_init_eNB()
 {
 
-  module_id_t    i,j;
-  int list_el;
-  UE_list_t *UE_list;
-  eNB_MAC_INST *mac;
-
-  LOG_I(MAC,"[MAIN] Init function start:nb_macrlc_inst=%d\n",RC.nb_macrlc_inst);
-
-  if (RC.nb_macrlc_inst>0) {
-    RC.mac = (eNB_MAC_INST**)malloc16(RC.nb_macrlc_inst*sizeof(eNB_MAC_INST*));
-    AssertFatal(RC.mac != NULL,"can't ALLOCATE %zu Bytes for %d eNB_MAC_INST with size %zu \n",
-		RC.nb_macrlc_inst*sizeof(eNB_MAC_INST*),
-		RC.nb_macrlc_inst,
-		sizeof(eNB_MAC_INST));
-    for (i=0;i<RC.nb_macrlc_inst;i++) {
-      RC.mac[i] = (eNB_MAC_INST*)malloc16(sizeof(eNB_MAC_INST));
-      AssertFatal(RC.mac != NULL,
-		  "can't ALLOCATE %zu Bytes for %d eNB_MAC_INST with size %zu \n",
-		  RC.nb_macrlc_inst*sizeof(eNB_MAC_INST*),RC.nb_macrlc_inst,sizeof(eNB_MAC_INST));
-      LOG_D(MAC,"[MAIN] ALLOCATE %zu Bytes for %d eNB_MAC_INST @ %p\n",sizeof(eNB_MAC_INST),RC.nb_macrlc_inst,RC.mac);
-      bzero(RC.mac[i],sizeof(eNB_MAC_INST));
-      RC.mac[i]->Mod_id = i;
-      for (j=0;j<MAX_NUM_CCs;j++) {
-	RC.mac[i]->DL_req[j].dl_config_request_body.dl_config_pdu_list      = RC.mac[i]->dl_config_pdu_list[j];
-	RC.mac[i]->UL_req[j].ul_config_request_body.ul_config_pdu_list      = RC.mac[i]->ul_config_pdu_list[j];
-	for (int k=0;k<10;k++) RC.mac[i]->UL_req_tmp[j][k].ul_config_request_body.ul_config_pdu_list   = RC.mac[i]->ul_config_pdu_list_tmp[j][k];
-	RC.mac[i]->HI_DCI0_req[j].hi_dci0_request_body.hi_dci0_pdu_list     = RC.mac[i]->hi_dci0_pdu_list[j];
-	RC.mac[i]->TX_req[j].tx_request_body.tx_pdu_list                    = RC.mac[i]->tx_request_pdu[j];
-	RC.mac[i]->ul_handle                                                = 0;
-      }
+    module_id_t i, j;
+    int list_el;
+    UE_list_t *UE_list;
+    eNB_MAC_INST *mac;
+
+    LOG_I(MAC, "[MAIN] Init function start:nb_macrlc_inst=%d\n",
+	  RC.nb_macrlc_inst);
+
+    if (RC.nb_macrlc_inst > 0) {
+	RC.mac =
+	    (eNB_MAC_INST **) malloc16(RC.nb_macrlc_inst *
+				       sizeof(eNB_MAC_INST *));
+	AssertFatal(RC.mac != NULL,
+		    "can't ALLOCATE %zu Bytes for %d eNB_MAC_INST with size %zu \n",
+		    RC.nb_macrlc_inst * sizeof(eNB_MAC_INST *),
+		    RC.nb_macrlc_inst, sizeof(eNB_MAC_INST));
+	for (i = 0; i < RC.nb_macrlc_inst; i++) {
+	    RC.mac[i] = (eNB_MAC_INST *) malloc16(sizeof(eNB_MAC_INST));
+	    AssertFatal(RC.mac != NULL,
+			"can't ALLOCATE %zu Bytes for %d eNB_MAC_INST with size %zu \n",
+			RC.nb_macrlc_inst * sizeof(eNB_MAC_INST *),
+			RC.nb_macrlc_inst, sizeof(eNB_MAC_INST));
+	    LOG_D(MAC,
+		  "[MAIN] ALLOCATE %zu Bytes for %d eNB_MAC_INST @ %p\n",
+		  sizeof(eNB_MAC_INST), RC.nb_macrlc_inst, RC.mac);
+	    bzero(RC.mac[i], sizeof(eNB_MAC_INST));
+	    RC.mac[i]->Mod_id = i;
+	    for (j = 0; j < MAX_NUM_CCs; j++) {
+		RC.mac[i]->DL_req[j].dl_config_request_body.
+		    dl_config_pdu_list = RC.mac[i]->dl_config_pdu_list[j];
+		RC.mac[i]->UL_req[j].ul_config_request_body.
+		    ul_config_pdu_list = RC.mac[i]->ul_config_pdu_list[j];
+		for (int k = 0; k < 10; k++)
+		    RC.mac[i]->UL_req_tmp[j][k].
+			ul_config_request_body.ul_config_pdu_list =
+			RC.mac[i]->ul_config_pdu_list_tmp[j][k];
+		RC.mac[i]->HI_DCI0_req[j].
+		    hi_dci0_request_body.hi_dci0_pdu_list =
+		    RC.mac[i]->hi_dci0_pdu_list[j];
+		RC.mac[i]->TX_req[j].tx_request_body.tx_pdu_list =
+		    RC.mac[i]->tx_request_pdu[j];
+		RC.mac[i]->ul_handle = 0;
+	    }
+	}
+
+	AssertFatal(rlc_module_init() == 0,
+		    "Could not initialize RLC layer\n");
+
+	// These should be out of here later
+	pdcp_layer_init();
+
+	rrc_init_global_param();
+
+    } else {
+	RC.mac = NULL;
     }
 
-    AssertFatal(rlc_module_init()==0,"Could not initialize RLC layer\n");
+    // Initialize Linked-List for Active UEs
+    for (i = 0; i < RC.nb_macrlc_inst; i++) {
+	mac = RC.mac[i];
 
-    // These should be out of here later
-    pdcp_layer_init ();
 
-    rrc_init_global_param();
+	mac->if_inst = IF_Module_init(i);
 
-  } else {
-    RC.mac = NULL;
-  }
-  
-  // Initialize Linked-List for Active UEs
-  for(i=0; i<RC.nb_macrlc_inst; i++) {
-    mac = RC.mac[i];
+	UE_list = &mac->UE_list;
 
+	UE_list->num_UEs = 0;
+	UE_list->head = -1;
+	UE_list->head_ul = -1;
+	UE_list->avail = 0;
 
-    mac->if_inst                = IF_Module_init(i);
+	for (list_el = 0; list_el < NUMBER_OF_UE_MAX - 1; list_el++) {
+	    UE_list->next[list_el] = list_el + 1;
+	    UE_list->next_ul[list_el] = list_el + 1;
+	}
 
-    UE_list = &mac->UE_list;
+	UE_list->next[list_el] = -1;
+	UE_list->next_ul[list_el] = -1;
+    }
 
-    UE_list->num_UEs=0;
-    UE_list->head=-1;
-    UE_list->head_ul=-1;
-    UE_list->avail=0;
+}
 
-    for (list_el=0; list_el<NUMBER_OF_UE_MAX-1; list_el++) {
-      UE_list->next[list_el]=list_el+1;
-      UE_list->next_ul[list_el]=list_el+1;
-    }
+void mac_init_cell_params(int Mod_idP, int CC_idP)
+{
 
-    UE_list->next[list_el]=-1;
-    UE_list->next_ul[list_el]=-1;
-  }
+    int j;
+    UE_TEMPLATE *UE_template;
 
-}
+    LOG_D(MAC, "[MSC_NEW][FRAME 00000][MAC_eNB][MOD %02d][]\n", Mod_idP);
+    COMMON_channels_t *cc = &RC.mac[Mod_idP]->common_channels[CC_idP];
 
-void mac_init_cell_params(int Mod_idP,int CC_idP) {
-
-  int j;
-  RA_TEMPLATE *RA_template;
-  UE_TEMPLATE *UE_template;
-  int size_bytes1,size_bytes2,size_bits1,size_bits2;
-
-  LOG_D(MAC,"[MAIN][eNB %d] CC_id %d initializing RA_template\n",Mod_idP, CC_idP);
-  LOG_D(MAC, "[MSC_NEW][FRAME 00000][MAC_eNB][MOD %02d][]\n", Mod_idP);
-  COMMON_channels_t *cc = &RC.mac[Mod_idP]->common_channels[CC_idP];
-
-  RA_template = (RA_TEMPLATE *)&cc->RA_template[0];
-  
-  for (j=0; j<NB_RA_PROC_MAX; j++) {
-    if ( cc->tdd_Config != NULL) {
-      switch (cc->mib->message.dl_Bandwidth) {
-      case MasterInformationBlock__dl_Bandwidth_n6:
-	size_bytes1 = sizeof(DCI1A_1_5MHz_TDD_1_6_t);
-	size_bytes2 = sizeof(DCI1A_1_5MHz_TDD_1_6_t);
-	size_bits1 = sizeof_DCI1A_1_5MHz_TDD_1_6_t;
-	size_bits2 = sizeof_DCI1A_1_5MHz_TDD_1_6_t;
-	break;
-	
-      case MasterInformationBlock__dl_Bandwidth_n25:
-	size_bytes1 = sizeof(DCI1A_5MHz_TDD_1_6_t);
-	size_bytes2 = sizeof(DCI1A_5MHz_TDD_1_6_t);
-	size_bits1 = sizeof_DCI1A_5MHz_TDD_1_6_t;
-	size_bits2 = sizeof_DCI1A_5MHz_TDD_1_6_t;
-	break;
-	
-      case MasterInformationBlock__dl_Bandwidth_n50:
-	size_bytes1 = sizeof(DCI1A_10MHz_TDD_1_6_t);
-	size_bytes2 = sizeof(DCI1A_10MHz_TDD_1_6_t);
-	size_bits1 = sizeof_DCI1A_10MHz_TDD_1_6_t;
-	size_bits2 = sizeof_DCI1A_10MHz_TDD_1_6_t;
-	break;
-	
-      case MasterInformationBlock__dl_Bandwidth_n100:
-	size_bytes1 = sizeof(DCI1A_20MHz_TDD_1_6_t);
-	size_bytes2 = sizeof(DCI1A_20MHz_TDD_1_6_t);
-	size_bits1 = sizeof_DCI1A_20MHz_TDD_1_6_t;
-	size_bits2 = sizeof_DCI1A_20MHz_TDD_1_6_t;
-	break;
-	
-      default:
-	size_bytes1 = sizeof(DCI1A_1_5MHz_TDD_1_6_t);
-	size_bytes2 = sizeof(DCI1A_1_5MHz_TDD_1_6_t);
-	size_bits1 = sizeof_DCI1A_1_5MHz_TDD_1_6_t;
-	size_bits2 = sizeof_DCI1A_1_5MHz_TDD_1_6_t;
-	break;
-      }
-      
-    } else {
-      switch (cc->mib->message.dl_Bandwidth) {
-      case MasterInformationBlock__dl_Bandwidth_n6:
-	size_bytes1 = sizeof(DCI1A_1_5MHz_FDD_t);
-	size_bytes2 = sizeof(DCI1A_1_5MHz_FDD_t);
-	size_bits1 = sizeof_DCI1A_1_5MHz_FDD_t;
-	size_bits2 = sizeof_DCI1A_1_5MHz_FDD_t;
-	break;
-	
-      case MasterInformationBlock__dl_Bandwidth_n25:
-	size_bytes1 = sizeof(DCI1A_5MHz_FDD_t);
-	size_bytes2 = sizeof(DCI1A_5MHz_FDD_t);
-	size_bits1 = sizeof_DCI1A_5MHz_FDD_t;
-	size_bits2 = sizeof_DCI1A_5MHz_FDD_t;
-	break;
-	
-      case MasterInformationBlock__dl_Bandwidth_n50:
-	size_bytes1 = sizeof(DCI1A_10MHz_FDD_t);
-	size_bytes2 = sizeof(DCI1A_10MHz_FDD_t);
-	size_bits1 = sizeof_DCI1A_10MHz_FDD_t;
-	size_bits2 = sizeof_DCI1A_10MHz_FDD_t;
-	break;
-	
-      case MasterInformationBlock__dl_Bandwidth_n100:
-	size_bytes1 = sizeof(DCI1A_20MHz_FDD_t);
-	size_bytes2 = sizeof(DCI1A_20MHz_FDD_t);
-	size_bits1 = sizeof_DCI1A_20MHz_FDD_t;
-	size_bits2 = sizeof_DCI1A_20MHz_FDD_t;
-	break;
-	
-      default:
-	size_bytes1 = sizeof(DCI1A_1_5MHz_FDD_t);
-	size_bytes2 = sizeof(DCI1A_1_5MHz_FDD_t);
-	size_bits1 = sizeof_DCI1A_1_5MHz_FDD_t;
-	size_bits2 = sizeof_DCI1A_1_5MHz_FDD_t;
-	break;
-      }
+    memset(&RC.mac[Mod_idP]->eNB_stats, 0, sizeof(eNB_STATS));
+    UE_template =
+	(UE_TEMPLATE *) & RC.mac[Mod_idP]->UE_list.UE_template[CC_idP][0];
+
+    for (j = 0; j < NUMBER_OF_UE_MAX; j++) {
+	UE_template[j].rnti = 0;
+	// initiallize the eNB to UE statistics
+	memset(&RC.mac[Mod_idP]->UE_list.eNB_UE_stats[CC_idP][j], 0,
+	       sizeof(eNB_UE_STATS));
     }
-    
-    memcpy((void *)&RA_template[j].RA_alloc_pdu1[0],(void *)&RA_alloc_pdu,size_bytes1);
-    memcpy((void *)&RA_template[j].RA_alloc_pdu2[0],(void *)&DLSCH_alloc_pdu1A,size_bytes2);
-    RA_template[j].RA_dci_size_bytes1 = size_bytes1;
-    RA_template[j].RA_dci_size_bytes2 = size_bytes2;
-    RA_template[j].RA_dci_size_bits1  = size_bits1;
-    RA_template[j].RA_dci_size_bits2  = size_bits2;
-    
-    RA_template[j].RA_dci_fmt1        = format1A;
-    RA_template[j].RA_dci_fmt2        = format1A;
-  }
-  
-  memset (&RC.mac[Mod_idP]->eNB_stats,0,sizeof(eNB_STATS));
-  UE_template = (UE_TEMPLATE *)&RC.mac[Mod_idP]->UE_list.UE_template[CC_idP][0];
-  
-  for (j=0; j<NUMBER_OF_UE_MAX; j++) {
-    UE_template[j].rnti=0;
-    // initiallize the eNB to UE statistics
-    memset (&RC.mac[Mod_idP]->UE_list.eNB_UE_stats[CC_idP][j],0,sizeof(eNB_UE_STATS));
-  }
 
 }
 
@@ -300,17 +230,17 @@ int rlcmac_init_global_param(void)
 {
 
 
-  LOG_I(MAC,"[MAIN] CALLING RLC_MODULE_INIT...\n");
+    LOG_I(MAC, "[MAIN] CALLING RLC_MODULE_INIT...\n");
 
-  if (rlc_module_init()!=0) {
-    return(-1);
-  }
+    if (rlc_module_init() != 0) {
+	return (-1);
+    }
 
-  pdcp_layer_init ();
+    pdcp_layer_init();
 
-  LOG_I(MAC,"[MAIN] Init Global Param Done\n");
+    LOG_I(MAC, "[MAIN] Init Global Param Done\n");
 
-  return 0;
+    return 0;
 }
 
 
@@ -318,29 +248,31 @@ void mac_top_cleanup(void)
 {
 
 #ifndef USER_MODE
-  pdcp_module_cleanup ();
+    pdcp_module_cleanup();
 #endif
 
-  if (NB_UE_INST>0) {
-    free (UE_mac_inst);
-  }
+    if (NB_UE_INST > 0) {
+	free(UE_mac_inst);
+    }
 
-  if (RC.nb_macrlc_inst>0) {
-    free(RC.mac);
-  }
+    if (RC.nb_macrlc_inst > 0) {
+	free(RC.mac);
+    }
 
 }
 
-int l2_init_ue(int eMBMS_active, char *uecap_xer,uint8_t cba_group_active, uint8_t HO_active)
+int
+l2_init_ue(int eMBMS_active, char *uecap_xer, uint8_t cba_group_active,
+	   uint8_t HO_active)
 {
-  LOG_I(MAC,"[MAIN] MAC_INIT_GLOBAL_PARAM IN...\n");
-  //    NB_NODE=2;
-  //    NB_INST=2;
-
-  rlcmac_init_global_param();
-  LOG_I(MAC,"[MAIN] init UE MAC functions \n");
-  mac_top_init_ue(eMBMS_active,uecap_xer,cba_group_active,HO_active);
-  return(1);
+    LOG_I(MAC, "[MAIN] MAC_INIT_GLOBAL_PARAM IN...\n");
+    //    NB_NODE=2;
+    //    NB_INST=2;
+
+    rlcmac_init_global_param();
+    LOG_I(MAC, "[MAIN] init UE MAC functions \n");
+    mac_top_init_ue(eMBMS_active, uecap_xer, cba_group_active, HO_active);
+    return (1);
 }
 
 int l2_init_eNB()
@@ -348,13 +280,12 @@ int l2_init_eNB()
 
 
 
-  LOG_I(MAC,"[MAIN] MAC_INIT_GLOBAL_PARAM IN...\n");
+    LOG_I(MAC, "[MAIN] MAC_INIT_GLOBAL_PARAM IN...\n");
 
-  rlcmac_init_global_param();
+    rlcmac_init_global_param();
 
-  LOG_D(MAC,"[MAIN] ALL INIT OK\n");
+    LOG_D(MAC, "[MAIN] ALL INIT OK\n");
 
 
-  return(1);
+    return (1);
 }
-
diff --git a/openair2/LAYER2/MAC/openair2_proc.c b/openair2/LAYER2/MAC/openair2_proc.c
deleted file mode 100644
index ab78be3c61048577790325f1b83d4a5f29af1058..0000000000000000000000000000000000000000
--- a/openair2/LAYER2/MAC/openair2_proc.c
+++ /dev/null
@@ -1,405 +0,0 @@
-/*
- * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The OpenAirInterface Software Alliance licenses this file to You under
- * the OAI Public License, Version 1.1  (the "License"); you may not use this file
- * except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.openairinterface.org/?page_id=698
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *-------------------------------------------------------------------------------
- * For more information about the OpenAirInterface (OAI) Software Alliance:
- *      contact@openairinterface.org
- */
-
-/* \file openair2_proc.c
- \brief MAC layer online statistics
- \author Navid Nikaein
- \date 2013 - 2014
- \version 1.0
- @ingroup _mac
-*/
-
-#ifndef USER_MODE
-#include <linux/kernel.h>
-#include <linux/module.h>
-#include <linux/proc_fs.h>
-
-#endif
-#include "LAYER2/MAC/defs.h"
-#include "LAYER2/MAC/extern.h"
-//#include "RRC/LITE/extern.h"
-//#include "LAYER2/PDCP/pdcp.h"
-#include "proto.h"
-
-extern RAN_CONTEXT_t RC;
-
-int openair2_stats_read(char *buffer, char **my_buffer, off_t off, int length)
-
-{
-
-  int len = 0,fg,Overhead, Sign;
-  unsigned int i,j,k,kk;
-  unsigned int ue_id, eNB_id;
-  unsigned int Mod_id = 0,CH_index;
-  unsigned int stat_tx_pdcp_sdu;
-  unsigned int stat_tx_pdcp_bytes;
-  unsigned int stat_tx_pdcp_sdu_discarded;
-  unsigned int stat_tx_pdcp_bytes_discarded;
-  unsigned int stat_tx_data_pdu;
-  unsigned int stat_tx_data_bytes;
-  unsigned int stat_tx_retransmit_pdu_by_status;
-  unsigned int stat_tx_retransmit_bytes_by_status;
-  unsigned int stat_tx_retransmit_pdu;
-  unsigned int stat_tx_retransmit_bytes;
-  unsigned int stat_tx_control_pdu;
-  unsigned int stat_tx_control_bytes;
-  unsigned int stat_rx_pdcp_sdu;
-  unsigned int stat_rx_pdcp_bytes;
-  unsigned int stat_rx_data_pdus_duplicate;
-  unsigned int stat_rx_data_bytes_duplicate;
-  unsigned int stat_rx_data_pdu;
-  unsigned int stat_rx_data_bytes;
-  unsigned int stat_rx_data_pdu_dropped;
-  unsigned int stat_rx_data_bytes_dropped;
-  unsigned int stat_rx_data_pdu_out_of_window;
-  unsigned int stat_rx_data_bytes_out_of_window;
-  unsigned int stat_rx_control_pdu;
-  unsigned int stat_rx_control_bytes;
-  unsigned int stat_timer_reordering_timed_out;
-  unsigned int stat_timer_poll_retransmit_timed_out;
-  unsigned int stat_timer_status_prohibit_timed_out;
-
-  // UE part
-  for (ue_id=0; ue_id<NUM_UE_INST; ue_id++) {
-    // mod_id used for PDCP and RLC
-    Mod_id = NB_eNB_INST + ue_id ;
-
-    len+=sprintf(&buffer[len],"UE RX TTI: %d\n",UE_mac_inst[ue_id].rxFrame);
-
-    for (enb_id= 0; enb_id <NB_SIG_CNX_UE; enb_id++) {
-
-      switch (mac_get_rrc_status(ue_id,0,enb_id) > RRC_CONNECTED) {
-      case RRC_RECONFIGURED :
-      case RRC_CONNECTED:
-      case RRC_SI_RECEIVED:
-      case RRC_IDLE:
-        break;
-
-        if (mac_get_rrc_status(ue_id,0,enb_id) > RRC_CONNECTED) {
-          //  if (UE_mac_inst[ue_id].Dcch_lchan[CH_index].Active==1) {
-          len+=sprintf(&buffer[len],"eNB %d: Wideband SINR %d dB---\n",
-                       CH_index,UE_mac_inst[Mod_id].Def_meas[CH_index].Wideband_sinr);
-          len+=sprintf(&buffer[len],"CH %d: Subband SINR (dB) :",
-                       CH_index);
-
-          for (fg=0; fg<NUMBER_OF_MEASUREMENT_SUBBANDS; fg++) {
-            len+=sprintf(&buffer[len],"%d ",UE_mac_inst[Mod_id].Def_meas[CH_index].Sinr_meas[0][fg]);
-          }
-
-          len+=sprintf(&buffer[len],"\n");
-
-
-          len+=sprintf(&buffer[len],"BCCH %d, NB_RX_MAC = %d (%d errors)\n",
-                       UE_mac_inst[Mod_id].Bcch_lchan[CH_index].Lchan_info.Lchan_id.Index,
-                       UE_mac_inst[Mod_id].Bcch_lchan[CH_index].Lchan_info.NB_RX,
-                       UE_mac_inst[Mod_id].Bcch_lchan[CH_index].Lchan_info.NB_RX_ERRORS);
-
-
-
-          len+=sprintf(&buffer[len],"CCCH %d, NB_RX_MAC = %d (%d errors)\n",
-                       UE_mac_inst[Mod_id].Ccch_lchan[CH_index].Lchan_info.Lchan_id.Index,
-                       UE_mac_inst[Mod_id].Ccch_lchan[CH_index].Lchan_info.NB_RX,
-                       UE_mac_inst[Mod_id].Ccch_lchan[CH_index].Lchan_info.NB_RX_ERRORS);
-
-
-          len+=sprintf(&buffer[len],"LCHAN %d (DCCH), NB_TX_MAC = %d (%d bits/TTI, %d kbits/sec), NB_RX_MAC = %d (%d errors)\n",
-                       UE_mac_inst[Mod_id].Dcch_lchan[CH_index].Lchan_info.Lchan_id.Index,
-                       UE_mac_inst[Mod_id].Dcch_lchan[CH_index].Lchan_info.NB_TX,
-                       UE_mac_inst[Mod_id].Dcch_lchan[CH_index].Lchan_info.output_rate,
-                       (10*UE_mac_inst[Mod_id].Dcch_lchan[CH_index].Lchan_info.output_rate)>>5,
-                       UE_mac_inst[Mod_id].Dcch_lchan[CH_index].Lchan_info.NB_RX,
-                       UE_mac_inst[Mod_id].Dcch_lchan[CH_index].Lchan_info.NB_RX_ERRORS);
-
-          for(i=1; i<NB_RAB_MAX; i++) {
-            if (UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Active==1) {
-              Overhead=UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.output_rate - Pdcp_stats_tx_rate[k][CH_index][i];
-
-              if(Overhead<0) {
-                Overhead=-Overhead;
-                Sign=-1;
-              } else {
-                Sign=1;
-              }
-
-              len+=sprintf(&buffer[len],"[PDCP]LCHAN %d: NB_TX = %d ,Tx_rate =(%d bits/TTI ,%d Kbits/s), NB_RX = %d ,Rx_rate =(%d bits/TTI ,%d Kbits/s) , LAYER2 TX OVERHEAD: %d Kbits/s\n",
-                           UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.Lchan_id.Index,
-                           Pdcp_stats_tx[k][CH_index][i],
-                           Pdcp_stats_tx_rate[k][CH_index][i],
-                           (10*Pdcp_stats_tx_rate[k][CH_index][i])>>5,
-                           Pdcp_stats_rx[k][CH_index][i],
-                           Pdcp_stats_rx_rate[k][CH_index][i],
-                           (10*Pdcp_stats_rx_rate[k][CH_index][i])>>5,
-                           Sign*(10*Overhead)>>5);
-
-
-              int status =  rlc_stat_req     (k,
-                                              UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.Lchan_id.Index,
-                                              &stat_tx_pdcp_sdu,
-                                              &stat_tx_pdcp_bytes,
-                                              &stat_tx_pdcp_sdu_discarded,
-                                              &stat_tx_pdcp_bytes_discarded,
-                                              &stat_tx_data_pdu,
-                                              &stat_tx_data_bytes,
-                                              &stat_tx_retransmit_pdu_by_status,
-                                              &stat_tx_retransmit_bytes_by_status,
-                                              &stat_tx_retransmit_pdu,
-                                              &stat_tx_retransmit_bytes,
-                                              &stat_tx_control_pdu,
-                                              &stat_tx_control_bytes,
-                                              &stat_rx_pdcp_sdu,
-                                              &stat_rx_pdcp_bytes,
-                                              &stat_rx_data_pdus_duplicate,
-                                              &stat_rx_data_bytes_duplicate,
-                                              &stat_rx_data_pdu,
-                                              &stat_rx_data_bytes,
-                                              &stat_rx_data_pdu_dropped,
-                                              &stat_rx_data_bytes_dropped,
-                                              &stat_rx_data_pdu_out_of_window,
-                                              &stat_rx_data_bytes_out_of_window,
-                                              &stat_rx_control_pdu,
-                                              &stat_rx_control_bytes,
-                                              &stat_timer_reordering_timed_out,
-                                              &stat_timer_poll_retransmit_timed_out,
-                                              &stat_timer_status_prohibit_timed_out) ;
-
-              if (status == RLC_OP_STATUS_OK) {
-                len+=sprintf(&buffer[len],"RLC LCHAN %d, NB_SDU_TO_TX = %d\tNB_SDU_DISC %d\tNB_RX_SDU %d\n",
-                             UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.Lchan_id.Index,
-                             tx_pdcp_sdu,
-                             tx_pdcp_sdu_discarded,
-                             rx_sdu);
-                len+=sprintf(&buffer[len],"RLC LCHAN %d, NB_TB_TX_DATA = %d\tNB_TB_TX_CONTROL %d\tNB_TX_TB_RETRANS %d",
-                             UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.Lchan_id.Index,
-                             tx_data_pdu,
-                             tx_control_pdu,
-                             tx_retransmit_pdu);
-                len+=sprintf(&buffer[len],"\tRLC LCHAN %d, NB_TX_TB_RETRANS_BY_STATUS = %d\tNB_TX_TB_RETRANS_PADD %d\n",
-                             UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.Lchan_id.Index,
-                             tx_retransmit_pdu_by_status,
-                             tx_retransmit_pdu_unblock);
-                len+=sprintf(&buffer[len],"RLC LCHAN %d, NB_RX_DATA = %d\tNB_RX_TB_OUT_WIN %d\tNB_RX_TB_CORRUPT %d\n",
-                             UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.Lchan_id.Index,
-                             rx_data_pdu,
-                             rx_data_pdu_out_of_window,
-                             rx_error_pdu);
-              }
-
-              len+=sprintf(&buffer[len],"[MAC]: LCHAN %d, NB_TX_MAC = %d (%d bits/TTI, %d kbits/s), NB_RX_MAC = %d (%d errors)\n",
-                           UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.Lchan_id.Index,
-                           UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.NB_TX,
-                           UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.output_rate,
-                           (10*UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.output_rate)>>5,
-                           UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.NB_RX,
-                           UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.NB_RX_ERRORS);
-              len+=sprintf(&buffer[len],"        TX per TB: ");
-
-              for(kk=0; kk<MAX_NUMBER_TB_PER_LCHAN/2; kk++) {
-                len+=sprintf(&buffer[len],"%d . ",UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.NB_TX_TB[kk]);
-              }
-
-              len+=sprintf(&buffer[len],"\n");
-              len+=sprintf(&buffer[len],"        RXerr per TB: ");
-
-              for(kk=0; kk<MAX_NUMBER_TB_PER_LCHAN/2; kk++)
-                len+=sprintf(&buffer[len],"%d/%d . ",UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.NB_RX_ERRORS_TB[kk],
-                             UE_mac_inst[Mod_id].Dtch_lchan[i][CH_index].Lchan_info.NB_RX_TB[kk]);
-
-              len+=sprintf(&buffer[len],"\n");
-
-
-
-            }
-
-          }
-        }
-      }
-
-#endif //PHY_EMUL_ONE_MACHINE
-    }
-
-    else if(Mac_rlc_xface->Is_cluster_head[k] ==1) {
-
-      Mod_id=k;
-      len+=sprintf(&buffer[len],
-                   "-------------------------------------------------------------------CH %d: TTI: %d------------------------------------------------------------------\n",
-                   NODE_ID[Mod_id],Mac_rlc_xface->frame);
-
-      for(i=1; i<=NB_CNX_CH; i++) {
-        if (CH_mac_inst[Mod_id].Dcch_lchan[i].Active==1) {
-          len+=sprintf(&buffer[len],"\nMR index %d: DL SINR (feedback) %d dB, CQI: %s\n\n",
-                       i,//CH_rrc_inst[Mod_id].Info.UE_list[i].L2_id[0],
-                       CH_mac_inst[Mod_id].Def_meas[i].Wideband_sinr);
-          //print_cqi(CH_mac_inst[Mod_id].Def_meas[i].cqi));
-
-          len+=sprintf(&buffer[len],
-                       "[MAC] LCHAN %d (DCCH), NB_TX_MAC= %d (%d bits/TTI, %d kbits/s), NB_RX_MAC= %d (errors %d, sacch errors %d, sach errors %d, sach_missing %d)\n\n",
-                       CH_mac_inst[Mod_id].Dcch_lchan[i].Lchan_info.Lchan_id.Index,
-                       CH_mac_inst[Mod_id].Dcch_lchan[i].Lchan_info.NB_TX,
-                       CH_mac_inst[Mod_id].Dcch_lchan[i].Lchan_info.output_rate,
-                       (10*CH_mac_inst[Mod_id].Dcch_lchan[i].Lchan_info.output_rate)>>5,
-                       CH_mac_inst[Mod_id].Dcch_lchan[i].Lchan_info.NB_RX,
-                       CH_mac_inst[Mod_id].Dcch_lchan[i].Lchan_info.NB_RX_ERRORS,
-                       CH_mac_inst[Mod_id].Dcch_lchan[i].Lchan_info.NB_RX_SACCH_ERRORS,
-                       CH_mac_inst[Mod_id].Dcch_lchan[i].Lchan_info.NB_RX_SACH_ERRORS,
-                       CH_mac_inst[Mod_id].Dcch_lchan[i].Lchan_info.NB_RX_SACH_MISSING);
-
-          for(j=0; j<NB_RAB_MAX; j++) {
-            if (CH_mac_inst[Mod_id].Dtch_lchan[j][i].Active==1) {
-              Overhead=CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.output_rate - Pdcp_stats_tx_rate[k][i][j];
-
-              if(Overhead<0) {
-                Overhead=-Overhead;
-                Sign=-1;
-              } else {
-                Sign=1;
-              }
-
-              len+=sprintf(&buffer[len],
-                           "[PDCP]LCHAN %d: NB_TX = %d ,Tx_rate =(%d bits/TTI ,%d Kbits/s), NB_RX = %d ,Rx_rate =(%d bits/TTI ,%d Kbits/s), LAYER2 TX OVERHEAD= %d Kbits/s\n",
-                           CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.Lchan_id.Index,
-                           Pdcp_stats_tx[k][i][j],
-                           Pdcp_stats_tx_rate[k][i][j],
-                           (10*Pdcp_stats_tx_rate[k][i][j])>>5,
-                           Pdcp_stats_rx[k][i][j],
-                           Pdcp_stats_rx_rate[k][i][j],
-                           (10*Pdcp_stats_rx_rate[k][i][j])>>5,
-                           Sign*(10*Overhead)>>5);
-              int status =  rlc_stat_req     (k,
-                                              CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.Lchan_id.Index,
-                                              &tx_pdcp_sdu,
-                                              &tx_pdcp_sdu_discarded,
-                                              &tx_retransmit_pdu_unblock,
-                                              &tx_retransmit_pdu_by_status,
-                                              &tx_retransmit_pdu,
-                                              &tx_data_pdu,
-                                              &tx_control_pdu,
-                                              &rx_sdu,
-                                              &rx_error_pdu,
-                                              &rx_data_pdu,
-                                              &rx_data_pdu_out_of_window,
-                                              &rx_control_pdu) ;
-              /*
-              if (status == RLC_OP_STATUS_OK) {
-                len+=sprintf(&buffer[len],"\t[RLC] LCHAN %d, NB_SDU_TO_TX = %d\tNB_SDU_DISC %d\tNB_RX_SDU %d\n",
-                      CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.Lchan_id.Index,
-                   tx_pdcp_sdu,
-                   tx_pdcp_sdu_discarded,
-                   rx_sdu);
-                len+=sprintf(&buffer[len],"\t[RLC] LCHAN %d, NB_TB_TX_DATA = %d\tNB_TB_TX_CONTROL %d\tNB_TX_TB_RETRANS %\n",
-                      CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.Lchan_id.Index,
-                   tx_data_pdu,
-                   tx_control_pdu,
-                   tx_retransmit_pdu);
-                len+=sprintf(&buffer[len],"\t[RLC] LCHAN %d, NB_TX_TB_RETRANS_BY_STATUS = %d\tNB_TX_TB_RETRANS_PADD %d\n",
-                      CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.Lchan_id.Index,
-                   tx_retransmit_pdu_by_status,
-                   tx_retransmit_pdu_unblock);
-                len+=sprintf(&buffer[len],"\t[RLC] LCHAN %d, NB_RX_DATA = %d\tNB_RX_TB_OUT_WIN %d\tNB_RX_TB_CORRUPT %d\n",
-                      CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.Lchan_id.Index,
-                   rx_data_pdu,
-                   rx_data_pdu_out_of_window,
-                   rx_error_pdu);
-              }
-              */
-              len+=sprintf(&buffer[len],
-                           "[MAC]LCHAN %d (CNX %d,RAB %d), NB_TX_MAC= %d (%d bits/TTI, %d kbit/s), NB_RX_MAC= %d (errors %d, sacch_errors %d, sach_errors %d, sach_missing %d)\n",
-                           CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.Lchan_id.Index,
-                           i,j,
-                           CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.NB_TX,
-                           CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.output_rate,
-                           (10*CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.output_rate)>>5,
-                           CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.NB_RX,
-                           CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.NB_RX_ERRORS,
-                           CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.NB_RX_SACCH_ERRORS,
-                           CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.NB_RX_SACH_ERRORS,
-                           CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.NB_RX_SACH_MISSING);
-              len+=sprintf(&buffer[len],"[MAC][SCHEDULER] TX Arrival Rate %d, TX Service Rate %d, RX Arrival rate %d, RX Service rate %d, NB_BW_REQ_RX %d\n\n",
-                           CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.Arrival_rate,
-                           CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.Tx_rate,
-                           CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.Req_rate,
-                           CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.Rx_rate,
-                           CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.NB_BW_REQ_RX);
-
-              /*
-                      len+=sprintf(&buffer[len],"        TX per TB: ");
-                      for(kk=0;kk<MAX_NUMBER_TB_PER_LCHAN/2;kk++)
-                  len+=sprintf(&buffer[len],"%d.",CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.NB_TX_TB[kk]);
-                      len+=sprintf(&buffer[len],"\n");
-                      len+=sprintf(&buffer[len],"        RXerr per TB: ");
-                      for(kk=0;kk<MAX_NUMBER_TB_PER_LCHAN/2;kk++)
-                  len+=sprintf(&buffer[len],"%d/%d . ",CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.NB_RX_ERRORS_TB[kk],
-                         CH_mac_inst[Mod_id].Dtch_lchan[j][i].Lchan_info.NB_RX_TB[kk]);
-                      len+=sprintf(&buffer[len],"\n");
-              */
-            }
-          }
-        }
-      }
-
-    }
-  }
-
-  return len;
-}
-
-#ifndef USER_MODE
-static struct proc_dir_entry *proc_openair2_root;
-/*
- * Initialize the module and add the /proc file.
- */
-int add_openair2_stats()
-{
-  //#ifdef  KERNEL_VERSION_GREATER_THAN_2629
-  struct proc_dir_entry *pde;
-  //#endif
-
-  proc_openair2_root = proc_mkdir("openair2",0);
-  //#ifdef  KERNEL_VERSION_GREATER_THAN_2629
-  // pde = proc_create_entry("lchan_stats", S_IFREG | S_IRUGO, proc_openair2_root);
-  pde = create_proc_read_entry("lchan_stats", S_IFREG | S_IRUGO, proc_openair2_root, (read_proc_t*)&openair2_stats_read, NULL);
-
-  if (!pde) {
-    printk("[OPENAIR][ERROR] can't create proc entry !\n");
-  }
-
-  //#else
-  //create_proc_info_entry("lchan_stats", S_IFREG | S_IRUGO, proc_openair2_root, openair2_stats_read);
-  //#endif
-
-  return 0;
-}
-/*
- * Unregister the file when the module is closed.
- */
-void remove_openair2_stats()
-{
-
-  if (proc_openair2_root) {
-    printk("[OPENAIR][CLEANUP] Removing openair proc entry\n");
-    remove_proc_entry("lchan_stats", proc_openair2_root);
-    //#ifdef  KERNEL_VERSION_GREATER_THAN_2629
-
-    //#else
-    remove_proc_entry("openair2",NULL);
-    //#endif;
-  }
-}
-#endif
diff --git a/openair2/LAYER2/MAC/pre_processor.c b/openair2/LAYER2/MAC/pre_processor.c
index 612f64fe61c2f991fc24944d1debe28da7e0aafb..23d3f3bd603770684c0b2c4364d5f59e609e9668 100644
--- a/openair2/LAYER2/MAC/pre_processor.c
+++ b/openair2/LAYER2/MAC/pre_processor.c
@@ -95,389 +95,458 @@ int phy_stats_exist(module_id_t Mod_id, int rnti)
 */
 
 // This function stores the downlink buffer for all the logical channels
-void store_dlsch_buffer (module_id_t Mod_id,
-                         frame_t     frameP,
-                         sub_frame_t subframeP)
+void
+store_dlsch_buffer(module_id_t Mod_id, frame_t frameP,
+		   sub_frame_t subframeP)
 {
 
-  int                   UE_id,i;
-  rnti_t                rnti;
-  mac_rlc_status_resp_t rlc_status;
-  UE_list_t             *UE_list = &RC.mac[Mod_id]->UE_list;
-  UE_TEMPLATE           *UE_template;
+    int UE_id, i;
+    rnti_t rnti;
+    mac_rlc_status_resp_t rlc_status;
+    UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
+    UE_TEMPLATE *UE_template;
 
-  for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
-    if (UE_list->active[UE_id] != TRUE) continue;
-
-    UE_template = &UE_list->UE_template[UE_PCCID(Mod_id,UE_id)][UE_id];
-
-    // clear logical channel interface variables
-    UE_template->dl_buffer_total = 0;
-    UE_template->dl_pdus_total = 0;
+    for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
+	if (UE_list->active[UE_id] != TRUE)
+	    continue;
 
-    for(i=0; i< MAX_NUM_LCID; i++) {
-      UE_template->dl_buffer_info[i]=0;
-      UE_template->dl_pdus_in_buffer[i]=0;
-      UE_template->dl_buffer_head_sdu_creation_time[i]=0;
-      UE_template->dl_buffer_head_sdu_remaining_size_to_send[i]=0;
-    }
+	UE_template =
+	    &UE_list->UE_template[UE_PCCID(Mod_id, UE_id)][UE_id];
 
-    rnti = UE_RNTI(Mod_id,UE_id);
+	// clear logical channel interface variables
+	UE_template->dl_buffer_total = 0;
+	UE_template->dl_pdus_total = 0;
 
-    for(i=0; i< MAX_NUM_LCID; i++) { // loop over all the logical channels
+	for (i = 0; i < MAX_NUM_LCID; i++) {
+	    UE_template->dl_buffer_info[i] = 0;
+	    UE_template->dl_pdus_in_buffer[i] = 0;
+	    UE_template->dl_buffer_head_sdu_creation_time[i] = 0;
+	    UE_template->dl_buffer_head_sdu_remaining_size_to_send[i] = 0;
+	}
 
-      rlc_status = mac_rlc_status_ind(Mod_id,rnti, Mod_id,frameP,subframeP,ENB_FLAG_YES,MBMS_FLAG_NO,i,0 );
-      UE_template->dl_buffer_info[i] = rlc_status.bytes_in_buffer; //storing the dlsch buffer for each logical channel
-      UE_template->dl_pdus_in_buffer[i] = rlc_status.pdus_in_buffer;
-      UE_template->dl_buffer_head_sdu_creation_time[i] = rlc_status.head_sdu_creation_time ;
-      UE_template->dl_buffer_head_sdu_creation_time_max = cmax(UE_template->dl_buffer_head_sdu_creation_time_max,
-          rlc_status.head_sdu_creation_time );
-      UE_template->dl_buffer_head_sdu_remaining_size_to_send[i] = rlc_status.head_sdu_remaining_size_to_send;
-      UE_template->dl_buffer_head_sdu_is_segmented[i] = rlc_status.head_sdu_is_segmented;
-      UE_template->dl_buffer_total += UE_template->dl_buffer_info[i];//storing the total dlsch buffer
-      UE_template->dl_pdus_total   += UE_template->dl_pdus_in_buffer[i];
+	rnti = UE_RNTI(Mod_id, UE_id);
+
+	for (i = 0; i < MAX_NUM_LCID; i++) {	// loop over all the logical channels
+
+	    rlc_status =
+		mac_rlc_status_ind(Mod_id, rnti, Mod_id, frameP, subframeP,
+				   ENB_FLAG_YES, MBMS_FLAG_NO, i, 0);
+	    UE_template->dl_buffer_info[i] = rlc_status.bytes_in_buffer;	//storing the dlsch buffer for each logical channel
+	    UE_template->dl_pdus_in_buffer[i] = rlc_status.pdus_in_buffer;
+	    UE_template->dl_buffer_head_sdu_creation_time[i] =
+		rlc_status.head_sdu_creation_time;
+	    UE_template->dl_buffer_head_sdu_creation_time_max =
+		cmax(UE_template->dl_buffer_head_sdu_creation_time_max,
+		     rlc_status.head_sdu_creation_time);
+	    UE_template->dl_buffer_head_sdu_remaining_size_to_send[i] =
+		rlc_status.head_sdu_remaining_size_to_send;
+	    UE_template->dl_buffer_head_sdu_is_segmented[i] =
+		rlc_status.head_sdu_is_segmented;
+	    UE_template->dl_buffer_total += UE_template->dl_buffer_info[i];	//storing the total dlsch buffer
+	    UE_template->dl_pdus_total +=
+		UE_template->dl_pdus_in_buffer[i];
 
 #ifdef DEBUG_eNB_SCHEDULER
 
-      /* note for dl_buffer_head_sdu_remaining_size_to_send[i] :
-       * 0 if head SDU has not been segmented (yet), else remaining size not already segmented and sent
-       */
-      if (UE_template->dl_buffer_info[i]>0)
-        LOG_D(MAC,
-              "[eNB %d] Frame %d Subframe %d : RLC status for UE %d in LCID%d: total of %d pdus and size %d, head sdu queuing time %d, remaining size %d, is segmeneted %d \n",
-              Mod_id, frameP, subframeP, UE_id,
-              i, UE_template->dl_pdus_in_buffer[i],UE_template->dl_buffer_info[i],
-              UE_template->dl_buffer_head_sdu_creation_time[i],
-              UE_template->dl_buffer_head_sdu_remaining_size_to_send[i],
-              UE_template->dl_buffer_head_sdu_is_segmented[i]
-             );
+	    /* note for dl_buffer_head_sdu_remaining_size_to_send[i] :
+	     * 0 if head SDU has not been segmented (yet), else remaining size not already segmented and sent
+	     */
+	    if (UE_template->dl_buffer_info[i] > 0)
+		LOG_D(MAC,
+		      "[eNB %d] Frame %d Subframe %d : RLC status for UE %d in LCID%d: total of %d pdus and size %d, head sdu queuing time %d, remaining size %d, is segmeneted %d \n",
+		      Mod_id, frameP, subframeP, UE_id,
+		      i, UE_template->dl_pdus_in_buffer[i],
+		      UE_template->dl_buffer_info[i],
+		      UE_template->dl_buffer_head_sdu_creation_time[i],
+		      UE_template->
+		      dl_buffer_head_sdu_remaining_size_to_send[i],
+		      UE_template->dl_buffer_head_sdu_is_segmented[i]);
 
 #endif
 
-    }
+	}
 
-    //#ifdef DEBUG_eNB_SCHEDULER
-    if ( UE_template->dl_buffer_total>0)
-      LOG_D(MAC,"[eNB %d] Frame %d Subframe %d : RLC status for UE %d : total DL buffer size %d and total number of pdu %d \n",
-            Mod_id, frameP, subframeP, UE_id,
-            UE_template->dl_buffer_total,
-            UE_template->dl_pdus_total
-           );
+	//#ifdef DEBUG_eNB_SCHEDULER
+	if (UE_template->dl_buffer_total > 0)
+	    LOG_D(MAC,
+		  "[eNB %d] Frame %d Subframe %d : RLC status for UE %d : total DL buffer size %d and total number of pdu %d \n",
+		  Mod_id, frameP, subframeP, UE_id,
+		  UE_template->dl_buffer_total,
+		  UE_template->dl_pdus_total);
 
-    //#endif
-  }
+	//#endif
+    }
 }
 
 
 // This function returns the estimated number of RBs required by each UE for downlink scheduling
-void assign_rbs_required (module_id_t Mod_id,
-                          frame_t     frameP,
-                          sub_frame_t subframe,
-                          uint16_t    nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-                          int         min_rb_unit[MAX_NUM_CCs])
+void
+assign_rbs_required(module_id_t Mod_id,
+		    frame_t frameP,
+		    sub_frame_t subframe,
+		    uint16_t
+		    nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
+		    int min_rb_unit[MAX_NUM_CCs])
 {
 
-  uint16_t         TBS = 0;
-
-  int              UE_id,n,i,j,CC_id,pCCid,tmp;
-  UE_list_t        *UE_list = &RC.mac[Mod_id]->UE_list;
-  eNB_UE_STATS     *eNB_UE_stats,*eNB_UE_stats_i,*eNB_UE_stats_j;
-  int N_RB_DL;
-
-  // clear rb allocations across all CC_id
-  for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
-    if (UE_list->active[UE_id] != TRUE) continue;
-
-    pCCid = UE_PCCID(Mod_id,UE_id);
-
-    //update CQI information across component carriers
-    for (n=0; n<UE_list->numactiveCCs[UE_id]; n++) {
+    uint16_t TBS = 0;
 
-      CC_id = UE_list->ordered_CCids[n][UE_id];
-      eNB_UE_stats = &UE_list->eNB_UE_stats[CC_id][UE_id];
+    int UE_id, n, i, j, CC_id, pCCid, tmp;
+    UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
+    eNB_UE_STATS *eNB_UE_stats, *eNB_UE_stats_i, *eNB_UE_stats_j;
+    int N_RB_DL;
 
-      eNB_UE_stats->dlsch_mcs1=cqi_to_mcs[UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id]];
-
-    }
-
-    // provide the list of CCs sorted according to MCS
-    for (i=0; i<UE_list->numactiveCCs[UE_id]; i++) {
-      eNB_UE_stats_i = &UE_list->eNB_UE_stats[UE_list->ordered_CCids[i][UE_id]][UE_id];
-      for (j=i+1; j<UE_list->numactiveCCs[UE_id]; j++) {
-        DevAssert( j < MAX_NUM_CCs );
-	eNB_UE_stats_j = &UE_list->eNB_UE_stats[UE_list->ordered_CCids[j][UE_id]][UE_id];
-        if (eNB_UE_stats_j->dlsch_mcs1 >
-            eNB_UE_stats_i->dlsch_mcs1) {
-          tmp = UE_list->ordered_CCids[i][UE_id];
-          UE_list->ordered_CCids[i][UE_id] = UE_list->ordered_CCids[j][UE_id];
-          UE_list->ordered_CCids[j][UE_id] = tmp;
-        }
-      }
-    }
-
-    if (UE_list->UE_template[pCCid][UE_id].dl_buffer_total> 0) {
-      LOG_D(MAC,"[preprocessor] assign RB for UE %d\n",UE_id);
-
-      for (i=0; i<UE_list->numactiveCCs[UE_id]; i++) {
-        CC_id = UE_list->ordered_CCids[i][UE_id];
-	eNB_UE_stats = &UE_list->eNB_UE_stats[CC_id][UE_id];
-
-        if (eNB_UE_stats->dlsch_mcs1==0) {
-          nb_rbs_required[CC_id][UE_id] = 4;  // don't let the TBS get too small
-        } else {
-          nb_rbs_required[CC_id][UE_id] = min_rb_unit[CC_id];
-        }
+    // clear rb allocations across all CC_id
+    for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
+	if (UE_list->active[UE_id] != TRUE)
+	    continue;
 
-        TBS = get_TBS_DL(eNB_UE_stats->dlsch_mcs1,nb_rbs_required[CC_id][UE_id]);
+	pCCid = UE_PCCID(Mod_id, UE_id);
 
-        LOG_D(MAC,"[preprocessor] start RB assignement for UE %d CC_id %d dl buffer %d (RB unit %d, MCS %d, TBS %d) \n",
-              UE_id, CC_id, UE_list->UE_template[pCCid][UE_id].dl_buffer_total,
-              nb_rbs_required[CC_id][UE_id],eNB_UE_stats->dlsch_mcs1,TBS);
+	//update CQI information across component carriers
+	for (n = 0; n < UE_list->numactiveCCs[UE_id]; n++) {
 
-	N_RB_DL = to_prb(RC.mac[Mod_id]->common_channels[CC_id].mib->message.dl_Bandwidth);
+	    CC_id = UE_list->ordered_CCids[n][UE_id];
+	    eNB_UE_stats = &UE_list->eNB_UE_stats[CC_id][UE_id];
 
-        /* calculating required number of RBs for each UE */
-        while (TBS < UE_list->UE_template[pCCid][UE_id].dl_buffer_total)  {
-          nb_rbs_required[CC_id][UE_id] += min_rb_unit[CC_id];
+	    eNB_UE_stats->dlsch_mcs1 =
+		cqi_to_mcs[UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id]];
 
-          if (nb_rbs_required[CC_id][UE_id] > N_RB_DL) {
-            TBS = get_TBS_DL(eNB_UE_stats->dlsch_mcs1,N_RB_DL);
-            nb_rbs_required[CC_id][UE_id] = N_RB_DL;
-            break;
-          }
+	}
 
-          TBS = get_TBS_DL(eNB_UE_stats->dlsch_mcs1,nb_rbs_required[CC_id][UE_id]);
-        } // end of while
+	// provide the list of CCs sorted according to MCS
+	for (i = 0; i < UE_list->numactiveCCs[UE_id]; i++) {
+	    eNB_UE_stats_i =
+		&UE_list->eNB_UE_stats[UE_list->
+				       ordered_CCids[i][UE_id]][UE_id];
+	    for (j = i + 1; j < UE_list->numactiveCCs[UE_id]; j++) {
+		DevAssert(j < MAX_NUM_CCs);
+		eNB_UE_stats_j =
+		    &UE_list->
+		    eNB_UE_stats[UE_list->ordered_CCids[j][UE_id]][UE_id];
+		if (eNB_UE_stats_j->dlsch_mcs1 >
+		    eNB_UE_stats_i->dlsch_mcs1) {
+		    tmp = UE_list->ordered_CCids[i][UE_id];
+		    UE_list->ordered_CCids[i][UE_id] =
+			UE_list->ordered_CCids[j][UE_id];
+		    UE_list->ordered_CCids[j][UE_id] = tmp;
+		}
+	    }
+	}
 
-        LOG_D(MAC,"[eNB %d] Frame %d: UE %d on CC %d: RB unit %d,  nb_required RB %d (TBS %d, mcs %d)\n",
-              Mod_id, frameP,UE_id, CC_id,  min_rb_unit[CC_id], nb_rbs_required[CC_id][UE_id], TBS, eNB_UE_stats->dlsch_mcs1);
-      }
+	if (UE_list->UE_template[pCCid][UE_id].dl_buffer_total > 0) {
+	    LOG_D(MAC, "[preprocessor] assign RB for UE %d\n", UE_id);
+
+	    for (i = 0; i < UE_list->numactiveCCs[UE_id]; i++) {
+		CC_id = UE_list->ordered_CCids[i][UE_id];
+		eNB_UE_stats = &UE_list->eNB_UE_stats[CC_id][UE_id];
+
+		if (eNB_UE_stats->dlsch_mcs1 == 0) {
+		    nb_rbs_required[CC_id][UE_id] = 4;	// don't let the TBS get too small
+		} else {
+		    nb_rbs_required[CC_id][UE_id] = min_rb_unit[CC_id];
+		}
+
+		TBS =
+		    get_TBS_DL(eNB_UE_stats->dlsch_mcs1,
+			       nb_rbs_required[CC_id][UE_id]);
+
+		LOG_D(MAC,
+		      "[preprocessor] start RB assignement for UE %d CC_id %d dl buffer %d (RB unit %d, MCS %d, TBS %d) \n",
+		      UE_id, CC_id,
+		      UE_list->UE_template[pCCid][UE_id].dl_buffer_total,
+		      nb_rbs_required[CC_id][UE_id],
+		      eNB_UE_stats->dlsch_mcs1, TBS);
+
+		N_RB_DL =
+		    to_prb(RC.mac[Mod_id]->common_channels[CC_id].
+			   mib->message.dl_Bandwidth);
+
+		/* calculating required number of RBs for each UE */
+		while (TBS <
+		       UE_list->UE_template[pCCid][UE_id].
+		       dl_buffer_total) {
+		    nb_rbs_required[CC_id][UE_id] += min_rb_unit[CC_id];
+
+		    if (nb_rbs_required[CC_id][UE_id] > N_RB_DL) {
+			TBS =
+			    get_TBS_DL(eNB_UE_stats->dlsch_mcs1, N_RB_DL);
+			nb_rbs_required[CC_id][UE_id] = N_RB_DL;
+			break;
+		    }
+
+		    TBS =
+			get_TBS_DL(eNB_UE_stats->dlsch_mcs1,
+				   nb_rbs_required[CC_id][UE_id]);
+		}		// end of while
+
+		LOG_D(MAC,
+		      "[eNB %d] Frame %d: UE %d on CC %d: RB unit %d,  nb_required RB %d (TBS %d, mcs %d)\n",
+		      Mod_id, frameP, UE_id, CC_id, min_rb_unit[CC_id],
+		      nb_rbs_required[CC_id][UE_id], TBS,
+		      eNB_UE_stats->dlsch_mcs1);
+	    }
+	}
     }
-  }
 }
 
 
 // This function scans all CC_ids for a particular UE to find the maximum round index of its HARQ processes
 
-int maxround(module_id_t Mod_id,uint16_t rnti,int frame,sub_frame_t subframe,uint8_t ul_flag )
+int
+maxround(module_id_t Mod_id, uint16_t rnti, int frame,
+	 sub_frame_t subframe, uint8_t ul_flag)
 {
 
-  uint8_t round,round_max=0,UE_id;
-  int CC_id,harq_pid;
-  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
-  COMMON_channels_t *cc;
+    uint8_t round, round_max = 0, UE_id;
+    int CC_id, harq_pid;
+    UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
+    COMMON_channels_t *cc;
 
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
 
-    cc = &RC.mac[Mod_id]->common_channels[CC_id];
+	cc = &RC.mac[Mod_id]->common_channels[CC_id];
 
-    UE_id = find_UE_id(Mod_id,rnti);
-    if (cc->tdd_Config) harq_pid = ((frame*10)+subframe)%10;
-    else harq_pid = ((frame*10)+subframe)&7;
+	UE_id = find_UE_id(Mod_id, rnti);
+	if (cc->tdd_Config)
+	    harq_pid = ((frame * 10) + subframe) % 10;
+	else
+	    harq_pid = ((frame * 10) + subframe) & 7;
 
-    round    = UE_list->UE_sched_ctrl[UE_id].round[CC_id][harq_pid];
-    if (round > round_max) {
-      round_max = round;
+	round = UE_list->UE_sched_ctrl[UE_id].round[CC_id][harq_pid];
+	if (round > round_max) {
+	    round_max = round;
+	}
     }
-  }
 
-  return round_max;
+    return round_max;
 }
 
 // This function scans all CC_ids for a particular UE to find the maximum DL CQI
 // it returns -1 if the UE is not found in PHY layer (get_eNB_UE_stats gives NULL)
-int maxcqi(module_id_t Mod_id,int32_t UE_id)
+int maxcqi(module_id_t Mod_id, int32_t UE_id)
 {
-  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
-  int CC_id,n;
-  int CQI = 0;
+    UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
+    int CC_id, n;
+    int CQI = 0;
 
-  for (n=0; n<UE_list->numactiveCCs[UE_id]; n++) {
-    CC_id = UE_list->ordered_CCids[n][UE_id];
+    for (n = 0; n < UE_list->numactiveCCs[UE_id]; n++) {
+	CC_id = UE_list->ordered_CCids[n][UE_id];
 
-    if (UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id] > CQI) {
-      CQI = UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id];
+	if (UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id] > CQI) {
+	    CQI = UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id];
+	}
     }
-  }
 
-  return CQI;
+    return CQI;
 }
 
 struct sort_ue_dl_params {
-  int Mod_idP;
-  int frameP;
-  int subframeP;
+    int Mod_idP;
+    int frameP;
+    int subframeP;
 };
 
 static int ue_dl_compare(const void *_a, const void *_b, void *_params)
 {
-  struct sort_ue_dl_params *params = _params;
-  UE_list_t *UE_list = &RC.mac[params->Mod_idP]->UE_list;
-
-  int UE_id1 = *(const int *)_a;
-  int UE_id2 = *(const int *)_b;
-
-  int rnti1  = UE_RNTI(params->Mod_idP, UE_id1);
-  int pCC_id1 = UE_PCCID(params->Mod_idP, UE_id1);
-  int round1 = maxround(params->Mod_idP, rnti1, params->frameP, params->subframeP, 1);
-
-  int rnti2  = UE_RNTI(params->Mod_idP, UE_id2);
-  int pCC_id2 = UE_PCCID(params->Mod_idP, UE_id2);
-  int round2 = maxround(params->Mod_idP, rnti2, params->frameP, params->subframeP, 1);
-
-  int cqi1 = maxcqi(params->Mod_idP, UE_id1);
-  int cqi2 = maxcqi(params->Mod_idP, UE_id2);
-
-  if (round1 > round2) return -1;
-  if (round1 < round2) return 1;
-
-  if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] + UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2] >
-      UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] + UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])
-    return -1;
-  if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] + UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2] <
-      UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] + UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])
-    return 1;
-
-  if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_head_sdu_creation_time_max >
-      UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_head_sdu_creation_time_max)
-    return -1;
-  if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_head_sdu_creation_time_max <
-      UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_head_sdu_creation_time_max)
-    return 1;
-
-  if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total >
-      UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total)
-    return -1;
-  if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total <
-      UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total)
-    return 1;
-
-  if (cqi1 > cqi2) return -1;
-  if (cqi1 < cqi2) return 1;
-
-  return 0;
+    struct sort_ue_dl_params *params = _params;
+    UE_list_t *UE_list = &RC.mac[params->Mod_idP]->UE_list;
+
+    int UE_id1 = *(const int *) _a;
+    int UE_id2 = *(const int *) _b;
+
+    int rnti1 = UE_RNTI(params->Mod_idP, UE_id1);
+    int pCC_id1 = UE_PCCID(params->Mod_idP, UE_id1);
+    int round1 =
+	maxround(params->Mod_idP, rnti1, params->frameP, params->subframeP,
+		 1);
+
+    int rnti2 = UE_RNTI(params->Mod_idP, UE_id2);
+    int pCC_id2 = UE_PCCID(params->Mod_idP, UE_id2);
+    int round2 =
+	maxround(params->Mod_idP, rnti2, params->frameP, params->subframeP,
+		 1);
+
+    int cqi1 = maxcqi(params->Mod_idP, UE_id1);
+    int cqi2 = maxcqi(params->Mod_idP, UE_id2);
+
+    if (round1 > round2)
+	return -1;
+    if (round1 < round2)
+	return 1;
+
+    if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] +
+	UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2] >
+	UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] +
+	UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])
+	return -1;
+    if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] +
+	UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2] <
+	UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] +
+	UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])
+	return 1;
+
+    if (UE_list->
+	UE_template[pCC_id1][UE_id1].dl_buffer_head_sdu_creation_time_max >
+	UE_list->
+	UE_template[pCC_id2][UE_id2].dl_buffer_head_sdu_creation_time_max)
+	return -1;
+    if (UE_list->
+	UE_template[pCC_id1][UE_id1].dl_buffer_head_sdu_creation_time_max <
+	UE_list->
+	UE_template[pCC_id2][UE_id2].dl_buffer_head_sdu_creation_time_max)
+	return 1;
+
+    if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total >
+	UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total)
+	return -1;
+    if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total <
+	UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total)
+	return 1;
+
+    if (cqi1 > cqi2)
+	return -1;
+    if (cqi1 < cqi2)
+	return 1;
+
+    return 0;
 #if 0
-  /* The above order derives from the following.  */
-      if(round2 > round1) { // Check first if one of the UEs has an active HARQ process which needs service and swap order
-        swap_UEs(UE_list,UE_id1,UE_id2,0);
-      } else if (round2 == round1) {
-        // RK->NN : I guess this is for fairness in the scheduling. This doesn't make sense unless all UEs have the same configuration of logical channels.  This should be done on the sum of all information that has to be sent.  And still it wouldn't ensure fairness.  It should be based on throughput seen by each UE or maybe using the head_sdu_creation_time, i.e. swap UEs if one is waiting longer for service.
-        //  for(j=0;j<MAX_NUM_LCID;j++){
-        //    if (eNB_mac_inst[Mod_id][pCC_id1].UE_template[UE_id1].dl_buffer_info[j] <
-        //      eNB_mac_inst[Mod_id][pCC_id2].UE_template[UE_id2].dl_buffer_info[j]){
-
-        // first check the buffer status for SRB1 and SRB2
-
-        if ( (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] + UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2]) <
-             (UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] + UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])   ) {
-          swap_UEs(UE_list,UE_id1,UE_id2,0);
-        } else if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_head_sdu_creation_time_max <
-                   UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_head_sdu_creation_time_max   ) {
-          swap_UEs(UE_list,UE_id1,UE_id2,0);
-        } else if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total <
-                   UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total   ) {
-          swap_UEs(UE_list,UE_id1,UE_id2,0);
-        } else if (cqi1 < cqi2) {
-          swap_UEs(UE_list,UE_id1,UE_id2,0);
-        }
-      }
+    /* The above order derives from the following.  */
+    if (round2 > round1) {	// Check first if one of the UEs has an active HARQ process which needs service and swap order
+	swap_UEs(UE_list, UE_id1, UE_id2, 0);
+    } else if (round2 == round1) {
+	// RK->NN : I guess this is for fairness in the scheduling. This doesn't make sense unless all UEs have the same configuration of logical channels.  This should be done on the sum of all information that has to be sent.  And still it wouldn't ensure fairness.  It should be based on throughput seen by each UE or maybe using the head_sdu_creation_time, i.e. swap UEs if one is waiting longer for service.
+	//  for(j=0;j<MAX_NUM_LCID;j++){
+	//    if (eNB_mac_inst[Mod_id][pCC_id1].UE_template[UE_id1].dl_buffer_info[j] <
+	//      eNB_mac_inst[Mod_id][pCC_id2].UE_template[UE_id2].dl_buffer_info[j]){
+
+	// first check the buffer status for SRB1 and SRB2
+
+	if ((UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] +
+	     UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2]) <
+	    (UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] +
+	     UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])) {
+	    swap_UEs(UE_list, UE_id1, UE_id2, 0);
+	} else if (UE_list->UE_template[pCC_id1]
+		   [UE_id1].dl_buffer_head_sdu_creation_time_max <
+		   UE_list->UE_template[pCC_id2]
+		   [UE_id2].dl_buffer_head_sdu_creation_time_max) {
+	    swap_UEs(UE_list, UE_id1, UE_id2, 0);
+	} else if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total <
+		   UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total) {
+	    swap_UEs(UE_list, UE_id1, UE_id2, 0);
+	} else if (cqi1 < cqi2) {
+	    swap_UEs(UE_list, UE_id1, UE_id2, 0);
+	}
+    }
 #endif
 }
 
 // This fuction sorts the UE in order their dlsch buffer and CQI
-void sort_UEs (module_id_t Mod_idP,
-               int         frameP,
-               sub_frame_t subframeP)
+void sort_UEs(module_id_t Mod_idP, int frameP, sub_frame_t subframeP)
 {
-  int               i;
-  int               list[NUMBER_OF_UE_MAX];
-  int               list_size = 0;
-  int               rnti;
-  struct sort_ue_dl_params params = { Mod_idP, frameP, subframeP };
+    int i;
+    int list[NUMBER_OF_UE_MAX];
+    int list_size = 0;
+    int rnti;
+    struct sort_ue_dl_params params = { Mod_idP, frameP, subframeP };
 
-  UE_list_t *UE_list = &RC.mac[Mod_idP]->UE_list;
+    UE_list_t *UE_list = &RC.mac[Mod_idP]->UE_list;
 
-  for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
+    for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
 
-    if (UE_list->active[i]==FALSE) continue;
-    if ((rnti = UE_RNTI(Mod_idP, i)) == NOT_A_RNTI) continue;
-    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1) continue;
+	if (UE_list->active[i] == FALSE)
+	    continue;
+	if ((rnti = UE_RNTI(Mod_idP, i)) == NOT_A_RNTI)
+	    continue;
+	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+	    continue;
 
-    list[list_size] = i;
-    list_size++;
-  }
+	list[list_size] = i;
+	list_size++;
+    }
 
-  qsort_r(list, list_size, sizeof(int), ue_dl_compare, &params);
+    qsort_r(list, list_size, sizeof(int), ue_dl_compare, &params);
 
-  if (list_size) {
-    for (i = 0; i < list_size-1; i++)
-      UE_list->next[list[i]] = list[i+1];
-    UE_list->next[list[list_size-1]] = -1;
-    UE_list->head = list[0];
-  } else {
-    UE_list->head = -1;
-  }
+    if (list_size) {
+	for (i = 0; i < list_size - 1; i++)
+	    UE_list->next[list[i]] = list[i + 1];
+	UE_list->next[list[list_size - 1]] = -1;
+	UE_list->head = list[0];
+    } else {
+	UE_list->head = -1;
+    }
 
 #if 0
 
 
-  int               UE_id1,UE_id2;
-  int               pCC_id1,pCC_id2;
-  int               cqi1,cqi2,round1,round2;
-  int               i=0,ii=0;//,j=0;
-  rnti_t            rnti1,rnti2;
-
-  UE_list_t *UE_list = &RC.mac[Mod_idP]->UE_list;
-
-  for (i=UE_list->head; i>=0; i=UE_list->next[i]) {
-
-    for(ii=UE_list->next[i]; ii>=0; ii=UE_list->next[ii]) {
-
-      UE_id1  = i;
-      rnti1 = UE_RNTI(Mod_idP,UE_id1);
-      if(rnti1 == NOT_A_RNTI)
-	continue;
-      if (UE_list->UE_sched_ctrl[UE_id1].ul_out_of_sync == 1)
-	continue;
-      pCC_id1 = UE_PCCID(Mod_idP,UE_id1);
-      cqi1    = maxcqi(Mod_idP,UE_id1); //
-      round1  = maxround(Mod_idP,rnti1,frameP,subframeP,0);
-
-      UE_id2 = ii;
-      rnti2 = UE_RNTI(Mod_idP,UE_id2);
-      if(rnti2 == NOT_A_RNTI)
-        continue;
-      if (UE_list->UE_sched_ctrl[UE_id2].ul_out_of_sync == 1)
-	continue;
-      cqi2    = maxcqi(Mod_idP,UE_id2);
-      round2  = maxround(Mod_idP,rnti2,frameP,subframeP,0);  //mac_xface->get_ue_active_harq_pid(Mod_id,rnti2,subframe,&harq_pid2,&round2,0);
-      pCC_id2 = UE_PCCID(Mod_idP,UE_id2);
-
-      if(round2 > round1) { // Check first if one of the UEs has an active HARQ process which needs service and swap order
-        swap_UEs(UE_list,UE_id1,UE_id2,0);
-      } else if (round2 == round1) {
-        // RK->NN : I guess this is for fairness in the scheduling. This doesn't make sense unless all UEs have the same configuration of logical channels.  This should be done on the sum of all information that has to be sent.  And still it wouldn't ensure fairness.  It should be based on throughput seen by each UE or maybe using the head_sdu_creation_time, i.e. swap UEs if one is waiting longer for service.
-        //  for(j=0;j<MAX_NUM_LCID;j++){
-        //    if (eNB_mac_inst[Mod_id][pCC_id1].UE_template[UE_id1].dl_buffer_info[j] <
-        //      eNB_mac_inst[Mod_id][pCC_id2].UE_template[UE_id2].dl_buffer_info[j]){
-
-        // first check the buffer status for SRB1 and SRB2
-
-        if ( (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] + UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2]) <
-             (UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] + UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])   ) {
-          swap_UEs(UE_list,UE_id1,UE_id2,0);
-        } else if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_head_sdu_creation_time_max <
-                   UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_head_sdu_creation_time_max   ) {
-          swap_UEs(UE_list,UE_id1,UE_id2,0);
-        } else if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total <
-                   UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total   ) {
-          swap_UEs(UE_list,UE_id1,UE_id2,0);
-        } else if (cqi1 < cqi2) {
-          swap_UEs(UE_list,UE_id1,UE_id2,0);
-        }
-      }
+    int UE_id1, UE_id2;
+    int pCC_id1, pCC_id2;
+    int cqi1, cqi2, round1, round2;
+    int i = 0, ii = 0;		//,j=0;
+    rnti_t rnti1, rnti2;
+
+    UE_list_t *UE_list = &RC.mac[Mod_idP]->UE_list;
+
+    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
+
+	for (ii = UE_list->next[i]; ii >= 0; ii = UE_list->next[ii]) {
+
+	    UE_id1 = i;
+	    rnti1 = UE_RNTI(Mod_idP, UE_id1);
+	    if (rnti1 == NOT_A_RNTI)
+		continue;
+	    if (UE_list->UE_sched_ctrl[UE_id1].ul_out_of_sync == 1)
+		continue;
+	    pCC_id1 = UE_PCCID(Mod_idP, UE_id1);
+	    cqi1 = maxcqi(Mod_idP, UE_id1);	//
+	    round1 = maxround(Mod_idP, rnti1, frameP, subframeP, 0);
+
+	    UE_id2 = ii;
+	    rnti2 = UE_RNTI(Mod_idP, UE_id2);
+	    if (rnti2 == NOT_A_RNTI)
+		continue;
+	    if (UE_list->UE_sched_ctrl[UE_id2].ul_out_of_sync == 1)
+		continue;
+	    cqi2 = maxcqi(Mod_idP, UE_id2);
+	    round2 = maxround(Mod_idP, rnti2, frameP, subframeP, 0);	//mac_xface->get_ue_active_harq_pid(Mod_id,rnti2,subframe,&harq_pid2,&round2,0);
+	    pCC_id2 = UE_PCCID(Mod_idP, UE_id2);
+
+	    if (round2 > round1) {	// Check first if one of the UEs has an active HARQ process which needs service and swap order
+		swap_UEs(UE_list, UE_id1, UE_id2, 0);
+	    } else if (round2 == round1) {
+		// RK->NN : I guess this is for fairness in the scheduling. This doesn't make sense unless all UEs have the same configuration of logical channels.  This should be done on the sum of all information that has to be sent.  And still it wouldn't ensure fairness.  It should be based on throughput seen by each UE or maybe using the head_sdu_creation_time, i.e. swap UEs if one is waiting longer for service.
+		//  for(j=0;j<MAX_NUM_LCID;j++){
+		//    if (eNB_mac_inst[Mod_id][pCC_id1].UE_template[UE_id1].dl_buffer_info[j] <
+		//      eNB_mac_inst[Mod_id][pCC_id2].UE_template[UE_id2].dl_buffer_info[j]){
+
+		// first check the buffer status for SRB1 and SRB2
+
+		if ((UE_list->UE_template[pCC_id1][UE_id1].
+		     dl_buffer_info[1] +
+		     UE_list->UE_template[pCC_id1][UE_id1].
+		     dl_buffer_info[2]) <
+		    (UE_list->UE_template[pCC_id2][UE_id2].
+		     dl_buffer_info[1] +
+		     UE_list->UE_template[pCC_id2][UE_id2].
+		     dl_buffer_info[2])) {
+		    swap_UEs(UE_list, UE_id1, UE_id2, 0);
+		} else if (UE_list->UE_template[pCC_id1]
+			   [UE_id1].dl_buffer_head_sdu_creation_time_max <
+			   UE_list->UE_template[pCC_id2]
+			   [UE_id2].dl_buffer_head_sdu_creation_time_max) {
+		    swap_UEs(UE_list, UE_id1, UE_id2, 0);
+		} else if (UE_list->UE_template[pCC_id1][UE_id1].
+			   dl_buffer_total <
+			   UE_list->UE_template[pCC_id2][UE_id2].
+			   dl_buffer_total) {
+		    swap_UEs(UE_list, UE_id1, UE_id2, 0);
+		} else if (cqi1 < cqi2) {
+		    swap_UEs(UE_list, UE_id1, UE_id2, 0);
+		}
+	    }
+	}
     }
-  }
 #endif
 }
 
@@ -485,1050 +554,1297 @@ void sort_UEs (module_id_t Mod_idP,
 
 
 // This function assigns pre-available RBS to each UE in specified sub-bands before scheduling is done
-void dlsch_scheduler_pre_processor (module_id_t   Mod_id,
-                                    frame_t       frameP,
-                                    sub_frame_t   subframeP,
-                                    int           N_RBG[MAX_NUM_CCs],
-                                    int           *mbsfn_flag)
+void
+dlsch_scheduler_pre_processor(module_id_t Mod_id,
+			      frame_t frameP,
+			      sub_frame_t subframeP,
+			      int N_RBG[MAX_NUM_CCs], int *mbsfn_flag)
 {
 
-  unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX],harq_pid=0,round=0,total_ue_count;
-  unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX];
-  int                     UE_id, i; 
-  uint16_t                ii,j;
-  uint16_t                nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
-  uint16_t                nb_rbs_required_remaining[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
-  uint16_t                nb_rbs_required_remaining_1[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
-  uint16_t                average_rbs_per_user[MAX_NUM_CCs] = {0};
-  rnti_t             rnti;
-  int                min_rb_unit[MAX_NUM_CCs];
-  uint16_t r1=0;
-  uint8_t CC_id;
-  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
-
-  int N_RB_DL;
-  int transmission_mode = 0;
-  UE_sched_ctrl *ue_sched_ctl;
-  //  int rrc_status           = RRC_IDLE;
-  COMMON_channels_t *cc;
+    unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX], harq_pid =
+	0, round = 0, total_ue_count;
+    unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX];
+    int UE_id, i;
+    uint16_t ii, j;
+    uint16_t nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
+    uint16_t nb_rbs_required_remaining[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
+    uint16_t nb_rbs_required_remaining_1[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
+    uint16_t average_rbs_per_user[MAX_NUM_CCs] = { 0 };
+    rnti_t rnti;
+    int min_rb_unit[MAX_NUM_CCs];
+    uint16_t r1 = 0;
+    uint8_t CC_id;
+    UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
+
+    int N_RB_DL;
+    int transmission_mode = 0;
+    UE_sched_ctrl *ue_sched_ctl;
+    //  int rrc_status           = RRC_IDLE;
+    COMMON_channels_t *cc;
 
 #ifdef TM5
-  int harq_pid1=0;
-  int round1=0,round2=0;
-  int UE_id2;
-  uint16_t                i1,i2,i3;
-  rnti_t             rnti1,rnti2;
-  LTE_eNB_UE_stats  *eNB_UE_stats1 = NULL;
-  LTE_eNB_UE_stats  *eNB_UE_stats2 = NULL;
-  UE_sched_ctrl *ue_sched_ctl1,*ue_sched_ctl2;
+    int harq_pid1 = 0;
+    int round1 = 0, round2 = 0;
+    int UE_id2;
+    uint16_t i1, i2, i3;
+    rnti_t rnti1, rnti2;
+    LTE_eNB_UE_stats *eNB_UE_stats1 = NULL;
+    LTE_eNB_UE_stats *eNB_UE_stats2 = NULL;
+    UE_sched_ctrl *ue_sched_ctl1, *ue_sched_ctl2;
 #endif
 
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
 
-    if (mbsfn_flag[CC_id]>0)  // If this CC is allocated for MBSFN skip it here
-      continue;
+	if (mbsfn_flag[CC_id] > 0)	// If this CC is allocated for MBSFN skip it here
+	    continue;
 
 
 
-    min_rb_unit[CC_id]=get_min_rb_unit(Mod_id,CC_id);
+	min_rb_unit[CC_id] = get_min_rb_unit(Mod_id, CC_id);
 
-    for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
-      if (UE_list->active[i] != TRUE) continue;
+	for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
+	    if (UE_list->active[i] != TRUE)
+		continue;
 
-      UE_id = i;
-      // Initialize scheduling information for all active UEs
-      
+	    UE_id = i;
+	    // Initialize scheduling information for all active UEs
 
 
-      dlsch_scheduler_pre_processor_reset(Mod_id,
-        UE_id,
-        CC_id,
-        frameP,
-        subframeP,
-        N_RBG[CC_id],
-        nb_rbs_required,
-        nb_rbs_required_remaining,
-        rballoc_sub,
-        MIMO_mode_indicator);
 
-    }
-  }
+	    dlsch_scheduler_pre_processor_reset(Mod_id,
+						UE_id,
+						CC_id,
+						frameP,
+						subframeP,
+						N_RBG[CC_id],
+						nb_rbs_required,
+						nb_rbs_required_remaining,
+						rballoc_sub,
+						MIMO_mode_indicator);
 
+	}
+    }
 
-  // Store the DLSCH buffer for each logical channel
-  store_dlsch_buffer (Mod_id,frameP,subframeP);
 
+    // Store the DLSCH buffer for each logical channel
+    store_dlsch_buffer(Mod_id, frameP, subframeP);
 
 
-  // Calculate the number of RBs required by each UE on the basis of logical channel's buffer
-  assign_rbs_required (Mod_id,frameP,subframeP,nb_rbs_required,min_rb_unit);
 
+    // Calculate the number of RBs required by each UE on the basis of logical channel's buffer
+    assign_rbs_required(Mod_id, frameP, subframeP, nb_rbs_required,
+			min_rb_unit);
 
 
-  // Sorts the user on the basis of dlsch logical channel buffer and CQI
-  sort_UEs (Mod_id,frameP,subframeP);
 
+    // Sorts the user on the basis of dlsch logical channel buffer and CQI
+    sort_UEs(Mod_id, frameP, subframeP);
 
 
-  total_ue_count =0;
 
-  // loop over all active UEs
-  for (i=UE_list->head; i>=0; i=UE_list->next[i]) {
-    rnti = UE_RNTI(Mod_id,i);
+    total_ue_count = 0;
 
-    if(rnti == NOT_A_RNTI)
-      continue;
-    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-      continue;
-    UE_id = i;
+    // loop over all active UEs
+    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
+	rnti = UE_RNTI(Mod_id, i);
 
-    for (ii=0; ii<UE_num_active_CC(UE_list,UE_id); ii++) {
-      CC_id = UE_list->ordered_CCids[ii][UE_id];
-      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-      cc=&RC.mac[Mod_id]->common_channels[ii];
-      if (cc->tdd_Config) harq_pid = ((frameP*10)+subframeP)%10;
-      else harq_pid = ((frameP*10)+subframeP)&7;
-      round    = ue_sched_ctl->round[CC_id][harq_pid];
+	if (rnti == NOT_A_RNTI)
+	    continue;
+	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+	    continue;
+	UE_id = i;
 
-      average_rbs_per_user[CC_id]=0;
+	for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
+	    CC_id = UE_list->ordered_CCids[ii][UE_id];
+	    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+	    cc = &RC.mac[Mod_id]->common_channels[ii];
+	    if (cc->tdd_Config)
+		harq_pid = ((frameP * 10) + subframeP) % 10;
+	    else
+		harq_pid = ((frameP * 10) + subframeP) & 7;
+	    round = ue_sched_ctl->round[CC_id][harq_pid];
 
+	    average_rbs_per_user[CC_id] = 0;
 
-      if(round != 8) {
-        nb_rbs_required[CC_id][UE_id] = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
-      }
 
-      //nb_rbs_required_remaining[UE_id] = nb_rbs_required[UE_id];
-      if (nb_rbs_required[CC_id][UE_id] > 0) {
-        total_ue_count = total_ue_count + 1;
-      }
+	    if (round != 8) {
+		nb_rbs_required[CC_id][UE_id] =
+		    UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
+	    }
+	    //nb_rbs_required_remaining[UE_id] = nb_rbs_required[UE_id];
+	    if (nb_rbs_required[CC_id][UE_id] > 0) {
+		total_ue_count = total_ue_count + 1;
+	    }
+	    // hypothetical assignment
+	    /*
+	     * If schedule is enabled and if the priority of the UEs is modified
+	     * The average rbs per logical channel per user will depend on the level of
+	     * priority. Concerning the hypothetical assignement, we should assign more
+	     * rbs to prioritized users. Maybe, we can do a mapping between the
+	     * average rbs per user and the level of priority or multiply the average rbs
+	     * per user by a coefficient which represents the degree of priority.
+	     */
+
+	    N_RB_DL =
+		to_prb(RC.mac[Mod_id]->common_channels[CC_id].mib->
+		       message.dl_Bandwidth);
+
+	    if (total_ue_count == 0) {
+		average_rbs_per_user[CC_id] = 0;
+	    } else if ((min_rb_unit[CC_id] * total_ue_count) <= (N_RB_DL)) {
+		average_rbs_per_user[CC_id] =
+		    (uint16_t) floor(N_RB_DL / total_ue_count);
+	    } else {
+		average_rbs_per_user[CC_id] = min_rb_unit[CC_id];	// consider the total number of use that can be scheduled UE
+	    }
+	}
+    }
 
+    // note: nb_rbs_required is assigned according to total_buffer_dl
+    // extend nb_rbs_required to capture per LCID RB required
+    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
+	rnti = UE_RNTI(Mod_id, i);
 
-      // hypothetical assignment
-      /*
-       * If schedule is enabled and if the priority of the UEs is modified
-       * The average rbs per logical channel per user will depend on the level of
-       * priority. Concerning the hypothetical assignement, we should assign more
-       * rbs to prioritized users. Maybe, we can do a mapping between the
-       * average rbs per user and the level of priority or multiply the average rbs
-       * per user by a coefficient which represents the degree of priority.
-       */
+	if (rnti == NOT_A_RNTI)
+	    continue;
+	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+	    continue;
 
-      N_RB_DL = to_prb(RC.mac[Mod_id]->common_channels[CC_id].mib->message.dl_Bandwidth);
+	for (ii = 0; ii < UE_num_active_CC(UE_list, i); ii++) {
+	    CC_id = UE_list->ordered_CCids[ii][i];
+	    ue_sched_ctl = &UE_list->UE_sched_ctrl[i];
+	    round = ue_sched_ctl->round[CC_id][harq_pid];
+
+	    // control channel or retransmission
+	    /* TODO: do we have to check for retransmission? */
+	    if (mac_eNB_get_rrc_status(Mod_id, rnti) < RRC_RECONFIGURED
+		|| round > 0) {
+		nb_rbs_required_remaining_1[CC_id][i] =
+		    nb_rbs_required[CC_id][i];
+	    } else {
+		nb_rbs_required_remaining_1[CC_id][i] =
+		    cmin(average_rbs_per_user[CC_id],
+			 nb_rbs_required[CC_id][i]);
 
-      if (total_ue_count == 0) {
-        average_rbs_per_user[CC_id] = 0;
-      } else if( (min_rb_unit[CC_id] * total_ue_count) <= (N_RB_DL) ) {
-        average_rbs_per_user[CC_id] = (uint16_t) floor(N_RB_DL/total_ue_count);
-      } else {
-        average_rbs_per_user[CC_id] = min_rb_unit[CC_id]; // consider the total number of use that can be scheduled UE
-      }
+	    }
+	}
     }
-  }
 
-  // note: nb_rbs_required is assigned according to total_buffer_dl
-  // extend nb_rbs_required to capture per LCID RB required
-  for(i=UE_list->head; i>=0; i=UE_list->next[i]) {
-    rnti = UE_RNTI(Mod_id,i);
-
-    if(rnti == NOT_A_RNTI)
-      continue;
-    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-      continue;
-
-    for (ii=0; ii<UE_num_active_CC(UE_list,i); ii++) {
-      CC_id = UE_list->ordered_CCids[ii][i];
-      ue_sched_ctl = &UE_list->UE_sched_ctrl[i];
-      round    = ue_sched_ctl->round[CC_id][harq_pid];
-
-      // control channel or retransmission
-      /* TODO: do we have to check for retransmission? */
-      if (mac_eNB_get_rrc_status(Mod_id,rnti) < RRC_RECONFIGURED || round > 0) {
-        nb_rbs_required_remaining_1[CC_id][i] = nb_rbs_required[CC_id][i];
-      } else {
-        nb_rbs_required_remaining_1[CC_id][i] = cmin(average_rbs_per_user[CC_id],nb_rbs_required[CC_id][i]);
-
-      }
-    }
-  }
+    //Allocation to UEs is done in 2 rounds,
+    // 1st stage: average number of RBs allocated to each UE
+    // 2nd stage: remaining RBs are allocated to high priority UEs
+    for (r1 = 0; r1 < 2; r1++) {
+
+	for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
+	    for (ii = 0; ii < UE_num_active_CC(UE_list, i); ii++) {
+		CC_id = UE_list->ordered_CCids[ii][i];
+
+		if (r1 == 0) {
+		    nb_rbs_required_remaining[CC_id][i] =
+			nb_rbs_required_remaining_1[CC_id][i];
+		} else {	// rb required based only on the buffer - rb allloctaed in the 1st round + extra reaming rb form the 1st round
+		    nb_rbs_required_remaining[CC_id][i] =
+			nb_rbs_required[CC_id][i] -
+			nb_rbs_required_remaining_1[CC_id][i] +
+			nb_rbs_required_remaining[CC_id][i];
+		    if (nb_rbs_required_remaining[CC_id][i] < 0)
+			abort();
+		}
+
+		if (nb_rbs_required[CC_id][i] > 0)
+		    LOG_D(MAC,
+			  "round %d : nb_rbs_required_remaining[%d][%d]= %d (remaining_1 %d, required %d,  pre_nb_available_rbs %d, N_RBG %d, rb_unit %d)\n",
+			  r1, CC_id, i,
+			  nb_rbs_required_remaining[CC_id][i],
+			  nb_rbs_required_remaining_1[CC_id][i],
+			  nb_rbs_required[CC_id][i],
+			  UE_list->UE_sched_ctrl[i].
+			  pre_nb_available_rbs[CC_id], N_RBG[CC_id],
+			  min_rb_unit[CC_id]);
 
-  //Allocation to UEs is done in 2 rounds,
-  // 1st stage: average number of RBs allocated to each UE
-  // 2nd stage: remaining RBs are allocated to high priority UEs
-  for(r1=0; r1<2; r1++) {
-
-    for(i=UE_list->head; i>=0; i=UE_list->next[i]) {
-      for (ii=0; ii<UE_num_active_CC(UE_list,i); ii++) {
-        CC_id = UE_list->ordered_CCids[ii][i];
-
-        if(r1 == 0) {
-          nb_rbs_required_remaining[CC_id][i] = nb_rbs_required_remaining_1[CC_id][i];
-        } else { // rb required based only on the buffer - rb allloctaed in the 1st round + extra reaming rb form the 1st round
-          nb_rbs_required_remaining[CC_id][i] = nb_rbs_required[CC_id][i]-nb_rbs_required_remaining_1[CC_id][i]+nb_rbs_required_remaining[CC_id][i];
-if (nb_rbs_required_remaining[CC_id][i]<0) abort();
-        }
-
-        if (nb_rbs_required[CC_id][i]> 0 )
-          LOG_D(MAC,"round %d : nb_rbs_required_remaining[%d][%d]= %d (remaining_1 %d, required %d,  pre_nb_available_rbs %d, N_RBG %d, rb_unit %d)\n",
-                r1, CC_id, i,
-                nb_rbs_required_remaining[CC_id][i],
-                nb_rbs_required_remaining_1[CC_id][i],
-                nb_rbs_required[CC_id][i],
-                UE_list->UE_sched_ctrl[i].pre_nb_available_rbs[CC_id],
-                N_RBG[CC_id],
-                min_rb_unit[CC_id]);
-
-      }
-    }
+	    }
+	}
 
-    if (total_ue_count > 0 ) {
-      for(i=UE_list->head; i>=0; i=UE_list->next[i]) {
-        UE_id = i;
+	if (total_ue_count > 0) {
+	    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
+		UE_id = i;
+
+		for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
+		    CC_id = UE_list->ordered_CCids[ii][UE_id];
+		    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+		    round = ue_sched_ctl->round[CC_id][harq_pid];
+
+		    rnti = UE_RNTI(Mod_id, UE_id);
+
+		    // LOG_D(MAC,"UE %d rnti 0x\n", UE_id, rnti );
+		    if (rnti == NOT_A_RNTI)
+			continue;
+		    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1)
+			continue;
+
+		    transmission_mode = get_tmode(Mod_id, CC_id, UE_id);
+		    //          mac_xface->get_ue_active_harq_pid(Mod_id,CC_id,rnti,frameP,subframeP,&harq_pid,&round,0);
+		    //rrc_status = mac_eNB_get_rrc_status(Mod_id,rnti);
+		    /* 1st allocate for the retx */
+
+		    // retransmission in data channels
+		    // control channel in the 1st transmission
+		    // data channel for all TM
+		    LOG_T(MAC,
+			  "calling dlsch_scheduler_pre_processor_allocate .. \n ");
+		    dlsch_scheduler_pre_processor_allocate(Mod_id, UE_id,
+							   CC_id,
+							   N_RBG[CC_id],
+							   transmission_mode,
+							   min_rb_unit
+							   [CC_id],
+							   to_prb(RC.mac
+								  [Mod_id]->common_channels
+								  [CC_id].mib->message.dl_Bandwidth),
+							   nb_rbs_required,
+							   nb_rbs_required_remaining,
+							   rballoc_sub,
+							   MIMO_mode_indicator);
 
-        for (ii=0; ii<UE_num_active_CC(UE_list,UE_id); ii++) {
-          CC_id = UE_list->ordered_CCids[ii][UE_id];
-	  ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-	  round    = ue_sched_ctl->round[CC_id][harq_pid];
+#ifdef TM5
 
-          rnti = UE_RNTI(Mod_id,UE_id);
+		    // data chanel TM5: to be revisted
+		    if ((round == 0) &&
+			(transmission_mode == 5) &&
+			(ue_sched_ctl->dl_pow_off[CC_id] != 1)) {
+
+			for (j = 0; j < N_RBG[CC_id]; j += 2) {
+
+			    if ((((j == (N_RBG[CC_id] - 1))
+				  && (rballoc_sub[CC_id][j] == 0)
+				  && (ue_sched_ctl->
+				      rballoc_sub_UE[CC_id][j] == 0))
+				 || ((j < (N_RBG[CC_id] - 1))
+				     && (rballoc_sub[CC_id][j + 1] == 0)
+				     &&
+				     (ue_sched_ctl->rballoc_sub_UE
+				      [CC_id][j + 1] == 0)))
+				&& (nb_rbs_required_remaining[CC_id][UE_id]
+				    > 0)) {
+
+				for (ii = UE_list->next[i + 1]; ii >= 0;
+				     ii = UE_list->next[ii]) {
+
+				    UE_id2 = ii;
+				    rnti2 = UE_RNTI(Mod_id, UE_id2);
+				    ue_sched_ctl2 =
+					&UE_list->UE_sched_ctrl[UE_id2];
+				    round2 = ue_sched_ctl2->round[CC_id];
+				    if (rnti2 == NOT_A_RNTI)
+					continue;
+				    if (UE_list->
+					UE_sched_ctrl
+					[UE_id2].ul_out_of_sync == 1)
+					continue;
+
+				    eNB_UE_stats2 =
+					UE_list->
+					eNB_UE_stats[CC_id][UE_id2];
+				    //mac_xface->get_ue_active_harq_pid(Mod_id,CC_id,rnti2,frameP,subframeP,&harq_pid2,&round2,0);
+
+				    if ((mac_eNB_get_rrc_status
+					 (Mod_id,
+					  rnti2) >= RRC_RECONFIGURED)
+					&& (round2 == 0)
+					&&
+					(get_tmode(Mod_id, CC_id, UE_id2)
+					 == 5)
+					&& (ue_sched_ctl->
+					    dl_pow_off[CC_id] != 1)) {
+
+					if ((((j == (N_RBG[CC_id] - 1))
+					      &&
+					      (ue_sched_ctl->rballoc_sub_UE
+					       [CC_id][j] == 0))
+					     || ((j < (N_RBG[CC_id] - 1))
+						 &&
+						 (ue_sched_ctl->
+						  rballoc_sub_UE[CC_id][j +
+									1]
+						  == 0)))
+					    &&
+					    (nb_rbs_required_remaining
+					     [CC_id]
+					     [UE_id2] > 0)) {
+
+					    if ((((eNB_UE_stats2->
+						   DL_pmi_single ^
+						   eNB_UE_stats1->
+						   DL_pmi_single)
+						  << (14 - j)) & 0xc000) == 0x4000) {	//MU-MIMO only for 25 RBs configuration
+
+						rballoc_sub[CC_id][j] = 1;
+						ue_sched_ctl->
+						    rballoc_sub_UE[CC_id]
+						    [j] = 1;
+						ue_sched_ctl2->
+						    rballoc_sub_UE[CC_id]
+						    [j] = 1;
+						MIMO_mode_indicator[CC_id]
+						    [j] = 0;
+
+						if (j < N_RBG[CC_id] - 1) {
+						    rballoc_sub[CC_id][j +
+								       1] =
+							1;
+						    ue_sched_ctl->
+							rballoc_sub_UE
+							[CC_id][j + 1] = 1;
+						    ue_sched_ctl2->rballoc_sub_UE
+							[CC_id][j + 1] = 1;
+						    MIMO_mode_indicator
+							[CC_id][j + 1]
+							= 0;
+						}
+
+						ue_sched_ctl->
+						    dl_pow_off[CC_id]
+						    = 0;
+						ue_sched_ctl2->
+						    dl_pow_off[CC_id]
+						    = 0;
+
+
+						if ((j == N_RBG[CC_id] - 1)
+						    && ((N_RB_DL == 25)
+							|| (N_RB_DL ==
+							    50))) {
+
+						    nb_rbs_required_remaining
+							[CC_id][UE_id] =
+							nb_rbs_required_remaining
+							[CC_id][UE_id] -
+							min_rb_unit[CC_id]
+							+ 1;
+						    ue_sched_ctl->pre_nb_available_rbs
+							[CC_id] =
+							ue_sched_ctl->pre_nb_available_rbs
+							[CC_id] +
+							min_rb_unit[CC_id]
+							- 1;
+						    nb_rbs_required_remaining
+							[CC_id][UE_id2] =
+							nb_rbs_required_remaining
+							[CC_id][UE_id2] -
+							min_rb_unit[CC_id]
+							+ 1;
+						    ue_sched_ctl2->pre_nb_available_rbs
+							[CC_id] =
+							ue_sched_ctl2->pre_nb_available_rbs
+							[CC_id] +
+							min_rb_unit[CC_id]
+							- 1;
+						} else {
+
+						    nb_rbs_required_remaining
+							[CC_id][UE_id] =
+							nb_rbs_required_remaining
+							[CC_id][UE_id] - 4;
+						    ue_sched_ctl->pre_nb_available_rbs
+							[CC_id] =
+							ue_sched_ctl->pre_nb_available_rbs
+							[CC_id] + 4;
+						    nb_rbs_required_remaining
+							[CC_id][UE_id2] =
+							nb_rbs_required_remaining
+							[CC_id][UE_id2] -
+							4;
+						    ue_sched_ctl2->pre_nb_available_rbs
+							[CC_id] =
+							ue_sched_ctl2->pre_nb_available_rbs
+							[CC_id] + 4;
+						}
+
+						break;
+					    }
+					}
+				    }
+				}
+			    }
+			}
+		    }
+#endif
+		}
+	    }
+	}			// total_ue_count
+    }				// end of for for r1 and r2
 
-          // LOG_D(MAC,"UE %d rnti 0x\n", UE_id, rnti );
-          if(rnti == NOT_A_RNTI)
-            continue;
-	  if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1)
-	    continue;
+#ifdef TM5
 
-          transmission_mode = get_tmode(Mod_id,CC_id,UE_id);
-	  //          mac_xface->get_ue_active_harq_pid(Mod_id,CC_id,rnti,frameP,subframeP,&harq_pid,&round,0);
-          //rrc_status = mac_eNB_get_rrc_status(Mod_id,rnti);
-          /* 1st allocate for the retx */
-
-          // retransmission in data channels
-          // control channel in the 1st transmission
-          // data channel for all TM
-          LOG_T(MAC,"calling dlsch_scheduler_pre_processor_allocate .. \n ");
-          dlsch_scheduler_pre_processor_allocate (Mod_id,
-                                                  UE_id,
-                                                  CC_id,
-                                                  N_RBG[CC_id],
-                                                  transmission_mode,
-                                                  min_rb_unit[CC_id],
-                                                  to_prb(RC.mac[Mod_id]->common_channels[CC_id].mib->message.dl_Bandwidth),
-                                                  nb_rbs_required,
-                                                  nb_rbs_required_remaining,
-                                                  rballoc_sub,
-                                                  MIMO_mode_indicator);
+    // This has to be revisited!!!!
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+	i1 = 0;
+	i2 = 0;
+	i3 = 0;
+
+	for (j = 0; j < N_RBG[CC_id]; j++) {
+	    if (MIMO_mode_indicator[CC_id][j] == 2) {
+		i1 = i1 + 1;
+	    } else if (MIMO_mode_indicator[CC_id][j] == 1) {
+		i2 = i2 + 1;
+	    } else if (MIMO_mode_indicator[CC_id][j] == 0) {
+		i3 = i3 + 1;
+	    }
+	}
 
-#ifdef TM5
+	if ((i1 < N_RBG[CC_id]) && (i2 > 0) && (i3 == 0)) {
+	    PHY_vars_eNB_g[Mod_id][CC_id]->check_for_SUMIMO_transmissions =
+		PHY_vars_eNB_g[Mod_id][CC_id]->
+		check_for_SUMIMO_transmissions + 1;
+	}
 
-          // data chanel TM5: to be revisted
-          if ((round == 0 )  &&
-              (transmission_mode == 5)  &&
-              (ue_sched_ctl->dl_pow_off[CC_id] != 1)) {
-
-            for(j=0; j<N_RBG[CC_id]; j+=2) {
-
-              if( (((j == (N_RBG[CC_id]-1))&& (rballoc_sub[CC_id][j] == 0) && (ue_sched_ctl->rballoc_sub_UE[CC_id][j] == 0))  ||
-                   ((j < (N_RBG[CC_id]-1)) && (rballoc_sub[CC_id][j+1] == 0) && (ue_sched_ctl->rballoc_sub_UE[CC_id][j+1] == 0)) ) &&
-                  (nb_rbs_required_remaining[CC_id][UE_id]>0)) {
-
-                for (ii = UE_list->next[i+1]; ii >=0; ii=UE_list->next[ii]) {
-
-                  UE_id2 = ii;
-                  rnti2 = UE_RNTI(Mod_id,UE_id2);
-		  ue_sched_ctl2 = &UE_list->UE_sched_ctrl[UE_id2];
-		  round2    = ue_sched_ctl2->round[CC_id];
-                  if(rnti2 == NOT_A_RNTI)
-                    continue;
-		  if (UE_list->UE_sched_ctrl[UE_id2].ul_out_of_sync == 1)
-		    continue;
-
-                  eNB_UE_stats2 = UE_list->eNB_UE_stats[CC_id][UE_id2];
-                  //mac_xface->get_ue_active_harq_pid(Mod_id,CC_id,rnti2,frameP,subframeP,&harq_pid2,&round2,0);
-
-                  if ((mac_eNB_get_rrc_status(Mod_id,rnti2) >= RRC_RECONFIGURED) &&
-                      (round2==0) &&
-                      (get_tmode(Mod_id,CC_id,UE_id2)==5) &&
-                      (ue_sched_ctl->dl_pow_off[CC_id] != 1)) {
-
-                    if( (((j == (N_RBG[CC_id]-1)) && (ue_sched_ctl->rballoc_sub_UE[CC_id][j] == 0)) ||
-                         ((j < (N_RBG[CC_id]-1)) && (ue_sched_ctl->rballoc_sub_UE[CC_id][j+1] == 0))  ) &&
-                        (nb_rbs_required_remaining[CC_id][UE_id2]>0)) {
-
-                      if((((eNB_UE_stats2->DL_pmi_single^eNB_UE_stats1->DL_pmi_single)<<(14-j))&0xc000)== 0x4000) { //MU-MIMO only for 25 RBs configuration
-
-                        rballoc_sub[CC_id][j] = 1;
-                        ue_sched_ctl->rballoc_sub_UE[CC_id][j] = 1;
-                        ue_sched_ctl2->rballoc_sub_UE[CC_id][j] = 1;
-                        MIMO_mode_indicator[CC_id][j] = 0;
-
-                        if (j< N_RBG[CC_id]-1) {
-                          rballoc_sub[CC_id][j+1] = 1;
-                          ue_sched_ctl->rballoc_sub_UE[CC_id][j+1] = 1;
-                          ue_sched_ctl2->rballoc_sub_UE[CC_id][j+1] = 1;
-                          MIMO_mode_indicator[CC_id][j+1] = 0;
-                        }
-
-                        ue_sched_ctl->dl_pow_off[CC_id] = 0;
-                        ue_sched_ctl2->dl_pow_off[CC_id] = 0;
-
-
-                        if ((j == N_RBG[CC_id]-1) &&
-                            ((N_RB_DL == 25) ||
-                             (N_RB_DL == 50))) {
-			  
-                          nb_rbs_required_remaining[CC_id][UE_id] = nb_rbs_required_remaining[CC_id][UE_id] - min_rb_unit[CC_id]+1;
-                          ue_sched_ctl->pre_nb_available_rbs[CC_id] = ue_sched_ctl->pre_nb_available_rbs[CC_id] + min_rb_unit[CC_id]-1;
-                          nb_rbs_required_remaining[CC_id][UE_id2] = nb_rbs_required_remaining[CC_id][UE_id2] - min_rb_unit[CC_id]+1;
-                          ue_sched_ctl2->pre_nb_available_rbs[CC_id] = ue_sched_ctl2->pre_nb_available_rbs[CC_id] + min_rb_unit[CC_id]-1;
-                        } else {
-                          
-			  nb_rbs_required_remaining[CC_id][UE_id] = nb_rbs_required_remaining[CC_id][UE_id] - 4;
-                          ue_sched_ctl->pre_nb_available_rbs[CC_id] = ue_sched_ctl->pre_nb_available_rbs[CC_id] + 4;
-                          nb_rbs_required_remaining[CC_id][UE_id2] = nb_rbs_required_remaining[CC_id][UE_id2] - 4;
-                          ue_sched_ctl2->pre_nb_available_rbs[CC_id] = ue_sched_ctl2->pre_nb_available_rbs[CC_id] + 4;
-                        }
-
-                        break;
-                      }
-                    }
-                  }
-                }
-              }
-            }
-          }
+	if (i3 == N_RBG[CC_id] && i1 == 0 && i2 == 0) {
+	    PHY_vars_eNB_g[Mod_id][CC_id]->FULL_MUMIMO_transmissions =
+		PHY_vars_eNB_g[Mod_id][CC_id]->FULL_MUMIMO_transmissions +
+		1;
+	}
 
-#endif
-        }
-      }
-    } // total_ue_count
-  } // end of for for r1 and r2
+	if ((i1 < N_RBG[CC_id]) && (i3 > 0)) {
+	    PHY_vars_eNB_g[Mod_id][CC_id]->check_for_MUMIMO_transmissions =
+		PHY_vars_eNB_g[Mod_id][CC_id]->
+		check_for_MUMIMO_transmissions + 1;
+	}
 
-#ifdef TM5
+	PHY_vars_eNB_g[Mod_id][CC_id]->check_for_total_transmissions =
+	    PHY_vars_eNB_g[Mod_id][CC_id]->check_for_total_transmissions +
+	    1;
 
-  // This has to be revisited!!!!
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-    i1=0;
-    i2=0;
-    i3=0;
-
-    for (j=0; j<N_RBG[CC_id]; j++) {
-      if(MIMO_mode_indicator[CC_id][j] == 2) {
-        i1 = i1+1;
-      } else if(MIMO_mode_indicator[CC_id][j] == 1) {
-        i2 = i2+1;
-      } else if(MIMO_mode_indicator[CC_id][j] == 0) {
-        i3 = i3+1;
-      }
     }
 
-    if((i1 < N_RBG[CC_id]) && (i2>0) && (i3==0)) {
-      PHY_vars_eNB_g[Mod_id][CC_id]->check_for_SUMIMO_transmissions = PHY_vars_eNB_g[Mod_id][CC_id]->check_for_SUMIMO_transmissions + 1;
-    }
+#endif
 
-    if(i3 == N_RBG[CC_id] && i1==0 && i2==0) {
-      PHY_vars_eNB_g[Mod_id][CC_id]->FULL_MUMIMO_transmissions = PHY_vars_eNB_g[Mod_id][CC_id]->FULL_MUMIMO_transmissions + 1;
+    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
+	UE_id = i;
+	ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+
+	for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
+	    CC_id = UE_list->ordered_CCids[ii][UE_id];
+	    //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].dl_pow_off = dl_pow_off[UE_id];
+
+	    if (ue_sched_ctl->pre_nb_available_rbs[CC_id] > 0) {
+		LOG_D(MAC,
+		      "******************DL Scheduling Information for UE%d ************************\n",
+		      UE_id);
+		LOG_D(MAC, "dl power offset UE%d = %d \n", UE_id,
+		      ue_sched_ctl->dl_pow_off[CC_id]);
+		LOG_D(MAC,
+		      "***********RB Alloc for every subband for UE%d ***********\n",
+		      UE_id);
+
+		for (j = 0; j < N_RBG[CC_id]; j++) {
+		    //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].rballoc_sub[i] = rballoc_sub_UE[CC_id][UE_id][i];
+		    LOG_D(MAC, "RB Alloc for UE%d and Subband%d = %d\n",
+			  UE_id, j,
+			  ue_sched_ctl->rballoc_sub_UE[CC_id][j]);
+		}
+
+		//PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].pre_nb_available_rbs = pre_nb_available_rbs[CC_id][UE_id];
+		LOG_D(MAC, "Total RBs allocated for UE%d = %d\n", UE_id,
+		      ue_sched_ctl->pre_nb_available_rbs[CC_id]);
+	    }
+	}
     }
+}
 
-    if((i1 < N_RBG[CC_id]) && (i3 > 0)) {
-      PHY_vars_eNB_g[Mod_id][CC_id]->check_for_MUMIMO_transmissions = PHY_vars_eNB_g[Mod_id][CC_id]->check_for_MUMIMO_transmissions + 1;
-    }
+#define SF0_LIMIT 1
 
-    PHY_vars_eNB_g[Mod_id][CC_id]->check_for_total_transmissions = PHY_vars_eNB_g[Mod_id][CC_id]->check_for_total_transmissions + 1;
+void
+dlsch_scheduler_pre_processor_reset(int module_idP,
+				    int UE_id,
+				    uint8_t CC_id,
+				    int frameP,
+				    int subframeP,
+				    int N_RBG,
+				    uint16_t nb_rbs_required[MAX_NUM_CCs]
+				    [NUMBER_OF_UE_MAX],
+				    uint16_t
+				    nb_rbs_required_remaining
+				    [MAX_NUM_CCs][NUMBER_OF_UE_MAX],
+				    unsigned char
+				    rballoc_sub[MAX_NUM_CCs]
+				    [N_RBG_MAX], unsigned char
+				    MIMO_mode_indicator[MAX_NUM_CCs]
+				    [N_RBG_MAX])
+{
+    int i, j;
+    UE_list_t *UE_list = &RC.mac[module_idP]->UE_list;
+    UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+    rnti_t rnti = UE_RNTI(module_idP, UE_id);
+
+    uint8_t *vrb_map = RC.mac[module_idP]->common_channels[CC_id].vrb_map;
+    int N_RB_DL =
+	to_prb(RC.mac[module_idP]->common_channels[CC_id].mib->
+	       message.dl_Bandwidth);
+    int RBGsize = N_RB_DL / N_RBG, RBGsize_last;
+#ifdef SF0_LIMIT
+    int sf0_upper = -1, sf0_lower = -1;
+#endif
 
-  }
 
-#endif
+    LOG_D(MAC, "Running preprocessor for UE %d (%x)\n", UE_id, rnti);
+    // initialize harq_pid and round
 
-  for(i=UE_list->head; i>=0; i=UE_list->next[i]) {
-    UE_id = i;
-    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+    if (ue_sched_ctl->ta_timer)
+	ue_sched_ctl->ta_timer--;
 
-    for (ii=0; ii<UE_num_active_CC(UE_list,UE_id); ii++) {
-      CC_id = UE_list->ordered_CCids[ii][UE_id];
-      //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].dl_pow_off = dl_pow_off[UE_id];
+    /*
+       eNB_UE_stats *eNB_UE_stats;
 
-      if (ue_sched_ctl->pre_nb_available_rbs[CC_id] > 0 ) {
-        LOG_D(MAC,"******************DL Scheduling Information for UE%d ************************\n",UE_id);
-        LOG_D(MAC,"dl power offset UE%d = %d \n",UE_id,ue_sched_ctl->dl_pow_off[CC_id]);
-        LOG_D(MAC,"***********RB Alloc for every subband for UE%d ***********\n",UE_id);
+       if (eNB_UE_stats == NULL)
+       return;
 
-        for(j=0; j<N_RBG[CC_id]; j++) {
-          //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].rballoc_sub[i] = rballoc_sub_UE[CC_id][UE_id][i];
-          LOG_D(MAC,"RB Alloc for UE%d and Subband%d = %d\n",UE_id,j,ue_sched_ctl->rballoc_sub_UE[CC_id][j]);
-        }
 
-        //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].pre_nb_available_rbs = pre_nb_available_rbs[CC_id][UE_id];
-        LOG_D(MAC,"Total RBs allocated for UE%d = %d\n",UE_id,ue_sched_ctl->pre_nb_available_rbs[CC_id]);
-      }
-    }
-  }
-}
+       mac_xface->get_ue_active_harq_pid(module_idP,CC_id,rnti,
+       frameP,subframeP,
+       &ue_sched_ctl->harq_pid[CC_id],
+       &ue_sched_ctl->round[CC_id],
+       openair_harq_DL);
 
-#define SF0_LIMIT 1
 
-void dlsch_scheduler_pre_processor_reset (int module_idP,
-					  int UE_id,
-					  uint8_t  CC_id,
-					  int frameP,
-					  int subframeP,					  
-					  int N_RBG,
-					  uint16_t nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-					  uint16_t nb_rbs_required_remaining[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-					  unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX],
-					  unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX])
-  
-{
-  int i,j;
-  UE_list_t *UE_list=&RC.mac[module_idP]->UE_list;
-  UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-  rnti_t rnti = UE_RNTI(module_idP,UE_id);
-
-  uint8_t *vrb_map = RC.mac[module_idP]->common_channels[CC_id].vrb_map;
-  int N_RB_DL = to_prb(RC.mac[module_idP]->common_channels[CC_id].mib->message.dl_Bandwidth);
-  int RBGsize = N_RB_DL/N_RBG,RBGsize_last;
-#ifdef SF0_LIMIT
-  int sf0_upper=-1,sf0_lower=-1;
-#endif
+       if (ue_sched_ctl->ta_timer == 0) {
 
+       // WE SHOULD PROTECT the eNB_UE_stats with a mutex here ...
 
-  LOG_D(MAC,"Running preprocessor for UE %d (%x)\n",UE_id,rnti);
-  // initialize harq_pid and round
+       ue_sched_ctl->ta_timer = 20;  // wait 20 subframes before taking TA measurement from PHY
+       switch (N_RB_DL) {
+       case 6:
+       ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update;
+       break;
 
-  if (ue_sched_ctl->ta_timer) ue_sched_ctl->ta_timer--;
+       case 15:
+       ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/2;
+       break;
 
-  /*
-  eNB_UE_stats *eNB_UE_stats;
+       case 25:
+       ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/4;
+       break;
 
-  if (eNB_UE_stats == NULL)
-    return;
+       case 50:
+       ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/8;
+       break;
 
-  
-  mac_xface->get_ue_active_harq_pid(module_idP,CC_id,rnti,
-				    frameP,subframeP,
-				    &ue_sched_ctl->harq_pid[CC_id],
-				    &ue_sched_ctl->round[CC_id],
-				    openair_harq_DL);
-  
+       case 75:
+       ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/12;
+       break;
 
-  if (ue_sched_ctl->ta_timer == 0) {
+       case 100:
+       ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/16;
+       break;
+       }
+       // clear the update in case PHY does not have a new measurement after timer expiry
+       eNB_UE_stats->timing_advance_update =  0;
+       }
+       else {
+       ue_sched_ctl->ta_timer--;
+       ue_sched_ctl->ta_update =0; // don't trigger a timing advance command
+       }
+
+
+       if (UE_id==0) {
+       VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_TIMING_ADVANCE,ue_sched_ctl->ta_update);
+       }
+     */
 
-    // WE SHOULD PROTECT the eNB_UE_stats with a mutex here ...
+    nb_rbs_required[CC_id][UE_id] = 0;
+    ue_sched_ctl->pre_nb_available_rbs[CC_id] = 0;
+    ue_sched_ctl->dl_pow_off[CC_id] = 2;
+    nb_rbs_required_remaining[CC_id][UE_id] = 0;
 
-    ue_sched_ctl->ta_timer = 20;  // wait 20 subframes before taking TA measurement from PHY
     switch (N_RB_DL) {
     case 6:
-      ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update;
-      break;
-      
+	RBGsize = 1;
+	RBGsize_last = 1;
+	break;
     case 15:
-      ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/2;
-      break;
-      
+	RBGsize = 2;
+	RBGsize_last = 1;
+	break;
     case 25:
-      ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/4;
-      break;
-      
+	RBGsize = 2;
+	RBGsize_last = 1;
+	break;
     case 50:
-      ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/8;
-      break;
-      
+	RBGsize = 3;
+	RBGsize_last = 2;
+	break;
     case 75:
-      ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/12;
-      break;
-      
+	RBGsize = 4;
+	RBGsize_last = 3;
+	break;
     case 100:
-	ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/16;
-      break;
+	RBGsize = 4;
+	RBGsize_last = 4;
+	break;
+    default:
+	AssertFatal(1 == 0, "unsupported RBs (%d)\n", N_RB_DL);
     }
-    // clear the update in case PHY does not have a new measurement after timer expiry
-    eNB_UE_stats->timing_advance_update =  0;
-  }
-  else {
-    ue_sched_ctl->ta_timer--;
-    ue_sched_ctl->ta_update =0; // don't trigger a timing advance command
-  }
-  
 
-  if (UE_id==0) {
-    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_TIMING_ADVANCE,ue_sched_ctl->ta_update);
-  }
-  */
-
-  nb_rbs_required[CC_id][UE_id]=0;
-  ue_sched_ctl->pre_nb_available_rbs[CC_id] = 0;
-  ue_sched_ctl->dl_pow_off[CC_id] = 2;
-  nb_rbs_required_remaining[CC_id][UE_id] = 0;
-  
-  switch (N_RB_DL) {
-  case 6:   RBGsize = 1; RBGsize_last = 1; break;
-  case 15:  RBGsize = 2; RBGsize_last = 1; break;
-  case 25:  RBGsize = 2; RBGsize_last = 1; break;
-  case 50:  RBGsize = 3; RBGsize_last = 2; break;
-  case 75:  RBGsize = 4; RBGsize_last = 3; break;
-  case 100: RBGsize = 4; RBGsize_last = 4; break;
-  default: AssertFatal(1==0,"unsupported RBs (%d)\n", N_RB_DL);
-  }
-  
 #ifdef SF0_LIMIT
-  switch (N_RBG) {
-  case 6:
-    sf0_lower=0;
-    sf0_upper=5;
-    break;
-  case 8:
-    sf0_lower=2;
-    sf0_upper=5;
-    break;
-  case 13:
-    sf0_lower=4;
-    sf0_upper=7;
-    break;
-  case 17:
-    sf0_lower=7;
-    sf0_upper=9;
-    break;
-  case 25:
-    sf0_lower=11;
-    sf0_upper=13;
-    break;
-  default: AssertFatal(1==0,"unsupported RBs (%d)\n", N_RB_DL);
-  }
+    switch (N_RBG) {
+    case 6:
+	sf0_lower = 0;
+	sf0_upper = 5;
+	break;
+    case 8:
+	sf0_lower = 2;
+	sf0_upper = 5;
+	break;
+    case 13:
+	sf0_lower = 4;
+	sf0_upper = 7;
+	break;
+    case 17:
+	sf0_lower = 7;
+	sf0_upper = 9;
+	break;
+    case 25:
+	sf0_lower = 11;
+	sf0_upper = 13;
+	break;
+    default:
+	AssertFatal(1 == 0, "unsupported RBs (%d)\n", N_RB_DL);
+    }
 #endif
-  // Initialize Subbands according to VRB map
-  for (i=0; i<N_RBG; i++) {
-    int rb_size = i==N_RBG-1 ? RBGsize_last : RBGsize;
+    // Initialize Subbands according to VRB map
+    for (i = 0; i < N_RBG; i++) {
+	int rb_size = i == N_RBG - 1 ? RBGsize_last : RBGsize;
 
-    ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 0;
-    rballoc_sub[CC_id][i] = 0;
+	ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 0;
+	rballoc_sub[CC_id][i] = 0;
 #ifdef SF0_LIMIT
-    // for avoiding 6+ PRBs around DC in subframe 0 (avoid excessive errors)
-    /* TODO: make it proper - allocate those RBs, do not "protect" them, but
-     * compute number of available REs and limit MCS according to the
-     * TBS table 36.213 7.1.7.2.1-1 (can be done after pre-processor)
-     */
-    if (subframeP==0 &&
-	i >= sf0_lower && i <= sf0_upper)
-      rballoc_sub[CC_id][i]=1;
+	// for avoiding 6+ PRBs around DC in subframe 0 (avoid excessive errors)
+	/* TODO: make it proper - allocate those RBs, do not "protect" them, but
+	 * compute number of available REs and limit MCS according to the
+	 * TBS table 36.213 7.1.7.2.1-1 (can be done after pre-processor)
+	 */
+	if (subframeP == 0 && i >= sf0_lower && i <= sf0_upper)
+	    rballoc_sub[CC_id][i] = 1;
 #endif
-    // for SI-RNTI,RA-RNTI and P-RNTI allocations
-    for (j = 0; j < rb_size; j++) {
-      if (vrb_map[j+(i*RBGsize)] != 0)  {
-	rballoc_sub[CC_id][i] = 1;
-	LOG_D(MAC,"Frame %d, subframe %d : vrb %d allocated\n",frameP,subframeP,j+(i*RBGsize));
-	break;
-      }
+	// for SI-RNTI,RA-RNTI and P-RNTI allocations
+	for (j = 0; j < rb_size; j++) {
+	    if (vrb_map[j + (i * RBGsize)] != 0) {
+		rballoc_sub[CC_id][i] = 1;
+		LOG_D(MAC, "Frame %d, subframe %d : vrb %d allocated\n",
+		      frameP, subframeP, j + (i * RBGsize));
+		break;
+	    }
+	}
+	LOG_D(MAC, "Frame %d Subframe %d CC_id %d RBG %i : rb_alloc %d\n",
+	      frameP, subframeP, CC_id, i, rballoc_sub[CC_id][i]);
+	MIMO_mode_indicator[CC_id][i] = 2;
     }
-    LOG_D(MAC,"Frame %d Subframe %d CC_id %d RBG %i : rb_alloc %d\n",frameP,subframeP,CC_id,i,rballoc_sub[CC_id][i]);
-    MIMO_mode_indicator[CC_id][i] = 2;
-  }
 }
 
 
-void dlsch_scheduler_pre_processor_allocate (module_id_t   Mod_id,
-    int           UE_id,
-    uint8_t       CC_id,
-    int           N_RBG,
-    int           transmission_mode,
-    int           min_rb_unit,
-    uint8_t       N_RB_DL,
-    uint16_t      nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-    uint16_t      nb_rbs_required_remaining[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-    unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX],
-    unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX])
+void
+dlsch_scheduler_pre_processor_allocate(module_id_t Mod_id,
+				       int UE_id,
+				       uint8_t CC_id,
+				       int N_RBG,
+				       int transmission_mode,
+				       int min_rb_unit,
+				       uint8_t N_RB_DL,
+				       uint16_t
+				       nb_rbs_required[MAX_NUM_CCs]
+				       [NUMBER_OF_UE_MAX],
+				       uint16_t
+				       nb_rbs_required_remaining
+				       [MAX_NUM_CCs]
+				       [NUMBER_OF_UE_MAX], unsigned char
+				       rballoc_sub[MAX_NUM_CCs]
+				       [N_RBG_MAX], unsigned char
+				       MIMO_mode_indicator
+				       [MAX_NUM_CCs][N_RBG_MAX])
 {
 
-  int i;
-  UE_list_t *UE_list=&RC.mac[Mod_id]->UE_list;
-  UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
-
-  for(i=0; i<N_RBG; i++) {
-
-    if((rballoc_sub[CC_id][i] == 0)           &&
-        (ue_sched_ctl->rballoc_sub_UE[CC_id][i] == 0) &&
-        (nb_rbs_required_remaining[CC_id][UE_id]>0)   &&
-        (ue_sched_ctl->pre_nb_available_rbs[CC_id] < nb_rbs_required[CC_id][UE_id])) {
-
-      // if this UE is not scheduled for TM5
-      if (ue_sched_ctl->dl_pow_off[CC_id] != 0 )  {
-
-	if ((i == N_RBG-1) && ((N_RB_DL == 25) || (N_RB_DL == 50))) {
-	  if (nb_rbs_required_remaining[CC_id][UE_id] >=  min_rb_unit-1){
-            rballoc_sub[CC_id][i] = 1;
-            ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 1;
-            MIMO_mode_indicator[CC_id][i] = 1;
-            if (transmission_mode == 5 ) {
-              ue_sched_ctl->dl_pow_off[CC_id] = 1;
-            }
-            nb_rbs_required_remaining[CC_id][UE_id] = nb_rbs_required_remaining[CC_id][UE_id] - min_rb_unit+1;
-            ue_sched_ctl->pre_nb_available_rbs[CC_id] = ue_sched_ctl->pre_nb_available_rbs[CC_id] + min_rb_unit - 1;
-          }
-        } else {
-	  if (nb_rbs_required_remaining[CC_id][UE_id] >=  min_rb_unit){
-	    rballoc_sub[CC_id][i] = 1;
-	    ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 1;
-	    MIMO_mode_indicator[CC_id][i] = 1;
-	    if (transmission_mode == 5 ) {
-	      ue_sched_ctl->dl_pow_off[CC_id] = 1;
-	    }
-	    nb_rbs_required_remaining[CC_id][UE_id] = nb_rbs_required_remaining[CC_id][UE_id] - min_rb_unit;
-	    ue_sched_ctl->pre_nb_available_rbs[CC_id] = ue_sched_ctl->pre_nb_available_rbs[CC_id] + min_rb_unit;
-	  }
+    int i;
+    UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
+    UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
+
+    for (i = 0; i < N_RBG; i++) {
+
+	if ((rballoc_sub[CC_id][i] == 0) &&
+	    (ue_sched_ctl->rballoc_sub_UE[CC_id][i] == 0) &&
+	    (nb_rbs_required_remaining[CC_id][UE_id] > 0) &&
+	    (ue_sched_ctl->pre_nb_available_rbs[CC_id] <
+	     nb_rbs_required[CC_id][UE_id])) {
+
+	    // if this UE is not scheduled for TM5
+	    if (ue_sched_ctl->dl_pow_off[CC_id] != 0) {
+
+		if ((i == N_RBG - 1)
+		    && ((N_RB_DL == 25) || (N_RB_DL == 50))) {
+		    if (nb_rbs_required_remaining[CC_id][UE_id] >=
+			min_rb_unit - 1) {
+			rballoc_sub[CC_id][i] = 1;
+			ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 1;
+			MIMO_mode_indicator[CC_id][i] = 1;
+			if (transmission_mode == 5) {
+			    ue_sched_ctl->dl_pow_off[CC_id] = 1;
+			}
+			nb_rbs_required_remaining[CC_id][UE_id] =
+			    nb_rbs_required_remaining[CC_id][UE_id] -
+			    min_rb_unit + 1;
+			ue_sched_ctl->pre_nb_available_rbs[CC_id] =
+			    ue_sched_ctl->pre_nb_available_rbs[CC_id] +
+			    min_rb_unit - 1;
+		    }
+		} else {
+		    if (nb_rbs_required_remaining[CC_id][UE_id] >=
+			min_rb_unit) {
+			rballoc_sub[CC_id][i] = 1;
+			ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 1;
+			MIMO_mode_indicator[CC_id][i] = 1;
+			if (transmission_mode == 5) {
+			    ue_sched_ctl->dl_pow_off[CC_id] = 1;
+			}
+			nb_rbs_required_remaining[CC_id][UE_id] =
+			    nb_rbs_required_remaining[CC_id][UE_id] -
+			    min_rb_unit;
+			ue_sched_ctl->pre_nb_available_rbs[CC_id] =
+			    ue_sched_ctl->pre_nb_available_rbs[CC_id] +
+			    min_rb_unit;
+		    }
+		}
+	    }			// dl_pow_off[CC_id][UE_id] ! = 0
 	}
-      } // dl_pow_off[CC_id][UE_id] ! = 0
     }
-  }
 }
 
 
 /// ULSCH PRE_PROCESSOR
 
 
-void ulsch_scheduler_pre_processor(module_id_t module_idP,
-                                   int frameP,
-                                   sub_frame_t subframeP,
-                                   uint16_t *first_rb)
+void
+ulsch_scheduler_pre_processor(module_id_t module_idP,
+			      int frameP,
+			      sub_frame_t subframeP, uint16_t * first_rb)
 {
 
-  int16_t            i;
-  uint16_t           UE_id,n,r;
-  uint8_t            CC_id, harq_pid;
-  uint16_t           nb_allocated_rbs[MAX_NUM_CCs][NUMBER_OF_UE_MAX],total_allocated_rbs[MAX_NUM_CCs],average_rbs_per_user[MAX_NUM_CCs];
-  int16_t            total_remaining_rbs[MAX_NUM_CCs];
-  uint16_t           max_num_ue_to_be_scheduled = 0;
-  uint16_t           total_ue_count             = 0;
-  rnti_t             rnti                       = -1;
-  UE_list_t          *UE_list                   = &RC.mac[module_idP]->UE_list;
-  UE_TEMPLATE        *UE_template               = 0;
-  int                N_RB_DL;
-  int                N_RB_UL;
-  LOG_D(MAC,"In ulsch_preprocessor: assign max mcs min rb\n");
-  // maximize MCS and then allocate required RB according to the buffer occupancy with the limit of max available UL RB
-  assign_max_mcs_min_rb(module_idP,frameP, subframeP, first_rb);
-
-  LOG_D(MAC,"In ulsch_preprocessor: sort ue \n");
-  // sort ues
-  sort_ue_ul (module_idP,frameP, subframeP);
-
-
-  // we need to distribute RBs among UEs
-  // step1:  reset the vars
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
-    N_RB_DL                     = to_prb(RC.mac[module_idP]->common_channels[CC_id].mib->message.dl_Bandwidth);
-    N_RB_UL                     = to_prb(RC.mac[module_idP]->common_channels[CC_id].ul_Bandwidth);
-    total_allocated_rbs[CC_id]  = 0;
-    total_remaining_rbs[CC_id]  = 0;
-    average_rbs_per_user[CC_id] = 0;
-
-    for (i=UE_list->head_ul; i>=0; i=UE_list->next_ul[i]) {
-      nb_allocated_rbs[CC_id][i]=0;
+    int16_t i;
+    uint16_t UE_id, n, r;
+    uint8_t CC_id, harq_pid;
+    uint16_t nb_allocated_rbs[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
+	total_allocated_rbs[MAX_NUM_CCs],
+	average_rbs_per_user[MAX_NUM_CCs];
+    int16_t total_remaining_rbs[MAX_NUM_CCs];
+    uint16_t max_num_ue_to_be_scheduled = 0;
+    uint16_t total_ue_count = 0;
+    rnti_t rnti = -1;
+    UE_list_t *UE_list = &RC.mac[module_idP]->UE_list;
+    UE_TEMPLATE *UE_template = 0;
+    int N_RB_DL;
+    int N_RB_UL;
+    LOG_D(MAC, "In ulsch_preprocessor: assign max mcs min rb\n");
+    // maximize MCS and then allocate required RB according to the buffer occupancy with the limit of max available UL RB
+    assign_max_mcs_min_rb(module_idP, frameP, subframeP, first_rb);
+
+    LOG_D(MAC, "In ulsch_preprocessor: sort ue \n");
+    // sort ues
+    sort_ue_ul(module_idP, frameP, subframeP);
+
+
+    // we need to distribute RBs among UEs
+    // step1:  reset the vars
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
+	N_RB_DL =
+	    to_prb(RC.mac[module_idP]->common_channels[CC_id].mib->
+		   message.dl_Bandwidth);
+	N_RB_UL =
+	    to_prb(RC.mac[module_idP]->common_channels[CC_id].
+		   ul_Bandwidth);
+	total_allocated_rbs[CC_id] = 0;
+	total_remaining_rbs[CC_id] = 0;
+	average_rbs_per_user[CC_id] = 0;
+
+	for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
+	    nb_allocated_rbs[CC_id][i] = 0;
+	}
     }
-  }
 
-  LOG_D(MAC,"In ulsch_preprocessor: step2 \n");
-  // step 2: calculate the average rb per UE
-  total_ue_count =0;
-  max_num_ue_to_be_scheduled=0;
-
-  for (i=UE_list->head_ul; i>=0; i=UE_list->next_ul[i]) {
-
-    rnti = UE_RNTI(module_idP,i);
-
-    if (rnti==NOT_A_RNTI)
-      continue;
-
-    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-      continue;
-
-
-    UE_id = i;
-
-    LOG_D(MAC,"In ulsch_preprocessor: handling UE %d/%x\n",UE_id,rnti);
-    for (n=0; n<UE_list->numactiveULCCs[UE_id]; n++) {
-      // This is the actual CC_id in the list
-      CC_id = UE_list->ordered_ULCCids[n][UE_id];
-      LOG_D(MAC,"In ulsch_preprocessor: handling UE %d/%x CCid %d\n",UE_id,rnti,CC_id);
-      UE_template = &UE_list->UE_template[CC_id][UE_id];
-      average_rbs_per_user[CC_id]=0;
-
-      if (UE_template->pre_allocated_nb_rb_ul > 0) {
-        total_ue_count+=1;
-      }
-      /*
-      if((mac_xface->get_nCCE_max(module_idP,CC_id,3,subframeP) - nCCE_to_be_used[CC_id])  > (1<<aggregation)) {
-        nCCE_to_be_used[CC_id] = nCCE_to_be_used[CC_id] + (1<<aggregation);
-        max_num_ue_to_be_scheduled+=1;
-	}*/
-
-      max_num_ue_to_be_scheduled+=1;
-
-      if (total_ue_count == 0) {
-        average_rbs_per_user[CC_id] = 0;
-      } else if (total_ue_count == 1 ) { // increase the available RBs, special case,
-        average_rbs_per_user[CC_id] = N_RB_UL-first_rb[CC_id]+1;
-      } else if( (total_ue_count <= (N_RB_DL-first_rb[CC_id])) &&
-                 (total_ue_count <= max_num_ue_to_be_scheduled)) {
-        average_rbs_per_user[CC_id] = (uint16_t) floor((N_RB_UL-first_rb[CC_id])/total_ue_count);
-      } else if (max_num_ue_to_be_scheduled > 0 ) {
-        average_rbs_per_user[CC_id] = (uint16_t) floor((N_RB_UL-first_rb[CC_id])/max_num_ue_to_be_scheduled);
-      } else {
-        average_rbs_per_user[CC_id]=1;
-        LOG_W(MAC,"[eNB %d] frame %d subframe %d: UE %d CC %d: can't get average rb per user (should not be here)\n",
-              module_idP,frameP,subframeP,UE_id,CC_id);
-      }
+    LOG_D(MAC, "In ulsch_preprocessor: step2 \n");
+    // step 2: calculate the average rb per UE
+    total_ue_count = 0;
+    max_num_ue_to_be_scheduled = 0;
+
+    for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
+
+	rnti = UE_RNTI(module_idP, i);
+
+	if (rnti == NOT_A_RNTI)
+	    continue;
+
+	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+	    continue;
+
+
+	UE_id = i;
+
+	LOG_D(MAC, "In ulsch_preprocessor: handling UE %d/%x\n", UE_id,
+	      rnti);
+	for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
+	    // This is the actual CC_id in the list
+	    CC_id = UE_list->ordered_ULCCids[n][UE_id];
+	    LOG_D(MAC,
+		  "In ulsch_preprocessor: handling UE %d/%x CCid %d\n",
+		  UE_id, rnti, CC_id);
+	    UE_template = &UE_list->UE_template[CC_id][UE_id];
+	    average_rbs_per_user[CC_id] = 0;
+
+	    if (UE_template->pre_allocated_nb_rb_ul > 0) {
+		total_ue_count += 1;
+	    }
+	    /*
+	       if((mac_xface->get_nCCE_max(module_idP,CC_id,3,subframeP) - nCCE_to_be_used[CC_id])  > (1<<aggregation)) {
+	       nCCE_to_be_used[CC_id] = nCCE_to_be_used[CC_id] + (1<<aggregation);
+	       max_num_ue_to_be_scheduled+=1;
+	       } */
+
+	    max_num_ue_to_be_scheduled += 1;
+
+	    if (total_ue_count == 0) {
+		average_rbs_per_user[CC_id] = 0;
+	    } else if (total_ue_count == 1) {	// increase the available RBs, special case,
+		average_rbs_per_user[CC_id] =
+		    N_RB_UL - first_rb[CC_id] + 1;
+	    } else if ((total_ue_count <= (N_RB_DL - first_rb[CC_id]))
+		       && (total_ue_count <= max_num_ue_to_be_scheduled)) {
+		average_rbs_per_user[CC_id] =
+		    (uint16_t) floor((N_RB_UL - first_rb[CC_id]) /
+				     total_ue_count);
+	    } else if (max_num_ue_to_be_scheduled > 0) {
+		average_rbs_per_user[CC_id] =
+		    (uint16_t) floor((N_RB_UL - first_rb[CC_id]) /
+				     max_num_ue_to_be_scheduled);
+	    } else {
+		average_rbs_per_user[CC_id] = 1;
+		LOG_W(MAC,
+		      "[eNB %d] frame %d subframe %d: UE %d CC %d: can't get average rb per user (should not be here)\n",
+		      module_idP, frameP, subframeP, UE_id, CC_id);
+	    }
+	}
     }
-  }
-  if (total_ue_count > 0)
-    LOG_D(MAC,"[eNB %d] Frame %d subframe %d: total ue to be scheduled %d/%d\n",
-	  module_idP, frameP, subframeP,total_ue_count, max_num_ue_to_be_scheduled);
+    if (total_ue_count > 0)
+	LOG_D(MAC,
+	      "[eNB %d] Frame %d subframe %d: total ue to be scheduled %d/%d\n",
+	      module_idP, frameP, subframeP, total_ue_count,
+	      max_num_ue_to_be_scheduled);
 
-  //LOG_D(MAC,"step3\n");
+    //LOG_D(MAC,"step3\n");
 
-  // step 3: assigne RBS
-  for (i=UE_list->head_ul; i>=0; i=UE_list->next_ul[i]) {
-    rnti = UE_RNTI(module_idP,i);
+    // step 3: assigne RBS
+    for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
+	rnti = UE_RNTI(module_idP, i);
 
-    if (rnti==NOT_A_RNTI)
-      continue;
-    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-      continue;
+	if (rnti == NOT_A_RNTI)
+	    continue;
+	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+	    continue;
 
-    UE_id = i;
+	UE_id = i;
 
-    for (n=0; n<UE_list->numactiveULCCs[UE_id]; n++) {
-      // This is the actual CC_id in the list
-      CC_id = UE_list->ordered_ULCCids[n][UE_id];
-      harq_pid = subframe2harqpid(&RC.mac[module_idP]->common_channels[CC_id],frameP,subframeP);
+	for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
+	    // This is the actual CC_id in the list
+	    CC_id = UE_list->ordered_ULCCids[n][UE_id];
+	    harq_pid =
+		subframe2harqpid(&RC.mac[module_idP]->
+				 common_channels[CC_id], frameP,
+				 subframeP);
 
 
-      //      mac_xface->get_ue_active_harq_pid(module_idP,CC_id,rnti,frameP,subframeP,&harq_pid,&round,openair_harq_UL);
+	    //      mac_xface->get_ue_active_harq_pid(module_idP,CC_id,rnti,frameP,subframeP,&harq_pid,&round,openair_harq_UL);
 
-      if(UE_list->UE_sched_ctrl[UE_id].round_UL[CC_id]>0) {
-        nb_allocated_rbs[CC_id][UE_id] = UE_list->UE_template[CC_id][UE_id].nb_rb_ul[harq_pid];
-      } else {
-        nb_allocated_rbs[CC_id][UE_id] = cmin(UE_list->UE_template[CC_id][UE_id].pre_allocated_nb_rb_ul, average_rbs_per_user[CC_id]);
-      }
+	    if (UE_list->UE_sched_ctrl[UE_id].round_UL[CC_id] > 0) {
+		nb_allocated_rbs[CC_id][UE_id] =
+		    UE_list->UE_template[CC_id][UE_id].nb_rb_ul[harq_pid];
+	    } else {
+		nb_allocated_rbs[CC_id][UE_id] =
+		    cmin(UE_list->
+			 UE_template[CC_id][UE_id].pre_allocated_nb_rb_ul,
+			 average_rbs_per_user[CC_id]);
+	    }
 
-      total_allocated_rbs[CC_id]+= nb_allocated_rbs[CC_id][UE_id];
-      LOG_D(MAC,"In ulsch_preprocessor: assigning %d RBs for UE %d/%x CCid %d, harq_pid %d\n",nb_allocated_rbs[CC_id][UE_id],UE_id,rnti,CC_id,harq_pid);
+	    total_allocated_rbs[CC_id] += nb_allocated_rbs[CC_id][UE_id];
+	    LOG_D(MAC,
+		  "In ulsch_preprocessor: assigning %d RBs for UE %d/%x CCid %d, harq_pid %d\n",
+		  nb_allocated_rbs[CC_id][UE_id], UE_id, rnti, CC_id,
+		  harq_pid);
+	}
     }
-  }
 
-  // step 4: assigne the remaining RBs and set the pre_allocated rbs accordingly
-  for(r=0; r<2; r++) {
-
-    for (i=UE_list->head_ul; i>=0; i=UE_list->next_ul[i]) {
-      rnti = UE_RNTI(module_idP,i);
-
-      if (rnti==NOT_A_RNTI)
-        continue;
-      if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-	continue;
-      UE_id = i;
-
-      for (n=0; n<UE_list->numactiveULCCs[UE_id]; n++) {
-        // This is the actual CC_id in the list
-        CC_id = UE_list->ordered_ULCCids[n][UE_id];
-        UE_template = &UE_list->UE_template[CC_id][UE_id];
-        total_remaining_rbs[CC_id]=N_RB_UL - first_rb[CC_id] - total_allocated_rbs[CC_id];
-
-        if (total_ue_count == 1 ) {
-          total_remaining_rbs[CC_id]+=1;
-        }
-
-        if ( r == 0 ) {
-          while ( (UE_template->pre_allocated_nb_rb_ul > 0 ) &&
-                  (nb_allocated_rbs[CC_id][UE_id] < UE_template->pre_allocated_nb_rb_ul) &&
-                  (total_remaining_rbs[CC_id] > 0)) {
-            nb_allocated_rbs[CC_id][UE_id] = cmin(nb_allocated_rbs[CC_id][UE_id]+1,UE_template->pre_allocated_nb_rb_ul);
-            total_remaining_rbs[CC_id]--;
-            total_allocated_rbs[CC_id]++;
-          }
-        } else {
-          UE_template->pre_allocated_nb_rb_ul= nb_allocated_rbs[CC_id][UE_id];
-          LOG_D(MAC,"******************UL Scheduling Information for UE%d CC_id %d ************************\n",UE_id, CC_id);
-          LOG_D(MAC,"[eNB %d] total RB allocated for UE%d CC_id %d  = %d\n", module_idP, UE_id, CC_id, UE_template->pre_allocated_nb_rb_ul);
-        }
-      }
+    // step 4: assigne the remaining RBs and set the pre_allocated rbs accordingly
+    for (r = 0; r < 2; r++) {
+
+	for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
+	    rnti = UE_RNTI(module_idP, i);
+
+	    if (rnti == NOT_A_RNTI)
+		continue;
+	    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+		continue;
+	    UE_id = i;
+
+	    for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
+		// This is the actual CC_id in the list
+		CC_id = UE_list->ordered_ULCCids[n][UE_id];
+		UE_template = &UE_list->UE_template[CC_id][UE_id];
+		total_remaining_rbs[CC_id] =
+		    N_RB_UL - first_rb[CC_id] - total_allocated_rbs[CC_id];
+
+		if (total_ue_count == 1) {
+		    total_remaining_rbs[CC_id] += 1;
+		}
+
+		if (r == 0) {
+		    while ((UE_template->pre_allocated_nb_rb_ul > 0) &&
+			   (nb_allocated_rbs[CC_id][UE_id] <
+			    UE_template->pre_allocated_nb_rb_ul)
+			   && (total_remaining_rbs[CC_id] > 0)) {
+			nb_allocated_rbs[CC_id][UE_id] =
+			    cmin(nb_allocated_rbs[CC_id][UE_id] + 1,
+				 UE_template->pre_allocated_nb_rb_ul);
+			total_remaining_rbs[CC_id]--;
+			total_allocated_rbs[CC_id]++;
+		    }
+		} else {
+		    UE_template->pre_allocated_nb_rb_ul =
+			nb_allocated_rbs[CC_id][UE_id];
+		    LOG_D(MAC,
+			  "******************UL Scheduling Information for UE%d CC_id %d ************************\n",
+			  UE_id, CC_id);
+		    LOG_D(MAC,
+			  "[eNB %d] total RB allocated for UE%d CC_id %d  = %d\n",
+			  module_idP, UE_id, CC_id,
+			  UE_template->pre_allocated_nb_rb_ul);
+		}
+	    }
+	}
     }
-  }
 
-  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
+    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
 
-    if (total_allocated_rbs[CC_id]>0) {
-      LOG_D(MAC,"[eNB %d] total RB allocated for all UEs = %d/%d\n", module_idP, total_allocated_rbs[CC_id], N_RB_UL - first_rb[CC_id]);
+	if (total_allocated_rbs[CC_id] > 0) {
+	    LOG_D(MAC, "[eNB %d] total RB allocated for all UEs = %d/%d\n",
+		  module_idP, total_allocated_rbs[CC_id],
+		  N_RB_UL - first_rb[CC_id]);
+	}
     }
-  }
 }
 
 
-void assign_max_mcs_min_rb(module_id_t module_idP,int frameP, sub_frame_t subframeP, uint16_t *first_rb)
+void
+assign_max_mcs_min_rb(module_id_t module_idP, int frameP,
+		      sub_frame_t subframeP, uint16_t * first_rb)
 {
 
-  int                i;
-  uint16_t           n,UE_id;
-  uint8_t            CC_id;
-  rnti_t             rnti           = -1;
-  int                mcs;
-  int                rb_table_index=0,tbs,tx_power;
-  eNB_MAC_INST       *eNB = RC.mac[module_idP];
-  UE_list_t          *UE_list = &eNB->UE_list;
-
-  UE_TEMPLATE       *UE_template;
-  int Ncp;
-  int N_RB_UL;
-
-  for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
-    if (UE_list->active[i] != TRUE) continue;
-
-    rnti = UE_RNTI(module_idP,i);
-
-    if (rnti==NOT_A_RNTI)
-      continue;
-    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-      continue;
-
-    if (UE_list->UE_sched_ctrl[i].phr_received == 1)
-      mcs = 20; // if we've received the power headroom information the UE, we can go to maximum mcs
-    else
-      mcs = 10; // otherwise, limit to QPSK PUSCH
-
-    UE_id = i;
-
-    for (n=0; n<UE_list->numactiveULCCs[UE_id]; n++) {
-      // This is the actual CC_id in the list
-      CC_id = UE_list->ordered_ULCCids[n][UE_id];
-
-      if (CC_id >= MAX_NUM_CCs) {
-        LOG_E( MAC, "CC_id %u should be < %u, loop n=%u < numactiveULCCs[%u]=%u",
-               CC_id,
-               MAX_NUM_CCs,
-               n,
-               UE_id,
-               UE_list->numactiveULCCs[UE_id]);
-      }
-
-      AssertFatal( CC_id < MAX_NUM_CCs, "CC_id %u should be < %u, loop n=%u < numactiveULCCs[%u]=%u",
-                   CC_id,
-                   MAX_NUM_CCs,
-                   n,
-                   UE_id,
-                   UE_list->numactiveULCCs[UE_id]);
-
-      UE_template = &UE_list->UE_template[CC_id][UE_id];
-
-      Ncp     = RC.mac[module_idP]->common_channels[CC_id].Ncp;
-      N_RB_UL = to_prb(RC.mac[module_idP]->common_channels[CC_id].ul_Bandwidth);
-      // if this UE has UL traffic
-      if (UE_template->ul_total_buffer > 0 ) {
-
-
-        tbs = get_TBS_UL(mcs,3)<<3;  // 1 or 2 PRB with cqi enabled does not work well!
-        rb_table_index = 2;
-
-        // fixme: set use_srs flag
-        tx_power= estimate_ue_tx_power(tbs,rb_table[rb_table_index],0,Ncp,0);
-
-        while ((((UE_template->phr_info - tx_power) < 0 ) || (tbs > UE_template->ul_total_buffer))&&
-               (mcs > 3)) {
-          // LOG_I(MAC,"UE_template->phr_info %d tx_power %d mcs %d\n", UE_template->phr_info,tx_power, mcs);
-          mcs--;
-          tbs = get_TBS_UL(mcs,rb_table[rb_table_index])<<3;
-          tx_power = estimate_ue_tx_power(tbs,rb_table[rb_table_index],0,Ncp,0); // fixme: set use_srs
-        }
-
-        while ((tbs < UE_template->ul_total_buffer) &&
-               (rb_table[rb_table_index]<(N_RB_UL-first_rb[CC_id])) &&
-               ((UE_template->phr_info - tx_power) > 0) &&
-               (rb_table_index < 32 )) {
-   
-          rb_table_index++;
-          tbs = get_TBS_UL(mcs,rb_table[rb_table_index])<<3;
-          tx_power = estimate_ue_tx_power(tbs,rb_table[rb_table_index],0,Ncp,0);
-        }
-
-        UE_template->ue_tx_power = tx_power;
-
-        if (rb_table[rb_table_index]>(N_RB_UL-first_rb[CC_id]-1)) {
-          rb_table_index--;
-        }
-
-        // 1 or 2 PRB with cqi enabled does not work well
-	if (rb_table[rb_table_index]<3) {
-          rb_table_index=2; //3PRB
-        }
-
-        UE_template->pre_assigned_mcs_ul=mcs;
-        UE_template->pre_allocated_rb_table_index_ul=rb_table_index;
-        UE_template->pre_allocated_nb_rb_ul= rb_table[rb_table_index];
-        LOG_D(MAC,"[eNB %d] frame %d subframe %d: for UE %d CC %d: pre-assigned mcs %d, pre-allocated rb_table[%d]=%d RBs (phr %d, tx power %d)\n",
-              module_idP, frameP, subframeP, UE_id, CC_id,
-              UE_template->pre_assigned_mcs_ul,
-              UE_template->pre_allocated_rb_table_index_ul,
-              UE_template->pre_allocated_nb_rb_ul,
-              UE_template->phr_info,tx_power);
-      } else {
-        /* if UE has pending scheduling request then pre-allocate 3 RBs */
-        //if (UE_template->ul_active == 1 && UE_template->ul_SR == 1) {
-        if (UE_is_to_be_scheduled(module_idP, CC_id, i)) {
-          /* use QPSK mcs */
-          UE_template->pre_assigned_mcs_ul             = 10;
-          UE_template->pre_allocated_rb_table_index_ul = 2;
-          UE_template->pre_allocated_nb_rb_ul          = 3;
-        } else {
-          UE_template->pre_assigned_mcs_ul             = 0;
-          UE_template->pre_allocated_rb_table_index_ul = -1;
-          UE_template->pre_allocated_nb_rb_ul          = 0;
-        }
-      }
-    }
-  }
-}
+    int i;
+    uint16_t n, UE_id;
+    uint8_t CC_id;
+    rnti_t rnti = -1;
+    int mcs;
+    int rb_table_index = 0, tbs, tx_power;
+    eNB_MAC_INST *eNB = RC.mac[module_idP];
+    UE_list_t *UE_list = &eNB->UE_list;
 
-struct sort_ue_ul_params {
-  int module_idP;
-  int frameP;
-  int subframeP;
-};
+    UE_TEMPLATE *UE_template;
+    int Ncp;
+    int N_RB_UL;
 
-static int ue_ul_compare(const void *_a, const void *_b, void *_params)
-{
-  struct sort_ue_ul_params *params = _params;
-  UE_list_t *UE_list = &RC.mac[params->module_idP]->UE_list;
+    for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
+	if (UE_list->active[i] != TRUE)
+	    continue;
+
+	rnti = UE_RNTI(module_idP, i);
+
+	if (rnti == NOT_A_RNTI)
+	    continue;
+	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+	    continue;
 
-  int UE_id1 = *(const int *)_a;
-  int UE_id2 = *(const int *)_b;
+	if (UE_list->UE_sched_ctrl[i].phr_received == 1)
+	    mcs = 20;		// if we've received the power headroom information the UE, we can go to maximum mcs
+	else
+	    mcs = 10;		// otherwise, limit to QPSK PUSCH
 
-  int rnti1  = UE_RNTI(params->module_idP, UE_id1);
-  int pCCid1 = UE_PCCID(params->module_idP, UE_id1);
-  int round1 = maxround(params->module_idP, rnti1, params->frameP, params->subframeP, 1);
+	UE_id = i;
 
-  int rnti2  = UE_RNTI(params->module_idP, UE_id2);
-  int pCCid2 = UE_PCCID(params->module_idP, UE_id2);
-  int round2 = maxround(params->module_idP, rnti2, params->frameP, params->subframeP, 1);
+	for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
+	    // This is the actual CC_id in the list
+	    CC_id = UE_list->ordered_ULCCids[n][UE_id];
 
-  if (round1 > round2) return -1;
-  if (round1 < round2) return 1;
+	    if (CC_id >= MAX_NUM_CCs) {
+		LOG_E(MAC,
+		      "CC_id %u should be < %u, loop n=%u < numactiveULCCs[%u]=%u",
+		      CC_id, MAX_NUM_CCs, n, UE_id,
+		      UE_list->numactiveULCCs[UE_id]);
+	    }
 
-  if (UE_list->UE_template[pCCid1][UE_id1].ul_buffer_info[LCGID0] > UE_list->UE_template[pCCid2][UE_id2].ul_buffer_info[LCGID0])
-    return -1;
-  if (UE_list->UE_template[pCCid1][UE_id1].ul_buffer_info[LCGID0] < UE_list->UE_template[pCCid2][UE_id2].ul_buffer_info[LCGID0])
-    return 1;
+	    AssertFatal(CC_id < MAX_NUM_CCs,
+			"CC_id %u should be < %u, loop n=%u < numactiveULCCs[%u]=%u",
+			CC_id, MAX_NUM_CCs, n, UE_id,
+			UE_list->numactiveULCCs[UE_id]);
+
+	    UE_template = &UE_list->UE_template[CC_id][UE_id];
+
+	    Ncp = RC.mac[module_idP]->common_channels[CC_id].Ncp;
+	    N_RB_UL =
+		to_prb(RC.mac[module_idP]->common_channels[CC_id].
+		       ul_Bandwidth);
+	    // if this UE has UL traffic
+	    if (UE_template->ul_total_buffer > 0) {
+
+
+		tbs = get_TBS_UL(mcs, 3) << 3;	// 1 or 2 PRB with cqi enabled does not work well!
+		rb_table_index = 2;
+
+		// fixme: set use_srs flag
+		tx_power =
+		    estimate_ue_tx_power(tbs, rb_table[rb_table_index], 0,
+					 Ncp, 0);
+
+		while ((((UE_template->phr_info - tx_power) < 0)
+			|| (tbs > UE_template->ul_total_buffer))
+		       && (mcs > 3)) {
+		    // LOG_I(MAC,"UE_template->phr_info %d tx_power %d mcs %d\n", UE_template->phr_info,tx_power, mcs);
+		    mcs--;
+		    tbs = get_TBS_UL(mcs, rb_table[rb_table_index]) << 3;
+		    tx_power = estimate_ue_tx_power(tbs, rb_table[rb_table_index], 0, Ncp, 0);	// fixme: set use_srs
+		}
+
+		while ((tbs < UE_template->ul_total_buffer) &&
+		       (rb_table[rb_table_index] <
+			(N_RB_UL - first_rb[CC_id]))
+		       && ((UE_template->phr_info - tx_power) > 0)
+		       && (rb_table_index < 32)) {
+
+		    rb_table_index++;
+		    tbs = get_TBS_UL(mcs, rb_table[rb_table_index]) << 3;
+		    tx_power =
+			estimate_ue_tx_power(tbs, rb_table[rb_table_index],
+					     0, Ncp, 0);
+		}
+
+		UE_template->ue_tx_power = tx_power;
+
+		if (rb_table[rb_table_index] >
+		    (N_RB_UL - first_rb[CC_id] - 1)) {
+		    rb_table_index--;
+		}
+		// 1 or 2 PRB with cqi enabled does not work well
+		if (rb_table[rb_table_index] < 3) {
+		    rb_table_index = 2;	//3PRB
+		}
+
+		UE_template->pre_assigned_mcs_ul = mcs;
+		UE_template->pre_allocated_rb_table_index_ul =
+		    rb_table_index;
+		UE_template->pre_allocated_nb_rb_ul =
+		    rb_table[rb_table_index];
+		LOG_D(MAC,
+		      "[eNB %d] frame %d subframe %d: for UE %d CC %d: pre-assigned mcs %d, pre-allocated rb_table[%d]=%d RBs (phr %d, tx power %d)\n",
+		      module_idP, frameP, subframeP, UE_id, CC_id,
+		      UE_template->pre_assigned_mcs_ul,
+		      UE_template->pre_allocated_rb_table_index_ul,
+		      UE_template->pre_allocated_nb_rb_ul,
+		      UE_template->phr_info, tx_power);
+	    } else {
+		/* if UE has pending scheduling request then pre-allocate 3 RBs */
+		//if (UE_template->ul_active == 1 && UE_template->ul_SR == 1) {
+		if (UE_is_to_be_scheduled(module_idP, CC_id, i)) {
+		    /* use QPSK mcs */
+		    UE_template->pre_assigned_mcs_ul = 10;
+		    UE_template->pre_allocated_rb_table_index_ul = 2;
+		    UE_template->pre_allocated_nb_rb_ul = 3;
+		} else {
+		    UE_template->pre_assigned_mcs_ul = 0;
+		    UE_template->pre_allocated_rb_table_index_ul = -1;
+		    UE_template->pre_allocated_nb_rb_ul = 0;
+		}
+	    }
+	}
+    }
+}
 
-  if (UE_list->UE_template[pCCid1][UE_id1].ul_total_buffer > UE_list->UE_template[pCCid2][UE_id2].ul_total_buffer)
-    return -1;
-  if (UE_list->UE_template[pCCid1][UE_id1].ul_total_buffer < UE_list->UE_template[pCCid2][UE_id2].ul_total_buffer)
-    return 1;
+struct sort_ue_ul_params {
+    int module_idP;
+    int frameP;
+    int subframeP;
+};
 
-  if (UE_list->UE_template[pCCid1][UE_id1].pre_assigned_mcs_ul > UE_list->UE_template[pCCid2][UE_id2].pre_assigned_mcs_ul)
-    return -1;
-  if (UE_list->UE_template[pCCid1][UE_id1].pre_assigned_mcs_ul < UE_list->UE_template[pCCid2][UE_id2].pre_assigned_mcs_ul)
-    return 1;
+static int ue_ul_compare(const void *_a, const void *_b, void *_params)
+{
+    struct sort_ue_ul_params *params = _params;
+    UE_list_t *UE_list = &RC.mac[params->module_idP]->UE_list;
+
+    int UE_id1 = *(const int *) _a;
+    int UE_id2 = *(const int *) _b;
+
+    int rnti1 = UE_RNTI(params->module_idP, UE_id1);
+    int pCCid1 = UE_PCCID(params->module_idP, UE_id1);
+    int round1 = maxround(params->module_idP, rnti1, params->frameP,
+			  params->subframeP, 1);
+
+    int rnti2 = UE_RNTI(params->module_idP, UE_id2);
+    int pCCid2 = UE_PCCID(params->module_idP, UE_id2);
+    int round2 = maxround(params->module_idP, rnti2, params->frameP,
+			  params->subframeP, 1);
+
+    if (round1 > round2)
+	return -1;
+    if (round1 < round2)
+	return 1;
+
+    if (UE_list->UE_template[pCCid1][UE_id1].ul_buffer_info[LCGID0] >
+	UE_list->UE_template[pCCid2][UE_id2].ul_buffer_info[LCGID0])
+	return -1;
+    if (UE_list->UE_template[pCCid1][UE_id1].ul_buffer_info[LCGID0] <
+	UE_list->UE_template[pCCid2][UE_id2].ul_buffer_info[LCGID0])
+	return 1;
+
+    if (UE_list->UE_template[pCCid1][UE_id1].ul_total_buffer >
+	UE_list->UE_template[pCCid2][UE_id2].ul_total_buffer)
+	return -1;
+    if (UE_list->UE_template[pCCid1][UE_id1].ul_total_buffer <
+	UE_list->UE_template[pCCid2][UE_id2].ul_total_buffer)
+	return 1;
+
+    if (UE_list->UE_template[pCCid1][UE_id1].pre_assigned_mcs_ul >
+	UE_list->UE_template[pCCid2][UE_id2].pre_assigned_mcs_ul)
+	return -1;
+    if (UE_list->UE_template[pCCid1][UE_id1].pre_assigned_mcs_ul <
+	UE_list->UE_template[pCCid2][UE_id2].pre_assigned_mcs_ul)
+	return 1;
 
-  return 0;
+    return 0;
 
 #if 0
-  /* The above order derives from the following.
-   * The last case is not handled: "if (UE_list->UE_template[pCCid2][UE_id2].ul_total_buffer > 0 )"
-   * I don't think it makes a big difference.
-   */
-      if(round2 > round1) {
-        swap_UEs(UE_list,UE_id1,UE_id2,1);
-      } else if (round2 == round1) {
-        if (UE_list->UE_template[pCCid1][UE_id1].ul_buffer_info[LCGID0] < UE_list->UE_template[pCCid2][UE_id2].ul_buffer_info[LCGID0]) {
-          swap_UEs(UE_list,UE_id1,UE_id2,1);
-        } else if (UE_list->UE_template[pCCid1][UE_id1].ul_total_buffer <  UE_list->UE_template[pCCid2][UE_id2].ul_total_buffer) {
-          swap_UEs(UE_list,UE_id1,UE_id2,1);
-        } else if (UE_list->UE_template[pCCid1][UE_id1].pre_assigned_mcs_ul <  UE_list->UE_template[pCCid2][UE_id2].pre_assigned_mcs_ul) {
-          if (UE_list->UE_template[pCCid2][UE_id2].ul_total_buffer > 0 ) {
-            swap_UEs(UE_list,UE_id1,UE_id2,1);
-          }
-        }
-      }
+    /* The above order derives from the following.
+     * The last case is not handled: "if (UE_list->UE_template[pCCid2][UE_id2].ul_total_buffer > 0 )"
+     * I don't think it makes a big difference.
+     */
+    if (round2 > round1) {
+	swap_UEs(UE_list, UE_id1, UE_id2, 1);
+    } else if (round2 == round1) {
+	if (UE_list->UE_template[pCCid1][UE_id1].ul_buffer_info[LCGID0] <
+	    UE_list->UE_template[pCCid2][UE_id2].ul_buffer_info[LCGID0]) {
+	    swap_UEs(UE_list, UE_id1, UE_id2, 1);
+	} else if (UE_list->UE_template[pCCid1][UE_id1].ul_total_buffer <
+		   UE_list->UE_template[pCCid2][UE_id2].ul_total_buffer) {
+	    swap_UEs(UE_list, UE_id1, UE_id2, 1);
+	} else if (UE_list->UE_template[pCCid1][UE_id1].
+		   pre_assigned_mcs_ul <
+		   UE_list->UE_template[pCCid2][UE_id2].
+		   pre_assigned_mcs_ul) {
+	    if (UE_list->UE_template[pCCid2][UE_id2].ul_total_buffer > 0) {
+		swap_UEs(UE_list, UE_id1, UE_id2, 1);
+	    }
+	}
+    }
 #endif
 }
 
-void sort_ue_ul (module_id_t module_idP,int frameP, sub_frame_t subframeP)
+void sort_ue_ul(module_id_t module_idP, int frameP, sub_frame_t subframeP)
 {
-  int               i;
-  int               list[NUMBER_OF_UE_MAX];
-  int               list_size = 0;
-  int               rnti;
-  struct sort_ue_ul_params params = { module_idP, frameP, subframeP };
+    int i;
+    int list[NUMBER_OF_UE_MAX];
+    int list_size = 0;
+    int rnti;
+    struct sort_ue_ul_params params = { module_idP, frameP, subframeP };
 
-  UE_list_t *UE_list = &RC.mac[module_idP]->UE_list;
+    UE_list_t *UE_list = &RC.mac[module_idP]->UE_list;
 
-  for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
-    if (UE_list->active[i] == FALSE) continue;
-    if ((rnti = UE_RNTI(module_idP, i)) == NOT_A_RNTI) continue;
-    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1) continue;
+    for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
+	if (UE_list->active[i] == FALSE)
+	    continue;
+	if ((rnti = UE_RNTI(module_idP, i)) == NOT_A_RNTI)
+	    continue;
+	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+	    continue;
 
-    list[list_size] = i;
-    list_size++;
-  }
+	list[list_size] = i;
+	list_size++;
+    }
 
-  qsort_r(list, list_size, sizeof(int), ue_ul_compare, &params);
+    qsort_r(list, list_size, sizeof(int), ue_ul_compare, &params);
 
-  if (list_size) {
-    for (i = 0; i < list_size-1; i++)
-      UE_list->next_ul[list[i]] = list[i+1];
-    UE_list->next_ul[list[list_size-1]] = -1;
-    UE_list->head_ul = list[0];
-  } else {
-    UE_list->head_ul = -1;
-  }
+    if (list_size) {
+	for (i = 0; i < list_size - 1; i++)
+	    UE_list->next_ul[list[i]] = list[i + 1];
+	UE_list->next_ul[list[list_size - 1]] = -1;
+	UE_list->head_ul = list[0];
+    } else {
+	UE_list->head_ul = -1;
+    }
 
 #if 0
-  int               UE_id1,UE_id2;
-  int               pCCid1,pCCid2;
-  int               round1,round2;
-  int               i=0,ii=0;
-  rnti_t            rnti1,rnti2;
-
-  UE_list_t *UE_list = &RC.mac[module_idP]->UE_list;
-
-  for (i=UE_list->head_ul; i>=0; i=UE_list->next_ul[i]) {
-
-    //LOG_I(MAC,"sort ue ul i %d\n",i);
-    for (ii=UE_list->next_ul[i]; ii>=0; ii=UE_list->next_ul[ii]) {
-      //LOG_I(MAC,"sort ul ue 2 ii %d\n",ii);
- 
-      UE_id1  = i;
-      rnti1 = UE_RNTI(module_idP,UE_id1);
-      
-      if(rnti1 == NOT_A_RNTI)
-	continue;
-      if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
-	continue;
-
-
-      pCCid1 = UE_PCCID(module_idP,UE_id1);
-      round1  = maxround(module_idP,rnti1,frameP,subframeP,1);
-      
-      UE_id2  = ii;
-      rnti2 = UE_RNTI(module_idP,UE_id2);
-      
-      if(rnti2 == NOT_A_RNTI)
-        continue;
-      if (UE_list->UE_sched_ctrl[UE_id2].ul_out_of_sync == 1)
-	continue;
-
-      pCCid2 = UE_PCCID(module_idP,UE_id2);
-      round2  = maxround(module_idP,rnti2,frameP,subframeP,1);
-
-      if(round2 > round1) {
-        swap_UEs(UE_list,UE_id1,UE_id2,1);
-      } else if (round2 == round1) {
-        if (UE_list->UE_template[pCCid1][UE_id1].ul_buffer_info[LCGID0] < UE_list->UE_template[pCCid2][UE_id2].ul_buffer_info[LCGID0]) {
-          swap_UEs(UE_list,UE_id1,UE_id2,1);
-        } else if (UE_list->UE_template[pCCid1][UE_id1].ul_total_buffer <  UE_list->UE_template[pCCid2][UE_id2].ul_total_buffer) {
-          swap_UEs(UE_list,UE_id1,UE_id2,1);
-        } else if (UE_list->UE_template[pCCid1][UE_id1].pre_assigned_mcs_ul <  UE_list->UE_template[pCCid2][UE_id2].pre_assigned_mcs_ul) {
-          if (UE_list->UE_template[pCCid2][UE_id2].ul_total_buffer > 0 ) {
-            swap_UEs(UE_list,UE_id1,UE_id2,1);
-          }
-        }
-      }
+    int UE_id1, UE_id2;
+    int pCCid1, pCCid2;
+    int round1, round2;
+    int i = 0, ii = 0;
+    rnti_t rnti1, rnti2;
+
+    UE_list_t *UE_list = &RC.mac[module_idP]->UE_list;
+
+    for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
+
+	//LOG_I(MAC,"sort ue ul i %d\n",i);
+	for (ii = UE_list->next_ul[i]; ii >= 0; ii = UE_list->next_ul[ii]) {
+	    //LOG_I(MAC,"sort ul ue 2 ii %d\n",ii);
+
+	    UE_id1 = i;
+	    rnti1 = UE_RNTI(module_idP, UE_id1);
+
+	    if (rnti1 == NOT_A_RNTI)
+		continue;
+	    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
+		continue;
+
+
+	    pCCid1 = UE_PCCID(module_idP, UE_id1);
+	    round1 = maxround(module_idP, rnti1, frameP, subframeP, 1);
+
+	    UE_id2 = ii;
+	    rnti2 = UE_RNTI(module_idP, UE_id2);
+
+	    if (rnti2 == NOT_A_RNTI)
+		continue;
+	    if (UE_list->UE_sched_ctrl[UE_id2].ul_out_of_sync == 1)
+		continue;
+
+	    pCCid2 = UE_PCCID(module_idP, UE_id2);
+	    round2 = maxround(module_idP, rnti2, frameP, subframeP, 1);
+
+	    if (round2 > round1) {
+		swap_UEs(UE_list, UE_id1, UE_id2, 1);
+	    } else if (round2 == round1) {
+		if (UE_list->
+		    UE_template[pCCid1][UE_id1].ul_buffer_info[LCGID0] <
+		    UE_list->UE_template[pCCid2][UE_id2].
+		    ul_buffer_info[LCGID0]) {
+		    swap_UEs(UE_list, UE_id1, UE_id2, 1);
+		} else if (UE_list->UE_template[pCCid1][UE_id1].
+			   ul_total_buffer <
+			   UE_list->UE_template[pCCid2][UE_id2].
+			   ul_total_buffer) {
+		    swap_UEs(UE_list, UE_id1, UE_id2, 1);
+		} else if (UE_list->
+			   UE_template[pCCid1][UE_id1].pre_assigned_mcs_ul
+			   <
+			   UE_list->
+			   UE_template[pCCid2][UE_id2].pre_assigned_mcs_ul)
+		{
+		    if (UE_list->UE_template[pCCid2][UE_id2].
+			ul_total_buffer > 0) {
+			swap_UEs(UE_list, UE_id1, UE_id2, 1);
+		    }
+		}
+	    }
+	}
     }
-  }
 #endif
 }
diff --git a/openair2/LAYER2/MAC/proto.h b/openair2/LAYER2/MAC/proto.h
index b316cbfeeb8333e709106d0e44a2dc024760d454..bbd6d6a36d84d365e6a63bc8b2222303a3a9bf02 100644
--- a/openair2/LAYER2/MAC/proto.h
+++ b/openair2/LAYER2/MAC/proto.h
@@ -43,12 +43,11 @@
 
 */
 
-void schedule_mib(module_id_t   module_idP,
-		  frame_t       frameP,
-		  sub_frame_t   subframeP);
+void schedule_mib(module_id_t module_idP,
+		  frame_t frameP, sub_frame_t subframeP);
 
 /** \fn void schedule_RA(module_id_t module_idP,frame_t frameP,sub_frame_t subframe);
-\brief First stage of Random-Access Scheduling. Loops over the RA_templates and checks if RAR, Msg3 or its retransmission are to be scheduled in the subframe.  It returns the total number of PRB used for RA SDUs.  For Msg3 it retrieves the L3msg from RRC and fills the appropriate buffers.  For the others it just computes the number of PRBs. Each DCI uses 3 PRBs (format 1A)
+\brief First stage of Random-Access Scheduling. Loops over the ras and checks if RAR, Msg3 or its retransmission are to be scheduled in the subframe.  It returns the total number of PRB used for RA SDUs.  For Msg3 it retrieves the L3msg from RRC and fills the appropriate buffers.  For the others it just computes the number of PRBs. Each DCI uses 3 PRBs (format 1A)
 for the message.
 @param Mod_id Instance ID of eNB
 @param frame Frame index
@@ -56,28 +55,32 @@ for the message.
 */
 
 
-void schedule_RA(module_id_t module_idP,frame_t frameP,sub_frame_t subframe);
+void schedule_RA(module_id_t module_idP, frame_t frameP,
+		 sub_frame_t subframe);
 
 /** \brief First stage of SI Scheduling. Gets a SI SDU from RRC if available and computes the MCS required to transport it as a function of the SDU length.  It assumes a length less than or equal to 64 bytes (MCS 6, 3 PRBs).
 @param Mod_id Instance ID of eNB
 @param frame Frame index
 @param subframe Subframe number on which to act
 */
-void schedule_SI(module_id_t module_idP,frame_t frameP,sub_frame_t subframeP);
+void schedule_SI(module_id_t module_idP, frame_t frameP,
+		 sub_frame_t subframeP);
 
 /** \brief MBMS scheduling: Checking the position for MBSFN subframes. Create MSI, transfer MCCH from RRC to MAC, transfer MTCHs from RLC to MAC. Multiplexing MSI,MCCH&MTCHs. Return 1 if there are MBSFN data being allocated, otherwise return 0;
 @param Mod_id Instance ID of eNB
 @param frame Frame index
 @param subframe Subframe number on which to act
 */
-int schedule_MBMS(module_id_t module_idP,uint8_t CC_id, frame_t frameP, sub_frame_t subframe);
+int schedule_MBMS(module_id_t module_idP, uint8_t CC_id, frame_t frameP,
+		  sub_frame_t subframe);
 
 /** \brief check the mapping between sf allocation and sync area, Currently only supports 1:1 mapping
 @param Mod_id Instance ID of eNB
 @param mbsfn_sync_area index of mbsfn sync area
 @param[out] index of sf pattern
 */
-int8_t get_mbsfn_sf_alloction (module_id_t module_idP, uint8_t CC_id, uint8_t mbsfn_sync_area);
+int8_t get_mbsfn_sf_alloction(module_id_t module_idP, uint8_t CC_id,
+			      uint8_t mbsfn_sync_area);
 
 /** \brief check the mapping between sf allocation and sync area, Currently only supports 1:1 mapping
 @param Mod_id Instance ID of eNB
@@ -85,14 +88,17 @@ int8_t get_mbsfn_sf_alloction (module_id_t module_idP, uint8_t CC_id, uint8_t mb
 @param eNB_index index of eNB
 @param[out] index of sf pattern
 */
-int8_t ue_get_mbsfn_sf_alloction (module_id_t module_idP, uint8_t mbsfn_sync_area, unsigned char eNB_index);
+int8_t ue_get_mbsfn_sf_alloction(module_id_t module_idP,
+				 uint8_t mbsfn_sync_area,
+				 unsigned char eNB_index);
 
 /** \brief top ULSCH Scheduling for TDD (config 1-6).
 @param Mod_id Instance ID of eNB
 @param frame Frame index
 @param subframe Subframe number on which to act
 */
-void schedule_ulsch(module_id_t module_idP,frame_t frameP,sub_frame_t subframe);
+void schedule_ulsch(module_id_t module_idP, frame_t frameP,
+		    sub_frame_t subframe);
 
 /** \brief ULSCH Scheduling per RNTI
 @param Mod_id Instance ID of eNB
@@ -100,7 +106,10 @@ void schedule_ulsch(module_id_t module_idP,frame_t frameP,sub_frame_t subframe);
 @param subframe Subframe number on which to act
 @param sched_subframe Subframe number where PUSCH is transmitted (for DAI lookup)
 */
-void schedule_ulsch_rnti(module_id_t module_idP, frame_t frameP, sub_frame_t subframe, unsigned char sched_subframe, uint16_t *first_rb);
+void schedule_ulsch_rnti(module_id_t module_idP, frame_t frameP,
+			 sub_frame_t subframe,
+			 unsigned char sched_subframe,
+			 uint16_t * first_rb);
 
 /** \brief Second stage of DLSCH scheduling, after schedule_SI, schedule_RA and schedule_dlsch have been called.  This routine first allocates random frequency assignments for SI and RA SDUs using distributed VRB allocations and adds the corresponding DCI SDU to the DCI buffer for PHY.  It then loops over the UE specific DCIs previously allocated and fills in the remaining DCI fields related to frequency allocation.  It assumes localized allocation of type 0 (DCI.rah=0).  The allocation is done for tranmission modes 1,2,4.
 @param Mod_id Instance of eNB
@@ -108,7 +117,8 @@ void schedule_ulsch_rnti(module_id_t module_idP, frame_t frameP, sub_frame_t sub
 @param subframe Index of subframe
 @param mbsfn_flag Indicates that this subframe is for MCH/MCCH
 */
-void fill_DLSCH_dci(module_id_t module_idP,frame_t frameP,sub_frame_t subframe,int *mbsfn_flag);
+void fill_DLSCH_dci(module_id_t module_idP, frame_t frameP,
+		    sub_frame_t subframe, int *mbsfn_flag);
 
 /** \brief UE specific DLSCH scheduling. Retrieves next ue to be schduled from round-robin scheduler and gets the appropriate harq_pid for the subframe from PHY. If the process is active and requires a retransmission, it schedules the retransmission with the same PRB count and MCS as the first transmission. Otherwise it consults RLC for DCCH/DTCH SDUs (status with maximum number of available PRBS), builds the MAC header (timing advance sent by default) and copies
 @param Mod_id Instance ID of eNB
@@ -117,39 +127,43 @@ void fill_DLSCH_dci(module_id_t module_idP,frame_t frameP,sub_frame_t subframe,i
 
 @param mbsfn_flag  Indicates that MCH/MCCH is in this subframe
 */
-void schedule_ue_spec(module_id_t module_idP,frame_t frameP,sub_frame_t subframe,int *mbsfn_flag);
+void schedule_ue_spec(module_id_t module_idP, frame_t frameP,
+		      sub_frame_t subframe, int *mbsfn_flag);
 
 
 /** \brief Function for UE/PHY to compute PUSCH transmit power in power-control procedure.
     @param Mod_id Module id of UE
     @returns Po_NOMINAL_PUSCH (PREAMBLE_RECEIVED_TARGET_POWER+DELTA_PREAMBLE
 */
-int8_t get_Po_NOMINAL_PUSCH(module_id_t module_idP,uint8_t CC_id);
+int8_t get_Po_NOMINAL_PUSCH(module_id_t module_idP, uint8_t CC_id);
 
 /** \brief Function to compute DELTA_PREAMBLE from 36.321 (for RA power ramping procedure and Msg3 PUSCH power control policy)
     @param Mod_id Module id of UE
     @returns DELTA_PREAMBLE
 */
-int8_t get_DELTA_PREAMBLE(module_id_t module_idP,int CC_id);
+int8_t get_DELTA_PREAMBLE(module_id_t module_idP, int CC_id);
 
 /** \brief Function for compute deltaP_rampup from 36.321 (for RA power ramping procedure and Msg3 PUSCH power control policy)
     @param Mod_id Module id of UE
     @param CC_id carrier component id of UE
     @returns deltaP_rampup
 */
-int8_t get_deltaP_rampup(module_id_t module_idP,uint8_t CC_id);
+int8_t get_deltaP_rampup(module_id_t module_idP, uint8_t CC_id);
 
-uint16_t mac_computeRIV(uint16_t N_RB_DL,uint16_t RBstart,uint16_t Lcrbs);
+uint16_t mac_computeRIV(uint16_t N_RB_DL, uint16_t RBstart,
+			uint16_t Lcrbs);
 
-void add_msg3(module_id_t module_idP,int CC_id, RA_TEMPLATE *RA_template, frame_t frameP, sub_frame_t subframeP);
+void add_msg3(module_id_t module_idP, int CC_id, RA_t * ra, frame_t frameP,
+	      sub_frame_t subframeP);
 
 //main.c
 
-int mac_top_init(int eMBMS_active, char *uecap_xer,uint8_t cba_group_active, uint8_t HO_active);
+int mac_top_init(int eMBMS_active, char *uecap_xer,
+		 uint8_t cba_group_active, uint8_t HO_active);
 
 void mac_top_init_eNB(void);
 
-void mac_init_cell_params(int Mod_idP,int CC_idP);
+void mac_init_cell_params(int Mod_idP, int CC_idP);
 
 char layer2_init_UE(module_id_t module_idP);
 
@@ -161,19 +175,29 @@ int mac_init_global_param(void);
 
 void mac_top_cleanup(void);
 
-void mac_UE_out_of_sync_ind(module_id_t module_idP,frame_t frameP, uint16_t eNB_index);
-
-void clear_nfapi_information(eNB_MAC_INST *eNB,int CC_idP,frame_t frameP,sub_frame_t subframeP);
-
-void dlsch_scheduler_pre_processor_reset (int module_idP,int UE_id,
-    uint8_t  CC_id,
-    int frameP,
-    int subframeP,
-    int N_RBG,
-    uint16_t nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-    uint16_t  nb_rbs_required_remaining[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-    unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX],
-    unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX]);
+void mac_UE_out_of_sync_ind(module_id_t module_idP, frame_t frameP,
+			    uint16_t eNB_index);
+
+void clear_nfapi_information(eNB_MAC_INST * eNB, int CC_idP,
+			     frame_t frameP, sub_frame_t subframeP);
+
+void dlsch_scheduler_pre_processor_reset(int module_idP, int UE_id,
+					 uint8_t CC_id,
+					 int frameP,
+					 int subframeP,
+					 int N_RBG,
+					 uint16_t
+					 nb_rbs_required[MAX_NUM_CCs]
+					 [NUMBER_OF_UE_MAX],
+					 uint16_t
+					 nb_rbs_required_remaining
+					 [MAX_NUM_CCs][NUMBER_OF_UE_MAX],
+					 unsigned char
+					 rballoc_sub[MAX_NUM_CCs]
+					 [N_RBG_MAX],
+					 unsigned char
+					 MIMO_mode_indicator[MAX_NUM_CCs]
+					 [N_RBG_MAX]);
 
 // eNB functions
 /* \brief This function assigns pre-available RBS to each UE in specified sub-bands before scheduling is done
@@ -184,24 +208,33 @@ void dlsch_scheduler_pre_processor_reset (int module_idP,int UE_id,
  */
 
 
-void dlsch_scheduler_pre_processor (module_id_t module_idP,
-                                    frame_t frameP,
-                                    sub_frame_t subframe,
-                                    int N_RBG[MAX_NUM_CCs],
-                                    int *mbsfn_flag);
-
-
-void dlsch_scheduler_pre_processor_allocate (module_id_t   Mod_id,
-    int           UE_id,
-    uint8_t       CC_id,
-    int           N_RBG,
-    int           transmission_mode,
-    int           min_rb_unit,
-    uint8_t       N_RB_DL,
-    uint16_t      nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-    uint16_t      nb_rbs_required_remaining[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
-    unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX],
-    unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX]);
+void dlsch_scheduler_pre_processor(module_id_t module_idP,
+				   frame_t frameP,
+				   sub_frame_t subframe,
+				   int N_RBG[MAX_NUM_CCs],
+				   int *mbsfn_flag);
+
+
+void dlsch_scheduler_pre_processor_allocate(module_id_t Mod_id,
+					    int UE_id,
+					    uint8_t CC_id,
+					    int N_RBG,
+					    int transmission_mode,
+					    int min_rb_unit,
+					    uint8_t N_RB_DL,
+					    uint16_t
+					    nb_rbs_required[MAX_NUM_CCs]
+					    [NUMBER_OF_UE_MAX],
+					    uint16_t
+					    nb_rbs_required_remaining
+					    [MAX_NUM_CCs]
+					    [NUMBER_OF_UE_MAX],
+					    unsigned char
+					    rballoc_sub[MAX_NUM_CCs]
+					    [N_RBG_MAX],
+					    unsigned char
+					    MIMO_mode_indicator
+					    [MAX_NUM_CCs][N_RBG_MAX]);
 
 /* \brief Function to trigger the eNB scheduling procedure.  It is called by PHY at the beginning of each subframe, \f$n$\f
    and generates all DLSCH allocations for subframe \f$n\f$ and ULSCH allocations for subframe \f$n+k$\f. 
@@ -209,7 +242,7 @@ void dlsch_scheduler_pre_processor_allocate (module_id_t   Mod_id,
 @param subframe Index of current subframe
 @param calibration_flag Flag to indicate that eNB scheduler should schedule TDD auto-calibration PUSCH.
 */
-void eNB_dlsch_ulsch_scheduler(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP);//, int calibration_flag);
+void eNB_dlsch_ulsch_scheduler(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP);	//, int calibration_flag);
 
 /* \brief Function to indicate a received preamble on PRACH.  It initiates the RA procedure.
 @param Mod_id Instance ID of eNB
@@ -218,12 +251,13 @@ void eNB_dlsch_ulsch_scheduler(module_id_t module_idP, frame_t frameP, sub_frame
 @param rnti RA rnti corresponding to this PRACH preamble
 @param rach_resource type (0=non BL/CE,1 CE level 0,2 CE level 1, 3 CE level 2,4 CE level 3)
 */
-void initiate_ra_proc(module_id_t module_idP,int CC_id,frame_t frameP, sub_frame_t subframeP, uint16_t preamble_index,int16_t timing_offset,uint16_t rnti
+void initiate_ra_proc(module_id_t module_idP, int CC_id, frame_t frameP,
+		      sub_frame_t subframeP, uint16_t preamble_index,
+		      int16_t timing_offset, uint16_t rnti
 #ifdef Rel14
-		      ,
-		      uint8_t rach_resource_type
+		      , uint8_t rach_resource_type
 #endif
-		      );
+    );
 
 /* \brief Function in eNB to fill RAR pdu when requested by PHY.  This provides a single RAR SDU for the moment and returns the t-CRNTI.
 @param Mod_id Instance ID of eNB
@@ -231,31 +265,30 @@ void initiate_ra_proc(module_id_t module_idP,int CC_id,frame_t frameP, sub_frame
 @param N_RB_UL Number of UL resource blocks
 @returns t_CRNTI
 */
-unsigned short fill_rar(
-  const module_id_t module_idP,
-  const int         CC_id,
-  const frame_t     frameP,
-  uint8_t   * const dlsch_buffer,
-  const uint16_t    N_RB_UL,
-  const uint8_t input_buffer_length
-);
+unsigned short fill_rar(const module_id_t module_idP,
+			const int CC_id,
+			RA_t *ra,
+			const frame_t frameP,
+			uint8_t * const dlsch_buffer,
+			const uint16_t N_RB_UL,
+			const uint8_t input_buffer_length);
 
 #ifdef Rel14
-unsigned short fill_rar_br(eNB_MAC_INST *eNB,
+unsigned short fill_rar_br(eNB_MAC_INST * eNB,
 			   int CC_id,
-			   RA_TEMPLATE        *RA_template,      
-			   const frame_t      frameP,
-			   const sub_frame_t  subframeP,
-			   uint8_t*    const  dlsch_buffer,
-			   const uint8_t      ce_level
-			   );
+			   RA_t * ra,
+			   const frame_t frameP,
+			   const sub_frame_t subframeP,
+			   uint8_t * const dlsch_buffer,
+			   const uint8_t ce_level);
 #endif
 
 /* \brief Function to indicate a failed RA response.  It removes all temporary variables related to the initial connection of a UE
 @param Mod_id Instance ID of eNB
 @param preamble_index index of the received RA request.
 */
-void cancel_ra_proc(module_id_t module_idP,int CC_id,frame_t frameP, uint16_t preamble_index);
+void cancel_ra_proc(module_id_t module_idP, int CC_id, frame_t frameP,
+		    uint16_t preamble_index);
 
 /* \brief Function used by PHY to inform MAC that an uplink is scheduled
           for Msg3 in given subframe. This is used so that the MAC
@@ -269,12 +302,10 @@ void cancel_ra_proc(module_id_t module_idP,int CC_id,frame_t frameP, uint16_t pr
 @param Msg3_subframe subframe where scheduling takes place
 */
 void set_msg3_subframe(module_id_t Mod_id,
-                       int CC_id,
-                       int frame,
-                       int subframe,
-                       int rnti,
-                       int Msg3_frame,
-                       int Msg3_subframe);
+		       int CC_id,
+		       int frame,
+		       int subframe,
+		       int rnti, int Msg3_frame, int Msg3_subframe);
 
 /* \brief Function to indicate a received SDU on ULSCH.
 @param Mod_id Instance ID of eNB
@@ -286,14 +317,13 @@ void set_msg3_subframe(module_id_t Mod_id,
 @param ul_cqi Uplink CQI estimate after this pdu (SNR quantized to 8 bits, -64 ... 63.5 dB in .5dB steps)
 */
 void rx_sdu(const module_id_t enb_mod_idP,
-	    const int         CC_idP,
-	    const frame_t     frameP,
+	    const int CC_idP,
+	    const frame_t frameP,
 	    const sub_frame_t subframeP,
-	    const rnti_t      rntiP,
-	    uint8_t          *sduP,
-	    const uint16_t    sdu_lenP,
-	    const uint16_t    timing_advance,
-	    const uint8_t     ul_cqi);
+	    const rnti_t rntiP,
+	    uint8_t * sduP,
+	    const uint16_t sdu_lenP,
+	    const uint16_t timing_advance, const uint8_t ul_cqi);
 
 
 /* \brief Function to indicate a scheduled schduling request (SR) was received by eNB.
@@ -304,7 +334,8 @@ void rx_sdu(const module_id_t enb_mod_idP,
 @param rnti RNTI of UE transmitting the SR
 @param ul_cqi SNR measurement of PUCCH (SNR quantized to 8 bits, -64 ... 63.5 dB in .5dB steps)
 */
-void SR_indication(module_id_t module_idP,int CC_id,frame_t frameP,sub_frame_t subframe,rnti_t rnti,uint8_t ul_cqi);
+void SR_indication(module_id_t module_idP, int CC_id, frame_t frameP,
+		   sub_frame_t subframe, rnti_t rnti, uint8_t ul_cqi);
 
 /* \brief Function to indicate a UL failure was detected by eNB PHY.
 @param Mod_id Instance ID of eNB
@@ -313,7 +344,8 @@ void SR_indication(module_id_t module_idP,int CC_id,frame_t frameP,sub_frame_t s
 @param rnti RNTI of UE transmitting the SR
 @param subframe Index of subframe where SR was received
 */
-void UL_failure_indication(module_id_t Mod_id,int CC_id,frame_t frameP,rnti_t rnti,sub_frame_t subframe);
+void UL_failure_indication(module_id_t Mod_id, int CC_id, frame_t frameP,
+			   rnti_t rnti, sub_frame_t subframe);
 
 /* \brief Function to indicate an HARQ ACK/NAK.
 @param Mod_id Instance ID of eNB
@@ -322,7 +354,9 @@ void UL_failure_indication(module_id_t Mod_id,int CC_id,frame_t frameP,rnti_t rn
 @param subframeP subframe index
 @param harq_pdu NFAPI HARQ PDU descriptor
 */
-void harq_indication(module_id_t mod_idP, int CC_idP, frame_t frameP, sub_frame_t subframeP, nfapi_harq_indication_pdu_t *harq_pdu);
+void harq_indication(module_id_t mod_idP, int CC_idP, frame_t frameP,
+		     sub_frame_t subframeP,
+		     nfapi_harq_indication_pdu_t * harq_pdu);
 
 /* \brief Function to indicate a received CQI pdu
 @param Mod_id Instance ID of eNB
@@ -332,11 +366,13 @@ void harq_indication(module_id_t mod_idP, int CC_idP, frame_t frameP, sub_frame_
 @param rntiP RNTI of incoming CQI information
 @param ul_cqi_information NFAPI UL CQI measurement
 */
-void cqi_indication(module_id_t mod_idP, int CC_idP, frame_t frameP, sub_frame_t subframeP, rnti_t rntiP, 
-		    nfapi_cqi_indication_rel9_t *rel9,uint8_t *pdu,
-		    nfapi_ul_cqi_information_t *ul_cqi_information);
+void cqi_indication(module_id_t mod_idP, int CC_idP, frame_t frameP,
+		    sub_frame_t subframeP, rnti_t rntiP,
+		    nfapi_cqi_indication_rel9_t * rel9, uint8_t * pdu,
+		    nfapi_ul_cqi_information_t * ul_cqi_information);
 
-uint8_t *get_dlsch_sdu(module_id_t module_idP,int CC_id,frame_t frameP,rnti_t rnti,uint8_t TBindex);
+uint8_t *get_dlsch_sdu(module_id_t module_idP, int CC_id, frame_t frameP,
+		       rnti_t rnti, uint8_t TBindex);
 
 /* \brief Function to retrieve MCH transport block and MCS used for MCH in this MBSFN subframe.  Returns null if no MCH is to be transmitted
 @param Mod_id Instance ID of eNB
@@ -345,69 +381,70 @@ uint8_t *get_dlsch_sdu(module_id_t module_idP,int CC_id,frame_t frameP,rnti_t rn
 @param mcs Pointer to mcs used by PHY (to be filled by MAC)
 @returns Pointer to MCH transport block and mcs for subframe
 */
-MCH_PDU *get_mch_sdu( module_id_t Mod_id, int CC_id, frame_t frame, sub_frame_t subframe);
+MCH_PDU *get_mch_sdu(module_id_t Mod_id, int CC_id, frame_t frame,
+		     sub_frame_t subframe);
 
 
 
 
 
-void        ue_mac_reset      (module_id_t module_idP,uint8_t eNB_index);
-void        ue_init_mac       (module_id_t module_idP);
-void        init_ue_sched_info(void);
-void        add_ue_ulsch_info (module_id_t module_idP, int CC_id, int UE_id, sub_frame_t subframe,UE_ULSCH_STATUS status);
-void        add_ue_dlsch_info (module_id_t module_idP, int CC_id,int UE_id, sub_frame_t subframe,UE_DLSCH_STATUS status);
-int         find_UE_id        (module_id_t module_idP, rnti_t rnti) ;
-int         find_RA_id        (module_id_t mod_idP, int CC_idP, rnti_t rntiP);
-rnti_t      UE_RNTI           (module_id_t module_idP, int UE_id);
-int         UE_PCCID          (module_id_t module_idP, int UE_id);
-uint8_t     find_active_UEs   (module_id_t module_idP);
-boolean_t   is_UE_active      (module_id_t module_idP, int UE_id);
-uint8_t     get_aggregation   (uint8_t bw_index, uint8_t cqi, uint8_t dci_fmt);
+void ue_mac_reset(module_id_t module_idP, uint8_t eNB_index);
+void ue_init_mac(module_id_t module_idP);
+void init_ue_sched_info(void);
+void add_ue_ulsch_info(module_id_t module_idP, int CC_id, int UE_id,
+		       sub_frame_t subframe, UE_ULSCH_STATUS status);
+void add_ue_dlsch_info(module_id_t module_idP, int CC_id, int UE_id,
+		       sub_frame_t subframe, UE_DLSCH_STATUS status);
+int find_UE_id(module_id_t module_idP, rnti_t rnti);
+int find_RA_id(module_id_t mod_idP, int CC_idP, rnti_t rntiP);
+rnti_t UE_RNTI(module_id_t module_idP, int UE_id);
+int UE_PCCID(module_id_t module_idP, int UE_id);
+uint8_t find_active_UEs(module_id_t module_idP);
+boolean_t is_UE_active(module_id_t module_idP, int UE_id);
+uint8_t get_aggregation(uint8_t bw_index, uint8_t cqi, uint8_t dci_fmt);
 
 int8_t find_active_UEs_with_traffic(module_id_t module_idP);
 
-void init_CCE_table(int module_idP,int CC_idP);
+void init_CCE_table(int module_idP, int CC_idP);
 
 int get_nCCE_offset(int *CCE_table,
-		    const unsigned char L, 
-		    const int nCCE, 
-		    const int common_dci, 
-		    const unsigned short rnti, 
+		    const unsigned char L,
+		    const int nCCE,
+		    const int common_dci,
+		    const unsigned short rnti,
 		    const unsigned char subframe);
 
-int allocate_CCEs(int module_idP,
-		  int CC_idP,
-		  int subframe,
-		  int test_only);
+int allocate_CCEs(int module_idP, int CC_idP, int subframe, int test_only);
 
 boolean_t CCE_allocation_infeasible(int module_idP,
-				  int CC_idP,
-				  int common_flag,
-				  int subframe,
-				  int aggregation,
-				  int rnti);
-
-void set_ue_dai(sub_frame_t   subframeP,
-                int           UE_id,
-                uint8_t       CC_id,
-                uint8_t       tdd_config,
-                UE_list_t     *UE_list);
-
-uint8_t find_num_active_UEs_in_cbagroup(module_id_t module_idP, unsigned char group_id);
-uint8_t UE_is_to_be_scheduled(module_id_t module_idP,int CC_id,uint8_t UE_id);
+				    int CC_idP,
+				    int common_flag,
+				    int subframe,
+				    int aggregation, int rnti);
+
+void set_ue_dai(sub_frame_t subframeP,
+		int UE_id,
+		uint8_t CC_id, uint8_t tdd_config, UE_list_t * UE_list);
+
+uint8_t find_num_active_UEs_in_cbagroup(module_id_t module_idP,
+					unsigned char group_id);
+uint8_t UE_is_to_be_scheduled(module_id_t module_idP, int CC_id,
+			      uint8_t UE_id);
 /** \brief Round-robin scheduler for ULSCH traffic.
 @param Mod_id Instance ID for eNB
 @param subframe Subframe number on which to act
 @returns UE index that is to be scheduled if needed/room
 */
-module_id_t schedule_next_ulue(module_id_t module_idP, int UE_id,sub_frame_t subframe);
+module_id_t schedule_next_ulue(module_id_t module_idP, int UE_id,
+			       sub_frame_t subframe);
 
 /** \brief Round-robin scheduler for DLSCH traffic.
 @param Mod_id Instance ID for eNB
 @param subframe Subframe number on which to act
 @returns UE index that is to be scheduled if needed/room
 */
-int schedule_next_dlue(module_id_t module_idP, int CC_id, sub_frame_t subframe);
+int schedule_next_dlue(module_id_t module_idP, int CC_id,
+		       sub_frame_t subframe);
 
 /* \brief Allocates a set of PRBS for a particular UE.  This is a simple function for the moment, later it should process frequency-domain CQI information and/or PMI information.  Currently it just returns the first PRBS that are available in the subframe based on the number requested.
 @param UE_id Index of UE on which to act
@@ -416,7 +453,8 @@ int schedule_next_dlue(module_id_t module_idP, int CC_id, sub_frame_t subframe);
 @param rballoc Pointer to bit-map of current PRB allocation given to previous users/control channels.  This is updated for subsequent calls to the routine.
 @returns an rballoc bitmap for resource type 0 allocation (DCI).
 */
-uint32_t allocate_prbs(int UE_id,uint8_t nb_rb, int N_RB_DL, uint32_t *rballoc);
+uint32_t allocate_prbs(int UE_id, uint8_t nb_rb, int N_RB_DL,
+		       uint32_t * rballoc);
 
 /* \fn uint32_t req_new_ulsch(module_id_t module_idP)
 \brief check for a new transmission in any drb
@@ -433,19 +471,25 @@ uint32_t req_new_ulsch(module_id_t module_idP);
 @param subframe subframe number
 @returns 0 for no SR, 1 for SR
 */
-uint32_t ue_get_SR(module_id_t module_idP, int CC_id,frame_t frameP, uint8_t eNB_id,rnti_t rnti,sub_frame_t subframe);
+uint32_t ue_get_SR(module_id_t module_idP, int CC_id, frame_t frameP,
+		   uint8_t eNB_id, rnti_t rnti, sub_frame_t subframe);
 
 uint8_t get_ue_weight(module_id_t module_idP, int CC_id, int UE_id);
 
 // UE functions
-void mac_out_of_sync_ind(module_id_t module_idP, frame_t frameP, uint16_t CH_index);
+void mac_out_of_sync_ind(module_id_t module_idP, frame_t frameP,
+			 uint16_t CH_index);
 
-void ue_decode_si(module_id_t module_idP, int CC_id,frame_t frame, uint8_t CH_index, void *pdu, uint16_t len);
+void ue_decode_si(module_id_t module_idP, int CC_id, frame_t frame,
+		  uint8_t CH_index, void *pdu, uint16_t len);
 
-void ue_decode_p(module_id_t module_idP, int CC_id,frame_t frame, uint8_t CH_index, void *pdu, uint16_t len);
+void ue_decode_p(module_id_t module_idP, int CC_id, frame_t frame,
+		 uint8_t CH_index, void *pdu, uint16_t len);
 
 
-void ue_send_sdu(module_id_t module_idP, uint8_t CC_id,frame_t frame, sub_frame_t subframe, uint8_t *sdu,uint16_t sdu_len,uint8_t CH_index);
+void ue_send_sdu(module_id_t module_idP, uint8_t CC_id, frame_t frame,
+		 sub_frame_t subframe, uint8_t * sdu, uint16_t sdu_len,
+		 uint8_t CH_index);
 
 
 #if defined(Rel10) || defined(Rel14)
@@ -457,7 +501,9 @@ void ue_send_sdu(module_id_t module_idP, uint8_t CC_id,frame_t frame, sub_frame_
 @param eNB_index Index of attached eNB
 @param sync_area the index of MBSFN sync area
 */
-void ue_send_mch_sdu(module_id_t module_idP,uint8_t CC_id, frame_t frameP,uint8_t *sdu,uint16_t sdu_len,uint8_t eNB_index,uint8_t sync_area) ;
+void ue_send_mch_sdu(module_id_t module_idP, uint8_t CC_id, frame_t frameP,
+		     uint8_t * sdu, uint16_t sdu_len, uint8_t eNB_index,
+		     uint8_t sync_area);
 
 /*\brief Function to check if UE PHY needs to decode MCH for MAC.
 @param Mod_id Index of protocol instance
@@ -467,7 +513,9 @@ void ue_send_mch_sdu(module_id_t module_idP,uint8_t CC_id, frame_t frameP,uint8_
 @param[out] sync_area return the sync area
 @param[out] mcch_active flag indicating whether this MCCH is active in this SF
 */
-int ue_query_mch(uint8_t Mod_id,uint8_t CC_id, uint32_t frame,sub_frame_t subframe, uint8_t eNB_index, uint8_t *sync_area, uint8_t *mcch_active);
+int ue_query_mch(uint8_t Mod_id, uint8_t CC_id, uint32_t frame,
+		 sub_frame_t subframe, uint8_t eNB_index,
+		 uint8_t * sync_area, uint8_t * mcch_active);
 
 #endif
 
@@ -478,7 +526,10 @@ int ue_query_mch(uint8_t Mod_id,uint8_t CC_id, uint32_t frame,sub_frame_t subfra
 @param subframe subframe number
 @returns 0 for no SR, 1 for SR
 */
-void ue_get_sdu(module_id_t module_idP, int CC_id,frame_t frameP, sub_frame_t subframe, uint8_t eNB_index,uint8_t *ulsch_buffer,uint16_t buflen,uint8_t *access_mode);
+void ue_get_sdu(module_id_t module_idP, int CC_id, frame_t frameP,
+		sub_frame_t subframe, uint8_t eNB_index,
+		uint8_t * ulsch_buffer, uint16_t buflen,
+		uint8_t * access_mode);
 
 /* \brief Function called by PHY to retrieve information to be transmitted using the RA procedure.  If the UE is not in PUSCH mode for a particular eNB index, this is assumed to be an Msg3 and MAC attempts to retrieves the CCCH message from RRC. If the UE is in PUSCH mode for a particular eNB index and PUCCH format 0 (Scheduling Request) is not activated, the MAC may use this resource for random-access to transmit a BSR along with the C-RNTI control element (see 5.1.4 from 36.321)
 @param Mod_id Index of UE instance
@@ -486,7 +537,9 @@ void ue_get_sdu(module_id_t module_idP, int CC_id,frame_t frameP, sub_frame_t su
 @param New_Msg3 Flag to indicate this call is for a new Msg3
 @param subframe Index of subframe for PRACH transmission (0 ... 9)
 @returns A pointer to a PRACH_RESOURCES_t */
-PRACH_RESOURCES_t *ue_get_rach(module_id_t module_idP,int CC_id,frame_t frameP,uint8_t new_Msg3,sub_frame_t subframe);
+PRACH_RESOURCES_t *ue_get_rach(module_id_t module_idP, int CC_id,
+			       frame_t frameP, uint8_t new_Msg3,
+			       sub_frame_t subframe);
 
 /* \brief Function called by PHY to process the received RAR.  It checks that the preamble matches what was sent by the eNB and provides the timing advance and t-CRNTI.
 @param Mod_id Index of UE instance
@@ -501,16 +554,14 @@ random-access procedure
 @returns timing advance or 0xffff if preamble doesn't match
 */
 uint16_t
-ue_process_rar(
-  const module_id_t module_idP,
-  const int CC_id,
-  const frame_t frameP,
-  const rnti_t ra_rnti,
-  uint8_t * const dlsch_buffer,
-  rnti_t * const t_crnti,
-  const uint8_t preamble_index,
-  uint8_t* selected_rar_buffer
-);
+ue_process_rar(const module_id_t module_idP,
+	       const int CC_id,
+	       const frame_t frameP,
+	       const rnti_t ra_rnti,
+	       uint8_t * const dlsch_buffer,
+	       rnti_t * const t_crnti,
+	       const uint8_t preamble_index,
+	       uint8_t * selected_rar_buffer);
 
 
 /* \brief Generate header for UL-SCH.  This function parses the desired control elements and sdus and generates the header as described
@@ -529,17 +580,17 @@ in the ULSCH buffer.
 @param post_padding Number of bytes for padding at the end of MAC PDU
 @returns Number of bytes used for header
 */
-unsigned char generate_ulsch_header(uint8_t *mac_header,
-                                    uint8_t num_sdus,
-                                    uint8_t short_padding,
-                                    uint16_t *sdu_lengths,
-                                    uint8_t *sdu_lcids,
-                                    POWER_HEADROOM_CMD *power_headroom,
-                                    uint16_t *crnti,
-                                    BSR_SHORT *truncated_bsr,
-                                    BSR_SHORT *short_bsr,
-                                    BSR_LONG *long_bsr,
-                                    unsigned short post_padding);
+unsigned char generate_ulsch_header(uint8_t * mac_header,
+				    uint8_t num_sdus,
+				    uint8_t short_padding,
+				    uint16_t * sdu_lengths,
+				    uint8_t * sdu_lcids,
+				    POWER_HEADROOM_CMD * power_headroom,
+				    uint16_t * crnti,
+				    BSR_SHORT * truncated_bsr,
+				    BSR_SHORT * short_bsr,
+				    BSR_LONG * long_bsr,
+				    unsigned short post_padding);
 
 /* \brief Parse header for UL-SCH.  This function parses the received UL-SCH header as described
 in 36-321 MAC layer specifications.  It returns the number of bytes used for the header to be used as an offset for the payload
@@ -552,40 +603,46 @@ in the ULSCH buffer.
 @param rx_lengths Pointer to array of SDU lengths
 @returns Pointer to payload following header
 */
-uint8_t *parse_ulsch_header(uint8_t *mac_header,
-                            uint8_t *num_ce,
-                            uint8_t *num_sdu,
-                            uint8_t *rx_ces,
-                            uint8_t *rx_lcids,
-                            uint16_t *rx_lengths,
-                            uint16_t tx_lenght);
+uint8_t *parse_ulsch_header(uint8_t * mac_header,
+			    uint8_t * num_ce,
+			    uint8_t * num_sdu,
+			    uint8_t * rx_ces,
+			    uint8_t * rx_lcids,
+			    uint16_t * rx_lengths, uint16_t tx_lenght);
 
 int to_prb(int);
 int to_rbg(int);
-int l2_init(LTE_DL_FRAME_PARMS *frame_parms,int eMBMS_active, char *uecap_xer, uint8_t cba_group_active, uint8_t HO_active);
+int l2_init(LTE_DL_FRAME_PARMS * frame_parms, int eMBMS_active,
+	    char *uecap_xer, uint8_t cba_group_active, uint8_t HO_active);
 int mac_init(void);
-int add_new_ue(module_id_t Mod_id, int CC_id, rnti_t rnti,int harq_pid
-               #ifdef Rel14
-                 ,uint8_t rach_resource_type
-               #endif
-               );
+int add_new_ue(module_id_t Mod_id, int CC_id, rnti_t rnti, int harq_pid
+#ifdef Rel14
+	       , uint8_t rach_resource_type
+#endif
+    );
 int rrc_mac_remove_ue(module_id_t Mod_id, rnti_t rntiP);
 
 
-int maxround(module_id_t Mod_id,uint16_t rnti,int frame,sub_frame_t subframe,uint8_t ul_flag);
-void swap_UEs(UE_list_t *listP,int nodeiP, int nodejP, int ul_flag);
-int prev(UE_list_t *listP, int nodeP, int ul_flag);
-void dump_ue_list(UE_list_t *listP, int ul_flag);
-int UE_num_active_CC(UE_list_t *listP,int ue_idP);
-int UE_PCCID(module_id_t mod_idP,int ue_idP);
+int maxround(module_id_t Mod_id, uint16_t rnti, int frame,
+	     sub_frame_t subframe, uint8_t ul_flag);
+void swap_UEs(UE_list_t * listP, int nodeiP, int nodejP, int ul_flag);
+int prev(UE_list_t * listP, int nodeP, int ul_flag);
+void dump_ue_list(UE_list_t * listP, int ul_flag);
+int UE_num_active_CC(UE_list_t * listP, int ue_idP);
+int UE_PCCID(module_id_t mod_idP, int ue_idP);
 rnti_t UE_RNTI(module_id_t mod_idP, int ue_idP);
 
 
-void ulsch_scheduler_pre_processor(module_id_t module_idP, int frameP, sub_frame_t subframeP, uint16_t *first_rb);
-void store_ulsch_buffer(module_id_t module_idP, int frameP, sub_frame_t subframeP);
-void sort_ue_ul (module_id_t module_idP,int frameP, sub_frame_t subframeP);
-void assign_max_mcs_min_rb(module_id_t module_idP,int frameP, sub_frame_t subframeP,uint16_t *first_rb);
-void adjust_bsr_info(int buffer_occupancy, uint16_t TBS, UE_TEMPLATE *UE_template);
+void ulsch_scheduler_pre_processor(module_id_t module_idP, int frameP,
+				   sub_frame_t subframeP,
+				   uint16_t * first_rb);
+void store_ulsch_buffer(module_id_t module_idP, int frameP,
+			sub_frame_t subframeP);
+void sort_ue_ul(module_id_t module_idP, int frameP, sub_frame_t subframeP);
+void assign_max_mcs_min_rb(module_id_t module_idP, int frameP,
+			   sub_frame_t subframeP, uint16_t * first_rb);
+void adjust_bsr_info(int buffer_occupancy, uint16_t TBS,
+		     UE_TEMPLATE * UE_template);
 int phy_stats_exist(module_id_t Mod_id, int rnti);
 
 /*! \fn  UE_L2_state_t ue_scheduler(const module_id_t module_idP,const frame_t frameP, const sub_frame_t subframe, const lte_subframe_t direction,const uint8_t eNB_index)
@@ -599,15 +656,13 @@ int phy_stats_exist(module_id_t Mod_id, int rnti);
 \param[in] eNB_index  instance of eNB
 @returns L2 state (CONNETION_OK or CONNECTION_LOST or PHY_RESYNCH)
 */
-UE_L2_STATE_t ue_scheduler(
-  const module_id_t module_idP,
-  const frame_t rxFrameP,
-  const sub_frame_t rxSubframe,
-  const frame_t txFrameP,
-  const sub_frame_t txSubframe,
-  const lte_subframe_t direction,
-  const uint8_t eNB_index,
-  const int CC_id);
+UE_L2_STATE_t ue_scheduler(const module_id_t module_idP,
+			   const frame_t rxFrameP,
+			   const sub_frame_t rxSubframe,
+			   const frame_t txFrameP,
+			   const sub_frame_t txSubframe,
+			   const lte_subframe_t direction,
+			   const uint8_t eNB_index, const int CC_id);
 
 /*! \fn  int cba_access(module_id_t module_idP,frame_t frameP,sub_frame_t subframe, uint8_t eNB_index,uint16_t buflen);
 \brief determine whether to use cba resource to transmit or not
@@ -617,7 +672,8 @@ UE_L2_STATE_t ue_scheduler(
 \param[in] eNB_index instance of eNB
 \param[out] access(1) or postpone (0)
 */
-int cba_access(module_id_t module_idP,frame_t frameP,sub_frame_t subframe, uint8_t eNB_index,uint16_t buflen);
+int cba_access(module_id_t module_idP, frame_t frameP,
+	       sub_frame_t subframe, uint8_t eNB_index, uint16_t buflen);
 
 /*! \fn  BSR_SHORT *  get_bsr_short(module_id_t module_idP, uint8_t bsr_len)
 \brief get short bsr level
@@ -633,14 +689,15 @@ BSR_SHORT *get_bsr_short(module_id_t module_idP, uint8_t bsr_len);
 \param[in] bsr_len indicator for no, short, or long bsr
 \param[out] bsr_l pointer to long bsr
 */
-BSR_LONG * get_bsr_long(module_id_t module_idP, uint8_t bsr_len);
+BSR_LONG *get_bsr_long(module_id_t module_idP, uint8_t bsr_len);
 
 /*! \fn  boolean_t update_bsr(module_id_t module_idP, frame_t frameP,sub_frame_t subframeP)
    \brief get the rlc stats and update the bsr level for each lcid
 \param[in] Mod_id instance of the UE
 \param[in] frame Frame index
 */
-boolean_t update_bsr(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP,eNB_index_t eNB_index);
+boolean_t update_bsr(module_id_t module_idP, frame_t frameP,
+		     sub_frame_t subframeP, eNB_index_t eNB_index);
 
 /*! \fn  locate_BsrIndexByBufferSize (int *table, int size, int value)
    \brief locate the BSR level in the table as defined in 36.321. This function requires that he values in table to be monotonic, either increasing or decreasing. The returned value is not less than 0, nor greater than n-1, where n is the size of table.
@@ -649,7 +706,8 @@ boolean_t update_bsr(module_id_t module_idP, frame_t frameP, sub_frame_t subfram
 \param[in] value Value of the buffer
 \return the index in the BSR_LEVEL table
 */
-uint8_t locate_BsrIndexByBufferSize (const uint32_t *table, int size, int value);
+uint8_t locate_BsrIndexByBufferSize(const uint32_t * table, int size,
+				    int value);
 
 
 /*! \fn  int get_sf_periodicBSRTimer(uint8_t periodicBSR_Timer)
@@ -700,7 +758,8 @@ int get_db_dl_PathlossChange(uint8_t dl_PathlossChange);
 \param[in] CC_id Component Carrier Index
 \return phr mapping
 */
-uint8_t get_phr_mapping (module_id_t module_idP, int CC_id, uint8_t eNB_index);
+uint8_t get_phr_mapping(module_id_t module_idP, int CC_id,
+			uint8_t eNB_index);
 
 /*! \fn  void update_phr (module_id_t module_idP)
    \brief update/reset the phr timers
@@ -708,13 +767,14 @@ uint8_t get_phr_mapping (module_id_t module_idP, int CC_id, uint8_t eNB_index);
 \param[in] CC_id Component carrier index
 \return void
 */
-void update_phr (module_id_t module_idP,int CC_id);
+void update_phr(module_id_t module_idP, int CC_id);
 
 /*! \brief Function to indicate Msg3 transmission/retransmission which initiates/reset Contention Resolution Timer
 \param[in] Mod_id Instance index of UE
 \param[in] eNB_id Index of eNB
 */
-void Msg3_tx(module_id_t module_idP,uint8_t CC_id,frame_t frameP,uint8_t eNB_id);
+void Msg3_tx(module_id_t module_idP, uint8_t CC_id, frame_t frameP,
+	     uint8_t eNB_id);
 
 
 /*! \brief Function to indicate the transmission of msg1/rach
@@ -722,12 +782,12 @@ void Msg3_tx(module_id_t module_idP,uint8_t CC_id,frame_t frameP,uint8_t eNB_id)
 \param[in] eNB_id Index of eNB
 */
 
-void Msg1_tx(module_id_t module_idP,uint8_t CC_id,frame_t frameP, uint8_t eNB_id);
+void Msg1_tx(module_id_t module_idP, uint8_t CC_id, frame_t frameP,
+	     uint8_t eNB_id);
 
-void dl_phy_sync_success(module_id_t   module_idP,
-                         frame_t       frameP,
-                         unsigned char eNB_index,
-                         uint8_t first_sync);
+void dl_phy_sync_success(module_id_t module_idP,
+			 frame_t frameP,
+			 unsigned char eNB_index, uint8_t first_sync);
 
 int dump_eNB_l2_stats(char *buffer, int length);
 
@@ -744,9 +804,11 @@ void add_common_dci(DCI_PDU *DCI_pdu,
                     uint8_t ra_flag);
 */
 
-uint32_t allocate_prbs_sub(int nb_rb, int N_RB_DL, int N_RBG, uint8_t *rballoc);
+uint32_t allocate_prbs_sub(int nb_rb, int N_RB_DL, int N_RBG,
+			   uint8_t * rballoc);
 
-void update_ul_dci(module_id_t module_idP,uint8_t CC_id,rnti_t rnti,uint8_t dai);
+void update_ul_dci(module_id_t module_idP, uint8_t CC_id, rnti_t rnti,
+		   uint8_t dai);
 
 int get_bw_index(module_id_t module_id, uint8_t CC_id);
 
@@ -767,14 +829,14 @@ in the DLSCH buffer.
 @returns Number of bytes used for header
 */
 unsigned char generate_dlsch_header(unsigned char *mac_header,
-                                    unsigned char num_sdus,
-                                    unsigned short *sdu_lengths,
-                                    unsigned char *sdu_lcids,
-                                    unsigned char drx_cmd,
-                                    unsigned short timing_advance_cmd,
-                                    unsigned char *ue_cont_res_id,
-                                    unsigned char short_padding,
-                                    unsigned short post_padding);
+				    unsigned char num_sdus,
+				    unsigned short *sdu_lengths,
+				    unsigned char *sdu_lcids,
+				    unsigned char drx_cmd,
+				    unsigned short timing_advance_cmd,
+				    unsigned char *ue_cont_res_id,
+				    unsigned char short_padding,
+				    unsigned short post_padding);
 
 /** \brief RRC eNB Configuration primitive for PHY/MAC.  Allows configuration of PHY/MAC resources based on System Information (SI), RRCConnectionSetup and RRCConnectionReconfiguration messages.
 @param Mod_id Instance ID of eNB
@@ -798,51 +860,55 @@ unsigned char generate_dlsch_header(unsigned char *mac_header,
 @param sib1_ext_r13 SI Scheduling information for SI-BR UEs         
 */
 
-int rrc_mac_config_req_eNB(module_id_t                             module_idP,
-			   int                                     CC_id,
-			   int                                     physCellId,
-			   int                                     p_eNB,
-			   int                                     Ncp,
-			   int                                     eutra_band,
-			   uint32_t                                dl_CarrierFreq,
+int rrc_mac_config_req_eNB(module_id_t module_idP,
+			   int CC_id,
+			   int physCellId,
+			   int p_eNB,
+			   int Ncp,
+			   int eutra_band, uint32_t dl_CarrierFreq,
 #ifdef Rel14
-                           int                                     pbch_repetition,
+			   int pbch_repetition,
 #endif
-			   rnti_t                                  rntiP,
-			   BCCH_BCH_Message_t                      *mib,
- 			   RadioResourceConfigCommonSIB_t          *radioResourceConfigCommon,
+			   rnti_t rntiP,
+			   BCCH_BCH_Message_t * mib,
+			   RadioResourceConfigCommonSIB_t *
+			   radioResourceConfigCommon,
 #ifdef Rel14
- 			   RadioResourceConfigCommonSIB_t          *radioResourceConfigCommon_BR,
+			   RadioResourceConfigCommonSIB_t *
+			   radioResourceConfigCommon_BR,
 #endif
-			   struct PhysicalConfigDedicated          *physicalConfigDedicated,
+			   struct PhysicalConfigDedicated
+			   *physicalConfigDedicated,
 #if defined(Rel10) || defined(Rel14)
-			   SCellToAddMod_r10_t                     *sCellToAddMod_r10,
+			   SCellToAddMod_r10_t * sCellToAddMod_r10,
 			   //struct PhysicalConfigDedicatedSCell_r10 *physicalConfigDedicatedSCell_r10,
 #endif
-			   MeasObjectToAddMod_t                    **measObj,
-			   MAC_MainConfig_t                        *mac_MainConfig,
-			   long                                    logicalChannelIdentity,
-			   LogicalChannelConfig_t                  *logicalChannelConfig,
-			   MeasGapConfig_t                         *measGapConfig,
-			   TDD_Config_t                            *tdd_Config,
-			   MobilityControlInfo_t                   *mobilityControlInfo,
-			   SchedulingInfoList_t                    *schedulingInfoList,
-			   uint32_t                                ul_CarrierFreq,
-			   long                                    *ul_Bandwidth,
-			   AdditionalSpectrumEmission_t            *additionalSpectrumEmission,
-			   struct MBSFN_SubframeConfigList         *mbsfn_SubframeConfigList
+			   MeasObjectToAddMod_t ** measObj,
+			   MAC_MainConfig_t * mac_MainConfig,
+			   long logicalChannelIdentity,
+			   LogicalChannelConfig_t * logicalChannelConfig,
+			   MeasGapConfig_t * measGapConfig,
+			   TDD_Config_t * tdd_Config,
+			   MobilityControlInfo_t * mobilityControlInfo,
+			   SchedulingInfoList_t * schedulingInfoList,
+			   uint32_t ul_CarrierFreq,
+			   long *ul_Bandwidth,
+			   AdditionalSpectrumEmission_t *
+			   additionalSpectrumEmission,
+			   struct MBSFN_SubframeConfigList
+			   *mbsfn_SubframeConfigList
 #if defined(Rel10) || defined(Rel14)
 			   ,
-			   uint8_t                                 MBMS_Flag,
-			   MBSFN_AreaInfoList_r9_t                 *mbsfn_AreaInfoList,
-			   PMCH_InfoList_r9_t                      *pmch_InfoList
-			   
+			   uint8_t MBMS_Flag,
+			   MBSFN_AreaInfoList_r9_t * mbsfn_AreaInfoList,
+			   PMCH_InfoList_r9_t * pmch_InfoList
 #endif
 #ifdef Rel14
 			   ,
-			   SystemInformationBlockType1_v1310_IEs_t *sib1_ext_r13          
+			   SystemInformationBlockType1_v1310_IEs_t *
+			   sib1_ext_r13
 #endif
-			   );
+    );
 
 /** \brief RRC eNB Configuration primitive for PHY/MAC.  Allows configuration of PHY/MAC resources based on System Information (SI), RRCConnectionSetup and RRCConnectionReconfiguration messages.
 @param Mod_id Instance ID of ue
@@ -864,132 +930,154 @@ int rrc_mac_config_req_eNB(module_id_t                             module_idP,
 @param mbsfn_AreaInfoList pointer to MBSFN Area Info list from SIB13
 @param pmch_InfoList pointer to PMCH_InfoList from MBSFNAreaConfiguration Message (MCCH Message)
 */
-int rrc_mac_config_req_ue(module_id_t     module_idP,
-			  int             CC_id,
-			  uint8_t         eNB_index,
-			  RadioResourceConfigCommonSIB_t *radioResourceConfigCommon,
-			  struct PhysicalConfigDedicated *physicalConfigDedicated,
+int rrc_mac_config_req_ue(module_id_t module_idP,
+			  int CC_id,
+			  uint8_t eNB_index,
+			  RadioResourceConfigCommonSIB_t *
+			  radioResourceConfigCommon,
+			  struct PhysicalConfigDedicated
+			  *physicalConfigDedicated,
 #if defined(Rel10) || defined(Rel14)
-			  SCellToAddMod_r10_t *sCellToAddMod_r10,
+			  SCellToAddMod_r10_t * sCellToAddMod_r10,
 			  //struct PhysicalConfigDedicatedSCell_r10 *physicalConfigDedicatedSCell_r10,
 #endif
-			  MeasObjectToAddMod_t **measObj,
-			  MAC_MainConfig_t *mac_MainConfig,
+			  MeasObjectToAddMod_t ** measObj,
+			  MAC_MainConfig_t * mac_MainConfig,
 			  long logicalChannelIdentity,
-			  LogicalChannelConfig_t *logicalChannelConfig,
-			  MeasGapConfig_t *measGapConfig,
-			  TDD_Config_t *tdd_Config,
-			  MobilityControlInfo_t *mobilityControlInfo,
-			  uint8_t *SIwindowsize,
-			  uint16_t *SIperiod,
-			  ARFCN_ValueEUTRA_t *ul_CarrierFreq,
+			  LogicalChannelConfig_t * logicalChannelConfig,
+			  MeasGapConfig_t * measGapConfig,
+			  TDD_Config_t * tdd_Config,
+			  MobilityControlInfo_t * mobilityControlInfo,
+			  uint8_t * SIwindowsize,
+			  uint16_t * SIperiod,
+			  ARFCN_ValueEUTRA_t * ul_CarrierFreq,
 			  long *ul_Bandwidth,
-			  AdditionalSpectrumEmission_t *additionalSpectrumEmission,
-			  struct MBSFN_SubframeConfigList *mbsfn_SubframeConfigList
+			  AdditionalSpectrumEmission_t *
+			  additionalSpectrumEmission,
+			  struct MBSFN_SubframeConfigList
+			  *mbsfn_SubframeConfigList
 #if defined(Rel10) || defined(Rel14)
 			  ,
 			  uint8_t MBMS_Flag,
-			  MBSFN_AreaInfoList_r9_t *mbsfn_AreaInfoList,
-			  PMCH_InfoList_r9_t *pmch_InfoList
-			  
+			  MBSFN_AreaInfoList_r9_t * mbsfn_AreaInfoList,
+			  PMCH_InfoList_r9_t * pmch_InfoList
 #endif
 #ifdef CBA
 			  ,
-			  uint8_t num_active_cba_groups,
-			  uint16_t cba_rnti
+			  uint8_t num_active_cba_groups, uint16_t cba_rnti
 #endif
-			  );
+    );
 
-uint16_t getRIV(uint16_t N_RB_DL,uint16_t RBstart,uint16_t Lcrbs);
+uint16_t getRIV(uint16_t N_RB_DL, uint16_t RBstart, uint16_t Lcrbs);
 
 int get_subbandsize(uint8_t dl_bandwidth);
 
 
-void get_Msg3allocret(COMMON_channels_t *cc,
+void get_Msg3allocret(COMMON_channels_t * cc,
 		      sub_frame_t current_subframe,
 		      frame_t current_frame,
-		      frame_t *frame,
-		      sub_frame_t *subframe);
+		      frame_t * frame, sub_frame_t * subframe);
 
-void get_Msg3alloc(COMMON_channels_t *cc,
+void get_Msg3alloc(COMMON_channels_t * cc,
 		   sub_frame_t current_subframe,
 		   frame_t current_frame,
-		   frame_t *frame,
-		   sub_frame_t *subframe);
+		   frame_t * frame, sub_frame_t * subframe);
 
-uint16_t mac_computeRIV(uint16_t N_RB_DL,uint16_t RBstart,uint16_t Lcrbs);
+uint16_t mac_computeRIV(uint16_t N_RB_DL, uint16_t RBstart,
+			uint16_t Lcrbs);
 
-int get_phich_resource_times6(COMMON_channels_t *cc);
+int get_phich_resource_times6(COMMON_channels_t * cc);
 
 int to_rbg(int dl_Bandwidth);
 
 int to_prb(int dl_Bandwidth);
 
-uint8_t get_Msg3harqpid(COMMON_channels_t *cc,
-			frame_t frame,
-			sub_frame_t current_subframe);
+uint8_t get_Msg3harqpid(COMMON_channels_t * cc,
+			frame_t frame, sub_frame_t current_subframe);
 
-uint32_t pdcchalloc2ulframe(COMMON_channels_t *ccP,uint32_t frame, uint8_t n);
+uint32_t pdcchalloc2ulframe(COMMON_channels_t * ccP, uint32_t frame,
+			    uint8_t n);
 
-uint8_t pdcchalloc2ulsubframe(COMMON_channels_t *ccP,uint8_t n);
+uint8_t pdcchalloc2ulsubframe(COMMON_channels_t * ccP, uint8_t n);
 
-int is_UL_sf(COMMON_channels_t *ccP,sub_frame_t subframeP);
+int is_UL_sf(COMMON_channels_t * ccP, sub_frame_t subframeP);
 
 uint8_t getQm(uint8_t mcs);
 
-uint8_t subframe2harqpid(COMMON_channels_t *cc,frame_t frame,sub_frame_t subframe);
-
-void get_srs_pos(COMMON_channels_t *cc,uint16_t isrs,uint16_t *psrsPeriodicity,uint16_t *psrsOffset);
-
-void get_csi_params(COMMON_channels_t *cc,struct CQI_ReportPeriodic *cqi_PMI_ConfigIndex,uint16_t *Npd,uint16_t *N_OFFSET_CQI,int *H);
-
-uint8_t get_rel8_dl_cqi_pmi_size(UE_sched_ctrl *sched_ctl,int CC_idP,COMMON_channels_t *cc,uint8_t tmode, struct CQI_ReportPeriodic *cqi_ReportPeriodic);
-
-uint8_t get_dl_cqi_pmi_size_pusch(COMMON_channels_t *cc,uint8_t tmode, uint8_t ri, CQI_ReportModeAperiodic_t *cqi_ReportModeAperiodic);
-void extract_pucch_csi(module_id_t mod_idP,int CC_idP,int UE_id, frame_t frameP,sub_frame_t subframeP, uint8_t *pdu, uint8_t length);
-
-void extract_pusch_csi(module_id_t mod_idP,int CC_idP,int UE_id, frame_t frameP,sub_frame_t subframeP,uint8_t *pdu, uint8_t length);
-
-uint16_t fill_nfapi_tx_req(nfapi_tx_request_body_t *tx_req_body,uint16_t absSF,uint16_t pdu_length, uint16_t pdu_index, uint8_t *pdu );
-
-void fill_nfapi_ulsch_config_request_rel8(nfapi_ul_config_request_pdu_t  *ul_config_pdu,
-					  uint8_t                        cqi_req,
-					  COMMON_channels_t              *cc,
-					  struct PhysicalConfigDedicated *physicalConfigDedicated,
-					  uint8_t                        tmode,
-					  uint32_t                       handle,
-					  uint16_t                       rnti,
-					  uint8_t                        resource_block_start,
-					  uint8_t                        number_of_resource_blocks,
-					  uint8_t                        mcs,
-					  uint8_t                        cyclic_shift_2_for_drms,
-					  uint8_t                        frequency_hopping_enabled_flag,
-					  uint8_t                        frequency_hopping_bits,
-					  uint8_t                        new_data_indication,
-					  uint8_t                        redundancy_version,
-					  uint8_t                        harq_process_number,
-					  uint8_t                        ul_tx_mode,
-					  uint8_t                        current_tx_nb,
-					  uint8_t                        n_srs,
-					  uint16_t                       size
-					  );
+uint8_t subframe2harqpid(COMMON_channels_t * cc, frame_t frame,
+			 sub_frame_t subframe);
+
+void get_srs_pos(COMMON_channels_t * cc, uint16_t isrs,
+		 uint16_t * psrsPeriodicity, uint16_t * psrsOffset);
+
+void get_csi_params(COMMON_channels_t * cc,
+		    struct CQI_ReportPeriodic *cqi_PMI_ConfigIndex,
+		    uint16_t * Npd, uint16_t * N_OFFSET_CQI, int *H);
+
+uint8_t get_rel8_dl_cqi_pmi_size(UE_sched_ctrl * sched_ctl, int CC_idP,
+				 COMMON_channels_t * cc, uint8_t tmode,
+				 struct CQI_ReportPeriodic
+				 *cqi_ReportPeriodic);
+
+uint8_t get_dl_cqi_pmi_size_pusch(COMMON_channels_t * cc, uint8_t tmode,
+				  uint8_t ri,
+				  CQI_ReportModeAperiodic_t *
+				  cqi_ReportModeAperiodic);
+void extract_pucch_csi(module_id_t mod_idP, int CC_idP, int UE_id,
+		       frame_t frameP, sub_frame_t subframeP,
+		       uint8_t * pdu, uint8_t length);
+
+void extract_pusch_csi(module_id_t mod_idP, int CC_idP, int UE_id,
+		       frame_t frameP, sub_frame_t subframeP,
+		       uint8_t * pdu, uint8_t length);
+
+uint16_t fill_nfapi_tx_req(nfapi_tx_request_body_t * tx_req_body,
+			   uint16_t absSF, uint16_t pdu_length,
+			   uint16_t pdu_index, uint8_t * pdu);
+
+void fill_nfapi_ulsch_config_request_rel8(nfapi_ul_config_request_pdu_t *
+					  ul_config_pdu, uint8_t cqi_req,
+					  COMMON_channels_t * cc,
+					  struct PhysicalConfigDedicated
+					  *physicalConfigDedicated,
+					  uint8_t tmode, uint32_t handle,
+					  uint16_t rnti,
+					  uint8_t resource_block_start,
+					  uint8_t
+					  number_of_resource_blocks,
+					  uint8_t mcs,
+					  uint8_t cyclic_shift_2_for_drms,
+					  uint8_t
+					  frequency_hopping_enabled_flag,
+					  uint8_t frequency_hopping_bits,
+					  uint8_t new_data_indication,
+					  uint8_t redundancy_version,
+					  uint8_t harq_process_number,
+					  uint8_t ul_tx_mode,
+					  uint8_t current_tx_nb,
+					  uint8_t n_srs, uint16_t size);
 
 #ifdef Rel14
-void fill_nfapi_ulsch_config_request_emtc(nfapi_ul_config_request_pdu_t  *ul_config_pdu,
-					  uint8_t ue_type,
-					  uint16_t total_number_of_repetitions,
+void fill_nfapi_ulsch_config_request_emtc(nfapi_ul_config_request_pdu_t *
+					  ul_config_pdu, uint8_t ue_type,
+					  uint16_t
+					  total_number_of_repetitions,
 					  uint16_t repetition_number,
-					  uint16_t initial_transmission_sf_io);
+					  uint16_t
+					  initial_transmission_sf_io);
 #endif
 
-void program_dlsch_acknak(module_id_t module_idP, int CC_idP,int UE_idP, frame_t frameP, sub_frame_t subframeP,uint8_t cce_idx);
+void program_dlsch_acknak(module_id_t module_idP, int CC_idP, int UE_idP,
+			  frame_t frameP, sub_frame_t subframeP,
+			  uint8_t cce_idx);
 
-void fill_nfapi_dlsch_config(eNB_MAC_INST *eNB, nfapi_dl_config_request_body_t *dl_req,
-			     uint16_t length,
-			     uint16_t pdu_index,
+void fill_nfapi_dlsch_config(eNB_MAC_INST * eNB,
+			     nfapi_dl_config_request_body_t * dl_req,
+			     uint16_t length, uint16_t pdu_index,
 			     uint16_t rnti,
 			     uint8_t resource_allocation_type,
-			     uint8_t virtual_resource_block_assignment_flag,
+			     uint8_t
+			     virtual_resource_block_assignment_flag,
 			     uint16_t resource_block_coding,
 			     uint8_t modulation,
 			     uint8_t redundancy_version,
@@ -998,7 +1086,7 @@ void fill_nfapi_dlsch_config(eNB_MAC_INST *eNB, nfapi_dl_config_request_body_t *
 			     uint8_t transmission_scheme,
 			     uint8_t number_of_layers,
 			     uint8_t number_of_subbands,
-			     //			     uint8_t codebook_index,
+			     //                      uint8_t codebook_index,
 			     uint8_t ue_category_capacity,
 			     uint8_t pa,
 			     uint8_t delta_power_offset_index,
@@ -1006,28 +1094,27 @@ void fill_nfapi_dlsch_config(eNB_MAC_INST *eNB, nfapi_dl_config_request_body_t *
 			     uint8_t nprb,
 			     uint8_t transmission_mode,
 			     uint8_t num_bf_prb_per_subband,
-			     uint8_t num_bf_vector
-			     );
+			     uint8_t num_bf_vector);
 
 void fill_nfapi_harq_information(module_id_t module_idP,
 				 int CC_idP,
 				 uint16_t rntiP,
 				 uint16_t absSFP,
-				 nfapi_ul_config_harq_information *harq_information,
-				 uint8_t cce_idxP);
+				 nfapi_ul_config_harq_information *
+				 harq_information, uint8_t cce_idxP);
 
 void fill_nfapi_ulsch_harq_information(module_id_t module_idP,
 				       int CC_idP,
 				       uint16_t rntiP,
-				       nfapi_ul_config_ulsch_harq_information *harq_information);
+				       nfapi_ul_config_ulsch_harq_information
+				       * harq_information);
 
 uint16_t fill_nfapi_uci_acknak(module_id_t module_idP,
 			       int CC_idP,
 			       uint16_t rntiP,
-			       uint16_t absSFP,
-			       uint8_t cce_idxP);
+			       uint16_t absSFP, uint8_t cce_idxP);
 
-void fill_nfapi_dl_dci_1A(nfapi_dl_config_request_pdu_t   *dl_config_pdu,
+void fill_nfapi_dl_dci_1A(nfapi_dl_config_request_pdu_t * dl_config_pdu,
 			  uint8_t aggregation_level,
 			  uint16_t rnti,
 			  uint8_t rnti_type,
@@ -1035,34 +1122,40 @@ void fill_nfapi_dl_dci_1A(nfapi_dl_config_request_pdu_t   *dl_config_pdu,
 			  uint8_t tpc,
 			  uint16_t resource_block_coding,
 			  uint8_t mcs,
-			  uint8_t ndi,
-			  uint8_t rv,
-			  uint8_t vrb_flag);
+			  uint8_t ndi, uint8_t rv, uint8_t vrb_flag);
 
-nfapi_ul_config_request_pdu_t* has_ul_grant(module_id_t module_idP,int CC_idP,uint16_t subframeP,uint16_t rnti);
+nfapi_ul_config_request_pdu_t *has_ul_grant(module_id_t module_idP,
+					    int CC_idP, uint16_t subframeP,
+					    uint16_t rnti);
 
-uint8_t get_tmode(module_id_t module_idP,int CC_idP,int UE_idP);
+uint8_t get_tmode(module_id_t module_idP, int CC_idP, int UE_idP);
 
-uint8_t get_ul_req_index(module_id_t module_idP, int CC_idP, sub_frame_t subframeP);
+uint8_t get_ul_req_index(module_id_t module_idP, int CC_idP,
+			 sub_frame_t subframeP);
 
 #ifdef Rel14
 int get_numnarrowbandbits(long dl_Bandwidth);
 
-int mpdcch_sf_condition(eNB_MAC_INST *eNB,int CC_id, frame_t frameP,sub_frame_t subframeP,int rmax,MPDCCH_TYPES_t mpdcch_type,int UE_id);
+int mpdcch_sf_condition(eNB_MAC_INST * eNB, int CC_id, frame_t frameP,
+			sub_frame_t subframeP, int rmax,
+			MPDCCH_TYPES_t mpdcch_type, int UE_id);
 
 int get_numnarrowbands(long dl_Bandwidth);
 
-int narrowband_to_first_rb(COMMON_channels_t *cc, int nb_index);
+int narrowband_to_first_rb(COMMON_channels_t * cc, int nb_index);
 
 #endif
 
 int l2_init_eNB(void);
 
-void Msg1_transmitted(module_id_t module_idP,uint8_t CC_id,frame_t frameP, uint8_t eNB_id);
-void Msg3_transmitted(module_id_t module_idP,uint8_t CC_id,frame_t frameP, uint8_t eNB_id);
-uint32_t from_earfcn(int eutra_bandP,uint32_t dl_earfcn);
+void Msg1_transmitted(module_id_t module_idP, uint8_t CC_id,
+		      frame_t frameP, uint8_t eNB_id);
+void Msg3_transmitted(module_id_t module_idP, uint8_t CC_id,
+		      frame_t frameP, uint8_t eNB_id);
+uint32_t from_earfcn(int eutra_bandP, uint32_t dl_earfcn);
 int32_t get_uldl_offset(int eutra_bandP);
-int l2_init_ue(int eMBMS_active, char *uecap_xer,uint8_t cba_group_active, uint8_t HO_active);
+int l2_init_ue(int eMBMS_active, char *uecap_xer, uint8_t cba_group_active,
+	       uint8_t HO_active);
 
 #endif
 /** @}*/
diff --git a/openair2/LAYER2/MAC/ra_procedures.c b/openair2/LAYER2/MAC/ra_procedures.c
index f649acad1ea1fcc21bed1dad0e3f3e66c01d0d70..3f2df025c773db688fd255b1439d86dcb15e4478 100644
--- a/openair2/LAYER2/MAC/ra_procedures.c
+++ b/openair2/LAYER2/MAC/ra_procedures.c
@@ -47,482 +47,604 @@
 #include "SIMULATION/simulation_defs.h"
 #endif
 
-#include "SIMULATION/TOOLS/defs.h" // for taus
+#include "SIMULATION/TOOLS/defs.h"	// for taus
 
-int8_t get_DELTA_PREAMBLE(module_id_t module_idP,int CC_id)
+int8_t get_DELTA_PREAMBLE(module_id_t module_idP, int CC_id)
 {
 
-  AssertFatal(CC_id==0,
-	      "Transmission on secondary CCs is not supported yet\n");
-  uint8_t prachConfigIndex = UE_mac_inst[module_idP].radioResourceConfigCommon->prach_Config.prach_ConfigInfo.prach_ConfigIndex;
-  uint8_t preambleformat;
-
-  if (UE_mac_inst[module_idP].tdd_Config) { // TDD
-    if (prachConfigIndex < 20) {
-      preambleformat = 0;
-    } else if (prachConfigIndex < 30) {
-      preambleformat = 1;
-    } else if (prachConfigIndex < 40) {
-      preambleformat = 2;
-    } else if (prachConfigIndex < 48) {
-      preambleformat = 3;
-    } else {
-      preambleformat = 4;
+    AssertFatal(CC_id == 0,
+		"Transmission on secondary CCs is not supported yet\n");
+    uint8_t prachConfigIndex =
+	UE_mac_inst[module_idP].radioResourceConfigCommon->
+	prach_Config.prach_ConfigInfo.prach_ConfigIndex;
+    uint8_t preambleformat;
+
+    if (UE_mac_inst[module_idP].tdd_Config) {	// TDD
+	if (prachConfigIndex < 20) {
+	    preambleformat = 0;
+	} else if (prachConfigIndex < 30) {
+	    preambleformat = 1;
+	} else if (prachConfigIndex < 40) {
+	    preambleformat = 2;
+	} else if (prachConfigIndex < 48) {
+	    preambleformat = 3;
+	} else {
+	    preambleformat = 4;
+	}
+    } else {			// FDD
+	preambleformat = prachConfigIndex >> 2;
     }
-  } else { // FDD
-    preambleformat = prachConfigIndex>>2;
-  }
 
-  switch (preambleformat) {
-  case 0:
-  case 1:
-    return(0);
+    switch (preambleformat) {
+    case 0:
+    case 1:
+	return (0);
 
-  case 2:
-  case 3:
-    return(-3);
+    case 2:
+    case 3:
+	return (-3);
 
-  case 4:
-    return(8);
+    case 4:
+	return (8);
 
-  default:
-    AssertFatal(1==0,"[UE %d] ue_procedures.c: FATAL, Illegal preambleformat %d, prachConfigIndex %d\n",
-		module_idP,
-		preambleformat,prachConfigIndex);
-  }
+    default:
+	AssertFatal(1 == 0,
+		    "[UE %d] ue_procedures.c: FATAL, Illegal preambleformat %d, prachConfigIndex %d\n",
+		    module_idP, preambleformat, prachConfigIndex);
+    }
 
 }
 
 
 /// This routine implements Section 5.1.2 (UE Random Access Resource Selection) from 36.321
-void get_prach_resources(module_id_t module_idP,
-                         int CC_id,
-                         uint8_t eNB_index,
-                         uint8_t t_id,
-                         uint8_t first_Msg3,
-                         RACH_ConfigDedicated_t *rach_ConfigDedicated)
+void
+get_prach_resources(module_id_t module_idP,
+		    int CC_id,
+		    uint8_t eNB_index,
+		    uint8_t t_id,
+		    uint8_t first_Msg3,
+		    RACH_ConfigDedicated_t * rach_ConfigDedicated)
 {
 
-  uint8_t Msg3_size = UE_mac_inst[module_idP].RA_Msg3_size;
-  PRACH_RESOURCES_t *prach_resources = &UE_mac_inst[module_idP].RA_prach_resources;
-  RACH_ConfigCommon_t *rach_ConfigCommon = NULL;
-  uint8_t noGroupB = 0;
-  uint8_t f_id = 0,num_prach=0;
-  int numberOfRA_Preambles;
-  int messageSizeGroupA;
-  int sizeOfRA_PreamblesGroupA;
-  int messagePowerOffsetGroupB;
-  int PLThreshold;
-
-  AssertFatal(CC_id==0,
-	      "Transmission on secondary CCs is not supported yet\n");
-  AssertFatal(UE_mac_inst[module_idP].radioResourceConfigCommon!=NULL,
-	      "[UE %d] FATAL  radioResourceConfigCommon is NULL !!!\n",module_idP);
-
-  rach_ConfigCommon = &UE_mac_inst[module_idP].radioResourceConfigCommon->rach_ConfigCommon;
-  numberOfRA_Preambles = (1+rach_ConfigCommon->preambleInfo.numberOfRA_Preambles)<<2;  
-
-  if (rach_ConfigDedicated) {   // This is for network controlled Mobility, later
-    if (rach_ConfigDedicated->ra_PRACH_MaskIndex != 0) {
-      prach_resources->ra_PreambleIndex = rach_ConfigDedicated->ra_PreambleIndex;
-      prach_resources->ra_RACH_MaskIndex = rach_ConfigDedicated->ra_PRACH_MaskIndex;
-      return;
+    uint8_t Msg3_size = UE_mac_inst[module_idP].RA_Msg3_size;
+    PRACH_RESOURCES_t *prach_resources =
+	&UE_mac_inst[module_idP].RA_prach_resources;
+    RACH_ConfigCommon_t *rach_ConfigCommon = NULL;
+    uint8_t noGroupB = 0;
+    uint8_t f_id = 0, num_prach = 0;
+    int numberOfRA_Preambles;
+    int messageSizeGroupA;
+    int sizeOfRA_PreamblesGroupA;
+    int messagePowerOffsetGroupB;
+    int PLThreshold;
+
+    AssertFatal(CC_id == 0,
+		"Transmission on secondary CCs is not supported yet\n");
+    AssertFatal(UE_mac_inst[module_idP].radioResourceConfigCommon != NULL,
+		"[UE %d] FATAL  radioResourceConfigCommon is NULL !!!\n",
+		module_idP);
+
+    rach_ConfigCommon =
+	&UE_mac_inst[module_idP].radioResourceConfigCommon->
+	rach_ConfigCommon;
+    numberOfRA_Preambles =
+	(1 + rach_ConfigCommon->preambleInfo.numberOfRA_Preambles) << 2;
+
+    if (rach_ConfigDedicated) {	// This is for network controlled Mobility, later
+	if (rach_ConfigDedicated->ra_PRACH_MaskIndex != 0) {
+	    prach_resources->ra_PreambleIndex =
+		rach_ConfigDedicated->ra_PreambleIndex;
+	    prach_resources->ra_RACH_MaskIndex =
+		rach_ConfigDedicated->ra_PRACH_MaskIndex;
+	    return;
+	}
     }
-  }
 
-  /* TODO: gcc warns if this variable is not always set, let's put -1 for no more warning */
-  messageSizeGroupA = -1;
+    /* TODO: gcc warns if this variable is not always set, let's put -1 for no more warning */
+    messageSizeGroupA = -1;
 
-  if (!rach_ConfigCommon->preambleInfo.preamblesGroupAConfig) {
-    noGroupB = 1;
-  } else {
-    sizeOfRA_PreamblesGroupA = (rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA+1)<<2;
-    switch (rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->messageSizeGroupA) {
-    case 0:
-      messageSizeGroupA = 56;
-      break;
-    case 1:
-      messageSizeGroupA = 144;
-      break;
-    case 2:
-      messageSizeGroupA = 208;
-      break;
-    case 3:
-      messageSizeGroupA = 256;
-      break;
-    }
+    if (!rach_ConfigCommon->preambleInfo.preamblesGroupAConfig) {
+	noGroupB = 1;
+    } else {
+	sizeOfRA_PreamblesGroupA =
+	    (rach_ConfigCommon->preambleInfo.
+	     preamblesGroupAConfig->sizeOfRA_PreamblesGroupA + 1) << 2;
+	switch (rach_ConfigCommon->preambleInfo.
+		preamblesGroupAConfig->messageSizeGroupA) {
+	case 0:
+	    messageSizeGroupA = 56;
+	    break;
+	case 1:
+	    messageSizeGroupA = 144;
+	    break;
+	case 2:
+	    messageSizeGroupA = 208;
+	    break;
+	case 3:
+	    messageSizeGroupA = 256;
+	    break;
+	}
 
-    /* TODO: what value to use as default? */
-    messagePowerOffsetGroupB = -9999;
-    switch (rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->messagePowerOffsetGroupB) {
-    case 0:
-      messagePowerOffsetGroupB = -9999;
-      break;
-    case 1:
-      messagePowerOffsetGroupB = 0;
-      break;
-    case 2:
-      messagePowerOffsetGroupB = 5;
-      break;
-    case 3:
-      messagePowerOffsetGroupB = 8;
-      break;
-    case 4:
-      messagePowerOffsetGroupB = 10;
-      break;
-    case 5:
-      messagePowerOffsetGroupB = 12;
-      break;
-    case 6:
-      messagePowerOffsetGroupB = 15;
-      break;
-    case 7:
-      messagePowerOffsetGroupB = 18;
-      break;
-    }
+	/* TODO: what value to use as default? */
+	messagePowerOffsetGroupB = -9999;
+	switch (rach_ConfigCommon->preambleInfo.
+		preamblesGroupAConfig->messagePowerOffsetGroupB) {
+	case 0:
+	    messagePowerOffsetGroupB = -9999;
+	    break;
+	case 1:
+	    messagePowerOffsetGroupB = 0;
+	    break;
+	case 2:
+	    messagePowerOffsetGroupB = 5;
+	    break;
+	case 3:
+	    messagePowerOffsetGroupB = 8;
+	    break;
+	case 4:
+	    messagePowerOffsetGroupB = 10;
+	    break;
+	case 5:
+	    messagePowerOffsetGroupB = 12;
+	    break;
+	case 6:
+	    messagePowerOffsetGroupB = 15;
+	    break;
+	case 7:
+	    messagePowerOffsetGroupB = 18;
+	    break;
+	}
 
-    PLThreshold = 0 - get_DELTA_PREAMBLE(module_idP,CC_id) - get_Po_NOMINAL_PUSCH(module_idP,CC_id) - messagePowerOffsetGroupB;
-    // Note Pcmax is set to 0 here, we have to fix this
+	PLThreshold =
+	    0 - get_DELTA_PREAMBLE(module_idP,
+				   CC_id) -
+	    get_Po_NOMINAL_PUSCH(module_idP,
+				 CC_id) - messagePowerOffsetGroupB;
+	// Note Pcmax is set to 0 here, we have to fix this
 
-    if (sizeOfRA_PreamblesGroupA == numberOfRA_Preambles) {
-      noGroupB = 1;
-    }
-  }
-
-  if (first_Msg3 == 1) {
-    if (noGroupB == 1) {
-      // use Group A procedure
-      UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = (taus())%numberOfRA_Preambles;
-      UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
-      UE_mac_inst[module_idP].RA_usedGroupA = 1;
-    } else if ((Msg3_size <messageSizeGroupA) ||
-               (get_PL(module_idP,0,eNB_index) > PLThreshold)) {
-      // use Group A procedure
-      UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = (taus())%sizeOfRA_PreamblesGroupA;
-      UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
-      UE_mac_inst[module_idP].RA_usedGroupA = 1;
-    } else { // use Group B
-      UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = sizeOfRA_PreamblesGroupA +
-        (taus())%(numberOfRA_Preambles - sizeOfRA_PreamblesGroupA);
-      UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
-      UE_mac_inst[module_idP].RA_usedGroupA = 0;
+	if (sizeOfRA_PreamblesGroupA == numberOfRA_Preambles) {
+	    noGroupB = 1;
+	}
     }
 
-    UE_mac_inst[module_idP].RA_prach_resources.ra_PREAMBLE_RECEIVED_TARGET_POWER = get_Po_NOMINAL_PUSCH(module_idP,CC_id);
-  } else { // Msg3 is being retransmitted
-    if (UE_mac_inst[module_idP].RA_usedGroupA == 1) {
-      if (rach_ConfigCommon->preambleInfo.preamblesGroupAConfig) {
-        UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = (taus())%rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA;
-      } else {
-        UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = (taus())&0x3f;
-      }
+    if (first_Msg3 == 1) {
+	if (noGroupB == 1) {
+	    // use Group A procedure
+	    UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex =
+		(taus()) % numberOfRA_Preambles;
+	    UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex =
+		0;
+	    UE_mac_inst[module_idP].RA_usedGroupA = 1;
+	} else if ((Msg3_size < messageSizeGroupA) ||
+		   (get_PL(module_idP, 0, eNB_index) > PLThreshold)) {
+	    // use Group A procedure
+	    UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex =
+		(taus()) % sizeOfRA_PreamblesGroupA;
+	    UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex =
+		0;
+	    UE_mac_inst[module_idP].RA_usedGroupA = 1;
+	} else {		// use Group B
+	    UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex =
+		sizeOfRA_PreamblesGroupA +
+		(taus()) % (numberOfRA_Preambles -
+			    sizeOfRA_PreamblesGroupA);
+	    UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex =
+		0;
+	    UE_mac_inst[module_idP].RA_usedGroupA = 0;
+	}
 
-      UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
-    } else {
-      // FIXME rach_ConfigCommon->preambleInfo.preamblesGroupAConfig may be zero
-      UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  =
-        rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA +
-        (taus())%(rach_ConfigCommon->preambleInfo.numberOfRA_Preambles -
-                  rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA);
-      UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
+	UE_mac_inst[module_idP].
+	    RA_prach_resources.ra_PREAMBLE_RECEIVED_TARGET_POWER =
+	    get_Po_NOMINAL_PUSCH(module_idP, CC_id);
+    } else {			// Msg3 is being retransmitted
+	if (UE_mac_inst[module_idP].RA_usedGroupA == 1) {
+	    if (rach_ConfigCommon->preambleInfo.preamblesGroupAConfig) {
+		UE_mac_inst[module_idP].RA_prach_resources.
+		    ra_PreambleIndex =
+		    (taus()) %
+		    rach_ConfigCommon->preambleInfo.
+		    preamblesGroupAConfig->sizeOfRA_PreamblesGroupA;
+	    } else {
+		UE_mac_inst[module_idP].RA_prach_resources.
+		    ra_PreambleIndex = (taus()) & 0x3f;
+	    }
+
+	    UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex =
+		0;
+	} else {
+	    // FIXME rach_ConfigCommon->preambleInfo.preamblesGroupAConfig may be zero
+	    UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex =
+		rach_ConfigCommon->preambleInfo.
+		preamblesGroupAConfig->sizeOfRA_PreamblesGroupA +
+		(taus()) %
+		(rach_ConfigCommon->preambleInfo.numberOfRA_Preambles -
+		 rach_ConfigCommon->preambleInfo.
+		 preamblesGroupAConfig->sizeOfRA_PreamblesGroupA);
+	    UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex =
+		0;
+	}
     }
-  }
-
-  // choose random PRACH resource in TDD
-  if (UE_mac_inst[module_idP].tdd_Config) {
-    num_prach = get_num_prach_tdd(module_idP);
 
-    if ((num_prach>0) && (num_prach<6)) {
-      UE_mac_inst[module_idP].RA_prach_resources.ra_TDD_map_index = (taus()%num_prach);
-    }
+    // choose random PRACH resource in TDD
+    if (UE_mac_inst[module_idP].tdd_Config) {
+	num_prach = get_num_prach_tdd(module_idP);
 
-    f_id = get_fid_prach_tdd(module_idP,
-			     UE_mac_inst[module_idP].RA_prach_resources.ra_TDD_map_index);
-  }
+	if ((num_prach > 0) && (num_prach < 6)) {
+	    UE_mac_inst[module_idP].RA_prach_resources.ra_TDD_map_index =
+		(taus() % num_prach);
+	}
 
-  // choose RA-RNTI
-  UE_mac_inst[module_idP].RA_prach_resources.ra_RNTI = 1 + t_id + 10*f_id;
+	f_id = get_fid_prach_tdd(module_idP,
+				 UE_mac_inst
+				 [module_idP].RA_prach_resources.
+				 ra_TDD_map_index);
+    }
+    // choose RA-RNTI
+    UE_mac_inst[module_idP].RA_prach_resources.ra_RNTI =
+	1 + t_id + 10 * f_id;
 }
 
-void Msg1_transmitted(module_id_t module_idP,uint8_t CC_id,frame_t frameP, uint8_t eNB_id)
+void
+Msg1_transmitted(module_id_t module_idP, uint8_t CC_id,
+		 frame_t frameP, uint8_t eNB_id)
 {
 
-  AssertFatal(CC_id==0,
-	      "Transmission on secondary CCs is not supported yet\n");
-  // start contention resolution timer
-  UE_mac_inst[module_idP].RA_attempt_number++;
-
-  if (opt_enabled) {
-    trace_pdu(0, NULL, 0, module_idP, 0 , UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex,
-        UE_mac_inst[module_idP].txFrame, UE_mac_inst[module_idP].txSubframe, 0, UE_mac_inst[module_idP].RA_attempt_number);
-    LOG_D(OPT,"[UE %d][RAPROC] TX MSG1 Frame %d trace pdu for rnti %x  with size %d\n",
-          module_idP, frameP, 1, UE_mac_inst[module_idP].RA_Msg3_size);
-  }
+    AssertFatal(CC_id == 0,
+		"Transmission on secondary CCs is not supported yet\n");
+    // start contention resolution timer
+    UE_mac_inst[module_idP].RA_attempt_number++;
+
+    if (opt_enabled) {
+	trace_pdu(0, NULL, 0, module_idP, 0,
+		  UE_mac_inst[module_idP].RA_prach_resources.
+		  ra_PreambleIndex, UE_mac_inst[module_idP].txFrame,
+		  UE_mac_inst[module_idP].txSubframe, 0,
+		  UE_mac_inst[module_idP].RA_attempt_number);
+	LOG_D(OPT,
+	      "[UE %d][RAPROC] TX MSG1 Frame %d trace pdu for rnti %x  with size %d\n",
+	      module_idP, frameP, 1, UE_mac_inst[module_idP].RA_Msg3_size);
+    }
 
 }
 
 
-void Msg3_transmitted(module_id_t module_idP,uint8_t CC_id,frame_t frameP, uint8_t eNB_id)
+void
+Msg3_transmitted(module_id_t module_idP, uint8_t CC_id,
+		 frame_t frameP, uint8_t eNB_id)
 {
 
-  AssertFatal(CC_id==0,
-	      "Transmission on secondary CCs is not supported yet\n");
-
-  // start contention resolution timer
-  LOG_D(MAC,"[UE %d][RAPROC] Frame %d : Msg3_tx: Setting contention resolution timer\n",module_idP,frameP);
-  UE_mac_inst[module_idP].RA_contention_resolution_cnt = 0;
-  UE_mac_inst[module_idP].RA_contention_resolution_timer_active = 1;
-
-  if (opt_enabled) { // msg3
-    trace_pdu(0, &UE_mac_inst[module_idP].CCCH_pdu.payload[0], UE_mac_inst[module_idP].RA_Msg3_size,
-              module_idP, 3, UE_mac_inst[module_idP].crnti, UE_mac_inst[module_idP].txFrame, UE_mac_inst[module_idP].txSubframe, 0, 0);
-    LOG_D(OPT,"[UE %d][RAPROC] MSG3 Frame %d trace pdu Preamble %d   with size %d\n",
-          module_idP, frameP, UE_mac_inst[module_idP].crnti /*UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex*/, UE_mac_inst[module_idP].RA_Msg3_size);
-  }
+    AssertFatal(CC_id == 0,
+		"Transmission on secondary CCs is not supported yet\n");
+
+    // start contention resolution timer
+    LOG_D(MAC,
+	  "[UE %d][RAPROC] Frame %d : Msg3_tx: Setting contention resolution timer\n",
+	  module_idP, frameP);
+    UE_mac_inst[module_idP].RA_contention_resolution_cnt = 0;
+    UE_mac_inst[module_idP].RA_contention_resolution_timer_active = 1;
+
+    if (opt_enabled) {		// msg3
+	trace_pdu(0, &UE_mac_inst[module_idP].CCCH_pdu.payload[0],
+		  UE_mac_inst[module_idP].RA_Msg3_size, module_idP, 3,
+		  UE_mac_inst[module_idP].crnti,
+		  UE_mac_inst[module_idP].txFrame,
+		  UE_mac_inst[module_idP].txSubframe, 0, 0);
+	LOG_D(OPT,
+	      "[UE %d][RAPROC] MSG3 Frame %d trace pdu Preamble %d   with size %d\n",
+	      module_idP, frameP, UE_mac_inst[module_idP].crnti
+	      /*UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex */
+	      , UE_mac_inst[module_idP].RA_Msg3_size);
+    }
 
 }
 
 
-PRACH_RESOURCES_t *ue_get_rach(module_id_t module_idP,int CC_id,frame_t frameP, uint8_t eNB_indexP,sub_frame_t subframeP)
+PRACH_RESOURCES_t *ue_get_rach(module_id_t module_idP, int CC_id,
+			       frame_t frameP, uint8_t eNB_indexP,
+			       sub_frame_t subframeP)
 {
 
 
-  uint8_t                  Size               = 0;
-  UE_MODE_t                UE_mode            = get_ue_mode(module_idP,0,eNB_indexP);
-  uint8_t                  lcid               = CCCH;
-  uint16_t                 Size16;
-  struct RACH_ConfigCommon *rach_ConfigCommon = (struct RACH_ConfigCommon *)NULL;
-  int32_t                  frame_diff         = 0;
-  mac_rlc_status_resp_t    rlc_status;
-  uint8_t                  dcch_header_len    = 0;
-  uint16_t                 sdu_lengths[8];
-  uint8_t                  ulsch_buff[MAX_ULSCH_PAYLOAD_BYTES];
-
-  AssertFatal(CC_id==0,
-	      "Transmission on secondary CCs is not supported yet\n");
-
-  if (UE_mode == PRACH) {
-    if (UE_mac_inst[module_idP].radioResourceConfigCommon) {
-      rach_ConfigCommon = &UE_mac_inst[module_idP].radioResourceConfigCommon->rach_ConfigCommon;
-    } else {
-      return(NULL);
-    }
-
-    if (UE_mac_inst[module_idP].RA_active == 0) {
-      LOG_I(MAC,"RA not active\n");
-      // check if RRC is ready to initiate the RA procedure
-      Size = mac_rrc_data_req(module_idP,
-			      CC_id,
-			      frameP,
-			      CCCH,1,
-			      &UE_mac_inst[module_idP].CCCH_pdu.payload[sizeof(SCH_SUBHEADER_SHORT)+1],0,
-			      eNB_indexP,
-			      0);
-      Size16 = (uint16_t)Size;
-      
-      //  LOG_D(MAC,"[UE %d] Frame %d: Requested RRCConnectionRequest, got %d bytes\n",module_idP,frameP,Size);
-      LOG_I(RRC, "[MSC_MSG][FRAME %05d][RRC_UE][MOD %02d][][--- MAC_DATA_REQ (RRCConnectionRequest eNB %d) --->][MAC_UE][MOD %02d][]\n",
-	    frameP, module_idP, eNB_indexP, module_idP);
-      LOG_I(MAC,"[UE %d] Frame %d: Requested RRCConnectionRequest, got %d bytes\n",module_idP,frameP,Size);
-      
-      if (Size>0) {
-	
-	UE_mac_inst[module_idP].RA_active                        = 1;
-	UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER = 1;
-	UE_mac_inst[module_idP].RA_Msg3_size                     = Size+sizeof(SCH_SUBHEADER_SHORT)+sizeof(SCH_SUBHEADER_SHORT);
-	UE_mac_inst[module_idP].RA_prachMaskIndex                = 0;
-	UE_mac_inst[module_idP].RA_prach_resources.Msg3          = UE_mac_inst[module_idP].CCCH_pdu.payload;
-	UE_mac_inst[module_idP].RA_backoff_cnt                   = 0;  // add the backoff condition here if we have it from a previous RA reponse which failed (i.e. backoff indicator)
-	
-	AssertFatal(rach_ConfigCommon!=NULL,
-		    "[UE %d] FATAL Frame %d: rach_ConfigCommon is NULL !!!\n",module_idP,frameP);
-	UE_mac_inst[module_idP].RA_window_cnt                    = 2+ rach_ConfigCommon->ra_SupervisionInfo.ra_ResponseWindowSize;
-	
-	if (UE_mac_inst[module_idP].RA_window_cnt == 9) {
-	  UE_mac_inst[module_idP].RA_window_cnt = 10;  // Note: 9 subframe window doesn't exist, after 8 is 10!
+    uint8_t Size = 0;
+    UE_MODE_t UE_mode = get_ue_mode(module_idP, 0, eNB_indexP);
+    uint8_t lcid = CCCH;
+    uint16_t Size16;
+    struct RACH_ConfigCommon *rach_ConfigCommon =
+	(struct RACH_ConfigCommon *) NULL;
+    int32_t frame_diff = 0;
+    mac_rlc_status_resp_t rlc_status;
+    uint8_t dcch_header_len = 0;
+    uint16_t sdu_lengths[8];
+    uint8_t ulsch_buff[MAX_ULSCH_PAYLOAD_BYTES];
+
+    AssertFatal(CC_id == 0,
+		"Transmission on secondary CCs is not supported yet\n");
+
+    if (UE_mode == PRACH) {
+	if (UE_mac_inst[module_idP].radioResourceConfigCommon) {
+	    rach_ConfigCommon =
+		&UE_mac_inst[module_idP].
+		radioResourceConfigCommon->rach_ConfigCommon;
+	} else {
+	    return (NULL);
 	}
-	
-	UE_mac_inst[module_idP].RA_tx_frame         = frameP;
-	UE_mac_inst[module_idP].RA_tx_subframe      = subframeP;
-	UE_mac_inst[module_idP].RA_backoff_frame    = frameP;
-	UE_mac_inst[module_idP].RA_backoff_subframe = subframeP;
-	// Fill in preamble and PRACH resource
-	get_prach_resources(module_idP,CC_id,eNB_indexP,subframeP,1,NULL);
-	
-	generate_ulsch_header((uint8_t*)&UE_mac_inst[module_idP].CCCH_pdu.payload[0],  // mac header
-			      1,      // num sdus
-			      0,            // short pading
-			      &Size16,  // sdu length
-			      &lcid,    // sdu lcid
-			      NULL,  // power headroom
-			      NULL,  // crnti
-			      NULL,  // truncated bsr
-			      NULL, // short bsr
-			      NULL, // long_bsr
-			      1); //post_padding
-	return(&UE_mac_inst[module_idP].RA_prach_resources);
-	
-      } else if (UE_mac_inst[module_idP].scheduling_info.BSR_bytes[UE_mac_inst[module_idP].scheduling_info.LCGID[DCCH]] > 0) {
-	// This is for triggering a transmission on DCCH using PRACH (during handover, or sending SR for example)
-	dcch_header_len = 2 + 2;  /// SHORT Subheader + C-RNTI control element
-	rlc_status = mac_rlc_status_ind(module_idP,UE_mac_inst[module_idP].crnti, eNB_indexP,frameP,subframeP,ENB_FLAG_NO,MBMS_FLAG_NO,
-					DCCH,
-					6);
-	
-	if (UE_mac_inst[module_idP].crnti_before_ho)
-	  LOG_D(MAC,
-		"[UE %d] Frame %d : UL-DCCH -> ULSCH, HO RRCConnectionReconfigurationComplete (%x, %x), RRC message has %d bytes to send throug PRACH (mac header len %d)\n",
-		module_idP,frameP, UE_mac_inst[module_idP].crnti,UE_mac_inst[module_idP].crnti_before_ho, rlc_status.bytes_in_buffer,dcch_header_len);
-	else
-	  LOG_D(MAC,"[UE %d] Frame %d : UL-DCCH -> ULSCH, RRC message has %d bytes to send through PRACH(mac header len %d)\n",
-		module_idP,frameP, rlc_status.bytes_in_buffer,dcch_header_len);
-	
-	sdu_lengths[0] = mac_rlc_data_req(module_idP, UE_mac_inst[module_idP].crnti,
-					  eNB_indexP, frameP,ENB_FLAG_NO, MBMS_FLAG_NO,
-					  DCCH,
-					  6,	//not used
-					  (char *)&ulsch_buff[0]);
-	
-	LOG_D(MAC,"[UE %d] TX Got %d bytes for DCCH\n",module_idP,sdu_lengths[0]);
-	update_bsr(module_idP, frameP, subframeP,eNB_indexP);
-	UE_mac_inst[module_idP].scheduling_info.BSR[UE_mac_inst[module_idP].scheduling_info.LCGID[DCCH]] =
-	  locate_BsrIndexByBufferSize(BSR_TABLE, BSR_TABLE_SIZE, UE_mac_inst[module_idP].scheduling_info.BSR_bytes[UE_mac_inst[module_idP].scheduling_info.LCGID[DCCH]]);
-	
-	//TO DO: fill BSR infos in UL TBS
-	
-	//header_len +=2;
-	UE_mac_inst[module_idP].RA_active                        = 1;
-	UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER = 1;
-	UE_mac_inst[module_idP].RA_Msg3_size                     = Size+dcch_header_len;
-	UE_mac_inst[module_idP].RA_prachMaskIndex                = 0;
-	UE_mac_inst[module_idP].RA_prach_resources.Msg3          = ulsch_buff;
-	UE_mac_inst[module_idP].RA_backoff_cnt                   = 0;  // add the backoff condition here if we have it from a previous RA reponse which failed (i.e. backoff indicator)
-	
-	AssertFatal(rach_ConfigCommon!=NULL,
-		    "[UE %d] FATAL Frame %d: rach_ConfigCommon is NULL !!!\n",module_idP,frameP);
-	UE_mac_inst[module_idP].RA_window_cnt                    = 2+ rach_ConfigCommon->ra_SupervisionInfo.ra_ResponseWindowSize;
-	
-	if (UE_mac_inst[module_idP].RA_window_cnt == 9) {
-	  UE_mac_inst[module_idP].RA_window_cnt = 10;  // Note: 9 subframe window doesn't exist, after 8 is 10!
-	}
-	
-	
-	UE_mac_inst[module_idP].RA_tx_frame         = frameP;
-	UE_mac_inst[module_idP].RA_tx_subframe      = subframeP;
-	UE_mac_inst[module_idP].RA_backoff_frame    = frameP;
-	UE_mac_inst[module_idP].RA_backoff_subframe = subframeP;
-	// Fill in preamble and PRACH resource
-	get_prach_resources(module_idP,CC_id,eNB_indexP,subframeP,1,NULL);
-	generate_ulsch_header((uint8_t*)ulsch_buff,  // mac header
-			      1,      // num sdus
-			      0,            // short pading
-			      &Size16,  // sdu length
-			      &lcid,    // sdu lcid
-			      NULL,  // power headroom
-			      &UE_mac_inst[module_idP].crnti,  // crnti
-			      NULL,  // truncated bsr
-			      NULL, // short bsr
-			      NULL, // long_bsr
-			      0); //post_padding
-	
-	return(&UE_mac_inst[module_idP].RA_prach_resources);
-      }
-    } else { // RACH is active
-      LOG_D(MAC,"[MAC][UE %d][RAPROC] frameP %d, subframe %d: RA Active, window cnt %d (RA_tx_frame %d, RA_tx_subframe %d)\n",module_idP,
-	    frameP,subframeP,UE_mac_inst[module_idP].RA_window_cnt,
-	    UE_mac_inst[module_idP].RA_tx_frame,UE_mac_inst[module_idP].RA_tx_subframe);
-      
-      // compute backoff parameters
-      if (UE_mac_inst[module_idP].RA_backoff_cnt>0) {
-	frame_diff = (sframe_t)frameP - UE_mac_inst[module_idP].RA_backoff_frame;
-	
-	if (frame_diff < 0) {
-	  frame_diff = -frame_diff;
-	}
-	
-	UE_mac_inst[module_idP].RA_backoff_cnt -= ((10*frame_diff) + (subframeP-UE_mac_inst[module_idP].RA_backoff_subframe));
-	
-	UE_mac_inst[module_idP].RA_backoff_frame    = frameP;
-	UE_mac_inst[module_idP].RA_backoff_subframe = subframeP;
-      }
-      
-      // compute RA window parameters
-      if (UE_mac_inst[module_idP].RA_window_cnt>0) {
-	frame_diff = (frame_t)frameP - UE_mac_inst[module_idP].RA_tx_frame;
-	
-	if (frame_diff < 0) {
-	  frame_diff = -frame_diff;
-	}
-	
-	UE_mac_inst[module_idP].RA_window_cnt -= ((10*frame_diff) + (subframeP-UE_mac_inst[module_idP].RA_tx_subframe));
-	LOG_D(MAC,"[MAC][UE %d][RAPROC] frameP %d, subframe %d: RA Active, adjusted window cnt %d\n",module_idP,
-	      frameP,subframeP,UE_mac_inst[module_idP].RA_window_cnt);
-      }
-      
-      if ((UE_mac_inst[module_idP].RA_window_cnt<=0) &&
-	  (UE_mac_inst[module_idP].RA_backoff_cnt<=0)) {
-	
-	UE_mac_inst[module_idP].RA_tx_frame    = frameP;
-	UE_mac_inst[module_idP].RA_tx_subframe = subframeP;
-	UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER++;
-	UE_mac_inst[module_idP].RA_prach_resources.ra_PREAMBLE_RECEIVED_TARGET_POWER +=
-	  (rach_ConfigCommon->powerRampingParameters.powerRampingStep<<1);  // 2dB increments in ASN.1 definition
-	int preambleTransMax = -1;
-	switch (rach_ConfigCommon->ra_SupervisionInfo.preambleTransMax) {
-	case PreambleTransMax_n3:
-	  preambleTransMax = 3;
-	  break;
-	case PreambleTransMax_n4:
-	  preambleTransMax = 4;
-	  break;
-	case PreambleTransMax_n5:
-	  preambleTransMax = 5;
-	  break;
-	case PreambleTransMax_n6:
-	  preambleTransMax = 6;
-	  break;
-	case PreambleTransMax_n7:
-	  preambleTransMax = 7;
-	  break;
-	case PreambleTransMax_n8:
-	  preambleTransMax = 8;
-	  break;
-	case PreambleTransMax_n10:
-	  preambleTransMax = 10;
-	  break;
-	case PreambleTransMax_n20:
-	  preambleTransMax = 20;
-	  break;
-	case PreambleTransMax_n50:
-	  preambleTransMax = 50;
-	  break;
-	case PreambleTransMax_n100:
-	  preambleTransMax = 100;
-	  break;
-	case PreambleTransMax_n200:
-	  preambleTransMax = 200;
-	  break;
-	} 
-	
-	if (UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER == preambleTransMax) {
-	  LOG_D(MAC,"[UE %d] Frame %d: Maximum number of RACH attempts (%d)\n",module_idP,frameP,preambleTransMax);
-	  // send message to RRC
-	  UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER=1;
-	  UE_mac_inst[module_idP].RA_prach_resources.ra_PREAMBLE_RECEIVED_TARGET_POWER = get_Po_NOMINAL_PUSCH(module_idP,CC_id);
+
+	if (UE_mac_inst[module_idP].RA_active == 0) {
+	    LOG_I(MAC, "RA not active\n");
+	    // check if RRC is ready to initiate the RA procedure
+	    Size = mac_rrc_data_req(module_idP,
+				    CC_id,
+				    frameP,
+				    CCCH, 1,
+				    &UE_mac_inst[module_idP].
+				    CCCH_pdu.payload[sizeof
+						     (SCH_SUBHEADER_SHORT)
+						     + 1], 0, eNB_indexP,
+				    0);
+	    Size16 = (uint16_t) Size;
+
+	    //  LOG_D(MAC,"[UE %d] Frame %d: Requested RRCConnectionRequest, got %d bytes\n",module_idP,frameP,Size);
+	    LOG_I(RRC,
+		  "[MSC_MSG][FRAME %05d][RRC_UE][MOD %02d][][--- MAC_DATA_REQ (RRCConnectionRequest eNB %d) --->][MAC_UE][MOD %02d][]\n",
+		  frameP, module_idP, eNB_indexP, module_idP);
+	    LOG_I(MAC,
+		  "[UE %d] Frame %d: Requested RRCConnectionRequest, got %d bytes\n",
+		  module_idP, frameP, Size);
+
+	    if (Size > 0) {
+
+		UE_mac_inst[module_idP].RA_active = 1;
+		UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER =
+		    1;
+		UE_mac_inst[module_idP].RA_Msg3_size =
+		    Size + sizeof(SCH_SUBHEADER_SHORT) +
+		    sizeof(SCH_SUBHEADER_SHORT);
+		UE_mac_inst[module_idP].RA_prachMaskIndex = 0;
+		UE_mac_inst[module_idP].RA_prach_resources.Msg3 =
+		    UE_mac_inst[module_idP].CCCH_pdu.payload;
+		UE_mac_inst[module_idP].RA_backoff_cnt = 0;	// add the backoff condition here if we have it from a previous RA reponse which failed (i.e. backoff indicator)
+
+		AssertFatal(rach_ConfigCommon != NULL,
+			    "[UE %d] FATAL Frame %d: rach_ConfigCommon is NULL !!!\n",
+			    module_idP, frameP);
+		UE_mac_inst[module_idP].RA_window_cnt =
+		    2 +
+		    rach_ConfigCommon->ra_SupervisionInfo.
+		    ra_ResponseWindowSize;
+
+		if (UE_mac_inst[module_idP].RA_window_cnt == 9) {
+		    UE_mac_inst[module_idP].RA_window_cnt = 10;	// Note: 9 subframe window doesn't exist, after 8 is 10!
+		}
+
+		UE_mac_inst[module_idP].RA_tx_frame = frameP;
+		UE_mac_inst[module_idP].RA_tx_subframe = subframeP;
+		UE_mac_inst[module_idP].RA_backoff_frame = frameP;
+		UE_mac_inst[module_idP].RA_backoff_subframe = subframeP;
+		// Fill in preamble and PRACH resource
+		get_prach_resources(module_idP, CC_id, eNB_indexP,
+				    subframeP, 1, NULL);
+
+		generate_ulsch_header((uint8_t *) & UE_mac_inst[module_idP].CCCH_pdu.payload[0],	// mac header
+				      1,	// num sdus
+				      0,	// short pading
+				      &Size16,	// sdu length
+				      &lcid,	// sdu lcid
+				      NULL,	// power headroom
+				      NULL,	// crnti
+				      NULL,	// truncated bsr
+				      NULL,	// short bsr
+				      NULL,	// long_bsr
+				      1);	//post_padding
+		return (&UE_mac_inst[module_idP].RA_prach_resources);
+
+	    } else if (UE_mac_inst[module_idP].
+		       scheduling_info.BSR_bytes[UE_mac_inst[module_idP].
+						 scheduling_info.LCGID
+						 [DCCH]] > 0) {
+		// This is for triggering a transmission on DCCH using PRACH (during handover, or sending SR for example)
+		dcch_header_len = 2 + 2;	/// SHORT Subheader + C-RNTI control element
+		rlc_status =
+		    mac_rlc_status_ind(module_idP,
+				       UE_mac_inst[module_idP].crnti,
+				       eNB_indexP, frameP, subframeP,
+				       ENB_FLAG_NO, MBMS_FLAG_NO, DCCH, 6);
+
+		if (UE_mac_inst[module_idP].crnti_before_ho)
+		    LOG_D(MAC,
+			  "[UE %d] Frame %d : UL-DCCH -> ULSCH, HO RRCConnectionReconfigurationComplete (%x, %x), RRC message has %d bytes to send throug PRACH (mac header len %d)\n",
+			  module_idP, frameP,
+			  UE_mac_inst[module_idP].crnti,
+			  UE_mac_inst[module_idP].crnti_before_ho,
+			  rlc_status.bytes_in_buffer, dcch_header_len);
+		else
+		    LOG_D(MAC,
+			  "[UE %d] Frame %d : UL-DCCH -> ULSCH, RRC message has %d bytes to send through PRACH(mac header len %d)\n",
+			  module_idP, frameP, rlc_status.bytes_in_buffer,
+			  dcch_header_len);
+
+		sdu_lengths[0] = mac_rlc_data_req(module_idP, UE_mac_inst[module_idP].crnti, eNB_indexP, frameP, ENB_FLAG_NO, MBMS_FLAG_NO, DCCH, 6,	//not used
+						  (char *) &ulsch_buff[0]);
+
+		LOG_D(MAC, "[UE %d] TX Got %d bytes for DCCH\n",
+		      module_idP, sdu_lengths[0]);
+		update_bsr(module_idP, frameP, subframeP, eNB_indexP);
+		UE_mac_inst[module_idP].
+		    scheduling_info.BSR[UE_mac_inst[module_idP].
+					scheduling_info.LCGID[DCCH]] =
+		    locate_BsrIndexByBufferSize(BSR_TABLE, BSR_TABLE_SIZE,
+						UE_mac_inst
+						[module_idP].scheduling_info.BSR_bytes
+						[UE_mac_inst
+						 [module_idP].scheduling_info.LCGID
+						 [DCCH]]);
+
+		//TO DO: fill BSR infos in UL TBS
+
+		//header_len +=2;
+		UE_mac_inst[module_idP].RA_active = 1;
+		UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER =
+		    1;
+		UE_mac_inst[module_idP].RA_Msg3_size =
+		    Size + dcch_header_len;
+		UE_mac_inst[module_idP].RA_prachMaskIndex = 0;
+		UE_mac_inst[module_idP].RA_prach_resources.Msg3 =
+		    ulsch_buff;
+		UE_mac_inst[module_idP].RA_backoff_cnt = 0;	// add the backoff condition here if we have it from a previous RA reponse which failed (i.e. backoff indicator)
+
+		AssertFatal(rach_ConfigCommon != NULL,
+			    "[UE %d] FATAL Frame %d: rach_ConfigCommon is NULL !!!\n",
+			    module_idP, frameP);
+		UE_mac_inst[module_idP].RA_window_cnt =
+		    2 +
+		    rach_ConfigCommon->ra_SupervisionInfo.
+		    ra_ResponseWindowSize;
+
+		if (UE_mac_inst[module_idP].RA_window_cnt == 9) {
+		    UE_mac_inst[module_idP].RA_window_cnt = 10;	// Note: 9 subframe window doesn't exist, after 8 is 10!
+		}
+
+
+		UE_mac_inst[module_idP].RA_tx_frame = frameP;
+		UE_mac_inst[module_idP].RA_tx_subframe = subframeP;
+		UE_mac_inst[module_idP].RA_backoff_frame = frameP;
+		UE_mac_inst[module_idP].RA_backoff_subframe = subframeP;
+		// Fill in preamble and PRACH resource
+		get_prach_resources(module_idP, CC_id, eNB_indexP,
+				    subframeP, 1, NULL);
+		generate_ulsch_header((uint8_t *) ulsch_buff,	// mac header
+				      1,	// num sdus
+				      0,	// short pading
+				      &Size16,	// sdu length
+				      &lcid,	// sdu lcid
+				      NULL,	// power headroom
+				      &UE_mac_inst[module_idP].crnti,	// crnti
+				      NULL,	// truncated bsr
+				      NULL,	// short bsr
+				      NULL,	// long_bsr
+				      0);	//post_padding
+
+		return (&UE_mac_inst[module_idP].RA_prach_resources);
+	    }
+	} else {		// RACH is active
+	    LOG_D(MAC,
+		  "[MAC][UE %d][RAPROC] frameP %d, subframe %d: RA Active, window cnt %d (RA_tx_frame %d, RA_tx_subframe %d)\n",
+		  module_idP, frameP, subframeP,
+		  UE_mac_inst[module_idP].RA_window_cnt,
+		  UE_mac_inst[module_idP].RA_tx_frame,
+		  UE_mac_inst[module_idP].RA_tx_subframe);
+
+	    // compute backoff parameters
+	    if (UE_mac_inst[module_idP].RA_backoff_cnt > 0) {
+		frame_diff =
+		    (sframe_t) frameP -
+		    UE_mac_inst[module_idP].RA_backoff_frame;
+
+		if (frame_diff < 0) {
+		    frame_diff = -frame_diff;
+		}
+
+		UE_mac_inst[module_idP].RA_backoff_cnt -=
+		    ((10 * frame_diff) +
+		     (subframeP -
+		      UE_mac_inst[module_idP].RA_backoff_subframe));
+
+		UE_mac_inst[module_idP].RA_backoff_frame = frameP;
+		UE_mac_inst[module_idP].RA_backoff_subframe = subframeP;
+	    }
+	    // compute RA window parameters
+	    if (UE_mac_inst[module_idP].RA_window_cnt > 0) {
+		frame_diff =
+		    (frame_t) frameP - UE_mac_inst[module_idP].RA_tx_frame;
+
+		if (frame_diff < 0) {
+		    frame_diff = -frame_diff;
+		}
+
+		UE_mac_inst[module_idP].RA_window_cnt -=
+		    ((10 * frame_diff) +
+		     (subframeP - UE_mac_inst[module_idP].RA_tx_subframe));
+		LOG_D(MAC,
+		      "[MAC][UE %d][RAPROC] frameP %d, subframe %d: RA Active, adjusted window cnt %d\n",
+		      module_idP, frameP, subframeP,
+		      UE_mac_inst[module_idP].RA_window_cnt);
+	    }
+
+	    if ((UE_mac_inst[module_idP].RA_window_cnt <= 0) &&
+		(UE_mac_inst[module_idP].RA_backoff_cnt <= 0)) {
+
+		UE_mac_inst[module_idP].RA_tx_frame = frameP;
+		UE_mac_inst[module_idP].RA_tx_subframe = subframeP;
+		UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER++;
+		UE_mac_inst[module_idP].RA_prach_resources.ra_PREAMBLE_RECEIVED_TARGET_POWER += (rach_ConfigCommon->powerRampingParameters.powerRampingStep << 1);	// 2dB increments in ASN.1 definition
+		int preambleTransMax = -1;
+		switch (rach_ConfigCommon->ra_SupervisionInfo.
+			preambleTransMax) {
+		case PreambleTransMax_n3:
+		    preambleTransMax = 3;
+		    break;
+		case PreambleTransMax_n4:
+		    preambleTransMax = 4;
+		    break;
+		case PreambleTransMax_n5:
+		    preambleTransMax = 5;
+		    break;
+		case PreambleTransMax_n6:
+		    preambleTransMax = 6;
+		    break;
+		case PreambleTransMax_n7:
+		    preambleTransMax = 7;
+		    break;
+		case PreambleTransMax_n8:
+		    preambleTransMax = 8;
+		    break;
+		case PreambleTransMax_n10:
+		    preambleTransMax = 10;
+		    break;
+		case PreambleTransMax_n20:
+		    preambleTransMax = 20;
+		    break;
+		case PreambleTransMax_n50:
+		    preambleTransMax = 50;
+		    break;
+		case PreambleTransMax_n100:
+		    preambleTransMax = 100;
+		    break;
+		case PreambleTransMax_n200:
+		    preambleTransMax = 200;
+		    break;
+		}
+
+		if (UE_mac_inst[module_idP].
+		    RA_PREAMBLE_TRANSMISSION_COUNTER == preambleTransMax) {
+		    LOG_D(MAC,
+			  "[UE %d] Frame %d: Maximum number of RACH attempts (%d)\n",
+			  module_idP, frameP, preambleTransMax);
+		    // send message to RRC
+		    UE_mac_inst[module_idP].
+			RA_PREAMBLE_TRANSMISSION_COUNTER = 1;
+		    UE_mac_inst[module_idP].
+			RA_prach_resources.ra_PREAMBLE_RECEIVED_TARGET_POWER
+			= get_Po_NOMINAL_PUSCH(module_idP, CC_id);
+		}
+
+		UE_mac_inst[module_idP].RA_window_cnt =
+		    2 +
+		    rach_ConfigCommon->ra_SupervisionInfo.
+		    ra_ResponseWindowSize;
+		UE_mac_inst[module_idP].RA_backoff_cnt = 0;
+
+		// Fill in preamble and PRACH resource
+		get_prach_resources(module_idP, CC_id, eNB_indexP,
+				    subframeP, 0, NULL);
+		return (&UE_mac_inst[module_idP].RA_prach_resources);
+	    }
 	}
-	
-	UE_mac_inst[module_idP].RA_window_cnt                    = 2+ rach_ConfigCommon->ra_SupervisionInfo.ra_ResponseWindowSize;
-	UE_mac_inst[module_idP].RA_backoff_cnt                   = 0;
-	
-	// Fill in preamble and PRACH resource
-	get_prach_resources(module_idP,CC_id,eNB_indexP,subframeP,0,NULL);
-	return(&UE_mac_inst[module_idP].RA_prach_resources);
-      }
+    } else if (UE_mode == PUSCH) {
+	LOG_D(MAC,
+	      "[UE %d] FATAL: Should not have checked for RACH in PUSCH yet ...",
+	      module_idP);
+	AssertFatal(1 == 0, "");
     }
-  } else if (UE_mode == PUSCH) {
-    LOG_D(MAC,"[UE %d] FATAL: Should not have checked for RACH in PUSCH yet ...",module_idP);
-    AssertFatal(1==0,"");
-  }
-  
-  return(NULL);
+
+    return (NULL);
 }
diff --git a/openair2/LAYER2/MAC/rar_tools.c b/openair2/LAYER2/MAC/rar_tools.c
index ad870b63879f4dd05bd3faea7d550f8b9ec3062c..8d9e5bb3e14d88664d06a3f621f25cb07c033188 100644
--- a/openair2/LAYER2/MAC/rar_tools.c
+++ b/openair2/LAYER2/MAC/rar_tools.c
@@ -40,269 +40,246 @@
 
 #define DEBUG_RAR
 
-extern unsigned int  localRIV2alloc_LUT25[512];
-extern unsigned int  distRIV2alloc_LUT25[512];
+extern unsigned int localRIV2alloc_LUT25[512];
+extern unsigned int distRIV2alloc_LUT25[512];
 extern unsigned short RIV2nb_rb_LUT25[512];
 extern unsigned short RIV2first_rb_LUT25[512];
 extern RAN_CONTEXT_t RC;
 
 //------------------------------------------------------------------------------
-unsigned short fill_rar(
-  const module_id_t module_idP,
-  const int         CC_id,
-  const frame_t     frameP,
-  uint8_t*    const dlsch_buffer,
-  const uint16_t    N_RB_UL,
-  const uint8_t input_buffer_length
-)
+unsigned short
+fill_rar(const module_id_t module_idP,
+	 const int CC_id,
+	 RA_t * ra,
+	 const frame_t frameP,
+	 uint8_t * const dlsch_buffer,
+	 const uint16_t N_RB_UL, const uint8_t input_buffer_length)
 //------------------------------------------------------------------------------
 {
 
-  RA_HEADER_RAPID *rarh = (RA_HEADER_RAPID *)dlsch_buffer;
-  //  RAR_PDU *rar = (RAR_PDU *)(dlsch_buffer+1);
-  uint8_t *rar = (uint8_t *)(dlsch_buffer+1);
-  int i,ra_idx = -1;
-  RA_TEMPLATE *RA_template;
-
-  AssertFatal(CC_id < MAX_NUM_CCs, "CC_id %u < MAX_NUM_CCs %u", CC_id, MAX_NUM_CCs);
-
-  for (i=0; i<NB_RA_PROC_MAX; i++) {
-    if (RC.mac[module_idP]->common_channels[CC_id].RA_template[i].generate_rar == 1) {
-      ra_idx=i;
-      RC.mac[module_idP]->common_channels[CC_id].RA_template[i].generate_rar = 0;
-      break;
+    RA_HEADER_RAPID *rarh = (RA_HEADER_RAPID *) dlsch_buffer;
+    uint8_t *rar = (uint8_t *) (dlsch_buffer + 1);
+
+
+    // subheader fixed
+    rarh->E = 0;		// First and last RAR
+    rarh->T = 1;		// 0 for E/T/R/R/BI subheader, 1 for E/T/RAPID subheader
+    rarh->RAPID = ra->preamble_index;	// Respond to Preamble 0 only for the moment
+    rar[4] = (uint8_t) (ra->rnti >> 8);
+    rar[5] = (uint8_t) (ra->rnti & 0xff);
+    //ra->timing_offset = 0;
+    ra->timing_offset /= 16;	//T_A = N_TA/16, where N_TA should be on a 30.72Msps
+    rar[0] = (uint8_t) (ra->timing_offset >> (2 + 4));	// 7 MSBs of timing advance + divide by 4
+    rar[1] = (uint8_t) (ra->timing_offset << (4 - 2)) & 0xf0;	// 4 LSBs of timing advance + divide by 4
+    ra->msg3_first_rb = 6;
+    ra->msg3_nb_rb = 1;
+    uint16_t rballoc = mac_computeRIV(N_RB_UL, ra->msg3_first_rb, ra->msg3_nb_rb);	// first PRB only for UL Grant
+    rar[1] |= (rballoc >> 7) & 7;	// Hopping = 0 (bit 3), 3 MSBs of rballoc
+    rar[2] = ((uint8_t) (rballoc & 0xff)) << 1;	// 7 LSBs of rballoc
+    ra->msg3_mcs = 10;
+    ra->msg3_TPC = 3;
+    ra->msg3_ULdelay = 0;
+    ra->msg3_cqireq = 0;
+    rar[2] |= ((ra->msg3_mcs & 0x8) >> 3);	// mcs 10
+    rar[3] =
+	(((ra->msg3_mcs & 0x7) << 5)) | ((ra->msg3_TPC & 7) << 2) |
+	((ra->msg3_ULdelay & 1) << 1) | (ra->msg3_cqireq & 1);
+
+    if (opt_enabled) {
+	trace_pdu(1, dlsch_buffer, input_buffer_length, module_idP, 2, 1,
+		  RC.mac[module_idP]->frame, RC.mac[module_idP]->subframe,
+		  0, 0);
+	LOG_D(OPT,
+	      "[eNB %d][RAPROC] CC_id %d RAR Frame %d trace pdu for rnti %x and  rapid %d size %d\n",
+	      module_idP, CC_id, frameP, ra->rnti, rarh->RAPID,
+	      input_buffer_length);
     }
-  }
-  RA_template = &RC.mac[module_idP]->common_channels[CC_id].RA_template[ra_idx];
- 
-  //DevAssert( ra_idx != -1 );
-  if (ra_idx==-1)
-    return(0);
-
-  // subheader fixed
-  rarh->E                     = 0; // First and last RAR
-  rarh->T                     = 1; // 0 for E/T/R/R/BI subheader, 1 for E/T/RAPID subheader
-  rarh->RAPID                 = RA_template->preamble_index; // Respond to Preamble 0 only for the moment
-  rar[4] = (uint8_t)(RA_template->rnti>>8);
-  rar[5] = (uint8_t)(RA_template->rnti&0xff);
-  //RA_template->timing_offset = 0;
-  RA_template->timing_offset /= 16; //T_A = N_TA/16, where N_TA should be on a 30.72Msps
-  rar[0] = (uint8_t)(RA_template->timing_offset>>(2+4)); // 7 MSBs of timing advance + divide by 4
-  rar[1] = (uint8_t)(RA_template->timing_offset<<(4-2))&0xf0; // 4 LSBs of timing advance + divide by 4
-  RA_template->msg3_first_rb=6;
-  RA_template->msg3_nb_rb=1;
-  uint16_t rballoc = mac_computeRIV(N_RB_UL,RA_template->msg3_first_rb,RA_template->msg3_nb_rb); // first PRB only for UL Grant
-  rar[1] |= (rballoc>>7)&7; // Hopping = 0 (bit 3), 3 MSBs of rballoc
-  rar[2] = ((uint8_t)(rballoc&0xff))<<1; // 7 LSBs of rballoc
-  RA_template->msg3_mcs = 10;
-  RA_template->msg3_TPC = 3;
-  RA_template->msg3_ULdelay = 0;
-  RA_template->msg3_cqireq = 0;
-  rar[2] |= ((RA_template->msg3_mcs&0x8)>>3);  // mcs 10
-  rar[3] = (((RA_template->msg3_mcs&0x7)<<5)) | ((RA_template->msg3_TPC&7)<<2) | ((RA_template->msg3_ULdelay&1)<<1) | (RA_template->msg3_cqireq&1);
-
-  LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d Generating RAR (%02x|%02x.%02x.%02x.%02x.%02x.%02x) for ra_idx %d, CRNTI %x,preamble %d/%d,TIMING OFFSET %d\n",
-        module_idP, CC_id,
-        frameP,
-        *(uint8_t*)rarh,rar[0],rar[1],rar[2],rar[3],rar[4],rar[5],
-        ra_idx,
-        RA_template->rnti,
-        rarh->RAPID,RC.mac[module_idP]->common_channels[CC_id].RA_template[0].preamble_index,
-        RA_template->timing_offset);
-
-  if (opt_enabled) {
-    trace_pdu(1, dlsch_buffer, input_buffer_length, module_idP, 2, 1,
-        RC.mac[module_idP]->frame, RC.mac[module_idP]->subframe, 0, 0);
-    LOG_D(OPT,"[eNB %d][RAPROC] CC_id %d RAR Frame %d trace pdu for rnti %x and  rapid %d size %d\n",
-          module_idP, CC_id, frameP, RA_template->rnti,
-          rarh->RAPID, input_buffer_length);
-  }
-
-  return(RA_template->rnti);
+
+    return (ra->rnti);
 }
 
 #ifdef Rel14
 //------------------------------------------------------------------------------
-unsigned short fill_rar_br(eNB_MAC_INST *eNB,
-			   int CC_id,
-			   RA_TEMPLATE        *RA_template,      
-			   const frame_t      frameP,
-			   const sub_frame_t  subframeP,
-			   uint8_t*    const  dlsch_buffer,
-			   const uint8_t      ce_level
-)
+unsigned short
+fill_rar_br(eNB_MAC_INST * eNB,
+	    int CC_id,
+	    RA_t * ra,
+	    const frame_t frameP,
+	    const sub_frame_t subframeP,
+	    uint8_t * const dlsch_buffer, const uint8_t ce_level)
 //------------------------------------------------------------------------------
 {
 
-  RA_HEADER_RAPID *rarh = (RA_HEADER_RAPID *)dlsch_buffer;
-  COMMON_channels_t *cc = &eNB->common_channels[CC_id];
-  uint8_t *rar = (uint8_t *)(dlsch_buffer+1);
-  //  uint8_t nb,reps;
-  uint8_t rballoc;
-  uint8_t mcs,TPC,ULdelay,cqireq;
-  int input_buffer_length;
-
-
-  AssertFatal(RA_template != NULL, "RA is null \n");
-
-  // subheader fixed
-  rarh->E                     = 0; // First and last RAR
-  rarh->T                     = 1; // 0 for E/T/R/R/BI subheader, 1 for E/T/RAPID subheader
-  rarh->RAPID                 = RA_template->preamble_index; // Respond to Preamble 0 only for the moment
-  RA_template->timing_offset /= 16; //T_A = N_TA/16, where N_TA should be on a 30.72Msps
-  rar[0] = (uint8_t)(RA_template->timing_offset>>(2+4)); // 7 MSBs of timing advance + divide by 4
-  rar[1] = (uint8_t)(RA_template->timing_offset<<(4-2))&0xf0; // 4 LSBs of timing advance + divide by 4
-
-  int N_NB_index;
-
-  AssertFatal(1==0,"RAR for BL/CE Still to be finished ...\n"); 
-
-  // Copy the Msg2 narrowband
-  RA_template->msg34_narrowband = RA_template->msg2_narrowband; 
-
-  if (ce_level<2) { //CE Level 0,1, CEmodeA
-    input_buffer_length =6;
-
-    N_NB_index = get_numnarrowbandbits(cc->mib->message.dl_Bandwidth);
-
-    rar[4] = (uint8_t)(RA_template->rnti>>8);
-    rar[5] = (uint8_t)(RA_template->rnti&0xff);
-    //cc->RA_template[ra_idx].timing_offset = 0;
-    //    nb      = 0;
-    rballoc = mac_computeRIV(6,1+ce_level,1); // one PRB only for UL Grant in position 1+ce_level within Narrowband
-    rar[1] |= (rballoc&15)<<(4-N_NB_index); // Hopping = 0 (bit 3), 3 MSBs of rballoc
-
-    //    reps    = 4;
-    mcs     = 7;
-    TPC     = 3; // no power increase
-    ULdelay = 0;
-    cqireq = 0;
-    rar[2] |= ((mcs&0x8)>>3);  // mcs 10
-    rar[3] = (((mcs&0x7)<<5)) | ((TPC&7)<<2) | ((ULdelay&1)<<1) | (cqireq&1);
-  }
-  else { // CE level 2,3 => CEModeB
-
-    input_buffer_length =5;
-
-    rar[3] = (uint8_t)(RA_template->rnti>>8);
-    rar[4] = (uint8_t)(RA_template->rnti&0xff);
-  }
-  LOG_D(MAC,"[RAPROC] Frame %d Generating RAR BR (%02x|%02x.%02x.%02x.%02x.%02x.%02x) for ce_level %d, CRNTI %x,preamble %d/%d,TIMING OFFSET %d\n",
-        frameP,
-        *(uint8_t*)rarh,rar[0],rar[1],rar[2],rar[3],rar[4],rar[5],
-        ce_level,
-        RA_template->rnti,
-        rarh->RAPID,RA_template->preamble_index,
-        RA_template->timing_offset);
-
-  if (opt_enabled) {
-    trace_pdu(1, dlsch_buffer, input_buffer_length, eNB->Mod_id, 2, 1,
-        eNB->frame, eNB->subframe, 0, 0);
-    LOG_D(OPT,"[RAPROC] RAR Frame %d trace pdu for rnti %x and  rapid %d size %d\n",
-          frameP, RA_template->rnti,
-          rarh->RAPID, input_buffer_length);
-  }
-
-  return(RA_template->rnti);
+    RA_HEADER_RAPID *rarh = (RA_HEADER_RAPID *) dlsch_buffer;
+    COMMON_channels_t *cc = &eNB->common_channels[CC_id];
+    uint8_t *rar = (uint8_t *) (dlsch_buffer + 1);
+    //  uint8_t nb,reps;
+    uint8_t rballoc;
+    uint8_t mcs, TPC, ULdelay, cqireq;
+    int input_buffer_length;
+
+
+    AssertFatal(ra != NULL, "RA is null \n");
+
+    // subheader fixed
+    rarh->E = 0;		// First and last RAR
+    rarh->T = 1;		// 0 for E/T/R/R/BI subheader, 1 for E/T/RAPID subheader
+    rarh->RAPID = ra->preamble_index;	// Respond to Preamble 0 only for the moment
+    ra->timing_offset /= 16;	//T_A = N_TA/16, where N_TA should be on a 30.72Msps
+    rar[0] = (uint8_t) (ra->timing_offset >> (2 + 4));	// 7 MSBs of timing advance + divide by 4
+    rar[1] = (uint8_t) (ra->timing_offset << (4 - 2)) & 0xf0;	// 4 LSBs of timing advance + divide by 4
+
+    int N_NB_index;
+
+    AssertFatal(1 == 0, "RAR for BL/CE Still to be finished ...\n");
+
+    // Copy the Msg2 narrowband
+    ra->msg34_narrowband = ra->msg2_narrowband;
+
+    if (ce_level < 2) {		//CE Level 0,1, CEmodeA
+	input_buffer_length = 6;
+
+	N_NB_index = get_numnarrowbandbits(cc->mib->message.dl_Bandwidth);
+
+	rar[4] = (uint8_t) (ra->rnti >> 8);
+	rar[5] = (uint8_t) (ra->rnti & 0xff);
+	//cc->ra[ra_idx].timing_offset = 0;
+	//    nb      = 0;
+	rballoc = mac_computeRIV(6, 1 + ce_level, 1);	// one PRB only for UL Grant in position 1+ce_level within Narrowband
+	rar[1] |= (rballoc & 15) << (4 - N_NB_index);	// Hopping = 0 (bit 3), 3 MSBs of rballoc
+
+	//    reps    = 4;
+	mcs = 7;
+	TPC = 3;		// no power increase
+	ULdelay = 0;
+	cqireq = 0;
+	rar[2] |= ((mcs & 0x8) >> 3);	// mcs 10
+	rar[3] =
+	    (((mcs & 0x7) << 5)) | ((TPC & 7) << 2) | ((ULdelay & 1) << 1)
+	    | (cqireq & 1);
+    } else {			// CE level 2,3 => CEModeB
+
+	input_buffer_length = 5;
+
+	rar[3] = (uint8_t) (ra->rnti >> 8);
+	rar[4] = (uint8_t) (ra->rnti & 0xff);
+    }
+    LOG_D(MAC,
+	  "[RAPROC] Frame %d Generating RAR BR (%02x|%02x.%02x.%02x.%02x.%02x.%02x) for ce_level %d, CRNTI %x,preamble %d/%d,TIMING OFFSET %d\n",
+	  frameP, *(uint8_t *) rarh, rar[0], rar[1], rar[2], rar[3],
+	  rar[4], rar[5], ce_level, ra->rnti, rarh->RAPID,
+	  ra->preamble_index, ra->timing_offset);
+
+    if (opt_enabled) {
+	trace_pdu(1, dlsch_buffer, input_buffer_length, eNB->Mod_id, 2, 1,
+		  eNB->frame, eNB->subframe, 0, 0);
+	LOG_D(OPT,
+	      "[RAPROC] RAR Frame %d trace pdu for rnti %x and  rapid %d size %d\n",
+	      frameP, ra->rnti, rarh->RAPID, input_buffer_length);
+    }
+
+    return (ra->rnti);
 }
 #endif
 
 //------------------------------------------------------------------------------
-uint16_t
-ue_process_rar(
-  const module_id_t module_idP,
-  const int CC_id,
-  const frame_t frameP,
-  const rnti_t ra_rnti,
-  uint8_t* const dlsch_buffer,
-  rnti_t* const t_crnti,
-  const uint8_t preamble_index,
-  uint8_t* selected_rar_buffer // output argument for storing the selected RAR header and RAR payload
-)
+uint16_t ue_process_rar(const module_id_t module_idP, const int CC_id, const frame_t frameP, const rnti_t ra_rnti, uint8_t * const dlsch_buffer, rnti_t * const t_crnti, const uint8_t preamble_index, uint8_t * selected_rar_buffer	// output argument for storing the selected RAR header and RAR payload
+    )
 //------------------------------------------------------------------------------
 {
-	uint16_t ret = 0; // return value
-
-  RA_HEADER_RAPID *rarh = (RA_HEADER_RAPID *)dlsch_buffer;
-  //  RAR_PDU *rar = (RAR_PDU *)(dlsch_buffer+1);
-  uint8_t *rar = (uint8_t *)(dlsch_buffer+1);
-
-        // get the last RAR payload for working with CMW500
-	uint8_t n_rarpy = 0; // number of RAR payloads
-	uint8_t n_rarh = 0; // number of MAC RAR subheaders
-	uint8_t best_rx_rapid = -1; // the closest RAPID receive from all RARs
-	while (1) {
-		n_rarh++;
-		if (rarh->T == 1) {
-			n_rarpy++;
-			LOG_D(MAC, "RAPID %d\n", rarh->RAPID);
-		}
-
-		if (rarh->RAPID == preamble_index) {
-			LOG_D(PHY, "Found RAR with the intended RAPID %d\n", rarh->RAPID);
-			rar = (uint8_t *)(dlsch_buffer+n_rarh + (n_rarpy-1)*6);
-			break;
-		}
-
-		if (abs((int)rarh->RAPID - (int)preamble_index) < abs((int)best_rx_rapid - (int)preamble_index)) {
-			best_rx_rapid = rarh->RAPID;
-			rar = (uint8_t *)(dlsch_buffer+n_rarh + (n_rarpy-1)*6);
-		}
-
-		if (rarh->E == 0) {
-			LOG_I(PHY, "No RAR found with the intended RAPID. The closest RAPID in all RARs is %d\n", best_rx_rapid);
-			break;
-		} else {
-			rarh++;
-		}
-	};
-	LOG_D(MAC, "number of RAR subheader %d; number of RAR pyloads %d\n", n_rarh, n_rarpy);
-
-  if (CC_id>0) {
-    LOG_W(MAC,"Should not have received RAR on secondary CCs! \n");
-    return(0xffff);
-  }
-
-  LOG_I(MAC,"[eNB %d][RAPROC] Frame %d Received RAR (%02x|%02x.%02x.%02x.%02x.%02x.%02x) for preamble %d/%d\n",module_idP,frameP,
-        *(uint8_t*)rarh,rar[0],rar[1],rar[2],rar[3],rar[4],rar[5],
-        rarh->RAPID,preamble_index);
+    uint16_t ret = 0;		// return value
+
+    RA_HEADER_RAPID *rarh = (RA_HEADER_RAPID *) dlsch_buffer;
+    //  RAR_PDU *rar = (RAR_PDU *)(dlsch_buffer+1);
+    uint8_t *rar = (uint8_t *) (dlsch_buffer + 1);
+
+    // get the last RAR payload for working with CMW500
+    uint8_t n_rarpy = 0;	// number of RAR payloads
+    uint8_t n_rarh = 0;		// number of MAC RAR subheaders
+    uint8_t best_rx_rapid = -1;	// the closest RAPID receive from all RARs
+    while (1) {
+	n_rarh++;
+	if (rarh->T == 1) {
+	    n_rarpy++;
+	    LOG_D(MAC, "RAPID %d\n", rarh->RAPID);
+	}
+
+	if (rarh->RAPID == preamble_index) {
+	    LOG_D(PHY, "Found RAR with the intended RAPID %d\n",
+		  rarh->RAPID);
+	    rar = (uint8_t *) (dlsch_buffer + n_rarh + (n_rarpy - 1) * 6);
+	    break;
+	}
+
+	if (abs((int) rarh->RAPID - (int) preamble_index) <
+	    abs((int) best_rx_rapid - (int) preamble_index)) {
+	    best_rx_rapid = rarh->RAPID;
+	    rar = (uint8_t *) (dlsch_buffer + n_rarh + (n_rarpy - 1) * 6);
+	}
+
+	if (rarh->E == 0) {
+	    LOG_I(PHY,
+		  "No RAR found with the intended RAPID. The closest RAPID in all RARs is %d\n",
+		  best_rx_rapid);
+	    break;
+	} else {
+	    rarh++;
+	}
+    };
+    LOG_D(MAC, "number of RAR subheader %d; number of RAR pyloads %d\n",
+	  n_rarh, n_rarpy);
+
+    if (CC_id > 0) {
+	LOG_W(MAC, "Should not have received RAR on secondary CCs! \n");
+	return (0xffff);
+    }
+
+    LOG_I(MAC,
+	  "[eNB %d][RAPROC] Frame %d Received RAR (%02x|%02x.%02x.%02x.%02x.%02x.%02x) for preamble %d/%d\n",
+	  module_idP, frameP, *(uint8_t *) rarh, rar[0], rar[1], rar[2],
+	  rar[3], rar[4], rar[5], rarh->RAPID, preamble_index);
 #ifdef DEBUG_RAR
-  LOG_D(MAC,"[UE %d][RAPROC] rarh->E %d\n",module_idP,rarh->E);
-  LOG_D(MAC,"[UE %d][RAPROC] rarh->T %d\n",module_idP,rarh->T);
-  LOG_D(MAC,"[UE %d][RAPROC] rarh->RAPID %d\n",module_idP,rarh->RAPID);
-
-  //  LOG_I(MAC,"[UE %d][RAPROC] rar->R %d\n",module_idP,rar->R);
-  LOG_D(MAC,"[UE %d][RAPROC] rar->Timing_Advance_Command %d\n",module_idP,(((uint16_t)(rar[0]&0x7f))<<4) + (rar[1]>>4));
-  //  LOG_I(MAC,"[UE %d][RAPROC] rar->hopping_flag %d\n",module_idP,rar->hopping_flag);
-  //  LOG_I(MAC,"[UE %d][RAPROC] rar->rb_alloc %d\n",module_idP,rar->rb_alloc);
-  //  LOG_I(MAC,"[UE %d][RAPROC] rar->mcs %d\n",module_idP,rar->mcs);
-  //  LOG_I(MAC,"[UE %d][RAPROC] rar->TPC %d\n",module_idP,rar->TPC);
-  //  LOG_I(MAC,"[UE %d][RAPROC] rar->UL_delay %d\n",module_idP,rar->UL_delay);
-  //  LOG_I(MAC,"[UE %d][RAPROC] rar->cqi_req %d\n",module_idP,rar->cqi_req);
-  LOG_D(MAC,"[UE %d][RAPROC] rar->t_crnti %x\n",module_idP,(uint16_t)rar[5]+(rar[4]<<8));
+    LOG_D(MAC, "[UE %d][RAPROC] rarh->E %d\n", module_idP, rarh->E);
+    LOG_D(MAC, "[UE %d][RAPROC] rarh->T %d\n", module_idP, rarh->T);
+    LOG_D(MAC, "[UE %d][RAPROC] rarh->RAPID %d\n", module_idP,
+	  rarh->RAPID);
+
+    //  LOG_I(MAC,"[UE %d][RAPROC] rar->R %d\n",module_idP,rar->R);
+    LOG_D(MAC, "[UE %d][RAPROC] rar->Timing_Advance_Command %d\n",
+	  module_idP, (((uint16_t) (rar[0] & 0x7f)) << 4) + (rar[1] >> 4));
+    //  LOG_I(MAC,"[UE %d][RAPROC] rar->hopping_flag %d\n",module_idP,rar->hopping_flag);
+    //  LOG_I(MAC,"[UE %d][RAPROC] rar->rb_alloc %d\n",module_idP,rar->rb_alloc);
+    //  LOG_I(MAC,"[UE %d][RAPROC] rar->mcs %d\n",module_idP,rar->mcs);
+    //  LOG_I(MAC,"[UE %d][RAPROC] rar->TPC %d\n",module_idP,rar->TPC);
+    //  LOG_I(MAC,"[UE %d][RAPROC] rar->UL_delay %d\n",module_idP,rar->UL_delay);
+    //  LOG_I(MAC,"[UE %d][RAPROC] rar->cqi_req %d\n",module_idP,rar->cqi_req);
+    LOG_D(MAC, "[UE %d][RAPROC] rar->t_crnti %x\n", module_idP,
+	  (uint16_t) rar[5] + (rar[4] << 8));
 #endif
 
-  if (opt_enabled) {
-    LOG_D(OPT,"[UE %d][RAPROC] CC_id %d RAR Frame %d trace pdu for ra-RNTI %x\n",
-          module_idP, CC_id, frameP, ra_rnti);
-    trace_pdu(1, (uint8_t*)dlsch_buffer, n_rarh + n_rarpy*6, module_idP, 2, ra_rnti,
-        UE_mac_inst[module_idP].rxFrame, UE_mac_inst[module_idP].rxSubframe, 0, 0);
-  }
-
-  if (preamble_index == rarh->RAPID) {
-    *t_crnti = (uint16_t)rar[5]+(rar[4]<<8);//rar->t_crnti;
-    UE_mac_inst[module_idP].crnti = *t_crnti;//rar->t_crnti;
-    //return(rar->Timing_Advance_Command);
-    ret = ((((uint16_t)(rar[0]&0x7f))<<4) + (rar[1]>>4));
-  } else {
-    UE_mac_inst[module_idP].crnti=0;
-    ret = (0xffff);
-  }
-
-  // move the selected RAR to the front of the RA_PDSCH buffer
-  memcpy(selected_rar_buffer+0, (uint8_t*)rarh, 1);
-  memcpy(selected_rar_buffer+1, (uint8_t*)rar , 6);
-
-  return ret;
+    if (opt_enabled) {
+	LOG_D(OPT,
+	      "[UE %d][RAPROC] CC_id %d RAR Frame %d trace pdu for ra-RNTI %x\n",
+	      module_idP, CC_id, frameP, ra_rnti);
+	trace_pdu(1, (uint8_t *) dlsch_buffer, n_rarh + n_rarpy * 6,
+		  module_idP, 2, ra_rnti, UE_mac_inst[module_idP].rxFrame,
+		  UE_mac_inst[module_idP].rxSubframe, 0, 0);
+    }
+
+    if (preamble_index == rarh->RAPID) {
+	*t_crnti = (uint16_t) rar[5] + (rar[4] << 8);	//rar->t_crnti;
+	UE_mac_inst[module_idP].crnti = *t_crnti;	//rar->t_crnti;
+	//return(rar->Timing_Advance_Command);
+	ret = ((((uint16_t) (rar[0] & 0x7f)) << 4) + (rar[1] >> 4));
+    } else {
+	UE_mac_inst[module_idP].crnti = 0;
+	ret = (0xffff);
+    }
+
+    // move the selected RAR to the front of the RA_PDSCH buffer
+    memcpy(selected_rar_buffer + 0, (uint8_t *) rarh, 1);
+    memcpy(selected_rar_buffer + 1, (uint8_t *) rar, 6);
+
+    return ret;
 
 }
diff --git a/openair2/LAYER2/MAC/ue_procedures.c b/openair2/LAYER2/MAC/ue_procedures.c
index d6c2512309ebf1130a79dc04dca51d111cb14e89..5b73a76312804f1a34536227101c90bda01a0ca8 100644
--- a/openair2/LAYER2/MAC/ue_procedures.c
+++ b/openair2/LAYER2/MAC/ue_procedures.c
@@ -37,10 +37,10 @@
 #include "defs.h"
 #include "proto.h"
 #ifdef PHY_EMUL
-# include "SIMULATION/PHY_EMULATION/impl_defs.h"
+#include "SIMULATION/PHY_EMULATION/impl_defs.h"
 #else
-# include "SCHED/defs.h"
-# include "PHY/impl_defs_top.h"
+#include "SCHED/defs.h"
+#include "PHY/impl_defs_top.h"
 #endif
 #include "PHY_INTERFACE/extern.h"
 #include "COMMON/mac_rrc_primitives.h"
@@ -54,17 +54,17 @@
 #include "OCG_extern.h"
 
 #ifdef PHY_EMUL
-# include "SIMULATION/simulation_defs.h"
+#include "SIMULATION/simulation_defs.h"
 #endif
 #include "pdcp.h"
 
 #if defined(ENABLE_ITTI)
-# include "intertask_interface.h"
+#include "intertask_interface.h"
 #endif
 
 #include "assertions.h"
 
-#include "SIMULATION/TOOLS/defs.h" // for taus
+#include "SIMULATION/TOOLS/defs.h"	// for taus
 
 #define DEBUG_HEADER_PARSING 1
 #define ENABLE_MAC_PAYLOAD_DEBUG 1
@@ -77,1738 +77,2071 @@ extern uint8_t usim_test;
 #endif
  */
 mapping BSR_names[] = {
-  {"NONE", 0},
-  {"SHORT BSR", 1},
-  {"TRUNCATED BSR", 2},
-  {"LONG BSR", 3},
-  {"PADDING BSR", 4},
-  {NULL, -1}
+    {"NONE", 0},
+    {"SHORT BSR", 1},
+    {"TRUNCATED BSR", 2},
+    {"LONG BSR", 3},
+    {"PADDING BSR", 4},
+    {NULL, -1}
 };
 
 
 void ue_init_mac(module_id_t module_idP)
 {
-  int i;
-  // default values as deined in 36.331 sec 9.2.2
-  LOG_I(MAC,"[UE%d] Applying default macMainConfig\n",module_idP);
-  //UE_mac_inst[module_idP].scheduling_info.macConfig=NULL;
-  UE_mac_inst[module_idP].scheduling_info.retxBSR_Timer= RetxBSR_Timer_r12_sf10240;
-  UE_mac_inst[module_idP].scheduling_info.periodicBSR_Timer=PeriodicBSR_Timer_r12_infinity;
-  UE_mac_inst[module_idP].scheduling_info.periodicPHR_Timer = MAC_MainConfig__phr_Config__setup__periodicPHR_Timer_sf20;
-  UE_mac_inst[module_idP].scheduling_info.prohibitPHR_Timer = MAC_MainConfig__phr_Config__setup__prohibitPHR_Timer_sf20;
-  UE_mac_inst[module_idP].scheduling_info.PathlossChange_db = MAC_MainConfig__phr_Config__setup__dl_PathlossChange_dB1;
-  UE_mac_inst[module_idP].PHR_state = MAC_MainConfig__phr_Config_PR_setup;
-  UE_mac_inst[module_idP].scheduling_info.SR_COUNTER=0;
-  UE_mac_inst[module_idP].scheduling_info.sr_ProhibitTimer=0;
-  UE_mac_inst[module_idP].scheduling_info.sr_ProhibitTimer_Running=0;
-  UE_mac_inst[module_idP].scheduling_info.maxHARQ_Tx=MAC_MainConfig__ul_SCH_Config__maxHARQ_Tx_n5;
-  UE_mac_inst[module_idP].scheduling_info.ttiBundling=0;
-  UE_mac_inst[module_idP].scheduling_info.extendedBSR_Sizes_r10=0;
-  UE_mac_inst[module_idP].scheduling_info.extendedPHR_r10=0;
-  UE_mac_inst[module_idP].scheduling_info.drx_config=NULL;
-  UE_mac_inst[module_idP].scheduling_info.phr_config=NULL;
-  // set init value 0xFFFF, make sure periodic timer and retx time counters are NOT active, after bsr transmission set the value configured by the NW.
-  UE_mac_inst[module_idP].scheduling_info.periodicBSR_SF  =  MAC_UE_BSR_TIMER_NOT_RUNNING;
-  UE_mac_inst[module_idP].scheduling_info.retxBSR_SF     =  MAC_UE_BSR_TIMER_NOT_RUNNING;
-  UE_mac_inst[module_idP].BSR_reporting_active = BSR_TRIGGER_NONE;
-  
-  UE_mac_inst[module_idP].scheduling_info.periodicPHR_SF =  get_sf_perioidicPHR_Timer(UE_mac_inst[module_idP].scheduling_info.periodicPHR_Timer);
-  UE_mac_inst[module_idP].scheduling_info.prohibitPHR_SF =  get_sf_prohibitPHR_Timer(UE_mac_inst[module_idP].scheduling_info.prohibitPHR_Timer);
-  UE_mac_inst[module_idP].scheduling_info.PathlossChange_db =  get_db_dl_PathlossChange(UE_mac_inst[module_idP].scheduling_info.PathlossChange);
-  UE_mac_inst[module_idP].PHR_reporting_active = 0;
-
-  for (i=0; i < MAX_NUM_LCID; i++) {
-    LOG_D(MAC,"[UE%d] Applying default logical channel config for LCGID %d\n",module_idP,i);
-    UE_mac_inst[module_idP].scheduling_info.Bj[i]=-1;
-    UE_mac_inst[module_idP].scheduling_info.bucket_size[i]=-1;
-
-    if (i < DTCH) { // initilize all control channels lcgid to 0
-      UE_mac_inst[module_idP].scheduling_info.LCGID[i]=0;
-    } else { // initialize all the data channels lcgid to 1
-      UE_mac_inst[module_idP].scheduling_info.LCGID[i]=1;
-    }
-
-    UE_mac_inst[module_idP].scheduling_info.LCID_status[i]=LCID_EMPTY;
-    UE_mac_inst[module_idP].scheduling_info.LCID_buffer_remain[i] = 0;
-  }
+    int i;
+    // default values as deined in 36.331 sec 9.2.2
+    LOG_I(MAC, "[UE%d] Applying default macMainConfig\n", module_idP);
+    //UE_mac_inst[module_idP].scheduling_info.macConfig=NULL;
+    UE_mac_inst[module_idP].scheduling_info.retxBSR_Timer =
+	RetxBSR_Timer_r12_sf10240;
+    UE_mac_inst[module_idP].scheduling_info.periodicBSR_Timer =
+	PeriodicBSR_Timer_r12_infinity;
+    UE_mac_inst[module_idP].scheduling_info.periodicPHR_Timer =
+	MAC_MainConfig__phr_Config__setup__periodicPHR_Timer_sf20;
+    UE_mac_inst[module_idP].scheduling_info.prohibitPHR_Timer =
+	MAC_MainConfig__phr_Config__setup__prohibitPHR_Timer_sf20;
+    UE_mac_inst[module_idP].scheduling_info.PathlossChange_db =
+	MAC_MainConfig__phr_Config__setup__dl_PathlossChange_dB1;
+    UE_mac_inst[module_idP].PHR_state =
+	MAC_MainConfig__phr_Config_PR_setup;
+    UE_mac_inst[module_idP].scheduling_info.SR_COUNTER = 0;
+    UE_mac_inst[module_idP].scheduling_info.sr_ProhibitTimer = 0;
+    UE_mac_inst[module_idP].scheduling_info.sr_ProhibitTimer_Running = 0;
+    UE_mac_inst[module_idP].scheduling_info.maxHARQ_Tx =
+	MAC_MainConfig__ul_SCH_Config__maxHARQ_Tx_n5;
+    UE_mac_inst[module_idP].scheduling_info.ttiBundling = 0;
+    UE_mac_inst[module_idP].scheduling_info.extendedBSR_Sizes_r10 = 0;
+    UE_mac_inst[module_idP].scheduling_info.extendedPHR_r10 = 0;
+    UE_mac_inst[module_idP].scheduling_info.drx_config = NULL;
+    UE_mac_inst[module_idP].scheduling_info.phr_config = NULL;
+    // set init value 0xFFFF, make sure periodic timer and retx time counters are NOT active, after bsr transmission set the value configured by the NW.
+    UE_mac_inst[module_idP].scheduling_info.periodicBSR_SF =
+	MAC_UE_BSR_TIMER_NOT_RUNNING;
+    UE_mac_inst[module_idP].scheduling_info.retxBSR_SF =
+	MAC_UE_BSR_TIMER_NOT_RUNNING;
+    UE_mac_inst[module_idP].BSR_reporting_active = BSR_TRIGGER_NONE;
+
+    UE_mac_inst[module_idP].scheduling_info.periodicPHR_SF =
+	get_sf_perioidicPHR_Timer(UE_mac_inst[module_idP].
+				  scheduling_info.periodicPHR_Timer);
+    UE_mac_inst[module_idP].scheduling_info.prohibitPHR_SF =
+	get_sf_prohibitPHR_Timer(UE_mac_inst[module_idP].
+				 scheduling_info.prohibitPHR_Timer);
+    UE_mac_inst[module_idP].scheduling_info.PathlossChange_db =
+	get_db_dl_PathlossChange(UE_mac_inst[module_idP].
+				 scheduling_info.PathlossChange);
+    UE_mac_inst[module_idP].PHR_reporting_active = 0;
+
+    for (i = 0; i < MAX_NUM_LCID; i++) {
+	LOG_D(MAC,
+	      "[UE%d] Applying default logical channel config for LCGID %d\n",
+	      module_idP, i);
+	UE_mac_inst[module_idP].scheduling_info.Bj[i] = -1;
+	UE_mac_inst[module_idP].scheduling_info.bucket_size[i] = -1;
+
+	if (i < DTCH) {		// initilize all control channels lcgid to 0
+	    UE_mac_inst[module_idP].scheduling_info.LCGID[i] = 0;
+	} else {		// initialize all the data channels lcgid to 1
+	    UE_mac_inst[module_idP].scheduling_info.LCGID[i] = 1;
+	}
+
+	UE_mac_inst[module_idP].scheduling_info.LCID_status[i] =
+	    LCID_EMPTY;
+	UE_mac_inst[module_idP].scheduling_info.LCID_buffer_remain[i] = 0;
+    }
 
 #ifdef CBA
 
-  for (i=0; i <NUM_MAX_CBA_GROUP; i++) {
-    UE_mac_inst[module_idP].cba_last_access[i]= round(uniform_rngen(1,30));
-  }
+    for (i = 0; i < NUM_MAX_CBA_GROUP; i++) {
+	UE_mac_inst[module_idP].cba_last_access[i] =
+	    round(uniform_rngen(1, 30));
+    }
 
 #endif
 }
 
 
 unsigned char *parse_header(unsigned char *mac_header,
-                            unsigned char *num_ce,
-                            unsigned char *num_sdu,
-                            unsigned char *rx_ces,
-                            unsigned char *rx_lcids,
-                            unsigned short *rx_lengths,
-                            unsigned short tb_length)
+			    unsigned char *num_ce,
+			    unsigned char *num_sdu,
+			    unsigned char *rx_ces,
+			    unsigned char *rx_lcids,
+			    unsigned short *rx_lengths,
+			    unsigned short tb_length)
 {
 
-  unsigned char not_done=1,num_ces=0,num_cont_res = 0,num_padding = 0,num_sdus=0,lcid, num_sdu_cnt;
-  unsigned char *mac_header_ptr = mac_header;
-  unsigned short length,ce_len=0;
-
-  while (not_done==1) {
-
-    if (((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E == 0) {
-      //      printf("E=0\n");
-      not_done = 0;
-    }
-
-    lcid = ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID;
-
-    if (lcid < UE_CONT_RES) {
-      //printf("[MAC][UE] header %x.%x.%x\n",mac_header_ptr[0],mac_header_ptr[1],mac_header_ptr[2]);
-      if (not_done==0) {// last MAC SDU, length is implicit
-        mac_header_ptr++;
-        length = tb_length-(mac_header_ptr-mac_header)-ce_len;
-
-        for (num_sdu_cnt=0; num_sdu_cnt < num_sdus ; num_sdu_cnt++) {
-          length -= rx_lengths[num_sdu_cnt];
-        }
-      } else {
-        if (((SCH_SUBHEADER_LONG *)mac_header_ptr)->F == 1) {
-          length = ((((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_MSB & 0x7f ) << 8 ) | (((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_LSB & 0xff);
-          mac_header_ptr += 3;
+    unsigned char not_done = 1, num_ces = 0, num_cont_res =
+	0, num_padding = 0, num_sdus = 0, lcid, num_sdu_cnt;
+    unsigned char *mac_header_ptr = mac_header;
+    unsigned short length, ce_len = 0;
+
+    while (not_done == 1) {
+
+	if (((SCH_SUBHEADER_FIXED *) mac_header_ptr)->E == 0) {
+	    //      printf("E=0\n");
+	    not_done = 0;
+	}
+
+	lcid = ((SCH_SUBHEADER_FIXED *) mac_header_ptr)->LCID;
+
+	if (lcid < UE_CONT_RES) {
+	    //printf("[MAC][UE] header %x.%x.%x\n",mac_header_ptr[0],mac_header_ptr[1],mac_header_ptr[2]);
+	    if (not_done == 0) {	// last MAC SDU, length is implicit
+		mac_header_ptr++;
+		length =
+		    tb_length - (mac_header_ptr - mac_header) - ce_len;
+
+		for (num_sdu_cnt = 0; num_sdu_cnt < num_sdus;
+		     num_sdu_cnt++) {
+		    length -= rx_lengths[num_sdu_cnt];
+		}
+	    } else {
+		if (((SCH_SUBHEADER_LONG *) mac_header_ptr)->F == 1) {
+		    length =
+			((((SCH_SUBHEADER_LONG *) mac_header_ptr)->
+			  L_MSB & 0x7f)
+			 << 8) | (((SCH_SUBHEADER_LONG *) mac_header_ptr)->
+				  L_LSB & 0xff);
+		    mac_header_ptr += 3;
 #ifdef DEBUG_HEADER_PARSING
-          LOG_D(MAC,"[UE] parse long sdu, size %x \n",length);
+		    LOG_D(MAC, "[UE] parse long sdu, size %x \n", length);
 #endif
 
-        }  else { //if (((SCH_SUBHEADER_SHORT *)mac_header_ptr)->F == 0) {
-          length = ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->L;
-          mac_header_ptr += 2;
-        }
-      }
+		} else {	//if (((SCH_SUBHEADER_SHORT *)mac_header_ptr)->F == 0) {
+		    length = ((SCH_SUBHEADER_SHORT *) mac_header_ptr)->L;
+		    mac_header_ptr += 2;
+		}
+	    }
 
 #ifdef DEBUG_HEADER_PARSING
-      LOG_D(MAC,"[UE] sdu %d lcid %d length %d (offset now %ld)\n",
-            num_sdus,lcid,length,mac_header_ptr-mac_header);
+	    LOG_D(MAC, "[UE] sdu %d lcid %d length %d (offset now %ld)\n",
+		  num_sdus, lcid, length, mac_header_ptr - mac_header);
 #endif
-      rx_lcids[num_sdus] = lcid;
-      rx_lengths[num_sdus] = length;
-      num_sdus++;
-    } else { // This is a control element subheader
-      if (lcid == SHORT_PADDING) {
-    	num_padding ++;
-        mac_header_ptr++;
-      } else {
-        rx_ces[num_ces] = lcid;
-        num_ces++;
-        mac_header_ptr ++;
-
-        if (lcid==TIMING_ADV_CMD) {
-          ce_len++;
-        } else if (lcid==UE_CONT_RES) {
-
-        	// FNA: check MAC Header is one of thoses defined in Annex B of 36.321
-        	// Check there is only 1 Contention Resolution
-        	if (num_cont_res) {
-        		LOG_W(MAC,"[UE] Msg4 Wrong received format: More than 1 Contention Resolution\n");
-        		// exit parsing
-        		return NULL;
-
-        	}
-
-        	// UE_CONT_RES shall never be the last subheader unless this is the only MAC subheader
-        	if ((not_done == 0) && ((num_sdus) || (num_ces > 1) || (num_padding))) {
-        		LOG_W(MAC,"[UE] Msg4 Wrong received format: Contention Resolution after num_ces=%d num_sdus=%d num_padding=%d\n",num_ces,num_sdus,num_padding);
-        		// exit parsing
-        		return NULL;
-        	}
-          num_cont_res ++;
-          ce_len+=6;
-        }
-      }
+	    rx_lcids[num_sdus] = lcid;
+	    rx_lengths[num_sdus] = length;
+	    num_sdus++;
+	} else {		// This is a control element subheader
+	    if (lcid == SHORT_PADDING) {
+		num_padding++;
+		mac_header_ptr++;
+	    } else {
+		rx_ces[num_ces] = lcid;
+		num_ces++;
+		mac_header_ptr++;
+
+		if (lcid == TIMING_ADV_CMD) {
+		    ce_len++;
+		} else if (lcid == UE_CONT_RES) {
+
+		    // FNA: check MAC Header is one of thoses defined in Annex B of 36.321
+		    // Check there is only 1 Contention Resolution
+		    if (num_cont_res) {
+			LOG_W(MAC,
+			      "[UE] Msg4 Wrong received format: More than 1 Contention Resolution\n");
+			// exit parsing
+			return NULL;
+
+		    }
+		    // UE_CONT_RES shall never be the last subheader unless this is the only MAC subheader
+		    if ((not_done == 0)
+			&& ((num_sdus) || (num_ces > 1) || (num_padding))) {
+			LOG_W(MAC,
+			      "[UE] Msg4 Wrong received format: Contention Resolution after num_ces=%d num_sdus=%d num_padding=%d\n",
+			      num_ces, num_sdus, num_padding);
+			// exit parsing
+			return NULL;
+		    }
+		    num_cont_res++;
+		    ce_len += 6;
+		}
+	    }
 
 #ifdef DEBUG_HEADER_PARSING
-      LOG_D(MAC,"[UE] ce %d lcid %d (offset now %ld)\n",num_ces,lcid,mac_header_ptr-mac_header);
+	    LOG_D(MAC, "[UE] ce %d lcid %d (offset now %ld)\n", num_ces,
+		  lcid, mac_header_ptr - mac_header);
 #endif
+	}
     }
-  }
 
-  *num_ce = num_ces;
-  *num_sdu = num_sdus;
+    *num_ce = num_ces;
+    *num_sdu = num_sdus;
 
-  return(mac_header_ptr);
+    return (mac_header_ptr);
 }
 
-uint32_t ue_get_SR(module_id_t module_idP,int CC_id,frame_t frameP,uint8_t eNB_id,uint16_t rnti, sub_frame_t subframe)
+uint32_t
+ue_get_SR(module_id_t module_idP, int CC_id, frame_t frameP,
+	  uint8_t eNB_id, uint16_t rnti, sub_frame_t subframe)
 {
 
-  // no UL-SCH resources available for this tti && UE has a valid PUCCH resources for SR configuration for this tti
-  //  int MGL=6;// measurement gap length in ms
-  int MGRP       = 0; // measurement gap repetition period in ms
-  int gapOffset  = -1;
-  int T          = 0;
-
-  DevCheck(module_idP < (int)NB_UE_INST, module_idP, NB_UE_INST, 0);
-
-  AssertFatal(CC_id==0,
-	      "Transmission on secondary CCs is not supported yet\n");
-
-  // determin the measurement gap
-  if (UE_mac_inst[module_idP].measGapConfig !=NULL) {
-    if (UE_mac_inst[module_idP].measGapConfig->choice.setup.gapOffset.present == MeasGapConfig__setup__gapOffset_PR_gp0) {
-      MGRP= 40;
-      gapOffset= UE_mac_inst[module_idP].measGapConfig->choice.setup.gapOffset.choice.gp0;
-    } else if (UE_mac_inst[module_idP].measGapConfig->choice.setup.gapOffset.present == MeasGapConfig__setup__gapOffset_PR_gp1) {
-      MGRP= 80;
-      gapOffset= UE_mac_inst[module_idP].measGapConfig->choice.setup.gapOffset.choice.gp1;
-    } else {
-      LOG_W(MAC, "Measurement GAP offset is unknown\n");
+    // no UL-SCH resources available for this tti && UE has a valid PUCCH resources for SR configuration for this tti
+    //  int MGL=6;// measurement gap length in ms
+    int MGRP = 0;		// measurement gap repetition period in ms
+    int gapOffset = -1;
+    int T = 0;
+
+    DevCheck(module_idP < (int) NB_UE_INST, module_idP, NB_UE_INST, 0);
+
+    AssertFatal(CC_id == 0,
+		"Transmission on secondary CCs is not supported yet\n");
+
+    // determin the measurement gap
+    if (UE_mac_inst[module_idP].measGapConfig != NULL) {
+	if (UE_mac_inst[module_idP].measGapConfig->choice.setup.
+	    gapOffset.present == MeasGapConfig__setup__gapOffset_PR_gp0) {
+	    MGRP = 40;
+	    gapOffset =
+		UE_mac_inst[module_idP].measGapConfig->choice.
+		setup.gapOffset.choice.gp0;
+	} else if (UE_mac_inst[module_idP].measGapConfig->choice.
+		   setup.gapOffset.present ==
+		   MeasGapConfig__setup__gapOffset_PR_gp1) {
+	    MGRP = 80;
+	    gapOffset =
+		UE_mac_inst[module_idP].measGapConfig->choice.
+		setup.gapOffset.choice.gp1;
+	} else {
+	    LOG_W(MAC, "Measurement GAP offset is unknown\n");
+	}
+
+	T = MGRP / 10;
+	DevAssert(T != 0);
+
+	//check the measurement gap and sr prohibit timer
+	if ((subframe == gapOffset % 10)
+	    && ((frameP % T) == (floor(gapOffset / 10)))
+	    && (UE_mac_inst[module_idP].
+		scheduling_info.sr_ProhibitTimer_Running == 0)) {
+	    UE_mac_inst[module_idP].scheduling_info.SR_pending = 1;
+	    return (0);
+	}
     }
 
-    T=MGRP/10;
-    DevAssert( T != 0 );
-
-    //check the measurement gap and sr prohibit timer
-    if ((subframe ==  gapOffset %10) && ((frameP %T) == (floor(gapOffset/10)))
-        && (UE_mac_inst[module_idP].scheduling_info.sr_ProhibitTimer_Running ==0)) {
-      UE_mac_inst[module_idP].scheduling_info.SR_pending=1;
-      return(0);
-    }
-  }
-
-  if ((UE_mac_inst[module_idP].physicalConfigDedicated != NULL) &&
-      (UE_mac_inst[module_idP].scheduling_info.SR_pending==1) &&
-      (UE_mac_inst[module_idP].scheduling_info.SR_COUNTER <
-       (1<<(2+UE_mac_inst[module_idP].physicalConfigDedicated->schedulingRequestConfig->choice.setup.dsr_TransMax)))
-     ) {
-    LOG_D(MAC,"[UE %d][SR %x] Frame %d subframe %d PHY asks for SR (SR_COUNTER/dsr_TransMax %d/%d), SR_pending %d\n",
-          module_idP,rnti,frameP,subframe,
-          UE_mac_inst[module_idP].scheduling_info.SR_COUNTER,
-          (1<<(2+UE_mac_inst[module_idP].physicalConfigDedicated->schedulingRequestConfig->choice.setup.dsr_TransMax)),
-          UE_mac_inst[module_idP].scheduling_info.SR_pending);
-
-    UE_mac_inst[module_idP].scheduling_info.SR_COUNTER++;
-
-    // start the sr-prohibittimer : rel 9 and above
-    if (UE_mac_inst[module_idP].scheduling_info.sr_ProhibitTimer > 0) { // timer configured
-      UE_mac_inst[module_idP].scheduling_info.sr_ProhibitTimer--;
-      UE_mac_inst[module_idP].scheduling_info.sr_ProhibitTimer_Running=1;
+    if ((UE_mac_inst[module_idP].physicalConfigDedicated != NULL) &&
+	(UE_mac_inst[module_idP].scheduling_info.SR_pending == 1) &&
+	(UE_mac_inst[module_idP].scheduling_info.SR_COUNTER <
+	 (1 <<
+	  (2 +
+	   UE_mac_inst[module_idP].
+	   physicalConfigDedicated->schedulingRequestConfig->choice.setup.
+	   dsr_TransMax)))) {
+	LOG_D(MAC,
+	      "[UE %d][SR %x] Frame %d subframe %d PHY asks for SR (SR_COUNTER/dsr_TransMax %d/%d), SR_pending %d\n",
+	      module_idP, rnti, frameP, subframe,
+	      UE_mac_inst[module_idP].scheduling_info.SR_COUNTER,
+	      (1 <<
+	       (2 +
+		UE_mac_inst[module_idP].
+		physicalConfigDedicated->schedulingRequestConfig->choice.
+		setup.dsr_TransMax)),
+	      UE_mac_inst[module_idP].scheduling_info.SR_pending);
+
+	UE_mac_inst[module_idP].scheduling_info.SR_COUNTER++;
+
+	// start the sr-prohibittimer : rel 9 and above
+	if (UE_mac_inst[module_idP].scheduling_info.sr_ProhibitTimer > 0) {	// timer configured
+	    UE_mac_inst[module_idP].scheduling_info.sr_ProhibitTimer--;
+	    UE_mac_inst[module_idP].scheduling_info.
+		sr_ProhibitTimer_Running = 1;
+	} else {
+	    UE_mac_inst[module_idP].scheduling_info.
+		sr_ProhibitTimer_Running = 0;
+	}
+
+	LOG_D(MAC,
+	      "[UE %d][SR %x] Frame %d subframe %d send SR indication (SR_COUNTER/dsr_TransMax %d/%d), SR_pending %d\n",
+	      module_idP, rnti, frameP, subframe,
+	      UE_mac_inst[module_idP].scheduling_info.SR_COUNTER,
+	      (1 <<
+	       (2 +
+		UE_mac_inst[module_idP].
+		physicalConfigDedicated->schedulingRequestConfig->choice.
+		setup.dsr_TransMax)),
+	      UE_mac_inst[module_idP].scheduling_info.SR_pending);
+
+	//UE_mac_inst[module_idP].ul_active =1;
+	return (1);		//instruct phy to signal SR
     } else {
-      UE_mac_inst[module_idP].scheduling_info.sr_ProhibitTimer_Running=0;
-    }
-
-    LOG_D(MAC,"[UE %d][SR %x] Frame %d subframe %d send SR indication (SR_COUNTER/dsr_TransMax %d/%d), SR_pending %d\n",
-          module_idP,rnti,frameP,subframe,
-          UE_mac_inst[module_idP].scheduling_info.SR_COUNTER,
-          (1<<(2+UE_mac_inst[module_idP].physicalConfigDedicated->schedulingRequestConfig->choice.setup.dsr_TransMax)),
-          UE_mac_inst[module_idP].scheduling_info.SR_pending);
-
-    //UE_mac_inst[module_idP].ul_active =1;
-    return(1); //instruct phy to signal SR
-  } else {
-    // notify RRC to relase PUCCH/SRS
-    // clear any configured dl/ul
-    // initiate RA
-      if (UE_mac_inst[module_idP].scheduling_info.SR_pending){
-          // release all pucch resource
-          UE_mac_inst[module_idP].physicalConfigDedicated = NULL;
-          UE_mac_inst[module_idP].ul_active=0;
-          UE_mac_inst[module_idP].BSR_reporting_active=BSR_TRIGGER_NONE;
-
-          LOG_I(MAC,"[UE %d] Release all SRs \n", module_idP);
-      }
-    UE_mac_inst[module_idP].scheduling_info.SR_pending=0;
-    UE_mac_inst[module_idP].scheduling_info.SR_COUNTER=0;
-    return(0);
-  }
+	// notify RRC to relase PUCCH/SRS
+	// clear any configured dl/ul
+	// initiate RA
+	if (UE_mac_inst[module_idP].scheduling_info.SR_pending) {
+	    // release all pucch resource
+	    UE_mac_inst[module_idP].physicalConfigDedicated = NULL;
+	    UE_mac_inst[module_idP].ul_active = 0;
+	    UE_mac_inst[module_idP].BSR_reporting_active =
+		BSR_TRIGGER_NONE;
+
+	    LOG_I(MAC, "[UE %d] Release all SRs \n", module_idP);
+	}
+	UE_mac_inst[module_idP].scheduling_info.SR_pending = 0;
+	UE_mac_inst[module_idP].scheduling_info.SR_COUNTER = 0;
+	return (0);
+    }
 }
 
 //------------------------------------------------------------------------------
 void
-ue_send_sdu(
-  module_id_t module_idP,
+ue_send_sdu(module_id_t module_idP,
 	    uint8_t CC_id,
 	    frame_t frameP,
-        sub_frame_t subframeP,
-	    uint8_t* sdu,
-	    uint16_t sdu_len,
-  uint8_t eNB_index
-)
+	    sub_frame_t subframeP,
+	    uint8_t * sdu, uint16_t sdu_len, uint8_t eNB_index)
 //------------------------------------------------------------------------------
 {
 
-  unsigned char rx_ces[MAX_NUM_CE],num_ce,num_sdu,i,*payload_ptr;
-  unsigned char rx_lcids[NB_RB_MAX];
-  unsigned short rx_lengths[NB_RB_MAX];
-  unsigned char *tx_sdu;
+    unsigned char rx_ces[MAX_NUM_CE], num_ce, num_sdu, i, *payload_ptr;
+    unsigned char rx_lcids[NB_RB_MAX];
+    unsigned short rx_lengths[NB_RB_MAX];
+    unsigned char *tx_sdu;
 #if UE_TIMING_TRACE
-  start_meas(&UE_mac_inst[module_idP].rx_dlsch_sdu);
+    start_meas(&UE_mac_inst[module_idP].rx_dlsch_sdu);
 #endif
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SEND_SDU, VCD_FUNCTION_IN);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SEND_SDU, VCD_FUNCTION_IN);
 
-  LOG_T(MAC,"sdu: %x.%x.%x\n",sdu[0],sdu[1],sdu[2]);
+    LOG_T(MAC, "sdu: %x.%x.%x\n", sdu[0], sdu[1], sdu[2]);
 
-  if (opt_enabled) {
-    trace_pdu(1, sdu, sdu_len, module_idP, 3, UE_mac_inst[module_idP].crnti,
-        frameP, subframeP, 0, 0);
-    LOG_D(OPT,"[UE %d][DLSCH] Frame %d trace pdu for rnti %x  with size %d\n",
-          module_idP, frameP, UE_mac_inst[module_idP].crnti, sdu_len);
-  }
+    if (opt_enabled) {
+	trace_pdu(1, sdu, sdu_len, module_idP, 3,
+		  UE_mac_inst[module_idP].crnti, frameP, subframeP, 0, 0);
+	LOG_D(OPT,
+	      "[UE %d][DLSCH] Frame %d trace pdu for rnti %x  with size %d\n",
+	      module_idP, frameP, UE_mac_inst[module_idP].crnti, sdu_len);
+    }
 
-  payload_ptr = parse_header(sdu,&num_ce,&num_sdu,rx_ces,rx_lcids,rx_lengths,sdu_len);
+    payload_ptr =
+	parse_header(sdu, &num_ce, &num_sdu, rx_ces, rx_lcids, rx_lengths,
+		     sdu_len);
 
 #ifdef DEBUG_HEADER_PARSING
-  LOG_D(MAC,"[UE %d] ue_send_sdu : Frame %d eNB_index %d : num_ce %d num_sdu %d\n",module_idP,
-        frameP,eNB_index,num_ce,num_sdu);
+    LOG_D(MAC,
+	  "[UE %d] ue_send_sdu : Frame %d eNB_index %d : num_ce %d num_sdu %d\n",
+	  module_idP, frameP, eNB_index, num_ce, num_sdu);
 #endif
 
 #if defined(ENABLE_MAC_PAYLOAD_DEBUG)
-  LOG_T(MAC,"[UE %d] First 32 bytes of DLSCH : \n", module_idP);
+    LOG_T(MAC, "[UE %d] First 32 bytes of DLSCH : \n", module_idP);
 
-  for (i=0; i<32; i++) {
-    LOG_T(MAC,"%x.",sdu[i]);
-  }
+    for (i = 0; i < 32; i++) {
+	LOG_T(MAC, "%x.", sdu[i]);
+    }
 
-  LOG_T(MAC,"\n");
+    LOG_T(MAC, "\n");
 #endif
 
-  if (payload_ptr != NULL) {
-
-  for (i=0; i<num_ce; i++) {
-    //    printf("ce %d : %d\n",i,rx_ces[i]);
-    switch (rx_ces[i]) {
-    case UE_CONT_RES:
-
-      LOG_I(MAC,"[UE %d][RAPROC] Frame %d : received contention resolution msg: %x.%x.%x.%x.%x.%x, Terminating RA procedure\n",
-            module_idP,frameP,payload_ptr[0],payload_ptr[1],payload_ptr[2],payload_ptr[3],payload_ptr[4],payload_ptr[5]);
-
-      if (UE_mac_inst[module_idP].RA_active == 1) {
-        LOG_I(MAC,"[UE %d][RAPROC] Frame %d : Clearing RA_active flag\n", module_idP, frameP);
-        UE_mac_inst[module_idP].RA_active=0;
-        // check if RA procedure has finished completely (no contention)
-        tx_sdu = &UE_mac_inst[module_idP].CCCH_pdu.payload[3];
-
-        //Note: 3 assumes sizeof(SCH_SUBHEADER_SHORT) + PADDING CE, which is when UL-Grant has TBS >= 9 (64 bits)
-        // (other possibility is 1 for TBS=7 (SCH_SUBHEADER_FIXED), or 2 for TBS=8 (SCH_SUBHEADER_FIXED+PADDING or SCH_SUBHEADER_SHORT)
-        for (i=0; i<6; i++)
-          if (tx_sdu[i] != payload_ptr[i]) {
-            LOG_E(MAC,"[UE %d][RAPROC] Contention detected, RA failed\n",module_idP);
-            ra_failed(module_idP,CC_id,eNB_index);
-            UE_mac_inst[module_idP].RA_contention_resolution_timer_active = 0;
-            VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SEND_SDU, VCD_FUNCTION_OUT);
-            return;
-          }
-
-        LOG_I(MAC,"[UE %d][RAPROC] Frame %d : Clearing contention resolution timer\n", module_idP, frameP);
-        UE_mac_inst[module_idP].RA_contention_resolution_timer_active = 0;
-        ra_succeeded(module_idP,CC_id,eNB_index);
-      }
-
-      payload_ptr+=6;
-      break;
-
-    case TIMING_ADV_CMD:
+    if (payload_ptr != NULL) {
+
+	for (i = 0; i < num_ce; i++) {
+	    //    printf("ce %d : %d\n",i,rx_ces[i]);
+	    switch (rx_ces[i]) {
+	    case UE_CONT_RES:
+
+		LOG_I(MAC,
+		      "[UE %d][RAPROC] Frame %d : received contention resolution msg: %x.%x.%x.%x.%x.%x, Terminating RA procedure\n",
+		      module_idP, frameP, payload_ptr[0], payload_ptr[1],
+		      payload_ptr[2], payload_ptr[3], payload_ptr[4],
+		      payload_ptr[5]);
+
+		if (UE_mac_inst[module_idP].RA_active == 1) {
+		    LOG_I(MAC,
+			  "[UE %d][RAPROC] Frame %d : Clearing RA_active flag\n",
+			  module_idP, frameP);
+		    UE_mac_inst[module_idP].RA_active = 0;
+		    // check if RA procedure has finished completely (no contention)
+		    tx_sdu = &UE_mac_inst[module_idP].CCCH_pdu.payload[3];
+
+		    //Note: 3 assumes sizeof(SCH_SUBHEADER_SHORT) + PADDING CE, which is when UL-Grant has TBS >= 9 (64 bits)
+		    // (other possibility is 1 for TBS=7 (SCH_SUBHEADER_FIXED), or 2 for TBS=8 (SCH_SUBHEADER_FIXED+PADDING or SCH_SUBHEADER_SHORT)
+		    for (i = 0; i < 6; i++)
+			if (tx_sdu[i] != payload_ptr[i]) {
+			    LOG_E(MAC,
+				  "[UE %d][RAPROC] Contention detected, RA failed\n",
+				  module_idP);
+			    ra_failed(module_idP, CC_id, eNB_index);
+			    UE_mac_inst
+				[module_idP].
+				RA_contention_resolution_timer_active = 0;
+			    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+				(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SEND_SDU,
+				 VCD_FUNCTION_OUT);
+			    return;
+			}
+
+		    LOG_I(MAC,
+			  "[UE %d][RAPROC] Frame %d : Clearing contention resolution timer\n",
+			  module_idP, frameP);
+		    UE_mac_inst
+			[module_idP].
+			RA_contention_resolution_timer_active = 0;
+		    ra_succeeded(module_idP, CC_id, eNB_index);
+		}
+
+		payload_ptr += 6;
+		break;
+
+	    case TIMING_ADV_CMD:
 #ifdef DEBUG_HEADER_PARSING
-      LOG_D(MAC,"[UE] CE %d : UE Timing Advance : %d\n",i,payload_ptr[0]);
+		LOG_D(MAC, "[UE] CE %d : UE Timing Advance : %d\n", i,
+		      payload_ptr[0]);
 #endif
-      process_timing_advance(module_idP,CC_id,payload_ptr[0]);
-      payload_ptr++;
-      break;
+		process_timing_advance(module_idP, CC_id, payload_ptr[0]);
+		payload_ptr++;
+		break;
 
-    case DRX_CMD:
+	    case DRX_CMD:
 #ifdef DEBUG_HEADER_PARSING
-      LOG_D(MAC,"[UE] CE %d : UE DRX :",i);
+		LOG_D(MAC, "[UE] CE %d : UE DRX :", i);
 #endif
-      payload_ptr++;
-      break;
-    }
-  }
+		payload_ptr++;
+		break;
+	    }
+	}
 
-  for (i=0; i<num_sdu; i++) {
+	for (i = 0; i < num_sdu; i++) {
 #ifdef DEBUG_HEADER_PARSING
-    LOG_D(MAC,"[UE] SDU %d : LCID %d, length %d\n",i,rx_lcids[i],rx_lengths[i]);
+	    LOG_D(MAC, "[UE] SDU %d : LCID %d, length %d\n", i,
+		  rx_lcids[i], rx_lengths[i]);
 #endif
-    
-    if (rx_lcids[i] == CCCH) {
 
-      LOG_D(MAC,"[UE %d] rnti %x Frame %d : DLSCH -> DL-CCCH, RRC message (eNB %d, %d bytes)\n",
-            module_idP,
-            UE_mac_inst[module_idP].crnti,
-            frameP,
-            eNB_index,
-            rx_lengths[i]);
+	    if (rx_lcids[i] == CCCH) {
+
+		LOG_D(MAC,
+		      "[UE %d] rnti %x Frame %d : DLSCH -> DL-CCCH, RRC message (eNB %d, %d bytes)\n",
+		      module_idP, UE_mac_inst[module_idP].crnti, frameP,
+		      eNB_index, rx_lengths[i]);
 
 #if defined(ENABLE_MAC_PAYLOAD_DEBUG)
-      int j;
+		int j;
 
-      for (j=0; j<rx_lengths[i]; j++) {
-        LOG_T(MAC,"%x.",(uint8_t)payload_ptr[j]);
-      }
+		for (j = 0; j < rx_lengths[i]; j++) {
+		    LOG_T(MAC, "%x.", (uint8_t) payload_ptr[j]);
+		}
 
-      LOG_T(MAC,"\n");
+		LOG_T(MAC, "\n");
 #endif
-      mac_rrc_data_ind(module_idP,
-                       CC_id,
-                       frameP,subframeP,
-                       UE_mac_inst[module_idP].crnti,
-                       CCCH,
-                       (uint8_t*)payload_ptr,
-                       rx_lengths[i],
-                       ENB_FLAG_NO,
-                       eNB_index,
-                       0);
-
-    } else if ((rx_lcids[i] == DCCH) || (rx_lcids[i] == DCCH1)) {
-      LOG_D(MAC,"[UE %d] Frame %d : DLSCH -> DL-DCCH%d, RRC message (eNB %d, %d bytes)\n", module_idP, frameP, rx_lcids[i],eNB_index,rx_lengths[i]);
-      mac_rlc_data_ind(module_idP,
-                       UE_mac_inst[module_idP].crnti,
-		       eNB_index,
-                       frameP,
-                       ENB_FLAG_NO,
-                       MBMS_FLAG_NO,
-                       rx_lcids[i],
-                       (char *)payload_ptr,
-                       rx_lengths[i],
-                       1,
-                       NULL);
- 
-    } else if ((rx_lcids[i]  < NB_RB_MAX) && (rx_lcids[i] > DCCH1 )) {
-      
-      LOG_D(MAC,"[UE %d] Frame %d : DLSCH -> DL-DTCH%d (eNB %d, %d bytes)\n", module_idP, frameP,rx_lcids[i], eNB_index,rx_lengths[i]);
+		mac_rrc_data_ind(module_idP,
+				 CC_id,
+				 frameP, subframeP,
+				 UE_mac_inst[module_idP].crnti,
+				 CCCH,
+				 (uint8_t *) payload_ptr,
+				 rx_lengths[i], ENB_FLAG_NO, eNB_index, 0);
+
+	    } else if ((rx_lcids[i] == DCCH) || (rx_lcids[i] == DCCH1)) {
+		LOG_D(MAC,
+		      "[UE %d] Frame %d : DLSCH -> DL-DCCH%d, RRC message (eNB %d, %d bytes)\n",
+		      module_idP, frameP, rx_lcids[i], eNB_index,
+		      rx_lengths[i]);
+		mac_rlc_data_ind(module_idP, UE_mac_inst[module_idP].crnti,
+				 eNB_index, frameP, ENB_FLAG_NO,
+				 MBMS_FLAG_NO, rx_lcids[i],
+				 (char *) payload_ptr, rx_lengths[i], 1,
+				 NULL);
+
+	    } else if ((rx_lcids[i] < NB_RB_MAX) && (rx_lcids[i] > DCCH1)) {
+
+		LOG_D(MAC,
+		      "[UE %d] Frame %d : DLSCH -> DL-DTCH%d (eNB %d, %d bytes)\n",
+		      module_idP, frameP, rx_lcids[i], eNB_index,
+		      rx_lengths[i]);
 
 #if defined(ENABLE_MAC_PAYLOAD_DEBUG)
-      int j;
-      for (j=0;j<rx_lengths[i];j++)
-	LOG_T(MAC,"%x.",(unsigned char)payload_ptr[j]);
-      LOG_T(MAC,"\n");
+		int j;
+		for (j = 0; j < rx_lengths[i]; j++)
+		    LOG_T(MAC, "%x.", (unsigned char) payload_ptr[j]);
+		LOG_T(MAC, "\n");
 #endif
-      mac_rlc_data_ind(module_idP,
-		       UE_mac_inst[module_idP].crnti,
-		       eNB_index,
-		       frameP,
-		       ENB_FLAG_NO,
-		       MBMS_FLAG_NO,
-		       rx_lcids[i],
-		       (char *)payload_ptr,
-		       rx_lengths[i],
-		       1,
-		       NULL);
-    } else {
-      LOG_E(MAC,"[UE %d] Frame %d : unknown LCID %d (eNB %d)\n", module_idP, frameP,rx_lcids[i], eNB_index);
-    }
-    payload_ptr+= rx_lengths[i];
-  }
-  } // end if (payload_ptr != NULL)
-  
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SEND_SDU, VCD_FUNCTION_OUT);
+		mac_rlc_data_ind(module_idP,
+				 UE_mac_inst[module_idP].crnti,
+				 eNB_index,
+				 frameP,
+				 ENB_FLAG_NO,
+				 MBMS_FLAG_NO,
+				 rx_lcids[i],
+				 (char *) payload_ptr, rx_lengths[i], 1,
+				 NULL);
+	    } else {
+		LOG_E(MAC, "[UE %d] Frame %d : unknown LCID %d (eNB %d)\n",
+		      module_idP, frameP, rx_lcids[i], eNB_index);
+	    }
+	    payload_ptr += rx_lengths[i];
+	}
+    }				// end if (payload_ptr != NULL)
+
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SEND_SDU, VCD_FUNCTION_OUT);
 #if UE_TIMING_TRACE
-  stop_meas(&UE_mac_inst[module_idP].rx_dlsch_sdu);
+    stop_meas(&UE_mac_inst[module_idP].rx_dlsch_sdu);
 #endif
 }
 
-void ue_decode_si(module_id_t module_idP,int CC_id,frame_t frameP, uint8_t eNB_index, void *pdu,uint16_t len)
+void
+ue_decode_si(module_id_t module_idP, int CC_id, frame_t frameP,
+	     uint8_t eNB_index, void *pdu, uint16_t len)
 {
 #if UE_TIMING_TRACE
     start_meas(&UE_mac_inst[module_idP].rx_si);
 #endif
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_SI, VCD_FUNCTION_IN);
-
-  LOG_D(MAC,"[UE %d] Frame %d Sending SI to RRC (LCID Id %d,len %d)\n",module_idP,frameP,BCCH,len);
-
-  mac_rrc_data_ind(module_idP,
-                   CC_id,
-                   frameP,0, // unknown subframe
-                   SI_RNTI,
-                   BCCH,
-                   (uint8_t *)pdu,
-                   len,
-                   ENB_FLAG_NO,
-                   eNB_index,
-                   0);
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_SI, VCD_FUNCTION_OUT);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_SI, VCD_FUNCTION_IN);
+
+    LOG_D(MAC, "[UE %d] Frame %d Sending SI to RRC (LCID Id %d,len %d)\n",
+	  module_idP, frameP, BCCH, len);
+
+    mac_rrc_data_ind(module_idP, CC_id, frameP, 0,	// unknown subframe
+		     SI_RNTI,
+		     BCCH, (uint8_t *) pdu, len, ENB_FLAG_NO, eNB_index,
+		     0);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_SI, VCD_FUNCTION_OUT);
 #if UE_TIMING_TRACE
-  stop_meas(&UE_mac_inst[module_idP].rx_si);
+    stop_meas(&UE_mac_inst[module_idP].rx_si);
 #endif
-  if (opt_enabled == 1) {
-    trace_pdu(0,
-	      (uint8_t *)pdu,
-	      len,
-	      module_idP,
-	      4,
-	      0xffff,
-	      UE_mac_inst[module_idP].rxFrame,
-	      UE_mac_inst[module_idP].rxSubframe,
-	      0,
-	      0);
-    LOG_D(OPT,"[UE %d][BCH] Frame %d trace pdu for CC_id %d rnti %x with size %d\n",
-	    module_idP, frameP, CC_id, 0xffff, len);
-  }
+    if (opt_enabled == 1) {
+	trace_pdu(0,
+		  (uint8_t *) pdu,
+		  len,
+		  module_idP,
+		  4,
+		  0xffff,
+		  UE_mac_inst[module_idP].rxFrame,
+		  UE_mac_inst[module_idP].rxSubframe, 0, 0);
+	LOG_D(OPT,
+	      "[UE %d][BCH] Frame %d trace pdu for CC_id %d rnti %x with size %d\n",
+	      module_idP, frameP, CC_id, 0xffff, len);
+    }
 }
 
-void ue_decode_p(module_id_t module_idP,int CC_id,frame_t frameP, uint8_t eNB_index, void *pdu,uint16_t len)
+void
+ue_decode_p(module_id_t module_idP, int CC_id, frame_t frameP,
+	    uint8_t eNB_index, void *pdu, uint16_t len)
 {
 #if UE_TIMING_TRACE
     start_meas(&UE_mac_inst[module_idP].rx_p);
 #endif
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_PCCH, VCD_FUNCTION_IN);
-
-  LOG_D(MAC,"[UE %d] Frame %d Sending Paging message to RRC (LCID Id %d,len %d)\n",module_idP,frameP,PCCH,len);
-
-  mac_rrc_data_ind(module_idP,
-                   CC_id,
-                   frameP,0, // unknown subframe
-                   P_RNTI,
-                   PCCH,
-                   (uint8_t *)pdu,
-                   len,
-                   ENB_FLAG_NO,
-                   eNB_index,
-                   0);
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_PCCH, VCD_FUNCTION_OUT);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_PCCH, VCD_FUNCTION_IN);
+
+    LOG_D(MAC,
+	  "[UE %d] Frame %d Sending Paging message to RRC (LCID Id %d,len %d)\n",
+	  module_idP, frameP, PCCH, len);
+
+    mac_rrc_data_ind(module_idP, CC_id, frameP, 0,	// unknown subframe
+		     P_RNTI,
+		     PCCH, (uint8_t *) pdu, len, ENB_FLAG_NO, eNB_index,
+		     0);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_PCCH, VCD_FUNCTION_OUT);
 #if UE_TIMING_TRACE
-  stop_meas(&UE_mac_inst[module_idP].rx_p);
+    stop_meas(&UE_mac_inst[module_idP].rx_p);
 #endif
-  if (opt_enabled == 1) {
-    trace_pdu(0,
-	      (uint8_t *)pdu,
-	      len,
-	      module_idP,
-	      4,
-	      P_RNTI,
-	      UE_mac_inst[module_idP].rxFrame,
-	      UE_mac_inst[module_idP].rxSubframe,
-	      0,
-	      0);
-    LOG_D(OPT,"[UE %d][BCH] Frame %d trace pdu for CC_id %d rnti %x with size %d\n",
-	    module_idP, frameP, CC_id, P_RNTI, len);
-  }
+    if (opt_enabled == 1) {
+	trace_pdu(0,
+		  (uint8_t *) pdu,
+		  len,
+		  module_idP,
+		  4,
+		  P_RNTI,
+		  UE_mac_inst[module_idP].rxFrame,
+		  UE_mac_inst[module_idP].rxSubframe, 0, 0);
+	LOG_D(OPT,
+	      "[UE %d][BCH] Frame %d trace pdu for CC_id %d rnti %x with size %d\n",
+	      module_idP, frameP, CC_id, P_RNTI, len);
+    }
 }
 
 #if defined(Rel10) || defined(Rel14)
 unsigned char *parse_mch_header(unsigned char *mac_header,
-                                unsigned char *num_sdu,
-                                unsigned char *rx_lcids,
-                                unsigned short *rx_lengths,
-                                unsigned short tb_length)
+				unsigned char *num_sdu,
+				unsigned char *rx_lcids,
+				unsigned short *rx_lengths,
+				unsigned short tb_length)
 {
-  unsigned char not_done=1, num_sdus=0, lcid, i;
-  unsigned char *mac_header_ptr = mac_header;
-  unsigned short length;
-
-  while (not_done == 1) {
-    if (((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E == 0) {
-      not_done = 0;
-    }
-
-    lcid = ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID;
-
-    if (lcid < SHORT_PADDING) {// subheader for MSI, MCCH or MTCH
-      if (not_done == 0) { // last MAC SDU, length is implicit
-        mac_header_ptr++;
-        length = tb_length- (mac_header_ptr - mac_header);
-
-        for (i=0; i<num_sdus; i++) {
-          length -= rx_lengths[i];
-        }
-      } else { // not the last MAC SDU
-        if ( ((SCH_SUBHEADER_LONG *)mac_header_ptr)->F == 1) {// subheader has length of 3octets
-          //    length = ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L;
-          length = ((((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_MSB & 0x7f ) << 8 ) | (((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_LSB & 0xff);
-          mac_header_ptr += 3;
-        } else { // subheader has length of 2octets
-          length = ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->L;
-          mac_header_ptr += 2;
-        }
-      }
-
-      rx_lcids[num_sdus] = lcid;
-      rx_lengths[num_sdus] = length;
-      num_sdus++;
-    } else { // subheader for padding
-      //     if (lcid == SHORT_PADDING)
-      mac_header_ptr++;
-    }
-  }
-
-  *num_sdu = num_sdus;
-  return(mac_header_ptr);
+    unsigned char not_done = 1, num_sdus = 0, lcid, i;
+    unsigned char *mac_header_ptr = mac_header;
+    unsigned short length;
+
+    while (not_done == 1) {
+	if (((SCH_SUBHEADER_FIXED *) mac_header_ptr)->E == 0) {
+	    not_done = 0;
+	}
+
+	lcid = ((SCH_SUBHEADER_FIXED *) mac_header_ptr)->LCID;
+
+	if (lcid < SHORT_PADDING) {	// subheader for MSI, MCCH or MTCH
+	    if (not_done == 0) {	// last MAC SDU, length is implicit
+		mac_header_ptr++;
+		length = tb_length - (mac_header_ptr - mac_header);
+
+		for (i = 0; i < num_sdus; i++) {
+		    length -= rx_lengths[i];
+		}
+	    } else {		// not the last MAC SDU
+		if (((SCH_SUBHEADER_LONG *) mac_header_ptr)->F == 1) {	// subheader has length of 3octets
+		    //    length = ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L;
+		    length =
+			((((SCH_SUBHEADER_LONG *) mac_header_ptr)->
+			  L_MSB & 0x7f)
+			 << 8) | (((SCH_SUBHEADER_LONG *) mac_header_ptr)->
+				  L_LSB & 0xff);
+		    mac_header_ptr += 3;
+		} else {	// subheader has length of 2octets
+		    length = ((SCH_SUBHEADER_SHORT *) mac_header_ptr)->L;
+		    mac_header_ptr += 2;
+		}
+	    }
+
+	    rx_lcids[num_sdus] = lcid;
+	    rx_lengths[num_sdus] = length;
+	    num_sdus++;
+	} else {		// subheader for padding
+	    //     if (lcid == SHORT_PADDING)
+	    mac_header_ptr++;
+	}
+    }
+
+    *num_sdu = num_sdus;
+    return (mac_header_ptr);
 }
 
 // this function is for sending mch_sdu from phy to mac
-void ue_send_mch_sdu(module_id_t module_idP, uint8_t CC_id, frame_t frameP, uint8_t *sdu, uint16_t sdu_len, uint8_t eNB_index, uint8_t sync_area)
+void
+ue_send_mch_sdu(module_id_t module_idP, uint8_t CC_id, frame_t frameP,
+		uint8_t * sdu, uint16_t sdu_len, uint8_t eNB_index,
+		uint8_t sync_area)
 {
 
-  unsigned char num_sdu, i, *payload_ptr;
-  unsigned char rx_lcids[NB_RB_MAX];
-  unsigned short rx_lengths[NB_RB_MAX];
+    unsigned char num_sdu, i, *payload_ptr;
+    unsigned char rx_lcids[NB_RB_MAX];
+    unsigned short rx_lengths[NB_RB_MAX];
 #if UE_TIMING_TRACE
-  start_meas(&UE_mac_inst[module_idP].rx_mch_sdu);
+    start_meas(&UE_mac_inst[module_idP].rx_mch_sdu);
 #endif
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SEND_MCH_SDU, VCD_FUNCTION_IN);
-
-  LOG_D(MAC,"[UE %d] Frame %d : process the mch PDU for sync area %d \n",module_idP,frameP, sync_area);
-  LOG_D(MAC,"[UE %d] sdu: %x.%x\n", module_idP,sdu[0], sdu[1]);
-  LOG_D(MAC,"[UE %d] parse_mch_header, demultiplex\n",module_idP);
-
-  payload_ptr = parse_mch_header(sdu, &num_sdu, rx_lcids, rx_lengths, sdu_len);
-  LOG_D(MAC,"[UE %d] parse_mch_header, found %d sdus\n",module_idP,num_sdu);
-
-  for (i=0; i<num_sdu; i++) {
-    if (rx_lcids[i] == MCH_SCHDL_INFO) {
-      if (UE_mac_inst[module_idP].mcch_status==1) {
-        LOG_I(MAC,"[UE %d] Frame %d : MCH->MSI for sync area %d (eNB %d, %d bytes)\n",module_idP,frameP, sync_area, eNB_index, rx_lengths[i]);
-        // ??store necessary scheduling info to ue_mac_inst in order to
-        // calculate exact position of interested service (for the complex case has >1 mtch)
-        // set msi_status to 1
-        UE_mac_inst[module_idP].msi_status = 1;
-      }
-    } else if (rx_lcids[i] == MCCH_LCHANID) {
-      LOG_I(MAC,"[UE %d] Frame %d : SDU %d MCH->MCCH for sync area %d (eNB %d, %d bytes)\n",module_idP,frameP, i, sync_area, eNB_index, rx_lengths[i]);
-      mac_rrc_data_ind(module_idP,
-                       CC_id,
-                       frameP,0, // unknown subframe
-                       M_RNTI,
-                       MCCH,
-                       payload_ptr, rx_lengths[i], 0, eNB_index, sync_area);
-    } else if (rx_lcids[i] == MTCH) {
-      if (UE_mac_inst[module_idP].msi_status==1) {
-        LOG_I(MAC,"[UE %d] Frame %d : MCH->MTCH for sync area %d (eNB %d, %d bytes)\n",module_idP,frameP, sync_area, eNB_index, rx_lengths[i]);
-
-        mac_rlc_data_ind(
-          module_idP,
-          UE_mac_inst[module_idP].crnti,
-          eNB_index,
-	  frameP,
-          ENB_FLAG_NO,
-          MBMS_FLAG_YES,
-          MTCH, /*+ (maxDRB + 3),*/
-          (char *)payload_ptr,
-          rx_lengths[i],
-          1,
-          NULL);
-
-      }
-    } else {
-      LOG_W(MAC,"[UE %d] Frame %d : unknown sdu %d rx_lcids[%d]=%d mcch status %d eNB %d \n",
-            module_idP,
-            frameP,
-            rx_lengths[i],
-            i,
-            rx_lcids[i],
-            UE_mac_inst[module_idP].mcch_status, eNB_index);
-    }
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SEND_MCH_SDU, VCD_FUNCTION_IN);
 
-    payload_ptr += rx_lengths[i];
-  }
+    LOG_D(MAC,
+	  "[UE %d] Frame %d : process the mch PDU for sync area %d \n",
+	  module_idP, frameP, sync_area);
+    LOG_D(MAC, "[UE %d] sdu: %x.%x\n", module_idP, sdu[0], sdu[1]);
+    LOG_D(MAC, "[UE %d] parse_mch_header, demultiplex\n", module_idP);
+
+    payload_ptr =
+	parse_mch_header(sdu, &num_sdu, rx_lcids, rx_lengths, sdu_len);
+    LOG_D(MAC, "[UE %d] parse_mch_header, found %d sdus\n", module_idP,
+	  num_sdu);
+
+    for (i = 0; i < num_sdu; i++) {
+	if (rx_lcids[i] == MCH_SCHDL_INFO) {
+	    if (UE_mac_inst[module_idP].mcch_status == 1) {
+		LOG_I(MAC,
+		      "[UE %d] Frame %d : MCH->MSI for sync area %d (eNB %d, %d bytes)\n",
+		      module_idP, frameP, sync_area, eNB_index,
+		      rx_lengths[i]);
+		// ??store necessary scheduling info to ue_mac_inst in order to
+		// calculate exact position of interested service (for the complex case has >1 mtch)
+		// set msi_status to 1
+		UE_mac_inst[module_idP].msi_status = 1;
+	    }
+	} else if (rx_lcids[i] == MCCH_LCHANID) {
+	    LOG_I(MAC,
+		  "[UE %d] Frame %d : SDU %d MCH->MCCH for sync area %d (eNB %d, %d bytes)\n",
+		  module_idP, frameP, i, sync_area, eNB_index,
+		  rx_lengths[i]);
+	    mac_rrc_data_ind(module_idP, CC_id, frameP, 0,	// unknown subframe
+			     M_RNTI,
+			     MCCH,
+			     payload_ptr, rx_lengths[i], 0, eNB_index,
+			     sync_area);
+	} else if (rx_lcids[i] == MTCH) {
+	    if (UE_mac_inst[module_idP].msi_status == 1) {
+		LOG_I(MAC,
+		      "[UE %d] Frame %d : MCH->MTCH for sync area %d (eNB %d, %d bytes)\n",
+		      module_idP, frameP, sync_area, eNB_index,
+		      rx_lengths[i]);
+
+		mac_rlc_data_ind(module_idP, UE_mac_inst[module_idP].crnti, eNB_index, frameP, ENB_FLAG_NO, MBMS_FLAG_YES, MTCH,	/*+ (maxDRB + 3), */
+				 (char *) payload_ptr, rx_lengths[i], 1,
+				 NULL);
+
+	    }
+	} else {
+	    LOG_W(MAC,
+		  "[UE %d] Frame %d : unknown sdu %d rx_lcids[%d]=%d mcch status %d eNB %d \n",
+		  module_idP, frameP, rx_lengths[i], i, rx_lcids[i],
+		  UE_mac_inst[module_idP].mcch_status, eNB_index);
+	}
+
+	payload_ptr += rx_lengths[i];
+    }
 
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SEND_MCH_SDU, VCD_FUNCTION_OUT);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SEND_MCH_SDU, VCD_FUNCTION_OUT);
 #if UE_TIMING_TRACE
-  stop_meas(&UE_mac_inst[module_idP].rx_mch_sdu);
+    stop_meas(&UE_mac_inst[module_idP].rx_mch_sdu);
 #endif
 }
 
-int8_t ue_get_mbsfn_sf_alloction (module_id_t module_idP, uint8_t mbsfn_sync_area, unsigned char eNB_index)
+int8_t
+ue_get_mbsfn_sf_alloction(module_id_t module_idP,
+			  uint8_t mbsfn_sync_area, unsigned char eNB_index)
 {
-  // currently there is one-to-one mapping between sf allocation pattern and sync area
-  if (mbsfn_sync_area >= MAX_MBSFN_AREA) {
-    LOG_W( MAC, "[UE %"PRIu8"] MBSFN synchronization area %"PRIu8" out of range for eNB %"PRIu8"\n", module_idP, mbsfn_sync_area, eNB_index );
-    return -1;
-  } else if (UE_mac_inst[module_idP].mbsfn_SubframeConfig[mbsfn_sync_area] != NULL) {
-    return mbsfn_sync_area;
-  } else {
-    LOG_W( MAC, "[UE %"PRIu8"] MBSFN Subframe Config pattern %"PRIu8" not found \n", module_idP, mbsfn_sync_area );
-    return -1;
-  }
+    // currently there is one-to-one mapping between sf allocation pattern and sync area
+    if (mbsfn_sync_area >= MAX_MBSFN_AREA) {
+	LOG_W(MAC,
+	      "[UE %" PRIu8 "] MBSFN synchronization area %" PRIu8
+	      " out of range for eNB %" PRIu8 "\n", module_idP,
+	      mbsfn_sync_area, eNB_index);
+	return -1;
+    } else if (UE_mac_inst[module_idP].
+	       mbsfn_SubframeConfig[mbsfn_sync_area] != NULL) {
+	return mbsfn_sync_area;
+    } else {
+	LOG_W(MAC,
+	      "[UE %" PRIu8 "] MBSFN Subframe Config pattern %" PRIu8
+	      " not found \n", module_idP, mbsfn_sync_area);
+	return -1;
+    }
 }
 
-int ue_query_mch(module_id_t module_idP, uint8_t CC_id, uint32_t frameP, uint32_t subframe, uint8_t eNB_index,uint8_t *sync_area, uint8_t *mcch_active)
+int
+ue_query_mch(module_id_t module_idP, uint8_t CC_id, uint32_t frameP,
+	     uint32_t subframe, uint8_t eNB_index, uint8_t * sync_area,
+	     uint8_t * mcch_active)
 {
 
-  int i=0, j=0, ii=0, msi_pos=0, mcch_mcs = - 1;
-  int mcch_flag=0, mtch_flag=0, msi_flag=0;
-  int mbsfn_period = 0;// 1<<(UE_mac_inst[module_idP].mbsfn_SubframeConfig[0]->radioframeAllocationPeriod);
-  int mcch_period = 0;// 32<<(UE_mac_inst[module_idP].mbsfn_AreaInfo[0]->mcch_Config_r9.mcch_RepetitionPeriod_r9);
-  int mch_scheduling_period = -1;
+    int i = 0, j = 0, ii = 0, msi_pos = 0, mcch_mcs = -1;
+    int mcch_flag = 0, mtch_flag = 0, msi_flag = 0;
+    int mbsfn_period = 0;	// 1<<(UE_mac_inst[module_idP].mbsfn_SubframeConfig[0]->radioframeAllocationPeriod);
+    int mcch_period = 0;	// 32<<(UE_mac_inst[module_idP].mbsfn_AreaInfo[0]->mcch_Config_r9.mcch_RepetitionPeriod_r9);
+    int mch_scheduling_period = -1;
 
-  int frame_FDD=1;
+    int frame_FDD = 1;
 
 
 #if UE_TIMING_TRACE
-  start_meas(&UE_mac_inst[module_idP].ue_query_mch);
+    start_meas(&UE_mac_inst[module_idP].ue_query_mch);
 #endif
 
-  if (UE_mac_inst[module_idP].pmch_Config[0]) {
-    mch_scheduling_period = 8<<(UE_mac_inst[module_idP].pmch_Config[0]->mch_SchedulingPeriod_r9);
-  }
-
-  for (i=0;
-       i< UE_mac_inst[module_idP].num_active_mbsfn_area;
-       i++ ) {
-    // assume, that there is always a mapping
-    if ((j=ue_get_mbsfn_sf_alloction(module_idP,i,eNB_index)) == -1) {
-      return -1; // continue;
+    if (UE_mac_inst[module_idP].pmch_Config[0]) {
+	mch_scheduling_period =
+	    8 << (UE_mac_inst[module_idP].
+		  pmch_Config[0]->mch_SchedulingPeriod_r9);
     }
 
-    ii=0;
-    msi_pos=0;
-    mbsfn_period = 1<<(UE_mac_inst[module_idP].mbsfn_SubframeConfig[0]->radioframeAllocationPeriod);
-    mcch_period = 32<<(UE_mac_inst[module_idP].mbsfn_AreaInfo[0]->mcch_Config_r9.mcch_RepetitionPeriod_r9);
+    for (i = 0; i < UE_mac_inst[module_idP].num_active_mbsfn_area; i++) {
+	// assume, that there is always a mapping
+	if ((j =
+	     ue_get_mbsfn_sf_alloction(module_idP, i, eNB_index)) == -1) {
+	    return -1;		// continue;
+	}
+
+	ii = 0;
+	msi_pos = 0;
+	mbsfn_period =
+	    1 << (UE_mac_inst[module_idP].
+		  mbsfn_SubframeConfig[0]->radioframeAllocationPeriod);
+	mcch_period =
+	    32 << (UE_mac_inst[module_idP].
+		   mbsfn_AreaInfo[0]->mcch_Config_r9.
+		   mcch_RepetitionPeriod_r9);
+
+	LOG_D(MAC,
+	      "[UE %d] Frame %d subframe %d: Checking MBSFN Sync Area %d/%d with SF allocation %d/%d for MCCH and MTCH (mbsfn period %d, mcch period %d,mac sched period (%d,%ld))\n",
+	      module_idP, frameP, subframe, i,
+	      UE_mac_inst[module_idP].num_active_mbsfn_area, j,
+	      UE_mac_inst[module_idP].num_sf_allocation_pattern,
+	      mbsfn_period, mcch_period, mch_scheduling_period,
+	      UE_mac_inst[module_idP].
+	      mbsfn_SubframeConfig[j]->radioframeAllocationOffset);
+
+	// get the real MCS value
+	switch (UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->
+		mcch_Config_r9.signallingMCS_r9) {
+	case 0:
+	    mcch_mcs = 2;
+	    break;
+
+	case 1:
+	    mcch_mcs = 7;
+	    break;
+
+	case 2:
+	    mcch_mcs = 13;
+	    break;
+
+	case 3:
+	    mcch_mcs = 19;
+	    break;
+	}
+
+	if (frameP % mbsfn_period == UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->radioframeAllocationOffset) {	// MBSFN frameP
+	    if (UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->subframeAllocation.present == MBSFN_SubframeConfig__subframeAllocation_PR_oneFrame) {	// one-frameP format
+
+		if (UE_mac_inst[module_idP].pmch_Config[0]) {
+		    //  Find the first subframe in this MCH to transmit MSI
+		    if (frameP % mch_scheduling_period ==
+			UE_mac_inst[module_idP].
+			mbsfn_SubframeConfig
+			[j]->radioframeAllocationOffset) {
+			while (ii == 0) {
+			    ii = UE_mac_inst[module_idP].
+				mbsfn_SubframeConfig[j]->
+				subframeAllocation.choice.
+				oneFrame.buf[0] & (0x80 >> msi_pos);
+			    msi_pos++;
+			}
+		    }
+		}
+
+		if (UE_mac_inst[module_idP].tdd_Config == NULL)
+		    frame_FDD = 1;
+		else
+		    frame_FDD = 0;
+		// Check if the subframe is for MSI, MCCH or MTCHs and Set the correspoding flag to 1
+		switch (subframe) {
+		case 1:
+		    if (frame_FDD == 1) {
+			if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig
+			     [j]->subframeAllocation.choice.oneFrame.
+			     buf[0] & MBSFN_FDD_SF1) == MBSFN_FDD_SF1) {
+			    if (msi_pos == 1) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 UE_mac_inst[module_idP].mbsfn_AreaInfo
+				 [i]->mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((UE_mac_inst[module_idP].mbsfn_AreaInfo
+				  [i]->mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_FDD_SF1) ==
+				 MBSFN_FDD_SF1)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
 
-    LOG_D(MAC,
-          "[UE %d] Frame %d subframe %d: Checking MBSFN Sync Area %d/%d with SF allocation %d/%d for MCCH and MTCH (mbsfn period %d, mcch period %d,mac sched period (%d,%ld))\n",
-          module_idP,frameP, subframe,i,UE_mac_inst[module_idP].num_active_mbsfn_area,
-          j,UE_mac_inst[module_idP].num_sf_allocation_pattern,mbsfn_period,mcch_period,
-          mch_scheduling_period,UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->radioframeAllocationOffset);
-
-    // get the real MCS value
-    switch (UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.signallingMCS_r9) {
-    case 0:
-      mcch_mcs = 2;
-      break;
-
-    case 1:
-      mcch_mcs = 7;
-      break;
-
-    case 2:
-      mcch_mcs = 13;
-      break;
-
-    case 3:
-      mcch_mcs = 19;
-      break;
-    }
-
-    if (frameP % mbsfn_period == UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->radioframeAllocationOffset) { // MBSFN frameP
-      if (UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->subframeAllocation.present == MBSFN_SubframeConfig__subframeAllocation_PR_oneFrame) { // one-frameP format
-
-        if (UE_mac_inst[module_idP].pmch_Config[0]) {
-          //  Find the first subframe in this MCH to transmit MSI
-          if (frameP % mch_scheduling_period == UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->radioframeAllocationOffset ) {
-            while (ii == 0) {
-              ii = UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & (0x80>>msi_pos);
-              msi_pos++;
-            }
-          }
-        }
-
-	if (UE_mac_inst[module_idP].tdd_Config == NULL) frame_FDD=1;
-	else frame_FDD=0;
-        // Check if the subframe is for MSI, MCCH or MTCHs and Set the correspoding flag to 1
-        switch (subframe) {
-        case 1:
-          if (frame_FDD==1) {
-            if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF1) == MBSFN_FDD_SF1) {
-              if (msi_pos == 1) {
-                msi_flag = 1;
-              }
-
-	      if ( (frameP % mcch_period == UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF1) == MBSFN_FDD_SF1) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-
-        case 2:
-          if (frame_FDD==1) {
-            if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF2) == MBSFN_FDD_SF2) {
-              if (msi_pos == 2) {
-                msi_flag = 1;
-              }
-
-              if ( (frameP % mcch_period == UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF2) == MBSFN_FDD_SF2) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-
-        case 3:
-          if (frame_FDD==0) { //TDD
-            if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF3) == MBSFN_TDD_SF3) {
-              if (msi_pos == 1) {
-                msi_flag = 1;
-              }
-
-              if ( (frameP % mcch_period == UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF3) == MBSFN_TDD_SF3) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          } else { // FDD
-            if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF3) == MBSFN_FDD_SF3) {
-              if (msi_pos == 3) {
-                msi_flag = 1;
-              }
-
-              if ((frameP % mcch_period == UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                  ((UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF3) == MBSFN_FDD_SF3) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-
-        case 4:
-          if (frame_FDD==0) {
-            if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF4) == MBSFN_TDD_SF4) {
-              if (msi_pos == 2) {
-                msi_flag = 1;
-              }
-
-              if ((frameP % mcch_period == UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                  ((UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF4) == MBSFN_TDD_SF4) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-
-        case 6:
-          if (frame_FDD==1) {
-            if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF6) == MBSFN_FDD_SF6) {
-              if (msi_pos == 4) {
-                msi_flag = 1;
-              }
-
-              if ((frameP % mcch_period == UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                  ((UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF6) == MBSFN_FDD_SF6) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-
-        case 7:
-          if (frame_FDD==0) { // TDD
-            if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF7) == MBSFN_TDD_SF7) {
-              if (msi_pos == 3) {
-                msi_flag = 1;
-              }
-
-              if ((frameP % mcch_period == UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                  ((UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF7) == MBSFN_TDD_SF7) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          } else { // FDD
-            if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF7) == MBSFN_FDD_SF7) {
-              if (msi_pos == 5) {
-                msi_flag = 1;
-              }
-
-              if ((frameP % mcch_period == UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                  ((UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF7) == MBSFN_FDD_SF7) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-
-        case 8:
-          if (frame_FDD==0) { //TDD
-            if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF8) == MBSFN_TDD_SF8) {
-              if (msi_pos == 4) {
-                msi_flag = 1;
-              }
-
-              if ( (frameP % mcch_period == UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                   ((UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF8) == MBSFN_TDD_SF8) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          } else { // FDD
-            if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF8) == MBSFN_FDD_SF8) {
-              if (msi_pos == 6) {
-                msi_flag = 1;
-              }
-
-              if ((frameP % mcch_period == UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                  ((UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF8) == MBSFN_FDD_SF8) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-
-        case 9:
-          if (frame_FDD==0) {
-            if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF9) == MBSFN_TDD_SF9) {
-              if (msi_pos == 5) {
-                msi_flag = 1;
-              }
-
-              if ((frameP % mcch_period == UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
-                  ((UE_mac_inst[module_idP].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF9) == MBSFN_TDD_SF9) ) {
-                mcch_flag = 1;
-              }
-
-              mtch_flag = 1;
-            }
-          }
-
-          break;
-        }// end switch
-
-        // sf allocation is non-overlapping
-        if ((msi_flag==1) || (mcch_flag==1) || (mtch_flag==1)) {
-          LOG_D(MAC,"[UE %d] Frame %d Subframe %d: sync area %d SF alloc %d: msi flag %d, mcch flag %d, mtch flag %d\n",
-                module_idP, frameP, subframe,i,j,msi_flag,mcch_flag,mtch_flag);
-
-          *sync_area=i;
-          break;
-        }
-      } else { // four-frameP format
-      }
-    }
-  } // end of for
+		    break;
+
+		case 2:
+		    if (frame_FDD == 1) {
+			if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig
+			     [j]->subframeAllocation.choice.oneFrame.
+			     buf[0] & MBSFN_FDD_SF2) == MBSFN_FDD_SF2) {
+			    if (msi_pos == 2) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 UE_mac_inst[module_idP].mbsfn_AreaInfo
+				 [i]->mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((UE_mac_inst[module_idP].mbsfn_AreaInfo
+				  [i]->mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_FDD_SF2) ==
+				 MBSFN_FDD_SF2)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+
+		case 3:
+		    if (frame_FDD == 0) {	//TDD
+			if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig
+			     [j]->subframeAllocation.choice.oneFrame.
+			     buf[0] & MBSFN_TDD_SF3) == MBSFN_TDD_SF3) {
+			    if (msi_pos == 1) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 UE_mac_inst[module_idP].mbsfn_AreaInfo
+				 [i]->mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((UE_mac_inst[module_idP].mbsfn_AreaInfo
+				  [i]->mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_TDD_SF3) ==
+				 MBSFN_TDD_SF3)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    } else {	// FDD
+			if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig
+			     [j]->subframeAllocation.choice.oneFrame.
+			     buf[0] & MBSFN_FDD_SF3) == MBSFN_FDD_SF3) {
+			    if (msi_pos == 3) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 UE_mac_inst[module_idP].mbsfn_AreaInfo
+				 [i]->mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((UE_mac_inst[module_idP].mbsfn_AreaInfo
+				  [i]->mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_FDD_SF3) ==
+				 MBSFN_FDD_SF3)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+
+		case 4:
+		    if (frame_FDD == 0) {
+			if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig
+			     [j]->subframeAllocation.choice.oneFrame.
+			     buf[0] & MBSFN_TDD_SF4) == MBSFN_TDD_SF4) {
+			    if (msi_pos == 2) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 UE_mac_inst[module_idP].mbsfn_AreaInfo
+				 [i]->mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((UE_mac_inst[module_idP].mbsfn_AreaInfo
+				  [i]->mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_TDD_SF4) ==
+				 MBSFN_TDD_SF4)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+
+		case 6:
+		    if (frame_FDD == 1) {
+			if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig
+			     [j]->subframeAllocation.choice.oneFrame.
+			     buf[0] & MBSFN_FDD_SF6) == MBSFN_FDD_SF6) {
+			    if (msi_pos == 4) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 UE_mac_inst[module_idP].mbsfn_AreaInfo
+				 [i]->mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((UE_mac_inst[module_idP].mbsfn_AreaInfo
+				  [i]->mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_FDD_SF6) ==
+				 MBSFN_FDD_SF6)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+
+		case 7:
+		    if (frame_FDD == 0) {	// TDD
+			if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig
+			     [j]->subframeAllocation.choice.oneFrame.
+			     buf[0] & MBSFN_TDD_SF7) == MBSFN_TDD_SF7) {
+			    if (msi_pos == 3) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 UE_mac_inst[module_idP].mbsfn_AreaInfo
+				 [i]->mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((UE_mac_inst[module_idP].mbsfn_AreaInfo
+				  [i]->mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_TDD_SF7) ==
+				 MBSFN_TDD_SF7)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    } else {	// FDD
+			if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig
+			     [j]->subframeAllocation.choice.oneFrame.
+			     buf[0] & MBSFN_FDD_SF7) == MBSFN_FDD_SF7) {
+			    if (msi_pos == 5) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 UE_mac_inst[module_idP].mbsfn_AreaInfo
+				 [i]->mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((UE_mac_inst[module_idP].mbsfn_AreaInfo
+				  [i]->mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_FDD_SF7) ==
+				 MBSFN_FDD_SF7)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+
+		case 8:
+		    if (frame_FDD == 0) {	//TDD
+			if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig
+			     [j]->subframeAllocation.choice.oneFrame.
+			     buf[0] & MBSFN_TDD_SF8) == MBSFN_TDD_SF8) {
+			    if (msi_pos == 4) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 UE_mac_inst[module_idP].mbsfn_AreaInfo
+				 [i]->mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((UE_mac_inst[module_idP].mbsfn_AreaInfo
+				  [i]->mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_TDD_SF8) ==
+				 MBSFN_TDD_SF8)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    } else {	// FDD
+			if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig
+			     [j]->subframeAllocation.choice.oneFrame.
+			     buf[0] & MBSFN_FDD_SF8) == MBSFN_FDD_SF8) {
+			    if (msi_pos == 6) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 UE_mac_inst[module_idP].mbsfn_AreaInfo
+				 [i]->mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((UE_mac_inst[module_idP].mbsfn_AreaInfo
+				  [i]->mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_FDD_SF8) ==
+				 MBSFN_FDD_SF8)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+
+		case 9:
+		    if (frame_FDD == 0) {
+			if ((UE_mac_inst[module_idP].mbsfn_SubframeConfig
+			     [j]->subframeAllocation.choice.oneFrame.
+			     buf[0] & MBSFN_TDD_SF9) == MBSFN_TDD_SF9) {
+			    if (msi_pos == 5) {
+				msi_flag = 1;
+			    }
+
+			    if ((frameP % mcch_period ==
+				 UE_mac_inst[module_idP].mbsfn_AreaInfo
+				 [i]->mcch_Config_r9.mcch_Offset_r9)
+				&&
+				((UE_mac_inst[module_idP].mbsfn_AreaInfo
+				  [i]->mcch_Config_r9.sf_AllocInfo_r9.
+				  buf[0] & MBSFN_TDD_SF9) ==
+				 MBSFN_TDD_SF9)) {
+				mcch_flag = 1;
+			    }
+
+			    mtch_flag = 1;
+			}
+		    }
+
+		    break;
+		}		// end switch
+
+		// sf allocation is non-overlapping
+		if ((msi_flag == 1) || (mcch_flag == 1)
+		    || (mtch_flag == 1)) {
+		    LOG_D(MAC,
+			  "[UE %d] Frame %d Subframe %d: sync area %d SF alloc %d: msi flag %d, mcch flag %d, mtch flag %d\n",
+			  module_idP, frameP, subframe, i, j, msi_flag,
+			  mcch_flag, mtch_flag);
+
+		    *sync_area = i;
+		    break;
+		}
+	    } else {		// four-frameP format
+	    }
+	}
+    }				// end of for
 #if UE_TIMING_TRACE
-  stop_meas(&UE_mac_inst[module_idP].ue_query_mch);
+    stop_meas(&UE_mac_inst[module_idP].ue_query_mch);
 #endif
 
-  if ( (mcch_flag==1)) { // || (msi_flag==1))
-    *mcch_active=1;
-  }
-
-  if ( (mcch_flag==1) || ((msi_flag==1) && (UE_mac_inst[module_idP].mcch_status==1)) ) {
-    return mcch_mcs;
-  } else if ((mtch_flag==1) && (UE_mac_inst[module_idP].msi_status==1)) {
-    return UE_mac_inst[module_idP].pmch_Config[0]->dataMCS_r9;
-  } else {
-    return -1;
-  }
+    if ((mcch_flag == 1)) {	// || (msi_flag==1))
+	*mcch_active = 1;
+    }
+
+    if ((mcch_flag == 1)
+	|| ((msi_flag == 1) && (UE_mac_inst[module_idP].mcch_status == 1))) {
+	return mcch_mcs;
+    } else if ((mtch_flag == 1)
+	       && (UE_mac_inst[module_idP].msi_status == 1)) {
+	return UE_mac_inst[module_idP].pmch_Config[0]->dataMCS_r9;
+    } else {
+	return -1;
+    }
 }
 
 #endif
 
-unsigned char generate_ulsch_header(uint8_t *mac_header,
-                                    uint8_t num_sdus,
-                                    uint8_t short_padding,
-                                    uint16_t *sdu_lengths,
-                                    uint8_t *sdu_lcids,
-                                    POWER_HEADROOM_CMD *power_headroom,
-                                    uint16_t *crnti,
-                                    BSR_SHORT *truncated_bsr,
-                                    BSR_SHORT *short_bsr,
-                                    BSR_LONG *long_bsr,
-                                    unsigned short post_padding)
+unsigned char
+generate_ulsch_header(uint8_t * mac_header,
+		      uint8_t num_sdus,
+		      uint8_t short_padding,
+		      uint16_t * sdu_lengths,
+		      uint8_t * sdu_lcids,
+		      POWER_HEADROOM_CMD * power_headroom,
+		      uint16_t * crnti,
+		      BSR_SHORT * truncated_bsr,
+		      BSR_SHORT * short_bsr,
+		      BSR_LONG * long_bsr, unsigned short post_padding)
 {
 
-  SCH_SUBHEADER_FIXED *mac_header_ptr = (SCH_SUBHEADER_FIXED *)mac_header;
-  unsigned char first_element=0,last_size=0,i;
-  unsigned char mac_header_control_elements[16],*ce_ptr;
+    SCH_SUBHEADER_FIXED *mac_header_ptr =
+	(SCH_SUBHEADER_FIXED *) mac_header;
+    unsigned char first_element = 0, last_size = 0, i;
+    unsigned char mac_header_control_elements[16], *ce_ptr;
 
-  LOG_D(MAC,"[UE] Generate ULSCH : num_sdus %d\n",num_sdus);
+    LOG_D(MAC, "[UE] Generate ULSCH : num_sdus %d\n", num_sdus);
 #ifdef DEBUG_HEADER_PARSING
 
-  for (i=0; i<num_sdus; i++) {
-    LOG_T(MAC,"[UE] sdu %d : lcid %d length %d",i,sdu_lcids[i],sdu_lengths[i]);
-  }
+    for (i = 0; i < num_sdus; i++) {
+	LOG_T(MAC, "[UE] sdu %d : lcid %d length %d", i, sdu_lcids[i],
+	      sdu_lengths[i]);
+    }
 
-  LOG_T(MAC,"\n");
+    LOG_T(MAC, "\n");
 #endif
-  ce_ptr = &mac_header_control_elements[0];
-
-  if ((short_padding == 1) || (short_padding == 2)) {
-    mac_header_ptr->R    = 0;
-    mac_header_ptr->E    = 0;
-    mac_header_ptr->LCID = SHORT_PADDING;
-    first_element=1;
-    last_size=1;
-  }
-
-  if (short_padding == 2) {
-    mac_header_ptr->E = 1;
-    mac_header_ptr++;
-    mac_header_ptr->R = 0;
-    mac_header_ptr->E    = 0;
-    mac_header_ptr->LCID = SHORT_PADDING;
-    last_size=1;
-  }
-
-  if (power_headroom) {
-    if (first_element>0) {
-      mac_header_ptr->E = 1;
-      mac_header_ptr++;
-    } else {
-      first_element=1;
+    ce_ptr = &mac_header_control_elements[0];
+
+    if ((short_padding == 1) || (short_padding == 2)) {
+	mac_header_ptr->R = 0;
+	mac_header_ptr->E = 0;
+	mac_header_ptr->LCID = SHORT_PADDING;
+	first_element = 1;
+	last_size = 1;
+    }
+
+    if (short_padding == 2) {
+	mac_header_ptr->E = 1;
+	mac_header_ptr++;
+	mac_header_ptr->R = 0;
+	mac_header_ptr->E = 0;
+	mac_header_ptr->LCID = SHORT_PADDING;
+	last_size = 1;
     }
 
-    mac_header_ptr->R = 0;
-    mac_header_ptr->E    = 0;
-    mac_header_ptr->LCID = POWER_HEADROOM;
-    last_size=1;
-    *((POWER_HEADROOM_CMD *)ce_ptr)=(*power_headroom);
-    ce_ptr+=sizeof(POWER_HEADROOM_CMD);
-    LOG_D(MAC, "phr header size %zu\n",sizeof(POWER_HEADROOM_CMD));
-  }
+    if (power_headroom) {
+	if (first_element > 0) {
+	    mac_header_ptr->E = 1;
+	    mac_header_ptr++;
+	} else {
+	    first_element = 1;
+	}
+
+	mac_header_ptr->R = 0;
+	mac_header_ptr->E = 0;
+	mac_header_ptr->LCID = POWER_HEADROOM;
+	last_size = 1;
+	*((POWER_HEADROOM_CMD *) ce_ptr) = (*power_headroom);
+	ce_ptr += sizeof(POWER_HEADROOM_CMD);
+	LOG_D(MAC, "phr header size %zu\n", sizeof(POWER_HEADROOM_CMD));
+    }
 
-  if (crnti) {
+    if (crnti) {
 #ifdef DEBUG_HEADER_PARSING
-    LOG_D(MAC,"[UE] CRNTI : %x (first_element %d)\n",*crnti,first_element);
+	LOG_D(MAC, "[UE] CRNTI : %x (first_element %d)\n", *crnti,
+	      first_element);
 #endif
 
-    if (first_element>0) {
-      mac_header_ptr->E = 1;
-      mac_header_ptr++;
-    } else {
-      first_element=1;
-    }
-
-    mac_header_ptr->R    = 0;
-    mac_header_ptr->E    = 0;
-    mac_header_ptr->LCID = CRNTI;
-    last_size=1;
-    *((uint16_t *)ce_ptr)=(*crnti);
-    ce_ptr+=sizeof(uint16_t);
-    //    printf("offset %d\n",ce_ptr-mac_header_control_elements);
-  }
-
-  if (truncated_bsr) {
-    if (first_element>0) {
-      mac_header_ptr->E = 1;
-      /*
-      printf("last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
-       */
-      mac_header_ptr++;
-    } else {
-      first_element=1;
+	if (first_element > 0) {
+	    mac_header_ptr->E = 1;
+	    mac_header_ptr++;
+	} else {
+	    first_element = 1;
+	}
+
+	mac_header_ptr->R = 0;
+	mac_header_ptr->E = 0;
+	mac_header_ptr->LCID = CRNTI;
+	last_size = 1;
+	*((uint16_t *) ce_ptr) = (*crnti);
+	ce_ptr += sizeof(uint16_t);
+	//    printf("offset %d\n",ce_ptr-mac_header_control_elements);
     }
 
+    if (truncated_bsr) {
+	if (first_element > 0) {
+	    mac_header_ptr->E = 1;
+	    /*
+	       printf("last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
+	     */
+	    mac_header_ptr++;
+	} else {
+	    first_element = 1;
+	}
+
 #ifdef DEBUG_HEADER_PARSING
-    LOG_D(MAC,"[UE] Scheduler Truncated BSR Header\n");
+	LOG_D(MAC, "[UE] Scheduler Truncated BSR Header\n");
 #endif
-    mac_header_ptr->R = 0;
-    mac_header_ptr->E    = 0;
-    mac_header_ptr->LCID = TRUNCATED_BSR;
-    last_size=1;
-    *((BSR_TRUNCATED *)ce_ptr)=(*truncated_bsr);
-    ce_ptr+=sizeof(BSR_TRUNCATED);
-    //    printf("(cont_res) : offset %d\n",ce_ptr-mac_header_control_elements);
-
-  } else if (short_bsr) {
-    if (first_element>0) {
-      mac_header_ptr->E = 1;
-      /*
-      printf("last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
-       */
-      mac_header_ptr++;
-    } else {
-      first_element=1;
-    }
+	mac_header_ptr->R = 0;
+	mac_header_ptr->E = 0;
+	mac_header_ptr->LCID = TRUNCATED_BSR;
+	last_size = 1;
+	*((BSR_TRUNCATED *) ce_ptr) = (*truncated_bsr);
+	ce_ptr += sizeof(BSR_TRUNCATED);
+	//    printf("(cont_res) : offset %d\n",ce_ptr-mac_header_control_elements);
+
+    } else if (short_bsr) {
+	if (first_element > 0) {
+	    mac_header_ptr->E = 1;
+	    /*
+	       printf("last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
+	     */
+	    mac_header_ptr++;
+	} else {
+	    first_element = 1;
+	}
 
 #ifdef DEBUG_HEADER_PARSING
-    LOG_D(MAC,"[UE] Scheduler SHORT BSR Header\n");
+	LOG_D(MAC, "[UE] Scheduler SHORT BSR Header\n");
 #endif
-    mac_header_ptr->R = 0;
-    mac_header_ptr->E    = 0;
-    mac_header_ptr->LCID = SHORT_BSR;
-    last_size=1;
-    *((BSR_SHORT *)ce_ptr)=(*short_bsr);
-    ce_ptr+=sizeof(BSR_SHORT);
-
-    //    printf("(cont_res) : offset %d\n",ce_ptr-mac_header_control_elements);
-  } else if (long_bsr) {
-    if (first_element>0) {
-      mac_header_ptr->E = 1;
-      /*
-      printf("last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
-      ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
-       */
-      mac_header_ptr++;
-    } else {
-      first_element=1;
-    }
+	mac_header_ptr->R = 0;
+	mac_header_ptr->E = 0;
+	mac_header_ptr->LCID = SHORT_BSR;
+	last_size = 1;
+	*((BSR_SHORT *) ce_ptr) = (*short_bsr);
+	ce_ptr += sizeof(BSR_SHORT);
+
+	//    printf("(cont_res) : offset %d\n",ce_ptr-mac_header_control_elements);
+    } else if (long_bsr) {
+	if (first_element > 0) {
+	    mac_header_ptr->E = 1;
+	    /*
+	       printf("last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
+	       ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
+	     */
+	    mac_header_ptr++;
+	} else {
+	    first_element = 1;
+	}
 
 #ifdef DEBUG_HEADER_PARSING
-    LOG_D(MAC,"[UE] Scheduler Long BSR Header\n");
+	LOG_D(MAC, "[UE] Scheduler Long BSR Header\n");
 #endif
-    mac_header_ptr->R = 0;
-    mac_header_ptr->E    = 0;
-    mac_header_ptr->LCID = LONG_BSR;
-    last_size=1;
-
-    *(ce_ptr)     = (long_bsr->Buffer_size0 << 2) | ((long_bsr->Buffer_size1 & 0x30) >> 4);
-    *(ce_ptr + 1) = ((long_bsr->Buffer_size1 & 0x0F) << 4) | ((long_bsr->Buffer_size2 & 0x3C) >> 2);
-    *(ce_ptr + 2) = ((long_bsr->Buffer_size2 & 0x03) << 2) | (long_bsr->Buffer_size3 & 0x3F);
-    ce_ptr += BSR_LONG_SIZE;
-
-    //    printf("(cont_res) : offset %d\n",ce_ptr-mac_header_control_elements);
-  }
+	mac_header_ptr->R = 0;
+	mac_header_ptr->E = 0;
+	mac_header_ptr->LCID = LONG_BSR;
+	last_size = 1;
+
+	*(ce_ptr) =
+	    (long_bsr->
+	     Buffer_size0 << 2) | ((long_bsr->Buffer_size1 & 0x30) >> 4);
+	*(ce_ptr + 1) =
+	    ((long_bsr->Buffer_size1 & 0x0F) << 4) | ((long_bsr->
+						       Buffer_size2 & 0x3C)
+						      >> 2);
+	*(ce_ptr + 2) =
+	    ((long_bsr->
+	      Buffer_size2 & 0x03) << 2) | (long_bsr->Buffer_size3 & 0x3F);
+	ce_ptr += BSR_LONG_SIZE;
+
+	//    printf("(cont_res) : offset %d\n",ce_ptr-mac_header_control_elements);
+    }
+    //  printf("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
 
-  //  printf("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
+    for (i = 0; i < num_sdus; i++) {
+#ifdef DEBUG_HEADER_PARSING
+	LOG_T(MAC, "[UE] sdu subheader %d (lcid %d, %d bytes)\n", i,
+	      sdu_lcids[i], sdu_lengths[i]);
+#endif
 
-  for (i=0; i<num_sdus; i++) {
+	if ((i == (num_sdus - 1))
+	    && ((short_padding) || (post_padding == 0))) {
+	    if (first_element > 0) {
+		mac_header_ptr->E = 1;
 #ifdef DEBUG_HEADER_PARSING
-    LOG_T(MAC,"[UE] sdu subheader %d (lcid %d, %d bytes)\n",i,sdu_lcids[i],sdu_lengths[i]);
+		LOG_D(MAC, "[UE] last subheader : %x (R%d,E%d,LCID%d)\n",
+		      *(unsigned char *) mac_header_ptr,
+		      ((SCH_SUBHEADER_FIXED *) mac_header_ptr)->R,
+		      ((SCH_SUBHEADER_FIXED *) mac_header_ptr)->E,
+		      ((SCH_SUBHEADER_FIXED *) mac_header_ptr)->LCID);
 #endif
+		mac_header_ptr += last_size;
+	    }
+	    mac_header_ptr->R = 0;
+	    mac_header_ptr->E = 0;
+	    mac_header_ptr->LCID = sdu_lcids[i];
+	} else {
+	    if ((first_element > 0)) {
+		mac_header_ptr->E = 1;
+#ifdef DEBUG_HEADER_PARSING
+		LOG_D(MAC, "[UE] last subheader : %x (R%d,E%d,LCID%d)\n",
+		      *(unsigned char *) mac_header_ptr,
+		      ((SCH_SUBHEADER_FIXED *) mac_header_ptr)->R,
+		      ((SCH_SUBHEADER_FIXED *) mac_header_ptr)->E,
+		      ((SCH_SUBHEADER_FIXED *) mac_header_ptr)->LCID);
+#endif
+		mac_header_ptr += last_size;
+		//      printf("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
+	    } else {
+		first_element = 1;
+
+	    }
+
+	    if (sdu_lengths[i] < 128) {
+		((SCH_SUBHEADER_SHORT *) mac_header_ptr)->R = 0;	// 3
+		((SCH_SUBHEADER_SHORT *) mac_header_ptr)->E = 0;
+		((SCH_SUBHEADER_SHORT *) mac_header_ptr)->F = 0;
+		((SCH_SUBHEADER_SHORT *) mac_header_ptr)->LCID =
+		    sdu_lcids[i];
+		((SCH_SUBHEADER_SHORT *) mac_header_ptr)->L =
+		    (unsigned char) sdu_lengths[i];
+		last_size = 2;
+#ifdef DEBUG_HEADER_PARSING
+		LOG_D(MAC, "[UE] short sdu\n");
+		LOG_T(MAC,
+		      "[UE] last subheader : %x (R%d,E%d,LCID%d,F%d,L%d)\n",
+		      ((uint16_t *) mac_header_ptr)[0],
+		      ((SCH_SUBHEADER_SHORT *) mac_header_ptr)->R,
+		      ((SCH_SUBHEADER_SHORT *) mac_header_ptr)->E,
+		      ((SCH_SUBHEADER_SHORT *) mac_header_ptr)->LCID,
+		      ((SCH_SUBHEADER_SHORT *) mac_header_ptr)->F,
+		      ((SCH_SUBHEADER_SHORT *) mac_header_ptr)->L);
+#endif
+	    } else {
+		((SCH_SUBHEADER_LONG *) mac_header_ptr)->R = 0;
+		((SCH_SUBHEADER_LONG *) mac_header_ptr)->E = 0;
+		((SCH_SUBHEADER_LONG *) mac_header_ptr)->F = 1;
+		((SCH_SUBHEADER_LONG *) mac_header_ptr)->LCID =
+		    sdu_lcids[i];
+		((SCH_SUBHEADER_LONG *) mac_header_ptr)->L_MSB =
+		    ((unsigned short) sdu_lengths[i] >> 8) & 0x7f;
+		((SCH_SUBHEADER_LONG *) mac_header_ptr)->L_LSB =
+		    (unsigned short) sdu_lengths[i] & 0xff;
+		((SCH_SUBHEADER_LONG *) mac_header_ptr)->padding = 0x00;
+		last_size = 3;
+#ifdef DEBUG_HEADER_PARSING
+		LOG_D(MAC, "[UE] long sdu\n");
+#endif
+	    }
+	}
+    }
+
+    if (post_padding > 0) {	// we have lots of padding at the end of the packet
+	mac_header_ptr->E = 1;
+	mac_header_ptr += last_size;
+	// add a padding element
+	mac_header_ptr->R = 0;
+	mac_header_ptr->E = 0;
+	mac_header_ptr->LCID = SHORT_PADDING;
+	mac_header_ptr++;
+    } else {			// no end of packet padding
+	// last SDU subhead is of fixed type (sdu length implicitly to be computed at UE)
+	mac_header_ptr++;
+	//mac_header_ptr=last_size; // FIXME: should be ++
+    }
 
-    if ((i == (num_sdus - 1)) && ((short_padding) || (post_padding == 0))) {
-        if (first_element>0) {
-            mac_header_ptr->E = 1;
-      #ifdef DEBUG_HEADER_PARSING
-            LOG_D(MAC,"[UE] last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
-                  ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
-                  ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
-                  ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
-      #endif
-            mac_header_ptr+=last_size;
-        }
-        mac_header_ptr->R    = 0;
-        mac_header_ptr->E    = 0;
-        mac_header_ptr->LCID = sdu_lcids[i];
-    }
-    else {
-        if ((first_element>0)) {
-          mac_header_ptr->E = 1;
-    #ifdef DEBUG_HEADER_PARSING
-          LOG_D(MAC,"[UE] last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
-                ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
-                ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
-                ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
-    #endif
-          mac_header_ptr+=last_size;
-          //      printf("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
-        } else {
-          first_element=1;
-
-        }
-
-        if (sdu_lengths[i] < 128) {
-          ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->R    = 0; // 3
-          ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->E    = 0;
-          ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->F    = 0;
-          ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->LCID = sdu_lcids[i];
-          ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->L    = (unsigned char)sdu_lengths[i];
-          last_size=2;
-    #ifdef DEBUG_HEADER_PARSING
-          LOG_D(MAC,"[UE] short sdu\n");
-          LOG_T(MAC,"[UE] last subheader : %x (R%d,E%d,LCID%d,F%d,L%d)\n",
-                ((uint16_t*)mac_header_ptr)[0],
-                ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->R,
-                ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->E,
-                ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->LCID,
-                ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->F,
-                ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->L);
-    #endif
-        } else {
-          ((SCH_SUBHEADER_LONG *)mac_header_ptr)->R    = 0;
-          ((SCH_SUBHEADER_LONG *)mac_header_ptr)->E    = 0;
-          ((SCH_SUBHEADER_LONG *)mac_header_ptr)->F    = 1;
-          ((SCH_SUBHEADER_LONG *)mac_header_ptr)->LCID = sdu_lcids[i];
-          ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_MSB    = ((unsigned short) sdu_lengths[i]>>8)&0x7f;
-          ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_LSB    = (unsigned short) sdu_lengths[i]&0xff;
-          ((SCH_SUBHEADER_LONG *)mac_header_ptr)->padding  = 0x00;
-          last_size=3;
-    #ifdef DEBUG_HEADER_PARSING
-          LOG_D(MAC,"[UE] long sdu\n");
-    #endif
-        }
-    }
-  }
-
-  if (post_padding>0) {// we have lots of padding at the end of the packet
-    mac_header_ptr->E = 1;
-    mac_header_ptr+=last_size;
-    // add a padding element
-    mac_header_ptr->R    = 0;
-    mac_header_ptr->E    = 0;
-    mac_header_ptr->LCID = SHORT_PADDING;
-    mac_header_ptr++;
-  } else { // no end of packet padding
-    // last SDU subhead is of fixed type (sdu length implicitly to be computed at UE)
-    mac_header_ptr++;
-    //mac_header_ptr=last_size; // FIXME: should be ++
-  }
-
-
-  if ((ce_ptr-mac_header_control_elements) > 0) {
-    memcpy((void*)mac_header_ptr,mac_header_control_elements,ce_ptr-mac_header_control_elements);
-    mac_header_ptr+=(unsigned char)(ce_ptr-mac_header_control_elements);
-  }
 
+    if ((ce_ptr - mac_header_control_elements) > 0) {
+	memcpy((void *) mac_header_ptr, mac_header_control_elements,
+	       ce_ptr - mac_header_control_elements);
+	mac_header_ptr +=
+	    (unsigned char) (ce_ptr - mac_header_control_elements);
+    }
 #ifdef DEBUG_HEADER_PARSING
-  LOG_T(MAC," [UE] header : ");
+    LOG_T(MAC, " [UE] header : ");
 
-  for (i=0; i<((unsigned char*)mac_header_ptr - mac_header); i++) {
-    LOG_T(MAC,"%2x.",mac_header[i]);
-  }
+    for (i = 0; i < ((unsigned char *) mac_header_ptr - mac_header); i++) {
+	LOG_T(MAC, "%2x.", mac_header[i]);
+    }
 
-  LOG_T(MAC,"\n");
+    LOG_T(MAC, "\n");
 #endif
-  return((unsigned char*)mac_header_ptr - mac_header);
+    return ((unsigned char *) mac_header_ptr - mac_header);
 
 }
 
-void ue_get_sdu(module_id_t module_idP,int CC_id,frame_t frameP,sub_frame_t subframe, uint8_t eNB_index,uint8_t *ulsch_buffer,uint16_t buflen, uint8_t *access_mode)
+void
+ue_get_sdu(module_id_t module_idP, int CC_id, frame_t frameP,
+	   sub_frame_t subframe, uint8_t eNB_index,
+	   uint8_t * ulsch_buffer, uint16_t buflen, uint8_t * access_mode)
 {
 
-  uint8_t total_rlc_pdu_header_len=0, rlc_pdu_header_len_last=0 ;
-  uint16_t buflen_remain = 0;
-  uint8_t bsr_len=0,bsr_ce_len=0,bsr_header_len=0;
-  uint8_t phr_header_len=0, phr_ce_len=0,phr_len=0;
-  uint8_t lcid=0,lcid_rlc_pdu_count=0;
-  boolean_t is_lcid_processed = FALSE;
-  boolean_t is_all_lcid_processed = FALSE;
-  uint16_t sdu_lengths[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
-  uint8_t sdu_lcids[8]    = { 0, 0, 0, 0, 0, 0, 0, 0 };
-  uint8_t payload_offset=0,num_sdus=0;
-  uint8_t ulsch_buff[MAX_ULSCH_PAYLOAD_BYTES];
-  uint16_t sdu_length_total=0;
-  BSR_SHORT bsr_short,bsr_truncated;
-  BSR_LONG bsr_long;
-  BSR_SHORT *bsr_s=&bsr_short;
-  BSR_LONG  *bsr_l=&bsr_long;
-  BSR_SHORT *bsr_t=&bsr_truncated;
-  POWER_HEADROOM_CMD phr;
-  POWER_HEADROOM_CMD *phr_p=&phr;
-  unsigned short short_padding=0, post_padding=0, padding_len=0;
-  int j; // used for padding
-  // Compute header length
-  int lcg_id = 0;
-  int lcg_id_bsr_trunc = 0;
-  int highest_priority = 16;
-  int num_lcg_id_with_data = 0;
-  rlc_buffer_occupancy_t lcid_buffer_occupancy_old=0, lcid_buffer_occupancy_new=0;
-  
-  LOG_D(MAC,"[UE %d] MAC PROCESS UL TRANSPORT BLOCK at frame%d subframe %d TBS=%d\n",
-                        module_idP, frameP, subframe, buflen);
-
-
-  AssertFatal(CC_id==0,
-	      "Transmission on secondary CCs is not supported yet\n");
+    uint8_t total_rlc_pdu_header_len = 0, rlc_pdu_header_len_last = 0;
+    uint16_t buflen_remain = 0;
+    uint8_t bsr_len = 0, bsr_ce_len = 0, bsr_header_len = 0;
+    uint8_t phr_header_len = 0, phr_ce_len = 0, phr_len = 0;
+    uint8_t lcid = 0, lcid_rlc_pdu_count = 0;
+    boolean_t is_lcid_processed = FALSE;
+    boolean_t is_all_lcid_processed = FALSE;
+    uint16_t sdu_lengths[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
+    uint8_t sdu_lcids[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
+    uint8_t payload_offset = 0, num_sdus = 0;
+    uint8_t ulsch_buff[MAX_ULSCH_PAYLOAD_BYTES];
+    uint16_t sdu_length_total = 0;
+    BSR_SHORT bsr_short, bsr_truncated;
+    BSR_LONG bsr_long;
+    BSR_SHORT *bsr_s = &bsr_short;
+    BSR_LONG *bsr_l = &bsr_long;
+    BSR_SHORT *bsr_t = &bsr_truncated;
+    POWER_HEADROOM_CMD phr;
+    POWER_HEADROOM_CMD *phr_p = &phr;
+    unsigned short short_padding = 0, post_padding = 0, padding_len = 0;
+    int j;			// used for padding
+    // Compute header length
+    int lcg_id = 0;
+    int lcg_id_bsr_trunc = 0;
+    int highest_priority = 16;
+    int num_lcg_id_with_data = 0;
+    rlc_buffer_occupancy_t lcid_buffer_occupancy_old =
+	0, lcid_buffer_occupancy_new = 0;
+
+    LOG_D(MAC,
+	  "[UE %d] MAC PROCESS UL TRANSPORT BLOCK at frame%d subframe %d TBS=%d\n",
+	  module_idP, frameP, subframe, buflen);
+
+
+    AssertFatal(CC_id == 0,
+		"Transmission on secondary CCs is not supported yet\n");
 
 #if UE_TIMING_TRACE
-  start_meas(&UE_mac_inst[module_idP].tx_ulsch_sdu);
+    start_meas(&UE_mac_inst[module_idP].tx_ulsch_sdu);
 #endif
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_GET_SDU, VCD_FUNCTION_IN);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_GET_SDU, VCD_FUNCTION_IN);
 
 #ifdef CBA
 
-  if (*access_mode==CBA_ACCESS) {
-    LOG_D(MAC,"[UE %d] frameP %d subframe %d try CBA transmission\n",
-          module_idP, frameP, subframe);
+    if (*access_mode == CBA_ACCESS) {
+	LOG_D(MAC, "[UE %d] frameP %d subframe %d try CBA transmission\n",
+	      module_idP, frameP, subframe);
+
+	//if (UE_mac_inst[module_idP].scheduling_info.LCID_status[DTCH] == LCID_EMPTY)
+	if (cba_access(module_idP, frameP, subframe, eNB_index, buflen) ==
+	    0) {
+	    *access_mode = POSTPONED_ACCESS;
+	    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+		(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_GET_SDU, VCD_FUNCTION_OUT);
+	    return;
+	}
+
+	LOG_I(MAC,
+	      "[UE %d] frameP %d subframe %d CBA transmission oppurtunity, tbs %d\n",
+	      module_idP, frameP, subframe, buflen);
+    }
+#endif
+    bsr_header_len = 0;
+    phr_header_len = 1;		//sizeof(SCH_SUBHEADER_FIXED);
+
+    while (lcg_id < MAX_NUM_LCGID) {
+	if (UE_mac_inst[module_idP].scheduling_info.BSR_bytes[lcg_id]) {
+	    num_lcg_id_with_data++;
+	}
+	lcg_id++;
+    }
+
+    if (num_lcg_id_with_data) {
+	LOG_D(MAC,
+	      "[UE %d] MAC Tx data pending at frame%d subframe %d nb LCG =%d Bytes for LCG0=%d LCG1=%d LCG2=%d LCG3=%d BSR Trigger status =%d TBS=%d\n",
+	      module_idP, frameP, subframe, num_lcg_id_with_data,
+	      UE_mac_inst[module_idP].scheduling_info.BSR_bytes[0],
+	      UE_mac_inst[module_idP].scheduling_info.BSR_bytes[1],
+	      UE_mac_inst[module_idP].scheduling_info.BSR_bytes[2],
+	      UE_mac_inst[module_idP].scheduling_info.BSR_bytes[3],
+	      UE_mac_inst[module_idP].BSR_reporting_active, buflen);
 
-    //if (UE_mac_inst[module_idP].scheduling_info.LCID_status[DTCH] == LCID_EMPTY)
-    if (cba_access(module_idP,frameP,subframe,eNB_index,buflen)==0) {
-      *access_mode=POSTPONED_ACCESS;
-      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_GET_SDU, VCD_FUNCTION_OUT);
-      return;
     }
+    //Restart ReTxBSR Timer at new grant indication (36.321)
+    if (UE_mac_inst[module_idP].scheduling_info.retxBSR_SF !=
+	MAC_UE_BSR_TIMER_NOT_RUNNING) {
+	UE_mac_inst[module_idP].scheduling_info.retxBSR_SF =
+	    get_sf_retxBSRTimer(UE_mac_inst[module_idP].
+				scheduling_info.retxBSR_Timer);
+    }
+    // periodicBSR-Timer expires, trigger BSR
+    if ((UE_mac_inst[module_idP].scheduling_info.periodicBSR_Timer !=
+	 PeriodicBSR_Timer_r12_infinity)
+	&& (UE_mac_inst[module_idP].scheduling_info.periodicBSR_SF == 0)) {
+	// Trigger BSR Periodic
+	UE_mac_inst[module_idP].BSR_reporting_active |=
+	    BSR_TRIGGER_PERIODIC;
+
+	LOG_D(MAC,
+	      "[UE %d] MAC BSR Triggered PeriodicBSR Timer expiry at frame%d subframe %d TBS=%d\n",
+	      module_idP, frameP, subframe, buflen);
 
-    LOG_I(MAC,"[UE %d] frameP %d subframe %d CBA transmission oppurtunity, tbs %d\n",
-          module_idP, frameP, subframe,buflen);
-  }
+    }
+    //Compute BSR Length if Regular or Periodic BSR is triggered
+    //WARNING: if BSR long is computed, it may be changed to BSR short during or after multiplexing if there remains less than 1 LCGROUP with data after Tx
+    if (UE_mac_inst[module_idP].BSR_reporting_active) {
+
+	AssertFatal((UE_mac_inst[module_idP].BSR_reporting_active &
+		     BSR_TRIGGER_PADDING) == 0,
+		    "Inconsistent BSR Trigger=%d !\n",
+		    UE_mac_inst[module_idP].BSR_reporting_active);
+
+	if (buflen >= 4) {
+	    //A Regular or Periodic BSR can only be sent if TBS >= 4 as transmitting only a BSR is not allowed if UE has data to transmit
+	    bsr_header_len = 1;
+
+	    if (num_lcg_id_with_data <= 1) {
+		bsr_ce_len = sizeof(BSR_SHORT);	//1 byte
+	    } else {
+		bsr_ce_len = BSR_LONG_SIZE;	//3 bytes
+	    }
+	}
+    }
 
-#endif
-  bsr_header_len = 0;
-  phr_header_len = 1;//sizeof(SCH_SUBHEADER_FIXED);
-
-  while (lcg_id < MAX_NUM_LCGID) {
-     if  (UE_mac_inst[module_idP].scheduling_info.BSR_bytes[lcg_id]){
-         num_lcg_id_with_data ++;
-     }
-     lcg_id ++;
-  }
-
-  if (num_lcg_id_with_data){
-      LOG_D(MAC,"[UE %d] MAC Tx data pending at frame%d subframe %d nb LCG =%d Bytes for LCG0=%d LCG1=%d LCG2=%d LCG3=%d BSR Trigger status =%d TBS=%d\n",
-                            module_idP,
-                            frameP,
-                            subframe,
-                            num_lcg_id_with_data,
-                            UE_mac_inst[module_idP].scheduling_info.BSR_bytes[0],
-                            UE_mac_inst[module_idP].scheduling_info.BSR_bytes[1],
-                            UE_mac_inst[module_idP].scheduling_info.BSR_bytes[2],
-                            UE_mac_inst[module_idP].scheduling_info.BSR_bytes[3],
-                            UE_mac_inst[module_idP].BSR_reporting_active, buflen);
-
-  }
-  //Restart ReTxBSR Timer at new grant indication (36.321)
-  if (UE_mac_inst[module_idP].scheduling_info.retxBSR_SF != MAC_UE_BSR_TIMER_NOT_RUNNING){
-      UE_mac_inst[module_idP].scheduling_info.retxBSR_SF = get_sf_retxBSRTimer(UE_mac_inst[module_idP].scheduling_info.retxBSR_Timer);
-  }
-
-  // periodicBSR-Timer expires, trigger BSR
-  if ((UE_mac_inst[module_idP].scheduling_info.periodicBSR_Timer != PeriodicBSR_Timer_r12_infinity)
-            && (UE_mac_inst[module_idP].scheduling_info.periodicBSR_SF == 0)){
-        // Trigger BSR Periodic
-      UE_mac_inst[module_idP].BSR_reporting_active |= BSR_TRIGGER_PERIODIC;
-
-      LOG_D(MAC,"[UE %d] MAC BSR Triggered PeriodicBSR Timer expiry at frame%d subframe %d TBS=%d\n",
-                       module_idP, frameP, subframe, buflen);
-
-    }
-
-  //Compute BSR Length if Regular or Periodic BSR is triggered
-  //WARNING: if BSR long is computed, it may be changed to BSR short during or after multiplexing if there remains less than 1 LCGROUP with data after Tx
-  if (UE_mac_inst[module_idP].BSR_reporting_active){
-
-	  AssertFatal ((UE_mac_inst[module_idP].BSR_reporting_active & BSR_TRIGGER_PADDING) == 0 , "Inconsistent BSR Trigger=%d !\n",
-			  UE_mac_inst[module_idP].BSR_reporting_active);
-
-      if (buflen >= 4){
-    	  //A Regular or Periodic BSR can only be sent if TBS >= 4 as transmitting only a BSR is not allowed if UE has data to transmit
-    	  bsr_header_len = 1;
-
-          if (num_lcg_id_with_data <= 1){
-              bsr_ce_len = sizeof(BSR_SHORT); //1 byte
-          }
-          else{
-              bsr_ce_len = BSR_LONG_SIZE; //3 bytes
-          }
-      }
-  }
-
-  bsr_len = bsr_ce_len + bsr_header_len;
-
-  phr_ce_len = (UE_mac_inst[module_idP].PHR_reporting_active == 1) ? 1 /* sizeof(POWER_HEADROOM_CMD)*/: 0;
-  if ((phr_ce_len > 0) && ((phr_ce_len + phr_header_len + bsr_len) <= buflen)){
-    phr_len = phr_ce_len + phr_header_len;
-    LOG_D(MAC,"[UE %d] header size info: PHR len %d (ce%d,hdr%d) buff_len %d\n",
-          module_idP, phr_len, phr_ce_len, phr_header_len, buflen);
-  } else {
-    phr_len=0;
-    phr_header_len = 0;
-    phr_ce_len = 0;
-  }
-
-  // check for UL bandwidth requests and add SR control element
-
-  // check for UL bandwidth requests and add SR control element
-
-  // Check for DCCH first
-// TO DO: Multiplex in the order defined by the logical channel prioritization
-for (lcid=DCCH; (lcid < MAX_NUM_LCID) && (is_all_lcid_processed == FALSE) ; lcid++) {
-  if (UE_mac_inst[module_idP].scheduling_info.LCID_status[lcid] == LCID_NOT_EMPTY) {
+    bsr_len = bsr_ce_len + bsr_header_len;
+
+    phr_ce_len =
+	(UE_mac_inst[module_idP].PHR_reporting_active ==
+	 1) ? 1 /* sizeof(POWER_HEADROOM_CMD) */ : 0;
+    if ((phr_ce_len > 0)
+	&& ((phr_ce_len + phr_header_len + bsr_len) <= buflen)) {
+	phr_len = phr_ce_len + phr_header_len;
+	LOG_D(MAC,
+	      "[UE %d] header size info: PHR len %d (ce%d,hdr%d) buff_len %d\n",
+	      module_idP, phr_len, phr_ce_len, phr_header_len, buflen);
+    } else {
+	phr_len = 0;
+	phr_header_len = 0;
+	phr_ce_len = 0;
+    }
 
-      lcid_rlc_pdu_count = 0;
-      is_lcid_processed	= FALSE;
-      lcid_buffer_occupancy_old = mac_rlc_get_buffer_occupancy_ind(module_idP,
-              	  	  	  	  	  	  	  	  	  	  	  	  	  UE_mac_inst[module_idP].crnti,
-																  eNB_index,
-																  frameP,
-																  subframe,
-																  ENB_FLAG_NO,
-																  lcid);
+    // check for UL bandwidth requests and add SR control element
 
-      lcid_buffer_occupancy_new = lcid_buffer_occupancy_old;
+    // check for UL bandwidth requests and add SR control element
 
-      AssertFatal (lcid_buffer_occupancy_new == UE_mac_inst[module_idP].scheduling_info.LCID_buffer_remain[lcid], "LCID=%d RLC has BO %d bytes but MAC has stored %d bytes\n",
-      					lcid,lcid_buffer_occupancy_new,UE_mac_inst[module_idP].scheduling_info.LCID_buffer_remain[lcid]);
+    // Check for DCCH first
+// TO DO: Multiplex in the order defined by the logical channel prioritization
+    for (lcid = DCCH;
+	 (lcid < MAX_NUM_LCID) && (is_all_lcid_processed == FALSE); lcid++)
+    {
+	if (UE_mac_inst[module_idP].scheduling_info.LCID_status[lcid] ==
+	    LCID_NOT_EMPTY) {
+
+	    lcid_rlc_pdu_count = 0;
+	    is_lcid_processed = FALSE;
+	    lcid_buffer_occupancy_old =
+		mac_rlc_get_buffer_occupancy_ind(module_idP,
+						 UE_mac_inst[module_idP].
+						 crnti, eNB_index, frameP,
+						 subframe, ENB_FLAG_NO,
+						 lcid);
+
+	    lcid_buffer_occupancy_new = lcid_buffer_occupancy_old;
+
+	    AssertFatal(lcid_buffer_occupancy_new ==
+			UE_mac_inst[module_idP].
+			scheduling_info.LCID_buffer_remain[lcid],
+			"LCID=%d RLC has BO %d bytes but MAC has stored %d bytes\n",
+			lcid, lcid_buffer_occupancy_new,
+			UE_mac_inst[module_idP].
+			scheduling_info.LCID_buffer_remain[lcid]);
+
+	    AssertFatal(lcid_buffer_occupancy_new <=
+			UE_mac_inst[module_idP].
+			scheduling_info.BSR_bytes[UE_mac_inst[module_idP].
+						  scheduling_info.LCGID
+						  [lcid]],
+			"LCID=%d RLC has more BO %d bytes than BSR = %d bytes\n",
+			lcid, lcid_buffer_occupancy_new,
+			UE_mac_inst[module_idP].
+			scheduling_info.BSR_bytes[UE_mac_inst[module_idP].
+						  scheduling_info.LCGID
+						  [lcid]]);
+
+
+	    //Multiplex all available DCCH RLC PDUs considering to multiplex the last PDU each time for maximize the data
+	    //Adjust at the end of the loop
+	    while ((!is_lcid_processed) && (lcid_buffer_occupancy_new)
+		   && (bsr_len + phr_len + total_rlc_pdu_header_len +
+		       sdu_length_total + MIN_MAC_HDR_RLC_SIZE <= buflen))
+	    {
+
+		// Workaround for issue in OAI eNB or EPC which are not able to process SRB2 message multiplexed with SRB1 on the same MAC PDU
+		if ((usim_test == 0) && (lcid == DCCH1)
+		    && (lcid_rlc_pdu_count == 0) && (num_sdus)) {
+
+		    // Skip SRB2 multiplex if at least one SRB1 SDU is already multiplexed
+		    break;
+		}
+
+		buflen_remain =
+		    buflen - (bsr_len + phr_len +
+			      total_rlc_pdu_header_len + sdu_length_total +
+			      1);
+
+
+		LOG_D(MAC,
+		      "[UE %d] Frame %d : UL-DXCH -> ULSCH, RLC %d has %d bytes to "
+		      "send (Transport Block size %d BSR size=%d PHR=%d SDU Length Total %d , mac header len %d BSR byte before Tx=%d)\n",
+		      module_idP, frameP, lcid, lcid_buffer_occupancy_new,
+		      buflen, bsr_len, phr_len, sdu_length_total,
+		      total_rlc_pdu_header_len,
+		      UE_mac_inst[module_idP].
+		      scheduling_info.BSR_bytes[UE_mac_inst[module_idP].
+						scheduling_info.LCGID
+						[lcid]]);
+
+
+		sdu_lengths[num_sdus] = mac_rlc_data_req(module_idP,
+							 UE_mac_inst
+							 [module_idP].
+							 crnti, eNB_index,
+							 frameP,
+							 ENB_FLAG_NO,
+							 MBMS_FLAG_NO,
+							 lcid,
+							 buflen_remain,
+							 (char *)
+							 &ulsch_buff
+							 [sdu_length_total]);
+
+
+		AssertFatal(buflen_remain >= sdu_lengths[num_sdus],
+			    "LCID=%d RLC has segmented %d bytes but MAC has max=%d\n",
+			    lcid, sdu_lengths[num_sdus], buflen_remain);
+
+
+		if (sdu_lengths[num_sdus]) {
+		    sdu_length_total += sdu_lengths[num_sdus];
+		    sdu_lcids[num_sdus] = lcid;
+		    LOG_D(MAC,
+			  "[UE %d] TX Multiplex RLC PDU TX Got %d bytes for LcId%d\n",
+			  module_idP, sdu_lengths[num_sdus], lcid);
+
+		    if (buflen ==
+			(bsr_len + phr_len + total_rlc_pdu_header_len +
+			 sdu_length_total + 1)) {
+			//No more remaining TBS after this PDU
+			//exit the function
+			rlc_pdu_header_len_last = 1;
+			is_lcid_processed = TRUE;
+			is_all_lcid_processed = TRUE;
+		    } else {
+			rlc_pdu_header_len_last =
+			    (sdu_lengths[num_sdus] > 128) ? 3 : 2;
+
+			//Change to 1 byte if it does not fit in the TBS, ie last PDU
+			if (buflen <=
+			    (bsr_len + phr_len + total_rlc_pdu_header_len +
+			     rlc_pdu_header_len_last + sdu_length_total)) {
+			    rlc_pdu_header_len_last = 1;
+			    is_lcid_processed = TRUE;
+			    is_all_lcid_processed = TRUE;
+			}
+		    }
+
+		    //Update number of SDU
+		    num_sdus++;
+
+		    //Update total MAC Header size for RLC PDUs and save last one
+		    total_rlc_pdu_header_len += rlc_pdu_header_len_last;
+
+		    lcid_rlc_pdu_count++;
+		} else {
+		    /* avoid infinite loop ... */
+		    is_lcid_processed = TRUE;
+		}
+
+		/* Get updated BO after multiplexing this PDU */
+		lcid_buffer_occupancy_new =
+		    mac_rlc_get_buffer_occupancy_ind(module_idP,
+						     UE_mac_inst
+						     [module_idP].crnti,
+						     eNB_index, frameP,
+						     subframe, ENB_FLAG_NO,
+						     lcid);
+
+		is_lcid_processed = (is_lcid_processed)
+		    || (lcid_buffer_occupancy_new <= 0);
+	    }
+
+	    //Update Buffer remain and BSR bytes after transmission
+
+	    AssertFatal(lcid_buffer_occupancy_new <=
+			lcid_buffer_occupancy_old,
+			"MAC UE Tx error : Buffer Occupancy After Tx=%d greater than before=%d BO! for LCID=%d RLC PDU nb=%d Frame %d Subrame %d\n",
+			lcid_buffer_occupancy_new,
+			lcid_buffer_occupancy_old, lcid,
+			lcid_rlc_pdu_count, frameP, subframe);
+
+	    UE_mac_inst[module_idP].scheduling_info.
+		LCID_buffer_remain[lcid] = lcid_buffer_occupancy_new;
+	    UE_mac_inst[module_idP].
+		scheduling_info.BSR_bytes[UE_mac_inst[module_idP].
+					  scheduling_info.LCGID[lcid]] +=
+		(lcid_buffer_occupancy_new - lcid_buffer_occupancy_old);
+
+	    //Update the number of LCGID with data as BSR shall reflect status after BSR transmission
+	    if ((num_lcg_id_with_data > 1)
+		&& (UE_mac_inst[module_idP].
+		    scheduling_info.BSR_bytes[UE_mac_inst[module_idP].
+					      scheduling_info.LCGID[lcid]]
+		    == 0)) {
+		num_lcg_id_with_data--;
+		// Change BSR size to BSR SHORT if num_lcg_id_with_data becomes to 1
+		if ((bsr_len) && (num_lcg_id_with_data == 1)) {
+		    bsr_ce_len = sizeof(BSR_SHORT);
+		    bsr_len = bsr_ce_len + bsr_header_len;
+		}
+	    }
+
+
+	    UE_mac_inst[module_idP].scheduling_info.LCID_status[lcid] =
+		LCID_EMPTY;
+	}
+    }
 
-      AssertFatal (lcid_buffer_occupancy_new <= UE_mac_inst[module_idP].scheduling_info.BSR_bytes[UE_mac_inst[module_idP].scheduling_info.LCGID[lcid]], "LCID=%d RLC has more BO %d bytes than BSR = %d bytes\n",
-      					lcid,lcid_buffer_occupancy_new,UE_mac_inst[module_idP].scheduling_info.BSR_bytes[UE_mac_inst[module_idP].scheduling_info.LCGID[lcid]]);
 
 
-      //Multiplex all available DCCH RLC PDUs considering to multiplex the last PDU each time for maximize the data
-      //Adjust at the end of the loop
-      while ((!is_lcid_processed) && (lcid_buffer_occupancy_new) && (bsr_len + phr_len + total_rlc_pdu_header_len + sdu_length_total + MIN_MAC_HDR_RLC_SIZE <= buflen)) {
+    // Compute BSR Values and update Nb LCGID with data after multiplexing
+    num_lcg_id_with_data = 0;
+    lcg_id_bsr_trunc = 0;
+    for (lcg_id = 0; lcg_id < MAX_NUM_LCGID; lcg_id++) {
+	UE_mac_inst[module_idP].scheduling_info.BSR[lcg_id] =
+	    locate_BsrIndexByBufferSize(BSR_TABLE, BSR_TABLE_SIZE,
+					UE_mac_inst
+					[module_idP].scheduling_info.
+					BSR_bytes[lcg_id]);
 
-    	  // Workaround for issue in OAI eNB or EPC which are not able to process SRB2 message multiplexed with SRB1 on the same MAC PDU
-    	  if ((usim_test == 0) && (lcid == DCCH1) && (lcid_rlc_pdu_count == 0) && (num_sdus)) {
+	if (UE_mac_inst[module_idP].scheduling_info.BSR_bytes[lcg_id]) {
+	    num_lcg_id_with_data++;
+	    lcg_id_bsr_trunc = lcg_id;
+	}
+    }
 
-    		  // Skip SRB2 multiplex if at least one SRB1 SDU is already multiplexed
-    		  break;
-    	  }
 
-          buflen_remain = buflen - (bsr_len + phr_len + total_rlc_pdu_header_len + sdu_length_total + 1);
+    if (bsr_ce_len) {
+	//Print updated BSR when sent
+	LOG_D(MAC,
+	      "[UE %d] Remaining Buffer after Tx frame%d subframe %d nb LCG =%d Bytes for LCG0=%d LCG1=%d LCG2=%d LCG3=%d BSR Trigger status =%d TBS=%d\n",
+	      module_idP, frameP, subframe, num_lcg_id_with_data,
+	      UE_mac_inst[module_idP].scheduling_info.BSR_bytes[0],
+	      UE_mac_inst[module_idP].scheduling_info.BSR_bytes[1],
+	      UE_mac_inst[module_idP].scheduling_info.BSR_bytes[2],
+	      UE_mac_inst[module_idP].scheduling_info.BSR_bytes[3],
+	      UE_mac_inst[module_idP].BSR_reporting_active, buflen);
+
+	LOG_D(MAC,
+	      "[UE %d] Frame %d Subframe %d TX BSR Regular or Periodic size=%d BSR0=%d BSR1=%d BSR2=%d BSR3=%d\n",
+	      module_idP, frameP, subframe, bsr_ce_len,
+	      UE_mac_inst[module_idP].scheduling_info.BSR[0],
+	      UE_mac_inst[module_idP].scheduling_info.BSR[1],
+	      UE_mac_inst[module_idP].scheduling_info.BSR[2],
+	      UE_mac_inst[module_idP].scheduling_info.BSR[3]);
+    }
+    // build PHR and update the timers
+    if (phr_ce_len == sizeof(POWER_HEADROOM_CMD)) {
+	phr_p->PH = get_phr_mapping(module_idP, CC_id, eNB_index);
+	phr_p->R = 0;
+	LOG_D(MAC,
+	      "[UE %d] Frame %d report PHR with mapping (%d->%d) for LCID %d\n",
+	      module_idP, frameP, get_PHR(module_idP, CC_id, eNB_index),
+	      phr_p->PH, POWER_HEADROOM);
+	update_phr(module_idP, CC_id);
+    } else {
+	phr_p = NULL;
+    }
 
+    LOG_T(MAC, "[UE %d] Frame %d: bsr s %p bsr_l %p, phr_p %p\n",
+	  module_idP, frameP, bsr_s, bsr_l, phr_p);
 
-		  LOG_D(MAC, "[UE %d] Frame %d : UL-DXCH -> ULSCH, RLC %d has %d bytes to "
-				"send (Transport Block size %d BSR size=%d PHR=%d SDU Length Total %d , mac header len %d BSR byte before Tx=%d)\n",
-				module_idP,frameP, lcid,lcid_buffer_occupancy_new,buflen,bsr_len,phr_len,sdu_length_total,total_rlc_pdu_header_len,UE_mac_inst[module_idP].scheduling_info.BSR_bytes[UE_mac_inst[module_idP].scheduling_info.LCGID[lcid]]);
 
+    // Check BSR padding: it is done after PHR according to Logical Channel Prioritization order
+    // Check for max padding size, ie MAC Hdr for last RLC PDU = 1
+    /* For Padding BSR:
+       -  if the number of padding bits is equal to or larger than the size of the Short BSR plus its subheader but smaller than the size of the Long BSR plus its subheader:
+       -  if more than one LCG has data available for transmission in the TTI where the BSR is transmitted: report Truncated BSR of the LCG with the highest priority logical channel with data available for transmission;
+       -  else report Short BSR.
+       -  else if the number of padding bits is equal to or larger than the size of the Long BSR plus its subheader, report Long BSR.
+     */
+    if (sdu_length_total) {
+	padding_len =
+	    buflen - (bsr_len + phr_len + total_rlc_pdu_header_len -
+		      rlc_pdu_header_len_last + sdu_length_total + 1);
+    } else {
+	padding_len = buflen - (bsr_len + phr_len);
+    }
 
-		  sdu_lengths[num_sdus] = mac_rlc_data_req(module_idP,
-							 UE_mac_inst[module_idP].crnti,
-							 eNB_index,
-							 frameP,
-							 ENB_FLAG_NO,
-							 MBMS_FLAG_NO,
-							 lcid,
-							 buflen_remain,
-							 (char *)&ulsch_buff[sdu_length_total]);
-
-
-		  AssertFatal (buflen_remain >= sdu_lengths[num_sdus], "LCID=%d RLC has segmented %d bytes but MAC has max=%d\n",
-					lcid,sdu_lengths[num_sdus],buflen_remain);
-
-
-		  if (sdu_lengths[num_sdus])
-		  {
-			  sdu_length_total += sdu_lengths[num_sdus];
-			  sdu_lcids[num_sdus] = lcid;
-			  LOG_D(MAC,"[UE %d] TX Multiplex RLC PDU TX Got %d bytes for LcId%d\n",module_idP,sdu_lengths[num_sdus],lcid);
-
-			  if (buflen == (bsr_len + phr_len + total_rlc_pdu_header_len + sdu_length_total + 1)) {
-				  //No more remaining TBS after this PDU
-				  //exit the function
-				  rlc_pdu_header_len_last = 1;
-				  is_lcid_processed = TRUE;
-				  is_all_lcid_processed = TRUE;
-			  }
-			  else {
-				  rlc_pdu_header_len_last = (sdu_lengths[num_sdus] > 128 ) ? 3 : 2 ;
-
-				  //Change to 1 byte if it does not fit in the TBS, ie last PDU
-				  if (buflen <= (bsr_len + phr_len + total_rlc_pdu_header_len + rlc_pdu_header_len_last + sdu_length_total)) {
-					  rlc_pdu_header_len_last = 1;
-					  is_lcid_processed = TRUE;
-					  is_all_lcid_processed = TRUE;
-				  }
-			  }
-
-			  //Update number of SDU
-			  num_sdus ++;
-
-			  //Update total MAC Header size for RLC PDUs and save last one
-			  total_rlc_pdu_header_len += rlc_pdu_header_len_last;
-
-			  lcid_rlc_pdu_count ++;
-		  }
-		  else
-		  {
-			  /* avoid infinite loop ... */
-			  is_lcid_processed = TRUE;
-		  }
-
-          /* Get updated BO after multiplexing this PDU */
-          lcid_buffer_occupancy_new = mac_rlc_get_buffer_occupancy_ind(module_idP,
-                        	  	  	  	  	  	  	  	  	  	  	  	  	  UE_mac_inst[module_idP].crnti,
-          																  eNB_index,
-          																  frameP,
-          																  subframe,
-          																  ENB_FLAG_NO,
-          																  lcid);
-
-          is_lcid_processed = (is_lcid_processed) || (lcid_buffer_occupancy_new <= 0);
-   }
-
-    //Update Buffer remain and BSR bytes after transmission
-
-    AssertFatal (lcid_buffer_occupancy_new <=  lcid_buffer_occupancy_old, "MAC UE Tx error : Buffer Occupancy After Tx=%d greater than before=%d BO! for LCID=%d RLC PDU nb=%d Frame %d Subrame %d\n",
-    		lcid_buffer_occupancy_new,lcid_buffer_occupancy_old,lcid,lcid_rlc_pdu_count,frameP,subframe);
-
-    UE_mac_inst[module_idP].scheduling_info.LCID_buffer_remain[lcid] = lcid_buffer_occupancy_new;
-    UE_mac_inst[module_idP].scheduling_info.BSR_bytes[UE_mac_inst[module_idP].scheduling_info.LCGID[lcid]] += (lcid_buffer_occupancy_new - lcid_buffer_occupancy_old);
-
-    //Update the number of LCGID with data as BSR shall reflect status after BSR transmission
-    if ((num_lcg_id_with_data > 1) && (UE_mac_inst[module_idP].scheduling_info.BSR_bytes[UE_mac_inst[module_idP].scheduling_info.LCGID[lcid]] == 0))
-    {
-        num_lcg_id_with_data --;
-        // Change BSR size to BSR SHORT if num_lcg_id_with_data becomes to 1
-        if ((bsr_len) && (num_lcg_id_with_data == 1))
-        {
-            bsr_ce_len = sizeof(BSR_SHORT);
-            bsr_len = bsr_ce_len + bsr_header_len;
-        }
-    }
-
-
-    UE_mac_inst[module_idP].scheduling_info.LCID_status[lcid] = LCID_EMPTY;
-   }
- }
-
-
-
-  // Compute BSR Values and update Nb LCGID with data after multiplexing
-  num_lcg_id_with_data = 0;
-  lcg_id_bsr_trunc = 0;
-  for (lcg_id=0;lcg_id<MAX_NUM_LCGID;lcg_id++){
-      UE_mac_inst[module_idP].scheduling_info.BSR[lcg_id] = locate_BsrIndexByBufferSize(BSR_TABLE, BSR_TABLE_SIZE, UE_mac_inst[module_idP].scheduling_info.BSR_bytes[lcg_id]);
-
-      if  (UE_mac_inst[module_idP].scheduling_info.BSR_bytes[lcg_id]){
-          num_lcg_id_with_data ++;
-          lcg_id_bsr_trunc = lcg_id;
-      }
-  }
-
-
-  if (bsr_ce_len) {
-      //Print updated BSR when sent
-      LOG_D(MAC,"[UE %d] Remaining Buffer after Tx frame%d subframe %d nb LCG =%d Bytes for LCG0=%d LCG1=%d LCG2=%d LCG3=%d BSR Trigger status =%d TBS=%d\n",
-                            module_idP,
-                            frameP,
-                            subframe,
-                            num_lcg_id_with_data,
-                            UE_mac_inst[module_idP].scheduling_info.BSR_bytes[0],
-                            UE_mac_inst[module_idP].scheduling_info.BSR_bytes[1],
-                            UE_mac_inst[module_idP].scheduling_info.BSR_bytes[2],
-                            UE_mac_inst[module_idP].scheduling_info.BSR_bytes[3],
-                            UE_mac_inst[module_idP].BSR_reporting_active, buflen);
-
-      LOG_D(MAC,"[UE %d] Frame %d Subframe %d TX BSR Regular or Periodic size=%d BSR0=%d BSR1=%d BSR2=%d BSR3=%d\n",module_idP,frameP,subframe,bsr_ce_len,
-              UE_mac_inst[module_idP].scheduling_info.BSR[0],
-              UE_mac_inst[module_idP].scheduling_info.BSR[1],
-              UE_mac_inst[module_idP].scheduling_info.BSR[2],
-              UE_mac_inst[module_idP].scheduling_info.BSR[3]);
-  }
-
-
-  // build PHR and update the timers
-  if (phr_ce_len == sizeof(POWER_HEADROOM_CMD)) {
-    phr_p->PH = get_phr_mapping(module_idP,CC_id,eNB_index);
-    phr_p->R  = 0;
-    LOG_D(MAC,"[UE %d] Frame %d report PHR with mapping (%d->%d) for LCID %d\n",
-          module_idP,frameP, get_PHR(module_idP,CC_id,eNB_index), phr_p->PH,POWER_HEADROOM);
-    update_phr(module_idP,CC_id);
-  } else {
-    phr_p=NULL;
-  }
-
-  LOG_T(MAC,"[UE %d] Frame %d: bsr s %p bsr_l %p, phr_p %p\n",  module_idP,frameP,bsr_s, bsr_l, phr_p);
-
-
-  // Check BSR padding: it is done after PHR according to Logical Channel Prioritization order
-  // Check for max padding size, ie MAC Hdr for last RLC PDU = 1
-  /* For Padding BSR:
-     -	if the number of padding bits is equal to or larger than the size of the Short BSR plus its subheader but smaller than the size of the Long BSR plus its subheader:
-         -	if more than one LCG has data available for transmission in the TTI where the BSR is transmitted: report Truncated BSR of the LCG with the highest priority logical channel with data available for transmission;
-         -	else report Short BSR.
-     -	else if the number of padding bits is equal to or larger than the size of the Long BSR plus its subheader, report Long BSR.
-   */
-  if (sdu_length_total) {
-      padding_len = buflen - (bsr_len + phr_len + total_rlc_pdu_header_len - rlc_pdu_header_len_last + sdu_length_total + 1);
-  }
-  else {
-      padding_len = buflen - (bsr_len + phr_len);
-  }
-
-
-   if ((padding_len) && (bsr_len == 0))
-   {
-	   /* if the number of padding bits is equal to or larger than the size of the Long BSR plus its subheader, report Long BSR*/
-     if (padding_len >= (1+BSR_LONG_SIZE))
-     {
-    	 bsr_ce_len = BSR_LONG_SIZE;
-    	 bsr_header_len = 1;
-         // Trigger BSR Padding
-       UE_mac_inst[module_idP].BSR_reporting_active |= BSR_TRIGGER_PADDING;
-
-
-     } else if (padding_len >= (1+sizeof(BSR_SHORT))) {
-    	 bsr_ce_len = sizeof(BSR_SHORT);
-    	 bsr_header_len = 1;
-
-    	 if (num_lcg_id_with_data > 1)
-    	 {
-    	  // REPORT TRUNCATED BSR
-       	  //Get LCGID of highest priority LCID with data
-    		 for (lcid=DCCH; lcid < MAX_NUM_LCID ; lcid++) {
-    			 if (UE_mac_inst[module_idP].logicalChannelConfig[lcid] != NULL) {
-        			 lcg_id = UE_mac_inst[module_idP].scheduling_info.LCGID[lcid];
-
-        			 if ((lcg_id < MAX_NUM_LCGID) && (UE_mac_inst[module_idP].scheduling_info.BSR_bytes[lcg_id])
-        					 && (UE_mac_inst[module_idP].logicalChannelConfig[lcid]->ul_SpecificParameters->priority <= highest_priority)) {
-        				 highest_priority = UE_mac_inst[module_idP].logicalChannelConfig[lcid]->ul_SpecificParameters->priority;
-        				 lcg_id_bsr_trunc = lcg_id;
-        			 }
-    			 }
-    		 }
-    	 }
-    	 else
-    	 {
-    		 //Report SHORT BSR, clear bsr_t
-    		 bsr_t = NULL;
-    	 }
-
-       // Trigger BSR Padding
-       UE_mac_inst[module_idP].BSR_reporting_active |= BSR_TRIGGER_PADDING;
-     }
- 	 bsr_len = bsr_header_len + bsr_ce_len;
-   }
-
-   //Fill BSR Infos
-   if (bsr_ce_len == 0 ) {
-     bsr_s = NULL;
-     bsr_l = NULL;
-     bsr_t = NULL;
-   } else if (bsr_ce_len == BSR_LONG_SIZE) {
-     bsr_s = NULL;
-     bsr_t = NULL;
-     bsr_l->Buffer_size0 = UE_mac_inst[module_idP].scheduling_info.BSR[LCGID0];
-     bsr_l->Buffer_size1 = UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1];
-     bsr_l->Buffer_size2 = UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2];
-     bsr_l->Buffer_size3 = UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3];
-
-     LOG_D(MAC, "[UE %d] Frame %d subframe %d BSR Trig=%d report long BSR (level LCGID0 %d,level LCGID1 %d,level LCGID2 %d,level LCGID3 %d)\n", module_idP,frameP,subframe,
-    	   UE_mac_inst[module_idP].BSR_reporting_active,
-           UE_mac_inst[module_idP].scheduling_info.BSR[LCGID0],
-           UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1],
-           UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2],
-           UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3]);
-
-   } else if (bsr_ce_len == sizeof(BSR_SHORT)) {
-     bsr_l = NULL;
-     if ((bsr_t != NULL) && (UE_mac_inst[module_idP].BSR_reporting_active & BSR_TRIGGER_PADDING)) {
-    	 //Truncated BSR
-    	 bsr_s = NULL;
-    	 bsr_t->LCGID = lcg_id_bsr_trunc;
-    	 bsr_t->Buffer_size = UE_mac_inst[module_idP].scheduling_info.BSR[lcg_id_bsr_trunc];
-
-         LOG_D(MAC,"[UE %d] Frame %d subframe %d BSR Trig=%d report TRUNCATED BSR with level %d for LCGID %d\n",
-               module_idP, frameP, subframe, UE_mac_inst[module_idP].BSR_reporting_active, UE_mac_inst[module_idP].scheduling_info.BSR[lcg_id_bsr_trunc],lcg_id_bsr_trunc);
-
-     }
-     else
-     {
-    	 bsr_t = NULL;
-         bsr_s->LCGID = lcg_id_bsr_trunc;
-         bsr_s->Buffer_size = UE_mac_inst[module_idP].scheduling_info.BSR[lcg_id_bsr_trunc];
-
-         LOG_D(MAC,"[UE %d] Frame %d subframe %d BSR Trig=%d report SHORT BSR with level %d for LCGID %d\n",
-               module_idP, frameP, subframe, UE_mac_inst[module_idP].BSR_reporting_active, UE_mac_inst[module_idP].scheduling_info.BSR[lcg_id_bsr_trunc],lcg_id_bsr_trunc);
-     }
-   }
 
+    if ((padding_len) && (bsr_len == 0)) {
+	/* if the number of padding bits is equal to or larger than the size of the Long BSR plus its subheader, report Long BSR */
+	if (padding_len >= (1 + BSR_LONG_SIZE)) {
+	    bsr_ce_len = BSR_LONG_SIZE;
+	    bsr_header_len = 1;
+	    // Trigger BSR Padding
+	    UE_mac_inst[module_idP].BSR_reporting_active |=
+		BSR_TRIGGER_PADDING;
+
+
+	} else if (padding_len >= (1 + sizeof(BSR_SHORT))) {
+	    bsr_ce_len = sizeof(BSR_SHORT);
+	    bsr_header_len = 1;
+
+	    if (num_lcg_id_with_data > 1) {
+		// REPORT TRUNCATED BSR
+		//Get LCGID of highest priority LCID with data
+		for (lcid = DCCH; lcid < MAX_NUM_LCID; lcid++) {
+		    if (UE_mac_inst[module_idP].
+			logicalChannelConfig[lcid] != NULL) {
+			lcg_id =
+			    UE_mac_inst[module_idP].scheduling_info.
+			    LCGID[lcid];
+
+			if ((lcg_id < MAX_NUM_LCGID)
+			    && (UE_mac_inst[module_idP].
+				scheduling_info.BSR_bytes[lcg_id])
+			    &&
+			    (UE_mac_inst[module_idP].logicalChannelConfig
+			     [lcid]->ul_SpecificParameters->priority <=
+			     highest_priority)) {
+			    highest_priority =
+				UE_mac_inst[module_idP].
+				logicalChannelConfig[lcid]->
+				ul_SpecificParameters->priority;
+			    lcg_id_bsr_trunc = lcg_id;
+			}
+		    }
+		}
+	    } else {
+		//Report SHORT BSR, clear bsr_t
+		bsr_t = NULL;
+	    }
+
+	    // Trigger BSR Padding
+	    UE_mac_inst[module_idP].BSR_reporting_active |=
+		BSR_TRIGGER_PADDING;
+	}
+	bsr_len = bsr_header_len + bsr_ce_len;
+    }
+    //Fill BSR Infos
+    if (bsr_ce_len == 0) {
+	bsr_s = NULL;
+	bsr_l = NULL;
+	bsr_t = NULL;
+    } else if (bsr_ce_len == BSR_LONG_SIZE) {
+	bsr_s = NULL;
+	bsr_t = NULL;
+	bsr_l->Buffer_size0 =
+	    UE_mac_inst[module_idP].scheduling_info.BSR[LCGID0];
+	bsr_l->Buffer_size1 =
+	    UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1];
+	bsr_l->Buffer_size2 =
+	    UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2];
+	bsr_l->Buffer_size3 =
+	    UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3];
+
+	LOG_D(MAC,
+	      "[UE %d] Frame %d subframe %d BSR Trig=%d report long BSR (level LCGID0 %d,level LCGID1 %d,level LCGID2 %d,level LCGID3 %d)\n",
+	      module_idP, frameP, subframe,
+	      UE_mac_inst[module_idP].BSR_reporting_active,
+	      UE_mac_inst[module_idP].scheduling_info.BSR[LCGID0],
+	      UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1],
+	      UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2],
+	      UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3]);
+
+    } else if (bsr_ce_len == sizeof(BSR_SHORT)) {
+	bsr_l = NULL;
+	if ((bsr_t != NULL)
+	    && (UE_mac_inst[module_idP].BSR_reporting_active &
+		BSR_TRIGGER_PADDING)) {
+	    //Truncated BSR
+	    bsr_s = NULL;
+	    bsr_t->LCGID = lcg_id_bsr_trunc;
+	    bsr_t->Buffer_size =
+		UE_mac_inst[module_idP].scheduling_info.
+		BSR[lcg_id_bsr_trunc];
+
+	    LOG_D(MAC,
+		  "[UE %d] Frame %d subframe %d BSR Trig=%d report TRUNCATED BSR with level %d for LCGID %d\n",
+		  module_idP, frameP, subframe,
+		  UE_mac_inst[module_idP].BSR_reporting_active,
+		  UE_mac_inst[module_idP].
+		  scheduling_info.BSR[lcg_id_bsr_trunc], lcg_id_bsr_trunc);
+
+	} else {
+	    bsr_t = NULL;
+	    bsr_s->LCGID = lcg_id_bsr_trunc;
+	    bsr_s->Buffer_size =
+		UE_mac_inst[module_idP].scheduling_info.
+		BSR[lcg_id_bsr_trunc];
+
+	    LOG_D(MAC,
+		  "[UE %d] Frame %d subframe %d BSR Trig=%d report SHORT BSR with level %d for LCGID %d\n",
+		  module_idP, frameP, subframe,
+		  UE_mac_inst[module_idP].BSR_reporting_active,
+		  UE_mac_inst[module_idP].
+		  scheduling_info.BSR[lcg_id_bsr_trunc], lcg_id_bsr_trunc);
+	}
+    }
 // 1-bit padding or 2-bit padding  special padding subheader
 // Check for max padding size, ie MAC Hdr for last RLC PDU = 1
-   if (sdu_length_total) {
-       padding_len = buflen - (bsr_len + phr_len + total_rlc_pdu_header_len - rlc_pdu_header_len_last + sdu_length_total + 1);
-   }
-   else {
-       padding_len = buflen - (bsr_len + phr_len);
-   }
-
-  if (padding_len <= 2) {
-    short_padding = padding_len;
-    // only add padding header
-    post_padding = 0;
-    //update total MAC Hdr size for RLC data
     if (sdu_length_total) {
-        total_rlc_pdu_header_len = total_rlc_pdu_header_len - rlc_pdu_header_len_last + 1;
-        rlc_pdu_header_len_last = 1;
-   }
-  }
-  else if (sdu_length_total) {
-      post_padding = buflen - (bsr_len + phr_len + total_rlc_pdu_header_len + sdu_length_total + 1);
-      // If by adding MAC Hdr for last RLC PDU the padding is 0 then set MAC Hdr for last RLC PDU = 1 and compute 1 or 2 byte padding
-      if (post_padding == 0) {
-          total_rlc_pdu_header_len -= rlc_pdu_header_len_last;
-          padding_len = buflen - (bsr_len + phr_len + total_rlc_pdu_header_len + sdu_length_total + 1);
-          short_padding = padding_len;
-          total_rlc_pdu_header_len ++;
-      }
-  }
-  else {
-    if (padding_len == buflen) {// nona mac pdu
-      *access_mode=CANCELED_ACCESS;
-    }
-
-    short_padding = 0;
-
-    post_padding = buflen - (bsr_len + phr_len + total_rlc_pdu_header_len + sdu_length_total + 1);
-  }
-
-  // Generate header
-  // if (num_sdus>0) {
-
-  payload_offset = generate_ulsch_header(ulsch_buffer,  // mac header
-                                         num_sdus,      // num sdus
-                                         short_padding,            // short pading
-                                         sdu_lengths,  // sdu length
-                                         sdu_lcids,    // sdu lcid
-                                         phr_p,  // power headroom
-                                         NULL,  // crnti
-                                         bsr_t,  // truncated bsr
-                                         bsr_s, // short bsr
-                                         bsr_l,
-                                         post_padding); // long_bsr
-
-  LOG_D(MAC,
-        "[UE %d] Generate header :bufflen %d  sdu_length_total %d, num_sdus %d, sdu_lengths[0] %d, sdu_lcids[0] %d => payload offset %d,  total_rlc_pdu_header_len %d, padding %d,post_padding %d, bsr len %d, phr len %d, reminder %d \n",
-        module_idP,buflen, sdu_length_total,num_sdus,sdu_lengths[0],sdu_lcids[0],payload_offset, total_rlc_pdu_header_len,
-        short_padding,post_padding, bsr_len, phr_len,buflen-sdu_length_total-payload_offset);
-  // cycle through SDUs and place in ulsch_buffer
-  if (sdu_length_total) {
-      memcpy(&ulsch_buffer[payload_offset],ulsch_buff,sdu_length_total);
-  }
-
-  // fill remainder of DLSCH with random data
-  if (post_padding) {
-      for (j=0; j<(buflen-sdu_length_total-payload_offset); j++) {
-        ulsch_buffer[payload_offset+sdu_length_total+j] = (char)(taus()&0xff);
-      }
-  }
-  LOG_D(MAC,"[UE %d][SR] Gave SDU to PHY, clearing any scheduling request\n", module_idP);
-  UE_mac_inst[module_idP].scheduling_info.SR_pending=0;
-  UE_mac_inst[module_idP].scheduling_info.SR_COUNTER=0;
-
-  /* Actions when a BSR is sent */
-  if (bsr_ce_len)
-  {
-      LOG_D(MAC,"[UE %d] MAC BSR Sent !! bsr (ce%d,hdr%d) buff_len %d\n",
-              module_idP, bsr_ce_len, bsr_header_len, buflen);
-
-	  // Reset ReTx BSR Timer
-	  UE_mac_inst[module_idP].scheduling_info.retxBSR_SF = get_sf_retxBSRTimer(UE_mac_inst[module_idP].scheduling_info.retxBSR_Timer);
-
-	  LOG_D(MAC,"[UE %d] MAC ReTx BSR Timer Reset =%d\n", module_idP,
-			  UE_mac_inst[module_idP].scheduling_info.retxBSR_SF);
-
-	  // Reset Periodic Timer except when BSR is truncated
-	  if ((bsr_t == NULL) && (UE_mac_inst[module_idP].scheduling_info.periodicBSR_Timer != PeriodicBSR_Timer_r12_infinity))
-	  {
-		  UE_mac_inst[module_idP].scheduling_info.periodicBSR_SF = get_sf_periodicBSRTimer(UE_mac_inst[module_idP].scheduling_info.periodicBSR_Timer);
-
-		  LOG_D(MAC,"[UE %d] MAC Periodic BSR Timer Reset =%d\n",
-		        module_idP, UE_mac_inst[module_idP].scheduling_info.periodicBSR_SF);
-
-	  }
-
-	  // Reset BSR Trigger flags
-	  UE_mac_inst[module_idP].BSR_reporting_active = BSR_TRIGGER_NONE;
-  }
-
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_GET_SDU, VCD_FUNCTION_OUT);
+	padding_len =
+	    buflen - (bsr_len + phr_len + total_rlc_pdu_header_len -
+		      rlc_pdu_header_len_last + sdu_length_total + 1);
+    } else {
+	padding_len = buflen - (bsr_len + phr_len);
+    }
+
+    if (padding_len <= 2) {
+	short_padding = padding_len;
+	// only add padding header
+	post_padding = 0;
+	//update total MAC Hdr size for RLC data
+	if (sdu_length_total) {
+	    total_rlc_pdu_header_len =
+		total_rlc_pdu_header_len - rlc_pdu_header_len_last + 1;
+	    rlc_pdu_header_len_last = 1;
+	}
+    } else if (sdu_length_total) {
+	post_padding =
+	    buflen - (bsr_len + phr_len + total_rlc_pdu_header_len +
+		      sdu_length_total + 1);
+	// If by adding MAC Hdr for last RLC PDU the padding is 0 then set MAC Hdr for last RLC PDU = 1 and compute 1 or 2 byte padding
+	if (post_padding == 0) {
+	    total_rlc_pdu_header_len -= rlc_pdu_header_len_last;
+	    padding_len =
+		buflen - (bsr_len + phr_len + total_rlc_pdu_header_len +
+			  sdu_length_total + 1);
+	    short_padding = padding_len;
+	    total_rlc_pdu_header_len++;
+	}
+    } else {
+	if (padding_len == buflen) {	// nona mac pdu
+	    *access_mode = CANCELED_ACCESS;
+	}
+
+	short_padding = 0;
+
+	post_padding =
+	    buflen - (bsr_len + phr_len + total_rlc_pdu_header_len +
+		      sdu_length_total + 1);
+    }
+
+    // Generate header
+    // if (num_sdus>0) {
+
+    payload_offset = generate_ulsch_header(ulsch_buffer,	// mac header
+					   num_sdus,	// num sdus
+					   short_padding,	// short pading
+					   sdu_lengths,	// sdu length
+					   sdu_lcids,	// sdu lcid
+					   phr_p,	// power headroom
+					   NULL,	// crnti
+					   bsr_t,	// truncated bsr
+					   bsr_s,	// short bsr
+					   bsr_l, post_padding);	// long_bsr
+
+    LOG_D(MAC,
+	  "[UE %d] Generate header :bufflen %d  sdu_length_total %d, num_sdus %d, sdu_lengths[0] %d, sdu_lcids[0] %d => payload offset %d,  total_rlc_pdu_header_len %d, padding %d,post_padding %d, bsr len %d, phr len %d, reminder %d \n",
+	  module_idP, buflen, sdu_length_total, num_sdus, sdu_lengths[0],
+	  sdu_lcids[0], payload_offset, total_rlc_pdu_header_len,
+	  short_padding, post_padding, bsr_len, phr_len,
+	  buflen - sdu_length_total - payload_offset);
+    // cycle through SDUs and place in ulsch_buffer
+    if (sdu_length_total) {
+	memcpy(&ulsch_buffer[payload_offset], ulsch_buff,
+	       sdu_length_total);
+    }
+    // fill remainder of DLSCH with random data
+    if (post_padding) {
+	for (j = 0; j < (buflen - sdu_length_total - payload_offset); j++) {
+	    ulsch_buffer[payload_offset + sdu_length_total + j] =
+		(char) (taus() & 0xff);
+	}
+    }
+    LOG_D(MAC,
+	  "[UE %d][SR] Gave SDU to PHY, clearing any scheduling request\n",
+	  module_idP);
+    UE_mac_inst[module_idP].scheduling_info.SR_pending = 0;
+    UE_mac_inst[module_idP].scheduling_info.SR_COUNTER = 0;
+
+    /* Actions when a BSR is sent */
+    if (bsr_ce_len) {
+	LOG_D(MAC,
+	      "[UE %d] MAC BSR Sent !! bsr (ce%d,hdr%d) buff_len %d\n",
+	      module_idP, bsr_ce_len, bsr_header_len, buflen);
+
+	// Reset ReTx BSR Timer
+	UE_mac_inst[module_idP].scheduling_info.retxBSR_SF =
+	    get_sf_retxBSRTimer(UE_mac_inst[module_idP].
+				scheduling_info.retxBSR_Timer);
+
+	LOG_D(MAC, "[UE %d] MAC ReTx BSR Timer Reset =%d\n", module_idP,
+	      UE_mac_inst[module_idP].scheduling_info.retxBSR_SF);
+
+	// Reset Periodic Timer except when BSR is truncated
+	if ((bsr_t == NULL)
+	    && (UE_mac_inst[module_idP].scheduling_info.
+		periodicBSR_Timer != PeriodicBSR_Timer_r12_infinity)) {
+	    UE_mac_inst[module_idP].scheduling_info.periodicBSR_SF =
+		get_sf_periodicBSRTimer(UE_mac_inst
+					[module_idP].scheduling_info.
+					periodicBSR_Timer);
+
+	    LOG_D(MAC, "[UE %d] MAC Periodic BSR Timer Reset =%d\n",
+		  module_idP,
+		  UE_mac_inst[module_idP].scheduling_info.periodicBSR_SF);
+
+	}
+	// Reset BSR Trigger flags
+	UE_mac_inst[module_idP].BSR_reporting_active = BSR_TRIGGER_NONE;
+    }
+
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_GET_SDU, VCD_FUNCTION_OUT);
 #if UE_TIMING_TRACE
-  stop_meas(&UE_mac_inst[module_idP].tx_ulsch_sdu);
+    stop_meas(&UE_mac_inst[module_idP].tx_ulsch_sdu);
 #endif
-  
-  if (opt_enabled) {
-    trace_pdu(0, ulsch_buffer, buflen, module_idP, 3, UE_mac_inst[module_idP].crnti, UE_mac_inst[module_idP].txFrame, UE_mac_inst[module_idP].txSubframe, 0, 0);
-    LOG_D(OPT,"[UE %d][ULSCH] Frame %d subframe %d trace pdu for rnti %x  with size %d\n",
-          module_idP, frameP, subframe, UE_mac_inst[module_idP].crnti, buflen);
-  }
+
+    if (opt_enabled) {
+	trace_pdu(0, ulsch_buffer, buflen, module_idP, 3,
+		  UE_mac_inst[module_idP].crnti,
+		  UE_mac_inst[module_idP].txFrame,
+		  UE_mac_inst[module_idP].txSubframe, 0, 0);
+	LOG_D(OPT,
+	      "[UE %d][ULSCH] Frame %d subframe %d trace pdu for rnti %x  with size %d\n",
+	      module_idP, frameP, subframe, UE_mac_inst[module_idP].crnti,
+	      buflen);
+    }
 }
 
 //------------------------------------------------------------------------------
@@ -1820,281 +2153,327 @@ for (lcid=DCCH; (lcid < MAX_NUM_LCID) && (is_all_lcid_processed == FALSE) ; lcid
 // 4. Perform PHR procedures
 
 UE_L2_STATE_t
-ue_scheduler(
-  const module_id_t    module_idP,
-  const frame_t        rxFrameP,
-  const sub_frame_t    rxSubframeP,
-  const frame_t        txFrameP,
-  const sub_frame_t    txSubframeP,
-  const lte_subframe_t directionP,
-  const uint8_t        eNB_indexP,
-  const int            CC_id)
+ue_scheduler(const module_id_t module_idP,
+	     const frame_t rxFrameP,
+	     const sub_frame_t rxSubframeP,
+	     const frame_t txFrameP,
+	     const sub_frame_t txSubframeP,
+	     const lte_subframe_t directionP,
+	     const uint8_t eNB_indexP, const int CC_id)
 //------------------------------------------------------------------------------
 {
-  int lcid; // lcid index
-  int TTI= 1;
-  int bucketsizeduration = -1;
-  int bucketsizeduration_max = -1;
-  // mac_rlc_status_resp_t rlc_status[MAX_NUM_LCGID]; // 4
-  // int8_t lcg_id;
-  struct RACH_ConfigCommon *rach_ConfigCommon = (struct RACH_ConfigCommon *)NULL;
-  protocol_ctxt_t   ctxt;
+    int lcid;			// lcid index
+    int TTI = 1;
+    int bucketsizeduration = -1;
+    int bucketsizeduration_max = -1;
+    // mac_rlc_status_resp_t rlc_status[MAX_NUM_LCGID]; // 4
+    // int8_t lcg_id;
+    struct RACH_ConfigCommon *rach_ConfigCommon =
+	(struct RACH_ConfigCommon *) NULL;
+    protocol_ctxt_t ctxt;
 
 #if defined(ENABLE_ITTI)
-  MessageDef   *msg_p;
-  const char   *msg_name;
-  instance_t    instance;
-  int           result;
+    MessageDef *msg_p;
+    const char *msg_name;
+    instance_t instance;
+    int result;
 #endif
 #if UE_TIMING_TRACE
-  start_meas(&UE_mac_inst[module_idP].ue_scheduler);
+    start_meas(&UE_mac_inst[module_idP].ue_scheduler);
 #endif
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER, VCD_FUNCTION_IN);
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER, VCD_FUNCTION_IN);
 
-  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, module_idP, ENB_FLAG_NO, UE_mac_inst[module_idP].crnti, txFrameP, txSubframeP,eNB_indexP);
+    PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, module_idP, ENB_FLAG_NO,
+				   UE_mac_inst[module_idP].crnti, txFrameP,
+				   txSubframeP, eNB_indexP);
 #if defined(ENABLE_ITTI)
 
-  do {
-    // Checks if a message has been sent to MAC sub-task
-    itti_poll_msg (TASK_MAC_UE, &msg_p);
+    do {
+	// Checks if a message has been sent to MAC sub-task
+	itti_poll_msg(TASK_MAC_UE, &msg_p);
 
-    if (msg_p != NULL) {
-      msg_name = ITTI_MSG_NAME (msg_p);
-      instance = ITTI_MSG_INSTANCE (msg_p);
+	if (msg_p != NULL) {
+	    msg_name = ITTI_MSG_NAME(msg_p);
+	    instance = ITTI_MSG_INSTANCE(msg_p);
 
-      switch (ITTI_MSG_ID(msg_p)) {
-      case RRC_MAC_CCCH_DATA_REQ:
-        LOG_I(MAC, "Received %s from %s: instance %d, frameP %d, eNB_index %d\n",
-              msg_name, ITTI_MSG_ORIGIN_NAME(msg_p), instance,
-              RRC_MAC_CCCH_DATA_REQ (msg_p).frame, RRC_MAC_CCCH_DATA_REQ (msg_p).enb_index);
+	    switch (ITTI_MSG_ID(msg_p)) {
+	    case RRC_MAC_CCCH_DATA_REQ:
+		LOG_I(MAC,
+		      "Received %s from %s: instance %d, frameP %d, eNB_index %d\n",
+		      msg_name, ITTI_MSG_ORIGIN_NAME(msg_p), instance,
+		      RRC_MAC_CCCH_DATA_REQ(msg_p).frame,
+		      RRC_MAC_CCCH_DATA_REQ(msg_p).enb_index);
 
-        // TODO process CCCH data req.
-        break;
+		// TODO process CCCH data req.
+		break;
 
 
-      default:
-        LOG_E(MAC, "Received unexpected message %s\n", msg_name);
-        break;
-      }
+	    default:
+		LOG_E(MAC, "Received unexpected message %s\n", msg_name);
+		break;
+	    }
 
-      result = itti_free (ITTI_MSG_ORIGIN_ID(msg_p), msg_p);
-      AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
+	    result = itti_free(ITTI_MSG_ORIGIN_ID(msg_p), msg_p);
+	    AssertFatal(result == EXIT_SUCCESS,
+			"Failed to free memory (%d)!\n", result);
+	}
     }
-  } while(msg_p != NULL);
+    while (msg_p != NULL);
 
 #endif
 
-  //Mac_rlc_xface->frameP=frameP;
-  //Rrc_xface->Frame_index=Mac_rlc_xface->frameP;
-  //if (subframe%5 == 0)
-  //LG#ifdef EXMIMO
-  pdcp_run(&ctxt);
-  //#endif
-  UE_mac_inst[module_idP].txFrame    = txFrameP;
-  UE_mac_inst[module_idP].txSubframe = txSubframeP;
-  UE_mac_inst[module_idP].rxFrame    = rxFrameP;
-  UE_mac_inst[module_idP].rxSubframe = rxSubframeP;
+    //Mac_rlc_xface->frameP=frameP;
+    //Rrc_xface->Frame_index=Mac_rlc_xface->frameP;
+    //if (subframe%5 == 0)
+    //LG#ifdef EXMIMO
+    pdcp_run(&ctxt);
+    //#endif
+    UE_mac_inst[module_idP].txFrame = txFrameP;
+    UE_mac_inst[module_idP].txSubframe = txSubframeP;
+    UE_mac_inst[module_idP].rxFrame = rxFrameP;
+    UE_mac_inst[module_idP].rxSubframe = rxSubframeP;
 
 #ifdef CELLULAR
-  rrc_rx_tx(module_idP, txFrameP, 0, eNB_indexP);
+    rrc_rx_tx(module_idP, txFrameP, 0, eNB_indexP);
 #else
 
-  switch (rrc_rx_tx(&ctxt,
-                    eNB_indexP,
-                    CC_id)) {
-  case RRC_OK:
-    break;
+    switch (rrc_rx_tx(&ctxt, eNB_indexP, CC_id)) {
+    case RRC_OK:
+	break;
 
-  case RRC_ConnSetup_failed:
-    LOG_E(MAC,"RRCConnectionSetup failed, returning to IDLE state\n");
-    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER, VCD_FUNCTION_OUT);
+    case RRC_ConnSetup_failed:
+	LOG_E(MAC, "RRCConnectionSetup failed, returning to IDLE state\n");
+	VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	    (VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER, VCD_FUNCTION_OUT);
 #if UE_TIMING_TRACE
-    stop_meas(&UE_mac_inst[module_idP].ue_scheduler);
+	stop_meas(&UE_mac_inst[module_idP].ue_scheduler);
 #endif
-    return(CONNECTION_LOST);
-    break;
+	return (CONNECTION_LOST);
+	break;
 
-  case RRC_PHY_RESYNCH:
-    LOG_E(MAC,"RRC Loss of synch, returning PHY_RESYNCH\n");
-    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER, VCD_FUNCTION_OUT);
+    case RRC_PHY_RESYNCH:
+	LOG_E(MAC, "RRC Loss of synch, returning PHY_RESYNCH\n");
+	VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	    (VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER, VCD_FUNCTION_OUT);
 #if UE_TIMING_TRACE
-    stop_meas(&UE_mac_inst[module_idP].ue_scheduler);
+	stop_meas(&UE_mac_inst[module_idP].ue_scheduler);
 #endif
-    return(PHY_RESYNCH);
-
-  case RRC_Handover_failed:
-    LOG_N(MAC,"Handover failure for UE %d eNB_index %d\n",module_idP,eNB_indexP);
-    //Invalid...need to add another MAC UE state for re-connection procedure
-    phy_config_afterHO_ue(module_idP,0,eNB_indexP,(MobilityControlInfo_t *)NULL,1);
-    //return(3);
-    break;
-
-  case RRC_HO_STARTED:
-    LOG_I(MAC,"RRC handover, Instruct PHY to start the contention-free PRACH and synchronization\n");
-    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER, VCD_FUNCTION_OUT);
+	return (PHY_RESYNCH);
+
+    case RRC_Handover_failed:
+	LOG_N(MAC, "Handover failure for UE %d eNB_index %d\n", module_idP,
+	      eNB_indexP);
+	//Invalid...need to add another MAC UE state for re-connection procedure
+	phy_config_afterHO_ue(module_idP, 0, eNB_indexP,
+			      (MobilityControlInfo_t *) NULL, 1);
+	//return(3);
+	break;
+
+    case RRC_HO_STARTED:
+	LOG_I(MAC,
+	      "RRC handover, Instruct PHY to start the contention-free PRACH and synchronization\n");
+	VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	    (VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER, VCD_FUNCTION_OUT);
 #if UE_TIMING_TRACE
-    stop_meas(&UE_mac_inst[module_idP].ue_scheduler);
+	stop_meas(&UE_mac_inst[module_idP].ue_scheduler);
 #endif
-    return(PHY_HO_PRACH);
+	return (PHY_HO_PRACH);
 
-  default:
-    break;
-  }
+    default:
+	break;
+    }
 
 #endif
 
-  // Check Contention resolution timer (put in a function later)
-  if (UE_mac_inst[module_idP].RA_contention_resolution_timer_active == 1) {
+    // Check Contention resolution timer (put in a function later)
+    if (UE_mac_inst[module_idP].RA_contention_resolution_timer_active == 1) {
 
-    if (UE_mac_inst[module_idP].radioResourceConfigCommon) {
-      rach_ConfigCommon = &UE_mac_inst[module_idP].radioResourceConfigCommon->rach_ConfigCommon;
-    } else {
-      LOG_E(MAC,"FATAL: radioResourceConfigCommon is NULL!!!\n");
-      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER, VCD_FUNCTION_OUT);
+	if (UE_mac_inst[module_idP].radioResourceConfigCommon) {
+	    rach_ConfigCommon =
+		&UE_mac_inst[module_idP].
+		radioResourceConfigCommon->rach_ConfigCommon;
+	} else {
+	    LOG_E(MAC, "FATAL: radioResourceConfigCommon is NULL!!!\n");
+	    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+		(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER,
+		 VCD_FUNCTION_OUT);
 
-      stop_meas(&UE_mac_inst[module_idP].ue_scheduler);
-      AssertFatal(1==0,"");
+	    stop_meas(&UE_mac_inst[module_idP].ue_scheduler);
+	    AssertFatal(1 == 0, "");
 
 #if UE_TIMING_TRACE
-      stop_meas(&UE_mac_inst[module_idP].ue_scheduler);
+	    stop_meas(&UE_mac_inst[module_idP].ue_scheduler);
 #endif
-      //return(RRC_OK);
-    }
-
-    LOG_I(MAC,"Frame %d: Contention resolution timer %d/%ld\n",txFrameP,UE_mac_inst[module_idP].RA_contention_resolution_cnt,
-          ((1+rach_ConfigCommon->ra_SupervisionInfo.mac_ContentionResolutionTimer)<<3));
-
-    UE_mac_inst[module_idP].RA_contention_resolution_cnt++;
-
-    if (UE_mac_inst[module_idP].RA_contention_resolution_cnt ==
-        ((1+rach_ConfigCommon->ra_SupervisionInfo.mac_ContentionResolutionTimer)<<3)) {
-      UE_mac_inst[module_idP].RA_active = 0;
-      UE_mac_inst[module_idP].RA_contention_resolution_timer_active = 0;
-      // Signal PHY to quit RA procedure
-      LOG_E(MAC,"Module id %u Contention resolution timer expired, RA failed\n", module_idP);
-      ra_failed(module_idP,0,eNB_indexP);
-    }
-  }
-
-
-  // Get RLC status info and update Bj for all lcids that are active
-  for (lcid=DCCH; lcid < MAX_NUM_LCID; lcid++ ) {
-    if (UE_mac_inst[module_idP].logicalChannelConfig[lcid]) {
-      // meausre the Bj
-      if ((directionP == SF_UL)&& (UE_mac_inst[module_idP].scheduling_info.Bj[lcid] >= 0)) {
-        if (UE_mac_inst[module_idP].logicalChannelConfig[lcid]->ul_SpecificParameters) {
-          bucketsizeduration = UE_mac_inst[module_idP].logicalChannelConfig[lcid]->ul_SpecificParameters->prioritisedBitRate * TTI;
-          bucketsizeduration_max = get_ms_bucketsizeduration(UE_mac_inst[module_idP].logicalChannelConfig[lcid]->ul_SpecificParameters->bucketSizeDuration);
-        } else {
-          LOG_E(MAC,"[UE %d] lcid %d, NULL ul_SpecificParameters\n",module_idP,lcid);
-          AssertFatal(1==0,"");
-        }
-
-        if ( UE_mac_inst[module_idP].scheduling_info.Bj[lcid] > bucketsizeduration_max ) {
-          UE_mac_inst[module_idP].scheduling_info.Bj[lcid] = bucketsizeduration_max;
-        } else {
-          UE_mac_inst[module_idP].scheduling_info.Bj[lcid] = bucketsizeduration;
-        }
-      }
-
-
-      /*
-      if (lcid == DCCH) {    
-        LOG_D(MAC,"[UE %d][SR] Frame %d subframe %d Pending data for SRB1=%d for LCGID %d \n",                  
-        module_idP, txFrameP,txSubframeP,UE_mac_inst[module_idP].scheduling_info.BSR[UE_mac_inst[module_idP].scheduling_info.LCGID[lcid]],                  
-//         UE_mac_inst[module_idP].scheduling_info.LCGID[lcid]);
-      }
-      */
-    }
-  }
-
-  // Call BSR procedure as described in Section 5.4.5 in 36.321
-
-  // First check ReTxBSR Timer because it is always configured
-  // Decrement ReTxBSR Timer if it is running and not null
-  if ((UE_mac_inst[module_idP].scheduling_info.retxBSR_SF != MAC_UE_BSR_TIMER_NOT_RUNNING)
-          && (UE_mac_inst[module_idP].scheduling_info.retxBSR_SF != 0)){
-      UE_mac_inst[module_idP].scheduling_info.retxBSR_SF --;
-  }
-
-  // Decrement Periodic Timer if it is running and not null
-  if ((UE_mac_inst[module_idP].scheduling_info.periodicBSR_SF != MAC_UE_BSR_TIMER_NOT_RUNNING)
-            && (UE_mac_inst[module_idP].scheduling_info.periodicBSR_SF != 0)){
-        UE_mac_inst[module_idP].scheduling_info.periodicBSR_SF--;
-    }
-
-  //Check whether Regular BSR is triggered
-  if (update_bsr(module_idP,txFrameP, txSubframeP,eNB_indexP) == TRUE) {
-  // call SR procedure to generate pending SR and BSR for next PUCCH/PUSCH TxOp.  This should implement the procedures
-  // outlined in Sections 5.4.4 an 5.4.5 of 36.321
-    UE_mac_inst[module_idP].scheduling_info.SR_pending= 1;
-    // Regular BSR trigger
-    UE_mac_inst[module_idP].BSR_reporting_active |= BSR_TRIGGER_REGULAR;
-    LOG_D(MAC,"[UE %d][BSR] Regular BSR Triggered Frame %d subframe %d SR for PUSCH is pending\n",
-          module_idP, txFrameP,txSubframeP);
-  }
-
-  // UE has no valid phy config dedicated ||  no valid/released  SR
-  if ((UE_mac_inst[module_idP].physicalConfigDedicated == NULL)) {
-    // cancel all pending SRs
-    UE_mac_inst[module_idP].scheduling_info.SR_pending=0;
-    UE_mac_inst[module_idP].ul_active=0;
-    LOG_T(MAC,"[UE %d] Release all SRs \n", module_idP);
-    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER, VCD_FUNCTION_OUT);
+	    //return(RRC_OK);
+	}
+
+	LOG_I(MAC, "Frame %d: Contention resolution timer %d/%ld\n",
+	      txFrameP,
+	      UE_mac_inst[module_idP].RA_contention_resolution_cnt,
+	      ((1 +
+		rach_ConfigCommon->
+		ra_SupervisionInfo.mac_ContentionResolutionTimer) << 3));
+
+	UE_mac_inst[module_idP].RA_contention_resolution_cnt++;
+
+	if (UE_mac_inst[module_idP].RA_contention_resolution_cnt ==
+	    ((1 +
+	      rach_ConfigCommon->
+	      ra_SupervisionInfo.mac_ContentionResolutionTimer) << 3)) {
+	    UE_mac_inst[module_idP].RA_active = 0;
+	    UE_mac_inst[module_idP].RA_contention_resolution_timer_active =
+		0;
+	    // Signal PHY to quit RA procedure
+	    LOG_E(MAC,
+		  "Module id %u Contention resolution timer expired, RA failed\n",
+		  module_idP);
+	    ra_failed(module_idP, 0, eNB_indexP);
+	}
+    }
+    // Get RLC status info and update Bj for all lcids that are active
+    for (lcid = DCCH; lcid < MAX_NUM_LCID; lcid++) {
+	if (UE_mac_inst[module_idP].logicalChannelConfig[lcid]) {
+	    // meausre the Bj
+	    if ((directionP == SF_UL)
+		&& (UE_mac_inst[module_idP].scheduling_info.Bj[lcid] >= 0)) {
+		if (UE_mac_inst[module_idP].
+		    logicalChannelConfig[lcid]->ul_SpecificParameters) {
+		    bucketsizeduration =
+			UE_mac_inst[module_idP].logicalChannelConfig
+			[lcid]->ul_SpecificParameters->prioritisedBitRate *
+			TTI;
+		    bucketsizeduration_max =
+			get_ms_bucketsizeduration(UE_mac_inst
+						  [module_idP].logicalChannelConfig
+						  [lcid]->ul_SpecificParameters->bucketSizeDuration);
+		} else {
+		    LOG_E(MAC,
+			  "[UE %d] lcid %d, NULL ul_SpecificParameters\n",
+			  module_idP, lcid);
+		    AssertFatal(1 == 0, "");
+		}
+
+		if (UE_mac_inst[module_idP].scheduling_info.Bj[lcid] >
+		    bucketsizeduration_max) {
+		    UE_mac_inst[module_idP].scheduling_info.Bj[lcid] =
+			bucketsizeduration_max;
+		} else {
+		    UE_mac_inst[module_idP].scheduling_info.Bj[lcid] =
+			bucketsizeduration;
+		}
+	    }
+
+
+	    /*
+	       if (lcid == DCCH) {    
+	       LOG_D(MAC,"[UE %d][SR] Frame %d subframe %d Pending data for SRB1=%d for LCGID %d \n",                  
+	       module_idP, txFrameP,txSubframeP,UE_mac_inst[module_idP].scheduling_info.BSR[UE_mac_inst[module_idP].scheduling_info.LCGID[lcid]],                  
+	       //         UE_mac_inst[module_idP].scheduling_info.LCGID[lcid]);
+	       }
+	     */
+	}
+    }
+
+    // Call BSR procedure as described in Section 5.4.5 in 36.321
+
+    // First check ReTxBSR Timer because it is always configured
+    // Decrement ReTxBSR Timer if it is running and not null
+    if ((UE_mac_inst[module_idP].scheduling_info.retxBSR_SF !=
+	 MAC_UE_BSR_TIMER_NOT_RUNNING)
+	&& (UE_mac_inst[module_idP].scheduling_info.retxBSR_SF != 0)) {
+	UE_mac_inst[module_idP].scheduling_info.retxBSR_SF--;
+    }
+    // Decrement Periodic Timer if it is running and not null
+    if ((UE_mac_inst[module_idP].scheduling_info.periodicBSR_SF !=
+	 MAC_UE_BSR_TIMER_NOT_RUNNING)
+	&& (UE_mac_inst[module_idP].scheduling_info.periodicBSR_SF != 0)) {
+	UE_mac_inst[module_idP].scheduling_info.periodicBSR_SF--;
+    }
+    //Check whether Regular BSR is triggered
+    if (update_bsr(module_idP, txFrameP, txSubframeP, eNB_indexP) == TRUE) {
+	// call SR procedure to generate pending SR and BSR for next PUCCH/PUSCH TxOp.  This should implement the procedures
+	// outlined in Sections 5.4.4 an 5.4.5 of 36.321
+	UE_mac_inst[module_idP].scheduling_info.SR_pending = 1;
+	// Regular BSR trigger
+	UE_mac_inst[module_idP].BSR_reporting_active |=
+	    BSR_TRIGGER_REGULAR;
+	LOG_D(MAC,
+	      "[UE %d][BSR] Regular BSR Triggered Frame %d subframe %d SR for PUSCH is pending\n",
+	      module_idP, txFrameP, txSubframeP);
+    }
+    // UE has no valid phy config dedicated ||  no valid/released  SR
+    if ((UE_mac_inst[module_idP].physicalConfigDedicated == NULL)) {
+	// cancel all pending SRs
+	UE_mac_inst[module_idP].scheduling_info.SR_pending = 0;
+	UE_mac_inst[module_idP].ul_active = 0;
+	LOG_T(MAC, "[UE %d] Release all SRs \n", module_idP);
+	VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	    (VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER, VCD_FUNCTION_OUT);
 #if UE_TIMING_TRACE
-    stop_meas(&UE_mac_inst[module_idP].ue_scheduler);
+	stop_meas(&UE_mac_inst[module_idP].ue_scheduler);
 #endif
-    return(CONNECTION_OK);
-  }
-
-  if ((UE_mac_inst[module_idP].physicalConfigDedicated->schedulingRequestConfig == NULL) ||
-      (UE_mac_inst[module_idP].physicalConfigDedicated->schedulingRequestConfig->present == SchedulingRequestConfig_PR_release)) {
+	return (CONNECTION_OK);
+    }
 
-    // initiate RA with CRNTI included in msg3 (no contention) as descibed in 36.321 sec 5.1.5
+    if ((UE_mac_inst[module_idP].
+	 physicalConfigDedicated->schedulingRequestConfig == NULL)
+	|| (UE_mac_inst[module_idP].
+	    physicalConfigDedicated->schedulingRequestConfig->present ==
+	    SchedulingRequestConfig_PR_release)) {
 
-    // cancel all pending SRs
-    UE_mac_inst[module_idP].scheduling_info.SR_pending=0;
-    UE_mac_inst[module_idP].ul_active=0;
-    LOG_T(MAC,"[UE %d] Release all SRs \n", module_idP);
-  }
+	// initiate RA with CRNTI included in msg3 (no contention) as descibed in 36.321 sec 5.1.5
 
-  // Put this in a function
-  // Call PHR procedure as described in Section 5.4.6 in 36.321
-  if (UE_mac_inst[module_idP].PHR_state == MAC_MainConfig__phr_Config_PR_setup) { // normal operation
+	// cancel all pending SRs
+	UE_mac_inst[module_idP].scheduling_info.SR_pending = 0;
+	UE_mac_inst[module_idP].ul_active = 0;
+	LOG_T(MAC, "[UE %d] Release all SRs \n", module_idP);
+    }
+    // Put this in a function
+    // Call PHR procedure as described in Section 5.4.6 in 36.321
+    if (UE_mac_inst[module_idP].PHR_state == MAC_MainConfig__phr_Config_PR_setup) {	// normal operation
+
+	if (UE_mac_inst[module_idP].PHR_reconfigured == 1) {	// upon (re)configuration of the power headroom reporting functionality by upper layers
+	    UE_mac_inst[module_idP].PHR_reporting_active = 1;
+	    UE_mac_inst[module_idP].PHR_reconfigured = 0;
+	} else {
+	    //LOG_D(MAC,"PHR normal operation %d active %d \n", UE_mac_inst[module_idP].scheduling_info.periodicPHR_SF, UE_mac_inst[module_idP].PHR_reporting_active);
+	    if ((UE_mac_inst[module_idP].scheduling_info.prohibitPHR_SF <=
+		 0)
+		&&
+		((get_PL(module_idP, 0, eNB_indexP) <
+		  UE_mac_inst[module_idP].scheduling_info.
+		  PathlossChange_db)
+		 || (UE_mac_inst[module_idP].power_backoff_db[eNB_indexP] >
+		     UE_mac_inst[module_idP].
+		     scheduling_info.PathlossChange_db)))
+		// trigger PHR and reset the timer later when the PHR report is sent
+	    {
+		UE_mac_inst[module_idP].PHR_reporting_active = 1;
+	    } else if (UE_mac_inst[module_idP].PHR_reporting_active == 0) {
+		UE_mac_inst[module_idP].scheduling_info.prohibitPHR_SF--;
+	    }
+
+	    if (UE_mac_inst[module_idP].scheduling_info.periodicPHR_SF <=
+		0)
+		// trigger PHR and reset the timer later when the PHR report is sent
+	    {
+		UE_mac_inst[module_idP].PHR_reporting_active = 1;
+	    } else if (UE_mac_inst[module_idP].PHR_reporting_active == 0) {
+		UE_mac_inst[module_idP].scheduling_info.periodicPHR_SF--;
+	    }
+	}
+    } else {			// release / nothing
+	UE_mac_inst[module_idP].PHR_reporting_active = 0;	// release PHR
+    }
 
-    if (UE_mac_inst[module_idP].PHR_reconfigured == 1) { // upon (re)configuration of the power headroom reporting functionality by upper layers
-      UE_mac_inst[module_idP].PHR_reporting_active = 1;
-      UE_mac_inst[module_idP].PHR_reconfigured = 0;
-    } else {
-      //LOG_D(MAC,"PHR normal operation %d active %d \n", UE_mac_inst[module_idP].scheduling_info.periodicPHR_SF, UE_mac_inst[module_idP].PHR_reporting_active);
-      if ((UE_mac_inst[module_idP].scheduling_info.prohibitPHR_SF <= 0) &&
-          ((get_PL(module_idP,0,eNB_indexP) <  UE_mac_inst[module_idP].scheduling_info.PathlossChange_db) ||
-           (UE_mac_inst[module_idP].power_backoff_db[eNB_indexP] > UE_mac_inst[module_idP].scheduling_info.PathlossChange_db)))
-        // trigger PHR and reset the timer later when the PHR report is sent
-      {
-        UE_mac_inst[module_idP].PHR_reporting_active = 1;
-      } else if (UE_mac_inst[module_idP].PHR_reporting_active ==0 ) {
-        UE_mac_inst[module_idP].scheduling_info.prohibitPHR_SF--;
-      }
-
-      if (UE_mac_inst[module_idP].scheduling_info.periodicPHR_SF <= 0 )
-        // trigger PHR and reset the timer later when the PHR report is sent
-      {
-        UE_mac_inst[module_idP].PHR_reporting_active = 1;
-      } else if (UE_mac_inst[module_idP].PHR_reporting_active == 0 ) {
-        UE_mac_inst[module_idP].scheduling_info.periodicPHR_SF--;
-      }
-    }
-  } else {    // release / nothing
-    UE_mac_inst[module_idP].PHR_reporting_active = 0; // release PHR
-  }
-
-  //If the UE has UL resources allocated for new transmission for this TTI here:
-  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER, VCD_FUNCTION_OUT);
+    //If the UE has UL resources allocated for new transmission for this TTI here:
+    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME
+	(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SCHEDULER, VCD_FUNCTION_OUT);
 #if UE_TIMING_TRACE
     stop_meas(&UE_mac_inst[module_idP].ue_scheduler);
 #endif
-  return(CONNECTION_OK);
+    return (CONNECTION_OK);
 }
 
 // to be improved
@@ -2102,522 +2481,564 @@ ue_scheduler(
 extern int cba_backoff;
 double uniform_rngen(int min, int max)
 {
-  double random = (double)taus()/((double)0xffffffff);
-  return (max - min) * random + min;
+    double random = (double) taus() / ((double) 0xffffffff);
+    return (max - min) * random + min;
 }
 
-int cba_access(module_id_t module_idP,frame_t frameP,sub_frame_t subframe, uint8_t eNB_index,uint16_t buflen)
+int
+cba_access(module_id_t module_idP, frame_t frameP,
+	   sub_frame_t subframe, uint8_t eNB_index, uint16_t buflen)
 {
 
-  mac_rlc_status_resp_t rlc_status;
-  int header_offset=4;
-  int rv =0;
-
-  /*
-  if (( ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1]>0)&&(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1]<64))   ||
-      ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2]>0)&&(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2]<64))   ||
-      ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3]>0)&&(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3]<64)) )
-      //  && (UE_mac_inst[module_idP].ul_active == 0) // check if the ul is acrtive
-      && (UE_mac_inst[module_idP].cba_last_access[0] <= 0) ) { // backoff
-      //  LOG_D(MAC,"[UE %d] Frame %d Subframe %d: the current CBA backoff is %d \n", module_idP, frameP, subframe,
-      //  UE_mac_inst[module_idP].cba_last_access[0] );
-
-      UE_mac_inst[module_idP].cba_last_access[0]= round(uniform_rngen(1,40));
-      LOG_D(MAC,"[UE %d] Frame %d Subframe %d: start a new CBA backoff  %d UL active state %d \n", module_idP, frameP, subframe,
-          UE_mac_inst[module_idP].cba_last_access[0], UE_mac_inst[module_idP].ul_active);
-
-      rv=1;
-  } else if (( ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1]> 0 ))   ||
-      ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2]> 0 ))   ||
-      ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3]> 0 )) )
-      // && (UE_mac_inst[module_idP].ul_active == 0) // check if the ul is acrtive
-      && (UE_mac_inst[module_idP].cba_last_access[0]> 0) ){
-
-      UE_mac_inst[module_idP].cba_last_access[0]-=1;
-      LOG_D(MAC,"[UE %d] Frame %d Subframe %d: CBA backoff is decreased by one to %d UL active state %d \n",
-          module_idP, frameP, subframe,
-          UE_mac_inst[module_idP].cba_last_access[0], UE_mac_inst[module_idP].ul_active);
-
-  } else if (( ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1] == 0 ))   &&
-               ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2] == 0 ))   &&
-               ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3] ==  0 )) )
-             && (UE_mac_inst[module_idP].cba_last_access[0]> 0) ){
-    UE_mac_inst[module_idP].cba_last_access[0]-=1;
-    }*/
-
-  if ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID0]>0)&&(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID0]<64) ) {
-    return 0;
-  }
-
-  if ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1] <= 0 ) &&
-      (UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2] <= 0 ) &&
-      (UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3] <= 0 ) ) {
-    return 0;
-  }
-
-  if (cba_backoff == 0 ) { // apply probablisitc method
-    UE_mac_inst[module_idP].cba_last_access[0]= uniform_rngen(0,1);
-
-    if (uniform_rngen(0,1) > 0.6 ) {
-      LOG_I(MAC,"[UE %d] Frame %d Subframe %d: CBA probability-based backoff (%d), UL active state %d \n", module_idP, frameP, subframe,
-            cba_backoff,UE_mac_inst[module_idP].ul_active);
-
-      rv=1;
-    }
-  } else {
-
-    if (UE_mac_inst[module_idP].cba_last_access[0] <= 0) {
-      UE_mac_inst[module_idP].cba_last_access[0]= round(uniform_rngen(1,cba_backoff));
-
-      LOG_I(MAC,"[UE %d] Frame %d Subframe %d: start a new CBA backoff  %d/%d UL active state %d \n", module_idP, frameP, subframe,
-            UE_mac_inst[module_idP].cba_last_access[0], cba_backoff,UE_mac_inst[module_idP].ul_active);
-
-      rv = 1;
-      /*
-      rlc_status = mac_rlc_status_ind(module_idP, UE_mac_inst[module_idP].crnti,frameP,ENB_FLAG_NO,MBMS_FLAG_NO, // eNB_index
-      DTCH,
-      0);
-
-      if ((
-      //  (rlc_status.pdus_in_buffer > 0 )           &&
-      // (UE_mac_inst[module_idP].ul_active == 0)  && // check if the ul is acrtive
-      (rlc_status.head_sdu_is_segmented  == 0 )          &&
-      ((rlc_status.head_sdu_remaining_size_to_send + header_offset ) <= buflen )
-      )){
-      rv = 1;
-
-      UE_mac_inst[module_idP].cba_last_access[0]= round(uniform_rngen(1,30));
-      LOG_D(MAC,"[UE %d] Frame %d Subframe %d: start a new CBA backoff  %d UL active state %d \n", module_idP, frameP, subframe,
-      UE_mac_inst[module_idP].cba_last_access[0], UE_mac_inst[module_idP].ul_active);
-      */
+    mac_rlc_status_resp_t rlc_status;
+    int header_offset = 4;
+    int rv = 0;
+
+    /*
+       if (( ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1]>0)&&(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1]<64))   ||
+       ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2]>0)&&(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2]<64))   ||
+       ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3]>0)&&(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3]<64)) )
+       //  && (UE_mac_inst[module_idP].ul_active == 0) // check if the ul is acrtive
+       && (UE_mac_inst[module_idP].cba_last_access[0] <= 0) ) { // backoff
+       //  LOG_D(MAC,"[UE %d] Frame %d Subframe %d: the current CBA backoff is %d \n", module_idP, frameP, subframe,
+       //  UE_mac_inst[module_idP].cba_last_access[0] );
+
+       UE_mac_inst[module_idP].cba_last_access[0]= round(uniform_rngen(1,40));
+       LOG_D(MAC,"[UE %d] Frame %d Subframe %d: start a new CBA backoff  %d UL active state %d \n", module_idP, frameP, subframe,
+       UE_mac_inst[module_idP].cba_last_access[0], UE_mac_inst[module_idP].ul_active);
+
+       rv=1;
+       } else if (( ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1]> 0 ))   ||
+       ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2]> 0 ))   ||
+       ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3]> 0 )) )
+       // && (UE_mac_inst[module_idP].ul_active == 0) // check if the ul is acrtive
+       && (UE_mac_inst[module_idP].cba_last_access[0]> 0) ){
+
+       UE_mac_inst[module_idP].cba_last_access[0]-=1;
+       LOG_D(MAC,"[UE %d] Frame %d Subframe %d: CBA backoff is decreased by one to %d UL active state %d \n",
+       module_idP, frameP, subframe,
+       UE_mac_inst[module_idP].cba_last_access[0], UE_mac_inst[module_idP].ul_active);
+
+       } else if (( ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1] == 0 ))   &&
+       ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2] == 0 ))   &&
+       ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3] ==  0 )) )
+       && (UE_mac_inst[module_idP].cba_last_access[0]> 0) ){
+       UE_mac_inst[module_idP].cba_last_access[0]-=1;
+       } */
+
+    if ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID0] > 0)
+	&& (UE_mac_inst[module_idP].scheduling_info.BSR[LCGID0] < 64)) {
+	return 0;
+    }
+
+    if ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1] <= 0) &&
+	(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2] <= 0) &&
+	(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3] <= 0)) {
+	return 0;
+    }
+
+    if (cba_backoff == 0) {	// apply probablisitc method
+	UE_mac_inst[module_idP].cba_last_access[0] = uniform_rngen(0, 1);
+
+	if (uniform_rngen(0, 1) > 0.6) {
+	    LOG_I(MAC,
+		  "[UE %d] Frame %d Subframe %d: CBA probability-based backoff (%d), UL active state %d \n",
+		  module_idP, frameP, subframe, cba_backoff,
+		  UE_mac_inst[module_idP].ul_active);
+
+	    rv = 1;
+	}
     } else {
-      UE_mac_inst[module_idP].cba_last_access[0]-=1;
-      LOG_D(MAC,"[UE %d] Frame %d Subframe %d: wait for backoff to expire (%d) CBA UL active state %d \n",
-            module_idP, frameP, subframe,
-            UE_mac_inst[module_idP].cba_last_access[0], UE_mac_inst[module_idP].ul_active);
-    }
-  }
-
-  return rv;
-  /*
-    if (( ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1]>0)&&(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1]<64))   ||
-    ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2]>0)&&(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2]<64))   ||
-    ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3]>0)&&(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3]<64)) )
-  //  && (UE_mac_inst[module_idP].ul_active == 0) // check if the ul is acrtive
-  && (UE_mac_inst[module_idP].cba_last_access[0] <= 0) ) {
-
-      UE_mac_inst[module_idP].cba_last_access[0]= round(uniform_rngen(1,cba_backoff));
-
-      LOG_I(MAC,"[UE %d] Frame %d Subframe %d: start a new CBA backoff  %d/%d UL active state %d \n", module_idP, frameP, subframe,
-      UE_mac_inst[module_idP].cba_last_access[0], cba_backoff,UE_mac_inst[module_idP].ul_active);
-
-      rv = 1;
-
-    rlc_status = mac_rlc_status_ind(module_idP, UE_mac_inst[module_idP].crnti,frameP,ENB_FLAG_NO,MBMS_FLAG_NO, // eNB_index
-            DTCH,
-            0);
-
-    if ((
-   // (rlc_status.pdus_in_buffer > 0 )           &&
-  // (UE_mac_inst[module_idP].ul_active == 0)  && // check if the ul is acrtive
-   (rlc_status.head_sdu_is_segmented  == 0 )          &&
-   ((rlc_status.head_sdu_remaining_size_to_send + header_offset ) <= buflen )
-   )){
-      rv = 1;
-
-      UE_mac_inst[module_idP].cba_last_access[0]= round(uniform_rngen(1,30));
-      LOG_D(MAC,"[UE %d] Frame %d Subframe %d: start a new CBA backoff  %d UL active state %d \n", module_idP, frameP, subframe,
-      UE_mac_inst[module_idP].cba_last_access[0], UE_mac_inst[module_idP].ul_active);
-    } else
-      UE_mac_inst[module_idP].cba_last_access[0]= round(uniform_rngen(1,5));
-
-
-    } else if (( ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1]> 0 ))   ||
-     ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2]> 0 ))   ||
-     ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3]> 0 )) )
-         // && (UE_mac_inst[module_idP].ul_active == 0) // check if the ul is acrtive
-         && (UE_mac_inst[module_idP].cba_last_access[0]> 0) )
-      {
-
-  UE_mac_inst[module_idP].cba_last_access[0]-=1;
-  LOG_D(MAC,"[UE %d] Frame %d Subframe %d: wait for backoff to expire (%d) CBA UL active state %d \n",
-        module_idP, frameP, subframe,
-        UE_mac_inst[module_idP].cba_last_access[0], UE_mac_inst[module_idP].ul_active);
-      }
-  }
-  */
+
+	if (UE_mac_inst[module_idP].cba_last_access[0] <= 0) {
+	    UE_mac_inst[module_idP].cba_last_access[0] =
+		round(uniform_rngen(1, cba_backoff));
+
+	    LOG_I(MAC,
+		  "[UE %d] Frame %d Subframe %d: start a new CBA backoff  %d/%d UL active state %d \n",
+		  module_idP, frameP, subframe,
+		  UE_mac_inst[module_idP].cba_last_access[0], cba_backoff,
+		  UE_mac_inst[module_idP].ul_active);
+
+	    rv = 1;
+	    /*
+	       rlc_status = mac_rlc_status_ind(module_idP, UE_mac_inst[module_idP].crnti,frameP,ENB_FLAG_NO,MBMS_FLAG_NO, // eNB_index
+	       DTCH,
+	       0);
+
+	       if ((
+	       //  (rlc_status.pdus_in_buffer > 0 )           &&
+	       // (UE_mac_inst[module_idP].ul_active == 0)  && // check if the ul is acrtive
+	       (rlc_status.head_sdu_is_segmented  == 0 )          &&
+	       ((rlc_status.head_sdu_remaining_size_to_send + header_offset ) <= buflen )
+	       )){
+	       rv = 1;
+
+	       UE_mac_inst[module_idP].cba_last_access[0]= round(uniform_rngen(1,30));
+	       LOG_D(MAC,"[UE %d] Frame %d Subframe %d: start a new CBA backoff  %d UL active state %d \n", module_idP, frameP, subframe,
+	       UE_mac_inst[module_idP].cba_last_access[0], UE_mac_inst[module_idP].ul_active);
+	     */
+	} else {
+	    UE_mac_inst[module_idP].cba_last_access[0] -= 1;
+	    LOG_D(MAC,
+		  "[UE %d] Frame %d Subframe %d: wait for backoff to expire (%d) CBA UL active state %d \n",
+		  module_idP, frameP, subframe,
+		  UE_mac_inst[module_idP].cba_last_access[0],
+		  UE_mac_inst[module_idP].ul_active);
+	}
+    }
+
+    return rv;
+    /*
+       if (( ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1]>0)&&(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1]<64))   ||
+       ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2]>0)&&(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2]<64))   ||
+       ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3]>0)&&(UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3]<64)) )
+       //  && (UE_mac_inst[module_idP].ul_active == 0) // check if the ul is acrtive
+       && (UE_mac_inst[module_idP].cba_last_access[0] <= 0) ) {
+
+       UE_mac_inst[module_idP].cba_last_access[0]= round(uniform_rngen(1,cba_backoff));
+
+       LOG_I(MAC,"[UE %d] Frame %d Subframe %d: start a new CBA backoff  %d/%d UL active state %d \n", module_idP, frameP, subframe,
+       UE_mac_inst[module_idP].cba_last_access[0], cba_backoff,UE_mac_inst[module_idP].ul_active);
+
+       rv = 1;
+
+       rlc_status = mac_rlc_status_ind(module_idP, UE_mac_inst[module_idP].crnti,frameP,ENB_FLAG_NO,MBMS_FLAG_NO, // eNB_index
+       DTCH,
+       0);
+
+       if ((
+       // (rlc_status.pdus_in_buffer > 0 )           &&
+       // (UE_mac_inst[module_idP].ul_active == 0)  && // check if the ul is acrtive
+       (rlc_status.head_sdu_is_segmented  == 0 )          &&
+       ((rlc_status.head_sdu_remaining_size_to_send + header_offset ) <= buflen )
+       )){
+       rv = 1;
+
+       UE_mac_inst[module_idP].cba_last_access[0]= round(uniform_rngen(1,30));
+       LOG_D(MAC,"[UE %d] Frame %d Subframe %d: start a new CBA backoff  %d UL active state %d \n", module_idP, frameP, subframe,
+       UE_mac_inst[module_idP].cba_last_access[0], UE_mac_inst[module_idP].ul_active);
+       } else
+       UE_mac_inst[module_idP].cba_last_access[0]= round(uniform_rngen(1,5));
+
+
+       } else if (( ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID1]> 0 ))   ||
+       ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID2]> 0 ))   ||
+       ((UE_mac_inst[module_idP].scheduling_info.BSR[LCGID3]> 0 )) )
+       // && (UE_mac_inst[module_idP].ul_active == 0) // check if the ul is acrtive
+       && (UE_mac_inst[module_idP].cba_last_access[0]> 0) )
+       {
+
+       UE_mac_inst[module_idP].cba_last_access[0]-=1;
+       LOG_D(MAC,"[UE %d] Frame %d Subframe %d: wait for backoff to expire (%d) CBA UL active state %d \n",
+       module_idP, frameP, subframe,
+       UE_mac_inst[module_idP].cba_last_access[0], UE_mac_inst[module_idP].ul_active);
+       }
+       }
+     */
 
 }
 #endif
 
 
-boolean_t  update_bsr(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP,eNB_index_t eNB_index)
+boolean_t
+update_bsr(module_id_t module_idP, frame_t frameP,
+	   sub_frame_t subframeP, eNB_index_t eNB_index)
 {
 
-  mac_rlc_status_resp_t rlc_status;
-  boolean_t bsr_regular_triggered = FALSE;
-  uint8_t lcid;
-  uint8_t lcgid;
-  uint8_t num_lcid_with_data = 0; // for LCID with data only if LCGID is defined
-  uint16_t lcgid_buffer_remain[MAX_NUM_LCGID] = {0,0,0,0};
-  int32_t lcid_bytes_in_buffer[MAX_NUM_LCID];
-  /* Array for ordering LCID with data per decreasing priority order */
-  uint8_t lcid_reordered_array[MAX_NUM_LCID]=
-  {MAX_NUM_LCID,MAX_NUM_LCID,MAX_NUM_LCID,MAX_NUM_LCID,MAX_NUM_LCID,MAX_NUM_LCID,MAX_NUM_LCID,MAX_NUM_LCID,MAX_NUM_LCID,MAX_NUM_LCID,MAX_NUM_LCID};
-  uint8_t pos_next = 0;
-  uint8_t highest_priority = 16;
-  uint8_t array_index = 0;
-
-  // Reset All BSR Infos
-  lcid_bytes_in_buffer[0] = 0;
-  for (lcid=DCCH; lcid < MAX_NUM_LCID; lcid++)
-  {
-	  // Reset transmission status
-	  lcid_bytes_in_buffer[lcid] = 0;
-	  UE_mac_inst[module_idP].scheduling_info.LCID_status[lcid]=LCID_EMPTY;
-  }
-
-  for (lcgid=0; lcgid < MAX_NUM_LCGID; lcgid++)
-  {
-	  // Reset Buffer Info
-	  UE_mac_inst[module_idP].scheduling_info.BSR[lcgid]=0;
-	  UE_mac_inst[module_idP].scheduling_info.BSR_bytes[lcgid]=0;
-  }
-
-  //Get Buffer Occupancy and fill lcid_reordered_array
-  for (lcid=DCCH; lcid < MAX_NUM_LCID; lcid++)
-  {
-	  if (UE_mac_inst[module_idP].logicalChannelConfig[lcid])
-	  {
-		  	lcgid = UE_mac_inst[module_idP].scheduling_info.LCGID[lcid];
-
-		  	// Store already available data to transmit per Group
-		  	if (lcgid < MAX_NUM_LCGID)
-		  	{
-			  	lcgid_buffer_remain[lcgid] += UE_mac_inst[module_idP].scheduling_info.LCID_buffer_remain[lcid];
-		  	}
-
-		    rlc_status = mac_rlc_status_ind(module_idP, UE_mac_inst[module_idP].crnti,eNB_index,frameP,subframeP,ENB_FLAG_NO,MBMS_FLAG_NO,
-		                                    lcid,
-		                                    0xFFFF); //TBS is not used in RLC at this step, set a special value for debug
-
-		    lcid_bytes_in_buffer[lcid] = rlc_status.bytes_in_buffer;
-
-		    if (rlc_status.bytes_in_buffer > 0)
-		    {
-		          LOG_D(MAC,"[UE %d] PDCCH Tick : LCID%d LCGID%d has data to transmit =%d bytes at frame %d subframe %d\n",
-		                              module_idP, lcid,lcgid,rlc_status.bytes_in_buffer,frameP,subframeP);
-
-		         UE_mac_inst[module_idP].scheduling_info.LCID_status[lcid] = LCID_NOT_EMPTY;
-		         //Update BSR_bytes and position in lcid_reordered_array only if Group is defined
-		         if (lcgid < MAX_NUM_LCGID)
-		         {
-		        	 num_lcid_with_data ++;
-			         // sum lcid buffer which has same lcgid
-			         UE_mac_inst[module_idP].scheduling_info.BSR_bytes[lcgid] += rlc_status.bytes_in_buffer;
-
-			         //Fill in the array
-			         array_index = 0;
-			         do
-					{
-			        	 if (UE_mac_inst[module_idP].logicalChannelConfig[lcid]->ul_SpecificParameters->priority <= highest_priority)
-			        	 {
-			        		 //Insert if priority is higher or equal (lower or equal in value)
-			        		 for (pos_next=num_lcid_with_data-1; pos_next > array_index; pos_next--)
-			        		 {
-			        			 lcid_reordered_array[pos_next] = lcid_reordered_array[pos_next - 1];
-
-			        		 }
-			        		 lcid_reordered_array[array_index] = lcid;
-			        		 break;
-
-			        	 }
-			        	 array_index ++;
-					}
-			         while ((array_index < num_lcid_with_data) && (array_index < MAX_NUM_LCID));
-		         }
+    mac_rlc_status_resp_t rlc_status;
+    boolean_t bsr_regular_triggered = FALSE;
+    uint8_t lcid;
+    uint8_t lcgid;
+    uint8_t num_lcid_with_data = 0;	// for LCID with data only if LCGID is defined
+    uint16_t lcgid_buffer_remain[MAX_NUM_LCGID] = { 0, 0, 0, 0 };
+    int32_t lcid_bytes_in_buffer[MAX_NUM_LCID];
+    /* Array for ordering LCID with data per decreasing priority order */
+    uint8_t lcid_reordered_array[MAX_NUM_LCID] =
+	{ MAX_NUM_LCID, MAX_NUM_LCID, MAX_NUM_LCID, MAX_NUM_LCID,
+	MAX_NUM_LCID, MAX_NUM_LCID, MAX_NUM_LCID, MAX_NUM_LCID,
+	    MAX_NUM_LCID,
+	MAX_NUM_LCID, MAX_NUM_LCID
+    };
+    uint8_t pos_next = 0;
+    uint8_t highest_priority = 16;
+    uint8_t array_index = 0;
+
+    // Reset All BSR Infos
+    lcid_bytes_in_buffer[0] = 0;
+    for (lcid = DCCH; lcid < MAX_NUM_LCID; lcid++) {
+	// Reset transmission status
+	lcid_bytes_in_buffer[lcid] = 0;
+	UE_mac_inst[module_idP].scheduling_info.LCID_status[lcid] =
+	    LCID_EMPTY;
+    }
+
+    for (lcgid = 0; lcgid < MAX_NUM_LCGID; lcgid++) {
+	// Reset Buffer Info
+	UE_mac_inst[module_idP].scheduling_info.BSR[lcgid] = 0;
+	UE_mac_inst[module_idP].scheduling_info.BSR_bytes[lcgid] = 0;
+    }
+
+    //Get Buffer Occupancy and fill lcid_reordered_array
+    for (lcid = DCCH; lcid < MAX_NUM_LCID; lcid++) {
+	if (UE_mac_inst[module_idP].logicalChannelConfig[lcid]) {
+	    lcgid = UE_mac_inst[module_idP].scheduling_info.LCGID[lcid];
+
+	    // Store already available data to transmit per Group
+	    if (lcgid < MAX_NUM_LCGID) {
+		lcgid_buffer_remain[lcgid] +=
+		    UE_mac_inst[module_idP].
+		    scheduling_info.LCID_buffer_remain[lcid];
+	    }
+
+	    rlc_status = mac_rlc_status_ind(module_idP, UE_mac_inst[module_idP].crnti, eNB_index, frameP, subframeP, ENB_FLAG_NO, MBMS_FLAG_NO, lcid, 0xFFFF);	//TBS is not used in RLC at this step, set a special value for debug
+
+	    lcid_bytes_in_buffer[lcid] = rlc_status.bytes_in_buffer;
+
+	    if (rlc_status.bytes_in_buffer > 0) {
+		LOG_D(MAC,
+		      "[UE %d] PDCCH Tick : LCID%d LCGID%d has data to transmit =%d bytes at frame %d subframe %d\n",
+		      module_idP, lcid, lcgid, rlc_status.bytes_in_buffer,
+		      frameP, subframeP);
+
+		UE_mac_inst[module_idP].scheduling_info.LCID_status[lcid] =
+		    LCID_NOT_EMPTY;
+		//Update BSR_bytes and position in lcid_reordered_array only if Group is defined
+		if (lcgid < MAX_NUM_LCGID) {
+		    num_lcid_with_data++;
+		    // sum lcid buffer which has same lcgid
+		    UE_mac_inst[module_idP].scheduling_info.
+			BSR_bytes[lcgid] += rlc_status.bytes_in_buffer;
+
+		    //Fill in the array
+		    array_index = 0;
+		    do {
+			if (UE_mac_inst[module_idP].logicalChannelConfig
+			    [lcid]->ul_SpecificParameters->priority <=
+			    highest_priority) {
+			    //Insert if priority is higher or equal (lower or equal in value)
+			    for (pos_next = num_lcid_with_data - 1;
+				 pos_next > array_index; pos_next--) {
+				lcid_reordered_array[pos_next] =
+				    lcid_reordered_array[pos_next - 1];
+
+			    }
+			    lcid_reordered_array[array_index] = lcid;
+			    break;
+
+			}
+			array_index++;
 		    }
-	  }
-
-  }
-
-  // Check whether a regular BSR can be triggered according to the first cases in 36.321
-  if (num_lcid_with_data)
-  {
-          LOG_D(MAC,"[UE %d] PDCCH Tick at frame %d subframe %d: NumLCID with data=%d Reordered LCID0=%d LCID1=%d LCID2=%d\n",
-                            module_idP, frameP, subframeP,num_lcid_with_data,lcid_reordered_array[0],lcid_reordered_array[1],lcid_reordered_array[2]);
-
-	  for (array_index=0; array_index < num_lcid_with_data; array_index++)
-	  {
-		  lcid = lcid_reordered_array[array_index];
-	      /* UL data, for a logical channel which belongs to a LCG, becomes available for transmission in the RLC entity
-	         either the data belongs to a logical channel with higher priority than the priorities of the logical channels
-	         which belong to any LCG and for which data is already available for transmission
-	      */
-		  if ((UE_mac_inst[module_idP].scheduling_info.LCID_buffer_remain[lcid] == 0)
-			/* or there is no data available for any of the logical channels which belong to a LCG */
-			||(lcgid_buffer_remain[UE_mac_inst[module_idP].scheduling_info.LCGID[lcid]] == 0)
-			)
-		  {
-			  bsr_regular_triggered = TRUE;
-
-		      LOG_D(MAC,"[UE %d] PDCCH Tick : MAC BSR Triggered LCID%d LCGID%d data become available at frame %d subframe %d\n",
-		                      module_idP, lcid,UE_mac_inst[module_idP].scheduling_info.LCGID[lcid],frameP, subframeP);
-
-			  break;
-		  }
-	  }
-
-	  // Trigger Regular BSR if ReTxBSR Timer has expired and UE has data for transmission
-	  if (UE_mac_inst[module_idP].scheduling_info.retxBSR_SF == 0)
-	  {
-		  bsr_regular_triggered = TRUE;
-
-		  if ((UE_mac_inst[module_idP].BSR_reporting_active & BSR_TRIGGER_REGULAR) == 0) {
-		        LOG_I(MAC,"[UE %d] PDCCH Tick : MAC BSR Triggered ReTxBSR Timer expiry at frame %d subframe %d\n",
-		                          module_idP, frameP, subframeP);
-		  }
-
-	  }
-  }
-
-  //Store Buffer Occupancy in remain buffers for next TTI
-  for (lcid=DCCH; lcid < MAX_NUM_LCID; lcid++)
-  {
-	  UE_mac_inst[module_idP].scheduling_info.LCID_buffer_remain[lcid] = lcid_bytes_in_buffer[lcid];
-  }
-
-  return bsr_regular_triggered;
+		    while ((array_index < num_lcid_with_data)
+			   && (array_index < MAX_NUM_LCID));
+		}
+	    }
+	}
+
+    }
+
+    // Check whether a regular BSR can be triggered according to the first cases in 36.321
+    if (num_lcid_with_data) {
+	LOG_D(MAC,
+	      "[UE %d] PDCCH Tick at frame %d subframe %d: NumLCID with data=%d Reordered LCID0=%d LCID1=%d LCID2=%d\n",
+	      module_idP, frameP, subframeP, num_lcid_with_data,
+	      lcid_reordered_array[0], lcid_reordered_array[1],
+	      lcid_reordered_array[2]);
+
+	for (array_index = 0; array_index < num_lcid_with_data;
+	     array_index++) {
+	    lcid = lcid_reordered_array[array_index];
+	    /* UL data, for a logical channel which belongs to a LCG, becomes available for transmission in the RLC entity
+	       either the data belongs to a logical channel with higher priority than the priorities of the logical channels
+	       which belong to any LCG and for which data is already available for transmission
+	     */
+	    if ((UE_mac_inst[module_idP].
+		 scheduling_info.LCID_buffer_remain[lcid] == 0)
+		/* or there is no data available for any of the logical channels which belong to a LCG */
+		||
+		(lcgid_buffer_remain
+		 [UE_mac_inst[module_idP].scheduling_info.LCGID[lcid]] ==
+		 0)) {
+		bsr_regular_triggered = TRUE;
+
+		LOG_D(MAC,
+		      "[UE %d] PDCCH Tick : MAC BSR Triggered LCID%d LCGID%d data become available at frame %d subframe %d\n",
+		      module_idP, lcid,
+		      UE_mac_inst[module_idP].scheduling_info.LCGID[lcid],
+		      frameP, subframeP);
+
+		break;
+	    }
+	}
+
+	// Trigger Regular BSR if ReTxBSR Timer has expired and UE has data for transmission
+	if (UE_mac_inst[module_idP].scheduling_info.retxBSR_SF == 0) {
+	    bsr_regular_triggered = TRUE;
+
+	    if ((UE_mac_inst[module_idP].BSR_reporting_active &
+		 BSR_TRIGGER_REGULAR) == 0) {
+		LOG_I(MAC,
+		      "[UE %d] PDCCH Tick : MAC BSR Triggered ReTxBSR Timer expiry at frame %d subframe %d\n",
+		      module_idP, frameP, subframeP);
+	    }
+
+	}
+    }
+    //Store Buffer Occupancy in remain buffers for next TTI
+    for (lcid = DCCH; lcid < MAX_NUM_LCID; lcid++) {
+	UE_mac_inst[module_idP].scheduling_info.LCID_buffer_remain[lcid] =
+	    lcid_bytes_in_buffer[lcid];
+    }
+
+    return bsr_regular_triggered;
 }
 
-uint8_t locate_BsrIndexByBufferSize (const uint32_t *table, int size, int value)
+uint8_t
+locate_BsrIndexByBufferSize(const uint32_t * table, int size, int value)
 {
 
-  uint8_t ju, jm, jl;
-  int ascend;
+    uint8_t ju, jm, jl;
+    int ascend;
 
-  DevAssert( size > 0 );
-  DevAssert( size <= 256 );
+    DevAssert(size > 0);
+    DevAssert(size <= 256);
 
-  if (value == 0) {
-    return 0;  //elseif (value > 150000) return 63;
-  }
+    if (value == 0) {
+	return 0;		//elseif (value > 150000) return 63;
+    }
 
-  jl = 0;        // lower bound
-  ju = size - 1; // upper bound
-  ascend = (table[ju] >= table[jl]) ? 1 : 0; // determine the order of the the table:  1 if ascending order of table, 0 otherwise
+    jl = 0;			// lower bound
+    ju = size - 1;		// upper bound
+    ascend = (table[ju] >= table[jl]) ? 1 : 0;	// determine the order of the the table:  1 if ascending order of table, 0 otherwise
 
-  while (ju-jl > 1) { //If we are not yet done,
-    jm = (ju+jl) >> 1; //compute a midpoint,
+    while (ju - jl > 1) {	//If we are not yet done,
+	jm = (ju + jl) >> 1;	//compute a midpoint,
 
-    if ((value >= table[jm]) == ascend) {
-      jl=jm; // replace the lower limit
-    } else {
-      ju=jm; //replace the upper limit
-    }
+	if ((value >= table[jm]) == ascend) {
+	    jl = jm;		// replace the lower limit
+	} else {
+	    ju = jm;		//replace the upper limit
+	}
 
-    LOG_T(MAC,"[UE] searching BSR index %d for (BSR TABLE %d < value %d)\n", jm, table[jm], value);
-  }
+	LOG_T(MAC,
+	      "[UE] searching BSR index %d for (BSR TABLE %d < value %d)\n",
+	      jm, table[jm], value);
+    }
 
-  if (value == table[jl]) {
-    return jl;
-  } else                    {
-    return jl+1;  //equally  ju
-  }
+    if (value == table[jl]) {
+	return jl;
+    } else {
+	return jl + 1;		//equally  ju
+    }
 
 }
 
 int get_sf_periodicBSRTimer(uint8_t sf_offset)
 {
 
-  switch (sf_offset) {
-  case PeriodicBSR_Timer_r12_sf5:
-    return 5;
-    break;
+    switch (sf_offset) {
+    case PeriodicBSR_Timer_r12_sf5:
+	return 5;
+	break;
 
-  case PeriodicBSR_Timer_r12_sf10:
-    return 10;
-    break;
+    case PeriodicBSR_Timer_r12_sf10:
+	return 10;
+	break;
 
-  case PeriodicBSR_Timer_r12_sf16:
-    return 16;
-    break;
+    case PeriodicBSR_Timer_r12_sf16:
+	return 16;
+	break;
 
-  case PeriodicBSR_Timer_r12_sf20:
-    return 20;
-    break;
+    case PeriodicBSR_Timer_r12_sf20:
+	return 20;
+	break;
 
-  case PeriodicBSR_Timer_r12_sf32:
-    return 32;
-    break;
+    case PeriodicBSR_Timer_r12_sf32:
+	return 32;
+	break;
 
-  case PeriodicBSR_Timer_r12_sf40:
-    return 40;
-    break;
+    case PeriodicBSR_Timer_r12_sf40:
+	return 40;
+	break;
 
-  case PeriodicBSR_Timer_r12_sf64:
-    return 64;
-    break;
+    case PeriodicBSR_Timer_r12_sf64:
+	return 64;
+	break;
 
-  case PeriodicBSR_Timer_r12_sf80:
-    return 80;
-    break;
+    case PeriodicBSR_Timer_r12_sf80:
+	return 80;
+	break;
 
-  case PeriodicBSR_Timer_r12_sf128:
-    return 128;
-    break;
+    case PeriodicBSR_Timer_r12_sf128:
+	return 128;
+	break;
 
-  case PeriodicBSR_Timer_r12_sf160:
-    return 160;
-    break;
+    case PeriodicBSR_Timer_r12_sf160:
+	return 160;
+	break;
 
-  case PeriodicBSR_Timer_r12_sf320:
-    return 320;
-    break;
+    case PeriodicBSR_Timer_r12_sf320:
+	return 320;
+	break;
 
-  case PeriodicBSR_Timer_r12_sf640:
-    return 640;
-    break;
+    case PeriodicBSR_Timer_r12_sf640:
+	return 640;
+	break;
 
-  case PeriodicBSR_Timer_r12_sf1280:
-    return 1280;
-    break;
+    case PeriodicBSR_Timer_r12_sf1280:
+	return 1280;
+	break;
 
-  case PeriodicBSR_Timer_r12_sf2560:
-    return 2560;
-    break;
+    case PeriodicBSR_Timer_r12_sf2560:
+	return 2560;
+	break;
 
-  case PeriodicBSR_Timer_r12_infinity:
-  default:
-    return 0xFFFF;
-    break;
-  }
+    case PeriodicBSR_Timer_r12_infinity:
+    default:
+	return 0xFFFF;
+	break;
+    }
 }
 
 int get_sf_retxBSRTimer(uint8_t sf_offset)
 {
 
-  switch (sf_offset) {
-  case RetxBSR_Timer_r12_sf320:
-    return 320;
-    break;
+    switch (sf_offset) {
+    case RetxBSR_Timer_r12_sf320:
+	return 320;
+	break;
 
-  case RetxBSR_Timer_r12_sf640:
-    return 640;
-    break;
+    case RetxBSR_Timer_r12_sf640:
+	return 640;
+	break;
 
-  case RetxBSR_Timer_r12_sf1280:
-    return 1280;
-    break;
+    case RetxBSR_Timer_r12_sf1280:
+	return 1280;
+	break;
 
-  case RetxBSR_Timer_r12_sf2560:
-    return 2560;
-    break;
+    case RetxBSR_Timer_r12_sf2560:
+	return 2560;
+	break;
 
-  case RetxBSR_Timer_r12_sf5120:
-    return 5120;
-    break;
+    case RetxBSR_Timer_r12_sf5120:
+	return 5120;
+	break;
 
-  case RetxBSR_Timer_r12_sf10240:
-    return 10240;
-    break;
+    case RetxBSR_Timer_r12_sf10240:
+	return 10240;
+	break;
 
-  default:
-    return -1;
-    break;
-  }
+    default:
+	return -1;
+	break;
+    }
 }
+
 int get_ms_bucketsizeduration(uint8_t bucketsizeduration)
 {
 
-  switch (bucketsizeduration) {
-  case LogicalChannelConfig__ul_SpecificParameters__bucketSizeDuration_ms50:
-    return 50;
-    break;
-
-  case LogicalChannelConfig__ul_SpecificParameters__bucketSizeDuration_ms100:
-    return 100;
-    break;
-
-  case LogicalChannelConfig__ul_SpecificParameters__bucketSizeDuration_ms150:
-    return 150;
-    break;
-
-  case LogicalChannelConfig__ul_SpecificParameters__bucketSizeDuration_ms300:
-    return 300;
-    break;
-
-  case LogicalChannelConfig__ul_SpecificParameters__bucketSizeDuration_ms500:
-    return 500;
-    break;
-
-  case LogicalChannelConfig__ul_SpecificParameters__bucketSizeDuration_ms1000:
-    return 1000;
-    break;
-
-  default:
-    return 0;
-    break;
-  }
+    switch (bucketsizeduration) {
+    case LogicalChannelConfig__ul_SpecificParameters__bucketSizeDuration_ms50:
+	return
+	    50;
+	break;
+
+    case LogicalChannelConfig__ul_SpecificParameters__bucketSizeDuration_ms100:
+	return
+	    100;
+	break;
+
+    case LogicalChannelConfig__ul_SpecificParameters__bucketSizeDuration_ms150:
+	return
+	    150;
+	break;
+
+    case LogicalChannelConfig__ul_SpecificParameters__bucketSizeDuration_ms300:
+	return
+	    300;
+	break;
+
+    case LogicalChannelConfig__ul_SpecificParameters__bucketSizeDuration_ms500:
+	return
+	    500;
+	break;
+
+    case LogicalChannelConfig__ul_SpecificParameters__bucketSizeDuration_ms1000:
+	return
+	    1000;
+	break;
+
+    default:
+	return 0;
+	break;
+    }
 }
 
-void update_phr(module_id_t module_idP,int CC_id)
+void update_phr(module_id_t module_idP, int CC_id)
 {
 
-  AssertFatal(CC_id==0,
-	      "Transmission on secondary CCs is not supported yet\n");
-
-  UE_mac_inst[module_idP].PHR_reporting_active =0;
-  UE_mac_inst[module_idP].scheduling_info.periodicPHR_SF =  get_sf_perioidicPHR_Timer(UE_mac_inst[module_idP].scheduling_info.periodicPHR_Timer);
-  UE_mac_inst[module_idP].scheduling_info.prohibitPHR_SF =  get_sf_prohibitPHR_Timer(UE_mac_inst[module_idP].scheduling_info.prohibitPHR_Timer);
-  // LOG_D(MAC,"phr %d %d\n ",UE_mac_inst[module_idP].scheduling_info.periodicPHR_SF, UE_mac_inst[module_idP].scheduling_info.prohibitPHR_SF);
+    AssertFatal(CC_id == 0,
+		"Transmission on secondary CCs is not supported yet\n");
+
+    UE_mac_inst[module_idP].PHR_reporting_active = 0;
+    UE_mac_inst[module_idP].scheduling_info.periodicPHR_SF =
+	get_sf_perioidicPHR_Timer(UE_mac_inst[module_idP].
+				  scheduling_info.periodicPHR_Timer);
+    UE_mac_inst[module_idP].scheduling_info.prohibitPHR_SF =
+	get_sf_prohibitPHR_Timer(UE_mac_inst[module_idP].
+				 scheduling_info.prohibitPHR_Timer);
+    // LOG_D(MAC,"phr %d %d\n ",UE_mac_inst[module_idP].scheduling_info.periodicPHR_SF, UE_mac_inst[module_idP].scheduling_info.prohibitPHR_SF);
 }
-uint8_t get_phr_mapping (module_id_t module_idP, int CC_id, uint8_t eNB_index)
+
+uint8_t
+get_phr_mapping(module_id_t module_idP, int CC_id, uint8_t eNB_index)
 {
 
-  AssertFatal(CC_id==0,
-	      "Transmission on secondary CCs is not supported yet\n");
+    AssertFatal(CC_id == 0,
+		"Transmission on secondary CCs is not supported yet\n");
 
-  //power headroom reporting range is from -23 ...+40 dB, as described in 36313
-  //note: mac_xface->get_Po_NOMINAL_PUSCH(module_idP) is float
-  if (get_PHR(module_idP,CC_id,eNB_index) < -23) {
-    return 0;
-  } else if (get_PHR(module_idP,CC_id,eNB_index) >= 40) {
-    return 63;
-  } else { // -23 to 40
-    return  (uint8_t) get_PHR(module_idP,CC_id,eNB_index) + PHR_MAPPING_OFFSET;
+    //power headroom reporting range is from -23 ...+40 dB, as described in 36313
+    //note: mac_xface->get_Po_NOMINAL_PUSCH(module_idP) is float
+    if (get_PHR(module_idP, CC_id, eNB_index) < -23) {
+	return 0;
+    } else if (get_PHR(module_idP, CC_id, eNB_index) >= 40) {
+	return 63;
+    } else {			// -23 to 40
+	return (uint8_t) get_PHR(module_idP, CC_id,
+				 eNB_index) + PHR_MAPPING_OFFSET;
 
-  }
+    }
 }
+
 int get_sf_perioidicPHR_Timer(uint8_t perioidicPHR_Timer)
 {
-  return (perioidicPHR_Timer+1)*10;
+    return (perioidicPHR_Timer + 1) * 10;
 }
 
 
 int get_sf_prohibitPHR_Timer(uint8_t prohibitPHR_Timer)
 {
-  return (prohibitPHR_Timer)*10;
+    return (prohibitPHR_Timer) * 10;
 }
 
 int get_db_dl_PathlossChange(uint8_t dl_PathlossChange)
 {
-  switch (dl_PathlossChange) {
-  case MAC_MainConfig__phr_Config__setup__dl_PathlossChange_dB1:
-    return 1;
-    break;
-
-  case MAC_MainConfig__phr_Config__setup__dl_PathlossChange_dB3:
-    return 3;
-    break;
-
-  case MAC_MainConfig__phr_Config__setup__dl_PathlossChange_dB6:
-    return 6;
-    break;
-
-  case MAC_MainConfig__phr_Config__setup__dl_PathlossChange_infinity:
-  default:
-    return -1;
-    break;
-  }
+    switch (dl_PathlossChange) {
+    case MAC_MainConfig__phr_Config__setup__dl_PathlossChange_dB1:
+	return 1;
+	break;
+
+    case MAC_MainConfig__phr_Config__setup__dl_PathlossChange_dB3:
+	return 3;
+	break;
+
+    case MAC_MainConfig__phr_Config__setup__dl_PathlossChange_dB6:
+	return 6;
+	break;
+
+    case MAC_MainConfig__phr_Config__setup__dl_PathlossChange_infinity:
+    default:
+	return -1;
+	break;
+    }
 }
diff --git a/openair2/LAYER2/MAC/vars.h b/openair2/LAYER2/MAC/vars.h
index 4ff54752530fe9e892d8bf93f355effd03d12c89..238608816121c5e438f0089a8bd8d96df9191113 100644
--- a/openair2/LAYER2/MAC/vars.h
+++ b/openair2/LAYER2/MAC/vars.h
@@ -34,25 +34,32 @@
 #define __MAC_VARS_H__
 #ifdef USER_MODE
 //#include "stdio.h"
-#endif //USER_MODE
+#endif				//USER_MODE
 #include "PHY/defs.h"
 #include "defs.h"
 #include "COMMON/mac_rrc_primitives.h"
 
-const uint32_t BSR_TABLE[BSR_TABLE_SIZE]= {0,10,12,14,17,19,22,26,31,36,42,49,57,67,78,91,
-                                           105,125,146,171,200,234,274,321,376,440,515,603,706,826,967,1132,
-                                           1326,1552,1817,2127,2490,2915,3413,3995,4677,5467,6411,7505,8787,10287,12043,14099,
-                                           16507,19325,22624,26487,31009,36304,42502,49759,58255,68201,79846,93479,109439, 128125,150000, 300000
-                                          };
-// extended bsr table--currently not used										  
-const uint32_t Extended_BSR_TABLE[BSR_TABLE_SIZE] = {0,10,13,16,19,23,29,35,43,53,65,80,98,120,147,
-                                                     181,223,274,337,414,509,625,769,945,1162,1429,
-                                                     1757,2161,2657,3267,4017,4940,6074,7469,9185,
-                                                     11294,13888,17077,20999,25822,31752,39045,48012,
-                                                     59039,72598,89272,109774,134986,165989,204111,
-                                                     250990,308634,379519,466683,573866,705666,867737,
-                                                     1067031,1312097,1613447,1984009,2439678,3000000,
-                                                     6000000};
+const uint32_t BSR_TABLE[BSR_TABLE_SIZE] =
+    { 0, 10, 12, 14, 17, 19, 22, 26, 31, 36, 42, 49, 57, 67, 78, 91,
+    105, 125, 146, 171, 200, 234, 274, 321, 376, 440, 515, 603, 706, 826,
+	967, 1132,
+    1326, 1552, 1817, 2127, 2490, 2915, 3413, 3995, 4677, 5467, 6411, 7505,
+	8787, 10287, 12043, 14099,
+    16507, 19325, 22624, 26487, 31009, 36304, 42502, 49759, 58255, 68201,
+	79846, 93479, 109439, 128125, 150000, 300000
+};
+
+// extended bsr table--currently not used                                                                                 
+const uint32_t Extended_BSR_TABLE[BSR_TABLE_SIZE] =
+    { 0, 10, 13, 16, 19, 23, 29, 35, 43, 53, 65, 80, 98, 120, 147,
+    181, 223, 274, 337, 414, 509, 625, 769, 945, 1162, 1429,
+    1757, 2161, 2657, 3267, 4017, 4940, 6074, 7469, 9185,
+    11294, 13888, 17077, 20999, 25822, 31752, 39045, 48012,
+    59039, 72598, 89272, 109774, 134986, 165989, 204111,
+    250990, 308634, 379519, 466683, 573866, 705666, 867737,
+    1067031, 1312097, 1613447, 1984009, 2439678, 3000000,
+    6000000
+};
 
 //#define MAX_SIZE_OF_AGG3   576 
 //#define MAX_SIZE_OF_AGG2   288
@@ -64,24 +71,26 @@ const uint32_t Extended_BSR_TABLE[BSR_TABLE_SIZE] = {0,10,13,16,19,23,29,35,43,5
  * this is also dependent to transmission mode, where an offset could be defined
  */
 // the follwoing three tables are calibrated for TXMODE 1 and 2
-const uint8_t cqi2fmt0_agg[MAX_SUPPORTED_BW][CQI_VALUE_RANGE]= { 
-  {3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, // 1.4_DCI0_CRC_Size= 37 bits
-  //{3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0}, // 5_DCI0_CRC_SIZE = 41
-  {3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, // 5_DCI0_CRC_SIZE = 41
-  {3, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0}, // 10_DCI0_CRC_SIZE = 43
-  {3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0}   // 20_DCI0_CRC_SIZE = 44
+const uint8_t cqi2fmt0_agg[MAX_SUPPORTED_BW][CQI_VALUE_RANGE] = {
+    {3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},	// 1.4_DCI0_CRC_Size= 37 bits
+    //{3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0}, // 5_DCI0_CRC_SIZE = 41
+    {3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0},	// 5_DCI0_CRC_SIZE = 41
+    {3, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0},	// 10_DCI0_CRC_SIZE = 43
+    {3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0}	// 20_DCI0_CRC_SIZE = 44
 };
-const uint8_t cqi2fmt1x_agg[MAX_SUPPORTED_BW][CQI_VALUE_RANGE]= { 
-  {3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, // 1.4_DCI0_CRC_Size < 38 bits
-  {3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, // 5_DCI0_CRC_SIZE  < 43
-  {3, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0}, // 10_DCI0_CRC_SIZE  < 47
-  {3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0}   // 20_DCI0_CRC_SIZE  < 55
+
+const uint8_t cqi2fmt1x_agg[MAX_SUPPORTED_BW][CQI_VALUE_RANGE] = {
+    {3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},	// 1.4_DCI0_CRC_Size < 38 bits
+    {3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0},	// 5_DCI0_CRC_SIZE  < 43
+    {3, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0},	// 10_DCI0_CRC_SIZE  < 47
+    {3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0}	// 20_DCI0_CRC_SIZE  < 55
 };
-const uint8_t cqi2fmt2x_agg[MAX_SUPPORTED_BW][CQI_VALUE_RANGE]= { 
-  {3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, // 1.4_DCI0_CRC_Size= 47 bits
-  {3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, // 5_DCI0_CRC_SIZE = 55
-  {3, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0}, // 10_DCI0_CRC_SIZE = 59
-  {3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0}   // 20_DCI0_CRC_SIZE = 64
+
+const uint8_t cqi2fmt2x_agg[MAX_SUPPORTED_BW][CQI_VALUE_RANGE] = {
+    {3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},	// 1.4_DCI0_CRC_Size= 47 bits
+    {3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0},	// 5_DCI0_CRC_SIZE = 55
+    {3, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0},	// 10_DCI0_CRC_SIZE = 59
+    {3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0}	// 20_DCI0_CRC_SIZE = 64
 };
 
 //uint32_t EBSR_Level[63]={0,10,13,16,19,23,29,35,43,53,65,80,98,120,147,181};
@@ -89,7 +98,7 @@ const uint8_t cqi2fmt2x_agg[MAX_SUPPORTED_BW][CQI_VALUE_RANGE]= {
 
 uint32_t RRC_CONNECTION_FLAG;
 
-UE_MAC_INST *UE_mac_inst; //[NB_MODULE_MAX];
+UE_MAC_INST *UE_mac_inst;	//[NB_MODULE_MAX];
 MAC_RLC_XFACE *Mac_rlc_xface;
 
 /// Primary component carrier index of eNB
@@ -97,39 +106,39 @@ int pCC_id[NUMBER_OF_eNB_MAX];
 
 
 
-eNB_ULSCH_INFO eNB_ulsch_info[NUMBER_OF_eNB_MAX][MAX_NUM_CCs][NUMBER_OF_UE_MAX]; // eNBxUE = 8x8
-eNB_DLSCH_INFO eNB_dlsch_info[NUMBER_OF_eNB_MAX][MAX_NUM_CCs][NUMBER_OF_UE_MAX]; // eNBxUE = 8x8
+eNB_ULSCH_INFO eNB_ulsch_info[NUMBER_OF_eNB_MAX][MAX_NUM_CCs][NUMBER_OF_UE_MAX];	// eNBxUE = 8x8
+eNB_DLSCH_INFO eNB_dlsch_info[NUMBER_OF_eNB_MAX][MAX_NUM_CCs][NUMBER_OF_UE_MAX];	// eNBxUE = 8x8
 
 
 #ifdef OPENAIR2
-unsigned char NB_eNB_INST=0;
-unsigned char NB_UE_INST=0;
-unsigned char NB_RN_INST=0;
-unsigned char NB_INST=0;
+unsigned char NB_eNB_INST = 0;
+unsigned char NB_UE_INST = 0;
+unsigned char NB_RN_INST = 0;
+unsigned char NB_INST = 0;
 #endif
 
 
-DCI0_5MHz_TDD_1_6_t       UL_alloc_pdu;
+DCI0_5MHz_TDD_1_6_t UL_alloc_pdu;
 
-DCI1A_5MHz_TDD_1_6_t      DLSCH_alloc_pdu1A;
-DCI1A_5MHz_TDD_1_6_t      RA_alloc_pdu;
-DCI1A_5MHz_TDD_1_6_t      BCCH_alloc_pdu;
+DCI1A_5MHz_TDD_1_6_t DLSCH_alloc_pdu1A;
+DCI1A_5MHz_TDD_1_6_t RA_alloc_pdu;
+DCI1A_5MHz_TDD_1_6_t BCCH_alloc_pdu;
 
-DCI1A_5MHz_TDD_1_6_t      CCCH_alloc_pdu;
-DCI1_5MHz_TDD_t           DLSCH_alloc_pdu;
+DCI1A_5MHz_TDD_1_6_t CCCH_alloc_pdu;
+DCI1_5MHz_TDD_t DLSCH_alloc_pdu;
 
 #if defined(Rel10) || defined(Rel14)
-DCI1C_5MHz_t                 MCCH_alloc_pdu;
+DCI1C_5MHz_t MCCH_alloc_pdu;
 #endif
 
-DCI0_5MHz_FDD_t       UL_alloc_pdu_fdd;
+DCI0_5MHz_FDD_t UL_alloc_pdu_fdd;
 
-DCI1A_5MHz_FDD_t      DLSCH_alloc_pdu1A_fdd;
-DCI1A_5MHz_FDD_t      RA_alloc_pdu_fdd;
-DCI1A_5MHz_FDD_t      BCCH_alloc_pdu_fdd;
+DCI1A_5MHz_FDD_t DLSCH_alloc_pdu1A_fdd;
+DCI1A_5MHz_FDD_t RA_alloc_pdu_fdd;
+DCI1A_5MHz_FDD_t BCCH_alloc_pdu_fdd;
 
-DCI1A_5MHz_FDD_t      CCCH_alloc_pdu_fdd;
-DCI1_5MHz_FDD_t       DLSCH_alloc_pdu_fdd;
+DCI1A_5MHz_FDD_t CCCH_alloc_pdu_fdd;
+DCI1_5MHz_FDD_t DLSCH_alloc_pdu_fdd;
 
 DCI2_5MHz_2A_TDD_t DLSCH_alloc_pdu1;
 DCI2_5MHz_2A_TDD_t DLSCH_alloc_pdu2;
@@ -137,5 +146,3 @@ DCI2_5MHz_2A_TDD_t DLSCH_alloc_pdu2;
 DCI1E_5MHz_2A_M10PRB_TDD_t DLSCH_alloc_pdu1E;
 
 #endif
-
-