tests/tbf: Test for final ack issue

Ticket: SYS#382
Sponsored-by: On-Waves ehf
diff --git a/tests/tbf/TbfTest.cpp b/tests/tbf/TbfTest.cpp
index 911092a..f4b2e01 100644
--- a/tests/tbf/TbfTest.cpp
+++ b/tests/tbf/TbfTest.cpp
@@ -78,6 +78,84 @@
 	OSMO_ASSERT(the_bts.timing_advance()->recall(0x4232) == 4);
 }
 
+static uint8_t llc_data[200];
+
+int pcu_sock_send(struct msgb *msg)
+{
+	return 0;
+}
+
+static void test_tbf_final_ack()
+{
+	BTS the_bts;
+	gprs_rlcmac_bts *bts;
+	gprs_rlcmac_trx *trx;
+	gprs_rlcmac_pdch * pdch;
+	int tfi, i;
+	uint8_t ts_no, trx_no;
+	uint8_t ms_class = 45;
+	uint32_t fn;
+
+	uint8_t rbb[64/8];
+
+	struct msgb *dl_msg;
+	gprs_rlcmac_dl_tbf *dl_tbf;
+	gprs_rlcmac_tbf *new_tbf;
+
+	bts = the_bts.bts_data();
+	bts->alloc_algorithm = alloc_algorithm_a;
+	trx = &bts->trx[0];
+
+	ts_no = 4;
+	trx->pdch[ts_no].enable();
+	pdch = &trx->pdch[ts_no];
+
+	tfi = the_bts.tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx_no, -1);
+	OSMO_ASSERT(tfi >= 0);
+	dl_tbf = tbf_alloc_dl_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
+	OSMO_ASSERT(dl_tbf);
+
+	/* "Establish" the DL TBF */
+	dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_SEND_ASS;
+	dl_tbf->set_state(GPRS_RLCMAC_FLOW);
+	dl_tbf->m_wait_confirm = 0;
+	dl_tbf->set_new_tbf(dl_tbf);
+
+	for (i = 0; i < sizeof(llc_data); i++)
+		llc_data[i] = i%256;
+
+	/* Schedule two blocks */
+	dl_msg = msgb_alloc(sizeof(llc_data), "llc data");
+	memcpy(msgb_put(dl_msg, sizeof(llc_data)), llc_data, sizeof(llc_data));
+	dl_tbf->m_llc.enqueue(dl_msg);
+
+	dl_msg = msgb_alloc(sizeof(llc_data), "llc data");
+	memcpy(msgb_put(dl_msg, sizeof(llc_data)), llc_data, sizeof(llc_data));
+	dl_tbf->m_llc.enqueue(dl_msg);
+
+
+	/* FIXME: Need correct frame number here? */
+	fn = 0;
+	for (; fn < 3; fn++) {
+		/* Request to send one block */
+		gprs_rlcmac_rcv_rts_block(bts, trx_no, ts_no, 0, fn, 0);
+	}
+
+	/* Queue a final ACK */
+	memset(rbb, 0, sizeof(rbb));
+	/* Receive a final ACK */
+	dl_tbf->rcvd_dl_ack(1, 1, rbb);
+
+	/* Clean up and ensure tbfs are in the correct state */
+	OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE));
+	new_tbf = dl_tbf->new_tbf();
+	OSMO_ASSERT(new_tbf != dl_tbf);
+	OSMO_ASSERT(new_tbf->tfi() == 1);
+	dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
+	tbf_free(dl_tbf);
+	tbf_free(new_tbf);
+}
+
 static const struct log_info_cat default_categories[] = {
         {"DCSN1", "\033[1;31m", "Concrete Syntax Notation One (CSN1)", LOGL_INFO, 0},
         {"DL1IF", "\033[1;32m", "GPRS PCU L1 interface (L1IF)", LOGL_DEBUG, 1},
@@ -115,6 +193,7 @@
 	log_set_print_filename(osmo_stderr_target, 0);
 
 	test_tbf_tlli_update();
+	test_tbf_final_ack();
 	return EXIT_SUCCESS;
 }
 
