157 lines
4.4 KiB
C++
157 lines
4.4 KiB
C++
|
/*
|
||
|
* GlobalConfigHandler.cpp
|
||
|
*
|
||
|
* Created on: May 3, 2022
|
||
|
* Author: metobs
|
||
|
*/
|
||
|
|
||
|
#include "GlobalConfigHandler.h"
|
||
|
#include <fsfw/ipc/MutexFactory.h>
|
||
|
#include "fsfw/serviceinterface/ServiceInterface.h"
|
||
|
|
||
|
MutexIF* GlobalConfigHandler::configLock = nullptr;
|
||
|
GlobalConfigHandler::GlobalConfigHandler(object_id_t objectId, std::string configFilePath): SystemObject(objectId), NVMParameterBase(configFilePath){
|
||
|
if (configLock == nullptr) {
|
||
|
configLock = MutexFactory::instance()->createMutex();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
ReturnValue_t GlobalConfigHandler::initialize(){
|
||
|
|
||
|
ReturnValue_t result = SystemObject::initialize();
|
||
|
if (result != 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;
|
||
|
}
|
||
|
}
|
||
|
#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;
|
||
|
}
|
||
|
|
||
|
ReturnValue_t GlobalConfigHandler::lockConfigFile(){
|
||
|
ReturnValue_t result = RETURN_OK;
|
||
|
result = configLock->lockMutex(MutexIF::TimeoutType::WAITING, 0);
|
||
|
return result;
|
||
|
}
|
||
|
ReturnValue_t GlobalConfigHandler::unlockConfigFile(){
|
||
|
ReturnValue_t result = RETURN_OK;
|
||
|
result=configLock->unlockMutex();
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
//TBD for set/get/insert: Mutx lock, set/get/insert, Mutex unlock
|
||
|
//Use template functions for different data types
|
||
|
ReturnValue_t GlobalConfigHandler::setConfigFileValue(){
|
||
|
ReturnValue_t result = RETURN_OK;
|
||
|
return result;
|
||
|
}
|
||
|
ReturnValue_t GlobalConfigHandler::getConfigFileValue(){
|
||
|
ReturnValue_t result = RETURN_OK;
|
||
|
return result;
|
||
|
}
|
||
|
ReturnValue_t GlobalConfigHandler::insertConfigFileValue(){
|
||
|
ReturnValue_t result = RETURN_OK;
|
||
|
return result;
|
||
|
}
|
||
|
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){
|
||
|
#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){
|
||
|
#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;
|
||
|
}
|