eive-obsw/bsp_q7s/memory/LocalParameterHandler.h

105 lines
2.9 KiB
C
Raw Normal View History

2023-02-13 11:28:27 +01:00
#ifndef BSP_Q7S_MEMORY_LOCALPARAMETERHANDLER_H_
#define BSP_Q7S_MEMORY_LOCALPARAMETERHANDLER_H_
#include <string>
#include <mission/memory/NVMParameterBase.h>
#include <mission/memory/SdCardMountedIF.h>
/**
* @brief Class to handle persistent parameters
*
* @details Use the insertValue function to add parameters
*/
class LocalParameterHandler : public NVMParameterBase {
public:
static constexpr uint8_t INTERFACE_ID = CLASS_ID::LOCAL_PARAM_HANDLER;
static constexpr ReturnValue_t SD_NOT_READY = returnvalue::makeCode(INTERFACE_ID, 0);
2023-02-13 11:28:27 +01:00
/**
* @brief Constructor
*
* @param sdRelativeName Absolute name of json file relative to mount
* directory of SD card. E.g. conf/example.json
* @param sdcMan Pointer to SD card manager
*/
LocalParameterHandler(std::string sdRelativeName, SdCardMountedIF* sdcMan);
virtual ~LocalParameterHandler();
/**
* @brief Will initialize the local parameter handler
*
* @return OK if successful, otherwise error return value
*/
ReturnValue_t initialize();
/**
2023-02-21 14:13:46 +01:00
* @brief Function to add parameter to json file. If the json file does
* not yet exist it will be created here.
2023-02-13 11:28:27 +01:00
*
* @param key The string to identify the parameter
* @param value The value to set for this parameter
*
* @return OK if successful, otherwise error return value
*
* @details The function will add the parameter only if it is not already
* present in the json file
*/
template<typename T> ReturnValue_t addParameter(std::string key, T value);
2023-02-21 14:13:46 +01:00
/**
* @brief Function will update a parameter which already exists in the json
* file
*
* @param key The unique string to identify the parameter to update
* @param value The new new value to set
*
* @return OK if successful, otherwise error return value
*/
template<typename T> ReturnValue_t updateParameter(std::string key, T value);
2023-02-13 11:28:27 +01:00
private:
// Name relative to mount point of SD card where parameters will be stored
std::string sdRelativeName;
SdCardMountedIF* sdcMan;
virtual ReturnValue_t writeJsonFile();
/**
* @brief This function sets the name of the json file dependent on the
* currently active SD card
*
* @return OK if successful, otherwise error return value
*/
ReturnValue_t updateFullName();
2023-02-13 11:28:27 +01:00
};
template<typename T> inline ReturnValue_t LocalParameterHandler::addParameter(std::string key, T value) {
ReturnValue_t result = insertValue(key, value);
if (result != returnvalue::OK) {
return result;
}
result = writeJsonFile();
if (result != returnvalue::OK) {
return result;
}
return returnvalue::OK;
}
2023-02-21 14:13:46 +01:00
template<typename T> inline ReturnValue_t LocalParameterHandler::updateParameter(std::string key, T value) {
ReturnValue_t result = setValue(key, value);
if (result != returnvalue::OK) {
return result;
}
result = writeJsonFile();
if (result != returnvalue::OK) {
return result;
}
return returnvalue::OK;
}
2023-02-13 11:28:27 +01:00
#endif /* BSP_Q7S_MEMORY_LOCALPARAMETERHANDLER_H_ */