summaryrefslogtreecommitdiff
path: root/tools/supplicant.c
blob: 0e4ffc23bf98ecc32b72126cf4d55b9225a6a32b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <syslog.h>

#include <gdbus.h>

#include "supplicant.h"

#define DBG(fmt, arg...) do { \
	syslog(LOG_DEBUG, "%s() " fmt, __FUNCTION__ , ## arg); \
} while (0)

#define SUPPLICANT_SERVICE	"fi.w1.wpa_supplicant1"
#define SUPPLICANT_INTERFACE	"fi.w1.wpa_supplicant1"
#define SUPPLICANT_PATH		"/fi/w1/wpa_supplicant1"

#define TIMEOUT 5000

static DBusConnection *connection;

static void show_property(const char *key, DBusMessageIter *iter)
{
	DBusMessageIter array;
	const char *str;
	unsigned char byte;

	switch (dbus_message_iter_get_arg_type(iter)) {
	case DBUS_TYPE_STRING:
		dbus_message_iter_get_basic(iter, &str);
		DBG("%s = %s", key, str);
		break;
	case DBUS_TYPE_BYTE:
		dbus_message_iter_get_basic(iter, &byte);
		DBG("%s = %u", key, byte);
		break;
	case DBUS_TYPE_ARRAY:
		DBG("%s = {array}", key);
		dbus_message_iter_recurse(iter, &array);
		while (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_INVALID) {
			dbus_message_iter_get_basic(&array, &str);
			DBG("  %s", str);
			dbus_message_iter_next(&array);
		}
		break;
	default:
		DBG("%s = ...", key);
		break;
	}
}

static void properties_decode(DBusMessageIter *iter)
{
	DBusMessageIter dict;

	if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY) {
		syslog(LOG_ERR, "Invalid message type");
		return;
	}

	dbus_message_iter_recurse(iter, &dict);

	while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
		DBusMessageIter entry, value;
		const char *key;

		dbus_message_iter_recurse(&dict, &entry);

		if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
			return;

		dbus_message_iter_get_basic(&entry, &key);
		dbus_message_iter_next(&entry);

		if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
			return;

		dbus_message_iter_recurse(&entry, &value);

		show_property(key, &value);

		dbus_message_iter_next(&dict);
	}
}

static void properties_get_all_reply(DBusPendingCall *call, void *user_data)
{
	DBusMessage *reply;
	DBusMessageIter iter;

	DBG("call %p", call);

	reply = dbus_pending_call_steal_reply(call);
	if (reply == NULL)
		return;

	if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
		goto failed;

	if (dbus_message_iter_init(reply, &iter) == FALSE)
		goto failed;

	DBG("success");

	properties_decode(&iter);

	dbus_message_unref(reply);

	return;

failed:
	dbus_message_unref(reply);
}

static int properties_get_all(const char *path, const char *interface)
{
	DBusMessage *message;
	DBusPendingCall *call;

	DBG("");

	message = dbus_message_new_method_call(SUPPLICANT_SERVICE, path,
					DBUS_INTERFACE_PROPERTIES, "GetAll");
	if (message == NULL)
		return -ENOMEM;

	dbus_message_set_auto_start(message, FALSE);

	dbus_message_append_args(message, DBUS_TYPE_STRING, &interface, NULL);

	if (dbus_connection_send_with_reply(connection, message,
						&call, TIMEOUT) == FALSE) {
		syslog(LOG_ERR, "Failed to add interface");
		dbus_message_unref(message);
		return -EIO;
	}

	if (call == NULL) {
		syslog(LOG_ERR, "D-Bus connection not available");
		dbus_message_unref(message);
		return -EIO;
	}

	DBG("call %p", call);

	dbus_pending_call_set_notify(call, properties_get_all_reply,
								NULL, NULL);

	dbus_message_unref(message);

	return 0;
}

int supplicant_init(void)
{
	DBG("");

	connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
	if (connection == NULL)
		return -EIO;

	properties_get_all(SUPPLICANT_PATH, SUPPLICANT_INTERFACE);

	return 0;
}

void supplicant_exit(void)
{
	DBG("");

	if (connection != NULL)
		dbus_connection_unref(connection);
}