1 #pragma once
2 
3 #include <systemd/sd-bus.h>
4 
5 #include <chrono>
6 
7 // ABC for sdbus implementation.
8 namespace sdbusplus
9 {
10 
11 // Defined by systemd taking uint64_t usec params
12 using SdBusDuration =
13     std::chrono::duration<uint64_t, std::chrono::microseconds::period>;
14 
15 // A wrapper for interfacing or testing sd-bus.  This will hold methods for
16 // buses, messages, etc.
17 class SdBusInterface
18 {
19   public:
20     virtual ~SdBusInterface() = default;
21 
22     virtual int sd_bus_add_object_manager(sd_bus* bus, sd_bus_slot** slot,
23                                           const char* path) = 0;
24 
25     virtual int sd_bus_add_object_vtable(sd_bus* bus, sd_bus_slot** slot,
26                                          const char* path,
27                                          const char* interface,
28                                          const sd_bus_vtable* vtable,
29                                          void* userdata) = 0;
30 
31     virtual int sd_bus_attach_event(sd_bus* bus, sd_event* e, int priority) = 0;
32 
33     virtual int sd_bus_call(sd_bus* bus, sd_bus_message* m, uint64_t usec,
34                             sd_bus_error* ret_error,
35                             sd_bus_message** reply) = 0;
36 
37     virtual int sd_bus_detach_event(sd_bus* bus) = 0;
38 
39     virtual int sd_bus_emit_interfaces_added_strv(sd_bus* bus, const char* path,
40                                                   char** interfaces) = 0;
41     virtual int sd_bus_emit_interfaces_removed_strv(sd_bus* bus,
42                                                     const char* path,
43                                                     char** interfaces) = 0;
44     virtual int sd_bus_emit_object_added(sd_bus* bus, const char* path) = 0;
45     virtual int sd_bus_emit_object_removed(sd_bus* bus, const char* path) = 0;
46     virtual int sd_bus_emit_properties_changed_strv(sd_bus* bus,
47                                                     const char* path,
48                                                     const char* interface,
49                                                     char** names) = 0;
50 
51     virtual int sd_bus_error_set(sd_bus_error* e, const char* name,
52                                  const char* message) = 0;
53     virtual int sd_bus_error_set_const(sd_bus_error* e, const char* name,
54                                        const char* message) = 0;
55     virtual int sd_bus_error_get_errno(const sd_bus_error* e) = 0;
56     virtual int sd_bus_error_set_errno(sd_bus_error* e, int error) = 0;
57     virtual int sd_bus_error_is_set(const sd_bus_error* e) = 0;
58     virtual void sd_bus_error_free(sd_bus_error* e) = 0;
59 
60     virtual sd_event* sd_bus_get_event(sd_bus* bus) = 0;
61     virtual int sd_bus_get_fd(sd_bus* bus) = 0;
62     virtual int sd_bus_get_unique_name(sd_bus* bus, const char** unique) = 0;
63 
64     virtual int sd_bus_list_names(sd_bus* bus, char*** acquired,
65                                   char*** activatable) = 0;
66 
67     // https://github.com/systemd/systemd/blob/master/src/systemd/sd-bus.h
68     virtual int sd_bus_message_append_basic(sd_bus_message* message, char type,
69                                             const void* value) = 0;
70 
71     virtual int sd_bus_message_at_end(sd_bus_message* m, int complete) = 0;
72 
73     virtual int sd_bus_message_close_container(sd_bus_message* m) = 0;
74 
75     virtual int sd_bus_message_enter_container(sd_bus_message* m, char type,
76                                                const char* contents) = 0;
77 
78     virtual int sd_bus_message_exit_container(sd_bus_message* m) = 0;
79 
80     virtual sd_bus* sd_bus_message_get_bus(sd_bus_message* m) = 0;
81     virtual int sd_bus_message_get_type(sd_bus_message* m, uint8_t* type) = 0;
82     virtual int sd_bus_message_get_cookie(sd_bus_message* m,
83                                           uint64_t* cookie) = 0;
84     virtual int sd_bus_message_get_reply_cookie(sd_bus_message* m,
85                                                 uint64_t* cookie) = 0;
86     virtual const char* sd_bus_message_get_destination(sd_bus_message* m) = 0;
87     virtual const char* sd_bus_message_get_interface(sd_bus_message* m) = 0;
88     virtual const char* sd_bus_message_get_member(sd_bus_message* m) = 0;
89     virtual const char* sd_bus_message_get_path(sd_bus_message* m) = 0;
90     virtual const char* sd_bus_message_get_sender(sd_bus_message* m) = 0;
91     virtual const char* sd_bus_message_get_signature(sd_bus_message* m,
92                                                      int complete) = 0;
93     virtual int sd_bus_message_get_errno(sd_bus_message* m) = 0;
94     virtual const sd_bus_error* sd_bus_message_get_error(sd_bus_message* m) = 0;
95 
96     virtual int sd_bus_message_is_method_call(sd_bus_message* m,
97                                               const char* interface,
98                                               const char* member) = 0;
99     virtual int sd_bus_message_is_method_error(sd_bus_message* m,
100                                                const char* name) = 0;
101     virtual int sd_bus_message_is_signal(sd_bus_message* m,
102                                          const char* interface,
103                                          const char* member) = 0;
104 
105     virtual int sd_bus_message_new_method_call(sd_bus* bus, sd_bus_message** m,
106                                                const char* destination,
107                                                const char* path,
108                                                const char* interface,
109                                                const char* member) = 0;
110 
111     virtual int sd_bus_message_new_method_return(sd_bus_message* call,
112                                                  sd_bus_message** m) = 0;
113 
114     virtual int sd_bus_message_new_method_errno(sd_bus_message* call,
115                                                 sd_bus_message** m, int error,
116                                                 const sd_bus_error* p) = 0;
117 
118     virtual int sd_bus_message_new_signal(sd_bus* bus, sd_bus_message** m,
119                                           const char* path,
120                                           const char* interface,
121                                           const char* member) = 0;
122 
123     virtual int sd_bus_message_open_container(sd_bus_message* m, char type,
124                                               const char* contents) = 0;
125 
126     virtual int sd_bus_message_read_basic(sd_bus_message* m, char type,
127                                           void* p) = 0;
128 
129     virtual sd_bus_message* sd_bus_message_ref(sd_bus_message* m) = 0;
130 
131     virtual int sd_bus_message_skip(sd_bus_message* m, const char* types) = 0;
132 
133     virtual int sd_bus_message_verify_type(sd_bus_message* m, char type,
134                                            const char* contents) = 0;
135 
136     virtual int sd_bus_process(sd_bus* bus, sd_bus_message** r) = 0;
137 
138     virtual sd_bus* sd_bus_ref(sd_bus* bus) = 0;
139 
140     virtual int sd_bus_request_name(sd_bus* bus, const char* name,
141                                     uint64_t flags) = 0;
142 
143     virtual int sd_bus_send(sd_bus* bus, sd_bus_message* m,
144                             uint64_t* cookie) = 0;
145 
146     virtual sd_bus* sd_bus_unref(sd_bus* bus) = 0;
147     virtual sd_bus* sd_bus_flush_close_unref(sd_bus* bus) = 0;
148 
149     virtual int sd_bus_flush(sd_bus* bus) = 0;
150     virtual void sd_bus_close(sd_bus* bus) = 0;
151 
152     virtual int sd_bus_wait(sd_bus* bus, uint64_t timeout_usec) = 0;
153 };
154 
155 class SdBusImpl : public SdBusInterface
156 {
157   public:
158     SdBusImpl() = default;
159     ~SdBusImpl() = default;
160     SdBusImpl(const SdBusImpl&) = default;
161     SdBusImpl& operator=(const SdBusImpl&) = default;
162     SdBusImpl(SdBusImpl&&) = default;
163     SdBusImpl& operator=(SdBusImpl&&) = default;
164 
165     int sd_bus_add_object_manager(sd_bus* bus, sd_bus_slot** slot,
166                                   const char* path) override
167     {
168         return ::sd_bus_add_object_manager(bus, slot, path);
169     }
170 
171     int sd_bus_add_object_vtable(sd_bus* bus, sd_bus_slot** slot,
172                                  const char* path, const char* interface,
173                                  const sd_bus_vtable* vtable,
174                                  void* userdata) override
175     {
176         return ::sd_bus_add_object_vtable(bus, slot, path, interface, vtable,
177                                           userdata);
178     }
179 
180     int sd_bus_attach_event(sd_bus* bus, sd_event* e, int priority) override
181     {
182         return ::sd_bus_attach_event(bus, e, priority);
183     }
184 
185     int sd_bus_call(sd_bus* bus, sd_bus_message* m, uint64_t usec,
186                     sd_bus_error* ret_error, sd_bus_message** reply) override
187     {
188         return ::sd_bus_call(bus, m, usec, ret_error, reply);
189     }
190 
191     int sd_bus_detach_event(sd_bus* bus) override
192     {
193         return ::sd_bus_detach_event(bus);
194     }
195 
196     int sd_bus_emit_interfaces_added_strv(sd_bus* bus, const char* path,
197                                           char** interfaces) override
198     {
199         return ::sd_bus_emit_interfaces_added_strv(bus, path, interfaces);
200     }
201 
202     int sd_bus_emit_interfaces_removed_strv(sd_bus* bus, const char* path,
203                                             char** interfaces) override
204     {
205         return ::sd_bus_emit_interfaces_removed_strv(bus, path, interfaces);
206     }
207 
208     int sd_bus_emit_object_added(sd_bus* bus, const char* path) override
209     {
210         return ::sd_bus_emit_object_added(bus, path);
211     }
212 
213     int sd_bus_emit_object_removed(sd_bus* bus, const char* path) override
214     {
215         return ::sd_bus_emit_object_removed(bus, path);
216     }
217 
218     int sd_bus_emit_properties_changed_strv(sd_bus* bus, const char* path,
219                                             const char* interface,
220                                             char** names) override
221     {
222         return ::sd_bus_emit_properties_changed_strv(bus, path, interface,
223                                                      names);
224     }
225 
226     int sd_bus_error_set(sd_bus_error* e, const char* name,
227                          const char* message) override
228     {
229         return ::sd_bus_error_set(e, name, message);
230     }
231 
232     int sd_bus_error_set_const(sd_bus_error* e, const char* name,
233                                const char* message) override
234     {
235         return ::sd_bus_error_set_const(e, name, message);
236     }
237 
238     int sd_bus_error_get_errno(const sd_bus_error* e) override
239     {
240         return ::sd_bus_error_get_errno(e);
241     }
242 
243     int sd_bus_error_set_errno(sd_bus_error* e, int error) override
244     {
245         return ::sd_bus_error_set_errno(e, error);
246     }
247 
248     int sd_bus_error_is_set(const sd_bus_error* e) override
249     {
250         return ::sd_bus_error_is_set(e);
251     }
252 
253     void sd_bus_error_free(sd_bus_error* e) override
254     {
255         return ::sd_bus_error_free(e);
256     }
257 
258     sd_event* sd_bus_get_event(sd_bus* bus) override
259     {
260         return ::sd_bus_get_event(bus);
261     }
262 
263     int sd_bus_get_fd(sd_bus* bus) override
264     {
265         return ::sd_bus_get_fd(bus);
266     }
267 
268     int sd_bus_get_unique_name(sd_bus* bus, const char** unique) override
269     {
270         return ::sd_bus_get_unique_name(bus, unique);
271     }
272 
273     int sd_bus_list_names(sd_bus* bus, char*** acquired,
274                           char*** activatable) override
275     {
276         return ::sd_bus_list_names(bus, acquired, activatable);
277     }
278 
279     int sd_bus_message_append_basic(sd_bus_message* message, char type,
280                                     const void* value) override
281     {
282         return ::sd_bus_message_append_basic(message, type, value);
283     }
284 
285     int sd_bus_message_at_end(sd_bus_message* m, int complete) override
286     {
287         return ::sd_bus_message_at_end(m, complete);
288     }
289 
290     int sd_bus_message_close_container(sd_bus_message* m) override
291     {
292         return ::sd_bus_message_close_container(m);
293     }
294 
295     int sd_bus_message_enter_container(sd_bus_message* m, char type,
296                                        const char* contents) override
297     {
298         return ::sd_bus_message_enter_container(m, type, contents);
299     }
300 
301     int sd_bus_message_exit_container(sd_bus_message* m) override
302     {
303         return ::sd_bus_message_exit_container(m);
304     }
305 
306     sd_bus* sd_bus_message_get_bus(sd_bus_message* m) override
307     {
308         return ::sd_bus_message_get_bus(m);
309     }
310 
311     int sd_bus_message_get_type(sd_bus_message* m, uint8_t* type) override
312     {
313         return ::sd_bus_message_get_type(m, type);
314     }
315 
316     int sd_bus_message_get_cookie(sd_bus_message* m, uint64_t* cookie) override
317     {
318         return ::sd_bus_message_get_cookie(m, cookie);
319     }
320 
321     int sd_bus_message_get_reply_cookie(sd_bus_message* m,
322                                         uint64_t* cookie) override
323     {
324         return ::sd_bus_message_get_reply_cookie(m, cookie);
325     }
326 
327     const char* sd_bus_message_get_destination(sd_bus_message* m) override
328     {
329         return ::sd_bus_message_get_destination(m);
330     }
331 
332     const char* sd_bus_message_get_interface(sd_bus_message* m) override
333     {
334         return ::sd_bus_message_get_interface(m);
335     }
336 
337     const char* sd_bus_message_get_member(sd_bus_message* m) override
338     {
339         return ::sd_bus_message_get_member(m);
340     }
341 
342     const char* sd_bus_message_get_path(sd_bus_message* m) override
343     {
344         return ::sd_bus_message_get_path(m);
345     }
346 
347     const char* sd_bus_message_get_sender(sd_bus_message* m) override
348     {
349         return ::sd_bus_message_get_sender(m);
350     }
351 
352     const char* sd_bus_message_get_signature(sd_bus_message* m,
353                                              int complete) override
354     {
355         return ::sd_bus_message_get_signature(m, complete);
356     }
357 
358     int sd_bus_message_get_errno(sd_bus_message* m) override
359     {
360         return ::sd_bus_message_get_errno(m);
361     }
362 
363     const sd_bus_error* sd_bus_message_get_error(sd_bus_message* m) override
364     {
365         return ::sd_bus_message_get_error(m);
366     }
367 
368     int sd_bus_message_is_method_call(sd_bus_message* m, const char* interface,
369                                       const char* member) override
370     {
371         return ::sd_bus_message_is_method_call(m, interface, member);
372     }
373 
374     int sd_bus_message_is_method_error(sd_bus_message* m,
375                                        const char* name) override
376     {
377         return ::sd_bus_message_is_method_error(m, name);
378     }
379 
380     int sd_bus_message_is_signal(sd_bus_message* m, const char* interface,
381                                  const char* member) override
382     {
383         return ::sd_bus_message_is_signal(m, interface, member);
384     }
385 
386     int sd_bus_message_new_method_call(sd_bus* bus, sd_bus_message** m,
387                                        const char* destination,
388                                        const char* path, const char* interface,
389                                        const char* member) override
390     {
391         return ::sd_bus_message_new_method_call(bus, m, destination, path,
392                                                 interface, member);
393     }
394 
395     int sd_bus_message_new_method_return(sd_bus_message* call,
396                                          sd_bus_message** m) override
397     {
398         return ::sd_bus_message_new_method_return(call, m);
399     }
400 
401     int sd_bus_message_new_method_errno(sd_bus_message* call,
402                                         sd_bus_message** m, int error,
403                                         const sd_bus_error* p) override
404     {
405         return ::sd_bus_message_new_method_errno(call, m, error, p);
406     }
407 
408     int sd_bus_message_new_signal(sd_bus* bus, sd_bus_message** m,
409                                   const char* path, const char* interface,
410                                   const char* member) override
411     {
412         return ::sd_bus_message_new_signal(bus, m, path, interface, member);
413     }
414 
415     int sd_bus_message_open_container(sd_bus_message* m, char type,
416                                       const char* contents) override
417     {
418         return ::sd_bus_message_open_container(m, type, contents);
419     }
420 
421     int sd_bus_message_read_basic(sd_bus_message* m, char type,
422                                   void* p) override
423     {
424         return ::sd_bus_message_read_basic(m, type, p);
425     }
426 
427     sd_bus_message* sd_bus_message_ref(sd_bus_message* m) override
428     {
429         return ::sd_bus_message_ref(m);
430     }
431 
432     int sd_bus_message_skip(sd_bus_message* m, const char* types) override
433     {
434         return ::sd_bus_message_skip(m, types);
435     }
436 
437     int sd_bus_message_verify_type(sd_bus_message* m, char type,
438                                    const char* contents) override
439     {
440         return ::sd_bus_message_verify_type(m, type, contents);
441     }
442 
443     int sd_bus_process(sd_bus* bus, sd_bus_message** r) override
444     {
445         return ::sd_bus_process(bus, r);
446     }
447 
448     sd_bus* sd_bus_ref(sd_bus* bus) override
449     {
450         return ::sd_bus_ref(bus);
451     }
452 
453     int sd_bus_request_name(sd_bus* bus, const char* name,
454                             uint64_t flags) override
455     {
456         return ::sd_bus_request_name(bus, name, flags);
457     }
458 
459     int sd_bus_send(sd_bus* bus, sd_bus_message* m, uint64_t* cookie) override
460     {
461         return ::sd_bus_send(bus, m, cookie);
462     }
463 
464     sd_bus* sd_bus_unref(sd_bus* bus) override
465     {
466         return ::sd_bus_unref(bus);
467     }
468 
469     sd_bus* sd_bus_flush_close_unref(sd_bus* bus) override
470     {
471         return ::sd_bus_flush_close_unref(bus);
472     }
473 
474     int sd_bus_flush(sd_bus* bus) override
475     {
476         return ::sd_bus_flush(bus);
477     }
478 
479     void sd_bus_close(sd_bus* bus) override
480     {
481         ::sd_bus_close(bus);
482     }
483 
484     int sd_bus_wait(sd_bus* bus, uint64_t timeout_usec) override
485     {
486         return ::sd_bus_wait(bus, timeout_usec);
487     }
488 };
489 
490 extern SdBusImpl sdbus_impl;
491 
492 } // namespace sdbusplus
493