google-ipmi-sys: Refactor to use new version of OEM IPMI Handler

Using the new version of ipmi handler provide a higher level wrapper
over the same functionalities. It helps us parse the input and output to
have more control of the input/output we see.

The input and output will be
`std::uint8_t, std::optional<std::vector<uint8_t>>`.

This represents `subcommand` and any input data.

Changes to note,
- all subcommand in the request/response struct are removed. It will be
  managed by the wrapper directly.
    - Unit tests checking for input with only the subcommand are
      removed.
- Move all reply struct to header files to be accessible in unit test.

Tested:
All IPMI OEM command still works the same as before this change.

Change-Id: I4230ab84a497a867248fe82224e32cc69b314b64
Signed-off-by: Willy Tu <wltu@google.com>
diff --git a/cable.cpp b/cable.cpp
index 175241d..d06cf23 100644
--- a/cable.cpp
+++ b/cable.cpp
@@ -20,7 +20,9 @@
 
 #include <cstdint>
 #include <cstring>
+#include <ipmid/api-types.hpp>
 #include <string>
+#include <vector>
 
 namespace google
 {
@@ -29,12 +31,11 @@
 
 struct CableRequest
 {
-    uint8_t subcommand;
     uint8_t ifNameLength;
 } __attribute__((packed));
 
-ipmi_ret_t cableCheck(const uint8_t* reqBuf, uint8_t* replyBuf, size_t* dataLen,
-                      const HandlerInterface* handler)
+Resp cableCheck(const std::vector<std::uint8_t>& data,
+                const HandlerInterface* handler)
 {
     // There is an IPMI LAN channel statistics command which could be used for
     // this type of check, however, we're not able to wait for the OpenBMC
@@ -44,30 +45,31 @@
     // The path we're checking: /sys/class/net/eth1/statistics/rx_packets
 
     // This command is expecting: [0x00][len][ifName]
-    if ((*dataLen) < sizeof(struct CableRequest) + sizeof(uint8_t))
+    // data should have [len][ifName]
+    if (data.size() < sizeof(struct CableRequest))
     {
         std::fprintf(stderr, "Invalid command length: %u\n",
-                     static_cast<uint32_t>(*dataLen));
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
+                     static_cast<uint32_t>(data.size()));
+        return ::ipmi::responseReqDataLenInvalid();
     }
 
     const auto request =
-        reinterpret_cast<const struct CableRequest*>(&reqBuf[0]);
+        reinterpret_cast<const struct CableRequest*>(data.data());
 
     // Sanity check the object contents.
     if (request->ifNameLength == 0)
     {
         std::fprintf(stderr, "Invalid string length: %d\n",
                      request->ifNameLength);
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
+        return ::ipmi::responseReqDataLenInvalid();
     }
 
     // Verify the request buffer contains the object and the string.
-    if ((*dataLen) < (sizeof(struct CableRequest) + request->ifNameLength))
+    if (data.size() < (sizeof(struct CableRequest) + request->ifNameLength))
     {
         std::fprintf(stderr, "*dataLen too small: %u\n",
-                     static_cast<uint32_t>(*dataLen));
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
+                     static_cast<uint32_t>(data.size()));
+        return ::ipmi::responseReqDataLenInvalid();
     }
 
     // Maximum length one can specify, plus null terminator.
@@ -83,20 +85,14 @@
     }
     catch (const IpmiException& e)
     {
-        return e.getIpmiError();
+        return ::ipmi::response(e.getIpmiError());
     }
 
-    struct CableReply reply;
-    reply.subcommand = SysCableCheck;
-
     // If we have received packets then there is a cable present.
-    reply.value = (count > 0) ? 1 : 0;
+    std::uint8_t value = (count > 0) ? 1 : 0;
 
-    // Return the subcommand and the result.
-    std::memcpy(&replyBuf[0], &reply, sizeof(struct CableReply));
-    (*dataLen) = sizeof(struct CableReply);
-
-    return IPMI_CC_OK;
+    return ::ipmi::responseSuccess(SysOEMCommands::SysCableCheck,
+                                   std::vector<std::uint8_t>{value});
 }
 
 } // namespace ipmi
diff --git a/cable.hpp b/cable.hpp
index 5a41d4e..79a4d2e 100644
--- a/cable.hpp
+++ b/cable.hpp
@@ -18,6 +18,9 @@
 
 #include <ipmid/api.h>
 
+#include <ipmid/api-types.hpp>
+#include <vector>
+
 namespace google
 {
 namespace ipmi
@@ -25,15 +28,14 @@
 
 struct CableReply
 {
-    uint8_t subcommand;
     uint8_t value;
 } __attribute__((packed));
 
 //
 // Handle the cablecheck.  Sys must supply which ethernet device they're
 // interested in.
-ipmi_ret_t cableCheck(const uint8_t* reqBuf, uint8_t* replyBuf, size_t* dataLen,
-                      const HandlerInterface* handler);
+Resp cableCheck(const std::vector<std::uint8_t>& data,
+                const HandlerInterface* handler);
 
 } // namespace ipmi
 } // namespace google
diff --git a/cpld.cpp b/cpld.cpp
index 9d12dee..f91fed4 100644
--- a/cpld.cpp
+++ b/cpld.cpp
@@ -19,6 +19,8 @@
 #include "handler.hpp"
 
 #include <cstring>
+#include <ipmid/api-types.hpp>
+#include <vector>
 
 namespace google
 {
@@ -27,42 +29,31 @@
 
 struct CpldRequest
 {
-    uint8_t subcommand;
     uint8_t id;
 } __attribute__((packed));
 
-struct CpldReply
-{
-    uint8_t subcommand;
-    uint8_t major;
-    uint8_t minor;
-    uint8_t point;
-    uint8_t subpoint;
-} __attribute__((packed));
-
 //
 // Handle reading the cpld version from the tmpfs.
 //
-ipmi_ret_t cpldVersion(const uint8_t* reqBuf, uint8_t* replyBuf,
-                       size_t* dataLen, const HandlerInterface* handler)
+Resp cpldVersion(const std::vector<std::uint8_t>& data,
+                 const HandlerInterface* handler)
 {
     struct CpldRequest request;
 
-    if ((*dataLen) < sizeof(request))
+    if (data.size() < sizeof(request))
     {
         std::fprintf(stderr, "Invalid command length: %u\n",
-                     static_cast<uint32_t>(*dataLen));
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
+                     static_cast<uint32_t>(data.size()));
+        return ::ipmi::responseReqDataLenInvalid();
     }
 
-    // reqBuf[0] is the subcommand.
-    // reqBuf[1] is the CPLD id. "/run/cpld{id}.version" is what we read.
+    // data[0] is the CPLD id. "/run/cpld{id}.version" is what we read.
     // Verified that this cast actually returns the value 255 and not something
-    // negative in the case where reqBuf[1] is 0xff.  However, it looks weird
+    // negative in the case where data[0] is 0xff.  However, it looks weird
     // since I would expect int(uint8(0xff)) to be -1.  So, just cast it
     // unsigned. we're casting to an int width to avoid it thinking it's a
     // letter, because it does that.
-    std::memcpy(&request, &reqBuf[0], sizeof(request));
+    std::memcpy(&request, data.data(), sizeof(request));
 
     try
     {
@@ -70,20 +61,18 @@
             handler->getCpldVersion(static_cast<unsigned int>(request.id));
 
         // Truncate if the version is too high (documented).
-        struct CpldReply reply;
-        reply.subcommand = SysCpldVersion;
-        reply.major = std::get<0>(values);
-        reply.minor = std::get<1>(values);
-        reply.point = std::get<2>(values);
-        reply.subpoint = std::get<3>(values);
+        auto major = std::get<0>(values);
+        auto minor = std::get<1>(values);
+        auto point = std::get<2>(values);
+        auto subpoint = std::get<3>(values);
 
-        std::memcpy(&replyBuf[0], &reply, sizeof(reply));
-        (*dataLen) = sizeof(reply);
-        return IPMI_CC_OK;
+        return ::ipmi::responseSuccess(
+            SysOEMCommands::SysCpldVersion,
+            std::vector<std::uint8_t>{major, minor, point, subpoint});
     }
     catch (const IpmiException& e)
     {
-        return e.getIpmiError();
+        return ::ipmi::response(e.getIpmiError());
     }
 }
 
diff --git a/cpld.hpp b/cpld.hpp
index 688fb54..84db3ba 100644
--- a/cpld.hpp
+++ b/cpld.hpp
@@ -18,14 +18,25 @@
 
 #include <ipmid/api.h>
 
