eive-obsw/mission/memory/NVMParameterBase.h

86 lines
2.1 KiB
C
Raw Normal View History

2021-07-16 21:51:30 +02:00
#ifndef BSP_Q7S_CORE_NVMPARAMS_NVMPARAMIF_H_
#define BSP_Q7S_CORE_NVMPARAMS_NVMPARAMIF_H_
2022-01-17 15:58:27 +01:00
#include <filesystem>
2021-07-16 21:51:30 +02:00
#include <nlohmann/json.hpp>
#include <string>
2022-08-24 17:27:47 +02:00
#include "fsfw/returnvalues/returnvalue.h"
2022-09-14 12:47:15 +02:00
#include "returnvalues/classIds.h"
2021-07-16 21:51:30 +02:00
2022-08-24 17:27:47 +02:00
class NVMParameterBase {
2022-01-17 15:58:27 +01:00
public:
virtual ~NVMParameterBase() {}
2021-07-16 21:51:30 +02:00
2022-01-17 15:58:27 +01:00
NVMParameterBase(std::string fullName);
2021-07-16 21:51:30 +02:00
/**
* @brief Use this constructor when name of json file shall be set on an later
* point
*/
NVMParameterBase();
2022-01-17 15:58:27 +01:00
bool getJsonFileExists();
2021-07-16 21:51:30 +02:00
2022-01-17 15:58:27 +01:00
/**
2022-08-24 17:27:47 +02:00
* Returns returnvalue::OK on successfull read and HasFileSystemIF::FILE_DOES_NOT_EXIST if
2022-01-17 15:58:27 +01:00
* file does not exist yet.
* @return
*/
virtual ReturnValue_t readJsonFile();
2021-07-16 21:51:30 +02:00
2022-01-17 15:58:27 +01:00
virtual ReturnValue_t writeJsonFile();
2021-07-16 21:51:30 +02:00
virtual void setFullName(std::string fullName);
2022-01-17 15:58:27 +01:00
std::string getFullName() const;
2021-07-16 21:51:30 +02:00
2022-01-17 15:58:27 +01:00
template <typename T>
ReturnValue_t insertValue(std::string key, T value);
2021-07-16 21:51:30 +02:00
2022-01-17 15:58:27 +01:00
template <typename T>
ReturnValue_t setValue(std::string key, T value);
2021-07-16 21:51:30 +02:00
2022-01-17 15:58:27 +01:00
template <typename T>
ReturnValue_t getValue(std::string key, T& value) const;
2021-07-16 21:51:30 +02:00
2022-01-17 15:58:27 +01:00
void printKeys() const;
void print() const;
2021-12-29 20:33:20 +01:00
2022-01-17 15:58:27 +01:00
private:
static const uint8_t INTERFACE_ID = CLASS_ID::NVM_PARAM_BASE;
2021-12-29 20:33:20 +01:00
2022-01-17 15:58:27 +01:00
//! [EXPORT] : [COMMENT] Specified key does not exist in json file
static const ReturnValue_t KEY_NOT_EXISTS = MAKE_RETURN_CODE(0xA0);
2021-12-29 20:33:20 +01:00
2022-01-17 15:58:27 +01:00
nlohmann::json json;
std::vector<std::string> keys;
std::string fullName = "";
2021-07-16 21:51:30 +02:00
};
2022-01-17 15:58:27 +01:00
template <typename T>
2021-07-16 21:51:30 +02:00
inline ReturnValue_t NVMParameterBase::insertValue(std::string key, T value) {
2022-01-17 15:58:27 +01:00
// Check whether key already exists. If it does not, insert it
if (std::find(keys.begin(), keys.end(), key) == keys.end()) {
keys.push_back(key);
}
json[key] = value;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-07-16 21:51:30 +02:00
}
2022-01-17 15:58:27 +01:00
template <typename T>
2021-07-16 21:51:30 +02:00
inline ReturnValue_t NVMParameterBase::setValue(std::string key, T value) {
2023-02-23 15:27:24 +01:00
json[key] = value;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-07-16 21:51:30 +02:00
}
2022-01-17 15:58:27 +01:00
template <typename T>
inline ReturnValue_t NVMParameterBase::getValue(std::string key, T& value) const {
2022-01-17 15:58:27 +01:00
if (!json.contains(key)) {
return KEY_NOT_EXISTS;
}
value = json[key];
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-07-16 21:51:30 +02:00
}
#endif /* BSP_Q7S_CORE_NVMPARAMS_NVMPARAMIF_H_ */