Xinqi Bao's Git

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