From 03231dd163e673651ada2555e564f7157faad616 Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Thu, 24 Nov 2016 16:44:15 +0100
Subject: [PATCH] hotfix: fix pucch_procedures

Something went wrong somewhere in the commit history, the function
was not correct.

The most obvious symptom was that way too much SR (scheduling requests)
were given to the UE whenever it had some PUCCH to send.

The case for TDD has not been tested.
---
 openair1/SCHED/phy_procedures_lte_eNb.c | 577 ++++++++++++------------
 1 file changed, 277 insertions(+), 300 deletions(-)

diff --git a/openair1/SCHED/phy_procedures_lte_eNb.c b/openair1/SCHED/phy_procedures_lte_eNb.c
index 11fb37dcdf..6d0bc53ac8 100644
--- a/openair1/SCHED/phy_procedures_lte_eNb.c
+++ b/openair1/SCHED/phy_procedures_lte_eNb.c
@@ -2016,11 +2016,11 @@ void prach_procedures(PHY_VARS_eNB *eNB) {
   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_PRACH_RX,0);
 }
 
-void pucch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,int UE_id,int harq_pid) {
-
+void pucch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,int UE_id,int harq_pid)
+{
   LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
   uint8_t SR_payload = 0,*pucch_payload=NULL,pucch_payload0[2]= {0,0},pucch_payload1[2]= {0,0};
-  int16_t n1_pucch0,n1_pucch1,n1_pucch2,n1_pucch3;
+  int16_t n1_pucch0 = -1, n1_pucch1 = -1, n1_pucch2 = -1, n1_pucch3 = -1;
   uint8_t do_SR = 0;
   uint8_t pucch_sel = 0;
   int32_t metric0=0,metric1=0,metric0_SR=0;
@@ -2031,352 +2031,329 @@ void pucch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,int UE_id,int harq
 
   if ((eNB->dlsch[UE_id][0]) &&
       (eNB->dlsch[UE_id][0]->rnti>0) &&
-      (eNB->ulsch[UE_id]->harq_processes[harq_pid]->subframe_scheduling_flag==0)) { 
+      (eNB->ulsch[UE_id]->harq_processes[harq_pid]->subframe_scheduling_flag==0)) {
 
     // check SR availability
     do_SR = is_SR_subframe(eNB,proc,UE_id);
     //      do_SR = 0;
-    
+
     // Now ACK/NAK
     // First check subframe_tx flag for earlier subframes
 
     get_n1_pucch_eNB(eNB,
-		     proc,
-		     UE_id,
-		     &n1_pucch0,
-		     &n1_pucch1,
-		     &n1_pucch2,
-		     &n1_pucch3);
-    
+                     proc,
+                     UE_id,
+                     &n1_pucch0,
+                     &n1_pucch1,
+                     &n1_pucch2,
+                     &n1_pucch3);
+
     LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d, subframe %d Checking for PUCCH (%d,%d,%d,%d) SR %d\n",
-	  eNB->Mod_id,eNB->dlsch[UE_id][0]->rnti,
-	  frame,subframe,
-	  n1_pucch0,n1_pucch1,n1_pucch2,n1_pucch3,do_SR);
-    
+          eNB->Mod_id,eNB->dlsch[UE_id][0]->rnti,
+          frame,subframe,
+          n1_pucch0,n1_pucch1,n1_pucch2,n1_pucch3,do_SR);
+
     if ((n1_pucch0==-1) && (n1_pucch1==-1) && (do_SR==0)) {  // no TX PDSCH that have to be checked and no SR for this UE_id
     } else {
       // otherwise we have some PUCCH detection to do
-      
+
       // Null out PUCCH PRBs for noise measurement
       switch(fp->N_RB_UL) {
       case 6:
-	eNB->rb_mask_ul[0] |= (0x1 | (1<<5)); //position 5
-	break;
+        eNB->rb_mask_ul[0] |= (0x1 | (1<<5)); //position 5
+        break;
       case 15:
-	eNB->rb_mask_ul[0] |= (0x1 | (1<<14)); // position 14
-	break;
+        eNB->rb_mask_ul[0] |= (0x1 | (1<<14)); // position 14
+        break;
       case 25:
-	eNB->rb_mask_ul[0] |= (0x1 | (1<<24)); // position 24
-	break;
+        eNB->rb_mask_ul[0] |= (0x1 | (1<<24)); // position 24
+        break;
       case 50:
-	eNB->rb_mask_ul[0] |= 0x1;
-	eNB->rb_mask_ul[1] |= (1<<17); // position 49 (49-32)
-	break;
+        eNB->rb_mask_ul[0] |= 0x1;
+        eNB->rb_mask_ul[1] |= (1<<17); // position 49 (49-32)
+        break;
       case 75:
-	eNB->rb_mask_ul[0] |= 0x1;
-	eNB->rb_mask_ul[2] |= (1<<10); // position 74 (74-64)
-	break;
+        eNB->rb_mask_ul[0] |= 0x1;
+        eNB->rb_mask_ul[2] |= (1<<10); // position 74 (74-64)
+        break;
       case 100:
-	eNB->rb_mask_ul[0] |= 0x1;
-	eNB->rb_mask_ul[3] |= (1<<3); // position 99 (99-96)
-	break;
+        eNB->rb_mask_ul[0] |= 0x1;
+        eNB->rb_mask_ul[3] |= (1<<3); // position 99 (99-96)
+        break;
       default:
-	LOG_E(PHY,"Unknown number for N_RB_UL %d\n",fp->N_RB_UL);
-	break;
+        LOG_E(PHY,"Unknown number for N_RB_UL %d\n",fp->N_RB_UL);
+        break;
       }
-      
+
       if (do_SR == 1) {
-	eNB->UE_stats[UE_id].sr_total++;
+        eNB->UE_stats[UE_id].sr_total++;
 
 
-	if (eNB->abstraction_flag == 0)
-	  metric0_SR = rx_pucch(eNB,
-				pucch_format1,
-				UE_id,
-				eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex,
-				0, // n2_pucch
-				0, // shortened format, should be use_srs flag, later
-				&SR_payload,
-				frame,
-				subframe,
-				PUCCH1_THRES);
-	
+        if (eNB->abstraction_flag == 0) {
+          metric0_SR = rx_pucch(eNB,
+                                pucch_format1,
+                                UE_id,
+                                eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex,
+                                0, // n2_pucch
+                                0, // shortened format, should be use_srs flag, later
+                                &SR_payload,
+                                frame,
+                                subframe,
+                                PUCCH1_THRES);
+          LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d Checking SR is %d (SR n1pucch is %d)\n",
+                eNB->Mod_id,
+                eNB->ulsch[UE_id]->rnti,
+                frame,
+                subframe,
+                SR_payload,
+                eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex);
+        }
 #ifdef PHY_ABSTRACTION
-	else {
-	  metric0_SR = rx_pucch_emul(eNB,
-				     proc,
-				     UE_id,
-				     pucch_format1,
-				     0,
-				     &SR_payload);
-	  LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d Checking SR (UE SR %d/%d)\n",eNB->Mod_id,
-		eNB->ulsch[UE_id]->rnti,frame,subframe,SR_payload,eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex);
-	}
-	
+        else {
+          metric0_SR = rx_pucch_emul(eNB,
+                                     proc,
+                                     UE_id,
+                                     pucch_format1,
+                                     0,
+                                     &SR_payload);
+          LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d Checking SR (UE SR %d/%d)\n",eNB->Mod_id,
+                eNB->ulsch[UE_id]->rnti,frame,subframe,SR_payload,eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex);
+        }
 #endif
