File: station-debug.c

package info (click to toggle)
iwd 3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,112 kB
  • sloc: ansic: 140,705; sh: 4,960; makefile: 714
file content (443 lines) | stat: -rw-r--r-- 10,477 bytes parent folder | download | duplicates (3)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include <ell/ell.h>
#include "ell/useful.h"

#include "client/command.h"
#include "client/dbus-proxy.h"
#include "client/device.h"
#include "client/display.h"
#include "client/properties.h"
#include "client/network.h"
#include "src/util.h"

struct station_debug {
	struct l_queue *network_list;
	bool autoconnect;
};

struct network {
	char *ssid;
	struct l_queue *bss_list;
};

struct bss {
	char addr[18];
	uint32_t frequency;
	int8_t rssi;
	int32_t rank;
	uint8_t mde[3];
};

static void network_free(void *data)
{
	struct network *network = data;

	l_queue_destroy(network->bss_list, l_free);
}

static void *station_debug_create(void)
{
	struct station_debug *debug = l_new(struct station_debug, 1);

	debug->network_list = l_queue_new();

	return debug;
}

static void station_debug_destroy(void *data)
{
	struct station_debug *debug = data;

	l_queue_destroy(debug->network_list, network_free);

	l_free(debug);
}

static void check_errors_method_callback(struct l_dbus_message *message,
								void *user_data)
{
	dbus_message_has_error(message);
}

static enum cmd_status cmd_debug_connect(const char *device_name,
						char **argv, int argc)
{

	const struct proxy_interface *debug_i;
	uint8_t addr[6];

	if (argc < 1)
		return CMD_STATUS_INVALID_ARGS;

	debug_i = device_proxy_find(device_name, IWD_STATION_DEBUG_INTERFACE);
	if (!debug_i) {
		display_error("IWD not in developer mode");
		return CMD_STATUS_INVALID_VALUE;
	}

	if (!util_string_to_address(argv[0], addr))
		return CMD_STATUS_INVALID_ARGS;

	proxy_interface_method_call(debug_i, "ConnectBssid", "ay",
					check_errors_method_callback, 6,
					addr[0], addr[1], addr[2],
					addr[3], addr[4], addr[5]);
	return CMD_STATUS_TRIGGERED;
}

static enum cmd_status cmd_debug_roam(const char *device_name,
						char **argv, int argc)
{
	const struct proxy_interface *debug_i;
	uint8_t addr[6];

	if (argc < 1)
		return CMD_STATUS_INVALID_ARGS;

	debug_i = device_proxy_find(device_name, IWD_STATION_DEBUG_INTERFACE);
	if (!debug_i) {
		display_error("IWD not in developer mode");
		return CMD_STATUS_INVALID_VALUE;
	}

	if (!util_string_to_address(argv[0], addr))
		return CMD_STATUS_INVALID_ARGS;

	proxy_interface_method_call(debug_i, "Roam", "ay",
					check_errors_method_callback, 6,
					addr[0], addr[1], addr[2],
					addr[3], addr[4], addr[5]);
	return CMD_STATUS_TRIGGERED;
}

static void get_byte_array(struct l_dbus_message_iter *variant, uint8_t *out,
				unsigned int len)
{
	struct l_dbus_message_iter array;
	uint32_t elems = 6;
	uint8_t *a;

	if (!l_dbus_message_iter_get_variant(variant, "ay", &array))
		goto error;

	if (!l_dbus_message_iter_get_fixed_array(&array, &a, &elems))
		goto error;

	if (elems != len)
		goto error;

	memcpy(out, a, len);

	return;

error:
	display_error("Invalid Address element");
}

static void get_address(struct l_dbus_message_iter *variant,
			char addr_out[static 18])
{
	char *s;

	if (!l_dbus_message_iter_get_variant(variant, "s", &s))
		goto error;

	if (strlen(s) != 17)
		goto error;

	strcpy(addr_out, s);

	return;

error:
	display_error("Invalid Address element");
}

static uint32_t get_u32(struct l_dbus_message_iter *variant)
{
	uint32_t u32 = 0;

	if (!l_dbus_message_iter_get_variant(variant, "u", &u32))
		display_error("Invalid Frequency element");

	return u32;
}

