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

121 lines
3.3 KiB
C++

#include <Tactility/app/wificonnect/WifiConnect.h>
#include <Tactility/app/AppContext.h>
#include <Tactility/service/loader/Loader.h>
#include <Tactility/service/wifi/Wifi.h>
#include <Tactility/lvgl/LvglSync.h>
namespace tt::app::wificonnect {
constexpr auto* TAG = "WifiConnect";
constexpr auto* WIFI_CONNECT_PARAM_SSID = "ssid"; // String
constexpr auto* WIFI_CONNECT_PARAM_PASSWORD = "password"; // String
extern const AppManifest manifest;
static void onConnect(const service::wifi::settings::WifiApSettings& ap_settings, bool remember, TT_UNUSED void* parameter) {
auto* wifi = static_cast<WifiConnect*>(parameter);
wifi->getState().setApSettings(ap_settings);
wifi->getState().setConnecting(true);
service::wifi::connect(ap_settings, remember);
}
void WifiConnect::onWifiEvent(service::wifi::WifiEvent event) {
State& state = getState();
switch (event) {
case service::wifi::WifiEvent::ConnectionFailed:
if (state.isConnecting()) {
state.setConnecting(false);
state.setConnectionError(true);
requestViewUpdate();
}
break;
case service::wifi::WifiEvent::ConnectionSuccess:
if (getState().isConnecting()) {
state.setConnecting(false);
stop(manifest.appId);
}
break;
default:
break;
}
requestViewUpdate();
}
WifiConnect::WifiConnect() {
wifiSubscription = service::wifi::getPubsub()->subscribe([this](auto event) {
onWifiEvent(event);
});
bindings = (Bindings) {
.onConnectSsid = onConnect,
.onConnectSsidContext = this,
};
}
WifiConnect::~WifiConnect() {
service::wifi::getPubsub()->unsubscribe(wifiSubscription);
}
void WifiConnect::lock() {
mutex.lock();
}
void WifiConnect::unlock() {
mutex.unlock();
}
void WifiConnect::requestViewUpdate() {
lock();
if (viewEnabled) {
if (lvgl::lock(1000)) {
view.update();
lvgl::unlock();
} else {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "LVGL");
}
}
unlock();
}
void WifiConnect::onShow(AppContext& app, lv_obj_t* parent) {
lock();
viewEnabled = true;
view.init(app, parent);
view.update();
unlock();
}
void WifiConnect::onHide(TT_UNUSED AppContext& app) {
// No need to lock view, as this is called from within Gui's LVGL context
lock();
viewEnabled = false;
unlock();
}
extern const AppManifest manifest = {
.appId = "WifiConnect",
.appName = "Wi-Fi Connect",
.appIcon = LV_SYMBOL_WIFI,
.appCategory = Category::System,
.appFlags = AppManifest::Flags::Hidden,
.createApp = create<WifiConnect>
};
LaunchId start(const std::string& ssid, const std::string& password) {
auto parameters = std::make_shared<Bundle>();
parameters->putString(WIFI_CONNECT_PARAM_SSID, ssid);
parameters->putString(WIFI_CONNECT_PARAM_PASSWORD, password);
return app::start(manifest.appId, parameters);
}
bool optSsidParameter(const std::shared_ptr<const Bundle>& bundle, std::string& ssid) {
return bundle->optString(WIFI_CONNECT_PARAM_SSID, ssid);
}
bool optPasswordParameter(const std::shared_ptr<const Bundle>& bundle, std::string& password) {
return bundle->optString(WIFI_CONNECT_PARAM_PASSWORD, password);
}
} // namespace