1 #pragma once
2 
3 #include "common/types.hpp"
4 #include "common/utils.hpp"
5 
6 #include <libpldm/pldm.h>
7 
8 #include <sdbusplus/bus/match.hpp>
9 
10 #include <filesystem>
11 #include <initializer_list>
12 #include <vector>
13 
14 namespace pldm
15 {
16 
17 const std::string emptyUUID = "00000000-0000-0000-0000-000000000000";
18 constexpr const char* MCTPService = "xyz.openbmc_project.MCTP";
19 constexpr const char* MCTPInterface = "xyz.openbmc_project.MCTP.Endpoint";
20 constexpr const char* MCTPPath = "/xyz/openbmc_project/mctp";
21 
22 /** @class MctpDiscoveryHandlerIntf
23  *
24  * This abstract class defines the APIs for MctpDiscovery class has common
25  * interface to execute function from different Manager Classes
26  */
27 class MctpDiscoveryHandlerIntf
28 {
29   public:
30     virtual void handleMctpEndpoints(const MctpInfos& mctpInfos) = 0;
31     virtual void handleRemovedMctpEndpoints(const MctpInfos& mctpInfos) = 0;
32     virtual ~MctpDiscoveryHandlerIntf() {}
33 };
34 
35 class MctpDiscovery
36 {
37   public:
38     MctpDiscovery() = delete;
39     MctpDiscovery(const MctpDiscovery&) = delete;
40     MctpDiscovery(MctpDiscovery&&) = delete;
41     MctpDiscovery& operator=(const MctpDiscovery&) = delete;
42     MctpDiscovery& operator=(MctpDiscovery&&) = delete;
43     ~MctpDiscovery() = default;
44 
45     /** @brief Constructs the MCTP Discovery object to handle discovery of
46      *         MCTP enabled devices
47      *
48      *  @param[in] bus - reference to systemd bus
49      *  @param[in] list - initializer list to the MctpDiscoveryHandlerIntf
50      */
51     explicit MctpDiscovery(
52         sdbusplus::bus_t& bus,
53         std::initializer_list<MctpDiscoveryHandlerIntf*> list);
54 
55     /** @brief reference to the systemd bus */
56     sdbusplus::bus_t& bus;
57 
58     /** @brief Used to watch for new MCTP endpoints */
59     sdbusplus::bus::match_t mctpEndpointAddedSignal;
60 
61     /** @brief Used to watch for the removed MCTP endpoints */
62     sdbusplus::bus::match_t mctpEndpointRemovedSignal;
63 
64     /** @brief List of handlers need to notify when new MCTP
65      * Endpoint is Added/Removed */
66     std::vector<MctpDiscoveryHandlerIntf*> handlers;
67 
68     /** @brief The existing MCTP endpoints */
69     MctpInfos existingMctpInfos;
70 
71     /** @brief Callback function when MCTP endpoints addedInterface
72      * D-Bus signal raised.
73      *
74      *  @param[in] msg - Data associated with subscribed signal
75      */
76     void discoverEndpoints(sdbusplus::message_t& msg);
77 
78     /** @brief Callback function when MCTP endpoint removedInterface
79      * D-Bus signal raised.
80      *
81      *  @param[in] msg - Data associated with subscribed signal
82      */
83     void removeEndpoints(sdbusplus::message_t& msg);
84 
85     /** @brief Helper function to invoke registered handlers for
86      *  the added MCTP endpoints
87      *
88      *  @param[in] mctpInfos - information of discovered MCTP endpoints
89      */
90     void handleMctpEndpoints(const MctpInfos& mctpInfos);
91 
92     /** @brief Helper function to invoke registered handlers for
93      *  the removed MCTP endpoints
94      *
95      *  @param[in] mctpInfos - information of removed MCTP endpoints
96      */
97     void handleRemovedMctpEndpoints(const MctpInfos& mctpInfos);
98 
99     /** @brief Get list of MctpInfos in MCTP control interface.
100      *
101      *  @param[in] mctpInfos - information of discovered MCTP endpoints
102      */
103     void getMctpInfos(MctpInfos& mctpInfos);
104 
105     /** @brief Get list of new MctpInfos in addedInterace D-Bus signal message.
106      *
107      *  @param[in] msg - addedInterace D-Bus signal message
108      *  @param[in] mctpInfos - information of added MCTP endpoints
109      */
110     void getAddedMctpInfos(sdbusplus::message_t& msg, MctpInfos& mctpInfos);
111 
112     /** @brief Add new MctpInfos to existingMctpInfos.
113      *
114      *  @param[in] mctpInfos - information of new MCTP endpoints
115      */
116     void addToExistingMctpInfos(const MctpInfos& mctpInfos);
117 
118     /** @brief Erase the removed MCTP endpoint from existingMctpInfos.
119      *
120      *  @param[in] mctpInfos - the remaining MCTP endpoints
121      *  @param[out] removedInfos - the removed MCTP endpoints
122      */
123     void removeFromExistingMctpInfos(MctpInfos& mctpInfos,
124                                      MctpInfos& removedInfos);
125 
126   private:
127     static constexpr uint8_t mctpTypePLDM = 1;
128 };
129 
130 } // namespace pldm
131