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/handler.cpp b/handler.cpp
index ac2ca3a..8a076f2 100644
--- a/handler.cpp
+++ b/handler.cpp
@@ -24,6 +24,8 @@
 #include <cstdio>
 #include <filesystem>
 #include <fstream>
+#include <phosphor-logging/log.hpp>
+#include <sdbusplus/bus.hpp>
 #include <sstream>
 #include <string>
 #include <tuple>
@@ -140,6 +142,54 @@
     return version;
 }
 
+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";
+
+void Handler::psuResetDelay(std::uint32_t delay) const
+{
+    using namespace phosphor::logging;
+
+    std::ofstream ofs;
+    ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
+    if (!ofs.good())
+    {
+        std::fprintf(stderr, "Unable to open file for output.\n");
+        throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
+    }
+
+    ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl;
+    if (ofs.fail())
+    {
+        std::fprintf(stderr, "Write failed\n");
+        ofs.close();
+        throw IpmiException(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);
+    }
+    catch (const sdbusplus::exception::SdBusError& ex)
+    {
+        log<level::ERR>("Failed to call PSU hard reset");
+        throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
+    }
+}
+
 Handler handlerImpl;
 
 } // namespace ipmi
diff --git a/handler.hpp b/handler.hpp
index 735ee15..a068fce 100644
--- a/handler.hpp
+++ b/handler.hpp
@@ -41,6 +41,14 @@
      * @throw IpmiException on failure.
      */
     virtual VersionTuple getCpldVersion(unsigned int id) const = 0;
+
+    /**
+     * Set the PSU Reset delay.
+     *
+     * @param[in] delay - delay in seconds.
+     * @throw IpmiException on failure.
+     */
+    virtual void psuResetDelay(std::uint32_t delay) const = 0;
 };
 
 class Handler : public HandlerInterface
@@ -52,6 +60,7 @@
     std::tuple<std::uint8_t, std::string> getEthDetails() const override;
     std::int64_t getRxPackets(const std::string& name) const override;
     VersionTuple getCpldVersion(unsigned int id) const override;
+    void psuResetDelay(std::uint32_t delay) const override;
 };
 
 extern Handler handlerImpl;
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;
diff --git a/psu.hpp b/psu.hpp
index bbbc963..657b65c 100644
--- a/psu.hpp
+++ b/psu.hpp
@@ -1,5 +1,7 @@
 #pragma once
 
+#include "handler.hpp"
+
 #include <ipmid/api.h>
 
 namespace google
@@ -7,9 +9,17 @@
 namespace ipmi
 {
 
+struct PsuResetRequest
+{
+    uint8_t subcommand;
+    // Delay in seconds.
+    uint32_t delay;
+} __attribute__((packed));
+
 // Set a time-delayed PSU hard reset.
 ipmi_ret_t PsuHardReset(const uint8_t* reqBuf, uint8_t* replyBuf,
-                        size_t* dataLen);
+                        size_t* dataLen,
+                        const HandlerInterface* handler = &handlerImpl);
 
 } // namespace ipmi
 } // namespace google
diff --git a/test/Makefile.am b/test/Makefile.am
index df796d7..04b6836 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -30,3 +30,7 @@
 check_PROGRAMS += cpld_unittest
 cpld_unittest_SOURCES = cpld_unittest.cpp
 cpld_unittest_LDADD = $(top_builddir)/libsyscmds_common.la
+
+check_PROGRAMS += psu_unittest
+psu_unittest_SOURCES = psu_unittest.cpp
+psu_unittest_LDADD = $(top_builddir)/libsyscmds_common.la
diff --git a/test/handler_mock.hpp b/test/handler_mock.hpp
index a92606a..6eb929a 100644
--- a/test/handler_mock.hpp
+++ b/test/handler_mock.hpp
@@ -20,6 +20,7 @@
     MOCK_CONST_METHOD1(getCpldVersion,
                        std::tuple<std::uint8_t, std::uint8_t, std::uint8_t,
                                   std::uint8_t>(unsigned int));
+    MOCK_CONST_METHOD1(psuResetDelay, void(std::uint32_t));
 };
 
 } // namespace ipmi
diff --git a/test/psu_unittest.cpp b/test/psu_unittest.cpp
new file mode 100644
index 0000000..d2c84b1
--- /dev/null
+++ b/test/psu_unittest.cpp
@@ -0,0 +1,50 @@
+#include "handler_mock.hpp"
+#include "main.hpp"
+#include "psu.hpp"
+
+#include <cstdint>
+#include <cstring>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#define MAX_IPMI_BUFFER 64
+
+using ::testing::Return;
+
+namespace google
+{
+namespace ipmi
+{
+
+TEST(PsuCommandTest, InvalidRequestLength)
+{
+    std::vector<std::uint8_t> request = {SysOEMCommands::SysPsuHardReset};
+    size_t dataLen = request.size();
+    std::uint8_t reply[MAX_IPMI_BUFFER];
+
+    HandlerMock hMock;
+    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
+              PsuHardReset(request.data(), reply, &dataLen, &hMock));
+}
+
+TEST(PsuCommandTest, ValidRequest)
+{
+    std::uint32_t delayValue = 0x45;
+    struct PsuResetRequest requestContents;
+    requestContents.subcommand = SysOEMCommands::SysPsuHardReset;
+    requestContents.delay = delayValue;
+
+    std::vector<std::uint8_t> request(sizeof(requestContents));
+    std::memcpy(request.data(), &requestContents, sizeof(requestContents));
+    size_t dataLen = request.size();
+    std::uint8_t reply[MAX_IPMI_BUFFER];
+
+    HandlerMock hMock;
+    EXPECT_CALL(hMock, psuResetDelay(delayValue));
+    EXPECT_EQ(IPMI_CC_OK,
+              PsuHardReset(request.data(), reply, &dataLen, &hMock));
+}
+
+} // namespace ipmi
+} // namespace google