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
|
* 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.
|
* `microbu_link/ble_transport.py`, which this follows step for step.
|
||||||
*
|
*
|
||||||
* ## The GATT layout (what the firmware actually implements)
|
* ## The GATT layout (what the firmware actually implements)
|
||||||
@@ -335,7 +335,7 @@ class BleLinkTransport @Inject constructor(
|
|||||||
* does not finish within [BOND_TIMEOUT_MS].
|
* does not finish within [BOND_TIMEOUT_MS].
|
||||||
*
|
*
|
||||||
* The probe is a read of the STATUS characteristic, which needs an encrypted and authenticated
|
* 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
|
* 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.
|
* retries the read itself. While it pairs, the card says which passkey to type.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ class Esp32Link @Inject constructor(
|
|||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "Esp32Link"
|
private const val TAG = "Esp32Link"
|
||||||
private const val REPLY_TIMEOUT_MS = 3_000L
|
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. */
|
/** 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 PROVISION_TIMEOUT_MS = 15_000L
|
||||||
private const val SEGMENT_SIZE = 240
|
private const val SEGMENT_SIZE = 240
|
||||||
@@ -114,7 +115,7 @@ class Esp32Link @Inject constructor(
|
|||||||
const val ITS_AID_VAM = 638L
|
const val ITS_AID_VAM = 638L
|
||||||
/** CAM SSP version 1, no special-vehicle permissions: what the demo ticket grants for ITS-AID 36. */
|
/** CAM SSP version 1, no special-vehicle permissions: what the demo ticket grants for ITS-AID 36. */
|
||||||
val SSP_CAM = byteArrayOf(0x01, 0x00, 0x00)
|
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)
|
val SSP_VAM = byteArrayOf(0x01)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,6 +171,7 @@ class Esp32Link @Inject constructor(
|
|||||||
@Volatile private var lastPotiFixMs: Long? = null
|
@Volatile private var lastPotiFixMs: Long? = null
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
scope.launch { pollStatus() }
|
||||||
scope.launch { usb.incomingFrames.collect { onUsbFrame(it) } }
|
scope.launch { usb.incomingFrames.collect { onUsbFrame(it) } }
|
||||||
scope.launch { ble.incoming.collect { onLinkMessage(it) } }
|
scope.launch { ble.incoming.collect { onLinkMessage(it) } }
|
||||||
scope.launch { usb.linkStatus.collect { if (it != null) _linkStatus.value = 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 {
|
private suspend fun sendStationLink(its: OutgoingIts): Boolean {
|
||||||
val pv = its.positionVector
|
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
|
// 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
|
// 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
|
// 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 {
|
private suspend fun ensureConfigured(wanted: StationConfigure?): Boolean = sessionMutex.withLock {
|
||||||
val current = configured
|
val current = configured
|
||||||
if (current != null && (wanted == null || current == wanted)) return@withLock true
|
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"
|
_detail.value = "Configuring the micrOBU"
|
||||||
val result = request(LinkOpcode.STATION_CONFIGURE, config.encode(), REPLY_TIMEOUT_MS)
|
val result = request(LinkOpcode.STATION_CONFIGURE, config.encode(), REPLY_TIMEOUT_MS)
|
||||||
if (result == null || !result.accepted) {
|
if (result == null || !result.accepted) {
|
||||||
@@ -371,7 +373,7 @@ class Esp32Link @Inject constructor(
|
|||||||
val message = LinkMessage.decode(octets) ?: return
|
val message = LinkMessage.decode(octets) ?: return
|
||||||
when (message.opcode) {
|
when (message.opcode) {
|
||||||
LinkOpcode.V2X_RX -> _incomingFrames.tryEmit(DecodedFrame(SerialFrameType.V2X_RX, message.body))
|
LinkOpcode.V2X_RX -> _incomingFrames.tryEmit(DecodedFrame(SerialFrameType.V2X_RX, message.body))
|
||||||
LinkOpcode.RESULT -> {
|
LinkOpcode.CONFIRM -> {
|
||||||
val result = LinkResult.decode(message.body) ?: return
|
val result = LinkResult.decode(message.body) ?: return
|
||||||
val waiting = pending.remove(message.sequence)
|
val waiting = pending.remove(message.sequence)
|
||||||
if (waiting != null) {
|
if (waiting != null) {
|
||||||
@@ -387,26 +389,37 @@ class Esp32Link @Inject constructor(
|
|||||||
noteRefusal("micrOBU refused a request: ${LinkResultCode.name(result.code)}")
|
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) {
|
private fun noteRefusal(line: String) {
|
||||||
val now = System.currentTimeMillis()
|
val now = System.currentTimeMillis()
|
||||||
if (now - lastRefusalLogMs < 5_000) return
|
if (now - lastRefusalLogMs < 5_000) return
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package com.hawhamburg.micr0bu.data.transport
|
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
|
* 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
|
* 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.
|
* Python module produced.
|
||||||
*
|
*
|
||||||
* Transport independent: over USB each message is the payload of one serial frame of type
|
* 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 is one GATT value (see [BleLinkTransport]).
|
||||||
*
|
*
|
||||||
* Message: `[opcode:1][flags:1][sequence:2 LE][body]`, at most [LINK_MAX_MESSAGE] octets, all
|
* 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
|
* integers little-endian. The phone numbers its requests; the firmware answers with a CONFIRM
|
||||||
* carrying the same sequence.
|
* 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
|
* 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
|
* 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.
|
* [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_PROVISION = 0x04
|
||||||
const val CREDENTIALS_ERASE = 0x05
|
const val CREDENTIALS_ERASE = 0x05
|
||||||
const val STATUS_REQUEST = 0x0C
|
const val STATUS_REQUEST = 0x0C
|
||||||
const val RESULT = 0x80
|
const val CONFIRM = 0x80
|
||||||
const val BTP_DATA_INDICATION = 0x81
|
const val BTP_DATA_INDICATION = 0x81
|
||||||
const val STATUS = 0x84
|
/** MicrOBU extension: body is exactly the old [SerialFrameType.V2X_RX] payload ([V2xRxFrame]). 0x85 is the spec's RAW_FRAME, 0x86 reserved. */
|
||||||
/** MicrOBU extension: body is exactly the old [SerialFrameType.V2X_RX] payload ([V2xRxFrame]). */
|
const val V2X_RX = 0x90
|
||||||
const val V2X_RX = 0x85
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 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 {
|
object LinkResultCode {
|
||||||
const val ACCEPTED = 0
|
const val ACCEPTED = 0
|
||||||
const val TIME_REGRESSION = 7
|
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.
|
* STATION_CONFIGURE body (6 octets). (Re)creates the GeoNetworking stack and security entity on the
|
||||||
* [mid] is the pseudonym MAC: with [addressConfiguration] 0 (AUTO) it becomes both the GN_ADDR MID
|
* micrOBU. The GN address derives from the authorization ticket (signed) or is drawn at random per
|
||||||
* and the 802.11 source address, as with the old CAM_TX_PV prefix.
|
* 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(
|
data class StationConfigure(
|
||||||
val stationType: Int,
|
val stationType: Int,
|
||||||
val mid: ByteArray,
|
|
||||||
val security: Int = 1,
|
val security: Int = 1,
|
||||||
val addressConfiguration: Int = 0,
|
|
||||||
val beaconing: Int = 0,
|
val beaconing: Int = 0,
|
||||||
val channelNumber: Int = 180,
|
val channelNumber: Int = 180,
|
||||||
val transmitPowerDbm: Int = 20,
|
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()
|
fun encode(): ByteArray = LinkWriter()
|
||||||
.u8(stationType).u8(security).u8(addressConfiguration).bytes(mid).u8(beaconing)
|
.u8(stationType).u16(channelNumber).u8(transmitPowerDbm).u8(security).u8(beaconing)
|
||||||
.u16(channelNumber).u8(transmitPowerDbm).u8(radio).u8(defaultTrafficClass).u8(defaultLifetime)
|
|
||||||
.toByteArray()
|
.toByteArray()
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean = other is StationConfigure && encode().contentEquals(other.encode())
|
override fun equals(other: Any?): Boolean = other is StationConfigure && encode().contentEquals(other.encode())
|
||||||
override fun hashCode(): Int = encode().contentHashCode()
|
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) {
|
data class StationInfo(val credentialsLoaded: Boolean, val tickets: Int) {
|
||||||
companion object {
|
companion object {
|
||||||
fun decode(detail: ByteArray): StationInfo? =
|
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
|
* Status snapshot, the CONFIRM.detail of STATUS_REQUEST (131 octets, docs/station-link.pdf). Counters
|
||||||
* the colleague's layout; this firmware appends [batteryMv] and [batteryFlags] (106 octets), and a
|
* are since the last configure. The board reports only the cell voltage ([batteryMv]); the warning
|
||||||
* 103-octet body from an older board still decodes, with the battery left at "not measured".
|
* and shutdown thresholds below mirror obu-firmware's Kconfig defaults (3.50 V and 3.30 V).
|
||||||
*/
|
*/
|
||||||
data class StationStatus(
|
data class StationStatus(
|
||||||
val uptimeMs: Long,
|
val uptimeMs: Long,
|
||||||
@@ -241,13 +231,11 @@ data class StationStatus(
|
|||||||
val itsTimeMs: Long,
|
val itsTimeMs: Long,
|
||||||
/** Cell voltage in millivolts; 0 when the board does not report one (older firmware, no cell, no sensor). */
|
/** Cell voltage in millivolts; 0 when the board does not report one (older firmware, no cell, no sensor). */
|
||||||
val batteryMv: Int = 0,
|
val batteryMv: Int = 0,
|
||||||
/** BATTERY_FLAG_* bits. */
|
|
||||||
val batteryFlags: Int = 0,
|
|
||||||
) {
|
) {
|
||||||
/** The cell is below the firmware's warning threshold. */
|
/** The cell is below the warning threshold. */
|
||||||
val batteryLow: Boolean get() = batteryMv > 0 && (batteryFlags and BATTERY_FLAG_LOW) != 0
|
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. */
|
/** 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. */
|
/** 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
|
val batteryPercent: Int? get() = if (batteryMv > 0) lipoPercent(batteryMv) else null
|
||||||
|
|
||||||
@@ -259,12 +247,10 @@ data class StationStatus(
|
|||||||
override fun hashCode(): Int = toString().hashCode()
|
override fun hashCode(): Int = toString().hashCode()
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
/** Octets of the colleague's STATUS body. */
|
/** Octets of the status snapshot. */
|
||||||
const val SIZE = 103
|
const val SIZE = 131
|
||||||
/** Octets with the MicrOBU battery fields appended. */
|
const val BATTERY_WARN_MV = 3500
|
||||||
const val SIZE_WITH_BATTERY = 106
|
const val BATTERY_SHUTDOWN_MV = 3300
|
||||||
const val BATTERY_FLAG_LOW = 0x01
|
|
||||||
const val BATTERY_FLAG_CRITICAL = 0x02
|
|
||||||
|
|
||||||
/** Open-circuit voltage (mV) to charge (%) of a single LiPo cell, interpolated linearly. */
|
/** Open-circuit voltage (mV) to charge (%) of a single LiPo cell, interpolated linearly. */
|
||||||
private val LIPO_CURVE = arrayOf(
|
private val LIPO_CURVE = arrayOf(
|
||||||
@@ -285,9 +271,9 @@ data class StationStatus(
|
|||||||
return 100
|
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? {
|
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 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 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)) }
|
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
|
// c + 56: link_rx_frames
|
||||||
linkCrcErrors = u32(c + 60), linkMalformed = u32(c + 64), potiUpdates = u32(c + 68),
|
linkCrcErrors = u32(c + 60), linkMalformed = u32(c + 64), potiUpdates = u32(c + 68),
|
||||||
itsTimeMs = u64(95),
|
itsTimeMs = u64(95),
|
||||||
batteryMv = if (body.size >= SIZE_WITH_BATTERY) u8(103) or (u8(104) shl 8) else 0,
|
// [103] cbr, dcc, heap and temperature fields the app does not use; [129] battery_mv u16
|
||||||
batteryFlags = if (body.size >= SIZE_WITH_BATTERY) u8(105) else 0,
|
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,
|
* 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.
|
* 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
|
* 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
|
* 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
|
* 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
|
* 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
|
* (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).
|
* 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.
|
* Pins the phone side of the station-link message layer.
|
||||||
*
|
*
|
||||||
* ## Where the expected bytes come from
|
* ## 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`),
|
* 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
|
* 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.
|
* with, so an agreement here is agreement with a second, independent implementation.
|
||||||
@@ -34,9 +34,10 @@ class StationLinkTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `STATION_CONFIGURE matches the Python encoder`() {
|
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(
|
assertEquals(
|
||||||
"0100070002010002112233445500b40014020205",
|
"0100070002b400140100",
|
||||||
LinkMessage(LinkOpcode.STATION_CONFIGURE, 7, body).encode().hex(),
|
LinkMessage(LinkOpcode.STATION_CONFIGURE, 7, body).encode().hex(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -71,17 +72,22 @@ class StationLinkTest {
|
|||||||
assertEquals("04000a00b702f00003ababab", LinkMessage(LinkOpcode.CREDENTIALS_PROVISION, 10, body).encode().hex())
|
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
|
@Test
|
||||||
fun `STATUS from the Python encoder decodes field by field`() {
|
fun `STATUS snapshot decodes field by field`() {
|
||||||
val message = LinkMessage.decode(
|
val body = status131(3871)
|
||||||
("8400341240e20100010800021122334455b80b49387a4c12eb00010b0000000c0000000d0000000e000000" +
|
assertEquals(StationStatus.SIZE, body.size)
|
||||||
"0f000000100000001100000012000000130000001400000015000000160000001700000018000000" +
|
val status = StationStatus.decode(body)!!
|
||||||
"190000001a0000001b0000001c000000fbb2b7ffa6000000").hexToBytes(),
|
|
||||||
)
|
|
||||||
assertNotNull(message)
|
|
||||||
assertEquals(LinkOpcode.STATUS, message!!.opcode)
|
|
||||||
assertEquals(0x1234, message.sequence)
|
|
||||||
val status = StationStatus.decode(message.body)!!
|
|
||||||
assertEquals(123_456L, status.uptimeMs)
|
assertEquals(123_456L, status.uptimeMs)
|
||||||
assertTrue(status.configured)
|
assertTrue(status.configured)
|
||||||
assertArrayEquals("b80b49387a4c12eb".hexToBytes(), status.identifier)
|
assertArrayEquals("b80b49387a4c12eb".hexToBytes(), status.identifier)
|
||||||
@@ -106,67 +112,35 @@ class StationLinkTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `a STATUS of the wrong length is refused, as the Python decoder does`() {
|
fun `a STATUS shorter than 131 octets is refused, a longer one is accepted`() {
|
||||||
assertNull(StationStatus.decode(ByteArray(StationStatus.SIZE + 1)))
|
|
||||||
assertNull(StationStatus.decode(ByteArray(StationStatus.SIZE + 2)))
|
|
||||||
assertNull(StationStatus.decode(ByteArray(StationStatus.SIZE - 1)))
|
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
|
@Test
|
||||||
fun `a 103 octet STATUS from an older board has no battery`() {
|
fun `the battery voltage at octet 129 is decoded`() {
|
||||||
val legacy = legacyStatusBody
|
val status = StationStatus.decode(status131(3871))!!
|
||||||
assertEquals(StationStatus.SIZE, legacy.size)
|
assertEquals(3871, status.batteryMv)
|
||||||
val status = StationStatus.decode(legacy)!!
|
|
||||||
assertEquals(0, status.batteryMv)
|
|
||||||
assertNull(status.batteryPercent)
|
|
||||||
assertFalse(status.batteryLow)
|
assertFalse(status.batteryLow)
|
||||||
assertFalse(status.batteryCritical)
|
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(60, status.batteryPercent)
|
||||||
assertEquals(28L, status.potiUpdates)
|
|
||||||
assertEquals(717_254_800_123L, status.itsTimeMs)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `critical flag, and octets of a later extension are tolerated`() {
|
fun `low and critical follow the voltage thresholds`() {
|
||||||
// 3290 mV = 0x0CDA, flags: low + critical, then two octets of a later extension
|
val low = StationStatus.decode(status131(3400))!!
|
||||||
val body = legacyStatusBody + "da0c03aabb".hexToBytes()
|
assertTrue(low.batteryLow)
|
||||||
val status = StationStatus.decode(body)!!
|
assertFalse(low.batteryCritical)
|
||||||
assertEquals(3290, status.batteryMv)
|
val critical = StationStatus.decode(status131(3290))!!
|
||||||
assertTrue(status.batteryLow)
|
assertTrue(critical.batteryLow)
|
||||||
assertTrue(status.batteryCritical)
|
assertTrue(critical.batteryCritical)
|
||||||
assertEquals(0, status.batteryPercent)
|
assertEquals(0, critical.batteryPercent)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `half a battery field is malformed`() {
|
fun `no voltage means no battery`() {
|
||||||
assertNull(StationStatus.decode(legacyStatusBody + "1f".hexToBytes()))
|
val status = StationStatus.decode(status131(0))!!
|
||||||
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)!!
|
|
||||||
assertFalse(status.batteryLow)
|
assertFalse(status.batteryLow)
|
||||||
assertFalse(status.batteryCritical)
|
assertFalse(status.batteryCritical)
|
||||||
assertNull(status.batteryPercent)
|
assertNull(status.batteryPercent)
|
||||||
@@ -188,7 +162,7 @@ class StationLinkTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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 message = LinkMessage.decode("8000070000120800021122334455b80b49387a4c12eb0101".hexToBytes())!!
|
||||||
val result = LinkResult.decode(message.body)!!
|
val result = LinkResult.decode(message.body)!!
|
||||||
assertTrue(result.accepted)
|
assertTrue(result.accepted)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import org.junit.Test
|
|||||||
* ## Where the expected bytes come from
|
* ## Where the expected bytes come from
|
||||||
* asn1tools 0.167, compiling the ETSI modules vanetza-idf ships (asn1/release2:
|
* 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),
|
* 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
|
* (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
|
* 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
|
* 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