Fold *GPIOHandler() functions into requestGPIOEvents()

They're essentially all just boilerplate verbosity at this point; by
having the generic code do the re-arming of the handler callback and
determine the state of the line and pass it in, they're no longer needed
at all.

Signed-off-by: Zev Weiss <zev@bewilderbeest.net>
Change-Id: Ib5163fd3525b37e61112b9bb0547e4f628fa6f0e
diff --git a/power-control-x86/src/power_control.cpp b/power-control-x86/src/power_control.cpp
index 032d6f5..c24fe92 100644
--- a/power-control-x86/src/power_control.cpp
+++ b/power-control-x86/src/power_control.cpp
@@ -1061,8 +1061,33 @@
         "xyz.openbmc_project.Common.ACBoot", "ACBoot");
 }
 
+static void waitForGPIOEvent(const std::string& name,
+                             const std::function<void(bool)>& eventHandler,
+                             gpiod::line& line,
+                             boost::asio::posix::stream_descriptor& event)
+{
+    event.async_wait(
+        boost::asio::posix::stream_descriptor::wait_read,
+        [&name, eventHandler, &line,
+         &event](const boost::system::error_code ec) {
+            if (ec)
+            {
+                std::string errMsg =
+                    name + " fd handler error: " + ec.message();
+                phosphor::logging::log<phosphor::logging::level::ERR>(
+                    errMsg.c_str());
+                // TODO: throw here to force power-control to restart?
+                return;
+            }
+            gpiod::line_event line_event = line.event_read();
+            eventHandler(line_event.event_type ==
+                         gpiod::line_event::RISING_EDGE);
+            waitForGPIOEvent(name, eventHandler, line, event);
+        });
+}
+
 static bool requestGPIOEvents(
-    const std::string& name, const std::function<void()>& handler,
+    const std::string& name, const std::function<void(bool)>& handler,
     gpiod::line& gpioLine,
     boost::asio::posix::stream_descriptor& gpioEventDescriptor)
 {
@@ -1097,20 +1122,7 @@
 
     gpioEventDescriptor.assign(gpioLineFd);
 
-    gpioEventDescriptor.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [&name, handler](const boost::system::error_code ec) {
-            if (ec)
-            {
-                std::string errMsg =
-                    name + " fd handler error: " + ec.message();
-                phosphor::logging::log<phosphor::logging::level::ERR>(
-                    errMsg.c_str());
-                // TODO: throw here to force power-control to restart?
-                return;
-            }
-            handler();
-        });
+    waitForGPIOEvent(name, handler, gpioLine, gpioEventDescriptor);
     return true;
 }
 
@@ -1999,28 +2011,6 @@
     sendPowerControlEvent(powerControlEvent);
 }
 
-static void psPowerOKGPIOHandler()
-{
-    gpiod::line_event gpioLineEvent = psPowerOKLine.event_read();
-
-    psPowerOKHandler(gpioLineEvent.event_type ==
-                     gpiod::line_event::RISING_EDGE);
-
-    psPowerOKEvent.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [](const boost::system::error_code ec) {
-            if (ec)
-            {
-                std::string errMsg =
-                    "power supply power OK handler error: " + ec.message();
-                phosphor::logging::log<phosphor::logging::level::ERR>(
-                    errMsg.c_str());
-                return;
-            }
-            psPowerOKGPIOHandler();
-        });
-}
-
 static void sioPowerGoodHandler(bool state)
 {
     Event powerControlEvent =
@@ -2028,28 +2018,6 @@
     sendPowerControlEvent(powerControlEvent);
 }
 
-static void sioPowerGoodGPIOHandler()
-{
-    gpiod::line_event gpioLineEvent = sioPowerGoodLine.event_read();
-
-    sioPowerGoodHandler(gpioLineEvent.event_type ==
-                        gpiod::line_event::RISING_EDGE);
-
-    sioPowerGoodEvent.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [](const boost::system::error_code ec) {
-            if (ec)
-            {
-                std::string errMsg =
-                    "SIO power good handler error: " + ec.message();
-                phosphor::logging::log<phosphor::logging::level::ERR>(
-                    errMsg.c_str());
-                return;
-            }
-            sioPowerGoodGPIOHandler();
-        });
-}
-
 static void sioOnControlHandler(bool state)
 {
     std::string logMsg =
@@ -2057,54 +2025,12 @@
     phosphor::logging::log<phosphor::logging::level::INFO>(logMsg.c_str());
 }
 
