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 software may be distributed under the terms of the BSD license.
7 * See README for more details.
10 #include "utils/includes.h"
12 #include "utils/common.h"
13 #include "utils/eloop.h"
14 #include "dbus_common.h"
15 #include "dbus_common_i.h"
17 #include "dbus_new_helpers.h"
18 #include "dbus_new_handlers.h"
19 #include "dbus_dict_helpers.h"
22 static dbus_bool_t fill_dict_with_properties(
23 DBusMessageIter *dict_iter,
24 const struct wpa_dbus_property_desc *props,
25 const char *interface, void *user_data, DBusError *error)
27 DBusMessageIter entry_iter;
28 const struct wpa_dbus_property_desc *dsc;
30 for (dsc = props; dsc && dsc->dbus_property; dsc++) {
31 /* Only return properties for the requested D-Bus interface */
32 if (os_strncmp(dsc->dbus_interface, interface,
33 WPAS_DBUS_INTERFACE_MAX) != 0)
36 /* Skip write-only properties */
37 if (dsc->getter == NULL)
40 if (!dbus_message_iter_open_container(dict_iter,
43 !dbus_message_iter_append_basic(&entry_iter,
48 /* An error getting a property fails the request entirely */
49 if (!dsc->getter(&entry_iter, error, user_data))
52 if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
59 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
65 * get_all_properties - Responds for GetAll properties calls on object
66 * @message: Message with GetAll call
67 * @interface: interface name which properties will be returned
68 * @property_dsc: list of object's properties
69 * Returns: Message with dict of variants as argument with properties values
71 * Iterates over all properties registered with object and execute getters
72 * of those, which are readable and which interface matches interface
73 * specified as argument. Returned message contains one dict argument
74 * with properties names as keys and theirs values as values.
76 static DBusMessage * get_all_properties(DBusMessage *message, char *interface,
77 struct wpa_dbus_object_desc *obj_dsc)
80 DBusMessageIter iter, dict_iter;
83 reply = dbus_message_new_method_return(message);
85 return wpas_dbus_error_no_memory(message);
87 dbus_message_iter_init_append(reply, &iter);
88 if (!wpa_dbus_dict_open_write(&iter, &dict_iter)) {
89 dbus_message_unref(reply);
90 return wpas_dbus_error_no_memory(message);
93 dbus_error_init(&error);
94 if (!fill_dict_with_properties(&dict_iter, obj_dsc->properties,
95 interface, obj_dsc->user_data, &error)) {
96 dbus_message_unref(reply);
97 reply = wpas_dbus_reply_new_from_error(
98 message, &error, DBUS_ERROR_INVALID_ARGS,
99 "No readable properties in this interface");
100 dbus_error_free(&error);
104 if (!wpa_dbus_dict_close_write(&iter, &dict_iter)) {
105 dbus_message_unref(reply);
106 return wpas_dbus_error_no_memory(message);
113 static int is_signature_correct(DBusMessage *message,
114 const struct wpa_dbus_method_desc *method_dsc)
116 /* According to DBus documentation max length of signature is 255 */
117 #define MAX_SIG_LEN 256
118 char registered_sig[MAX_SIG_LEN], *pos;
119 const char *sig = dbus_message_get_signature(message);
121 const struct wpa_dbus_argument *arg;
123 pos = registered_sig;
126 for (arg = method_dsc->args; arg && arg->name; arg++) {
127 if (arg->dir == ARG_IN) {
128 size_t blen = registered_sig + MAX_SIG_LEN - pos;
130 ret = os_snprintf(pos, blen, "%s", arg->type);
131 if (os_snprintf_error(blen, ret))
137 return !os_strncmp(registered_sig, sig, MAX_SIG_LEN);
141 static DBusMessage * properties_get_all(DBusMessage *message, char *interface,
142 struct wpa_dbus_object_desc *obj_dsc)
144 if (os_strcmp(dbus_message_get_signature(message), "s") != 0)
145 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
148 return get_all_properties(message, interface, obj_dsc);
152 static DBusMessage * properties_get(DBusMessage *message,
153 const struct wpa_dbus_property_desc *dsc,
157 DBusMessageIter iter;
160 if (os_strcmp(dbus_message_get_signature(message), "ss")) {
161 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
165 if (dsc->getter == NULL) {
166 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
167 "Property is write-only");
170 reply = dbus_message_new_method_return(message);
171 dbus_message_iter_init_append(reply, &iter);
173 dbus_error_init(&error);
174 if (dsc->getter(&iter, &error, user_data) == FALSE) {
175 dbus_message_unref(reply);
176 reply = wpas_dbus_reply_new_from_error(
177 message, &error, DBUS_ERROR_FAILED,
178 "Failed to read property");
179 dbus_error_free(&error);
186 static DBusMessage * properties_set(DBusMessage *message,
187 const struct wpa_dbus_property_desc *dsc,
191 DBusMessageIter iter;
194 if (os_strcmp(dbus_message_get_signature(message), "ssv")) {
195 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
199 if (dsc->setter == NULL) {
200 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
201 "Property is read-only");
204 dbus_message_iter_init(message, &iter);
205 /* Skip the interface name and the property name */
206 dbus_message_iter_next(&iter);
207 dbus_message_iter_next(&iter);
209 /* Iter will now point to the property's new value */
210 dbus_error_init(&error);
211 if (dsc->setter(&iter, &error, user_data) == TRUE) {
213 reply = dbus_message_new_method_return(message);
215 reply = wpas_dbus_reply_new_from_error(
216 message, &error, DBUS_ERROR_FAILED,
217 "Failed to set property");
218 dbus_error_free(&error);
226 properties_get_or_set(DBusMessage *message, DBusMessageIter *iter,
228 struct wpa_dbus_object_desc *obj_dsc)
230 const struct wpa_dbus_property_desc *property_dsc;
234 method = dbus_message_get_member(message);
235 property_dsc = obj_dsc->properties;
237 /* Second argument: property name (DBUS_TYPE_STRING) */
238 if (!dbus_message_iter_next(iter) ||
239 dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING) {
240 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
243 dbus_message_iter_get_basic(iter, &property);
245 while (property_dsc && property_dsc->dbus_property) {
246 /* compare property names and
248 if (!os_strncmp(property_dsc->dbus_property, property,
249 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
250 !os_strncmp(property_dsc->dbus_interface, interface,
251 WPAS_DBUS_INTERFACE_MAX))
256 if (property_dsc == NULL || property_dsc->dbus_property == NULL) {
257 wpa_printf(MSG_DEBUG, "no property handler for %s.%s on %s",
259 dbus_message_get_path(message));
260 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
264 if (os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
265 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) == 0) {
266 wpa_printf(MSG_MSGDUMP, "%s: Get(%s)", __func__, property);
267 return properties_get(message, property_dsc,
271 wpa_printf(MSG_MSGDUMP, "%s: Set(%s)", __func__, property);
272 return properties_set(message, property_dsc, obj_dsc->user_data);
276 static DBusMessage * properties_handler(DBusMessage *message,
277 struct wpa_dbus_object_desc *obj_dsc)
279 DBusMessageIter iter;
283 method = dbus_message_get_member(message);
284 dbus_message_iter_init(message, &iter);
286 if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
287 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
288 !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
289 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
290 !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
291 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
292 /* First argument: interface name (DBUS_TYPE_STRING) */
293 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
294 return dbus_message_new_error(message,
295 DBUS_ERROR_INVALID_ARGS,
299 dbus_message_iter_get_basic(&iter, &interface);
301 if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
302 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
304 return properties_get_all(message, interface, obj_dsc);
307 return properties_get_or_set(message, &iter, interface,
310 return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
315 static DBusMessage * msg_method_handler(DBusMessage *message,
316 struct wpa_dbus_object_desc *obj_dsc)
318 const struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
320 const char *msg_interface;
322 method = dbus_message_get_member(message);
323 msg_interface = dbus_message_get_interface(message);
325 /* try match call to any registered method */
326 while (method_dsc && method_dsc->dbus_method) {
327 /* compare method names and interfaces */
328 if (!os_strncmp(method_dsc->dbus_method, method,
329 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
330 !os_strncmp(method_dsc->dbus_interface, msg_interface,
331 WPAS_DBUS_INTERFACE_MAX))
336 if (method_dsc == NULL || method_dsc->dbus_method == NULL) {
337 wpa_printf(MSG_DEBUG, "no method handler for %s.%s on %s",
338 msg_interface, method,
339 dbus_message_get_path(message));
340 return dbus_message_new_error(message,
341 DBUS_ERROR_UNKNOWN_METHOD, NULL);
344 if (!is_signature_correct(message, method_dsc)) {
345 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
349 return method_dsc->method_handler(message, obj_dsc->user_data);
354 * message_handler - Handles incoming DBus messages
355 * @connection: DBus connection on which message was received
356 * @message: Received message
357 * @user_data: pointer to description of object to which message was sent
358 * Returns: Returns information whether message was handled or not
360 * Reads message interface and method name, then checks if they matches one
361 * of the special cases i.e. introspection call or properties get/getall/set
362 * methods and handles it. Else it iterates over registered methods list
363 * and tries to match method's name and interface to those read from message
364 * If appropriate method was found its handler function is called and
365 * response is sent. Otherwise, the DBUS_ERROR_UNKNOWN_METHOD error message
368 static DBusHandlerResult message_handler(DBusConnection *connection,
369 DBusMessage *message, void *user_data)
371 struct wpa_dbus_object_desc *obj_dsc = user_data;
374 const char *msg_interface;
377 /* get method, interface and path the message is addressed to */
378 method = dbus_message_get_member(message);
379 path = dbus_message_get_path(message);
380 msg_interface = dbus_message_get_interface(message);
381 if (!method || !path || !msg_interface)
382 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
384 wpa_printf(MSG_MSGDUMP, "dbus: %s.%s (%s) [%s]",
385 msg_interface, method, path,
386 dbus_message_get_signature(message));
388 /* if message is introspection method call */
389 if (!os_strncmp(WPA_DBUS_INTROSPECTION_METHOD, method,
390 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
391 !os_strncmp(WPA_DBUS_INTROSPECTION_INTERFACE, msg_interface,
392 WPAS_DBUS_INTERFACE_MAX)) {
393 #ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
394 reply = wpa_dbus_introspect(message, obj_dsc);
395 #else /* CONFIG_CTRL_IFACE_DBUS_INTRO */
396 reply = dbus_message_new_error(
397 message, DBUS_ERROR_UNKNOWN_METHOD,
398 "wpa_supplicant was compiled without introspection support.");
399 #endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
400 } else if (!os_strncmp(WPA_DBUS_PROPERTIES_INTERFACE, msg_interface,
401 WPAS_DBUS_INTERFACE_MAX)) {
402 /* if message is properties method call */
403 reply = properties_handler(message, obj_dsc);
405 reply = msg_method_handler(message, obj_dsc);
408 /* If handler succeed returning NULL, reply empty message */
410 reply = dbus_message_new_method_return(message);
412 if (!dbus_message_get_no_reply(message))
413 dbus_connection_send(connection, reply, NULL);
414 dbus_message_unref(reply);
417 wpa_dbus_flush_all_changed_properties(connection);
419 return DBUS_HANDLER_RESULT_HANDLED;
424 * free_dbus_object_desc - Frees object description data structure
425 * @connection: DBus connection
426 * @obj_dsc: Object description to free
428 * Frees each of properties, methods and signals description lists and
429 * the object description structure itself.
431 void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
436 /* free handler's argument */
437 if (obj_dsc->user_data_free_func)
438 obj_dsc->user_data_free_func(obj_dsc->user_data);
440 os_free(obj_dsc->path);
441 os_free(obj_dsc->prop_changed_flags);
446 static void free_dbus_object_desc_cb(DBusConnection *connection, void *obj_dsc)
448 free_dbus_object_desc(obj_dsc);
453 * wpa_dbus_ctrl_iface_init - Initialize dbus control interface
454 * @application_data: Pointer to application specific data structure
455 * @dbus_path: DBus path to interface object
456 * @dbus_service: DBus service name to register with
457 * @messageHandler: a pointer to function which will handle dbus messages
458 * coming on interface
459 * Returns: 0 on success, -1 on failure
461 * Initialize the dbus control interface and start receiving commands from
462 * external programs over the bus.
464 int wpa_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface,
465 char *dbus_path, char *dbus_service,
466 struct wpa_dbus_object_desc *obj_desc)
470 DBusObjectPathVTable wpa_vtable = {
471 &free_dbus_object_desc_cb, &message_handler,
472 NULL, NULL, NULL, NULL
475 obj_desc->connection = iface->con;
476 obj_desc->path = os_strdup(dbus_path);
478 /* Register the message handler for the global dbus interface */
479 if (!dbus_connection_register_object_path(iface->con, dbus_path,
480 &wpa_vtable, obj_desc)) {
481 wpa_printf(MSG_ERROR, "dbus: Could not set up message handler");
485 /* Register our service with the message bus */
486 dbus_error_init(&error);
487 switch (dbus_bus_request_name(iface->con, dbus_service, 0, &error)) {
488 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
491 case DBUS_REQUEST_NAME_REPLY_EXISTS:
492 case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
493 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
494 wpa_printf(MSG_ERROR,
495 "dbus: Could not request service name: already registered");
498 wpa_printf(MSG_ERROR,
499 "dbus: Could not request service name: %s %s",
500 error.name, error.message);
503 dbus_error_free(&error);
508 wpa_printf(MSG_DEBUG, "Providing DBus service '%s'.", dbus_service);
515 * wpa_dbus_register_object_per_iface - Register a new object with dbus
516 * @ctrl_iface: pointer to dbus private data
517 * @path: DBus path to object
518 * @ifname: interface name
519 * @obj_desc: description of object's methods, signals and properties
520 * Returns: 0 on success, -1 on error
522 * Registers a new interface with dbus and assigns it a dbus object path.
524 int wpa_dbus_register_object_per_iface(struct wpas_dbus_priv *ctrl_iface,
525 const char *path, const char *ifname,
526 struct wpa_dbus_object_desc *obj_desc)
530 DBusObjectPathVTable vtable = {
531 &free_dbus_object_desc_cb, &message_handler,
532 NULL, NULL, NULL, NULL
535 /* Do nothing if the control interface is not turned on */
536 if (ctrl_iface == NULL)
539 con = ctrl_iface->con;
540 obj_desc->connection = con;
541 obj_desc->path = os_strdup(path);
543 dbus_error_init(&error);
544 /* Register the message handler for the interface functions */
545 if (!dbus_connection_try_register_object_path(con, path, &vtable,
547 if (os_strcmp(error.name, DBUS_ERROR_OBJECT_PATH_IN_USE) == 0) {
548 wpa_printf(MSG_DEBUG, "dbus: %s", error.message);
550 wpa_printf(MSG_ERROR,
551 "dbus: Could not set up message handler for interface %s object %s (error: %s message: %s)",
552 ifname, path, error.name, error.message);
554 dbus_error_free(&error);
558 dbus_error_free(&error);
563 static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx);
567 * wpa_dbus_unregister_object_per_iface - Unregisters DBus object
568 * @ctrl_iface: Pointer to dbus private data
569 * @path: DBus path to object which will be unregistered
570 * Returns: Zero on success and -1 on failure
572 * Unregisters DBus object given by its path
574 int wpa_dbus_unregister_object_per_iface(
575 struct wpas_dbus_priv *ctrl_iface, const char *path)
577 DBusConnection *con = ctrl_iface->con;
578 struct wpa_dbus_object_desc *obj_desc = NULL;
580 dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
582 wpa_printf(MSG_ERROR,
583 "dbus: %s: Could not obtain object's private data: %s",
588 eloop_cancel_timeout(flush_object_timeout_handler, con, obj_desc);
590 if (!dbus_connection_unregister_object_path(con, path))
597 static dbus_bool_t put_changed_properties(
598 const struct wpa_dbus_object_desc *obj_dsc, const char *interface,
599 DBusMessageIter *dict_iter, int clear_changed)
601 DBusMessageIter entry_iter;
602 const struct wpa_dbus_property_desc *dsc;
606 for (dsc = obj_dsc->properties, i = 0; dsc && dsc->dbus_property;
608 if (obj_dsc->prop_changed_flags == NULL ||
609 !obj_dsc->prop_changed_flags[i])
611 if (os_strcmp(dsc->dbus_interface, interface) != 0)
614 obj_dsc->prop_changed_flags[i] = 0;
616 if (!dbus_message_iter_open_container(dict_iter,
617 DBUS_TYPE_DICT_ENTRY,
618 NULL, &entry_iter) ||
619 !dbus_message_iter_append_basic(&entry_iter,
621 &dsc->dbus_property))
624 dbus_error_init(&error);
625 if (!dsc->getter(&entry_iter, &error, obj_dsc->user_data)) {
626 if (dbus_error_is_set(&error)) {
627 wpa_printf(MSG_ERROR,
628 "dbus: %s: Cannot get new value of property %s: (%s) %s",
629 __func__, dsc->dbus_property,
630 error.name, error.message);
632 wpa_printf(MSG_ERROR,
633 "dbus: %s: Cannot get new value of property %s",
634 __func__, dsc->dbus_property);
636 dbus_error_free(&error);
640 if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
648 static void do_send_prop_changed_signal(
649 DBusConnection *con, const char *path, const char *interface,
650 const struct wpa_dbus_object_desc *obj_dsc)
653 DBusMessageIter signal_iter, dict_iter;
655 msg = dbus_message_new_signal(path, DBUS_INTERFACE_PROPERTIES,
656 "PropertiesChanged");
660 dbus_message_iter_init_append(msg, &signal_iter);
662 if (!dbus_message_iter_append_basic(&signal_iter, DBUS_TYPE_STRING,
664 /* Changed properties dict */
665 !dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
666 "{sv}", &dict_iter) ||
667 !put_changed_properties(obj_dsc, interface, &dict_iter, 0) ||
668 !dbus_message_iter_close_container(&signal_iter, &dict_iter) ||
669 /* Invalidated properties array (empty) */
670 !dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
672 !dbus_message_iter_close_container(&signal_iter, &dict_iter)) {
673 wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
676 dbus_connection_send(con, msg, NULL);
679 dbus_message_unref(msg);
683 static void do_send_deprecated_prop_changed_signal(
684 DBusConnection *con, const char *path, const char *interface,
685 const struct wpa_dbus_object_desc *obj_dsc)
688 DBusMessageIter signal_iter, dict_iter;
690 msg = dbus_message_new_signal(path, interface, "PropertiesChanged");
694 dbus_message_iter_init_append(msg, &signal_iter);
696 if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
697 "{sv}", &dict_iter) ||
698 !put_changed_properties(obj_dsc, interface, &dict_iter, 1) ||
699 !dbus_message_iter_close_container(&signal_iter, &dict_iter)) {
700 wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
703 dbus_connection_send(con, msg, NULL);
706 dbus_message_unref(msg);
710 static void send_prop_changed_signal(
711 DBusConnection *con, const char *path, const char *interface,
712 const struct wpa_dbus_object_desc *obj_dsc)
715 * First, send property change notification on the standardized
716 * org.freedesktop.DBus.Properties interface. This call will not
717 * clear the property change bits, so that they are preserved for
718 * the call that follows.
720 do_send_prop_changed_signal(con, path, interface, obj_dsc);
723 * Now send PropertiesChanged on our own interface for backwards
724 * compatibility. This is deprecated and will be removed in a future
727 do_send_deprecated_prop_changed_signal(con, path, interface, obj_dsc);
729 /* Property change bits have now been cleared. */
733 static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx)
735 DBusConnection *con = eloop_ctx;
736 struct wpa_dbus_object_desc *obj_desc = timeout_ctx;
738 wpa_printf(MSG_DEBUG,
739 "dbus: %s: Timeout - sending changed properties of object %s",
740 __func__, obj_desc->path);
741 wpa_dbus_flush_object_changed_properties(con, obj_desc->path);
745 static void recursive_flush_changed_properties(DBusConnection *con,
748 char **objects = NULL;
749 char subobj_path[WPAS_DBUS_OBJECT_PATH_MAX];
752 wpa_dbus_flush_object_changed_properties(con, path);
754 if (!dbus_connection_list_registered(con, path, &objects))
757 for (i = 0; objects[i]; i++) {
758 os_snprintf(subobj_path, WPAS_DBUS_OBJECT_PATH_MAX,
759 "%s/%s", path, objects[i]);
760 recursive_flush_changed_properties(con, subobj_path);
764 dbus_free_string_array(objects);
769 * wpa_dbus_flush_all_changed_properties - Send all PropertiesChanged signals
770 * @con: DBus connection
772 * Traverses through all registered objects and sends PropertiesChanged for
775 void wpa_dbus_flush_all_changed_properties(DBusConnection *con)
777 recursive_flush_changed_properties(con, WPAS_DBUS_NEW_PATH);
782 * wpa_dbus_flush_object_changed_properties - Send PropertiesChanged for object
783 * @con: DBus connection
784 * @path: path to a DBus object for which PropertiesChanged will be sent.
786 * Iterates over all properties registered with object and for each interface
787 * containing properties marked as changed, sends a PropertiesChanged signal
788 * containing names and new values of properties that have changed.
790 * You need to call this function after wpa_dbus_mark_property_changed()
791 * if you want to send PropertiesChanged signal immediately (i.e., without
792 * waiting timeout to expire). PropertiesChanged signal for an object is sent
793 * automatically short time after first marking property as changed. All
794 * PropertiesChanged signals are sent automatically after responding on DBus
795 * message, so if you marked a property changed as a result of DBus call
796 * (e.g., param setter), you usually do not need to call this function.
798 void wpa_dbus_flush_object_changed_properties(DBusConnection *con,
801 struct wpa_dbus_object_desc *obj_desc = NULL;
802 const struct wpa_dbus_property_desc *dsc;
805 dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
808 eloop_cancel_timeout(flush_object_timeout_handler, con, obj_desc);
810 for (dsc = obj_desc->properties, i = 0; dsc && dsc->dbus_property;
812 if (obj_desc->prop_changed_flags == NULL ||
813 !obj_desc->prop_changed_flags[i])
815 send_prop_changed_signal(con, path, dsc->dbus_interface,
821 #define WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT 5000
825 * wpa_dbus_mark_property_changed - Mark a property as changed and
826 * @iface: dbus priv struct
827 * @path: path to DBus object which property has changed
828 * @interface: interface containing changed property
829 * @property: property name which has changed
831 * Iterates over all properties registered with an object and marks the one
832 * given in parameters as changed. All parameters registered for an object
833 * within a single interface will be aggregated together and sent in one
834 * PropertiesChanged signal when function
835 * wpa_dbus_flush_object_changed_properties() is called.
837 void wpa_dbus_mark_property_changed(struct wpas_dbus_priv *iface,
838 const char *path, const char *interface,
839 const char *property)
841 struct wpa_dbus_object_desc *obj_desc = NULL;
842 const struct wpa_dbus_property_desc *dsc;
848 dbus_connection_get_object_path_data(iface->con, path,
849 (void **) &obj_desc);
851 wpa_printf(MSG_ERROR,
852 "dbus: wpa_dbus_property_changed: could not obtain object's private data: %s",
857 for (dsc = obj_desc->properties; dsc && dsc->dbus_property; dsc++, i++)
858 if (os_strcmp(property, dsc->dbus_property) == 0 &&
859 os_strcmp(interface, dsc->dbus_interface) == 0) {
860 if (obj_desc->prop_changed_flags)
861 obj_desc->prop_changed_flags[i] = 1;
865 if (!dsc || !dsc->dbus_property) {
866 wpa_printf(MSG_ERROR,
867 "dbus: wpa_dbus_property_changed: no property %s in object %s",
872 if (!eloop_is_timeout_registered(flush_object_timeout_handler,
873 iface->con, obj_desc)) {
874 eloop_register_timeout(0, WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT,
875 flush_object_timeout_handler,
876 iface->con, obj_desc);
882 * wpa_dbus_get_object_properties - Put object's properties into dictionary
883 * @iface: dbus priv struct
884 * @path: path to DBus object which properties will be obtained
885 * @interface: interface name which properties will be obtained
886 * @iter: DBus message iter at which to append property dictionary.
888 * Iterates over all properties registered with object and execute getters
889 * of those, which are readable and which interface matches interface
890 * specified as argument. Obtained properties values are stored in
891 * dict_iter dictionary.
893 dbus_bool_t wpa_dbus_get_object_properties(struct wpas_dbus_priv *iface,
895 const char *interface,
896 DBusMessageIter *iter)
898 struct wpa_dbus_object_desc *obj_desc = NULL;
899 DBusMessageIter dict_iter;
902 dbus_connection_get_object_path_data(iface->con, path,
903 (void **) &obj_desc);
905 wpa_printf(MSG_ERROR,
906 "dbus: %s: could not obtain object's private data: %s",
911 if (!wpa_dbus_dict_open_write(iter, &dict_iter)) {
912 wpa_printf(MSG_ERROR, "dbus: %s: failed to open message dict",
917 dbus_error_init(&error);
918 if (!fill_dict_with_properties(&dict_iter, obj_desc->properties,
919 interface, obj_desc->user_data,
921 wpa_printf(MSG_ERROR,
922 "dbus: %s: failed to get object properties: (%s) %s",
924 dbus_error_is_set(&error) ? error.name : "none",
925 dbus_error_is_set(&error) ? error.message : "none");
926 dbus_error_free(&error);
930 return wpa_dbus_dict_close_write(iter, &dict_iter);
934 * wpas_dbus_new_decompose_object_path - Decompose an interface object path into parts
935 * @path: The dbus object path
936 * @sep: Separating part (e.g., "Networks" or "PersistentGroups")
937 * @item: (out) The part following the specified separator, if any
938 * Returns: The object path of the interface this path refers to
940 * For a given object path, decomposes the object path into object id and
941 * requested part, if those parts exist. The caller is responsible for freeing
942 * the returned value. The *item pointer points to that allocated value and must
943 * not be freed separately.
945 * As an example, path = "/fi/w1/wpa_supplicant1/Interfaces/1/Networks/0" and
946 * sep = "Networks" would result in "/fi/w1/wpa_supplicant1/Interfaces/1"
947 * getting returned and *items set to point to "0".
949 char * wpas_dbus_new_decompose_object_path(const char *path, const char *sep,
952 const unsigned int dev_path_prefix_len =
953 os_strlen(WPAS_DBUS_NEW_PATH_INTERFACES "/");
960 /* Verify that this starts with our interface prefix */
961 if (os_strncmp(path, WPAS_DBUS_NEW_PATH_INTERFACES "/",
962 dev_path_prefix_len) != 0)
963 return NULL; /* not our path */
965 /* Ensure there's something at the end of the path */
966 if ((path + dev_path_prefix_len)[0] == '\0')
969 obj_path_only = os_strdup(path);
970 if (obj_path_only == NULL)
973 pos = obj_path_only + dev_path_prefix_len;
974 pos = os_strchr(pos, '/');
976 return obj_path_only; /* no next item on the path */
978 /* Separate network interface prefix from the path */
981 sep_len = os_strlen(sep);
982 if (os_strncmp(pos, sep, sep_len) != 0 || pos[sep_len] != '/')
983 return obj_path_only; /* no match */
985 /* return a pointer to the requested item */
986 *item = pos + sep_len + 1;
987 return obj_path_only;
992 * wpas_dbus_reply_new_from_error - Create a new D-Bus error message from a
993 * dbus error structure
994 * @message: The original request message for which the error is a reply
995 * @error: The error containing a name and a descriptive error cause
996 * @fallback_name: A generic error name if @error was not set
997 * @fallback_string: A generic error string if @error was not set
998 * Returns: A new D-Bus error message
1000 * Given a DBusMessage structure, creates a new D-Bus error message using
1001 * the error name and string contained in that structure.
1003 DBusMessage * wpas_dbus_reply_new_from_error(DBusMessage *message,
1005 const char *fallback_name,
1006 const char *fallback_string)
1008 if (error && error->name && error->message) {
1009 return dbus_message_new_error(message, error->name,
1012 if (fallback_name && fallback_string) {
1013 return dbus_message_new_error(message, fallback_name,