smbiosmdrv2handler:move mdr2unlock data & get dir

Rewrite "MDRII_UNLOCK_DATA & GET_DIR" cmds to new IPMI provider API.

Tested:
verified using ipmitool smbiosmdrv2handler commands.
a. mdr2 unlock data
b. mdr2 get dir, before and after the changes.
ipmitool raw 0x3E  0x34 0x01 0x01 0x01 0x00
output: Unable to send RAW command (channel=0x0 netfn=0x3e lun=0x0
cmd=0x34 rsp=0xc9): Parameter out of range

ipmitool raw 0x3E 0x31 0x01 0x01 0x00
output: 02 01 01 00 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33

Signed-off-by: jayaprakash Mutyala <mutyalax.jayaprakash@intel.com>
Change-Id: I637bbf20e4a67477617bd8d579a7ed4181d6e943
diff --git a/include/smbiosmdrv2handler.hpp b/include/smbiosmdrv2handler.hpp
index a2c3baa..3254f47 100644
--- a/include/smbiosmdrv2handler.hpp
+++ b/include/smbiosmdrv2handler.hpp
@@ -138,22 +138,6 @@
 static constexpr const size_t syncDirCommonSize = 3;
 
 // ====================== MDR II Pull Command Structures ======================
-struct MDRiiGetDirRequest
-{
-    uint16_t agentId;
-    uint8_t dirIndex;
-};
-
-// MDR II directory information inquiry response
-struct MDRiiGetDirResponse
-{
-    uint8_t mdrVersion;
-    uint8_t dirVersion;
-    uint8_t returnedEntries;
-    uint8_t remainingEntries;
-    uint8_t data[1];
-};
-
 struct MDRiiGetDataInfoRequest
 {
     uint16_t agentId;
@@ -236,13 +220,6 @@
     uint32_t xferLength;
 };
 
-// MDR II Pull Agent unlock data set command
-struct MDRiiUnlockDataRequest
-{
-    uint16_t agentId;
-    uint16_t lockHandle;
-};
-
 // MDR II Push Agent send data start command
 struct MDRiiDataStartRequest
 {
diff --git a/src/smbiosmdrv2handler.cpp b/src/smbiosmdrv2handler.cpp
index d97ccf4..2d1c92c 100644
--- a/src/smbiosmdrv2handler.cpp
+++ b/src/smbiosmdrv2handler.cpp
@@ -268,22 +268,15 @@
                                  dirEntries, dataRequest);
 }
 
-ipmi_ret_t cmd_mdr2_get_dir(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 implements mdr2 get directory command
+ *  @param agentId
+ *  @param dirIndex
+ *  @returns IPMI completion code plus response data
+ *  - dataOut
+ */
+ipmi::RspType<std::vector<uint8_t>> mdr2GetDir(uint16_t agentId,
+                                               uint8_t dirIndex)
 {
-    auto requestData = reinterpret_cast<const MDRiiGetDirRequest *>(request);
-    auto dataOut = reinterpret_cast<uint8_t *>(response);
-    std::vector<uint8_t> dirInfo;
-
-    if (*data_len != sizeof(MDRiiGetDirRequest))
-    {
-        *data_len = 0;
-        return IPMI_CC_REQ_DATA_LEN_INVALID;
-    }
-
-    *data_len = 0;
-
     std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
     std::string service = ipmi::getService(*bus, mdrv2Interface, mdrv2Path);
 
@@ -292,36 +285,36 @@
         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();
     }
 
-    sdbusplus::message::variant<uint8_t> value = 0;
+    std::variant<uint8_t> value = 0;
     if (0 != mdrv2->sdplusMdrv2GetProperty("DirectoryEntries", value, service))
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
             "Error getting DirEnries");
-        return IPMI_CC_UNSPECIFIED_ERROR;
+        return ipmi::responseUnspecifiedError();
     }
-    if (requestData->dirIndex > std::get<uint8_t>(value))
+    if (dirIndex > std::get<uint8_t>(value))
     {
-        return IPMI_CC_PARM_OUT_OF_RANGE;
+        return ipmi::responseParmOutOfRange();
     }
 
     sdbusplus::message::message method = bus->new_method_call(
         service.c_str(), mdrv2Path, mdrv2Interface, "GetDirectoryInformation");
 
-    method.append(requestData->dirIndex);
+    method.append(dirIndex);
 
