D-Bus: Move NetworkRequest signal to correct registration array
[mech_eap.git] / wpa_supplicant / dbus / dbus_new_introspect.c
1 /*
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>
6  *
7  * This software may be distributed under the terms of the BSD license.
8  * See README for more details.
9  */
10
11 #include "utils/includes.h"
12
13 #include "utils/common.h"
14 #include "utils/list.h"
15 #include "utils/wpabuf.h"
16 #include "dbus_common_i.h"
17 #include "dbus_new_helpers.h"
18
19
20 struct interfaces {
21         struct dl_list list;
22         char *dbus_interface;
23         struct wpabuf *xml;
24 };
25
26
27 static struct interfaces * add_interface(struct dl_list *list,
28                                          const char *dbus_interface)
29 {
30         struct interfaces *iface;
31
32         dl_list_for_each(iface, list, struct interfaces, list) {
33                 if (os_strcmp(iface->dbus_interface, dbus_interface) == 0)
34                         return iface; /* already in the list */
35         }
36
37         iface = os_zalloc(sizeof(struct interfaces));
38         if (!iface)
39                 return NULL;
40         iface->xml = wpabuf_alloc(6000);
41         if (iface->xml == NULL) {
42                 os_free(iface);
43                 return NULL;
44         }
45         wpabuf_printf(iface->xml, "<interface name=\"%s\">", dbus_interface);
46         dl_list_add_tail(list, &iface->list);
47         iface->dbus_interface = os_strdup(dbus_interface);
48         return iface;
49 }
50
51
52 static void add_arg(struct wpabuf *xml, const char *name, const char *type,
53                     const char *direction)
54 {
55         wpabuf_printf(xml, "<arg name=\"%s\"", name);
56         if (type)
57                 wpabuf_printf(xml, " type=\"%s\"", type);
58         if (direction)
59                 wpabuf_printf(xml, " direction=\"%s\"", direction);
60         wpabuf_put_str(xml, "/>");
61 }
62
63
64 static void add_entry(struct wpabuf *xml, const char *type, const char *name,
65                       const struct wpa_dbus_argument *args, int include_dir)
66 {
67         const struct wpa_dbus_argument *arg;
68
69         if (args == NULL || args->name == NULL) {
70                 wpabuf_printf(xml, "<%s name=\"%s\"/>", type, name);
71                 return;
72         }
73         wpabuf_printf(xml, "<%s name=\"%s\">", type, name);
74         for (arg = args; arg && arg->name; arg++) {
75                 add_arg(xml, arg->name, arg->type,
76                         include_dir ? (arg->dir == ARG_IN ? "in" : "out") :
77                         NULL);
78         }
79         wpabuf_printf(xml, "</%s>", type);
80 }
81
82
83 static void add_property(struct wpabuf *xml,
84                          const struct wpa_dbus_property_desc *dsc)
85 {
86         wpabuf_printf(xml, "<property name=\"%s\" type=\"%s\" "
87                       "access=\"%s%s\"/>",
88                       dsc->dbus_property, dsc->type,
89                       dsc->getter ? "read" : "",
90                       dsc->setter ? "write" : "");
91 }
92
93
94 static void extract_interfaces_methods(
95         struct dl_list *list, const struct wpa_dbus_method_desc *methods)
96 {
97         const struct wpa_dbus_method_desc *dsc;
98         struct interfaces *iface;
99
100         for (dsc = methods; dsc && dsc->dbus_method; dsc++) {
101                 iface = add_interface(list, dsc->dbus_interface);
102                 if (iface)
103                         add_entry(iface->xml, "method", dsc->dbus_method,
104                                   dsc->args, 1);
105         }
106 }
107
108
109 static void extract_interfaces_signals(
110         struct dl_list *list, const struct wpa_dbus_signal_desc *signals)
111 {
112         const struct wpa_dbus_signal_desc *dsc;
113         struct interfaces *iface;
114
115         for (dsc = signals; dsc && dsc->dbus_signal; dsc++) {
116                 iface = add_interface(list, dsc->dbus_interface);
117                 if (iface)
118                         add_entry(iface->xml, "signal", dsc->dbus_signal,
119                                   dsc->args, 0);
120         }
121 }
122
123
124 static void extract_interfaces_properties(
125         struct dl_list *list, const struct wpa_dbus_property_desc *properties)
126 {
127         const struct wpa_dbus_property_desc *dsc;
128         struct interfaces *iface;
129
130         for (dsc = properties; dsc && dsc->dbus_property; dsc++) {
131                 iface = add_interface(list, dsc->dbus_interface);
132                 if (iface)
133                         add_property(iface->xml, dsc);
134         }
135 }
136
137
138 /**
139  * extract_interfaces - Extract interfaces from methods, signals and props
140  * @list: Interface list to be filled
141  * @obj_dsc: Description of object from which interfaces will be extracted
142  *
143  * Iterates over all methods, signals, and properties registered with an
144  * object and collects all declared DBus interfaces and create interfaces'
145  * node in XML root node for each. Returned list elements contain interface
146  * name and XML node of corresponding interface.
147  */
148 static void extract_interfaces(struct dl_list *list,
149                                struct wpa_dbus_object_desc *obj_dsc)
150 {
151         extract_interfaces_methods(list, obj_dsc->methods);
152         extract_interfaces_signals(list, obj_dsc->signals);
153         extract_interfaces_properties(list, obj_dsc->properties);
154 }
155
156
157 static void add_interfaces(struct dl_list *list, struct wpabuf *xml)
158 {
159         struct interfaces *iface, *n;
160
161         dl_list_for_each_safe(iface, n, list, struct interfaces, list) {
162                 if (wpabuf_len(iface->xml) + 20 < wpabuf_tailroom(xml)) {
163                         wpabuf_put_buf(xml, iface->xml);
164                         wpabuf_put_str(xml, "</interface>");
165                 } else {
166                         wpa_printf(MSG_DEBUG,
167                                    "dbus: Not enough room for add_interfaces inspect data: tailroom %u, add %u",
168                                    (unsigned int) wpabuf_tailroom(xml),
169                                    (unsigned int) wpabuf_len(iface->xml));
170                 }
171                 dl_list_del(&iface->list);
172                 wpabuf_free(iface->xml);
173                 os_free(iface->dbus_interface);
174                 os_free(iface);
175         }
176 }
177
178
179 static void add_child_nodes(struct wpabuf *xml, DBusConnection *con,
180                             const char *path)
181 {
182         char **children;
183         int i;
184
185         /* add child nodes to introspection tree */
186         dbus_connection_list_registered(con, path, &children);
187         for (i = 0; children[i]; i++)
188                 wpabuf_printf(xml, "<node name=\"%s\"/>", children[i]);
189         dbus_free_string_array(children);
190 }
191
192
193 static void add_introspectable_interface(struct wpabuf *xml)
194 {
195         wpabuf_printf(xml, "<interface name=\"%s\">"
196                       "<method name=\"%s\">"
197                       "<arg name=\"data\" type=\"s\" direction=\"out\"/>"
198                       "</method>"
199                       "</interface>",
200                       WPA_DBUS_INTROSPECTION_INTERFACE,
201                       WPA_DBUS_INTROSPECTION_METHOD);
202 }
203
204
205 static void add_properties_interface(struct wpabuf *xml)
206 {
207         wpabuf_printf(xml, "<interface name=\"%s\">",
208                       WPA_DBUS_PROPERTIES_INTERFACE);
209
210         wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_GET);
211         add_arg(xml, "interface", "s", "in");
212         add_arg(xml, "propname", "s", "in");
213         add_arg(xml, "value", "v", "out");
214         wpabuf_put_str(xml, "</method>");
215
216         wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_GETALL);
217         add_arg(xml, "interface", "s", "in");
218         add_arg(xml, "props", "a{sv}", "out");
219         wpabuf_put_str(xml, "</method>");
220
221         wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_SET);
222         add_arg(xml, "interface", "s", "in");
223         add_arg(xml, "propname", "s", "in");
224         add_arg(xml, "value", "v", "in");
225         wpabuf_put_str(xml, "</method>");
226
227         wpabuf_put_str(xml, "</interface>");
228 }
229
230
231 static void add_wpas_interfaces(struct wpabuf *xml,
232                                 struct wpa_dbus_object_desc *obj_dsc)
233 {
234         struct dl_list ifaces;
235
236         dl_list_init(&ifaces);
237         extract_interfaces(&ifaces, obj_dsc);
238         add_interfaces(&ifaces, xml);
239 }
240
241
242 /**
243  * wpa_dbus_introspect - Responds for Introspect calls on object
244  * @message: Message with Introspect call
245  * @obj_dsc: Object description on which Introspect was called
246  * Returns: Message with introspection result XML string as only argument
247  *
248  * Iterates over all methods, signals and properties registered with
249  * object and generates introspection data for the object as XML string.
250  */
251 DBusMessage * wpa_dbus_introspect(DBusMessage *message,
252                                   struct wpa_dbus_object_desc *obj_dsc)
253 {
254
255         DBusMessage *reply;
256         struct wpabuf *xml;
257
258         xml = wpabuf_alloc(10000);
259         if (xml == NULL)
260                 return NULL;
261
262         wpabuf_put_str(xml, "<?xml version=\"1.0\"?>\n");
263         wpabuf_put_str(xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
264         wpabuf_put_str(xml, "<node>");
265
266         add_introspectable_interface(xml);
267         add_properties_interface(xml);
268         add_wpas_interfaces(xml, obj_dsc);
269         add_child_nodes(xml, obj_dsc->connection,
270                         dbus_message_get_path(message));
271
272         wpabuf_put_str(xml, "</node>\n");
273
274         reply = dbus_message_new_method_return(message);
275         if (reply) {
276                 const char *intro_str = wpabuf_head(xml);
277
278                 dbus_message_append_args(reply, DBUS_TYPE_STRING, &intro_str,
279                                          DBUS_TYPE_INVALID);
280         }
281         wpabuf_free(xml);
282
283         return reply;
284 }