1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "sd-bus-protocol.h"
24 #include "bus-introspect.h"
25 #include "bus-signature.h"
26 #include "bus-internal.h"
28 int introspect_begin(struct introspect *i) {
33 i->f = open_memstream(&i->introspection, &i->size);
37 fputs(SD_BUS_INTROSPECT_DOCTYPE
43 int introspect_write_default_interfaces(struct introspect *i, bool object_manager) {
46 fputs(SD_BUS_INTROSPECT_INTERFACE_PEER
47 SD_BUS_INTROSPECT_INTERFACE_INTROSPECTABLE
48 SD_BUS_INTROSPECT_INTERFACE_PROPERTIES, i->f);
51 fputs(SD_BUS_INTROSPECT_INTERFACE_OBJECT_MANAGER, i->f);
56 int introspect_write_child_nodes(struct introspect *i, Set *s, const char *prefix) {
62 while ((node = set_steal_first(s))) {
65 e = object_path_startswith(node, prefix);
67 fprintf(i->f, " <node name=\"%s\"/>\n", e);
75 static void introspect_write_flags(struct introspect *i, int type, int flags) {
76 if (flags & SD_BUS_VTABLE_DEPRECATED)
77 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i->f);
79 if (type == _SD_BUS_VTABLE_METHOD && flags & SD_BUS_VTABLE_METHOD_NO_REPLY)
80 fputs(" <annotation name=\"org.freedesktop.DBus.Method.NoReply\" value=\"true\"/>\n", i->f);
82 if (type == _SD_BUS_VTABLE_PROPERTY || type == _SD_BUS_VTABLE_WRITABLE_PROPERTY) {
83 if (!(flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))
84 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"false\"/>\n", i->f);
85 else if (flags & SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY)
86 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"invalidates\"/>\n", i->f);
90 static int introspect_write_arguments(struct introspect *i, const char *signature, const char *direction) {
99 r = signature_element_length(signature, &l);
103 fprintf(i->f, " <arg type=\"%.*s\"", (int) l, signature);
106 fprintf(i->f, " direction=\"%s\"/>\n", direction);
114 int introspect_write_interface(struct introspect *i, const char *interface, const sd_bus_vtable *v) {
119 fprintf(i->f, " <interface name=\"%s\">\n", interface);
121 for (; v->type != _SD_BUS_VTABLE_END; v++) {
125 case _SD_BUS_VTABLE_START:
126 if (v->flags & SD_BUS_VTABLE_DEPRECATED)
127 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i->f);
130 case _SD_BUS_VTABLE_METHOD:
131 fprintf(i->f, " <method name=\"%s\">\n", v->x.method.member);
132 introspect_write_arguments(i, strempty(v->x.method.signature), "in");
133 introspect_write_arguments(i, strempty(v->x.method.result), "out");
134 introspect_write_flags(i, v->type, v->flags);
135 fputs(" </method>\n", i->f);
138 case _SD_BUS_VTABLE_PROPERTY:
139 case _SD_BUS_VTABLE_WRITABLE_PROPERTY:
140 fprintf(i->f, " <property name=\"%s\" type=\"%s\" access=\"%s\">\n",
141 v->x.property.member,
142 v->x.property.signature,
143 v->type == _SD_BUS_VTABLE_WRITABLE_PROPERTY ? "readwrite" : "read");
144 introspect_write_flags(i, v->type, v->flags);
145 fputs(" </property>\n", i->f);
148 case _SD_BUS_VTABLE_SIGNAL:
149 fprintf(i->f, " <signal name=\"%s\">\n", v->x.signal.member);
150 introspect_write_arguments(i, strempty(v->x.signal.signature), NULL);
151 introspect_write_flags(i, v->type, v->flags);
152 fputs(" </signal>\n", i->f);
158 fputs(" </interface>\n", i->f);
162 int introspect_finish(struct introspect *i, sd_bus *bus, sd_bus_message *m, sd_bus_message **reply) {
170 fputs("</node>\n", i->f);
176 r = sd_bus_message_new_method_return(bus, m, &q);
180 r = sd_bus_message_append(q, "s", i->introspection);
182 sd_bus_message_unref(q);
190 void introspect_free(struct introspect *i) {
196 if (i->introspection)
197 free(i->introspection);