Files
MicrOBU/obu-firmware/test/host/test_battery.cpp
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

114 lines
3.8 KiB
C++

// Host tests for the battery policy (battery_policy.hpp). No ESP-IDF involved: it compiles unmodified on the PC.
#include "battery_policy.hpp"
#include <cstdio>
#include <cstdlib>
using namespace microbu;
static int failures = 0;
#define CHECK(cond) do { if (!(cond)) { std::printf("FAIL %s:%d %s\n", __FILE__, __LINE__, #cond); ++failures; } } while (0)
// Feeds the same sample n times, 5 s apart, starting at t_ms.
static void feed(BatteryPolicy& m, std::uint16_t mv, int n, bool usb, std::uint32_t& t_ms) {
for (int i = 0; i < n; ++i) { m.update(mv, usb, t_ms); t_ms += 5000; }
}
static void test_smoothing_and_validity() {
BatteryPolicy m;
CHECK(m.millivolts() == 0); // nothing measured yet
m.update(4100, false, 0);
CHECK(m.millivolts() == 4100); // first sample taken as is, no ramp from zero
m.update(4000, false, 5000);
CHECK(m.millivolts() == 4075); // 1/4 of the step
m.update(0, false, 10000); // failed measurement
CHECK(m.millivolts() == 0 && !m.low() && !m.critical() && !m.shutdown_due());
m.update(4100, false, 15000);
CHECK(m.millivolts() == 4100); // restarts from the new reading
m.update(5200, false, 20000); // implausible: no cell connected
CHECK(m.millivolts() == 0);
}
static void test_warning_hysteresis() {
BatteryPolicy m;
std::uint32_t t = 0;
feed(m, 3700, 3, false, t);
CHECK(!m.low());
feed(m, 3450, 20, false, t);
CHECK(m.low());
feed(m, 3550, 20, false, t); // above the warning threshold but below the clear threshold
CHECK(m.low());
feed(m, 3650, 20, false, t);
CHECK(!m.low());
}
static void test_shutdown_rules() {
{ // sustained low, no USB, after the grace period
BatteryPolicy m;
std::uint32_t t = 120000;
feed(m, 3250, 5, false, t);
CHECK(m.critical() && !m.shutdown_due()); // five samples are not enough
feed(m, 3250, 1, false, t);
CHECK(m.shutdown_due());
}
{ // a phone on USB powers the board: never shut down, and the count restarts
BatteryPolicy m;
std::uint32_t t = 120000;
feed(m, 3250, 5, false, t);
feed(m, 3250, 1, true, t);
CHECK(!m.shutdown_due());
feed(m, 3250, 5, false, t);
CHECK(!m.shutdown_due());
feed(m, 3250, 1, false, t);
CHECK(m.shutdown_due());
}
{ // not within the first minute after boot
BatteryPolicy m;
std::uint32_t t = 0;
feed(m, 3250, 11, false, t); // 0..50 s
CHECK(!m.shutdown_due());
feed(m, 3250, 7, false, t); // 55..85 s: six of them at or past the 60 s grace period
CHECK(m.shutdown_due());
}
{ // a dip that recovers (TX sag) does not count
BatteryPolicy m;
std::uint32_t t = 120000;
feed(m, 3250, 4, false, t);
feed(m, 3900, 10, false, t);
feed(m, 3250, 4, false, t);
CHECK(!m.shutdown_due());
}
{ // a normal cell never trips it
BatteryPolicy m;
std::uint32_t t = 120000;
feed(m, 3800, 100, false, t);
CHECK(!m.shutdown_due() && !m.low() && !m.critical());
}
{ // no cell / no sensor: no shutdown
BatteryPolicy m;
std::uint32_t t = 120000;
feed(m, 0, 100, false, t);
CHECK(!m.shutdown_due());
}
}
static void test_resume() {
BatteryPolicy m;
CHECK(!m.resume_ok(3300));
CHECK(!m.resume_ok(3599));
CHECK(m.resume_ok(3600));
CHECK(m.resume_ok(4100));
CHECK(m.resume_ok(0)); // cannot judge: run
}
int main() {
test_smoothing_and_validity();
test_warning_hysteresis();
test_shutdown_rules();
test_resume();
if (failures) { std::printf("%d check(s) failed\n", failures); return EXIT_FAILURE; }
std::printf("battery: all checks passed\n");
return EXIT_SUCCESS;
}