2022-06-07 09:54:52 +02:00
|
|
|
/*
|
|
|
|
* GlobalConfigHandler.cpp
|
|
|
|
*
|
|
|
|
* Created on: May 3, 2022
|
|
|
|
* Author: metobs
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "GlobalConfigHandler.h"
|
|
|
|
#include <fsfw/ipc/MutexFactory.h>
|
|
|
|
#include "fsfw/serviceinterface/ServiceInterface.h"
|
2022-06-08 11:08:19 +02:00
|
|
|
#include <fsfw/ipc/MessageQueueIF.h>
|
|
|
|
#include <fsfw/ipc/QueueFactory.h>
|
2022-06-07 09:54:52 +02:00
|
|
|
|
|
|
|
MutexIF* GlobalConfigHandler::configLock = nullptr;
|
2022-06-08 11:08:19 +02:00
|
|
|
GlobalConfigHandler::GlobalConfigHandler(object_id_t objectId, std::string configFilePath):
|
|
|
|
SystemObject(objectId),
|
|
|
|
NVMParameterBase(configFilePath),
|
2022-07-05 10:02:43 +02:00
|
|
|
commandQueue(QueueFactory::instance()->createMessageQueue(20))
|
|
|
|
{
|
2022-06-07 09:54:52 +02:00
|
|
|
if (configLock == nullptr) {
|
|
|
|
configLock = MutexFactory::instance()->createMutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
ReturnValue_t GlobalConfigHandler::initialize(){
|
|
|
|
|
|
|
|
ReturnValue_t result = SystemObject::initialize();
|
|
|
|
if (result != RETURN_OK) {
|
2022-07-05 10:02:43 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
|
|
|
sif::info << "GlobalConfigHandler::initialize: SystemObject::initialize() failed with " << result << std::endl;
|
|
|
|
#endif
|
2022-06-07 09:54:52 +02:00
|
|
|
return result;
|
|
|
|
}
|
2022-06-08 11:08:19 +02:00
|
|
|
|
|
|
|
|
2022-06-07 09:54:52 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2022-06-08 11:08:19 +02:00
|
|
|
|
|
|
|
|
2022-06-07 09:54:52 +02:00
|
|
|
#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"<<std::endl;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-06-08 12:53:41 +02:00
|
|
|
|
2022-06-08 11:08:19 +02:00
|
|
|
|
2022-06-07 09:54:52 +02:00
|
|
|
ReturnValue_t GlobalConfigHandler::lockConfigFile(){
|
|
|
|
ReturnValue_t result = RETURN_OK;
|
2022-06-08 12:53:41 +02:00
|
|
|
result = configLock->lockMutex(MutexIF::TimeoutType::WAITING, 10);
|
2022-06-07 09:54:52 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
ReturnValue_t GlobalConfigHandler::unlockConfigFile(){
|
|
|
|
ReturnValue_t result = RETURN_OK;
|
|
|
|
result=configLock->unlockMutex();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-06-08 08:27:45 +02:00
|
|
|
template <typename T> ReturnValue_t GlobalConfigHandler::setConfigFileValue(std::string key, T data){
|
2022-06-07 09:54:52 +02:00
|
|
|
ReturnValue_t result = RETURN_OK;
|
2022-06-08 08:27:45 +02:00
|
|
|
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){
|
2022-06-08 09:18:17 +02:00
|
|
|
triggerEvent(SET_CONFIGFILEVALUE_FAILED, 0, 0);
|
2022-06-08 08:27:45 +02:00
|
|
|
#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;
|
2022-06-07 09:54:52 +02:00
|
|
|
}
|
2022-06-08 08:27:45 +02:00
|
|
|
template <typename T> ReturnValue_t GlobalConfigHandler::getConfigFileValue(std::string key, T& data){
|
2022-06-07 09:54:52 +02:00
|
|
|
ReturnValue_t result = RETURN_OK;
|
2022-06-08 08:27:45 +02:00
|
|
|
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){
|
2022-06-08 09:18:17 +02:00
|
|
|
triggerEvent(GET_CONFIGFILEVALUE_FAILED, 0, 0);
|
2022-06-08 08:27:45 +02:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2022-06-07 09:54:52 +02:00
|
|
|
return result;
|
|
|
|
}
|
2022-06-08 08:27:45 +02:00
|
|
|
template <typename T> ReturnValue_t GlobalConfigHandler::insertConfigFileValue(std::string key, T data){
|
2022-06-07 09:54:52 +02:00
|
|
|
ReturnValue_t result = RETURN_OK;
|
2022-06-08 08:27:45 +02:00
|
|
|
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){
|
2022-06-08 09:18:17 +02:00
|
|
|
triggerEvent(INSERT_CONFIGFILEVALUE_FAILED, 0, 0);
|
2022-06-08 08:27:45 +02:00
|
|
|
#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;
|
2022-06-07 09:54:52 +02:00
|
|
|
}
|
|
|
|
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){
|
2022-06-08 09:18:17 +02:00
|
|
|
triggerEvent(WRITE_CONFIGFILE_FAILED, 0, 0);
|
2022-06-07 09:54:52 +02:00
|
|
|
#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){
|
2022-06-08 09:18:17 +02:00
|
|
|
triggerEvent(READ_CONFIGFILE_FAILED, 0, 0);
|
2022-06-07 09:54:52 +02:00
|
|
|
#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;
|
|
|
|
}
|
2022-06-08 09:18:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
ReturnValue_t GlobalConfigHandler::setConfigFileName(std::string configFileName){
|
|
|
|
ReturnValue_t result = RETURN_OK;
|
|
|
|
setFullName(configFileName);
|
|
|
|
result=ResetConfigFile();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
std::string GlobalConfigHandler::getConfigFileName(){
|
|
|
|
|
|
|
|
return getFullName();
|
|
|
|
}
|
2022-06-08 11:08:19 +02:00
|
|
|
|
|
|
|
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<ParamIds>(uniqueId)],
|
|
|
|
parameterWrapper, newValues);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(ParamIds::PARAM1):{
|
|
|
|
result=handleIntParamUpdate(PARAM_KEY_MAP[static_cast<ParamIds>(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<double>(&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
|
2022-07-05 10:02:43 +02:00
|
|
|
|
2022-06-08 11:08:19 +02:00
|
|
|
return WriteConfigFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t GlobalConfigHandler::handleIntParamUpdate(std::string key,
|
|
|
|
ParameterWrapper* parameterWrapper,
|
|
|
|
const ParameterWrapper* newValues) {
|
|
|
|
int newValue = 0;
|
|
|
|
ReturnValue_t result = newValues->getElement<int>(&newValue, 0, 0);
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
result=setConfigFileValue(key, newValue);
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-07-05 10:02:43 +02:00
|
|
|
return WriteConfigFile();
|
2022-06-08 11:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|