From 27281af392f2f75f439b622ce632cef3d94383d0 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 16 Jul 2021 22:20:53 +0200 Subject: [PATCH] scratch buffer API finished --- bsp_q7s/boardtest/Q7STestTask.cpp | 16 +++++- bsp_q7s/memory/SdCardManager.cpp | 4 ++ bsp_q7s/memory/scratchApi.cpp | 39 ++++++++++++- bsp_q7s/memory/scratchApi.h | 93 ++++++++++++++++++++++++------- 4 files changed, 130 insertions(+), 22 deletions(-) diff --git a/bsp_q7s/boardtest/Q7STestTask.cpp b/bsp_q7s/boardtest/Q7STestTask.cpp index 6a0dc690..51ba99d2 100644 --- a/bsp_q7s/boardtest/Q7STestTask.cpp +++ b/bsp_q7s/boardtest/Q7STestTask.cpp @@ -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() { diff --git a/bsp_q7s/memory/SdCardManager.cpp b/bsp_q7s/memory/SdCardManager.cpp index 3d309dd4..d24f540d 100644 --- a/bsp_q7s/memory/SdCardManager.cpp +++ b/bsp_q7s/memory/SdCardManager.cpp @@ -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) { diff --git a/bsp_q7s/memory/scratchApi.cpp b/bsp_q7s/memory/scratchApi.cpp index 0b159775..08d59df1 100644 --- a/bsp_q7s/memory/scratchApi.cpp +++ b/bsp_q7s/memory/scratchApi.cpp @@ -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; diff --git a/bsp_q7s/memory/scratchApi.h b/bsp_q7s/memory/scratchApi.h index 866f1035..4f3dcd60 100644 --- a/bsp_q7s/memory/scratchApi.h +++ b/bsp_q7s/memory/scratchApi.h @@ -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::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::value>::type> +inline ReturnValue_t readNumber(std::string key, T& num) noexcept; + + +// Anonymous namespace namespace { + static uint8_t counter = 0; -} -template::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::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::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::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_ */