Add a receive path switch: V2X_RX or the spec's RAW_FRAME, with fragment reassembly
Settings > Connection > ESP32-C5 > Receive path chooses where received ITS messages come from. V2X_RX (default) is the MicrOBU extension the board builds with gn_unwrap.c. Raw frames is the station-link RAW_FRAME debug capture: the app switches RAW_CAPTURE on (mode 3), reassembles fragmented link messages as the spec defines (LinkReassembly), parses the ITS5 record (RawFrame) and unwraps the 802.11 frame with GnUnwrap, a Kotlin port of gn_unwrap.c. Both end as the same V2X_RX frames, so nothing above the link changes, and the firmware keeps both. GnUnwrapTest checks the port against the C code on 602 recorded, cut and corrupted frames (corpus from obu-firmware/test/host: make corpus). 145 app tests. Fix the Connection text that claimed the app picks the strongest micrOBU.
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
package com.hawhamburg.micr0bu
|
||||
|
||||
import com.hawhamburg.micr0bu.data.transport.GnUnwrap
|
||||
import com.hawhamburg.micr0bu.data.transport.V2xRxFrame
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
/**
|
||||
* The Kotlin port of `gn_unwrap.c` against the firmware's own code.
|
||||
*
|
||||
* `gn_unwrap_corpus.tsv` is written by `make corpus` in obu-firmware/test/host: frames from the recorded
|
||||
* captures (unsigned and signed, single-hop and GeoBroadcast, CAM, DENM, MAPEM, SPATEM), copies cut at
|
||||
* several lengths, copies with flipped bytes, and a few rejected frames, each with the verdict and the
|
||||
* fields that `gn_unwrap_its` returns. Every line must come out the same here: same accept or reject,
|
||||
* same port, flags, destination area, truncation and payload bytes.
|
||||
*/
|
||||
class GnUnwrapTest {
|
||||
|
||||
private class Line(
|
||||
val frame: ByteArray, val accepted: Boolean, val port: Int, val flags: Int,
|
||||
val lat: Int, val lon: Int, val dist: Int, val truncated: Boolean, val payload: ByteArray,
|
||||
)
|
||||
|
||||
private fun String.hex(): ByteArray = ByteArray(length / 2) { substring(2 * it, 2 * it + 2).toInt(16).toByte() }
|
||||
|
||||
private fun corpus(): List<Line> {
|
||||
val text = javaClass.classLoader!!.getResourceAsStream("gn_unwrap_corpus.tsv")!!.bufferedReader().readText()
|
||||
return text.lines().filter { it.isNotBlank() }.map { l ->
|
||||
val f = l.split("\t")
|
||||
assertEquals("fields in: ${l.take(60)}", 9, f.size)
|
||||
Line(f[0].hex(), f[1] == "A", f[2].toInt(), f[3].toInt(), f[4].toInt(), f[5].toInt(), f[6].toInt(),
|
||||
f[7] == "1", f[8].hex())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `every corpus frame gets the verdict and fields of the C code`() {
|
||||
val lines = corpus()
|
||||
assertTrue("corpus too small: ${lines.size}", lines.size > 400)
|
||||
var accepted = 0
|
||||
var rejected = 0
|
||||
for ((n, line) in lines.withIndex()) {
|
||||
val r = GnUnwrap.unwrap(line.frame)
|
||||
if (!line.accepted) {
|
||||
assertNull("line $n: C rejects, Kotlin accepted", r)
|
||||
rejected++
|
||||
continue
|
||||
}
|
||||
assertNotNull("line $n: C accepts, Kotlin rejected", r)
|
||||
r!!
|
||||
assertEquals("line $n port", line.port, r.btpPort)
|
||||
assertEquals("line $n area flag", line.flags and 1 != 0, r.hasGeoArea)
|
||||
assertEquals("line $n signed flag", line.flags and 2 != 0, r.signedUnverified)
|
||||
assertEquals("line $n area lat", line.lat, r.areaLatTenMicroDeg)
|
||||
assertEquals("line $n area lon", line.lon, r.areaLonTenMicroDeg)
|
||||
assertEquals("line $n area dist", line.dist, r.areaDistanceAMeters)
|
||||
assertEquals("line $n truncated", line.truncated, r.truncated)
|
||||
assertArrayEquals("line $n payload", line.payload, r.payload)
|
||||
accepted++
|
||||
}
|
||||
// the corpus has both kinds in quantity: the test means something only if it does
|
||||
assertTrue("accepted $accepted", accepted > 100)
|
||||
assertTrue("rejected $rejected", rejected > 100)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the corpus covers every message kind and both security shapes`() {
|
||||
val seen = corpus().filter { it.accepted && !it.truncated }.map { Triple(it.port, it.flags and 2 != 0, it.flags and 1 != 0) }.toSet()
|
||||
assertTrue(seen.contains(Triple(2001, false, false))) // unsigned single-hop CAM
|
||||
assertTrue(seen.contains(Triple(2001, true, false))) // signed CAM
|
||||
assertTrue(seen.any { it.first == 2002 && it.third }) // GeoBroadcast DENM
|
||||
assertTrue(seen.any { it.first == 2003 }) // MAPEM
|
||||
assertTrue(seen.any { it.first == 2004 }) // SPATEM
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a frame for a port that is not an ITS message port is rejected, VAM 2018 is accepted`() {
|
||||
val cam = corpus().first { it.accepted && !it.truncated && it.port == 2001 && it.flags == 0 }
|
||||
val vam = patchBtpPort(cam.frame, 2018)
|
||||
assertEquals(2018, GnUnwrap.unwrap(vam)!!.btpPort)
|
||||
assertNull(GnUnwrap.unwrap(patchBtpPort(cam.frame, 1234)))
|
||||
assertNull(GnUnwrap.unwrap(patchBtpPort(cam.frame, 2005)))
|
||||
}
|
||||
|
||||
/** The BTP-B destination port sits right after the GN headers: find it by the known payload start. */
|
||||
private fun patchBtpPort(frame: ByteArray, port: Int): ByteArray {
|
||||
val r = GnUnwrap.unwrap(frame)!!
|
||||
val start = frame.indices.first { i -> i + r.payload.size <= frame.size && (r.payload.indices).all { frame[i + it] == r.payload[it] } }
|
||||
val copy = frame.copyOf()
|
||||
copy[start - 4] = (port shr 8).toByte() // BTP-B header: port u16 BE, port info u16 BE
|
||||
copy[start - 3] = port.toByte()
|
||||
return copy
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `short and garbage input is rejected without an exception`() {
|
||||
assertNull(GnUnwrap.unwrap(ByteArray(0)))
|
||||
assertNull(GnUnwrap.unwrap(ByteArray(23)))
|
||||
assertNull(GnUnwrap.unwrap(ByteArray(200)))
|
||||
assertNull(GnUnwrap.unwrap(ByteArray(200) { 0xFF.toByte() }))
|
||||
// every prefix of a real frame: never throws
|
||||
val frame = corpus().first { it.accepted && !it.truncated && it.flags and 2 != 0 }.frame
|
||||
for (n in 0..frame.size) GnUnwrap.unwrap(frame.copyOf(n))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the V2X_RX body is the one V2xRxFrame reads and the firmware writes`() {
|
||||
val line = corpus().first { it.accepted && !it.truncated && it.port == 2002 && it.flags and 1 != 0 }
|
||||
val r = GnUnwrap.unwrap(line.frame)!!
|
||||
val body = GnUnwrap.toV2xRxBody(r, -61)!!
|
||||
assertEquals(V2xRxFrame.PREFIX_SIZE + r.payload.size, body.size)
|
||||
val parsed = V2xRxFrame.parse(body)!!
|
||||
assertEquals(2002, parsed.btpPort)
|
||||
assertEquals(-61, parsed.rssiDbm)
|
||||
assertNotNull(parsed.geoArea)
|
||||
assertEquals(line.lat / 1e7, parsed.geoArea!!.latitude, 1e-9)
|
||||
assertEquals(line.dist, parsed.geoArea!!.radiusMeters)
|
||||
assertArrayEquals(r.payload, parsed.uper)
|
||||
// flags: bit 0 area, bit 1 signed
|
||||
assertEquals(0x01 or (if (r.signedUnverified) 0x02 else 0), body[3].toInt())
|
||||
// a truncated message is not forwarded
|
||||
val cut = corpus().firstOrNull { it.accepted && it.truncated }
|
||||
if (cut != null) assertNull(GnUnwrap.toV2xRxBody(GnUnwrap.unwrap(cut.frame)!!, -50))
|
||||
assertEquals(127, GnUnwrap.toV2xRxBody(r, 500)!![2].toInt()) // clamped like the firmware
|
||||
assertEquals(-128, GnUnwrap.toV2xRxBody(r, -500)!![2].toInt())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user