summaryrefslogtreecommitdiff
path: root/src/print_wireless_info.c
blob: 99b07f89d919fd1a41f36525f4958973e56c3d78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// vim:ts=4:sw=4:expandtab
#include <stdio.h>
#include <string.h>
#include <yajl/yajl_gen.h>
#include <yajl/yajl_version.h>

#ifdef LINUX
#include <iwlib.h>
#else
#ifndef __FreeBSD__
#define IW_ESSID_MAX_SIZE 32
#endif
#endif

#ifdef __FreeBSD__
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <net/if_media.h>
#include <net80211/ieee80211.h>
#include <net80211/ieee80211_ioctl.h>
#include <unistd.h>
#define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN
#endif

#ifdef __DragonFly__
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <net/if_media.h>
#include <netproto/802_11/ieee80211.h>
#include <netproto/802_11/ieee80211_ioctl.h>
#include <unistd.h>
#define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN
#endif

#ifdef __OpenBSD__
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <net80211/ieee80211.h>
#include <net80211/ieee80211_ioctl.h>
#endif

#include "i3status.h"

#define WIRELESS_INFO_FLAG_HAS_ESSID (1 << 0)
#define WIRELESS_INFO_FLAG_HAS_QUALITY (1 << 1)
#define WIRELESS_INFO_FLAG_HAS_SIGNAL (1 << 2)
#define WIRELESS_INFO_FLAG_HAS_NOISE (1 << 3)
#define WIRELESS_INFO_FLAG_HAS_FREQUENCY (1 << 4)

#define PERCENT_VALUE(value, total) ((int)(value * 100 / (float)total + 0.5f))

typedef struct {
    int flags;
    char essid[IW_ESSID_MAX_SIZE + 1];
    int quality;
    int quality_max;
    int quality_average;
    int signal_level;
    int signal_level_max;
    int noise_level;
    int noise_level_max;
    int bitrate;
    double frequency;
} wireless_info_t;

