4
|
1 |
#include <stdio.h>
|
|
2 |
#include <stdlib.h>
|
|
3 |
#include <math.h>
|
|
4 |
|
|
5 |
#include <sys/types.h>
|
|
6 |
#include <sys/stat.h>
|
|
7 |
#include <fcntl.h>
|
|
8 |
#include <assert.h>
|
|
9 |
#include <limits.h>
|
|
10 |
#include <unistd.h>
|
|
11 |
#include <iwlib.h>
|
|
12 |
|
5
|
13 |
#include "config.h"
|
|
14 |
|
4
|
15 |
char *eights[] = { "ø", "▁", "▂", "▃", "▄", "▅", "▆","▇", "█" };
|
|
16 |
|
|
17 |
char *color_stop;
|
|
18 |
char *color_red;
|
|
19 |
char *color_orange;
|
|
20 |
char *color_green;
|
|
21 |
char *color_none;
|
|
22 |
|
|
23 |
char *color(int low,int mid,int rev,int value) {
|
|
24 |
if (value>mid) {
|
|
25 |
return rev ? color_red : color_green;
|
|
26 |
} else if (value>low) {
|
|
27 |
return color_orange;
|
|
28 |
}
|
|
29 |
return rev ? color_green : color_red;
|
|
30 |
}
|
|
31 |
|
|
32 |
int main(int argc, char *argv[]) {
|
5
|
33 |
if (argc<=1) {
|
|
34 |
fprintf(stderr, "Usage: %s <wlan0> [term|pango]\n", argv[0]);
|
|
35 |
fflush(stderr);
|
|
36 |
return 1;
|
|
37 |
}
|
4
|
38 |
|
|
39 |
int skfd;
|
|
40 |
if((skfd = iw_sockets_open()) < 0) {
|
|
41 |
perror("socket");
|
|
42 |
exit(-1);
|
|
43 |
}
|
|
44 |
|
|
45 |
while (1) {
|
|
46 |
if (argc>2 && !strcmp(argv[2],"term")) {
|
|
47 |
color_stop = "\x1b[0m";
|
|
48 |
color_red = "\x1b[1;31m";
|
|
49 |
color_green = "\x1b[1;32m";
|
|
50 |
color_orange = "\x1b[1;33m";
|
|
51 |
color_none = color_stop;
|
|
52 |
} else if (argc>2 && !strcmp(argv[2],"pango")) {
|
|
53 |
color_stop = "</span>";
|
13
|
54 |
color_red = "<span color='#f33'>";
|
|
55 |
color_green = "<span color='#0f0'>";
|
|
56 |
color_orange = "<span color='#f30'>";
|
4
|
57 |
color_none = "<span>";
|
|
58 |
} else {
|
|
59 |
color_stop = color_red = color_green = color_orange = color_none = "";
|
|
60 |
}
|
|
61 |
|
|
62 |
iwstats stats;
|
|
63 |
iwrange range;
|
|
64 |
int down = 0;
|
|
65 |
wireless_config info;
|
|
66 |
|
|
67 |
|
|
68 |
if (iw_get_basic_config(skfd,argv[1],&info) < 0) {
|
|
69 |
perror("iw_get_basic_config");
|
|
70 |
exit(-2);
|
|
71 |
}
|
|
72 |
|
|
73 |
if (iw_get_range_info(skfd,argv[1],&range) < 0) {
|
|
74 |
perror("iw_get_range_info");
|
|
75 |
exit(-2);
|
|
76 |
}
|
|
77 |
if (iw_get_stats(skfd,argv[1],&stats,&range,1) < 0) {
|
|
78 |
if (argc>2)
|
|
79 |
down = 1;
|
|
80 |
else {
|
|
81 |
perror("iw_get_stats");
|
|
82 |
exit(-2);
|
|
83 |
}
|
|
84 |
}
|
|
85 |
if (down) {
|
|
86 |
if (!strcmp(info.essid,"")) {
|
|
87 |
printf("%sDOWN%s\n",color_orange,color_stop);
|
|
88 |
} else {
|
|
89 |
printf("%s [%sXX/XX%s] XX\n",info.essid,color_red,color_stop);
|
|
90 |
}
|
|
91 |
} else {
|
|
92 |
int ql = 100 * stats.qual.qual / range.max_qual.qual;
|
|
93 |
char *qual_color = color(20,40,0,ql);
|
|
94 |
if (stats.qual.qual<range.avg_qual.qual) qual_color = color_red;
|
|
95 |
|
|
96 |
signed char dbm;
|
|
97 |
dbm = (signed char)stats.qual.level;
|
|
98 |
|
|
99 |
int perc;
|
|
100 |
if (dbm>-35)
|
|
101 |
perc=100;
|
|
102 |
else if(dbm<=-95)
|
|
103 |
perc = 1;
|
|
104 |
else
|
|
105 |
perc = (dbm+95)*100/60;
|
|
106 |
|
|
107 |
printf("%s [%s%02d/%02d%s] ",info.essid,qual_color,stats.qual.qual,range.max_qual.qual,color_stop);
|
|
108 |
printf("%s%hhd%s ",color(-83,-77,0,dbm),dbm,color_stop);
|
|
109 |
printf("%s%d%%%s\n",color(20,40,0,perc),perc,color_stop);
|
|
110 |
}
|
|
111 |
fflush(stdout);
|
|
112 |
sleep(UPDATE_PERIOD);
|
|
113 |
}
|
|
114 |
close(skfd);
|
|
115 |
}
|