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