Improve graceful power state handling

The current timeout of 1 minute for an OS to respond to a graceful
shutdown, is not enough for some situations.  Extending this to
5 minutes to allow enough time for an OS to gracefully shut down.

This causes an issue with the current code which ignores further
power change requests during the graceful timeout, which would now
block forced shutdown and reset commands for 5 minutes.  So, this
change also adds support for shutdown and reset commands to be
accepted during a graceful shutdown timeout.

Tested:
Triggered a graceful shutdown and confirmed that it will time out
after 5 minutes and that shutdown and reset commands can be issued
during that time.

Change-Id: Ie88207cbc754a34642b4e1bc9636a257475cdee6
Signed-off-by: Jason M. Bills <jason.m.bills@linux.intel.com>
diff --git a/power-control-x86/src/power_control.cpp b/power-control-x86/src/power_control.cpp
index 6a02753..cc9b573 100644
--- a/power-control-x86/src/power_control.cpp
+++ b/power-control-x86/src/power_control.cpp
@@ -75,9 +75,8 @@
 const static constexpr int powerCycleTimeMs = 5000;
 const static constexpr int sioPowerGoodWatchdogTimeMs = 1000;
 const static constexpr int psPowerOKWatchdogTimeMs = 8000;
-const static constexpr int gracefulPowerOffTimeMs = 60000;
+const static constexpr int gracefulPowerOffTimeS = 5 * 60;
 const static constexpr int warmResetCheckTimeMs = 500;
-const static constexpr int buttonMaskTimeMs = 60000;
 const static constexpr int powerOffSaveTimeMs = 7000;
 
 const static std::filesystem::path powerControlDir = "/var/lib/power-control";
@@ -1192,7 +1191,7 @@
     phosphor::logging::log<phosphor::logging::level::INFO>(
         "Graceful power-off timer started");
     gracefulPowerOffTimer.expires_after(
-        std::chrono::milliseconds(gracefulPowerOffTimeMs));
+        std::chrono::seconds(gracefulPowerOffTimeS));
     gracefulPowerOffTimer.async_wait([](const boost::system::error_code ec) {
         if (ec)
         {
@@ -1669,6 +1668,21 @@
         case Event::gracefulPowerOffTimerExpired:
             setPowerState(PowerState::on);
             break;
+        case Event::powerOffRequest:
+            gracefulPowerOffTimer.cancel();
+            setPowerState(PowerState::transitionToOff);
+            forcePowerOff();
+            break;
+        case Event::powerCycleRequest:
+            gracefulPowerOffTimer.cancel();
+            setPowerState(PowerState::transitionToCycleOff);
+            forcePowerOff();
+            break;
+        case Event::resetRequest:
+            gracefulPowerOffTimer.cancel();
+            setPowerState(PowerState::on);
+            reset();
+            break;
         default:
             phosphor::logging::log<phosphor::logging::level::INFO>(
                 "No action taken.");
@@ -1746,6 +1760,21 @@
         case Event::gracefulPowerOffTimerExpired:
             setPowerState(PowerState::on);
             break;
+        case Event::powerOffRequest:
+            gracefulPowerOffTimer.cancel();
+            setPowerState(PowerState::transitionToOff);
+            forcePowerOff();
+            break;
+        case Event::powerCycleRequest:
+            gracefulPowerOffTimer.cancel();
+            setPowerState(PowerState::transitionToCycleOff);
+            forcePowerOff();
+            break;
+        case Event::resetRequest:
+            gracefulPowerOffTimer.cancel();
+            setPowerState(PowerState::on);
+            reset();
+            break;
         default:
             phosphor::logging::log<phosphor::logging::level::INFO>(
                 "No action taken.");