Support IPMI command as last power event in chassis status

This change uses the IPMI command restart cause to determine
if the last power event was from an IPMI command.

Tested:
Pressed the physical power button:
ipmitool chassis status
Last Power Event     :

ipmitool power on
ipmitool chassis status
Last Power Event     : command

Change-Id: I974d025087218d3ca0aab7327f89993fdf9c2fc9
Signed-off-by: Jason M. Bills <jason.m.bills@linux.intel.com>
diff --git a/src/chassiscommands.cpp b/src/chassiscommands.cpp
index 1af5b28..ca12904 100644
--- a/src/chassiscommands.cpp
+++ b/src/chassiscommands.cpp
@@ -366,6 +366,47 @@
     return 0;
 }
 
+static bool getRestartCause(std::string& restartCause)
+{
+    constexpr const char* restartCausePath =
+        "/xyz/openbmc_project/control/host0/restart_cause";
+    constexpr const char* restartCauseIntf =
+        "xyz.openbmc_project.Common.RestartCause";
+    std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
+
+    try
+    {
+        auto service =
+            ipmi::getService(*busp, restartCauseIntf, restartCausePath);
+
+        ipmi::Value result = ipmi::getDbusProperty(
+            *busp, service, restartCausePath, restartCauseIntf, "RestartCause");
+        restartCause = std::get<std::string>(result);
+    }
+    catch (const std::exception& e)
+    {
+        log<level::ERR>("Failed to fetch RestartCause property",
+                        entry("ERROR=%s", e.what()),
+                        entry("PATH=%s", restartCausePath),
+                        entry("INTERFACE=%s", restartCauseIntf));
+        return false;
+    }
+    return true;
+}
+
+static bool checkIPMIRestartCause(bool& ipmiRestartCause)
+{
+    std::string restartCause;
+    if (!getRestartCause(restartCause))
+    {
+        return false;
+    }
+    ipmiRestartCause =
+        (restartCause ==
+         "xyz.openbmc_project.State.Host.RestartCause.IpmiCommand");
+    return true;
+}
+
 //----------------------------------------------------------------------
 // Get Chassis Status commands
 //----------------------------------------------------------------------
@@ -449,6 +490,12 @@
 
     bool powerDownAcFailed = power_policy::getACFailStatus();
 
+    bool powerStatusIPMI = false;
+    if (!checkIPMIRestartCause(powerStatusIPMI))
+    {
+        return ipmi::responseUnspecifiedError();
+    }
+
     // This response has a lot of hard-coded, unsupported fields
     // They are set to false or 0
     constexpr bool powerOverload = false;
@@ -458,7 +505,6 @@
     constexpr bool powerDownOverload = false;
     constexpr bool powerDownInterlock = false;
     constexpr bool powerDownPowerFault = false;
-    constexpr bool powerStatusIPMI = false;
     constexpr bool chassisIntrusionActive = false;
     constexpr bool frontPanelLockoutActive = false;
     constexpr bool driveFault = false;
@@ -487,7 +533,7 @@
         interruptButtonDisableAllow, sleepButtonDisableAllow);
 }
 
-static uint4_t getRestartCause(const std::string& cause)
+static uint4_t getRestartCauseValue(const std::string& cause)
 {
     uint4_t restartCauseValue = 0;
     if (cause == "xyz.openbmc_project.State.Host.RestartCause.Unknown")
@@ -551,32 +597,13 @@
               >
     ipmiGetSystemRestartCause()
 {
-    constexpr const char* restartCausePath =
-        "/xyz/openbmc_project/control/host0/restart_cause";
-    constexpr const char* restartCauseIntf =
-        "xyz.openbmc_project.Common.RestartCause";
     std::string restartCauseStr;
-    std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
-
-    try
+    if (!getRestartCause(restartCauseStr))
     {
-        auto service =
-            ipmi::getService(*busp, restartCauseIntf, restartCausePath);
-
-        ipmi::Value result = ipmi::getDbusProperty(
-            *busp, service, restartCausePath, restartCauseIntf, "RestartCause");
-        restartCauseStr = std::get<std::string>(result);
-    }
-    catch (const std::exception& e)
-    {
-        log<level::ERR>("Failed to fetch RestartCause property",
-                        entry("ERROR=%s", e.what()),
-                        entry("PATH=%s", restartCausePath),
-                        entry("INTERFACE=%s", restartCauseIntf));
         return ipmi::responseUnspecifiedError();
     }
 
-    return ipmi::responseSuccess(getRestartCause(restartCauseStr), 0, 0);
+    return ipmi::responseSuccess(getRestartCauseValue(restartCauseStr), 0, 0);
 }
 
 ipmi::RspType<> ipmiSetFrontPanelButtonEnables(bool disablePowerButton,