-
-	if (SR_payload == 1) {
-	  LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d Got SR for PUSCH, transmitting to MAC\n",eNB->Mod_id,
-		eNB->ulsch[UE_id]->rnti,frame,subframe);
-	  eNB->UE_stats[UE_id].sr_received++;
-	  
-	  if (eNB->first_sr[UE_id] == 1) { // this is the first request for uplink after Connection Setup, so clear HARQ process 0 use for Msg4
-	    eNB->first_sr[UE_id] = 0;
-	    eNB->dlsch[UE_id][0]->harq_processes[0]->round=0;
-	    eNB->dlsch[UE_id][0]->harq_processes[0]->status=SCH_IDLE;
-	    LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d First SR\n",
-		  eNB->Mod_id,
-		  eNB->ulsch[UE_id]->rnti,frame,subframe);
-	  }
-	  
-	  if (eNB->mac_enabled==1) {
-	    mac_xface->SR_indication(eNB->Mod_id,
-				     eNB->CC_id,
-				     frame,
-				     eNB->dlsch[UE_id][0]->rnti,subframe);
-	  }
-	}
       }// do_SR==1
-      
+
       if ((n1_pucch0==-1) && (n1_pucch1==-1)) { // just check for SR
-      } else if (eNB->frame_parms.frame_type==FDD) { // FDD
-	// if SR was detected, use the n1_pucch from SR, else use n1_pucch0
-	//          n1_pucch0 = (SR_payload==1) ? eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex:n1_pucch0;
-	
-	LOG_D(PHY,"Demodulating PUCCH for ACK/NAK: n1_pucch0 %d (%d), SR_payload %d\n",n1_pucch0,eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex,SR_payload);
-	
-	if (eNB->abstraction_flag == 0) {
-	  
-	  
-	  
-	  metric0 = rx_pucch(eNB,
-			     pucch_format1a,
-			     UE_id,
-			     (uint16_t)n1_pucch0,
-			     0, //n2_pucch
-			     0, // shortened format
-			     pucch_payload0,
-			     frame,
-			     subframe,
-			     PUCCH1a_THRES);
-	  
-	  if (metric0 < metric0_SR)
-	    metric0=rx_pucch(eNB,
-			     pucch_format1a,
-			     UE_id,
-			     eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex,
-			     0, //n2_pucch
-			     0, // shortened format
-			     pucch_payload0,
-			     frame,
-			     subframe,
-			     PUCCH1a_THRES);
-	  
-	}
-	
-	if (eNB->mac_enabled==1) {
-	  mac_xface->SR_indication(eNB->Mod_id,
-				   eNB->CC_id,
-				   frame,
-				   eNB->dlsch[UE_id][0]->rnti,subframe);
-	}
-      }
-    }// do_SR==1
-    
-    if ((n1_pucch0==-1) && (n1_pucch1==-1)) { // just check for SR
-    } else if (fp->frame_type==FDD) { // FDD
-      // if SR was detected, use the n1_pucch from SR, else use n1_pucch0
-      //          n1_pucch0 = (SR_payload==1) ? eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex:n1_pucch0;
-      
-      LOG_D(PHY,"Demodulating PUCCH for ACK/NAK: n1_pucch0 %d (%d), SR_payload %d\n",n1_pucch0,eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex,SR_payload);
-      
-      if (eNB->abstraction_flag == 0) {
-	
-	
-	
-	metric0 = rx_pucch(eNB,
-			   pucch_format1a,
-			   UE_id,
-			   (uint16_t)n1_pucch0,
-			   0, //n2_pucch
-			   0, // shortened format
-			   pucch_payload0,
-			   frame,
-			   subframe,
-			   PUCCH1a_THRES);
-	  
-	if (metric0 < metric0_SR)
-	  metric0=rx_pucch(eNB,
-			   pucch_format1a,
-			   UE_id,
-			   eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex,
-			   0, //n2_pucch
-			   0, // shortened format
-			   pucch_payload0,
-			   frame,
-			   subframe,
-			   PUCCH1a_THRES);
-      }
-      else {
+      } else if (fp->frame_type==FDD) { // FDD
+        // if SR was detected, use the n1_pucch from SR, else use n1_pucch0
+        //          n1_pucch0 = (SR_payload==1) ? eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex:n1_pucch0;
+
+        LOG_D(PHY,"Demodulating PUCCH for ACK/NAK: n1_pucch0 %d (%d), SR_payload %d\n",n1_pucch0,eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex,SR_payload);
+
+        if (eNB->abstraction_flag == 0) {
+          metric0 = rx_pucch(eNB,
+                             pucch_format1a,
+                             UE_id,
+                             (uint16_t)n1_pucch0,
+                             0, //n2_pucch
+                             0, // shortened format
+                             pucch_payload0,
+                             frame,
+                             subframe,
+                             PUCCH1a_THRES);
+        }
 #ifdef PHY_ABSTRACTION
-	metric0 = rx_pucch_emul(eNB,
-				proc,
-				UE_id,
-				pucch_format1a,
-				0,
-				pucch_payload0);
+        else {
+          metric0 = rx_pucch_emul(eNB,
+                                  proc,
+                                  UE_id,
+                                  pucch_format1a,
+                                  0,
+                                  pucch_payload0);
+        }
 #endif
-      }
-	
+
+        /* cancel SR detection if reception on n1_pucch0 is better than on SR PUCCH resource index */
+        if (do_SR && metric0 > metric0_SR) SR_payload = 0;
+
+        if (do_SR && metric0 <= metric0_SR) {
+          /* when transmitting ACK/NACK on SR PUCCH resource index, SR payload is always 1 */
+          SR_payload = 1;
+
+          if (eNB->abstraction_flag == 0) {
+            metric0=rx_pucch(eNB,
+                             pucch_format1a,
+                             UE_id,
+                             eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex,
+                             0, //n2_pucch
+                             0, // shortened format
+                             pucch_payload0,
+                             frame,
+                             subframe,
+                             PUCCH1a_THRES);
+          }
+#ifdef PHY_ABSTRACTION
+          else {
+            metric0 = rx_pucch_emul(eNB,
+                                    proc,
+                                    UE_id,
+                                    pucch_format1a,
+                                    0,
+                                    pucch_payload0);
+          }
+#endif
+        }
+
 #ifdef DEBUG_PHY_PROC
-      LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d pucch1a (FDD) payload %d (metric %d)\n",
-	    eNB->Mod_id,
-	    eNB->dlsch[UE_id][0]->rnti,
-	    frame,subframe,
-	    pucch_payload0[0],metric0);
+        LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d pucch1a (FDD) payload %d (metric %d)\n",
+            eNB->Mod_id,
+            eNB->dlsch[UE_id][0]->rnti,
+            frame,subframe,
+            pucch_payload0[0],metric0);
 #endif
