Xinqi Bao's Git

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