App and Service improvements (#106)

This commit is contained in:
Ken Van Hoeylandt
2024-12-05 22:46:27 +01:00
committed by GitHub
parent e86a11b7e2
commit 50ee77d572
69 changed files with 692 additions and 596 deletions
+6 -6
View File
@@ -18,8 +18,8 @@ Gui* gui = nullptr;
void loader_callback(const void* message, TT_UNUSED void* context) {
auto* event = static_cast<const loader::LoaderEvent*>(message);
if (event->type == loader::LoaderEventTypeApplicationShowing) {
app::App& app = event->app_showing.app;
const app::Manifest& app_manifest = app.getManifest();
app::AppContext& app = event->app_showing.app;
const app::AppManifest& app_manifest = app.getManifest();
showApp(app, app_manifest.onShow, app_manifest.onHide);
} else if (event->type == loader::LoaderEventTypeApplicationHiding) {
hideApp();
@@ -77,7 +77,7 @@ void requestDraw() {
thread_flags_set(thread_id, GUI_THREAD_FLAG_DRAW);
}
void showApp(app::App& app, ViewPortShowCallback on_show, ViewPortHideCallback on_hide) {
void showApp(app::AppContext& app, ViewPortShowCallback on_show, ViewPortHideCallback on_hide) {
lock();
tt_check(gui->app_view_port == nullptr);
gui->app_view_port = view_port_alloc(app, on_show, on_hide);
@@ -128,14 +128,14 @@ static int32_t gui_main(TT_UNUSED void* p) {
// region AppManifest
static void start(TT_UNUSED Service& service) {
static void start(TT_UNUSED ServiceContext& service) {
gui = gui_alloc();
gui->thread->setPriority(THREAD_PRIORITY_SERVICE);
gui->thread->start();
}
static void stop(TT_UNUSED Service& service) {
static void stop(TT_UNUSED ServiceContext& service) {
lock();
ThreadId thread_id = gui->thread->getId();
@@ -148,7 +148,7 @@ static void stop(TT_UNUSED Service& service) {
gui_free(gui);
}
extern const Manifest manifest = {
extern const ServiceManifest manifest = {
.id = "Gui",
.onStart = &start,
.onStop = &stop
+2 -2
View File
@@ -1,6 +1,6 @@
#pragma once
#include "app/App.h"
#include "app/AppContext.h"
#include "ViewPort.h"
namespace tt::service::gui {
@@ -14,7 +14,7 @@ typedef struct Gui Gui;
* @param on_show
* @param on_hide
*/
void showApp(app::App& app, ViewPortShowCallback on_show, ViewPortHideCallback on_hide);
void showApp(app::AppContext& app, ViewPortShowCallback on_show, ViewPortHideCallback on_hide);
/**
* Hide the current app's viewport.
+2 -2
View File
@@ -9,7 +9,7 @@ namespace tt::service::gui {
#define TAG "gui"
static lv_obj_t* create_app_views(Gui* gui, lv_obj_t* parent, app::App& app) {
static lv_obj_t* create_app_views(Gui* gui, lv_obj_t* parent, app::AppContext& app) {
lvgl::obj_set_style_bg_blacken(parent);
lv_obj_t* vertical_container = lv_obj_create(parent);
@@ -49,7 +49,7 @@ void redraw(Gui* gui) {
ViewPort* view_port = gui->app_view_port;
if (view_port != nullptr) {
app::App& app = view_port->app;
app::AppContext& app = view_port->app;
lv_obj_t* container = create_app_views(gui, gui->lvgl_parent, app);
view_port_show(view_port, container);
} else {
+1 -1
View File
@@ -9,7 +9,7 @@ namespace tt::service::gui {
#define TAG "viewport"
ViewPort* view_port_alloc(
app::App& app,
app::AppContext& app,
ViewPortShowCallback on_show,
ViewPortHideCallback on_hide
) {
+6 -6
View File
@@ -1,6 +1,6 @@
#pragma once
#include "app/App.h"
#include "app/AppContext.h"
#include "lvgl.h"
namespace tt::service::gui {
@@ -8,18 +8,18 @@ namespace tt::service::gui {
/** ViewPort Draw callback
* @warning called from GUI thread
*/
typedef void (*ViewPortShowCallback)(app::App& app, lv_obj_t* parent);
typedef void (*ViewPortHideCallback)(app::App& app);
typedef void (*ViewPortShowCallback)(app::AppContext& app, lv_obj_t* parent);
typedef void (*ViewPortHideCallback)(app::AppContext& app);
// TODO: Move internally, use handle publicly
typedef struct ViewPort {
app::App& app;
app::AppContext& app;
ViewPortShowCallback onShow;
ViewPortHideCallback _Nullable onHide;
ViewPort(
app::App& app,
app::AppContext& app,
ViewPortShowCallback on_show,
ViewPortHideCallback _Nullable on_hide
) : app(app), onShow(on_show), onHide(on_hide) {}
@@ -35,7 +35,7 @@ typedef struct ViewPort {
* @return ViewPort instance
*/
ViewPort* view_port_alloc(
app::App& app,
app::AppContext& app,
ViewPortShowCallback on_show,
ViewPortHideCallback on_hide
);
+19 -19
View File
@@ -1,8 +1,8 @@
#include "Tactility.h"
#include <Mutex.h>
#include "app/Manifest.h"
#include "app/AppManifest.h"
#include "app/ManifestRegistry.h"
#include "service/Manifest.h"
#include "service/ServiceManifest.h"
#include "service/gui/Gui.h"
#include "service/loader/Loader_i.h"
#include "RtosCompat.h"
@@ -63,7 +63,7 @@ static void loader_unlock() {
tt_check(tt_mutex_release(loader_singleton->mutex) == TtStatusOk);
}
LoaderStatus startApp(const std::string& id, bool blocking, const Bundle& arguments) {
LoaderStatus startApp(const std::string& id, bool blocking, std::shared_ptr<const Bundle> parameters) {
TT_LOG_I(TAG, "Start app %s", id.c_str());
tt_assert(loader_singleton);
@@ -71,7 +71,7 @@ LoaderStatus startApp(const std::string& id, bool blocking, const Bundle& argume
.value = LoaderStatusOk
};
auto* start_message = new LoaderMessageAppStart(id, arguments);
auto* start_message = new LoaderMessageAppStart(id, parameters);
LoaderMessage message(start_message, result);
EventFlag* event_flag = blocking ? new EventFlag() : nullptr;
@@ -99,12 +99,12 @@ void stopApp() {
loader_singleton->queue.put(&message, TtWaitForever);
}
app::App* _Nullable getCurrentApp() {
app::AppContext* _Nullable getCurrentApp() {
tt_assert(loader_singleton);
loader_lock();
app::AppInstance* app = loader_singleton->app_stack.top();
loader_unlock();
return dynamic_cast<app::App*>(app);
return dynamic_cast<app::AppContext*>(app);
}
PubSub* getPubsub() {
@@ -133,7 +133,7 @@ static const char* app_state_to_string(app::State state) {
}
static void app_transition_to_state(app::AppInstance& app, app::State state) {
const app::Manifest& manifest = app.getManifest();
const app::AppManifest& manifest = app.getManifest();
const app::State old_state = app.getState();
TT_LOG_I(
@@ -187,15 +187,15 @@ static void app_transition_to_state(app::AppInstance& app, app::State state) {
}
static LoaderStatus loader_do_start_app_with_manifest(
const app::Manifest* manifest,
const Bundle& bundle
const app::AppManifest* manifest,
std::shared_ptr<const Bundle> _Nullable parameters
) {
TT_LOG_I(TAG, "start with manifest %s", manifest->id.c_str());
loader_lock();
auto previous_app = !loader_singleton->app_stack.empty() ? loader_singleton->app_stack.top() : nullptr;
auto new_app = new app::AppInstance(*manifest, bundle);
auto new_app = new app::AppInstance(*manifest, parameters);
new_app->mutableFlags().showStatusbar = (manifest->type != app::TypeBoot);
loader_singleton->app_stack.push(new_app);
@@ -227,16 +227,16 @@ static LoaderStatus loader_do_start_app_with_manifest(
static LoaderStatus do_start_by_id(
const std::string& id,
const Bundle& bundle
std::shared_ptr<const Bundle> _Nullable parameters
) {
TT_LOG_I(TAG, "Start by id %s", id.c_str());
const app::Manifest* manifest = app::findAppById(id);
const app::AppManifest* manifest = app::findAppById(id);
if (manifest == nullptr) {
TT_LOG_E(TAG, "App not found: %s", id.c_str());
return LoaderStatusErrorUnknownApp;
} else {
return loader_do_start_app_with_manifest(manifest, bundle);
return loader_do_start_app_with_manifest(manifest, parameters);
}
}
@@ -263,7 +263,7 @@ static void do_stop_app() {
std::unique_ptr<app::ResultHolder> result_holder = std::move(app_to_stop->getResult());
const app::Manifest& manifest = app_to_stop->getManifest();
const app::AppManifest& manifest = app_to_stop->getManifest();
app_transition_to_state(*app_to_stop, app::StateHiding);
app_transition_to_state(*app_to_stop, app::StateStopped);
@@ -283,7 +283,7 @@ static void do_stop_app() {
auto on_result = app_to_resume->getManifest().onResult;
if (on_result != nullptr) {
if (result_holder != nullptr) {
Bundle* result_bundle = result_holder->resultData;
auto result_bundle = result_holder->resultData.get();
if (result_bundle != nullptr) {
on_result(
*app_to_resume,
@@ -335,7 +335,7 @@ static int32_t loader_main(TT_UNUSED void* parameter) {
case LoaderMessageTypeAppStart:
message.result.status_value.value = do_start_by_id(
message.payload.start->id,
message.payload.start->bundle
message.payload.start->parameters
);
if (message.api_lock != nullptr) {
message.api_lock->set(TT_API_LOCK_EVENT);
@@ -359,7 +359,7 @@ static int32_t loader_main(TT_UNUSED void* parameter) {
// region AppManifest
static void loader_start(TT_UNUSED Service& service) {
static void loader_start(TT_UNUSED ServiceContext& service) {
tt_check(loader_singleton == nullptr);
loader_singleton = loader_alloc();
@@ -367,7 +367,7 @@ static void loader_start(TT_UNUSED Service& service) {
loader_singleton->thread->start();
}
static void loader_stop(TT_UNUSED Service& service) {
static void loader_stop(TT_UNUSED ServiceContext& service) {
tt_check(loader_singleton != nullptr);
// Send stop signal to thread and wait for thread to finish
@@ -383,7 +383,7 @@ static void loader_stop(TT_UNUSED Service& service) {
loader_singleton = nullptr;
}
extern const Manifest manifest = {
extern const ServiceManifest manifest = {
.id = "Loader",
.onStart = &loader_start,
.onStop = &loader_stop
+6 -5
View File
@@ -1,9 +1,10 @@
#pragma once
#include "app/Manifest.h"
#include "app/AppManifest.h"
#include "Bundle.h"
#include "Pubsub.h"
#include "service/Manifest.h"
#include "service/ServiceManifest.h"
#include <memory>
namespace tt::service::loader {
@@ -21,17 +22,17 @@ typedef enum {
* @brief Start an app
* @param[in] id application name or id
* @param[in] blocking whether this call is blocking or not. You cannot call this from an LVGL thread.
* @param[in] arguments optional parameters to pass onto the application
* @param[in] parameters optional parameters to pass onto the application
* @return LoaderStatus
*/
LoaderStatus startApp(const std::string& id, bool blocking = false, const Bundle& arguments = Bundle());
LoaderStatus startApp(const std::string& id, bool blocking = false, std::shared_ptr<const Bundle> _Nullable parameters = nullptr);
/**
* @brief Stop the currently showing app. Show the previous app if any app was still running.
*/
void stopApp();
app::App* _Nullable getCurrentApp();
app::AppContext* _Nullable getCurrentApp();
/**
* @brief PubSub for LoaderEvent
@@ -3,7 +3,7 @@
#include "Mutex.h"
#include "ScreenshotTask.h"
#include "service/Service.h"
#include "service/ServiceContext.h"
#include "service/ServiceRegistry.h"
#include "TactilityCore.h"
@@ -11,7 +11,7 @@ namespace tt::service::screenshot {
#define TAG "screenshot_service"
extern const Manifest manifest;
extern const ServiceManifest manifest;
typedef struct {
Mutex* mutex;
@@ -41,12 +41,12 @@ static void service_data_unlock(ServiceData* data) {
tt_check(tt_mutex_release(data->mutex) == TtStatusOk);
}
static void on_start(Service& service) {
static void on_start(ServiceContext& service) {
ServiceData* data = service_data_alloc();
service.setData(data);
}
static void on_stop(Service& service) {
static void on_stop(ServiceContext& service) {
auto* data = static_cast<ServiceData*>(service.getData());
if (data->task) {
task::free(data->task);
@@ -95,7 +95,7 @@ void startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount) {
}
void stop() {
_Nullable Service* service = findServiceById(manifest.id);
_Nullable ServiceContext* service = findServiceById(manifest.id);
if (service == nullptr) {
TT_LOG_E(TAG, "Service not found");
return;
@@ -132,7 +132,7 @@ bool isStarted() {
return getMode() != ScreenshotModeNone;
}
extern const Manifest manifest = {
extern const ServiceManifest manifest = {
.id = "Screenshot",
.onStart = &on_start,
.onStop = &on_stop
@@ -1,7 +1,7 @@
#include "ScreenshotTask.h"
#include "lv_screenshot.h"
#include "app/App.h"
#include "app/AppContext.h"
#include "Mutex.h"
#include "TactilityCore.h"
#include "Thread.h"
@@ -98,9 +98,9 @@ static int32_t screenshot_task(void* context) {
break; // Interrupted loop
}
} else if (data->work.type == TASK_WORK_TYPE_APPS) {
app::App* _Nullable app = loader::getCurrentApp();
app::AppContext* _Nullable app = loader::getCurrentApp();
if (app) {
const app::Manifest& manifest = app->getManifest();
const app::AppManifest& manifest = app->getManifest();
if (manifest.id != last_app_id) {
delay_ms(100);
last_app_id = manifest.id;
@@ -2,7 +2,7 @@
#include "hal/Power.h"
#include "hal/sdcard/Sdcard.h"
#include "Mutex.h"
#include "service/Service.h"
#include "service/ServiceContext.h"
#include "service/wifi/Wifi.h"
#include "Tactility.h"
#include "lvgl/Statusbar.h"
@@ -184,7 +184,7 @@ int32_t service_main(TT_UNUSED void* parameter) {
return 0;
}
static void on_start(Service& service) {
static void on_start(ServiceContext& service) {
ServiceData* data = service_data_alloc();
service.setData(data);
@@ -195,7 +195,7 @@ static void on_start(Service& service) {
data->thread->start();
}
static void on_stop(Service& service) {
static void on_stop(ServiceContext& service) {
auto* data = static_cast<ServiceData*>(service.getData());
// Stop thread
@@ -208,7 +208,7 @@ static void on_stop(Service& service) {
service_data_free(data);
}
extern const Manifest manifest = {
extern const ServiceManifest manifest = {
.id = "Statusbar",
.onStart = &on_start,
.onStop = &on_stop