Add a V2X data logger to Settings > Developer

Records every received V2X message (CAM, DENM, SPATEM, MAPEM, VAM) into one
SQLite file per run, with a timestamp, the phone's GNSS fix, the OBU's GNSS fix
where there is one (CiT One), and RSSI where the hardware reports it (ESP32-C5).
Works on both hardware paths: V2X_RX frames from the ESP32 link, and the raw
v2x/rx plus processed v2x-uca topics over MQTT. The UPER bytes are stored
untouched; CAM, DENM and SPATEM also get a few decoded columns.

A foreground service keeps it recording with the screen locked. Subscribes to
v2x/rx/mapem so MAPEM is received on the CiT One.

The current ESP32-C5 firmware forwards only BTP ports 2001, 2002 and 2004, so
MAPEM and VAM appear in an ESP32 log only once it accepts 2003 and 2018.

Checked on a Pixel 9 Pro against a simulated car and an RSU: 10 minutes with the
screen locked and forced deep Doze, no gaps, files pass integrity_check.
This commit is contained in:
Ashin Walpola
2026-10-02 16:16:02 +02:00
parent d3fc9bf66c
commit c13c3891c9
17 changed files with 1336 additions and 5 deletions
@@ -0,0 +1,73 @@
package com.hawhamburg.micr0bu
import com.hawhamburg.micr0bu.domain.log.ItsMessageKind
import com.hawhamburg.micr0bu.domain.log.ItsPduHeader
import com.hawhamburg.micr0bu.domain.log.classifyItsPdu
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
/**
* Pins how the V2X data logger names a message it may have no decoder for. The CAM fixture is the
* golden UPER frame from [CamEncodeGoldenTest] (protocolVersion 2, messageID 2, stationID 999999);
* the others only need a valid ItsPduHeader, which is all the classifier reads.
*/
class ItsMessageKindTest {
private fun String.hexToBytes() = chunked(2).map { it.toInt(16).toByte() }.toByteArray()
private val goldenCam =
"0202000f423f3700402ab215af6e286477dffffffc23b7743e0027ffc0d0fe0118329337feebfff6000000".hexToBytes()
/** An ItsPduHeader with [messageId] and stationID 0x01020304, then a stand-in body. */
private fun pdu(messageId: Int) =
byteArrayOf(2, messageId.toByte(), 1, 2, 3, 4, 0x55, 0x66)
@Test fun `header of the golden CAM is read`() {
val h = ItsPduHeader.parse(goldenCam)!!
assertEquals(2, h.protocolVersion)
assertEquals(2, h.messageId)
assertEquals(999_999L, h.stationId)
}
@Test fun `station id above 2^31 is not sign-extended`() {
val h = ItsPduHeader.parse(byteArrayOf(2, 2, 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFE.toByte()))!!
assertEquals(0xFFFFFFFEL, h.stationId)
}
@Test fun `every tracked kind is recognised by messageID alone`() {
assertEquals(ItsMessageKind.DENM, classifyItsPdu(pdu(1), null).kind)
assertEquals(ItsMessageKind.CAM, classifyItsPdu(pdu(2), null).kind)
assertEquals(ItsMessageKind.SPATEM, classifyItsPdu(pdu(4), null).kind)
assertEquals(ItsMessageKind.MAPEM, classifyItsPdu(pdu(5), null).kind)
assertEquals(ItsMessageKind.VAM, classifyItsPdu(pdu(16), null).kind)
}
@Test fun `messageID wins over the BTP port, and the SPATEM-MAPEM numbers are not swapped`() {
// SPATEM is port 2004 but messageID 4; MAPEM is port 2003 but messageID 5.
assertEquals(ItsMessageKind.SPATEM, classifyItsPdu(pdu(4), 2004).kind)
assertEquals(ItsMessageKind.MAPEM, classifyItsPdu(pdu(5), 2003).kind)
// A PDU that disagrees with the port it arrived on is what its header says it is.
assertEquals(ItsMessageKind.MAPEM, classifyItsPdu(pdu(5), 2004).kind)
}
@Test fun `a PDU too short for a header falls back to the BTP port`() {
val c = classifyItsPdu(byteArrayOf(2, 5), 2018)
assertEquals(ItsMessageKind.VAM, c.kind)
assertNull(c.messageId)
assertNull(c.stationId)
}
@Test fun `an unknown messageID is kept as OTHER with its id`() {
val c = classifyItsPdu(pdu(99), null)
assertEquals(ItsMessageKind.OTHER, c.kind)
assertEquals(99, c.messageId)
assertEquals(0x01020304L, c.stationId)
}
@Test fun `processed topic names map to kinds`() {
assertEquals(ItsMessageKind.MAPEM, ItsMessageKind.fromProcessedTopicName("map"))
assertEquals(ItsMessageKind.SPATEM, ItsMessageKind.fromProcessedTopicName("spat"))
assertNull(ItsMessageKind.fromProcessedTopicName("unknown"))
}
}