diff --git a/inc/Message.hpp b/inc/Message.hpp index bbc9d76ebabc4196dddd470c98e95735a1f04426..b55bf7c354519fd68561f7be7b5578e010101569 100644 --- a/inc/Message.hpp +++ b/inc/Message.hpp @@ -272,7 +272,7 @@ 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)(), + ASSERT_INTERNAL(string.size() <= (std::numeric_limits<uint16_t>::max)(), ErrorHandler::StringTooLarge); appendUint16(string.size()); @@ -466,9 +466,9 @@ public: 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()); diff --git a/inc/macros.hpp b/inc/macros.hpp index db57516f3ac7802c4200d4088163cff1b8d76dc8..f5205be237b3768b4791b0e7f47ff256b968970d 100644 --- a/inc/macros.hpp +++ b/inc/macros.hpp @@ -6,13 +6,13 @@ * * @todo Actually hold program execution or throw an exception here */ -#define assertI(cond, error) (ErrorHandler::assertInternal((cond), (error))) +#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 assertR(cond, error) (ErrorHandler::assertRequest((cond), *this, (error))) +#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 6c4f1fb613f6e0a46f767f1a436e91e67cc34f2d..72a343033a9f9278e253ed9abe8cc45e58d63e21 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 @@ -37,16 +37,16 @@ void Message::appendBits(uint8_t numBits, uint16_t data) { } 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); @@ -55,8 +55,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); @@ -67,12 +67,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); @@ -94,7 +94,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++; @@ -103,7 +103,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; @@ -112,7 +112,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]; @@ -122,8 +122,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 @@ -132,8 +132,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);