#pragma once #include #include #include #include #include #include #include #include namespace phosphor { namespace fan { namespace util { namespace detail { namespace errors = sdbusplus::xyz::openbmc_project::Common::Error; } // namespace detail /** * @class DBusError * * The base class for the exceptions thrown on fails in the various * SDBusPlus calls. Used so that a single catch statement can catch * any type of these exceptions. * * None of these exceptions will log anything when they are created, * it is up to the handler to do that if desired. */ class DBusError : public std::runtime_error { public: explicit DBusError(const std::string& msg) : std::runtime_error(msg) {} }; /** * @class DBusMethodError * * Thrown on a DBus Method call failure */ class DBusMethodError : public DBusError { public: DBusMethodError(const std::string& busName, const std::string& path, const std::string& interface, const std::string& method) : DBusError(fmt::format("DBus method failed: {} {} {} {}", busName, path, interface, method)), busName(busName), path(path), interface(interface), method(method) {} const std::string busName; const std::string path; const std::string interface; const std::string method; }; /** * @class DBusServiceError * * Thrown when a service lookup fails. Usually this points to * the object path not being present in D-Bus. */ class DBusServiceError : public DBusError { public: DBusServiceError(const std::string& path, const std::string& interface) : DBusError( fmt::format("DBus service lookup failed: {} {}", path, interface)), path(path), interface(interface) {} const std::string path; const std::string interface; }; /** * @class DBusPropertyError * * Thrown when a set/get property fails. */ class DBusPropertyError : public DBusError { public: DBusPropertyError(const std::string& msg, const std::string& busName, const std::string& path, const std::string& interface, const std::string& property) : DBusError(msg + fmt::format(": {} {} {} {}", busName, path, interface, property)), busName(busName), path(path), interface(interface), property(property) {} const std::string busName; const std::string path; const std::string interface; const std::string property; }; /** @brief Alias for PropertiesChanged signal callbacks. */ template using Properties = std::map>; /** @class SDBusPlus * @brief DBus access delegate implementation for sdbusplus. */ class SDBusPlus { public: /** @brief Get the bus connection. */ static auto& getBus() __attribute__((pure)) { static auto bus = sdbusplus::bus::new_default(); return bus; } /** @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 = bus.new_method_call(busName.c_str(), path.c_str(), interface.c_str(), method.c_str()); reqMsg.append(std::forward(args)...); try { auto respMsg = bus.call(reqMsg); if (respMsg.is_method_error()) { throw DBusMethodError{busName, path, interface, method}; } return respMsg; } catch (const sdbusplus::exception::exception&) { throw DBusMethodError{busName, path, interface, method}; } } /** @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, const std::string& method, Args&&... args) { sdbusplus::message::message respMsg = callMethod( bus, busName, path, interface, method, std::forward(args)...); Ret resp; respMsg.read(resp); 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 subtree from the mapper without checking response. */ static auto getSubTreeRaw(sdbusplus::bus::bus& bus, const std::string& path, const std::string& interface, int32_t depth) { using namespace std::literals::string_literals; using Path = std::string; using Intf = std::string; using Serv = std::string; using Intfs = std::vector; using Objects = std::map>; Intfs intfs = {interface}; return callMethodAndRead(bus, "xyz.openbmc_project.ObjectMapper"s, "/xyz/openbmc_project/object_mapper"s, "xyz.openbmc_project.ObjectMapper"s, "GetSubTree"s, path, depth, intfs); } /** @brief Get subtree from the mapper without checking response, * (multiple interfaces version). */ static auto getSubTreeRaw(sdbusplus::bus::bus& bus, const std::string& path, const std::vector& intfs, int32_t depth) { using namespace std::literals::string_literals; using Path = std::string; using Intf = std::string; using Serv = std::string; using Intfs = std::vector; using Objects = std::map>; return callMethodAndRead(bus, "xyz.openbmc_project.ObjectMapper"s, "/xyz/openbmc_project/object_mapper"s, "xyz.openbmc_project.ObjectMapper"s, "GetSubTree"s, path, depth, intfs); } /** @brief Get subtree from the mapper. */ static auto getSubTree(sdbusplus::bus::bus& bus, const std::string& path, const std::string& interface, int32_t depth) { auto mapperResp = getSubTreeRaw(bus, path, interface, depth); if (mapperResp.empty()) { phosphor::logging::log( "Empty response from mapper GetSubTree", phosphor::logging::entry("SUBTREE=%s", path.c_str()), phosphor::logging::entry("INTERFACE=%s", interface.c_str()), phosphor::logging::entry("DEPTH=%u", depth)); phosphor::logging::elog(); } return mapperResp; } /** @brief Get subtree paths from the mapper without checking response. */ static auto getSubTreePathsRaw(sdbusplus::bus::bus& bus, const std::string& path, const std::string& interface, int32_t depth) { using namespace std::literals::string_literals; using Path = std::string; using Intf = std::string; using Intfs = std::vector; using ObjectPaths = std::vector; Intfs intfs = {interface}; return callMethodAndRead( bus, "xyz.openbmc_project.ObjectMapper"s, "/xyz/openbmc_project/object_mapper"s, "xyz.openbmc_project.ObjectMapper"s, "GetSubTreePaths"s, path, depth, intfs); } /** @brief Get subtree paths from the mapper. */ static auto getSubTreePaths(sdbusplus::bus::bus& bus, const std::string& path, const std::string& interface, int32_t depth) { auto mapperResp = getSubTreePathsRaw(bus, path, interface, depth); if (mapperResp.empty()) { phosphor::logging::log( "Empty response from mapper GetSubTreePaths", phosphor::logging::entry("SUBTREE=%s", path.c_str()), phosphor::logging::entry("INTERFACE=%s", interface.c_str()), phosphor::logging::entry("DEPTH=%u", depth)); phosphor::logging::elog(); } return mapperResp; } /** @brief Get service from the mapper without checking response. */ static auto getServiceRaw(sdbusplus::bus::bus& bus, const std::string& path, const std::string& interface) { using namespace std::literals::string_literals; using GetObject = std::map>; return callMethodAndRead( bus, "xyz.openbmc_project.ObjectMapper"s, "/xyz/openbmc_project/object_mapper"s, "xyz.openbmc_project.ObjectMapper"s, "GetObject"s, path, GetObject::mapped_type{interface}); } /** @brief Get service from the mapper. */ static auto getService(sdbusplus::bus::bus& bus, const std::string& path, const std::string& interface) { try { auto mapperResp = getServiceRaw(bus, path, interface); if (mapperResp.empty()) { // Should never happen. A missing object would fail // in callMethodAndRead() phosphor::logging::log( "Empty mapper response on service lookup"); throw DBusServiceError{path, interface}; } return mapperResp.begin()->first; } catch (const DBusMethodError& e) { throw DBusServiceError{path, interface}; } } /** @brief Get service from the mapper. */ static auto getService(const std::string& path, const std::string& interface) { return getService(getBus(), path, interface); } /** @brief Get managed objects. */ template static auto getManagedObjects(sdbusplus::bus::bus& bus, const std::string& service, const std::string& path) { using namespace std::literals::string_literals; using Path = sdbusplus::message::object_path; using Intf = std::string; using Prop = std::string; using GetManagedObjects = std::map>>; return callMethodAndRead( bus, service, path, "org.freedesktop.DBus.ObjectManager"s, "GetManagedObjects"s); } /** @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) { using namespace std::literals::string_literals; auto service = getService(bus, path, interface); auto msg = callMethod(bus, service, path, "org.freedesktop.DBus.Properties"s, "Get"s, interface, property); if (msg.is_method_error()) { throw DBusPropertyError{"DBus get property failed", service, path, interface, property}; } std::variant value; msg.read(value); return std::get(value); } /** @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 Get a property variant with mapper lookup. */ template static auto getPropertyVariant(sdbusplus::bus::bus& bus, const std::string& path, const std::string& interface, const std::string& property) { using namespace std::literals::string_literals; auto service = getService(bus, path, interface); auto msg = callMethod(bus, service, path, "org.freedesktop.DBus.Properties"s, "Get"s, interface, property); if (msg.is_method_error()) { throw DBusPropertyError{"DBus get property variant failed", service, path, interface, property}; } Variant value; msg.read(value); return value; } /** @brief Get a property variant with mapper lookup. */ template static auto getPropertyVariant(const std::string& path, const std::string& interface, const std::string& property) { return getPropertyVariant(getBus(), path, interface, property); } /** @brief Get a property without mapper lookup. */ template static auto getProperty(sdbusplus::bus::bus& bus, const std::string& service, const std::string& path, const std::string& interface, const std::string& property) { using namespace std::literals::string_literals; auto msg = callMethodAndReturn(bus, service, path, "org.freedesktop.DBus.Properties"s, "Get"s, interface, property); if (msg.is_method_error()) { throw DBusPropertyError{"DBus get property failed", service, path, interface, property}; } std::variant value; msg.read(value); return std::get(value); } /** @brief Get a property without mapper lookup. */ template static auto getProperty(const std::string& service, const std::string& path, const std::string& interface, const std::string& property) { return getProperty(getBus(), service, path, interface, property); } /** @brief Get a property variant without mapper lookup. */ template static auto getPropertyVariant(sdbusplus::bus::bus& bus, const std::string& service, const std::string& path, const std::string& interface, const std::string& property) { using namespace std::literals::string_literals; auto msg = callMethodAndReturn(bus, service, path, "org.freedesktop.DBus.Properties"s, "Get"s, interface, property); if (msg.is_method_error()) { throw DBusPropertyError{"DBus get property variant failed", service, path, interface, property}; } Variant value; msg.read(value); return value; } /** @brief Get a property variant without mapper lookup. */ template static auto getPropertyVariant(const std::string& service, const std::string& path, const std::string& interface, const std::string& property) { return getPropertyVariant(getBus(), service, 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, Property&& value) { using namespace std::literals::string_literals; std::variant varValue(std::forward(value)); auto service = getService(bus, path, interface); auto msg = callMethodAndReturn(bus, service, path, "org.freedesktop.DBus.Properties"s, "Set"s, interface, property, varValue); if (msg.is_method_error()) { throw DBusPropertyError{"DBus set property failed", service, path, interface, property}; } } /** @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 Set a property without mapper lookup. */ template static void setProperty(sdbusplus::bus::bus& bus, const std::string& service, const std::string& path, const std::string& interface, const std::string& property, Property&& value) { using namespace std::literals::string_literals; std::variant varValue(std::forward(value)); auto msg = callMethodAndReturn(bus, service, path, "org.freedesktop.DBus.Properties"s, "Set"s, interface, property, varValue); if (msg.is_method_error()) { throw DBusPropertyError{"DBus set property failed", service, path, interface, property}; } } /** @brief Set a property without mapper lookup. */ template static void setProperty(const std::string& service, const std::string& path, const std::string& interface, const std::string& property, Property&& value) { return setProperty(getBus(), service, 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(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, std::forward(args)...); } /** @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(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, std::forward(args)...); } /** @brief Invoke a method and return without checking for error. */ template static auto callMethodAndReturn(sdbusplus::bus::bus& bus, const std::string& busName, const std::string& path, const std::string& interface, const std::string& method, Args&&... args) { 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 = bus.call(reqMsg); return respMsg; } }; } // namespace util } // namespace fan } // namespace phosphor