From 66d116053831a4e18a29dcdae491c92a404961af Mon Sep 17 00:00:00 2001
From: pravin <pravin.manoharan@radisys.com>
Date: Fri, 10 Jun 2016 20:49:11 +0530
Subject: [PATCH 1/2] Added Test case for EPDAN profiling

Fixed data is used for test case
function test_tbf_final_ack_EPDAN is added.
---
 tests/tbf/TbfTest.cpp | 383 +++++++++++++++++++++++++++++++++-----------------
 1 file changed, 254 insertions(+), 129 deletions(-)

diff --git a/tests/tbf/TbfTest.cpp b/tests/tbf/TbfTest.cpp
index e1be844..0a4c0de 100644
--- a/tests/tbf/TbfTest.cpp
+++ b/tests/tbf/TbfTest.cpp
@@ -26,6 +26,7 @@
 #include "pcu_utils.h"
 #include "gprs_bssgp_pcu.h"
 #include "pcu_l1_if.h"
+#include "egprs_rlc_compression.h"
 
 extern "C" {
 #include "pcu_vty.h"
@@ -51,19 +52,261 @@ static void check_tbf(gprs_rlcmac_tbf *tbf)
 		OSMO_ASSERT(tbf->T != 0);
 }
 
-/*
-static unsigned inc_fn(fn)
+static gprs_rlcmac_dl_tbf *create_dl_tbf(BTS *the_bts, uint8_t ms_class,
+	uint8_t egprs_ms_class, uint8_t *trx_no_)
+{
+	gprs_rlcmac_bts *bts;
+	int tfi;
+	uint8_t trx_no;
+
+	gprs_rlcmac_dl_tbf *dl_tbf;
+
+	bts = the_bts->bts_data();
+
+	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, trx_no, ms_class, egprs_ms_class, 1);
+	check_tbf(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;
+
+	*trx_no_ = trx_no;
+
+	return dl_tbf;
+}
+
+static gprs_rlcmac_dl_tbf *tbf_init(BTS *the_bts,
+		int mcs)
+{
+	unsigned i;
+	uint8_t ms_class = 11;
+	uint8_t egprs_ms_class = 11;
+	uint32_t fn = 0;
+	uint8_t trx_no;
+	uint32_t tlli = 0xffeeddcc;
+	uint8_t test_data[512];
+
+	uint8_t rbb[64/8];
+
+	gprs_rlcmac_dl_tbf *dl_tbf;
+
+	memset(test_data, 1, sizeof(test_data));
+	the_bts->bts_data()->initial_mcs_dl = mcs;
+
+	dl_tbf = create_dl_tbf(the_bts, ms_class, egprs_ms_class, &trx_no);
+	dl_tbf->update_ms(tlli, GPRS_RLCMAC_DL_TBF);
+
+	for (i = 0; i < sizeof(test_data); i++)
+		test_data[i] = i%256;
+
+	OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
+
+	/* Schedule a LLC frame
+	 * passing only 100 bytes, since it is enough to construct
+	 * 2 RLC data blocks. Which are enough to test Header Type 1
+	 * cases
+	 */
+	dl_tbf->append_data(ms_class, 1000, test_data, 100);
+
+	OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
+
+	return dl_tbf;
+
+}
+
+static void send_ul_mac_block(BTS *the_bts, unsigned trx_no, unsigned ts_no,
+	RlcMacUplink_t *ulreq, unsigned fn)
 {
-	unsigned next_fn;
+	bitvec *rlc_block;
+	uint8_t buf[64];
+	int num_bytes;
+	struct gprs_rlcmac_pdch *pdch;
+	struct pcu_l1_meas meas;
+
+	meas.set_rssi(31);
+
+	rlc_block = bitvec_alloc(23);
+
+	encode_gsm_rlcmac_uplink(rlc_block, ulreq);
+	num_bytes = bitvec_pack(rlc_block, &buf[0]);
+	OSMO_ASSERT(size_t(num_bytes) < sizeof(buf));
+	bitvec_free(rlc_block);
 
-	next_fn = fn + 4;
-	if ((block_nr % 3) == 2)
-		next_fn ++;
-	next_fn = next_fn % 2715648;
+	the_bts->set_current_block_frame_number(fn, 0);
 
-	return next_fn;
+	pdch = &the_bts->bts_data()->trx[trx_no].pdch[ts_no];
+	pdch->rcv_block(&buf[0], num_bytes, fn, &meas);
+}
+static void send_control_ack(gprs_rlcmac_tbf *tbf)
+{
+	RlcMacUplink_t ulreq = {0};
+
+	OSMO_ASSERT(tbf->poll_fn != 0);
+	OSMO_ASSERT(tbf->is_control_ts(tbf->poll_ts));
+
+	ulreq.u.MESSAGE_TYPE = MT_PACKET_CONTROL_ACK;
+	Packet_Control_Acknowledgement_t *ctrl_ack =
+		&ulreq.u.Packet_Control_Acknowledgement;
+
+	ctrl_ack->PayloadType = GPRS_RLCMAC_CONTROL_BLOCK;
+	ctrl_ack->TLLI = tbf->tlli();
+	send_ul_mac_block(tbf->bts, tbf->trx->trx_no, tbf->poll_ts,
+		&ulreq, tbf->poll_fn);
+}
+
+
+static unsigned fn2bn(unsigned fn)
+{
+	return (fn % 52) / 4;
+}
+
+static unsigned fn_add_blocks(unsigned fn, unsigned blocks)
+{
+	unsigned bn = fn2bn(fn) + blocks;
+	fn = fn - (fn % 52);
+	fn += bn * 4 + bn / 3;
+	return fn % 2715648;
+}
+
+static void setup_bts(BTS *the_bts, uint8_t ts_no, uint8_t cs = 1)
+{
+	gprs_rlcmac_bts *bts;
+	gprs_rlcmac_trx *trx;
+
+	bts = the_bts->bts_data();
+	bts->alloc_algorithm = alloc_algorithm_a;
+	bts->initial_cs_dl = cs;
+	bts->initial_cs_ul = cs;
+	trx = &bts->trx[0];
+
+	trx->pdch[ts_no].enable();
+}
+
+
+
+static void request_dl_rlc_block(struct gprs_rlcmac_bts *bts,
+	uint8_t trx_no, uint8_t ts_no, uint16_t arfcn,
+	uint32_t *fn, uint8_t *block_nr = NULL)
+{
+	uint8_t bn = fn2bn(*fn);
+	gprs_rlcmac_rcv_rts_block(bts, trx_no, ts_no, arfcn, *fn, bn);
+	*fn = fn_add_blocks(*fn, 1);
+	bn += 1;
+	if (block_nr)
+		*block_nr = bn;
+}
+
+static void request_dl_rlc_block(struct gprs_rlcmac_tbf *tbf,
+	uint32_t *fn, uint8_t *block_nr = NULL)
+{
+	request_dl_rlc_block(tbf->bts->bts_data(), tbf->trx->trx_no,
+		tbf->control_ts, tbf->trx->arfcn, fn, block_nr);
+}
+
+enum test_tbf_final_ack_mode {
+	TEST_MODE_STANDARD,
+	TEST_MODE_REVERSE_FREE
+};
+
+
+static gprs_rlcmac_ul_tbf *establish_ul_tbf_two_phase_spb_pdan(BTS *the_bts,
+	uint8_t ts_no, uint32_t tlli, uint32_t *fn, uint16_t qta,
+	uint8_t ms_class, uint8_t egprs_ms_class)
+{
+	GprsMs *ms;
+	uint32_t rach_fn = *fn - 51;
+	uint32_t sba_fn = *fn + 52;
+	uint8_t trx_no = 0;
+	int tfi = 0;
+	gprs_rlcmac_ul_tbf *ul_tbf;
+	struct gprs_rlcmac_pdch *pdch;
+	gprs_rlcmac_bts *bts;
+	RlcMacUplink_t ulreq = {0};
+	struct pcu_l1_meas meas;
+	struct gprs_rlc_ul_header_egprs_3 *egprs3  = NULL;
+	GprsCodingScheme cs;
+
+	meas.set_rssi(31);
+	bts = the_bts->bts_data();
+	uint8_t data_msg[42] = {
+	0x40, 0x20, 0x0a, 0x7f, 0x79, 0x61, 0x00, 0x49, 0x2b, 0x4c, 0x00,
+	0xe1, 0xf2, 0xe9, 0xfe, 0xae, 0x7a, 0x00, 0xf0, 0x30, 0x3b, 0x2b, 0x2b};
+	pdch = &the_bts->bts_data()->trx[trx_no].pdch[ts_no];
+	pdch->rcv_block(&data_msg[0], 23, *fn, &meas);
+}
+
+static void establish_and_use_egprs_dl_tbf_pdan(BTS *the_bts,
+		int mcs, int demanded_mcs)
+{
+	uint32_t fn = 0;
+	gprs_rlcmac_dl_tbf *dl_tbf;
+	uint8_t block_nr = 0;
+	int index1 = 0;
+	int itr = 0;
+	uint8_t bn;
+	struct msgb *msg;
+	struct gprs_rlc_dl_header_egprs_3 *egprs3;
+	int ts_no = 4;
+	uint32_t fn1 = 26;
+	uint16_t qta = 31;
+	uint32_t tlli = 0xf1223344;
+	const char *imsi = "0011223344";
+	uint8_t ms_class = 1;
+	uint8_t egprs_ms_class = 1;
+	gprs_rlcmac_ul_tbf *ul_tbf;
+	GprsMs *ms;
+	uint8_t test_data[1900] = { 0 };
+
+	printf("Testing  for pdan MCS %d to reseg_mcs %d\n", mcs, demanded_mcs);
+
+	/* Table 10.4.8a.3.1 of 44.060 */
+	dl_tbf = tbf_init(the_bts, mcs);
+	dl_tbf->append_data(ms_class, 1000, test_data, 1400); /* changed*/
+	dl_tbf->append_data(ms_class, 1000, test_data, 1400); /* changed*/
+	dl_tbf->append_data(ms_class, 1000, test_data, 1400); /* changed*/
+	dl_tbf->append_data(ms_class, 1000, test_data, 1400); /* changed*/
+	dl_tbf->append_data(ms_class, 1000, test_data, 1400); /* changed*/
+	dl_tbf->append_data(ms_class, 1000, test_data, 1400); /* changed*/
+	dl_tbf->append_data(ms_class, 1000, test_data, 1400); /* changed*/
+	dl_tbf->append_data(ms_class, 1000, test_data, 1400); /* changed*/
+	dl_tbf->append_data(ms_class, 1000, test_data, 1400); /* changed*/
+	dl_tbf->append_data(ms_class, 1000, test_data, 1400); /* changed*/
+	for (itr ; itr < 150 ; itr++) {
+		fn = fn_add_blocks(fn, 1);
+		/* Send first RLC data block BSN 0 */
+		msg = dl_tbf->create_dl_acked_block(fn, dl_tbf->control_ts);
+	}
+	fn1 = dl_tbf->poll_fn;
+	ul_tbf = establish_ul_tbf_two_phase_spb_pdan(the_bts, ts_no, tlli,
+		&fn1, qta, ms_class, egprs_ms_class);
+}
+
+static void test_tbf_final_ack_EPDAN(void)
+{
+	BTS the_bts;
+	gprs_rlcmac_bts *bts;
+	uint8_t ts_no = 4;
+	int i, j;
+
+	printf("=== start %s ===\n", __func__);
+	bts = the_bts.bts_data();
+	bts->cs_downgrade_threshold = 0;
+	setup_bts(&the_bts, ts_no);
+	bts->dl_tbf_idle_msec = 200;
+	bts->ws_base = 128;
+	bts->egprs_enabled = 1;
+
+	/*
+	 * First parameter is current MCS, second one is demanded_mcs
+	 * currently only MCS5->MCS2, MCS6->3, MCS4->MCS1 is tested in UT
+	 * rest scenarios has been integration tested
+	 */
+	establish_and_use_egprs_dl_tbf_pdan(&the_bts, 6, 3);
+	printf("=== end %s ===\n", __func__);
 }
