diff --git a/src/fsfw/ipc/MessageQueueBase.cpp b/src/fsfw/ipc/MessageQueueBase.cpp index c43670ed..56041bee 100644 --- a/src/fsfw/ipc/MessageQueueBase.cpp +++ b/src/fsfw/ipc/MessageQueueBase.cpp @@ -8,7 +8,7 @@ MessageQueueBase::MessageQueueBase(MessageQueueId_t id, MessageQueueId_t default } } -MessageQueueBase::~MessageQueueBase() {} +MessageQueueBase::~MessageQueueBase() = default; ReturnValue_t MessageQueueBase::sendToDefault(MessageQueueMessageIF* message) { return sendToDefaultFrom(message, this->getId(), false); diff --git a/src/fsfw/ipc/MessageQueueBase.h b/src/fsfw/ipc/MessageQueueBase.h index 942b6121..9c05b1a2 100644 --- a/src/fsfw/ipc/MessageQueueBase.h +++ b/src/fsfw/ipc/MessageQueueBase.h @@ -7,28 +7,28 @@ class MessageQueueBase : public MessageQueueIF { public: MessageQueueBase(MessageQueueId_t id, MessageQueueId_t defaultDest, MqArgs* mqArgs); - virtual ~MessageQueueBase(); + ~MessageQueueBase() override; // Default implementations for MessageQueueIF where possible - virtual MessageQueueId_t getLastPartner() const override; - virtual MessageQueueId_t getId() const override; - virtual MqArgs& getMqArgs() override; - virtual void setDefaultDestination(MessageQueueId_t defaultDestination) override; - virtual MessageQueueId_t getDefaultDestination() const override; - virtual bool isDefaultDestinationSet() const override; - virtual ReturnValue_t sendMessage(MessageQueueId_t sendTo, MessageQueueMessageIF* message, + [[nodiscard]] MessageQueueId_t getLastPartner() const override; + [[nodiscard]] MessageQueueId_t getId() const override; + MqArgs& getMqArgs() override; + void setDefaultDestination(MessageQueueId_t defaultDestination) override; + [[nodiscard]] MessageQueueId_t getDefaultDestination() const override; + [[nodiscard]] bool isDefaultDestinationSet() const override; + ReturnValue_t sendMessage(MessageQueueId_t sendTo, MessageQueueMessageIF* message, bool ignoreFault) override; - virtual ReturnValue_t sendToDefault(MessageQueueMessageIF* message) override; - virtual ReturnValue_t reply(MessageQueueMessageIF* message) override; - virtual ReturnValue_t receiveMessage(MessageQueueMessageIF* message, + ReturnValue_t sendToDefault(MessageQueueMessageIF* message) override; + ReturnValue_t reply(MessageQueueMessageIF* message) override; + ReturnValue_t receiveMessage(MessageQueueMessageIF* message, MessageQueueId_t* receivedFrom) override; - virtual ReturnValue_t sendToDefaultFrom(MessageQueueMessageIF* message, MessageQueueId_t sentFrom, + ReturnValue_t sendToDefaultFrom(MessageQueueMessageIF* message, MessageQueueId_t sentFrom, bool ignoreFault = false) override; // OSAL specific, forward the abstract function - virtual ReturnValue_t receiveMessage(MessageQueueMessageIF* message) = 0; - virtual ReturnValue_t sendMessageFrom(MessageQueueId_t sendTo, MessageQueueMessageIF* message, - MessageQueueId_t sentFrom, bool ignoreFault = false) = 0; + ReturnValue_t receiveMessage(MessageQueueMessageIF* message) override = 0; + ReturnValue_t sendMessageFrom(MessageQueueId_t sendTo, MessageQueueMessageIF* message, + MessageQueueId_t sentFrom, bool ignoreFault = false) override = 0; protected: MessageQueueId_t id = MessageQueueIF::NO_QUEUE; diff --git a/src/fsfw/ipc/MessageQueueIF.h b/src/fsfw/ipc/MessageQueueIF.h index 9532b2d6..4f69695a 100644 --- a/src/fsfw/ipc/MessageQueueIF.h +++ b/src/fsfw/ipc/MessageQueueIF.h @@ -30,7 +30,7 @@ class MessageQueueIF { //! [EXPORT] : [COMMENT] Returned if the target destination is invalid. static constexpr ReturnValue_t DESTINATION_INVALID = MAKE_RETURN_CODE(4); - virtual ~MessageQueueIF() {} + virtual ~MessageQueueIF() = default; /** * @brief This operation sends a message to the last communication partner. * @details @@ -82,11 +82,11 @@ class MessageQueueIF { /** * @brief This method returns the message queue ID of the last communication partner. */ - virtual MessageQueueId_t getLastPartner() const = 0; + [[nodiscard]] virtual MessageQueueId_t getLastPartner() const = 0; /** * @brief This method returns the message queue ID of this class's message queue. */ - virtual MessageQueueId_t getId() const = 0; + [[nodiscard]] virtual MessageQueueId_t getId() const = 0; /** * @brief With the sendMessage call, a queue message is sent to a receiving queue. @@ -159,9 +159,9 @@ class MessageQueueIF { /** * @brief This method is a simple getter for the default destination. */ - virtual MessageQueueId_t getDefaultDestination() const = 0; + [[nodiscard]] virtual MessageQueueId_t getDefaultDestination() const = 0; - virtual bool isDefaultDestinationSet() const = 0; + [[nodiscard]] virtual bool isDefaultDestinationSet() const = 0; virtual MqArgs& getMqArgs() = 0; }; diff --git a/src/fsfw/osal/host/MessageQueue.cpp b/src/fsfw/osal/host/MessageQueue.cpp index 5eb0c99a..dad05a5a 100644 --- a/src/fsfw/osal/host/MessageQueue.cpp +++ b/src/fsfw/osal/host/MessageQueue.cpp @@ -65,11 +65,11 @@ ReturnValue_t MessageQueue::sendMessageFromMessageQueue(MessageQueueId_t sendTo, // But I will still return a failure here. return HasReturnvaluesIF::RETURN_FAILED; } - MessageQueue* targetQueue = + auto* targetQueue = dynamic_cast(QueueMapManager::instance()->getMessageQueue(sendTo)); if (targetQueue == nullptr) { if (not ignoreFault) { - InternalErrorReporterIF* internalErrorReporter = + auto* internalErrorReporter = ObjectManager::instance()->get(objects::INTERNAL_ERROR_REPORTER); if (internalErrorReporter != nullptr) { internalErrorReporter->queueMessageNotSent(); diff --git a/src/fsfw/osal/host/QueueFactory.cpp b/src/fsfw/osal/host/QueueFactory.cpp index 732892ca..e2d83fd4 100644 --- a/src/fsfw/osal/host/QueueFactory.cpp +++ b/src/fsfw/osal/host/QueueFactory.cpp @@ -13,7 +13,6 @@ ReturnValue_t MessageQueueSenderIF::sendMessage(MessageQueueId_t sendTo, MessageQueueMessageIF* message, MessageQueueId_t sentFrom, bool ignoreFault) { return MessageQueue::sendMessageFromMessageQueue(sendTo, message, sentFrom, ignoreFault); - return HasReturnvaluesIF::RETURN_OK; } QueueFactory* QueueFactory::instance() { @@ -23,9 +22,9 @@ QueueFactory* QueueFactory::instance() { return factoryInstance; } -QueueFactory::QueueFactory() {} +QueueFactory::QueueFactory() = default; -QueueFactory::~QueueFactory() {} +QueueFactory::~QueueFactory() = default; MessageQueueIF* QueueFactory::createMessageQueue(uint32_t messageDepth, size_t maxMessageSize, MqArgs* args) { diff --git a/src/fsfw/tmtcservices/PusServiceBase.cpp b/src/fsfw/tmtcservices/PusServiceBase.cpp index e7a0e5b9..6a21b65b 100644 --- a/src/fsfw/tmtcservices/PusServiceBase.cpp +++ b/src/fsfw/tmtcservices/PusServiceBase.cpp @@ -131,7 +131,7 @@ void PusServiceBase::initializeTmHelpers(TmSendHelper& tmSendHelper, TmStoreHelp } void PusServiceBase::initializeTmSendHelper(TmSendHelper& tmSendHelper) { - tmSendHelper.setMsgSource(requestQueue->getId()); + tmSendHelper.setMsgQueue(requestQueue); tmSendHelper.setMsgDestination(requestQueue->getDefaultDestination()); if (errReporter == nullptr) { errReporter = diff --git a/src/fsfw/tmtcservices/TmSendHelper.cpp b/src/fsfw/tmtcservices/TmSendHelper.cpp index 76591ca2..f8c76b75 100644 --- a/src/fsfw/tmtcservices/TmSendHelper.cpp +++ b/src/fsfw/tmtcservices/TmSendHelper.cpp @@ -2,15 +2,21 @@ #include "fsfw/ipc/MessageQueueSenderIF.h" -TmSendHelper::TmSendHelper(MessageQueueId_t tmtcMsgDest, MessageQueueId_t tmtcMsgSrc, +TmSendHelper::TmSendHelper(MessageQueueIF* queue, MessageQueueId_t tmtcMsgDest, InternalErrorReporterIF *reporter) - : tmtcMsgDest(tmtcMsgDest), tmtcMsgSrc(tmtcMsgSrc), errReporter(reporter) {} + : tmtcMsgDest(tmtcMsgDest), queue(queue), errReporter(reporter) {} + +TmSendHelper::TmSendHelper(MessageQueueIF *queue, InternalErrorReporterIF *reporter) + : queue(queue), errReporter(reporter) {} TmSendHelper::TmSendHelper(InternalErrorReporterIF *reporter) : errReporter(reporter) {} ReturnValue_t TmSendHelper::sendPacket(const store_address_t &storeId) { + if(queue == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } TmTcMessage message(storeId); - ReturnValue_t result = MessageQueueSenderIF::sendMessage(tmtcMsgDest, &message, tmtcMsgSrc); + ReturnValue_t result = queue->sendMessage(tmtcMsgDest, &message, ignoreFault); if (result != HasReturnvaluesIF::RETURN_OK) { if (errReporter != nullptr) { errReporter->lostTm(); @@ -22,8 +28,7 @@ ReturnValue_t TmSendHelper::sendPacket(const store_address_t &storeId) { void TmSendHelper::setMsgDestination(MessageQueueId_t msgDest) { tmtcMsgDest = msgDest; } -void TmSendHelper::setMsgSource(MessageQueueId_t msgSrc) { tmtcMsgSrc = msgSrc; } - void TmSendHelper::setInternalErrorReporter(InternalErrorReporterIF *reporter) { errReporter = reporter; } +void TmSendHelper::setMsgQueue(MessageQueueIF *queue_) { queue = queue_; } diff --git a/src/fsfw/tmtcservices/TmSendHelper.h b/src/fsfw/tmtcservices/TmSendHelper.h index f7acb237..bca0d4a2 100644 --- a/src/fsfw/tmtcservices/TmSendHelper.h +++ b/src/fsfw/tmtcservices/TmSendHelper.h @@ -10,16 +10,18 @@ class TmSendHelper { public: explicit TmSendHelper(InternalErrorReporterIF* reporter); - TmSendHelper(MessageQueueId_t tmtcMsgDest, MessageQueueId_t tmtcMsgSrc, + TmSendHelper(MessageQueueIF* queue, InternalErrorReporterIF* reporter); + TmSendHelper(MessageQueueIF* queue, MessageQueueId_t tmtcMsgDest, InternalErrorReporterIF* reporter); + void setMsgQueue(MessageQueueIF* queue); void setMsgDestination(MessageQueueId_t msgDest); - void setMsgSource(MessageQueueId_t msgSrc); void setInternalErrorReporter(InternalErrorReporterIF* reporter); ReturnValue_t sendPacket(const store_address_t& storeId); private: MessageQueueId_t tmtcMsgDest = MessageQueueIF::NO_QUEUE; - MessageQueueId_t tmtcMsgSrc = MessageQueueIF::NO_QUEUE; + bool ignoreFault = false; + MessageQueueIF* queue = nullptr; InternalErrorReporterIF* errReporter; }; diff --git a/unittests/internalerror/TestInternalErrorReporter.cpp b/unittests/internalerror/TestInternalErrorReporter.cpp index 2b999fae..7349400e 100644 --- a/unittests/internalerror/TestInternalErrorReporter.cpp +++ b/unittests/internalerror/TestInternalErrorReporter.cpp @@ -33,7 +33,7 @@ TEST_CASE("Internal Error Reporter", "[TestInternalError]") { MessageQueueIF* hkQueue = QueueFactory::instance()->createMessageQueue(1); internalErrorReporter->getSubscriptionInterface()->subscribeForSetUpdateMessage( InternalErrorDataset::ERROR_SET_ID, objects::NO_OBJECT, hkQueue->getId(), true); - StorageManagerIF* ipcStore = ObjectManager::instance()->get(objects::IPC_STORE); + auto* ipcStore = ObjectManager::instance()->get(objects::IPC_STORE); SECTION("MessageQueueFull") { CommandMessage message; ActionMessage::setCompletionReply(&message, 10, true); @@ -57,7 +57,7 @@ TEST_CASE("Internal Error Reporter", "[TestInternalError]") { REQUIRE(gpid.objectId == objects::INTERNAL_ERROR_REPORTER); // We need the object ID of the reporter here (NO_OBJECT) InternalErrorDataset dataset(objects::INTERNAL_ERROR_REPORTER); - CCSDSTime::CDS_short time; + CCSDSTime::CDS_short time{}; ConstAccessorPair data = ipcStore->getData(storeAddress); REQUIRE(data.first == HasReturnvaluesIF::RETURN_OK); HousekeepingSnapshot hkSnapshot(&time, &dataset); @@ -87,7 +87,7 @@ TEST_CASE("Internal Error Reporter", "[TestInternalError]") { ConstAccessorPair data = ipcStore->getData(storeAddress); REQUIRE(data.first == HasReturnvaluesIF::RETURN_OK); - CCSDSTime::CDS_short time; + CCSDSTime::CDS_short time{}; // We need the object ID of the reporter here (NO_OBJECT) InternalErrorDataset dataset(objects::INTERNAL_ERROR_REPORTER); HousekeepingSnapshot hkSnapshot(&time, &dataset); @@ -107,10 +107,10 @@ TEST_CASE("Internal Error Reporter", "[TestInternalError]") { // Message Queue Id MessageQueueId_t id = internalErrorReporter->getCommandQueue(); REQUIRE(id != MessageQueueIF::NO_QUEUE); - CommandMessage message; + CommandMessage message2; sid_t sid(objects::INTERNAL_ERROR_REPORTER, InternalErrorDataset::ERROR_SET_ID); - HousekeepingMessage::setToggleReportingCommand(&message, sid, true, false); - result = hkQueue->sendMessage(id, &message); + HousekeepingMessage::setToggleReportingCommand(&message2, sid, true, false); + result = hkQueue->sendMessage(id, &message2); REQUIRE(result == HasReturnvaluesIF::RETURN_OK); internalErrorReporter->performOperation(0); } diff --git a/unittests/mocks/CMakeLists.txt b/unittests/mocks/CMakeLists.txt index 50a34ce8..90448798 100644 --- a/unittests/mocks/CMakeLists.txt +++ b/unittests/mocks/CMakeLists.txt @@ -4,4 +4,5 @@ target_sources(${FSFW_TEST_TGT} PRIVATE DeviceFdirMock.cpp CookieIFMock.cpp ComIFMock.cpp + MessageQueueMockBase.cpp ) diff --git a/unittests/mocks/InternalErrorReporterMock.h b/unittests/mocks/InternalErrorReporterMock.h new file mode 100644 index 00000000..ccc158d0 --- /dev/null +++ b/unittests/mocks/InternalErrorReporterMock.h @@ -0,0 +1,11 @@ +#ifndef FSFW_TESTS_INTERNALERRORREPORTERMOCK_H +#define FSFW_TESTS_INTERNALERRORREPORTERMOCK_H + +#include "fsfw/internalerror/InternalErrorReporterIF.h" + +class InternalErrorReporterMock: public InternalErrorReporterIF { + public: + InternalErrorReporterMock(); + private: +}; +#endif // FSFW_TESTS_INTERNALERRORREPORTERMOCK_H diff --git a/unittests/mocks/MessageQueueMockBase.cpp b/unittests/mocks/MessageQueueMockBase.cpp new file mode 100644 index 00000000..8c577851 --- /dev/null +++ b/unittests/mocks/MessageQueueMockBase.cpp @@ -0,0 +1,67 @@ +#include "MessageQueueMockBase.h" + + +MessageQueueMockBase::MessageQueueMockBase() + : MessageQueueBase(MessageQueueIF::NO_QUEUE, MessageQueueIF::NO_QUEUE, nullptr) {} + +MessageQueueMockBase::MessageQueueMockBase(MessageQueueId_t queueId) + : MessageQueueBase(queueId, MessageQueueIF::NO_QUEUE, nullptr) {} + +bool MessageQueueMockBase::wasMessageSent(uint8_t* messageSentCounter_, bool resetCounter) { + bool tempMessageSent = messageSent; + messageSent = false; + if (messageSentCounter_ != nullptr) { + *messageSentCounter_ = this->messageSentCounter; + } + if (resetCounter) { + this->messageSentCounter = 0; + } + return tempMessageSent; +} + +ReturnValue_t MessageQueueMockBase::popMessage() { + CommandMessage message; + message.clear(); + return receiveMessage(&message); +} + +ReturnValue_t MessageQueueMockBase::receiveMessage(MessageQueueMessageIF* message) { + if (messagesSentQueue.empty()) { + return MessageQueueIF::EMPTY; + } + this->last = message->getSender(); + std::memcpy(message->getBuffer(), messagesSentQueue.front().getBuffer(), + message->getMessageSize()); + messagesSentQueue.pop(); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t MessageQueueMockBase::flush(uint32_t* count) { + return HasReturnvaluesIF::RETURN_FAILED; +} + +ReturnValue_t MessageQueueMockBase::sendMessageFrom(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, + MessageQueueId_t sentFrom, bool ignoreFault) { + messageSent = true; + messageSentCounter++; + MessageQueueMessage& messageRef = *(dynamic_cast(message)); + messagesSentQueue.push(messageRef); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t MessageQueueMockBase::reply(MessageQueueMessageIF* message) { + return sendMessageFrom(MessageQueueIF::NO_QUEUE, message, this->getId(), false); +} + +void MessageQueueMockBase::clearMessages(bool clearCommandMessages) { + while (not messagesSentQueue.empty()) { + if (clearCommandMessages) { + CommandMessage message; + std::memcpy(message.getBuffer(), messagesSentQueue.front().getBuffer(), + message.getMessageSize()); + message.clear(); + } + messagesSentQueue.pop(); + } +} diff --git a/unittests/mocks/MessageQueueMockBase.h b/unittests/mocks/MessageQueueMockBase.h index 92729a7a..4a4b7ce7 100644 --- a/unittests/mocks/MessageQueueMockBase.h +++ b/unittests/mocks/MessageQueueMockBase.h @@ -12,71 +12,30 @@ class MessageQueueMockBase : public MessageQueueBase { public: - MessageQueueMockBase() - : MessageQueueBase(MessageQueueIF::NO_QUEUE, MessageQueueIF::NO_QUEUE, nullptr) {} + MessageQueueMockBase(); + + explicit MessageQueueMockBase(MessageQueueId_t queueId); uint8_t messageSentCounter = 0; bool messageSent = false; - bool wasMessageSent(uint8_t* messageSentCounter_ = nullptr, bool resetCounter = true) { - bool tempMessageSent = messageSent; - messageSent = false; - if (messageSentCounter_ != nullptr) { - *messageSentCounter_ = this->messageSentCounter; - } - if (resetCounter) { - this->messageSentCounter = 0; - } - return tempMessageSent; - } + bool wasMessageSent(uint8_t* messageSentCounter_ = nullptr, bool resetCounter = true); /** * Pop a message, clearing it in the process. * @return */ - ReturnValue_t popMessage() { - CommandMessage message; - message.clear(); - return receiveMessage(&message); - } + ReturnValue_t popMessage(); - ReturnValue_t receiveMessage(MessageQueueMessageIF* message) override { - if (messagesSentQueue.empty()) { - return MessageQueueIF::EMPTY; - } - this->last = message->getSender(); - std::memcpy(message->getBuffer(), messagesSentQueue.front().getBuffer(), - message->getMessageSize()); - messagesSentQueue.pop(); - return HasReturnvaluesIF::RETURN_OK; - } - ReturnValue_t flush(uint32_t* count) override { return HasReturnvaluesIF::RETURN_OK; } + ReturnValue_t receiveMessage(MessageQueueMessageIF* message) override; + + ReturnValue_t flush(uint32_t* count) override; ReturnValue_t sendMessageFrom(MessageQueueId_t sendTo, MessageQueueMessageIF* message, MessageQueueId_t sentFrom, - bool ignoreFault = false) override { - messageSent = true; - messageSentCounter++; - MessageQueueMessage& messageRef = *(dynamic_cast(message)); - messagesSentQueue.push(messageRef); - return HasReturnvaluesIF::RETURN_OK; - } - - ReturnValue_t reply(MessageQueueMessageIF* message) override { - return sendMessageFrom(MessageQueueIF::NO_QUEUE, message, this->getId(), false); - } - - void clearMessages(bool clearCommandMessages = true) { - while (not messagesSentQueue.empty()) { - if (clearCommandMessages) { - CommandMessage message; - std::memcpy(message.getBuffer(), messagesSentQueue.front().getBuffer(), - message.getMessageSize()); - message.clear(); - } - messagesSentQueue.pop(); - } - } + bool ignoreFault = false) override; + ReturnValue_t reply(MessageQueueMessageIF* message) override; + void clearMessages(bool clearCommandMessages = true); private: std::queue messagesSentQueue; }; diff --git a/unittests/tmtcservices/testSendHelper.cpp b/unittests/tmtcservices/testSendHelper.cpp index 66c7ab59..84ef0162 100644 --- a/unittests/tmtcservices/testSendHelper.cpp +++ b/unittests/tmtcservices/testSendHelper.cpp @@ -1,5 +1,7 @@ #include -TEST_CASE("TM Send Helper", "[tm-send-helper]") { +#include "fsfw/tmtcservices/TmSendHelper.h" +TEST_CASE("TM Send Helper", "[tm-send-helper]") { + TmSendHelper sendHelper(nullptr); } \ No newline at end of file