Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ internal class CarlifeSession(
val quietTxIds = setOf(131073, 131074) + CarlifeAudio.Lane.entries.map { it.dataId }
const val KEYFRAME_RECOVERY_INTERVAL_NS = 1_000_000_000L
const val KEYFRAME_REFRESH_INTERVAL_NS = 5_000_000_000L
/**
* Newer receivers (CarLife 4.3) send no challenge and wait for the sender's 65611, the path where the
* sender checks the head unit after its statistics (Baidu integration guide §17.2). Older receivers
* challenge right after device info, so the result goes out unasked only after this wait.
*/
const val UNSOLICITED_AUTH_RESULT_AFTER_NS = 3_000_000_000L
}

val active = AtomicBoolean(true)
Expand All @@ -42,6 +48,7 @@ internal class CarlifeSession(
private var deviceSent = false
private var authAnswered = false
private var authAccepted = false
private var deviceSentAt: Long? = null
@Volatile var authenticated = false
private set
@Volatile var streaming = false
Expand Down Expand Up @@ -187,6 +194,11 @@ internal class CarlifeSession(
}
98380 -> {
protocolRequire(authAccepted, "unexpected_phase")
// A late challenge after an unasked result is answered as usual, so its acknowledgement can repeat.
if (authenticated) {
diagnostic("auth acknowledged repeated=true peerMajor=$peerMajor")
return
}
authenticated = true
stage("established")
report(CarlifeState.AUTHENTICATED)
Expand Down Expand Up @@ -386,6 +398,7 @@ internal class CarlifeSession(
send(1, 65540, ProtoWriter().text(1, "Android").text(4, info.brand).text(14, info.model)
.text(20, info.sdk).text(24, "4.0.0").build())
deviceSent = true
deviceSentAt = clock()
stage("device_sent")
}

Expand Down Expand Up @@ -439,6 +452,16 @@ internal class CarlifeSession(
if (System.nanoTime() - lastSent.get() > 1_000_000_000L) {
send(if (channels[2] != null) 2 else 1, 131074)
}
val sentAt = deviceSentAt
if (sentAt != null && receiver.challenges && !authenticated && !authAnswered && !authAccepted) {
val waited = clock() - sentAt
if (waited >= UNSOLICITED_AUTH_RESULT_AFTER_NS) {
authAccepted = true
diagnostic("auth unsolicited_result=true reason=no_challenge_after_device_info peerMajor=$peerMajor " +
"waitedMs=${waited / 1_000_000}")
send(1, 65611, ProtoWriter().number(1, 1).build())
}
}
if (streaming) {
flushKeyframeRequest()
val time = clock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,55 @@ class CarlifeSessionTest {
assertFalse(fixture.session.authenticated)
}

/** CarLife 4.3 receivers send no challenge and wait for the sender's 65611 (Baidu guide §17.2). */
@Test fun receiverWithoutChallengeGetsAnUnaskedAuthResult() {
var now = 0L
val fixture = Fixture(clock = { now })
fixture.receive(98305, ProtoWriter().number(1, 4).number(2, 1).build())
fixture.receive(98343)
fixture.receive(98307)
now += 2_999_999_999L
fixture.session.tick()
assertFalse(fixture.messages().any { it.id == 65611 })
now += 1
fixture.session.tick()
val results = fixture.messages().filter { it.id == 65611 }
assertEquals(listOf(1L), results.map { Proto.parse(it.payload).number(1) })
assertTrue(fixture.diagnostics.any { it.contains("unsolicited_result=true") && it.contains("waitedMs=3000") })
now += 1_000_000_000L
fixture.session.tick()
assertEquals(1, fixture.messages().count { it.id == 65611 })
fixture.receive(98380)
assertTrue(fixture.session.authenticated)
assertEquals(listOf(65538, 65540, 65611, 65617), fixture.messages().map { it.id })
}

@Test fun lateChallengeAfterAnUnaskedAuthResultIsAnsweredAndEstablishesOnce() {
var now = 0L
val fixture = Fixture(clock = { now })
fixture.receive(98305, ProtoWriter().number(1, 4).build())
fixture.receive(98343)
now += 3_000_000_000L
fixture.session.tick()
fixture.receive(98376, ProtoWriter().text(1, "2.0;2026-11-02-10-20-30").build())
fixture.receive(98378, ProtoWriter().number(1, 1).build())
fixture.receive(98380)
fixture.receive(98380)
assertTrue(fixture.session.authenticated)
assertEquals(listOf(65538, 65540, 65611, 65609, 65611, 65617), fixture.messages().map { it.id })
assertEquals(1, fixture.diagnostics.count { it.contains("phase=established") && it.contains("phase_changed") })
}

@Test fun challengedReceiverGetsNoUnaskedAuthResult() {
var now = 0L
val fixture = Fixture(clock = { now })
fixture.authenticate()
now += 3_000_000_000L
fixture.session.tick()
assertEquals(1, fixture.messages().count { it.id == 65611 })
assertFalse(fixture.diagnostics.any { it.contains("unsolicited_result") })
}

@Test fun rejectedVersionReportsOnlyBoundedProtocolFields() {
val fixture = Fixture()
val unsupported = ProtoWriter().number(1, 2).number(2, 11).build()
Expand Down
Loading