Deduplicate GPIO & DBus event handling code paths
Each input signal now has a generic core handler function that's invoked
by the respective GPIO and DBus monitoring code instead of each
monitoring path having its own separate copy of the handling code.
Signed-off-by: Zev Weiss <zev@bewilderbeest.net>
Change-Id: I410ca98ef59698a62558af3cb1178cffd5282d07
diff --git a/power-control-x86/src/power_control.cpp b/power-control-x86/src/power_control.cpp
index 47bdd04..032d6f5 100644
--- a/power-control-x86/src/power_control.cpp
+++ b/power-control-x86/src/power_control.cpp
@@ -1992,16 +1992,20 @@
}
}
-static void psPowerOKHandler()
+static void psPowerOKHandler(bool state)
+{
+ Event powerControlEvent =
+ state ? Event::psPowerOKAssert : Event::psPowerOKDeAssert;
+ sendPowerControlEvent(powerControlEvent);
+}
+
+static void psPowerOKGPIOHandler()
{
gpiod::line_event gpioLineEvent = psPowerOKLine.event_read();
- Event powerControlEvent =
- gpioLineEvent.event_type == gpiod::line_event::RISING_EDGE
- ? Event::psPowerOKAssert
- : Event::psPowerOKDeAssert;
+ psPowerOKHandler(gpioLineEvent.event_type ==
+ gpiod::line_event::RISING_EDGE);
- sendPowerControlEvent(powerControlEvent);
psPowerOKEvent.async_wait(
boost::asio::posix::stream_descriptor::wait_read,
[](const boost::system::error_code ec) {
@@ -2013,20 +2017,24 @@
errMsg.c_str());
return;
}
- psPowerOKHandler();
+ psPowerOKGPIOHandler();
});
}
-static void sioPowerGoodHandler()
+static void sioPowerGoodHandler(bool state)
+{
+ Event powerControlEvent =
+ state ? Event::sioPowerGoodAssert : Event::sioPowerGoodDeAssert;
+ sendPowerControlEvent(powerControlEvent);
+}
+
+static void sioPowerGoodGPIOHandler()
{
gpiod::line_event gpioLineEvent = sioPowerGoodLine.event_read();
- Event powerControlEvent =
- gpioLineEvent.event_type == gpiod::line_event::RISING_EDGE
- ? Event::sioPowerGoodAssert
- : Event::sioPowerGoodDeAssert;
+ sioPowerGoodHandler(gpioLineEvent.event_type ==
+ gpiod::line_event::RISING_EDGE);
- sendPowerControlEvent(powerControlEvent);
sioPowerGoodEvent.async_wait(
boost::asio::posix::stream_descriptor::wait_read,
[](const boost::system::error_code ec) {
@@ -2038,19 +2046,24 @@
errMsg.c_str());
return;
}
- sioPowerGoodHandler();
+ sioPowerGoodGPIOHandler();
});
}
-static void sioOnControlHandler()
+static void sioOnControlHandler(bool state)
+{
+ std::string logMsg =
+ "SIO_ONCONTROL value changed: " + std::to_string(state);
+ phosphor::logging::log<phosphor::logging::level::INFO>(logMsg.c_str());
+}
+
+static void sioOnControlGPIOHandler()
{
gpiod::line_event gpioLineEvent = sioOnControlLine.event_read();
- bool sioOnControl =
- gpioLineEvent.event_type == gpiod::line_event::RISING_EDGE;
- std::string logMsg =
- "SIO_ONCONTROL value changed: " + std::to_string(sioOnControl);
- phosphor::logging::log<phosphor::logging::level::INFO>(logMsg.c_str());
+ 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) {
@@ -2062,20 +2075,22 @@
errMsg.c_str());
return;
}
- sioOnControlHandler();
+ sioOnControlGPIOHandler();
});
}
-static void sioS5Handler()
+static void sioS5Handler(bool state)
+{
+ Event powerControlEvent = state ? Event::sioS5DeAssert : Event::sioS5Assert;
+ sendPowerControlEvent(powerControlEvent);
+}
+
+static void sioS5GPIOHandler()
{
gpiod::line_event gpioLineEvent = sioS5Line.event_read();
- Event powerControlEvent =
- gpioLineEvent.event_type == gpiod::line_event::FALLING_EDGE
- ? Event::sioS5Assert
- : Event::sioS5DeAssert;
+ sioS5Handler(gpioLineEvent.event_type == gpiod::line_event::RISING_EDGE);
- sendPowerControlEvent(powerControlEvent);
sioS5Event.async_wait(
boost::asio::posix::stream_descriptor::wait_read,
[](const boost::system::error_code ec) {
@@ -2086,18 +2101,16 @@
errMsg.c_str());
return;
}
- sioS5Handler();
+ sioS5GPIOHandler();
});
}
-static void powerButtonHandler()
+static void powerButtonHandler(bool state)
{
- gpiod::line_event gpioLineEvent = powerButtonLine.event_read();
-
- if (gpioLineEvent.event_type == gpiod::line_event::FALLING_EDGE)
+ powerButtonIface->set_property("ButtonPressed", !state);
+ if (!state)
{
powerButtonPressLog();
- powerButtonIface->set_property("ButtonPressed", true);
if (!powerButtonMask)
{
sendPowerControlEvent(Event::powerButtonPressed);
@@ -2109,10 +2122,15 @@
"power button press masked");
}
}
- else if (gpioLineEvent.event_type == gpiod::line_event::RISING_EDGE)
- {
- powerButtonIface->set_property("ButtonPressed", false);
- }
+}
+
+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) {
@@ -2124,18 +2142,16 @@
errMsg.c_str());
return;
}
- powerButtonHandler();
+ powerButtonGPIOHandler();
});
}
-static void resetButtonHandler()
+static void resetButtonHandler(bool state)
{
- gpiod::line_event gpioLineEvent = resetButtonLine.event_read();
-
- if (gpioLineEvent.event_type == gpiod::line_event::FALLING_EDGE)
+ resetButtonIface->set_property("ButtonPressed", !state);
+ if (!state)
{
resetButtonPressLog();
- resetButtonIface->set_property("ButtonPressed", true);
if (!resetButtonMask)
{
sendPowerControlEvent(Event::resetButtonPressed);
@@ -2147,10 +2163,15 @@
"reset button press masked");
}
}
- else if (gpioLineEvent.event_type == gpiod::line_event::RISING_EDGE)
- {
- resetButtonIface->set_property("ButtonPressed", false);
- }
+}
+
+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) {
@@ -2162,7 +2183,7 @@
errMsg.c_str());
return;
}
- resetButtonHandler();
+ resetButtonGPIOHandler();
});
}
@@ -2309,14 +2330,12 @@
nmiSetEnableProperty(true);
}
-static void nmiButtonHandler()
+static void nmiButtonHandler(bool state)
{
- gpiod::line_event gpioLineEvent = nmiButtonLine.event_read();
-
- if (gpioLineEvent.event_type == gpiod::line_event::FALLING_EDGE)
+ nmiButtonIface->set_property("ButtonPressed", !state);
+ if (!state)
{
nmiButtonPressLog();
- nmiButtonIface->set_property("ButtonPressed", true);
if (nmiButtonMasked)
{
phosphor::logging::log<phosphor::logging::level::INFO>(
@@ -2327,10 +2346,15 @@
setNmiSource();
}
}
- else if (gpioLineEvent.event_type == gpiod::line_event::RISING_EDGE)
- {
- nmiButtonIface->set_property("ButtonPressed", false);
- }
+}
+
+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) {
@@ -2342,22 +2366,21 @@
errMsg.c_str());
return;
}
- nmiButtonHandler();
+ nmiButtonGPIOHandler();
});
}
-static void idButtonHandler()
+static void idButtonHandler(bool state)
+{
+ idButtonIface->set_property("ButtonPressed", !state);
+}
+
+static void idButtonGPIOHandler()
{
gpiod::line_event gpioLineEvent = idButtonLine.event_read();
- if (gpioLineEvent.event_type == gpiod::line_event::FALLING_EDGE)
- {
- idButtonIface->set_property("ButtonPressed", true);
- }
- else if (gpioLineEvent.event_type == gpiod::line_event::RISING_EDGE)
- {
- idButtonIface->set_property("ButtonPressed", false);
- }
+ 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) {
@@ -2368,7 +2391,7 @@
errMsg.c_str());
return;
}
- idButtonHandler();
+ idButtonGPIOHandler();
});
}
@@ -2424,13 +2447,9 @@
}
}
-static void postCompleteHandler()
+static void postCompleteHandler(bool state)
{
- gpiod::line_event gpioLineEvent = postCompleteLine.event_read();
-
- bool postComplete =
- gpioLineEvent.event_type == gpiod::line_event::FALLING_EDGE;
- if (postComplete)
+ if (!state)
{
sendPowerControlEvent(Event::postCompleteAssert);
osIface->set_property("OperatingSystemState", std::string("Standby"));
@@ -2440,6 +2459,15 @@
sendPowerControlEvent(Event::postCompleteDeAssert);
osIface->set_property("OperatingSystemState", std::string("Inactive"));
}
+}
+
+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) {
@@ -2451,7 +2479,7 @@
errMsg.c_str());
return;
}
- postCompleteHandler();
+ postCompleteGPIOHandler();
});
}
@@ -2666,166 +2694,6 @@
std::move(pulseEventMatcherCallback));
}
-inline static sdbusplus::bus::match::match powerButtonEventMonitor()
-{
- auto onMatch = [](bool value) {
- if (value == false)
- {
- powerButtonPressLog();
- powerButtonIface->set_property("ButtonPressed", true);
- if (!powerButtonMask)
- {
- sendPowerControlEvent(Event::powerButtonPressed);
- addRestartCause(RestartCause::powerButton);
- }
- else
- {
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "power button press masked\n");
- }
- }
- else
- {
- powerButtonIface->set_property("ButtonPressed", false);
- }
- };
-
- return dbusGPIOMatcher(powerButtonConfig, std::move(onMatch));
-}
-
-inline static sdbusplus::bus::match::match resetButtonEventMonitor()
-{
- auto onMatch = [](bool value) {
- if (value == false)
- {
- resetButtonPressLog();
- resetButtonIface->set_property("ButtonPressed", true);
- if (!resetButtonMask)
- {
- sendPowerControlEvent(Event::resetButtonPressed);
- addRestartCause(RestartCause::resetButton);
- }
- else
- {
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "reset button press masked");
- }
- }
- else
- {
- resetButtonIface->set_property("ButtonPressed", false);
- }
- };
-
- return dbusGPIOMatcher(resetButtonConfig, std::move(onMatch));
-}
-
-inline static sdbusplus::bus::match::match powerOkEventMonitor()
-{
- auto onMatch = [](bool value) {
- Event powerControlEvent =
- value ? Event::psPowerOKAssert : Event::psPowerOKDeAssert;
- sendPowerControlEvent(powerControlEvent);
- };
-
- return dbusGPIOMatcher(powerOkConfig, std::move(onMatch));
-}
-
-inline static sdbusplus::bus::match::match sioPwrGoodEventMonitor()
-{
- auto onMatch = [](bool value) {
- Event powerControlEvent =
- value ? Event::sioPowerGoodAssert : Event::sioPowerGoodDeAssert;
- sendPowerControlEvent(powerControlEvent);
- };
-
- return dbusGPIOMatcher(sioPwrGoodConfig, std::move(onMatch));
-}
-
-inline static sdbusplus::bus::match::match sioOnControlEventMonitor()
-{
- auto onMatch = [](bool value) {
- std::string errMsg =
- "SIO_ONCONTROL value changed : " + std::to_string(value);
- phosphor::logging::log<phosphor::logging::level::ERR>(errMsg.c_str());
- };
-
- return dbusGPIOMatcher(sioOnControlConfig, std::move(onMatch));
-}
-
-inline static sdbusplus::bus::match::match sioS5EventMonitor()
-{
- auto onMatch = [](bool value) {
- Event powerControlEvent =
- value ? Event::sioS5DeAssert : Event::sioS5Assert;
- sendPowerControlEvent(powerControlEvent);
- };
-
- return dbusGPIOMatcher(sioS5Config, std::move(onMatch));
-}
-
-inline static sdbusplus::bus::match::match nmiButtonEventMonitor()
-{
- auto onMatch = [](bool value) {
- if (value)
- {
- nmiButtonIface->set_property("ButtonPressed", false);
- }
- else
- {
- nmiButtonPressLog();
- nmiButtonIface->set_property("ButtonPressed", true);
- if (nmiButtonMasked)
- {
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "NMI button press masked");
- }
- else
- {
- setNmiSource();
- }
- }
- };
-
- return dbusGPIOMatcher(nmiButtonConfig, std::move(onMatch));
-}
-
-inline static sdbusplus::bus::match::match idButtonEventMonitor()
-{
- auto onMatch = [](bool value) {
- if (value)
- {
- idButtonIface->set_property("ButtonPressed", false);
- }
- else
- {
- idButtonIface->set_property("ButtonPressed", true);
- }
- };
-
- return dbusGPIOMatcher(idButtonConfig, std::move(onMatch));
-}
-
-inline static sdbusplus::bus::match::match postCompleteEventMonitor()
-{
- auto onMatch = [](bool value) {
- if (value)
- {
- sendPowerControlEvent(Event::postCompleteDeAssert);
- osIface->set_property("OperatingSystemState",
- std::string("Inactive"));
- }
- else
- {
- sendPowerControlEvent(Event::postCompleteAssert);
- osIface->set_property("OperatingSystemState",
- std::string("Standby"));
- }
- };
-
- return dbusGPIOMatcher(postCompleteConfig, std::move(onMatch));
-}
-
int getProperty(ConfigData& configData)
{
auto method = conn->new_method_call(
@@ -2916,7 +2784,7 @@
// Request PS_PWROK GPIO events
if (powerOkConfig.type == ConfigType::GPIO)
{
- if (!requestGPIOEvents(powerOkConfig.lineName, psPowerOKHandler,
+ if (!requestGPIOEvents(powerOkConfig.lineName, psPowerOKGPIOHandler,
psPowerOKLine, psPowerOKEvent))
{
return -1;
@@ -2926,7 +2794,7 @@
{
static sdbusplus::bus::match::match powerOkEventMonitor =
- power_control::powerOkEventMonitor();
+ power_control::dbusGPIOMatcher(powerOkConfig, psPowerOKHandler);
}
else
{
@@ -2941,7 +2809,7 @@
if (sioPwrGoodConfig.type == ConfigType::GPIO)
{
if (!requestGPIOEvents(sioPwrGoodConfig.lineName,
- sioPowerGoodHandler, sioPowerGoodLine,
+ sioPowerGoodGPIOHandler, sioPowerGoodLine,
sioPowerGoodEvent))
{
return -1;
@@ -2950,7 +2818,8 @@
else if (sioPwrGoodConfig.type == ConfigType::DBUS)
{
static sdbusplus::bus::match::match sioPwrGoodEventMonitor =
- power_control::sioPwrGoodEventMonitor();
+ power_control::dbusGPIOMatcher(sioPwrGoodConfig,
+ sioPowerGoodHandler);
}
else
{
@@ -2963,7 +2832,7 @@
if (sioOnControlConfig.type == ConfigType::GPIO)
{
if (!requestGPIOEvents(sioOnControlConfig.lineName,
- sioOnControlHandler, sioOnControlLine,
+ sioOnControlGPIOHandler, sioOnControlLine,
sioOnControlEvent))
{
return -1;
@@ -2972,7 +2841,8 @@
else if (sioOnControlConfig.type == ConfigType::DBUS)
{
static sdbusplus::bus::match::match sioOnControlEventMonitor =
- power_control::sioOnControlEventMonitor();
+ power_control::dbusGPIOMatcher(sioOnControlConfig,
+ sioOnControlHandler);
}
else
{
@@ -2985,7 +2855,7 @@
// Request SIO_S5 GPIO events
if (sioS5Config.type == ConfigType::GPIO)
{
- if (!requestGPIOEvents(sioS5Config.lineName, sioS5Handler,
+ if (!requestGPIOEvents(sioS5Config.lineName, sioS5GPIOHandler,
sioS5Line, sioS5Event))
{
return -1;
@@ -2994,7 +2864,7 @@
else if (sioS5Config.type == ConfigType::DBUS)
{
static sdbusplus::bus::match::match sioS5EventMonitor =
- power_control::sioS5EventMonitor();
+ power_control::dbusGPIOMatcher(sioS5Config, sioS5Handler);
}
else
{
@@ -3007,8 +2877,9 @@
// Request POWER_BUTTON GPIO events
if (powerButtonConfig.type == ConfigType::GPIO)
{
- if (!requestGPIOEvents(powerButtonConfig.lineName, powerButtonHandler,
- powerButtonLine, powerButtonEvent))
+ if (!requestGPIOEvents(powerButtonConfig.lineName,
+ powerButtonGPIOHandler, powerButtonLine,
+ powerButtonEvent))
{
return -1;
}
@@ -3016,14 +2887,16 @@
else if (powerButtonConfig.type == ConfigType::DBUS)
{
static sdbusplus::bus::match::match powerButtonEventMonitor =
- power_control::powerButtonEventMonitor();
+ power_control::dbusGPIOMatcher(powerButtonConfig,
+ powerButtonHandler);
}
// Request RESET_BUTTON GPIO events
if (resetButtonConfig.type == ConfigType::GPIO)
{
- if (!requestGPIOEvents(resetButtonConfig.lineName, resetButtonHandler,
- resetButtonLine, resetButtonEvent))
+ if (!requestGPIOEvents(resetButtonConfig.lineName,
+ resetButtonGPIOHandler, resetButtonLine,
+ resetButtonEvent))
{
return -1;
}
@@ -3031,7 +2904,8 @@
else if (resetButtonConfig.type == ConfigType::DBUS)
{
static sdbusplus::bus::match::match resetButtonEventMonitor =
- power_control::resetButtonEventMonitor();
+ power_control::dbusGPIOMatcher(resetButtonConfig,
+ resetButtonHandler);
}
// Request NMI_BUTTON GPIO events
@@ -3039,14 +2913,14 @@
{
if (!nmiButtonConfig.lineName.empty())
{
- requestGPIOEvents(nmiButtonConfig.lineName, nmiButtonHandler,
+ requestGPIOEvents(nmiButtonConfig.lineName, nmiButtonGPIOHandler,
nmiButtonLine, nmiButtonEvent);
}
}
else if (nmiButtonConfig.type == ConfigType::DBUS)
{
static sdbusplus::bus::match::match nmiButtonEventMonitor =
- power_control::nmiButtonEventMonitor();
+ power_control::dbusGPIOMatcher(nmiButtonConfig, nmiButtonHandler);
}
// Request ID_BUTTON GPIO events
@@ -3054,14 +2928,14 @@
{
if (!idButtonConfig.lineName.empty())
{
- requestGPIOEvents(idButtonConfig.lineName, idButtonHandler,
+ requestGPIOEvents(idButtonConfig.lineName, idButtonGPIOHandler,
idButtonLine, idButtonEvent);
}
}
else if (idButtonConfig.type == ConfigType::DBUS)
{
static sdbusplus::bus::match::match idButtonEventMonitor =
- power_control::idButtonEventMonitor();
+ power_control::dbusGPIOMatcher(idButtonConfig, idButtonHandler);
}
#ifdef USE_PLT_RST
@@ -3075,8 +2949,9 @@
// Request POST_COMPLETE GPIO events
if (postCompleteConfig.type == ConfigType::GPIO)
{
- if (!requestGPIOEvents(postCompleteConfig.lineName, postCompleteHandler,
- postCompleteLine, postCompleteEvent))
+ if (!requestGPIOEvents(postCompleteConfig.lineName,
+ postCompleteGPIOHandler, postCompleteLine,
+ postCompleteEvent))
{
return -1;
}
@@ -3084,7 +2959,8 @@
else if (postCompleteConfig.type == ConfigType::DBUS)
{
static sdbusplus::bus::match::match postCompleteEventMonitor =
- power_control::postCompleteEventMonitor();
+ power_control::dbusGPIOMatcher(postCompleteConfig,
+ postCompleteHandler);
}
else
{