From 28d30493fde556cee758891049d79ea313a35434 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Thu, 10 Dec 2020 22:12:57 +0100 Subject: [PATCH] channel hopping switch on/off option --- README.md | 5 +++-- include/wifiscan.h | 2 ++ src/main.cpp | 4 ++-- src/rcommand.cpp | 23 ++++++++++++++++------- src/wifiscan.cpp | 19 +++++++++++++------ 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index a5f01596..658782f5 100644 --- a/README.md +++ b/README.md @@ -303,7 +303,7 @@ Hereafter described is the default *plain* format, which uses MSB bit numbering. byte 6: Counter mode (0=cyclic unconfirmed, 1=cumulative, 2=cyclic confirmed) [default 0] bytes 7-8: RSSI limiter threshold value (negative) [default 0] byte 9: Lora Payload send cycle in seconds/2 (0..255) [default 120] - byte 10: Wifi channel switch interval in seconds/100 (0..255) [default 50] + byte 10: Wifi channel hopping interval in seconds/100 (0..255), 0 means no hopping [default 50] byte 11: Bluetooth channel switch interval in seconds/100 (0..255) [efault 10] byte 12: Bluetooth scanner status (1=on, 0=0ff) [default 1] byte 13: Wifi antenna switch (0=internal, 1=external) [default 0] @@ -437,10 +437,11 @@ Send for example `8386` as Downlink on Port 2 to get battery status and time/dat 0 ... 255 payload send cycle in seconds/2 e.g. 120 -> payload is transmitted each 240 seconds [default] -0x0B set Wifi channel switch interval timer +0x0B set Wifi channel hopping interval timer 0 ... 255 duration for scanning a wifi channel in seconds/100 e.g. 50 -> each channel is scanned for 500 milliseconds [default] + 0 means no hopping, scanning on channel WIFI_CHANNEL_MIN only 0x0C set Bluetooth channel switch interval timer diff --git a/include/wifiscan.h b/include/wifiscan.h index 0b212a9d..53a414c3 100644 --- a/include/wifiscan.h +++ b/include/wifiscan.h @@ -8,6 +8,8 @@ #include "antenna.h" // code for switching wifi antennas #include "macsniff.h" +extern TimerHandle_t WifiChanTimer; + void wifi_sniffer_init(void); void switch_wifi_sniffer(uint8_t state); void IRAM_ATTR wifi_sniffer_packet_handler(void *buff, diff --git a/src/main.cpp b/src/main.cpp index 65e13492..ffaa544c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -88,8 +88,8 @@ triggers pps 1 sec impulse configData_t cfg; // struct holds current device configuration char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer for LMIC event message uint8_t batt_level = 0; // display value -uint8_t volatile channel = 0; // channel rotation counter -uint8_t volatile rf_load = 0; // RF traffic indicator +uint8_t volatile channel = WIFI_CHANNEL_MIN; // channel rotation counter +uint8_t volatile rf_load = 0; // RF traffic indicator uint16_t volatile macs_wifi = 0, macs_ble = 0; // globals for display hw_timer_t *ppsIRQ = NULL, *displayIRQ = NULL, *matrixDisplayIRQ = NULL; diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 0295fab1..791346e2 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -63,11 +63,20 @@ void set_sleepcycle(uint8_t val[]) { void set_wifichancycle(uint8_t val[]) { cfg.wifichancycle = val[0]; // update Wifi channel rotation timer period - xTimerChangePeriod(WifiChanTimer, pdMS_TO_TICKS(cfg.wifichancycle * 10), 100); - + if (cfg.wifichancycle > 0) { + if (xTimerIsTimerActive(WifiChanTimer) == pdFALSE) + xTimerStart(WifiChanTimer, (TickType_t) 0); + xTimerChangePeriod(WifiChanTimer, pdMS_TO_TICKS(cfg.wifichancycle * 10), + 100); ESP_LOGI(TAG, - "Remote command: set Wifi channel switch interval to %.1f seconds", + "Remote command: set Wifi channel hopping interval to %.1f seconds", cfg.wifichancycle / float(100)); + } else { + xTimerStop(WifiChanTimer, (TickType_t) 0); + esp_wifi_set_channel(WIFI_CHANNEL_MIN, WIFI_SECOND_CHAN_NONE); + channel = WIFI_CHANNEL_MIN; + ESP_LOGI(TAG, "Remote command: set Wifi channel hopping to off"); + } } void set_blescantime(uint8_t val[]) { @@ -404,10 +413,10 @@ void rcommand(const uint8_t cmd[], const uint8_t cmdlength) { table[i].func( foundcmd); // execute assigned function with given parameters } else - ESP_LOGI( - TAG, - "Remote command x%02X called with missing parameter(s), skipped", - table[i].opcode); + ESP_LOGI(TAG, + "Remote command x%02X called with missing parameter(s), " + "skipped", + table[i].opcode); break; // command found -> exit table lookup loop } // end of command validation } // end of command table lookup loop diff --git a/src/wifiscan.cpp b/src/wifiscan.cpp index d6325fcd..3dc1bfeb 100644 --- a/src/wifiscan.cpp +++ b/src/wifiscan.cpp @@ -41,8 +41,6 @@ IRAM_ATTR void wifi_sniffer_packet_handler(void *buff, // Software-timer driven Wifi channel rotation callback function void switchWifiChannel(TimerHandle_t xTimer) { - // static uint8_t channel = 0; // channel rotation counter - _ASSERT(xTimer != NULL); channel = (channel % WIFI_CHANNEL_MAX) + 1; // rotate channel 1..WIFI_CHANNEL_MAX esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); @@ -74,20 +72,29 @@ void wifi_sniffer_init(void) { // setup wifi channel rotation timer WifiChanTimer = - xTimerCreate("WifiChannelTimer", pdMS_TO_TICKS(cfg.wifichancycle * 10), + xTimerCreate("WifiChannelTimer", + (cfg.wifichancycle > 0) ? pdMS_TO_TICKS(cfg.wifichancycle) + : pdMS_TO_TICKS(50), pdTRUE, (void *)0, switchWifiChannel); + if (cfg.wifichancycle > 0) + xTimerStart(WifiChanTimer, (TickType_t) 0); + else + esp_wifi_set_channel(WIFI_CHANNEL_MIN, WIFI_SECOND_CHAN_NONE); } void switch_wifi_sniffer(uint8_t state) { - _ASSERT(WifiChanTimer != NULL); if (state) { // switch wifi sniffer on ESP_ERROR_CHECK(esp_wifi_start()); - xTimerStart(WifiChanTimer, 0); + if (cfg.wifichancycle > 0) + xTimerStart(WifiChanTimer, (TickType_t) 0); + else + esp_wifi_set_channel(WIFI_CHANNEL_MIN, WIFI_SECOND_CHAN_NONE); esp_wifi_set_promiscuous(true); } else { // switch wifi sniffer off - xTimerStop(WifiChanTimer, 0); + if (xTimerIsTimerActive(WifiChanTimer) != pdFALSE) + xTimerStop(WifiChanTimer, (TickType_t) 0); esp_wifi_set_promiscuous(false); ESP_ERROR_CHECK(esp_wifi_stop()); macs_wifi = 0; // clear WIFI counter