From da17d5425a275bca2d891603ac5764b4fb6fbaff Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 14 Jun 2016 13:09:46 +0800 Subject: [PATCH] Fix regression in WiFi.onEvent, add testcase (thanks @everslick) --- .../ESP8266WiFi/src/ESP8266WiFiGeneric.cpp | 1 + .../test_WiFi_events/test_WiFi_events.ino | 130 ++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 tests/device/test_WiFi_events/test_WiFi_events.ino diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index 362929f07..f89cf17de 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -92,6 +92,7 @@ void ESP8266WiFiGenericClass::onEvent(WiFiEventCb f, WiFiEvent_t event) (*f)(static_cast(e->event)); }); handler->mCanExpire = false; + sCbEventList.push_back(handler); } WiFiEventHandler ESP8266WiFiGenericClass::onStationModeConnected(std::function f) diff --git a/tests/device/test_WiFi_events/test_WiFi_events.ino b/tests/device/test_WiFi_events/test_WiFi_events.ino new file mode 100644 index 000000000..3344f24ab --- /dev/null +++ b/tests/device/test_WiFi_events/test_WiFi_events.ino @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +#include +#include + +BS_ENV_DECLARE(); + +void setup() +{ + Serial.begin(115200); + Serial.setDebugOutput(false); + WiFi.persistent(false); + WiFi.mode(WIFI_OFF); + BS_RUN(Serial); +} + +static std::map sEventsReceived; + +static void onWiFiEvent(WiFiEvent_t event) +{ + sEventsReceived[event]++; +} + +TEST_CASE("WiFi.onEvent is called for specific events", "[wifi][events]") +{ + sEventsReceived[WIFI_EVENT_STAMODE_CONNECTED] = 0; + sEventsReceived[WIFI_EVENT_STAMODE_DISCONNECTED] = 0; + sEventsReceived[WIFI_EVENT_STAMODE_GOT_IP] = 0; + + WiFi.onEvent(onWiFiEvent, WIFI_EVENT_STAMODE_CONNECTED); + WiFi.onEvent(onWiFiEvent, WIFI_EVENT_STAMODE_DISCONNECTED); + WiFi.onEvent(onWiFiEvent, WIFI_EVENT_STAMODE_GOT_IP); + WiFi.onEvent(onWiFiEvent, WIFI_EVENT_ANY); + + WiFi.mode(WIFI_STA); + WiFi.begin(STA_SSID, STA_PASS); + unsigned long start = millis(); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + REQUIRE(millis() - start < 5000); + } + WiFi.disconnect(); + delay(100); + WiFi.mode(WIFI_OFF); + REQUIRE(sEventsReceived[WIFI_EVENT_STAMODE_CONNECTED] == 2); + REQUIRE(sEventsReceived[WIFI_EVENT_STAMODE_DISCONNECTED] >= 2 && sEventsReceived[WIFI_EVENT_STAMODE_DISCONNECTED] % 2 == 0); + REQUIRE(sEventsReceived[WIFI_EVENT_STAMODE_GOT_IP] == 2); +} + +TEST_CASE("STA mode events are called both when using DHCP and static config", "[wifi][events]") +{ + String events; + + auto handler1 = WiFi.onStationModeConnected([&](const WiFiEventStationModeConnected& evt){ + events += "connected,"; + }); + auto handler2 = WiFi.onStationModeDisconnected([&](const WiFiEventStationModeDisconnected& evt){ + if (events.length()) { + events += "disconnected,"; + } + }); + auto handler3 = WiFi.onStationModeGotIP([&](const WiFiEventStationModeGotIP& evt){ + events += "got_ip,"; + }); + + // run the test with DHCP + WiFi.mode(WIFI_STA); + WiFi.begin(STA_SSID, STA_PASS); + unsigned long start = millis(); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + REQUIRE(millis() - start < 5000); + } + // save IP config + IPAddress localIP = WiFi.localIP(); + IPAddress subnetMask = WiFi.subnetMask(); + IPAddress gatewayIP = WiFi.gatewayIP(); + WiFi.disconnect(); + delay(100); + + REQUIRE(events == "connected,got_ip,disconnected,"); + events = String(); + + // now run the same with static IP config saved above + + WiFi.mode(WIFI_STA); + WiFi.config(localIP, gatewayIP, subnetMask); + WiFi.begin(STA_SSID, STA_PASS); + start = millis(); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + REQUIRE(millis() - start < 5000); + } + WiFi.disconnect(); + delay(100); + WiFi.mode(WIFI_OFF); + REQUIRE(events == "connected,got_ip,disconnected,"); +} + +TEST_CASE("Events are not called if handler is deleted", "[wifi][events]") +{ + String events; + + WiFi.onStationModeConnected([&](const WiFiEventStationModeConnected& evt){ + events += "connected,"; + }); + WiFi.onStationModeDisconnected([&](const WiFiEventStationModeDisconnected& evt){ + events += "disconnected,"; + }); + WiFi.onStationModeGotIP([&](const WiFiEventStationModeGotIP& evt){ + events += "got_ip,"; + }); + + WiFi.mode(WIFI_STA); + WiFi.begin(STA_SSID, STA_PASS); + unsigned long start = millis(); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + REQUIRE(millis() - start < 5000); + } + WiFi.disconnect(); + delay(100); + + REQUIRE(events == ""); +} + +void loop() {}