Xinqi Bao's Git

Clean up header includes
[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
113 memset(&bssid, 0, sizeof(bssid));
114 memset(nr, 0, sizeof(struct ieee80211_nodereq));
115 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
116 warn("socket 'AF_INET':");
117 return 0;
118 }
119 strlcpy(bssid.i_name, iface, sizeof(bssid.i_name));
120 if ((ioctl(sockfd, SIOCG80211BSSID, &bssid)) < 0) {
121 warn("ioctl 'SIOCG80211BSSID':");
122 close(sockfd);
123 return 0;
124 }
125 strlcpy(nr->nr_ifname, iface, sizeof(nr->nr_ifname));
126 memcpy(&nr->nr_macaddr, bssid.i_bssid, sizeof(nr->nr_macaddr));
127 if ((ioctl(sockfd, SIOCG80211NODE, nr)) < 0 && nr->nr_rssi) {
128 warn("ioctl 'SIOCG80211NODE':");
129 close(sockfd);
130 return 0;
131 }
132
133 return close(sockfd), 1;
134 }
135
136 const char *
137 wifi_perc(const char *iface)
138 {
139 struct ieee80211_nodereq nr;
140 int q;
141
142 if (load_ieee80211_nodereq(iface, &nr)) {
143 if (nr.nr_max_rssi) {
144 q = IEEE80211_NODEREQ_RSSI(&nr);
145 } else {
146 q = nr.nr_rssi >= -50 ? 100 : (nr.nr_rssi <= -100 ? 0 :
147 (2 * (nr.nr_rssi + 100)));
148 }
149 return bprintf("%d", q);
150 }
151
152 return NULL;
153 }
154
155 const char *
156 wifi_essid(const char *iface)
157 {
158 struct ieee80211_nodereq nr;
159
160 if (load_ieee80211_nodereq(iface, &nr)) {
161 return bprintf("%s", nr.nr_nwid);
162 }
163
164 return NULL;
165 }
166 #endif