eive-obsw/bsp_q7s/boardtest/Q7STestTask.cpp

177 lines
5.9 KiB
C++
Raw Normal View History

2021-07-19 12:44:43 +02:00
#include <bsp_q7s/memory/FileSystemHandler.h>
#include <fsfw/objectmanager/ObjectManager.h>
2021-07-05 12:09:31 +02:00
#include "Q7STestTask.h"
2021-07-06 18:17:32 +02:00
2021-07-16 21:51:30 +02:00
#include "bsp_q7s/memory/SdCardManager.h"
#include "bsp_q7s/memory/scratchApi.h"
2021-07-05 12:09:31 +02:00
#include "fsfw/timemanager/Stopwatch.h"
#include "fsfw/tasks/TaskFactory.h"
2021-07-06 18:17:32 +02:00
2021-07-16 21:51:30 +02:00
#include "test/DummyParameter.h"
#include <nlohmann/json.hpp>
2021-07-09 17:21:26 +02:00
2021-07-05 12:09:31 +02:00
#include <iostream>
#include <fstream>
#include <cstdio>
Q7STestTask::Q7STestTask(object_id_t objectId): TestTask(objectId) {
}
ReturnValue_t Q7STestTask::performOneShotAction() {
2021-07-19 12:44:43 +02:00
//testSdCard();
//testScratchApi();
2021-07-16 21:51:30 +02:00
//testJsonLibDirect();
2021-07-16 22:20:53 +02:00
//testDummyParams();
2021-07-19 14:34:03 +02:00
FsOpCodes opCode = FsOpCodes::REMOVE_TMP_FILE;
testFileSystemHandlerDirect(opCode);
2021-07-05 12:09:31 +02:00
return TestTask::performOneShotAction();
}
2021-07-19 12:44:43 +02:00
void Q7STestTask::testSdCard() {
2021-07-05 12:09:31 +02:00
using namespace std;
Stopwatch stopwatch;
2021-07-06 18:17:32 +02:00
int result = std::system("q7hw sd info all > /tmp/sd_status.txt");
if(result != 0) {
sif::debug << "system call failed with " << result << endl;
}
ifstream sdStatus("/tmp/sd_status.txt");
string line;
uint8_t idx = 0;
while (std::getline(sdStatus, line)) {
std::istringstream iss(line);
string word;
while(iss >> word) {
if(word == "on") {
sif::info << "SD card " << static_cast<int>(idx) << " is on" << endl;
}
else if(word == "off") {
sif::info << "SD card " << static_cast<int>(idx) << " is off" << endl;
}
}
idx++;
}
std::remove("/tmp/sd_status.txt");
2021-07-05 12:09:31 +02:00
}
2021-07-06 18:17:32 +02:00
void Q7STestTask::fileTests() {
using namespace std;
ofstream testFile("/tmp/test.txt");
testFile << "Hallo Welt" << endl;
testFile.close();
system("echo \"Hallo Welt\" > /tmp/test2.txt");
system("echo \"Hallo Welt\"");
}
2021-07-09 17:21:26 +02:00
void Q7STestTask::testScratchApi() {
ReturnValue_t result = scratch::writeNumber("TEST", 1);
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::debug << "Q7STestTask::scratchApiTest: Writing number failed" << std::endl;
}
int number = 0;
result = scratch::readNumber("TEST", number);
2021-07-16 22:20:53 +02:00
sif::info << "Q7STestTask::testScratchApi: Value for key \"TEST\": " << number << std::endl;
2021-07-09 17:21:26 +02:00
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::debug << "Q7STestTask::scratchApiTest: Reading number failed" << std::endl;
}
2021-07-16 22:20:53 +02:00
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;
2021-07-16 22:22:24 +02:00
result = scratch::clearValue("TEST");
result = scratch::clearValue("TEST2");
2021-07-09 17:21:26 +02:00
}
2021-07-16 21:51:30 +02:00
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<int>(DummyParameter::DUMMY_KEY_PARAM_1);
std::string test2 = param.getValue<std::string>(DummyParameter::DUMMY_KEY_PARAM_2);
sif::info << "Test value (3 expected): " << test << std::endl;
sif::info << "Test value 2 (\"blirb\" expected): " << test2 << std::endl;
}
2021-07-19 12:44:43 +02:00
2021-07-19 14:34:03 +02:00
void Q7STestTask::testFileSystemHandlerDirect(FsOpCodes opCode) {
2021-07-19 12:44:43 +02:00
auto fsHandler = ObjectManager::instance()->
get<FileSystemHandler>(objects::FILE_SYSTEM_HANDLER);
if(fsHandler == nullptr) {
sif::warning << "Q7STestTask::testFileSystemHandlerDirect: No FS handler running.."
<< std::endl;
}
2021-07-19 14:34:03 +02:00
FileSystemHandler::FsCommandCfg cfg;
switch(opCode) {
case(FsOpCodes::CREATE_EMPTY_FILE_IN_TMP): {
// No mount prefix, cause file is created in tmp
cfg.useMountPrefix = false;
sif::info << "Creating empty file in /tmp folder" << std::endl;
// Do not delete file, user can check existence in shell
fsHandler->createFile("/tmp", "test.txt", nullptr, 0, &cfg);
break;
}
case(FsOpCodes::REMOVE_TMP_FILE): {
sif::info << "Deleting /tmp/test.txt sample file" << std::endl;
// No mount prefix, cause file is created in tmp
cfg.useMountPrefix = false;
if(not std::filesystem::exists("/tmp/test.txt")) {
// Creating sample file
sif::info << "Creating sample file /tmp/test.txt to delete" << std::endl;
fsHandler->createFile("/tmp", "test.txt", nullptr, 0, &cfg);
}
ReturnValue_t result = fsHandler->deleteFile("/tmp", "test.txt", &cfg);
if(result == HasReturnvaluesIF::RETURN_OK) {
sif::info << "File deleted successfully" << std::endl;
}
else {
sif::info << "File deletion failed!" << std::endl;
}
break;
}
case(FsOpCodes::CREATE_DIR): {
break;
}
case(FsOpCodes::REMOVE_DIR): {
break;
}
}
2021-07-19 12:44:43 +02:00
}