pinhole: move gpio function to utils

The function to read a GPIO is needed elsewhere in later reviews so move
to the utility file.

Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
Change-Id: I994d4a912c0abe9cae6cb02d22bf5be09581d332
diff --git a/chassis_state_manager.cpp b/chassis_state_manager.cpp
index fd2ef22..d23f348 100644
--- a/chassis_state_manager.cpp
+++ b/chassis_state_manager.cpp
@@ -2,11 +2,10 @@
 
 #include "chassis_state_manager.hpp"
 
+#include "utils.hpp"
 #include "xyz/openbmc_project/Common/error.hpp"
 #include "xyz/openbmc_project/State/Shutdown/Power/error.hpp"
 
-#include <gpiod.h>
-
 #include <cereal/archives/json.hpp>
 #include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/lg2.hpp>
@@ -442,36 +441,21 @@
 {
     bool regulatorFault = false;
 
-    // find standby voltage regulator fault via gpio
-    gpiod_line* line = gpiod_line_find("regulator-standby-faulted");
+    // find standby voltage regulator fault via gpiog
 
-    if (nullptr != line)
+    auto gpioval = utils::getGpioValue("regulator-standby-faulted");
+
+    if (-1 == gpioval)
     {
-        // take ownership of gpio
-        if (0 != gpiod_line_request_input(line, "chassis"))
-        {
-            error("Failed request for regulator-standby-faulted GPIO");
-        }
-        else
-        {
-            // get gpio value
-            auto gpioval = gpiod_line_get_value(line);
-
-            // release ownership of gpio
-            gpiod_line_close_chip(line);
-
-            if (-1 == gpioval)
-            {
-                error("Failed reading regulator-standby-faulted GPIO");
-            }
-
-            if (1 == gpioval)
-            {
-                info("Detected standby voltage regulator fault");
-                regulatorFault = true;
-            }
-        }
+        error("Failed reading regulator-standby-faulted GPIO");
     }
+
+    if (1 == gpioval)
+    {
+        info("Detected standby voltage regulator fault");
+        regulatorFault = true;
+    }
+
     return regulatorFault;
 }
 
diff --git a/meson.build b/meson.build
index 149a28f..8bafd18 100644
--- a/meson.build
+++ b/meson.build
@@ -100,6 +100,7 @@
 executable('phosphor-chassis-state-manager',
             'chassis_state_manager.cpp',
             'chassis_state_manager_main.cpp',
+            'utils.cpp',
             dependencies: [
             sdbusplus, sdeventplus, phosphorlogging,
             phosphordbusinterfaces, cppfs, libgpiod
@@ -145,7 +146,7 @@
             'scheduled_host_transition.cpp',
             'utils.cpp',
             dependencies: [
-            sdbusplus, sdeventplus, phosphorlogging
+            sdbusplus, sdeventplus, phosphorlogging, libgpiod
             ],
     implicit_include_directories: true,
     install: true
@@ -221,7 +222,7 @@
           'scheduled_host_transition.cpp',
           'utils.cpp',
           dependencies: [
-              gtest, gmock, sdbusplus, sdeventplus, phosphorlogging,
+              gtest, gmock, sdbusplus, sdeventplus, phosphorlogging, libgpiod
           ],
           implicit_include_directories: true,
           include_directories: '../'
diff --git a/utils.cpp b/utils.cpp
index 6dc7754..8e0e7ed 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -1,5 +1,7 @@
 #include "utils.hpp"
 
+#include <gpiod.h>
+
 #include <phosphor-logging/lg2.hpp>
 
 namespace phosphor
@@ -69,6 +71,31 @@
     return;
 }
 
+int getGpioValue(const std::string& gpioName)
+{
+
+    int gpioval = -1;
+    gpiod_line* line = gpiod_line_find(gpioName.c_str());
+
+    if (nullptr != line)
+    {
+        // take ownership of gpio
+        if (0 != gpiod_line_request_input(line, "state-manager"))
+        {
+            error("Failed request for {GPIO_NAME} GPIO", "GPIO_NAME", gpioName);
+        }
+        else
+        {
+            // get gpio value
+            gpioval = gpiod_line_get_value(line);
+
+            // release ownership of gpio
+            gpiod_line_close_chip(line);
+        }
+    }
+    return gpioval;
+}
+
 } // namespace utils
 } // namespace manager
 } // namespace state
diff --git a/utils.hpp b/utils.hpp
index db7b361..0127206 100644
--- a/utils.hpp
+++ b/utils.hpp
@@ -34,7 +34,15 @@
                  const std::string& interface, const std::string& property,
                  const std::string& value);
 
+/** @brief Return the value of the input GPIO
+ *
+ * @param[in] gpioName          - The name of the GPIO to read
+ *
+ *  * @return The value of the gpio (0 or 1) or -1 on error
+ */
+int getGpioValue(const std::string& gpioName);
+
 } // namespace utils
 } // namespace manager
 } // namespace state
-} // namespace phosphor
\ No newline at end of file
+} // namespace phosphor