From bf673c56c69b16a8aeae12b30de1dd90c5588dfc Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Sun, 5 Jun 2022 12:52:55 +0200 Subject: [PATCH] unit test for dhb --- src/fsfw/devicehandlers/DeviceHandlerBase.cpp | 8 ++- src/fsfw/events/EventManager.cpp | 5 ++ src/fsfw/events/EventManager.h | 2 + src/fsfw/events/EventManagerIF.h | 2 + src/fsfw/fdir/FailureIsolationBase.cpp | 12 +++- src/fsfw/fdir/FaultCounter.cpp | 8 +-- src/fsfw/fdir/FaultCounter.h | 14 +++- src/fsfw/health/HealthHelper.cpp | 4 +- src/fsfw/health/HealthTable.cpp | 9 +++ src/fsfw/health/HealthTable.h | 1 + src/fsfw/health/HealthTableIF.h | 2 + tests/src/fsfw_tests/unit/CMakeLists.txt | 1 + tests/src/fsfw_tests/unit/CatchFactory.cpp | 2 +- .../unit/devicehandler/ComIFMock.cpp | 31 ++++++-- .../fsfw_tests/unit/devicehandler/ComIFMock.h | 15 +++- .../unit/devicehandler/CookieIFMock.h | 2 +- .../devicehandler/DeviceHandlerCommander.cpp | 9 +-- .../devicehandler/DeviceHandlerCommander.h | 2 + .../unit/devicehandler/DeviceHandlerMock.cpp | 71 +++++++++++++------ .../unit/devicehandler/DeviceHandlerMock.h | 48 ++++++++----- .../devicehandler/TestDeviceHandlerBase.cpp | 65 +++++++++++------ .../devicehandler/TestDeviceHandlerBase.h | 49 ------------- 22 files changed, 224 insertions(+), 138 deletions(-) delete mode 100644 tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.h diff --git a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp index fb9a597cf..a874824d0 100644 --- a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp +++ b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp @@ -65,7 +65,9 @@ void DeviceHandlerBase::setThermalStateRequestPoolIds(lp_id_t thermalStatePoolId } DeviceHandlerBase::~DeviceHandlerBase() { - delete comCookie; + if (comCookie != nullptr) { + delete comCookie; + } if (defaultFDIRUsed) { delete fdirInstance; } @@ -253,7 +255,9 @@ void DeviceHandlerBase::decrementDeviceReplyMap() { replyToReply(replyPair.first, replyPair.second, TIMEOUT); missedReply(replyPair.first); timedOut = false; - replyPair.second.active = false; + if (not replyPair.second.periodic) { + replyPair.second.active = false; + } } } } diff --git a/src/fsfw/events/EventManager.cpp b/src/fsfw/events/EventManager.cpp index aaa7d6c52..47270d2ab 100644 --- a/src/fsfw/events/EventManager.cpp +++ b/src/fsfw/events/EventManager.cpp @@ -88,6 +88,11 @@ ReturnValue_t EventManager::subscribeToEventRange(MessageQueueId_t listener, Eve return result; } +ReturnValue_t EventManager::unsubscribeFromAllEvents(MessageQueueId_t listener, + object_id_t object) { + return unsubscribeFromEventRange(listener, 0, 0, true, object); +} + ReturnValue_t EventManager::unsubscribeFromEventRange(MessageQueueId_t listener, EventId_t idFrom, EventId_t idTo, bool idInverted, object_id_t reporterFrom, diff --git a/src/fsfw/events/EventManager.h b/src/fsfw/events/EventManager.h index f2d642ffb..77d657e8a 100644 --- a/src/fsfw/events/EventManager.h +++ b/src/fsfw/events/EventManager.h @@ -37,6 +37,8 @@ class EventManager : public EventManagerIF, public ExecutableObjectIF, public Sy EventId_t idTo = 0, bool idInverted = false, object_id_t reporterFrom = 0, object_id_t reporterTo = 0, bool reporterInverted = false); + ReturnValue_t unsubscribeFromAllEvents(MessageQueueId_t listener, + object_id_t object); ReturnValue_t unsubscribeFromEventRange(MessageQueueId_t listener, EventId_t idFrom = 0, EventId_t idTo = 0, bool idInverted = false, object_id_t reporterFrom = 0, object_id_t reporterTo = 0, diff --git a/src/fsfw/events/EventManagerIF.h b/src/fsfw/events/EventManagerIF.h index 98051ba05..893f86f1d 100644 --- a/src/fsfw/events/EventManagerIF.h +++ b/src/fsfw/events/EventManagerIF.h @@ -20,6 +20,8 @@ class EventManagerIF { bool forwardAllButSelected = false) = 0; virtual ReturnValue_t subscribeToEvent(MessageQueueId_t listener, EventId_t event) = 0; virtual ReturnValue_t subscribeToAllEventsFrom(MessageQueueId_t listener, object_id_t object) = 0; + virtual ReturnValue_t unsubscribeFromAllEvents(MessageQueueId_t listener, + object_id_t object) = 0; virtual ReturnValue_t subscribeToEventRange(MessageQueueId_t listener, EventId_t idFrom = 0, EventId_t idTo = 0, bool idInverted = false, object_id_t reporterFrom = 0, diff --git a/src/fsfw/fdir/FailureIsolationBase.cpp b/src/fsfw/fdir/FailureIsolationBase.cpp index fedef8696..da17d783c 100644 --- a/src/fsfw/fdir/FailureIsolationBase.cpp +++ b/src/fsfw/fdir/FailureIsolationBase.cpp @@ -14,7 +14,17 @@ FailureIsolationBase::FailureIsolationBase(object_id_t owner, object_id_t parent } FailureIsolationBase::~FailureIsolationBase() { - QueueFactory::instance()->deleteMessageQueue(eventQueue); + EventManagerIF* manager = ObjectManager::instance()->get(objects::EVENT_MANAGER); + if (manager == nullptr) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::error << "FailureIsolationBase::~FailureIsolationBase: Event Manager has not" + " been initialized!" + << std::endl; +#endif + return; + } + manager->unsubscribeFromAllEvents(eventQueue->getId(), ownerId); + QueueFactory::instance()->deleteMessageQueue(eventQueue); } ReturnValue_t FailureIsolationBase::initialize() { diff --git a/src/fsfw/fdir/FaultCounter.cpp b/src/fsfw/fdir/FaultCounter.cpp index e87cf6134..4515e5b11 100644 --- a/src/fsfw/fdir/FaultCounter.cpp +++ b/src/fsfw/fdir/FaultCounter.cpp @@ -60,14 +60,14 @@ ReturnValue_t FaultCounter::getParameter(uint8_t domainId, uint8_t uniqueId, return INVALID_DOMAIN_ID; } - switch (uniqueId) { - case 0: + switch (static_cast(uniqueId)) { + case ParameterIds::FAILURE_THRESHOLD: parameterWrapper->set(failureThreshold); break; - case 1: + case ParameterIds::FAULT_COUNT: parameterWrapper->set(faultCount); break; - case 2: + case ParameterIds::TIMEOUT: parameterWrapper->set(timer.timeout); break; default: diff --git a/src/fsfw/fdir/FaultCounter.h b/src/fsfw/fdir/FaultCounter.h index 3b59885c2..c5630da97 100644 --- a/src/fsfw/fdir/FaultCounter.h +++ b/src/fsfw/fdir/FaultCounter.h @@ -6,6 +6,13 @@ class FaultCounter : public HasParametersIF { public: + + enum class ParameterIds { + FAILURE_THRESHOLD, + FAULT_COUNT, + TIMEOUT + }; + FaultCounter(); FaultCounter(uint32_t failureThreshold, uint32_t decrementAfterMs, uint8_t setParameterDomain = 0); @@ -23,9 +30,10 @@ class FaultCounter : public HasParametersIF { void setFailureThreshold(uint32_t failureThreshold); void setFaultDecrementTimeMs(uint32_t timeMs); - virtual ReturnValue_t getParameter(uint8_t domainId, uint8_t uniqueId, - ParameterWrapper *parameterWrapper, - const ParameterWrapper *newValues, uint16_t startAtIndex); + virtual ReturnValue_t getParameter(uint8_t domainId, uint8_t uniqueId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues = nullptr, uint16_t startAtIndex = + 0); void setParameterDomain(uint8_t domain); diff --git a/src/fsfw/health/HealthHelper.cpp b/src/fsfw/health/HealthHelper.cpp index 4bef128c1..b98841156 100644 --- a/src/fsfw/health/HealthHelper.cpp +++ b/src/fsfw/health/HealthHelper.cpp @@ -5,7 +5,9 @@ HealthHelper::HealthHelper(HasHealthIF* owner, object_id_t objectId) : objectId(objectId), owner(owner) {} -HealthHelper::~HealthHelper() {} +HealthHelper::~HealthHelper() { + healthTable->removeObject(objectId); +} ReturnValue_t HealthHelper::handleHealthCommand(CommandMessage* message) { switch (message->getCommand()) { diff --git a/src/fsfw/health/HealthTable.cpp b/src/fsfw/health/HealthTable.cpp index ab4881f41..ee8fcd94f 100644 --- a/src/fsfw/health/HealthTable.cpp +++ b/src/fsfw/health/HealthTable.cpp @@ -27,6 +27,15 @@ ReturnValue_t HealthTable::registerObject(object_id_t object, return HasReturnvaluesIF::RETURN_OK; } +ReturnValue_t HealthTable::removeObject(object_id_t object) { + mapIterator = healthMap.find(object); + if (mapIterator == healthMap.end()) { + return HasReturnvaluesIF::RETURN_FAILED; + } + healthMap.erase(mapIterator); + return HasReturnvaluesIF::RETURN_OK; +} + void HealthTable::setHealth(object_id_t object, HasHealthIF::HealthState newState) { MutexGuard(mutex, timeoutType, mutexTimeoutMs); HealthMap::iterator iter = healthMap.find(object); diff --git a/src/fsfw/health/HealthTable.h b/src/fsfw/health/HealthTable.h index 4718abc35..60e00f92c 100644 --- a/src/fsfw/health/HealthTable.h +++ b/src/fsfw/health/HealthTable.h @@ -17,6 +17,7 @@ class HealthTable : public HealthTableIF, public SystemObject { /** HealthTableIF overrides */ virtual ReturnValue_t registerObject( object_id_t object, HasHealthIF::HealthState initilialState = HasHealthIF::HEALTHY) override; + ReturnValue_t removeObject(object_id_t object) override; virtual size_t getPrintSize() override; virtual void printAll(uint8_t* pointer, size_t maxSize) override; diff --git a/src/fsfw/health/HealthTableIF.h b/src/fsfw/health/HealthTableIF.h index 50266ac23..aab3f0eb1 100644 --- a/src/fsfw/health/HealthTableIF.h +++ b/src/fsfw/health/HealthTableIF.h @@ -14,6 +14,8 @@ class HealthTableIF : public ManagesHealthIF { virtual ReturnValue_t registerObject( object_id_t object, HasHealthIF::HealthState initilialState = HasHealthIF::HEALTHY) = 0; + virtual ReturnValue_t removeObject(object_id_t objectId) = 0; + virtual size_t getPrintSize() = 0; virtual void printAll(uint8_t *pointer, size_t maxSize) = 0; diff --git a/tests/src/fsfw_tests/unit/CMakeLists.txt b/tests/src/fsfw_tests/unit/CMakeLists.txt index a8d31d88a..eb57202ac 100644 --- a/tests/src/fsfw_tests/unit/CMakeLists.txt +++ b/tests/src/fsfw_tests/unit/CMakeLists.txt @@ -23,3 +23,4 @@ add_subdirectory(tmtcpacket) add_subdirectory(cfdp) add_subdirectory(hal) add_subdirectory(internalerror) +add_subdirectory(devicehandler) diff --git a/tests/src/fsfw_tests/unit/CatchFactory.cpp b/tests/src/fsfw_tests/unit/CatchFactory.cpp index 01018dfac..860a9bed2 100644 --- a/tests/src/fsfw_tests/unit/CatchFactory.cpp +++ b/tests/src/fsfw_tests/unit/CatchFactory.cpp @@ -65,7 +65,7 @@ void Factory::setStaticFrameworkObjectIds() { VerificationReporter::messageReceiver = objects::PUS_SERVICE_1_VERIFICATION; DeviceHandlerBase::powerSwitcherId = objects::NO_OBJECT; - DeviceHandlerBase::rawDataReceiverId = objects::PUS_SERVICE_2_DEVICE_ACCESS; + DeviceHandlerBase::rawDataReceiverId = objects::NO_OBJECT; LocalDataPoolManager::defaultHkDestination = objects::HK_RECEIVER_MOCK; diff --git a/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.cpp b/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.cpp index 6bcf5e201..4dd64c1a4 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.cpp @@ -1,13 +1,14 @@ #include "ComIFMock.h" +#include "DeviceHandlerMock.h" -ComIFMock::ComIFMock(obejct_id_t objectId) {} +ComIFMock::ComIFMock(object_id_t objectId) : SystemObject(objectId){} ComIFMock::~ComIFMock() {} ReturnValue_t ComIFMock::initializeInterface(CookieIF *cookie) { return RETURN_OK; } ReturnValue_t ComIFMock::sendMessage(CookieIF *cookie, const uint8_t *sendData, size_t sendLen) { - rememberSentByte = *sendData; + data = *sendData; return RETURN_OK; } @@ -18,7 +19,29 @@ ReturnValue_t ComIFMock::requestReceiveMessage(CookieIF *cookie, size_t requestL } ReturnValue_t ComIFMock::readReceivedMessage(CookieIF *cookie, uint8_t **buffer, size_t *size) { - *size = sizeof(rememberSentByte); - *buffer = &rememberSentByte; + switch(testCase) { + case TestCase::MISSED_REPLY: { + *size = 0; + return RETURN_OK; + } + case TestCase::SIMPLE_COMMAND_NOMINAL: { + *size = 1; + data = DeviceHandlerMock::SIMPLE_COMMAND_DATA; + *buffer = &data; + break; + } + case TestCase::PERIODIC_REPLY_NOMINAL: { + *size = 1; + data = DeviceHandlerMock::PERIODIC_REPLY_DATA; + *buffer = &data; + break; + } + default: + break; + } return RETURN_OK; } + +void ComIFMock::setTestCase(TestCase testCase_) { + testCase = testCase_; +} diff --git a/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.h b/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.h index fde6be453..7de4937f8 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.h +++ b/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.h @@ -2,14 +2,17 @@ #define TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_COMIFMOCK_H_ #include +#include /** * @brief The ComIFMock supports the simulation of various device communication error cases - * like incomplete or wrong replies and can be used to test the DeviceHandlerBase. + * like incomplete or wrong replies and can be used to test the DeviceHandlerBase. */ class ComIFMock : public DeviceCommunicationIF, public SystemObject { public: - ComIFMock(obejct_id_t objectId); + enum class TestCase { SIMPLE_COMMAND_NOMINAL, PERIODIC_REPLY_NOMINAL, MISSED_REPLY }; + + ComIFMock(object_id_t objectId); virtual ~ComIFMock(); virtual ReturnValue_t initializeInterface(CookieIF *cookie) override; @@ -19,9 +22,15 @@ class ComIFMock : public DeviceCommunicationIF, public SystemObject { virtual ReturnValue_t requestReceiveMessage(CookieIF *cookie, size_t requestLen) override; virtual ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer, size_t *size) override; + void setTestCase(TestCase testCase_); private: - uint8_t rememberSentByte = 0; + TestCase testCase = TestCase::SIMPLE_COMMAND_NOMINAL; + + static const uint8_t SIMPLE_COMMAND_DATA = 1; + static const uint8_t PERIODIC_REPLY_DATA = 2; + + uint8_t data = 0; }; #endif /* TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_COMIFMOCK_H_ */ diff --git a/tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.h b/tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.h index ad40f55f6..5c8689328 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.h +++ b/tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.h @@ -3,7 +3,7 @@ #include "fsfw/devicehandlers/CookieIF.h" -class CookieIFMock { +class CookieIFMock : public CookieIF { public: CookieIFMock(); virtual ~CookieIFMock(); diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.cpp b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.cpp index 4da5f36cf..b3889b2c9 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.cpp @@ -1,4 +1,5 @@ #include "DeviceHandlerCommander.h" +#include DeviceHandlerCommander::DeviceHandlerCommander(object_id_t objectId) : SystemObject(objectId), commandActionHelper(this) { @@ -11,6 +12,7 @@ DeviceHandlerCommander::~DeviceHandlerCommander() {} ReturnValue_t DeviceHandlerCommander::performOperation(uint8_t operationCode) { readCommandQueue(); + return RETURN_OK; } ReturnValue_t DeviceHandlerCommander::initialize() { @@ -18,10 +20,6 @@ ReturnValue_t DeviceHandlerCommander::initialize() { if (result != HasReturnvaluesIF::RETURN_OK) { return result; } - result = actionHelper.initialize(commandQueue); - if (result != HasReturnvaluesIF::RETURN_OK) { - return result; - } return HasReturnvaluesIF::RETURN_OK; } @@ -49,9 +47,6 @@ void DeviceHandlerCommander::readCommandQueue() { ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; for (result = commandQueue->receiveMessage(&message); result == HasReturnvaluesIF::RETURN_OK; result = commandQueue->receiveMessage(&message)) { - if (result != HasReturnvaluesIF::RETURN_OK) { - continue; - } result = commandActionHelper.handleReply(&message); if (result == HasReturnvaluesIF::RETURN_OK) { continue; diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h index 587932e04..771680437 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h @@ -37,6 +37,8 @@ class DeviceHandlerCommander : public ExecutableObjectIF, private: + static const uint32_t QUEUE_SIZE = 20; + MessageQueueIF* commandQueue = nullptr; CommandActionHelper commandActionHelper; diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp index ccdabc40a..ca1dff2f6 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp @@ -1,18 +1,18 @@ #include "DeviceHandlerMock.h" -DeviceHandlerMock::DeviceHandlerMock(object_id_t objectId, object_id_t deviceCommunication) - : DeviceHandlerBase(objetcId, deviceCommunication, nullptr) {} +#include -DeviceHandlerMock::~DeviceHandlerMock() { +DeviceHandlerMock::DeviceHandlerMock(object_id_t objectId, object_id_t deviceCommunication, + CookieIF *comCookie, FailureIsolationBase *fdirInstance) + : DeviceHandlerBase(objectId, deviceCommunication, comCookie, fdirInstance) { + mode = MODE_ON; } -void DeviceHandlerMock::doStartup() { - setMode(_MODE_TO_ON); -} +DeviceHandlerMock::~DeviceHandlerMock() {} -void DeviceHandlerMock::doShutdown() { - setMode(_MODE_POWER_DOWN); -} +void DeviceHandlerMock::doStartUp() { setMode(_MODE_TO_ON); } + +void DeviceHandlerMock::doShutDown() { setMode(_MODE_POWER_DOWN); } ReturnValue_t DeviceHandlerMock::buildNormalDeviceCommand(DeviceCommandId_t *id) { return NOTHING_TO_SEND; @@ -25,22 +25,32 @@ ReturnValue_t DeviceHandlerMock::buildTransitionDeviceCommand(DeviceCommandId_t ReturnValue_t DeviceHandlerMock::buildCommandFromCommand(DeviceCommandId_t deviceCommand, const uint8_t *commandData, size_t commandDataLen) { - switch(deviceCommand) { - case PERIODIC_REPLY_TEST_COMMAND: { - commandBuffer[0] = periodicReplyTestData; + switch (deviceCommand) { + case SIMPLE_COMMAND: { + commandBuffer[0] = SIMPLE_COMMAND_DATA; rawPacket = commandBuffer; - rawPacketLen = sizeof(periodicReplyTestData); + rawPacketLen = sizeof(SIMPLE_COMMAND_DATA); + break; } default: WARN("DeviceHandlerMock::buildCommandFromCommand: Invalid device command"); + break; } return RETURN_OK; } -ReturnValue_t DeviceHandlerMock::scanForReply(const uint8_t *start, size_t len, DeviceCommandId_t *foundId, - size_t *foundLen) { - switch(*start) { - case periodicReplyTestData: { +ReturnValue_t DeviceHandlerMock::scanForReply(const uint8_t *start, size_t len, + DeviceCommandId_t *foundId, size_t *foundLen) { + switch (*start) { + case SIMPLE_COMMAND_DATA: { + *foundId = SIMPLE_COMMAND; + *foundLen = sizeof(SIMPLE_COMMAND_DATA); + return RETURN_OK; + break; + } + case PERIODIC_REPLY_DATA: { + *foundId = PERIODIC_REPLY; + *foundLen = sizeof(PERIODIC_REPLY_DATA); return RETURN_OK; break; } @@ -51,20 +61,35 @@ ReturnValue_t DeviceHandlerMock::scanForReply(const uint8_t *start, size_t len, } ReturnValue_t DeviceHandlerMock::interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) { - switch(id){ - case PERIODIC_REPLY_TEST_COMMAND: + switch (id) { + case SIMPLE_COMMAND: + case PERIODIC_REPLY: { + periodicReplyReceived = true; break; + } default: break; } + return RETURN_OK; } void DeviceHandlerMock::fillCommandAndReplyMap() { - insertInCommandAndReplyMap(PERIODIC_REPLY_TEST_COMMAND, 2, nullptr, 0, true, false, 0, - periodicReplyCountdown); + insertInCommandAndReplyMap(SIMPLE_COMMAND, 0, nullptr, 0, false, false, 0, + &simpleCommandReplyTimeout); + insertInCommandAndReplyMap(PERIODIC_REPLY, 0, nullptr, 0, true, false, 0, + &periodicReplyCountdown); } -uint32_t DeviceHandlerMock::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) { - return 500; +uint32_t DeviceHandlerMock::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) { return 500; } + +void DeviceHandlerMock::changePeriodicReplyCountdown(uint32_t timeout) { + periodicReplyCountdown.setTimeout(0); } +void DeviceHandlerMock::resetPeriodicReplyState() { + periodicReplyReceived = false; +} + +bool DeviceHandlerMock::getPeriodicReplyReceived() { + return periodicReplyReceived; +} diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h index 3e358d655..ac79d926d 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h @@ -3,34 +3,44 @@ #include -class DeviceHandlerMock : public DeviceHandlerBase { - public: +class DeviceHandlerMock: public DeviceHandlerBase { +public: - static const DeviceCommandId_t PERIODIC_REPLY_TEST_COMMAND = 1; + static const DeviceCommandId_t SIMPLE_COMMAND = 1; + static const DeviceCommandId_t PERIODIC_REPLY = 2; - DeviceHandlerMock(object_id_t objectId, object_id_t deviceCommunication); - virtual ~DeviceHandlerMock(); + static const uint8_t SIMPLE_COMMAND_DATA = 1; + static const uint8_t PERIODIC_REPLY_DATA = 2; + + DeviceHandlerMock(object_id_t objectId, object_id_t deviceCommunication, + CookieIF *comCookie, FailureIsolationBase* fdirInstance); + virtual ~DeviceHandlerMock(); + void changePeriodicReplyCountdown(uint32_t timeout); + void resetPeriodicReplyState(); + bool getPeriodicReplyReceived(); protected: - void doStartUp() override; - void doStartShutdown() override; - ReturnValue_t buildNormalDeviceCommand(DeviceCommandId_t *id) override; - ReturnValue_t buildTransitionDeviceCommand(DeviceCommandId_t *id) override; - ReturnValue_t buildCommandFromCommand(DeviceCommandId_t deviceCommand, const uint8_t *commandData, - size_t commandDataLen) override; - ReturnValue_t scanForReply(const uint8_t *start, size_t len, DeviceCommandId_t *foundId, - size_t *foundLen) override; - ReturnValue_t interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) override; - void fillCommandAndReplyMap() override; - uint32_t getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) override; + void doStartUp() override; + void doShutDown() override; + ReturnValue_t buildNormalDeviceCommand(DeviceCommandId_t *id) override; + ReturnValue_t buildTransitionDeviceCommand(DeviceCommandId_t *id) override; + ReturnValue_t buildCommandFromCommand(DeviceCommandId_t deviceCommand, + const uint8_t *commandData, size_t commandDataLen) override; + ReturnValue_t scanForReply(const uint8_t *start, size_t len, + DeviceCommandId_t *foundId, size_t *foundLen) override; + ReturnValue_t interpretDeviceReply(DeviceCommandId_t id, + const uint8_t *packet) override; + void fillCommandAndReplyMap() override; + uint32_t getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) override; private: - uint8_t periodicReplyTestData = 1; + Countdown simpleCommandReplyTimeout = Countdown(50); + Countdown periodicReplyCountdown = Countdown(10); - Countdown periodicReplyCountdown = Countdown(10); + uint8_t commandBuffer[1]; - uint8_t commandBuffer[1]; + bool periodicReplyReceived = false; }; #endif /* TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_DEVICEHANDLERMOCK_H_ */ diff --git a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp index b32917829..1271eb49f 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp @@ -1,36 +1,61 @@ -#include "TestDeviceHandlerBase.h" - #include -#include "ComIFMock.h" -#include "fsfw_tests/unit/devicehandler/DeviceHandlerMock.h" -#include "fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h" +#include "ComIFMock.h" +#include "DeviceFdirMock.h" #include "fsfw_tests/unit/devicehandler/CookieIFMock.h" +#include "fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h" +#include "fsfw_tests/unit/devicehandler/DeviceHandlerMock.h" #include "fsfw_tests/unit/testcfg/objects/systemObjectList.h" TEST_CASE("Device Handler Base", "[DeviceHandlerBase]") { - SECTION("Periodic reply with countdown based timeout success") { - CookieIFMock cookieIFMock; - ComIFperiodicMock comIFperiodic(objects::COM_IF_MOCK); - DeviceHandlerMock deviceHandlerMock(objects::DEVICE_HANDLER_MOCK, objects::COM_IF_MOCK, - &cookieIFMock); - ReturnValue_t result = deviceHandlerMock.initialize(); - REQUIRE(result == RETURN_OK); - DeviceHandlerCommander deviceHandlerCommander(objects::DEVICE_HANDLER_COMMANDER); - result = deviceHandlerCommander.initialize(); - REQUIRE(result == RETURN_OK); + // Will be deleted with DHB destructor + CookieIFMock* cookieIFMock = new CookieIFMock; + ComIFMock comIF(objects::COM_IF_MOCK); + DeviceFdirMock deviceFdirMock(objects::DEVICE_HANDLER_MOCK, objects::NO_OBJECT); + DeviceHandlerMock deviceHandlerMock(objects::DEVICE_HANDLER_MOCK, objects::COM_IF_MOCK, + cookieIFMock, &deviceFdirMock); + ReturnValue_t result = deviceHandlerMock.initialize(); + REQUIRE(result == HasReturnvaluesIF::RETURN_OK); + DeviceHandlerCommander deviceHandlerCommander(objects::DEVICE_HANDLER_COMMANDER); + result = deviceHandlerCommander.initialize(); + REQUIRE(result == HasReturnvaluesIF::RETURN_OK); + + SECTION("Commanding") { + comIF.setTestCase(ComIFMock::TestCase::SIMPLE_COMMAND_NOMINAL); result = deviceHandlerCommander.sendCommand(objects::DEVICE_HANDLER_MOCK, - DeviceHandlerMock::PERIODIC_REPLY_TEST_COMMAND); + DeviceHandlerMock::SIMPLE_COMMAND); + REQUIRE(result == HasReturnvaluesIF::RETURN_OK); deviceHandlerMock.performOperation(DeviceHandlerIF::PERFORM_OPERATION); deviceHandlerMock.performOperation(DeviceHandlerIF::SEND_WRITE); deviceHandlerMock.performOperation(DeviceHandlerIF::GET_WRITE); deviceHandlerMock.performOperation(DeviceHandlerIF::SEND_READ); deviceHandlerMock.performOperation(DeviceHandlerIF::GET_READ); + deviceHandlerCommander.performOperation(); result = deviceHandlerCommander.getReplyReturnCode(); - REQUIRE(result == RETURN_OK); + REQUIRE(result == HasReturnvaluesIF::RETURN_OK); + } + + SECTION("Periodic reply") { + comIF.setTestCase(ComIFMock::TestCase::PERIODIC_REPLY_NOMINAL); + deviceHandlerMock.performOperation(DeviceHandlerIF::PERFORM_OPERATION); + deviceHandlerMock.performOperation(DeviceHandlerIF::SEND_WRITE); + deviceHandlerMock.performOperation(DeviceHandlerIF::GET_WRITE); + deviceHandlerMock.performOperation(DeviceHandlerIF::SEND_READ); + deviceHandlerMock.performOperation(DeviceHandlerIF::GET_READ); + REQUIRE(deviceHandlerMock.getPeriodicReplyReceived() == true); + } + + SECTION("Missed periodic reply") { + comIF.setTestCase(ComIFMock::TestCase::MISSED_REPLY); + // Set the timeout to 0 to immediately timeout the reply + deviceHandlerMock.changePeriodicReplyCountdown(0); + deviceHandlerMock.performOperation(DeviceHandlerIF::PERFORM_OPERATION); + deviceHandlerMock.performOperation(DeviceHandlerIF::SEND_WRITE); + deviceHandlerMock.performOperation(DeviceHandlerIF::GET_WRITE); + deviceHandlerMock.performOperation(DeviceHandlerIF::SEND_READ); + deviceHandlerMock.performOperation(DeviceHandlerIF::GET_READ); + uint32_t missedReplies = deviceFdirMock.getMissedReplyCount(); + REQUIRE(missedReplies == 1); } -// SECTION("Periodic reply with countdown based timeout failed") { -// -// } } diff --git a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.h b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.h deleted file mode 100644 index ed35c34aa..000000000 --- a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef UNITTEST_HOSTED_TESTACTIONHELPER_H_ -#define UNITTEST_HOSTED_TESTACTIONHELPER_H_ - -#include -#include - -#include - -#include "fsfw_tests/unit/CatchDefinitions.h" - -class ActionHelperOwnerMockBase : public HasActionsIF { - public: - bool getCommandQueueCalled = false; - bool executeActionCalled = false; - static const size_t MAX_SIZE = 3; - uint8_t buffer[MAX_SIZE] = {0, 0, 0}; - size_t size = 0; - - MessageQueueId_t getCommandQueue() const override { return tconst::testQueueId; } - - ReturnValue_t executeAction(ActionId_t actionId, MessageQueueId_t commandedBy, - const uint8_t* data, size_t size) override { - executeActionCalled = true; - if (size > MAX_SIZE) { - return 0xAFFE; - } - this->size = size; - memcpy(buffer, data, size); - return HasReturnvaluesIF::RETURN_OK; - } - - void clearBuffer() { - this->size = 0; - for (size_t i = 0; i < MAX_SIZE; i++) { - buffer[i] = 0; - } - } - - void getBuffer(const uint8_t** ptr, size_t* size) { - if (size != nullptr) { - *size = this->size; - } - if (ptr != nullptr) { - *ptr = buffer; - } - } -}; - -#endif /* UNITTEST_TESTFW_NEWTESTS_TESTACTIONHELPER_H_ */