cleanup sw req

This commit is contained in:
2026-02-01 19:47:53 +01:00
parent 0bdbcb1657
commit 304371c6b8
608 changed files with 47798 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
idf_component_register(
SRCS "com/swtimer.cpp"
INCLUDE_DIRS "com"
)

View File

@@ -0,0 +1,81 @@
/**
* @file swtimer.cpp
* @brief Software Timer component implementation
* @author Mahmoud Elmohtady
* @company Nabd solutions - ASF
* @copyright Copyright (c) 2025
*/
#include "swtimer.hpp"
SoftwareTimer::SoftwareTimer()
: m_isActive(false)
, m_interval(0)
, m_type(TimerType::ONE_SHOT)
{
}
SoftwareTimer::~SoftwareTimer()
{
stop();
}
bool SoftwareTimer::create(uint32_t interval_ms, TimerType type, TimerCallback callback)
{
if (interval_ms == 0)
{
return false;
}
m_interval = interval_ms;
m_type = type;
m_callback = callback;
return true;
}
bool SoftwareTimer::start()
{
if (m_callback == nullptr)
{
return false;
}
m_isActive = true;
// TODO: Implement actual timer start logic
return true;
}
bool SoftwareTimer::stop()
{
if (!m_isActive)
{
return false;
}
m_isActive = false;
// TODO: Implement actual timer stop logic
return true;
}
bool SoftwareTimer::reset()
{
if (!m_isActive)
{
return false;
}
stop();
return start();
}
bool SoftwareTimer::isActive() const
{
return m_isActive;
}
uint32_t SoftwareTimer::getInterval() const
{
return m_interval;
}

View File

@@ -0,0 +1,95 @@
/**
* @file swtimer.hpp
* @brief Software Timer component header - Manages time-based events and scheduling
* @author Mahmoud Elmohtady
* @company Nabd solutions - ASF
* @copyright Copyright (c) 2025
*/
#ifndef SWTIMER_HPP
#define SWTIMER_HPP
#include <cstdint>
#include <functional>
/**
* @brief Timer type enumeration
*/
enum class TimerType
{
ONE_SHOT, ///< Timer expires once and stops
PERIODIC ///< Timer repeats until stopped
};
/**
* @brief Timer callback function type
*/
using TimerCallback = std::function<void(void)>;
/**
* @brief Software Timer class
*
* Provides functionality to create, start, stop, and manage timers
* that can trigger callbacks after specified intervals.
*/
class SoftwareTimer
{
public:
/**
* @brief Constructor
*/
SoftwareTimer();
/**
* @brief Destructor
*/
~SoftwareTimer();
/**
* @brief Create a new timer
* @param interval_ms Timer interval in milliseconds
* @param type Timer type (ONE_SHOT or PERIODIC)
* @param callback Function to call when timer expires
* @return true if timer created successfully, false otherwise
*/
bool create(uint32_t interval_ms, TimerType type, TimerCallback callback);
/**
* @brief Start the timer
* @return true if started successfully, false otherwise
*/
bool start();
/**
* @brief Stop the timer
* @return true if stopped successfully, false otherwise
*/
bool stop();
/**
* @brief Reset the timer
* @return true if reset successfully, false otherwise
*/
bool reset();
/**
* @brief Check if timer is active
* @return true if timer is running, false otherwise
*/
bool isActive() const;
/**
* @brief Get the timer interval
* @return Timer interval in milliseconds
*/
uint32_t getInterval() const;
private:
bool m_isActive; ///< Timer active state
uint32_t m_interval; ///< Timer interval in milliseconds
TimerType m_type; ///< Timer type
TimerCallback m_callback; ///< Callback function
};
#endif // SWTIMER_HPP

View File

