diff --git a/Boards/CYD-2432S024C/Source/CYD2432S024C.cpp b/Boards/CYD-2432S024C/Source/CYD2432S024C.cpp index de9700c9..d9455601 100644 --- a/Boards/CYD-2432S024C/Source/CYD2432S024C.cpp +++ b/Boards/CYD-2432S024C/Source/CYD2432S024C.cpp @@ -73,7 +73,7 @@ const tt::hal::Configuration cyd_2432S024c_config = { .initMode = tt::hal::spi::InitMode::ByTactility, .canReinit = false, .hasMutableConfiguration = false, - .lock = tt::lvgl::getLvglSyncLockable() // esp_lvgl_port owns the lock for the display + .lock = tt::lvgl::getLvglSyncLock() // esp_lvgl_port owns the lock for the display }, tt::hal::spi::Configuration { .device = SPI3_HOST, diff --git a/Boards/LilygoTdeck/Source/LilygoTdeck.cpp b/Boards/LilygoTdeck/Source/LilygoTdeck.cpp index 152e9591..e1e57bfd 100644 --- a/Boards/LilygoTdeck/Source/LilygoTdeck.cpp +++ b/Boards/LilygoTdeck/Source/LilygoTdeck.cpp @@ -78,7 +78,7 @@ extern const tt::hal::Configuration lilygo_tdeck = { .initMode = tt::hal::spi::InitMode::ByTactility, .canReinit = false, .hasMutableConfiguration = false, - .lock = tt::lvgl::getLvglSyncLockable() // esp_lvgl_port owns the lock for the display + .lock = tt::lvgl::getLvglSyncLock() // esp_lvgl_port owns the lock for the display } }, .uart { diff --git a/Boards/LilygoTdeck/Source/hal/TdeckSdCard.cpp b/Boards/LilygoTdeck/Source/hal/TdeckSdCard.cpp index 1f24bd97..ff3eae97 100644 --- a/Boards/LilygoTdeck/Source/hal/TdeckSdCard.cpp +++ b/Boards/LilygoTdeck/Source/hal/TdeckSdCard.cpp @@ -18,7 +18,7 @@ std::shared_ptr createTdeckSdCard() { GPIO_NUM_NC, GPIO_NUM_NC, SdCardDevice::MountBehaviour::AtBoot, - tt::lvgl::getLvglSyncLockable(), + tt::lvgl::getLvglSyncLock(), { TDECK_RADIO_PIN_CS, TDECK_LCD_PIN_CS diff --git a/Boards/M5stackCore2/Source/M5stackCore2.cpp b/Boards/M5stackCore2/Source/M5stackCore2.cpp index 5408da21..1cdd901a 100644 --- a/Boards/M5stackCore2/Source/M5stackCore2.cpp +++ b/Boards/M5stackCore2/Source/M5stackCore2.cpp @@ -75,7 +75,7 @@ extern const tt::hal::Configuration m5stack_core2 = { .initMode = tt::hal::spi::InitMode::ByTactility, .canReinit = false, .hasMutableConfiguration = false, - .lock = tt::lvgl::getLvglSyncLockable() // esp_lvgl_port owns the lock for the display + .lock = tt::lvgl::getLvglSyncLock() // esp_lvgl_port owns the lock for the display } } }; diff --git a/Boards/M5stackCore2/Source/hal/Core2SdCard.cpp b/Boards/M5stackCore2/Source/hal/Core2SdCard.cpp index 075c2494..85c6f262 100644 --- a/Boards/M5stackCore2/Source/hal/Core2SdCard.cpp +++ b/Boards/M5stackCore2/Source/hal/Core2SdCard.cpp @@ -17,7 +17,7 @@ std::shared_ptr createSdCard() { GPIO_NUM_NC, GPIO_NUM_NC, SdCardDevice::MountBehaviour::AtBoot, - tt::lvgl::getLvglSyncLockable(), + tt::lvgl::getLvglSyncLock(), { CORE2_LCD_PIN_CS } diff --git a/Boards/M5stackCoreS3/Source/M5stackCoreS3.cpp b/Boards/M5stackCoreS3/Source/M5stackCoreS3.cpp index 740e071f..2335fe38 100644 --- a/Boards/M5stackCoreS3/Source/M5stackCoreS3.cpp +++ b/Boards/M5stackCoreS3/Source/M5stackCoreS3.cpp @@ -74,7 +74,7 @@ const tt::hal::Configuration m5stack_cores3 = { .initMode = tt::hal::spi::InitMode::ByTactility, .canReinit = false, .hasMutableConfiguration = false, - .lock = tt::lvgl::getLvglSyncLockable() // esp_lvgl_port owns the lock for the display + .lock = tt::lvgl::getLvglSyncLock() // esp_lvgl_port owns the lock for the display } } }; diff --git a/Boards/M5stackCoreS3/Source/hal/CoreS3SdCard.cpp b/Boards/M5stackCoreS3/Source/hal/CoreS3SdCard.cpp index cca72682..805ebcb1 100644 --- a/Boards/M5stackCoreS3/Source/hal/CoreS3SdCard.cpp +++ b/Boards/M5stackCoreS3/Source/hal/CoreS3SdCard.cpp @@ -17,7 +17,7 @@ std::shared_ptr createSdCard() { GPIO_NUM_NC, GPIO_NUM_NC, SdCardDevice::MountBehaviour::AtBoot, - tt::lvgl::getLvglSyncLockable(), + tt::lvgl::getLvglSyncLock(), { CORES3_LCD_PIN_CS }, diff --git a/Boards/Simulator/Source/hal/SimulatorSdCard.h b/Boards/Simulator/Source/hal/SimulatorSdCard.h index 528aecf0..246a09fa 100644 --- a/Boards/Simulator/Source/hal/SimulatorSdCard.h +++ b/Boards/Simulator/Source/hal/SimulatorSdCard.h @@ -11,14 +11,14 @@ class SimulatorSdCard final : public SdCardDevice { private: State state; - std::shared_ptr lockable; + std::shared_ptr lock; std::string mountPath; public: SimulatorSdCard() : SdCardDevice(MountBehaviour::AtBoot), state(State::Unmounted), - lockable(std::make_shared()) + lock(std::make_shared()) {} std::string getName() const final { return "Mock SD Card"; } @@ -38,8 +38,7 @@ public: std::string getMountPath() const final { return mountPath; }; - std::shared_ptr getLockable() const final { return lockable; } + tt::Lock& getLock() const final { return *lock; } State getState() const override { return state; } }; - diff --git a/Boards/UnPhone/Source/UnPhone.cpp b/Boards/UnPhone/Source/UnPhone.cpp index 5d30b830..70797742 100644 --- a/Boards/UnPhone/Source/UnPhone.cpp +++ b/Boards/UnPhone/Source/UnPhone.cpp @@ -76,7 +76,7 @@ extern const tt::hal::Configuration unPhone = { .initMode = tt::hal::spi::InitMode::ByTactility, .canReinit = false, .hasMutableConfiguration = false, - .lock = tt::lvgl::getLvglSyncLockable() // esp_lvgl_port owns the lock for the display + .lock = tt::lvgl::getLvglSyncLock() // esp_lvgl_port owns the lock for the display } } }; diff --git a/Boards/UnPhone/Source/hal/UnPhoneSdCard.cpp b/Boards/UnPhone/Source/hal/UnPhoneSdCard.cpp index 47dc57d5..2cab6436 100644 --- a/Boards/UnPhone/Source/hal/UnPhoneSdCard.cpp +++ b/Boards/UnPhone/Source/hal/UnPhoneSdCard.cpp @@ -19,7 +19,7 @@ std::shared_ptr createUnPhoneSdCard() { GPIO_NUM_NC, GPIO_NUM_NC, SdCardDevice::MountBehaviour::AtBoot, - tt::lvgl::getLvglSyncLockable(), + tt::lvgl::getLvglSyncLock(), { UNPHONE_LORA_PIN_CS, UNPHONE_LCD_PIN_CS, diff --git a/Tactility/Include/Tactility/app/App.h b/Tactility/Include/Tactility/app/App.h index 0fd2530e..5135b485 100644 --- a/Tactility/Include/Tactility/app/App.h +++ b/Tactility/Include/Tactility/app/App.h @@ -51,8 +51,8 @@ public: bool hasResult() const { return resultHolder != nullptr; } void setResult(Result result, std::unique_ptr resultData = nullptr) { - auto lockable = getMutex().scoped(); - lockable->lock(portMAX_DELAY); + auto lock = getMutex().asScopedLock(); + lock.lock(); resultHolder = std::make_unique(result, std::move(resultData)); } @@ -61,8 +61,8 @@ public: * Note that this removes the data from the class! */ bool moveResult(Result& outResult, std::unique_ptr& outBundle) { - auto lockable = getMutex().scoped(); - lockable->lock(portMAX_DELAY); + auto lock = getMutex().asScopedLock(); + lock.lock(); if (resultHolder != nullptr) { outResult = resultHolder->result; diff --git a/Tactility/Include/Tactility/lvgl/LvglSync.h b/Tactility/Include/Tactility/lvgl/LvglSync.h index b29a637b..4c6895d8 100644 --- a/Tactility/Include/Tactility/lvgl/LvglSync.h +++ b/Tactility/Include/Tactility/lvgl/LvglSync.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include @@ -25,6 +25,6 @@ void syncSet(LvglLock lock, LvglUnlock unlock); bool lock(TickType_t timeout); void unlock(); -std::shared_ptr getLvglSyncLockable(); +std::shared_ptr getLvglSyncLock(); } // namespace diff --git a/Tactility/Source/app/files/State.cpp b/Tactility/Source/app/files/State.cpp index 58fb6d0a..00575b4f 100644 --- a/Tactility/Source/app/files/State.cpp +++ b/Tactility/Source/app/files/State.cpp @@ -32,8 +32,8 @@ std::string State::getSelectedChildPath() const { } bool State::setEntriesForPath(const std::string& path) { - auto scoped_lock = mutex.scoped(); - if (!scoped_lock->lock(100)) { + auto lock = mutex.asScopedLock(); + if (!lock.lock(100)) { TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "setEntriesForPath"); return false; } @@ -102,8 +102,8 @@ bool State::setEntriesForChildPath(const std::string& child_path) { } bool State::getDirent(uint32_t index, dirent& dirent) { - auto scoped_mutex = mutex.scoped(); - if (!scoped_mutex->lock(50 / portTICK_PERIOD_MS)) { + auto lock = mutex.asScopedLock(); + if (!lock.lock(50 / portTICK_PERIOD_MS)) { return false; } diff --git a/Tactility/Source/app/files/View.cpp b/Tactility/Source/app/files/View.cpp index 2b9781ec..e97b0f75 100644 --- a/Tactility/Source/app/files/View.cpp +++ b/Tactility/Source/app/files/View.cpp @@ -228,7 +228,7 @@ void View::showActionsForFile() { } void View::update() { - auto scoped_lockable = lvgl::getLvglSyncLockable()->scoped(); + auto scoped_lockable = lvgl::getLvglSyncLock()->scoped(); if (scoped_lockable->lock(100 / portTICK_PERIOD_MS)) { lv_obj_clean(dir_entry_list); @@ -277,14 +277,14 @@ void View::init(lv_obj_t* parent) { } void View::onDirEntryListScrollBegin() { - auto scoped_lockable = lvgl::getLvglSyncLockable()->scoped(); + auto scoped_lockable = lvgl::getLvglSyncLock()->scoped(); if (scoped_lockable->lock(100 / portTICK_PERIOD_MS)) { lv_obj_add_flag(action_list, LV_OBJ_FLAG_HIDDEN); } } void View::onNavigate() { - auto scoped_lockable = lvgl::getLvglSyncLockable()->scoped(); + auto scoped_lockable = lvgl::getLvglSyncLock()->scoped(); if (scoped_lockable->lock(100 / portTICK_PERIOD_MS)) { lv_obj_add_flag(action_list, LV_OBJ_FLAG_HIDDEN); } diff --git a/Tactility/Source/app/gpio/Gpio.cpp b/Tactility/Source/app/gpio/Gpio.cpp index 9adf4489..2d08f60b 100644 --- a/Tactility/Source/app/gpio/Gpio.cpp +++ b/Tactility/Source/app/gpio/Gpio.cpp @@ -49,9 +49,9 @@ void GpioApp::updatePinStates() { } void GpioApp::updatePinWidgets() { - auto scoped_lvgl_lock = lvgl::getLvglSyncLockable()->scoped(); - auto scoped_gpio_lock = mutex.scoped(); - if (scoped_gpio_lock->lock() && scoped_lvgl_lock->lock(100)) { + auto scoped_lvgl_lock = lvgl::getLvglSyncLock()->scoped(); + auto scoped_gpio_lock = mutex.asScopedLock(); + if (scoped_gpio_lock.lock() && scoped_lvgl_lock->lock(100)) { for (int j = 0; j < GPIO_NUM_MAX; ++j) { int level = pinStates[j]; lv_obj_t* label = lvPins[j]; diff --git a/Tactility/Source/app/screenshot/Screenshot.cpp b/Tactility/Source/app/screenshot/Screenshot.cpp index 61780a43..29ac2c6d 100644 --- a/Tactility/Source/app/screenshot/Screenshot.cpp +++ b/Tactility/Source/app/screenshot/Screenshot.cpp @@ -90,7 +90,7 @@ ScreenshotApp::~ScreenshotApp() { } void ScreenshotApp::onTimerTick() { - auto lvgl_lock = lvgl::getLvglSyncLockable()->scoped(); + auto lvgl_lock = lvgl::getLvglSyncLock()->scoped(); if (lvgl_lock->lock(50 / portTICK_PERIOD_MS)) { updateScreenshotMode(); } diff --git a/Tactility/Source/lvgl/LvglSync.cpp b/Tactility/Source/lvgl/LvglSync.cpp index 1cdafab7..62327b74 100644 --- a/Tactility/Source/lvgl/LvglSync.cpp +++ b/Tactility/Source/lvgl/LvglSync.cpp @@ -36,7 +36,7 @@ void unlock() { unlock_singleton(); } -class LvglSync : public Lockable { +class LvglSync : public Lock { public: ~LvglSync() override = default; @@ -50,9 +50,9 @@ public: } }; -static std::shared_ptr lvglSync = std::make_shared(); +static std::shared_ptr lvglSync = std::make_shared(); -std::shared_ptr getLvglSyncLockable() { +std::shared_ptr getLvglSyncLock() { return lvglSync; } diff --git a/Tactility/Source/service/loader/Loader.cpp b/Tactility/Source/service/loader/Loader.cpp index 9eb08121..64a67c5a 100644 --- a/Tactility/Source/service/loader/Loader.cpp +++ b/Tactility/Source/service/loader/Loader.cpp @@ -147,8 +147,8 @@ static LoaderStatus startAppWithManifestInternal( TT_LOG_I(TAG, "Start with manifest %s", manifest->id.c_str()); - auto scoped_lock = loader_singleton->mutex.scoped(); - if (!scoped_lock->lock(50 / portTICK_PERIOD_MS)) { + auto lock = loader_singleton->mutex.asScopedLock(); + if (!lock.lock(50 / portTICK_PERIOD_MS)) { return LoaderStatus::ErrorInternal; } @@ -202,8 +202,8 @@ static LoaderStatus startAppInternal( static void stopAppInternal() { tt_check(loader_singleton != nullptr); - auto scoped_lock = loader_singleton->mutex.scoped(); - if (!scoped_lock->lock(50 / portTICK_PERIOD_MS)) { + auto lock = loader_singleton->mutex.asScopedLock(); + if (!lock.lock(50 / portTICK_PERIOD_MS)) { return; } @@ -257,7 +257,7 @@ static void stopAppInternal() { } // Unlock so that we can send results to app and they can also start/stop new apps while processing these results - scoped_lock->unlock(); + lock.unlock(); // WARNING: After this point we cannot change the app states from this method directly anymore as we don't have a lock! LoaderEvent event_external = { .type = LoaderEventTypeApplicationStopped }; diff --git a/Tactility/Source/service/screenshot/Screenshot.cpp b/Tactility/Source/service/screenshot/Screenshot.cpp index 08753a7b..368c2da6 100644 --- a/Tactility/Source/service/screenshot/Screenshot.cpp +++ b/Tactility/Source/service/screenshot/Screenshot.cpp @@ -20,8 +20,8 @@ std::shared_ptr _Nullable optScreenshotService() { } void ScreenshotService::startApps(const std::string& path) { - auto scoped_lockable = mutex.scoped(); - if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) { + auto lock = mutex.asScopedLock(); + if (!lock.lock(50 / portTICK_PERIOD_MS)) { TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); return; } @@ -36,8 +36,8 @@ void ScreenshotService::startApps(const std::string& path) { } void ScreenshotService::startTimed(const std::string& path, uint8_t delayInSeconds, uint8_t amount) { - auto scoped_lockable = mutex.scoped(); - if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) { + auto lock = mutex.asScopedLock(); + if (!lock.lock(50 / portTICK_PERIOD_MS)) { TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); return; } @@ -52,8 +52,8 @@ void ScreenshotService::startTimed(const std::string& path, uint8_t delayInSecon } void ScreenshotService::stop() { - auto scoped_lockable = mutex.scoped(); - if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) { + auto lock = mutex.asScopedLock(); + if (!lock.lock(50 / portTICK_PERIOD_MS)) { TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); return; } @@ -67,8 +67,8 @@ void ScreenshotService::stop() { } Mode ScreenshotService::getMode() const { - auto scoped_lockable = mutex.scoped(); - if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) { + auto lock = mutex.asScopedLock(); + if (!lock.lock(50 / portTICK_PERIOD_MS)) { TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); return Mode::None; } diff --git a/Tactility/Source/service/screenshot/ScreenshotTask.cpp b/Tactility/Source/service/screenshot/ScreenshotTask.cpp index a2d93bda..6327bbdf 100644 --- a/Tactility/Source/service/screenshot/ScreenshotTask.cpp +++ b/Tactility/Source/service/screenshot/ScreenshotTask.cpp @@ -23,8 +23,8 @@ ScreenshotTask::~ScreenshotTask() { } bool ScreenshotTask::isInterrupted() { - auto scoped_lockable = mutex.scoped(); - if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) { + auto lock = mutex.asScopedLock(); + if (!lock.lock(50 / portTICK_PERIOD_MS)) { TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); return true; } @@ -32,8 +32,8 @@ bool ScreenshotTask::isInterrupted() { } bool ScreenshotTask::isFinished() { - auto scoped_lockable = mutex.scoped(); - if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) { + auto lock = mutex.asScopedLock(); + if (!lock.lock(50 / portTICK_PERIOD_MS)) { TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); return false; } @@ -41,8 +41,8 @@ bool ScreenshotTask::isFinished() { } void ScreenshotTask::setFinished() { - auto scoped_lockable = mutex.scoped(); - scoped_lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(); finished = true; } @@ -106,8 +106,8 @@ void ScreenshotTask::taskMain() { } void ScreenshotTask::taskStart() { - auto scoped_lockable = mutex.scoped(); - if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) { + auto lock = mutex.asScopedLock(); + if (!lock.lock(50 / portTICK_PERIOD_MS)) { TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); return; } @@ -123,8 +123,8 @@ void ScreenshotTask::taskStart() { } void ScreenshotTask::startApps(const std::string& path) { - auto scoped_lockable = mutex.scoped(); - if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) { + auto lock = mutex.asScopedLock(); + if (!lock.lock(50 / portTICK_PERIOD_MS)) { TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); return; } @@ -140,8 +140,8 @@ void ScreenshotTask::startApps(const std::string& path) { } void ScreenshotTask::startTimed(const std::string& path, uint8_t delay_in_seconds, uint8_t amount) { - auto scoped_lockable = mutex.scoped(); - if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) { + auto lock = mutex.asScopedLock(); + if (!lock.lock(50 / portTICK_PERIOD_MS)) { TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); return; } diff --git a/TactilityCore/Include/Tactility/Lockable.h b/TactilityCore/Include/Tactility/Lock.h similarity index 76% rename from TactilityCore/Include/Tactility/Lockable.h rename to TactilityCore/Include/Tactility/Lock.h index 033e61a9..d4988996 100644 --- a/TactilityCore/Include/Tactility/Lockable.h +++ b/TactilityCore/Include/Tactility/Lock.h @@ -8,14 +8,14 @@ namespace tt { -class ScopedLockableUsage; +class ScopedLock; /** Represents a lock/mutex */ -class Lockable { +class Lock { public: - virtual ~Lockable() = default; + virtual ~Lock() = default; virtual bool lock(TickType_t timeout) const = 0; @@ -44,30 +44,29 @@ public: void withLock(const std::function& onLockAcquired, const std::function& onLockFailed) const { withLock(portMAX_DELAY, onLockAcquired, onLockFailed); } [[deprecated("use asScopedLock()")]] - std::unique_ptr scoped() const; + std::unique_ptr scoped() const; - ScopedLockableUsage asScopedLock() const; + ScopedLock asScopedLock() const; }; - /** * Represents a lockable instance that is scoped to a specific lifecycle. - * Once the ScopedLockableUsage is destroyed, unlock() is called automatically. + * Once the ScopedLock is destroyed, unlock() is called automatically. * * In other words: * You have to lock() this object manually, but unlock() happens automatically on destruction. */ -class ScopedLockableUsage final : public Lockable { +class ScopedLock final : public Lock { - const Lockable& lockable; + const Lock& lockable; public: - using Lockable::lock; + using Lock::lock; - explicit ScopedLockableUsage(const Lockable& lockable) : lockable(lockable) {} + explicit ScopedLock(const Lock& lockable) : lockable(lockable) {} - ~ScopedLockableUsage() final { + ~ScopedLock() final { lockable.unlock(); // We don't care whether it succeeded or not } diff --git a/TactilityCore/Include/Tactility/Mutex.h b/TactilityCore/Include/Tactility/Mutex.h index 82b09c8a..03b204f9 100644 --- a/TactilityCore/Include/Tactility/Mutex.h +++ b/TactilityCore/Include/Tactility/Mutex.h @@ -4,10 +4,10 @@ */ #pragma once -#include "Thread.h" -#include "RtosCompatSemaphore.h" #include "Check.h" -#include "Lockable.h" +#include "Lock.h" +#include "RtosCompatSemaphore.h" +#include "Thread.h" #include "kernel/Kernel.h" #include @@ -17,7 +17,7 @@ namespace tt { * Wrapper for FreeRTOS xSemaphoreCreateMutex and xSemaphoreCreateRecursiveMutex * Cannot be used in IRQ mode (within ISR context) */ -class Mutex final : public Lockable { +class Mutex final : public Lock { public: @@ -40,7 +40,7 @@ private: public: - using Lockable::lock; + using Lock::lock; explicit Mutex(Type type = Type::Normal); ~Mutex() final = default; diff --git a/TactilityCore/Include/Tactility/Semaphore.h b/TactilityCore/Include/Tactility/Semaphore.h index db37a123..788e2fea 100644 --- a/TactilityCore/Include/Tactility/Semaphore.h +++ b/TactilityCore/Include/Tactility/Semaphore.h @@ -1,7 +1,7 @@ #pragma once +#include "Lock.h" #include "kernel/Kernel.h" -#include "Lockable.h" #include #include @@ -19,7 +19,7 @@ namespace tt { * Wrapper for xSemaphoreCreateBinary (max count == 1) and xSemaphoreCreateCounting (max count > 1) * Can be used from IRQ/ISR mode, but cannot be created/destroyed from such a context. */ -class Semaphore final : public Lockable { +class Semaphore final : public Lock { private: @@ -34,7 +34,7 @@ private: public: - using Lockable::lock; + using Lock::lock; /** * Cannot be called from IRQ/ISR mode. diff --git a/TactilityCore/Source/Lock.cpp b/TactilityCore/Source/Lock.cpp new file mode 100644 index 00000000..afd38a1a --- /dev/null +++ b/TactilityCore/Source/Lock.cpp @@ -0,0 +1,13 @@ +#include "Tactility/Lock.h" + +namespace tt { + +std::unique_ptr Lock::scoped() const { + return std::make_unique(*this); +} + +ScopedLock Lock::asScopedLock() const { + return ScopedLock(*this); +} + +} diff --git a/TactilityCore/Source/Lockable.cpp b/TactilityCore/Source/Lockable.cpp deleted file mode 100644 index 2ec1c207..00000000 --- a/TactilityCore/Source/Lockable.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "Tactility/Lockable.h" - -namespace tt { - -std::unique_ptr Lockable::scoped() const { - return std::make_unique(*this); -} - -ScopedLockableUsage Lockable::asScopedLock() const { - return ScopedLockableUsage(*this); -} - -} diff --git a/TactilityHeadless/Include/Tactility/hal/i2c/I2c.h b/TactilityHeadless/Include/Tactility/hal/i2c/I2c.h index 91346112..a9f01d5e 100644 --- a/TactilityHeadless/Include/Tactility/hal/i2c/I2c.h +++ b/TactilityHeadless/Include/Tactility/hal/i2c/I2c.h @@ -1,7 +1,7 @@ #pragma once #include "./I2cCompat.h" -#include "Tactility/Lockable.h" +#include "Tactility/Lock.h" #include @@ -89,6 +89,6 @@ bool masterHasDeviceAtAddress(i2c_port_t port, uint8_t address, TickType_t timeo * The lock for the specified bus. * This can be used when calling native I2C functionality outside of Tactility. */ -Lockable& getLock(i2c_port_t port); +Lock& getLock(i2c_port_t port); } // namespace diff --git a/TactilityHeadless/Include/Tactility/hal/sdcard/SdCardDevice.h b/TactilityHeadless/Include/Tactility/hal/sdcard/SdCardDevice.h index 065119fa..7ca7bf10 100644 --- a/TactilityHeadless/Include/Tactility/hal/sdcard/SdCardDevice.h +++ b/TactilityHeadless/Include/Tactility/hal/sdcard/SdCardDevice.h @@ -29,7 +29,7 @@ private: public: explicit SdCardDevice(MountBehaviour mountBehaviour) : mountBehaviour(mountBehaviour) {} - virtual ~SdCardDevice() override = default; + ~SdCardDevice() override = default; Type getType() const final { return Type::SdCard; }; @@ -39,7 +39,7 @@ public: /** Return empty string when not mounted or the mount path if mounted */ virtual std::string getMountPath() const = 0; - virtual std::shared_ptr getLockable() const = 0; + virtual Lock& getLock() const = 0; virtual MountBehaviour getMountBehaviour() const { return mountBehaviour; } bool isMounted() const { return getState() == State::Mounted; } @@ -55,10 +55,9 @@ std::shared_ptr _Nullable find(const std::string& path); template inline ReturnType withSdCardLock(const std::string& path, std::function fn) { auto sdcard = find(path); - std::unique_ptr scoped_lockable; if (sdcard != nullptr) { - scoped_lockable = sdcard->getLockable()->scoped(); - scoped_lockable->lock(portMAX_DELAY); + auto scoped_lockable = sdcard->getLock().asScopedLock(); + scoped_lockable.lock(portMAX_DELAY); } return fn(); diff --git a/TactilityHeadless/Include/Tactility/hal/sdcard/SpiSdCardDevice.h b/TactilityHeadless/Include/Tactility/hal/sdcard/SpiSdCardDevice.h index 64fd612c..e6fd2c99 100644 --- a/TactilityHeadless/Include/Tactility/hal/sdcard/SpiSdCardDevice.h +++ b/TactilityHeadless/Include/Tactility/hal/sdcard/SpiSdCardDevice.h @@ -26,7 +26,7 @@ public: gpio_num_t spiPinWp, gpio_num_t spiPinInt, MountBehaviour mountBehaviourAtBoot, - std::shared_ptr lockable = std::make_shared(), + std::shared_ptr lockable = std::make_shared(), std::vector csPinWorkAround = std::vector(), spi_host_device_t spiHost = SPI2_HOST, int spiFrequencyKhz = SDMMC_FREQ_DEFAULT @@ -49,7 +49,7 @@ public: gpio_num_t spiPinWp; // Write-protect gpio_num_t spiPinInt; // Interrupt SdCardDevice::MountBehaviour mountBehaviourAtBoot; - std::shared_ptr _Nullable lockable; + std::shared_ptr _Nullable lockable; std::vector csPinWorkAround; spi_host_device_t spiHost; bool formatOnMountFailed = false; @@ -80,7 +80,7 @@ public: bool unmount() final; std::string getMountPath() const final { return mountPath; } - std::shared_ptr getLockable() const final { return config->lockable; } + Lock& getLock() const final { return *config->lockable; } State getState() const override; diff --git a/TactilityHeadless/Include/Tactility/hal/spi/Spi.h b/TactilityHeadless/Include/Tactility/hal/spi/Spi.h index 24137a97..a21b98cd 100644 --- a/TactilityHeadless/Include/Tactility/hal/spi/Spi.h +++ b/TactilityHeadless/Include/Tactility/hal/spi/Spi.h @@ -2,7 +2,7 @@ #include "SpiCompat.h" -#include +#include #include namespace tt::hal::spi { @@ -26,7 +26,7 @@ struct Configuration { /** Whether configuration can be changed. */ bool hasMutableConfiguration; /** Optional custom lock */ - std::shared_ptr _Nullable lock; + std::shared_ptr _Nullable lock; }; enum class Status { @@ -45,6 +45,6 @@ bool stop(spi_host_device_t device); bool isStarted(spi_host_device_t device); /** @return the lock that represents the specified device. Can be used with third party SPI implementations or native API calls (e.g. ESP-IDF). */ -Lockable& getLock(spi_host_device_t device); +Lock& getLock(spi_host_device_t device); } // namespace tt::hal::spi diff --git a/TactilityHeadless/Include/Tactility/hal/uart/Uart.h b/TactilityHeadless/Include/Tactility/hal/uart/Uart.h index 3a0acac0..de301147 100644 --- a/TactilityHeadless/Include/Tactility/hal/uart/Uart.h +++ b/TactilityHeadless/Include/Tactility/hal/uart/Uart.h @@ -3,7 +3,7 @@ #include #include "../Gpio.h" -#include "Tactility/Lockable.h" +#include "Tactility/Lock.h" #include "UartCompat.h" #include @@ -59,7 +59,7 @@ bool stop(uart_port_t port); bool isStarted(uart_port_t port); /** @return a lock that is usable for using ESP-IDF directly, or for use with third party APIs */ -Lockable& getLock(uart_port_t port); +Lock& getLock(uart_port_t port); /** * Read up to a specified amount of bytes diff --git a/TactilityHeadless/Source/hal/Device.cpp b/TactilityHeadless/Source/hal/Device.cpp index f8b93c4f..21d8a548 100644 --- a/TactilityHeadless/Source/hal/Device.cpp +++ b/TactilityHeadless/Source/hal/Device.cpp @@ -19,56 +19,55 @@ auto toVector(RangeType&& range) { } void registerDevice(const std::shared_ptr& device) { - auto scoped_mutex = mutex.scoped(); - if (scoped_mutex->lock()) { - if (findDevice(device->getId()) == nullptr) { - devices.push_back(device); - TT_LOG_I(TAG, "Registered %s with id %lu", device->getName().c_str(), device->getId()); - } else { - TT_LOG_W(TAG, "Device %s with id %lu was already registered", device->getName().c_str(), device->getId()); - } + auto scoped_mutex = mutex.asScopedLock(); + scoped_mutex.lock(); + + if (findDevice(device->getId()) == nullptr) { + devices.push_back(device); + TT_LOG_I(TAG, "Registered %s with id %lu", device->getName().c_str(), device->getId()); + } else { + TT_LOG_W(TAG, "Device %s with id %lu was already registered", device->getName().c_str(), device->getId()); } } void deregisterDevice(const std::shared_ptr& device) { - auto scoped_mutex = mutex.scoped(); + auto scoped_mutex = mutex.asScopedLock(); + scoped_mutex.lock(); + auto id_to_remove = device->getId(); - if (scoped_mutex->lock()) { - auto remove_iterator = std::remove_if(devices.begin(), devices.end(), [id_to_remove](const auto& device) { - return device->getId() == id_to_remove; - }); - if (remove_iterator != devices.end()) { - TT_LOG_I(TAG, "Deregistering %s with id %lu", device->getName().c_str(), device->getId()); - devices.erase(remove_iterator); - } else { - TT_LOG_W(TAG, "Deregistering %s with id %lu failed: not found", device->getName().c_str(), device->getId()); - } + auto remove_iterator = std::remove_if(devices.begin(), devices.end(), [id_to_remove](const auto& device) { + return device->getId() == id_to_remove; + }); + if (remove_iterator != devices.end()) { + TT_LOG_I(TAG, "Deregistering %s with id %lu", device->getName().c_str(), device->getId()); + devices.erase(remove_iterator); + } else { + TT_LOG_W(TAG, "Deregistering %s with id %lu failed: not found", device->getName().c_str(), device->getId()); } } std::vector> findDevices(const std::function&)>& filterFunction) { - auto scoped_mutex = mutex.scoped(); - if (scoped_mutex->lock()) { - auto devices_view = devices | std::views::filter([&filterFunction](auto& device) { - return filterFunction(device); - }); - return toVector(devices_view); - } - return {}; + auto scoped_mutex = mutex.asScopedLock(); + scoped_mutex.lock(); + + auto devices_view = devices | std::views::filter([&filterFunction](auto& device) { + return filterFunction(device); + }); + return toVector(devices_view); } std::shared_ptr _Nullable findDevice(const std::function&)>& filterFunction) { - auto scoped_mutex = mutex.scoped(); - if (scoped_mutex->lock()) { - auto result_set = devices | std::views::filter([&filterFunction](auto& device) { - return filterFunction(device); - }); - if (!result_set.empty()) { - return result_set.front(); - } - } + auto scoped_mutex = mutex.asScopedLock(); + scoped_mutex.lock(); - return nullptr; + auto result_set = devices | std::views::filter([&filterFunction](auto& device) { + return filterFunction(device); + }); + if (!result_set.empty()) { + return result_set.front(); + } else { + return nullptr; + } } std::shared_ptr _Nullable findDevice(std::string name) { diff --git a/TactilityHeadless/Source/hal/gps/GpsDevice.cpp b/TactilityHeadless/Source/hal/gps/GpsDevice.cpp index 316648ca..da488e69 100644 --- a/TactilityHeadless/Source/hal/gps/GpsDevice.cpp +++ b/TactilityHeadless/Source/hal/gps/GpsDevice.cpp @@ -75,8 +75,8 @@ int32_t GpsDevice::threadMain() { } bool GpsDevice::start() { - auto scoped_lockable = mutex.scoped(); - scoped_lockable->lock(portMAX_DELAY); + auto lock = mutex.asScopedLock(); + lock.lock(); if (thread != nullptr && thread->getState() != Thread::State::Stopped) { TT_LOG_W(TAG, "Already started"); @@ -108,8 +108,8 @@ bool GpsDevice::start() { } bool GpsDevice::stop() { - auto scoped_lockable = mutex.scoped(); - scoped_lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(portMAX_DELAY); if (thread != nullptr) { threadInterrupted = true; @@ -119,11 +119,11 @@ bool GpsDevice::stop() { if (old_thread->getState() != Thread::State::Stopped) { // Unlock so thread can lock - scoped_lockable->unlock(); + lock.unlock(); // Wait for thread to finish old_thread->join(); // Re-lock to continue logic below - scoped_lockable->lock(); + lock.lock(); } } @@ -138,14 +138,14 @@ bool GpsDevice::stop() { } bool GpsDevice::isStarted() const { - auto scoped_lockable = mutex.scoped(); - scoped_lockable->lock(portMAX_DELAY); + auto lock = mutex.asScopedLock(); + lock.lock(); return thread != nullptr && thread->getState() != Thread::State::Stopped; } bool GpsDevice::isThreadInterrupted() const { - auto scoped_lockable = mutex.scoped(); - scoped_lockable->lock(portMAX_DELAY); + auto lock = mutex.asScopedLock(); + lock.lock(portMAX_DELAY); return threadInterrupted; } diff --git a/TactilityHeadless/Source/hal/gps/Satellites.cpp b/TactilityHeadless/Source/hal/gps/Satellites.cpp index 317b95da..1b321176 100644 --- a/TactilityHeadless/Source/hal/gps/Satellites.cpp +++ b/TactilityHeadless/Source/hal/gps/Satellites.cpp @@ -23,8 +23,8 @@ SatelliteStorage::SatelliteRecord* SatelliteStorage::findRecord(int number) { } SatelliteStorage::SatelliteRecord* SatelliteStorage::findUnusedRecord() { - auto lockable = mutex.scoped(); - lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(); auto result = records | std::views::filter([](auto& record) { return !record.inUse; @@ -41,8 +41,8 @@ SatelliteStorage::SatelliteRecord* SatelliteStorage::findUnusedRecord() { } SatelliteStorage::SatelliteRecord* SatelliteStorage::findRecordToRecycle() { - auto lockable = mutex.scoped(); - lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(); int candidate_index = -1; auto candidate_age = portMAX_DELAY; @@ -72,8 +72,8 @@ SatelliteStorage::SatelliteRecord* SatelliteStorage::findRecordToRecycle() { } SatelliteStorage::SatelliteRecord* SatelliteStorage::findWithFallback(int number) { - auto lockable = mutex.scoped(); - lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(); if (auto* found_record = findRecord(number)) { return found_record; @@ -85,8 +85,8 @@ SatelliteStorage::SatelliteRecord* SatelliteStorage::findWithFallback(int number } void SatelliteStorage::notify(const minmea_sat_info& data) { - auto lockable = mutex.scoped(); - lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(); auto* record = findWithFallback(data.nr); if (record != nullptr) { @@ -98,8 +98,8 @@ void SatelliteStorage::notify(const minmea_sat_info& data) { } void SatelliteStorage::getRecords(const std::function& onRecord) const { - auto lockable = mutex.scoped(); - lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(); TickType_t expire_duration = kernel::secondsToTicks(recentTimeSeconds); TickType_t now = kernel::getTicks(); diff --git a/TactilityHeadless/Source/hal/i2c/I2c.cpp b/TactilityHeadless/Source/hal/i2c/I2c.cpp index f7c45783..c9f96f98 100644 --- a/TactilityHeadless/Source/hal/i2c/I2c.cpp +++ b/TactilityHeadless/Source/hal/i2c/I2c.cpp @@ -76,8 +76,8 @@ bool init(const std::vector& configurations) { } bool configure(i2c_port_t port, const i2c_config_t& configuration) { - auto lockable = getLock(port).asScopedLock(); - lockable.lock(); + auto lock = getLock(port).asScopedLock(); + lock.lock(); Data& data = dataArray[port]; if (data.isStarted) { @@ -93,8 +93,8 @@ bool configure(i2c_port_t port, const i2c_config_t& configuration) { } bool start(i2c_port_t port) { - auto lockable = getLock(port).asScopedLock(); - lockable.lock(); + auto lock = getLock(port).asScopedLock(); + lock.lock(); Data& data = dataArray[port]; printInfo(data); @@ -131,8 +131,8 @@ bool start(i2c_port_t port) { } bool stop(i2c_port_t port) { - auto lockable = getLock(port).asScopedLock(); - lockable.lock(); + auto lock = getLock(port).asScopedLock(); + lock.lock(); Data& data = dataArray[port]; Configuration& config = data.configuration; @@ -162,14 +162,14 @@ bool stop(i2c_port_t port) { } bool isStarted(i2c_port_t port) { - auto lockable = getLock(port).asScopedLock(); - lockable.lock(); + auto lock = getLock(port).asScopedLock(); + lock.lock(); return dataArray[port].isStarted; } bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize, TickType_t timeout) { - auto lockable = getLock(port).asScopedLock(); - if (!lockable.lock(timeout)) { + auto lock = getLock(port).asScopedLock(); + if (!lock.lock(timeout)) { TT_LOG_E(TAG, "(%d) Mutex timeout", port); return false; } @@ -184,8 +184,8 @@ bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize } bool masterReadRegister(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t* data, size_t dataSize, TickType_t timeout) { - auto lockable = getLock(port).asScopedLock(); - if (!lockable.lock(timeout)) { + auto lock = getLock(port).asScopedLock(); + if (!lock.lock(timeout)) { TT_LOG_E(TAG, "(%d) Mutex timeout", port); return false; } @@ -217,8 +217,8 @@ bool masterReadRegister(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t* } bool masterWrite(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) { - auto lockable = getLock(port).asScopedLock(); - if (!lockable.lock(timeout)) { + auto lock = getLock(port).asScopedLock(); + if (!lock.lock(timeout)) { TT_LOG_E(TAG, "(%d) Mutex timeout", port); return false; } @@ -235,8 +235,8 @@ bool masterWrite(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t bool masterWriteRegister(i2c_port_t port, uint8_t address, uint8_t reg, const uint8_t* data, uint16_t dataSize, TickType_t timeout) { tt_check(reg != 0); - auto lockable = getLock(port).asScopedLock(); - if (!lockable.lock(timeout)) { + auto lock = getLock(port).asScopedLock(); + if (!lock.lock(timeout)) { TT_LOG_E(TAG, "(%d) Mutex timeout", port); return false; } @@ -277,8 +277,8 @@ bool masterWriteRegisterArray(i2c_port_t port, uint8_t address, const uint8_t* d } bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize, TickType_t timeout) { - auto lockable = getLock(port).asScopedLock(); - if (!lockable.lock(timeout)) { + auto lock = getLock(port).asScopedLock(); + if (!lock.lock(timeout)) { TT_LOG_E(TAG, "(%d) Mutex timeout", port); return false; } @@ -293,8 +293,8 @@ bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, } bool masterHasDeviceAtAddress(i2c_port_t port, uint8_t address, TickType_t timeout) { - auto lockable = getLock(port).asScopedLock(); - if (!lockable.lock(timeout)) { + auto lock = getLock(port).asScopedLock(); + if (!lock.lock(timeout)) { TT_LOG_E(TAG, "(%d) Mutex timeout", port); return false; } @@ -308,7 +308,7 @@ bool masterHasDeviceAtAddress(i2c_port_t port, uint8_t address, TickType_t timeo #endif // ESP_PLATFORM } -Lockable& getLock(i2c_port_t port) { +Lock& getLock(i2c_port_t port) { return dataArray[port].mutex; } diff --git a/TactilityHeadless/Source/hal/spi/Spi.cpp b/TactilityHeadless/Source/hal/spi/Spi.cpp index 29ef3318..29e610c6 100644 --- a/TactilityHeadless/Source/hal/spi/Spi.cpp +++ b/TactilityHeadless/Source/hal/spi/Spi.cpp @@ -7,7 +7,7 @@ namespace tt::hal::spi { struct Data { - std::shared_ptr lock; + std::shared_ptr lock; bool isConfigured = false; bool isStarted = false; Configuration configuration; @@ -169,7 +169,7 @@ bool isStarted(spi_host_device_t device) { return dataArray[device].isStarted; } -Lockable& getLock(spi_host_device_t device) { +Lock& getLock(spi_host_device_t device) { return *dataArray[device].lock; } diff --git a/TactilityHeadless/Source/hal/uart/Uart.cpp b/TactilityHeadless/Source/hal/uart/Uart.cpp index 98df6c27..ba04ab33 100644 --- a/TactilityHeadless/Source/hal/uart/Uart.cpp +++ b/TactilityHeadless/Source/hal/uart/Uart.cpp @@ -180,7 +180,7 @@ bool isStarted(uart_port_t port) { return dataArray[port].isStarted; } -Lockable& getLock(uart_port_t port) { +Lock& getLock(uart_port_t port) { return dataArray[port].mutex; } diff --git a/TactilityHeadless/Source/service/gps/GpsService.cpp b/TactilityHeadless/Source/service/gps/GpsService.cpp index e8a2cee3..4ef52050 100644 --- a/TactilityHeadless/Source/service/gps/GpsService.cpp +++ b/TactilityHeadless/Source/service/gps/GpsService.cpp @@ -8,8 +8,8 @@ using tt::hal::gps::GpsDevice; namespace tt::service::gps { GpsService::GpsDeviceRecord* _Nullable GpsService::findGpsRecord(const std::shared_ptr& device) { - auto lockable = mutex.scoped(); - lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(); auto result = std::views::filter(deviceRecords, [&device](auto& record){ return record.device.get() == device.get(); @@ -22,8 +22,8 @@ GpsService::GpsDeviceRecord* _Nullable GpsService::findGpsRecord(const std::shar } void GpsService::addGpsDevice(const std::shared_ptr& device) { - auto lockable = mutex.scoped(); - lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(); GpsDeviceRecord record = { .device = device }; @@ -35,8 +35,8 @@ void GpsService::addGpsDevice(const std::shared_ptr& device) { } void GpsService::removeGpsDevice(const std::shared_ptr& device) { - auto lockable = mutex.scoped(); - lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(); GpsDeviceRecord* record = findGpsRecord(device); @@ -50,8 +50,8 @@ void GpsService::removeGpsDevice(const std::shared_ptr& device) { } void GpsService::onStart(tt::service::ServiceContext &serviceContext) { - auto lockable = mutex.scoped(); - lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(); deviceRecords.clear(); @@ -71,8 +71,8 @@ void GpsService::onStop(tt::service::ServiceContext &serviceContext) { bool GpsService::startGpsDevice(GpsDeviceRecord& record) { TT_LOG_I(TAG, "[device %lu] starting", record.device->getId()); - auto lockable = mutex.scoped(); - lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(); auto device = record.device; @@ -114,8 +114,8 @@ bool GpsService::stopGpsDevice(GpsDeviceRecord& record) { bool GpsService::startReceiving() { TT_LOG_I(TAG, "Start receiving"); - auto lockable = mutex.scoped(); - lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(); bool started_one_or_more = false; @@ -131,8 +131,8 @@ bool GpsService::startReceiving() { void GpsService::stopReceiving() { TT_LOG_I(TAG, "Stop receiving"); - auto lockable = mutex.scoped(); - lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(); for (auto& record : deviceRecords) { stopGpsDevice(record); @@ -142,8 +142,8 @@ void GpsService::stopReceiving() { } bool GpsService::isReceiving() { - auto lockable = mutex.scoped(); - lockable->lock(); + auto lock = mutex.asScopedLock(); + lock.lock(); return receiving; } diff --git a/TactilityHeadless/Source/service/wifi/WifiEsp.cpp b/TactilityHeadless/Source/service/wifi/WifiEsp.cpp index f350f16c..b2e332a6 100644 --- a/TactilityHeadless/Source/service/wifi/WifiEsp.cpp +++ b/TactilityHeadless/Source/service/wifi/WifiEsp.cpp @@ -73,54 +73,54 @@ public: bool connection_target_remember = false; // Whether to store the connection_target on successful connection or not RadioState getRadioState() const { - auto lockable = dataMutex.scoped(); - lockable->lock(); + auto lock = dataMutex.asScopedLock(); + lock.lock(); // TODO: Handle lock failure return radio_state; } void setRadioState(RadioState newState) { - auto lockable = dataMutex.scoped(); - lockable->lock(); + auto lock = dataMutex.asScopedLock(); + lock.lock(); // TODO: Handle lock failure radio_state = newState; } bool isScanning() const { - auto lockable = dataMutex.scoped(); - lockable->lock(); + auto lock = dataMutex.asScopedLock(); + lock.lock(); // TODO: Handle lock failure return scan_active; } void setScanning(bool newState) { - auto lockable = dataMutex.scoped(); - lockable->lock(); + auto lock = dataMutex.asScopedLock(); + lock.lock(); // TODO: Handle lock failure scan_active = newState; } bool isScanActive() const { - auto lcokable = dataMutex.scoped(); - lcokable->lock(); + auto lock = dataMutex.asScopedLock(); + lock.lock(); return scan_active; } void setScanActive(bool newState) { - auto lockable = dataMutex.scoped(); - lockable->lock(); + auto lock = dataMutex.asScopedLock(); + lock.lock(); scan_active = newState; } bool isSecureConnection() const { - auto lockable = dataMutex.scoped(); - lockable->lock(); + auto lock = dataMutex.asScopedLock(); + lock.lock(); return secure_connection; } void setSecureConnection(bool newState) { - auto lockable = dataMutex.scoped(); - lockable->lock(); + auto lock = dataMutex.asScopedLock(); + lock.lock(); secure_connection = newState; } }; @@ -191,8 +191,8 @@ void connect(const settings::WifiApSettings* ap, bool remember) { return; } - auto lockable = wifi->dataMutex.scoped(); - if (!lockable->lock(10 / portTICK_PERIOD_MS)) { + auto lock = wifi->dataMutex.asScopedLock(); + if (!lock.lock(10 / portTICK_PERIOD_MS)) { return; } @@ -215,8 +215,8 @@ void disconnect() { return; } - auto lockable = wifi->dataMutex.scoped(); - if (!lockable->lock(10 / portTICK_PERIOD_MS)) { + auto lock = wifi->dataMutex.asScopedLock(); + if (!lock.lock(10 / portTICK_PERIOD_MS)) { return; } @@ -237,8 +237,8 @@ void setScanRecords(uint16_t records) { return; } - auto lockable = wifi->dataMutex.scoped(); - if (!lockable->lock(10 / portTICK_PERIOD_MS)) { + auto lock = wifi->dataMutex.asScopedLock(); + if (!lock.lock(10 / portTICK_PERIOD_MS)) { return; } @@ -258,8 +258,8 @@ std::vector getScanResults() { return records; } - auto lockable = wifi->dataMutex.scoped(); - if (!lockable->lock(10 / portTICK_PERIOD_MS)) { + auto lock = wifi->dataMutex.asScopedLock(); + if (!lock.lock(10 / portTICK_PERIOD_MS)) { return records; } @@ -285,8 +285,8 @@ void setEnabled(bool enabled) { return; } - auto lockable = wifi->dataMutex.scoped(); - if (!lockable->lock(10 / portTICK_PERIOD_MS)) { + auto lock = wifi->dataMutex.asScopedLock(); + if (!lock.lock(10 / portTICK_PERIOD_MS)) { return; } @@ -305,8 +305,8 @@ bool isConnectionSecure() { return false; } - auto lockable = wifi->dataMutex.scoped(); - if (!lockable->lock(10 / portTICK_PERIOD_MS)) { + auto lock = wifi->dataMutex.asScopedLock(); + if (!lock.lock(10 / portTICK_PERIOD_MS)) { return false; } @@ -326,8 +326,8 @@ int getRssi() { // endregion Public functions static void scan_list_alloc(std::shared_ptr wifi) { - auto lockable = wifi->dataMutex.scoped(); - if (lockable->lock()) { + auto lock = wifi->dataMutex.asScopedLock(); + if (lock.lock()) { 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; @@ -335,8 +335,8 @@ static void scan_list_alloc(std::shared_ptr wifi) { } static void scan_list_alloc_safely(std::shared_ptr wifi) { - auto lockable = wifi->dataMutex.scoped(); - if (lockable->lock()) { + auto lock = wifi->dataMutex.asScopedLock(); + if (lock.lock()) { if (wifi->scan_list == nullptr) { scan_list_alloc(wifi); } @@ -344,8 +344,8 @@ static void scan_list_alloc_safely(std::shared_ptr wifi) { } static void scan_list_free(std::shared_ptr wifi) { - auto lockable = wifi->dataMutex.scoped(); - if (lockable->lock()) { + auto lock = wifi->dataMutex.asScopedLock(); + if (lock.lock()) { assert(wifi->scan_list != nullptr); free(wifi->scan_list); wifi->scan_list = nullptr; @@ -354,8 +354,8 @@ static void scan_list_free(std::shared_ptr wifi) { } static void scan_list_free_safely(std::shared_ptr wifi) { - auto lockable = wifi->dataMutex.scoped(); - if (lockable->lock()) { + auto lock = wifi->dataMutex.asScopedLock(); + if (lock.lock()) { if (wifi->scan_list != nullptr) { scan_list_free(wifi); } @@ -363,8 +363,8 @@ static void scan_list_free_safely(std::shared_ptr wifi) { } static void publish_event_simple(std::shared_ptr wifi, EventType type) { - auto lockable = wifi->dataMutex.scoped(); - if (lockable->lock()) { + auto lock = wifi->dataMutex.asScopedLock(); + if (lock.lock()) { Event turning_on_event = {.type = type}; wifi->pubsub->publish(&turning_on_event); } @@ -380,8 +380,8 @@ static bool copy_scan_list(std::shared_ptr wifi) { return false; } - auto lockable = wifi->dataMutex.scoped(); - if (!lockable->lock()) { + auto lock = wifi->dataMutex.asScopedLock(); + if (lock.lock()) { return false; } @@ -407,9 +407,9 @@ static bool copy_scan_list(std::shared_ptr wifi) { static bool find_auto_connect_ap(std::shared_ptr context, settings::WifiApSettings& settings) { auto wifi = std::static_pointer_cast(context); - auto lockable = wifi->dataMutex.scoped(); + auto lock = wifi->dataMutex.asScopedLock(); - if (lockable->lock(10 / portTICK_PERIOD_MS)) { + if (lock.lock(10 / portTICK_PERIOD_MS)) { 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); @@ -512,9 +512,8 @@ static void dispatchEnable(std::shared_ptr context) { return; } - auto lockable = wifi->radioMutex.scoped(); - - if (lockable->lock(50 / portTICK_PERIOD_MS)) { + auto lock = wifi->radioMutex.asScopedLock(); + if (lock.lock(50 / portTICK_PERIOD_MS)) { TT_LOG_I(TAG, "Enabling"); wifi->setRadioState(RadioState::OnPending); publish_event_simple(wifi, EventType::RadioStateOnPending); @@ -590,9 +589,9 @@ static void dispatchEnable(std::shared_ptr context) { static void dispatchDisable(std::shared_ptr context) { TT_LOG_I(TAG, "dispatchDisable()"); auto wifi = std::static_pointer_cast(context); - auto lockable = wifi->radioMutex.scoped(); + auto lock = wifi->radioMutex.asScopedLock(); - if (!lockable->lock(50 / portTICK_PERIOD_MS)) { + if (!lock.lock(50 / portTICK_PERIOD_MS)) { TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "disable()"); return; } @@ -657,9 +656,9 @@ static void dispatchDisable(std::shared_ptr context) { static void dispatchScan(std::shared_ptr context) { TT_LOG_I(TAG, "dispatchScan()"); auto wifi = std::static_pointer_cast(context); - auto lockable = wifi->radioMutex.scoped(); + auto lock = wifi->radioMutex.asScopedLock(); - if (!lockable->lock(10 / portTICK_PERIOD_MS)) { + if (!lock.lock(10 / portTICK_PERIOD_MS)) { TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); return; } @@ -691,9 +690,9 @@ static void dispatchScan(std::shared_ptr context) { static void dispatchConnect(std::shared_ptr context) { TT_LOG_I(TAG, "dispatchConnect()"); auto wifi = std::static_pointer_cast(context); - auto lockable = wifi->radioMutex.scoped(); + auto lock = wifi->radioMutex.asScopedLock(); - if (!lockable->lock(50 / portTICK_PERIOD_MS)) { + if (!lock.lock(50 / portTICK_PERIOD_MS)) { TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "dispatchConnect()"); return; } @@ -790,9 +789,9 @@ static void dispatchConnect(std::shared_ptr context) { static void dispatchDisconnectButKeepActive(std::shared_ptr context) { TT_LOG_I(TAG, "dispatchDisconnectButKeepActive()"); auto wifi = std::static_pointer_cast(context); - auto lockable = wifi->radioMutex.scoped(); + auto lock = wifi->radioMutex.asScopedLock(); - if (!lockable->lock(50 / portTICK_PERIOD_MS)) { + if (!lock.lock(50 / portTICK_PERIOD_MS)) { TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); return; } @@ -835,9 +834,8 @@ static void dispatchDisconnectButKeepActive(std::shared_ptr context) { } static bool shouldScanForAutoConnect(std::shared_ptr wifi) { - auto lockable = wifi->dataMutex.scoped(); - - if (!lockable->lock(100)) { + auto lock = wifi->dataMutex.asScopedLock(); + if (!lock.lock(100)) { return false; } diff --git a/Tests/TactilityCore/CMakeLists.txt b/Tests/TactilityCore/CMakeLists.txt index d3b97b15..e97a60d8 100644 --- a/Tests/TactilityCore/CMakeLists.txt +++ b/Tests/TactilityCore/CMakeLists.txt @@ -18,7 +18,7 @@ add_test(NAME TactilityCoreTests COMMAND TactilityCoreTests ) -target_link_libraries(TactilityCoreTests - PUBLIC TactilityCore - freertos_kernel +target_link_libraries(TactilityCoreTests PUBLIC + TactilityCore + freertos_kernel ) diff --git a/Tests/TactilityCore/LockableTest.cpp b/Tests/TactilityCore/LockTest.cpp similarity index 96% rename from Tests/TactilityCore/LockableTest.cpp rename to Tests/TactilityCore/LockTest.cpp index 8cd7fbe3..03212bab 100644 --- a/Tests/TactilityCore/LockableTest.cpp +++ b/Tests/TactilityCore/LockTest.cpp @@ -1,5 +1,5 @@ #include "doctest.h" -#include +#include #include #include