diff --git a/src/globals.h b/src/globals.h index 7fb0cf30..9ce9802c 100644 --- a/src/globals.h +++ b/src/globals.h @@ -55,6 +55,10 @@ extern std::array beacons; #include "gps.h" #endif +#ifdef HAS_LED +#include "led.h" +#endif + #include "payload.h" #ifdef HAS_LORA diff --git a/src/led.cpp b/src/led.cpp index 412b4872..0ade583a 100644 --- a/src/led.cpp +++ b/src/led.cpp @@ -1,5 +1,6 @@ // Basic Config #include "globals.h" +#include "led.h" led_states LEDState = LED_OFF; // LED state global for state machine led_states previousLEDState = diff --git a/src/macsniff.cpp b/src/macsniff.cpp index ecaef4fb..8bc7addd 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -7,19 +7,8 @@ #endif // Local logging tag -static const char TAG[] = "wifi"; +static const char TAG[] = "main"; -/* -static wifi_country_t wifi_country = {WIFI_MY_COUNTRY, WIFI_CHANNEL_MIN, - WIFI_CHANNEL_MAX, 0, - WIFI_COUNTRY_POLICY_MANUAL}; -*/ - -static wifi_country_t wifi_country = {WIFI_MY_COUNTRY, WIFI_CHANNEL_MIN, - WIFI_CHANNEL_MAX, - WIFI_COUNTRY_POLICY_MANUAL}; - -// globals uint16_t salt; uint16_t reset_salt(void) { @@ -129,71 +118,4 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { // True if MAC WiFi/BLE was new return added; // function returns bool if a new and unique Wifi or BLE mac was // counted (true) or not (false) -} - -void wifi_sniffer_init(void) { - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - cfg.nvs_enable = 0; // we don't need any wifi settings from NVRAM - wifi_promiscuous_filter_t filter = { - .filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT}; // we need only MGMT frames - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); // configure Wifi with cfg - ESP_ERROR_CHECK( - esp_wifi_set_country(&wifi_country)); // set locales for RF and channels - ESP_ERROR_CHECK( - esp_wifi_set_storage(WIFI_STORAGE_RAM)); // we don't need NVRAM - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL)); - ESP_ERROR_CHECK( - esp_wifi_set_promiscuous_filter(&filter)); // set MAC frame filter - ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler)); - ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true)); // now switch on monitor mode -} - -void wifi_sniffer_set_channel(uint8_t channel) { - esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); -} - -// using IRAM_:ATTR here to speed up callback function -IRAM_ATTR void wifi_sniffer_packet_handler(void *buff, - wifi_promiscuous_pkt_type_t type) { - const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buff; - const wifi_ieee80211_packet_t *ipkt = - (wifi_ieee80211_packet_t *)ppkt->payload; - const wifi_ieee80211_mac_hdr_t *hdr = &ipkt->hdr; - - if ((cfg.rssilimit) && - (ppkt->rx_ctrl.rssi < cfg.rssilimit)) { // rssi is negative value - ESP_LOGI(TAG, "WiFi RSSI %d -> ignoring (limit: %d)", ppkt->rx_ctrl.rssi, - cfg.rssilimit); - } else { - uint8_t *p = (uint8_t *)hdr->addr2; - mac_add(p, ppkt->rx_ctrl.rssi, MAC_SNIFF_WIFI); - } -} - -void IRAM_ATTR ChannelSwitchIRQ() { - portENTER_CRITICAL(&timerMux); - ChannelTimerIRQ++; - portEXIT_CRITICAL(&timerMux); -} - -// Wifi channel rotation task -void wifi_channel_loop(void *pvParameters) { - - configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check - - while (1) { - - if (ChannelTimerIRQ) { - portENTER_CRITICAL(&timerMux); - ChannelTimerIRQ = 0; - portEXIT_CRITICAL(&timerMux); - // rotates variable channel 1..WIFI_CHANNEL_MAX - channel = (channel % WIFI_CHANNEL_MAX) + 1; - wifi_sniffer_set_channel(channel); - ESP_LOGD(TAG, "Wifi set channel %d", channel); - - vTaskDelay(1 / portTICK_PERIOD_MS); // reset watchdog - } - - } // end of infinite wifi channel rotation loop } \ No newline at end of file diff --git a/src/macsniff.h b/src/macsniff.h index 167486de..1f2c2adb 100644 --- a/src/macsniff.h +++ b/src/macsniff.h @@ -6,35 +6,14 @@ // Hash function for scrambling MAC addresses #include "hash.h" -#include "led.h" #include "senddata.h" #define MAC_SNIFF_WIFI 0 #define MAC_SNIFF_BLE 1 -typedef struct { - unsigned frame_ctrl : 16; - unsigned duration_id : 16; - uint8_t addr1[6]; /* receiver address */ - uint8_t addr2[6]; /* sender address */ - uint8_t addr3[6]; /* filtering address */ - unsigned sequence_ctrl : 16; - uint8_t addr4[6]; /* optional */ -} wifi_ieee80211_mac_hdr_t; - -typedef struct { - wifi_ieee80211_mac_hdr_t hdr; - uint8_t payload[0]; /* network data ended with 4 bytes csum (CRC32) */ -} wifi_ieee80211_packet_t; - uint16_t reset_salt(void); -void wifi_sniffer_init(void); -void wifi_sniffer_set_channel(uint8_t channel); -void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type); -bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type); -void ChannelSwitchIRQ(void); -void wifi_channel_loop(void *pvParameters); uint64_t macConvert(uint8_t *paddr); +bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type); #endif \ No newline at end of file diff --git a/src/main.h b/src/main.h index 280f38b5..89dc0be4 100644 --- a/src/main.h +++ b/src/main.h @@ -1,8 +1,9 @@ #ifndef _MAIN_H #define _MAIN_H -#include "led.h" +//#include "led.h" #include "macsniff.h" +#include "wifiscan.h" #include "configmanager.h" #include "senddata.h" #include "cyclic.h" diff --git a/src/wifiscan.cpp b/src/wifiscan.cpp new file mode 100644 index 00000000..8400117b --- /dev/null +++ b/src/wifiscan.cpp @@ -0,0 +1,77 @@ +// Basic Config +#include "globals.h" +#include "wifiscan.h" + +// Local logging tag +static const char TAG[] = "wifi"; + +static wifi_country_t wifi_country = {WIFI_MY_COUNTRY, WIFI_CHANNEL_MIN, + WIFI_CHANNEL_MAX, 0, + WIFI_COUNTRY_POLICY_MANUAL}; + +// using IRAM_:ATTR here to speed up callback function +IRAM_ATTR void wifi_sniffer_packet_handler(void *buff, + wifi_promiscuous_pkt_type_t type) { + const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buff; + const wifi_ieee80211_packet_t *ipkt = + (wifi_ieee80211_packet_t *)ppkt->payload; + const wifi_ieee80211_mac_hdr_t *hdr = &ipkt->hdr; + + if ((cfg.rssilimit) && + (ppkt->rx_ctrl.rssi < cfg.rssilimit)) { // rssi is negative value + ESP_LOGI(TAG, "WiFi RSSI %d -> ignoring (limit: %d)", ppkt->rx_ctrl.rssi, + cfg.rssilimit); + } else { + uint8_t *p = (uint8_t *)hdr->addr2; + mac_add(p, ppkt->rx_ctrl.rssi, MAC_SNIFF_WIFI); + } +} + +void wifi_sniffer_init(void) { + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + cfg.nvs_enable = 0; // we don't need any wifi settings from NVRAM + wifi_promiscuous_filter_t filter = { + .filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT}; // we need only MGMT frames + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); // configure Wifi with cfg + ESP_ERROR_CHECK( + esp_wifi_set_country(&wifi_country)); // set locales for RF and channels + ESP_ERROR_CHECK( + esp_wifi_set_storage(WIFI_STORAGE_RAM)); // we don't need NVRAM + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL)); + ESP_ERROR_CHECK( + esp_wifi_set_promiscuous_filter(&filter)); // set MAC frame filter + ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler)); + ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true)); // now switch on monitor mode +} + +void wifi_sniffer_set_channel(uint8_t channel) { + esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); +} + +void IRAM_ATTR ChannelSwitchIRQ() { + portENTER_CRITICAL(&timerMux); + ChannelTimerIRQ++; + portEXIT_CRITICAL(&timerMux); +} + +// Wifi channel rotation task +void wifi_channel_loop(void *pvParameters) { + + configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check + + while (1) { + + if (ChannelTimerIRQ) { + portENTER_CRITICAL(&timerMux); + ChannelTimerIRQ = 0; + portEXIT_CRITICAL(&timerMux); + // rotates variable channel 1..WIFI_CHANNEL_MAX + channel = (channel % WIFI_CHANNEL_MAX) + 1; + wifi_sniffer_set_channel(channel); + ESP_LOGD(TAG, "Wifi set channel %d", channel); + + vTaskDelay(1 / portTICK_PERIOD_MS); // reset watchdog + } + + } // end of infinite wifi channel rotation loop +} \ No newline at end of file diff --git a/src/wifiscan.h b/src/wifiscan.h new file mode 100644 index 00000000..d442eccd --- /dev/null +++ b/src/wifiscan.h @@ -0,0 +1,34 @@ +#ifndef _WIFISCAN_H +#define _WIFISCAN_H + +// ESP32 Functions +#include + +// Hash function for scrambling MAC addresses +#include "hash.h" + +#define MAC_SNIFF_WIFI 0 +#define MAC_SNIFF_BLE 1 + +typedef struct { + unsigned frame_ctrl : 16; + unsigned duration_id : 16; + uint8_t addr1[6]; /* receiver address */ + uint8_t addr2[6]; /* sender address */ + uint8_t addr3[6]; /* filtering address */ + unsigned sequence_ctrl : 16; + uint8_t addr4[6]; /* optional */ +} wifi_ieee80211_mac_hdr_t; + +typedef struct { + wifi_ieee80211_mac_hdr_t hdr; + uint8_t payload[0]; /* network data ended with 4 bytes csum (CRC32) */ +} wifi_ieee80211_packet_t; + +void wifi_sniffer_init(void); +void wifi_sniffer_set_channel(uint8_t channel); +void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type); +void ChannelSwitchIRQ(void); +void wifi_channel_loop(void *pvParameters); + +#endif \ No newline at end of file