summaryrefslogtreecommitdiffstats
path: root/sdbusplus/bus.hpp.in
blob: 9e7476b021c18825eaf658f0766538a8f05e880d (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#pragma once

#include <algorithm>
#include <climits>
#include <memory>
#include <sdbusplus/exception.hpp>
#include <sdbusplus/message.hpp>
#include <sdbusplus/sdbus.hpp>
#include <string>
#include <systemd/sd-bus.h>
#include <systemd/sd-event.h>
#include <vector>

namespace sdbusplus
{

// Forward declare.
namespace server
{
namespace interface
{
struct interface;
}
} // namespace server
namespace server
{
namespace manager
{
struct manager;
}
} // namespace server
namespace server
{
namespace object
{
template <class...> struct object;
}
} // namespace server
namespace bus
{
namespace match
{
struct match;
}
} // namespace bus

namespace bus
{

using busp_t = sd_bus*;
class bus;

/** @brief Get an instance of the 'default' bus. */
bus new_default();
/** @brief Get an instance of the 'user' session bus. */
bus new_user();
/** @brief Get an instance of the 'system' bus. */
bus new_system();

namespace details
{

/** @brief unique_ptr functor to release a bus reference. */
struct BusDeleter
{
    void operator()(sd_bus* ptr) const
    {
        deleter(ptr);
    }

    decltype(&sd_bus_flush_close_unref) deleter = sd_bus_flush_close_unref;
};

/** @brief Convert a vector of strings to c-style char** array. */
class Strv
{
    public:
        ~Strv() = default;
        Strv() = delete;
        Strv(const Strv&) = delete;
        Strv& operator=(const Strv&) = delete;
        Strv(Strv&&) = default;
        Strv& operator=(Strv&&) = default;

        explicit Strv(const std::vector<std::string>& v)
        {
            std::transform(v.begin(), v.end(), std::back_inserter(ptrs),
                           [](const auto& i) { return i.c_str(); });
            ptrs.push_back(nullptr);
        }

        explicit operator char**()
        {
            return const_cast<char**>(&ptrs.front());
        }

    private:
        std::vector<const char*> ptrs;
};

/* @brief Alias 'bus' to a unique_ptr type for auto-release. */
using bus = std::unique_ptr<sd_bus, BusDeleter>;

} // namespace details

/** @class bus
 *  @brief Provides C++ bindings to the sd_bus_* class functions.
 */
struct bus
{
    /* Define all of the basic class operations:
     *     Not allowed:
     *         - Default constructor to avoid nullptrs.
     *         - Copy operations due to internal unique_ptr.
     *     Allowed:
     *         - Move operations.
     *         - Destructor.
     */
    bus() = delete;
    bus(const bus&) = delete;
    bus& operator=(const bus&) = delete;

    bus(bus&&) = default;
    bus& operator=(bus&&) = default;
    ~bus() = default;

    bus(busp_t b, sdbusplus::SdBusInterface* intf);

    /** @brief Conversion constructor from 'busp_t'.
     *
     *  Increments ref-count of the bus-pointer and releases it when done.
     */
    explicit bus(busp_t b);

    /** @brief Constructor for 'bus'.
     *
     *  Takes ownership of the bus-pointer and releases it when done.
     *  This method will also cause the bus to be flushed and closed
     *  on destruction.
     */
    bus(busp_t b, std::false_type);

    /** @brief Release ownership of the stored bus-pointer. */
    busp_t release()
    {
        return _bus.release();
    }

    /** @brief Wait for new dbus messages or signals.
     *
     *  @param[in] timeout_us - Timeout in usec.
     */
    void wait(uint64_t timeout_us = ULLONG_MAX)
    {
        _intf->sd_bus_wait(_bus.get(), timeout_us);
    }

    /** @brief Process waiting dbus messages or signals. */
    auto process()
    {
        sd_bus_message* m = nullptr;
        int r = _intf->sd_bus_process(_bus.get(), &m);
        if (r < 0)
        {
            throw exception::SdBusError(-r, "sd_bus_process");
        }

        return message::message(m, _intf, std::false_type());
    }

    /** @brief Process waiting dbus messages or signals, discarding unhandled.
     */
    auto process_discard()
    {
        int r = _intf->sd_bus_process(_bus.get(), nullptr);
        if (r < 0)
        {
            throw exception::SdBusError(-r, "sd_bus_process discard");
        }
        return r > 0;
    }

    /** @brief Claim a service name on the dbus.
     *
     *  @param[in] service - The service name to claim.
     */
    void request_name(const char* service)
    {
        _intf->sd_bus_request_name(_bus.get(), service, 0);
    }

    /** @brief Create a method_call message.
     *
     *  @param[in] service - The service to call.
     *  @param[in] objpath - The object's path for the call.
     *  @param[in] interf - The object's interface to call.
     *  @param[in] method - The object's method to call.
     *
     *  @return A newly constructed message.
     */
    auto new_method_call(const char* service, const char* objpath,
                         const char* interf, const char* method)
    {
        sd_bus_message* m = nullptr;
        int r = _intf->sd_bus_message_new_method_call(_bus.get(), &m, service,
                                                      objpath, interf, method);
        if (r < 0)
        {
            throw exception::SdBusError(-r, "sd_bus_message_new_method_call");
        }

        return message::message(m, _intf, std::false_type());
    }

    /** @brief Create a signal message.
     *
     *  @param[in] objpath - The object's path for the signal.
     *  @param[in] interf - The object's interface for the signal.
     *  @param[in] member - The signal name.
     *
     *  @return A newly constructed message.
     */
    auto new_signal(const char* objpath, const char* interf, const char* member)
    {
        sd_bus_message* m = nullptr;
        int r = _intf->sd_bus_message_new_signal(_bus.get(), &m, objpath,
                                                 interf, member);
        if (r < 0)
        {
            throw exception::SdBusError(-r, "sd_bus_message_new_signal");
        }

        return message::message(m, _intf, std::false_type());
    }

    /** @brief Perform a message call.
     *
     *  @param[in] m - The method_call message.
     *  @param[in] timeout_us - The timeout for the method call.
     *
     *  @return The response message.
     */
    auto call(message::message& m, uint64_t timeout_us = 0)
    {
        sd_bus_error error = SD_BUS_ERROR_NULL;
        sd_bus_message* reply = nullptr;
        int r = _intf->sd_bus_call(_bus.get(), m.get(), timeout_us, &error,
                                   &reply);
        if (r < 0)
        {
            throw exception::SdBusError(&error, "sd_bus_call");
        }

        return message::message(reply, _intf, std::false_type());
    }

    /** @brief Perform a message call, ignoring the reply.
     *
     *  @param[in] m - The method_call message.
     *  @param[in] timeout_us - The timeout for the method call.
     */
    void call_noreply(message::message& m, uint64_t timeout_us = 0)
    {
        sd_bus_error error = SD_BUS_ERROR_NULL;
        int r = _intf->sd_bus_call(_bus.get(), m.get(), timeout_us, &error,
                                   nullptr);
        if (r < 0)
        {
            throw exception::SdBusError(&error, "sd_bus_call noreply");
        }
    }

    /** @brief Perform a message call, ignoring the reply and any errors
     *         in the dbus stack.
     *
     *  @param[in] m - The method_call message.
     *  @param[in] timeout_us - The timeout for the method call.
     */
    void call_noreply_noerror(message::message& m, uint64_t timeout_us = 0)
    {
        try
        {
            call_noreply(m, timeout_us);
        }
        catch (exception::SdBusError&)
        {
            // Intentionally ignoring these sd_bus errors
        }
    }

    /** @brief Get the bus unique name. Ex: ":1.11".
     *
     * @return The bus unique name.
     */
    auto get_unique_name()
    {
        const char* unique = nullptr;
        _intf->sd_bus_get_unique_name(_bus.get(), &unique);
        return std::string(unique);
    }

    auto get_fd(){
        return _intf->sd_bus_get_fd(_bus.get());
    }

    /** @brief Attach the bus with a sd-event event loop object.
     *
     *  @param[in] event - sd_event object.
     *  @param[in] priority - priority of bus event source.
     */
    void attach_event(sd_event* event, int priority)
    {
        _intf->sd_bus_attach_event(_bus.get(), event, priority);
    }

    /** @brief Detach the bus from its sd-event event loop object */
    void detach_event()
    {
        _intf->sd_bus_detach_event(_bus.get());
    }

    /** @brief Get the sd-event event loop object of the bus */
    auto get_event()
    {
        return _intf->sd_bus_get_event(_bus.get());
    }

    /** @brief Wrapper for sd_bus_emit_interfaces_added_strv
     *
     *  In general the similarly named server::object::object API should
     *  be used to manage emission of ObjectManager signals in favor
     *  of this one.  Provided here for complex usage scenarios.
     *
     *  @param[in] path - The path to forward.
     *  @param[in] ifaces - The interfaces to forward.
     */
    void emit_interfaces_added(const char* path,
                               const std::vector<std::string>& ifaces)
    {
        details::Strv s{ifaces};
        _intf->sd_bus_emit_interfaces_added_strv(_bus.get(), path,
                                                 static_cast<char**>(s));
    }

    /** @brief Wrapper for sd_bus_emit_interfaces_removed_strv
     *
     *  In general the similarly named server::object::object API should
     *  be used to manage emission of ObjectManager signals in favor
     *  of this one.  Provided here for complex usage scenarios.
     *
     *  @param[in] path - The path to forward.
     *  @param[in] ifaces - The interfaces to forward.
     */
    void emit_interfaces_removed(const char* path,
                                 const std::vector<std::string>& ifaces)
    {
        details::Strv s{ifaces};
        _intf->sd_bus_emit_interfaces_removed_strv(_bus.get(), path,
                                                   static_cast<char**>(s));
    }

    /** @brief Wrapper for sd_bus_emit_object_added
     *
     *  In general the similarly named server::object::object API should
     *  be used to manage emission of ObjectManager signals in favor
     *  of this one.  Provided here for complex usage scenarios.
     *
     *  @param[in] path - The path to forward to sd_bus_emit_object_added
     */
    void emit_object_added(const char* path)
    {
        _intf->sd_bus_emit_object_added(_bus.get(), path);
    }

    /** @brief Wrapper for sd_bus_emit_object_removed
     *
     *  In general the similarly named server::object::object API should
     *  be used to manage emission of ObjectManager signals in favor
     *  of this one.  Provided here for complex usage scenarios.
     *
     *  @param[in] path - The path to forward to sd_bus_emit_object_removed
     */
    void emit_object_removed(const char* path)
    {
        _intf->sd_bus_emit_object_removed(_bus.get(), path);
    }

    /** @brief Wrapper for sd_bus_list_names.
     *
     *  @return A vector of strings containing the 'acquired' names from
     *          sd_bus_list_names.
     */
    auto list_names_acquired()
    {
        char** names = nullptr;

        _intf->sd_bus_list_names(_bus.get(), &names, nullptr);

        std::vector<std::string> result;
        for (auto ptr = names; ptr && *ptr; ++ptr)
        {
            result.push_back(*ptr);
            free(*ptr);
        }
        free(names);

        return result;
    }

    /** @brief Get the SdBusInterface used by this bus.
     *
     *  @return A pointer to the SdBusInterface used by this bus.
     */
    sdbusplus::SdBusInterface* getInterface()
    {
        return _intf;
    }

    friend struct server::interface::interface;
    friend struct server::manager::manager;
    template <class... Args> friend struct server::object::object;
    friend struct match::match;

  protected:
    busp_t get()
    {
        return _bus.get();
    }
    sdbusplus::SdBusInterface* _intf;
    details::bus _bus;
};

inline bus::bus(busp_t b, sdbusplus::SdBusInterface* intf) :
    _intf(intf), _bus(_intf->sd_bus_ref(b))
{
    // We can leave this as the real deleter if we just use a null pointer.
    _bus.get_deleter().deleter = sd_bus_unref;

#if @WANT_TRANSACTION@
    // Emitting object added causes a message to get the properties
    // which can trigger a 'transaction' in the server bindings.  If
    // the bus isn't up far enough, this causes an assert deep in
    // sd-bus code.  Get the 'unique_name' to ensure the bus is up far
    // enough to avoid the assert.
    if (b != nullptr)
    {
        get_unique_name();
    }
#endif
}

inline bus::bus(busp_t b) : _intf(&sdbus_impl), _bus(_intf->sd_bus_ref(b))
{
    // We can leave this as the real deleter if we just use a null pointer.
    _bus.get_deleter().deleter = sd_bus_unref;

#if @WANT_TRANSACTION@
    // Emitting object added causes a message to get the properties
    // which can trigger a 'transaction' in the server bindings.  If
    // the bus isn't up far enough, this causes an assert deep in
    // sd-bus code.  Get the 'unique_name' to ensure the bus is up far
    // enough to avoid the assert.
    if (b != nullptr)
    {
        get_unique_name();
    }
#endif
}

inline bus::bus(busp_t b, std::false_type) : _intf(&sdbus_impl), _bus(b)
{
#if @WANT_TRANSACTION@
    // Emitting object added causes a message to get the properties
    // which can trigger a 'transaction' in the server bindings.  If
    // the bus isn't up far enough, this causes an assert deep in
    // sd-bus code.  Get the 'unique_name' to ensure the bus is up far
    // enough to avoid the assert.
    if (b != nullptr)
    {
        get_unique_name();
    }
#endif
}

inline bus new_default()
{
    sd_bus* b = nullptr;
    sd_bus_open(&b);
    return bus(b, std::false_type());
}

inline bus new_user()
{
    sd_bus* b = nullptr;
    sd_bus_open_user(&b);
    return bus(b, std::false_type());
}

inline bus new_system()
{
    sd_bus* b = nullptr;
    sd_bus_open_system(&b);
    return bus(b, std::false_type());
}

} // namespace bus

/** @brief Get the dbus bus from the message.
 *
 *  @return The dbus bus.
 */
inline auto message::message::get_bus()
{
    sd_bus* b = nullptr;
    b = _intf->sd_bus_message_get_bus(_msg.get());
    return bus::bus(b, _intf);
}

} // namespace sdbusplus
OpenPOWER on IntegriCloud