Skip to content
Snippets Groups Projects
Commit 31bee53b authored by Sedictious's avatar Sedictious
Browse files

Append CRC to packets

parent d3ac99a9
Branches
No related tags found
No related merge requests found
...@@ -145,4 +145,10 @@ ...@@ -145,4 +145,10 @@
* @brief Size of the array holding the Parameter objects for the ST[20] parameter service * @brief Size of the array holding the Parameter objects for the ST[20] parameter service
*/ */
#define ECSS_PARAMETER_COUNT 3 #define ECSS_PARAMETER_COUNT 3
/**
* @brief Defines whether the optional CRC field is included
*/
#define ECSS_CRC_INCLUDE true
#endif // ECSS_SERVICES_ECSS_DEFINITIONS_H #endif // ECSS_SERVICES_ECSS_DEFINITIONS_H
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "macros.hpp" #include "macros.hpp"
#include "Services/TestService.hpp" #include "Services/TestService.hpp"
#include "Services/RequestVerificationService.hpp" #include "Services/RequestVerificationService.hpp"
#include "Helpers/CRCHelper.hpp"
void MessageParser::execute(Message& message) { void MessageParser::execute(Message& message) {
switch (message.serviceType) { switch (message.serviceType) {
...@@ -194,6 +195,14 @@ String<CCSDS_MAX_MESSAGE_SIZE> MessageParser::compose(const Message& message) { ...@@ -194,6 +195,14 @@ String<CCSDS_MAX_MESSAGE_SIZE> MessageParser::compose(const Message& message) {
String<CCSDS_MAX_MESSAGE_SIZE> ccsdsMessage(header, 6); String<CCSDS_MAX_MESSAGE_SIZE> ccsdsMessage(header, 6);
ccsdsMessage.append(ecssMessage); ccsdsMessage.append(ecssMessage);
#if ECSS_CRC_INCLUDE
// Append CRC field
uint16_t crcField = CRCHelper::calculateCRC(reinterpret_cast<uint8_t*>(ccsdsMessage.data()), 6 +
packetDataLength);
ccsdsMessage.push_back(static_cast<uint8_t>(crcField >> 8U));
ccsdsMessage.push_back(static_cast<uint8_t>(crcField & 0xFF));
#endif
return ccsdsMessage; return ccsdsMessage;
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <Services/RequestVerificationService.hpp> #include <Services/RequestVerificationService.hpp>
#include <Message.hpp> #include <Message.hpp>
#include <cstring> #include <cstring>
#include "Helpers/CRCHelper.hpp"
#include "MessageParser.hpp" #include "MessageParser.hpp"
#include "Services/ServiceTests.hpp" #include "Services/ServiceTests.hpp"
#include "ServicePool.hpp" #include "ServicePool.hpp"
...@@ -35,10 +36,18 @@ TEST_CASE("TC Message parsing into a string", "[MessageParser]") { ...@@ -35,10 +36,18 @@ TEST_CASE("TC Message parsing into a string", "[MessageParser]") {
message.dataSize = 5; message.dataSize = 5;
String<CCSDS_MAX_MESSAGE_SIZE> createdPacket = MessageParser::compose(message); String<CCSDS_MAX_MESSAGE_SIZE> createdPacket = MessageParser::compose(message);
#if ECSS_CRC_INCLUDE
CHECK(createdPacket.size() == 18);
CHECK(memcmp(createdPacket.data(), wantedPacket, 16) == 0);
uint16_t crcField = CRCHelper::calculateCRC(wantedPacket, 16);
uint16_t calculatedCRC = (static_cast<uint16_t>(createdPacket.data()[16]) << 0x8U) | (static_cast<uint16_t>(createdPacket.data()[17]) & 0xFF);
CHECK(calculatedCRC == crcField);
#else
CHECK(createdPacket.size() == 16); CHECK(createdPacket.size() == 16);
// The two parentheses are necessary so that Catch2 doesn't try to parse the strings here // The two parentheses are necessary so that Catch2 doesn't try to parse the strings here
CHECK((createdPacket == String<16>(wantedPacket))); CHECK((createdPacket == String<16>(wantedPacket)));
#endif
} }
TEST_CASE("TM message parsing", "[MessageParser]") { TEST_CASE("TM message parsing", "[MessageParser]") {
...@@ -67,10 +76,19 @@ TEST_CASE("TM Message parsing into a string", "[MessageParser]") { ...@@ -67,10 +76,19 @@ TEST_CASE("TM Message parsing into a string", "[MessageParser]") {
message.messageType = 17; message.messageType = 17;
memcpy(message.data, "hellohi", 7); memcpy(message.data, "hellohi", 7);
message.dataSize = 7; message.dataSize = 7;
String<CCSDS_MAX_MESSAGE_SIZE> createdPacket = MessageParser::compose(message); String<CCSDS_MAX_MESSAGE_SIZE> createdPacket = MessageParser::compose(message);
#if ECSS_CRC_INCLUDE
CHECK(createdPacket.size() == 20);
CHECK(memcmp(createdPacket.data(), wantedPacket, 18) == 0);
uint16_t crcField = CRCHelper::calculateCRC(wantedPacket, 18);
uint16_t calculatedCRC = (static_cast<uint16_t>(createdPacket.data()[18]) << 0x8U) | (static_cast<uint16_t>
(createdPacket.data()[19]) & 0xFFU);
CHECK(calculatedCRC == crcField);
#else
CHECK(createdPacket.size() == 18); CHECK(createdPacket.size() == 18);
// The two parentheses are necessary so that Catch2 doesn't try to parse the strings here // The two parentheses are necessary so that Catch2 doesn't try to parse the strings here
CHECK((createdPacket == String<18>(wantedPacket))); CHECK((createdPacket == String<18>(wantedPacket)));
#endif
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment