dbus: Rename old D-Bus API files to include "_old"
[hostap-gosc2009.git] / wpa_supplicant / dbus / dbus_new_helpers.c
blob4c0ba65253854e9c9c937af977777eaee845805f
1 /*
2 * WPA Supplicant / dbus-based control interface
3 * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
4 * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
11 * license.
13 * See README and COPYING for more details.
16 #include "includes.h"
18 #include "common.h"
19 #include "eloop.h"
20 #include "dbus_common.h"
21 #include "dbus_common_i.h"
22 #include "dbus_new_helpers.h"
24 /**
25 * struct wpa_dbus_method_desc - DBus method description
27 struct wpa_dbus_method_desc {
28 /* pointer to next description in list */
29 struct wpa_dbus_method_desc *next;
31 /* method interface */
32 char *dbus_interface;
33 /* method name */
34 char *dbus_method;
36 /* method handling function */
37 WPADBusMethodHandler method_handler;
38 /* handler function argument */
39 void *handler_argument;
40 /* function used to free handler argument */
41 WPADBusArgumentFreeFunction argument_free_func;
43 /* number of method arguments */
44 int args_num;
45 /* array of arguments */
46 struct wpa_dbus_argument args[];
50 /**
51 * struct wpa_dbus_signal_desc - DBus signal description
53 struct wpa_dbus_signal_desc {
54 /* pointer to next description in list */
55 struct wpa_dbus_signal_desc *next;
57 /* signal interface */
58 char *dbus_interface;
59 /* signal name */
60 char *dbus_signal;
62 /* number of signal arguments */
63 int args_num;
64 /* array of arguments */
65 struct wpa_dbus_argument args[0];
69 /**
70 * struct wpa_dbus_property_desc - DBus property description
72 struct wpa_dbus_property_desc {
73 /* pointer to next description in list */
74 struct wpa_dbus_property_desc *next;
76 /* property interface */
77 char *dbus_interface;
78 /* property name */
79 char *dbus_property;
80 /* property type signature in DBus type notation */
81 char *type;
83 /* property access permissions */
84 enum dbus_prop_access access;
86 /* property getter function */
87 WPADBusPropertyAccessor getter;
88 /* property setter function */
89 WPADBusPropertyAccessor setter;
90 /* argument for getter and setter functions */
91 void *user_data;
92 /* function used to free accessors argument */
93 WPADBusArgumentFreeFunction user_data_free_func;
97 #ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
98 #include <libxml/tree.h>
100 struct interfaces {
101 struct interfaces *next;
102 char *dbus_interface;
103 xmlNodePtr interface_node;
105 #endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
108 #ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
111 * extract_interfaces - Extract interfaces from methods, signals and props
112 * @obj_dsc: Description of object from which interfaces will be extracted
113 * @root_node: root node of XML introspection document
114 * Returns: List of interfaces found in object description
116 * Iterates over all methods, signals and properties registered with
117 * object and collects all declared DBus interfaces and create interface's
118 * node in XML root node for each. Returned list elements contains interface
119 * name and XML node of corresponding interface.
121 static struct interfaces * extract_interfaces(
122 struct wpa_dbus_object_desc *obj_dsc, xmlNodePtr root_node)
124 struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
125 struct wpa_dbus_signal_desc *signal_dsc = obj_dsc->signals;
126 struct wpa_dbus_property_desc *property_dsc = obj_dsc->properties;
127 struct interfaces *head = NULL;
128 struct interfaces *iface, *last;
129 int len;
131 /* extract interfaces from methods */
132 while (method_dsc) {
133 iface = head;
134 last = NULL;
136 /* go to next method if its interface is already extracted */
137 while (iface) {
138 if (!os_strcmp(iface->dbus_interface,
139 method_dsc->dbus_interface))
140 break;
141 last = iface;
142 iface = iface->next;
144 if (iface) {
145 method_dsc = method_dsc->next;
146 continue;
149 iface = os_zalloc(sizeof(struct interfaces));
150 if (!iface) {
151 wpa_printf(MSG_ERROR, "Not enough memory to create "
152 "interface introspection data");
153 method_dsc = method_dsc->next;
154 continue;
157 if (last)
158 last->next = iface;
159 else
160 head = iface;
162 len = os_strlen(method_dsc->dbus_interface) + 1;
163 iface->dbus_interface = os_malloc(len);
164 if (!iface->dbus_interface) {
165 wpa_printf(MSG_ERROR, "Not enough memory to create "
166 "interface introspection data (interface "
167 "name)");
168 method_dsc = method_dsc->next;
169 continue;
171 os_strncpy(iface->dbus_interface, method_dsc->dbus_interface,
172 len);
174 iface->interface_node = xmlNewChild(root_node, NULL,
175 BAD_CAST "interface",
176 NULL);
177 xmlNewProp(iface->interface_node, BAD_CAST "name",
178 BAD_CAST method_dsc->dbus_interface);
180 method_dsc = method_dsc->next;
183 /* extract interfaces from signals */
184 while (signal_dsc) {
185 iface = head;
186 last = NULL;
188 /* go to next signal if its interface is already extracted */
189 while (iface) {
190 if (!os_strcmp(iface->dbus_interface,
191 signal_dsc->dbus_interface))
192 break;
193 last = iface;
194 iface = iface->next;
196 if (iface) {
197 signal_dsc = signal_dsc->next;
198 continue;
201 iface = os_zalloc(sizeof(struct interfaces));
202 if (!iface) {
203 wpa_printf(MSG_ERROR, "Not enough memory to create "
204 "interface introspection data");
205 signal_dsc = signal_dsc->next;
206 continue;
209 if (last)
210 last->next = iface;
211 else
212 head = iface;
214 len = os_strlen(signal_dsc->dbus_interface) + 1;
215 iface->dbus_interface = os_malloc(len);
216 if (!iface->dbus_interface) {
217 wpa_printf(MSG_ERROR, "Not enough memory to create "
218 "interface introspection data (interface "
219 "name)");
220 signal_dsc = signal_dsc->next;
221 continue;
223 os_strncpy(iface->dbus_interface, signal_dsc->dbus_interface,
224 len);
226 iface->interface_node = xmlNewChild(root_node, NULL,
227 BAD_CAST "interface",
228 NULL);
229 xmlNewProp(iface->interface_node, BAD_CAST "name",
230 BAD_CAST signal_dsc->dbus_interface);
232 signal_dsc = signal_dsc->next;
235 /* extract interfaces from properties */
236 while (property_dsc) {
237 iface = head;
238 last = NULL;
240 /* go to next property if its interface is already extracted */
241 while (iface) {
242 if (!os_strcmp(iface->dbus_interface,
243 property_dsc->dbus_interface))
244 break;
245 last = iface;
246 iface = iface->next;
248 if (iface) {
249 property_dsc = property_dsc->next;
250 continue;
253 iface = os_zalloc(sizeof(struct interfaces));
254 if (!iface) {
255 wpa_printf(MSG_ERROR, "Not enough memory to create "
256 "interface introspection data");
257 property_dsc = property_dsc->next;
258 continue;
261 if (last)
262 last->next = iface;
263 else
264 head = iface;
266 len = os_strlen(property_dsc->dbus_interface) + 1;
267 iface->dbus_interface = os_malloc(len);
268 if (!iface->dbus_interface) {
269 wpa_printf(MSG_ERROR, "Not enough memory to create "
270 "interface introspection data (interface "
271 "name)");
272 property_dsc = property_dsc->next;
273 continue;
275 os_strncpy(iface->dbus_interface, property_dsc->dbus_interface,
276 len);
278 iface->interface_node = xmlNewChild(root_node, NULL,
279 BAD_CAST "interface",
280 NULL);
281 xmlNewProp(iface->interface_node, BAD_CAST "name",
282 BAD_CAST property_dsc->dbus_interface);
284 property_dsc = property_dsc->next;
287 return head;
292 * introspect - Responds for Introspect calls on object
293 * @message: Message with Introspect call
294 * @obj_dsc: Object description on which Introspect was called
295 * Returns: Message with introspection result XML string as only argument
297 * Iterates over all methods, signals and properties registered with
298 * object and generates introspection data for the object as XML string.
300 static DBusMessage * introspect(DBusMessage *message,
301 struct wpa_dbus_object_desc *obj_dsc)
304 DBusMessage *reply;
305 struct interfaces *ifaces, *tmp;
306 struct wpa_dbus_signal_desc *signal_dsc;
307 struct wpa_dbus_method_desc *method_dsc;
308 struct wpa_dbus_property_desc *property_dsc;
309 xmlChar *intro_str;
310 char **children;
311 int i, s;
313 xmlDocPtr doc = NULL;
314 xmlNodePtr root_node = NULL, node = NULL, iface_node = NULL;
315 xmlNodePtr method_node = NULL, signal_node = NULL;
316 xmlNodePtr property_node = NULL, arg_node = NULL;
318 /* root node and dtd */
319 doc = xmlNewDoc(BAD_CAST "1.0");
320 root_node = xmlNewNode(NULL, BAD_CAST "node");
321 xmlDocSetRootElement(doc, root_node);
322 xmlCreateIntSubset(doc, BAD_CAST "node",
323 BAD_CAST DBUS_INTROSPECT_1_0_XML_PUBLIC_IDENTIFIER,
324 BAD_CAST DBUS_INTROSPECT_1_0_XML_SYSTEM_IDENTIFIER);
326 /* Add Introspectable interface */
327 iface_node = xmlNewChild(root_node, NULL, BAD_CAST "interface", NULL);
328 xmlNewProp(iface_node, BAD_CAST "name",
329 BAD_CAST WPA_DBUS_INTROSPECTION_INTERFACE);
331 /* Add Introspect method */
332 method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
333 xmlNewProp(method_node, BAD_CAST "name",
334 BAD_CAST WPA_DBUS_INTROSPECTION_METHOD);
335 arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
336 xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "data");
337 xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
338 xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
341 /* Add Properties interface */
342 iface_node = xmlNewChild(root_node, NULL,
343 BAD_CAST "interface", NULL);
344 xmlNewProp(iface_node, BAD_CAST "name",
345 BAD_CAST WPA_DBUS_PROPERTIES_INTERFACE);
347 /* Add Get method */
348 method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
349 xmlNewProp(method_node, BAD_CAST "name",
350 BAD_CAST WPA_DBUS_PROPERTIES_GET);
351 arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
352 xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
353 xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
354 xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
355 arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
356 xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "propname");
357 xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
358 xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
359 arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
360 xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "value");
361 xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "v");
362 xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
363 method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
365 /* Add GetAll method */
366 xmlNewProp(method_node, BAD_CAST "name",
367 BAD_CAST WPA_DBUS_PROPERTIES_GETALL);
368 arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
369 xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
370 xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
371 xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
372 arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
373 xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "props");
374 xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "a{sv}");
375 xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
376 method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
378 /* Add Set method */
379 xmlNewProp(method_node, BAD_CAST "name",
380 BAD_CAST WPA_DBUS_PROPERTIES_SET);
381 arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
382 xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
383 xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
384 xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
385 arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
386 xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "propname");
387 xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
388 xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
389 arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
390 xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "value");
391 xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "v");
392 xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
394 /* get all interfaces registered with object */
395 ifaces = extract_interfaces(obj_dsc, root_node);
397 /* create methods' nodes */
398 method_dsc = obj_dsc->methods;
399 while (method_dsc) {
401 struct interfaces *iface = ifaces;
402 while (iface) {
403 if (!os_strcmp(iface->dbus_interface,
404 method_dsc->dbus_interface))
405 break;
406 iface = iface->next;
408 if (!iface)
409 continue;
411 iface_node = iface->interface_node;
412 method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method",
413 NULL);
414 xmlNewProp(method_node, BAD_CAST "name",
415 BAD_CAST method_dsc->dbus_method);
417 /* create args' nodes */
418 for (i = 0; i < method_dsc->args_num; i++) {
419 struct wpa_dbus_argument arg = method_dsc->args[i];
420 arg_node = xmlNewChild(method_node, NULL,
421 BAD_CAST "arg", NULL);
422 if (arg.name && strlen(arg.name)) {
423 xmlNewProp(arg_node, BAD_CAST "name",
424 BAD_CAST arg.name);
426 xmlNewProp(arg_node, BAD_CAST "type",
427 BAD_CAST arg.type);
428 xmlNewProp(arg_node, BAD_CAST "direction",
429 BAD_CAST (arg.dir == ARG_IN ?
430 "in" : "out"));
432 method_dsc = method_dsc->next;
435 /* create signals' nodes */
436 signal_dsc = obj_dsc->signals;
437 while (signal_dsc) {
439 struct interfaces *iface = ifaces;
440 while (iface) {
441 if (!os_strcmp(iface->dbus_interface,
442 signal_dsc->dbus_interface))
443 break;
444 iface = iface->next;
446 if (!iface)
447 continue;
449 iface_node = iface->interface_node;
450 signal_node = xmlNewChild(iface_node, NULL, BAD_CAST "signal",
451 NULL);
452 xmlNewProp(signal_node, BAD_CAST "name",
453 BAD_CAST signal_dsc->dbus_signal);
455 /* create args' nodes */
456 for (i = 0; i < signal_dsc->args_num; i++) {
457 struct wpa_dbus_argument arg = signal_dsc->args[i];
458 arg_node = xmlNewChild(signal_node, NULL,
459 BAD_CAST "arg", NULL);
460 if (arg.name && strlen(arg.name)) {
461 xmlNewProp(arg_node, BAD_CAST "name",
462 BAD_CAST arg.name);
464 xmlNewProp(arg_node, BAD_CAST "type",
465 BAD_CAST arg.type);
467 signal_dsc = signal_dsc->next;
470 /* create properties' nodes */
471 property_dsc = obj_dsc->properties;
472 while (property_dsc) {
474 struct interfaces *iface = ifaces;
475 while (iface) {
476 if (!os_strcmp(iface->dbus_interface,
477 property_dsc->dbus_interface))
478 break;
479 iface = iface->next;
481 if (!iface)
482 continue;
484 iface_node = iface->interface_node;
485 property_node = xmlNewChild(iface_node, NULL,
486 BAD_CAST "property", NULL);
487 xmlNewProp(property_node, BAD_CAST "name",
488 BAD_CAST property_dsc->dbus_property);
489 xmlNewProp(property_node, BAD_CAST "type",
490 BAD_CAST property_dsc->type);
491 xmlNewProp(property_node, BAD_CAST "access", BAD_CAST
492 (property_dsc->access == R ? "read" :
493 (property_dsc->access == W ?
494 "write" : "readwrite")));
496 property_dsc = property_dsc->next;
499 /* add child nodes to introspection tree; */
500 dbus_connection_list_registered(obj_dsc->connection,
501 dbus_message_get_path(message),
502 &children);
503 for (i = 0; children[i]; i++) {
504 node = xmlNewChild(root_node, NULL, BAD_CAST "node", NULL);
505 xmlNewProp(node, BAD_CAST "name", BAD_CAST children[i]);
507 dbus_free_string_array(children);
510 xmlDocDumpFormatMemory(doc, &intro_str, &s, 1);
512 xmlFreeDoc(doc);
514 while (ifaces) {
515 tmp = ifaces;
516 ifaces = ifaces->next;
517 os_free(tmp->dbus_interface);
518 os_free(tmp);
521 reply = dbus_message_new_method_return(message);
522 if (reply == NULL) {
523 xmlFree(intro_str);
524 return NULL;
527 dbus_message_append_args(reply, DBUS_TYPE_STRING, &intro_str,
528 DBUS_TYPE_INVALID);
530 xmlFree(intro_str);
532 return reply;
535 #else /* CONFIG_CTRL_IFACE_DBUS_INTRO */
538 * introspect - Responds for Introspect calls on object
539 * @message: Message with Introspect call
540 * @obj_dsc: Object description on which Introspect was called
541 * Returns: Message with introspection result XML string as only argument
543 * Returns error informing that introspection support was not compiled.
545 static DBusMessage * introspect(DBusMessage *message,
546 struct wpa_dbus_object_desc *obj_dsc)
548 return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
549 "wpa_supplicant was compiled without "
550 "introspection support.");
553 #endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
557 * recursive_iter_copy - Reads arguments from one iterator and
558 * writes to another recursively
559 * @from: iterator to read from
560 * @to: iterator to write to
562 * Copies one iterator's elements to another. If any element in
563 * iterator is of container type, its content is copied recursively
565 static void recursive_iter_copy(DBusMessageIter *from, DBusMessageIter *to)
568 char *subtype = NULL;
569 int type;
571 /* iterate over iterator to copy */
572 while ((type = dbus_message_iter_get_arg_type (from)) !=
573 DBUS_TYPE_INVALID) {
575 /* simply copy basic type entries */
576 if (dbus_type_is_basic(type)) {
577 if (dbus_type_is_fixed(type)) {
579 * According to DBus documentation all
580 * fixed-length types are guaranteed to fit
581 * 8 bytes
583 dbus_uint64_t v;
584 dbus_message_iter_get_basic (from, &v);
585 dbus_message_iter_append_basic (to, type, &v);
586 } else {
587 char *v;
588 dbus_message_iter_get_basic (from, &v);
589 dbus_message_iter_append_basic (to, type, &v);
591 } else {
592 /* recursively copy container type entries */
593 DBusMessageIter write_subiter, read_subiter;
595 dbus_message_iter_recurse(from, &read_subiter);
597 if (type == DBUS_TYPE_VARIANT ||
598 type == DBUS_TYPE_ARRAY) {
599 subtype = dbus_message_iter_get_signature(
600 &read_subiter);
603 dbus_message_iter_open_container(to, type, subtype,
604 &write_subiter);
606 recursive_iter_copy(&read_subiter, &write_subiter);
608 dbus_message_iter_close_container(to, &write_subiter);
609 if (subtype)
610 dbus_free(subtype);
613 dbus_message_iter_next(from);
619 * get_all_properties - Responds for GetAll properties calls on object
620 * @message: Message with GetAll call
621 * @interface: interface name which properties will be returned
622 * @property_dsc: list of object's properties
623 * Returns: Message with dict of variants as argument with properties values
625 * Iterates over all properties registered with object and execute getters
626 * of those, which are readable and which interface matches interface
627 * specified as argument. Returned message contains one dict argument
628 * with properties names as keys and theirs values as values.
630 static DBusMessage * get_all_properties(
631 DBusMessage *message, char *interface,
632 struct wpa_dbus_property_desc *property_dsc)
634 /* Create and initialize the return message */
635 DBusMessage *reply = dbus_message_new_method_return(message);
636 DBusMessage *getterReply = NULL;
637 DBusMessageIter iter, dict_iter, entry_iter, ret_iter;
638 int counter = 0;
640 dbus_message_iter_init_append(reply, &iter);
642 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
643 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
644 DBUS_TYPE_STRING_AS_STRING
645 DBUS_TYPE_VARIANT_AS_STRING
646 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
647 &dict_iter);
649 while (property_dsc) {
650 if (!os_strncmp(property_dsc->dbus_interface, interface,
651 WPAS_DBUS_INTERFACE_MAX) &&
652 property_dsc->access != W && property_dsc->getter) {
654 getterReply = property_dsc->getter(
655 message, property_dsc->user_data);
656 dbus_message_iter_init(getterReply, &ret_iter);
658 dbus_message_iter_open_container(&dict_iter,
659 DBUS_TYPE_DICT_ENTRY,
660 NULL, &entry_iter);
661 dbus_message_iter_append_basic(
662 &entry_iter, DBUS_TYPE_STRING,
663 &(property_dsc->dbus_property));
665 recursive_iter_copy(&ret_iter, &entry_iter);
667 dbus_message_iter_close_container(&dict_iter,
668 &entry_iter);
669 dbus_message_unref(getterReply);
670 counter++;
672 property_dsc = property_dsc->next;
674 dbus_message_iter_close_container(&iter, &dict_iter);
676 if (counter == 0) {
677 dbus_message_unref(reply);
678 reply = dbus_message_new_error(message,
679 DBUS_ERROR_INVALID_ARGS,
680 "No readable properties in "
681 "this interface");
684 return reply;
688 static int is_signature_correct(DBusMessage *message,
689 struct wpa_dbus_method_desc *method_dsc)
691 /* According to DBus documentation max length of signature is 255 */
692 #define MAX_SIG_LEN 256
693 char registered_sig[MAX_SIG_LEN], *pos;
694 const char *sig = dbus_message_get_signature(message);
695 int i, ret;
697 pos = registered_sig;
698 *pos = '\0';
700 for (i = 0; i < method_dsc->args_num; i++) {
701 struct wpa_dbus_argument arg = method_dsc->args[i];
702 if (arg.dir == ARG_IN) {
703 size_t blen = registered_sig + MAX_SIG_LEN - pos;
704 ret = os_snprintf(pos, blen, "%s", arg.type);
705 if (ret < 0 || (size_t) ret >= blen)
706 return 0;
707 pos += ret;
711 return !os_strncmp(registered_sig, sig, MAX_SIG_LEN);
715 static DBusMessage * properties_get_all(DBusMessage *message, char *interface,
716 struct wpa_dbus_object_desc *obj_dsc)
718 if (os_strcmp(dbus_message_get_signature(message), "s") != 0)
719 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
720 NULL);
722 return get_all_properties(message, interface,
723 obj_dsc->properties);
727 static DBusMessage * properties_get(DBusMessage *message,
728 struct wpa_dbus_property_desc *dsc)
730 if (os_strcmp(dbus_message_get_signature(message), "ss"))
731 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
732 NULL);
734 if (dsc->access != W && dsc->getter)
735 return dsc->getter(message, dsc->user_data);
737 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
738 "Property is write-only");
742 static DBusMessage * properties_set(DBusMessage *message,
743 struct wpa_dbus_property_desc *dsc)
745 if (os_strcmp(dbus_message_get_signature(message), "ssv"))
746 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
747 NULL);
749 if (dsc->access != R && dsc->setter)
750 return dsc->setter(message, dsc->user_data);
752 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
753 "Property is read-only");
757 static DBusMessage *
758 properties_get_or_set(DBusMessage *message, DBusMessageIter *iter,
759 char *interface,
760 struct wpa_dbus_object_desc *obj_dsc)
762 struct wpa_dbus_property_desc *property_dsc;
763 char *property;
764 const char *method;
766 method = dbus_message_get_member(message);
767 property_dsc = obj_dsc->properties;
769 /* Second argument: property name (DBUS_TYPE_STRING) */
770 if (!dbus_message_iter_next(iter) ||
771 dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING) {
772 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
773 NULL);
775 dbus_message_iter_get_basic(iter, &property);
777 while (property_dsc) {
778 /* compare property names and
779 * interfaces */
780 if (!os_strncmp(property_dsc->dbus_property, property,
781 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
782 !os_strncmp(property_dsc->dbus_interface, interface,
783 WPAS_DBUS_INTERFACE_MAX))
784 break;
786 property_dsc = property_dsc->next;
788 if (property_dsc == NULL) {
789 wpa_printf(MSG_DEBUG, "no property handler for %s.%s on %s",
790 interface, property,
791 dbus_message_get_path(message));
792 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
793 "No such property");
796 if (os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
797 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) == 0)
798 return properties_get(message, property_dsc);
800 return properties_set(message, property_dsc);
804 static DBusMessage * properties_handler(DBusMessage *message,
805 struct wpa_dbus_object_desc *obj_dsc)
807 DBusMessageIter iter;
808 char *interface;
809 const char *method;
811 method = dbus_message_get_member(message);
812 dbus_message_iter_init(message, &iter);
814 if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
815 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
816 !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
817 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
818 !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
819 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
820 /* First argument: interface name (DBUS_TYPE_STRING) */
821 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
823 return dbus_message_new_error(message,
824 DBUS_ERROR_INVALID_ARGS,
825 NULL);
828 dbus_message_iter_get_basic(&iter, &interface);
830 if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
831 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
832 /* GetAll */
833 return properties_get_all(message, interface, obj_dsc);
835 /* Get or Set */
836 return properties_get_or_set(message, &iter, interface,
837 obj_dsc);
839 return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
840 NULL);
844 static DBusMessage * msg_method_handler(DBusMessage *message,
845 struct wpa_dbus_object_desc *obj_dsc)
847 struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
848 const char *method;
849 const char *msg_interface;
851 method = dbus_message_get_member(message);
852 msg_interface = dbus_message_get_interface(message);
854 /* try match call to any registered method */
855 while (method_dsc) {
856 /* compare method names and interfaces */
857 if (!os_strncmp(method_dsc->dbus_method, method,
858 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
859 !os_strncmp(method_dsc->dbus_interface, msg_interface,
860 WPAS_DBUS_INTERFACE_MAX))
861 break;
863 method_dsc = method_dsc->next;
865 if (method_dsc == NULL) {
866 wpa_printf(MSG_DEBUG, "no method handler for %s.%s on %s",
867 msg_interface, method,
868 dbus_message_get_path(message));
869 return dbus_message_new_error(message,
870 DBUS_ERROR_UNKNOWN_METHOD, NULL);
873 if (!is_signature_correct(message, method_dsc)) {
874 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
875 NULL);
878 return method_dsc->method_handler(message,
879 method_dsc->handler_argument);
884 * message_handler - Handles incoming DBus messages
885 * @connection: DBus connection on which message was received
886 * @message: Received message
887 * @user_data: pointer to description of object to which message was sent
888 * Returns: Returns information whether message was handled or not
890 * Reads message interface and method name, then checks if they matches one
891 * of the special cases i.e. introspection call or properties get/getall/set
892 * methods and handles it. Else it iterates over registered methods list
893 * and tries to match method's name and interface to those read from message
894 * If appropriate method was found its handler function is called and
895 * response is sent. Otherwise, the DBUS_ERROR_UNKNOWN_METHOD error message
896 * will be sent.
898 static DBusHandlerResult message_handler(DBusConnection *connection,
899 DBusMessage *message, void *user_data)
901 struct wpa_dbus_object_desc *obj_dsc = user_data;
902 const char *method;
903 const char *path;
904 const char *msg_interface;
905 DBusMessage *reply;
907 /* get method, interface and path the message is addressed to */
908 method = dbus_message_get_member(message);
909 path = dbus_message_get_path(message);
910 msg_interface = dbus_message_get_interface(message);
911 if (!method || !path || !msg_interface)
912 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
914 wpa_printf(MSG_MSGDUMP, "dbus: %s.%s (%s)",
915 msg_interface, method, path);
917 /* if message is introspection method call */
918 if (!os_strncmp(WPA_DBUS_INTROSPECTION_METHOD, method,
919 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
920 !os_strncmp(WPA_DBUS_INTROSPECTION_INTERFACE, msg_interface,
921 WPAS_DBUS_INTERFACE_MAX))
922 reply = introspect(message, obj_dsc);
923 else if (!os_strncmp(WPA_DBUS_PROPERTIES_INTERFACE, msg_interface,
924 WPAS_DBUS_INTERFACE_MAX)) {
925 /* if message is properties method call */
926 reply = properties_handler(message, obj_dsc);
927 } else {
928 reply = msg_method_handler(message, obj_dsc);
931 /* If handler succeed returning NULL, reply empty message */
932 if (!reply)
933 reply = dbus_message_new_method_return(message);
934 if (reply) {
935 if (!dbus_message_get_no_reply(message))
936 dbus_connection_send(connection, reply, NULL);
937 dbus_message_unref(reply);
939 return DBUS_HANDLER_RESULT_HANDLED;
944 * free_dbus_object_desc - Frees object description data structure
945 * @connection: DBus connection
946 * @obj_dsc: Object description to free
948 * Frees each of properties, methods and signals description lists and
949 * the object description structure itself.
951 void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
953 struct wpa_dbus_method_desc *method_dsc, *tmp_met_dsc;
954 struct wpa_dbus_signal_desc *signal_dsc, *tmp_sig_dsc;
955 struct wpa_dbus_property_desc *property_dsc, *tmp_prop_dsc;
956 int i;
958 if (!obj_dsc)
959 return;
961 /* free methods */
962 method_dsc = obj_dsc->methods;
964 while (method_dsc) {
965 tmp_met_dsc = method_dsc;
966 method_dsc = method_dsc->next;
968 os_free(tmp_met_dsc->dbus_interface);
969 os_free(tmp_met_dsc->dbus_method);
971 for (i = 0; i < tmp_met_dsc->args_num; i++) {
972 os_free(tmp_met_dsc->args[i].name);
973 os_free(tmp_met_dsc->args[i].type);
976 if (tmp_met_dsc->argument_free_func)
977 tmp_met_dsc->argument_free_func(
978 tmp_met_dsc->handler_argument);
980 os_free(tmp_met_dsc);
983 /* free signals */
984 signal_dsc = obj_dsc->signals;
986 while (signal_dsc) {
987 tmp_sig_dsc = signal_dsc;
988 signal_dsc = signal_dsc->next;
990 os_free(tmp_sig_dsc->dbus_interface);
991 os_free(tmp_sig_dsc->dbus_signal);
993 for (i = 0; i < tmp_sig_dsc->args_num; i++) {
994 os_free(tmp_sig_dsc->args[i].name);
995 os_free(tmp_sig_dsc->args[i].type);
998 os_free(tmp_sig_dsc);
1001 /* free properties */
1002 property_dsc = obj_dsc->properties;
1004 while (property_dsc) {
1005 tmp_prop_dsc = property_dsc;
1006 property_dsc = property_dsc->next;
1008 os_free(tmp_prop_dsc->dbus_interface);
1009 os_free(tmp_prop_dsc->dbus_property);
1010 os_free(tmp_prop_dsc->type);
1012 if (tmp_prop_dsc->user_data_free_func)
1013 tmp_prop_dsc->user_data_free_func(
1014 tmp_prop_dsc->user_data);
1016 os_free(tmp_prop_dsc);
1019 os_free(obj_dsc);
1023 static void free_dbus_object_desc_cb(DBusConnection *connection, void *obj_dsc)
1025 free_dbus_object_desc(obj_dsc);
1029 * wpa_dbus_ctrl_iface_init - Initialize dbus control interface
1030 * @application_data: Pointer to application specific data structure
1031 * @dbus_path: DBus path to interface object
1032 * @dbus_service: DBus service name to register with
1033 * @messageHandler: a pointer to function which will handle dbus messages
1034 * coming on interface
1035 * Returns: 0 on success, -1 on failure
1037 * Initialize the dbus control interface and start receiving commands from
1038 * external programs over the bus.
1040 int wpa_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface,
1041 char *dbus_path, char *dbus_service,
1042 struct wpa_dbus_object_desc *obj_desc)
1044 DBusError error;
1045 int ret = -1;
1046 DBusObjectPathVTable wpa_vtable = {
1047 &free_dbus_object_desc_cb, &message_handler,
1048 NULL, NULL, NULL, NULL
1051 obj_desc->connection = iface->con;
1053 /* Register the message handler for the global dbus interface */
1054 if (!dbus_connection_register_object_path(iface->con,
1055 dbus_path, &wpa_vtable,
1056 obj_desc)) {
1057 perror("dbus_connection_register_object_path[dbus]");
1058 wpa_printf(MSG_ERROR, "Could not set up DBus message "
1059 "handler.");
1060 return -1;
1063 /* Register our service with the message bus */
1064 dbus_error_init(&error);
1065 switch (dbus_bus_request_name(iface->con, dbus_service,
1066 0, &error)) {
1067 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
1068 ret = 0;
1069 break;
1070 case DBUS_REQUEST_NAME_REPLY_EXISTS:
1071 case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
1072 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
1073 perror("dbus_bus_request_name[dbus]");
1074 wpa_printf(MSG_ERROR, "Could not request DBus service name: "
1075 "already registered.");
1076 break;
1077 default:
1078 perror("dbus_bus_request_name[dbus]");
1079 wpa_printf(MSG_ERROR, "Could not request DBus service name: "
1080 "%s %s.", error.name, error.message);
1081 break;
1083 dbus_error_free(&error);
1085 if (ret != 0)
1086 return -1;
1088 wpa_printf(MSG_DEBUG, "Providing DBus service '%s'.", dbus_service);
1090 return 0;
1095 * wpa_dbus_register_object_per_iface - Register a new object with dbus
1096 * @ctrl_iface: pointer to dbus private data
1097 * @path: DBus path to object
1098 * @ifname: interface name
1099 * @obj_desc: description of object's methods, signals and properties
1100 * Returns: 0 on success, -1 on error
1102 * Registers a new interface with dbus and assigns it a dbus object path.
1104 int wpa_dbus_register_object_per_iface(
1105 struct wpas_dbus_priv *ctrl_iface,
1106 const char *path, const char *ifname,
1107 struct wpa_dbus_object_desc *obj_desc)
1109 DBusConnection *con;
1111 DBusObjectPathVTable vtable = {
1112 &free_dbus_object_desc_cb, &message_handler,
1113 NULL, NULL, NULL, NULL
1116 /* Do nothing if the control interface is not turned on */
1117 if (ctrl_iface == NULL)
1118 return 0;
1120 con = ctrl_iface->con;
1121 obj_desc->connection = con;
1123 /* Register the message handler for the interface functions */
1124 if (!dbus_connection_register_object_path(con, path, &vtable,
1125 obj_desc)) {
1126 perror("wpa_dbus_register_iface [dbus]");
1127 wpa_printf(MSG_ERROR, "Could not set up DBus message "
1128 "handler for interface %s\n"
1129 "and object %s.", ifname, path);
1130 return -1;
1133 return 0;
1138 * wpa_dbus_unregister_object_per_iface - Unregisters DBus object
1139 * @ctrl_iface: Pointer to dbus private data
1140 * @path: DBus path to object which will be unregistered
1141 * Returns: Zero on success and -1 on failure
1143 * Unregisters DBus object given by its path
1145 int wpa_dbus_unregister_object_per_iface(
1146 struct wpas_dbus_priv *ctrl_iface, const char *path)
1148 DBusConnection *con = ctrl_iface->con;
1149 if (!dbus_connection_unregister_object_path(con, path))
1150 return -1;
1152 return 0;
1157 * wpa_dbus_method_register - Registers DBus method for given object
1158 * @obj_dsc: Object description for which a method will be registered
1159 * @dbus_interface: DBus interface under which method will be registered
1160 * @dbus_method: a name the method will be registered with
1161 * @method_handler: a function which will be called to handle this method call
1162 * @handler_argument: an additional argument passed to handler function
1163 * @argument_free_func: function used to free handler argument
1164 * @args: method arguments list
1165 * Returns: Zero on success and -1 on failure
1167 * Registers DBus method under given name and interface for the object.
1168 * Method calls will be handled with given handling function and optional
1169 * argument passed to this function. Handler function is required to return
1170 * a DBusMessage pointer which will be response to method call. Any method
1171 * call before being handled must have registered appropriate handler by
1172 * using this function.
1174 int wpa_dbus_method_register(struct wpa_dbus_object_desc *obj_dsc,
1175 const char *dbus_interface,
1176 const char *dbus_method,
1177 WPADBusMethodHandler method_handler,
1178 void *handler_argument,
1179 WPADBusArgumentFreeFunction argument_free_func,
1180 const struct wpa_dbus_argument args[])
1182 struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
1183 struct wpa_dbus_method_desc *prev_desc;
1184 int args_num = 0;
1185 int interface_len, method_len, i, len, error;
1187 prev_desc = NULL;
1188 while (method_dsc) {
1189 prev_desc = method_dsc;
1190 method_dsc = method_dsc->next;
1193 /* count args */
1194 if (args) {
1195 while (args[args_num].name && args[args_num].type)
1196 args_num++;
1199 method_dsc = os_zalloc(sizeof(struct wpa_dbus_method_desc) +
1200 args_num * sizeof(struct wpa_dbus_argument));
1201 if (!method_dsc)
1202 goto err;
1204 if (prev_desc == NULL)
1205 obj_dsc->methods = method_dsc;
1206 else
1207 prev_desc->next = method_dsc;
1209 /* copy interface name */
1210 interface_len = os_strlen(dbus_interface) + 1;
1211 method_dsc->dbus_interface = os_malloc(interface_len);
1212 if (!method_dsc->dbus_interface)
1213 goto err;
1214 os_strncpy(method_dsc->dbus_interface, dbus_interface, interface_len);
1216 /* copy method name */
1217 method_len = os_strlen(dbus_method) + 1;
1218 method_dsc->dbus_method = os_malloc(method_len);
1219 if (!method_dsc->dbus_method)
1220 goto err;
1221 os_strncpy(method_dsc->dbus_method, dbus_method, method_len);
1223 /* copy arguments */
1224 error = 0;
1225 method_dsc->args_num = args_num;
1226 for (i = 0; i < args_num; i++) {
1227 len = os_strlen(args[i].name) + 1;
1228 method_dsc->args[i].name = os_malloc(len);
1229 if (!method_dsc->args[i].name) {
1230 error = 1;
1231 continue;
1233 os_strncpy(method_dsc->args[i].name, args[i].name, len);
1235 len = os_strlen(args[i].type) + 1;
1236 method_dsc->args[i].type = os_malloc(len);
1237 if (!method_dsc->args[i].type) {
1238 error = 1;
1239 continue;
1241 os_strncpy(method_dsc->args[i].type, args[i].type, len);
1243 method_dsc->args[i].dir = args[i].dir;
1245 if (error)
1246 goto err;
1248 method_dsc->method_handler = method_handler;
1249 method_dsc->handler_argument = handler_argument;
1250 method_dsc->argument_free_func = argument_free_func;
1251 method_dsc->next = NULL;
1253 return 0;
1255 err:
1256 wpa_printf(MSG_WARNING, "Failed to register dbus method %s in "
1257 "interface %s", dbus_method, dbus_interface);
1258 if (method_dsc) {
1259 os_free(method_dsc->dbus_interface);
1260 os_free(method_dsc->dbus_method);
1261 for (i = 0; i < method_dsc->args_num; i++) {
1262 os_free(method_dsc->args[i].name);
1263 os_free(method_dsc->args[i].type);
1266 if (prev_desc == NULL)
1267 obj_dsc->methods = NULL;
1268 else
1269 prev_desc->next = NULL;
1271 os_free(method_dsc);
1274 return -1;
1279 * wpa_dbus_signal_register - Registers DBus signal for given object
1280 * @obj_dsc: Object description for which a signal will be registered
1281 * @dbus_interface: DBus interface under which signal will be registered
1282 * @dbus_signal: a name the signal will be registered with
1283 * @args: signal arguments list
1284 * Returns: Zero on success and -1 on failure
1286 * Registers DBus signal under given name and interface for the object.
1287 * Signal registration is NOT required in order to send signals, but not
1288 * registered signals will not be respected in introspection data
1289 * therefore it is highly recommended to register every signal before
1290 * using it.
1292 int wpa_dbus_signal_register(struct wpa_dbus_object_desc *obj_dsc,
1293 const char *dbus_interface,
1294 const char *dbus_signal,
1295 const struct wpa_dbus_argument args[])
1298 struct wpa_dbus_signal_desc *signal_dsc = obj_dsc->signals;
1299 struct wpa_dbus_signal_desc *prev_desc;
1300 int args_num = 0;
1301 int interface_len, signal_len, i, len, error = 0;
1303 prev_desc = NULL;
1304 while (signal_dsc) {
1305 prev_desc = signal_dsc;
1306 signal_dsc = signal_dsc->next;
1309 /* count args */
1310 if (args) {
1311 while (args[args_num].name && args[args_num].type)
1312 args_num++;
1315 signal_dsc = os_zalloc(sizeof(struct wpa_dbus_signal_desc) +
1316 args_num * sizeof(struct wpa_dbus_argument));
1317 if (!signal_dsc)
1318 goto err;
1320 if (prev_desc == NULL)
1321 obj_dsc->signals = signal_dsc;
1322 else
1323 prev_desc->next = signal_dsc;
1325 /* copy interface name */
1326 interface_len = strlen(dbus_interface) + 1;
1327 signal_dsc->dbus_interface = os_malloc(interface_len);
1328 if (!signal_dsc->dbus_interface)
1329 goto err;
1330 os_strncpy(signal_dsc->dbus_interface, dbus_interface, interface_len);
1332 /* copy signal name */
1333 signal_len = strlen(dbus_signal) + 1;
1334 signal_dsc->dbus_signal = os_malloc(signal_len);
1335 if (!signal_dsc->dbus_signal)
1336 goto err;
1337 os_strncpy(signal_dsc->dbus_signal, dbus_signal, signal_len);
1339 /* copy arguments */
1340 signal_dsc->args_num = args_num;
1341 for (i = 0; i < args_num; i++) {
1342 len = os_strlen(args[i].name) + 1;
1343 signal_dsc->args[i].name = os_malloc(len);
1344 if (!signal_dsc->args[i].name) {
1345 error = 1;
1346 continue;
1348 os_strncpy(signal_dsc->args[i].name, args[i].name, len);
1350 len = strlen(args[i].type) + 1;
1351 signal_dsc->args[i].type = os_malloc(len);
1352 if (!signal_dsc->args[i].type) {
1353 error = 1;
1354 continue;
1356 os_strncpy(signal_dsc->args[i].type, args[i].type, len);
1358 if (error)
1359 goto err;
1361 signal_dsc->next = NULL;
1363 return 0;
1365 err:
1366 wpa_printf(MSG_WARNING, "Failed to register dbus signal %s in "
1367 "interface %s", dbus_signal, dbus_interface);
1368 if (signal_dsc) {
1369 os_free(signal_dsc->dbus_interface);
1370 os_free(signal_dsc->dbus_signal);
1371 for (i = 0; i < signal_dsc->args_num; i++) {
1372 os_free(signal_dsc->args[i].name);
1373 os_free(signal_dsc->args[i].type);
1376 if (prev_desc == NULL)
1377 obj_dsc->signals = NULL;
1378 else
1379 prev_desc->next = NULL;
1381 os_free(signal_dsc);
1384 return -1;
1389 * wpa_dbus_property_register - Registers DBus property for given object
1390 * @obj_dsc: Object description for which a property will be registered
1391 * @dbus_interface: DBus interface under which method will be registered
1392 * @dbus_property: a name the property will be registered with
1393 * @type: a property type signature in form of DBus type description
1394 * @getter: a function called in order to get property value
1395 * @setter: a function called in order to set property value
1396 * @user_data: additional argument passed to setter or getter
1397 * @user_data_free_func: function used to free additional argument
1398 * @access: property access permissions specifier (R, W or RW)
1399 * Returns: Zero on success and -1 on failure
1401 * Registers DBus property under given name and interface for the object.
1402 * Property are set with giver setter function and get with getter.
1403 * Additional argument is passed to getter or setter. Getter or setter
1404 * are required to return DBusMessage which is response to Set/Get method
1405 * calls. Every property must be registered by this function before being
1406 * used.
1408 int wpa_dbus_property_register(struct wpa_dbus_object_desc *obj_dsc,
1409 const char *dbus_interface,
1410 const char *dbus_property,
1411 const char *type,
1412 WPADBusPropertyAccessor getter,
1413 WPADBusPropertyAccessor setter,
1414 void *user_data,
1415 WPADBusArgumentFreeFunction user_data_free_func,
1416 enum dbus_prop_access _access)
1418 struct wpa_dbus_property_desc *property_dsc = obj_dsc->properties;
1419 struct wpa_dbus_property_desc *prev_desc;
1420 int interface_len, property_len, type_len;
1422 prev_desc = NULL;
1423 while (property_dsc) {
1424 prev_desc = property_dsc;
1425 property_dsc = property_dsc->next;
1428 property_dsc = os_zalloc(sizeof(struct wpa_dbus_property_desc));
1429 if (!property_dsc)
1430 goto err;
1432 if (prev_desc == NULL)
1433 obj_dsc->properties = property_dsc;
1434 else
1435 prev_desc->next = property_dsc;
1437 /* copy interface name */
1438 interface_len = os_strlen(dbus_interface) + 1;
1439 property_dsc->dbus_interface = os_malloc(interface_len);
1440 if (!property_dsc->dbus_interface)
1441 goto err;
1442 os_strncpy(property_dsc->dbus_interface, dbus_interface,
1443 interface_len);
1445 /* copy property name */
1446 property_len = os_strlen(dbus_property) + 1;
1447 property_dsc->dbus_property = os_malloc(property_len);
1448 if (!property_dsc->dbus_property)
1449 goto err;
1450 os_strncpy(property_dsc->dbus_property, dbus_property, property_len);
1452 /* copy property type */
1453 type_len = os_strlen(type) + 1;
1454 property_dsc->type = os_malloc(type_len);
1455 if (!property_dsc->type)
1456 goto err;
1457 os_strncpy(property_dsc->type, type, type_len);
1459 property_dsc->getter = getter;
1460 property_dsc->setter = setter;
1461 property_dsc->user_data = user_data;
1462 property_dsc->user_data_free_func = user_data_free_func;
1463 property_dsc->access = _access;
1464 property_dsc->next = NULL;
1466 return 0;
1468 err:
1469 wpa_printf(MSG_WARNING, "Failed to register dbus property %s in "
1470 "interface %s", dbus_property, dbus_interface);
1471 if (property_dsc) {
1472 os_free(property_dsc->dbus_interface);
1473 os_free(property_dsc->dbus_property);
1474 os_free(property_dsc->type);
1476 if (prev_desc == NULL)
1477 obj_dsc->properties = NULL;
1478 else
1479 prev_desc->next = NULL;
1481 os_free(property_dsc);
1484 return -1;
1489 * wpas_dbus_signal_network_added - Send a property changed signal
1490 * @iface: dbus priv struct
1491 * @property_getter: propperty getter used to fetch new property value
1492 * @getter_arg: argument passed to property getter
1493 * @path: path to object which property has changed
1494 * @interface_name: signal and property interface
1495 * @property_name: name of property which has changed
1497 * Notify listeners about changing value of some property. Signal
1498 * contains property name and its value fetched using given property
1499 * getter.
1501 void wpa_dbus_signal_property_changed(struct wpas_dbus_priv *iface,
1502 WPADBusPropertyAccessor property_getter,
1503 void *getter_arg,
1504 const char *path,
1505 const char *interface_name,
1506 const char *property_name)
1509 DBusConnection *connection;
1510 DBusMessage *_signal, *getter_reply;
1511 DBusMessageIter prop_iter, signal_iter, dict_iter, entry_iter;
1513 if (!iface)
1514 return;
1515 connection = iface->con;
1517 if (!property_getter) {
1518 wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
1519 "[dbus]: property getter not specified");
1520 return;
1523 if (!path || !interface_name || !property_name) {
1524 wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
1525 "[dbus]: path interface of property not specified");
1526 return;
1529 getter_reply = property_getter(NULL, getter_arg);
1530 if (!getter_reply ||
1531 dbus_message_get_type(getter_reply) == DBUS_MESSAGE_TYPE_ERROR) {
1532 wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
1533 "[dbus]: cannot get new value of property %s",
1534 property_name);
1535 return;
1538 _signal = dbus_message_new_signal(path, interface_name,
1539 "PropertiesChanged");
1540 if (!_signal) {
1541 wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
1542 "[dbus]: cannot allocate signal");
1543 dbus_message_unref(getter_reply);
1544 return;
1547 dbus_message_iter_init(getter_reply, &prop_iter);
1548 dbus_message_iter_init_append(_signal, &signal_iter);
1550 if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
1551 "{sv}", &dict_iter)) {
1552 wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
1553 "[dbus]: out of memory. cannot open dictionary");
1554 goto err;
1557 if (!dbus_message_iter_open_container(&dict_iter, DBUS_TYPE_DICT_ENTRY,
1558 NULL, &entry_iter)) {
1559 wpa_printf(MSG_ERROR, "iwpa_dbus_signal_property_changed"
1560 "[dbus]: out of memory. cannot open dictionary "
1561 "element");
1562 goto err;
1565 if (!dbus_message_iter_append_basic(&entry_iter, DBUS_TYPE_STRING,
1566 &property_name)) {
1567 wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
1568 "[dbus]: out of memory. cannot open add property "
1569 "name");
1570 goto err;
1573 recursive_iter_copy(&prop_iter, &entry_iter);
1575 if (!dbus_message_iter_close_container(&dict_iter, &entry_iter)) {
1576 wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
1577 "[dbus]: out of memory. cannot close dictionary "
1578 "element");
1579 goto err;
1582 if (!dbus_message_iter_close_container(&signal_iter, &dict_iter)) {
1583 wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
1584 "[dbus]: out of memory. cannot close dictionary");
1585 goto err;
1588 dbus_connection_send(connection, _signal, NULL);
1590 err:
1591 dbus_message_unref(getter_reply);
1592 dbus_message_unref(_signal);