Calculate strength based on the level reported instead of
authorRoy Marples <roy@marples.name>
Thu, 4 Sep 2014 12:44:00 +0000 (12:44 +0000)
committerRoy Marples <roy@marples.name>
Thu, 4 Sep 2014 12:44:00 +0000 (12:44 +0000)
the wpa_supplicant quality field.
Add conversions for WEXT and dBm.

src/dhcpcd-gtk/menu.c
src/dhcpcd-qt/dhcpcd-ssidmenu.cpp
src/libdhcpcd/dhcpcd.h
src/libdhcpcd/wpa.c

index dbf39756eb48b2d170304451988c9363ab36430e..f2c7bc9fbbaccbba91197b95a08748342e5eaf95 100644 (file)
@@ -120,7 +120,6 @@ add_scans(WI_SCAN *scan)
        DHCPCD_WI_SCAN *wis;
        GtkWidget *menu, *item, *image, *box, *label, *bar;
        double perc;
-       int strength;
        const char *icon;
        char *tip;
 
@@ -150,11 +149,7 @@ 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);
-               if (wis->quality.value == 0)
-                       strength = wis->level.average;
-               else
-                       strength = wis->quality.average;
-               perc = CLAMP(strength, 0, 100) / 100.0;
+               perc = wis->strength.value / 100.0;
                gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(bar), perc);
 
                if (wis->flags[0] == '\0')
index cc91667678971a3f4e4070c215b81c0e362cafb5..ed7f5ca6dcc1979771b5c9b85898549bee3ebacc 100644 (file)
@@ -64,13 +64,7 @@ DhcpcdSsidMenu::DhcpcdSsidMenu(QWidget *parent, DHCPCD_IF *ifp, DHCPCD_WI_SCAN *
        licon->setPixmap(picon);
        layout->addWidget(licon);
        bar = new QProgressBar(this);
-       bar->setMinimum(0);
-       bar->setMaximum(100);
-       if (scan->quality.value == 0)
-           strength = scan->level.average;
-       else
-           strength = scan->quality.average;
-       bar->setValue(strength < 0 ? 0 : strength > 100 ? 100 : strength);
+       bar->setValue(scan->strength.value);
        layout->addWidget(bar);
 
        button->installEventFilter(this);
index 788bd23a7b666622f9d4527c54caab1908e93cc4..4b6fecb2812b11137bedab2fc5a84f3d8f9d444a 100644 (file)
@@ -65,6 +65,7 @@ typedef struct dhcpcd_wi_scan {
        DHCPCD_WI_AV quality;
        DHCPCD_WI_AV noise;
        DHCPCD_WI_AV level;
+       DHCPCD_WI_AV strength;
        char ssid[IF_SSIDSIZE];
        char flags[FLAGSIZE];
 } DHCPCD_WI_SCAN;
@@ -116,6 +117,7 @@ typedef struct dhcpcd_wi_hist {
        int quality;
        int noise;
        int level;
+       int strength;
 } DHCPCD_WI_HIST;
 
 typedef struct dhcpcd_wpa {
index 933dfcfb1471e6a67fdd7a8ae348f21b7eb9ea1b..92a690696e951f1fc43b62f1aa9e9d01bc713584 100644 (file)
@@ -46,6 +46,9 @@
        (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
 #endif
 
+#define CLAMP(x, low, high) \
+       (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
+
 static int
 wpa_open(const char *ifname, char **path)
 {
@@ -235,6 +238,23 @@ dhcpcd_wpa_scans_read(DHCPCD_WPA *wpa)
                        else if (strncmp(s, "ssid=", 5) == 0)
                                strlcpy(w->ssid, s + 5, sizeof(w->ssid));
                }
+
+               w->strength.value = w->level.value;
+               if (w->strength.value > 110 && w->strength.value < 256)
+                       /* Convert WEXT level to dBm */
+                       w->strength.value -= 256;
+
+               if (w->strength.value < 0) {
+                       /* Assume dBm */
+                       w->strength.value =
+                           abs(CLAMP(w->strength.value, -100, -40) + 40);
+                       w->strength.value =
+                           100 - ((100 * w->strength.value) / 60);
+               } else {
+                       /* Assume quality percentage */
+                       w->strength.value = CLAMP(w->strength.value, 0, 100);
+               }
+
        }
        return wis;
 }
@@ -257,6 +277,7 @@ dhcpcd_wi_scans(DHCPCD_IF *i)
                w->quality.average = w->quality.value;
                w->noise.average = w->noise.value;
                w->level.average = w->level.value;
+               w->strength.average = w->strength.value;
 
                for (h = wpa->con->wi_history; h; h = h->next) {
                        if (strcmp(h->ifname, i->ifname) == 0 &&
@@ -265,6 +286,7 @@ dhcpcd_wi_scans(DHCPCD_IF *i)
                                w->quality.average += h->quality;
                                w->noise.average += h->noise;
                                w->level.average += h->level;
+                               w->strength.average += h->strength;
                                if (++nh == DHCPCD_WI_HIST_MAX) {
                                        hl->next = h->next;
                                        free(h);
@@ -278,6 +300,7 @@ dhcpcd_wi_scans(DHCPCD_IF *i)
                        w->quality.average /= nh;
                        w->noise.average /= nh;
                        w->level.average /= nh;
+                       w->strength.average /= nh;
                }
                h = malloc(sizeof(*h));
                if (h) {
@@ -286,6 +309,7 @@ dhcpcd_wi_scans(DHCPCD_IF *i)
                        h->quality = w->quality.value;
                        h->noise = w->noise.value;
                        h->level = w->level.value;
+                       h->strength = w->strength.value;
                        h->next = wpa->con->wi_history;
                        wpa->con->wi_history = h;
                }