Files
MicrOBU/obu-firmware/main/its_clock.hpp
T
Ashin Walpola 50bbf6552d Rebuild obu-firmware on Niklas Dathe's microbu-firmware
Replace the vanetza-idf port with his current station firmware (microbu-firmware
e4e4bd8: main/, components/microbu_v2x, components/vanetza, external/vanetza) and
re-apply the MicrOBU additions, each marked "MicrOBU:" in the sources:

- V2X_RX (opcode 0x90): every received ITS message, signed or not, through
  gn_unwrap.c, because the stack drops unsigned and foreign-root traffic. 0x85 is
  the spec's RAW_FRAME and 0x86 is reserved. The body is built in v2x_rx.hpp.
- Unsigned transmission (geonet.c) for security profile 1 or station security 0,
  from a random MAC per configuration.
- Radio polled from STATION_CONFIGURE on; manual country policy, TX power
  read-back, static RX buffer, short frames ignored.
- Battery guard (battery_policy.hpp): deep sleep below 3.30 V with no phone on USB.
- No USB writes without a host; last_frame_ms() for the guard.
- 80 KB NVS with the app at 0x20000, 8 MB flash, XIAO ESP32-C5 board, 20 dBm.

Remove the old C firmware, the bench listener and the old vanetza-idf copy. Add
docs/station-link.tex as revision 2 of the interface reference (V2X_RX, unsigned
sending, battery rule, revision history). Add 39 host unit tests (make firmware in
test/host) next to the existing chain, replay, fuzz and battery tests, and point
tools/station_status_watch.py at the new CONFIRM-based status.
2026-11-11 00:13:57 +01:00

57 lines
2.8 KiB
C++

#pragma once
// How a PoTi time reference from the phone moves the micrOBU's ITS clock. Honest: the caller passes the
// measured drift and local time and applies the returned step.
#include <cstdint>
namespace microbu::its_clock {
/// @brief What a PoTi update does to the station's ITS clock.
enum class Step {
start, ///< first reference: synchronise the clock (and restart the station at real time)
forward, ///< the reference is ahead: step the clock forward to it
hold, ///< less than 1 s behind: keep the local clock, later updates catch up
refuse_stale, ///< more than 1 s behind: a late-delivered update, refuse it and keep the clock
restart, ///< consistently behind over >= 5 updates and >= 2 s: the reference really moved back
slew_back, ///< even the freshest reference of >= 5 updates over >= 2 s is behind: the clock runs ahead,
///< slow it down by Lag::slew_us (monotonic, EN 302 890-2 clause 6.3.4 |error| < 20 ms)
};
/// @brief Backward offsets below 1 s seen while holding (state owned by the caller). The freshest one (the largest
/// drift) is the reference least delayed by the link: if even it is behind, the clock itself is ahead.
struct Lag {
int count = 0;
std::int64_t freshest_us = 0;
std::int64_t first_local_us = 0;
std::int64_t slew_us = 0; ///< set with Step::slew_back: how far to slow the clock down
};
/// @brief Clock ahead by less than this is left alone (well inside pPoTiMaxTimeDiff = 20 ms).
constexpr std::int64_t slew_deadband_us = 5000;
/// @brief Slew rate: the clock runs 1/slew_divisor slower until a pending correction is applied (10 %).
constexpr std::int64_t slew_divisor = 10;
/// @brief Part of a pending slew applied after `elapsed_us` of local time.
constexpr std::int64_t slew_applied_us(std::int64_t pending_us, std::int64_t elapsed_us) {
const std::int64_t possible = elapsed_us / slew_divisor;
return possible < pending_us ? possible : pending_us;
}
/// @brief Backward offsets seen so far (state owned by the caller).
struct Regression {
int count = 0;
std::int64_t offset_us = 0;
std::int64_t first_local_us = 0;
};
/// @brief Decide the clock step for one PoTi update. Over BLE, updates delivered late (10-15 s observed)
/// arrive in bursts with a shrinking offset and never look like a consistent backward move.
/// @param synchronised Whether the clock already follows a reference.
/// @param drift_us Reference time minus the station's ITS time.
/// @param local_us Local monotonic time of this update.
/// @param regression Backward-offset tracker, updated in place.
/// @param lag Freshest-backward-offset tracker for Step::slew_back, updated in place.
Step next_step(bool synchronised, std::int64_t drift_us, std::int64_t local_us, Regression& regression, Lag& lag);
} // namespace microbu::its_clock