summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile.am10
-rw-r--r--configure.ac2
-rw-r--r--control/actions.cpp4
-rw-r--r--control/types.hpp14
-rw-r--r--control/zone.cpp42
-rw-r--r--control/zone.hpp13
-rw-r--r--event.hpp28
-rw-r--r--monitor/tach_sensor.cpp17
-rw-r--r--monitor/tach_sensor.hpp9
-rw-r--r--test/.gitignore1
-rw-r--r--test/Makefile.am13
-rw-r--r--test/timertest.cpp337
-rw-r--r--timer.cpp172
-rw-r--r--timer.hpp163
14 files changed, 45 insertions, 780 deletions
diff --git a/Makefile.am b/Makefile.am
index a20fe8a..5d81f75 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -5,18 +5,16 @@ libfan_la_LDFLAGS = -static
libfan_la_LIBADD = \
$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
$(PHOSPHOR_LOGGING_LIBS) \
- $(SDBUSPLUS_LIBS) \
- $(SDEVENTPLUS_LIBS)
+ $(SDBUSPLUS_LIBS)
libfan_la_CXXFLAGS = \
$(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
$(PHOSPHOR_LOGGING_CFLAGS) \
$(SDBUSPLUS_CFLAGS) \
- $(SDEVENTPLUS_CFLAGS) -flto
+ -flto
libfan_la_SOURCES = \
- utility.cpp \
- timer.cpp
+ utility.cpp
-SUBDIRS = . test sdevent/test evdevpp/test
+SUBDIRS = . sdevent/test evdevpp/test
if WANT_PRESENCE
SUBDIRS += presence
diff --git a/configure.ac b/configure.ac
index b60d032..f57bb02 100644
--- a/configure.ac
+++ b/configure.ac
@@ -170,5 +170,5 @@ AS_IF([test "x$enable_monitor" != "xno"], [
])
# Create configured output
-AC_CONFIG_FILES([Makefile test/Makefile sdevent/test/Makefile evdevpp/test/Makefile presence/test/Makefile])
+AC_CONFIG_FILES([Makefile sdevent/test/Makefile evdevpp/test/Makefile presence/test/Makefile])
AC_OUTPUT
diff --git a/control/actions.cpp b/control/actions.cpp
index 0267c10..25b6a09 100644
--- a/control/actions.cpp
+++ b/control/actions.cpp
@@ -41,10 +41,6 @@ Action call_actions_based_on_timer(TimerConf&& tConf,
auto timer = zone.findTimer(group, actions);
if (timer != std::end(zone.getTimerEvents()))
{
- if (std::get<timerTimerPos>(*timer)->running())
- {
- std::get<timerTimerPos>(*timer)->stop();
- }
zone.removeTimer(timer);
}
}
diff --git a/control/types.hpp b/control/types.hpp
index 967b0b4..239d00d 100644
--- a/control/types.hpp
+++ b/control/types.hpp
@@ -3,7 +3,8 @@
#include <tuple>
#include <vector>
#include <sdbusplus/server.hpp>
-#include "timer.hpp"
+#include <sdeventplus/clock.hpp>
+#include <sdeventplus/utility/timer.hpp>
namespace phosphor
{
@@ -64,7 +65,11 @@ using Service = std::tuple<std::string, bool>;
constexpr auto intervalPos = 0;
constexpr auto typePos = 1;
-using TimerType = phosphor::fan::util::Timer::TimerType;
+enum class TimerType
+{
+ oneshot,
+ repeating,
+};
using TimerConf = std::tuple<std::chrono::seconds,
TimerType>;
@@ -89,9 +94,8 @@ using EventData = std::tuple<Group, std::string, Handler, std::vector<Action>>;
constexpr auto timerEventDataPos = 0;
constexpr auto timerTimerPos = 1;
-using TimerEvent =
- std::tuple<std::unique_ptr<EventData>,
- std::unique_ptr<phosphor::fan::util::Timer>>;
+using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
+using TimerEvent = std::tuple<std::unique_ptr<EventData>, Timer>;
constexpr auto signalEventDataPos = 0;
constexpr auto signalMatchPos = 1;
diff --git a/control/zone.cpp b/control/zone.cpp
index 4531151..293945c 100644
--- a/control/zone.cpp
+++ b/control/zone.cpp
@@ -18,6 +18,7 @@
#include <phosphor-logging/log.hpp>
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/elog-errors.hpp>
+#include <stdexcept>
#include <xyz/openbmc_project/Common/error.hpp>
#include "zone.hpp"
#include "utility.hpp"
@@ -72,10 +73,7 @@ Zone::Zone(Mode mode,
initEvent(event);
}
// Start timer for fan speed decreases
- if (!_decTimer.running() && _decInterval != seconds::zero())
- {
- _decTimer.start(_decInterval, TimerType::repeating);
- }
+ _decTimer.restart(_decInterval);
}
}
@@ -238,14 +236,9 @@ void Zone::requestSpeedIncrease(uint64_t targetDelta)
{
requestTarget = _ceilingSpeed;
}
- // Cancel current timer countdown
- if (_incTimer.running())
- {
- _incTimer.stop();
- }
setSpeed(requestTarget);
- // Start timer countdown for fan speed increase
- _incTimer.start(_incDelay, TimerType::oneshot);
+ // Retart timer countdown for fan speed increase
+ _incTimer.restartOnce(_incDelay);
}
}
@@ -277,7 +270,7 @@ void Zone::decTimerExpired()
// where no requested increases exist and
// the increase timer is not running
// (i.e. not in the middle of increasing)
- if (decAllowed && _incSpeedDelta == 0 && !_incTimer.running())
+ if (decAllowed && _incSpeedDelta == 0 && !_incTimer.isEnabled())
{
auto requestTarget = getRequestSpeedBase();
// Request target speed should not start above ceiling
@@ -449,26 +442,31 @@ void Zone::addTimer(const Group& group,
const std::vector<Action>& actions,
const TimerConf& tConf)
{
- // Associate event data with timer
- auto data = std::make_unique<EventData>(
+ auto eventData = std::make_unique<EventData>(
group,
"",
nullptr,
actions
);
- auto timer = std::make_unique<util::Timer>(
+ Timer timer(
_eventLoop,
std::bind(&Zone::timerExpired,
this,
- std::cref(std::get<Group>(*data)),
- std::cref(std::get<std::vector<Action>>(*data)))
- );
- if (!timer->running())
+ std::cref(std::get<Group>(*eventData)),
+ std::cref(std::get<std::vector<Action>>(*eventData))));
+ if (std::get<TimerType>(tConf) == TimerType::repeating)
+ {
+ timer.restart(std::get<intervalPos>(tConf));
+ }
+ else if (std::get<TimerType>(tConf) == TimerType::oneshot)
+ {
+ timer.restartOnce(std::get<intervalPos>(tConf));
+ }
+ else
{
- timer->start(std::get<intervalPos>(tConf),
- std::get<typePos>(tConf));
+ throw std::invalid_argument("Invalid Timer Type");
}
- _timerEvents.emplace_back(std::move(data), std::move(timer));
+ _timerEvents.emplace_back(std::move(eventData), std::move(timer));
}
void Zone::timerExpired(const Group& eventGroup,
diff --git a/control/zone.hpp b/control/zone.hpp
index d01e972..9c85655 100644
--- a/control/zone.hpp
+++ b/control/zone.hpp
@@ -1,14 +1,12 @@
#pragma once
+#include <algorithm>
#include <cassert>
#include <chrono>
-#include <vector>
-#include <cassert>
-#include <algorithm>
#include <sdbusplus/bus.hpp>
#include <sdeventplus/event.hpp>
+#include <vector>
#include "fan.hpp"
#include "types.hpp"
-#include "timer.hpp"
namespace phosphor
{
@@ -423,9 +421,6 @@ class Zone
*/
inline void removeTimer(std::vector<TimerEvent>::iterator& teIter)
{
- assert(teIter != std::end(_timerEvents));
- std::get<timerEventDataPos>(*teIter).reset();
- std::get<timerTimerPos>(*teIter).reset();
_timerEvents.erase(teIter);
}
@@ -547,12 +542,12 @@ class Zone
/**
* The increase timer object
*/
- phosphor::fan::util::Timer _incTimer;
+ Timer _incTimer;
/**
* The decrease timer object
*/
- phosphor::fan::util::Timer _decTimer;
+ Timer _decTimer;
/**
* Event loop used on set speed event timers
diff --git a/event.hpp b/event.hpp
deleted file mode 100644
index a937d6d..0000000
--- a/event.hpp
+++ /dev/null
@@ -1,28 +0,0 @@
-#pragma once
-
-#include <memory>
-#include <systemd/sd-event.h>
-
-namespace phosphor
-{
-namespace fan
-{
-namespace event
-{
-
-/**
- * Custom deleter for sd_event_source
- */
-struct EventSourceDeleter
-{
- void operator()(sd_event_source* eventSource) const
- {
- sd_event_source_unref(eventSource);
- }
-};
-
-using EventSourcePtr = std::unique_ptr<sd_event_source, EventSourceDeleter>;
-
-}
-}
-}
diff --git a/monitor/tach_sensor.cpp b/monitor/tach_sensor.cpp
index 26ba3c9..5494450 100644
--- a/monitor/tach_sensor.cpp
+++ b/monitor/tach_sensor.cpp
@@ -221,24 +221,11 @@ void TachSensor::handleTachChange(sdbusplus::message::message& msg)
void TachSensor::startTimer(TimerMode mode)
{
- if (!timerRunning())
+ if (!timerRunning() || mode != _timerMode)
{
- _timer.start(
- getDelay(mode),
- util::Timer::TimerType::oneshot);
+ _timer.restartOnce(getDelay(mode));
_timerMode = mode;
}
- else
- {
- if (mode != _timerMode)
- {
- _timer.stop();
- _timer.start(
- getDelay(mode),
- util::Timer::TimerType::oneshot);
- _timerMode = mode;
- }
- }
}
std::chrono::microseconds TachSensor::getDelay(TimerMode mode)
diff --git a/monitor/tach_sensor.hpp b/monitor/tach_sensor.hpp
index c46aa00..2a643c5 100644
--- a/monitor/tach_sensor.hpp
+++ b/monitor/tach_sensor.hpp
@@ -3,8 +3,9 @@
#include <chrono>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/server.hpp>
+#include <sdeventplus/clock.hpp>
#include <sdeventplus/event.hpp>
-#include "timer.hpp"
+#include <sdeventplus/utility/timer.hpp>
namespace phosphor
{
@@ -158,7 +159,7 @@ class TachSensor
*/
inline bool timerRunning()
{
- return _timer.running();
+ return _timer.isEnabled();
}
/**
@@ -174,7 +175,7 @@ class TachSensor
*/
inline void stopTimer()
{
- _timer.stop();
+ _timer.setEnabled(false);
}
/**
@@ -302,7 +303,7 @@ class TachSensor
/**
* The timer object
*/
- phosphor::fan::util::Timer _timer;
+ sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
/**
* @brief The match object for the Value properties changed signal
diff --git a/test/.gitignore b/test/.gitignore
deleted file mode 100644
index 0e55de9..0000000
--- a/test/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-/timertest
diff --git a/test/Makefile.am b/test/Makefile.am
deleted file mode 100644
index c4cd77d..0000000
--- a/test/Makefile.am
+++ /dev/null
@@ -1,13 +0,0 @@
-AM_CPPFLAGS = -I$(top_srcdir)
-
-# Run all 'check' test programs
-TESTS = $(check_PROGRAMS)
-
-# # Build/add timertest to test suite
-check_PROGRAMS = timertest
-timertest_CPPFLAGS = -Igtest $(GTEST_CPPFLAGS) $(AM_CPPFLAGS)
-timertest_CXXFLAGS = $(PTHREAD_CFLAGS) ${PHOSPHOR_DBUS_INTERFACES_CFLAGS}
-timertest_LDFLAGS = -lgtest_main -lgtest $(PTHREAD_LIBS) $(OESDK_TESTCASE_FLAGS) \
- $(SYSTEMD_LIBS) ${SDBUSPLUS_LIBS} $(SDEVENTPLUS_LIBS) ${PHOSPHOR_DBUS_INTERFACES_LIBS}
-timertest_SOURCES = timertest.cpp
-timertest_LDADD = $(top_builddir)/timer.o
diff --git a/test/timertest.cpp b/test/timertest.cpp
deleted file mode 100644
index 845a0e1..0000000
--- a/test/timertest.cpp
+++ /dev/null
@@ -1,337 +0,0 @@
-/**
- * Copyright © 2017 IBM Corporation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include <iostream>
-#include <chrono>
-#include <gtest/gtest.h>
-#include <sdeventplus/event.hpp>
-#include "timer.hpp"
-
-/**
- * Testcases for the Timer class
- */
-
-using namespace phosphor::fan::util;
-using namespace std::chrono;
-
-
-/**
- * Class to ensure sd_events are correctly
- * setup and destroyed.
- */
-class TimerTest : public ::testing::Test
-{
- public:
- // event loop
- sdeventplus::Event event;
-
- // Gets called as part of each TEST_F construction
- TimerTest() : event(sdeventplus::Event::get_default())
- { }
-};
-
-/**
- * Helper class to hande tracking timer expirations
- * via callback functions.
- */
-class CallbackTester
-{
- public:
-
- CallbackTester() {}
-
- size_t getCount()
- {
- return _count;
- }
-
- void callbackFunction()
- {
- _count++;
- _gotCallback = true;
- }
-
- bool gotCallback()
- {
- return _gotCallback;
- }
-
- private:
- bool _gotCallback = false;
- size_t _count = 0;
-};
-
-
-/**
- * Helper class that more closely mimics real usage,
- * which is another class containing a timer and using
- * one of its member functions as the callback.
- */
-class CallbackTesterWithTimer : public CallbackTester
-{
- public:
- CallbackTesterWithTimer(const sdeventplus::Event& event) :
- _timer(event,
- std::bind(&CallbackTesterWithTimer::callbackFunction,
- this))
- {
- }
-
- void callbackFunction()
- {
- //restart the timer once from the callback
- if (!_restarted)
- {
- _restarted = true;
- auto time = duration_cast<microseconds>(seconds(1));
- _timer.start(time, Timer::TimerType::oneshot);
- }
-
- CallbackTester::callbackFunction();
- }
-
- Timer& getTimer()
- {
- return _timer;
- }
-
- inline bool restarted() const
- {
- return _restarted;
- }
-
- private:
-
- Timer _timer;
- bool _restarted = false;
-};
-
-
-/**
- * Test that a callback will occur after 2 seconds.
- */
-TEST_F(TimerTest, timerExpiresAfter2seconds)
-{
- CallbackTester tester;
-
- Timer timer(event,
- std::bind(&CallbackTester::callbackFunction, &tester));
-
-
- auto time = duration_cast<microseconds>(seconds(2));
-
- EXPECT_EQ(false, timer.running());
-
- timer.start(time, Timer::TimerType::oneshot);
- EXPECT_EQ(false, tester.gotCallback());
- EXPECT_EQ(true, timer.running());
-
- int count = 0;
-
- //Wait for 2 1s timeouts
- while (count < 2)
- {
- // Returns 0 on timeout and positive number on dispatch
- if (event.run(seconds(1)) == 0)
- {
- count++;
- }
- }
-
- EXPECT_EQ(true, tester.gotCallback());
- EXPECT_EQ(1, tester.getCount());
- EXPECT_EQ(false, timer.running());
-}
-
-/**
- * Test that a timer can be restarted.
- */
-TEST_F(TimerTest, timerRestart)
-{
- CallbackTester tester;
-
- Timer timer(event,
- std::bind(&CallbackTester::callbackFunction, &tester));
-
-
- auto time = duration_cast<microseconds>(seconds(2));
- timer.start(time, Timer::TimerType::oneshot);
-
- //wait for a second
- auto rc = event.run(seconds(1));
-
- //expect the timeout, not the dispatch
- //and the timer should still be running
- EXPECT_EQ(0, rc);
- EXPECT_EQ(true, timer.running());
-
- //Restart it
- timer.start(time, Timer::TimerType::oneshot);
-
- //Wait just 1s, make sure not done
- rc = event.run(seconds(1));
- EXPECT_EQ(0, rc);
- EXPECT_EQ(true, timer.running());
- EXPECT_EQ(false, tester.gotCallback());
-
- //Wait 1 more second, this time expecting a dispatch
- int count = 0;
- while (count < 1)
- {
- // Returns 0 on timeout and positive number on dispatch
- if (event.run(seconds(1)) == 0)
- {
- count++;
- }
- }
-
- EXPECT_EQ(true, tester.gotCallback());
- EXPECT_EQ(1, tester.getCount());
- EXPECT_EQ(false, timer.running());
-}
-
-
-/**
- * Test that a timer can be stopped.
- */
-TEST_F(TimerTest, timerStop)
-{
- CallbackTester tester;
-
- Timer timer(event,
- std::bind(&CallbackTester::callbackFunction, &tester));
-
-
- auto time = duration_cast<microseconds>(seconds(2));
- timer.start(time, Timer::TimerType::oneshot);
-
- //wait 1s
- auto rc = event.run(seconds(1));
-
- //expect the timeout, not the dispatch
- EXPECT_EQ(rc, 0);
- EXPECT_EQ(true, timer.running());
-
- timer.stop();
-
- EXPECT_EQ(false, timer.running());
- EXPECT_EQ(false, tester.gotCallback());
-
- //Wait another 2s, make sure no callbacks happened
- rc = event.run(seconds(2));
-
- EXPECT_EQ(rc, 0);
- EXPECT_EQ(false, timer.running());
- EXPECT_EQ(false, tester.gotCallback());
-}
-
-
-/**
- * Test that the timer can be restarted from within
- * a callback function.
- */
-TEST_F(TimerTest, timerRestartFromCallback)
-{
- CallbackTesterWithTimer tester(event);
-
- auto& timer = tester.getTimer();
-
- auto time = duration_cast<microseconds>(seconds(2));
- timer.start(time, Timer::TimerType::oneshot);
-
- //after running for 2 seconds, the timer will get restarted
- //for another 1s
-
- int count = 0;
- while (count < 3)
- {
- // Returns 0 on timeout and positive number on dispatch
- if (event.run(seconds(1)) == 0)
- {
- count++;
- }
- }
-
- EXPECT_EQ(false, timer.running());
- EXPECT_EQ(true, tester.gotCallback());
- EXPECT_EQ(2, tester.getCount()); //2 callbacks
- EXPECT_EQ(true, tester.restarted());
-}
-
-/**
- * This shows what happens when the timer expires but
- * sd_event_run never got called.
- */
-TEST_F(TimerTest, timerNoEventRun)
-{
- CallbackTester tester;
-
- Timer timer(event,
- std::bind(&CallbackTester::callbackFunction, &tester));
-
-
- auto time = duration_cast<microseconds>(milliseconds(500));
-
- timer.start(time, Timer::TimerType::oneshot);
-
- sleep(1);
-
- //The timer should have expired, but with no event processing
- //it will still think it's running.
-
- EXPECT_EQ(true, timer.running());
- EXPECT_EQ(false, tester.gotCallback());
-
- //Now process an event
- auto rc = event.run(milliseconds(5));
-
- EXPECT_GT(rc, 0);
- EXPECT_EQ(false, timer.running());
- EXPECT_EQ(true, tester.gotCallback());
-}
-
-
-/**
- * Tests that a timer in repeating mode will keep calling
- * the callback.
- */
-TEST_F(TimerTest, RepeatingTimer)
-{
- CallbackTester tester;
-
- Timer timer(event,
- std::bind(&CallbackTester::callbackFunction, &tester));
-
- auto time = duration_cast<microseconds>(seconds(1));
- timer.start(time, Timer::TimerType::repeating);
-
- int count = 0;
-
- while (count < 5)
- {
- if (event.run(milliseconds(500)) == 0)
- {
- count++;
- }
- }
-
- EXPECT_EQ(true, timer.running());
- EXPECT_EQ(true, tester.gotCallback());
- EXPECT_EQ(4, tester.getCount());
-
- timer.stop();
-
- EXPECT_EQ(false, timer.running());
-}
diff --git a/timer.cpp b/timer.cpp
deleted file mode 100644
index 36057ab..0000000
--- a/timer.cpp
+++ /dev/null
@@ -1,172 +0,0 @@
-/**
- * Copyright © 2017 IBM Corporation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include <chrono>
-#include <phosphor-logging/log.hpp>
-#include <phosphor-logging/elog.hpp>
-#include <phosphor-logging/elog-errors.hpp>
-#include <xyz/openbmc_project/Common/error.hpp>
-#include <type_traits>
-#include "timer.hpp"
-
-namespace phosphor
-{
-namespace fan
-{
-namespace util
-{
-
-using namespace phosphor::logging;
-using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
- Error::InternalFailure;
-
-Timer::Timer(const sdeventplus::Event& event,
- std::function<void()> callbackFunc) :
- callback(callbackFunc),
- timeout(0)
-{
- sd_event_source* source = nullptr;
-
- // Start with an infinite expiration time
- auto r = sd_event_add_time(event.get(),
- &source,
- CLOCK_MONOTONIC, // Time base
- UINT64_MAX, // Expire time - way long time
- 0, // Use default event accuracy
- timeoutHandler, // Callback handler on timeout
- this); // User data
- if (r < 0)
- {
- log<level::ERR>("Timer::Timer failed call to sd_event_add_time",
- entry("ERROR=%s", strerror(-r)));
- elog<InternalFailure>();
- }
-
- eventSource.reset(source);
-
- //Ensure timer isn't running
- setTimer(SD_EVENT_OFF);
-}
-
-
-Timer::~Timer()
-{
- setTimer(SD_EVENT_OFF);
-}
-
-
-int Timer::timeoutHandler(sd_event_source* eventSource,
- uint64_t usec, void* userData)
-{
- auto timer = static_cast<Timer*>(userData);
-
- if (timer->type == TimerType::repeating)
- {
- //Set the next expiration time
- timer->setTimeout();
- }
-
- timer->callback();
-
- return 0;
-}
-
-
-std::chrono::microseconds Timer::getTime()
-{
- using namespace std::chrono;
- auto now = steady_clock::now().time_since_epoch();
- return duration_cast<microseconds>(now);
-}
-
-
-void Timer::setTimer(int action)
-{
- auto r = sd_event_source_set_enabled(eventSource.get(), action);
- if (r < 0)
- {
- log<level::ERR>("Failed call to sd_event_source_set_enabled",
- entry("ERROR=%s", strerror(-r)),
- entry("ACTION=%d", action));
- elog<InternalFailure>();
- }
-}
-
-
-void Timer::stop()
-{
- setTimer(SD_EVENT_OFF);
-}
-
-
-bool Timer::running()
-{
- int status = 0;
-
- //returns SD_EVENT_OFF, SD_EVENT_ON, or SD_EVENT_ONESHOT
- auto r = sd_event_source_get_enabled(eventSource.get(), &status);
- if (r < 0)
- {
- log<level::ERR>("Failed call to sd_event_source_get_enabled",
- entry("ERROR=%s", strerror(-r)));
- elog<InternalFailure>();
- }
-
- return (status != SD_EVENT_OFF);
-}
-
-
-void Timer::setTimeout()
-{
- //Get the current time and add the delta
- static_assert(std::is_same<decltype(getTime()),
- std::chrono::microseconds>::value,
- "Timer::getTime() is the wrong type");
- static_assert(std::is_same<decltype(timeout),
- std::chrono::microseconds>::value,
- "Timer::timeout is the wrong type");
-
- auto expireTime = getTime() + timeout;
-
- //Set the time
- auto r = sd_event_source_set_time(eventSource.get(), expireTime.count());
- if (r < 0)
- {
- log<level::ERR>("Failed call to sd_event_source_set_time",
- entry("ERROR=%s", strerror(-r)));
- elog<InternalFailure>();
- }
-}
-
-
-void Timer::start(std::chrono::microseconds timeValue,
- TimerType timerType)
-{
- type = timerType;
-
- // Disable the timer
- setTimer(SD_EVENT_OFF);
-
- //Rearm the timer
- timeout = timeValue;
- setTimeout();
-
- setTimer((type == TimerType::oneshot) ? SD_EVENT_ONESHOT : SD_EVENT_ON);
-}
-
-
-}
-}
-}
diff --git a/timer.hpp b/timer.hpp
deleted file mode 100644
index 3b7783b..0000000
--- a/timer.hpp
+++ /dev/null
@@ -1,163 +0,0 @@
-#pragma once
-
-#include <chrono>
-#include <functional>
-#include <memory>
-#include <sdeventplus/event.hpp>
-#include "event.hpp"
-
-namespace phosphor
-{
-namespace fan
-{
-namespace util
-{
-
-
-/**
- * @class Timer
- *
- * This class implements a simple timer that runs an arbitrary
- * function on expiration. The timeout value is set in microseconds.
- * It can be stopped while it is running, and queried to see if it is
- * running.
- *
- * If started with the 'repeating' argument, it will keep calling the
- * callback function every <timeout> microseconds. If started with the
- * 'oneshot' argument, it will just call the callback function one time.
- *
- * It needs an sd_event loop to function.
- */
-class Timer
-{
- public:
-
- enum class TimerType
- {
- oneshot,
- repeating
- };
-
- Timer() = delete;
- Timer(const Timer&) = delete;
- Timer& operator=(const Timer&) = delete;
- Timer(Timer&&) = default;
- Timer& operator=(Timer&&) = default;
-
- /**
- * @brief Constructs timer object
- *
- * @param[in] event - Event loop reference, previously created
- * @param[in] callbackFunc - The function to call on timer expiration
- */
- Timer(const sdeventplus::Event& event,
- std::function<void()> callbackFunc);
-
- /**
- * @brief Destructor
- */
- ~Timer();
-
- /**
- * @brief Starts the timer
- *
- * The input is an offset from the current steady clock.
- *
- * @param[in] usec - the timeout value in microseconds
- * @param[in] type - either a oneshot, or repeating
- */
- void start(std::chrono::microseconds usec,
- TimerType type = TimerType::oneshot);
-
- /**
- * @brief Stop the timer
- */
- void stop();
-
- /**
- * @brief Returns true if the timer is running
- */
- bool running();
-
- /**
- * @brief Returns the timeout value
- *
- * @return - the last value sent in via start().
- *
- * Could be used to restart the timer with the same
- * timeout. i.e. start(getTimeout());
- */
- inline auto getTimeout() const
- {
- return timeout;
- }
-
- /**
- * @brief Returns the timer type
- */
- inline auto getType() const
- {
- return type;
- }
-
- private:
-
- /**
- * @brief Callback function when timer goes off
- *
- * Calls the callback function passed in by the user.
- *
- * @param[in] eventSource - Source of the event
- * @param[in] usec - time in micro seconds
- * @param[in] userData - User data pointer
- */
- static int timeoutHandler(sd_event_source* eventSource,
- uint64_t usec, void* userData);
-
- /**
- * @brief Gets the current time from the steady clock
- */
- std::chrono::microseconds getTime();
-
- /**
- * @brief Wrapper around sd_event_source_set_enabled
- *
- * @param[in] action - either SD_EVENT_OFF, SD_EVENT_ON,
- * or SD_EVENT_ONESHOT
- */
- void setTimer(int action);
-
-
- /**
- * @brief Sets the expiration time for the timer
- *
- * Sets it to timeout microseconds in the future
- */
- void setTimeout();
-
- /**
- * @brief Source of events
- */
- phosphor::fan::event::EventSourcePtr eventSource;
-
- /**
- * @brief Either 'repeating' or 'oneshot'
- */
- TimerType type = TimerType::oneshot;
-
- /**
- * @brief The function to call when the timer expires
- */
- std::function<void()> callback;
-
- /**
- * @brief What the timer was set to run for
- *
- * Not cleared on timer expiration
- */
- std::chrono::microseconds timeout;
-};
-
-}
-}
-}
OpenPOWER on IntegriCloud