diff --git a/bsp_q7s/boardtest/Q7STestTask.cpp b/bsp_q7s/boardtest/Q7STestTask.cpp index 2f2c0ffc..6a0dc690 100644 --- a/bsp_q7s/boardtest/Q7STestTask.cpp +++ b/bsp_q7s/boardtest/Q7STestTask.cpp @@ -1,10 +1,13 @@ -#include #include "Q7STestTask.h" +#include "bsp_q7s/memory/SdCardManager.h" +#include "bsp_q7s/memory/scratchApi.h" + #include "fsfw/timemanager/Stopwatch.h" #include "fsfw/tasks/TaskFactory.h" -#include "bsp_q7s/memory/scratchApi.h" +#include "test/DummyParameter.h" + #include #include @@ -16,16 +19,9 @@ Q7STestTask::Q7STestTask(object_id_t objectId): TestTask(objectId) { ReturnValue_t Q7STestTask::performOneShotAction() { //sdCardTests(); - testScratchApi(); - Stopwatch stopwatch; - // for convenience - using json = nlohmann::json; - json helloTest; - // add a number that is stored as double (note the implicit conversion of j to an object) - helloTest["pi"] = 3.141; - std::string mntPrefix = SdCardManager::instance()->getCurrentMountPrefix(); - std::ofstream o("/mnt/sd0/pretty.json"); - o << std::setw(4) << helloTest << std::endl; + //testScratchApi(); + //testJsonLibDirect(); + testDummyParams(); return TestTask::performOneShotAction(); } @@ -76,3 +72,42 @@ void Q7STestTask::testScratchApi() { sif::debug << "Q7STestTask::scratchApiTest: Reading number failed" << std::endl; } } + +void Q7STestTask::testJsonLibDirect() { + Stopwatch stopwatch; + // for convenience + using json = nlohmann::json; + json helloTest; + // add a number that is stored as double (note the implicit conversion of j to an object) + helloTest["pi"] = 3.141; + std::string mntPrefix = SdCardManager::instance()->getCurrentMountPrefix(); + std::string fileName = mntPrefix + "/pretty.json"; + std::ofstream o(fileName); + o << std::setw(4) << helloTest << std::endl; +} + +void Q7STestTask::testDummyParams() { + std::string mntPrefix = SdCardManager::instance()->getCurrentMountPrefix(); + DummyParameter param(mntPrefix, "dummy_json.txt"); + param.printKeys(); + param.print(); + if(not param.getJsonFileExists()) { + param.writeJsonFile(); + } + + ReturnValue_t result = param.readJsonFile(); + if(result != HasReturnvaluesIF::RETURN_OK) { + + } + + param.setValue(DummyParameter::DUMMY_KEY_PARAM_1, 3); + param.setValue(DummyParameter::DUMMY_KEY_PARAM_2, "blirb"); + + param.writeJsonFile(); + param.print(); + + int test = param.getValue(DummyParameter::DUMMY_KEY_PARAM_1); + std::string test2 = param.getValue(DummyParameter::DUMMY_KEY_PARAM_2); + sif::info << "Test value (3 expected): " << test << std::endl; + sif::info << "Test value 2 (\"blirb\" expected): " << test2 << std::endl; +} diff --git a/bsp_q7s/boardtest/Q7STestTask.h b/bsp_q7s/boardtest/Q7STestTask.h index 664a8fa2..7ecdd83a 100644 --- a/bsp_q7s/boardtest/Q7STestTask.h +++ b/bsp_q7s/boardtest/Q7STestTask.h @@ -13,6 +13,8 @@ private: void fileTests(); void testScratchApi(); + void testJsonLibDirect(); + void testDummyParams(); }; diff --git a/bsp_q7s/core/ParameterHandler.cpp b/bsp_q7s/core/ParameterHandler.cpp new file mode 100644 index 00000000..d6c8f34f --- /dev/null +++ b/bsp_q7s/core/ParameterHandler.cpp @@ -0,0 +1,8 @@ +#include "ParameterHandler.h" + +ParameterHandler::ParameterHandler(std::string mountPrefix): mountPrefix(mountPrefix) { +} + +void ParameterHandler::setMountPrefix(std::string prefix) { + mountPrefix = prefix; +} diff --git a/bsp_q7s/core/ParameterHandler.h b/bsp_q7s/core/ParameterHandler.h new file mode 100644 index 00000000..81cbc099 --- /dev/null +++ b/bsp_q7s/core/ParameterHandler.h @@ -0,0 +1,22 @@ +#ifndef BSP_Q7S_CORE_PARAMETERHANDLER_H_ +#define BSP_Q7S_CORE_PARAMETERHANDLER_H_ + +#include +#include + + + +class ParameterHandler { +public: + ParameterHandler(std::string mountPrefix); + + void setMountPrefix(std::string prefix); + + void setUpDummyParameter(); +private: + std::string mountPrefix; + DummyParameter dummyParam; +}; + + +#endif /* BSP_Q7S_CORE_PARAMETERHANDLER_H_ */ diff --git a/cmake/Q7SCrossCompileConfig.cmake b/cmake/Q7SCrossCompileConfig.cmake index a4336983..746df453 100644 --- a/cmake/Q7SCrossCompileConfig.cmake +++ b/cmake/Q7SCrossCompileConfig.cmake @@ -3,8 +3,7 @@ if(NOT DEFINED ENV{Q7S_SYSROOT}) # Sysroot has not been cached yet and was not set in environment either if(NOT DEFINED SYSROOT_PATH) message(FATAL_ERROR - "Define the Q7S_ROOTFS variable to " - "point to the raspbian rootfs." + "Define the Q7S_ROOTFS variable to point to the Q7S rootfs." ) endif() else() diff --git a/cmake/scripts/Q7S/ninja_release_cfg.sh b/cmake/scripts/Q7S/ninja_release_cfg.sh new file mode 100755 index 00000000..6a94acde --- /dev/null +++ b/cmake/scripts/Q7S/ninja_release_cfg.sh @@ -0,0 +1,33 @@ +#!/bin/sh +counter=0 +while [ ${counter} -lt 5 ] +do + cd .. + if [ -f "cmake_build_config.py" ];then + break + fi + counter=$((counter=counter + 1)) +done + +if [ "${counter}" -ge 5 ];then + echo "cmake_build_config.py not found in upper directories!" + exit 1 +fi + +os_fsfw="linux" +tgt_bsp="arm/q7s" +build_dir="build-Debug-Q7S" +build_generator="Ninja" +if [ "${OS}" = "Windows_NT" ]; then + python="py" +# Could be other OS but this works for now. +else + python="python3" +fi + +echo "Running command (without the leading +):" +set -x # Print command +${python} cmake_build_config.py -o "${os_fsfw}" -g "${build_generator}" -b "release" -t "${tgt_bsp}" \ + -l"${build_dir}" +# set +x + diff --git a/mission/CMakeLists.txt b/mission/CMakeLists.txt index 9cb18749..46f562a5 100644 --- a/mission/CMakeLists.txt +++ b/mission/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(core) add_subdirectory(devices) add_subdirectory(utility) +add_subdirectory(memory) diff --git a/mission/memory/CMakeLists.txt b/mission/memory/CMakeLists.txt new file mode 100644 index 00000000..ccaef754 --- /dev/null +++ b/mission/memory/CMakeLists.txt @@ -0,0 +1,5 @@ +target_sources(${TARGET_NAME} PUBLIC + NVMParameterBase.cpp +) + + diff --git a/mission/memory/NVMParameterBase.cpp b/mission/memory/NVMParameterBase.cpp new file mode 100644 index 00000000..e7b8e8a6 --- /dev/null +++ b/mission/memory/NVMParameterBase.cpp @@ -0,0 +1,50 @@ +#include "NVMParameterBase.h" +#include "fsfw/memory/HasFileSystemIF.h" +#include "fsfw/serviceinterface/ServiceInterface.h" + +#include + +NVMParameterBase::NVMParameterBase(std::string fullName): fullName(fullName) { +} + +ReturnValue_t NVMParameterBase::readJsonFile() { + if(std::filesystem::exists(fullName)) { + // Read JSON file content into object + std::ifstream i(fullName); + i >> json; + return HasReturnvaluesIF::RETURN_OK; + } + return HasFileSystemIF::FILE_DOES_NOT_EXIST; +} + +ReturnValue_t NVMParameterBase::writeJsonFile() { + std::ofstream o(fullName); + o << std::setw(4) << json; + return HasReturnvaluesIF::RETURN_OK; +} + +void NVMParameterBase::setFullName(std::string fullName) { + this->fullName = fullName; +} + +std::string NVMParameterBase::getFullName() const { + return fullName; +} + +bool NVMParameterBase::getJsonFileExists() { + return std::filesystem::exists(fullName); +} + +void NVMParameterBase::printKeys() const { + sif::info << "Printing keys for JSON file " << fullName << std::endl; + for(const auto& key: keys) { + sif::info << key << std::endl; + } +} + +void NVMParameterBase::print() const { + sif::info << "Printing JSON file " << fullName << std::endl; + for(const auto& key: keys) { + sif::info << key << ": " << json[key] << std::endl; + } +} diff --git a/mission/memory/NVMParameterBase.h b/mission/memory/NVMParameterBase.h new file mode 100644 index 00000000..4814acf4 --- /dev/null +++ b/mission/memory/NVMParameterBase.h @@ -0,0 +1,69 @@ +#ifndef BSP_Q7S_CORE_NVMPARAMS_NVMPARAMIF_H_ +#define BSP_Q7S_CORE_NVMPARAMS_NVMPARAMIF_H_ + +#include "fsfw/returnvalues/HasReturnvaluesIF.h" + +#include +#include +#include + +class NVMParameterBase { +public: + virtual~ NVMParameterBase() {} + + NVMParameterBase(std::string fullName); + + bool getJsonFileExists(); + + /** + * Returns RETURN_OK on successfull read and HasFileSystemIF::FILE_DOES_NOT_EXIST if + * file does not exist yet. + * @return + */ + virtual ReturnValue_t readJsonFile(); + + virtual ReturnValue_t writeJsonFile(); + + void setFullName(std::string fullName); + std::string getFullName() const; + + template + ReturnValue_t insertValue(std::string key, T value); + + template + ReturnValue_t setValue(std::string key, T value); + + template + T getValue(std::string key) const; + + void printKeys() const; + void print() const; + +private: + nlohmann::json json; + std::vector keys; + std::string fullName; +}; + +template +inline ReturnValue_t NVMParameterBase::insertValue(std::string key, T value) { + // Check whether key already exists. If it does not, insert it + if (std::find(keys.begin(), keys.end(), key) == keys.end()) { + keys.push_back(key); + } + json[key] = value; + return HasReturnvaluesIF::RETURN_OK; +} + +template +inline ReturnValue_t NVMParameterBase::setValue(std::string key, T value) { + json[key] = value; + return HasReturnvaluesIF::RETURN_OK; +} + +template +inline T NVMParameterBase::getValue(std::string key) const { + return json[key]; +} + +#endif /* BSP_Q7S_CORE_NVMPARAMS_NVMPARAMIF_H_ */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 00000000..06c053c8 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(testtasks) diff --git a/test/DummyParameter.h b/test/DummyParameter.h new file mode 100644 index 00000000..b8da7275 --- /dev/null +++ b/test/DummyParameter.h @@ -0,0 +1,26 @@ +#ifndef BSP_Q7S_CORE_NVMPARAMS_PARAMETERDEFINITIONS_H_ +#define BSP_Q7S_CORE_NVMPARAMS_PARAMETERDEFINITIONS_H_ + +#include "mission/memory/NVMParameterBase.h" +#include + +#include + +class DummyParameter: public NVMParameterBase { +public: + static constexpr char DUMMY_KEY_PARAM_1[] = "dummy1"; + static constexpr char DUMMY_KEY_PARAM_2[] = "dummy2"; + + DummyParameter(std::string mountPrefix, std::string jsonFileName): + NVMParameterBase(mountPrefix + "/conf/" + jsonFileName), + mountPrefix(mountPrefix) { + insertValue(DUMMY_KEY_PARAM_1, 1); + insertValue(DUMMY_KEY_PARAM_2, "blablub"); + } + +private: + std::string mountPrefix; +}; + + +#endif /* BSP_Q7S_CORE_NVMPARAMS_PARAMETERDEFINITIONS_H_ */