tbf: Add frames_since_last_poll method

This functions calculates the number of frames that have passed since
the last DL poll (RRBP flag set) has been sent. It returns a value
less than zero (fn_now - fn_sched) if the block has been scheduled but
not yet sent.

If the function is called before the first data block has been sent
it will return -1.

If the function is called before the first DL poll is sent, it
returns the number of frames since the first data block has been
sent.

Sponsored-by: On-Waves ehf
diff --git a/src/tbf_dl.cpp b/src/tbf_dl.cpp
index c9ad058..0630ba1 100644
--- a/src/tbf_dl.cpp
+++ b/src/tbf_dl.cpp
@@ -484,6 +484,11 @@
 	len = m_rlc.block(index)->len;
 	rh = (struct rlc_dl_header *)data;
 
+	/* If the TBF has just started, relate frames_since_last_poll to the
+	 * current fn */
+	if (m_last_dl_poll_fn < 0)
+		m_last_dl_poll_fn = fn;
+
 	need_poll = state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
 	/* Clear Polling, if still set in history buffer */
 	rh->s_p = 0;
@@ -538,6 +543,8 @@
 			/* set polling in header */
 			rh->rrbp = 0; /* N+13 */
 			rh->s_p = 1; /* Polling */
+
+			m_last_dl_poll_fn = poll_fn;
 		}
 	}
 
@@ -730,3 +737,17 @@
 {
 	return m_llc.chunk_size() > 0 || !llist_empty(&m_llc.queue);
 }
+
+int gprs_rlcmac_dl_tbf::frames_since_last_poll(unsigned fn) const
+{
+	unsigned wrapped;
+	if (m_last_dl_poll_fn < 0)
+		return -1;
+
+	wrapped = (fn + 2715648 - m_last_dl_poll_fn) % 2715648;
+	if (wrapped < 2715648/2)
+		return wrapped;
+	else
+		return wrapped - 2715648;
+}
+