From 86ca4f246bbf2a8adb3e265f8c4f0b47fde0807d Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 18 May 2022 14:32:35 +0200 Subject: [PATCH] new base class for periodic tasks --- src/fsfw/osal/linux/PeriodicPosixTask.cpp | 57 ++--------------------- src/fsfw/osal/linux/PeriodicPosixTask.h | 55 ++++------------------ src/fsfw/tasks/PeriodicTaskBase.cpp | 57 +++++++++++++++++++++++ src/fsfw/tasks/PeriodicTaskBase.h | 52 +++++++++++++++++++++ src/fsfw/tasks/PeriodicTaskIF.h | 24 +++++----- src/fsfw/tasks/TaskFactory.h | 2 +- src/fsfw/tasks/Typedef.h | 13 ------ src/fsfw/tasks/definitions.h | 13 ++++++ 8 files changed, 148 insertions(+), 125 deletions(-) create mode 100644 src/fsfw/tasks/PeriodicTaskBase.cpp create mode 100644 src/fsfw/tasks/PeriodicTaskBase.h delete mode 100644 src/fsfw/tasks/Typedef.h create mode 100644 src/fsfw/tasks/definitions.h diff --git a/src/fsfw/osal/linux/PeriodicPosixTask.cpp b/src/fsfw/osal/linux/PeriodicPosixTask.cpp index 44d669df..510ec59c 100644 --- a/src/fsfw/osal/linux/PeriodicPosixTask.cpp +++ b/src/fsfw/osal/linux/PeriodicPosixTask.cpp @@ -8,12 +8,10 @@ #include "fsfw/tasks/ExecutableObjectIF.h" PeriodicPosixTask::PeriodicPosixTask(const char* name_, int priority_, size_t stackSize_, - uint32_t period_, void(deadlineMissedFunc_)()) + uint32_t period_, TaskDeadlineMissedFunction dlMissedFunc_) : PosixThread(name_, priority_, stackSize_), - objectList(), - started(false), - periodMs(period_), - deadlineMissedFunc(deadlineMissedFunc_) {} + PeriodicTaskBase(period_, dlMissedFunc_), + started(false) {} PeriodicPosixTask::~PeriodicPosixTask() { // Not Implemented @@ -27,31 +25,8 @@ void* PeriodicPosixTask::taskEntryPoint(void* arg) { return nullptr; } -ReturnValue_t PeriodicPosixTask::addComponent(object_id_t object, uint8_t opCode) { - ExecutableObjectIF* newObject = ObjectManager::instance()->get(object); - return addComponent(newObject, opCode); -} - -ReturnValue_t PeriodicPosixTask::addComponent(ExecutableObjectIF* object, uint8_t opCode) { - if (object == nullptr) { -#if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::error << "PeriodicTask::addComponent: Invalid object. Make sure" - << " it implements ExecutableObjectIF!" << std::endl; -#else - sif::printError( - "PeriodicTask::addComponent: Invalid object. Make sure it " - "implements ExecutableObjectIF!\n"); -#endif - return HasReturnvaluesIF::RETURN_FAILED; - } - objectList.push_back({object, opCode}); - object->setTaskIF(this); - - return HasReturnvaluesIF::RETURN_OK; -} - ReturnValue_t PeriodicPosixTask::sleepFor(uint32_t ms) { - return PosixThread::sleep((uint64_t)ms * 1000000); + return PosixThread::sleep(static_cast(ms * 1000000)); } ReturnValue_t PeriodicPosixTask::startTask(void) { @@ -84,27 +59,3 @@ void PeriodicPosixTask::taskFunctionality(void) { } } } - -uint32_t PeriodicPosixTask::getPeriodMs() const { return periodMs; } - -bool PeriodicPosixTask::isEmpty() const { return objectList.empty(); } - -ReturnValue_t PeriodicPosixTask::initObjsAfterTaskCreation() { - std::multiset uniqueObjects; - ReturnValue_t status = HasReturnvaluesIF::RETURN_OK; - uint32_t count = 0; - for (const auto& obj : objectList) { - // Ensure that each unique object is initialized once. - if (uniqueObjects.find(obj.first) == uniqueObjects.end()) { - ReturnValue_t result = obj.first->initializeAfterTaskCreation(); - if (result != HasReturnvaluesIF::RETURN_OK) { - count++; - status = result; - } - uniqueObjects.emplace(obj.first); - } - } - if (count > 0) { - } - return status; -} diff --git a/src/fsfw/osal/linux/PeriodicPosixTask.h b/src/fsfw/osal/linux/PeriodicPosixTask.h index a3c6b187..3dcc6bcc 100644 --- a/src/fsfw/osal/linux/PeriodicPosixTask.h +++ b/src/fsfw/osal/linux/PeriodicPosixTask.h @@ -1,14 +1,17 @@ #ifndef FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_ #define FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_ -#include - -#include "../../objectmanager/ObjectManagerIF.h" -#include "../../tasks/ExecutableObjectIF.h" -#include "../../tasks/PeriodicTaskIF.h" #include "PosixThread.h" -class PeriodicPosixTask : public PosixThread, public PeriodicTaskIF { +#include + +#include "fsfw/objectmanager/ObjectManagerIF.h" +#include "fsfw/tasks/ExecutableObjectIF.h" +#include "fsfw/tasks/PeriodicTaskIF.h" +#include "fsfw/tasks/PeriodicTaskBase.h" + + +class PeriodicPosixTask : public PosixThread, public PeriodicTaskBase { public: /** * Create a generic periodic task. @@ -34,48 +37,16 @@ class PeriodicPosixTask : public PosixThread, public PeriodicTaskIF { * to the system call. */ ReturnValue_t startTask() override; - /** - * Adds an object to the list of objects to be executed. - * The objects are executed in the order added. - * @param object Id of the object to add. - * @return RETURN_OK on success, RETURN_FAILED if the object could not be added. - */ - ReturnValue_t addComponent(object_id_t object, uint8_t opCode) override; - - /** - * Adds an object to the list of objects to be executed. - * The objects are executed in the order added. - * @param object pointer to the object to add. - * @return RETURN_OK on success, RETURN_FAILED if the object could not be added. - */ - ReturnValue_t addComponent(ExecutableObjectIF* object, uint8_t opCode) override; - - uint32_t getPeriodMs() const override; ReturnValue_t sleepFor(uint32_t ms) override; - ReturnValue_t initObjsAfterTaskCreation(); - - bool isEmpty() const override; - private: - //! Typedef for the List of objects. Will contain the objects to execute and their respective - //! op codes - using ObjectList = std::vector>; - /** - * @brief This attribute holds a list of objects to be executed. - */ - ObjectList objectList; /** * @brief Flag to indicate that the task was started and is allowed to run */ bool started; - /** - * @brief Period of the task in milliseconds - */ - uint32_t periodMs; /** * @brief The function containing the actual functionality of the task. * @details The method sets and starts @@ -92,14 +63,6 @@ class PeriodicPosixTask : public PosixThread, public PeriodicTaskIF { * of the child class. Needs a valid pointer to the derived class. */ static void* taskEntryPoint(void* arg); - /** - * @brief The pointer to the deadline-missed function. - * @details This pointer stores the function that is executed if the task's deadline is missed. - * So, each may react individually on a timing failure. The pointer may be - * NULL, then nothing happens on missing the deadline. The deadline is equal to the next execution - * of the periodic task. - */ - void (*deadlineMissedFunc)(); }; #endif /* FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_ */ diff --git a/src/fsfw/tasks/PeriodicTaskBase.cpp b/src/fsfw/tasks/PeriodicTaskBase.cpp new file mode 100644 index 00000000..606fee1e --- /dev/null +++ b/src/fsfw/tasks/PeriodicTaskBase.cpp @@ -0,0 +1,57 @@ +#include +#include "PeriodicTaskBase.h" + +#include + +PeriodicTaskBase::PeriodicTaskBase(uint32_t periodMs_, + TaskDeadlineMissedFunction deadlineMissedFunc_) + : periodMs(periodMs_), deadlineMissedFunc(deadlineMissedFunc_) {} + +uint32_t PeriodicTaskBase::getPeriodMs() const { return periodMs; } + +bool PeriodicTaskBase::isEmpty() const override { + return objectList.empty(); +} + +ReturnValue_t PeriodicTaskBase::initObjsAfterTaskCreation() { + std::multiset uniqueObjects; + ReturnValue_t status = HasReturnvaluesIF::RETURN_OK; + uint32_t count = 0; + for (const auto& obj : objectList) { + // Ensure that each unique object is initialized once. + if (uniqueObjects.find(obj.first) == uniqueObjects.end()) { + ReturnValue_t result = obj.first->initializeAfterTaskCreation(); + if (result != HasReturnvaluesIF::RETURN_OK) { + count++; + status = result; + } + uniqueObjects.emplace(obj.first); + } + } + if (count > 0) { + } + return status; +} + +ReturnValue_t PeriodicTaskBase::addComponent(object_id_t object, uint8_t opCode) { + ExecutableObjectIF* newObject = ObjectManager::instance()->get(object); + return addComponent(newObject, opCode); +} + +ReturnValue_t PeriodicTaskBase::addComponent(ExecutableObjectIF* object, uint8_t opCode) { + if (object == nullptr) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::error << "PeriodicTask::addComponent: Invalid object. Make sure" + << " it implements ExecutableObjectIF!" << std::endl; +#else + sif::printError( + "PeriodicTask::addComponent: Invalid object. Make sure it " + "implements ExecutableObjectIF!\n"); +#endif + return HasReturnvaluesIF::RETURN_FAILED; + } + objectList.push_back({object, opCode}); + object->setTaskIF(this); + + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/src/fsfw/tasks/PeriodicTaskBase.h b/src/fsfw/tasks/PeriodicTaskBase.h new file mode 100644 index 00000000..4b330426 --- /dev/null +++ b/src/fsfw/tasks/PeriodicTaskBase.h @@ -0,0 +1,52 @@ +#ifndef FSFW_SRC_FSFW_TASKS_PERIODICTASKBASE_H_ +#define FSFW_SRC_FSFW_TASKS_PERIODICTASKBASE_H_ + +#include "fsfw/tasks/PeriodicTaskIF.h" +#include "fsfw/tasks/definitions.h" +#include +#include + +class ExecutableObjectIF; + +class PeriodicTaskBase: public PeriodicTaskIF { +public: + PeriodicTaskBase(uint32_t periodMs, TaskDeadlineMissedFunction deadlineMissedFunc = nullptr); + + ReturnValue_t addComponent(object_id_t object, uint8_t opCode) override; + ReturnValue_t addComponent(ExecutableObjectIF* object, uint8_t opCode) override; + + + uint32_t getPeriodMs() const override; + + bool isEmpty() const override; + + ReturnValue_t initObjsAfterTaskCreation(); + +protected: + + //! Typedef for the List of objects. Will contain the objects to execute and their respective + //! operation codes + using ObjectList = std::vector>; + /** + * @brief This attribute holds a list of objects to be executed. + */ + ObjectList objectList; + + /** + * @brief Period of the task in milliseconds + */ + uint32_t periodMs; + + /** + * @brief The pointer to the deadline-missed function. + * @details This pointer stores the function that is executed if the task's deadline is missed. + * So, each may react individually on a timing failure. The pointer may be + * NULL, then nothing happens on missing the deadline. The deadline is equal to the next execution + * of the periodic task. + */ + TaskDeadlineMissedFunction deadlineMissedFunc = nullptr; +}; + + + +#endif /* FSFW_SRC_FSFW_TASKS_PERIODICTASKBASE_H_ */ diff --git a/src/fsfw/tasks/PeriodicTaskIF.h b/src/fsfw/tasks/PeriodicTaskIF.h index 2ae268fc..076ef56e 100644 --- a/src/fsfw/tasks/PeriodicTaskIF.h +++ b/src/fsfw/tasks/PeriodicTaskIF.h @@ -1,11 +1,10 @@ #ifndef FRAMEWORK_TASK_PERIODICTASKIF_H_ #define FRAMEWORK_TASK_PERIODICTASKIF_H_ -#include +#include "fsfw/objectmanager/SystemObjectIF.h" +#include "fsfw/tasks/ExecutableObjectIF.h" -#include "../objectmanager/SystemObjectIF.h" -#include "../timemanager/Clock.h" -class ExecutableObjectIF; +#include /** * New version of TaskIF @@ -26,20 +25,21 @@ class PeriodicTaskIF { virtual ReturnValue_t startTask() = 0; /** - * Add a component (object) to a periodic task. - * @param object - * Add an object to the task. The object needs to implement ExecutableObjectIF - * @return + * Adds an object to the list of objects to be executed. + * The objects are executed in the order added. The object needs to implement + * ExecutableObjectIF + * @param object Id of the object to add. + * @return RETURN_OK on success, RETURN_FAILED if the object could not be added. */ virtual ReturnValue_t addComponent(object_id_t object, uint8_t opCode = 0) { return HasReturnvaluesIF::RETURN_FAILED; }; /** - * Add an object to a periodic task. - * @param object - * Add an object to the task. - * @return + * Adds an object to the list of objects to be executed. + * The objects are executed in the order added. + * @param object pointer to the object to add. + * @return RETURN_OK on success, RETURN_FAILED if the object could not be added. */ virtual ReturnValue_t addComponent(ExecutableObjectIF* object, uint8_t opCode = 0) { return HasReturnvaluesIF::RETURN_FAILED; diff --git a/src/fsfw/tasks/TaskFactory.h b/src/fsfw/tasks/TaskFactory.h index fcd62678..828c533e 100644 --- a/src/fsfw/tasks/TaskFactory.h +++ b/src/fsfw/tasks/TaskFactory.h @@ -4,7 +4,7 @@ #include #include "FixedTimeslotTaskIF.h" -#include "Typedef.h" +#include "definitions.h" /** * Singleton Class that produces Tasks. diff --git a/src/fsfw/tasks/Typedef.h b/src/fsfw/tasks/Typedef.h deleted file mode 100644 index 1bb75131..00000000 --- a/src/fsfw/tasks/Typedef.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef FSFW_TASKS_TYPEDEF_H_ -#define FSFW_TASKS_TYPEDEF_H_ - -#include -#include - -typedef const char* TaskName; -typedef uint32_t TaskPriority; -typedef size_t TaskStackSize; -typedef double TaskPeriod; -typedef void (*TaskDeadlineMissedFunction)(); - -#endif /* FSFW_TASKS_TYPEDEF_H_ */ diff --git a/src/fsfw/tasks/definitions.h b/src/fsfw/tasks/definitions.h new file mode 100644 index 00000000..bca9b768 --- /dev/null +++ b/src/fsfw/tasks/definitions.h @@ -0,0 +1,13 @@ +#ifndef FSFW_TASKS_TYPEDEF_H_ +#define FSFW_TASKS_TYPEDEF_H_ + +#include +#include + +using TaskName = const char*; +using TaskPriority = uint32_t; +using TaskStackSize = size_t; +using TaskPeriod = double; +using TaskDeadlineMissedFunction = void (*)(); + +#endif /* FSFW_TASKS_TYPEDEF_H_ */