Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added timeout to WiFiScan.cpp to prevent getting stuck at WIFI_SCAN_RUNNING #3197

Merged
merged 6 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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