-*/
 
 static void test_tbf_base()
 {
@@ -151,84 +394,6 @@ int pcu_sock_send(struct msgb *msg)
 	return 0;
 }
 
-static void setup_bts(BTS *the_bts, uint8_t ts_no, uint8_t cs = 1)
-{
-	gprs_rlcmac_bts *bts;
-	gprs_rlcmac_trx *trx;
-
-	bts = the_bts->bts_data();
-	bts->alloc_algorithm = alloc_algorithm_a;
-	bts->initial_cs_dl = cs;
-	bts->initial_cs_ul = cs;
-	trx = &bts->trx[0];
-
-	trx->pdch[ts_no].enable();
-}
-
-static gprs_rlcmac_dl_tbf *create_dl_tbf(BTS *the_bts, uint8_t ms_class,
-	uint8_t egprs_ms_class, uint8_t *trx_no_)
-{
-	gprs_rlcmac_bts *bts;
-	int tfi;
-	uint8_t trx_no;
-
-	gprs_rlcmac_dl_tbf *dl_tbf;
-
-	bts = the_bts->bts_data();
-
-	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, trx_no, ms_class, egprs_ms_class, 1);
-	check_tbf(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;
-	check_tbf(dl_tbf);
-
-	*trx_no_ = trx_no;
-
-	return dl_tbf;
-}
-
-static unsigned fn2bn(unsigned fn)
-{
-	return (fn % 52) / 4;
-}
-
-static unsigned fn_add_blocks(unsigned fn, unsigned blocks)
-{
-	unsigned bn = fn2bn(fn) + blocks;
-	fn = fn - (fn % 52);
-	fn += bn * 4 + bn / 3;
-	return fn % 2715648;
-}
-
-static void request_dl_rlc_block(struct gprs_rlcmac_bts *bts,
-	uint8_t trx_no, uint8_t ts_no, uint16_t arfcn,
-	uint32_t *fn, uint8_t *block_nr = NULL)
-{
-	uint8_t bn = fn2bn(*fn);
-	gprs_rlcmac_rcv_rts_block(bts, trx_no, ts_no, arfcn, *fn, bn);
-	*fn = fn_add_blocks(*fn, 1);
-	bn += 1;
-	if (block_nr)
-		*block_nr = bn;
-}
-
-static void request_dl_rlc_block(struct gprs_rlcmac_tbf *tbf,
-	uint32_t *fn, uint8_t *block_nr = NULL)
-{
-	request_dl_rlc_block(tbf->bts->bts_data(), tbf->trx->trx_no,
-		tbf->control_ts, tbf->trx->arfcn, fn, block_nr);
-}
-
-enum test_tbf_final_ack_mode {
-	TEST_MODE_STANDARD,
-	TEST_MODE_REVERSE_FREE
-};
-
 static void test_tbf_final_ack(enum test_tbf_final_ack_mode test_mode)
 {
 	BTS the_bts;
@@ -576,47 +741,6 @@ static gprs_rlcmac_ul_tbf *establish_ul_tbf_single_phase(BTS *the_bts,
 	return ul_tbf;
 }
 
-static void send_ul_mac_block(BTS *the_bts, unsigned trx_no, unsigned ts_no,
-	RlcMacUplink_t *ulreq, unsigned fn)
-{
-	bitvec *rlc_block;
-	uint8_t buf[64];
-	int num_bytes;
-	struct gprs_rlcmac_pdch *pdch;
-	struct pcu_l1_meas meas;
-
-	meas.set_rssi(31);
-
-	rlc_block = bitvec_alloc(23);
-
-	encode_gsm_rlcmac_uplink(rlc_block, ulreq);
-	num_bytes = bitvec_pack(rlc_block, &buf[0]);
-	OSMO_ASSERT(size_t(num_bytes) < sizeof(buf));
-	bitvec_free(rlc_block);
-
-	the_bts->set_current_block_frame_number(fn, 0);
-
-	pdch = &the_bts->bts_data()->trx[trx_no].pdch[ts_no];
-	pdch->rcv_block(&buf[0], num_bytes, fn, &meas);
-}
-
-static void send_control_ack(gprs_rlcmac_tbf *tbf)
-{
-	RlcMacUplink_t ulreq = {0};
-
-	OSMO_ASSERT(tbf->poll_fn != 0);
-	OSMO_ASSERT(tbf->is_control_ts(tbf->poll_ts));
-
-	ulreq.u.MESSAGE_TYPE = MT_PACKET_CONTROL_ACK;
-	Packet_Control_Acknowledgement_t *ctrl_ack =
-		&ulreq.u.Packet_Control_Acknowledgement;
-
-	ctrl_ack->PayloadType = GPRS_RLCMAC_CONTROL_BLOCK;
-	ctrl_ack->TLLI = tbf->tlli();
-	send_ul_mac_block(tbf->bts, tbf->trx->trx_no, tbf->poll_ts,
-		&ulreq, tbf->poll_fn);
-}
-
 static gprs_rlcmac_ul_tbf *establish_ul_tbf_two_phase(BTS *the_bts,
 	uint8_t ts_no, uint32_t tlli, uint32_t *fn, uint16_t qta,
 	uint8_t ms_class, uint8_t egprs_ms_class)
@@ -1338,6 +1462,7 @@ int main(int argc, char **argv)
 	vty_init(&pcu_vty_info);
 	pcu_vty_init(&debug_log_info);
 
+	egprs_compress::instance()->decode_tree_init();
 	test_tbf_base();
 	test_tbf_tlli_update();
 	test_tbf_final_ack(TEST_MODE_STANDARD);
@@ -1356,7 +1481,7 @@ int main(int argc, char **argv)
 	test_tbf_ws();
 	test_tbf_egprs_two_phase();
 	test_tbf_egprs_dl();
-
+	test_tbf_final_ack_EPDAN();
 	if (getenv("TALLOC_REPORT_FULL"))
 		talloc_report_full(tall_pcu_ctx, stderr);
 	return EXIT_SUCCESS;
-- 
1.9.1

