Filesystem and CFDP updates
This commit is contained in:
@ -4,9 +4,7 @@
|
||||
|
||||
DeviceHandlerMock::DeviceHandlerMock(object_id_t objectId, object_id_t deviceCommunication,
|
||||
CookieIF *comCookie, FailureIsolationBase *fdirInstance)
|
||||
: DeviceHandlerBase(objectId, deviceCommunication, comCookie, fdirInstance) {
|
||||
setMode(MODE_ON);
|
||||
}
|
||||
: DeviceHandlerBase(objectId, deviceCommunication, comCookie, fdirInstance) {}
|
||||
|
||||
DeviceHandlerMock::~DeviceHandlerMock() = default;
|
||||
|
||||
@ -101,3 +99,12 @@ ReturnValue_t DeviceHandlerMock::enablePeriodicReply(DeviceCommandId_t replyId)
|
||||
ReturnValue_t DeviceHandlerMock::disablePeriodicReply(DeviceCommandId_t replyId) {
|
||||
return updatePeriodicReply(false, replyId);
|
||||
}
|
||||
|
||||
ReturnValue_t DeviceHandlerMock::initialize() {
|
||||
ReturnValue_t result = DeviceHandlerBase::initialize();
|
||||
if (result != returnvalue::OK) {
|
||||
return result;
|
||||
}
|
||||
setMode(MODE_ON);
|
||||
return result;
|
||||
}
|
||||
|
@ -21,6 +21,8 @@ class DeviceHandlerMock : public DeviceHandlerBase {
|
||||
ReturnValue_t enablePeriodicReply(DeviceCommandId_t replyId);
|
||||
ReturnValue_t disablePeriodicReply(DeviceCommandId_t replyId);
|
||||
|
||||
ReturnValue_t initialize() override;
|
||||
|
||||
protected:
|
||||
void doStartUp() override;
|
||||
void doShutDown() override;
|
||||
|
@ -1,60 +1,58 @@
|
||||
#include "FilesystemMock.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
|
||||
#include "fsfw/serialize/SerializeIF.h"
|
||||
|
||||
ReturnValue_t FilesystemMock::feedFile(const std::string &filename, std::ifstream &file) {
|
||||
ReturnValue_t FilesystemMock::feedFile(const std::string& filename, std::ifstream& file) {
|
||||
if (not std::filesystem::exists(filename)) {
|
||||
return returnvalue::FAILED;
|
||||
}
|
||||
size_t fileSize = std::filesystem::file_size(filename);
|
||||
FileOpParams params(filename.c_str(), fileSize);
|
||||
std::vector<uint8_t> rawData(fileSize);
|
||||
file.read(reinterpret_cast<char *>(rawData.data()), static_cast<unsigned int>(rawData.size()));
|
||||
createOrAddToFile(params, rawData.data());
|
||||
file.read(reinterpret_cast<char*>(rawData.data()), static_cast<unsigned int>(rawData.size()));
|
||||
createOrAddToFile(filename.data(), 0, rawData.data(), rawData.size());
|
||||
return returnvalue::OK;
|
||||
}
|
||||
|
||||
ReturnValue_t FilesystemMock::writeToFile(FileOpParams params, const uint8_t *data) {
|
||||
createOrAddToFile(params, data);
|
||||
ReturnValue_t FilesystemMock::writeToFile(const char* path, size_t offset, const uint8_t* data,
|
||||
size_t size) {
|
||||
createOrAddToFile(path, offset, data, size);
|
||||
return returnvalue::OK;
|
||||
}
|
||||
|
||||
ReturnValue_t FilesystemMock::readFromFile(FileOpParams params, uint8_t **buffer, size_t &readSize,
|
||||
size_t maxSize) {
|
||||
std::string filename(params.path());
|
||||
auto iter = fileMap.find(filename);
|
||||
ReturnValue_t FilesystemMock::readFromFile(const char* path, size_t offset, size_t size,
|
||||
uint8_t* buffer, size_t& readSize, size_t maxSize) {
|
||||
const std::string filename(path);
|
||||
const auto iter = fileMap.find(filename);
|
||||
if (iter == fileMap.end()) {
|
||||
return HasFileSystemIF::FILE_DOES_NOT_EXIST;
|
||||
} else {
|
||||
FileInfo &info = iter->second;
|
||||
size_t readLen = params.size;
|
||||
if (params.offset + params.size > info.fileRaw.size()) {
|
||||
if (params.offset > info.fileRaw.size()) {
|
||||
return returnvalue::OK;
|
||||
}
|
||||
readLen = info.fileRaw.size() - params.offset;
|
||||
}
|
||||
if (readSize + readLen > maxSize) {
|
||||
return SerializeIF::STREAM_TOO_SHORT;
|
||||
}
|
||||
std::copy(info.fileRaw.data() + params.offset, info.fileRaw.data() + params.offset + readLen,
|
||||
*buffer);
|
||||
*buffer += readLen;
|
||||
readSize += readLen;
|
||||
}
|
||||
auto& [fileSegQueue, fileRaw] = iter->second;
|
||||
size_t readLen = size;
|
||||
if (offset + size > fileRaw.size()) {
|
||||
if (offset > fileRaw.size()) {
|
||||
return returnvalue::OK;
|
||||
}
|
||||
readLen = fileRaw.size() - offset;
|
||||
}
|
||||
if (readSize + readLen > maxSize) {
|
||||
return SerializeIF::STREAM_TOO_SHORT;
|
||||
}
|
||||
// std::copy(info.fileRaw.data() + offset, info.fileRaw.data() + offset + readLen, *buffer);
|
||||
std::memcpy(buffer, fileRaw.data() + offset, readLen);
|
||||
readSize = readLen;
|
||||
return returnvalue::OK;
|
||||
}
|
||||
|
||||
ReturnValue_t FilesystemMock::createFile(FilesystemParams params, const uint8_t *data,
|
||||
size_t size) {
|
||||
FileOpParams params2(params.path, size);
|
||||
createOrAddToFile(params2, data);
|
||||
ReturnValue_t FilesystemMock::createFile(const char* path, const uint8_t* data, size_t size) {
|
||||
createOrAddToFile(path, 0, data, size);
|
||||
return returnvalue::OK;
|
||||
}
|
||||
|
||||
ReturnValue_t FilesystemMock::removeFile(const char *path, FileSystemArgsIF *args) {
|
||||
ReturnValue_t FilesystemMock::removeFile(const char* path, FileSystemArgsIF* args) {
|
||||
std::string filename(path);
|
||||
auto iter = fileMap.find(filename);
|
||||
if (iter == fileMap.end()) {
|
||||
@ -65,56 +63,58 @@ ReturnValue_t FilesystemMock::removeFile(const char *path, FileSystemArgsIF *arg
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t FilesystemMock::createDirectory(FilesystemParams params, bool createParentDirs) {
|
||||
std::string dirPath = params.path;
|
||||
ReturnValue_t FilesystemMock::createDirectory(const char* path, bool createParentDirs,
|
||||
FileSystemArgsIF* args) {
|
||||
std::string dirPath = path;
|
||||
dirMap[dirPath].createCallCount++;
|
||||
dirMap[dirPath].wihParentDir.push(createParentDirs);
|
||||
return returnvalue::OK;
|
||||
}
|
||||
|
||||
ReturnValue_t FilesystemMock::removeDirectory(FilesystemParams params, bool deleteRecurively) {
|
||||
std::string dirPath = params.path;
|
||||
ReturnValue_t FilesystemMock::removeDirectory(const char* path, bool deleteRecurively,
|
||||
FileSystemArgsIF* args) {
|
||||
std::string dirPath = path;
|
||||
dirMap[dirPath].delCallCount++;
|
||||
dirMap[dirPath].recursiveDeletion.push(deleteRecurively);
|
||||
return returnvalue::OK;
|
||||
}
|
||||
|
||||
ReturnValue_t FilesystemMock::rename(const char *oldPath, const char *newPath,
|
||||
FileSystemArgsIF *args) {
|
||||
ReturnValue_t FilesystemMock::rename(const char* oldPath, const char* newPath,
|
||||
FileSystemArgsIF* args) {
|
||||
renameQueue.emplace(oldPath, newPath);
|
||||
return returnvalue::OK;
|
||||
}
|
||||
|
||||
void FilesystemMock::createOrAddToFile(FileOpParams params, const uint8_t *data) {
|
||||
std::string filename(params.path());
|
||||
void FilesystemMock::createOrAddToFile(const char* path, size_t offset, const uint8_t* data,
|
||||
size_t dataSize) {
|
||||
std::string filename(path);
|
||||
auto iter = fileMap.find(filename);
|
||||
if (iter == fileMap.end()) {
|
||||
FileSegmentQueue queue;
|
||||
if (params.size > 0) {
|
||||
queue.emplace(filename, params.offset, data, params.size);
|
||||
if (dataSize > 0) {
|
||||
queue.emplace(filename, offset, data, dataSize);
|
||||
}
|
||||
FileInfo info;
|
||||
info.fileSegQueue = queue;
|
||||
if (data != nullptr) {
|
||||
info.fileRaw.insert(info.fileRaw.end(), data, data + params.size);
|
||||
info.fileRaw.insert(info.fileRaw.end(), data, data + dataSize);
|
||||
}
|
||||
fileMap.emplace(filename, info);
|
||||
} else {
|
||||
FileInfo &info = iter->second;
|
||||
info.fileSegQueue.emplace(filename, params.offset, data, params.size);
|
||||
FileInfo& info = iter->second;
|
||||
info.fileSegQueue.emplace(filename, offset, data, dataSize);
|
||||
if (data == nullptr) {
|
||||
return;
|
||||
}
|
||||
// Easiest case: append data to the end
|
||||
if (params.offset == info.fileRaw.size()) {
|
||||
info.fileRaw.insert(info.fileRaw.end(), data, data + params.size);
|
||||
if (offset == info.fileRaw.size()) {
|
||||
info.fileRaw.insert(info.fileRaw.end(), data, data + dataSize);
|
||||
} else {
|
||||
size_t totalNewLen = params.offset + params.size;
|
||||
size_t totalNewLen = offset + dataSize;
|
||||
if (totalNewLen > info.fileRaw.size()) {
|
||||
info.fileRaw.resize(params.offset + params.size);
|
||||
info.fileRaw.resize(offset + dataSize);
|
||||
}
|
||||
std::copy(data, data + params.size,
|
||||
info.fileRaw.begin() + static_cast<unsigned int>(params.offset));
|
||||
std::copy(data, data + dataSize, info.fileRaw.begin() + static_cast<unsigned int>(offset));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -126,8 +126,8 @@ void FilesystemMock::reset() {
|
||||
std::swap(renameQueue, empty);
|
||||
}
|
||||
|
||||
bool FilesystemMock::fileExists(FilesystemParams params) {
|
||||
std::string filename(params.path);
|
||||
bool FilesystemMock::fileExists(const char* path, FileSystemArgsIF* args) {
|
||||
std::string filename(path);
|
||||
auto iter = fileMap.find(filename);
|
||||
if (iter == fileMap.end()) {
|
||||
return false;
|
||||
@ -135,20 +135,21 @@ bool FilesystemMock::fileExists(FilesystemParams params) {
|
||||
return true;
|
||||
}
|
||||
|
||||
ReturnValue_t FilesystemMock::truncateFile(FilesystemParams params) {
|
||||
truncateCalledOnFile = params.path;
|
||||
ReturnValue_t FilesystemMock::truncateFile(const char* path, FileSystemArgsIF* args) {
|
||||
truncateCalledOnFile = path;
|
||||
return returnvalue::OK;
|
||||
}
|
||||
|
||||
ReturnValue_t FilesystemMock::getBaseFilename(FilesystemParams params, char *nameBuf, size_t maxLen,
|
||||
size_t &baseNameLen) {
|
||||
ReturnValue_t FilesystemMock::getBaseFilename(const char* path, char* nameBuf, size_t maxLen,
|
||||
size_t& baseNameLen) {
|
||||
return returnvalue::OK;
|
||||
}
|
||||
|
||||
bool FilesystemMock::isDirectory(const char *path) { return false; }
|
||||
ReturnValue_t FilesystemMock::isDirectory(const char* path, bool& isDirectory) { return false; }
|
||||
|
||||
bool FilesystemMock::getFileSize(FilesystemParams params, size_t &fileSize) {
|
||||
std::string filename(params.path);
|
||||
ReturnValue_t FilesystemMock::getFileSize(const char* path, uint64_t& fileSize,
|
||||
FileSystemArgsIF* args) {
|
||||
std::string filename(path);
|
||||
auto iter = fileMap.find(filename);
|
||||
if (iter != fileMap.end()) {
|
||||
fileSize = iter->second.fileRaw.size();
|
||||
|
@ -20,7 +20,7 @@
|
||||
class FilesystemMock : public HasFileSystemIF {
|
||||
public:
|
||||
struct FileWriteInfo {
|
||||
FileWriteInfo(std::string filename, size_t offset, const uint8_t *data, size_t len)
|
||||
FileWriteInfo(std::string filename, size_t offset, const uint8_t* data, size_t len)
|
||||
: filename(std::move(filename)), offset(offset) {
|
||||
this->data.insert(this->data.end(), data, data + len);
|
||||
}
|
||||
@ -54,24 +54,34 @@ class FilesystemMock : public HasFileSystemIF {
|
||||
};
|
||||
std::queue<RenameInfo> renameQueue;
|
||||
std::string truncateCalledOnFile;
|
||||
ReturnValue_t feedFile(const std::string &filename, std::ifstream &file);
|
||||
ReturnValue_t feedFile(const std::string& filename, std::ifstream& file);
|
||||
|
||||
ReturnValue_t getBaseFilename(FilesystemParams params, char *nameBuf, size_t maxLen,
|
||||
size_t &baseNameLen) override;
|
||||
ReturnValue_t getBaseFilename(const char* path, char* nameBuf, size_t maxLen,
|
||||
size_t& baseNameLen) override;
|
||||
|
||||
bool isDirectory(const char *path) override;
|
||||
bool fileExists(FilesystemParams params) override;
|
||||
ReturnValue_t truncateFile(FilesystemParams params) override;
|
||||
bool getFileSize(FilesystemParams params, size_t &fileSize) override;
|
||||
ReturnValue_t isDirectory(const char* path, bool& isDirectory) override;
|
||||
|
||||
ReturnValue_t writeToFile(FileOpParams params, const uint8_t *data) override;
|
||||
ReturnValue_t readFromFile(FileOpParams params, uint8_t **buffer, size_t &readSize,
|
||||
size_t maxSize) override;
|
||||
ReturnValue_t createFile(FilesystemParams params, const uint8_t *data, size_t size) override;
|
||||
ReturnValue_t removeFile(const char *path, FileSystemArgsIF *args) override;
|
||||
ReturnValue_t createDirectory(FilesystemParams params, bool createParentDirs) override;
|
||||
ReturnValue_t removeDirectory(FilesystemParams params, bool deleteRecurively) override;
|
||||
ReturnValue_t rename(const char *oldPath, const char *newPath, FileSystemArgsIF *args) override;
|
||||
bool fileExists(const char* path, FileSystemArgsIF* args) override;
|
||||
|
||||
ReturnValue_t truncateFile(const char* path, FileSystemArgsIF* args) override;
|
||||
|
||||
ReturnValue_t getFileSize(const char* path, uint64_t& fileSize, FileSystemArgsIF* args) override;
|
||||
|
||||
ReturnValue_t writeToFile(const char* path, size_t offset, const uint8_t* data,
|
||||
size_t size) override;
|
||||
|
||||
ReturnValue_t readFromFile(const char* path, size_t offset, size_t size, uint8_t* buffer,
|
||||
size_t& readSize, size_t maxSize) override;
|
||||
|
||||
ReturnValue_t createFile(const char* path, const uint8_t* data, size_t size) override;
|
||||
ReturnValue_t removeFile(const char* path, FileSystemArgsIF* args) override;
|
||||
|
||||
ReturnValue_t createDirectory(const char* path, bool createParentDirs,
|
||||
FileSystemArgsIF* args) override;
|
||||
|
||||
ReturnValue_t removeDirectory(const char* path, bool deleteRecurively,
|
||||
FileSystemArgsIF* args) override;
|
||||
ReturnValue_t rename(const char* oldPath, const char* newPath, FileSystemArgsIF* args) override;
|
||||
|
||||
void reset();
|
||||
|
||||
@ -80,7 +90,7 @@ class FilesystemMock : public HasFileSystemIF {
|
||||
using HasFileSystemIF::readFromFile;
|
||||
|
||||
private:
|
||||
void createOrAddToFile(FileOpParams params, const uint8_t *data);
|
||||
void createOrAddToFile(const char* path, size_t offset, const uint8_t* data, size_t dataSize);
|
||||
};
|
||||
|
||||
#endif // FSFW_MOCKS_FILESYSTEMMOCK_H
|
||||
|
Reference in New Issue
Block a user