/* * GlobalConfigHandler.cpp * * Created on: May 3, 2022 * Author: metobs */ #include "GlobalConfigHandler.h" #include #include "fsfw/serviceinterface/ServiceInterface.h" #include #include MutexIF* GlobalConfigHandler::configLock = nullptr; GlobalConfigHandler::GlobalConfigHandler(object_id_t objectId, std::string configFilePath): SystemObject(objectId), NVMParameterBase(configFilePath), parameterHelper(this), commandQueue(QueueFactory::instance()->createMessageQueue(20)), actionHelper(this, commandQueue){ if (configLock == nullptr) { configLock = MutexFactory::instance()->createMutex(); } } ReturnValue_t GlobalConfigHandler::initialize(){ ReturnValue_t result = SystemObject::initialize(); if (result != RETURN_OK) { return result; } result = actionHelper.initialize(commandQueue); if(result != HasReturnvaluesIF::RETURN_OK) { return result; } result = ReadConfigFile(); if(result!=RETURN_OK){ #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::initialize: Creating JSON file at " << getFullName() << std::endl; #endif result=ResetConfigFile(); if (result != RETURN_OK) { return result; } } result = parameterHelper.initialize(); if (result != HasReturnvaluesIF::RETURN_OK) { #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::initialize: parameterHelper init failed with " << result << std::endl; #endif return result; } #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::initialize success " << std::endl; #endif return result; } GlobalConfigHandler::~GlobalConfigHandler() { } ReturnValue_t GlobalConfigHandler::performOperation(uint8_t operationCode) { ReturnValue_t result = RETURN_OK; sif::debug<<"GlobalConfigHandler::performOperation"<receiveMessage(&command); if (result != RETURN_OK) { return; } result = parameterHelper.handleParameterMessage(&command); if (result == RETURN_OK) { return; } result = actionHelper.handleActionMessage(&command); if (result == HasReturnvaluesIF::RETURN_OK) { sif::debug<<"GlobalConfigHandler::readCommandQueue handleActionMessage success"<lockMutex(MutexIF::TimeoutType::WAITING, 10); return result; } ReturnValue_t GlobalConfigHandler::unlockConfigFile(){ ReturnValue_t result = RETURN_OK; result=configLock->unlockMutex(); return result; } template ReturnValue_t GlobalConfigHandler::setConfigFileValue(std::string key, T data){ ReturnValue_t result = RETURN_OK; ReturnValue_t resultSet = RETURN_OK; result=lockConfigFile(); if (result!= RETURN_OK){ #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::setConfigFileValue lock mutex failed with " << result << std::endl; #endif return result; } resultSet=setValue(key, data); if(resultSet!=RETURN_OK){ triggerEvent(SET_CONFIGFILEVALUE_FAILED, 0, 0); #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::setConfigFileValue set json failed with " << result << std::endl; #endif } result=unlockConfigFile(); if (result!= RETURN_OK){ #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::setConfigFileValue unlock mutex failed with " << result << std::endl; #endif return result; } return resultSet; } template ReturnValue_t GlobalConfigHandler::getConfigFileValue(std::string key, T& data){ ReturnValue_t result = RETURN_OK; ReturnValue_t resultGet = RETURN_OK; result=lockConfigFile(); if (result!= RETURN_OK){ #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::getConfigFileValue lock mutex failed with " << result << std::endl; #endif return result; } resultGet=getValue(key, data); if (resultGet!= RETURN_OK){ triggerEvent(GET_CONFIGFILEVALUE_FAILED, 0, 0); #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::getConfigFileValue lock mutex failed with " << result << std::endl; #endif } result=unlockConfigFile(); if (result!= RETURN_OK){ #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::getConfigFileValue unlock mutex failed with " << result << std::endl; #endif return result; } 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(); if (result!= RETURN_OK){ #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::resetConfigFileValues lock mutex failed with " << result << std::endl; #endif return result; } insertValue(PARAM_KEY_MAP[PARAM0], PARAM0_DEFAULT); insertValue(PARAM_KEY_MAP[PARAM1], PARAM1_DEFAULT); result=unlockConfigFile(); if (result!= RETURN_OK){ #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::resetConfigFileValues unlock mutex failed with " << result << std::endl; #endif return result; } return result; } ReturnValue_t GlobalConfigHandler::WriteConfigFile(){ ReturnValue_t result = RETURN_OK; ReturnValue_t resultWrite = RETURN_OK; result=lockConfigFile(); if (result!= RETURN_OK){ #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::WriteConfigFile lock mutex failed with " << result << std::endl; #endif return result; } resultWrite =writeJsonFile(); if(resultWrite!=RETURN_OK){ triggerEvent(WRITE_CONFIGFILE_FAILED, 0, 0); #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::WriteConfigFile write json failed with " << result << std::endl; #endif } result=unlockConfigFile(); if (result!= RETURN_OK){ #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::WriteConfigFile unlock mutex failed with " << result << std::endl; #endif return result; } return resultWrite; } ReturnValue_t GlobalConfigHandler::ReadConfigFile(){ ReturnValue_t result = RETURN_OK; ReturnValue_t resultRead = RETURN_OK; result=lockConfigFile(); if (result!= RETURN_OK){ #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::ReadConfigFile lock mutex failed with " << result << std::endl; #endif return result; } resultRead=readJsonFile(); if(resultRead!=RETURN_OK){ triggerEvent(READ_CONFIGFILE_FAILED, 0, 0); #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::ReadConfigFile read json failed with " << result << std::endl; #endif } result=unlockConfigFile(); if (result!= RETURN_OK){ #if OBSW_VERBOSE_LEVEL >= 1 sif::info << "GlobalConfigHandler::ReadConfigFile unlock mutex failed with " << result << std::endl; #endif return result; } return resultRead; } ReturnValue_t GlobalConfigHandler::ResetConfigFile(){ ReturnValue_t result = RETURN_OK; resetConfigFileValues(); result =writeJsonFile(); return result; } ReturnValue_t GlobalConfigHandler::setConfigFileName(std::string configFileName){ ReturnValue_t result = RETURN_OK; setFullName(configFileName); result=ResetConfigFile(); return result; } 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 doubleDummy = newValue; parameterWrapper->set(doubleDummy); 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; } // Do this so the dumping and loading with the framework works as well intDummy = newValue; parameterWrapper->set(intDummy); return WriteConfigFile(); } MessageQueueId_t GlobalConfigHandler::getCommandQueue() const{ return commandQueue->getId(); } ReturnValue_t GlobalConfigHandler::executeAction(ActionId_t actionId, MessageQueueId_t commandedBy, const uint8_t* data, size_t size) { //Currently, no commands are used return RETURN_FAILED; }