From 7df1922633061419f0a66c03e34ad16d4db7457d Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sat, 14 May 2022 09:38:59 +0200 Subject: [PATCH] refactor task IF --- hal/src/fsfw_hal/linux/CommandExecutor.cpp | 2 + hal/src/fsfw_hal/linux/spi/SpiComIF.cpp | 27 ++++++++++-- hal/src/fsfw_hal/linux/spi/SpiComIF.h | 30 +++++++++++--- src/fsfw/osal/linux/FixedTimeslotTask.cpp | 4 +- src/fsfw/osal/linux/FixedTimeslotTask.h | 13 +++--- src/fsfw/osal/linux/PeriodicPosixTask.cpp | 41 +++++++++++++++---- src/fsfw/osal/linux/PeriodicPosixTask.h | 14 +++++-- src/fsfw/tasks/FixedSlotSequence.cpp | 2 + src/fsfw/tasks/FixedSlotSequence.h | 2 + src/fsfw/tasks/FixedTimeslotTaskIF.h | 2 +- src/fsfw/tasks/PeriodicTaskIF.h | 6 ++- .../unit/power/testPowerSwitcher.cpp | 4 +- 12 files changed, 114 insertions(+), 33 deletions(-) diff --git a/hal/src/fsfw_hal/linux/CommandExecutor.cpp b/hal/src/fsfw_hal/linux/CommandExecutor.cpp index 49c44ebf..dcdd10ee 100644 --- a/hal/src/fsfw_hal/linux/CommandExecutor.cpp +++ b/hal/src/fsfw_hal/linux/CommandExecutor.cpp @@ -205,3 +205,5 @@ ReturnValue_t CommandExecutor::executeBlocking() { } return HasReturnvaluesIF::RETURN_OK; } + +const std::vector& CommandExecutor::getReadVector() const { return readVec; } diff --git a/hal/src/fsfw_hal/linux/spi/SpiComIF.cpp b/hal/src/fsfw_hal/linux/spi/SpiComIF.cpp index dcf92b5d..3c257f1f 100644 --- a/hal/src/fsfw_hal/linux/spi/SpiComIF.cpp +++ b/hal/src/fsfw_hal/linux/spi/SpiComIF.cpp @@ -401,12 +401,33 @@ void SpiComIF::setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed) if (retval != 0) { utility::handleIoctlError("SpiComIF::setSpiSpeedAndMode: Setting SPI speed failed"); } - // This updates the SPI clock default polarity. Only setting the mode does not update - // the line state, which can be an issue on mode switches because the clock line will - // switch the state after the chip select is pulled low +} + +void SpiComIF::getSpiSpeedAndMode(int spiFd, spi::SpiModes& mode, uint32_t& speed) const { + uint8_t tmpMode = 0; + int retval = ioctl(spiFd, SPI_IOC_RD_MODE, &tmpMode); + if (retval != 0) { + utility::handleIoctlError("SpiComIF::getSpiSpeedAndMode: Reading SPI mode failed"); + } + mode = static_cast(tmpMode); + + retval = ioctl(spiFd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); + if (retval != 0) { + utility::handleIoctlError("SpiComIF::getSpiSpeedAndMode: Getting SPI speed failed"); + } +} + +const std::string& SpiComIF::getSpiDev() const { return dev; } + +void SpiComIF::updateLinePolarity(int spiFd) { clockUpdateTransfer.len = 0; retval = ioctl(spiFd, SPI_IOC_MESSAGE(1), &clockUpdateTransfer); if (retval != 0) { utility::handleIoctlError("SpiComIF::setSpiSpeedAndMode: Updating SPI default clock failed"); } } + +void SpiComIF::setMutexParams(MutexIF::TimeoutType timeoutType_, uint32_t timeoutMs_) { + timeoutType = timeoutType_; + timeoutMs = timeoutMs_; +} diff --git a/hal/src/fsfw_hal/linux/spi/SpiComIF.h b/hal/src/fsfw_hal/linux/spi/SpiComIF.h index 357afa2f..1400dcfc 100644 --- a/hal/src/fsfw_hal/linux/spi/SpiComIF.h +++ b/hal/src/fsfw_hal/linux/spi/SpiComIF.h @@ -22,15 +22,17 @@ class SpiCookie; */ class SpiComIF : public DeviceCommunicationIF, public SystemObject { public: - static constexpr uint8_t spiRetvalId = CLASS_ID::HAL_SPI; + static constexpr dur_millis_t DEFAULT_MUTEX_TIMEOUT = 20; + + static constexpr uint8_t CLASS_ID = CLASS_ID::HAL_SPI; static constexpr ReturnValue_t OPENING_FILE_FAILED = - HasReturnvaluesIF::makeReturnCode(spiRetvalId, 0); + HasReturnvaluesIF::makeReturnCode(CLASS_ID, 0); /* Full duplex (ioctl) transfer failure */ static constexpr ReturnValue_t FULL_DUPLEX_TRANSFER_FAILED = - HasReturnvaluesIF::makeReturnCode(spiRetvalId, 1); + HasReturnvaluesIF::makeReturnCode(CLASS_ID, 1); /* Half duplex (read/write) transfer failure */ static constexpr ReturnValue_t HALF_DUPLEX_TRANSFER_FAILED = - HasReturnvaluesIF::makeReturnCode(spiRetvalId, 2); + HasReturnvaluesIF::makeReturnCode(CLASS_ID, 2); SpiComIF(object_id_t objectId, GpioIF* gpioComIF); @@ -45,6 +47,7 @@ class SpiComIF : public DeviceCommunicationIF, public SystemObject { * the chip select must be driven from outside of the com if. */ MutexIF* getMutex(MutexIF::TimeoutType* timeoutType = nullptr, uint32_t* timeoutMs = nullptr); + void setMutexParams(MutexIF::TimeoutType timeoutType, uint32_t timeoutMs); /** * Perform a regular send operation using Linux iotcl. This is public so it can be used @@ -59,6 +62,23 @@ class SpiComIF : public DeviceCommunicationIF, public SystemObject { GpioIF* getGpioInterface(); void setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed); +<<<<<<< Updated upstream +======= + void getSpiSpeedAndMode(int spiFd, spi::SpiModes& mode, uint32_t& speed) const; + + /** + * This updates the SPI clock default polarity. Only setting the mode does not update + * the line state, which can be an issue on mode switches because the clock line will + * switch the state after the chip select is pulled low. + * + * It is recommended to call this function after #setSpiSpeedAndMode and after locking the + * CS mutex if the SPI bus has multiple SPI devices with different speed and SPI modes attached. + * @param spiFd + */ + void updateLinePolarity(int spiFd); + + const std::string& getSpiDev() const; +>>>>>>> Stashed changes void performSpiWiretapping(SpiCookie* spiCookie); ReturnValue_t getReadBuffer(address_t spiAddress, uint8_t** buffer); @@ -73,7 +93,7 @@ class SpiComIF : public DeviceCommunicationIF, public SystemObject { MutexIF* spiMutex = nullptr; MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::WAITING; - uint32_t timeoutMs = 20; + uint32_t timeoutMs = DEFAULT_MUTEX_TIMEOUT; spi_ioc_transfer clockUpdateTransfer = {}; using SpiDeviceMap = std::unordered_map; diff --git a/src/fsfw/osal/linux/FixedTimeslotTask.cpp b/src/fsfw/osal/linux/FixedTimeslotTask.cpp index 1f4d6e23..d1fccdf9 100644 --- a/src/fsfw/osal/linux/FixedTimeslotTask.cpp +++ b/src/fsfw/osal/linux/FixedTimeslotTask.cpp @@ -14,6 +14,8 @@ FixedTimeslotTask::FixedTimeslotTask(const char* name_, int priority_, size_t st FixedTimeslotTask::~FixedTimeslotTask() {} +bool FixedTimeslotTask::isEmpty() const { return pst.isEmpty(); } + void* FixedTimeslotTask::taskEntryPoint(void* arg) { // The argument is re-interpreted as PollingTask. FixedTimeslotTask* originalTask(reinterpret_cast(arg)); @@ -50,7 +52,7 @@ ReturnValue_t FixedTimeslotTask::addSlot(object_id_t componentId, uint32_t slotT return HasReturnvaluesIF::RETURN_FAILED; } -ReturnValue_t FixedTimeslotTask::checkSequence() const { return pst.checkSequence(); } +ReturnValue_t FixedTimeslotTask::checkSequence() { return pst.checkSequence(); } void FixedTimeslotTask::taskFunctionality() { // Like FreeRTOS pthreads are running as soon as they are created diff --git a/src/fsfw/osal/linux/FixedTimeslotTask.h b/src/fsfw/osal/linux/FixedTimeslotTask.h index 76b92db3..a5dc9032 100644 --- a/src/fsfw/osal/linux/FixedTimeslotTask.h +++ b/src/fsfw/osal/linux/FixedTimeslotTask.h @@ -24,15 +24,18 @@ class FixedTimeslotTask : public FixedTimeslotTaskIF, public PosixThread { FixedTimeslotTask(const char* name_, int priority_, size_t stackSize_, uint32_t periodMs_); virtual ~FixedTimeslotTask(); - virtual ReturnValue_t startTask(); + ReturnValue_t startTask() override; - virtual ReturnValue_t sleepFor(uint32_t ms); + ReturnValue_t sleepFor(uint32_t ms) override; - virtual uint32_t getPeriodMs() const; + uint32_t getPeriodMs() const override; - virtual ReturnValue_t addSlot(object_id_t componentId, uint32_t slotTimeMs, int8_t executionStep); + ReturnValue_t addSlot(object_id_t componentId, uint32_t slotTimeMs, + int8_t executionStep) override; - virtual ReturnValue_t checkSequence() const; + ReturnValue_t checkSequence() override; + + bool isEmpty() const override; /** * This static function can be used as #deadlineMissedFunc. diff --git a/src/fsfw/osal/linux/PeriodicPosixTask.cpp b/src/fsfw/osal/linux/PeriodicPosixTask.cpp index e1937df4..26b6f53e 100644 --- a/src/fsfw/osal/linux/PeriodicPosixTask.cpp +++ b/src/fsfw/osal/linux/PeriodicPosixTask.cpp @@ -26,12 +26,12 @@ void* PeriodicPosixTask::taskEntryPoint(void* arg) { return NULL; } -ReturnValue_t PeriodicPosixTask::addComponent(object_id_t object) { +ReturnValue_t PeriodicPosixTask::addComponent(object_id_t object, uint8_t opCode) { ExecutableObjectIF* newObject = ObjectManager::instance()->get(object); - return addComponent(newObject); + return addComponent(newObject, opCode); } -ReturnValue_t PeriodicPosixTask::addComponent(ExecutableObjectIF* object) { +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" @@ -43,7 +43,7 @@ ReturnValue_t PeriodicPosixTask::addComponent(ExecutableObjectIF* object) { #endif return HasReturnvaluesIF::RETURN_FAILED; } - objectList.push_back(object); + objectList.emplace(object, opCode); object->setTaskIF(this); return HasReturnvaluesIF::RETURN_OK; @@ -54,6 +54,9 @@ ReturnValue_t PeriodicPosixTask::sleepFor(uint32_t ms) { } ReturnValue_t PeriodicPosixTask::startTask(void) { + if (isEmpty()) { + return HasReturnvaluesIF::RETURN_FAILED; + } started = true; PosixThread::createTask(&taskEntryPoint, this); return HasReturnvaluesIF::RETURN_OK; @@ -64,15 +67,13 @@ void PeriodicPosixTask::taskFunctionality(void) { suspend(); } - for (auto const& object : objectList) { - object->initializeAfterTaskCreation(); - } + initObjsAfterTaskCreation(); uint64_t lastWakeTime = getCurrentMonotonicTimeMs(); // The task's "infinite" inner loop is entered. while (1) { - for (auto const& object : objectList) { - object->performOperation(); + for (auto const& objOpCodePair : objectList) { + objOpCodePair.first->performOperation(objOpCodePair.second); } if (not PosixThread::delayUntil(&lastWakeTime, periodMs)) { @@ -84,3 +85,25 @@ 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 3cd9847a..1142c854 100644 --- a/src/fsfw/osal/linux/PeriodicPosixTask.h +++ b/src/fsfw/osal/linux/PeriodicPosixTask.h @@ -1,7 +1,7 @@ #ifndef FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_ #define FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_ -#include +#include #include "../../objectmanager/ObjectManagerIF.h" #include "../../tasks/ExecutableObjectIF.h" @@ -40,7 +40,7 @@ class PeriodicPosixTask : public PosixThread, public PeriodicTaskIF { * @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) override; + ReturnValue_t addComponent(object_id_t object, uint8_t opCode) override; /** * Adds an object to the list of objects to be executed. @@ -48,14 +48,20 @@ class PeriodicPosixTask : public PosixThread, public PeriodicTaskIF { * @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) override; + 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 std::vector ObjectList; //!< Typedef for the List of objects. + //! Typedef for the List of objects. Will contain the objects to execute and their respective + //! op codes + using ObjectList = std::multiset>; /** * @brief This attribute holds a list of objects to be executed. */ diff --git a/src/fsfw/tasks/FixedSlotSequence.cpp b/src/fsfw/tasks/FixedSlotSequence.cpp index d4c67b4d..62c0e99c 100644 --- a/src/fsfw/tasks/FixedSlotSequence.cpp +++ b/src/fsfw/tasks/FixedSlotSequence.cpp @@ -164,3 +164,5 @@ ReturnValue_t FixedSlotSequence::intializeSequenceAfterTaskCreation() const { void FixedSlotSequence::addCustomCheck(ReturnValue_t (*customCheckFunction)(const SlotList&)) { this->customCheckFunction = customCheckFunction; } + +bool FixedSlotSequence::isEmpty() const { return slotList.empty(); } diff --git a/src/fsfw/tasks/FixedSlotSequence.h b/src/fsfw/tasks/FixedSlotSequence.h index a287c5b2..5ece7126 100644 --- a/src/fsfw/tasks/FixedSlotSequence.h +++ b/src/fsfw/tasks/FixedSlotSequence.h @@ -159,6 +159,8 @@ class FixedSlotSequence { */ ReturnValue_t intializeSequenceAfterTaskCreation() const; + bool isEmpty() const; + protected: /** * @brief This list contains all PollingSlot objects, defining order and diff --git a/src/fsfw/tasks/FixedTimeslotTaskIF.h b/src/fsfw/tasks/FixedTimeslotTaskIF.h index 497db245..9d85ac4a 100644 --- a/src/fsfw/tasks/FixedTimeslotTaskIF.h +++ b/src/fsfw/tasks/FixedTimeslotTaskIF.h @@ -30,7 +30,7 @@ class FixedTimeslotTaskIF : public PeriodicTaskIF { * Check whether the sequence is valid and perform all other required * initialization steps which are needed after task creation */ - virtual ReturnValue_t checkSequence() const = 0; + virtual ReturnValue_t checkSequence() = 0; }; #endif /* FRAMEWORK_TASKS_FIXEDTIMESLOTTASKIF_H_ */ diff --git a/src/fsfw/tasks/PeriodicTaskIF.h b/src/fsfw/tasks/PeriodicTaskIF.h index c78a32de..2ae268fc 100644 --- a/src/fsfw/tasks/PeriodicTaskIF.h +++ b/src/fsfw/tasks/PeriodicTaskIF.h @@ -31,7 +31,7 @@ class PeriodicTaskIF { * Add an object to the task. The object needs to implement ExecutableObjectIF * @return */ - virtual ReturnValue_t addComponent(object_id_t object) { + virtual ReturnValue_t addComponent(object_id_t object, uint8_t opCode = 0) { return HasReturnvaluesIF::RETURN_FAILED; }; @@ -41,13 +41,15 @@ class PeriodicTaskIF { * Add an object to the task. * @return */ - virtual ReturnValue_t addComponent(ExecutableObjectIF* object) { + virtual ReturnValue_t addComponent(ExecutableObjectIF* object, uint8_t opCode = 0) { return HasReturnvaluesIF::RETURN_FAILED; }; virtual ReturnValue_t sleepFor(uint32_t ms) = 0; virtual uint32_t getPeriodMs() const = 0; + + virtual bool isEmpty() const = 0; }; #endif /* PERIODICTASKIF_H_ */ diff --git a/tests/src/fsfw_tests/unit/power/testPowerSwitcher.cpp b/tests/src/fsfw_tests/unit/power/testPowerSwitcher.cpp index d8523558..941055ac 100644 --- a/tests/src/fsfw_tests/unit/power/testPowerSwitcher.cpp +++ b/tests/src/fsfw_tests/unit/power/testPowerSwitcher.cpp @@ -67,7 +67,5 @@ TEST_CASE("Power Switcher", "[power-switcher]") { REQUIRE(not switcherUsingDummy.active()); } - SECTION("More Dummy Tests") { - - } + SECTION("More Dummy Tests") {} }