Merge branch 'dev/its-time-tai' into 'main'
Count ITS time in TAI, with the leap seconds since 2004 See merge request urban-mobility-lab/microbu/microbuapp!2
This commit is contained in:
@@ -42,9 +42,6 @@ object DenmUperCodec {
|
||||
private const val MESSAGE_ID_DENM = 1
|
||||
private const val PROTOCOL_VERSION = 2
|
||||
|
||||
/** TimestampIts epoch: 2004-01-01T00:00:00Z in Unix epoch milliseconds. */
|
||||
private const val TS_ITS_EPOCH_MS = 1_072_915_200_000L
|
||||
|
||||
/**
|
||||
* Decodes a UPER DENM into a [DenmEvent].
|
||||
*
|
||||
@@ -147,7 +144,8 @@ object DenmUperCodec {
|
||||
// on it; if any of it is ever needed, the unparsed optionals must be consumed in order
|
||||
// first or every later read lands at the wrong bit offset.
|
||||
|
||||
val detectionTimeMs = detectionTimeIts + TS_ITS_EPOCH_MS
|
||||
// TimestampIts is TAI-based: back to UTC through the one conversion the transmit side uses.
|
||||
val detectionTimeMs = ItsTime.epochMs(detectionTimeIts)
|
||||
|
||||
return DenmEvent(
|
||||
stationId = originatingStationId,
|
||||
|
||||
@@ -1,30 +1,65 @@
|
||||
package com.hawhamburg.micr0bu.domain.asn1
|
||||
|
||||
/**
|
||||
* ITS time, as used by every timestamp this app puts on the air.
|
||||
* ITS time, as used by every timestamp this app puts on the air or reads from it.
|
||||
*
|
||||
* TimestampIts (ETSI TS 102 894-2) counts milliseconds from 2004-01-01T00:00:00Z. Two fields in a
|
||||
* single transmitted frame come from it: the CAM's generationDeltaTime (modulo 65536) and the
|
||||
* GeoNetworking Source Position Vector's TST (modulo 2^32). A receiver can compare the two, so
|
||||
* they must follow one rule. Both go through here so they cannot drift apart.
|
||||
* TimestampIts (ETSI TS 102 894-2 V2.5.1) is "the number of elapsed (TAI) milliseconds since the ITS
|
||||
* Epoch", 2004-01-01T00:00:00.000 UTC, counted without interruption, so it is not altered by the leap
|
||||
* seconds UTC inserts. It therefore runs ahead of UTC by the leap seconds inserted since 2004: 5 s since
|
||||
* 2017-01-01 (the standard's own example: 2007-01-01T00:00:00.000 UTC is 94 694 401 000 ms, one leap
|
||||
* second included). Three fields in a single transmitted frame come from it: the CAM's and VAM's
|
||||
* generationDeltaTime (modulo 65536), the GeoNetworking Source Position Vector's TST (modulo 2^32) and,
|
||||
* on the station-link path, the PoTi timestamp that sets the micrOBU's ITS clock, which the security
|
||||
* entity stamps into every signature's generationTime and checks received messages against. A receiver
|
||||
* compares them with its own TAI clock, so they all go through here and cannot drift apart.
|
||||
*
|
||||
* Until 2026-10 this was Unix time minus the epoch, without the leap seconds: the micrOBU's clock ran
|
||||
* 5 s behind, so it rejected correctly timed VAMs from other stations as Invalid_Timestamp and restarted
|
||||
* its stack whenever another source (the PC tools, which add the leap seconds) had set it correctly.
|
||||
*
|
||||
* **Which clock.** The input should be GNSS time, not the phone's wall clock. A phone with no SIM
|
||||
* and no internet time has no automatic time source at all, and the bench phone was found 24
|
||||
* minutes fast that way. `GnssTimeSource` moves a timestamp onto GNSS time, using [onGnssTime],
|
||||
* before it gets here.
|
||||
*
|
||||
* **Open question: leap seconds.** This is Unix time minus the 2004 epoch, with no leap-second
|
||||
* term. If TimestampIts is read as TAI-based, the correct value is currently 5 s higher, for the
|
||||
* five leap seconds inserted since 2004. Whichever reading turns out right, it is changed here and
|
||||
* nowhere else. Settling it needs a frame from a third-party stack with a trusted clock, such as
|
||||
* the RSU's CAM compared against GNSS time, and no such traffic was on air when this was written.
|
||||
* before it gets here. Android reports GNSS time as UTC (Unix epoch ms), so the leap seconds are
|
||||
* added here, not there.
|
||||
*/
|
||||
object ItsTime {
|
||||
/** 2004-01-01T00:00:00Z in Unix epoch milliseconds. */
|
||||
const val EPOCH_MS = 1_072_915_200_000L
|
||||
|
||||
/** TimestampIts for wall-clock [epochMs], before any modulo is applied. */
|
||||
fun timestampIts(epochMs: Long): Long = epochMs - EPOCH_MS
|
||||
/**
|
||||
* Unix epoch ms (UTC) from which each leap second inserted after the ITS epoch counts, from IERS via
|
||||
* the IANA leap-seconds.list (TAI-UTC 32 s at the epoch, 37 s since 2017-01-01; the file of
|
||||
* 2026-07 announces none before its expiry on 2027-06-28). A new leap second is added here when
|
||||
* IERS Bulletin C announces one.
|
||||
*/
|
||||
private val LEAP_SECONDS_FROM_MS = longArrayOf(
|
||||
1_136_073_600_000L, // 2006-01-01
|
||||
1_230_768_000_000L, // 2009-01-01
|
||||
1_341_100_800_000L, // 2012-07-01
|
||||
1_435_708_800_000L, // 2015-07-01
|
||||
1_483_228_800_000L, // 2017-01-01
|
||||
)
|
||||
|
||||
/** Leap seconds inserted between the ITS epoch and the UTC instant [epochMs]. */
|
||||
fun leapSecondsAt(epochMs: Long): Int = LEAP_SECONDS_FROM_MS.count { epochMs >= it }
|
||||
|
||||
/** TimestampIts for the UTC instant [epochMs] (Unix epoch ms), before any modulo is applied. */
|
||||
fun timestampIts(epochMs: Long): Long = epochMs - EPOCH_MS + 1_000L * leapSecondsAt(epochMs)
|
||||
|
||||
/**
|
||||
* The UTC instant (Unix epoch ms) of [timestampIts], the inverse of [timestampIts]. During an
|
||||
* inserted leap second, which UTC cannot name, the first millisecond after it is returned.
|
||||
*/
|
||||
fun epochMs(timestampIts: Long): Long {
|
||||
var leaps = 0
|
||||
for ((index, from) in LEAP_SECONDS_FROM_MS.withIndex()) {
|
||||
val itsAtFrom = timestampIts(from) // first TimestampIts after leap second index + 1
|
||||
if (timestampIts >= itsAtFrom) leaps = index + 1
|
||||
else if (timestampIts >= itsAtFrom - 1_000L) return from // inside the inserted second
|
||||
}
|
||||
return timestampIts + EPOCH_MS - 1_000L * leaps
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves [systemMs], a reading of this phone's wall clock, onto GNSS time, using one pair of
|
||||
|
||||
@@ -31,9 +31,12 @@ class CamEncodeGoldenTest {
|
||||
/**
|
||||
* asn1tools-verified encoding of [referenceCam]. The trap this pins down: `YawRateConfidence`
|
||||
* has nine enumerands (0..8), so it needs 4 bits and `unavailable` is 8 - not 3 bits and 7.
|
||||
* generationDeltaTime 19080 is TimestampIts mod 65536 with the 5 leap seconds since 2004 (TAI,
|
||||
* TS 102 894-2 V2.5.1); regenerated 2026-10 through asn1tools from the earlier UTC-based vector
|
||||
* (14080), every other bit unchanged.
|
||||
*/
|
||||
private val expectedHex =
|
||||
"0202000f423f3700402ab215af6e286477dffffffc23b7743e0027ffc0d0fe0118329337feebfff6000000"
|
||||
"0202000f423f4a88402ab215af6e286477dffffffc23b7743e0027ffc0d0fe0118329337feebfff6000000"
|
||||
|
||||
private val referenceCam = Cam(
|
||||
stationId = 999_999L,
|
||||
|
||||
@@ -104,7 +104,7 @@ class CamTxPvSerialTest {
|
||||
assertTrue(pv.pai)
|
||||
// The GN TST and the CAM's generationDeltaTime must follow one time rule.
|
||||
assertEquals(ItsTime.timestampIts(cam.timestamp), pv.tstMs)
|
||||
assertEquals(716_121_572_779L, pv.tstMs)
|
||||
assertEquals(716_121_577_779L, pv.tstMs) // TAI: Unix ms - ITS epoch + 5 leap seconds
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -145,7 +145,7 @@ class DenmAirReceiveTest {
|
||||
// detectionTime is a 42-bit TimestampIts counted from the 2004 ITS epoch. Getting either
|
||||
// the width or the epoch wrong lands the hazard decades away, so the absolute value is
|
||||
// asserted rather than a range.
|
||||
assertEquals(1_786_979_460_563L, denm.detectionTimeMs)
|
||||
assertEquals(1_786_979_455_563L, denm.detectionTimeMs) // TimestampIts is TAI: 5 leap seconds less in UTC
|
||||
|
||||
// Carried through from the GeoNetworking header and the serial prefix, not the payload.
|
||||
assertEquals(1000, denm.relevanceRadiusM)
|
||||
@@ -164,7 +164,7 @@ class DenmAirReceiveTest {
|
||||
assertNull("a terminating DENM carries no SituationContainer", termDenm.causeCode)
|
||||
assertEquals(4_194_380_752L, termDenm.stationId)
|
||||
assertEquals(6, termDenm.sequenceNumber)
|
||||
assertEquals(1_786_980_053_703L, termDenm.detectionTimeMs)
|
||||
assertEquals(1_786_980_048_703L, termDenm.detectionTimeMs)
|
||||
|
||||
// The whole point of keying dedup on actionID: the termination must land on the same key as
|
||||
// the event it ends, so filtering terminations actually removes that hazard from the map
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.hawhamburg.micr0bu
|
||||
|
||||
import com.hawhamburg.micr0bu.domain.asn1.CamUperCodec
|
||||
import com.hawhamburg.micr0bu.domain.asn1.ItsTime
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import java.time.Instant
|
||||
|
||||
/**
|
||||
* TimestampIts is TAI-based (ETSI TS 102 894-2 V2.5.1): it runs ahead of UTC by the leap seconds
|
||||
* inserted since the ITS epoch. Values from the standard's own example and the IANA leap-seconds.list.
|
||||
*/
|
||||
class ItsTimeLeapSecondTest {
|
||||
|
||||
private fun utc(text: String) = Instant.parse(text).toEpochMilli()
|
||||
|
||||
@Test
|
||||
fun `the ITS epoch is zero`() {
|
||||
assertEquals(0L, ItsTime.timestampIts(utc("2004-01-01T00:00:00Z")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the standard's example 2007-01-01 includes one leap second`() {
|
||||
assertEquals(94_694_401_000L, ItsTime.timestampIts(utc("2007-01-01T00:00:00Z")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `today TimestampIts is 5 s ahead of Unix time minus the epoch`() {
|
||||
val now = utc("2026-10-02T16:00:00Z")
|
||||
assertEquals(now - ItsTime.EPOCH_MS + 5_000L, ItsTime.timestampIts(now))
|
||||
// the value the micrOBU station-link reference (vbs.py its_timestamp_ms) produces for the same instant
|
||||
assertEquals(718_041_605_000L, ItsTime.timestampIts(now))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the leap second of 2017 counts from 2017-01-01 on`() {
|
||||
assertEquals(4, ItsTime.leapSecondsAt(utc("2016-12-31T23:59:59.999Z")))
|
||||
assertEquals(5, ItsTime.leapSecondsAt(utc("2017-01-01T00:00:00Z")))
|
||||
// the inserted second 23:59:60 lies between: two UTC instants 1 ms apart are 1001 ms apart in TAI
|
||||
assertEquals(1_001L, ItsTime.timestampIts(utc("2017-01-01T00:00:00Z")) -
|
||||
ItsTime.timestampIts(utc("2016-12-31T23:59:59.999Z")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `back to UTC is the inverse, also across every leap second`() {
|
||||
for (t in listOf("2004-01-01T00:00:00Z", "2005-12-31T23:59:59.999Z", "2006-01-01T00:00:00Z",
|
||||
"2012-06-30T23:59:59.500Z", "2012-07-01T00:00:00.001Z", "2017-01-01T00:00:00Z", "2026-10-02T16:00:00.123Z")) {
|
||||
assertEquals(t, utc(t), ItsTime.epochMs(ItsTime.timestampIts(utc(t))))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a TimestampIts inside an inserted leap second maps to the end of it`() {
|
||||
val its = ItsTime.timestampIts(utc("2017-01-01T00:00:00Z")) - 400L // 2016-12-31T23:59:60.600
|
||||
assertEquals(utc("2017-01-01T00:00:00Z"), ItsTime.epochMs(its))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `generationDeltaTime follows TimestampIts`() {
|
||||
val now = utc("2026-10-02T16:00:00Z")
|
||||
assertEquals(Math.floorMod(718_041_605_000L, 65_536L).toInt(), CamUperCodec.generationDeltaTime(now))
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,9 @@ import org.junit.Test
|
||||
* 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
|
||||
* (microbu-esp32c5/station-link/python/microbu_link/vbs.py) builds its VAMs with. Station
|
||||
* 0x12345678, fix at Unix ms 1790170000123 (generationDeltaTime 45819), 53.5546667 N
|
||||
* 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
|
||||
* earlier UTC-based vectors with 45819, every other bit unchanged), 53.5546667 N
|
||||
* 10.0223889 E, 5.43 m/s, heading 123.4 deg, -1.26 m/s^2; every confidence and the altitude
|
||||
* unavailable, as the encoder sends them.
|
||||
*/
|
||||
@@ -36,7 +38,7 @@ class VamUperCodecTest {
|
||||
@Test
|
||||
fun `VAM with the low-frequency container matches asn1tools`() {
|
||||
assertEquals(
|
||||
"031012345678b2fb400aac85a15b8a18ec88f30f3708eddd0f8002697e087ff24f322220",
|
||||
"031012345678c683400aac85a15b8a18ec88f30f3708eddd0f8002697e087ff24f322220",
|
||||
VamUperCodec.encode(content(includeLowFrequency = true)).hex(),
|
||||
)
|
||||
}
|
||||
@@ -44,7 +46,7 @@ class VamUperCodecTest {
|
||||
@Test
|
||||
fun `VAM without the low-frequency container matches asn1tools`() {
|
||||
assertEquals(
|
||||
"031012345678b2fb000aac85a15b8a18ec88f30f3708eddd0f8002697e087ff24f30",
|
||||
"031012345678c683000aac85a15b8a18ec88f30f3708eddd0f8002697e087ff24f30",
|
||||
VamUperCodec.encode(content(includeLowFrequency = false)).hex(),
|
||||
)
|
||||
}
|
||||
@@ -52,7 +54,7 @@ class VamUperCodecTest {
|
||||
@Test
|
||||
fun `unknown accuracy encodes the confidence ellipse as unavailable`() {
|
||||
assertEquals(
|
||||
"031012345678b2fb000aac85a15b8a18ec8fffffff08eddd0f8002697e087ff24f30",
|
||||
"031012345678c683000aac85a15b8a18ec8fffffff08eddd0f8002697e087ff24f30",
|
||||
VamUperCodec.encode(content(includeLowFrequency = false, accuracyM = null)).hex(),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user