timer: Replace with sdeventplus/timer
This is aimed at replacing the ad-hoc timer implementation contained in
each of our openbmc daemons, with a single well-tested timer
implementation.
Tested:
Compiled
Change-Id: I3e562ab72820442aa137a2d517e476192ea6c1bd
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/chassis_state_manager.cpp b/chassis_state_manager.cpp
index 4e8e1e7..d721460 100644
--- a/chassis_state_manager.cpp
+++ b/chassis_state_manager.cpp
@@ -1,5 +1,7 @@
#include <sdbusplus/bus.hpp>
#include <sdbusplus/exception.hpp>
+#include <sdeventplus/event.hpp>
+#include <sdeventplus/exception.hpp>
#include <phosphor-logging/log.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include "xyz/openbmc_project/Common/error.hpp"
@@ -21,6 +23,7 @@
using namespace phosphor::logging;
using sdbusplus::exception::SdBusError;
+using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
constexpr auto CHASSIS_STATE_POWEROFF_TGT = "obmc-chassis-poweroff@0.target";
constexpr auto CHASSIS_STATE_HARD_POWEROFF_TGT =
@@ -268,14 +271,7 @@
convertForMessage(value).c_str()));
chassisPowerState = server::Chassis::currentPowerState(value);
- if (chassisPowerState == PowerState::On)
- {
- timer->state(timer::ON);
- }
- else
- {
- timer->state(timer::OFF);
- }
+ pOHTimer.setEnabled(chassisPowerState == PowerState::On);
return chassisPowerState;
}
@@ -289,6 +285,14 @@
return pOHCounter();
}
+void Chassis::pOHCallback()
+{
+ if (ChassisInherit::currentPowerState() == PowerState::On)
+ {
+ pOHCounter(pOHCounter() + 1);
+ }
+}
+
void Chassis::restorePOHCounter()
{
uint32_t counter;
@@ -340,47 +344,19 @@
void Chassis::startPOHCounter()
{
- using namespace std::chrono_literals;
- using namespace phosphor::logging;
- using namespace sdbusplus::xyz::openbmc_project::Common::Error;
-
auto dir = fs::path(POH_COUNTER_PERSIST_PATH).parent_path();
fs::create_directories(dir);
- sd_event* event = nullptr;
- auto r = sd_event_default(&event);
- if (r < 0)
- {
- log<level::ERR>("Error creating a default sd_event handler");
- throw;
- }
-
- phosphor::state::manager::EventPtr eventP{event};
- event = nullptr;
-
- auto callback = [&]() {
- if (ChassisInherit::currentPowerState() == PowerState::On)
- {
- pOHCounter(pOHCounter() + 1);
- }
- };
-
try
{
- timer = std::make_unique<phosphor::state::manager::Timer>(
- eventP, callback, std::chrono::seconds(POH::hour),
- phosphor::state::manager::timer::ON);
- bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
- r = sd_event_loop(eventP.get());
- if (r < 0)
- {
- log<level::ERR>("Error occurred during the sd_event_loop",
- entry("RC=%d", r));
- elog<InternalFailure>();
- }
+ auto event = sdeventplus::Event::get_default();
+ bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
+ event.loop();
}
- catch (InternalFailure& e)
+ catch (const sdeventplus::SdEventError& e)
{
+ log<level::ERR>("Error occurred during the sdeventplus loop",
+ entry("ERROR=%s", e.what()));
phosphor::logging::commit<InternalFailure>();
}
}