Add M0 services: settings, battery, clock, storage, power

lib/services holds the host-tested logic:
- Settings: typed, validated, persisted, SettingChanged events,
  transmit gated on a confirmed Region
- BatteryEstimator: LiPo curve, median smoothing against TX sags
- ClockModel: source priority GNSS > NTP > mesh, relative ages,
  Europe/Brussels local time via POSIX TZ
- StorageMonitor: warning once per boot at 80%, Logs stop at 90%,
  Captures stop with < 2 MiB left
- PowerPolicy / PowerButton: dim/off timeouts, wake key swallowed only
  when the screen was off, G0 long press

src/services wires them to the hardware (NVS, battery ADC, SD on the
shared SPI bus with the LoRa CS held high, backlight, deep sleep), and
main.cpp is a temporary diagnostics screen for the hardware checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
This commit is contained in:
2026-10-02 09:47:36 +02:00
co-authored by Claude Opus 5.5
parent ae0726b7f9
commit 987406fa7c
28 changed files with 1434 additions and 39 deletions
+64
View File
@@ -0,0 +1,64 @@
#include <unity.h>
#include "battery_estimator.h"
using namespace roro;
void setUp() {}
void tearDown() {}
void test_curve_end_points_and_clamping() {
TEST_ASSERT_EQUAL(100, BatteryEstimator::percentFromMillivolts(4200));
TEST_ASSERT_EQUAL(100, BatteryEstimator::percentFromMillivolts(4350));
TEST_ASSERT_EQUAL(0, BatteryEstimator::percentFromMillivolts(3270));
TEST_ASSERT_EQUAL(0, BatteryEstimator::percentFromMillivolts(2900));
}
void test_curve_is_monotonic() {
int previous = 0;
for (int mv = 3200; mv <= 4250; mv += 5) {
int p = BatteryEstimator::percentFromMillivolts(mv);
TEST_ASSERT_TRUE(p >= previous);
previous = p;
}
}
void test_curve_interpolates_between_points() {
// 3840 mV = 50 %, 3850 mV = 55 %
TEST_ASSERT_EQUAL(50, BatteryEstimator::percentFromMillivolts(3840));
TEST_ASSERT_INT_WITHIN(1, 52, BatteryEstimator::percentFromMillivolts(3845));
}
void test_first_sample_is_reported_immediately() {
BatteryEstimator b;
TEST_ASSERT_FALSE(b.hasReading());
b.addSample(4000);
TEST_ASSERT_TRUE(b.hasReading());
TEST_ASSERT_EQUAL(4000, b.millivolts());
}
void test_single_outlier_barely_moves_the_estimate() {
BatteryEstimator b;
for (int i = 0; i < 8; i++) b.addSample(3900);
int before = b.percent();
b.addSample(3500); // e.g. a transmit burst sagging the voltage
TEST_ASSERT_INT_WITHIN(6, before, b.percent());
}
void test_estimate_follows_a_sustained_change() {
BatteryEstimator b;
for (int i = 0; i < 8; i++) b.addSample(4100);
for (int i = 0; i < 8; i++) b.addSample(3800);
TEST_ASSERT_EQUAL(3800, b.millivolts());
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_curve_end_points_and_clamping);
RUN_TEST(test_curve_is_monotonic);
RUN_TEST(test_curve_interpolates_between_points);
RUN_TEST(test_first_sample_is_reported_immediately);
RUN_TEST(test_single_outlier_barely_moves_the_estimate);
RUN_TEST(test_estimate_follows_a_sustained_change);
return UNITY_END();
}
+74
View File
@@ -0,0 +1,74 @@
#include <unity.h>
#include "clock_model.h"
using namespace roro;
void setUp() {}
void tearDown() {}
static const char* kBrussels = "CET-1CEST,M3.5.0,M10.5.0/3";
static const int64_t k2026_07_01_1200Z = 1782907200;
static const int64_t k2026_01_15_1200Z = 1768478400;
void test_clock_is_unset_until_a_source_sets_it() {
ClockModel c;
TEST_ASSERT_FALSE(c.isSet());
TEST_ASSERT_EQUAL(static_cast<int>(TimeSource::None), static_cast<int>(c.source()));
}
void test_time_advances_with_uptime_after_being_set() {
ClockModel c;
TEST_ASSERT_TRUE(c.set(k2026_07_01_1200Z, TimeSource::Ntp, 5000));
TEST_ASSERT_TRUE(c.isSet());
TEST_ASSERT_EQUAL_INT64(k2026_07_01_1200Z + 10, c.utcNow(15000));
}
void test_lower_priority_source_does_not_override() {
ClockModel c;
c.set(k2026_07_01_1200Z, TimeSource::Ntp, 0);
TEST_ASSERT_FALSE(c.set(k2026_07_01_1200Z + 500, TimeSource::Mesh, 0));
TEST_ASSERT_EQUAL_INT64(k2026_07_01_1200Z, c.utcNow(0));
}
void test_equal_or_higher_priority_source_overrides() {
ClockModel c;
c.set(k2026_07_01_1200Z, TimeSource::Mesh, 0);
TEST_ASSERT_TRUE(c.set(k2026_07_01_1200Z + 1, TimeSource::Mesh, 0));
TEST_ASSERT_TRUE(c.set(k2026_07_01_1200Z + 2, TimeSource::Gnss, 0));
TEST_ASSERT_EQUAL(static_cast<int>(TimeSource::Gnss), static_cast<int>(c.source()));
}
void test_utc_now_survives_millis_wraparound() {
ClockModel c;
c.set(k2026_07_01_1200Z, TimeSource::Ntp, 0xFFFFFFFFu - 999);
TEST_ASSERT_EQUAL_INT64(k2026_07_01_1200Z + 2, c.utcNow(1000));
}
void test_relative_age_formatting() {
TEST_ASSERT_EQUAL_STRING("now", ClockModel::formatAge(0).c_str());
TEST_ASSERT_EQUAL_STRING("now", ClockModel::formatAge(59).c_str());
TEST_ASSERT_EQUAL_STRING("1 min ago", ClockModel::formatAge(60).c_str());
TEST_ASSERT_EQUAL_STRING("59 min ago", ClockModel::formatAge(3599).c_str());
TEST_ASSERT_EQUAL_STRING("1 h ago", ClockModel::formatAge(3600).c_str());
TEST_ASSERT_EQUAL_STRING("23 h ago", ClockModel::formatAge(86399).c_str());
TEST_ASSERT_EQUAL_STRING("2 d ago", ClockModel::formatAge(2 * 86400).c_str());
}
void test_local_time_in_brussels_summer_and_winter() {
ClockModel::applyTimezone(kBrussels);
TEST_ASSERT_EQUAL_STRING("14:00", ClockModel::formatLocalTime(k2026_07_01_1200Z).c_str());
TEST_ASSERT_EQUAL_STRING("13:00", ClockModel::formatLocalTime(k2026_01_15_1200Z).c_str());
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_clock_is_unset_until_a_source_sets_it);
RUN_TEST(test_time_advances_with_uptime_after_being_set);
RUN_TEST(test_lower_priority_source_does_not_override);
RUN_TEST(test_equal_or_higher_priority_source_overrides);
RUN_TEST(test_utc_now_survives_millis_wraparound);
RUN_TEST(test_relative_age_formatting);
RUN_TEST(test_local_time_in_brussels_summer_and_winter);
return UNITY_END();
}
+65
View File
@@ -0,0 +1,65 @@
#include <unity.h>
#include "power_policy.h"
using namespace roro;
void setUp() {}
void tearDown() {}
void test_screen_dims_then_turns_off_after_inactivity() {
PowerPolicy p(30000, 60000);
p.activity(0);
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::On), static_cast<int>(p.update(29999)));
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::Dimmed), static_cast<int>(p.update(30000)));
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::Off), static_cast<int>(p.update(60000)));
}
void test_key_on_dimmed_screen_wakes_and_still_acts() {
PowerPolicy p(30000, 60000);
p.activity(0);
p.update(40000);
TEST_ASSERT_FALSE(p.activity(40000)); // not swallowed
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::On), static_cast<int>(p.update(40001)));
}
void test_key_on_off_screen_only_wakes() {
PowerPolicy p(30000, 60000);
p.activity(0);
p.update(70000);
TEST_ASSERT_TRUE(p.activity(70000)); // swallowed
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::On), static_cast<int>(p.update(70001)));
}
void test_new_timeouts_apply_immediately() {
PowerPolicy p(30000, 60000);
p.activity(0);
p.setTimeouts(10000, 20000);
TEST_ASSERT_EQUAL(static_cast<int>(ScreenState::Off), static_cast<int>(p.update(20000)));
}
void test_short_press_reported_on_release() {
PowerButton b(2000);
TEST_ASSERT_EQUAL(static_cast<int>(ButtonAction::None), static_cast<int>(b.update(true, 0)));
TEST_ASSERT_EQUAL(static_cast<int>(ButtonAction::None), static_cast<int>(b.update(true, 500)));
TEST_ASSERT_EQUAL(static_cast<int>(ButtonAction::ShortPress), static_cast<int>(b.update(false, 600)));
}
void test_long_press_fires_once_while_still_held() {
PowerButton b(2000);
b.update(true, 0);
TEST_ASSERT_EQUAL(static_cast<int>(ButtonAction::LongPress), static_cast<int>(b.update(true, 2000)));
TEST_ASSERT_EQUAL(static_cast<int>(ButtonAction::None), static_cast<int>(b.update(true, 3000)));
TEST_ASSERT_EQUAL(static_cast<int>(ButtonAction::None), static_cast<int>(b.update(false, 3100)));
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_screen_dims_then_turns_off_after_inactivity);
RUN_TEST(test_key_on_dimmed_screen_wakes_and_still_acts);
RUN_TEST(test_key_on_off_screen_only_wakes);
RUN_TEST(test_new_timeouts_apply_immediately);
RUN_TEST(test_short_press_reported_on_release);
RUN_TEST(test_long_press_fires_once_while_still_held);
return UNITY_END();
}
+184
View File
@@ -0,0 +1,184 @@
#include <unity.h>
#include <cstring>
#include <map>
#include <string>
#include "event_bus.h"
#include "settings.h"
using namespace roro;
struct MemoryStore : KeyValueStore {
std::map<std::string, int32_t> ints;
std::map<std::string, std::string> strings;
bool getInt(const char* key, int32_t& out) override {
auto it = ints.find(key);
if (it == ints.end()) return false;
out = it->second;
return true;
}
bool getString(const char* key, std::string& out) override {
auto it = strings.find(key);
if (it == strings.end()) return false;
out = it->second;
return true;
}
void putInt(const char* key, int32_t v) override { ints[key] = v; }
void putString(const char* key, const std::string& v) override { strings[key] = v; }
};
void setUp() {}
void tearDown() {}
void test_defaults_when_store_is_empty() {
MemoryStore store;
EventBus bus;
Settings s(store, bus);
s.load();
TEST_ASSERT_FALSE(s.getBool(Setting::SetupDone));
TEST_ASSERT_EQUAL(60, s.getInt(Setting::Brightness));
TEST_ASSERT_EQUAL(30, s.getInt(Setting::DimTimeoutS));
TEST_ASSERT_EQUAL(60, s.getInt(Setting::OffTimeoutS));
TEST_ASSERT_TRUE(s.getBool(Setting::Sound));
TEST_ASSERT_TRUE(s.getBool(Setting::ProbeMacRaw));
TEST_ASSERT_EQUAL_STRING("EU868", s.getString(Setting::Region).c_str());
TEST_ASSERT_EQUAL_STRING("CET-1CEST,M3.5.0,M10.5.0/3", s.getString(Setting::Timezone).c_str());
}
void test_set_persists_and_survives_reload() {
MemoryStore store;
EventBus bus;
{
Settings s(store, bus);
s.load();
TEST_ASSERT_TRUE(s.setInt(Setting::Brightness, 80));
TEST_ASSERT_TRUE(s.setString(Setting::LongName, "Clément"));
}
Settings again(store, bus);
again.load();
TEST_ASSERT_EQUAL(80, again.getInt(Setting::Brightness));
TEST_ASSERT_EQUAL_STRING("Clément", again.getString(Setting::LongName).c_str());
}
void test_out_of_range_values_are_rejected_and_not_stored() {
MemoryStore store;
EventBus bus;
Settings s(store, bus);
s.load();
TEST_ASSERT_FALSE(s.setInt(Setting::Brightness, 5)); // would make the screen unreadable
TEST_ASSERT_FALSE(s.setInt(Setting::Brightness, 101));
TEST_ASSERT_EQUAL(60, s.getInt(Setting::Brightness));
TEST_ASSERT_EQUAL(0, store.ints.count(Settings::key(Setting::Brightness)));
}
void test_corrupt_stored_value_falls_back_to_default() {
MemoryStore store;
EventBus bus;
store.ints[Settings::key(Setting::Brightness)] = 9999;
Settings s(store, bus);
s.load();
TEST_ASSERT_EQUAL(60, s.getInt(Setting::Brightness));
}
void test_dim_timeout_must_stay_below_off_timeout() {
MemoryStore store;
EventBus bus;
Settings s(store, bus);
s.load();
TEST_ASSERT_FALSE(s.setInt(Setting::DimTimeoutS, 60)); // off is 60
TEST_ASSERT_FALSE(s.setInt(Setting::OffTimeoutS, 30)); // dim is 30
TEST_ASSERT_TRUE(s.setInt(Setting::OffTimeoutS, 120));
TEST_ASSERT_TRUE(s.setInt(Setting::DimTimeoutS, 90));
}
void test_short_name_is_one_to_four_bytes() {
MemoryStore store;
EventBus bus;
Settings s(store, bus);
s.load();
TEST_ASSERT_FALSE(s.setString(Setting::ShortName, ""));
TEST_ASSERT_FALSE(s.setString(Setting::ShortName, "ROROX"));
TEST_ASSERT_TRUE(s.setString(Setting::ShortName, "RORO"));
}
void test_long_name_is_limited_to_39_bytes() {
MemoryStore store;
EventBus bus;
Settings s(store, bus);
s.load();
TEST_ASSERT_TRUE(s.setString(Setting::LongName, std::string(39, 'a')));
TEST_ASSERT_FALSE(s.setString(Setting::LongName, std::string(40, 'a')));
TEST_ASSERT_FALSE(s.setString(Setting::LongName, ""));
}
void test_only_known_regions_are_accepted() {
MemoryStore store;
EventBus bus;
Settings s(store, bus);
s.load();
TEST_ASSERT_FALSE(s.setString(Setting::Region, "US915"));
TEST_ASSERT_TRUE(s.setString(Setting::Region, "EU868"));
}
void test_type_mismatch_is_rejected() {
MemoryStore store;
EventBus bus;
Settings s(store, bus);
s.load();
TEST_ASSERT_FALSE(s.setString(Setting::Brightness, "80"));
TEST_ASSERT_FALSE(s.setInt(Setting::LongName, 3));
}
void test_change_publishes_setting_changed_once() {
MemoryStore store;
EventBus bus;
Settings s(store, bus);
s.load();
int changes = 0;
int32_t which = -1;
bus.subscribe(EventType::SettingChanged, [&](const Event& e) {
changes++;
which = e.a;
});
s.setInt(Setting::Brightness, 70);
s.setInt(Setting::Brightness, 70); // same value: no event
s.setInt(Setting::Brightness, 1); // invalid: no event
bus.dispatch();
TEST_ASSERT_EQUAL(1, changes);
TEST_ASSERT_EQUAL(static_cast<int32_t>(Setting::Brightness), which);
}
void test_transmit_requires_confirmed_region() {
MemoryStore store;
EventBus bus;
Settings s(store, bus);
s.load();
TEST_ASSERT_FALSE(s.canTransmit());
s.setBool(Setting::RegionConfirmed, true);
TEST_ASSERT_TRUE(s.canTransmit());
}
void test_storage_keys_fit_nvs_limit() {
for (int i = 0; i < static_cast<int>(Setting::Count); i++)
TEST_ASSERT_TRUE(std::strlen(Settings::key(static_cast<Setting>(i))) <= 15);
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_defaults_when_store_is_empty);
RUN_TEST(test_set_persists_and_survives_reload);
RUN_TEST(test_out_of_range_values_are_rejected_and_not_stored);
RUN_TEST(test_corrupt_stored_value_falls_back_to_default);
RUN_TEST(test_dim_timeout_must_stay_below_off_timeout);
RUN_TEST(test_short_name_is_one_to_four_bytes);
RUN_TEST(test_long_name_is_limited_to_39_bytes);
RUN_TEST(test_only_known_regions_are_accepted);
RUN_TEST(test_type_mismatch_is_rejected);
RUN_TEST(test_change_publishes_setting_changed_once);
RUN_TEST(test_transmit_requires_confirmed_region);
RUN_TEST(test_storage_keys_fit_nvs_limit);
return UNITY_END();
}
+108
View File
@@ -0,0 +1,108 @@
#include <unity.h>
#include <vector>
#include "event_bus.h"
#include "storage_monitor.h"
using namespace roro;
void setUp() {}
void tearDown() {}
static const uint64_t GB = 1024ull * 1024 * 1024;
static const uint64_t MB = 1024ull * 1024;
struct Fixture {
EventBus bus{16};
StorageMonitor monitor{bus};
std::vector<Event> notifications;
std::vector<Event> thresholds;
Fixture() {
bus.subscribe(EventType::Notification, [&](const Event& e) { notifications.push_back(e); });
bus.subscribe(EventType::StorageThreshold, [&](const Event& e) { thresholds.push_back(e); });
}
void update(uint64_t used, uint64_t total = 32 * GB) {
monitor.update(true, total, used);
bus.dispatch();
}
};
void test_no_card_means_nothing_can_be_written() {
Fixture f;
f.monitor.update(false, 0, 0);
TEST_ASSERT_FALSE(f.monitor.state().present);
TEST_ASSERT_FALSE(f.monitor.state().logsAllowed);
TEST_ASSERT_FALSE(f.monitor.state().capturesAllowed);
}
void test_below_80_percent_everything_is_allowed_quietly() {
Fixture f;
f.update(16 * GB);
TEST_ASSERT_EQUAL(50, f.monitor.state().usedPercent);
TEST_ASSERT_TRUE(f.monitor.state().logsAllowed);
TEST_ASSERT_TRUE(f.monitor.state().capturesAllowed);
TEST_ASSERT_EQUAL(0, f.notifications.size());
}
void test_crossing_80_percent_warns_once_per_boot() {
Fixture f;
f.update(26 * GB); // 81 %
TEST_ASSERT_EQUAL(1, f.notifications.size());
f.update(26 * GB);
f.update(10 * GB); // cleaned up
f.update(27 * GB); // over again, same boot
TEST_ASSERT_EQUAL(1, f.notifications.size());
}
void test_warning_is_a_warning_level_notification() {
Fixture f;
f.update(26 * GB);
TEST_ASSERT_EQUAL(static_cast<int32_t>(NotificationLevel::Warning), f.notifications[0].a);
}
void test_at_90_percent_logs_stop_but_captures_continue() {
Fixture f;
f.update(29 * GB); // 90.6 %
TEST_ASSERT_FALSE(f.monitor.state().logsAllowed);
TEST_ASSERT_TRUE(f.monitor.state().capturesAllowed);
}
void test_logs_resume_after_clean_up_below_90() {
Fixture f;
f.update(29 * GB);
f.update(20 * GB);
TEST_ASSERT_TRUE(f.monitor.state().logsAllowed);
}
void test_captures_stop_when_card_is_full() {
Fixture f;
f.update(32 * GB - MB); // less than the 2 MiB reserve left
TEST_ASSERT_FALSE(f.monitor.state().capturesAllowed);
}
void test_threshold_event_only_when_level_changes() {
Fixture f;
f.update(10 * GB); // level 0, initial
f.update(26 * GB); // 80
f.update(26 * GB); // still 80
f.update(29 * GB); // 90
f.update(32 * GB); // full
TEST_ASSERT_EQUAL(3, f.thresholds.size());
TEST_ASSERT_EQUAL(80, f.thresholds[0].b);
TEST_ASSERT_EQUAL(90, f.thresholds[1].b);
TEST_ASSERT_EQUAL(100, f.thresholds[2].b);
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_no_card_means_nothing_can_be_written);
RUN_TEST(test_below_80_percent_everything_is_allowed_quietly);
RUN_TEST(test_crossing_80_percent_warns_once_per_boot);
RUN_TEST(test_warning_is_a_warning_level_notification);
RUN_TEST(test_at_90_percent_logs_stop_but_captures_continue);
RUN_TEST(test_logs_resume_after_clean_up_below_90);
RUN_TEST(test_captures_stop_when_card_is_full);
RUN_TEST(test_threshold_event_only_when_level_changes);
return UNITY_END();
}