]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/sd_bus_add_object_vtable.xml
sd-bus: Wrap add_object_vtable docs at 100 columns
[thirdparty/systemd.git] / man / sd_bus_add_object_vtable.xml
1 <?xml version='1.0'?> <!--*-nxml-*-->
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4 <!-- SPDX-License-Identifier: LGPL-2.1+ -->
5
6 <refentry id="sd_bus_add_object_vtable"
7 xmlns:xi="http://www.w3.org/2001/XInclude">
8
9 <refentryinfo>
10 <title>sd_bus_add_object_vtable</title>
11 <productname>systemd</productname>
12 </refentryinfo>
13
14 <refmeta>
15 <refentrytitle>sd_bus_add_object_vtable</refentrytitle>
16 <manvolnum>3</manvolnum>
17 </refmeta>
18
19 <refnamediv>
20 <refname>sd_bus_add_object_vtable</refname>
21 <refname>sd_bus_add_fallback_vtable</refname>
22 <refname>SD_BUS_VTABLE_START</refname>
23 <refname>SD_BUS_VTABLE_END</refname>
24 <refname>SD_BUS_METHOD_WITH_NAMES_OFFSET</refname>
25 <refname>SD_BUS_METHOD_WITH_NAMES</refname>
26 <refname>SD_BUS_METHOD_WITH_OFFSET</refname>
27 <refname>SD_BUS_METHOD</refname>
28 <refname>SD_BUS_SIGNAL_WITH_NAMES</refname>
29 <refname>SD_BUS_SIGNAL</refname>
30 <refname>SD_BUS_WRITABLE_PROPERTY</refname>
31 <refname>SD_BUS_PROPERTY</refname>
32 <refname>SD_BUS_PARAM</refname>
33
34 <refpurpose>Declare properties and methods for a D-Bus path</refpurpose>
35 </refnamediv>
36
37 <refsynopsisdiv>
38 <funcsynopsis>
39 <funcsynopsisinfo>#include &lt;systemd/sd-bus-vtable.h&gt;</funcsynopsisinfo>
40
41 <funcprototype>
42 <funcdef>typedef int (*<function>sd_bus_message_handler_t</function>)</funcdef>
43 <paramdef>sd_bus_message *<parameter>m</parameter></paramdef>
44 <paramdef>void *<parameter>userdata</parameter></paramdef>
45 <paramdef>sd_bus_error *<parameter>ret_error</parameter></paramdef>
46 </funcprototype>
47
48 <funcprototype>
49 <funcdef>typedef int (*<function>sd_bus_property_get_t</function>)</funcdef>
50 <paramdef>sd_bus *<parameter>bus</parameter></paramdef>
51 <paramdef>const char *<parameter>path</parameter></paramdef>
52 <paramdef>const char *<parameter>interface</parameter></paramdef>
53 <paramdef>const char *<parameter>property</parameter></paramdef>
54 <paramdef>sd_bus_message *<parameter>reply</parameter></paramdef>
55 <paramdef>void *<parameter>userdata</parameter></paramdef>
56 <paramdef>sd_bus_error *<parameter>ret_error</parameter></paramdef>
57 </funcprototype>
58
59 <funcprototype>
60 <funcdef>typedef int (*<function>sd_bus_property_set_t</function>)</funcdef>
61 <paramdef>sd_bus *<parameter>bus</parameter></paramdef>
62 <paramdef>const char *<parameter>path</parameter></paramdef>
63 <paramdef>const char *<parameter>interface</parameter></paramdef>
64 <paramdef>const char *<parameter>property</parameter></paramdef>
65 <paramdef>sd_bus_message *<parameter>value</parameter></paramdef>
66 <paramdef>void *<parameter>userdata</parameter></paramdef>
67 <paramdef>sd_bus_error *<parameter>ret_error</parameter></paramdef>
68 </funcprototype>
69
70 <funcprototype>
71 <funcdef>typedef int (*<function>sd_bus_object_find_t</function>)</funcdef>
72 <paramdef>const char *<parameter>path</parameter></paramdef>
73 <paramdef>const char *<parameter>interface</parameter></paramdef>
74 <paramdef>void *<parameter>userdata</parameter></paramdef>
75 <paramdef>void **<parameter>ret_found</parameter></paramdef>
76 <paramdef>sd_bus_error *<parameter>ret_error</parameter></paramdef>
77 </funcprototype>
78
79 <funcprototype>
80 <funcdef>int <function>sd_bus_add_object_vtable</function></funcdef>
81 <paramdef>sd_bus *<parameter>bus</parameter></paramdef>
82 <paramdef>sd_bus_slot **<parameter>slot</parameter></paramdef>
83 <paramdef>const char *<parameter>path</parameter></paramdef>
84 <paramdef>const char *<parameter>interface</parameter></paramdef>
85 <paramdef>const sd_bus_vtable *<parameter>vtable</parameter></paramdef>
86 <paramdef>void *<parameter>userdata</parameter></paramdef>
87 </funcprototype>
88
89 <funcprototype>
90 <funcdef>int <function>sd_bus_add_fallback_vtable</function></funcdef>
91 <paramdef>sd_bus *<parameter>bus</parameter></paramdef>
92 <paramdef>sd_bus_slot **<parameter>slot</parameter></paramdef>
93 <paramdef>const char *<parameter>prefix</parameter></paramdef>
94 <paramdef>const char *<parameter>interface</parameter></paramdef>
95 <paramdef>const sd_bus_vtable *<parameter>vtable</parameter></paramdef>
96 <paramdef>sd_bus_object_find_t <parameter>find</parameter></paramdef>
97 <paramdef>void *<parameter>userdata</parameter></paramdef>
98 </funcprototype>
99
100 <para>
101 <constant>SD_BUS_VTABLE_START(<replaceable>flags</replaceable>)</constant>
102 </para>
103 <para>
104 <constant>SD_BUS_VTABLE_END</constant>
105 </para>
106 <para>
107 <constant>SD_BUS_METHOD_WITH_NAMES_OFFSET(
108 <replaceable>member</replaceable>,
109 <replaceable>signature</replaceable>,
110 <replaceable>in_names</replaceable>,
111 <replaceable>result</replaceable>,
112 <replaceable>out_names</replaceable>,
113 <replaceable>handler</replaceable>,
114 <replaceable>offset</replaceable>,
115 <replaceable>flags</replaceable>)
116 </constant>
117 </para>
118 <para>
119 <constant>SD_BUS_METHOD_WITH_NAMES(
120 <replaceable>member</replaceable>,
121 <replaceable>signature</replaceable>,
122 <replaceable>in_names</replaceable>,
123 <replaceable>result</replaceable>,
124 <replaceable>out_names</replaceable>,
125 <replaceable>handler</replaceable>,
126 <replaceable>flags</replaceable>)
127 </constant>
128 </para>
129 <para>
130 <constant>SD_BUS_METHOD_WITH_OFFSET(
131 <replaceable>member</replaceable>,
132 <replaceable>signature</replaceable>,
133 <replaceable>result</replaceable>,
134 <replaceable>handler</replaceable>,
135 <replaceable>offset</replaceable>,
136 <replaceable>flags</replaceable>)
137 </constant>
138 </para>
139 <para>
140 <constant>SD_BUS_METHOD(
141 <replaceable>member</replaceable>,
142 <replaceable>signature</replaceable>,
143 <replaceable>result</replaceable>,
144 <replaceable>handler</replaceable>,
145 <replaceable>flags</replaceable>)
146 </constant>
147 </para>
148 <para>
149 <constant>SD_BUS_SIGNAL_WITH_NAMES(
150 <replaceable>member</replaceable>,
151 <replaceable>signature</replaceable>,
152 <replaceable>names</replaceable>,
153 <replaceable>flags</replaceable>)
154 </constant>
155 </para>
156 <para>
157 <constant>SD_BUS_SIGNAL(
158 <replaceable>member</replaceable>,
159 <replaceable>signature</replaceable>,
160 <replaceable>flags</replaceable>)
161 </constant>
162 </para>
163 <para>
164 <constant>SD_BUS_WRITABLE_PROPERTY(
165 <replaceable>member</replaceable>,
166 <replaceable>signature</replaceable>,
167 <replaceable>get</replaceable>,
168 <replaceable>set</replaceable>,
169 <replaceable>offset</replaceable>,
170 <replaceable>flags</replaceable>)
171 </constant>
172 </para>
173 <para>
174 <constant>SD_BUS_PROPERTY(
175 <replaceable>member</replaceable>,
176 <replaceable>signature</replaceable>,
177 <replaceable>get</replaceable>,
178 <replaceable>offset</replaceable>,
179 <replaceable>flags</replaceable>)
180 </constant>
181 </para>
182 <para>
183 <constant>SD_BUS_PARAM(<replaceable>name</replaceable>)</constant>
184 </para>
185 </funcsynopsis>
186 </refsynopsisdiv>
187
188 <refsect1>
189 <title>Description</title>
190
191 <para><function>sd_bus_add_object_vtable()</function> is used to declare attributes for the path
192 object path <parameter>path</parameter> connected to the bus connection
193 <parameter>bus</parameter> under the interface <parameter>interface</parameter>. The table
194 <parameter>vtable</parameter> may contain property declarations using
195 <constant>SD_BUS_PROPERTY()</constant> or <constant>SD_BUS_WRITABLE_PROPERTY()</constant>,
196 method declarations using <constant>SD_BUS_METHOD()</constant>,
197 <constant>SD_BUS_METHOD_WITH_NAMES()</constant>,
198 <constant>SD_BUS_METHOD_WITH_OFFSET()</constant>, or
199 <constant>SD_BUS_METHOD_WITH_NAMES_OFFSET()</constant>, and signal declarations using
200 <constant>SD_BUS_SIGNAL_WITH_NAMES()</constant> or <constant>SD_BUS_SIGNAL()</constant>, see
201 below. The <replaceable>userdata</replaceable> parameter contains a pointer that will be passed
202 to various callback functions. It may be specified as <constant>NULL</constant> if no value is
203 necessary.</para>
204
205 <para><function>sd_bus_add_fallback_vtable()</function> is similar to
206 <function>sd_bus_add_object_vtable()</function>, but is used to register "fallback" attributes.
207 When looking for an attribute declaration, bus object paths registered with
208 <function>sd_bus_add_object_vtable()</function> are checked first. If no match is found, the
209 fallback vtables are checked for each prefix of the bus object path, i.e. with the last
210 slash-separated components successively removed. This allows the vtable to be used for an
211 arbitrary number of dynamically created objects.</para>
212
213 <para>Parameter <replaceable>find</replaceable> is a function which is used to locate the target
214 object based on the bus object path <replaceable>path</replaceable>. It must return
215 <constant>1</constant> and set the <parameter>ret_found</parameter> output parameter if the
216 object is found, return <constant>0</constant> if the object was not found, and return a
217 negative errno-style error code or initialize the error structure
218 <replaceable>ret_error</replaceable> on error. The pointer passed in
219 <parameter>ret_found</parameter> will be used as the <parameter>userdata</parameter> parameter
220 for the callback functions (offset by the <parameter>offset</parameter> offsets as specified in
221 the vtable entries).</para>
222
223 <para>For both functions, a match slot is created internally. If the output parameter
224 <replaceable>slot</replaceable> is <constant>NULL</constant>, a "floating" slot object is
225 created, see
226 <citerefentry><refentrytitle>sd_bus_slot_set_floating</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
227 Otherwise, a pointer to the slot object is returned. In that case, the reference to the slot
228 object should be dropped when the vtable is not needed anymore, see
229 <citerefentry><refentrytitle>sd_bus_slot_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
230 </para>
231
232 <refsect2>
233 <title>The <structname>sd_bus_vtable</structname> array</title>
234
235 <para>The array consists of the structures of type <structname>sd_bus_vtable</structname>, but it
236 should never be filled in manually, but through one of the following macros:</para>
237
238 <variablelist>
239 <varlistentry>
240 <term><constant>SD_BUS_VTABLE_START()</constant></term>
241 <term><constant>SD_BUS_VTABLE_END</constant></term>
242
243 <listitem><para>Those must always be the first and last element.</para></listitem>
244 </varlistentry>
245
246 <varlistentry>
247 <term><constant>SD_BUS_METHOD_WITH_NAMES_OFFSET()</constant></term>
248 <term><constant>SD_BUS_METHOD_WITH_NAMES()</constant></term>
249 <term><constant>SD_BUS_METHOD_WITH_OFFSET()</constant></term>
250 <term><constant>SD_BUS_METHOD()</constant></term>
251
252 <listitem><para>Declare a D-Bus method with the name <replaceable>member</replaceable>,
253 parameter signature <replaceable>signature</replaceable>, result signature
254 <replaceable>result</replaceable>. Parameters <replaceable>in_names</replaceable> and
255 <replaceable>out_names</replaceable> specify the argument names of the input and output
256 arguments in the function signature. The handler function
257 <replaceable>handler</replaceable> must be of type
258 <function>sd_bus_message_handler_t</function>. It will be called to handle the incoming
259 messages that call this method. It receives a pointer that is the
260 <replaceable>userdata</replaceable> parameter passed to the registration function offset
261 by <replaceable>offset</replaceable> bytes. This may be used to pass pointers to different
262 fields in the same data structure to different methods in the same vtable.
263 <replaceable>in_names</replaceable> and <replaceable>out_names</replaceable> should be
264 created using the <constant>SD_BUS_PARAM()</constant> macro, see below. Parameter
265 <replaceable>flags</replaceable> is a combination of flags, see below.</para>
266
267 <para><constant>SD_BUS_METHOD_WITH_NAMES()</constant>,
268 <constant>SD_BUS_METHOD_WITH_OFFSET()</constant>, and <constant>SD_BUS_METHOD()</constant>
269 are variants which specify zero offset (<replaceable>userdata</replaceable> parameter is
270 passed with no change), leave the names unset (i.e. no parameter names), or both.</para>
271 </listitem>
272 </varlistentry>
273
274 <varlistentry>
275 <term><constant>SD_BUS_SIGNAL_WITH_NAMES()</constant></term>
276 <term><constant>SD_BUS_SIGNAL()</constant></term>
277
278 <listitem><para>Declare a D-Bus signal with the name <replaceable>member</replaceable>,
279 parameter signature <replaceable>signature</replaceable>, and argument names
280 <replaceable>names</replaceable>. <replaceable>names</replaceable> should be
281 created using the <constant>SD_BUS_PARAM()</constant> macro, see below.
282 Parameter <replaceable>flags</replaceable> is a combination of flags, see below.
283 </para>
284
285 <para>Equivalent to <constant>SD_BUS_SIGNAL_WITH_NAMES()</constant> with the
286 <replaceable>names</replaceable> parameter unset (i.e. no parameter names).</para>
287 </listitem>
288 </varlistentry>
289
290 <varlistentry>
291 <term><constant>SD_BUS_WRITABLE_PROPERTY()</constant></term>
292 <term><constant>SD_BUS_PROPERTY()</constant></term>
293
294 <listitem><para>Declare a D-Bus property with the name <replaceable>member</replaceable>
295 and value signature <replaceable>signature</replaceable>. Parameters
296 <replaceable>get</replaceable> and <replaceable>set</replaceable> are the getter and
297 setter methods. They are called with a pointer that is the
298 <replaceable>userdata</replaceable> parameter passed to the registration function offset
299 by <replaceable>offset</replaceable> bytes. This may be used pass pointers to different
300 fields in the same data structure to different setters and getters in the same vtable.
301 Parameter <replaceable>flags</replaceable> is a combination of flags, see below.</para>
302
303 <para>The setter and getter methods may be omitted (specified as
304 <constant>NULL</constant>), if the property has one of the basic types or
305 <literal>as</literal> in case of read-only properties. In those cases, the
306 <replaceable>userdata</replaceable> and <replaceable>offset</replaceable> parameters must
307 together point to valid variable of the corresponding type. A default setter and getters
308 will be provided, which simply copy the argument between this variable and the message.
309 </para>
310
311 <para><constant>SD_BUS_PROPERTY()</constant> is used to define a read-only property.
312 </para></listitem>
313 </varlistentry>
314
315 <varlistentry>
316 <term><constant>SD_BUS_PARAM()</constant></term>
317 <listitem><para>Parameter names should be wrapped in this macro, see the example below.
318 </para></listitem>
319 </varlistentry>
320 </variablelist>
321 </refsect2>
322
323 <refsect2>
324 <title>Flags</title>
325
326 <para>The <replaceable>flags</replaceable> parameter is used to specify a combination of
327 <ulink url="https://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format">D-Bus annotations</ulink>.
328 </para>
329
330 <variablelist>
331 <varlistentry>
332 <term><constant>SD_BUS_VTABLE_DEPRECATED</constant></term>
333
334 <listitem><para>Mark this vtable entry as deprecated using the
335 <constant>org.freedesktop.DBus.Deprecated</constant> annotation in introspection data. If
336 specified for <constant>SD_BUS_VTABLE_START()</constant>, the annotation is applied to the
337 enclosing interface.</para></listitem>
338 </varlistentry>
339
340 <varlistentry>
341 <term><constant>SD_BUS_VTABLE_HIDDEN</constant></term>
342
343 <listitem><para>Make this vtable entry hidden. It will not be shown in introspection data.
344 If specified for <constant>SD_BUS_VTABLE_START()</constant>, all entries in the array are
345 hidden.</para></listitem>
346 </varlistentry>
347
348 <varlistentry>
349 <term><constant>SD_BUS_VTABLE_UNPRIVILEGED</constant></term>
350
351 <listitem><para>Mark this vtable entry as unprivileged. If not specified, the
352 <constant>org.freedesktop.systemd1.Privileged</constant> annotation with value
353 <literal>true</literal> will be shown in introspection data.</para></listitem>
354 </varlistentry>
355
356 <varlistentry>
357 <term><constant>SD_BUS_VTABLE_METHOD_NO_REPLY</constant></term>
358
359 <listitem><para>Mark his vtable entry as a method that will not return a reply using the
360 <constant>org.freedesktop.DBus.Method.NoReply</constant> annotation in introspection data.
361 </para></listitem>
362 </varlistentry>
363
364 <varlistentry>
365 <term><constant>SD_BUS_VTABLE_PROPERTY_CONST</constant></term>
366 <term><constant>SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE</constant></term>
367 <term><constant>SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION</constant></term>
368
369 <listitem><para>Those three flags correspond to different values of the
370 <constant>org.freedesktop.DBus.Property.EmitsChangedSignal</constant> annotation, which
371 specifies whether the
372 <constant>org.freedesktop.DBus.Properties.PropertiesChanged</constant> signal is emitted
373 whenever the property changes. <constant>SD_BUS_VTABLE_PROPERTY_CONST</constant>
374 corresponds to <constant>const</constant> and means that the property never changes during
375 the lifetime of the object it belongs to, so no signal needs to be emitted.
376 <constant>SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE</constant> corresponds to
377 <constant>true</constant> and means that the signal is emitted.
378 <constant>SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION</constant> corresponds to
379 <constant>invalidates</constant> and means that the signal is emitted, but the value is
380 not included in the signal.</para></listitem>
381 </varlistentry>
382
383 <varlistentry>
384 <term><constant>SD_BUS_VTABLE_PROPERTY_EXPLICIT</constant></term>
385
386 <listitem><para>Mark this vtable property entry as requiring explicit request to for the
387 value to be shown (generally because the value is large or slow to calculate). This entry
388 cannot be combined with <constant>SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE</constant>, and will
389 not be shown in property listings by default (e.g. <command>busctl introspect</command>).
390 This corresponds to the <constant>org.freedesktop.systemd1.Explicit</constant> annotation
391 in introspection data.</para></listitem>
392 </varlistentry>
393 </variablelist>
394 </refsect2>
395 </refsect1>
396
397 <refsect1>
398 <title>Examples</title>
399
400 <example>
401 <title>Create a simple listener on the bus</title>
402
403 <programlisting><xi:include href="vtable-example.c" parse="text" /></programlisting>
404
405 <para>This creates a simple client on the bus (the user bus, when run as normal user). We may
406 use the D-Bus <constant>org.freedesktop.DBus.Introspectable.Introspect</constant> call to
407 acquire the XML description of the interface:</para>
408
409 <programlisting><xi:include href="vtable-example.xml" parse="text" /></programlisting>
410 </example>
411 </refsect1>
412
413 <refsect1>
414 <title>Return Value</title>
415
416 <para>On success, <function>sd_bus_add_object_vtable</function> and
417 <function>sd_bus_add_fallback_vtable</function> calls return 0 or a positive integer. On
418 failure, they return a negative errno-style error code.</para>
419
420 <refsect2>
421 <title>Errors</title>
422
423 <para>Returned errors may indicate the following problems:</para>
424
425 <variablelist>
426 <varlistentry>
427 <term><constant>-EINVAL</constant></term>
428
429 <listitem><para>One of the required parameters is <constant>NULL</constant> or invalid. A
430 reserved D-Bus interface was passed as the <replaceable>interface</replaceable> parameter.
431 </para></listitem>
432 </varlistentry>
433
434 <varlistentry>
435 <term><constant>-ENOPKG</constant></term>
436
437 <listitem><para>The bus cannot be resolved.</para></listitem>
438 </varlistentry>
439
440 <varlistentry>
441 <term><constant>-ECHILD</constant></term>
442
443 <listitem><para>The bus was created in a different process.</para></listitem>
444 </varlistentry>
445
446 <varlistentry>
447 <term><constant>-ENOMEM</constant></term>
448
449 <listitem><para>Memory allocation failed.</para></listitem>
450 </varlistentry>
451
452 <varlistentry>
453 <term><constant>-EPROTOTYPE</constant></term>
454
455 <listitem><para><function>sd_bus_add_object_vtable</function> and
456 <function>sd_bus_add_fallback_vtable</function> have been both called for the same bus
457 object path, which is not allowed.</para></listitem>
458 </varlistentry>
459
460 <varlistentry>
461 <term><constant>-EEXIST</constant></term>
462
463 <listitem><para>This vtable has already been registered for this
464 <replaceable>interface</replaceable> and <replaceable>path</replaceable>.
465 </para></listitem>
466 </varlistentry>
467 </variablelist>
468 </refsect2>
469 </refsect1>
470
471 <xi:include href="libsystemd-pkgconfig.xml" />
472
473 <refsect1>
474 <title>See Also</title>
475
476 <para>
477 <citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
478 <citerefentry><refentrytitle>busctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
479 </para>
480 </refsect1>
481 </refentry>