bts: Introduce a PollController that has the responsibility to poll

For each frame indication received by the BTS the poll controller
is asked to expire timedout entries.
diff --git a/src/Makefile.am b/src/Makefile.am
index 012f8a9..272827d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -43,7 +43,8 @@
 	pcu_l1_if.cpp \
 	pcu_vty.c \
 	tbf.cpp \
-	bts.cpp
+	bts.cpp \
+	poll_controller.cpp
 
 if ENABLE_SYSMOBTS
 libgprs_la_SOURCES += \
@@ -77,7 +78,8 @@
 	sysmo_l1_if.h \
 	femtobts.h \
 	tbf.h \
-	bts.h
+	bts.h \
+	poll_controller.h
 
 osmo_pcu_SOURCES = pcu_main.cpp
 
diff --git a/src/bts.cpp b/src/bts.cpp
index b278ecf..29cd3e5 100644
--- a/src/bts.cpp
+++ b/src/bts.cpp
@@ -19,6 +19,8 @@
  */
 
 #include <bts.h>
+#include <poll_controller.h>
+
 #include <string.h>
 
 static BTS s_bts;
@@ -40,6 +42,7 @@
 
 BTS::BTS()
 	: m_cur_fn(0)
+	, m_pollController(*this)
 {
 	memset(&m_bts, 0, sizeof(m_bts));
 	m_bts.bts = this;
@@ -48,4 +51,5 @@
 void BTS::set_current_frame_number(int fn)
 {
 	m_cur_fn = fn;
+	m_pollController.expireTimedout(m_cur_fn);
 }
diff --git a/src/bts.h b/src/bts.h
index 507c79f..4babd25 100644
--- a/src/bts.h
+++ b/src/bts.h
@@ -26,6 +26,8 @@
 #include <osmocom/core/linuxlist.h>
 #include <osmocom/core/timer.h>
 }
+
+#include "poll_controller.h"
 #endif
 
 #include <stdint.h>
@@ -113,6 +115,12 @@
 private:
 	int m_cur_fn;
 	struct gprs_rlcmac_bts m_bts;
+	PollController m_pollController;
+
+private:
+	/* disable copying to avoid slicing */
+	BTS(const BTS&);
+	BTS& operator=(const BTS&);
 };
 
 inline int BTS::current_frame_number() const
diff --git a/src/pcu_l1_if.cpp b/src/pcu_l1_if.cpp
index ad0887e..438bfa8 100644
--- a/src/pcu_l1_if.cpp
+++ b/src/pcu_l1_if.cpp
@@ -494,12 +494,7 @@
 
 static int pcu_rx_time_ind(struct gsm_pcu_if_time_ind *time_ind)
 {
-	struct gprs_rlcmac_bts *bts = bts_main_data();
-	struct gprs_rlcmac_tbf *tbf;
-	struct gprs_rlcmac_sba *sba, *sba2;
-	uint32_t elapsed;
 	uint8_t fn13 = time_ind->fn % 13;
-	int frame_number = time_ind->fn;
 
 	/* omit frame numbers not starting at a MAC block */
 	if (fn13 != 0 && fn13 != 4 && fn13 != 8)
@@ -509,32 +504,6 @@
 //		time_ind->fn % 52);
 
 	BTS::main_bts()->set_current_frame_number(time_ind->fn);
-
-	/* check for poll timeout */
-	llist_for_each_entry(tbf, &gprs_rlcmac_ul_tbfs, list) {
-		if (tbf->poll_state == GPRS_RLCMAC_POLL_SCHED) {
-			elapsed = (frame_number + 2715648 - tbf->poll_fn)
-								% 2715648;
-			if (elapsed >= 20 && elapsed < 2715400)
-				gprs_rlcmac_poll_timeout(bts, tbf);
-		}
-	}
-	llist_for_each_entry(tbf, &gprs_rlcmac_dl_tbfs, list) {
-		if (tbf->poll_state == GPRS_RLCMAC_POLL_SCHED) {
-			elapsed = (frame_number + 2715648 - tbf->poll_fn)
-								% 2715648;
-			if (elapsed >= 20 && elapsed < 2715400)
-				gprs_rlcmac_poll_timeout(bts, tbf);
-		}
-	}
-	llist_for_each_entry_safe(sba, sba2, &gprs_rlcmac_sbas, list) {
-		elapsed = (frame_number + 2715648 - sba->fn) % 2715648;
-		if (elapsed >= 20 && elapsed < 2715400) {
-			/* sba will be freed here */
-			gprs_rlcmac_sba_timeout(sba);
-		}
-	}
-
 	return 0;
 }
 
diff --git a/src/poll_controller.cpp b/src/poll_controller.cpp
new file mode 100644
index 0000000..59eef76
--- /dev/null
+++ b/src/poll_controller.cpp
@@ -0,0 +1,62 @@
+/* poll_controller.h
+ *
+ * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
+ * Copyright (C) 2013 by Holger Hans Peter Freyther
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 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 Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <poll_controller.h>
+#include <tbf.h>
+
+PollController::PollController(BTS& bts)
+	: m_bts(bts)
+{}
+
+void PollController::expireTimedout(int frame_number)
+{
+	struct gprs_rlcmac_bts *bts = m_bts.bts_data();
+	struct gprs_rlcmac_tbf *tbf;
+	struct gprs_rlcmac_sba *sba, *sba2;
+	uint32_t elapsed;
+
+	/* check for poll timeout */
+	llist_for_each_entry(tbf, &gprs_rlcmac_ul_tbfs, list) {
+		if (tbf->poll_state == GPRS_RLCMAC_POLL_SCHED) {
+			elapsed = (frame_number + 2715648 - tbf->poll_fn)
+								% 2715648;
+			if (elapsed >= 20 && elapsed < 2715400)
+				gprs_rlcmac_poll_timeout(bts, tbf);
+		}
+	}
+	llist_for_each_entry(tbf, &gprs_rlcmac_dl_tbfs, list) {
+		if (tbf->poll_state == GPRS_RLCMAC_POLL_SCHED) {
+			elapsed = (frame_number + 2715648 - tbf->poll_fn)
+								% 2715648;
+			if (elapsed >= 20 && elapsed < 2715400)
+				gprs_rlcmac_poll_timeout(bts, tbf);
+		}
+	}
+	llist_for_each_entry_safe(sba, sba2, &gprs_rlcmac_sbas, list) {
+		elapsed = (frame_number + 2715648 - sba->fn) % 2715648;
+		if (elapsed >= 20 && elapsed < 2715400) {
+			/* sba will be freed here */
+			gprs_rlcmac_sba_timeout(sba);
+		}
+	}
+
+}
diff --git a/src/poll_controller.h b/src/poll_controller.h
new file mode 100644
index 0000000..67af408
--- /dev/null
+++ b/src/poll_controller.h
@@ -0,0 +1,46 @@
+/* poll_controller.h
+ *
+ * Copyright (C) 2013 by Holger Hans Peter Freyther
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 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 Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#pragma once
+
+struct gprs_rlcmac_bts;
+
+class BTS;
+
+/**
+ * I belong to a BTS and I am responsible for finding TBFs and
+ * SBAs that should have been polled and execute the timeout
+ * action on them.
+ */
+class PollController {
+public:
+	PollController(BTS& bts);
+
+	void expireTimedout(int frame_number);
+
+private:
+	BTS& m_bts;
+
+private:
+	/* disable copying to avoid slicing */
+	PollController(const PollController&);
+	PollController& operator=(const PollController&);
+};