From d9060734b05dd29cf2f0849040bdb2506789f968 Mon Sep 17 00:00:00 2001 From: petriVM18 Date: Tue, 5 Jul 2022 12:57:50 +0200 Subject: [PATCH] Removed insert function in config handler, not needed. Added more tests for config handler --- mission/utility/GlobalConfigFileDefinitions.h | 23 ++++ mission/utility/GlobalConfigHandler.cpp | 103 ++---------------- mission/utility/GlobalConfigHandler.h | 53 ++++----- unittest/controller/testConfigFileHandler.cpp | 44 +++++++- 4 files changed, 95 insertions(+), 128 deletions(-) create mode 100644 mission/utility/GlobalConfigFileDefinitions.h diff --git a/mission/utility/GlobalConfigFileDefinitions.h b/mission/utility/GlobalConfigFileDefinitions.h new file mode 100644 index 00000000..7a48c095 --- /dev/null +++ b/mission/utility/GlobalConfigFileDefinitions.h @@ -0,0 +1,23 @@ +/* + * GlobalConfigFileDefinitions.h + * + * Created on: July 05, 2022 + * Author: Jona Petri (IRS) + */ + +#ifndef MISSION_UTILITY_GLOBALCONFIGFILEDEFINITIONS_H_ +#define MISSION_UTILITY_GLOBALCONFIGFILEDEFINITIONS_H_ + + +static constexpr double PARAM0_DEFAULT = 5.0; +static constexpr int PARAM1_DEFAULT = 905; + +enum ParamIds : uint8_t { + PARAM0 = 0, + PARAM1 = 1, + +}; + + + +#endif /* MISSION_UTILITY_GLOBALCONFIGFILEDEFINITIONS_H_ */ diff --git a/mission/utility/GlobalConfigHandler.cpp b/mission/utility/GlobalConfigHandler.cpp index 109fbfa6..c76af5d1 100644 --- a/mission/utility/GlobalConfigHandler.cpp +++ b/mission/utility/GlobalConfigHandler.cpp @@ -2,7 +2,7 @@ * GlobalConfigHandler.cpp * * Created on: May 3, 2022 - * Author: metobs + * Author: Jona Petri (IRS) */ #include "GlobalConfigHandler.h" @@ -11,6 +11,7 @@ #include #include + MutexIF* GlobalConfigHandler::configLock = nullptr; GlobalConfigHandler::GlobalConfigHandler(object_id_t objectId, std::string configFilePath): SystemObject(objectId), @@ -73,7 +74,7 @@ ReturnValue_t GlobalConfigHandler::unlockConfigFile(){ return result; } -template ReturnValue_t GlobalConfigHandler::setConfigFileValue(std::string key, T data){ +template ReturnValue_t GlobalConfigHandler::setConfigFileValue(ParamIds paramID, T data){ ReturnValue_t result = RETURN_OK; ReturnValue_t resultSet = RETURN_OK; @@ -85,7 +86,7 @@ template ReturnValue_t GlobalConfigHandler::setConfigFileValue(std: return result; } - resultSet=setValue(key, data); + resultSet=setValue(PARAM_KEY_MAP[paramID], data); if(resultSet!=RETURN_OK){ triggerEvent(SET_CONFIGFILEVALUE_FAILED, 0, 0); #if OBSW_VERBOSE_LEVEL >= 1 @@ -104,7 +105,7 @@ template ReturnValue_t GlobalConfigHandler::setConfigFileValue(std: return resultSet; } -template ReturnValue_t GlobalConfigHandler::getConfigFileValue(std::string key, T& data){ +template ReturnValue_t GlobalConfigHandler::getConfigFileValue(ParamIds paramID, T& data){ ReturnValue_t result = RETURN_OK; ReturnValue_t resultGet = RETURN_OK; @@ -116,7 +117,7 @@ template ReturnValue_t GlobalConfigHandler::getConfigFileValue(std: return result; } - resultGet=getValue(key, data); + resultGet=getValue(PARAM_KEY_MAP[paramID], data); if (resultGet!= RETURN_OK){ triggerEvent(GET_CONFIGFILEVALUE_FAILED, 0, 0); #if OBSW_VERBOSE_LEVEL >= 1 @@ -133,36 +134,7 @@ template ReturnValue_t GlobalConfigHandler::getConfigFileValue(std: return result; } -template ReturnValue_t GlobalConfigHandler::insertConfigFileValue(std::string key, T data){ - ReturnValue_t result = RETURN_OK; - ReturnValue_t resultInsert = RETURN_OK; - result=lockConfigFile(); - if (result!= RETURN_OK){ -#if OBSW_VERBOSE_LEVEL >= 1 - sif::info << "GlobalConfigHandler::insertConfigFileValue lock mutex failed with " << result << std::endl; -#endif - return result; - } - - resultInsert=insertValue(key, data);//Should never fail - if(resultInsert!=RETURN_OK){ - triggerEvent(INSERT_CONFIGFILEVALUE_FAILED, 0, 0); -#if OBSW_VERBOSE_LEVEL >= 1 - sif::info << "GlobalConfigHandler::insertConfigFileValue insert failed with " << result << std::endl; -#endif - } - - result=unlockConfigFile(); - if (result!= RETURN_OK){ -#if OBSW_VERBOSE_LEVEL >= 1 - sif::info << "GlobalConfigHandler::insertConfigFileValue unlock mutex failed with " << result << std::endl; -#endif - return result; - } - - return resultInsert; -} ReturnValue_t GlobalConfigHandler::resetConfigFileValues(){ ReturnValue_t result = RETURN_OK; result=lockConfigFile(); @@ -260,65 +232,10 @@ std::string GlobalConfigHandler::getConfigFileName(){ return getFullName(); } -ReturnValue_t GlobalConfigHandler::getParameter(uint8_t domainId, uint8_t uniqueId, - ParameterWrapper* parameterWrapper, - const ParameterWrapper* newValues, - uint16_t startAtIndex) { - ReturnValue_t result = RETURN_OK; - switch(uniqueId){ - - case(ParamIds::PARAM0):{ - result=handleDoubleParamUpdate(PARAM_KEY_MAP[static_cast(uniqueId)], - parameterWrapper, newValues); - break; - } - case(ParamIds::PARAM1):{ - result=handleIntParamUpdate(PARAM_KEY_MAP[static_cast(uniqueId)], - parameterWrapper, newValues); - break; - } - default:{ - result=RETURN_FAILED; - break; - } - } - return result; - -} - -//Taken from payloadPCDU Handler Definition -ReturnValue_t GlobalConfigHandler::handleDoubleParamUpdate(std::string key, - ParameterWrapper* parameterWrapper, - const ParameterWrapper* newValues) { - double newValue = 0.0; - ReturnValue_t result = newValues->getElement(&newValue, 0, 0); - if (result != HasReturnvaluesIF::RETURN_OK) { - return result; - } - result=setConfigFileValue(key, newValue); - if (result != HasReturnvaluesIF::RETURN_OK) { - return result; - } - // Do this so the dumping and loading with the framework works as well - - return WriteConfigFile(); -} - -ReturnValue_t GlobalConfigHandler::handleIntParamUpdate(std::string key, - ParameterWrapper* parameterWrapper, - const ParameterWrapper* newValues) { - int newValue = 0; - ReturnValue_t result = newValues->getElement(&newValue, 0, 0); - if (result != HasReturnvaluesIF::RETURN_OK) { - return result; - } - result=setConfigFileValue(key, newValue); - if (result != HasReturnvaluesIF::RETURN_OK) { - return result; - } - - return WriteConfigFile(); -} +template ReturnValue_t GlobalConfigHandler::getConfigFileValue(ParamIds paramID, double& data); +template ReturnValue_t GlobalConfigHandler::getConfigFileValue(ParamIds paramID, int32_t& data); +template ReturnValue_t GlobalConfigHandler::setConfigFileValue(ParamIds paramID, double data); +template ReturnValue_t GlobalConfigHandler::setConfigFileValue(ParamIds paramID, int32_t data); diff --git a/mission/utility/GlobalConfigHandler.h b/mission/utility/GlobalConfigHandler.h index 14e20b28..7869aaf0 100644 --- a/mission/utility/GlobalConfigHandler.h +++ b/mission/utility/GlobalConfigHandler.h @@ -2,11 +2,11 @@ * GlobalConfigHandler.h * * Created on: May 3, 2022 - * Author: metobs + * Author: Jona Petri (IRS) */ -#ifndef BSP_LINUX_GLOBALCONFIGHANDLER_H_ -#define BSP_LINUX_GLOBALCONFIGHANDLER_H_ +#ifndef MISSION_UTILITY_GLOBALCONFIGHANDLER_H_ +#define MISSION_UTILITY_GLOBALCONFIGHANDLER_H_ #include #include @@ -21,14 +21,7 @@ #include #include -static constexpr double PARAM0_DEFAULT = 5.0; -static constexpr int PARAM1_DEFAULT = 905; - -enum ParamIds : uint8_t { - PARAM0 = 0, - PARAM1 = 1, - -}; +#include "GlobalConfigFileDefinitions.h" static std::map PARAM_KEY_MAP = { {PARAM0, "Parameter0"}, @@ -58,34 +51,26 @@ public: ReturnValue_t initialize(); - ReturnValue_t lockConfigFile(); - ReturnValue_t unlockConfigFile(); - template ReturnValue_t setConfigFileValue(std::string key, T data); - template ReturnValue_t getConfigFileValue(std::string key, T& data); - template ReturnValue_t insertConfigFileValue(std::string key, T data); + + template ReturnValue_t setConfigFileValue(ParamIds paramID, T data); + template ReturnValue_t getConfigFileValue(ParamIds paramID, T& data); + ReturnValue_t resetConfigFileValues(); ReturnValue_t WriteConfigFile(); + std::string getConfigFileName(); + +private: + static MutexIF* configLock ; + + ReturnValue_t lockConfigFile(); + ReturnValue_t unlockConfigFile(); + + + ReturnValue_t ReadConfigFile(); ReturnValue_t ResetConfigFile(); ReturnValue_t setConfigFileName(std::string configFileName); - std::string getConfigFileName(); - - ReturnValue_t getParameter(uint8_t domainId, uint8_t uniqueId, - ParameterWrapper* parameterWrapper, - const ParameterWrapper* newValues, - uint16_t startAtIndex); - - ReturnValue_t handleDoubleParamUpdate(std::string key, - ParameterWrapper* parameterWrapper, - const ParameterWrapper* newValues); - ReturnValue_t handleIntParamUpdate(std::string key, - ParameterWrapper* parameterWrapper, - const ParameterWrapper* newValues) ; - - -private: - static MutexIF* configLock ; MessageQueueIF* commandQueue; @@ -94,4 +79,4 @@ private: }; -#endif /* BSP_LINUX_GLOBALCONFIGHANDLER_H_ */ +#endif /* MISSION_UTILITY_GLOBALCONFIGHANDLER_H_ */ diff --git a/unittest/controller/testConfigFileHandler.cpp b/unittest/controller/testConfigFileHandler.cpp index a76a9efe..30365863 100644 --- a/unittest/controller/testConfigFileHandler.cpp +++ b/unittest/controller/testConfigFileHandler.cpp @@ -11,9 +11,51 @@ TEST_CASE("Configfile Handler", "[ConfigHandler]") { - GlobalConfigHandler confighandler= GlobalConfigHandler(objects::CONFIG_TEST,"JSON.config"); sif::debug<<"Testcase config file handler"<