1 // vim:ts=4:sw=4:expandtab
4 #include <yajl/yajl_gen.h>
5 #include <yajl/yajl_version.h>
11 #define IW_ESSID_MAX_SIZE 32
16 #include <sys/param.h>
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
21 #include <net/if_media.h>
22 #include <net80211/ieee80211.h>
23 #include <net80211/ieee80211_ioctl.h>
25 #define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN
29 #include <sys/param.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
34 #include <net/if_media.h>
35 #include <netproto/802_11/ieee80211.h>
36 #include <netproto/802_11/ieee80211_ioctl.h>
38 #define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN
42 #include <sys/ioctl.h>
43 #include <sys/socket.h>
45 #include <sys/types.h>
46 #include <netinet/in.h>
47 #include <netinet/if_ether.h>
48 #include <net80211/ieee80211.h>
49 #include <net80211/ieee80211_ioctl.h>
54 #define WIRELESS_INFO_FLAG_HAS_ESSID (1 << 0)
55 #define WIRELESS_INFO_FLAG_HAS_QUALITY (1 << 1)
56 #define WIRELESS_INFO_FLAG_HAS_SIGNAL (1 << 2)
57 #define WIRELESS_INFO_FLAG_HAS_NOISE (1 << 3)
58 #define WIRELESS_INFO_FLAG_HAS_FREQUENCY (1 << 4)
60 #define PERCENT_VALUE(value, total) ((int)(value * 100 / (float)total + 0.5f))
64 char essid[IW_ESSID_MAX_SIZE + 1];
76 static int get_wireless_info(const char *interface, wireless_info_t *info) {
77 memset(info, 0, sizeof(wireless_info_t));
80 int skfd = iw_sockets_open();
82 perror("iw_sockets_open");
87 if (iw_get_basic_config(skfd, interface, &wcfg) < 0) {
92 if (wcfg.has_essid && wcfg.essid_on) {
93 info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
94 strncpy(&info->essid[0], wcfg.essid, IW_ESSID_MAX_SIZE);
95 info->essid[IW_ESSID_MAX_SIZE] = '\0';
99 info->frequency = wcfg.freq;
100 info->flags |= WIRELESS_INFO_FLAG_HAS_FREQUENCY;
103 /* If the function iw_get_stats does not return proper stats, the
104 * wifi is considered as down.
105 * Since ad-hoc network does not have theses stats, we need to return
106 * here for this mode. */
107 if (wcfg.mode == 1) {
112 /* Wireless quality is a relative value in a driver-specific range.
113 * Signal and noise level can be either relative or absolute values
114 * in dBm. Furthermore, noise and quality can be expressed directly
115 * in dBm or in RCPI (802.11k), which we convert to dBm. When those
116 * values are expressed directly in dBm, they range from -192 to 63,
117 * and since the values are packed into 8 bits, we need to perform
118 * 8-bit arithmetic on them. Assume absolute values if everything
119 * else fails (driver bug). */
122 if (iw_get_range_info(skfd, interface, &range) < 0) {
128 if (iw_get_stats(skfd, interface, &stats, &range, 1) < 0) {
133 if (stats.qual.level != 0 || (stats.qual.updated & (IW_QUAL_DBM | IW_QUAL_RCPI))) {
134 if (!(stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
135 info->quality = stats.qual.qual;
136 info->quality_max = range.max_qual.qual;
137 info->quality_average = range.avg_qual.qual;
138 info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
141 if (stats.qual.updated & IW_QUAL_RCPI) {
142 if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
143 info->signal_level = stats.qual.level / 2.0 - 110 + 0.5;
144 info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
146 if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
147 info->noise_level = stats.qual.noise / 2.0 - 110 + 0.5;
148 info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
151 if ((stats.qual.updated & IW_QUAL_DBM) || stats.qual.level > range.max_qual.level) {
152 if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
153 info->signal_level = stats.qual.level;
154 if (info->signal_level > 63)
155 info->signal_level -= 256;
156 info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
158 if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
159 info->noise_level = stats.qual.noise;
160 if (info->noise_level > 63)
161 info->noise_level -= 256;
162 info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
165 if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
166 info->signal_level = stats.qual.level;
167 info->signal_level_max = range.max_qual.level;
168 info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
170 if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
171 info->noise_level = stats.qual.noise;
172 info->noise_level_max = range.max_qual.noise;
173 info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
178 if (!(stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
179 info->quality = stats.qual.qual;
180 info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
182 if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
183 info->quality = stats.qual.level;
184 info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
186 if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
187 info->quality = stats.qual.noise;
188 info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
193 if (iw_get_ext(skfd, interface, SIOCGIWRATE, &wrq) >= 0)
194 info->bitrate = wrq.u.bitrate.value;
199 #if defined(__FreeBSD__) || defined(__DragonFly__)
201 uint8_t buf[24 * 1024], *cp;
202 struct ieee80211req na;
203 char network_id[IEEE80211_NWID_LEN + 1];
205 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
208 memset(&na, 0, sizeof(na));
209 strlcpy(na.i_name, interface, sizeof(na.i_name));
210 na.i_type = IEEE80211_IOC_SSID;
211 na.i_data = &info->essid[0];
212 na.i_len = IEEE80211_NWID_LEN + 1;
213 if ((inwid = ioctl(s, SIOCG80211, (caddr_t)&na)) == -1) {
218 if (na.i_len <= IEEE80211_NWID_LEN)
221 len = IEEE80211_NWID_LEN + 1;
222 info->essid[len - 1] = '\0';
227 info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
229 memset(&na, 0, sizeof(na));
230 strlcpy(na.i_name, interface, sizeof(na.i_name));
231 na.i_type = IEEE80211_IOC_SCAN_RESULTS;
233 na.i_len = sizeof(buf);
235 if (ioctl(s, SIOCG80211, (caddr_t)&na) == -1) {
244 struct ieee80211req_scan_result *sr;
246 sr = (struct ieee80211req_scan_result *)cp;
247 vp = (u_int8_t *)(sr + 1);
248 strlcpy(network_id, (const char *)vp, sr->isr_ssid_len + 1);
249 if (!strcmp(network_id, &info->essid[0])) {
250 info->signal_level = sr->isr_rssi;
251 info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
252 info->noise_level = sr->isr_noise;
253 info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
254 info->quality = sr->isr_intval;
255 info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
262 struct ieee80211_bssid bssid;
263 struct ieee80211_nwid nwid;
264 struct ieee80211_nodereq nr;
266 struct ether_addr ea;
268 int s, len, ibssid, inwid;
269 u_int8_t zero_bssid[IEEE80211_ADDR_LEN];
271 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
274 memset(&ifr, 0, sizeof(ifr));
275 ifr.ifr_data = (caddr_t)&nwid;
276 (void)strlcpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
277 inwid = ioctl(s, SIOCG80211NWID, (caddr_t)&ifr);
279 memset(&bssid, 0, sizeof(bssid));
280 strlcpy(bssid.i_name, interface, sizeof(bssid.i_name));
281 ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
283 if (ibssid != 0 || inwid != 0) {
290 if (nwid.i_len <= IEEE80211_NWID_LEN)
291 len = nwid.i_len + 1;
293 len = IEEE80211_NWID_LEN + 1;
295 strncpy(&info->essid[0], nwid.i_nwid, len);
296 info->essid[IW_ESSID_MAX_SIZE] = '\0';
297 info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
300 /* Signal strength */
302 memset(&zero_bssid, 0, sizeof(zero_bssid));
303 if (ibssid == 0 && memcmp(bssid.i_bssid, zero_bssid, IEEE80211_ADDR_LEN) != 0) {
304 memcpy(&ea.ether_addr_octet, bssid.i_bssid, sizeof(ea.ether_addr_octet));
306 bzero(&nr, sizeof(nr));
307 bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
308 strlcpy(nr.nr_ifname, interface, sizeof(nr.nr_ifname));
310 if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
311 info->signal_level = nr.nr_rssi;
313 info->signal_level_max = nr.nr_max_rssi;
315 info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
326 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
328 char *outwalk = buffer;
329 wireless_info_t info;
333 const char *ip_address = get_ip_addr(interface);
334 if (ip_address == NULL) {
335 START_COLOR("color_bad");
336 outwalk += sprintf(outwalk, "%s", format_down);
340 if (get_wireless_info(interface, &info)) {
342 if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY)
343 START_COLOR((info.quality < info.quality_average ? "color_degraded" : "color_good"));
345 START_COLOR("color_good");
348 START_COLOR("color_bad");
351 for (; *walk != '\0'; walk++) {
353 *(outwalk++) = *walk;
357 if (BEGINS_WITH(walk + 1, "quality")) {
358 if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) {
359 if (info.quality_max)
360 outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.quality, info.quality_max));
362 outwalk += sprintf(outwalk, "%d", info.quality);
366 walk += strlen("quality");
369 if (BEGINS_WITH(walk + 1, "signal")) {
370 if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) {
371 if (info.signal_level_max)
372 outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.signal_level, info.signal_level_max));
374 outwalk += sprintf(outwalk, "%d dBm", info.signal_level);
378 walk += strlen("signal");
381 if (BEGINS_WITH(walk + 1, "noise")) {
382 if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) {
383 if (info.noise_level_max)
384 outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.noise_level, info.noise_level_max));
386 outwalk += sprintf(outwalk, "%d dBm", info.noise_level);
390 walk += strlen("noise");
393 if (BEGINS_WITH(walk + 1, "essid")) {
394 if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID)
395 outwalk += sprintf(outwalk, "%s", info.essid);
398 walk += strlen("essid");
401 if (BEGINS_WITH(walk + 1, "frequency")) {
402 if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY)
403 outwalk += sprintf(outwalk, "%1.1f GHz", info.frequency / 1e9);
406 walk += strlen("frequency");
409 if (BEGINS_WITH(walk + 1, "ip")) {
410 outwalk += sprintf(outwalk, "%s", ip_address);
411 walk += strlen("ip");
415 if (BEGINS_WITH(walk + 1, "bitrate")) {
418 iw_print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate);
420 outwalk += sprintf(outwalk, "%s", br_buffer);
421 walk += strlen("bitrate");
428 OUTPUT_FULL_TEXT(buffer);