blob: 3c37e44702c9288ed1060e47a55f01cff2a88cf8 [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 Hansend20f0ac2025-11-26 14:16:01 +01009#include <xyz/openbmc_project/Common/UUID/common.hpp>
Alexander Hansen1f2794d2025-11-11 15:58:13 +010010#include <xyz/openbmc_project/MCTP/Endpoint/client.hpp>
Tom Josephfb3bc062021-08-17 07:48:11 -070011
Gilbert Chen44524a52022-02-14 12:12:25 +000012#include <filesystem>
13#include <initializer_list>
14#include <vector>
15
Alexander Hansen1f2794d2025-11-11 15:58:13 +010016using MCTPEndpoint = sdbusplus::common::xyz::openbmc_project::mctp::Endpoint;
Alexander Hansend20f0ac2025-11-26 14:16:01 +010017using CommonUUID = sdbusplus::common::xyz::openbmc_project::common::UUID;
Alexander Hansen1f2794d2025-11-11 15:58:13 +010018
Unive Tienc40d4a62025-03-12 11:36:07 +080019class TestMctpDiscovery;
20
Tom Josephfb3bc062021-08-17 07:48:11 -070021namespace pldm
22{
23
Gilbert Chen44524a52022-02-14 12:12:25 +000024const std::string emptyUUID = "00000000-0000-0000-0000-000000000000";
Thu Nguyenef16cde2024-07-09 05:56:57 +000025constexpr const char* MCTPService = "au.com.codeconstruct.MCTP1";
Thu Nguyenef16cde2024-07-09 05:56:57 +000026constexpr const char* MCTPPath = "/au/com/codeconstruct/mctp1";
Chau Ly75e00422024-03-19 12:33:08 +000027constexpr const char* MCTPInterfaceCC = "au.com.codeconstruct.MCTP.Endpoint1";
28constexpr const char* MCTPConnectivityProp = "Connectivity";
Unive Tienc40d4a62025-03-12 11:36:07 +080029constexpr const char* inventorySubtreePathStr =
30 "/xyz/openbmc_project/inventory/system";
31
32const std::vector<std::string> interfaceFilter = {
33 "xyz.openbmc_project.Configuration.MCTPI2CTarget",
34 "xyz.openbmc_project.Configuration.MCTPI3CTarget"};
Gilbert Chen44524a52022-02-14 12:12:25 +000035
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 */
41class MctpDiscoveryHandlerIntf
42{
43 public:
44 virtual void handleMctpEndpoints(const MctpInfos& mctpInfos) = 0;
45 virtual void handleRemovedMctpEndpoints(const MctpInfos& mctpInfos) = 0;
Chau Ly75e00422024-03-19 12:33:08 +000046 virtual void updateMctpEndpointAvailability(const MctpInfo& mctpInfo,
47 Availability availability) = 0;
Thu Nguyen38e12aa2025-01-21 22:47:56 +000048 /** @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
Unive Tienc40d4a62025-03-12 11:36:07 +080056 virtual void handleConfigurations(const Configurations& /*configurations*/)
57 {}
Gilbert Chen44524a52022-02-14 12:12:25 +000058 virtual ~MctpDiscoveryHandlerIntf() {}
59};
Riya Dixit754041d2024-02-20 06:15:49 -060060
Tom Josephfb3bc062021-08-17 07:48:11 -070061class 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
Gilbert Chen44524a52022-02-14 12:12:25 +000075 * @param[in] list - initializer list to the MctpDiscoveryHandlerIntf
Tom Josephfb3bc062021-08-17 07:48:11 -070076 */
Gilbert Chen44524a52022-02-14 12:12:25 +000077 explicit MctpDiscovery(
Patrick Williamsba741d52024-05-08 02:32:10 -050078 sdbusplus::bus_t& bus,
Gilbert Chen44524a52022-02-14 12:12:25 +000079 std::initializer_list<MctpDiscoveryHandlerIntf*> list);
Tom Josephfb3bc062021-08-17 07:48:11 -070080
Tom Josephfb3bc062021-08-17 07:48:11 -070081 /** @brief reference to the systemd bus */
Patrick Williams84b790c2022-07-22 19:26:56 -050082 sdbusplus::bus_t& bus;
Tom Josephfb3bc062021-08-17 07:48:11 -070083
Tom Josephfb3bc062021-08-17 07:48:11 -070084 /** @brief Used to watch for new MCTP endpoints */
Gilbert Chen44524a52022-02-14 12:12:25 +000085 sdbusplus::bus::match_t mctpEndpointAddedSignal;
Tom Josephfb3bc062021-08-17 07:48:11 -070086
Gilbert Chen44524a52022-02-14 12:12:25 +000087 /** @brief Used to watch for the removed MCTP endpoints */
88 sdbusplus::bus::match_t mctpEndpointRemovedSignal;
Tom Josephfb3bc062021-08-17 07:48:11 -070089
Chau Ly75e00422024-03-19 12:33:08 +000090 /** @brief Used to watch for new MCTP endpoints */
91 sdbusplus::bus::match_t mctpEndpointPropChangedSignal;
92
Gilbert Chen44524a52022-02-14 12:12:25 +000093 /** @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
Chau Ly75e00422024-03-19 12:33:08 +0000100 /** @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
Gilbert Chen44524a52022-02-14 12:12:25 +0000107 /** @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
Chau Ly75e00422024-03-19 12:33:08 +0000135 /** @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
Gilbert Chen44524a52022-02-14 12:12:25 +0000144 /** @brief Get list of MctpInfos in MCTP control interface.
145 *
Chau Ly75e00422024-03-19 12:33:08 +0000146 * @param[in] mctpInfoMap - information of discovered MCTP endpoints
147 * and the availability status of each endpoint
Gilbert Chen44524a52022-02-14 12:12:25 +0000148 */
Chau Ly75e00422024-03-19 12:33:08 +0000149 void getMctpInfos(std::map<MctpInfo, Availability>& mctpInfoMap);
Gilbert Chen44524a52022-02-14 12:12:25 +0000150
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
Unive Tienc40d4a62025-03-12 11:36:07 +0800172 friend class ::TestMctpDiscovery;
173
Gilbert Chen44524a52022-02-14 12:12:25 +0000174 private:
Thu Nguyen90274972024-07-17 07:02:16 +0000175 /** @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
Chau Ly75e00422024-03-19 12:33:08 +0000197 /** @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
Tom Josephfb3bc062021-08-17 07:48:11 -0700207 static constexpr uint8_t mctpTypePLDM = 1;
Unive Tienc40d4a62025-03-12 11:36:07 +0800208
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;
Tom Josephfb3bc062021-08-17 07:48:11 -0700241};
242
Patrick Williams6da4f912023-05-10 07:50:53 -0500243} // namespace pldm