Add sys command for powercycle on host shutdown.

The new command will trigger a power cycle the next time the host shuts
down. This can exist in parallel with the existing mechanism to trigger
a power cycle after a specified time interval.

The implementation of host state detection and power cycling is platfrom
specific; the new command will just add a temporary file that marks the
system ready to powercycle on the next shutdown. Usually, a systemd unit
would be enabled by the presence of this file to handle the power
cycling process.

Signed-off-by: Shounak Mitra <shounak@google.com>
Change-Id: I0cc40307748fb996be3f6062d8cba1a4b5049683
diff --git a/psu.cpp b/psu.cpp
index d3ded47..49c0b72 100644
--- a/psu.cpp
+++ b/psu.cpp
@@ -56,5 +56,34 @@
     return IPMI_CC_OK;
 }
 
+ipmi_ret_t psuHardResetOnShutdown(const uint8_t* reqBuf, uint8_t* replyBuf,
+                                  size_t* dataLen,
+                                  const HandlerInterface* handler)
+{
+    struct PsuResetOnShutdownRequest request;
+
+    if ((*dataLen) < sizeof(request))
+    {
+        std::fprintf(stderr, "Invalid command length: %u\n",
+                     static_cast<uint32_t>(*dataLen));
+        return IPMI_CC_REQ_DATA_LEN_INVALID;
+    }
+
+    std::memcpy(&request, &reqBuf[0], sizeof(request));
+    try
+    {
+        handler->psuResetOnShutdown();
+    }
+    catch (const IpmiException& e)
+    {
+        return e.getIpmiError();
+    }
+
+    replyBuf[0] = SysPsuHardResetOnShutdown;
+    (*dataLen) = sizeof(uint8_t);
+
+    return IPMI_CC_OK;
+}
+
 } // namespace ipmi
 } // namespace google