+#include <ipmid/api-types.hpp>
+#include <vector>
+
 namespace google
 {
 namespace ipmi
 {
 
+struct CpldReply
+{
+    uint8_t major;
+    uint8_t minor;
+    uint8_t point;
+    uint8_t subpoint;
+} __attribute__((packed));
+
 // Given a cpld identifier, return a version if available.
-ipmi_ret_t cpldVersion(const uint8_t* reqBuf, uint8_t* replyBuf,
-                       size_t* dataLen, const HandlerInterface* handler);
+Resp cpldVersion(const std::vector<std::uint8_t>& data,
+                 const HandlerInterface* handler);
 
 } // namespace ipmi
 } // namespace google
diff --git a/entity_name.cpp b/entity_name.cpp
index 3f7fc64..3702ad5 100644
--- a/entity_name.cpp
+++ b/entity_name.cpp
@@ -20,6 +20,7 @@
 
 #include <cstdint>
 #include <cstring>
+#include <ipmid/api-types.hpp>
 #include <string>
 #include <vector>
 
@@ -40,30 +41,23 @@
 
 struct GetEntityNameRequest
 {
-    uint8_t subcommand;
     uint8_t entityId;
     uint8_t entityInstance;
 } __attribute__((packed));
 
-struct GetEntityNameReply
-{
-    uint8_t subcommand;
-    uint8_t entityNameLength;
-} __attribute__((packed));
-
-ipmi_ret_t getEntityName(const uint8_t* reqBuf, uint8_t* replyBuf,
-                         size_t* dataLen, HandlerInterface* handler)
+Resp getEntityName(const std::vector<std::uint8_t>& data,
+                   HandlerInterface* handler)
 {
     struct GetEntityNameRequest request;
 
-    if ((*dataLen) < sizeof(request))
+    if (data.size() < sizeof(request))
     {
         std::fprintf(stderr, "Invalid command length: %u\n",
-                     static_cast<uint32_t>(*dataLen));
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
+                     static_cast<uint32_t>(data.size()));
+        return ::ipmi::responseReqDataLenInvalid();
     }
 
-    std::memcpy(&request, &reqBuf[0], sizeof(request));
+    std::memcpy(&request, data.data(), sizeof(request));
     std::string entityName;
     try
     {
@@ -72,7 +66,7 @@
     }
     catch (const IpmiException& e)
     {
-        return e.getIpmiError();
+        return ::ipmi::response(e.getIpmiError());
     }
 
     int length = sizeof(struct GetEntityNameReply) + entityName.length();
@@ -81,16 +75,16 @@
     if (length > MAX_IPMI_BUFFER)
     {
         std::fprintf(stderr, "Response would overflow response buffer\n");
-        return IPMI_CC_INVALID;
+        return ::ipmi::responseInvalidCommand();
     }
 
-    auto reply = reinterpret_cast<struct GetEntityNameReply*>(&replyBuf[0]);
-    reply->subcommand = SysEntityName;
-    reply->entityNameLength = entityName.length();
-    std::memcpy(reply + 1, entityName.c_str(), entityName.length());
+    std::vector<std::uint8_t> reply;
+    reply.reserve(entityName.length() + sizeof(struct GetEntityNameReply));
+    reply.emplace_back(entityName.length()); /* entityNameLength */
+    reply.insert(reply.end(), entityName.begin(),
+                 entityName.end()); /* entityName */
 
-    (*dataLen) = length;
-    return IPMI_CC_OK;
+    return ::ipmi::responseSuccess(SysOEMCommands::SysEntityName, reply);
 }
 } // namespace ipmi
 } // namespace google
diff --git a/entity_name.hpp b/entity_name.hpp
index 26f12b7..fb04770 100644
--- a/entity_name.hpp
+++ b/entity_name.hpp
@@ -18,15 +18,23 @@
 
 #include <ipmid/api.h>
 
+#include <ipmid/api-types.hpp>
+#include <vector>
+
 namespace google
 {
 namespace ipmi
 {
 
+struct GetEntityNameReply
+{
+    uint8_t entityNameLength;
+} __attribute__((packed));
+
 // Handle the "entity id:entity instance" to entity name mapping command.
 // Sys can query the entity name for a particular "entity id:entity instance".
-ipmi_ret_t getEntityName(const uint8_t* reqBuf, uint8_t* replyBuf,
-                         size_t* dataLen, HandlerInterface* handler);
+Resp getEntityName(const std::vector<std::uint8_t>& data,
+                   HandlerInterface* handler);
 
 } // namespace ipmi
 } // namespace google
diff --git a/eth.cpp b/eth.cpp
index be6eefe..bbe1940 100644
--- a/eth.cpp
+++ b/eth.cpp
@@ -19,63 +19,48 @@
 
 #include <cstdint>
 #include <cstring>
+#include <ipmid/api-types.hpp>
 #include <string>
 #include <tuple>
+#include <vector>
 
 namespace google
 {
 namespace ipmi
 {
 
-struct EthDeviceRequest
-{
-    uint8_t subcommand;
-} __attribute__((packed));
-
 // TOOD(venture): The ipmid.h has this macro, which is a header we
 // can't normally access.
 #ifndef MAX_IPMI_BUFFER
 #define MAX_IPMI_BUFFER 64
 #endif
 
-ipmi_ret_t getEthDevice(const uint8_t* reqBuf, uint8_t* replyBuf,
-                        size_t* dataLen, const HandlerInterface* handler)
+Resp getEthDevice(const std::vector<std::uint8_t>& data,
+                  const HandlerInterface* handler)
 {
-    if ((*dataLen) < sizeof(struct EthDeviceRequest))
-    {
-        std::fprintf(stderr, "Invalid command length: %u\n",
-                     static_cast<uint32_t>(*dataLen));
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
-    }
-    reqBuf += sizeof(struct EthDeviceRequest);
-    *dataLen -= sizeof(struct EthDeviceRequest);
-
-    std::tuple<std::uint8_t, std::string> details = handler->getEthDetails(
-        std::string(reinterpret_cast<const char*>(reqBuf), *dataLen));
+    std::tuple<std::uint8_t, std::string> details =
+        handler->getEthDetails(std::string(data.begin(), data.end()));
 
     std::string device = std::get<1>(details);
     if (device.length() == 0)
     {
         std::fprintf(stderr, "Invalid eth string\n");
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
+        return ::ipmi::responseReqDataLenInvalid();
     }
 
     if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER)
     {
         std::fprintf(stderr, "Response would overflow response buffer\n");
-        return IPMI_CC_REQUESTED_TOO_MANY_BYTES;
+        return ::ipmi::responseRetBytesUnavailable();
     }
 
-    // Fill in the response buffer.
-    auto reply = reinterpret_cast<struct EthDeviceReply*>(&replyBuf[0]);
-    reply->subcommand = SysGetEthDevice;
-    reply->channel = std::get<0>(details);
-    reply->ifNameLength = device.length();
-    std::memcpy(reply + 1, device.c_str(), device.length());
+    std::vector<std::uint8_t> reply;
+    reply.reserve(device.length() + sizeof(struct EthDeviceReply));
+    reply.emplace_back(std::get<0>(details));                /* channel */
+    reply.emplace_back(device.length());                     /* ifNameLength */
+    reply.insert(reply.end(), device.begin(), device.end()); /* name */
 
-    (*dataLen) = sizeof(struct EthDeviceReply) + device.length();
-
-    return IPMI_CC_OK;
+    return ::ipmi::responseSuccess(SysOEMCommands::SysGetEthDevice, reply);
 }
 
 } // namespace ipmi
diff --git a/eth.hpp b/eth.hpp
index 046ac77..0fe8d4e 100644
--- a/eth.hpp
+++ b/eth.hpp
@@ -18,6 +18,9 @@
 
 #include <ipmid/api.h>
 
+#include <ipmid/api-types.hpp>
+#include <vector>
+
 namespace google
 {
 namespace ipmi
@@ -28,7 +31,6 @@
 // ncis connection.
 struct EthDeviceReply
 {
-    uint8_t subcommand;
     uint8_t channel;
     // ifNameLength doesn't include the null-terminator.
     uint8_t ifNameLength;
@@ -37,8 +39,8 @@
 // Handle the eth query command.
 // Sys can query the ifName and IPMI channel of the BMC's NCSI ethernet
 // device.
-ipmi_ret_t getEthDevice(const uint8_t* reqBuf, uint8_t* replyBuf,
-                        size_t* dataLen, const HandlerInterface* handler);
+Resp getEthDevice(const std::vector<std::uint8_t>& data,
+                  const HandlerInterface* handler);
 
 } // namespace ipmi
 } // namespace google
diff --git a/flash_size.cpp b/flash_size.cpp
index 069b1b1..4fb4976 100644
--- a/flash_size.cpp
+++ b/flash_size.cpp
@@ -22,6 +22,7 @@
 
 #include <cstdint>
 #include <cstring>
+#include <ipmid/api-types.hpp>
 #include <string>
 #include <vector>
 
