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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 194 additions and 221 deletions

View File

@ -60,7 +60,7 @@ static void onShow(TT_UNUSED AppContext& app, lv_obj_t* parent) {
} }
static void onStart(AppContext& app) { static void onStart(AppContext& app) {
auto data = std::shared_ptr<Data>(new Data()); auto data = std::make_shared<Data>();
app.setData(data); app.setData(data);
} }

View File

@ -12,6 +12,7 @@
#include "lvgl/Toolbar.h" #include "lvgl/Toolbar.h"
#include <dirent.h> #include <dirent.h>
#include <unistd.h> #include <unistd.h>
#include <memory>
namespace tt::app::files { namespace tt::app::files {
@ -120,11 +121,11 @@ static void viewFile(const char* path, const char* filename) {
TT_LOG_I(TAG, "Clicked %s", filepath); TT_LOG_I(TAG, "Clicked %s", filepath);
if (isSupportedImageFile(filename)) { 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); bundle->putString(IMAGE_VIEWER_FILE_ARGUMENT, processed_filepath);
service::loader::startApp("ImageViewer", false, bundle); service::loader::startApp("ImageViewer", false, bundle);
} else if (isSupportedTextFile(filename)) { } else if (isSupportedTextFile(filename)) {
auto bundle = std::shared_ptr<Bundle>(new Bundle()); auto bundle = std::make_shared<Bundle>();
if (get_platform() == PlatformEsp) { if (get_platform() == PlatformEsp) {
bundle->putString(TEXT_VIEWER_FILE_ARGUMENT, processed_filepath); bundle->putString(TEXT_VIEWER_FILE_ARGUMENT, processed_filepath);
} else { } else {
@ -217,7 +218,7 @@ static void onShow(AppContext& app, lv_obj_t* parent) {
} }
static void onStart(AppContext& app) { 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 // PC platform is bound to current work directory because of the LVGL file system mapping
if (get_platform() == PlatformSimulator) { if (get_platform() == PlatformSimulator) {
char cwd[PATH_MAX]; char cwd[PATH_MAX];

View File

@ -21,11 +21,11 @@ private:
public: public:
void lock() { void lock() const {
tt_check(mutex.acquire(1000) == TtStatusOk); tt_check(mutex.acquire(1000) == TtStatusOk);
} }
void unlock() { void unlock() const {
tt_check(mutex.release() == TtStatusOk); tt_check(mutex.release() == TtStatusOk);
} }
@ -34,7 +34,7 @@ public:
void startTask(); void startTask();
void stopTask(); void stopTask();
bool shouldInterruptTask() { return interruptTask; }; bool shouldInterruptTask() const { return interruptTask; };
void updatePinStates(); void updatePinStates();
void updatePinWidgets(); void updatePinWidgets();
@ -210,7 +210,7 @@ static void onHide(AppContext& app) {
} }
static void onStart(AppContext& app) { static void onStart(AppContext& app) {
auto gpio = std::shared_ptr<Gpio>(new Gpio()); auto gpio = std::make_shared<Gpio>();
app.setData(gpio); app.setData(gpio);
} }

View File

@ -165,7 +165,7 @@ static void onHide(AppContext& app) {
} }
static void onStart(AppContext& app) { static void onStart(AppContext& app) {
auto data = std::shared_ptr<Data>(new Data()); auto data = std::make_shared<Data>();
app.setData(data); app.setData(data);
} }

View File

@ -119,7 +119,7 @@ static void onHide(TT_UNUSED AppContext& app) {
} }
static void onStart(AppContext& app) { static void onStart(AppContext& app) {
auto data = std::shared_ptr<Data>(new Data()); auto data = std::shared_ptr<Data>();
app.setData(data); app.setData(data);
data->power = getConfiguration()->hardware->power; data->power = getConfiguration()->hardware->power;
assert(data->power != nullptr); // The Power app only shows up on supported devices assert(data->power != nullptr); // The Power app only shows up on supported devices

View File

@ -50,7 +50,7 @@ static void onListItemSelected(lv_event_t* e) {
size_t index = (size_t)(e->user_data); size_t index = (size_t)(e->user_data);
TT_LOG_I(TAG, "Selected item at index %d", index); TT_LOG_I(TAG, "Selected item at index %d", index);
tt::app::AppContext* app = service::loader::getCurrentApp(); 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); setResultIndex(bundle, (int32_t)index);
app->setResult(app::ResultOk, bundle); app->setResult(app::ResultOk, bundle);
service::loader::stopApp(); service::loader::stopApp();
@ -82,7 +82,7 @@ static void onShow(AppContext& app, lv_obj_t* parent) {
app.setResult(ResultError); app.setResult(ResultError);
service::loader::stopApp(); service::loader::stopApp();
} else if (items.size() == 1) { } 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); setResultIndex(result_bundle, 0);
app.setResult(ResultOk, result_bundle); app.setResult(ResultOk, result_bundle);
service::loader::stopApp(); service::loader::stopApp();

View File

@ -24,7 +24,7 @@ const AppContext* _Nullable optWifiApSettingsApp() {
} }
void start(const std::string& ssid) { void start(const std::string& ssid) {
auto bundle = std::shared_ptr<Bundle>(new Bundle()); auto bundle = std::make_shared<Bundle>();
bundle->putString("ssid", ssid); bundle->putString("ssid", ssid);
service::loader::startApp(manifest.id, false, bundle); service::loader::startApp(manifest.id, false, bundle);
} }

View File

@ -114,7 +114,7 @@ static void onHide(AppContext& app) {
} }
static void onStart(AppContext& app) { static void onStart(AppContext& app) {
auto wifi = std::shared_ptr<WifiConnect>(new WifiConnect()); auto wifi = std::make_shared<WifiConnect>();
app.setData(wifi); app.setData(wifi);
} }

View File

@ -33,7 +33,7 @@ static void onConnect(const char* ssid) {
service::wifi::connect(&settings, false); service::wifi::connect(&settings, false);
} else { } else {
TT_LOG_I(TAG, "Starting connection dialog"); 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_SSID, ssid);
bundle->putString(WIFI_CONNECT_PARAM_PASSWORD, ""); bundle->putString(WIFI_CONNECT_PARAM_PASSWORD, "");
service::loader::startApp("WifiConnect", false, bundle); service::loader::startApp("WifiConnect", false, bundle);
@ -145,7 +145,7 @@ void WifiManage::onHide(TT_UNUSED AppContext& app) {
// region Manifest methods // region Manifest methods
static void onStart(AppContext& app) { static void onStart(AppContext& app) {
auto wifi = std::shared_ptr<WifiManage>(new WifiManage()); auto wifi = std::make_shared<WifiManage>();
app.setData(wifi); app.setData(wifi);
} }

View File

@ -1,5 +1,6 @@
#include "Screenshot.h" #include "Screenshot.h"
#include <cstdlib> #include <cstdlib>
#include <memory>
#include "Mutex.h" #include "Mutex.h"
#include "ScreenshotTask.h" #include "ScreenshotTask.h"
@ -13,48 +14,25 @@ namespace tt::service::screenshot {
extern const ServiceManifest manifest; extern const ServiceManifest manifest;
typedef struct { struct ServiceData {
Mutex* mutex; Mutex mutex;
task::ScreenshotTask* task; task::ScreenshotTask* task = nullptr;
Mode mode; Mode mode = ScreenshotModeNone;
} ServiceData;
static ServiceData* service_data_alloc() { ~ServiceData() {
auto* data = static_cast<ServiceData*>(malloc(sizeof(ServiceData))); if (task) {
*data = (ServiceData) { task::free(task);
.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;
} }
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) { void startApps(const char* path) {
_Nullable auto* service = findServiceById(manifest.id); _Nullable auto* service = findServiceById(manifest.id);
@ -63,8 +41,8 @@ void startApps(const char* path) {
return; return;
} }
auto* data = static_cast<ServiceData*>(service->getData()); auto data = std::static_pointer_cast<ServiceData>(service->getData());
service_data_lock(data); data->lock();
if (data->task == nullptr) { if (data->task == nullptr) {
data->task = task::alloc(); data->task = task::alloc();
data->mode = ScreenshotModeApps; data->mode = ScreenshotModeApps;
@ -72,7 +50,7 @@ void startApps(const char* path) {
} else { } else {
TT_LOG_E(TAG, "Screenshot task already running"); 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) { 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; return;
} }
auto* data = static_cast<ServiceData*>(service->getData()); auto data = std::static_pointer_cast<ServiceData>(service->getData());
service_data_lock(data); data->lock();
if (data->task == nullptr) { if (data->task == nullptr) {
data->task = task::alloc(); data->task = task::alloc();
data->mode = ScreenshotModeTimed; data->mode = ScreenshotModeTimed;
@ -91,7 +69,7 @@ void startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount) {
} else { } else {
TT_LOG_E(TAG, "Screenshot task already running"); TT_LOG_E(TAG, "Screenshot task already running");
} }
service_data_unlock(data); data->unlock();
} }
void stop() { void stop() {
@ -101,8 +79,8 @@ void stop() {
return; return;
} }
auto data = static_cast<ServiceData*>(service->getData()); auto data = std::static_pointer_cast<ServiceData>(service->getData());
service_data_lock(data); data->lock();
if (data->task != nullptr) { if (data->task != nullptr) {
task::stop(data->task); task::stop(data->task);
task::free(data->task); task::free(data->task);
@ -111,7 +89,7 @@ void stop() {
} else { } else {
TT_LOG_E(TAG, "Screenshot task not running"); TT_LOG_E(TAG, "Screenshot task not running");
} }
service_data_unlock(data); data->unlock();
} }
Mode getMode() { Mode getMode() {
@ -120,10 +98,10 @@ Mode getMode() {
TT_LOG_E(TAG, "Service not found"); TT_LOG_E(TAG, "Service not found");
return ScreenshotModeNone; return ScreenshotModeNone;
} else { } else {
auto* data = static_cast<ServiceData*>(service->getData()); auto data = std::static_pointer_cast<ServiceData>(service->getData());
service_data_lock(data); data->lock();
Mode mode = data->mode; Mode mode = data->mode;
service_data_unlock(data); data->unlock();
return mode; return mode;
} }
} }
@ -132,10 +110,14 @@ bool isStarted() {
return getMode() != ScreenshotModeNone; return getMode() != ScreenshotModeNone;
} }
static void onStart(ServiceContext& service) {
auto data = std::make_shared<ServiceData>();
service.setData(data);
}
extern const ServiceManifest manifest = { extern const ServiceManifest manifest = {
.id = "Screenshot", .id = "Screenshot",
.onStart = &on_start, .onStart = onStart
.onStop = &on_stop
}; };
} // namespace } // namespace

View File

@ -6,22 +6,39 @@
#include "service/wifi/Wifi.h" #include "service/wifi/Wifi.h"
#include "Tactility.h" #include "Tactility.h"
#include "lvgl/Statusbar.h" #include "lvgl/Statusbar.h"
#include "service/ServiceRegistry.h"
namespace tt::service::statusbar { namespace tt::service::statusbar {
#define TAG "statusbar_service" #define TAG "statusbar_service"
typedef struct { extern const ServiceManifest manifest;
Mutex* mutex;
Thread* thread; struct ServiceData {
bool service_interrupted; Mutex mutex;
int8_t wifi_icon_id; Thread thread;
const char* wifi_last_icon; bool service_interrupted = false;
int8_t sdcard_icon_id; int8_t wifi_icon_id = lvgl::statusbar_icon_add(nullptr);
const char* sdcard_last_icon; const char* wifi_last_icon = nullptr;
int8_t power_icon_id; int8_t sdcard_icon_id = lvgl::statusbar_icon_add(nullptr);
const char* power_last_icon; const char* sdcard_last_icon = nullptr;
} ServiceData; 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 // 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(); wifi::WifiRadioState radio_state = wifi::getRadioState();
bool is_secure = wifi::isConnectionSecure(); bool is_secure = wifi::isConnectionSecure();
const char* desired_icon = wifi_get_status_icon(radio_state, is_secure); 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(); hal::sdcard::State state = hal::sdcard::getState();
const char* desired_icon = sdcard_get_status_icon(state); const char* desired_icon = sdcard_get_status_icon(state);
if (data->sdcard_last_icon != desired_icon) { 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(); const char* desired_icon = power_get_status_icon();
if (data->power_last_icon != desired_icon) { if (data->power_last_icon != desired_icon) {
lvgl::statusbar_icon_set_image(data->power_icon_id, 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 // 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) { 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) { int32_t serviceMain(TT_UNUSED void* parameter) {
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) {
TT_LOG_I(TAG, "Started main loop"); TT_LOG_I(TAG, "Started main loop");
auto* data = (ServiceData*)parameter; delay_ms(20); // TODO: Make service instance findable earlier on (but expose "starting" state?)
tt_assert(data != nullptr); 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) { while (!data->service_interrupted) {
update_wifi_icon(data); update_wifi_icon(data);
update_sdcard_icon(data); update_sdcard_icon(data);
@ -184,34 +173,37 @@ int32_t service_main(TT_UNUSED void* parameter) {
return 0; return 0;
} }
static void on_start(ServiceContext& service) { static void onStart(ServiceContext& service) {
ServiceData* data = service_data_alloc(); auto data = std::make_shared<ServiceData>();
service.setData(data); service.setData(data);
data->thread->setCallback(service_main, data); lvgl::statusbar_icon_set_visibility(data->wifi_icon_id, true);
data->thread->setPriority(Thread::PriorityLow); update_wifi_icon(data);
data->thread->setStackSize(3000); update_sdcard_icon(data); // also updates visibility
data->thread->setName("statusbar"); update_power_icon(data);
data->thread->start();
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) { static void onStop(ServiceContext& service) {
auto* data = static_cast<ServiceData*>(service.getData()); auto data = std::static_pointer_cast<ServiceData>(service.getData());
// Stop thread // Stop thread
service_data_lock(data); data->lock();
data->service_interrupted = true; data->service_interrupted = true;
service_data_unlock(data); data->unlock();
tt_mutex_release(data->mutex); data->thread.join();
data->thread->join();
service_data_free(data);
} }
extern const ServiceManifest manifest = { extern const ServiceManifest manifest = {
.id = "Statusbar", .id = "Statusbar",
.onStart = &on_start, .onStart = onStart,
.onStop = &on_stop .onStop = onStop
}; };
// endregion service // endregion service

View File

@ -10,7 +10,7 @@ private:
Mutex mutex = Mutex(MutexTypeNormal); Mutex mutex = Mutex(MutexTypeNormal);
const service::ServiceManifest& manifest; const service::ServiceManifest& manifest;
void* data = nullptr; std::shared_ptr<void> data = nullptr;
public: public:
@ -18,8 +18,8 @@ public:
~ServiceInstance() override = default; ~ServiceInstance() override = default;
const service::ServiceManifest& getManifest() const override; const service::ServiceManifest& getManifest() const override;
void* getData() const override; std::shared_ptr<void> getData() const override;
void setData(void* newData) override; void setData(std::shared_ptr<void> newData) override;
}; };
} }

View File

@ -2,6 +2,7 @@
#include "Mutex.h" #include "Mutex.h"
#include "ServiceManifest.h" #include "ServiceManifest.h"
#include <memory>
namespace tt::service { namespace tt::service {
@ -14,8 +15,8 @@ protected:
public: public:
virtual const service::ServiceManifest& getManifest() const = 0; virtual const service::ServiceManifest& getManifest() const = 0;
virtual void* getData() const = 0; virtual std::shared_ptr<void> getData() const = 0;
virtual void setData(void* newData) = 0; virtual void setData(std::shared_ptr<void> newData) = 0;
}; };
} // namespace } // namespace

View File

@ -1,3 +1,5 @@
#include <utility>
#include "service/ServiceInstance.h" #include "service/ServiceInstance.h"
namespace tt::service { namespace tt::service {
@ -6,16 +8,16 @@ ServiceInstance::ServiceInstance(const service::ServiceManifest&manifest) : mani
const service::ServiceManifest& ServiceInstance::getManifest() const { return manifest; } const service::ServiceManifest& ServiceInstance::getManifest() const { return manifest; }
void* ServiceInstance::getData() const { std::shared_ptr<void> ServiceInstance::getData() const {
mutex.acquire(TtWaitForever); mutex.acquire(TtWaitForever);
void* data_copy = data; std::shared_ptr<void> result = data;
mutex.release(); mutex.release();
return data_copy; return result;
} }
void ServiceInstance::setData(void* newData) { void ServiceInstance::setData(std::shared_ptr<void> newData) {
mutex.acquire(TtWaitForever); mutex.acquire(TtWaitForever);
data = newData; data = std::move(newData);
mutex.release(); mutex.release();
} }

View File

@ -4,56 +4,54 @@
#include "service/ServiceContext.h" #include "service/ServiceContext.h"
#include "TactilityCore.h" #include "TactilityCore.h"
#include "TactilityHeadless.h" #include "TactilityHeadless.h"
#include "service/ServiceRegistry.h"
#define TAG "sdcard_service" #define TAG "sdcard_service"
namespace tt::service::sdcard { namespace tt::service::sdcard {
static int32_t sdcard_task(void* context); static int32_t sdcard_task(void* context);
extern const ServiceManifest manifest;
typedef struct { struct ServiceData {
Mutex* mutex; Mutex mutex;
Thread* thread; Thread thread = Thread(
hal::sdcard::State lastState;
bool interrupted;
} ServiceData;
static ServiceData* service_data_alloc() {
auto* data = static_cast<ServiceData*>(malloc(sizeof(ServiceData)));
*data = (ServiceData) {
.mutex = tt_mutex_alloc(MutexTypeNormal),
.thread = new Thread(
"sdcard", "sdcard",
3000, // Minimum is ~2800 @ ESP-IDF 5.1.2 when ejecting sdcard 3000, // Minimum is ~2800 @ ESP-IDF 5.1.2 when ejecting sdcard
&sdcard_task, &sdcard_task,
data nullptr
), );
.lastState = hal::sdcard::StateUnmounted, hal::sdcard::State lastState = hal::sdcard::StateUnmounted;
.interrupted = false bool interrupted = false;
};
data->thread->setPriority(Thread::PriorityLow);
return data;
}
static void service_data_free(ServiceData* data) { ServiceData() {
tt_mutex_free(data->mutex); thread.setPriority(Thread::PriorityLow);
delete data->thread; }
}
static void service_data_lock(ServiceData* data) { void lock() const {
tt_check(tt_mutex_acquire(data->mutex, TtWaitForever) == TtStatusOk); tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
} }
static void service_data_unlock(ServiceData* data) { void unlock() const {
tt_check(tt_mutex_release(data->mutex) == TtStatusOk); tt_check(mutex.release() == TtStatusOk);
} }
};
static int32_t sdcard_task(TT_UNUSED void* context) {
delay_ms(20); // TODO: Make service instance findable earlier on (but expose "starting" state?)
auto service = findServiceById(manifest.id);
if (service == nullptr) {
TT_LOG_E(TAG, "Service not found");
return -1;
}
auto data = std::static_pointer_cast<ServiceData>(service->getData());
static int32_t sdcard_task(void* context) {
auto* data = (ServiceData*)context;
bool interrupted = false; bool interrupted = false;
do { do {
service_data_lock(data); data->lock();
interrupted = data->interrupted; interrupted = data->interrupted;
@ -68,40 +66,38 @@ static int32_t sdcard_task(void* context) {
data->lastState = new_state; data->lastState = new_state;
} }
service_data_unlock(data); data->lock();
delay_ms(2000); delay_ms(2000);
} while (!interrupted); } while (!interrupted);
return 0; return 0;
} }
static void on_start(ServiceContext& service) { static void onStart(ServiceContext& service) {
if (hal::getConfiguration().sdcard != nullptr) { if (hal::getConfiguration().sdcard != nullptr) {
ServiceData* data = service_data_alloc(); auto data = std::make_shared<ServiceData>();
service.setData(data); service.setData(data);
data->thread->start(); data->thread.start();
} else { } else {
TT_LOG_I(TAG, "task not started due to config"); TT_LOG_I(TAG, "task not started due to config");
} }
} }
static void on_stop(ServiceContext& service) { static void onStop(ServiceContext& service) {
auto* data = static_cast<ServiceData*>(service.getData()); auto data = std::static_pointer_cast<ServiceData>(service.getData());
if (data != nullptr) { if (data != nullptr) {
service_data_lock(data); data->lock();
data->interrupted = true; data->interrupted = true;
service_data_unlock(data); data->unlock();
data->thread->join(); data->thread.join();
service_data_free(data);
} }
} }
extern const ServiceManifest manifest = { extern const ServiceManifest manifest = {
.id = "sdcard", .id = "sdcard",
.onStart = &on_start, .onStart = onStart,
.onStop = &on_stop .onStop = onStop
}; };
} // namespace } // namespace

View File

@ -80,13 +80,13 @@ public:
bool connection_target_remember = false; // Whether to store the connection_target on successful connection or not bool connection_target_remember = false; // Whether to store the connection_target on successful connection or not
}; };
static Wifi* wifi_singleton = nullptr; static std::shared_ptr<Wifi> wifi_singleton;
// Forward declarations // Forward declarations
static void scan_list_free_safely(Wifi* wifi); static void scan_list_free_safely(std::shared_ptr<Wifi> wifi);
static void disconnect_internal_but_keep_active(Wifi* wifi); static void disconnect_internal_but_keep_active(std::shared_ptr<Wifi> wifi);
static void lock(Wifi* wifi); static void lock(std::shared_ptr<Wifi> wifi);
static void unlock(Wifi* wifi); static void unlock(std::shared_ptr<Wifi> wifi);
// region Alloc // region Alloc
@ -253,47 +253,47 @@ int getRssi() {
// endregion Public functions // endregion Public functions
static void lock(Wifi* wifi) { static void lock(std::shared_ptr<Wifi> wifi) {
tt_assert(wifi); tt_assert(wifi);
wifi->mutex.acquire(ms_to_ticks(100)); wifi->mutex.acquire(ms_to_ticks(100));
} }
static void unlock(Wifi* wifi) { static void unlock(std::shared_ptr<Wifi> wifi) {
tt_assert(wifi); tt_assert(wifi);
wifi->mutex.release(); wifi->mutex.release();
} }
static void scan_list_alloc(Wifi* wifi) { static void scan_list_alloc(std::shared_ptr<Wifi> wifi) {
tt_assert(wifi->scan_list == nullptr); tt_assert(wifi->scan_list == nullptr);
wifi->scan_list = static_cast<wifi_ap_record_t*>(malloc(sizeof(wifi_ap_record_t) * wifi->scan_list_limit)); wifi->scan_list = static_cast<wifi_ap_record_t*>(malloc(sizeof(wifi_ap_record_t) * wifi->scan_list_limit));
wifi->scan_list_count = 0; wifi->scan_list_count = 0;
} }
static void scan_list_alloc_safely(Wifi* wifi) { static void scan_list_alloc_safely(std::shared_ptr<Wifi> wifi) {
if (wifi->scan_list == nullptr) { if (wifi->scan_list == nullptr) {
scan_list_alloc(wifi); scan_list_alloc(wifi);
} }
} }
static void scan_list_free(Wifi* wifi) { static void scan_list_free(std::shared_ptr<Wifi> wifi) {
tt_assert(wifi->scan_list != nullptr); tt_assert(wifi->scan_list != nullptr);
free(wifi->scan_list); free(wifi->scan_list);
wifi->scan_list = nullptr; wifi->scan_list = nullptr;
wifi->scan_list_count = 0; wifi->scan_list_count = 0;
} }
static void scan_list_free_safely(Wifi* wifi) { static void scan_list_free_safely(std::shared_ptr<Wifi> wifi) {
if (wifi->scan_list != nullptr) { if (wifi->scan_list != nullptr) {
scan_list_free(wifi); scan_list_free(wifi);
} }
} }
static void publish_event_simple(Wifi* wifi, WifiEventType type) { static void publish_event_simple(std::shared_ptr<Wifi> wifi, WifiEventType type) {
WifiEvent turning_on_event = {.type = type}; WifiEvent turning_on_event = {.type = type};
tt_pubsub_publish(wifi->pubsub, &turning_on_event); tt_pubsub_publish(wifi->pubsub, &turning_on_event);
} }
static bool copy_scan_list(Wifi* wifi) { static bool copy_scan_list(std::shared_ptr<Wifi> wifi) {
bool can_fetch_results = (wifi->radio_state == WIFI_RADIO_ON || wifi->radio_state == WIFI_RADIO_CONNECTION_ACTIVE) && bool can_fetch_results = (wifi->radio_state == WIFI_RADIO_ON || wifi->radio_state == WIFI_RADIO_CONNECTION_ACTIVE) &&
wifi->scan_active; wifi->scan_active;
@ -322,7 +322,7 @@ static bool copy_scan_list(Wifi* wifi) {
} }
} }
static void auto_connect(Wifi* wifi) { static void auto_connect(std::shared_ptr<Wifi> wifi) {
TT_LOG_I(TAG, "auto_connect()"); TT_LOG_I(TAG, "auto_connect()");
for (int i = 0; i < wifi->scan_list_count; ++i) { for (int i = 0; i < wifi->scan_list_count; ++i) {
auto ssid = reinterpret_cast<const char*>(wifi->scan_list[i].ssid); auto ssid = reinterpret_cast<const char*>(wifi->scan_list[i].ssid);
@ -389,7 +389,7 @@ static void event_handler(TT_UNUSED void* arg, esp_event_base_t event_base, int3
unlock(wifi_singleton); unlock(wifi_singleton);
} }
static void enable(Wifi* wifi) { static void enable(std::shared_ptr<Wifi> wifi) {
WifiRadioState state = wifi->radio_state; WifiRadioState state = wifi->radio_state;
if ( if (
state == WIFI_RADIO_ON || state == WIFI_RADIO_ON ||
@ -469,7 +469,7 @@ static void enable(Wifi* wifi) {
TT_LOG_I(TAG, "Enabled"); TT_LOG_I(TAG, "Enabled");
} }
static void disable(Wifi* wifi) { static void disable(std::shared_ptr<Wifi> wifi) {
WifiRadioState state = wifi->radio_state; WifiRadioState state = wifi->radio_state;
if ( if (
state == WIFI_RADIO_OFF || state == WIFI_RADIO_OFF ||
@ -527,7 +527,7 @@ static void disable(Wifi* wifi) {
TT_LOG_I(TAG, "Disabled"); TT_LOG_I(TAG, "Disabled");
} }
static void scan_internal(Wifi* wifi) { static void scan_internal(std::shared_ptr<Wifi> wifi) {
WifiRadioState state = wifi->radio_state; WifiRadioState state = wifi->radio_state;
if (state != WIFI_RADIO_ON && state != WIFI_RADIO_CONNECTION_ACTIVE && state != WIFI_RADIO_CONNECTION_PENDING) { if (state != WIFI_RADIO_ON && state != WIFI_RADIO_CONNECTION_ACTIVE && state != WIFI_RADIO_CONNECTION_PENDING) {
TT_LOG_W(TAG, "Scan unavailable: wifi not enabled"); TT_LOG_W(TAG, "Scan unavailable: wifi not enabled");
@ -548,7 +548,7 @@ static void scan_internal(Wifi* wifi) {
} }
} }
static void connect_internal(Wifi* wifi) { static void connect_internal(std::shared_ptr<Wifi> wifi) {
TT_LOG_I(TAG, "Connecting to %s", wifi->connection_target.ssid); TT_LOG_I(TAG, "Connecting to %s", wifi->connection_target.ssid);
// Stop radio first, if needed // Stop radio first, if needed
@ -668,7 +668,7 @@ static void connect_internal(Wifi* wifi) {
wifi_singleton->connection_wait_flags.clear(WIFI_FAIL_BIT | WIFI_CONNECTED_BIT); wifi_singleton->connection_wait_flags.clear(WIFI_FAIL_BIT | WIFI_CONNECTED_BIT);
} }
static void disconnect_internal_but_keep_active(Wifi* wifi) { static void disconnect_internal_but_keep_active(std::shared_ptr<Wifi> wifi) {
esp_err_t stop_result = esp_wifi_stop(); esp_err_t stop_result = esp_wifi_stop();
if (stop_result != ESP_OK) { if (stop_result != ESP_OK) {
TT_LOG_E(TAG, "Failed to disconnect (%s)", esp_err_to_name(stop_result)); TT_LOG_E(TAG, "Failed to disconnect (%s)", esp_err_to_name(stop_result));
@ -711,7 +711,7 @@ static void disconnect_internal_but_keep_active(Wifi* wifi) {
TT_LOG_I(TAG, "Disconnected"); TT_LOG_I(TAG, "Disconnected");
} }
static bool shouldScanForAutoConnect(Wifi* wifi) { static bool shouldScanForAutoConnect(std::shared_ptr<Wifi> wifi) {
bool is_radio_in_scannable_state = wifi->radio_state == WIFI_RADIO_ON && bool is_radio_in_scannable_state = wifi->radio_state == WIFI_RADIO_ON &&
!wifi->scan_active && !wifi->scan_active &&
!wifi->pause_auto_connect; !wifi->pause_auto_connect;
@ -730,7 +730,7 @@ static bool shouldScanForAutoConnect(Wifi* wifi) {
_Noreturn int32_t wifi_main(TT_UNUSED void* parameter) { _Noreturn int32_t wifi_main(TT_UNUSED void* parameter) {
TT_LOG_I(TAG, "Started main loop"); TT_LOG_I(TAG, "Started main loop");
tt_assert(wifi_singleton != nullptr); tt_assert(wifi_singleton != nullptr);
Wifi* wifi = wifi_singleton; auto wifi = wifi_singleton;
MessageQueue& queue = wifi->queue; MessageQueue& queue = wifi->queue;
if (settings::shouldEnableOnBoot()) { if (settings::shouldEnableOnBoot()) {
@ -791,7 +791,7 @@ _Noreturn int32_t wifi_main(TT_UNUSED void* parameter) {
static void service_start(ServiceContext& service) { static void service_start(ServiceContext& service) {
tt_assert(wifi_singleton == nullptr); tt_assert(wifi_singleton == nullptr);
wifi_singleton = new Wifi(); wifi_singleton = std::make_shared<Wifi>();
service.setData(wifi_singleton); service.setData(wifi_singleton);
} }
@ -803,7 +803,6 @@ static void service_stop(ServiceContext& service) {
disable(wifi_singleton); disable(wifi_singleton);
} }
delete wifi_singleton;
wifi_singleton = nullptr; wifi_singleton = nullptr;
// wifi_main() cannot be stopped yet as it runs in the main task. // wifi_main() cannot be stopped yet as it runs in the main task.