added optional crc check to space packet parsing

This commit is contained in:
Jakob Meier 2024-11-15 10:11:19 +01:00
parent d34db0a3be
commit 3b68a3ed1d
2 changed files with 18 additions and 0 deletions

View File

@ -1,11 +1,16 @@
#include <fsfw/serviceinterface/ServiceInterface.h> #include <fsfw/serviceinterface/ServiceInterface.h>
#include <fsfw/tmtcservices/SpacePacketParser.h> #include <fsfw/tmtcservices/SpacePacketParser.h>
#include <fsfw/globalfunctions/CRC.h>
#include <algorithm> #include <algorithm>
SpacePacketParser::SpacePacketParser(std::vector<uint16_t> validPacketIds) SpacePacketParser::SpacePacketParser(std::vector<uint16_t> validPacketIds)
: validPacketIds(validPacketIds) {} : validPacketIds(validPacketIds) {}
void SpacePacketParser::enableCrcCheck() {
checkCrc = true;
}
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) {
if (buffer == nullptr or nextStartIdx > maxSize) { if (buffer == nullptr or nextStartIdx > maxSize) {
@ -31,6 +36,11 @@ ReturnValue_t SpacePacketParser::parseSpacePackets(const uint8_t** buffer, const
} }
*buffer += packetInfo.sizeFound; *buffer += packetInfo.sizeFound;
packetInfo.startIdx = localIdx + amountRead; packetInfo.startIdx = localIdx + amountRead;
if (checkCrc) {
if (CRC::crc16ccitt(bufPtr + localIdx, packetSize) != 0) {
return CRC_CHECK_FAILED;
}
}
nextStartIdx = localIdx + amountRead + packetInfo.sizeFound; nextStartIdx = localIdx + amountRead + packetInfo.sizeFound;
amountRead = nextStartIdx; amountRead = nextStartIdx;
return result; return result;

View File

@ -25,6 +25,13 @@ class SpacePacketParser {
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);
static constexpr ReturnValue_t SPLIT_PACKET = MAKE_RETURN_CODE(0x01); static constexpr ReturnValue_t SPLIT_PACKET = MAKE_RETURN_CODE(0x01);
static constexpr ReturnValue_t CRC_CHECK_FAILED = MAKE_RETURN_CODE(0x02);
/**
* brief If the last to bytes of a space packet hold a CRC16, this function can be used to enable
* the CRC16 check when parsing the data.
*/
void enableCrcCheck();
/** /**
* @brief Parser constructor. * @brief Parser constructor.
@ -61,6 +68,7 @@ class SpacePacketParser {
std::vector<uint16_t> validPacketIds; std::vector<uint16_t> validPacketIds;
size_t nextStartIdx = 0; size_t nextStartIdx = 0;
size_t amountRead = 0; size_t amountRead = 0;
bool checkCrc = false;
}; };
#endif /* FRAMEWORK_TMTCSERVICES_PUSPARSER_H_ */ #endif /* FRAMEWORK_TMTCSERVICES_PUSPARSER_H_ */