encoding: pass RFN to write_immediate_assignment(_reject)()

Those function don't really require the full FN, hence let's pass only
the required information.
This makes the implementation here less dependent on how/if we are able to calculate
full FNs based on RFN: We get an RFN, and we have to encode so that the RFN can be
derived again, so feels less cumbersome having to go through RFN->FN->RFN which may
only cause possible issues if there's some FN timing bug.

3GPP TS 44.018 10.5.2.30 Request Reference:
"The purpose of the Request Reference information element is to provide the random
access information used in the channel request and the frame number, FN modulo 42432"

3GPP TS 44.018 10.5.2.38 Starting Time:
"The purpose of the Starting Time information element is to provide the start TDMA
frame number, FN modulo 42432."

Change-Id: If9b758434c00f2a3868534d5be84946809c989a9
diff --git a/src/bts.cpp b/src/bts.cpp
index 640405f..8be87be 100644
--- a/src/bts.cpp
+++ b/src/bts.cpp
@@ -1022,7 +1022,7 @@
 	     trx->trx_no, trx->arfcn & ~ARFCN_FLAG_MASK,
 	     pdch->ts_no, ta, pdch->tsc, tbf ? tbf->tfi() : -1, usf);
 	plen = Encoding::write_immediate_assignment(pdch, tbf, bv,
-		false, rip->ra, Fn, ta, usf, false, sb_fn,
+		false, rip->ra, rip->rfn, ta, usf, false, fn2rfn(sb_fn),
 		bts_get_ms_pwr_alpha(bts), bts->pcu->vty.gamma, -1,
 		rip->burst_type);
 	bts_do_rate_ctr_inc(bts, CTR_IMMEDIATE_ASSIGN_UL_TBF);
@@ -1038,7 +1038,7 @@
 send_imm_ass_rej:
 	LOGP(DRLCMAC, LOGL_DEBUG, "Tx Immediate Assignment Reject on AGCH\n");
 	plen = Encoding::write_immediate_assignment_reject(
-		bv, rip->ra, Fn, rip->burst_type,
+		bv, rip->ra, rip->rfn, rip->burst_type,
 		(uint8_t)osmo_tdef_get(bts->T_defs_bts, 3142, OSMO_TDEF_S, -1));
 	bts_do_rate_ctr_inc(bts, CTR_IMMEDIATE_ASSIGN_REJ);
 	if (plen >= 0)
@@ -1118,7 +1118,7 @@
 	     tbf->trx->trx_no, tbf->trx->arfcn, pdch->ts_no, tbf->ta());
 	plen = Encoding::write_immediate_assignment(pdch,
 						    tbf, immediate_assignment, true, 125,
-						    GSM_TDMA_FN_SUM(pdch->last_rts_fn, 21216),
+						    fn2rfn(GSM_TDMA_FN_SUM(pdch->last_rts_fn, 21216)),
 						    tbf->ta(), 7, false, 0,
 						    bts_get_ms_pwr_alpha(bts), bts->pcu->vty.gamma, -1,
 						    GSM_L1_BURST_TYPE_ACCESS_0);
diff --git a/src/encoding.cpp b/src/encoding.cpp
index 6b626cc..5695666 100644
--- a/src/encoding.cpp
+++ b/src/encoding.cpp
@@ -63,22 +63,22 @@
 }
 
 /* TBF_STARTING_TIME -- same as 3GPP TS 44.018 §10.5.2.38 Starting Time without tag: */
-static int write_tbf_start_time(bitvec *dest, uint32_t fn)
+static int write_tbf_start_time(bitvec *dest, uint16_t rfn)
 {
 	int rc;
 
 	/* Set values according to 3GPP TS 44.018 Table 10.5.2.38.1 */
 
 	/* T1' */
-	rc = bitvec_set_u64(dest, (fn / (26 * 51)) % 32, 5, false);
+	rc = bitvec_set_u64(dest, (rfn / (26 * 51)) % 32, 5, false);
 	CHECK(rc);
 
 	/* T3  */
-	rc = bitvec_set_u64(dest, fn % 51, 6, false);
+	rc = bitvec_set_u64(dest, rfn % 51, 6, false);
 	CHECK(rc);
 
 	/* T2  */
-	rc = bitvec_set_u64(dest, fn % 26, 5, false);
+	rc = bitvec_set_u64(dest, rfn % 26, 5, false);
 	CHECK(rc);
 
 	return rc;
@@ -180,7 +180,7 @@
 }
 
 static int write_ia_rest_downlink(const gprs_rlcmac_dl_tbf *tbf, bitvec * dest, bool polling, bool ta_valid,
-				  uint32_t fn, uint8_t alpha, uint8_t gamma, int8_t ta_idx)
+				  uint16_t rfn, uint8_t alpha, uint8_t gamma, int8_t ta_idx)
 {
 	int rc = 0;
 
@@ -213,7 +213,7 @@
 
 	if (polling) {
 		SET_1(dest);
-		rc = write_tbf_start_time(dest, fn);
+		rc = write_tbf_start_time(dest, rfn);
 		CHECK(rc);
 	} else
 		SET_0(dest);
@@ -235,7 +235,7 @@
 }
 
 /* 3GPP TS 44.018 Table 10.5.2.16.1 < Packet Uplink Assignment > -- Single Block Allocation */
