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