From a618562807e7c7f34a1dd9aff040c1f37fca7dba Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 19 Jul 2021 14:34:03 +0200 Subject: [PATCH] more tests added --- bsp_q7s/boardtest/Q7STestTask.cpp | 40 +++++++++++++++++++++++-- bsp_q7s/boardtest/Q7STestTask.h | 9 +++++- bsp_q7s/memory/FileSystemHandler.cpp | 44 ++++++++++++++++++++++++---- bsp_q7s/memory/FileSystemHandler.h | 29 +++++++++++------- 4 files changed, 102 insertions(+), 20 deletions(-) diff --git a/bsp_q7s/boardtest/Q7STestTask.cpp b/bsp_q7s/boardtest/Q7STestTask.cpp index 786952a9..9e21cc91 100644 --- a/bsp_q7s/boardtest/Q7STestTask.cpp +++ b/bsp_q7s/boardtest/Q7STestTask.cpp @@ -24,7 +24,8 @@ ReturnValue_t Q7STestTask::performOneShotAction() { //testScratchApi(); //testJsonLibDirect(); //testDummyParams(); - testFileSystemHandlerDirect(); + FsOpCodes opCode = FsOpCodes::REMOVE_TMP_FILE; + testFileSystemHandlerDirect(opCode); return TestTask::performOneShotAction(); } @@ -130,11 +131,46 @@ void Q7STestTask::testDummyParams() { sif::info << "Test value 2 (\"blirb\" expected): " << test2 << std::endl; } -void Q7STestTask::testFileSystemHandlerDirect() { +void Q7STestTask::testFileSystemHandlerDirect(FsOpCodes opCode) { auto fsHandler = ObjectManager::instance()-> get(objects::FILE_SYSTEM_HANDLER); if(fsHandler == nullptr) { sif::warning << "Q7STestTask::testFileSystemHandlerDirect: No FS handler running.." << std::endl; } + 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; + } + } } diff --git a/bsp_q7s/boardtest/Q7STestTask.h b/bsp_q7s/boardtest/Q7STestTask.h index def18722..6f6b04f2 100644 --- a/bsp_q7s/boardtest/Q7STestTask.h +++ b/bsp_q7s/boardtest/Q7STestTask.h @@ -15,7 +15,14 @@ private: void testScratchApi(); void testJsonLibDirect(); void testDummyParams(); - void testFileSystemHandlerDirect(); + + enum FsOpCodes { + CREATE_EMPTY_FILE_IN_TMP, + REMOVE_TMP_FILE, + CREATE_DIR, + REMOVE_DIR + }; + void testFileSystemHandlerDirect(FsOpCodes opCode); }; diff --git a/bsp_q7s/memory/FileSystemHandler.cpp b/bsp_q7s/memory/FileSystemHandler.cpp index 47cb05a1..171c470a 100644 --- a/bsp_q7s/memory/FileSystemHandler.cpp +++ b/bsp_q7s/memory/FileSystemHandler.cpp @@ -143,9 +143,15 @@ ReturnValue_t FileSystemHandler::appendToFile(const char *repositoryPath, const ReturnValue_t FileSystemHandler::createFile(const char *repositoryPath, const char *filename, const uint8_t *data, size_t size, void *args) { + std::string fullPath; + bool useMountPrefix = true; + parseCfg(reinterpret_cast(args), useMountPrefix); + if(useMountPrefix) { + fullPath += currentMountPrefix; + } + // A double slash between repo and filename should not be an issue, so add it in any case - std::string fullPath = currentMountPrefix + std::string(repositoryPath) + "/" + - std::string(filename); + fullPath += std::string(repositoryPath) + "/" + std::string(filename); if(std::filesystem::exists(fullPath)) { return FILE_ALREADY_EXISTS; } @@ -159,9 +165,15 @@ ReturnValue_t FileSystemHandler::createFile(const char *repositoryPath, const ch ReturnValue_t FileSystemHandler::deleteFile(const char *repositoryPath, const char *filename, void *args) { + std::string fullPath; + bool useMountPrefix = true; + parseCfg(reinterpret_cast(args), useMountPrefix); + if(useMountPrefix) { + fullPath += currentMountPrefix; + } + // A double slash between repo and filename should not be an issue, so add it in any case - std::string fullPath = currentMountPrefix + std::string(repositoryPath) + "/" + - std::string(filename); + fullPath += std::string(repositoryPath) + "/" + std::string(filename); if(not std::filesystem::exists(fullPath)) { return FILE_DOES_NOT_EXIST; } @@ -174,7 +186,14 @@ ReturnValue_t FileSystemHandler::deleteFile(const char *repositoryPath, const ch } ReturnValue_t FileSystemHandler::createDirectory(const char *repositoryPath, void *args) { - std::string fullPath = currentMountPrefix + std::string(repositoryPath); + std::string fullPath; + bool useMountPrefix = true; + parseCfg(reinterpret_cast(args), useMountPrefix); + if(useMountPrefix) { + fullPath += currentMountPrefix; + } + + fullPath += std::string(repositoryPath); if(std::filesystem::exists(fullPath)) { return DIRECTORY_ALREADY_EXISTS; } @@ -187,7 +206,14 @@ ReturnValue_t FileSystemHandler::createDirectory(const char *repositoryPath, voi ReturnValue_t FileSystemHandler::removeDirectory(const char *repositoryPath, bool deleteRecurively, void *args) { - std::string fullPath = currentMountPrefix + std::string(repositoryPath); + std::string fullPath; + bool useMountPrefix = true; + parseCfg(reinterpret_cast(args), useMountPrefix); + if(useMountPrefix) { + fullPath += currentMountPrefix; + } + + fullPath += std::string(repositoryPath); if(not std::filesystem::exists(fullPath)) { return DIRECTORY_DOES_NOT_EXIST; } @@ -217,3 +243,9 @@ ReturnValue_t FileSystemHandler::removeDirectory(const char *repositoryPath, return HasReturnvaluesIF::RETURN_OK; } + +void FileSystemHandler::parseCfg(FsCommandCfg *cfg, bool& useMountPrefix) { + if(cfg != nullptr) { + useMountPrefix = cfg->useMountPrefix; + } +} diff --git a/bsp_q7s/memory/FileSystemHandler.h b/bsp_q7s/memory/FileSystemHandler.h index 73f52a62..2d22f24b 100644 --- a/bsp_q7s/memory/FileSystemHandler.h +++ b/bsp_q7s/memory/FileSystemHandler.h @@ -15,6 +15,12 @@ class FileSystemHandler: public SystemObject, public ExecutableObjectIF, public HasFileSystemIF { public: + struct FsCommandCfg { + // Can be used to automatically use mount prefix of active SD card. + // Otherwise, the operator has to specify the full path to the mounted SD card as well. + bool useMountPrefix = false; + }; + FileSystemHandler(object_id_t fileSystemHandler); virtual~ FileSystemHandler(); @@ -28,17 +34,6 @@ public: */ MessageQueueId_t getCommandQueue() const override; -private: - MessageQueueIF* mq = nullptr; - std::string currentMountPrefix = SdCardManager::SD_0_MOUNT_POINT; - static constexpr uint32_t FS_MAX_QUEUE_SIZE = config::OBSW_FILESYSTEM_HANDLER_QUEUE_SIZE; - - SdCardManager* sdcMan = nullptr; - uint8_t opCounter = 0; - - void fileSystemHandlerLoop(); - void fileSystemCheckup(); - ReturnValue_t appendToFile(const char* repositoryPath, const char* filename, const uint8_t* data, size_t size, uint16_t packetNumber, void* args = nullptr) override; @@ -50,6 +45,18 @@ private: ReturnValue_t createDirectory(const char* repositoryPath, void* args = nullptr) override; ReturnValue_t removeDirectory(const char* repositoryPath, bool deleteRecurively = false, void* args = nullptr) override; + +private: + MessageQueueIF* mq = nullptr; + std::string currentMountPrefix = SdCardManager::SD_0_MOUNT_POINT; + static constexpr uint32_t FS_MAX_QUEUE_SIZE = config::OBSW_FILESYSTEM_HANDLER_QUEUE_SIZE; + + SdCardManager* sdcMan = nullptr; + uint8_t opCounter = 0; + + void fileSystemHandlerLoop(); + void fileSystemCheckup(); + void parseCfg(FsCommandCfg* cfg, bool& useMountPrefix); };