oem-ibm: Handler support for newFileAvailableWithMetaData

The commits adds handler support for the oem-ibm file I/O
newFileAvailableWithMetaData command.
This also adds a support for NotifyDump method call as part
of DumpHandler for the newFileAvailableWithMetaData command.

Tested: The newFileAvailableWithMetaData command was honored and
the Notify Dump was triggered when the file type was DUMP.

Change-Id: I654c4586341019850b3010e975a9948ed22b50f9
Signed-off-by: Pavithra Barithaya <pavithrabarithaya07@gmail.com>
diff --git a/oem/ibm/libpldmresponder/file_io.cpp b/oem/ibm/libpldmresponder/file_io.cpp
index 0ce4b1c..6acc1a3 100644
--- a/oem/ibm/libpldmresponder/file_io.cpp
+++ b/oem/ibm/libpldmresponder/file_io.cpp
@@ -1193,6 +1193,49 @@
     return response;
 }
 
+Response Handler::newFileAvailableWithMetaData(const pldm_msg* request,
+                                               size_t payloadLength)
+{
+    Response response(sizeof(pldm_msg_hdr) +
+                      PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_RESP_BYTES);
+    if (payloadLength != PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_REQ_BYTES)
+    {
+        return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
+    }
+    uint16_t fileType{};
+    uint32_t fileHandle{};
+    uint64_t length{};
+    uint32_t fileMetaData1{};
+    uint32_t fileMetaData2{};
+    uint32_t fileMetaData3{};
+    uint32_t fileMetaData4{};
+    auto rc = decode_new_file_with_metadata_req(
+        request, payloadLength, &fileType, &fileHandle, &length, &fileMetaData1,
+        &fileMetaData2, &fileMetaData3, &fileMetaData4);
+    if (rc != PLDM_SUCCESS)
+    {
+        return CmdHandler::ccOnlyResponse(request, rc);
+    }
+    std::unique_ptr<FileHandler> handler{};
+    try
+    {
+        handler = getHandlerByType(fileType, fileHandle);
+    }
+    catch (const InternalFailure& e)
+    {
+        error(
+            "Unknown file type, '{TYPE}' in NewFileAvailableMetaData response, error - {ERROR}",
+            "TYPE", fileType, "ERROR", e);
+        return CmdHandler::ccOnlyResponse(request, PLDM_INVALID_FILE_TYPE);
+    }
+    rc = handler->newFileAvailableWithMetaData(
+        length, fileMetaData1, fileMetaData2, fileMetaData3, fileMetaData4);
+    auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+    encode_new_file_with_metadata_resp(request->hdr.instance_id, rc,
+                                       responsePtr);
+    return response;
+}
+
 } // namespace oem_ibm
 } // namespace responder
 } // namespace pldm
diff --git a/oem/ibm/libpldmresponder/file_io.hpp b/oem/ibm/libpldmresponder/file_io.hpp
index 5d70f53..ed2cd6e 100644
--- a/oem/ibm/libpldmresponder/file_io.hpp
+++ b/oem/ibm/libpldmresponder/file_io.hpp
@@ -236,6 +236,13 @@
             [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
                 return this->fileAckWithMetaData(request, payloadLength);
             });
+        handlers.emplace(
+            PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA,
+            [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
+                return this->newFileAvailableWithMetaData(request,
+                                                          payloadLength);
+            });
+
         resDumpMatcher = std::make_unique<sdbusplus::bus::match_t>(
             pldm::utils::DBusHandler::getBus(),
             sdbusplus::bus::match::rules::interfacesAdded() +
@@ -428,6 +435,16 @@
      */
     Response fileAckWithMetaData(const pldm_msg* request, size_t payloadLength);
 
+    /** @brief Handler for newFileAvailableWithMetaData command
+     *
+     *  @param[in] request - PLDM request msg
+     *  @param[in] payloadLength - length of the message payload
+     *
+     *  @return PLDM response messsage
+     */
+    Response newFileAvailableWithMetaData(const pldm_msg* request,
+                                          size_t payloadLength);
+
   private:
     oem_platform::Handler* oemPlatformHandler;
     using DBusInterfaceAdded = std::vector<std::pair<
diff --git a/oem/ibm/libpldmresponder/file_io_by_type.hpp b/oem/ibm/libpldmresponder/file_io_by_type.hpp
index 30af4d1..d336c87 100644
--- a/oem/ibm/libpldmresponder/file_io_by_type.hpp
+++ b/oem/ibm/libpldmresponder/file_io_by_type.hpp
@@ -127,6 +127,21 @@
     virtual int transferFileDataToSocket(int fd, uint32_t& length,
                                          uint64_t address);
 
+    /** @brief method to process a new file available metadata notification from
+     *  the host
+     *
+     *  @param[in] length - size of the file content to be transferred
+     *  @param[in] metaDataValue1 - value of meta data sent by host
+     *  @param[in] metaDataValue2 - value of meta data sent by host
+     *  @param[in] metaDataValue3 - value of meta data sent by host
+     *  @param[in] metaDataValue4 - value of meta data sent by host
+     *
+     *  @return PLDM status code
+     */
+    virtual int newFileAvailableWithMetaData(
+        uint64_t length, uint32_t metaDataValue1, uint32_t metaDataValue2,
+        uint32_t metaDataValue3, uint32_t metaDataValue4) = 0;
+
     /** @brief Constructor to create a FileHandler object
      */
     FileHandler(uint32_t fileHandle) : fileHandle(fileHandle) {}
diff --git a/oem/ibm/libpldmresponder/file_io_type_dump.cpp b/oem/ibm/libpldmresponder/file_io_type_dump.cpp
index fc14f2c..1c033f0 100644
--- a/oem/ibm/libpldmresponder/file_io_type_dump.cpp
+++ b/oem/ibm/libpldmresponder/file_io_type_dump.cpp
@@ -1,5 +1,6 @@
 #include "file_io_type_dump.hpp"
 
+#include "com/ibm/Dump/Notify/server.hpp"
 #include "common/utils.hpp"
 #include "utils.hpp"
 #include "xyz/openbmc_project/Common/error.hpp"
@@ -347,5 +348,43 @@
     return readFile(resDumpDirPath, offset, length, response);
 }
 
