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_error(sd_bus_message* call,
115                                                 sd_bus_message** m,
116                                                 const char* name,
117                                                 const char* description) = 0;
118 
119     virtual int sd_bus_message_new_method_errno(sd_bus_message* call,
120                                                 sd_bus_message** m, int error,
121                                                 const sd_bus_error* p) = 0;
122 
123     virtual int sd_bus_message_new_signal(sd_bus* bus, sd_bus_message** m,
124                                           const char* path,
125                                           const char* interface,
126                                           const char* member) = 0;
127 
128     virtual int sd_bus_message_open_container(sd_bus_message* m, char type,
129                                               const char* contents) = 0;
130 
131     virtual int sd_bus_message_read_basic(sd_bus_message* m, char type,
132                                           void* p) = 0;
133 
134     virtual sd_bus_message* sd_bus_message_ref(sd_bus_message* m) = 0;
135 
136     virtual int sd_bus_message_skip(sd_bus_message* m, const char* types) = 0;
137 
138     virtual int sd_bus_message_verify_type(sd_bus_message* m, char type,
139                                            const char* contents) = 0;
140 
141     virtual int sd_bus_process(sd_bus* bus, sd_bus_message** r) = 0;
142 
143     virtual sd_bus* sd_bus_ref(sd_bus* bus) = 0;
144 
145     virtual int sd_bus_request_name(sd_bus* bus, const char* name,
146                                     uint64_t flags) = 0;
147 
148     virtual int sd_bus_send(sd_bus* bus, sd_bus_message* m,
149                             uint64_t* cookie) = 0;
150 
151     virtual sd_bus* sd_bus_unref(sd_bus* bus) = 0;
152     virtual sd_bus* sd_bus_flush_close_unref(sd_bus* bus) = 0;
153 
154     virtual int sd_bus_flush(sd_bus* bus) = 0;
155     virtual void sd_bus_close(sd_bus* bus) = 0;
156 
157     virtual int sd_bus_wait(sd_bus* bus, uint64_t timeout_usec) = 0;
158 };
159 
160 class SdBusImpl : public SdBusInterface
161 {
162   public:
163     SdBusImpl() = default;
164     ~SdBusImpl() = default;
165     SdBusImpl(const SdBusImpl&) = default;
166     SdBusImpl& operator=(const SdBusImpl&) = default;
167     SdBusImpl(SdBusImpl&&) = default;
168     SdBusImpl& operator=(SdBusImpl&&) = default;
169 
170     int sd_bus_add_object_manager(sd_bus* bus, sd_bus_slot** slot,
171                                   const char* path) override
172     {
173         return ::sd_bus_add_object_manager(bus, slot, path);
174     }
175 
176     int sd_bus_add_object_vtable(sd_bus* bus, sd_bus_slot** slot,
177                                  const char* path, const char* interface,
178                                  const sd_bus_vtable* vtable,
179                                  void* userdata) override
180     {
181         return ::sd_bus_add_object_vtable(bus, slot, path, interface, vtable,
182                                           userdata);
183     }
184 
185     int sd_bus_attach_event(sd_bus* bus, sd_event* e, int priority) override
186     {
187         return ::sd_bus_attach_event(bus, e, priority);
188     }
189 
190     int sd_bus_call(sd_bus* bus, sd_bus_message* m, uint64_t usec,
191                     sd_bus_error* ret_error, sd_bus_message** reply) override
192     {
193         return ::sd_bus_call(bus, m, usec, ret_error, reply);
194     }
195 
196     int sd_bus_detach_event(sd_bus* bus) override
197     {
198         return ::sd_bus_detach_event(bus);
199     }
200 
201     int sd_bus_emit_interfaces_added_strv(sd_bus* bus, const char* path,
202                                           char** interfaces) override
203     {
204         return ::sd_bus_emit_interfaces_added_strv(bus, path, interfaces);
205     }
206 
207     int sd_bus_emit_interfaces_removed_strv(sd_bus* bus, const char* path,
208                                             char** interfaces) override
209     {
210         return ::sd_bus_emit_interfaces_removed_strv(bus, path, interfaces);
211     }
212 
213     int sd_bus_emit_object_added(sd_bus* bus, const char* path) override
214     {
215         return ::sd_bus_emit_object_added(bus, path);
216     }
217 
218     int sd_bus_emit_object_removed(sd_bus* bus, const char* path) override
219     {
220         return ::sd_bus_emit_object_removed(bus, path);
221     }
222 
223     int sd_bus_emit_properties_changed_strv(sd_bus* bus, const char* path,
224                                             const char* interface,
225                                             char** names) override
226     {
227         return ::sd_bus_emit_properties_changed_strv(bus, path, interface,
228                                                      names);
229     }
230 
231     int sd_bus_error_set(sd_bus_error* e, const char* name,
232                          const char* message) override
233     {
234         return ::sd_bus_error_set(e, name, message);
235     }
236 
237     int sd_bus_error_set_const(sd_bus_error* e, const char* name,
238                                const char* message) override
239     {
240         return ::sd_bus_error_set_const(e, name, message);
241     }
242 
243     int sd_bus_error_get_errno(const sd_bus_error* e) override
244     {
245         return ::sd_bus_error_get_errno(e);
246     }
247 
248     int sd_bus_error_set_errno(sd_bus_error* e, int error) override
249     {
250         return ::sd_bus_error_set_errno(e, error);
251     }
252 
253     int sd_bus_error_is_set(const sd_bus_error* e) override
254     {
255         return ::sd_bus_error_is_set(e);
256     }
257 
258     void sd_bus_error_free(sd_bus_error* e) override
259     {
260         return ::sd_bus_error_free(e);
261     }
262 
263     sd_event* sd_bus_get_event(sd_bus* bus) override
264     {
265         return ::sd_bus_get_event(bus);
266     }
267 
268     int sd_bus_get_fd(sd_bus* bus) override
269     {
270         return ::sd_bus_get_fd(bus);
271     }
272 
273     int sd_bus_get_unique_name(sd_bus* bus, const char** unique) override
274     {
275         return ::sd_bus_get_unique_name(bus, unique);
276     }
277 
278     int sd_bus_list_names(sd_bus* bus, char*** acquired,
279                           char*** activatable) override
280     {
281         return ::sd_bus_list_names(bus, acquired, activatable);
282     }
283 
284     int sd_bus_message_append_basic(sd_bus_message* message, char type,
285                                     const void* value) override
286     {
287         return ::sd_bus_message_append_basic(message, type, value);
288     }
289 
290     int sd_bus_message_at_end(sd_bus_message* m, int complete) override
291     {
292         return ::sd_bus_message_at_end(m, complete);
293     }
294 
295     int sd_bus_message_close_container(sd_bus_message* m) override
296     {
297         return ::sd_bus_message_close_container(m);
298     }
299 
300     int sd_bus_message_enter_container(sd_bus_message* m, char type,
301                                        const char* contents) override
302     {
303         return ::sd_bus_message_enter_container(m, type, contents);
304     }
305 
306     int sd_bus_message_exit_container(sd_bus_message* m) override
307     {
308         return ::sd_bus_message_exit_container(m);
309     }
310 
311     sd_bus* sd_bus_message_get_bus(sd_bus_message* m) override
312     {
313         return ::sd_bus_message_get_bus(m);
314     }
315 
316     int sd_bus_message_get_type(sd_bus_message* m, uint8_t* type) override
317     {
318         return ::sd_bus_message_get_type(m, type);
319     }
320 
321     int sd_bus_message_get_cookie(sd_bus_message* m, uint64_t* cookie) override
322     {
323         return ::sd_bus_message_get_cookie(m, cookie);
324     }
325 
326     int sd_bus_message_get_reply_cookie(sd_bus_message* m,
327                                         uint64_t* cookie) override
328     {
329         return ::sd_bus_message_get_reply_cookie(m, cookie);
330     }
331 
332     const char* sd_bus_message_get_destination(sd_bus_message* m) override
333     {
334         return ::sd_bus_message_get_destination(m);
335     }
336 
337     const char* sd_bus_message_get_interface(sd_bus_message* m) override
338     {
339         return ::sd_bus_message_get_interface(m);
340     }
341 
342     const char* sd_bus_message_get_member(sd_bus_message* m) override
343     {
344         return ::sd_bus_message_get_member(m);
345     }
346 
347     const char* sd_bus_message_get_path(sd_bus_message* m) override
348     {
349         return ::sd_bus_message_get_path(m);
350     }
351 
352     const char* sd_bus_message_get_sender(sd_bus_message* m) override
353     {
354         return ::sd_bus_message_get_sender(m);
355     }
356 
357     const char* sd_bus_message_get_signature(sd_bus_message* m,
358                                              int complete) override
359     {
360         return ::sd_bus_message_get_signature(m, complete);
361     }
362 
363     int sd_bus_message_get_errno(sd_bus_message* m) override
364     {
365         return ::sd_bus_message_get_errno(m);
366     }
367 
368     const sd_bus_error* sd_bus_message_get_error(sd_bus_message* m) override
369     {
370         return ::sd_bus_message_get_error(m);
371     }
372 
373     int sd_bus_message_is_method_call(sd_bus_message* m, const char* interface,
374                                       const char* member) override
375     {
376         return ::sd_bus_message_is_method_call(m, interface, member);
377     }
378 
379     int sd_bus_message_is_method_error(sd_bus_message* m,
380                                        const char* name) override
381     {
382         return ::sd_bus_message_is_method_error(m, name);
383     }
384 
385     int sd_bus_message_is_signal(sd_bus_message* m, const char* interface,
386                                  const char* member) override
387     {
388         return ::sd_bus_message_is_signal(m, interface, member);
389     }
390 
391     int sd_bus_message_new_method_call(sd_bus* bus, sd_bus_message** m,
392                                        const char* destination,
393                                        const char* path, const char* interface,
394                                        const char* member) override
395     {
396         return ::sd_bus_message_new_method_call(bus, m, destination, path,
397                                                 interface, member);
398     }
399 
400     int sd_bus_message_new_method_return(sd_bus_message* call,
401                                          sd_bus_message** m) override
402     {
403         return ::sd_bus_message_new_method_return(call, m);
404     }
405 
406     int sd_bus_message_new_method_error(sd_bus_message* call,
407                                         sd_bus_message** m, const char* name,
408                                         const char* description) override
409     {
410         return ::sd_bus_message_new_method_errorf(call, m, name, "%s",
411                                                   description);
412     }
413 
414     int sd_bus_message_new_method_errno(sd_bus_message* call,
415                                         sd_bus_message** m, int error,
416                                         const sd_bus_error* p) override
417     {
418         return ::sd_bus_message_new_method_errno(call, m, error, p);
419     }
420 
421     int sd_bus_message_new_signal(sd_bus* bus, sd_bus_message** m,
422                                   const char* path, const char* interface,
423                                   const char* member) override
424     {
425         return ::sd_bus_message_new_signal(bus, m, path, interface, member);
426     }
427 
428     int sd_bus_message_open_container(sd_bus_message* m, char type,
429                                       const char* contents) override
430     {
431         return ::sd_bus_message_open_container(m, type, contents);
432     }
433 
434     int sd_bus_message_read_basic(sd_bus_message* m, char type,
435                                   void* p) override
436     {
437         return ::sd_bus_message_read_basic(m, type, p);
438     }
439 
440     sd_bus_message* sd_bus_message_ref(sd_bus_message* m) override
441     {
442         return ::sd_bus_message_ref(m);
443     }
444 
445     int sd_bus_message_skip(sd_bus_message* m, const char* types) override
446     {
447         return ::sd_bus_message_skip(m, types);
448     }
449 
450     int sd_bus_message_verify_type(sd_bus_message* m, char type,
451                                    const char* contents) override
452     {
453         return ::sd_bus_message_verify_type(m, type, contents);
454     }
455 
456     int sd_bus_process(sd_bus* bus, sd_bus_message** r) override
457     {
458         return ::sd_bus_process(bus, r);
459     }
460 
461     sd_bus* sd_bus_ref(sd_bus* bus) override
462     {
463         return ::sd_bus_ref(bus);
464     }
465 
466     int sd_bus_request_name(sd_bus* bus, const char* name,
467                             uint64_t flags) override
468     {
469         return ::sd_bus_request_name(bus, name, flags);
470     }
471 
472     int sd_bus_send(sd_bus* bus, sd_bus_message* m, uint64_t* cookie) override
473     {
474         return ::sd_bus_send(bus, m, cookie);
475     }
476 
477     sd_bus* sd_bus_unref(sd_bus* bus) override
478     {
479         return ::sd_bus_unref(bus);
480     }
481 
482     sd_bus* sd_bus_flush_close_unref(sd_bus* bus) override
483     {
484         return ::sd_bus_flush_close_unref(bus);
485     }
486 
487     int sd_bus_flush(sd_bus* bus) override
488     {
489         return ::sd_bus_flush(bus);
490     }
491 
492     void sd_bus_close(sd_bus* bus) override
493     {
494         ::sd_bus_close(bus);
495     }
496 
497     int sd_bus_wait(sd_bus* bus, uint64_t timeout_usec) override
498     {
499         return ::sd_bus_wait(bus, timeout_usec);
500     }
501 };
502 
503 extern SdBusImpl sdbus_impl;
504 
505 } // namespace sdbusplus
506