pseq: Create Rail class

Create new Rail class for the phosphor-power-sequencer application.  The
class contains the properties from the updated JSON config file format.

Create gtests to test all new code.

Change-Id: Ia55fe3fcc85345deefc1d1987ea1a8421baf50c4
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
diff --git a/phosphor-power-sequencer/meson.build b/phosphor-power-sequencer/meson.build
index 4f5bcf9..e6a3770 100644
--- a/phosphor-power-sequencer/meson.build
+++ b/phosphor-power-sequencer/meson.build
@@ -4,3 +4,6 @@
 
 subdir('src')
 
+if get_option('tests').allowed()
+    subdir('test')
+endif
diff --git a/phosphor-power-sequencer/src/rail.hpp b/phosphor-power-sequencer/src/rail.hpp
new file mode 100644
index 0000000..5445449
--- /dev/null
+++ b/phosphor-power-sequencer/src/rail.hpp
@@ -0,0 +1,209 @@
+/**
+ * Copyright © 2024 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.
+ */
+#pragma once
+
+#include <cstdint>
+#include <optional>
+#include <stdexcept>
+#include <string>
+
+namespace phosphor::power::sequencer
+{
+
+/**
+ * @struct GPIO
+ *
+ * General Purpose Input/Output (GPIO) that can be read to obtain the pgood
+ * status of a voltage rail.
+ */
+struct GPIO
+{
+    /**
+     * The libgpiod line offset of the GPIO.
+     */
+    unsigned int line{0};
+
+    /**
+     * Specifies whether the GPIO is active low.
+     *
+     * If true, the GPIO value 0 indicates a true pgood status. If false, the
+     * GPIO value 1 indicates a true pgood status.
+     */
+    bool activeLow{false};
+};
+
+/**
+ * @class Rail
+ *
+ * A voltage rail that is enabled or monitored by the power sequencer device.
+ */
+class Rail
+{
+  public:
+    // Specify which compiler-generated methods we want
+    Rail() = delete;
+    Rail(const Rail&) = delete;
+    Rail(Rail&&) = delete;
+    Rail& operator=(const Rail&) = delete;
+    Rail& operator=(Rail&&) = delete;
+    ~Rail() = default;
+
+    /**
+     * Constructor.
+     *
+     * Throws an exception if any of the input parameters are invalid.
+     *
+     * @param name Unique name for the rail
+     * @param presence Optional D-Bus inventory path of a system component which
+     *                 must be present in order for the rail to be present
+     * @param page Optional PMBus PAGE number of the rail.  Required if
+     *             checkStatusVout or compareVoltageToLimits is true.
+     * @param checkStatusVout Specifies whether to check the value of the PMBus
+     *                        STATUS_VOUT command when determining the pgood
+     *                        status of the rail
+     * @param compareVoltageToLimits Specifies whether to compare the output
+     *                               voltage to the undervoltage and
+     *                               overvoltage limits when determining the
+     *                               pgood status of the rail
+     * @param gpio Optional GPIO to read to determine the pgood status of the
+     *             rail
+     */
+    explicit Rail(const std::string& name,
+                  const std::optional<std::string>& presence,
+                  const std::optional<uint8_t>& page, bool checkStatusVout,
+                  bool compareVoltageToLimits,
+                  const std::optional<GPIO>& gpio) :
+        name{name},
+        presence{presence}, page{page}, checkStatusVout{checkStatusVout},
+        compareVoltageToLimits{compareVoltageToLimits}, gpio{gpio}
+    {
+        // If checking STATUS_VOUT or output voltage, verify PAGE was specified
+        if ((checkStatusVout || compareVoltageToLimits) && !page.has_value())
+        {
+            throw std::invalid_argument{"PMBus PAGE is required"};
+        }
+    }
+
+    /**
+     * Returns the unique name for the rail.
+     *
+     * @return rail name
+     */
+    const std::string& getName() const
+    {
+        return name;
+    }
+
+    /**
+     * Returns the D-Bus inventory path of a system component which must be
+     * present in order for the rail to be present.
+     *
+     * @return inventory path for presence detection
+     */
+    const std::optional<std::string>& getPresence() const
+    {
+        return presence;
+    }
+
+    /**
+     * Returns the PMBus PAGE number of the rail.
+     *
+     * @return PAGE number for rail
+     */
+    const std::optional<uint8_t>& getPage() const
+    {
+        return page;
+    }
+
+    /**
+     * Returns whether the value of the PMBus STATUS_VOUT command is checked
+     * when determining the pgood status of the rail.
+     *
+     * @return true if STATUS_VOUT is checked, false otherwise
+     */
+    bool getCheckStatusVout() const
+    {
+        return checkStatusVout;
+    }
+
+    /**
+     * Returns whether the output voltage should be compared to the undervoltage
+     * and overvoltage limits when determining the pgood status of the rail.
+     *
+     * @return true if output voltage is compared to limits, false otherwise
+     */
+    bool getCompareVoltageToLimits() const
+    {
+        return compareVoltageToLimits;
+    }
+
+    /**
+     * Returns the GPIO to read to determine the pgood status of the rail.
+     *
+     * @return GPIO
+     */
+    const std::optional<GPIO>& getGPIO() const
+    {
+        return gpio;
+    }
+
+  private:
+    /**
+     * Unique name for the rail.
+     */
+    std::string name{};
+
+    /**
+     * D-Bus inventory path of a system component which must be present in order
+     * for the rail to be present.
+     *
+     * If not specified, the rail is assumed to always be present.
+     */
+    std::optional<std::string> presence{};
+
+    /**
+     * PMBus PAGE number of the rail.
+     */
+    std::optional<uint8_t> page{};
+
+    /**
+     * Specifies whether to check the value of the PMBus STATUS_VOUT command
+     * when determining the pgood status of the rail.
+     *
+     * If one of the error bits is set in STATUS_VOUT, the rail pgood will be
+     * considered false.
+     */
+    bool checkStatusVout{false};
+
+    /**
+     * Specifies whether to compare the output voltage to the undervoltage and
+     * overvoltage limits when determining the pgood status of the rail.
+     *
+     * If the output voltage is beyond those limits, the rail pgood will be
+     * considered false.
+     *
+     * Uses the values of the PMBus READ_VOUT, VOUT_UV_FAULT_LIMIT, and
+     * VOUT_OV_FAULT_LIMIT commands.
+     */
+    bool compareVoltageToLimits{false};
+
+    /**
+     * GPIO to read to determine the pgood status of the rail.
+     */
+    std::optional<GPIO> gpio{};
+};
+
+} // namespace phosphor::power::sequencer
diff --git a/phosphor-power-sequencer/test/meson.build b/phosphor-power-sequencer/test/meson.build
new file mode 100644
index 0000000..ab03041
--- /dev/null
+++ b/phosphor-power-sequencer/test/meson.build
@@ -0,0 +1,16 @@
+test('phosphor-power-sequencer-tests',
+     executable('phosphor-power-sequencer-tests',
+                'rail_tests.cpp',
+                dependencies: [
+                    gtest,
+                    nlohmann_json_dep
+                ],
+                link_args: dynamic_linker,
+                build_rpath: get_option('oe-sdk').allowed() ? rpath : '',
+                implicit_include_directories: false,
+                include_directories: [
+                    '.',
+                    '../src'
+                ]
+     )
+)
diff --git a/phosphor-power-sequencer/test/rail_tests.cpp b/phosphor-power-sequencer/test/rail_tests.cpp
new file mode 100644
index 0000000..3caf573
--- /dev/null
+++ b/phosphor-power-sequencer/test/rail_tests.cpp
@@ -0,0 +1,240 @@
+/**
+ * Copyright © 2024 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 "rail.hpp"
+
+#include <cstdint>
+#include <optional>
+#include <string>
+
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::sequencer;
+
+TEST(GPIOTests, Initialization)
+{
+    // Default initialization
+    {
+        GPIO gpio;
+        EXPECT_EQ(gpio.line, 0);
+        EXPECT_FALSE(gpio.activeLow);
+    }
+
+    // Explicit initialization
+    {
+        GPIO gpio{48, true};
+        EXPECT_EQ(gpio.line, 48);
+        EXPECT_TRUE(gpio.activeLow);
+    }
+}
+
+TEST(RailTests, Constructor)
+{
+    // Test where succeeds: No optional parameters have values
+    {
+        std::string name{"VDD1"};
+        std::optional<std::string> presence{};
+        std::optional<uint8_t> page{};
+        bool checkStatusVout{false};
+        bool compareVoltageToLimits{false};
+        std::optional<GPIO> gpio{};
+        Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits,
+                  gpio};
+
+        EXPECT_EQ(rail.getName(), "VDD1");
+        EXPECT_FALSE(rail.getPresence().has_value());
+        EXPECT_FALSE(rail.getPage().has_value());
+        EXPECT_FALSE(rail.getCheckStatusVout());
+        EXPECT_FALSE(rail.getCompareVoltageToLimits());
+        EXPECT_FALSE(rail.getGPIO().has_value());
+    }
+
+    // Test where succeeds: All optional parameters have values
+    {
+        std::string name{"VCS_CPU1"};
+        std::optional<std::string> presence{
+            "/xyz/openbmc_project/inventory/system/chassis/motherboard/cpu1"};
+        std::optional<uint8_t> page{11};
+        bool checkStatusVout{true};
+        bool compareVoltageToLimits{true};
+        std::optional<GPIO> gpio{GPIO(60, true)};
+        Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits,
+                  gpio};
+
+        EXPECT_EQ(rail.getName(), "VCS_CPU1");
+        EXPECT_TRUE(rail.getPresence().has_value());
+        EXPECT_EQ(
+            rail.getPresence().value(),
+            "/xyz/openbmc_project/inventory/system/chassis/motherboard/cpu1");
+        EXPECT_TRUE(rail.getPage().has_value());
+        EXPECT_EQ(rail.getPage().value(), 11);
+        EXPECT_TRUE(rail.getCheckStatusVout());
+        EXPECT_TRUE(rail.getCompareVoltageToLimits());
+        EXPECT_TRUE(rail.getGPIO().has_value());
+        EXPECT_EQ(rail.getGPIO().value().line, 60);
+        EXPECT_TRUE(rail.getGPIO().value().activeLow);
+    }
+
+    // Test where fails: checkStatusVout is true and page has no value
+    {
+        std::string name{"VDD1"};
+        std::optional<std::string> presence{};
+        std::optional<uint8_t> page{};
+        bool checkStatusVout{true};
+        bool compareVoltageToLimits{false};
+        std::optional<GPIO> gpio{};
+        EXPECT_THROW((Rail{name, presence, page, checkStatusVout,
+                           compareVoltageToLimits, gpio}),
+                     std::invalid_argument);
+    }
+
+    // Test where fails: compareVoltageToLimits is true and page has no value
+    {
+        std::string name{"VDD1"};
+        std::optional<std::string> presence{};
+        std::optional<uint8_t> page{};
+        bool checkStatusVout{false};
+        bool compareVoltageToLimits{true};
+        std::optional<GPIO> gpio{};
+        EXPECT_THROW((Rail{name, presence, page, checkStatusVout,
+                           compareVoltageToLimits, gpio}),
+                     std::invalid_argument);
+    }
+}
+
+TEST(RailTests, GetName)
+{
+    std::string name{"VDD2"};
+    std::optional<std::string> presence{};
+    std::optional<uint8_t> page{};
+    bool checkStatusVout{false};
+    bool compareVoltageToLimits{false};
+    std::optional<GPIO> gpio{};
+    Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits,
+              gpio};
+
+    EXPECT_EQ(rail.getName(), "VDD2");
+}
+
+TEST(RailTests, GetPresence)
+{
+    std::string name{"VDDR2"};
+    std::optional<uint8_t> page{};
+    bool checkStatusVout{false};
+    bool compareVoltageToLimits{false};
+    std::optional<GPIO> gpio{};
+
+    // Test where presence has no value
+    {
+        std::optional<std::string> presence{};
+        Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits,
+                  gpio};
+        EXPECT_FALSE(rail.getPresence().has_value());
+    }
+
+    // Test where presence has a value
+    {
+        std::optional<std::string> presence{
+            "/xyz/openbmc_project/inventory/system/chassis/motherboard/dimm2"};
+        Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits,
+                  gpio};
+        EXPECT_TRUE(rail.getPresence().has_value());
+        EXPECT_EQ(
+            rail.getPresence().value(),
+            "/xyz/openbmc_project/inventory/system/chassis/motherboard/dimm2");
+    }
+}
+
+TEST(RailTests, GetPage)
+{
+    std::string name{"VDD2"};
+    std::optional<std::string> presence{};
+    bool checkStatusVout{false};
+    bool compareVoltageToLimits{false};
+    std::optional<GPIO> gpio{};
+
+    // Test where page has no value
+    {
+        std::optional<uint8_t> page{};
+        Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits,
+                  gpio};
+        EXPECT_FALSE(rail.getPage().has_value());
+    }
+
+    // Test where page has a value
+    {
+        std::optional<uint8_t> page{7};
+        Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits,
+                  gpio};
+        EXPECT_TRUE(rail.getPage().has_value());
+        EXPECT_EQ(rail.getPage().value(), 7);
+    }
+}
+
+TEST(RailTests, GetCheckStatusVout)
+{
+    std::string name{"VDD2"};
+    std::optional<std::string> presence{};
+    std::optional<uint8_t> page{};
+    bool checkStatusVout{false};
+    bool compareVoltageToLimits{false};
+    std::optional<GPIO> gpio{};
+    Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits,
+              gpio};
+
+    EXPECT_FALSE(rail.getCheckStatusVout());
+}
+
+TEST(RailTests, GetCompareVoltageToLimits)
+{
+    std::string name{"VDD2"};
+    std::optional<std::string> presence{};
+    std::optional<uint8_t> page{13};
+    bool checkStatusVout{false};
+    bool compareVoltageToLimits{true};
+    std::optional<GPIO> gpio{};
+    Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits,
+              gpio};
+
+    EXPECT_TRUE(rail.getCompareVoltageToLimits());
+}
+
+TEST(RailTests, GetGPIO)
+{
+    std::string name{"VDD2"};
+    std::optional<std::string> presence{};
+    std::optional<uint8_t> page{};
+    bool checkStatusVout{false};
+    bool compareVoltageToLimits{false};
+
+    // Test where gpio has no value
+    {
+        std::optional<GPIO> gpio{};
+        Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits,
+                  gpio};
+        EXPECT_FALSE(rail.getGPIO().has_value());
+    }
+
+    // Test where gpio has a value
+    {
+        std::optional<GPIO> gpio{GPIO(12, false)};
+        Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits,
+                  gpio};
+        EXPECT_TRUE(rail.getGPIO().has_value());
+        EXPECT_EQ(rail.getGPIO().value().line, 12);
+        EXPECT_FALSE(rail.getGPIO().value().activeLow);
+    }
+}