eiwd

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

device.c (10189B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2017-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #include <ell/ell.h>
     28 
     29 #include "client/command.h"
     30 #include "client/dbus-proxy.h"
     31 #include "client/device.h"
     32 #include "client/display.h"
     33 #include "client/network.h"
     34 #include "client/properties.h"
     35 
     36 struct device {
     37 	bool powered;
     38 	bool wds;
     39 	char *address;
     40 	char *name;
     41 	char *mode;
     42 	const struct proxy_interface *adapter;
     43 };
     44 
     45 static void display_device(const struct proxy_interface *proxy)
     46 {
     47 	const struct device *device = proxy_interface_get_data(proxy);
     48 	char *caption = l_strdup_printf("%s: %s", "Device", device->name);
     49 
     50 	proxy_properties_display(proxy, caption, MARGIN, 20, 47);
     51 
     52 	l_free(caption);
     53 
     54 	if (device->adapter) {
     55 		display_table_row(MARGIN, 3, 8, "", 20, "Adapter", 47,
     56 				proxy_interface_get_identity_str(
     57 						device->adapter) ? : "");
     58 
     59 	}
     60 
     61 	display_table_footer();
     62 }
     63 
     64 static const char *get_name(const void *data)
     65 {
     66 	const struct device *device = data;
     67 
     68 	return device->name;
     69 }
     70 
     71 static void update_name(void *data, struct l_dbus_message_iter *variant)
     72 {
     73 	struct device *device = data;
     74 	const char *value;
     75 
     76 	l_free(device->name);
     77 
     78 	if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
     79 		device->name = NULL;
     80 
     81 		return;
     82 	}
     83 
     84 	device->name = l_strdup(value);
     85 }
     86 
     87 static const char *device_mode_opts[] = { "ad-hoc", "ap", "station", NULL };
     88 
     89 static const char *get_mode(const void *data)
     90 {
     91 	const struct device *device = data;
     92 
     93 	return device->mode;
     94 }
     95 
     96 static void update_mode(void *data, struct l_dbus_message_iter *variant)
     97 {
     98 	struct device *device = data;
     99 	const char *value;
    100 
    101 	l_free(device->mode);
    102 
    103 	if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
    104 		device->mode = NULL;
    105 
    106 		return;
    107 	}
    108 
    109 	device->mode = l_strdup(value);
    110 }
    111 
    112 static bool builder_append_string_variant(
    113 					struct l_dbus_message_builder *builder,
    114 					const char *value_str)
    115 {
    116 	return l_dbus_message_builder_append_basic(builder, 's', value_str);
    117 }
    118 
    119 static const char *get_address(const void *data)
    120 {
    121 	const struct device *device = data;
    122 
    123 	return device->address;
    124 }
    125 
    126 static void update_address(void *data, struct l_dbus_message_iter *variant)
    127 {
    128 	struct device *device = data;
    129 	const char *value;
    130 
    131 	l_free(device->address);
    132 
    133 	if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
    134 		device->address = NULL;
    135 
    136 		return;
    137 	}
    138 
    139 	device->address = l_strdup(value);
    140 }
    141 
    142 static const char *get_powered_tostr(const void *data)
    143 {
    144 	const struct device *device = data;
    145 
    146 	return device->powered ? "on" : "off";
    147 }
    148 
    149 static void update_powered(void *data, struct l_dbus_message_iter *variant)
    150 {
    151 	struct device *device = data;
    152 	bool value;
    153 
    154 	if (!l_dbus_message_iter_get_variant(variant, "b", &value)) {
    155 		device->powered = false;
    156 
    157 		return;
    158 	}
    159 
    160 	device->powered = value;
    161 }
    162 
    163 static void update_adapter(void *data, struct l_dbus_message_iter *variant)
    164 {
    165 	struct device *device = data;
    166 	const char *path;
    167 
    168 	if (!l_dbus_message_iter_get_variant(variant, "o", &path)) {
    169 		device->adapter = NULL;
    170 
    171 		return;
    172 	}
    173 
    174 	device->adapter = proxy_interface_find(IWD_ADAPTER_INTERFACE, path);
    175 }
    176 
    177 static const struct proxy_interface_property device_properties[] = {
    178 	{ "Name",     "s", update_name,     get_name },
    179 	{ "Mode",     "s", update_mode,     get_mode,          true,
    180 		builder_append_string_variant, device_mode_opts },
    181 	{ "Powered",  "b", update_powered,  get_powered_tostr, true,
    182 		properties_builder_append_on_off_variant,
    183 		properties_on_off_opts },
    184 	{ "Adapter",  "o", update_adapter },
    185 	{ "Address",  "s", update_address,  get_address },
    186 	{ }
    187 };
    188 
    189 static void *device_create(void)
    190 {
    191 	return l_new(struct device, 1);
    192 }
    193 
    194 static void device_destroy(void *data)
    195 {
    196 	struct device *device = data;
    197 
    198 	l_free(device->address);
    199 	l_free(device->name);
    200 	l_free(device->mode);
    201 
    202 	device->adapter = NULL;
    203 
    204 	l_free(device);
    205 }
    206 
    207 static void display_device_inline(const char *margin, const void *data)
    208 {
    209 	const struct device *device = data;
    210 	const char *adapter_str;
    211 
    212 	if (device->adapter &&
    213 			proxy_interface_get_identity_str(device->adapter))
    214 		adapter_str = proxy_interface_get_identity_str(device->adapter);
    215 	else
    216 		adapter_str = "-";
    217 
    218 	display_table_row(margin, 5, 20, device->name ? : "",
    219 				20, device->address ? : "",
    220 				10, get_powered_tostr(device),
    221 				10, adapter_str,
    222 				10, device->mode);
    223 }
    224 
    225 static const char *device_identity(void *data)
    226 {
    227 	const struct device *device = data;
    228 
    229 	return device->name;
    230 }
    231 
    232 static const struct proxy_interface_type_ops device_ops = {
    233 	.create = device_create,
    234 	.destroy = device_destroy,
    235 	.identity = device_identity,
    236 	.display = display_device_inline,
    237 };
    238 
    239 static struct proxy_interface_type device_interface_type = {
    240 	.interface = IWD_DEVICE_INTERFACE,
    241 	.properties = device_properties,
    242 	.ops = &device_ops,
    243 };
    244 
    245 static bool match_by_name(const void *a, const void *b)
    246 {
    247 	const struct device *device = a;
    248 	const char *name = b;
    249 
    250 	return !strcmp(device->name, name);
    251 }
    252 
    253 static bool match_by_partial_name(const void *a, const void *b)
    254 {
    255 	const struct device *device = a;
    256 	const char *text = b;
    257 
    258 	return !strncmp(device->name, text, strlen(text));
    259 }
    260 
    261 const struct proxy_interface *device_proxy_find_by_name(const char *name)
    262 {
    263 	struct l_queue *match;
    264 	struct proxy_interface *proxy = NULL;
    265 
    266 	if (!name)
    267 		return NULL;
    268 
    269 	match = proxy_interface_find_all(device_interface_type.interface,
    270 						match_by_name, name);
    271 
    272 	if (l_queue_length(match))
    273 		proxy = l_queue_pop_head(match);
    274 	else
    275 		display("Device %s not found.\n", name);
    276 
    277 	l_queue_destroy(match, NULL);
    278 
    279 	return proxy;
    280 }
    281 
    282 const struct proxy_interface *device_proxy_find(const char *device_name,
    283 							const char *interface)
    284 {
    285 	const struct proxy_interface *device_i =
    286 					device_proxy_find_by_name(device_name);
    287 	const struct proxy_interface *proxy;
    288 
    289 	if (!device_i)
    290 		return NULL;
    291 
    292 	proxy = proxy_interface_find(interface,
    293 					proxy_interface_get_path(device_i));
    294 	if (!proxy)
    295 		return NULL;
    296 
    297 	return proxy;
    298 }
    299 
    300 static enum cmd_status cmd_show(const char *device_name,
    301 						char **argv, int argc)
    302 {
    303 	const struct proxy_interface *proxy =
    304 					device_proxy_find_by_name(device_name);
    305 
    306 	if (!proxy)
    307 		return CMD_STATUS_INVALID_ARGS;
    308 
    309 	display_device(proxy);
    310 
    311 	return CMD_STATUS_DONE;
    312 }
    313 
    314 static void check_errors_method_callback(struct l_dbus_message *message,
    315 								void *user_data)
    316 {
    317 	dbus_message_has_error(message);
    318 }
    319 
    320 static enum cmd_status cmd_list(const char *device_name,
    321 						char **argv, int argc)
    322 {
    323 	display_table_header("Devices", MARGIN "%-*s  %-*s  %-*s  %-*s  %-*s",
    324 				20, "Name", 20, "Address", 10, "Powered",
    325 				10, "Adapter", 10, "Mode");
    326 
    327 	proxy_interface_display_list(device_interface_type.interface);
    328 
    329 	display_table_footer();
    330 
    331 	return CMD_STATUS_DONE;
    332 }
    333 
    334 static enum cmd_status cmd_set_property(const char *device_name,
    335 						char **argv, int argc)
    336 {
    337 	const struct proxy_interface *proxy =
    338 					device_proxy_find_by_name(device_name);
    339 
    340 	if (!proxy)
    341 		return CMD_STATUS_INVALID_VALUE;
    342 
    343 	if (argc != 2)
    344 		return CMD_STATUS_INVALID_ARGS;
    345 
    346 	if (!proxy_property_set(proxy, argv[0], argv[1],
    347 						check_errors_method_callback))
    348 		return CMD_STATUS_INVALID_VALUE;
    349 
    350 	return CMD_STATUS_TRIGGERED;
    351 }
    352 
    353 static char *set_property_cmd_arg_completion(const char *text, int state,
    354 						const char *device_name)
    355 {
    356 	return proxy_property_completion(device_properties, text, state);
    357 }
    358 
    359 static const struct command device_commands[] = {
    360 	{ NULL,     "list",     NULL,   cmd_list, "List devices",     true },
    361 	{ "<wlan>", "show",     NULL,   cmd_show, "Show device info", true },
    362 	{ "<wlan>", "set-property",
    363 				"<name> <value>",
    364 					cmd_set_property,
    365 						"Set property",       false,
    366 		set_property_cmd_arg_completion },
    367 	{ }
    368 };
    369 
    370 char *device_arg_completion(const char *text, int state,
    371 				const struct command *commands,
    372 				const char *extra_interface)
    373 {
    374 	static bool first_pass;
    375 	static size_t index;
    376 	static size_t len;
    377 	const char *cmd;
    378 
    379 	if (!state) {
    380 		index = 0;
    381 		len = strlen(text);
    382 		first_pass = true;
    383 	}
    384 
    385 	while ((cmd = commands[index].cmd)) {
    386 		if (commands[index++].entity)
    387 			continue;
    388 
    389 		if (!strncmp(cmd, text, len))
    390 			return l_strdup(cmd);
    391 	}
    392 
    393 	if (first_pass) {
    394 		state = 0;
    395 		first_pass = false;
    396 	}
    397 
    398 	return proxy_property_str_completion(&device_interface_type,
    399 						match_by_partial_name, "Name",
    400 						text, state, extra_interface);
    401 }
    402 
    403 static char *family_arg_completion(const char *text, int state)
    404 {
    405 	return device_arg_completion(text, state, device_commands, NULL);
    406 }
    407 
    408 static char *entity_arg_completion(const char *text, int state)
    409 {
    410 	return command_entity_arg_completion(text, state, device_commands);
    411 }
    412 
    413 static struct command_family device_command_family = {
    414 	.caption = "Devices",
    415 	.name = "device",
    416 	.command_list = device_commands,
    417 	.family_arg_completion = family_arg_completion,
    418 	.entity_arg_completion = entity_arg_completion,
    419 };
    420 
    421 static int device_command_family_init(void)
    422 {
    423 	command_family_register(&device_command_family);
    424 
    425 	return 0;
    426 }
    427 
    428 static void device_command_family_exit(void)
    429 {
    430 	command_family_unregister(&device_command_family);
    431 }
    432 
    433 COMMAND_FAMILY(device_command_family, device_command_family_init,
    434 						device_command_family_exit)
    435 
    436 static int device_interface_init(void)
    437 {
    438 	proxy_interface_type_register(&device_interface_type);
    439 
    440 	return 0;
    441 }
    442 
    443 static void device_interface_exit(void)
    444 {
    445 	proxy_interface_type_unregister(&device_interface_type);
    446 }
    447 
    448 INTERFACE_TYPE(device_interface_type, device_interface_init,
    449 						device_interface_exit)