static int get_wireless_info(const char *interface, wireless_info_t *info) {
    memset(info, 0, sizeof(wireless_info_t));

#ifdef LINUX
    int skfd = iw_sockets_open();
    if (skfd < 0) {
        perror("iw_sockets_open");
        return 0;
    }

    wireless_config wcfg;
    if (iw_get_basic_config(skfd, interface, &wcfg) < 0) {
        close(skfd);
        return 0;
    }

    if (wcfg.has_essid && wcfg.essid_on) {
        info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
        strncpy(&info->essid[0], wcfg.essid, IW_ESSID_MAX_SIZE);
        info->essid[IW_ESSID_MAX_SIZE] = '\0';
    }

    if (wcfg.has_freq) {
        info->frequency = wcfg.freq;
        info->flags |= WIRELESS_INFO_FLAG_HAS_FREQUENCY;
    }

    /* If the function iw_get_stats does not return proper stats, the
     * wifi is considered as down.
     * Since ad-hoc network does not have theses stats, we need to return
     * here for this mode. */
    if (wcfg.mode == 1) {
        close(skfd);
        return 1;
    }

    /* Wireless quality is a relative value in a driver-specific range.
     * Signal and noise level can be either relative or absolute values
     * in dBm. Furthermore, noise and quality can be expressed directly
     * in dBm or in RCPI (802.11k), which we convert to dBm. When those
     * values are expressed directly in dBm, they range from -192 to 63,
     * and since the values are packed into 8 bits, we need to perform
     * 8-bit arithmetic on them. Assume absolute values if everything
     * else fails (driver bug). */

    iwrange range;
    if (iw_get_range_info(skfd, interface, &range) < 0) {
        close(skfd);
        return 0;
    }

    iwstats stats;
    if (iw_get_stats(skfd, interface, &stats, &range, 1) < 0) {
        close(skfd);
        return 0;
    }

    if (stats.qual.level != 0 || (stats.qual.updated & (IW_QUAL_DBM | IW_QUAL_RCPI))) {
        if (!(stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
            info->quality = stats.qual.qual;
            info->quality_max = range.max_qual.qual;
            info->quality_average = range.avg_qual.qual;
            info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
        }

        if (stats.qual.updated & IW_QUAL_RCPI) {
            if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
                info->signal_level = stats.qual.level / 2.0 - 110 + 0.5;
                info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
            }
            if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
                info->noise_level = stats.qual.noise / 2.0 - 110 + 0.5;
                info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
            }
        } else {
            if ((stats.qual.updated & IW_QUAL_DBM) || stats.qual.level > range.max_qual.level) {
                if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
                    info->signal_level = stats.qual.level;
                    if (info->signal_level > 63)
                        info->signal_level -= 256;
                    info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
                }
                if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
                    info->noise_level = stats.qual.noise;
                    if (info->noise_level > 63)
                        info->noise_level -= 256;
                    info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
                }
            } else {
                if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
                    info->signal_level = stats.qual.level;
                    info->signal_level_max = range.max_qual.level;
                    info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
                }
                if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
                    info->noise_level = stats.qual.noise;
                    info->noise_level_max = range.max_qual.noise;
                    info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
                }
            }
        }
    } else {
        if (!(stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
            info->quality = stats.qual.qual;
            info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
        }
        if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
            info->quality = stats.qual.level;
            info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
        }
        if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
            info->quality = stats.qual.noise;
            info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
        }
    }

    struct iwreq wrq;
    if (iw_get_ext(skfd, interface, SIOCGIWRATE, &wrq) >= 0)
        info->bitrate = wrq.u.bitrate.value;

    close(skfd);
    return 1;
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
    int s, inwid;
    union {
        struct ieee80211req_sta_req req;
        uint8_t buf[24 * 1024];
    } u;
    struct ieee80211req na;
    char bssid[IEEE80211_ADDR_LEN];
    size_t len;

    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
        return (0);

    memset(&na, 0, sizeof(na));
    strlcpy(na.i_name, interface, sizeof(na.i_name));
    na.i_type = IEEE80211_IOC_SSID;
    na.i_data = &info->essid[0];
    na.i_len = IEEE80211_NWID_LEN + 1;
    if ((inwid = ioctl(s, SIOCG80211, (caddr_t)&na)) == -1) {
        close(s);
        return (0);
    }
    if (inwid == 0) {
        if (na.i_len <= IEEE80211_NWID_LEN)
            len = na.i_len + 1;
        else
            len = IEEE80211_NWID_LEN + 1;
        info->essid[len - 1] = '\0';
    } else {
        close(s);
        return (0);
    }
    info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;

    memset(&na, 0, sizeof(na));
    strlcpy(na.i_name, interface, sizeof(na.i_name));
    na.i_type = IEEE80211_IOC_BSSID;
    na.i_data = bssid;
    na.i_len = sizeof(bssid);

    if (ioctl(s, SIOCG80211, (caddr_t)&na) == -1) {
        close(s);
        return (0);
    }

    memcpy(u.req.is_u.macaddr, bssid, sizeof(bssid));
    memset(&na, 0, sizeof(na));
    strlcpy(na.i_name, interface, sizeof(na.i_name));
    na.i_type = IEEE80211_IOC_STA_INFO;
    na.i_data = &u;
    na.i_len = sizeof(u);

    if (ioctl(s, SIOCG80211, (caddr_t)&na) == -1) {
        close(s);
        return (0);
    }

    close(s);
    if (na.i_len >= sizeof(u.req)) {
        /*
	 * Just use the first BSSID returned even if there are
	 * multiple APs sharing the same BSSID.
	 */
        info->signal_level = u.req.info[0].isi_rssi / 2 +
                             u.req.info[0].isi_noise;
        info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
        info->noise_level = u.req.info[0].isi_noise;
        info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
    }

    return 1;
