2016-06-14 23:32:35 -04:00
|
|
|
#pragma once
|
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
#include <chrono>
|
2016-11-25 02:42:31 -05:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <iwlib.h>
|
|
|
|
|
|
|
|
#ifdef inline
|
|
|
|
#undef inline
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "config.hpp"
|
2016-11-25 07:55:15 -05:00
|
|
|
#include "errors.hpp"
|
2016-12-23 02:13:32 -05:00
|
|
|
#include "utils/math.hpp"
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
namespace chrono = std::chrono;
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
namespace net {
|
|
|
|
DEFINE_ERROR(network_error);
|
|
|
|
|
2016-11-25 07:55:15 -05:00
|
|
|
bool is_wireless_interface(const string& ifname);
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
// types {{{
|
|
|
|
|
2016-10-18 03:34:35 -04:00
|
|
|
struct quality_range {
|
2016-11-14 10:27:58 -05:00
|
|
|
int val{0};
|
|
|
|
int max{0};
|
2016-10-18 03:34:35 -04:00
|
|
|
|
|
|
|
int percentage() const {
|
2016-12-23 02:13:32 -05:00
|
|
|
if (val < 0) {
|
|
|
|
return std::max(std::min(std::abs(math_util::percentage(val, max, -20)), 100), 0);
|
|
|
|
}
|
|
|
|
return std::max(std::min(math_util::percentage(val, 0, max), 100), 0);
|
2016-10-18 03:34:35 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using bytes_t = unsigned int;
|
|
|
|
|
|
|
|
struct link_activity {
|
2016-11-14 10:27:58 -05:00
|
|
|
bytes_t transmitted{0};
|
|
|
|
bytes_t received{0};
|
2016-10-18 03:34:35 -04:00
|
|
|
chrono::system_clock::time_point time;
|
2016-06-14 23:32:35 -04:00
|
|
|
};
|
|
|
|
|
2016-10-18 03:34:35 -04:00
|
|
|
struct link_status {
|
|
|
|
string ip;
|
2016-11-14 10:27:58 -05:00
|
|
|
link_activity previous{};
|
|
|
|
link_activity current{};
|
2016-06-14 23:32:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
2016-11-02 15:22:45 -04:00
|
|
|
// class : network {{{
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
class network {
|
|
|
|
public:
|
2016-11-02 15:22:45 -04:00
|
|
|
explicit network(string interface);
|
|
|
|
virtual ~network();
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-14 06:42:58 -05:00
|
|
|
virtual bool query(bool accumulate = false);
|
2016-10-18 03:34:35 -04:00
|
|
|
virtual bool connected() const = 0;
|
2016-11-02 15:22:45 -04:00
|
|
|
virtual bool ping() const;
|
2016-10-18 03:34:35 -04:00
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
string ip() const;
|
|
|
|
string downspeed(int minwidth = 3) const;
|
|
|
|
string upspeed(int minwidth = 3) const;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-10-18 03:34:35 -04:00
|
|
|
protected:
|
2016-11-14 10:27:58 -05:00
|
|
|
void check_tuntap();
|
2016-11-02 15:22:45 -04:00
|
|
|
bool test_interface() const;
|
|
|
|
string format_speedrate(float bytes_diff, int minwidth) const;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-14 10:27:58 -05:00
|
|
|
int m_socketfd{0};
|
|
|
|
link_status m_status{};
|
2016-06-14 23:32:35 -04:00
|
|
|
string m_interface;
|
2016-11-14 10:27:58 -05:00
|
|
|
bool m_tuntap{false};
|
2016-06-14 23:32:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
2016-11-02 15:22:45 -04:00
|
|
|
// class : wired_network {{{
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
class wired_network : public network {
|
|
|
|
public:
|
2016-10-18 03:34:35 -04:00
|
|
|
explicit wired_network(string interface) : network(interface) {}
|
|
|
|
|
2016-11-14 06:42:58 -05:00
|
|
|
bool query(bool accumulate = false) override;
|
2016-11-02 15:22:45 -04:00
|
|
|
bool connected() const override;
|
|
|
|
string linkspeed() const;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
private:
|
2016-11-14 10:27:58 -05:00
|
|
|
int m_linkspeed{0};
|
2016-06-14 23:32:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
2016-11-02 15:22:45 -04:00
|
|
|
// class : wireless_network {{{
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
class wireless_network : public network {
|
|
|
|
public:
|
2016-10-18 03:34:35 -04:00
|
|
|
wireless_network(string interface) : network(interface) {}
|
|
|
|
|
2016-11-14 06:42:58 -05:00
|
|
|
bool query(bool accumulate = false) override;
|
2016-11-02 15:22:45 -04:00
|
|
|
bool connected() const override;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
string essid() const;
|
|
|
|
int signal() const;
|
|
|
|
int quality() const;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-10-18 03:34:35 -04:00
|
|
|
protected:
|
2016-11-02 15:22:45 -04:00
|
|
|
void query_essid(const int& socket_fd);
|
|
|
|
void query_quality(const int& socket_fd);
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
private:
|
2016-12-14 21:30:41 -05:00
|
|
|
shared_ptr<wireless_info> m_info{};
|
|
|
|
string m_essid{};
|
2016-11-14 10:27:58 -05:00
|
|
|
quality_range m_signalstrength{};
|
|
|
|
quality_range m_linkquality{};
|
2016-06-14 23:32:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
2016-10-18 03:34:35 -04:00
|
|
|
using wireless_t = unique_ptr<wireless_network>;
|
|
|
|
using wired_t = unique_ptr<wired_network>;
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|