Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Adriana Kobylak | 0049c98 | 2018-06-06 15:33:22 -0500 | [diff] [blame^] | 3 | #include <phosphor-logging/log.hpp> |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 4 | #include <sdbusplus/bus.hpp> |
Adriana Kobylak | 0049c98 | 2018-06-06 15:33:22 -0500 | [diff] [blame^] | 5 | #include <sdbusplus/exception.hpp> |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 6 | #include <sdbusplus/message.hpp> |
| 7 | #include <sdbusplus/bus/match.hpp> |
Matt Spinler | df1b7cf | 2017-10-31 14:17:23 -0500 | [diff] [blame] | 8 | #include "data_types.hpp" |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 9 | |
Brad Bishop | d9c1fab | 2017-06-04 22:32:12 -0400 | [diff] [blame] | 10 | struct Loop; |
| 11 | |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace dbus |
| 15 | { |
| 16 | namespace monitoring |
| 17 | { |
| 18 | |
Adriana Kobylak | 0049c98 | 2018-06-06 15:33:22 -0500 | [diff] [blame^] | 19 | using namespace phosphor::logging; |
| 20 | using sdbusplus::exception::SdBusError; |
| 21 | |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 22 | /** @class SDBusPlus |
| 23 | * @brief DBus access delegate implementation for sdbusplus. |
| 24 | */ |
| 25 | class SDBusPlus |
| 26 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 27 | private: |
| 28 | static auto& getWatches() |
| 29 | { |
| 30 | static std::vector<sdbusplus::bus::match::match> watches; |
| 31 | return watches; |
| 32 | } |
| 33 | |
| 34 | public: |
| 35 | static auto& getBus() |
| 36 | { |
| 37 | static auto bus = sdbusplus::bus::new_default(); |
| 38 | return bus; |
| 39 | } |
| 40 | |
| 41 | /** @brief Invoke a method; ignore reply. */ |
| 42 | template <typename... Args> |
| 43 | static void callMethodNoReply(const std::string& busName, |
| 44 | const std::string& path, |
| 45 | const std::string& interface, |
| 46 | const std::string& method, Args&&... args) |
| 47 | { |
| 48 | auto reqMsg = getBus().new_method_call( |
| 49 | busName.c_str(), path.c_str(), interface.c_str(), method.c_str()); |
| 50 | reqMsg.append(std::forward<Args>(args)...); |
| 51 | getBus().call_noreply(reqMsg); |
| 52 | |
| 53 | // TODO: openbmc/openbmc#1719 |
| 54 | // invoke these methods async, with a callback |
| 55 | // handler that checks for errors and logs. |
| 56 | } |
| 57 | |
| 58 | /** @brief Invoke a method. */ |
| 59 | template <typename... Args> |
| 60 | static auto callMethod(const std::string& busName, const std::string& path, |
| 61 | const std::string& interface, |
| 62 | const std::string& method, Args&&... args) |
| 63 | { |
| 64 | auto reqMsg = getBus().new_method_call( |
| 65 | busName.c_str(), path.c_str(), interface.c_str(), method.c_str()); |
| 66 | reqMsg.append(std::forward<Args>(args)...); |
| 67 | return getBus().call(reqMsg); |
| 68 | } |
| 69 | |
| 70 | /** @brief Invoke a method and read the response. */ |
| 71 | template <typename Ret, typename... Args> |
| 72 | static auto callMethodAndRead(const std::string& busName, |
| 73 | const std::string& path, |
| 74 | const std::string& interface, |
| 75 | const std::string& method, Args&&... args) |
| 76 | { |
| 77 | Ret resp; |
| 78 | sdbusplus::message::message respMsg = callMethod<Args...>( |
| 79 | busName, path, interface, method, std::forward<Args>(args)...); |
Adriana Kobylak | 0049c98 | 2018-06-06 15:33:22 -0500 | [diff] [blame^] | 80 | try |
| 81 | { |
| 82 | respMsg.read(resp); |
| 83 | } |
| 84 | catch (const SdBusError& e) |
| 85 | { |
| 86 | log<level::ERR>("Failed to parse method response", |
| 87 | entry("ERROR=%s", e.what()), |
| 88 | entry("REPLY_SIG=%s", respMsg.get_signature())); |
| 89 | } |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 90 | return resp; |
| 91 | } |
| 92 | |
| 93 | /** @brief Register a DBus signal callback. */ |
| 94 | static auto |
| 95 | addMatch(const std::string& match, |
| 96 | const sdbusplus::bus::match::match::callback_t& callback) |
| 97 | { |
| 98 | getWatches().emplace_back(getBus(), match, callback); |
| 99 | } |
| 100 | |
| 101 | /** @brief Look up the bus name for a path and interface */ |
| 102 | static auto getBusName(const std::string& path, |
| 103 | const std::string& interface) |
| 104 | { |
| 105 | std::vector<std::string> interfaces{interface}; |
| 106 | |
| 107 | auto object = callMethodAndRead<GetObject>( |
| 108 | MAPPER_BUSNAME, MAPPER_PATH, MAPPER_INTERFACE, "GetObject", path, |
| 109 | interfaces); |
| 110 | |
| 111 | std::string name; |
| 112 | if (!object.empty()) |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 113 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 114 | name = object.begin()->first; |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 115 | } |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 116 | return name; |
| 117 | } |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 118 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 119 | friend Loop; |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace monitoring |
| 123 | } // namespace dbus |
| 124 | } // namespace phosphor |