Generate errorlog when timer expires waiting for host to shutdown

Fixes openbmc/openbmc#930

Change-Id: I1780071d5cbef23df88c17fe131098f54f54f25f
Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
diff --git a/softoff/mainapp.cpp b/softoff/mainapp.cpp
index 9ed34dd..1f037fc 100644
--- a/softoff/mainapp.cpp
+++ b/softoff/mainapp.cpp
@@ -14,7 +14,9 @@
  * limitations under the License.
  */
 #include <systemd/sd-event.h>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <phosphor-logging/elog-errors.hpp>
+#include <xyz/openbmc_project/State/Host/error.hpp>
 #include "softoff.hpp"
 #include "config.h"
 #include "timer.hpp"
@@ -45,14 +47,14 @@
     // Attach the bus to sd_event to service user requests
     bus.attach_event(events, SD_EVENT_PRIORITY_NORMAL);
 
-    // Create the SoftPowerOff object.
-    phosphor::ipmi::SoftPowerOff powerObj(bus, events, SOFTOFF_OBJPATH);
-
     // Claim the bus. Delaying it until sending SMS_ATN may result
     // in a race condition between this available and IPMI trying to send
     // message as a reponse to ack from host.
     bus.request_name(SOFTOFF_BUSNAME);
 
+    // Create the SoftPowerOff object.
+    phosphor::ipmi::SoftPowerOff powerObj(bus, events, SOFTOFF_OBJPATH);
+
     // Wait for client requests until this application has processed
     // at least one successful SoftPowerOff or we timed out
     while(!powerObj.isCompleted() && !powerObj.isTimerExpired())
@@ -67,6 +69,25 @@
         }
     }
 
+    // Log an error if we timed out after getting Ack for SMS_ATN and before
+    // getting the Host Shutdown response
+    if(powerObj.isTimerExpired() && (powerObj.responseReceived() ==
+            phosphor::ipmi::Base::SoftPowerOff::HostResponse::SoftOffReceived))
+    {
+        try
+        {
+            elog<sdbusplus::xyz::openbmc_project::State
+                    ::Host::Error::SoftOffTimeout>(
+                 prev_entry<phosphor::logging::xyz::openbmc_project::State
+                    ::Host::SoftOffTimeout::TIMEOUT_IN_MSEC>());
+        }
+        catch (sdbusplus::xyz::openbmc_project::State::Host::Error
+                    ::SoftOffTimeout& elog)
+        {
+            commit(elog.name());
+        }
+    }
+
     // Cleanup the event handler
     events = sd_event_unref(events);