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
31 #include "supplicant.h"
33 #define DBG(fmt, arg...) do { \
34 syslog(LOG_DEBUG, "%s() " fmt, __FUNCTION__ , ## arg); \
37 #define SUPPLICANT_SERVICE "fi.w1.wpa_supplicant1"
38 #define SUPPLICANT_INTERFACE "fi.w1.wpa_supplicant1"
39 #define SUPPLICANT_PATH "/fi/w1/wpa_supplicant1"
43 static DBusConnection *connection;
45 static void show_property(const char *key, DBusMessageIter *iter)
47 DBusMessageIter array;
51 switch (dbus_message_iter_get_arg_type(iter)) {
52 case DBUS_TYPE_STRING:
53 dbus_message_iter_get_basic(iter, &str);
54 DBG("%s = %s", key, str);
57 dbus_message_iter_get_basic(iter, &byte);
58 DBG("%s = %u", key, byte);
61 DBG("%s = {array}", key);
62 dbus_message_iter_recurse(iter, &array);
63 while (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_INVALID) {
64 dbus_message_iter_get_basic(&array, &str);
66 dbus_message_iter_next(&array);
75 static void properties_decode(DBusMessageIter *iter)
79 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY) {
80 syslog(LOG_ERR, "Invalid message type");
84 dbus_message_iter_recurse(iter, &dict);
86 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
87 DBusMessageIter entry, value;
90 dbus_message_iter_recurse(&dict, &entry);
92 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
95 dbus_message_iter_get_basic(&entry, &key);
96 dbus_message_iter_next(&entry);
98 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
101 dbus_message_iter_recurse(&entry, &value);
103 show_property(key, &value);
105 dbus_message_iter_next(&dict);
109 static void properties_get_all_reply(DBusPendingCall *call, void *user_data)
112 DBusMessageIter iter;
114 DBG("call %p", call);
116 reply = dbus_pending_call_steal_reply(call);
120 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
123 if (dbus_message_iter_init(reply, &iter) == FALSE)
128 properties_decode(&iter);
130 dbus_message_unref(reply);
135 dbus_message_unref(reply);
138 static int properties_get_all(const char *path, const char *interface)
140 DBusMessage *message;
141 DBusPendingCall *call;
145 message = dbus_message_new_method_call(SUPPLICANT_SERVICE, path,
146 DBUS_INTERFACE_PROPERTIES, "GetAll");
150 dbus_message_set_auto_start(message, FALSE);
152 dbus_message_append_args(message, DBUS_TYPE_STRING, &interface, NULL);
154 if (dbus_connection_send_with_reply(connection, message,
155 &call, TIMEOUT) == FALSE) {
156 syslog(LOG_ERR, "Failed to add interface");
157 dbus_message_unref(message);
162 syslog(LOG_ERR, "D-Bus connection not available");
163 dbus_message_unref(message);
167 DBG("call %p", call);
169 dbus_pending_call_set_notify(call, properties_get_all_reply,
172 dbus_message_unref(message);
177 int supplicant_init(void)
181 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
182 if (connection == NULL)
185 properties_get_all(SUPPLICANT_PATH, SUPPLICANT_INTERFACE);
190 void supplicant_exit(void)
194 if (connection != NULL)
195 dbus_connection_unref(connection);