blob: 76928492f612e836d9e2a4298cac44e9938db2ac [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>
9
Gilbert Chen44524a52022-02-14 12:12:25 +000010#include <filesystem>
11#include <initializer_list>
12#include <vector>
13
Unive Tienc40d4a62025-03-12 11:36:07 +080014class TestMctpDiscovery;
15
Tom Josephfb3bc062021-08-17 07:48:11 -070016namespace pldm
17{
18
Gilbert Chen44524a52022-02-14 12:12:25 +000019const std::string emptyUUID = "00000000-0000-0000-0000-000000000000";
Thu Nguyenef16cde2024-07-09 05:56:57 +000020constexpr const char* MCTPService = "au.com.codeconstruct.MCTP1";
Gilbert Chen44524a52022-02-14 12:12:25 +000021constexpr const char* MCTPInterface = "xyz.openbmc_project.MCTP.Endpoint";
Thu Nguyen90274972024-07-17 07:02:16 +000022constexpr const char* EndpointUUID = "xyz.openbmc_project.Common.UUID";
Thu Nguyenef16cde2024-07-09 05:56:57 +000023constexpr const char* MCTPPath = "/au/com/codeconstruct/mctp1";
Chau Ly75e00422024-03-19 12:33:08 +000024constexpr const char* MCTPInterfaceCC = "au.com.codeconstruct.MCTP.Endpoint1";
25constexpr const char* MCTPConnectivityProp = "Connectivity";
Unive Tienc40d4a62025-03-12 11:36:07 +080026constexpr const char* inventorySubtreePathStr =
27 "/xyz/openbmc_project/inventory/system";
28
29const std::vector<std::string> interfaceFilter = {
30 "xyz.openbmc_project.Configuration.MCTPI2CTarget",
31 "xyz.openbmc_project.Configuration.MCTPI3CTarget"};
Gilbert Chen44524a52022-02-14 12:12:25 +000032
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 */
38class MctpDiscoveryHandlerIntf
39{
40 public:
41 virtual void handleMctpEndpoints(const MctpInfos& mctpInfos) = 0;
42 virtual void handleRemovedMctpEndpoints(const MctpInfos& mctpInfos) = 0;
Chau Ly75e00422024-03-19 12:33:08 +000043 virtual void updateMctpEndpointAvailability(const MctpInfo& mctpInfo,
44 Availability availability) = 0;
Thu Nguyen38e12aa2025-01-21 22:47:56 +000045 /** @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
Unive Tienc40d4a62025-03-12 11:36:07 +080053 virtual void handleConfigurations(const Configurations& /*configurations*/)
54 {}
Gilbert Chen44524a52022-02-14 12:12:25 +000055 virtual ~MctpDiscoveryHandlerIntf() {}
56};
Riya Dixit754041d2024-02-20 06:15:49 -060057
Tom Josephfb3bc062021-08-17 07:48:11 -070058class 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
Gilbert Chen44524a52022-02-14 12:12:25 +000072 * @param[in] list - initializer list to the MctpDiscoveryHandlerIntf
Tom Josephfb3bc062021-08-17 07:48:11 -070073 */
Gilbert Chen44524a52022-02-14 12:12:25 +000074 explicit MctpDiscovery(
Patrick Williamsba741d52024-05-08 02:32:10 -050075 sdbusplus::bus_t& bus,
Gilbert Chen44524a52022-02-14 12:12:25 +000076 std::initializer_list<MctpDiscoveryHandlerIntf*> list);
Tom Josephfb3bc062021-08-17 07:48:11 -070077
Tom Josephfb3bc062021-08-17 07:48:11 -070078 /** @brief reference to the systemd bus */
Patrick Williams84b790c2022-07-22 19:26:56 -050079 sdbusplus::bus_t& bus;
Tom Josephfb3bc062021-08-17 07:48:11 -070080
Tom Josephfb3bc062021-08-17 07:48:11 -070081 /** @brief Used to watch for new MCTP endpoints */
Gilbert Chen44524a52022-02-14 12:12:25 +000082 sdbusplus::bus::match_t mctpEndpointAddedSignal;
Tom Josephfb3bc062021-08-17 07:48:11 -070083
Gilbert Chen44524a52022-02-14 12:12:25 +000084 /** @brief Used to watch for the removed MCTP endpoints */
85 sdbusplus::bus::match_t mctpEndpointRemovedSignal;
Tom Josephfb3bc062021-08-17 07:48:11 -070086
Chau Ly75e00422024-03-19 12:33:08 +000087 /** @brief Used to watch for new MCTP endpoints */
88 sdbusplus::bus::match_t mctpEndpointPropChangedSignal;
89
Gilbert Chen44524a52022-02-14 12:12:25 +000090 /** @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
Chau Ly75e00422024-03-19 12:33:08 +000097 /** @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
Gilbert Chen44524a52022-02-14 12:12:25 +0000104 /** @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
Chau Ly75e00422024-03-19 12:33:08 +0000132 /** @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
Gilbert Chen44524a52022-02-14 12:12:25 +0000141 /** @brief Get list of MctpInfos in MCTP control interface.
142 *
Chau Ly75e00422024-03-19 12:33:08 +0000143 * @param[in] mctpInfoMap - information of discovered MCTP endpoints
144 * and the availability status of each endpoint
Gilbert Chen44524a52022-02-14 12:12:25 +0000145 */
Chau Ly75e00422024-03-19 12:33:08 +0000146 void getMctpInfos(std::map<MctpInfo, Availability>& mctpInfoMap);
Gilbert Chen44524a52022-02-14 12:12:25 +0000147
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
Unive Tienc40d4a62025-03-12 11:36:07 +0800169 friend class ::TestMctpDiscovery;
170
Gilbert Chen44524a52022-02-14 12:12:25 +0000171 private:
Thu Nguyen90274972024-07-17 07:02:16 +0000172 /** @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
Chau Ly75e00422024-03-19 12:33:08 +0000194 /** @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
Tom Josephfb3bc062021-08-17 07:48:11 -0700204 static constexpr uint8_t mctpTypePLDM = 1;
Unive Tienc40d4a62025-03-12 11:36:07 +0800205
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;
Tom Josephfb3bc062021-08-17 07:48:11 -0700238};
239
Patrick Williams6da4f912023-05-10 07:50:53 -0500240} // namespace pldm