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