gclue-wifi.c 27.1 KB
Newer Older
1 2
/* vim: set et ts=8 sw=8: */
/*
3
 * Copyright 2014 Red Hat, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Geoclue is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * Geoclue 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 General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with Geoclue; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 */

#include <stdlib.h>
#include <glib.h>
#include <string.h>
25
#include <config.h>
26
#include "gclue-wifi.h"
27
#include "gclue-config.h"
28
#include "gclue-error.h"
29
#include "gclue-mozilla.h"
30

31
#define WIFI_SCAN_TIMEOUT_HIGH_ACCURACY 10
32 33 34
/* Since this is only used for city-level accuracy, 5 minutes betweeen each
 * scan is more than enough.
 */
35
#define WIFI_SCAN_TIMEOUT_LOW_ACCURACY  300
36

37 38 39 40 41
/**
 * SECTION:gclue-wifi
 * @short_description: WiFi-based geolocation
 * @include: gclue-glib/gclue-wifi.h
 *
42
 * Contains functions to get the geolocation based on nearby WiFi networks.
43 44
 **/

45 46 47 48 49
static gboolean
gclue_wifi_start (GClueLocationSource *source);
static gboolean
gclue_wifi_stop (GClueLocationSource *source);

50
struct _GClueWifiPrivate {
51 52 53 54
        WPASupplicant *supplicant;
        WPAInterface *interface;
        GHashTable *bss_proxies;
        GHashTable *ignored_bss_proxies;
55
        gboolean bss_list_changed;
56 57 58

        gulong bss_added_id;
        gulong bss_removed_id;
59
        gulong scan_done_id;
60

61
        guint scan_timeout;
62 63 64 65 66 67 68 69 70

        GClueAccuracyLevel accuracy_level;
};

enum
{
        PROP_0,
        PROP_ACCURACY_LEVEL,
        LAST_PROP
71
};
72
static GParamSpec *gParamSpecs[LAST_PROP];
73

74 75 76
static SoupMessage *
gclue_wifi_create_query (GClueWebSource *source,
                         GError        **error);
77 78
static SoupMessage *
gclue_wifi_create_submit_query (GClueWebSource  *source,
Ankit's avatar
Ankit committed
79
                                GClueLocation   *location,
80
                                GError         **error);
Ankit's avatar
Ankit committed
81
static GClueLocation *
82 83 84
gclue_wifi_parse_response (GClueWebSource *source,
                           const char     *json,
                           GError        **error);
85 86 87
static GClueAccuracyLevel
gclue_wifi_get_available_accuracy_level (GClueWebSource *source,
                                         gboolean        net_available);
88

89 90 91 92
G_DEFINE_TYPE_WITH_CODE (GClueWifi,
                         gclue_wifi,
                         GCLUE_TYPE_WEB_SOURCE,
                         G_ADD_PRIVATE (GClueWifi))
93

94
static void
95
disconnect_bss_signals (GClueWifi *wifi);
96 97 98 99 100 101 102 103
static void
on_scan_call_done (GObject      *source_object,
                   GAsyncResult *res,
                   gpointer      user_data);
static void
on_scan_done (WPAInterface *object,
              gboolean      success,
              gpointer      user_data);
104 105 106 107 108 109

static void
gclue_wifi_finalize (GObject *gwifi)
{
        GClueWifi *wifi = (GClueWifi *) gwifi;

110 111
        G_OBJECT_CLASS (gclue_wifi_parent_class)->finalize (gwifi);

112 113 114 115 116
        disconnect_bss_signals (wifi);
        g_clear_object (&wifi->priv->supplicant);
        g_clear_object (&wifi->priv->interface);
        g_clear_pointer (&wifi->priv->bss_proxies, g_hash_table_unref);
        g_clear_pointer (&wifi->priv->ignored_bss_proxies, g_hash_table_unref);
117
}
118 119 120

