Merge branch 'master' into mueller/FixedTimeslotUpdate
This commit is contained in:
commit
d38e13f9d3
@ -1,95 +1,124 @@
|
||||
#include "DleEncoder.h"
|
||||
#include "../globalfunctions/DleEncoder.h"
|
||||
|
||||
DleEncoder::DleEncoder() {
|
||||
}
|
||||
DleEncoder::DleEncoder() {}
|
||||
|
||||
DleEncoder::~DleEncoder() {
|
||||
DleEncoder::~DleEncoder() {}
|
||||
|
||||
ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
|
||||
size_t sourceLen, uint8_t* destStream, size_t maxDestLen,
|
||||
size_t* encodedLen, bool addStxEtx) {
|
||||
if (maxDestLen < 2) {
|
||||
return STREAM_TOO_SHORT;
|
||||
}
|
||||
size_t encodedIndex = 0, sourceIndex = 0;
|
||||
uint8_t nextByte;
|
||||
if (addStxEtx) {
|
||||
destStream[0] = STX_CHAR;
|
||||
++encodedIndex;
|
||||
}
|
||||
|
||||
while (encodedIndex < maxDestLen and sourceIndex < sourceLen)
|
||||
{
|
||||
nextByte = sourceStream[sourceIndex];
|
||||
// STX, ETX and CR characters in the stream need to be escaped with DLE
|
||||
if (nextByte == STX_CHAR or nextByte == ETX_CHAR or nextByte == CARRIAGE_RETURN) {
|
||||
if (encodedIndex + 1 >= maxDestLen) {
|
||||
return STREAM_TOO_SHORT;
|
||||
}
|
||||
else {
|
||||
destStream[encodedIndex] = DLE_CHAR;
|
||||
++encodedIndex;
|
||||
/* Escaped byte will be actual byte + 0x40. This prevents
|
||||
* STX, ETX, and carriage return characters from appearing
|
||||
* in the encoded data stream at all, so when polling an
|
||||
* encoded stream, the transmission can be stopped at ETX.
|
||||
* 0x40 was chosen at random with special requirements:
|
||||
* - Prevent going from one control char to another
|
||||
* - Prevent overflow for common characters */
|
||||
destStream[encodedIndex] = nextByte + 0x40;
|
||||
}
|
||||
}
|
||||
// DLE characters are simply escaped with DLE.
|
||||
else if (nextByte == DLE_CHAR) {
|
||||
if (encodedIndex + 1 >= maxDestLen) {
|
||||
return STREAM_TOO_SHORT;
|
||||
}
|
||||
else {
|
||||
destStream[encodedIndex] = DLE_CHAR;
|
||||
++encodedIndex;
|
||||
destStream[encodedIndex] = DLE_CHAR;
|
||||
}
|
||||
}
|
||||
else {
|
||||
destStream[encodedIndex] = nextByte;
|
||||
}
|
||||
++encodedIndex;
|
||||
++sourceIndex;
|
||||
}
|
||||
|
||||
if (sourceIndex == sourceLen and encodedIndex < maxDestLen) {
|
||||
if (addStxEtx) {
|
||||
destStream[encodedIndex] = ETX_CHAR;
|
||||
++encodedIndex;
|
||||
}
|
||||
*encodedLen = encodedIndex;
|
||||
return RETURN_OK;
|
||||
}
|
||||
else {
|
||||
return STREAM_TOO_SHORT;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t DleEncoder::decode(const uint8_t *sourceStream,
|
||||
uint32_t sourceStreamLen, uint32_t *readLen, uint8_t *destStream,
|
||||
uint32_t maxDestStreamlen, uint32_t *decodedLen) {
|
||||
uint32_t encodedIndex = 0, decodedIndex = 0;
|
||||
size_t sourceStreamLen, size_t *readLen, uint8_t *destStream,
|
||||
size_t maxDestStreamlen, size_t *decodedLen) {
|
||||
size_t encodedIndex = 0, decodedIndex = 0;
|
||||
uint8_t nextByte;
|
||||
if (*sourceStream != STX) {
|
||||
return RETURN_FAILED;
|
||||
if (*sourceStream != STX_CHAR) {
|
||||
return DECODING_ERROR;
|
||||
}
|
||||
++encodedIndex;
|
||||
|
||||
while ((encodedIndex < sourceStreamLen) && (decodedIndex < maxDestStreamlen)
|
||||
&& (sourceStream[encodedIndex] != ETX)
|
||||
&& (sourceStream[encodedIndex] != STX)) {
|
||||
if (sourceStream[encodedIndex] == DLE) {
|
||||
&& (sourceStream[encodedIndex] != ETX_CHAR)
|
||||
&& (sourceStream[encodedIndex] != STX_CHAR)) {
|
||||
if (sourceStream[encodedIndex] == DLE_CHAR) {
|
||||
nextByte = sourceStream[encodedIndex + 1];
|
||||
if (nextByte == 0x10) {
|
||||
// The next byte is a DLE character that was escaped by another
|
||||
// DLE character, so we can write it to the destination stream.
|
||||
if (nextByte == DLE_CHAR) {
|
||||
destStream[decodedIndex] = nextByte;
|
||||
} else {
|
||||
if ((nextByte == 0x42) || (nextByte == 0x43)
|
||||
|| (nextByte == 0x4D)) {
|
||||
}
|
||||
else {
|
||||
/* The next byte is a STX, DTX or 0x0D character which
|
||||
* was escaped by a DLE character. The actual byte was
|
||||
* also encoded by adding + 0x40 to prevent having control chars,
|
||||
* in the stream at all, so we convert it back. */
|
||||
if (nextByte == 0x42 or nextByte == 0x43 or nextByte == 0x4D) {
|
||||
destStream[decodedIndex] = nextByte - 0x40;
|
||||
} else {
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
else {
|
||||
return DECODING_ERROR;
|
||||
}
|
||||
}
|
||||
++encodedIndex;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
destStream[decodedIndex] = sourceStream[encodedIndex];
|
||||
}
|
||||
|
||||
++encodedIndex;
|
||||
++decodedIndex;
|
||||
}
|
||||
if (sourceStream[encodedIndex] != ETX) {
|
||||
return RETURN_FAILED;
|
||||
} else {
|
||||
|
||||
if (sourceStream[encodedIndex] != ETX_CHAR) {
|
||||
*readLen = ++encodedIndex;
|
||||
return DECODING_ERROR;
|
||||
}
|
||||
else {
|
||||
*readLen = ++encodedIndex;
|
||||
*decodedLen = decodedIndex;
|
||||
return RETURN_OK;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
|
||||
uint32_t sourceLen, uint8_t* destStream, uint32_t maxDestLen,
|
||||
uint32_t* encodedLen, bool addStxEtx) {
|
||||
if (maxDestLen < 2) {
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
uint32_t encodedIndex = 0, sourceIndex = 0;
|
||||
uint8_t nextByte;
|
||||
if (addStxEtx) {
|
||||
destStream[0] = STX;
|
||||
++encodedIndex;
|
||||
}
|
||||
while ((encodedIndex < maxDestLen) && (sourceIndex < sourceLen)) {
|
||||
nextByte = sourceStream[sourceIndex];
|
||||
if ((nextByte == STX) || (nextByte == ETX) || (nextByte == 0x0D)) {
|
||||
if (encodedIndex + 1 >= maxDestLen) {
|
||||
return RETURN_FAILED;
|
||||
} else {
|
||||
destStream[encodedIndex] = DLE;
|
||||
++encodedIndex;
|
||||
destStream[encodedIndex] = nextByte + 0x40;
|
||||
}
|
||||
} else if (nextByte == DLE) {
|
||||
if (encodedIndex + 1 >= maxDestLen) {
|
||||
return RETURN_FAILED;
|
||||
} else {
|
||||
destStream[encodedIndex] = DLE;
|
||||
++encodedIndex;
|
||||
destStream[encodedIndex] = DLE;
|
||||
}
|
||||
} else {
|
||||
destStream[encodedIndex] = nextByte;
|
||||
}
|
||||
++encodedIndex;
|
||||
++sourceIndex;
|
||||
}
|
||||
if ((sourceIndex == sourceLen) && (encodedIndex < maxDestLen)) {
|
||||
if (addStxEtx) {
|
||||
destStream[encodedIndex] = ETX;
|
||||
++encodedIndex;
|
||||
}
|
||||
*encodedLen = encodedIndex;
|
||||
return RETURN_OK;
|
||||
} else {
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +1,79 @@
|
||||
#ifndef DLEENCODER_H_
|
||||
#define DLEENCODER_H_
|
||||
#ifndef FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_
|
||||
#define FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_
|
||||
|
||||
#include "../returnvalues/HasReturnvaluesIF.h"
|
||||
#include <cstddef>
|
||||
|
||||
/**
|
||||
* @brief This DLE Encoder (Data Link Encoder) can be used to encode and
|
||||
* decode arbitrary data with ASCII control characters
|
||||
* @details
|
||||
* List of control codes:
|
||||
* https://en.wikipedia.org/wiki/C0_and_C1_control_codes
|
||||
*
|
||||
* This encoder can be used to achieve a basic transport layer when using
|
||||
* char based transmission systems.
|
||||
* The passed source strean is converted into a encoded stream by adding
|
||||
* a STX marker at the start of the stream and an ETX marker at the end of
|
||||
* the stream. Any STX, ETX, DLE and CR occurrences in the source stream are
|
||||
* escaped by a DLE character. The encoder also replaces escaped control chars
|
||||
* by another char, so STX, ETX and CR should not appear anywhere in the actual
|
||||
* encoded data stream.
|
||||
*
|
||||
* When using a strictly char based reception of packets encoded with DLE,
|
||||
* STX can be used to notify a reader that actual data will start to arrive
|
||||
* while ETX can be used to notify the reader that the data has ended.
|
||||
*/
|
||||
class DleEncoder: public HasReturnvaluesIF {
|
||||
private:
|
||||
DleEncoder();
|
||||
virtual ~DleEncoder();
|
||||
|
||||
public:
|
||||
static const uint8_t STX = 0x02;
|
||||
static const uint8_t ETX = 0x03;
|
||||
static const uint8_t DLE = 0x10;
|
||||
static constexpr uint8_t INTERFACE_ID = CLASS_ID::DLE_ENCODER;
|
||||
static constexpr ReturnValue_t STREAM_TOO_SHORT = MAKE_RETURN_CODE(0x01);
|
||||
static constexpr ReturnValue_t DECODING_ERROR = MAKE_RETURN_CODE(0x02);
|
||||
|
||||
//! Start Of Text character. First character is encoded stream
|
||||
static constexpr uint8_t STX_CHAR = 0x02;
|
||||
//! End Of Text character. Last character in encoded stream
|
||||
static constexpr uint8_t ETX_CHAR = 0x03;
|
||||
//! Data Link Escape character. Used to escape STX, ETX and DLE occurrences
|
||||
//! in the source stream.
|
||||
static constexpr uint8_t DLE_CHAR = 0x10;
|
||||
static constexpr uint8_t CARRIAGE_RETURN = 0x0D;
|
||||
|
||||
/**
|
||||
* Encodes the give data stream by preceding it with the STX marker
|
||||
* and ending it with an ETX marker. STX, ETX and DLE characters inside
|
||||
* the stream are escaped by DLE characters and also replaced by adding
|
||||
* 0x40 (which is reverted in the decoding process).
|
||||
* @param sourceStream
|
||||
* @param sourceLen
|
||||
* @param destStream
|
||||
* @param maxDestLen
|
||||
* @param encodedLen
|
||||
* @param addStxEtx
|
||||
* Adding STX and ETX can be omitted, if they are added manually.
|
||||
* @return
|
||||
*/
|
||||
static ReturnValue_t encode(const uint8_t *sourceStream, size_t sourceLen,
|
||||
uint8_t *destStream, size_t maxDestLen, size_t *encodedLen,
|
||||
bool addStxEtx = true);
|
||||
|
||||
/**
|
||||
* Converts an encoded stream back.
|
||||
* @param sourceStream
|
||||
* @param sourceStreamLen
|
||||
* @param readLen
|
||||
* @param destStream
|
||||
* @param maxDestStreamlen
|
||||
* @param decodedLen
|
||||
* @return
|
||||
*/
|
||||
static ReturnValue_t decode(const uint8_t *sourceStream,
|
||||
uint32_t sourceStreamLen, uint32_t *readLen, uint8_t *destStream,
|
||||
uint32_t maxDestStreamlen, uint32_t *decodedLen);
|
||||
|
||||
static ReturnValue_t encode(const uint8_t *sourceStream, uint32_t sourceLen,
|
||||
uint8_t *destStream, uint32_t maxDestLen, uint32_t *encodedLen,
|
||||
bool addStxEtx = true);
|
||||
size_t sourceStreamLen, size_t *readLen, uint8_t *destStream,
|
||||
size_t maxDestStreamlen, size_t *decodedLen);
|
||||
};
|
||||
|
||||
#endif /* DLEENCODER_H_ */
|
||||
#endif /* FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_ */
|
||||
|
34
globalfunctions/PeriodicOperationDivider.cpp
Normal file
34
globalfunctions/PeriodicOperationDivider.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include "PeriodicOperationDivider.h"
|
||||
|
||||
|
||||
PeriodicOperationDivider::PeriodicOperationDivider(uint32_t divider,
|
||||
bool resetAutomatically): resetAutomatically(resetAutomatically),
|
||||
counter(divider), divider(divider) {
|
||||
}
|
||||
|
||||
bool PeriodicOperationDivider::checkAndIncrement() {
|
||||
if(counter >= divider) {
|
||||
if(resetAutomatically) {
|
||||
counter = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
counter ++;
|
||||
return false;
|
||||
}
|
||||
|
||||
void PeriodicOperationDivider::resetCounter() {
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
void PeriodicOperationDivider::setDivider(uint32_t newDivider) {
|
||||
divider = newDivider;
|
||||
}
|
||||
|
||||
uint32_t PeriodicOperationDivider::getCounter() const {
|
||||
return counter;
|
||||
}
|
||||
|
||||
uint32_t PeriodicOperationDivider::getDivider() const {
|
||||
return divider;
|
||||
}
|
55
globalfunctions/PeriodicOperationDivider.h
Normal file
55
globalfunctions/PeriodicOperationDivider.h
Normal file
@ -0,0 +1,55 @@
|
||||
#ifndef FSFW_GLOBALFUNCTIONS_PERIODICOPERATIONDIVIDER_H_
|
||||
#define FSFW_GLOBALFUNCTIONS_PERIODICOPERATIONDIVIDER_H_
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
/**
|
||||
* @brief Lightweight helper class to facilitate periodic operation with
|
||||
* decreased frequencies.
|
||||
* @details
|
||||
* This class is useful to perform operations which have to be performed
|
||||
* with a reduced frequency, like debugging printouts in high periodic tasks
|
||||
* or low priority operations.
|
||||
*/
|
||||
class PeriodicOperationDivider {
|
||||
public:
|
||||
/**
|
||||
* Initialize with the desired divider and specify whether the internal
|
||||
* counter will be reset automatically.
|
||||
* @param divider
|
||||
* @param resetAutomatically
|
||||
*/
|
||||
PeriodicOperationDivider(uint32_t divider, bool resetAutomatically = true);
|
||||
|
||||
/**
|
||||
* Check whether operation is necessary.
|
||||
* If an operation is necessary and the class has been
|
||||
* configured to be reset automatically, the counter will be reset.
|
||||
* If not, the counter will be incremented.
|
||||
* @return
|
||||
* -@c true if the counter is larger or equal to the divider
|
||||
* -@c false otherwise
|
||||
*/
|
||||
bool checkAndIncrement();
|
||||
|
||||
/**
|
||||
* Can be used to reset the counter to 0 manually.
|
||||
*/
|
||||
void resetCounter();
|
||||
uint32_t getCounter() const;
|
||||
|
||||
/**
|
||||
* Can be used to set a new divider value.
|
||||
* @param newDivider
|
||||
*/
|
||||
void setDivider(uint32_t newDivider);
|
||||
uint32_t getDivider() const;
|
||||
private:
|
||||
bool resetAutomatically = true;
|
||||
uint32_t counter = 0;
|
||||
uint32_t divider = 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif /* FSFW_GLOBALFUNCTIONS_PERIODICOPERATIONDIVIDER_H_ */
|
@ -1,9 +1,8 @@
|
||||
#include "HealthHelper.h"
|
||||
#include "../ipc/MessageQueueSenderIF.h"
|
||||
#include "../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
HealthHelper::HealthHelper(HasHealthIF* owner, object_id_t objectId) :
|
||||
healthTable(NULL), eventSender(NULL), objectId(objectId), parentQueue(
|
||||
0), owner(owner) {
|
||||
objectId(objectId), owner(owner) {
|
||||
}
|
||||
|
||||
HealthHelper::~HealthHelper() {
|
||||
@ -40,9 +39,19 @@ void HealthHelper::setParentQueue(MessageQueueId_t parentQueue) {
|
||||
ReturnValue_t HealthHelper::initialize() {
|
||||
healthTable = objectManager->get<HealthTableIF>(objects::HEALTH_TABLE);
|
||||
eventSender = objectManager->get<EventReportingProxyIF>(objectId);
|
||||
if ((healthTable == NULL) || eventSender == NULL) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
|
||||
if (healthTable == nullptr) {
|
||||
sif::error << "HealthHelper::initialize: Health table object needs"
|
||||
"to be created in factory." << std::endl;
|
||||
return ObjectManagerIF::CHILD_INIT_FAILED;
|
||||
}
|
||||
|
||||
if(eventSender == nullptr) {
|
||||
sif::error << "HealthHelper::initialize: Owner has to implement "
|
||||
"ReportingProxyIF." << std::endl;
|
||||
return ObjectManagerIF::CHILD_INIT_FAILED;
|
||||
}
|
||||
|
||||
ReturnValue_t result = healthTable->registerObject(objectId,
|
||||
HasHealthIF::HEALTHY);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
@ -62,22 +71,22 @@ void HealthHelper::setHealth(HasHealthIF::HealthState health) {
|
||||
|
||||
void HealthHelper::informParent(HasHealthIF::HealthState health,
|
||||
HasHealthIF::HealthState oldHealth) {
|
||||
if (parentQueue == 0) {
|
||||
if (parentQueue == MessageQueueIF::NO_QUEUE) {
|
||||
return;
|
||||
}
|
||||
CommandMessage message;
|
||||
HealthMessage::setHealthMessage(&message, HealthMessage::HEALTH_INFO,
|
||||
CommandMessage information;
|
||||
HealthMessage::setHealthMessage(&information, HealthMessage::HEALTH_INFO,
|
||||
health, oldHealth);
|
||||
if (MessageQueueSenderIF::sendMessage(parentQueue, &message,
|
||||
owner->getCommandQueue()) != HasReturnvaluesIF::RETURN_OK) {
|
||||
if (MessageQueueSenderIF::sendMessage(parentQueue, &information,
|
||||
owner->getCommandQueue()) != HasReturnvaluesIF::RETURN_OK) {
|
||||
sif::debug << "HealthHelper::informParent: sending health reply failed."
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void HealthHelper::handleSetHealthCommand(CommandMessage* message) {
|
||||
ReturnValue_t result = owner->setHealth(HealthMessage::getHealth(message));
|
||||
if (message->getSender() == 0) {
|
||||
void HealthHelper::handleSetHealthCommand(CommandMessage* command) {
|
||||
ReturnValue_t result = owner->setHealth(HealthMessage::getHealth(command));
|
||||
if (command->getSender() == MessageQueueIF::NO_QUEUE) {
|
||||
return;
|
||||
}
|
||||
CommandMessage reply;
|
||||
@ -85,12 +94,12 @@ void HealthHelper::handleSetHealthCommand(CommandMessage* message) {
|
||||
HealthMessage::setHealthMessage(&reply,
|
||||
HealthMessage::REPLY_HEALTH_SET);
|
||||
} else {
|
||||
reply.setReplyRejected(result, message->getCommand());
|
||||
reply.setReplyRejected(result, command->getCommand());
|
||||
}
|
||||
if (MessageQueueSenderIF::sendMessage(message->getSender(), &reply,
|
||||
owner->getCommandQueue()) != HasReturnvaluesIF::RETURN_OK) {
|
||||
sif::debug
|
||||
<< "HealthHelper::handleHealthCommand: sending health reply failed."
|
||||
<< std::endl;
|
||||
if (MessageQueueSenderIF::sendMessage(command->getSender(), &reply,
|
||||
owner->getCommandQueue()) != HasReturnvaluesIF::RETURN_OK) {
|
||||
sif::debug << "HealthHelper::handleHealthCommand: sending health "
|
||||
"reply failed." << std::endl;
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
#ifndef HEALTHHELPER_H_
|
||||
#define HEALTHHELPER_H_
|
||||
#ifndef FSFW_HEALTH_HEALTHHELPER_H_
|
||||
#define FSFW_HEALTH_HEALTHHELPER_H_
|
||||
|
||||
#include "../events/EventManagerIF.h"
|
||||
#include "../events/EventReportingProxyIF.h"
|
||||
#include "HasHealthIF.h"
|
||||
#include "HealthMessage.h"
|
||||
#include "HealthTableIF.h"
|
||||
|
||||
#include "../events/EventManagerIF.h"
|
||||
#include "../events/EventReportingProxyIF.h"
|
||||
#include "../ipc/MessageQueueIF.h"
|
||||
#include "../objectmanager/ObjectManagerIF.h"
|
||||
#include "../returnvalues/HasReturnvaluesIF.h"
|
||||
|
||||
@ -27,8 +29,8 @@ public:
|
||||
/**
|
||||
* ctor
|
||||
*
|
||||
* @param owner
|
||||
* @param objectId the object Id to use when communication with the HealthTable
|
||||
* @param useAsFrom id to use as from id when sending replies, can be set to 0
|
||||
*/
|
||||
HealthHelper(HasHealthIF* owner, object_id_t objectId);
|
||||
|
||||
@ -39,12 +41,12 @@ public:
|
||||
*
|
||||
* only valid after initialize() has been called
|
||||
*/
|
||||
HealthTableIF *healthTable;
|
||||
HealthTableIF *healthTable = nullptr;
|
||||
|
||||
/**
|
||||
* Proxy to forward events.
|
||||
*/
|
||||
EventReportingProxyIF* eventSender;
|
||||
EventReportingProxyIF* eventSender = nullptr;
|
||||
|
||||
/**
|
||||
* Try to handle the message.
|
||||
@ -100,7 +102,7 @@ private:
|
||||
/**
|
||||
* The Queue of the parent
|
||||
*/
|
||||
MessageQueueId_t parentQueue;
|
||||
MessageQueueId_t parentQueue = MessageQueueIF::NO_QUEUE;
|
||||
|
||||
/**
|
||||
* The one using the healthHelper.
|
||||
@ -117,4 +119,4 @@ private:
|
||||
void handleSetHealthCommand(CommandMessage *message);
|
||||
};
|
||||
|
||||
#endif /* HEALTHHELPER_H_ */
|
||||
#endif /* FSFW_HEALTH_HEALTHHELPER_H_ */
|
||||
|
@ -64,6 +64,11 @@ ReturnValue_t PeriodicTask::sleepFor(uint32_t ms) {
|
||||
void PeriodicTask::taskFunctionality() {
|
||||
TickType_t xLastWakeTime;
|
||||
const TickType_t xPeriod = pdMS_TO_TICKS(this->period * 1000.);
|
||||
|
||||
for (auto const &object: objectList) {
|
||||
object->initializeAfterTaskCreation();
|
||||
}
|
||||
|
||||
/* The xLastWakeTime variable needs to be initialized with the current tick
|
||||
count. Note that this is the only time the variable is written to
|
||||
explicitly. After this assignment, xLastWakeTime is updated automatically
|
||||
|
@ -1,11 +1,10 @@
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_PERIODICTASK_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_PERIODICTASK_H_
|
||||
#ifndef FSFW_OSAL_FREERTOS_PERIODICTASK_H_
|
||||
#define FSFW_OSAL_FREERTOS_PERIODICTASK_H_
|
||||
|
||||
#include "FreeRTOSTaskIF.h"
|
||||
#include "../../objectmanager/ObjectManagerIF.h"
|
||||
#include "../../tasks/PeriodicTaskIF.h"
|
||||
#include "../../tasks/Typedef.h"
|
||||
#include "FreeRTOSTaskIF.h"
|
||||
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
@ -24,7 +23,6 @@ public:
|
||||
/**
|
||||
* Keep in Mind that you need to call before this vTaskStartScheduler()!
|
||||
* A lot of task parameters are set in "FreeRTOSConfig.h".
|
||||
* TODO: why does this need to be called before vTaskStartScheduler?
|
||||
* @details
|
||||
* The class is initialized without allocated objects.
|
||||
* These need to be added with #addComponent.
|
||||
@ -125,4 +123,4 @@ protected:
|
||||
void handleMissedDeadline();
|
||||
};
|
||||
|
||||
#endif /* PERIODICTASK_H_ */
|
||||
#endif /* FSFW_OSAL_FREERTOS_PERIODICTASK_H_ */
|
||||
|
227
osal/host/Clock.cpp
Normal file
227
osal/host/Clock.cpp
Normal file
@ -0,0 +1,227 @@
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
#include "../../timemanager/Clock.h"
|
||||
|
||||
#include <chrono>
|
||||
#if defined(WIN32)
|
||||
#include <windows.h>
|
||||
#elif defined(LINUX)
|
||||
#include <fstream>
|
||||
#endif
|
||||
|
||||
uint16_t Clock::leapSeconds = 0;
|
||||
MutexIF* Clock::timeMutex = NULL;
|
||||
|
||||
using SystemClock = std::chrono::system_clock;
|
||||
|
||||
uint32_t Clock::getTicksPerSecond(void){
|
||||
sif::warning << "Clock::getTicksPerSecond: not implemented yet" << std::endl;
|
||||
return 0;
|
||||
//return CLOCKS_PER_SEC;
|
||||
//uint32_t ticks = sysconf(_SC_CLK_TCK);
|
||||
//return ticks;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::setClock(const TimeOfDay_t* time) {
|
||||
// do some magic with chrono
|
||||
sif::warning << "Clock::setClock: not implemented yet" << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::setClock(const timeval* time) {
|
||||
// do some magic with chrono
|
||||
#if defined(WIN32)
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
#elif defined(LINUX)
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
#else
|
||||
|
||||
#endif
|
||||
sif::warning << "Clock::getUptime: Not implemented for found OS" << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getClock_timeval(timeval* time) {
|
||||
#if defined(WIN32)
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto secondsChrono = std::chrono::time_point_cast<std::chrono::seconds>(now);
|
||||
auto epoch = now.time_since_epoch();
|
||||
time->tv_sec = std::chrono::duration_cast<std::chrono::seconds>(epoch).count();
|
||||
auto fraction = now - secondsChrono;
|
||||
time->tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
fraction).count();
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
#elif defined(LINUX)
|
||||
timespec timeUnix;
|
||||
int status = clock_gettime(CLOCK_REALTIME,&timeUnix);
|
||||
if(status!=0){
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
time->tv_sec = timeUnix.tv_sec;
|
||||
time->tv_usec = timeUnix.tv_nsec / 1000.0;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
#else
|
||||
sif::warning << "Clock::getUptime: Not implemented for found OS" << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getClock_usecs(uint64_t* time) {
|
||||
// do some magic with chrono
|
||||
sif::warning << "Clock::gerClock_usecs: not implemented yet" << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
timeval Clock::getUptime() {
|
||||
timeval timeval;
|
||||
#if defined(WIN32)
|
||||
auto uptime = std::chrono::milliseconds(GetTickCount64());
|
||||
auto secondsChrono = std::chrono::duration_cast<std::chrono::seconds>(uptime);
|
||||
timeval.tv_sec = secondsChrono.count();
|
||||
auto fraction = uptime - secondsChrono;
|
||||
timeval.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
fraction).count();
|
||||
#elif defined(LINUX)
|
||||
double uptimeSeconds;
|
||||
if (std::ifstream("/proc/uptime", std::ios::in) >> uptimeSeconds)
|
||||
{
|
||||
// value is rounded down automatically
|
||||
timeval.tv_sec = uptimeSeconds;
|
||||
timeval.tv_usec = uptimeSeconds *(double) 1e6 - (timeval.tv_sec *1e6);
|
||||
}
|
||||
#else
|
||||
sif::warning << "Clock::getUptime: Not implemented for found OS" << std::endl;
|
||||
#endif
|
||||
return timeval;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getUptime(timeval* uptime) {
|
||||
*uptime = getUptime();
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getUptime(uint32_t* uptimeMs) {
|
||||
timeval uptime = getUptime();
|
||||
*uptimeMs = uptime.tv_sec * 1000 + uptime.tv_usec / 1000;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
|
||||
ReturnValue_t Clock::getDateAndTime(TimeOfDay_t* time) {
|
||||
// do some magic with chrono (C++20!)
|
||||
// Right now, the library doesn't have the new features yet.
|
||||
// so we work around that for now.
|
||||
auto now = SystemClock::now();
|
||||
auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
|
||||
auto fraction = now - seconds;
|
||||
time_t tt = SystemClock::to_time_t(now);
|
||||
struct tm* timeInfo;
|
||||
timeInfo = gmtime(&tt);
|
||||
time->year = timeInfo->tm_year + 1900;
|
||||
time->month = timeInfo->tm_mon+1;
|
||||
time->day = timeInfo->tm_mday;
|
||||
time->hour = timeInfo->tm_hour;
|
||||
time->minute = timeInfo->tm_min;
|
||||
time->second = timeInfo->tm_sec;
|
||||
auto usecond = std::chrono::duration_cast<std::chrono::microseconds>(fraction);
|
||||
time->usecond = usecond.count();
|
||||
|
||||
//sif::warning << "Clock::getDateAndTime: not implemented yet" << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::convertTimeOfDayToTimeval(const TimeOfDay_t* from,
|
||||
timeval* to) {
|
||||
struct tm time_tm;
|
||||
|
||||
time_tm.tm_year = from->year - 1900;
|
||||
time_tm.tm_mon = from->month - 1;
|
||||
time_tm.tm_mday = from->day;
|
||||
|
||||
time_tm.tm_hour = from->hour;
|
||||
time_tm.tm_min = from->minute;
|
||||
time_tm.tm_sec = from->second;
|
||||
|
||||
time_t seconds = mktime(&time_tm);
|
||||
|
||||
to->tv_sec = seconds;
|
||||
to->tv_usec = from->usecond;
|
||||
//Fails in 2038..
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
sif::warning << "Clock::convertTimeBla: not implemented yet" << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::convertTimevalToJD2000(timeval time, double* JD2000) {
|
||||
*JD2000 = (time.tv_sec - 946728000. + time.tv_usec / 1000000.) / 24.
|
||||
/ 3600.;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval* tt) {
|
||||
//SHOULDDO: works not for dates in the past (might have less leap seconds)
|
||||
if (timeMutex == NULL) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
|
||||
uint16_t leapSeconds;
|
||||
ReturnValue_t result = getLeapSeconds(&leapSeconds);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
timeval leapSeconds_timeval = { 0, 0 };
|
||||
leapSeconds_timeval.tv_sec = leapSeconds;
|
||||
|
||||
//initial offset between UTC and TAI
|
||||
timeval UTCtoTAI1972 = { 10, 0 };
|
||||
|
||||
timeval TAItoTT = { 32, 184000 };
|
||||
|
||||
*tt = utc + leapSeconds_timeval + UTCtoTAI1972 + TAItoTT;
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::setLeapSeconds(const uint16_t leapSeconds_) {
|
||||
if(checkOrCreateClockMutex()!=HasReturnvaluesIF::RETURN_OK){
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
ReturnValue_t result = timeMutex->lockMutex(MutexIF::BLOCKING);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
leapSeconds = leapSeconds_;
|
||||
|
||||
result = timeMutex->unlockMutex();
|
||||
return result;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getLeapSeconds(uint16_t* leapSeconds_) {
|
||||
if(timeMutex == nullptr){
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
ReturnValue_t result = timeMutex->lockMutex(MutexIF::BLOCKING);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
*leapSeconds_ = leapSeconds;
|
||||
|
||||
result = timeMutex->unlockMutex();
|
||||
return result;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::checkOrCreateClockMutex(){
|
||||
if(timeMutex == nullptr){
|
||||
MutexFactory* mutexFactory = MutexFactory::instance();
|
||||
if (mutexFactory == nullptr) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
timeMutex = mutexFactory->createMutex();
|
||||
if (timeMutex == nullptr) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
197
osal/host/FixedTimeslotTask.cpp
Normal file
197
osal/host/FixedTimeslotTask.cpp
Normal file
@ -0,0 +1,197 @@
|
||||
#include "../../osal/host/FixedTimeslotTask.h"
|
||||
|
||||
#include "../../ipc/MutexFactory.h"
|
||||
#include "../../osal/host/Mutex.h"
|
||||
#include "../../osal/host/FixedTimeslotTask.h"
|
||||
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
#include "../../tasks/ExecutableObjectIF.h"
|
||||
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
#if defined(WIN32)
|
||||
#include <windows.h>
|
||||
#elif defined(LINUX)
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
FixedTimeslotTask::FixedTimeslotTask(const char *name, TaskPriority setPriority,
|
||||
TaskStackSize setStack, TaskPeriod setPeriod,
|
||||
void (*setDeadlineMissedFunc)()) :
|
||||
started(false), pollingSeqTable(setPeriod*1000), taskName(name),
|
||||
period(setPeriod), deadlineMissedFunc(setDeadlineMissedFunc) {
|
||||
// It is propably possible to set task priorities by using the native
|
||||
// task handles for Windows / Linux
|
||||
mainThread = std::thread(&FixedTimeslotTask::taskEntryPoint, this, this);
|
||||
#if defined(WIN32)
|
||||
/* List of possible priority classes:
|
||||
* https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/
|
||||
* nf-processthreadsapi-setpriorityclass
|
||||
* And respective thread priority numbers:
|
||||
* https://docs.microsoft.com/en-us/windows/
|
||||
* win32/procthread/scheduling-priorities */
|
||||
int result = SetPriorityClass(
|
||||
reinterpret_cast<HANDLE>(mainThread.native_handle()),
|
||||
ABOVE_NORMAL_PRIORITY_CLASS);
|
||||
if(result != 0) {
|
||||
sif::error << "FixedTimeslotTask: Windows SetPriorityClass failed with code "
|
||||
<< GetLastError() << std::endl;
|
||||
}
|
||||
result = SetThreadPriority(
|
||||
reinterpret_cast<HANDLE>(mainThread.native_handle()),
|
||||
THREAD_PRIORITY_NORMAL);
|
||||
if(result != 0) {
|
||||
sif::error << "FixedTimeslotTask: Windows SetPriorityClass failed with code "
|
||||
<< GetLastError() << std::endl;
|
||||
}
|
||||
#elif defined(LINUX)
|
||||
// we can just copy and paste the code from linux here.
|
||||
#endif
|
||||
}
|
||||
|
||||
FixedTimeslotTask::~FixedTimeslotTask(void) {
|
||||
//Do not delete objects, we were responsible for ptrs only.
|
||||
terminateThread = true;
|
||||
if(mainThread.joinable()) {
|
||||
mainThread.join();
|
||||
}
|
||||
delete this;
|
||||
}
|
||||
|
||||
void FixedTimeslotTask::taskEntryPoint(void* argument) {
|
||||
FixedTimeslotTask *originalTask(reinterpret_cast<FixedTimeslotTask*>(argument));
|
||||
|
||||
if (not originalTask->started) {
|
||||
// we have to suspend/block here until the task is started.
|
||||
// if semaphores are implemented, use them here.
|
||||
std::unique_lock<std::mutex> lock(initMutex);
|
||||
initCondition.wait(lock);
|
||||
}
|
||||
|
||||
this->taskFunctionality();
|
||||
sif::debug << "FixedTimeslotTask::taskEntryPoint: "
|
||||
"Returned from taskFunctionality." << std::endl;
|
||||
}
|
||||
|
||||
ReturnValue_t FixedTimeslotTask::startTask() {
|
||||
started = true;
|
||||
|
||||
// Notify task to start.
|
||||
std::lock_guard<std::mutex> lock(initMutex);
|
||||
initCondition.notify_one();
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t FixedTimeslotTask::sleepFor(uint32_t ms) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
void FixedTimeslotTask::taskFunctionality() {
|
||||
pollingSeqTable.intializeSequenceAfterTaskCreation();
|
||||
|
||||
// A local iterator for the Polling Sequence Table is created to
|
||||
// find the start time for the first entry.
|
||||
auto slotListIter = pollingSeqTable.current;
|
||||
|
||||
// Get start time for first entry.
|
||||
chron_ms interval(slotListIter->pollingTimeMs);
|
||||
auto currentStartTime {
|
||||
std::chrono::duration_cast<chron_ms>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
};
|
||||
if(interval.count() > 0) {
|
||||
delayForInterval(¤tStartTime, interval);
|
||||
}
|
||||
|
||||
/* Enter the loop that defines the task behavior. */
|
||||
for (;;) {
|
||||
if(terminateThread.load()) {
|
||||
break;
|
||||
}
|
||||
//The component for this slot is executed and the next one is chosen.
|
||||
this->pollingSeqTable.executeAndAdvance();
|
||||
if (not pollingSeqTable.slotFollowsImmediately()) {
|
||||
// we need to wait before executing the current slot
|
||||
//this gives us the time to wait:
|
||||
interval = chron_ms(this->pollingSeqTable.getIntervalToPreviousSlotMs());
|
||||
delayForInterval(¤tStartTime, interval);
|
||||
//TODO deadline missed check
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t FixedTimeslotTask::addSlot(object_id_t componentId,
|
||||
uint32_t slotTimeMs, int8_t executionStep) {
|
||||
ExecutableObjectIF* executableObject = objectManager->
|
||||
get<ExecutableObjectIF>(componentId);
|
||||
if (executableObject != nullptr) {
|
||||
pollingSeqTable.addSlot(componentId, slotTimeMs, executionStep,
|
||||
executableObject, this);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
sif::error << "Component " << std::hex << componentId <<
|
||||
" not found, not adding it to pst" << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
|
||||
ReturnValue_t FixedTimeslotTask::checkSequence() const {
|
||||
return pollingSeqTable.checkSequence();
|
||||
}
|
||||
|
||||
uint32_t FixedTimeslotTask::getPeriodMs() const {
|
||||
return period * 1000;
|
||||
}
|
||||
|
||||
bool FixedTimeslotTask::delayForInterval(chron_ms * previousWakeTimeMs,
|
||||
const chron_ms interval) {
|
||||
bool shouldDelay = false;
|
||||
//Get current wakeup time
|
||||
auto currentStartTime =
|
||||
std::chrono::duration_cast<chron_ms>(
|
||||
std::chrono::system_clock::now().time_since_epoch());
|
||||
/* Generate the tick time at which the task wants to wake. */
|
||||
auto nextTimeToWake_ms = (*previousWakeTimeMs) + interval;
|
||||
|
||||
if (currentStartTime < *previousWakeTimeMs) {
|
||||
/* The tick count has overflowed since this function was
|
||||
lasted called. In this case the only time we should ever
|
||||
actually delay is if the wake time has also overflowed,
|
||||
and the wake time is greater than the tick time. When this
|
||||
is the case it is as if neither time had overflowed. */
|
||||
if ((nextTimeToWake_ms < *previousWakeTimeMs)
|
||||
&& (nextTimeToWake_ms > currentStartTime)) {
|
||||
shouldDelay = true;
|
||||
}
|
||||
} else {
|
||||
/* The tick time has not overflowed. In this case we will
|
||||
delay if either the wake time has overflowed, and/or the
|
||||
tick time is less than the wake time. */
|
||||
if ((nextTimeToWake_ms < *previousWakeTimeMs)
|
||||
|| (nextTimeToWake_ms > currentStartTime)) {
|
||||
shouldDelay = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* Update the wake time ready for the next call. */
|
||||
|
||||
(*previousWakeTimeMs) = nextTimeToWake_ms;
|
||||
|
||||
if (shouldDelay) {
|
||||
auto sleepTime = std::chrono::duration_cast<chron_ms>(
|
||||
nextTimeToWake_ms - currentStartTime);
|
||||
std::this_thread::sleep_for(sleepTime);
|
||||
return true;
|
||||
}
|
||||
//We are shifting the time in case the deadline was missed like rtems
|
||||
(*previousWakeTimeMs) = currentStartTime;
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
130
osal/host/FixedTimeslotTask.h
Normal file
130
osal/host/FixedTimeslotTask.h
Normal file
@ -0,0 +1,130 @@
|
||||
#ifndef FRAMEWORK_OSAL_HOST_FIXEDTIMESLOTTASK_H_
|
||||
#define FRAMEWORK_OSAL_HOST_FIXEDTIMESLOTTASK_H_
|
||||
|
||||
#include "../../objectmanager/ObjectManagerIF.h"
|
||||
#include "../../tasks/FixedSlotSequence.h"
|
||||
#include "../../tasks/FixedTimeslotTaskIF.h"
|
||||
#include "../../tasks/Typedef.h"
|
||||
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <condition_variable>
|
||||
#include <atomic>
|
||||
|
||||
class ExecutableObjectIF;
|
||||
|
||||
/**
|
||||
* @brief This class represents a task for periodic activities with multiple
|
||||
* steps and strict timeslot requirements for these steps.
|
||||
* @details
|
||||
* @ingroup task_handling
|
||||
*/
|
||||
class FixedTimeslotTask: public FixedTimeslotTaskIF {
|
||||
public:
|
||||
/**
|
||||
* @brief Standard constructor of the class.
|
||||
* @details
|
||||
* The class is initialized without allocated objects. These need to be
|
||||
* added with #addComponent.
|
||||
* @param priority
|
||||
* @param stack_size
|
||||
* @param setPeriod
|
||||
* @param setDeadlineMissedFunc
|
||||
* The function pointer to the deadline missed function that shall be
|
||||
* assigned.
|
||||
*/
|
||||
FixedTimeslotTask(const char *name, TaskPriority setPriority,
|
||||
TaskStackSize setStack, TaskPeriod setPeriod,
|
||||
void (*setDeadlineMissedFunc)());
|
||||
/**
|
||||
* @brief Currently, the executed object's lifetime is not coupled with
|
||||
* the task object's lifetime, so the destructor is empty.
|
||||
*/
|
||||
virtual ~FixedTimeslotTask(void);
|
||||
|
||||
/**
|
||||
* @brief The method to start the task.
|
||||
* @details The method starts the task with the respective system call.
|
||||
* Entry point is the taskEntryPoint method described below.
|
||||
* The address of the task object is passed as an argument
|
||||
* to the system call.
|
||||
*/
|
||||
ReturnValue_t startTask(void);
|
||||
|
||||
/**
|
||||
* Add timeslot to the polling sequence table.
|
||||
* @param componentId
|
||||
* @param slotTimeMs
|
||||
* @param executionStep
|
||||
* @return
|
||||
*/
|
||||
ReturnValue_t addSlot(object_id_t componentId,
|
||||
uint32_t slotTimeMs, int8_t executionStep);
|
||||
|
||||
ReturnValue_t checkSequence() const override;
|
||||
|
||||
uint32_t getPeriodMs() const;
|
||||
|
||||
ReturnValue_t sleepFor(uint32_t ms);
|
||||
|
||||
protected:
|
||||
using chron_ms = std::chrono::milliseconds;
|
||||
|
||||
bool started;
|
||||
//!< Typedef for the List of objects.
|
||||
typedef std::vector<ExecutableObjectIF*> ObjectList;
|
||||
std::thread mainThread;
|
||||
std::atomic<bool> terminateThread = false;
|
||||
|
||||
//! Polling sequence table which contains the object to execute
|
||||
//! and information like the timeslots and the passed execution step.
|
||||
FixedSlotSequence pollingSeqTable;
|
||||
|
||||
std::condition_variable initCondition;
|
||||
std::mutex initMutex;
|
||||
std::string taskName;
|
||||
/**
|
||||
* @brief The period of the task.
|
||||
* @details
|
||||
* The period determines the frequency of the task's execution.
|
||||
* It is expressed in clock ticks.
|
||||
*/
|
||||
TaskPeriod period;
|
||||
|
||||
/**
|
||||
* @brief The pointer to the deadline-missed function.
|
||||
* @details
|
||||
* This pointer stores the function that is executed if the task's deadline
|
||||
* is missed. So, each may react individually on a timing failure.
|
||||
* The pointer may be NULL, then nothing happens on missing the deadline.
|
||||
* The deadline is equal to the next execution of the periodic task.
|
||||
*/
|
||||
void (*deadlineMissedFunc)(void);
|
||||
/**
|
||||
* @brief This is the function executed in the new task's context.
|
||||
* @details
|
||||
* It converts the argument back to the thread object type and copies the
|
||||
* class instance to the task context.
|
||||
* The taskFunctionality method is called afterwards.
|
||||
* @param A pointer to the task object itself is passed as argument.
|
||||
*/
|
||||
|
||||
void taskEntryPoint(void* argument);
|
||||
/**
|
||||
* @brief The function containing the actual functionality of the task.
|
||||
* @details
|
||||
* The method sets and starts the task's period, then enters a loop that is
|
||||
* repeated as long as the isRunning attribute is true. Within the loop,
|
||||
* all performOperation methods of the added objects are called. Afterwards
|
||||
* the checkAndRestartPeriod system call blocks the task until the next
|
||||
* period. On missing the deadline, the deadlineMissedFunction is executed.
|
||||
*/
|
||||
void taskFunctionality(void);
|
||||
|
||||
bool delayForInterval(chron_ms * previousWakeTimeMs,
|
||||
const chron_ms interval);
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_HOST_FIXEDTIMESLOTTASK_H_ */
|
159
osal/host/MessageQueue.cpp
Normal file
159
osal/host/MessageQueue.cpp
Normal file
@ -0,0 +1,159 @@
|
||||
#include "MessageQueue.h"
|
||||
#include "QueueMapManager.h"
|
||||
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
#include "../../ipc/MutexFactory.h"
|
||||
#include "../../ipc/MutexHelper.h"
|
||||
|
||||
MessageQueue::MessageQueue(size_t messageDepth, size_t maxMessageSize):
|
||||
messageSize(maxMessageSize), messageDepth(messageDepth) {
|
||||
queueLock = MutexFactory::instance()->createMutex();
|
||||
auto result = QueueMapManager::instance()->addMessageQueue(this, &mqId);
|
||||
if(result != HasReturnvaluesIF::RETURN_OK) {
|
||||
sif::error << "MessageQueue::MessageQueue:"
|
||||
<< " Could not be created" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
MessageQueue::~MessageQueue() {
|
||||
MutexFactory::instance()->deleteMutex(queueLock);
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::sendMessage(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, bool ignoreFault) {
|
||||
return sendMessageFrom(sendTo, message, this->getId(), ignoreFault);
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::sendToDefault(MessageQueueMessageIF* message) {
|
||||
return sendToDefaultFrom(message, this->getId());
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::sendToDefaultFrom(MessageQueueMessageIF* message,
|
||||
MessageQueueId_t sentFrom, bool ignoreFault) {
|
||||
return sendMessageFrom(defaultDestination,message,sentFrom,ignoreFault);
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::reply(MessageQueueMessageIF* message) {
|
||||
if (this->lastPartner != 0) {
|
||||
return sendMessageFrom(this->lastPartner, message, this->getId());
|
||||
} else {
|
||||
return MessageQueueIF::NO_REPLY_PARTNER;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::sendMessageFrom(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, MessageQueueId_t sentFrom,
|
||||
bool ignoreFault) {
|
||||
return sendMessageFromMessageQueue(sendTo, message, sentFrom,
|
||||
ignoreFault);
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message,
|
||||
MessageQueueId_t* receivedFrom) {
|
||||
ReturnValue_t status = this->receiveMessage(message);
|
||||
if(status == HasReturnvaluesIF::RETURN_OK) {
|
||||
*receivedFrom = this->lastPartner;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message) {
|
||||
if(messageQueue.empty()) {
|
||||
return MessageQueueIF::EMPTY;
|
||||
}
|
||||
// not sure this will work..
|
||||
//*message = std::move(messageQueue.front());
|
||||
MutexHelper mutexLock(queueLock, MutexIF::TimeoutType::WAITING, 20);
|
||||
MessageQueueMessage* currentMessage = &messageQueue.front();
|
||||
std::copy(currentMessage->getBuffer(),
|
||||
currentMessage->getBuffer() + messageSize, message->getBuffer());
|
||||
messageQueue.pop();
|
||||
// The last partner is the first uint32_t field in the message
|
||||
this->lastPartner = message->getSender();
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
MessageQueueId_t MessageQueue::getLastPartner() const {
|
||||
return lastPartner;
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::flush(uint32_t* count) {
|
||||
*count = messageQueue.size();
|
||||
// Clears the queue.
|
||||
messageQueue = std::queue<MessageQueueMessage>();
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
MessageQueueId_t MessageQueue::getId() const {
|
||||
return mqId;
|
||||
}
|
||||
|
||||
void MessageQueue::setDefaultDestination(MessageQueueId_t defaultDestination) {
|
||||
defaultDestinationSet = true;
|
||||
this->defaultDestination = defaultDestination;
|
||||
}
|
||||
|
||||
MessageQueueId_t MessageQueue::getDefaultDestination() const {
|
||||
return defaultDestination;
|
||||
}
|
||||
|
||||
bool MessageQueue::isDefaultDestinationSet() const {
|
||||
return defaultDestinationSet;
|
||||
}
|
||||
|
||||
|
||||
// static core function to send messages.
|
||||
ReturnValue_t MessageQueue::sendMessageFromMessageQueue(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, MessageQueueId_t sentFrom,
|
||||
bool ignoreFault) {
|
||||
if(message->getMessageSize() > message->getMaximumMessageSize()) {
|
||||
// Actually, this should never happen or an error will be emitted
|
||||
// in MessageQueueMessage.
|
||||
// But I will still return a failure here.
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
MessageQueue* targetQueue = dynamic_cast<MessageQueue*>(
|
||||
QueueMapManager::instance()->getMessageQueue(sendTo));
|
||||
if(targetQueue == nullptr) {
|
||||
if(not ignoreFault) {
|
||||
InternalErrorReporterIF* internalErrorReporter =
|
||||
objectManager->get<InternalErrorReporterIF>(
|
||||
objects::INTERNAL_ERROR_REPORTER);
|
||||
if (internalErrorReporter != nullptr) {
|
||||
internalErrorReporter->queueMessageNotSent();
|
||||
}
|
||||
}
|
||||
// TODO: Better returnvalue
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
|
||||
if(targetQueue->messageQueue.size() < targetQueue->messageDepth) {
|
||||
MutexHelper mutexLock(targetQueue->queueLock,
|
||||
MutexIF::TimeoutType::WAITING, 20);
|
||||
// not ideal, works for now though.
|
||||
MessageQueueMessage* mqmMessage =
|
||||
dynamic_cast<MessageQueueMessage*>(message);
|
||||
if(message != nullptr) {
|
||||
targetQueue->messageQueue.push(*mqmMessage);
|
||||
}
|
||||
else {
|
||||
sif::error << "MessageQueue::sendMessageFromMessageQueue: Message"
|
||||
"is not MessageQueueMessage!" << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
return MessageQueueIF::FULL;
|
||||
}
|
||||
message->setSender(sentFrom);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::lockQueue(MutexIF::TimeoutType timeoutType,
|
||||
dur_millis_t lockTimeout) {
|
||||
return queueLock->lockMutex(timeoutType, lockTimeout);
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::unlockQueue() {
|
||||
return queueLock->unlockMutex();
|
||||
}
|
231
osal/host/MessageQueue.h
Normal file
231
osal/host/MessageQueue.h
Normal file
@ -0,0 +1,231 @@
|
||||
#ifndef FRAMEWORK_OSAL_HOST_MESSAGEQUEUE_H_
|
||||
#define FRAMEWORK_OSAL_HOST_MESSAGEQUEUE_H_
|
||||
|
||||
#include "../../internalError/InternalErrorReporterIF.h"
|
||||
#include "../../ipc/MessageQueueIF.h"
|
||||
#include "../../ipc/MessageQueueMessage.h"
|
||||
#include "../../ipc/MutexIF.h"
|
||||
#include "../../timemanager/Clock.h"
|
||||
|
||||
#include <queue>
|
||||
#include <memory>
|
||||
|
||||
/**
|
||||
* @brief This class manages sending and receiving of
|
||||
* message queue messages.
|
||||
* @details
|
||||
* Message queues are used to pass asynchronous messages between processes.
|
||||
* They work like post boxes, where all incoming messages are stored in FIFO
|
||||
* order. This class creates a new receiving queue and provides methods to fetch
|
||||
* received messages. Being a child of MessageQueueSender, this class also
|
||||
* provides methods to send a message to a user-defined or a default destination.
|
||||
* In addition it also provides a reply method to answer to the queue it
|
||||
* received its last message from.
|
||||
*
|
||||
* The MessageQueue should be used as "post box" for a single owning object.
|
||||
* So all message queue communication is "n-to-one".
|
||||
* For creating the queue, as well as sending and receiving messages, the class
|
||||
* makes use of the operating system calls provided.
|
||||
*
|
||||
* Please keep in mind that FreeRTOS offers different calls for message queue
|
||||
* operations if called from an ISR.
|
||||
* For now, the system context needs to be switched manually.
|
||||
* @ingroup osal
|
||||
* @ingroup message_queue
|
||||
*/
|
||||
class MessageQueue : public MessageQueueIF {
|
||||
friend class MessageQueueSenderIF;
|
||||
public:
|
||||
/**
|
||||
* @brief The constructor initializes and configures the message queue.
|
||||
* @details
|
||||
* By making use of the according operating system call, a message queue is
|
||||
* created and initialized. The message depth - the maximum number of
|
||||
* messages to be buffered - may be set with the help of a parameter,
|
||||
* whereas the message size is automatically set to the maximum message
|
||||
* queue message size. The operating system sets the message queue id, or
|
||||
* in case of failure, it is set to zero.
|
||||
* @param message_depth
|
||||
* The number of messages to be buffered before passing an error to the
|
||||
* sender. Default is three.
|
||||
* @param max_message_size
|
||||
* With this parameter, the maximum message size can be adjusted.
|
||||
* This should be left default.
|
||||
*/
|
||||
MessageQueue(size_t messageDepth = 3,
|
||||
size_t maxMessageSize = MessageQueueMessage::MAX_MESSAGE_SIZE);
|
||||
|
||||
/** Copying message queues forbidden */
|
||||
MessageQueue(const MessageQueue&) = delete;
|
||||
MessageQueue& operator=(const MessageQueue&) = delete;
|
||||
|
||||
/**
|
||||
* @brief The destructor deletes the formerly created message queue.
|
||||
* @details This is accomplished by using the delete call provided
|
||||
* by the operating system.
|
||||
*/
|
||||
virtual ~MessageQueue();
|
||||
|
||||
/**
|
||||
* @brief This operation sends a message to the given destination.
|
||||
* @details It directly uses the sendMessage call of the MessageQueueSender
|
||||
* parent, but passes its queue id as "sentFrom" parameter.
|
||||
* @param sendTo This parameter specifies the message queue id of the
|
||||
* destination message queue.
|
||||
* @param message A pointer to a previously created message, which is sent.
|
||||
* @param ignoreFault If set to true, the internal software fault counter
|
||||
* is not incremented if queue is full.
|
||||
*/
|
||||
ReturnValue_t sendMessage(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, bool ignoreFault = false) override;
|
||||
/**
|
||||
* @brief This operation sends a message to the default destination.
|
||||
* @details As in the sendMessage method, this function uses the
|
||||
* sendToDefault call of the MessageQueueSender parent class and adds its
|
||||
* queue id as "sentFrom" information.
|
||||
* @param message A pointer to a previously created message, which is sent.
|
||||
*/
|
||||
ReturnValue_t sendToDefault(MessageQueueMessageIF* message) override;
|
||||
/**
|
||||
* @brief This operation sends a message to the last communication partner.
|
||||
* @details This operation simplifies answering an incoming message by using
|
||||
* the stored lastPartner information as destination. If there was no
|
||||
* message received yet (i.e. lastPartner is zero), an error code is returned.
|
||||
* @param message A pointer to a previously created message, which is sent.
|
||||
*/
|
||||
ReturnValue_t reply(MessageQueueMessageIF* message) override;
|
||||
|
||||
/**
|
||||
* @brief With the sendMessage call, a queue message is sent to a
|
||||
* receiving queue.
|
||||
* @details
|
||||
* This method takes the message provided, adds the sentFrom information and
|
||||
* passes it on to the destination provided with an operating system call.
|
||||
* The OS's return value is returned.
|
||||
* @param sendTo This parameter specifies the message queue id to send
|
||||
* the message to.
|
||||
* @param message This is a pointer to a previously created message,
|
||||
* which is sent.
|
||||
* @param sentFrom The sentFrom information can be set to inject the
|
||||
* sender's queue id into the message. This variable is set to zero by
|
||||
* default.
|
||||
* @param ignoreFault If set to true, the internal software fault counter
|
||||
* is not incremented if queue is full.
|
||||
*/
|
||||
virtual ReturnValue_t sendMessageFrom( MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, MessageQueueId_t sentFrom = NO_QUEUE,
|
||||
bool ignoreFault = false) override;
|
||||
|
||||
/**
|
||||
* @brief The sendToDefault method sends a queue message to the default
|
||||
* destination.
|
||||
* @details
|
||||
* In all other aspects, it works identical to the sendMessage method.
|
||||
* @param message This is a pointer to a previously created message,
|
||||
* which is sent.
|
||||
* @param sentFrom The sentFrom information can be set to inject the
|
||||
* sender's queue id into the message. This variable is set to zero by
|
||||
* default.
|
||||
*/
|
||||
virtual ReturnValue_t sendToDefaultFrom( MessageQueueMessageIF* message,
|
||||
MessageQueueId_t sentFrom = NO_QUEUE,
|
||||