| 1 | #include <Arduino.h> |
| 2 | #include <WiFi.h> |
| 3 | #include <esp_wifi.h> |
| 4 | |
| 5 | #include "../ui/display.h" |
| 6 | #include "../utils/buttons.h" |
| 7 | |
| 8 | // ===== CONFIG ===== |
| 9 | #define GRAPH_WIDTH 128 |
| 10 | #define GRAPH_HEIGHT 44 |
| 11 | #define GRAPH_TOP 10 |
| 12 | #define MAX_POINTS 128 |
| 13 | #define SPIKE_THRESHOLD 30 |
| 14 | |
| 15 | // ===== STATE ===== |
| 16 | struct SnifferGraph { |
| 17 | uint8_t graphData[MAX_POINTS]; |
| 18 | uint8_t currentChannel = 1; |
| 19 | volatile uint16_t packetCounter = 0; |
| 20 | unsigned long lastChannelSwitch = 0; |
| 21 | unsigned long lastUpdate = 0; |
| 22 | }; |
| 23 | |
| 24 | static SnifferGraph sniffer; |
| 25 | |
| 26 | // ===== CALLBACK ===== |
| 27 | void IRAM_ATTR snifferCallback(void *buf, wifi_promiscuous_pkt_type_t type) |
| 28 | { |
| 29 | if (type == WIFI_PKT_MGMT || type == WIFI_PKT_DATA || type == WIFI_PKT_CTRL) |
| 30 | { |
| 31 | sniffer.packetCounter++; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // ===== INIT ===== |
| 36 | void wifi_analyzer_start() |
| 37 | { |
| 38 | // display init (safe to call again) |
| 39 | u8g2.clearBuffer(); |
| 40 | u8g2.setFont(u8g2_font_5x8_tr); |
| 41 | u8g2.drawStr(0, 10, "Starting analyzer..."); |
| 42 | u8g2.sendBuffer(); |
| 43 | |
| 44 | // reset graph |
| 45 | memset(sniffer.graphData, 0, sizeof(sniffer.graphData)); |
| 46 | sniffer.packetCounter = 0; |
| 47 | sniffer.currentChannel = 1; |
| 48 | |
| 49 | // reset WiFi |
| 50 | WiFi.disconnect(true, true); |
| 51 | esp_wifi_stop(); |
| 52 | delay(200); |
| 53 | esp_wifi_deinit(); |
| 54 | |
| 55 | wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); |
| 56 | esp_wifi_init(&cfg); |
| 57 | esp_wifi_set_storage(WIFI_STORAGE_RAM); |
| 58 | esp_wifi_set_mode(WIFI_MODE_NULL); |
| 59 | esp_wifi_start(); |
| 60 | |
| 61 | esp_wifi_set_channel(sniffer.currentChannel, WIFI_SECOND_CHAN_NONE); |
| 62 | esp_wifi_set_promiscuous_rx_cb(snifferCallback); |
| 63 | esp_wifi_set_promiscuous(true); |
| 64 | } |
| 65 | |
| 66 | // ===== HELPERS ===== |
| 67 | static void switchChannel() |
| 68 | { |
| 69 | sniffer.currentChannel++; |
| 70 | if (sniffer.currentChannel > 13) sniffer.currentChannel = 1; |
| 71 | |
| 72 | esp_wifi_set_channel(sniffer.currentChannel, WIFI_SECOND_CHAN_NONE); |
| 73 | } |
| 74 | |
| 75 | static void updateGraph(uint8_t value) |
| 76 | { |
| 77 | for (int i = 0; i < MAX_POINTS - 1; i++) |
| 78 | { |
| 79 | sniffer.graphData[i] = sniffer.graphData[i + 1]; |
| 80 | } |
| 81 | |
| 82 | sniffer.graphData[MAX_POINTS - 1] = value; |
| 83 | } |
| 84 | |
| 85 | static void drawGraph(uint16_t pktCount) |
| 86 | { |
| 87 | u8g2.clearBuffer(); |
| 88 | u8g2.setFont(u8g2_font_5x8_tr); |
| 89 | |
| 90 | char line1[16]; |
| 91 | char line2[16]; |
| 92 | |
| 93 | sprintf(line1, "Ch:%d", sniffer.currentChannel); |
| 94 | sprintf(line2, "Pkts:%d", pktCount * 5); |
| 95 | |
| 96 | u8g2.drawStr(0, 8, line1); |
| 97 | u8g2.drawStr(60, 8, line2); |
| 98 | |
| 99 | for (int x = 1; x < GRAPH_WIDTH; x++) |
| 100 | { |
| 101 | int y1 = GRAPH_TOP + GRAPH_HEIGHT - sniffer.graphData[x - 1]; |
| 102 | int y2 = GRAPH_TOP + GRAPH_HEIGHT - sniffer.graphData[x]; |
| 103 | |
| 104 | u8g2.drawLine(x - 1, y1, x, y2); |
| 105 | } |
| 106 | |
| 107 | if (pktCount >= SPIKE_THRESHOLD) |
| 108 | { |
| 109 | u8g2.drawVLine(GRAPH_WIDTH / 2, GRAPH_TOP, GRAPH_HEIGHT); |
| 110 | } |
| 111 | |
| 112 | u8g2.sendBuffer(); |
| 113 | } |
| 114 | |
| 115 | // ===== LOOP ===== |
| 116 | void wifi_analyzer_loop() |
| 117 | { |
| 118 | static uint32_t lastPress = 0; |
| 119 | unsigned long now = millis(); |
| 120 | |
| 121 | // channel hopping |
| 122 | if (now - sniffer.lastChannelSwitch >= 1000) |
| 123 | { |
| 124 | sniffer.lastChannelSwitch = now; |
| 125 | switchChannel(); |
| 126 | } |
| 127 | |
| 128 | // graph update |
| 129 | if (now - sniffer.lastUpdate >= 200) |
| 130 | { |
| 131 | sniffer.lastUpdate = now; |
| 132 | |
| 133 | uint16_t pktCount = sniffer.packetCounter; |
| 134 | |
| 135 | uint8_t scaled = pktCount * 2; |
| 136 | uint8_t value = min(scaled, (uint8_t)GRAPH_HEIGHT); |
| 137 | |
| 138 | updateGraph(value); |
| 139 | drawGraph(pktCount); |
| 140 | |
| 141 | sniffer.packetCounter = 0; |
| 142 | } |
| 143 | |
| 144 | // optional: small debounce to not hammer CPU |
| 145 | if (millis() - lastPress < 10) |
| 146 | return; |
| 147 | } |