blob: 8afb8b51eab91875da3d4a69a88f7d3ac2b1d3e2 [file] [log] [blame]
Brad Bishopfac1b102017-05-15 22:29:20 -04001#pragma once
2
3#include <sdbusplus/bus.hpp>
4#include <sdbusplus/message.hpp>
5#include <sdbusplus/bus/match.hpp>
6
7namespace phosphor
8{
9namespace dbus
10{
11namespace monitoring
12{
13
14/** @class SDBusPlus
15 * @brief DBus access delegate implementation for sdbusplus.
16 */
17class SDBusPlus
18{
19 private:
20 static auto& getBus()
21 {
22 static auto bus = sdbusplus::bus::new_default();
23 return bus;
24 }
25
26 static auto& getWatches()
27 {
28 static std::vector<sdbusplus::bus::match::match> watches;
29 return watches;
30 }
31
32 public:
Brad Bishop0df00be2017-05-25 23:38:37 -040033 /** @brief Invoke a method; ignore reply. */
34 template <typename ...Args>
35 static void callMethodNoReply(
36 const std::string& busName,
37 const std::string& path,
38 const std::string& interface,
39 const std::string& method,
40 Args&& ... args)
41 {
42 auto reqMsg = getBus().new_method_call(
43 busName.c_str(),
44 path.c_str(),
45 interface.c_str(),
46 method.c_str());
47 reqMsg.append(std::forward<Args>(args)...);
48 getBus().call_noreply(reqMsg);
49
50 // TODO: openbmc/openbmc#1719
51 // invoke these methods async, with a callback
52 // handler that checks for errors and logs.
53 }
54
Brad Bishopfac1b102017-05-15 22:29:20 -040055 /** @brief Invoke a method. */
56 template <typename ...Args>
57 static auto callMethod(
58 const std::string& busName,
59 const std::string& path,
60 const std::string& interface,
61 const std::string& method,
62 Args&& ... args)
63 {
64 auto reqMsg = getBus().new_method_call(
65 busName.c_str(),
66 path.c_str(),
67 interface.c_str(),
68 method.c_str());
69 reqMsg.append(std::forward<Args>(args)...);
70 return getBus().call(reqMsg);
71 }
72
73 /** @brief Invoke a method and read the response. */
74 template <typename Ret, typename ...Args>
75 static auto callMethodAndRead(
76 const std::string& busName,
77 const std::string& path,
78 const std::string& interface,
79 const std::string& method,
80 Args&& ... args)
81 {
82 Ret resp;
83 sdbusplus::message::message respMsg =
84 callMethod<Args...>(
85 busName,
86 path,
87 interface,
88 method,
89 std::forward<Args>(args)...);
90 respMsg.read(resp);
91 return resp;
92 }
93
94 /** @brief Register a DBus signal callback. */
95 static auto addMatch(
96 const std::string& match,
97 const sdbusplus::bus::match::match::callback_t& callback)
98 {
99 getWatches().emplace_back(
100 getBus(),
101 match,
102 callback);
103 }
104};
105
106} // namespace monitoring
107} // namespace dbus
108} // namespace phosphor