diff --git a/inc/ErrorHandler.hpp b/inc/ErrorHandler.hpp index 8c6e53917275c434e0f5d3182b2335585895b2a0..9864f708b31bc297459627581f2542bb4352fb4e 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 @@ -67,6 +66,10 @@ public: * Asked to append unnecessary spare bits */ InvalidSpareBits = 8, + /** + * A function received a Message that was not of the correct type + */ + OtherMessageType = 9, }; /** @@ -226,9 +229,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 c6ee50067e4b67cddb0e263feae6100ec62cf2ce..3240626189f8848a4147f9e97e9d46399dfaa5fe 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" @@ -63,7 +62,6 @@ public: // Next byte to read for read...() functions uint16_t readPosition = 0; - /** * Appends the least significant \p numBits from \p data to the message * @@ -287,8 +285,8 @@ public: template<const size_t SIZE> void appendOctetString(const String<SIZE> &string) { // Make sure that the string is large enough to count - assertI(string.size() <= (std::numeric_limits<uint16_t>::max)(), - ErrorHandler::StringTooLarge); + ASSERT_INTERNAL(string.size() <= (std::numeric_limits<uint16_t>::max)(), + ErrorHandler::StringTooLarge); appendUint16(string.size()); appendString(string); @@ -444,18 +442,50 @@ 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> inline void Message::appendString(const String<SIZE> &string) { - assertI(dataSize + string.size() < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); + ASSERT_INTERNAL(dataSize + string.size() < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); // TODO: Do we need to keep this check? How does etl::string handle it? - assertI(string.size() < string.capacity(), ErrorHandler::StringTooLarge); + ASSERT_INTERNAL(string.size() < string.capacity(), ErrorHandler::StringTooLarge); memcpy(data + dataSize, string.data(), string.size()); dataSize += string.size(); } - #endif //ECSS_SERVICES_PACKET_H diff --git a/inc/macros.hpp b/inc/macros.hpp index 25a0833b069d95c291b668d7bb2d7eb93d4f79b9..f5205be237b3768b4791b0e7f47ff256b968970d 100644 --- a/inc/macros.hpp +++ b/inc/macros.hpp @@ -1,7 +1,18 @@ #ifndef ECSS_SERVICES_MACROS_HPP #define ECSS_SERVICES_MACROS_HPP -#define assertI(cond, error) (ErrorHandler::assertInternal((cond), (error))) -#define assertR(cond, error) (ErrorHandler::assertRequest((cond), *this, (error))) +/** + * Perform an assertion that, if failed, throws an ErrorHandler::Internal error + * + * @todo Actually hold program execution or throw an exception here + */ +#define ASSERT_INTERNAL(cond, error) (ErrorHandler::assertInternal((cond), (error))) + +/** + * A wrapper for ErrorHandler::assertRequest() that uses `this` as the Message object. + * + * Only to be used within the Message class. + */ +#define ASSERT_REQUEST(cond, error) (ErrorHandler::assertRequest((cond), *this, (error))) #endif //ECSS_SERVICES_MACROS_HPP diff --git a/src/Helpers/TimeAndDate.cpp b/src/Helpers/TimeAndDate.cpp index d0a5fc7d937744663c501f3c54b7329cbe1ffa29..38280ecb745925f3ce989ad3547b9379bb888f67 100644 --- a/src/Helpers/TimeAndDate.cpp +++ b/src/Helpers/TimeAndDate.cpp @@ -14,12 +14,12 @@ TimeAndDate::TimeAndDate() { TimeAndDate::TimeAndDate(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) { // check if the parameters make sense - assertI(2019 <= year, ErrorHandler::InternalErrorType::InvalidDate); - assertI(1 <= month && month <= 12, ErrorHandler::InternalErrorType::InvalidDate); - assertI(1 <= day && day <= 31, ErrorHandler::InternalErrorType::InvalidDate); - assertI(0 <= hour && hour <= 24, ErrorHandler::InternalErrorType::InvalidDate); - assertI(0 <= minute && minute <= 60, ErrorHandler::InternalErrorType::InvalidDate); - assertI(0 <= second && second <= 60, ErrorHandler::InternalErrorType::InvalidDate); + ASSERT_INTERNAL(2019 <= year, ErrorHandler::InternalErrorType::InvalidDate); + ASSERT_INTERNAL(1 <= month && month <= 12, ErrorHandler::InternalErrorType::InvalidDate); + ASSERT_INTERNAL(1 <= day && day <= 31, ErrorHandler::InternalErrorType::InvalidDate); + ASSERT_INTERNAL(0 <= hour && hour <= 24, ErrorHandler::InternalErrorType::InvalidDate); + ASSERT_INTERNAL(0 <= minute && minute <= 60, ErrorHandler::InternalErrorType::InvalidDate); + ASSERT_INTERNAL(0 <= second && second <= 60, ErrorHandler::InternalErrorType::InvalidDate); this->year = year; this->month = month; diff --git a/src/Helpers/TimeHelper.cpp b/src/Helpers/TimeHelper.cpp index 93aa1cae95a557edad4a7e17dabbbecfe846cbfb..4b6885dc9b842ade6bb33fd0c5a09270b4df11e4 100644 --- a/src/Helpers/TimeHelper.cpp +++ b/src/Helpers/TimeHelper.cpp @@ -13,16 +13,16 @@ bool TimeHelper::IsLeapYear(uint16_t year) { uint32_t TimeHelper::utcToSeconds(TimeAndDate &TimeInfo) { // the date, that \p TimeInfo represents, should be greater than or equal to 1/1/2019 and the // date should be valid according to Gregorian calendar - assertI(TimeInfo.year >= 2019, ErrorHandler::InternalErrorType::InvalidDate); - assertI(1 <= TimeInfo.month && TimeInfo.month <= 12, + ASSERT_INTERNAL(TimeInfo.year >= 2019, ErrorHandler::InternalErrorType::InvalidDate); + ASSERT_INTERNAL(1 <= TimeInfo.month && TimeInfo.month <= 12, ErrorHandler::InternalErrorType::InvalidDate); - assertI(1 <= TimeInfo.day && TimeInfo.day <= 31, + ASSERT_INTERNAL(1 <= TimeInfo.day && TimeInfo.day <= 31, ErrorHandler::InternalErrorType::InvalidDate); - assertI(0 <= TimeInfo.hour && TimeInfo.hour <= 24, + ASSERT_INTERNAL(0 <= TimeInfo.hour && TimeInfo.hour <= 24, ErrorHandler::InternalErrorType::InvalidDate); - assertI(0 <= TimeInfo.minute && TimeInfo.minute <= 60, + ASSERT_INTERNAL(0 <= TimeInfo.minute && TimeInfo.minute <= 60, ErrorHandler::InternalErrorType::InvalidDate); - assertI(0 <= TimeInfo.second && TimeInfo.second <= 60, + ASSERT_INTERNAL(0 <= TimeInfo.second && TimeInfo.second <= 60, ErrorHandler::InternalErrorType::InvalidDate); uint32_t secs = 1546300800; // elapsed seconds from Unix epoch until 1/1/2019 00:00:00 (UTC) @@ -44,7 +44,7 @@ uint32_t TimeHelper::utcToSeconds(TimeAndDate &TimeInfo) { struct TimeAndDate TimeHelper::secondsToUTC(uint32_t seconds) { // elapsed seconds should be between dates, that are after 1/1/2019 and Unix epoch - assertI(seconds >= 1546300800, ErrorHandler::InternalErrorType::InvalidDate); + ASSERT_INTERNAL(seconds >= 1546300800, ErrorHandler::InternalErrorType::InvalidDate); seconds -= 1546300800; // elapsed seconds from Unix epoch until 1/1/2019 00:00:00 (UTC) TimeAndDate TimeInfo; diff --git a/src/Message.cpp b/src/Message.cpp index e6b6834b8932cf929d18576fcf884c26c9a452ec..409e65e4e18ae303c8cbcac02422172615943184 100644 --- a/src/Message.cpp +++ b/src/Message.cpp @@ -10,10 +10,10 @@ Message::Message(uint8_t serviceType, uint8_t messageType, Message::PacketType p void Message::appendBits(uint8_t numBits, uint16_t data) { // TODO: Add assertion that data does not contain 1s outside of numBits bits - assertI(numBits <= 16, ErrorHandler::TooManyBitsAppend); + ASSERT_INTERNAL(numBits <= 16, ErrorHandler::TooManyBitsAppend); while (numBits > 0) { // For every sequence of 8 bits... - assertI(dataSize < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); + ASSERT_INTERNAL(dataSize < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); if (currentBit + numBits >= 8) { // Will have to shift the bits and insert the next ones later @@ -46,16 +46,16 @@ void Message::finalize() { } void Message::appendByte(uint8_t value) { - assertI(dataSize < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); - assertI(currentBit == 0, ErrorHandler::ByteBetweenBits); + ASSERT_INTERNAL(dataSize < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); + ASSERT_INTERNAL(currentBit == 0, ErrorHandler::ByteBetweenBits); data[dataSize] = value; dataSize++; } void Message::appendHalfword(uint16_t value) { - assertI(dataSize + 2 <= ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); - assertI(currentBit == 0, ErrorHandler::ByteBetweenBits); + ASSERT_INTERNAL(dataSize + 2 <= ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); + ASSERT_INTERNAL(currentBit == 0, ErrorHandler::ByteBetweenBits); data[dataSize] = static_cast<uint8_t>((value >> 8) & 0xFF); data[dataSize + 1] = static_cast<uint8_t>(value & 0xFF); @@ -64,8 +64,8 @@ void Message::appendHalfword(uint16_t value) { } void Message::appendWord(uint32_t value) { - assertI(dataSize + 4 <= ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); - assertI(currentBit == 0, ErrorHandler::ByteBetweenBits); + ASSERT_INTERNAL(dataSize + 4 <= ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); + ASSERT_INTERNAL(currentBit == 0, ErrorHandler::ByteBetweenBits); data[dataSize] = static_cast<uint8_t>((value >> 24) & 0xFF); data[dataSize + 1] = static_cast<uint8_t>((value >> 16) & 0xFF); @@ -76,12 +76,12 @@ void Message::appendWord(uint32_t value) { } uint16_t Message::readBits(uint8_t numBits) { - assertR(numBits <= 16, ErrorHandler::TooManyBitsRead); + ASSERT_REQUEST(numBits <= 16, ErrorHandler::TooManyBitsRead); uint16_t value = 0x0; while (numBits > 0) { - assertR(readPosition < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooShort); + ASSERT_REQUEST(readPosition < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooShort); if (currentBit + numBits >= 8) { auto bitsToAddNow = static_cast<uint8_t>(8 - currentBit); @@ -103,7 +103,7 @@ uint16_t Message::readBits(uint8_t numBits) { } uint8_t Message::readByte() { - assertR(readPosition < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooShort); + ASSERT_REQUEST(readPosition < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooShort); uint8_t value = data[readPosition]; readPosition++; @@ -112,7 +112,7 @@ uint8_t Message::readByte() { } uint16_t Message::readHalfword() { - assertR(readPosition + 2 <= ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooShort); + ASSERT_REQUEST(readPosition + 2 <= ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooShort); uint16_t value = (data[readPosition] << 8) | data[readPosition + 1]; readPosition += 2; @@ -121,7 +121,7 @@ uint16_t Message::readHalfword() { } uint32_t Message::readWord() { - assertR(readPosition + 4 <= ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooShort); + ASSERT_REQUEST(readPosition + 4 <= ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooShort); uint32_t value = (data[readPosition] << 24) | (data[readPosition + 1] << 16) | (data[readPosition + 2] << 8) | data[readPosition + 3]; @@ -131,8 +131,8 @@ uint32_t Message::readWord() { } void Message::readString(char *string, uint8_t size) { - assertR(readPosition + size <= ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooShort); - assertR(size < ECSS_MAX_STRING_SIZE, ErrorHandler::StringTooShort); + ASSERT_REQUEST(readPosition + size <= ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooShort); + ASSERT_REQUEST(size < ECSS_MAX_STRING_SIZE, ErrorHandler::StringTooShort); memcpy(string, data + readPosition, size); string[size] = '\0'; // todo: Use that for now to avoid problems. Later to be removed @@ -141,8 +141,8 @@ void Message::readString(char *string, uint8_t size) { } void Message::readString(uint8_t *string, uint16_t size) { - assertR(readPosition + size <= ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooShort); - assertR(size < ECSS_MAX_STRING_SIZE, ErrorHandler::StringTooShort); + ASSERT_REQUEST(readPosition + size <= ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooShort); + ASSERT_REQUEST(size < ECSS_MAX_STRING_SIZE, ErrorHandler::StringTooShort); memcpy(string, data + readPosition, size); diff --git a/src/MessageParser.cpp b/src/MessageParser.cpp index 8e18900b8818b9eb0e04669534c898c423786026..af4988a1a302b2da35eed9d2cbe45e6b1508b706 100644 --- a/src/MessageParser.cpp +++ b/src/MessageParser.cpp @@ -23,7 +23,7 @@ void MessageParser::execute(Message &message) { } Message MessageParser::parse(uint8_t *data, uint32_t length) { - assertI(length >= 6, ErrorHandler::UnacceptablePacket); + ASSERT_INTERNAL(length >= 6, ErrorHandler::UnacceptablePacket); uint16_t packetHeaderIdentification = (data[0] << 8) | data[1]; uint16_t packetSequenceControl = (data[2] << 8) | data[3]; @@ -37,10 +37,10 @@ Message MessageParser::parse(uint8_t *data, uint32_t length) { auto sequenceFlags = static_cast<uint8_t>(packetSequenceControl >> 14); // Returning an internal error, since the Message is not available yet - assertI(versionNumber == 0, ErrorHandler::UnacceptablePacket); - assertI(secondaryHeaderFlag == 1, ErrorHandler::UnacceptablePacket); - assertI(sequenceFlags == 0x3, ErrorHandler::UnacceptablePacket); - assertI(packetDataLength == length - 6, ErrorHandler::UnacceptablePacket); + ASSERT_INTERNAL(versionNumber == 0, ErrorHandler::UnacceptablePacket); + ASSERT_INTERNAL(secondaryHeaderFlag == 1, ErrorHandler::UnacceptablePacket); + ASSERT_INTERNAL(sequenceFlags == 0x3, ErrorHandler::UnacceptablePacket); + ASSERT_INTERNAL(packetDataLength == length - 6, ErrorHandler::UnacceptablePacket); Message message(0, 0, packetType, APID); diff --git a/src/Services/EventActionService.cpp b/src/Services/EventActionService.cpp index e33ff714f803a6e150980559ae2f6b7130ee2512..2ace42dd9796cfff95120b189ed95e35e44c9722 100644 --- a/src/Services/EventActionService.cpp +++ b/src/Services/EventActionService.cpp @@ -8,145 +8,136 @@ */ void EventActionService::addEventActionDefinitions(Message message) { // TC[19,1] + message.assertTC(19, 1); - if (message.messageType == 1 && message.packetType == Message::TC && message.serviceType == - 19) { - uint16_t index; - uint16_t applicationID = message.readEnum16(); - uint16_t eventDefinitionID = message.readEnum16(); - bool accepted = true; + uint16_t index; + uint16_t applicationID = message.readEnum16(); + uint16_t eventDefinitionID = message.readEnum16(); + bool accepted = true; + for (index = 0; index < ECSS_EVENT_ACTION_STRUCT_ARRAY_SIZE; index++) { + if (eventActionDefinitionArray[index].applicationId == applicationID && + eventActionDefinitionArray[index].eventDefinitionID == eventDefinitionID && + eventActionDefinitionArray[index].enabled) { + // @todo: throw a failed start of execution error + accepted = false; + } + } + if (accepted){ for (index = 0; index < ECSS_EVENT_ACTION_STRUCT_ARRAY_SIZE; index++) { - if (eventActionDefinitionArray[index].applicationId == applicationID && - eventActionDefinitionArray[index].eventDefinitionID == eventDefinitionID && - eventActionDefinitionArray[index].enabled) { - // @todo: throw a failed start of execution error - accepted = false; + // @todo: throw an error if it's full + if (eventActionDefinitionArray[index].empty) { + break; } } - if (accepted){ - for (index = 0; index < ECSS_EVENT_ACTION_STRUCT_ARRAY_SIZE; index++) { - // @todo: throw an error if it's full - if (eventActionDefinitionArray[index].empty) { - break; - } - } - if (index < ECSS_EVENT_ACTION_STRUCT_ARRAY_SIZE) { - eventActionDefinitionArray[index].empty = false; - eventActionDefinitionArray[index].enabled = true; - eventActionDefinitionArray[index].applicationId = applicationID; - eventActionDefinitionArray[index].eventDefinitionID = eventDefinitionID; - if (message.dataSize - 4 > ECSS_EVENT_SERVICE_STRING_SIZE) { - ErrorHandler::reportInternalError(ErrorHandler::InternalErrorType::MessageTooLarge); - } else { - char data[ECSS_EVENT_SERVICE_STRING_SIZE]; - message.readString(data, message.dataSize - 4); - eventActionDefinitionArray[index].request = String<ECSS_EVENT_SERVICE_STRING_SIZE>( - data); - } + if (index < ECSS_EVENT_ACTION_STRUCT_ARRAY_SIZE) { + eventActionDefinitionArray[index].empty = false; + eventActionDefinitionArray[index].enabled = true; + eventActionDefinitionArray[index].applicationId = applicationID; + eventActionDefinitionArray[index].eventDefinitionID = eventDefinitionID; + if (message.dataSize - 4 > ECSS_EVENT_SERVICE_STRING_SIZE) { + ErrorHandler::reportInternalError(ErrorHandler::InternalErrorType::MessageTooLarge); + } else { + char data[ECSS_EVENT_SERVICE_STRING_SIZE]; + message.readString(data, message.dataSize - 4); + eventActionDefinitionArray[index].request = String<ECSS_EVENT_SERVICE_STRING_SIZE>( + data); } } } } void EventActionService::deleteEventActionDefinitions(Message message) { - // TC[19,2] - if (message.messageType == 2 && message.packetType == Message::TC && message.serviceType - == 19) { - uint16_t numberOfEventActionDefinitions = message.readUint16(); - for (uint16_t i = 0; i < numberOfEventActionDefinitions; i++) { - uint16_t applicationID = message.readEnum16(); - uint16_t eventDefinitionID = message.readEnum16(); - for (uint16_t index = 0; index < ECSS_EVENT_ACTION_STRUCT_ARRAY_SIZE; index++) { - if (eventActionDefinitionArray[index].applicationId == applicationID && - eventActionDefinitionArray[index].eventDefinitionID == eventDefinitionID && - eventActionDefinitionArray[index].enabled) { - eventActionDefinitionArray[index].empty = true; - eventActionDefinitionArray[index].eventDefinitionID = 65535; - eventActionDefinitionArray[index].request = ""; - eventActionDefinitionArray[index].applicationId = 0; - eventActionDefinitionArray[index].enabled = false; - } - } - } + message.assertTC(19, 2); - } -} - -void EventActionService::deleteAllEventActionDefinitions(Message message) { - // TC[19,3] - if (message.messageType == 3 && message.packetType == Message::TC && message.serviceType - == 19) { - setEventActionFunctionStatus(false); + uint16_t numberOfEventActionDefinitions = message.readUint16(); + for (uint16_t i = 0; i < numberOfEventActionDefinitions; i++) { + uint16_t applicationID = message.readEnum16(); + uint16_t eventDefinitionID = message.readEnum16(); for (uint16_t index = 0; index < ECSS_EVENT_ACTION_STRUCT_ARRAY_SIZE; index++) { - if (not eventActionDefinitionArray[index].empty) { + if (eventActionDefinitionArray[index].applicationId == applicationID && + eventActionDefinitionArray[index].eventDefinitionID == eventDefinitionID && + eventActionDefinitionArray[index].enabled) { eventActionDefinitionArray[index].empty = true; - eventActionDefinitionArray[index].enabled = false; eventActionDefinitionArray[index].eventDefinitionID = 65535; eventActionDefinitionArray[index].request = ""; eventActionDefinitionArray[index].applicationId = 0; + eventActionDefinitionArray[index].enabled = false; } } } } +void EventActionService::deleteAllEventActionDefinitions(Message message) { + // TC[19,3] + message.assertTC(19, 3); + + setEventActionFunctionStatus(false); + for (uint16_t index = 0; index < ECSS_EVENT_ACTION_STRUCT_ARRAY_SIZE; index++) { + if (not eventActionDefinitionArray[index].empty) { + eventActionDefinitionArray[index].empty = true; + eventActionDefinitionArray[index].enabled = false; + eventActionDefinitionArray[index].eventDefinitionID = 65535; + eventActionDefinitionArray[index].request = ""; + eventActionDefinitionArray[index].applicationId = 0; + } + } +} + void EventActionService::enableEventActionDefinitions(Message message) { // TC[19,4] - if (message.messageType == 4 && message.packetType == Message::TC && message.serviceType - == 19) { - uint16_t numberOfEventActionDefinitions = message.readUint16(); - if (numberOfEventActionDefinitions != 0){ - for (uint16_t i = 0; i < numberOfEventActionDefinitions; i++) { - uint16_t applicationID = message.readEnum16(); - uint16_t eventDefinitionID = message.readEnum16(); - for (uint16_t index = 0; index < ECSS_EVENT_ACTION_STRUCT_ARRAY_SIZE; index++) { - if (eventActionDefinitionArray[index].applicationId == applicationID && - eventActionDefinitionArray[index].eventDefinitionID == eventDefinitionID) { - eventActionDefinitionArray[index].enabled = true; - } - } - } - } else { + message.assertTC(19, 4); + + uint16_t numberOfEventActionDefinitions = message.readUint16(); + if (numberOfEventActionDefinitions != 0){ + for (uint16_t i = 0; i < numberOfEventActionDefinitions; i++) { + uint16_t applicationID = message.readEnum16(); + uint16_t eventDefinitionID = message.readEnum16(); for (uint16_t index = 0; index < ECSS_EVENT_ACTION_STRUCT_ARRAY_SIZE; index++) { - if (not eventActionDefinitionArray[index].empty){ + if (eventActionDefinitionArray[index].applicationId == applicationID && + eventActionDefinitionArray[index].eventDefinitionID == eventDefinitionID) { eventActionDefinitionArray[index].enabled = true; } } } + } else { + for (uint16_t index = 0; index < ECSS_EVENT_ACTION_STRUCT_ARRAY_SIZE; index++) { + if (not eventActionDefinitionArray[index].empty){ + eventActionDefinitionArray[index].enabled = true; + } + } } } void EventActionService::disableEventActionDefinitions(Message message) { // TC[19,5] - if (message.messageType == 5 && message.packetType == Message::TC && message.serviceType - == 19) { - uint16_t numberOfEventActionDefinitions = message.readUint16(); - if (numberOfEventActionDefinitions != 0){ - for (uint16_t i = 0; i < numberOfEventActionDefinitions; i++) { - uint16_t applicationID = message.readEnum16(); - uint16_t eventDefinitionID = message.readEnum16(); - for (uint16_t index = 0; index < ECSS_EVENT_ACTION_STRUCT_ARRAY_SIZE; index++) { - if (eventActionDefinitionArray[index].applicationId == applicationID && - eventActionDefinitionArray[index].eventDefinitionID == eventDefinitionID) { - eventActionDefinitionArray[index].enabled = false; - } - } - } - } else { + message.assertTC(19, 5); + + uint16_t numberOfEventActionDefinitions = message.readUint16(); + if (numberOfEventActionDefinitions != 0){ + for (uint16_t i = 0; i < numberOfEventActionDefinitions; i++) { + uint16_t applicationID = message.readEnum16(); + uint16_t eventDefinitionID = message.readEnum16(); for (uint16_t index = 0; index < ECSS_EVENT_ACTION_STRUCT_ARRAY_SIZE; index++) { - if (not eventActionDefinitionArray[index].empty){ + if (eventActionDefinitionArray[index].applicationId == applicationID && + eventActionDefinitionArray[index].eventDefinitionID == eventDefinitionID) { eventActionDefinitionArray[index].enabled = false; } } } + } else { + for (uint16_t index = 0; index < ECSS_EVENT_ACTION_STRUCT_ARRAY_SIZE; index++) { + if (not eventActionDefinitionArray[index].empty){ + eventActionDefinitionArray[index].enabled = false; + } + } } } void EventActionService::requestEventActionDefinitionStatus(Message message) { // TC[19,6] - if (message.messageType == 6 && message.packetType == Message::TC && message.serviceType - == 19) { - eventActionStatusReport(); - } + message.assertTC(19, 6); + + eventActionStatusReport(); } void EventActionService::eventActionStatusReport() { @@ -171,18 +162,16 @@ void EventActionService::eventActionStatusReport() { void EventActionService::enableEventActionFunction(Message message) { // TC[19,8] - if (message.messageType == 8 && message.packetType == Message::TC && message.serviceType - == 19) { - setEventActionFunctionStatus(true); - } + message.assertTC(19, 8); + + setEventActionFunctionStatus(true); } void EventActionService::disableEventActionFunction(Message message) { // TC[19,9] - if (message.messageType == 9 && message.packetType == Message::TC && message.serviceType - == 19) { - setEventActionFunctionStatus(false); - } + message.assertTC(19, 9); + + setEventActionFunctionStatus(false); } // TODO: Should I use applicationID too? diff --git a/src/Services/EventReportService.cpp b/src/Services/EventReportService.cpp index b86a017c6c808b9121b9c85159c25904057af655..f537f32988fe319ffb79f46f5985656f09868241 100644 --- a/src/Services/EventReportService.cpp +++ b/src/Services/EventReportService.cpp @@ -71,51 +71,49 @@ EventReportService::highSeverityAnomalyReport(Event eventID, const String<64> & void EventReportService::enableReportGeneration(Message message) { // TC[5,5] - if (message.serviceType == 5 && message.packetType == Message::TC && message.messageType == 5) { - /** - * @todo: Report an error if length > numberOfEvents - */ - uint16_t length = message.readUint16(); - Event eventID[length]; + message.assertTC(5, 5); + + /** + * @todo: Report an error if length > numberOfEvents + */ + uint16_t length = message.readUint16(); + Event eventID[length]; + for (uint16_t i = 0; i < length; i++) { + eventID[i] = static_cast<Event >(message.readEnum16()); + } + if (length <= numberOfEvents) { for (uint16_t i = 0; i < length; i++) { - eventID[i] = static_cast<Event >(message.readEnum16()); + stateOfEvents[static_cast<uint16_t> (eventID[i])] = true; } - if (length <= numberOfEvents) { - for (uint16_t i = 0; i < length; i++) { - stateOfEvents[static_cast<uint16_t> (eventID[i])] = true; - } - } - disabledEventsCount = stateOfEvents.size() - stateOfEvents.count(); } + disabledEventsCount = stateOfEvents.size() - stateOfEvents.count(); } void EventReportService::disableReportGeneration(Message message) { // TC[5,6] - if (message.serviceType == 5 && message.packetType == Message::TC && message.messageType - == 6) { - /** - * @todo: Report an error if length > numberOfEvents - */ - uint16_t length = message.readUint16(); - Event eventID[length]; + message.assertTC(5, 6); + + /** + * @todo: Report an error if length > numberOfEvents + */ + uint16_t length = message.readUint16(); + Event eventID[length]; + for (uint16_t i = 0; i < length; i++) { + eventID[i] = static_cast<Event >(message.readEnum16()); + } + if (length <= numberOfEvents) { for (uint16_t i = 0; i < length; i++) { - eventID[i] = static_cast<Event >(message.readEnum16()); - } - if (length <= numberOfEvents) { - for (uint16_t i = 0; i < length; i++) { - stateOfEvents[static_cast<uint16_t> (eventID[i])] = false; - } + stateOfEvents[static_cast<uint16_t> (eventID[i])] = false; } - disabledEventsCount = stateOfEvents.size() - stateOfEvents.count(); } + disabledEventsCount = stateOfEvents.size() - stateOfEvents.count(); } void EventReportService::requestListOfDisabledEvents(Message message) { // TC[5,7] - // I think this is all that is needed here. - if (message.serviceType == 5 && message.packetType == Message::TC && message.messageType == 7) { - listOfDisabledEventsReport(); - } + message.assertTC(5, 7); + + listOfDisabledEventsReport(); } void EventReportService::listOfDisabledEventsReport() { diff --git a/src/Services/MemoryManagementService.cpp b/src/Services/MemoryManagementService.cpp index ebb58a115bf2f7a3779708d47f9037ac16f42871..c29c8fc1fc6a60efda6386a10d20c733d7ad78b2 100644 --- a/src/Services/MemoryManagementService.cpp +++ b/src/Services/MemoryManagementService.cpp @@ -23,8 +23,7 @@ void MemoryManagementService::RawDataMemoryManagement::loadRawData(Message &requ * @todo Add failure reporting */ // Check if we have the correct packet - assert(request.serviceType == 6); - assert(request.messageType == 2); + request.assertTC(6, 2); // Read the memory ID from the request auto memoryID = MemoryManagementService::MemoryID(request.readEnum8()); @@ -74,8 +73,7 @@ void MemoryManagementService::RawDataMemoryManagement::loadRawData(Message &requ void MemoryManagementService::RawDataMemoryManagement::dumpRawData(Message &request) { // Check if we have the correct packet - assert(request.serviceType == 6); - assert(request.messageType == 5); + request.assertTC(6, 5); // Create the report message object of telemetry message subtype 6 Message report = mainService.createTM(6); @@ -124,8 +122,7 @@ void MemoryManagementService::RawDataMemoryManagement::dumpRawData(Message &requ void MemoryManagementService::RawDataMemoryManagement::checkRawData(Message &request) { // Check if we have the correct packet - assert(request.serviceType == 6); - assert(request.messageType == 9); + request.assertTC(6, 9); // Create the report message object of telemetry message subtype 10 Message report = mainService.createTM(10); diff --git a/src/Services/ParameterService.cpp b/src/Services/ParameterService.cpp index dd0f4c7495d4b925feb1355fe8808415043450c2..b32fa5b589adf76d9910c7fd3ca1c3aa33803f38 100644 --- a/src/Services/ParameterService.cpp +++ b/src/Services/ParameterService.cpp @@ -35,6 +35,7 @@ ParameterService::ParameterService() { } void ParameterService::reportParameterIds(Message& paramIds) { + paramIds.assertTC(20, 1); Message reqParam(20, 2, Message::TM, 1); // empty TM[20, 2] parameter report message paramIds.resetRead(); // since we're passing a reference, the reading position shall be reset @@ -69,6 +70,7 @@ void ParameterService::reportParameterIds(Message& paramIds) { } void ParameterService::setParameterIds(Message& newParamValues) { + newParamValues.assertTC(20, 3); // assertion: correct message, packet and service type (at failure throws an // InternalError::UnacceptablePacket which gets logged) diff --git a/src/Services/RequestVerificationService.cpp b/src/Services/RequestVerificationService.cpp index 768971daf89b7388075adad9f3e6a471b0795e0c..465b1c5635ed2ac2164433cf282defa377e7b838 100644 --- a/src/Services/RequestVerificationService.cpp +++ b/src/Services/RequestVerificationService.cpp @@ -152,7 +152,8 @@ RequestVerificationService::failRoutingVerification(const Message &request, storeMessage(report); } - +// TODO: This function should not be here. These are TM messages, but `execute` should accept a +// TC message. void RequestVerificationService::execute(const Message &message) { switch (message.messageType) { case 1: diff --git a/src/Services/TestService.cpp b/src/Services/TestService.cpp index 2584c75cb529826e3653a56cda8eb33d114933be..224c2614d6a29742bb57162e1d96669bdb3ffa21 100644 --- a/src/Services/TestService.cpp +++ b/src/Services/TestService.cpp @@ -1,6 +1,7 @@ #include "Services/TestService.hpp" void TestService::areYouAlive(Message &request) { + request.assertTC(17, 1); // TM[17,2] are-you-alive connection test report Message report = createTM(2); @@ -8,6 +9,7 @@ void TestService::areYouAlive(Message &request) { } void TestService::onBoardConnection(Message &request) { + request.assertTC(17, 3); // TM[17,4] on-board connection test report Message report = createTM(4); diff --git a/src/Services/TimeManagementService.cpp b/src/Services/TimeManagementService.cpp index 70c89dba2ec097f2d332bcfd2371550bcab9e03c..c6f922953ba4b434dd8a2b6461f5eab05d435aea 100644 --- a/src/Services/TimeManagementService.cpp +++ b/src/Services/TimeManagementService.cpp @@ -14,7 +14,8 @@ void TimeManagementService::cdsTimeReport(TimeAndDate &TimeInfo) { } TimeAndDate TimeManagementService::cdsTimeRequest(Message &message) { - // TC{9,128] CDS time request + // TC[9,128] CDS time request + message.assertTC(9, 128); // check if we have the correct size of the data. The size should be 6 (48 bits) ErrorHandler::assertRequest(message.dataSize == 6, message,