SmbiosMdrv2Handler:Move mdr2GetDataBlock to new API

Rewrite mdr2GetDataBlock command to use the newly introduced IPMI
 provider API.

Tested:
Verified using ipmitool Move MDRII_GET_DATA_BLOCK command,
behaviour is same before and after the changes.

Signed-off-by: Deepak Kumar Sahu <deepakx.sahu@intel.com>
Change-Id: Id7b42f2fcc10522680c9dbae3f22a31b9bb7e5c2
diff --git a/include/smbiosmdrv2handler.hpp b/include/smbiosmdrv2handler.hpp
index 8b4b5b7..d677376 100644
--- a/include/smbiosmdrv2handler.hpp
+++ b/include/smbiosmdrv2handler.hpp
@@ -155,23 +155,6 @@
     uint32_t timeStamp;
 };
 
-// MDR II Pull Agent get data block command
-struct MDRiiGetDataBlockRequest
-{
-    uint16_t agentId;
-    uint16_t lockHandle;
-    uint32_t xferOffset;
-    uint32_t xferLength;
-};
-
-// MDR II Pull Agent get data block response
-struct MDRiiGetDataBlockResponse
-{
-    uint32_t xferLength;
-    uint32_t checksum;
-    uint8_t data[msgPayloadSize];
-};
-
 // ====================== MDR II Push Command Structures ======================
 // MDR II Client send data set info offer response
 struct MDRiiOfferDataInfoResponse
diff --git a/src/smbiosmdrv2handler.cpp b/src/smbiosmdrv2handler.cpp
index c2ced77..18aff58 100644
--- a/src/smbiosmdrv2handler.cpp
+++ b/src/smbiosmdrv2handler.cpp
@@ -600,121 +600,106 @@
     return ipmi::responseSuccess(entryChanged);
 }
 
-ipmi_ret_t cmd_mdr2_get_data_block(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
-                                   ipmi_request_t request,
-                                   ipmi_response_t response,
-                                   ipmi_data_len_t data_len,
-                                   ipmi_context_t context)
+/**
+@brief This command is MDR related get data block command.
+
+@param - agentId
+@param - lockHandle
+@param - xferOffset
+@param - xferLength
+
+@return on success
+   - xferLength
+   - checksum
+   - data
+**/
+ipmi::RspType<uint32_t,            // xferLength
+              uint32_t,            // Checksum
+              std::vector<uint8_t> // data
+              >
+    mdr2GetDataBlock(uint16_t agentId, uint16_t lockHandle, uint32_t xferOffset,
+                     uint32_t xferLength)
 {
-    auto requestData =
-        reinterpret_cast<const MDRiiGetDataBlockRequest *>(request);
-    auto responseData = reinterpret_cast<MDRiiGetDataBlockResponse *>(response);
     std::tuple<uint8_t, uint32_t, uint32_t, std::vector<uint8_t>> res;
     std::vector<uint8_t> resData;
     uint8_t status = 1;
 
-    if (*data_len != sizeof(MDRiiGetDataBlockRequest))
-    {
-        *data_len = 0;
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
-    }
-
-    *data_len = 0;
-
     if (mdrv2 == nullptr)
     {
         mdrv2 = std::make_unique<MDRV2>();
     }
 
-    int agentIndex = mdrv2->agentLookup(requestData->agentId);
+    int agentIndex = mdrv2->agentLookup(agentId);
     if (agentIndex == -1)
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
-            "Unknown agent id",
-            phosphor::logging::entry("ID=%x", requestData->agentId));
-        return IPMI_CC_PARM_OUT_OF_RANGE;
+            "Unknown agent id", phosphor::logging::entry("ID=%x", agentId));
+        return ipmi::responseParmOutOfRange();
     }
 
-    int idIndex = mdrv2->findLockHandle(requestData->lockHandle);
+    int idIndex = mdrv2->findLockHandle(lockHandle);
 
     if ((idIndex < 0) || (idIndex >= maxDirEntries))
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
             "Invalid Data ID", phosphor::logging::entry("IDINDEX=%x", idIndex));
-        return IPMI_CC_PARM_OUT_OF_RANGE;
+        return ipmi::responseParmOutOfRange();
     }
 
-    if (requestData->xferOffset >= mdrv2->smbiosDir.dir[idIndex].common.size)
+    if (xferOffset >= mdrv2->smbiosDir.dir[idIndex].common.size)
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
             "Offset is outside of range.");
-        return IPMI_CC_PARM_OUT_OF_RANGE;
+        return ipmi::responseParmOutOfRange();
     }
 
