eive-obsw/bsp_q7s/memory/scratchApi.h

66 lines
1.9 KiB
C++

#ifndef BSP_Q7S_MEMORY_SCRATCHAPI_H_
#define BSP_Q7S_MEMORY_SCRATCHAPI_H_
#include "fsfw/returnvalues/HasReturnvaluesIF.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
#include "linux/utility/utility.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <type_traits>
#include <cstdlib>
/**
* @brief API for the scratch buffer
*/
namespace scratch {
namespace {
static size_t counter = 0;
}
template<typename T, class = typename std::enable_if<std::is_integral<T>::value>::type>
inline ReturnValue_t writeNumber(std::string name, T num) noexcept {
std::ostringstream oss;
oss << "xsc_scratch write " << name << " " << num;
sif::debug << oss.str() << std::endl;
int result = std::system(oss.str().c_str());
if(result != 0) {
utility::handleSystemError(result, "scratch::writeNumber");
return HasReturnvaluesIF::RETURN_FAILED;
}
return HasReturnvaluesIF::RETURN_OK;
}
template<typename T, class = typename std::enable_if<std::is_integral<T>::value>::type>
inline ReturnValue_t readNumber(std::string name, T& num) noexcept {
using namespace std;
string filename = "/tmp/sro" + counter++;
ostringstream oss;
oss << "xsc_scratch read " << name << " < " << filename;
sif::debug << oss.str() << std::endl;
int result = std::system(oss.str().c_str());
if(result != 0) {
utility::handleSystemError(result, "scratch::writeNumber");
return HasReturnvaluesIF::RETURN_FAILED;
}
ifstream file(filename);
string line;
if (not std::getline(file, line)) {
return HasReturnvaluesIF::RETURN_FAILED;
}
size_t pos = line.find("=");
std::string valueAsString = line.substr(pos);
sif::debug << valueAsString << std::endl;
num = std::stoi(valueAsString);
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t writeString(std::string name, std::string string);
}
#endif /* BSP_Q7S_MEMORY_SCRATCHAPI_H_ */