diff options
Diffstat (limited to 'src/print_wireless_info.c')
-rw-r--r-- | src/print_wireless_info.c | 140 |
1 files changed, 73 insertions, 67 deletions
diff --git a/src/print_wireless_info.c b/src/print_wireless_info.c index 49421bb..cfb51b6 100644 --- a/src/print_wireless_info.c +++ b/src/print_wireless_info.c @@ -68,6 +68,8 @@ #include "i3status.h" +#define STRING_SIZE 30 + #define WIRELESS_INFO_FLAG_HAS_ESSID (1 << 0) #define WIRELESS_INFO_FLAG_HAS_QUALITY (1 << 1) #define WIRELESS_INFO_FLAG_HAS_SIGNAL (1 << 2) @@ -95,7 +97,7 @@ typedef struct { double frequency; } wireless_info_t; -#ifdef __linux__ +#if defined(__linux__) || defined(__FreeBSD__) // Like iw_print_bitrate, but without the dependency on libiw. static void print_bitrate(char *buffer, int buflen, int bitrate, const char *format_bitrate) { const int kilo = 1e3; @@ -118,7 +120,9 @@ static void print_bitrate(char *buffer, int buflen, int bitrate, const char *for } snprintf(buffer, buflen, format_bitrate, rate / divisor, scale); } +#endif +#ifdef __linux__ // Based on NetworkManager/src/platform/wifi/wifi-utils-nl80211.c static uint32_t nl80211_xbm_to_percent(int32_t xbm, int32_t divisor) { #define NOISE_FLOOR_DBM -90 @@ -406,6 +410,9 @@ error1: info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL; info->noise_level = u.req.info[0].isi_noise; info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE; + // isi_txmbps is specified in units of 500 Kbit/s + // Convert them to bit/s + info->bitrate = u.req.info[0].isi_txmbps * 500 * 1000; } return 1; @@ -517,7 +524,12 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, if (ipv6_address == NULL) { START_COLOR("color_bad"); outwalk += sprintf(outwalk, "%s", format_down); - goto out; + + END_COLOR; + free(ipv4_address); + free(ipv6_address); + OUTPUT_FULL_TEXT(buffer); + return; } else { prefer_ipv4 = false; } @@ -542,81 +554,75 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, } } - for (; *walk != '\0'; walk++) { - if (*walk != '%') { - *(outwalk++) = *walk; + char string_quality[STRING_SIZE]; + char string_signal[STRING_SIZE]; + char string_noise[STRING_SIZE]; + char string_essid[STRING_SIZE]; + char string_frequency[STRING_SIZE]; + char string_ip[STRING_SIZE]; + char string_bitrate[STRING_SIZE]; + + if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) { + if (info.quality_max) + snprintf(string_quality, STRING_SIZE, format_quality, PERCENT_VALUE(info.quality, info.quality_max), pct_mark); + else + snprintf(string_quality, STRING_SIZE, "%d", info.quality); + } else { + snprintf(string_quality, STRING_SIZE, "?"); + } - } else if (BEGINS_WITH(walk + 1, "quality")) { - if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) { - if (info.quality_max) - 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"); - - } else if (BEGINS_WITH(walk + 1, "signal")) { - if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) { - if (info.signal_level_max) - outwalk += sprintf(outwalk, format_signal, 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"); - - } else if (BEGINS_WITH(walk + 1, "noise")) { - if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) { - if (info.noise_level_max) - outwalk += sprintf(outwalk, format_noise, 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 (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) { + if (info.signal_level_max) + snprintf(string_signal, STRING_SIZE, format_signal, PERCENT_VALUE(info.signal_level, info.signal_level_max), pct_mark); + else + snprintf(string_signal, STRING_SIZE, "%d dBm", info.signal_level); + } else { + snprintf(string_signal, STRING_SIZE, "?"); + } + + if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) { + if (info.noise_level_max) + snprintf(string_noise, STRING_SIZE, format_noise, PERCENT_VALUE(info.noise_level, info.noise_level_max), pct_mark); + else + snprintf(string_noise, STRING_SIZE, "%d dBm", info.noise_level); + } else { + snprintf(string_noise, STRING_SIZE, "?"); + } - } else if (BEGINS_WITH(walk + 1, "essid")) { + char *tmp = string_essid; #ifdef IW_ESSID_MAX_SIZE - if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID) - maybe_escape_markup(info.essid, &outwalk); - else + if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID) + maybe_escape_markup(info.essid, &tmp); + else #endif - *(outwalk++) = '?'; - walk += strlen("essid"); - - } 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"); - - } else if (BEGINS_WITH(walk + 1, "ip")) { - outwalk += sprintf(outwalk, "%s", ip_address); - walk += strlen("ip"); - } -#ifdef __linux__ - else if (BEGINS_WITH(walk + 1, "bitrate")) { - char br_buffer[128]; + snprintf(string_essid, STRING_SIZE, "?"); - print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate, format_bitrate); + if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY) + snprintf(string_frequency, STRING_SIZE, "%1.1f GHz", info.frequency / 1e9); + else + snprintf(string_frequency, STRING_SIZE, "?"); - outwalk += sprintf(outwalk, "%s", br_buffer); - walk += strlen("bitrate"); - } + snprintf(string_ip, STRING_SIZE, "%s", ip_address); + +#if defined(__linux__) || defined(__FreeBSD__) + print_bitrate(string_bitrate, sizeof(string_bitrate), info.bitrate, format_bitrate); #endif - else { - *(outwalk++) = '%'; - } - } -out: + placeholder_t placeholders[] = { + {.name = "%quality", .value = string_quality}, + {.name = "%signal", .value = string_signal}, + {.name = "%noise", .value = string_noise}, + {.name = "%essid", .value = string_essid}, + {.name = "%frequency", .value = string_frequency}, + {.name = "%ip", .value = string_ip}, + {.name = "%bitrate", .value = string_bitrate}}; + + const size_t num = sizeof(placeholders) / sizeof(placeholder_t); + buffer = format_placeholders(walk, &placeholders[0], num); + END_COLOR; free(ipv4_address); free(ipv6_address); OUTPUT_FULL_TEXT(buffer); + free(buffer); } |