From 5c0140104277313393c23f81faa0e4633804c336 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 19 Jul 2021 20:06:28 +0200 Subject: [PATCH] added command to list directory into file --- README.md | 2 +- bsp_q7s/core/CoreController.cpp | 53 +++++++++++++++++++++++++++++++++ bsp_q7s/core/CoreController.h | 5 ++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 356ccf75..95780100 100644 --- a/README.md +++ b/README.md @@ -724,7 +724,7 @@ EIVE implementation ### SD Cards - Folder `bin` for binaries, for example the OBSW -- Folder `misc` for miscellaneous files +- Folder `misc` for miscellaneous files. Contains `ls` for directory listings - Folder `tc` for telecommands - Folder `tm` for telemetry - Folder `xdi` for XDI components (e.g. for firmware or device tree updates) diff --git a/bsp_q7s/core/CoreController.cpp b/bsp_q7s/core/CoreController.cpp index b7dabbb7..a017ecde 100644 --- a/bsp_q7s/core/CoreController.cpp +++ b/bsp_q7s/core/CoreController.cpp @@ -109,6 +109,59 @@ ReturnValue_t CoreController::sdCardSetup(SdCardManager& sdcMan, } } +ReturnValue_t CoreController::executeAction(ActionId_t actionId, MessageQueueId_t commandedBy, + const uint8_t *data, size_t size) { + switch(actionId) { + case(LIST_DIRECTORY_INTO_FILE): { + // TODO: Packet definition for clean deserialization + // Two bytes for a and R flag, at least 5 bytes for minimum valid path /tmp with + // null termination + if(size < 7) { + return HasActionsIF::INVALID_PARAMETERS; + } + // This flag specifies to run ls with -a + bool aFlag = data[0]; + data += 1; + // This flag specifies to run ls with -R + bool RFlag = data[1]; + data += 1; + + size_t remainingSize = size - 2; + // One larger for null termination, which prevents undefined behaviour if the sent + // string is not 0 terminated + std::vector repoAndTargetFileBuffer(remainingSize + 1, 0); + std::memcpy(repoAndTargetFileBuffer.data(), data, remainingSize); + const char* currentCharPtr = reinterpret_cast(repoAndTargetFileBuffer.data()); + // Full target file name + std::string repoName(currentCharPtr); + size_t repoLength = repoName.length(); + // The other string needs to be at least one letter plus NULL termination to be valid at all + // The first string also needs to be NULL terminated, but the termination is not included + // in the string length, so this is subtracted from the remaining size as well + if(repoLength > remainingSize - 3) { + return HasActionsIF::INVALID_PARAMETERS; + } + // The file length will not include the NULL termination, so we skip it + currentCharPtr += repoLength + 1; + std::string targetFileName(currentCharPtr); + std::ostringstream oss; + oss << "ls -l"; + if(aFlag) { + oss << "a"; + } + if(RFlag) { + oss << "R"; + } + oss << " " << repoName << " > " << targetFileName; + std::system(oss.str().c_str()); + return HasReturnvaluesIF::RETURN_OK; + } + default: { + return HasActionsIF::INVALID_ACTION_ID; + } + } +} + ReturnValue_t CoreController::sdCardColdRedundantInit(SdCardManager* sdcMan, SdCardManager::SdStatusPair& statusPair) { sd::SdCard preferredSdCard = sd::SdCard::SLOT_0; diff --git a/bsp_q7s/core/CoreController.h b/bsp_q7s/core/CoreController.h index 8386055b..3e7e108a 100644 --- a/bsp_q7s/core/CoreController.h +++ b/bsp_q7s/core/CoreController.h @@ -8,6 +8,8 @@ class CoreController: public ExtendedControllerBase { public: + static constexpr ActionId_t LIST_DIRECTORY_INTO_FILE = 0; + static constexpr uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::CORE; static constexpr Event ALLOC_FAILURE = event::makeEvent(SUBSYSTEM_ID, 0, severity::MEDIUM); @@ -16,6 +18,9 @@ public: ReturnValue_t initialize() override; + ReturnValue_t executeAction(ActionId_t actionId, + MessageQueueId_t commandedBy, const uint8_t *data, size_t size) override; + ReturnValue_t handleCommandMessage(CommandMessage *message) override; void performControlOperation() override;