For Linux and FreeBSD enable background scanning of 60 seconds
authorRoy Marples <roy@marples.name>
Thu, 4 Dec 2014 11:11:00 +0000 (11:11 +0000)
committerRoy Marples <roy@marples.name>
Thu, 4 Dec 2014 11:11:00 +0000 (11:11 +0000)
for new AP's or 5 seconds when the SSID menu is displayed.

configure
src/dhcpcd-gtk/main.c
src/dhcpcd-gtk/menu.c
src/dhcpcd-qt/dhcpcd-qt.pro
src/dhcpcd-qt/dhcpcd-wi.cpp
src/dhcpcd-qt/dhcpcd-wi.h
src/libdhcpcd/dhcpcd.h

index a5589f5aa6e7c233aff4bd915c19ff2b77545f81..1de774916dbc5b54c956cee19599df8470d2db8f 100755 (executable)
--- a/configure
+++ b/configure
@@ -135,7 +135,7 @@ if [ -z "$OS" ]; then
        esac
 fi
 
-echo "Configuring dhcpcd for ... $OS"
+echo "Configuring dhcpcd-ui for ... $OS"
 rm -f $CONFIG_H $CONFIG_MK
 echo "# $OS" >$CONFIG_MK
 echo "/* $OS */" >$CONFIG_H
@@ -216,6 +216,7 @@ if [ -n "$LDFLAGS" ]; then
        echo "LDFLAGS=" >>$CONFIG_MK
        echo "LDFLAGS+= $LDFLAGS" >>$CONFIG_MK
 fi
+QMAKE_CONFIG=
 
 for x in $INCLUDEDIR; do
        echo "CPPFLAGS+=        -I$x" >>$CONFIG_MK
@@ -248,6 +249,23 @@ else
        echo "CFLAGS+=  -DNDEBUG" >>$CONFIG_MK
 fi
 
+printf "OS supports background scanning ... "
+# Most OS's don't support background scanning
+case "$OS" in
+       linux*|freebsd*)
+               BGSCAN=true;;
+       *)      BGSCAN=false;;
+esac
+printf "%s ... " "$OS"
+if $BGSCAN; then
+       echo "yes"
+       echo "CFLAGS+=  -DBG_SCAN" >> $CONFIG_MK
+       QMAKE_CONFIG="$QMAKE_CONFIG CONFIG+=build_bgscan"
+else
+       echo "no"
+fi
+
+
 # Add CPPFLAGS and CFLAGS to CC for testing features
 XCC="$CC `$SED -n -e 's/CPPLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
 XCC="$XCC `$SED -n -e 's/CFLAGS+=*\(.*\)/\1/p' $CONFIG_MK`"
@@ -267,7 +285,7 @@ EOF
        elif $XCC _gettext.c -o _gettext -lintl 2>&3; then
                GETTEXT="yes (-lintl)"
                LIB_INTL=-lintl
-               QMAKE_CONFIG="CONFIG+=has_libintl"
+               QMAKE_CONFIG="$QMAKE_CONFIG CONFIG+=has_libintl"
        else
                GETTEXT=no
        fi
index fcb2de4a363bbd32b1608bbd234cbb6b348d1d98..35fe4b26bc00107011405140dbc0ab045206d259 100644 (file)
@@ -601,6 +601,27 @@ dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, const char *status, _unused void *data)
        }
 }
 
+#ifdef BG_SCAN
+static gboolean
+bgscan(gpointer data)
+{
+       WI_SCAN *w;
+       DHCPCD_CONNECTION *con;
+       DHCPCD_WPA *wpa;
+
+       con = (DHCPCD_CONNECTION *)data;
+       TAILQ_FOREACH(w, &wi_scans, next) {
+               if (w->interface->wireless && w->interface->up) {
+                       wpa = dhcpcd_wpa_find(con, w->interface->ifname);
+                       if (wpa)
+                               dhcpcd_wpa_scan(wpa);
+               }
+       }
+
+       return TRUE;
+}
+#endif
+
 int
 main(int argc, char *argv[])
 {
@@ -641,6 +662,9 @@ main(int argc, char *argv[])
                g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
 
        menu_init(status_icon, con);
+#ifdef BG_SCAN
+       g_timeout_add(DHCPCD_WPA_SCAN_LONG, bgscan, con);
+#endif
 
        gtk_main();
        dhcpcd_close(con);
index db41fa4d5974b0ec0acea60c6c3ea1e77b28c3f3..e13353685ff1a81203eb2f9948e118a8b33366f0 100644 (file)
@@ -32,6 +32,9 @@ static const char *copyright = "Copyright (c) 2009-2014 Roy Marples";
 static GtkStatusIcon *sicon;
 static GtkWidget *menu;
 static GtkAboutDialog *about;
+#ifdef BG_SCAN
+static guint bgscan_timer;
+#endif
 
 static void
 on_pref(_unused GObject *o, gpointer data)
@@ -333,6 +336,13 @@ menu_abort(void)
        WI_SCAN *wis;
        WI_MENU *wim;
 
+#ifdef BG_SCAN
+       if (bgscan_timer) {
+               g_source_remove(bgscan_timer);
+               bgscan_timer = 0;
+       }
+#endif
+
        TAILQ_FOREACH(wis, &wi_scans, next) {
                wis->ifmenu = NULL;
                while ((wim = TAILQ_FIRST(&wis->menus))) {
@@ -349,6 +359,32 @@ menu_abort(void)
        }
 }
 
+#ifdef BG_SCAN
+static gboolean
+menu_bgscan(gpointer data)
+{
+       WI_SCAN *w;
+       DHCPCD_CONNECTION *con;
+       DHCPCD_WPA *wpa;
+
+       if (menu == NULL || !gtk_widget_get_visible(menu)) {
+               bgscan_timer = 0;
+               return FALSE;
+       }
+
+       con = (DHCPCD_CONNECTION *)data;
+       TAILQ_FOREACH(w, &wi_scans, next) {
+               if (w->interface->wireless && w->interface->up) {
+                       wpa = dhcpcd_wpa_find(con, w->interface->ifname);
+                       if (wpa)
+                               dhcpcd_wpa_scan(wpa);
+               }
+       }
+
+       return TRUE;
+}
+#endif
+
 static void
 on_activate(GtkStatusIcon *icon)
 {
@@ -386,6 +422,11 @@ on_activate(GtkStatusIcon *icon)
                gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
                    gtk_status_icon_position_menu, icon,
                    1, gtk_get_current_event_time());
+
+#ifdef BG_SCAN
+               bgscan_timer = g_timeout_add(DHCPCD_WPA_SCAN_SHORT,
+                   menu_bgscan, dhcpcd_if_connection(w->interface));
+#endif
        }
 }
 
