add handler logic to handle SysPsuHardReset

Add handler logic to handler for SysPsuHardReset such that it splits the
true IPMI processing from the business logic.

Tested: Only ran unit-tests (added new ones).
Change-Id: Id5dae85cbc54db7dee3cb5e62ed80c77ff3d5719
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/psu.cpp b/psu.cpp
index 7f334ef..73d716d 100644
--- a/psu.cpp
+++ b/psu.cpp
@@ -16,36 +16,20 @@
 
 #include "psu.hpp"
 
+#include "errors.hpp"
+#include "handler.hpp"
 #include "main.hpp"
 
 #include <cstdint>
 #include <cstring>
-#include <fstream>
-#include <phosphor-logging/log.hpp>
-#include <sdbusplus/bus.hpp>
 
 namespace google
 {
 namespace ipmi
 {
 
-using namespace phosphor::logging;
-
-struct PsuResetRequest
-{
-    uint8_t subcommand;
-    // Delay in seconds.
-    uint32_t delay;
-} __attribute__((packed));
-
-static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
-static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
-static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
-static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
-static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
-
 ipmi_ret_t PsuHardReset(const uint8_t* reqBuf, uint8_t* replyBuf,
-                        size_t* dataLen)
+                        size_t* dataLen, const HandlerInterface* handler)
 {
     struct PsuResetRequest request;
 
@@ -57,42 +41,13 @@
     }
 
     std::memcpy(&request, &reqBuf[0], sizeof(struct PsuResetRequest));
-
-    std::ofstream ofs;
-    ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
-    if (!ofs.good())
-    {
-        std::fprintf(stderr, "Unable to open file for output.\n");
-        return IPMI_CC_UNSPECIFIED_ERROR;
-    }
-
-    ofs << "PSU_HARDRESET_DELAY=" << request.delay << std::endl;
-    if (ofs.fail())
-    {
-        std::fprintf(stderr, "Write failed\n");
-        ofs.close();
-        return IPMI_CC_UNSPECIFIED_ERROR;
-    }
-
-    // Write succeeded, please continue.
-    ofs.flush();
-    ofs.close();
-
-    auto bus = sdbusplus::bus::new_default();
-    auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
-                                      SYSTEMD_INTERFACE, "StartUnit");
-
-    method.append(PSU_HARDRESET_TARGET);
-    method.append("replace");
-
     try
     {
-        bus.call_noreply(method);
+        handler->psuResetDelay(request.delay);
     }
-    catch (const sdbusplus::exception::SdBusError& ex)
+    catch (const IpmiException& e)
     {
-        log<level::ERR>("Failed to call PSU hard reset");
-        return IPMI_CC_UNSPECIFIED_ERROR;
+        return e.getIpmiError();
     }
 
     replyBuf[0] = SysPsuHardReset;