TaskIF refactoring continued #91

Merged
meierj merged 16 commits from mueller/task-if-refactoring-eive into develop 2022-05-25 16:11:23 +02:00
12 changed files with 114 additions and 33 deletions
Showing only changes of commit 7df1922633 - Show all commits

View File

@ -205,3 +205,5 @@ ReturnValue_t CommandExecutor::executeBlocking() {
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
const std::vector<char>& CommandExecutor::getReadVector() const { return readVec; }

View File

@ -401,12 +401,33 @@ void SpiComIF::setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed)
if (retval != 0) { if (retval != 0) {
utility::handleIoctlError("SpiComIF::setSpiSpeedAndMode: Setting SPI speed failed"); 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<spi::SpiModes>(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; clockUpdateTransfer.len = 0;
retval = ioctl(spiFd, SPI_IOC_MESSAGE(1), &clockUpdateTransfer); retval = ioctl(spiFd, SPI_IOC_MESSAGE(1), &clockUpdateTransfer);
if (retval != 0) { if (retval != 0) {
utility::handleIoctlError("SpiComIF::setSpiSpeedAndMode: Updating SPI default clock failed"); utility::handleIoctlError("SpiComIF::setSpiSpeedAndMode: Updating SPI default clock failed");
} }
} }
void SpiComIF::setMutexParams(MutexIF::TimeoutType timeoutType_, uint32_t timeoutMs_) {
timeoutType = timeoutType_;
timeoutMs = timeoutMs_;
}

View File

@ -22,15 +22,17 @@ class SpiCookie;
*/ */
class SpiComIF : public DeviceCommunicationIF, public SystemObject { class SpiComIF : public DeviceCommunicationIF, public SystemObject {
public: 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 = static constexpr ReturnValue_t OPENING_FILE_FAILED =
HasReturnvaluesIF::makeReturnCode(spiRetvalId, 0); HasReturnvaluesIF::makeReturnCode(CLASS_ID, 0);
/* Full duplex (ioctl) transfer failure */ /* Full duplex (ioctl) transfer failure */
static constexpr ReturnValue_t FULL_DUPLEX_TRANSFER_FAILED = static constexpr ReturnValue_t FULL_DUPLEX_TRANSFER_FAILED =
HasReturnvaluesIF::makeReturnCode(spiRetvalId, 1); HasReturnvaluesIF::makeReturnCode(CLASS_ID, 1);
/* Half duplex (read/write) transfer failure */ /* Half duplex (read/write) transfer failure */
static constexpr ReturnValue_t HALF_DUPLEX_TRANSFER_FAILED = static constexpr ReturnValue_t HALF_DUPLEX_TRANSFER_FAILED =
HasReturnvaluesIF::makeReturnCode(spiRetvalId, 2); HasReturnvaluesIF::makeReturnCode(CLASS_ID, 2);
SpiComIF(object_id_t objectId, GpioIF* gpioComIF); 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. * the chip select must be driven from outside of the com if.
*/ */
MutexIF* getMutex(MutexIF::TimeoutType* timeoutType = nullptr, uint32_t* timeoutMs = nullptr); 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 * 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(); GpioIF* getGpioInterface();
void setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed); 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); void performSpiWiretapping(SpiCookie* spiCookie);
ReturnValue_t getReadBuffer(address_t spiAddress, uint8_t** buffer); ReturnValue_t getReadBuffer(address_t spiAddress, uint8_t** buffer);
@ -73,7 +93,7 @@ class SpiComIF : public DeviceCommunicationIF, public SystemObject {
MutexIF* spiMutex = nullptr; MutexIF* spiMutex = nullptr;
MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::WAITING; MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::WAITING;
uint32_t timeoutMs = 20; uint32_t timeoutMs = DEFAULT_MUTEX_TIMEOUT;
spi_ioc_transfer clockUpdateTransfer = {}; spi_ioc_transfer clockUpdateTransfer = {};
using SpiDeviceMap = std::unordered_map<address_t, SpiInstance>; using SpiDeviceMap = std::unordered_map<address_t, SpiInstance>;

View File

@ -14,6 +14,8 @@ FixedTimeslotTask::FixedTimeslotTask(const char* name_, int priority_, size_t st
FixedTimeslotTask::~FixedTimeslotTask() {} FixedTimeslotTask::~FixedTimeslotTask() {}
bool FixedTimeslotTask::isEmpty() const { return pst.isEmpty(); }
void* FixedTimeslotTask::taskEntryPoint(void* arg) { void* FixedTimeslotTask::taskEntryPoint(void* arg) {
// The argument is re-interpreted as PollingTask. // The argument is re-interpreted as PollingTask.
FixedTimeslotTask* originalTask(reinterpret_cast<FixedTimeslotTask*>(arg)); FixedTimeslotTask* originalTask(reinterpret_cast<FixedTimeslotTask*>(arg));
@ -50,7 +52,7 @@ ReturnValue_t FixedTimeslotTask::addSlot(object_id_t componentId, uint32_t slotT
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
ReturnValue_t FixedTimeslotTask::checkSequence() const { return pst.checkSequence(); } ReturnValue_t FixedTimeslotTask::checkSequence() { return pst.checkSequence(); }
void FixedTimeslotTask::taskFunctionality() { void FixedTimeslotTask::taskFunctionality() {
// Like FreeRTOS pthreads are running as soon as they are created // Like FreeRTOS pthreads are running as soon as they are created

View File

@ -24,15 +24,18 @@ class FixedTimeslotTask : public FixedTimeslotTaskIF, public PosixThread {
FixedTimeslotTask(const char* name_, int priority_, size_t stackSize_, uint32_t periodMs_); FixedTimeslotTask(const char* name_, int priority_, size_t stackSize_, uint32_t periodMs_);
virtual ~FixedTimeslotTask(); 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. * This static function can be used as #deadlineMissedFunc.

View File

@ -26,12 +26,12 @@ void* PeriodicPosixTask::taskEntryPoint(void* arg) {
return NULL; 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<ExecutableObjectIF>(object); ExecutableObjectIF* newObject = ObjectManager::instance()->get<ExecutableObjectIF>(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 (object == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "PeriodicTask::addComponent: Invalid object. Make sure" sif::error << "PeriodicTask::addComponent: Invalid object. Make sure"
@ -43,7 +43,7 @@ ReturnValue_t PeriodicPosixTask::addComponent(ExecutableObjectIF* object) {
#endif #endif
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
objectList.push_back(object); objectList.emplace(object, opCode);
object->setTaskIF(this); object->setTaskIF(this);
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
@ -54,6 +54,9 @@ ReturnValue_t PeriodicPosixTask::sleepFor(uint32_t ms) {
} }
ReturnValue_t PeriodicPosixTask::startTask(void) { ReturnValue_t PeriodicPosixTask::startTask(void) {
if (isEmpty()) {
return HasReturnvaluesIF::RETURN_FAILED;
}
started = true; started = true;
PosixThread::createTask(&taskEntryPoint, this); PosixThread::createTask(&taskEntryPoint, this);
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
@ -64,15 +67,13 @@ void PeriodicPosixTask::taskFunctionality(void) {
suspend(); suspend();
} }
for (auto const& object : objectList) { initObjsAfterTaskCreation();
object->initializeAfterTaskCreation();
}
uint64_t lastWakeTime = getCurrentMonotonicTimeMs(); uint64_t lastWakeTime = getCurrentMonotonicTimeMs();
// The task's "infinite" inner loop is entered. // The task's "infinite" inner loop is entered.
while (1) { while (1) {
for (auto const& object : objectList) { for (auto const& objOpCodePair : objectList) {
object->performOperation(); objOpCodePair.first->performOperation(objOpCodePair.second);
} }
if (not PosixThread::delayUntil(&lastWakeTime, periodMs)) { if (not PosixThread::delayUntil(&lastWakeTime, periodMs)) {
@ -84,3 +85,25 @@ void PeriodicPosixTask::taskFunctionality(void) {
} }
uint32_t PeriodicPosixTask::getPeriodMs() const { return periodMs; } uint32_t PeriodicPosixTask::getPeriodMs() const { return periodMs; }
bool PeriodicPosixTask::isEmpty() const { return objectList.empty(); }
ReturnValue_t PeriodicPosixTask::initObjsAfterTaskCreation() {
std::multiset<ExecutableObjectIF*> 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;
}

View File

@ -1,7 +1,7 @@
#ifndef FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_ #ifndef FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_
#define FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_ #define FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_
#include <vector> #include <set>
#include "../../objectmanager/ObjectManagerIF.h" #include "../../objectmanager/ObjectManagerIF.h"
#include "../../tasks/ExecutableObjectIF.h" #include "../../tasks/ExecutableObjectIF.h"
@ -40,7 +40,7 @@ class PeriodicPosixTask : public PosixThread, public PeriodicTaskIF {
* @param object Id of the object to add. * @param object Id of the object to add.
* @return RETURN_OK on success, RETURN_FAILED if the object could not be added. * @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. * 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. * @param object pointer to the object to add.
* @return RETURN_OK on success, RETURN_FAILED if the object could not be added. * @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; uint32_t getPeriodMs() const override;
ReturnValue_t sleepFor(uint32_t ms) override; ReturnValue_t sleepFor(uint32_t ms) override;
ReturnValue_t initObjsAfterTaskCreation();
bool isEmpty() const override;
private: private:
typedef std::vector<ExecutableObjectIF*> 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<std::pair<ExecutableObjectIF*, uint8_t>>;
/** /**
* @brief This attribute holds a list of objects to be executed. * @brief This attribute holds a list of objects to be executed.
*/ */

View File

@ -164,3 +164,5 @@ ReturnValue_t FixedSlotSequence::intializeSequenceAfterTaskCreation() const {
void FixedSlotSequence::addCustomCheck(ReturnValue_t (*customCheckFunction)(const SlotList&)) { void FixedSlotSequence::addCustomCheck(ReturnValue_t (*customCheckFunction)(const SlotList&)) {
this->customCheckFunction = customCheckFunction; this->customCheckFunction = customCheckFunction;
} }
bool FixedSlotSequence::isEmpty() const { return slotList.empty(); }

View File

@ -159,6 +159,8 @@ class FixedSlotSequence {
*/ */
ReturnValue_t intializeSequenceAfterTaskCreation() const; ReturnValue_t intializeSequenceAfterTaskCreation() const;
bool isEmpty() const;
protected: protected:
/** /**
* @brief This list contains all PollingSlot objects, defining order and * @brief This list contains all PollingSlot objects, defining order and

View File

@ -30,7 +30,7 @@ class FixedTimeslotTaskIF : public PeriodicTaskIF {
* Check whether the sequence is valid and perform all other required * Check whether the sequence is valid and perform all other required
* initialization steps which are needed after task creation * initialization steps which are needed after task creation
*/ */
virtual ReturnValue_t checkSequence() const = 0; virtual ReturnValue_t checkSequence() = 0;
}; };
#endif /* FRAMEWORK_TASKS_FIXEDTIMESLOTTASKIF_H_ */ #endif /* FRAMEWORK_TASKS_FIXEDTIMESLOTTASKIF_H_ */

View File

@ -31,7 +31,7 @@ class PeriodicTaskIF {
* Add an object to the task. The object needs to implement ExecutableObjectIF * Add an object to the task. The object needs to implement ExecutableObjectIF
* @return * @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; return HasReturnvaluesIF::RETURN_FAILED;
}; };
@ -41,13 +41,15 @@ class PeriodicTaskIF {
* Add an object to the task. * Add an object to the task.
* @return * @return
*/ */
virtual ReturnValue_t addComponent(ExecutableObjectIF* object) { virtual ReturnValue_t addComponent(ExecutableObjectIF* object, uint8_t opCode = 0) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}; };
virtual ReturnValue_t sleepFor(uint32_t ms) = 0; virtual ReturnValue_t sleepFor(uint32_t ms) = 0;
virtual uint32_t getPeriodMs() const = 0; virtual uint32_t getPeriodMs() const = 0;
virtual bool isEmpty() const = 0;
}; };
#endif /* PERIODICTASKIF_H_ */ #endif /* PERIODICTASKIF_H_ */

View File

@ -67,7 +67,5 @@ TEST_CASE("Power Switcher", "[power-switcher]") {
REQUIRE(not switcherUsingDummy.active()); REQUIRE(not switcherUsingDummy.active());
} }
SECTION("More Dummy Tests") { SECTION("More Dummy Tests") {}
}
} }