-	
-      process_HARQ_feedback(UE_id,eNB,proc,
-			    0,// pusch_flag
-			    pucch_payload0,
-			    2,
-			    SR_payload);
 
-    } // FDD
-    else {  //TDD
+        process_HARQ_feedback(UE_id,eNB,proc,
+                            0,// pusch_flag
+                            pucch_payload0,
+                            2,
+                            SR_payload);
+      } // FDD
+      else {  //TDD
 
-      bundling_flag = eNB->pucch_config_dedicated[UE_id].tdd_AckNackFeedbackMode;
+        bundling_flag = eNB->pucch_config_dedicated[UE_id].tdd_AckNackFeedbackMode;
 
-      // fix later for 2 TB case and format1b
+        // fix later for 2 TB case and format1b
 
-      if ((fp->frame_type==FDD) ||
-	  (bundling_flag==bundling)    ||
-	  ((fp->frame_type==TDD)&&(fp->tdd_config==1)&&((subframe!=2)||(subframe!=7)))) {
-	format = pucch_format1a;
-      } else {
-	format = pucch_format1b;
-      }
+        if ((fp->frame_type==FDD) ||
+          (bundling_flag==bundling)    ||
+          ((fp->frame_type==TDD)&&(fp->tdd_config==1)&&((subframe!=2)||(subframe!=7)))) {
+          format = pucch_format1a;
+        } else {
+          format = pucch_format1b;
+        }
 
-      // if SR was detected, use the n1_pucch from SR
-      if (SR_payload==1) {
+        // if SR was detected, use the n1_pucch from SR
+        if (SR_payload==1) {
 #ifdef DEBUG_PHY_PROC
-	LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d Checking ACK/NAK (%d,%d,%d,%d) format %d with SR\n",eNB->Mod_id,
-	      eNB->dlsch[UE_id][0]->rnti,
-	      frame,subframe,
-	      n1_pucch0,n1_pucch1,n1_pucch2,n1_pucch3,format);
-#endif
-
-	if (eNB->abstraction_flag == 0)
-	  metric0_SR = rx_pucch(eNB,
-				format,
-				UE_id,
-				eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex,
-				0, //n2_pucch
-				0, // shortened format
-				pucch_payload0,
-				frame,
-				subframe,
-				PUCCH1a_THRES);
-	else {
+          LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d Checking ACK/NAK (%d,%d,%d,%d) format %d with SR\n",eNB->Mod_id,
+                eNB->dlsch[UE_id][0]->rnti,
+                frame,subframe,
+                n1_pucch0,n1_pucch1,n1_pucch2,n1_pucch3,format);
+#endif
+
+          if (eNB->abstraction_flag == 0)
+            metric0 = rx_pucch(eNB,
+                               format,
+                               UE_id,
+                               eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex,
+                               0, //n2_pucch
+                               0, // shortened format
+                               pucch_payload0,
+                               frame,
+                               subframe,
+                               PUCCH1a_THRES);
+          else {
 #ifdef PHY_ABSTRACTION
-	  metric0 = rx_pucch_emul(eNB,proc,
-				  UE_id,
-				  format,
-				  0,
-				  pucch_payload0);
+            metric0 = rx_pucch_emul(eNB,proc,
+                                    UE_id,
+                                    format,
+                                    0,
+                                    pucch_payload0);
 #endif
-	}
-      } else { //using n1_pucch0/n1_pucch1 resources
+          }
+        } else { //using n1_pucch0/n1_pucch1 resources
 #ifdef DEBUG_PHY_PROC
-	LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d Checking ACK/NAK (%d,%d,%d,%d) format %d\n",eNB->Mod_id,
-	      eNB->dlsch[UE_id][0]->rnti,
-	      frame,subframe,
-	      n1_pucch0,n1_pucch1,n1_pucch2,n1_pucch3,format);
-#endif
-	metric0=0;
-	metric1=0;
-
-	// Check n1_pucch0 metric
-	if (n1_pucch0 != -1) {
-	  if (eNB->abstraction_flag == 0)
-	    metric0 = rx_pucch(eNB,
-			       format,
-			       UE_id,
-			       (uint16_t)n1_pucch0,
-			       0, // n2_pucch
-			       0, // shortened format
-			       pucch_payload0,
-			       frame,
-			       subframe,
-			       PUCCH1a_THRES);
-	  else {
+          LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d Checking ACK/NAK (%d,%d,%d,%d) format %d\n",eNB->Mod_id,
+                eNB->dlsch[UE_id][0]->rnti,
+                frame,subframe,
+                n1_pucch0,n1_pucch1,n1_pucch2,n1_pucch3,format);
+#endif
+          metric0=0;
+          metric1=0;
+
+          // Check n1_pucch0 metric
+          if (n1_pucch0 != -1) {
+            if (eNB->abstraction_flag == 0)
+              metric0 = rx_pucch(eNB,
+                                 format,
+                                 UE_id,
+                                 (uint16_t)n1_pucch0,
+                                 0, // n2_pucch
+                                 0, // shortened format
+                                 pucch_payload0,
+                                 frame,
+                                 subframe,
+                                 PUCCH1a_THRES);
+            else {
 #ifdef PHY_ABSTRACTION
-	    metric0 = rx_pucch_emul(eNB,
-				    proc,
-				    UE_id,
-				    format,
-				    0,
-				    pucch_payload0);
-#endif
-	  }
-	}
+              metric0 = rx_pucch_emul(eNB,
+                                      proc,
+                                      UE_id,
+                                      format,
+                                      0,
+                                      pucch_payload0);
+#endif
+            }
+          }
 
