Xinqi Bao's Git

wifi: Fix order and add missing header
[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 snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface,
27 "/operstate");
28 if (!(fp = fopen(path, "r"))) {
29 fprintf(stderr, "fopen '%s': %s\n", path,
30 strerror(errno));
31 return NULL;
32 }
33 p = fgets(status, 5, fp);
34 fclose(fp);
35 if(!p || strcmp(status, "up\n") != 0) {
36 return NULL;
37 }
38
39 if (!(fp = fopen("/proc/net/wireless", "r"))) {
40 fprintf(stderr, "fopen '/proc/net/wireless': %s\n",
41 strerror(errno));
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 snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
75
76 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
77 fprintf(stderr, "socket 'AF_INET': %s\n",
78 strerror(errno));
79 return NULL;
80 }
81 wreq.u.essid.pointer = id;
82 if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) {
83 fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno));
84 close(sockfd);
85 return NULL;
86 }
87
88 close(sockfd);
89
90 if (!strcmp(id, "")) {
91 return NULL;
92 }
93
94 return id;
95 }
96 #elif defined(__OpenBSD__)
97 #include <net/if.h>
98 #include <net/if_media.h>
99 #include <net80211/ieee80211.h>
100 #include <sys/select.h> /* before <sys/ieee80211_ioctl.h> for NBBY */
101 #include <net80211/ieee80211_ioctl.h>
102 #include <stdlib.h>
103 #include <sys/types.h>
104
105 static int
106 load_ieee80211_nodereq(const char *iface, struct ieee80211_nodereq *nr)
107 {
108 struct ieee80211_bssid bssid;
109 int sockfd;
110
111 memset(&bssid, 0, sizeof(bssid));
112 memset(nr, 0, sizeof(struct ieee80211_nodereq));
113 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
114 fprintf(stderr, "socket 'AF_INET': %s\n",
115 strerror(errno));
116 return 0;
117 }
118 strlcpy(bssid.i_name, iface, sizeof(bssid.i_name));
119 if ((ioctl(sockfd, SIOCG80211BSSID, &bssid)) < 0) {
120 fprintf(stderr, "ioctl 'SIOCG80211BSSID': %s\n",
121 strerror(errno));
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 fprintf(stderr, "ioctl 'SIOCG80211NODE': %s\n",
129 strerror(errno));
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