2018-07-12 16:29:32 +02:00
|
|
|
#include <framework/devicehandlers/FixedSlotSequence.h>
|
|
|
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
|
|
|
|
|
|
|
FixedSlotSequence::FixedSlotSequence(uint32_t setLengthMs) :
|
2020-04-19 21:37:55 +02:00
|
|
|
lengthMs(setLengthMs) {
|
2018-07-12 16:29:32 +02:00
|
|
|
current = slotList.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
FixedSlotSequence::~FixedSlotSequence() {
|
2020-05-07 20:06:17 +02:00
|
|
|
// Call the destructor on each list entry.
|
2020-04-06 11:19:05 +02:00
|
|
|
slotList.clear();
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FixedSlotSequence::executeAndAdvance() {
|
2020-04-06 11:19:05 +02:00
|
|
|
current->handler->performOperation(current->opcode);
|
2018-07-12 16:29:32 +02:00
|
|
|
// 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() {
|
2018-07-12 16:29:32 +02:00
|
|
|
uint32_t oldTime;
|
2020-04-06 11:19:05 +02:00
|
|
|
SlotListIter slotListIter = current;
|
2018-07-12 16:29:32 +02:00
|
|
|
// Get the pollingTimeMs of the current slot object.
|
2020-04-06 11:19:05 +02:00
|
|
|
oldTime = slotListIter->pollingTimeMs;
|
2018-07-12 16:29:32 +02:00
|
|
|
// Advance to the next object.
|
2020-04-06 11:19:05 +02:00
|
|
|
slotListIter++;
|
2018-07-12 16:29:32 +02:00
|
|
|
// Find the next interval which is not 0.
|
2020-04-06 11:19:05 +02:00
|
|
|
while (slotListIter != slotList.end()) {
|
|
|
|
if (oldTime != slotListIter->pollingTimeMs) {
|
|
|
|
return slotListIter->pollingTimeMs - oldTime;
|
2018-07-12 16:29:32 +02:00
|
|
|
} else {
|
2020-04-06 11:19:05 +02:00
|
|
|
slotListIter++;
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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.
|
2020-04-06 11:19:05 +02:00
|
|
|
slotListIter = slotList.begin();
|
2020-04-19 21:37:55 +02:00
|
|
|
return lengthMs - oldTime + slotListIter->pollingTimeMs;
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 15:56:37 +02:00
|
|
|
uint32_t FixedSlotSequence::getIntervalToPreviousSlotMs() {
|
|
|
|
uint32_t currentTime;
|
2020-04-06 11:19:05 +02:00
|
|
|
SlotListIter slotListIter = current;
|
2018-07-13 15:56:37 +02:00
|
|
|
// Get the pollingTimeMs of the current slot object.
|
2020-04-06 11:19:05 +02:00
|
|
|
currentTime = slotListIter->pollingTimeMs;
|
2018-07-13 15:56:37 +02:00
|
|
|
|
|
|
|
//if it is the first slot, calculate difference to last slot
|
2020-04-06 11:19:05 +02:00
|
|
|
if (slotListIter == slotList.begin()){
|
2020-04-19 21:37:55 +02:00
|
|
|
return lengthMs - (--slotList.end())->pollingTimeMs + currentTime;
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
// get previous slot
|
2020-04-06 11:19:05 +02:00
|
|
|
slotListIter--;
|
2018-07-13 15:56:37 +02:00
|
|
|
|
2020-04-06 11:19:05 +02:00
|
|
|
return currentTime - slotListIter->pollingTimeMs;
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
bool FixedSlotSequence::slotFollowsImmediately() {
|
2020-04-06 11:19:05 +02:00
|
|
|
uint32_t currentTime = current->pollingTimeMs;
|
|
|
|
SlotListIter fixedSequenceIter = this->current;
|
2018-07-12 16:29:32 +02:00
|
|
|
// Get the pollingTimeMs of the current slot object.
|
2020-04-06 11:19:05 +02:00
|
|
|
if (fixedSequenceIter == slotList.begin())
|
2018-07-12 16:29:32 +02:00
|
|
|
return false;
|
2020-04-06 11:19:05 +02:00
|
|
|
fixedSequenceIter--;
|
|
|
|
if (fixedSequenceIter->pollingTimeMs == currentTime) {
|
2018-07-12 16:29:32 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t FixedSlotSequence::getLengthMs() const {
|
2020-04-19 21:37:55 +02:00
|
|
|
return this->lengthMs;
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t FixedSlotSequence::checkSequence() const {
|
2020-05-05 18:57:30 +02:00
|
|
|
if(slotList.empty()) {
|
|
|
|
error << "Fixed Slot Sequence: Slot list is empty!" << std::endl;
|
2020-05-05 19:33:06 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
2020-05-05 18:57:30 +02:00
|
|
|
}
|
2020-05-07 20:06:17 +02:00
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
auto slotIt = slotList.begin();
|
|
|
|
uint32_t count = 0;
|
|
|
|
uint32_t time = 0;
|
|
|
|
while (slotIt != slotList.end()) {
|
2020-04-06 11:19:05 +02:00
|
|
|
if (slotIt->handler == NULL) {
|
2018-07-12 16:29:32 +02:00
|
|
|
error << "FixedSlotSequene::initialize: ObjectId does not exist!"
|
|
|
|
<< std::endl;
|
|
|
|
count++;
|
2020-04-06 11:19:05 +02:00
|
|
|
} else if (slotIt->pollingTimeMs < time) {
|
2018-07-12 16:29:32 +02:00
|
|
|
error << "FixedSlotSequence::initialize: Time: "
|
2020-04-06 11:19:05 +02:00
|
|
|
<< slotIt->pollingTimeMs
|
2018-07-12 16:29:32 +02:00
|
|
|
<< " is smaller than previous with " << time << std::endl;
|
|
|
|
count++;
|
|
|
|
} else {
|
2020-04-06 11:19:05 +02:00
|
|
|
// All ok, print slot.
|
|
|
|
//info << "Current slot polling time: " << std::endl;
|
|
|
|
//info << std::dec << slotIt->pollingTimeMs << std::endl;
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
2020-04-06 11:19:05 +02:00
|
|
|
time = slotIt->pollingTimeMs;
|
2018-07-12 16:29:32 +02:00
|
|
|
slotIt++;
|
|
|
|
}
|
2020-04-06 11:19:05 +02:00
|
|
|
//info << "Number of elements in slot list: "
|
|
|
|
// << slotList.size() << std::endl;
|
2018-07-12 16:29:32 +02:00
|
|
|
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) {
|
2020-04-06 11:19:05 +02:00
|
|
|
this->slotList.insert(FixedSequenceSlot(componentId, slotTimeMs, executionStep,
|
|
|
|
executingTask));
|
2018-07-12 16:29:32 +02:00
|
|
|
this->current = slotList.begin();
|
|
|
|
}
|