eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

station-debug.c (10477B)


      1 #include <ell/ell.h>
      2 #include "ell/useful.h"
      3 
      4 #include "client/command.h"
      5 #include "client/dbus-proxy.h"
      6 #include "client/device.h"
      7 #include "client/display.h"
      8 #include "client/properties.h"
      9 #include "client/network.h"
     10 #include "src/util.h"
     11 
     12 struct station_debug {
     13 	struct l_queue *network_list;
     14 	bool autoconnect;
     15 };
     16 
     17 struct network {
     18 	char *ssid;
     19 	struct l_queue *bss_list;
     20 };
     21 
     22 struct bss {
     23 	char addr[18];
     24 	uint32_t frequency;
     25 	int8_t rssi;
     26 	int32_t rank;
     27 	uint8_t mde[3];
     28 };
     29 
     30 static void network_free(void *data)
     31 {
     32 	struct network *network = data;
     33 
     34 	l_queue_destroy(network->bss_list, l_free);
     35 }
     36 
     37 static void *station_debug_create(void)
     38 {
     39 	struct station_debug *debug = l_new(struct station_debug, 1);
     40 
     41 	debug->network_list = l_queue_new();
     42 
     43 	return debug;
     44 }
     45 
     46 static void station_debug_destroy(void *data)
     47 {
     48 	struct station_debug *debug = data;
     49 
     50 	l_queue_destroy(debug->network_list, network_free);
     51 
     52 	l_free(debug);
     53 }
     54 
     55 static void check_errors_method_callback(struct l_dbus_message *message,
     56 								void *user_data)
     57 {
     58 	dbus_message_has_error(message);
     59 }
     60 
     61 static enum cmd_status cmd_debug_connect(const char *device_name,
     62 						char **argv, int argc)
     63 {
     64 
     65 	const struct proxy_interface *debug_i;
     66 	uint8_t addr[6];
     67 
     68 	if (argc < 1)
     69 		return CMD_STATUS_INVALID_ARGS;
     70 
     71 	debug_i = device_proxy_find(device_name, IWD_STATION_DEBUG_INTERFACE);
     72 	if (!debug_i) {
     73 		display_error("IWD not in developer mode");
     74 		return CMD_STATUS_INVALID_VALUE;
     75 	}
     76 
     77 	if (!util_string_to_address(argv[0], addr))
     78 		return CMD_STATUS_INVALID_ARGS;
     79 
     80 	proxy_interface_method_call(debug_i, "ConnectBssid", "ay",
     81 					check_errors_method_callback, 6,
     82 					addr[0], addr[1], addr[2],
     83 					addr[3], addr[4], addr[5]);
     84 	return CMD_STATUS_TRIGGERED;
     85 }
     86 
     87 static enum cmd_status cmd_debug_roam(const char *device_name,
     88 						char **argv, int argc)
     89 {
     90 	const struct proxy_interface *debug_i;
     91 	uint8_t addr[6];
     92 
     93 	if (argc < 1)
     94 		return CMD_STATUS_INVALID_ARGS;
     95 
     96 	debug_i = device_proxy_find(device_name, IWD_STATION_DEBUG_INTERFACE);
     97 	if (!debug_i) {
     98 		display_error("IWD not in developer mode");
     99 		return CMD_STATUS_INVALID_VALUE;
    100 	}
    101 
    102 	if (!util_string_to_address(argv[0], addr))
    103 		return CMD_STATUS_INVALID_ARGS;
    104 
    105 	proxy_interface_method_call(debug_i, "Roam", "ay",
    106 					check_errors_method_callback, 6,
    107 					addr[0], addr[1], addr[2],
    108 					addr[3], addr[4], addr[5]);
    109 	return CMD_STATUS_TRIGGERED;
    110 }
    111 
    112 static void get_byte_array(struct l_dbus_message_iter *variant, uint8_t *out,
    113 				unsigned int len)
    114 {
    115 	struct l_dbus_message_iter array;
    116 	uint32_t elems = 6;
    117 	uint8_t *a;
    118 
    119 	if (!l_dbus_message_iter_get_variant(variant, "ay", &array))
    120 		goto error;
    121 
    122 	if (!l_dbus_message_iter_get_fixed_array(&array, &a, &elems))
    123 		goto error;
    124 
    125 	if (elems != len)
    126 		goto error;
    127 
    128 	memcpy(out, a, len);
    129 
    130 	return;
    131 
    132 error:
    133 	display_error("Invalid Address element");
    134 }
    135 
    136 static void get_address(struct l_dbus_message_iter *variant,
    137 			char addr_out[static 18])
    138 {
    139 	char *s;
    140 
    141 	if (!l_dbus_message_iter_get_variant(variant, "s", &s))
    142 		goto error;
    143 
    144 	if (strlen(s) != 17)
    145 		goto error;
    146 
    147 	strcpy(addr_out, s);
    148 
    149 	return;
    150 
    151 error:
    152 	display_error("Invalid Address element");
    153 }
    154 
    155 static uint32_t get_u32(struct l_dbus_message_iter *variant)
    156 {
    157 	uint32_t u32 = 0;
    158 
    159 	if (!l_dbus_message_iter_get_variant(variant, "u", &u32))
    160 		display_error("Invalid Frequency element");
    161 
    162 	return u32;
    163 }
    164 
    165 static uint32_t get_u16(struct l_dbus_message_iter *variant)
    166 {
    167 	uint32_t u16 = 0;
    168 
    169 	if (!l_dbus_message_iter_get_variant(variant, "q", &u16))
    170 		display_error("Invalid Frequency element");
    171 
    172 	return u16;
    173 }
    174 
    175 static uint32_t get_i32(struct l_dbus_message_iter *variant)
    176 {
    177 	int32_t i32 = 0;
    178 
    179 	if (!l_dbus_message_iter_get_variant(variant, "i", &i32))
    180 		display_error("Invalid Frequency element");
    181 
    182 	return i32;
    183 }
    184 
    185 static void display_bss(struct bss *bss)
    186 {
    187 	char row[128];
    188 
    189 	sprintf(row, "%s%-*s  %s  %-*i  %-*u  %-*i  %02x%02x%02x",
    190 		MARGIN MARGIN, 4, "", bss->addr, 4, bss->rssi,
    191 		6, bss->frequency, 8, bss->rank,
    192 		bss->mde[0], bss->mde[1], bss->mde[2]);
    193 
    194 	display("%s\n", row);
    195 
    196 	return;
    197 }
    198 
    199 static void get_networks_method_callback(struct l_dbus_message *message,
    200 								void *user_data)
    201 {
    202 	struct proxy_interface *debug_i = user_data;
    203 	struct station_debug *debug = proxy_interface_get_data(debug_i);
    204 	struct l_dbus_message_iter iter;
    205 	struct l_dbus_message_iter variant;
    206 	struct l_dbus_message_iter array;
    207 	struct l_dbus_message_iter dict;
    208 	const char *key;
    209 
    210 	if (dbus_message_has_error(message))
    211 		return;
    212 
    213 	if (!l_dbus_message_get_arguments(message, "a{oaa{sv}}", &iter)) {
    214 		l_error("Failed to parse GetDiagnostics message");
    215 		return;
    216 	}
    217 
    218 	display_table_header("Available Networks (debug)",
    219 				"%s%-*s  %-*s  %-*s  %-*s  %-*s  %-*s  %-*s",
    220 				"", 2, "", 4, "SSID", 17, "BSSID", 4, "RSSI",
    221 				6, "Freq", 8, "Rank", 10, "MDE");
    222 
    223 	while (l_dbus_message_iter_next_entry(&iter, &key, &array)) {
    224 		struct network *network = l_new(struct network, 1);
    225 		const struct proxy_interface *net_i = network_get_proxy(key);
    226 
    227 		network->ssid = l_strdup(network_get_name(net_i));
    228 		network->bss_list = l_queue_new();
    229 
    230 		display_table_row(MARGIN MARGIN, 3, 32, network->ssid,
    231 					18, "", 6, "");
    232 
    233 		while (l_dbus_message_iter_next_entry(&array, &dict)) {
    234 			struct bss *bss = l_new(struct bss, 1);
    235 
    236 			while (l_dbus_message_iter_next_entry(&dict, &key,
    237 								&variant)) {
    238 				if (!strcmp(key, "Address"))
    239 					get_address(&variant, bss->addr);
    240 				else if (!strcmp(key, "Frequency"))
    241 					bss->frequency = get_u32(&variant);
    242 				else if (!strcmp(key, "RSSI"))
    243 					bss->rssi = get_i32(&variant);
    244 				else if (!strcmp(key, "Rank"))
    245 					bss->rank = get_u16(&variant);
    246 				else if (!strcmp(key, "MDE"))
    247 					get_byte_array(&variant, bss->mde, 3);
    248 			}
    249 
    250 			display_bss(bss);
    251 
    252 			l_queue_push_tail(network->bss_list, bss);
    253 		}
    254 
    255 		l_queue_push_tail(debug->network_list, network);
    256 	}
    257 
    258 	display_table_footer();
    259 }
    260 
    261 static enum cmd_status cmd_debug_get_networks(const char *device_name,
    262 						char **argv, int argc)
    263 {
    264 	const struct proxy_interface *debug_i;
    265 
    266 	debug_i = device_proxy_find(device_name, IWD_STATION_DEBUG_INTERFACE);
    267 	if (!debug_i) {
    268 		display_error("IWD not in developer mode");
    269 		return CMD_STATUS_INVALID_VALUE;
    270 	}
    271 
    272 	proxy_interface_method_call(debug_i, "GetNetworks", "",
    273 					get_networks_method_callback);
    274 
    275 	return CMD_STATUS_TRIGGERED;
    276 }
    277 
    278 static char *connect_debug_cmd_arg_completion(const char *text, int state,
    279 						const char *device_name)
    280 {
    281 	const struct proxy_interface *debug_i;
    282 	struct station_debug *debug;
    283 	static const struct l_queue_entry *n_entry;
    284 	static const struct l_queue_entry *b_entry;
    285 	size_t len = strlen(text);
    286 
    287 	debug_i = device_proxy_find(device_name, IWD_STATION_DEBUG_INTERFACE);
    288 	if (!debug_i)
    289 		return NULL;
    290 
    291 	debug = proxy_interface_get_data(debug_i);
    292 
    293 	if (!state)
    294 		n_entry = l_queue_get_entries(debug->network_list);
    295 
    296 	while (n_entry) {
    297 		struct network *network = n_entry->data;
    298 
    299 		n_entry = n_entry->next;
    300 
    301 		if (!b_entry)
    302 			b_entry = l_queue_get_entries(network->bss_list);
    303 
    304 		while (b_entry) {
    305 			struct bss *bss = b_entry->data;
    306 
    307 			b_entry = b_entry->next;
    308 
    309 			if (len > strlen(bss->addr))
    310 				return NULL;
    311 
    312 			if (strncmp(text, bss->addr, len))
    313 				continue;
    314 
    315 			return l_strdup_printf(MAC, MAC_STR(bss->addr));
    316 		}
    317 	}
    318 
    319 	return NULL;
    320 }
    321 
    322 static const char *get_autoconnect_tostr(const void *data)
    323 {
    324 	const struct station_debug *debug = data;
    325 
    326 	return debug->autoconnect ? "on" : "off";
    327 }
    328 
    329 static void update_autoconnect(void *data, struct l_dbus_message_iter *variant)
    330 {
    331 	struct station_debug *debug = data;
    332 	bool value;
    333 
    334 	if (!l_dbus_message_iter_get_variant(variant, "b", &value)) {
    335 		debug->autoconnect = false;
    336 
    337 		return;
    338 	}
    339 
    340 	debug->autoconnect = value;
    341 }
    342 
    343 static const struct proxy_interface_property debug_properties[] = {
    344 	{ "AutoConnect",  "b", update_autoconnect,  get_autoconnect_tostr, true,
    345 			properties_builder_append_on_off_variant,
    346 			properties_on_off_opts },
    347 	{ }
    348 };
    349 
    350 static enum cmd_status cmd_debug_set_autoconnect(const char *device_name,
    351 						char **argv, int argc)
    352 {
    353 	const struct proxy_interface *proxy = device_proxy_find(device_name,
    354 						IWD_STATION_DEBUG_INTERFACE);
    355 
    356 	if (!proxy) {
    357 		display_error("IWD not in developer mode");
    358 		return CMD_STATUS_INVALID_VALUE;
    359 	}
    360 
    361 	if (argc != 1)
    362 		return CMD_STATUS_INVALID_ARGS;
    363 
    364 	if (!proxy_property_set(proxy, "AutoConnect", argv[0],
    365 						check_errors_method_callback))
    366 		return CMD_STATUS_INVALID_VALUE;
    367 
    368 	return CMD_STATUS_TRIGGERED;
    369 }
    370 
    371 static const struct command station_debug_commands[] = {
    372 	{ "<wlan>", "connect", "<bssid>", cmd_debug_connect,
    373 					"Connect to a specific BSS", false,
    374 					connect_debug_cmd_arg_completion },
    375 	{ "<wlan>", "roam", "<bssid>", cmd_debug_roam,
    376 					"Roam to a BSS", false },
    377 	{ "<wlan>", "get-networks", NULL, cmd_debug_get_networks,
    378 					"Get networks", true },
    379 	{ "<wlan>", "autoconnect", "on|off", cmd_debug_set_autoconnect,
    380 					"Set AutoConnect property", false },
    381 	{ }
    382 };
    383 
    384 static char *family_debug_arg_completion(const char *text, int state)
    385 {
    386 	return device_arg_completion(text, state, station_debug_commands,
    387 						IWD_STATION_DEBUG_INTERFACE);
    388 }
    389 
    390 static char *entity_debug_arg_completion(const char *text, int state)
    391 {
    392 	return command_entity_arg_completion(text, state,
    393 						station_debug_commands);
    394 }
    395 
    396 static struct command_family station_debug_command_family = {
    397 	.caption = "Station Debug",
    398 	.name = "debug",
    399 	.command_list = station_debug_commands,
    400 	.family_arg_completion = family_debug_arg_completion,
    401 	.entity_arg_completion = entity_debug_arg_completion,
    402 };
    403 
    404 static int station_debug_command_family_init(void)
    405 {
    406 	command_family_register(&station_debug_command_family);
    407 
    408 	return 0;
    409 }
    410 
    411 static void station_debug_command_family_exit(void)
    412 {
    413 	command_family_unregister(&station_debug_command_family);
    414 }
    415 
    416 COMMAND_FAMILY(station_debug_command_family, station_debug_command_family_init,
    417 					station_debug_command_family_exit)
    418 
    419 static const struct proxy_interface_type_ops station_debug_ops = {
    420 	.create = station_debug_create,
    421 	.destroy = station_debug_destroy,
    422 };
    423 
    424 static struct proxy_interface_type station_debug_interface_type = {
    425 	.interface = IWD_STATION_DEBUG_INTERFACE,
    426 	.ops = &station_debug_ops,
    427 	.properties = debug_properties,
    428 };
    429 
    430 static int station_debug_interface_init(void)
    431 {
    432 	proxy_interface_type_register(&station_debug_interface_type);
    433 
    434 	return 0;
    435 }
    436 
    437 static void station_debug_interface_exit(void)
    438 {
    439 	proxy_interface_type_unregister(&station_debug_interface_type);
    440 }
    441 
    442 INTERFACE_TYPE(station_debug_interface_type, station_debug_interface_init,
    443 					station_debug_interface_exit)