static void
gclue_wifi_constructed (GObject *object);
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
static void
gclue_wifi_get_property (GObject    *object,
                         guint       prop_id,
                         GValue     *value,
                         GParamSpec *pspec)
{
        GClueWifi *wifi = GCLUE_WIFI (object);

        switch (prop_id) {
        case PROP_ACCURACY_LEVEL:
                g_value_set_enum (value, wifi->priv->accuracy_level);
                break;

        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
}

static void
gclue_wifi_set_property (GObject      *object,
                         guint         prop_id,
                         const GValue *value,
                         GParamSpec   *pspec)
{
        GClueWifi *wifi = GCLUE_WIFI (object);

        switch (prop_id) {
        case PROP_ACCURACY_LEVEL:
                wifi->priv->accuracy_level = g_value_get_enum (value);
                break;

        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
}

158 159 160
static void
gclue_wifi_class_init (GClueWifiClass *klass)
{
161 162
        GClueWebSourceClass *web_class = GCLUE_WEB_SOURCE_CLASS (klass);
        GClueLocationSourceClass *source_class = GCLUE_LOCATION_SOURCE_CLASS (klass);
163
        GObjectClass *gwifi_class = G_OBJECT_CLASS (klass);
164

165 166 167
        source_class->start = gclue_wifi_start;
        source_class->stop = gclue_wifi_stop;
        web_class->create_submit_query = gclue_wifi_create_submit_query;
168
        web_class->create_query = gclue_wifi_create_query;
169
        web_class->parse_response = gclue_wifi_parse_response;
170 171
        web_class->get_available_accuracy_level =
                gclue_wifi_get_available_accuracy_level;
172 173
        gwifi_class->get_property = gclue_wifi_get_property;
        gwifi_class->set_property = gclue_wifi_set_property;
174
        gwifi_class->finalize = gclue_wifi_finalize;
175
        gwifi_class->constructed = gclue_wifi_constructed;
176

177 178 179 180 181 182 183 184 185 186
        gParamSpecs[PROP_ACCURACY_LEVEL] = g_param_spec_enum ("accuracy-level",
                                                              "AccuracyLevel",
                                                              "Max accuracy level",
                                                              GCLUE_TYPE_ACCURACY_LEVEL,
                                                              GCLUE_ACCURACY_LEVEL_CITY,
                                                              G_PARAM_READWRITE |
                                                              G_PARAM_CONSTRUCT_ONLY);
        g_object_class_install_property (gwifi_class,
                                         PROP_ACCURACY_LEVEL,
                                         gParamSpecs[PROP_ACCURACY_LEVEL]);
187 188
}

189
static void
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
on_bss_added (WPAInterface *object,
              const gchar  *path,
              GVariant     *properties,
              gpointer      user_data);

static char *
variant_to_string (GVariant *variant, guint *len)
{
        guint n_bytes, i;
        char *ret;

        n_bytes = g_variant_n_children (variant);
        if (len != NULL)
                *len = n_bytes;
        if (n_bytes <= 0)
                return NULL;
        ret = g_malloc (n_bytes + 1);
        ret[n_bytes] = '\0';

        for (i = 0; i < n_bytes; i++)
                g_variant_get_child (variant,
                                     i,
                                     "y",
                                     &ret[i]);

        return ret;
}

static char *
get_ssid_from_bss (WPABSS *bss)
{
        GVariant *variant = wpa_bss_get_ssid (bss);

        return variant_to_string (variant, NULL);
}

static char *
get_bssid_from_bss (WPABSS *bss)
{
        GVariant *variant;
        char *raw_bssid;
        char *bssid;
        guint raw_len, len, i, j;

        variant = wpa_bss_get_bssid (bss);

        raw_bssid = variant_to_string (variant, &raw_len);
        len = raw_len * 2 + raw_len;
        bssid = g_malloc (len);
        for (i = 0, j = 0; i < len; i = i + 3, j++)
                g_snprintf (bssid + i,
                            4,
                           "%02x:",
                           (unsigned char) raw_bssid[j]);
        bssid[len - 1] = '\0';

        return bssid;
}
248

249 250 251 252 253 254 255
static void
add_bss_proxy (GClueWifi *wifi,
               WPABSS    *bss)
{
        const char *path;

        path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (bss));
Zeeshan Ali's avatar
Zeeshan Ali committed
256 257 258 259 260
        if (g_hash_table_replace (wifi->priv->bss_proxies,
                                  g_strdup (path),
                                  bss)) {
                char *ssid;

261
                wifi->priv->bss_list_changed = TRUE;
Zeeshan Ali's avatar
Zeeshan Ali committed
262 263 264 265
                ssid = get_ssid_from_bss (bss);
                g_debug ("WiFi AP '%s' added.", ssid);
                g_free (ssid);
        }
266 267
}

268
static void
269 270 271
on_bss_signal_notify (GObject    *gobject,
                      GParamSpec *pspec,
                      gpointer    user_data)
272 273
{
        GClueWifi *wifi = GCLUE_WIFI (user_data);
274 275
        WPABSS *bss = WPA_BSS (gobject);
        const char *path;
276

277 278 279
        if (wpa_bss_get_signal (bss) <= -90) {
                char *bssid = get_bssid_from_bss (bss);
                g_debug ("WiFi AP '%s' still has very low strength (%u dBm)"
280
                         ", ignoring again..",
281 282 283
                         bssid,
                         wpa_bss_get_signal (bss));
                g_free (bssid);
284 285 286
                return;
        }

287 288
        g_signal_handlers_disconnect_by_func (G_OBJECT (bss),
                                              on_bss_signal_notify,
289
                                              user_data);
290 291
        add_bss_proxy (wifi, g_object_ref (bss));
        path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (bss));
292
        g_hash_table_remove (wifi->priv->ignored_bss_proxies, path);
293 294
}

