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