Add new TRAU frame sync code

This code is able to detect and sync against a variety of TRAU
frame sync patterns.  Focus is so far on those patterns present on
16k sub-slots, but 8k sub-slots are expected to be supported soon,
too.

A new codebase for this is required as the old OsmoNITB code had
conflated a 16k sub-slot multiplexer with TRAU frame synchronization,
so there was no way to separate those two parts and hence no way to
support 8k sub-slots.

Change-Id: Ia6fe6228b0b8b9a27999f37ce1115ed5558881ea
diff --git a/tests/Makefile.am b/tests/Makefile.am
index bd95cf5..dcd9a4f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -7,7 +7,8 @@
 		  ipa_proxy_test	\
 		  subchan_demux/subchan_demux_test \
 		  ipa_recv/ipa_recv_test \
-		  rtp_test/rtp_test
+		  rtp_test/rtp_test \
+		  trau_sync/trau_sync_test
 
 e1inp_ipa_bsc_test_SOURCES = e1inp_ipa_bsc_test.c
 e1inp_ipa_bsc_test_LDADD = $(top_builddir)/src/libosmoabis.la \
@@ -36,6 +37,10 @@
 rtp_test_rtp_test_LDADD = $(top_builddir)/src/libosmotrau.la \
 			$(LIBOSMOCORE_LIBS)
 
+trau_sync_trau_sync_test_SOURCES = trau_sync/trau_sync_test.c
+trau_sync_trau_sync_test_LDADD = $(top_builddir)/src/libosmotrau.la \
+			$(LIBOSMOCORE_LIBS)
+
 
 # boilerplate for the tests
 # The `:;' works around a Bash 3.2 bug when the output is not writeable.
@@ -59,7 +64,8 @@
 EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) \
 	subchan_demux/subchan_demux_test.ok \
 	ipa_recv/ipa_recv_test.ok \
-	rtp_test/rtp_test.ok
+	rtp_test/rtp_test.ok \
+	trau_sync/trau_sync_test.ok trau_sync/trau_sync_test.err
 
 TESTSUITE = $(srcdir)/testsuite
 
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 5e87248..4faf429 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -29,3 +29,9 @@
 AT_CHECK([$abs_top_builddir/tests/rtp_test/rtp_test], [ignore], [expout])
 AT_CLEANUP
 