-    size_t outSize =
-        (requestData->xferLength > mdrv2->smbiosDir.dir[idIndex].xferSize)
-            ? mdrv2->smbiosDir.dir[idIndex].xferSize
-            : requestData->xferLength;
-    if (outSize > UINT_MAX - requestData->xferOffset)
+    size_t outSize = (xferLength > mdrv2->smbiosDir.dir[idIndex].xferSize)
+                         ? mdrv2->smbiosDir.dir[idIndex].xferSize
+                         : xferLength;
+    if (outSize > UINT_MAX - xferOffset)
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
             "Out size and offset are out of range");
-        return IPMI_CC_PARM_OUT_OF_RANGE;
+        return ipmi::responseParmOutOfRange();
     }
-    if ((requestData->xferOffset + outSize) >
-        mdrv2->smbiosDir.dir[idIndex].common.size)
+    if ((xferOffset + outSize) > mdrv2->smbiosDir.dir[idIndex].common.size)
     {
-        outSize =
-            mdrv2->smbiosDir.dir[idIndex].common.size - requestData->xferOffset;
+        outSize = mdrv2->smbiosDir.dir[idIndex].common.size - xferOffset;
     }
 
-    responseData->xferLength = outSize;
-    if (responseData->xferLength > requestData->xferLength)
+    uint32_t respXferLength = outSize;
+
+    if (respXferLength > xferLength)
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
             "Get data block unexpected error.");
-        return IPMI_CC_UNSPECIFIED_ERROR;
+        return ipmi::responseUnspecifiedError();
     }
 
-    if ((requestData->xferOffset + outSize) >
+    if ((xferOffset + outSize) >
         UINT_MAX -
             reinterpret_cast<size_t>(mdrv2->smbiosDir.dir[idIndex].dataStorage))
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
             "Input data to calculate checksum is out of range");
-        return IPMI_CC_PARM_OUT_OF_RANGE;
+        return ipmi::responseParmOutOfRange();
     }
 
     uint32_t u32Checksum = mdrv2->calcChecksum32(
-        mdrv2->smbiosDir.dir[idIndex].dataStorage + requestData->xferOffset,
-        outSize);
+        mdrv2->smbiosDir.dir[idIndex].dataStorage + xferOffset, outSize);
     if (u32Checksum == invalidChecksum)
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
             "Get data block failed - invalid checksum");
-        return IPMI_CC_OEM_INVALID_CHECKSUM;
+        return ipmi::response(ccOemInvalidChecksum);
     }
-    responseData->checksum = u32Checksum;
+    std::vector<uint8_t> data(outSize);
 
-    *data_len = sizeof(responseData->xferLength) +
-                sizeof(responseData->checksum) + outSize;
+    std::copy(&mdrv2->smbiosDir.dir[idIndex].dataStorage[xferOffset],
+              &mdrv2->smbiosDir.dir[idIndex].dataStorage[xferOffset + outSize],
+              data.begin());
 
-    if (*data_len > MAX_IPMI_BUFFER) // length + completion code should no more
-                                     // than MAX_IPMI_BUFFER
-    {
-        phosphor::logging::log<phosphor::logging::level::ERR>(
-            "Data length send from service is invalid");
-        *data_len = 0;
-        return IPMI_CC_RESPONSE_ERROR;
-    }
-
-    std::copy(
-        &mdrv2->smbiosDir.dir[idIndex].dataStorage[requestData->xferOffset],
-        &mdrv2->smbiosDir.dir[idIndex]
-             .dataStorage[requestData->xferOffset + outSize],
-        responseData->data);
-
-    return IPMI_CC_OK;
+    return ipmi::responseSuccess(respXferLength, u32Checksum, data);
 }
 
 /** @brief implements mdr2 send data block command
@@ -730,6 +715,7 @@
                                   uint32_t xferOffset, uint32_t xferLength,
                                   uint32_t checksum)
 {
+
     if (mdrv2 == nullptr)
     {
         mdrv2 = std::make_unique<MDRV2>();
@@ -1297,9 +1283,9 @@
                           ipmi::Privilege::Operator, mdr2SendDataInfo);
 
     // <Get MDRII Data Block Command>
-    ipmi_register_callback(NETFUN_INTEL_APP_OEM,
-                           IPMI_NETFN_INTEL_OEM_APP_CMD::MDRII_GET_DATA_BLOCK,
-                           NULL, cmd_mdr2_get_data_block, PRIVILEGE_OPERATOR);
+    ipmi::registerHandler(ipmi::prioOemBase, NETFUN_INTEL_APP_OEM,
+                          IPMI_NETFN_INTEL_OEM_APP_CMD::MDRII_GET_DATA_BLOCK,
+                          ipmi::Privilege::Operator, mdr2GetDataBlock);
 
     // <Send MDRII Data Block>
     ipmi::registerHandler(ipmi::prioOemBase, NETFUN_INTEL_APP_OEM,