static uint32_t get_u16(struct l_dbus_message_iter *variant)
{
	uint32_t u16 = 0;

	if (!l_dbus_message_iter_get_variant(variant, "q", &u16))
		display_error("Invalid Frequency element");

	return u16;
}

static uint32_t get_i32(struct l_dbus_message_iter *variant)
{
	int32_t i32 = 0;

	if (!l_dbus_message_iter_get_variant(variant, "i", &i32))
		display_error("Invalid Frequency element");

	return i32;
}

static void display_bss(struct bss *bss)
{
	char row[128];

	sprintf(row, "%s%-*s  %s  %-*i  %-*u  %-*i  %02x%02x%02x",
		MARGIN MARGIN, 4, "", bss->addr, 4, bss->rssi,
		6, bss->frequency, 8, bss->rank,
		bss->mde[0], bss->mde[1], bss->mde[2]);

	display("%s\n", row);

	return;
}

static void get_networks_method_callback(struct l_dbus_message *message,
								void *user_data)
{
	struct proxy_interface *debug_i = user_data;
	struct station_debug *debug = proxy_interface_get_data(debug_i);
	struct l_dbus_message_iter iter;
	struct l_dbus_message_iter variant;
	struct l_dbus_message_iter array;
	struct l_dbus_message_iter dict;
	const char *key;

	if (dbus_message_has_error(message))
		return;

	if (!l_dbus_message_get_arguments(message, "a{oaa{sv}}", &iter)) {
		l_error("Failed to parse GetDiagnostics message");
		return;
	}

	display_table_header("Available Networks (debug)",
				"%s%-*s  %-*s  %-*s  %-*s  %-*s  %-*s  %-*s",
				"", 2, "", 4, "SSID", 17, "BSSID", 4, "RSSI",
				6, "Freq", 8, "Rank", 10, "MDE");

	while (l_dbus_message_iter_next_entry(&iter, &key, &array)) {
		struct network *network = l_new(struct network, 1);
		const struct proxy_interface *net_i = network_get_proxy(key);

		network->ssid = l_strdup(network_get_name(net_i));
		network->bss_list = l_queue_new();

		display_table_row(MARGIN MARGIN, 3, 32, network->ssid,
					18, "", 6, "");

		while (l_dbus_message_iter_next_entry(&array, &dict)) {
			struct bss *bss = l_new(struct bss, 1);

			while (l_dbus_message_iter_next_entry(&dict, &key,
								&variant)) {
				if (!strcmp(key, "Address"))
					get_address(&variant, bss->addr);
				else if (!strcmp(key, "Frequency"))
					bss->frequency = get_u32(&variant);
				else if (!strcmp(key, "RSSI"))
					bss->rssi = get_i32(&variant);
				else if (!strcmp(key, "Rank"))
					bss->rank = get_u16(&variant);
				else if (!strcmp(key, "MDE"))
					get_byte_array(&variant, bss->mde, 3);
			}

			display_bss(bss);

			l_queue_push_tail(network->bss_list, bss);
		}

		l_queue_push_tail(debug->network_list, network);
	}

	display_table_footer();
}

static enum cmd_status cmd_debug_get_networks(const char *device_name,
						char **argv, int argc)
{
	const struct proxy_interface *debug_i;

	debug_i = device_proxy_find(device_name, IWD_STATION_DEBUG_INTERFACE);
	if (!debug_i) {
		display_error("IWD not in developer mode");
		return CMD_STATUS_INVALID_VALUE;
	}

	proxy_interface_method_call(debug_i, "GetNetworks", "",
					get_networks_method_callback);

	return CMD_STATUS_TRIGGERED;
}