#endif
#ifdef __OpenBSD__
    struct ifreq ifr;
    struct ieee80211_bssid bssid;
    struct ieee80211_nwid nwid;
    struct ieee80211_nodereq nr;

    struct ether_addr ea;

    int s, len, ibssid, inwid;
    u_int8_t zero_bssid[IEEE80211_ADDR_LEN];

    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
        return (0);

    memset(&ifr, 0, sizeof(ifr));
    ifr.ifr_data = (caddr_t)&nwid;
    (void)strlcpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
    inwid = ioctl(s, SIOCG80211NWID, (caddr_t)&ifr);

    memset(&bssid, 0, sizeof(bssid));
    strlcpy(bssid.i_name, interface, sizeof(bssid.i_name));
    ibssid = ioctl(s, SIOCG80211BSSID, &bssid);

    if (ibssid != 0 || inwid != 0) {
        close(s);
        return 0;
    }

    /* NWID */
    {
        if (nwid.i_len <= IEEE80211_NWID_LEN)
            len = nwid.i_len + 1;
        else
            len = IEEE80211_NWID_LEN + 1;

        strncpy(&info->essid[0], nwid.i_nwid, len);
        info->essid[IW_ESSID_MAX_SIZE] = '\0';
        info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
    }

    /* Signal strength */
    {
        memset(&zero_bssid, 0, sizeof(zero_bssid));
        if (ibssid == 0 && memcmp(bssid.i_bssid, zero_bssid, IEEE80211_ADDR_LEN) != 0) {
            memcpy(&ea.ether_addr_octet, bssid.i_bssid, sizeof(ea.ether_addr_octet));

            bzero(&nr, sizeof(nr));
            bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
            strlcpy(nr.nr_ifname, interface, sizeof(nr.nr_ifname));

            if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
                info->signal_level = nr.nr_rssi;
                if (nr.nr_max_rssi)
                    info->signal_level_max = nr.nr_max_rssi;

                info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
            }
        }
    }

    close(s);
    return 1;
#endif
    return 0;
}

void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
    const char *walk;
    char *outwalk = buffer;
    wireless_info_t info;

    INSTANCE(interface);

    const char *ip_address = get_ip_addr(interface);
    if (ip_address == NULL) {
        START_COLOR("color_bad");
        outwalk += sprintf(outwalk, "%s", format_down);
        goto out;
    }

    if (get_wireless_info(interface, &info)) {
        walk = format_up;
        if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY)
            START_COLOR((info.quality < info.quality_average ? "color_degraded" : "color_good"));
        else
            START_COLOR((BEGINS_WITH(ip_address, "no IP") ? "color_degraded" : "color_good"));
    } else {
        walk = format_down;
        START_COLOR("color_bad");
    }

    for (; *walk != '\0'; walk++) {
        if (*walk != '%') {
            *(outwalk++) = *walk;
            continue;
        }

        if (BEGINS_WITH(walk + 1, "quality")) {
            if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) {
                if (info.quality_max)
                    outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.quality, info.quality_max));
                else
                    outwalk += sprintf(outwalk, "%d", info.quality);
            } else {
                *(outwalk++) = '?';
            }
            walk += strlen("quality");
        }

        if (BEGINS_WITH(walk + 1, "signal")) {
            if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) {
                if (info.signal_level_max)
                    outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.signal_level, info.signal_level_max));
                else
                    outwalk += sprintf(outwalk, "%d dBm", info.signal_level);
            } else {
                *(outwalk++) = '?';
            }
            walk += strlen("signal");
        }

        if (BEGINS_WITH(walk + 1, "noise")) {
            if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) {
                if (info.noise_level_max)
                    outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.noise_level, info.noise_level_max));
                else
                    outwalk += sprintf(outwalk, "%d dBm", info.noise_level);
            } else {
                *(outwalk++) = '?';
            }
            walk += strlen("noise");
        }

        if (BEGINS_WITH(walk + 1, "essid")) {
            if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID)
                outwalk += sprintf(outwalk, "%s", info.essid);
            else
                *(outwalk++) = '?';
            walk += strlen("essid");
        }

        if (BEGINS_WITH(walk + 1, "frequency")) {
            if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY)
                outwalk += sprintf(outwalk, "%1.1f GHz", info.frequency / 1e9);
            else
                *(outwalk++) = '?';
            walk += strlen("frequency");
        }

        if (BEGINS_WITH(walk + 1, "ip")) {
            outwalk += sprintf(outwalk, "%s", ip_address);
            walk += strlen("ip");
        }

#ifdef LINUX
        if (BEGINS_WITH(walk + 1, "bitrate")) {
            char br_buffer[128];

            iw_print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate);

            outwalk += sprintf(outwalk, "%s", br_buffer);
            walk += strlen("bitrate");
        }
#endif
    }

out:
    END_COLOR;
    OUTPUT_FULL_TEXT(buffer);
}