Move FINISHED tbf_state transition to tbf_fsm

Related: OS#2709
Change-Id: I81f507e3a2821254f03364a58ead02333e63099f
diff --git a/src/tbf_dl.cpp b/src/tbf_dl.cpp
index 12fd99f..ed5003c 100644
--- a/src/tbf_dl.cpp
+++ b/src/tbf_dl.cpp
@@ -711,7 +711,7 @@
 				is_final = llc_queue_size(llc_queue()) == 0 && !keep_open(fn);
 				if (is_final) {
 					rdbi->cv = 0;
-					TBF_SET_STATE(this, TBF_ST_FINISHED);
+					osmo_fsm_inst_dispatch(this->state_fsm.fi, TBF_EV_LAST_DL_DATA_SENT, NULL);
 				}
 
 				if (mcs_is_edge(cs)) {
@@ -756,7 +756,7 @@
 
 		if (is_final) {
 			request_dl_ack();
-			TBF_SET_STATE(this, TBF_ST_FINISHED);
+			osmo_fsm_inst_dispatch(this->state_fsm.fi, TBF_EV_LAST_DL_DATA_SENT, NULL);
 		}
 
 		/* dequeue next LLC frame, if any */
diff --git a/src/tbf_fsm.c b/src/tbf_fsm.c
index 5799d90..8e0649d 100644
--- a/src/tbf_fsm.c
+++ b/src/tbf_fsm.c
@@ -46,6 +46,8 @@
 	{ TBF_EV_ASSIGN_DEL_CCCH, "ASSIGN_DEL_CCCH" },
 	{ TBF_EV_ASSIGN_ACK_PACCH, "ASSIGN_ACK_PACCH" },
 	{ TBF_EV_ASSIGN_READY_CCCH, "ASSIGN_READY_CCCH" },
+	{ TBF_EV_LAST_DL_DATA_SENT, "LAST_DL_DATA_SENT" },
+	{ TBF_EV_LAST_UL_DATA_RECVD, "LAST_UL_DATA_RECVD" },
 	{ 0, NULL }
 };
 
@@ -137,6 +139,19 @@
 	}
 }
 
+static void st_flow(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+	switch (event) {
+	case TBF_EV_LAST_DL_DATA_SENT:
+	case TBF_EV_LAST_UL_DATA_RECVD:
+		/* All data has been sent or received, change state to FINISHED */
+		tbf_fsm_state_chg(fi, TBF_ST_FINISHED);
+		break;
+	default:
+		OSMO_ASSERT(0);
+	}
+}
+
 static void tbf_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
 {
 	/* TODO: needed ?
@@ -180,12 +195,14 @@
 	},
 	[TBF_ST_FLOW] = {
 		.in_event_mask =
-			0,
+			X(TBF_EV_LAST_DL_DATA_SENT) |
+			X(TBF_EV_LAST_UL_DATA_RECVD),
 		.out_state_mask =
 			X(TBF_ST_FINISHED) |
 			X(TBF_ST_WAIT_RELEASE) |
 			X(TBF_ST_RELEASING),
 		.name = "FLOW",
+		.action = st_flow,
 	},
 	[TBF_ST_FINISHED] = {
 		.in_event_mask =
diff --git a/src/tbf_fsm.h b/src/tbf_fsm.h
index a3050c0..ea0f58d 100644
--- a/src/tbf_fsm.h
+++ b/src/tbf_fsm.h
@@ -32,6 +32,8 @@
 	TBF_EV_ASSIGN_DEL_CCCH, /* An assignment previously sent over CCCH has been confirmed by MS */
 	TBF_EV_ASSIGN_ACK_PACCH, /*  We received a CTRL ACK confirming assignment started on PACCH */
 	TBF_EV_ASSIGN_READY_CCCH, /* TBF Start Time timer triggered */
+	TBF_EV_LAST_DL_DATA_SENT, /* DL TBF sends RLCMAC block containing last DL avilable data buffered */
+	TBF_EV_LAST_UL_DATA_RECVD, /* UL TBF sends RLCMAC block containing last UL data (cv=0) */
 };
 
 enum tbf_fsm_states {
diff --git a/src/tbf_ul.cpp b/src/tbf_ul.cpp
index 2874fc6..3eec555 100644
--- a/src/tbf_ul.cpp
+++ b/src/tbf_ul.cpp
@@ -536,7 +536,7 @@
 			  rdbi->bsn, rdbi->cv);
 		if (rdbi->cv == 0) {
 			LOGPTBFUL(this, LOGL_DEBUG, "Finished with UL TBF\n");
-			TBF_SET_STATE(this, TBF_ST_FINISHED);
+			osmo_fsm_inst_dispatch(this->state_fsm.fi, TBF_EV_LAST_UL_DATA_RECVD, NULL);
 			/* Reset N3103 counter. */
 			this->n_reset(N3103);
 		}