blob: 1db6292f71c2825007254c1a491e4ebb07e502eb [file] [log] [blame]
Tom Josephfb3bc062021-08-17 07:48:11 -07001#pragma once
2
Gilbert Chen44524a52022-02-14 12:12:25 +00003#include "common/types.hpp"
4#include "common/utils.hpp"
5
6#include <libpldm/pldm.h>
Tom Josephfb3bc062021-08-17 07:48:11 -07007
8#include <sdbusplus/bus/match.hpp>
Alexander Hansen1f2794d2025-11-11 15:58:13 +01009#include <xyz/openbmc_project/MCTP/Endpoint/client.hpp>
Tom Josephfb3bc062021-08-17 07:48:11 -070010
Gilbert Chen44524a52022-02-14 12:12:25 +000011#include <filesystem>
12#include <initializer_list>
13#include <vector>
14
Alexander Hansen1f2794d2025-11-11 15:58:13 +010015using MCTPEndpoint = sdbusplus::common::xyz::openbmc_project::mctp::Endpoint;
16
Unive Tienc40d4a62025-03-12 11:36:07 +080017class TestMctpDiscovery;
18
Tom Josephfb3bc062021-08-17 07:48:11 -070019namespace pldm
20{
21
Gilbert Chen44524a52022-02-14 12:12:25 +000022const std::string emptyUUID = "00000000-0000-0000-0000-000000000000";
Thu Nguyenef16cde2024-07-09 05:56:57 +000023constexpr const char* MCTPService = "au.com.codeconstruct.MCTP1";
Thu Nguyen90274972024-07-17 07:02:16 +000024constexpr const char* EndpointUUID = "xyz.openbmc_project.Common.UUID";
Thu Nguyenef16cde2024-07-09 05:56:57 +000025constexpr const char* MCTPPath = "/au/com/codeconstruct/mctp1";
Chau Ly75e00422024-03-19 12:33:08 +000026constexpr const char* MCTPInterfaceCC = "au.com.codeconstruct.MCTP.Endpoint1";
27constexpr const char* MCTPConnectivityProp = "Connectivity";
Unive Tienc40d4a62025-03-12 11:36:07 +080028constexpr const char* inventorySubtreePathStr =
29 "/xyz/openbmc_project/inventory/system";
30
31const std::vector<std::string> interfaceFilter = {
32 "xyz.openbmc_project.Configuration.MCTPI2CTarget",
33 "xyz.openbmc_project.Configuration.MCTPI3CTarget"};
Gilbert Chen44524a52022-02-14 12:12:25 +000034
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 */
40class MctpDiscoveryHandlerIntf
41{
42 public:
43 virtual void handleMctpEndpoints(const MctpInfos& mctpInfos) = 0;
44 virtual void handleRemovedMctpEndpoints(const MctpInfos& mctpInfos) = 0;
Chau Ly75e00422024-03-19 12:33:08 +000045 virtual void updateMctpEndpointAvailability(const MctpInfo& mctpInfo,
46 Availability availability) = 0;
Thu Nguyen38e12aa2025-01-21 22:47:56 +000047 /** @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
Unive Tienc40d4a62025-03-12 11:36:07 +080055 virtual void handleConfigurations(const Configurations& /*configurations*/)
56 {}
Gilbert Chen44524a52022-02-14 12:12:25 +000057 virtual ~MctpDiscoveryHandlerIntf() {}
58};
Riya Dixit754041d2024-02-20 06:15:49 -060059
Tom Josephfb3bc062021-08-17 07:48:11 -070060class 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
Gilbert Chen44524a52022-02-14 12:12:25 +000074 * @param[in] list - initializer list to the MctpDiscoveryHandlerIntf
Tom Josephfb3bc062021-08-17 07:48:11 -070075 */
Gilbert Chen44524a52022-02-14 12:12:25 +000076 explicit MctpDiscovery(
Patrick Williamsba741d52024-05-08 02:32:10 -050077 sdbusplus::bus_t& bus,
Gilbert Chen44524a52022-02-14 12:12:25 +000078 std::initializer_list<MctpDiscoveryHandlerIntf*> list);
Tom Josephfb3bc062021-08-17 07:48:11 -070079
Tom Josephfb3bc062021-08-17 07:48:11 -070080 /** @brief reference to the systemd bus */
Patrick Williams84b790c2022-07-22 19:26:56 -050081 sdbusplus::bus_t& bus;
Tom Josephfb3bc062021-08-17 07:48:11 -070082
Tom Josephfb3bc062021-08-17 07:48:11 -070083 /** @brief Used to watch for new MCTP endpoints */
Gilbert Chen44524a52022-02-14 12:12:25 +000084 sdbusplus::bus::match_t mctpEndpointAddedSignal;
Tom Josephfb3bc062021-08-17 07:48:11 -070085
Gilbert Chen44524a52022-02-14 12:12:25 +000086 /** @brief Used to watch for the removed MCTP endpoints */
87 sdbusplus::bus::match_t mctpEndpointRemovedSignal;
Tom Josephfb3bc062021-08-17 07:48:11 -070088
Chau Ly75e00422024-03-19 12:33:08 +000089 /** @brief Used to watch for new MCTP endpoints */
90 sdbusplus::bus::match_t mctpEndpointPropChangedSignal;
91
Gilbert Chen44524a52022-02-14 12:12:25 +000092 /** @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
Chau Ly75e00422024-03-19 12:33:08 +000099 /** @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
Gilbert Chen44524a52022-02-14 12:12:25 +0000106 /** @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
Chau Ly75e00422024-03-19 12:33:08 +0000134 /** @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
Gilbert Chen44524a52022-02-14 12:12:25 +0000143 /** @brief Get list of MctpInfos in MCTP control interface.
144 *
Chau Ly75e00422024-03-19 12:33:08 +0000145 * @param[in] mctpInfoMap - information of discovered MCTP endpoints
146 * and the availability status of each endpoint
Gilbert Chen44524a52022-02-14 12:12:25 +0000147 */
Chau Ly75e00422024-03-19 12:33:08 +0000148 void getMctpInfos(std::map<MctpInfo, Availability>& mctpInfoMap);
Gilbert Chen44524a52022-02-14 12:12:25 +0000149
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
Unive Tienc40d4a62025-03-12 11:36:07 +0800171 friend class ::TestMctpDiscovery;
172
Gilbert Chen44524a52022-02-14 12:12:25 +0000173 private:
Thu Nguyen90274972024-07-17 07:02:16 +0000174 /** @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
Chau Ly75e00422024-03-19 12:33:08 +0000196 /** @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
Tom Josephfb3bc062021-08-17 07:48:11 -0700206 static constexpr uint8_t mctpTypePLDM = 1;
Unive Tienc40d4a62025-03-12 11:36:07 +0800207
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;
Tom Josephfb3bc062021-08-17 07:48:11 -0700240};
241
Patrick Williams6da4f912023-05-10 07:50:53 -0500242} // namespace pldm