Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 1 | #pragma once |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 2 | |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 3 | #include <boost/system/error_code.hpp> |
Yong Li | 7dc4ac0 | 2019-08-23 17:44:32 +0800 | [diff] [blame] | 4 | #include <ipmid/api-types.hpp> |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 5 | #include <ipmid/message.hpp> |
Vernon Mauery | 3325024 | 2019-03-12 16:49:26 -0700 | [diff] [blame] | 6 | #include <ipmid/types.hpp> |
George Liu | de1420d | 2025-03-03 15:14:25 +0800 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 8 | #include <sdbusplus/server.hpp> |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 9 | |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 10 | #include <chrono> |
| 11 | #include <optional> |
Prithvi Pai | c1e7b5c | 2025-09-01 13:13:15 +0530 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 14 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 15 | namespace ipmi |
| 16 | { |
| 17 | |
Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 18 | using namespace std::literals::chrono_literals; |
| 19 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 20 | constexpr auto MAPPER_BUS_NAME = "xyz.openbmc_project.ObjectMapper"; |
| 21 | constexpr auto MAPPER_OBJ = "/xyz/openbmc_project/object_mapper"; |
| 22 | constexpr auto MAPPER_INTF = "xyz.openbmc_project.ObjectMapper"; |
| 23 | |
| 24 | constexpr auto ROOT = "/"; |
| 25 | constexpr auto HOST_MATCH = "host0"; |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 26 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 27 | constexpr auto PROP_INTF = "org.freedesktop.DBus.Properties"; |
| 28 | constexpr auto DELETE_INTERFACE = "xyz.openbmc_project.Object.Delete"; |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 29 | |
| 30 | constexpr auto METHOD_GET = "Get"; |
| 31 | constexpr auto METHOD_GET_ALL = "GetAll"; |
| 32 | constexpr auto METHOD_SET = "Set"; |
| 33 | |
Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 34 | /* Use a value of 5s which aligns with BT/KCS bridged timeouts, rather |
| 35 | * than the default 25s D-Bus timeout. */ |
| 36 | constexpr std::chrono::microseconds IPMI_DBUS_TIMEOUT = 5s; |
| 37 | |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 38 | /** @class ServiceCache |
| 39 | * @brief Caches lookups of service names from the object mapper. |
| 40 | * @details Most ipmi commands need to talk to other dbus daemons to perform |
| 41 | * their intended actions on the BMC. This usually means they will |
| 42 | * first look up the service name providing the interface they |
| 43 | * require. This class reduces the number of such calls by caching |
| 44 | * the lookup for a specific service. |
| 45 | */ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 46 | class ServiceCache |
| 47 | { |
| 48 | public: |
| 49 | /** @brief Creates a new service cache for the given interface |
| 50 | * and path. |
| 51 | * |
| 52 | * @param[in] intf - The interface used for each lookup |
| 53 | * @param[in] path - The path used for each lookup |
| 54 | */ |
| 55 | ServiceCache(const std::string& intf, const std::string& path); |
| 56 | ServiceCache(std::string&& intf, std::string&& path); |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 57 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 58 | /** @brief Gets the service name from the cache or does in a |
| 59 | * lookup when invalid. |
| 60 | * |
| 61 | * @param[in] bus - The bus associated with and used for looking |
| 62 | * up the service. |
| 63 | */ |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 64 | const std::string& getService(sdbusplus::bus_t& bus); |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 65 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 66 | /** @brief Invalidates the current service name */ |
| 67 | void invalidate(); |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 68 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 69 | /** @brief A wrapper around sdbusplus bus.new_method_call |
| 70 | * |
| 71 | * @param[in] bus - The bus used for calling the method |
| 72 | * @param[in] intf - The interface containing the method |
| 73 | * @param[in] method - The method name |
| 74 | * @return The message containing the method call. |
| 75 | */ |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 76 | sdbusplus::message_t newMethodCall(sdbusplus::bus_t& bus, const char* intf, |
| 77 | const char* method); |
William A. Kennington III | 82c173a | 2018-05-11 16:10:12 -0700 | [diff] [blame] | 78 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 79 | /** @brief Check to see if the current cache is valid |
| 80 | * |
| 81 | * @param[in] bus - The bus used for the service lookup |
| 82 | * @return True if the cache is valid false otherwise. |
| 83 | */ |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 84 | bool isValid(sdbusplus::bus_t& bus) const; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | /** @brief DBUS interface provided by the service */ |
| 88 | const std::string intf; |
| 89 | /** @brief DBUS path provided by the service */ |
| 90 | const std::string path; |
| 91 | /** @brief The name of the service if valid */ |
Vernon Mauery | 5401250 | 2018-11-07 10:17:31 -0800 | [diff] [blame] | 92 | std::optional<std::string> cachedService; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 93 | /** @brief The name of the bus used in the service lookup */ |
Vernon Mauery | 5401250 | 2018-11-07 10:17:31 -0800 | [diff] [blame] | 94 | std::optional<std::string> cachedBusName; |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 97 | /** |
| 98 | * @brief Get the DBUS Service name for the input dbus path |
| 99 | * |
| 100 | * @param[in] bus - DBUS Bus Object |
| 101 | * @param[in] intf - DBUS Interface |
| 102 | * @param[in] path - DBUS Object Path |
| 103 | * |
| 104 | */ |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 105 | std::string getService(sdbusplus::bus_t& bus, const std::string& intf, |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 106 | const std::string& path); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 107 | |
George Liu | 50f186c | 2024-02-04 16:51:26 +0800 | [diff] [blame] | 108 | /** @brief Gets the dbus sub tree implementing the given interface. |
| 109 | * @param[in] bus - DBUS Bus Object. |
| 110 | * @param[in] interfaces - Dbus interface. |
| 111 | * @param[in] subtreePath - subtree from where the search should start. |
| 112 | * @param[in] depth - Search depth |
| 113 | * @return map of object path and service info. |
| 114 | */ |
| 115 | ObjectTree getSubTree(sdbusplus::bus_t& bus, const InterfaceList& interface, |
| 116 | const std::string& subtreePath = ROOT, int32_t depth = 0); |
| 117 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 118 | /** @brief Gets the dbus object info implementing the given interface |
| 119 | * from the given subtree. |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 120 | * @param[in] bus - DBUS Bus Object. |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 121 | * @param[in] interface - Dbus interface. |
| 122 | * @param[in] subtreePath - subtree from where the search should start. |
| 123 | * @param[in] match - identifier for object. |
| 124 | * @return On success returns the object having objectpath and servicename. |
| 125 | */ |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 126 | DbusObjectInfo getDbusObject( |
| 127 | sdbusplus::bus_t& bus, const std::string& interface, |
| 128 | const std::string& subtreePath = ROOT, const std::string& match = {}); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 129 | |
| 130 | /** @brief Gets the value associated with the given object |
| 131 | * and the interface. |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 132 | * @param[in] bus - DBUS Bus Object. |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 133 | * @param[in] service - Dbus service name. |
| 134 | * @param[in] objPath - Dbus object path. |
| 135 | * @param[in] interface - Dbus interface. |
| 136 | * @param[in] property - name of the property. |
| 137 | * @return On success returns the value of the property. |
| 138 | */ |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 139 | Value getDbusProperty(sdbusplus::bus_t& bus, const std::string& service, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 140 | const std::string& objPath, const std::string& interface, |
Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 141 | const std::string& property, |
| 142 | std::chrono::microseconds timeout = IPMI_DBUS_TIMEOUT); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 143 | |
| 144 | /** @brief Gets all the properties associated with the given object |
| 145 | * and the interface. |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 146 | * @param[in] bus - DBUS Bus Object. |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 147 | * @param[in] service - Dbus service name. |
| 148 | * @param[in] objPath - Dbus object path. |
| 149 | * @param[in] interface - Dbus interface. |
| 150 | * @return On success returns the map of name value pair. |
| 151 | */ |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 152 | PropertyMap getAllDbusProperties( |
| 153 | sdbusplus::bus_t& bus, const std::string& service, |
| 154 | const std::string& objPath, const std::string& interface, |
| 155 | std::chrono::microseconds timeout = IPMI_DBUS_TIMEOUT); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 156 | |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 157 | /** @brief Gets all managed objects associated with the given object |
| 158 | * path and service. |
| 159 | * @param[in] bus - D-Bus Bus Object. |
| 160 | * @param[in] service - D-Bus service name. |
| 161 | * @param[in] objPath - D-Bus object path. |
| 162 | * @return On success returns the map of name value pair. |
| 163 | */ |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 164 | ObjectValueTree getManagedObjects(sdbusplus::bus_t& bus, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 165 | const std::string& service, |
| 166 | const std::string& objPath); |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 167 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 168 | /** @brief Sets the property value of the given object. |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 169 | * @param[in] bus - DBUS Bus Object. |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 170 | * @param[in] service - Dbus service name. |
| 171 | * @param[in] objPath - Dbus object path. |
| 172 | * @param[in] interface - Dbus interface. |
| 173 | * @param[in] property - name of the property. |
| 174 | * @param[in] value - value which needs to be set. |
| 175 | */ |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 176 | void setDbusProperty(sdbusplus::bus_t& bus, const std::string& service, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 177 | const std::string& objPath, const std::string& interface, |
Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 178 | const std::string& property, const Value& value, |
| 179 | std::chrono::microseconds timeout = IPMI_DBUS_TIMEOUT); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 180 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 181 | /** @brief Gets all the dbus objects from the given service root |
| 182 | * which matches the object identifier. |
| 183 | * @param[in] bus - DBUS Bus Object. |
| 184 | * @param[in] serviceRoot - Service root path. |
| 185 | * @param[in] interface - Dbus interface. |
| 186 | * @param[in] match - Identifier for a path. |
| 187 | * @returns map of object path and service info. |
| 188 | */ |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 189 | ObjectTree getAllDbusObjects( |
| 190 | sdbusplus::bus_t& bus, const std::string& serviceRoot, |
| 191 | const std::string& interface, const std::string& match = {}); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 192 | |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 193 | /********* Begin co-routine yielding alternatives ***************/ |
| 194 | |
| 195 | /** @brief Get the D-Bus Service name for the input D-Bus path |
| 196 | * |
| 197 | * @param[in] ctx - ipmi::Context::ptr |
| 198 | * @param[in] intf - D-Bus Interface |
| 199 | * @param[in] path - D-Bus Object Path |
| 200 | * @param[out] service - requested service name |
| 201 | * @return boost error code |
| 202 | * |
| 203 | */ |
Patrick Williams | 69b4c28 | 2025-03-03 11:19:13 -0500 | [diff] [blame] | 204 | boost::system::error_code getService(Context::ptr ctx, const std::string& intf, |
| 205 | const std::string& path, |
| 206 | std::string& service); |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 207 | |
George Liu | 50f186c | 2024-02-04 16:51:26 +0800 | [diff] [blame] | 208 | /** @brief Gets the dbus sub tree implementing the given interface. |
| 209 | * @param[in] ctx - ipmi::Context::ptr |
| 210 | * @param[in] bus - DBUS Bus Object. |
| 211 | * @param[in] interfaces - Dbus interface. |
| 212 | * @param[in] subtreePath - subtree from where the search should start. |
| 213 | * @param[in] depth - Search depth |
| 214 | * @param[out] objectTree - map of object path and service info. |
| 215 | * @return map of object path and service info. |
| 216 | */ |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 217 | boost::system::error_code getSubTree( |
| 218 | Context::ptr ctx, const InterfaceList& interface, |
| 219 | const std::string& subtreePath, int32_t depth, ObjectTree& objectTree); |
George Liu | 50f186c | 2024-02-04 16:51:26 +0800 | [diff] [blame] | 220 | |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 221 | /** @brief Gets the D-Bus object info implementing the given interface |
| 222 | * from the given subtree. |
| 223 | * @param[in] ctx - ipmi::Context::ptr |
| 224 | * @param[in] interface - D-Bus interface. |
| 225 | * @param[in][optional] subtreePath - subtree from where the search starts. |
| 226 | * @param[in][optional] match - identifier for object. |
| 227 | * @param[out] D-Bus object with path and service name |
| 228 | * @return - boost error code object |
| 229 | */ |
Patrick Williams | 69b4c28 | 2025-03-03 11:19:13 -0500 | [diff] [blame] | 230 | boost::system::error_code getDbusObject( |
| 231 | Context::ptr ctx, const std::string& interface, |
| 232 | const std::string& subtreePath, const std::string& match, |
| 233 | DbusObjectInfo& dbusObject); |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 234 | |
| 235 | // default for ROOT for subtreePath and std::string{} for match |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 236 | static inline boost::system::error_code getDbusObject( |
| 237 | Context::ptr ctx, const std::string& interface, DbusObjectInfo& dbusObject) |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 238 | { |
| 239 | return getDbusObject(ctx, interface, ROOT, {}, dbusObject); |
| 240 | } |
| 241 | |
| 242 | // default std::string{} for match |
Patrick Williams | 69b4c28 | 2025-03-03 11:19:13 -0500 | [diff] [blame] | 243 | static inline boost::system::error_code getDbusObject( |
| 244 | Context::ptr ctx, const std::string& interface, |
| 245 | const std::string& subtreePath, DbusObjectInfo& dbusObject) |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 246 | { |
| 247 | return getDbusObject(ctx, interface, subtreePath, {}, dbusObject); |
| 248 | } |
| 249 | |
| 250 | /** @brief Gets the value associated with the given object |
| 251 | * and the interface. |
| 252 | * @param[in] ctx - ipmi::Context::ptr |
| 253 | * @param[in] service - D-Bus service name. |
| 254 | * @param[in] objPath - D-Bus object path. |
| 255 | * @param[in] interface - D-Bus interface. |
| 256 | * @param[in] property - name of the property. |
| 257 | * @param[out] propertyValue - value of the D-Bus property. |
| 258 | * @return - boost error code object |
| 259 | */ |
| 260 | template <typename Type> |
Patrick Williams | 69b4c28 | 2025-03-03 11:19:13 -0500 | [diff] [blame] | 261 | boost::system::error_code getDbusProperty( |
| 262 | Context::ptr ctx, const std::string& service, const std::string& objPath, |
| 263 | const std::string& interface, const std::string& property, |
| 264 | Type& propertyValue) |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 265 | { |
| 266 | boost::system::error_code ec; |
| 267 | auto variant = ctx->bus->yield_method_call<std::variant<Type>>( |
| 268 | ctx->yield, ec, service.c_str(), objPath.c_str(), PROP_INTF, METHOD_GET, |
| 269 | interface, property); |
| 270 | if (!ec) |
| 271 | { |
| 272 | Type* tmp = std::get_if<Type>(&variant); |
| 273 | if (tmp) |
| 274 | { |
| 275 | propertyValue = *tmp; |
| 276 | return ec; |
| 277 | } |
| 278 | // user requested incorrect type; make an error code for them |
| 279 | ec = boost::system::errc::make_error_code( |
| 280 | boost::system::errc::invalid_argument); |
| 281 | } |
| 282 | return ec; |
| 283 | } |
| 284 | |
| 285 | /** @brief Gets all the properties associated with the given object |
| 286 | * and the interface. |
| 287 | * @param[in] ctx - ipmi::Context::ptr |
| 288 | * @param[in] service - D-Bus service name. |
| 289 | * @param[in] objPath - D-Bus object path. |
| 290 | * @param[in] interface - D-Bus interface. |
| 291 | * @param[out] properties - map of name value pair. |
| 292 | * @return - boost error code object |
| 293 | */ |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 294 | boost::system::error_code getAllDbusProperties( |
| 295 | Context::ptr ctx, const std::string& service, const std::string& objPath, |
| 296 | const std::string& interface, PropertyMap& properties); |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 297 | |
| 298 | /** @brief Sets the property value of the given object. |
| 299 | * @param[in] ctx - ipmi::Context::ptr |
| 300 | * @param[in] service - D-Bus service name. |
| 301 | * @param[in] objPath - D-Bus object path. |
| 302 | * @param[in] interface - D-Bus interface. |
| 303 | * @param[in] property - name of the property. |
| 304 | * @param[in] value - value which needs to be set. |
| 305 | * @return - boost error code object |
| 306 | */ |
Patrick Williams | 69b4c28 | 2025-03-03 11:19:13 -0500 | [diff] [blame] | 307 | boost::system::error_code setDbusProperty( |
| 308 | Context::ptr ctx, const std::string& service, const std::string& objPath, |
| 309 | const std::string& interface, const std::string& property, |
| 310 | const Value& value); |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 311 | |
| 312 | /** @brief Gets all the D-Bus objects from the given service root |
| 313 | * which matches the object identifier. |
| 314 | * @param[in] ctx - ipmi::Context::ptr |
| 315 | * @param[in] serviceRoot - Service root path. |
| 316 | * @param[in] interface - D-Bus interface. |
| 317 | * @param[in][optional] match - Identifier for a path. |
| 318 | * @param[out] objectree - map of object path and service info. |
| 319 | * @return - boost error code object |
| 320 | */ |
Patrick Williams | 69b4c28 | 2025-03-03 11:19:13 -0500 | [diff] [blame] | 321 | boost::system::error_code getAllDbusObjects( |
| 322 | Context::ptr ctx, const std::string& serviceRoot, |
| 323 | const std::string& interface, const std::string& match, |
| 324 | ObjectTree& objectTree); |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 325 | |
| 326 | // default std::string{} for match |
Patrick Williams | 69b4c28 | 2025-03-03 11:19:13 -0500 | [diff] [blame] | 327 | static inline boost::system::error_code getAllDbusObjects( |
| 328 | Context::ptr ctx, const std::string& serviceRoot, |
| 329 | const std::string& interface, ObjectTree& objectTree) |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 330 | { |
| 331 | return getAllDbusObjects(ctx, serviceRoot, interface, {}, objectTree); |
| 332 | } |
| 333 | |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 334 | /** @brief Gets all managed objects associated with the given object |
| 335 | * path and service. |
| 336 | * @param[in] ctx - ipmi::Context::ptr |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 337 | * @param[in] service - D-Bus service name. |
| 338 | * @param[in] objPath - D-Bus object path. |
Vernon Mauery | fe39ec9 | 2020-03-02 13:58:41 -0800 | [diff] [blame] | 339 | * @param[out] objects - map of name value pair. |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 340 | * @return - boost error code object |
| 341 | */ |
Patrick Williams | 69b4c28 | 2025-03-03 11:19:13 -0500 | [diff] [blame] | 342 | boost::system::error_code getManagedObjects( |
| 343 | Context::ptr ctx, const std::string& service, const std::string& objPath, |
| 344 | ObjectValueTree& objects); |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 345 | |
Albert Zhang | b53049e | 2022-04-02 15:39:51 +0800 | [diff] [blame] | 346 | /** @brief Gets the value associated with the given object |
| 347 | * and the interface. |
| 348 | * @param[in] ctx - ipmi::Context::ptr |
| 349 | * @param[in] service - D-Bus service name. |
| 350 | * @param[in] objPath - D-Bus object path. |
| 351 | * @param[in] interface - D-Bus interface. |
| 352 | * @param[in] method - name of the method. |
| 353 | * @return - boost error code object |
| 354 | */ |
| 355 | |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 356 | boost::system::error_code callDbusMethod( |
| 357 | Context::ptr ctx, const std::string& service, const std::string& objPath, |
| 358 | const std::string& interface, const std::string& method); |
Albert Zhang | b53049e | 2022-04-02 15:39:51 +0800 | [diff] [blame] | 359 | |
Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 360 | /********* End co-routine yielding alternatives ***************/ |
| 361 | |
Vernon Mauery | e7e8b81 | 2019-10-28 16:00:34 -0700 | [diff] [blame] | 362 | /** @brief Retrieve the value from map of variants, |
| 363 | * returning a default if the key does not exist or the |
| 364 | * type of the value does not match the expected type |
| 365 | * |
| 366 | * @tparam T - type of expected value to return |
| 367 | * @param[in] props - D-Bus propery map (Map of variants) |
| 368 | * @param[in] name - key name of property to fetch |
| 369 | * @param[in] defaultValue - default value to return on error |
| 370 | * @return - value from propery map at name, or defaultValue |
| 371 | */ |
| 372 | template <typename T> |
| 373 | T mappedVariant(const ipmi::PropertyMap& props, const std::string& name, |
| 374 | const T& defaultValue) |
| 375 | { |
| 376 | auto item = props.find(name); |
| 377 | if (item == props.end()) |
| 378 | { |
| 379 | return defaultValue; |
| 380 | } |
| 381 | const T* prop = std::get_if<T>(&item->second); |
| 382 | if (!prop) |
| 383 | { |
| 384 | return defaultValue; |
| 385 | } |
| 386 | return *prop; |
| 387 | } |
| 388 | |
James Feist | 1e12112 | 2018-07-31 11:44:09 -0700 | [diff] [blame] | 389 | /** @struct VariantToDoubleVisitor |
| 390 | * @brief Visitor to convert variants to doubles |
| 391 | * @details Performs a static cast on the underlying type |
| 392 | */ |
| 393 | struct VariantToDoubleVisitor |
| 394 | { |
| 395 | template <typename T> |
Patrick Williams | 69b4c28 | 2025-03-03 11:19:13 -0500 | [diff] [blame] | 396 | std::enable_if_t<std::is_arithmetic<T>::value, double> operator()( |
| 397 | const T& t) const |
James Feist | 1e12112 | 2018-07-31 11:44:09 -0700 | [diff] [blame] | 398 | { |
| 399 | return static_cast<double>(t); |
| 400 | } |
| 401 | |
| 402 | template <typename T> |
Patrick Williams | 69b4c28 | 2025-03-03 11:19:13 -0500 | [diff] [blame] | 403 | std::enable_if_t<!std::is_arithmetic<T>::value, double> operator()( |
| 404 | const T&) const |
James Feist | 1e12112 | 2018-07-31 11:44:09 -0700 | [diff] [blame] | 405 | { |
| 406 | throw std::invalid_argument("Cannot translate type to double"); |
| 407 | } |
| 408 | }; |
| 409 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 410 | namespace method_no_args |
| 411 | { |
| 412 | |
| 413 | /** @brief Calls the Dbus method which waits for response. |
| 414 | * @param[in] bus - DBUS Bus Object. |
| 415 | * @param[in] service - Dbus service name. |
| 416 | * @param[in] objPath - Dbus object path. |
| 417 | * @param[in] interface - Dbus interface. |
| 418 | * @param[in] method - Dbus method. |
| 419 | */ |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 420 | void callDbusMethod(sdbusplus::bus_t& bus, const std::string& service, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 421 | const std::string& objPath, const std::string& interface, |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 422 | const std::string& method); |
| 423 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 424 | } // namespace method_no_args |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 425 | |
George Liu | de1420d | 2025-03-03 15:14:25 +0800 | [diff] [blame] | 426 | template <typename... InputArgs> |
| 427 | boost::system::error_code callDbusMethod( |
| 428 | ipmi::Context::ptr ctx, const std::string& service, |
| 429 | const std::string& objPath, const std::string& interface, |
| 430 | const std::string& method, const InputArgs&... args) |
| 431 | { |
| 432 | boost::system::error_code ec; |
| 433 | ctx->bus->yield_method_call(ctx->yield, ec, service, objPath, interface, |
| 434 | method, args...); |
| 435 | |
| 436 | return ec; |
| 437 | } |
| 438 | |
| 439 | template <typename RetType, typename... InputArgs> |
| 440 | RetType callDbusMethod(ipmi::Context::ptr ctx, boost::system::error_code& ec, |
| 441 | const std::string& service, const std::string& objPath, |
| 442 | const std::string& interface, const std::string& method, |
| 443 | const InputArgs&... args) |
| 444 | { |
| 445 | auto rc = ctx->bus->yield_method_call<RetType>( |
| 446 | ctx->yield, ec, service, objPath, interface, method, args...); |
| 447 | |
| 448 | return rc; |
| 449 | } |
| 450 | |
Yong Li | 7dc4ac0 | 2019-08-23 17:44:32 +0800 | [diff] [blame] | 451 | /** @brief Perform the low-level i2c bus write-read. |
| 452 | * @param[in] i2cBus - i2c bus device node name, such as /dev/i2c-2. |
Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 453 | * @param[in] targetAddr - i2c device target address. |
Yong Li | 7dc4ac0 | 2019-08-23 17:44:32 +0800 | [diff] [blame] | 454 | * @param[in] writeData - The data written to i2c device. |
| 455 | * @param[out] readBuf - Data read from the i2c device. |
| 456 | */ |
Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 457 | ipmi::Cc i2cWriteRead(std::string i2cBus, const uint8_t targetAddr, |
Yong Li | 7dc4ac0 | 2019-08-23 17:44:32 +0800 | [diff] [blame] | 458 | std::vector<uint8_t> writeData, |
| 459 | std::vector<uint8_t>& readBuf); |
Prithvi Pai | c1e7b5c | 2025-09-01 13:13:15 +0530 | [diff] [blame] | 460 | |
| 461 | /** @brief Split a string into a vector of strings |
| 462 | * @param[in] srcStr - The string to split |
| 463 | * @param[in] delim - The delimiter to split the string on |
| 464 | * @return A vector of strings |
| 465 | */ |
| 466 | std::vector<std::string> split(const std::string& srcStr, char delim); |
| 467 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 468 | } // namespace ipmi |