From 28dfb0ba41f2d0f97b2f84de77e878c6c24fc29d Mon Sep 17 00:00:00 2001 From: Anthony PERARD Date: Wed, 26 May 2021 18:28:49 +0100 Subject: [PATCH 1/2] network: Fix use of carrier information Some RTM_NEWLINK messages may not have the IFLA_CARRIER information. This is the case when a WiFi interface report scan result are available. `carrier` is used regardless of if it is present in the message or not. This would result in the interface appearing "disconnected" in waybar when it isn't. This patch now check that `carrier` is available before using it. The same thing could potentially happen to `ifname` so check if it's set before recording it. Fixes: c1427ff (network: Handle carrier information) Fixes #388 --- src/modules/network.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 6102a43c..9e0ed395 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -425,7 +425,7 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) { struct rtattr *ifla = IFLA_RTA(ifi); const char *ifname = NULL; size_t ifname_len = 0; - bool carrier = false; + std::optional carrier; if (net->ifid_ != -1 && ifi->ifi_index != net->ifid_) { return NL_OK; @@ -446,11 +446,13 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) { if (!is_del_event && ifi->ifi_index == net->ifid_) { // Update inferface information - if (net->ifname_.empty()) { + if (net->ifname_.empty() && ifname != NULL) { std::string new_ifname (ifname, ifname_len); net->ifname_ = new_ifname; } - net->carrier_ = carrier; + if (carrier.has_value()) { + net->carrier_ = carrier.value(); + } } else if (!is_del_event && net->ifid_ == -1) { // Checking if it's an interface we care about. std::string new_ifname (ifname, ifname_len); @@ -459,7 +461,9 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) { net->ifname_ = new_ifname; net->ifid_ = ifi->ifi_index; - net->carrier_ = carrier; + if (carrier.has_value()) { + net->carrier_ = carrier.value(); + } net->thread_timer_.wake_up(); /* An address for this new interface should be received via an * RTM_NEWADDR event either because we ask for a dump of both links From f49a7a1acbe8489f37a88f5c8f7941a44bb90ac7 Mon Sep 17 00:00:00 2001 From: Anthony PERARD Date: Wed, 26 May 2021 18:51:02 +0100 Subject: [PATCH 2/2] network: Update WiFi information when available The module doesn't update the `essid_` as soon as a WiFi interface is connected, but that happens at some point later, depending on "interval" configuration. Fix that by rerunning the get WiFi information thread when the `carrier` state changes. Also, we will clear the state related to WiFi when the connection is drop to avoid stale information. --- src/modules/network.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 9e0ed395..583daaea 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -451,6 +451,18 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) { net->ifname_ = new_ifname; } if (carrier.has_value()) { + if (net->carrier_ != *carrier) { + if (*carrier) { + // Ask for WiFi information + net->thread_timer_.wake_up(); + } else { + // clear state related to WiFi connection + net->essid_.clear(); + net->signal_strength_dbm_ = 0; + net->signal_strength_ = 0; + net->frequency_ = 0; + } + } net->carrier_ = carrier.value(); } } else if (!is_del_event && net->ifid_ == -1) {