From 2b24de4f2bb7d9288af1aa3223139fce5d13be9e Mon Sep 17 00:00:00 2001 From: alek Date: Fri, 8 Jan 2021 15:17:38 +0100 Subject: [PATCH] [EGD-5151] Fix timers callback issue Fix timers callbacked issue and make sure there is no need to call connect() after each stop(). --- module-sys/Service/Timer.cpp | 9 ++++++--- module-sys/Service/Timer.hpp | 10 ++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/module-sys/Service/Timer.cpp b/module-sys/Service/Timer.cpp index 593a8d2c62b60181aacdb08b84a12a453fd462ab..b9e88a5a804c4b89b2f5a79fbbd671543a4e95d8 100644 --- a/module-sys/Service/Timer.cpp +++ b/module-sys/Service/Timer.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "Timer.hpp" @@ -67,6 +67,7 @@ namespace sys void Timer::start() { log_timers("Timer %s start!", name.c_str()); + isActive = true; Start(0); } @@ -78,13 +79,15 @@ namespace sys SetPeriod(pdMS_TO_TICKS(interval)); } log_timers("Timer %s reload!", name.c_str()); + isActive = true; Start(0); // start with no waittime } void Timer::stop() { log_timers("Timer %s stop!", name.c_str()); - callback = nullptr; + // make sure callback is not called even if it is already in the queue + isActive = false; Stop(0); } @@ -98,7 +101,7 @@ namespace sys void Timer::onTimeout() { log_timers("Timer %s tick", name.c_str()); - if (callback != nullptr) { + if (callback != nullptr && isActive) { log_timers("Timer %s callback run!", name.c_str()); callback(*this); } diff --git a/module-sys/Service/Timer.hpp b/module-sys/Service/Timer.hpp index 9ca185baedea9cc80b368c4ca0b9f050ae82a91a..d44c27371068d9564e53505669a59ae7cbbff2da 100644 --- a/module-sys/Service/Timer.hpp +++ b/module-sys/Service/Timer.hpp @@ -1,13 +1,14 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once #include "FreeRTOS.h" -#include "portmacro.h" // for TickType_t +#include "portmacro.h" // for TickType_t #include // for Timer -#include // for function -#include // for string +#include // for function +#include // for string +#include namespace sys { class Service; @@ -69,6 +70,7 @@ namespace sys Type type; ms interval; std::function callback = nullptr; + std::atomic_bool isActive = false; const std::string name; public: