eive-obsw/bsp_q7s/memory/scratchApi.h

147 lines
4.1 KiB
C
Raw Normal View History

2021-07-09 16:57:34 +02:00
#ifndef BSP_Q7S_MEMORY_SCRATCHAPI_H_
#define BSP_Q7S_MEMORY_SCRATCHAPI_H_
2022-01-17 15:58:27 +01:00
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <type_traits>
2022-08-24 17:27:47 +02:00
#include "fsfw/returnvalues/returnvalue.h"
2021-07-09 17:21:26 +02:00
#include "fsfw/serviceinterface/ServiceInterface.h"
2021-07-09 16:57:34 +02:00
#include "linux/utility/utility.h"
#include "returnvalues/classIds.h"
2021-07-09 16:57:34 +02:00
/**
* @brief API for the scratch buffer
*/
namespace scratch {
static constexpr char PREFERED_SDC_KEY[] = "PREFSD";
2021-07-19 12:44:43 +02:00
static constexpr char ALLOC_FAILURE_COUNT[] = "ALLOCERR";
static constexpr uint8_t INTERFACE_ID = CLASS_ID::SCRATCH_BUFFER;
2022-08-24 17:27:47 +02:00
static constexpr ReturnValue_t KEY_NOT_FOUND = returnvalue::makeCode(INTERFACE_ID, 0);
2021-07-16 22:20:53 +02:00
ReturnValue_t clearValue(std::string key);
/**
* Write a string to the scratch buffer
* @param key
* @param string String to write
* @return
*/
ReturnValue_t writeString(std::string key, std::string string);
/**
* Read a string from the scratch buffer
* @param key
* @param string Will be set to read string
* @return
*/
ReturnValue_t readString(std::string key, std::string& string);
2021-07-09 16:57:34 +02:00
2021-07-16 22:20:53 +02:00
/**
* Write a number to the scratch buffer
* @tparam T
* @tparam
* @param key
* @param num Number. Template allows to set signed, unsigned and floating point numbers
* @return
*/
2022-01-17 15:58:27 +01:00
template <typename T, class = typename std::enable_if<std::is_integral<T>::value>::type>
2021-07-16 22:20:53 +02:00
inline ReturnValue_t writeNumber(std::string key, T num) noexcept;
2021-07-09 16:57:34 +02:00
2021-07-16 22:20:53 +02:00
/**
* Read a number from the scratch buffer.
* @tparam T
* @tparam
* @param name
* @param num
* @return
*/
2022-01-17 15:58:27 +01:00
template <typename T, class = typename std::enable_if<std::is_integral<T>::value>::type>
2021-07-16 22:20:53 +02:00
inline ReturnValue_t readNumber(std::string key, T& num) noexcept;
// Anonymous namespace
namespace {
static uint8_t counter = 0;
ReturnValue_t readToFile(std::string name, std::ifstream& file, std::string& filename) {
2022-01-17 15:58:27 +01:00
using namespace std;
filename = "/tmp/sro" + std::to_string(counter++);
ostringstream oss;
oss << "xsc_scratch read " << name << " > " << filename;
int result = std::system(oss.str().c_str());
if (result != 0) {
2022-03-08 11:57:27 +01:00
if (WEXITSTATUS(result) == 1) {
2022-05-26 01:56:19 +02:00
sif::warning << "scratch::readToFile: Key " << name << " does not exist" << std::endl;
2022-01-17 15:58:27 +01:00
// Could not find value
std::remove(filename.c_str());
return KEY_NOT_FOUND;
} else {
2022-05-26 01:56:19 +02:00
utility::handleSystemError(result, "scratch::readToFile");
2022-01-17 15:58:27 +01:00
std::remove(filename.c_str());
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2021-07-09 16:57:34 +02:00
}
2022-01-17 15:58:27 +01:00
}
file.open(filename);
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-07-16 22:20:53 +02:00
}
2022-01-17 15:58:27 +01:00
} // End of anonymous namespace
2021-07-16 22:20:53 +02:00
2022-01-17 15:58:27 +01:00
template <typename T, class = typename std::enable_if<std::is_integral<T>::value>::type>
2021-07-16 22:20:53 +02:00
inline ReturnValue_t writeNumber(std::string key, T num) noexcept {
2022-07-13 11:25:53 +02:00
std::ostringstream oss("xsc_scratch write ", std::ostringstream::ate);
oss << key << " " << std::to_string(num);
2022-01-17 15:58:27 +01:00
int result = std::system(oss.str().c_str());
if (result != 0) {
utility::handleSystemError(result, "scratch::writeNumber");
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-07-16 22:20:53 +02:00
}
2022-01-17 15:58:27 +01:00
template <typename T, class = typename std::enable_if<std::is_integral<T>::value>::type>
2021-07-16 22:20:53 +02:00
inline ReturnValue_t readNumber(std::string key, T& num) noexcept {
2022-01-17 15:58:27 +01:00
using namespace std;
ifstream file;
std::string filename;
ReturnValue_t result = readToFile(key, file, filename);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
std::remove(filename.c_str());
return result;
}
2021-07-09 17:54:32 +02:00
2022-01-17 15:58:27 +01:00
string line;
if (not std::getline(file, line)) {
std::remove(filename.c_str());
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
size_t pos = line.find("=");
if (pos == string::npos) {
sif::warning << "scratch::readNumber: Output file format invalid, "
"no \"=\" found"
<< std::endl;
// Could not find value
2021-07-09 17:54:32 +02:00
std::remove(filename.c_str());
2022-01-17 15:58:27 +01:00
return KEY_NOT_FOUND;
}
std::string valueAsString = line.substr(pos + 1);
try {
num = std::stoi(valueAsString);
} catch (std::invalid_argument& e) {
sif::warning << "scratch::readNumber: stoi call failed with " << e.what() << std::endl;
}
std::remove(filename.c_str());
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-07-09 16:57:34 +02:00
}
2022-01-17 15:58:27 +01:00
} // namespace scratch
2021-07-09 16:57:34 +02:00
#endif /* BSP_Q7S_MEMORY_SCRATCHAPI_H_ */