From 2437d51d2987f72e7b550fe5a1fc01e772a40839 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Tue, 14 Jan 2020 14:52:24 +0900 Subject: [PATCH] Supports a onConnect exit --- src/AutoConnect.cpp | 13 +++++++++++++ src/AutoConnect.h | 3 +++ 2 files changed, 16 insertions(+) diff --git a/src/AutoConnect.cpp b/src/AutoConnect.cpp index a078a1c..79de6f9 100644 --- a/src/AutoConnect.cpp +++ b/src/AutoConnect.cpp @@ -649,6 +649,14 @@ bool AutoConnect::on(const String& uri, const AuxHandlerFunctionT handler, AutoC return false; } +/** + * Register the exit routine that is being called when WiFi connected. + * @param fn A function of the exit routine. + */ +void AutoConnect::onConnect(ConnectExit_ft fn) { + _onConnectExit = fn; +} + /** * Register the exit routine for the starting captive portal. * @param fn A function of the exit routine. @@ -1115,6 +1123,11 @@ wl_status_t AutoConnect::_waitForConnect(unsigned long timeout) { delay(300); } AC_DBG_DUMB("%s IP:%s\n", wifiStatus == WL_CONNECTED ? "established" : "time out", WiFi.localIP().toString().c_str()); + if (WiFi.status() == WL_CONNECTED) + if (_onConnectExit) { + IPAddress localIP = WiFi.localIP(); + _onConnectExit(localIP); + } return wifiStatus; } diff --git a/src/AutoConnect.h b/src/AutoConnect.h index 1cec25d..c3f6462 100644 --- a/src/AutoConnect.h +++ b/src/AutoConnect.h @@ -262,7 +262,9 @@ class AutoConnect { #endif // !AUTOCONNECT_USE_JSON typedef std::function DetectExit_ft; + typedef std::function ConnectExit_ft; void onDetect(DetectExit_ft fn); + void onConnect(ConnectExit_ft fn); void onNotFound(WebServerClass::THandlerFunction fn); protected: @@ -310,6 +312,7 @@ class AutoConnect { static uint32_t _getFlashChipRealSize(void); static String _toMACAddressString(const uint8_t mac[]); static unsigned int _toWiFiQuality(int32_t rssi); + ConnectExit_ft _onConnectExit; DetectExit_ft _onDetectExit; WebServerClass::THandlerFunction _notFoundHandler; size_t _freeHeapSize;