lapdm: fix SAPI-0/SAPI-3 frame prioritization on DCCH

According to 3GPP TS 44.005, section 4.2.2 "Priority":

  a) on DCCH, a SAPI=0 frame always has higher priority than SAPI=3;
  b) on ACCH, the priority arrangement is more complex:
    b1) if a SAPI = 3 frame is awaiting transmission, two SAPI=0
        frames shall not be sent in consecutive SACCH frames;
    b2) on the network side (LAPDM_MODE_BTS), it must also be ensured
        that any SAPI=3 frame is followed by at least one SAPI=0 frame;
    b3) a SAPI = 0 frame may be repeated in the next SACCH period
        if the Repeated SACCH is supported (see 3GPP TS 44.006, section 11).

We definitely need to extend our testing coverage to ensure that
we implement b) correctly, but for now let's focus on DCCH:

  a) for DCCH, ensure that SAPI=0 frames preceed SAPI=3 ones;
  b) for ACCH, re-use the existing round-robin implementation.

Change-Id: Ia3780bce1222b312ae2fd2d21496a4d6c5ccb6e0
Related: SYS#5047, OS#4731
diff --git a/src/gsm/lapdm.c b/src/gsm/lapdm.c
index 21227e3..bfd6d26 100644
--- a/src/gsm/lapdm.c
+++ b/src/gsm/lapdm.c
@@ -380,7 +380,21 @@
 	return le->l1_prim_cb(&pp.oph, le->l1_ctx);
 }
 
-static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
+/* Dequeue a Downlink message for DCCH (dedicated channel) */
+static struct msgb *tx_dequeue_dcch_msgb(struct lapdm_entity *le)
+{
+	struct msgb *msg;
+
+	/* SAPI=0 always has higher priority than SAPI=3 */
+	msg = msgb_dequeue(&le->datalink[DL_SAPI0].dl.tx_queue);
+	if (msg == NULL) /* no SAPI=0 messages, dequeue SAPI=3 (if any) */
+		msg = msgb_dequeue(&le->datalink[DL_SAPI3].dl.tx_queue);
+
+	return msg;
+}
+
+/* Dequeue a Downlink message for ACCH (associated channel) */
+static struct msgb *tx_dequeue_acch_msgb(struct lapdm_entity *le)
 {
 	struct lapdm_datalink *dl;
 	int last = le->last_tx_dequeue;
@@ -411,7 +425,12 @@
 	struct msgb *msg;
 	uint8_t pad;
 
-	msg = tx_dequeue_msgb(le);
+	/* Dequeue depending on channel type: DCCH or ACCH.
+	 * See 3GPP TS 44.005, section 4.2.2 "Priority". */
+	if (le == &le->lapdm_ch->lapdm_dcch)
+		msg = tx_dequeue_dcch_msgb(le);
+	else
+		msg = tx_dequeue_acch_msgb(le);
 	if (!msg)
 		return -ENODEV;
 
diff --git a/tests/lapd/lapd_test.ok b/tests/lapd/lapd_test.ok
index 5b95195..065886c 100644
--- a/tests/lapd/lapd_test.ok
+++ b/tests/lapd/lapd_test.ok
@@ -131,13 +131,13 @@
 bts_to_ms_dummy_tx_cb: MS->BTS(us) message 22
 BTS is establishing a SAPI=3 link
 lapdm_phsap_dequeue_prim(): got rc 0: Success
-Took message from DCCH queue: L2 header size 3, L3 size 20, SAP 0x1000000, 0/0, Link 0x03
-Message: [L2]> 0f 3f 01 [L3]> 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 
-lapdm_phsap_dequeue_prim(): got rc 0: Success
 Took message from DCCH queue: L2 header size 3, L3 size 20, SAP 0x1000000, 0/0, Link 0x00
 Message: [L2]> 01 73 35 [L3]> 06 27 07 03 50 58 92 05 f4 44 59 ba 63 2b 2b 2b 2b 2b 2b 2b 
-Checking the func=UA message: FAIL
-Checking the func=SABM message: FAIL
+lapdm_phsap_dequeue_prim(): got rc 0: Success
+Took message from DCCH queue: L2 header size 3, L3 size 20, SAP 0x1000000, 0/0, Link 0x03
+Message: [L2]> 0f 3f 01 [L3]> 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 
+Checking the func=UA message: OK
+Checking the func=SABM message: OK
 
 Checking whether the DCCH/SACCH queues are empty
 lapdm_phsap_dequeue_prim(): got rc -19: No such device