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
This commit is contained in:
committed by
GitHub
parent
dcf28d0868
commit
f6cdabf3c0
@@ -1,19 +1,17 @@
|
||||
#include "Tactility/service/loader/Loader.h"
|
||||
#include "Tactility/app/AppInstance.h"
|
||||
#include "Tactility/app/AppManifest.h"
|
||||
#include "Tactility/app/AppRegistration.h"
|
||||
#include <Tactility/service/loader/Loader.h>
|
||||
#include <Tactility/app/AppInstance.h>
|
||||
#include <Tactility/app/AppManifest.h>
|
||||
#include <Tactility/app/AppRegistration.h>
|
||||
|
||||
#include <Tactility/DispatcherThread.h>
|
||||
#include <Tactility/service/ServiceManifest.h>
|
||||
#include <Tactility/service/ServiceRegistration.h>
|
||||
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include <esp_heap_caps.h>
|
||||
#include <utility>
|
||||
#else
|
||||
#include "Tactility/lvgl/LvglSync.h"
|
||||
#endif
|
||||
|
||||
namespace tt::service::loader {
|
||||
@@ -21,6 +19,7 @@ namespace tt::service::loader {
|
||||
constexpr auto* TAG = "Loader";
|
||||
constexpr auto LOADER_TIMEOUT = (100 / portTICK_PERIOD_MS);
|
||||
|
||||
// Forward declaration
|
||||
extern const ServiceManifest manifest;
|
||||
|
||||
static const char* appStateToString(app::State state) {
|
||||
@@ -28,63 +27,19 @@ static const char* appStateToString(app::State state) {
|
||||
using enum app::State;
|
||||
case Initial:
|
||||
return "initial";
|
||||
case Started:
|
||||
case Created:
|
||||
return "started";
|
||||
case Showing:
|
||||
return "showing";
|
||||
case Hiding:
|
||||
return "hiding";
|
||||
case Stopped:
|
||||
case Destroyed:
|
||||
return "stopped";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
// region AppManifest
|
||||
|
||||
class LoaderService final : public Service {
|
||||
|
||||
std::shared_ptr<PubSub<LoaderEvent>> pubsubExternal = std::make_shared<PubSub<LoaderEvent>>();
|
||||
Mutex mutex = Mutex(Mutex::Type::Recursive);
|
||||
std::stack<std::shared_ptr<app::AppInstance>> appStack;
|
||||
app::LaunchId nextLaunchId = 0;
|
||||
|
||||
/** The dispatcher thread needs a callstack large enough to accommodate all the dispatched methods.
|
||||
* This includes full LVGL redraw via Gui::redraw()
|
||||
*/
|
||||
std::unique_ptr<DispatcherThread> dispatcherThread = std::make_unique<DispatcherThread>("loader_dispatcher", 6144); // Files app requires ~5k
|
||||
|
||||
void onStartAppMessage(const std::string& id, app::LaunchId launchId, std::shared_ptr<const Bundle> parameters);
|
||||
void onStopAppMessage(const std::string& id);
|
||||
|
||||
void transitionAppToState(const std::shared_ptr<app::AppInstance>& app, app::State state);
|
||||
|
||||
public:
|
||||
|
||||
bool onStart(TT_UNUSED ServiceContext& service) override {
|
||||
dispatcherThread->start();
|
||||
return true;
|
||||
}
|
||||
|
||||
void onStop(TT_UNUSED ServiceContext& service) override {
|
||||
// Send stop signal to thread and wait for thread to finish
|
||||
mutex.withLock([this] {
|
||||
dispatcherThread->stop();
|
||||
});
|
||||
}
|
||||
|
||||
app::LaunchId startApp(const std::string& id, std::shared_ptr<const Bundle> parameters);
|
||||
void stopApp();
|
||||
std::shared_ptr<app::AppContext> _Nullable getCurrentAppContext();
|
||||
|
||||
std::shared_ptr<PubSub<LoaderEvent>> getPubsub() const { return pubsubExternal; }
|
||||
};
|
||||
|
||||
std::shared_ptr<LoaderService> _Nullable optScreenshotService() {
|
||||
return service::findServiceById<LoaderService>(manifest.id);
|
||||
}
|
||||
|
||||
void LoaderService::onStartAppMessage(const std::string& id, app::LaunchId launchId, std::shared_ptr<const Bundle> parameters) {
|
||||
TT_LOG_I(TAG, "Start by id %s", id.c_str());
|
||||
|
||||
@@ -100,26 +55,22 @@ void LoaderService::onStartAppMessage(const std::string& id, app::LaunchId launc
|
||||
return;
|
||||
}
|
||||
|
||||
auto previous_app = !appStack.empty() ? appStack.top() : nullptr;
|
||||
auto previous_app = !appStack.empty() ? appStack[appStack.size() - 1]: nullptr;
|
||||
auto new_app = std::make_shared<app::AppInstance>(app_manifest, launchId, parameters);
|
||||
|
||||
new_app->mutableFlags().hideStatusbar = (app_manifest->appFlags & app::AppManifest::Flags::HideStatusBar);
|
||||
|
||||
appStack.push(new_app);
|
||||
transitionAppToState(new_app, app::State::Initial);
|
||||
transitionAppToState(new_app, app::State::Started);
|
||||
|
||||
// We might have to hide the previous app first
|
||||
if (previous_app != nullptr) {
|
||||
transitionAppToState(previous_app, app::State::Hiding);
|
||||
}
|
||||
|
||||
appStack.push_back(new_app);
|
||||
transitionAppToState(new_app, app::State::Created);
|
||||
transitionAppToState(new_app, app::State::Showing);
|
||||
|
||||
pubsubExternal->publish(LoaderEvent::ApplicationStarted);
|
||||
}
|
||||
|
||||
void LoaderService::onStopAppMessage(const std::string& id) {
|
||||
void LoaderService::onStopTopAppMessage(const std::string& id) {
|
||||
auto lock = mutex.asScopedLock();
|
||||
if (!lock.lock(LOADER_TIMEOUT)) {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
|
||||
@@ -134,7 +85,7 @@ void LoaderService::onStopAppMessage(const std::string& id) {
|
||||
}
|
||||
|
||||
// Stop current app
|
||||
auto app_to_stop = appStack.top();
|
||||
auto app_to_stop = appStack[appStack.size() - 1];
|
||||
|
||||
if (app_to_stop->getManifest().appId != id) {
|
||||
TT_LOG_E(TAG, "Stop app: id mismatch (wanted %s but found %s on top of stack)", id.c_str(), app_to_stop->getManifest().appId.c_str());
|
||||
@@ -156,9 +107,9 @@ void LoaderService::onStopAppMessage(const std::string& id) {
|
||||
auto app_to_stop_launch_id = app_to_stop->getLaunchId();
|
||||
|
||||
transitionAppToState(app_to_stop, app::State::Hiding);
|
||||
transitionAppToState(app_to_stop, app::State::Stopped);
|
||||
transitionAppToState(app_to_stop, app::State::Destroyed);
|
||||
|
||||
appStack.pop();
|
||||
appStack.pop_back();
|
||||
|
||||
// We only expect the app to be referenced within the current scope
|
||||
if (app_to_stop.use_count() > 1) {
|
||||
@@ -177,7 +128,7 @@ void LoaderService::onStopAppMessage(const std::string& id) {
|
||||
std::shared_ptr<app::AppInstance> instance_to_resume;
|
||||
// If there's a previous app, resume it
|
||||
if (!appStack.empty()) {
|
||||
instance_to_resume = appStack.top();
|
||||
instance_to_resume = appStack[appStack.size() - 1];
|
||||
assert(instance_to_resume);
|
||||
transitionAppToState(instance_to_resume, app::State::Showing);
|
||||
}
|
||||
@@ -186,8 +137,6 @@ void LoaderService::onStopAppMessage(const std::string& id) {
|
||||
lock.unlock();
|
||||
// WARNING: After this point we cannot change the app states from this method directly anymore as we don't have a lock!
|
||||
|
||||
pubsubExternal->publish(LoaderEvent::ApplicationStopped);
|
||||
|
||||
if (instance_to_resume != nullptr) {
|
||||
if (result_set) {
|
||||
if (result_bundle != nullptr) {
|
||||
@@ -206,7 +155,6 @@ void LoaderService::onStopAppMessage(const std::string& id) {
|
||||
);
|
||||
}
|
||||
} else {
|
||||
const Bundle empty_bundle;
|
||||
instance_to_resume->getApp()->onResult(
|
||||
*instance_to_resume,
|
||||
app_to_stop_launch_id,
|
||||
@@ -217,6 +165,70 @@ void LoaderService::onStopAppMessage(const std::string& id) {
|
||||
}
|
||||
}
|
||||
|
||||
int LoaderService::findAppInStack(const std::string& id) const {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
for (size_t i = 0; i < appStack.size(); i++) {
|
||||
if (appStack[i]->getManifest().appId == id) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void LoaderService::onStopAllAppMessage(const std::string& id) {
|
||||
auto lock = mutex.asScopedLock();
|
||||
if (!lock.lock(LOADER_TIMEOUT)) {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isRunning(id)) {
|
||||
TT_LOG_E(TAG, "Stop all: %s not running", id.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
int app_to_stop_index = findAppInStack(id);
|
||||
if (app_to_stop_index < 0) {
|
||||
TT_LOG_E(TAG, "Stop all: %s not found in stack", id.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Find an app to resume, if any
|
||||
std::shared_ptr<app::AppInstance> instance_to_resume;
|
||||
if (app_to_stop_index > 0) {
|
||||
instance_to_resume = appStack[app_to_stop_index - 1];
|
||||
assert(instance_to_resume);
|
||||
}
|
||||
|
||||
// Stop all apps and find the LaunchId of the last-closed app, so we can call onResult() if needed
|
||||
app::LaunchId last_launch_id = 0;
|
||||
for (int i = appStack.size() - 1; i >= app_to_stop_index; i--) {
|
||||
auto app_to_stop = appStack[i];
|
||||
// Hide the app first in case it's still being shown
|
||||
if (app_to_stop->getState() == app::State::Showing) {
|
||||
transitionAppToState(app_to_stop, app::State::Hiding);
|
||||
}
|
||||
transitionAppToState(app_to_stop, app::State::Destroyed);
|
||||
last_launch_id = app_to_stop->getLaunchId();
|
||||
|
||||
appStack.pop_back();
|
||||
}
|
||||
|
||||
if (instance_to_resume != nullptr) {
|
||||
TT_LOG_I(TAG, "Resuming %s", instance_to_resume->getManifest().appId.c_str());
|
||||
transitionAppToState(instance_to_resume, app::State::Showing);
|
||||
|
||||
instance_to_resume->getApp()->onResult(
|
||||
*instance_to_resume,
|
||||
last_launch_id,
|
||||
app::Result::Cancelled,
|
||||
nullptr
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void LoaderService::transitionAppToState(const std::shared_ptr<app::AppInstance>& app, app::State state) {
|
||||
const app::AppManifest& app_manifest = app->getManifest();
|
||||
const app::State old_state = app->getState();
|
||||
@@ -232,89 +244,87 @@ void LoaderService::transitionAppToState(const std::shared_ptr<app::AppInstance>
|
||||
switch (state) {
|
||||
using enum app::State;
|
||||
case Initial:
|
||||
break;
|
||||
case Started:
|
||||
tt_crash(LOG_MESSAGE_ILLEGAL_STATE);
|
||||
case Created:
|
||||
assert(app->getState() == app::State::Initial);
|
||||
app->getApp()->onCreate(*app);
|
||||
pubsubExternal->publish(Event::ApplicationStarted);
|
||||
break;
|
||||
case Showing: {
|
||||
pubsubExternal->publish(LoaderEvent::ApplicationShowing);
|
||||
assert(app->getState() == app::State::Hiding || app->getState() == app::State::Created);
|
||||
pubsubExternal->publish(Event::ApplicationShowing);
|
||||
break;
|
||||
}
|
||||
case Hiding: {
|
||||
pubsubExternal->publish(LoaderEvent::ApplicationHiding);
|
||||
assert(app->getState() == app::State::Showing);
|
||||
pubsubExternal->publish(Event::ApplicationHiding);
|
||||
break;
|
||||
}
|
||||
case Stopped:
|
||||
// TODO: Verify manifest
|
||||
case Destroyed:
|
||||
app->getApp()->onDestroy(*app);
|
||||
pubsubExternal->publish(Event::ApplicationStopped);
|
||||
break;
|
||||
}
|
||||
|
||||
app->setState(state);
|
||||
}
|
||||
|
||||
app::LaunchId LoaderService::startApp(const std::string& id, std::shared_ptr<const Bundle> parameters) {
|
||||
auto launch_id = nextLaunchId++;
|
||||
app::LaunchId LoaderService::start(const std::string& id, std::shared_ptr<const Bundle> parameters) {
|
||||
const auto launch_id = nextLaunchId++;
|
||||
dispatcherThread->dispatch([this, id, launch_id, parameters]() {
|
||||
onStartAppMessage(id, launch_id, parameters);
|
||||
});
|
||||
return launch_id;
|
||||
}
|
||||
|
||||
void LoaderService::stopApp() {
|
||||
TT_LOG_I(TAG, "stopApp()");
|
||||
auto id = getCurrentAppContext()->getManifest().appId;
|
||||
dispatcherThread->dispatch([this, id]() {
|
||||
onStopAppMessage(id);
|
||||
void LoaderService::stopTop() {
|
||||
const auto& id = getCurrentAppContext()->getManifest().appId;
|
||||
stopTop(id);
|
||||
}
|
||||
|
||||
void LoaderService::stopTop(const std::string& id) {
|
||||
TT_LOG_I(TAG, "dispatching stopTop(%s)", id.c_str());
|
||||
dispatcherThread->dispatch([this, id] {
|
||||
onStopTopAppMessage(id);
|
||||
});
|
||||
}
|
||||
|
||||
void LoaderService::stopAll(const std::string& id) {
|
||||
TT_LOG_I(TAG, "dispatching stopAll(%s)", id.c_str());
|
||||
dispatcherThread->dispatch([this, id] {
|
||||
onStopAllAppMessage(id);
|
||||
});
|
||||
TT_LOG_I(TAG, "dispatched");
|
||||
}
|
||||
|
||||
std::shared_ptr<app::AppContext> _Nullable LoaderService::getCurrentAppContext() {
|
||||
auto lock = mutex.asScopedLock();
|
||||
const auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
return appStack.top();
|
||||
if (appStack.empty()) {
|
||||
return nullptr;
|
||||
} else {
|
||||
return appStack[appStack.size() - 1];
|
||||
}
|
||||
}
|
||||
|
||||
// region Public API
|
||||
|
||||
app::LaunchId startApp(const std::string& id, std::shared_ptr<const Bundle> parameters) {
|
||||
TT_LOG_I(TAG, "Start app %s", id.c_str());
|
||||
auto service = optScreenshotService();
|
||||
assert(service);
|
||||
return service->startApp(id, std::move(parameters));
|
||||
bool LoaderService::isRunning(const std::string& id) const {
|
||||
const auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
for (const auto& app : appStack) {
|
||||
if (app->getManifest().appId == id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void stopApp() {
|
||||
TT_LOG_I(TAG, "Stop app");
|
||||
auto service = optScreenshotService();
|
||||
service->stopApp();
|
||||
std::shared_ptr<LoaderService> _Nullable findLoaderService() {
|
||||
return service::findServiceById<LoaderService>(manifest.id);
|
||||
}
|
||||
|
||||
std::shared_ptr<app::AppContext> _Nullable getCurrentAppContext() {
|
||||
auto service = optScreenshotService();
|
||||
assert(service);
|
||||
return service->getCurrentAppContext();
|
||||
}
|
||||
|
||||
std::shared_ptr<app::App> _Nullable getCurrentApp() {
|
||||
auto app_context = getCurrentAppContext();
|
||||
return app_context != nullptr ? app_context->getApp() : nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<PubSub<LoaderEvent>> getPubsub() {
|
||||
auto service = optScreenshotService();
|
||||
assert(service);
|
||||
return service->getPubsub();
|
||||
}
|
||||
|
||||
// endregion Public API
|
||||
|
||||
extern const ServiceManifest manifest = {
|
||||
.id = "Loader",
|
||||
.createService = create<LoaderService>
|
||||
};
|
||||
|
||||
// endregion
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user