+    std::vector<uint8_t> dataOut;
     try
     {
         sdbusplus::message::message reply = bus->call(method);
-        reply.read(dirInfo);
+        reply.read(dataOut);
     }
     catch (sdbusplus::exception_t &e)
     {
@@ -329,32 +322,26 @@
             "Error get dir", phosphor::logging::entry("ERROR=%s", e.what()),
             phosphor::logging::entry("SERVICE=%s", service.c_str()),
             phosphor::logging::entry("PATH=%s", mdrv2Path));
-        return IPMI_CC_RESPONSE_ERROR;
+        return ipmi::responseResponseError();
     }
 
-    if (dirInfo.size() < sizeof(MDRiiGetDirResponse))
+    constexpr size_t getDirRespSize = 6;
+    if (dataOut.size() < getDirRespSize)
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
             "Error get dir, response length invalid");
-        return IPMI_CC_UNSPECIFIED_ERROR;
+        return ipmi::responseUnspecifiedError();
     }
 
-    auto responseData = reinterpret_cast<MDRiiGetDirResponse *>(dirInfo.data());
-
-    *data_len = dirInfo.size();
-
-    if (*data_len > MAX_IPMI_BUFFER) // length + completion code should no more
-                                     // than MAX_IPMI_BUFFER
+    if (dataOut.size() > 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;
+        return ipmi::responseResponseError();
     }
 
-    std::copy(&dirInfo[0], &dirInfo[*data_len], dataOut);
-
-    return IPMI_CC_OK;
+    return ipmi::responseSuccess(dataOut);
 }
 
 ipmi_ret_t cmd_mdr2_send_dir(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
@@ -1092,55 +1079,46 @@
     return IPMI_CC_OK;
 }
 
-ipmi_ret_t cmd_mdr2_unlock_data(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 implements mdr2 unlock data command
+ *  @param agentId
+ *  @param lockHandle
+ *
+ *  @returns IPMI completion code
+ */
+ipmi::RspType<> mdr2UnlockData(uint16_t agentId, uint16_t lockHandle)
 {
     phosphor::logging::log<phosphor::logging::level::ERR>("unlock data");
-    auto requestData =
-        reinterpret_cast<const MDRiiUnlockDataRequest *>(request);
-
-    if (*data_len != sizeof(MDRiiUnlockDataRequest))
-    {
-        *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 (!mdrv2->smbiosUnlock(idIndex))
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
             "Unlock Data failed - cannot unlock idIndex");
-        return IPMI_CC_PARAMETER_NOT_SUPPORT_IN_PRESENT_STATE;
+        return ipmi::responseCommandNotAvailable();
     }
 
-    return IPMI_CC_OK;
+    return ipmi::responseSuccess();
 }
 
 /**
@@ -1335,9 +1313,9 @@
                           ipmi::Privilege::Operator, mdr2AgentStatus);
 
     // <Get MDRII Directory Command>
-    ipmi_register_callback(NETFUN_INTEL_APP_OEM,
-                           IPMI_NETFN_INTEL_OEM_APP_CMD::MDRII_GET_DIR, NULL,
-                           cmd_mdr2_get_dir, PRIVILEGE_OPERATOR);
+    ipmi::registerHandler(ipmi::prioOemBase, NETFUN_INTEL_APP_OEM,
+                          IPMI_NETFN_INTEL_OEM_APP_CMD::MDRII_GET_DIR,
+                          ipmi::Privilege::Operator, mdr2GetDir);
 
     // <Send MDRII Directory Command>
     ipmi_register_callback(NETFUN_INTEL_APP_OEM,
@@ -1376,9 +1354,9 @@
                            cmd_mdr2_lock_data, PRIVILEGE_OPERATOR);
 
     // <Unlock MDRII Data Command>
-    ipmi_register_callback(NETFUN_INTEL_APP_OEM,
-                           IPMI_NETFN_INTEL_OEM_APP_CMD::MDRII_UNLOCK_DATA,
-                           NULL, cmd_mdr2_unlock_data, PRIVILEGE_OPERATOR);
+    ipmi::registerHandler(ipmi::prioOemBase, NETFUN_INTEL_APP_OEM,
+                          IPMI_NETFN_INTEL_OEM_APP_CMD::MDRII_UNLOCK_DATA,
+                          ipmi::Privilege::Operator, mdr2UnlockData);
 
     // <Send MDRII Data Start>
     ipmi::registerHandler(ipmi::prioOemBase, NETFUN_INTEL_APP_OEM,