+AT_SETUP([trau_sync])
+AT_KEYWORDS([trau_sync])
+cat $abs_srcdir/trau_sync/trau_sync_test.ok > expout
+cat $abs_srcdir/trau_sync/trau_sync_test.err > experr
+AT_CHECK([$abs_top_builddir/tests/trau_sync/trau_sync_test], [0], [expout], [experr])
+AT_CLEANUP
diff --git a/tests/trau_sync/trau_sync_test.c b/tests/trau_sync/trau_sync_test.c
new file mode 100644
index 0000000..392a236
--- /dev/null
+++ b/tests/trau_sync/trau_sync_test.c
@@ -0,0 +1,85 @@
+
+#include <osmocom/core/application.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/bits.h>
+
+#include <osmocom/trau/trau_sync.h>
+
+static void frame_out_cb(void *user_data, const ubit_t *bits, unsigned int num_bits)
+{
+	char *str = user_data;
+	printf("demux_bits_cb '%s': %s\n", str, osmo_ubit_dump(bits, num_bits));
+}
+
+static const uint8_t sync_pattern[] = {
+	0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
+	0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
+	0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
+	0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
+	0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
+};
+
+#define ASSERT_STATE(fi, x) OSMO_ASSERT(!strcmp(osmo_fsm_inst_state_name(fi), x))
+
+static void test_body(void)
+{
+	struct osmo_fsm_inst *fi = osmo_trau_sync_alloc(NULL, "test", frame_out_cb, OSMO_TRAU_SYNCP_16_FR_EFR, "test");
+	OSMO_ASSERT(fi);
+
+	printf("\n==> %s\n", __func__);
+
+	ubit_t bits[40*8];
+
+	/* send some invalid data */
+	memset(bits, 0, sizeof(bits));
+	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
+	osmo_trau_sync_rx_ubits(fi, bits, 23);
+
+	/* first valid frame */
+	osmo_pbit2ubit(bits, sync_pattern, sizeof(sync_pattern)*8);
+	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
+	ASSERT_STATE(fi, "FRAME_ALIGNED");
+
+	/* second valid frame */
+	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
+	ASSERT_STATE(fi, "FRAME_ALIGNED");
+
+	/* send wrong frame */
+	memset(bits, 1, sizeof(bits));
+	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
+	ASSERT_STATE(fi, "FRAME_ALIGNED");
+
+	/* intersperse a valid frame */
+	osmo_pbit2ubit(bits, sync_pattern, sizeof(sync_pattern)*8);
+	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
+
+	/* second wrong frame - but not consecutive */
+	memset(bits, 1, sizeof(bits));
+	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
+	ASSERT_STATE(fi, "FRAME_ALIGNED");
+
+	/* third wrong frame - second consecutive */
+	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
+	ASSERT_STATE(fi, "FRAME_ALIGNED");
+
+	/* only from third consecutive invalid frame onwards we should loose alignment */
+	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
+	ASSERT_STATE(fi, "FRAME_ALIGNMENT_LOST");
+}
+
+
+static const struct log_info_cat default_categories[] = {
+};
+
+const struct log_info log_info = {
+	.cat = default_categories,
+	.num_cat = ARRAY_SIZE(default_categories),
+};
+
+int main(int argc, char **argv)
+{
+	osmo_init_logging2(NULL, NULL);
+	osmo_fsm_log_addr(false);
+	log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
+	test_body();
+}
diff --git a/tests/trau_sync/trau_sync_test.err b/tests/trau_sync/trau_sync_test.err
new file mode 100644
index 0000000..f3eac3c
--- /dev/null
+++ b/tests/trau_sync/trau_sync_test.err
@@ -0,0 +1,15 @@
+<0000> trau_sync(test){WAIT_FRAME_ALIGN}: Allocated
+<0000> trau_sync(test){WAIT_FRAME_ALIGN}: Received Event RX_BITS
+<0000> trau_sync(test){WAIT_FRAME_ALIGN}: Received Event RX_BITS
+<0000> trau_sync(test){WAIT_FRAME_ALIGN}: Received Event RX_BITS
+<0000> trau_sync(test){WAIT_FRAME_ALIGN}: state_chg to FRAME_ALIGNED
+<0000> trau_sync(test){FRAME_ALIGNED}: Received Event RX_BITS
+<0000> trau_sync(test){FRAME_ALIGNED}: Received Event RX_BITS
+<0000> trau_sync(test){FRAME_ALIGNED}: Received Event RX_BITS
+<0000> trau_sync(test){FRAME_ALIGNED}: Received Event RX_BITS
+<0000> trau_sync(test){FRAME_ALIGNED}: Received Event RX_BITS
+<0000> trau_sync(test){FRAME_ALIGNED}: Received Event RX_BITS
+<0000> trau_sync(test){FRAME_ALIGNED}: Received Event RX_BITS
+<0000> trau_sync(test){FRAME_ALIGNED}: state_chg to FRAME_ALIGNMENT_LOST
+<0000> trau_sync(test){FRAME_ALIGNMENT_LOST}: Received Event RX_BITS
+
\ No newline at end of file
diff --git a/tests/trau_sync/trau_sync_test.ok b/tests/trau_sync/trau_sync_test.ok
new file mode 100644
index 0000000..2555913
--- /dev/null
+++ b/tests/trau_sync/trau_sync_test.ok
@@ -0,0 +1,9 @@
+
+==> test_body
+demux_bits_cb 'test': 00000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000
+demux_bits_cb 'test': 00000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000
+demux_bits_cb 'test': 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
+demux_bits_cb 'test': 00000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000001000000000000000
+demux_bits_cb 'test': 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
+demux_bits_cb 'test': 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
+demux_bits_cb 'test':