@@ -30,30 +31,8 @@
 namespace ipmi
 {
 
-struct GetFlashSizeRequest
+Resp getFlashSize(const std::vector<std::uint8_t>&, HandlerInterface* handler)
 {
-    uint8_t subcommand;
-} __attribute__((packed));
-
-struct GetFlashSizeReply
-{
-    uint8_t subcommand;
-    uint32_t flashSize;
-} __attribute__((packed));
-
-ipmi_ret_t getFlashSize(const uint8_t* reqBuf, uint8_t* replyBuf,
-                        size_t* dataLen, HandlerInterface* handler)
-{
-    struct GetFlashSizeRequest 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));
     uint32_t flashSize;
     try
     {
@@ -61,15 +40,15 @@
     }
     catch (const IpmiException& e)
     {
-        return e.getIpmiError();
+        return ::ipmi::response(e.getIpmiError());
     }
 
-    auto reply = reinterpret_cast<struct GetFlashSizeReply*>(&replyBuf[0]);
-    reply->subcommand = SysGetFlashSize;
-    reply->flashSize = htole32(flashSize);
-
-    (*dataLen) = sizeof(struct GetFlashSizeReply);
-    return IPMI_CC_OK;
+    flashSize = htole32(flashSize);
+    return ::ipmi::responseSuccess(
+        SysOEMCommands::SysGetFlashSize,
+        std::vector<std::uint8_t>((std::uint8_t*)&(flashSize),
+                                  (std::uint8_t*)&(flashSize) +
+                                      sizeof(std::uint32_t)));
 }
 } // namespace ipmi
 } // namespace google
diff --git a/flash_size.hpp b/flash_size.hpp
index 4e9e2d0..9ae1c5f 100644
--- a/flash_size.hpp
+++ b/flash_size.hpp
@@ -18,13 +18,21 @@
 
 #include <ipmid/api.h>
 
+#include <ipmid/api-types.hpp>
+#include <vector>
+
 namespace google
 {
 namespace ipmi
 {
 
-ipmi_ret_t getFlashSize(const uint8_t* reqBuf, uint8_t* replyBuf,
-                        size_t* dataLen, HandlerInterface* handler);
+struct GetFlashSizeReply
+{
+    uint32_t flashSize;
+} __attribute__((packed));
+
+Resp getFlashSize(const std::vector<std::uint8_t>& data,
+                  HandlerInterface* handler);
 
 } // namespace ipmi
 } // namespace google
diff --git a/handler.hpp b/handler.hpp
index e0d73d7..26950e9 100644
--- a/handler.hpp
+++ b/handler.hpp
@@ -15,7 +15,9 @@
 #pragma once
 
 #include <cstdint>
+#include <ipmid/api-types.hpp>
 #include <map>
+#include <span>
 #include <string>
 #include <tuple>
 #include <vector>
@@ -25,6 +27,8 @@
 namespace ipmi
 {
 
+using Resp = ::ipmi::RspType<std::uint8_t, std::vector<uint8_t>>;
+
 using VersionTuple =
     std::tuple<std::uint8_t, std::uint8_t, std::uint8_t, std::uint8_t>;
 
diff --git a/host_power_off.cpp b/host_power_off.cpp
index 57e45a1..98e5daa 100644
--- a/host_power_off.cpp
+++ b/host_power_off.cpp
@@ -22,38 +22,38 @@
 
 #include <cstdint>
 #include <cstring>
+#include <ipmid/api-types.hpp>
+#include <vector>
 
 namespace google
 {
 namespace ipmi
 {
 
-ipmi_ret_t hostPowerOff(const uint8_t* reqBuf, uint8_t* replyBuf,
-                        size_t* dataLen, const HandlerInterface* handler)
+Resp hostPowerOff(const std::vector<uint8_t>& data,
+                  const HandlerInterface* handler)
 {
     struct HostPowerOffRequest request;
 
-    if ((*dataLen) < sizeof(request))
+    if (data.size() < sizeof(request))
     {
         std::fprintf(stderr, "Invalid command length: %u\n",
-                     static_cast<uint32_t>(*dataLen));
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
+                     static_cast<uint32_t>(data.size()));
+        return ::ipmi::responseReqDataLenInvalid();
     }
 
-    std::memcpy(&request, &reqBuf[0], sizeof(struct HostPowerOffRequest));
+    std::memcpy(&request, data.data(), sizeof(struct HostPowerOffRequest));
     try
     {
         handler->hostPowerOffDelay(request.delay);
     }
     catch (const IpmiException& e)
     {
-        return e.getIpmiError();
+        return ::ipmi::response(e.getIpmiError());
     }
 
-    replyBuf[0] = SysHostPowerOff;
-    (*dataLen) = sizeof(uint8_t);
-
-    return IPMI_CC_OK;
+    return ::ipmi::responseSuccess(SysOEMCommands::SysHostPowerOff,
+                                   std::vector<uint8_t>{});
 }
 } // namespace ipmi
 } // namespace google
diff --git a/host_power_off.hpp b/host_power_off.hpp
index 5b8b71f..49231a3 100644
--- a/host_power_off.hpp
+++ b/host_power_off.hpp
@@ -18,6 +18,9 @@
 
 #include <ipmid/api.h>
 
+#include <ipmid/api-types.hpp>
+#include <vector>
+
 namespace google
 {
 namespace ipmi
@@ -25,14 +28,13 @@
 
 struct HostPowerOffRequest
 {
-    uint8_t subcommand;
     // Delay in seconds.
     uint32_t delay;
 } __attribute__((packed));
 
 // Disable the fallback watchdog with given time delay and Power Off Host
-ipmi_ret_t hostPowerOff(const uint8_t* reqBuf, uint8_t* replyBuf,
-                        size_t* dataLen, const HandlerInterface* handler);
+Resp hostPowerOff(const std::vector<std::uint8_t>& data,
+                  const HandlerInterface* handler);
 
 } // namespace ipmi
 } // namespace google
diff --git a/ipmi.cpp b/ipmi.cpp
index a271d34..0451c6e 100644
--- a/ipmi.cpp
+++ b/ipmi.cpp
@@ -30,52 +30,46 @@
 
 #include <cstdint>
 #include <cstdio>
