| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 3 | #include "common/types.hpp" |
| 4 | #include "common/utils.hpp" |
| 5 | |
| 6 | #include <libpldm/pldm.h> |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 7 | |
| 8 | #include <sdbusplus/bus/match.hpp> |
| Alexander Hansen | 1f2794d | 2025-11-11 15:58:13 +0100 | [diff] [blame^] | 9 | #include <xyz/openbmc_project/MCTP/Endpoint/client.hpp> |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 10 | |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 11 | #include <filesystem> |
| 12 | #include <initializer_list> |
| 13 | #include <vector> |
| 14 | |
| Alexander Hansen | 1f2794d | 2025-11-11 15:58:13 +0100 | [diff] [blame^] | 15 | using MCTPEndpoint = sdbusplus::common::xyz::openbmc_project::mctp::Endpoint; |
| 16 | |
| Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame] | 17 | class TestMctpDiscovery; |
| 18 | |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 19 | namespace pldm |
| 20 | { |
| 21 | |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 22 | const std::string emptyUUID = "00000000-0000-0000-0000-000000000000"; |
| Thu Nguyen | ef16cde | 2024-07-09 05:56:57 +0000 | [diff] [blame] | 23 | constexpr const char* MCTPService = "au.com.codeconstruct.MCTP1"; |
| Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 24 | constexpr const char* EndpointUUID = "xyz.openbmc_project.Common.UUID"; |
| Thu Nguyen | ef16cde | 2024-07-09 05:56:57 +0000 | [diff] [blame] | 25 | constexpr const char* MCTPPath = "/au/com/codeconstruct/mctp1"; |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 26 | constexpr const char* MCTPInterfaceCC = "au.com.codeconstruct.MCTP.Endpoint1"; |
| 27 | constexpr const char* MCTPConnectivityProp = "Connectivity"; |
| Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame] | 28 | constexpr const char* inventorySubtreePathStr = |
| 29 | "/xyz/openbmc_project/inventory/system"; |
| 30 | |
| 31 | const std::vector<std::string> interfaceFilter = { |
| 32 | "xyz.openbmc_project.Configuration.MCTPI2CTarget", |
| 33 | "xyz.openbmc_project.Configuration.MCTPI3CTarget"}; |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 34 | |
| 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 | */ |
| 40 | class MctpDiscoveryHandlerIntf |
| 41 | { |
| 42 | public: |
| 43 | virtual void handleMctpEndpoints(const MctpInfos& mctpInfos) = 0; |
| 44 | virtual void handleRemovedMctpEndpoints(const MctpInfos& mctpInfos) = 0; |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 45 | virtual void updateMctpEndpointAvailability(const MctpInfo& mctpInfo, |
| 46 | Availability availability) = 0; |
| Thu Nguyen | 38e12aa | 2025-01-21 22:47:56 +0000 | [diff] [blame] | 47 | /** @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 Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame] | 55 | virtual void handleConfigurations(const Configurations& /*configurations*/) |
| 56 | {} |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 57 | virtual ~MctpDiscoveryHandlerIntf() {} |
| 58 | }; |
| Riya Dixit | 754041d | 2024-02-20 06:15:49 -0600 | [diff] [blame] | 59 | |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 60 | class 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 Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 74 | * @param[in] list - initializer list to the MctpDiscoveryHandlerIntf |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 75 | */ |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 76 | explicit MctpDiscovery( |
| Patrick Williams | ba741d5 | 2024-05-08 02:32:10 -0500 | [diff] [blame] | 77 | sdbusplus::bus_t& bus, |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 78 | std::initializer_list<MctpDiscoveryHandlerIntf*> list); |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 79 | |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 80 | /** @brief reference to the systemd bus */ |
| Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 81 | sdbusplus::bus_t& bus; |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 82 | |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 83 | /** @brief Used to watch for new MCTP endpoints */ |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 84 | sdbusplus::bus::match_t mctpEndpointAddedSignal; |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 85 | |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 86 | /** @brief Used to watch for the removed MCTP endpoints */ |
| 87 | sdbusplus::bus::match_t mctpEndpointRemovedSignal; |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 88 | |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 89 | /** @brief Used to watch for new MCTP endpoints */ |
| 90 | sdbusplus::bus::match_t mctpEndpointPropChangedSignal; |
| 91 | |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 92 | /** @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 Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 99 | /** @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 Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 106 | /** @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 Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 134 | /** @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 Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 143 | /** @brief Get list of MctpInfos in MCTP control interface. |
| 144 | * |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 145 | * @param[in] mctpInfoMap - information of discovered MCTP endpoints |
| 146 | * and the availability status of each endpoint |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 147 | */ |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 148 | void getMctpInfos(std::map<MctpInfo, Availability>& mctpInfoMap); |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 149 | |
| 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 Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame] | 171 | friend class ::TestMctpDiscovery; |
| 172 | |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 173 | private: |
| Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 174 | /** @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 Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 196 | /** @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 Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 206 | static constexpr uint8_t mctpTypePLDM = 1; |
| Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame] | 207 | |
| 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 Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 240 | }; |
| 241 | |
| Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 242 | } // namespace pldm |