Refactored events (#621)

Event handling:

- Added unified event handling for application, system, and Wi‑Fi events.
- Updated all apps to reflect event handling changes

Wi-Fi:

- Refactored event handling from listener interface to task & event group.
- Added direct Wi‑Fi radio controls and improved event subscriptions.
- Wi‑Fi screens now refresh asynchronously and handle unavailable devices more gracefully.
- Enabled Wi‑Fi by default on in dts files, but radio on/off is still done by code. The main reason was reliable event subscription and consistent devicetree states.
- Improved Wi‑Fi shutdown cleanup and radio-state handling.

Other:
- Renamed the Kernel Display app to Display.
This commit is contained in:
Ken Van Hoeylandt
2026-08-25 17:44:20 +02:00
committed by GitHub
parent db48dfe812
commit ab75d2022d
121 changed files with 2416 additions and 1381 deletions
+90 -32
View File
@@ -7,13 +7,18 @@
#include <app/event.h>
#include <app/manager.h>
#include <app/manifest.h>
#include <app/scheduler.h>
#include <lvgl_window_manager/window_manager.h>
#include <tactility/check.h>
#include <tactility/device.h>
#include <tactility/log.h>
#include <lvgl/lvgl.h>
#include <atomic>
namespace tt::app::wifimanage {
constexpr auto* TAG = "WifiManage";
@@ -24,12 +29,18 @@ namespace {
struct Context {
uint32_t appInstanceId;
PubSub<service::wifi::WifiEvent>::SubscriptionHandle wifiSubscription = nullptr;
// Set once in appMain() before subscribing, left null if this device has no WiFi driver
Device* wifiDevice = nullptr;
WifiEventSubscription wifiEventSub {};
Mutex mutex;
Bindings bindings {};
State state;
View view = View(&bindings, &state);
TaskEventGroup* eventGroup = nullptr;
uint32_t refreshBit = 0;
std::atomic<bool> needsRefresh {false};
void lock() { mutex.lock(); }
void unlock() { mutex.unlock(); }
};
@@ -62,17 +73,18 @@ static void onConnectToHidden() {
wificonnect::start();
}
void requestViewUpdate(Context* ctx) {
ctx->lock();
void updateView(Context* ctx) {
// Same lock order as createWidgets() (called with the LVGL lock already held, per the
// window-manager's WindowCreateWidgetsFn contract, then acquiring ctx->mutex) - acquiring
// these in the opposite order here would deadlock against a concurrent createWidgets() call.
lvgl_lock();
// Safe even while buried (e.g. WifiApSettings/WifiConnect opened on top): destroyWidgets()
// nulls the view's widget pointers before they're deleted, and update() no-ops on that.
ctx->lock();
ctx->view.update();
lvgl_unlock();
ctx->unlock();
lvgl_unlock();
}
void onWifiEvent(Context* ctx, service::wifi::WifiEvent event) {
void onWifiEvent(Context* ctx, WifiEvent event) {
auto radio_state = service::wifi::getRadioState();
LOG_I(TAG, "Update with state %s", service::wifi::radioStateToString(radio_state));
ctx->state.setRadioState(radio_state);
@@ -93,7 +105,7 @@ void onWifiEvent(Context* ctx, service::wifi::WifiEvent event) {
break;
}
requestViewUpdate(ctx);
updateView(ctx);
}
void createWidgets(lv_obj_t* parent, void* userData) {
@@ -101,18 +113,18 @@ void createWidgets(lv_obj_t* parent, void* userData) {
ctx->lock();
ctx->state.setConnectSsid("Connected"); // TODO update with proper SSID
ctx->view.init(ctx->appInstanceId, parent);
ctx->view.update();
ctx->unlock();
ctx->needsRefresh = true;
task_event_group_signal(ctx->eventGroup, ctx->refreshBit);
}
// Runs with the LVGL lock already held, possibly on another app's thread - see
// WindowDestroyWidgetsFn's warnings. Must stay lock-free: View::reset() only nulls pointers.
void destroyWidgets(void* userData) {
auto* ctx = static_cast<Context*>(userData);
ctx->view.reset();
}
int32_t appMain(uint32_t appInstanceId, int argc, char* argv[]) {
int32_t appMain(int argc, char* argv[]) {
uint32_t appInstanceId = app_scheduler_current_app_id();
Context ctx;
ctx.appInstanceId = appInstanceId;
ctx.bindings = (Bindings) {
@@ -123,18 +135,32 @@ int32_t appMain(uint32_t appInstanceId, int argc, char* argv[]) {
.onConnectToHidden = onConnectToHidden
};
ctx.wifiSubscription = service::wifi::getPubsub()->subscribe([&ctx](auto event) {
onWifiEvent(&ctx, event);
});
// State update (it has its own locking)
ctx.state.setRadioState(service::wifi::getRadioState());
ctx.state.setScanning(service::wifi::isScanning());
ctx.state.updateApRecords();
TaskEventGroup event_group {};
task_event_group_construct(&event_group);
ctx.eventGroup = &event_group;
if (task_event_group_claim_bit(&event_group, &ctx.refreshBit) != ERROR_NONE) {
LOG_W(TAG, "Failed to claim a refresh bit; resurfacing after burial won't repopulate the view");
}
AppEventSubscription sub {};
sub.app_instance_id = appInstanceId;
app_event_subscribe(&sub);
check(app_event_subscribe(&sub, &event_group) == ERROR_NONE);
Device* wifi_device = nullptr;
if (device_get_first_by_type(&WIFI_TYPE, &wifi_device) == ERROR_NONE) {
if (wifi_event_subscribe(wifi_device, &ctx.wifiEventSub, &event_group) == ERROR_NONE) {
ctx.wifiDevice = wifi_device;
} else {
LOG_W(TAG, "Failed to subscribe to WiFi events");
device_put(wifi_device);
}
} else {
LOG_W(TAG, "No WiFi device found");
}
WindowId window = window_manager_create_ext(appInstanceId, createWidgets, destroyWidgets, &ctx);
@@ -154,26 +180,58 @@ int32_t appMain(uint32_t appInstanceId, int argc, char* argv[]) {
bool shouldClose = false;
while (!shouldClose) {
AppEvent event {};
if (app_event_await(&sub, &event, portMAX_DELAY) != ERROR_NONE) {
break;
// If the wifi device wasn't started yet when this app opened (boot-order race),
// ctx.wifiDevice is still null and there's no wifi-driven wake source to learn
// "it's ready now" from, so poll for it on a bounded timeout instead of blocking
// indefinitely. Once subscribed, this reverts to
// portMAX_DELAY - task_event_group_wait_any() still returns immediately for app_event
// and (once live) wifi_event, this timeout only matters while neither has fired yet.
TickType_t wait_timeout = (ctx.wifiDevice == nullptr) ? pdMS_TO_TICKS(500) : portMAX_DELAY;
task_event_group_wait_any(&event_group, nullptr, wait_timeout);
if (ctx.wifiDevice == nullptr) {
Device* retry_device = nullptr;
if (device_get_first_by_type(&WIFI_TYPE, &retry_device) == ERROR_NONE) {
if (wifi_event_subscribe(retry_device, &ctx.wifiEventSub, &event_group) == ERROR_NONE) {
ctx.wifiDevice = retry_device;
} else {
device_put(retry_device);
}
}
}
switch (event.type) {
case APP_EVENT_CLOSE:
shouldClose = true;
break;
default:
break;
AppEvent event {};
while (app_event_poll(&sub, &event) == ERROR_NONE) {
switch (event.type) {
case APP_EVENT_CLOSE:
shouldClose = true;
break;
default:
break;
}
if (shouldClose) break;
}
if (ctx.wifiDevice != nullptr) {
WifiEvent wifi_event {};
while (wifi_event_poll(&ctx.wifiEventSub, &wifi_event) == ERROR_NONE) {
onWifiEvent(&ctx, wifi_event);
}
}
if (ctx.needsRefresh.exchange(false)) {
updateView(&ctx);
}
}
ctx.lock();
service::wifi::getPubsub()->unsubscribe(ctx.wifiSubscription);
ctx.wifiSubscription = nullptr;
ctx.unlock();
if (ctx.wifiDevice != nullptr) {
wifi_event_unsubscribe(ctx.wifiDevice, &ctx.wifiEventSub);
device_put(ctx.wifiDevice);
}
window_manager_remove(window);
app_event_unsubscribe(&sub);
check(app_event_unsubscribe(&sub) == ERROR_NONE);
task_event_group_destruct(&event_group);
return 0;
}