Modern Apps git

← Modern-Apps.git

maauto: frame channel-open on its target channel

Commit
eb102d7cfd2528002a0c3e3a67cb1a8fd62af283
Author
vayun-mathur <[email protected]>
When
2026-09-12 16:15 UTC (2 months ago)
Parents
41bef7f

Diff

auto/protocol/src/main/java/com/vayunmathur/auto/protocol/GalConnection.kt
diff --git a/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/GalConnection.kt b/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/GalConnection.ktindex 3e10fd4813..89e410e141 100644--- a/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/GalConnection.kt+++ b/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/GalConnection.kt@@ -70,9 +70,16 @@ class GalConnection(          for (message in reader.offer(buffer, 0, count)) {             val decoded = MessageCodec.decode(message.channelId, message.payload)-            // Routed by channel id, not by the frame's control bit: a head unit leaves that-            // bit clear on the version request, which is a control-channel message.-            if (message.channelId == CONTROL_CHANNEL) {+            // Channel-open traffic (0x7 out / 0x8 in) rides the TARGET channel,+            // not channel 0: gearhead's `izd.b()` sends the open via+            // `izl.g(this.b, ...)` where `b` is the channel being opened, and+            // the HU's 0x8 comes back the same way. A ch0-framed 0x7 parses but+            // is refused with STATUS_INVALID_CHANNEL (-5, Run 6). So an inbound+            // 0x8 on ANY channel belongs to the session; everything else on a+            // service channel goes to that channel's owner.+            if (message.channelId == CONTROL_CHANNEL ||+                decoded.type == GalMessage.Control.CHANNEL_OPEN_RESPONSE+            ) {                 val before = session.state                 val replies = session.onMessage(decoded.type, decoded.payload)                 // Log the payload: incoming message bodies are otherwise invisible, and@@ -104,8 +111,10 @@ class GalConnection(      /** Frames a control message and writes it. */     fun send(message: OutboundMessage) {+        // Most control messages ride channel 0; the channel-open request rides+        // its target channel (see OutboundMessage.channelId).         write(-            channelId = CONTROL_CHANNEL,+            channelId = message.channelId,             payload = MessageCodec.encode(message.type, message.payload),             // Per-message: only the channel-open request sets CONTROL today.             // The CONTROL-less 0x5 is live-accepted, so nothing else takes the
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 6a3d94d8d3..7b0b3fb6e2 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@@ -31,6 +31,13 @@ class OutboundMessage(      * rather than applied to every encrypted channel-0 send.      */     val isControl: Boolean = false,+    /**+     * Frame channel for sends that don't ride channel 0. The channel-open+     * request targets the channel being opened: gearhead's `izd.b()` sends via+     * `izl.g(this.b, ...)`, and a ch0-framed 0x7 is refused with+     * STATUS_INVALID_CHANNEL (-5, Run 6). Ignored for other message types.+     */+    val channelId: Int = 0, )  /** Where the session has got to. */@@ -155,6 +162,10 @@ class GalControlSession(      * 0x04, so gearhead's 0x7 goes out as FIRST|LAST|CONTROL|ENCRYPTED = 0x0F.      * Scoped to this path only -- the CONTROL-less 0x5 is live-accepted, so no      * other channel-0 send takes the bit.+     *+     * The frame rides the TARGET channel, not channel 0: `izd.b()` sends via+     * `izl.g(this.b, ...)`, and a ch0-framed 0x7 is refused with+     * STATUS_INVALID_CHANNEL (-5, Run 6) even though it parses.      */     fun openChannel(service: Service, priority: Int = 0): OutboundMessage {         pendingChannels += service.id@@ -167,6 +178,7 @@ class GalControlSession(                 .toByteArray(),             encrypted = true,             isControl = true,+            channelId = service.id,         )     } 
auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalConnectionTest.kt
diff --git a/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalConnectionTest.kt b/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalConnectionTest.ktindex 052824cc76..e5e1021642 100644--- a/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalConnectionTest.kt+++ b/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalConnectionTest.kt@@ -1,5 +1,7 @@ package com.vayunmathur.auto.protocol +import com.vayunmathur.auto.protocol.gal.ChannelOpenResponse+import com.vayunmathur.auto.protocol.gal.Service import java.io.ByteArrayOutputStream import java.nio.ByteBuffer import kotlin.test.Test@@ -109,11 +111,10 @@ class GalConnectionTest {     }      @Test-    fun `the channel-open request carries the CONTROL bit`() {-        // Scoped to the 0x7 path only: DHU 2.0 answers a CONTROL-less 0x7 with-        // an empty 0xff and no 0x8 (Run 5), while the CONTROL-less 0x5 is-        // accepted -- so the bit goes on the open, nothing else. Dalvik ground-        // truth (`Ljbe.i`): gearhead's 0x7 goes out as 0x0F.+    fun `the channel-open request rides its target channel`() {+        // gearhead's `izd.b()` sends the open via `izl.g(this.b, ...)` -- the+        // frame channel is the channel being opened. A ch0-framed 0x7 parses+        // but is refused with STATUS_INVALID_CHANNEL (-5, Run 6).         //         // Framed at the FrameWriter level with a handshook engine: sending         // encrypted data through a full connection needs a completed handshake@@ -127,19 +128,51 @@ class GalConnectionTest {             payload = byteArrayOf(0x08, 0x00, 0x10, 0x02),             encrypted = true,             isControl = true,+            channelId = GalService.VIDEO_SINK.id,         )          val frame = writer.frame(-            channelId = 0,+            channelId = message.channelId,             payload = MessageCodec.encode(message.type, message.payload),             isControl = message.isControl,             encrypted = message.encrypted,         ).single() -        assertEquals(0x00, frame[0])+        assertEquals(GalService.VIDEO_SINK.id, frame[0].toInt() and 0xFF)         assertEquals(0x0F, frame[1].toInt() and 0xFF)     } +    @Test+    fun `a channel-open response on a service channel reaches the session`() {+        // The HU's 0x8 comes back on the target channel, not channel 0. It must+        // still land in the session rather than the service handler.+        val transport = FakeTransport()+        val received = mutableListOf<ChannelMessage>()+        val connection = GalConnection(+            transport,+            TestTls.context(),+            deviceModel = "Pixel 8",+            onChannelMessage = { received += it },+        )+        val service = Service.newBuilder().setId(GalService.VIDEO_SINK.id).build()+        connection.session.openChannel(service)+        transport.queue(+            FrameWriter().frame(+                channelId = GalService.VIDEO_SINK.id,+                payload = MessageCodec.encode(+                    GalMessage.Control.CHANNEL_OPEN_RESPONSE,+                    ChannelOpenResponse.newBuilder().setStatus(0).build().toByteArray(),+                ),+                isControl = true,+                encrypted = false,+            ).single(),+        )++        assertTrue(connection.pump())+        assertTrue(received.isEmpty(), "the 0x8 must reach the session, not the handler")+        assertEquals(setOf(GalService.VIDEO_SINK.id), connection.session.openChannels)+    }+     @Test     fun `other control messages leave the CONTROL bit clear`() {         // Discovery (0x5), ping responses and byebye ride CONTROL-less: the 0x5