WifiStatistics.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 #ifndef HH_SENF_Ext_NetEmu_WLAN_WifiStatistics_
15 #define HH_SENF_Ext_NetEmu_WLAN_WifiStatistics_ 1
16 
17 // Custom includes
18 extern "C" {
19 # include <sys/stat.h>
20 }
21 #include <boost/unordered/unordered_map.hpp>
22 #include <boost/property_tree/ptree.hpp>
26 #include <senf/Utils/Statistics.hh>
27 
29 
30 
31 namespace senf {
32 
33  class mmapFile
34  {
35  public:
36  mmapFile (std::string const & fname);
37  ~mmapFile ();
38 
39  void *open(std::string const & fname);
40 
41  void *begin() const;
42  void *end() const;
43  off_t size() const;
44  bool eof() const;
45 
46  template <class T>
47  T const & next() {
48  if ((next_ + sizeof(T)) > end())
49  throw std::exception();
50  T* t ((T*) ((void*)next_));
51  next_ += sizeof(T);
52  return (T const &) *t;
53  }
54 
55  std::uint8_t *next(size_t const & len) {
56  if ((next_ + len) > end())
57  throw std::exception();
58  std::uint8_t *t ((std::uint8_t*)next_);
59  next_ += len;
60  return t;
61  }
62 
63  template <class T>
64  T const & at(off_t const & pos) {
65  if ((begin_ + pos + sizeof(T)) > end())
66  throw std::exception();
67  T* t ((T*) ((void*)(begin_ + pos)));
68  return (T const &) *t;
69  }
70 
71  private:
72  int fd_;
73  struct stat stat_;
74  std::uint8_t *begin_, *end_, *next_, *buffer_;
75  };
76 
77 namespace emu {
78 
79  struct StatsDataCollectorKernel {
80  std::int32_t sum;
81  std::int32_t min, max;
82  std::uint32_t count;
83  std::uint64_t sum2;
84  } __attribute__((packed));
85 
87  std::uint32_t rx_packets;
88  std::uint32_t rx_bytes;
89  std::uint32_t bad_fcs_packets;
90  std::uint32_t bad_fcs_bytes;
91  std::uint32_t rTx_packets;
92  std::uint32_t rTx_bytes;
93  std::uint32_t air_time;
94  } __attribute__((packed));
95 
96  struct WifiStatsKernel {
102  std::uint32_t lastSeen;
103  std::uint32_t type;
104  std::uint8_t mac[6];
105  std::uint8_t bssid[6];
106  char ssid[36];
107  } __attribute__((packed));
108 
110  enum Type {UNKNOWN, AP, STA, IBSS, MESH};
111 
116  std::uint32_t total;
117  std::uint32_t totalBytes;
118  std::uint32_t badFCS;
119  std::uint32_t badFCSBytes;
120  std::uint32_t rTx;
121  std::uint32_t rTxBytes;
122  std::uint32_t airTime;
125  std::string ssId;
127 
129  total = totalBytes = badFCS = badFCSBytes = rTx = rTxBytes = airTime = 0;
130  lastSeen = senf::ClockService::clock_type(0);
131  };
132 
134  };
135 
136  typedef boost::unordered_map<senf::MACAddress,WifiStatisticsData> WifiStatisticsMap;
137 
139  public:
140 
141  WifiStatistics (std::string ifName, std::string debugFS = "/sys/kernel/debug");
142  ~WifiStatistics();
143 
144  bool enable(bool on = true);
145  bool pollStatistics(std::uint32_t tag, senf::ClockService::clock_type const & maxAge);
146  bool pollStatisticsCSV(std::uint32_t tag, senf::ClockService::clock_type const & maxAge);
147  bool pollStatisticsBIN(std::uint32_t tag, senf::ClockService::clock_type const & maxAge);
148  WifiStatisticsMap const & map() const;
149  ClockService::clock_type const & timestamp() const;
150  std::uint32_t tag() const;
151  std::uint32_t invalidEntries() const;
152  std::uint32_t ioErrors() const;
153 
154  WifiStatisticsMap const & statisticsMap(std::uint32_t tag, senf::ClockService::clock_type const & maxAge);
155 
156  private:
157  std::string debugFsPath_;
158  WifiStatisticsMap map_;
159  ClockService::clock_type timestamp_;
160  std::uint32_t tag_;
161  std::uint32_t invalidEntries_;
162  std::uint32_t ioErrors_;
163  };
164 
165 }
166 }
167 
169 //#include "WifiStatistics.cci"
170 //#include "WifiStatistics.ct"
171 //#include "WifiStatistics.cti"
172 #endif
config::time_type clock_type
std::uint8_t * next(size_t const &len)
senf::StatisticsData bitrate
std::uint8_t mac[6]
T const & next()
mmapFile(std::string const &fname)
senf::StatisticsData signal
char ssid[36]
std::int32_t min
StatsDataCollectorKernel bitrateNonData
std::int32_t max
senf::StatisticsData signalNonData
std::uint64_t sum2
std::uint8_t bssid[6]
struct senf::emu::StatsDataCollectorKernel __attribute__((packed))
bool eof() const
void * begin() const
off_t size() const
boost::unordered_map< senf::MACAddress, WifiStatisticsData > WifiStatisticsMap
StatsDataCollectorKernel signalNonData
std::uint32_t count
T const & at(off_t const &pos)
void * open(std::string const &fname)
senf::ClockService::clock_type lastSeen
StatsDataPktCountsKernel pktCounts
senf::StatisticsData bitrateNonData
StatsDataCollectorKernel bitrate
std::int32_t sum
void * end() const
StatsDataCollectorKernel signal