fsfw/devicehandlers/FixedSlotSequence.cpp

130 lines
3.9 KiB
C++
Raw Normal View History

#include <framework/devicehandlers/FixedSlotSequence.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
2020-04-16 13:17:26 +02:00
#include <cstdlib>
FixedSlotSequence::FixedSlotSequence(uint32_t setLengthMs) :
2020-04-19 21:36:10 +02:00
lengthMs(setLengthMs) {
current = slotList.begin();
}
FixedSlotSequence::~FixedSlotSequence() {
// This should call the destructor on each list entry.
slotList.clear();
// SlotListIter slotListIter = this->slotList.begin();
// //Iterate through slotList and delete all entries.
// while (slotListIter != this->slotList.end()) {
// delete (*slotIt);
// slotIt++;
// }
}
void FixedSlotSequence::executeAndAdvance() {
current->handler->performOperation(current->opcode);
// if (returnValue != RETURN_OK) {
// this->sendErrorMessage( returnValue );
// }
//Increment the polling Sequence iterator
this->current++;
//Set it to the beginning, if the list's end is reached.
if (this->current == this->slotList.end()) {
this->current = this->slotList.begin();
}
}
2018-07-13 15:56:37 +02:00
uint32_t FixedSlotSequence::getIntervalToNextSlotMs() {
uint32_t oldTime;
SlotListIter slotListIter = current;
// Get the pollingTimeMs of the current slot object.
oldTime = slotListIter->pollingTimeMs;
// Advance to the next object.
slotListIter++;
// Find the next interval which is not 0.
while (slotListIter != slotList.end()) {
if (oldTime != slotListIter->pollingTimeMs) {
return slotListIter->pollingTimeMs - oldTime;
} else {
slotListIter++;
}
}
// If the list end is reached (this is definitely an interval != 0),
// the interval is calculated by subtracting the remaining time of the PST
// and adding the start time of the first handler in the list.
slotListIter = slotList.begin();
2020-04-19 21:36:10 +02:00
return lengthMs - oldTime + slotListIter->pollingTimeMs;
}
2018-07-13 15:56:37 +02:00
uint32_t FixedSlotSequence::getIntervalToPreviousSlotMs() {
uint32_t currentTime;
SlotListIter slotListIter = current;
2018-07-13 15:56:37 +02:00
// Get the pollingTimeMs of the current slot object.
currentTime = slotListIter->pollingTimeMs;
2018-07-13 15:56:37 +02:00
//if it is the first slot, calculate difference to last slot
if (slotListIter == slotList.begin()){
2020-04-19 21:36:10 +02:00
return lengthMs - (--slotList.end())->pollingTimeMs + currentTime;
2018-07-13 15:56:37 +02:00
}
// get previous slot
slotListIter--;
2018-07-13 15:56:37 +02:00
return currentTime - slotListIter->pollingTimeMs;
2018-07-13 15:56:37 +02:00
}
bool FixedSlotSequence::slotFollowsImmediately() {
uint32_t currentTime = current->pollingTimeMs;
SlotListIter fixedSequenceIter = this->current;
// Get the pollingTimeMs of the current slot object.
if (fixedSequenceIter == slotList.begin())
return false;
fixedSequenceIter--;
if (fixedSequenceIter->pollingTimeMs == currentTime) {
return true;
} else {
return false;
}
}
uint32_t FixedSlotSequence::getLengthMs() const {
2020-04-19 21:36:10 +02:00
return this->lengthMs;
}
ReturnValue_t FixedSlotSequence::checkSequence() const {
2020-04-16 13:17:26 +02:00
if(slotList.empty()) {
2020-05-07 19:41:42 +02:00
sif::error << "Fixed Slot Sequence: Slot list is empty!" << std::endl;
2020-05-05 19:33:06 +02:00
return HasReturnvaluesIF::RETURN_FAILED;
2020-04-16 13:17:26 +02:00
}
auto slotIt = slotList.begin();
uint32_t count = 0;
uint32_t time = 0;
while (slotIt != slotList.end()) {
if (slotIt->handler == NULL) {
sif::error << "FixedSlotSequene::initialize: ObjectId does not exist!"
<< std::endl;
count++;
} else if (slotIt->pollingTimeMs < time) {
sif::error << "FixedSlotSequence::initialize: Time: "
<< slotIt->pollingTimeMs
<< " is smaller than previous with " << time << std::endl;
count++;
} else {
// All ok, print slot.
//info << "Current slot polling time: " << std::endl;
//info << std::dec << slotIt->pollingTimeMs << std::endl;
}
time = slotIt->pollingTimeMs;
slotIt++;
}
//info << "Number of elements in slot list: "
// << slotList.size() << std::endl;
if (count > 0) {
return HasReturnvaluesIF::RETURN_FAILED;
}
return HasReturnvaluesIF::RETURN_OK;
}
void FixedSlotSequence::addSlot(object_id_t componentId, uint32_t slotTimeMs,
int8_t executionStep, PeriodicTaskIF* executingTask) {
this->slotList.insert(FixedSequenceSlot(componentId, slotTimeMs, executionStep,
executingTask));
this->current = slotList.begin();
}