Forward ETWS Primary Notification to MS

Receive an Application Information Request from the BTS via PCU
interface. Construct a Packet Application Information message from it
(3GPP TS 44.060 11.2.47) and send it to all MS with active TBF.

The TTCN-3 test infrastructure to test this feature is not quite ready
yet, so I've added C unit tests instead.

Related: OS#4048
Change-Id: Ie35959f833f46bde5f2126314b6f96763f863b36
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 887200d..42dade9 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,7 +1,7 @@
 AM_CPPFLAGS = $(STD_DEFINES_AND_INCLUDES) $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGB_CFLAGS) $(LIBOSMOGSM_CFLAGS) -I$(top_srcdir)/src/ -I$(top_srcdir)/include/
 AM_LDFLAGS = -lrt -no-install
 
-check_PROGRAMS = rlcmac/RLCMACTest alloc/AllocTest alloc/MslotTest tbf/TbfTest types/TypesTest ms/MsTest llist/LListTest llc/LlcTest codel/codel_test edge/EdgeTest bitcomp/BitcompTest fn/FnTest
+check_PROGRAMS = rlcmac/RLCMACTest alloc/AllocTest alloc/MslotTest tbf/TbfTest types/TypesTest ms/MsTest llist/LListTest llc/LlcTest codel/codel_test edge/EdgeTest bitcomp/BitcompTest fn/FnTest app_info/AppInfoTest
 noinst_PROGRAMS = emu/pcu_emu
 
 rlcmac_RLCMACTest_SOURCES = rlcmac/RLCMACTest.cpp
@@ -108,6 +108,14 @@
 	$(LIBOSMOCORE_LIBS) \
 	$(COMMON_LA)
 
