Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 3 | #include <phosphor-logging/elog-errors.hpp> |
| 4 | #include <phosphor-logging/elog.hpp> |
| 5 | #include <phosphor-logging/log.hpp> |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
| 7 | #include <sdbusplus/message.hpp> |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 8 | #include <xyz/openbmc_project/Common/error.hpp> |
| 9 | |
| 10 | namespace util |
| 11 | { |
| 12 | namespace detail |
| 13 | { |
| 14 | namespace errors = sdbusplus::xyz::openbmc_project::Common::Error; |
| 15 | } // namespace detail |
| 16 | |
| 17 | /** @brief Alias for PropertiesChanged signal callbacks. */ |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 18 | template <typename... T> |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 19 | using Properties = std::map<std::string, sdbusplus::message::variant<T...>>; |
| 20 | |
| 21 | namespace sdbusplus |
| 22 | { |
| 23 | |
| 24 | /** @brief Get the bus connection. */ |
| 25 | static auto& getBus() __attribute__((pure)); |
| 26 | static auto& getBus() |
| 27 | { |
| 28 | static auto bus = ::sdbusplus::bus::new_default(); |
| 29 | return bus; |
| 30 | } |
| 31 | |
| 32 | /** @brief Invoke a method. */ |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 33 | template <typename... Args> |
| 34 | static auto callMethod(::sdbusplus::bus::bus& bus, const std::string& busName, |
| 35 | const std::string& path, const std::string& interface, |
| 36 | const std::string& method, Args&&... args) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 37 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 38 | auto reqMsg = bus.new_method_call(busName.c_str(), path.c_str(), |
| 39 | interface.c_str(), method.c_str()); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 40 | reqMsg.append(std::forward<Args>(args)...); |
| 41 | auto respMsg = bus.call(reqMsg); |
| 42 | |
| 43 | if (respMsg.is_method_error()) |
| 44 | { |
| 45 | phosphor::logging::log<phosphor::logging::level::INFO>( |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 46 | "Failed to invoke DBus method.", |
| 47 | phosphor::logging::entry("PATH=%s", path.c_str()), |
| 48 | phosphor::logging::entry("INTERFACE=%s", interface.c_str()), |
| 49 | phosphor::logging::entry("METHOD=%s", method.c_str())); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 50 | phosphor::logging::elog<detail::errors::InternalFailure>(); |
| 51 | } |
| 52 | |
| 53 | return respMsg; |
| 54 | } |
| 55 | |
| 56 | /** @brief Invoke a method. */ |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 57 | template <typename... Args> |
| 58 | static auto callMethod(const std::string& busName, const std::string& path, |
| 59 | const std::string& interface, const std::string& method, |
| 60 | Args&&... args) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 61 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 62 | return callMethod(getBus(), busName, path, interface, method, |
| 63 | std::forward<Args>(args)...); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** @brief Invoke a method and read the response. */ |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 67 | template <typename Ret, typename... Args> |
| 68 | static auto |
| 69 | callMethodAndRead(::sdbusplus::bus::bus& bus, const std::string& busName, |
| 70 | const std::string& path, const std::string& interface, |
| 71 | const std::string& method, Args&&... args) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 72 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 73 | ::sdbusplus::message::message respMsg = callMethod<Args...>( |
| 74 | bus, busName, path, interface, method, std::forward<Args>(args)...); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 75 | Ret resp; |
| 76 | respMsg.read(resp); |
| 77 | return resp; |
| 78 | } |
| 79 | |
| 80 | /** @brief Invoke a method and read the response. */ |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 81 | template <typename Ret, typename... Args> |
| 82 | static auto callMethodAndRead(const std::string& busName, |
| 83 | const std::string& path, |
| 84 | const std::string& interface, |
| 85 | const std::string& method, Args&&... args) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 86 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 87 | return callMethodAndRead<Ret>(getBus(), busName, path, interface, method, |
| 88 | std::forward<Args>(args)...); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 91 | /** @brief Get service from the mapper. */ |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 92 | static auto getService(::sdbusplus::bus::bus& bus, const std::string& path, |
| 93 | const std::string& interface) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 94 | { |
| 95 | using namespace std::literals::string_literals; |
| 96 | using GetObject = std::map<std::string, std::vector<std::string>>; |
| 97 | |
| 98 | auto mapperResp = callMethodAndRead<GetObject>( |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 99 | bus, "xyz.openbmc_project.ObjectMapper"s, |
| 100 | "/xyz/openbmc_project/object_mapper"s, |
| 101 | "xyz.openbmc_project.ObjectMapper"s, "GetObject"s, path, |
| 102 | GetObject::mapped_type{interface}); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 103 | |
| 104 | if (mapperResp.empty()) |
| 105 | { |
| 106 | phosphor::logging::log<phosphor::logging::level::INFO>( |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 107 | "Object not found.", |
| 108 | phosphor::logging::entry("PATH=%s", path.c_str()), |
| 109 | phosphor::logging::entry("INTERFACE=%s", interface.c_str())); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 110 | phosphor::logging::elog<detail::errors::InternalFailure>(); |
| 111 | } |
| 112 | return mapperResp.begin()->first; |
| 113 | } |
| 114 | |
| 115 | /** @brief Get a property without mapper lookup. */ |
| 116 | template <typename Property> |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 117 | static auto getProperty(::sdbusplus::bus::bus& bus, const std::string& busName, |
| 118 | const std::string& path, const std::string& interface, |
| 119 | const std::string& property) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 120 | { |
| 121 | using namespace std::literals::string_literals; |
| 122 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 123 | auto msg = |
| 124 | callMethod(bus, busName, path, "org.freedesktop.DBus.Properties"s, |
| 125 | "Get"s, interface, property); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 126 | ::sdbusplus::message::variant<Property> value; |
| 127 | msg.read(value); |
William A. Kennington III | efcd653 | 2018-11-12 15:54:32 -0800 | [diff] [blame] | 128 | return ::sdbusplus::message::variant_ns::get<Property>(value); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** @brief Get a property without mapper lookup. */ |
| 132 | template <typename Property> |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 133 | static auto getProperty(const std::string& busName, const std::string& path, |
| 134 | const std::string& interface, |
| 135 | const std::string& property) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 136 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 137 | return getProperty<Property>(getBus(), busName, path, interface, property); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** @brief Get a property with mapper lookup. */ |
| 141 | template <typename Property> |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 142 | static auto getProperty(::sdbusplus::bus::bus& bus, const std::string& path, |
| 143 | const std::string& interface, |
| 144 | const std::string& property) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 145 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 146 | return getProperty<Property>(bus, getService(bus, path, interface), path, |
| 147 | interface, property); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** @brief Get a property with mapper lookup. */ |
| 151 | template <typename Property> |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 152 | static auto getProperty(const std::string& path, const std::string& interface, |
| 153 | const std::string& property) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 154 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 155 | return getProperty<Property>(getBus(), path, interface, property); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | } // namespace sdbusplus |
| 159 | } // namespace util |