eive-obsw/bsp_q7s/boardtest/Q7STestTask.cpp

415 lines
15 KiB
C++
Raw Normal View History

2021-08-19 14:16:26 +02:00
#include <bsp_q7s/core/CoreController.h>
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>
2022-01-15 13:05:11 +01:00
#include <gps.h>
#include <libgpsmm.h>
2021-07-09 17:21:26 +02:00
2022-01-15 13:05:11 +01:00
#include <ctime>
2021-07-05 12:09:31 +02:00
#include <iostream>
2022-01-15 13:05:11 +01:00
#include <iomanip>
2021-07-05 12:09:31 +02:00
#include <fstream>
#include <cstdio>
Q7STestTask::Q7STestTask(object_id_t objectId): TestTask(objectId) {
2022-01-15 13:09:33 +01:00
doTestSdCard = false;
doTestScratchApi = false;
doTestGps = false;
2021-07-05 12:09:31 +02:00
}
ReturnValue_t Q7STestTask::performOneShotAction() {
2022-01-15 13:09:33 +01:00
if (doTestSdCard) {
testSdCard();
}
if (doTestScratchApi) {
testScratchApi();
}
2021-07-16 21:51:30 +02:00
//testJsonLibDirect();
2021-07-16 22:20:53 +02:00
//testDummyParams();
2021-08-19 18:10:19 +02:00
//testProtHandler();
2021-11-25 21:38:13 +01:00
FsOpCodes opCode = FsOpCodes::APPEND_TO_FILE;
testFileSystemHandlerDirect(opCode);
2021-07-05 12:09:31 +02:00
return TestTask::performOneShotAction();
}
2022-01-15 13:05:11 +01:00
ReturnValue_t Q7STestTask::performPeriodicAction() {
2022-01-15 13:09:33 +01:00
if(doTestGps) {
testGpsDaemon();
}
2022-01-15 13:05:11 +01:00
return TestTask::performPeriodicAction();
}
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();
2021-12-29 20:33:20 +01:00
int test = 0;
result = param.getValue<int>(DummyParameter::DUMMY_KEY_PARAM_1, &test);
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Q7STestTask::testDummyParams: Key " << DummyParameter::DUMMY_KEY_PARAM_1
<< " does not exist" << std::endl;
}
std::string test2;
result = param.getValue<std::string>(DummyParameter::DUMMY_KEY_PARAM_2, &test2);
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Q7STestTask::testDummyParams: Key " << DummyParameter::DUMMY_KEY_PARAM_1
<< " does not exist" << std::endl;
}
2021-07-16 21:51:30 +02:00
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-08-19 14:16:26 +02:00
ReturnValue_t Q7STestTask::initialize() {
coreController = ObjectManager::instance()->get<CoreController>(objects::CORE_CONTROLLER);
if(coreController == nullptr) {
sif::warning << "Q7STestTask::initialize: Could not retrieve CORE_CONTROLLER object" <<
std::endl;
}
return TestTask::initialize();
}
void Q7STestTask::testProtHandler() {
bool opPerformed = false;
2021-08-19 14:34:24 +02:00
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
// If any chips are unlocked, lock them here
2021-08-19 14:45:25 +02:00
result = coreController->setBootCopyProtection(
2021-08-19 14:34:24 +02:00
CoreController::Chip::ALL_CHIP, CoreController::Copy::ALL_COPY, true,
opPerformed, true);
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Q7STestTask::testProtHandler: Op failed" << std::endl;
}
// unlock own copy
2021-08-19 14:45:25 +02:00
result = coreController->setBootCopyProtection(
2021-08-19 14:16:26 +02:00
CoreController::Chip::SELF_CHIP, CoreController::Copy::SELF_COPY, false,
opPerformed, true);
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Q7STestTask::testProtHandler: Op failed" << std::endl;
}
if(not opPerformed) {
sif::warning << "Q7STestTask::testProtHandler: No op performed" << std::endl;
}
int retval = std::system("print-chip-prot-status.sh");
if(retval != 0) {
utility::handleSystemError(retval, "Q7STestTask::testProtHandler");
}
2021-08-19 14:34:24 +02:00
// lock own copy
2021-08-19 14:45:25 +02:00
result = coreController->setBootCopyProtection(
2021-08-19 14:16:26 +02:00
CoreController::Chip::SELF_CHIP, CoreController::Copy::SELF_COPY, true,
opPerformed, true);
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Q7STestTask::testProtHandler: Op failed" << std::endl;
2021-08-19 14:45:25 +02:00
}
if(not opPerformed) {
sif::warning << "Q7STestTask::testProtHandler: No op performed" << std::endl;
}
retval = std::system("print-chip-prot-status.sh");
if(retval != 0) {
utility::handleSystemError(retval, "Q7STestTask::testProtHandler");
}
// unlock specific copy
result = coreController->setBootCopyProtection(
CoreController::Chip::CHIP_1, CoreController::Copy::COPY_1, false,
opPerformed, true);
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Q7STestTask::testProtHandler: Op failed" << std::endl;
}
if(not opPerformed) {
sif::warning << "Q7STestTask::testProtHandler: No op performed" << std::endl;
}
retval = std::system("print-chip-prot-status.sh");
if(retval != 0) {
utility::handleSystemError(retval, "Q7STestTask::testProtHandler");
}
// lock specific copy
result = coreController->setBootCopyProtection(
CoreController::Chip::CHIP_1, CoreController::Copy::COPY_1, true,
opPerformed, true);
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Q7STestTask::testProtHandler: Op failed" << std::endl;
2021-08-19 14:16:26 +02:00
}
if(not opPerformed) {
sif::warning << "Q7STestTask::testProtHandler: No op performed" << std::endl;
}
retval = std::system("print-chip-prot-status.sh");
if(retval != 0) {
utility::handleSystemError(retval, "Q7STestTask::testProtHandler");
}
}
2022-01-15 13:05:11 +01:00
void Q7STestTask::testGpsDaemon() {
gpsmm gpsmm(GPSD_SHARED_MEMORY, 0);
gps_data_t* gps;
gps = gpsmm.read();
if(gps == nullptr) {
sif::warning << "Q7STestTask: Reading GPS data failed" << std::endl;
}
sif::info << "-- Q7STestTask: GPS shared memory read test --" << std::endl;
time_t timeRaw = gps->fix.time.tv_sec;
std::tm* time = gmtime(&timeRaw);
sif::info << "Time: " << std::put_time(time, "%c %Z") << std::endl;
sif::info << "Visible satellites: " << gps->satellites_visible << std::endl;
sif::info << "Satellites used: " << gps->satellites_used << std::endl;
sif::info << "Fix (0:Not Seen|1:No Fix|2:2D|3:3D): " << gps->fix.mode << std::endl;
sif::info << "Latitude: " << gps->fix.latitude << std::endl;
sif::info << "Longitude: " << gps->fix.longitude << std::endl;
sif::info << "Altitude(MSL): " << gps->fix.altMSL << std::endl;
sif::info << "Speed(m/s): " << gps->fix.speed << std::endl;
}
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 19:21:34 +02:00
FileSystemHandler::FsCommandCfg cfg = {};
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
// Lambda for common code
auto createNonEmptyTmpDir = [&]() {
if(not std::filesystem::exists("/tmp/test")) {
2021-11-19 13:22:20 +01:00
result = fsHandler->createDirectory("/tmp", "test", false, &cfg);
2021-07-19 19:21:34 +02:00
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
}
// Creating sample files
sif::info << "Creating sample files in directory" << std::endl;
result = fsHandler->createFile("/tmp/test", "test1.txt", nullptr, 0, &cfg);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
result = fsHandler->createFile("/tmp/test", "test2.txt", nullptr, 0, &cfg);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
return result;
};
2021-07-19 14:34:03 +02:00
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
2021-11-25 21:38:13 +01:00
fsHandler->createFile("/tmp/", "test.txt", nullptr, 0, &cfg);
2021-07-19 14:34:03 +02:00
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;
2021-11-25 21:38:13 +01:00
fsHandler->createFile("/tmp/", "test.txt", nullptr, 0, &cfg);
2021-07-19 14:34:03 +02:00
}
2021-07-19 19:21:34 +02:00
result = fsHandler->removeFile("/tmp", "test.txt", &cfg);
2021-07-19 14:34:03 +02:00
if(result == HasReturnvaluesIF::RETURN_OK) {
2021-07-19 19:21:34 +02:00
sif::info << "File removed successfully" << std::endl;
2021-07-19 14:34:03 +02:00
}
else {
2021-07-19 19:21:34 +02:00
sif::warning << "File removal failed!" << std::endl;
2021-07-19 14:34:03 +02:00
}
break;
}
2021-07-19 19:21:34 +02:00
case(FsOpCodes::CREATE_DIR_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
2021-11-25 21:38:13 +01:00
ReturnValue_t result = fsHandler->createDirectory("/tmp/", "test", false, &cfg);
2021-07-19 19:21:34 +02:00
if(result == HasReturnvaluesIF::RETURN_OK) {
sif::info << "Directory created successfully" << std::endl;
}
else {
sif::warning << "Directory creation failed!" << std::endl;
}
2021-07-19 14:34:03 +02:00
break;
}
2021-07-19 19:21:34 +02:00
case(FsOpCodes::REMOVE_EMPTY_DIR_IN_TMP): {
// No mount prefix, cause file is created in tmp
cfg.useMountPrefix = false;
if(not std::filesystem::exists("/tmp/test")) {
2021-11-19 13:22:20 +01:00
result = fsHandler->createDirectory("/tmp", "test", false, &cfg);
2021-07-19 19:21:34 +02:00
}
else {
// Delete any leftover files to regular dir removal works
std::remove("/tmp/test/*");
}
2021-11-25 21:38:13 +01:00
result = fsHandler->removeDirectory("/tmp/", "test", false, &cfg);
2021-07-19 19:21:34 +02:00
if(result == HasReturnvaluesIF::RETURN_OK) {
sif::info << "Directory removed successfully" << std::endl;
}
else {
sif::warning << "Directory removal failed!" << std::endl;
}
2021-07-19 14:34:03 +02:00
break;
}
2021-07-19 19:21:34 +02:00
case(FsOpCodes::REMOVE_FILLED_DIR_IN_TMP): {
result = createNonEmptyTmpDir();
if(result != HasReturnvaluesIF::RETURN_OK) {
return;
}
2021-11-25 21:38:13 +01:00
result = fsHandler->removeDirectory("/tmp/", "test", true, &cfg);
2021-07-19 19:21:34 +02:00
if(result == HasReturnvaluesIF::RETURN_OK) {
sif::info << "Directory removed recursively successfully" << std::endl;
}
else {
sif::warning << "Recursive directory removal failed!" << std::endl;
}
break;
}
case(FsOpCodes::ATTEMPT_DIR_REMOVAL_NON_EMPTY): {
result = createNonEmptyTmpDir();
if(result != HasReturnvaluesIF::RETURN_OK) {
return;
}
2021-11-25 21:38:13 +01:00
result = fsHandler->removeDirectory("/tmp/", "test", false, &cfg);
2021-07-19 19:21:34 +02:00
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::info << "Directory removal attempt failed as expected" << std::endl;
}
else {
sif::warning << "Directory removal worked when it should not have!" << std::endl;
}
2021-11-25 21:38:13 +01:00
break;
}
case(FsOpCodes::RENAME_FILE): {
// No mount prefix, cause file is created in tmp
cfg.useMountPrefix = false;
if(std::filesystem::exists("/tmp/test.txt")) {
fsHandler->removeDirectory("/tmp/", "test", false, &cfg);
}
sif::info << "Creating empty file /tmp/test.txt and rename to /tmp/test2.txt" << std::endl;
// Do not delete file, user can check existence in shell
fsHandler->createFile("/tmp/", "test.txt", nullptr, 0, &cfg);
fsHandler->renameFile("/tmp/", "test.txt", "test2.txt", &cfg);
break;
}
case(FsOpCodes::APPEND_TO_FILE): {
// No mount prefix, cause file is created in tmp
cfg.useMountPrefix = false;
if(std::filesystem::exists("/tmp/test.txt")) {
fsHandler->removeDirectory("/tmp/", "test", false, &cfg);
}
if(std::filesystem::exists("/tmp/test.txt")) {
fsHandler->removeDirectory("/tmp/", "test", false, &cfg);
}
sif::info << "Creating empty file /tmp/test.txt and adding content" << std::endl;
std::string content = "Hello World\n";
// Do not delete file, user can check existence in shell
fsHandler->createFile("/tmp/", "test.txt", nullptr, 0, &cfg);
fsHandler->appendToFile("/tmp/", "test.txt", reinterpret_cast<const uint8_t*>(
content.data()), content.size(), 0, &cfg);
2021-07-19 19:21:34 +02:00
}
2021-07-19 14:34:03 +02:00
}
2021-07-19 12:44:43 +02:00
}