status-wifi
authorStiletto <blasux@blasux.ru>
Thu, 23 Oct 2014 17:55:13 +0400
changeset 4 70b4e0b27e2c
parent 3 dae770da6416
child 5 e3ee9aa21b1f
status-wifi
config.mk
status-wifi/Makefile
status-wifi/config.def.h
status-wifi/config.mk
status-wifi/wifi.c
--- a/config.mk	Mon Oct 20 23:24:03 2014 +0400
+++ b/config.mk	Thu Oct 23 17:55:13 2014 +0400
@@ -16,6 +16,6 @@
 STATUS_CFLAGS = -std=c99 -pedantic -Wall -Os ${STATUS_INCS} ${STATUS_CPPFLAGS}
 STATUS_LDFLAGS = ${STATUS_LIBS} -static
 
-STATUS = status-time status-mem status-loadavg
+STATUS = status-time status-mem status-loadavg status-wifi
 # compiler and linker
 CC = cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/status-wifi/Makefile	Thu Oct 23 17:55:13 2014 +0400
@@ -0,0 +1,12 @@
+# dwm - dynamic window manager
+# See LICENSE file for copyright and license details.
+
+include ../config.mk
+include config.mk
+
+OUT = status-wifi
+SRC = wifi.c
+OBJ = ${SRC:.c=.o}
+CONFIG = config.h
+
+include ../Makefile.status.inc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/status-wifi/config.def.h	Thu Oct 23 17:55:13 2014 +0400
@@ -0,0 +1,2 @@
+#define UPDATE_PERIOD 15
+//#define PANGO_MARKUP // use second argument instead
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/status-wifi/config.mk	Thu Oct 23 17:55:13 2014 +0400
@@ -0,0 +1,5 @@
+STATUS_LIBS = -L/usr/lib -liw
+STATUS_INCS = -std=gnu99
+STATUS_LDFLAGS = ${STATUS_LIBS}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/status-wifi/wifi.c	Thu Oct 23 17:55:13 2014 +0400
@@ -0,0 +1,109 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <limits.h>
+#include <unistd.h>
+#include <iwlib.h>
+
+char *eights[] = { "ø", "▁", "▂", "▃", "▄", "▅", "▆","▇", "█" };
+
+char *color_stop;
+char *color_red;
+char *color_orange;
+char *color_green;
+char *color_none;
+
+char *color(int low,int mid,int rev,int value) {
+    if (value>mid) {
+        return rev ? color_red : color_green;
+    } else if (value>low) {
+        return color_orange;
+    }
+    return rev ? color_green : color_red;
+}
+
+int main(int argc, char *argv[]) {
+    assert(argc>1);
+
+    int skfd;
+    if((skfd = iw_sockets_open()) < 0) {
+        perror("socket");
+        exit(-1);
+    }
+
+    while (1) {
+        if (argc>2 && !strcmp(argv[2],"term")) {
+            color_stop = "\x1b[0m";
+            color_red = "\x1b[1;31m";
+            color_green = "\x1b[1;32m";
+            color_orange = "\x1b[1;33m";
+            color_none = color_stop;
+        } else if (argc>2 && !strcmp(argv[2],"pango")) {
+            color_stop = "</span>";
+            color_red = "<span color='red'>";
+            color_green = "<span color='green'>";
+            color_orange = "<span color='orange'>";
+            color_none = "<span>";
+        } else {
+            color_stop = color_red = color_green = color_orange = color_none = "";
+        }
+
+        iwstats stats;
+        iwrange range;
+        int down = 0;
+        wireless_config info;
+    
+
+        if (iw_get_basic_config(skfd,argv[1],&info) < 0) {
+            perror("iw_get_basic_config");
+            exit(-2);
+        }
+
+        if (iw_get_range_info(skfd,argv[1],&range) < 0) {
+            perror("iw_get_range_info");
+            exit(-2);
+        }
+        if (iw_get_stats(skfd,argv[1],&stats,&range,1) < 0) {
+            if (argc>2)
+                down = 1;
+            else {
+                perror("iw_get_stats");
+                exit(-2);
+            }
+        }
+        if (down) {
+            if (!strcmp(info.essid,"")) {
+                printf("%sDOWN%s\n",color_orange,color_stop);
+            } else {
+                printf("%s [%sXX/XX%s] XX\n",info.essid,color_red,color_stop);
+            }
+        } else {
+            int ql = 100 * stats.qual.qual / range.max_qual.qual;
+            char *qual_color = color(20,40,0,ql);
+            if (stats.qual.qual<range.avg_qual.qual) qual_color = color_red;
+
+            signed char dbm;
+            dbm = (signed char)stats.qual.level;
+
+            int perc;
+            if (dbm>-35)
+                perc=100;
+            else if(dbm<=-95)
+                perc = 1;
+            else
+            perc = (dbm+95)*100/60;
+
+            printf("%s [%s%02d/%02d%s] ",info.essid,qual_color,stats.qual.qual,range.max_qual.qual,color_stop);
+            printf("%s%hhd%s ",color(-83,-77,0,dbm),dbm,color_stop);
+            printf("%s%d%%%s\n",color(20,40,0,perc),perc,color_stop);
+        }
+        fflush(stdout);
+        sleep(UPDATE_PERIOD);
+    }
+    close(skfd);
+}