eive-obsw/bsp_q7s/memory/LocalParameterHandler.h

107 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_
2023-03-08 14:50:25 +01:00
#include <mission/memory/NvmParameterBase.h>
2023-02-13 11:28:27 +01:00
#include <mission/memory/SdCardMountedIF.h>
2023-02-23 15:27:24 +01:00
#include <string>
2023-02-13 11:28:27 +01:00
/**
* @brief Class to handle persistent parameters
*
*/
class LocalParameterHandler : public NVMParameterBase {
public:
2023-02-23 15:27:24 +01:00
static constexpr uint8_t INTERFACE_ID = CLASS_ID::LOCAL_PARAM_HANDLER;
2023-02-23 15:27:24 +01:00
static constexpr ReturnValue_t SD_NOT_READY = returnvalue::makeCode(INTERFACE_ID, 0);
/**
* @brief Constructor
*
* @param sdRelativeName Absolute name of json file relative to mount
2023-02-28 19:14:15 +01:00
* directory
* of SD card. E.g. conf/example.json
2023-02-23 15:27:24 +01:00
* @param sdcMan Pointer to SD card manager
*/
2023-02-13 11:28:27 +01:00
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
*/
2023-02-23 15:27:24 +01:00
template <typename T>
ReturnValue_t addParameter(std::string key, T value);
2023-02-13 11:28:27 +01:00
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
*/
2023-02-23 15:27:24 +01:00
template <typename T>
ReturnValue_t updateParameter(std::string key, T value);
2023-02-21 14:13:46 +01:00
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
};
2023-02-23 15:27:24 +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-13 11:28:27 +01:00
}
2023-02-23 15:27:24 +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-21 14:13:46 +01:00
}
2023-02-13 11:28:27 +01:00
#endif /* BSP_Q7S_MEMORY_LOCALPARAMETERHANDLER_H_ */