2021-07-09 16:57:34 +02:00
|
|
|
#ifndef BSP_Q7S_MEMORY_SCRATCHAPI_H_
|
|
|
|
#define BSP_Q7S_MEMORY_SCRATCHAPI_H_
|
|
|
|
|
|
|
|
#include "fsfw/returnvalues/HasReturnvaluesIF.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 <iostream>
|
2021-07-09 17:21:26 +02:00
|
|
|
#include <fstream>
|
2021-07-09 16:57:34 +02:00
|
|
|
#include <sstream>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief API for the scratch buffer
|
|
|
|
*/
|
|
|
|
namespace scratch {
|
|
|
|
|
|
|
|
namespace {
|
2021-07-09 17:54:32 +02:00
|
|
|
static uint8_t counter = 0;
|
2021-07-09 16:57:34 +02:00
|
|
|
}
|
|
|
|
|
2021-07-09 17:21:26 +02:00
|
|
|
template<typename T, class = typename std::enable_if<std::is_integral<T>::value>::type>
|
2021-07-09 16:57:34 +02:00
|
|
|
inline ReturnValue_t writeNumber(std::string name, T num) noexcept {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "xsc_scratch write " << name << " " << num;
|
|
|
|
int result = std::system(oss.str().c_str());
|
|
|
|
if(result != 0) {
|
|
|
|
utility::handleSystemError(result, "scratch::writeNumber");
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
2021-07-09 17:21:26 +02:00
|
|
|
template<typename T, class = typename std::enable_if<std::is_integral<T>::value>::type>
|
2021-07-09 16:57:34 +02:00
|
|
|
inline ReturnValue_t readNumber(std::string name, T& num) noexcept {
|
2021-07-09 17:21:26 +02:00
|
|
|
using namespace std;
|
2021-07-09 17:54:32 +02:00
|
|
|
string filename = "/tmp/sro" + std::to_string(counter++);
|
2021-07-09 17:21:26 +02:00
|
|
|
ostringstream oss;
|
2021-07-09 17:54:32 +02:00
|
|
|
oss << "xsc_scratch read " << name << " > " << filename;
|
|
|
|
|
2021-07-09 16:57:34 +02:00
|
|
|
int result = std::system(oss.str().c_str());
|
|
|
|
if(result != 0) {
|
|
|
|
utility::handleSystemError(result, "scratch::writeNumber");
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2021-07-09 17:21:26 +02:00
|
|
|
ifstream file(filename);
|
|
|
|
string line;
|
|
|
|
if (not std::getline(file, line)) {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t pos = line.find("=");
|
2021-07-09 17:54:32 +02:00
|
|
|
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());
|
2021-07-09 16:57:34 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t writeString(std::string name, std::string string);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* BSP_Q7S_MEMORY_SCRATCHAPI_H_ */
|