Xinqi Bao's Git

wifi: fixed disconnected wifi status on openbsd
[slstatus.git] / components / wifi.c
1 /* See LICENSE file for copyright and license details. */
2 #include <ifaddrs.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/ioctl.h>
6 #include <sys/socket.h>
7 #include <unistd.h>
8
9 #include "../util.h"
10
11 #if defined(__linux__)
12 #include <limits.h>
13 #include <linux/wireless.h>
14
15 const char *
16 wifi_perc(const char *iface)
17 {
18 int i, cur;
19 int total = 70; /* the max of /proc/net/wireless */
20 char *p, *datastart;
21 char path[PATH_MAX];
22 char status[5];
23 FILE *fp;
24
25 if (esnprintf(path, sizeof(path),
26 "/sys/class/net/%s/operstate",
27 iface) < 0) {
28 return NULL;
29 }
30 if (!(fp = fopen(path, "r"))) {
31 warn("fopen '%s':", path);
32 return NULL;
33 }
34 p = fgets(status, 5, fp);
35 fclose(fp);
36 if(!p || strcmp(status, "up\n") != 0) {
37 return NULL;
38 }
39
40 if (!(fp = fopen("/proc/net/wireless", "r"))) {
41 warn("fopen '/proc/net/wireless':");
42 return NULL;
43 }
44
45 for (i = 0; i < 3; i++) {
46 if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
47 break;
48 }
49 fclose(fp);
50 if (i < 2 || !p) {
51 return NULL;
52 }
53
54 if (!(datastart = strstr(buf, iface))) {
55 return NULL;
56 }
57
58 datastart = (datastart+(strlen(iface)+1));
59 sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t "
60 "%*d\t\t%*d\t\t %*d\t %*d\t\t %*d", &cur);
61
62 return bprintf("%d", (int)((float)cur / total * 100));
63 }
64
65 const char *
66 wifi_essid(const char *iface)
67 {
68 static char id[IW_ESSID_MAX_SIZE+1];
69 int sockfd;
70 struct iwreq wreq;
71
72 memset(&wreq, 0, sizeof(struct iwreq));
73 wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
74 if (esnprintf(wreq.ifr_name, sizeof(wreq.ifr_name),
75 "%s", iface) < 0) {
76 return NULL;
77 }
78
79 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
80 warn("socket 'AF_INET':");
81 return NULL;
82 }
83 wreq.u.essid.pointer = id;
84 if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) {
85 warn("ioctl 'SIOCGIWESSID':");
86 close(sockfd);
87 return NULL;
88 }
89
90 close(sockfd);
91
92 if (!strcmp(id, "")) {
93 return NULL;
94 }
95
96 return id;
97 }
98 #elif defined(__OpenBSD__)
99 #include <net/if.h>
100 #include <net/if_media.h>
101 #include <net80211/ieee80211.h>
102 #include <sys/select.h> /* before <sys/ieee80211_ioctl.h> for NBBY */
103 #include <net80211/ieee80211_ioctl.h>
104 #include <stdlib.h>
105 #include <sys/types.h>
106
107 static int
108 load_ieee80211_nodereq(const char *iface, struct ieee80211_nodereq *nr)
109 {
110 struct ieee80211_bssid bssid;
111 int sockfd;
112 uint8_t zero_bssid[IEEE80211_ADDR_LEN];
113
114 memset(&bssid, 0, sizeof(bssid));
115 memset(nr, 0, sizeof(struct ieee80211_nodereq));
116 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
117 warn("socket 'AF_INET':");
118 return 0;
119 }
120 strlcpy(bssid.i_name, iface, sizeof(bssid.i_name));
121 if ((ioctl(sockfd, SIOCG80211BSSID, &bssid)) < 0) {
122 warn("ioctl 'SIOCG80211BSSID':");
123 close(sockfd);
124 return 0;
125 }
126 memset(&zero_bssid, 0, sizeof(zero_bssid));
127 if (memcmp(bssid.i_bssid, zero_bssid,
128 IEEE80211_ADDR_LEN) == 0) {
129 close(sockfd);
130 return 0;
131 }
132 strlcpy(nr->nr_ifname, iface, sizeof(nr->nr_ifname));
133 memcpy(&nr->nr_macaddr, bssid.i_bssid, sizeof(nr->nr_macaddr));
134 if ((ioctl(sockfd, SIOCG80211NODE, nr)) < 0 && nr->nr_rssi) {
135 warn("ioctl 'SIOCG80211NODE':");
136 close(sockfd);
137 return 0;
138 }
139
140 return close(sockfd), 1;
141 }
142
143 const char *
144 wifi_perc(const char *iface)
145 {
146 struct ieee80211_nodereq nr;
147 int q;
148
149 if (load_ieee80211_nodereq(iface, &nr)) {
150 if (nr.nr_max_rssi) {
151 q = IEEE80211_NODEREQ_RSSI(&nr);
152 } else {
153 q = nr.nr_rssi >= -50 ? 100 : (nr.nr_rssi <= -100 ? 0 :
154 (2 * (nr.nr_rssi + 100)));
155 }
156 return bprintf("%d", q);
157 }
158
159 return NULL;
160 }
161
162 const char *
163 wifi_essid(const char *iface)
164 {
165 struct ieee80211_nodereq nr;
166
167 if (load_ieee80211_nodereq(iface, &nr)) {
168 return bprintf("%s", nr.nr_nwid);
169 }
170
171 return NULL;
172 }
173 #endif