author | Stiletto <blasux@blasux.ru> |
Sat, 17 Aug 2019 15:26:16 +0400 | |
changeset 18 | 85165bbd4ebb |
parent 13 | d7ce3b1dddc4 |
permissions | -rw-r--r-- |
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'>"; |
|
18
85165bbd4ebb
[status-wifi] Support simplier display for Wi-Fi
Stiletto <blasux@blasux.ru>
parents:
13
diff
changeset
|
56 |
color_orange = "<span color='#fa0'>"; |
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,"")) { |
|
18
85165bbd4ebb
[status-wifi] Support simplier display for Wi-Fi
Stiletto <blasux@blasux.ru>
parents:
13
diff
changeset
|
87 |
printf("%sOFFLINE%s\n",color_orange,color_stop); |
4 | 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; |
|
18
85165bbd4ebb
[status-wifi] Support simplier display for Wi-Fi
Stiletto <blasux@blasux.ru>
parents:
13
diff
changeset
|
95 |
#ifdef SIMPLE_AS_FUCK |
85165bbd4ebb
[status-wifi] Support simplier display for Wi-Fi
Stiletto <blasux@blasux.ru>
parents:
13
diff
changeset
|
96 |
printf("%s%s%s %s\n",qual_color, eights[stats.qual.qual*8/range.max_qual.qual], color_stop, info.essid); |
85165bbd4ebb
[status-wifi] Support simplier display for Wi-Fi
Stiletto <blasux@blasux.ru>
parents:
13
diff
changeset
|
97 |
#else |
4 | 98 |
signed char dbm; |
99 |
dbm = (signed char)stats.qual.level; |
|
100 |
||
101 |
int perc; |
|
102 |
if (dbm>-35) |
|
103 |
perc=100; |
|
104 |
else if(dbm<=-95) |
|
105 |
perc = 1; |
|
106 |
else |
|
107 |
perc = (dbm+95)*100/60; |
|
108 |
||
109 |
printf("%s [%s%02d/%02d%s] ",info.essid,qual_color,stats.qual.qual,range.max_qual.qual,color_stop); |
|
110 |
printf("%s%hhd%s ",color(-83,-77,0,dbm),dbm,color_stop); |
|
111 |
printf("%s%d%%%s\n",color(20,40,0,perc),perc,color_stop); |
|
18
85165bbd4ebb
[status-wifi] Support simplier display for Wi-Fi
Stiletto <blasux@blasux.ru>
parents:
13
diff
changeset
|
112 |
#endif |
4 | 113 |
} |
114 |
fflush(stdout); |
|
115 |
sleep(UPDATE_PERIOD); |
|
116 |
} |
|
117 |
close(skfd); |
|
118 |
} |