add wireless format-options

This commit is contained in:
Felix Buehler 2020-02-10 22:33:20 +01:00
parent eccd4a7618
commit 42e7147291
4 changed files with 19 additions and 9 deletions

View File

@ -338,7 +338,10 @@ int main(int argc, char *argv[]) {
cfg_opt_t wireless_opts[] = { cfg_opt_t wireless_opts[] = {
CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE), CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
CFG_STR("format_down", "W: down", CFGF_NONE), CFG_STR("format_down", "W: down", CFGF_NONE),
CFG_STR("format_bitrate", "%g %cb/s", CFGF_NONE),
CFG_STR("format_noise", "%3d%s", CFGF_NONE),
CFG_STR("format_quality", "%3d%s", CFGF_NONE), CFG_STR("format_quality", "%3d%s", CFGF_NONE),
CFG_STR("format_signal", "%3d%s", CFGF_NONE),
CFG_CUSTOM_ALIGN_OPT, CFG_CUSTOM_ALIGN_OPT,
CFG_CUSTOM_COLOR_OPTS, CFG_CUSTOM_COLOR_OPTS,
CFG_CUSTOM_MIN_WIDTH_OPT, CFG_CUSTOM_MIN_WIDTH_OPT,
@ -732,7 +735,7 @@ int main(int argc, char *argv[]) {
interface = first_eth_interface(NET_TYPE_WIRELESS); interface = first_eth_interface(NET_TYPE_WIRELESS);
if (interface == NULL) if (interface == NULL)
interface = title; interface = title;
print_wireless_info(json_gen, buffer, interface, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"), cfg_getstr(sec, "format_quality")); print_wireless_info(json_gen, buffer, interface, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"), cfg_getstr(sec, "format_bitrate"), cfg_getstr(sec, "format_noise"), cfg_getstr(sec, "format_quality"), cfg_getstr(sec, "format_signal"));
SEC_CLOSE_MAP; SEC_CLOSE_MAP;
} }

View File

@ -218,7 +218,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *locale, const char *format_time, bool hide_if_equals_localtime, time_t t); void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *locale, const char *format_time, bool hide_if_equals_localtime, time_t t);
void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t); void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
const char *get_ip_addr(const char *interface, int family); const char *get_ip_addr(const char *interface, int family);
void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down, const char *quality_min_lenght); void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down, const char *format_bitrate, const char *format_noise, const char *format_quality, const char *format_signal);
void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format, const char *format_down); void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format, const char *format_down);
void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down); void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down);
void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, const char *format_above_threshold, int); void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, const char *format_above_threshold, int);

View File

@ -306,7 +306,8 @@ There also is an option "format_down". You can hide the output with
Gets the link quality, frequency and ESSID of the given wireless network Gets the link quality, frequency and ESSID of the given wireless network
interface. You can specify different format strings for the network being interface. You can specify different format strings for the network being
connected or not connected. The quality is padded with leading zeroes by connected or not connected. The quality is padded with leading zeroes by
default; to pad with something else use +format_quality+. default; to pad with something else use +format_bitrate+, +format_noise+,
+format_quality+ or +format_signal+.
The special interface name `_first_` will be replaced by the first wireless The special interface name `_first_` will be replaced by the first wireless
network interface found on the system (excluding devices starting with "lo"). network interface found on the system (excluding devices starting with "lo").
@ -317,8 +318,14 @@ network interface found on the system (excluding devices starting with "lo").
*Example format_down*: +W: down+ *Example format_down*: +W: down+
*Example format_bitrate*: +"%g %cb/s"+
*Example format_noise*: +"%03d%s"+
*Example format_quality*: +"%03d%s"+ *Example format_quality*: +"%03d%s"+
*Example format_signal*: +"%03d%s"+
=== Ethernet === Ethernet
Gets the IP address and (if possible) the link speed of the given ethernet Gets the IP address and (if possible) the link speed of the given ethernet

View File

@ -97,7 +97,7 @@ typedef struct {
#ifdef __linux__ #ifdef __linux__
// Like iw_print_bitrate, but without the dependency on libiw. // Like iw_print_bitrate, but without the dependency on libiw.
static void print_bitrate(char *buffer, int buflen, int bitrate) { static void print_bitrate(char *buffer, int buflen, int bitrate, const char *format_bitrate) {
const int kilo = 1e3; const int kilo = 1e3;
const int mega = 1e6; const int mega = 1e6;
const int giga = 1e9; const int giga = 1e9;
@ -116,7 +116,7 @@ static void print_bitrate(char *buffer, int buflen, int bitrate) {
scale = 'k'; scale = 'k';
divisor = kilo; divisor = kilo;
} }
snprintf(buffer, buflen, "%g %cb/s", rate / divisor, scale); snprintf(buffer, buflen, format_bitrate, rate / divisor, scale);
} }
// Based on NetworkManager/src/platform/wifi/wifi-utils-nl80211.c // Based on NetworkManager/src/platform/wifi/wifi-utils-nl80211.c
@ -491,7 +491,7 @@ error1:
* | 127.0.0.1 | no IP | IPv4 | ok | * | 127.0.0.1 | no IP | IPv4 | ok |
* | 127.0.0.1 | ::1/128 | 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) { void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down, const char *format_bitrate, const char *format_noise, const char *format_quality, const char *format_signal) {
const char *walk; const char *walk;
char *outwalk = buffer; char *outwalk = buffer;
wireless_info_t info; wireless_info_t info;
@ -560,7 +560,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
} else if (BEGINS_WITH(walk + 1, "signal")) { } else if (BEGINS_WITH(walk + 1, "signal")) {
if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) { if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) {
if (info.signal_level_max) if (info.signal_level_max)
outwalk += sprintf(outwalk, "%3d%s", PERCENT_VALUE(info.signal_level, info.signal_level_max), pct_mark); outwalk += sprintf(outwalk, format_signal, PERCENT_VALUE(info.signal_level, info.signal_level_max), pct_mark);
else else
outwalk += sprintf(outwalk, "%d dBm", info.signal_level); outwalk += sprintf(outwalk, "%d dBm", info.signal_level);
} else { } else {
@ -571,7 +571,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
} else if (BEGINS_WITH(walk + 1, "noise")) { } else if (BEGINS_WITH(walk + 1, "noise")) {
if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) { if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) {
if (info.noise_level_max) if (info.noise_level_max)
outwalk += sprintf(outwalk, "%3d%s", PERCENT_VALUE(info.noise_level, info.noise_level_max), pct_mark); outwalk += sprintf(outwalk, format_noise, PERCENT_VALUE(info.noise_level, info.noise_level_max), pct_mark);
else else
outwalk += sprintf(outwalk, "%d dBm", info.noise_level); outwalk += sprintf(outwalk, "%d dBm", info.noise_level);
} else { } else {
@ -603,7 +603,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
else if (BEGINS_WITH(walk + 1, "bitrate")) { else if (BEGINS_WITH(walk + 1, "bitrate")) {
char br_buffer[128]; char br_buffer[128];
print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate); print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate, format_bitrate);
outwalk += sprintf(outwalk, "%s", br_buffer); outwalk += sprintf(outwalk, "%s", br_buffer);
walk += strlen("bitrate"); walk += strlen("bitrate");