static char *connect_debug_cmd_arg_completion(const char *text, int state,
						const char *device_name)
{
	const struct proxy_interface *debug_i;
	struct station_debug *debug;
	static const struct l_queue_entry *n_entry;
	static const struct l_queue_entry *b_entry;
	size_t len = strlen(text);

	debug_i = device_proxy_find(device_name, IWD_STATION_DEBUG_INTERFACE);
	if (!debug_i)
		return NULL;

	debug = proxy_interface_get_data(debug_i);

	if (!state)
		n_entry = l_queue_get_entries(debug->network_list);

	while (n_entry) {
		struct network *network = n_entry->data;

		n_entry = n_entry->next;

		if (!b_entry)
			b_entry = l_queue_get_entries(network->bss_list);

		while (b_entry) {
			struct bss *bss = b_entry->data;

			b_entry = b_entry->next;

			if (len > strlen(bss->addr))
				return NULL;

			if (strncmp(text, bss->addr, len))
				continue;

			return l_strdup_printf(MAC, MAC_STR(bss->addr));
		}
	}

	return NULL;
}

static const char *get_autoconnect_tostr(const void *data)
{
	const struct station_debug *debug = data;

	return debug->autoconnect ? "on" : "off";
}

static void update_autoconnect(void *data, struct l_dbus_message_iter *variant)
{
	struct station_debug *debug = data;
	bool value;

	if (!l_dbus_message_iter_get_variant(variant, "b", &value)) {
		debug->autoconnect = false;

		return;
	}

	debug->autoconnect = value;
}

static const struct proxy_interface_property debug_properties[] = {
	{ "AutoConnect",  "b", update_autoconnect,  get_autoconnect_tostr, true,
			properties_builder_append_on_off_variant,
			properties_on_off_opts },
	{ }
};

static enum cmd_status cmd_debug_set_autoconnect(const char *device_name,
						char **argv, int argc)
{
	const struct proxy_interface *proxy = device_proxy_find(device_name,
						IWD_STATION_DEBUG_INTERFACE);

	if (!proxy) {
		display_error("IWD not in developer mode");
		return CMD_STATUS_INVALID_VALUE;
	}

	if (argc != 1)
		return CMD_STATUS_INVALID_ARGS;

	if (!proxy_property_set(proxy, "AutoConnect", argv[0],
						check_errors_method_callback))
		return CMD_STATUS_INVALID_VALUE;

	return CMD_STATUS_TRIGGERED;
}

static const struct command station_debug_commands[] = {
	{ "<wlan>", "connect", "<bssid>", cmd_debug_connect,
					"Connect to a specific BSS", false,
					connect_debug_cmd_arg_completion },
	{ "<wlan>", "roam", "<bssid>", cmd_debug_roam,
					"Roam to a BSS", false },
	{ "<wlan>", "get-networks", NULL, cmd_debug_get_networks,
					"Get networks", true },
	{ "<wlan>", "autoconnect", "on|off", cmd_debug_set_autoconnect,
					"Set AutoConnect property", false },
	{ }
};

static char *family_debug_arg_completion(const char *text, int state)
{
	return device_arg_completion(text, state, station_debug_commands,
						IWD_STATION_DEBUG_INTERFACE);
}

static char *entity_debug_arg_completion(const char *text, int state)
{
	return command_entity_arg_completion(text, state,
						station_debug_commands);
}

static struct command_family station_debug_command_family = {
	.caption = "Station Debug",
	.name = "debug",
	.command_list = station_debug_commands,
	.family_arg_completion = family_debug_arg_completion,
	.entity_arg_completion = entity_debug_arg_completion,
};

static int station_debug_command_family_init(void)
{
	command_family_register(&station_debug_command_family);

	return 0;
}

static void station_debug_command_family_exit(void)
{
	command_family_unregister(&station_debug_command_family);
}

COMMAND_FAMILY(station_debug_command_family, station_debug_command_family_init,
					station_debug_command_family_exit)

static const struct proxy_interface_type_ops station_debug_ops = {
	.create = station_debug_create,
	.destroy = station_debug_destroy,
};

static struct proxy_interface_type station_debug_interface_type = {
	.interface = IWD_STATION_DEBUG_INTERFACE,
	.ops = &station_debug_ops,
	.properties = debug_properties,
};

static int station_debug_interface_init(void)
{
	proxy_interface_type_register(&station_debug_interface_type);

	return 0;
}

static void station_debug_interface_exit(void)
{
	proxy_interface_type_unregister(&station_debug_interface_type);
}

INTERFACE_TYPE(station_debug_interface_type, station_debug_interface_init,
					station_debug_interface_exit)