295
static void
296 297 298
on_bss_proxy_ready (GObject      *source_object,
                    GAsyncResult *res,
                    gpointer      user_data)
299 300
{
        GClueWifi *wifi = GCLUE_WIFI (user_data);
301 302 303 304 305 306 307 308
        WPABSS *bss;
        GError *error = NULL;
        char *ssid;

        bss = wpa_bss_proxy_new_for_bus_finish (res, &error);
        if (bss == NULL) {
                g_debug ("%s", error->message);
                g_error_free (error);
309 310

                return;
311 312
        }

313
        if (gclue_mozilla_should_ignore_bss (bss)) {
314
                g_object_unref (bss);
315 316

                return;
317 318 319 320 321
        }

        ssid = get_ssid_from_bss (bss);
        g_debug ("WiFi AP '%s' added.", ssid);
        g_free (ssid);
322

323
        if (wpa_bss_get_signal (bss) <= -90) {
324
                const char *path;
325 326
                char *bssid = get_bssid_from_bss (bss);
                g_debug ("WiFi AP '%s' has very low strength (%u dBm)"
327
                         ", ignoring for now..",
328 329 330 331 332 333
                         bssid,
                         wpa_bss_get_signal (bss));
                g_free (bssid);
                g_signal_connect (G_OBJECT (bss),
                                  "notify::signal",
                                  G_CALLBACK (on_bss_signal_notify),
334
                                  user_data);
335
                path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (bss));
336 337 338
                g_hash_table_replace (wifi->priv->ignored_bss_proxies,
                                      g_strdup (path),
                                      bss);
339 340 341
                return;
        }

342
        add_bss_proxy (wifi, bss);
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
}

static void
on_bss_added (WPAInterface *object,
              const gchar  *path,
              GVariant     *properties,
              gpointer      user_data)
{
        wpa_bss_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
                                   G_DBUS_PROXY_FLAGS_NONE,
                                   "fi.w1.wpa_supplicant1",
                                   path,
                                   NULL,
                                   on_bss_proxy_ready,
                                   user_data);
}

360
static gboolean
361 362 363 364 365 366 367
remove_bss_from_hashtable (const gchar *path, GHashTable *hash_table)
{
        char *ssid;
        WPABSS *bss = NULL;

        bss = g_hash_table_lookup (hash_table, path);
        if (bss == NULL)
368
                return FALSE;
369 370 371 372 373 374

        ssid = get_ssid_from_bss (bss);
        g_debug ("WiFi AP '%s' removed.", ssid);
        g_free (ssid);

        g_hash_table_remove (hash_table, path);
375 376

        return TRUE;
377 378
}

