use registerHandler instead of ipmi_register_callback

Since ipmi_register_callback declared in api.h has been gradually
deprecated, this patch is to use registerHandler instead of
ipmi_register_callback.

Change-Id: I90d1e78593abcad117afa9330602c46d7f237c20
Signed-off-by: George Liu <liuxiwei@ieisystem.com>
diff --git a/strgfnhandler.cpp b/strgfnhandler.cpp
index 748c1a3..2a2d2d1 100644
--- a/strgfnhandler.cpp
+++ b/strgfnhandler.cpp
@@ -1,9 +1,9 @@
 #include "writefrudata.hpp"
 
-#include <ipmid/api.h>
 #include <unistd.h>
 
 #include <ipmid/api-types.hpp>
+#include <ipmid/handler.hpp>
 #include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/bus.hpp>
 
@@ -17,47 +17,24 @@
 ///-------------------------------------------------------
 // Called by IPMI netfn router for write fru data command
 //--------------------------------------------------------
-ipmi_ret_t ipmiStorageWriteFruData(
-    ipmi_netfn_t /*netfn*/, ipmi_cmd_t /*cmd*/, ipmi_request_t request,
-    ipmi_response_t response, ipmi_data_len_t dataLen,
-    ipmi_context_t /*context*/)
+ipmi::RspType<uint8_t> ipmiStorageWriteFruData(uint8_t fruId, uint16_t offset,
+                                               std::vector<uint8_t>& buffer)
 {
     FILE* fp = nullptr;
     char fruFilename[16] = {0};
-    size_t offset = 0;
-    size_t len = 0;
-    ipmi_ret_t rc = ipmi::ccInvalidCommand;
-    const char* mode = nullptr;
-
-    // From the payload, extract the header that has fruid and the offsets
-    auto reqptr = static_cast<write_fru_data_t*>(request);
+    const char* mode = "rb+";
 
     // Maintaining a temporary file to pump the data
-    std::sprintf(fruFilename, "%s%02x", "/tmp/ipmifru", reqptr->frunum);
-
-    offset = ((size_t)reqptr->offsetms) << 8 | reqptr->offsetls;
-
-    // Length is the number of request bytes minus the header itself.
-    // The header contains an extra byte to indicate the start of
-    // the data (so didn't need to worry about word/byte boundaries)
-    // hence the -1...
-    len = ((size_t)*dataLen) - (sizeof(write_fru_data_t) - 1);
-
-    // On error there is no response data for this command.
-    *dataLen = 0;
+    std::sprintf(fruFilename, "%s%02x", "/tmp/ipmifru", fruId);
 
     lg2::debug(
         "IPMI WRITE-FRU-DATA, file name: {FILE}, offset: {OFFSET}, length: {LENGTH}",
-        "FILE", fruFilename, "OFFSET", offset, "LENGTH", len);
+        "FILE", fruFilename, "OFFSET", offset, "LENGTH", buffer.size());
 
     if (access(fruFilename, F_OK) == -1)
     {
         mode = "wb";
     }
-    else
-    {
-        mode = "rb+";
-    }
 
     if ((fp = std::fopen(fruFilename, mode)) != nullptr)
     {
@@ -67,16 +44,18 @@
                 "Seek into fru file failed, file name: {FILE}, errno: {ERRNO}",
                 "FILE", fruFilename, "ERRNO", errno);
             std::fclose(fp);
-            return rc;
+
+            return ipmi::responseInvalidFieldRequest();
         }
 
-        if (std::fwrite(&reqptr->data, len, 1, fp) != 1)
+        if (std::fwrite(buffer.data(), 1, buffer.size(), fp) != buffer.size())
         {
             lg2::error(
                 "Write into fru file failed, file name: {FILE}, errno: {ERRNO}",
                 "FILE", fruFilename, "ERRNO", errno);
             std::fclose(fp);
-            return rc;
+
+            return ipmi::responseInvalidFieldRequest();
         }
 
         std::fclose(fp);
@@ -84,14 +63,9 @@
     else
     {
         lg2::error("Error trying to write to {FILE}", "FILE", fruFilename);
-        return rc;
-    }
 
-    // If we got here then set the response byte
-    // to the number of bytes written
-    std::memcpy(response, &len, 1);
-    *dataLen = 1;
-    rc = ipmi::ccSuccess;
+        return ipmi::responseInvalidFieldRequest();
+    }
 
     // Get the reference to global sd_bus object
     sd_bus* bus_type = ipmid_get_sd_bus_connection();
@@ -99,9 +73,9 @@
     // We received some bytes. It may be full or partial. Send a valid
     // FRU file to the inventory controller on DBus for the correct number
     sdbusplus::bus_t bus{bus_type};
-    validateFRUArea(reqptr->frunum, fruFilename, bus);
+    validateFRUArea(fruId, fruFilename, bus);
 
-    return rc;
+    return ipmi::responseSuccess(buffer.size());
 }
 
 //-------------------------------------------------------
@@ -114,6 +88,7 @@
         "NETFN", lg2::hex, ipmi::netFnStorage, "CMD", lg2::hex,
         ipmi::storage::cmdWriteFruData);
 
-    ipmi_register_callback(ipmi::netFnStorage, ipmi::storage::cmdWriteFruData,
-                           nullptr, ipmiStorageWriteFruData, SYSTEM_INTERFACE);
+    ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
+                          ipmi::storage::cmdWriteFruData,
+                          ipmi::Privilege::Admin, ipmiStorageWriteFruData);
 }