more simplfications

This commit is contained in:
Robin Müller 2022-09-01 10:51:09 +02:00
parent 28ecd0e5c6
commit 01651f0521
3 changed files with 40 additions and 73 deletions

View File

@ -335,13 +335,13 @@ ReturnValue_t TcpTmTcServer::handleTcRingBufferData(size_t availableReadData) {
} }
ringBuffer.readData(receptionBuffer.data(), readAmount, true); ringBuffer.readData(receptionBuffer.data(), readAmount, true);
const uint8_t* bufPtr = receptionBuffer.data(); const uint8_t* bufPtr = receptionBuffer.data();
FoundPacketInfo info; SpacePacketParser::FoundPacketInfo info;
ParsingState parseState; if (spacePacketParser == nullptr) {
while (parseState.amountRead < readAmount) { return returnvalue::FAILED;
if (spacePacketParser == nullptr) { }
return returnvalue::FAILED; spacePacketParser->reset();
} while (spacePacketParser->getAmountRead() < readAmount) {
result = spacePacketParser->parseSpacePackets(&bufPtr, readAmount, info, parseState); result = spacePacketParser->parseSpacePackets(&bufPtr, readAmount, info);
switch (result) { switch (result) {
case (SpacePacketParser::NO_PACKET_FOUND): case (SpacePacketParser::NO_PACKET_FOUND):
case (SpacePacketParser::SPLIT_PACKET): { case (SpacePacketParser::SPLIT_PACKET): {
@ -356,7 +356,6 @@ ReturnValue_t TcpTmTcServer::handleTcRingBufferData(size_t availableReadData) {
} }
ringBuffer.deleteData(info.sizeFound); ringBuffer.deleteData(info.sizeFound);
lastRingBufferSize = ringBuffer.getAvailableReadData(); lastRingBufferSize = ringBuffer.getAvailableReadData();
// std::memset(receptionBuffer.data() + startIdx, 0, foundSize);
} }
return status; return status;
} }

View File

@ -6,17 +6,9 @@
SpacePacketParser::SpacePacketParser(std::vector<uint16_t> validPacketIds) SpacePacketParser::SpacePacketParser(std::vector<uint16_t> validPacketIds)
: validPacketIds(validPacketIds) {} : validPacketIds(validPacketIds) {}
ReturnValue_t SpacePacketParser::parseSpacePackets(const uint8_t* buffer, const size_t maxSize,
FoundPacketInfo& packetInfo,
ParsingState& parsingState) {
const uint8_t** tempPtr = &buffer;
return parseSpacePackets(tempPtr, maxSize, packetInfo, parsingState);
}
ReturnValue_t SpacePacketParser::parseSpacePackets(const uint8_t** buffer, const size_t maxSize, ReturnValue_t SpacePacketParser::parseSpacePackets(const uint8_t** buffer, const size_t maxSize,
FoundPacketInfo& packetInfo, FoundPacketInfo& packetInfo) {
ParsingState& parsingState) { if (buffer == nullptr or nextStartIdx > maxSize) {
if (buffer == nullptr or parsingState.nextStartIdx > maxSize) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "SpacePacketParser::parseSpacePackets: Frame invalid" << std::endl; sif::warning << "SpacePacketParser::parseSpacePackets: Frame invalid" << std::endl;
#else #else
@ -30,11 +22,7 @@ ReturnValue_t SpacePacketParser::parseSpacePackets(const uint8_t** buffer, const
uint16_t lengthField = (bufPtr[localIdx + 4] << 8) | bufPtr[localIdx + 5]; uint16_t lengthField = (bufPtr[localIdx + 4] << 8) | bufPtr[localIdx + 5];
size_t packetSize = lengthField + 7; size_t packetSize = lengthField + 7;
ReturnValue_t result = returnvalue::OK; ReturnValue_t result = returnvalue::OK;
if (lengthField == 0) { if (packetSize + localIdx + amountRead > maxSize) {
// Skip whole header for now
packetInfo.sizeFound = 6;
result = NO_PACKET_FOUND;
} else if (packetSize + localIdx + parsingState.amountRead > maxSize) {
// Don't increment buffer and read length here, user has to decide what to do // Don't increment buffer and read length here, user has to decide what to do
packetInfo.sizeFound = packetSize; packetInfo.sizeFound = packetSize;
return SPLIT_PACKET; return SPLIT_PACKET;
@ -42,20 +30,20 @@ ReturnValue_t SpacePacketParser::parseSpacePackets(const uint8_t** buffer, const
packetInfo.sizeFound = packetSize; packetInfo.sizeFound = packetSize;
} }
*buffer += packetInfo.sizeFound; *buffer += packetInfo.sizeFound;
packetInfo.startIdx = localIdx + parsingState.amountRead; packetInfo.startIdx = localIdx + amountRead;
parsingState.nextStartIdx = localIdx + parsingState.amountRead + packetInfo.sizeFound; nextStartIdx = localIdx + amountRead + packetInfo.sizeFound;
parsingState.amountRead = parsingState.nextStartIdx; amountRead = nextStartIdx;
return result; return result;
}; };
size_t idx = 0; size_t idx = 0;
// Space packet ID as start marker // Space packet ID as start marker
if (validPacketIds.size() > 0) { if (validPacketIds.size() > 0) {
while (idx + parsingState.amountRead < maxSize - 5) { while (idx + amountRead < maxSize - 5) {
uint16_t currentPacketId = (bufPtr[idx] << 8) | bufPtr[idx + 1]; uint16_t currentPacketId = (bufPtr[idx] << 8) | bufPtr[idx + 1];
if (std::find(validPacketIds.begin(), validPacketIds.end(), currentPacketId) != if (std::find(validPacketIds.begin(), validPacketIds.end(), currentPacketId) !=
validPacketIds.end()) { validPacketIds.end()) {
if (idx + parsingState.amountRead >= maxSize - 5) { if (idx + amountRead >= maxSize - 5) {
return SPLIT_PACKET; return SPLIT_PACKET;
} }
return verifyLengthField(idx); return verifyLengthField(idx);
@ -63,9 +51,9 @@ ReturnValue_t SpacePacketParser::parseSpacePackets(const uint8_t** buffer, const
idx++; idx++;
} }
} }
parsingState.nextStartIdx = maxSize; nextStartIdx = maxSize;
packetInfo.sizeFound = maxSize; packetInfo.sizeFound = maxSize;
parsingState.amountRead = maxSize; amountRead = maxSize;
*buffer += maxSize; *buffer += maxSize;
return NO_PACKET_FOUND; return NO_PACKET_FOUND;
} }

View File

@ -7,16 +7,6 @@
#include "fsfw/container/DynamicFIFO.h" #include "fsfw/container/DynamicFIFO.h"
#include "fsfw/returnvalues/FwClassIds.h" #include "fsfw/returnvalues/FwClassIds.h"
struct FoundPacketInfo {
size_t startIdx = 0;
size_t sizeFound = 0;
};
struct ParsingState {
size_t nextStartIdx = 0;
size_t amountRead = 0;
};
/** /**
* @brief This small helper class scans a given buffer for space packets. * @brief This small helper class scans a given buffer for space packets.
* Can be used if space packets are serialized in a tightly packed frame. * Can be used if space packets are serialized in a tightly packed frame.
@ -27,9 +17,11 @@ struct ParsingState {
*/ */
class SpacePacketParser { class SpacePacketParser {
public: public:
//! The first entry is the index inside the buffer while the second index
//! is the size of the PUS packet starting at that index. struct FoundPacketInfo {
using IndexSizePair = std::pair<size_t, size_t>; size_t startIdx = 0;
size_t sizeFound = 0;
};
static constexpr uint8_t INTERFACE_ID = CLASS_ID::SPACE_PACKET_PARSER; static constexpr uint8_t INTERFACE_ID = CLASS_ID::SPACE_PACKET_PARSER;
static constexpr ReturnValue_t NO_PACKET_FOUND = MAKE_RETURN_CODE(0x00); static constexpr ReturnValue_t NO_PACKET_FOUND = MAKE_RETURN_CODE(0x00);
@ -46,44 +38,32 @@ class SpacePacketParser {
SpacePacketParser(std::vector<uint16_t> validPacketIds); SpacePacketParser(std::vector<uint16_t> validPacketIds);
/** /**
* Parse a given frame for space packets but also increment the given buffer and assign the * Parse a given frame for space packets but also increments the given buffer.
* total number of bytes read so far
* @param buffer Parser will look for space packets in this buffer * @param buffer Parser will look for space packets in this buffer
* @param maxSize Maximum size of the buffer * @param maxSize Maximum size of the buffer
* @param startIndex Start index of a found space packet * @param packetInfo Information about packets found.
* @param foundSize Found size of the space packet * -@c NO_PACKET_FOUND if no packet was found in the given buffer
* @param readLen Length read so far. This value is incremented by the number of parsed * -@c SPLIT_PACKET if a packet was found but the detected size exceeds maxSize. packetInfo
* bytes which also includes the size of a found packet * will contain the detected packet size and start index.
* -@c NO_PACKET_FOUND if no packet was found in the given buffer or the length field is * -@c returnvalue::OK if a packet was found. Packet size and start index will be set in
* invalid. foundSize will be set to the size of the space packet header. buffer and * packetInfo
* readLen will be incremented accordingly.
* -@c SPLIT_PACKET if a packet was found but the detected size exceeds maxSize. foundSize
* will be set to the detected packet size and startIndex will be set to the start of the
* detected packet. buffer and read length will not be incremented but the found length
* will be assigned.
* -@c returnvalue::OK if a packet was found
*/ */
ReturnValue_t parseSpacePackets(const uint8_t** buffer, const size_t maxSize, ReturnValue_t parseSpacePackets(const uint8_t** buffer, const size_t maxSize,
FoundPacketInfo& packetInfo, ParsingState& parsingState); FoundPacketInfo& packetInfo);
/** size_t getAmountRead() {
* Parse a given frame for space packets return amountRead;
* @param buffer Parser will look for space packets in this buffer }
* @param maxSize Maximum size of the buffer
* @param startIndex Start index of a found space packet void reset() {
* @param foundSize Found size of the space packet nextStartIdx = 0;
* -@c NO_PACKET_FOUND if no packet was found in the given buffer or the length field is amountRead = 0;
* invalid. foundSize will be set to the size of the space packet header }
* -@c SPLIT_PACKET if a packet was found but the detected size exceeds maxSize. foundSize
* will be set to the detected packet size and startIndex will be set to the start of the
* detected packet
* -@c returnvalue::OK if a packet was found
*/
ReturnValue_t parseSpacePackets(const uint8_t* buffer, const size_t maxSize,
FoundPacketInfo& packetInfo, ParsingState& parsingState);
private: private:
std::vector<uint16_t> validPacketIds; std::vector<uint16_t> validPacketIds;
size_t nextStartIdx = 0;
size_t amountRead = 0;
}; };
#endif /* FRAMEWORK_TMTCSERVICES_PUSPARSER_H_ */ #endif /* FRAMEWORK_TMTCSERVICES_PUSPARSER_H_ */