Replace Tactility SystemEvents with new kernel implementation (#598)
TactilityKernel now has a system event API to replace the one from the Tactility subproject. It also implements several tests for it.
This commit is contained in:
committed by
GitHub
parent
6e55e71e67
commit
acb2f1d4c7
@@ -3,7 +3,6 @@
|
||||
#include <Tactility/CoreDefines.h>
|
||||
#include <Tactility/LogMessages.h>
|
||||
#include <Tactility/RecursiveMutex.h>
|
||||
#include <Tactility/SystemEvents.h>
|
||||
#include <Tactility/Tactility.h>
|
||||
#include <Tactility/Timer.h>
|
||||
#include <Tactility/service/Service.h>
|
||||
@@ -16,6 +15,7 @@
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/wifi.h>
|
||||
#include <tactility/log.h>
|
||||
#include <tactility/system_event.h>
|
||||
#include <tactility/time.h>
|
||||
#include <tactility/wifi_auto_scan.h>
|
||||
|
||||
@@ -78,7 +78,7 @@ struct WifiServiceState {
|
||||
uint16_t scanRecordLimit = TT_WIFI_SCAN_RECORD_LIMIT;
|
||||
TickType_t lastScanTime = MAX_TICKS;
|
||||
std::unique_ptr<Timer> autoConnectTimer;
|
||||
kernel::SystemEventSubscription bootEventSubscription = kernel::NoSystemEventSubscription;
|
||||
bool bootEventSubscribed = false;
|
||||
};
|
||||
|
||||
WifiServiceState state;
|
||||
@@ -278,7 +278,7 @@ void onAutoConnectTimer() {
|
||||
|
||||
// ---- Kernel driver event bridge ----
|
||||
|
||||
void onWifiDeviceEvent(Device* /*device*/, void* /*context*/, ::WifiEvent event) {
|
||||
void onWifiDeviceEvent(Device* device, void* /*context*/, ::WifiEvent event) {
|
||||
switch (event.type) {
|
||||
case WIFI_EVENT_TYPE_SCAN_FINISHED:
|
||||
getMainDispatcher().dispatch([] { dispatchAutoConnect(); });
|
||||
@@ -291,7 +291,8 @@ void onWifiDeviceEvent(Device* /*device*/, void* /*context*/, ::WifiEvent event)
|
||||
// Resetting it on every disconnect (including deliberate ones) would
|
||||
// let auto-connect immediately reconnect the user. Attempts that fail
|
||||
// while pending are unpaused via WIFI_EVENT_TYPE_STATION_CONNECTION_RESULT below.
|
||||
kernel::publishSystemEvent(kernel::SystemEvent::NetworkDisconnected);
|
||||
NetworkDisconnectedEvent disconnected_event = { .device = device };
|
||||
system_event_emit(KERNEL_EVENT_NETWORK_DISCONNECTED, &disconnected_event, sizeof(disconnected_event));
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -319,7 +320,6 @@ void onWifiDeviceEvent(Device* /*device*/, void* /*context*/, ::WifiEvent event)
|
||||
if (remember && !settings::save(target)) {
|
||||
LOG_E(TAG, "Failed to store credentials");
|
||||
}
|
||||
kernel::publishSystemEvent(kernel::SystemEvent::NetworkConnected);
|
||||
} else {
|
||||
// The pending connection attempt (which paused auto-connect via connect())
|
||||
// failed; unpause so auto-connect can try other saved APs.
|
||||
@@ -492,6 +492,10 @@ std::string getIp() {
|
||||
|
||||
namespace {
|
||||
|
||||
void onBootCompleted(struct SystemEvent* /*event*/, void* /*context*/) {
|
||||
bootSplashInit();
|
||||
}
|
||||
|
||||
class WifiService final : public Service {
|
||||
|
||||
public:
|
||||
@@ -506,9 +510,9 @@ public:
|
||||
LOG_W(TAG, "No WiFi device found");
|
||||
}
|
||||
|
||||
state.bootEventSubscription = kernel::subscribeSystemEvent(kernel::SystemEvent::BootSplash, [](auto) {
|
||||
bootSplashInit();
|
||||
});
|
||||
if (system_event_subscribe(KERNEL_EVENT_BOOT_COMPLETED, onBootCompleted, nullptr) == ERROR_NONE) {
|
||||
state.bootEventSubscribed = true;
|
||||
}
|
||||
|
||||
auto timer_interval = std::min(2000, AUTO_SCAN_INTERVAL);
|
||||
state.autoConnectTimer = std::make_unique<Timer>(Timer::Type::Periodic, timer_interval, [] { onAutoConnectTimer(); });
|
||||
@@ -526,8 +530,10 @@ public:
|
||||
state.autoConnectTimer->stop();
|
||||
state.autoConnectTimer = nullptr; // Must release as it holds a reference via its callback.
|
||||
|
||||
kernel::unsubscribeSystemEvent(state.bootEventSubscription);
|
||||
state.bootEventSubscription = kernel::NoSystemEventSubscription;
|
||||
if (state.bootEventSubscribed) {
|
||||
system_event_unsubscribe(KERNEL_EVENT_BOOT_COMPLETED, onBootCompleted);
|
||||
state.bootEventSubscribed = false;
|
||||
}
|
||||
|
||||
if (state.device != nullptr && device_is_ready(state.device)) {
|
||||
wifi_remove_event_callback(state.device, onWifiDeviceEvent);
|
||||
|
||||
Reference in New Issue
Block a user