5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <dbus/dbus.h>
31 #include "supplicant-dbus.h"
35 static DBusConnection *connection;
37 void supplicant_dbus_setup(DBusConnection *conn)
42 void supplicant_dbus_array_foreach(DBusMessageIter *iter,
43 supplicant_dbus_array_function function,
46 DBusMessageIter entry;
48 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
51 dbus_message_iter_recurse(iter, &entry);
53 while (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_INVALID) {
55 function(&entry, user_data);
57 dbus_message_iter_next(&entry);
61 void supplicant_dbus_property_foreach(DBusMessageIter *iter,
62 supplicant_dbus_property_function function,
67 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
70 dbus_message_iter_recurse(iter, &dict);
72 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
73 DBusMessageIter entry, value;
76 dbus_message_iter_recurse(&dict, &entry);
78 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
81 dbus_message_iter_get_basic(&entry, &key);
82 dbus_message_iter_next(&entry);
84 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
87 dbus_message_iter_recurse(&entry, &value);
90 if (strcmp(key, "Properties") == 0)
91 supplicant_dbus_property_foreach(&value,
93 else if (function != NULL)
94 function(key, &value, user_data);
97 dbus_message_iter_next(&dict);
101 struct property_data {
102 supplicant_dbus_property_function function;
106 static void property_get_all_reply(DBusPendingCall *call, void *user_data)
108 struct property_data *data = user_data;
110 DBusMessageIter iter;
112 reply = dbus_pending_call_steal_reply(call);
116 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
119 if (dbus_message_iter_init(reply, &iter) == FALSE)
122 supplicant_dbus_property_foreach(&iter, data->function,
125 if (data->function != NULL)
126 data->function(NULL, NULL, data->user_data);
129 dbus_message_unref(reply);
132 int supplicant_dbus_property_get_all(const char *path, const char *interface,
133 supplicant_dbus_property_function function,
136 struct property_data *data;
137 DBusMessage *message;
138 DBusPendingCall *call;
140 if (path == NULL || interface == NULL)
143 data = dbus_malloc0(sizeof(*data));
147 message = dbus_message_new_method_call(SUPPLICANT_SERVICE, path,
148 DBUS_INTERFACE_PROPERTIES, "GetAll");
149 if (message == NULL) {
154 dbus_message_set_auto_start(message, FALSE);
156 dbus_message_append_args(message, DBUS_TYPE_STRING, &interface, NULL);
158 if (dbus_connection_send_with_reply(connection, message,
159 &call, TIMEOUT) == FALSE) {
160 dbus_message_unref(message);
166 dbus_message_unref(message);
171 data->function = function;
172 data->user_data = user_data;
174 dbus_pending_call_set_notify(call, property_get_all_reply,
177 dbus_message_unref(message);