@@ -0,0 +1,108 @@
/**
* @file test_swtimer.cpp
* @brief Unit tests for Software Timer component
* @author Mahmoud Elmohtady
* @company Nabd solutions - ASF
* @copyright Copyright (c) 2025
*/
#include "unity.h"
#include "swtimer.hpp"
extern "C" {
void setUp(void)
{
// Set up test fixtures before each test
}
void tearDown(void)
{
// Clean up test fixtures after each test
}
/**
* @brief Test timer creation with valid parameters
*/
void test_swtimer_create_valid(void)
{
SoftwareTimer timer;
bool result = timer.create(1000, TimerType::ONE_SHOT, [](){});
TEST_ASSERT_TRUE(result);
TEST_ASSERT_FALSE(timer.isActive());
TEST_ASSERT_EQUAL(1000, timer.getInterval());
}
/**
* @brief Test timer creation with zero interval
*/
void test_swtimer_create_zero_interval(void)
{
SoftwareTimer timer;
bool result = timer.create(0, TimerType::ONE_SHOT, [](){});
TEST_ASSERT_FALSE(result);
}
/**
* @brief Test timer start
*/
void test_swtimer_start(void)
{
SoftwareTimer timer;
timer.create(1000, TimerType::ONE_SHOT, [](){});
bool result = timer.start();
TEST_ASSERT_TRUE(result);
TEST_ASSERT_TRUE(timer.isActive());
}
/**
* @brief Test timer stop
*/
void test_swtimer_stop(void)
{
SoftwareTimer timer;
timer.create(1000, TimerType::ONE_SHOT, [](){});
timer.start();
bool result = timer.stop();
TEST_ASSERT_TRUE(result);
TEST_ASSERT_FALSE(timer.isActive());
}
/**
* @brief Test timer reset
*/
void test_swtimer_reset(void)
{
SoftwareTimer timer;
timer.create(1000, TimerType::ONE_SHOT, [](){});
timer.start();
bool result = timer.reset();
TEST_ASSERT_TRUE(result);
TEST_ASSERT_TRUE(timer.isActive());
}
/**
* @brief Test timer one-shot type
*/
void test_swtimer_one_shot(void)
{
SoftwareTimer timer;
timer.create(1000, TimerType::ONE_SHOT, [](){});
TEST_ASSERT_TRUE(timer.start());
}
/**
* @brief Test timer periodic type
*/
void test_swtimer_periodic(void)
{
SoftwareTimer timer;
timer.create(500, TimerType::PERIODIC, [](){});
TEST_ASSERT_TRUE(timer.start());
}
} // extern "C"

View File

@@ -0,0 +1,4 @@
idf_component_register(
SRCS "com/task.cpp"
INCLUDE_DIRS "com"
)

View File

@@ -0,0 +1,111 @@
/**
* @file task.cpp
* @brief Task component implementation
* @author Mahmoud Elmohtady
* @company Nabd solutions - ASF
* @copyright Copyright (c) 2025
*/
#include "task.hpp"
Task::Task()
: m_state(TaskState::CREATED)
, m_priority(TaskPriority::NORMAL)
, m_handle(nullptr)
{
}
Task::~Task()
{
destroy();
}
bool Task::create(const TaskConfig& config, void (*taskFunction)(void*), void* arg)
{
if (config.name == nullptr || taskFunction == nullptr)
{
return false;
}
if (config.stackSize < 2048) // Minimum stack size
{
return false;
}
m_priority = config.priority;
m_state = TaskState::CREATED;
// TODO: Implement actual task creation logic
m_handle = (void*)1; // Placeholder
return true;
}
bool Task::destroy()
{
if (m_state == TaskState::DELETED)
{
return false;
}
// TODO: Implement actual task deletion logic
m_state = TaskState::DELETED;
m_handle = nullptr;
return true;
}
bool Task::suspend()
{
if (m_state != TaskState::RUNNING && m_state != TaskState::READY)
{
return false;
}
// TODO: Implement actual task suspension logic
m_state = TaskState::SUSPENDED;
return true;
}
bool Task::resume()
{
if (m_state != TaskState::SUSPENDED)
{
return false;
}
// TODO: Implement actual task resumption logic
m_state = TaskState::READY;
return true;
}
TaskState Task::getState() const
{
return m_state;
}
TaskPriority Task::getPriority() const
{
return m_priority;
}
bool Task::setPriority(TaskPriority priority)
{
m_priority = priority;
// TODO: Implement actual priority change logic
return true;
}
void Task::delay(uint32_t delayMs)
{
// TODO: Implement actual delay logic
(void)delayMs;
}
Task* Task::getCurrentTask()
{
// TODO: Implement actual current task retrieval
return nullptr;
}

View File

