From 951c077abce4e88d4c2bf48502de341d751148e1 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Tue, 26 Apr 2022 10:03:04 +0200 Subject: [PATCH 01/15] option to use Countdown object to time out replies --- src/fsfw/devicehandlers/DeviceHandlerBase.cpp | 85 ++++++++++++++----- src/fsfw/devicehandlers/DeviceHandlerBase.h | 27 +++++- 2 files changed, 89 insertions(+), 23 deletions(-) diff --git a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp index 0e2802ac..fb9a597c 100644 --- a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp +++ b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp @@ -233,17 +233,28 @@ ReturnValue_t DeviceHandlerBase::initialize() { } void DeviceHandlerBase::decrementDeviceReplyMap() { + bool timedOut = false; for (std::pair& replyPair : deviceReplyMap) { - if (replyPair.second.delayCycles != 0) { + if (replyPair.second.countdown != nullptr && replyPair.second.active) { + if (replyPair.second.countdown->hasTimedOut()) { + timedOut = true; + } + } + if (replyPair.second.delayCycles != 0 && replyPair.second.countdown == nullptr) { replyPair.second.delayCycles--; if (replyPair.second.delayCycles == 0) { if (replyPair.second.periodic) { replyPair.second.delayCycles = replyPair.second.maxDelayCycles; } - replyToReply(replyPair.first, replyPair.second, TIMEOUT); - missedReply(replyPair.first); + timedOut = true; } } + if (timedOut) { + replyToReply(replyPair.first, replyPair.second, TIMEOUT); + missedReply(replyPair.first); + timedOut = false; + replyPair.second.active = false; + } } } @@ -359,7 +370,6 @@ void DeviceHandlerBase::doStateMachine() { setMode(MODE_OFF); break; } - if (currentUptime - timeoutStart >= powerSwitcher->getSwitchDelayMs()) { triggerEvent(MODE_TRANSITION_FAILED, PowerSwitchIF::SWITCH_TIMEOUT, 0); setMode(MODE_ERROR_ON); @@ -408,20 +418,22 @@ ReturnValue_t DeviceHandlerBase::isModeCombinationValid(Mode_t mode, Submode_t s ReturnValue_t DeviceHandlerBase::insertInCommandAndReplyMap( DeviceCommandId_t deviceCommand, uint16_t maxDelayCycles, LocalPoolDataSetBase* replyDataSet, - size_t replyLen, bool periodic, bool hasDifferentReplyId, DeviceCommandId_t replyId) { + size_t replyLen, bool periodic, bool hasDifferentReplyId, DeviceCommandId_t replyId, + Countdown* countdown) { // No need to check, as we may try to insert multiple times. insertInCommandMap(deviceCommand, hasDifferentReplyId, replyId); if (hasDifferentReplyId) { - return insertInReplyMap(replyId, maxDelayCycles, replyDataSet, replyLen, periodic); + return insertInReplyMap(replyId, maxDelayCycles, replyDataSet, replyLen, periodic, countdown); } else { - return insertInReplyMap(deviceCommand, maxDelayCycles, replyDataSet, replyLen, periodic); + return insertInReplyMap(deviceCommand, maxDelayCycles, replyDataSet, replyLen, periodic, + countdown); } } ReturnValue_t DeviceHandlerBase::insertInReplyMap(DeviceCommandId_t replyId, uint16_t maxDelayCycles, LocalPoolDataSetBase* dataSet, size_t replyLen, - bool periodic) { + bool periodic, Countdown* countdown) { DeviceReplyInfo info; info.maxDelayCycles = maxDelayCycles; info.periodic = periodic; @@ -429,6 +441,10 @@ ReturnValue_t DeviceHandlerBase::insertInReplyMap(DeviceCommandId_t replyId, info.replyLen = replyLen; info.dataSet = dataSet; info.command = deviceCommandMap.end(); + info.countdown = countdown; + if (info.periodic) { + info.active = true; + } auto resultPair = deviceReplyMap.emplace(replyId, info); if (resultPair.second) { return RETURN_OK; @@ -458,16 +474,16 @@ size_t DeviceHandlerBase::getNextReplyLength(DeviceCommandId_t commandId) { DeviceCommandId_t replyId = NO_COMMAND_ID; DeviceCommandMap::iterator command = cookieInfo.pendingCommand; if (command->second.useAlternativeReplyId) { - replyId = command->second.alternativeReplyId; - } - else { - replyId = commandId; + replyId = command->second.alternativeReplyId; + } else { + replyId = commandId; } DeviceReplyIter iter = deviceReplyMap.find(replyId); if (iter != deviceReplyMap.end()) { - if (iter->second.delayCycles != 0) { - return iter->second.replyLen; - } + if ((iter->second.delayCycles != 0 && iter->second.countdown == nullptr) || + (iter->second.active && iter->second.countdown != nullptr)) { + return iter->second.replyLen; + } } return 0; } @@ -809,17 +825,18 @@ void DeviceHandlerBase::handleReply(const uint8_t* receivedData, DeviceCommandId DeviceReplyInfo* info = &(iter->second); - if (info->delayCycles != 0) { + if ((info->delayCycles != 0 && info->countdown == nullptr) || + (info->active && info->countdown != nullptr)) { result = interpretDeviceReply(foundId, receivedData); if (result == IGNORE_REPLY_DATA) { return; } - if (info->periodic) { - info->delayCycles = info->maxDelayCycles; - } else { - info->delayCycles = 0; + if (info->active && info->countdown != nullptr) { + disableTimeoutControlledReply(info); + } else if (info->delayCycles != 0) { + disableDelayCyclesControlledReply(info); } if (result != RETURN_OK) { @@ -838,6 +855,24 @@ void DeviceHandlerBase::handleReply(const uint8_t* receivedData, DeviceCommandId } } +void DeviceHandlerBase::disableTimeoutControlledReply(DeviceReplyInfo* info) { + if (info->periodic) { + info->countdown->resetTimer(); + } else { + info->active = false; + info->countdown->timeOut(); + } +} + +void DeviceHandlerBase::disableDelayCyclesControlledReply(DeviceReplyInfo* info) { + if (info->periodic) { + info->delayCycles = info->maxDelayCycles; + } else { + info->delayCycles = 0; + info->active = false; + } +} + ReturnValue_t DeviceHandlerBase::getStorageData(store_address_t storageAddress, uint8_t** data, size_t* len) { size_t lenTmp; @@ -963,6 +998,10 @@ ReturnValue_t DeviceHandlerBase::enableReplyInReplyMap(DeviceCommandMap::iterato info->delayCycles = info->maxDelayCycles; info->command = command; command->second.expectedReplies = expectedReplies; + if (info->countdown != nullptr) { + info->countdown->resetTimer(); + } + info->active = true; return RETURN_OK; } else { return NO_REPLY_EXPECTED; @@ -1197,7 +1236,8 @@ void DeviceHandlerBase::setParentQueue(MessageQueueId_t parentQueueId) { bool DeviceHandlerBase::isAwaitingReply() { std::map::iterator iter; for (iter = deviceReplyMap.begin(); iter != deviceReplyMap.end(); ++iter) { - if (iter->second.delayCycles != 0) { + if ((iter->second.delayCycles != 0 && iter->second.countdown == nullptr) || + (iter->second.active && iter->second.countdown != nullptr)) { return true; } } @@ -1353,6 +1393,9 @@ uint8_t DeviceHandlerBase::getReplyDelayCycles(DeviceCommandId_t deviceCommand) if (iter == deviceReplyMap.end()) { return 0; } + else if (iter->second.countdown != nullptr) { + return 0; + } return iter->second.delayCycles; } diff --git a/src/fsfw/devicehandlers/DeviceHandlerBase.h b/src/fsfw/devicehandlers/DeviceHandlerBase.h index 5e974831..4a9a8aa2 100644 --- a/src/fsfw/devicehandlers/DeviceHandlerBase.h +++ b/src/fsfw/devicehandlers/DeviceHandlerBase.h @@ -448,6 +448,9 @@ class DeviceHandlerBase : public DeviceHandlerIF, * by the device repeatedly without request) or not. Default is aperiodic (0). * Please note that periodic replies are disabled by default. You can enable them with * #updatePeriodicReply + * @param countdown Instead of using maxDelayCycles to timeout a device reply it is also possible + * to provide a pointer to a Countdown object which will signal the timeout + * when expired * @return - @c RETURN_OK when the command was successfully inserted, * - @c RETURN_FAILED else. */ @@ -455,7 +458,8 @@ class DeviceHandlerBase : public DeviceHandlerIF, LocalPoolDataSetBase *replyDataSet = nullptr, size_t replyLen = 0, bool periodic = false, bool hasDifferentReplyId = false, - DeviceCommandId_t replyId = 0); + DeviceCommandId_t replyId = 0, + Countdown *countdown = nullptr); /** * @brief This is a helper method to insert replies in the reply map. * @param deviceCommand Identifier of the reply to add. @@ -465,12 +469,15 @@ class DeviceHandlerBase : public DeviceHandlerIF, * by the device repeatedly without request) or not. Default is aperiodic (0). * Please note that periodic replies are disabled by default. You can enable them with * #updatePeriodicReply + * @param countdown Instead of using maxDelayCycles to timeout a device reply it is also possible + * to provide a pointer to a Countdown object which will signal the timeout + * when expired * @return - @c RETURN_OK when the command was successfully inserted, * - @c RETURN_FAILED else. */ ReturnValue_t insertInReplyMap(DeviceCommandId_t deviceCommand, uint16_t maxDelayCycles, LocalPoolDataSetBase *dataSet = nullptr, size_t replyLen = 0, - bool periodic = false); + bool periodic = false, Countdown *countdown = nullptr); /** * @brief A simple command to add a command to the commandList. @@ -783,6 +790,11 @@ class DeviceHandlerBase : public DeviceHandlerIF, LocalPoolDataSetBase *dataSet = nullptr; //! The command that expects this reply. DeviceCommandMap::iterator command; + //! Instead of using delayCycles to specify the maximum time to wait for the device reply, it + //! is also possible specify a countdown + Countdown* countdown = nullptr; + //! will be set to true when reply is enabled + bool active = false; }; using DeviceReplyMap = std::map; @@ -1243,6 +1255,17 @@ class DeviceHandlerBase : public DeviceHandlerIF, */ void doGetRead(void); + /** + * @brief Handles disabling of replies which use a timeout to detect missed replies. + */ + void disableTimeoutControlledReply(DeviceReplyInfo* info); + + /** + * @brief Handles disabling of replies which use a number of maximum delay cycles to detect + * missed replies. + */ + void disableDelayCyclesControlledReply(DeviceReplyInfo* info); + /** * Retrive data from the #IPCStore. * From 0aee86442eafa6d8827435e2c2e22074ba733e34 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Tue, 26 Apr 2022 11:48:18 +0200 Subject: [PATCH 02/15] typo in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 99c842af..5112230a 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ You can also use `-DFSFW_OSAL=linux` on Linux systems. Coverage data in HTML format can be generated using the `CodeCoverage` [CMake module](https://github.com/bilke/cmake-modules/tree/master). -To build the unittests, run them and then generare the coverage data in this format, +To build the unittests, run them and then generate the coverage data in this format, the following command can be used inside the build directory after the build system was set up ```sh From 1611a4e1f0c013d9fa632edc858a14cb7509c32f Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Mon, 16 May 2022 11:10:35 +0200 Subject: [PATCH 03/15] device handler unittest wip --- scripts/helper.py | 27 +++++-- .../unit/devicehandler/ComIFMock.cpp | 24 +++++++ .../fsfw_tests/unit/devicehandler/ComIFMock.h | 27 +++++++ .../unit/devicehandler/CookieIFMock.cpp | 7 ++ .../unit/devicehandler/CookieIFMock.h | 12 ++++ .../devicehandler/DeviceHandlerCommander.cpp | 70 +++++++++++++++++++ .../devicehandler/DeviceHandlerCommander.h | 49 +++++++++++++ .../unit/devicehandler/DeviceHandlerMock.cpp | 70 +++++++++++++++++++ .../unit/devicehandler/DeviceHandlerMock.h | 36 ++++++++++ .../devicehandler/TestDeviceHandlerBase.cpp | 36 ++++++++++ .../devicehandler/TestDeviceHandlerBase.h | 49 +++++++++++++ .../unit/testcfg/objects/systemObjectList.h | 61 ++++++++-------- 12 files changed, 435 insertions(+), 33 deletions(-) create mode 100644 tests/src/fsfw_tests/unit/devicehandler/ComIFMock.cpp create mode 100644 tests/src/fsfw_tests/unit/devicehandler/ComIFMock.h create mode 100644 tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.cpp create mode 100644 tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.h create mode 100644 tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.cpp create mode 100644 tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h create mode 100644 tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp create mode 100644 tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h create mode 100644 tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp create mode 100644 tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.h diff --git a/scripts/helper.py b/scripts/helper.py index 4dff908d..56cf352b 100755 --- a/scripts/helper.py +++ b/scripts/helper.py @@ -48,6 +48,20 @@ def main(): action="store_true", help="Run valgrind on generated test binary", ) + parser.add_argument( + "-g", + "--generators", + default = "Ninja", + action="store", + help="CMake generators", + ) + parser.add_argument( + "-w", + "--windows", + default=False, + action="store_true", + help="Run on windows", + ) args = parser.parse_args() if args.all: @@ -115,14 +129,14 @@ def handle_tests_type(args, build_dir_list: list): if args.create: if os.path.exists(UNITTEST_FOLDER_NAME): shutil.rmtree(UNITTEST_FOLDER_NAME) - create_tests_build_cfg() + create_tests_build_cfg(args) build_directory = UNITTEST_FOLDER_NAME elif len(build_dir_list) == 0: print( "No valid CMake tests build directory found. " "Trying to set up test build system" ) - create_tests_build_cfg() + create_tests_build_cfg(args) build_directory = UNITTEST_FOLDER_NAME elif len(build_dir_list) == 1: build_directory = build_dir_list[0] @@ -147,10 +161,15 @@ def handle_tests_type(args, build_dir_list: list): os.chdir("..") -def create_tests_build_cfg(): +def create_tests_build_cfg(args): os.mkdir(UNITTEST_FOLDER_NAME) os.chdir(UNITTEST_FOLDER_NAME) - cmd_runner("cmake -DFSFW_OSAL=host -DFSFW_BUILD_UNITTESTS=ON ..") + if args.windows: + cmake_cmd = 'cmake -G "' + args.generators + '" -DFSFW_OSAL=host -DFSFW_BUILD_UNITTESTS=ON \ + -DGCOVR_PATH="py -m gcovr" ..' + else: + cmake_cmd = 'cmake -G "' + args.generators + '" -DFSFW_OSAL=host -DFSFW_BUILD_UNITTESTS=ON ..' + cmd_runner(cmake_cmd) os.chdir("..") diff --git a/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.cpp b/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.cpp new file mode 100644 index 00000000..6bcf5e20 --- /dev/null +++ b/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.cpp @@ -0,0 +1,24 @@ +#include "ComIFMock.h" + +ComIFMock::ComIFMock(obejct_id_t 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; + return RETURN_OK; +} + +ReturnValue_t ComIFMock::getSendSuccess(CookieIF *cookie) { return RETURN_OK; } + +ReturnValue_t ComIFMock::requestReceiveMessage(CookieIF *cookie, size_t requestLen) { + return RETURN_OK; +} + +ReturnValue_t ComIFMock::readReceivedMessage(CookieIF *cookie, uint8_t **buffer, size_t *size) { + *size = sizeof(rememberSentByte); + *buffer = &rememberSentByte; + return RETURN_OK; +} diff --git a/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.h b/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.h new file mode 100644 index 00000000..fde6be45 --- /dev/null +++ b/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.h @@ -0,0 +1,27 @@ +#ifndef TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_COMIFMOCK_H_ +#define TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_COMIFMOCK_H_ + +#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. + */ +class ComIFMock : public DeviceCommunicationIF, public SystemObject { + public: + ComIFMock(obejct_id_t objectId); + virtual ~ComIFMock(); + + virtual ReturnValue_t initializeInterface(CookieIF *cookie) override; + virtual ReturnValue_t sendMessage(CookieIF *cookie, const uint8_t *sendData, + size_t sendLen) override; + virtual ReturnValue_t getSendSuccess(CookieIF *cookie) override; + virtual ReturnValue_t requestReceiveMessage(CookieIF *cookie, size_t requestLen) override; + virtual ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer, + size_t *size) override; + + private: + uint8_t rememberSentByte = 0; +}; + +#endif /* TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_COMIFMOCK_H_ */ diff --git a/tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.cpp b/tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.cpp new file mode 100644 index 00000000..f73c1731 --- /dev/null +++ b/tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.cpp @@ -0,0 +1,7 @@ +#include "CookieIFMock.h" + +CookieIFMock::CookieIFMock() { +} + +CookieIFMock::~CookieIFMock() { +} diff --git a/tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.h b/tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.h new file mode 100644 index 00000000..ad40f55f --- /dev/null +++ b/tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.h @@ -0,0 +1,12 @@ +#ifndef TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_COOKIEIFMOCK_H_ +#define TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_COOKIEIFMOCK_H_ + +#include "fsfw/devicehandlers/CookieIF.h" + +class CookieIFMock { + public: + CookieIFMock(); + virtual ~CookieIFMock(); +}; + +#endif /* TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_COOKIEIFMOCK_H_ */ diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.cpp b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.cpp new file mode 100644 index 00000000..4da5f36c --- /dev/null +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.cpp @@ -0,0 +1,70 @@ +#include "DeviceHandlerCommander.h" + +DeviceHandlerCommander::DeviceHandlerCommander(object_id_t objectId) + : SystemObject(objectId), commandActionHelper(this) { + auto mqArgs = MqArgs(this->getObjectId()); + commandQueue = QueueFactory::instance()->createMessageQueue( + QUEUE_SIZE, MessageQueueMessage::MAX_MESSAGE_SIZE, &mqArgs); +} + +DeviceHandlerCommander::~DeviceHandlerCommander() {} + +ReturnValue_t DeviceHandlerCommander::performOperation(uint8_t operationCode) { + readCommandQueue(); +} + +ReturnValue_t DeviceHandlerCommander::initialize() { + ReturnValue_t result = commandActionHelper.initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = actionHelper.initialize(commandQueue); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return HasReturnvaluesIF::RETURN_OK; +} + +MessageQueueIF* DeviceHandlerCommander::getCommandQueuePtr() { return commandQueue; } + +void DeviceHandlerCommander::stepSuccessfulReceived(ActionId_t actionId, uint8_t step) {} + +void DeviceHandlerCommander::stepFailedReceived(ActionId_t actionId, uint8_t step, + ReturnValue_t returnCode) {} + +void DeviceHandlerCommander::dataReceived(ActionId_t actionId, const uint8_t* data, uint32_t size) { +} + +void DeviceHandlerCommander::completionSuccessfulReceived(ActionId_t actionId) { + lastReplyReturnCode = RETURN_OK; +} + +void DeviceHandlerCommander::completionFailedReceived(ActionId_t actionId, + ReturnValue_t returnCode) { + lastReplyReturnCode = returnCode; +} + +void DeviceHandlerCommander::readCommandQueue() { + CommandMessage message; + 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; + } + } +} + +ReturnValue_t DeviceHandlerCommander::sendCommand(object_id_t target, ActionId_t actionId) { + return commandActionHelper.commandAction(target, actionId, nullptr, 0); +} + +ReturnValue_t DeviceHandlerCommander::getReplyReturnCode() { return lastReplyReturnCode; } + +void DeviceHandlerCommander::resetReplyReturnCode() { + lastReplyReturnCode = RETURN_FAILED; +} diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h new file mode 100644 index 00000000..587932e0 --- /dev/null +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h @@ -0,0 +1,49 @@ +#ifndef TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_DEVICEHANDLERCOMMANDER_H_ +#define TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_DEVICEHANDLERCOMMANDER_H_ + +#include "fsfw/objectmanager/SystemObject.h" +#include "fsfw/action/CommandActionHelper.h" +#include "fsfw/action/CommandsActionsIF.h" +#include "fsfw/tasks/ExecutableObjectIF.h" +#include "fsfw/returnvalues/HasReturnvaluesIF.h" + +class DeviceHandlerCommander : public ExecutableObjectIF, + public SystemObject, + public CommandsActionsIF, + public HasReturnvaluesIF { + public: + DeviceHandlerCommander(object_id_t objectId); + virtual ~DeviceHandlerCommander(); + + ReturnValue_t performOperation(uint8_t operationCode = 0); + ReturnValue_t initialize() override; + MessageQueueIF* getCommandQueuePtr() override; + void stepSuccessfulReceived(ActionId_t actionId, uint8_t step) override; + void stepFailedReceived(ActionId_t actionId, uint8_t step, ReturnValue_t returnCode) override; + void dataReceived(ActionId_t actionId, const uint8_t* data, uint32_t size) override; + void completionSuccessfulReceived(ActionId_t actionId) override; + void completionFailedReceived(ActionId_t actionId, ReturnValue_t returnCode) override; + + /** + * @brief Calling this function will send the command to the device handler object. + * + * @param target Object ID of the device handler + * @param actionId Action ID of the command to send + */ + ReturnValue_t sendCommand(object_id_t target, ActionId_t actionId); + + ReturnValue_t getReplyReturnCode(); + void resetReplyReturnCode(); + + private: + + MessageQueueIF* commandQueue = nullptr; + + CommandActionHelper commandActionHelper; + + ReturnValue_t lastReplyReturnCode = RETURN_FAILED; + + void readCommandQueue(); +}; + +#endif /* TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_DEVICEHANDLERCOMMANDER_H_ */ diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp new file mode 100644 index 00000000..ccdabc40 --- /dev/null +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp @@ -0,0 +1,70 @@ +#include "DeviceHandlerMock.h" + +DeviceHandlerMock::DeviceHandlerMock(object_id_t objectId, object_id_t deviceCommunication) + : DeviceHandlerBase(objetcId, deviceCommunication, nullptr) {} + +DeviceHandlerMock::~DeviceHandlerMock() { +} + +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; +} + +ReturnValue_t DeviceHandlerMock::buildTransitionDeviceCommand(DeviceCommandId_t *id) { + return NOTHING_TO_SEND; +} + +ReturnValue_t DeviceHandlerMock::buildCommandFromCommand(DeviceCommandId_t deviceCommand, + const uint8_t *commandData, + size_t commandDataLen) { + switch(deviceCommand) { + case PERIODIC_REPLY_TEST_COMMAND: { + commandBuffer[0] = periodicReplyTestData; + rawPacket = commandBuffer; + rawPacketLen = sizeof(periodicReplyTestData); + } + default: + WARN("DeviceHandlerMock::buildCommandFromCommand: Invalid device command"); + } + return RETURN_OK; +} + +ReturnValue_t DeviceHandlerMock::scanForReply(const uint8_t *start, size_t len, DeviceCommandId_t *foundId, + size_t *foundLen) { + switch(*start) { + case periodicReplyTestData: { + return RETURN_OK; + break; + } + default: + break; + } + return RETURN_FAILED; +} + +ReturnValue_t DeviceHandlerMock::interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) { + switch(id){ + case PERIODIC_REPLY_TEST_COMMAND: + break; + default: + break; + } +} + +void DeviceHandlerMock::fillCommandAndReplyMap() { + insertInCommandAndReplyMap(PERIODIC_REPLY_TEST_COMMAND, 2, nullptr, 0, true, false, 0, + periodicReplyCountdown); +} + +uint32_t DeviceHandlerMock::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) { + return 500; +} + diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h new file mode 100644 index 00000000..3e358d65 --- /dev/null +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h @@ -0,0 +1,36 @@ +#ifndef TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_DEVICEHANDLERMOCK_H_ +#define TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_DEVICEHANDLERMOCK_H_ + +#include + +class DeviceHandlerMock : public DeviceHandlerBase { + public: + + static const DeviceCommandId_t PERIODIC_REPLY_TEST_COMMAND = 1; + + DeviceHandlerMock(object_id_t objectId, object_id_t deviceCommunication); + virtual ~DeviceHandlerMock(); + +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; + +private: + + uint8_t periodicReplyTestData = 1; + + Countdown periodicReplyCountdown = Countdown(10); + + uint8_t commandBuffer[1]; +}; + +#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 new file mode 100644 index 00000000..b3291782 --- /dev/null +++ b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp @@ -0,0 +1,36 @@ +#include "TestDeviceHandlerBase.h" + +#include +#include "ComIFMock.h" + +#include "fsfw_tests/unit/devicehandler/DeviceHandlerMock.h" +#include "fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h" +#include "fsfw_tests/unit/devicehandler/CookieIFMock.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); + result = deviceHandlerCommander.sendCommand(objects::DEVICE_HANDLER_MOCK, + DeviceHandlerMock::PERIODIC_REPLY_TEST_COMMAND); + 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); + result = deviceHandlerCommander.getReplyReturnCode(); + REQUIRE(result == RETURN_OK); + } +// 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 new file mode 100644 index 00000000..ed35c34a --- /dev/null +++ b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.h @@ -0,0 +1,49 @@ +#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_ */ diff --git a/tests/src/fsfw_tests/unit/testcfg/objects/systemObjectList.h b/tests/src/fsfw_tests/unit/testcfg/objects/systemObjectList.h index 3eba1484..29450309 100644 --- a/tests/src/fsfw_tests/unit/testcfg/objects/systemObjectList.h +++ b/tests/src/fsfw_tests/unit/testcfg/objects/systemObjectList.h @@ -1,29 +1,32 @@ -#ifndef HOSTED_CONFIG_OBJECTS_SYSTEMOBJECTLIST_H_ -#define HOSTED_CONFIG_OBJECTS_SYSTEMOBJECTLIST_H_ - -#include - -#include "fsfw/objectmanager/frameworkObjects.h" - -// The objects will be instantiated in the ID order -namespace objects { -enum sourceObjects : uint32_t { - /* All addresses between start and end are reserved for the FSFW */ - FSFW_CONFIG_RESERVED_START = PUS_SERVICE_1_VERIFICATION, - FSFW_CONFIG_RESERVED_END = TM_STORE, - - UDP_BRIDGE = 15, - UDP_POLLING_TASK = 16, - - TEST_ECHO_COM_IF = 20, - TEST_DEVICE = 21, - - HK_RECEIVER_MOCK = 22, - TEST_LOCAL_POOL_OWNER_BASE = 25, - - SHARED_SET_ID = 26 - -}; -} - -#endif /* BSP_CONFIG_OBJECTS_SYSTEMOBJECTLIST_H_ */ +#ifndef HOSTED_CONFIG_OBJECTS_SYSTEMOBJECTLIST_H_ +#define HOSTED_CONFIG_OBJECTS_SYSTEMOBJECTLIST_H_ + +#include + +#include "fsfw/objectmanager/frameworkObjects.h" + +// The objects will be instantiated in the ID order +namespace objects { +enum sourceObjects : uint32_t { + /* All addresses between start and end are reserved for the FSFW */ + FSFW_CONFIG_RESERVED_START = PUS_SERVICE_1_VERIFICATION, + FSFW_CONFIG_RESERVED_END = TM_STORE, + + UDP_BRIDGE = 15, + UDP_POLLING_TASK = 16, + + TEST_ECHO_COM_IF = 20, + TEST_DEVICE = 21, + + HK_RECEIVER_MOCK = 22, + TEST_LOCAL_POOL_OWNER_BASE = 25, + + SHARED_SET_ID = 26, + + DEVICE_HANDLER_MOCK = 27, + COM_IF_MOCK = 28, + DEVICE_HANDLER_COMMANDER = 29, +}; +} + +#endif /* BSP_CONFIG_OBJECTS_SYSTEMOBJECTLIST_H_ */ From bf673c56c69b16a8aeae12b30de1dd90c5588dfc Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Sun, 5 Jun 2022 12:52:55 +0200 Subject: [PATCH 04/15] 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 fb9a597c..a874824d 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 aaa7d6c5..47270d2a 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 f2d642ff..77d657e8 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 98051ba0..893f86f1 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 fedef869..da17d783 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 e87cf613..4515e5b1 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 3b59885c..c5630da9 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 4bef128c..b9884115 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 ab4881f4..ee8fcd94 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 4718abc3..60e00f92 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 50266ac2..aab3f0eb 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 a8d31d88..eb57202a 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 01018dfa..860a9bed 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 6bcf5e20..4dd64c1a 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 fde6be45..7de4937f 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 ad40f55f..5c868932 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 4da5f36c..b3889b2c 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 587932e0..77168043 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 ccdabc40..ca1dff2f 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 3e358d65..ac79d926 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 b3291782..1271eb49 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 ed35c34a..00000000 --- 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_ */ From 2fa4fd61d0ee5fb626e9fb9d70a98a08109d2438 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Sun, 5 Jun 2022 12:54:13 +0200 Subject: [PATCH 05/15] device fdir mock --- .../unit/devicehandler/CMakeLists.txt | 8 ++++++++ .../unit/devicehandler/DeviceFdirMock.cpp | 18 ++++++++++++++++++ .../unit/devicehandler/DeviceFdirMock.h | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 tests/src/fsfw_tests/unit/devicehandler/CMakeLists.txt create mode 100644 tests/src/fsfw_tests/unit/devicehandler/DeviceFdirMock.cpp create mode 100644 tests/src/fsfw_tests/unit/devicehandler/DeviceFdirMock.h diff --git a/tests/src/fsfw_tests/unit/devicehandler/CMakeLists.txt b/tests/src/fsfw_tests/unit/devicehandler/CMakeLists.txt new file mode 100644 index 00000000..7ad5d316 --- /dev/null +++ b/tests/src/fsfw_tests/unit/devicehandler/CMakeLists.txt @@ -0,0 +1,8 @@ +target_sources(${FSFW_TEST_TGT} PRIVATE + CookieIFMock.cpp + ComIFMock.cpp + DeviceHandlerCommander.cpp + DeviceHandlerMock.cpp + DeviceFdirMock.cpp + TestDeviceHandlerBase.cpp +) diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceFdirMock.cpp b/tests/src/fsfw_tests/unit/devicehandler/DeviceFdirMock.cpp new file mode 100644 index 00000000..dfef7cd3 --- /dev/null +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceFdirMock.cpp @@ -0,0 +1,18 @@ +#include "DeviceFdirMock.h" + +#include + +DeviceFdirMock::DeviceFdirMock(object_id_t owner, object_id_t parent) + : DeviceHandlerFailureIsolation(owner, parent) {} + +DeviceFdirMock::~DeviceFdirMock() {} + +uint32_t DeviceFdirMock::getMissedReplyCount() { + ParameterWrapper parameterWrapper; + this->getParameter(MISSED_REPLY_DOMAIN_ID, + static_cast(FaultCounter::ParameterIds::FAULT_COUNT), + ¶meterWrapper, nullptr, 0); + uint32_t missedReplyCount = 0; + parameterWrapper.getElement(&missedReplyCount); + return missedReplyCount; +} diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceFdirMock.h b/tests/src/fsfw_tests/unit/devicehandler/DeviceFdirMock.h new file mode 100644 index 00000000..b314fc98 --- /dev/null +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceFdirMock.h @@ -0,0 +1,18 @@ +#ifndef TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_DEVICEFDIRMOCK_H_ +#define TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_DEVICEFDIRMOCK_H_ + +#include "fsfw/devicehandlers/DeviceHandlerFailureIsolation.h" + +class DeviceFdirMock : public DeviceHandlerFailureIsolation { + public: + DeviceFdirMock(object_id_t owner, object_id_t parent); + virtual ~DeviceFdirMock(); + + uint32_t getMissedReplyCount(); + + private: + static const uint8_t STRANGE_REPLY_DOMAIN_ID = 0xF0; + static const uint8_t MISSED_REPLY_DOMAIN_ID = 0xF1; +}; + +#endif /* TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_DEVICEFDIRMOCK_H_ */ From 4fba2704aa0d834cf96d2db05eee92ca960fdb9f Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Mon, 6 Jun 2022 10:53:08 +0200 Subject: [PATCH 06/15] unit test for detection of missed reply when commanded externally --- .../fsfw_tests/unit/devicehandler/ComIFMock.h | 3 ++- .../unit/devicehandler/DeviceHandlerMock.cpp | 6 ++++- .../unit/devicehandler/DeviceHandlerMock.h | 1 + .../devicehandler/TestDeviceHandlerBase.cpp | 25 +++++++++++++++++-- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.h b/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.h index 7de4937f..1463deb6 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.h +++ b/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.h @@ -6,7 +6,8 @@ /** * @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: diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp index ca1dff2f..88f712db 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp @@ -83,7 +83,11 @@ void DeviceHandlerMock::fillCommandAndReplyMap() { uint32_t DeviceHandlerMock::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) { return 500; } void DeviceHandlerMock::changePeriodicReplyCountdown(uint32_t timeout) { - periodicReplyCountdown.setTimeout(0); + periodicReplyCountdown.setTimeout(timeout); +} + +void DeviceHandlerMock::changeSimpleCommandReplyCountdown(uint32_t timeout) { + simpleCommandReplyTimeout.setTimeout(timeout); } void DeviceHandlerMock::resetPeriodicReplyState() { diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h index ac79d926..4ff0d77d 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h @@ -16,6 +16,7 @@ public: CookieIF *comCookie, FailureIsolationBase* fdirInstance); virtual ~DeviceHandlerMock(); void changePeriodicReplyCountdown(uint32_t timeout); + void changeSimpleCommandReplyCountdown(uint32_t timeout); void resetPeriodicReplyState(); bool getPeriodicReplyReceived(); diff --git a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp index 1271eb49..283a8799 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp @@ -21,7 +21,7 @@ TEST_CASE("Device Handler Base", "[DeviceHandlerBase]") { result = deviceHandlerCommander.initialize(); REQUIRE(result == HasReturnvaluesIF::RETURN_OK); - SECTION("Commanding") { + SECTION("Commanding nominal") { comIF.setTestCase(ComIFMock::TestCase::SIMPLE_COMMAND_NOMINAL); result = deviceHandlerCommander.sendCommand(objects::DEVICE_HANDLER_MOCK, DeviceHandlerMock::SIMPLE_COMMAND); @@ -36,7 +36,28 @@ TEST_CASE("Device Handler Base", "[DeviceHandlerBase]") { REQUIRE(result == HasReturnvaluesIF::RETURN_OK); } - SECTION("Periodic reply") { + SECTION("Commanding missed reply") { + comIF.setTestCase(ComIFMock::TestCase::MISSED_REPLY); + deviceHandlerCommander.resetReplyReturnCode(); + // Set the timeout to 0 to immediately timeout the reply + deviceHandlerMock.changeSimpleCommandReplyCountdown(0); + result = deviceHandlerCommander.sendCommand(objects::DEVICE_HANDLER_MOCK, + 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); + deviceHandlerMock.performOperation(DeviceHandlerIF::PERFORM_OPERATION); + deviceHandlerCommander.performOperation(); + result = deviceHandlerCommander.getReplyReturnCode(); + REQUIRE(result == DeviceHandlerIF::TIMEOUT); + uint32_t missedReplies = deviceFdirMock.getMissedReplyCount(); + REQUIRE(missedReplies == 1); + } + + SECTION("Periodic reply nominal") { comIF.setTestCase(ComIFMock::TestCase::PERIODIC_REPLY_NOMINAL); deviceHandlerMock.performOperation(DeviceHandlerIF::PERFORM_OPERATION); deviceHandlerMock.performOperation(DeviceHandlerIF::SEND_WRITE); From ae2f7219fd9d0f78fb9cb6846cedbbd159db142f Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Mon, 6 Jun 2022 11:55:42 +0200 Subject: [PATCH 07/15] run auto-formatter --- README.md | 5 +- src/fsfw/devicehandlers/DeviceHandlerBase.cpp | 7 +- src/fsfw/devicehandlers/DeviceHandlerBase.h | 6 +- src/fsfw/events/EventManager.h | 3 +- src/fsfw/events/EventManagerIF.h | 3 +- src/fsfw/fdir/FailureIsolationBase.cpp | 14 ++-- src/fsfw/fdir/FaultCounter.h | 15 ++--- src/fsfw/health/HealthHelper.cpp | 4 +- src/fsfw/health/HealthTable.cpp | 8 +-- .../unit/devicehandler/ComIFMock.cpp | 47 +++++++------ .../unit/devicehandler/CookieIFMock.cpp | 6 +- .../devicehandler/DeviceHandlerCommander.cpp | 5 +- .../devicehandler/DeviceHandlerCommander.h | 7 +- .../unit/devicehandler/DeviceHandlerMock.cpp | 30 ++++----- .../unit/devicehandler/DeviceHandlerMock.h | 63 +++++++++-------- .../devicehandler/TestDeviceHandlerBase.cpp | 67 +++++++++---------- .../unit/testcfg/objects/systemObjectList.h | 2 +- 17 files changed, 136 insertions(+), 156 deletions(-) diff --git a/README.md b/README.md index 5112230a..673b66c6 100644 --- a/README.md +++ b/README.md @@ -188,7 +188,10 @@ and open the documentation conveniently. Try `helper.py -h for more information. The formatting is done by the `clang-format` tool. The configuration is contained within the `.clang-format` file in the repository root. As long as `clang-format` is installed, you -can run the `apply-clang-format.sh` helper script to format all source files consistently. +can run the `auto-format.sh` helper script to format all source files consistently. Furthermore cmake-format is required to format CMake files which can be installed with: +````sh +sudo pip install cmakelang +```` ## Index diff --git a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp index a874824d..5eb45685 100644 --- a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp +++ b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp @@ -66,7 +66,7 @@ void DeviceHandlerBase::setThermalStateRequestPoolIds(lp_id_t thermalStatePoolId DeviceHandlerBase::~DeviceHandlerBase() { if (comCookie != nullptr) { - delete comCookie; + delete comCookie; } if (defaultFDIRUsed) { delete fdirInstance; @@ -256,7 +256,7 @@ void DeviceHandlerBase::decrementDeviceReplyMap() { missedReply(replyPair.first); timedOut = false; if (not replyPair.second.periodic) { - replyPair.second.active = false; + replyPair.second.active = false; } } } @@ -1396,8 +1396,7 @@ uint8_t DeviceHandlerBase::getReplyDelayCycles(DeviceCommandId_t deviceCommand) DeviceReplyMap::iterator iter = deviceReplyMap.find(deviceCommand); if (iter == deviceReplyMap.end()) { return 0; - } - else if (iter->second.countdown != nullptr) { + } else if (iter->second.countdown != nullptr) { return 0; } return iter->second.delayCycles; diff --git a/src/fsfw/devicehandlers/DeviceHandlerBase.h b/src/fsfw/devicehandlers/DeviceHandlerBase.h index 1e1803e8..1945ac65 100644 --- a/src/fsfw/devicehandlers/DeviceHandlerBase.h +++ b/src/fsfw/devicehandlers/DeviceHandlerBase.h @@ -792,7 +792,7 @@ class DeviceHandlerBase : public DeviceHandlerIF, DeviceCommandMap::iterator command; //! Instead of using delayCycles to specify the maximum time to wait for the device reply, it //! is also possible specify a countdown - Countdown* countdown = nullptr; + Countdown *countdown = nullptr; //! will be set to true when reply is enabled bool active = false; }; @@ -1259,13 +1259,13 @@ class DeviceHandlerBase : public DeviceHandlerIF, /** * @brief Handles disabling of replies which use a timeout to detect missed replies. */ - void disableTimeoutControlledReply(DeviceReplyInfo* info); + void disableTimeoutControlledReply(DeviceReplyInfo *info); /** * @brief Handles disabling of replies which use a number of maximum delay cycles to detect * missed replies. */ - void disableDelayCyclesControlledReply(DeviceReplyInfo* info); + void disableDelayCyclesControlledReply(DeviceReplyInfo *info); /** * Retrive data from the #IPCStore. diff --git a/src/fsfw/events/EventManager.h b/src/fsfw/events/EventManager.h index 77d657e8..70245f5d 100644 --- a/src/fsfw/events/EventManager.h +++ b/src/fsfw/events/EventManager.h @@ -37,8 +37,7 @@ 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 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 893f86f1..adb61f2b 100644 --- a/src/fsfw/events/EventManagerIF.h +++ b/src/fsfw/events/EventManagerIF.h @@ -20,8 +20,7 @@ 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 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 da17d783..3e6d5dcf 100644 --- a/src/fsfw/fdir/FailureIsolationBase.cpp +++ b/src/fsfw/fdir/FailureIsolationBase.cpp @@ -15,16 +15,16 @@ FailureIsolationBase::FailureIsolationBase(object_id_t owner, object_id_t parent FailureIsolationBase::~FailureIsolationBase() { EventManagerIF* manager = ObjectManager::instance()->get(objects::EVENT_MANAGER); - if (manager == nullptr) { + if (manager == nullptr) { #if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::error << "FailureIsolationBase::~FailureIsolationBase: Event Manager has not" - " been initialized!" - << std::endl; + sif::error << "FailureIsolationBase::~FailureIsolationBase: Event Manager has not" + " been initialized!" + << std::endl; #endif - return; - } + return; + } manager->unsubscribeFromAllEvents(eventQueue->getId(), ownerId); - QueueFactory::instance()->deleteMessageQueue(eventQueue); + QueueFactory::instance()->deleteMessageQueue(eventQueue); } ReturnValue_t FailureIsolationBase::initialize() { diff --git a/src/fsfw/fdir/FaultCounter.h b/src/fsfw/fdir/FaultCounter.h index c5630da9..cca780d6 100644 --- a/src/fsfw/fdir/FaultCounter.h +++ b/src/fsfw/fdir/FaultCounter.h @@ -6,12 +6,7 @@ class FaultCounter : public HasParametersIF { public: - - enum class ParameterIds { - FAILURE_THRESHOLD, - FAULT_COUNT, - TIMEOUT - }; + enum class ParameterIds { FAILURE_THRESHOLD, FAULT_COUNT, TIMEOUT }; FaultCounter(); FaultCounter(uint32_t failureThreshold, uint32_t decrementAfterMs, @@ -30,10 +25,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 = nullptr, uint16_t startAtIndex = - 0); + 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 b9884115..0f51ddeb 100644 --- a/src/fsfw/health/HealthHelper.cpp +++ b/src/fsfw/health/HealthHelper.cpp @@ -5,9 +5,7 @@ HealthHelper::HealthHelper(HasHealthIF* owner, object_id_t objectId) : objectId(objectId), owner(owner) {} -HealthHelper::~HealthHelper() { - healthTable->removeObject(objectId); -} +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 ee8fcd94..f4bda1c3 100644 --- a/src/fsfw/health/HealthTable.cpp +++ b/src/fsfw/health/HealthTable.cpp @@ -28,10 +28,10 @@ ReturnValue_t HealthTable::registerObject(object_id_t object, } ReturnValue_t HealthTable::removeObject(object_id_t object) { - mapIterator = healthMap.find(object); - if (mapIterator == healthMap.end()) { - return HasReturnvaluesIF::RETURN_FAILED; - } + mapIterator = healthMap.find(object); + if (mapIterator == healthMap.end()) { + return HasReturnvaluesIF::RETURN_FAILED; + } healthMap.erase(mapIterator); return HasReturnvaluesIF::RETURN_OK; } diff --git a/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.cpp b/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.cpp index 4dd64c1a..4d985f94 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/ComIFMock.cpp @@ -1,7 +1,8 @@ #include "ComIFMock.h" + #include "DeviceHandlerMock.h" -ComIFMock::ComIFMock(object_id_t objectId) : SystemObject(objectId){} +ComIFMock::ComIFMock(object_id_t objectId) : SystemObject(objectId) {} ComIFMock::~ComIFMock() {} @@ -19,29 +20,27 @@ ReturnValue_t ComIFMock::requestReceiveMessage(CookieIF *cookie, size_t requestL } ReturnValue_t ComIFMock::readReceivedMessage(CookieIF *cookie, uint8_t **buffer, size_t *size) { - 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; - } + 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_; -} +void ComIFMock::setTestCase(TestCase testCase_) { testCase = testCase_; } diff --git a/tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.cpp b/tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.cpp index f73c1731..e9b03a6c 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/CookieIFMock.cpp @@ -1,7 +1,5 @@ #include "CookieIFMock.h" -CookieIFMock::CookieIFMock() { -} +CookieIFMock::CookieIFMock() {} -CookieIFMock::~CookieIFMock() { -} +CookieIFMock::~CookieIFMock() {} diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.cpp b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.cpp index b3889b2c..4540a388 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) @@ -60,6 +61,4 @@ ReturnValue_t DeviceHandlerCommander::sendCommand(object_id_t target, ActionId_t ReturnValue_t DeviceHandlerCommander::getReplyReturnCode() { return lastReplyReturnCode; } -void DeviceHandlerCommander::resetReplyReturnCode() { - lastReplyReturnCode = RETURN_FAILED; -} +void DeviceHandlerCommander::resetReplyReturnCode() { lastReplyReturnCode = RETURN_FAILED; } diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h index 77168043..82183baf 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerCommander.h @@ -1,16 +1,16 @@ #ifndef TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_DEVICEHANDLERCOMMANDER_H_ #define TESTS_SRC_FSFW_TESTS_UNIT_DEVICEHANDLER_DEVICEHANDLERCOMMANDER_H_ -#include "fsfw/objectmanager/SystemObject.h" #include "fsfw/action/CommandActionHelper.h" #include "fsfw/action/CommandsActionsIF.h" -#include "fsfw/tasks/ExecutableObjectIF.h" +#include "fsfw/objectmanager/SystemObject.h" #include "fsfw/returnvalues/HasReturnvaluesIF.h" +#include "fsfw/tasks/ExecutableObjectIF.h" class DeviceHandlerCommander : public ExecutableObjectIF, public SystemObject, public CommandsActionsIF, - public HasReturnvaluesIF { + public HasReturnvaluesIF { public: DeviceHandlerCommander(object_id_t objectId); virtual ~DeviceHandlerCommander(); @@ -36,7 +36,6 @@ class DeviceHandlerCommander : public ExecutableObjectIF, void resetReplyReturnCode(); private: - static const uint32_t QUEUE_SIZE = 20; MessageQueueIF* commandQueue = nullptr; diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp index 88f712db..7350d579 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp @@ -42,13 +42,13 @@ ReturnValue_t DeviceHandlerMock::buildCommandFromCommand(DeviceCommandId_t devic 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: { + 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; @@ -64,7 +64,7 @@ ReturnValue_t DeviceHandlerMock::interpretDeviceReply(DeviceCommandId_t id, cons switch (id) { case SIMPLE_COMMAND: case PERIODIC_REPLY: { - periodicReplyReceived = true; + periodicReplyReceived = true; break; } default: @@ -74,9 +74,9 @@ ReturnValue_t DeviceHandlerMock::interpretDeviceReply(DeviceCommandId_t id, cons } void DeviceHandlerMock::fillCommandAndReplyMap() { - insertInCommandAndReplyMap(SIMPLE_COMMAND, 0, nullptr, 0, false, false, 0, - &simpleCommandReplyTimeout); - insertInCommandAndReplyMap(PERIODIC_REPLY, 0, nullptr, 0, true, false, 0, + insertInCommandAndReplyMap(SIMPLE_COMMAND, 0, nullptr, 0, false, false, 0, + &simpleCommandReplyTimeout); + insertInCommandAndReplyMap(PERIODIC_REPLY, 0, nullptr, 0, true, false, 0, &periodicReplyCountdown); } @@ -90,10 +90,6 @@ void DeviceHandlerMock::changeSimpleCommandReplyCountdown(uint32_t timeout) { simpleCommandReplyTimeout.setTimeout(timeout); } -void DeviceHandlerMock::resetPeriodicReplyState() { - periodicReplyReceived = false; -} +void DeviceHandlerMock::resetPeriodicReplyState() { periodicReplyReceived = false; } -bool DeviceHandlerMock::getPeriodicReplyReceived() { - return periodicReplyReceived; -} +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 4ff0d77d..95d0fdbb 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h @@ -3,45 +3,42 @@ #include -class DeviceHandlerMock: public DeviceHandlerBase { -public: +class DeviceHandlerMock : public DeviceHandlerBase { + public: + static const DeviceCommandId_t SIMPLE_COMMAND = 1; + static const DeviceCommandId_t PERIODIC_REPLY = 2; - static const DeviceCommandId_t SIMPLE_COMMAND = 1; - static const DeviceCommandId_t PERIODIC_REPLY = 2; + static const uint8_t SIMPLE_COMMAND_DATA = 1; + static const uint8_t PERIODIC_REPLY_DATA = 2; - 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 changeSimpleCommandReplyCountdown(uint32_t timeout); + void resetPeriodicReplyState(); + bool getPeriodicReplyReceived(); - DeviceHandlerMock(object_id_t objectId, object_id_t deviceCommunication, - CookieIF *comCookie, FailureIsolationBase* fdirInstance); - virtual ~DeviceHandlerMock(); - void changePeriodicReplyCountdown(uint32_t timeout); - void changeSimpleCommandReplyCountdown(uint32_t timeout); - void resetPeriodicReplyState(); - bool getPeriodicReplyReceived(); + protected: + 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; -protected: - 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: + Countdown simpleCommandReplyTimeout = Countdown(50); + Countdown periodicReplyCountdown = Countdown(10); -private: + uint8_t commandBuffer[1]; - Countdown simpleCommandReplyTimeout = Countdown(50); - Countdown periodicReplyCountdown = Countdown(10); - - uint8_t commandBuffer[1]; - - bool periodicReplyReceived = false; + 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 283a8799..9a20f1a8 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp @@ -8,21 +8,20 @@ #include "fsfw_tests/unit/testcfg/objects/systemObjectList.h" TEST_CASE("Device Handler Base", "[DeviceHandlerBase]") { - - // 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); + // 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 nominal") { - comIF.setTestCase(ComIFMock::TestCase::SIMPLE_COMMAND_NOMINAL); + comIF.setTestCase(ComIFMock::TestCase::SIMPLE_COMMAND_NOMINAL); result = deviceHandlerCommander.sendCommand(objects::DEVICE_HANDLER_MOCK, DeviceHandlerMock::SIMPLE_COMMAND); REQUIRE(result == HasReturnvaluesIF::RETURN_OK); @@ -37,28 +36,28 @@ TEST_CASE("Device Handler Base", "[DeviceHandlerBase]") { } SECTION("Commanding missed reply") { - comIF.setTestCase(ComIFMock::TestCase::MISSED_REPLY); - deviceHandlerCommander.resetReplyReturnCode(); - // Set the timeout to 0 to immediately timeout the reply - deviceHandlerMock.changeSimpleCommandReplyCountdown(0); - result = deviceHandlerCommander.sendCommand(objects::DEVICE_HANDLER_MOCK, - 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); - deviceHandlerMock.performOperation(DeviceHandlerIF::PERFORM_OPERATION); - deviceHandlerCommander.performOperation(); - result = deviceHandlerCommander.getReplyReturnCode(); - REQUIRE(result == DeviceHandlerIF::TIMEOUT); - uint32_t missedReplies = deviceFdirMock.getMissedReplyCount(); - REQUIRE(missedReplies == 1); - } + comIF.setTestCase(ComIFMock::TestCase::MISSED_REPLY); + deviceHandlerCommander.resetReplyReturnCode(); + // Set the timeout to 0 to immediately timeout the reply + deviceHandlerMock.changeSimpleCommandReplyCountdown(0); + result = deviceHandlerCommander.sendCommand(objects::DEVICE_HANDLER_MOCK, + 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); + deviceHandlerMock.performOperation(DeviceHandlerIF::PERFORM_OPERATION); + deviceHandlerCommander.performOperation(); + result = deviceHandlerCommander.getReplyReturnCode(); + REQUIRE(result == DeviceHandlerIF::TIMEOUT); + uint32_t missedReplies = deviceFdirMock.getMissedReplyCount(); + REQUIRE(missedReplies == 1); + } SECTION("Periodic reply nominal") { - comIF.setTestCase(ComIFMock::TestCase::PERIODIC_REPLY_NOMINAL); + comIF.setTestCase(ComIFMock::TestCase::PERIODIC_REPLY_NOMINAL); deviceHandlerMock.performOperation(DeviceHandlerIF::PERFORM_OPERATION); deviceHandlerMock.performOperation(DeviceHandlerIF::SEND_WRITE); deviceHandlerMock.performOperation(DeviceHandlerIF::GET_WRITE); @@ -68,7 +67,7 @@ TEST_CASE("Device Handler Base", "[DeviceHandlerBase]") { } SECTION("Missed periodic reply") { - comIF.setTestCase(ComIFMock::TestCase::MISSED_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); diff --git a/tests/src/fsfw_tests/unit/testcfg/objects/systemObjectList.h b/tests/src/fsfw_tests/unit/testcfg/objects/systemObjectList.h index 6f40da3c..17b980e9 100644 --- a/tests/src/fsfw_tests/unit/testcfg/objects/systemObjectList.h +++ b/tests/src/fsfw_tests/unit/testcfg/objects/systemObjectList.h @@ -31,4 +31,4 @@ enum sourceObjects : uint32_t { }; } -#endif /* BSP_CONFIG_OBJECTS_SYSTEMOBJECTLIST_H_ */ +#endif /* BSP_CONFIG_OBJECTS_SYSTEMOBJECTLIST_H_ */ From 103661faccd8cd505b2ca179dc1999b05ec2a933 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Mon, 6 Jun 2022 12:26:00 +0200 Subject: [PATCH 08/15] deugging --- .../devicehandler/TestDeviceHandlerBase.cpp | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp index 9a20f1a8..5ced746b 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp @@ -35,26 +35,26 @@ TEST_CASE("Device Handler Base", "[DeviceHandlerBase]") { REQUIRE(result == HasReturnvaluesIF::RETURN_OK); } - SECTION("Commanding missed reply") { - comIF.setTestCase(ComIFMock::TestCase::MISSED_REPLY); - deviceHandlerCommander.resetReplyReturnCode(); - // Set the timeout to 0 to immediately timeout the reply - deviceHandlerMock.changeSimpleCommandReplyCountdown(0); - result = deviceHandlerCommander.sendCommand(objects::DEVICE_HANDLER_MOCK, - 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); - deviceHandlerMock.performOperation(DeviceHandlerIF::PERFORM_OPERATION); - deviceHandlerCommander.performOperation(); - result = deviceHandlerCommander.getReplyReturnCode(); - REQUIRE(result == DeviceHandlerIF::TIMEOUT); - uint32_t missedReplies = deviceFdirMock.getMissedReplyCount(); - REQUIRE(missedReplies == 1); - } +// SECTION("Commanding missed reply") { +// comIF.setTestCase(ComIFMock::TestCase::MISSED_REPLY); +// deviceHandlerCommander.resetReplyReturnCode(); +// // Set the timeout to 0 to immediately timeout the reply +// deviceHandlerMock.changeSimpleCommandReplyCountdown(0); +// result = deviceHandlerCommander.sendCommand(objects::DEVICE_HANDLER_MOCK, +// 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); +// deviceHandlerMock.performOperation(DeviceHandlerIF::PERFORM_OPERATION); +// deviceHandlerCommander.performOperation(); +// result = deviceHandlerCommander.getReplyReturnCode(); +// REQUIRE(result == DeviceHandlerIF::TIMEOUT); +// uint32_t missedReplies = deviceFdirMock.getMissedReplyCount(); +// REQUIRE(missedReplies == 1); +// } SECTION("Periodic reply nominal") { comIF.setTestCase(ComIFMock::TestCase::PERIODIC_REPLY_NOMINAL); From ade36e65c6a1026ad9b2dff838622fc439856a57 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Mon, 6 Jun 2022 12:30:27 +0200 Subject: [PATCH 09/15] missed reply check in simple command nominal test case --- .../devicehandler/TestDeviceHandlerBase.cpp | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp index 5ced746b..dd9e9ca5 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp @@ -32,29 +32,31 @@ TEST_CASE("Device Handler Base", "[DeviceHandlerBase]") { deviceHandlerMock.performOperation(DeviceHandlerIF::GET_READ); deviceHandlerCommander.performOperation(); result = deviceHandlerCommander.getReplyReturnCode(); + uint32_t missedReplies = deviceFdirMock.getMissedReplyCount(); + REQUIRE(missedReplies == 0); REQUIRE(result == HasReturnvaluesIF::RETURN_OK); } -// SECTION("Commanding missed reply") { -// comIF.setTestCase(ComIFMock::TestCase::MISSED_REPLY); -// deviceHandlerCommander.resetReplyReturnCode(); -// // Set the timeout to 0 to immediately timeout the reply -// deviceHandlerMock.changeSimpleCommandReplyCountdown(0); -// result = deviceHandlerCommander.sendCommand(objects::DEVICE_HANDLER_MOCK, -// 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); -// deviceHandlerMock.performOperation(DeviceHandlerIF::PERFORM_OPERATION); -// deviceHandlerCommander.performOperation(); -// result = deviceHandlerCommander.getReplyReturnCode(); -// REQUIRE(result == DeviceHandlerIF::TIMEOUT); -// uint32_t missedReplies = deviceFdirMock.getMissedReplyCount(); -// REQUIRE(missedReplies == 1); -// } + SECTION("Commanding missed reply") { + comIF.setTestCase(ComIFMock::TestCase::MISSED_REPLY); + deviceHandlerCommander.resetReplyReturnCode(); + // Set the timeout to 0 to immediately timeout the reply + deviceHandlerMock.changeSimpleCommandReplyCountdown(0); + result = deviceHandlerCommander.sendCommand(objects::DEVICE_HANDLER_MOCK, + 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); + deviceHandlerMock.performOperation(DeviceHandlerIF::PERFORM_OPERATION); + deviceHandlerCommander.performOperation(); + result = deviceHandlerCommander.getReplyReturnCode(); + REQUIRE(result == DeviceHandlerIF::TIMEOUT); + uint32_t missedReplies = deviceFdirMock.getMissedReplyCount(); + REQUIRE(missedReplies == 1); + } SECTION("Periodic reply nominal") { comIF.setTestCase(ComIFMock::TestCase::PERIODIC_REPLY_NOMINAL); From 21eb386f3cae3b0e05a7731382069a3a8d2663fd Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Mon, 6 Jun 2022 12:34:57 +0200 Subject: [PATCH 10/15] changed reply timeouts --- tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h index 95d0fdbb..1ab1d90b 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h @@ -33,8 +33,8 @@ class DeviceHandlerMock : public DeviceHandlerBase { uint32_t getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) override; private: - Countdown simpleCommandReplyTimeout = Countdown(50); - Countdown periodicReplyCountdown = Countdown(10); + Countdown simpleCommandReplyTimeout = Countdown(1000); + Countdown periodicReplyCountdown = Countdown(1000); uint8_t commandBuffer[1]; From 2d2f65bf89d02f8605e3471063bfd4ab5e53c39d Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Thu, 23 Jun 2022 11:54:51 +0200 Subject: [PATCH 11/15] moved activation of periodic replies to updatePeriodicReply --- src/fsfw/devicehandlers/DeviceHandlerBase.cpp | 7 ++++--- .../unit/devicehandler/DeviceHandlerMock.cpp | 8 ++++++++ .../unit/devicehandler/DeviceHandlerMock.h | 2 ++ .../unit/devicehandler/TestDeviceHandlerBase.cpp | 12 ++++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp index 5eb45685..f3cfcee4 100644 --- a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp +++ b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp @@ -446,9 +446,6 @@ ReturnValue_t DeviceHandlerBase::insertInReplyMap(DeviceCommandId_t replyId, info.dataSet = dataSet; info.command = deviceCommandMap.end(); info.countdown = countdown; - if (info.periodic) { - info.active = true; - } auto resultPair = deviceReplyMap.emplace(replyId, info); if (resultPair.second) { return RETURN_OK; @@ -522,8 +519,10 @@ ReturnValue_t DeviceHandlerBase::updatePeriodicReply(bool enable, DeviceCommandI } if (enable) { info->delayCycles = info->maxDelayCycles; + info->active = true; } else { info->delayCycles = 0; + info->active = false; } } return HasReturnvaluesIF::RETURN_OK; @@ -999,6 +998,8 @@ ReturnValue_t DeviceHandlerBase::enableReplyInReplyMap(DeviceCommandMap::iterato } if (iter != deviceReplyMap.end()) { DeviceReplyInfo* info = &(iter->second); + // If a countdown has been set, the delay cycles will be ignored and the reply times out + // as soon as the countdown has expired info->delayCycles = info->maxDelayCycles; info->command = command; command->second.expectedReplies = expectedReplies; diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp index 7350d579..a17da98b 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp @@ -93,3 +93,11 @@ void DeviceHandlerMock::changeSimpleCommandReplyCountdown(uint32_t timeout) { void DeviceHandlerMock::resetPeriodicReplyState() { periodicReplyReceived = false; } bool DeviceHandlerMock::getPeriodicReplyReceived() { return periodicReplyReceived; } + +ReturnValue_t DeviceHandlerMock::enablePeriodicReply(DeviceCommandId_t replyId) { + return updatePeriodicReply(true, replyId); +} + +ReturnValue_t DeviceHandlerMock::disablePeriodicReply(DeviceCommandId_t replyId) { + return updatePeriodicReply(false, replyId); +} diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h index 1ab1d90b..f5fcb4aa 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.h @@ -18,6 +18,8 @@ class DeviceHandlerMock : public DeviceHandlerBase { void changeSimpleCommandReplyCountdown(uint32_t timeout); void resetPeriodicReplyState(); bool getPeriodicReplyReceived(); + ReturnValue_t enablePeriodicReply(DeviceCommandId_t replyId); + ReturnValue_t disablePeriodicReply(DeviceCommandId_t replyId); protected: void doStartUp() override; diff --git a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp index dd9e9ca5..99d3816f 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp @@ -60,6 +60,7 @@ TEST_CASE("Device Handler Base", "[DeviceHandlerBase]") { SECTION("Periodic reply nominal") { comIF.setTestCase(ComIFMock::TestCase::PERIODIC_REPLY_NOMINAL); + deviceHandlerMock.enablePeriodicReply(DeviceHandlerMock::PERIODIC_REPLY); deviceHandlerMock.performOperation(DeviceHandlerIF::PERFORM_OPERATION); deviceHandlerMock.performOperation(DeviceHandlerIF::SEND_WRITE); deviceHandlerMock.performOperation(DeviceHandlerIF::GET_WRITE); @@ -72,6 +73,7 @@ TEST_CASE("Device Handler Base", "[DeviceHandlerBase]") { comIF.setTestCase(ComIFMock::TestCase::MISSED_REPLY); // Set the timeout to 0 to immediately timeout the reply deviceHandlerMock.changePeriodicReplyCountdown(0); + deviceHandlerMock.enablePeriodicReply(DeviceHandlerMock::PERIODIC_REPLY); deviceHandlerMock.performOperation(DeviceHandlerIF::PERFORM_OPERATION); deviceHandlerMock.performOperation(DeviceHandlerIF::SEND_WRITE); deviceHandlerMock.performOperation(DeviceHandlerIF::GET_WRITE); @@ -79,5 +81,15 @@ TEST_CASE("Device Handler Base", "[DeviceHandlerBase]") { deviceHandlerMock.performOperation(DeviceHandlerIF::GET_READ); uint32_t missedReplies = deviceFdirMock.getMissedReplyCount(); REQUIRE(missedReplies == 1); + // Test if disabling of periodic reply + deviceHandlerMock.disablePeriodicReply(DeviceHandlerMock::PERIODIC_REPLY); + 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); + missedReplies = deviceFdirMock.getMissedReplyCount(); + // Should still be 1 because periodic reply is now disabled + REQUIRE(missedReplies == 1); } } From df97bbc6913a7501f80fab893586e7564abb3678 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Thu, 23 Jun 2022 11:56:46 +0200 Subject: [PATCH 12/15] run auto-formatter --- src/fsfw/datapoollocal/LocalDataPoolManager.cpp | 3 ++- src/fsfw/osal/linux/FixedTimeslotTask.cpp | 2 +- .../unit/devicehandler/DeviceHandlerMock.cpp | 4 ++-- .../unit/devicehandler/TestDeviceHandlerBase.cpp | 16 ++++++++-------- .../fsfw_tests/unit/hal/testCommandExecutor.cpp | 2 +- .../unit/storagemanager/TestNewAccessor.cpp | 10 +++++----- .../fsfw_tests/unit/storagemanager/TestPool.cpp | 2 +- 7 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/fsfw/datapoollocal/LocalDataPoolManager.cpp b/src/fsfw/datapoollocal/LocalDataPoolManager.cpp index 215d1753..9b7f800f 100644 --- a/src/fsfw/datapoollocal/LocalDataPoolManager.cpp +++ b/src/fsfw/datapoollocal/LocalDataPoolManager.cpp @@ -696,7 +696,8 @@ void LocalDataPoolManager::performPeriodicHkGeneration(HkReceiver& receiver) { if (result != HasReturnvaluesIF::RETURN_OK) { /* Configuration error */ #if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::warning << "LocalDataPoolManager::performPeriodicHkOperation: HK generation failed." << std::endl; + sif::warning << "LocalDataPoolManager::performPeriodicHkOperation: HK generation failed." + << std::endl; #else sif::printWarning("LocalDataPoolManager::performPeriodicHkOperation: HK generation failed.\n"); #endif diff --git a/src/fsfw/osal/linux/FixedTimeslotTask.cpp b/src/fsfw/osal/linux/FixedTimeslotTask.cpp index 775acea8..156413ea 100644 --- a/src/fsfw/osal/linux/FixedTimeslotTask.cpp +++ b/src/fsfw/osal/linux/FixedTimeslotTask.cpp @@ -54,7 +54,7 @@ ReturnValue_t FixedTimeslotTask::sleepFor(uint32_t ms) { // If the deadline was missed, the deadlineMissedFunc is called. if (!PosixThread::delayUntil(&lastWakeTime, interval)) { // No time left on timer -> we missed the deadline - if(dlmFunc != nullptr){ + if (dlmFunc != nullptr) { dlmFunc(); } } diff --git a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp index a17da98b..1e05f8f3 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/DeviceHandlerMock.cpp @@ -95,9 +95,9 @@ void DeviceHandlerMock::resetPeriodicReplyState() { periodicReplyReceived = fals bool DeviceHandlerMock::getPeriodicReplyReceived() { return periodicReplyReceived; } ReturnValue_t DeviceHandlerMock::enablePeriodicReply(DeviceCommandId_t replyId) { - return updatePeriodicReply(true, replyId); + return updatePeriodicReply(true, replyId); } ReturnValue_t DeviceHandlerMock::disablePeriodicReply(DeviceCommandId_t replyId) { - return updatePeriodicReply(false, replyId); + return updatePeriodicReply(false, replyId); } diff --git a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp index 99d3816f..873329c3 100644 --- a/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp +++ b/tests/src/fsfw_tests/unit/devicehandler/TestDeviceHandlerBase.cpp @@ -33,7 +33,7 @@ TEST_CASE("Device Handler Base", "[DeviceHandlerBase]") { deviceHandlerCommander.performOperation(); result = deviceHandlerCommander.getReplyReturnCode(); uint32_t missedReplies = deviceFdirMock.getMissedReplyCount(); - REQUIRE(missedReplies == 0); + REQUIRE(missedReplies == 0); REQUIRE(result == HasReturnvaluesIF::RETURN_OK); } @@ -84,12 +84,12 @@ TEST_CASE("Device Handler Base", "[DeviceHandlerBase]") { // Test if disabling of periodic reply deviceHandlerMock.disablePeriodicReply(DeviceHandlerMock::PERIODIC_REPLY); 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); - missedReplies = deviceFdirMock.getMissedReplyCount(); - // Should still be 1 because periodic reply is now disabled - REQUIRE(missedReplies == 1); + deviceHandlerMock.performOperation(DeviceHandlerIF::SEND_WRITE); + deviceHandlerMock.performOperation(DeviceHandlerIF::GET_WRITE); + deviceHandlerMock.performOperation(DeviceHandlerIF::SEND_READ); + deviceHandlerMock.performOperation(DeviceHandlerIF::GET_READ); + missedReplies = deviceFdirMock.getMissedReplyCount(); + // Should still be 1 because periodic reply is now disabled + REQUIRE(missedReplies == 1); } } diff --git a/tests/src/fsfw_tests/unit/hal/testCommandExecutor.cpp b/tests/src/fsfw_tests/unit/hal/testCommandExecutor.cpp index 09d31280..d34f67aa 100644 --- a/tests/src/fsfw_tests/unit/hal/testCommandExecutor.cpp +++ b/tests/src/fsfw_tests/unit/hal/testCommandExecutor.cpp @@ -4,12 +4,12 @@ #include #include -#include "tests/TestsConfig.h" #include "fsfw/container/DynamicFIFO.h" #include "fsfw/container/SimpleRingBuffer.h" #include "fsfw/platform.h" #include "fsfw/serviceinterface.h" #include "fsfw_hal/linux/CommandExecutor.h" +#include "tests/TestsConfig.h" #ifdef PLATFORM_UNIX diff --git a/tests/src/fsfw_tests/unit/storagemanager/TestNewAccessor.cpp b/tests/src/fsfw_tests/unit/storagemanager/TestNewAccessor.cpp index 9631de38..7b90c86e 100644 --- a/tests/src/fsfw_tests/unit/storagemanager/TestNewAccessor.cpp +++ b/tests/src/fsfw_tests/unit/storagemanager/TestNewAccessor.cpp @@ -157,7 +157,7 @@ TEST_CASE("New Accessor", "[NewAccessor]") { } } - SECTION("Operators"){ + SECTION("Operators") { result = SimplePool.addData(&testStoreId, testDataArray.data(), size); REQUIRE(result == retval::CATCH_OK); { @@ -173,13 +173,13 @@ TEST_CASE("New Accessor", "[NewAccessor]") { REQUIRE(result == HasReturnvaluesIF::RETURN_OK); CHECK(accessor2.getId() == testStoreId); CHECK(accessor2.size() == 10); - + std::array newData; // Expect data to be invalid so this must return RETURN_FAILED - result = accessor.getDataCopy(newData.data(),newData.size()); + result = accessor.getDataCopy(newData.data(), newData.size()); REQUIRE(result == HasReturnvaluesIF::RETURN_FAILED); - // Expect data to be too small - result = accessor2.getDataCopy(data.data(),data.size()); + // Expect data to be too small + result = accessor2.getDataCopy(data.data(), data.size()); REQUIRE(result == HasReturnvaluesIF::RETURN_FAILED); } } diff --git a/tests/src/fsfw_tests/unit/storagemanager/TestPool.cpp b/tests/src/fsfw_tests/unit/storagemanager/TestPool.cpp index ce1f8518..51130047 100644 --- a/tests/src/fsfw_tests/unit/storagemanager/TestPool.cpp +++ b/tests/src/fsfw_tests/unit/storagemanager/TestPool.cpp @@ -1,9 +1,9 @@ #include #include +#include #include #include -#include #include "fsfw_tests/unit/CatchDefinitions.h" From adbf375f38c30dc24af246b3638bbb3bcb5dda64 Mon Sep 17 00:00:00 2001 From: Ulrich Mohr Date: Wed, 13 Jul 2022 21:58:07 +0200 Subject: [PATCH 13/15] some small fixes to dhb countdown addition --- src/fsfw/devicehandlers/DeviceHandlerBase.cpp | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp index f3cfcee4..a6f9b35d 100644 --- a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp +++ b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp @@ -239,25 +239,20 @@ void DeviceHandlerBase::decrementDeviceReplyMap() { for (std::pair& replyPair : deviceReplyMap) { if (replyPair.second.countdown != nullptr && replyPair.second.active) { if (replyPair.second.countdown->hasTimedOut()) { + disableTimeoutControlledReply(&replyPair.second); timedOut = true; } } if (replyPair.second.delayCycles != 0 && replyPair.second.countdown == nullptr) { replyPair.second.delayCycles--; if (replyPair.second.delayCycles == 0) { - if (replyPair.second.periodic) { - replyPair.second.delayCycles = replyPair.second.maxDelayCycles; - } + disableDelayCyclesControlledReply(&replyPair.second); timedOut = true; } } if (timedOut) { replyToReply(replyPair.first, replyPair.second, TIMEOUT); missedReply(replyPair.first); - timedOut = false; - if (not replyPair.second.periodic) { - replyPair.second.active = false; - } } } } @@ -518,11 +513,19 @@ ReturnValue_t DeviceHandlerBase::updatePeriodicReply(bool enable, DeviceCommandI return COMMAND_NOT_SUPPORTED; } if (enable) { - info->delayCycles = info->maxDelayCycles; info->active = true; + if (info->countdown != nullptr) { + info->delayCycles = info->maxDelayCycles; + } else { + info->countdown->resetTimer(); + } } else { - info->delayCycles = 0; info->active = false; + if (info->countdown != nullptr) { + info->delayCycles = 0; + } else { + info->countdown->timeOut(); + } } } return HasReturnvaluesIF::RETURN_OK; @@ -1398,7 +1401,12 @@ uint8_t DeviceHandlerBase::getReplyDelayCycles(DeviceCommandId_t deviceCommand) if (iter == deviceReplyMap.end()) { return 0; } else if (iter->second.countdown != nullptr) { - return 0; + // fake a useful return value for legacy code + if (iter->second.active) { + return 1; + } else { + return 0; + } } return iter->second.delayCycles; } From 4d34f93cfc32400c09cf5c8d7653e2ac85538d14 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Thu, 14 Jul 2022 08:58:23 +0200 Subject: [PATCH 14/15] missing reset of timedOut value in loop of decrementDeviceReplyMap --- src/fsfw/devicehandlers/DeviceHandlerBase.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp index a6f9b35d..ca7701d0 100644 --- a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp +++ b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp @@ -253,6 +253,7 @@ void DeviceHandlerBase::decrementDeviceReplyMap() { if (timedOut) { replyToReply(replyPair.first, replyPair.second, TIMEOUT); missedReply(replyPair.first); + timedOut = false; } } } From ecac08814e8dbcf4d47ac0bea111502bcb528dfa Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Thu, 14 Jul 2022 09:15:13 +0200 Subject: [PATCH 15/15] better naming for functions which reset states of replies --- src/fsfw/devicehandlers/DeviceHandlerBase.cpp | 12 ++++++------ src/fsfw/devicehandlers/DeviceHandlerBase.h | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp index ca7701d0..cf457a03 100644 --- a/src/fsfw/devicehandlers/DeviceHandlerBase.cpp +++ b/src/fsfw/devicehandlers/DeviceHandlerBase.cpp @@ -239,14 +239,14 @@ void DeviceHandlerBase::decrementDeviceReplyMap() { for (std::pair& replyPair : deviceReplyMap) { if (replyPair.second.countdown != nullptr && replyPair.second.active) { if (replyPair.second.countdown->hasTimedOut()) { - disableTimeoutControlledReply(&replyPair.second); + resetTimeoutControlledReply(&replyPair.second); timedOut = true; } } if (replyPair.second.delayCycles != 0 && replyPair.second.countdown == nullptr) { replyPair.second.delayCycles--; if (replyPair.second.delayCycles == 0) { - disableDelayCyclesControlledReply(&replyPair.second); + resetDelayCyclesControlledReply(&replyPair.second); timedOut = true; } } @@ -841,9 +841,9 @@ void DeviceHandlerBase::handleReply(const uint8_t* receivedData, DeviceCommandId } if (info->active && info->countdown != nullptr) { - disableTimeoutControlledReply(info); + resetTimeoutControlledReply(info); } else if (info->delayCycles != 0) { - disableDelayCyclesControlledReply(info); + resetDelayCyclesControlledReply(info); } if (result != RETURN_OK) { @@ -862,7 +862,7 @@ void DeviceHandlerBase::handleReply(const uint8_t* receivedData, DeviceCommandId } } -void DeviceHandlerBase::disableTimeoutControlledReply(DeviceReplyInfo* info) { +void DeviceHandlerBase::resetTimeoutControlledReply(DeviceReplyInfo* info) { if (info->periodic) { info->countdown->resetTimer(); } else { @@ -871,7 +871,7 @@ void DeviceHandlerBase::disableTimeoutControlledReply(DeviceReplyInfo* info) { } } -void DeviceHandlerBase::disableDelayCyclesControlledReply(DeviceReplyInfo* info) { +void DeviceHandlerBase::resetDelayCyclesControlledReply(DeviceReplyInfo* info) { if (info->periodic) { info->delayCycles = info->maxDelayCycles; } else { diff --git a/src/fsfw/devicehandlers/DeviceHandlerBase.h b/src/fsfw/devicehandlers/DeviceHandlerBase.h index 1945ac65..e29e6596 100644 --- a/src/fsfw/devicehandlers/DeviceHandlerBase.h +++ b/src/fsfw/devicehandlers/DeviceHandlerBase.h @@ -1257,15 +1257,15 @@ class DeviceHandlerBase : public DeviceHandlerIF, void doGetRead(void); /** - * @brief Handles disabling of replies which use a timeout to detect missed replies. + * @brief Resets replies which use a timeout to detect missed replies. */ - void disableTimeoutControlledReply(DeviceReplyInfo *info); + void resetTimeoutControlledReply(DeviceReplyInfo *info); /** - * @brief Handles disabling of replies which use a number of maximum delay cycles to detect + * @brief Resets replies which use a number of maximum delay cycles to detect * missed replies. */ - void disableDelayCyclesControlledReply(DeviceReplyInfo *info); + void resetDelayCyclesControlledReply(DeviceReplyInfo *info); /** * Retrive data from the #IPCStore.