379 380 381 382 383 384 385
static void
on_bss_removed (WPAInterface *object,
                const gchar  *path,
                gpointer      user_data)
{
        GClueWifiPrivate *priv = GCLUE_WIFI (user_data)->priv;

386 387
        if (remove_bss_from_hashtable (path, priv->bss_proxies))
                priv->bss_list_changed = TRUE;
388
        remove_bss_from_hashtable (path, priv->ignored_bss_proxies);
389 390
}

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
static void
cancel_wifi_scan (GClueWifi *wifi)
{
        GClueWifiPrivate *priv = wifi->priv;

        if (priv->scan_timeout != 0) {
                g_source_remove (priv->scan_timeout);
                priv->scan_timeout = 0;
        }

        if (priv->scan_done_id != 0) {
                g_signal_handler_disconnect (priv->interface,
                                             priv->scan_done_id);
                priv->scan_done_id = 0;
        }
}

static gboolean
on_scan_timeout (gpointer user_data)
{
        GClueWifi *wifi = GCLUE_WIFI (user_data);
        GClueWifiPrivate *priv = wifi->priv;
        GVariantBuilder builder;
        GVariant *args;

        if (priv->interface == NULL)
                return FALSE;

        g_debug ("WiFi scan timeout. Restarting-scan..");
        priv->scan_timeout = 0;

        if (priv->scan_done_id == 0)
                priv->scan_done_id = g_signal_connect
                                        (priv->interface,
                                         "scan-done",
                                         G_CALLBACK (on_scan_done),
                                         wifi);

        g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
        g_variant_builder_add (&builder,
                               "{sv}",
                               "Type", g_variant_new ("s", "passive"));
        args = g_variant_builder_end (&builder);

        wpa_interface_call_scan (WPA_INTERFACE (priv->interface),
                                 args,
                                 NULL,
                                 on_scan_call_done,
                                 wifi);

        return FALSE;
}

static void
on_scan_done (WPAInterface *object,
              gboolean      success,
              gpointer      user_data)
{
        GClueWifi *wifi = GCLUE_WIFI (user_data);
        GClueWifiPrivate *priv = wifi->priv;
451
        guint timeout;
452 453 454 455 456 457 458 459 460 461 462

        if (!success) {
                g_warning ("WiFi scan failed");

                return;
        }
        g_debug ("WiFi scan completed");

        if (priv->interface == NULL)
                return;

463 464 465 466 467
        if (priv->bss_list_changed) {
                priv->bss_list_changed = FALSE;
                g_debug ("Refreshing location..");
                gclue_web_source_refresh (GCLUE_WEB_SOURCE (wifi));
        }
468

469 470 471 472
        /* With high-enough accuracy requests, we need to scan more often since
         * user's location can change quickly. With low accuracy, we don't since
         * we wouldn't want to drain power unnecessarily.
         */
473
        if (priv->accuracy_level >= GCLUE_ACCURACY_LEVEL_STREET)
474
                timeout = WIFI_SCAN_TIMEOUT_HIGH_ACCURACY;
475
        else
476 477 478 479 480
                timeout = WIFI_SCAN_TIMEOUT_LOW_ACCURACY;
        priv->scan_timeout = g_timeout_add_seconds (timeout,
                                                    on_scan_timeout,
                                                    wifi);
        g_debug ("Next scan scheduled in %u seconds", timeout);
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
}

static void
on_scan_call_done (GObject      *source_object,
                   GAsyncResult *res,
                   gpointer      user_data)
{
        GClueWifi *wifi = GCLUE_WIFI (user_data);
        GError *error = NULL;

        if (!wpa_interface_call_scan_finish
                (WPA_INTERFACE (source_object),
                 res,
                 &error)) {
                g_warning ("Scanning of WiFi networks failed: %s",
                           error->message);
                g_error_free (error);

                cancel_wifi_scan (wifi);

                return;
        }
}

