From d6612d8b59fd7f2d0cd3488eeb02b5ee2b6ad4ef Mon Sep 17 00:00:00 2001
From: kongr45gpen <electrovesta@gmail.com>
Date: Sat, 23 Mar 2019 20:28:16 +0200
Subject: [PATCH] Add assertions for expected message types

---
 inc/ErrorHandler.hpp |  9 +++++----
 inc/Message.hpp      | 35 +++++++++++++++++++++++++++++++++--
 2 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/inc/ErrorHandler.hpp b/inc/ErrorHandler.hpp
index 07c29093..5b62032a 100644
--- a/inc/ErrorHandler.hpp
+++ b/inc/ErrorHandler.hpp
@@ -52,7 +52,6 @@ public:
 		 * An error in the header of a packet makes it unable to be parsed
 		 */
 			UnacceptablePacket = 5,
-
 		/**
  		 * A date that isn't valid according to the Gregorian calendar or cannot be parsed by the
  		 * TimeHelper
@@ -62,6 +61,10 @@ public:
 		 * Asked a Message type that doesn't exist
 		 */
 			UnknownMessageType = 7,
+		/**
+		 * A function received a Message that was not of the correct type
+		 */
+		    OtherMessageType = 8,
 	};
 
 	/**
@@ -222,9 +225,7 @@ public:
 	 */
 	template<typename ErrorType>
 	inline static ErrorSource findErrorSource(ErrorType error) {
-		// While this may seem like a "hacky" way to convert enums to ErrorSource, it should be
-		// optimised by the compiler to constant time.
-
+		// Static type checking
 		if (std::is_same<ErrorType, AcceptanceErrorType>()) {
 			return Acceptance;
 		}
diff --git a/inc/Message.hpp b/inc/Message.hpp
index fc26623c..bbc9d76e 100644
--- a/inc/Message.hpp
+++ b/inc/Message.hpp
@@ -7,7 +7,6 @@ class Message;
 
 #include "ECSS_Definitions.hpp"
 #include <cstdint>
-#include <cassert>
 #include <etl/String.hpp>
 #include <etl/wstring.h>
 #include "ErrorHandler.hpp"
@@ -430,6 +429,39 @@ public:
 	 * Reset the message reading status, and start reading data from it again
 	 */
 	void resetRead();
+
+	/**
+	 * Compare the message type to an expected one. An unexpected message type will throw an
+	 * OtherMessageType error.
+	 *
+	 * @return True if the message is of correct type, false if not
+	 */
+	bool assertType(Message::PacketType expectedPacketType, uint8_t expectedServiceType,
+		uint8_t expectedMessageType) {
+		if (packetType != expectedPacketType || serviceType != expectedServiceType ||
+		    messageType != expectedMessageType) {
+			ErrorHandler::reportInternalError(ErrorHandler::OtherMessageType);
+			return false;
+		}
+
+		return true;
+	}
+
+	/**
+	 * Alias for Message::assertType(Message::TC, \p expectedServiceType, \p
+	 * expectedMessageType)
+	 */
+	bool assertTC(uint8_t expectedServiceType, uint8_t expectedMessageType) {
+		return assertType(TC, expectedServiceType, expectedMessageType);
+	}
+
+	/**
+	 * Alias for Message::assertType(Message::TM, \p expectedServiceType, \p
+	 * expectedMessageType)
+	 */
+	bool assertTM(uint8_t expectedServiceType, uint8_t expectedMessageType) {
+		return assertType(TM, expectedServiceType, expectedMessageType);
+	}
 };
 
 template<const size_t SIZE>
@@ -443,5 +475,4 @@ inline void Message::appendString(const String<SIZE> & string) {
 	dataSize += string.size();
 }
 
-
 #endif //ECSS_SERVICES_PACKET_H
-- 
GitLab