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_dict_helpers.h"
21 static dbus_bool_t fill_dict_with_properties(
22 DBusMessageIter *dict_iter,
23 const struct wpa_dbus_property_desc *props,
24 const char *interface, void *user_data, DBusError *error)
26 DBusMessageIter entry_iter;
27 const struct wpa_dbus_property_desc *dsc;
29 for (dsc = props; dsc && dsc->dbus_property; dsc++) {
30 /* Only return properties for the requested D-Bus interface */
31 if (os_strncmp(dsc->dbus_interface, interface,
32 WPAS_DBUS_INTERFACE_MAX) != 0)
35 /* Skip write-only properties */
36 if (dsc->getter == NULL)
39 if (!dbus_message_iter_open_container(dict_iter,
42 !dbus_message_iter_append_basic(&entry_iter,
47 /* An error getting a property fails the request entirely */
48 if (!dsc->getter(&entry_iter, error, user_data))
51 if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
58 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
64 * get_all_properties - Responds for GetAll properties calls on object
65 * @message: Message with GetAll call
66 * @interface: interface name which properties will be returned
67 * @property_dsc: list of object's properties
68 * Returns: Message with dict of variants as argument with properties values
70 * Iterates over all properties registered with object and execute getters
71 * of those, which are readable and which interface matches interface
72 * specified as argument. Returned message contains one dict argument
73 * with properties names as keys and theirs values as values.
75 static DBusMessage * get_all_properties(DBusMessage *message, char *interface,
76 struct wpa_dbus_object_desc *obj_dsc)
79 DBusMessageIter iter, dict_iter;
82 reply = dbus_message_new_method_return(message);
84 wpa_printf(MSG_ERROR, "%s: out of memory creating dbus reply",
89 dbus_message_iter_init_append(reply, &iter);
90 if (!wpa_dbus_dict_open_write(&iter, &dict_iter)) {
91 wpa_printf(MSG_ERROR, "%s: out of memory creating reply",
93 dbus_message_unref(reply);
94 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
99 dbus_error_init(&error);
100 if (!fill_dict_with_properties(&dict_iter, obj_dsc->properties,
101 interface, obj_dsc->user_data, &error))
103 dbus_message_unref(reply);
104 reply = wpas_dbus_reply_new_from_error(message, &error,
105 DBUS_ERROR_INVALID_ARGS,
106 "No readable properties"
107 " in this interface");
108 dbus_error_free(&error);
112 if (!wpa_dbus_dict_close_write(&iter, &dict_iter)) {
113 dbus_message_unref(reply);
114 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
122 static int is_signature_correct(DBusMessage *message,
123 const struct wpa_dbus_method_desc *method_dsc)
125 /* According to DBus documentation max length of signature is 255 */
126 #define MAX_SIG_LEN 256
127 char registered_sig[MAX_SIG_LEN], *pos;
128 const char *sig = dbus_message_get_signature(message);
130 const struct wpa_dbus_argument *arg;
132 pos = registered_sig;
135 for (arg = method_dsc->args; arg && arg->name; arg++) {
136 if (arg->dir == ARG_IN) {
137 size_t blen = registered_sig + MAX_SIG_LEN - pos;
138 ret = os_snprintf(pos, blen, "%s", arg->type);
139 if (os_snprintf_error(blen, ret))
145 return !os_strncmp(registered_sig, sig, MAX_SIG_LEN);
149 static DBusMessage * properties_get_all(DBusMessage *message, char *interface,
150 struct wpa_dbus_object_desc *obj_dsc)
152 if (os_strcmp(dbus_message_get_signature(message), "s") != 0)
153 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
156 return get_all_properties(message, interface, obj_dsc);
160 static DBusMessage * properties_get(DBusMessage *message,
161 const struct wpa_dbus_property_desc *dsc,
165 DBusMessageIter iter;
168 if (os_strcmp(dbus_message_get_signature(message), "ss")) {
169 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
173 if (dsc->getter == NULL) {
174 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
175 "Property is write-only");
178 reply = dbus_message_new_method_return(message);
179 dbus_message_iter_init_append(reply, &iter);
181 dbus_error_init(&error);
182 if (dsc->getter(&iter, &error, user_data) == FALSE) {
183 dbus_message_unref(reply);
184 reply = wpas_dbus_reply_new_from_error(
185 message, &error, DBUS_ERROR_FAILED,
186 "Failed to read property");
187 dbus_error_free(&error);
194 static DBusMessage * properties_set(DBusMessage *message,
195 const struct wpa_dbus_property_desc *dsc,
199 DBusMessageIter iter;
202 if (os_strcmp(dbus_message_get_signature(message), "ssv")) {
203 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
207 if (dsc->setter == NULL) {
208 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
209 "Property is read-only");
212 dbus_message_iter_init(message, &iter);
213 /* Skip the interface name and the property name */
214 dbus_message_iter_next(&iter);
215 dbus_message_iter_next(&iter);
217 /* Iter will now point to the property's new value */
218 dbus_error_init(&error);
219 if (dsc->setter(&iter, &error, user_data) == TRUE) {
221 reply = dbus_message_new_method_return(message);
223 reply = wpas_dbus_reply_new_from_error(
224 message, &error, DBUS_ERROR_FAILED,
225 "Failed to set property");
226 dbus_error_free(&error);
234 properties_get_or_set(DBusMessage *message, DBusMessageIter *iter,
236 struct wpa_dbus_object_desc *obj_dsc)
238 const struct wpa_dbus_property_desc *property_dsc;
242 method = dbus_message_get_member(message);
243 property_dsc = obj_dsc->properties;
245 /* Second argument: property name (DBUS_TYPE_STRING) */
246 if (!dbus_message_iter_next(iter) ||
247 dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING) {
248 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
251 dbus_message_iter_get_basic(iter, &property);
253 while (property_dsc && property_dsc->dbus_property) {
254 /* compare property names and
256 if (!os_strncmp(property_dsc->dbus_property, property,
257 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
258 !os_strncmp(property_dsc->dbus_interface, interface,
259 WPAS_DBUS_INTERFACE_MAX))
264 if (property_dsc == NULL || property_dsc->dbus_property == NULL) {
265 wpa_printf(MSG_DEBUG, "no property handler for %s.%s on %s",
267 dbus_message_get_path(message));
268 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
272 if (os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
273 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) == 0) {
274 wpa_printf(MSG_MSGDUMP, "%s: Get(%s)", __func__, property);
275 return properties_get(message, property_dsc,
279 wpa_printf(MSG_MSGDUMP, "%s: Set(%s)", __func__, property);
280 return properties_set(message, property_dsc, obj_dsc->user_data);
284 static DBusMessage * properties_handler(DBusMessage *message,
285 struct wpa_dbus_object_desc *obj_dsc)
287 DBusMessageIter iter;
291 method = dbus_message_get_member(message);
292 dbus_message_iter_init(message, &iter);
294 if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
295 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
296 !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
297 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
298 !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
299 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
300 /* First argument: interface name (DBUS_TYPE_STRING) */
301 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
303 return dbus_message_new_error(message,
304 DBUS_ERROR_INVALID_ARGS,
308 dbus_message_iter_get_basic(&iter, &interface);
310 if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
311 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
313 return properties_get_all(message, interface, obj_dsc);
316 return properties_get_or_set(message, &iter, interface,
319 return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
324 static DBusMessage * msg_method_handler(DBusMessage *message,
325 struct wpa_dbus_object_desc *obj_dsc)
327 const struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
329 const char *msg_interface;
331 method = dbus_message_get_member(message);
332 msg_interface = dbus_message_get_interface(message);
334 /* try match call to any registered method */
335 while (method_dsc && method_dsc->dbus_method) {
336 /* compare method names and interfaces */
337 if (!os_strncmp(method_dsc->dbus_method, method,
338 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
339 !os_strncmp(method_dsc->dbus_interface, msg_interface,
340 WPAS_DBUS_INTERFACE_MAX))
345 if (method_dsc == NULL || method_dsc->dbus_method == NULL) {
346 wpa_printf(MSG_DEBUG, "no method handler for %s.%s on %s",
347 msg_interface, method,
348 dbus_message_get_path(message));
349 return dbus_message_new_error(message,
350 DBUS_ERROR_UNKNOWN_METHOD, NULL);
353 if (!is_signature_correct(message, method_dsc)) {
354 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
358 return method_dsc->method_handler(message,
364 * message_handler - Handles incoming DBus messages
365 * @connection: DBus connection on which message was received
366 * @message: Received message
367 * @user_data: pointer to description of object to which message was sent
368 * Returns: Returns information whether message was handled or not
370 * Reads message interface and method name, then checks if they matches one
371 * of the special cases i.e. introspection call or properties get/getall/set
372 * methods and handles it. Else it iterates over registered methods list
373 * and tries to match method's name and interface to those read from message
374 * If appropriate method was found its handler function is called and
375 * response is sent. Otherwise, the DBUS_ERROR_UNKNOWN_METHOD error message
378 static DBusHandlerResult message_handler(DBusConnection *connection,
379 DBusMessage *message, void *user_data)
381 struct wpa_dbus_object_desc *obj_dsc = user_data;
384 const char *msg_interface;
387 /* get method, interface and path the message is addressed to */
388 method = dbus_message_get_member(message);
389 path = dbus_message_get_path(message);
390 msg_interface = dbus_message_get_interface(message);
391 if (!method || !path || !msg_interface)
392 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
394 wpa_printf(MSG_MSGDUMP, "dbus: %s.%s (%s) [%s]",
395 msg_interface, method, path,
396 dbus_message_get_signature(message));
398 /* if message is introspection method call */
399 if (!os_strncmp(WPA_DBUS_INTROSPECTION_METHOD, method,
400 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
401 !os_strncmp(WPA_DBUS_INTROSPECTION_INTERFACE, msg_interface,
402 WPAS_DBUS_INTERFACE_MAX)) {
403 #ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
404 reply = wpa_dbus_introspect(message, obj_dsc);
405 #else /* CONFIG_CTRL_IFACE_DBUS_INTRO */
406 reply = dbus_message_new_error(
407 message, DBUS_ERROR_UNKNOWN_METHOD,
408 "wpa_supplicant was compiled without "
409 "introspection support.");
410 #endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
411 } else if (!os_strncmp(WPA_DBUS_PROPERTIES_INTERFACE, msg_interface,
412 WPAS_DBUS_INTERFACE_MAX)) {
413 /* if message is properties method call */
414 reply = properties_handler(message, obj_dsc);
416 reply = msg_method_handler(message, obj_dsc);
419 /* If handler succeed returning NULL, reply empty message */
421 reply = dbus_message_new_method_return(message);
423 if (!dbus_message_get_no_reply(message))
424 dbus_connection_send(connection, reply, NULL);
425 dbus_message_unref(reply);
428 wpa_dbus_flush_all_changed_properties(connection);
430 return DBUS_HANDLER_RESULT_HANDLED;
435 * free_dbus_object_desc - Frees object description data structure
436 * @connection: DBus connection
437 * @obj_dsc: Object description to free
439 * Frees each of properties, methods and signals description lists and
440 * the object description structure itself.
442 void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
447 /* free handler's argument */
448 if (obj_dsc->user_data_free_func)
449 obj_dsc->user_data_free_func(obj_dsc->user_data);
451 os_free(obj_dsc->path);
452 os_free(obj_dsc->prop_changed_flags);
457 static void free_dbus_object_desc_cb(DBusConnection *connection, void *obj_dsc)
459 free_dbus_object_desc(obj_dsc);
463 * wpa_dbus_ctrl_iface_init - Initialize dbus control interface
464 * @application_data: Pointer to application specific data structure
465 * @dbus_path: DBus path to interface object
466 * @dbus_service: DBus service name to register with
467 * @messageHandler: a pointer to function which will handle dbus messages
468 * coming on interface
469 * Returns: 0 on success, -1 on failure
471 * Initialize the dbus control interface and start receiving commands from
472 * external programs over the bus.
474 int wpa_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface,
475 char *dbus_path, char *dbus_service,
476 struct wpa_dbus_object_desc *obj_desc)
480 DBusObjectPathVTable wpa_vtable = {
481 &free_dbus_object_desc_cb, &message_handler,
482 NULL, NULL, NULL, NULL
485 obj_desc->connection = iface->con;
486 obj_desc->path = os_strdup(dbus_path);
488 /* Register the message handler for the global dbus interface */
489 if (!dbus_connection_register_object_path(iface->con,
490 dbus_path, &wpa_vtable,
492 wpa_printf(MSG_ERROR, "dbus: Could not set up message "
497 /* Register our service with the message bus */
498 dbus_error_init(&error);
499 switch (dbus_bus_request_name(iface->con, dbus_service,
501 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
504 case DBUS_REQUEST_NAME_REPLY_EXISTS:
505 case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
506 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
507 wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
508 "already registered");
511 wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
512 "%s %s", error.name, error.message);
515 dbus_error_free(&error);
520 wpa_printf(MSG_DEBUG, "Providing DBus service '%s'.", dbus_service);
527 * wpa_dbus_register_object_per_iface - Register a new object with dbus
528 * @ctrl_iface: pointer to dbus private data
529 * @path: DBus path to object
530 * @ifname: interface name
531 * @obj_desc: description of object's methods, signals and properties
532 * Returns: 0 on success, -1 on error
534 * Registers a new interface with dbus and assigns it a dbus object path.
536 int wpa_dbus_register_object_per_iface(
537 struct wpas_dbus_priv *ctrl_iface,
538 const char *path, const char *ifname,
539 struct wpa_dbus_object_desc *obj_desc)
544 DBusObjectPathVTable vtable = {
545 &free_dbus_object_desc_cb, &message_handler,
546 NULL, NULL, NULL, NULL
549 /* Do nothing if the control interface is not turned on */
550 if (ctrl_iface == NULL)
553 con = ctrl_iface->con;
554 obj_desc->connection = con;
555 obj_desc->path = os_strdup(path);
557 dbus_error_init(&error);
558 /* Register the message handler for the interface functions */
559 if (!dbus_connection_try_register_object_path(con, path, &vtable,
561 if (!os_strcmp(error.name, DBUS_ERROR_OBJECT_PATH_IN_USE)) {
562 wpa_printf(MSG_DEBUG, "dbus: %s", error.message);
564 wpa_printf(MSG_ERROR, "dbus: Could not set up message "
565 "handler for interface %s object %s",
567 wpa_printf(MSG_ERROR, "dbus error: %s", error.name);
568 wpa_printf(MSG_ERROR, "dbus: %s", error.message);
570 dbus_error_free(&error);
574 dbus_error_free(&error);
579 static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx);
583 * wpa_dbus_unregister_object_per_iface - Unregisters DBus object
584 * @ctrl_iface: Pointer to dbus private data
585 * @path: DBus path to object which will be unregistered
586 * Returns: Zero on success and -1 on failure
588 * Unregisters DBus object given by its path
590 int wpa_dbus_unregister_object_per_iface(
591 struct wpas_dbus_priv *ctrl_iface, const char *path)
593 DBusConnection *con = ctrl_iface->con;
594 struct wpa_dbus_object_desc *obj_desc = NULL;
596 dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
598 wpa_printf(MSG_ERROR, "dbus: %s: Could not obtain object's "
599 "private data: %s", __func__, path);
603 eloop_cancel_timeout(flush_object_timeout_handler, con, obj_desc);
605 if (!dbus_connection_unregister_object_path(con, path))
612 static dbus_bool_t put_changed_properties(
613 const struct wpa_dbus_object_desc *obj_dsc, const char *interface,
614 DBusMessageIter *dict_iter, int clear_changed)
616 DBusMessageIter entry_iter;
617 const struct wpa_dbus_property_desc *dsc;
621 for (dsc = obj_dsc->properties, i = 0; dsc && dsc->dbus_property;
623 if (obj_dsc->prop_changed_flags == NULL ||
624 !obj_dsc->prop_changed_flags[i])
626 if (os_strcmp(dsc->dbus_interface, interface) != 0)
629 obj_dsc->prop_changed_flags[i] = 0;
631 if (!dbus_message_iter_open_container(dict_iter,
632 DBUS_TYPE_DICT_ENTRY,
633 NULL, &entry_iter) ||
634 !dbus_message_iter_append_basic(&entry_iter,
636 &dsc->dbus_property))
639 dbus_error_init(&error);
640 if (!dsc->getter(&entry_iter, &error, obj_dsc->user_data)) {
641 if (dbus_error_is_set (&error)) {
642 wpa_printf(MSG_ERROR, "dbus: %s: Cannot get "
643 "new value of property %s: (%s) %s",
644 __func__, dsc->dbus_property,
645 error.name, error.message);
647 wpa_printf(MSG_ERROR, "dbus: %s: Cannot get "
648 "new value of property %s",
649 __func__, dsc->dbus_property);
651 dbus_error_free(&error);
655 if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
663 static void do_send_prop_changed_signal(
664 DBusConnection *con, const char *path, const char *interface,
665 const struct wpa_dbus_object_desc *obj_dsc)
668 DBusMessageIter signal_iter, dict_iter;
670 msg = dbus_message_new_signal(path, DBUS_INTERFACE_PROPERTIES,
671 "PropertiesChanged");
675 dbus_message_iter_init_append(msg, &signal_iter);
677 if (!dbus_message_iter_append_basic(&signal_iter, DBUS_TYPE_STRING,
679 /* Changed properties dict */
680 !dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
681 "{sv}", &dict_iter) ||
682 !put_changed_properties(obj_dsc, interface, &dict_iter, 0) ||
683 !dbus_message_iter_close_container(&signal_iter, &dict_iter) ||
684 /* Invalidated properties array (empty) */
685 !dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
687 !dbus_message_iter_close_container(&signal_iter, &dict_iter))
690 dbus_connection_send(con, msg, NULL);
693 dbus_message_unref(msg);
697 wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
703 static void do_send_deprecated_prop_changed_signal(
704 DBusConnection *con, const char *path, const char *interface,
705 const struct wpa_dbus_object_desc *obj_dsc)
708 DBusMessageIter signal_iter, dict_iter;
710 msg = dbus_message_new_signal(path, interface, "PropertiesChanged");
714 dbus_message_iter_init_append(msg, &signal_iter);
716 if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
717 "{sv}", &dict_iter) ||
718 !put_changed_properties(obj_dsc, interface, &dict_iter, 1) ||
719 !dbus_message_iter_close_container(&signal_iter, &dict_iter))
722 dbus_connection_send(con, msg, NULL);
725 dbus_message_unref(msg);
729 wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
735 static void send_prop_changed_signal(
736 DBusConnection *con, const char *path, const char *interface,
737 const struct wpa_dbus_object_desc *obj_dsc)
740 * First, send property change notification on the standardized
741 * org.freedesktop.DBus.Properties interface. This call will not
742 * clear the property change bits, so that they are preserved for
743 * the call that follows.
745 do_send_prop_changed_signal(con, path, interface, obj_dsc);
748 * Now send PropertiesChanged on our own interface for backwards
749 * compatibility. This is deprecated and will be removed in a future
752 do_send_deprecated_prop_changed_signal(con, path, interface, obj_dsc);
754 /* Property change bits have now been cleared. */
758 static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx)
760 DBusConnection *con = eloop_ctx;
761 struct wpa_dbus_object_desc *obj_desc = timeout_ctx;
763 wpa_printf(MSG_DEBUG, "dbus: %s: Timeout - sending changed properties "
764 "of object %s", __func__, obj_desc->path);
765 wpa_dbus_flush_object_changed_properties(con, obj_desc->path);
769 static void recursive_flush_changed_properties(DBusConnection *con,
772 char **objects = NULL;
773 char subobj_path[WPAS_DBUS_OBJECT_PATH_MAX];
776 wpa_dbus_flush_object_changed_properties(con, path);
778 if (!dbus_connection_list_registered(con, path, &objects))
781 for (i = 0; objects[i]; i++) {
782 os_snprintf(subobj_path, WPAS_DBUS_OBJECT_PATH_MAX,
783 "%s/%s", path, objects[i]);
784 recursive_flush_changed_properties(con, subobj_path);
788 dbus_free_string_array(objects);
793 * wpa_dbus_flush_all_changed_properties - Send all PropertiesChanged signals
794 * @con: DBus connection
796 * Traverses through all registered objects and sends PropertiesChanged for
799 void wpa_dbus_flush_all_changed_properties(DBusConnection *con)
801 recursive_flush_changed_properties(con, WPAS_DBUS_NEW_PATH);
806 * wpa_dbus_flush_object_changed_properties - Send PropertiesChanged for object
807 * @con: DBus connection
808 * @path: path to a DBus object for which PropertiesChanged will be sent.
810 * Iterates over all properties registered with object and for each interface
811 * containing properties marked as changed, sends a PropertiesChanged signal
812 * containing names and new values of properties that have changed.
814 * You need to call this function after wpa_dbus_mark_property_changed()
815 * if you want to send PropertiesChanged signal immediately (i.e., without
816 * waiting timeout to expire). PropertiesChanged signal for an object is sent
817 * automatically short time after first marking property as changed. All
818 * PropertiesChanged signals are sent automatically after responding on DBus
819 * message, so if you marked a property changed as a result of DBus call
820 * (e.g., param setter), you usually do not need to call this function.
822 void wpa_dbus_flush_object_changed_properties(DBusConnection *con,
825 struct wpa_dbus_object_desc *obj_desc = NULL;
826 const struct wpa_dbus_property_desc *dsc;
829 dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
832 eloop_cancel_timeout(flush_object_timeout_handler, con, obj_desc);
834 for (dsc = obj_desc->properties, i = 0; dsc && dsc->dbus_property;
836 if (obj_desc->prop_changed_flags == NULL ||
837 !obj_desc->prop_changed_flags[i])
839 send_prop_changed_signal(con, path, dsc->dbus_interface,
845 #define WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT 5000
849 * wpa_dbus_mark_property_changed - Mark a property as changed and
850 * @iface: dbus priv struct
851 * @path: path to DBus object which property has changed
852 * @interface: interface containing changed property
853 * @property: property name which has changed
855 * Iterates over all properties registered with an object and marks the one
856 * given in parameters as changed. All parameters registered for an object
857 * within a single interface will be aggregated together and sent in one
858 * PropertiesChanged signal when function
859 * wpa_dbus_flush_object_changed_properties() is called.
861 void wpa_dbus_mark_property_changed(struct wpas_dbus_priv *iface,
862 const char *path, const char *interface,
863 const char *property)
865 struct wpa_dbus_object_desc *obj_desc = NULL;
866 const struct wpa_dbus_property_desc *dsc;
872 dbus_connection_get_object_path_data(iface->con, path,
873 (void **) &obj_desc);
875 wpa_printf(MSG_ERROR, "dbus: wpa_dbus_property_changed: "
876 "could not obtain object's private data: %s", path);
880 for (dsc = obj_desc->properties; dsc && dsc->dbus_property; dsc++, i++)
881 if (os_strcmp(property, dsc->dbus_property) == 0 &&
882 os_strcmp(interface, dsc->dbus_interface) == 0) {
883 if (obj_desc->prop_changed_flags)
884 obj_desc->prop_changed_flags[i] = 1;
888 if (!dsc || !dsc->dbus_property) {
889 wpa_printf(MSG_ERROR, "dbus: wpa_dbus_property_changed: "
890 "no property %s in object %s", property, path);
894 if (!eloop_is_timeout_registered(flush_object_timeout_handler,
895 iface->con, obj_desc)) {
896 eloop_register_timeout(0, WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT,
897 flush_object_timeout_handler,
898 iface->con, obj_desc);
904 * wpa_dbus_get_object_properties - Put object's properties into dictionary
905 * @iface: dbus priv struct
906 * @path: path to DBus object which properties will be obtained
907 * @interface: interface name which properties will be obtained
908 * @iter: DBus message iter at which to append property dictionary.
910 * Iterates over all properties registered with object and execute getters
911 * of those, which are readable and which interface matches interface
912 * specified as argument. Obtained properties values are stored in
913 * dict_iter dictionary.
915 dbus_bool_t wpa_dbus_get_object_properties(struct wpas_dbus_priv *iface,
917 const char *interface,
918 DBusMessageIter *iter)
920 struct wpa_dbus_object_desc *obj_desc = NULL;
921 DBusMessageIter dict_iter;
924 dbus_connection_get_object_path_data(iface->con, path,
925 (void **) &obj_desc);
927 wpa_printf(MSG_ERROR, "dbus: %s: could not obtain object's "
928 "private data: %s", __func__, path);
932 if (!wpa_dbus_dict_open_write(iter, &dict_iter)) {
933 wpa_printf(MSG_ERROR, "dbus: %s: failed to open message dict",
938 dbus_error_init(&error);
939 if (!fill_dict_with_properties(&dict_iter, obj_desc->properties,
940 interface, obj_desc->user_data,
942 wpa_printf(MSG_ERROR, "dbus: %s: failed to get object"
943 " properties: (%s) %s", __func__,
944 dbus_error_is_set(&error) ? error.name : "none",
945 dbus_error_is_set(&error) ? error.message : "none");
946 dbus_error_free(&error);
950 return wpa_dbus_dict_close_write(iter, &dict_iter);
954 * wpas_dbus_new_decompose_object_path - Decompose an interface object path into parts
955 * @path: The dbus object path
956 * @sep: Separating part (e.g., "Networks" or "PersistentGroups")
957 * @item: (out) The part following the specified separator, if any
958 * Returns: The object path of the interface this path refers to
960 * For a given object path, decomposes the object path into object id and
961 * requested part, if those parts exist. The caller is responsible for freeing
962 * the returned value. The *item pointer points to that allocated value and must
963 * not be freed separately.
965 * As an example, path = "/fi/w1/wpa_supplicant1/Interfaces/1/Networks/0" and
966 * sep = "Networks" would result in "/fi/w1/wpa_supplicant1/Interfaces/1"
967 * getting returned and *items set to point to "0".
969 char * wpas_dbus_new_decompose_object_path(const char *path, const char *sep,
972 const unsigned int dev_path_prefix_len =
973 os_strlen(WPAS_DBUS_NEW_PATH_INTERFACES "/");
980 /* Verify that this starts with our interface prefix */
981 if (os_strncmp(path, WPAS_DBUS_NEW_PATH_INTERFACES "/",
982 dev_path_prefix_len) != 0)
983 return NULL; /* not our path */
985 /* Ensure there's something at the end of the path */
986 if ((path + dev_path_prefix_len)[0] == '\0')
989 obj_path_only = os_strdup(path);
990 if (obj_path_only == NULL)
993 pos = obj_path_only + dev_path_prefix_len;
994 pos = os_strchr(pos, '/');
996 return obj_path_only; /* no next item on the path */
998 /* Separate network interface prefix from the path */
1001 sep_len = os_strlen(sep);
1002 if (os_strncmp(pos, sep, sep_len) != 0 || pos[sep_len] != '/')
1003 return obj_path_only; /* no match */
1005 /* return a pointer to the requested item */
1006 *item = pos + sep_len + 1;
1007 return obj_path_only;
1012 * wpas_dbus_reply_new_from_error - Create a new D-Bus error message from a
1013 * dbus error structure
1014 * @message: The original request message for which the error is a reply
1015 * @error: The error containing a name and a descriptive error cause
1016 * @fallback_name: A generic error name if @error was not set
1017 * @fallback_string: A generic error string if @error was not set
1018 * Returns: A new D-Bus error message
1020 * Given a DBusMessage structure, creates a new D-Bus error message using
1021 * the error name and string contained in that structure.
1023 DBusMessage * wpas_dbus_reply_new_from_error(DBusMessage *message,
1025 const char *fallback_name,
1026 const char *fallback_string)
1028 if (error && error->name && error->message) {
1029 return dbus_message_new_error(message, error->name,
1032 if (fallback_name && fallback_string) {
1033 return dbus_message_new_error(message, fallback_name,