Modern Apps git

← Modern-Apps.git

auto: an empty LOCATION reading can no longer fabricate a (0, 0) fix

Commit
f61b5370bb4654d4a8ab8a4bc37fc589d8be99ff
Author
Vayun Mathur <[email protected]>
When
2026-12-01 10:21 UTC (4 days ago)
Parents
c212bb5
withLocation checked only that the event carries a location
message, then read both coordinate getters unconditionally.
The proto makes latitude_e7/longitude_e7 optional, so a
well-formed empty or half-stated reading (wire
0A 04 08 01 12 00) clobbered a seeded last-known fix
(37.7699060, -122.4195609) with (0, 0) — a fabricated
position in the Gulf of Guinea. Every neighbouring scalar
fold guards its leaf (hasSpeedMps, hasIsNight, ...) and the
snapshot contract says a payload-less event keeps
last-known; location was the one fold missing the guard.

The fold now requires both coordinates present (presence,
not non-zero: an explicitly stated (0,0) still updates).
Four regression tests failed 3/4 pre-fix. Framing,
negotiation, ack tracking and the other codecs were audited
and cleared. Verified: :auto:protocol:testDebugUnitTest 300
tests pass; lint success.

Diff

auto/protocol/src/main/java/com/vayunmathur/auto/protocol/SensorCodec.kt
diff --git a/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/SensorCodec.kt b/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/SensorCodec.ktindex f0cab02da6..f8a0221e63 100644--- a/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/SensorCodec.kt+++ b/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/SensorCodec.kt@@ -111,10 +111,15 @@ data class SensorSnapshot(         else -> this     } -    /** Folds a LOCATION fix; payload-less events keep last-known. */+    /** Folds a LOCATION fix; events without a full lat/long pair keep last-known. */     private fun SensorSnapshot.withLocation(event: SensorEvent): SensorSnapshot {         if (!event.hasLocation()) return this         val fix = event.location+        // Both coordinates are optional on the wire: a reading that states+        // only one (or neither) has no fix to fold, exactly like an empty+        // SPEED/NIGHT/COMPASS reading above. Defaulting the absent half to+        // 0 would fabricate a position and clobber the last real fix.+        if (!fix.hasLatitudeE7() || !fix.hasLongitudeE7()) return this         return copy(             location = LocationFix(                 latitudeE7 = fix.latitudeE7,
auto/protocol/src/test/java/com/vayunmathur/auto/protocol/SensorLocationPresenceTest.kt
diff --git a/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/SensorLocationPresenceTest.kt b/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/SensorLocationPresenceTest.ktnew file mode 100644index 0000000000..3a2412e06d--- /dev/null+++ b/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/SensorLocationPresenceTest.kt@@ -0,0 +1,75 @@+package com.vayunmathur.auto.protocol++import com.vayunmathur.auto.protocol.gal.LocationReading+import com.vayunmathur.auto.protocol.gal.SensorEvent+import com.vayunmathur.auto.protocol.gal.SensorType+import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * Regression: a LOCATION event whose reading carries no latitude/longitude+ * pair is a payload-object-but-no-leaf-value event, like the empty SPEED /+ * NIGHT / DRIVING_STATUS / COMPASS readings the snapshot already ignores.+ * It must keep last-known instead of fabricating a fix at (0, 0).+ */+class SensorLocationPresenceTest {++    private val seeded = SensorSnapshot(+        location = LocationFix(+            latitudeE7 = 377_699_060L,+            longitudeE7 = -1_224_195_609L,+            accuracyM = 8,+            timestampMs = 42L,+        ),+    )++    @Test+    fun `an empty location reading on the wire keeps last known`() {+        // SensorBatch{events:[{sensor_type:LOCATION, location:{}}]}:+        //   0A 04          batch field 1, 4 bytes+        //     08 01        event field 1, LOCATION = 1+        //     12 00        event field 2, empty LocationReading+        val batch = byteArrayOf(0x0A, 0x04, 0x08, 0x01, 0x12, 0x00)+        val inbound = SensorCodec.decodeInbound(GalMessage.Sensor.SENSOR_BATCH, batch)+        val events = (inbound as InboundSensor.Readings).events++        assertEquals(seeded.location, seeded.withEvents(events).location)+    }++    @Test+    fun `a location reading with only latitude keeps last known`() {+        val event = SensorEvent.newBuilder()+            .setSensorType(SensorType.LOCATION)+            .setLocation(LocationReading.newBuilder().setLatitudeE7(100_000_000L))+            .build()++        assertEquals(seeded.location, seeded.withEvents(listOf(event)).location)+    }++    @Test+    fun `a location reading with only longitude keeps last known`() {+        val event = SensorEvent.newBuilder()+            .setSensorType(SensorType.LOCATION)+            .setLocation(LocationReading.newBuilder().setLongitudeE7(100_000_000L))+            .build()++        assertEquals(seeded.location, seeded.withEvents(listOf(event)).location)+    }++    @Test+    fun `an explicit zero-zero fix still updates`() {+        // Presence, not non-zero-ness, is the guard: (0, 0) stated with both+        // fields present is a real fix and replaces last-known.+        val event = SensorEvent.newBuilder()+            .setSensorType(SensorType.LOCATION)+            .setLocation(+                LocationReading.newBuilder().setLatitudeE7(0L).setLongitudeE7(0L),+            )+            .build()++        assertEquals(+            LocationFix(0L, 0L, null, null),+            seeded.withEvents(listOf(event)).location,+        )+    }+}