+int DumpHandler::newFileAvailableWithMetaData(
+    uint64_t length, uint32_t metaDataValue1, uint32_t /*metaDataValue2*/,
+    uint32_t /*metaDataValue3*/, uint32_t /*metaDataValue4*/)
+{
+    static constexpr auto dumpInterface = "com.ibm.Dump.Notify";
+    auto& bus = pldm::utils::DBusHandler::getBus();
+
+    auto notifyObjPath = dumpObjPath;
+    auto notifyDumpType =
+        sdbusplus::common::com::ibm::dump::Notify::DumpType::System;
+    if (dumpType == PLDM_FILE_TYPE_RESOURCE_DUMP)
+    {
+        notifyDumpType =
+            sdbusplus::common::com::ibm::dump::Notify::DumpType::Resource;
+    }
+
+    try
+    {
+        auto service =
+            pldm::utils::DBusHandler().getService(notifyObjPath, dumpInterface);
+        auto method = bus.new_method_call(service.c_str(), notifyObjPath,
+                                          dumpInterface, "NotifyDump");
+        method.append(fileHandle, length, notifyDumpType, metaDataValue1);
+        bus.call(method, dbusTimeout);
+    }
+    catch (const sdbusplus::exception_t& e)
+    {
+        error(
+            "failed to make a d-bus call to notify a new dump request using newFileAvailableWithMetaData, error - {ERROR}",
+            "ERROR", e);
+        pldm::utils::reportError(
+            "xyz.openbmc_project.PLDM.Error.newFileAvailableWithMetaData.NewDumpNotifyFail");
+        return PLDM_ERROR;
+    }
+
+    return PLDM_SUCCESS;
+}
+
 } // namespace responder
 } // namespace pldm
diff --git a/oem/ibm/libpldmresponder/file_io_type_dump.hpp b/oem/ibm/libpldmresponder/file_io_type_dump.hpp
index a474f71..4379d09 100644
--- a/oem/ibm/libpldmresponder/file_io_type_dump.hpp
+++ b/oem/ibm/libpldmresponder/file_io_type_dump.hpp
@@ -48,6 +48,10 @@
         return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
     }
 
+    virtual int newFileAvailableWithMetaData(
+        uint64_t length, uint32_t metaDataValue1, uint32_t /*metaDataValue2*/,
+        uint32_t /*metaDataValue3*/, uint32_t /*metaDataValue4*/);
+
     std::string findDumpObjPath(uint32_t fileHandle);
     std::string getOffloadUri(uint32_t fileHandle);
 
diff --git a/oem/ibm/libpldmresponder/file_io_type_lid.hpp b/oem/ibm/libpldmresponder/file_io_type_lid.hpp
index dd2ca3d..fcac808 100644
--- a/oem/ibm/libpldmresponder/file_io_type_lid.hpp
+++ b/oem/ibm/libpldmresponder/file_io_type_lid.hpp
@@ -316,6 +316,14 @@
         return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
     }
 
+    virtual int newFileAvailableWithMetaData(
+        uint64_t /*length*/, uint32_t /*metaDataValue1*/,
+        uint32_t /*metaDataValue2*/, uint32_t /*metaDataValue3*/,
+        uint32_t /*metaDataValue4*/)
+    {
+        return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
+    }
+
     /** @brief LidHandler destructor
      */
     ~LidHandler() {}
diff --git a/oem/ibm/libpldmresponder/file_io_type_pel.hpp b/oem/ibm/libpldmresponder/file_io_type_pel.hpp
index bd88409..a3c3955 100644
--- a/oem/ibm/libpldmresponder/file_io_type_pel.hpp
+++ b/oem/ibm/libpldmresponder/file_io_type_pel.hpp
@@ -56,6 +56,14 @@
         return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
     }
 
+    virtual int newFileAvailableWithMetaData(
+        uint64_t /*length*/, uint32_t /*metaDataValue1*/,
+        uint32_t /*metaDataValue2*/, uint32_t /*metaDataValue3*/,
+        uint32_t /*metaDataValue4*/)
+    {
+        return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
+    }
+
     /** @brief PelHandler destructor
      */
     ~PelHandler() {}
diff --git a/oem/ibm/libpldmresponder/file_io_type_progress_src.hpp b/oem/ibm/libpldmresponder/file_io_type_progress_src.hpp
index deecf93..76f90c2 100644
--- a/oem/ibm/libpldmresponder/file_io_type_progress_src.hpp
+++ b/oem/ibm/libpldmresponder/file_io_type_progress_src.hpp
@@ -53,6 +53,14 @@
         return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
     }
 
+    virtual int newFileAvailableWithMetaData(
+        uint64_t /*length*/, uint32_t /*metaDataValue1*/,
+        uint32_t /*metaDataValue2*/, uint32_t /*metaDataValue3*/,
+        uint32_t /*metaDataValue4*/) override
+    {
+        return PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
+    }
+
     /** @brief method to set the dbus Raw value Property with
      * the obtained progress code from the host.
      *