Fragment large V2X_RX messages and decode road-RSU SPATEMs
The firmware dropped every received ITS message over 494 bytes (one link message), so a road RSU's 888-byte SPATEM and 1198-byte MAPEM never reached the phone. V2X_RX now goes out in link fragments up to 2300 bytes, best effort and non-blocking; messages up to 494 bytes are unchanged. The app's SPATEM decoder then refused those messages because they carry intersection and movement names and advisory speed lists. It now reads past both, checked against asn1tools on 718 distinct messages. Adds the pure fragmenter with host tests and a pcap replay, a Kotlin cross-check on the firmware's own fragments, tools/rx_size_compare.py, docs/08-large-message-reception.md and station-link revision 3.
This commit is contained in:
@@ -3,6 +3,7 @@ package com.hawhamburg.micr0bu
|
||||
import com.hawhamburg.micr0bu.data.transport.LinkMessage
|
||||
import com.hawhamburg.micr0bu.data.transport.LinkReassembly
|
||||
import com.hawhamburg.micr0bu.data.transport.RawFrame
|
||||
import com.hawhamburg.micr0bu.data.transport.V2xRxFrame
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
@@ -100,6 +101,61 @@ class LinkReassemblyTest {
|
||||
assertArrayEquals(bytes(9, 9, 8, 8), b.body)
|
||||
}
|
||||
|
||||
// ---- large V2X_RX, fragmented by the firmware ---------------------------------------------
|
||||
|
||||
private fun hexBytes(s: String) = ByteArray(s.length / 2) { s.substring(it * 2, it * 2 + 2).toInt(16).toByte() }
|
||||
|
||||
/**
|
||||
* `v2x_rx_fragments.tsv` is written by `test_v2x_fragments --dump` in obu-firmware/test/host: the first 888-byte
|
||||
* SPATEM and 1198-byte MAPEM of the CiT node's 2026-11-23 recording, as the firmware's own code splits them for
|
||||
* serial / BLE MTU 517 (512-octet messages) and BLE MTU 247 (244). Each line: port, UPER size, limit, V2X_RX body,
|
||||
* then the link messages in order. The app must put back exactly that body and read the message out of it.
|
||||
*/
|
||||
@Test
|
||||
fun `the firmware's fragments of a large V2X_RX reassemble to the body and parse`() {
|
||||
val lines = javaClass.classLoader!!.getResource("v2x_rx_fragments.tsv")!!.readText().lines().filter { it.isNotBlank() }
|
||||
assertEquals(4, lines.size)
|
||||
for (line in lines) {
|
||||
val f = line.split('\t')
|
||||
val port = f[0].toInt()
|
||||
val size = f[1].toInt()
|
||||
val limit = f[2].toInt()
|
||||
val body = hexBytes(f[3])
|
||||
val wires = f.drop(4).map { hexBytes(it) }
|
||||
assertTrue("$port/$size at $limit: needs fragments", wires.size >= 2)
|
||||
var whole: LinkMessage? = null
|
||||
wires.forEachIndexed { i, wire ->
|
||||
assertTrue("$port/$size at $limit: a message of ${wire.size} octets exceeds $limit", wire.size <= limit)
|
||||
val message = LinkMessage.decode(wire)!!
|
||||
assertEquals(0x90, message.opcode)
|
||||
assertEquals(if (i == 0) 1 else if (i == wires.size - 1) 3 else 2, message.flags)
|
||||
whole = reassembly.feed(message)
|
||||
if (i < wires.size - 1) assertNull(whole)
|
||||
}
|
||||
assertEquals(0x90, whole!!.opcode)
|
||||
assertEquals(0, whole!!.flags)
|
||||
assertArrayEquals(body, whole!!.body)
|
||||
val v2x = V2xRxFrame.parse(whole!!.body)!!
|
||||
assertEquals(port, v2x.btpPort)
|
||||
assertEquals(size, v2x.uper.size)
|
||||
assertEquals(if (port == 2003) 5 else 4, v2x.uper[1].toInt() and 0xFF) // ItsPduHeader.messageId: MAPEM 5, SPATEM 4
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `losing one fragment of a large V2X_RX costs that message only`() {
|
||||
val lines = javaClass.classLoader!!.getResource("v2x_rx_fragments.tsv")!!.readText().lines().filter { it.isNotBlank() }
|
||||
val f = lines.first { it.split('\t')[2] == "512" && it.split('\t')[0] == "2003" }.split('\t')
|
||||
val wires = f.drop(4).map { hexBytes(it) }
|
||||
// the first message loses its middle fragment; the same message again, in full, still completes afterwards
|
||||
val lost = wires.mapIndexedNotNull { i, w -> if (i == 1) null else LinkMessage.decode(w)!! }
|
||||
for (m in lost) assertNull(reassembly.feed(m))
|
||||
val next = wires.map { w -> LinkMessage.decode(w)!!.let { LinkMessage(it.opcode, it.sequence + 1, it.body, it.flags) } }
|
||||
var whole: LinkMessage? = null
|
||||
for (m in next) whole = reassembly.feed(m)
|
||||
assertArrayEquals(hexBytes(f[3]), whole!!.body)
|
||||
}
|
||||
|
||||
// ---- RAW_FRAME record ------------------------------------------------------------------
|
||||
|
||||
private fun rawBody(frame: ByteArray, flags: Int = 0x02, rssi: Int = -61): ByteArray {
|
||||
|
||||
@@ -153,6 +153,34 @@ class SpatemUperCodecTest {
|
||||
assertNull(SignalPhaseEvent(SignalPhase.DARK, null, null, null).secondsUntil(topOfHour))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `decodes a road RSU SPATEM with names and advisory speeds`() {
|
||||
// 888 bytes from the Berliner Tor RSU (2026-11-23): intersection and movement names, and a greenwave
|
||||
// plus an ecoDrive AdvisorySpeed on every event. Before names and speeds were walked, every one of
|
||||
// its SPATEMs was refused and the V2X screens stayed empty. Expected values: asn1tools, ETSI modules.
|
||||
val bytes = javaClass.classLoader!!.getResource("spatem_road_rsu_888.hex")!!.readText().trim().hexToBytes()
|
||||
assertEquals(888, bytes.size)
|
||||
val spat = SpatemUperCodec.decode(bytes, receivedAt)
|
||||
assertNotNull(spat)
|
||||
spat!!
|
||||
assertEquals(1_095_348_116L, spat.stationId)
|
||||
assertEquals(470_408, spat.minuteOfYear)
|
||||
val i = spat.intersections.single()
|
||||
assertEquals("-1/23", i.key)
|
||||
assertEquals(2, i.revision)
|
||||
assertEquals(470_408, i.moy)
|
||||
assertEquals(59_719, i.timeStampMs)
|
||||
assertEquals(listOf(1, 2, 3, 4, 5), i.movements.map { it.signalGroup })
|
||||
i.movements.forEach { assertEquals(8, it.events.size) }
|
||||
assertEquals(SignalPhaseEvent(SignalPhase.STOP_AND_REMAIN, 5562, 5562, 5562), i.movements[0].events.first())
|
||||
assertEquals(SignalPhase.PERMISSIVE_CLEARANCE, i.movements[0].events.last().phase)
|
||||
assertEquals(6522, i.movements[0].events.last().minEndTimeDs)
|
||||
assertEquals(SignalPhase.PROTECTED_MOVEMENT_ALLOWED, i.movements[2].events.first().phase)
|
||||
assertEquals(SignalPhase.PERMISSIVE_MOVEMENT_ALLOWED, i.movements[3].events.first().phase)
|
||||
assertEquals(SignalPhaseEvent(SignalPhase.STOP_AND_REMAIN, 5802, 5802, 5802), i.movements[4].events.first())
|
||||
assertEquals(6762, i.movements[4].events.last().minEndTimeDs)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `does not decode another message type as SPATEM`() {
|
||||
// protocolVersion 2, messageId 2 (CAM) - the header guard must reject it outright.
|
||||
|
||||
Reference in New Issue
Block a user