summaryrefslogtreecommitdiff
path: root/src/wifi/wifi_analyzer.cpp
diff options
context:
space:
mode:
authorkrolyxon <me@krolyxon.com>2026-05-14 23:19:41 +0530
committerkrolyxon <me@krolyxon.com>2026-05-14 23:19:41 +0530
commit58c9c8f51dcec555250195d127a49572c1b9fd9a (patch)
treebbf6f4003fbb36882ee81ed511eec66fd15e735f /src/wifi/wifi_analyzer.cpp
parent206ed229198be252a9ae94342b39618aaab55925 (diff)
add .clang-format, and apply formatting
Diffstat (limited to 'src/wifi/wifi_analyzer.cpp')
-rw-r--r--src/wifi/wifi_analyzer.cpp159
1 files changed, 80 insertions, 79 deletions
diff --git a/src/wifi/wifi_analyzer.cpp b/src/wifi/wifi_analyzer.cpp
index f1c6eb9..2f46b87 100644
--- a/src/wifi/wifi_analyzer.cpp
+++ b/src/wifi/wifi_analyzer.cpp
@@ -14,123 +14,124 @@
// ===== STATE =====
struct SnifferGraph {
- uint8_t graphData[MAX_POINTS];
- uint8_t currentChannel = 1;
- volatile uint16_t packetCounter = 0;
- unsigned long lastChannelSwitch = 0;
- unsigned long lastUpdate = 0;
+ uint8_t graphData[MAX_POINTS];
+ uint8_t currentChannel = 1;
+ volatile uint16_t packetCounter = 0;
+ unsigned long lastChannelSwitch = 0;
+ unsigned long lastUpdate = 0;
};
static SnifferGraph sniffer;
// ===== CALLBACK =====
void IRAM_ATTR snifferCallback(void *buf, wifi_promiscuous_pkt_type_t type) {
- if (type == WIFI_PKT_MGMT || type == WIFI_PKT_DATA || type == WIFI_PKT_CTRL) {
- sniffer.packetCounter++;
- }
+ if (type == WIFI_PKT_MGMT || type == WIFI_PKT_DATA ||
+ type == WIFI_PKT_CTRL) {
+ sniffer.packetCounter++;
+ }
}
// ===== INIT =====
void wifi_analyzer_start() {
- // display init (safe to call again)
- u8g2.clearBuffer();
- u8g2.setFont(u8g2_font_5x8_tr);
- u8g2.drawStr(0, 10, "Starting analyzer...");
- u8g2.sendBuffer();
-
- // reset graph
- memset(sniffer.graphData, 0, sizeof(sniffer.graphData));
- sniffer.packetCounter = 0;
- sniffer.currentChannel = 1;
-
- // reset WiFi
- WiFi.disconnect(true, true);
- esp_wifi_stop();
- delay(200);
- esp_wifi_deinit();
-
- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
- esp_wifi_init(&cfg);
- esp_wifi_set_storage(WIFI_STORAGE_RAM);
- esp_wifi_set_mode(WIFI_MODE_NULL);
- esp_wifi_start();
-
- esp_wifi_set_channel(sniffer.currentChannel, WIFI_SECOND_CHAN_NONE);
- esp_wifi_set_promiscuous_rx_cb(snifferCallback);
- esp_wifi_set_promiscuous(true);
+ // display init (safe to call again)
+ u8g2.clearBuffer();
+ u8g2.setFont(u8g2_font_5x8_tr);
+ u8g2.drawStr(0, 10, "Starting analyzer...");
+ u8g2.sendBuffer();
+
+ // reset graph
+ memset(sniffer.graphData, 0, sizeof(sniffer.graphData));
+ sniffer.packetCounter = 0;
+ sniffer.currentChannel = 1;
+
+ // reset WiFi
+ WiFi.disconnect(true, true);
+ esp_wifi_stop();
+ delay(200);
+ esp_wifi_deinit();
+
+ wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
+ esp_wifi_init(&cfg);
+ esp_wifi_set_storage(WIFI_STORAGE_RAM);
+ esp_wifi_set_mode(WIFI_MODE_NULL);
+ esp_wifi_start();
+
+ esp_wifi_set_channel(sniffer.currentChannel, WIFI_SECOND_CHAN_NONE);
+ esp_wifi_set_promiscuous_rx_cb(snifferCallback);
+ esp_wifi_set_promiscuous(true);
}
// ===== HELPERS =====
static void switchChannel() {
- sniffer.currentChannel++;
- if (sniffer.currentChannel > 13)
- sniffer.currentChannel = 1;
+ sniffer.currentChannel++;
+ if (sniffer.currentChannel > 13)
+ sniffer.currentChannel = 1;
- esp_wifi_set_channel(sniffer.currentChannel, WIFI_SECOND_CHAN_NONE);
+ esp_wifi_set_channel(sniffer.currentChannel, WIFI_SECOND_CHAN_NONE);
}
static void updateGraph(uint8_t value) {
- for (int i = 0; i < MAX_POINTS - 1; i++) {
- sniffer.graphData[i] = sniffer.graphData[i + 1];
- }
+ for (int i = 0; i < MAX_POINTS - 1; i++) {
+ sniffer.graphData[i] = sniffer.graphData[i + 1];
+ }
- sniffer.graphData[MAX_POINTS - 1] = value;
+ sniffer.graphData[MAX_POINTS - 1] = value;
}
static void drawGraph(uint16_t pktCount) {
- u8g2.clearBuffer();
- u8g2.setFont(u8g2_font_5x8_tr);
+ u8g2.clearBuffer();
+ u8g2.setFont(u8g2_font_5x8_tr);
- char line1[16];
- char line2[16];
+ char line1[16];
+ char line2[16];
- sprintf(line1, "Ch:%d", sniffer.currentChannel);
- sprintf(line2, "Pkts:%d", pktCount * 5);
+ sprintf(line1, "Ch:%d", sniffer.currentChannel);
+ sprintf(line2, "Pkts:%d", pktCount * 5);
- u8g2.drawStr(0, 8, line1);
- u8g2.drawStr(60, 8, line2);
+ u8g2.drawStr(0, 8, line1);
+ u8g2.drawStr(60, 8, line2);
- for (int x = 1; x < GRAPH_WIDTH; x++) {
- int y1 = GRAPH_TOP + GRAPH_HEIGHT - sniffer.graphData[x - 1];
- int y2 = GRAPH_TOP + GRAPH_HEIGHT - sniffer.graphData[x];
+ for (int x = 1; x < GRAPH_WIDTH; x++) {
+ int y1 = GRAPH_TOP + GRAPH_HEIGHT - sniffer.graphData[x - 1];
+ int y2 = GRAPH_TOP + GRAPH_HEIGHT - sniffer.graphData[x];
- u8g2.drawLine(x - 1, y1, x, y2);
- }
+ u8g2.drawLine(x - 1, y1, x, y2);
+ }
- if (pktCount >= SPIKE_THRESHOLD) {
- u8g2.drawVLine(GRAPH_WIDTH / 2, GRAPH_TOP, GRAPH_HEIGHT);
- }
+ if (pktCount >= SPIKE_THRESHOLD) {
+ u8g2.drawVLine(GRAPH_WIDTH / 2, GRAPH_TOP, GRAPH_HEIGHT);
+ }
- u8g2.sendBuffer();
+ u8g2.sendBuffer();
}
// ===== LOOP =====
void wifi_analyzer_loop() {
- static uint32_t lastPress = 0;
- unsigned long now = millis();
+ static uint32_t lastPress = 0;
+ unsigned long now = millis();
- // channel hopping
- if (now - sniffer.lastChannelSwitch >= 1000) {
- sniffer.lastChannelSwitch = now;
- switchChannel();
- }
+ // channel hopping
+ if (now - sniffer.lastChannelSwitch >= 1000) {
+ sniffer.lastChannelSwitch = now;
+ switchChannel();
+ }
- // graph update
- if (now - sniffer.lastUpdate >= 200) {
- sniffer.lastUpdate = now;
+ // graph update
+ if (now - sniffer.lastUpdate >= 200) {
+ sniffer.lastUpdate = now;
- uint16_t pktCount = sniffer.packetCounter;
+ uint16_t pktCount = sniffer.packetCounter;
- uint8_t scaled = pktCount * 2;
- uint8_t value = min(scaled, (uint8_t)GRAPH_HEIGHT);
+ uint8_t scaled = pktCount * 2;
+ uint8_t value = min(scaled, (uint8_t)GRAPH_HEIGHT);
- updateGraph(value);
- drawGraph(pktCount);
+ updateGraph(value);
+ drawGraph(pktCount);
- sniffer.packetCounter = 0;
- }
+ sniffer.packetCounter = 0;
+ }
- // optional: small debounce to not hammer CPU
- if (millis() - lastPress < 10)
- return;
+ // optional: small debounce to not hammer CPU
+ if (millis() - lastPress < 10)
+ return;
}