+#include <ipmid/api-types.hpp>
+#include <ipmid/message.hpp>
+#include <optional>
+#include <vector>
 
 namespace google
 {
 namespace ipmi
 {
 
-ipmi_ret_t handleSysCommand(HandlerInterface* handler, ipmi_cmd_t,
-                            const uint8_t* reqBuf, uint8_t* replyCmdBuf,
-                            size_t* dataLen)
+Resp handleSysCommand(HandlerInterface* handler, ::ipmi::Context::ptr,
+                      uint8_t cmd, const std::vector<uint8_t>& data)
 {
-    // Verify it's at least as long as it needs to be for a subcommand.
-    if ((*dataLen) < 1)
-    {
-        std::fprintf(stderr, "*dataLen too small: %u\n",
-                     static_cast<uint32_t>(*dataLen));
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
-    }
-
-    switch (reqBuf[0])
+    switch (cmd)
     {
         case SysCableCheck:
-            return cableCheck(reqBuf, replyCmdBuf, dataLen, handler);
+            return cableCheck(data, handler);
         case SysCpldVersion:
-            return cpldVersion(reqBuf, replyCmdBuf, dataLen, handler);
+            return cpldVersion(data, handler);
         case SysGetEthDevice:
-            return getEthDevice(reqBuf, replyCmdBuf, dataLen, handler);
+            return getEthDevice(data, handler);
         case SysPsuHardReset:
-            return psuHardReset(reqBuf, replyCmdBuf, dataLen, handler);
+            return psuHardReset(data, handler);
         case SysPcieSlotCount:
-            return pcieSlotCount(reqBuf, replyCmdBuf, dataLen, handler);
+            return pcieSlotCount(data, handler);
         case SysPcieSlotI2cBusMapping:
-            return pcieSlotI2cBusMapping(reqBuf, replyCmdBuf, dataLen, handler);
+            return pcieSlotI2cBusMapping(data, handler);
         case SysEntityName:
-            return getEntityName(reqBuf, replyCmdBuf, dataLen, handler);
+            return getEntityName(data, handler);
         case SysMachineName:
-            return getMachineName(reqBuf, replyCmdBuf, dataLen, handler);
+            return getMachineName(data, handler);
         case SysPsuHardResetOnShutdown:
-            return psuHardResetOnShutdown(reqBuf, replyCmdBuf, dataLen,
-                                          handler);
+            return psuHardResetOnShutdown(data, handler);
         case SysGetFlashSize:
-            return getFlashSize(reqBuf, replyCmdBuf, dataLen, handler);
+            return getFlashSize(data, handler);
         case SysHostPowerOff:
-            return hostPowerOff(reqBuf, replyCmdBuf, dataLen, handler);
+            return hostPowerOff(data, handler);
         default:
-            std::fprintf(stderr, "Invalid subcommand: 0x%x\n", reqBuf[0]);
-            return IPMI_CC_INVALID;
+            std::fprintf(stderr, "Invalid subcommand: 0x%x\n", cmd);
+            return ::ipmi::responseInvalidCommand();
     }
 }
 
diff --git a/ipmi.hpp b/ipmi.hpp
index 62754c2..ef8c627 100644
--- a/ipmi.hpp
+++ b/ipmi.hpp
@@ -18,15 +18,19 @@
 
 #include <ipmid/api.h>
 
+#include <ipmid/api-types.hpp>
+#include <ipmid/message.hpp>
+#include <optional>
+#include <vector>
+
 namespace google
 {
 namespace ipmi
 {
 
 // Handle the google-ipmi-sys IPMI OEM commands.
-ipmi_ret_t handleSysCommand(HandlerInterface* handler, ipmi_cmd_t cmd,
-                            const uint8_t* reqBuf, uint8_t* replyCmdBuf,
-                            size_t* dataLen);
+Resp handleSysCommand(HandlerInterface* handler, ::ipmi::Context::ptr ctx,
+                      uint8_t cmd, const std::vector<uint8_t>& data);
 
 } // namespace ipmi
 } // namespace google
diff --git a/machine_name.cpp b/machine_name.cpp
index c8bf701..c1b9c4e 100644
--- a/machine_name.cpp
+++ b/machine_name.cpp
@@ -14,41 +14,24 @@
 
 #include "machine_name.hpp"
 
+#include "commands.hpp"
 #include "errors.hpp"
 
 #include <cstddef>
 #include <cstdio>
 #include <cstring>
+#include <ipmid/api-types.hpp>
 #include <optional>
 #include <string>
+#include <vector>
 
 namespace google
 {
 namespace ipmi
 {
 
-struct GetMachineNameRequest
+Resp getMachineName(const std::vector<std::uint8_t>&, HandlerInterface* handler)
 {
-    uint8_t subcommand;
-} __attribute__((packed));
-
-struct GetMachineNameReply
-{
-    uint8_t subcommand;
-    uint8_t machineNameLength;
-} __attribute__((packed));
-
-ipmi_ret_t getMachineName(const uint8_t* reqBuf, uint8_t* replyBuf,
-                          size_t* dataLen, HandlerInterface* handler)
-{
-    GetMachineNameRequest request;
-    if (*dataLen < sizeof(request))
-    {
-        std::fprintf(stderr, "Invalid command length: %zu\n", *dataLen);
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
-    }
-    std::memcpy(&request, reqBuf, sizeof(request));
-
     static std::optional<std::string> machineName;
     if (!machineName)
     {
@@ -58,24 +41,24 @@
         }
         catch (const IpmiException& e)
         {
-            return e.getIpmiError();
+            return ::ipmi::response(e.getIpmiError());
         }
     }
 
-    GetMachineNameReply reply;
-    size_t len = sizeof(reply) + machineName->size();
+    size_t len = sizeof(struct GetMachineNameReply) + machineName->size();
     if (len > MAX_IPMI_BUFFER)
     {
         std::fprintf(stderr, "Response would overflow response buffer\n");
-        return IPMI_CC_INVALID;
+        return ::ipmi::responseInvalidCommand();
     }
-    reply.subcommand = request.subcommand;
-    reply.machineNameLength = machineName->size();
-    std::memcpy(replyBuf, &reply, sizeof(reply));
-    std::memcpy(replyBuf + sizeof(reply), machineName->data(),
-                machineName->size());
-    (*dataLen) = len;
-    return IPMI_CC_OK;
+
+    std::vector<std::uint8_t> reply;
+    reply.reserve(len);
+    reply.emplace_back(machineName->size()); /* machineNameLength */
+    reply.insert(reply.end(), machineName->begin(),
+                 machineName->end()); /* machineName */
+
+    return ::ipmi::responseSuccess(SysOEMCommands::SysMachineName, reply);
 }
 
 } // namespace ipmi
diff --git a/machine_name.hpp b/machine_name.hpp
index 61bbbe4..4c8a60c 100644
--- a/machine_name.hpp
+++ b/machine_name.hpp
@@ -18,14 +18,22 @@
 
 #include <ipmid/api.h>
 
+#include <ipmid/api-types.hpp>
+#include <vector>
+
 namespace google
 {
 namespace ipmi
 {
 
+struct GetMachineNameReply
+{
+    uint8_t machineNameLength;
+} __attribute__((packed));
+
 // Handle the machine name command.
-ipmi_ret_t getMachineName(const uint8_t* reqBuf, uint8_t* replyBuf,
-                          size_t* dataLen, HandlerInterface* handler);
+Resp getMachineName(const std::vector<std::uint8_t>& data,
+                    HandlerInterface* handler);
 
 } // namespace ipmi
 } // namespace google
diff --git a/main.cpp b/main.cpp
index 09d0990..63ece82 100644
--- a/main.cpp
+++ b/main.cpp
@@ -20,8 +20,10 @@
 #include <cstdint>
 #include <cstdio>
 #include <functional>
+#include <ipmid/api-types.hpp>
+#include <ipmid/handler.hpp>
 #include <ipmid/iana.hpp>
-#include <ipmid/oemrouter.hpp>
+#include <span>
 
 namespace oem
 {
@@ -41,16 +43,17 @@
 void setupGoogleOemSysCommands()
 {
     static Handler handlerImpl;
-    oem::Router* oemRouter = oem::mutableRouter();
 
     std::fprintf(stderr,
                  "Registering OEM:[%#08X], Cmd:[%#04X] for Sys Commands\n",
                  oem::googOemNumber, oem::google::sysCmd);
 
-    using namespace std::placeholders;
-    oemRouter->registerHandler(
-        oem::googOemNumber, oem::google::sysCmd,
-        std::bind(handleSysCommand, &handlerImpl, _1, _2, _3, _4));
+    ::ipmi::registerOemHandler(
+        ::ipmi::prioOemBase, oem::googOemNumber, oem::google::sysCmd,
+        ::ipmi::Privilege::User,
+        [](::ipmi::Context::ptr ctx, uint8_t cmd, std::vector<uint8_t> data) {
+            return handleSysCommand(&handlerImpl, ctx, cmd, data);
+        });
 }
 
 } // namespace ipmi
diff --git a/pcie_i2c.cpp b/pcie_i2c.cpp
index 4c6fed1..46088a5 100644
--- a/pcie_i2c.cpp
+++ b/pcie_i2c.cpp
@@ -15,9 +15,11 @@
 #include "pcie_i2c.hpp"
 
 #include "commands.hpp"
+#include "handler.hpp"
 
 #include <cstdint>
 #include <cstring>
+#include <ipmid/api-types.hpp>
 #include <string>
 #include <tuple>
 #include <vector>
@@ -31,82 +33,49 @@
 #define MAX_IPMI_BUFFER 64
 #endif
 
-struct PcieSlotCountRequest
-{
-    uint8_t subcommand;
-} __attribute__((packed));
-
-struct PcieSlotCountReply
-{
-    uint8_t subcommand;
-    uint8_t value;
-} __attribute__((packed));
-
 struct PcieSlotI2cBusMappingRequest
 {
-    uint8_t subcommand;
     uint8_t entry;
 } __attribute__((packed));
 
-struct PcieSlotI2cBusMappingReply
+Resp pcieSlotCount(const std::vector<std::uint8_t>&, HandlerInterface* handler)
 {
-    uint8_t subcommand;
-    uint8_t i2c_bus_number;
-    uint8_t pcie_slot_name_len;
-} __attribute__((packed));
-
-ipmi_ret_t pcieSlotCount(const uint8_t*, uint8_t* replyBuf, size_t* dataLen,
-                         HandlerInterface* handler)
-{
-    if ((*dataLen) < sizeof(struct PcieSlotCountRequest))
-    {
-        std::fprintf(stderr, "Invalid command length: %u\n",
-                     static_cast<uint32_t>(*dataLen));
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
-    }
-
     // If there are already entries in the vector, clear them.
     handler->buildI2cPcieMapping();
 
-    struct PcieSlotCountReply reply;
-    reply.subcommand = SysPcieSlotCount;
     // Fill the pcie slot count as the number of entries in the vector.
-    reply.value = handler->getI2cPcieMappingSize();
+    std::uint8_t value = handler->getI2cPcieMappingSize();
 
-    std::memcpy(&replyBuf[0], &reply, sizeof(reply));
-
-    // Return the subcommand and the result.
-    (*dataLen) = sizeof(reply);
-
-    return IPMI_CC_OK;
+    return ::ipmi::responseSuccess(SysOEMCommands::SysPcieSlotCount,
+                                   std::vector<std::uint8_t>{value});
 }
 
-ipmi_ret_t pcieSlotI2cBusMapping(const uint8_t* reqBuf, uint8_t* replyBuf,
-                                 size_t* dataLen, HandlerInterface* handler)
+Resp pcieSlotI2cBusMapping(const std::vector<std::uint8_t>& data,
+                           HandlerInterface* handler)
 {
     struct PcieSlotI2cBusMappingRequest request;
 
-    if ((*dataLen) < sizeof(request))
+    if (data.size() < sizeof(request))
     {
         std::fprintf(stderr, "Invalid command length: %u\n",
-                     static_cast<uint32_t>(*dataLen));
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
+                     static_cast<uint32_t>(data.size()));
+        return ::ipmi::responseReqDataLenInvalid();
     }
 
     // If there are no entries in the vector return error.
     size_t mapSize = handler->getI2cPcieMappingSize();
     if (mapSize == 0)
     {
-        return IPMI_CC_INVALID_RESERVATION_ID;
+        return ::ipmi::responseInvalidReservationId();
     }
 
-    std::memcpy(&request, &reqBuf[0], sizeof(request));
+    std::memcpy(&request, data.data(), sizeof(request));
 
     // The valid entries range from 0 to N - 1, N being the total number of
     // entries in the vector.
     if (request.entry >= mapSize)
     {
-        return IPMI_CC_PARM_OUT_OF_RANGE;
+        return ::ipmi::responseParmOutOfRange();
     }
 
     // Get the i2c bus number and the pcie slot name from the vector.
@@ -122,20 +91,20 @@
     if (length > MAX_IPMI_BUFFER)
     {
         std::fprintf(stderr, "Response would overflow response buffer\n");
-        return IPMI_CC_INVALID;
+        return ::ipmi::responseInvalidCommand();
     }
 
-    auto reply =
-        reinterpret_cast<struct PcieSlotI2cBusMappingReply*>(&replyBuf[0]);
-    reply->subcommand = SysPcieSlotI2cBusMapping;
+    std::vector<std::uint8_t> reply;
+    reply.reserve(pcie_slot_name.length() +
+                  sizeof(struct PcieSlotI2cBusMappingReply));
     // Copy the i2c bus number and the pcie slot name to the reply struct.
-    reply->i2c_bus_number = i2c_bus_number;
-    reply->pcie_slot_name_len = pcie_slot_name.length();
-    std::memcpy(reply + 1, pcie_slot_name.c_str(), pcie_slot_name.length());
+    reply.emplace_back(i2c_bus_number);          /* i2c_bus_number */
+    reply.emplace_back(pcie_slot_name.length()); /* pcie_slot_name length */
+    reply.insert(reply.end(), pcie_slot_name.begin(),
+                 pcie_slot_name.end()); /* pcie_slot_name */
 
-    // Return the subcommand and the result.
-    (*dataLen) = length;
-    return IPMI_CC_OK;
+    return ::ipmi::responseSuccess(SysOEMCommands::SysPcieSlotI2cBusMapping,
+                                   reply);
 }
 } // namespace ipmi
 } // namespace google
