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:
@@ -45,7 +45,7 @@ private const val TAG = "BleLinkTransport"
|
||||
|
||||
/**
|
||||
* BLE GATT central for the micrOBU's station link: the Android counterpart of the firmware's
|
||||
* `obu-firmware/main/simple_ble.cpp` (from the colleague's microbu-esp32c5) and of their Python
|
||||
* `obu-firmware/main/simple_ble.cpp` (from Niklas Dathe's microbu-esp32c5) and of their Python
|
||||
* `microbu_link/ble_transport.py`, which this follows step for step.
|
||||
*
|
||||
* ## The GATT layout (what the firmware actually implements)
|
||||
@@ -335,7 +335,7 @@ class BleLinkTransport @Inject constructor(
|
||||
* does not finish within [BOND_TIMEOUT_MS].
|
||||
*
|
||||
* The probe is a read of the STATUS characteristic, which needs an encrypted and authenticated
|
||||
* link, as the colleague's Python transport does. Android answers a read the link is not
|
||||
* link, as Niklas Dathe's Python transport does. Android answers a read the link is not
|
||||
* secure enough for by encrypting, or by pairing and showing the passkey dialog, and then
|
||||
* retries the read itself. While it pairs, the card says which passkey to type.
|
||||
*/
|
||||
|
||||
@@ -95,6 +95,7 @@ class Esp32Link @Inject constructor(
|
||||
companion object {
|
||||
private const val TAG = "Esp32Link"
|
||||
private const val REPLY_TIMEOUT_MS = 3_000L
|
||||
private const val STATUS_POLL_MS = 1_000L
|
||||
/** Applying a bundle verifies the chain and rebuilds the stack on the C5: seconds, not ms. */
|
||||
private const val PROVISION_TIMEOUT_MS = 15_000L
|
||||
private const val SEGMENT_SIZE = 240
|
||||
@@ -114,7 +115,7 @@ class Esp32Link @Inject constructor(
|
||||
const val ITS_AID_VAM = 638L
|
||||
/** CAM SSP version 1, no special-vehicle permissions: what the demo ticket grants for ITS-AID 36. */
|
||||
val SSP_CAM = byteArrayOf(0x01, 0x00, 0x00)
|
||||
/** VRU SSP as the demo ticket grants for ITS-AID 638 (same as the colleague's VBS). */
|
||||
/** VRU SSP as the demo ticket grants for ITS-AID 638 (same as Niklas Dathe's VBS). */
|
||||
val SSP_VAM = byteArrayOf(0x01)
|
||||
}
|
||||
|
||||
@@ -170,6 +171,7 @@ class Esp32Link @Inject constructor(
|
||||
@Volatile private var lastPotiFixMs: Long? = null
|
||||
|
||||
init {
|
||||
scope.launch { pollStatus() }
|
||||
scope.launch { usb.incomingFrames.collect { onUsbFrame(it) } }
|
||||
scope.launch { ble.incoming.collect { onLinkMessage(it) } }
|
||||
scope.launch { usb.linkStatus.collect { if (it != null) _linkStatus.value = it } }
|
||||
@@ -232,7 +234,7 @@ class Esp32Link @Inject constructor(
|
||||
|
||||
private suspend fun sendStationLink(its: OutgoingIts): Boolean {
|
||||
val pv = its.positionVector
|
||||
if (!ensureConfigured(StationConfigure(stationType = pv.stationType, mid = pv.mac))) return false
|
||||
if (!ensureConfigured(StationConfigure(stationType = pv.stationType))) return false
|
||||
// The micrOBU's ITS clock must never be sent backwards: past 1 s it answers
|
||||
// time_regression and rebuilds its whole stack. Two things tried to. The transmit loops
|
||||
// re-send the latest GNSS fix every tick while fused location pauses, so an old fix time
|
||||
@@ -271,7 +273,7 @@ class Esp32Link @Inject constructor(
|
||||
private suspend fun ensureConfigured(wanted: StationConfigure?): Boolean = sessionMutex.withLock {
|
||||
val current = configured
|
||||
if (current != null && (wanted == null || current == wanted)) return@withLock true
|
||||
val config = wanted ?: StationConfigure(stationType = StationType.CYCLIST, mid = pseudonymManager.current().mac)
|
||||
val config = wanted ?: StationConfigure(stationType = StationType.CYCLIST)
|
||||
_detail.value = "Configuring the micrOBU"
|
||||
val result = request(LinkOpcode.STATION_CONFIGURE, config.encode(), REPLY_TIMEOUT_MS)
|
||||
if (result == null || !result.accepted) {
|
||||
@@ -371,7 +373,7 @@ class Esp32Link @Inject constructor(
|
||||
val message = LinkMessage.decode(octets) ?: return
|
||||
when (message.opcode) {
|
||||
LinkOpcode.V2X_RX -> _incomingFrames.tryEmit(DecodedFrame(SerialFrameType.V2X_RX, message.body))
|
||||
LinkOpcode.RESULT -> {
|
||||
LinkOpcode.CONFIRM -> {
|
||||
val result = LinkResult.decode(message.body) ?: return
|
||||
val waiting = pending.remove(message.sequence)
|
||||
if (waiting != null) {
|
||||
@@ -387,26 +389,37 @@ class Esp32Link @Inject constructor(
|
||||
noteRefusal("micrOBU refused a request: ${LinkResultCode.name(result.code)}")
|
||||
}
|
||||
}
|
||||
LinkOpcode.STATUS -> {
|
||||
val status = StationStatus.decode(message.body) ?: return
|
||||
val first = _protocol.value != Esp32Protocol.STATION_LINK
|
||||
_protocol.value = Esp32Protocol.STATION_LINK
|
||||
_stationStatus.value = status
|
||||
_linkStatus.value = EspLinkStatus(
|
||||
status = 0,
|
||||
oversizeDrops = 0,
|
||||
txFailures = status.radioFailed.coerceAtMost(0xFFFF).toInt(),
|
||||
rxCrcErrors = status.linkCrcErrors.coerceAtMost(0xFFFF).toInt(),
|
||||
rxQueueDrops = status.radioDropped.coerceAtMost(0xFFFF).toInt(),
|
||||
)
|
||||
// Board reset (or first contact): configure now, so its receiver runs even before
|
||||
// the first message is sent.
|
||||
if (!status.configured) { configured = null; lastPotiMs = null; lastPotiFixMs = null }
|
||||
if (first || !status.configured) scope.launch { ensureConfigured(null) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Asks for the status snapshot once a second while connected (the board does not push it). */
|
||||
private suspend fun pollStatus() {
|
||||
while (true) {
|
||||
delay(STATUS_POLL_MS)
|
||||
if (state.value != Esp32LinkState.CONNECTED || _protocol.value == Esp32Protocol.LEGACY_SERIAL) continue
|
||||
val result = request(LinkOpcode.STATUS_REQUEST, ByteArray(0), REPLY_TIMEOUT_MS) ?: continue
|
||||
if (!result.accepted) continue
|
||||
handleStatus(StationStatus.decode(result.detail) ?: continue)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleStatus(status: StationStatus) {
|
||||
val first = _protocol.value != Esp32Protocol.STATION_LINK
|
||||
_protocol.value = Esp32Protocol.STATION_LINK
|
||||
_stationStatus.value = status
|
||||
_linkStatus.value = EspLinkStatus(
|
||||
status = 0,
|
||||
oversizeDrops = 0,
|
||||
txFailures = status.radioFailed.coerceAtMost(0xFFFF).toInt(),
|
||||
rxCrcErrors = status.linkCrcErrors.coerceAtMost(0xFFFF).toInt(),
|
||||
rxQueueDrops = status.radioDropped.coerceAtMost(0xFFFF).toInt(),
|
||||
)
|
||||
// Board reset (or first contact): configure now, so its receiver runs even before the first
|
||||
// message is sent.
|
||||
if (!status.configured) { configured = null; lastPotiMs = null; lastPotiFixMs = null }
|
||||
if (first || !status.configured) scope.launch { ensureConfigured(null) }
|
||||
}
|
||||
|
||||
private fun noteRefusal(line: String) {
|
||||
val now = System.currentTimeMillis()
|
||||
if (now - lastRefusalLogMs < 5_000) return
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.hawhamburg.micr0bu.data.transport
|
||||
|
||||
/**
|
||||
* Phone side of the station-link message layer, version 1: the protocol of the colleague's
|
||||
* Phone side of the station-link message layer, version 1: the protocol of Niklas Dathe's
|
||||
* vanetza-idf ESP32-C5 firmware (microbu-esp32c5/station-link/README.md), which obu-firmware runs
|
||||
* since 2026-09-23. Kotlin counterpart of `obu-firmware/main/link_protocol.hpp` and of the
|
||||
* colleague's Python `microbu_link/messages.py`; the unit test pins these encoders to bytes that
|
||||
* Niklas Dathe's Python `microbu_link/messages.py`; the unit test pins these encoders to bytes that
|
||||
* Python module produced.
|
||||
*
|
||||
* Transport independent: over USB each message is the payload of one serial frame of type
|
||||
@@ -12,11 +12,12 @@ package com.hawhamburg.micr0bu.data.transport
|
||||
* message is one GATT value (see [BleLinkTransport]).
|
||||
*
|
||||
* Message: `[opcode:1][flags:1][sequence:2 LE][body]`, at most [LINK_MAX_MESSAGE] octets, all
|
||||
* integers little-endian. The phone numbers its requests; the firmware answers with a RESULT
|
||||
* carrying the same sequence.
|
||||
* integers little-endian. The phone numbers its requests; the firmware answers with a CONFIRM
|
||||
* carrying the same sequence (docs/station-link.pdf, obu-firmware/docs). The status snapshot is not
|
||||
* pushed: the phone asks for it with STATUS_REQUEST once a second and finds it in CONFIRM.detail.
|
||||
*
|
||||
* Only what this app uses is implemented: configure, PoTi, BTP-DATA.request, credential
|
||||
* provisioning, RESULT, STATUS, and the MicrOBU extension [LinkOpcode.V2X_RX]. The SF-SAP
|
||||
* provisioning, STATUS_REQUEST, CONFIRM, and the MicrOBU extension [LinkOpcode.V2X_RX]. The SF-SAP
|
||||
* identifier-change primitives are not used: the app owns its pseudonym (see
|
||||
* [com.hawhamburg.micr0bu.data.cam.PseudonymManager]) and reconfigures the station on a change.
|
||||
*/
|
||||
@@ -31,14 +32,13 @@ object LinkOpcode {
|
||||
const val CREDENTIALS_PROVISION = 0x04
|
||||
const val CREDENTIALS_ERASE = 0x05
|
||||
const val STATUS_REQUEST = 0x0C
|
||||
const val RESULT = 0x80
|
||||
const val CONFIRM = 0x80
|
||||
const val BTP_DATA_INDICATION = 0x81
|
||||
const val STATUS = 0x84
|
||||
/** MicrOBU extension: body is exactly the old [SerialFrameType.V2X_RX] payload ([V2xRxFrame]). */
|
||||
const val V2X_RX = 0x85
|
||||
/** MicrOBU extension: body is exactly the old [SerialFrameType.V2X_RX] payload ([V2xRxFrame]). 0x85 is the spec's RAW_FRAME, 0x86 reserved. */
|
||||
const val V2X_RX = 0x90
|
||||
}
|
||||
|
||||
/** RESULT codes: vanetza_idf::Result first, then the link's own. */
|
||||
/** CONFIRM result codes: the stack's own first, then the link's. */
|
||||
object LinkResultCode {
|
||||
const val ACCEPTED = 0
|
||||
const val TIME_REGRESSION = 7
|
||||
@@ -90,37 +90,27 @@ internal class LinkWriter {
|
||||
}
|
||||
|
||||
/**
|
||||
* STATION_CONFIGURE body. (Re)creates the GeoNetworking stack and security entity on the micrOBU.
|
||||
* [mid] is the pseudonym MAC: with [addressConfiguration] 0 (AUTO) it becomes both the GN_ADDR MID
|
||||
* and the 802.11 source address, as with the old CAM_TX_PV prefix.
|
||||
* STATION_CONFIGURE body (6 octets). (Re)creates the GeoNetworking stack and security entity on the
|
||||
* micrOBU. The GN address derives from the authorization ticket (signed) or is drawn at random per
|
||||
* configuration (unsigned), so the app's pseudonym MAC is no longer sent; radio mode, traffic class
|
||||
* and lifetime are fixed by the station (transmit and receive, DP2, 1 s).
|
||||
*/
|
||||
data class StationConfigure(
|
||||
val stationType: Int,
|
||||
val mid: ByteArray,
|
||||
val security: Int = 1,
|
||||
val addressConfiguration: Int = 0,
|
||||
val beaconing: Int = 0,
|
||||
val channelNumber: Int = 180,
|
||||
val transmitPowerDbm: Int = 20,
|
||||
/** 0 off, 1 receive only, 2 transmit and receive. */
|
||||
val radio: Int = 2,
|
||||
/** Raw GN traffic class octet: TC-ID 2, as the previous firmware's geonet.c. */
|
||||
val defaultTrafficClass: Int = 2,
|
||||
/** Raw GN lifetime octet: 1 s. */
|
||||
val defaultLifetime: Int = 0x05,
|
||||
) {
|
||||
init { require(mid.size == 6) }
|
||||
|
||||
fun encode(): ByteArray = LinkWriter()
|
||||
.u8(stationType).u8(security).u8(addressConfiguration).bytes(mid).u8(beaconing)
|
||||
.u16(channelNumber).u8(transmitPowerDbm).u8(radio).u8(defaultTrafficClass).u8(defaultLifetime)
|
||||
.u8(stationType).u16(channelNumber).u8(transmitPowerDbm).u8(security).u8(beaconing)
|
||||
.toByteArray()
|
||||
|
||||
override fun equals(other: Any?): Boolean = other is StationConfigure && encode().contentEquals(other.encode())
|
||||
override fun hashCode(): Int = encode().contentHashCode()
|
||||
}
|
||||
|
||||
/** RESULT detail of STATION_CONFIGURE. */
|
||||
/** CONFIRM detail of STATION_CONFIGURE. */
|
||||
data class StationInfo(val credentialsLoaded: Boolean, val tickets: Int) {
|
||||
companion object {
|
||||
fun decode(detail: ByteArray): StationInfo? =
|
||||
@@ -213,9 +203,9 @@ data class LinkResult(val code: Int, val detail: ByteArray) {
|
||||
}
|
||||
|
||||
/**
|
||||
* STATUS body, sent by the micrOBU every second. Counters are since the last configure. 103 octets in
|
||||
* the colleague's layout; this firmware appends [batteryMv] and [batteryFlags] (106 octets), and a
|
||||
* 103-octet body from an older board still decodes, with the battery left at "not measured".
|
||||
* Status snapshot, the CONFIRM.detail of STATUS_REQUEST (131 octets, docs/station-link.pdf). Counters
|
||||
* are since the last configure. The board reports only the cell voltage ([batteryMv]); the warning
|
||||
* and shutdown thresholds below mirror obu-firmware's Kconfig defaults (3.50 V and 3.30 V).
|
||||
*/
|
||||
data class StationStatus(
|
||||
val uptimeMs: Long,
|
||||
@@ -241,13 +231,11 @@ data class StationStatus(
|
||||
val itsTimeMs: Long,
|
||||
/** Cell voltage in millivolts; 0 when the board does not report one (older firmware, no cell, no sensor). */
|
||||
val batteryMv: Int = 0,
|
||||
/** BATTERY_FLAG_* bits. */
|
||||
val batteryFlags: Int = 0,
|
||||
) {
|
||||
/** The cell is below the firmware's warning threshold. */
|
||||
val batteryLow: Boolean get() = batteryMv > 0 && (batteryFlags and BATTERY_FLAG_LOW) != 0
|
||||
/** The cell is below the warning threshold. */
|
||||
val batteryLow: Boolean get() = batteryMv > 0 && batteryMv < BATTERY_WARN_MV
|
||||
/** The cell is below the shutdown threshold: the board powers down unless this phone keeps it on USB. */
|
||||
val batteryCritical: Boolean get() = batteryMv > 0 && (batteryFlags and BATTERY_FLAG_CRITICAL) != 0
|
||||
val batteryCritical: Boolean get() = batteryMv > 0 && batteryMv < BATTERY_SHUTDOWN_MV
|
||||
/** Rough charge of a LiPo cell from its voltage, or null when not measured. An indicator, not a fuel gauge. */
|
||||
val batteryPercent: Int? get() = if (batteryMv > 0) lipoPercent(batteryMv) else null
|
||||
|
||||
@@ -259,12 +247,10 @@ data class StationStatus(
|
||||
override fun hashCode(): Int = toString().hashCode()
|
||||
|
||||
companion object {
|
||||
/** Octets of the colleague's STATUS body. */
|
||||
const val SIZE = 103
|
||||
/** Octets with the MicrOBU battery fields appended. */
|
||||
const val SIZE_WITH_BATTERY = 106
|
||||
const val BATTERY_FLAG_LOW = 0x01
|
||||
const val BATTERY_FLAG_CRITICAL = 0x02
|
||||
/** Octets of the status snapshot. */
|
||||
const val SIZE = 131
|
||||
const val BATTERY_WARN_MV = 3500
|
||||
const val BATTERY_SHUTDOWN_MV = 3300
|
||||
|
||||
/** Open-circuit voltage (mV) to charge (%) of a single LiPo cell, interpolated linearly. */
|
||||
private val LIPO_CURVE = arrayOf(
|
||||
@@ -285,9 +271,9 @@ data class StationStatus(
|
||||
return 100
|
||||
}
|
||||
|
||||
/** Accepts the 103-octet body and any body of at least 106 octets; 104 or 105 are malformed. */
|
||||
/** Accepts the 131-octet snapshot and any longer one (later fields are appended); shorter is malformed. */
|
||||
fun decode(body: ByteArray): StationStatus? {
|
||||
if (body.size != SIZE && body.size < SIZE_WITH_BATTERY) return null
|
||||
if (body.size < SIZE) return null
|
||||
fun u8(i: Int) = body[i].toInt() and 0xFF
|
||||
fun u32(i: Int) = (0 until 4).fold(0L) { acc, k -> acc or ((body[i + k].toLong() and 0xFF) shl (8 * k)) }
|
||||
fun u64(i: Int) = (0 until 8).fold(0L) { acc, k -> acc or ((body[i + k].toLong() and 0xFF) shl (8 * k)) }
|
||||
@@ -308,8 +294,8 @@ data class StationStatus(
|
||||
// c + 56: link_rx_frames
|
||||
linkCrcErrors = u32(c + 60), linkMalformed = u32(c + 64), potiUpdates = u32(c + 68),
|
||||
itsTimeMs = u64(95),
|
||||
batteryMv = if (body.size >= SIZE_WITH_BATTERY) u8(103) or (u8(104) shl 8) else 0,
|
||||
batteryFlags = if (body.size >= SIZE_WITH_BATTERY) u8(105) else 0,
|
||||
// [103] cbr, dcc, heap and temperature fields the app does not use; [129] battery_mv u16
|
||||
batteryMv = u8(129) or (u8(130) shl 8),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ data class VamContent(
|
||||
* UPER encoder for the VAM of ETSI TS 103 300-3 V2.3.1 (VAM-PDU-Descriptions major version 3,
|
||||
* over the CDD of TS 102 894-2 V2.4.1), for a bicyclist in VRU profile 2.
|
||||
*
|
||||
* Covers the same field set as the colleague's reference VBS (microbu-esp32c5,
|
||||
* Covers the same field set as Niklas Dathe's reference VBS (microbu-esp32c5,
|
||||
* station-link/python/microbu_link/vbs.py, `VbsLite.assemble`), which encodes with asn1tools from
|
||||
* the ETSI modules: header, basic container, the three mandatory fields of the high-frequency
|
||||
* container, and optionally the low-frequency container with profile and size class. No cluster
|
||||
|
||||
@@ -5,7 +5,7 @@ import kotlin.math.abs
|
||||
|
||||
/**
|
||||
* When to send an individual VAM: ETSI TS 103 300-3 V2.3.1 clause 6.4, items 1 to 4, with the
|
||||
* recommended values of Tables 16 and 17 — the same rules the colleague's reference VBS applies
|
||||
* recommended values of Tables 16 and 17 — the same rules Niklas Dathe's reference VBS applies
|
||||
* (microbu-esp32c5/station-link/python/microbu_link/vbs.py, `VbsLite.due`). No clustering, no
|
||||
* redundancy mitigation, T_GenVam fixed at its minimum (no DCC input).
|
||||
*
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user