2022-06-07 09:54:52 +02:00
|
|
|
/*
|
|
|
|
* GlobalConfigHandler.cpp
|
|
|
|
*
|
|
|
|
* Created on: May 3, 2022
|
2022-07-05 12:57:50 +02:00
|
|
|
* Author: Jona Petri (IRS)
|
2022-06-07 09:54:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "GlobalConfigHandler.h"
|
2022-07-14 17:35:49 +02:00
|
|
|
|
2022-06-08 11:08:19 +02:00
|
|
|
#include <fsfw/ipc/MessageQueueIF.h>
|
2022-07-14 17:35:49 +02:00
|
|
|
#include <fsfw/ipc/MutexFactory.h>
|
2022-06-08 11:08:19 +02:00
|
|
|
#include <fsfw/ipc/QueueFactory.h>
|
2022-06-07 09:54:52 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
#include "fsfw/serviceinterface/ServiceInterface.h"
|
2022-07-05 12:57:50 +02:00
|
|
|
|
2022-09-29 13:20:05 +02:00
|
|
|
MutexIF* GlobalConfigHandler::CONFIG_LOCK = nullptr;
|
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
GlobalConfigHandler::GlobalConfigHandler(object_id_t objectId, std::string configFilePath)
|
|
|
|
: SystemObject(objectId),
|
|
|
|
NVMParameterBase(configFilePath),
|
|
|
|
commandQueue(QueueFactory::instance()->createMessageQueue(20)) {
|
2022-09-29 13:20:05 +02:00
|
|
|
if (CONFIG_LOCK == nullptr) {
|
|
|
|
CONFIG_LOCK = MutexFactory::instance()->createMutex();
|
2022-07-14 17:35:49 +02:00
|
|
|
}
|
2022-06-07 09:54:52 +02:00
|
|
|
}
|
2022-07-14 17:35:49 +02:00
|
|
|
ReturnValue_t GlobalConfigHandler::initialize() {
|
|
|
|
ReturnValue_t result = SystemObject::initialize();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-07-05 10:02:43 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::initialize: SystemObject::initialize() failed with "
|
|
|
|
<< result << std::endl;
|
2022-07-05 10:02:43 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
2022-06-08 11:08:19 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
result = ReadConfigFile();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-06-07 09:54:52 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::initialize: Creating JSON file at " << getFullName()
|
|
|
|
<< std::endl;
|
2022-06-07 09:54:52 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
result = ResetConfigFile();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2022-06-08 11:08:19 +02:00
|
|
|
|
2022-06-07 09:54:52 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::initialize success " << std::endl;
|
2022-06-07 09:54:52 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
2022-06-07 09:54:52 +02:00
|
|
|
}
|
2022-07-14 17:35:49 +02:00
|
|
|
GlobalConfigHandler::~GlobalConfigHandler() {}
|
2022-06-07 09:54:52 +02:00
|
|
|
|
|
|
|
ReturnValue_t GlobalConfigHandler::performOperation(uint8_t operationCode) {
|
2022-09-27 19:38:35 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::debug << "GlobalConfigHandler::performOperation" << std::endl;
|
2022-06-07 09:54:52 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
ReturnValue_t GlobalConfigHandler::lockConfigFile() {
|
2022-09-27 19:38:35 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-09-29 13:20:05 +02:00
|
|
|
result = CONFIG_LOCK->lockMutex(MutexIF::TimeoutType::WAITING, 10);
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
2022-06-07 09:54:52 +02:00
|
|
|
}
|
2022-07-14 17:35:49 +02:00
|
|
|
ReturnValue_t GlobalConfigHandler::unlockConfigFile() {
|
2022-09-27 19:38:35 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-09-29 13:20:05 +02:00
|
|
|
result = CONFIG_LOCK->unlockMutex();
|
2022-06-07 09:54:52 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
template <typename T>
|
|
|
|
ReturnValue_t GlobalConfigHandler::setConfigFileValue(ParamIds paramID, T data) {
|
2022-09-27 19:38:35 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
|
|
|
ReturnValue_t resultSet = returnvalue::OK;
|
2022-06-08 08:27:45 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
result = lockConfigFile();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-06-08 08:27:45 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::setConfigFileValue lock mutex failed with " << result
|
|
|
|
<< std::endl;
|
2022-06-08 08:27:45 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
2022-06-08 08:27:45 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
std::string paramString;
|
|
|
|
paramString = PARAM_KEY_MAP[paramID];
|
2022-07-05 14:56:31 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
// Check if key exists in map before setting value. No check is done in setValue! Somehow
|
|
|
|
// PARAM_KEY_MAP.count(paramID) == 0 does not work
|
|
|
|
if (paramString.empty() == true) {
|
2022-07-05 14:56:31 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::setConfigFileValue ParamId " << PARAM_KEY_MAP[paramID]
|
|
|
|
<< " not found!" << std::endl;
|
2022-07-05 14:56:31 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
triggerEvent(SET_CONFIGFILEVALUE_FAILED, 1, 0);
|
2022-09-27 19:38:35 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-07-14 17:35:49 +02:00
|
|
|
}
|
2022-07-05 14:56:31 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
resultSet = setValue(PARAM_KEY_MAP[paramID], data);
|
2022-09-27 19:38:35 +02:00
|
|
|
if (resultSet != returnvalue::OK) {
|
2022-07-14 17:35:49 +02:00
|
|
|
triggerEvent(SET_CONFIGFILEVALUE_FAILED, 0, 0);
|
2022-06-08 08:27:45 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::setConfigFileValue set json failed with " << resultSet
|
|
|
|
<< std::endl;
|
2022-06-08 08:27:45 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
}
|
2022-06-08 08:27:45 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
result = unlockConfigFile();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-06-08 08:27:45 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::setConfigFileValue unlock mutex failed with " << result
|
|
|
|
<< std::endl;
|
2022-06-08 08:27:45 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
2022-06-08 08:27:45 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
return resultSet;
|
2022-06-07 09:54:52 +02:00
|
|
|
}
|
2022-07-14 17:35:49 +02:00
|
|
|
template <typename T>
|
|
|
|
ReturnValue_t GlobalConfigHandler::getConfigFileValue(ParamIds paramID, T& data) {
|
2022-09-27 19:38:35 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
|
|
|
ReturnValue_t resultGet = returnvalue::OK;
|
2022-06-08 08:27:45 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
result = lockConfigFile();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-06-08 08:27:45 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::getConfigFileValue lock mutex failed with " << result
|
|
|
|
<< std::endl;
|
2022-06-08 08:27:45 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
2022-06-08 08:27:45 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
resultGet = getValue(PARAM_KEY_MAP[paramID], data);
|
2022-09-27 19:38:35 +02:00
|
|
|
if (resultGet != returnvalue::OK) {
|
2022-07-14 17:35:49 +02:00
|
|
|
triggerEvent(GET_CONFIGFILEVALUE_FAILED, 0, 0);
|
2022-06-08 08:27:45 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::getConfigFileValue getValue failed with " << resultGet
|
|
|
|
<< std::endl;
|
2022-06-08 08:27:45 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
}
|
|
|
|
result = unlockConfigFile();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-06-08 08:27:45 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::getConfigFileValue unlock mutex failed with " << result
|
|
|
|
<< std::endl;
|
2022-06-08 08:27:45 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
2022-06-08 08:27:45 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
return resultGet;
|
2022-06-07 09:54:52 +02:00
|
|
|
}
|
2022-06-08 08:27:45 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
ReturnValue_t GlobalConfigHandler::resetConfigFileValues() {
|
2022-09-27 19:38:35 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-07-14 17:35:49 +02:00
|
|
|
result = lockConfigFile();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-06-07 09:54:52 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::resetConfigFileValues lock mutex failed with " << result
|
|
|
|
<< std::endl;
|
2022-06-07 09:54:52 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
insertValue(PARAM_KEY_MAP[PARAM0], PARAM0_DEFAULT);
|
|
|
|
insertValue(PARAM_KEY_MAP[PARAM1], PARAM1_DEFAULT);
|
2022-07-05 14:56:31 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
result = unlockConfigFile();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-06-07 09:54:52 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::resetConfigFileValues unlock mutex failed with " << result
|
|
|
|
<< std::endl;
|
2022-06-07 09:54:52 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return result;
|
2022-06-07 09:54:52 +02:00
|
|
|
}
|
2022-07-14 17:35:49 +02:00
|
|
|
ReturnValue_t GlobalConfigHandler::WriteConfigFile() {
|
2022-09-27 19:38:35 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
|
|
|
ReturnValue_t resultWrite = returnvalue::OK;
|
2022-07-14 17:35:49 +02:00
|
|
|
result = lockConfigFile();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-06-07 09:54:52 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::WriteConfigFile lock mutex failed with " << result
|
|
|
|
<< std::endl;
|
2022-06-07 09:54:52 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
2022-06-07 09:54:52 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
resultWrite = writeJsonFile();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (resultWrite != returnvalue::OK) {
|
2022-07-14 17:35:49 +02:00
|
|
|
triggerEvent(WRITE_CONFIGFILE_FAILED, 0, 0);
|
2022-06-07 09:54:52 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::WriteConfigFile write json failed with " << resultWrite
|
|
|
|
<< std::endl;
|
2022-06-07 09:54:52 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
}
|
2022-06-07 09:54:52 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
result = unlockConfigFile();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-06-07 09:54:52 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::WriteConfigFile unlock mutex failed with " << result
|
|
|
|
<< std::endl;
|
2022-06-07 09:54:52 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return resultWrite;
|
2022-06-07 09:54:52 +02:00
|
|
|
}
|
2022-07-14 17:35:49 +02:00
|
|
|
ReturnValue_t GlobalConfigHandler::ReadConfigFile() {
|
2022-09-27 19:38:35 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
|
|
|
ReturnValue_t resultRead = returnvalue::OK;
|
2022-07-14 17:35:49 +02:00
|
|
|
result = lockConfigFile();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-06-07 09:54:52 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::ReadConfigFile lock mutex failed with " << result
|
|
|
|
<< std::endl;
|
2022-06-07 09:54:52 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
2022-06-07 09:54:52 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
resultRead = readJsonFile();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (resultRead != returnvalue::OK) {
|
2022-07-14 17:35:49 +02:00
|
|
|
triggerEvent(READ_CONFIGFILE_FAILED, 0, 0);
|
2022-06-07 09:54:52 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::ReadConfigFile read json failed with " << resultRead
|
|
|
|
<< std::endl;
|
2022-06-07 09:54:52 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
}
|
2022-06-07 09:54:52 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
result = unlockConfigFile();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-06-07 09:54:52 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::ReadConfigFile unlock mutex failed with " << result
|
|
|
|
<< std::endl;
|
2022-06-07 09:54:52 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
2022-06-07 09:54:52 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
return resultRead;
|
2022-06-07 09:54:52 +02:00
|
|
|
}
|
2022-07-14 17:35:49 +02:00
|
|
|
ReturnValue_t GlobalConfigHandler::ResetConfigFile() {
|
2022-09-27 19:38:35 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-07-14 17:35:49 +02:00
|
|
|
result = resetConfigFileValues();
|
2022-09-27 19:38:35 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-07-05 14:56:31 +02:00
|
|
|
#if OBSW_VERBOSE_LEVEL >= 1
|
2022-07-14 17:35:49 +02:00
|
|
|
sif::info << "GlobalConfigHandler::ResetConfigFile failed with " << result << std::endl;
|
2022-07-05 14:56:31 +02:00
|
|
|
#endif
|
2022-07-14 17:35:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
result = writeJsonFile();
|
|
|
|
return result;
|
2022-06-08 09:18:17 +02:00
|
|
|
}
|
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
ReturnValue_t GlobalConfigHandler::setConfigFileName(std::string configFileName) {
|
2022-09-27 19:38:35 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-07-14 17:35:49 +02:00
|
|
|
setFullName(configFileName);
|
|
|
|
result = ResetConfigFile();
|
|
|
|
return result;
|
2022-06-08 09:18:17 +02:00
|
|
|
}
|
2022-07-14 17:35:49 +02:00
|
|
|
std::string GlobalConfigHandler::getConfigFileName() { return getFullName(); }
|
2022-06-08 11:08:19 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
template ReturnValue_t GlobalConfigHandler::getConfigFileValue<double>(ParamIds paramID,
|
|
|
|
double& data);
|
|
|
|
template ReturnValue_t GlobalConfigHandler::getConfigFileValue<int32_t>(ParamIds paramID,
|
|
|
|
int32_t& data);
|
2022-06-08 11:08:19 +02:00
|
|
|
|
2022-07-14 17:35:49 +02:00
|
|
|
template ReturnValue_t GlobalConfigHandler::setConfigFileValue<double>(ParamIds paramID,
|
|
|
|
double data);
|
|
|
|
template ReturnValue_t GlobalConfigHandler::setConfigFileValue<int32_t>(ParamIds paramID,
|
|
|
|
int32_t data);
|