X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fprint_wireless_info.c;h=d495b47eeb76dfa49fe347d841cabbf7bab3fb63;hb=52e9f6f63b74db2a6a1d67524851649b18794950;hp=14879ebf7cbccb5b077e040c52afb5b309f765e4;hpb=6e4ca31aa5febcc546ec3b32a09793085ea114f0;p=i3%2Fi3status diff --git a/src/print_wireless_info.c b/src/print_wireless_info.c index 14879eb..d495b47 100644 --- a/src/print_wireless_info.c +++ b/src/print_wireless_info.c @@ -16,6 +16,7 @@ #endif #ifdef __APPLE__ +#include #define IW_ESSID_MAX_SIZE 32 #endif @@ -49,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -75,7 +77,9 @@ typedef struct { int flags; +#ifdef IW_ESSID_MAX_SIZE char essid[IW_ESSID_MAX_SIZE + 1]; +#endif #ifdef LINUX uint8_t bssid[ETH_ALEN]; #endif @@ -461,96 +465,131 @@ error1: return 0; } -void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) { +/* Table summarizing what is the decision to prefer IPv4 or IPv6 + * based their values. + * + * | ipv4_address | ipv6_address | Chosen IP | Color | + * |--------------|--------------|-----------|-------------------| + * | NULL | NULL | None | bad (red) | + * | NULL | no IP | IPv6 | degraded (orange) | + * | NULL | ::1/128 | IPv6 | ok (green) | + * | no IP | NULL | IPv4 | degraded | + * | no IP | no IP | IPv4 | degraded | + * | no IP | ::1/128 | IPv6 | ok | + * | 127.0.0.1 | NULL | IPv4 | ok | + * | 127.0.0.1 | no IP | IPv4 | ok | + * | 127.0.0.1 | ::1/128 | IPv4 | ok | + */ +void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down, const char *format_quality) { 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; + char *ipv4_address = sstrdup(get_ip_addr(interface, AF_INET)); + char *ipv6_address = sstrdup(get_ip_addr(interface, AF_INET6)); + + /* + * Removing '%' and following characters from IPv6 since the interface identifier is redundant, + * as the output already includes the interface name. + */ + if (ipv6_address != NULL) { + char *prct_ptr = strstr(ipv6_address, "%"); + if (prct_ptr != NULL) { + *prct_ptr = '\0'; + } + } + + bool prefer_ipv4 = true; + if (ipv4_address == NULL) { + if (ipv6_address == NULL) { + START_COLOR("color_bad"); + outwalk += sprintf(outwalk, "%s", format_down); + goto out; + } else { + prefer_ipv4 = false; + } + } else if (BEGINS_WITH(ipv4_address, "no IP") && ipv6_address != NULL && !BEGINS_WITH(ipv6_address, "no IP")) { + prefer_ipv4 = false; } - if (get_wireless_info(interface, &info)) { + const char *ip_address = (prefer_ipv4) ? ipv4_address : ipv6_address; + if (!get_wireless_info(interface, &info)) { + walk = format_down; + START_COLOR("color_bad"); + } else { 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"); + else { + if (BEGINS_WITH(ip_address, "no IP")) { + START_COLOR("color_degraded"); + } else { + START_COLOR("color_good"); + } + } } for (; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "quality")) { + } else if (BEGINS_WITH(walk + 1, "quality")) { if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) { if (info.quality_max) - outwalk += sprintf(outwalk, "%03d%s", PERCENT_VALUE(info.quality, info.quality_max), pct_mark); + outwalk += sprintf(outwalk, format_quality, PERCENT_VALUE(info.quality, info.quality_max), pct_mark); else outwalk += sprintf(outwalk, "%d", info.quality); } else { *(outwalk++) = '?'; } walk += strlen("quality"); - } - if (BEGINS_WITH(walk + 1, "signal")) { + } else if (BEGINS_WITH(walk + 1, "signal")) { if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) { if (info.signal_level_max) - outwalk += sprintf(outwalk, "%03d%s", PERCENT_VALUE(info.signal_level, info.signal_level_max), pct_mark); + outwalk += sprintf(outwalk, "%3d%s", PERCENT_VALUE(info.signal_level, info.signal_level_max), pct_mark); else outwalk += sprintf(outwalk, "%d dBm", info.signal_level); } else { *(outwalk++) = '?'; } walk += strlen("signal"); - } - if (BEGINS_WITH(walk + 1, "noise")) { + } else if (BEGINS_WITH(walk + 1, "noise")) { if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) { if (info.noise_level_max) - outwalk += sprintf(outwalk, "%03d%s", PERCENT_VALUE(info.noise_level, info.noise_level_max), pct_mark); + outwalk += sprintf(outwalk, "%3d%s", PERCENT_VALUE(info.noise_level, info.noise_level_max), pct_mark); else outwalk += sprintf(outwalk, "%d dBm", info.noise_level); } else { *(outwalk++) = '?'; } walk += strlen("noise"); - } - if (BEGINS_WITH(walk + 1, "essid")) { + } else if (BEGINS_WITH(walk + 1, "essid")) { +#ifdef IW_ESSID_MAX_SIZE if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID) maybe_escape_markup(info.essid, &outwalk); else +#endif *(outwalk++) = '?'; walk += strlen("essid"); - } - if (BEGINS_WITH(walk + 1, "frequency")) { + } else 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")) { + } else if (BEGINS_WITH(walk + 1, "ip")) { outwalk += sprintf(outwalk, "%s", ip_address); walk += strlen("ip"); } - #ifdef LINUX - if (BEGINS_WITH(walk + 1, "bitrate")) { + else if (BEGINS_WITH(walk + 1, "bitrate")) { char br_buffer[128]; print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate); @@ -559,9 +598,14 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, walk += strlen("bitrate"); } #endif + else { + *(outwalk++) = '%'; + } } out: END_COLOR; + free(ipv4_address); + free(ipv6_address); OUTPUT_FULL_TEXT(buffer); }