wsc.c (6081B)
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 34 static struct proxy_interface_type wsc_interface_type = { 35 .interface = IWD_WSC_INTERFACE, 36 }; 37 38 static void check_errors_method_callback(struct l_dbus_message *message, 39 void *user_data) 40 { 41 dbus_message_has_error(message); 42 } 43 44 static void generate_pin_callback(struct l_dbus_message *message, 45 void *user_data) 46 { 47 const struct proxy_interface *proxy = user_data; 48 const char *pin; 49 50 if (dbus_message_has_error(message)) 51 return; 52 53 if (!l_dbus_message_get_arguments(message, "s", &pin)) { 54 l_error("Failed to parse 'generate pin' callback message"); 55 56 return; 57 } 58 59 if (!pin) 60 return; 61 62 proxy_interface_method_call(proxy, "StartPin", "s", 63 check_errors_method_callback, pin); 64 } 65 66 static void display_wsc_inline(const char *margin, const void *data) 67 { 68 const struct proxy_interface *wsc_i = data; 69 struct proxy_interface *device_i = 70 proxy_interface_find(IWD_DEVICE_INTERFACE, 71 proxy_interface_get_path(wsc_i)); 72 const char *identity; 73 74 if (!device_i) 75 return; 76 77 identity = proxy_interface_get_identity_str(device_i); 78 if (!identity) 79 return; 80 81 display("%s%-*s\n", margin, 20, identity); 82 } 83 84 static enum cmd_status cmd_list(const char *device_name, char **argv, int argc) 85 { 86 const struct l_queue_entry *entry; 87 struct l_queue *match = 88 proxy_interface_find_all(IWD_WSC_INTERFACE, NULL, NULL); 89 90 display_table_header("WSC-capable Devices", MARGIN "%-*s", 20, "Name"); 91 92 if (!match) { 93 display("No WSC-capable devices available\n"); 94 display_table_footer(); 95 96 return CMD_STATUS_DONE; 97 } 98 99 for (entry = l_queue_get_entries(match); entry; entry = entry->next) { 100 const struct proxy_interface *wsc = entry->data; 101 display_wsc_inline(MARGIN, wsc); 102 } 103 104 display_table_footer(); 105 106 l_queue_destroy(match, NULL); 107 108 return CMD_STATUS_DONE; 109 } 110 111 static enum cmd_status cmd_push_button(const char *device_name, 112 char **argv, int argc) 113 { 114 const struct proxy_interface *wsc_i = 115 device_proxy_find(device_name, IWD_WSC_INTERFACE); 116 117 if (!wsc_i) { 118 display("No wsc on device: '%s'\n", device_name); 119 return CMD_STATUS_INVALID_VALUE; 120 } 121 122 proxy_interface_method_call(wsc_i, "PushButton", "", 123 check_errors_method_callback); 124 125 return CMD_STATUS_TRIGGERED; 126 } 127 128 static enum cmd_status cmd_start_user_pin(const char *device_name, 129 char **argv, int argc) 130 { 131 const struct proxy_interface *wsc_i; 132 133 if (argc != 1) 134 return CMD_STATUS_INVALID_ARGS; 135 136 wsc_i = device_proxy_find(device_name, IWD_WSC_INTERFACE); 137 if (!wsc_i) { 138 display("No wsc on device: '%s'\n", device_name); 139 return CMD_STATUS_INVALID_VALUE; 140 } 141 142 proxy_interface_method_call(wsc_i, "StartPin", "s", 143 check_errors_method_callback, argv[0]); 144 145 return CMD_STATUS_TRIGGERED; 146 } 147 148 static enum cmd_status cmd_start_pin(const char *device_name, 149 char **argv, int argc) 150 { 151 const struct proxy_interface *wsc_i = 152 device_proxy_find(device_name, IWD_WSC_INTERFACE); 153 154 if (!wsc_i) { 155 display("No wsc on device: '%s'\n", device_name); 156 return CMD_STATUS_INVALID_VALUE; 157 } 158 159 proxy_interface_method_call(wsc_i, "GeneratePin", "", 160 generate_pin_callback); 161 162 return CMD_STATUS_TRIGGERED; 163 } 164 165 static enum cmd_status cmd_cancel(const char *device_name, 166 char **argv, int argc) 167 { 168 const struct proxy_interface *wsc_i = 169 device_proxy_find(device_name, IWD_WSC_INTERFACE); 170 171 if (!wsc_i) { 172 display("No wsc on device: '%s'\n", device_name); 173 return CMD_STATUS_INVALID_VALUE; 174 } 175 176 proxy_interface_method_call(wsc_i, "Cancel", "", 177 check_errors_method_callback); 178 179 return CMD_STATUS_TRIGGERED; 180 } 181 182 static const struct command wsc_commands[] = { 183 { NULL, "list", NULL, cmd_list, "List WSC-capable devices", true }, 184 { "<wlan>", "push-button", NULL, cmd_push_button, "PushButton mode" }, 185 { "<wlan>", "start-user-pin", "<PIN>", cmd_start_user_pin, 186 "PIN mode" }, 187 { "<wlan>", "start-pin", NULL, cmd_start_pin, 188 "PIN mode with generated 8 digit PIN" }, 189 { "<wlan>", "cancel", NULL, cmd_cancel, "Aborts WSC operations" }, 190 { } 191 }; 192 193 static char *family_arg_completion(const char *text, int state) 194 { 195 return device_arg_completion(text, state, wsc_commands, 196 IWD_WSC_INTERFACE); 197 } 198 199 static char *entity_arg_completion(const char *text, int state) 200 { 201 return command_entity_arg_completion(text, state, wsc_commands); 202 } 203 204 static struct command_family wsc_command_family = { 205 .caption = "WiFi Simple Configuration", 206 .name = "wsc", 207 .command_list = wsc_commands, 208 .family_arg_completion = family_arg_completion, 209 .entity_arg_completion = entity_arg_completion, 210 }; 211 212 static int wsc_command_family_init(void) 213 { 214 command_family_register(&wsc_command_family); 215 216 return 0; 217 } 218 219 static void wsc_command_family_exit(void) 220 { 221 command_family_unregister(&wsc_command_family); 222 } 223 224 COMMAND_FAMILY(wsc_command_family, wsc_command_family_init, 225 wsc_command_family_exit) 226 227 static int wsc_interface_init(void) 228 { 229 proxy_interface_type_register(&wsc_interface_type); 230 231 return 0; 232 } 233 234 static void wsc_interface_exit(void) 235 { 236 proxy_interface_type_unregister(&wsc_interface_type); 237 } 238 239 INTERFACE_TYPE(wsc_interface_type, wsc_interface_init, wsc_interface_exit)