+app_info_AppInfoTest_SOURCES = app_info/AppInfoTest.cpp
+app_info_AppInfoTest_LDADD = \
+	$(top_builddir)/src/libgprs.la \
+	$(LIBOSMOGB_LIBS) \
+	$(LIBOSMOGSM_LIBS) \
+	$(LIBOSMOCORE_LIBS) \
+	$(COMMON_LA)
+
 # The `:;' works around a Bash 3.2 bug when the output is not writeable.
 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
 	:;{ \
@@ -138,7 +146,8 @@
 	llist/LListTest.ok llist/LListTest.err \
 	codel/codel_test.ok \
 	edge/EdgeTest.ok \
-	fn/FnTest.ok
+	fn/FnTest.ok \
+	app_info/AppInfoTest.ok app_info/AppInfoTest.err
 
 DISTCLEANFILES = atconfig
 
diff --git a/tests/app_info/AppInfoTest.cpp b/tests/app_info/AppInfoTest.cpp
new file mode 100644
index 0000000..c4cf548
--- /dev/null
+++ b/tests/app_info/AppInfoTest.cpp
@@ -0,0 +1,191 @@
+/* Copyright (C) 2019 by sysmocom - s.f.m.c. GmbH <info@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 <cstdlib>
+#include <cstring>
+#include <assert.h>
+#include "gprs_rlcmac.h"
+#include "bts.h"
+
+extern "C" {
+#include <osmocom/vty/telnet_interface.h>
+#include <osmocom/vty/logging.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/application.h>
+}
+
+using namespace std;
+gprs_rlcmac_dl_tbf *tbf1, *tbf2;
+GprsMs *ms1, *ms2;
+struct msgb *sched_app_info(struct gprs_rlcmac_tbf *tbf);
+
+/* globals used by the code */
+void *tall_pcu_ctx;
+int16_t spoof_mnc = 0, spoof_mcc = 0;
+bool spoof_mnc_3_digits = false;
+
+void test_enc_zero_len() {
+	struct gsm_pcu_if_app_info_req req = {0, 0, {0}};
+
+	fprintf(stderr, "--- %s ---\n",  __func__);
+	assert(gprs_rlcmac_app_info_msg(&req) == NULL);
+	fprintf(stderr, "\n");
+}
+
+void test_enc() {
+	struct gsm_pcu_if_app_info_req req = {0, 15, {0xff, 0x00, 0xff}};
+	const char *exp = "03 fc 03 fc 00 00 00 00 00 00 00 00 00 00 00 00 "; /* shifted by two bits to the right */
+	struct msgb *msg;
+	char *msg_dump;
+
+	fprintf(stderr, "--- %s ---\n",  __func__);
+	msg = gprs_rlcmac_app_info_msg(&req);
+	msg_dump = msgb_hexdump_c(tall_pcu_ctx, msg);
+
+	fprintf(stderr, "exp: %s\n", exp);
+	fprintf(stderr, "msg: %s\n", msg_dump);
+	assert(strcmp(msg_dump, exp) == 0);
+
+	msgb_free(msg);
+	talloc_free(msg_dump);
+	fprintf(stderr, "\n");
+}
+
+void test_pcu_rx_no_subscr_with_active_tbf()
+{
+	struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, };
+
+	fprintf(stderr, "--- %s ---\n",  __func__);
+	pcu_rx(PCU_IF_MSG_APP_INFO_REQ, &pcu_prim);
+	fprintf(stderr, "\n");
+}
+
+void prepare_bts_with_two_dl_tbf_subscr()
+{
+	BTS *bts = BTS::main_bts();
+	struct gprs_rlcmac_bts *bts_data;
+	struct gprs_rlcmac_trx *trx;
+
+	fprintf(stderr, "--- %s ---\n",  __func__);
+
+	bts_data = bts->bts_data();
+	bts_data->alloc_algorithm = alloc_algorithm_b;
+
+	trx = bts_data->trx;
+	trx->pdch[4].enable();
+	trx->pdch[5].enable();
+	trx->pdch[6].enable();
+	trx->pdch[7].enable();
+
+	ms1 = bts->ms_alloc(10, 11);
+	tbf1 = tbf_alloc_dl_tbf(bts_data, ms1, 0, 10, 11, false);
+	ms2 = bts->ms_alloc(12, 13);
+	tbf2 = tbf_alloc_dl_tbf(bts_data, ms2, 0, 12, 13, false);
+
+	fprintf(stderr, "\n");
+}
+
+void test_sched_app_info_ok()
+{
+	struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, };
+	struct msgb *msg;
+
+	fprintf(stderr, "--- %s ---\n",  __func__);
+	pcu_prim.u.app_info_req = {0, 15, {0xff, 0x00, 0xff}};
+	pcu_rx(PCU_IF_MSG_APP_INFO_REQ, &pcu_prim);
+
+	msg = sched_app_info(tbf1);
+	assert(msg);
+	msgb_free(msg);
+
+	msg = sched_app_info(tbf2);
+	assert(msg);
+	msgb_free(msg);
+
+	fprintf(stderr, "\n");
+}
+
+void test_sched_app_info_missing_app_info_in_bts()
+{
+	struct gprs_rlcmac_bts *bts_data = BTS::main_bts()->bts_data();
+	struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, };
+
+	fprintf(stderr, "--- %s ---\n",  __func__);
+	pcu_prim.u.app_info_req = {0, 15, {0xff, 0x00, 0xff}};
+	pcu_rx(PCU_IF_MSG_APP_INFO_REQ, &pcu_prim);
+
+	msgb_free(bts_data->app_info);
+	bts_data->app_info = NULL;
+
+	assert(sched_app_info(tbf1) == NULL);
+
+	fprintf(stderr, "\n");
+}
+
+void test_pcu_rx_overwrite_app_info()
+{
+	struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, };
+
+	fprintf(stderr, "--- %s ---\n",  __func__);
+	pcu_prim.u.app_info_req = {0, 15, {0xff, 0x00, 0xff}};
+	pcu_rx(PCU_IF_MSG_APP_INFO_REQ, &pcu_prim);
+	pcu_rx(PCU_IF_MSG_APP_INFO_REQ, &pcu_prim);
+	fprintf(stderr, "\n");
+}
+
+void cleanup()
+{
+	fprintf(stderr, "--- %s ---\n",  __func__);
+
+	BTS::main_bts()->cleanup();
+	talloc_free(tbf1);
+	talloc_free(tbf2);
+	/* FIXME: talloc report disabled, because bts->ms_alloc() in prepare_bts_with_two_dl_tbf_subscr() causes leak */
+	/* talloc_report_full(tall_pcu_ctx, stderr); */
+	talloc_free(tall_pcu_ctx);
+}
+
+int main(int argc, char *argv[])
+{
+	tall_pcu_ctx = talloc_named_const(NULL, 1, "AppInfoTest");
+	osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
+	log_set_use_color(osmo_stderr_target, 0);
+	log_set_print_filename(osmo_stderr_target, 0);
+	log_parse_category_mask(osmo_stderr_target, "DL1IF,1:DRLCMAC,3:DRLCMACSCHED,1");
+
+	test_enc_zero_len();
+	test_enc();
+	test_pcu_rx_no_subscr_with_active_tbf();
+
+	prepare_bts_with_two_dl_tbf_subscr();
+	test_sched_app_info_ok();
+	test_sched_app_info_missing_app_info_in_bts();
+	test_pcu_rx_overwrite_app_info();
+
+	cleanup();
+}
+
+/*
+ * stubs that should not be reached
+ */
+extern "C" {
+void l1if_pdch_req() { abort(); }
+void l1if_connect_pdch() { abort(); }
+void l1if_close_pdch() { abort(); }
+void l1if_open_pdch() { abort(); }
+}
diff --git a/tests/app_info/AppInfoTest.err b/tests/app_info/AppInfoTest.err
new file mode 100644
index 0000000..6ef5d83
--- /dev/null
+++ b/tests/app_info/AppInfoTest.err
@@ -0,0 +1,50 @@
+--- test_enc_zero_len ---
+Application Information Request with zero length received!
+
+--- test_enc ---
+exp: 03 fc 03 fc 00 00 00 00 00 00 00 00 00 00 00 00 
+msg: 03 fc 03 fc 00 00 00 00 00 00 00 00 00 00 00 00 
+
+--- test_pcu_rx_no_subscr_with_active_tbf ---
+Application Information Request received: type=0x00000000 len=0
+Packet Application Information will not be sent, no subscribers with active TBF
+
+--- prepare_bts_with_two_dl_tbf_subscr ---
+Creating MS object, TLLI = 0x00000000
+Modifying MS object, TLLI = 0x00000000, MS class 0 -> 10
+Modifying MS object, TLLI = 0x00000000, EGPRS MS class 0 -> 11
+[DL] algo B <multi> (suggested TRX: 0): using 4 slots
+PDCH(TS 4, TRX 0): Attaching TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=NULL), 1 TBFs, USFs = 00, TFIs = 00000001.
+PDCH(TS 5, TRX 0): Attaching TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=NULL), 1 TBFs, USFs = 00, TFIs = 00000001.
+PDCH(TS 6, TRX 0): Attaching TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=NULL), 1 TBFs, USFs = 00, TFIs = 00000001.
+PDCH(TS 7, TRX 0): Attaching TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=NULL), 1 TBFs, USFs = 00, TFIs = 00000001.
+Attaching TBF to MS object, TLLI = 0x00000000, TBF = TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=NULL)
+Creating MS object, TLLI = 0x00000000
+Modifying MS object, TLLI = 0x00000000, MS class 0 -> 12
+Modifying MS object, TLLI = 0x00000000, EGPRS MS class 0 -> 13
+[DL] algo B <multi> (suggested TRX: 0): using 3 slots
+PDCH(TS 4, TRX 0): Attaching TBF(TFI=1 TLLI=0x00000000 DIR=DL STATE=NULL), 2 TBFs, USFs = 00, TFIs = 00000003.
+PDCH(TS 5, TRX 0): Attaching TBF(TFI=1 TLLI=0x00000000 DIR=DL STATE=NULL), 2 TBFs, USFs = 00, TFIs = 00000003.
+PDCH(TS 6, TRX 0): Attaching TBF(TFI=1 TLLI=0x00000000 DIR=DL STATE=NULL), 2 TBFs, USFs = 00, TFIs = 00000003.
+Attaching TBF to MS object, TLLI = 0x00000000, TBF = TBF(TFI=1 TLLI=0x00000000 DIR=DL STATE=NULL)
+
+--- test_sched_app_info_ok ---
+Application Information Request received: type=0x00000000 len=15
+Sending Packet Application Information to 2 subscribers with active TBF
+Sending Packet Application Information message
+Sending Packet Application Information message
+Packet Application Information successfully sent to all MS with active TBF
+
+--- test_sched_app_info_missing_app_info_in_bts ---
+Application Information Request received: type=0x00000000 len=15
+Sending Packet Application Information to 2 subscribers with active TBF
+MS has app_info_pending flag set, but no Packet Application Information message stored in BTS!
+
+--- test_pcu_rx_overwrite_app_info ---
+Application Information Request received: type=0x00000000 len=15
+Sending Packet Application Information to 2 subscribers with active TBF
+Application Information Request received: type=0x00000000 len=15
+Previous Packet Application Information was not sent to all subscribers, overwriting with new one
+Sending Packet Application Information to 2 subscribers with active TBF
+
+--- cleanup ---
diff --git a/tests/app_info/AppInfoTest.ok b/tests/app_info/AppInfoTest.ok
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/app_info/AppInfoTest.ok
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 86f45a8..09b0247 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -82,3 +82,10 @@
 cat $abs_srcdir/fn/FnTest.ok > expout
 AT_CHECK([$OSMO_QEMU $abs_top_builddir/tests/fn/FnTest], [0], [expout], [ignore])
 AT_CLEANUP
+
+AT_SETUP([app_info])
+AT_KEYWORDS([app_info])
+cat $abs_srcdir/app_info/AppInfoTest.ok > expout
+cat $abs_srcdir/app_info/AppInfoTest.err > experr
+AT_CHECK([$OSMO_QEMU $abs_top_builddir/tests/app_info/AppInfoTest], [0], [expout], [experr])
+AT_CLEANUP