waybar/include/modules/network.hpp

75 lines
2.1 KiB
C++
Raw Normal View History

2018-08-09 16:38:24 +02:00
#pragma once
#include <arpa/inet.h>
2019-04-18 17:52:00 +02:00
#include <fmt/format.h>
#include <ifaddrs.h>
2018-08-09 16:38:24 +02:00
#include <linux/nl80211.h>
2019-04-18 17:52:00 +02:00
#include <net/if.h>
#include <netlink/genl/ctrl.h>
#include <netlink/genl/genl.h>
#include <netlink/netlink.h>
2018-12-26 11:13:36 +01:00
#include <sys/epoll.h>
#include "ALabel.hpp"
2019-04-18 17:52:00 +02:00
#include "util/sleeper_thread.hpp"
2018-08-09 16:38:24 +02:00
namespace waybar::modules {
class Network : public ALabel {
2019-04-18 17:52:00 +02:00
public:
Network(const std::string&, const Json::Value&);
~Network();
auto update() -> void;
private:
static const uint8_t MAX_RETRY = 5;
static const uint8_t EPOLL_MAX = 200;
static int handleEvents(struct nl_msg*, void*);
static int handleScan(struct nl_msg*, void*);
void worker();
void createInfoSocket();
void createEventSocket();
int getExternalInterface(int skip_idx = -1);
2019-04-18 17:52:00 +02:00
void getInterfaceAddress();
int netlinkRequest(void*, uint32_t, uint32_t groups = 0);
int netlinkResponse(void*, uint32_t, uint32_t groups = 0);
void parseEssid(struct nlattr**);
void parseSignal(struct nlattr**);
2019-05-17 11:27:38 +02:00
void parseFreq(struct nlattr**);
2019-04-18 17:52:00 +02:00
bool associatedOrJoined(struct nlattr**);
2019-05-18 13:57:35 +02:00
bool checkInterface(struct ifinfomsg *rtif, std::string name);
int getPreferredIface(int skip_idx = -1);
2019-04-18 17:52:00 +02:00
auto getInfo() -> void;
void clearIface();
2019-05-16 11:22:22 +02:00
bool wildcardMatch(const std::string& pattern, const std::string& text);
2019-04-18 17:52:00 +02:00
waybar::util::SleeperThread thread_;
waybar::util::SleeperThread thread_timer_;
int ifid_;
2019-05-16 11:22:22 +02:00
int last_ext_iface_;
2019-04-18 17:52:00 +02:00
sa_family_t family_;
struct sockaddr_nl nladdr_ = {0};
2019-05-16 11:22:22 +02:00
struct nl_sock* sock_ = nullptr;
struct nl_sock* ev_sock_ = nullptr;
2019-04-18 17:52:00 +02:00
int efd_;
int ev_fd_;
int nl80211_id_;
2019-05-16 11:22:22 +02:00
std::mutex mutex_;
2019-04-18 17:52:00 +02:00
unsigned long long bandwidth_down_total_;
unsigned long long bandwidth_up_total_;
2019-04-18 17:52:00 +02:00
std::string essid_;
std::string ifname_;
std::string ipaddr_;
std::string netmask_;
int cidr_;
2019-05-16 11:22:22 +02:00
bool linked_;
2019-04-18 17:52:00 +02:00
int32_t signal_strength_dbm_;
uint8_t signal_strength_;
2019-05-17 11:27:38 +02:00
uint32_t frequency_;
2018-08-16 14:29:41 +02:00
};
2018-08-09 16:38:24 +02:00
2019-04-18 17:52:00 +02:00
} // namespace waybar::modules