Service data refactored (#109)

This commit is contained in:
Ken Van Hoeylandt
2024-12-06 01:02:53 +01:00
committed by GitHub
parent c6f1cd6098
commit 36bb25deba
16 changed files with 194 additions and 221 deletions
+1 -1
View File
@@ -60,7 +60,7 @@ static void onShow(TT_UNUSED AppContext& app, lv_obj_t* parent) {
}
static void onStart(AppContext& app) {
auto data = std::shared_ptr<Data>(new Data());
auto data = std::make_shared<Data>();
app.setData(data);
}
+4 -3
View File
@@ -12,6 +12,7 @@
#include "lvgl/Toolbar.h"
#include <dirent.h>
#include <unistd.h>
#include <memory>
namespace tt::app::files {
@@ -120,11 +121,11 @@ static void viewFile(const char* path, const char* filename) {
TT_LOG_I(TAG, "Clicked %s", filepath);
if (isSupportedImageFile(filename)) {
auto bundle = std::shared_ptr<Bundle>(new Bundle());
auto bundle = std::make_shared<Bundle>();
bundle->putString(IMAGE_VIEWER_FILE_ARGUMENT, processed_filepath);
service::loader::startApp("ImageViewer", false, bundle);
} else if (isSupportedTextFile(filename)) {
auto bundle = std::shared_ptr<Bundle>(new Bundle());
auto bundle = std::make_shared<Bundle>();
if (get_platform() == PlatformEsp) {
bundle->putString(TEXT_VIEWER_FILE_ARGUMENT, processed_filepath);
} else {
@@ -217,7 +218,7 @@ static void onShow(AppContext& app, lv_obj_t* parent) {
}
static void onStart(AppContext& app) {
auto data = std::shared_ptr<Data>(new Data());
auto data = std::make_shared<Data>();
// PC platform is bound to current work directory because of the LVGL file system mapping
if (get_platform() == PlatformSimulator) {
char cwd[PATH_MAX];
+4 -4
View File
@@ -21,11 +21,11 @@ private:
public:
void lock() {
void lock() const {
tt_check(mutex.acquire(1000) == TtStatusOk);
}
void unlock() {
void unlock() const {
tt_check(mutex.release() == TtStatusOk);
}
@@ -34,7 +34,7 @@ public:
void startTask();
void stopTask();
bool shouldInterruptTask() { return interruptTask; };
bool shouldInterruptTask() const { return interruptTask; };
void updatePinStates();
void updatePinWidgets();
@@ -210,7 +210,7 @@ static void onHide(AppContext& app) {
}
static void onStart(AppContext& app) {
auto gpio = std::shared_ptr<Gpio>(new Gpio());
auto gpio = std::make_shared<Gpio>();
app.setData(gpio);
}
@@ -165,7 +165,7 @@ static void onHide(AppContext& app) {
}
static void onStart(AppContext& app) {
auto data = std::shared_ptr<Data>(new Data());
auto data = std::make_shared<Data>();
app.setData(data);
}
+1 -1
View File
@@ -119,7 +119,7 @@ static void onHide(TT_UNUSED AppContext& app) {
}
static void onStart(AppContext& app) {
auto data = std::shared_ptr<Data>(new Data());
auto data = std::shared_ptr<Data>();
app.setData(data);
data->power = getConfiguration()->hardware->power;
assert(data->power != nullptr); // The Power app only shows up on supported devices
@@ -50,7 +50,7 @@ static void onListItemSelected(lv_event_t* e) {
size_t index = (size_t)(e->user_data);
TT_LOG_I(TAG, "Selected item at index %d", index);
tt::app::AppContext* app = service::loader::getCurrentApp();
auto bundle = std::shared_ptr<Bundle>(new Bundle());
auto bundle = std::make_shared<Bundle>();
setResultIndex(bundle, (int32_t)index);
app->setResult(app::ResultOk, bundle);
service::loader::stopApp();
@@ -82,7 +82,7 @@ static void onShow(AppContext& app, lv_obj_t* parent) {
app.setResult(ResultError);
service::loader::stopApp();
} else if (items.size() == 1) {
auto result_bundle = std::shared_ptr<Bundle>(new Bundle());
auto result_bundle = std::make_shared<Bundle>();
setResultIndex(result_bundle, 0);
app.setResult(ResultOk, result_bundle);
service::loader::stopApp();
@@ -24,7 +24,7 @@ const AppContext* _Nullable optWifiApSettingsApp() {
}
void start(const std::string& ssid) {
auto bundle = std::shared_ptr<Bundle>(new Bundle());
auto bundle = std::make_shared<Bundle>();
bundle->putString("ssid", ssid);
service::loader::startApp(manifest.id, false, bundle);
}
@@ -114,7 +114,7 @@ static void onHide(AppContext& app) {
}
static void onStart(AppContext& app) {
auto wifi = std::shared_ptr<WifiConnect>(new WifiConnect());
auto wifi = std::make_shared<WifiConnect>();
app.setData(wifi);
}
@@ -33,7 +33,7 @@ static void onConnect(const char* ssid) {
service::wifi::connect(&settings, false);
} else {
TT_LOG_I(TAG, "Starting connection dialog");
auto bundle = std::shared_ptr<Bundle>(new Bundle());
auto bundle = std::make_shared<Bundle>();
bundle->putString(WIFI_CONNECT_PARAM_SSID, ssid);
bundle->putString(WIFI_CONNECT_PARAM_PASSWORD, "");
service::loader::startApp("WifiConnect", false, bundle);
@@ -145,7 +145,7 @@ void WifiManage::onHide(TT_UNUSED AppContext& app) {
// region Manifest methods
static void onStart(AppContext& app) {
auto wifi = std::shared_ptr<WifiManage>(new WifiManage());
auto wifi = std::make_shared<WifiManage>();
app.setData(wifi);
}
@@ -1,5 +1,6 @@
#include "Screenshot.h"
#include <cstdlib>
#include <memory>
#include "Mutex.h"
#include "ScreenshotTask.h"
@@ -13,48 +14,25 @@ namespace tt::service::screenshot {
extern const ServiceManifest manifest;
typedef struct {
Mutex* mutex;
task::ScreenshotTask* task;
Mode mode;
} ServiceData;
struct ServiceData {
Mutex mutex;
task::ScreenshotTask* task = nullptr;
Mode mode = ScreenshotModeNone;
static ServiceData* service_data_alloc() {
auto* data = static_cast<ServiceData*>(malloc(sizeof(ServiceData)));
*data = (ServiceData) {
.mutex = tt_mutex_alloc(MutexTypeNormal),
.task = nullptr,
.mode = ScreenshotModeNone
};
return data;
}
static void service_data_free(ServiceData* data) {
tt_mutex_free(data->mutex);
}
static void service_data_lock(ServiceData* data) {
tt_check(tt_mutex_acquire(data->mutex, TtWaitForever) == TtStatusOk);
}
static void service_data_unlock(ServiceData* data) {
tt_check(tt_mutex_release(data->mutex) == TtStatusOk);
}
static void on_start(ServiceContext& service) {
ServiceData* data = service_data_alloc();
service.setData(data);
}
static void on_stop(ServiceContext& service) {
auto* data = static_cast<ServiceData*>(service.getData());
if (data->task) {
task::free(data->task);
data->task = nullptr;
~ServiceData() {
if (task) {
task::free(task);
}
}
tt_mutex_free(data->mutex);
service_data_free(data);
}
void lock() {
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
}
void unlock() {
tt_check(mutex.release() == TtStatusOk);
}
};
void startApps(const char* path) {
_Nullable auto* service = findServiceById(manifest.id);
@@ -63,8 +41,8 @@ void startApps(const char* path) {
return;
}
auto* data = static_cast<ServiceData*>(service->getData());
service_data_lock(data);
auto data = std::static_pointer_cast<ServiceData>(service->getData());
data->lock();
if (data->task == nullptr) {
data->task = task::alloc();
data->mode = ScreenshotModeApps;
@@ -72,7 +50,7 @@ void startApps(const char* path) {
} else {
TT_LOG_E(TAG, "Screenshot task already running");
}
service_data_unlock(data);
data->unlock();
}
void startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount) {
@@ -82,8 +60,8 @@ void startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount) {
return;
}
auto* data = static_cast<ServiceData*>(service->getData());
service_data_lock(data);
auto data = std::static_pointer_cast<ServiceData>(service->getData());
data->lock();
if (data->task == nullptr) {
data->task = task::alloc();
data->mode = ScreenshotModeTimed;
@@ -91,7 +69,7 @@ void startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount) {
} else {
TT_LOG_E(TAG, "Screenshot task already running");
}
service_data_unlock(data);
data->unlock();
}
void stop() {
@@ -101,8 +79,8 @@ void stop() {
return;
}
auto data = static_cast<ServiceData*>(service->getData());
service_data_lock(data);
auto data = std::static_pointer_cast<ServiceData>(service->getData());
data->lock();
if (data->task != nullptr) {
task::stop(data->task);
task::free(data->task);
@@ -111,7 +89,7 @@ void stop() {
} else {
TT_LOG_E(TAG, "Screenshot task not running");
}
service_data_unlock(data);
data->unlock();
}
Mode getMode() {
@@ -120,10 +98,10 @@ Mode getMode() {
TT_LOG_E(TAG, "Service not found");
return ScreenshotModeNone;
} else {
auto* data = static_cast<ServiceData*>(service->getData());
service_data_lock(data);
auto data = std::static_pointer_cast<ServiceData>(service->getData());
data->lock();
Mode mode = data->mode;
service_data_unlock(data);
data->unlock();
return mode;
}
}
@@ -132,10 +110,14 @@ bool isStarted() {
return getMode() != ScreenshotModeNone;
}
static void onStart(ServiceContext& service) {
auto data = std::make_shared<ServiceData>();
service.setData(data);
}
extern const ServiceManifest manifest = {
.id = "Screenshot",
.onStart = &on_start,
.onStop = &on_stop
.onStart = onStart
};
} // namespace
@@ -6,22 +6,39 @@
#include "service/wifi/Wifi.h"
#include "Tactility.h"
#include "lvgl/Statusbar.h"
#include "service/ServiceRegistry.h"
namespace tt::service::statusbar {
#define TAG "statusbar_service"
typedef struct {
Mutex* mutex;
Thread* thread;
bool service_interrupted;
int8_t wifi_icon_id;
const char* wifi_last_icon;
int8_t sdcard_icon_id;
const char* sdcard_last_icon;
int8_t power_icon_id;
const char* power_last_icon;
} ServiceData;
extern const ServiceManifest manifest;
struct ServiceData {
Mutex mutex;
Thread thread;
bool service_interrupted = false;
int8_t wifi_icon_id = lvgl::statusbar_icon_add(nullptr);
const char* wifi_last_icon = nullptr;
int8_t sdcard_icon_id = lvgl::statusbar_icon_add(nullptr);
const char* sdcard_last_icon = nullptr;
int8_t power_icon_id = lvgl::statusbar_icon_add(nullptr);
const char* power_last_icon = nullptr;
~ServiceData() {
lvgl::statusbar_icon_remove(wifi_icon_id);
lvgl::statusbar_icon_remove(sdcard_icon_id);
lvgl::statusbar_icon_remove(power_icon_id);
}
void lock() const {
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
}
void unlock() const {
tt_check(mutex.release() == TtStatusOk);
}
};
// region wifi
@@ -59,7 +76,7 @@ static const char* wifi_get_status_icon(wifi::WifiRadioState state, bool secure)
}
}
static void update_wifi_icon(ServiceData* data) {
static void update_wifi_icon(std::shared_ptr<ServiceData> data) {
wifi::WifiRadioState radio_state = wifi::getRadioState();
bool is_secure = wifi::isConnectionSecure();
const char* desired_icon = wifi_get_status_icon(radio_state, is_secure);
@@ -85,7 +102,7 @@ static _Nullable const char* sdcard_get_status_icon(hal::sdcard::State state) {
}
}
static void update_sdcard_icon(ServiceData* data) {
static void update_sdcard_icon(std::shared_ptr<ServiceData> data) {
hal::sdcard::State state = hal::sdcard::getState();
const char* desired_icon = sdcard_get_status_icon(state);
if (data->sdcard_last_icon != desired_icon) {
@@ -119,7 +136,7 @@ static _Nullable const char* power_get_status_icon() {
}
}
static void update_power_icon(ServiceData* data) {
static void update_power_icon(std::shared_ptr<ServiceData> data) {
const char* desired_icon = power_get_status_icon();
if (data->power_last_icon != desired_icon) {
lvgl::statusbar_icon_set_image(data->power_icon_id, desired_icon);
@@ -132,49 +149,21 @@ static void update_power_icon(ServiceData* data) {
// region service
static ServiceData* service_data_alloc() {
auto* data = static_cast<ServiceData*>(malloc(sizeof(ServiceData)));
*data = (ServiceData) {
.mutex = tt_mutex_alloc(MutexTypeNormal),
.thread = new Thread(),
.service_interrupted = false,
.wifi_icon_id = lvgl::statusbar_icon_add(nullptr),
.wifi_last_icon = nullptr,
.sdcard_icon_id = lvgl::statusbar_icon_add(nullptr),
.sdcard_last_icon = nullptr,
.power_icon_id = lvgl::statusbar_icon_add(nullptr),
.power_last_icon = nullptr
};
lvgl::statusbar_icon_set_visibility(data->wifi_icon_id, true);
update_wifi_icon(data);
update_sdcard_icon(data); // also updates visibility
update_power_icon(data);
return data;
}
static void service_data_free(ServiceData* data) {
tt_mutex_free(data->mutex);
delete data->thread;
lvgl::statusbar_icon_remove(data->wifi_icon_id);
lvgl::statusbar_icon_remove(data->sdcard_icon_id);
lvgl::statusbar_icon_remove(data->power_icon_id);
free(data);
free(data);
}
static void service_data_lock(ServiceData* data) {
tt_check(tt_mutex_acquire(data->mutex, TtWaitForever) == TtStatusOk);
}
static void service_data_unlock(ServiceData* data) {
tt_check(tt_mutex_release(data->mutex) == TtStatusOk);
}
int32_t service_main(TT_UNUSED void* parameter) {
int32_t serviceMain(TT_UNUSED void* parameter) {
TT_LOG_I(TAG, "Started main loop");
auto* data = (ServiceData*)parameter;
tt_assert(data != nullptr);
delay_ms(20); // TODO: Make service instance findable earlier on (but expose "starting" state?)
auto context = tt::service::findServiceById(manifest.id);
if (context == nullptr) {
TT_LOG_E(TAG, "Service not found");
return -1;
}
auto data = std::static_pointer_cast<ServiceData>(context->getData());
while (!data->service_interrupted) {
update_wifi_icon(data);
update_sdcard_icon(data);
@@ -184,34 +173,37 @@ int32_t service_main(TT_UNUSED void* parameter) {
return 0;
}
static void on_start(ServiceContext& service) {
ServiceData* data = service_data_alloc();
static void onStart(ServiceContext& service) {
auto data = std::make_shared<ServiceData>();
service.setData(data);
data->thread->setCallback(service_main, data);
data->thread->setPriority(Thread::PriorityLow);
data->thread->setStackSize(3000);
data->thread->setName("statusbar");
data->thread->start();
lvgl::statusbar_icon_set_visibility(data->wifi_icon_id, true);
update_wifi_icon(data);
update_sdcard_icon(data); // also updates visibility
update_power_icon(data);
data->thread.setCallback(serviceMain, nullptr);
data->thread.setPriority(Thread::PriorityLow);
data->thread.setStackSize(3000);
data->thread.setName("statusbar");
data->thread.start();
}
static void on_stop(ServiceContext& service) {
auto* data = static_cast<ServiceData*>(service.getData());
static void onStop(ServiceContext& service) {
auto data = std::static_pointer_cast<ServiceData>(service.getData());
// Stop thread
service_data_lock(data);
data->lock();
data->service_interrupted = true;
service_data_unlock(data);
tt_mutex_release(data->mutex);
data->thread->join();
service_data_free(data);
data->unlock();
data->thread.join();
}
extern const ServiceManifest manifest = {
.id = "Statusbar",
.onStart = &on_start,
.onStop = &on_stop
.onStart = onStart,
.onStop = onStop
};
// endregion service