diff --git a/inc/MessageParser.hpp b/inc/MessageParser.hpp index 5c43505f4c4aa1c73009cce45c6c6023b0af20f0..c8c162f0720d1a1e1036eabbd9c038704dce0c58 100644 --- a/inc/MessageParser.hpp +++ b/inc/MessageParser.hpp @@ -64,6 +64,17 @@ private: * @param message The Message to modify based on the header */ void parseTC(uint8_t *data, uint16_t length, Message &message); + + /** + * Parse the ECSS Telemetry packet secondary header + * + * As specified in section 7.4.3.1 of the standard + * + * @param data The data of the header (not null-terminated) + * @param length The size of the header + * @param message The Message to modify based on the header + */ + void parseTM(uint8_t *data, uint16_t length, Message &message); }; diff --git a/src/MessageParser.cpp b/src/MessageParser.cpp index ff933f68824253cd227b7ba87b1ed71ed4b4f412..6472871ece12c8b267aa1b421ea2a3ecb631697a 100644 --- a/src/MessageParser.cpp +++ b/src/MessageParser.cpp @@ -48,7 +48,7 @@ Message MessageParser::parse(uint8_t *data, uint32_t length) { if (packetType == Message::TC) { parseTC(data + 6, packetDataLength, message); } else { - assert(false); // Not implemented yet + parseTM(data + 6, packetDataLength, message); } return message; @@ -82,3 +82,24 @@ Message MessageParser::parseRequestTC(String<ECSS_EVENT_SERVICE_STRING_SIZE> dat parseTC(dataInt, ECSS_EVENT_SERVICE_STRING_SIZE, message); return message; } + +void MessageParser::parseTM(uint8_t *data, uint16_t length, Message &message) { + ErrorHandler::assertRequest(length >= 5, message, ErrorHandler::UnacceptableMessage); + + // Individual fields of the TM header + uint8_t pusVersion = data[0] >> 4; + uint8_t serviceType = data[1]; + uint8_t messageType = data[2]; + + ErrorHandler::assertRequest(pusVersion == 2, message, ErrorHandler::UnacceptableMessage); + + // Remove the length of the header + length -= 5; + + // Copy the data to the message + // TODO: See if memcpy is needed for this + message.serviceType = serviceType; + message.messageType = messageType; + memcpy(message.data, data + 5, length); + message.dataSize = length; +} diff --git a/test/MessageParser.cpp b/test/MessageParser.cpp index ace886d2b54e89ec08e4cb7c83d4c4a69ec3b52a..ccf84c7a3b9ca4ffe2731bc90998827cbd36cbbc 100644 --- a/test/MessageParser.cpp +++ b/test/MessageParser.cpp @@ -110,3 +110,17 @@ TEST_CASE("TC message parsing", "[MessageParser]") { TEST_CASE("TC data parsing into a message", "[MessageParser]") { } + +TEST_CASE("TM message parsing", "[MessageParser]") { + MessageParser messageParser; + uint8_t packet[] = {0x08, 0x02, 0xc0, 0x4d, 0x00, 0x0c, 0x20, 0x16, 0x11, 0x00, 0x00, 0x68, + 0x65, 0x6c, 0x6c, 0x6f, + 0x68, 0x69}; + Message message = messageParser.parse(packet, 18); + CHECK(message.packetType == Message::TM); + CHECK(message.applicationId == 2); + CHECK(message.dataSize == 7); + CHECK(message.serviceType == 22); + CHECK(message.messageType == 17); + CHECK(memcmp(message.data, "hellohi", 7) == 0); +}