Add core runtime: event bus, services, app lifecycle

lib/core is hardware-independent and unit-tested on the host:
- EventBus: fixed-size thread-safe queue, delivered on the UI task by
  dispatch(); drops newest when full and counts drops
- Service / ServiceManager: cooperative ticking at per-service intervals,
  no catch-up bursts, safe across millis() wraparound
- App / AppManager: one foreground App, Home always to Launcher, Back
  offered to the App first, hidden Apps, redraw requests
- KeyEvent: logical keys for the upcoming input layer

Firmware main loop now ticks services and dispatches events.

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:10:10 +02:00
co-authored by Claude Opus 5.5
parent b293197ace
commit ae0726b7f9
14 changed files with 810 additions and 0 deletions
+163
View File
@@ -0,0 +1,163 @@
#include <unity.h>
#include <string>
#include <vector>
#include "app_manager.h"
using namespace roro;
void setUp() {}
void tearDown() {}
struct FakeApp : App {
explicit FakeApp(std::vector<std::string>* log, const char* tag) : log_(log), tag_(tag) {}
void onEnter() override { log_->push_back(std::string("enter ") + tag_); }
void onExit() override { log_->push_back(std::string("exit ") + tag_); }
bool onKey(const KeyEvent& e) override {
keys.push_back(e);
return handles;
}
void draw(Canvas&) override {}
std::vector<KeyEvent> keys;
bool handles = false;
private:
std::vector<std::string>* log_;
const char* tag_;
};
struct Fixture {
std::vector<std::string> log;
FakeApp launcher{&log, "launcher"};
FakeApp notes{&log, "notes"};
FakeApp demo{&log, "demo"};
AppManager manager{launcher};
Fixture() {
manager.registerApp({"notes", "Notes", false, &notes});
manager.registerApp({"demo", "Demo", true, &demo});
manager.begin();
log.clear();
}
};
void test_launcher_is_in_foreground_after_begin() {
std::vector<std::string> log;
FakeApp launcher(&log, "launcher");
AppManager m(launcher);
m.begin();
TEST_ASSERT_EQUAL_PTR(&launcher, &m.foreground());
TEST_ASSERT_EQUAL_STRING("enter launcher", log[0].c_str());
}
void test_open_switches_foreground_with_exit_then_enter() {
Fixture f;
TEST_ASSERT_TRUE(f.manager.open("notes"));
TEST_ASSERT_EQUAL_PTR(&f.notes, &f.manager.foreground());
std::vector<std::string> expected = {"exit launcher", "enter notes"};
TEST_ASSERT_TRUE(f.log == expected);
}
void test_open_unknown_app_keeps_current_foreground() {
Fixture f;
TEST_ASSERT_FALSE(f.manager.open("nope"));
TEST_ASSERT_EQUAL_PTR(&f.launcher, &f.manager.foreground());
TEST_ASSERT_TRUE(f.log.empty());
}
void test_opening_the_foreground_app_again_does_nothing() {
Fixture f;
f.manager.open("notes");
f.log.clear();
TEST_ASSERT_TRUE(f.manager.open("notes"));
TEST_ASSERT_TRUE(f.log.empty());
}
void test_home_key_always_returns_to_launcher_without_reaching_the_app() {
Fixture f;
f.manager.open("notes");
f.notes.handles = true;
f.manager.handleKey(KeyEvent::of(Key::Home));
TEST_ASSERT_EQUAL_PTR(&f.launcher, &f.manager.foreground());
TEST_ASSERT_EQUAL(0, f.notes.keys.size());
}
void test_back_goes_to_the_app_first_and_it_can_keep_it() {
Fixture f;
f.manager.open("notes");
f.notes.handles = true; // e.g. leaving a sub-screen inside the app
f.manager.handleKey(KeyEvent::of(Key::Back));
TEST_ASSERT_EQUAL_PTR(&f.notes, &f.manager.foreground());
TEST_ASSERT_EQUAL(1, f.notes.keys.size());
}
void test_unhandled_back_returns_to_launcher() {
Fixture f;
f.manager.open("notes");
f.manager.handleKey(KeyEvent::of(Key::Back));
TEST_ASSERT_EQUAL_PTR(&f.launcher, &f.manager.foreground());
}
void test_back_on_launcher_stays_on_launcher() {
Fixture f;
f.manager.handleKey(KeyEvent::of(Key::Back));
TEST_ASSERT_EQUAL_PTR(&f.launcher, &f.manager.foreground());
TEST_ASSERT_TRUE(f.log.empty());
}
void test_other_keys_go_to_the_foreground_app() {
Fixture f;
f.manager.open("notes");
f.manager.handleKey(KeyEvent::character('a'));
TEST_ASSERT_EQUAL(1, f.notes.keys.size());
TEST_ASSERT_EQUAL('a', f.notes.keys[0].ch);
}
void test_visible_apps_exclude_hidden_ones_and_keep_registration_order() {
Fixture f;
auto visible = f.manager.visibleApps();
TEST_ASSERT_EQUAL(1, visible.size());
TEST_ASSERT_EQUAL_STRING("notes", visible[0]->id);
TEST_ASSERT_TRUE(f.manager.open("demo")); // hidden apps can still be opened
}
void test_switching_apps_requests_a_redraw() {
Fixture f;
f.manager.takeRedraw();
f.manager.open("notes");
TEST_ASSERT_TRUE(f.manager.takeRedraw());
TEST_ASSERT_FALSE(f.manager.takeRedraw());
}
void test_app_can_request_its_own_redraw() {
Fixture f;
f.manager.open("notes");
f.manager.takeRedraw();
f.notes.requestRedraw();
TEST_ASSERT_TRUE(f.manager.takeRedraw());
TEST_ASSERT_FALSE(f.manager.takeRedraw());
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_launcher_is_in_foreground_after_begin);
RUN_TEST(test_open_switches_foreground_with_exit_then_enter);
RUN_TEST(test_open_unknown_app_keeps_current_foreground);
RUN_TEST(test_opening_the_foreground_app_again_does_nothing);
RUN_TEST(test_home_key_always_returns_to_launcher_without_reaching_the_app);
RUN_TEST(test_back_goes_to_the_app_first_and_it_can_keep_it);
RUN_TEST(test_unhandled_back_returns_to_launcher);
RUN_TEST(test_back_on_launcher_stays_on_launcher);
RUN_TEST(test_other_keys_go_to_the_foreground_app);
RUN_TEST(test_visible_apps_exclude_hidden_ones_and_keep_registration_order);
RUN_TEST(test_switching_apps_requests_a_redraw);
RUN_TEST(test_app_can_request_its_own_redraw);
return UNITY_END();
}
+118
View File
@@ -0,0 +1,118 @@
#include <unity.h>
#include <string>
#include <vector>
#include "event_bus.h"
using namespace roro;
void setUp() {}
void tearDown() {}
void test_published_events_are_delivered_on_dispatch_not_before() {
EventBus bus(8);
int calls = 0;
bus.subscribe(EventType::BatteryChanged, [&](const Event&) { calls++; });
bus.publish(Event{EventType::BatteryChanged});
TEST_ASSERT_EQUAL(0, calls);
TEST_ASSERT_EQUAL(1, bus.dispatch());
TEST_ASSERT_EQUAL(1, calls);
}
void test_only_subscribers_of_the_event_type_are_called() {
EventBus bus(8);
int battery = 0, notification = 0;
bus.subscribe(EventType::BatteryChanged, [&](const Event&) { battery++; });
bus.subscribe(EventType::Notification, [&](const Event&) { notification++; });
bus.publish(Event{EventType::Notification});
bus.dispatch();
TEST_ASSERT_EQUAL(0, battery);
TEST_ASSERT_EQUAL(1, notification);
}
void test_every_subscriber_of_a_type_is_called_in_subscription_order() {
EventBus bus(8);
std::vector<int> order;
bus.subscribe(EventType::Notification, [&](const Event&) { order.push_back(1); });
bus.subscribe(EventType::Notification, [&](const Event&) { order.push_back(2); });
bus.publish(Event{EventType::Notification});
bus.dispatch();
TEST_ASSERT_EQUAL(2, order.size());
TEST_ASSERT_EQUAL(1, order[0]);
TEST_ASSERT_EQUAL(2, order[1]);
}
void test_events_keep_publish_order_and_payload() {
EventBus bus(8);
std::vector<int32_t> values;
bus.subscribe(EventType::BatteryChanged, [&](const Event& e) { values.push_back(e.a); });
bus.publish(Event::withValues(EventType::BatteryChanged, 80));
bus.publish(Event::withValues(EventType::BatteryChanged, 79));
bus.dispatch();
TEST_ASSERT_EQUAL(2, values.size());
TEST_ASSERT_EQUAL(80, values[0]);
TEST_ASSERT_EQUAL(79, values[1]);
}
void test_text_payload_is_copied_and_truncated_safely() {
EventBus bus(8);
std::string received;
bus.subscribe(EventType::Notification, [&](const Event& e) { received = e.text; });
std::string longText(200, 'x');
bus.publish(Event::withText(EventType::Notification, longText.c_str()));
bus.dispatch();
TEST_ASSERT_EQUAL(Event::kTextCapacity - 1, received.size());
}
void test_full_queue_drops_newest_and_counts_drops() {
EventBus bus(2);
std::vector<int32_t> values;
bus.subscribe(EventType::BatteryChanged, [&](const Event& e) { values.push_back(e.a); });
TEST_ASSERT_TRUE(bus.publish(Event::withValues(EventType::BatteryChanged, 1)));
TEST_ASSERT_TRUE(bus.publish(Event::withValues(EventType::BatteryChanged, 2)));
TEST_ASSERT_FALSE(bus.publish(Event::withValues(EventType::BatteryChanged, 3)));
TEST_ASSERT_EQUAL(1, bus.dropped());
bus.dispatch();
TEST_ASSERT_EQUAL(2, values.size());
TEST_ASSERT_EQUAL(2, values[1]);
}
void test_events_published_during_dispatch_wait_for_next_dispatch() {
EventBus bus(8);
int notifications = 0;
bus.subscribe(EventType::BatteryChanged,
[&](const Event&) { bus.publish(Event{EventType::Notification}); });
bus.subscribe(EventType::Notification, [&](const Event&) { notifications++; });
bus.publish(Event{EventType::BatteryChanged});
TEST_ASSERT_EQUAL(1, bus.dispatch());
TEST_ASSERT_EQUAL(0, notifications);
TEST_ASSERT_EQUAL(1, bus.dispatch());
TEST_ASSERT_EQUAL(1, notifications);
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_published_events_are_delivered_on_dispatch_not_before);
RUN_TEST(test_only_subscribers_of_the_event_type_are_called);
RUN_TEST(test_every_subscriber_of_a_type_is_called_in_subscription_order);
RUN_TEST(test_events_keep_publish_order_and_payload);
RUN_TEST(test_text_payload_is_copied_and_truncated_safely);
RUN_TEST(test_full_queue_drops_newest_and_counts_drops);
RUN_TEST(test_events_published_during_dispatch_wait_for_next_dispatch);
return UNITY_END();
}
@@ -0,0 +1,127 @@
#include <unity.h>
#include <string>
#include <vector>
#include "service_manager.h"
using namespace roro;
void setUp() {}
void tearDown() {}
struct FakeService : Service {
FakeService(const char* n, uint32_t interval, std::vector<std::string>* log)
: n_(n), interval_(interval), log_(log) {}
const char* name() const override { return n_; }
uint32_t tickIntervalMs() const override { return interval_; }
void start() override { log_->push_back(std::string("start ") + n_); }
void stop() override { log_->push_back(std::string("stop ") + n_); }
void tick(uint32_t nowMs) override {
ticks.push_back(nowMs);
log_->push_back(std::string("tick ") + n_);
}
std::vector<uint32_t> ticks;
private:
const char* n_;
uint32_t interval_;
std::vector<std::string>* log_;
};
void test_services_start_in_order_and_stop_in_reverse() {
std::vector<std::string> log;
FakeService a("a", 100, &log), b("b", 100, &log);
ServiceManager m;
m.add(a);
m.add(b);
m.startAll(0);
m.stopAll();
std::vector<std::string> expected = {"start a", "start b", "stop b", "stop a"};
TEST_ASSERT_TRUE(log == expected);
}
void test_service_ticks_immediately_after_start_then_at_its_interval() {
std::vector<std::string> log;
FakeService s("s", 100, &log);
ServiceManager m;
m.add(s);
m.startAll(0);
for (uint32_t t = 0; t <= 250; t += 10) m.tick(t);
TEST_ASSERT_EQUAL(3, s.ticks.size());
TEST_ASSERT_EQUAL(0, s.ticks[0]);
TEST_ASSERT_EQUAL(100, s.ticks[1]);
TEST_ASSERT_EQUAL(200, s.ticks[2]);
}
void test_each_service_keeps_its_own_interval() {
std::vector<std::string> log;
FakeService fast("fast", 10, &log), slow("slow", 100, &log);
ServiceManager m;
m.add(fast);
m.add(slow);
m.startAll(0);
for (uint32_t t = 0; t < 100; t += 10) m.tick(t);
TEST_ASSERT_EQUAL(10, fast.ticks.size());
TEST_ASSERT_EQUAL(1, slow.ticks.size());
}
void test_late_tick_does_not_cause_a_burst_of_catch_up_ticks() {
std::vector<std::string> log;
FakeService s("s", 100, &log);
ServiceManager m;
m.add(s);
m.startAll(0);
m.tick(0);
m.tick(1000); // UI stalled for a second
m.tick(1010);
TEST_ASSERT_EQUAL(2, s.ticks.size());
}
void test_ticking_survives_millis_wraparound() {
std::vector<std::string> log;
FakeService s("s", 100, &log);
ServiceManager m;
m.add(s);
const uint32_t nearWrap = 0xFFFFFFFFu - 50;
m.startAll(nearWrap);
m.tick(nearWrap);
m.tick(nearWrap + 60); // wrapped, only 60 ms elapsed
m.tick(nearWrap + 100); // wrapped, 100 ms elapsed
TEST_ASSERT_EQUAL(2, s.ticks.size());
}
void test_services_are_not_ticked_before_start_or_after_stop() {
std::vector<std::string> log;
FakeService s("s", 10, &log);
ServiceManager m;
m.add(s);
m.tick(0);
m.startAll(0);
m.stopAll();
m.tick(100);
TEST_ASSERT_EQUAL(0, s.ticks.size());
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_services_start_in_order_and_stop_in_reverse);
RUN_TEST(test_service_ticks_immediately_after_start_then_at_its_interval);
RUN_TEST(test_each_service_keeps_its_own_interval);
RUN_TEST(test_late_tick_does_not_cause_a_burst_of_catch_up_ticks);
RUN_TEST(test_ticking_survives_millis_wraparound);
RUN_TEST(test_services_are_not_ticked_before_start_or_after_stop);
return UNITY_END();
}