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;