Modern Apps git

← Modern-Apps.git

maauto: drain open queue on bare 0xff, try service 1

Commit
25d0ad0096e0759e9c996bbf908b1f2adbc66f4a
Author
vayun-mathur <[email protected]>
When
2026-09-12 14:54 UTC (2 months ago)
Parents
a2894f0

Diff

auto/protocol/src/main/java/com/vayunmathur/auto/protocol/GalControlSession.kt
diff --git a/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/GalControlSession.kt b/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/GalControlSession.ktindex 1b668fbc8d..bfe19df0ed 100644--- a/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/GalControlSession.kt+++ b/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/GalControlSession.kt@@ -122,7 +122,12 @@ class GalControlSession(         // The payload names the offending message; the trace in GalConnection         // prints it. Killing the session here is what turned a recoverable         // per-message rejection into a dead bring-up.-        GalMessage.Control.MESSAGE_ERROR -> emptyList()+        //+        // When the error is bare (empty payload, no channel context) and an open+        // is in flight, it is that open's refusal: DHU 2.0 answers this way+        // instead of sending 0x8. Draining the queue lets the driver move on to+        // the next service instead of stalling with a phantom pending open.+        GalMessage.Control.MESSAGE_ERROR -> onMessageError()          else -> fail("unexpected control message type 0x${type.toString(16)}")     }@@ -247,20 +252,41 @@ class GalControlSession(      * A refusal is recorded, not fatal: the HU answers some opens with an empty      * MessageError and never sends 0x8 at all (DHU 2.0 vs video service 2), and      * the bring-up must continue with the remaining services. The driver moves-     * on when the refusal is visible via [refusedChannels] or a bare 0xff.+     * on when the refusal is visible via [refusedChannels] or a bare 0xff+     * (see [onMessageError], which drains the same queue).      */     private fun onChannelOpen(payload: ByteArray): List<OutboundMessage> {         val status = ChannelOpenResponse.parseFrom(payload).status-        val id = pendingChannels.firstOrNull() ?: return emptyList()-        pendingChannels -= id         if (status != MessageStatus.STATUS_SUCCESS.number) {-            refusedChannels += id+            drainPendedToRefused()             return emptyList()         }+        val id = pendingChannels.firstOrNull() ?: return emptyList()+        pendingChannels -= id         openChannels += id         return emptyList()     } +    /**+     * Records a bare MessageError as the refusal of the in-flight open.+     *+     * DHU 2.0 rejects some channel opens with an empty 0xff that names no+     * channel, instead of a 0x8 refusal. Without this the queue stalls: the+     * open stays "pending" forever and no further 0x7 ever goes out (Run 4).+     * The head of the queue is what the error must belong to -- nothing else+     * is in flight, since the driver sends one open at a time.+     */+    fun onMessageError(): List<OutboundMessage> {+        drainPendedToRefused()+        return emptyList()+    }++    private fun drainPendedToRefused() {+        val id = pendingChannels.firstOrNull() ?: return+        pendingChannels -= id+        refusedChannels += id+    }+     private fun onPing(payload: ByteArray): List<OutboundMessage> = listOf(         OutboundMessage(             type = GalMessage.Control.PING_RESPONSE,
auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalControlSessionTest.kt
diff --git a/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalControlSessionTest.kt b/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalControlSessionTest.ktindex 082246cda7..60f14f7a22 100644--- a/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalControlSessionTest.kt+++ b/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalControlSessionTest.kt@@ -259,20 +259,22 @@ class GalControlSessionTest {     }      @Test-    fun `sequential opens track one flight at a time`() {+    fun `a bare message error refuses the in-flight open`() {+        // Run 4: DHU 2.0 answers the 0x7 with an empty 0xff and never sends 0x8.+        // The queue must drain so the driver moves on to the next service.         val session = session(GalCredential.serverEngine(context()))         val video = Service.newBuilder().setId(GalService.VIDEO_SINK.id).build()         val input = Service.newBuilder().setId(GalService.INPUT_SOURCE.id).build()         session.openChannel(video)         session.openChannel(input) -        // First answer drains the head of the queue.-        session.onMessage(-            GalMessage.Control.CHANNEL_OPEN_RESPONSE,-            ChannelOpenResponse.newBuilder().setStatus(0).build().toByteArray(),-        )-        assertEquals(setOf(GalService.VIDEO_SINK.id), session.openChannels)+        val replies = session.onMessage(GalMessage.Control.MESSAGE_ERROR, byteArrayOf())++        assertTrue(replies.isEmpty())+        assertEquals(setOf(GalService.VIDEO_SINK.id), session.refusedChannels)         assertEquals(listOf(GalService.INPUT_SOURCE.id), session.pendingChannels)+        assertTrue(session.openChannels.isEmpty())+        assertNull(session.failure)     }      @Test
auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt b/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.ktindex 143e5b85a7..89eab5eb0c 100644--- a/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt+++ b/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt@@ -96,15 +96,21 @@ class ProjectionService : Service() {      * Opens the next not-yet-attempted advertised service, in the HU's wire order.      *      * Only one open is ever in flight: while `pendingChannels` is non-empty the-     * head unit still owes us an answer, so wait. Channel 0 (control) is local-     * -- gearhead creates it without any 0x7 -- so it is skipped, never sent.+     * head unit still owes us an answer, so wait.+     *+     * Service 1 IS sent, first: gearhead opens channel 0 locally and never sends+     * a 0x7 for it, but DHU 2.0 rejects a first-0x7 for service 2 with an empty+     * 0xff and no 0x8 (Run 4) -- so the HU-side may expect the wire-order+     * sequence to start at 1. If the HU 0xffs/0x8-refuses service 1 as well, it+     * lands in refusedChannels like any other refusal and the bring-up moves on+     * to service 2; the experiment then distinguishes "first-open must be 1"+     * (0x8 arrives for 1) from "video refused regardless" (both refused).      */     private fun openNext(connection: GalConnection) {         val session = connection.session         if (session.pendingChannels.isNotEmpty()) return         val next = session.services.firstOrNull {-            it.id != CONTROL_SERVICE_ID &&-                it.id !in session.openChannels &&+            it.id !in session.openChannels &&                 it.id !in session.refusedChannels         } ?: return         connection.send(session.openChannel(next))@@ -135,9 +141,6 @@ class ProjectionService : Service() {         private const val CHANNEL_ID = "projection"         private const val NOTIFICATION_ID = 1 -        /** Service 1 is the control channel: opened locally, never via 0x7. */-        private const val CONTROL_SERVICE_ID = 1-         fun start(context: Context) {             context.startForegroundService(Intent(context, ProjectionService::class.java))         }