Ken Van Hoeylandt c3bcf93698
Refactor app launching (#174)
- Refactor the way apps work: Instead of a C interface, they are now C++ classes. The main reasoning is that attaching data to an app was cumbersome. Having different implementations for different kinds of apps was cumbersome too. (3 or 4 layers of manifest nesting for the TactilityC project)
- External apps are still written in C, but they get a createData/destroyData in their manifest, so:
- External apps now have their own manifest.
- All functions in the original AppManifest are removed and replaced by a single `createApp` function
- External apps now automatically register (each app individually!) when they run the first time. As a side-effect they become visible in the `AppList` app!
- Adapted all apps for the new interface.
- Adapted all internal logic for these changes (Gui, ViewPort, Loader, AppContext, AppInstance, etc.)
- Rewrote parts of Loader to use std::shared_ptr to make the code much safer.
- Added a refcount check for the `AppInstance` and `App` at the end of their lifecycle. Show warning if refcount is too high.
2025-01-21 17:48:32 +01:00

132 lines
3.9 KiB
C++

#include "app/wificonnect/WifiConnectPrivate.h"
#include "app/AppContext.h"
#include "TactilityCore.h"
#include "service/loader/Loader.h"
#include "service/wifi/Wifi.h"
#include "lvgl/LvglSync.h"
namespace tt::app::wificonnect {
#define TAG "wifi_connect"
#define WIFI_CONNECT_PARAM_SSID "ssid" // String
#define WIFI_CONNECT_PARAM_PASSWORD "password" // String
extern const AppManifest manifest;
/** Returns the app data if the app is active. Note that this could clash if the same app is started twice and a background thread is slow. */
std::shared_ptr<WifiConnect> _Nullable optWifiConnect() {
auto appContext = service::loader::getCurrentAppContext();
if (appContext != nullptr && appContext->getManifest().id == manifest.id) {
return std::static_pointer_cast<WifiConnect>(appContext->getApp());
} else {
return nullptr;
}
}
static void eventCallback(const void* message, void* context) {
auto* event = static_cast<const service::wifi::Event*>(message);
auto* wifi = static_cast<WifiConnect*>(context);
State& state = wifi->getState();
switch (event->type) {
case service::wifi::EventType::ConnectionFailed:
if (state.isConnecting()) {
state.setConnecting(false);
state.setConnectionError(true);
wifi->requestViewUpdate();
}
break;
case service::wifi::EventType::ConnectionSuccess:
if (wifi->getState().isConnecting()) {
state.setConnecting(false);
service::loader::stopApp();
}
break;
default:
break;
}
wifi->requestViewUpdate();
}
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);
}
WifiConnect::WifiConnect() {
auto wifi_pubsub = service::wifi::getPubsub();
wifiSubscription = tt_pubsub_subscribe(wifi_pubsub, &eventCallback, this);
bindings = (Bindings) {
.onConnectSsid = onConnect,
.onConnectSsidContext = this,
};
}
WifiConnect::~WifiConnect() {
auto pubsub = service::wifi::getPubsub();
tt_pubsub_unsubscribe(pubsub, wifiSubscription);
}
void WifiConnect::lock() {
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
}
void WifiConnect::unlock() {
tt_check(mutex.release() == TtStatusOk);
}
void WifiConnect::requestViewUpdate() {
lock();
if (view_enabled) {
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();
view_enabled = 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();
view_enabled = false;
unlock();
}
extern const AppManifest manifest = {
.id = "WifiConnect",
.name = "Wi-Fi Connect",
.icon = LV_SYMBOL_WIFI,
.type = Type::Hidden,
.createApp = create<WifiConnect>
};
void 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);
service::loader::startApp(manifest.id, 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