diff --git a/pcie_i2c.hpp b/pcie_i2c.hpp
index 2b572b9..5f1cd35 100644
--- a/pcie_i2c.hpp
+++ b/pcie_i2c.hpp
@@ -18,20 +18,34 @@
 
 #include <ipmid/api.h>
 
+#include <ipmid/api-types.hpp>
+#include <vector>
+
 namespace google
 {
 namespace ipmi
 {
 
+struct PcieSlotCountReply
+{
+    uint8_t value;
+} __attribute__((packed));
+
+struct PcieSlotI2cBusMappingReply
+{
+    uint8_t i2c_bus_number;
+    uint8_t pcie_slot_name_len;
+} __attribute__((packed));
+
 //  Handle the pcie slot count command.
 //  Sys can query the number of pcie slots.
-ipmi_ret_t pcieSlotCount(const uint8_t* reqBuf, uint8_t* replyBuf,
-                         size_t* dataLen, HandlerInterface* handler);
+Resp pcieSlotCount(const std::vector<std::uint8_t>& data,
+                   HandlerInterface* handler);
 
 // Handle the pcie slot to i2c bus mapping command.
 // Sys can query which i2c bus is routed to which pcie slot.
-ipmi_ret_t pcieSlotI2cBusMapping(const uint8_t* reqBuf, uint8_t* replyBuf,
-                                 size_t* dataLen, HandlerInterface* handler);
+Resp pcieSlotI2cBusMapping(const std::vector<std::uint8_t>& data,
+                           HandlerInterface* handler);
 
 } // namespace ipmi
 } // namespace google
diff --git a/psu.cpp b/psu.cpp
index 085c259..1e2ed92 100644
--- a/psu.cpp
+++ b/psu.cpp
@@ -20,67 +20,54 @@
 
 #include <cstdint>
 #include <cstring>
+#include <ipmid/api-types.hpp>
+#include <vector>
 
 namespace google
 {
 namespace ipmi
 {
 
-ipmi_ret_t psuHardReset(const uint8_t* reqBuf, uint8_t* replyBuf,
-                        size_t* dataLen, const HandlerInterface* handler)
+Resp psuHardReset(const std::vector<std::uint8_t>& data,
+                  const HandlerInterface* handler)
 {
     struct PsuResetRequest request;
 
-    if ((*dataLen) < sizeof(request))
+    if (data.size() < sizeof(request))
     {
         std::fprintf(stderr, "Invalid command length: %u\n",
-                     static_cast<uint32_t>(*dataLen));
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
+                     static_cast<uint32_t>(data.size()));
+        return ::ipmi::responseReqDataLenInvalid();
     }
 
-    std::memcpy(&request, &reqBuf[0], sizeof(struct PsuResetRequest));
+    std::memcpy(&request, data.data(), sizeof(struct PsuResetRequest));
     try
     {
         handler->psuResetDelay(request.delay);
     }
     catch (const IpmiException& e)
     {
-        return e.getIpmiError();
+        return ::ipmi::response(e.getIpmiError());
     }
 
-    replyBuf[0] = SysPsuHardReset;
-    (*dataLen) = sizeof(uint8_t);
-
-    return IPMI_CC_OK;
+    return ::ipmi::responseSuccess(SysOEMCommands::SysPsuHardReset,
+                                   std::vector<uint8_t>{});
 }
 
-ipmi_ret_t psuHardResetOnShutdown(const uint8_t* reqBuf, uint8_t* replyBuf,
-                                  size_t* dataLen,
-                                  const HandlerInterface* handler)
+Resp psuHardResetOnShutdown(const std::vector<std::uint8_t>&,
+                            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();
+        return ::ipmi::response(e.getIpmiError());
     }
 
-    replyBuf[0] = SysPsuHardResetOnShutdown;
-    (*dataLen) = sizeof(uint8_t);
-
-    return IPMI_CC_OK;
+    return ::ipmi::responseSuccess(SysOEMCommands::SysPsuHardResetOnShutdown,
+                                   std::vector<uint8_t>{});
 }
 
 } // namespace ipmi
diff --git a/psu.hpp b/psu.hpp
index f0d5775..b022881 100644
--- a/psu.hpp
+++ b/psu.hpp
@@ -18,6 +18,9 @@
 
 #include <ipmid/api.h>
 
+#include <ipmid/api-types.hpp>
+#include <vector>
+
 namespace google
 {
 namespace ipmi
@@ -25,24 +28,17 @@
 
 struct PsuResetRequest
 {
-    uint8_t subcommand;
     // Delay in seconds.
     uint32_t delay;
 } __attribute__((packed));
 
-struct PsuResetOnShutdownRequest
-{
-    uint8_t subcommand;
-} __attribute__((packed));
-
 // Set a time-delayed PSU hard reset.
-ipmi_ret_t psuHardReset(const uint8_t* reqBuf, uint8_t* replyBuf,
-                        size_t* dataLen, const HandlerInterface* handler);
+Resp psuHardReset(const std::vector<std::uint8_t>& data,
+                  const HandlerInterface* handler);
 
 // Arm for PSU hard reset on host shutdown.
-ipmi_ret_t psuHardResetOnShutdown(const uint8_t* reqBuf, uint8_t* replyBuf,
-                                  size_t* dataLen,
-                                  const HandlerInterface* handler);
+Resp psuHardResetOnShutdown(const std::vector<std::uint8_t>& data,
+                            const HandlerInterface* handler);
 
 } // namespace ipmi
 } // namespace google
diff --git a/test/cable_unittest.cpp b/test/cable_unittest.cpp
index 06b4706..27124ad 100644
--- a/test/cable_unittest.cpp
+++ b/test/cable_unittest.cpp
@@ -15,15 +15,15 @@
 #include "cable.hpp"
 #include "commands.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 
 #include <cstdint>
 #include <cstring>
