scratch buffer API finished

This commit is contained in:
Robin Müller 2021-07-16 22:20:53 +02:00
parent ea8af2a7dd
commit 27281af392
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
4 changed files with 130 additions and 22 deletions

View File

@ -19,9 +19,9 @@ Q7STestTask::Q7STestTask(object_id_t objectId): TestTask(objectId) {
ReturnValue_t Q7STestTask::performOneShotAction() {
//sdCardTests();
//testScratchApi();
testScratchApi();
//testJsonLibDirect();
testDummyParams();
//testDummyParams();
return TestTask::performOneShotAction();
}
@ -68,9 +68,21 @@ void Q7STestTask::testScratchApi() {
}
int number = 0;
result = scratch::readNumber("TEST", number);
sif::info << "Q7STestTask::testScratchApi: Value for key \"TEST\": " << number << std::endl;
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::debug << "Q7STestTask::scratchApiTest: Reading number failed" << std::endl;
}
result = scratch::writeString("TEST2", "halloWelt");
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::debug << "Q7STestTask::scratchApiTest: Writing string failed" << std::endl;
}
std::string string;
result = scratch::readString("TEST2", string);
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::debug << "Q7STestTask::scratchApiTest: Reading number failed" << std::endl;
}
sif::info << "Q7STestTask::testScratchApi: Value for key \"TEST2\": " << string << std::endl;
}
void Q7STestTask::testJsonLibDirect() {

View File

@ -56,6 +56,10 @@ ReturnValue_t SdCardManager::switchOnSdCard(sd::SdCard sdCard, bool doMountSdCar
else if(sdCard == sd::SdCard::SLOT_1) {
targetStatus = statusPair->second;
}
else {
// Should not happen
targetStatus = sd::SdStatus::OFF;
}
auto switchCall = [&]() {
if(targetStatus == sd::SdStatus::ON) {

View File

@ -5,7 +5,44 @@ ReturnValue_t scratch::writeString(std::string name, std::string string) {
oss << "xsc_scratch write " << name << " \"" << string << "\"";
int result = std::system(oss.str().c_str());
if(result != 0) {
utility::handleSystemError(result, "scratch::String");
utility::handleSystemError(result, "scratch::writeString");
return HasReturnvaluesIF::RETURN_FAILED;
}
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t scratch::readString(std::string key, std::string &string) {
std::ifstream file;
std::string filename;
ReturnValue_t result = readToFile(key, file, filename);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
std::string line;
if (not std::getline(file, line)) {
std::remove(filename.c_str());
return HasReturnvaluesIF::RETURN_FAILED;
}
size_t pos = line.find("=");
if(pos == std::string::npos) {
sif::warning << "scratch::readNumber: Output file format invalid, "
"no \"=\" found" << std::endl;
// Could not find value
std::remove(filename.c_str());
return KEY_NOT_FOUND;
}
string = line.substr(pos + 1);
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t scratch::clearValue(std::string key) {
std::ostringstream oss;
oss << "xsc_scratch clear " << key;
int result = std::system(oss.str().c_str());
if(result != 0) {
utility::handleSystemError(result, "scratch::clearValue");
return HasReturnvaluesIF::RETURN_FAILED;
}
return HasReturnvaluesIF::RETURN_OK;

View File

@ -22,26 +22,54 @@ static constexpr char PREFERED_SDC_KEY[] = "PREFSD";
static constexpr uint8_t INTERFACE_ID = CLASS_ID::SCRATCH_BUFFER;
static constexpr ReturnValue_t KEY_NOT_FOUND = HasReturnvaluesIF::makeReturnCode(INTERFACE_ID, 0);
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);
/**
* 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
*/
template<typename T, class = typename std::enable_if<std::is_integral<T>::value>::type>
inline ReturnValue_t writeNumber(std::string key, T num) noexcept;
/**
* Read a number from the scratch buffer.
* @tparam T
* @tparam
* @param name
* @param num
* @return
*/
template<typename T, class = typename std::enable_if<std::is_integral<T>::value>::type>
inline ReturnValue_t readNumber(std::string key, T& num) noexcept;
// Anonymous namespace
namespace {
static uint8_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 << " " << std::to_string(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;
}
template<typename T, class = typename std::enable_if<std::is_integral<T>::value>::type>
inline ReturnValue_t readNumber(std::string name, T& num) noexcept {
ReturnValue_t readToFile(std::string name, std::ifstream& file, std::string& filename) {
using namespace std;
string filename = "/tmp/sro" + std::to_string(counter++);
filename = "/tmp/sro" + std::to_string(counter++);
ostringstream oss;
oss << "xsc_scratch read " << name << " > " << filename;
@ -59,9 +87,38 @@ inline ReturnValue_t readNumber(std::string name, T& num) noexcept {
return HasReturnvaluesIF::RETURN_FAILED;
}
}
ifstream file(filename);
file.open(filename);
return HasReturnvaluesIF::RETURN_OK;
}
} // End of anonymous namespace
template<typename T, class = typename std::enable_if<std::is_integral<T>::value>::type>
inline ReturnValue_t writeNumber(std::string key, T num) noexcept {
std::ostringstream oss;
oss << "xsc_scratch write " << key << " " << std::to_string(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;
}
template<typename T, class = typename std::enable_if<std::is_integral<T>::value>::type>
inline ReturnValue_t readNumber(std::string key, T& num) noexcept {
using namespace std;
ifstream file;
std::string filename;
ReturnValue_t result = readToFile(key, file, filename);
if(result != HasReturnvaluesIF::RETURN_OK) {
std::remove(filename.c_str());
return result;
}
string line;
if (not std::getline(file, line)) {
std::remove(filename.c_str());
return HasReturnvaluesIF::RETURN_FAILED;
}
@ -85,8 +142,6 @@ inline ReturnValue_t readNumber(std::string name, T& num) noexcept {
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t writeString(std::string name, std::string string);
}
#endif /* BSP_Q7S_MEMORY_SCRATCHAPI_H_ */