From 74b38a6d06e6bf7186f95c880e0a75cc2455100b Mon Sep 17 00:00:00 2001 From: petriVM18 Date: Tue, 7 Jun 2022 09:54:52 +0200 Subject: [PATCH] Worked on config file handler, added all functions --- bsp_hosted/CMakeLists.txt | 1 + bsp_hosted/GlobalConfigHandler.cpp | 156 ++++++++++++++++++ bsp_hosted/GlobalConfigHandler.h | 63 +++++++ bsp_hosted/InitMission.cpp | 10 ++ bsp_hosted/ObjectFactory.cpp | 3 + .../fsfwconfig/objects/systemObjectList.h | 1 + bsp_hosted/main.cpp | 1 + linux/fsfwconfig/objects/systemObjectList.h | 1 + 8 files changed, 236 insertions(+) create mode 100644 bsp_hosted/GlobalConfigHandler.cpp create mode 100644 bsp_hosted/GlobalConfigHandler.h diff --git a/bsp_hosted/CMakeLists.txt b/bsp_hosted/CMakeLists.txt index 7787cf7e..88710bd1 100644 --- a/bsp_hosted/CMakeLists.txt +++ b/bsp_hosted/CMakeLists.txt @@ -2,6 +2,7 @@ target_sources(${OBSW_NAME} PUBLIC InitMission.cpp main.cpp ObjectFactory.cpp + GlobalConfigHandler.cpp ) add_subdirectory(fsfwconfig) diff --git a/bsp_hosted/GlobalConfigHandler.cpp b/bsp_hosted/GlobalConfigHandler.cpp new file mode 100644 index 00000000..b450bfa9 --- /dev/null +++ b/bsp_hosted/GlobalConfigHandler.cpp @@ -0,0 +1,156 @@ +/* + * GlobalConfigHandler.cpp + * + * Created on: May 3, 2022 + * Author: metobs + */ + +#include "GlobalConfigHandler.h" +#include +#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"<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; +} diff --git a/bsp_hosted/GlobalConfigHandler.h b/bsp_hosted/GlobalConfigHandler.h new file mode 100644 index 00000000..9db594a3 --- /dev/null +++ b/bsp_hosted/GlobalConfigHandler.h @@ -0,0 +1,63 @@ +/* + * GlobalConfigHandler.h + * + * Created on: May 3, 2022 + * Author: metobs + */ + +#ifndef BSP_LINUX_GLOBALCONFIGHANDLER_H_ +#define BSP_LINUX_GLOBALCONFIGHANDLER_H_ + +#include +#include +#include +#include +#include +#include +#include "mission/memory/NVMParameterBase.h" +#include "OBSWConfig.h" + +static constexpr double PARAM0_DEFAULT = 5.0; +static constexpr double PARAM1_DEFAULT = 905.0; + +enum ParamIds : uint8_t { + PARAM0 = 0, + PARAM1 = 1, + +}; + +static std::map PARAM_KEY_MAP = { + {PARAM0, "Parameter0"}, + {PARAM1, "Parameter1"}, +}; +/* + * Idea: This class is intended to be used as a subclass for the Core Controller. + * Its tasks is managing a configuration JSON file containing config values important for various object. + * If some function to read or write a config value is called, a mutex should be used so only one call is done at a time. + */ +class GlobalConfigHandler: public SystemObject, public ExecutableObjectIF, public NVMParameterBase{ +public: + GlobalConfigHandler(object_id_t objectId, std::string configFilePath); + virtual ~GlobalConfigHandler(); + ReturnValue_t performOperation(uint8_t operationCode); + ReturnValue_t initialize(); + + ReturnValue_t lockConfigFile(); + ReturnValue_t unlockConfigFile(); + ReturnValue_t setConfigFileValue(); + ReturnValue_t getConfigFileValue(); + ReturnValue_t insertConfigFileValue(); + ReturnValue_t resetConfigFileValues(); + ReturnValue_t WriteConfigFile(); + ReturnValue_t ReadConfigFile(); + ReturnValue_t ResetConfigFile(); + +private: + std::string configFilePath; + static MutexIF* configLock ; + + + +}; + +#endif /* BSP_LINUX_GLOBALCONFIGHANDLER_H_ */ diff --git a/bsp_hosted/InitMission.cpp b/bsp_hosted/InitMission.cpp index 0ca59db5..b1463967 100644 --- a/bsp_hosted/InitMission.cpp +++ b/bsp_hosted/InitMission.cpp @@ -138,6 +138,15 @@ void initmission::initTasks() { } #endif /* OBSW_ADD_TEST_CODE == 1 */ + PeriodicTaskIF* configTask = factory->createPeriodicTask( + "CONFIG_TASK", 40, PeriodicTaskIF::MINIMUM_STACK_SIZE, 2.0, missedDeadlineFunc); + + result = configTask->addComponent(objects::CONFIG_TEST); + if (result != HasReturnvaluesIF::RETURN_OK) { + initmission::printAddObjectError("CONFIG_TASK", objects::CONFIG_TEST); + } + + sif::info << "Starting tasks.." << std::endl; tmTcDistributor->startTask(); tmtcBridgeTask->startTask(); @@ -148,6 +157,7 @@ void initmission::initTasks() { pusHighPrio->startTask(); pusMedPrio->startTask(); pusLowPrio->startTask(); + configTask->startTask(); #if OBSW_ADD_TEST_CODE == 1 testTask->startTask(); diff --git a/bsp_hosted/ObjectFactory.cpp b/bsp_hosted/ObjectFactory.cpp index e1be7588..91fbb509 100644 --- a/bsp_hosted/ObjectFactory.cpp +++ b/bsp_hosted/ObjectFactory.cpp @@ -24,6 +24,8 @@ #include #endif +#include "GlobalConfigHandler.h" + void Factory::setStaticFrameworkObjectIds() { PusServiceBase::packetSource = objects::PUS_PACKET_DISTRIBUTOR; PusServiceBase::packetDestination = objects::TM_FUNNEL; @@ -44,4 +46,5 @@ void ObjectFactory::produce(void* args) { ObjectFactory::produceGenericObjects(); new TestTask(objects::TEST_TASK); + new GlobalConfigHandler(objects::CONFIG_TEST,"/path/to/JSON.config"); } diff --git a/bsp_hosted/fsfwconfig/objects/systemObjectList.h b/bsp_hosted/fsfwconfig/objects/systemObjectList.h index 91bd2bed..8eddfc4f 100644 --- a/bsp_hosted/fsfwconfig/objects/systemObjectList.h +++ b/bsp_hosted/fsfwconfig/objects/systemObjectList.h @@ -21,6 +21,7 @@ enum sourceObjects : uint32_t { /* Test Task */ TEST_TASK = 0x42694269, + CONFIG_TEST = 0x42694270, DUMMY_INTERFACE = 0xCAFECAFE, DUMMY_HANDLER = 0x4400AFFE, diff --git a/bsp_hosted/main.cpp b/bsp_hosted/main.cpp index 938adb76..cdf1bdb4 100644 --- a/bsp_hosted/main.cpp +++ b/bsp_hosted/main.cpp @@ -23,6 +23,7 @@ int main(void) { << " v" << common::OBSW_VERSION << " | FSFW v" << fsfw::FSFW_VERSION << " --" << std::endl; std::cout << "-- " << __DATE__ << " " << __TIME__ << " --" << std::endl; + std::cout << "-- " <<" BSP HOSTED"<< " --" << std::endl; initmission::initMission(); diff --git a/linux/fsfwconfig/objects/systemObjectList.h b/linux/fsfwconfig/objects/systemObjectList.h index a03e4d38..ec711711 100644 --- a/linux/fsfwconfig/objects/systemObjectList.h +++ b/linux/fsfwconfig/objects/systemObjectList.h @@ -59,6 +59,7 @@ enum sourceObjects : uint32_t { /* 0x54 ('T') for test handlers */ TEST_TASK = 0x54694269, + CONFIG_TEST = 0x54694270, LIBGPIOD_TEST = 0x54123456, SPI_TEST = 0x54000010, UART_TEST = 0x54000020,