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
13 * See README and COPYING for more details.
20 #include "dbus_common.h"
21 #include "dbus_common_i.h"
22 #include "dbus_new_helpers.h"
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 */
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 */
45 /* array of arguments */
46 struct wpa_dbus_argument args
[];
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 */
62 /* number of signal arguments */
64 /* array of arguments */
65 struct wpa_dbus_argument args
[0];
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 */
80 /* property type signature in DBus type notation */
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 */
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>
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
;
131 /* extract interfaces from methods */
136 /* go to next method if its interface is already extracted */
138 if (!os_strcmp(iface
->dbus_interface
,
139 method_dsc
->dbus_interface
))
145 method_dsc
= method_dsc
->next
;
149 iface
= os_zalloc(sizeof(struct interfaces
));
151 wpa_printf(MSG_ERROR
, "Not enough memory to create "
152 "interface introspection data");
153 method_dsc
= method_dsc
->next
;
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 "
168 method_dsc
= method_dsc
->next
;
171 os_strncpy(iface
->dbus_interface
, method_dsc
->dbus_interface
,
174 iface
->interface_node
= xmlNewChild(root_node
, NULL
,
175 BAD_CAST
"interface",
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 */
188 /* go to next signal if its interface is already extracted */
190 if (!os_strcmp(iface
->dbus_interface
,
191 signal_dsc
->dbus_interface
))
197 signal_dsc
= signal_dsc
->next
;
201 iface
= os_zalloc(sizeof(struct interfaces
));
203 wpa_printf(MSG_ERROR
, "Not enough memory to create "
204 "interface introspection data");
205 signal_dsc
= signal_dsc
->next
;
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 "
220 signal_dsc
= signal_dsc
->next
;
223 os_strncpy(iface
->dbus_interface
, signal_dsc
->dbus_interface
,
226 iface
->interface_node
= xmlNewChild(root_node
, NULL
,
227 BAD_CAST
"interface",
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
) {
240 /* go to next property if its interface is already extracted */
242 if (!os_strcmp(iface
->dbus_interface
,
243 property_dsc
->dbus_interface
))
249 property_dsc
= property_dsc
->next
;
253 iface
= os_zalloc(sizeof(struct interfaces
));
255 wpa_printf(MSG_ERROR
, "Not enough memory to create "
256 "interface introspection data");
257 property_dsc
= property_dsc
->next
;
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 "
272 property_dsc
= property_dsc
->next
;
275 os_strncpy(iface
->dbus_interface
, property_dsc
->dbus_interface
,
278 iface
->interface_node
= xmlNewChild(root_node
, NULL
,
279 BAD_CAST
"interface",
281 xmlNewProp(iface
->interface_node
, BAD_CAST
"name",
282 BAD_CAST property_dsc
->dbus_interface
);
284 property_dsc
= property_dsc
->next
;
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
)
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
;
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
);
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
);
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
;
401 struct interfaces
*iface
= ifaces
;
403 if (!os_strcmp(iface
->dbus_interface
,
404 method_dsc
->dbus_interface
))
411 iface_node
= iface
->interface_node
;
412 method_node
= xmlNewChild(iface_node
, NULL
, BAD_CAST
"method",
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",
426 xmlNewProp(arg_node
, BAD_CAST
"type",
428 xmlNewProp(arg_node
, BAD_CAST
"direction",
429 BAD_CAST (arg
.dir
== ARG_IN
?
432 method_dsc
= method_dsc
->next
;
435 /* create signals' nodes */
436 signal_dsc
= obj_dsc
->signals
;
439 struct interfaces
*iface
= ifaces
;
441 if (!os_strcmp(iface
->dbus_interface
,
442 signal_dsc
->dbus_interface
))
449 iface_node
= iface
->interface_node
;
450 signal_node
= xmlNewChild(iface_node
, NULL
, BAD_CAST
"signal",
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",
464 xmlNewProp(arg_node
, BAD_CAST
"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
;
476 if (!os_strcmp(iface
->dbus_interface
,
477 property_dsc
->dbus_interface
))
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
),
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);
516 ifaces
= ifaces
->next
;
517 os_free(tmp
->dbus_interface
);
521 reply
= dbus_message_new_method_return(message
);
527 dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &intro_str
,
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
;
571 /* iterate over iterator to copy */
572 while ((type
= dbus_message_iter_get_arg_type (from
)) !=
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
584 dbus_message_iter_get_basic (from
, &v
);
585 dbus_message_iter_append_basic (to
, type
, &v
);
588 dbus_message_iter_get_basic (from
, &v
);
589 dbus_message_iter_append_basic (to
, type
, &v
);
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(
603 dbus_message_iter_open_container(to
, type
, subtype
,
606 recursive_iter_copy(&read_subiter
, &write_subiter
);
608 dbus_message_iter_close_container(to
, &write_subiter
);
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
;
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
,
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
,
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
,
669 dbus_message_unref(getterReply
);
672 property_dsc
= property_dsc
->next
;
674 dbus_message_iter_close_container(&iter
, &dict_iter
);
677 dbus_message_unref(reply
);
678 reply
= dbus_message_new_error(message
,
679 DBUS_ERROR_INVALID_ARGS
,
680 "No readable properties in "
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
);
697 pos
= registered_sig
;
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
)
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
,
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
,
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
,
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");
758 properties_get_or_set(DBusMessage
*message
, DBusMessageIter
*iter
,
760 struct wpa_dbus_object_desc
*obj_dsc
)
762 struct wpa_dbus_property_desc
*property_dsc
;
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
,
775 dbus_message_iter_get_basic(iter
, &property
);
777 while (property_dsc
) {
778 /* compare property names and
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
))
786 property_dsc
= property_dsc
->next
;
788 if (property_dsc
== NULL
) {
789 wpa_printf(MSG_DEBUG
, "no property handler for %s.%s on %s",
791 dbus_message_get_path(message
));
792 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
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
;
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
,
828 dbus_message_iter_get_basic(&iter
, &interface
);
830 if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL
, method
,
831 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX
)) {
833 return properties_get_all(message
, interface
, obj_dsc
);
836 return properties_get_or_set(message
, &iter
, interface
,
839 return dbus_message_new_error(message
, DBUS_ERROR_UNKNOWN_METHOD
,
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
;
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 */
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
))
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
,
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
898 static DBusHandlerResult
message_handler(DBusConnection
*connection
,
899 DBusMessage
*message
, void *user_data
)
901 struct wpa_dbus_object_desc
*obj_dsc
= user_data
;
904 const char *msg_interface
;
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
);
928 reply
= msg_method_handler(message
, obj_dsc
);
931 /* If handler succeed returning NULL, reply empty message */
933 reply
= dbus_message_new_method_return(message
);
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
;
962 method_dsc
= obj_dsc
->methods
;
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
);
984 signal_dsc
= obj_dsc
->signals
;
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
);
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
)
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
,
1057 perror("dbus_connection_register_object_path[dbus]");
1058 wpa_printf(MSG_ERROR
, "Could not set up DBus message "
1063 /* Register our service with the message bus */
1064 dbus_error_init(&error
);
1065 switch (dbus_bus_request_name(iface
->con
, dbus_service
,
1067 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
:
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.");
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
);
1083 dbus_error_free(&error
);
1088 wpa_printf(MSG_DEBUG
, "Providing DBus service '%s'.", dbus_service
);
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
)
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
,
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
);
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
))
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
;
1185 int interface_len
, method_len
, i
, len
, error
;
1188 while (method_dsc
) {
1189 prev_desc
= method_dsc
;
1190 method_dsc
= method_dsc
->next
;
1195 while (args
[args_num
].name
&& args
[args_num
].type
)
1199 method_dsc
= os_zalloc(sizeof(struct wpa_dbus_method_desc
) +
1200 args_num
* sizeof(struct wpa_dbus_argument
));
1204 if (prev_desc
== NULL
)
1205 obj_dsc
->methods
= method_dsc
;
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
)
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
)
1221 os_strncpy(method_dsc
->dbus_method
, dbus_method
, method_len
);
1223 /* copy arguments */
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
) {
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
) {
1241 os_strncpy(method_dsc
->args
[i
].type
, args
[i
].type
, len
);
1243 method_dsc
->args
[i
].dir
= args
[i
].dir
;
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
;
1256 wpa_printf(MSG_WARNING
, "Failed to register dbus method %s in "
1257 "interface %s", dbus_method
, dbus_interface
);
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
;
1269 prev_desc
->next
= NULL
;
1271 os_free(method_dsc
);
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
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
;
1301 int interface_len
, signal_len
, i
, len
, error
= 0;
1304 while (signal_dsc
) {
1305 prev_desc
= signal_dsc
;
1306 signal_dsc
= signal_dsc
->next
;
1311 while (args
[args_num
].name
&& args
[args_num
].type
)
1315 signal_dsc
= os_zalloc(sizeof(struct wpa_dbus_signal_desc
) +
1316 args_num
* sizeof(struct wpa_dbus_argument
));
1320 if (prev_desc
== NULL
)
1321 obj_dsc
->signals
= signal_dsc
;
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
)
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
)
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
) {
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
) {
1356 os_strncpy(signal_dsc
->args
[i
].type
, args
[i
].type
, len
);
1361 signal_dsc
->next
= NULL
;
1366 wpa_printf(MSG_WARNING
, "Failed to register dbus signal %s in "
1367 "interface %s", dbus_signal
, dbus_interface
);
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
;
1379 prev_desc
->next
= NULL
;
1381 os_free(signal_dsc
);
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
1408 int wpa_dbus_property_register(struct wpa_dbus_object_desc
*obj_dsc
,
1409 const char *dbus_interface
,
1410 const char *dbus_property
,
1412 WPADBusPropertyAccessor getter
,
1413 WPADBusPropertyAccessor setter
,
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
;
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
));
1432 if (prev_desc
== NULL
)
1433 obj_dsc
->properties
= property_dsc
;
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
)
1442 os_strncpy(property_dsc
->dbus_interface
, dbus_interface
,
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
)
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
)
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
;
1469 wpa_printf(MSG_WARNING
, "Failed to register dbus property %s in "
1470 "interface %s", dbus_property
, dbus_interface
);
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
;
1479 prev_desc
->next
= NULL
;
1481 os_free(property_dsc
);
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
1501 void wpa_dbus_signal_property_changed(struct wpas_dbus_priv
*iface
,
1502 WPADBusPropertyAccessor property_getter
,
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
;
1515 connection
= iface
->con
;
1517 if (!property_getter
) {
1518 wpa_printf(MSG_ERROR
, "wpa_dbus_signal_property_changed"
1519 "[dbus]: property getter not specified");
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");
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",
1538 _signal
= dbus_message_new_signal(path
, interface_name
,
1539 "PropertiesChanged");
1541 wpa_printf(MSG_ERROR
, "wpa_dbus_signal_property_changed"
1542 "[dbus]: cannot allocate signal");
1543 dbus_message_unref(getter_reply
);
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");
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 "
1565 if (!dbus_message_iter_append_basic(&entry_iter
, DBUS_TYPE_STRING
,
1567 wpa_printf(MSG_ERROR
, "wpa_dbus_signal_property_changed"
1568 "[dbus]: out of memory. cannot open add property "
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 "
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");
1588 dbus_connection_send(connection
, _signal
, NULL
);
1591 dbus_message_unref(getter_reply
);
1592 dbus_message_unref(_signal
);