Add test case for testing PUAN

This test case is for testing generation of
EGPRS PUAN. Corresponding log files .ok and .err
are modified.

Change-Id: I18e6d4a9e90fd6453fe14187beab27dfeae8dbe9
diff --git a/tests/tbf/TbfTest.cpp b/tests/tbf/TbfTest.cpp
index bebb6ef..7f4c6ac 100644
--- a/tests/tbf/TbfTest.cpp
+++ b/tests/tbf/TbfTest.cpp
@@ -1215,6 +1215,348 @@
 	return ul_tbf;
 }
 
+static gprs_rlcmac_ul_tbf *establish_ul_tbf(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, i = 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();
+
+	/* needed to set last_rts_fn in the PDCH object */
+	request_dl_rlc_block(bts, trx_no, ts_no, fn);
+
+	/*
+	 * simulate RACH, this sends an Immediate
+	 * Assignment Uplink on the AGCH
+	 */
+	the_bts->rcv_rach(0x73, rach_fn, qta, 0, GSM_L1_BURST_TYPE_ACCESS_0);
+
+	/* get next free TFI */
+	tfi = the_bts->tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
+
+	/* fake a resource request */
+	ulreq.u.MESSAGE_TYPE = MT_PACKET_RESOURCE_REQUEST;
+	ulreq.u.Packet_Resource_Request.PayloadType = GPRS_RLCMAC_CONTROL_BLOCK;
+	ulreq.u.Packet_Resource_Request.ID.UnionType = 1; /* != 0 */
+	ulreq.u.Packet_Resource_Request.ID.u.TLLI = tlli;
+	ulreq.u.Packet_Resource_Request.Exist_MS_Radio_Access_capability = 1;
+	ulreq.u.Packet_Resource_Request.MS_Radio_Access_capability.
+		Count_MS_RA_capability_value = 1;
+	ulreq.u.Packet_Resource_Request.MS_Radio_Access_capability.
+		MS_RA_capability_value[0].u.Content.
+		Exist_Multislot_capability = 1;
+	ulreq.u.Packet_Resource_Request.MS_Radio_Access_capability.
+		MS_RA_capability_value[0].u.Content.Multislot_capability.
+		Exist_GPRS_multislot_class = 1;
+	ulreq.u.Packet_Resource_Request.MS_Radio_Access_capability.
+		MS_RA_capability_value[0].u.Content.Multislot_capability.
+		GPRS_multislot_class = ms_class;
+	if (egprs_ms_class) {
+		ulreq.u.Packet_Resource_Request.MS_Radio_Access_capability.
+			MS_RA_capability_value[0].u.Content.
+			Multislot_capability.Exist_EGPRS_multislot_class = 1;
+		ulreq.u.Packet_Resource_Request.MS_Radio_Access_capability.
+			MS_RA_capability_value[0].u.Content.
+			Multislot_capability.EGPRS_multislot_class = ms_class;
+	}
+	send_ul_mac_block(the_bts, trx_no, ts_no, &ulreq, sba_fn);
+
+	/* check the TBF */
+	ul_tbf = the_bts->ul_tbf_by_tfi(tfi, trx_no, ts_no);
+	/* send packet uplink assignment */
+	*fn = sba_fn;
+	request_dl_rlc_block(ul_tbf, fn);
+
+	/* send real acknowledgement */
+	send_control_ack(ul_tbf);
+
+	check_tbf(ul_tbf);
+
+	return ul_tbf;
+}
+
+static gprs_rlcmac_ul_tbf *establish_ul_tbf_two_phase_puan_URBB_no_length(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, gprs_rlcmac_ul_tbf *ul_tbf)
+{
+	OSMO_ASSERT(ul_tbf);
+	OSMO_ASSERT(ul_tbf->ta() == qta / 4);
+	GprsMs *ms;
+	uint32_t rach_fn = *fn - 51;
+	uint32_t sba_fn = *fn + 52;
+	uint8_t trx_no = 0;
+	int tfi = 0, i = 0;
+	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;
+
+
+	/* send fake data with cv=0*/
+	struct gprs_rlc_ul_header_egprs_3 *hdr3 = NULL;
+	uint8_t data[49] = {0};
+
+	hdr3 = (struct gprs_rlc_ul_header_egprs_3 *)data;
+
+	/*header_construction */
+	memset(data, 0x2b, sizeof(data));
+	/* Message with CRBB */
+	for (int i = 0 ; i < 80; i++) {
+		hdr3->r = 0;
+		hdr3->si = 0;
+		hdr3->cv = 10;
+		hdr3->tfi_hi = (tfi >> 3) & 0x3;
+		hdr3->tfi_lo = tfi & 0x7;
+		hdr3->bsn1_hi = ((i * 2)&0x1f);
+		hdr3->bsn1_lo = ((i * 2)/32);
+		hdr3->cps_hi = 0;
+		hdr3->cps_lo = 0;
+		hdr3->spb = 0;
+		hdr3->rsb = 0;
+		hdr3->pi = 0;
+		hdr3->spare = 0;
+		hdr3->dummy = 1;
+		data[4] = 0x0;
+		data[5] = 0x0;
+		data[6] = 0x2b;
+		data[7] = 0x2b;
+		pdch = &the_bts->bts_data()->trx[trx_no].pdch[ts_no];
+		pdch->rcv_block(&data[0], sizeof(data), *fn, &meas);
+	}
+	ul_tbf->create_ul_ack(*fn, ts_no);
+	memset(data, 0x2b, sizeof(data));
+	hdr3 = (struct gprs_rlc_ul_header_egprs_3 *)data;
+	hdr3->r = 0;
+	hdr3->si = 0;
+	hdr3->cv = 0;
+	hdr3->tfi_hi = (tfi >> 3) & 0x3;
+	hdr3->tfi_lo = tfi & 0x7;
+	hdr3->bsn1_hi = 0;
+	hdr3->bsn1_lo = 2;
+	hdr3->cps_hi = 0;
+	hdr3->cps_lo = 0;
+	hdr3->spb = 0;
+	hdr3->rsb = 0;
+	hdr3->pi = 0;
+	hdr3->spare = 0;
+	hdr3->dummy = 1;
+	data[4] = 0x0;
+	data[5] = 0x2b;
+	data[6] = 0x2b;
+	data[7] = 0x2b;
+
+	pdch = &the_bts->bts_data()->trx[trx_no].pdch[ts_no];
+	pdch->rcv_block(&data[0], sizeof(data), *fn, &meas);
+
+	request_dl_rlc_block(ul_tbf, fn);
+
+	check_tbf(ul_tbf);
+	OSMO_ASSERT(ul_tbf->ul_ack_state == GPRS_RLCMAC_UL_ACK_NONE);
+
+	ms = the_bts->ms_by_tlli(tlli);
+	OSMO_ASSERT(ms != NULL);
+	OSMO_ASSERT(ms->ta() == qta/4);
+	OSMO_ASSERT(ms->ul_tbf() == ul_tbf);
+
+	return ul_tbf;
+}
+
+static gprs_rlcmac_ul_tbf *establish_ul_tbf_two_phase_puan_URBB_with_length(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, gprs_rlcmac_ul_tbf *ul_tbf)
+{
+	OSMO_ASSERT(ul_tbf);
+	OSMO_ASSERT(ul_tbf->ta() == qta / 4);
+	GprsMs *ms;
+	uint32_t rach_fn = *fn - 51;
+	uint32_t sba_fn = *fn + 52;
+	uint8_t trx_no = 0;
+	int tfi = 0, i = 0;
+	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;
+
+	check_tbf(ul_tbf);
+	/* send fake data with cv=0*/
+	struct gprs_rlc_ul_header_egprs_3 *hdr3 = NULL;
+	uint8_t data[49] = {0};
+
+	hdr3 = (struct gprs_rlc_ul_header_egprs_3 *)data;
+
+	/*header_construction */
+	memset(data, 0x2b, sizeof(data));
+
+	/* Message with URBB & URBB length */
+	for (int i = 0 ; i < 20; i++) {
+		hdr3->r = 0;
+		hdr3->si = 0;
+		hdr3->cv = 10;
+		hdr3->tfi_hi = (tfi >> 3) & 0x3;
+		hdr3->tfi_lo = tfi & 0x7;
+		hdr3->bsn1_hi = ((i * 2)&0x1f);
+		hdr3->bsn1_lo = ((i * 2)/32);
+		hdr3->cps_hi = 0;
+		hdr3->cps_lo = 0;
+		hdr3->spb = 0;
+		hdr3->rsb = 0;
+		hdr3->pi = 0;
+		hdr3->spare = 0;
+		hdr3->dummy = 1;
+		data[4] = 0x0;
+		data[5] = 0x0;
+		data[6] = 0x2b;
+		data[7] = 0x2b;
+		pdch = &the_bts->bts_data()->trx[trx_no].pdch[ts_no];
+		pdch->rcv_block(&data[0], sizeof(data), *fn, &meas);
+	}
+	ul_tbf->create_ul_ack(*fn, ts_no);
+	memset(data, 0x2b, sizeof(data));
+	hdr3 = (struct gprs_rlc_ul_header_egprs_3 *)data;
+	hdr3->r = 0;
+	hdr3->si = 0;
+	hdr3->cv = 0;
+	hdr3->tfi_hi = (tfi >> 3) & 0x3;
+	hdr3->tfi_lo = tfi & 0x7;
+	hdr3->bsn1_hi = 0;
+	hdr3->bsn1_lo = 2;
+	hdr3->cps_hi = 0;
+	hdr3->cps_lo = 0;
+	hdr3->spb = 0;
+	hdr3->rsb = 0;
+	hdr3->pi = 0;
+	hdr3->spare = 0;
+	hdr3->dummy = 1;
+	data[4] = 0x0;
+	data[5] = 0x2b;
+	data[6] = 0x2b;
+	data[7] = 0x2b;
+
+	pdch = &the_bts->bts_data()->trx[trx_no].pdch[ts_no];
+	pdch->rcv_block(&data[0], sizeof(data), *fn, &meas);
+
+	request_dl_rlc_block(ul_tbf, fn);
+
+	check_tbf(ul_tbf);
+	OSMO_ASSERT(ul_tbf->ul_ack_state == GPRS_RLCMAC_UL_ACK_NONE);
+
+	ms = the_bts->ms_by_tlli(tlli);
+	OSMO_ASSERT(ms != NULL);
+	OSMO_ASSERT(ms->ta() == qta/4);
+	OSMO_ASSERT(ms->ul_tbf() == ul_tbf);
+
+	return ul_tbf;
+}
+
+static gprs_rlcmac_ul_tbf *establish_ul_tbf_two_phase_puan_CRBB(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, i = 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;
+
+
+	/* check the TBF */
+	ul_tbf = the_bts->ul_tbf_by_tfi(tfi, trx_no, ts_no);
+	OSMO_ASSERT(ul_tbf);
+	OSMO_ASSERT(ul_tbf->ta() == qta / 4);
+
+	/* send fake data with cv=0*/
+	struct gprs_rlc_ul_header_egprs_3 *hdr3 = NULL;
+	uint8_t data[49] = {0};
+
+	hdr3 = (struct gprs_rlc_ul_header_egprs_3 *)data;
+
+	/*header_construction */
+	memset(data, 0x2b, sizeof(data));
+
+	/* Message with CRBB */
+	for (int i = 80 ; i < 160; i++) {
+		hdr3->r = 0;
+		hdr3->si = 0;
+		hdr3->cv = 10;
+		hdr3->tfi_hi = (tfi >> 3) & 0x3;
+		hdr3->tfi_lo = tfi & 0x7;
+		hdr3->bsn1_hi = ((i)&0x1f);
+		hdr3->bsn1_lo = ((i)/32);
+		hdr3->cps_hi = 0;
+		hdr3->cps_lo = 0;
+		hdr3->spb = 0;
+		hdr3->rsb = 0;
+		hdr3->pi = 0;
+		hdr3->spare = 0;
+		hdr3->dummy = 1;
+		data[4] = 0x0;
+		data[5] = 0x0;
+		data[6] = 0x2b;
+		data[7] = 0x2b;
+		pdch = &the_bts->bts_data()->trx[trx_no].pdch[ts_no];
+		pdch->rcv_block(&data[0], sizeof(data), *fn, &meas);
+	}
+	ul_tbf->create_ul_ack(*fn, ts_no);
+	memset(data, 0x2b, sizeof(data));
+	hdr3 = (struct gprs_rlc_ul_header_egprs_3 *)data;
+	hdr3->r = 0;
+	hdr3->si = 0;
+	hdr3->cv = 0;
+	hdr3->tfi_hi = (tfi >> 3) & 0x3;
+	hdr3->tfi_lo = tfi & 0x7;
+	hdr3->bsn1_hi = 0;
+	hdr3->bsn1_lo = 2;
+	hdr3->cps_hi = 0;
+	hdr3->cps_lo = 0;
+	hdr3->spb = 0;
+	hdr3->rsb = 0;
+	hdr3->pi = 0;
+	hdr3->spare = 0;
+	hdr3->dummy = 1;
+	data[4] = 0x0;
+	data[5] = 0x2b;
+	data[6] = 0x2b;
+	data[7] = 0x2b;
+
+	pdch = &the_bts->bts_data()->trx[trx_no].pdch[ts_no];
+	pdch->rcv_block(&data[0], sizeof(data), *fn, &meas);
+
+	request_dl_rlc_block(ul_tbf, fn);
+
+	check_tbf(ul_tbf);
+	OSMO_ASSERT(ul_tbf->ul_ack_state == GPRS_RLCMAC_UL_ACK_NONE);
+
+	ms = the_bts->ms_by_tlli(tlli);
+	OSMO_ASSERT(ms != NULL);
+	OSMO_ASSERT(ms->ta() == qta/4);
+	OSMO_ASSERT(ms->ul_tbf() == ul_tbf);
+
+	return ul_tbf;
+}
 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)
