summaryrefslogtreecommitdiffstats
path: root/sdbusplus.hpp
blob: 133443f90f6e53663a693ed2a16db491f179154d (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#pragma once

#include <sdbusplus/bus.hpp>
#include <sdbusplus/message.hpp>
#include <sdbusplus/bus/match.hpp>
#include <phosphor-logging/log.hpp>
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include <xyz/openbmc_project/Common/error.hpp>

namespace phosphor
{
namespace fan
{
namespace util
{
namespace detail
{
namespace errors = sdbusplus::xyz::openbmc_project::Common::Error;
} // namespace detail

/** @class SDBusPlus
 *  @brief DBus access delegate implementation for sdbusplus.
 */
class SDBusPlus
{

    public:
        /** @brief Get the bus connection. */
        static auto& getBus() __attribute__((pure))
        {
            static auto bus = sdbusplus::bus::new_default();
            return bus;
        }

        /** @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)...);
            auto respMsg = getBus().call(reqMsg);

            if (respMsg.is_method_error())
            {
                phosphor::logging::log<phosphor::logging::level::INFO>(
                        "Failed to invoke DBus method.",
                        phosphor::logging::entry("PATH=%s", path.c_str()),
                        phosphor::logging::entry(
                            "INTERFACE=%s", interface.c_str()),
                        phosphor::logging::entry("METHOD=%s", method.c_str()));
                phosphor::logging::elog<detail::errors::InternalFailure>();
            }

            return respMsg;
        }

        /** @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)
        {
            sdbusplus::message::message respMsg =
                callMethod<Args...>(
                    busName,
                    path,
                    interface,
                    method,
                    std::forward<Args>(args)...);
            Ret resp;
            respMsg.read(resp);
            return resp;
        }

        /** @brief Get service from the mapper. */
        static auto getService(
            const std::string& path,
            const std::string& interface)
        {
            using namespace std::literals::string_literals;
            using GetObject = std::map<std::string, std::vector<std::string>>;

            auto mapperResp = callMethodAndRead<GetObject>(
                                  "xyz.openbmc_project.ObjectMapper"s,
                                  "/xyz/openbmc_project/object_mapper"s,
                                  "xyz.openbmc_project.ObjectMapper"s,
                                  "GetObject"s,
                                  path,
                                  GetObject::mapped_type{interface});

            if (mapperResp.empty())
            {
                phosphor::logging::log<phosphor::logging::level::INFO>(
                        "Object not found.",
                        phosphor::logging::entry("PATH=%s", path.c_str()),
                        phosphor::logging::entry(
                            "INTERFACE=%s", interface.c_str()));
                phosphor::logging::elog<detail::errors::InternalFailure>();
            }
            return mapperResp.begin()->first;
        }

        /** @brief Get a property with mapper lookup. */
        template <typename Property>
        static auto getProperty(
            const std::string& path,
            const std::string& interface,
            const std::string& property)
        {
            using namespace std::literals::string_literals;

            auto msg = callMethod(
                           getService(path, interface),
                           path,
                           "org.freedesktop.DBus.Properties"s,
                           "Get"s,
                           interface,
                           property);
            sdbusplus::message::variant<Property> value;
            msg.read(value);
            return value.template get<Property>();
        }

        /** @brief Set a property with mapper lookup. */
        template <typename Property>
        static void setProperty(
            const std::string& path,
            const std::string& interface,
            const std::string& property,
            Property&& value)
        {
            using namespace std::literals::string_literals;

            sdbusplus::message::variant<Property> varValue(
                std::forward<Property>(value));

            callMethod(
                getService(path, interface),
                path,
                "org.freedesktop.DBus.Properties"s,
                "Set"s,
                interface,
                property,
                varValue);
        }

        /** @brief Invoke method with mapper lookup. */
        template <typename ...Args>
        static auto lookupAndCallMethod(
            const std::string& path,
            const std::string& interface,
            const std::string& method,
            Args&& ... args)
        {
            return callMethod(
                       getService(path, interface),
                       path,
                       interface,
                       method,
                       std::forward<Args>(args)...);
        }

        /** @brief Invoke method and read with mapper lookup. */
        template <typename Ret, typename ...Args>
        static auto lookupCallMethodAndRead(
            const std::string& path,
            const std::string& interface,
            const std::string& method,
            Args&& ... args)
        {
            return callMethodAndRead(
                       getService(path, interface),
                       path,
                       interface,
                       method,
                       std::forward<Args>(args)...);
        }
};

} // namespace util
} // namespace fan
} // namespace phosphor
OpenPOWER on IntegriCloud