-static int write_ia_rest_uplink_sba(bitvec *dest, uint32_t fn, uint8_t alpha, uint8_t gamma)
+static int write_ia_rest_uplink_sba(bitvec *dest, uint32_t rfn, uint8_t alpha, uint8_t gamma)
 {
 	int rc = 0;
 
@@ -248,7 +248,7 @@
 	SET_0(dest);
 	SET_1(dest);
 
-	rc = write_tbf_start_time(dest, fn);
+	rc = write_tbf_start_time(dest, rfn);
 	CHECK(rc);
 
 	 /* No P0 nor PR_MODE */
@@ -292,7 +292,7 @@
 	return rc;
 }
 
-static int write_ia_rest_egprs_uplink_mba(bitvec * dest, uint32_t fn, uint8_t alpha, uint8_t gamma)
+static int write_ia_rest_egprs_uplink_mba(bitvec * dest, uint32_t rfn, uint8_t alpha, uint8_t gamma)
 {
 	int rc = 0;
 
@@ -301,7 +301,7 @@
 	rc = write_alpha_gamma(dest, alpha, gamma);
 	CHECK(rc);
 
-	rc = write_tbf_start_time(dest, fn);
+	rc = write_tbf_start_time(dest, rfn);
 	CHECK(rc);
 
 	SET_0(dest); /* NUMBER OF RADIO BLOCKS ALLOCATED: */
@@ -358,7 +358,7 @@
  * see GSM 44.018, 9.1.20 + 10.5.2.30
  */
 int Encoding::write_immediate_assignment_reject(bitvec *dest, uint16_t ra,
-	uint32_t ref_fn, enum ph_burst_type burst_type, uint8_t t3142)
+	uint16_t ref_rfn, enum ph_burst_type burst_type, uint8_t t3142)
 {
 	unsigned wp = 0;
 	int plen;
@@ -394,9 +394,9 @@
 		}
 
 		bitvec_write_field(dest, &wp,
-					(ref_fn / (26 * 51)) % 32, 5); // T1'
-		bitvec_write_field(dest, &wp, ref_fn % 51, 6);          // T3
-		bitvec_write_field(dest, &wp, ref_fn % 26, 5);          // T2
+					(ref_rfn / (26 * 51)) % 32, 5); // T1'
+		bitvec_write_field(dest, &wp, ref_rfn % 51, 6);          // T3
+		bitvec_write_field(dest, &wp, ref_rfn % 26, 5);          // T2
 
 		/* 10.5.2.43 Wait Indication */
 		bitvec_write_field(dest, &wp, t3142, 8);
@@ -437,8 +437,8 @@
 	const struct gprs_rlcmac_pdch *pdch,
 	const struct gprs_rlcmac_tbf *tbf,
 	bitvec * dest, bool downlink, uint16_t ra,
-	uint32_t ref_fn, uint8_t ta,
-	uint8_t usf, bool polling, uint32_t fn, uint8_t alpha,
+	uint16_t ref_rfn, uint8_t ta,
+	uint8_t usf, bool polling, uint16_t rfn, uint8_t alpha,
 	uint8_t gamma, int8_t ta_idx, enum ph_burst_type burst_type)
 {
 	unsigned wp = 0;
@@ -480,9 +480,9 @@
 		bitvec_write_field(dest, &wp, ra, 8);	/* RACH value */
 	}
 
-	bitvec_write_field(dest, &wp, (ref_fn / (26 * 51)) % 32, 5); // T1'
-	bitvec_write_field(dest, &wp, ref_fn % 51, 6);               // T3
-	bitvec_write_field(dest, &wp, ref_fn % 26, 5);               // T2
+	bitvec_write_field(dest, &wp, (ref_rfn / (26 * 51)) % 32, 5); // T1'
+	bitvec_write_field(dest, &wp, ref_rfn % 51, 6);               // T3
+	bitvec_write_field(dest, &wp, ref_rfn % 26, 5);               // T2
 
 	// 10.5.2.40 Timing Advance
 	bitvec_write_field(dest, &wp, 0x0, 2); // spare
