Ken Van Hoeylandt f6cdabf3c0
Merge develop into main (#348)
## App state

Improved app state management in `LoaderService` and `GuiService`:

- Re-ordered some of the state transitions
- Hardened `GuiService` for repeated events (that might trigger a re-render of an app that's already rendered)
- Validate state transitions in `LoaderService` and crash if an app transitions from the wrong state to the next one.

## LoaderService

- Removed `tt::loader::` functions and expose `LoaderService` interface publicly.
- Implement `stopAll()` and `stopAll(id)` which stops all instances of an app, including any apps that were launched by it.
- Rename `stop()` functions to `stopTop()`
- Created `stopTop(id)` which only stops the top-most app when the app id matches.
- Moved `loader::LoaderEvent` to `loader::LoaderService::Event`
- Changed app instance `std::stack` to `std::vector`

## Improvements

- `ElfApp`: error 22 now shows a hint that `main()` might be missing
- Starting, installing and uninstalling apps now stops any running app (and its children) on the stack

## Bugfixes

- `HttpdReq` out of memory issue now shows an error message and doesn't crash anymore (this would happen on devices without PSRAM with WiFi active, when an app was installed)
- `GuiService::hideApp()` lock should not wait for timeout and now waits indefinitely
- `Buildscript/release-sdk-current.sh` deletes the previous local release before building a new one

## Code correctness

- App classes were made `final`
- Apps that had a `void start()` now have a `LaunchId start()`
- `tt::app::State`: renamed `Started` to `Created` and `Stopped` to `Destroyed` to properly reflect earlier name changes
2025-09-27 18:04:09 +02:00

148 lines
3.9 KiB
C++

#include <Tactility/app/wifimanage/WifiManagePrivate.h>
#include <Tactility/app/wifimanage/View.h>
#include <Tactility/app/AppContext.h>
#include <Tactility/app/wifiapsettings/WifiApSettings.h>
#include <Tactility/app/wificonnect/WifiConnect.h>
#include <Tactility/lvgl/LvglSync.h>
#include <Tactility/service/loader/Loader.h>
namespace tt::app::wifimanage {
constexpr auto TAG = "WifiManage";
extern const AppManifest manifest;
static void onConnect(const std::string& ssid) {
service::wifi::settings::WifiApSettings settings;
if (service::wifi::settings::load(ssid, settings)) {
TT_LOG_I(TAG, "Connecting with known credentials");
service::wifi::connect(settings, false);
} else {
TT_LOG_I(TAG, "Starting connection dialog");
wificonnect::start(ssid);
}
}
static void onShowApSettings(const std::string& ssid) {
wifiapsettings::start(ssid);
}
static void onDisconnect() {
service::wifi::disconnect();
}
static void onWifiToggled(bool enabled) {
service::wifi::setEnabled(enabled);
}
static void onConnectToHidden() {
wificonnect::start();
}
WifiManage::WifiManage() {
bindings = (Bindings) {
.onWifiToggled = onWifiToggled,
.onConnectSsid = onConnect,
.onDisconnect = onDisconnect,
.onShowApSettings = onShowApSettings,
.onConnectToHidden = onConnectToHidden
};
}
void WifiManage::lock() {
mutex.lock();
}
void WifiManage::unlock() {
mutex.unlock();
}
void WifiManage::requestViewUpdate() {
lock();
if (isViewEnabled) {
if (lvgl::lock(1000)) {
view.update();
lvgl::unlock();
} else {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "LVGL");
}
}
unlock();
}
void WifiManage::onWifiEvent(service::wifi::WifiEvent event) {
auto radio_state = service::wifi::getRadioState();
TT_LOG_I(TAG, "Update with state %s", service::wifi::radioStateToString(radio_state));
getState().setRadioState(radio_state);
switch (event) {
using enum service::wifi::WifiEvent;
case ScanStarted:
getState().setScanning(true);
break;
case ScanFinished:
getState().setScanning(false);
getState().updateApRecords();
break;
case RadioStateOn:
if (!service::wifi::isScanning()) {
service::wifi::scan();
}
break;
default:
break;
}
requestViewUpdate();
}
void WifiManage::onShow(AppContext& app, lv_obj_t* parent) {
wifiSubscription = service::wifi::getPubsub()->subscribe([this](auto event) {
onWifiEvent(event);
});
// State update (it has its own locking)
state.setRadioState(service::wifi::getRadioState());
state.setScanning(service::wifi::isScanning());
state.updateApRecords();
// View update
lock();
isViewEnabled = true;
state.setConnectSsid("Connected"); // TODO update with proper SSID
view.init(app, parent);
view.update();
unlock();
service::wifi::RadioState radio_state = service::wifi::getRadioState();
bool can_scan = radio_state == service::wifi::RadioState::On ||
radio_state == service::wifi::RadioState::ConnectionPending ||
radio_state == service::wifi::RadioState::ConnectionActive;
TT_LOG_I(TAG, "%s %d", service::wifi::radioStateToString(radio_state), (int)service::wifi::isScanning());
if (can_scan && !service::wifi::isScanning()) {
service::wifi::scan();
}
}
void WifiManage::onHide(TT_UNUSED AppContext& app) {
lock();
service::wifi::getPubsub()->unsubscribe(wifiSubscription);
wifiSubscription = nullptr;
isViewEnabled = false;
unlock();
}
extern const AppManifest manifest = {
.appId = "WifiManage",
.appName = "Wi-Fi",
.appIcon = LV_SYMBOL_WIFI,
.appCategory = Category::Settings,
.createApp = create<WifiManage>
};
LaunchId start() {
return app::start(manifest.appId);
}
} // namespace