coding: implement dedicated codec API for FACCH/[FH]

Currently FACCH/[FH] encoding and decoding is implemented as part of
the gsm0503_tch_[fh]r_{en,de}code and gsm0503_tch_a[fh]s_{en,de}code
API.  This works fine for speech because one FACCH frame completely
replaces one or two speech frames, but this is not the case for CSD.

According to 3GPP TS 45.002, sections 4.2.5 and 4.3.5, for TCH data
channels FACCH does not replace data frames but disturbs some amount
of bits from them.  Therefore we need to be able to perform FACCH
encoding and decoding independently from CSD specific coding API.

Change-Id: I0c7a9c180dcafe64e6aebe53518d3d11e1f29886
Related: OS#1572
diff --git a/tests/coding/coding_test.c b/tests/coding/coding_test.c
index a13dc4c..e8dbe83 100644
--- a/tests/coding/coding_test.c
+++ b/tests/coding/coding_test.c
@@ -24,6 +24,7 @@
 #include <osmocom/core/bits.h>
 #include <osmocom/core/utils.h>
 
+#include <osmocom/gsm/protocol/gsm_04_08.h>
 #include <osmocom/coding/gsm0503_coding.h>
 
 #define DUMP_U_AT(b, x, u) do {						\
@@ -312,6 +313,51 @@
 	printf("\n");
 }
 
+static void test_facch(const uint8_t *data, bool half_rate)
+{
+	ubit_t bursts_u[116 * 8 * 2] = { 0 };
+	sbit_t bursts_s[116 * 8 * 2] = { 0 };
+	int rc;
+
+	/* Encode the given FACCH message three times (at different offsets) */
+	printf("%s(FACCH/%c): encoding: %s\n",
+	       __func__, half_rate ? 'H' : 'F',
+	       osmo_hexdump(&data[0], GSM_MACBLOCK_LEN));
+	for (unsigned int i = 0; i < 3; i++) {
+		ubit_t *pos = &bursts_u[116 * 4 * i];
+
+		if (half_rate)
+			rc = gsm0503_tch_hr_facch_encode(pos, &data[0]);
+		else
+			rc = gsm0503_tch_fr_facch_encode(pos, &data[0]);
+		CHECK_RC_OR_RET(rc == 0, "encoding");
+	}
+
+	/* Prepare soft-bits */
+	osmo_ubit2sbit(bursts_s, bursts_u, sizeof(bursts_s));
+
+	/* Decode three FACCH messages (at different offsets) */
+	for (unsigned int i = 0; i < 3; i++) {
+		const sbit_t *pos = &bursts_s[116 * 4 * i];
+		uint8_t result[GSM_MACBLOCK_LEN];
+		int n_errors, n_bits_total;
+
+		if (half_rate)
+			rc = gsm0503_tch_hr_facch_decode(&result[0], pos,
+							 &n_errors, &n_bits_total);
+		else
+			rc = gsm0503_tch_fr_facch_decode(&result[0], pos,
+							 &n_errors, &n_bits_total);
+		CHECK_RC_OR_RET(rc == GSM_MACBLOCK_LEN, "decoding");
+
+		printf("%s(FACCH/%c): decoded (BER=%d/%d): %s\n",
+		       __func__, half_rate ? 'H' : 'F', n_errors, n_bits_total,
+		       osmo_hexdump(result, GSM_MACBLOCK_LEN));
+	}
+
+	printf("\n");
+}
+
 struct test_macblock {
 	bool is_egprs;
 	uint16_t exp_burst_bits;
@@ -635,6 +681,13 @@
 		}
 	}
 
+	printf("\nTesting FACCH/F codec:\n");
+	for (i = 0; i < ARRAY_SIZE(test_l2); i++)
+		test_facch(test_l2[i], false);
+	printf("\nTesting FACCH/H codec:\n");
+	for (i = 0; i < ARRAY_SIZE(test_l2); i++)
+		test_facch(test_l2[i], true);
+
 	printf("\nTesting CSD functions:\n");
 	for (i = 0; i < ARRAY_SIZE(csd_tests); i++)
 		test_csd(&csd_tests[i]);