blob: 403b15fbf921cd67cd5cf47112eef2a79ad921a7 [file] [log] [blame]
Unive Tien852160b2024-12-06 15:58:21 +08001#pragma once
2
3#include "common/utils.hpp"
4#include "mctp_endpoint_discovery.hpp"
5
6#include <stdexcept>
7
8namespace pldm
9{
10
11struct MctpEndpoint
12{
13 uint64_t address;
14 uint64_t EndpointId;
15 uint64_t bus;
16 std::string name;
17 std::optional<std::string> iana;
18};
19
20class ConfigurationDiscoveryHandler : public MctpDiscoveryHandlerIntf
21{
22 public:
23 ConfigurationDiscoveryHandler() = delete;
24 ConfigurationDiscoveryHandler(const ConfigurationDiscoveryHandler&) =
25 delete;
26 ConfigurationDiscoveryHandler(ConfigurationDiscoveryHandler&&) = delete;
27 ConfigurationDiscoveryHandler&
28 operator=(const ConfigurationDiscoveryHandler&) = delete;
29 ConfigurationDiscoveryHandler&
30 operator=(ConfigurationDiscoveryHandler&&) = delete;
31 ~ConfigurationDiscoveryHandler() = default;
32
33 explicit ConfigurationDiscoveryHandler(
34 const pldm::utils::DBusHandler* dBusIntf) : dBusIntf(dBusIntf)
35 {}
36
37 /** @brief Discover configuration associated with the new MCTP endpoints.
38 *
39 * @param[in] newMctpInfos - information of discovered MCTP endpoint
40 */
41 void handleMctpEndpoints(const MctpInfos& newMctpInfos);
42
43 /** @brief Remove configuration associated with the removed MCTP endpoints.
44 *
45 * @param[in] removedMctpInfos - the removed MCTP endpoints
46 */
47 void handleRemovedMctpEndpoints(const MctpInfos& removedMctpInfos);
48
49 /** @brief Get existing configurations.
50 *
51 * @return The configurations.
52 */
53 std::map<std::string, MctpEndpoint>& getConfigurations();
54
55 private:
56 /** @brief Search for associated configuration for the MctpInfo.
57 *
58 * @param[in] mctpInfo - information of discovered MCTP endpoint
59 */
60 void searchConfigurationFor(MctpInfo mctpInfo);
61
62 /** @brief Append to configurations if Endpoint Id is matched.
63 *
64 * @param[in] targetEid - discovered MCTP endpoint Id
65 * @param[in] configPath - the Dbus path of a configuration
66 * @param[in] serviceMap - the map contains the service's information who
67 * expose the configuration
68 */
69 void
70 appendConfigIfEidMatch(uint8_t targetEid, const std::string& configPath,
71 const pldm::utils::MapperServiceMap& serviceMap);
72
73 /** @brief Parse MctpEndpoint from the response of GetAll method.
74 *
75 * @param[in] response - Response data of GetAll method
76 *
77 * @return Parsed MctpEndpoint object.
78 */
79 MctpEndpoint
80 parseMctpEndpointFromResponse(const pldm::utils::PropertyMap& response);
81
82 /** @brief Append to configuration if the MctpEndpoint's EID matches
83 * targetEid.
84 *
85 * @param[in] targetEid - The target EID
86 * @param[in] configPath - Discovered configuration's Dbus path
87 * @param[in] endpoint - The configuration's MctpEndpoint information.
88 */
89 void appendIfEidMatch(uint8_t targetEid, const std::string& configPath,
90 const MctpEndpoint& endpoint);
91
92 /** @brief Remove configuration associated with the removed MCTP endpoint.
93 *
94 * @param[in] eid - endpoint Id of the removed MCTP Endpoint
95 */
96 void removeConfigByEid(uint8_t eid);
97
98 /** @brief The configuration contains Dbus path and the MCTP endpoint
99 * information.
100 */
101 std::map<std::string /*configDbusPath*/, MctpEndpoint> configurations;
102
103 /** @brief D-Bus Interface object*/
104 const pldm::utils::DBusHandler* dBusIntf;
105};
106
107class NoSuchPropertiesException : public std::exception
108{
109 public:
110 NoSuchPropertiesException() = delete;
111 ~NoSuchPropertiesException() = default;
112
113 explicit NoSuchPropertiesException(
114 const std::vector<std::string> properties)
115 {
116 std::string missingProperties{};
117 for (const auto& property : properties)
118 {
119 missingProperties += std::string(property) + " ";
120 }
121 message = "Interface has no properties: " + missingProperties;
122 }
123
124 const char* what() const noexcept override
125 {
126 return message.c_str();
127 }
128
129 private:
130 std::string message;
131};
132
133} // namespace pldm