more tests added

This commit is contained in:
Robin Müller 2021-07-19 14:34:03 +02:00 committed by Robin Mueller
parent d4e5919a21
commit 42578ba2ac
4 changed files with 102 additions and 20 deletions

View File

@ -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<FileSystemHandler>(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;
}
}
}

View File

@ -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);
};

View File

@ -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<FsCommandCfg*>(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<FsCommandCfg*>(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<FsCommandCfg*>(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<FsCommandCfg*>(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;
}
}

View File

@ -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);
};