Follow station-link revision 2 in the app

CONFIRM replaces RESULT, the status snapshot is polled with STATUS_REQUEST once a
second (131 octets, battery_mv at octet 129; low and critical are derived from the
voltage), STATION_CONFIGURE is the 6-octet form without the pseudonym MAC, and
V2X_RX moves to opcode 0x90. StationLinkTest is updated to the new bytes.
This commit is contained in:
Ashin Walpola
2026-11-11 00:13:57 +01:00
parent 50bbf6552d
commit 4ce1aaf250
7 changed files with 105 additions and 132 deletions
@@ -22,7 +22,7 @@ import org.junit.Test
* Pins the phone side of the station-link message layer.
*
* ## Where the expected bytes come from
* The colleague's Python implementation of the same protocol, microbu-esp32c5
* Niklas Dathe's Python implementation of the same protocol, microbu-esp32c5
* station-link/python/microbu_link/messages.py (`encode_message` over each body's `encode`),
* run with the same field values. That module is what their phone emulator drives the firmware
* with, so an agreement here is agreement with a second, independent implementation.
@@ -34,9 +34,10 @@ class StationLinkTest {
@Test
fun `STATION_CONFIGURE matches the Python encoder`() {
val body = StationConfigure(stationType = 2, mid = "021122334455".hexToBytes()).encode()
// station_type 2, channel 180 (0x00b4), 20 dBm, security on, beaconing off: the 6-octet body of docs/station-link.pdf
val body = StationConfigure(stationType = 2).encode()
assertEquals(
"0100070002010002112233445500b40014020205",
"0100070002b400140100",
LinkMessage(LinkOpcode.STATION_CONFIGURE, 7, body).encode().hex(),
)
}
@@ -71,17 +72,22 @@ class StationLinkTest {
assertEquals("04000a00b702f00003ababab", LinkMessage(LinkOpcode.CREDENTIALS_PROVISION, 10, body).encode().hex())
}
/** The first 103 octets of the Python-encoder STATUS snapshot: counters up to its_time_ms. */
private val statusHead = (
"40e20100010800021122334455b80b49387a4c12eb00010b0000000c0000000d0000000e000000" +
"0f000000100000001100000012000000130000001400000015000000160000001700000018000000" +
"190000001a0000001b0000001c000000fbb2b7ffa6000000"
).hexToBytes()
/** A 131-octet snapshot: [statusHead], 26 octets of CBR, DCC, heap and temperature, then the cell voltage. */
private fun status131(batteryMv: Int): ByteArray =
statusHead + ByteArray(26) + byteArrayOf(batteryMv.toByte(), (batteryMv shr 8).toByte())
@Test
fun `STATUS from the Python encoder decodes field by field`() {
val message = LinkMessage.decode(
("8400341240e20100010800021122334455b80b49387a4c12eb00010b0000000c0000000d0000000e000000" +
"0f000000100000001100000012000000130000001400000015000000160000001700000018000000" +
"190000001a0000001b0000001c000000fbb2b7ffa6000000").hexToBytes(),
)
assertNotNull(message)
assertEquals(LinkOpcode.STATUS, message!!.opcode)
assertEquals(0x1234, message.sequence)
val status = StationStatus.decode(message.body)!!
fun `STATUS snapshot decodes field by field`() {
val body = status131(3871)
assertEquals(StationStatus.SIZE, body.size)
val status = StationStatus.decode(body)!!
assertEquals(123_456L, status.uptimeMs)
assertTrue(status.configured)
assertArrayEquals("b80b49387a4c12eb".hexToBytes(), status.identifier)
@@ -106,67 +112,35 @@ class StationLinkTest {
}
@Test
fun `a STATUS of the wrong length is refused, as the Python decoder does`() {
assertNull(StationStatus.decode(ByteArray(StationStatus.SIZE + 1)))
assertNull(StationStatus.decode(ByteArray(StationStatus.SIZE + 2)))
fun `a STATUS shorter than 131 octets is refused, a longer one is accepted`() {
assertNull(StationStatus.decode(ByteArray(StationStatus.SIZE - 1)))
assertNull(StationStatus.decode(statusHead))
assertNotNull(StationStatus.decode(status131(3871) + ByteArray(4)))
}
/** The STATUS body of the Python-encoder test above, without its 4-octet message header. */
private val legacyStatusBody = (
"40e20100010800021122334455b80b49387a4c12eb00010b0000000c0000000d0000000e000000" +
"0f000000100000001100000012000000130000001400000015000000160000001700000018000000" +
"190000001a0000001b0000001c000000fbb2b7ffa6000000"
).hexToBytes()
@Test
fun `a 103 octet STATUS from an older board has no battery`() {
val legacy = legacyStatusBody
assertEquals(StationStatus.SIZE, legacy.size)
val status = StationStatus.decode(legacy)!!
assertEquals(0, status.batteryMv)
assertNull(status.batteryPercent)
fun `the battery voltage at octet 129 is decoded`() {
val status = StationStatus.decode(status131(3871))!!
assertEquals(3871, status.batteryMv)
assertFalse(status.batteryLow)
assertFalse(status.batteryCritical)
assertEquals(28L, status.potiUpdates)
}
@Test
fun `the battery fields after the 103 octets are decoded`() {
// 3871 mV = 0x0F1F, flags: low
val body = legacyStatusBody + "1f0f01".hexToBytes()
assertEquals(StationStatus.SIZE_WITH_BATTERY, body.size)
val status = StationStatus.decode(body)!!
assertEquals(3871, status.batteryMv)
assertEquals(StationStatus.BATTERY_FLAG_LOW, status.batteryFlags)
assertTrue(status.batteryLow)
assertFalse(status.batteryCritical)
assertEquals(60, status.batteryPercent)
assertEquals(28L, status.potiUpdates)
assertEquals(717_254_800_123L, status.itsTimeMs)
}
@Test
fun `critical flag, and octets of a later extension are tolerated`() {
// 3290 mV = 0x0CDA, flags: low + critical, then two octets of a later extension
val body = legacyStatusBody + "da0c03aabb".hexToBytes()
val status = StationStatus.decode(body)!!
assertEquals(3290, status.batteryMv)
assertTrue(status.batteryLow)
assertTrue(status.batteryCritical)
assertEquals(0, status.batteryPercent)
fun `low and critical follow the voltage thresholds`() {
val low = StationStatus.decode(status131(3400))!!
assertTrue(low.batteryLow)
assertFalse(low.batteryCritical)
val critical = StationStatus.decode(status131(3290))!!
assertTrue(critical.batteryLow)
assertTrue(critical.batteryCritical)
assertEquals(0, critical.batteryPercent)
}
@Test
fun `half a battery field is malformed`() {
assertNull(StationStatus.decode(legacyStatusBody + "1f".hexToBytes()))
assertNull(StationStatus.decode(legacyStatusBody + "1f0f".hexToBytes()))
}
@Test
fun `battery flags are ignored while no voltage is reported`() {
val body = legacyStatusBody + "000003".hexToBytes()
val status = StationStatus.decode(body)!!
fun `no voltage means no battery`() {
val status = StationStatus.decode(status131(0))!!
assertFalse(status.batteryLow)
assertFalse(status.batteryCritical)
assertNull(status.batteryPercent)
@@ -188,7 +162,7 @@ class StationLinkTest {
}
@Test
fun `RESULT of STATION_CONFIGURE carries the credential state`() {
fun `CONFIRM of STATION_CONFIGURE carries the credential state`() {
val message = LinkMessage.decode("8000070000120800021122334455b80b49387a4c12eb0101".hexToBytes())!!
val result = LinkResult.decode(message.body)!!
assertTrue(result.accepted)
@@ -11,7 +11,7 @@ import org.junit.Test
* ## Where the expected bytes come from
* asn1tools 0.167, compiling the ETSI modules vanetza-idf ships (asn1/release2:
* TS102894-2v241-CDD.asn, TS103300-3v231/VAM-PDU-Descriptions.asn and its motorcyclist container),
* encoding the same values as a Python dict: the same toolchain the colleague's reference VBS
* encoding the same values as a Python dict: the same toolchain Niklas Dathe's reference VBS
* (microbu-esp32c5/station-link/python/microbu_link/vbs.py) builds its VAMs with. Station
* 0x12345678, fix at Unix ms 1790170000123 (generationDeltaTime 50819: TimestampIts mod 65536 with the
* 5 leap seconds since 2004, TAI per TS 102 894-2 V2.5.1; regenerated 2026-10 through asn1tools from the