505
static void
506
connect_bss_signals (GClueWifi *wifi)
507
{
508
        GClueWifiPrivate *priv = wifi->priv;
509 510
        const gchar *const *bss_list;
        guint i;
511

512
        if (priv->bss_added_id != 0)
513
                return;
514
        if (priv->interface == NULL) {
515
                gclue_web_source_refresh (GCLUE_WEB_SOURCE (wifi));
516 517 518

                return;
        }
519

520 521
        on_scan_timeout (wifi);

522
        priv->bss_list_changed = TRUE;
523
        priv->bss_added_id = g_signal_connect (priv->interface,
524 525 526
                                               "bss-added",
                                               G_CALLBACK (on_bss_added),
                                               wifi);
527 528 529 530 531 532
        priv->bss_removed_id = g_signal_connect (priv->interface,
                                                "bss-removed",
                                                G_CALLBACK (on_bss_removed),
                                                wifi);

        bss_list = wpa_interface_get_bsss (WPA_INTERFACE (priv->interface));
533 534 535
        if (bss_list == NULL)
                return;

536 537 538 539 540
        for (i = 0; bss_list[i] != NULL; i++)
                on_bss_added (WPA_INTERFACE (priv->interface),
                              bss_list[i],
                              NULL,
                              wifi);
541 542 543
}

static void
544
disconnect_bss_signals (GClueWifi *wifi)
545 546 547
{
        GClueWifiPrivate *priv = wifi->priv;

548
        if (priv->bss_added_id == 0 || priv->interface == NULL)
549 550
                return;

551
        cancel_wifi_scan (wifi);
552 553 554 555
        g_signal_handler_disconnect (priv->interface, priv->bss_added_id);
        priv->bss_added_id = 0;
        g_signal_handler_disconnect (priv->interface, priv->bss_removed_id);
        priv->bss_removed_id = 0;
556

557 558
        g_hash_table_remove_all (priv->bss_proxies);
        g_hash_table_remove_all (priv->ignored_bss_proxies);
559 560
}

561 562 563 564 565 566 567 568 569 570 571
static gboolean
gclue_wifi_start (GClueLocationSource *source)
{
        GClueLocationSourceClass *base_class;

        g_return_val_if_fail (GCLUE_IS_WIFI (source), FALSE);

        base_class = GCLUE_LOCATION_SOURCE_CLASS (gclue_wifi_parent_class);
        if (!base_class->start (source))
                return FALSE;

572
        connect_bss_signals (GCLUE_WIFI (source));
573 574 575 576 577 578 579 580 581 582 583 584 585 586
        return TRUE;
}

static gboolean
gclue_wifi_stop (GClueLocationSource *source)
{
        GClueLocationSourceClass *base_class;

        g_return_val_if_fail (GCLUE_IS_WIFI (source), FALSE);

        base_class = GCLUE_LOCATION_SOURCE_CLASS (gclue_wifi_parent_class);
        if (!base_class->stop (source))
                return FALSE;

587
        disconnect_bss_signals (GCLUE_WIFI (source));
588 589 590
        return TRUE;
}

591 592 593 594
static GClueAccuracyLevel
gclue_wifi_get_available_accuracy_level (GClueWebSource *source,
                                         gboolean        net_available)
{
595 596
        GClueWifiPrivate *priv = GCLUE_WIFI (source)->priv;

597 598
        if (!net_available)
                return GCLUE_ACCURACY_LEVEL_NONE;
599 600
        else if (priv->interface != NULL &&
                 priv->accuracy_level != GCLUE_ACCURACY_LEVEL_CITY)
601 602 603
                return GCLUE_ACCURACY_LEVEL_STREET;
        else
                return GCLUE_ACCURACY_LEVEL_CITY;
604 605
}

606
static void
607 608 609
on_interface_proxy_ready (GObject      *source_object,
                          GAsyncResult *res,
                          gpointer      user_data)
610 611
{
        GClueWifi *wifi = GCLUE_WIFI (user_data);
612 613 614 615 616 617 618 619 620 621
        WPAInterface *interface;
        GError *error = NULL;

        interface = wpa_interface_proxy_new_for_bus_finish (res, &error);
        if (interface == NULL) {
                g_debug ("%s", error->message);
                g_error_free (error);

                return;
        }
622

623 624
        if (wifi->priv->interface != NULL) {
                g_object_unref (interface);
625
                return;
626
        }
627

628
        wifi->priv->interface = interface;
629
        g_debug ("WiFi device '%s' added.",
630
                 wpa_interface_get_ifname (interface));
631

632
        if (gclue_location_source_get_active (GCLUE_LOCATION_SOURCE (wifi)))
633 634 635 636
                connect_bss_signals (wifi);
        else
                gclue_web_source_refresh (GCLUE_WEB_SOURCE (wifi));
}
637

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
static void
on_interface_added (WPASupplicant *supplicant,
                    const gchar   *path,
                    GVariant      *properties,
                    gpointer       user_data)
{
        GClueWifi *wifi = GCLUE_WIFI (user_data);

        if (wifi->priv->interface != NULL)
                return;

        wpa_interface_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
                                         G_DBUS_PROXY_FLAGS_NONE,
                                         "fi.w1.wpa_supplicant1",
                                         path,
                                         NULL,
                                         on_interface_proxy_ready,
                                         wifi);
