xref: /openbmc/pldm/requester/mctp_endpoint_discovery.hpp (revision 1f2794d0537b7c071ebc6052124108fecae6d3cc)
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 #include <xyz/openbmc_project/MCTP/Endpoint/client.hpp>
10 
11 #include <filesystem>
12 #include <initializer_list>
13 #include <vector>
14 
15 using MCTPEndpoint = sdbusplus::common::xyz::openbmc_project::mctp::Endpoint;
16 
17 class TestMctpDiscovery;
18 
19 namespace pldm
20 {
21 
22 const std::string emptyUUID = "00000000-0000-0000-0000-000000000000";
23 constexpr const char* MCTPService = "au.com.codeconstruct.MCTP1";
24 constexpr const char* EndpointUUID = "xyz.openbmc_project.Common.UUID";
25 constexpr const char* MCTPPath = "/au/com/codeconstruct/mctp1";
26 constexpr const char* MCTPInterfaceCC = "au.com.codeconstruct.MCTP.Endpoint1";
27 constexpr const char* MCTPConnectivityProp = "Connectivity";
28 constexpr const char* inventorySubtreePathStr =
29     "/xyz/openbmc_project/inventory/system";
30 
31 const std::vector<std::string> interfaceFilter = {
32     "xyz.openbmc_project.Configuration.MCTPI2CTarget",
33     "xyz.openbmc_project.Configuration.MCTPI3CTarget"};
34 
35 /** @class MctpDiscoveryHandlerIntf
36  *
37  * This abstract class defines the APIs for MctpDiscovery class has common
38  * interface to execute function from different Manager Classes
39  */
40 class MctpDiscoveryHandlerIntf
41 {
42   public:
43     virtual void handleMctpEndpoints(const MctpInfos& mctpInfos) = 0;
44     virtual void handleRemovedMctpEndpoints(const MctpInfos& mctpInfos) = 0;
45     virtual void updateMctpEndpointAvailability(const MctpInfo& mctpInfo,
46                                                 Availability availability) = 0;
47     /** @brief Get Active EIDs.
48      *
49      *  @param[in] addr - MCTP address of terminus
50      *  @param[in] terminiNames - MCTP terminus name
51      */
52     virtual std::optional<mctp_eid_t> getActiveEidByName(
53         const std::string& terminusName) = 0;
54 
handleConfigurations(const Configurations &)55     virtual void handleConfigurations(const Configurations& /*configurations*/)
56     {}
~MctpDiscoveryHandlerIntf()57     virtual ~MctpDiscoveryHandlerIntf() {}
58 };
59 
60 class MctpDiscovery
61 {
62   public:
63     MctpDiscovery() = delete;
64     MctpDiscovery(const MctpDiscovery&) = delete;
65     MctpDiscovery(MctpDiscovery&&) = delete;
66     MctpDiscovery& operator=(const MctpDiscovery&) = delete;
67     MctpDiscovery& operator=(MctpDiscovery&&) = delete;
68     ~MctpDiscovery() = default;
69 
70     /** @brief Constructs the MCTP Discovery object to handle discovery of
71      *         MCTP enabled devices
72      *
73      *  @param[in] bus - reference to systemd bus
74      *  @param[in] list - initializer list to the MctpDiscoveryHandlerIntf
75      */
76     explicit MctpDiscovery(
77         sdbusplus::bus_t& bus,
78         std::initializer_list<MctpDiscoveryHandlerIntf*> list);
79 
80     /** @brief reference to the systemd bus */
81     sdbusplus::bus_t& bus;
82 
83     /** @brief Used to watch for new MCTP endpoints */
84     sdbusplus::bus::match_t mctpEndpointAddedSignal;
85 
86     /** @brief Used to watch for the removed MCTP endpoints */
87     sdbusplus::bus::match_t mctpEndpointRemovedSignal;
88 
89     /** @brief Used to watch for new MCTP endpoints */
90     sdbusplus::bus::match_t mctpEndpointPropChangedSignal;
91 
92     /** @brief List of handlers need to notify when new MCTP
93      * Endpoint is Added/Removed */
94     std::vector<MctpDiscoveryHandlerIntf*> handlers;
95 
96     /** @brief The existing MCTP endpoints */
97     MctpInfos existingMctpInfos;
98 
99     /** @brief Callback function when the propertiesChanged D-Bus
100      * signal is triggered for MCTP endpoint's properties.
101      *
102      *  @param[in] msg - Data associated with subscribed signal
103      */
104     void propertiesChangedCb(sdbusplus::message_t& msg);
105 
106     /** @brief Callback function when MCTP endpoints addedInterface
107      * D-Bus signal raised.
108      *
109      *  @param[in] msg - Data associated with subscribed signal
110      */
111     void discoverEndpoints(sdbusplus::message_t& msg);
112 
113     /** @brief Callback function when MCTP endpoint removedInterface
114      * D-Bus signal raised.
115      *
116      *  @param[in] msg - Data associated with subscribed signal
117      */
118     void removeEndpoints(sdbusplus::message_t& msg);
119 
120     /** @brief Helper function to invoke registered handlers for
121      *  the added MCTP endpoints
122      *
123      *  @param[in] mctpInfos - information of discovered MCTP endpoints
124      */
125     void handleMctpEndpoints(const MctpInfos& mctpInfos);
126 
127     /** @brief Helper function to invoke registered handlers for
128      *  the removed MCTP endpoints
129      *
130      *  @param[in] mctpInfos - information of removed MCTP endpoints
131      */
132     void handleRemovedMctpEndpoints(const MctpInfos& mctpInfos);
133 
134     /** @brief Helper function to invoke registered handlers for
135      *  updating the availability status of the MCTP endpoint
136      *
137      *  @param[in] mctpInfo - information of the target endpoint
138      *  @param[in] availability - new availability status
139      */
140     void updateMctpEndpointAvailability(const MctpInfo& mctpInfo,
141                                         Availability availability);
142 
143     /** @brief Get list of MctpInfos in MCTP control interface.
144      *
145      *  @param[in] mctpInfoMap - information of discovered MCTP endpoints
146      *  and the availability status of each endpoint
147      */
148     void getMctpInfos(std::map<MctpInfo, Availability>& mctpInfoMap);
149 
150     /** @brief Get list of new MctpInfos in addedInterace D-Bus signal message.
151      *
152      *  @param[in] msg - addedInterace D-Bus signal message
153      *  @param[in] mctpInfos - information of added MCTP endpoints
154      */
155     void getAddedMctpInfos(sdbusplus::message_t& msg, MctpInfos& mctpInfos);
156 
157     /** @brief Add new MctpInfos to existingMctpInfos.
158      *
159      *  @param[in] mctpInfos - information of new MCTP endpoints
160      */
161     void addToExistingMctpInfos(const MctpInfos& mctpInfos);
162 
163     /** @brief Erase the removed MCTP endpoint from existingMctpInfos.
164      *
165      *  @param[in] mctpInfos - the remaining MCTP endpoints
166      *  @param[out] removedInfos - the removed MCTP endpoints
167      */
168     void removeFromExistingMctpInfos(MctpInfos& mctpInfos,
169                                      MctpInfos& removedInfos);
170 
171     friend class ::TestMctpDiscovery;
172 
173   private:
174     /** @brief Get MCTP Endpoint D-Bus Properties in the
175      *         `xyz.openbmc_project.MCTP.Endpoint` D-Bus interface
176      *
177      *  @param[in] service - the MCTP service name
178      *  @param[in] path - the MCTP endpoints object path
179      *
180      *  @return tuple of Network Index, Endpoint ID and MCTP message types
181      */
182     MctpEndpointProps getMctpEndpointProps(const std::string& service,
183                                            const std::string& path);
184 
185     /** @brief Get Endpoint UUID from `UUID` D-Bus property in the
186      *         `xyz.openbmc_project.Common.UUID` D-Bus interface.
187      *
188      *  @param[in] service - the MCTP service name
189      *  @param[in] path - the MCTP endpoints object path
190      *
191      *  @return Endpoint UUID
192      */
193     UUID getEndpointUUIDProp(const std::string& service,
194                              const std::string& path);
195 
196     /** @brief Get Endpoint Availability status from `Connectivity` D-Bus
197      *         property in the `au.com.codeconstruct.MCTP.Endpoint1` D-Bus
198      *         interface.
199      *
200      *  @param[in] path - the MCTP endpoints object path
201      *
202      *  @return Availability status: true if active false if inactive
203      */
204     Availability getEndpointConnectivityProp(const std::string& path);
205 
206     static constexpr uint8_t mctpTypePLDM = 1;
207 
208     /** @brief Construct the MCTP reactor object path
209      *
210      *  @param[in] mctpInfo - information of discovered MCTP endpoint
211      *
212      *  @return the MCTP reactor object path
213      */
214     std::string constructMctpReactorObjectPath(const MctpInfo& mctpInfo);
215 
216     /** @brief Search for associated configuration for the MctpInfo.
217      *
218      *  @param[in] mctpInfo - information of discovered MCTP endpoint
219      */
220     void searchConfigurationFor(const pldm::utils::DBusHandler& handler,
221                                 MctpInfo& mctpInfo);
222 
223     /** @brief Remove configuration associated with the removed MCTP endpoint.
224      *
225      *  @param[in] removedInfos - the removed MCTP endpoints
226      */
227     void removeConfigs(const MctpInfos& removedInfos);
228 
229     /** @brief An internal helper function to get the name property from the
230      * properties
231      * @param[in] properties - the properties of the D-Bus object
232      * @return the name property
233      */
234     std::string getNameFromProperties(const utils::PropertyMap& properties);
235 
236     /** @brief The configuration contains D-Bus path and the MCTP endpoint
237      * information.
238      */
239     Configurations configurations;
240 };
241 
242 } // namespace pldm
243