psu: Update to use Power Supply Manager class

Create a power supply manager class that will be used create a list of
power supplies to manage.

Change-Id: I4c392f7060fbd06dbe6a9417810ed911b3ea689a
Signed-off-by: Brandon Wyman <bjwyman@gmail.com>
diff --git a/phosphor-power-supply/main.cpp b/phosphor-power-supply/main.cpp
index bbbd459..d19a254 100644
--- a/phosphor-power-supply/main.cpp
+++ b/phosphor-power-supply/main.cpp
@@ -13,8 +13,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#include "psu_manager.hpp"
+
 #include <CLI/CLI.hpp>
 #include <phosphor-logging/log.hpp>
+#include <sdbusplus/bus.hpp>
+#include <sdeventplus/event.hpp>
 
 #include <filesystem>
 
@@ -42,5 +46,15 @@
         return -1;
     }
 
-    return 0;
+    auto bus = sdbusplus::bus::new_default();
+    auto event = sdeventplus::Event::get_default();
+
+    // Attach the event object to the bus object so we can
+    // handle both sd_events (for the timers) and dbus signals.
+    bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
+
+    // TODO: Should get polling interval from JSON file.
+    auto pollInterval = std::chrono::milliseconds(1000);
+
+    return phosphor::power::manager::PSUManager(bus, event, pollInterval).run();
 }
diff --git a/phosphor-power-supply/meson.build b/phosphor-power-supply/meson.build
index 1bb79de..c1fcb1f 100644
--- a/phosphor-power-supply/meson.build
+++ b/phosphor-power-supply/meson.build
@@ -4,5 +4,6 @@
     'main.cpp',
     dependencies: [
         sdbusplus,
+        sdeventplus,
     ],
     install: true)
diff --git a/phosphor-power-supply/psu_manager.hpp b/phosphor-power-supply/psu_manager.hpp
new file mode 100644
index 0000000..7e9daf6
--- /dev/null
+++ b/phosphor-power-supply/psu_manager.hpp
@@ -0,0 +1,126 @@
+#pragma once
+
+#include <sdbusplus/bus/match.hpp>
+#include <sdeventplus/event.hpp>
+#include <sdeventplus/utility/timer.hpp>
+
+namespace phosphor
+{
+namespace power
+{
+namespace manager
+{
+
+/**
+ * @class PSUManager
+ *
+ * This class will create an object used to manage and monitor a list of power
+ * supply devices.
+ */
+class PSUManager
+{
+  public:
+    PSUManager() = delete;
+    ~PSUManager() = default;
+    PSUManager(const PSUManager&) = delete;
+    PSUManager& operator=(const PSUManager&) = delete;
+    PSUManager(PSUManager&&) = delete;
+    PSUManager& operator=(PSUManager&&) = delete;
+
+    /**
+     * Constructor
+     *
+     * @param[in] bus - D-Bus bus object
+     * @param[in] e - event object
+     * @param[in] i - polling interval in milliseconds
+     */
+    PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e,
+               std::chrono::milliseconds i) :
+        bus(bus),
+        timer(e, std::bind(&PSUManager::analyze, this), i)
+    {
+    }
+
+    /**
+     * Initializes the manager.
+     *
+     * Get current BMC state, ...
+     */
+    void initialize()
+    {
+    }
+
+    /**
+     * Starts the timer to start monitoring the list of devices.
+     */
+    int run()
+    {
+        return timer.get_event().loop();
+    }
+
+    /**
+     * This function will be called in various situations in order to clear
+     * any fault status bits that may have been set, in order to start over
+     * with a clean state. Presence changes and power state changes will want
+     * to clear any faults logged.
+     */
+    void clearFaults()
+    {
+    }
+
+  private:
+    /**
+     * The D-Bus object
+     */
+    sdbusplus::bus::bus& bus;
+
+    /**
+     * The timer that runs to periodically check the power supplies.
+     */
+    sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
+
+    /**
+     * Analyze the status of each of the power supplies.
+     */
+    void analyze()
+    {
+    }
+
+    /** @brief True if the power is on. */
+    bool powerOn = false;
+
+    /** @brief Used to subscribe to D-Bus power on state changes */
+    std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch;
+
+    /**
+     * @brief Updates the poweredOn status by querying D-Bus
+     *
+     * The D-Bus property for the system power state will be read to determine
+     * if the system is powered on or not.
+     */
+    void updatePowerState();
+
+    /**
+     * @brief Callback for power state property changes
+     *
+     * Process changes to the powered on state property for the system.
+     *
+     * @param[in] msg - Data associated with the power state signal
+     */
+    void powerStateChanged(sdbusplus::message::message& msg);
+
+    /**
+     * @brief Adds properties to the inventory.
+     *
+     * Reads the values from the devices and writes them to the associated
+     * power supply D-Bus inventory objects.
+     *
+     * This needs to be done on startup, and each time the presence state
+     * changes.
+     */
+    void updateInventory();
+};
+
+} // namespace manager
+} // namespace power
+} // namespace phosphor