From 85c04dee2340da305684a347c01c391ef41dfcc7 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 27 Sep 2021 11:12:38 +0200 Subject: [PATCH 1/8] increase limit of packets stored --- src/fsfw/tmtcservices/TmTcBridge.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fsfw/tmtcservices/TmTcBridge.h b/src/fsfw/tmtcservices/TmTcBridge.h index d3689d19f..4980caff9 100644 --- a/src/fsfw/tmtcservices/TmTcBridge.h +++ b/src/fsfw/tmtcservices/TmTcBridge.h @@ -19,7 +19,7 @@ class TmTcBridge : public AcceptsTelemetryIF, public: static constexpr uint8_t TMTC_RECEPTION_QUEUE_DEPTH = 20; static constexpr uint8_t LIMIT_STORED_DATA_SENT_PER_CYCLE = 15; - static constexpr uint8_t LIMIT_DOWNLINK_PACKETS_STORED = 20; + static constexpr uint8_t LIMIT_DOWNLINK_PACKETS_STORED = 200; static constexpr uint8_t DEFAULT_STORED_DATA_SENT_PER_CYCLE = 5; static constexpr uint8_t DEFAULT_DOWNLINK_PACKETS_STORED = 10; From 8ec35f158c0d445c72f4a5761dd1660c5725e7e0 Mon Sep 17 00:00:00 2001 From: Steffen Gaisser Date: Mon, 27 Sep 2021 19:57:42 +0200 Subject: [PATCH 2/8] Removed Timer and updated Countdown --- osal/linux/Timer.cpp | 45 ------------------------- osal/linux/Timer.h | 45 ------------------------- timemanager/Countdown.cpp | 36 ++++++++++++++------ timemanager/Countdown.h | 71 +++++++++++++++++++++++++++++++++------ 4 files changed, 86 insertions(+), 111 deletions(-) delete mode 100644 osal/linux/Timer.cpp delete mode 100644 osal/linux/Timer.h diff --git a/osal/linux/Timer.cpp b/osal/linux/Timer.cpp deleted file mode 100644 index fe0fbebba..000000000 --- a/osal/linux/Timer.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "Timer.h" -#include "../../serviceinterface/ServiceInterfaceStream.h" -#include - - -Timer::Timer() { - sigevent sigEvent; - sigEvent.sigev_notify = SIGEV_NONE; - sigEvent.sigev_signo = 0; - sigEvent.sigev_value.sival_ptr = &timerId; - int status = timer_create(CLOCK_MONOTONIC, &sigEvent, &timerId); - if(status!=0){ -#if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::error << "Timer creation failed with: " << status << - " errno: " << errno << std::endl; -#endif - } -} - -Timer::~Timer() { - timer_delete(timerId); -} - -int Timer::setTimer(uint32_t intervalMs) { - itimerspec timer; - timer.it_value.tv_sec = intervalMs / 1000; - timer.it_value.tv_nsec = (intervalMs * 1000000) % (1000000000); - timer.it_interval.tv_sec = 0; - timer.it_interval.tv_nsec = 0; - return timer_settime(timerId, 0, &timer, NULL); -} - - -int Timer::getTimer(uint32_t* remainingTimeMs){ - itimerspec timer; - timer.it_value.tv_sec = 0; - timer.it_value.tv_nsec = 0; - timer.it_interval.tv_sec = 0; - timer.it_interval.tv_nsec = 0; - int status = timer_gettime(timerId, &timer); - - *remainingTimeMs = timer.it_value.tv_sec * 1000 + timer.it_value.tv_nsec / 1000000; - - return status; -} diff --git a/osal/linux/Timer.h b/osal/linux/Timer.h deleted file mode 100644 index f94bca59b..000000000 --- a/osal/linux/Timer.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef FRAMEWORK_OSAL_LINUX_TIMER_H_ -#define FRAMEWORK_OSAL_LINUX_TIMER_H_ - -#include -#include -#include - -/** - * This class is a helper for the creation of a Clock Monotonic timer which does not trigger a signal - */ -class Timer { -public: - /** - * Creates the Timer sets the timerId Member - */ - Timer(); - /** - * Deletes the timer - * - * Careful! According to POSIX documentation: - * The treatment of any pending signal generated by the deleted timer is unspecified. - */ - virtual ~Timer(); - - /** - * Set the timer given in timerId to the given interval - * - * @param intervalMs Interval in ms to be set - * @return 0 on Success 1 else - */ - int setTimer(uint32_t intervalMs); - - /** - * Get the remaining time of the timer - * - * @param remainingTimeMs Pointer to integer value which is used to return the remaining time - * @return 0 on Success 1 else (see timer_getime documentation of posix function) - */ - int getTimer(uint32_t* remainingTimeMs); - -private: - timer_t timerId; -}; - -#endif /* FRAMEWORK_OSAL_LINUX_TIMER_H_ */ diff --git a/timemanager/Countdown.cpp b/timemanager/Countdown.cpp index 20b56189f..7aa40e3e9 100644 --- a/timemanager/Countdown.cpp +++ b/timemanager/Countdown.cpp @@ -1,4 +1,6 @@ #include "Countdown.h" +#include "fsfw/serviceinterface/ServiceInterfaceStream.h" + Countdown::Countdown(uint32_t initialTimeout): timeout(initialTimeout) { } @@ -6,16 +8,14 @@ Countdown::Countdown(uint32_t initialTimeout): timeout(initialTimeout) { Countdown::~Countdown() { } -ReturnValue_t Countdown::setTimeout(uint32_t miliseconds) { - ReturnValue_t return_value = Clock::getUptime( &startTime ); - timeout = miliseconds; - return return_value; +ReturnValue_t Countdown::setTimeout(uint32_t milliseconds) { + ReturnValue_t returnValue = Clock::getUptime( &startTime ); + timeout = milliseconds; + return returnValue; } bool Countdown::hasTimedOut() const { - uint32_t current_time; - Clock::getUptime( ¤t_time ); - if ( uint32_t(current_time - startTime) >= timeout) { + if ( uint32_t( this->getCurrentTime() - startTime) >= timeout) { return true; } else { return false; @@ -31,7 +31,23 @@ ReturnValue_t Countdown::resetTimer() { } void Countdown::timeOut() { - uint32_t current_time; - Clock::getUptime( ¤t_time ); - startTime= current_time - timeout; + startTime = this->getCurrentTime() - timeout; +} + +uint32_t Countdown::getRemainingMillis() const { + // We fetch the time before the if-statement + // to be sure that the return is in + // range 0 <= number <= timeout + uint32_t currentTime = this->getCurrentTime(); + if (this->hasTimedOut()){ + return 0; + }else{ + return (startTime + timeout) - currentTime; + } +} + +uint32_t Countdown::getCurrentTime() const { + uint32_t current_time; + Clock::getUptime( ¤t_time ); + return current_time; } diff --git a/timemanager/Countdown.h b/timemanager/Countdown.h index f6a41e73d..c0afdf755 100644 --- a/timemanager/Countdown.h +++ b/timemanager/Countdown.h @@ -4,28 +4,77 @@ #include "Clock.h" /** - * @brief This file defines the Countdown class. - * @author baetz + * + * Countdown keeps track of a timespan. + * + * Countdown::resetTimer restarts the timer. + * Countdown::setTimeout sets a new countdown duration and resets. + * + * Can be checked with Countdown::hasTimedOut or + * Countdown::isBusy. + * + * Countdown::timeOut will force the timer to time out. + * */ class Countdown { public: - uint32_t timeout; + /** + * Constructor which sets the countdown duration in milliseconds + * + * It does not start the countdown! + * Call resetTimer or setTimeout before usage! + * Otherwise a call to hasTimedOut might return True. + * + * @param initialTimeout Countdown duration in milliseconds + */ Countdown(uint32_t initialTimeout = 0); ~Countdown(); - ReturnValue_t setTimeout(uint32_t miliseconds); - + /** + * Call to set a new countdown duration. + * + * Resets the countdown! + * + * @param milliseconds new countdown duration in milliseconds + * @return Returnvalue from Clock::getUptime + */ + ReturnValue_t setTimeout(uint32_t milliseconds); + /** + * Returns true if the countdown duration has passed. + * + * @return True if the countdown has passed + * False if it is still running + */ bool hasTimedOut() const; - + /** + * Complementary to hasTimedOut. + * + * @return True if the countdown is till running + * False if it is still running + */ bool isBusy() const; - - //!< Use last set timeout value and restart timer. + /** + * Uses last set timeout value and restarts timer. + */ ReturnValue_t resetTimer(); - - //!< Make hasTimedOut() return true + /** + * Returns the remaining milliseconds (0 if timeout) + */ + uint32_t getRemainingMillis() const; + /** + * Makes hasTimedOut() return true + */ void timeOut(); - + /** + * Internal countdown duration in milliseconds + */ + uint32_t timeout; private: + /** + * Last time the timer was started (uptime) + */ uint32_t startTime = 0; + + uint32_t getCurrentTime() const; }; #endif /* FSFW_TIMEMANAGER_COUNTDOWN_H_ */ From 5064d449992a921936211d7e2d8dbac99e4cf6cc Mon Sep 17 00:00:00 2001 From: Steffen Gaisser Date: Mon, 27 Sep 2021 20:45:44 +0200 Subject: [PATCH 3/8] Removed Timer.cpp from CMakeLists --- osal/linux/CMakeLists.txt | 1 - timemanager/Countdown.cpp | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/osal/linux/CMakeLists.txt b/osal/linux/CMakeLists.txt index 0fb66b3ed..418007643 100644 --- a/osal/linux/CMakeLists.txt +++ b/osal/linux/CMakeLists.txt @@ -13,7 +13,6 @@ target_sources(${LIB_FSFW_NAME} QueueFactory.cpp SemaphoreFactory.cpp TaskFactory.cpp - Timer.cpp tcpipHelpers.cpp unixUtility.cpp ) diff --git a/timemanager/Countdown.cpp b/timemanager/Countdown.cpp index 7aa40e3e9..81681beb1 100644 --- a/timemanager/Countdown.cpp +++ b/timemanager/Countdown.cpp @@ -47,7 +47,7 @@ uint32_t Countdown::getRemainingMillis() const { } uint32_t Countdown::getCurrentTime() const { - uint32_t current_time; - Clock::getUptime( ¤t_time ); - return current_time; + uint32_t currentTime; + Clock::getUptime( ¤tTime ); + return currentTime; } From 4b62c8aa81abcf5490c6a3045e38d0acc9955f4b Mon Sep 17 00:00:00 2001 From: Steffen Gaisser Date: Mon, 27 Sep 2021 21:53:27 +0200 Subject: [PATCH 4/8] Added tests --- tests/src/fsfw_tests/unit/CMakeLists.txt | 1 + .../unit/timemanager/CMakeLists.txt | 3 +++ .../unit/timemanager/TestCountdown.cpp | 27 +++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 tests/src/fsfw_tests/unit/timemanager/CMakeLists.txt create mode 100644 tests/src/fsfw_tests/unit/timemanager/TestCountdown.cpp diff --git a/tests/src/fsfw_tests/unit/CMakeLists.txt b/tests/src/fsfw_tests/unit/CMakeLists.txt index 01e4d19c3..f30e4b6bf 100644 --- a/tests/src/fsfw_tests/unit/CMakeLists.txt +++ b/tests/src/fsfw_tests/unit/CMakeLists.txt @@ -18,4 +18,5 @@ add_subdirectory(serialize) add_subdirectory(datapoollocal) add_subdirectory(storagemanager) add_subdirectory(globalfunctions) +add_subdirectory(timemanager) add_subdirectory(tmtcpacket) diff --git a/tests/src/fsfw_tests/unit/timemanager/CMakeLists.txt b/tests/src/fsfw_tests/unit/timemanager/CMakeLists.txt new file mode 100644 index 000000000..2c6357114 --- /dev/null +++ b/tests/src/fsfw_tests/unit/timemanager/CMakeLists.txt @@ -0,0 +1,3 @@ +target_sources(${TARGET_NAME} PRIVATE + TestCountdown.cpp +) diff --git a/tests/src/fsfw_tests/unit/timemanager/TestCountdown.cpp b/tests/src/fsfw_tests/unit/timemanager/TestCountdown.cpp new file mode 100644 index 000000000..b1b26679e --- /dev/null +++ b/tests/src/fsfw_tests/unit/timemanager/TestCountdown.cpp @@ -0,0 +1,27 @@ +#include "fsfw_tests/unit/CatchDefinitions.h" +#include +#include + + +TEST_CASE( "Countdown Tests", "[TestCountdown]") { + INFO("Countdown Tests"); + Countdown count(20); + REQUIRE(count.timeout == 20); + REQUIRE(count.setTimeout(100) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(count.timeout == 100); + REQUIRE(count.setTimeout(150) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(count.isBusy()); + REQUIRE(not count.hasTimedOut()); + uint32_t number = count.getRemainingMillis(); + REQUIRE(number > 0); + bool blocked = false; + while(not count.hasTimedOut()){ + blocked = true; + }; + REQUIRE(blocked); + number = count.getRemainingMillis(); + REQUIRE(number==0); + count.resetTimer(); + REQUIRE(not count.hasTimedOut()); + REQUIRE(count.isBusy()); +} From b0cbd40e647d1a131e12f1e3ff7ae4ff45443d26 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 30 Sep 2021 11:25:42 +0200 Subject: [PATCH 5/8] possible bugfix for DLE encoder --- src/fsfw/globalfunctions/DleEncoder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fsfw/globalfunctions/DleEncoder.cpp b/src/fsfw/globalfunctions/DleEncoder.cpp index 47ea5c4e5..f4691cc64 100644 --- a/src/fsfw/globalfunctions/DleEncoder.cpp +++ b/src/fsfw/globalfunctions/DleEncoder.cpp @@ -165,7 +165,7 @@ ReturnValue_t DleEncoder::decodeStreamEscaped(const uint8_t *sourceStream, size_ if (sourceStream[encodedIndex++] != STX_CHAR) { return DECODING_ERROR; } - while ((encodedIndex < sourceStreamLen) + while ((encodedIndex < sourceStreamLen - 1) and (decodedIndex < maxDestStreamlen) and (sourceStream[encodedIndex] != ETX_CHAR) and (sourceStream[encodedIndex] != STX_CHAR)) { From f76f462022e45dbc13b452bb2e2c4e8cea5159f5 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 30 Sep 2021 11:27:14 +0200 Subject: [PATCH 6/8] test added --- tests/src/fsfw_tests/unit/globalfunctions/testDleEncoder.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/src/fsfw_tests/unit/globalfunctions/testDleEncoder.cpp b/tests/src/fsfw_tests/unit/globalfunctions/testDleEncoder.cpp index 8c2e55ed5..cffd53083 100644 --- a/tests/src/fsfw_tests/unit/globalfunctions/testDleEncoder.cpp +++ b/tests/src/fsfw_tests/unit/globalfunctions/testDleEncoder.cpp @@ -218,5 +218,10 @@ TEST_CASE("DleEncoder" , "[DleEncoder]") { REQUIRE(result == static_cast(DleEncoder::DECODING_ERROR)); dleEncoder.setEscapeMode(true); + testArray1EncodedFaulty = TEST_ARRAY_1_ENCODED_ESCAPED; + testArray1EncodedFaulty[5] = 0; + result = dleEncoder.decode(testArray1EncodedFaulty.data(), testArray1EncodedFaulty.size(), + &readLen, buffer.data(), buffer.size(), &encodedLen); + REQUIRE(result == static_cast(DleEncoder::DECODING_ERROR)); } } From afb472996c219c20a94a0df3754430b9df3a546a Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 30 Sep 2021 16:49:30 +0200 Subject: [PATCH 7/8] refactoring, code more understandable --- src/fsfw/globalfunctions/DleEncoder.cpp | 48 +++++++++++++------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/fsfw/globalfunctions/DleEncoder.cpp b/src/fsfw/globalfunctions/DleEncoder.cpp index f4691cc64..f77d5472d 100644 --- a/src/fsfw/globalfunctions/DleEncoder.cpp +++ b/src/fsfw/globalfunctions/DleEncoder.cpp @@ -165,11 +165,9 @@ ReturnValue_t DleEncoder::decodeStreamEscaped(const uint8_t *sourceStream, size_ if (sourceStream[encodedIndex++] != STX_CHAR) { return DECODING_ERROR; } - while ((encodedIndex < sourceStreamLen - 1) - and (decodedIndex < maxDestStreamlen) - and (sourceStream[encodedIndex] != ETX_CHAR) - and (sourceStream[encodedIndex] != STX_CHAR)) { - if (sourceStream[encodedIndex] == DLE_CHAR) { + while ((encodedIndex < sourceStreamLen) and (decodedIndex < maxDestStreamlen)) { + switch(sourceStream[encodedIndex]) { + case(DLE_CHAR): { if(encodedIndex + 1 >= sourceStreamLen) { //reached the end of the sourceStream *readLen = sourceStreamLen; @@ -197,29 +195,33 @@ ReturnValue_t DleEncoder::decodeStreamEscaped(const uint8_t *sourceStream, size_ } } ++encodedIndex; + break; } - else { - destStream[decodedIndex] = sourceStream[encodedIndex]; - } - - ++encodedIndex; - ++decodedIndex; - } - if (sourceStream[encodedIndex] != ETX_CHAR) { - if(decodedIndex == maxDestStreamlen) { - //so far we did not find anything wrong here, so let user try again - *readLen = 0; - return STREAM_TOO_SHORT; - } - else { + case(STX_CHAR): { *readLen = ++encodedIndex; return DECODING_ERROR; } + case(ETX_CHAR): { + *readLen = ++encodedIndex; + *decodedLen = decodedIndex; + return RETURN_OK; + } + default: { + destStream[decodedIndex] = sourceStream[encodedIndex]; + break; + } + } + ++encodedIndex; + ++decodedIndex; } - else { - *readLen = ++encodedIndex; - *decodedLen = decodedIndex; - return RETURN_OK; + + if(decodedIndex == maxDestStreamlen) { + //so far we did not find anything wrong here, so let user try again + *readLen = 0; + return STREAM_TOO_SHORT; + } else { + *readLen = encodedIndex; + return DECODING_ERROR; } } From 2439613f210d416c4d1c734756f76d4951fa2ab1 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 4 Oct 2021 14:38:10 +0200 Subject: [PATCH 8/8] preserve STX char --- src/fsfw/globalfunctions/DleEncoder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fsfw/globalfunctions/DleEncoder.cpp b/src/fsfw/globalfunctions/DleEncoder.cpp index f77d5472d..91db54454 100644 --- a/src/fsfw/globalfunctions/DleEncoder.cpp +++ b/src/fsfw/globalfunctions/DleEncoder.cpp @@ -198,7 +198,7 @@ ReturnValue_t DleEncoder::decodeStreamEscaped(const uint8_t *sourceStream, size_ break; } case(STX_CHAR): { - *readLen = ++encodedIndex; + *readLen = encodedIndex; return DECODING_ERROR; } case(ETX_CHAR): {