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> |
George Liu | 13e3df6 | 2022-06-22 15:03:06 +0800 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
George Liu | 6f04b22 | 2022-06-22 14:23:19 +0800 | [diff] [blame] | 7 | #include <sdbusplus/exception.hpp> |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 8 | #include <sdbusplus/message.hpp> |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 9 | #include <xyz/openbmc_project/Common/error.hpp> |
| 10 | |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 11 | #include <string> |
| 12 | |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 13 | namespace util |
| 14 | { |
| 15 | namespace detail |
| 16 | { |
| 17 | namespace errors = sdbusplus::xyz::openbmc_project::Common::Error; |
| 18 | } // namespace detail |
| 19 | |
| 20 | /** @brief Alias for PropertiesChanged signal callbacks. */ |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 21 | template <typename... T> |
Patrick Williams | 35b4f33 | 2020-05-13 17:53:09 -0500 | [diff] [blame] | 22 | using Properties = std::map<std::string, std::variant<T...>>; |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 23 | |
| 24 | namespace sdbusplus |
| 25 | { |
| 26 | |
| 27 | /** @brief Get the bus connection. */ |
| 28 | static auto& getBus() __attribute__((pure)); |
| 29 | static auto& getBus() |
| 30 | { |
| 31 | static auto bus = ::sdbusplus::bus::new_default(); |
| 32 | return bus; |
| 33 | } |
| 34 | |
| 35 | /** @brief Invoke a method. */ |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 36 | template <typename... Args> |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 37 | static auto callMethod(::sdbusplus::bus_t& bus, const std::string& busName, |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 38 | const std::string& path, const std::string& interface, |
| 39 | const std::string& method, Args&&... args) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 40 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 41 | auto reqMsg = bus.new_method_call(busName.c_str(), path.c_str(), |
| 42 | interface.c_str(), method.c_str()); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 43 | reqMsg.append(std::forward<Args>(args)...); |
George Liu | 6f04b22 | 2022-06-22 14:23:19 +0800 | [diff] [blame] | 44 | try |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 45 | { |
George Liu | 6f04b22 | 2022-06-22 14:23:19 +0800 | [diff] [blame] | 46 | return bus.call(reqMsg); |
| 47 | } |
| 48 | catch (const std::exception& e) |
| 49 | { |
| 50 | lg2::error("Failed to invoke DBus method: {PATH}, {INTF}, {METHOD}", |
George Liu | 13e3df6 | 2022-06-22 15:03:06 +0800 | [diff] [blame] | 51 | "PATH", path, "INTF", interface, "METHOD", method); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 52 | phosphor::logging::elog<detail::errors::InternalFailure>(); |
| 53 | } |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 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 |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 69 | callMethodAndRead(::sdbusplus::bus_t& bus, const std::string& busName, |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 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 | { |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 73 | ::sdbusplus::message_t respMsg = callMethod<Args...>( |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 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> |
Patrick Williams | eab4f8c | 2024-08-16 15:20:10 -0400 | [diff] [blame^] | 82 | static auto callMethodAndRead( |
| 83 | const std::string& busName, const std::string& path, |
| 84 | const std::string& interface, const std::string& method, Args&&... args) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 85 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 86 | return callMethodAndRead<Ret>(getBus(), busName, path, interface, method, |
| 87 | std::forward<Args>(args)...); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 88 | } |
| 89 | |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 90 | /** @brief Get service from the mapper. */ |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 91 | static auto getService(::sdbusplus::bus_t& bus, const std::string& path, |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 92 | const std::string& interface) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 93 | { |
| 94 | using namespace std::literals::string_literals; |
| 95 | using GetObject = std::map<std::string, std::vector<std::string>>; |
| 96 | |
| 97 | auto mapperResp = callMethodAndRead<GetObject>( |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 98 | bus, "xyz.openbmc_project.ObjectMapper"s, |
| 99 | "/xyz/openbmc_project/object_mapper"s, |
| 100 | "xyz.openbmc_project.ObjectMapper"s, "GetObject"s, path, |
| 101 | GetObject::mapped_type{interface}); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 102 | |
| 103 | if (mapperResp.empty()) |
| 104 | { |
George Liu | 13e3df6 | 2022-06-22 15:03:06 +0800 | [diff] [blame] | 105 | lg2::error("Object not found. {PATH}, {INTF}", "PATH", path, "INTF", |
| 106 | interface); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 107 | phosphor::logging::elog<detail::errors::InternalFailure>(); |
| 108 | } |
| 109 | return mapperResp.begin()->first; |
| 110 | } |
| 111 | |
| 112 | /** @brief Get a property without mapper lookup. */ |
| 113 | template <typename Property> |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 114 | static auto getProperty(::sdbusplus::bus_t& bus, const std::string& busName, |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 115 | const std::string& path, const std::string& interface, |
| 116 | const std::string& property) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 117 | { |
| 118 | using namespace std::literals::string_literals; |
| 119 | |
Patrick Williams | eab4f8c | 2024-08-16 15:20:10 -0400 | [diff] [blame^] | 120 | auto msg = |
| 121 | callMethod(bus, busName, path, "org.freedesktop.DBus.Properties"s, |
| 122 | "Get"s, interface, property); |
Patrick Williams | 35b4f33 | 2020-05-13 17:53:09 -0500 | [diff] [blame] | 123 | ::std::variant<Property> value; |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 124 | msg.read(value); |
Patrick Williams | 34ef1e5 | 2020-05-13 11:07:43 -0500 | [diff] [blame] | 125 | return std::get<Property>(value); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** @brief Get a property without mapper lookup. */ |
| 129 | template <typename Property> |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 130 | static auto getProperty(const std::string& busName, const std::string& path, |
| 131 | const std::string& interface, |
| 132 | const std::string& property) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 133 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 134 | return getProperty<Property>(getBus(), busName, path, interface, property); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** @brief Get a property with mapper lookup. */ |
| 138 | template <typename Property> |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 139 | static auto getProperty(::sdbusplus::bus_t& bus, const std::string& path, |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 140 | const std::string& interface, |
| 141 | const std::string& property) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 142 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 143 | return getProperty<Property>(bus, getService(bus, path, interface), path, |
| 144 | interface, property); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** @brief Get a property with mapper lookup. */ |
| 148 | template <typename Property> |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 149 | static auto getProperty(const std::string& path, const std::string& interface, |
| 150 | const std::string& property) |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 151 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 152 | return getProperty<Property>(getBus(), path, interface, property); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } // namespace sdbusplus |
| 156 | } // namespace util |