fsfw/src/fsfw/tmtcservices/SpacePacketParser.cpp

65 lines
2.2 KiB
C++
Raw Normal View History

#include <fsfw/serviceinterface/ServiceInterface.h>
2022-02-02 10:29:30 +01:00
#include <fsfw/tmtcservices/SpacePacketParser.h>
#include <algorithm>
2022-02-02 10:29:30 +01:00
SpacePacketParser::SpacePacketParser(std::vector<uint16_t> validPacketIds)
: validPacketIds(validPacketIds) {}
2022-02-02 10:29:30 +01:00
ReturnValue_t SpacePacketParser::parseSpacePackets(const uint8_t** buffer, const size_t maxSize,
2022-09-01 10:51:09 +02:00
FoundPacketInfo& packetInfo) {
if (buffer == nullptr or nextStartIdx > maxSize) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "SpacePacketParser::parseSpacePackets: Frame invalid" << std::endl;
#else
2022-02-02 10:29:30 +01:00
sif::printWarning("SpacePacketParser::parseSpacePackets: Frame invalid\n");
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
const uint8_t* bufPtr = *buffer;
2022-09-01 08:51:12 +02:00
auto verifyLengthField = [&](size_t localIdx) {
uint16_t lengthField = (bufPtr[localIdx + 4] << 8) | bufPtr[localIdx + 5];
2022-02-02 10:29:30 +01:00
size_t packetSize = lengthField + 7;
2022-08-15 20:28:16 +02:00
ReturnValue_t result = returnvalue::OK;
2022-09-01 10:51:09 +02:00
if (packetSize + localIdx + amountRead > maxSize) {
2022-02-02 10:29:30 +01:00
// Don't increment buffer and read length here, user has to decide what to do
2022-09-01 08:51:12 +02:00
packetInfo.sizeFound = packetSize;
2022-02-02 10:29:30 +01:00
return SPLIT_PACKET;
} else {
2022-09-01 08:51:12 +02:00
packetInfo.sizeFound = packetSize;
2022-02-02 10:29:30 +01:00
}
2022-09-01 08:51:12 +02:00
*buffer += packetInfo.sizeFound;
2022-09-01 10:51:09 +02:00
packetInfo.startIdx = localIdx + amountRead;
nextStartIdx = localIdx + amountRead + packetInfo.sizeFound;
amountRead = nextStartIdx;
2022-02-02 10:29:30 +01:00
return result;
};
2022-02-02 10:29:30 +01:00
size_t idx = 0;
// Space packet ID as start marker
if (validPacketIds.size() > 0) {
2022-09-01 10:51:09 +02:00
while (idx + amountRead < maxSize - 5) {
2022-09-01 08:51:12 +02:00
uint16_t currentPacketId = (bufPtr[idx] << 8) | bufPtr[idx + 1];
2022-02-02 10:29:30 +01:00
if (std::find(validPacketIds.begin(), validPacketIds.end(), currentPacketId) !=
validPacketIds.end()) {
2022-09-01 10:51:09 +02:00
if (idx + amountRead >= maxSize - 5) {
2022-02-02 10:29:30 +01:00
return SPLIT_PACKET;
}
return verifyLengthField(idx);
2022-02-02 10:29:30 +01:00
} else {
idx++;
}
}
2022-09-01 10:51:09 +02:00
nextStartIdx = maxSize;
2022-09-01 08:51:12 +02:00
packetInfo.sizeFound = maxSize;
2022-09-01 10:51:09 +02:00
amountRead = maxSize;
2022-09-01 08:51:12 +02:00
*buffer += maxSize;
2022-02-02 10:29:30 +01:00
return NO_PACKET_FOUND;
}
// Assume that the user verified a valid start of a space packet
else {
return verifyLengthField(idx);
}
}