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
@@ -8,6 +8,7 @@
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/rtc.h>
|
||||
#include <tactility/log.h>
|
||||
#include <tactility/system_event.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <ctime>
|
||||
@@ -96,15 +97,17 @@ static void writeRtcFromSystemTime(Device* rtc) {
|
||||
}
|
||||
}
|
||||
|
||||
void RtcTimeService::onTimeChanged(kernel::SystemEvent event) {
|
||||
if (event == kernel::SystemEvent::Time) {
|
||||
Device* rtc = findRtcDevice();
|
||||
if (rtc) {
|
||||
writeRtcFromSystemTime(rtc);
|
||||
}
|
||||
void RtcTimeService::onTimeChanged() {
|
||||
Device* rtc = findRtcDevice();
|
||||
if (rtc) {
|
||||
writeRtcFromSystemTime(rtc);
|
||||
}
|
||||
}
|
||||
|
||||
void RtcTimeService::onTimeChangedTrampoline(struct SystemEvent* /*event*/, void* context) {
|
||||
static_cast<RtcTimeService*>(context)->onTimeChanged();
|
||||
}
|
||||
|
||||
bool RtcTimeService::onStart(ServiceContext& serviceContext) {
|
||||
Device* rtc = findRtcDevice();
|
||||
if (!rtc) {
|
||||
@@ -113,22 +116,21 @@ bool RtcTimeService::onStart(ServiceContext& serviceContext) {
|
||||
}
|
||||
|
||||
if (setSystemTimeFromRtc(rtc)) {
|
||||
// Publish time event so other components know time is now valid
|
||||
kernel::publishSystemEvent(kernel::SystemEvent::Time);
|
||||
// Emit time event so other components know time is now valid
|
||||
system_event_emit(KERNEL_EVENT_TIME_CHANGED, nullptr, 0);
|
||||
}
|
||||
|
||||
timeEventSubscription = kernel::subscribeSystemEvent(
|
||||
kernel::SystemEvent::Time,
|
||||
[this](kernel::SystemEvent event) { onTimeChanged(event); }
|
||||
);
|
||||
if (system_event_subscribe(KERNEL_EVENT_TIME_CHANGED, &RtcTimeService::onTimeChangedTrampoline, this) == ERROR_NONE) {
|
||||
timeEventSubscribed = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void RtcTimeService::onStop(ServiceContext& serviceContext) {
|
||||
if (timeEventSubscription != 0) {
|
||||
kernel::unsubscribeSystemEvent(timeEventSubscription);
|
||||
timeEventSubscription = 0;
|
||||
if (timeEventSubscribed) {
|
||||
system_event_unsubscribe(KERNEL_EVENT_TIME_CHANGED, &RtcTimeService::onTimeChangedTrampoline);
|
||||
timeEventSubscribed = false;
|
||||
}
|
||||
|
||||
if (rtcDevice) {
|
||||
|
||||
@@ -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