gsm0808_utils: fix gsm48 multirate configuration generator

The function gsm0808_sc_cfg_from_gsm48_mr_cfg() takes an S15 to S0
bitmask and converts that bitmask into an AMR multirate configuration
struct.

Unfortunately the current implementation implements 3GPP TS 28.062,
Table 7.11.3.1.3-2 wrongly in some aspects. Lets fix this.

- Fix wrong interpretation of the bitpatterns
- 5,15K is invalid and must never be selected
- Make sure that no more than 4 rates are selected in the active set
- Extend unit-test

Change-Id: I6fd7f4073b84093742c322752f2fd878d1071e15
Related: SYS#4470
diff --git a/tests/gsm0808/gsm0808_test.c b/tests/gsm0808/gsm0808_test.c
index c448f43..87f5d2a 100644
--- a/tests/gsm0808/gsm0808_test.c
+++ b/tests/gsm0808/gsm0808_test.c
@@ -1907,12 +1907,13 @@
 static void test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(uint16_t s15_s0)
 {
 	struct gsm48_multi_rate_conf cfg;
+	int rc;
 
 	printf("Input:\n");
 	printf(" S15-S0 = %04x = 0b" OSMO_BIN_SPEC OSMO_BIN_SPEC "\n", s15_s0,
 	       OSMO_BIN_PRINT(s15_s0 >> 8), OSMO_BIN_PRINT(s15_s0));
 
-	gsm48_mr_cfg_from_gsm0808_sc_cfg(&cfg, s15_s0);
+	rc = gsm48_mr_cfg_from_gsm0808_sc_cfg(&cfg, s15_s0);
 
 	printf("Output:\n");
 	printf(" m4_75= %u   smod=  %u\n", cfg.m4_75, cfg.smod);
@@ -1924,6 +1925,9 @@
 	printf(" m10_2= %u\n", cfg.m10_2);
 	printf(" m12_2= %u\n", cfg.m12_2);
 
+	if (rc != 0)
+		printf(" Result invalid!\n");
+
 	printf("\n");
 }
 
@@ -1931,7 +1935,8 @@
 {
 	printf("Testing gsm48_mr_cfg_from_gsm0808_sc_cfg():\n");
 
-	/* Only one codec per setting */
+	/* Test with settings as defined in 3GPP TS 28.062, Table 7.11.3.1.3-2,
+	 * (up to four codecs may become selected) */
 	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
 	    (GSM0808_SC_CFG_DEFAULT_AMR_4_75);
 	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
@@ -1949,15 +1954,40 @@
 	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
 	    (GSM0808_SC_CFG_DEFAULT_AMR_12_2);
 
-	/* Combinations */
+	/* Test with settings as defined in 3GPP TS 28.062, Table 7.11.3.1.3-2,
+	 * but pick only one distinctive setting at a time */
+	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_4_75);
 	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
-	    (GSM0808_SC_CFG_DEFAULT_AMR_4_75 | GSM0808_SC_CFG_DEFAULT_AMR_6_70 |
-	     GSM0808_SC_CFG_DEFAULT_AMR_10_2);
+	    (GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20);
+	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_5_90);
+	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_6_70);
+	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_40);
+	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_95);
+	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_10_2);
+	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_12_2);
+
+	/* Arbitrary, but valid combinations */
+	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_40 |
+						     GSM0808_SC_CFG_AMR_6_70 |
+						     GSM0808_SC_CFG_AMR_10_2);
+	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_95 |
+						     GSM0808_SC_CFG_AMR_4_75);
+	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_5_90 |
+						     GSM0808_SC_CFG_AMR_12_2);
 	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
-	    (GSM0808_SC_CFG_DEFAULT_AMR_10_2 | GSM0808_SC_CFG_DEFAULT_AMR_12_2 |
-	     GSM0808_SC_CFG_DEFAULT_AMR_7_40);
+	    (GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20 | GSM0808_SC_CFG_AMR_5_90 |
+	     GSM0808_SC_CFG_AMR_12_2);
+
+	/* Invalid combinations */
 	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single
-	    (GSM0808_SC_CFG_DEFAULT_AMR_7_95 | GSM0808_SC_CFG_DEFAULT_AMR_12_2);
+	    (GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20 | GSM0808_SC_CFG_AMR_6_70);
+	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(GSM0808_SC_CFG_AMR_7_40 |
+						     GSM0808_SC_CFG_AMR_6_70 |
+						     GSM0808_SC_CFG_AMR_10_2 |
+						     GSM0808_SC_CFG_AMR_7_95 |
+						     GSM0808_SC_CFG_AMR_4_75);
+	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(0x0000);
+	test_gsm48_mr_cfg_from_gsm0808_sc_cfg_single(0xffff);
 }
 
 struct test_cell_id_matching_data {
@@ -2359,7 +2389,6 @@
 	test_gsm0808_enc_dec_cell_id_ci();
 	test_gsm0808_enc_dec_cell_id_lac_and_ci();
 	test_gsm0808_enc_dec_cell_id_global();
-
 	test_gsm0808_sc_cfg_from_gsm48_mr_cfg();
 	test_gsm48_mr_cfg_from_gsm0808_sc_cfg();