Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 3 | #include "data_types.hpp" |
| 4 | |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 5 | #include <sdbusplus/bus.hpp> |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 6 | #include <sdbusplus/bus/match.hpp> |
Adriana Kobylak | 0049c98 | 2018-06-06 15:33:22 -0500 | [diff] [blame] | 7 | #include <sdbusplus/exception.hpp> |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 8 | #include <sdbusplus/message.hpp> |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 9 | |
Andrew Geissler | ae4c95c | 2020-05-16 13:58:53 -0500 | [diff] [blame] | 10 | #include <string> |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 11 | |
Brad Bishop | d9c1fab | 2017-06-04 22:32:12 -0400 | [diff] [blame] | 12 | struct Loop; |
| 13 | |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 14 | namespace phosphor |
| 15 | { |
| 16 | namespace dbus |
| 17 | { |
| 18 | namespace monitoring |
| 19 | { |
| 20 | |
| 21 | /** @class SDBusPlus |
| 22 | * @brief DBus access delegate implementation for sdbusplus. |
| 23 | */ |
| 24 | class SDBusPlus |
| 25 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 26 | private: |
| 27 | static auto& getWatches() |
| 28 | { |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 29 | static std::vector<sdbusplus::bus::match_t> watches; |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 30 | return watches; |
| 31 | } |
| 32 | |
| 33 | public: |
| 34 | static auto& getBus() |
| 35 | { |
| 36 | static auto bus = sdbusplus::bus::new_default(); |
| 37 | return bus; |
| 38 | } |
| 39 | |
| 40 | /** @brief Invoke a method; ignore reply. */ |
| 41 | template <typename... Args> |
Patrick Williams | eab4f8c | 2024-08-16 15:20:10 -0400 | [diff] [blame^] | 42 | static void callMethodNoReply( |
| 43 | const std::string& busName, const std::string& path, |
| 44 | const std::string& interface, const std::string& method, Args&&... args) |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 45 | { |
| 46 | auto reqMsg = getBus().new_method_call( |
| 47 | busName.c_str(), path.c_str(), interface.c_str(), method.c_str()); |
| 48 | reqMsg.append(std::forward<Args>(args)...); |
| 49 | getBus().call_noreply(reqMsg); |
| 50 | |
| 51 | // TODO: openbmc/openbmc#1719 |
| 52 | // invoke these methods async, with a callback |
| 53 | // handler that checks for errors and logs. |
| 54 | } |
| 55 | |
| 56 | /** @brief Invoke a method. */ |
| 57 | template <typename... Args> |
| 58 | static auto callMethod(const std::string& busName, const std::string& path, |
| 59 | const std::string& interface, |
| 60 | const std::string& method, Args&&... args) |
| 61 | { |
| 62 | auto reqMsg = getBus().new_method_call( |
| 63 | busName.c_str(), path.c_str(), interface.c_str(), method.c_str()); |
| 64 | reqMsg.append(std::forward<Args>(args)...); |
| 65 | return getBus().call(reqMsg); |
| 66 | } |
| 67 | |
| 68 | /** @brief Invoke a method and read the response. */ |
| 69 | template <typename Ret, typename... Args> |
Patrick Williams | eab4f8c | 2024-08-16 15:20:10 -0400 | [diff] [blame^] | 70 | static auto callMethodAndRead( |
| 71 | const std::string& busName, const std::string& path, |
| 72 | const std::string& interface, const std::string& method, Args&&... args) |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 73 | { |
| 74 | Ret resp; |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 75 | sdbusplus::message_t respMsg = callMethod<Args...>( |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 76 | busName, path, interface, method, std::forward<Args>(args)...); |
Adriana Kobylak | 0049c98 | 2018-06-06 15:33:22 -0500 | [diff] [blame] | 77 | try |
| 78 | { |
| 79 | respMsg.read(resp); |
| 80 | } |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 81 | catch (const sdbusplus::exception_t& e) |
Adriana Kobylak | 0049c98 | 2018-06-06 15:33:22 -0500 | [diff] [blame] | 82 | { |
Matt Spinler | 195550c | 2018-06-13 13:51:40 -0500 | [diff] [blame] | 83 | // Empty responses are expected sometimes, and the calling |
| 84 | // code is set up to handle it. |
Adriana Kobylak | 0049c98 | 2018-06-06 15:33:22 -0500 | [diff] [blame] | 85 | } |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 86 | return resp; |
| 87 | } |
| 88 | |
| 89 | /** @brief Register a DBus signal callback. */ |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 90 | static auto addMatch(const std::string& match, |
William A. Kennington III | bf97583 | 2022-11-24 05:38:04 -0800 | [diff] [blame] | 91 | sdbusplus::bus::match_t::callback_t&& callback) |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 92 | { |
William A. Kennington III | bf97583 | 2022-11-24 05:38:04 -0800 | [diff] [blame] | 93 | getWatches().emplace_back(getBus(), match, std::move(callback)); |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** @brief Look up the bus name for a path and interface */ |
| 97 | static auto getBusName(const std::string& path, |
| 98 | const std::string& interface) |
| 99 | { |
| 100 | std::vector<std::string> interfaces{interface}; |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 101 | std::string name; |
Adriana Kobylak | 2ded5e1 | 2018-07-10 12:55:51 -0500 | [diff] [blame] | 102 | |
| 103 | try |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 104 | { |
Adriana Kobylak | 2ded5e1 | 2018-07-10 12:55:51 -0500 | [diff] [blame] | 105 | auto object = callMethodAndRead<GetObject>( |
| 106 | MAPPER_BUSNAME, MAPPER_PATH, MAPPER_INTERFACE, "GetObject", |
| 107 | path, interfaces); |
| 108 | |
| 109 | if (!object.empty()) |
| 110 | { |
| 111 | name = object.begin()->first; |
| 112 | } |
| 113 | } |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 114 | catch (const sdbusplus::exception_t& e) |
Adriana Kobylak | 2ded5e1 | 2018-07-10 12:55:51 -0500 | [diff] [blame] | 115 | { |
| 116 | // Empty responses are expected sometimes, and the calling |
| 117 | // code is set up to handle it. |
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 | return name; |
| 120 | } |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 121 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 122 | friend Loop; |
Brad Bishop | fac1b10 | 2017-05-15 22:29:20 -0400 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | } // namespace monitoring |
| 126 | } // namespace dbus |
| 127 | } // namespace phosphor |