@@ -508,7 +508,7 @@
 	if (downlink) {
 		OSMO_ASSERT(tbf_as_dl_tbf_const(tbf) != NULL);
 
-		rc = write_ia_rest_downlink(tbf_as_dl_tbf_const(tbf), dest, polling, gsm48_ta_is_valid(ta), fn, alpha, gamma,
+		rc = write_ia_rest_downlink(tbf_as_dl_tbf_const(tbf), dest, polling, gsm48_ta_is_valid(ta), rfn, alpha, gamma,
 					    ta_idx);
 	} else if (((burst_type == GSM_L1_BURST_TYPE_ACCESS_1) || (burst_type == GSM_L1_BURST_TYPE_ACCESS_2))) {
 		SET_L(dest); SET_H(dest); // "LH"
@@ -521,7 +521,7 @@
 		if (tbf_as_ul_tbf_const(tbf) != NULL)
 			rc = write_ia_rest_egprs_uplink_sba(tbf_as_ul_tbf_const(tbf), dest, usf, alpha, gamma, ta_idx);
 		else
-			rc = write_ia_rest_egprs_uplink_mba(dest, fn, alpha, gamma);
+			rc = write_ia_rest_egprs_uplink_mba(dest, rfn, alpha, gamma);
 	} else {
 		OSMO_ASSERT(!tbf || !tbf->is_egprs_enabled());
 
@@ -531,7 +531,7 @@
 		if (tbf_as_ul_tbf_const(tbf) != NULL)
 			rc = write_ia_rest_uplink_mba(tbf_as_ul_tbf_const(tbf), dest, usf, alpha, gamma, ta_idx);
 		else
-			rc = write_ia_rest_uplink_sba(dest, fn, alpha, gamma);
+			rc = write_ia_rest_uplink_sba(dest, rfn, alpha, gamma);
 	}
 
 	if (rc < 0) {
diff --git a/src/encoding.h b/src/encoding.h
index ae4d57f..89f6964 100644
--- a/src/encoding.h
+++ b/src/encoding.h
@@ -46,15 +46,15 @@
 			const struct gprs_rlcmac_pdch *pdch,
 			const struct gprs_rlcmac_tbf *tbf,
 			bitvec * dest, bool downlink, uint16_t ra,
-			uint32_t ref_fn, uint8_t ta,
+			uint16_t ref_rfn, uint8_t ta,
 			uint8_t usf, bool polling,
-			uint32_t fn, uint8_t alpha, uint8_t gamma,
+			uint16_t rfn, uint8_t alpha, uint8_t gamma,
 			int8_t ta_idx,
 			enum ph_burst_type burst_type);
 
 	static int write_immediate_assignment_reject(
 			bitvec *dest, uint16_t ra,
-			uint32_t ref_fn,
+			uint16_t ref_rfn,
 			enum ph_burst_type burst_type,
 			uint8_t t3142
 		);
diff --git a/tests/types/TypesTest.cpp b/tests/types/TypesTest.cpp
index 871e0d1..c6a2fbf 100644
--- a/tests/types/TypesTest.cpp
+++ b/tests/types/TypesTest.cpp
@@ -753,7 +753,7 @@
 	struct msgb *m = msgb_alloc(80, "test");
 	bool poll = true;
 	uint16_t ra = 13;
-	uint32_t ref_fn = 24, fn = 11;
+	uint32_t ref_rfn = 24, rfn = 11;
 	int8_t ta_idx = 0;
 
 	/* HACK: tbf can be NULL, so we cannot use tbf->trx here */
@@ -766,8 +766,8 @@
 	bitvec_unhex(immediate_assignment, DUMMY_VEC);
 	plen = Encoding::write_immediate_assignment(&trx.pdch[5], tbf,
 						    immediate_assignment,
-						    dl, ra, ref_fn, ta, usf,
-						    poll, fn, alpha, gamma, ta_idx, bt);
+						    dl, ra, ref_rfn, ta, usf,
+						    poll, rfn, alpha, gamma, ta_idx, bt);
 	printf("[%u] %s Immediate Assignment <%s>:\n\t%s\n", plen, dl ? "DL" : "UL", kind,
 	       osmo_hexdump(immediate_assignment->data, sz));