ad-hoc.c (6858B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2018-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 34 struct ad_hoc { 35 bool started; 36 }; 37 38 static void *ad_hoc_create(void) 39 { 40 return l_new(struct ad_hoc, 1); 41 } 42 43 static void ad_hoc_destroy(void *data) 44 { 45 struct ad_hoc *ad_hoc = data; 46 47 l_free(ad_hoc); 48 } 49 50 static const struct proxy_interface_type_ops ad_hoc_ops = { 51 .create = ad_hoc_create, 52 .destroy = ad_hoc_destroy, 53 }; 54 55 static const char *get_started_tostr(const void *data) 56 { 57 const struct ad_hoc *ad_hoc = data; 58 59 return ad_hoc->started ? "yes" : "no"; 60 } 61 62 static void update_started(void *data, struct l_dbus_message_iter *variant) 63 { 64 struct ad_hoc *ad_hoc = data; 65 bool value; 66 67 if (!l_dbus_message_iter_get_variant(variant, "b", &value)) { 68 ad_hoc->started = false; 69 70 return; 71 } 72 73 ad_hoc->started = value; 74 } 75 76 static const struct proxy_interface_property ad_hoc_properties[] = { 77 { "Started", "b", update_started, get_started_tostr }, 78 { } 79 }; 80 81 static struct proxy_interface_type ad_hoc_interface_type = { 82 .interface = IWD_AD_HOC_INTERFACE, 83 .properties = ad_hoc_properties, 84 .ops = &ad_hoc_ops, 85 }; 86 87 static void check_errors_method_callback(struct l_dbus_message *message, 88 void *user_data) 89 { 90 dbus_message_has_error(message); 91 } 92 93 static void display_ad_hoc_inline(const char *margin, const void *data) 94 { 95 const struct proxy_interface *ad_hoc_i = data; 96 const struct ad_hoc *ad_hoc = proxy_interface_get_data(ad_hoc_i); 97 struct proxy_interface *device_i = 98 proxy_interface_find(IWD_DEVICE_INTERFACE, 99 proxy_interface_get_path(ad_hoc_i)); 100 const char *identity; 101 102 if (!device_i) 103 return; 104 105 identity = proxy_interface_get_identity_str(device_i); 106 if (!identity) 107 return; 108 109 display("%s%-*s%-*s\n", margin, 110 20, identity, 111 8, get_started_tostr(ad_hoc)); 112 } 113 114 static enum cmd_status cmd_list(const char *device_name, char **argv, int argc) 115 { 116 const struct l_queue_entry *entry; 117 struct l_queue *match = 118 proxy_interface_find_all(IWD_AD_HOC_INTERFACE, NULL, NULL); 119 120 display_table_header("Devices in Ad-Hoc Mode", MARGIN "%-*s %-*s", 121 20, "Name", 8, "Started"); 122 123 if (!match) { 124 display("No devices in Ad-Hoc mode available.\n"); 125 display_table_footer(); 126 127 return CMD_STATUS_DONE; 128 } 129 130 for (entry = l_queue_get_entries(match); entry; entry = entry->next) { 131 const struct proxy_interface *ad_hoc = entry->data; 132 display_ad_hoc_inline(MARGIN, ad_hoc); 133 } 134 135 display_table_footer(); 136 137 l_queue_destroy(match, NULL); 138 139 return CMD_STATUS_DONE; 140 } 141 142 static enum cmd_status cmd_start(const char *device_name, char **argv, int argc) 143 { 144 const struct proxy_interface *adhoc_i; 145 146 if (argc < 2) 147 return CMD_STATUS_INVALID_ARGS; 148 149 if (strlen(argv[0]) > 32) { 150 display("Network name cannot exceed 32 characters.\n"); 151 return CMD_STATUS_INVALID_VALUE; 152 } 153 154 if (strlen(argv[1]) < 8) { 155 display("Passphrase cannot be shorted than 8 characters.\n"); 156 return CMD_STATUS_INVALID_VALUE; 157 } 158 159 adhoc_i = device_proxy_find(device_name, IWD_AD_HOC_INTERFACE); 160 if (!adhoc_i) { 161 display("No ad-hoc on device: '%s'\n", device_name); 162 return CMD_STATUS_INVALID_VALUE; 163 } 164 165 proxy_interface_method_call(adhoc_i, "Start", "ss", 166 check_errors_method_callback, 167 argv[0], argv[1]); 168 169 return CMD_STATUS_TRIGGERED; 170 } 171 172 static enum cmd_status cmd_start_open(const char *device_name, 173 char **argv, int argc) 174 { 175 const struct proxy_interface *adhoc_i; 176 177 if (argc < 1) 178 return CMD_STATUS_INVALID_ARGS; 179 180 if (strlen(argv[0]) > 32) { 181 display("Network name cannot exceed 32 characters.\n"); 182 183 return CMD_STATUS_INVALID_VALUE; 184 } 185 186 adhoc_i = device_proxy_find(device_name, IWD_AD_HOC_INTERFACE); 187 if (!adhoc_i) { 188 display("No ad-hoc on device: '%s'\n", device_name); 189 return CMD_STATUS_INVALID_VALUE; 190 } 191 192 proxy_interface_method_call(adhoc_i, "StartOpen", "s", 193 check_errors_method_callback, 194 argv[0]); 195 196 return CMD_STATUS_TRIGGERED; 197 } 198 199 static enum cmd_status cmd_stop(const char *device_name, char **argv, int argc) 200 { 201 const struct proxy_interface *adhoc_i = 202 device_proxy_find(device_name, IWD_AD_HOC_INTERFACE); 203 204 if (!adhoc_i) { 205 display("No ad-hoc on device: '%s'\n", device_name); 206 return CMD_STATUS_INVALID_VALUE; 207 } 208 209 proxy_interface_method_call(adhoc_i, "Stop", "", 210 check_errors_method_callback); 211 212 return CMD_STATUS_TRIGGERED; 213 } 214 215 static const struct command ad_hoc_commands[] = { 216 { NULL, "list", NULL, cmd_list, "List devices in Ad-hoc mode", true }, 217 { "<wlan>", "start", "<\"network name\"> <passphrase>", cmd_start, 218 "Start or join an existing Ad-Hoc network called " 219 "\"network name\" with a passphrase" }, 220 { "<wlan>", "start_open", "<\"network name\">", cmd_start_open, 221 "Start or join an existing open Ad-Hoc network called" 222 " \"network name\"" }, 223 { "<wlan>", "stop", NULL, cmd_stop, "Leave an Ad-Hoc network" }, 224 { } 225 }; 226 227 static char *family_arg_completion(const char *text, int state) 228 { 229 return device_arg_completion(text, state, ad_hoc_commands, 230 IWD_AD_HOC_INTERFACE); 231 } 232 233 static char *entity_arg_completion(const char *text, int state) 234 { 235 return command_entity_arg_completion(text, state, ad_hoc_commands); 236 } 237 238 static struct command_family ad_hoc_command_family = { 239 .caption = "Ad-Hoc", 240 .name = "ad-hoc", 241 .command_list = ad_hoc_commands, 242 .family_arg_completion = family_arg_completion, 243 .entity_arg_completion = entity_arg_completion, 244 }; 245 246 static int ad_hoc_command_family_init(void) 247 { 248 command_family_register(&ad_hoc_command_family); 249 250 return 0; 251 } 252 253 static void ad_hoc_command_family_exit(void) 254 { 255 command_family_unregister(&ad_hoc_command_family); 256 } 257 258 COMMAND_FAMILY(ap_command_family, ad_hoc_command_family_init, 259 ad_hoc_command_family_exit) 260 261 static int ad_hoc_interface_init(void) 262 { 263 proxy_interface_type_register(&ad_hoc_interface_type); 264 265 return 0; 266 } 267 268 static void ad_hoc_interface_exit(void) 269 { 270 proxy_interface_type_unregister(&ad_hoc_interface_type); 271 } 272 273 INTERFACE_TYPE(ap_interface_type, ad_hoc_interface_init, ad_hoc_interface_exit)