656 657 658
}

static void
659 660 661
on_interface_removed (WPASupplicant *supplicant,
                      const gchar   *path,
                      gpointer       user_data)
662 663
{
        GClueWifi *wifi = GCLUE_WIFI (user_data);
664 665 666 667 668
        GClueWifiPrivate *priv = wifi->priv;
        const char *object_path;

        if (priv->interface == NULL)
                return;
669

670 671 672
        object_path = g_dbus_proxy_get_object_path
                        (G_DBUS_PROXY (priv->interface));
        if (g_strcmp0 (object_path, path) != 0)
673
                return;
674

675
        g_debug ("WiFi device '%s' removed.",
676
                 wpa_interface_get_ifname (priv->interface));
677

678 679
        disconnect_bss_signals (wifi);
        g_clear_object (&wifi->priv->interface);
680 681

        gclue_web_source_refresh (GCLUE_WEB_SOURCE (wifi));
682 683
}

684 685 686
static void
gclue_wifi_init (GClueWifi *wifi)
{
687
        wifi->priv = G_TYPE_INSTANCE_GET_PRIVATE ((wifi), GCLUE_TYPE_WIFI, GClueWifiPrivate);
688 689 690 691 692 693 694 695 696

        wifi->priv->bss_proxies = g_hash_table_new_full (g_str_hash,
                                                         g_str_equal,
                                                         g_free,
                                                         g_object_unref);
        wifi->priv->ignored_bss_proxies = g_hash_table_new_full (g_str_hash,
                                                                 g_str_equal,
                                                                 g_free,
                                                                 g_object_unref);
697 698 699 700 701 702
}

static void
gclue_wifi_constructed (GObject *object)
{
        GClueWifi *wifi = GCLUE_WIFI (object);
703 704 705
        GClueWifiPrivate *priv = wifi->priv;
        const gchar *const *interfaces;
        GError *error = NULL;
706

707
        G_OBJECT_CLASS (gclue_wifi_parent_class)->constructed (object);
708

709 710 711 712 713 714
        if (wifi->priv->accuracy_level == GCLUE_ACCURACY_LEVEL_CITY) {
                GClueConfig *config = gclue_config_get_singleton ();

                if (!gclue_config_get_enable_wifi_source (config))
                        goto refresh_n_exit;
        }
715

716 717 718 719 720 721 722 723 724 725 726 727
        /* FIXME: We should be using async variant */
        priv->supplicant = wpa_supplicant_proxy_new_for_bus_sync
                        (G_BUS_TYPE_SYSTEM,
                         G_DBUS_PROXY_FLAGS_NONE,
                         "fi.w1.wpa_supplicant1",
                         "/fi/w1/wpa_supplicant1",
                         NULL,
                         &error);
        if (priv->supplicant == NULL) {
                g_warning ("Failed to connect to wpa_supplicant service: %s",
                           error->message);
                g_error_free (error);
728
                goto refresh_n_exit;
729
        }
Zeeshan Ali's avatar
Zeeshan Ali committed
730

731 732 733 734 735 736 737 738
        g_signal_connect (priv->supplicant,
                          "interface-added",
                          G_CALLBACK (on_interface_added),
                          wifi);
        g_signal_connect (priv->supplicant,
                          "interface-removed",
                          G_CALLBACK (on_interface_removed),
                          wifi);
739

740 741 742 743 744 745
        interfaces = wpa_supplicant_get_interfaces (priv->supplicant);
        if (interfaces != NULL && interfaces[0] != NULL)
                on_interface_added (priv->supplicant,
                                    interfaces[0],
                                    NULL,
                                    wifi);
746

747
refresh_n_exit:
748
        gclue_web_source_refresh (GCLUE_WEB_SOURCE (object));
749 750
}

