Modern Apps git

← Modern-Apps.git

maauto: open channels sequentially in HU wire order

Commit
9843e0c47b4767c56522059c56dec208b4b44e37
Author
vayun-mathur <[email protected]>
When
2026-09-12 14:50 UTC (2 months ago)
Parents
21becb2

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 7f657af610..1b668fbc8d 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@@ -79,7 +79,17 @@ class GalControlSession(     var openChannels: Set<Int> = emptySet()         private set -    private var pendingChannel: Int = -1+    /**+     * Service ids the head unit rejected with a non-SUCCESS ChannelOpenResponse.+     * Kept so the driver can move on to the next service instead of retrying a+     * refused open forever.+     */+    var refusedChannels: Set<Int> = emptySet()+        private set++    /** Ids with an open request in flight, in the order requested. */+    var pendingChannels: List<Int> = emptyList()+        private set      private var tlsInbound: ByteBuffer = ByteBuffer.allocate(0) @@ -122,11 +132,12 @@ class GalControlSession(      *      * The channel opens asynchronously: the head unit answers with      * ChannelOpenResponse, and [openChannels] gains the id only on-     * STATUS_SUCCESS. Nothing may be sent on the channel before that ---     * gearhead's `jdk.Q()` sends media setup from `onChannelOpened`.+     * STATUS_SUCCESS ([refusedChannels] on anything else). Nothing may be sent+     * on the channel before that -- gearhead's `jdk.Q()` sends media setup from+     * `onChannelOpened`.      */     fun openChannel(service: Service, priority: Int = 0): OutboundMessage {-        pendingChannel = service.id+        pendingChannels += service.id         return OutboundMessage(             type = GalMessage.Control.CHANNEL_OPEN_REQUEST,             payload = ChannelOpenRequest.newBuilder()@@ -232,14 +243,21 @@ class GalControlSession(      * the driver must wait for STATUS_SUCCESS here before sending anything on      * the new channel. Sending service messages (e.g. media setup) against a      * channel the head unit has not opened earns a bare MessageError (0xff).+     *+     * 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.      */     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) {-            return fail("head unit refused channel open: status $status")+            refusedChannels += id+            return emptyList()         }-        openChannels += pendingChannel-        pendingChannel = -1+        openChannels += id         return emptyList()     } 
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 a3466d0651..4cf33306e8 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@@ -218,19 +218,42 @@ class GalControlSessionTest {     }      @Test-    fun `a refused channel open fails the session`() {+    fun `a refused channel open is recorded, not fatal`() {+        // DHU 2.0 answers some opens with an empty MessageError and never sends+        // 0x8 at all; other times 0x8 carries non-SUCCESS. Either way the+        // bring-up continues with the remaining services.         val session = session(GalCredential.serverEngine(context()))         val service = Service.newBuilder().setId(GalService.VIDEO_SINK.id).build()         session.openChannel(service)+        assertEquals(listOf(GalService.VIDEO_SINK.id), session.pendingChannels)          session.onMessage(             GalMessage.Control.CHANNEL_OPEN_RESPONSE,             ChannelOpenResponse.newBuilder().setStatus(-4).build().toByteArray(),         ) -        assertEquals(SessionState.CLOSED, session.state)         assertTrue(session.openChannels.isEmpty())-        assertNotNull(session.failure)+        assertEquals(setOf(GalService.VIDEO_SINK.id), session.refusedChannels)+        assertTrue(session.pendingChannels.isEmpty())+        assertNull(session.failure)+        assertEquals(SessionState.DISCOVERING, session.state)+    }++    @Test+    fun `sequential opens track one flight at a time`() {+        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)+        assertEquals(listOf(GalService.INPUT_SOURCE.id), session.pendingChannels)     }      @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 211ac011af..143e5b85a7 100644--- a/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt+++ b/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt@@ -67,16 +67,17 @@ class ProjectionService : Service() {                 trace = { Log.d(TAG, it) },             ) -            var openedVideo = false             var setupVideo = false             while (running && connection.pump()) {-                // Opening the video channel is the first thing to do once discovery lands.-                if (!openedVideo && connection.session.state == SessionState.ACTIVE) {-                    openedVideo = openVideo(connection)+                // Open every advertised service in HU-discovery wire order, one at a+                // time: the next 0x7 goes out only once the previous channel is+                // granted (openChannels) or refused (refusedChannels, or a bare+                // 0xff with no 0x8 -- DHU 2.0 answers that way). gearhead opens in+                // this same order (`jbp.i[]` follows the `xob.c` wire order).+                // Video setup additionally waits for its own channel grant.+                if (connection.session.state == SessionState.ACTIVE) {+                    openNext(connection)                 }-                // Media setup only AFTER the head unit's ChannelOpenResponse grants the-                // channel (gearhead's `jdk.Q()` sends setup from `onChannelOpened`).-                // Setting up against a not-yet-open channel earns MessageError (0xff).                 if (!setupVideo && GalService.VIDEO_SINK.id in connection.session.openChannels) {                     setupVideo = true                     video?.requestSetup()@@ -91,17 +92,26 @@ class ProjectionService : Service() {      private var video: VideoSinkChannel? = null -    /** @return true once the request is away, so it is only sent once. */-    private fun openVideo(connection: GalConnection): Boolean {-        val service = connection.session.services-            .firstOrNull { it.id == GalService.VIDEO_SINK.id && it.hasMediaSink() }-        if (service == null) {-            Log.w(TAG, "head unit advertised no video sink; nothing to project onto")-            return true+    /**+     * 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.+     */+    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.refusedChannels+        } ?: return+        connection.send(session.openChannel(next))+        Log.i(TAG, "requesting channel open for service ${next.id}")+        if (next.id == GalService.VIDEO_SINK.id && next.hasMediaSink()) {+            video = VideoSinkChannel(this, next, connection)         }-        connection.send(connection.session.openChannel(service))-        video = VideoSinkChannel(this, service, connection)-        return true     }      private fun notification(): Notification {@@ -125,6 +135,9 @@ 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))         }