Move tbf ul_ack_state to osmocom FSM

Related: OS#2709
Change-Id: Icf23bf5a4b85fbcbf1542cebceb76b9ba7185d30
diff --git a/src/Makefile.am b/src/Makefile.am
index 1a2ea97..08b3309 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -60,6 +60,7 @@
 	tbf.cpp \
 	tbf_fsm.c \
 	tbf_ul.cpp \
+	tbf_ul_ack_fsm.c \
 	tbf_ul_ass_fsm.c \
 	tbf_dl.cpp \
 	tbf_dl_ass_fsm.c \
@@ -102,6 +103,7 @@
 	tbf.h \
 	tbf_fsm.h \
 	tbf_ul.h \
+	tbf_ul_ack_fsm.h \
 	tbf_ul_ass_fsm.h \
 	tbf_dl.h \
 	tbf_dl_ass_fsm.h \
diff --git a/src/encoding.cpp b/src/encoding.cpp
index f945161..30b0ee1 100644
--- a/src/encoding.cpp
+++ b/src/encoding.cpp
@@ -1106,9 +1106,8 @@
 	bitvec_write_field(dest, &wp, 0, 1); // 0: don't have REL 5
 };
 
-void Encoding::write_packet_uplink_ack(
-	bitvec * dest, struct gprs_rlcmac_ul_tbf *tbf, bool is_final,
-	uint8_t rrbp)
+void write_packet_uplink_ack(struct bitvec *dest, struct gprs_rlcmac_ul_tbf *tbf,
+			     bool is_final, uint8_t rrbp)
 {
 	unsigned wp = 0;
 
diff --git a/src/encoding.h b/src/encoding.h
index 298a937..998e69a 100644
--- a/src/encoding.h
+++ b/src/encoding.h
@@ -65,10 +65,6 @@
 
 	static void encode_rbb(const char *show_rbb, bitvec *rbb);
 
-	static void write_packet_uplink_ack(
-			bitvec * dest, struct gprs_rlcmac_ul_tbf *tbf, bool is_final,
-			uint8_t rrbp);
-
 	static int write_paging_request(bitvec * dest, const struct osmo_mobile_identity *mi);
 
 	static unsigned write_repeated_page_info(bitvec * dest, unsigned& wp, uint8_t len,
@@ -119,6 +115,9 @@
 				      int8_t ta_idx, uint8_t ta_ts, bool use_egprs,
 				      uint8_t control_ack);
 
+void write_packet_uplink_ack(struct bitvec *dest, struct gprs_rlcmac_ul_tbf *tbf,
+			     bool is_final, uint8_t rrbp);
+
 void write_packet_neighbour_cell_data(RlcMacDownlink_t *block,
 		bool tfi_is_dl, uint8_t tfi, uint8_t container_id,
 		uint8_t container_idx, PNCDContainer_t *container);
diff --git a/src/gprs_rlcmac_sched.cpp b/src/gprs_rlcmac_sched.cpp
index ed81981..eb163a7 100644
--- a/src/gprs_rlcmac_sched.cpp
+++ b/src/gprs_rlcmac_sched.cpp
@@ -54,7 +54,7 @@
 		/* this trx, this ts */
 		if (!ul_tbf->is_control_ts(pdch->ts_no))
 			continue;
-		if (ul_tbf->ul_ack_state_is(GPRS_RLCMAC_UL_ACK_SEND_ACK))
+		if (tbf_ul_ack_rts(ul_tbf))
 			tbf_cand->ul_ack = ul_tbf;
 		if (tbf_dl_ass_rts(ul_tbf))
 			tbf_cand->dl_ass = ul_tbf;
@@ -173,7 +173,7 @@
 			else if (tbf == tbfs->dl_ass && tbf->direction == GPRS_RLCMAC_UL_TBF)
 				msg = tbf_dl_ass_create_rlcmac_msg(tbfs->dl_ass, fn, ts);
 			else if (tbf == tbfs->ul_ack)
-				msg = tbfs->ul_ack->create_ul_ack(fn, ts);
+				msg = tbf_ul_ack_create_rlcmac_msg(tbfs->ul_ack, fn, ts);
 			else if (tbf == tbfs->nacc) {
 				msg = ms_nacc_create_rlcmac_msg(tbf->ms(), tbf, fn, ts);
 			}
diff --git a/src/pdch.cpp b/src/pdch.cpp
index 439759f..da43bdf 100644
--- a/src/pdch.cpp
+++ b/src/pdch.cpp
@@ -344,11 +344,10 @@
 
 	/* check if this control ack belongs to packet uplink ack */
 	ul_tbf = as_ul_tbf(tbf);
-	if (ul_tbf && ul_tbf->handle_ctrl_ack(reason)) {
+	if (ul_tbf && reason == PDCH_ULC_POLL_UL_ACK && tbf_ul_ack_exp_ctrl_ack(ul_tbf, fn, ts_no)) {
+		osmo_fsm_inst_dispatch(ul_tbf->ul_ack_fsm.fi, TBF_UL_ACK_EV_RX_CTRL_ACK, NULL);
+		/* We can free since we only set polling on final UL ACK/NACK */
 		LOGPTBF(tbf, LOGL_DEBUG, "[UPLINK] END\n");
-		if (ul_tbf->ctrl_ack_to_toggle())
-			LOGPTBF(tbf, LOGL_NOTICE, "Recovered uplink ack for UL\n");
-
 		tbf_free(tbf);
 		return;
 	}
diff --git a/src/tbf.cpp b/src/tbf.cpp
index 5efbe7f..b43ffde 100644
--- a/src/tbf.cpp
+++ b/src/tbf.cpp
@@ -59,13 +59,6 @@
 
 unsigned int next_tbf_ctr_group_id = 0; /* Incrementing group id */
 
-const struct value_string gprs_rlcmac_tbf_ul_ack_state_names[] = {
-	OSMO_VALUE_STRING(GPRS_RLCMAC_UL_ACK_NONE),
-	OSMO_VALUE_STRING(GPRS_RLCMAC_UL_ACK_SEND_ACK), /* send acknowledge on next RTS */
-	OSMO_VALUE_STRING(GPRS_RLCMAC_UL_ACK_WAIT_ACK), /* wait for PACKET CONTROL ACK */
-	{ 0, NULL }
-};
-
 static const struct value_string tbf_counters_names[] = {
 	OSMO_VALUE_STRING(N3101),
 	OSMO_VALUE_STRING(N3103),
@@ -113,7 +106,6 @@
 	m_created_ts(0),
 	m_ctrs(NULL),
 	m_ms(ms),
-	ul_ack_state(GPRS_RLCMAC_UL_ACK_NONE),
 	m_egprs_enabled(false)
 {
 	/* The classes of these members do not have proper constructors yet.
@@ -568,8 +560,6 @@
 			  chan, new_poll_fn, ts);
 		break;
 	case PDCH_ULC_POLL_UL_ACK:
-		ul_ack_state = GPRS_RLCMAC_UL_ACK_WAIT_ACK;
-
 		LOGPTBFUL(this, LOGL_DEBUG, "Scheduled UL Acknowledgement polling on %s (FN=%d, TS=%d)\n",
 			  chan, new_poll_fn, ts);
 		break;
@@ -591,12 +581,7 @@
 	LOGPTBF(this, LOGL_NOTICE, "poll timeout for FN=%d, TS=%d (curr FN %d)\n",
 		poll_fn, pdch->ts_no, bts_current_frame_number(bts));
 
-	if (ul_tbf && ul_tbf->handle_ctrl_ack(reason)) {
-		if (!ul_tbf->ctrl_ack_to_toggle()) {
-			LOGPTBF(this, LOGL_NOTICE,
-				"Timeout for polling PACKET CONTROL ACK for PACKET UPLINK ACK: %s\n",
-				tbf_rlcmac_diag(this));
-		}
+	if (ul_tbf && reason == PDCH_ULC_POLL_UL_ACK && tbf_ul_ack_exp_ctrl_ack(ul_tbf, poll_fn, pdch->ts_no)) {
 		bts_do_rate_ctr_inc(bts, CTR_RLC_ACK_TIMEDOUT);
 		bts_do_rate_ctr_inc(bts, CTR_PUAN_POLL_TIMEDOUT);
 		if (state_is(TBF_ST_FINISHED)) {
@@ -605,10 +590,8 @@
 				osmo_fsm_inst_dispatch(this->state_fsm.fi, TBF_EV_MAX_N3103, NULL);
 				return;
 			}
-			/* reschedule UL ack */
-			ul_tbf->ul_ack_state = GPRS_RLCMAC_UL_ACK_SEND_ACK;
 		}
-
+		osmo_fsm_inst_dispatch(ul_tbf->ul_ack_fsm.fi, TBF_UL_ACK_EV_POLL_TIMEOUT, NULL);
 	} else if (ul_ass_state_is(TBF_UL_ASS_WAIT_ACK)) {
 		bts_do_rate_ctr_inc(bts, CTR_RLC_ASS_TIMEDOUT);
 		bts_do_rate_ctr_inc(bts, CTR_PUA_POLL_TIMEDOUT);
@@ -761,6 +744,12 @@
 	osmo_fsm_inst_update_id(tbf->state_fsm.fi, buf);
 	osmo_fsm_inst_update_id(tbf->ul_ass_fsm.fi, buf);
 	osmo_fsm_inst_update_id(tbf->dl_ass_fsm.fi, buf);
+
+	if (tbf_direction(tbf) == GPRS_RLCMAC_UL_TBF) {
+		struct gprs_rlcmac_ul_tbf *ul_tbf = as_ul_tbf(tbf);
+		osmo_fsm_inst_update_id(ul_tbf->ul_ack_fsm.fi, buf);
+	}
+
 }
 
 void gprs_rlcmac_tbf::rotate_in_list()
diff --git a/src/tbf.h b/src/tbf.h
index 358a7ad..ffd6cc2 100644
--- a/src/tbf.h
+++ b/src/tbf.h
@@ -58,14 +58,6 @@
  * TBF instance
  */
 
-enum gprs_rlcmac_tbf_ul_ack_state {
-	GPRS_RLCMAC_UL_ACK_NONE = 0,
-	GPRS_RLCMAC_UL_ACK_SEND_ACK, /* send acknowledge on next RTS */
-	GPRS_RLCMAC_UL_ACK_WAIT_ACK, /* wait for PACKET CONTROL ACK */
-};
-
-extern const struct value_string gprs_rlcmac_tbf_ul_ack_state_names[];
-
 enum gprs_rlcmac_tbf_direction {
 	GPRS_RLCMAC_DL_TBF,
 	GPRS_RLCMAC_UL_TBF
@@ -125,12 +117,10 @@
 #define GPRS_RLCMAC_FLAG_CCCH		0 /* assignment on CCCH */
 #define GPRS_RLCMAC_FLAG_PACCH		1 /* assignment on PACCH */
 #define GPRS_RLCMAC_FLAG_DL_ACK		2 /* DL TBF: At least one DL ACK/NACK was recieved since it was assigned */
-#define GPRS_RLCMAC_FLAG_TO_UL_ACK	3 /* UL TBF: Failed to receive last polled CTRL ACK confirming our UL ACK/NACK */
-#define GPRS_RLCMAC_FLAG_TO_DL_ACK	4 /* DL TBF: Failed to receive last polled DL ACK/NACK */
+#define GPRS_RLCMAC_FLAG_TO_DL_ACK	3 /* DL TBF: Failed to receive last polled DL ACK/NACK */
 #define GPRS_RLCMAC_FLAG_TO_MASK	0xf0 /* timeout bits */
 
 #define T_START(tbf, t, T, r, f) tbf->t_start(t, T, r, f, __FILE__, __LINE__)
-#define TBF_SET_ACK_STATE(t, st) do { t->set_ack_state(st, __FILE__, __LINE__); } while(0)
 
 #ifdef __cplusplus
 extern "C" {
@@ -180,8 +170,6 @@
 	bool state_is_not(enum tbf_fsm_states rhs) const;
 	bool dl_ass_state_is(enum tbf_dl_ass_fsm_states rhs) const;
 	bool ul_ass_state_is(enum tbf_ul_ass_fsm_states rhs) const;
-	bool ul_ack_state_is(enum gprs_rlcmac_tbf_ul_ack_state rhs) const;
-	void set_ack_state(enum gprs_rlcmac_tbf_ul_ack_state new_state, const char *file, int line);
 	void poll_sched_set(const char *file, int line);
 	void poll_sched_unset(const char *file, int line);
 	bool check_n_clear(uint8_t state_flag);
@@ -293,7 +281,6 @@
 	struct GprsMs *m_ms;
 private:
 	void enable_egprs();
-	enum gprs_rlcmac_tbf_ul_ack_state ul_ack_state;
 	bool m_egprs_enabled;
 	struct osmo_timer_list Tarr[T_MAX];
 	uint8_t Narr[N_MAX];
@@ -315,11 +302,6 @@
 	return tbf_ul_ass_fi(this)->state == rhs;
 }
 
-inline bool gprs_rlcmac_tbf::ul_ack_state_is(enum gprs_rlcmac_tbf_ul_ack_state rhs) const
-{
-	return ul_ack_state == rhs;
-}
-
 inline bool gprs_rlcmac_tbf::state_is_not(enum tbf_fsm_states rhs) const
 {
 	return tbf_state(this) != rhs;
@@ -331,15 +313,6 @@
 	return osmo_fsm_inst_state_name(state_fsm.fi);
 }
 
-inline void gprs_rlcmac_tbf::set_ack_state(enum gprs_rlcmac_tbf_ul_ack_state new_state, const char *file, int line)
-{
-	LOGPSRC(DTBF, LOGL_DEBUG, file, line, "%s changes UL ACK state from %s to %s\n",
-		tbf_name(this),
-		get_value_string(gprs_rlcmac_tbf_ul_ack_state_names, ul_ack_state),
-		get_value_string(gprs_rlcmac_tbf_ul_ack_state_names, new_state));
-	ul_ack_state = new_state;
-}
-
 inline bool gprs_rlcmac_tbf::check_n_clear(uint8_t state_flag)
 {
 	if ((state_fsm.state_flags & (1 << state_flag))) {
diff --git a/src/tbf_dl.cpp b/src/tbf_dl.cpp
index 34c5630..905bf05 100644
--- a/src/tbf_dl.cpp
+++ b/src/tbf_dl.cpp
@@ -249,7 +249,7 @@
 	* or MULTIPLE TBF DOWNLINK ASSIGNMENT messages before contention resolution is
 	* completed on the mobile station side." */
 	if (ul_tbf && ul_tbf->m_contention_resolution_done
-	 && !ul_tbf->m_final_ack_sent) {
+	 && !tbf_ul_ack_waiting_cnf_final_ack(ul_tbf)) {
 		use_trx = ul_tbf->trx->trx_no;
 		ss = false;
 		old_ul_tbf = ul_tbf;
diff --git a/src/tbf_ul.cpp b/src/tbf_ul.cpp
index c84f373..d4f2093 100644
--- a/src/tbf_ul.cpp
+++ b/src/tbf_ul.cpp
@@ -93,6 +93,9 @@
 
 gprs_rlcmac_ul_tbf::~gprs_rlcmac_ul_tbf()
 {
+	osmo_fsm_inst_free(ul_ack_fsm.fi);
+	ul_ack_fsm.fi = NULL;
+
 	rate_ctr_group_free(m_ul_egprs_ctrs);
 	rate_ctr_group_free(m_ul_gprs_ctrs);
 	/* ~gprs_rlcmac_tbf() is called automatically upon return */
@@ -231,11 +234,15 @@
 	gprs_rlcmac_tbf(bts_, ms, GPRS_RLCMAC_UL_TBF),
 	m_rx_counter(0),
 	m_contention_resolution_done(0),
-	m_final_ack_sent(0),
 	m_ul_gprs_ctrs(NULL),
 	m_ul_egprs_ctrs(NULL)
 {
 	memset(&m_usf, USF_INVALID, sizeof(m_usf));
+
+	memset(&ul_ack_fsm, 0, sizeof(ul_ack_fsm));
+	ul_ack_fsm.tbf = this;
+	ul_ack_fsm.fi = osmo_fsm_inst_alloc(&tbf_ul_ack_fsm, this, &ul_ack_fsm, LOGL_INFO, NULL);
+
 }
 
 /*
@@ -288,26 +295,6 @@
 	return 0;
 }
 
-bool gprs_rlcmac_ul_tbf::ctrl_ack_to_toggle()
-{
-	if (check_n_clear(GPRS_RLCMAC_FLAG_TO_UL_ACK))
-		return true; /* GPRS_RLCMAC_FLAG_TO_UL_ACK was set, now cleared */
-
-	state_fsm.state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_UL_ACK);
-	return false; /* GPRS_RLCMAC_FLAG_TO_UL_ACK was unset, now set */
-}
-
-bool gprs_rlcmac_ul_tbf::handle_ctrl_ack(enum pdch_ulc_tbf_poll_reason reason)
-{
-	/* check if this control ack belongs to packet uplink ack */
-	if (reason == PDCH_ULC_POLL_UL_ACK && ul_ack_state_is(GPRS_RLCMAC_UL_ACK_WAIT_ACK)) {
-		TBF_SET_ACK_STATE(this, GPRS_RLCMAC_UL_ACK_NONE);
-		return true;
-	}
-
-	return false;
-}
-
 void gprs_rlcmac_ul_tbf::contention_resolution_start()
 {
 	/* 3GPP TS 44.018 sec 11.1.2 Timers on the network side: "This timer is
@@ -339,59 +326,6 @@
 	m_contention_resolution_done = 1;
 }
 
-struct msgb *gprs_rlcmac_ul_tbf::create_ul_ack(uint32_t fn, uint8_t ts)
-{
-	int final = (state_is(TBF_ST_FINISHED));
-	struct msgb *msg;
-	int rc;
-	unsigned int rrbp = 0;
-	uint32_t new_poll_fn = 0;
-
-	if (final) {
-		if (ul_ack_state_is(GPRS_RLCMAC_UL_ACK_WAIT_ACK)) {
-			LOGPTBFUL(this, LOGL_DEBUG,
-				  "Polling is already scheduled, so we must wait for the final uplink ack...\n");
-			return NULL;
-		}
-
-		rc = check_polling(fn, ts, &new_poll_fn, &rrbp);
-		if (rc < 0)
-			return NULL;
-	}
-
-	msg = msgb_alloc(23, "rlcmac_ul_ack");
-	if (!msg)
-		return NULL;
-	bitvec *ack_vec = bitvec_alloc(23, tall_pcu_ctx);
-	if (!ack_vec) {
-		msgb_free(msg);
-		return NULL;
-	}
-	bitvec_unhex(ack_vec, DUMMY_VEC);
-	Encoding::write_packet_uplink_ack(ack_vec, this, final, rrbp);
-	bitvec_pack(ack_vec, msgb_put(msg, 23));
-	bitvec_free(ack_vec);
-
-	/* TS 44.060 7a.2.1.1: "The contention resolution is completed on
-	 * the network side when the network receives an RLC data block that
-	 * comprises the TLLI value that identifies the mobile station and the
-	 * TFI value associated with the TBF."
-	 * However, it's handier for us to mark contention resolution success
-	 * here since according to spec upon rx UL ACK is the time at which MS
-	 * realizes contention resolution succeeds. */
-	if (is_tlli_valid())
-		contention_resolution_success();
-
-	if (final) {
-		set_polling(new_poll_fn, ts, PDCH_ULC_POLL_UL_ACK);
-		/* waiting for final acknowledge */
-		m_final_ack_sent = 1;
-	} else
-		TBF_SET_ACK_STATE(this, GPRS_RLCMAC_UL_ACK_NONE);
-
-	return msg;
-}
-
 /*! \brief receive data from PDCH/L1 */
 int gprs_rlcmac_ul_tbf::rcv_data_block_acknowledged(
 	const struct gprs_rlc_data_info *rlc,
@@ -590,14 +524,7 @@
 	if (!require_ack)
 		return;
 
-	if (ul_ack_state_is(GPRS_RLCMAC_UL_ACK_NONE)) {
-		/* trigger sending at next RTS */
-		TBF_SET_ACK_STATE(this, GPRS_RLCMAC_UL_ACK_SEND_ACK);
-	} else {
-		/* already triggered */
-		LOGPTBFUL(this, LOGL_DEBUG,
-			  "Sending Ack/Nack already scheduled, no need to re-schedule\n");
-	}
+	osmo_fsm_inst_dispatch(this->ul_ack_fsm.fi, TBF_UL_ACK_EV_SCHED_ACK, NULL);
 }
 
 /* Send Uplink unit-data to SGSN. */
@@ -843,3 +770,13 @@
 {
 	return tbf->m_contention_resolution_done;
 }
+
+struct osmo_fsm_inst *tbf_ul_ack_fi(const struct gprs_rlcmac_ul_tbf *tbf)
+{
+	return tbf->ul_ack_fsm.fi;
+}
+
+void ul_tbf_contention_resolution_success(struct gprs_rlcmac_ul_tbf *tbf)
+{
+	return tbf->contention_resolution_success();
+}
diff --git a/src/tbf_ul.h b/src/tbf_ul.h
index 826062f..defdfba 100644
--- a/src/tbf_ul.h
+++ b/src/tbf_ul.h
@@ -24,6 +24,15 @@
 #include <stdbool.h>
 
 #include "tbf.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#include <tbf_ul_ack_fsm.h>
+#ifdef __cplusplus
+}
+#endif
+
 /*
  * TBF instance
  */
@@ -54,9 +63,6 @@
 	gprs_rlcmac_ul_tbf(struct gprs_rlcmac_bts *bts, GprsMs *ms);
 	~gprs_rlcmac_ul_tbf();
 	gprs_rlc_window *window();
-	struct msgb *create_ul_ack(uint32_t fn, uint8_t ts);
-	bool ctrl_ack_to_toggle();
-	bool handle_ctrl_ack(enum pdch_ulc_tbf_poll_reason reason);
 	/* blocks were acked */
 	int rcv_data_block_acknowledged(
 		const struct gprs_rlc_data_info *rlc,
@@ -97,11 +103,12 @@
 	int32_t m_rx_counter; /* count all received blocks */
 	uint8_t m_usf[8];	/* list USFs per PDCH (timeslot), initialized to USF_INVALID */
 	uint8_t m_contention_resolution_done; /* set after done */
-	uint8_t m_final_ack_sent; /* set if we sent final ack */
 
 	struct rate_ctr_group *m_ul_gprs_ctrs;
 	struct rate_ctr_group *m_ul_egprs_ctrs;
 
+	struct tbf_ul_ass_fsm_ctx ul_ack_fsm;
+
 protected:
 	void maybe_schedule_uplink_acknack(const gprs_rlc_data_info *rlc, bool countdown_finished);
 
@@ -138,6 +145,8 @@
 struct gprs_rlcmac_ul_tbf *as_ul_tbf(struct gprs_rlcmac_tbf *tbf);
 void tbf_usf_timeout(struct gprs_rlcmac_ul_tbf *tbf);
 bool ul_tbf_contention_resolution_done(const struct gprs_rlcmac_ul_tbf *tbf);
+struct osmo_fsm_inst *tbf_ul_ack_fi(const struct gprs_rlcmac_ul_tbf *tbf);
+void ul_tbf_contention_resolution_success(struct gprs_rlcmac_ul_tbf *tbf);
 
 #define LOGPTBFUL(tbf, level, fmt, args...) LOGP(DTBFUL, level, "%s " fmt, tbf_name(tbf), ## args)
 #ifdef __cplusplus
diff --git a/src/tbf_ul_ack_fsm.c b/src/tbf_ul_ack_fsm.c
new file mode 100644
index 0000000..16ca22b
--- /dev/null
+++ b/src/tbf_ul_ack_fsm.c
@@ -0,0 +1,254 @@
+/* tbf_ul_ack_fsm.c
+ *
+ * Copyright (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
+ * Author: Pau Espin Pedrol <pespin@sysmocom.de>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ */
+
+#include <unistd.h>
+
+#include <talloc.h>
+
+#include <osmocom/core/bitvec.h>
+
+#include <tbf_ul_ack_fsm.h>
+#include <gprs_rlcmac.h>
+#include <gprs_debug.h>
+#include <gprs_ms.h>
+#include <encoding.h>
+#include <bts.h>
+#include <tbf.h>
+#include <tbf_ul.h>
+
+#include <tbf_ul_ack_fsm.h>
+
+#define X(s) (1 << (s))
+
+const struct osmo_tdef_state_timeout tbf_ul_ack_fsm_timeouts[32] = {
+	[TBF_UL_ACK_ST_NONE] = {},
+	[TBF_UL_ACK_ST_SCHED_UL_ACK] = {},
+	[TBF_UL_ACK_ST_WAIT_ACK] = {},
+};
+
+const struct value_string tbf_ul_ack_fsm_event_names[] = {
+	{ TBF_UL_ACK_EV_SCHED_ACK, "SCHED_ACK" },
+	{ TBF_UL_ACK_EV_CREATE_RLCMAC_MSG, "CREATE_RLCMAC_MSG" },
+	{ TBF_UL_ACK_EV_RX_CTRL_ACK, "RX_CTRL_ACK" },
+	{ TBF_UL_ACK_EV_POLL_TIMEOUT, "POLL_TIMEOUT" },
+	{ 0, NULL }
+};
+
+static struct msgb *create_ul_ack_nack(const struct tbf_ul_ack_fsm_ctx *ctx,
+				       const struct tbf_ul_ack_ev_create_rlcmac_msg_ctx *d,
+				       bool final)
+{
+	struct msgb *msg;
+	int rc;
+	unsigned int rrbp = 0;
+	uint32_t new_poll_fn = 0;
+	struct gprs_rlcmac_tbf *tbf = (struct gprs_rlcmac_tbf *)ctx->tbf;
+	struct GprsMs *ms = tbf_ms(tbf);
+
+	if (final) {
+		rc = tbf_check_polling(tbf, d->fn, d->ts, &new_poll_fn, &rrbp);
+		if (rc < 0)
+			return NULL;
+	}
+
+	msg = msgb_alloc(23, "rlcmac_ul_ack");
+	if (!msg)
+		return NULL;
+	struct bitvec *ack_vec = bitvec_alloc(23, tbf);
+	if (!ack_vec) {
+		msgb_free(msg);
+		return NULL;
+	}
+	bitvec_unhex(ack_vec, DUMMY_VEC);
+	write_packet_uplink_ack(ack_vec, ctx->tbf, final, rrbp);
+	bitvec_pack(ack_vec, msgb_put(msg, 23));
+	bitvec_free(ack_vec);
+
+	/* TS 44.060 7a.2.1.1: "The contention resolution is completed on
+	 * the network side when the network receives an RLC data block that
+	 * comprises the TLLI value that identifies the mobile station and the
+	 * TFI value associated with the TBF."
+	 * However, it's handier for us to mark contention resolution success
+	 * here since according to spec upon rx UL ACK is the time at which MS
+	 * realizes contention resolution succeeds. */
+	if (ms_tlli(ms) != GSM_RESERVED_TMSI)
+		ul_tbf_contention_resolution_success(ctx->tbf);
+
+	if (final)
+		tbf_set_polling(tbf, new_poll_fn, d->ts, PDCH_ULC_POLL_UL_ACK);
+
+	return msg;
+}
+
+static void st_none(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+	switch (event) {
+	case TBF_UL_ACK_EV_SCHED_ACK:
+		tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_SCHED_UL_ACK);
+		break;
+	default:
+		OSMO_ASSERT(0);
+	}
+}
+
+static void st_sched_ul_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+	struct tbf_ul_ack_fsm_ctx *ctx = (struct tbf_ul_ack_fsm_ctx *)fi->priv;
+	struct gprs_rlcmac_tbf *tbf = (struct gprs_rlcmac_tbf *)ctx->tbf;
+	struct tbf_ul_ack_ev_create_rlcmac_msg_ctx *data_ctx;
+	bool final;
+
+	switch (event) {
+	case TBF_UL_ACK_EV_SCHED_ACK:
+		LOGPTBFUL(tbf, LOGL_DEBUG,
+			  "Sending Ack/Nack already scheduled, no need to re-schedule\n");
+		break;
+	case TBF_UL_ACK_EV_CREATE_RLCMAC_MSG:
+		data_ctx = (struct tbf_ul_ack_ev_create_rlcmac_msg_ctx *)data;
+		final = tbf_state(tbf) == TBF_ST_FINISHED;
+		data_ctx->msg = create_ul_ack_nack(ctx, data_ctx, final);
+		if (!data_ctx->msg)
+			return;
+		if (final) /* poll set */
+			tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_WAIT_ACK);
+		else
+			tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_NONE);
+		break;
+	default:
+		OSMO_ASSERT(0);
+	}
+}
+
+static void st_wait_ctrl_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+	struct tbf_ul_ack_fsm_ctx *ctx = (struct tbf_ul_ack_fsm_ctx *)fi->priv;
+	struct gprs_rlcmac_tbf *tbf = (struct gprs_rlcmac_tbf *)ctx->tbf;
+
+	switch (event) {
+	case TBF_UL_ACK_EV_SCHED_ACK:
+		/* ignore, we are in the middle of waiting for a response */
+		break;
+	case TBF_UL_ACK_EV_RX_CTRL_ACK:
+		tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_NONE);
+		break;
+	case TBF_UL_ACK_EV_POLL_TIMEOUT:
+		LOGPTBF(tbf, LOGL_NOTICE,
+			"Timeout for polling PACKET CONTROL ACK for PACKET UPLINK ACK: %s\n",
+			tbf_rlcmac_diag(tbf));
+		/* Reschedule Ul Ack/NAck */
+		tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_SCHED_UL_ACK);
+		break;
+	default:
+		OSMO_ASSERT(0);
+	}
+}
+
+static int tbf_ul_ack_fsm_timer_cb(struct osmo_fsm_inst *fi)
+{
+	switch (fi->T) {
+	default:
+		OSMO_ASSERT(0);
+	}
+	return 0;
+}
+
+static struct osmo_fsm_state tbf_ul_ack_fsm_states[] = {
+	[TBF_UL_ACK_ST_NONE] = {
+		.in_event_mask =
+			X(TBF_UL_ACK_EV_SCHED_ACK),
+		.out_state_mask =
+			X(TBF_UL_ACK_ST_SCHED_UL_ACK),
+		.name = "NONE",
+		.action = st_none,
+	},
+	[TBF_UL_ACK_ST_SCHED_UL_ACK] = {
+		.in_event_mask =
+			X(TBF_UL_ACK_EV_SCHED_ACK) |
+			X(TBF_UL_ACK_EV_CREATE_RLCMAC_MSG),
+		.out_state_mask =
+			X(TBF_UL_ACK_ST_NONE) |
+			X(TBF_UL_ACK_ST_WAIT_ACK),
+		.name = "SCHED_UL_ACK",
+		.action = st_sched_ul_ack,
+	},
+	[TBF_UL_ACK_ST_WAIT_ACK] = {
+		.in_event_mask =
+			X(TBF_UL_ACK_EV_SCHED_ACK) |
+			X(TBF_UL_ACK_EV_RX_CTRL_ACK) |
+			X(TBF_UL_ACK_EV_POLL_TIMEOUT),
+		.out_state_mask =
+			X(TBF_UL_ACK_ST_NONE) |
+			X(TBF_UL_ACK_ST_SCHED_UL_ACK),
+		.name = "WAIT_ACK",
+		.action = st_wait_ctrl_ack,
+	},
+};
+
+struct osmo_fsm tbf_ul_ack_fsm = {
+	.name = "UL_ACK_TBF",
+	.states = tbf_ul_ack_fsm_states,
+	.num_states = ARRAY_SIZE(tbf_ul_ack_fsm_states),
+	.timer_cb = tbf_ul_ack_fsm_timer_cb,
+	.log_subsys = DTBFUL,
+	.event_names = tbf_ul_ack_fsm_event_names,
+};
+
+static __attribute__((constructor)) void tbf_ul_ack_fsm_init(void)
+{
+	OSMO_ASSERT(osmo_fsm_register(&tbf_ul_ack_fsm) == 0);
+}
+
+
+struct msgb *tbf_ul_ack_create_rlcmac_msg(const struct gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t ts)
+{
+	int rc;
+	struct tbf_ul_ack_ev_create_rlcmac_msg_ctx data_ctx = {
+		.fn = fn,
+		.ts = ts,
+		.msg = NULL,
+	};
+	OSMO_ASSERT(tbf_direction(tbf) == GPRS_RLCMAC_UL_TBF);
+
+	rc = osmo_fsm_inst_dispatch(tbf_ul_ack_fi((const struct gprs_rlcmac_ul_tbf *)tbf), TBF_UL_ACK_EV_CREATE_RLCMAC_MSG, &data_ctx);
+	if (rc != 0 || !data_ctx.msg)
+		return NULL;
+	return data_ctx.msg;
+}
+
+bool tbf_ul_ack_rts(const struct gprs_rlcmac_tbf *tbf)
+{
+	struct osmo_fsm_inst *fi = tbf_ul_ack_fi((const struct gprs_rlcmac_ul_tbf *)tbf);
+	return fi->state == TBF_UL_ACK_ST_SCHED_UL_ACK;
+}
+
+/* Did we already send the Final ACK and we are waiting for its confirmation (CTRL ACK) ? */
+bool tbf_ul_ack_waiting_cnf_final_ack(const struct gprs_rlcmac_tbf* tbf)
+{
+	OSMO_ASSERT(tbf_direction(tbf) == GPRS_RLCMAC_UL_TBF);
+	struct osmo_fsm_inst *fi = tbf_ul_ack_fi((const struct gprs_rlcmac_ul_tbf *)tbf);
+	return fi->state == TBF_UL_ACK_ST_WAIT_ACK;
+}
+
+bool tbf_ul_ack_exp_ctrl_ack(const struct gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t ts)
+{
+	struct osmo_fsm_inst *fi = tbf_ul_ack_fi((const struct gprs_rlcmac_ul_tbf *)tbf);
+	return fi->state == TBF_UL_ACK_ST_WAIT_ACK;
+	/* FIXME: validate FN and TS match: && ctx->poll_fn = fn && ctx->poll_ts == ts */
+}
diff --git a/src/tbf_ul_ack_fsm.h b/src/tbf_ul_ack_fsm.h
new file mode 100644
index 0000000..aaee5a3
--- /dev/null
+++ b/src/tbf_ul_ack_fsm.h
@@ -0,0 +1,72 @@
+/* tbf_ul_ack_fsm.h
+ *
+ * Copyright (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
+ * Author: Pau Espin Pedrol <pespin@sysmocom.de>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ */
+#pragma once
+
+#include <osmocom/core/fsm.h>
+#include <osmocom/core/tdef.h>
+
+#include <gprs_pcu.h>
+
+struct gprs_rlcmac_tbf;
+struct gprs_rlcmac_ul_tbf;
+
+enum tbf_ul_ack_fsm_event {
+	TBF_UL_ACK_EV_SCHED_ACK, /* Tx UL ACK/NACK is pending */
+	TBF_UL_ACK_EV_CREATE_RLCMAC_MSG, /* Scheduler wants to gen+Tx the Ass (rej): data=tbf_ul_ack_ev_create_rlcmac_msg_ctx */
+	TBF_UL_ACK_EV_RX_CTRL_ACK, /* Received CTRL ACK answering poll set on UL ACK/NACK */
+	TBF_UL_ACK_EV_POLL_TIMEOUT, /* Pdch Ul Controller signals timeout for poll set on UL ACK/NACK */
+};
+
+enum tbf_ul_ack_fsm_states {
+	TBF_UL_ACK_ST_NONE = 0,
+	TBF_UL_ACK_ST_SCHED_UL_ACK, /* send UL ACK/NACK on next RTS */
+	TBF_UL_ACK_ST_WAIT_ACK, /* wait for PACKET CONTROL ACK */
+};
+
+struct tbf_ul_ack_fsm_ctx {
+	struct osmo_fsm_inst *fi;
+	struct gprs_rlcmac_ul_tbf *tbf; /* back pointer */
+};
+
+extern const struct osmo_tdef_state_timeout tbf_ul_ack_fsm_timeouts[32];
+/* Transition to a state, using the T timer defined in tbf_ul_ack_fsm_timeouts.
+ * The actual timeout value is in turn obtained from conn->T_defs.
+ * Assumes local variable fi exists. */
+#define tbf_ul_ack_fsm_state_chg(fi, NEXT_STATE) \
+	osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, \
+				     tbf_ul_ack_fsm_timeouts, \
+				     the_pcu->T_defs, \
+				     -1)
+
+extern struct osmo_fsm tbf_ul_ack_fsm;
+
+
+/* passed as data in TBF_UL_ACK_EV_CREATE_RLCMAC_MSG */
+struct tbf_ul_ack_ev_create_rlcmac_msg_ctx {
+	uint32_t fn; /* FN where the created DL ctrl block is to be sent */
+	uint8_t ts; /* TS where the created DL ctrl block is to be sent */
+	struct msgb *msg; /* to be filled by FSM during event processing */
+};
+
+
+struct msgb *tbf_ul_ack_create_rlcmac_msg(const struct gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t ts);
+bool tbf_ul_ack_rts(const struct gprs_rlcmac_tbf *tbf);
+bool tbf_ul_ack_waiting_cnf_final_ack(const struct gprs_rlcmac_tbf *tbf);
+bool tbf_ul_ack_exp_ctrl_ack(const struct gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t ts);