network: Handle carrier information

IFLA_CARRIER allows to know when a cable is plugged to the Ethernet
card or when the WiFi is connected. If there's no carrier, the
interface will be considered disconnected.
pull/1106/head
Anthony PERARD 2021-05-15 16:38:00 +01:00
parent 0bb436f949
commit c1427ff807
2 changed files with 10 additions and 0 deletions

View File

@ -64,6 +64,7 @@ class Network : public ALabel {
std::string state_; std::string state_;
std::string essid_; std::string essid_;
bool carrier_;
std::string ifname_; std::string ifname_;
std::string ipaddr_; std::string ipaddr_;
std::string netmask_; std::string netmask_;

View File

@ -292,6 +292,7 @@ const std::string waybar::modules::Network::getNetworkState() const {
#endif #endif
return "disconnected"; return "disconnected";
} }
if (!carrier_) return "disconnected";
if (ipaddr_.empty()) return "linked"; if (ipaddr_.empty()) return "linked";
if (essid_.empty()) return "ethernet"; if (essid_.empty()) return "ethernet";
return "wifi"; return "wifi";
@ -402,6 +403,7 @@ void waybar::modules::Network::clearIface() {
essid_.clear(); essid_.clear();
ipaddr_.clear(); ipaddr_.clear();
netmask_.clear(); netmask_.clear();
carrier_ = false;
cidr_ = 0; cidr_ = 0;
signal_strength_dbm_ = 0; signal_strength_dbm_ = 0;
signal_strength_ = 0; signal_strength_ = 0;
@ -423,6 +425,7 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
struct rtattr *ifla = IFLA_RTA(ifi); struct rtattr *ifla = IFLA_RTA(ifi);
const char *ifname = NULL; const char *ifname = NULL;
size_t ifname_len = 0; size_t ifname_len = 0;
bool carrier = false;
if (net->ifid_ != -1 && ifi->ifi_index != net->ifid_) { if (net->ifid_ != -1 && ifi->ifi_index != net->ifid_) {
return NL_OK; return NL_OK;
@ -434,6 +437,10 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
ifname = static_cast<const char *>(RTA_DATA(ifla)); ifname = static_cast<const char *>(RTA_DATA(ifla));
ifname_len = RTA_PAYLOAD(ifla) - 1; // minus \0 ifname_len = RTA_PAYLOAD(ifla) - 1; // minus \0
break; break;
case IFLA_CARRIER: {
carrier = *(char*)RTA_DATA(ifla) == 1;
break;
}
} }
} }
@ -443,6 +450,7 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
std::string new_ifname (ifname, ifname_len); std::string new_ifname (ifname, ifname_len);
net->ifname_ = new_ifname; net->ifname_ = new_ifname;
} }
net->carrier_ = carrier;
} else if (!is_del_event && net->ifid_ == -1) { } else if (!is_del_event && net->ifid_ == -1) {
// Checking if it's an interface we care about. // Checking if it's an interface we care about.
std::string new_ifname (ifname, ifname_len); std::string new_ifname (ifname, ifname_len);
@ -451,6 +459,7 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
net->ifname_ = new_ifname; net->ifname_ = new_ifname;
net->ifid_ = ifi->ifi_index; net->ifid_ = ifi->ifi_index;
net->carrier_ = carrier;
net->thread_timer_.wake_up(); net->thread_timer_.wake_up();
/* An address for this new interface should be received via an /* An address for this new interface should be received via an
* RTM_NEWADDR event either because we ask for a dump of both links * RTM_NEWADDR event either because we ask for a dump of both links