Merge remote-tracking branch 'upstream/master' into mueller/newLocalDataPools
This commit is contained in:
commit
1f632b9331
@ -1,10 +1,33 @@
|
||||
#ifndef FRAMEWORK_CONTAINER_FIXEDORDEREDMULTIMAP_H_
|
||||
#define FRAMEWORK_CONTAINER_FIXEDORDEREDMULTIMAP_H_
|
||||
#ifndef FSFW_CONTAINER_FIXEDORDEREDMULTIMAP_H_
|
||||
#define FSFW_CONTAINER_FIXEDORDEREDMULTIMAP_H_
|
||||
|
||||
#include "ArrayList.h"
|
||||
#include <cstring>
|
||||
#include <set>
|
||||
|
||||
/**
|
||||
* @brief An associative container which allows multiple entries of the same key.
|
||||
* @details
|
||||
* Same keys are ordered by KEY_COMPARE function which is std::less<key_t> > by default.
|
||||
*
|
||||
* It uses the ArrayList, so technically this is not a real map, it is an array of pairs
|
||||
* of type key_t, T. It is ordered by key_t as FixedMap but allows same keys. Thus it has a linear
|
||||
* complexity O(n). As long as the number of entries remains low, this
|
||||
* should not be an issue.
|
||||
* The number of insertion and deletion operation should be minimized
|
||||
* as those incur extensive memory move operations (the underlying container
|
||||
* is not node based).
|
||||
*
|
||||
* Its of fixed size so no allocations are performed after the construction.
|
||||
*
|
||||
* The maximum size is given as first parameter of the constructor.
|
||||
*
|
||||
* It provides an iterator to do list iterations.
|
||||
*
|
||||
* The type T must have a copy constructor if it is not trivial copy-able.
|
||||
*
|
||||
* @warning Iterators return a non-const key_t in the pair.
|
||||
* @warning A User is not allowed to change the key, otherwise the map is corrupted.
|
||||
*
|
||||
* \ingroup container
|
||||
*/
|
||||
template<typename key_t, typename T, typename KEY_COMPARE = std::less<key_t>>
|
||||
@ -14,152 +37,170 @@ public:
|
||||
static const ReturnValue_t MAP_FULL = MAKE_RETURN_CODE(0x01);
|
||||
static const ReturnValue_t KEY_DOES_NOT_EXIST = MAKE_RETURN_CODE(0x02);
|
||||
|
||||
private:
|
||||
typedef KEY_COMPARE compare;
|
||||
compare myComp;
|
||||
ArrayList<std::pair<key_t, T>, uint32_t> theMap;
|
||||
uint32_t _size;
|
||||
/***
|
||||
* Constructor which needs a size_t for the maximum allowed size
|
||||
*
|
||||
* Can not be resized during runtime
|
||||
*
|
||||
* Allocates memory at construction
|
||||
* @param maxSize size_t of Maximum allowed size
|
||||
*/
|
||||
FixedOrderedMultimap(size_t maxSize):theMap(maxSize), _size(0){
|
||||
}
|
||||
|
||||
uint32_t findFirstIndex(key_t key, uint32_t startAt = 0) const {
|
||||
if (startAt >= _size) {
|
||||
return startAt + 1;
|
||||
}
|
||||
uint32_t i = startAt;
|
||||
for (i = startAt; i < _size; ++i) {
|
||||
if (theMap[i].first == key) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
uint32_t findNicePlace(key_t key) const {
|
||||
uint32_t i = 0;
|
||||
for (i = 0; i < _size; ++i) {
|
||||
if (myComp(key, theMap[i].first)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
void removeFromPosition(uint32_t position) {
|
||||
if (_size <= position) {
|
||||
return;
|
||||
}
|
||||
memmove(static_cast<void*>(&theMap[position]), static_cast<void*>(&theMap[position + 1]),
|
||||
(_size - position - 1) * sizeof(std::pair<key_t,T>));
|
||||
--_size;
|
||||
}
|
||||
public:
|
||||
FixedOrderedMultimap(uint32_t maxSize) :
|
||||
theMap(maxSize), _size(0) {
|
||||
}
|
||||
/***
|
||||
* Virtual destructor frees Memory by deleting its member
|
||||
*/
|
||||
virtual ~FixedOrderedMultimap() {
|
||||
}
|
||||
|
||||
class Iterator: public ArrayList<std::pair<key_t, T>, uint32_t>::Iterator {
|
||||
/***
|
||||
* Special iterator for FixedOrderedMultimap
|
||||
*/
|
||||
class Iterator: public ArrayList<std::pair<key_t, T>, size_t>::Iterator {
|
||||
public:
|
||||
Iterator() :
|
||||
ArrayList<std::pair<key_t, T>, uint32_t>::Iterator() {
|
||||
ArrayList<std::pair<key_t, T>, size_t>::Iterator() {
|
||||
}
|
||||
|
||||
Iterator(std::pair<key_t, T> *pair) :
|
||||
ArrayList<std::pair<key_t, T>, uint32_t>::Iterator(pair) {
|
||||
ArrayList<std::pair<key_t, T>, size_t>::Iterator(pair) {
|
||||
}
|
||||
};
|
||||
|
||||
/***
|
||||
* Returns an iterator pointing to the first element
|
||||
* @return Iterator pointing to first element
|
||||
*/
|
||||
Iterator begin() const {
|
||||
return Iterator(&theMap[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator pointing to one element past the end
|
||||
* @return Iterator pointing to one element past the end
|
||||
*/
|
||||
Iterator end() const {
|
||||
return Iterator(&theMap[_size]);
|
||||
}
|
||||
|
||||
uint32_t size() const {
|
||||
/***
|
||||
* Returns the current size of the map (not maximum size!)
|
||||
* @return Current size
|
||||
*/
|
||||
size_t size() const{
|
||||
return _size;
|
||||
}
|
||||
|
||||
ReturnValue_t insert(key_t key, T value, Iterator *storedValue = nullptr) {
|
||||
if (_size == theMap.maxSize()) {
|
||||
return MAP_FULL;
|
||||
}
|
||||
uint32_t position = findNicePlace(key);
|
||||
memmove(static_cast<void*>(&theMap[position + 1]),static_cast<void*>(&theMap[position]),
|
||||
(_size - position) * sizeof(std::pair<key_t,T>));
|
||||
theMap[position].first = key;
|
||||
theMap[position].second = value;
|
||||
++_size;
|
||||
if (storedValue != nullptr) {
|
||||
*storedValue = Iterator(&theMap[position]);
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
/**
|
||||
* Clears the map, does not deallocate any memory
|
||||
*/
|
||||
void clear(){
|
||||
_size = 0;
|
||||
}
|
||||
|
||||
ReturnValue_t insert(std::pair<key_t, T> pair) {
|
||||
return insert(pair.first, pair.second);
|
||||
/**
|
||||
* Returns the maximum size of the map
|
||||
* @return Maximum size of the map
|
||||
*/
|
||||
size_t maxSize() const{
|
||||
return theMap.maxSize();
|
||||
}
|
||||
|
||||
ReturnValue_t exists(key_t key) const {
|
||||
ReturnValue_t result = KEY_DOES_NOT_EXIST;
|
||||
if (findFirstIndex(key) < _size) {
|
||||
result = HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/***
|
||||
* Used to insert a key and value separately.
|
||||
*
|
||||
* @param[in] key Key of the new element
|
||||
* @param[in] value Value of the new element
|
||||
* @param[in/out] (optional) storedValue On success this points to the new value, otherwise a nullptr
|
||||
* @return RETURN_OK if insert was successful, MAP_FULL if no space is available
|
||||
*/
|
||||
ReturnValue_t insert(key_t key, T value, Iterator *storedValue = nullptr);
|
||||
|
||||
ReturnValue_t erase(Iterator *iter) {
|
||||
uint32_t i;
|
||||
if ((i = findFirstIndex((*iter).value->first)) >= _size) {
|
||||
return KEY_DOES_NOT_EXIST;
|
||||
}
|
||||
removeFromPosition(i);
|
||||
if (*iter != begin()) {
|
||||
(*iter)--;
|
||||
} else {
|
||||
*iter = begin();
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
/***
|
||||
* Used to insert new pair instead of single values
|
||||
*
|
||||
* @param pair Pair to be inserted
|
||||
* @return RETURN_OK if insert was successful, MAP_FULL if no space is available
|
||||
*/
|
||||
ReturnValue_t insert(std::pair<key_t, T> pair);
|
||||
|
||||
ReturnValue_t erase(key_t key) {
|
||||
uint32_t i;
|
||||
if ((i = findFirstIndex(key)) >= _size) {
|
||||
return KEY_DOES_NOT_EXIST;
|
||||
}
|
||||
do {
|
||||
removeFromPosition(i);
|
||||
i = findFirstIndex(key, i);
|
||||
} while (i < _size);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
/***
|
||||
* Can be used to check if a certain key is in the map
|
||||
* @param key Key to be checked
|
||||
* @return RETURN_OK if the key exists KEY_DOES_NOT_EXIST otherwise
|
||||
*/
|
||||
ReturnValue_t exists(key_t key) const;
|
||||
|
||||
Iterator find(key_t key) const {
|
||||
/***
|
||||
* Used to delete the element in the iterator
|
||||
*
|
||||
* The iterator will point to the element before or begin(),
|
||||
* but never to one element in front of the map.
|
||||
*
|
||||
* @warning The iterator needs to be valid and dereferenceable
|
||||
* @param[in/out] iter Pointer to iterator to the element that needs to be ereased
|
||||
* @return RETURN_OK if erased, KEY_DOES_NOT_EXIST if the there is no element like this
|
||||
*/
|
||||
ReturnValue_t erase(Iterator *iter);
|
||||
|
||||
/***
|
||||
* Used to erase by key
|
||||
* @param key Key to be erased
|
||||
* @return RETURN_OK if erased, KEY_DOES_NOT_EXIST if the there is no element like this
|
||||
*/
|
||||
ReturnValue_t erase(key_t key);
|
||||
|
||||
/***
|
||||
* Find returns the first appearance of the key
|
||||
*
|
||||
* If the key does not exist, it points to end()
|
||||
*
|
||||
* @param key Key to search for
|
||||
* @return Iterator pointing to the first entry of key
|
||||
*/
|
||||
Iterator find(key_t key) const{
|
||||
ReturnValue_t result = exists(key);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return end();
|
||||
}
|
||||
return Iterator(&theMap[findFirstIndex(key)]);
|
||||
};
|
||||
|
||||
/***
|
||||
* Finds first entry of the given key and returns a
|
||||
* pointer to the value
|
||||
*
|
||||
* @param key Key to search for
|
||||
* @param value Found value
|
||||
* @return RETURN_OK if it points to the value,
|
||||
* KEY_DOES_NOT_EXIST if the key is not in the map
|
||||
*/
|
||||
ReturnValue_t find(key_t key, T **value) const;
|
||||
|
||||
friend bool operator==(const typename FixedOrderedMultimap::Iterator& lhs,
|
||||
const typename FixedOrderedMultimap::Iterator& rhs) {
|
||||
return (lhs.value == rhs.value);
|
||||
}
|
||||
|
||||
ReturnValue_t find(key_t key, T **value) const {
|
||||
ReturnValue_t result = exists(key);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
*value = &theMap[findFirstIndex(key)].second;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
friend bool operator!=(const typename FixedOrderedMultimap::Iterator& lhs,
|
||||
const typename FixedOrderedMultimap::Iterator& rhs) {
|
||||
return not (lhs.value == rhs.value);
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_size = 0;
|
||||
}
|
||||
private:
|
||||
typedef KEY_COMPARE compare;
|
||||
compare myComp;
|
||||
ArrayList<std::pair<key_t, T>, size_t> theMap;
|
||||
size_t _size;
|
||||
|
||||
uint32_t maxSize() const {
|
||||
return theMap.maxSize();
|
||||
}
|
||||
size_t findFirstIndex(key_t key, size_t startAt = 0) const;
|
||||
|
||||
size_t findNicePlace(key_t key) const;
|
||||
|
||||
void removeFromPosition(size_t position);
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_CONTAINER_FIXEDORDEREDMULTIMAP_H_ */
|
||||
#include "FixedOrderedMultimap.tpp"
|
||||
|
||||
#endif /* FSFW_CONTAINER_FIXEDORDEREDMULTIMAP_H_ */
|
||||
|
109
container/FixedOrderedMultimap.tpp
Normal file
109
container/FixedOrderedMultimap.tpp
Normal file
@ -0,0 +1,109 @@
|
||||
#ifndef FRAMEWORK_CONTAINER_FIXEDORDEREDMULTIMAP_TPP_
|
||||
#define FRAMEWORK_CONTAINER_FIXEDORDEREDMULTIMAP_TPP_
|
||||
|
||||
|
||||
template<typename key_t, typename T, typename KEY_COMPARE>
|
||||
inline ReturnValue_t FixedOrderedMultimap<key_t, T, KEY_COMPARE>::insert(key_t key, T value, Iterator *storedValue) {
|
||||
if (_size == theMap.maxSize()) {
|
||||
return MAP_FULL;
|
||||
}
|
||||
size_t position = findNicePlace(key);
|
||||
memmove(static_cast<void*>(&theMap[position + 1]),static_cast<void*>(&theMap[position]),
|
||||
(_size - position) * sizeof(std::pair<key_t,T>));
|
||||
theMap[position].first = key;
|
||||
theMap[position].second = value;
|
||||
++_size;
|
||||
if (storedValue != nullptr) {
|
||||
*storedValue = Iterator(&theMap[position]);
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
template<typename key_t, typename T, typename KEY_COMPARE>
|
||||
inline ReturnValue_t FixedOrderedMultimap<key_t, T, KEY_COMPARE>::insert(std::pair<key_t, T> pair) {
|
||||
return insert(pair.first, pair.second);
|
||||
}
|
||||
|
||||
template<typename key_t, typename T, typename KEY_COMPARE>
|
||||
inline ReturnValue_t FixedOrderedMultimap<key_t, T, KEY_COMPARE>::exists(key_t key) const {
|
||||
ReturnValue_t result = KEY_DOES_NOT_EXIST;
|
||||
if (findFirstIndex(key) < _size) {
|
||||
result = HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename key_t, typename T, typename KEY_COMPARE>
|
||||
inline ReturnValue_t FixedOrderedMultimap<key_t, T, KEY_COMPARE>::erase(Iterator *iter) {
|
||||
size_t i;
|
||||
if ((i = findFirstIndex((*iter).value->first)) >= _size) {
|
||||
return KEY_DOES_NOT_EXIST;
|
||||
}
|
||||
removeFromPosition(i);
|
||||
if (*iter != begin()) {
|
||||
(*iter)--;
|
||||
} else {
|
||||
*iter = begin();
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
template<typename key_t, typename T, typename KEY_COMPARE>
|
||||
inline ReturnValue_t FixedOrderedMultimap<key_t, T, KEY_COMPARE>::erase(key_t key) {
|
||||
size_t i;
|
||||
if ((i = findFirstIndex(key)) >= _size) {
|
||||
return KEY_DOES_NOT_EXIST;
|
||||
}
|
||||
do {
|
||||
removeFromPosition(i);
|
||||
i = findFirstIndex(key, i);
|
||||
} while (i < _size);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
template<typename key_t, typename T, typename KEY_COMPARE>
|
||||
inline ReturnValue_t FixedOrderedMultimap<key_t, T, KEY_COMPARE>::find(key_t key, T **value) const {
|
||||
ReturnValue_t result = exists(key);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
*value = &theMap[findFirstIndex(key)].second;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
template<typename key_t, typename T, typename KEY_COMPARE>
|
||||
inline size_t FixedOrderedMultimap<key_t, T, KEY_COMPARE>::findFirstIndex(key_t key, size_t startAt) const {
|
||||
if (startAt >= _size) {
|
||||
return startAt + 1;
|
||||
}
|
||||
size_t i = startAt;
|
||||
for (i = startAt; i < _size; ++i) {
|
||||
if (theMap[i].first == key) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
template<typename key_t, typename T, typename KEY_COMPARE>
|
||||
inline size_t FixedOrderedMultimap<key_t, T, KEY_COMPARE>::findNicePlace(key_t key) const {
|
||||
size_t i = 0;
|
||||
for (i = 0; i < _size; ++i) {
|
||||
if (myComp(key, theMap[i].first)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
template<typename key_t, typename T, typename KEY_COMPARE>
|
||||
inline void FixedOrderedMultimap<key_t, T, KEY_COMPARE>::removeFromPosition(size_t position) {
|
||||
if (_size <= position) {
|
||||
return;
|
||||
}
|
||||
memmove(static_cast<void*>(&theMap[position]), static_cast<void*>(&theMap[position + 1]),
|
||||
(_size - position - 1) * sizeof(std::pair<key_t,T>));
|
||||
--_size;
|
||||
}
|
||||
|
||||
|
||||
#endif /* FRAMEWORK_CONTAINER_FIXEDORDEREDMULTIMAP_TPP_ */
|
@ -9,6 +9,7 @@ SharedRingBuffer::SharedRingBuffer(object_id_t objectId, const size_t size,
|
||||
mutex = MutexFactory::instance()->createMutex();
|
||||
}
|
||||
|
||||
|
||||
SharedRingBuffer::SharedRingBuffer(object_id_t objectId, uint8_t *buffer,
|
||||
const size_t size, bool overwriteOld, size_t maxExcessBytes):
|
||||
SystemObject(objectId), SimpleRingBuffer(buffer, size, overwriteOld,
|
||||
@ -16,6 +17,11 @@ SharedRingBuffer::SharedRingBuffer(object_id_t objectId, uint8_t *buffer,
|
||||
mutex = MutexFactory::instance()->createMutex();
|
||||
}
|
||||
|
||||
|
||||
void SharedRingBuffer::setToUseReceiveSizeFIFO(size_t fifoDepth) {
|
||||
this->fifoDepth = fifoDepth;
|
||||
}
|
||||
|
||||
ReturnValue_t SharedRingBuffer::lockRingBufferMutex(
|
||||
MutexIF::TimeoutType timeoutType, dur_millis_t timeout) {
|
||||
return mutex->lockMutex(timeoutType, timeout);
|
||||
@ -25,6 +31,25 @@ ReturnValue_t SharedRingBuffer::unlockRingBufferMutex() {
|
||||
return mutex->unlockMutex();
|
||||
}
|
||||
|
||||
|
||||
|
||||
MutexIF* SharedRingBuffer::getMutexHandle() const {
|
||||
return mutex;
|
||||
}
|
||||
|
||||
ReturnValue_t SharedRingBuffer::initialize() {
|
||||
if(fifoDepth > 0) {
|
||||
receiveSizesFIFO = new DynamicFIFO<size_t>(fifoDepth);
|
||||
}
|
||||
return SystemObject::initialize();
|
||||
}
|
||||
|
||||
DynamicFIFO<size_t>* SharedRingBuffer::getReceiveSizesFIFO() {
|
||||
if(receiveSizesFIFO == nullptr) {
|
||||
// Configuration error.
|
||||
sif::warning << "SharedRingBuffer::getReceiveSizesFIFO: Ring buffer"
|
||||
<< " was not configured to have sizes FIFO, returning nullptr!"
|
||||
<< std::endl;
|
||||
}
|
||||
return receiveSizesFIFO;
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define FSFW_CONTAINER_SHAREDRINGBUFFER_H_
|
||||
|
||||
#include "SimpleRingBuffer.h"
|
||||
#include "DynamicFIFO.h"
|
||||
#include "../ipc/MutexIF.h"
|
||||
#include "../objectmanager/SystemObject.h"
|
||||
#include "../timemanager/Clock.h"
|
||||
@ -26,6 +27,16 @@ public:
|
||||
SharedRingBuffer(object_id_t objectId, const size_t size,
|
||||
bool overwriteOld, size_t maxExcessBytes);
|
||||
|
||||
/**
|
||||
* @brief This function can be used to add an optional FIFO to the class
|
||||
* @details
|
||||
* This FIFO will be allocated in the initialize function (and will
|
||||
* have a fixed maximum size after that). It can be used to store
|
||||
* values like packet sizes, for example for a shared ring buffer
|
||||
* used by producer/consumer tasks.
|
||||
*/
|
||||
void setToUseReceiveSizeFIFO(size_t fifoDepth);
|
||||
|
||||
/**
|
||||
* This constructor takes an external buffer with the specified size.
|
||||
* @param buffer
|
||||
@ -59,8 +70,21 @@ public:
|
||||
* @return
|
||||
*/
|
||||
MutexIF* getMutexHandle() const;
|
||||
|
||||
ReturnValue_t initialize() override;
|
||||
|
||||
/**
|
||||
* If the shared ring buffer was configured to have a sizes FIFO, a handle
|
||||
* to that FIFO can be retrieved with this function.
|
||||
* Do not forget to protect access with a lock if required!
|
||||
* @return
|
||||
*/
|
||||
DynamicFIFO<size_t>* getReceiveSizesFIFO();
|
||||
private:
|
||||
MutexIF* mutex = nullptr;
|
||||
|
||||
size_t fifoDepth = 0;
|
||||
DynamicFIFO<size_t>* receiveSizesFIFO = nullptr;
|
||||
};
|
||||
|
||||
|
||||
|
@ -1251,7 +1251,7 @@ void DeviceHandlerBase::buildInternalCommand(void) {
|
||||
if (result == BUSY) {
|
||||
//so we can track misconfigurations
|
||||
sif::debug << std::hex << getObjectId()
|
||||
<< ": DHB::buildInternalCommand: Busy" << std::endl;
|
||||
<< ": DHB::buildInternalCommand: Busy" << std::dec << std::endl;
|
||||
result = NOTHING_TO_SEND; //no need to report this
|
||||
}
|
||||
}
|
||||
|
@ -1,61 +1,77 @@
|
||||
#ifndef FSFW_IPC_MESSAGEQUEUEIF_H_
|
||||
#define FSFW_IPC_MESSAGEQUEUEIF_H_
|
||||
|
||||
// COULDDO: We could support blocking calls
|
||||
|
||||
#include "messageQueueDefinitions.h"
|
||||
#include "MessageQueueMessage.h"
|
||||
#include "MessageQueueMessageIF.h"
|
||||
#include "../returnvalues/HasReturnvaluesIF.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
// COULDDO: We could support blocking calls
|
||||
// semaphores are being implemented, which makes this idea even more iteresting.
|
||||
|
||||
/**
|
||||
* @defgroup message_queue Message Queue
|
||||
* @brief Message Queue related software components
|
||||
*/
|
||||
class MessageQueueIF {
|
||||
public:
|
||||
static const MessageQueueId_t NO_QUEUE = 0;
|
||||
|
||||
static const uint8_t INTERFACE_ID = CLASS_ID::MESSAGE_QUEUE_IF;
|
||||
/**
|
||||
* No new messages on the queue
|
||||
*/
|
||||
//! No new messages on the queue
|
||||
static const ReturnValue_t EMPTY = MAKE_RETURN_CODE(1);
|
||||
/**
|
||||
* No space left for more messages
|
||||
*/
|
||||
//! No space left for more messages
|
||||
static const ReturnValue_t FULL = MAKE_RETURN_CODE(2);
|
||||
/**
|
||||
* Returned if a reply method was called without partner
|
||||
*/
|
||||
//! Returned if a reply method was called without partner
|
||||
static const ReturnValue_t NO_REPLY_PARTNER = MAKE_RETURN_CODE(3);
|
||||
//! Returned if the target destination is invalid.
|
||||
static constexpr ReturnValue_t DESTINVATION_INVALID = MAKE_RETURN_CODE(4);
|
||||
|
||||
virtual ~MessageQueueIF() {}
|
||||
/**
|
||||
* @brief This operation sends a message to the last communication partner.
|
||||
* @details This operation simplifies answering an incoming message by using the stored
|
||||
* lastParnter 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.
|
||||
* \return RETURN_OK if ok
|
||||
* \return NO_REPLY_PARTNER Should return NO_REPLY_PARTNER if partner was found
|
||||
* @details
|
||||
* This operation simplifies answering an incoming message by using the
|
||||
* stored lastParnter 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.
|
||||
* @return
|
||||
* -@c RETURN_OK if ok
|
||||
* -@c NO_REPLY_PARTNER Should return NO_REPLY_PARTNER if partner was found.
|
||||
*/
|
||||
virtual ReturnValue_t reply( MessageQueueMessage* message ) = 0;
|
||||
virtual ReturnValue_t reply(MessageQueueMessageIF* message) = 0;
|
||||
|
||||
/**
|
||||
* @brief This function reads available messages from the message queue and returns the sender.
|
||||
* @details It works identically to the other receiveMessage call, but in addition returns the
|
||||
* sender's queue id.
|
||||
* @param message A pointer to a message in which the received data is stored.
|
||||
* @param receivedFrom A pointer to a queue id in which the sender's id is stored.
|
||||
* @brief This function reads available messages from the message queue
|
||||
* and returns the sender.
|
||||
* @details
|
||||
* It works identically to the other receiveMessage call, but in addition
|
||||
* returns the sender's queue id.
|
||||
* @param message
|
||||
* A pointer to a message in which the received data is stored.
|
||||
* @param receivedFrom
|
||||
* A pointer to a queue id in which the sender's id is stored.
|
||||
*/
|
||||
virtual ReturnValue_t receiveMessage(MessageQueueMessage* message,
|
||||
virtual ReturnValue_t receiveMessage(MessageQueueMessageIF* message,
|
||||
MessageQueueId_t *receivedFrom) = 0;
|
||||
|
||||
/**
|
||||
* @brief This function reads available messages from the message queue.
|
||||
* @details If data is available it is stored in the passed message pointer. The message's
|
||||
* original content is overwritten and the sendFrom information is stored in the
|
||||
* lastPartner attribute. Else, the lastPartner information remains untouched, the
|
||||
* message's content is cleared and the function returns immediately.
|
||||
* @param message A pointer to a message in which the received data is stored.
|
||||
* @details
|
||||
* If data is available it is stored in the passed message pointer.
|
||||
* The message's original content is overwritten and the sendFrom
|
||||
* information is stored in theblastPartner attribute. Else, the lastPartner
|
||||
* information remains untouched, the message's content is cleared and the
|
||||
* function returns immediately.
|
||||
* @param message
|
||||
* A pointer to a message in which the received data is stored.
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c MessageQueueIF::EMPTY if queue is empty
|
||||
*/
|
||||
virtual ReturnValue_t receiveMessage(MessageQueueMessage* message) = 0;
|
||||
virtual ReturnValue_t receiveMessage(MessageQueueMessageIF* message) = 0;
|
||||
/**
|
||||
* Deletes all pending messages in the queue.
|
||||
* @param count The number of flushed messages.
|
||||
@ -63,57 +79,89 @@ public:
|
||||
*/
|
||||
virtual ReturnValue_t flush(uint32_t* count) = 0;
|
||||
/**
|
||||
* @brief This method returns the message queue id of the last communication partner.
|
||||
* @brief This method returns the message queue
|
||||
* id of the last communication partner.
|
||||
*/
|
||||
virtual MessageQueueId_t getLastPartner() const = 0;
|
||||
/**
|
||||
* @brief This method returns the message queue id of this class's message queue.
|
||||
* @brief This method returns the message queue
|
||||
* id of this class's message queue.
|
||||
*/
|
||||
virtual MessageQueueId_t getId() const = 0;
|
||||
|
||||
/**
|
||||
* \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 (if implemented).
|
||||
* @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 returnvalue 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 (if implemented).
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c MessageQueueIF::FULL if queue is full
|
||||
*/
|
||||
virtual ReturnValue_t sendMessageFrom( MessageQueueId_t sendTo, MessageQueueMessage* message, MessageQueueId_t sentFrom, bool ignoreFault = false ) = 0;
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
virtual ReturnValue_t sendMessage( MessageQueueId_t sendTo, MessageQueueMessage* message, bool ignoreFault = false ) = 0;
|
||||
virtual ReturnValue_t sendMessageFrom( MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, MessageQueueId_t sentFrom,
|
||||
bool ignoreFault = false ) = 0;
|
||||
|
||||
/**
|
||||
* \brief The sendToDefaultFrom 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.
|
||||
* @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.
|
||||
*/
|
||||
virtual ReturnValue_t sendToDefaultFrom( MessageQueueMessage* message, MessageQueueId_t sentFrom, bool ignoreFault = false ) = 0;
|
||||
virtual ReturnValue_t sendMessage( MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, bool ignoreFault = false ) = 0;
|
||||
|
||||
/**
|
||||
* @brief The sendToDefaultFrom 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.
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c MessageQueueIF::FULL if queue is full
|
||||
*/
|
||||
virtual ReturnValue_t sendToDefaultFrom( MessageQueueMessageIF* message,
|
||||
MessageQueueId_t sentFrom, bool ignoreFault = false ) = 0;
|
||||
/**
|
||||
* @brief This operation sends a message to the default destination.
|
||||
* @details As in the sendMessage method, this function uses the sendToDefault call of the
|
||||
* Implementation class and adds its queue id as "sentFrom" information.
|
||||
* @details
|
||||
* As in the sendMessage method, this function uses the sendToDefault
|
||||
* call of the Implementation class and adds its queue id as
|
||||
* "sentFrom" information.
|
||||
* @param message A pointer to a previously created message, which is sent.
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c MessageQueueIF::FULL if queue is full
|
||||
*/
|
||||
virtual ReturnValue_t sendToDefault( MessageQueueMessage* message ) = 0;
|
||||
virtual ReturnValue_t sendToDefault( MessageQueueMessageIF* message ) = 0;
|
||||
/**
|
||||
* \brief This method is a simple setter for the default destination.
|
||||
* @brief This method is a simple setter for the default destination.
|
||||
*/
|
||||
virtual void setDefaultDestination(MessageQueueId_t defaultDestination) = 0;
|
||||
/**
|
||||
* \brief This method is a simple getter for the default destination.
|
||||
* @brief This method is a simple getter for the default destination.
|
||||
*/
|
||||
virtual MessageQueueId_t getDefaultDestination() const = 0;
|
||||
|
||||
@ -122,4 +170,4 @@ public:
|
||||
|
||||
|
||||
|
||||
#endif /* FRAMEWORK_IPC_MESSAGEQUEUEIF_H_ */
|
||||
#endif /* FSFW_IPC_MESSAGEQUEUEIF_H_ */
|
||||
|
@ -1,8 +1,8 @@
|
||||
#ifndef FSFW_IPC_MESSAGEQUEUESENDERIF_H_
|
||||
#define FSFW_IPC_MESSAGEQUEUESENDERIF_H_
|
||||
|
||||
#include "../ipc/MessageQueueIF.h"
|
||||
#include "../ipc/MessageQueueMessageIF.h"
|
||||
#include "MessageQueueIF.h"
|
||||
#include "MessageQueueMessageIF.h"
|
||||
#include "../objectmanager/ObjectManagerIF.h"
|
||||
|
||||
class MessageQueueSenderIF {
|
||||
@ -15,7 +15,7 @@ public:
|
||||
* Not sure whether this is actually a good idea.
|
||||
*/
|
||||
static ReturnValue_t sendMessage(MessageQueueId_t sendTo,
|
||||
MessageQueueMessage* message,
|
||||
MessageQueueMessageIF* message,
|
||||
MessageQueueId_t sentFrom = MessageQueueIF::NO_QUEUE,
|
||||
bool ignoreFault = false);
|
||||
private:
|
||||
|
@ -1,8 +1,10 @@
|
||||
#ifndef FRAMEWORK_IPC_QUEUEFACTORY_H_
|
||||
#define FRAMEWORK_IPC_QUEUEFACTORY_H_
|
||||
#ifndef FSFW_IPC_QUEUEFACTORY_H_
|
||||
#define FSFW_IPC_QUEUEFACTORY_H_
|
||||
|
||||
#include "MessageQueueIF.h"
|
||||
#include <stdint.h>
|
||||
#include "MessageQueueMessage.h"
|
||||
#include <cstdint>
|
||||
|
||||
/**
|
||||
* Creates message queues.
|
||||
* This class is a "singleton" interface, i.e. it provides an
|
||||
@ -30,4 +32,4 @@ private:
|
||||
static QueueFactory* factoryInstance;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_IPC_QUEUEFACTORY_H_ */
|
||||
#endif /* FSFW_IPC_QUEUEFACTORY_H_ */
|
||||
|
@ -1,42 +1,76 @@
|
||||
#include "MessageQueue.h"
|
||||
|
||||
#include "../../objectmanager/ObjectManagerIF.h"
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
// TODO I guess we should have a way of checking if we are in an ISR and then use the "fromISR" versions of all calls
|
||||
|
||||
MessageQueue::MessageQueue(size_t message_depth, size_t max_message_size) :
|
||||
defaultDestination(0),lastPartner(0) {
|
||||
handle = xQueueCreate(message_depth, max_message_size);
|
||||
if (handle == NULL) {
|
||||
sif::error << "MessageQueue creation failed" << std::endl;
|
||||
// TODO I guess we should have a way of checking if we are in an ISR and then
|
||||
// use the "fromISR" versions of all calls
|
||||
// As a first step towards this, introduces system context variable which needs
|
||||
// to be switched manually
|
||||
// Haven't found function to find system context.
|
||||
MessageQueue::MessageQueue(size_t messageDepth, size_t maxMessageSize):
|
||||
maxMessageSize(maxMessageSize) {
|
||||
handle = xQueueCreate(messageDepth, maxMessageSize);
|
||||
if (handle == nullptr) {
|
||||
sif::error << "MessageQueue::MessageQueue Creation failed" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
MessageQueue::~MessageQueue() {
|
||||
if (handle != NULL) {
|
||||
if (handle != nullptr) {
|
||||
vQueueDelete(handle);
|
||||
}
|
||||
}
|
||||
|
||||
void MessageQueue::switchSystemContext(CallContext callContext) {
|
||||
this->callContext = callContext;
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::sendMessage(MessageQueueId_t sendTo,
|
||||
MessageQueueMessage* message, bool ignoreFault) {
|
||||
MessageQueueMessageIF* message, bool ignoreFault) {
|
||||
return sendMessageFrom(sendTo, message, this->getId(), ignoreFault);
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::sendToDefault(MessageQueueMessage* message) {
|
||||
ReturnValue_t MessageQueue::sendToDefault(MessageQueueMessageIF* message) {
|
||||
return sendToDefaultFrom(message, this->getId());
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::reply(MessageQueueMessage* message) {
|
||||
if (this->lastPartner != 0) {
|
||||
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 != MessageQueueIF::NO_QUEUE) {
|
||||
return sendMessageFrom(this->lastPartner, message, this->getId());
|
||||
} else {
|
||||
return NO_REPLY_PARTNER;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message,
|
||||
ReturnValue_t MessageQueue::sendMessageFrom(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, MessageQueueId_t sentFrom,
|
||||
bool ignoreFault) {
|
||||
return sendMessageFromMessageQueue(sendTo, message, sentFrom, ignoreFault,
|
||||
callContext);
|
||||
}
|
||||
|
||||
|
||||
ReturnValue_t MessageQueue::handleSendResult(BaseType_t result, bool ignoreFault) {
|
||||
if (result != pdPASS) {
|
||||
if (not ignoreFault) {
|
||||
InternalErrorReporterIF* internalErrorReporter = objectManager->
|
||||
get<InternalErrorReporterIF>(
|
||||
objects::INTERNAL_ERROR_REPORTER);
|
||||
if (internalErrorReporter != nullptr) {
|
||||
internalErrorReporter->queueMessageNotSent();
|
||||
}
|
||||
}
|
||||
return MessageQueueIF::FULL;
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message,
|
||||
MessageQueueId_t* receivedFrom) {
|
||||
ReturnValue_t status = this->receiveMessage(message);
|
||||
if(status == HasReturnvaluesIF::RETURN_OK) {
|
||||
@ -45,8 +79,9 @@ ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message,
|
||||
return status;
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message) {
|
||||
BaseType_t result = xQueueReceive(handle,reinterpret_cast<void*>(message->getBuffer()), 0);
|
||||
ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message) {
|
||||
BaseType_t result = xQueueReceive(handle,reinterpret_cast<void*>(
|
||||
message->getBuffer()), 0);
|
||||
if (result == pdPASS){
|
||||
this->lastPartner = message->getSender();
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
@ -67,51 +102,55 @@ ReturnValue_t MessageQueue::flush(uint32_t* count) {
|
||||
}
|
||||
|
||||
MessageQueueId_t MessageQueue::getId() const {
|
||||
return (MessageQueueId_t) handle;
|
||||
return reinterpret_cast<MessageQueueId_t>(handle);
|
||||
}
|
||||
|
||||
void MessageQueue::setDefaultDestination(MessageQueueId_t defaultDestination) {
|
||||
defaultDestinationSet = true;
|
||||
this->defaultDestination = defaultDestination;
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::sendMessageFrom(MessageQueueId_t sendTo,
|
||||
MessageQueueMessage* message, MessageQueueId_t sentFrom,
|
||||
bool ignoreFault) {
|
||||
return sendMessageFromMessageQueue(sendTo,message,sentFrom,ignoreFault);
|
||||
}
|
||||
|
||||
ReturnValue_t MessageQueue::sendToDefaultFrom(MessageQueueMessage* message,
|
||||
MessageQueueId_t sentFrom, bool ignoreFault) {
|
||||
return sendMessageFrom(defaultDestination,message,sentFrom,ignoreFault);
|
||||
}
|
||||
|
||||
MessageQueueId_t MessageQueue::getDefaultDestination() const {
|
||||
return defaultDestination;
|
||||
}
|
||||
|
||||
bool MessageQueue::isDefaultDestinationSet() const {
|
||||
return 0;
|
||||
return defaultDestinationSet;
|
||||
}
|
||||
|
||||
|
||||
// static core function to send messages.
|
||||
ReturnValue_t MessageQueue::sendMessageFromMessageQueue(MessageQueueId_t sendTo,
|
||||
MessageQueueMessage *message, MessageQueueId_t sentFrom,
|
||||
bool ignoreFault) {
|
||||
message->setSender(sentFrom);
|
||||
MessageQueueMessageIF* message, MessageQueueId_t sentFrom,
|
||||
bool ignoreFault, CallContext callContext) {
|
||||
BaseType_t result = pdFALSE;
|
||||
QueueHandle_t destination = nullptr;
|
||||
|
||||
BaseType_t result = xQueueSendToBack(reinterpret_cast<QueueHandle_t>(sendTo),
|
||||
reinterpret_cast<const void*>(message->getBuffer()), 0);
|
||||
if (result != pdPASS) {
|
||||
if (!ignoreFault) {
|
||||
InternalErrorReporterIF* internalErrorReporter =
|
||||
objectManager->get<InternalErrorReporterIF>(
|
||||
objects::INTERNAL_ERROR_REPORTER);
|
||||
if (internalErrorReporter != NULL) {
|
||||
internalErrorReporter->queueMessageNotSent();
|
||||
}
|
||||
}
|
||||
return MessageQueueIF::FULL;
|
||||
if(sendTo == MessageQueueIF::NO_QUEUE or sendTo == 0x00) {
|
||||
return MessageQueueIF::DESTINVATION_INVALID;
|
||||
}
|
||||
else {
|
||||
destination = reinterpret_cast<QueueHandle_t>(sendTo);
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
|
||||
message->setSender(sentFrom);
|
||||
|
||||
|
||||
if(callContext == CallContext::TASK) {
|
||||
result = xQueueSendToBack(destination,
|
||||
static_cast<const void*>(message->getBuffer()), 0);
|
||||
}
|
||||
else {
|
||||
/* If the call context is from an interrupt,
|
||||
* request a context switch if a higher priority task
|
||||
* was blocked by the interrupt. */
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
result = xQueueSendFromISR(reinterpret_cast<QueueHandle_t>(sendTo),
|
||||
static_cast<const void*>(message->getBuffer()),
|
||||
&xHigherPriorityTaskWoken);
|
||||
if(xHigherPriorityTaskWoken == pdTRUE) {
|
||||
TaskManagement::requestContextSwitch(callContext);
|
||||
}
|
||||
}
|
||||
return handleSendResult(result, ignoreFault);
|
||||
}
|
||||
|
||||
|
@ -1,159 +1,150 @@
|
||||
#ifndef MESSAGEQUEUE_H_
|
||||
#define MESSAGEQUEUE_H_
|
||||
#ifndef FSFW_OSAL_FREERTOS_MESSAGEQUEUE_H_
|
||||
#define FSFW_OSAL_FREERTOS_MESSAGEQUEUE_H_
|
||||
|
||||
#include "../../internalError/InternalErrorReporterIF.h"
|
||||
#include "../../ipc/MessageQueueIF.h"
|
||||
#include "../../ipc/MessageQueueMessage.h"
|
||||
#include "../../ipc/MessageQueueMessageIF.h"
|
||||
#include "../../osal/FreeRTOS/TaskManagement.h"
|
||||
|
||||
#include <FreeRTOS.h>
|
||||
#include <queue.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/queue.h>
|
||||
#include <fsfw/ipc/MessageQueueMessage.h>
|
||||
|
||||
//TODO this class assumes that MessageQueueId_t is the same size as void* (the FreeRTOS handle type), compiler will catch this but it might be nice to have something checking or even an always working solution
|
||||
// TODO: this class assumes that MessageQueueId_t is the same size as void*
|
||||
// (the FreeRTOS handle type), compiler will catch this but it might be nice
|
||||
// to have something checking or even an always working solution
|
||||
// https://scaryreasoner.wordpress.com/2009/02/28/checking-sizeof-at-compile-time/
|
||||
|
||||
/**
|
||||
* @brief This class manages sending and receiving of message queue messages.
|
||||
* @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.
|
||||
*
|
||||
* @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.
|
||||
* \ingroup message_queue
|
||||
* 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 i 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.
|
||||
* @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 message_depth = 3, size_t max_message_size = MessageQueueMessage::MAX_MESSAGE_SIZE );
|
||||
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.
|
||||
* @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.
|
||||
* This function is used to switch the call context. This has to be called
|
||||
* if a message is sent or received from an ISR!
|
||||
* @param callContext
|
||||
*/
|
||||
void switchSystemContext(CallContext callContext);
|
||||
|
||||
/** MessageQueueIF implementation */
|
||||
ReturnValue_t sendMessage(MessageQueueId_t sendTo,
|
||||
MessageQueueMessage* message, bool ignoreFault = false );
|
||||
/**
|
||||
* @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( MessageQueueMessage* message );
|
||||
/**
|
||||
* @brief This operation sends a message to the last communication partner.
|
||||
* @details This operation simplifies answering an incoming message by using the stored
|
||||
* lastParnter 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( MessageQueueMessage* message );
|
||||
MessageQueueMessageIF* message, bool ignoreFault = false) override;
|
||||
|
||||
/**
|
||||
* @brief This function reads available messages from the message queue and returns the sender.
|
||||
* @details It works identically to the other receiveMessage call, but in addition returns the
|
||||
* sender's queue id.
|
||||
* @param message A pointer to a message in which the received data is stored.
|
||||
* @param receivedFrom A pointer to a queue id in which the sender's id is stored.
|
||||
*/
|
||||
ReturnValue_t receiveMessage(MessageQueueMessage* message,
|
||||
MessageQueueId_t *receivedFrom);
|
||||
ReturnValue_t sendToDefault(MessageQueueMessageIF* message) override;
|
||||
|
||||
/**
|
||||
* @brief This function reads available messages from the message queue.
|
||||
* @details If data is available it is stored in the passed message pointer. The message's
|
||||
* original content is overwritten and the sendFrom information is stored in the
|
||||
* lastPartner attribute. Else, the lastPartner information remains untouched, the
|
||||
* message's content is cleared and the function returns immediately.
|
||||
* @param message A pointer to a message in which the received data is stored.
|
||||
*/
|
||||
ReturnValue_t receiveMessage(MessageQueueMessage* message);
|
||||
/**
|
||||
* Deletes all pending messages in the queue.
|
||||
* @param count The number of flushed messages.
|
||||
* @return RETURN_OK on success.
|
||||
*/
|
||||
ReturnValue_t flush(uint32_t* count);
|
||||
/**
|
||||
* @brief This method returns the message queue id of the last communication partner.
|
||||
*/
|
||||
MessageQueueId_t getLastPartner() const;
|
||||
/**
|
||||
* @brief This method returns the message queue id of this class's message queue.
|
||||
*/
|
||||
MessageQueueId_t getId() const;
|
||||
/**
|
||||
* \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, MessageQueueMessage* message, MessageQueueId_t sentFrom = NO_QUEUE, bool ignoreFault = false );
|
||||
/**
|
||||
* \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( MessageQueueMessage* message, MessageQueueId_t sentFrom = NO_QUEUE, bool ignoreFault = false );
|
||||
/**
|
||||
* \brief This method is a simple setter for the default destination.
|
||||
*/
|
||||
void setDefaultDestination(MessageQueueId_t defaultDestination);
|
||||
/**
|
||||
* \brief This method is a simple getter for the default destination.
|
||||
*/
|
||||
MessageQueueId_t getDefaultDestination() const;
|
||||
ReturnValue_t reply(MessageQueueMessageIF* message) override;
|
||||
virtual ReturnValue_t sendMessageFrom(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message,
|
||||
MessageQueueId_t sentFrom = NO_QUEUE,
|
||||
bool ignoreFault = false) override;
|
||||
|
||||
virtual ReturnValue_t sendToDefaultFrom( MessageQueueMessageIF* message,
|
||||
MessageQueueId_t sentFrom = NO_QUEUE,
|
||||
bool ignoreFault = false) override;
|
||||
|
||||
ReturnValue_t receiveMessage(MessageQueueMessageIF* message,
|
||||
MessageQueueId_t *receivedFrom) override;
|
||||
|
||||
ReturnValue_t receiveMessage(MessageQueueMessageIF* message) override;
|
||||
|
||||
ReturnValue_t flush(uint32_t* count) override;
|
||||
|
||||
MessageQueueId_t getLastPartner() const override;
|
||||
|
||||
MessageQueueId_t getId() const override;
|
||||
|
||||
void setDefaultDestination(MessageQueueId_t defaultDestination) override;
|
||||
|
||||
MessageQueueId_t getDefaultDestination() const override;
|
||||
|
||||
bool isDefaultDestinationSet() const override;
|
||||
|
||||
bool isDefaultDestinationSet() const;
|
||||
protected:
|
||||
/**
|
||||
* Implementation to be called from any send Call within MessageQueue and MessageQueueSenderIF
|
||||
* \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.
|
||||
* @brief Implementation to be called from any send Call within
|
||||
* MessageQueue and MessageQueueSenderIF.
|
||||
* @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.
|
||||
* @param context Specify whether call is made from task or from an ISR.
|
||||
*/
|
||||
static ReturnValue_t sendMessageFromMessageQueue(MessageQueueId_t sendTo,MessageQueueMessage* message, MessageQueueId_t sentFrom = NO_QUEUE,bool ignoreFault=false);
|
||||
static ReturnValue_t sendMessageFromMessageQueue(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, MessageQueueId_t sentFrom = NO_QUEUE,
|
||||
bool ignoreFault=false, CallContext callContext = CallContext::TASK);
|
||||
|
||||
static ReturnValue_t handleSendResult(BaseType_t result, bool ignoreFault);
|
||||
|
||||
private:
|
||||
bool defaultDestinationSet = false;
|
||||
QueueHandle_t handle;
|
||||
MessageQueueId_t defaultDestination;
|
||||
MessageQueueId_t lastPartner;
|
||||
MessageQueueId_t defaultDestination = MessageQueueIF::NO_QUEUE;
|
||||
MessageQueueId_t lastPartner = MessageQueueIF::NO_QUEUE;
|
||||
const size_t maxMessageSize;
|
||||
//! Stores the current system context
|
||||
CallContext callContext = CallContext::TASK;
|
||||
};
|
||||
|
||||
#endif /* MESSAGEQUEUE_H_ */
|
||||
#endif /* FSFW_OSAL_FREERTOS_MESSAGEQUEUE_H_ */
|
||||
|
@ -1,14 +1,14 @@
|
||||
#include "MessageQueue.h"
|
||||
|
||||
#include "../../ipc/MessageQueueSenderIF.h"
|
||||
#include "../../ipc/QueueFactory.h"
|
||||
|
||||
#include "MessageQueue.h"
|
||||
|
||||
|
||||
QueueFactory* QueueFactory::factoryInstance = nullptr;
|
||||
|
||||
|
||||
ReturnValue_t MessageQueueSenderIF::sendMessage(MessageQueueId_t sendTo,
|
||||
MessageQueueMessage* message, MessageQueueId_t sentFrom,
|
||||
MessageQueueMessageIF* message, MessageQueueId_t sentFrom,
|
||||
bool ignoreFault) {
|
||||
return MessageQueue::sendMessageFromMessageQueue(sendTo,message,
|
||||
sentFrom,ignoreFault);
|
||||
|
@ -9,9 +9,11 @@
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
|
||||
MessageQueue::MessageQueue(uint32_t messageDepth, size_t maxMessageSize):
|
||||
id(MessageQueueIF::NO_QUEUE),lastPartner(MessageQueueIF::NO_QUEUE),
|
||||
defaultDestination(MessageQueueIF::NO_QUEUE) {
|
||||
defaultDestination(MessageQueueIF::NO_QUEUE),
|
||||
maxMessageSize(maxMessageSize) {
|
||||
//debug << "MessageQueue::MessageQueue: Creating a queue" << std::endl;
|
||||
mq_attr attributes;
|
||||
this->id = 0;
|
||||
@ -46,7 +48,7 @@ MessageQueue::~MessageQueue() {
|
||||
status = mq_unlink(name);
|
||||
if(status != 0){
|
||||
sif::error << "MessageQueue::Destructor: mq_unlink Failed with status: "
|
||||
<< strerror(errno) <<std::endl;
|
||||
<< strerror(errno) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,22 +63,27 @@ ReturnValue_t MessageQueue::handleError(mq_attr* attributes,
|
||||
// Just an additional helpful printout :-)
|
||||
if(std::ifstream("/proc/sys/fs/mqueue/msg_max",std::ios::in) >>
|
||||
defaultMqMaxMsg and defaultMqMaxMsg < messageDepth) {
|
||||
// See: https://www.man7.org/linux/man-pages/man3/mq_open.3.html
|
||||
// This happens if the msg_max value is not large enough
|
||||
// It is ignored if the executable is run in privileged mode.
|
||||
// Run the unlockRealtime script or grant the mode manually by using:
|
||||
// sudo setcap 'CAP_SYS_RESOURCE=+ep' <pathToBinary>
|
||||
/*
|
||||
See: https://www.man7.org/linux/man-pages/man3/mq_open.3.html
|
||||
This happens if the msg_max value is not large enough
|
||||
It is ignored if the executable is run in privileged mode.
|
||||
Run the unlockRealtime script or grant the mode manually by using:
|
||||
sudo setcap 'CAP_SYS_RESOURCE=+ep' <pathToBinary>
|
||||
|
||||
// Persistent solution for session:
|
||||
// echo <newMsgMax> | sudo tee /proc/sys/fs/mqueue/msg_max
|
||||
Persistent solution for session:
|
||||
echo <newMsgMax> | sudo tee /< |