-	// Check n1_pucch1 metric
-	if (n1_pucch1 != -1) {
-	  if (eNB->abstraction_flag == 0)
-	    metric1 = rx_pucch(eNB,
-			       format,
-			       UE_id,
-			       (uint16_t)n1_pucch1,
-			       0, //n2_pucch
-			       0, // shortened format
-			       pucch_payload1,
-			       frame,
-			       subframe,
-			       PUCCH1a_THRES);
-	  else {
+          // Check n1_pucch1 metric
+          if (n1_pucch1 != -1) {
+            if (eNB->abstraction_flag == 0)
+              metric1 = rx_pucch(eNB,
+                                 format,
+                                 UE_id,
+                                 (uint16_t)n1_pucch1,
+                                 0, //n2_pucch
+                                 0, // shortened format
+                                 pucch_payload1,
+                                 frame,
+                                 subframe,
+                                 PUCCH1a_THRES);
+            else {
 #ifdef PHY_ABSTRACTION
-	    metric1 = rx_pucch_emul(eNB,
-				    proc,
-				    UE_id,
-				    format,
-				    1,
-				    pucch_payload1);
-#endif
-	  }
-	}
-      }
+              metric1 = rx_pucch_emul(eNB,
+                                      proc,
+                                      UE_id,
+                                      format,
+                                      1,
+                                      pucch_payload1);
+#endif
+            }
+          }
+        }
 
