From cebc5cd77a76e7b1afb263c368ba5fb57932de30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 7 Aug 2023 08:40:51 +0000 Subject: [PATCH] Wifi (Linux): support signal quality detection with ioctl Also don't show 802.11 as protocol --- src/detection/wifi/wifi_linux.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/detection/wifi/wifi_linux.c b/src/detection/wifi/wifi_linux.c index f15d0932f..9a75d587e 100644 --- a/src/detection/wifi/wifi_linux.c +++ b/src/detection/wifi/wifi_linux.c @@ -249,7 +249,7 @@ static const char* detectWifiWithIoctls(FFlist* result) if(!ffAppendFileBuffer(path.chars, &item->inf.status) || !ffStrbufEqualS(&item->inf.status, "up")) continue; - int sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); + FF_AUTO_CLOSE_FD int sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if(sock < 0) continue; @@ -262,7 +262,7 @@ static const char* detectWifiWithIoctls(FFlist* result) if(ioctl(sock, SIOCGIWESSID, &iwr) >= 0) ffStrbufRecalculateLength(&item->conn.ssid); - if(ioctl(sock, SIOCGIWNAME, &iwr) >= 0) + if(ioctl(sock, SIOCGIWNAME, &iwr) >= 0 && !ffStrEqualsIgnCase(iwr.u.name, "IEEE 802.11")) { if(ffStrStartsWithIgnCase(iwr.u.name, "IEEE ")) ffStrbufSetS(&item->conn.protocol, iwr.u.name + strlen("IEEE ")); @@ -277,9 +277,16 @@ static const char* detectWifiWithIoctls(FFlist* result) ffStrbufTrimRight(&item->conn.macAddress, '-'); } - //FIXME: doesn't work - if(ioctl(sock, SIOCGIWSPY, &iwr) >= 0) - item->conn.signalQuality = iwr.u.qual.level; + struct iw_statistics stats; + iwr.u.data.pointer = &stats; + iwr.u.data.length = sizeof(stats); + iwr.u.data.flags = 0; + + if(ioctl(sock, SIOCGIWSTATS, &iwr) >= 0) + { + int8_t level = (int8_t) stats.qual.level; // https://stackoverflow.com/questions/18079771/wireless-h-how-do-i-print-out-the-signal-level + item->conn.signalQuality = level >= -50 ? 100 : level <= -100 ? 0 : (level + 100) * 2; + } //FIXME: doesn't work struct iw_encode_ext iwe; @@ -311,8 +318,6 @@ static const char* detectWifiWithIoctls(FFlist* result) break; } } - - close(sock); } if_freenameindex(infs);