X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fprint_wireless_info.c;h=b5e7ba26e9d6a9ecc63fc8c1fd2d25707b8f0baf;hb=794151cfe76f80fb2c7eebb8d3fbbce8fc5ccb09;hp=1aca346f3bdef1e9597405f1f99b78d45e90be53;hpb=342f942f15a79a2a149485c2c00810919aa7c927;p=i3%2Fi3status diff --git a/src/print_wireless_info.c b/src/print_wireless_info.c index 1aca346..b5e7ba2 100644 --- a/src/print_wireless_info.c +++ b/src/print_wireless_info.c @@ -1,6 +1,8 @@ // vim:ts=8:expandtab #include #include +#include +#include #ifdef LINUX #include @@ -23,6 +25,17 @@ #define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN #endif +#ifdef __OpenBSD__ +#include +#include +#include +#include +#include +#include +#include +#include +#endif + #include "i3status.h" #define WIRELESS_INFO_FLAG_HAS_ESSID (1 << 0) @@ -217,38 +230,105 @@ static int get_wireless_info(const char *interface, wireless_info_t *info) { } 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) { + if (nr.nr_max_rssi) + info->signal_level_max = IEEE80211_NODEREQ_RSSI(&nr); + else + info->signal_level = nr.nr_rssi; + + info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL; + } + } + } + + close(s); + return 1; #endif return 0; } -void print_wireless_info(const char *interface, const char *format_up, const char *format_down) { +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); + if (get_wireless_info(interface, &info)) { walk = format_up; if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) - printf("%s", info.quality < info.quality_average ? color("color_degraded") : color("color_good")); + START_COLOR((info.quality < info.quality_average ? "color_degraded" : "color_good")); } else { walk = format_down; - printf("%s", color("color_bad")); + START_COLOR("color_bad"); } for (; *walk != '\0'; walk++) { if (*walk != '%') { - putchar(*walk); + *(outwalk++) = *walk; continue; } if (BEGINS_WITH(walk+1, "quality")) { if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) { if (info.quality_max) - printf("%03d%%", PERCENT_VALUE(info.quality, info.quality_max)); + outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.quality, info.quality_max)); else - printf("%d", info.quality); - } - else { - printf("no value"); + outwalk += sprintf(outwalk, "%d", info.quality); + } else { + *(outwalk++) = '?'; } walk += strlen("quality"); } @@ -256,12 +336,11 @@ void print_wireless_info(const char *interface, const char *format_up, const cha if (BEGINS_WITH(walk+1, "signal")) { if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) { if (info.signal_level_max) - printf("%03d%%", PERCENT_VALUE(info.signal_level, info.signal_level_max)); + outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.signal_level, info.signal_level_max)); else - printf("%d dBm", info.signal_level); - } - else { - printf("no value"); + outwalk += sprintf(outwalk, "%d dBm", info.signal_level); + } else { + *(outwalk++) = '?'; } walk += strlen("signal"); } @@ -269,43 +348,41 @@ void print_wireless_info(const char *interface, const char *format_up, const cha if (BEGINS_WITH(walk+1, "noise")) { if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) { if (info.noise_level_max) - printf("%03d%%", PERCENT_VALUE(info.noise_level, info.noise_level_max)); + outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.noise_level, info.noise_level_max)); else - printf("%d dBm", info.noise_level); - } - else { - printf("no value"); + 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) - printf("%s", info.essid); + outwalk += sprintf(outwalk, "%s", info.essid); else - printf("no value"); + *(outwalk++) = '?'; walk += strlen("essid"); } if (BEGINS_WITH(walk+1, "ip")) { const char *ip_address = get_ip_addr(interface); - if (ip_address != NULL) - (void)printf("%s", get_ip_addr(interface)); - else (void)printf("no IP"); + outwalk += sprintf(outwalk, "%s", (ip_address ? ip_address : "no IP")); walk += strlen("ip"); } #ifdef LINUX if (BEGINS_WITH(walk+1, "bitrate")) { - char buffer[128]; + char br_buffer[128]; - iw_print_bitrate(buffer, sizeof(buffer), info.bitrate); + iw_print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate); - printf("%s", buffer); + outwalk += sprintf(outwalk, "%s", br_buffer); walk += strlen("bitrate"); } #endif } - (void)printf("%s", endcolor()); + END_COLOR; + OUTPUT_FULL_TEXT(buffer); }