GSM 04.08: Add support for parsing CSD related bearer capabilities

Also adds a test case for both encoder and decoder of this IE
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 308c691..d2c9e5c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -3,7 +3,7 @@
 check_PROGRAMS = timer/timer_test sms/sms_test ussd/ussd_test		\
                  smscb/smscb_test bits/bitrev_test a5/a5_test		\
                  conv/conv_test auth/milenage_test lapd/lapd_test	\
-                 gsm0808/gsm0808_test
+                 gsm0808/gsm0808_test gsm0408/gsm0408_test
 if ENABLE_MSGFILE
 check_PROGRAMS += msgfile/msgfile_test
 endif
@@ -23,6 +23,9 @@
 gsm0808_gsm0808_test_SOURCES = gsm0808/gsm0808_test.c
 gsm0808_gsm0808_test_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/src/gsm/libosmogsm.la
 
+gsm0408_gsm0408_test_SOURCES = gsm0408/gsm0408_test.c
+gsm0408_gsm0408_test_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/src/gsm/libosmogsm.la
+
 lapd_lapd_test_SOURCES = lapd/lapd_test.c
 lapd_lapd_test_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/src/gsm/libosmogsm.la
 
diff --git a/tests/gsm0408/gsm0408_test.c b/tests/gsm0408/gsm0408_test.c
new file mode 100644
index 0000000..077063b
--- /dev/null
+++ b/tests/gsm0408/gsm0408_test.c
@@ -0,0 +1,133 @@
+/*
+ * (C) 2012 by Harald Welte <laforge@gnumonks.org>
+ * All Rights Reserved
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <string.h>
+#include <stdio.h>
+
+#include <osmocom/gsm/protocol/gsm_04_08.h>
+#include <osmocom/gsm/gsm48_ie.h>
+#include <osmocom/gsm/mncc.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/msgb.h>
+
+
+static const uint8_t csd_9600_v110_lv[] = { 0x07, 0xa1, 0xb8, 0x89, 0x21, 0x15, 0x63, 0x80 };
+
+static const struct gsm_mncc_bearer_cap bcap_csd_9600_v110 = {
+	.transfer =	GSM48_BCAP_ITCAP_UNR_DIG_INF,
+	.mode =		GSM48_BCAP_TMOD_CIRCUIT,
+	.coding =	GSM48_BCAP_CODING_GSM_STD,
+	.radio =	GSM48_BCAP_RRQ_FR_ONLY,
+	.speech_ver[0]=	-1,
+	.data = {
+		.rate_adaption =	GSM48_BCAP_RA_V110_X30,
+		.sig_access =		GSM48_BCAP_SA_I440_I450,
+		.async =		1,
+		.nr_stop_bits =		1,
+		.nr_data_bits =		8,
+		.user_rate =		GSM48_BCAP_UR_9600,
+		.parity =		GSM48_BCAP_PAR_NONE,
+		.interm_rate =		GSM48_BCAP_IR_16k,
+		.transp =		GSM48_BCAP_TR_TRANSP,
+		.modem_type =		GSM48_BCAP_MT_NONE,
+	},
+};
+
+static const uint8_t speech_all_lv[] = { 0x06, 0x60, 0x04, 0x02, 0x00, 0x05, 0x81 };
+
+static const struct gsm_mncc_bearer_cap bcap_speech_all = {
+	.transfer =	GSM48_BCAP_ITCAP_SPEECH,
+	.mode =		GSM48_BCAP_TMOD_CIRCUIT,
+	.coding =	GSM48_BCAP_CODING_GSM_STD,
+	.radio =	GSM48_BCAP_RRQ_DUAL_FR,
+	.speech_ver = {
+		4, 2, 0, 5, 1, -1,
+	},
+};
+
+
+struct bcap_test {
+	const uint8_t *lv;
+	const struct gsm_mncc_bearer_cap *bc;
+	const char *name;
+};
+
+static const struct bcap_test bcap_tests[] = {
+	{ csd_9600_v110_lv, &bcap_csd_9600_v110, "CSD 9600/V.110/transparent" },
+	{ speech_all_lv, &bcap_speech_all, "Speech, all codecs" },
+};
+
+static int test_bearer_cap()
+{
+	struct gsm_mncc_bearer_cap bc;
+	int i, rc;
+
+	for (i = 0; i < ARRAY_SIZE(bcap_tests); i++) {
+		struct msgb *msg = msgb_alloc(100, "test");
+		int lv_len;
+
+		memset(&bc, 0, sizeof(bc));
+
+		/* test decoding */
+		rc = gsm48_decode_bearer_cap(&bc, bcap_tests[i].lv);
+		if (rc < 0) {
+			fprintf(stderr, "Error decoding %s\n",
+				bcap_tests[i].name);
+			return rc;
+		}
+		if (memcmp(&bc, bcap_tests[i].bc, sizeof(bc))) {
+			fprintf(stderr, "Incorrect decoded result of %s:\n",
+				bcap_tests[i].name);
+			fprintf(stderr, " should: %s\n",
+				osmo_hexdump((uint8_t *) bcap_tests[i].bc, sizeof(bc)));
+			fprintf(stderr, " is:     %s\n",
+				osmo_hexdump((uint8_t *) &bc, sizeof(bc)));
+			return -1;
+		}
+
+		/* also test re-encode? */
+		rc = gsm48_encode_bearer_cap(msg, 1, &bc);
+		if (rc < 0) {
+			fprintf(stderr, "Error encoding %s\n",
+				bcap_tests[i].name);
+			return rc;
+		}
+		lv_len = bcap_tests[i].lv[0]+1;
+		if (memcmp(msg->data, bcap_tests[i].lv, lv_len)) {
+			fprintf(stderr, "Incorrect encoded result of %s:\n",
+				bcap_tests[i].name);
+			fprintf(stderr, " should: %s\n",
+				osmo_hexdump(bcap_tests[i].lv, lv_len));
+			fprintf(stderr, " is:     %s\n",
+				osmo_hexdump(msg->data, msg->len));
+			return -1;
+		}
+
+		printf("Test `%s' passed\n", bcap_tests[i].name);
+		msgb_free(msg);
+	}
+
+	return 0;
+}
+
+int main(int argc, char **argv)
+{
+	test_bearer_cap();
+}
diff --git a/tests/gsm0408/gsm0408_test.ok b/tests/gsm0408/gsm0408_test.ok
new file mode 100644
index 0000000..5ce19e6
--- /dev/null
+++ b/tests/gsm0408/gsm0408_test.ok
@@ -0,0 +1,2 @@
+Test `CSD 9600/V.110/transparent' passed
+Test `Speech, all codecs' passed
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 69624c1..2154269 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -71,3 +71,9 @@
 cat $abs_srcdir/gsm0808/gsm0808_test.ok > expout
 AT_CHECK([$abs_top_builddir/tests/gsm0808/gsm0808_test], [], [expout], [ignore])
 AT_CLEANUP
+
+AT_SETUP([gsm0408])
+AT_KEYWORDS([gsm0408])
+cat $abs_srcdir/gsm0408/gsm0408_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/gsm0408/gsm0408_test], [], [expout], [ignore])
+AT_CLEANUP