-static void sioOnControlGPIOHandler()
-{
-    gpiod::line_event gpioLineEvent = sioOnControlLine.event_read();
-
-    sioOnControlHandler(gpioLineEvent.event_type ==
-                        gpiod::line_event::RISING_EDGE);
-
-    sioOnControlEvent.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [](const boost::system::error_code ec) {
-            if (ec)
-            {
-                std::string errMsg =
-                    "SIO ONCONTROL handler error: " + ec.message();
-                phosphor::logging::log<phosphor::logging::level::ERR>(
-                    errMsg.c_str());
-                return;
-            }
-            sioOnControlGPIOHandler();
-        });
-}
-
 static void sioS5Handler(bool state)
 {
     Event powerControlEvent = state ? Event::sioS5DeAssert : Event::sioS5Assert;
     sendPowerControlEvent(powerControlEvent);
 }
 
-static void sioS5GPIOHandler()
-{
-    gpiod::line_event gpioLineEvent = sioS5Line.event_read();
-
-    sioS5Handler(gpioLineEvent.event_type == gpiod::line_event::RISING_EDGE);
-
-    sioS5Event.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [](const boost::system::error_code ec) {
-            if (ec)
-            {
-                std::string errMsg = "SIO S5 handler error: " + ec.message();
-                phosphor::logging::log<phosphor::logging::level::ERR>(
-                    errMsg.c_str());
-                return;
-            }
-            sioS5GPIOHandler();
-        });
-}
-
 static void powerButtonHandler(bool state)
 {
     powerButtonIface->set_property("ButtonPressed", !state);
@@ -2124,28 +2050,6 @@
     }
 }
 
-static void powerButtonGPIOHandler()
-{
-    gpiod::line_event gpioLineEvent = powerButtonLine.event_read();
-
-    powerButtonHandler(gpioLineEvent.event_type ==
-                       gpiod::line_event::RISING_EDGE);
-
-    powerButtonEvent.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [](const boost::system::error_code ec) {
-            if (ec)
-            {
-                std::string errMsg =
-                    "power button handler error: " + ec.message();
-                phosphor::logging::log<phosphor::logging::level::ERR>(
-                    errMsg.c_str());
-                return;
-            }
-            powerButtonGPIOHandler();
-        });
-}
-
 static void resetButtonHandler(bool state)
 {
     resetButtonIface->set_property("ButtonPressed", !state);
@@ -2165,28 +2069,6 @@
     }
 }
 
-static void resetButtonGPIOHandler()
-{
-    gpiod::line_event gpioLineEvent = resetButtonLine.event_read();
-
-    resetButtonHandler(gpioLineEvent.event_type ==
-                       gpiod::line_event::RISING_EDGE);
-
-    resetButtonEvent.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [](const boost::system::error_code ec) {
-            if (ec)
-            {
-                std::string errMsg =
-                    "reset button handler error: " + ec.message();
-                phosphor::logging::log<phosphor::logging::level::ERR>(
-                    errMsg.c_str());
-                return;
-            }
-            resetButtonGPIOHandler();
-        });
-}
-
 #ifdef CHASSIS_SYSTEM_RESET
 static constexpr auto systemdBusname = "org.freedesktop.systemd1";
 static constexpr auto systemdPath = "/org/freedesktop/systemd1";
@@ -2348,53 +2230,11 @@
     }
 }
 
-static void nmiButtonGPIOHandler()
-{
-    gpiod::line_event gpioLineEvent = nmiButtonLine.event_read();
-
-    nmiButtonHandler(gpioLineEvent.event_type ==
-                     gpiod::line_event::RISING_EDGE);
-
-    nmiButtonEvent.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [](const boost::system::error_code ec) {
-            if (ec)
-            {
-                std::string errMsg =
-                    "NMI button handler error: " + ec.message();
-                phosphor::logging::log<phosphor::logging::level::ERR>(
-                    errMsg.c_str());
-                return;
-            }
-            nmiButtonGPIOHandler();
-        });
-}
-
 static void idButtonHandler(bool state)
 {
     idButtonIface->set_property("ButtonPressed", !state);
 }
 
-static void idButtonGPIOHandler()
-{
-    gpiod::line_event gpioLineEvent = idButtonLine.event_read();
-
-    idButtonHandler(gpioLineEvent.event_type == gpiod::line_event::RISING_EDGE);
-
-    idButtonEvent.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [](const boost::system::error_code& ec) {
-            if (ec)
-            {
-                std::string errMsg = "ID button handler error: " + ec.message();
-                phosphor::logging::log<phosphor::logging::level::ERR>(
-                    errMsg.c_str());
-                return;
-            }
-            idButtonGPIOHandler();
-        });
-}
-
 static void pltRstHandler(bool pltRst)
 {
     if (pltRst)
@@ -2461,28 +2301,6 @@
     }
 }
 