index 29fedf14a6335d168fa5e0d7c4a79c2484c63ae8..9d6b83880be3d7db66512e1ba53c80e941d79417 100644 (file)
@@ -16,6 +16,10 @@ INCLUDEPATH+=                ../libdhcpcd/
 
 LIBS+=                 -L../libdhcpcd ../libdhcpcd/libdhcpcd.a
 
+build_bgscan {
+       DEFINES+=       BG_SCAN
+}
+
 has_libintl {
        LIBS +=         -lintl
 }
index 060b2b30c104361a67930230ccb054d1079c339c..b4c32f8a8c182c7b01da0a18b1105e7b7c2141ae 100644 (file)
@@ -52,6 +52,9 @@ DhcpcdWi::DhcpcdWi(DhcpcdQt *parent, DHCPCD_WPA *wpa)
 
        notifier = NULL;
        pingTimer = NULL;
+#ifdef BG_SCAN
+       scanTimer = NULL;
+#endif
 }
 
 DhcpcdWi::~DhcpcdWi()
@@ -76,6 +79,14 @@ DhcpcdWi::~DhcpcdWi()
                pingTimer = NULL;
        }
 
+#ifdef BG_SCAN
+       if (scanTimer) {
+               scanTimer->stop();
+               scanTimer->deleteLater();
+               scanTimer = NULL;
+       }
+#endif
+
        dhcpcd_wi_scans_free(scans);
 }
 
@@ -178,6 +189,11 @@ void DhcpcdWi::createMenu1(QMenu *menu)
        DHCPCD_WI_SCAN *scan;
        QAction *before;
 
+#ifdef BG_SCAN
+       connect(menu, SIGNAL(aboutToShow()), this, SLOT(menuShown()));
+       connect(menu, SIGNAL(aboutToHide()), this, SLOT(menuHidden()));
+#endif
+
        i = dhcpcd_wpa_if(wpa);
        for (scan = scans; scan; scan = scan->next) {
                before = NULL;
@@ -233,6 +249,11 @@ bool DhcpcdWi::open()
        pingTimer = new QTimer(this);
        connect(pingTimer, SIGNAL(timeout()), this, SLOT(ping()));
        pingTimer->start(DHCPCD_WPA_PING);
+#ifdef BG_SCAN
+       scanTimer = new QTimer(this);
+       connect(scanTimer, SIGNAL(timeout()), this, SLOT(scan()));
+       scanTimer->start(DHCPCD_WPA_SCAN_LONG);
+#endif
        return true;
 }
 
@@ -249,6 +270,32 @@ void DhcpcdWi::ping()
                dhcpcd_wpa_close(wpa);
 }
 
+#ifdef BG_SCAN
+void DhcpcdWi::scan()
+{
+
+       dhcpcd_wpa_scan(wpa);
+}
+
+void DhcpcdWi::menuHidden()
+{
+
+       if (scanTimer) {
+               scanTimer->stop();
+               scanTimer->start(DHCPCD_WPA_SCAN_LONG);
+       }
+}
+
+void DhcpcdWi::menuShown()
+{
+
+       if (scanTimer) {
+               scanTimer->stop();
+               scanTimer->start(DHCPCD_WPA_SCAN_SHORT);
+       }
+}
+#endif
+
 void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
 {
        DHCPCD_WI_SCAN s;
index 516a9acfd0ff9863cff83eed2334630ec24b5edd..1159725cb71a4e7522e20e63599fa7ca95930967 100644 (file)
@@ -59,6 +59,11 @@ public:
 private slots:
        void dispatch();
        void ping();
+#ifdef BG_SCAN
+       void scan();
+       void menuHidden();
+       void menuShown();
+#endif
        void connectSsid(DHCPCD_WI_SCAN *scan);
 
 private:
@@ -68,6 +73,9 @@ private:
 
        QSocketNotifier *notifier;
        QTimer *pingTimer;
+#ifdef BG_SCAN
+       QTimer *scanTimer;
+#endif
 
        QMenu *menu;
        void createMenuItem(QMenu *menu, DHCPCD_WI_SCAN *scan,
index ff8f2483dd0781a56f5fe94e1577553449b3428f..11e64d3734d57db358e9d5297c000a46f1e39410 100644 (file)
@@ -49,6 +49,8 @@ extern "C" {
 
 #define DHCPCD_RETRYOPEN       100     /* milliseconds */
 #define DHCPCD_WPA_PING                500     /* milliseconds */
+#define DHCPCD_WPA_SCAN_LONG   60000   /* milliseconds */
+#define DHCPCD_WPA_SCAN_SHORT  5000    /* milliseconds */
 #define DHCPCD_WI_HIST_MAX     10      /* Recall 10 scans for averages */
 
 /* Each non printable byte of the SSID is represented as \000 */