Skip to content

Commit

Permalink
Added timeout to WiFiScan.cpp to prevent getting stuck at WIFI_SCAN_R…
Browse files Browse the repository at this point in the history
…UNNING (#3197)

* Added timeout to WiFiScan class to prevent haning at stucking at WIFI_SCAN_RUNNING when scan fails internally

* fixed tabs and returns, connected scanTimeout to max_scan_per_channel timeout

* Corrected tabs two

* Added static vars scanTimeout und scanStarted to WiFiScan.h protected section

* Fixed missing ; in line 64
  • Loading branch information
EricMc1289 authored and me-no-dev committed Sep 11, 2019
1 parent cd4f903 commit bab3a70
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 15 additions & 1 deletion libraries/WiFi/src/WiFiScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/


#include "WiFi.h"
#include "WiFiGeneric.h"
#include "WiFiScan.h"
Expand All @@ -42,7 +43,8 @@ extern "C" {
}

bool WiFiScanClass::_scanAsync = false;

uint32_t WiFiScanClass::_scanStarted = 0;
uint32_t WiFiScanClass::_scanTimeout = 10000;
uint16_t WiFiScanClass::_scanCount = 0;
void* WiFiScanClass::_scanResult = 0;

Expand All @@ -58,6 +60,7 @@ int16_t WiFiScanClass::scanNetworks(bool async, bool show_hidden, bool passive,
return WIFI_SCAN_RUNNING;
}

WiFiScanClass::_scanTimeout = max_ms_per_chan * 20;
WiFiScanClass::_scanAsync = async;

WiFi.enableSTA(true);
Expand All @@ -78,6 +81,11 @@ int16_t WiFiScanClass::scanNetworks(bool async, bool show_hidden, bool passive,
config.scan_time.active.max = max_ms_per_chan;
}
if(esp_wifi_scan_start(&config, false) == ESP_OK) {
_scanStarted = millis();
if (!_scanStarted) { //Prevent 0 from millis overflow
++_scanStarted;
}

WiFiGenericClass::clearStatusBits(WIFI_SCAN_DONE_BIT);
WiFiGenericClass::setStatusBits(WIFI_SCANNING_BIT);

Expand Down Expand Up @@ -107,6 +115,7 @@ void WiFiScanClass::_scanDone()
WiFiScanClass::_scanCount = 0;
}
}
WiFiScanClass::_scanStarted=0; //Reset after a scan is completed for normal behavior
WiFiGenericClass::setStatusBits(WIFI_SCAN_DONE_BIT);
WiFiGenericClass::clearStatusBits(WIFI_SCANNING_BIT);
}
Expand All @@ -132,6 +141,11 @@ void * WiFiScanClass::_getScanInfoByIndex(int i)
*/
int16_t WiFiScanClass::scanComplete()
{
if (WiFiScanClass::_scanStarted && (millis()-WiFiScanClass::_scanStarted) > WiFiScanClass::_scanTimeout) { //Check is scan was started and if the delay expired, return WIFI_SCAN_FAILED in this case
WiFiGenericClass::clearStatusBits(WIFI_SCANNING_BIT);
return WIFI_SCAN_FAILED;
}

if(WiFiGenericClass::getStatusBits() & WIFI_SCAN_DONE_BIT) {
return WiFiScanClass::_scanCount;
}
Expand Down
5 changes: 4 additions & 1 deletion libraries/WiFi/src/WiFiScan.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ class WiFiScanClass
protected:

static bool _scanAsync;


static uint32_t _scanStarted;
static uint32_t _scanTimeout;
static uint16_t _scanCount;

static void* _scanResult;

static void * _getScanInfoByIndex(int i);
Expand Down

0 comments on commit bab3a70

Please sign in to comment.