diff --git a/tests/tbf/TbfTest.err b/tests/tbf/TbfTest.err
index d975743..3466370 100644
--- a/tests/tbf/TbfTest.err
+++ b/tests/tbf/TbfTest.err
@@ -15,3 +15,66 @@
 - Setting Control TS 2
 TBF(TFI=0 TLLI=0x00000000 DIR=UL STATE=NULL) changing tlli from TLLI=0x00000000 TLLI=0x00002342 ul_changed=0
 TBF(TFI=0 TLLI=0x00002342 DIR=DL STATE=NULL) changing tlli from TLLI=0x00002342 TLLI=0x00004232 ul_changed=1
+Searching for first unallocated TFI: TRX=0 first TS=4
+ Found TFI=0.
+********** TBF starts here **********
+Allocating DL TBF: TFI=0 TRX=0 MS_CLASS=45
+Slot Allocation (Algorithm A) for class 45
+- Skipping TS 0, because not enabled
+- Skipping TS 1, because not enabled
+- Skipping TS 2, because not enabled
+- Skipping TS 3, because not enabled
+- Assign downlink TS=4
+- Setting Control TS 4
+TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=NULL) changes state from NULL to FLOW
+TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=FLOW)  start Packet Downlink Assignment (PACCH)
++++++++++++++++++++++++++ TX : Packet Downlink Assignment +++++++++++++++++++++++++
+------------------------- TX : Packet Downlink Assignment -------------------------
+Scheduling control message at RTS for TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=FLOW) (TRX=0, TS=4)
+Sending data request: trx=0 ts=4 sapi=5 arfcn=0 fn=0 block=0 data=4f 08 20 00 44 02 00 02 08 04 00 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 
+Scheduling data message at RTS for DL TFI=0 (TRX=0, TS=4)
+TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=FLOW) downlink (V(A)==0 .. V(S)==0)
+- Sending new block at BSN 0
+-- Chunk with length 0 is less than remaining space (20): add length header to to delimit LLC frame
+Complete DL frame for TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=FLOW)len=0
+- Dequeue next LLC for TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=FLOW) (len=168)
+-- Chunk with length 168 larger than space (19) left in block: copy only remaining space, and we are done
+data block: 07 00 00 03 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 
+Sending data request: trx=0 ts=4 sapi=5 arfcn=0 fn=1 block=0 data=07 00 00 03 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 
+Scheduling data message at RTS for DL TFI=0 (TRX=0, TS=4)
+TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=FLOW) downlink (V(A)==0 .. V(S)==1)
+- Sending new block at BSN 1
+-- Chunk with length 149 larger than space (20) left in block: copy only remaining space, and we are done
+data block: 07 00 03 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 
+Sending data request: trx=0 ts=4 sapi=5 arfcn=0 fn=2 block=0 data=07 00 03 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 
+TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=FLOW) downlink acknowledge
+- Final ACK received.
+TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=FLOW) changes state from FLOW to WAIT RELEASE
+Searching for first unallocated TFI: TRX=0 first TS=4
+ Found TFI=1.
+********** TBF starts here **********
+Allocating DL TBF: TFI=1 TRX=0 MS_CLASS=45
+Slot Allocation (Algorithm A) for class 45
+- Skipping TS 0, because not enabled
+- Skipping TS 1, because not enabled
+- Skipping TS 2, because not enabled
+- Skipping TS 3, because not enabled
+- Assign downlink TS=4
+- Setting Control TS 4
+********** TBF update **********
+Slot Allocation (Algorithm A) for class 45
+- Skipping TS 0, because not enabled
+- Skipping TS 1, because not enabled
+- Skipping TS 2, because not enabled
+- Skipping TS 3, because not enabled
+- Assign downlink TS=4
+TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=WAIT RELEASE) Trigger dowlink assignment on PACCH, because another LLC PDU has arrived in between
+Send dowlink assignment on PACCH, because TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=WAIT RELEASE) exists
+TBF(TFI=1 TLLI=0x00000000 DIR=DL STATE=NULL) changes state from NULL to ASSIGN
+TBF(TFI=1 TLLI=0x00000000 DIR=DL STATE=ASSIGN) starting timer 0.
+DL packet loss of IMSI= / TLLI=0x00000000: 0%
+TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=WAIT RELEASE) free
+********** TBF ends here **********
+TBF(TFI=1 TLLI=0x00000000 DIR=DL STATE=ASSIGN) free
+TBF(TFI=1 TLLI=0x00000000 DIR=DL STATE=ASSIGN) stopping timer 0.
+********** TBF ends here **********