+#include <tuple>
 #include <vector>
 
 #include <gtest/gtest.h>
 
-#define MAX_IPMI_BUFFER 64
-
 using ::testing::Return;
 using ::testing::StrEq;
 
@@ -34,69 +34,49 @@
 
 TEST(CableCommandTest, RequestTooSmall)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
+    std::vector<std::uint8_t> request = {};
     HandlerMock hMock;
 
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              cableCheck(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), cableCheck(request, &hMock));
 }
 
 TEST(CableCommandTest, FailsLengthSanityCheck)
 {
     // Minimum is three bytes, but a length of zero for the string is invalid.
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x00,
-                                         'a'};
-
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
+    std::vector<std::uint8_t> request = {0x00, 'a'};
     HandlerMock hMock;
 
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              cableCheck(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), cableCheck(request, &hMock));
 }
 
 TEST(CableCommandTest, LengthTooLongForPacket)
 {
     // The length of a the string, as specified is longer than string provided.
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x02,
-                                         'a'};
-
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
+    std::vector<std::uint8_t> request = {0x02, 'a'};
     HandlerMock hMock;
 
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              cableCheck(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), cableCheck(request, &hMock));
 }
 
 TEST(CableCommandTest, ValidRequestValidReturn)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x01,
-                                         'a'};
-
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {0x01, 'a'};
 
     HandlerMock hMock;
 
     EXPECT_CALL(hMock, getRxPackets(StrEq("a"))).WillOnce(Return(0));
-    EXPECT_EQ(IPMI_CC_OK, cableCheck(request.data(), reply, &dataLen, &hMock));
 
     // Check results.
-    struct CableReply expectedReply, actualReply;
-    expectedReply.subcommand = SysOEMCommands::SysCableCheck;
+    struct CableReply expectedReply;
     expectedReply.value = 0;
 
-    EXPECT_EQ(sizeof(expectedReply), dataLen);
-    std::memcpy(&actualReply, reply, dataLen);
+    auto reply = cableCheck(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
 
-    EXPECT_EQ(expectedReply.subcommand, actualReply.subcommand);
-    EXPECT_EQ(expectedReply.value, actualReply.value);
+    EXPECT_EQ(sizeof(struct CableReply), data.size());
+    EXPECT_EQ(SysOEMCommands::SysCableCheck, result.first);
+    EXPECT_EQ(expectedReply.value, data[0]);
 }
 
 } // namespace ipmi
diff --git a/test/cpld_unittest.cpp b/test/cpld_unittest.cpp
index f9ab539..5747ce5 100644
--- a/test/cpld_unittest.cpp
+++ b/test/cpld_unittest.cpp
@@ -15,6 +15,7 @@
 #include "commands.hpp"
 #include "cpld.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 
 #include <cstdint>
 #include <tuple>
@@ -22,8 +23,6 @@
 
 #include <gtest/gtest.h>
 
-#define MAX_IPMI_BUFFER 64
-
 using ::testing::Return;
 
 namespace google
@@ -33,20 +32,17 @@
 
 TEST(CpldCommandTest, RequestTooSmall)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysCpldVersion};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
+    std::vector<std::uint8_t> request = {};
     HandlerMock hMock;
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              cpldVersion(request.data(), reply, &dataLen, &hMock));
+
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
+              cpldVersion(request, &hMock));
 }
 
 TEST(CpldCommandTest, ValidRequestReturnsHappy)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysCpldVersion, 0x04};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {0x04};
+
     std::uint8_t expectedMaj = 0x5;
     std::uint8_t expectedMin = 0x3;
     std::uint8_t expectedPt = 0x7;
@@ -57,11 +53,18 @@
         .WillOnce(Return(std::make_tuple(expectedMaj, expectedMin, expectedPt,
                                          expectedSbPtr)));
 
-    EXPECT_EQ(IPMI_CC_OK, cpldVersion(request.data(), reply, &dataLen, &hMock));
-    EXPECT_EQ(expectedMaj, reply[1]);
-    EXPECT_EQ(expectedMin, reply[2]);
-    EXPECT_EQ(expectedPt, reply[3]);
-    EXPECT_EQ(expectedSbPtr, reply[4]);
+    // Reply is in the form of
+    // std::tuple<ipmi::Cc, std::optional<std::tuple<RetTypes...>>>
+    auto reply = cpldVersion(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(sizeof(struct CpldReply), data.size());
+    EXPECT_EQ(SysOEMCommands::SysCpldVersion, result.first);
+    EXPECT_EQ(expectedMaj, data[0]);
+    EXPECT_EQ(expectedMin, data[1]);
+    EXPECT_EQ(expectedPt, data[2]);
+    EXPECT_EQ(expectedSbPtr, data[3]);
 }
 
 } // namespace ipmi
diff --git a/test/entity_unittest.cpp b/test/entity_unittest.cpp
index a939a8a..7b7e843 100644
--- a/test/entity_unittest.cpp
+++ b/test/entity_unittest.cpp
@@ -15,6 +15,7 @@
 #include "commands.hpp"
 #include "entity_name.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 
 #include <cstdint>
 #include <cstring>
@@ -35,32 +36,34 @@
 TEST(EntityNameCommandTest, InvalidCommandLength)
 {
     // GetEntityNameRequest is three bytes, let's send 2.
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysEntityName, 0x01};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
+    std::vector<std::uint8_t> request = {0x01};
     HandlerMock hMock;
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              getEntityName(request.data(), reply, &dataLen, &hMock));
+
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
+              getEntityName(request, &hMock));
 }
 
 TEST(EntityNameCommandTest, ValidRequest)
 {
     std::uint8_t entityId = 3;
     std::uint8_t entityInstance = 5;
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysEntityName,
-                                         entityId, entityInstance};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {entityId, entityInstance};
     std::string entityName = "asdf";
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, getEntityName(entityId, entityInstance))
         .WillOnce(Return(entityName));
-    EXPECT_EQ(IPMI_CC_OK,
-              getEntityName(request.data(), reply, &dataLen, &hMock));
-    EXPECT_EQ(reply[1], entityName.length());
-    EXPECT_EQ(0, std::memcmp(&reply[2], entityName.c_str(), reply[1]));
+
+    auto reply = getEntityName(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(sizeof(GetEntityNameReply) + entityName.size(), data.size());
+    EXPECT_EQ(SysOEMCommands::SysEntityName, result.first);
+    EXPECT_EQ(entityName.length(), data[0]);
+    EXPECT_EQ(entityName.data(),
+              std::string(data.begin() + sizeof(struct GetEntityNameReply),
+                          data.end()));
 }
 
 } // namespace ipmi
diff --git a/test/eth_unittest.cpp b/test/eth_unittest.cpp
index 2b5192a..14b0875 100644
--- a/test/eth_unittest.cpp
+++ b/test/eth_unittest.cpp
@@ -15,17 +15,16 @@
 #include "commands.hpp"
 #include "eth.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 
 #include <cstdint>
 #include <cstring>
-#include <string>
+#include <string_view>
 #include <tuple>
 #include <vector>
 
 #include <gtest/gtest.h>
 
-#define MAX_IPMI_BUFFER 64
-
 using ::testing::Return;
 
 namespace google
@@ -37,54 +36,50 @@
 {
     // This command requests no input, therefore it will just return what it
     // knows.
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysGetEthDevice};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-    const std::uint8_t expectedAnswer[4] = {'e', 't', 'h', '0'};
+    std::vector<std::uint8_t> request = {};
+    const std::string_view expectedAnswer = "eth0";
     const std::uint8_t expectedChannel = 14;
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, getEthDetails(""))
-        .WillOnce(Return(std::make_tuple(
-            expectedChannel,
-            std::string(expectedAnswer,
-                        expectedAnswer + sizeof(expectedAnswer)))));
+        .WillOnce(
+            Return(std::make_tuple(expectedChannel, expectedAnswer.data())));
 
-    EXPECT_EQ(IPMI_CC_OK,
-              getEthDevice(request.data(), &reply[0], &dataLen, &hMock));
-    struct EthDeviceReply check;
-    std::memcpy(&check, &reply[0], sizeof(check));
-    EXPECT_EQ(check.subcommand, SysOEMCommands::SysGetEthDevice);
-    EXPECT_EQ(check.channel, expectedChannel);
-    EXPECT_EQ(check.ifNameLength, sizeof(expectedAnswer));
-    EXPECT_EQ(0, std::memcmp(expectedAnswer, &reply[sizeof(check)],
-                             sizeof(expectedAnswer)));
+    auto reply = getEthDevice(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(sizeof(EthDeviceReply) + expectedAnswer.size(), data.size());
+    EXPECT_EQ(SysOEMCommands::SysGetEthDevice, result.first);
+    EXPECT_EQ(expectedChannel, data[0]);
+    EXPECT_EQ(expectedAnswer.size(), data[1]);
+    EXPECT_EQ(
+        expectedAnswer.data(),
+        std::string(data.begin() + sizeof(struct EthDeviceReply), data.end()));
 }
 
 TEST(EthCommandTest, ValidPopulatedReturnsSuccess)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysGetEthDevice, 'e'};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-    const std::uint8_t expectedAnswer[1] = {'e'};
