diff --git a/Tactility/Source/app/boot/Boot.cpp b/Tactility/Source/app/boot/Boot.cpp index 532595ef..f0316e39 100644 --- a/Tactility/Source/app/boot/Boot.cpp +++ b/Tactility/Source/app/boot/Boot.cpp @@ -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(new Data()); + auto data = std::make_shared(); app.setData(data); } diff --git a/Tactility/Source/app/files/Files.cpp b/Tactility/Source/app/files/Files.cpp index 9fb056fc..6f0becbc 100644 --- a/Tactility/Source/app/files/Files.cpp +++ b/Tactility/Source/app/files/Files.cpp @@ -12,6 +12,7 @@ #include "lvgl/Toolbar.h" #include #include +#include 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(new Bundle()); + auto bundle = std::make_shared(); bundle->putString(IMAGE_VIEWER_FILE_ARGUMENT, processed_filepath); service::loader::startApp("ImageViewer", false, bundle); } else if (isSupportedTextFile(filename)) { - auto bundle = std::shared_ptr(new Bundle()); + auto bundle = std::make_shared(); 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(new Data()); + auto data = std::make_shared(); // PC platform is bound to current work directory because of the LVGL file system mapping if (get_platform() == PlatformSimulator) { char cwd[PATH_MAX]; diff --git a/Tactility/Source/app/gpio/Gpio.cpp b/Tactility/Source/app/gpio/Gpio.cpp index 75525a37..b60faec8 100644 --- a/Tactility/Source/app/gpio/Gpio.cpp +++ b/Tactility/Source/app/gpio/Gpio.cpp @@ -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(new Gpio()); + auto gpio = std::make_shared(); app.setData(gpio); } diff --git a/Tactility/Source/app/i2cscanner/I2cScanner.cpp b/Tactility/Source/app/i2cscanner/I2cScanner.cpp index be75dfdb..465cae6c 100644 --- a/Tactility/Source/app/i2cscanner/I2cScanner.cpp +++ b/Tactility/Source/app/i2cscanner/I2cScanner.cpp @@ -165,7 +165,7 @@ static void onHide(AppContext& app) { } static void onStart(AppContext& app) { - auto data = std::shared_ptr(new Data()); + auto data = std::make_shared(); app.setData(data); } diff --git a/Tactility/Source/app/power/Power.cpp b/Tactility/Source/app/power/Power.cpp index 9bc24ef7..1168ea41 100644 --- a/Tactility/Source/app/power/Power.cpp +++ b/Tactility/Source/app/power/Power.cpp @@ -119,7 +119,7 @@ static void onHide(TT_UNUSED AppContext& app) { } static void onStart(AppContext& app) { - auto data = std::shared_ptr(new Data()); + auto data = std::shared_ptr(); app.setData(data); data->power = getConfiguration()->hardware->power; assert(data->power != nullptr); // The Power app only shows up on supported devices diff --git a/Tactility/Source/app/selectiondialog/SelectionDialog.cpp b/Tactility/Source/app/selectiondialog/SelectionDialog.cpp index c8edf4a7..b6cf414b 100644 --- a/Tactility/Source/app/selectiondialog/SelectionDialog.cpp +++ b/Tactility/Source/app/selectiondialog/SelectionDialog.cpp @@ -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(new Bundle()); + auto bundle = std::make_shared(); 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(new Bundle()); + auto result_bundle = std::make_shared(); setResultIndex(result_bundle, 0); app.setResult(ResultOk, result_bundle); service::loader::stopApp(); diff --git a/Tactility/Source/app/wifiapsettings/WifiApSettings.cpp b/Tactility/Source/app/wifiapsettings/WifiApSettings.cpp index 142e5c1f..7e992247 100644 --- a/Tactility/Source/app/wifiapsettings/WifiApSettings.cpp +++ b/Tactility/Source/app/wifiapsettings/WifiApSettings.cpp @@ -24,7 +24,7 @@ const AppContext* _Nullable optWifiApSettingsApp() { } void start(const std::string& ssid) { - auto bundle = std::shared_ptr(new Bundle()); + auto bundle = std::make_shared(); bundle->putString("ssid", ssid); service::loader::startApp(manifest.id, false, bundle); } diff --git a/Tactility/Source/app/wificonnect/WifiConnect.cpp b/Tactility/Source/app/wificonnect/WifiConnect.cpp index 03ea1424..71784c89 100644 --- a/Tactility/Source/app/wificonnect/WifiConnect.cpp +++ b/Tactility/Source/app/wificonnect/WifiConnect.cpp @@ -114,7 +114,7 @@ static void onHide(AppContext& app) { } static void onStart(AppContext& app) { - auto wifi = std::shared_ptr(new WifiConnect()); + auto wifi = std::make_shared(); app.setData(wifi); } diff --git a/Tactility/Source/app/wifimanage/WifiManage.cpp b/Tactility/Source/app/wifimanage/WifiManage.cpp index ad964bb3..986bf6f9 100644 --- a/Tactility/Source/app/wifimanage/WifiManage.cpp +++ b/Tactility/Source/app/wifimanage/WifiManage.cpp @@ -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(new Bundle()); + auto bundle = std::make_shared(); 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(new WifiManage()); + auto wifi = std::make_shared(); app.setData(wifi); } diff --git a/Tactility/Source/service/screenshot/Screenshot.cpp b/Tactility/Source/service/screenshot/Screenshot.cpp index 5c13e7b0..a78b4e8d 100644 --- a/Tactility/Source/service/screenshot/Screenshot.cpp +++ b/Tactility/Source/service/screenshot/Screenshot.cpp @@ -1,5 +1,6 @@ #include "Screenshot.h" #include +#include #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(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(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(service->getData()); - service_data_lock(data); + auto data = std::static_pointer_cast(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(service->getData()); - service_data_lock(data); + auto data = std::static_pointer_cast(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(service->getData()); - service_data_lock(data); + auto data = std::static_pointer_cast(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(service->getData()); - service_data_lock(data); + auto data = std::static_pointer_cast(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(); + service.setData(data); +} + extern const ServiceManifest manifest = { .id = "Screenshot", - .onStart = &on_start, - .onStop = &on_stop + .onStart = onStart }; } // namespace diff --git a/Tactility/Source/service/statusbar/Statusbar.cpp b/Tactility/Source/service/statusbar/Statusbar.cpp index f214a75f..d6fad481 100644 --- a/Tactility/Source/service/statusbar/Statusbar.cpp +++ b/Tactility/Source/service/statusbar/Statusbar.cpp @@ -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 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 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 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(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(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(); 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(service.getData()); +static void onStop(ServiceContext& service) { + auto data = std::static_pointer_cast(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 diff --git a/TactilityHeadless/Private/service/ServiceInstance.h b/TactilityHeadless/Private/service/ServiceInstance.h index c165f946..142c9e79 100644 --- a/TactilityHeadless/Private/service/ServiceInstance.h +++ b/TactilityHeadless/Private/service/ServiceInstance.h @@ -10,7 +10,7 @@ private: Mutex mutex = Mutex(MutexTypeNormal); const service::ServiceManifest& manifest; - void* data = nullptr; + std::shared_ptr data = nullptr; public: @@ -18,8 +18,8 @@ public: ~ServiceInstance() override = default; const service::ServiceManifest& getManifest() const override; - void* getData() const override; - void setData(void* newData) override; + std::shared_ptr getData() const override; + void setData(std::shared_ptr newData) override; }; } diff --git a/TactilityHeadless/Source/service/ServiceContext.h b/TactilityHeadless/Source/service/ServiceContext.h index a5ec461d..9473d800 100644 --- a/TactilityHeadless/Source/service/ServiceContext.h +++ b/TactilityHeadless/Source/service/ServiceContext.h @@ -2,6 +2,7 @@ #include "Mutex.h" #include "ServiceManifest.h" +#include namespace tt::service { @@ -14,8 +15,8 @@ protected: public: virtual const service::ServiceManifest& getManifest() const = 0; - virtual void* getData() const = 0; - virtual void setData(void* newData) = 0; + virtual std::shared_ptr getData() const = 0; + virtual void setData(std::shared_ptr newData) = 0; }; } // namespace diff --git a/TactilityHeadless/Source/service/ServiceInstance.cpp b/TactilityHeadless/Source/service/ServiceInstance.cpp index ce22ad32..ad992cef 100644 --- a/TactilityHeadless/Source/service/ServiceInstance.cpp +++ b/TactilityHeadless/Source/service/ServiceInstance.cpp @@ -1,3 +1,5 @@ +#include + #include "service/ServiceInstance.h" namespace tt::service { @@ -6,16 +8,16 @@ ServiceInstance::ServiceInstance(const service::ServiceManifest&manifest) : mani const service::ServiceManifest& ServiceInstance::getManifest() const { return manifest; } -void* ServiceInstance::getData() const { +std::shared_ptr ServiceInstance::getData() const { mutex.acquire(TtWaitForever); - void* data_copy = data; + std::shared_ptr result = data; mutex.release(); - return data_copy; + return result; } -void ServiceInstance::setData(void* newData) { +void ServiceInstance::setData(std::shared_ptr newData) { mutex.acquire(TtWaitForever); - data = newData; + data = std::move(newData); mutex.release(); } diff --git a/TactilityHeadless/Source/service/sdcard/Sdcard.cpp b/TactilityHeadless/Source/service/sdcard/Sdcard.cpp index 826c4f0a..5d4ad9e9 100644 --- a/TactilityHeadless/Source/service/sdcard/Sdcard.cpp +++ b/TactilityHeadless/Source/service/sdcard/Sdcard.cpp @@ -4,56 +4,54 @@ #include "service/ServiceContext.h" #include "TactilityCore.h" #include "TactilityHeadless.h" +#include "service/ServiceRegistry.h" #define TAG "sdcard_service" namespace tt::service::sdcard { static int32_t sdcard_task(void* context); +extern const ServiceManifest manifest; -typedef struct { - Mutex* mutex; - Thread* thread; - hal::sdcard::State lastState; - bool interrupted; -} ServiceData; +struct ServiceData { + Mutex mutex; + Thread thread = Thread( + "sdcard", + 3000, // Minimum is ~2800 @ ESP-IDF 5.1.2 when ejecting sdcard + &sdcard_task, + nullptr + ); + hal::sdcard::State lastState = hal::sdcard::StateUnmounted; + bool interrupted = false; -static ServiceData* service_data_alloc() { - auto* data = static_cast(malloc(sizeof(ServiceData))); - *data = (ServiceData) { - .mutex = tt_mutex_alloc(MutexTypeNormal), - .thread = new Thread( - "sdcard", - 3000, // Minimum is ~2800 @ ESP-IDF 5.1.2 when ejecting sdcard - &sdcard_task, - data - ), - .lastState = hal::sdcard::StateUnmounted, - .interrupted = false - }; - data->thread->setPriority(Thread::PriorityLow); - return data; -} + ServiceData() { + thread.setPriority(Thread::PriorityLow); + } -static void service_data_free(ServiceData* data) { - tt_mutex_free(data->mutex); - delete data->thread; -} + void lock() const { + tt_check(mutex.acquire(TtWaitForever) == TtStatusOk); + } -static void service_data_lock(ServiceData* data) { - tt_check(tt_mutex_acquire(data->mutex, TtWaitForever) == TtStatusOk); -} + void unlock() const { + tt_check(mutex.release() == TtStatusOk); + } +}; -static void service_data_unlock(ServiceData* data) { - tt_check(tt_mutex_release(data->mutex) == TtStatusOk); -} -static int32_t sdcard_task(void* context) { - auto* data = (ServiceData*)context; +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(service->getData()); + bool interrupted = false; do { - service_data_lock(data); + data->lock(); interrupted = data->interrupted; @@ -68,40 +66,38 @@ static int32_t sdcard_task(void* context) { data->lastState = new_state; } - service_data_unlock(data); + data->lock(); delay_ms(2000); } while (!interrupted); return 0; } -static void on_start(ServiceContext& service) { +static void onStart(ServiceContext& service) { if (hal::getConfiguration().sdcard != nullptr) { - ServiceData* data = service_data_alloc(); + auto data = std::make_shared(); service.setData(data); - data->thread->start(); + data->thread.start(); } else { TT_LOG_I(TAG, "task not started due to config"); } } -static void on_stop(ServiceContext& service) { - auto* data = static_cast(service.getData()); +static void onStop(ServiceContext& service) { + auto data = std::static_pointer_cast(service.getData()); if (data != nullptr) { - service_data_lock(data); + data->lock(); data->interrupted = true; - service_data_unlock(data); + data->unlock(); - data->thread->join(); - - service_data_free(data); + data->thread.join(); } } extern const ServiceManifest manifest = { .id = "sdcard", - .onStart = &on_start, - .onStop = &on_stop + .onStart = onStart, + .onStop = onStop }; } // namespace diff --git a/TactilityHeadless/Source/service/wifi/WifiEsp.cpp b/TactilityHeadless/Source/service/wifi/WifiEsp.cpp index 264578dc..9b7806df 100644 --- a/TactilityHeadless/Source/service/wifi/WifiEsp.cpp +++ b/TactilityHeadless/Source/service/wifi/WifiEsp.cpp @@ -80,13 +80,13 @@ public: 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_singleton; // Forward declarations -static void scan_list_free_safely(Wifi* wifi); -static void disconnect_internal_but_keep_active(Wifi* wifi); -static void lock(Wifi* wifi); -static void unlock(Wifi* wifi); +static void scan_list_free_safely(std::shared_ptr wifi); +static void disconnect_internal_but_keep_active(std::shared_ptr wifi); +static void lock(std::shared_ptr wifi); +static void unlock(std::shared_ptr wifi); // region Alloc @@ -253,47 +253,47 @@ int getRssi() { // endregion Public functions -static void lock(Wifi* wifi) { +static void lock(std::shared_ptr wifi) { tt_assert(wifi); wifi->mutex.acquire(ms_to_ticks(100)); } -static void unlock(Wifi* wifi) { +static void unlock(std::shared_ptr wifi) { tt_assert(wifi); wifi->mutex.release(); } -static void scan_list_alloc(Wifi* wifi) { +static void scan_list_alloc(std::shared_ptr wifi) { tt_assert(wifi->scan_list == nullptr); wifi->scan_list = static_cast(malloc(sizeof(wifi_ap_record_t) * wifi->scan_list_limit)); wifi->scan_list_count = 0; } -static void scan_list_alloc_safely(Wifi* wifi) { +static void scan_list_alloc_safely(std::shared_ptr wifi) { if (wifi->scan_list == nullptr) { scan_list_alloc(wifi); } } -static void scan_list_free(Wifi* wifi) { +static void scan_list_free(std::shared_ptr wifi) { tt_assert(wifi->scan_list != nullptr); free(wifi->scan_list); wifi->scan_list = nullptr; wifi->scan_list_count = 0; } -static void scan_list_free_safely(Wifi* wifi) { +static void scan_list_free_safely(std::shared_ptr wifi) { if (wifi->scan_list != nullptr) { scan_list_free(wifi); } } -static void publish_event_simple(Wifi* wifi, WifiEventType type) { +static void publish_event_simple(std::shared_ptr wifi, WifiEventType type) { WifiEvent turning_on_event = {.type = type}; tt_pubsub_publish(wifi->pubsub, &turning_on_event); } -static bool copy_scan_list(Wifi* wifi) { +static bool copy_scan_list(std::shared_ptr wifi) { bool can_fetch_results = (wifi->radio_state == WIFI_RADIO_ON || wifi->radio_state == WIFI_RADIO_CONNECTION_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) { TT_LOG_I(TAG, "auto_connect()"); for (int i = 0; i < wifi->scan_list_count; ++i) { auto ssid = reinterpret_cast(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); } -static void enable(Wifi* wifi) { +static void enable(std::shared_ptr wifi) { WifiRadioState state = wifi->radio_state; if ( state == WIFI_RADIO_ON || @@ -469,7 +469,7 @@ static void enable(Wifi* wifi) { TT_LOG_I(TAG, "Enabled"); } -static void disable(Wifi* wifi) { +static void disable(std::shared_ptr wifi) { WifiRadioState state = wifi->radio_state; if ( state == WIFI_RADIO_OFF || @@ -527,7 +527,7 @@ static void disable(Wifi* wifi) { TT_LOG_I(TAG, "Disabled"); } -static void scan_internal(Wifi* wifi) { +static void scan_internal(std::shared_ptr wifi) { WifiRadioState state = wifi->radio_state; if (state != WIFI_RADIO_ON && state != WIFI_RADIO_CONNECTION_ACTIVE && state != WIFI_RADIO_CONNECTION_PENDING) { 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) { TT_LOG_I(TAG, "Connecting to %s", wifi->connection_target.ssid); // 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); } -static void disconnect_internal_but_keep_active(Wifi* wifi) { +static void disconnect_internal_but_keep_active(std::shared_ptr wifi) { esp_err_t stop_result = esp_wifi_stop(); if (stop_result != ESP_OK) { 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"); } -static bool shouldScanForAutoConnect(Wifi* wifi) { +static bool shouldScanForAutoConnect(std::shared_ptr wifi) { bool is_radio_in_scannable_state = wifi->radio_state == WIFI_RADIO_ON && !wifi->scan_active && !wifi->pause_auto_connect; @@ -730,7 +730,7 @@ static bool shouldScanForAutoConnect(Wifi* wifi) { _Noreturn int32_t wifi_main(TT_UNUSED void* parameter) { TT_LOG_I(TAG, "Started main loop"); tt_assert(wifi_singleton != nullptr); - Wifi* wifi = wifi_singleton; + auto wifi = wifi_singleton; MessageQueue& queue = wifi->queue; if (settings::shouldEnableOnBoot()) { @@ -791,7 +791,7 @@ _Noreturn int32_t wifi_main(TT_UNUSED void* parameter) { static void service_start(ServiceContext& service) { tt_assert(wifi_singleton == nullptr); - wifi_singleton = new Wifi(); + wifi_singleton = std::make_shared(); service.setData(wifi_singleton); } @@ -803,7 +803,6 @@ static void service_stop(ServiceContext& service) { disable(wifi_singleton); } - delete wifi_singleton; wifi_singleton = nullptr; // wifi_main() cannot be stopped yet as it runs in the main task.