#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>
#include "config.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[]) {
if (argc<=1) {
fprintf(stderr, "Usage: %s <wlan0> [term|pango]\n", argv[0]);
fflush(stderr);
return 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);
}