+    std::vector<std::uint8_t> request = {'e'};
+    const std::string_view expectedAnswer = "e";
     const std::uint8_t expectedChannel = 11;
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, getEthDetails("e"))
-        .WillOnce(Return(std::make_tuple(
-            expectedChannel,
-            std::string(expectedAnswer,
-                        expectedAnswer + sizeof(expectedAnswer)))));
+        .WillOnce(
+            Return(std::make_tuple(expectedChannel, expectedAnswer.data())));
 
-    EXPECT_EQ(IPMI_CC_OK,
-              getEthDevice(request.data(), &reply[0], &dataLen, &hMock));
-    struct EthDeviceReply check;
-    std::memcpy(&check, &reply[0], sizeof(check));
-    EXPECT_EQ(check.subcommand, SysOEMCommands::SysGetEthDevice);
-    EXPECT_EQ(check.channel, expectedChannel);
-    EXPECT_EQ(check.ifNameLength, sizeof(expectedAnswer));
-    EXPECT_EQ(0, std::memcmp(expectedAnswer, &reply[sizeof(check)],
-                             sizeof(expectedAnswer)));
+    auto reply = getEthDevice(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(sizeof(EthDeviceReply) + expectedAnswer.size(), data.size());
+    EXPECT_EQ(SysOEMCommands::SysGetEthDevice, result.first);
+    EXPECT_EQ(expectedChannel, data[0]);
+    EXPECT_EQ(expectedAnswer.size(), data[1]);
+    EXPECT_EQ(
+        expectedAnswer.data(),
+        std::string(data.begin() + sizeof(struct EthDeviceReply), data.end()));
 }
 } // namespace ipmi
 } // namespace google
diff --git a/test/flash_unittest.cpp b/test/flash_unittest.cpp
index f4337f7..3d575df 100644
--- a/test/flash_unittest.cpp
+++ b/test/flash_unittest.cpp
@@ -15,6 +15,7 @@
 #include "commands.hpp"
 #include "flash_size.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 
 #include <cstdint>
 #include <cstring>
@@ -23,8 +24,6 @@
 
 #include <gtest/gtest.h>
 
-#define MAX_IPMI_BUFFER 64
-
 using ::testing::Return;
 
 namespace google
@@ -32,34 +31,24 @@
 namespace ipmi
 {
 
-TEST(FlashSizeCommandTest, InvalidCommandLength)
-{
-    // GetFlashSizeRequest is one byte, let's send 0.
-    std::vector<std::uint8_t> request = {};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
-    HandlerMock hMock;
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              getFlashSize(request.data(), reply, &dataLen, &hMock));
-}
-
 TEST(FlashSizeCommandTest, ValidRequest)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysGetFlashSize};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {};
     uint32_t flashSize = 5422312; // 0x52BCE8
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, getFlashSize()).WillOnce(Return(flashSize));
-    EXPECT_EQ(IPMI_CC_OK,
-              getFlashSize(request.data(), reply, &dataLen, &hMock));
-    EXPECT_EQ(dataLen, 5);
-    EXPECT_EQ(reply[4], 0);
-    EXPECT_EQ(reply[3], 0x52);
-    EXPECT_EQ(reply[2], 0xBC);
-    EXPECT_EQ(reply[1], 0xE8);
+
+    auto reply = getFlashSize(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(sizeof(struct GetFlashSizeReply), data.size());
+    EXPECT_EQ(SysOEMCommands::SysGetFlashSize, result.first);
+    EXPECT_EQ(0, data[3]);
+    EXPECT_EQ(0x52, data[2]);
+    EXPECT_EQ(0xBC, data[1]);
+    EXPECT_EQ(0xE8, data[0]);
 }
 
 } // namespace ipmi
diff --git a/test/helper.cpp b/test/helper.cpp
new file mode 100644
index 0000000..6f16b55
--- /dev/null
+++ b/test/helper.cpp
@@ -0,0 +1,32 @@
+#include "helper.hpp"
+
+#include <ipmid/api-types.hpp>
+#include <optional>
+#include <span>
+#include <utility>
+
+namespace google
+{
+namespace ipmi
+{
+
+std::pair<std::uint8_t, std::vector<std::uint8_t>>
+    ValidateReply(::ipmi::RspType<std::uint8_t, std::vector<uint8_t>> reply,
+                  bool hasData)
+{
+    // Reply is in the form of
+    // std::tuple<ipmi::Cc, std::optional<std::tuple<RetTypes...>>>
+    EXPECT_EQ(::ipmi::ccSuccess, std::get<0>(reply));
+
+    auto actualReply = std::get<1>(reply);
+    EXPECT_TRUE(actualReply.has_value());
+
+    auto subcommand = std::get<0>(*actualReply);
+    auto data = std::get<1>(*actualReply);
+    EXPECT_EQ(hasData, !data.empty());
+
+    return std::make_pair(subcommand, hasData ? data : std::vector<uint8_t>{});
+}
+
+} // namespace ipmi
+} // namespace google
diff --git a/test/helper.hpp b/test/helper.hpp
new file mode 100644
index 0000000..f8fb170
--- /dev/null
+++ b/test/helper.hpp
@@ -0,0 +1,23 @@
+#pragma once
+
+#include "handler_mock.hpp"
+
+#include <ipmid/api-types.hpp>
+#include <span>
+#include <utility>
+
+#include <gtest/gtest.h>
+
+namespace google
+{
+namespace ipmi
+{
+
+// Validate the return code and the data for the IPMI reply.
+// Returns the subcommand and the optional informations.
+std::pair<std::uint8_t, std::vector<std::uint8_t>>
+    ValidateReply(::ipmi::RspType<std::uint8_t, std::vector<uint8_t>> reply,
+                  bool hasData = true);
+
+} // namespace ipmi
+} // namespace google
diff --git a/test/machine_unittest.cpp b/test/machine_unittest.cpp
index 924c25a..3dfd3c0 100644
--- a/test/machine_unittest.cpp
+++ b/test/machine_unittest.cpp
@@ -15,6 +15,7 @@
 #include "commands.hpp"
 #include "errors.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 #include "machine_name.hpp"
 
 #include <cstdint>
@@ -34,53 +35,33 @@
 namespace ipmi
 {
 
-TEST(MachineNameCommandTest, InvalidCommandLength)
-{
-    std::vector<std::uint8_t> request = {};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
-    ::testing::StrictMock<HandlerMock> hMock;
-
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              getMachineName(request.data(), reply, &dataLen, &hMock));
-}
-
 TEST(MachineNameCommandTest, InvalidFile)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysMachineName};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
+    std::vector<std::uint8_t> request = {};
     ::testing::StrictMock<HandlerMock> hMock;
     EXPECT_CALL(hMock, getMachineName()).WillOnce(Throw(IpmiException(5)));
 
-    EXPECT_EQ(5, getMachineName(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(::ipmi::response(5), getMachineName(request, &hMock));
 }
 
 TEST(MachineNameCommandTest, CachesValidRequest)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysMachineName};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {};
     const std::string ret = "Machine";
 
     ::testing::StrictMock<HandlerMock> hMock;
     EXPECT_CALL(hMock, getMachineName()).WillOnce(Return(ret));
 
-    EXPECT_EQ(IPMI_CC_OK,
-              getMachineName(request.data(), reply, &dataLen, &hMock));
-    EXPECT_EQ(SysOEMCommands::SysMachineName, reply[0]);
-    EXPECT_EQ(ret.size(), reply[1]);
-    EXPECT_EQ(0, std::memcmp(&reply[2], ret.data(), ret.size()));
+    auto reply = getMachineName(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
 
-    dataLen = request.size();
-    memset(reply, 0, sizeof(reply));
-    EXPECT_EQ(IPMI_CC_OK,
-              getMachineName(request.data(), reply, &dataLen, &hMock));
-    EXPECT_EQ(SysOEMCommands::SysMachineName, reply[0]);
-    EXPECT_EQ(ret.size(), reply[1]);
-    EXPECT_EQ(0, std::memcmp(&reply[2], ret.data(), ret.size()));
+    EXPECT_EQ(sizeof(GetMachineNameReply) + ret.size(), data.size());
+    EXPECT_EQ(SysOEMCommands::SysMachineName, result.first);
+    EXPECT_EQ(ret.length(), data[0]);
+    EXPECT_EQ(ret.data(),
+              std::string(data.begin() + sizeof(struct GetMachineNameReply),
+                          data.end()));
 }
 
 } // namespace ipmi
