Replace tbf->establish_dl_tbf_on_pacch() refactoring GprsMs functions

ms_new_dl_tbf_assignment() is split into 2 functions, one to
allocate+assign on PACCH and another one for PCH.
This makes a lot clearer the aim of each caller of the function.
Once this is done, it becomes obvious tbf->establish_dl_tbf_on_pacch()
is basically doing the same as ms_new_dl_tbf_assigned_on_pacch() so drop
it.

Change-Id: I610210e3120c962d91ce8ff94c66161e086761ba
diff --git a/src/gprs_ms.c b/src/gprs_ms.c
index 37071e4..90e8289 100644
--- a/src/gprs_ms.c
+++ b/src/gprs_ms.c
@@ -1056,39 +1056,46 @@
 
 }
 
-/* This should be called only when MS is reachable, see ms_is_reachable_for_dl_ass(). */
-int ms_new_dl_tbf_assignment(struct GprsMs *ms)
+/* A new DL-TBF is allocated and assigned through PACCH using "tbf".
+ * "tbf" may be either a UL-TBF or a DL-TBF.
+ * Note: This should be called only when MS is reachable, see ms_is_reachable_for_dl_ass().
+ */
+int ms_new_dl_tbf_assigned_on_pacch(struct GprsMs *ms, struct gprs_rlcmac_tbf *tbf)
 {
-	bool ss;
-	int8_t use_trx;
-	struct gprs_rlcmac_ul_tbf *ul_tbf;
+	OSMO_ASSERT(tbf);
+	const int8_t trx_no = tbf_get_trx(tbf)->trx_no;
+	const bool single_slot = false;
 	struct gprs_rlcmac_dl_tbf *dl_tbf;
 
-	ul_tbf = ms_ul_tbf(ms);
-
-	if (ul_tbf) {
-		use_trx = tbf_get_trx((struct gprs_rlcmac_tbf *)ul_tbf)->trx_no;
-		ss = false;
-	} else {
-		use_trx = -1;
-		ss = true; /* PCH assignment only allows one timeslot */
+	dl_tbf = dl_tbf_alloc(ms->bts, ms, trx_no, single_slot);
+	if (!dl_tbf) {
+		LOGPMS(ms, DTBF, LOGL_NOTICE, "No PDCH resource\n");
+		return -EBUSY;
 	}
 
-	/* set number of downlink slots according to multislot class */
-	dl_tbf = dl_tbf_alloc(ms->bts, ms, use_trx, ss);
+	LOGPTBFDL(dl_tbf, LOGL_DEBUG, "[DOWNLINK] START (PACCH)\n");
+	dl_tbf_trigger_ass_on_pacch(dl_tbf, tbf);
+	return 0;
+}
+
+/* A new DL-TBF is allocated and assigned through PCH.
+ * Note: This should be called only when MS is reachable, see ms_is_reachable_for_dl_ass().
+ */
+int ms_new_dl_tbf_assigned_on_pch(struct GprsMs *ms)
+{
+	const int8_t trx_no = -1;
+	const bool single_slot = true;
+	struct gprs_rlcmac_dl_tbf *dl_tbf;
+
+	dl_tbf = dl_tbf_alloc(ms->bts, ms, trx_no, single_slot);
 
 	if (!dl_tbf) {
 		LOGPMS(ms, DTBF, LOGL_NOTICE, "No PDCH resource\n");
 		return -EBUSY;
 	}
 
-	LOGPTBFDL(dl_tbf, LOGL_DEBUG, "[DOWNLINK] START\n");
-
-	/* Trigger the assignment now. */
-	if (ul_tbf)
-		dl_tbf_trigger_ass_on_pacch(dl_tbf, ul_tbf_as_tbf(ul_tbf));
-	else
-		dl_tbf_trigger_ass_on_pch(dl_tbf);
+	LOGPTBFDL(dl_tbf, LOGL_DEBUG, "[DOWNLINK] START (PCH)\n");
+	dl_tbf_trigger_ass_on_pch(dl_tbf);
 	return 0;
 }
 
@@ -1113,14 +1120,18 @@
 	if (dl_tbf) {
 		if (tbf_state(dl_tbf_as_tbf_const(dl_tbf)) == TBF_ST_WAIT_RELEASE) {
 			LOGPTBFDL(dl_tbf, LOGL_DEBUG, "in WAIT RELEASE state (T3193), so reuse TBF\n");
-			tbf_establish_dl_tbf_on_pacch(dl_tbf_as_tbf(dl_tbf));
+			rc = ms_new_dl_tbf_assigned_on_pacch(ms, dl_tbf_as_tbf(dl_tbf));
 		}
 	} else {
 		/* Check if we can create a DL TBF to start sending the enqueued
 		 * data. Otherwise it will be triggered later when it is reachable
 		* again. */
-		if (ms_is_reachable_for_dl_ass(ms))
-			rc = ms_new_dl_tbf_assignment(ms);
+		if (ms_is_reachable_for_dl_ass(ms)) {
+			if (ms_ul_tbf(ms))
+				rc = ms_new_dl_tbf_assigned_on_pacch(ms, ul_tbf_as_tbf(ms_ul_tbf(ms)));
+			else
+				rc = ms_new_dl_tbf_assigned_on_pch(ms);
+		}
 	}
 	return rc;
 }
diff --git a/src/gprs_ms.h b/src/gprs_ms.h
index b9a1386..8014f26 100644
--- a/src/gprs_ms.h
+++ b/src/gprs_ms.h
@@ -144,7 +144,8 @@
 bool ms_nacc_rts(const struct GprsMs *ms);
 struct msgb *ms_nacc_create_rlcmac_msg(struct GprsMs *ms, struct gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t ts);
 
