Clear host command queue on a power on

When the RequestedHostTransition property changes to On, clear
any pending commands in the command queue.  This is done to avoid
race conditions around state transitions as well as other
scenarios like the following:

1) Host is already off
2) RequestedHostTransition is set to Off
3) RequestedHostTransition is set to On
4) Host powers on
5) Host immediately powers off because of the pending command
   sent in 2).

Resolves openbmc/openbmc#3207

Tested:  Verified the scenario above no longer occurs.

Change-Id: I26c8195c305c75b01333d1b10ff4bf16d76b91a6
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/host-cmd-manager.hpp b/host-cmd-manager.hpp
index 06ac5fa..c25706c 100644
--- a/host-cmd-manager.hpp
+++ b/host-cmd-manager.hpp
@@ -3,6 +3,7 @@
 #include <tuple>
 #include <queue>
 #include <sdbusplus/bus.hpp>
+#include <sdbusplus/bus/match.hpp>
 #include <timer.hpp>
 #include <host-ipmid/ipmid-host-cmd-utils.hpp>
 
@@ -68,6 +69,31 @@
          */
         void hostTimeout();
 
+        /** @brief Clears the command queue
+         *
+         *  @detail Clears the command queue and calls all callbacks
+         *          specifying the command wasn't successful.
+         */
+        void clearQueue();
+
+        /** @brief Clears the command queue on a power on
+         *
+         *  @detail The properties changed handler for the
+         *          RequestedHostTransition property.  When this property
+         *          changes to 'On', this function will purge the command
+         *          queue.
+         *
+         *          This is done to avoid having commands that were issued
+         *          before the host powers on from getting sent to the host,
+         *          either due to race conditions around state transitions
+         *          or from a user doing something like requesting an already
+         *          powered off system to power off again and then immediately
+         *          requesting a power on.
+         *
+         *  @param[in] msg - the sdbusplus message containing the property
+         */
+        void clearQueueOnPowerOn(sdbusplus::message::message& msg);
+
         /** @brief Reference to the dbus handler */
         sdbusplus::bus::bus& bus;
 
@@ -76,6 +102,9 @@
 
         /** @brief Timer for commands to host */
         phosphor::ipmi::Timer timer;
+
+        /** @brief Match handler for the requested host state */
+        sdbusplus::bus::match_t hostTransitionMatch;
 };
 
 } // namespace command