@@ -1372,6 +1714,73 @@
 	printf("=== end %s ===\n", __func__);
 }
 
+static void test_tbf_egprs_two_phase_puan(void)
+{
+	BTS the_bts;
+	int ts_no = 7;
+	uint32_t fn = 2654218;
+	uint16_t qta = 31;
+	uint32_t tlli = 0xf1223344;
+	const char *imsi = "0011223344";
+	uint8_t ms_class = 1;
+	gprs_rlcmac_bts *bts;
+	uint8_t egprs_ms_class = 1;
+	gprs_rlcmac_ul_tbf *ul_tbf;
+	GprsMs *ms;
+	uint8_t test_data[256];
+
+	printf("=== start %s ===\n", __func__);
+
+	memset(test_data, 1, sizeof(test_data));
+
+	setup_bts(&the_bts, ts_no, 4);
+	the_bts.bts_data()->initial_mcs_dl = 9;
+	the_bts.bts_data()->egprs_enabled = 1;
+	bts = the_bts.bts_data();
+	bts->ws_base = 128;
+	bts->ws_pdch = 64;
+
+	ul_tbf = establish_ul_tbf(&the_bts, ts_no, tlli, &fn, qta, ms_class, egprs_ms_class);
+	/* Function to generate URBB with no length */
+	ul_tbf = establish_ul_tbf_two_phase_puan_URBB_no_length(&the_bts, ts_no, tlli, &fn,
+		qta, ms_class, egprs_ms_class, ul_tbf);
+
+	ms = ul_tbf->ms();
+	fprintf(stderr, "Got '%s', TA=%d\n", ul_tbf->name(), ul_tbf->ta());
+	fprintf(stderr,
+		"Got MS: TLLI = 0x%08x, TA = %d\n", ms->tlli(), ms->ta());
+	send_dl_data(&the_bts, tlli, imsi, test_data, sizeof(test_data));
+
+	ul_tbf->m_window.set_v_r(0);
+	ul_tbf->m_window.set_v_q(0);
+	/* Function to generate URBB with length */
+	ul_tbf = establish_ul_tbf_two_phase_puan_URBB_with_length(&the_bts, ts_no, tlli, &fn,
+		qta, ms_class, egprs_ms_class, ul_tbf);
+
+	ms = ul_tbf->ms();
+	fprintf(stderr, "Got '%s', TA=%d\n", ul_tbf->name(), ul_tbf->ta());
+	fprintf(stderr,
+		"Got MS: TLLI = 0x%08x, TA = %d\n", ms->tlli(), ms->ta());
+
+	send_dl_data(&the_bts, tlli, imsi, test_data, sizeof(test_data));
+
+	ul_tbf->m_window.set_v_r(0);
+	ul_tbf->m_window.set_v_q(0);
+	/* Function to generate CRBB */
+	bts->ws_base = 128;
+	bts->ws_pdch = 64;
+	ul_tbf = establish_ul_tbf_two_phase_puan_CRBB(&the_bts, ts_no, tlli, &fn,
+		qta, ms_class, egprs_ms_class);
+
+	ms = ul_tbf->ms();
+	fprintf(stderr, "Got '%s', TA=%d\n", ul_tbf->name(), ul_tbf->ta());
+	fprintf(stderr,
+		"Got MS: TLLI = 0x%08x, TA = %d\n", ms->tlli(), ms->ta());
+
+	send_dl_data(&the_bts, tlli, imsi, test_data, sizeof(test_data));
+
+	printf("=== end %s ===\n", __func__);
+}
 /*
  * Trigger rach for single block
  */
@@ -2899,6 +3308,7 @@
 	test_tbf_li_decoding();
 	test_tbf_epdan_out_of_rx_window();
 	test_immediate_assign_rej();
+	test_tbf_egprs_two_phase_puan();
 	test_packet_access_rej_epdan();
 
 	if (getenv("TALLOC_REPORT_FULL"))