requester: Modified MctpDiscovery class

Modified MctpDiscovery class to take list of managers instead of single
fwManager. The change is for adding platform-mc manager.
Added loadStaticEndpoints API for MCTP layer which doesn't implement
/xyz/openbmc_project/MCTP/Endpoint.Interface

The patch is part of implementation of design document below.
https://gerrit.openbmc-project.xyz/c/openbmc/docs/+/47252

Signed-off-by: Gilbert Chen <gilbert.chen@arm.com>
Signed-off-by: Thu Nguyen <thu@os.amperecomputing.com>
Change-Id: I1e1673504583a87f2a9bc3adf76fb49c2dc30254
diff --git a/requester/mctp_endpoint_discovery.hpp b/requester/mctp_endpoint_discovery.hpp
index de3a6bd..675b6c7 100644
--- a/requester/mctp_endpoint_discovery.hpp
+++ b/requester/mctp_endpoint_discovery.hpp
@@ -1,14 +1,36 @@
 #pragma once
 
-#include "fw-update/manager.hpp"
+#include "common/types.hpp"
+#include "common/utils.hpp"
+
+#include <libpldm/pldm.h>
 
 #include <sdbusplus/bus/match.hpp>
 
+#include <filesystem>
+#include <initializer_list>
+#include <vector>
+
 namespace pldm
 {
 
-constexpr auto MCTPService = "xyz.openbmc_project.MCTP";
-constexpr auto MCTPPath = "/xyz/openbmc_project/mctp";
+const std::string emptyUUID = "00000000-0000-0000-0000-000000000000";
+constexpr const char* MCTPService = "xyz.openbmc_project.MCTP";
+constexpr const char* MCTPInterface = "xyz.openbmc_project.MCTP.Endpoint";
+constexpr const char* MCTPPath = "/xyz/openbmc_project/mctp";
+
+/** @class MctpDiscoveryHandlerIntf
+ *
+ * This abstract class defines the APIs for MctpDiscovery class has common
+ * interface to execute function from different Manager Classes
+ */
+class MctpDiscoveryHandlerIntf
+{
+  public:
+    virtual void handleMctpEndpoints(const MctpInfos& mctpInfos) = 0;
+    virtual void handleRemovedMctpEndpoints(const MctpInfos& mctpInfos) = 0;
+    virtual ~MctpDiscoveryHandlerIntf() {}
+};
 
 class MctpDiscovery
 {
@@ -24,26 +46,85 @@
      *         MCTP enabled devices
      *
      *  @param[in] bus - reference to systemd bus
-     *  @param[in] fwManager - pointer to the firmware manager
+     *  @param[in] list - initializer list to the MctpDiscoveryHandlerIntf
      */
-    explicit MctpDiscovery(sdbusplus::bus_t& bus,
-                           fw_update::Manager* fwManager);
+    explicit MctpDiscovery(
+        sdbusplus::bus::bus& bus,
+        std::initializer_list<MctpDiscoveryHandlerIntf*> list);
 
-  private:
     /** @brief reference to the systemd bus */
     sdbusplus::bus_t& bus;
 
-    fw_update::Manager* fwManager;
-
     /** @brief Used to watch for new MCTP endpoints */
-    sdbusplus::bus::match_t mctpEndpointSignal;
+    sdbusplus::bus::match_t mctpEndpointAddedSignal;
 
-    void dicoverEndpoints(sdbusplus::message_t& msg);
+    /** @brief Used to watch for the removed MCTP endpoints */
+    sdbusplus::bus::match_t mctpEndpointRemovedSignal;
 
+    /** @brief List of handlers need to notify when new MCTP
+     * Endpoint is Added/Removed */
+    std::vector<MctpDiscoveryHandlerIntf*> handlers;
+
+    /** @brief The existing MCTP endpoints */
+    MctpInfos existingMctpInfos;
+
+    /** @brief Callback function when MCTP endpoints addedInterface
+     * D-Bus signal raised.
+     *
+     *  @param[in] msg - Data associated with subscribed signal
+     */
+    void discoverEndpoints(sdbusplus::message_t& msg);
+
+    /** @brief Callback function when MCTP endpoint removedInterface
+     * D-Bus signal raised.
+     *
+     *  @param[in] msg - Data associated with subscribed signal
+     */
+    void removeEndpoints(sdbusplus::message_t& msg);
+
+    /** @brief Helper function to invoke registered handlers for
+     *  the added MCTP endpoints
+     *
+     *  @param[in] mctpInfos - information of discovered MCTP endpoints
+     */
+    void handleMctpEndpoints(const MctpInfos& mctpInfos);
+
+    /** @brief Helper function to invoke registered handlers for
+     *  the removed MCTP endpoints
+     *
+     *  @param[in] mctpInfos - information of removed MCTP endpoints
+     */
+    void handleRemovedMctpEndpoints(const MctpInfos& mctpInfos);
+
+    /** @brief Get list of MctpInfos in MCTP control interface.
+     *
+     *  @param[in] mctpInfos - information of discovered MCTP endpoints
+     */
+    void getMctpInfos(MctpInfos& mctpInfos);
+
+    /** @brief Get list of new MctpInfos in addedInterace D-Bus signal message.
+     *
+     *  @param[in] msg - addedInterace D-Bus signal message
+     *  @param[in] mctpInfos - information of added MCTP endpoints
+     */
+    void getAddedMctpInfos(sdbusplus::message_t& msg, MctpInfos& mctpInfos);
+
+    /** @brief Add new MctpInfos to existingMctpInfos.
+     *
+     *  @param[in] mctpInfos - information of new MCTP endpoints
+     */
+    void addToExistingMctpInfos(const MctpInfos& mctpInfos);
+
+    /** @brief Erase the removed MCTP endpoint from existingMctpInfos.
+     *
+     *  @param[in] mctpInfos - the remaining MCTP endpoints
+     *  @param[out] removedInfos - the removed MCTP endpoints
+     */
+    void removeFromExistingMctpInfos(MctpInfos& mctpInfos,
+                                     MctpInfos& removedInfos);
+
+  private:
     static constexpr uint8_t mctpTypePLDM = 1;
-
-    static constexpr std::string_view mctpEndpointIntfName{
-        "xyz.openbmc_project.MCTP.Endpoint"};
 };
 
 } // namespace pldm