Linux CommandExecutor

The CommandExecutor helper class can execute shell commands in blocking and non-blocking mode
This class is able to execute processes by using the Linux popen call. It also has the capability of writing
the read output of a process into a provided ring buffer.

The executor works by first loading the command which should be executed and specifying whether
it should be executed blocking or non-blocking. After that, execution can be started with the execute call.

Using non-blocking mode allows to execute commands which might take a longer time in the background,
and allowing the user thread to check completion status with the check function

Moved to HAL like requested in code review and unit tested with failing commands as well.
Also, Linux HAL components are compiled by default now unless explicitely disabled.
This commit is contained in:
2022-01-26 12:11:52 +01:00
parent bd64a43819
commit 371ff931bf
22 changed files with 608 additions and 57 deletions

View File

@ -21,3 +21,4 @@ add_subdirectory(storagemanager)
add_subdirectory(globalfunctions)
add_subdirectory(timemanager)
add_subdirectory(tmtcpacket)
add_subdirectory(hal)

View File

@ -0,0 +1,3 @@
target_sources(${FSFW_TEST_TGT} PRIVATE
testCommandExecutor.cpp
)

View File

@ -0,0 +1,110 @@
#include <catch2/catch_test_macros.hpp>
#include "fsfw/serviceinterface.h"
#include "fsfw/container/SimpleRingBuffer.h"
#include "fsfw/container/DynamicFIFO.h"
#include "fsfw_hal/linux/CommandExecutor.h"
#include "fsfw/platform.h"
#include <unistd.h>
#include <fstream>
#ifdef PLATFORM_UNIX
static const char TEST_FILE_NAME[] = "/tmp/fsfw-unittest-test.txt";
TEST_CASE( "Command Executor" , "[cmd-exec]") {
// Check blocking mode first
CommandExecutor cmdExecutor(1024);
std::string cmd = "echo \"test\" >> " + std::string(TEST_FILE_NAME);
REQUIRE(cmdExecutor.getCurrentState() == CommandExecutor::States::IDLE);
ReturnValue_t result = cmdExecutor.load(cmd, true, true);
REQUIRE(cmdExecutor.getCurrentState() == CommandExecutor::States::COMMAND_LOADED);
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
REQUIRE(cmdExecutor.execute() == HasReturnvaluesIF::RETURN_OK);
// Check that file exists with contents
std::ifstream file(TEST_FILE_NAME);
std::string line;
std::getline(file, line);
CHECK(line == "test");
std::remove(TEST_FILE_NAME);
REQUIRE(cmdExecutor.getCurrentState() == CommandExecutor::States::IDLE);
// Now check non-blocking mode
SimpleRingBuffer outputBuffer(524, true);
DynamicFIFO<uint16_t> sizesFifo(12);
cmdExecutor.setRingBuffer(&outputBuffer, &sizesFifo);
result = cmdExecutor.load("echo \"Hello World\"", false, false);
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
cmdExecutor.execute();
bool bytesHaveBeenRead = false;
size_t limitIdx = 0;
while(result != CommandExecutor::EXECUTION_FINISHED) {
limitIdx++;
result = cmdExecutor.check(bytesHaveBeenRead);
REQUIRE(result != CommandExecutor::COMMAND_ERROR);
usleep(500);
REQUIRE(limitIdx < 5);
}
limitIdx = 0;
CHECK(bytesHaveBeenRead == true);
CHECK(result == CommandExecutor::EXECUTION_FINISHED);
uint16_t readBytes = 0;
sizesFifo.retrieve(&readBytes);
REQUIRE(readBytes == 12);
REQUIRE(outputBuffer.getAvailableReadData() == 12);
uint8_t readBuffer[32];
REQUIRE(outputBuffer.readData(readBuffer, 12) == HasReturnvaluesIF::RETURN_OK);
CHECK(strcmp(reinterpret_cast<char*>(readBuffer), "Hello World\n") == 0);
outputBuffer.deleteData(12, true);
// Test more complex command
result = cmdExecutor.load("ping -c 1 localhost", false, false);
REQUIRE(cmdExecutor.getCurrentState() == CommandExecutor::States::COMMAND_LOADED);
REQUIRE(cmdExecutor.execute() == HasReturnvaluesIF::RETURN_OK);
REQUIRE(cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING);
limitIdx = 0;
while(result != CommandExecutor::EXECUTION_FINISHED) {
limitIdx++;
result = cmdExecutor.check(bytesHaveBeenRead);
REQUIRE(result != CommandExecutor::COMMAND_ERROR);
usleep(500);
REQUIRE(limitIdx < 20);
}
limitIdx = 0;
CHECK(bytesHaveBeenRead == true);
CHECK(result == CommandExecutor::EXECUTION_FINISHED);
REQUIRE(cmdExecutor.getCurrentState() == CommandExecutor::States::IDLE);
readBytes = 0;
sizesFifo.retrieve(&readBytes);
// That's about the size of the reply
bool beTrue = (readBytes > 200) and (readBytes < 300);
REQUIRE(beTrue);
uint8_t largerReadBuffer[1024] = {};
outputBuffer.readData(largerReadBuffer, readBytes);
// You can also check this output in the debugger
std::string allTheReply(reinterpret_cast<char*>(largerReadBuffer));
// I am just going to assume that this string is the same across ping implementations
// of different Linux systems
REQUIRE(allTheReply.find("localhost ping statistics") != std::string::npos);
// Now check failing command
result = cmdExecutor.load("false", false, false);
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
result = cmdExecutor.execute();
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
while(result != CommandExecutor::EXECUTION_FINISHED and result != HasReturnvaluesIF::RETURN_FAILED) {
limitIdx++;
result = cmdExecutor.check(bytesHaveBeenRead);
REQUIRE(result != CommandExecutor::COMMAND_ERROR);
usleep(500);
REQUIRE(limitIdx < 20);
}
REQUIRE(result == HasReturnvaluesIF::RETURN_FAILED);
REQUIRE(cmdExecutor.getLastError() == 1);
}
#endif