Core Controller Dir Listing FSM #669

Merged
muellerr merged 10 commits from core-ctrl-dir-listing-fsm into v3.0.0-dev 2023-06-09 14:45:40 +02:00
3 changed files with 87 additions and 20 deletions
Showing only changes of commit 06b381d965 - Show all commits

View File

@ -112,6 +112,9 @@ void CoreController::performControlOperation() {
sdStateMachine();
performMountedSdCardOperations();
readHkData();
if (dumpContext.active) {
dirListingDumpHandler();
}
if (shellCmdIsExecuting) {
bool replyReceived = false;
@ -1038,7 +1041,6 @@ ReturnValue_t CoreController::actionListDirectoryDumpDirectly(ActionId_t actionI
return returnvalue::FAILED;
}
}
std::array<uint8_t, 1024> dirListingBuf{};
dirListingBuf[8] = parser.compressionOptionSet();
// First four bytes reserved for segment index. One byte for compression option information
std::strcpy(reinterpret_cast<char *>(dirListingBuf.data() + 2 * sizeof(uint32_t) + 1), repoName);
@ -1047,38 +1049,46 @@ ReturnValue_t CoreController::actionListDirectoryDumpDirectly(ActionId_t actionI
return returnvalue::FAILED;
}
std::error_code e;
size_t totalFileSize = std::filesystem::file_size(LIST_DIR_DUMP_WORK_FILE, e);
uint32_t segmentIdx = 0;
size_t dumpedBytes = 0;
dumpContext.totalFileSize = std::filesystem::file_size(LIST_DIR_DUMP_WORK_FILE, e);
dumpContext.segmentIdx = 0;
dumpContext.dumpedBytes = 0;
size_t nextDumpLen = 0;
size_t dummy = 0;
size_t maxDumpLen = dirListingBuf.size() - 2 * sizeof(uint32_t) - 1 - repoNameLen - 1;
size_t listingDataOffset = 2 * sizeof(uint32_t) + 1 + repoNameLen + 1;
uint32_t chunks = totalFileSize / maxDumpLen;
if (totalFileSize % maxDumpLen != 0) {
dumpContext.maxDumpLen = dirListingBuf.size() - 2 * sizeof(uint32_t) - 1 - repoNameLen - 1;
dumpContext.listingDataOffset = 2 * sizeof(uint32_t) + 1 + repoNameLen + 1;
uint32_t chunks = dumpContext.totalFileSize / dumpContext.maxDumpLen;
if (dumpContext.totalFileSize % dumpContext.maxDumpLen != 0) {
chunks++;
}
SerializeAdapter::serialize(&chunks, dirListingBuf.data() + sizeof(uint32_t), &dummy,
dirListingBuf.size() - sizeof(uint32_t),
SerializeIF::Endianness::NETWORK);
while (dumpedBytes < totalFileSize) {
ifile.seekg(dumpedBytes, std::ios::beg);
nextDumpLen = maxDumpLen;
if (totalFileSize - dumpedBytes < maxDumpLen) {
nextDumpLen = totalFileSize - dumpedBytes;
while (dumpContext.dumpedBytes < dumpContext.totalFileSize) {
ifile.seekg(dumpContext.dumpedBytes, std::ios::beg);
nextDumpLen = dumpContext.maxDumpLen;
if (dumpContext.totalFileSize - dumpContext.dumpedBytes < dumpContext.maxDumpLen) {
nextDumpLen = dumpContext.totalFileSize - dumpContext.dumpedBytes;
}
SerializeAdapter::serialize(&segmentIdx, dirListingBuf.data(), &dummy, dirListingBuf.size(),
SerializeIF::Endianness::NETWORK);
ifile.read(reinterpret_cast<char *>(dirListingBuf.data() + listingDataOffset), nextDumpLen);
SerializeAdapter::serialize(&dumpContext.segmentIdx, dirListingBuf.data(), &dummy,
dirListingBuf.size(), SerializeIF::Endianness::NETWORK);
ifile.read(reinterpret_cast<char *>(dirListingBuf.data() + dumpContext.listingDataOffset),
nextDumpLen);
result = actionHelper.reportData(commandedBy, actionId, dirListingBuf.data(),
listingDataOffset + nextDumpLen);
dumpContext.listingDataOffset + nextDumpLen);
if (result != returnvalue::OK) {
// Remove work file when we are done
std::filesystem::remove(LIST_DIR_DUMP_WORK_FILE, e);
return result;
}
segmentIdx++;
dumpedBytes += nextDumpLen;
dumpContext.segmentIdx++;
dumpContext.dumpedBytes += nextDumpLen;
// Dump takes multiple task cycles, so cache the dump state and continue dump the next cycles.
if (dumpContext.segmentIdx == 25) {
dumpContext.active = true;
dumpContext.commander = commandedBy;
dumpContext.actionId = actionId;
return returnvalue::OK;
}
}
// Remove work file when we are done
std::filesystem::remove(LIST_DIR_DUMP_WORK_FILE, e);
@ -2346,6 +2356,50 @@ MessageQueueId_t CoreController::getCommandQueue() const {
return ExtendedControllerBase::getCommandQueue();
}
void CoreController::dirListingDumpHandler() {
size_t nextDumpLen = 0;
size_t dummy = 0;
ReturnValue_t result;
std::error_code e;
std::ifstream ifile(LIST_DIR_DUMP_WORK_FILE, std::ios::binary);
if (ifile.bad()) {
return;
}
while (dumpContext.dumpedBytes < dumpContext.totalFileSize) {
ifile.seekg(dumpContext.dumpedBytes, std::ios::beg);
nextDumpLen = dumpContext.maxDumpLen;
if (dumpContext.totalFileSize - dumpContext.dumpedBytes < dumpContext.maxDumpLen) {
nextDumpLen = dumpContext.totalFileSize - dumpContext.dumpedBytes;
}
SerializeAdapter::serialize(&dumpContext.segmentIdx, dirListingBuf.data(), &dummy,
dirListingBuf.size(), SerializeIF::Endianness::NETWORK);
ifile.read(reinterpret_cast<char *>(dirListingBuf.data() + dumpContext.listingDataOffset),
nextDumpLen);
result =
actionHelper.reportData(dumpContext.commander, dumpContext.actionId, dirListingBuf.data(),
dumpContext.listingDataOffset + nextDumpLen);
if (result != returnvalue::OK) {
// Remove work file when we are done
std::filesystem::remove(LIST_DIR_DUMP_WORK_FILE, e);
dumpContext.active = false;
actionHelper.finish(false, dumpContext.commander, dumpContext.actionId, result);
return;
}
dumpContext.segmentIdx++;
dumpContext.dumpedBytes += nextDumpLen;
// Dump takes multiple task cycles, so cache the dump state and continue dump the next cycles.
if (dumpContext.segmentIdx == 25) {
break;
}
}
if (dumpContext.dumpedBytes >= dumpContext.totalFileSize) {
actionHelper.finish(true, dumpContext.commander, dumpContext.actionId, result);
dumpContext.active = false;
// Remove work file when we are done
std::filesystem::remove(LIST_DIR_DUMP_WORK_FILE, e);
}
}
bool CoreController::isNumber(const std::string &s) {
return !s.empty() && std::find_if(s.begin(), s.end(),
[](unsigned char c) { return !std::isdigit(c); }) == s.end();

View File

@ -177,6 +177,19 @@ class CoreController : public ExtendedControllerBase, public ReceivesParameterMe
DeviceCommandId_t actionId;
} sdCommandingInfo;
struct DirListingDumpContext {
bool active;
size_t dumpedBytes;
size_t totalFileSize;
size_t listingDataOffset;
size_t maxDumpLen;
uint32_t segmentIdx;
MessageQueueId_t commander = MessageQueueIF::NO_QUEUE;
DeviceCommandId_t actionId;
};
std::array<uint8_t, 1024> dirListingBuf{};
DirListingDumpContext dumpContext{};
RebootFile rebootFile = {};
CommandExecutor cmdExecutor;
@ -274,6 +287,7 @@ class CoreController : public ExtendedControllerBase, public ReceivesParameterMe
void rewriteRebootFile(RebootFile file);
void announceBootCounts();
void readHkData();
void dirListingDumpHandler();
bool isNumber(const std::string& s);
};

View File

@ -3,7 +3,6 @@
#include <cmath>
class ActuatorCmd {
public:
ActuatorCmd();