-int ms_new_dl_tbf_assignment(struct GprsMs *ms);
+int ms_new_dl_tbf_assigned_on_pacch(struct GprsMs *ms, struct gprs_rlcmac_tbf *tbf);
+int ms_new_dl_tbf_assigned_on_pch(struct GprsMs *ms);
 int ms_append_llc_dl_data(struct GprsMs *ms, uint16_t pdu_delay_csec, const uint8_t *data, uint16_t len);
 
 static inline bool ms_is_idle(const struct GprsMs *ms)
diff --git a/src/pdch.cpp b/src/pdch.cpp
index 1df8db8..ad521e3 100644
--- a/src/pdch.cpp
+++ b/src/pdch.cpp
@@ -421,7 +421,7 @@
 		 * TBF might have been released while the UL TBF has been
 		 * established */
 		if (ms_need_dl_tbf(new_tbf->ms()))
-			new_tbf->establish_dl_tbf_on_pacch();
+			ms_new_dl_tbf_assigned_on_pacch(new_tbf->ms(), new_tbf);
 		return;
 
 	case PDCH_ULC_POLL_DL_ASS:
diff --git a/src/tbf.cpp b/src/tbf.cpp
index 4bf4ce8..d855083 100644
--- a/src/tbf.cpp
+++ b/src/tbf.cpp
@@ -688,26 +688,6 @@
 	return 0;
 }
 
-int gprs_rlcmac_tbf::establish_dl_tbf_on_pacch()
-{
-	struct gprs_rlcmac_dl_tbf *new_tbf = NULL;
-
-	bts_do_rate_ctr_inc(bts, CTR_TBF_REUSED);
-
-	new_tbf = dl_tbf_alloc(bts, ms(),
-		this->trx->trx_no, false);
-
-	if (!new_tbf) {
-		LOGP(DTBF, LOGL_NOTICE, "No PDCH resource\n");
-		return -1;
-	}
-
-	LOGPTBF(this, LOGL_DEBUG, "Trigger downlink assignment on PACCH\n");
-	dl_tbf_trigger_ass_on_pacch(new_tbf, this);
-
-	return 0;
-}
-
 const char *tbf_name(const gprs_rlcmac_tbf *tbf)
 {
 	return tbf ? tbf->name() : "(no TBF)";
@@ -951,11 +931,6 @@
 	return buf;
 }
 
-int tbf_establish_dl_tbf_on_pacch(struct gprs_rlcmac_tbf *tbf)
-{
-	return tbf->establish_dl_tbf_on_pacch();
-}
-
 struct gprs_rlcmac_trx *tbf_get_trx(struct gprs_rlcmac_tbf *tbf)
 {
 	return tbf->trx;
diff --git a/src/tbf.h b/src/tbf.h
index 09de188..65b131e 100644
--- a/src/tbf.h
+++ b/src/tbf.h
@@ -148,7 +148,6 @@
 bool tbf_is_control_ts(const struct gprs_rlcmac_tbf *tbf, uint8_t ts);
 bool tbf_can_upgrade_to_multislot(const struct gprs_rlcmac_tbf *tbf);
 int tbf_update(struct gprs_rlcmac_tbf *tbf);
-int tbf_establish_dl_tbf_on_pacch(struct gprs_rlcmac_tbf *tbf);
 struct gprs_rlcmac_trx *tbf_get_trx(struct gprs_rlcmac_tbf *tbf);
 void tbf_stop_timers(struct gprs_rlcmac_tbf *tbf, const char *reason);
 #ifdef __cplusplus
@@ -190,7 +189,6 @@
 	void t_stop(enum tbf_timers t, const char *reason);
 	void t_start(enum tbf_timers t, int T, const char *reason, bool force,
 		     const char *file, unsigned line);
-	int establish_dl_tbf_on_pacch();
 
 	int check_polling(uint32_t fn, uint8_t ts,
 		uint32_t *poll_fn, unsigned int *rrbp) const;
diff --git a/src/tbf_dl.cpp b/src/tbf_dl.cpp
index 0e6b922..c63af58 100644
--- a/src/tbf_dl.cpp
+++ b/src/tbf_dl.cpp
@@ -973,6 +973,7 @@
 int gprs_rlcmac_dl_tbf::rcvd_dl_final_ack()
 {
 	uint16_t received;
+	int rc = 0;
 
 	osmo_fsm_inst_dispatch(this->state_fsm.fi, TBF_EV_FINAL_ACK_RECVD, NULL);
 
@@ -986,9 +987,9 @@
 	/* check for LLC PDU in the LLC Queue */
 	if (llc_queue_size(llc_queue()) > 0)
 		/* we have more data so we will re-use this tbf */
-		establish_dl_tbf_on_pacch();
+		rc = ms_new_dl_tbf_assigned_on_pacch(ms(), dl_tbf_as_tbf(this));
 
-	return 0;
+	return rc;
 }
 
 int gprs_rlcmac_dl_tbf::rcvd_dl_ack(bool final_ack, unsigned first_bsn,
diff --git a/src/tbf_fsm.c b/src/tbf_fsm.c
index eeccd6b..f019214 100644
--- a/src/tbf_fsm.c
+++ b/src/tbf_fsm.c
@@ -276,7 +276,7 @@
 		 * back to packet-idle mode then we can assign the DL TBF on PCH
 		 * now. */
 		if (!new_ul_tbf_requested && ms_need_dl_tbf(ms))
-			ms_new_dl_tbf_assignment(ms);
+			ms_new_dl_tbf_assigned_on_pch(ms);
 		ms_unref(ms);
 		break;
 	case TBF_EV_MAX_N3103: