timer: Remove in favor of sdeventplus/timer
This change removes the ad-hoc timer implementation and uses the common
one that comes with sdeventplus.
Tested: Compiled
Change-Id: Id3b7e464a472a7421785601634af58f681ebd3a6
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/Makefile.am b/Makefile.am
index 28b5178..ed9413e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,7 +19,6 @@
libpower_la_SOURCES = \
gpio.cpp \
pmbus.cpp \
- timer.cpp \
utility.cpp \
org/open_power/Witherspoon/Fault/error.cpp
diff --git a/device_monitor.hpp b/device_monitor.hpp
index 0813737..3bde1b5 100644
--- a/device_monitor.hpp
+++ b/device_monitor.hpp
@@ -2,9 +2,10 @@
#include <phosphor-logging/log.hpp>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/server.hpp>
+#include <sdeventplus/clock.hpp>
#include <sdeventplus/event.hpp>
+#include <sdeventplus/utility/timer.hpp>
#include "device.hpp"
-#include "timer.hpp"
namespace witherspoon
{
@@ -42,12 +43,7 @@
const sdeventplus::Event& e,
std::chrono::milliseconds i) :
device(std::move(d)),
- event(e),
- interval(i),
- timer(e, [this]()
- {
- this->analyze();
- })
+ timer(e, std::bind(&DeviceMonitor::analyze, this), i)
{
}
@@ -56,8 +52,7 @@
*/
virtual int run()
{
- timer.start(interval, Timer::TimerType::repeating);
- return event.loop();
+ return timer.get_event().loop();
}
protected:
@@ -80,19 +75,9 @@
std::unique_ptr<Device> device;
/**
- * The event loop used by the timer
- */
- sdeventplus::Event event;
-
- /**
- * The polling interval in milliseconds
- */
- std::chrono::milliseconds interval;
-
- /**
* The timer that runs fault check polls.
*/
- Timer timer;
+ sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
};
}
diff --git a/power-sequencer/pgood_monitor.cpp b/power-sequencer/pgood_monitor.cpp
index b9995c6..29382c9 100644
--- a/power-sequencer/pgood_monitor.cpp
+++ b/power-sequencer/pgood_monitor.cpp
@@ -83,7 +83,7 @@
//The pgood-wait service (with a longer timeout)
//will handle powering off the system.
- event.exit(EXIT_SUCCESS);
+ timer.get_event().exit(EXIT_SUCCESS);
}
void PGOODMonitor::propertyChanged()
@@ -93,7 +93,7 @@
if (!pgoodPending())
{
//PGOOD is on, or system is off, so we are done.
- event.exit(EXIT_SUCCESS);
+ timer.get_event().exit(EXIT_SUCCESS);
}
}
@@ -121,8 +121,7 @@
return EXIT_SUCCESS;
}
- timer.start(interval);
- return event.loop();
+ return timer.get_event().loop();
}
catch (std::exception& e)
{
diff --git a/power-sequencer/pgood_monitor.hpp b/power-sequencer/pgood_monitor.hpp
index 14a2735..ec8e05e 100644
--- a/power-sequencer/pgood_monitor.hpp
+++ b/power-sequencer/pgood_monitor.hpp
@@ -5,7 +5,6 @@
#include <sdeventplus/event.hpp>
#include "device.hpp"
#include "device_monitor.hpp"
-#include "timer.hpp"
namespace witherspoon
{
diff --git a/power-sequencer/runtime_monitor.cpp b/power-sequencer/runtime_monitor.cpp
index 9bfeaf0..514ba8f 100644
--- a/power-sequencer/runtime_monitor.cpp
+++ b/power-sequencer/runtime_monitor.cpp
@@ -43,7 +43,7 @@
try
{
- timer.stop();
+ timer.setEnabled(false);
#ifdef UCD90160_DEVICE_ACCESS
device->onFailure();
diff --git a/power-sequencer/runtime_monitor.hpp b/power-sequencer/runtime_monitor.hpp
index d37cdc0..fb3dcb1 100644
--- a/power-sequencer/runtime_monitor.hpp
+++ b/power-sequencer/runtime_monitor.hpp
@@ -5,7 +5,6 @@
#include <sdeventplus/event.hpp>
#include "device.hpp"
#include "device_monitor.hpp"
-#include "timer.hpp"
namespace witherspoon
{
diff --git a/power-supply/power_supply.cpp b/power-supply/power_supply.cpp
index f5187f0..ff98457 100644
--- a/power-supply/power_supply.cpp
+++ b/power-supply/power_supply.cpp
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+#include <functional>
#include <phosphor-logging/log.hpp>
#include <phosphor-logging/elog.hpp>
#include <org/open_power/Witherspoon/Fault/error.hpp>
@@ -74,7 +75,7 @@
: Device(name, inst), monitorPath(objpath), pmbusIntf(objpath),
inventoryPath(INVENTORY_OBJ_PATH + invpath), bus(bus),
presentInterval(p),
- presentTimer(e, [this]()
+ presentTimer(e, std::bind([this]()
{
// The hwmon path may have changed.
pmbusIntf.findHwmonDir();
@@ -85,12 +86,12 @@
// Update the inventory for the new device
updateInventory();
- }),
+ })),
powerOnInterval(t),
- powerOnTimer(e, [this]()
+ powerOnTimer(e, std::bind([this]()
{
this->powerOn = true;
- })
+ }))
{
using namespace sdbusplus::bus;
presentMatch = std::make_unique<match_t>(bus,
@@ -196,12 +197,12 @@
if (sdbusplus::message::variant_ns::get<bool>(valPropMap->second))
{
clearFaults();
- presentTimer.start(presentInterval, Timer::TimerType::oneshot);
+ presentTimer.restartOnce(presentInterval);
}
else
{
present = false;
- presentTimer.stop();
+ presentTimer.setEnabled(false);
//Clear out the now outdated inventory properties
updateInventory();
@@ -238,11 +239,11 @@
if (state)
{
clearFaults();
- powerOnTimer.start(powerOnInterval, Timer::TimerType::oneshot);
+ powerOnTimer.restartOnce(powerOnInterval);
}
else
{
- powerOnTimer.stop();
+ powerOnTimer.setEnabled(false);
powerOn = false;
}
}
@@ -329,7 +330,7 @@
powerOn = false;
// Start up the timer that will set the state to indicate we
// are ready for the powered on fault checks.
- powerOnTimer.start(powerOnInterval, Timer::TimerType::oneshot);
+ powerOnTimer.restartOnce(powerOnInterval);
}
}
}
diff --git a/power-supply/power_supply.hpp b/power-supply/power_supply.hpp
index 30f660f..a56e2e2 100644
--- a/power-supply/power_supply.hpp
+++ b/power-supply/power_supply.hpp
@@ -1,13 +1,14 @@
#pragma once
#include <sdbusplus/bus/match.hpp>
+#include <sdeventplus/clock.hpp>
#include <sdeventplus/event.hpp>
+#include <sdeventplus/utility/timer.hpp>
#include "average.hpp"
#include "device.hpp"
#include "maximum.hpp"
#include "names_values.hpp"
#include "pmbus.hpp"
#include "record_manager.hpp"
-#include "timer.hpp"
namespace witherspoon
{
@@ -142,7 +143,7 @@
* The timer used to do the callback after the present property has
* changed.
*/
- Timer presentTimer;
+ sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> presentTimer;
/** @brief True if a fault has already been found and not cleared */
bool faultFound = false;
@@ -171,7 +172,7 @@
* The timer used to do the callback after the power state has been on
* long enough.
*/
- Timer powerOnTimer;
+ sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> powerOnTimer;
/** @brief Used to subscribe to D-Bus power on state changes */
std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch;
diff --git a/timer.cpp b/timer.cpp
deleted file mode 100644
index fba2845..0000000
--- a/timer.cpp
+++ /dev/null
@@ -1,167 +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 witherspoon
-{
-namespace power
-{
-
-using namespace phosphor::logging;
-using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
- Error::InternalFailure;
-
-Timer::Timer(const sdeventplus::Event& event,
- std::function<void()> callbackFunc) :
- eventSource(nullptr),
- callback(callbackFunc),
- timeout(0)
-{
- // Start with an infinite expiration time
- auto r = sd_event_add_time(event.get(),
- &eventSource,
- 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>();
- }
-
- //Ensure timer isn't running
- setTimer(SD_EVENT_OFF);
-}
-
-
-Timer::~Timer()
-{
- setTimer(SD_EVENT_OFF);
- sd_event_source_unref(eventSource);
-}
-
-
-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, 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, &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, 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 600fe42..0000000
--- a/timer.hpp
+++ /dev/null
@@ -1,157 +0,0 @@
-#pragma once
-
-#include <chrono>
-#include <functional>
-#include <memory>
-#include <sdeventplus/event.hpp>
-#include <systemd/sd-event.h>
-
-namespace witherspoon
-{
-namespace power
-{
-
-
-/**
- * @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 - sd_event pointer, 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().
- */
- 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
- */
- sd_event_source* 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;
-};
-
-}
-}