adapter.c (9357B)
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/display.h" 32 #include "client/properties.h" 33 34 struct adapter { 35 bool powered; 36 char *model; 37 char *name; 38 char *vendor; 39 char *supported_modes_str; 40 }; 41 42 static const char *get_name(const void *data) 43 { 44 const struct adapter *adapter = data; 45 46 return adapter->name; 47 } 48 49 static void update_name(void *data, struct l_dbus_message_iter *variant) 50 { 51 struct adapter *adapter = data; 52 const char *value; 53 54 l_free(adapter->name); 55 56 if (!l_dbus_message_iter_get_variant(variant, "s", &value)) { 57 adapter->name = NULL; 58 59 return; 60 } 61 62 adapter->name = l_strdup(value); 63 } 64 65 static void update_vendor(void *data, struct l_dbus_message_iter *variant) 66 { 67 struct adapter *adapter = data; 68 const char *value; 69 70 l_free(adapter->vendor); 71 72 if (!l_dbus_message_iter_get_variant(variant, "s", &value)) { 73 adapter->vendor = NULL; 74 75 return; 76 } 77 78 adapter->vendor = l_strdup(value); 79 } 80 81 static const char *get_vendor(const void *data) 82 { 83 const struct adapter *adapter = data; 84 85 return adapter->vendor; 86 } 87 88 static void update_model(void *data, struct l_dbus_message_iter *variant) 89 { 90 struct adapter *adapter = data; 91 const char *value; 92 93 l_free(adapter->model); 94 95 if (!l_dbus_message_iter_get_variant(variant, "s", &value)) { 96 adapter->model = NULL; 97 98 return; 99 } 100 101 adapter->model = l_strdup(value); 102 } 103 104 static const char *get_model(const void *data) 105 { 106 const struct adapter *adapter = data; 107 108 return adapter->model; 109 } 110 111 static const char *get_powered_tostr(const void *data) 112 { 113 const struct adapter *adapter = data; 114 115 return adapter->powered ? "on" : "off"; 116 } 117 118 static void update_powered(void *data, struct l_dbus_message_iter *variant) 119 { 120 struct adapter *adapter = data; 121 bool value; 122 123 if (!l_dbus_message_iter_get_variant(variant, "b", &value)) { 124 adapter->powered = false; 125 126 return; 127 } 128 129 adapter->powered = value; 130 } 131 132 static void update_supported_modes(void *data, 133 struct l_dbus_message_iter *variant) 134 { 135 struct adapter *adapter = data; 136 struct l_string *buf; 137 struct l_dbus_message_iter iter; 138 const char *mode; 139 140 if (!l_dbus_message_iter_get_variant(variant, "as", &iter)) 141 return; 142 143 l_free(adapter->supported_modes_str); 144 145 buf = l_string_new(128); 146 147 while (l_dbus_message_iter_next_entry(&iter, &mode)) 148 l_string_append_printf(buf, "%s ", mode); 149 150 adapter->supported_modes_str = l_string_unwrap(buf); 151 } 152 153 static const char *get_supported_modes_tostr(const void *data) 154 { 155 const struct adapter *adapter = data; 156 157 return adapter->supported_modes_str; 158 } 159 160 static const struct proxy_interface_property adapter_properties[] = { 161 { "Name", "s", update_name, get_name }, 162 { "Powered", "b", update_powered, get_powered_tostr, true, 163 properties_builder_append_on_off_variant, 164 properties_on_off_opts }, 165 { "Vendor", "s", update_vendor, get_vendor }, 166 { "Model", "s", update_model, get_model }, 167 { "SupportedModes", "as", update_supported_modes, 168 get_supported_modes_tostr }, 169 { } 170 }; 171 172 static void display_adapter(const struct proxy_interface *proxy) 173 { 174 const struct adapter *adapter = proxy_interface_get_data(proxy); 175 char *caption = l_strdup_printf("%s: %s", "Adapter", adapter->name); 176 177 proxy_properties_display(proxy, caption, MARGIN, 17, 50); 178 179 l_free(caption); 180 181 display_table_footer(); 182 } 183 184 static void display_adapter_inline(const char *margin, const void *data) 185 { 186 const struct adapter *adapter = data; 187 188 display_table_row(margin, 4, 8, adapter->name ? : "-", 189 8, get_powered_tostr(adapter), 190 20, adapter->vendor ? : "-", 191 20, adapter->model ? : "-"); 192 } 193 194 static void *adapter_create(void) 195 { 196 return l_new(struct adapter, 1); 197 } 198 199 static void adapter_destroy(void *data) 200 { 201 struct adapter *adapter = data; 202 203 l_free(adapter->model); 204 l_free(adapter->vendor); 205 l_free(adapter->name); 206 l_free(adapter->supported_modes_str); 207 208 l_free(adapter); 209 } 210 211 static const char *adapter_identity(void *data) 212 { 213 const struct adapter *adapter = data; 214 215 return adapter->name; 216 } 217 218 static const struct proxy_interface_type_ops adapter_ops = { 219 .create = adapter_create, 220 .destroy = adapter_destroy, 221 .display = display_adapter_inline, 222 .identity = adapter_identity, 223 }; 224 225 static struct proxy_interface_type adapter_interface_type = { 226 .interface = IWD_ADAPTER_INTERFACE, 227 .properties = adapter_properties, 228 .ops = &adapter_ops, 229 }; 230 231 static bool match_by_name(const void *a, const void *b) 232 { 233 const struct adapter *adapter = a; 234 const char *name = b; 235 236 return !strcmp(adapter->name, name); 237 } 238 239 static bool match_by_partial_name(const void *a, const void *b) 240 { 241 const struct adapter *adapter = a; 242 const char *text = b; 243 244 return !strncmp(adapter->name, text, strlen(text)); 245 } 246 247 static const struct proxy_interface *get_adapter_proxy_by_name( 248 const char *adapter_name) 249 { 250 struct l_queue *match; 251 struct proxy_interface *proxy = NULL; 252 253 if (!adapter_name) 254 return NULL; 255 256 match = proxy_interface_find_all(adapter_interface_type.interface, 257 match_by_name, adapter_name); 258 259 if (l_queue_length(match)) 260 proxy = l_queue_pop_head(match); 261 else 262 display("Adapter '%s' not found\n", adapter_name); 263 264 l_queue_destroy(match, NULL); 265 266 return proxy; 267 } 268 269 static enum cmd_status cmd_list(const char *adapter_name, 270 char **argv, int argc) 271 { 272 display_table_header("Adapters", MARGIN "%-*s %-*s %-*s %-*s", 273 8, "Name", 8, "Powered", 274 20, "Vendor", 20, "Model"); 275 276 proxy_interface_display_list(adapter_interface_type.interface); 277 278 display_table_footer(); 279 280 return CMD_STATUS_DONE; 281 } 282 283 static enum cmd_status cmd_show(const char *adapter_name, 284 char **argv, int argc) 285 { 286 const struct proxy_interface *proxy = 287 get_adapter_proxy_by_name(adapter_name); 288 289 if (!proxy) 290 return CMD_STATUS_INVALID_ARGS; 291 292 display_adapter(proxy); 293 294 return CMD_STATUS_DONE; 295 } 296 297 static void property_set_callback(struct l_dbus_message *message, 298 void *user_data) 299 { 300 dbus_message_has_error(message); 301 } 302 303 static enum cmd_status cmd_set_property(const char *adapter_name, 304 char **argv, int argc) 305 { 306 const struct proxy_interface *proxy = 307 get_adapter_proxy_by_name(adapter_name); 308 309 if (!proxy) 310 return CMD_STATUS_INVALID_VALUE; 311 312 if (argc != 2) 313 return CMD_STATUS_INVALID_ARGS; 314 315 if (!proxy_property_set(proxy, argv[0], argv[1], 316 property_set_callback)) 317 return CMD_STATUS_INVALID_VALUE; 318 319 return CMD_STATUS_TRIGGERED; 320 } 321 322 static char *set_property_cmd_arg_completion(const char *text, int state, 323 const char *phy) 324 { 325 return proxy_property_completion(adapter_properties, text, state); 326 } 327 328 static const struct command adapter_commands[] = { 329 { NULL, "list", NULL, cmd_list, "List adapters", 330 true }, 331 { "<phy>", "show", NULL, cmd_show, "Show adapter info", 332 true }, 333 { "<phy>", "set-property", "<name> <value>", 334 cmd_set_property, "Set property", 335 false, 336 set_property_cmd_arg_completion }, 337 { } 338 }; 339 340 static char *family_arg_completion(const char *text, int state) 341 { 342 static bool first_pass; 343 static size_t index; 344 static size_t len; 345 const char *cmd; 346 347 if (!state) { 348 index = 0; 349 len = strlen(text); 350 first_pass = true; 351 } 352 353 while ((cmd = adapter_commands[index].cmd)) { 354 if (adapter_commands[index++].entity) 355 continue; 356 357 if (strncmp(cmd, text, len)) 358 continue; 359 360 return l_strdup(cmd); 361 } 362 363 if (first_pass) { 364 state = 0; 365 first_pass = false; 366 } 367 368 return proxy_property_str_completion(&adapter_interface_type, 369 match_by_partial_name, "Name", 370 text, state, NULL); 371 } 372 373 static char *entity_arg_completion(const char *text, int state) 374 { 375 return command_entity_arg_completion(text, state, adapter_commands); 376 } 377 378 static struct command_family adapter_command_family = { 379 .caption = "Adapters", 380 .name = "adapter", 381 .family_arg_completion = family_arg_completion, 382 .entity_arg_completion = entity_arg_completion, 383 .command_list = adapter_commands, 384 }; 385 386 static int adapter_command_family_init(void) 387 { 388 command_family_register(&adapter_command_family); 389 390 return 0; 391 } 392 393 static void adapter_command_family_exit(void) 394 { 395 command_family_unregister(&adapter_command_family); 396 } 397 398 COMMAND_FAMILY(adapter_command_family, adapter_command_family_init, 399 adapter_command_family_exit) 400 401 static int adapter_interface_init(void) 402 { 403 proxy_interface_type_register(&adapter_interface_type); 404 405 return 0; 406 } 407 408 static void adapter_interface_exit(void) 409 { 410 proxy_interface_type_unregister(&adapter_interface_type); 411 } 412 413 INTERFACE_TYPE(adapter_interface_type, adapter_interface_init, 414 adapter_interface_exit)