regs: Add an event timer for monitoring

Create and enable a repeating 1 second timer when monitoring is enabled.
Delete the timer when monitoring is disabled.

Tested:
    Timer created when monitoring is enabled
    Each timer expiration calls bound callback function
    Timer stopped/deleted when monitoring is disabled

Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
Change-Id: I990124fb727bb0a0545ce06f9c878e81b572c7a5
diff --git a/phosphor-regulators/src/main.cpp b/phosphor-regulators/src/main.cpp
index e104ef9..35a3b49 100644
--- a/phosphor-regulators/src/main.cpp
+++ b/phosphor-regulators/src/main.cpp
@@ -27,7 +27,7 @@
     auto event = sdeventplus::Event::get_default();
     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
 
-    regulators::Manager manager(bus);
+    regulators::Manager manager(bus, event);
 
     return event.loop();
 }
diff --git a/phosphor-regulators/src/manager.cpp b/phosphor-regulators/src/manager.cpp
index 77c715a..d7743b7 100644
--- a/phosphor-regulators/src/manager.cpp
+++ b/phosphor-regulators/src/manager.cpp
@@ -18,6 +18,8 @@
 
 #include <sdbusplus/bus.hpp>
 
+#include <chrono>
+
 namespace phosphor
 {
 namespace power
@@ -25,8 +27,8 @@
 namespace regulators
 {
 
-Manager::Manager(sdbusplus::bus::bus& bus) :
-    ManagerObject(bus, objPath, true), bus(bus)
+Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) :
+    ManagerObject(bus, objPath, true), bus(bus), eventLoop(event)
 {
     // TODO get property (IM keyword)
     // call parse json function
@@ -48,14 +50,24 @@
 {
     if (enable)
     {
-        // TODO Enable timer event that will do the monitoring
+        Timer timer(eventLoop, std::bind(&Manager::timerExpired, this));
+        // Set timer as a repeating 1sec timer
+        timer.restart(std::chrono::milliseconds(1000));
+        timers.emplace_back(std::move(timer));
     }
     else
     {
-        // TODO Disable/delete timer event to stop monitoring
+        // Delete all timers to disable monitoring
+        timers.clear();
     }
 }
 
+void Manager::timerExpired()
+{
+    // TODO Analyze, refresh sensor status, and
+    // collect/update telemetry for each regulator
+}
+
 } // namespace regulators
 } // namespace power
 } // namespace phosphor
diff --git a/phosphor-regulators/src/manager.hpp b/phosphor-regulators/src/manager.hpp
index 1c680ce..2b38821 100644
--- a/phosphor-regulators/src/manager.hpp
+++ b/phosphor-regulators/src/manager.hpp
@@ -3,6 +3,8 @@
 #include <interfaces/manager_interface.hpp>
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server/object.hpp>
+#include <sdeventplus/event.hpp>
+#include <sdeventplus/utility/timer.hpp>
 
 namespace phosphor::power::regulators
 {
@@ -10,6 +12,8 @@
 constexpr auto busName = "xyz.openbmc_project.Power.Regulators";
 constexpr auto objPath = "/xyz/openbmc_project/power/regulators/manager";
 
+using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
+
 using ManagerObject = sdbusplus::server::object::object<
     phosphor::power::regulators::interface::ManagerInterface>;
 
@@ -28,8 +32,9 @@
      * Creates a manager over the regulators.
      *
      * @param[in] bus - the dbus bus
+     * @param[in] event - the sdevent event
      */
-    Manager(sdbusplus::bus::bus& bus);
+    Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event);
 
     /**
      * @brief Overridden manager object's configure method
@@ -43,11 +48,26 @@
      */
     void monitor(bool enable) override;
 
+    /**
+     * @brief Timer expired callback function
+     */
+    void timerExpired();
+
   private:
     /**
      * The dbus bus
      */
     sdbusplus::bus::bus& bus;
+
+    /**
+     * Event to loop on
+     */
+    sdeventplus::Event eventLoop;
+
+    /**
+     * List of event timers
+     */
+    std::vector<Timer> timers;
 };
 
 } // namespace phosphor::power::regulators