add BSSLAP coding for Location Services

BSSLAP: there are APDUs transferred in BSSMAP-LE Connection Oriented
Information messages on Lb between BSC and SMLC.
Add BSSLAP coding for these APDU messages:
- TA Layer3
- TA Request
- TA Response, possibly containing Location Estimate coded in GAD
- Reject
- Reset (for intra-BSS handover during TA Request)
- Abort (for inter-BSS handover)

Add encoding and decoding tests.

Change-Id: I6409c4bcac402dc7626a3afce9081c59cd715fe8
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c4e6b9f..fc99485 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -39,6 +39,7 @@
                  i460_mux/i460_mux_test					\
 		 bitgen/bitgen_test					\
 		 gad/gad_test						\
+		 bsslap/bsslap_test					\
 		 $(NULL)
 
 if ENABLE_MSGFILE
@@ -285,6 +286,9 @@
 gad_gad_test_SOURCES = gad/gad_test.c
 gad_gad_test_LDADD = $(LDADD) $(top_builddir)/src/gsm/gad.o
 
+bsslap_bsslap_test_SOURCES = bsslap/bsslap_test.c
+bsslap_bsslap_test_LDADD = $(LDADD) $(top_builddir)/src/gsm/libosmogsm.la
+
 # The `:;' works around a Bash 3.2 bug when the output is not writeable.
 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
 	:;{ \
@@ -366,6 +370,7 @@
 	     i460_mux/i460_mux_test.ok \
 	     bitgen/bitgen_test.ok \
 	     gad/gad_test.ok \
+	     bsslap/bsslap_test.ok \
 	     $(NULL)
 
 if ENABLE_LIBSCTP
diff --git a/tests/bsslap/bsslap_test.c b/tests/bsslap/bsslap_test.c
new file mode 100644
index 0000000..43ea0d4
--- /dev/null
+++ b/tests/bsslap/bsslap_test.c
@@ -0,0 +1,103 @@
+#include <stdio.h>
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/gsm/bsslap.h>
+
+struct bsslap_pdu bsslap_test_pdus[] = {
+	{
+		.msg_type = BSSLAP_MSGT_TA_REQUEST,
+	},
+	{
+		.msg_type = BSSLAP_MSGT_TA_RESPONSE,
+		.ta_response = {
+			.cell_id = 23,
+			.ta = 42,
+		},
+	},
+	{
+		.msg_type = BSSLAP_MSGT_REJECT,
+		.reject = BSSLAP_CAUSE_OTHER_RADIO_EVT_FAIL,
+	},
+	{
+		.msg_type = BSSLAP_MSGT_RESET,
+		.reset = {
+			.cell_id = 23,
+			.ta = 42,
+			.chan_desc =  {
+				.chan_nr = 23,
+				.h0 = {
+					.tsc = 5,
+					.h = 1,
+					.arfcn_high = 2,
+					.arfcn_low = 3,
+				},
+			},
+			.cause = BSSLAP_CAUSE_INTRA_BSS_HO,
+		},
+	},
+	{
+		.msg_type = BSSLAP_MSGT_ABORT,
+		.abort = BSSLAP_CAUSE_LOSS_SIG_CONN_MS,
+	},
+	{
+		.msg_type = BSSLAP_MSGT_TA_LAYER3,
+		.ta_layer3 = {
+			.ta = 23,
+		},
+	},
+};
+
+void test_bsslap_enc_dec()
+{
+	struct bsslap_pdu *pdu;
+	printf("--- %s\n", __func__);
+
+	for (pdu = bsslap_test_pdus; (pdu - bsslap_test_pdus) < ARRAY_SIZE(bsslap_test_pdus); pdu++) {
+		struct msgb *msg = msgb_alloc(1024, __func__);
+		struct bsslap_pdu dec_pdu;
+		struct osmo_bsslap_err *err;
+		int rc;
+		void *loop_ctx = msg;
+		rc = osmo_bsslap_enc(msg, pdu);
+		if (rc <= 0) {
+			printf("[%ld] %s: ERROR: failed to encode pdu\n", (pdu - bsslap_test_pdus),
+			       osmo_bsslap_msgt_name(pdu->msg_type));
+			goto loop_end;
+		}
+		if (rc != msg->len) {
+			printf("[%ld] %s: ERROR: osmo_bsslap_enc() returned length %d but msgb has %d bytes\n",
+			       (pdu - bsslap_test_pdus), osmo_bsslap_msgt_name(pdu->msg_type),
+			       rc, msg->len);
+			goto loop_end;
+		}
+
+		memset(&dec_pdu, 0xff, sizeof(dec_pdu));
+		rc = osmo_bsslap_dec(&dec_pdu, &err, loop_ctx, msg->data, msg->len);
+		if (rc) {
+			printf("[%ld] %s: ERROR: failed to decode pdu: %s\n", (pdu - bsslap_test_pdus),
+			       osmo_bsslap_msgt_name(pdu->msg_type), err->logmsg);
+			printf("     encoded data: %s\n", osmo_hexdump(msg->data, msg->len));
+			goto loop_end;
+		}
+
+		if (memcmp(pdu, &dec_pdu, sizeof(dec_pdu))) {
+			printf("[%ld] %s: ERROR: decoded PDU != encoded PDU\n", (pdu - bsslap_test_pdus),
+			       osmo_bsslap_msgt_name(pdu->msg_type));
+			printf("     original struct: %s\n", osmo_hexdump((void*)pdu, sizeof(*pdu)));
+			printf("      decoded struct: %s\n", osmo_hexdump((void*)&dec_pdu, sizeof(dec_pdu)));
+			goto loop_end;
+		}
+
+		printf("[%ld] %s: ok\n", (pdu - bsslap_test_pdus), osmo_bsslap_msgt_name(pdu->msg_type));
+
+loop_end:
+		msgb_free(msg);
+	}
+}
+
+int main()
+{
+	test_bsslap_enc_dec();
+	return 0;
+}
diff --git a/tests/bsslap/bsslap_test.ok b/tests/bsslap/bsslap_test.ok
new file mode 100644
index 0000000..f3199e1
--- /dev/null
+++ b/tests/bsslap/bsslap_test.ok
@@ -0,0 +1,7 @@
+--- test_bsslap_enc_dec
+[0] TA Request: ok
+[1] TA Response: ok
+[2] Reject: ok
+[3] Reset: ok
+[4] Abort: ok
+[5] TA Layer3: ok
diff --git a/tests/testsuite.at b/tests/testsuite.at
index e29c131..0923c25 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -409,3 +409,9 @@
 cat $abs_srcdir/gad/gad_test.ok > expout
 AT_CHECK([$abs_top_builddir/tests/gad/gad_test], [0], [expout], [ignore])
 AT_CLEANUP
+
+AT_SETUP([bsslap])
+AT_KEYWORDS([bsslap])
+cat $abs_srcdir/bsslap/bsslap_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/bsslap/bsslap_test], [0], [expout], [ignore])
+AT_CLEANUP