@@ -0,0 +1,134 @@
/**
* @file task.hpp
* @brief Task component header - Manages system tasks and their lifecycle
* @author Mahmoud Elmohtady
* @company Nabd solutions - ASF
* @copyright Copyright (c) 2025
*/
#ifndef TASK_HPP
#define TASK_HPP
#include <cstdint>
#include <cstddef>
/**
* @brief Task priority levels
*/
enum class TaskPriority
{
LOW = 1,
NORMAL = 5,
HIGH = 10,
CRITICAL = 20
};
/**
* @brief Task state enumeration
*/
enum class TaskState
{
CREATED,
READY,
RUNNING,
BLOCKED,
SUSPENDED,
DELETED
};
/**
* @brief Task configuration structure
*/
struct TaskConfig
{
const char* name; ///< Task name
uint32_t stackSize; ///< Stack size in bytes
TaskPriority priority; ///< Task priority
uint32_t coreId; ///< CPU core ID (0 or 1 for ESP32)
};
/**
* @brief Task class
*
* Provides functionality to create, manage, and control tasks
* with scheduling, prioritization, and resource management.
*/
class Task
{
public:
/**
* @brief Constructor
*/
Task();
/**
* @brief Destructor
*/
~Task();
/**
* @brief Create a new task
* @param config Task configuration
* @param taskFunction Function to run in the task
* @param arg Argument to pass to task function
* @return true if task created successfully, false otherwise
*/
bool create(const TaskConfig& config, void (*taskFunction)(void*), void* arg);
/**
* @brief Delete the task
* @return true if deleted successfully, false otherwise
*/
bool destroy();
/**
* @brief Suspend the task
* @return true if suspended successfully, false otherwise
*/
bool suspend();
/**
* @brief Resume the task
* @return true if resumed successfully, false otherwise
*/
bool resume();
/**
* @brief Get the current task state
* @return Current task state
*/
TaskState getState() const;
/**
* @brief Get the task priority
* @return Task priority level
*/
TaskPriority getPriority() const;
/**
* @brief Set the task priority
* @param priority New priority level
* @return true if priority set successfully, false otherwise
*/
bool setPriority(TaskPriority priority);
/**
* @brief Delay task execution
* @param delayMs Delay in milliseconds
*/
static void delay(uint32_t delayMs);
/**
* @brief Get current running task handle
* @return Task handle or nullptr if not running
*/
static Task* getCurrentTask();
private:
TaskState m_state;
TaskPriority m_priority;
void* m_handle;
};
#endif // TASK_HPP

View File

@@ -0,0 +1,114 @@
/**
* @file test_task.cpp
* @brief Unit tests for Task component
* @author Mahmoud Elmohtady
* @company Nabd solutions - ASF
* @copyright Copyright (c) 2025
*/
#include "unity.h"
#include "task.hpp"
extern "C" {
void setUp(void)
{
// Set up test fixtures before each test
}
void tearDown(void)
{
// Clean up test fixtures after each test
}
/**
* @brief Test task creation with valid parameters
*/
void test_task_create_valid(void)
{
Task task;
TaskConfig config = {"test_task", 4096, TaskPriority::NORMAL, 0};
bool result = task.create(config, [](void* arg){(void)arg;}, nullptr);
TEST_ASSERT_TRUE(result);
TEST_ASSERT_EQUAL(TaskState::CREATED, task.getState());
}
/**
* @brief Test task creation with invalid parameters
*/
void test_task_create_invalid_name(void)
{
Task task;
TaskConfig config = {nullptr, 4096, TaskPriority::NORMAL, 0};
bool result = task.create(config, [](void* arg){(void)arg;}, nullptr);
TEST_ASSERT_FALSE(result);
}
/**
* @brief Test task creation with small stack size
*/
void test_task_create_small_stack(void)
{
Task task;
TaskConfig config = {"test_task", 1024, TaskPriority::NORMAL, 0};
bool result = task.create(config, [](void* arg){(void)arg;}, nullptr);
TEST_ASSERT_FALSE(result);
}
/**
* @brief Test task destroy
*/
void test_task_destroy(void)
{
Task task;
TaskConfig config = {"test_task", 4096, TaskPriority::NORMAL, 0};
task.create(config, [](void* arg){(void)arg;}, nullptr);
bool result = task.destroy();
TEST_ASSERT_TRUE(result);
TEST_ASSERT_EQUAL(TaskState::DELETED, task.getState());
}
/**
* @brief Test task suspend
*/
void test_task_suspend(void)
{
Task task;
TaskConfig config = {"test_task", 4096, TaskPriority::NORMAL, 0};
task.create(config, [](void* arg){(void)arg;}, nullptr);
// TODO: Set task to running state for proper suspend test
// For now, test will likely fail as task is in CREATED state
// This is expected until proper state management is implemented
}
/**
* @brief Test task resume
*/
void test_task_resume(void)
{
Task task;
TaskConfig config = {"test_task", 4096, TaskPriority::NORMAL, 0};
task.create(config, [](void* arg){(void)arg;}, nullptr);
// TODO: Suspend task first, then resume
}
/**
* @brief Test task priority get and set
*/
void test_task_priority(void)
{
Task task;
TaskConfig config = {"test_task", 4096, TaskPriority::NORMAL, 0};
task.create(config, [](void* arg){(void)arg;}, nullptr);
TEST_ASSERT_EQUAL(TaskPriority::NORMAL, task.getPriority());
bool result = task.setPriority(TaskPriority::HIGH);
TEST_ASSERT_TRUE(result);
TEST_ASSERT_EQUAL(TaskPriority::HIGH, task.getPriority());
}
} // extern "C"