Skip to content

Commit 2e379d4

Browse files
committed
[BH-2076] Fix timer interval overflow
Fix of the issue that SystemTimer interval computation would overflow when trying to set interval longer than 4,294,967ms. That resulted in setting invalid timeout value.
1 parent 240e9eb commit 2e379d4

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

module-sys/Service/SystemTimer.cpp

+8-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <Service/Service.hpp>
66
#include <Timers/TimerMessage.hpp>
77
#include <log/log.hpp>
8-
#include <projdefs.h>
8+
#include <ticks.hpp>
99
#include <memory>
1010

1111
#if DEBUG_TIMER == 1
@@ -16,12 +16,9 @@
1616

1717
namespace sys::timer
1818
{
19-
SystemTimer::SystemTimer(Service *parent,
20-
const std::string &name,
21-
std::chrono::milliseconds interval,
22-
timer::Type type)
23-
: cpp_freertos::Timer(name.c_str(), pdMS_TO_TICKS(interval.count()), type == timer::Type::Periodic), name{name},
24-
interval{interval}, type{type}, parent{parent}
19+
SystemTimer::SystemTimer(Service *parent, const std::string &name, std::chrono::milliseconds interval, Type type)
20+
: cpp_freertos::Timer(name.c_str(), cpp_freertos::Ticks::MsToTicks(interval.count()), type == Type::Periodic),
21+
name{name}, interval{interval}, type{type}, parent{parent}
2522
{
2623
attachToService();
2724
log_debug("%s %s timer created", name.c_str(), type == Type::Periodic ? "periodic" : "single-shot");
@@ -84,9 +81,9 @@ namespace sys::timer
8481

8582
void SystemTimer::setInterval(std::chrono::milliseconds value)
8683
{
87-
log_debug("Timer %s set interval to %ld ms!", name.c_str(), static_cast<long int>(value.count()));
84+
log_debug("Timer %s set interval to %" PRIi64 " ms!", name.c_str(), value.count());
8885
interval = value;
89-
cpp_freertos::Timer::SetPeriod(pdMS_TO_TICKS(interval.count()), 0);
86+
cpp_freertos::Timer::SetPeriod(cpp_freertos::Ticks::MsToTicks(interval.count()), 0);
9087
}
9188

9289
void SystemTimer::onTimeout()
@@ -101,7 +98,7 @@ namespace sys::timer
10198
return;
10299
}
103100
log_debug("Timer %s runs callback", name.c_str());
104-
if (type == timer::Type::SingleShot) {
101+
if (type == Type::SingleShot) {
105102
stop();
106103
}
107104
callback(*this);
@@ -112,7 +109,7 @@ namespace sys::timer
112109
return active;
113110
}
114111

115-
void SystemTimer::connect(timer::TimerCallback &&newCallback) noexcept
112+
void SystemTimer::connect(TimerCallback &&newCallback) noexcept
116113
{
117114
callback = std::move(newCallback);
118115
}

module-sys/Service/include/Timers/SystemTimer.hpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#pragma once
55

66
#include "FreeRTOS.h"
7-
#include "portmacro.h" // for TickType_t
8-
#include <module-os/RTOSWrapper/include/timer.hpp> // for Timer
7+
#include "portmacro.h"
98
#include <Timers/Timer.hpp>
10-
#include <functional> // for function
11-
#include <string> // for string
9+
#include <timer.hpp>
10+
#include <functional>
11+
#include <string>
1212
#include <atomic>
1313

1414
namespace sys
@@ -26,7 +26,7 @@ namespace sys::timer
2626
/// @param name this will be name of timer + postfix
2727
/// @param interval time for next timer event in
2828
/// @param type type of timer
29-
SystemTimer(Service *parent, const std::string &name, std::chrono::milliseconds interval, timer::Type type);
29+
SystemTimer(Service *parent, const std::string &name, std::chrono::milliseconds interval, Type type);
3030
SystemTimer(const SystemTimer &) = delete;
3131
SystemTimer(SystemTimer &&) noexcept = delete;
3232
SystemTimer &operator=(const SystemTimer &) = delete;
@@ -36,10 +36,10 @@ namespace sys::timer
3636
void start() override;
3737
void restart(std::chrono::milliseconds newInterval) override;
3838
void stop() override;
39-
bool isActive() const noexcept override;
39+
[[nodiscard]] bool isActive() const noexcept override;
4040

4141
void setInterval(std::chrono::milliseconds value);
42-
void connect(timer::TimerCallback &&newCallback) noexcept;
42+
void connect(TimerCallback &&newCallback) noexcept;
4343
void onTimeout();
4444

4545
private:
@@ -52,9 +52,9 @@ namespace sys::timer
5252
void attachToService();
5353

5454
std::string name;
55-
timer::TimerCallback callback;
55+
TimerCallback callback;
5656
std::chrono::milliseconds interval;
57-
timer::Type type;
57+
Type type;
5858
Service *parent = nullptr;
5959
std::atomic_bool active = false;
6060
};

0 commit comments

Comments
 (0)