pcu: Wait for USF before start using UL TBF

Change-Id: Ibe8f8c24b239715c0a43d52e59475011e78cb4eb
diff --git a/pcu/GPRS_Components.ttcn b/pcu/GPRS_Components.ttcn
index a3e8c94..59c99d8 100644
--- a/pcu/GPRS_Components.ttcn
+++ b/pcu/GPRS_Components.ttcn
@@ -992,6 +992,36 @@
 	return;
 }
 
+/* Keep receiving & discarding DL blocks until the PCU requests USF for this MS */
+function f_ms_wait_usf(inout GprsMS ms, template (value) TsTrxBtsNum nr := ts_TsTrxBtsNum)
+runs on MS_BTS_IFACE_CT return uint32_t {
+	var BTS_PDTCH_Block data_msg;
+
+	BTS.send(ts_PCUIF_RTS_REQ(nr.bts_nr, nr.trx_nr, nr.ts_nr,
+				  sapi := PCU_IF_SAPI_PDTCH, fn := 0,
+				  arfcn := f_trxnr2arfcn(valueof(nr.trx_nr)),
+				  block_nr := nr.blk_nr));
+	alt {
+	[] as_pcuif_rx_ignore_empty(nr);
+	[] BTS.receive(tr_PCUIF_DATA_PDTCH(nr.bts_nr,
+					   tr_PCUIF_DATA(nr.trx_nr, nr.ts_nr, sapi := PCU_IF_SAPI_PDTCH),
+					   ?)) -> value data_msg {
+		var uint3_t rx_usf := f_rlcmac_dl_block_get_usf(data_msg.dl_block);
+		var uint3_t exp_usf := ms.ul_tbf.usf[valueof(nr.ts_nr)];
+		log("Rx DL block USF ", rx_usf, " vs exp USF ", exp_usf);
+		if (rx_usf != exp_usf) {
+			BTS.send(ts_PCUIF_RTS_REQ(nr.bts_nr, nr.trx_nr, nr.ts_nr,
+				  sapi := PCU_IF_SAPI_PDTCH, fn := 0,
+				  arfcn := f_trxnr2arfcn(valueof(nr.trx_nr)),
+				  block_nr := nr.blk_nr));
+			repeat;
+		}
+		}
+	};
+
+	return data_msg.raw.fn;
+}
+
 ////////////////////////
 // Low level APIs
 ////////////////////////
diff --git a/pcu/PCU_Tests.ttcn b/pcu/PCU_Tests.ttcn
index e485868..a1da67b 100644
--- a/pcu/PCU_Tests.ttcn
+++ b/pcu/PCU_Tests.ttcn
@@ -326,6 +326,9 @@
 			var RlcmacDlBlock dl_block;
 			var uint32_t poll_fn;
 
+			/* Wait until PCU starts requesting for UL block on this TBF: */
+			f_ms_wait_usf(g_ms[i]);
+
 			f_ms_tx_ul_data_block(g_ms[i], dummy, with_tlli := true, fn := g_ms[i].ul_tbf.start_time_fn, nr := nr);
 			f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, poll_fn, nr := nr);
 		}
@@ -429,6 +432,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send one UL block (with TLLI since we are in One-Phase Access
 	   contention resolution) and make sure it is ACKED fine */
 	f_ms_tx_ul_data_block_multi(ms, 1, with_tlli := true, fn := ms.ul_tbf.start_time_fn);
@@ -531,6 +537,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* In a busy network, there can be a significant delay between resource
 	 * allocation (Packet Uplink Assignment above) and the actual time when
 	 * the MS is allowed to transmit the first Uplink data block. */
@@ -825,6 +834,8 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
 
 	/* The actual / old link quality values. We need to keep track of the old
 	 * (basically previous) link quality value, because OsmoPCU actually
@@ -917,6 +928,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send one UL block (with TLLI since we are in One-Phase Access
 	   contention resolution) and make sure it is ACKED fine. */
 	/* 16 bytes fills the llc block (because TLLI takes 4 bytes) */
@@ -989,6 +1003,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send one UL block (with TLLI since we are in One-Phase Access
 	   contention resolution) and make sure it is ACKED fine. */
 	/* 16 bytes fills the llc block (because TLLI takes 4 bytes) */
@@ -1602,6 +1619,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	f_ms_tx_ul_data_block_multi(ms, 5, with_tlli := true);
 	exp_ul_ack_sub := tr_UlAckNackGprs(*, tr_AckNackDescription('1'B), *);
 	exp_ul_ack := tr_RLCMAC_UL_ACK_NACK_GPRS(ms.ul_tbf.tfi, exp_ul_ack_sub);
@@ -2172,6 +2192,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send one UL block (with TLLI since we are in One-Phase Access
 	   contention resolution) and make sure it is ACKED fine. */
 	total_payload := f_rnd_octstring(f_ultbf_payload_fill_length(ms.ul_tbf, true));
@@ -2223,6 +2246,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send one UL block (with TLLI since we are in One-Phase Access
 	   contention resolution) and make sure it is ACKED fine. */
 	payload := f_rnd_octstring(16); /* 16 bytes fills the llc block (because TLLI takes 4 bytes) */
@@ -2358,6 +2384,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	f_TC_ul_data_toolong_fills_padding_cs(ms, CS_2, 2);
 	f_TC_ul_data_toolong_fills_padding_cs(ms, CS_3, 1);
 	f_TC_ul_data_toolong_fills_padding_cs(ms, CS_4, 0);
@@ -2391,6 +2420,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send one UL block (with TLLI since we are in One-Phase Access
 	   contention resolution) and make sure it is ACKED fine */
 	f_ms_tx_ul_data_block_multi(ms, 1, with_tlli := true, fn := ms.ul_tbf.start_time_fn);
@@ -2668,6 +2700,9 @@
 	f_ms_establish_ul_tbf(ms);
 	tfi := ms.ul_tbf.tfi;
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send one UL block (with TLLI since we are in One-Phase Access
 	   contention resolution) and make sure it is ACKED fine. */
 	payload := f_rnd_octstring(f_ultbf_payload_fill_length(ms.ul_tbf, true)); /* 16 bytes fills the llc block (because TLLI takes 4 bytes) */
@@ -2884,6 +2919,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Summary of what's transmitted:
 	 * 1- UL RlcDataBlock(dataA) [BSN=0, CV=3]
 	 * 2- UL RlcDataBlock(dataA finished, dataB starts) [BSN=1, CV=2]
@@ -3185,6 +3223,9 @@
 	  through PDCH (no multiblock assignment possible through PCH) */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send one UL block (with TLLI since we are in One-Phase Access
 	   contention resolution) and make sure it is ACKED fine */
 	f_ms_tx_ul_data_block(ms, data, with_tlli := true, fn := ms.ul_tbf.start_time_fn,
@@ -3330,6 +3371,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send one UL block (with TLLI since we are in One-Phase Access
 	   contention resolution) and make sure it is ACKED fine */
 	f_ms_tx_ul_data_block_multi(ms, 1, with_tlli := true, fn := ms.ul_tbf.start_time_fn);
@@ -3416,6 +3460,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send one UL block (with TLLI since we are in One-Phase Access
 	   contention resolution) and make sure it is ACKED fine */
 	f_ms_tx_ul_data_block_multi(ms, 1, with_tlli := true, fn := ms.ul_tbf.start_time_fn);
@@ -3580,6 +3627,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* build mobile Identity */
 	mi := valueof(ts_MI_IMSI_LV(imsi));
 	mi_enc_lv := enc_MobileIdentityLV(mi);
@@ -3624,6 +3674,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send paging request with or without TMSI */
 	if (use_ptmsi) {
 		tmsi := oct2int(f_rnd_octstring(4)); /* Random P-TMSI */
@@ -3786,6 +3839,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Fake GMM GPRS Attach or similar, PCU doesn't care about upper layers here */
 	f_ms_tx_ul_data_block_multi(ms, 1, with_tlli := true, fn := ms.ul_tbf.start_time_fn);
 	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
@@ -3845,6 +3901,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Fake GMM GPRS Attach or similar, PCU doesn't care about upper layers here */
 	f_ms_tx_ul_data_block_multi(ms, 1, with_tlli := true, fn := ms.ul_tbf.start_time_fn);
 	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
@@ -3960,6 +4019,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send one UL block (with TLLI since we are in One-Phase Access
 	   contention resolution) and make sure it is ACKED fine. */
 	payload := f_rnd_octstring(f_ultbf_payload_fill_length(ms.ul_tbf, true));
@@ -4024,6 +4086,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send one UL block (with TLLI since we are in One-Phase Access
 	   contention resolution) and make sure it is ACKED fine. */
 	payload := f_rnd_octstring(f_ultbf_payload_fill_length(ms.ul_tbf, true));
@@ -4048,10 +4113,12 @@
 	 * The MS decides it want to send new Ul TBF so it send RACH req to ask for it: */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	dl_fn := f_ms_wait_usf(ms);
+
 	/* Send one UL block (with TLLI since we are in One-Phase Access
 	 * contention resolution) and make sure it is ACKED fine. */
 	payload := f_rnd_octstring(f_ultbf_payload_fill_length(ms.ul_tbf, true));
-	dl_fn := f_rx_rlcmac_dl_block_exp_dummy(dl_block);
 	f_ms_tx_ul_data_block(ms, payload, cv := 1, with_tlli := true, fn := f_next_pdch_block(dl_fn));
 
 	/* UL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
@@ -4099,6 +4166,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send one UL block (with TLLI since we are in One-Phase Access
 	   contention resolution) and make sure it is ACKED fine. */
 	payload := f_rnd_octstring(f_ultbf_payload_fill_length(ms.ul_tbf, true));
@@ -4692,6 +4762,9 @@
 	/* Establish an Uplink TBF */
 	f_ms_establish_ul_tbf(ms);
 
+	/* Wait until PCU starts requesting for UL block on this TBF: */
+	f_ms_wait_usf(ms);
+
 	/* Send an Uplink block, so this TBF becomes "active" */
 	f_ms_tx_ul_data_block(ms, data, with_tlli := true, fn := ms.ul_tbf.start_time_fn);