X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/16a97fbca129bca5656f76191c1b324b1649ec22..49d1e5fae2e4957abcf0f1056b3e8df8d695094c:/components/wifi.c diff --git a/components/wifi.c b/components/wifi.c index 139ec8c..591f6ad 100644 --- a/components/wifi.c +++ b/components/wifi.c @@ -1,22 +1,22 @@ /* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include +#include +#include +#include + +#include "../util.h" + #if defined(__linux__) - #include - #include #include #include - #include - #include - #include - #include - #include - - #include "../util.h" const char * wifi_perc(const char *iface) { int i, cur; - float perc; int total = 70; /* the max of /proc/net/wireless */ char *p, *datastart; char path[PATH_MAX]; @@ -26,8 +26,7 @@ snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, "/operstate"); if (!(fp = fopen(path, "r"))) { - fprintf(stderr, "fopen '%s': %s\n", path, - strerror(errno)); + warn("fopen '%s':", path); return NULL; } p = fgets(status, 5, fp); @@ -37,8 +36,7 @@ } if (!(fp = fopen("/proc/net/wireless", "r"))) { - fprintf(stderr, "fopen '/proc/net/wireless': %s\n", - strerror(errno)); + warn("fopen '/proc/net/wireless':"); return NULL; } @@ -59,30 +57,27 @@ sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t " "%*d\t\t%*d\t\t %*d\t %*d\t\t %*d", &cur); - perc = (float)cur / total * 100.0; - - return bprintf("%.0f", perc); + return bprintf("%d", (int)((float)cur / total * 100)); } const char * wifi_essid(const char *iface) { static char id[IW_ESSID_MAX_SIZE+1]; - int sockfd = socket(AF_INET, SOCK_DGRAM, 0); + int sockfd; struct iwreq wreq; memset(&wreq, 0, sizeof(struct iwreq)); wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface); - if (sockfd < 0) { - fprintf(stderr, "socket 'AF_INET': %s\n", - strerror(errno)); + if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + warn("socket 'AF_INET':"); return NULL; } wreq.u.essid.pointer = id; if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) { - fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno)); + warn("ioctl 'SIOCGIWESSID':"); close(sockfd); return NULL; } @@ -96,5 +91,71 @@ return id; } #elif defined(__OpenBSD__) - /* unimplemented */ + #include + #include + #include + #include /* before for NBBY */ + #include + #include + #include + + static int + load_ieee80211_nodereq(const char *iface, struct ieee80211_nodereq *nr) + { + struct ieee80211_bssid bssid; + int sockfd; + + memset(&bssid, 0, sizeof(bssid)); + memset(nr, 0, sizeof(struct ieee80211_nodereq)); + if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + warn("socket 'AF_INET':"); + return 0; + } + strlcpy(bssid.i_name, iface, sizeof(bssid.i_name)); + if ((ioctl(sockfd, SIOCG80211BSSID, &bssid)) < 0) { + warn("ioctl 'SIOCG80211BSSID':"); + close(sockfd); + return 0; + } + strlcpy(nr->nr_ifname, iface, sizeof(nr->nr_ifname)); + memcpy(&nr->nr_macaddr, bssid.i_bssid, sizeof(nr->nr_macaddr)); + if ((ioctl(sockfd, SIOCG80211NODE, nr)) < 0 && nr->nr_rssi) { + warn("ioctl 'SIOCG80211NODE':"); + close(sockfd); + return 0; + } + + return close(sockfd), 1; + } + + const char * + wifi_perc(const char *iface) + { + struct ieee80211_nodereq nr; + int q; + + if (load_ieee80211_nodereq(iface, &nr)) { + if (nr.nr_max_rssi) { + q = IEEE80211_NODEREQ_RSSI(&nr); + } else { + q = nr.nr_rssi >= -50 ? 100 : (nr.nr_rssi <= -100 ? 0 : + (2 * (nr.nr_rssi + 100))); + } + return bprintf("%d", q); + } + + return NULL; + } + + const char * + wifi_essid(const char *iface) + { + struct ieee80211_nodereq nr; + + if (load_ieee80211_nodereq(iface, &nr)) { + return bprintf("%s", nr.nr_nwid); + } + + return NULL; + } #endif