2 * wpa_supplicant - D-Bus introspection
3 * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
4 * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
5 * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Alternatively, this software may be distributed under the terms of BSD
14 * See README and COPYING for more details.
17 #include "utils/includes.h"
19 #include "utils/common.h"
20 #include "utils/list.h"
21 #include "utils/wpabuf.h"
22 #include "dbus_common_i.h"
23 #include "dbus_new_helpers.h"
33 static struct interfaces
* add_interface(struct dl_list
*list
,
34 const char *dbus_interface
)
36 struct interfaces
*iface
;
38 dl_list_for_each(iface
, list
, struct interfaces
, list
) {
39 if (os_strcmp(iface
->dbus_interface
, dbus_interface
) == 0)
40 return iface
; /* already in the list */
43 iface
= os_zalloc(sizeof(struct interfaces
));
46 iface
->xml
= wpabuf_alloc(3000);
47 if (iface
->xml
== NULL
) {
51 wpabuf_printf(iface
->xml
, "<interface name=\"%s\">", dbus_interface
);
52 dl_list_add_tail(list
, &iface
->list
);
53 iface
->dbus_interface
= os_strdup(dbus_interface
);
58 static void add_arg(struct wpabuf
*xml
, const char *name
, const char *type
,
59 const char *direction
)
61 wpabuf_printf(xml
, "<arg name=\"%s\"", name
);
63 wpabuf_printf(xml
, " type=\"%s\"", type
);
65 wpabuf_printf(xml
, " direction=\"%s\"", direction
);
66 wpabuf_put_str(xml
, "/>");
70 static void add_entry(struct wpabuf
*xml
, const char *type
, const char *name
,
71 const struct wpa_dbus_argument
*args
, int include_dir
)
73 const struct wpa_dbus_argument
*arg
;
75 if (args
== NULL
|| args
->name
== NULL
) {
76 wpabuf_printf(xml
, "<%s name=\"%s\"/>", type
, name
);
79 wpabuf_printf(xml
, "<%s name=\"%s\">", type
, name
);
80 for (arg
= args
; arg
&& arg
->name
; arg
++) {
81 add_arg(xml
, arg
->name
, arg
->type
,
82 include_dir
? (arg
->dir
== ARG_IN
? "in" : "out") :
85 wpabuf_printf(xml
, "</%s>", type
);
89 static void add_property(struct wpabuf
*xml
,
90 const struct wpa_dbus_property_desc
*dsc
)
92 wpabuf_printf(xml
, "<property name=\"%s\" type=\"%s\" access=\"%s\"/>",
93 dsc
->dbus_property
, dsc
->type
,
94 (dsc
->access
== R
? "read" :
95 (dsc
->access
== W
? "write" : "readwrite")));
99 static void extract_interfaces_methods(
100 struct dl_list
*list
, const struct wpa_dbus_method_desc
*methods
)
102 const struct wpa_dbus_method_desc
*dsc
;
103 struct interfaces
*iface
;
104 for (dsc
= methods
; dsc
&& dsc
->dbus_method
; dsc
++) {
105 iface
= add_interface(list
, dsc
->dbus_interface
);
107 add_entry(iface
->xml
, "method", dsc
->dbus_method
,
113 static void extract_interfaces_signals(
114 struct dl_list
*list
, const struct wpa_dbus_signal_desc
*signals
)
116 const struct wpa_dbus_signal_desc
*dsc
;
117 struct interfaces
*iface
;
118 for (dsc
= signals
; dsc
&& dsc
->dbus_signal
; dsc
++) {
119 iface
= add_interface(list
, dsc
->dbus_interface
);
121 add_entry(iface
->xml
, "signal", dsc
->dbus_signal
,
127 static void extract_interfaces_properties(
128 struct dl_list
*list
, const struct wpa_dbus_property_desc
*properties
)
130 const struct wpa_dbus_property_desc
*dsc
;
131 struct interfaces
*iface
;
132 for (dsc
= properties
; dsc
&& dsc
->dbus_property
; dsc
++) {
133 iface
= add_interface(list
, dsc
->dbus_interface
);
135 add_property(iface
->xml
, dsc
);
141 * extract_interfaces - Extract interfaces from methods, signals and props
142 * @list: Interface list to be filled
143 * @obj_dsc: Description of object from which interfaces will be extracted
145 * Iterates over all methods, signals, and properties registered with an
146 * object and collects all declared DBus interfaces and create interfaces'
147 * node in XML root node for each. Returned list elements contain interface
148 * name and XML node of corresponding interface.
150 static void extract_interfaces(struct dl_list
*list
,
151 struct wpa_dbus_object_desc
*obj_dsc
)
153 extract_interfaces_methods(list
, obj_dsc
->methods
);
154 extract_interfaces_signals(list
, obj_dsc
->signals
);
155 extract_interfaces_properties(list
, obj_dsc
->properties
);
159 static void add_interfaces(struct dl_list
*list
, struct wpabuf
*xml
)
161 struct interfaces
*iface
, *n
;
162 dl_list_for_each_safe(iface
, n
, list
, struct interfaces
, list
) {
163 if (wpabuf_len(iface
->xml
) + 20 < wpabuf_tailroom(xml
)) {
164 wpabuf_put_buf(xml
, iface
->xml
);
165 wpabuf_put_str(xml
, "</interface>");
167 dl_list_del(&iface
->list
);
168 wpabuf_free(iface
->xml
);
169 os_free(iface
->dbus_interface
);
175 static void add_child_nodes(struct wpabuf
*xml
, DBusConnection
*con
,
181 /* add child nodes to introspection tree */
182 dbus_connection_list_registered(con
, path
, &children
);
183 for (i
= 0; children
[i
]; i
++)
184 wpabuf_printf(xml
, "<node name=\"%s\"/>", children
[i
]);
185 dbus_free_string_array(children
);
189 static void add_introspectable_interface(struct wpabuf
*xml
)
191 wpabuf_printf(xml
, "<interface name=\"%s\">"
192 "<method name=\"%s\">"
193 "<arg name=\"data\" type=\"s\" direction=\"out\"/>"
196 WPA_DBUS_INTROSPECTION_INTERFACE
,
197 WPA_DBUS_INTROSPECTION_METHOD
);
201 static void add_properties_interface(struct wpabuf
*xml
)
203 wpabuf_printf(xml
, "<interface name=\"%s\">",
204 WPA_DBUS_PROPERTIES_INTERFACE
);
206 wpabuf_printf(xml
, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_GET
);
207 add_arg(xml
, "interface", "s", "in");
208 add_arg(xml
, "propname", "s", "in");
209 add_arg(xml
, "value", "v", "out");
210 wpabuf_put_str(xml
, "</method>");
212 wpabuf_printf(xml
, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_GETALL
);
213 add_arg(xml
, "interface", "s", "in");
214 add_arg(xml
, "props", "a{sv}", "out");
215 wpabuf_put_str(xml
, "</method>");
217 wpabuf_printf(xml
, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_SET
);
218 add_arg(xml
, "interface", "s", "in");
219 add_arg(xml
, "propname", "s", "in");
220 add_arg(xml
, "value", "v", "in");
221 wpabuf_put_str(xml
, "</method>");
223 wpabuf_put_str(xml
, "</interface>");
227 static void add_wpas_interfaces(struct wpabuf
*xml
,
228 struct wpa_dbus_object_desc
*obj_dsc
)
230 struct dl_list ifaces
;
231 dl_list_init(&ifaces
);
232 extract_interfaces(&ifaces
, obj_dsc
);
233 add_interfaces(&ifaces
, xml
);
238 * wpa_dbus_introspect - Responds for Introspect calls on object
239 * @message: Message with Introspect call
240 * @obj_dsc: Object description on which Introspect was called
241 * Returns: Message with introspection result XML string as only argument
243 * Iterates over all methods, signals and properties registered with
244 * object and generates introspection data for the object as XML string.
246 DBusMessage
* wpa_dbus_introspect(DBusMessage
*message
,
247 struct wpa_dbus_object_desc
*obj_dsc
)
253 xml
= wpabuf_alloc(4000);
257 wpabuf_put_str(xml
, "<?xml version=\"1.0\"?>\n");
258 wpabuf_put_str(xml
, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
);
259 wpabuf_put_str(xml
, "<node>");
261 add_introspectable_interface(xml
);
262 add_properties_interface(xml
);
263 add_wpas_interfaces(xml
, obj_dsc
);
264 add_child_nodes(xml
, obj_dsc
->connection
,
265 dbus_message_get_path(message
));
267 wpabuf_put_str(xml
, "</node>\n");
269 reply
= dbus_message_new_method_return(message
);
271 const char *intro_str
= wpabuf_head(xml
);
272 dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &intro_str
,