fsfw/osal/linux/PeriodicPosixTask.cpp

81 lines
2.2 KiB
C++
Raw Normal View History

2020-08-13 20:53:35 +02:00
#include "../../tasks/ExecutableObjectIF.h"
#include "../../serviceinterface/ServiceInterfaceStream.h"
2018-07-13 18:28:26 +02:00
#include <errno.h>
2020-08-13 20:53:35 +02:00
#include "PeriodicPosixTask.h"
2018-07-13 18:28:26 +02:00
PeriodicPosixTask::PeriodicPosixTask(const char* name_, int priority_,
size_t stackSize_, uint32_t period_, void(deadlineMissedFunc_)()):
2020-09-04 14:49:59 +02:00
PosixThread(name_, priority_, stackSize_), objectList(), started(false),
periodMs(period_), deadlineMissedFunc(deadlineMissedFunc_) {
2018-07-13 18:28:26 +02:00
}
PeriodicPosixTask::~PeriodicPosixTask() {
//Not Implemented
}
void* PeriodicPosixTask::taskEntryPoint(void* arg) {
//The argument is re-interpreted as PollingTask.
PeriodicPosixTask *originalTask(reinterpret_cast<PeriodicPosixTask*>(arg));
//The task's functionality is called.
originalTask->taskFunctionality();
return NULL;
}
ReturnValue_t PeriodicPosixTask::addComponent(object_id_t object) {
2018-07-13 18:28:26 +02:00
ExecutableObjectIF* newObject = objectManager->get<ExecutableObjectIF>(
object);
2020-06-23 01:14:28 +02:00
if (newObject == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-09-04 14:49:59 +02:00
sif::error << "PeriodicTask::addComponent: Invalid object. Make sure"
<< " it implements ExecutableObjectIF!" << std::endl;
#endif
2018-07-13 18:28:26 +02:00
return HasReturnvaluesIF::RETURN_FAILED;
}
objectList.push_back(newObject);
newObject->setTaskIF(this);
2020-06-23 01:14:28 +02:00
2018-07-13 18:28:26 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t PeriodicPosixTask::sleepFor(uint32_t ms) {
return PosixThread::sleep((uint64_t)ms*1000000);
}
2020-09-04 14:49:59 +02:00
ReturnValue_t PeriodicPosixTask::startTask(void) {
2018-07-13 18:28:26 +02:00
started = true;
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
//sif::info << stackSize << std::endl;
#endif
PosixThread::createTask(&taskEntryPoint,this);
2018-07-13 18:28:26 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
2020-09-04 14:49:59 +02:00
void PeriodicPosixTask::taskFunctionality(void) {
if(not started) {
2018-07-13 18:28:26 +02:00
suspend();
}
2020-09-04 14:43:53 +02:00
for (auto const &object: objectList) {
object->initializeAfterTaskCreation();
}
2018-07-13 18:28:26 +02:00
uint64_t lastWakeTime = getCurrentMonotonicTimeMs();
//The task's "infinite" inner loop is entered.
while (1) {
2020-09-04 14:43:53 +02:00
for (auto const &object: objectList) {
object->performOperation();
2018-07-13 18:28:26 +02:00
}
2020-09-04 14:43:53 +02:00
if(not PosixThread::delayUntil(&lastWakeTime, periodMs)){
if (this->deadlineMissedFunc != nullptr) {
2018-07-13 18:28:26 +02:00
this->deadlineMissedFunc();
}
}
}
}
uint32_t PeriodicPosixTask::getPeriodMs() const {
return periodMs;
}