Record a history of scan results so we can use an average for a more accurate display.
authorRoy Marples <roy@marples.name>
Mon, 20 Apr 2009 12:01:50 +0000 (12:01 +0000)
committerRoy Marples <roy@marples.name>
Mon, 20 Apr 2009 12:01:50 +0000 (12:01 +0000)
src/dhcpcd-gtk/menu.c
src/libdhcpcd/libdhcpcd.h
src/libdhcpcd/main.c
src/libdhcpcd/wpa.c

index 51b74dd522de39e021dae9d31e11bb9e6b1b265e..f80f75587d9e2153ba61e8b9ec4f3cd7d508f4f6 100644 (file)
@@ -105,6 +105,8 @@ add_scans(WI_SCAN *scan)
        const char *icon;
        char *tip;
 
+       if (scan->scans == NULL)
+               return NULL;
        menu = gtk_menu_new();
        for (wis = scan->scans; wis; wis = wis->next) {
                item = gtk_check_menu_item_new();
@@ -129,8 +131,11 @@ add_scans(WI_SCAN *scan)
                bar = gtk_progress_bar_new();
                gtk_widget_set_size_request(bar, 100, -1);
                gtk_box_pack_end(GTK_BOX(box), bar, FALSE, TRUE, 0);
-               strength = CLAMP(wis->quality, 0, 100);
-               perc = strength / 100.0;
+               if (wis->quality.value == 0)
+                       strength = wis->level.average;
+               else
+                       strength = wis->quality.average;
+               perc = CLAMP(strength, 0, 100) / 100.0;
                gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(bar), perc);
 
                tip = g_strconcat(wis->bssid, " ", wis->flags, NULL);
@@ -173,11 +178,13 @@ on_activate(GtkStatusIcon *icon)
                }
        } else
                menu = add_scans(wi_scans);
-       
-       gtk_widget_show_all(GTK_WIDGET(menu));
-       gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
-           gtk_status_icon_position_menu, icon,
-           1, gtk_get_current_event_time());
+
+       if (menu) {
+               gtk_widget_show_all(GTK_WIDGET(menu));
+               gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
+                   gtk_status_icon_position_menu, icon,
+                   1, gtk_get_current_event_time());
+       }
 }
 
 static void
index 323b9938d7127f3ff6b93e4409a92bceea9cc69d..efbd502665dd17a2a9cedfd8db3d149c37c41e1d 100644 (file)
 #define FLAGSIZE 8
 #define REASONSIZE 16
 
