maauto: gate media setup on ChannelOpenResponse
- Commit
3a65e9ffa2ecef84e9a07ddb5b02a2b7e6a9260d- Author
- vayun-mathur <[email protected]>
- When
- 2026-09-12 14:37 UTC (2 months ago)
- Parents
97e28f9
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 0c30d500c2..71492e4926 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@@ -75,16 +75,26 @@ class GalConnection( if (message.channelId == CONTROL_CHANNEL) { val before = session.state val replies = session.onMessage(decoded.type, decoded.payload)+ // Log the payload: incoming message bodies are otherwise invisible, and+ // the head unit's answers (discovery list, open status, errors) carry+ // the only explanation we ever get for a rejection. First bytes only:+ // enough for status enums and service ids, small enough to read. trace( "ctrl in 0x${decoded.type.toString(16)} (${decoded.payload.size}B) " +- "$before -> ${session.state}, ${replies.size} reply",+ "$before -> ${session.state}, ${replies.size} reply " ++ decoded.payload.take(IN_PAYLOAD_LOG_BYTES).joinToString("") {+ "%02x".format(it)+ }, ) session.failure?.let { trace("session failed: $it") } replies.forEach(::send) } else { trace( "ch${message.channelId} in 0x${decoded.type.toString(16)} " +- "(${decoded.payload.size}B)",+ "(${decoded.payload.size}B) " ++ decoded.payload.take(IN_PAYLOAD_LOG_BYTES).joinToString("") {+ "%02x".format(it)+ }, ) onChannelMessage.onMessage(decoded) }@@ -134,5 +144,8 @@ class GalConnection( /** Comfortably larger than one frame, so a read rarely splits one. */ const val READ_BUFFER_SIZE = 32 * 1024++ /** Bytes of each inbound payload in the trace log: enough for ids and statuses. */+ const val IN_PAYLOAD_LOG_BYTES = 64 } }
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 f0e07af04c..2424ddfa4f 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@@ -4,6 +4,7 @@ import com.vayunmathur.auto.protocol.gal.AuthComplete import com.vayunmathur.auto.protocol.gal.ByeByeReason import com.vayunmathur.auto.protocol.gal.ByeByeRequest import com.vayunmathur.auto.protocol.gal.ChannelOpenRequest+import com.vayunmathur.auto.protocol.gal.ChannelOpenResponse import com.vayunmathur.auto.protocol.gal.MessageStatus import com.vayunmathur.auto.protocol.gal.PingRequest import com.vayunmathur.auto.protocol.gal.PingResponse@@ -74,6 +75,12 @@ class GalControlSession( var failure: String? = null private set + /** Service ids whose ChannelOpenResponse came back STATUS_SUCCESS. */+ var openChannels: Set<Int> = emptySet()+ private set++ private var pendingChannel: Int = -1+ private var tlsInbound: ByteBuffer = ByteBuffer.allocate(0) /** Handles one control-channel message and returns whatever should go back. */@@ -82,6 +89,7 @@ class GalControlSession( GalMessage.Control.SSL_HANDSHAKE -> onHandshakeData(payload) GalMessage.Control.AUTH_COMPLETE -> onAuthComplete(payload) GalMessage.Control.SERVICE_DISCOVERY_RESPONSE -> onServiceDiscovery(payload)+ GalMessage.Control.CHANNEL_OPEN_RESPONSE -> onChannelOpen(payload) GalMessage.Control.PING_REQUEST -> onPing(payload) GalMessage.Control.BYEBYE_REQUEST -> onByeBye() GalMessage.Control.BYEBYE_RESPONSE -> {@@ -94,23 +102,32 @@ class GalControlSession( GalMessage.Control.NAVIGATION_FOCUS_NOTIFICATION, GalMessage.Control.CALL_AVAILABILITY_STATUS, GalMessage.Control.SERVICE_DISCOVERY_UPDATE,- GalMessage.Control.CHANNEL_OPEN_RESPONSE, GalMessage.Control.PING_RESPONSE, -> emptyList() else -> fail("unexpected control message type 0x${type.toString(16)}") } - /** Asks the head unit to open [service]'s channel. */- fun openChannel(service: Service, priority: Int = 0): OutboundMessage = OutboundMessage(- type = GalMessage.Control.CHANNEL_OPEN_REQUEST,- payload = ChannelOpenRequest.newBuilder()- .setPriority(priority)- .setServiceId(service.id)- .build()- .toByteArray(),- encrypted = true,- )+ /**+ * Asks the head unit to open [service]'s channel.+ *+ * 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`.+ */+ fun openChannel(service: Service, priority: Int = 0): OutboundMessage {+ pendingChannel = service.id+ return OutboundMessage(+ type = GalMessage.Control.CHANNEL_OPEN_REQUEST,+ payload = ChannelOpenRequest.newBuilder()+ .setPriority(priority)+ .setServiceId(service.id)+ .build()+ .toByteArray(),+ encrypted = true,+ )+ } /** Ends the session politely. */ fun disconnect(reason: ByeByeReason = ByeByeReason.USER_SELECTION): OutboundMessage =@@ -199,6 +216,24 @@ class GalControlSession( return emptyList() } + /**+ * Records the head unit's answer to [openChannel].+ *+ * The channel is NOT open yet when this returns: like gearhead's `iza.a()`,+ * 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).+ */+ private fun onChannelOpen(payload: ByteArray): List<OutboundMessage> {+ val status = ChannelOpenResponse.parseFrom(payload).status+ if (status != MessageStatus.STATUS_SUCCESS.number) {+ return fail("head unit refused channel open: status $status")+ }+ openChannels += pendingChannel+ pendingChannel = -1+ return emptyList()+ }+ private fun onPing(payload: ByteArray): List<OutboundMessage> = listOf( OutboundMessage( type = GalMessage.Control.PING_RESPONSE,
auto/protocol/src/main/proto/gal/control.proto
diff --git a/auto/protocol/src/main/proto/gal/control.proto b/auto/protocol/src/main/proto/gal/control.protoindex f4e8c0239f..ead58bb0be 100644--- a/auto/protocol/src/main/proto/gal/control.proto+++ b/auto/protocol/src/main/proto/gal/control.proto@@ -50,13 +50,16 @@ enum MessageStatus { STATUS_PING_TIMEOUT = -25; STATUS_CAR_PROPERTY_SET_REQUEST_FAILED = -26; STATUS_CAR_PROPERTY_LISTENER_REGISTRATION_FAILED = -27;- STATUS_COMMAND_NOT_SUPPORTED = -250;- STATUS_FRAMING_ERROR = -251;- STATUS_UNEXPECTED_MESSAGE = -253;- STATUS_BUSY = -254;- STATUS_OUT_OF_MEMORY = -255;+// Control message 7. Note the order: priority first, then the service id. `priority` is+// sint32 because the control channel opens itself at -128.+message ChannelOpenRequest {+ required sint32 priority = 1;+ required int32 service_id = 2; } +// Control message 8. Sent by the HEAD UNIT: whether our channel-open request+// succeeded. `status` is the `xkr` status enum value (STATUS_SUCCESS = 0).+ enum ByeByeReason { USER_SELECTION = 1; DEVICE_SWITCH = 2;@@ -164,11 +167,12 @@ message ServiceDiscoveryUpdate { optional Service service = 1; } -// Control message 7. Note the order: priority first, then the service id. `priority` is-// sint32 because the control channel opens itself at -128.-message ChannelOpenRequest {- required sint32 priority = 1;- required int32 service_id = 2;+// Control message 8. Sent by the HEAD UNIT: whether our channel-open request+// succeeded. `xir.c` is the `xkr` status enum value (STATUS_SUCCESS = 0).+// gearhead's `iza.a()` parses exactly this and, on anything but SUCCESS, closes+// the channel (`P(1)` + `c()`).+message ChannelOpenResponse {+ required int32 status = 1; } // Control message 4.
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 48348db212..f8f22917ce 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@@ -1,6 +1,7 @@ package com.vayunmathur.auto.protocol import com.vayunmathur.auto.protocol.gal.AuthComplete+import com.vayunmathur.auto.protocol.gal.ChannelOpenResponse import com.vayunmathur.auto.protocol.gal.MediaCodecType import com.vayunmathur.auto.protocol.gal.MediaSinkService import com.vayunmathur.auto.protocol.gal.MessageStatus@@ -200,17 +201,36 @@ class GalControlSessionTest { } @Test- fun `opening a channel names the service and rides encrypted`() {+ fun `the channel opens only on a success response`() { val session = session(GalCredential.serverEngine(context())) val service = Service.newBuilder().setId(GalService.VIDEO_SINK.id).build()+ session.openChannel(service)++ // A success response marks the channel open with no reply of its own.+ assertTrue(+ session.onMessage(+ GalMessage.Control.CHANNEL_OPEN_RESPONSE,+ ChannelOpenResponse.newBuilder().setStatus(0).build().toByteArray(),+ ).isEmpty(),+ )+ assertEquals(setOf(GalService.VIDEO_SINK.id), session.openChannels)+ assertNull(session.failure)+ } - val open = session.openChannel(service, priority = -128)+ @Test+ fun `a refused channel open fails the session`() {+ val session = session(GalCredential.serverEngine(context()))+ val service = Service.newBuilder().setId(GalService.VIDEO_SINK.id).build()+ session.openChannel(service) - assertEquals(GalMessage.Control.CHANNEL_OPEN_REQUEST, open.type)- assertTrue(open.encrypted)- val parsed = com.vayunmathur.auto.protocol.gal.ChannelOpenRequest.parseFrom(open.payload)- assertEquals(GalService.VIDEO_SINK.id, parsed.serviceId)- assertEquals(-128, parsed.priority)+ 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) } // ---- helpers ----
auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalProtoTest.kt
diff --git a/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalProtoTest.kt b/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalProtoTest.ktindex d3e028364b..44e6a72eb3 100644--- a/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalProtoTest.kt+++ b/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalProtoTest.kt@@ -4,6 +4,7 @@ import com.vayunmathur.auto.protocol.gal.AudioConfiguration import com.vayunmathur.auto.protocol.gal.AuthComplete import com.vayunmathur.auto.protocol.gal.ByeByeReason import com.vayunmathur.auto.protocol.gal.ByeByeRequest+import com.vayunmathur.auto.protocol.gal.ChannelOpenResponse import com.vayunmathur.auto.protocol.gal.ChannelOpenRequest import com.vayunmathur.auto.protocol.gal.MediaCodecType import com.vayunmathur.auto.protocol.gal.MediaSinkService@@ -73,6 +74,15 @@ class GalProtoTest { ) } + @Test+ fun `a channel-open response pins status in field 1`() {+ // `xir`: single enum field 1. STATUS_SUCCESS = 0 encodes as 08 00.+ assertContentEquals(+ byteArrayOf(0x08, 0x00),+ ChannelOpenResponse.newBuilder().setStatus(0).build().toByteArray(),+ )+ }+ @Test fun `ByeByeRequest encodes the reason enum by value`() { val bytes = ByeByeRequest.newBuilder()
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 81ad9176a5..211ac011af 100644--- a/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt+++ b/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt@@ -68,11 +68,19 @@ class ProjectionService : Service() { ) 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) }+ // 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()+ } video?.pumpEncoder() } Log.i(TAG, "head unit disconnected: ${connection.session.failure ?: "cleanly"}")@@ -92,7 +100,7 @@ class ProjectionService : Service() { return true } connection.send(connection.session.openChannel(service))- video = VideoSinkChannel(this, service, connection).also { it.requestSetup() }+ video = VideoSinkChannel(this, service, connection) return true }