From c4e039eaf6ce8946208a10bb3f0f67ac34745a0d Mon Sep 17 00:00:00 2001 From: alkonosst Date: Sat, 18 Apr 2026 18:48:38 -0400 Subject: [PATCH] feat: Add naming functionality to all objects with nullptr fallback, update unit tests and README, bump version --- README.md | 5 +- library.json | 2 +- library.properties | 2 +- platformio.ini | 20 ++-- src/RTOScppBuffer.h | 39 ++++++-- src/RTOScppLock.h | 64 +++++++++--- src/RTOScppQueue.h | 81 +++++++++++++--- src/RTOScppQueueSet.h | 16 ++- src/RTOScppRingBuffer.h | 63 ++++++++---- src/RTOScppTask.h | 97 ++++++++++--------- src/RTOScppTimer.h | 107 ++++++++++++--------- test/test_buffers/test_buffers.cpp | 42 ++++---- test/test_locks/test_locks.cpp | 47 ++++----- test/test_queues/test_queues.cpp | 59 ++++++++---- test/test_queuesets/test_queuesets.cpp | 33 ++----- test/test_ringbuffers/test_ringbuffers.cpp | 82 +++++++++++----- test/test_tasks/test_tasks.cpp | 20 +++- test/test_timers/test_timers.cpp | 33 ++----- 18 files changed, 514 insertions(+), 298 deletions(-) diff --git a/README.md b/README.md index e27009d..1c24c37 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ lib_deps = ; Release vx.y.z (using an exact version is recommended) lib_deps = - https://github.com/alkonosst/RTOScppESP32.git#v1.0.0 + https://github.com/alkonosst/RTOScppESP32.git#v1.1.0 ``` ## Using the library @@ -166,6 +166,9 @@ microcontroller with limited resources. For now, only the constructors are explained here. You can check the methods using the code completion feature of your IDE or checking the source code of each object type. +All objects have a name parameter that is used for debugging purposes (**with a default name if you +don't provide one**). It is a good practice to give a meaningful name to each object to make it easier to identify them in the debugging process. + ### Using tasks ```cpp diff --git a/library.json b/library.json index b32dc14..63ba2e7 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "RTOScppESP32", - "version": "1.0.2", + "version": "1.1.0", "authors": { "name": "Maximiliano Ramirez", "email": "maximiliano.ramirezbravo@gmail.com" diff --git a/library.properties b/library.properties index acdbaa7..e9b90ef 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=RTOScppESP32 -version=1.0.2 +version=1.1.0 author=Maximiliano Ramirez maintainer=Maximiliano Ramirez sentence=FreeRTOS abstraction layer for ESP32 with C++ interface. diff --git a/platformio.ini b/platformio.ini index bf9862b..27ed666 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,7 +9,7 @@ ; https://docs.platformio.org/page/projectconf.html [platformio] -lib_dir = . +; lib_dir = . ; src_dir = examples/Task ; src_dir = examples/Timer ; src_dir = examples/Mutex @@ -25,22 +25,19 @@ lib_dir = . ; src_dir = examples/QueueSet [env:esp32-s3] -platform = https://github.com/pioarduino/platform-espressif32/releases/download/53.03.13+github/platform-espressif32.zip +platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.37/platform-espressif32.zip board = esp32-s3-devkitc-1 framework = arduino ; Tests -; Ignore Unity library to avoid conflicts with source code due to lib_dir = . -lib_ignore = Unity - ; test_ignore = - ; test_tasks - ; test_timers + ; test_buffers ; test_locks ; test_queues - ; test_buffers - ; test_ringbuffers ; test_queuesets + ; test_ringbuffers + ; test_tasks + ; test_timers ; Config ESP32 board_build.f_flash = 80000000L @@ -59,6 +56,11 @@ monitor_filters = ; Flags build_flags = + ; Enable all warnings and treat them as errors + -Wall + -Wextra + -Werror + ; Enable debug (ESP-IDF logs) ; -DUSE_ESP_IDF_LOG ; -DCORE_DEBUG_LEVEL=5 diff --git a/src/RTOScppBuffer.h b/src/RTOScppBuffer.h index 41dc851..4f29fbf 100644 --- a/src/RTOScppBuffer.h +++ b/src/RTOScppBuffer.h @@ -1,5 +1,5 @@ /** - * SPDX-FileCopyrightText: 2025 Maximiliano Ramirez + * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez * * SPDX-License-Identifier: MIT */ @@ -32,6 +32,12 @@ class IBuffer { */ virtual StreamBufferHandle_t getHandle() const = 0; + /** + * @brief Get the name of the buffer. Useful for debugging and logging purposes. + * @return const char* Name of the buffer. Default is "RtosBuffer" if no name is provided. + */ + virtual const char* getName() const = 0; + /** * @brief Check if the buffer is created. * @return true Buffer is created. @@ -128,12 +134,15 @@ class Policy { StreamBufferHandle_t getHandle() const { return _handle; } bool isCreated() const { return _handle != nullptr; } + const char* getName() const { return _name; } protected: Policy() - : _handle(nullptr) {} + : _handle(nullptr) + , _name("RtosBuffer") {} StreamBufferHandle_t _handle; + const char* _name; }; // CRTP stream buffer base policy class @@ -157,8 +166,9 @@ template class StreamBufferDynamicPolicy : public StreamBufferPolicy> { public: - StreamBufferDynamicPolicy() { + StreamBufferDynamicPolicy(const char* name = nullptr) { this->_handle = xStreamBufferGenericCreate(BufferSize, TriggerBytes, false, nullptr, nullptr); + if (name) this->_name = name; } }; @@ -167,7 +177,7 @@ template class StreamBufferStaticPolicy : public StreamBufferPolicy> { public: - StreamBufferStaticPolicy() { + StreamBufferStaticPolicy(const char* name = nullptr) { this->_handle = xStreamBufferGenericCreateStatic(BufferSize + 1, TriggerBytes, false, @@ -175,6 +185,7 @@ class StreamBufferStaticPolicy &_buf_buffer, nullptr, nullptr); + if (name) this->_name = name; } private: @@ -189,6 +200,10 @@ class StreamBufferExternalStoragePolicy public: static constexpr uint32_t REQUIRED_SIZE = BufferSize + 2; + StreamBufferExternalStoragePolicy(const char* name = nullptr) { + if (name) this->_name = name; + } + /** * @brief Create the stream buffer with an external memory allocation. * @param buffer External memory buffer. @@ -216,8 +231,9 @@ class StreamBufferExternalStoragePolicy template class MessageBufferDynamicPolicy : public Policy> { public: - MessageBufferDynamicPolicy() { + MessageBufferDynamicPolicy(const char* name = nullptr) { this->_handle = xStreamBufferGenericCreate(BufferSize, 0, true, nullptr, nullptr); + if (name) this->_name = name; } }; @@ -225,7 +241,7 @@ class MessageBufferDynamicPolicy : public Policy class MessageBufferStaticPolicy : public Policy> { public: - MessageBufferStaticPolicy() { + MessageBufferStaticPolicy(const char* name = nullptr) { this->_handle = xStreamBufferGenericCreateStatic(BufferSize + 1, 0, true, @@ -233,6 +249,7 @@ class MessageBufferStaticPolicy : public Policy_name = name; } private: @@ -247,6 +264,10 @@ class MessageBufferExternalStoragePolicy public: static constexpr uint32_t REQUIRED_SIZE = BufferSize + 2; + MessageBufferExternalStoragePolicy(const char* name = nullptr) { + if (name) this->_name = name; + } + /** * @brief Create the message buffer with an external memory allocation. * @param buffer External memory buffer. @@ -287,6 +308,12 @@ class DataBuffer : public IBuffer, public Policy { */ StreamBufferHandle_t getHandle() const override { return Policy::getHandle(); } + /** + * @brief Get the name of the buffer. Useful for debugging and logging purposes. + * @return const char* Name of the buffer. Default is "RtosBuffer" if no name is provided. + */ + virtual const char* getName() const override { return Policy::getName(); } + /** * @brief Check if the buffer is created. * @return true Buffer is created. diff --git a/src/RTOScppLock.h b/src/RTOScppLock.h index 23e10be..ac08304 100644 --- a/src/RTOScppLock.h +++ b/src/RTOScppLock.h @@ -1,5 +1,5 @@ /** - * SPDX-FileCopyrightText: 2025 Maximiliano Ramirez + * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez * * SPDX-License-Identifier: MIT */ @@ -32,6 +32,12 @@ class ILock { */ virtual SemaphoreHandle_t getHandle() const = 0; + /** + * @brief Get the name of the lock. Useful for debugging and logging purposes. + * @return const char* Name of the lock. Default is "RtosLock" if no name is provided. + */ + virtual const char* getName() const = 0; + /** * @brief Check if the lock is created. * @return true Lock is created. @@ -72,11 +78,14 @@ template class Policy { protected: Policy() - : _handle(nullptr) {} + : _handle(nullptr) + , _name("RtosLock") {} public: SemaphoreHandle_t getHandle() const { return _handle; } + const char* getName() const { return _name; } + bool isCreated() const { return _handle != nullptr; } bool take(const TickType_t ticks_to_wait = portMAX_DELAY) { @@ -91,6 +100,7 @@ class Policy { protected: SemaphoreHandle_t _handle; + const char* _name; }; // CRTP mutex base policy class @@ -110,14 +120,20 @@ class MutexPolicy : public Policy> { template class MutexDynamicPolicy : public MutexPolicy> { public: - MutexDynamicPolicy() { this->_handle = xSemaphoreCreateMutex(); } + MutexDynamicPolicy(const char* name = nullptr) { + this->_handle = xSemaphoreCreateMutex(); + if (name) this->_name = name; + } }; // Policy for mutex with static memory allocation template class MutexStaticPolicy : public MutexPolicy> { public: - MutexStaticPolicy() { this->_handle = xSemaphoreCreateMutexStatic(&_mutex_buffer); } + MutexStaticPolicy(const char* name = nullptr) { + this->_handle = xSemaphoreCreateMutexStatic(&_mutex_buffer); + if (name) this->_name = name; + } private: StaticSemaphore_t _mutex_buffer; @@ -140,15 +156,19 @@ class MutexRecursivePolicy : public Policy> { template class MutexRecursiveDynamicPolicy : public MutexRecursivePolicy> { public: - MutexRecursiveDynamicPolicy() { this->_handle = xSemaphoreCreateRecursiveMutex(); } + MutexRecursiveDynamicPolicy(const char* name = nullptr) { + this->_handle = xSemaphoreCreateRecursiveMutex(); + if (name) this->_name = name; + } }; // Policy for recursive mutex with static memory allocation template class MutexRecursiveStaticPolicy : public MutexRecursivePolicy> { public: - MutexRecursiveStaticPolicy() { + MutexRecursiveStaticPolicy(const char* name = nullptr) { this->_handle = xSemaphoreCreateRecursiveMutexStatic(&_mutex_recursive_buffer); + if (name) this->_name = name; } private: @@ -172,15 +192,19 @@ class SemaphoreBinaryPolicy : public Policy> { template class SemaphoreBinaryDynamicPolicy : public SemaphoreBinaryPolicy> { public: - SemaphoreBinaryDynamicPolicy() { this->_handle = xSemaphoreCreateBinary(); } + SemaphoreBinaryDynamicPolicy(const char* name = nullptr) { + this->_handle = xSemaphoreCreateBinary(); + if (name) this->_name = name; + } }; // Policy for binary semaphore with static memory allocation template class SemaphoreBinaryStaticPolicy : public SemaphoreBinaryPolicy> { public: - SemaphoreBinaryStaticPolicy() { + SemaphoreBinaryStaticPolicy(const char* name = nullptr) { this->_handle = xSemaphoreCreateBinaryStatic(&_sem_binary_buffer); + if (name) this->_name = name; } private: @@ -216,8 +240,9 @@ class SemaphoreCountingDynamicPolicy : public SemaphoreCountingPolicy> { public: - SemaphoreCountingDynamicPolicy() { + SemaphoreCountingDynamicPolicy(const char* name = nullptr) { this->_handle = xSemaphoreCreateCounting(MaxCount, InitialCount); + if (name) this->_name = name; } }; @@ -227,8 +252,9 @@ class SemaphoreCountingStaticPolicy : public SemaphoreCountingPolicy> { public: - SemaphoreCountingStaticPolicy() { + SemaphoreCountingStaticPolicy(const char* name = nullptr) { this->_handle = xSemaphoreCreateCountingStatic(MaxCount, InitialCount, &_sem_counting_buffer); + if (name) this->_name = name; } private: @@ -250,32 +276,40 @@ class Lock : public ILock, public Policy { * caution. * @return SemaphoreHandle_t Lock handle, nullptr if the lock is not created. */ - SemaphoreHandle_t getHandle() const { return Policy::getHandle(); } + SemaphoreHandle_t getHandle() const override { return Policy::getHandle(); } + + /** + * @brief Get the name of the lock. Useful for debugging and logging purposes. + * @return const char* Name of the lock. Default is "RtosLock" if no name is provided. + */ + const char* getName() const override { return Policy::getName(); } /** * @brief Check if the lock is created. * @return true Lock is created. */ - bool isCreated() const { return Policy::isCreated(); } + bool isCreated() const override { return Policy::isCreated(); } /** * @brief Take the lock. * @param ticks_to_wait Maximum time to wait for the operation to complete. * @return true Lock taken successfully, false if the lock is not created or failed to take. */ - bool take(const TickType_t ticks_to_wait = portMAX_DELAY) { return Policy::take(ticks_to_wait); } + bool take(const TickType_t ticks_to_wait = portMAX_DELAY) override { + return Policy::take(ticks_to_wait); + } /** * @brief Give the lock. * @return true Lock given successfully, false if the lock is not created or failed to give. */ - bool give() { return Policy::give(); } + bool give() override { return Policy::give(); } /** * @brief Check if the lock is taken. * @return true Lock is taken. */ - explicit operator bool() const { return isCreated(); } + explicit operator bool() const override { return isCreated(); } }; } // namespace Internal diff --git a/src/RTOScppQueue.h b/src/RTOScppQueue.h index 4830c5f..469f63e 100644 --- a/src/RTOScppQueue.h +++ b/src/RTOScppQueue.h @@ -1,5 +1,5 @@ /** - * SPDX-FileCopyrightText: 2025 Maximiliano Ramirez + * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez * * SPDX-License-Identifier: MIT */ @@ -12,6 +12,18 @@ namespace RTOS::Queues { +/** + * @brief Behavior when trying to insert an item into a full queue. Safe to use with Queue Sets. + * Useful to avoid duplicated code whenever you want to confirm if the queue is full before sending + * an item. + * Example: a polling task that needs to send data to a queue but doesn't care if the data is lost + * when the queue is full. + */ +enum class FullBehavior { + Block, // Block until space is available (default) + Fail, // Fail immediately if the queue is full +}; + // Interface for Queue objects, useful when using pointers class IQueue { protected: @@ -31,6 +43,12 @@ class IQueue { */ virtual QueueHandle_t getHandle() const = 0; + /** + * @brief Get the name of the queue. Useful for debugging and logging purposes. + * @return const char* Name of the queue. Default is "RtosQueue" if no name is provided. + */ + virtual const char* getName() const = 0; + /** * @brief Check if the queue is created. * @return true Queue is created. @@ -106,29 +124,35 @@ template class Policy { protected: Policy() - : _handle(nullptr) {} + : _handle(nullptr) + , _name("RtosQueue") {} QueueHandle_t getHandle() const { return _handle; } - + const char* getName() const { return _name; } bool isCreated() const { return _handle != nullptr; } protected: QueueHandle_t _handle; + const char* _name; }; // Policy for queue with dynamic memory allocation template class DynamicPolicy : public Policy> { public: - DynamicPolicy() { this->_handle = xQueueCreate(Length, sizeof(T)); } + DynamicPolicy(const char* name = nullptr) { + this->_handle = xQueueCreate(Length, sizeof(T)); + if (name) this->_name = name; + } }; // Policy for queue with static memory allocation template class StaticPolicy : public Policy> { public: - StaticPolicy() { + StaticPolicy(const char* name = nullptr) { this->_handle = xQueueCreateStatic(Length, sizeof(T), _storage, &_queue_buffer); + if (name) this->_name = name; } private: @@ -142,7 +166,10 @@ class ExternalStoragePolicy : public Policy> { public: static constexpr uint32_t REQUIRED_SIZE = Length * sizeof(T); - ExternalStoragePolicy() { this->_handle = nullptr; } + ExternalStoragePolicy(const char* name = nullptr) { + this->_handle = nullptr; + if (name) this->_name = name; + } /** * @brief Create the queue with the specified buffer. @@ -160,7 +187,7 @@ class ExternalStoragePolicy : public Policy> { }; // Main Queue class. You need to specify the policy used -template +template class Queue : public IQueue, public Policy { public: using Policy::Policy; // Inherit constructor @@ -176,6 +203,12 @@ class Queue : public IQueue, public Policy { */ QueueHandle_t getHandle() const override { return Policy::getHandle(); } + /** + * @brief Get the name of the queue. Useful for debugging and logging purposes. + * @return const char* Name of the queue. Default is "RtosQueue" if no name is provided. + */ + const char* getName() const override { return Policy::getName(); } + /** * @brief Check if the queue is created. * @return true Queue is created. @@ -216,6 +249,7 @@ class Queue : public IQueue, public Policy { bool reset() const override { if (!isCreated()) return false; xQueueReset(getHandle()); + return true; } /** @@ -268,6 +302,11 @@ class Queue : public IQueue, public Policy { */ bool push(const T& item, const TickType_t ticks_to_wait = portMAX_DELAY) const { if (!isCreated()) return false; + + if constexpr (Behavior == FullBehavior::Fail) { + if (isFull()) return false; + } + return xQueueSendToFront(getHandle(), &item, ticks_to_wait); } @@ -280,6 +319,11 @@ class Queue : public IQueue, public Policy { */ bool pushFromISR(const T& item, BaseType_t& task_woken) const { if (!isCreated()) return false; + + if constexpr (Behavior == FullBehavior::Fail) { + if (isFullFromISR()) return false; + } + return xQueueSendToFrontFromISR(getHandle(), &item, &task_woken); } @@ -291,6 +335,11 @@ class Queue : public IQueue, public Policy { */ bool add(const T& item, const TickType_t ticks_to_wait = portMAX_DELAY) const { if (!isCreated()) return false; + + if constexpr (Behavior == FullBehavior::Fail) { + if (isFull()) return false; + } + return xQueueSendToBack(getHandle(), &item, ticks_to_wait); } @@ -303,6 +352,11 @@ class Queue : public IQueue, public Policy { */ bool addFromISR(const T& item, BaseType_t& task_woken) const { if (!isCreated()) return false; + + if constexpr (Behavior == FullBehavior::Fail) { + if (isFullFromISR()) return false; + } + return xQueueSendToBackFromISR(getHandle(), &item, &task_woken); } @@ -376,13 +430,14 @@ class Queue : public IQueue, public Policy { } // namespace Internal -template -using QueueDynamic = Internal::Queue, T>; +template +using QueueDynamic = Internal::Queue, T, Behavior>; -template -using QueueStatic = Internal::Queue, T>; +template +using QueueStatic = Internal::Queue, T, Behavior>; -template -using QueueExternalStorage = Internal::Queue, T>; +template +using QueueExternalStorage = + Internal::Queue, T, Behavior>; } // namespace RTOS::Queues \ No newline at end of file diff --git a/src/RTOScppQueueSet.h b/src/RTOScppQueueSet.h index 76477d8..a0b9d08 100644 --- a/src/RTOScppQueueSet.h +++ b/src/RTOScppQueueSet.h @@ -1,5 +1,5 @@ /** - * SPDX-FileCopyrightText: 2025 Maximiliano Ramirez + * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez * * SPDX-License-Identifier: MIT */ @@ -23,9 +23,12 @@ class QueueSet { * that are going to be added. Each lock will use 1 event. Each queue will use events equal up to * the queue length. Each ringbuffer will use the number of messages capable of holding at a * certain time, not the length of it. + * @param name Name of the queue set. Defaults to nullptr. */ - QueueSet(const UBaseType_t queue_length) - : _handle(xQueueCreateSet(queue_length)) {} + + QueueSet(const UBaseType_t queue_length, const char* name = nullptr) + : _handle(xQueueCreateSet(queue_length)) + , _name(name ? name : "RtosQueueSet") {} ~QueueSet() { if (_handle) vQueueDelete(_handle); @@ -43,6 +46,12 @@ class QueueSet { */ QueueSetHandle_t getHandle() const { return _handle; } + /** + * @brief Get the name of the queue set. Useful for debugging and logging purposes. + * @return const char* Name of the queue set. Default is "RtosQueueSet" if no name is provided. + */ + const char* getName() const { return _name; } + /** * @brief Check if the queue set is created. * @return true Queue set is created. @@ -144,6 +153,7 @@ class QueueSet { private: QueueSetHandle_t _handle; + const char* _name; }; } // namespace RTOS::QueueSets \ No newline at end of file diff --git a/src/RTOScppRingBuffer.h b/src/RTOScppRingBuffer.h index 54cd8bd..a16bf55 100644 --- a/src/RTOScppRingBuffer.h +++ b/src/RTOScppRingBuffer.h @@ -1,5 +1,5 @@ /** - * SPDX-FileCopyrightText: 2025 Maximiliano Ramirez + * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez * * SPDX-License-Identifier: MIT */ @@ -31,6 +31,12 @@ class IRingBuffer { */ virtual RingbufHandle_t getHandle() const = 0; + /** + * @brief Get the name of the ringbuffer. Useful for debugging and logging purposes. + * @return const char* Name of the ringbuffer. Default is "RtosRingBuffer" if no name is provided. + */ + virtual const char* getName() const = 0; + /** * @brief Check if the ringbuffer is created. * @return true Ringbuffer is created. @@ -70,17 +76,10 @@ namespace Internal { template class Policy { public: - /** - * @brief Get the low-level handle of the ringbuffer. Useful for direct FreeRTOS API calls. Use it - * with caution. - * @return RingbufHandle_t Ringbuffer handle, nullptr if the Ringbuffer is not created. - */ RingbufHandle_t getHandle() const { return _handle; } - /** - * @brief Check if the ringbuffer is created. - * @return true Ringbuffer is created. - */ + const char* getName() const { return _name; } + bool isCreated() const { return _handle != nullptr; } /** @@ -136,9 +135,11 @@ class Policy { protected: Policy() - : _handle(nullptr) {} + : _handle(nullptr) + , _name("RtosRingBuffer") {} RingbufHandle_t _handle; + const char* _name; }; // CRTP no-split base policy class @@ -173,7 +174,10 @@ class NoSplitPolicy : public Policy { template class NoSplitDynamicPolicy : public NoSplitPolicy, T> { public: - NoSplitDynamicPolicy() { this->_handle = xRingbufferCreate(Length, RINGBUF_TYPE_NOSPLIT); } + NoSplitDynamicPolicy(const char* name = nullptr) { + this->_handle = xRingbufferCreate(Length, RINGBUF_TYPE_NOSPLIT); + if (name) this->_name = name; + } }; // Policy for no-split ring buffer with static memory allocation @@ -184,9 +188,10 @@ class NoSplitStaticPolicy : public NoSplitPolicy, static constexpr size_t REQUIRED_SIZE = 4 * ((Length + 3) / 4); public: - NoSplitStaticPolicy() { + NoSplitStaticPolicy(const char* name = nullptr) { this->_handle = xRingbufferCreateStatic(REQUIRED_SIZE, RINGBUF_TYPE_NOSPLIT, _storage, &_ringbuf_buffer); + if (name) this->_name = name; } private: @@ -202,6 +207,10 @@ class NoSplitExternalStoragePolicy // Size aligned to nearest 4 bytes static constexpr size_t REQUIRED_SIZE = 4 * ((Length + 3) / 4); + NoSplitExternalStoragePolicy(const char* name = nullptr) { + if (name) this->_name = name; + } + /** * @brief Create the ring buffer with external storage. * @param buffer External storage buffer. @@ -275,8 +284,9 @@ class SplitDynamicPolicy : public SplitPolicy, T> static constexpr size_t REQUIRED_SIZE = 4 * ((Length + 3) / 4); public: - SplitDynamicPolicy() { + SplitDynamicPolicy(const char* name = nullptr) { this->_handle = xRingbufferCreate(REQUIRED_SIZE, RINGBUF_TYPE_ALLOWSPLIT); + if (name) this->_name = name; } }; @@ -288,9 +298,10 @@ class SplitStaticPolicy : public SplitPolicy, T> { static constexpr size_t REQUIRED_SIZE = 4 * ((Length + 3) / 4); public: - SplitStaticPolicy() { + SplitStaticPolicy(const char* name = nullptr) { this->_handle = xRingbufferCreateStatic(REQUIRED_SIZE, RINGBUF_TYPE_ALLOWSPLIT, _storage, &_ringbuf_buffer); + if (name) this->_name = name; } private: @@ -305,6 +316,10 @@ class SplitExternalStoragePolicy : public SplitPolicy_name = name; + } + /** * @brief Create the ring buffer with external storage. * @param buffer External storage buffer. @@ -363,16 +378,20 @@ class BytePolicy : public Policy { template class ByteDynamicPolicy : public BytePolicy> { public: - ByteDynamicPolicy() { this->_handle = xRingbufferCreate(Length, RINGBUF_TYPE_BYTEBUF); } + ByteDynamicPolicy(const char* name = nullptr) { + this->_handle = xRingbufferCreate(Length, RINGBUF_TYPE_BYTEBUF); + if (name) this->_name = name; + } }; // Policy for byte ring buffer with static memory allocation template class ByteStaticPolicy : public BytePolicy> { public: - ByteStaticPolicy() { + ByteStaticPolicy(const char* name = nullptr) { this->_handle = xRingbufferCreateStatic(Length, RINGBUF_TYPE_BYTEBUF, _storage, &_ringbuf_buffer); + if (name) this->_name = name; } private: @@ -386,6 +405,10 @@ class ByteExternalStoragePolicy : public BytePolicy_name = name; + } + /** * @brief Create the ring buffer with external storage. * @param buffer External storage buffer. @@ -420,6 +443,12 @@ class RingBuffer : public IRingBuffer, public Policy { */ RingbufHandle_t getHandle() const override { return Policy::getHandle(); } + /** + * @brief Get the name of the ringbuffer. Useful for debugging and logging purposes. + * @return const char* Name of the ringbuffer. Default is "RtosRingBuffer" if no name is provided. + */ + const char* getName() const override { return Policy::getName(); } + /** * @brief Check if the ringbuffer is created. * @return true Ringbuffer is created. diff --git a/src/RTOScppTask.h b/src/RTOScppTask.h index ee874db..ba0ac64 100644 --- a/src/RTOScppTask.h +++ b/src/RTOScppTask.h @@ -1,5 +1,5 @@ /** - * SPDX-FileCopyrightText: 2025 Maximiliano Ramirez + * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez * * SPDX-License-Identifier: MIT */ @@ -31,6 +31,13 @@ class ITask { */ virtual TaskHandle_t getHandle() const = 0; + /** + * @brief Get the name of the task. + * @return const char* Name of the task. Default is "RtosTask" if no name is provided or the name + * exceeds configMAX_TASK_NAME_LEN. + */ + virtual const char* getName() const = 0; + /** * @brief Create the task with the already set parameters. * @return true Task created. @@ -39,7 +46,8 @@ class ITask { /** * @brief Create the task with the specified parameters. - * @param name Task name + * @param name Task name. Default is "RtosTask" if no name is provided or the name exceeds + * configMAX_TASK_NAME_LEN. * @param function Task function * @param priority Task priority (0 to configMAX_PRIORITIES - 1) * @param parameters Task parameters @@ -80,12 +88,6 @@ class ITask { */ virtual bool abortDelay() const = 0; - /** - * @brief Get the name of the task. - * @return const char* Task name. - */ - virtual const char* getName() const = 0; - /** * @brief Get the parameters of the task. * @return void* Task parameters, nullptr if the task is not created or no parameters were set. @@ -249,7 +251,7 @@ class Policy { public: Policy() : _handle(nullptr) - , _name(nullptr) + , _name("RtosTask") , _function(nullptr) , _priority(0) , _parameters(nullptr) @@ -262,7 +264,8 @@ class Policy { bool setup(const char* name, TaskFunction_t function, uint8_t priority, void* parameters = nullptr, BaseType_t core = ARDUINO_RUNNING_CORE) { - _name = name; + if (name && strlen(name) <= configMAX_TASK_NAME_LEN) this->_name = name; + _parameters = parameters; _function = function; _priority = priority; @@ -272,8 +275,7 @@ class Policy { } bool isValid() const { - if (_name == nullptr || _function == nullptr || _priority >= configMAX_PRIORITIES || - !taskVALID_CORE_ID(_core)) + if (_function == nullptr || _priority >= configMAX_PRIORITIES || !taskVALID_CORE_ID(_core)) return false; return true; @@ -379,17 +381,25 @@ class Task : public ITask { * caution. * @return TaskHandle_t Task handle, nullptr if the task is not created. */ - TaskHandle_t getHandle() const { return _policy.getHandle(); } + TaskHandle_t getHandle() const override { return _policy.getHandle(); } + + /** + * @brief Get the name of the task. + * @return const char* Name of the task. Default is "RtosTask" if no name is provided or the name + * exceeds configMAX_TASK_NAME_LEN. + */ + const char* getName() const override { return _policy.getName(); } /** * @brief Create the task with the already set parameters. * @return true Task created. */ - bool create() { return _policy.create(); } + bool create() override { return _policy.create(); } /** * @brief Create the task with the specified parameters. - * @param name Task name + * @param name Task name. Default is "RtosTask" if no name is provided or the name exceeds + * configMAX_TASK_NAME_LEN. * @param function Task function * @param priority Task priority (0 to configMAX_PRIORITIES - 1) * @param parameters Task parameters @@ -397,7 +407,7 @@ class Task : public ITask { * @return true Task created. */ bool create(const char* name, TaskFunction_t function, uint8_t priority, - void* parameters = nullptr, uint8_t running_core = ARDUINO_RUNNING_CORE) { + void* parameters = nullptr, uint8_t running_core = ARDUINO_RUNNING_CORE) override { if (!_policy.setup(name, function, priority, parameters, running_core)) { return false; } @@ -409,13 +419,13 @@ class Task : public ITask { * @brief Check if the task is created. * @return true Task is created. */ - bool isCreated() const { return _policy.isCreated(); } + bool isCreated() const override { return _policy.isCreated(); } /** * @brief Suspend the task. * @return true Task suspended successfully, false if the task is not created. */ - bool suspend() const { + bool suspend() const override { if (!isCreated()) return false; vTaskSuspend(getHandle()); return true; @@ -425,7 +435,7 @@ class Task : public ITask { * @brief Resume the task. * @return true Task resumed successfully, false if the task is not created. */ - bool resume() const { + bool resume() const override { if (!isCreated()) return false; vTaskResume(getHandle()); return true; @@ -435,7 +445,7 @@ class Task : public ITask { * @brief Get the state of the task. * @return eTaskState Task state, eTaskState::eInvalid if the task is not created. */ - eTaskState getState() const { + eTaskState getState() const override { if (!isCreated()) return eTaskState::eInvalid; return eTaskGetState(getHandle()); } @@ -445,22 +455,16 @@ class Task : public ITask { * @return true Delay aborted successfully, false if the task is not created or the task is not in * the Blocked state. */ - bool abortDelay() const { + bool abortDelay() const override { if (!isCreated()) return false; return xTaskAbortDelay(getHandle()); } - /** - * @brief Get the name of the task. - * @return const char* Task name. - */ - const char* getName() const { return _policy.getName(); } - /** * @brief Get the parameters of the task. * @return void* Task parameters, nullptr if the task is not created or no parameters were set. */ - void* getParameters() const { + void* getParameters() const override { if (!isCreated()) return nullptr; return _policy.getParameters(); } @@ -469,7 +473,7 @@ class Task : public ITask { * @brief Get the core where the task is running. * @return uint8_t Core number, 0xFF if the task is not created. */ - uint8_t getCore() const { + uint8_t getCore() const override { if (!isCreated()) return 0xFF; return _policy.getCore(); } @@ -479,7 +483,7 @@ class Task : public ITask { * @param priority Task priority (0 to configMAX_PRIORITIES - 1) * @return true Priority set successfully, false if the task is not created. */ - bool setPriority(const uint8_t priority) { + bool setPriority(const uint8_t priority) override { if (!isCreated()) return false; vTaskPrioritySet(getHandle(), priority); return true; @@ -489,7 +493,7 @@ class Task : public ITask { * @brief Get the priority of the task. * @return uint8_t Task priority, 0xFF if the task is not created. */ - uint8_t getPriority() const { + uint8_t getPriority() const override { if (!isCreated()) return 0xFF; return uxTaskPriorityGet(getHandle()); } @@ -498,7 +502,7 @@ class Task : public ITask { * @brief Get the priority of the task from an ISR. * @return uint8_t Task priority, 0xFF if the task is not created. */ - uint8_t getPriorityFromISR() const { + uint8_t getPriorityFromISR() const override { if (!isCreated()) return 0xFF; return uxTaskPriorityGetFromISR(getHandle()); } @@ -507,7 +511,7 @@ class Task : public ITask { * @brief Get the stack size of the task. * @return uint32_t Stack size. */ - uint32_t getStackSize() const { return Policy::_stack_size; } + uint32_t getStackSize() const override { return Policy::_stack_size; } /** * @brief Notify the task. @@ -516,7 +520,7 @@ class Task : public ITask { * @return true Notification sent successfully, false if the task is not created or failed to send * the notification. */ - bool notify(const uint32_t value, const eNotifyAction action) const { + bool notify(const uint32_t value, const eNotifyAction action) const override { if (!isCreated()) return false; return xTaskNotify(getHandle(), value, action); } @@ -531,7 +535,7 @@ class Task : public ITask { * the notification. */ bool notifyFromISR(const uint32_t value, const eNotifyAction action, - BaseType_t& task_woken) const { + BaseType_t& task_woken) const override { if (!isCreated()) return false; return xTaskNotifyFromISR(getHandle(), value, action, &task_woken); } @@ -544,7 +548,8 @@ class Task : public ITask { * @return true Notification sent successfully, false if the task is not created or failed to send * the notification. */ - bool notifyAndQuery(const uint32_t value, const eNotifyAction action, uint32_t& old_value) const { + bool notifyAndQuery(const uint32_t value, const eNotifyAction action, + uint32_t& old_value) const override { if (!isCreated()) return false; return xTaskNotifyAndQuery(getHandle(), value, action, &old_value); } @@ -560,7 +565,7 @@ class Task : public ITask { * the notification. */ bool notifyAndQueryFromISR(const uint32_t value, const eNotifyAction action, uint32_t& old_value, - BaseType_t& task_woken) const { + BaseType_t& task_woken) const override { if (!isCreated()) return false; return xTaskNotifyAndQueryFromISR(getHandle(), value, action, &old_value, &task_woken); } @@ -570,7 +575,7 @@ class Task : public ITask { * notification value by 1. The task can wait for the notification using notifyTake(). * @return true Notification sent successfully, false if the task is not created. */ - bool notifyGive() const { + bool notifyGive() const override { if (!isCreated()) return false; return xTaskNotifyGive(getHandle()); } @@ -583,7 +588,7 @@ class Task : public ITask { * the ISR. * @return true Notification sent successfully, false if the task is not created. */ - bool notifyGiveFromISR(BaseType_t& task_woken) const { + bool notifyGiveFromISR(BaseType_t& task_woken) const override { if (!isCreated()) return false; vTaskNotifyGiveFromISR(getHandle(), &task_woken); return true; @@ -597,7 +602,7 @@ class Task : public ITask { * @param ticks_to_wait Maximum time to wait for the notification. * @return uint32_t Value of the notification. */ - uint32_t notifyTake(const bool clear, const TickType_t ticks_to_wait) const { + uint32_t notifyTake(const bool clear, const TickType_t ticks_to_wait) const override { if (!isCreated()) return 0; return ulTaskNotifyTake(clear, ticks_to_wait); } @@ -613,7 +618,7 @@ class Task : public ITask { * receive the notification. */ bool notifyWait(const uint32_t clear_on_entry, const uint32_t clear_on_exit, uint32_t& value, - const TickType_t ticks_to_wait) const { + const TickType_t ticks_to_wait) const override { if (!isCreated()) return false; return xTaskNotifyWait(clear_on_entry, clear_on_exit, &value, ticks_to_wait); } @@ -623,7 +628,7 @@ class Task : public ITask { * stack usage. * @return true Stack statistics updated successfully, false if the task is not created. */ - bool updateStackStats() { + bool updateStackStats() override { if (!isCreated()) return false; _stack_used = Policy::_stack_size - uxTaskGetStackHighWaterMark(getHandle()); @@ -637,7 +642,7 @@ class Task : public ITask { * @brief Get the stack used by the task. * @return uint32_t Stack used. */ - uint32_t getStackUsed() const { + uint32_t getStackUsed() const override { if (!isCreated()) return 0; return _stack_used; } @@ -646,7 +651,7 @@ class Task : public ITask { * @brief Get the minimum stack used by the task. * @return uint32_t Minimum stack used. */ - uint32_t getStackMinUsed() const { + uint32_t getStackMinUsed() const override { if (!isCreated()) return 0; return _stack_min; } @@ -655,7 +660,7 @@ class Task : public ITask { * @brief Get the maximum stack used by the task. * @return uint32_t Maximum stack used. */ - uint32_t getStackMaxUsed() const { + uint32_t getStackMaxUsed() const override { if (!isCreated()) return 0; return _stack_max; } @@ -664,7 +669,7 @@ class Task : public ITask { * @brief Check if the task is created. * @return true Task is created. */ - explicit operator bool() const { return isCreated(); } + explicit operator bool() const override { return isCreated(); } private: Policy _policy; diff --git a/src/RTOScppTimer.h b/src/RTOScppTimer.h index 8ca4d4c..5644901 100644 --- a/src/RTOScppTimer.h +++ b/src/RTOScppTimer.h @@ -1,5 +1,5 @@ /** - * SPDX-FileCopyrightText: 2025 Maximiliano Ramirez + * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez * * SPDX-License-Identifier: MIT */ @@ -31,9 +31,15 @@ class ITimer { */ virtual TimerHandle_t getHandle() const = 0; + /** + * @brief Get the name of the timer. Useful for debugging and logging purposes. + * @return const char* Name of the timer, "RtosTimer" if the timer has no name. + */ + virtual const char* getName() const = 0; + /** * @brief Create the timer with the specified parameters. - * @param name Timer name + * @param name Timer name. If nullptr, the timer will be named "RtosTimer". * @param callback Timer callback function * @param period Timer period in ticks (can't be 0) * @param id Timer ID (nullptr if not used) @@ -101,12 +107,6 @@ class ITimer { */ virtual bool resetFromISR(BaseType_t& task_woken) const = 0; - /** - * @brief Get the timer name. - * @return const char* Timer name, nullptr if the timer is not created. - */ - virtual const char* getName() const = 0; - /** * @brief Get the time left for the timer to expire. * @return TickType_t Time left for the timer to expire, 0 if the timer is not created. @@ -179,29 +179,36 @@ template class Policy { public: Policy() - : _handle(nullptr) {} + : _handle(nullptr) + , _name("RtosTimer") {} TimerHandle_t getHandle() const { return _handle; } + const char* getName() const { return _name; } bool create(const char* name, TimerCallbackFunction_t callback, const TickType_t period, void* id, const bool auto_reload, const bool start) { if (_handle != nullptr) return true; + if (callback == nullptr || period == 0) return false; + if (name) _name = name; // Update name if provided - if (name == nullptr || callback == nullptr || period == 0) return false; - - return static_cast(this)->createImpl(name, callback, period, id, auto_reload, start); + return static_cast(this)->createImpl(callback, period, id, auto_reload, start); } protected: TimerHandle_t _handle; + const char* _name; }; // Policy for timer with dynamic memory allocation class DynamicPolicy : public Policy { public: - bool createImpl(const char* name, TimerCallbackFunction_t callback, const TickType_t period, - void* id, const bool auto_reload, const bool start) { - this->_handle = xTimerCreate(name, period, auto_reload, id, callback); + DynamicPolicy(const char* name = nullptr) { + if (name) this->_name = name; + } + + bool createImpl(TimerCallbackFunction_t callback, const TickType_t period, void* id, + const bool auto_reload, const bool start) { + this->_handle = xTimerCreate(this->_name, period, auto_reload, id, callback); if (this->_handle == nullptr) return false; @@ -214,9 +221,14 @@ class DynamicPolicy : public Policy { // Policy for timer with static memory allocation class StaticPolicy : public Policy { public: - bool createImpl(const char* name, TimerCallbackFunction_t callback, const TickType_t period, - void* id, const bool auto_reload, const bool start) { - this->_handle = xTimerCreateStatic(name, period, auto_reload, id, callback, &_timer_buffer); + StaticPolicy(const char* name = nullptr) { + if (name) this->_name = name; + } + + bool createImpl(TimerCallbackFunction_t callback, const TickType_t period, void* id, + const bool auto_reload, const bool start) { + this->_handle = + xTimerCreateStatic(this->_name, period, auto_reload, id, callback, &_timer_buffer); if (this->_handle == nullptr) return false; @@ -236,11 +248,12 @@ class Timer : public ITimer { /** * @brief Instantiate a empty Timer object. You need to call create(parameters) before using it. */ - Timer() = default; + Timer(const char* name = nullptr) + : _policy(name) {} /** * @brief Instantiate a Timer object with the specified parameters. - * @param name Timer name + * @param name Timer name. If nullptr, the timer will be named "RtosTimer". * @param callback Timer callback function * @param period Timer period in ticks (can't be 0) * @param id Timer ID (nullptr if not used) @@ -261,7 +274,13 @@ class Timer : public ITimer { * caution. * @return TimerHandle_t Timer handle, nullptr if the timer is not created. */ - TimerHandle_t getHandle() const { return _policy.getHandle(); } + TimerHandle_t getHandle() const override { return _policy.getHandle(); } + + /** + * @brief Get the name of the timer. Useful for debugging and logging purposes. + * @return const char* Name of the timer, "RtosTimer" if the timer has no name. + */ + const char* getName() const override { return _policy.getName(); } /** * @brief Create the timer with the specified parameters. @@ -274,7 +293,7 @@ class Timer : public ITimer { * @return true Timer created. */ bool create(const char* name, TimerCallbackFunction_t callback, const TickType_t period, void* id, - const bool auto_reload, const bool start) { + const bool auto_reload, const bool start) override { return _policy.create(name, callback, period, id, auto_reload, start); } @@ -282,14 +301,14 @@ class Timer : public ITimer { * @brief Check if the timer is created. * @return true Timer is created. */ - bool isCreated() const { return getHandle() != nullptr; } + bool isCreated() const override { return getHandle() != nullptr; } /** * @brief Start the timer. * @param ticks_to_wait Maximum time to wait for the operation to complete. * @return true Timer started successfully, false if the timer is not created or failed to start. */ - bool start(const TickType_t ticks_to_wait = portMAX_DELAY) const { + bool start(const TickType_t ticks_to_wait = portMAX_DELAY) const override { if (!isCreated()) return false; return xTimerStart(getHandle(), ticks_to_wait); } @@ -300,7 +319,7 @@ class Timer : public ITimer { * the ISR. * @return true Timer started successfully, false if the timer is not created or failed to start. */ - bool startFromISR(BaseType_t& task_woken) const { + bool startFromISR(BaseType_t& task_woken) const override { if (!isCreated()) return false; return xTimerStartFromISR(getHandle(), &task_woken); } @@ -310,7 +329,7 @@ class Timer : public ITimer { * @param ticks_to_wait Maximum time to wait for the operation to complete. * @return true Timer stopped successfully, false if the timer is not created or failed to stop. */ - bool stop(const TickType_t ticks_to_wait = portMAX_DELAY) const { + bool stop(const TickType_t ticks_to_wait = portMAX_DELAY) const override { if (!isCreated()) return false; return xTimerStop(getHandle(), ticks_to_wait); } @@ -321,7 +340,7 @@ class Timer : public ITimer { * the ISR. * @return true Timer stopped successfully, false if the timer is not created or failed to stop. */ - bool stopFromISR(BaseType_t& task_woken) const { + bool stopFromISR(BaseType_t& task_woken) const override { if (!isCreated()) return false; return xTimerStopFromISR(getHandle(), &task_woken); } @@ -330,7 +349,7 @@ class Timer : public ITimer { * @brief Check if the timer is active. * @return true Timer is active. */ - bool isActive() const { + bool isActive() const override { if (!isCreated()) return false; return xTimerIsTimerActive(getHandle()); } @@ -340,7 +359,7 @@ class Timer : public ITimer { * @param ticks_to_wait Maximum time to wait for the operation to complete. * @return true Timer reset successfully, false if the timer is not created or failed to reset. */ - bool reset(const TickType_t ticks_to_wait = portMAX_DELAY) const { + bool reset(const TickType_t ticks_to_wait = portMAX_DELAY) const override { if (!isCreated()) return false; return xTimerReset(getHandle(), ticks_to_wait); } @@ -351,25 +370,16 @@ class Timer : public ITimer { * the ISR. * @return true Timer reset successfully, false if the timer is not created or failed to reset. */ - bool resetFromISR(BaseType_t& task_woken) const { + bool resetFromISR(BaseType_t& task_woken) const override { if (!isCreated()) return false; return xTimerResetFromISR(getHandle(), &task_woken); } - /** - * @brief Get the timer name. - * @return const char* Timer name, nullptr if the timer is not created. - */ - const char* getName() const { - if (!isCreated()) return nullptr; - return pcTimerGetName(getHandle()); - } - /** * @brief Get the time left for the timer to expire. * @return TickType_t Time left for the timer to expire, 0 if the timer is not created. */ - TickType_t getExpiryTime() const { + TickType_t getExpiryTime() const override { if (!isCreated()) return 0; return xTimerGetExpiryTime(getHandle()) - xTaskGetTickCount(); } @@ -381,7 +391,8 @@ class Timer : public ITimer { * @return true Timer period set successfully, false if the timer is not created or failed to set * the period. */ - bool setPeriod(const TickType_t period, const TickType_t ticks_to_wait = portMAX_DELAY) const { + bool setPeriod(const TickType_t period, + const TickType_t ticks_to_wait = portMAX_DELAY) const override { if (!isCreated()) return false; return xTimerChangePeriod(getHandle(), period, ticks_to_wait); } @@ -394,7 +405,7 @@ class Timer : public ITimer { * @return true Timer period set successfully, false if the timer is not created or failed to set * the period. */ - bool setPeriodFromISR(const TickType_t period, BaseType_t& task_woken) const { + bool setPeriodFromISR(const TickType_t period, BaseType_t& task_woken) const override { if (!isCreated()) return false; return xTimerChangePeriodFromISR(getHandle(), period, &task_woken); } @@ -403,7 +414,7 @@ class Timer : public ITimer { * @brief Get the timer period. * @return TickType_t Timer period in ticks, 0 if the timer is not created. */ - TickType_t getPeriod() const { + TickType_t getPeriod() const override { if (!isCreated()) return 0; return xTimerGetPeriod(getHandle()); } @@ -413,7 +424,7 @@ class Timer : public ITimer { * @param id Timer ID. * @return true Timer ID set successfully, false if the timer is not created. */ - bool setTimerID(void* id) const { + bool setTimerID(void* id) const override { if (!isCreated()) return false; vTimerSetTimerID(getHandle(), id); return true; @@ -423,7 +434,7 @@ class Timer : public ITimer { * @brief Get the timer ID. * @return void* Timer ID, nullptr if the timer is not created. */ - void* getTimerID() const { + void* getTimerID() const override { if (!isCreated()) return nullptr; return pvTimerGetTimerID(getHandle()); } @@ -433,7 +444,7 @@ class Timer : public ITimer { * @param auto_reload Timer auto reload mode. * @return true Timer reload mode set successfully, false if the timer is not created. */ - bool setReloadMode(const bool auto_reload) const { + bool setReloadMode(const bool auto_reload) const override { if (!isCreated()) return false; vTimerSetReloadMode(getHandle(), auto_reload); return true; @@ -443,7 +454,7 @@ class Timer : public ITimer { * @brief Get the timer reload mode. * @return bool Timer auto reload mode, false if the timer is not created. */ - bool getReloadMode() const { + bool getReloadMode() const override { if (!isCreated()) return false; return uxTimerGetReloadMode(getHandle()); } @@ -452,7 +463,7 @@ class Timer : public ITimer { * @brief Check if the timer is created. * @return true Timer is created. */ - explicit operator bool() const { return isCreated(); } + explicit operator bool() const override { return isCreated(); } private: Policy _policy; diff --git a/test/test_buffers/test_buffers.cpp b/test/test_buffers/test_buffers.cpp index 40c6a7c..d75d77b 100644 --- a/test/test_buffers/test_buffers.cpp +++ b/test/test_buffers/test_buffers.cpp @@ -3,7 +3,6 @@ #include "RTOScppBuffer.h" using namespace RTOS::Buffers; -static constexpr const char* tag = "test_buffers"; /* ---------------------------------------- Data Buffers ---------------------------------------- */ static char tx_buffer[] = "123456789"; @@ -31,6 +30,10 @@ void test_sb_creation() { TEST_ASSERT_NOT_NULL(ext_buffer); TEST_ASSERT_TRUE(sb_ext.create(ext_buffer)); + + TEST_ASSERT_EQUAL_STRING("RtosBuffer", sb_dyn.getName()); + TEST_ASSERT_EQUAL_STRING("RtosBuffer", sb_st.getName()); + TEST_ASSERT_EQUAL_STRING("RtosBuffer", sb_ext.getName()); } void test_sb_send_receive() { @@ -106,6 +109,10 @@ void test_mb_creation() { TEST_ASSERT_NOT_NULL(ext_buffer); TEST_ASSERT_TRUE(mb_ext.create(ext_buffer)); + + TEST_ASSERT_EQUAL_STRING("RtosBuffer", mb_dyn.getName()); + TEST_ASSERT_EQUAL_STRING("RtosBuffer", mb_st.getName()); + TEST_ASSERT_EQUAL_STRING("RtosBuffer", mb_ext.getName()); } void test_mb_send_receive() { @@ -138,32 +145,27 @@ void test_mb_send_receive() { TEST_ASSERT_EQUAL_STRING(tx_buffer, rx_buffer); TEST_ASSERT_TRUE(mb_ext.isEmpty()); } -/* ---------------------------------------------------------------------------------------------- */ - -/* ------------------------------------------ USB Logs ------------------------------------------ */ -SemaphoreHandle_t log_mutex = nullptr; - -int redirectLogs(const char* str, va_list list) { - if (log_mutex != nullptr) xSemaphoreTake(log_mutex, portMAX_DELAY); - static char buffer[2048]; - int ret = vsnprintf(buffer, sizeof(buffer), str, list); - Serial.write(buffer); +void test_custom_names() { + static constexpr const char* name = "CustomName"; - if (log_mutex != nullptr) xSemaphoreGive(log_mutex); + static StreamBufferDynamic sb_dyn_named(name); + static StreamBufferStatic sb_st_named(name); + static MessageBufferDynamic mb_dyn_named(name); + static MessageBufferStatic mb_st_named(name); - return ret; + TEST_ASSERT_EQUAL_STRING(name, sb_dyn_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, sb_st_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, mb_dyn_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, mb_st_named.getName()); } /* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */ void setup() { - log_mutex = xSemaphoreCreateMutex(); - esp_log_set_vprintf(redirectLogs); - esp_log_level_set("*", ESP_LOG_VERBOSE); - delay(3000); + Serial.begin(115200); + delay(1000); - ESP_LOGI(tag, "Running tests..."); UNITY_BEGIN(); RUN_TEST(test_sb_creation); @@ -172,10 +174,10 @@ void setup() { RUN_TEST(test_sb_change_trigger_level); RUN_TEST(test_mb_creation); RUN_TEST(test_mb_send_receive); + RUN_TEST(test_custom_names); - ESP_LOGI(tag, "Finishing tests..."); UNITY_END(); } -void loop() { vTaskDelete(nullptr); } +void loop() {} /* ---------------------------------------------------------------------------------------------- */ \ No newline at end of file diff --git a/test/test_locks/test_locks.cpp b/test/test_locks/test_locks.cpp index 1a7d636..88441dd 100644 --- a/test/test_locks/test_locks.cpp +++ b/test/test_locks/test_locks.cpp @@ -1,4 +1,3 @@ -#include #include #include "RTOScppLock.h" @@ -6,7 +5,6 @@ #include using namespace RTOS::Locks; -static constexpr const char* tag = "test_locks"; /* -------------------------------------------- Locks ------------------------------------------- */ MutexDynamic mutex_dyn; @@ -113,32 +111,35 @@ void test_semaphore_counting() { TEST_ASSERT_EQUAL(sem_count, count_dyn); TEST_ASSERT_EQUAL(sem_count, count_st); } -/* ---------------------------------------------------------------------------------------------- */ - -/* ------------------------------------------ USB Logs ------------------------------------------ */ -SemaphoreHandle_t log_mutex = nullptr; - -int redirectLogs(const char* str, va_list list) { - if (log_mutex != nullptr) xSemaphoreTake(log_mutex, portMAX_DELAY); - - static char buffer[2048]; - int ret = vsnprintf(buffer, sizeof(buffer), str, list); - Serial.write(buffer); - - if (log_mutex != nullptr) xSemaphoreGive(log_mutex); - return ret; +void test_custom_names() { + static constexpr const char* name = "CustomLock"; + + static MutexDynamic mutex_dyn_named(name); + static MutexStatic mutex_st_named(name); + static MutexRecursiveDynamic mutex_rec_dyn_named(name); + static MutexRecursiveStatic mutex_rec_st_named(name); + static SemBinaryDynamic sem_bin_dyn_named(name); + static SemBinaryStatic sem_bin_st_named(name); + static SemCountingDynamic sem_count_dyn_named(name); + static SemCountingStatic sem_count_st_named(name); + + TEST_ASSERT_EQUAL_STRING(name, mutex_dyn_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, mutex_st_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, mutex_rec_dyn_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, mutex_rec_st_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, sem_bin_dyn_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, sem_bin_st_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, sem_count_dyn_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, sem_count_st_named.getName()); } /* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */ void setup() { - log_mutex = xSemaphoreCreateMutex(); - esp_log_set_vprintf(redirectLogs); - esp_log_level_set("*", ESP_LOG_VERBOSE); - delay(3000); + Serial.begin(115200); + delay(1000); - ESP_LOGI(tag, "Running tests..."); UNITY_BEGIN(); RUN_TEST(test_locks_creation); @@ -146,10 +147,10 @@ void setup() { RUN_TEST(test_mutex_recursive); RUN_TEST(test_semaphore_binary); RUN_TEST(test_semaphore_counting); + RUN_TEST(test_custom_names); - ESP_LOGI(tag, "Finishing tests..."); UNITY_END(); } -void loop() { vTaskDelete(nullptr); } +void loop() {} /* ---------------------------------------------------------------------------------------------- */ \ No newline at end of file diff --git a/test/test_queues/test_queues.cpp b/test/test_queues/test_queues.cpp index 047993f..7781e3f 100644 --- a/test/test_queues/test_queues.cpp +++ b/test/test_queues/test_queues.cpp @@ -3,7 +3,6 @@ #include "RTOScppQueue.h" using namespace RTOS::Queues; -static constexpr const char* tag = "test_queues"; /* ------------------------------------------- Queues ------------------------------------------- */ static constexpr uint32_t queue_size = 3; @@ -13,9 +12,6 @@ QueueDynamic q_dyn; QueueStatic q_st; QueueExternalStorage q_ext; -static uint32_t available_msgs; -static uint32_t available_spaces; - // Queue of length 1 to test overwrite QueueStatic q_overwrite; /* ---------------------------------------------------------------------------------------------- */ @@ -30,6 +26,11 @@ void test_queues_creation() { TEST_ASSERT_TRUE(q_ext.create(ext_buffer)); TEST_ASSERT_TRUE(q_overwrite); + + TEST_ASSERT_EQUAL_STRING("RtosQueue", q_dyn.getName()); + TEST_ASSERT_EQUAL_STRING("RtosQueue", q_st.getName()); + TEST_ASSERT_EQUAL_STRING("RtosQueue", q_ext.getName()); + TEST_ASSERT_EQUAL_STRING("RtosQueue", q_overwrite.getName()); } void test_queues_full_empty() { @@ -201,32 +202,47 @@ void test_queue_overwrite() { TEST_ASSERT_EQUAL(0, q_overwrite.getAvailableSpaces()); TEST_ASSERT_EQUAL(2, item_to_peek); } -/* ---------------------------------------------------------------------------------------------- */ -/* ------------------------------------------ USB Logs ------------------------------------------ */ -SemaphoreHandle_t log_mutex = nullptr; +void test_queue_full_behavior_fail() { + static QueueStatic q_fail; + TEST_ASSERT_TRUE(q_fail); + + TEST_ASSERT_TRUE(q_fail.add(1)); + TEST_ASSERT_TRUE(q_fail.add(2)); + TEST_ASSERT_FALSE(q_fail.add(3)); + TEST_ASSERT_FALSE(q_fail.push(3)); -int redirectLogs(const char* str, va_list list) { - if (log_mutex != nullptr) xSemaphoreTake(log_mutex, portMAX_DELAY); + uint8_t item = 0; + TEST_ASSERT_TRUE(q_fail.pop(item)); + TEST_ASSERT_TRUE(q_fail.add(3)); + TEST_ASSERT_FALSE(q_fail.add(4)); + TEST_ASSERT_FALSE(q_fail.push(4)); +} + +void test_custom_names() { + static constexpr const char* name = "CustomQueue"; - static char buffer[2048]; - int ret = vsnprintf(buffer, sizeof(buffer), str, list); - Serial.write(buffer); + static QueueDynamic q_dyn_named(name); + static QueueStatic q_st_named(name); + + static uint8_t* ext_buffer = + static_cast(malloc(QueueExternalStorage::REQUIRED_SIZE)); + TEST_ASSERT_NOT_NULL(ext_buffer); - if (log_mutex != nullptr) xSemaphoreGive(log_mutex); + static QueueExternalStorage q_ext_named(name); + TEST_ASSERT_TRUE(q_ext_named.create(ext_buffer)); - return ret; + TEST_ASSERT_EQUAL_STRING(name, q_dyn_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, q_st_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, q_ext_named.getName()); } /* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */ void setup() { - log_mutex = xSemaphoreCreateMutex(); - esp_log_set_vprintf(redirectLogs); - esp_log_level_set("*", ESP_LOG_VERBOSE); - delay(3000); + Serial.begin(115200); + delay(1000); - ESP_LOGI(tag, "Running tests..."); UNITY_BEGIN(); RUN_TEST(test_queues_creation); @@ -235,10 +251,11 @@ void setup() { RUN_TEST(test_queues_push); RUN_TEST(test_queues_peek); RUN_TEST(test_queue_overwrite); + RUN_TEST(test_queue_full_behavior_fail); + RUN_TEST(test_custom_names); - ESP_LOGI(tag, "Finishing tests..."); UNITY_END(); } -void loop() { vTaskDelete(nullptr); } +void loop() {} /* ---------------------------------------------------------------------------------------------- */ \ No newline at end of file diff --git a/test/test_queuesets/test_queuesets.cpp b/test/test_queuesets/test_queuesets.cpp index 0a5b510..cb40b09 100644 --- a/test/test_queuesets/test_queuesets.cpp +++ b/test/test_queuesets/test_queuesets.cpp @@ -1,4 +1,3 @@ -#include #include #include "RTOScppQueueSet.h" @@ -7,7 +6,6 @@ using namespace RTOS::QueueSets; using namespace RTOS::Locks; using namespace RTOS::Queues; using namespace RTOS::RingBuffers; -static constexpr const char* tag = "test_queue_set"; /* ------------------------------------- Items for Queue Set ------------------------------------ */ QueueStatic queue; @@ -24,6 +22,8 @@ void test_objects_creation() { TEST_ASSERT_TRUE(rbuffer); TEST_ASSERT_TRUE(sem); TEST_ASSERT_TRUE(q_set); + + TEST_ASSERT_EQUAL_STRING("RtosQueueSet", q_set.getName()); } void test_add_objects_to_queueset() { @@ -73,46 +73,33 @@ void test_remove_objects_from_queueset() { TEST_ASSERT_TRUE(q_set.remove(rbuffer)); TEST_ASSERT_TRUE(q_set.remove(sem)); } -/* ---------------------------------------------------------------------------------------------- */ - -/* ------------------------------------------ USB Logs ------------------------------------------ */ -SemaphoreHandle_t log_mutex = nullptr; -int redirectLogs(const char* str, va_list list) { - if (log_mutex != nullptr) xSemaphoreTake(log_mutex, portMAX_DELAY); +void test_custom_names() { + static constexpr const char* name = "CustomQueueSet"; - static char buffer[2048]; - int ret = vsnprintf(buffer, sizeof(buffer), str, list); - Serial.write(buffer); - - if (log_mutex != nullptr) xSemaphoreGive(log_mutex); - - return ret; + static QueueSet q_set_named(3, name); + TEST_ASSERT_TRUE(q_set_named); + TEST_ASSERT_EQUAL_STRING(name, q_set_named.getName()); } /* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */ void setup() { - log_mutex = xSemaphoreCreateMutex(); - esp_log_set_vprintf(redirectLogs); - esp_log_level_set("*", ESP_LOG_VERBOSE); Serial.begin(115200); - delay(2000); + delay(1000); - ESP_LOGI(tag, "Running tests..."); UNITY_BEGIN(); RUN_TEST(test_objects_creation); - RUN_TEST(test_add_objects_to_queueset); RUN_TEST(test_select_queue_from_queueset); RUN_TEST(test_select_ringbuffer_from_queueset); RUN_TEST(test_select_semaphore_from_queueset); RUN_TEST(test_remove_objects_from_queueset); + RUN_TEST(test_custom_names); - ESP_LOGI(tag, "Finishing tests..."); UNITY_END(); } -void loop() { vTaskDelete(nullptr); } +void loop() {} /* ---------------------------------------------------------------------------------------------- */ \ No newline at end of file diff --git a/test/test_ringbuffers/test_ringbuffers.cpp b/test/test_ringbuffers/test_ringbuffers.cpp index 59b920a..86ac807 100644 --- a/test/test_ringbuffers/test_ringbuffers.cpp +++ b/test/test_ringbuffers/test_ringbuffers.cpp @@ -1,10 +1,8 @@ -#include #include #include "RTOScppRingBuffer.h" using namespace RTOS::RingBuffers; -static constexpr const char* tag = "test_ringbufs"; /* ----------------------------------------- RingBuffers ---------------------------------------- */ static constexpr uint8_t rb_len = 64; @@ -48,6 +46,16 @@ void test_rb_creation() { rb_byte_ext_buffer = static_cast(malloc(rb_byte_ext.REQUIRED_SIZE)); TEST_ASSERT_NOT_NULL(rb_byte_ext_buffer); TEST_ASSERT_TRUE(rb_byte_ext.create(rb_byte_ext_buffer)); + + TEST_ASSERT_EQUAL_STRING("RtosRingBuffer", rb_nosp_dyn.getName()); + TEST_ASSERT_EQUAL_STRING("RtosRingBuffer", rb_nosp_st.getName()); + TEST_ASSERT_EQUAL_STRING("RtosRingBuffer", rb_nosp_ext.getName()); + TEST_ASSERT_EQUAL_STRING("RtosRingBuffer", rb_sp_dyn.getName()); + TEST_ASSERT_EQUAL_STRING("RtosRingBuffer", rb_sp_st.getName()); + TEST_ASSERT_EQUAL_STRING("RtosRingBuffer", rb_sp_ext.getName()); + TEST_ASSERT_EQUAL_STRING("RtosRingBuffer", rb_byte_dyn.getName()); + TEST_ASSERT_EQUAL_STRING("RtosRingBuffer", rb_byte_st.getName()); + TEST_ASSERT_EQUAL_STRING("RtosRingBuffer", rb_byte_ext.getName()); } void test_rb_nosplit_send_recv() { @@ -318,42 +326,70 @@ void test_rb_byte_send_recv() { TEST_ASSERT_EQUAL_MEMORY(item_to_send, item_recv, item_recv_size); rb_byte_ext.returnItem(item_recv); } -/* ---------------------------------------------------------------------------------------------- */ - -/* ------------------------------------------ USB Logs ------------------------------------------ */ -SemaphoreHandle_t log_mutex = nullptr; - -int redirectLogs(const char* str, va_list list) { - if (log_mutex != nullptr) xSemaphoreTake(log_mutex, portMAX_DELAY); - - static char buffer[2048]; - int ret = vsnprintf(buffer, sizeof(buffer), str, list); - Serial.write(buffer); - - if (log_mutex != nullptr) xSemaphoreGive(log_mutex); - return ret; +void test_custom_names() { + static constexpr const char* name = "CustomRingBuffer"; + + static RingBufferNoSplitDynamic rb_nosp_dyn_named(name); + static RingBufferNoSplitStatic rb_nosp_st_named(name); + static RingBufferNoSplitExternalStorage rb_nosp_ext_named(name); + static RingBufferSplitDynamic rb_sp_dyn_named(name); + static RingBufferSplitStatic rb_sp_st_named(name); + static RingBufferSplitExternalStorage rb_sp_ext_named(name); + static RingBufferByteDynamic rb_byte_dyn_named(name); + static RingBufferByteStatic rb_byte_st_named(name); + static RingBufferByteExternalStorage rb_byte_ext_named(name); + + TEST_ASSERT_TRUE(rb_nosp_dyn_named); + TEST_ASSERT_TRUE(rb_nosp_st_named); + + uint8_t* rb_nosp_ext_named_buffer = + static_cast(malloc(rb_nosp_ext_named.REQUIRED_SIZE)); + TEST_ASSERT_NOT_NULL(rb_nosp_ext_named_buffer); + TEST_ASSERT_TRUE(rb_nosp_ext_named.create(rb_nosp_ext_named_buffer)); + + TEST_ASSERT_TRUE(rb_sp_dyn_named); + TEST_ASSERT_TRUE(rb_sp_st_named); + + uint8_t* rb_sp_ext_named_buffer = static_cast(malloc(rb_sp_ext_named.REQUIRED_SIZE)); + TEST_ASSERT_NOT_NULL(rb_sp_ext_named_buffer); + TEST_ASSERT_TRUE(rb_sp_ext_named.create(rb_sp_ext_named_buffer)); + + TEST_ASSERT_TRUE(rb_byte_dyn_named); + TEST_ASSERT_TRUE(rb_byte_st_named); + + uint8_t* rb_byte_ext_named_buffer = + static_cast(malloc(rb_byte_ext_named.REQUIRED_SIZE)); + TEST_ASSERT_NOT_NULL(rb_byte_ext_named_buffer); + TEST_ASSERT_TRUE(rb_byte_ext_named.create(rb_byte_ext_named_buffer)); + + TEST_ASSERT_EQUAL_STRING(name, rb_nosp_dyn_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, rb_nosp_st_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, rb_nosp_ext_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, rb_sp_dyn_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, rb_sp_st_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, rb_sp_ext_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, rb_byte_dyn_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, rb_byte_st_named.getName()); + TEST_ASSERT_EQUAL_STRING(name, rb_byte_ext_named.getName()); } /* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */ void setup() { - log_mutex = xSemaphoreCreateMutex(); - esp_log_set_vprintf(redirectLogs); - esp_log_level_set("*", ESP_LOG_VERBOSE); - delay(3000); + Serial.begin(115200); + delay(1000); - ESP_LOGI(tag, "Running tests..."); UNITY_BEGIN(); RUN_TEST(test_rb_creation); RUN_TEST(test_rb_nosplit_send_recv); RUN_TEST(test_rb_split_send_recv); RUN_TEST(test_rb_byte_send_recv); + RUN_TEST(test_custom_names); - ESP_LOGI(tag, "Finishing tests..."); UNITY_END(); } -void loop() { vTaskDelete(nullptr); } +void loop() {} /* ---------------------------------------------------------------------------------------------- */ \ No newline at end of file diff --git a/test/test_tasks/test_tasks.cpp b/test/test_tasks/test_tasks.cpp index ed478e9..07feca7 100644 --- a/test/test_tasks/test_tasks.cpp +++ b/test/test_tasks/test_tasks.cpp @@ -82,7 +82,7 @@ void setup() { log_mutex = xSemaphoreCreateMutex(); esp_log_set_vprintf(redirectLogs); esp_log_level_set("*", ESP_LOG_VERBOSE); - delay(3000); + delay(1000); ESP_LOGI(tag, "Running tests..."); UNITY_BEGIN(); @@ -112,7 +112,7 @@ void setup() { UNITY_END(); } -void loop() { vTaskDelete(nullptr); } +void loop() {} void taskDynamicCtor(void* params) { for (;;) { @@ -180,46 +180,56 @@ void taskFunction(void* params) { /* -------------------------------------------- Tests ------------------------------------------- */ void test_create_dynamic_ctor() { + TEST_ASSERT_EQUAL_STRING("TaskDynCtor", task_dyn_ctor.getName()); + TEST_ASSERT_FALSE(task_dyn_ctor.isCreated()); TEST_ASSERT_TRUE(task_dyn_ctor.create()); TEST_ASSERT_TRUE(task_dyn_ctor.isCreated()); } void test_create_dynamic() { + TEST_ASSERT_EQUAL_STRING("RtosTask", task_dyn.getName()); + TEST_ASSERT_FALSE(task_dyn.isCreated()); TEST_ASSERT_TRUE(task_dyn.create("TaskDyn", taskDynamic, 1, nullptr, ARDUINO_RUNNING_CORE)); TEST_ASSERT_TRUE(task_dyn.isCreated()); } void test_create_static_ctor() { + TEST_ASSERT_EQUAL_STRING("TaskStCtor", task_st_ctor.getName()); + TEST_ASSERT_FALSE(task_st_ctor.isCreated()); TEST_ASSERT_TRUE(task_st_ctor.create()); TEST_ASSERT_TRUE(task_st_ctor.isCreated()); } void test_create_static() { + TEST_ASSERT_EQUAL_STRING("RtosTask", task_st.getName()); + TEST_ASSERT_FALSE(task_st.isCreated()); TEST_ASSERT_TRUE(task_st.create("TaskSt", taskStatic, 1, nullptr, ARDUINO_RUNNING_CORE)); TEST_ASSERT_TRUE(task_st.isCreated()); } void test_create_testing_task() { + TEST_ASSERT_EQUAL_STRING("task", task.getName()); + TEST_ASSERT_FALSE(task.isCreated()); TEST_ASSERT_TRUE(task.create()); TEST_ASSERT_TRUE(task.isCreated()); } void test_invalid_task() { + TEST_ASSERT_EQUAL_STRING("RtosTask", task_invalid.getName()); + TEST_ASSERT_FALSE(task_invalid.isCreated()); TEST_ASSERT_FALSE(task_invalid.create(nullptr, nullptr, 0)); TEST_ASSERT_FALSE(task_invalid.isCreated()); - const char* name = task_invalid.getName(); void* params = task_invalid.getParameters(); uint8_t core = task_invalid.getCore(); uint8_t priority = task_invalid.getPriority(); - TEST_ASSERT_NULL(name); TEST_ASSERT_NULL(params); TEST_ASSERT_EQUAL(0xFF, core); TEST_ASSERT_EQUAL(0xFF, priority); @@ -235,7 +245,7 @@ void test_get_task_info() { TEST_ASSERT_EQUAL(123, static_cast(params)->value); uint8_t core = task.getCore(); - TEST_ASSERT_EQUAL(1, task.getCore()); + TEST_ASSERT_EQUAL(1, core); uint8_t priority = task.getPriority(); TEST_ASSERT_EQUAL(1, priority); diff --git a/test/test_timers/test_timers.cpp b/test/test_timers/test_timers.cpp index a776802..2618024 100644 --- a/test/test_timers/test_timers.cpp +++ b/test/test_timers/test_timers.cpp @@ -44,9 +44,11 @@ TimerStatic timer("Timer", timerCb, timer_period, nullptr, false, false); /* -------------------------------------------- Tests ------------------------------------------- */ void test_timers_creation() { // Dynamic timers + TEST_ASSERT_EQUAL_STRING("TimerDynCtor", timer_dyn_ctor.getName()); TEST_ASSERT_TRUE(timer_dyn_ctor); TEST_ASSERT_TRUE(timer_dyn_ctor.isCreated()); + TEST_ASSERT_EQUAL_STRING("RtosTimer", timer_dyn.getName()); TEST_ASSERT_FALSE(timer_dyn); TEST_ASSERT_FALSE(timer_dyn.isCreated()); TEST_ASSERT_TRUE(timer_dyn.create( @@ -60,9 +62,11 @@ void test_timers_creation() { TEST_ASSERT_TRUE(timer_dyn.isCreated()); // Static timers + TEST_ASSERT_EQUAL_STRING("TimerStCtor", timer_st_ctor.getName()); TEST_ASSERT_TRUE(timer_st_ctor); TEST_ASSERT_TRUE(timer_st_ctor.isCreated()); + TEST_ASSERT_EQUAL_STRING("RtosTimer", timer_st.getName()); TEST_ASSERT_FALSE(timer_st); TEST_ASSERT_FALSE(timer_st.isCreated()); TEST_ASSERT_TRUE(timer_st.create( @@ -76,11 +80,14 @@ void test_timers_creation() { TEST_ASSERT_TRUE(timer_st.isCreated()); // Testing timer + TEST_ASSERT_EQUAL_STRING("Timer", timer.getName()); TEST_ASSERT_TRUE(timer); TEST_ASSERT_TRUE(timer.isCreated()); } void test_invalid_timer() { + TEST_ASSERT_EQUAL_STRING("RtosTimer", timer_invalid.getName()); + TEST_ASSERT_FALSE(timer_invalid.isCreated()); TEST_ASSERT_FALSE(timer_invalid.create(nullptr, nullptr, 0, nullptr, false, false)); TEST_ASSERT_FALSE(timer_invalid.isCreated()); @@ -132,30 +139,11 @@ void test_control() { } /* ---------------------------------------------------------------------------------------------- */ -/* ------------------------------------------ USB Logs ------------------------------------------ */ -SemaphoreHandle_t log_mutex = nullptr; - -int redirectLogs(const char* str, va_list list) { - if (log_mutex != nullptr) xSemaphoreTake(log_mutex, portMAX_DELAY); - - static char buffer[2048]; - int ret = vsnprintf(buffer, sizeof(buffer), str, list); - Serial.write(buffer); - - if (log_mutex != nullptr) xSemaphoreGive(log_mutex); - - return ret; -} -/* ---------------------------------------------------------------------------------------------- */ - /* ---------------------------------------------------------------------------------------------- */ void setup() { - log_mutex = xSemaphoreCreateMutex(); - esp_log_set_vprintf(redirectLogs); - esp_log_level_set("*", ESP_LOG_VERBOSE); - delay(3000); + Serial.begin(115200); + delay(1000); - ESP_LOGI(tag, "Running tests..."); UNITY_BEGIN(); RUN_TEST(test_timers_creation); @@ -163,9 +151,8 @@ void setup() { RUN_TEST(test_get_timer_info); RUN_TEST(test_control); - ESP_LOGI(tag, "Finishing tests..."); UNITY_END(); } -void loop() { vTaskDelete(nullptr); } +void loop() {} /* ---------------------------------------------------------------------------------------------- */ \ No newline at end of file