From f2238ae65801eaf0795f483afd8e1fc5eb6f85a4 Mon Sep 17 00:00:00 2001 From: Brad Bishop Date: Sun, 30 Jul 2017 13:32:23 -0400 Subject: Allow existing bus connections Overload the wrapper methods in sdbusplus.hpp for clients that already have a bus connection. Change-Id: I662385e6afe52179293ccbd41212d208e34bfbc5 Signed-off-by: Brad Bishop --- sdbusplus.hpp | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 129 insertions(+), 6 deletions(-) diff --git a/sdbusplus.hpp b/sdbusplus.hpp index 0b842e6..1efe54d 100644 --- a/sdbusplus.hpp +++ b/sdbusplus.hpp @@ -40,19 +40,20 @@ class SDBusPlus /** @brief Invoke a method. */ template static auto callMethod( + sdbusplus::bus::bus& bus, const std::string& busName, const std::string& path, const std::string& interface, const std::string& method, Args&& ... args) { - auto reqMsg = getBus().new_method_call( + auto reqMsg = bus.new_method_call( busName.c_str(), path.c_str(), interface.c_str(), method.c_str()); reqMsg.append(std::forward(args)...); - auto respMsg = getBus().call(reqMsg); + auto respMsg = bus.call(reqMsg); if (respMsg.is_method_error()) { @@ -68,9 +69,28 @@ class SDBusPlus return respMsg; } + /** @brief Invoke a method. */ + template + static auto callMethod( + const std::string& busName, + const std::string& path, + const std::string& interface, + const std::string& method, + Args&& ... args) + { + return callMethod( + getBus(), + busName, + path, + interface, + method, + std::forward(args)...); + } + /** @brief Invoke a method and read the response. */ template static auto callMethodAndRead( + sdbusplus::bus::bus& bus, const std::string& busName, const std::string& path, const std::string& interface, @@ -79,6 +99,7 @@ class SDBusPlus { sdbusplus::message::message respMsg = callMethod( + bus, busName, path, interface, @@ -89,8 +110,28 @@ class SDBusPlus return resp; } + /** @brief Invoke a method and read the response. */ + template + static auto callMethodAndRead( + const std::string& busName, + const std::string& path, + const std::string& interface, + const std::string& method, + Args&& ... args) + { + return callMethodAndRead( + getBus(), + busName, + path, + interface, + method, + std::forward(args)...); + } + + /** @brief Get service from the mapper. */ static auto getService( + sdbusplus::bus::bus& bus, const std::string& path, const std::string& interface) { @@ -98,6 +139,7 @@ class SDBusPlus using GetObject = std::map>; auto mapperResp = callMethodAndRead( + bus, "xyz.openbmc_project.ObjectMapper"s, "/xyz/openbmc_project/object_mapper"s, "xyz.openbmc_project.ObjectMapper"s, @@ -117,9 +159,21 @@ class SDBusPlus return mapperResp.begin()->first; } + /** @brief Get service from the mapper. */ + static auto getService( + const std::string& path, + const std::string& interface) + { + return getService( + getBus(), + path, + interface); + } + /** @brief Get a property with mapper lookup. */ template static auto getProperty( + sdbusplus::bus::bus& bus, const std::string& path, const std::string& interface, const std::string& property) @@ -127,7 +181,8 @@ class SDBusPlus using namespace std::literals::string_literals; auto msg = callMethod( - getService(path, interface), + bus, + getService(bus, path, interface), path, "org.freedesktop.DBus.Properties"s, "Get"s, @@ -138,9 +193,24 @@ class SDBusPlus return value.template get(); } + /** @brief Get a property with mapper lookup. */ + template + static auto getProperty( + const std::string& path, + const std::string& interface, + const std::string& property) + { + return getProperty( + getBus(), + path, + interface, + property); + } + /** @brief Set a property with mapper lookup. */ template static void setProperty( + sdbusplus::bus::bus& bus, const std::string& path, const std::string& interface, const std::string& property, @@ -152,7 +222,8 @@ class SDBusPlus std::forward(value)); callMethod( - getService(path, interface), + bus, + getService(bus, path, interface), path, "org.freedesktop.DBus.Properties"s, "Set"s, @@ -161,16 +232,50 @@ class SDBusPlus varValue); } + /** @brief Set a property with mapper lookup. */ + template + static void setProperty( + const std::string& path, + const std::string& interface, + const std::string& property, + Property&& value) + { + return setProperty( + getBus(), + path, + interface, + property, + std::forward(value)); + } + /** @brief Invoke method with mapper lookup. */ template static auto lookupAndCallMethod( + sdbusplus::bus::bus& bus, const std::string& path, const std::string& interface, const std::string& method, Args&& ... args) { return callMethod( - getService(path, interface), + bus, + getService(bus, path, interface), + path, + interface, + method, + std::forward(args)...); + } + + /** @brief Invoke method with mapper lookup. */ + template + static auto lookupAndCallMethod( + const std::string& path, + const std::string& interface, + const std::string& method, + Args&& ... args) + { + return lookupAndCallMethod( + getBus(), path, interface, method, @@ -180,13 +285,31 @@ class SDBusPlus /** @brief Invoke method and read with mapper lookup. */ template static auto lookupCallMethodAndRead( + sdbusplus::bus::bus& bus, const std::string& path, const std::string& interface, const std::string& method, Args&& ... args) { return callMethodAndRead( - getService(path, interface), + bus, + getService(bus, path, interface), + path, + interface, + method, + std::forward(args)...); + } + + /** @brief Invoke method and read with mapper lookup. */ + template + static auto lookupCallMethodAndRead( + const std::string& path, + const std::string& interface, + const std::string& method, + Args&& ... args) + { + return lookupCallMethodAndRead( + getBus(), path, interface, method, -- cgit v1.2.1