-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathWiFiMonitor.cpp
More file actions
284 lines (232 loc) · 10.1 KB
/
Copy pathWiFiMonitor.cpp
File metadata and controls
284 lines (232 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "WiFiMonitor.hpp"
#include <algorithm>
#include <cstring>
#include <fstream>
#include <regex>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include "spdlog/spdlog.h"
extern "C" {
#include "osd.h"
}
namespace fs = std::filesystem;
namespace {
/* Realtek drivers register their proc directory under the driver name:
* rtl88x2eu, rtl88x2cu, rtl88x2bu, rtl8812au, ... */
constexpr const char* DRIVER_DIR_PREFIX = "rtl";
void set_tag(osd_tag* tag, const char* key, const std::string& val) {
strncpy(tag->key, key, TAG_MAX_LEN - 1);
strncpy(tag->val, val.c_str(), TAG_MAX_LEN - 1);
tag->key[TAG_MAX_LEN - 1] = '\0';
tag->val[TAG_MAX_LEN - 1] = '\0';
}
std::string trim(const std::string& s) {
const auto begin = s.find_first_not_of(" \t\r\n");
if (begin == std::string::npos) {
return "";
}
return s.substr(begin, s.find_last_not_of(" \t\r\n") - begin + 1);
}
} // namespace
// Constructor implementation
WiFiMonitor::WiFiMonitor()
: base_path_("/proc/net"), last_thermal_read_(), warned_no_driver_(false) {}
// WiFiStats constructor implementation
WiFiMonitor::WiFiStats::WiFiStats() : rssi_a(0), rssi_b(0), rssi_min(0), rssi_percent(0), is_linked(false) {}
std::vector<fs::path> WiFiMonitor::find_interfaces() {
std::vector<fs::path> interfaces;
std::error_code ec;
// directory_iterator(ec) yields end() instead of throwing when /proc/net or a
// driver directory disappears (USB adapter unplugged) while we walk it.
for (const auto& driver : fs::directory_iterator(base_path_, ec)) {
if (!driver.is_directory(ec)) continue;
if (driver.path().filename().string().rfind(DRIVER_DIR_PREFIX, 0) != 0) continue;
for (const auto& interface : fs::directory_iterator(driver.path(), ec)) {
if (interface.is_directory(ec)) {
interfaces.push_back(interface.path());
}
}
}
// The position in this list is published as the `adapter` tag, so sort by
// interface name: readdir order is not guaranteed, and cards may sit below
// different drivers.
std::sort(interfaces.begin(), interfaces.end(),
[](const fs::path& a, const fs::path& b) { return a.filename() < b.filename(); });
return interfaces;
}
/* Drop the whole os_mon.wifi.* namespace when the set of adapters changes.
*
* A card that disappears (unplugged, driver unloaded) simply stops being
* published for, and nothing else retracts its facts, so its widgets would keep
* rendering the last value it ever reported. Worse, the `adapter` tag is the
* position in the list, so adding or removing a card renumbers the ones after
* it and their widgets would show another card's readings until every one of
* them happens to publish again.
*
* The adapters that are still here re-publish in the same run() below, and the
* fact processor applies pending flushes before the facts of the same batch, so
* they don't get cleared by our own flush. */
void WiFiMonitor::flush_if_interfaces_changed(const std::vector<fs::path>& interfaces) {
std::vector<std::string> names;
names.reserve(interfaces.size());
for (const auto& interface : interfaces) {
names.push_back(interface.filename());
}
if (names == known_interfaces_) {
return;
}
known_interfaces_ = std::move(names);
static const char* const prefix = "os_mon.wifi.";
osd_flush_facts(&prefix, 1);
}
void WiFiMonitor::run() {
std::vector<fs::path> interfaces = find_interfaces();
flush_if_interfaces_changed(interfaces);
if (interfaces.empty()) {
if (!warned_no_driver_) {
spdlog::error("No Realtek WiFi driver found below {}, no wifi stats will be published", base_path_);
warned_no_driver_ = true;
}
return;
}
warned_no_driver_ = false;
// Temperature is sampled on its own, slower schedule (see THERMAL_INTERVAL)
const auto now = std::chrono::steady_clock::now();
const bool read_thermal = (now - last_thermal_read_) >= THERMAL_INTERVAL;
if (read_thermal) {
last_thermal_read_ = now;
}
// Initialize batch - estimate 6 facts per interface
void* batch = osd_batch_init(24);
// Collect the stats of every interface of every driver
for (size_t adapter = 0; adapter < interfaces.size(); adapter++) {
const fs::path& interface = interfaces[adapter];
osd_tag base_tags[BASE_TAGS];
make_base_tags(base_tags, interface.filename(), (int)adapter);
std::string debug_file = interface / "trx_info_debug";
if (fs::exists(debug_file)) {
WiFiStats stats = parse_interface_stats(debug_file);
add_interface_stats_to_batch(batch, base_tags, stats);
}
std::string thermal_file = interface / "thermal_state";
if (read_thermal && fs::exists(thermal_file)) {
for (const auto& thermal : parse_thermal_state(thermal_file)) {
add_temperature_fact_to_batch(batch, base_tags, thermal.rf_path, thermal.temperature);
}
}
}
// Publish all collected facts
osd_publish_batch(batch);
}
WiFiMonitor::WiFiStats WiFiMonitor::parse_interface_stats(const std::string& file_path) {
WiFiStats stats;
std::ifstream file(file_path);
if (!file.is_open()) {
return stats;
}
std::string line;
while (std::getline(file, line)) {
// Parse RSSI A and B
if (line.find("rssi_a =") != std::string::npos) {
std::regex rssi_ab_regex(R"(rssi_a\s*=\s*(\d+)\(%\),\s*rssi_b\s*=\s*(\d+)\(%\))");
std::smatch match;
if (std::regex_search(line, match, rssi_ab_regex) && match.size() == 3) {
stats.rssi_a = std::stoi(match[1]);
stats.rssi_b = std::stoi(match[2]);
}
}
// Parse RSSI percentage
else if (line.find("rssi :") != std::string::npos) {
std::regex rssi_regex(R"(rssi\s*:\s*(\d+)\s*\(\%\))");
std::smatch match;
if (std::regex_search(line, match, rssi_regex) && match.size() > 1) {
stats.rssi_percent = std::stoi(match[1]);
}
}
else if (line.find("is_linked =") != std::string::npos) {
std::regex linked_regex(R"(is_linked\s*=\s*(\d+))");
std::smatch match;
if (std::regex_search(line, match, linked_regex) && match.size() > 1) {
stats.is_linked = (std::stoi(match[1]) == 1);
}
}
}
file.close();
return stats;
}
std::vector<WiFiMonitor::ThermalStats> WiFiMonitor::parse_thermal_state(const std::string& file_path) {
// One line per RF path, e.g.
// rf_path: 0, thermal_value: 53, offset: 21, temperature: 112
// Parsed as generic comma-separated "key: value" pairs (like wfb-ng does), so
// additional or reordered fields don't break the parser.
std::vector<ThermalStats> thermals;
std::ifstream file(file_path);
if (!file.is_open()) {
return thermals;
}
std::string line;
while (std::getline(file, line)) {
std::unordered_map<std::string, int> fields;
std::stringstream fields_stream(line);
std::string field;
while (std::getline(fields_stream, field, ',')) {
const auto colon = field.find(':');
if (colon == std::string::npos) continue;
try {
fields[trim(field.substr(0, colon))] = std::stoi(field.substr(colon + 1));
} catch (const std::exception&) {
// Not a numeric field, ignore it
}
}
const auto rf_path = fields.find("rf_path");
const auto temperature = fields.find("temperature");
if (rf_path != fields.end() && temperature != fields.end()) {
thermals.push_back({rf_path->second, temperature->second});
}
}
file.close();
return thermals;
}
void WiFiMonitor::make_base_tags(osd_tag* tags, const std::string& interface_name, int adapter) {
set_tag(&tags[0], "interface", interface_name);
set_tag(&tags[1], "adapter", std::to_string(adapter));
}
void WiFiMonitor::add_interface_stats_to_batch(void* batch, const osd_tag* base_tags, const WiFiStats& stats) {
/* An unlinked adapter (no drone yet, drone powered off, adapter never brought
* up) has no RSSI to report. Going silent leaves the widgets showing the last
* RSSI of a link that is gone, so publish RSSI_NONE instead: it is outside
* every icon range, so IconSelectorWidget hides the bar, and a config that
* wants a visible "no signal" instead can just add a range covering it.
* Retracting per adapter, rather than with osd_flush_facts(), because that is
* scoped by fact-name prefix - it would take the other adapters and the
* temperature down with it. */
const bool linked = stats.is_linked;
// Publish RSSI A
add_rssi_fact_to_batch(batch, base_tags, "rssi_a", linked ? stats.rssi_a : RSSI_NONE);
// Publish RSSI B
add_rssi_fact_to_batch(batch, base_tags, "rssi_b", linked ? stats.rssi_b : RSSI_NONE);
// Publish RSSI Overall Percentage
add_rssi_fact_to_batch(batch, base_tags, "rssi_percent", linked ? stats.rssi_percent : RSSI_NONE);
// Publish connection status
add_rssi_fact_to_batch(batch, base_tags, "connected", linked ? 1 : 0);
}
void WiFiMonitor::add_rssi_fact_to_batch(void* batch, const osd_tag* base_tags, const std::string& rssi_type, int value) {
osd_tag tags[N_TAGS];
// Copy interface and adapter tags
memcpy(tags, base_tags, BASE_TAGS * sizeof(osd_tag));
// Add type tag
set_tag(&tags[BASE_TAGS], "type", rssi_type);
// Add fact to batch
osd_add_int_fact(batch, "os_mon.wifi.rssi", tags, N_TAGS, value);
}
void WiFiMonitor::add_temperature_fact_to_batch(void* batch, const osd_tag* base_tags, int rf_path, int value) {
osd_tag tags[N_TAGS];
// Copy interface and adapter tags
memcpy(tags, base_tags, BASE_TAGS * sizeof(osd_tag));
// Add the RF path the temperature was measured on
set_tag(&tags[BASE_TAGS], "rf_path", std::to_string(rf_path));
// Add fact to batch, degrees C
osd_add_int_fact(batch, "os_mon.wifi.temperature", tags, N_TAGS, value);
}