1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
/*
*
* Wireless daemon for Linux
*
* Copyright (C) 2017-2019 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ell/ell.h>
#include "client/dbus-proxy.h"
#include "client/display.h"
#include "client/network.h"
struct network {
bool connected;
char *identity;
char *name;
char *type;
struct l_queue *bss_list;
const struct proxy_interface *device;
};
static void check_errors_method_callback(struct l_dbus_message *message,
void *user_data)
{
dbus_message_has_error(message);
}
const struct proxy_interface *network_get_proxy(const char *path)
{
return proxy_interface_find(IWD_NETWORK_INTERFACE, path);
}
bool network_is_connected(const struct proxy_interface *network_proxy)
{
const struct network *network = proxy_interface_get_data(network_proxy);
return network->connected;
}
const char *network_get_type(const struct proxy_interface *network_proxy)
{
const struct network *network = proxy_interface_get_data(network_proxy);
return network->type;
}
const char *network_get_name(const struct proxy_interface *network_proxy)
{
const struct network *network = proxy_interface_get_data(network_proxy);
return network->name;
}
void network_connect(const struct proxy_interface *proxy)
{
if (!proxy)
return;
proxy_interface_method_call(proxy, "Connect", "",
check_errors_method_callback);
}
static const char *get_name(const void *data)
{
const struct network *network = data;
return network->name;
}
static void update_name(void *data, struct l_dbus_message_iter *variant)
{
struct network *network = data;
const char *value;
l_free(network->name);
if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
network->name = NULL;
return;
}
network->name = l_strdup(value);
}
static void update_connected(void *data, struct l_dbus_message_iter *variant)
{
struct network *network = data;
bool value;
if (!l_dbus_message_iter_get_variant(variant, "b", &value)) {
network->connected = false;
return;
}
network->connected = value;
}
static void update_device(void *data, struct l_dbus_message_iter *variant)
{
struct network *network = data;
const char *path;
if (!l_dbus_message_iter_get_variant(variant, "o", &path)) {
network->device = NULL;
return;
}
network->device = proxy_interface_find(IWD_DEVICE_INTERFACE, path);
}
static void update_type(void *data, struct l_dbus_message_iter *variant)
{
struct network *network = data;
const char *value;
l_free(network->type);
if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
network->type = NULL;
return;
}
network->type = l_strdup(value);
}
static bool match_path(const void *a, const void *user_data)
{
const char *path1 = a;
const char *path2 = user_data;
return !strcmp(path1, path2);
}
static void update_ess(void *data, struct l_dbus_message_iter *variant)
{
struct network *network = data;
struct l_dbus_message_iter array;
const char *path;
if (!network->bss_list)
network->bss_list = l_queue_new();
if (!l_dbus_message_iter_get_variant(variant, "ao", &array))
return;
while (l_dbus_message_iter_next_entry(&array, &path)) {
l_free(l_queue_remove_if(network->bss_list, match_path, path));
l_queue_push_head(network->bss_list, l_strdup(path));
}
}
static const char *get_ess(const void *data)
{
const struct network *network = data;
static char count[10];
snprintf(count, 10, "Count %u", l_queue_length(network->bss_list));
return count;
}
struct l_queue *network_get_bss_list(
const struct proxy_interface *network_proxy)
{
const struct network *network = proxy_interface_get_data(network_proxy);
if (!network)
return NULL;
return network->bss_list;
}
static const struct proxy_interface_property network_properties[] = {
{ "Name", "s", update_name, get_name },
{ "Connected", "b", update_connected},
{ "Device", "o", update_device},
{ "Type", "s", update_type},
{ "ExtendedServiceSet", "ao", update_ess, get_ess },
{ }
};
static const char *network_identity(void *data)
{
struct network *network = data;
if (!network->identity)
network->identity =
l_strdup_printf("%s %s", network->name, network->type);
return network->identity;
}
static void network_display_inline(const char *margin, const void *data)
{
const struct network *network = data;
display("%s%s %s %s\n", margin, network->name ? network->name : "",
network->type ? network->type : "",
network->connected ? "connected" : "disconnected");
}
static void *network_create(void)
{
return l_new(struct network, 1);
}
static void network_destroy(void *data)
{
struct network *network = data;
l_free(network->name);
l_free(network->type);
l_free(network->identity);
l_queue_destroy(network->bss_list, l_free);
network->device = NULL;
l_free(network);
}
static const struct proxy_interface_type_ops ops = {
.create = network_create,
.destroy = network_destroy,
.display = network_display_inline,
.identity = network_identity,
};
static struct proxy_interface_type network_interface_type = {
.interface = IWD_NETWORK_INTERFACE,
.properties = network_properties,
.ops = &ops,
};
struct completion_search_parameters {
const char *text;
const struct proxy_interface *device;
};
static bool match_by_partial_name(const void *a, const void *b)
{
const struct network *network = a;
const struct completion_search_parameters *params = b;
const char *name;
const char *text;
if (!proxy_interface_is_same(network->device, params->device))
return false;
for (text = params->text, name = network->name; *text && *name;
name++, text++) {
if (*name == *text)
continue;
return false;
}
if (*text)
return false;
return true;
}
char *network_name_completion(const struct proxy_interface *device,
const char *text, int state)
{
const struct completion_search_parameters params = {
.text = text, .device = device,
};
return proxy_property_str_completion(&network_interface_type,
match_by_partial_name, "Name",
¶ms, state, NULL);
}
struct network_search_parameters {
const struct network_args *args;
const struct proxy_interface *device;
};
static bool match_by_device_and_args(const void *a, const void *b)
{
const struct network *network = a;
const struct network_search_parameters *params = b;
if (!proxy_interface_is_same(network->device, params->device))
return false;
if (strcmp(network->name, params->args->name))
return false;
if (params->args->type && strcmp(network->type, params->args->type))
return false;
return true;
}
struct l_queue *network_match_by_device_and_args(
const struct proxy_interface *device,
const struct network_args *args)
{
struct network_search_parameters params = {
.args = args, .device = device
};
return proxy_interface_find_all(network_interface_type.interface,
match_by_device_and_args, ¶ms);
}
static int network_interface_init(void)
{
proxy_interface_type_register(&network_interface_type);
return 0;
}
static void network_interface_exit(void)
{
proxy_interface_type_unregister(&network_interface_type);
}
INTERFACE_TYPE(network_interface_type, network_interface_init,
network_interface_exit)
|