+typedef struct dhcpcd_wi_avs {
+       int value;
+       int average;
+} DHCPCD_WI_AV;
+
 typedef struct dhcpcd_wi_scan {
        char bssid[IF_BSSIDSIZE];
        int frequency;
-       int quality;
-       int noise;
-       int level;
+       DHCPCD_WI_AV quality;
+       DHCPCD_WI_AV noise;
+       DHCPCD_WI_AV level;
        char flags[FLAGSIZE];
        char ssid[IF_SSIDSIZE];
        struct dhcpcd_wi_scan *next;
@@ -66,6 +71,15 @@ typedef struct dhcpcd_if {
 typedef DBusMessage DHCPCD_MESSAGE;
 typedef DBusMessageIter DHCPCD_MESSAGEITER;
 
+typedef struct dhcpcd_wi_hist {
+       char ifname[IF_NAMESIZE];
+       char bssid[IF_BSSIDSIZE];
+       int quality;
+       int noise;
+       int level;
+       struct dhcpcd_wi_hist *next;
+} DHCPCD_WI_HIST;
+
 typedef struct dhcpcd_connection {
        DBusConnection *bus;
        char *error;
@@ -84,6 +98,7 @@ typedef struct dhcpcd_connection {
            void *);
        void *signal_data;
        DHCPCD_IF *interfaces;
+       DHCPCD_WI_HIST *wi_history;
        struct dhcpcd_connection *next;
 } DHCPCD_CONNECTION;
 
@@ -141,6 +156,7 @@ char * dhcpcd_if_message(const DHCPCD_IF *);
 
 DHCPCD_WI_SCAN * dhcpcd_wi_scans(DHCPCD_CONNECTION *, DHCPCD_IF *);
 void dhcpcd_wi_scans_free(DHCPCD_WI_SCAN *);
+void dhcpcd_wi_history_clear(DHCPCD_CONNECTION *);
 bool dhcpcd_wpa_command(DHCPCD_CONNECTION *, DHCPCD_IF *, const char *, int);
 bool dhcpcd_wpa_set_network(DHCPCD_CONNECTION *, DHCPCD_IF *,
     int, const char *, const char *);
index 327c3dfb32549ea87b1919186a47f834297e85b1..6e4059e2dbb714e5e2a428062b585f0228a8e299 100644 (file)
@@ -198,6 +198,7 @@ dhcpcd_close(DHCPCD_CONNECTION *con)
                                l->next = con->next;
                        dbus_connection_unref(con->bus);
                        dhcpcd_if_free(con->interfaces);
+                       dhcpcd_wi_history_clear(con);
                        free(con->status);
                        free(con->error);
                        free(con);
index 73a6acf0517930293b28d35df2f00abf5319d853..6d2360125f0d37031f3e0f98d498902a16877503 100644 (file)
 #define IN_LIBDHCPCD
 #include "libdhcpcd.h"
 
+#define HIST_MAX 10 /* Max history per ssid/bssid */
+
+void
+dhcpcd_wi_history_clear(DHCPCD_CONNECTION *con)
+{
+       DHCPCD_WI_HIST *h;
+
+       while (con->wi_history) {
+               h = con->wi_history->next;
+               free(con->wi_history);
+               con->wi_history = h;
+       }
+}
+
 void
 dhcpcd_wi_scans_free(DHCPCD_WI_SCAN *wis)
 {
@@ -83,15 +97,15 @@ dhcpcd_scanresult_new(DHCPCD_CONNECTION *con, DBusMessageIter *array)
                } else if (strcmp(s, "Quality") == 0) {
                        if (!dhcpcd_iter_get(con, &var, DBUS_TYPE_INT32, &i32))
                                break;
-                       wis->quality = i32;
+                       wis->quality.value = i32;
                } else if (strcmp(s, "Noise") == 0) {
                        if (!dhcpcd_iter_get(con, &var, DBUS_TYPE_INT32, &i32))
                                break;
-                       wis->noise = i32;
+                       wis->noise.value = i32;
                } else if (strcmp(s, "Level") == 0) {
                        if (!dhcpcd_iter_get(con, &var, DBUS_TYPE_INT32, &i32))
                                break;
-                       wis->level = i32;
+                       wis->level.value = i32;
                } else if (strcmp(s, "Flags") == 0) {
                        if (!dhcpcd_iter_get(con, &var, DBUS_TYPE_STRING, &s))
                                break;
@@ -117,7 +131,8 @@ dhcpcd_wi_scans(DHCPCD_CONNECTION *con, DHCPCD_IF *i)
        DBusMessage *msg;
        DBusMessageIter args, array;
        DHCPCD_WI_SCAN *wis, *scans, *l;
-       int errors;
+       DHCPCD_WI_HIST *h, *hl;
+       int errors, nh;
 
        msg = dhcpcd_message_reply(con, "ScanResults", i->ifname);
        if (!dbus_message_iter_init(msg, &args) ||
@@ -137,6 +152,41 @@ dhcpcd_wi_scans(DHCPCD_CONNECTION *con, DHCPCD_IF *i)
                wis = dhcpcd_scanresult_new(con, &array);
                if (wis == NULL)
                        break;
+               nh = 1;
+               hl = NULL;
+               wis->quality.average = wis->quality.value;
+               wis->noise.average = wis->noise.value;
+               wis->level.average = wis->level.value;
+               for (h = con->wi_history; h; h = h->next) {
+                       if (strcmp(h->ifname, i->ifname) == 0 &&
+                           strcmp(h->bssid, wis->bssid) == 0)
+                       {
+                               wis->quality.average += h->quality;
+                               wis->noise.average += h->noise;
+                               wis->level.average += h->level;
+                               if (++nh == HIST_MAX) {
+                                       hl->next = h->next;
+                                       free(h);
+                                       break;
+                               }
+                       }
+                       hl = h;
+               }
+               if (nh != 1) {
+                       wis->quality.average /= nh;
+                               wis->noise.average /= nh;
+                       wis->level.average /= nh;
+               }
+               h = malloc(sizeof(*h));
+               if (h) {
+                       strlcpy(h->ifname, i->ifname, sizeof(h->ifname));
+                       strlcpy(h->bssid, wis->bssid, sizeof(h->bssid));
+                       h->quality = wis->quality.value;
+                       h->noise = wis->noise.value;
+                       h->level = wis->level.value;
+                       h->next = con->wi_history;
+                       con->wi_history = h;
+               }
                if (l == NULL)
                        scans = l = wis;
                else {