package com.hawhamburg.micr0bu import com.hawhamburg.micr0bu.data.transport.BtpDataRequest import com.hawhamburg.micr0bu.data.transport.LinkMessage import com.hawhamburg.micr0bu.data.transport.LinkOpcode import com.hawhamburg.micr0bu.data.transport.LinkResult import com.hawhamburg.micr0bu.data.transport.LinkSecurityProfile import com.hawhamburg.micr0bu.data.transport.PotiUpdate import com.hawhamburg.micr0bu.data.transport.StationConfigure import com.hawhamburg.micr0bu.data.transport.StationInfo import com.hawhamburg.micr0bu.data.transport.StationStatus import com.hawhamburg.micr0bu.data.transport.credentialsSegment import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNotNull import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test /** * Pins the phone side of the station-link message layer. * * ## Where the expected bytes come from * 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. */ class StationLinkTest { private fun String.hexToBytes(): ByteArray = chunked(2).map { it.toInt(16).toByte() }.toByteArray() private fun ByteArray.hex(): String = joinToString("") { "%02x".format(it) } @Test fun `STATION_CONFIGURE matches the Python encoder`() { // 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( "0100070002b400140100", LinkMessage(LinkOpcode.STATION_CONFIGURE, 7, body).encode().hex(), ) } @Test fun `STATION_CONFIGURE with a MAC is the 16-octet form that names the address`() { // type 2, security on, address_configuration 0 (AUTO), the MAC, no beaconing, channel 180, 20 dBm, TX+RX, DP2, 1 s val body = StationConfigure(stationType = 2, mid = "021122334455".hexToBytes()).encode() assertEquals( "0100070002010002112233445500b40014020205", LinkMessage(LinkOpcode.STATION_CONFIGURE, 7, body).encode().hex(), ) // another MAC is another configuration (the station is reconfigured), the same MAC is the same one assertEquals(StationConfigure(2, mid = "021122334455".hexToBytes()), StationConfigure(2, mid = "021122334455".hexToBytes())) assertFalse(StationConfigure(2, mid = "021122334455".hexToBytes()) == StationConfigure(2, mid = "021122334466".hexToBytes())) assertFalse(StationConfigure(2, mid = "021122334455".hexToBytes()) == StationConfigure(2)) } @Test fun `POTI_UPDATE matches the Python encoder`() { val body = PotiUpdate( timestampMs = 717_254_800_123L, latTenMicroDeg = 535_546_667, lonTenMicroDeg = 100_223_889, semiMajorCm = 486, semiMinorCm = 486, speedCms = 543, headingDeciDeg = 1234, pai = true, ).encode() assertEquals( "02000800fbb2b7ffa60000002bcbeb1f914bf905e601e60100000e000000001f02d204", LinkMessage(LinkOpcode.POTI_UPDATE, 8, body).encode().hex(), ) } @Test fun `BTP_DATA_REQUEST for a signed CAM matches the Python encoder`() { val body = BtpDataRequest( destinationPort = 2001, itsAid = 36, securityProfile = LinkSecurityProfile.SECURED, permissions = "010000".hexToBytes(), flSdu = "0102030405".hexToBytes(), ).encode() assertEquals( "0300090001d1070000010102ffff000000000024000000030100000005000102030405", LinkMessage(LinkOpcode.BTP_DATA_REQUEST, 9, body).encode().hex(), ) } @Test fun `CREDENTIALS_PROVISION segment matches the Python encoder`() { val body = credentialsSegment(totalLength = 695, offset = 240, segment = ByteArray(3) { 0xAB.toByte() }) 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 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) assertEquals(1, status.tickets) assertEquals(11L, status.signedMessages) assertEquals(12L, status.refusedNoTicket) assertEquals(13L, status.refusedChangePending) assertEquals(14L, status.refusedPermission) assertEquals(15L, status.signFailed) assertEquals(16L, status.verified) assertEquals(17L, status.rejected) assertEquals(18L, status.requestsAccepted) assertEquals(19L, status.requestsRefused) assertEquals(21L, status.radioSubmitted) assertEquals(22L, status.radioFailed) assertEquals(23L, status.radioReceived) assertEquals(24L, status.radioDropped) assertEquals(26L, status.linkCrcErrors) assertEquals(27L, status.linkMalformed) assertEquals(28L, status.potiUpdates) assertEquals(717_254_800_123L, status.itsTimeMs) } @Test 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))) } @Test 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(60, status.batteryPercent) } @Test 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 `no voltage means no battery`() { val status = StationStatus.decode(status131(0))!! assertFalse(status.batteryLow) assertFalse(status.batteryCritical) assertNull(status.batteryPercent) } @Test fun `LiPo percent is monotonic, clamped and anchored`() { assertEquals(0, StationStatus.lipoPercent(3000)) assertEquals(0, StationStatus.lipoPercent(3300)) assertEquals(5, StationStatus.lipoPercent(3500)) assertEquals(100, StationStatus.lipoPercent(4200)) assertEquals(100, StationStatus.lipoPercent(4300)) var last = -1 for (mv in 3000..4300 step 10) { val p = StationStatus.lipoPercent(mv) assertTrue("$mv mV -> $p% went down", p >= last) last = p } } @Test fun `CONFIRM of STATION_CONFIGURE carries the credential state`() { val message = LinkMessage.decode("8000070000120800021122334455b80b49387a4c12eb0101".hexToBytes())!! val result = LinkResult.decode(message.body)!! assertTrue(result.accepted) assertEquals(StationInfo(credentialsLoaded = true, tickets = 1), StationInfo.decode(result.detail)) } }