fsfw/src/fsfw/osal/host/QueueMapManager.cpp

65 lines
2.0 KiB
C++
Raw Normal View History

#include "fsfw/osal/host/QueueMapManager.h"
2020-09-05 21:19:53 +02:00
#include "fsfw/ipc/MutexFactory.h"
#include "fsfw/ipc/MutexGuard.h"
2022-02-02 10:29:30 +01:00
#include "fsfw/serviceinterface/ServiceInterface.h"
2020-09-05 20:18:52 +02:00
QueueMapManager* QueueMapManager::mqManagerInstance = nullptr;
2022-02-02 10:29:30 +01:00
QueueMapManager::QueueMapManager() { mapLock = MutexFactory::instance()->createMutex(); }
2020-09-05 20:18:52 +02:00
2022-02-02 10:29:30 +01:00
QueueMapManager::~QueueMapManager() { MutexFactory::instance()->deleteMutex(mapLock); }
2021-04-08 18:57:24 +02:00
2020-09-05 20:18:52 +02:00
QueueMapManager* QueueMapManager::instance() {
2022-02-02 10:29:30 +01:00
if (mqManagerInstance == nullptr) {
mqManagerInstance = new QueueMapManager();
}
return QueueMapManager::mqManagerInstance;
2020-09-05 20:18:52 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t QueueMapManager::addMessageQueue(MessageQueueIF* queueToInsert,
MessageQueueId_t* id) {
MutexGuard lock(mapLock);
uint32_t currentId = queueCounter;
queueCounter++;
if (currentId == MessageQueueIF::NO_QUEUE) {
// Skip the NO_QUEUE value
currentId = queueCounter;
2021-07-19 15:03:02 +02:00
queueCounter++;
2022-02-02 10:29:30 +01:00
}
auto returnPair = queueMap.emplace(currentId, queueToInsert);
if (not returnPair.second) {
/* This should never happen for the atomic variable. */
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "QueueMapManager::addMessageQueue This ID is already "
"inside the map!"
<< std::endl;
2021-02-04 13:46:27 +01:00
#else
2022-02-02 10:29:30 +01:00
sif::printError(
"QueueMapManager::addMessageQueue This ID is already "
"inside the map!\n");
#endif
2022-08-16 01:08:26 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
if (id != nullptr) {
*id = currentId;
}
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2020-09-05 20:18:52 +02:00
}
2022-02-02 10:29:30 +01:00
MessageQueueIF* QueueMapManager::getMessageQueue(MessageQueueId_t messageQueueId) const {
auto queueIter = queueMap.find(messageQueueId);
if (queueIter != queueMap.end()) {
return queueIter->second;
} else {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "QueueMapManager::getQueueHandle: The ID " << messageQueueId
2022-07-27 17:00:43 +02:00
<< " does not exist in the map" << std::endl;
2021-02-04 13:46:27 +01:00
#else
2022-02-02 10:29:30 +01:00
sif::printWarning("QueueMapManager::getQueueHandle: The ID %d does not exist in the map!\n",
messageQueueId);
#endif
2022-02-02 10:29:30 +01:00
}
return nullptr;
2020-09-05 20:18:52 +02:00
}