-      if (SR_payload == 1) {
-	pucch_payload = pucch_payload0;
-
-	if (bundling_flag == bundling)
-	  pucch_sel = 2;
-      } else if (bundling_flag == multiplexing) { // multiplexing + no SR
-	pucch_payload = (metric1>metric0) ? pucch_payload1 : pucch_payload0;
-	pucch_sel     = (metric1>metric0) ? 1 : 0;
-      } else { // bundling + no SR
-	if (n1_pucch1 != -1)
-	  pucch_payload = pucch_payload1;
-	else if (n1_pucch0 != -1)
-	  pucch_payload = pucch_payload0;
-
-	pucch_sel = 2;  // indicate that this is a bundled ACK/NAK
-      }
+        if (SR_payload == 1) {
+          pucch_payload = pucch_payload0;
+
+          if (bundling_flag == bundling)
+            pucch_sel = 2;
+        } else if (bundling_flag == multiplexing) { // multiplexing + no SR
+          pucch_payload = (metric1>metric0) ? pucch_payload1 : pucch_payload0;
+          pucch_sel     = (metric1>metric0) ? 1 : 0;
+        } else { // bundling + no SR
+          if (n1_pucch1 != -1)
+            pucch_payload = pucch_payload1;
+          else if (n1_pucch0 != -1)
+            pucch_payload = pucch_payload0;
+
+          pucch_sel = 2;  // indicate that this is a bundled ACK/NAK
+        }
 
 #ifdef DEBUG_PHY_PROC
