* Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
* Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
*/
-#include "includes.h"
+#include "utils/includes.h"
-#include "common.h"
-#include "eloop.h"
+#include "utils/common.h"
+#include "utils/eloop.h"
+#include "dbus_common.h"
+#include "dbus_common_i.h"
+#include "dbus_new.h"
#include "dbus_new_helpers.h"
-
-/**
- * struct wpa_dbus_method_desc - DBus method description
- */
-struct wpa_dbus_method_desc {
- /* pointer to next description in list */
- struct wpa_dbus_method_desc *next;
-
- /* method interface */
- char *dbus_interface;
- /* method name */
- char *dbus_method;
-
- /* method handling function */
- WPADBusMethodHandler method_handler;
- /* handler function argument */
- void *handler_argument;
- /* function used to free handler argument */
- WPADBusArgumentFreeFunction argument_free_func;
-
- /* number of method arguments */
- int args_num;
- /* array of arguments */
- struct wpa_dbus_argument args[];
-};
-
-
-/**
- * struct wpa_dbus_signal_desc - DBus signal description
- */
-struct wpa_dbus_signal_desc {
- /* pointer to next description in list */
- struct wpa_dbus_signal_desc *next;
-
- /* signal interface */
- char *dbus_interface;
- /* signal name */
- char *dbus_signal;
-
- /* number of signal arguments */
- int args_num;
- /* array of arguments */
- struct wpa_dbus_argument args[0];
-};
-
-
-/**
- * struct wpa_dbus_property_desc - DBus property description
- */
-struct wpa_dbus_property_desc {
- /* pointer to next description in list */
- struct wpa_dbus_property_desc *next;
-
- /* property interface */
- char *dbus_interface;
- /* property name */
- char *dbus_property;
- /* property type signature in DBus type notation */
- char *type;
-
- /* property access permissions */
- enum dbus_prop_access access;
-
- /* property getter function */
- WPADBusPropertyAccessor getter;
- /* property setter function */
- WPADBusPropertyAccessor setter;
- /* argument for getter and setter functions */
- void *user_data;
- /* function used to free accessors argument */
- WPADBusArgumentFreeFunction user_data_free_func;
-};
-
-
-#ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
-#include <libxml/tree.h>
-
-struct interfaces {
- struct interfaces *next;
- char *dbus_interface;
- xmlNodePtr interface_node;
-};
-#endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
-
-static void process_watch(struct ctrl_iface_dbus_new_priv *iface,
- DBusWatch *watch, eloop_event_type type)
-{
- dbus_connection_ref(iface->con);
-
- iface->should_dispatch = 0;
-
- if (type == EVENT_TYPE_READ)
- dbus_watch_handle(watch, DBUS_WATCH_READABLE);
- else if (type == EVENT_TYPE_WRITE)
- dbus_watch_handle(watch, DBUS_WATCH_WRITABLE);
- else if (type == EVENT_TYPE_EXCEPTION)
- dbus_watch_handle(watch, DBUS_WATCH_ERROR);
-
- if (iface->should_dispatch) {
- while (dbus_connection_get_dispatch_status(iface->con) ==
- DBUS_DISPATCH_DATA_REMAINS)
- dbus_connection_dispatch(iface->con);
- iface->should_dispatch = 0;
- }
-
- dbus_connection_unref(iface->con);
-}
-
-
-static void process_watch_exception(int sock, void *eloop_ctx, void *sock_ctx)
-{
- process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_EXCEPTION);
-}
-
-
-static void process_watch_read(int sock, void *eloop_ctx, void *sock_ctx)
-{
- process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_READ);
-}
+#include "dbus_new_handlers.h"
+#include "dbus_dict_helpers.h"
-static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
+static dbus_bool_t fill_dict_with_properties(
+ DBusMessageIter *dict_iter,
+ const struct wpa_dbus_property_desc *props,
+ const char *interface, void *user_data, DBusError *error)
{
- process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_WRITE);
-}
-
+ DBusMessageIter entry_iter;
+ const struct wpa_dbus_property_desc *dsc;
-static void connection_setup_add_watch(struct ctrl_iface_dbus_new_priv *iface,
- DBusWatch *watch)
-{
- unsigned int flags;
- int fd;
+ for (dsc = props; dsc && dsc->dbus_property; dsc++) {
+ /* Only return properties for the requested D-Bus interface */
+ if (os_strncmp(dsc->dbus_interface, interface,
+ WPAS_DBUS_INTERFACE_MAX) != 0)
+ continue;
- if (!dbus_watch_get_enabled(watch))
- return;
+ /* Skip write-only properties */
+ if (dsc->getter == NULL)
+ continue;
- flags = dbus_watch_get_flags(watch);
- fd = dbus_watch_get_unix_fd(watch);
+ if (!dbus_message_iter_open_container(dict_iter,
+ DBUS_TYPE_DICT_ENTRY,
+ NULL, &entry_iter) ||
+ !dbus_message_iter_append_basic(&entry_iter,
+ DBUS_TYPE_STRING,
+ &dsc->dbus_property))
+ goto error;
- eloop_register_sock(fd, EVENT_TYPE_EXCEPTION, process_watch_exception,
- iface, watch);
+ /* An error getting a property fails the request entirely */
+ if (!dsc->getter(dsc, &entry_iter, error, user_data)) {
+ wpa_printf(MSG_INFO,
+ "dbus: %s dbus_interface=%s dbus_property=%s getter failed",
+ __func__, dsc->dbus_interface,
+ dsc->dbus_property);
+ return FALSE;
+ }
- if (flags & DBUS_WATCH_READABLE) {
- eloop_register_sock(fd, EVENT_TYPE_READ, process_watch_read,
- iface, watch);
- }
- if (flags & DBUS_WATCH_WRITABLE) {
- eloop_register_sock(fd, EVENT_TYPE_WRITE, process_watch_write,
- iface, watch);
+ if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
+ goto error;
}
- dbus_watch_set_data(watch, iface, NULL);
-}
-
-
-static void connection_setup_remove_watch(
- struct ctrl_iface_dbus_new_priv *iface, DBusWatch *watch)
-{
- unsigned int flags;
- int fd;
-
- flags = dbus_watch_get_flags(watch);
- fd = dbus_watch_get_unix_fd(watch);
-
- eloop_unregister_sock(fd, EVENT_TYPE_EXCEPTION);
-
- if (flags & DBUS_WATCH_READABLE)
- eloop_unregister_sock(fd, EVENT_TYPE_READ);
- if (flags & DBUS_WATCH_WRITABLE)
- eloop_unregister_sock(fd, EVENT_TYPE_WRITE);
-
- dbus_watch_set_data(watch, NULL, NULL);
-}
-
-
-static dbus_bool_t add_watch(DBusWatch *watch, void *data)
-{
- connection_setup_add_watch(data, watch);
return TRUE;
-}
-
-static void remove_watch(DBusWatch *watch, void *data)
-{
- connection_setup_remove_watch(data, watch);
-}
-
-
-static void watch_toggled(DBusWatch *watch, void *data)
-{
- if (dbus_watch_get_enabled(watch))
- add_watch(watch, data);
- else
- remove_watch(watch, data);
-}
-
-
-static void process_timeout(void *eloop_ctx, void *sock_ctx)
-{
- DBusTimeout *timeout = sock_ctx;
-
- dbus_timeout_handle(timeout);
-}
-
-
-static void connection_setup_add_timeout(
- struct ctrl_iface_dbus_new_priv *iface, DBusTimeout *timeout)
-{
- if (!dbus_timeout_get_enabled(timeout))
- return;
-
- eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
- process_timeout, iface, timeout);
-
- dbus_timeout_set_data(timeout, iface, NULL);
-}
-
-
-static void connection_setup_remove_timeout(
- struct ctrl_iface_dbus_new_priv *iface, DBusTimeout *timeout)
-{
- eloop_cancel_timeout(process_timeout, iface, timeout);
- dbus_timeout_set_data(timeout, NULL, NULL);
-}
-
-
-static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
-{
- if (!dbus_timeout_get_enabled(timeout))
- return TRUE;
-
- connection_setup_add_timeout(data, timeout);
-
- return TRUE;
-}
-
-
-static void remove_timeout(DBusTimeout *timeout, void *data)
-{
- connection_setup_remove_timeout(data, timeout);
-}
-
-
-static void timeout_toggled(DBusTimeout *timeout, void *data)
-{
- if (dbus_timeout_get_enabled(timeout))
- add_timeout(timeout, data);
- else
- remove_timeout(timeout, data);
-}
-
-
-static void process_wakeup_main(int sig, void *signal_ctx)
-{
- struct ctrl_iface_dbus_new_priv *iface = signal_ctx;
-
- if (sig != SIGPOLL || !iface->con)
- return;
-
- if (dbus_connection_get_dispatch_status(iface->con) !=
- DBUS_DISPATCH_DATA_REMAINS)
- return;
-
- /* Only dispatch once - we do not want to starve other events */
- dbus_connection_ref(iface->con);
- dbus_connection_dispatch(iface->con);
- dbus_connection_unref(iface->con);
+error:
+ dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
+ return FALSE;
}
/**
- * wakeup_main - Attempt to wake our mainloop up
- * @data: dbus control interface private data
+ * get_all_properties - Responds for GetAll properties calls on object
+ * @message: Message with GetAll call
+ * @interface: interface name which properties will be returned
+ * @property_dsc: list of object's properties
+ * Returns: Message with dict of variants as argument with properties values
*
- * Try to wake up the main eloop so it will process
- * dbus events that may have happened.
+ * Iterates over all properties registered with object and execute getters
+ * of those, which are readable and which interface matches interface
+ * specified as argument. Returned message contains one dict argument
+ * with properties names as keys and theirs values as values.
*/
-static void wakeup_main(void *data)
+static DBusMessage * get_all_properties(DBusMessage *message, char *interface,
+ struct wpa_dbus_object_desc *obj_dsc)
{
- struct ctrl_iface_dbus_new_priv *iface = data;
+ DBusMessage *reply;
+ DBusMessageIter iter, dict_iter;
+ DBusError error;
- /* Use SIGPOLL to break out of the eloop select() */
- raise(SIGPOLL);
- iface->should_dispatch = 1;
-}
+ reply = dbus_message_new_method_return(message);
+ if (reply == NULL)
+ return wpas_dbus_error_no_memory(message);
+ dbus_message_iter_init_append(reply, &iter);
+ if (!wpa_dbus_dict_open_write(&iter, &dict_iter)) {
+ dbus_message_unref(reply);
+ return wpas_dbus_error_no_memory(message);
+ }
-/**
- * connection_setup_wakeup_main - Tell dbus about our wakeup_main function
- * @iface: dbus control interface private data
- * Returns: 0 on success, -1 on failure
- *
- * Register our wakeup_main handler with dbus
- */
-static int connection_setup_wakeup_main(struct ctrl_iface_dbus_new_priv *iface)
-{
- if (eloop_register_signal(SIGPOLL, process_wakeup_main, iface))
- return -1;
+ dbus_error_init(&error);
+ if (!fill_dict_with_properties(&dict_iter, obj_dsc->properties,
+ interface, obj_dsc->user_data, &error)) {
+ dbus_message_unref(reply);
+ reply = wpas_dbus_reply_new_from_error(
+ message, &error, DBUS_ERROR_INVALID_ARGS,
+ "No readable properties in this interface");
+ dbus_error_free(&error);
+ return reply;
+ }
- dbus_connection_set_wakeup_main_function(iface->con, wakeup_main,
- iface, NULL);
+ if (!wpa_dbus_dict_close_write(&iter, &dict_iter)) {
+ dbus_message_unref(reply);
+ return wpas_dbus_error_no_memory(message);
+ }
- return 0;
+ return reply;
}
-/**
- * wpa_dbus_next_objid - Return next available object id
- * @iface: dbus control interface private data
- * Returns: Object id
- */
-u32 wpa_dbus_next_objid(struct ctrl_iface_dbus_new_priv *iface)
+static int is_signature_correct(DBusMessage *message,
+ const struct wpa_dbus_method_desc *method_dsc)
{
- return iface->next_objid++;
-}
-
+ /* According to DBus documentation max length of signature is 255 */
+#define MAX_SIG_LEN 256
+ char registered_sig[MAX_SIG_LEN], *pos;
+ const char *sig = dbus_message_get_signature(message);
+ int ret;
+ const struct wpa_dbus_argument *arg;
-/**
- * integrate_with_eloop - Register our mainloop integration with dbus
- * @connection: connection to the system message bus
- * @iface: a dbus control interface data structure
- * Returns: 0 on success, -1 on failure
- *
- * We register our mainloop integration functions with dbus here.
- */
-static int integrate_with_eloop(DBusConnection *connection,
- struct ctrl_iface_dbus_new_priv *iface)
-{
- if (!dbus_connection_set_watch_functions(connection, add_watch,
- remove_watch, watch_toggled,
- iface, NULL)) {
- perror("dbus_connection_set_watch_functions[dbus]");
- wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
- return -1;
- }
+ pos = registered_sig;
+ *pos = '\0';
- if (!dbus_connection_set_timeout_functions(connection, add_timeout,
- remove_timeout,
- timeout_toggled, iface,
- NULL)) {
- perror("dbus_connection_set_timeout_functions[dbus]");
- wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
- return -1;
- }
+ for (arg = method_dsc->args; arg && arg->name; arg++) {
+ if (arg->dir == ARG_IN) {
+ size_t blen = registered_sig + MAX_SIG_LEN - pos;
- if (connection_setup_wakeup_main(iface) < 0) {
- perror("connection_setup_wakeup_main[dbus]");
- wpa_printf(MSG_ERROR, "Could not setup main wakeup function.");
- return -1;
+ ret = os_snprintf(pos, blen, "%s", arg->type);
+ if (os_snprintf_error(blen, ret))
+ return 0;
+ pos += ret;
+ }
}
- return 0;
+ return !os_strncmp(registered_sig, sig, MAX_SIG_LEN);
}
-/**
- * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
- * claiming bus name
- * @eloop_ctx: the DBusConnection to dispatch on
- * @timeout_ctx: unused
- *
- * If clients are quick to notice that service claimed its bus name,
- * there may have been messages that came in before initialization was
- * all finished. Dispatch those here.
- */
-static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
+static DBusMessage * properties_get_all(DBusMessage *message, char *interface,
+ struct wpa_dbus_object_desc *obj_dsc)
{
- DBusConnection *con = eloop_ctx;
+ if (os_strcmp(dbus_message_get_signature(message), "s") != 0)
+ return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
+ NULL);
- while (dbus_connection_get_dispatch_status(con) ==
- DBUS_DISPATCH_DATA_REMAINS)
- dbus_connection_dispatch(con);
+ return get_all_properties(message, interface, obj_dsc);
}
-#ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
-
-/**
- * extract_interfaces - Extract interfaces from methods, signals and props
- * @obj_dsc: Description of object from which interfaces will be extracted
- * @root_node: root node of XML introspection document
- * Returns: List of interfaces found in object description
- *
- * Iterates over all methods, signals and properties registered with
- * object and collects all declared DBus interfaces and create interface's
- * node in XML root node for each. Returned list elements contains interface
- * name and XML node of corresponding interface.
- */
-static struct interfaces * extract_interfaces(
- struct wpa_dbus_object_desc *obj_dsc, xmlNodePtr root_node)
+static DBusMessage * properties_get(DBusMessage *message,
+ const struct wpa_dbus_property_desc *dsc,
+ void *user_data)
{
- struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
- struct wpa_dbus_signal_desc *signal_dsc = obj_dsc->signals;
- struct wpa_dbus_property_desc *property_dsc = obj_dsc->properties;
- struct interfaces *head = NULL;
- struct interfaces *iface, *last;
- int len;
-
- /* extract interfaces from methods */
- while (method_dsc) {
- iface = head;
- last = NULL;
-
- /* go to next method if its interface is already extracted */
- while (iface) {
- if (!os_strcmp(iface->dbus_interface,
- method_dsc->dbus_interface))
- break;
- last = iface;
- iface = iface->next;
- }
- if (iface) {
- method_dsc = method_dsc->next;
- continue;
- }
-
- iface = os_zalloc(sizeof(struct interfaces));
- if (!iface) {
- wpa_printf(MSG_ERROR, "Not enough memory to create "
- "interface introspection data");
- method_dsc = method_dsc->next;
- continue;
- }
-
- if (last)
- last->next = iface;
- else
- head = iface;
-
- len = os_strlen(method_dsc->dbus_interface) + 1;
- iface->dbus_interface = os_malloc(len);
- if (!iface->dbus_interface) {
- wpa_printf(MSG_ERROR, "Not enough memory to create "
- "interface introspection data (interface "
- "name)");
- method_dsc = method_dsc->next;
- continue;
- }
- os_strncpy(iface->dbus_interface, method_dsc->dbus_interface,
- len);
-
- iface->interface_node = xmlNewChild(root_node, NULL,
- BAD_CAST "interface",
- NULL);
- xmlNewProp(iface->interface_node, BAD_CAST "name",
- BAD_CAST method_dsc->dbus_interface);
+ DBusMessage *reply;
+ DBusMessageIter iter;
+ DBusError error;
- method_dsc = method_dsc->next;
+ if (os_strcmp(dbus_message_get_signature(message), "ss")) {
+ return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
+ NULL);
}
- /* extract interfaces from signals */
- while (signal_dsc) {
- iface = head;
- last = NULL;
-
- /* go to next signal if its interface is already extracted */
- while (iface) {
- if (!os_strcmp(iface->dbus_interface,
- signal_dsc->dbus_interface))
- break;
- last = iface;
- iface = iface->next;
- }
- if (iface) {
- signal_dsc = signal_dsc->next;
- continue;
- }
-
- iface = os_zalloc(sizeof(struct interfaces));
- if (!iface) {
- wpa_printf(MSG_ERROR, "Not enough memory to create "
- "interface introspection data");
- signal_dsc = signal_dsc->next;
- continue;
- }
-
- if (last)
- last->next = iface;
- else
- head = iface;
-
- len = os_strlen(signal_dsc->dbus_interface) + 1;
- iface->dbus_interface = os_malloc(len);
- if (!iface->dbus_interface) {
- wpa_printf(MSG_ERROR, "Not enough memory to create "
- "interface introspection data (interface "
- "name)");
- signal_dsc = signal_dsc->next;
- continue;
- }
- os_strncpy(iface->dbus_interface, signal_dsc->dbus_interface,
- len);
-
- iface->interface_node = xmlNewChild(root_node, NULL,
- BAD_CAST "interface",
- NULL);
- xmlNewProp(iface->interface_node, BAD_CAST "name",
- BAD_CAST signal_dsc->dbus_interface);
-
- signal_dsc = signal_dsc->next;
+ if (dsc->getter == NULL) {
+ return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
+ "Property is write-only");
}
- /* extract interfaces from properties */
- while (property_dsc) {
- iface = head;
- last = NULL;
-
- /* go to next property if its interface is already extracted */
- while (iface) {
- if (!os_strcmp(iface->dbus_interface,
- property_dsc->dbus_interface))
- break;
- last = iface;
- iface = iface->next;
- }
- if (iface) {
- property_dsc = property_dsc->next;
- continue;
- }
-
- iface = os_zalloc(sizeof(struct interfaces));
- if (!iface) {
- wpa_printf(MSG_ERROR, "Not enough memory to create "
- "interface introspection data");
- property_dsc = property_dsc->next;
- continue;
- }
-
- if (last)
- last->next = iface;
- else
- head = iface;
-
- len = os_strlen(property_dsc->dbus_interface) + 1;
- iface->dbus_interface = os_malloc(len);
- if (!iface->dbus_interface) {
- wpa_printf(MSG_ERROR, "Not enough memory to create "
- "interface introspection data (interface "
- "name)");
- property_dsc = property_dsc->next;
- continue;
- }
- os_strncpy(iface->dbus_interface, property_dsc->dbus_interface,
- len);
-
- iface->interface_node = xmlNewChild(root_node, NULL,
- BAD_CAST "interface",
- NULL);
- xmlNewProp(iface->interface_node, BAD_CAST "name",
- BAD_CAST property_dsc->dbus_interface);
+ reply = dbus_message_new_method_return(message);
+ dbus_message_iter_init_append(reply, &iter);
- property_dsc = property_dsc->next;
+ dbus_error_init(&error);
+ if (dsc->getter(dsc, &iter, &error, user_data) == FALSE) {
+ dbus_message_unref(reply);
+ reply = wpas_dbus_reply_new_from_error(
+ message, &error, DBUS_ERROR_FAILED,
+ "Failed to read property");
+ dbus_error_free(&error);
}
- return head;
+ return reply;
}
-/**
- * introspect - Responds for Introspect calls on object
- * @message: Message with Introspect call
- * @obj_dsc: Object description on which Introspect was called
- * Returns: Message with introspection result XML string as only argument
- *
- * Iterates over all methods, signals and properties registered with
- * object and generates introspection data for the object as XML string.
- */
-static DBusMessage * introspect(DBusMessage *message,
- struct wpa_dbus_object_desc *obj_dsc)
+static DBusMessage * properties_set(DBusMessage *message,
+ const struct wpa_dbus_property_desc *dsc,
+ void *user_data)
{
+ DBusMessage *reply;
+ DBusMessageIter iter;
+ DBusError error;
- DBusMessage *reply = NULL;
- struct interfaces *ifaces, *tmp;
- struct wpa_dbus_signal_desc *signal_dsc;
- struct wpa_dbus_method_desc *method_dsc;
- struct wpa_dbus_property_desc *property_dsc;
- xmlChar *intro_str;
- char **children;
- int i, s;
-
- xmlDocPtr doc = NULL;
- xmlNodePtr root_node = NULL, node = NULL, iface_node = NULL;
- xmlNodePtr method_node = NULL, signal_node = NULL;
- xmlNodePtr property_node = NULL, arg_node = NULL;
-
- /* Create and initialize the return message */
- reply = dbus_message_new_method_return(message);
-
- /* root node and dtd */
- doc = xmlNewDoc(BAD_CAST "1.0");
- root_node = xmlNewNode(NULL, BAD_CAST "node");
- xmlDocSetRootElement(doc, root_node);
- xmlCreateIntSubset(doc, BAD_CAST "node",
- BAD_CAST DBUS_INTROSPECT_1_0_XML_PUBLIC_IDENTIFIER,
- BAD_CAST DBUS_INTROSPECT_1_0_XML_SYSTEM_IDENTIFIER);
-
- /* Add Introspectable interface */
- iface_node = xmlNewChild(root_node, NULL, BAD_CAST "interface", NULL);
- xmlNewProp(iface_node, BAD_CAST "name",
- BAD_CAST WPA_DBUS_INTROSPECTION_INTERFACE);
-
- /* Add Introspect method */
- method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
- xmlNewProp(method_node, BAD_CAST "name",
- BAD_CAST WPA_DBUS_INTROSPECTION_METHOD);
- arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
- xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "data");
- xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
- xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
-
-
- /* Add Properties interface */
- iface_node = xmlNewChild(root_node, NULL,
- BAD_CAST "interface", NULL);
- xmlNewProp(iface_node, BAD_CAST "name",
- BAD_CAST WPA_DBUS_PROPERTIES_INTERFACE);
-
- /* Add Get method */
- method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
- xmlNewProp(method_node, BAD_CAST "name",
- BAD_CAST WPA_DBUS_PROPERTIES_GET);
- arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
- xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
- xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
- xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
- arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
- xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "propname");
- xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
- xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
- arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
- xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "value");
- xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "v");
- xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
- method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
-
- /* Add GetAll method */
- xmlNewProp(method_node, BAD_CAST "name",
- BAD_CAST WPA_DBUS_PROPERTIES_GETALL);
- arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
- xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
- xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
- xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
- arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
- xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "props");
- xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "a{sv}");
- xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
- method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
-
- /* Add Set method */
- xmlNewProp(method_node, BAD_CAST "name",
- BAD_CAST WPA_DBUS_PROPERTIES_SET);
- arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
- xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
- xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
- xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
- arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
- xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "propname");
- xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
- xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
- arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
- xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "value");
- xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "v");
- xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
-
- /* get all interfaces registered with object */
- ifaces = extract_interfaces(obj_dsc, root_node);
-
- /* create methods' nodes */
- method_dsc = obj_dsc->methods;
- while (method_dsc) {
-
- struct interfaces *iface = ifaces;
- while (iface) {
- if (!os_strcmp(iface->dbus_interface,
- method_dsc->dbus_interface))
- break;
- iface = iface->next;
- }
- if (!iface)
- continue;
-
- iface_node = iface->interface_node;
- method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method",
- NULL);
- xmlNewProp(method_node, BAD_CAST "name",
- BAD_CAST method_dsc->dbus_method);
-
- /* create args' nodes */
- for (i = 0; i < method_dsc->args_num; i++) {
- struct wpa_dbus_argument arg = method_dsc->args[i];
- arg_node = xmlNewChild(method_node, NULL,
- BAD_CAST "arg", NULL);
- if (arg.name && strlen(arg.name)) {
- xmlNewProp(arg_node, BAD_CAST "name",
- BAD_CAST arg.name);
- }
- xmlNewProp(arg_node, BAD_CAST "type",
- BAD_CAST arg.type);
- xmlNewProp(arg_node, BAD_CAST "direction",
- BAD_CAST (arg.dir == ARG_IN ?
- "in" : "out"));
- }
- method_dsc = method_dsc->next;
- }
-
- /* create signals' nodes */
- signal_dsc = obj_dsc->signals;
- while (signal_dsc) {
-
- struct interfaces *iface = ifaces;
- while (iface) {
- if (!os_strcmp(iface->dbus_interface,
- signal_dsc->dbus_interface))
- break;
- iface = iface->next;
- }
- if (!iface)
- continue;
-
- iface_node = iface->interface_node;
- signal_node = xmlNewChild(iface_node, NULL, BAD_CAST "signal",
- NULL);
- xmlNewProp(signal_node, BAD_CAST "name",
- BAD_CAST signal_dsc->dbus_signal);
-
- /* create args' nodes */
- for (i = 0; i < signal_dsc->args_num; i++) {
- struct wpa_dbus_argument arg = signal_dsc->args[i];
- arg_node = xmlNewChild(signal_node, NULL,
- BAD_CAST "arg", NULL);
- if (arg.name && strlen(arg.name)) {
- xmlNewProp(arg_node, BAD_CAST "name",
- BAD_CAST arg.name);
- }
- xmlNewProp(arg_node, BAD_CAST "type",
- BAD_CAST arg.type);
- }
- signal_dsc = signal_dsc->next;
- }
-
- /* create properties' nodes */
- property_dsc = obj_dsc->properties;
- while (property_dsc) {
-
- struct interfaces *iface = ifaces;
- while (iface) {
- if (!os_strcmp(iface->dbus_interface,
- property_dsc->dbus_interface))
- break;
- iface = iface->next;
- }
- if (!iface)
- continue;
-
- iface_node = iface->interface_node;
- property_node = xmlNewChild(iface_node, NULL,
- BAD_CAST "property", NULL);
- xmlNewProp(property_node, BAD_CAST "name",
- BAD_CAST property_dsc->dbus_property);
- xmlNewProp(property_node, BAD_CAST "type",
- BAD_CAST property_dsc->type);
- xmlNewProp(property_node, BAD_CAST "access", BAD_CAST
- (property_dsc->access == R ? "read" :
- (property_dsc->access == W ?
- "write" : "readwrite")));
-
- property_dsc = property_dsc->next;
+ if (os_strcmp(dbus_message_get_signature(message), "ssv")) {
+ return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
+ NULL);
}
- /* add child nodes to introspection tree; */
- dbus_connection_list_registered(obj_dsc->connection,
- dbus_message_get_path(message),
- &children);
- for (i = 0; children[i]; i++) {
- node = xmlNewChild(root_node, NULL, BAD_CAST "node", NULL);
- xmlNewProp(node, BAD_CAST "name", BAD_CAST children[i]);
+ if (dsc->setter == NULL) {
+ return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
+ "Property is read-only");
}
- dbus_free_string_array(children);
-
- xmlDocDumpFormatMemory(doc, &intro_str, &s, 1);
+ dbus_message_iter_init(message, &iter);
+ /* Skip the interface name and the property name */
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_next(&iter);
- xmlFreeDoc(doc);
-
- while (ifaces) {
- tmp = ifaces;
- ifaces = ifaces->next;
- os_free(tmp->dbus_interface);
- os_free(tmp);
+ /* Iter will now point to the property's new value */
+ dbus_error_init(&error);
+ if (dsc->setter(dsc, &iter, &error, user_data) == TRUE) {
+ /* Success */
+ reply = dbus_message_new_method_return(message);
+ } else {
+ reply = wpas_dbus_reply_new_from_error(
+ message, &error, DBUS_ERROR_FAILED,
+ "Failed to set property");
+ dbus_error_free(&error);
}
- dbus_message_append_args(reply, DBUS_TYPE_STRING, &intro_str,
- DBUS_TYPE_INVALID);
-
- xmlFree(intro_str);
-
return reply;
}
-#else /* CONFIG_CTRL_IFACE_DBUS_INTRO */
-
-/**
- * introspect - Responds for Introspect calls on object
- * @message: Message with Introspect call
- * @obj_dsc: Object description on which Introspect was called
- * Returns: Message with introspection result XML string as only argument
- *
- * Returns error informing that introspection support was not compiled.
- */
-static DBusMessage * introspect(DBusMessage *message,
- struct wpa_dbus_object_desc *obj_dsc)
-{
- return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
- "wpa_supplicant was compiled without "
- "introspection support.");
-}
-
-#endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
-
-/**
- * recursive_iter_copy - Reads arguments from one iterator and
- * writes to another recursively
- * @from: iterator to read from
- * @to: iterator to write to
- *
- * Copies one iterator's elements to another. If any element in
- * iterator is of container type, its content is copied recursively
- */
-static void recursive_iter_copy(DBusMessageIter *from, DBusMessageIter *to)
+static DBusMessage *
+properties_get_or_set(DBusMessage *message, DBusMessageIter *iter,
+ char *interface,
+ struct wpa_dbus_object_desc *obj_dsc)
{
+ const struct wpa_dbus_property_desc *property_dsc;
+ char *property;
+ const char *method;
- char *subtype = NULL;
- int type;
-
- /* iterate over iterator to copy */
- while ((type = dbus_message_iter_get_arg_type (from)) !=
- DBUS_TYPE_INVALID) {
-
- /* simply copy basic type entries */
- if (dbus_type_is_basic(type)) {
- if (dbus_type_is_fixed(type)) {
- /*
- * According to DBus documentation all
- * fixed-length types are guaranteed to fit
- * 8 bytes
- */
- dbus_uint64_t v;
- dbus_message_iter_get_basic (from, &v);
- dbus_message_iter_append_basic (to, type, &v);
- } else {
- char *v;
- dbus_message_iter_get_basic (from, &v);
- dbus_message_iter_append_basic (to, type, &v);
- }
- } else {
- /* recursively copy container type entries */
- DBusMessageIter write_subiter, read_subiter;
-
- dbus_message_iter_recurse(from, &read_subiter);
-
- if (type == DBUS_TYPE_VARIANT ||
- type == DBUS_TYPE_ARRAY) {
- subtype = dbus_message_iter_get_signature(
- &read_subiter);
- }
+ method = dbus_message_get_member(message);
+ property_dsc = obj_dsc->properties;
- dbus_message_iter_open_container(to, type, subtype,
- &write_subiter);
+ /* Second argument: property name (DBUS_TYPE_STRING) */
+ if (!dbus_message_iter_next(iter) ||
+ dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING) {
+ return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
+ NULL);
+ }
+ dbus_message_iter_get_basic(iter, &property);
- recursive_iter_copy(&read_subiter, &write_subiter);
+ while (property_dsc && property_dsc->dbus_property) {
+ /* compare property names and
+ * interfaces */
+ if (!os_strncmp(property_dsc->dbus_property, property,
+ WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
+ !os_strncmp(property_dsc->dbus_interface, interface,
+ WPAS_DBUS_INTERFACE_MAX))
+ break;
- dbus_message_iter_close_container(to, &write_subiter);
- if (subtype)
- dbus_free(subtype);
- }
+ property_dsc++;
+ }
+ if (property_dsc == NULL || property_dsc->dbus_property == NULL) {
+ wpa_printf(MSG_DEBUG, "no property handler for %s.%s on %s",
+ interface, property,
+ dbus_message_get_path(message));
+ return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
+ "No such property");
+ }
- dbus_message_iter_next(from);
+ if (os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
+ WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) == 0) {
+ wpa_printf(MSG_MSGDUMP, "%s: Get(%s)", __func__, property);
+ return properties_get(message, property_dsc,
+ obj_dsc->user_data);
}
+
+ wpa_printf(MSG_MSGDUMP, "%s: Set(%s)", __func__, property);
+ return properties_set(message, property_dsc, obj_dsc->user_data);
}
-/**
- * get_all_properties - Responds for GetAll properties calls on object
- * @message: Message with GetAll call
- * @interface: interface name which properties will be returned
- * @property_dsc: list of object's properties
- * Returns: Message with dict of variants as argument with properties values
- *
- * Iterates over all properties registered with object and execute getters
- * of those, which are readable and which interface matches interface
- * specified as argument. Returned message contains one dict argument
- * with properties names as keys and theirs values as values.
- */
-static DBusMessage * get_all_properties(
- DBusMessage *message, char *interface,
- struct wpa_dbus_property_desc *property_dsc)
+static DBusMessage * properties_handler(DBusMessage *message,
+ struct wpa_dbus_object_desc *obj_dsc)
{
- /* Create and initialize the return message */
- DBusMessage *reply = dbus_message_new_method_return(message);
- DBusMessage *getterReply = NULL;
- DBusMessageIter iter, dict_iter, entry_iter, ret_iter;
- int counter = 0;
+ DBusMessageIter iter;
+ char *interface;
+ const char *method;
- dbus_message_iter_init_append(reply, &iter);
+ method = dbus_message_get_member(message);
+ dbus_message_iter_init(message, &iter);
- dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
- DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
- DBUS_TYPE_STRING_AS_STRING
- DBUS_TYPE_VARIANT_AS_STRING
- DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
- &dict_iter);
-
- while (property_dsc) {
- if (!os_strncmp(property_dsc->dbus_interface, interface,
- WPAS_DBUS_INTERFACE_MAX) &&
- property_dsc->access != W && property_dsc->getter) {
-
- getterReply = property_dsc->getter(
- message, property_dsc->user_data);
- dbus_message_iter_init(getterReply, &ret_iter);
-
- dbus_message_iter_open_container(&dict_iter,
- DBUS_TYPE_DICT_ENTRY,
- NULL, &entry_iter);
- dbus_message_iter_append_basic(
- &entry_iter, DBUS_TYPE_STRING,
- &(property_dsc->dbus_property));
-
- recursive_iter_copy(&ret_iter, &entry_iter);
-
- dbus_message_iter_close_container(&dict_iter,
- &entry_iter);
- dbus_message_unref(getterReply);
- counter++;
+ if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
+ WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
+ !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
+ WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
+ !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
+ WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
+ /* First argument: interface name (DBUS_TYPE_STRING) */
+ if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
+ return dbus_message_new_error(message,
+ DBUS_ERROR_INVALID_ARGS,
+ NULL);
}
- property_dsc = property_dsc->next;
- }
- dbus_message_iter_close_container(&iter, &dict_iter);
- if (counter == 0) {
- dbus_message_unref(reply);
- reply = dbus_message_new_error(message,
- DBUS_ERROR_INVALID_ARGS,
- "No readable properties in "
- "this interface");
- }
+ dbus_message_iter_get_basic(&iter, &interface);
- return reply;
+ if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
+ WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
+ /* GetAll */
+ return properties_get_all(message, interface, obj_dsc);
+ }
+ /* Get or Set */
+ return properties_get_or_set(message, &iter, interface,
+ obj_dsc);
+ }
+ return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
+ NULL);
}
-static int is_signature_correct(DBusMessage *message,
- struct wpa_dbus_method_desc *method_dsc)
+static DBusMessage * msg_method_handler(DBusMessage *message,
+ struct wpa_dbus_object_desc *obj_dsc)
{
- /* According to DBus documentation max length of signature is 255 */
-#define MAX_SIG_LEN 256
- char registered_sig[MAX_SIG_LEN], *pos;
- const char *sig = dbus_message_get_signature(message);
- int i, ret;
+ const struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
+ const char *method;
+ const char *msg_interface;
- pos = registered_sig;
- *pos = '\0';
+ method = dbus_message_get_member(message);
+ msg_interface = dbus_message_get_interface(message);
- for (i = 0; i < method_dsc->args_num; i++) {
- struct wpa_dbus_argument arg = method_dsc->args[i];
- if (arg.dir == ARG_IN) {
- size_t blen = registered_sig + MAX_SIG_LEN - pos;
- ret = os_snprintf(pos, blen, "%s", arg.type);
- if (ret < 0 || (size_t) ret >= blen)
- return 0;
- pos += ret;
- }
+ /* try match call to any registered method */
+ while (method_dsc && method_dsc->dbus_method) {
+ /* compare method names and interfaces */
+ if (!os_strncmp(method_dsc->dbus_method, method,
+ WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
+ !os_strncmp(method_dsc->dbus_interface, msg_interface,
+ WPAS_DBUS_INTERFACE_MAX))
+ break;
+
+ method_dsc++;
+ }
+ if (method_dsc == NULL || method_dsc->dbus_method == NULL) {
+ wpa_printf(MSG_DEBUG, "no method handler for %s.%s on %s",
+ msg_interface, method,
+ dbus_message_get_path(message));
+ return dbus_message_new_error(message,
+ DBUS_ERROR_UNKNOWN_METHOD, NULL);
}
- return !os_strncmp(registered_sig, sig, MAX_SIG_LEN);
+ if (!is_signature_correct(message, method_dsc)) {
+ return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
+ NULL);
+ }
+
+ return method_dsc->method_handler(message, obj_dsc->user_data);
}
* of the special cases i.e. introspection call or properties get/getall/set
* methods and handles it. Else it iterates over registered methods list
* and tries to match method's name and interface to those read from message
- * If appropriate method was found it's handler function is called and
- * response is sent. Otherwise the DBUS_ERROR_UNKNOWN_METHOD error message
+ * If appropriate method was found its handler function is called and
+ * response is sent. Otherwise, the DBUS_ERROR_UNKNOWN_METHOD error message
* will be sent.
*/
static DBusHandlerResult message_handler(DBusConnection *connection,
const char *method;
const char *path;
const char *msg_interface;
-
-#define MESSAGE_UNHANDLED (DBusMessage *) 1
-
- DBusMessage *reply = MESSAGE_UNHANDLED;
+ DBusMessage *reply;
/* get method, interface and path the message is addressed to */
method = dbus_message_get_member(message);
path = dbus_message_get_path(message);
msg_interface = dbus_message_get_interface(message);
if (!method || !path || !msg_interface)
- goto out;
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ wpa_printf(MSG_MSGDUMP, "dbus: %s.%s (%s) [%s]",
+ msg_interface, method, path,
+ dbus_message_get_signature(message));
/* if message is introspection method call */
if (!os_strncmp(WPA_DBUS_INTROSPECTION_METHOD, method,
WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
!os_strncmp(WPA_DBUS_INTROSPECTION_INTERFACE, msg_interface,
- WPAS_DBUS_INTERFACE_MAX))
- reply = introspect(message, obj_dsc);
- else if (!strncmp(WPA_DBUS_PROPERTIES_INTERFACE, msg_interface,
- WPAS_DBUS_INTERFACE_MAX)) {
+ WPAS_DBUS_INTERFACE_MAX)) {
+#ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
+ reply = wpa_dbus_introspect(message, obj_dsc);
+#else /* CONFIG_CTRL_IFACE_DBUS_INTRO */
+ reply = dbus_message_new_error(
+ message, DBUS_ERROR_UNKNOWN_METHOD,
+ "wpa_supplicant was compiled without introspection support.");
+#endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
+ } else if (!os_strncmp(WPA_DBUS_PROPERTIES_INTERFACE, msg_interface,
+ WPAS_DBUS_INTERFACE_MAX)) {
/* if message is properties method call */
- DBusMessageIter iter;
- char *interface;
- char *property;
-
- dbus_message_iter_init(message, &iter);
-
- if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
- WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
- !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
- WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
- !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
- WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
- /* First argument: interface name (DBUS_TYPE_STRING) */
- if (dbus_message_iter_get_arg_type(&iter) !=
- DBUS_TYPE_STRING) {
- reply = dbus_message_new_error(
- message, DBUS_ERROR_INVALID_ARGS,
- NULL);
- goto out;
- }
-
- dbus_message_iter_get_basic(&iter, &interface);
-
- /* GetAll */
- if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
- WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
- if (os_strcmp(dbus_message_get_signature(
- message), "s"))
- reply = dbus_message_new_error(
- message,
- DBUS_ERROR_INVALID_ARGS, NULL);
- else
- reply = get_all_properties(
- message, interface,
- obj_dsc->properties);
- } else {
- /* Get or Set */
- struct wpa_dbus_property_desc *property_dsc;
- property_dsc = obj_dsc->properties;
-
- /* Second argument: property name
- * (DBUS_TYPE_STRING) */
- if (!dbus_message_iter_next(&iter) ||
- dbus_message_iter_get_arg_type(&iter) !=
- DBUS_TYPE_STRING) {
- reply = dbus_message_new_error(
- message,
- DBUS_ERROR_INVALID_ARGS, NULL);
- goto out;
- }
- dbus_message_iter_get_basic(&iter, &property);
-
- while (property_dsc) {
- /* compare property names and
- * interfaces */
- if (!os_strncmp(property_dsc->dbus_property,
- property,
- WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
- !os_strncmp(property_dsc->dbus_interface, interface,
- WPAS_DBUS_INTERFACE_MAX))
- break;
-
- property_dsc = property_dsc->next;
- }
- if (property_dsc) {
- /* Get */
- if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
- WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
- if (os_strcmp(dbus_message_get_signature(message), "ss"))
- reply = dbus_message_new_error(message,
- DBUS_ERROR_INVALID_ARGS, NULL);
- else if (property_dsc->access != W &&
- property_dsc->getter)
- reply = property_dsc->getter(message,
- property_dsc->user_data);
- else
- reply = dbus_message_new_error(message,
- DBUS_ERROR_INVALID_ARGS,
- "Property is write-only");
- } else {
- /* Set */
- if (os_strcmp(dbus_message_get_signature(message), "ssv"))
- reply = dbus_message_new_error(message,
- DBUS_ERROR_INVALID_ARGS, NULL);
- else if (property_dsc->access != R &&
- property_dsc->setter) {
- reply = property_dsc->setter
- (message, property_dsc->user_data);
- } else {
- reply = dbus_message_new_error(message,
- DBUS_ERROR_INVALID_ARGS,
- "Property is read-only");
- }
- }
- } else {
- wpa_printf(MSG_DEBUG, "no property handler for %s.%s\n"
- "on %s", interface, property, path);
- reply = dbus_message_new_error(message,
- DBUS_ERROR_INVALID_ARGS,
- "No such property");
- }
- }
- } else {
- reply = dbus_message_new_error(
- message, DBUS_ERROR_UNKNOWN_METHOD, NULL);
- goto out;
- }
+ reply = properties_handler(message, obj_dsc);
} else {
- struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
-
- /* try match call to any registered method */
- while (method_dsc) {
- /* compare method names and interfaces */
- if (!os_strncmp(method_dsc->dbus_method, method,
- WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
- !os_strncmp(method_dsc->dbus_interface,
- msg_interface,
- WPAS_DBUS_INTERFACE_MAX))
- break;
-
- method_dsc = method_dsc->next;
- }
- if (method_dsc) {
- if (is_signature_correct(message, method_dsc)) {
- reply = method_dsc->method_handler(
- message, method_dsc->handler_argument);
- } else {
- reply = dbus_message_new_error(
- message, DBUS_ERROR_INVALID_ARGS,
- NULL);
- }
- } else {
- wpa_printf(MSG_DEBUG, "no method handler for %s.%s "
- "on %s", msg_interface, method, path);
- reply = dbus_message_new_error(
- message, DBUS_ERROR_UNKNOWN_METHOD, NULL);
- }
+ reply = msg_method_handler(message, obj_dsc);
}
-out:
- /* If the message was handled, send back the reply */
- if (reply != MESSAGE_UNHANDLED) {
- /* If handler succeed returning NULL, reply empty message */
- if (!reply)
- reply = dbus_message_new_method_return(message);
- if (reply) {
- if (!dbus_message_get_no_reply(message))
- dbus_connection_send(connection, reply, NULL);
- dbus_message_unref(reply);
- }
- return DBUS_HANDLER_RESULT_HANDLED;
- } else
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ /* If handler succeed returning NULL, reply empty message */
+ if (!reply)
+ reply = dbus_message_new_method_return(message);
+ if (reply) {
+ if (!dbus_message_get_no_reply(message))
+ dbus_connection_send(connection, reply, NULL);
+ dbus_message_unref(reply);
+ }
+
+ wpa_dbus_flush_all_changed_properties(connection);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
}
*/
void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
{
- struct wpa_dbus_method_desc *method_dsc, *tmp_met_dsc;
- struct wpa_dbus_signal_desc *signal_dsc, *tmp_sig_dsc;
- struct wpa_dbus_property_desc *property_dsc, *tmp_prop_dsc;
- int i;
-
if (!obj_dsc)
return;
- /* free methods */
- method_dsc = obj_dsc->methods;
-
- while (method_dsc) {
- tmp_met_dsc = method_dsc;
- method_dsc = method_dsc->next;
-
- os_free(tmp_met_dsc->dbus_interface);
- os_free(tmp_met_dsc->dbus_method);
-
- for (i = 0; i < tmp_met_dsc->args_num; i++) {
- os_free(tmp_met_dsc->args[i].name);
- os_free(tmp_met_dsc->args[i].type);
- }
-
- if (tmp_met_dsc->argument_free_func)
- tmp_met_dsc->argument_free_func(
- tmp_met_dsc->handler_argument);
-
- os_free(tmp_met_dsc);
- }
-
- /* free signals */
- signal_dsc = obj_dsc->signals;
-
- while (signal_dsc) {
- tmp_sig_dsc = signal_dsc;
- signal_dsc = signal_dsc->next;
-
- os_free(tmp_sig_dsc->dbus_interface);
- os_free(tmp_sig_dsc->dbus_signal);
-
- for (i = 0; i < tmp_sig_dsc->args_num; i++) {
- os_free(tmp_sig_dsc->args[i].name);
- os_free(tmp_sig_dsc->args[i].type);
- }
-
- os_free(tmp_sig_dsc);
- }
-
- /* free properties */
- property_dsc = obj_dsc->properties;
-
- while (property_dsc) {
- tmp_prop_dsc = property_dsc;
- property_dsc = property_dsc->next;
-
- os_free(tmp_prop_dsc->dbus_interface);
- os_free(tmp_prop_dsc->dbus_property);
- os_free(tmp_prop_dsc->type);
-
- if (tmp_prop_dsc->user_data_free_func)
- tmp_prop_dsc->user_data_free_func(
- tmp_prop_dsc->user_data);
-
- os_free(tmp_prop_dsc);
- }
+ /* free handler's argument */
+ if (obj_dsc->user_data_free_func)
+ obj_dsc->user_data_free_func(obj_dsc->user_data);
+ os_free(obj_dsc->path);
+ os_free(obj_dsc->prop_changed_flags);
os_free(obj_dsc);
}
free_dbus_object_desc(obj_dsc);
}
+
/**
* wpa_dbus_ctrl_iface_init - Initialize dbus control interface
* @application_data: Pointer to application specific data structure
* @dbus_service: DBus service name to register with
* @messageHandler: a pointer to function which will handle dbus messages
* coming on interface
- * Returns: Pointer to dbus_new_ctrl_iface date or %NULL on failure
+ * Returns: 0 on success, -1 on failure
*
* Initialize the dbus control interface and start receiving commands from
* external programs over the bus.
*/
-struct ctrl_iface_dbus_new_priv *
-wpa_dbus_ctrl_iface_init(void *application_data,
- char *dbus_path, char *dbus_service,
- struct wpa_dbus_object_desc *obj_desc)
+int wpa_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface,
+ char *dbus_path, char *dbus_service,
+ struct wpa_dbus_object_desc *obj_desc)
{
- struct ctrl_iface_dbus_new_priv *iface;
DBusError error;
int ret = -1;
DBusObjectPathVTable wpa_vtable = {
NULL, NULL, NULL, NULL
};
- iface = os_zalloc(sizeof(struct ctrl_iface_dbus_new_priv));
- if (iface == NULL)
- return NULL;
-
- iface->application_data = application_data;
-
- /* Get a reference to the system bus */
- dbus_error_init(&error);
- iface->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
- dbus_error_free(&error);
- if (!iface->con) {
- perror("dbus_bus_get[ctrl_iface_dbus]");
- wpa_printf(MSG_ERROR, "Could not acquire the system bus.");
- goto fail;
- }
-
obj_desc->connection = iface->con;
-
- /* Tell dbus about our mainloop integration functions */
- if (integrate_with_eloop(iface->con, iface))
- goto fail;
+ obj_desc->path = os_strdup(dbus_path);
/* Register the message handler for the global dbus interface */
- if (!dbus_connection_register_object_path(iface->con,
- dbus_path, &wpa_vtable,
- obj_desc)) {
- perror("dbus_connection_register_object_path[dbus]");
- wpa_printf(MSG_ERROR, "Could not set up DBus message "
- "handler.");
- goto fail;
+ if (!dbus_connection_register_object_path(iface->con, dbus_path,
+ &wpa_vtable, obj_desc)) {
+ wpa_printf(MSG_ERROR, "dbus: Could not set up message handler");
+ return -1;
}
/* Register our service with the message bus */
dbus_error_init(&error);
- switch (dbus_bus_request_name(iface->con, dbus_service,
- 0, &error)) {
+ switch (dbus_bus_request_name(iface->con, dbus_service, 0, &error)) {
case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
ret = 0;
break;
case DBUS_REQUEST_NAME_REPLY_EXISTS:
case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
- perror("dbus_bus_request_name[dbus]");
- wpa_printf(MSG_ERROR, "Could not request DBus service name: "
- "already registered.");
+ wpa_printf(MSG_ERROR,
+ "dbus: Could not request service name: already registered");
break;
default:
- perror("dbus_bus_request_name[dbus]");
- wpa_printf(MSG_ERROR, "Could not request DBus service name: "
- "%s %s.", error.name, error.message);
+ wpa_printf(MSG_ERROR,
+ "dbus: Could not request service name: %s %s",
+ error.name, error.message);
break;
}
dbus_error_free(&error);
if (ret != 0)
- goto fail;
+ return -1;
wpa_printf(MSG_DEBUG, "Providing DBus service '%s'.", dbus_service);
- /*
- * Dispatch initial DBus messages that may have come in since the bus
- * name was claimed above. Happens when clients are quick to notice the
- * service.
- *
- * FIXME: is there a better solution to this problem?
- */
- eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
- iface->con, NULL);
-
- return iface;
-
-fail:
- wpa_dbus_ctrl_iface_deinit(iface);
- return NULL;
-}
-
-
-/**
- * wpa_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface
- * @iface: Pointer to dbus private data from wpa_dbus_ctrl_iface_init()
- *
- * Deinitialize the dbus control interface that was initialized with
- * wpa_dbus_ctrl_iface_init().
- */
-void wpa_dbus_ctrl_iface_deinit(struct ctrl_iface_dbus_new_priv *iface)
-{
- if (iface == NULL)
- return;
-
- if (iface->con) {
- eloop_cancel_timeout(dispatch_initial_dbus_messages,
- iface->con, NULL);
- dbus_connection_set_watch_functions(iface->con, NULL, NULL,
- NULL, NULL, NULL);
- dbus_connection_set_timeout_functions(iface->con, NULL, NULL,
- NULL, NULL, NULL);
- dbus_connection_unref(iface->con);
- }
-
- os_memset(iface, 0, sizeof(struct ctrl_iface_dbus_new_priv));
- os_free(iface);
+ return 0;
}
*
* Registers a new interface with dbus and assigns it a dbus object path.
*/
-int wpa_dbus_register_object_per_iface(
- struct ctrl_iface_dbus_new_priv *ctrl_iface,
- const char *path, const char *ifname,
- struct wpa_dbus_object_desc *obj_desc)
+int wpa_dbus_register_object_per_iface(struct wpas_dbus_priv *ctrl_iface,
+ const char *path, const char *ifname,
+ struct wpa_dbus_object_desc *obj_desc)
{
DBusConnection *con;
-
+ DBusError error;
DBusObjectPathVTable vtable = {
&free_dbus_object_desc_cb, &message_handler,
NULL, NULL, NULL, NULL
con = ctrl_iface->con;
obj_desc->connection = con;
+ obj_desc->path = os_strdup(path);
+ dbus_error_init(&error);
/* Register the message handler for the interface functions */
- if (!dbus_connection_register_object_path(con, path, &vtable,
- obj_desc)) {
- perror("wpa_dbus_register_iface [dbus]");
- wpa_printf(MSG_ERROR, "Could not set up DBus message "
- "handler for interface %s\n"
- "and object %s.", ifname, path);
+ if (!dbus_connection_try_register_object_path(con, path, &vtable,
+ obj_desc, &error)) {
+ if (os_strcmp(error.name, DBUS_ERROR_OBJECT_PATH_IN_USE) == 0) {
+ wpa_printf(MSG_DEBUG, "dbus: %s", error.message);
+ } else {
+ wpa_printf(MSG_ERROR,
+ "dbus: Could not set up message handler for interface %s object %s (error: %s message: %s)",
+ ifname, path, error.name, error.message);
+ }
+ dbus_error_free(&error);
return -1;
}
+ dbus_error_free(&error);
return 0;
}
+static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx);
+
+
/**
* wpa_dbus_unregister_object_per_iface - Unregisters DBus object
* @ctrl_iface: Pointer to dbus private data
* Unregisters DBus object given by its path
*/
int wpa_dbus_unregister_object_per_iface(
- struct ctrl_iface_dbus_new_priv *ctrl_iface, const char *path)
+ struct wpas_dbus_priv *ctrl_iface, const char *path)
{
DBusConnection *con = ctrl_iface->con;
+ struct wpa_dbus_object_desc *obj_desc = NULL;
+
+ dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
+ if (!obj_desc) {
+ wpa_printf(MSG_ERROR,
+ "dbus: %s: Could not obtain object's private data: %s",
+ __func__, path);
+ return 0;
+ }
+
+ eloop_cancel_timeout(flush_object_timeout_handler, con, obj_desc);
+
if (!dbus_connection_unregister_object_path(con, path))
return -1;
}
-/**
- * wpa_dbus_method_register - Registers DBus method for given object
- * @obj_dsc: Object description for which a method will be registered
- * @dbus_interface: DBus interface under which method will be registered
- * @dbus_method: a name the method will be registered with
- * @method_handler: a function which will be called to handle this method call
- * @handler_argument: an additional argument passed to handler function
- * @argument_free_func: function used to free handler argument
- * @args: method arguments list
- * Returns: Zero on success and -1 on failure
- *
- * Registers DBus method under given name and interface for the object.
- * Method calls will be handled with given handling function and optional
- * argument passed to this function. Handler function is required to return
- * a DBusMessage pointer which will be response to method call. Any method
- * call before being handled must have registered appropriate handler by
- * using this function.
- */
-int wpa_dbus_method_register(struct wpa_dbus_object_desc *obj_dsc,
- const char *dbus_interface,
- const char *dbus_method,
- WPADBusMethodHandler method_handler,
- void *handler_argument,
- WPADBusArgumentFreeFunction argument_free_func,
- const struct wpa_dbus_argument args[])
+static dbus_bool_t put_changed_properties(
+ const struct wpa_dbus_object_desc *obj_dsc, const char *interface,
+ DBusMessageIter *dict_iter, int clear_changed)
{
- struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
- struct wpa_dbus_method_desc *prev_desc;
- int args_num = 0;
- int interface_len, method_len, i, len, error;
-
- prev_desc = NULL;
- while (method_dsc) {
- prev_desc = method_dsc;
- method_dsc = method_dsc->next;
- }
-
- /* count args */
- if (args) {
- while (args[args_num].name && args[args_num].type)
- args_num++;
- }
+ DBusMessageIter entry_iter;
+ const struct wpa_dbus_property_desc *dsc;
+ int i;
+ DBusError error;
- method_dsc = os_zalloc(sizeof(struct wpa_dbus_method_desc) +
- args_num * sizeof(struct wpa_dbus_argument));
- if (!method_dsc)
- goto err;
-
- if (prev_desc == NULL)
- obj_dsc->methods = method_dsc;
- else
- prev_desc->next = method_dsc;
-
- /* copy interface name */
- interface_len = os_strlen(dbus_interface) + 1;
- method_dsc->dbus_interface = os_malloc(interface_len);
- if (!method_dsc->dbus_interface)
- goto err;
- os_strncpy(method_dsc->dbus_interface, dbus_interface, interface_len);
-
- /* copy method name */
- method_len = os_strlen(dbus_method) + 1;
- method_dsc->dbus_method = os_malloc(method_len);
- if (!method_dsc->dbus_method)
- goto err;
- os_strncpy(method_dsc->dbus_method, dbus_method, method_len);
-
- /* copy arguments */
- error = 0;
- method_dsc->args_num = args_num;
- for (i = 0; i < args_num; i++) {
- len = os_strlen(args[i].name) + 1;
- method_dsc->args[i].name = os_malloc(len);
- if (!method_dsc->args[i].name) {
- error = 1;
+ for (dsc = obj_dsc->properties, i = 0; dsc && dsc->dbus_property;
+ dsc++, i++) {
+ if (obj_dsc->prop_changed_flags == NULL ||
+ !obj_dsc->prop_changed_flags[i])
continue;
- }
- os_strncpy(method_dsc->args[i].name, args[i].name, len);
-
- len = os_strlen(args[i].type) + 1;
- method_dsc->args[i].type = os_malloc(len);
- if (!method_dsc->args[i].type) {
- error = 1;
+ if (os_strcmp(dsc->dbus_interface, interface) != 0)
continue;
+ if (clear_changed)
+ obj_dsc->prop_changed_flags[i] = 0;
+
+ if (!dbus_message_iter_open_container(dict_iter,
+ DBUS_TYPE_DICT_ENTRY,
+ NULL, &entry_iter) ||
+ !dbus_message_iter_append_basic(&entry_iter,
+ DBUS_TYPE_STRING,
+ &dsc->dbus_property))
+ return FALSE;
+
+ dbus_error_init(&error);
+ if (!dsc->getter(dsc, &entry_iter, &error, obj_dsc->user_data))
+ {
+ if (dbus_error_is_set(&error)) {
+ wpa_printf(MSG_ERROR,
+ "dbus: %s: Cannot get new value of property %s: (%s) %s",
+ __func__, dsc->dbus_property,
+ error.name, error.message);
+ } else {
+ wpa_printf(MSG_ERROR,
+ "dbus: %s: Cannot get new value of property %s",
+ __func__, dsc->dbus_property);
+ }
+ dbus_error_free(&error);
+ return FALSE;
}
- os_strncpy(method_dsc->args[i].type, args[i].type, len);
- method_dsc->args[i].dir = args[i].dir;
+ if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
+ return FALSE;
}
- if (error)
- goto err;
- method_dsc->method_handler = method_handler;
- method_dsc->handler_argument = handler_argument;
- method_dsc->argument_free_func = argument_free_func;
- method_dsc->next = NULL;
+ return TRUE;
+}
- return 0;
-err:
- wpa_printf(MSG_WARNING, "Failed to register dbus method %s in "
- "interface %s", dbus_method, dbus_interface);
- if (method_dsc) {
- os_free(method_dsc->dbus_interface);
- os_free(method_dsc->dbus_method);
- for (i = 0; i < method_dsc->args_num; i++) {
- os_free(method_dsc->args[i].name);
- os_free(method_dsc->args[i].type);
- }
+static void do_send_prop_changed_signal(
+ DBusConnection *con, const char *path, const char *interface,
+ const struct wpa_dbus_object_desc *obj_dsc)
+{
+ DBusMessage *msg;
+ DBusMessageIter signal_iter, dict_iter;
- if (prev_desc == NULL)
- obj_dsc->methods = NULL;
- else
- prev_desc->next = NULL;
+ msg = dbus_message_new_signal(path, DBUS_INTERFACE_PROPERTIES,
+ "PropertiesChanged");
+ if (msg == NULL)
+ return;
- os_free(method_dsc);
+ dbus_message_iter_init_append(msg, &signal_iter);
+
+ if (!dbus_message_iter_append_basic(&signal_iter, DBUS_TYPE_STRING,
+ &interface) ||
+ /* Changed properties dict */
+ !dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
+ "{sv}", &dict_iter) ||
+ !put_changed_properties(obj_dsc, interface, &dict_iter, 0) ||
+ !dbus_message_iter_close_container(&signal_iter, &dict_iter) ||
+ /* Invalidated properties array (empty) */
+ !dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
+ "s", &dict_iter) ||
+ !dbus_message_iter_close_container(&signal_iter, &dict_iter)) {
+ wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
+ __func__);
+ } else {
+ dbus_connection_send(con, msg, NULL);
}
- return -1;
+ dbus_message_unref(msg);
}
-/**
- * wpa_dbus_signal_register - Registers DBus signal for given object
- * @obj_dsc: Object description for which a signal will be registered
- * @dbus_interface: DBus interface under which signal will be registered
- * @dbus_signal: a name the signal will be registered with
- * @args: signal arguments list
- * Returns: Zero on success and -1 on failure
- *
- * Registers DBus signal under given name and interface for the object.
- * Signal registration is NOT required in order to send signals, but not
- * registered signals will not be respected in introspection data
- * therefore it is highly recommended to register every signal before
- * using it.
- */
-int wpa_dbus_signal_register(struct wpa_dbus_object_desc *obj_dsc,
- const char *dbus_interface,
- const char *dbus_signal,
- const struct wpa_dbus_argument args[])
+static void do_send_deprecated_prop_changed_signal(
+ DBusConnection *con, const char *path, const char *interface,
+ const struct wpa_dbus_object_desc *obj_dsc)
{
+ DBusMessage *msg;
+ DBusMessageIter signal_iter, dict_iter;
- struct wpa_dbus_signal_desc *signal_dsc = obj_dsc->signals;
- struct wpa_dbus_signal_desc *prev_desc;
- int args_num = 0;
- int interface_len, signal_len, i, len, error = 0;
+ msg = dbus_message_new_signal(path, interface, "PropertiesChanged");
+ if (msg == NULL)
+ return;
- prev_desc = NULL;
- while (signal_dsc) {
- prev_desc = signal_dsc;
- signal_dsc = signal_dsc->next;
- }
+ dbus_message_iter_init_append(msg, &signal_iter);
- /* count args */
- if (args) {
- while (args[args_num].name && args[args_num].type)
- args_num++;
+ if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
+ "{sv}", &dict_iter) ||
+ !put_changed_properties(obj_dsc, interface, &dict_iter, 1) ||
+ !dbus_message_iter_close_container(&signal_iter, &dict_iter)) {
+ wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
+ __func__);
+ } else {
+ dbus_connection_send(con, msg, NULL);
}
- signal_dsc = os_zalloc(sizeof(struct wpa_dbus_signal_desc) +
- args_num * sizeof(struct wpa_dbus_argument));
- if (!signal_dsc)
- goto err;
-
- if (prev_desc == NULL)
- obj_dsc->signals = signal_dsc;
- else
- prev_desc->next = signal_dsc;
-
- /* copy interface name */
- interface_len = strlen(dbus_interface) + 1;
- signal_dsc->dbus_interface = os_malloc(interface_len);
- if (!signal_dsc->dbus_interface)
- goto err;
- os_strncpy(signal_dsc->dbus_interface, dbus_interface, interface_len);
-
- /* copy signal name */
- signal_len = strlen(dbus_signal) + 1;
- signal_dsc->dbus_signal = os_malloc(signal_len);
- if (!signal_dsc->dbus_signal)
- goto err;
- os_strncpy(signal_dsc->dbus_signal, dbus_signal, signal_len);
-
- /* copy arguments */
- signal_dsc->args_num = args_num;
- for (i = 0; i < args_num; i++) {
- len = os_strlen(args[i].name) + 1;
- signal_dsc->args[i].name = os_malloc(len);
- if (!signal_dsc->args[i].name) {
- error = 1;
- continue;
- }
- os_strncpy(signal_dsc->args[i].name, args[i].name, len);
+ dbus_message_unref(msg);
+}
- len = strlen(args[i].type) + 1;
- signal_dsc->args[i].type = os_malloc(len);
- if (!signal_dsc->args[i].type) {
- error = 1;
- continue;
- }
- os_strncpy(signal_dsc->args[i].type, args[i].type, len);
- }
- if (error)
- goto err;
- signal_dsc->next = NULL;
+static void send_prop_changed_signal(
+ DBusConnection *con, const char *path, const char *interface,
+ const struct wpa_dbus_object_desc *obj_dsc)
+{
+ /*
+ * First, send property change notification on the standardized
+ * org.freedesktop.DBus.Properties interface. This call will not
+ * clear the property change bits, so that they are preserved for
+ * the call that follows.
+ */
+ do_send_prop_changed_signal(con, path, interface, obj_dsc);
- return 0;
+ /*
+ * Now send PropertiesChanged on our own interface for backwards
+ * compatibility. This is deprecated and will be removed in a future
+ * release.
+ */
+ do_send_deprecated_prop_changed_signal(con, path, interface, obj_dsc);
+
+ /* Property change bits have now been cleared. */
+}
+
+
+static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx)
+{
+ DBusConnection *con = eloop_ctx;
+ struct wpa_dbus_object_desc *obj_desc = timeout_ctx;
+
+ wpa_printf(MSG_DEBUG,
+ "dbus: %s: Timeout - sending changed properties of object %s",
+ __func__, obj_desc->path);
+ wpa_dbus_flush_object_changed_properties(con, obj_desc->path);
+}
-err:
- wpa_printf(MSG_WARNING, "Failed to register dbus signal %s in "
- "interface %s", dbus_signal, dbus_interface);
- if (signal_dsc) {
- os_free(signal_dsc->dbus_interface);
- os_free(signal_dsc->dbus_signal);
- for (i = 0; i < signal_dsc->args_num; i++) {
- os_free(signal_dsc->args[i].name);
- os_free(signal_dsc->args[i].type);
- }
- if (prev_desc == NULL)
- obj_dsc->signals = NULL;
- else
- prev_desc->next = NULL;
+static void recursive_flush_changed_properties(DBusConnection *con,
+ const char *path)
+{
+ char **objects = NULL;
+ char subobj_path[WPAS_DBUS_OBJECT_PATH_MAX];
+ int i;
+
+ wpa_dbus_flush_object_changed_properties(con, path);
+
+ if (!dbus_connection_list_registered(con, path, &objects))
+ goto out;
- os_free(signal_dsc);
+ for (i = 0; objects[i]; i++) {
+ os_snprintf(subobj_path, WPAS_DBUS_OBJECT_PATH_MAX,
+ "%s/%s", path, objects[i]);
+ recursive_flush_changed_properties(con, subobj_path);
}
- return -1;
+out:
+ dbus_free_string_array(objects);
}
/**
- * wpa_dbus_property_register - Registers DBus property for given object
- * @obj_dsc: Object description for which a property will be registered
- * @dbus_interface: DBus interface under which method will be registered
- * @dbus_property: a name the property will be registered with
- * @type: a property type signature in form of DBus type description
- * @getter: a function called in order to get property value
- * @setter: a function called in order to set property value
- * @user_data: additional argument passed to setter or getter
- * @user_data_free_func: function used to free additional argument
- * @access: property access permissions specifier (R, W or RW)
- * Returns: Zero on success and -1 on failure
+ * wpa_dbus_flush_all_changed_properties - Send all PropertiesChanged signals
+ * @con: DBus connection
*
- * Registers DBus property under given name and interface for the object.
- * Property are set with giver setter function and get with getter.
- * Additional argument is passed to getter or setter. Getter or setter
- * are required to return DBusMessage which is response to Set/Get method
- * calls. Every property must be registered by this function before being
- * used.
+ * Traverses through all registered objects and sends PropertiesChanged for
+ * each properties.
*/
-int wpa_dbus_property_register(struct wpa_dbus_object_desc *obj_dsc,
- const char *dbus_interface,
- const char *dbus_property,
- const char *type,
- WPADBusPropertyAccessor getter,
- WPADBusPropertyAccessor setter,
- void *user_data,
- WPADBusArgumentFreeFunction user_data_free_func,
- enum dbus_prop_access _access)
+void wpa_dbus_flush_all_changed_properties(DBusConnection *con)
{
- struct wpa_dbus_property_desc *property_dsc = obj_dsc->properties;
- struct wpa_dbus_property_desc *prev_desc;
- int interface_len, property_len, type_len;
-
- prev_desc = NULL;
- while (property_dsc) {
- prev_desc = property_dsc;
- property_dsc = property_dsc->next;
- }
-
- property_dsc = os_zalloc(sizeof(struct wpa_dbus_property_desc));
- if (!property_dsc)
- goto err;
-
- if (prev_desc == NULL)
- obj_dsc->properties = property_dsc;
- else
- prev_desc->next = property_dsc;
-
- /* copy interface name */
- interface_len = os_strlen(dbus_interface) + 1;
- property_dsc->dbus_interface = os_malloc(interface_len);
- if (!property_dsc->dbus_interface)
- goto err;
- os_strncpy(property_dsc->dbus_interface, dbus_interface,
- interface_len);
-
- /* copy property name */
- property_len = os_strlen(dbus_property) + 1;
- property_dsc->dbus_property = os_malloc(property_len);
- if (!property_dsc->dbus_property)
- goto err;
- os_strncpy(property_dsc->dbus_property, dbus_property, property_len);
-
- /* copy property type */
- type_len = os_strlen(type) + 1;
- property_dsc->type = os_malloc(type_len);
- if (!property_dsc->type)
- goto err;
- os_strncpy(property_dsc->type, type, type_len);
-
- property_dsc->getter = getter;
- property_dsc->setter = setter;
- property_dsc->user_data = user_data;
- property_dsc->user_data_free_func = user_data_free_func;
- property_dsc->access = _access;
- property_dsc->next = NULL;
+ recursive_flush_changed_properties(con, WPAS_DBUS_NEW_PATH);
+}
- return 0;
-err:
- wpa_printf(MSG_WARNING, "Failed to register dbus property %s in "
- "interface %s", dbus_property, dbus_interface);
- if (property_dsc) {
- os_free(property_dsc->dbus_interface);
- os_free(property_dsc->dbus_property);
- os_free(property_dsc->type);
+/**
+ * wpa_dbus_flush_object_changed_properties - Send PropertiesChanged for object
+ * @con: DBus connection
+ * @path: path to a DBus object for which PropertiesChanged will be sent.
+ *
+ * Iterates over all properties registered with object and for each interface
+ * containing properties marked as changed, sends a PropertiesChanged signal
+ * containing names and new values of properties that have changed.
+ *
+ * You need to call this function after wpa_dbus_mark_property_changed()
+ * if you want to send PropertiesChanged signal immediately (i.e., without
+ * waiting timeout to expire). PropertiesChanged signal for an object is sent
+ * automatically short time after first marking property as changed. All
+ * PropertiesChanged signals are sent automatically after responding on DBus
+ * message, so if you marked a property changed as a result of DBus call
+ * (e.g., param setter), you usually do not need to call this function.
+ */
+void wpa_dbus_flush_object_changed_properties(DBusConnection *con,
+ const char *path)
+{
+ struct wpa_dbus_object_desc *obj_desc = NULL;
+ const struct wpa_dbus_property_desc *dsc;
+ int i;
- if (prev_desc == NULL)
- obj_dsc->properties = NULL;
- else
- prev_desc->next = NULL;
+ dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
+ if (!obj_desc)
+ return;
+ eloop_cancel_timeout(flush_object_timeout_handler, con, obj_desc);
- os_free(property_dsc);
+ for (dsc = obj_desc->properties, i = 0; dsc && dsc->dbus_property;
+ dsc++, i++) {
+ if (obj_desc->prop_changed_flags == NULL ||
+ !obj_desc->prop_changed_flags[i])
+ continue;
+ send_prop_changed_signal(con, path, dsc->dbus_interface,
+ obj_desc);
}
-
- return -1;
}
+#define WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT 5000
+
+
/**
- * wpas_dbus_signal_network_added - Send a property changed signal
+ * wpa_dbus_mark_property_changed - Mark a property as changed and
* @iface: dbus priv struct
- * @property_getter: propperty getter used to fetch new property value
- * @getter_arg: argument passed to property getter
- * @path: path to object which property has changed
- * @interface_name: signal and property interface
- * @property_name: name of property which has changed
+ * @path: path to DBus object which property has changed
+ * @interface: interface containing changed property
+ * @property: property name which has changed
*
- * Notify listeners about changing value of some property. Signal
- * contains property name and its value fetched using given property
- * getter.
+ * Iterates over all properties registered with an object and marks the one
+ * given in parameters as changed. All parameters registered for an object
+ * within a single interface will be aggregated together and sent in one
+ * PropertiesChanged signal when function
+ * wpa_dbus_flush_object_changed_properties() is called.
*/
-void wpa_dbus_signal_property_changed(struct ctrl_iface_dbus_new_priv *iface,
- WPADBusPropertyAccessor property_getter,
- void *getter_arg,
- const char *path,
- const char *interface_name,
- const char *property_name)
+void wpa_dbus_mark_property_changed(struct wpas_dbus_priv *iface,
+ const char *path, const char *interface,
+ const char *property)
{
+ struct wpa_dbus_object_desc *obj_desc = NULL;
+ const struct wpa_dbus_property_desc *dsc;
+ int i = 0;
- DBusConnection *connection;
- DBusMessage *_signal, *getter_reply;
- DBusMessageIter prop_iter, signal_iter, dict_iter, entry_iter;
-
- if (!iface)
+ if (iface == NULL)
return;
- connection = iface->con;
- if (!property_getter) {
- wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
- "[dbus]: property getter not specified");
+ dbus_connection_get_object_path_data(iface->con, path,
+ (void **) &obj_desc);
+ if (!obj_desc) {
+ wpa_printf(MSG_ERROR,
+ "dbus: wpa_dbus_property_changed: could not obtain object's private data: %s",
+ path);
return;
}
- if (!path || !interface_name || !property_name) {
- wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
- "[dbus]: path interface of property not specified");
- return;
- }
+ for (dsc = obj_desc->properties; dsc && dsc->dbus_property; dsc++, i++)
+ if (os_strcmp(property, dsc->dbus_property) == 0 &&
+ os_strcmp(interface, dsc->dbus_interface) == 0) {
+ if (obj_desc->prop_changed_flags)
+ obj_desc->prop_changed_flags[i] = 1;
+ break;
+ }
- getter_reply = property_getter(NULL, getter_arg);
- if (!getter_reply ||
- dbus_message_get_type(getter_reply) == DBUS_MESSAGE_TYPE_ERROR) {
- wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
- "[dbus]: cannot get new value of property %s",
- property_name);
+ if (!dsc || !dsc->dbus_property) {
+ wpa_printf(MSG_ERROR,
+ "dbus: wpa_dbus_property_changed: no property %s in object %s",
+ property, path);
return;
}
- _signal = dbus_message_new_signal(path, interface_name,
- "PropertiesChanged");
- if (!_signal) {
- wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
- "[dbus]: cannot allocate signal");
- dbus_message_unref(getter_reply);
- return;
+ if (!eloop_is_timeout_registered(flush_object_timeout_handler,
+ iface->con, obj_desc)) {
+ eloop_register_timeout(0, WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT,
+ flush_object_timeout_handler,
+ iface->con, obj_desc);
}
+}
- dbus_message_iter_init(getter_reply, &prop_iter);
- dbus_message_iter_init_append(_signal, &signal_iter);
- if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
- "{sv}", &dict_iter)) {
- wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
- "[dbus]: out of memory. cannot open dictionary");
- goto err;
+/**
+ * wpa_dbus_get_object_properties - Put object's properties into dictionary
+ * @iface: dbus priv struct
+ * @path: path to DBus object which properties will be obtained
+ * @interface: interface name which properties will be obtained
+ * @iter: DBus message iter at which to append property dictionary.
+ *
+ * Iterates over all properties registered with object and execute getters
+ * of those, which are readable and which interface matches interface
+ * specified as argument. Obtained properties values are stored in
+ * dict_iter dictionary.
+ */
+dbus_bool_t wpa_dbus_get_object_properties(struct wpas_dbus_priv *iface,
+ const char *path,
+ const char *interface,
+ DBusMessageIter *iter)
+{
+ struct wpa_dbus_object_desc *obj_desc = NULL;
+ DBusMessageIter dict_iter;
+ DBusError error;
+
+ dbus_connection_get_object_path_data(iface->con, path,
+ (void **) &obj_desc);
+ if (!obj_desc) {
+ wpa_printf(MSG_ERROR,
+ "dbus: %s: could not obtain object's private data: %s",
+ __func__, path);
+ return FALSE;
}
- if (!dbus_message_iter_open_container(&dict_iter, DBUS_TYPE_DICT_ENTRY,
- NULL, &entry_iter)) {
- wpa_printf(MSG_ERROR, "iwpa_dbus_signal_property_changed"
- "[dbus]: out of memory. cannot open dictionary "
- "element");
- goto err;
+ if (!wpa_dbus_dict_open_write(iter, &dict_iter)) {
+ wpa_printf(MSG_ERROR, "dbus: %s: failed to open message dict",
+ __func__);
+ return FALSE;
}
- if (!dbus_message_iter_append_basic(&entry_iter, DBUS_TYPE_STRING,
- &property_name)) {
- wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
- "[dbus]: out of memory. cannot open add property "
- "name");
- goto err;
+ dbus_error_init(&error);
+ if (!fill_dict_with_properties(&dict_iter, obj_desc->properties,
+ interface, obj_desc->user_data,
+ &error)) {
+ wpa_printf(MSG_ERROR,
+ "dbus: %s: failed to get object properties: (%s) %s",
+ __func__,
+ dbus_error_is_set(&error) ? error.name : "none",
+ dbus_error_is_set(&error) ? error.message : "none");
+ dbus_error_free(&error);
+ return FALSE;
}
- recursive_iter_copy(&prop_iter, &entry_iter);
+ return wpa_dbus_dict_close_write(iter, &dict_iter);
+}
- if (!dbus_message_iter_close_container(&dict_iter, &entry_iter)) {
- wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
- "[dbus]: out of memory. cannot close dictionary "
- "element");
- goto err;
- }
+/**
+ * wpas_dbus_new_decompose_object_path - Decompose an interface object path into parts
+ * @path: The dbus object path
+ * @sep: Separating part (e.g., "Networks" or "PersistentGroups")
+ * @item: (out) The part following the specified separator, if any
+ * Returns: The object path of the interface this path refers to
+ *
+ * For a given object path, decomposes the object path into object id and
+ * requested part, if those parts exist. The caller is responsible for freeing
+ * the returned value. The *item pointer points to that allocated value and must
+ * not be freed separately.
+ *
+ * As an example, path = "/fi/w1/wpa_supplicant1/Interfaces/1/Networks/0" and
+ * sep = "Networks" would result in "/fi/w1/wpa_supplicant1/Interfaces/1"
+ * getting returned and *items set to point to "0".
+ */
+char * wpas_dbus_new_decompose_object_path(const char *path, const char *sep,
+ char **item)
+{
+ const unsigned int dev_path_prefix_len =
+ os_strlen(WPAS_DBUS_NEW_PATH_INTERFACES "/");
+ char *obj_path_only;
+ char *pos;
+ size_t sep_len;
- if (!dbus_message_iter_close_container(&signal_iter, &dict_iter)) {
- wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
- "[dbus]: out of memory. cannot close dictionary");
- goto err;
- }
+ *item = NULL;
- dbus_connection_send(connection, _signal, NULL);
+ /* Verify that this starts with our interface prefix */
+ if (os_strncmp(path, WPAS_DBUS_NEW_PATH_INTERFACES "/",
+ dev_path_prefix_len) != 0)
+ return NULL; /* not our path */
-err:
- dbus_message_unref(getter_reply);
- dbus_message_unref(_signal);
+ /* Ensure there's something at the end of the path */
+ if ((path + dev_path_prefix_len)[0] == '\0')
+ return NULL;
+
+ obj_path_only = os_strdup(path);
+ if (obj_path_only == NULL)
+ return NULL;
+
+ pos = obj_path_only + dev_path_prefix_len;
+ pos = os_strchr(pos, '/');
+ if (pos == NULL)
+ return obj_path_only; /* no next item on the path */
+
+ /* Separate network interface prefix from the path */
+ *pos++ = '\0';
+
+ sep_len = os_strlen(sep);
+ if (os_strncmp(pos, sep, sep_len) != 0 || pos[sep_len] != '/')
+ return obj_path_only; /* no match */
+
+ /* return a pointer to the requested item */
+ *item = pos + sep_len + 1;
+ return obj_path_only;
+}
+
+/**
+ * wpas_dbus_reply_new_from_error - Create a new D-Bus error message from a
+ * dbus error structure
+ * @message: The original request message for which the error is a reply
+ * @error: The error containing a name and a descriptive error cause
+ * @fallback_name: A generic error name if @error was not set
+ * @fallback_string: A generic error string if @error was not set
+ * Returns: A new D-Bus error message
+ *
+ * Given a DBusMessage structure, creates a new D-Bus error message using
+ * the error name and string contained in that structure.
+ */
+DBusMessage * wpas_dbus_reply_new_from_error(DBusMessage *message,
+ DBusError *error,
+ const char *fallback_name,
+ const char *fallback_string)
+{
+ if (error && error->name && error->message) {
+ return dbus_message_new_error(message, error->name,
+ error->message);
+ }
+ if (fallback_name && fallback_string) {
+ return dbus_message_new_error(message, fallback_name,
+ fallback_string);
+ }
+ return NULL;
}