psu-ng: Power supply class updates for input history

Update the meson files to include the record_manager with the
phosphor-psu-monitor application.

Since we do not want to blindly enable input history for all power
supplies, base the enablement of the feature off of the driver name.
Change the PowerSupply class to require the driver name be passed in,
and pass that down via the PSUManager during the configuration
determination.

Add a server manager to the PSUManager to handle the INPUT HISTORY data
that will be under /org/open_power/sensors.

The INPUT_HISTORY command is handled via a sysfs file in binary format,
so add in a readBinary() base function to allow for mock testing.

Change-Id: Iea163892d5482e6f2dacacfbfa746f605af52ed5
Signed-off-by: Brandon Wyman <bjwyman@gmail.com>
diff --git a/phosphor-power-supply/power_supply.hpp b/phosphor-power-supply/power_supply.hpp
index 6f1044c..b81599b 100644
--- a/phosphor-power-supply/power_supply.hpp
+++ b/phosphor-power-supply/power_supply.hpp
@@ -1,6 +1,9 @@
 #pragma once
 
+#include "average.hpp"
+#include "maximum.hpp"
 #include "pmbus.hpp"
+#include "record_manager.hpp"
 #include "types.hpp"
 #include "util.hpp"
 #include "utility.hpp"
@@ -55,12 +58,13 @@
      * @param[in] invpath - String for inventory path to use
      * @param[in] i2cbus - The bus number this power supply is on
      * @param[in] i2caddr - The 16-bit I2C address of the power supply
+     * @param[in] driver - i2c driver name for power supply
      * @param[in] gpioLineName - The gpio-line-name to read for presence. See
      * https://github.com/openbmc/docs/blob/master/designs/device-tree-gpio-naming.md
      */
     PowerSupply(sdbusplus::bus::bus& bus, const std::string& invpath,
                 std::uint8_t i2cbus, const std::uint16_t i2caddr,
-                const std::string& gpioLineName);
+                const std::string& driver, const std::string& gpioLineName);
 
     phosphor::pmbus::PMBusBase& getPMBus()
     {
@@ -448,6 +452,42 @@
      */
     void checkAvailability();
 
+    /**
+     * @brief Setup for power supply input history.
+     *
+     * This will setup the variables and interfaces needed to get the power
+     * supply input history data over to D-Bus. The only known support for this
+     * at this time is the INPUT_HISTORY command implemented by the IBM Common
+     * Form Factor Power Suppplies (ibm-cffps). The INPUT_HISTORY command for
+     * ibm-cffps is implemented via a manufacturing specific PMBus command.
+     */
+    void setupInputHistory();
+
+    /**
+     * @brief Returns true if this power supply has input history (supported).
+     */
+    bool hasInputHistory() const
+    {
+        return inputHistorySupported;
+    }
+
+    /**
+     * @brief Returns the number of input history records
+     *
+     * PowerSupply wrapper to getNumRecords() from RecordManager.
+     */
+    size_t getNumInputHistoryRecords() const
+    {
+        if (recordManager)
+        {
+            return recordManager->getNumRecords();
+        }
+        else
+        {
+            return 0;
+        }
+    }
+
   private:
     /** @brief systemd bus member */
     sdbusplus::bus::bus& bus;
@@ -794,6 +834,49 @@
      * @param[in]  msg - Data associated with Present add signal
      **/
     void inventoryAdded(sdbusplus::message::message& msg);
+
+    /**
+     * @brief Reads the most recent input history record from the power supply
+     * and updates the average and maximum properties in D-Bus if there is a new
+     * reading available.
+     *
+     * This will still run every time analyze() is called so code can post new
+     * data as soon as possible and the timestamp will more accurately reflect
+     * the correct time.
+     *
+     * D-Bus is only updated if there is a change and the oldest record will be
+     * pruned if the property already contains the max number of records.
+     */
+    void updateHistory();
+
+    /**
+     * @brief Set to true if INPUT_HISTORY command supported.
+     *
+     * Not all power supplies will support the INPUT_HISTORY command. The IBM
+     * Common Form Factor power supplies do support this command.
+     */
+    bool inputHistorySupported{false};
+
+    /**
+     * @brief Class that manages the input power history records.
+     **/
+    std::unique_ptr<history::RecordManager> recordManager;
+
+    /**
+     * @brief The D-Bus object for the average input power history
+     **/
+    std::unique_ptr<history::Average> average;
+
+    /**
+     * @brief The D-Bus object for the maximum input power history
+     **/
+    std::unique_ptr<history::Maximum> maximum;
+
+    /**
+     * @brief The base D-Bus object path to use for the average and maximum
+     * objects.
+     **/
+    std::string historyObjectPath;
 };
 
 } // namespace phosphor::power::psu