diff --git a/test/meson.build b/test/meson.build
index 6630893..a9da8b9 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -7,6 +7,7 @@
 tests_lib = static_library(
   'common',
   'common.cpp',
+  'helper.cpp',
   implicit_include_directories: false,
   dependencies: tests_pre)
 
diff --git a/test/pcie_unittest.cpp b/test/pcie_unittest.cpp
index 1eb8fd8..26c3725 100644
--- a/test/pcie_unittest.cpp
+++ b/test/pcie_unittest.cpp
@@ -14,6 +14,7 @@
 
 #include "commands.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 #include "pcie_i2c.hpp"
 
 #include <cstdint>
@@ -23,8 +24,6 @@
 
 #include <gtest/gtest.h>
 
-#define MAX_IPMI_BUFFER 64
-
 using ::testing::Return;
 
 namespace google
@@ -34,67 +33,57 @@
 
 TEST(PcieI2cCommandTest, PcieSlotCountTest)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysPcieSlotCount};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {};
     size_t expectedSize = 3;
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, buildI2cPcieMapping());
     EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(expectedSize));
-    EXPECT_EQ(IPMI_CC_OK,
-              pcieSlotCount(request.data(), reply, &dataLen, &hMock));
-    EXPECT_EQ(expectedSize, reply[1]);
+
+    auto reply = pcieSlotCount(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(sizeof(struct PcieSlotCountReply), data.size());
+    EXPECT_EQ(SysOEMCommands::SysPcieSlotCount, result.first);
+    EXPECT_EQ(expectedSize, data[0]);
 }
 
 TEST(PcieI2cCommandTest, PcieSlotEntryRequestTooShort)
 {
-    std::vector<std::uint8_t> request = {
-        SysOEMCommands::SysPcieSlotI2cBusMapping};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {};
 
     HandlerMock hMock;
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
+              pcieSlotI2cBusMapping(request, &hMock));
 }
 
 TEST(PcieI2cCommandTest, PcieSlotEntryRequestUnsupportedByPlatform)
 {
     // If there is no mapping in the device-tree, then the map is of size zero.
-    std::vector<std::uint8_t> request = {
-        SysOEMCommands::SysPcieSlotI2cBusMapping, 0};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {0};
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(0));
-    EXPECT_EQ(IPMI_CC_INVALID_RESERVATION_ID,
-              pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(::ipmi::responseInvalidReservationId(),
+              pcieSlotI2cBusMapping(request, &hMock));
 }
 
 TEST(PcieI2cCommandTest, PcieSlotEntryRequestInvalidIndex)
 {
     // index of 1 is invalid if length is 1.
-    std::vector<std::uint8_t> request = {
-        SysOEMCommands::SysPcieSlotI2cBusMapping, 1};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {1};
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1));
-    EXPECT_EQ(IPMI_CC_PARM_OUT_OF_RANGE,
-              pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(::ipmi::responseParmOutOfRange(),
+              pcieSlotI2cBusMapping(request, &hMock));
 }
 
 TEST(PcieI2cCommandTest, PcieSlotEntryRequestValidIndex)
 {
     unsigned int index = 0;
-    std::vector<std::uint8_t> request = {
-        SysOEMCommands::SysPcieSlotI2cBusMapping,
-        static_cast<std::uint8_t>(index)};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    std::vector<std::uint8_t> request = {static_cast<std::uint8_t>(index)};
     std::string slotName = "abcd";
     std::uint32_t busNum = 5;
 
@@ -102,11 +91,18 @@
     EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1));
     EXPECT_CALL(hMock, getI2cEntry(index))
         .WillOnce(Return(std::make_tuple(busNum, slotName)));
-    EXPECT_EQ(IPMI_CC_OK,
-              pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
-    EXPECT_EQ(busNum, reply[1]);
-    EXPECT_EQ(slotName.length(), reply[2]);
-    EXPECT_EQ(0, std::memcmp(slotName.c_str(), &reply[3], reply[2]));
+
+    auto reply = pcieSlotI2cBusMapping(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(SysOEMCommands::SysPcieSlotI2cBusMapping, result.first);
+    EXPECT_EQ(busNum, data[0]);
+    EXPECT_EQ(slotName.length(), data[1]);
+    EXPECT_EQ(
+        slotName,
+        std::string(data.begin() + sizeof(struct PcieSlotI2cBusMappingReply),
+                    data.end()));
 }
 
 } // namespace ipmi
diff --git a/test/poweroff_unittest.cpp b/test/poweroff_unittest.cpp
index 4bbd40a..6a90fc6 100644
--- a/test/poweroff_unittest.cpp
+++ b/test/poweroff_unittest.cpp
@@ -14,6 +14,7 @@
 
 #include "commands.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 #include "host_power_off.hpp"
 
 #include <cstdint>
@@ -22,8 +23,6 @@
 
 #include <gtest/gtest.h>
 
-#define MAX_IPMI_BUFFER 64
-
 namespace google
 {
 namespace ipmi
@@ -32,12 +31,10 @@
 TEST(PowerOffCommandTest, InvalidRequestLength)
 {
     std::vector<std::uint8_t> request = {SysOEMCommands::SysHostPowerOff};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
     HandlerMock hMock;
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              hostPowerOff(request.data(), reply, &dataLen, &hMock));
+
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
+              hostPowerOff(request, &hMock));
 }
 
 TEST(PowerOffCommandTest, ValidRequest)
@@ -45,18 +42,17 @@
     // Set the dealy to 15 mins
     std::uint32_t delayValue = 0x384;
     struct HostPowerOffRequest requestContents;
-    requestContents.subcommand = SysOEMCommands::SysHostPowerOff;
     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, hostPowerOffDelay(delayValue));
-    EXPECT_EQ(IPMI_CC_OK,
-              hostPowerOff(request.data(), reply, &dataLen, &hMock));
+
+    auto reply = hostPowerOff(request, &hMock);
+    auto result = ValidateReply(reply, false);
+    EXPECT_EQ(SysOEMCommands::SysHostPowerOff, result.first);
 }
 
 } // namespace ipmi
diff --git a/test/psu_unittest.cpp b/test/psu_unittest.cpp
index 257baad..a08f040 100644
--- a/test/psu_unittest.cpp
+++ b/test/psu_unittest.cpp
@@ -14,6 +14,7 @@
 
 #include "commands.hpp"
 #include "handler_mock.hpp"
+#include "helper.hpp"
 #include "psu.hpp"
 
 #include <cstdint>
@@ -22,8 +23,6 @@
 
 #include <gtest/gtest.h>
 
-#define MAX_IPMI_BUFFER 64
-
 using ::testing::Return;
 
 namespace google
@@ -33,58 +32,41 @@
 
 TEST(PsuCommandTest, InvalidRequestLength)
 {
-    std::vector<std::uint8_t> request = {SysOEMCommands::SysPsuHardReset};
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
-
+    std::vector<std::uint8_t> request = {};
     HandlerMock hMock;
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              psuHardReset(request.data(), reply, &dataLen, &hMock));
+
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
+              psuHardReset(request, &hMock));
 }
 
 TEST(PsuCommandTest, ValidRequest)
 {
-    std::uint32_t delayValue = 0x45;
+    std::uint8_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));
-}
 
-TEST(PsuResetOnShutdownCommandTest, InvalidRequestLength)
-{
-    std::vector<std::uint8_t> request;
-    size_t dataLen = request.size();
-    std::uint8_t reply[MAX_IPMI_BUFFER];
+    auto reply = psuHardReset(request, &hMock);
+    auto result = ValidateReply(reply, /*hasData=*/false);
 
-    HandlerMock hMock;
-    EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
-              psuHardResetOnShutdown(request.data(), reply, &dataLen, &hMock));
+    EXPECT_EQ(SysOEMCommands::SysPsuHardReset, result.first);
 }
 
 TEST(PsuResetOnShutdownCommandTest, ValidRequest)
 {
-    struct PsuResetOnShutdownRequest requestContents;
-    requestContents.subcommand = SysOEMCommands::SysPsuHardReset;
-
-    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];
+    std::vector<std::uint8_t> request = {};
 
     HandlerMock hMock;
     EXPECT_CALL(hMock, psuResetOnShutdown());
-    EXPECT_EQ(IPMI_CC_OK,
-              psuHardResetOnShutdown(request.data(), reply, &dataLen, &hMock));
+
+    auto reply = psuHardResetOnShutdown(request, &hMock);
+    auto result = ValidateReply(reply, /*hasData=*/false);
+
+    EXPECT_EQ(SysOEMCommands::SysPsuHardResetOnShutdown, result.first);
 }
 
 } // namespace ipmi