nl80211: fix code calculating average signal and rate
authorRafał Miłecki <rafal@milecki.pl>
Wed, 28 Mar 2018 08:30:07 +0000 (10:30 +0200)
committerRafał Miłecki <rafal@milecki.pl>
Mon, 9 Apr 2018 05:30:37 +0000 (07:30 +0200)
Using average of previous average and the next value is highly
imprecise. E.g. for values 20, 20, 20, 180 it would result in /average/
of 100 (instead of 60). Fix it by storing & using an info of how many
samples were used for the previous calculation.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
iwinfo_nl80211.c
iwinfo_nl80211.h

index 738e2f5ed9f4526af49caecbdc2a425e9cc8ac46..de5d2b4baabb3772d2a0ea70915a33ec667dabb1 100644 (file)
@@ -1279,7 +1279,8 @@ static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
                        if (sinfo[NL80211_STA_INFO_SIGNAL])
                        {
                                dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
-                               rr->rssi = rr->rssi ? (int8_t)((rr->rssi + dbm) / 2) : dbm;
+                               rr->rssi = (rr->rssi * rr->rssi_samples + dbm) / (rr->rssi_samples + 1);
+                               rr->rssi_samples++;
                        }
 
                        if (sinfo[NL80211_STA_INFO_TX_BITRATE])
@@ -1291,8 +1292,8 @@ static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
                                        if (rinfo[NL80211_RATE_INFO_BITRATE])
                                        {
                                                mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
-                                               rr->rate = rr->rate
-                                                       ? (int16_t)((rr->rate + mbit) / 2) : mbit;
+                                               rr->rate = (rr->rate * rr->rate_samples + mbit) / (rr->rate_samples + 1);
+                                               rr->rate_samples++;
                                        }
                                }
                        }
@@ -1307,8 +1308,7 @@ static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r)
        DIR *d;
        struct dirent *de;
 
-       r->rssi = 0;
-       r->rate = 0;
+       memset(r, 0, sizeof(*r));
 
        if ((d = opendir("/sys/class/net")) != NULL)
        {
@@ -1333,7 +1333,7 @@ static int nl80211_get_bitrate(const char *ifname, int *buf)
 
        nl80211_fill_signal(ifname, &rr);
 
-       if (rr.rate)
+       if (rr.rate_samples)
        {
                *buf = (rr.rate * 100);
                return 0;
@@ -1348,7 +1348,7 @@ static int nl80211_get_signal(const char *ifname, int *buf)
 
        nl80211_fill_signal(ifname, &rr);
 
-       if (rr.rssi)
+       if (rr.rssi_samples)
        {
                *buf = rr.rssi;
                return 0;
index 566ffcea99e6d626e7f4c900fffbeabe5bc924a7..2dff08b98b7505438fda61581fa0bb5166978da4 100644 (file)
@@ -59,7 +59,9 @@ struct nl80211_group_conveyor {
 
 struct nl80211_rssi_rate {
        int16_t rate;
+       int rate_samples;
        int8_t  rssi;
+       int rssi_samples;
 };
 
 struct nl80211_array_buf {