751 752 753 754 755 756 757 758 759
static void
on_wifi_destroyed (gpointer data,
                   GObject *where_the_object_was)
{
        GClueWifi **wifi = (GClueWifi **) data;

        *wifi = NULL;
}

760 761 762
/**
 * gclue_wifi_new:
 *
763
 * Get the #GClueWifi singleton, for the specified max accuracy level @level.
764
 *
765 766
 * Returns: (transfer full): a new ref to #GClueWifi. Use g_object_unref()
 * when done.
767 768
 **/
GClueWifi *
769
gclue_wifi_get_singleton (GClueAccuracyLevel level)
770
{
771 772
        static GClueWifi *wifi[] = { NULL, NULL };
        guint i;
773
        gboolean scramble_location = FALSE;
774 775

        g_return_val_if_fail (level >= GCLUE_ACCURACY_LEVEL_CITY, NULL);
776 777
        if (level == GCLUE_ACCURACY_LEVEL_NEIGHBORHOOD)
                level = GCLUE_ACCURACY_LEVEL_CITY;
778

779 780 781 782 783 784 785 786 787 788
        if (level == GCLUE_ACCURACY_LEVEL_CITY) {
                GClueConfig *config = gclue_config_get_singleton ();

                i = 0;
                if (gclue_config_get_enable_wifi_source (config))
                        scramble_location = TRUE;
        } else {
                i = 1;
        }

789 790 791
        if (wifi[i] == NULL) {
                wifi[i] = g_object_new (GCLUE_TYPE_WIFI,
                                        "accuracy-level", level,
792
                                        "scramble-location", scramble_location,
793 794
                                        NULL);
                g_object_weak_ref (G_OBJECT (wifi[i]),
795
                                   on_wifi_destroyed,
796
                                   &wifi[i]);
797
        } else
798
                g_object_ref (wifi[i]);
799

800
        return wifi[i];
801 802
}

803 804 805 806 807 808 809 810 811
GClueAccuracyLevel
gclue_wifi_get_accuracy_level (GClueWifi *wifi)
{
        g_return_val_if_fail (GCLUE_IS_WIFI (wifi),
                              GCLUE_ACCURACY_LEVEL_NONE);

        return wifi->priv->accuracy_level;
}

812 813 814
static GList *
get_bss_list (GClueWifi *wifi,
              GError   **error)
815
{
816
        if (wifi->priv->interface == NULL) {
817 818 819 820
                g_set_error_literal (error,
                                     G_IO_ERROR,
                                     G_IO_ERROR_FAILED,
                                     "No WiFi devices available");
821
                return NULL;
822 823
        }

824
        return g_hash_table_get_values (wifi->priv->bss_proxies);
825 826 827 828 829 830
}

static SoupMessage *
gclue_wifi_create_query (GClueWebSource *source,
                         GError        **error)
{
831
        GList *bss_list; /* As in Access Points */
832

833
        bss_list = get_bss_list (GCLUE_WIFI (source), NULL);
834

Zeeshan Ali's avatar
Zeeshan Ali committed
835
        return gclue_mozilla_create_query (bss_list, NULL, error);
836 837
}

Ankit's avatar
Ankit committed
838
static GClueLocation *
839 840 841
gclue_wifi_parse_response (GClueWebSource *source,
                           const char     *json,
                           GError        **error)
842
{
843
        return gclue_mozilla_parse_response (json, error);
844 845 846 847
}

static SoupMessage *
gclue_wifi_create_submit_query (GClueWebSource  *source,
Ankit's avatar
Ankit committed
848
                                GClueLocation   *location,
849 850
                                GError         **error)
{
851
        GList *bss_list; /* As in Access Points */
852

853 854 855
        bss_list = get_bss_list (GCLUE_WIFI (source), error);
        if (bss_list == NULL)
                return NULL;
856

857 858
        return gclue_mozilla_create_submit_query (location,
                                                  bss_list,
859
                                                  NULL,
860
                                                  error);
861
}