-      LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d ACK/NAK metric 0 %d, metric 1 %d, sel %d, (%d,%d)\n",eNB->Mod_id,
-	    eNB->dlsch[UE_id][0]->rnti,
-	    frame,subframe,
-	    metric0,metric1,pucch_sel,pucch_payload[0],pucch_payload[1]);
-#endif
-      process_HARQ_feedback(UE_id,eNB,proc,
-			    0,// pusch_flag
-			    pucch_payload,
-			    pucch_sel,
-			    SR_payload);
+        LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d ACK/NAK metric 0 %d, metric 1 %d, sel %d, (%d,%d)\n",eNB->Mod_id,
+              eNB->dlsch[UE_id][0]->rnti,
+              frame,subframe,
+              metric0,metric1,pucch_sel,pucch_payload[0],pucch_payload[1]);
+#endif
+        process_HARQ_feedback(UE_id,eNB,proc,
+                              0,// pusch_flag
+                              pucch_payload,
+                              pucch_sel,
+                              SR_payload);
+      } // TDD
     }
-  }  
+
+    if (SR_payload == 1) {
+      LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d Got SR for PUSCH, transmitting to MAC\n",eNB->Mod_id,
+            eNB->ulsch[UE_id]->rnti,frame,subframe);
+      eNB->UE_stats[UE_id].sr_received++;
+
+      if (eNB->first_sr[UE_id] == 1) { // this is the first request for uplink after Connection Setup, so clear HARQ process 0 use for Msg4
+        eNB->first_sr[UE_id] = 0;
+        eNB->dlsch[UE_id][0]->harq_processes[0]->round=0;
+        eNB->dlsch[UE_id][0]->harq_processes[0]->status=SCH_IDLE;
+        LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d First SR\n",
+              eNB->Mod_id,
+              eNB->ulsch[UE_id]->rnti,frame,subframe);
+      }
+
+      if (eNB->mac_enabled==1) {
+        mac_xface->SR_indication(eNB->Mod_id,
+                                 eNB->CC_id,
+                                 frame,
+                                 eNB->dlsch[UE_id][0]->rnti,subframe);
+      }
+    }
+  }
 }
 
 
-- 
2.26.2