summaryrefslogtreecommitdiffstats
path: root/src/sdbusplus.hpp
blob: 8afb8b51eab91875da3d4a69a88f7d3ac2b1d3e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once

#include <sdbusplus/bus.hpp>
#include <sdbusplus/message.hpp>
#include <sdbusplus/bus/match.hpp>

namespace phosphor
{
namespace dbus
{
namespace monitoring
{

/** @class SDBusPlus
 *  @brief DBus access delegate implementation for sdbusplus.
 */
class SDBusPlus
{
    private:
        static auto& getBus()
        {
            static auto bus = sdbusplus::bus::new_default();
            return bus;
        }

        static auto& getWatches()
        {
            static std::vector<sdbusplus::bus::match::match> watches;
            return watches;
        }

    public:
        /** @brief Invoke a method; ignore reply. */
        template <typename ...Args>
        static void callMethodNoReply(
            const std::string& busName,
            const std::string& path,
            const std::string& interface,
            const std::string& method,
            Args&& ... args)
        {
            auto reqMsg = getBus().new_method_call(
                              busName.c_str(),
                              path.c_str(),
                              interface.c_str(),
                              method.c_str());
            reqMsg.append(std::forward<Args>(args)...);
            getBus().call_noreply(reqMsg);

            // TODO: openbmc/openbmc#1719
            // invoke these methods async, with a callback
            // handler that checks for errors and logs.
        }

        /** @brief Invoke a method. */
        template <typename ...Args>
        static auto callMethod(
            const std::string& busName,
            const std::string& path,
            const std::string& interface,
            const std::string& method,
            Args&& ... args)
        {
            auto reqMsg = getBus().new_method_call(
                              busName.c_str(),
                              path.c_str(),
                              interface.c_str(),
                              method.c_str());
            reqMsg.append(std::forward<Args>(args)...);
            return getBus().call(reqMsg);
        }

        /** @brief Invoke a method and read the response. */
        template <typename Ret, typename ...Args>
        static auto callMethodAndRead(
            const std::string& busName,
            const std::string& path,
            const std::string& interface,
            const std::string& method,
            Args&& ... args)
        {
            Ret resp;
            sdbusplus::message::message respMsg =
                callMethod<Args...>(
                    busName,
                    path,
                    interface,
                    method,
                    std::forward<Args>(args)...);
            respMsg.read(resp);
            return resp;
        }

        /** @brief Register a DBus signal callback. */
        static auto addMatch(
            const std::string& match,
            const sdbusplus::bus::match::match::callback_t& callback)
        {
            getWatches().emplace_back(
                    getBus(),
                    match,
                    callback);
        }
};

} // namespace monitoring
} // namespace dbus
} // namespace phosphor
OpenPOWER on IntegriCloud