-static void postCompleteGPIOHandler()
-{
-    gpiod::line_event gpioLineEvent = postCompleteLine.event_read();
-
-    postCompleteHandler(gpioLineEvent.event_type ==
-                        gpiod::line_event::RISING_EDGE);
-
-    postCompleteEvent.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [](const boost::system::error_code ec) {
-            if (ec)
-            {
-                std::string errMsg =
-                    "POST complete handler error: " + ec.message();
-                phosphor::logging::log<phosphor::logging::level::ERR>(
-                    errMsg.c_str());
-                return;
-            }
-            postCompleteGPIOHandler();
-        });
-}
-
 static int loadConfigValues()
 {
     const std::string configFilePath =
@@ -2784,7 +2602,7 @@
     // Request PS_PWROK GPIO events
     if (powerOkConfig.type == ConfigType::GPIO)
     {
-        if (!requestGPIOEvents(powerOkConfig.lineName, psPowerOKGPIOHandler,
+        if (!requestGPIOEvents(powerOkConfig.lineName, psPowerOKHandler,
                                psPowerOKLine, psPowerOKEvent))
         {
             return -1;
@@ -2809,7 +2627,7 @@
         if (sioPwrGoodConfig.type == ConfigType::GPIO)
         {
             if (!requestGPIOEvents(sioPwrGoodConfig.lineName,
-                                   sioPowerGoodGPIOHandler, sioPowerGoodLine,
+                                   sioPowerGoodHandler, sioPowerGoodLine,
                                    sioPowerGoodEvent))
             {
                 return -1;
@@ -2832,7 +2650,7 @@
         if (sioOnControlConfig.type == ConfigType::GPIO)
         {
             if (!requestGPIOEvents(sioOnControlConfig.lineName,
-                                   sioOnControlGPIOHandler, sioOnControlLine,
+                                   sioOnControlHandler, sioOnControlLine,
                                    sioOnControlEvent))
             {
                 return -1;
@@ -2855,7 +2673,7 @@
         // Request SIO_S5 GPIO events
         if (sioS5Config.type == ConfigType::GPIO)
         {
-            if (!requestGPIOEvents(sioS5Config.lineName, sioS5GPIOHandler,
+            if (!requestGPIOEvents(sioS5Config.lineName, sioS5Handler,
                                    sioS5Line, sioS5Event))
             {
                 return -1;
@@ -2877,9 +2695,8 @@
     // Request POWER_BUTTON GPIO events
     if (powerButtonConfig.type == ConfigType::GPIO)
     {
-        if (!requestGPIOEvents(powerButtonConfig.lineName,
-                               powerButtonGPIOHandler, powerButtonLine,
-                               powerButtonEvent))
+        if (!requestGPIOEvents(powerButtonConfig.lineName, powerButtonHandler,
+                               powerButtonLine, powerButtonEvent))
         {
             return -1;
         }
@@ -2894,9 +2711,8 @@
     // Request RESET_BUTTON GPIO events
     if (resetButtonConfig.type == ConfigType::GPIO)
     {
-        if (!requestGPIOEvents(resetButtonConfig.lineName,
-                               resetButtonGPIOHandler, resetButtonLine,
-                               resetButtonEvent))
+        if (!requestGPIOEvents(resetButtonConfig.lineName, resetButtonHandler,
+                               resetButtonLine, resetButtonEvent))
         {
             return -1;
         }
@@ -2913,7 +2729,7 @@
     {
         if (!nmiButtonConfig.lineName.empty())
         {
-            requestGPIOEvents(nmiButtonConfig.lineName, nmiButtonGPIOHandler,
+            requestGPIOEvents(nmiButtonConfig.lineName, nmiButtonHandler,
                               nmiButtonLine, nmiButtonEvent);
         }
     }
@@ -2928,7 +2744,7 @@
     {
         if (!idButtonConfig.lineName.empty())
         {
-            requestGPIOEvents(idButtonConfig.lineName, idButtonGPIOHandler,
+            requestGPIOEvents(idButtonConfig.lineName, idButtonHandler,
                               idButtonLine, idButtonEvent);
         }
     }
@@ -2949,9 +2765,8 @@
     // Request POST_COMPLETE GPIO events
     if (postCompleteConfig.type == ConfigType::GPIO)
     {
-        if (!requestGPIOEvents(postCompleteConfig.lineName,
-                               postCompleteGPIOHandler, postCompleteLine,
-                               postCompleteEvent))
+        if (!requestGPIOEvents(postCompleteConfig.lineName, postCompleteHandler,
+                               postCompleteLine, postCompleteEvent))
         {
             return -1;
         }