diff --git a/src/fsfw/tmtcservices/SpacePacketParser.cpp b/src/fsfw/tmtcservices/SpacePacketParser.cpp index 75db7a4e..440a988e 100644 --- a/src/fsfw/tmtcservices/SpacePacketParser.cpp +++ b/src/fsfw/tmtcservices/SpacePacketParser.cpp @@ -1,11 +1,16 @@ #include #include +#include #include SpacePacketParser::SpacePacketParser(std::vector validPacketIds) : validPacketIds(validPacketIds) {} +void SpacePacketParser::enableCrcCheck() { + checkCrc = true; +} + ReturnValue_t SpacePacketParser::parseSpacePackets(const uint8_t** buffer, const size_t maxSize, FoundPacketInfo& packetInfo) { if (buffer == nullptr or nextStartIdx > maxSize) { @@ -31,6 +36,11 @@ ReturnValue_t SpacePacketParser::parseSpacePackets(const uint8_t** buffer, const } *buffer += packetInfo.sizeFound; packetInfo.startIdx = localIdx + amountRead; + if (checkCrc) { + if (CRC::crc16ccitt(bufPtr + localIdx, packetSize) != 0) { + return CRC_CHECK_FAILED; + } + } nextStartIdx = localIdx + amountRead + packetInfo.sizeFound; amountRead = nextStartIdx; return result; diff --git a/src/fsfw/tmtcservices/SpacePacketParser.h b/src/fsfw/tmtcservices/SpacePacketParser.h index ea0a2feb..ea780bed 100644 --- a/src/fsfw/tmtcservices/SpacePacketParser.h +++ b/src/fsfw/tmtcservices/SpacePacketParser.h @@ -25,6 +25,13 @@ class SpacePacketParser { 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 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. @@ -61,6 +68,7 @@ class SpacePacketParser { std::vector validPacketIds; size_t nextStartIdx = 0; size_t amountRead = 0; + bool checkCrc = false; }; #endif /* FRAMEWORK_TMTCSERVICES_PUSPARSER_H_ */